Daily bump.
[official-gcc.git] / gcc / tree-vrp.c
blob26652e3b048a5fa8dbeb400f73f37748f2408d3f
1 /* Support routines for Value Range Propagation (VRP).
2 Copyright (C) 2005-2017 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-general.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);
370 if (flag_checking
371 && (t == VR_UNDEFINED || t == VR_VARYING))
373 gcc_assert (min == NULL_TREE && max == NULL_TREE);
374 gcc_assert (equiv == NULL || bitmap_empty_p (equiv));
377 vr->type = t;
378 vr->min = min;
379 vr->max = max;
381 /* Since updating the equivalence set involves deep copying the
382 bitmaps, only do it if absolutely necessary. */
383 if (vr->equiv == NULL
384 && equiv != NULL)
385 vr->equiv = BITMAP_ALLOC (&vrp_equiv_obstack);
387 if (equiv != vr->equiv)
389 if (equiv && !bitmap_empty_p (equiv))
390 bitmap_copy (vr->equiv, equiv);
391 else
392 bitmap_clear (vr->equiv);
397 /* Set value range VR to the canonical form of {T, MIN, MAX, EQUIV}.
398 This means adjusting T, MIN and MAX representing the case of a
399 wrapping range with MAX < MIN covering [MIN, type_max] U [type_min, MAX]
400 as anti-rage ~[MAX+1, MIN-1]. Likewise for wrapping anti-ranges.
401 In corner cases where MAX+1 or MIN-1 wraps this will fall back
402 to varying.
403 This routine exists to ease canonicalization in the case where we
404 extract ranges from var + CST op limit. */
406 static void
407 set_and_canonicalize_value_range (value_range *vr, enum value_range_type t,
408 tree min, tree max, bitmap equiv)
410 /* Use the canonical setters for VR_UNDEFINED and VR_VARYING. */
411 if (t == VR_UNDEFINED)
413 set_value_range_to_undefined (vr);
414 return;
416 else if (t == VR_VARYING)
418 set_value_range_to_varying (vr);
419 return;
422 /* Nothing to canonicalize for symbolic ranges. */
423 if (TREE_CODE (min) != INTEGER_CST
424 || TREE_CODE (max) != INTEGER_CST)
426 set_value_range (vr, t, min, max, equiv);
427 return;
430 /* Wrong order for min and max, to swap them and the VR type we need
431 to adjust them. */
432 if (tree_int_cst_lt (max, min))
434 tree one, tmp;
436 /* For one bit precision if max < min, then the swapped
437 range covers all values, so for VR_RANGE it is varying and
438 for VR_ANTI_RANGE empty range, so drop to varying as well. */
439 if (TYPE_PRECISION (TREE_TYPE (min)) == 1)
441 set_value_range_to_varying (vr);
442 return;
445 one = build_int_cst (TREE_TYPE (min), 1);
446 tmp = int_const_binop (PLUS_EXPR, max, one);
447 max = int_const_binop (MINUS_EXPR, min, one);
448 min = tmp;
450 /* There's one corner case, if we had [C+1, C] before we now have
451 that again. But this represents an empty value range, so drop
452 to varying in this case. */
453 if (tree_int_cst_lt (max, min))
455 set_value_range_to_varying (vr);
456 return;
459 t = t == VR_RANGE ? VR_ANTI_RANGE : VR_RANGE;
462 /* Anti-ranges that can be represented as ranges should be so. */
463 if (t == VR_ANTI_RANGE)
465 bool is_min = vrp_val_is_min (min);
466 bool is_max = vrp_val_is_max (max);
468 if (is_min && is_max)
470 /* We cannot deal with empty ranges, drop to varying.
471 ??? This could be VR_UNDEFINED instead. */
472 set_value_range_to_varying (vr);
473 return;
475 else if (TYPE_PRECISION (TREE_TYPE (min)) == 1
476 && (is_min || is_max))
478 /* Non-empty boolean ranges can always be represented
479 as a singleton range. */
480 if (is_min)
481 min = max = vrp_val_max (TREE_TYPE (min));
482 else
483 min = max = vrp_val_min (TREE_TYPE (min));
484 t = VR_RANGE;
486 else if (is_min
487 /* As a special exception preserve non-null ranges. */
488 && !(TYPE_UNSIGNED (TREE_TYPE (min))
489 && integer_zerop (max)))
491 tree one = build_int_cst (TREE_TYPE (max), 1);
492 min = int_const_binop (PLUS_EXPR, max, one);
493 max = vrp_val_max (TREE_TYPE (max));
494 t = VR_RANGE;
496 else if (is_max)
498 tree one = build_int_cst (TREE_TYPE (min), 1);
499 max = int_const_binop (MINUS_EXPR, min, one);
500 min = vrp_val_min (TREE_TYPE (min));
501 t = VR_RANGE;
505 /* Do not drop [-INF(OVF), +INF(OVF)] to varying. (OVF) has to be sticky
506 to make sure VRP iteration terminates, otherwise we can get into
507 oscillations. */
509 set_value_range (vr, t, min, max, equiv);
512 /* Copy value range FROM into value range TO. */
514 static inline void
515 copy_value_range (value_range *to, value_range *from)
517 set_value_range (to, from->type, from->min, from->max, from->equiv);
520 /* Set value range VR to a single value. This function is only called
521 with values we get from statements, and exists to clear the
522 TREE_OVERFLOW flag so that we don't think we have an overflow
523 infinity when we shouldn't. */
525 static inline void
526 set_value_range_to_value (value_range *vr, tree val, bitmap equiv)
528 gcc_assert (is_gimple_min_invariant (val));
529 if (TREE_OVERFLOW_P (val))
530 val = drop_tree_overflow (val);
531 set_value_range (vr, VR_RANGE, val, val, equiv);
534 /* Set value range VR to a non-negative range of type TYPE.
535 OVERFLOW_INFINITY indicates whether to use an overflow infinity
536 rather than TYPE_MAX_VALUE; this should be true if we determine
537 that the range is nonnegative based on the assumption that signed
538 overflow does not occur. */
540 static inline void
541 set_value_range_to_nonnegative (value_range *vr, tree type,
542 bool overflow_infinity)
544 tree zero;
546 if (overflow_infinity && !supports_overflow_infinity (type))
548 set_value_range_to_varying (vr);
549 return;
552 zero = build_int_cst (type, 0);
553 set_value_range (vr, VR_RANGE, zero,
554 (overflow_infinity
555 ? positive_overflow_infinity (type)
556 : TYPE_MAX_VALUE (type)),
557 vr->equiv);
560 /* Set value range VR to a non-NULL range of type TYPE. */
562 static inline void
563 set_value_range_to_nonnull (value_range *vr, tree type)
565 tree zero = build_int_cst (type, 0);
566 set_value_range (vr, VR_ANTI_RANGE, zero, zero, vr->equiv);
570 /* Set value range VR to a NULL range of type TYPE. */
572 static inline void
573 set_value_range_to_null (value_range *vr, tree type)
575 set_value_range_to_value (vr, build_int_cst (type, 0), vr->equiv);
579 /* Set value range VR to a range of a truthvalue of type TYPE. */
581 static inline void
582 set_value_range_to_truthvalue (value_range *vr, tree type)
584 if (TYPE_PRECISION (type) == 1)
585 set_value_range_to_varying (vr);
586 else
587 set_value_range (vr, VR_RANGE,
588 build_int_cst (type, 0), build_int_cst (type, 1),
589 vr->equiv);
593 /* If abs (min) < abs (max), set VR to [-max, max], if
594 abs (min) >= abs (max), set VR to [-min, min]. */
596 static void
597 abs_extent_range (value_range *vr, tree min, tree max)
599 int cmp;
601 gcc_assert (TREE_CODE (min) == INTEGER_CST);
602 gcc_assert (TREE_CODE (max) == INTEGER_CST);
603 gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (min)));
604 gcc_assert (!TYPE_UNSIGNED (TREE_TYPE (min)));
605 min = fold_unary (ABS_EXPR, TREE_TYPE (min), min);
606 max = fold_unary (ABS_EXPR, TREE_TYPE (max), max);
607 if (TREE_OVERFLOW (min) || TREE_OVERFLOW (max))
609 set_value_range_to_varying (vr);
610 return;
612 cmp = compare_values (min, max);
613 if (cmp == -1)
614 min = fold_unary (NEGATE_EXPR, TREE_TYPE (min), max);
615 else if (cmp == 0 || cmp == 1)
617 max = min;
618 min = fold_unary (NEGATE_EXPR, TREE_TYPE (min), min);
620 else
622 set_value_range_to_varying (vr);
623 return;
625 set_and_canonicalize_value_range (vr, VR_RANGE, min, max, NULL);
629 /* Return value range information for VAR.
631 If we have no values ranges recorded (ie, VRP is not running), then
632 return NULL. Otherwise create an empty range if none existed for VAR. */
634 static value_range *
635 get_value_range (const_tree var)
637 static const value_range vr_const_varying
638 = { VR_VARYING, NULL_TREE, NULL_TREE, NULL };
639 value_range *vr;
640 tree sym;
641 unsigned ver = SSA_NAME_VERSION (var);
643 /* If we have no recorded ranges, then return NULL. */
644 if (! vr_value)
645 return NULL;
647 /* If we query the range for a new SSA name return an unmodifiable VARYING.
648 We should get here at most from the substitute-and-fold stage which
649 will never try to change values. */
650 if (ver >= num_vr_values)
651 return CONST_CAST (value_range *, &vr_const_varying);
653 vr = vr_value[ver];
654 if (vr)
655 return vr;
657 /* After propagation finished do not allocate new value-ranges. */
658 if (values_propagated)
659 return CONST_CAST (value_range *, &vr_const_varying);
661 /* Create a default value range. */
662 vr_value[ver] = vr = vrp_value_range_pool.allocate ();
663 memset (vr, 0, sizeof (*vr));
665 /* Defer allocating the equivalence set. */
666 vr->equiv = NULL;
668 /* If VAR is a default definition of a parameter, the variable can
669 take any value in VAR's type. */
670 if (SSA_NAME_IS_DEFAULT_DEF (var))
672 sym = SSA_NAME_VAR (var);
673 if (TREE_CODE (sym) == PARM_DECL)
675 /* Try to use the "nonnull" attribute to create ~[0, 0]
676 anti-ranges for pointers. Note that this is only valid with
677 default definitions of PARM_DECLs. */
678 if (POINTER_TYPE_P (TREE_TYPE (sym))
679 && (nonnull_arg_p (sym)
680 || get_ptr_nonnull (var)))
681 set_value_range_to_nonnull (vr, TREE_TYPE (sym));
682 else if (INTEGRAL_TYPE_P (TREE_TYPE (sym)))
684 wide_int min, max;
685 value_range_type rtype = get_range_info (var, &min, &max);
686 if (rtype == VR_RANGE || rtype == VR_ANTI_RANGE)
687 set_value_range (vr, rtype,
688 wide_int_to_tree (TREE_TYPE (var), min),
689 wide_int_to_tree (TREE_TYPE (var), max),
690 NULL);
691 else
692 set_value_range_to_varying (vr);
694 else
695 set_value_range_to_varying (vr);
697 else if (TREE_CODE (sym) == RESULT_DECL
698 && DECL_BY_REFERENCE (sym))
699 set_value_range_to_nonnull (vr, TREE_TYPE (sym));
702 return vr;
705 /* Set value-ranges of all SSA names defined by STMT to varying. */
707 static void
708 set_defs_to_varying (gimple *stmt)
710 ssa_op_iter i;
711 tree def;
712 FOR_EACH_SSA_TREE_OPERAND (def, stmt, i, SSA_OP_DEF)
714 value_range *vr = get_value_range (def);
715 /* Avoid writing to vr_const_varying get_value_range may return. */
716 if (vr->type != VR_VARYING)
717 set_value_range_to_varying (vr);
722 /* Return true, if VAL1 and VAL2 are equal values for VRP purposes. */
724 static inline bool
725 vrp_operand_equal_p (const_tree val1, const_tree val2)
727 if (val1 == val2)
728 return true;
729 if (!val1 || !val2 || !operand_equal_p (val1, val2, 0))
730 return false;
731 return is_overflow_infinity (val1) == is_overflow_infinity (val2);
734 /* Return true, if the bitmaps B1 and B2 are equal. */
736 static inline bool
737 vrp_bitmap_equal_p (const_bitmap b1, const_bitmap b2)
739 return (b1 == b2
740 || ((!b1 || bitmap_empty_p (b1))
741 && (!b2 || bitmap_empty_p (b2)))
742 || (b1 && b2
743 && bitmap_equal_p (b1, b2)));
746 /* Update the value range and equivalence set for variable VAR to
747 NEW_VR. Return true if NEW_VR is different from VAR's previous
748 value.
750 NOTE: This function assumes that NEW_VR is a temporary value range
751 object created for the sole purpose of updating VAR's range. The
752 storage used by the equivalence set from NEW_VR will be freed by
753 this function. Do not call update_value_range when NEW_VR
754 is the range object associated with another SSA name. */
756 static inline bool
757 update_value_range (const_tree var, value_range *new_vr)
759 value_range *old_vr;
760 bool is_new;
762 /* If there is a value-range on the SSA name from earlier analysis
763 factor that in. */
764 if (INTEGRAL_TYPE_P (TREE_TYPE (var)))
766 wide_int min, max;
767 value_range_type rtype = get_range_info (var, &min, &max);
768 if (rtype == VR_RANGE || rtype == VR_ANTI_RANGE)
770 tree nr_min, nr_max;
771 /* Range info on SSA names doesn't carry overflow information
772 so make sure to preserve the overflow bit on the lattice. */
773 if (rtype == VR_RANGE
774 && needs_overflow_infinity (TREE_TYPE (var))
775 && (new_vr->type == VR_VARYING
776 || (new_vr->type == VR_RANGE
777 && is_negative_overflow_infinity (new_vr->min)))
778 && wi::eq_p (vrp_val_min (TREE_TYPE (var)), min))
779 nr_min = negative_overflow_infinity (TREE_TYPE (var));
780 else
781 nr_min = wide_int_to_tree (TREE_TYPE (var), min);
782 if (rtype == VR_RANGE
783 && needs_overflow_infinity (TREE_TYPE (var))
784 && (new_vr->type == VR_VARYING
785 || (new_vr->type == VR_RANGE
786 && is_positive_overflow_infinity (new_vr->max)))
787 && wi::eq_p (vrp_val_max (TREE_TYPE (var)), max))
788 nr_max = positive_overflow_infinity (TREE_TYPE (var));
789 else
790 nr_max = wide_int_to_tree (TREE_TYPE (var), max);
791 value_range nr = VR_INITIALIZER;
792 set_and_canonicalize_value_range (&nr, rtype, nr_min, nr_max, NULL);
793 vrp_intersect_ranges (new_vr, &nr);
797 /* Update the value range, if necessary. */
798 old_vr = get_value_range (var);
799 is_new = old_vr->type != new_vr->type
800 || !vrp_operand_equal_p (old_vr->min, new_vr->min)
801 || !vrp_operand_equal_p (old_vr->max, new_vr->max)
802 || !vrp_bitmap_equal_p (old_vr->equiv, new_vr->equiv);
804 if (is_new)
806 /* Do not allow transitions up the lattice. The following
807 is slightly more awkward than just new_vr->type < old_vr->type
808 because VR_RANGE and VR_ANTI_RANGE need to be considered
809 the same. We may not have is_new when transitioning to
810 UNDEFINED. If old_vr->type is VARYING, we shouldn't be
811 called. */
812 if (new_vr->type == VR_UNDEFINED)
814 BITMAP_FREE (new_vr->equiv);
815 set_value_range_to_varying (old_vr);
816 set_value_range_to_varying (new_vr);
817 return true;
819 else
820 set_value_range (old_vr, new_vr->type, new_vr->min, new_vr->max,
821 new_vr->equiv);
824 BITMAP_FREE (new_vr->equiv);
826 return is_new;
830 /* Add VAR and VAR's equivalence set to EQUIV. This is the central
831 point where equivalence processing can be turned on/off. */
833 static void
834 add_equivalence (bitmap *equiv, const_tree var)
836 unsigned ver = SSA_NAME_VERSION (var);
837 value_range *vr = get_value_range (var);
839 if (*equiv == NULL)
840 *equiv = BITMAP_ALLOC (&vrp_equiv_obstack);
841 bitmap_set_bit (*equiv, ver);
842 if (vr && vr->equiv)
843 bitmap_ior_into (*equiv, vr->equiv);
847 /* Return true if VR is ~[0, 0]. */
849 static inline bool
850 range_is_nonnull (value_range *vr)
852 return vr->type == VR_ANTI_RANGE
853 && integer_zerop (vr->min)
854 && integer_zerop (vr->max);
858 /* Return true if VR is [0, 0]. */
860 static inline bool
861 range_is_null (value_range *vr)
863 return vr->type == VR_RANGE
864 && integer_zerop (vr->min)
865 && integer_zerop (vr->max);
868 /* Return true if max and min of VR are INTEGER_CST. It's not necessary
869 a singleton. */
871 static inline bool
872 range_int_cst_p (value_range *vr)
874 return (vr->type == VR_RANGE
875 && TREE_CODE (vr->max) == INTEGER_CST
876 && TREE_CODE (vr->min) == INTEGER_CST);
879 /* Return true if VR is a INTEGER_CST singleton. */
881 static inline bool
882 range_int_cst_singleton_p (value_range *vr)
884 return (range_int_cst_p (vr)
885 && !is_overflow_infinity (vr->min)
886 && !is_overflow_infinity (vr->max)
887 && tree_int_cst_equal (vr->min, vr->max));
890 /* Return true if value range VR involves at least one symbol. */
892 static inline bool
893 symbolic_range_p (value_range *vr)
895 return (!is_gimple_min_invariant (vr->min)
896 || !is_gimple_min_invariant (vr->max));
899 /* Return the single symbol (an SSA_NAME) contained in T if any, or NULL_TREE
900 otherwise. We only handle additive operations and set NEG to true if the
901 symbol is negated and INV to the invariant part, if any. */
903 static tree
904 get_single_symbol (tree t, bool *neg, tree *inv)
906 bool neg_;
907 tree inv_;
909 *inv = NULL_TREE;
910 *neg = false;
912 if (TREE_CODE (t) == PLUS_EXPR
913 || TREE_CODE (t) == POINTER_PLUS_EXPR
914 || TREE_CODE (t) == MINUS_EXPR)
916 if (is_gimple_min_invariant (TREE_OPERAND (t, 0)))
918 neg_ = (TREE_CODE (t) == MINUS_EXPR);
919 inv_ = TREE_OPERAND (t, 0);
920 t = TREE_OPERAND (t, 1);
922 else if (is_gimple_min_invariant (TREE_OPERAND (t, 1)))
924 neg_ = false;
925 inv_ = TREE_OPERAND (t, 1);
926 t = TREE_OPERAND (t, 0);
928 else
929 return NULL_TREE;
931 else
933 neg_ = false;
934 inv_ = NULL_TREE;
937 if (TREE_CODE (t) == NEGATE_EXPR)
939 t = TREE_OPERAND (t, 0);
940 neg_ = !neg_;
943 if (TREE_CODE (t) != SSA_NAME)
944 return NULL_TREE;
946 *neg = neg_;
947 *inv = inv_;
948 return t;
951 /* The reverse operation: build a symbolic expression with TYPE
952 from symbol SYM, negated according to NEG, and invariant INV. */
954 static tree
955 build_symbolic_expr (tree type, tree sym, bool neg, tree inv)
957 const bool pointer_p = POINTER_TYPE_P (type);
958 tree t = sym;
960 if (neg)
961 t = build1 (NEGATE_EXPR, type, t);
963 if (integer_zerop (inv))
964 return t;
966 return build2 (pointer_p ? POINTER_PLUS_EXPR : PLUS_EXPR, type, t, inv);
969 /* Return true if value range VR involves exactly one symbol SYM. */
971 static bool
972 symbolic_range_based_on_p (value_range *vr, const_tree sym)
974 bool neg, min_has_symbol, max_has_symbol;
975 tree inv;
977 if (is_gimple_min_invariant (vr->min))
978 min_has_symbol = false;
979 else if (get_single_symbol (vr->min, &neg, &inv) == sym)
980 min_has_symbol = true;
981 else
982 return false;
984 if (is_gimple_min_invariant (vr->max))
985 max_has_symbol = false;
986 else if (get_single_symbol (vr->max, &neg, &inv) == sym)
987 max_has_symbol = true;
988 else
989 return false;
991 return (min_has_symbol || max_has_symbol);
994 /* Return true if value range VR uses an overflow infinity. */
996 static inline bool
997 overflow_infinity_range_p (value_range *vr)
999 return (vr->type == VR_RANGE
1000 && (is_overflow_infinity (vr->min)
1001 || is_overflow_infinity (vr->max)));
1004 /* Return false if we can not make a valid comparison based on VR;
1005 this will be the case if it uses an overflow infinity and overflow
1006 is not undefined (i.e., -fno-strict-overflow is in effect).
1007 Otherwise return true, and set *STRICT_OVERFLOW_P to true if VR
1008 uses an overflow infinity. */
1010 static bool
1011 usable_range_p (value_range *vr, bool *strict_overflow_p)
1013 gcc_assert (vr->type == VR_RANGE);
1014 if (is_overflow_infinity (vr->min))
1016 *strict_overflow_p = true;
1017 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (vr->min)))
1018 return false;
1020 if (is_overflow_infinity (vr->max))
1022 *strict_overflow_p = true;
1023 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (vr->max)))
1024 return false;
1026 return true;
1029 /* Return true if the result of assignment STMT is know to be non-zero.
1030 If the return value is based on the assumption that signed overflow is
1031 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1032 *STRICT_OVERFLOW_P.*/
1034 static bool
1035 gimple_assign_nonzero_warnv_p (gimple *stmt, bool *strict_overflow_p)
1037 enum tree_code code = gimple_assign_rhs_code (stmt);
1038 switch (get_gimple_rhs_class (code))
1040 case GIMPLE_UNARY_RHS:
1041 return tree_unary_nonzero_warnv_p (gimple_assign_rhs_code (stmt),
1042 gimple_expr_type (stmt),
1043 gimple_assign_rhs1 (stmt),
1044 strict_overflow_p);
1045 case GIMPLE_BINARY_RHS:
1046 return tree_binary_nonzero_warnv_p (gimple_assign_rhs_code (stmt),
1047 gimple_expr_type (stmt),
1048 gimple_assign_rhs1 (stmt),
1049 gimple_assign_rhs2 (stmt),
1050 strict_overflow_p);
1051 case GIMPLE_TERNARY_RHS:
1052 return false;
1053 case GIMPLE_SINGLE_RHS:
1054 return tree_single_nonzero_warnv_p (gimple_assign_rhs1 (stmt),
1055 strict_overflow_p);
1056 case GIMPLE_INVALID_RHS:
1057 gcc_unreachable ();
1058 default:
1059 gcc_unreachable ();
1063 /* Return true if STMT is known to compute a non-zero value.
1064 If the return value is based on the assumption that signed overflow is
1065 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1066 *STRICT_OVERFLOW_P.*/
1068 static bool
1069 gimple_stmt_nonzero_warnv_p (gimple *stmt, bool *strict_overflow_p)
1071 switch (gimple_code (stmt))
1073 case GIMPLE_ASSIGN:
1074 return gimple_assign_nonzero_warnv_p (stmt, strict_overflow_p);
1075 case GIMPLE_CALL:
1077 tree fndecl = gimple_call_fndecl (stmt);
1078 if (!fndecl) return false;
1079 if (flag_delete_null_pointer_checks && !flag_check_new
1080 && DECL_IS_OPERATOR_NEW (fndecl)
1081 && !TREE_NOTHROW (fndecl))
1082 return true;
1083 /* References are always non-NULL. */
1084 if (flag_delete_null_pointer_checks
1085 && TREE_CODE (TREE_TYPE (fndecl)) == REFERENCE_TYPE)
1086 return true;
1087 if (flag_delete_null_pointer_checks &&
1088 lookup_attribute ("returns_nonnull",
1089 TYPE_ATTRIBUTES (gimple_call_fntype (stmt))))
1090 return true;
1092 gcall *call_stmt = as_a<gcall *> (stmt);
1093 unsigned rf = gimple_call_return_flags (call_stmt);
1094 if (rf & ERF_RETURNS_ARG)
1096 unsigned argnum = rf & ERF_RETURN_ARG_MASK;
1097 if (argnum < gimple_call_num_args (call_stmt))
1099 tree arg = gimple_call_arg (call_stmt, argnum);
1100 if (SSA_VAR_P (arg)
1101 && infer_nonnull_range_by_attribute (stmt, arg))
1102 return true;
1105 return gimple_alloca_call_p (stmt);
1107 default:
1108 gcc_unreachable ();
1112 /* Like tree_expr_nonzero_warnv_p, but this function uses value ranges
1113 obtained so far. */
1115 static bool
1116 vrp_stmt_computes_nonzero (gimple *stmt, bool *strict_overflow_p)
1118 if (gimple_stmt_nonzero_warnv_p (stmt, strict_overflow_p))
1119 return true;
1121 /* If we have an expression of the form &X->a, then the expression
1122 is nonnull if X is nonnull. */
1123 if (is_gimple_assign (stmt)
1124 && gimple_assign_rhs_code (stmt) == ADDR_EXPR)
1126 tree expr = gimple_assign_rhs1 (stmt);
1127 tree base = get_base_address (TREE_OPERAND (expr, 0));
1129 if (base != NULL_TREE
1130 && TREE_CODE (base) == MEM_REF
1131 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
1133 value_range *vr = get_value_range (TREE_OPERAND (base, 0));
1134 if (range_is_nonnull (vr))
1135 return true;
1139 return false;
1142 /* Returns true if EXPR is a valid value (as expected by compare_values) --
1143 a gimple invariant, or SSA_NAME +- CST. */
1145 static bool
1146 valid_value_p (tree expr)
1148 if (TREE_CODE (expr) == SSA_NAME)
1149 return true;
1151 if (TREE_CODE (expr) == PLUS_EXPR
1152 || TREE_CODE (expr) == MINUS_EXPR)
1153 return (TREE_CODE (TREE_OPERAND (expr, 0)) == SSA_NAME
1154 && TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST);
1156 return is_gimple_min_invariant (expr);
1159 /* Return
1160 1 if VAL < VAL2
1161 0 if !(VAL < VAL2)
1162 -2 if those are incomparable. */
1163 static inline int
1164 operand_less_p (tree val, tree val2)
1166 /* LT is folded faster than GE and others. Inline the common case. */
1167 if (TREE_CODE (val) == INTEGER_CST && TREE_CODE (val2) == INTEGER_CST)
1169 if (! is_positive_overflow_infinity (val2))
1170 return tree_int_cst_lt (val, val2);
1172 else
1174 tree tcmp;
1176 fold_defer_overflow_warnings ();
1178 tcmp = fold_binary_to_constant (LT_EXPR, boolean_type_node, val, val2);
1180 fold_undefer_and_ignore_overflow_warnings ();
1182 if (!tcmp
1183 || TREE_CODE (tcmp) != INTEGER_CST)
1184 return -2;
1186 if (!integer_zerop (tcmp))
1187 return 1;
1190 /* val >= val2, not considering overflow infinity. */
1191 if (is_negative_overflow_infinity (val))
1192 return is_negative_overflow_infinity (val2) ? 0 : 1;
1193 else if (is_positive_overflow_infinity (val2))
1194 return is_positive_overflow_infinity (val) ? 0 : 1;
1196 return 0;
1199 /* Compare two values VAL1 and VAL2. Return
1201 -2 if VAL1 and VAL2 cannot be compared at compile-time,
1202 -1 if VAL1 < VAL2,
1203 0 if VAL1 == VAL2,
1204 +1 if VAL1 > VAL2, and
1205 +2 if VAL1 != VAL2
1207 This is similar to tree_int_cst_compare but supports pointer values
1208 and values that cannot be compared at compile time.
1210 If STRICT_OVERFLOW_P is not NULL, then set *STRICT_OVERFLOW_P to
1211 true if the return value is only valid if we assume that signed
1212 overflow is undefined. */
1214 static int
1215 compare_values_warnv (tree val1, tree val2, bool *strict_overflow_p)
1217 if (val1 == val2)
1218 return 0;
1220 /* Below we rely on the fact that VAL1 and VAL2 are both pointers or
1221 both integers. */
1222 gcc_assert (POINTER_TYPE_P (TREE_TYPE (val1))
1223 == POINTER_TYPE_P (TREE_TYPE (val2)));
1225 /* Convert the two values into the same type. This is needed because
1226 sizetype causes sign extension even for unsigned types. */
1227 val2 = fold_convert (TREE_TYPE (val1), val2);
1228 STRIP_USELESS_TYPE_CONVERSION (val2);
1230 const bool overflow_undefined
1231 = INTEGRAL_TYPE_P (TREE_TYPE (val1))
1232 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (val1));
1233 tree inv1, inv2;
1234 bool neg1, neg2;
1235 tree sym1 = get_single_symbol (val1, &neg1, &inv1);
1236 tree sym2 = get_single_symbol (val2, &neg2, &inv2);
1238 /* If VAL1 and VAL2 are of the form '[-]NAME [+ CST]', return -1 or +1
1239 accordingly. If VAL1 and VAL2 don't use the same name, return -2. */
1240 if (sym1 && sym2)
1242 /* Both values must use the same name with the same sign. */
1243 if (sym1 != sym2 || neg1 != neg2)
1244 return -2;
1246 /* [-]NAME + CST == [-]NAME + CST. */
1247 if (inv1 == inv2)
1248 return 0;
1250 /* If overflow is defined we cannot simplify more. */
1251 if (!overflow_undefined)
1252 return -2;
1254 if (strict_overflow_p != NULL
1255 && (!inv1 || !TREE_NO_WARNING (val1))
1256 && (!inv2 || !TREE_NO_WARNING (val2)))
1257 *strict_overflow_p = true;
1259 if (!inv1)
1260 inv1 = build_int_cst (TREE_TYPE (val1), 0);
1261 if (!inv2)
1262 inv2 = build_int_cst (TREE_TYPE (val2), 0);
1264 return compare_values_warnv (inv1, inv2, strict_overflow_p);
1267 const bool cst1 = is_gimple_min_invariant (val1);
1268 const bool cst2 = is_gimple_min_invariant (val2);
1270 /* If one is of the form '[-]NAME + CST' and the other is constant, then
1271 it might be possible to say something depending on the constants. */
1272 if ((sym1 && inv1 && cst2) || (sym2 && inv2 && cst1))
1274 if (!overflow_undefined)
1275 return -2;
1277 if (strict_overflow_p != NULL
1278 && (!sym1 || !TREE_NO_WARNING (val1))
1279 && (!sym2 || !TREE_NO_WARNING (val2)))
1280 *strict_overflow_p = true;
1282 const signop sgn = TYPE_SIGN (TREE_TYPE (val1));
1283 tree cst = cst1 ? val1 : val2;
1284 tree inv = cst1 ? inv2 : inv1;
1286 /* Compute the difference between the constants. If it overflows or
1287 underflows, this means that we can trivially compare the NAME with
1288 it and, consequently, the two values with each other. */
1289 wide_int diff = wi::sub (cst, inv);
1290 if (wi::cmp (0, inv, sgn) != wi::cmp (diff, cst, sgn))
1292 const int res = wi::cmp (cst, inv, sgn);
1293 return cst1 ? res : -res;
1296 return -2;
1299 /* We cannot say anything more for non-constants. */
1300 if (!cst1 || !cst2)
1301 return -2;
1303 if (!POINTER_TYPE_P (TREE_TYPE (val1)))
1305 /* We cannot compare overflowed values, except for overflow
1306 infinities. */
1307 if (TREE_OVERFLOW (val1) || TREE_OVERFLOW (val2))
1309 if (strict_overflow_p != NULL)
1310 *strict_overflow_p = true;
1311 if (is_negative_overflow_infinity (val1))
1312 return is_negative_overflow_infinity (val2) ? 0 : -1;
1313 else if (is_negative_overflow_infinity (val2))
1314 return 1;
1315 else if (is_positive_overflow_infinity (val1))
1316 return is_positive_overflow_infinity (val2) ? 0 : 1;
1317 else if (is_positive_overflow_infinity (val2))
1318 return -1;
1319 return -2;
1322 return tree_int_cst_compare (val1, val2);
1324 else
1326 tree t;
1328 /* First see if VAL1 and VAL2 are not the same. */
1329 if (val1 == val2 || operand_equal_p (val1, val2, 0))
1330 return 0;
1332 /* If VAL1 is a lower address than VAL2, return -1. */
1333 if (operand_less_p (val1, val2) == 1)
1334 return -1;
1336 /* If VAL1 is a higher address than VAL2, return +1. */
1337 if (operand_less_p (val2, val1) == 1)
1338 return 1;
1340 /* If VAL1 is different than VAL2, return +2.
1341 For integer constants we either have already returned -1 or 1
1342 or they are equivalent. We still might succeed in proving
1343 something about non-trivial operands. */
1344 if (TREE_CODE (val1) != INTEGER_CST
1345 || TREE_CODE (val2) != INTEGER_CST)
1347 t = fold_binary_to_constant (NE_EXPR, boolean_type_node, val1, val2);
1348 if (t && integer_onep (t))
1349 return 2;
1352 return -2;
1356 /* Compare values like compare_values_warnv, but treat comparisons of
1357 nonconstants which rely on undefined overflow as incomparable. */
1359 static int
1360 compare_values (tree val1, tree val2)
1362 bool sop;
1363 int ret;
1365 sop = false;
1366 ret = compare_values_warnv (val1, val2, &sop);
1367 if (sop
1368 && (!is_gimple_min_invariant (val1) || !is_gimple_min_invariant (val2)))
1369 ret = -2;
1370 return ret;
1374 /* Return 1 if VAL is inside value range MIN <= VAL <= MAX,
1375 0 if VAL is not inside [MIN, MAX],
1376 -2 if we cannot tell either way.
1378 Benchmark compile/20001226-1.c compilation time after changing this
1379 function. */
1381 static inline int
1382 value_inside_range (tree val, tree min, tree max)
1384 int cmp1, cmp2;
1386 cmp1 = operand_less_p (val, min);
1387 if (cmp1 == -2)
1388 return -2;
1389 if (cmp1 == 1)
1390 return 0;
1392 cmp2 = operand_less_p (max, val);
1393 if (cmp2 == -2)
1394 return -2;
1396 return !cmp2;
1400 /* Return true if value ranges VR0 and VR1 have a non-empty
1401 intersection.
1403 Benchmark compile/20001226-1.c compilation time after changing this
1404 function.
1407 static inline bool
1408 value_ranges_intersect_p (value_range *vr0, value_range *vr1)
1410 /* The value ranges do not intersect if the maximum of the first range is
1411 less than the minimum of the second range or vice versa.
1412 When those relations are unknown, we can't do any better. */
1413 if (operand_less_p (vr0->max, vr1->min) != 0)
1414 return false;
1415 if (operand_less_p (vr1->max, vr0->min) != 0)
1416 return false;
1417 return true;
1421 /* Return 1 if [MIN, MAX] includes the value zero, 0 if it does not
1422 include the value zero, -2 if we cannot tell. */
1424 static inline int
1425 range_includes_zero_p (tree min, tree max)
1427 tree zero = build_int_cst (TREE_TYPE (min), 0);
1428 return value_inside_range (zero, min, max);
1431 /* Return true if *VR is know to only contain nonnegative values. */
1433 static inline bool
1434 value_range_nonnegative_p (value_range *vr)
1436 /* Testing for VR_ANTI_RANGE is not useful here as any anti-range
1437 which would return a useful value should be encoded as a
1438 VR_RANGE. */
1439 if (vr->type == VR_RANGE)
1441 int result = compare_values (vr->min, integer_zero_node);
1442 return (result == 0 || result == 1);
1445 return false;
1448 /* If *VR has a value rante that is a single constant value return that,
1449 otherwise return NULL_TREE. */
1451 static tree
1452 value_range_constant_singleton (value_range *vr)
1454 if (vr->type == VR_RANGE
1455 && vrp_operand_equal_p (vr->min, vr->max)
1456 && is_gimple_min_invariant (vr->min))
1457 return vr->min;
1459 return NULL_TREE;
1462 /* If OP has a value range with a single constant value return that,
1463 otherwise return NULL_TREE. This returns OP itself if OP is a
1464 constant. */
1466 static tree
1467 op_with_constant_singleton_value_range (tree op)
1469 if (is_gimple_min_invariant (op))
1470 return op;
1472 if (TREE_CODE (op) != SSA_NAME)
1473 return NULL_TREE;
1475 return value_range_constant_singleton (get_value_range (op));
1478 /* Return true if op is in a boolean [0, 1] value-range. */
1480 static bool
1481 op_with_boolean_value_range_p (tree op)
1483 value_range *vr;
1485 if (TYPE_PRECISION (TREE_TYPE (op)) == 1)
1486 return true;
1488 if (integer_zerop (op)
1489 || integer_onep (op))
1490 return true;
1492 if (TREE_CODE (op) != SSA_NAME)
1493 return false;
1495 vr = get_value_range (op);
1496 return (vr->type == VR_RANGE
1497 && integer_zerop (vr->min)
1498 && integer_onep (vr->max));
1501 /* Extract value range information for VAR when (OP COND_CODE LIMIT) is
1502 true and store it in *VR_P. */
1504 static void
1505 extract_range_for_var_from_comparison_expr (tree var, enum tree_code cond_code,
1506 tree op, tree limit,
1507 value_range *vr_p)
1509 tree min, max, type;
1510 value_range *limit_vr;
1511 limit = avoid_overflow_infinity (limit);
1512 type = TREE_TYPE (var);
1513 gcc_assert (limit != var);
1515 /* For pointer arithmetic, we only keep track of pointer equality
1516 and inequality. */
1517 if (POINTER_TYPE_P (type) && cond_code != NE_EXPR && cond_code != EQ_EXPR)
1519 set_value_range_to_varying (vr_p);
1520 return;
1523 /* If LIMIT is another SSA name and LIMIT has a range of its own,
1524 try to use LIMIT's range to avoid creating symbolic ranges
1525 unnecessarily. */
1526 limit_vr = (TREE_CODE (limit) == SSA_NAME) ? get_value_range (limit) : NULL;
1528 /* LIMIT's range is only interesting if it has any useful information. */
1529 if (! limit_vr
1530 || limit_vr->type == VR_UNDEFINED
1531 || limit_vr->type == VR_VARYING
1532 || (symbolic_range_p (limit_vr)
1533 && ! (limit_vr->type == VR_RANGE
1534 && (limit_vr->min == limit_vr->max
1535 || operand_equal_p (limit_vr->min, limit_vr->max, 0)))))
1536 limit_vr = NULL;
1538 /* Initially, the new range has the same set of equivalences of
1539 VAR's range. This will be revised before returning the final
1540 value. Since assertions may be chained via mutually exclusive
1541 predicates, we will need to trim the set of equivalences before
1542 we are done. */
1543 gcc_assert (vr_p->equiv == NULL);
1544 add_equivalence (&vr_p->equiv, var);
1546 /* Extract a new range based on the asserted comparison for VAR and
1547 LIMIT's value range. Notice that if LIMIT has an anti-range, we
1548 will only use it for equality comparisons (EQ_EXPR). For any
1549 other kind of assertion, we cannot derive a range from LIMIT's
1550 anti-range that can be used to describe the new range. For
1551 instance, ASSERT_EXPR <x_2, x_2 <= b_4>. If b_4 is ~[2, 10],
1552 then b_4 takes on the ranges [-INF, 1] and [11, +INF]. There is
1553 no single range for x_2 that could describe LE_EXPR, so we might
1554 as well build the range [b_4, +INF] for it.
1555 One special case we handle is extracting a range from a
1556 range test encoded as (unsigned)var + CST <= limit. */
1557 if (TREE_CODE (op) == NOP_EXPR
1558 || TREE_CODE (op) == PLUS_EXPR)
1560 if (TREE_CODE (op) == PLUS_EXPR)
1562 min = fold_build1 (NEGATE_EXPR, TREE_TYPE (TREE_OPERAND (op, 1)),
1563 TREE_OPERAND (op, 1));
1564 max = int_const_binop (PLUS_EXPR, limit, min);
1565 op = TREE_OPERAND (op, 0);
1567 else
1569 min = build_int_cst (TREE_TYPE (var), 0);
1570 max = limit;
1573 /* Make sure to not set TREE_OVERFLOW on the final type
1574 conversion. We are willingly interpreting large positive
1575 unsigned values as negative signed values here. */
1576 min = force_fit_type (TREE_TYPE (var), wi::to_widest (min), 0, false);
1577 max = force_fit_type (TREE_TYPE (var), wi::to_widest (max), 0, false);
1579 /* We can transform a max, min range to an anti-range or
1580 vice-versa. Use set_and_canonicalize_value_range which does
1581 this for us. */
1582 if (cond_code == LE_EXPR)
1583 set_and_canonicalize_value_range (vr_p, VR_RANGE,
1584 min, max, vr_p->equiv);
1585 else if (cond_code == GT_EXPR)
1586 set_and_canonicalize_value_range (vr_p, VR_ANTI_RANGE,
1587 min, max, vr_p->equiv);
1588 else
1589 gcc_unreachable ();
1591 else if (cond_code == EQ_EXPR)
1593 enum value_range_type range_type;
1595 if (limit_vr)
1597 range_type = limit_vr->type;
1598 min = limit_vr->min;
1599 max = limit_vr->max;
1601 else
1603 range_type = VR_RANGE;
1604 min = limit;
1605 max = limit;
1608 set_value_range (vr_p, range_type, min, max, vr_p->equiv);
1610 /* When asserting the equality VAR == LIMIT and LIMIT is another
1611 SSA name, the new range will also inherit the equivalence set
1612 from LIMIT. */
1613 if (TREE_CODE (limit) == SSA_NAME)
1614 add_equivalence (&vr_p->equiv, limit);
1616 else if (cond_code == NE_EXPR)
1618 /* As described above, when LIMIT's range is an anti-range and
1619 this assertion is an inequality (NE_EXPR), then we cannot
1620 derive anything from the anti-range. For instance, if
1621 LIMIT's range was ~[0, 0], the assertion 'VAR != LIMIT' does
1622 not imply that VAR's range is [0, 0]. So, in the case of
1623 anti-ranges, we just assert the inequality using LIMIT and
1624 not its anti-range.
1626 If LIMIT_VR is a range, we can only use it to build a new
1627 anti-range if LIMIT_VR is a single-valued range. For
1628 instance, if LIMIT_VR is [0, 1], the predicate
1629 VAR != [0, 1] does not mean that VAR's range is ~[0, 1].
1630 Rather, it means that for value 0 VAR should be ~[0, 0]
1631 and for value 1, VAR should be ~[1, 1]. We cannot
1632 represent these ranges.
1634 The only situation in which we can build a valid
1635 anti-range is when LIMIT_VR is a single-valued range
1636 (i.e., LIMIT_VR->MIN == LIMIT_VR->MAX). In that case,
1637 build the anti-range ~[LIMIT_VR->MIN, LIMIT_VR->MAX]. */
1638 if (limit_vr
1639 && limit_vr->type == VR_RANGE
1640 && compare_values (limit_vr->min, limit_vr->max) == 0)
1642 min = limit_vr->min;
1643 max = limit_vr->max;
1645 else
1647 /* In any other case, we cannot use LIMIT's range to build a
1648 valid anti-range. */
1649 min = max = limit;
1652 /* If MIN and MAX cover the whole range for their type, then
1653 just use the original LIMIT. */
1654 if (INTEGRAL_TYPE_P (type)
1655 && vrp_val_is_min (min)
1656 && vrp_val_is_max (max))
1657 min = max = limit;
1659 set_and_canonicalize_value_range (vr_p, VR_ANTI_RANGE,
1660 min, max, vr_p->equiv);
1662 else if (cond_code == LE_EXPR || cond_code == LT_EXPR)
1664 min = TYPE_MIN_VALUE (type);
1666 if (limit_vr == NULL || limit_vr->type == VR_ANTI_RANGE)
1667 max = limit;
1668 else
1670 /* If LIMIT_VR is of the form [N1, N2], we need to build the
1671 range [MIN, N2] for LE_EXPR and [MIN, N2 - 1] for
1672 LT_EXPR. */
1673 max = limit_vr->max;
1676 /* If the maximum value forces us to be out of bounds, simply punt.
1677 It would be pointless to try and do anything more since this
1678 all should be optimized away above us. */
1679 if ((cond_code == LT_EXPR
1680 && compare_values (max, min) == 0)
1681 || is_overflow_infinity (max))
1682 set_value_range_to_varying (vr_p);
1683 else
1685 /* For LT_EXPR, we create the range [MIN, MAX - 1]. */
1686 if (cond_code == LT_EXPR)
1688 if (TYPE_PRECISION (TREE_TYPE (max)) == 1
1689 && !TYPE_UNSIGNED (TREE_TYPE (max)))
1690 max = fold_build2 (PLUS_EXPR, TREE_TYPE (max), max,
1691 build_int_cst (TREE_TYPE (max), -1));
1692 else
1693 max = fold_build2 (MINUS_EXPR, TREE_TYPE (max), max,
1694 build_int_cst (TREE_TYPE (max), 1));
1695 if (EXPR_P (max))
1696 TREE_NO_WARNING (max) = 1;
1699 set_value_range (vr_p, VR_RANGE, min, max, vr_p->equiv);
1702 else if (cond_code == GE_EXPR || cond_code == GT_EXPR)
1704 max = TYPE_MAX_VALUE (type);
1706 if (limit_vr == NULL || limit_vr->type == VR_ANTI_RANGE)
1707 min = limit;
1708 else
1710 /* If LIMIT_VR is of the form [N1, N2], we need to build the
1711 range [N1, MAX] for GE_EXPR and [N1 + 1, MAX] for
1712 GT_EXPR. */
1713 min = limit_vr->min;
1716 /* If the minimum value forces us to be out of bounds, simply punt.
1717 It would be pointless to try and do anything more since this
1718 all should be optimized away above us. */
1719 if ((cond_code == GT_EXPR
1720 && compare_values (min, max) == 0)
1721 || is_overflow_infinity (min))
1722 set_value_range_to_varying (vr_p);
1723 else
1725 /* For GT_EXPR, we create the range [MIN + 1, MAX]. */
1726 if (cond_code == GT_EXPR)
1728 if (TYPE_PRECISION (TREE_TYPE (min)) == 1
1729 && !TYPE_UNSIGNED (TREE_TYPE (min)))
1730 min = fold_build2 (MINUS_EXPR, TREE_TYPE (min), min,
1731 build_int_cst (TREE_TYPE (min), -1));
1732 else
1733 min = fold_build2 (PLUS_EXPR, TREE_TYPE (min), min,
1734 build_int_cst (TREE_TYPE (min), 1));
1735 if (EXPR_P (min))
1736 TREE_NO_WARNING (min) = 1;
1739 set_value_range (vr_p, VR_RANGE, min, max, vr_p->equiv);
1742 else
1743 gcc_unreachable ();
1745 /* Finally intersect the new range with what we already know about var. */
1746 vrp_intersect_ranges (vr_p, get_value_range (var));
1749 /* Extract value range information from an ASSERT_EXPR EXPR and store
1750 it in *VR_P. */
1752 static void
1753 extract_range_from_assert (value_range *vr_p, tree expr)
1755 tree var = ASSERT_EXPR_VAR (expr);
1756 tree cond = ASSERT_EXPR_COND (expr);
1757 tree limit, op;
1758 enum tree_code cond_code;
1759 gcc_assert (COMPARISON_CLASS_P (cond));
1761 /* Find VAR in the ASSERT_EXPR conditional. */
1762 if (var == TREE_OPERAND (cond, 0)
1763 || TREE_CODE (TREE_OPERAND (cond, 0)) == PLUS_EXPR
1764 || TREE_CODE (TREE_OPERAND (cond, 0)) == NOP_EXPR)
1766 /* If the predicate is of the form VAR COMP LIMIT, then we just
1767 take LIMIT from the RHS and use the same comparison code. */
1768 cond_code = TREE_CODE (cond);
1769 limit = TREE_OPERAND (cond, 1);
1770 op = TREE_OPERAND (cond, 0);
1772 else
1774 /* If the predicate is of the form LIMIT COMP VAR, then we need
1775 to flip around the comparison code to create the proper range
1776 for VAR. */
1777 cond_code = swap_tree_comparison (TREE_CODE (cond));
1778 limit = TREE_OPERAND (cond, 0);
1779 op = TREE_OPERAND (cond, 1);
1781 extract_range_for_var_from_comparison_expr (var, cond_code, op,
1782 limit, vr_p);
1785 /* Extract range information from SSA name VAR and store it in VR. If
1786 VAR has an interesting range, use it. Otherwise, create the
1787 range [VAR, VAR] and return it. This is useful in situations where
1788 we may have conditionals testing values of VARYING names. For
1789 instance,
1791 x_3 = y_5;
1792 if (x_3 > y_5)
1795 Even if y_5 is deemed VARYING, we can determine that x_3 > y_5 is
1796 always false. */
1798 static void
1799 extract_range_from_ssa_name (value_range *vr, tree var)
1801 value_range *var_vr = get_value_range (var);
1803 if (var_vr->type != VR_VARYING)
1804 copy_value_range (vr, var_vr);
1805 else
1806 set_value_range (vr, VR_RANGE, var, var, NULL);
1808 add_equivalence (&vr->equiv, var);
1812 /* Wrapper around int_const_binop. If the operation overflows and we
1813 are not using wrapping arithmetic, then adjust the result to be
1814 -INF or +INF depending on CODE, VAL1 and VAL2. This can return
1815 NULL_TREE if we need to use an overflow infinity representation but
1816 the type does not support it. */
1818 static tree
1819 vrp_int_const_binop (enum tree_code code, tree val1, tree val2)
1821 tree res;
1823 res = int_const_binop (code, val1, val2);
1825 /* If we are using unsigned arithmetic, operate symbolically
1826 on -INF and +INF as int_const_binop only handles signed overflow. */
1827 if (TYPE_UNSIGNED (TREE_TYPE (val1)))
1829 int checkz = compare_values (res, val1);
1830 bool overflow = false;
1832 /* Ensure that res = val1 [+*] val2 >= val1
1833 or that res = val1 - val2 <= val1. */
1834 if ((code == PLUS_EXPR
1835 && !(checkz == 1 || checkz == 0))
1836 || (code == MINUS_EXPR
1837 && !(checkz == 0 || checkz == -1)))
1839 overflow = true;
1841 /* Checking for multiplication overflow is done by dividing the
1842 output of the multiplication by the first input of the
1843 multiplication. If the result of that division operation is
1844 not equal to the second input of the multiplication, then the
1845 multiplication overflowed. */
1846 else if (code == MULT_EXPR && !integer_zerop (val1))
1848 tree tmp = int_const_binop (TRUNC_DIV_EXPR,
1849 res,
1850 val1);
1851 int check = compare_values (tmp, val2);
1853 if (check != 0)
1854 overflow = true;
1857 if (overflow)
1859 res = copy_node (res);
1860 TREE_OVERFLOW (res) = 1;
1864 else if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (val1)))
1865 /* If the singed operation wraps then int_const_binop has done
1866 everything we want. */
1868 /* Signed division of -1/0 overflows and by the time it gets here
1869 returns NULL_TREE. */
1870 else if (!res)
1871 return NULL_TREE;
1872 else if ((TREE_OVERFLOW (res)
1873 && !TREE_OVERFLOW (val1)
1874 && !TREE_OVERFLOW (val2))
1875 || is_overflow_infinity (val1)
1876 || is_overflow_infinity (val2))
1878 /* If the operation overflowed but neither VAL1 nor VAL2 are
1879 overflown, return -INF or +INF depending on the operation
1880 and the combination of signs of the operands. */
1881 int sgn1 = tree_int_cst_sgn (val1);
1882 int sgn2 = tree_int_cst_sgn (val2);
1884 if (needs_overflow_infinity (TREE_TYPE (res))
1885 && !supports_overflow_infinity (TREE_TYPE (res)))
1886 return NULL_TREE;
1888 /* We have to punt on adding infinities of different signs,
1889 since we can't tell what the sign of the result should be.
1890 Likewise for subtracting infinities of the same sign. */
1891 if (((code == PLUS_EXPR && sgn1 != sgn2)
1892 || (code == MINUS_EXPR && sgn1 == sgn2))
1893 && is_overflow_infinity (val1)
1894 && is_overflow_infinity (val2))
1895 return NULL_TREE;
1897 /* Don't try to handle division or shifting of infinities. */
1898 if ((code == TRUNC_DIV_EXPR
1899 || code == FLOOR_DIV_EXPR
1900 || code == CEIL_DIV_EXPR
1901 || code == EXACT_DIV_EXPR
1902 || code == ROUND_DIV_EXPR
1903 || code == RSHIFT_EXPR)
1904 && (is_overflow_infinity (val1)
1905 || is_overflow_infinity (val2)))
1906 return NULL_TREE;
1908 /* Notice that we only need to handle the restricted set of
1909 operations handled by extract_range_from_binary_expr.
1910 Among them, only multiplication, addition and subtraction
1911 can yield overflow without overflown operands because we
1912 are working with integral types only... except in the
1913 case VAL1 = -INF and VAL2 = -1 which overflows to +INF
1914 for division too. */
1916 /* For multiplication, the sign of the overflow is given
1917 by the comparison of the signs of the operands. */
1918 if ((code == MULT_EXPR && sgn1 == sgn2)
1919 /* For addition, the operands must be of the same sign
1920 to yield an overflow. Its sign is therefore that
1921 of one of the operands, for example the first. For
1922 infinite operands X + -INF is negative, not positive. */
1923 || (code == PLUS_EXPR
1924 && (sgn1 >= 0
1925 ? !is_negative_overflow_infinity (val2)
1926 : is_positive_overflow_infinity (val2)))
1927 /* For subtraction, non-infinite operands must be of
1928 different signs to yield an overflow. Its sign is
1929 therefore that of the first operand or the opposite of
1930 that of the second operand. A first operand of 0 counts
1931 as positive here, for the corner case 0 - (-INF), which
1932 overflows, but must yield +INF. For infinite operands 0
1933 - INF is negative, not positive. */
1934 || (code == MINUS_EXPR
1935 && (sgn1 >= 0
1936 ? !is_positive_overflow_infinity (val2)
1937 : is_negative_overflow_infinity (val2)))
1938 /* We only get in here with positive shift count, so the
1939 overflow direction is the same as the sign of val1.
1940 Actually rshift does not overflow at all, but we only
1941 handle the case of shifting overflowed -INF and +INF. */
1942 || (code == RSHIFT_EXPR
1943 && sgn1 >= 0)
1944 /* For division, the only case is -INF / -1 = +INF. */
1945 || code == TRUNC_DIV_EXPR
1946 || code == FLOOR_DIV_EXPR
1947 || code == CEIL_DIV_EXPR
1948 || code == EXACT_DIV_EXPR
1949 || code == ROUND_DIV_EXPR)
1950 return (needs_overflow_infinity (TREE_TYPE (res))
1951 ? positive_overflow_infinity (TREE_TYPE (res))
1952 : TYPE_MAX_VALUE (TREE_TYPE (res)));
1953 else
1954 return (needs_overflow_infinity (TREE_TYPE (res))
1955 ? negative_overflow_infinity (TREE_TYPE (res))
1956 : TYPE_MIN_VALUE (TREE_TYPE (res)));
1959 return res;
1963 /* For range VR compute two wide_int bitmasks. In *MAY_BE_NONZERO
1964 bitmask if some bit is unset, it means for all numbers in the range
1965 the bit is 0, otherwise it might be 0 or 1. In *MUST_BE_NONZERO
1966 bitmask if some bit is set, it means for all numbers in the range
1967 the bit is 1, otherwise it might be 0 or 1. */
1969 static bool
1970 zero_nonzero_bits_from_vr (const tree expr_type,
1971 value_range *vr,
1972 wide_int *may_be_nonzero,
1973 wide_int *must_be_nonzero)
1975 *may_be_nonzero = wi::minus_one (TYPE_PRECISION (expr_type));
1976 *must_be_nonzero = wi::zero (TYPE_PRECISION (expr_type));
1977 if (!range_int_cst_p (vr)
1978 || is_overflow_infinity (vr->min)
1979 || is_overflow_infinity (vr->max))
1980 return false;
1982 if (range_int_cst_singleton_p (vr))
1984 *may_be_nonzero = vr->min;
1985 *must_be_nonzero = *may_be_nonzero;
1987 else if (tree_int_cst_sgn (vr->min) >= 0
1988 || tree_int_cst_sgn (vr->max) < 0)
1990 wide_int xor_mask = wi::bit_xor (vr->min, vr->max);
1991 *may_be_nonzero = wi::bit_or (vr->min, vr->max);
1992 *must_be_nonzero = wi::bit_and (vr->min, vr->max);
1993 if (xor_mask != 0)
1995 wide_int mask = wi::mask (wi::floor_log2 (xor_mask), false,
1996 may_be_nonzero->get_precision ());
1997 *may_be_nonzero = *may_be_nonzero | mask;
1998 *must_be_nonzero = must_be_nonzero->and_not (mask);
2002 return true;
2005 /* Create two value-ranges in *VR0 and *VR1 from the anti-range *AR
2006 so that *VR0 U *VR1 == *AR. Returns true if that is possible,
2007 false otherwise. If *AR can be represented with a single range
2008 *VR1 will be VR_UNDEFINED. */
2010 static bool
2011 ranges_from_anti_range (value_range *ar,
2012 value_range *vr0, value_range *vr1)
2014 tree type = TREE_TYPE (ar->min);
2016 vr0->type = VR_UNDEFINED;
2017 vr1->type = VR_UNDEFINED;
2019 if (ar->type != VR_ANTI_RANGE
2020 || TREE_CODE (ar->min) != INTEGER_CST
2021 || TREE_CODE (ar->max) != INTEGER_CST
2022 || !vrp_val_min (type)
2023 || !vrp_val_max (type))
2024 return false;
2026 if (!vrp_val_is_min (ar->min))
2028 vr0->type = VR_RANGE;
2029 vr0->min = vrp_val_min (type);
2030 vr0->max = wide_int_to_tree (type, wi::sub (ar->min, 1));
2032 if (!vrp_val_is_max (ar->max))
2034 vr1->type = VR_RANGE;
2035 vr1->min = wide_int_to_tree (type, wi::add (ar->max, 1));
2036 vr1->max = vrp_val_max (type);
2038 if (vr0->type == VR_UNDEFINED)
2040 *vr0 = *vr1;
2041 vr1->type = VR_UNDEFINED;
2044 return vr0->type != VR_UNDEFINED;
2047 /* Helper to extract a value-range *VR for a multiplicative operation
2048 *VR0 CODE *VR1. */
2050 static void
2051 extract_range_from_multiplicative_op_1 (value_range *vr,
2052 enum tree_code code,
2053 value_range *vr0, value_range *vr1)
2055 enum value_range_type type;
2056 tree val[4];
2057 size_t i;
2058 tree min, max;
2059 bool sop;
2060 int cmp;
2062 /* Multiplications, divisions and shifts are a bit tricky to handle,
2063 depending on the mix of signs we have in the two ranges, we
2064 need to operate on different values to get the minimum and
2065 maximum values for the new range. One approach is to figure
2066 out all the variations of range combinations and do the
2067 operations.
2069 However, this involves several calls to compare_values and it
2070 is pretty convoluted. It's simpler to do the 4 operations
2071 (MIN0 OP MIN1, MIN0 OP MAX1, MAX0 OP MIN1 and MAX0 OP MAX0 OP
2072 MAX1) and then figure the smallest and largest values to form
2073 the new range. */
2074 gcc_assert (code == MULT_EXPR
2075 || code == TRUNC_DIV_EXPR
2076 || code == FLOOR_DIV_EXPR
2077 || code == CEIL_DIV_EXPR
2078 || code == EXACT_DIV_EXPR
2079 || code == ROUND_DIV_EXPR
2080 || code == RSHIFT_EXPR
2081 || code == LSHIFT_EXPR);
2082 gcc_assert ((vr0->type == VR_RANGE
2083 || (code == MULT_EXPR && vr0->type == VR_ANTI_RANGE))
2084 && vr0->type == vr1->type);
2086 type = vr0->type;
2088 /* Compute the 4 cross operations. */
2089 sop = false;
2090 val[0] = vrp_int_const_binop (code, vr0->min, vr1->min);
2091 if (val[0] == NULL_TREE)
2092 sop = true;
2094 if (vr1->max == vr1->min)
2095 val[1] = NULL_TREE;
2096 else
2098 val[1] = vrp_int_const_binop (code, vr0->min, vr1->max);
2099 if (val[1] == NULL_TREE)
2100 sop = true;
2103 if (vr0->max == vr0->min)
2104 val[2] = NULL_TREE;
2105 else
2107 val[2] = vrp_int_const_binop (code, vr0->max, vr1->min);
2108 if (val[2] == NULL_TREE)
2109 sop = true;
2112 if (vr0->min == vr0->max || vr1->min == vr1->max)
2113 val[3] = NULL_TREE;
2114 else
2116 val[3] = vrp_int_const_binop (code, vr0->max, vr1->max);
2117 if (val[3] == NULL_TREE)
2118 sop = true;
2121 if (sop)
2123 set_value_range_to_varying (vr);
2124 return;
2127 /* Set MIN to the minimum of VAL[i] and MAX to the maximum
2128 of VAL[i]. */
2129 min = val[0];
2130 max = val[0];
2131 for (i = 1; i < 4; i++)
2133 if (!is_gimple_min_invariant (min)
2134 || (TREE_OVERFLOW (min) && !is_overflow_infinity (min))
2135 || !is_gimple_min_invariant (max)
2136 || (TREE_OVERFLOW (max) && !is_overflow_infinity (max)))
2137 break;
2139 if (val[i])
2141 if (!is_gimple_min_invariant (val[i])
2142 || (TREE_OVERFLOW (val[i])
2143 && !is_overflow_infinity (val[i])))
2145 /* If we found an overflowed value, set MIN and MAX
2146 to it so that we set the resulting range to
2147 VARYING. */
2148 min = max = val[i];
2149 break;
2152 if (compare_values (val[i], min) == -1)
2153 min = val[i];
2155 if (compare_values (val[i], max) == 1)
2156 max = val[i];
2160 /* If either MIN or MAX overflowed, then set the resulting range to
2161 VARYING. But we do accept an overflow infinity
2162 representation. */
2163 if (min == NULL_TREE
2164 || !is_gimple_min_invariant (min)
2165 || (TREE_OVERFLOW (min) && !is_overflow_infinity (min))
2166 || max == NULL_TREE
2167 || !is_gimple_min_invariant (max)
2168 || (TREE_OVERFLOW (max) && !is_overflow_infinity (max)))
2170 set_value_range_to_varying (vr);
2171 return;
2174 /* We punt if:
2175 1) [-INF, +INF]
2176 2) [-INF, +-INF(OVF)]
2177 3) [+-INF(OVF), +INF]
2178 4) [+-INF(OVF), +-INF(OVF)]
2179 We learn nothing when we have INF and INF(OVF) on both sides.
2180 Note that we do accept [-INF, -INF] and [+INF, +INF] without
2181 overflow. */
2182 if ((vrp_val_is_min (min) || is_overflow_infinity (min))
2183 && (vrp_val_is_max (max) || is_overflow_infinity (max)))
2185 set_value_range_to_varying (vr);
2186 return;
2189 cmp = compare_values (min, max);
2190 if (cmp == -2 || cmp == 1)
2192 /* If the new range has its limits swapped around (MIN > MAX),
2193 then the operation caused one of them to wrap around, mark
2194 the new range VARYING. */
2195 set_value_range_to_varying (vr);
2197 else
2198 set_value_range (vr, type, min, max, NULL);
2201 /* Extract range information from a binary operation CODE based on
2202 the ranges of each of its operands *VR0 and *VR1 with resulting
2203 type EXPR_TYPE. The resulting range is stored in *VR. */
2205 static void
2206 extract_range_from_binary_expr_1 (value_range *vr,
2207 enum tree_code code, tree expr_type,
2208 value_range *vr0_, value_range *vr1_)
2210 value_range vr0 = *vr0_, vr1 = *vr1_;
2211 value_range vrtem0 = VR_INITIALIZER, vrtem1 = VR_INITIALIZER;
2212 enum value_range_type type;
2213 tree min = NULL_TREE, max = NULL_TREE;
2214 int cmp;
2216 if (!INTEGRAL_TYPE_P (expr_type)
2217 && !POINTER_TYPE_P (expr_type))
2219 set_value_range_to_varying (vr);
2220 return;
2223 /* Not all binary expressions can be applied to ranges in a
2224 meaningful way. Handle only arithmetic operations. */
2225 if (code != PLUS_EXPR
2226 && code != MINUS_EXPR
2227 && code != POINTER_PLUS_EXPR
2228 && code != MULT_EXPR
2229 && code != TRUNC_DIV_EXPR
2230 && code != FLOOR_DIV_EXPR
2231 && code != CEIL_DIV_EXPR
2232 && code != EXACT_DIV_EXPR
2233 && code != ROUND_DIV_EXPR
2234 && code != TRUNC_MOD_EXPR
2235 && code != RSHIFT_EXPR
2236 && code != LSHIFT_EXPR
2237 && code != MIN_EXPR
2238 && code != MAX_EXPR
2239 && code != BIT_AND_EXPR
2240 && code != BIT_IOR_EXPR
2241 && code != BIT_XOR_EXPR)
2243 set_value_range_to_varying (vr);
2244 return;
2247 /* If both ranges are UNDEFINED, so is the result. */
2248 if (vr0.type == VR_UNDEFINED && vr1.type == VR_UNDEFINED)
2250 set_value_range_to_undefined (vr);
2251 return;
2253 /* If one of the ranges is UNDEFINED drop it to VARYING for the following
2254 code. At some point we may want to special-case operations that
2255 have UNDEFINED result for all or some value-ranges of the not UNDEFINED
2256 operand. */
2257 else if (vr0.type == VR_UNDEFINED)
2258 set_value_range_to_varying (&vr0);
2259 else if (vr1.type == VR_UNDEFINED)
2260 set_value_range_to_varying (&vr1);
2262 /* We get imprecise results from ranges_from_anti_range when
2263 code is EXACT_DIV_EXPR. We could mask out bits in the resulting
2264 range, but then we also need to hack up vrp_meet. It's just
2265 easier to special case when vr0 is ~[0,0] for EXACT_DIV_EXPR. */
2266 if (code == EXACT_DIV_EXPR
2267 && vr0.type == VR_ANTI_RANGE
2268 && vr0.min == vr0.max
2269 && integer_zerop (vr0.min))
2271 set_value_range_to_nonnull (vr, expr_type);
2272 return;
2275 /* Now canonicalize anti-ranges to ranges when they are not symbolic
2276 and express ~[] op X as ([]' op X) U ([]'' op X). */
2277 if (vr0.type == VR_ANTI_RANGE
2278 && ranges_from_anti_range (&vr0, &vrtem0, &vrtem1))
2280 extract_range_from_binary_expr_1 (vr, code, expr_type, &vrtem0, vr1_);
2281 if (vrtem1.type != VR_UNDEFINED)
2283 value_range vrres = VR_INITIALIZER;
2284 extract_range_from_binary_expr_1 (&vrres, code, expr_type,
2285 &vrtem1, vr1_);
2286 vrp_meet (vr, &vrres);
2288 return;
2290 /* Likewise for X op ~[]. */
2291 if (vr1.type == VR_ANTI_RANGE
2292 && ranges_from_anti_range (&vr1, &vrtem0, &vrtem1))
2294 extract_range_from_binary_expr_1 (vr, code, expr_type, vr0_, &vrtem0);
2295 if (vrtem1.type != VR_UNDEFINED)
2297 value_range vrres = VR_INITIALIZER;
2298 extract_range_from_binary_expr_1 (&vrres, code, expr_type,
2299 vr0_, &vrtem1);
2300 vrp_meet (vr, &vrres);
2302 return;
2305 /* The type of the resulting value range defaults to VR0.TYPE. */
2306 type = vr0.type;
2308 /* Refuse to operate on VARYING ranges, ranges of different kinds
2309 and symbolic ranges. As an exception, we allow BIT_{AND,IOR}
2310 because we may be able to derive a useful range even if one of
2311 the operands is VR_VARYING or symbolic range. Similarly for
2312 divisions, MIN/MAX and PLUS/MINUS.
2314 TODO, we may be able to derive anti-ranges in some cases. */
2315 if (code != BIT_AND_EXPR
2316 && code != BIT_IOR_EXPR
2317 && code != TRUNC_DIV_EXPR
2318 && code != FLOOR_DIV_EXPR
2319 && code != CEIL_DIV_EXPR
2320 && code != EXACT_DIV_EXPR
2321 && code != ROUND_DIV_EXPR
2322 && code != TRUNC_MOD_EXPR
2323 && code != MIN_EXPR
2324 && code != MAX_EXPR
2325 && code != PLUS_EXPR
2326 && code != MINUS_EXPR
2327 && code != RSHIFT_EXPR
2328 && (vr0.type == VR_VARYING
2329 || vr1.type == VR_VARYING
2330 || vr0.type != vr1.type
2331 || symbolic_range_p (&vr0)
2332 || symbolic_range_p (&vr1)))
2334 set_value_range_to_varying (vr);
2335 return;
2338 /* Now evaluate the expression to determine the new range. */
2339 if (POINTER_TYPE_P (expr_type))
2341 if (code == MIN_EXPR || code == MAX_EXPR)
2343 /* For MIN/MAX expressions with pointers, we only care about
2344 nullness, if both are non null, then the result is nonnull.
2345 If both are null, then the result is null. Otherwise they
2346 are varying. */
2347 if (range_is_nonnull (&vr0) && range_is_nonnull (&vr1))
2348 set_value_range_to_nonnull (vr, expr_type);
2349 else if (range_is_null (&vr0) && range_is_null (&vr1))
2350 set_value_range_to_null (vr, expr_type);
2351 else
2352 set_value_range_to_varying (vr);
2354 else if (code == POINTER_PLUS_EXPR)
2356 /* For pointer types, we are really only interested in asserting
2357 whether the expression evaluates to non-NULL. */
2358 if (range_is_nonnull (&vr0) || range_is_nonnull (&vr1))
2359 set_value_range_to_nonnull (vr, expr_type);
2360 else if (range_is_null (&vr0) && range_is_null (&vr1))
2361 set_value_range_to_null (vr, expr_type);
2362 else
2363 set_value_range_to_varying (vr);
2365 else if (code == BIT_AND_EXPR)
2367 /* For pointer types, we are really only interested in asserting
2368 whether the expression evaluates to non-NULL. */
2369 if (range_is_nonnull (&vr0) && range_is_nonnull (&vr1))
2370 set_value_range_to_nonnull (vr, expr_type);
2371 else if (range_is_null (&vr0) || range_is_null (&vr1))
2372 set_value_range_to_null (vr, expr_type);
2373 else
2374 set_value_range_to_varying (vr);
2376 else
2377 set_value_range_to_varying (vr);
2379 return;
2382 /* For integer ranges, apply the operation to each end of the
2383 range and see what we end up with. */
2384 if (code == PLUS_EXPR || code == MINUS_EXPR)
2386 const bool minus_p = (code == MINUS_EXPR);
2387 tree min_op0 = vr0.min;
2388 tree min_op1 = minus_p ? vr1.max : vr1.min;
2389 tree max_op0 = vr0.max;
2390 tree max_op1 = minus_p ? vr1.min : vr1.max;
2391 tree sym_min_op0 = NULL_TREE;
2392 tree sym_min_op1 = NULL_TREE;
2393 tree sym_max_op0 = NULL_TREE;
2394 tree sym_max_op1 = NULL_TREE;
2395 bool neg_min_op0, neg_min_op1, neg_max_op0, neg_max_op1;
2397 /* If we have a PLUS or MINUS with two VR_RANGEs, either constant or
2398 single-symbolic ranges, try to compute the precise resulting range,
2399 but only if we know that this resulting range will also be constant
2400 or single-symbolic. */
2401 if (vr0.type == VR_RANGE && vr1.type == VR_RANGE
2402 && (TREE_CODE (min_op0) == INTEGER_CST
2403 || (sym_min_op0
2404 = get_single_symbol (min_op0, &neg_min_op0, &min_op0)))
2405 && (TREE_CODE (min_op1) == INTEGER_CST
2406 || (sym_min_op1
2407 = get_single_symbol (min_op1, &neg_min_op1, &min_op1)))
2408 && (!(sym_min_op0 && sym_min_op1)
2409 || (sym_min_op0 == sym_min_op1
2410 && neg_min_op0 == (minus_p ? neg_min_op1 : !neg_min_op1)))
2411 && (TREE_CODE (max_op0) == INTEGER_CST
2412 || (sym_max_op0
2413 = get_single_symbol (max_op0, &neg_max_op0, &max_op0)))
2414 && (TREE_CODE (max_op1) == INTEGER_CST
2415 || (sym_max_op1
2416 = get_single_symbol (max_op1, &neg_max_op1, &max_op1)))
2417 && (!(sym_max_op0 && sym_max_op1)
2418 || (sym_max_op0 == sym_max_op1
2419 && neg_max_op0 == (minus_p ? neg_max_op1 : !neg_max_op1))))
2421 const signop sgn = TYPE_SIGN (expr_type);
2422 const unsigned int prec = TYPE_PRECISION (expr_type);
2423 wide_int type_min, type_max, wmin, wmax;
2424 int min_ovf = 0;
2425 int max_ovf = 0;
2427 /* Get the lower and upper bounds of the type. */
2428 if (TYPE_OVERFLOW_WRAPS (expr_type))
2430 type_min = wi::min_value (prec, sgn);
2431 type_max = wi::max_value (prec, sgn);
2433 else
2435 type_min = vrp_val_min (expr_type);
2436 type_max = vrp_val_max (expr_type);
2439 /* Combine the lower bounds, if any. */
2440 if (min_op0 && min_op1)
2442 if (minus_p)
2444 wmin = wi::sub (min_op0, min_op1);
2446 /* Check for overflow. */
2447 if (wi::cmp (0, min_op1, sgn)
2448 != wi::cmp (wmin, min_op0, sgn))
2449 min_ovf = wi::cmp (min_op0, min_op1, sgn);
2451 else
2453 wmin = wi::add (min_op0, min_op1);
2455 /* Check for overflow. */
2456 if (wi::cmp (min_op1, 0, sgn)
2457 != wi::cmp (wmin, min_op0, sgn))
2458 min_ovf = wi::cmp (min_op0, wmin, sgn);
2461 else if (min_op0)
2462 wmin = min_op0;
2463 else if (min_op1)
2464 wmin = minus_p ? wi::neg (min_op1) : min_op1;
2465 else
2466 wmin = wi::shwi (0, prec);
2468 /* Combine the upper bounds, if any. */
2469 if (max_op0 && max_op1)
2471 if (minus_p)
2473 wmax = wi::sub (max_op0, max_op1);
2475 /* Check for overflow. */
2476 if (wi::cmp (0, max_op1, sgn)
2477 != wi::cmp (wmax, max_op0, sgn))
2478 max_ovf = wi::cmp (max_op0, max_op1, sgn);
2480 else
2482 wmax = wi::add (max_op0, max_op1);
2484 if (wi::cmp (max_op1, 0, sgn)
2485 != wi::cmp (wmax, max_op0, sgn))
2486 max_ovf = wi::cmp (max_op0, wmax, sgn);
2489 else if (max_op0)
2490 wmax = max_op0;
2491 else if (max_op1)
2492 wmax = minus_p ? wi::neg (max_op1) : max_op1;
2493 else
2494 wmax = wi::shwi (0, prec);
2496 /* Check for type overflow. */
2497 if (min_ovf == 0)
2499 if (wi::cmp (wmin, type_min, sgn) == -1)
2500 min_ovf = -1;
2501 else if (wi::cmp (wmin, type_max, sgn) == 1)
2502 min_ovf = 1;
2504 if (max_ovf == 0)
2506 if (wi::cmp (wmax, type_min, sgn) == -1)
2507 max_ovf = -1;
2508 else if (wi::cmp (wmax, type_max, sgn) == 1)
2509 max_ovf = 1;
2512 /* If we have overflow for the constant part and the resulting
2513 range will be symbolic, drop to VR_VARYING. */
2514 if ((min_ovf && sym_min_op0 != sym_min_op1)
2515 || (max_ovf && sym_max_op0 != sym_max_op1))
2517 set_value_range_to_varying (vr);
2518 return;
2521 if (TYPE_OVERFLOW_WRAPS (expr_type))
2523 /* If overflow wraps, truncate the values and adjust the
2524 range kind and bounds appropriately. */
2525 wide_int tmin = wide_int::from (wmin, prec, sgn);
2526 wide_int tmax = wide_int::from (wmax, prec, sgn);
2527 if (min_ovf == max_ovf)
2529 /* No overflow or both overflow or underflow. The
2530 range kind stays VR_RANGE. */
2531 min = wide_int_to_tree (expr_type, tmin);
2532 max = wide_int_to_tree (expr_type, tmax);
2534 else if ((min_ovf == -1 && max_ovf == 0)
2535 || (max_ovf == 1 && min_ovf == 0))
2537 /* Min underflow or max overflow. The range kind
2538 changes to VR_ANTI_RANGE. */
2539 bool covers = false;
2540 wide_int tem = tmin;
2541 type = VR_ANTI_RANGE;
2542 tmin = tmax + 1;
2543 if (wi::cmp (tmin, tmax, sgn) < 0)
2544 covers = true;
2545 tmax = tem - 1;
2546 if (wi::cmp (tmax, tem, sgn) > 0)
2547 covers = true;
2548 /* If the anti-range would cover nothing, drop to varying.
2549 Likewise if the anti-range bounds are outside of the
2550 types values. */
2551 if (covers || wi::cmp (tmin, tmax, sgn) > 0)
2553 set_value_range_to_varying (vr);
2554 return;
2556 min = wide_int_to_tree (expr_type, tmin);
2557 max = wide_int_to_tree (expr_type, tmax);
2559 else
2561 /* Other underflow and/or overflow, drop to VR_VARYING. */
2562 set_value_range_to_varying (vr);
2563 return;
2566 else
2568 /* If overflow does not wrap, saturate to the types min/max
2569 value. */
2570 if (min_ovf == -1)
2572 if (needs_overflow_infinity (expr_type)
2573 && supports_overflow_infinity (expr_type))
2574 min = negative_overflow_infinity (expr_type);
2575 else
2576 min = wide_int_to_tree (expr_type, type_min);
2578 else if (min_ovf == 1)
2580 if (needs_overflow_infinity (expr_type)
2581 && supports_overflow_infinity (expr_type))
2582 min = positive_overflow_infinity (expr_type);
2583 else
2584 min = wide_int_to_tree (expr_type, type_max);
2586 else
2587 min = wide_int_to_tree (expr_type, wmin);
2589 if (max_ovf == -1)
2591 if (needs_overflow_infinity (expr_type)
2592 && supports_overflow_infinity (expr_type))
2593 max = negative_overflow_infinity (expr_type);
2594 else
2595 max = wide_int_to_tree (expr_type, type_min);
2597 else if (max_ovf == 1)
2599 if (needs_overflow_infinity (expr_type)
2600 && supports_overflow_infinity (expr_type))
2601 max = positive_overflow_infinity (expr_type);
2602 else
2603 max = wide_int_to_tree (expr_type, type_max);
2605 else
2606 max = wide_int_to_tree (expr_type, wmax);
2609 if (needs_overflow_infinity (expr_type)
2610 && supports_overflow_infinity (expr_type))
2612 if ((min_op0 && is_negative_overflow_infinity (min_op0))
2613 || (min_op1
2614 && (minus_p
2615 ? is_positive_overflow_infinity (min_op1)
2616 : is_negative_overflow_infinity (min_op1))))
2617 min = negative_overflow_infinity (expr_type);
2618 if ((max_op0 && is_positive_overflow_infinity (max_op0))
2619 || (max_op1
2620 && (minus_p
2621 ? is_negative_overflow_infinity (max_op1)
2622 : is_positive_overflow_infinity (max_op1))))
2623 max = positive_overflow_infinity (expr_type);
2626 /* If the result lower bound is constant, we're done;
2627 otherwise, build the symbolic lower bound. */
2628 if (sym_min_op0 == sym_min_op1)
2630 else if (sym_min_op0)
2631 min = build_symbolic_expr (expr_type, sym_min_op0,
2632 neg_min_op0, min);
2633 else if (sym_min_op1)
2635 /* We may not negate if that might introduce
2636 undefined overflow. */
2637 if (! minus_p
2638 || neg_min_op1
2639 || TYPE_OVERFLOW_WRAPS (expr_type))
2640 min = build_symbolic_expr (expr_type, sym_min_op1,
2641 neg_min_op1 ^ minus_p, min);
2642 else
2643 min = NULL_TREE;
2646 /* Likewise for the upper bound. */
2647 if (sym_max_op0 == sym_max_op1)
2649 else if (sym_max_op0)
2650 max = build_symbolic_expr (expr_type, sym_max_op0,
2651 neg_max_op0, max);
2652 else if (sym_max_op1)
2654 /* We may not negate if that might introduce
2655 undefined overflow. */
2656 if (! minus_p
2657 || neg_max_op1
2658 || TYPE_OVERFLOW_WRAPS (expr_type))
2659 max = build_symbolic_expr (expr_type, sym_max_op1,
2660 neg_max_op1 ^ minus_p, max);
2661 else
2662 max = NULL_TREE;
2665 else
2667 /* For other cases, for example if we have a PLUS_EXPR with two
2668 VR_ANTI_RANGEs, drop to VR_VARYING. It would take more effort
2669 to compute a precise range for such a case.
2670 ??? General even mixed range kind operations can be expressed
2671 by for example transforming ~[3, 5] + [1, 2] to range-only
2672 operations and a union primitive:
2673 [-INF, 2] + [1, 2] U [5, +INF] + [1, 2]
2674 [-INF+1, 4] U [6, +INF(OVF)]
2675 though usually the union is not exactly representable with
2676 a single range or anti-range as the above is
2677 [-INF+1, +INF(OVF)] intersected with ~[5, 5]
2678 but one could use a scheme similar to equivalences for this. */
2679 set_value_range_to_varying (vr);
2680 return;
2683 else if (code == MIN_EXPR
2684 || code == MAX_EXPR)
2686 if (vr0.type == VR_RANGE
2687 && !symbolic_range_p (&vr0))
2689 type = VR_RANGE;
2690 if (vr1.type == VR_RANGE
2691 && !symbolic_range_p (&vr1))
2693 /* For operations that make the resulting range directly
2694 proportional to the original ranges, apply the operation to
2695 the same end of each range. */
2696 min = vrp_int_const_binop (code, vr0.min, vr1.min);
2697 max = vrp_int_const_binop (code, vr0.max, vr1.max);
2699 else if (code == MIN_EXPR)
2701 min = vrp_val_min (expr_type);
2702 max = vr0.max;
2704 else if (code == MAX_EXPR)
2706 min = vr0.min;
2707 max = vrp_val_max (expr_type);
2710 else if (vr1.type == VR_RANGE
2711 && !symbolic_range_p (&vr1))
2713 type = VR_RANGE;
2714 if (code == MIN_EXPR)
2716 min = vrp_val_min (expr_type);
2717 max = vr1.max;
2719 else if (code == MAX_EXPR)
2721 min = vr1.min;
2722 max = vrp_val_max (expr_type);
2725 else
2727 set_value_range_to_varying (vr);
2728 return;
2731 else if (code == MULT_EXPR)
2733 /* Fancy code so that with unsigned, [-3,-1]*[-3,-1] does not
2734 drop to varying. This test requires 2*prec bits if both
2735 operands are signed and 2*prec + 2 bits if either is not. */
2737 signop sign = TYPE_SIGN (expr_type);
2738 unsigned int prec = TYPE_PRECISION (expr_type);
2740 if (range_int_cst_p (&vr0)
2741 && range_int_cst_p (&vr1)
2742 && TYPE_OVERFLOW_WRAPS (expr_type))
2744 typedef FIXED_WIDE_INT (WIDE_INT_MAX_PRECISION * 2) vrp_int;
2745 typedef generic_wide_int
2746 <wi::extended_tree <WIDE_INT_MAX_PRECISION * 2> > vrp_int_cst;
2747 vrp_int sizem1 = wi::mask <vrp_int> (prec, false);
2748 vrp_int size = sizem1 + 1;
2750 /* Extend the values using the sign of the result to PREC2.
2751 From here on out, everthing is just signed math no matter
2752 what the input types were. */
2753 vrp_int min0 = vrp_int_cst (vr0.min);
2754 vrp_int max0 = vrp_int_cst (vr0.max);
2755 vrp_int min1 = vrp_int_cst (vr1.min);
2756 vrp_int max1 = vrp_int_cst (vr1.max);
2757 /* Canonicalize the intervals. */
2758 if (sign == UNSIGNED)
2760 if (wi::ltu_p (size, min0 + max0))
2762 min0 -= size;
2763 max0 -= size;
2766 if (wi::ltu_p (size, min1 + max1))
2768 min1 -= size;
2769 max1 -= size;
2773 vrp_int prod0 = min0 * min1;
2774 vrp_int prod1 = min0 * max1;
2775 vrp_int prod2 = max0 * min1;
2776 vrp_int prod3 = max0 * max1;
2778 /* Sort the 4 products so that min is in prod0 and max is in
2779 prod3. */
2780 /* min0min1 > max0max1 */
2781 if (prod0 > prod3)
2782 std::swap (prod0, prod3);
2784 /* min0max1 > max0min1 */
2785 if (prod1 > prod2)
2786 std::swap (prod1, prod2);
2788 if (prod0 > prod1)
2789 std::swap (prod0, prod1);
2791 if (prod2 > prod3)
2792 std::swap (prod2, prod3);
2794 /* diff = max - min. */
2795 prod2 = prod3 - prod0;
2796 if (wi::geu_p (prod2, sizem1))
2798 /* the range covers all values. */
2799 set_value_range_to_varying (vr);
2800 return;
2803 /* The following should handle the wrapping and selecting
2804 VR_ANTI_RANGE for us. */
2805 min = wide_int_to_tree (expr_type, prod0);
2806 max = wide_int_to_tree (expr_type, prod3);
2807 set_and_canonicalize_value_range (vr, VR_RANGE, min, max, NULL);
2808 return;
2811 /* If we have an unsigned MULT_EXPR with two VR_ANTI_RANGEs,
2812 drop to VR_VARYING. It would take more effort to compute a
2813 precise range for such a case. For example, if we have
2814 op0 == 65536 and op1 == 65536 with their ranges both being
2815 ~[0,0] on a 32-bit machine, we would have op0 * op1 == 0, so
2816 we cannot claim that the product is in ~[0,0]. Note that we
2817 are guaranteed to have vr0.type == vr1.type at this
2818 point. */
2819 if (vr0.type == VR_ANTI_RANGE
2820 && !TYPE_OVERFLOW_UNDEFINED (expr_type))
2822 set_value_range_to_varying (vr);
2823 return;
2826 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
2827 return;
2829 else if (code == RSHIFT_EXPR
2830 || code == LSHIFT_EXPR)
2832 /* If we have a RSHIFT_EXPR with any shift values outside [0..prec-1],
2833 then drop to VR_VARYING. Outside of this range we get undefined
2834 behavior from the shift operation. We cannot even trust
2835 SHIFT_COUNT_TRUNCATED at this stage, because that applies to rtl
2836 shifts, and the operation at the tree level may be widened. */
2837 if (range_int_cst_p (&vr1)
2838 && compare_tree_int (vr1.min, 0) >= 0
2839 && compare_tree_int (vr1.max, TYPE_PRECISION (expr_type)) == -1)
2841 if (code == RSHIFT_EXPR)
2843 /* Even if vr0 is VARYING or otherwise not usable, we can derive
2844 useful ranges just from the shift count. E.g.
2845 x >> 63 for signed 64-bit x is always [-1, 0]. */
2846 if (vr0.type != VR_RANGE || symbolic_range_p (&vr0))
2848 vr0.type = type = VR_RANGE;
2849 vr0.min = vrp_val_min (expr_type);
2850 vr0.max = vrp_val_max (expr_type);
2852 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
2853 return;
2855 /* We can map lshifts by constants to MULT_EXPR handling. */
2856 else if (code == LSHIFT_EXPR
2857 && range_int_cst_singleton_p (&vr1))
2859 bool saved_flag_wrapv;
2860 value_range vr1p = VR_INITIALIZER;
2861 vr1p.type = VR_RANGE;
2862 vr1p.min = (wide_int_to_tree
2863 (expr_type,
2864 wi::set_bit_in_zero (tree_to_shwi (vr1.min),
2865 TYPE_PRECISION (expr_type))));
2866 vr1p.max = vr1p.min;
2867 /* We have to use a wrapping multiply though as signed overflow
2868 on lshifts is implementation defined in C89. */
2869 saved_flag_wrapv = flag_wrapv;
2870 flag_wrapv = 1;
2871 extract_range_from_binary_expr_1 (vr, MULT_EXPR, expr_type,
2872 &vr0, &vr1p);
2873 flag_wrapv = saved_flag_wrapv;
2874 return;
2876 else if (code == LSHIFT_EXPR
2877 && range_int_cst_p (&vr0))
2879 int prec = TYPE_PRECISION (expr_type);
2880 int overflow_pos = prec;
2881 int bound_shift;
2882 wide_int low_bound, high_bound;
2883 bool uns = TYPE_UNSIGNED (expr_type);
2884 bool in_bounds = false;
2886 if (!uns)
2887 overflow_pos -= 1;
2889 bound_shift = overflow_pos - tree_to_shwi (vr1.max);
2890 /* If bound_shift == HOST_BITS_PER_WIDE_INT, the llshift can
2891 overflow. However, for that to happen, vr1.max needs to be
2892 zero, which means vr1 is a singleton range of zero, which
2893 means it should be handled by the previous LSHIFT_EXPR
2894 if-clause. */
2895 wide_int bound = wi::set_bit_in_zero (bound_shift, prec);
2896 wide_int complement = ~(bound - 1);
2898 if (uns)
2900 low_bound = bound;
2901 high_bound = complement;
2902 if (wi::ltu_p (vr0.max, low_bound))
2904 /* [5, 6] << [1, 2] == [10, 24]. */
2905 /* We're shifting out only zeroes, the value increases
2906 monotonically. */
2907 in_bounds = true;
2909 else if (wi::ltu_p (high_bound, vr0.min))
2911 /* [0xffffff00, 0xffffffff] << [1, 2]
2912 == [0xfffffc00, 0xfffffffe]. */
2913 /* We're shifting out only ones, the value decreases
2914 monotonically. */
2915 in_bounds = true;
2918 else
2920 /* [-1, 1] << [1, 2] == [-4, 4]. */
2921 low_bound = complement;
2922 high_bound = bound;
2923 if (wi::lts_p (vr0.max, high_bound)
2924 && wi::lts_p (low_bound, vr0.min))
2926 /* For non-negative numbers, we're shifting out only
2927 zeroes, the value increases monotonically.
2928 For negative numbers, we're shifting out only ones, the
2929 value decreases monotomically. */
2930 in_bounds = true;
2934 if (in_bounds)
2936 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
2937 return;
2941 set_value_range_to_varying (vr);
2942 return;
2944 else if (code == TRUNC_DIV_EXPR
2945 || code == FLOOR_DIV_EXPR
2946 || code == CEIL_DIV_EXPR
2947 || code == EXACT_DIV_EXPR
2948 || code == ROUND_DIV_EXPR)
2950 if (vr0.type != VR_RANGE || symbolic_range_p (&vr0))
2952 /* For division, if op1 has VR_RANGE but op0 does not, something
2953 can be deduced just from that range. Say [min, max] / [4, max]
2954 gives [min / 4, max / 4] range. */
2955 if (vr1.type == VR_RANGE
2956 && !symbolic_range_p (&vr1)
2957 && range_includes_zero_p (vr1.min, vr1.max) == 0)
2959 vr0.type = type = VR_RANGE;
2960 vr0.min = vrp_val_min (expr_type);
2961 vr0.max = vrp_val_max (expr_type);
2963 else
2965 set_value_range_to_varying (vr);
2966 return;
2970 /* For divisions, if flag_non_call_exceptions is true, we must
2971 not eliminate a division by zero. */
2972 if (cfun->can_throw_non_call_exceptions
2973 && (vr1.type != VR_RANGE
2974 || range_includes_zero_p (vr1.min, vr1.max) != 0))
2976 set_value_range_to_varying (vr);
2977 return;
2980 /* For divisions, if op0 is VR_RANGE, we can deduce a range
2981 even if op1 is VR_VARYING, VR_ANTI_RANGE, symbolic or can
2982 include 0. */
2983 if (vr0.type == VR_RANGE
2984 && (vr1.type != VR_RANGE
2985 || range_includes_zero_p (vr1.min, vr1.max) != 0))
2987 tree zero = build_int_cst (TREE_TYPE (vr0.min), 0);
2988 int cmp;
2990 min = NULL_TREE;
2991 max = NULL_TREE;
2992 if (TYPE_UNSIGNED (expr_type)
2993 || value_range_nonnegative_p (&vr1))
2995 /* For unsigned division or when divisor is known
2996 to be non-negative, the range has to cover
2997 all numbers from 0 to max for positive max
2998 and all numbers from min to 0 for negative min. */
2999 cmp = compare_values (vr0.max, zero);
3000 if (cmp == -1)
3002 /* When vr0.max < 0, vr1.min != 0 and value
3003 ranges for dividend and divisor are available. */
3004 if (vr1.type == VR_RANGE
3005 && !symbolic_range_p (&vr0)
3006 && !symbolic_range_p (&vr1)
3007 && compare_values (vr1.min, zero) != 0)
3008 max = int_const_binop (code, vr0.max, vr1.min);
3009 else
3010 max = zero;
3012 else if (cmp == 0 || cmp == 1)
3013 max = vr0.max;
3014 else
3015 type = VR_VARYING;
3016 cmp = compare_values (vr0.min, zero);
3017 if (cmp == 1)
3019 /* For unsigned division when value ranges for dividend
3020 and divisor are available. */
3021 if (vr1.type == VR_RANGE
3022 && !symbolic_range_p (&vr0)
3023 && !symbolic_range_p (&vr1)
3024 && compare_values (vr1.max, zero) != 0)
3025 min = int_const_binop (code, vr0.min, vr1.max);
3026 else
3027 min = zero;
3029 else if (cmp == 0 || cmp == -1)
3030 min = vr0.min;
3031 else
3032 type = VR_VARYING;
3034 else
3036 /* Otherwise the range is -max .. max or min .. -min
3037 depending on which bound is bigger in absolute value,
3038 as the division can change the sign. */
3039 abs_extent_range (vr, vr0.min, vr0.max);
3040 return;
3042 if (type == VR_VARYING)
3044 set_value_range_to_varying (vr);
3045 return;
3048 else if (!symbolic_range_p (&vr0) && !symbolic_range_p (&vr1))
3050 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
3051 return;
3054 else if (code == TRUNC_MOD_EXPR)
3056 if (range_is_null (&vr1))
3058 set_value_range_to_undefined (vr);
3059 return;
3061 /* ABS (A % B) < ABS (B) and either
3062 0 <= A % B <= A or A <= A % B <= 0. */
3063 type = VR_RANGE;
3064 signop sgn = TYPE_SIGN (expr_type);
3065 unsigned int prec = TYPE_PRECISION (expr_type);
3066 wide_int wmin, wmax, tmp;
3067 wide_int zero = wi::zero (prec);
3068 wide_int one = wi::one (prec);
3069 if (vr1.type == VR_RANGE && !symbolic_range_p (&vr1))
3071 wmax = wi::sub (vr1.max, one);
3072 if (sgn == SIGNED)
3074 tmp = wi::sub (wi::minus_one (prec), vr1.min);
3075 wmax = wi::smax (wmax, tmp);
3078 else
3080 wmax = wi::max_value (prec, sgn);
3081 /* X % INT_MIN may be INT_MAX. */
3082 if (sgn == UNSIGNED)
3083 wmax = wmax - one;
3086 if (sgn == UNSIGNED)
3087 wmin = zero;
3088 else
3090 wmin = -wmax;
3091 if (vr0.type == VR_RANGE && TREE_CODE (vr0.min) == INTEGER_CST)
3093 tmp = vr0.min;
3094 if (wi::gts_p (tmp, zero))
3095 tmp = zero;
3096 wmin = wi::smax (wmin, tmp);
3100 if (vr0.type == VR_RANGE && TREE_CODE (vr0.max) == INTEGER_CST)
3102 tmp = vr0.max;
3103 if (sgn == SIGNED && wi::neg_p (tmp))
3104 tmp = zero;
3105 wmax = wi::min (wmax, tmp, sgn);
3108 min = wide_int_to_tree (expr_type, wmin);
3109 max = wide_int_to_tree (expr_type, wmax);
3111 else if (code == BIT_AND_EXPR || code == BIT_IOR_EXPR || code == BIT_XOR_EXPR)
3113 bool int_cst_range0, int_cst_range1;
3114 wide_int may_be_nonzero0, may_be_nonzero1;
3115 wide_int must_be_nonzero0, must_be_nonzero1;
3117 int_cst_range0 = zero_nonzero_bits_from_vr (expr_type, &vr0,
3118 &may_be_nonzero0,
3119 &must_be_nonzero0);
3120 int_cst_range1 = zero_nonzero_bits_from_vr (expr_type, &vr1,
3121 &may_be_nonzero1,
3122 &must_be_nonzero1);
3124 type = VR_RANGE;
3125 if (code == BIT_AND_EXPR)
3127 min = wide_int_to_tree (expr_type,
3128 must_be_nonzero0 & must_be_nonzero1);
3129 wide_int wmax = may_be_nonzero0 & may_be_nonzero1;
3130 /* If both input ranges contain only negative values we can
3131 truncate the result range maximum to the minimum of the
3132 input range maxima. */
3133 if (int_cst_range0 && int_cst_range1
3134 && tree_int_cst_sgn (vr0.max) < 0
3135 && tree_int_cst_sgn (vr1.max) < 0)
3137 wmax = wi::min (wmax, vr0.max, TYPE_SIGN (expr_type));
3138 wmax = wi::min (wmax, vr1.max, TYPE_SIGN (expr_type));
3140 /* If either input range contains only non-negative values
3141 we can truncate the result range maximum to the respective
3142 maximum of the input range. */
3143 if (int_cst_range0 && tree_int_cst_sgn (vr0.min) >= 0)
3144 wmax = wi::min (wmax, vr0.max, TYPE_SIGN (expr_type));
3145 if (int_cst_range1 && tree_int_cst_sgn (vr1.min) >= 0)
3146 wmax = wi::min (wmax, vr1.max, TYPE_SIGN (expr_type));
3147 max = wide_int_to_tree (expr_type, wmax);
3148 cmp = compare_values (min, max);
3149 /* PR68217: In case of signed & sign-bit-CST should
3150 result in [-INF, 0] instead of [-INF, INF]. */
3151 if (cmp == -2 || cmp == 1)
3153 wide_int sign_bit
3154 = wi::set_bit_in_zero (TYPE_PRECISION (expr_type) - 1,
3155 TYPE_PRECISION (expr_type));
3156 if (!TYPE_UNSIGNED (expr_type)
3157 && ((value_range_constant_singleton (&vr0)
3158 && !wi::cmps (vr0.min, sign_bit))
3159 || (value_range_constant_singleton (&vr1)
3160 && !wi::cmps (vr1.min, sign_bit))))
3162 min = TYPE_MIN_VALUE (expr_type);
3163 max = build_int_cst (expr_type, 0);
3167 else if (code == BIT_IOR_EXPR)
3169 max = wide_int_to_tree (expr_type,
3170 may_be_nonzero0 | may_be_nonzero1);
3171 wide_int wmin = must_be_nonzero0 | must_be_nonzero1;
3172 /* If the input ranges contain only positive values we can
3173 truncate the minimum of the result range to the maximum
3174 of the input range minima. */
3175 if (int_cst_range0 && int_cst_range1
3176 && tree_int_cst_sgn (vr0.min) >= 0
3177 && tree_int_cst_sgn (vr1.min) >= 0)
3179 wmin = wi::max (wmin, vr0.min, TYPE_SIGN (expr_type));
3180 wmin = wi::max (wmin, vr1.min, TYPE_SIGN (expr_type));
3182 /* If either input range contains only negative values
3183 we can truncate the minimum of the result range to the
3184 respective minimum range. */
3185 if (int_cst_range0 && tree_int_cst_sgn (vr0.max) < 0)
3186 wmin = wi::max (wmin, vr0.min, TYPE_SIGN (expr_type));
3187 if (int_cst_range1 && tree_int_cst_sgn (vr1.max) < 0)
3188 wmin = wi::max (wmin, vr1.min, TYPE_SIGN (expr_type));
3189 min = wide_int_to_tree (expr_type, wmin);
3191 else if (code == BIT_XOR_EXPR)
3193 wide_int result_zero_bits = ((must_be_nonzero0 & must_be_nonzero1)
3194 | ~(may_be_nonzero0 | may_be_nonzero1));
3195 wide_int result_one_bits
3196 = (must_be_nonzero0.and_not (may_be_nonzero1)
3197 | must_be_nonzero1.and_not (may_be_nonzero0));
3198 max = wide_int_to_tree (expr_type, ~result_zero_bits);
3199 min = wide_int_to_tree (expr_type, result_one_bits);
3200 /* If the range has all positive or all negative values the
3201 result is better than VARYING. */
3202 if (tree_int_cst_sgn (min) < 0
3203 || tree_int_cst_sgn (max) >= 0)
3205 else
3206 max = min = NULL_TREE;
3209 else
3210 gcc_unreachable ();
3212 /* If either MIN or MAX overflowed, then set the resulting range to
3213 VARYING. But we do accept an overflow infinity representation. */
3214 if (min == NULL_TREE
3215 || (TREE_OVERFLOW_P (min) && !is_overflow_infinity (min))
3216 || max == NULL_TREE
3217 || (TREE_OVERFLOW_P (max) && !is_overflow_infinity (max)))
3219 set_value_range_to_varying (vr);
3220 return;
3223 /* We punt if:
3224 1) [-INF, +INF]
3225 2) [-INF, +-INF(OVF)]
3226 3) [+-INF(OVF), +INF]
3227 4) [+-INF(OVF), +-INF(OVF)]
3228 We learn nothing when we have INF and INF(OVF) on both sides.
3229 Note that we do accept [-INF, -INF] and [+INF, +INF] without
3230 overflow. */
3231 if ((vrp_val_is_min (min) || is_overflow_infinity (min))
3232 && (vrp_val_is_max (max) || is_overflow_infinity (max)))
3234 set_value_range_to_varying (vr);
3235 return;
3238 cmp = compare_values (min, max);
3239 if (cmp == -2 || cmp == 1)
3241 /* If the new range has its limits swapped around (MIN > MAX),
3242 then the operation caused one of them to wrap around, mark
3243 the new range VARYING. */
3244 set_value_range_to_varying (vr);
3246 else
3247 set_value_range (vr, type, min, max, NULL);
3250 /* Extract range information from a binary expression OP0 CODE OP1 based on
3251 the ranges of each of its operands with resulting type EXPR_TYPE.
3252 The resulting range is stored in *VR. */
3254 static void
3255 extract_range_from_binary_expr (value_range *vr,
3256 enum tree_code code,
3257 tree expr_type, tree op0, tree op1)
3259 value_range vr0 = VR_INITIALIZER;
3260 value_range vr1 = VR_INITIALIZER;
3262 /* Get value ranges for each operand. For constant operands, create
3263 a new value range with the operand to simplify processing. */
3264 if (TREE_CODE (op0) == SSA_NAME)
3265 vr0 = *(get_value_range (op0));
3266 else if (is_gimple_min_invariant (op0))
3267 set_value_range_to_value (&vr0, op0, NULL);
3268 else
3269 set_value_range_to_varying (&vr0);
3271 if (TREE_CODE (op1) == SSA_NAME)
3272 vr1 = *(get_value_range (op1));
3273 else if (is_gimple_min_invariant (op1))
3274 set_value_range_to_value (&vr1, op1, NULL);
3275 else
3276 set_value_range_to_varying (&vr1);
3278 extract_range_from_binary_expr_1 (vr, code, expr_type, &vr0, &vr1);
3280 /* Try harder for PLUS and MINUS if the range of one operand is symbolic
3281 and based on the other operand, for example if it was deduced from a
3282 symbolic comparison. When a bound of the range of the first operand
3283 is invariant, we set the corresponding bound of the new range to INF
3284 in order to avoid recursing on the range of the second operand. */
3285 if (vr->type == VR_VARYING
3286 && (code == PLUS_EXPR || code == MINUS_EXPR)
3287 && TREE_CODE (op1) == SSA_NAME
3288 && vr0.type == VR_RANGE
3289 && symbolic_range_based_on_p (&vr0, op1))
3291 const bool minus_p = (code == MINUS_EXPR);
3292 value_range n_vr1 = VR_INITIALIZER;
3294 /* Try with VR0 and [-INF, OP1]. */
3295 if (is_gimple_min_invariant (minus_p ? vr0.max : vr0.min))
3296 set_value_range (&n_vr1, VR_RANGE, vrp_val_min (expr_type), op1, NULL);
3298 /* Try with VR0 and [OP1, +INF]. */
3299 else if (is_gimple_min_invariant (minus_p ? vr0.min : vr0.max))
3300 set_value_range (&n_vr1, VR_RANGE, op1, vrp_val_max (expr_type), NULL);
3302 /* Try with VR0 and [OP1, OP1]. */
3303 else
3304 set_value_range (&n_vr1, VR_RANGE, op1, op1, NULL);
3306 extract_range_from_binary_expr_1 (vr, code, expr_type, &vr0, &n_vr1);
3309 if (vr->type == VR_VARYING
3310 && (code == PLUS_EXPR || code == MINUS_EXPR)
3311 && TREE_CODE (op0) == SSA_NAME
3312 && vr1.type == VR_RANGE
3313 && symbolic_range_based_on_p (&vr1, op0))
3315 const bool minus_p = (code == MINUS_EXPR);
3316 value_range n_vr0 = VR_INITIALIZER;
3318 /* Try with [-INF, OP0] and VR1. */
3319 if (is_gimple_min_invariant (minus_p ? vr1.max : vr1.min))
3320 set_value_range (&n_vr0, VR_RANGE, vrp_val_min (expr_type), op0, NULL);
3322 /* Try with [OP0, +INF] and VR1. */
3323 else if (is_gimple_min_invariant (minus_p ? vr1.min : vr1.max))
3324 set_value_range (&n_vr0, VR_RANGE, op0, vrp_val_max (expr_type), NULL);
3326 /* Try with [OP0, OP0] and VR1. */
3327 else
3328 set_value_range (&n_vr0, VR_RANGE, op0, op0, NULL);
3330 extract_range_from_binary_expr_1 (vr, code, expr_type, &n_vr0, &vr1);
3333 /* If we didn't derive a range for MINUS_EXPR, and
3334 op1's range is ~[op0,op0] or vice-versa, then we
3335 can derive a non-null range. This happens often for
3336 pointer subtraction. */
3337 if (vr->type == VR_VARYING
3338 && code == MINUS_EXPR
3339 && TREE_CODE (op0) == SSA_NAME
3340 && ((vr0.type == VR_ANTI_RANGE
3341 && vr0.min == op1
3342 && vr0.min == vr0.max)
3343 || (vr1.type == VR_ANTI_RANGE
3344 && vr1.min == op0
3345 && vr1.min == vr1.max)))
3346 set_value_range_to_nonnull (vr, TREE_TYPE (op0));
3349 /* Extract range information from a unary operation CODE based on
3350 the range of its operand *VR0 with type OP0_TYPE with resulting type TYPE.
3351 The resulting range is stored in *VR. */
3353 void
3354 extract_range_from_unary_expr (value_range *vr,
3355 enum tree_code code, tree type,
3356 value_range *vr0_, tree op0_type)
3358 value_range vr0 = *vr0_, vrtem0 = VR_INITIALIZER, vrtem1 = VR_INITIALIZER;
3360 /* VRP only operates on integral and pointer types. */
3361 if (!(INTEGRAL_TYPE_P (op0_type)
3362 || POINTER_TYPE_P (op0_type))
3363 || !(INTEGRAL_TYPE_P (type)
3364 || POINTER_TYPE_P (type)))
3366 set_value_range_to_varying (vr);
3367 return;
3370 /* If VR0 is UNDEFINED, so is the result. */
3371 if (vr0.type == VR_UNDEFINED)
3373 set_value_range_to_undefined (vr);
3374 return;
3377 /* Handle operations that we express in terms of others. */
3378 if (code == PAREN_EXPR || code == OBJ_TYPE_REF)
3380 /* PAREN_EXPR and OBJ_TYPE_REF are simple copies. */
3381 copy_value_range (vr, &vr0);
3382 return;
3384 else if (code == NEGATE_EXPR)
3386 /* -X is simply 0 - X, so re-use existing code that also handles
3387 anti-ranges fine. */
3388 value_range zero = VR_INITIALIZER;
3389 set_value_range_to_value (&zero, build_int_cst (type, 0), NULL);
3390 extract_range_from_binary_expr_1 (vr, MINUS_EXPR, type, &zero, &vr0);
3391 return;
3393 else if (code == BIT_NOT_EXPR)
3395 /* ~X is simply -1 - X, so re-use existing code that also handles
3396 anti-ranges fine. */
3397 value_range minusone = VR_INITIALIZER;
3398 set_value_range_to_value (&minusone, build_int_cst (type, -1), NULL);
3399 extract_range_from_binary_expr_1 (vr, MINUS_EXPR,
3400 type, &minusone, &vr0);
3401 return;
3404 /* Now canonicalize anti-ranges to ranges when they are not symbolic
3405 and express op ~[] as (op []') U (op []''). */
3406 if (vr0.type == VR_ANTI_RANGE
3407 && ranges_from_anti_range (&vr0, &vrtem0, &vrtem1))
3409 extract_range_from_unary_expr (vr, code, type, &vrtem0, op0_type);
3410 if (vrtem1.type != VR_UNDEFINED)
3412 value_range vrres = VR_INITIALIZER;
3413 extract_range_from_unary_expr (&vrres, code, type,
3414 &vrtem1, op0_type);
3415 vrp_meet (vr, &vrres);
3417 return;
3420 if (CONVERT_EXPR_CODE_P (code))
3422 tree inner_type = op0_type;
3423 tree outer_type = type;
3425 /* If the expression evaluates to a pointer, we are only interested in
3426 determining if it evaluates to NULL [0, 0] or non-NULL (~[0, 0]). */
3427 if (POINTER_TYPE_P (type))
3429 if (range_is_nonnull (&vr0))
3430 set_value_range_to_nonnull (vr, type);
3431 else if (range_is_null (&vr0))
3432 set_value_range_to_null (vr, type);
3433 else
3434 set_value_range_to_varying (vr);
3435 return;
3438 /* If VR0 is varying and we increase the type precision, assume
3439 a full range for the following transformation. */
3440 if (vr0.type == VR_VARYING
3441 && INTEGRAL_TYPE_P (inner_type)
3442 && TYPE_PRECISION (inner_type) < TYPE_PRECISION (outer_type))
3444 vr0.type = VR_RANGE;
3445 vr0.min = TYPE_MIN_VALUE (inner_type);
3446 vr0.max = TYPE_MAX_VALUE (inner_type);
3449 /* If VR0 is a constant range or anti-range and the conversion is
3450 not truncating we can convert the min and max values and
3451 canonicalize the resulting range. Otherwise we can do the
3452 conversion if the size of the range is less than what the
3453 precision of the target type can represent and the range is
3454 not an anti-range. */
3455 if ((vr0.type == VR_RANGE
3456 || vr0.type == VR_ANTI_RANGE)
3457 && TREE_CODE (vr0.min) == INTEGER_CST
3458 && TREE_CODE (vr0.max) == INTEGER_CST
3459 && (!is_overflow_infinity (vr0.min)
3460 || (vr0.type == VR_RANGE
3461 && TYPE_PRECISION (outer_type) > TYPE_PRECISION (inner_type)
3462 && needs_overflow_infinity (outer_type)
3463 && supports_overflow_infinity (outer_type)))
3464 && (!is_overflow_infinity (vr0.max)
3465 || (vr0.type == VR_RANGE
3466 && TYPE_PRECISION (outer_type) > TYPE_PRECISION (inner_type)
3467 && needs_overflow_infinity (outer_type)
3468 && supports_overflow_infinity (outer_type)))
3469 && (TYPE_PRECISION (outer_type) >= TYPE_PRECISION (inner_type)
3470 || (vr0.type == VR_RANGE
3471 && integer_zerop (int_const_binop (RSHIFT_EXPR,
3472 int_const_binop (MINUS_EXPR, vr0.max, vr0.min),
3473 size_int (TYPE_PRECISION (outer_type)))))))
3475 tree new_min, new_max;
3476 if (is_overflow_infinity (vr0.min))
3477 new_min = negative_overflow_infinity (outer_type);
3478 else
3479 new_min = force_fit_type (outer_type, wi::to_widest (vr0.min),
3480 0, false);
3481 if (is_overflow_infinity (vr0.max))
3482 new_max = positive_overflow_infinity (outer_type);
3483 else
3484 new_max = force_fit_type (outer_type, wi::to_widest (vr0.max),
3485 0, false);
3486 set_and_canonicalize_value_range (vr, vr0.type,
3487 new_min, new_max, NULL);
3488 return;
3491 set_value_range_to_varying (vr);
3492 return;
3494 else if (code == ABS_EXPR)
3496 tree min, max;
3497 int cmp;
3499 /* Pass through vr0 in the easy cases. */
3500 if (TYPE_UNSIGNED (type)
3501 || value_range_nonnegative_p (&vr0))
3503 copy_value_range (vr, &vr0);
3504 return;
3507 /* For the remaining varying or symbolic ranges we can't do anything
3508 useful. */
3509 if (vr0.type == VR_VARYING
3510 || symbolic_range_p (&vr0))
3512 set_value_range_to_varying (vr);
3513 return;
3516 /* -TYPE_MIN_VALUE = TYPE_MIN_VALUE with flag_wrapv so we can't get a
3517 useful range. */
3518 if (!TYPE_OVERFLOW_UNDEFINED (type)
3519 && ((vr0.type == VR_RANGE
3520 && vrp_val_is_min (vr0.min))
3521 || (vr0.type == VR_ANTI_RANGE
3522 && !vrp_val_is_min (vr0.min))))
3524 set_value_range_to_varying (vr);
3525 return;
3528 /* ABS_EXPR may flip the range around, if the original range
3529 included negative values. */
3530 if (is_overflow_infinity (vr0.min))
3531 min = positive_overflow_infinity (type);
3532 else if (!vrp_val_is_min (vr0.min))
3533 min = fold_unary_to_constant (code, type, vr0.min);
3534 else if (!needs_overflow_infinity (type))
3535 min = TYPE_MAX_VALUE (type);
3536 else if (supports_overflow_infinity (type))
3537 min = positive_overflow_infinity (type);
3538 else
3540 set_value_range_to_varying (vr);
3541 return;
3544 if (is_overflow_infinity (vr0.max))
3545 max = positive_overflow_infinity (type);
3546 else if (!vrp_val_is_min (vr0.max))
3547 max = fold_unary_to_constant (code, type, vr0.max);
3548 else if (!needs_overflow_infinity (type))
3549 max = TYPE_MAX_VALUE (type);
3550 else if (supports_overflow_infinity (type)
3551 /* We shouldn't generate [+INF, +INF] as set_value_range
3552 doesn't like this and ICEs. */
3553 && !is_positive_overflow_infinity (min))
3554 max = positive_overflow_infinity (type);
3555 else
3557 set_value_range_to_varying (vr);
3558 return;
3561 cmp = compare_values (min, max);
3563 /* If a VR_ANTI_RANGEs contains zero, then we have
3564 ~[-INF, min(MIN, MAX)]. */
3565 if (vr0.type == VR_ANTI_RANGE)
3567 if (range_includes_zero_p (vr0.min, vr0.max) == 1)
3569 /* Take the lower of the two values. */
3570 if (cmp != 1)
3571 max = min;
3573 /* Create ~[-INF, min (abs(MIN), abs(MAX))]
3574 or ~[-INF + 1, min (abs(MIN), abs(MAX))] when
3575 flag_wrapv is set and the original anti-range doesn't include
3576 TYPE_MIN_VALUE, remember -TYPE_MIN_VALUE = TYPE_MIN_VALUE. */
3577 if (TYPE_OVERFLOW_WRAPS (type))
3579 tree type_min_value = TYPE_MIN_VALUE (type);
3581 min = (vr0.min != type_min_value
3582 ? int_const_binop (PLUS_EXPR, type_min_value,
3583 build_int_cst (TREE_TYPE (type_min_value), 1))
3584 : type_min_value);
3586 else
3588 if (overflow_infinity_range_p (&vr0))
3589 min = negative_overflow_infinity (type);
3590 else
3591 min = TYPE_MIN_VALUE (type);
3594 else
3596 /* All else has failed, so create the range [0, INF], even for
3597 flag_wrapv since TYPE_MIN_VALUE is in the original
3598 anti-range. */
3599 vr0.type = VR_RANGE;
3600 min = build_int_cst (type, 0);
3601 if (needs_overflow_infinity (type))
3603 if (supports_overflow_infinity (type))
3604 max = positive_overflow_infinity (type);
3605 else
3607 set_value_range_to_varying (vr);
3608 return;
3611 else
3612 max = TYPE_MAX_VALUE (type);
3616 /* If the range contains zero then we know that the minimum value in the
3617 range will be zero. */
3618 else if (range_includes_zero_p (vr0.min, vr0.max) == 1)
3620 if (cmp == 1)
3621 max = min;
3622 min = build_int_cst (type, 0);
3624 else
3626 /* If the range was reversed, swap MIN and MAX. */
3627 if (cmp == 1)
3628 std::swap (min, max);
3631 cmp = compare_values (min, max);
3632 if (cmp == -2 || cmp == 1)
3634 /* If the new range has its limits swapped around (MIN > MAX),
3635 then the operation caused one of them to wrap around, mark
3636 the new range VARYING. */
3637 set_value_range_to_varying (vr);
3639 else
3640 set_value_range (vr, vr0.type, min, max, NULL);
3641 return;
3644 /* For unhandled operations fall back to varying. */
3645 set_value_range_to_varying (vr);
3646 return;
3650 /* Extract range information from a unary expression CODE OP0 based on
3651 the range of its operand with resulting type TYPE.
3652 The resulting range is stored in *VR. */
3654 static void
3655 extract_range_from_unary_expr (value_range *vr, enum tree_code code,
3656 tree type, tree op0)
3658 value_range vr0 = VR_INITIALIZER;
3660 /* Get value ranges for the operand. For constant operands, create
3661 a new value range with the operand to simplify processing. */
3662 if (TREE_CODE (op0) == SSA_NAME)
3663 vr0 = *(get_value_range (op0));
3664 else if (is_gimple_min_invariant (op0))
3665 set_value_range_to_value (&vr0, op0, NULL);
3666 else
3667 set_value_range_to_varying (&vr0);
3669 extract_range_from_unary_expr (vr, code, type, &vr0, TREE_TYPE (op0));
3673 /* Extract range information from a conditional expression STMT based on
3674 the ranges of each of its operands and the expression code. */
3676 static void
3677 extract_range_from_cond_expr (value_range *vr, gassign *stmt)
3679 tree op0, op1;
3680 value_range vr0 = VR_INITIALIZER;
3681 value_range vr1 = VR_INITIALIZER;
3683 /* Get value ranges for each operand. For constant operands, create
3684 a new value range with the operand to simplify processing. */
3685 op0 = gimple_assign_rhs2 (stmt);
3686 if (TREE_CODE (op0) == SSA_NAME)
3687 vr0 = *(get_value_range (op0));
3688 else if (is_gimple_min_invariant (op0))
3689 set_value_range_to_value (&vr0, op0, NULL);
3690 else
3691 set_value_range_to_varying (&vr0);
3693 op1 = gimple_assign_rhs3 (stmt);
3694 if (TREE_CODE (op1) == SSA_NAME)
3695 vr1 = *(get_value_range (op1));
3696 else if (is_gimple_min_invariant (op1))
3697 set_value_range_to_value (&vr1, op1, NULL);
3698 else
3699 set_value_range_to_varying (&vr1);
3701 /* The resulting value range is the union of the operand ranges */
3702 copy_value_range (vr, &vr0);
3703 vrp_meet (vr, &vr1);
3707 /* Extract range information from a comparison expression EXPR based
3708 on the range of its operand and the expression code. */
3710 static void
3711 extract_range_from_comparison (value_range *vr, enum tree_code code,
3712 tree type, tree op0, tree op1)
3714 bool sop = false;
3715 tree val;
3717 val = vrp_evaluate_conditional_warnv_with_ops (code, op0, op1, false, &sop,
3718 NULL);
3720 /* A disadvantage of using a special infinity as an overflow
3721 representation is that we lose the ability to record overflow
3722 when we don't have an infinity. So we have to ignore a result
3723 which relies on overflow. */
3725 if (val && !is_overflow_infinity (val) && !sop)
3727 /* Since this expression was found on the RHS of an assignment,
3728 its type may be different from _Bool. Convert VAL to EXPR's
3729 type. */
3730 val = fold_convert (type, val);
3731 if (is_gimple_min_invariant (val))
3732 set_value_range_to_value (vr, val, vr->equiv);
3733 else
3734 set_value_range (vr, VR_RANGE, val, val, vr->equiv);
3736 else
3737 /* The result of a comparison is always true or false. */
3738 set_value_range_to_truthvalue (vr, type);
3741 /* Helper function for simplify_internal_call_using_ranges and
3742 extract_range_basic. Return true if OP0 SUBCODE OP1 for
3743 SUBCODE {PLUS,MINUS,MULT}_EXPR is known to never overflow or
3744 always overflow. Set *OVF to true if it is known to always
3745 overflow. */
3747 static bool
3748 check_for_binary_op_overflow (enum tree_code subcode, tree type,
3749 tree op0, tree op1, bool *ovf)
3751 value_range vr0 = VR_INITIALIZER;
3752 value_range vr1 = VR_INITIALIZER;
3753 if (TREE_CODE (op0) == SSA_NAME)
3754 vr0 = *get_value_range (op0);
3755 else if (TREE_CODE (op0) == INTEGER_CST)
3756 set_value_range_to_value (&vr0, op0, NULL);
3757 else
3758 set_value_range_to_varying (&vr0);
3760 if (TREE_CODE (op1) == SSA_NAME)
3761 vr1 = *get_value_range (op1);
3762 else if (TREE_CODE (op1) == INTEGER_CST)
3763 set_value_range_to_value (&vr1, op1, NULL);
3764 else
3765 set_value_range_to_varying (&vr1);
3767 if (!range_int_cst_p (&vr0)
3768 || TREE_OVERFLOW (vr0.min)
3769 || TREE_OVERFLOW (vr0.max))
3771 vr0.min = vrp_val_min (TREE_TYPE (op0));
3772 vr0.max = vrp_val_max (TREE_TYPE (op0));
3774 if (!range_int_cst_p (&vr1)
3775 || TREE_OVERFLOW (vr1.min)
3776 || TREE_OVERFLOW (vr1.max))
3778 vr1.min = vrp_val_min (TREE_TYPE (op1));
3779 vr1.max = vrp_val_max (TREE_TYPE (op1));
3781 *ovf = arith_overflowed_p (subcode, type, vr0.min,
3782 subcode == MINUS_EXPR ? vr1.max : vr1.min);
3783 if (arith_overflowed_p (subcode, type, vr0.max,
3784 subcode == MINUS_EXPR ? vr1.min : vr1.max) != *ovf)
3785 return false;
3786 if (subcode == MULT_EXPR)
3788 if (arith_overflowed_p (subcode, type, vr0.min, vr1.max) != *ovf
3789 || arith_overflowed_p (subcode, type, vr0.max, vr1.min) != *ovf)
3790 return false;
3792 if (*ovf)
3794 /* So far we found that there is an overflow on the boundaries.
3795 That doesn't prove that there is an overflow even for all values
3796 in between the boundaries. For that compute widest_int range
3797 of the result and see if it doesn't overlap the range of
3798 type. */
3799 widest_int wmin, wmax;
3800 widest_int w[4];
3801 int i;
3802 w[0] = wi::to_widest (vr0.min);
3803 w[1] = wi::to_widest (vr0.max);
3804 w[2] = wi::to_widest (vr1.min);
3805 w[3] = wi::to_widest (vr1.max);
3806 for (i = 0; i < 4; i++)
3808 widest_int wt;
3809 switch (subcode)
3811 case PLUS_EXPR:
3812 wt = wi::add (w[i & 1], w[2 + (i & 2) / 2]);
3813 break;
3814 case MINUS_EXPR:
3815 wt = wi::sub (w[i & 1], w[2 + (i & 2) / 2]);
3816 break;
3817 case MULT_EXPR:
3818 wt = wi::mul (w[i & 1], w[2 + (i & 2) / 2]);
3819 break;
3820 default:
3821 gcc_unreachable ();
3823 if (i == 0)
3825 wmin = wt;
3826 wmax = wt;
3828 else
3830 wmin = wi::smin (wmin, wt);
3831 wmax = wi::smax (wmax, wt);
3834 /* The result of op0 CODE op1 is known to be in range
3835 [wmin, wmax]. */
3836 widest_int wtmin = wi::to_widest (vrp_val_min (type));
3837 widest_int wtmax = wi::to_widest (vrp_val_max (type));
3838 /* If all values in [wmin, wmax] are smaller than
3839 [wtmin, wtmax] or all are larger than [wtmin, wtmax],
3840 the arithmetic operation will always overflow. */
3841 if (wmax < wtmin || wmin > wtmax)
3842 return true;
3843 return false;
3845 return true;
3848 /* Try to derive a nonnegative or nonzero range out of STMT relying
3849 primarily on generic routines in fold in conjunction with range data.
3850 Store the result in *VR */
3852 static void
3853 extract_range_basic (value_range *vr, gimple *stmt)
3855 bool sop = false;
3856 tree type = gimple_expr_type (stmt);
3858 if (is_gimple_call (stmt))
3860 tree arg;
3861 int mini, maxi, zerov = 0, prec;
3862 enum tree_code subcode = ERROR_MARK;
3863 combined_fn cfn = gimple_call_combined_fn (stmt);
3865 switch (cfn)
3867 case CFN_BUILT_IN_CONSTANT_P:
3868 /* If the call is __builtin_constant_p and the argument is a
3869 function parameter resolve it to false. This avoids bogus
3870 array bound warnings.
3871 ??? We could do this as early as inlining is finished. */
3872 arg = gimple_call_arg (stmt, 0);
3873 if (TREE_CODE (arg) == SSA_NAME
3874 && SSA_NAME_IS_DEFAULT_DEF (arg)
3875 && TREE_CODE (SSA_NAME_VAR (arg)) == PARM_DECL
3876 && cfun->after_inlining)
3878 set_value_range_to_null (vr, type);
3879 return;
3881 break;
3882 /* Both __builtin_ffs* and __builtin_popcount return
3883 [0, prec]. */
3884 CASE_CFN_FFS:
3885 CASE_CFN_POPCOUNT:
3886 arg = gimple_call_arg (stmt, 0);
3887 prec = TYPE_PRECISION (TREE_TYPE (arg));
3888 mini = 0;
3889 maxi = prec;
3890 if (TREE_CODE (arg) == SSA_NAME)
3892 value_range *vr0 = get_value_range (arg);
3893 /* If arg is non-zero, then ffs or popcount
3894 are non-zero. */
3895 if (((vr0->type == VR_RANGE
3896 && range_includes_zero_p (vr0->min, vr0->max) == 0)
3897 || (vr0->type == VR_ANTI_RANGE
3898 && range_includes_zero_p (vr0->min, vr0->max) == 1))
3899 && !is_overflow_infinity (vr0->min)
3900 && !is_overflow_infinity (vr0->max))
3901 mini = 1;
3902 /* If some high bits are known to be zero,
3903 we can decrease the maximum. */
3904 if (vr0->type == VR_RANGE
3905 && TREE_CODE (vr0->max) == INTEGER_CST
3906 && !operand_less_p (vr0->min,
3907 build_zero_cst (TREE_TYPE (vr0->min)))
3908 && !is_overflow_infinity (vr0->max))
3909 maxi = tree_floor_log2 (vr0->max) + 1;
3911 goto bitop_builtin;
3912 /* __builtin_parity* returns [0, 1]. */
3913 CASE_CFN_PARITY:
3914 mini = 0;
3915 maxi = 1;
3916 goto bitop_builtin;
3917 /* __builtin_c[lt]z* return [0, prec-1], except for
3918 when the argument is 0, but that is undefined behavior.
3919 On many targets where the CLZ RTL or optab value is defined
3920 for 0 the value is prec, so include that in the range
3921 by default. */
3922 CASE_CFN_CLZ:
3923 arg = gimple_call_arg (stmt, 0);
3924 prec = TYPE_PRECISION (TREE_TYPE (arg));
3925 mini = 0;
3926 maxi = prec;
3927 if (optab_handler (clz_optab, TYPE_MODE (TREE_TYPE (arg)))
3928 != CODE_FOR_nothing
3929 && CLZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (TREE_TYPE (arg)),
3930 zerov)
3931 /* Handle only the single common value. */
3932 && zerov != prec)
3933 /* Magic value to give up, unless vr0 proves
3934 arg is non-zero. */
3935 mini = -2;
3936 if (TREE_CODE (arg) == SSA_NAME)
3938 value_range *vr0 = get_value_range (arg);
3939 /* From clz of VR_RANGE minimum we can compute
3940 result maximum. */
3941 if (vr0->type == VR_RANGE
3942 && TREE_CODE (vr0->min) == INTEGER_CST
3943 && !is_overflow_infinity (vr0->min))
3945 maxi = prec - 1 - tree_floor_log2 (vr0->min);
3946 if (maxi != prec)
3947 mini = 0;
3949 else if (vr0->type == VR_ANTI_RANGE
3950 && integer_zerop (vr0->min)
3951 && !is_overflow_infinity (vr0->min))
3953 maxi = prec - 1;
3954 mini = 0;
3956 if (mini == -2)
3957 break;
3958 /* From clz of VR_RANGE maximum we can compute
3959 result minimum. */
3960 if (vr0->type == VR_RANGE
3961 && TREE_CODE (vr0->max) == INTEGER_CST
3962 && !is_overflow_infinity (vr0->max))
3964 mini = prec - 1 - tree_floor_log2 (vr0->max);
3965 if (mini == prec)
3966 break;
3969 if (mini == -2)
3970 break;
3971 goto bitop_builtin;
3972 /* __builtin_ctz* return [0, prec-1], except for
3973 when the argument is 0, but that is undefined behavior.
3974 If there is a ctz optab for this mode and
3975 CTZ_DEFINED_VALUE_AT_ZERO, include that in the range,
3976 otherwise just assume 0 won't be seen. */
3977 CASE_CFN_CTZ:
3978 arg = gimple_call_arg (stmt, 0);
3979 prec = TYPE_PRECISION (TREE_TYPE (arg));
3980 mini = 0;
3981 maxi = prec - 1;
3982 if (optab_handler (ctz_optab, TYPE_MODE (TREE_TYPE (arg)))
3983 != CODE_FOR_nothing
3984 && CTZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (TREE_TYPE (arg)),
3985 zerov))
3987 /* Handle only the two common values. */
3988 if (zerov == -1)
3989 mini = -1;
3990 else if (zerov == prec)
3991 maxi = prec;
3992 else
3993 /* Magic value to give up, unless vr0 proves
3994 arg is non-zero. */
3995 mini = -2;
3997 if (TREE_CODE (arg) == SSA_NAME)
3999 value_range *vr0 = get_value_range (arg);
4000 /* If arg is non-zero, then use [0, prec - 1]. */
4001 if (((vr0->type == VR_RANGE
4002 && integer_nonzerop (vr0->min))
4003 || (vr0->type == VR_ANTI_RANGE
4004 && integer_zerop (vr0->min)))
4005 && !is_overflow_infinity (vr0->min))
4007 mini = 0;
4008 maxi = prec - 1;
4010 /* If some high bits are known to be zero,
4011 we can decrease the result maximum. */
4012 if (vr0->type == VR_RANGE
4013 && TREE_CODE (vr0->max) == INTEGER_CST
4014 && !is_overflow_infinity (vr0->max))
4016 maxi = tree_floor_log2 (vr0->max);
4017 /* For vr0 [0, 0] give up. */
4018 if (maxi == -1)
4019 break;
4022 if (mini == -2)
4023 break;
4024 goto bitop_builtin;
4025 /* __builtin_clrsb* returns [0, prec-1]. */
4026 CASE_CFN_CLRSB:
4027 arg = gimple_call_arg (stmt, 0);
4028 prec = TYPE_PRECISION (TREE_TYPE (arg));
4029 mini = 0;
4030 maxi = prec - 1;
4031 goto bitop_builtin;
4032 bitop_builtin:
4033 set_value_range (vr, VR_RANGE, build_int_cst (type, mini),
4034 build_int_cst (type, maxi), NULL);
4035 return;
4036 case CFN_UBSAN_CHECK_ADD:
4037 subcode = PLUS_EXPR;
4038 break;
4039 case CFN_UBSAN_CHECK_SUB:
4040 subcode = MINUS_EXPR;
4041 break;
4042 case CFN_UBSAN_CHECK_MUL:
4043 subcode = MULT_EXPR;
4044 break;
4045 case CFN_GOACC_DIM_SIZE:
4046 case CFN_GOACC_DIM_POS:
4047 /* Optimizing these two internal functions helps the loop
4048 optimizer eliminate outer comparisons. Size is [1,N]
4049 and pos is [0,N-1]. */
4051 bool is_pos = cfn == CFN_GOACC_DIM_POS;
4052 int axis = oacc_get_ifn_dim_arg (stmt);
4053 int size = oacc_get_fn_dim_size (current_function_decl, axis);
4055 if (!size)
4056 /* If it's dynamic, the backend might know a hardware
4057 limitation. */
4058 size = targetm.goacc.dim_limit (axis);
4060 tree type = TREE_TYPE (gimple_call_lhs (stmt));
4061 set_value_range (vr, VR_RANGE,
4062 build_int_cst (type, is_pos ? 0 : 1),
4063 size ? build_int_cst (type, size - is_pos)
4064 : vrp_val_max (type), NULL);
4066 return;
4067 case CFN_BUILT_IN_STRLEN:
4068 if (tree lhs = gimple_call_lhs (stmt))
4069 if (ptrdiff_type_node
4070 && (TYPE_PRECISION (ptrdiff_type_node)
4071 == TYPE_PRECISION (TREE_TYPE (lhs))))
4073 tree type = TREE_TYPE (lhs);
4074 tree max = vrp_val_max (ptrdiff_type_node);
4075 wide_int wmax = wi::to_wide (max, TYPE_PRECISION (TREE_TYPE (max)));
4076 tree range_min = build_zero_cst (type);
4077 tree range_max = wide_int_to_tree (type, wmax - 1);
4078 set_value_range (vr, VR_RANGE, range_min, range_max, NULL);
4079 return;
4081 break;
4082 default:
4083 break;
4085 if (subcode != ERROR_MARK)
4087 bool saved_flag_wrapv = flag_wrapv;
4088 /* Pretend the arithmetics is wrapping. If there is
4089 any overflow, we'll complain, but will actually do
4090 wrapping operation. */
4091 flag_wrapv = 1;
4092 extract_range_from_binary_expr (vr, subcode, type,
4093 gimple_call_arg (stmt, 0),
4094 gimple_call_arg (stmt, 1));
4095 flag_wrapv = saved_flag_wrapv;
4097 /* If for both arguments vrp_valueize returned non-NULL,
4098 this should have been already folded and if not, it
4099 wasn't folded because of overflow. Avoid removing the
4100 UBSAN_CHECK_* calls in that case. */
4101 if (vr->type == VR_RANGE
4102 && (vr->min == vr->max
4103 || operand_equal_p (vr->min, vr->max, 0)))
4104 set_value_range_to_varying (vr);
4105 return;
4108 /* Handle extraction of the two results (result of arithmetics and
4109 a flag whether arithmetics overflowed) from {ADD,SUB,MUL}_OVERFLOW
4110 internal function. Similarly from ATOMIC_COMPARE_EXCHANGE. */
4111 else if (is_gimple_assign (stmt)
4112 && (gimple_assign_rhs_code (stmt) == REALPART_EXPR
4113 || gimple_assign_rhs_code (stmt) == IMAGPART_EXPR)
4114 && INTEGRAL_TYPE_P (type))
4116 enum tree_code code = gimple_assign_rhs_code (stmt);
4117 tree op = gimple_assign_rhs1 (stmt);
4118 if (TREE_CODE (op) == code && TREE_CODE (TREE_OPERAND (op, 0)) == SSA_NAME)
4120 gimple *g = SSA_NAME_DEF_STMT (TREE_OPERAND (op, 0));
4121 if (is_gimple_call (g) && gimple_call_internal_p (g))
4123 enum tree_code subcode = ERROR_MARK;
4124 switch (gimple_call_internal_fn (g))
4126 case IFN_ADD_OVERFLOW:
4127 subcode = PLUS_EXPR;
4128 break;
4129 case IFN_SUB_OVERFLOW:
4130 subcode = MINUS_EXPR;
4131 break;
4132 case IFN_MUL_OVERFLOW:
4133 subcode = MULT_EXPR;
4134 break;
4135 case IFN_ATOMIC_COMPARE_EXCHANGE:
4136 if (code == IMAGPART_EXPR)
4138 /* This is the boolean return value whether compare and
4139 exchange changed anything or not. */
4140 set_value_range (vr, VR_RANGE, build_int_cst (type, 0),
4141 build_int_cst (type, 1), NULL);
4142 return;
4144 break;
4145 default:
4146 break;
4148 if (subcode != ERROR_MARK)
4150 tree op0 = gimple_call_arg (g, 0);
4151 tree op1 = gimple_call_arg (g, 1);
4152 if (code == IMAGPART_EXPR)
4154 bool ovf = false;
4155 if (check_for_binary_op_overflow (subcode, type,
4156 op0, op1, &ovf))
4157 set_value_range_to_value (vr,
4158 build_int_cst (type, ovf),
4159 NULL);
4160 else if (TYPE_PRECISION (type) == 1
4161 && !TYPE_UNSIGNED (type))
4162 set_value_range_to_varying (vr);
4163 else
4164 set_value_range (vr, VR_RANGE, build_int_cst (type, 0),
4165 build_int_cst (type, 1), NULL);
4167 else if (types_compatible_p (type, TREE_TYPE (op0))
4168 && types_compatible_p (type, TREE_TYPE (op1)))
4170 bool saved_flag_wrapv = flag_wrapv;
4171 /* Pretend the arithmetics is wrapping. If there is
4172 any overflow, IMAGPART_EXPR will be set. */
4173 flag_wrapv = 1;
4174 extract_range_from_binary_expr (vr, subcode, type,
4175 op0, op1);
4176 flag_wrapv = saved_flag_wrapv;
4178 else
4180 value_range vr0 = VR_INITIALIZER;
4181 value_range vr1 = VR_INITIALIZER;
4182 bool saved_flag_wrapv = flag_wrapv;
4183 /* Pretend the arithmetics is wrapping. If there is
4184 any overflow, IMAGPART_EXPR will be set. */
4185 flag_wrapv = 1;
4186 extract_range_from_unary_expr (&vr0, NOP_EXPR,
4187 type, op0);
4188 extract_range_from_unary_expr (&vr1, NOP_EXPR,
4189 type, op1);
4190 extract_range_from_binary_expr_1 (vr, subcode, type,
4191 &vr0, &vr1);
4192 flag_wrapv = saved_flag_wrapv;
4194 return;
4199 if (INTEGRAL_TYPE_P (type)
4200 && gimple_stmt_nonnegative_warnv_p (stmt, &sop))
4201 set_value_range_to_nonnegative (vr, type,
4202 sop || stmt_overflow_infinity (stmt));
4203 else if (vrp_stmt_computes_nonzero (stmt, &sop)
4204 && !sop)
4205 set_value_range_to_nonnull (vr, type);
4206 else
4207 set_value_range_to_varying (vr);
4211 /* Try to compute a useful range out of assignment STMT and store it
4212 in *VR. */
4214 static void
4215 extract_range_from_assignment (value_range *vr, gassign *stmt)
4217 enum tree_code code = gimple_assign_rhs_code (stmt);
4219 if (code == ASSERT_EXPR)
4220 extract_range_from_assert (vr, gimple_assign_rhs1 (stmt));
4221 else if (code == SSA_NAME)
4222 extract_range_from_ssa_name (vr, gimple_assign_rhs1 (stmt));
4223 else if (TREE_CODE_CLASS (code) == tcc_binary)
4224 extract_range_from_binary_expr (vr, gimple_assign_rhs_code (stmt),
4225 gimple_expr_type (stmt),
4226 gimple_assign_rhs1 (stmt),
4227 gimple_assign_rhs2 (stmt));
4228 else if (TREE_CODE_CLASS (code) == tcc_unary)
4229 extract_range_from_unary_expr (vr, gimple_assign_rhs_code (stmt),
4230 gimple_expr_type (stmt),
4231 gimple_assign_rhs1 (stmt));
4232 else if (code == COND_EXPR)
4233 extract_range_from_cond_expr (vr, stmt);
4234 else if (TREE_CODE_CLASS (code) == tcc_comparison)
4235 extract_range_from_comparison (vr, gimple_assign_rhs_code (stmt),
4236 gimple_expr_type (stmt),
4237 gimple_assign_rhs1 (stmt),
4238 gimple_assign_rhs2 (stmt));
4239 else if (get_gimple_rhs_class (code) == GIMPLE_SINGLE_RHS
4240 && is_gimple_min_invariant (gimple_assign_rhs1 (stmt)))
4241 set_value_range_to_value (vr, gimple_assign_rhs1 (stmt), NULL);
4242 else
4243 set_value_range_to_varying (vr);
4245 if (vr->type == VR_VARYING)
4246 extract_range_basic (vr, stmt);
4249 /* Given a range VR, a LOOP and a variable VAR, determine whether it
4250 would be profitable to adjust VR using scalar evolution information
4251 for VAR. If so, update VR with the new limits. */
4253 static void
4254 adjust_range_with_scev (value_range *vr, struct loop *loop,
4255 gimple *stmt, tree var)
4257 tree init, step, chrec, tmin, tmax, min, max, type, tem;
4258 enum ev_direction dir;
4260 /* TODO. Don't adjust anti-ranges. An anti-range may provide
4261 better opportunities than a regular range, but I'm not sure. */
4262 if (vr->type == VR_ANTI_RANGE)
4263 return;
4265 chrec = instantiate_parameters (loop, analyze_scalar_evolution (loop, var));
4267 /* Like in PR19590, scev can return a constant function. */
4268 if (is_gimple_min_invariant (chrec))
4270 set_value_range_to_value (vr, chrec, vr->equiv);
4271 return;
4274 if (TREE_CODE (chrec) != POLYNOMIAL_CHREC)
4275 return;
4277 init = initial_condition_in_loop_num (chrec, loop->num);
4278 tem = op_with_constant_singleton_value_range (init);
4279 if (tem)
4280 init = tem;
4281 step = evolution_part_in_loop_num (chrec, loop->num);
4282 tem = op_with_constant_singleton_value_range (step);
4283 if (tem)
4284 step = tem;
4286 /* If STEP is symbolic, we can't know whether INIT will be the
4287 minimum or maximum value in the range. Also, unless INIT is
4288 a simple expression, compare_values and possibly other functions
4289 in tree-vrp won't be able to handle it. */
4290 if (step == NULL_TREE
4291 || !is_gimple_min_invariant (step)
4292 || !valid_value_p (init))
4293 return;
4295 dir = scev_direction (chrec);
4296 if (/* Do not adjust ranges if we do not know whether the iv increases
4297 or decreases, ... */
4298 dir == EV_DIR_UNKNOWN
4299 /* ... or if it may wrap. */
4300 || scev_probably_wraps_p (NULL_TREE, init, step, stmt,
4301 get_chrec_loop (chrec), true))
4302 return;
4304 /* We use TYPE_MIN_VALUE and TYPE_MAX_VALUE here instead of
4305 negative_overflow_infinity and positive_overflow_infinity,
4306 because we have concluded that the loop probably does not
4307 wrap. */
4309 type = TREE_TYPE (var);
4310 if (POINTER_TYPE_P (type) || !TYPE_MIN_VALUE (type))
4311 tmin = lower_bound_in_type (type, type);
4312 else
4313 tmin = TYPE_MIN_VALUE (type);
4314 if (POINTER_TYPE_P (type) || !TYPE_MAX_VALUE (type))
4315 tmax = upper_bound_in_type (type, type);
4316 else
4317 tmax = TYPE_MAX_VALUE (type);
4319 /* Try to use estimated number of iterations for the loop to constrain the
4320 final value in the evolution. */
4321 if (TREE_CODE (step) == INTEGER_CST
4322 && is_gimple_val (init)
4323 && (TREE_CODE (init) != SSA_NAME
4324 || get_value_range (init)->type == VR_RANGE))
4326 widest_int nit;
4328 /* We are only entering here for loop header PHI nodes, so using
4329 the number of latch executions is the correct thing to use. */
4330 if (max_loop_iterations (loop, &nit))
4332 value_range maxvr = VR_INITIALIZER;
4333 signop sgn = TYPE_SIGN (TREE_TYPE (step));
4334 bool overflow;
4336 widest_int wtmp = wi::mul (wi::to_widest (step), nit, sgn,
4337 &overflow);
4338 /* If the multiplication overflowed we can't do a meaningful
4339 adjustment. Likewise if the result doesn't fit in the type
4340 of the induction variable. For a signed type we have to
4341 check whether the result has the expected signedness which
4342 is that of the step as number of iterations is unsigned. */
4343 if (!overflow
4344 && wi::fits_to_tree_p (wtmp, TREE_TYPE (init))
4345 && (sgn == UNSIGNED
4346 || wi::gts_p (wtmp, 0) == wi::gts_p (step, 0)))
4348 tem = wide_int_to_tree (TREE_TYPE (init), wtmp);
4349 extract_range_from_binary_expr (&maxvr, PLUS_EXPR,
4350 TREE_TYPE (init), init, tem);
4351 /* Likewise if the addition did. */
4352 if (maxvr.type == VR_RANGE)
4354 value_range initvr = VR_INITIALIZER;
4356 if (TREE_CODE (init) == SSA_NAME)
4357 initvr = *(get_value_range (init));
4358 else if (is_gimple_min_invariant (init))
4359 set_value_range_to_value (&initvr, init, NULL);
4360 else
4361 return;
4363 /* Check if init + nit * step overflows. Though we checked
4364 scev {init, step}_loop doesn't wrap, it is not enough
4365 because the loop may exit immediately. Overflow could
4366 happen in the plus expression in this case. */
4367 if ((dir == EV_DIR_DECREASES
4368 && (is_negative_overflow_infinity (maxvr.min)
4369 || compare_values (maxvr.min, initvr.min) != -1))
4370 || (dir == EV_DIR_GROWS
4371 && (is_positive_overflow_infinity (maxvr.max)
4372 || compare_values (maxvr.max, initvr.max) != 1)))
4373 return;
4375 tmin = maxvr.min;
4376 tmax = maxvr.max;
4382 if (vr->type == VR_VARYING || vr->type == VR_UNDEFINED)
4384 min = tmin;
4385 max = tmax;
4387 /* For VARYING or UNDEFINED ranges, just about anything we get
4388 from scalar evolutions should be better. */
4390 if (dir == EV_DIR_DECREASES)
4391 max = init;
4392 else
4393 min = init;
4395 else if (vr->type == VR_RANGE)
4397 min = vr->min;
4398 max = vr->max;
4400 if (dir == EV_DIR_DECREASES)
4402 /* INIT is the maximum value. If INIT is lower than VR->MAX
4403 but no smaller than VR->MIN, set VR->MAX to INIT. */
4404 if (compare_values (init, max) == -1)
4405 max = init;
4407 /* According to the loop information, the variable does not
4408 overflow. If we think it does, probably because of an
4409 overflow due to arithmetic on a different INF value,
4410 reset now. */
4411 if (is_negative_overflow_infinity (min)
4412 || compare_values (min, tmin) == -1)
4413 min = tmin;
4416 else
4418 /* If INIT is bigger than VR->MIN, set VR->MIN to INIT. */
4419 if (compare_values (init, min) == 1)
4420 min = init;
4422 if (is_positive_overflow_infinity (max)
4423 || compare_values (tmax, max) == -1)
4424 max = tmax;
4427 else
4428 return;
4430 /* If we just created an invalid range with the minimum
4431 greater than the maximum, we fail conservatively.
4432 This should happen only in unreachable
4433 parts of code, or for invalid programs. */
4434 if (compare_values (min, max) == 1
4435 || (is_negative_overflow_infinity (min)
4436 && is_positive_overflow_infinity (max)))
4437 return;
4439 /* Even for valid range info, sometimes overflow flag will leak in.
4440 As GIMPLE IL should have no constants with TREE_OVERFLOW set, we
4441 drop them except for +-overflow_infinity which still need special
4442 handling in vrp pass. */
4443 if (TREE_OVERFLOW_P (min)
4444 && ! is_negative_overflow_infinity (min))
4445 min = drop_tree_overflow (min);
4446 if (TREE_OVERFLOW_P (max)
4447 && ! is_positive_overflow_infinity (max))
4448 max = drop_tree_overflow (max);
4450 set_value_range (vr, VR_RANGE, min, max, vr->equiv);
4454 /* Given two numeric value ranges VR0, VR1 and a comparison code COMP:
4456 - Return BOOLEAN_TRUE_NODE if VR0 COMP VR1 always returns true for
4457 all the values in the ranges.
4459 - Return BOOLEAN_FALSE_NODE if the comparison always returns false.
4461 - Return NULL_TREE if it is not always possible to determine the
4462 value of the comparison.
4464 Also set *STRICT_OVERFLOW_P to indicate whether a range with an
4465 overflow infinity was used in the test. */
4468 static tree
4469 compare_ranges (enum tree_code comp, value_range *vr0, value_range *vr1,
4470 bool *strict_overflow_p)
4472 /* VARYING or UNDEFINED ranges cannot be compared. */
4473 if (vr0->type == VR_VARYING
4474 || vr0->type == VR_UNDEFINED
4475 || vr1->type == VR_VARYING
4476 || vr1->type == VR_UNDEFINED)
4477 return NULL_TREE;
4479 /* Anti-ranges need to be handled separately. */
4480 if (vr0->type == VR_ANTI_RANGE || vr1->type == VR_ANTI_RANGE)
4482 /* If both are anti-ranges, then we cannot compute any
4483 comparison. */
4484 if (vr0->type == VR_ANTI_RANGE && vr1->type == VR_ANTI_RANGE)
4485 return NULL_TREE;
4487 /* These comparisons are never statically computable. */
4488 if (comp == GT_EXPR
4489 || comp == GE_EXPR
4490 || comp == LT_EXPR
4491 || comp == LE_EXPR)
4492 return NULL_TREE;
4494 /* Equality can be computed only between a range and an
4495 anti-range. ~[VAL1, VAL2] == [VAL1, VAL2] is always false. */
4496 if (vr0->type == VR_RANGE)
4498 /* To simplify processing, make VR0 the anti-range. */
4499 value_range *tmp = vr0;
4500 vr0 = vr1;
4501 vr1 = tmp;
4504 gcc_assert (comp == NE_EXPR || comp == EQ_EXPR);
4506 if (compare_values_warnv (vr0->min, vr1->min, strict_overflow_p) == 0
4507 && compare_values_warnv (vr0->max, vr1->max, strict_overflow_p) == 0)
4508 return (comp == NE_EXPR) ? boolean_true_node : boolean_false_node;
4510 return NULL_TREE;
4513 if (!usable_range_p (vr0, strict_overflow_p)
4514 || !usable_range_p (vr1, strict_overflow_p))
4515 return NULL_TREE;
4517 /* Simplify processing. If COMP is GT_EXPR or GE_EXPR, switch the
4518 operands around and change the comparison code. */
4519 if (comp == GT_EXPR || comp == GE_EXPR)
4521 comp = (comp == GT_EXPR) ? LT_EXPR : LE_EXPR;
4522 std::swap (vr0, vr1);
4525 if (comp == EQ_EXPR)
4527 /* Equality may only be computed if both ranges represent
4528 exactly one value. */
4529 if (compare_values_warnv (vr0->min, vr0->max, strict_overflow_p) == 0
4530 && compare_values_warnv (vr1->min, vr1->max, strict_overflow_p) == 0)
4532 int cmp_min = compare_values_warnv (vr0->min, vr1->min,
4533 strict_overflow_p);
4534 int cmp_max = compare_values_warnv (vr0->max, vr1->max,
4535 strict_overflow_p);
4536 if (cmp_min == 0 && cmp_max == 0)
4537 return boolean_true_node;
4538 else if (cmp_min != -2 && cmp_max != -2)
4539 return boolean_false_node;
4541 /* If [V0_MIN, V1_MAX] < [V1_MIN, V1_MAX] then V0 != V1. */
4542 else if (compare_values_warnv (vr0->min, vr1->max,
4543 strict_overflow_p) == 1
4544 || compare_values_warnv (vr1->min, vr0->max,
4545 strict_overflow_p) == 1)
4546 return boolean_false_node;
4548 return NULL_TREE;
4550 else if (comp == NE_EXPR)
4552 int cmp1, cmp2;
4554 /* If VR0 is completely to the left or completely to the right
4555 of VR1, they are always different. Notice that we need to
4556 make sure that both comparisons yield similar results to
4557 avoid comparing values that cannot be compared at
4558 compile-time. */
4559 cmp1 = compare_values_warnv (vr0->max, vr1->min, strict_overflow_p);
4560 cmp2 = compare_values_warnv (vr0->min, vr1->max, strict_overflow_p);
4561 if ((cmp1 == -1 && cmp2 == -1) || (cmp1 == 1 && cmp2 == 1))
4562 return boolean_true_node;
4564 /* If VR0 and VR1 represent a single value and are identical,
4565 return false. */
4566 else if (compare_values_warnv (vr0->min, vr0->max,
4567 strict_overflow_p) == 0
4568 && compare_values_warnv (vr1->min, vr1->max,
4569 strict_overflow_p) == 0
4570 && compare_values_warnv (vr0->min, vr1->min,
4571 strict_overflow_p) == 0
4572 && compare_values_warnv (vr0->max, vr1->max,
4573 strict_overflow_p) == 0)
4574 return boolean_false_node;
4576 /* Otherwise, they may or may not be different. */
4577 else
4578 return NULL_TREE;
4580 else if (comp == LT_EXPR || comp == LE_EXPR)
4582 int tst;
4584 /* If VR0 is to the left of VR1, return true. */
4585 tst = compare_values_warnv (vr0->max, vr1->min, strict_overflow_p);
4586 if ((comp == LT_EXPR && tst == -1)
4587 || (comp == LE_EXPR && (tst == -1 || tst == 0)))
4589 if (overflow_infinity_range_p (vr0)
4590 || overflow_infinity_range_p (vr1))
4591 *strict_overflow_p = true;
4592 return boolean_true_node;
4595 /* If VR0 is to the right of VR1, return false. */
4596 tst = compare_values_warnv (vr0->min, vr1->max, strict_overflow_p);
4597 if ((comp == LT_EXPR && (tst == 0 || tst == 1))
4598 || (comp == LE_EXPR && tst == 1))
4600 if (overflow_infinity_range_p (vr0)
4601 || overflow_infinity_range_p (vr1))
4602 *strict_overflow_p = true;
4603 return boolean_false_node;
4606 /* Otherwise, we don't know. */
4607 return NULL_TREE;
4610 gcc_unreachable ();
4614 /* Given a value range VR, a value VAL and a comparison code COMP, return
4615 BOOLEAN_TRUE_NODE if VR COMP VAL always returns true for all the
4616 values in VR. Return BOOLEAN_FALSE_NODE if the comparison
4617 always returns false. Return NULL_TREE if it is not always
4618 possible to determine the value of the comparison. Also set
4619 *STRICT_OVERFLOW_P to indicate whether a range with an overflow
4620 infinity was used in the test. */
4622 static tree
4623 compare_range_with_value (enum tree_code comp, value_range *vr, tree val,
4624 bool *strict_overflow_p)
4626 if (vr->type == VR_VARYING || vr->type == VR_UNDEFINED)
4627 return NULL_TREE;
4629 /* Anti-ranges need to be handled separately. */
4630 if (vr->type == VR_ANTI_RANGE)
4632 /* For anti-ranges, the only predicates that we can compute at
4633 compile time are equality and inequality. */
4634 if (comp == GT_EXPR
4635 || comp == GE_EXPR
4636 || comp == LT_EXPR
4637 || comp == LE_EXPR)
4638 return NULL_TREE;
4640 /* ~[VAL_1, VAL_2] OP VAL is known if VAL_1 <= VAL <= VAL_2. */
4641 if (value_inside_range (val, vr->min, vr->max) == 1)
4642 return (comp == NE_EXPR) ? boolean_true_node : boolean_false_node;
4644 return NULL_TREE;
4647 if (!usable_range_p (vr, strict_overflow_p))
4648 return NULL_TREE;
4650 if (comp == EQ_EXPR)
4652 /* EQ_EXPR may only be computed if VR represents exactly
4653 one value. */
4654 if (compare_values_warnv (vr->min, vr->max, strict_overflow_p) == 0)
4656 int cmp = compare_values_warnv (vr->min, val, strict_overflow_p);
4657 if (cmp == 0)
4658 return boolean_true_node;
4659 else if (cmp == -1 || cmp == 1 || cmp == 2)
4660 return boolean_false_node;
4662 else if (compare_values_warnv (val, vr->min, strict_overflow_p) == -1
4663 || compare_values_warnv (vr->max, val, strict_overflow_p) == -1)
4664 return boolean_false_node;
4666 return NULL_TREE;
4668 else if (comp == NE_EXPR)
4670 /* If VAL is not inside VR, then they are always different. */
4671 if (compare_values_warnv (vr->max, val, strict_overflow_p) == -1
4672 || compare_values_warnv (vr->min, val, strict_overflow_p) == 1)
4673 return boolean_true_node;
4675 /* If VR represents exactly one value equal to VAL, then return
4676 false. */
4677 if (compare_values_warnv (vr->min, vr->max, strict_overflow_p) == 0
4678 && compare_values_warnv (vr->min, val, strict_overflow_p) == 0)
4679 return boolean_false_node;
4681 /* Otherwise, they may or may not be different. */
4682 return NULL_TREE;
4684 else if (comp == LT_EXPR || comp == LE_EXPR)
4686 int tst;
4688 /* If VR is to the left of VAL, return true. */
4689 tst = compare_values_warnv (vr->max, val, strict_overflow_p);
4690 if ((comp == LT_EXPR && tst == -1)
4691 || (comp == LE_EXPR && (tst == -1 || tst == 0)))
4693 if (overflow_infinity_range_p (vr))
4694 *strict_overflow_p = true;
4695 return boolean_true_node;
4698 /* If VR is to the right of VAL, return false. */
4699 tst = compare_values_warnv (vr->min, val, strict_overflow_p);
4700 if ((comp == LT_EXPR && (tst == 0 || tst == 1))
4701 || (comp == LE_EXPR && tst == 1))
4703 if (overflow_infinity_range_p (vr))
4704 *strict_overflow_p = true;
4705 return boolean_false_node;
4708 /* Otherwise, we don't know. */
4709 return NULL_TREE;
4711 else if (comp == GT_EXPR || comp == GE_EXPR)
4713 int tst;
4715 /* If VR is to the right of VAL, return true. */
4716 tst = compare_values_warnv (vr->min, val, strict_overflow_p);
4717 if ((comp == GT_EXPR && tst == 1)
4718 || (comp == GE_EXPR && (tst == 0 || tst == 1)))
4720 if (overflow_infinity_range_p (vr))
4721 *strict_overflow_p = true;
4722 return boolean_true_node;
4725 /* If VR is to the left of VAL, return false. */
4726 tst = compare_values_warnv (vr->max, val, strict_overflow_p);
4727 if ((comp == GT_EXPR && (tst == -1 || tst == 0))
4728 || (comp == GE_EXPR && tst == -1))
4730 if (overflow_infinity_range_p (vr))
4731 *strict_overflow_p = true;
4732 return boolean_false_node;
4735 /* Otherwise, we don't know. */
4736 return NULL_TREE;
4739 gcc_unreachable ();
4743 /* Debugging dumps. */
4745 void dump_value_range (FILE *, const value_range *);
4746 void debug_value_range (value_range *);
4747 void dump_all_value_ranges (FILE *);
4748 void debug_all_value_ranges (void);
4749 void dump_vr_equiv (FILE *, bitmap);
4750 void debug_vr_equiv (bitmap);
4753 /* Dump value range VR to FILE. */
4755 void
4756 dump_value_range (FILE *file, const value_range *vr)
4758 if (vr == NULL)
4759 fprintf (file, "[]");
4760 else if (vr->type == VR_UNDEFINED)
4761 fprintf (file, "UNDEFINED");
4762 else if (vr->type == VR_RANGE || vr->type == VR_ANTI_RANGE)
4764 tree type = TREE_TYPE (vr->min);
4766 fprintf (file, "%s[", (vr->type == VR_ANTI_RANGE) ? "~" : "");
4768 if (is_negative_overflow_infinity (vr->min))
4769 fprintf (file, "-INF(OVF)");
4770 else if (INTEGRAL_TYPE_P (type)
4771 && !TYPE_UNSIGNED (type)
4772 && vrp_val_is_min (vr->min))
4773 fprintf (file, "-INF");
4774 else
4775 print_generic_expr (file, vr->min, 0);
4777 fprintf (file, ", ");
4779 if (is_positive_overflow_infinity (vr->max))
4780 fprintf (file, "+INF(OVF)");
4781 else if (INTEGRAL_TYPE_P (type)
4782 && vrp_val_is_max (vr->max))
4783 fprintf (file, "+INF");
4784 else
4785 print_generic_expr (file, vr->max, 0);
4787 fprintf (file, "]");
4789 if (vr->equiv)
4791 bitmap_iterator bi;
4792 unsigned i, c = 0;
4794 fprintf (file, " EQUIVALENCES: { ");
4796 EXECUTE_IF_SET_IN_BITMAP (vr->equiv, 0, i, bi)
4798 print_generic_expr (file, ssa_name (i), 0);
4799 fprintf (file, " ");
4800 c++;
4803 fprintf (file, "} (%u elements)", c);
4806 else if (vr->type == VR_VARYING)
4807 fprintf (file, "VARYING");
4808 else
4809 fprintf (file, "INVALID RANGE");
4813 /* Dump value range VR to stderr. */
4815 DEBUG_FUNCTION void
4816 debug_value_range (value_range *vr)
4818 dump_value_range (stderr, vr);
4819 fprintf (stderr, "\n");
4823 /* Dump value ranges of all SSA_NAMEs to FILE. */
4825 void
4826 dump_all_value_ranges (FILE *file)
4828 size_t i;
4830 for (i = 0; i < num_vr_values; i++)
4832 if (vr_value[i])
4834 print_generic_expr (file, ssa_name (i), 0);
4835 fprintf (file, ": ");
4836 dump_value_range (file, vr_value[i]);
4837 fprintf (file, "\n");
4841 fprintf (file, "\n");
4845 /* Dump all value ranges to stderr. */
4847 DEBUG_FUNCTION void
4848 debug_all_value_ranges (void)
4850 dump_all_value_ranges (stderr);
4854 /* Given a COND_EXPR COND of the form 'V OP W', and an SSA name V,
4855 create a new SSA name N and return the assertion assignment
4856 'N = ASSERT_EXPR <V, V OP W>'. */
4858 static gimple *
4859 build_assert_expr_for (tree cond, tree v)
4861 tree a;
4862 gassign *assertion;
4864 gcc_assert (TREE_CODE (v) == SSA_NAME
4865 && COMPARISON_CLASS_P (cond));
4867 a = build2 (ASSERT_EXPR, TREE_TYPE (v), v, cond);
4868 assertion = gimple_build_assign (NULL_TREE, a);
4870 /* The new ASSERT_EXPR, creates a new SSA name that replaces the
4871 operand of the ASSERT_EXPR. Create it so the new name and the old one
4872 are registered in the replacement table so that we can fix the SSA web
4873 after adding all the ASSERT_EXPRs. */
4874 create_new_def_for (v, assertion, NULL);
4876 return assertion;
4880 /* Return false if EXPR is a predicate expression involving floating
4881 point values. */
4883 static inline bool
4884 fp_predicate (gimple *stmt)
4886 GIMPLE_CHECK (stmt, GIMPLE_COND);
4888 return FLOAT_TYPE_P (TREE_TYPE (gimple_cond_lhs (stmt)));
4891 /* If the range of values taken by OP can be inferred after STMT executes,
4892 return the comparison code (COMP_CODE_P) and value (VAL_P) that
4893 describes the inferred range. Return true if a range could be
4894 inferred. */
4896 static bool
4897 infer_value_range (gimple *stmt, tree op, tree_code *comp_code_p, tree *val_p)
4899 *val_p = NULL_TREE;
4900 *comp_code_p = ERROR_MARK;
4902 /* Do not attempt to infer anything in names that flow through
4903 abnormal edges. */
4904 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op))
4905 return false;
4907 /* If STMT is the last statement of a basic block with no normal
4908 successors, there is no point inferring anything about any of its
4909 operands. We would not be able to find a proper insertion point
4910 for the assertion, anyway. */
4911 if (stmt_ends_bb_p (stmt))
4913 edge_iterator ei;
4914 edge e;
4916 FOR_EACH_EDGE (e, ei, gimple_bb (stmt)->succs)
4917 if (!(e->flags & (EDGE_ABNORMAL|EDGE_EH)))
4918 break;
4919 if (e == NULL)
4920 return false;
4923 if (infer_nonnull_range (stmt, op))
4925 *val_p = build_int_cst (TREE_TYPE (op), 0);
4926 *comp_code_p = NE_EXPR;
4927 return true;
4930 return false;
4934 void dump_asserts_for (FILE *, tree);
4935 void debug_asserts_for (tree);
4936 void dump_all_asserts (FILE *);
4937 void debug_all_asserts (void);
4939 /* Dump all the registered assertions for NAME to FILE. */
4941 void
4942 dump_asserts_for (FILE *file, tree name)
4944 assert_locus *loc;
4946 fprintf (file, "Assertions to be inserted for ");
4947 print_generic_expr (file, name, 0);
4948 fprintf (file, "\n");
4950 loc = asserts_for[SSA_NAME_VERSION (name)];
4951 while (loc)
4953 fprintf (file, "\t");
4954 print_gimple_stmt (file, gsi_stmt (loc->si), 0, 0);
4955 fprintf (file, "\n\tBB #%d", loc->bb->index);
4956 if (loc->e)
4958 fprintf (file, "\n\tEDGE %d->%d", loc->e->src->index,
4959 loc->e->dest->index);
4960 dump_edge_info (file, loc->e, dump_flags, 0);
4962 fprintf (file, "\n\tPREDICATE: ");
4963 print_generic_expr (file, loc->expr, 0);
4964 fprintf (file, " %s ", get_tree_code_name (loc->comp_code));
4965 print_generic_expr (file, loc->val, 0);
4966 fprintf (file, "\n\n");
4967 loc = loc->next;
4970 fprintf (file, "\n");
4974 /* Dump all the registered assertions for NAME to stderr. */
4976 DEBUG_FUNCTION void
4977 debug_asserts_for (tree name)
4979 dump_asserts_for (stderr, name);
4983 /* Dump all the registered assertions for all the names to FILE. */
4985 void
4986 dump_all_asserts (FILE *file)
4988 unsigned i;
4989 bitmap_iterator bi;
4991 fprintf (file, "\nASSERT_EXPRs to be inserted\n\n");
4992 EXECUTE_IF_SET_IN_BITMAP (need_assert_for, 0, i, bi)
4993 dump_asserts_for (file, ssa_name (i));
4994 fprintf (file, "\n");
4998 /* Dump all the registered assertions for all the names to stderr. */
5000 DEBUG_FUNCTION void
5001 debug_all_asserts (void)
5003 dump_all_asserts (stderr);
5007 /* If NAME doesn't have an ASSERT_EXPR registered for asserting
5008 'EXPR COMP_CODE VAL' at a location that dominates block BB or
5009 E->DEST, then register this location as a possible insertion point
5010 for ASSERT_EXPR <NAME, EXPR COMP_CODE VAL>.
5012 BB, E and SI provide the exact insertion point for the new
5013 ASSERT_EXPR. If BB is NULL, then the ASSERT_EXPR is to be inserted
5014 on edge E. Otherwise, if E is NULL, the ASSERT_EXPR is inserted on
5015 BB. If SI points to a COND_EXPR or a SWITCH_EXPR statement, then E
5016 must not be NULL. */
5018 static void
5019 register_new_assert_for (tree name, tree expr,
5020 enum tree_code comp_code,
5021 tree val,
5022 basic_block bb,
5023 edge e,
5024 gimple_stmt_iterator si)
5026 assert_locus *n, *loc, *last_loc;
5027 basic_block dest_bb;
5029 gcc_checking_assert (bb == NULL || e == NULL);
5031 if (e == NULL)
5032 gcc_checking_assert (gimple_code (gsi_stmt (si)) != GIMPLE_COND
5033 && gimple_code (gsi_stmt (si)) != GIMPLE_SWITCH);
5035 /* Never build an assert comparing against an integer constant with
5036 TREE_OVERFLOW set. This confuses our undefined overflow warning
5037 machinery. */
5038 if (TREE_OVERFLOW_P (val))
5039 val = drop_tree_overflow (val);
5041 /* The new assertion A will be inserted at BB or E. We need to
5042 determine if the new location is dominated by a previously
5043 registered location for A. If we are doing an edge insertion,
5044 assume that A will be inserted at E->DEST. Note that this is not
5045 necessarily true.
5047 If E is a critical edge, it will be split. But even if E is
5048 split, the new block will dominate the same set of blocks that
5049 E->DEST dominates.
5051 The reverse, however, is not true, blocks dominated by E->DEST
5052 will not be dominated by the new block created to split E. So,
5053 if the insertion location is on a critical edge, we will not use
5054 the new location to move another assertion previously registered
5055 at a block dominated by E->DEST. */
5056 dest_bb = (bb) ? bb : e->dest;
5058 /* If NAME already has an ASSERT_EXPR registered for COMP_CODE and
5059 VAL at a block dominating DEST_BB, then we don't need to insert a new
5060 one. Similarly, if the same assertion already exists at a block
5061 dominated by DEST_BB and the new location is not on a critical
5062 edge, then update the existing location for the assertion (i.e.,
5063 move the assertion up in the dominance tree).
5065 Note, this is implemented as a simple linked list because there
5066 should not be more than a handful of assertions registered per
5067 name. If this becomes a performance problem, a table hashed by
5068 COMP_CODE and VAL could be implemented. */
5069 loc = asserts_for[SSA_NAME_VERSION (name)];
5070 last_loc = loc;
5071 while (loc)
5073 if (loc->comp_code == comp_code
5074 && (loc->val == val
5075 || operand_equal_p (loc->val, val, 0))
5076 && (loc->expr == expr
5077 || operand_equal_p (loc->expr, expr, 0)))
5079 /* If E is not a critical edge and DEST_BB
5080 dominates the existing location for the assertion, move
5081 the assertion up in the dominance tree by updating its
5082 location information. */
5083 if ((e == NULL || !EDGE_CRITICAL_P (e))
5084 && dominated_by_p (CDI_DOMINATORS, loc->bb, dest_bb))
5086 loc->bb = dest_bb;
5087 loc->e = e;
5088 loc->si = si;
5089 return;
5093 /* Update the last node of the list and move to the next one. */
5094 last_loc = loc;
5095 loc = loc->next;
5098 /* If we didn't find an assertion already registered for
5099 NAME COMP_CODE VAL, add a new one at the end of the list of
5100 assertions associated with NAME. */
5101 n = XNEW (struct assert_locus);
5102 n->bb = dest_bb;
5103 n->e = e;
5104 n->si = si;
5105 n->comp_code = comp_code;
5106 n->val = val;
5107 n->expr = expr;
5108 n->next = NULL;
5110 if (last_loc)
5111 last_loc->next = n;
5112 else
5113 asserts_for[SSA_NAME_VERSION (name)] = n;
5115 bitmap_set_bit (need_assert_for, SSA_NAME_VERSION (name));
5118 /* (COND_OP0 COND_CODE COND_OP1) is a predicate which uses NAME.
5119 Extract a suitable test code and value and store them into *CODE_P and
5120 *VAL_P so the predicate is normalized to NAME *CODE_P *VAL_P.
5122 If no extraction was possible, return FALSE, otherwise return TRUE.
5124 If INVERT is true, then we invert the result stored into *CODE_P. */
5126 static bool
5127 extract_code_and_val_from_cond_with_ops (tree name, enum tree_code cond_code,
5128 tree cond_op0, tree cond_op1,
5129 bool invert, enum tree_code *code_p,
5130 tree *val_p)
5132 enum tree_code comp_code;
5133 tree val;
5135 /* Otherwise, we have a comparison of the form NAME COMP VAL
5136 or VAL COMP NAME. */
5137 if (name == cond_op1)
5139 /* If the predicate is of the form VAL COMP NAME, flip
5140 COMP around because we need to register NAME as the
5141 first operand in the predicate. */
5142 comp_code = swap_tree_comparison (cond_code);
5143 val = cond_op0;
5145 else if (name == cond_op0)
5147 /* The comparison is of the form NAME COMP VAL, so the
5148 comparison code remains unchanged. */
5149 comp_code = cond_code;
5150 val = cond_op1;
5152 else
5153 gcc_unreachable ();
5155 /* Invert the comparison code as necessary. */
5156 if (invert)
5157 comp_code = invert_tree_comparison (comp_code, 0);
5159 /* VRP only handles integral and pointer types. */
5160 if (! INTEGRAL_TYPE_P (TREE_TYPE (val))
5161 && ! POINTER_TYPE_P (TREE_TYPE (val)))
5162 return false;
5164 /* Do not register always-false predicates.
5165 FIXME: this works around a limitation in fold() when dealing with
5166 enumerations. Given 'enum { N1, N2 } x;', fold will not
5167 fold 'if (x > N2)' to 'if (0)'. */
5168 if ((comp_code == GT_EXPR || comp_code == LT_EXPR)
5169 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
5171 tree min = TYPE_MIN_VALUE (TREE_TYPE (val));
5172 tree max = TYPE_MAX_VALUE (TREE_TYPE (val));
5174 if (comp_code == GT_EXPR
5175 && (!max
5176 || compare_values (val, max) == 0))
5177 return false;
5179 if (comp_code == LT_EXPR
5180 && (!min
5181 || compare_values (val, min) == 0))
5182 return false;
5184 *code_p = comp_code;
5185 *val_p = val;
5186 return true;
5189 /* Find out smallest RES where RES > VAL && (RES & MASK) == RES, if any
5190 (otherwise return VAL). VAL and MASK must be zero-extended for
5191 precision PREC. If SGNBIT is non-zero, first xor VAL with SGNBIT
5192 (to transform signed values into unsigned) and at the end xor
5193 SGNBIT back. */
5195 static wide_int
5196 masked_increment (const wide_int &val_in, const wide_int &mask,
5197 const wide_int &sgnbit, unsigned int prec)
5199 wide_int bit = wi::one (prec), res;
5200 unsigned int i;
5202 wide_int val = val_in ^ sgnbit;
5203 for (i = 0; i < prec; i++, bit += bit)
5205 res = mask;
5206 if ((res & bit) == 0)
5207 continue;
5208 res = bit - 1;
5209 res = (val + bit).and_not (res);
5210 res &= mask;
5211 if (wi::gtu_p (res, val))
5212 return res ^ sgnbit;
5214 return val ^ sgnbit;
5217 /* Helper for overflow_comparison_p
5219 OP0 CODE OP1 is a comparison. Examine the comparison and potentially
5220 OP1's defining statement to see if it ultimately has the form
5221 OP0 CODE (OP0 PLUS INTEGER_CST)
5223 If so, return TRUE indicating this is an overflow test and store into
5224 *NEW_CST an updated constant that can be used in a narrowed range test.
5226 REVERSED indicates if the comparison was originally:
5228 OP1 CODE' OP0.
5230 This affects how we build the updated constant. */
5232 static bool
5233 overflow_comparison_p_1 (enum tree_code code, tree op0, tree op1,
5234 bool follow_assert_exprs, bool reversed, tree *new_cst)
5236 /* See if this is a relational operation between two SSA_NAMES with
5237 unsigned, overflow wrapping values. If so, check it more deeply. */
5238 if ((code == LT_EXPR || code == LE_EXPR
5239 || code == GE_EXPR || code == GT_EXPR)
5240 && TREE_CODE (op0) == SSA_NAME
5241 && TREE_CODE (op1) == SSA_NAME
5242 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
5243 && TYPE_UNSIGNED (TREE_TYPE (op0))
5244 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (op0)))
5246 gimple *op1_def = SSA_NAME_DEF_STMT (op1);
5248 /* If requested, follow any ASSERT_EXPRs backwards for OP1. */
5249 if (follow_assert_exprs)
5251 while (gimple_assign_single_p (op1_def)
5252 && TREE_CODE (gimple_assign_rhs1 (op1_def)) == ASSERT_EXPR)
5254 op1 = TREE_OPERAND (gimple_assign_rhs1 (op1_def), 0);
5255 if (TREE_CODE (op1) != SSA_NAME)
5256 break;
5257 op1_def = SSA_NAME_DEF_STMT (op1);
5261 /* Now look at the defining statement of OP1 to see if it adds
5262 or subtracts a nonzero constant from another operand. */
5263 if (op1_def
5264 && is_gimple_assign (op1_def)
5265 && gimple_assign_rhs_code (op1_def) == PLUS_EXPR
5266 && TREE_CODE (gimple_assign_rhs2 (op1_def)) == INTEGER_CST
5267 && !integer_zerop (gimple_assign_rhs2 (op1_def)))
5269 tree target = gimple_assign_rhs1 (op1_def);
5271 /* If requested, follow ASSERT_EXPRs backwards for op0 looking
5272 for one where TARGET appears on the RHS. */
5273 if (follow_assert_exprs)
5275 /* Now see if that "other operand" is op0, following the chain
5276 of ASSERT_EXPRs if necessary. */
5277 gimple *op0_def = SSA_NAME_DEF_STMT (op0);
5278 while (op0 != target
5279 && gimple_assign_single_p (op0_def)
5280 && TREE_CODE (gimple_assign_rhs1 (op0_def)) == ASSERT_EXPR)
5282 op0 = TREE_OPERAND (gimple_assign_rhs1 (op0_def), 0);
5283 if (TREE_CODE (op0) != SSA_NAME)
5284 break;
5285 op0_def = SSA_NAME_DEF_STMT (op0);
5289 /* If we did not find our target SSA_NAME, then this is not
5290 an overflow test. */
5291 if (op0 != target)
5292 return false;
5294 tree type = TREE_TYPE (op0);
5295 wide_int max = wi::max_value (TYPE_PRECISION (type), UNSIGNED);
5296 tree inc = gimple_assign_rhs2 (op1_def);
5297 if (reversed)
5298 *new_cst = wide_int_to_tree (type, max + inc);
5299 else
5300 *new_cst = wide_int_to_tree (type, max - inc);
5301 return true;
5304 return false;
5307 /* OP0 CODE OP1 is a comparison. Examine the comparison and potentially
5308 OP1's defining statement to see if it ultimately has the form
5309 OP0 CODE (OP0 PLUS INTEGER_CST)
5311 If so, return TRUE indicating this is an overflow test and store into
5312 *NEW_CST an updated constant that can be used in a narrowed range test.
5314 These statements are left as-is in the IL to facilitate discovery of
5315 {ADD,SUB}_OVERFLOW sequences later in the optimizer pipeline. But
5316 the alternate range representation is often useful within VRP. */
5318 static bool
5319 overflow_comparison_p (tree_code code, tree name, tree val,
5320 bool use_equiv_p, tree *new_cst)
5322 if (overflow_comparison_p_1 (code, name, val, use_equiv_p, false, new_cst))
5323 return true;
5324 return overflow_comparison_p_1 (swap_tree_comparison (code), val, name,
5325 use_equiv_p, true, new_cst);
5329 /* Try to register an edge assertion for SSA name NAME on edge E for
5330 the condition COND contributing to the conditional jump pointed to by BSI.
5331 Invert the condition COND if INVERT is true. */
5333 static void
5334 register_edge_assert_for_2 (tree name, edge e, gimple_stmt_iterator bsi,
5335 enum tree_code cond_code,
5336 tree cond_op0, tree cond_op1, bool invert)
5338 tree val;
5339 enum tree_code comp_code;
5341 if (!extract_code_and_val_from_cond_with_ops (name, cond_code,
5342 cond_op0,
5343 cond_op1,
5344 invert, &comp_code, &val))
5345 return;
5347 /* Only register an ASSERT_EXPR if NAME was found in the sub-graph
5348 reachable from E. */
5349 if (live_on_edge (e, name))
5351 tree x;
5352 if (overflow_comparison_p (comp_code, name, val, false, &x))
5354 enum tree_code new_code
5355 = ((comp_code == GT_EXPR || comp_code == GE_EXPR)
5356 ? GT_EXPR : LE_EXPR);
5357 register_new_assert_for (name, name, new_code, x, NULL, e, bsi);
5359 register_new_assert_for (name, name, comp_code, val, NULL, e, bsi);
5362 /* In the case of NAME <= CST and NAME being defined as
5363 NAME = (unsigned) NAME2 + CST2 we can assert NAME2 >= -CST2
5364 and NAME2 <= CST - CST2. We can do the same for NAME > CST.
5365 This catches range and anti-range tests. */
5366 if ((comp_code == LE_EXPR
5367 || comp_code == GT_EXPR)
5368 && TREE_CODE (val) == INTEGER_CST
5369 && TYPE_UNSIGNED (TREE_TYPE (val)))
5371 gimple *def_stmt = SSA_NAME_DEF_STMT (name);
5372 tree cst2 = NULL_TREE, name2 = NULL_TREE, name3 = NULL_TREE;
5374 /* Extract CST2 from the (optional) addition. */
5375 if (is_gimple_assign (def_stmt)
5376 && gimple_assign_rhs_code (def_stmt) == PLUS_EXPR)
5378 name2 = gimple_assign_rhs1 (def_stmt);
5379 cst2 = gimple_assign_rhs2 (def_stmt);
5380 if (TREE_CODE (name2) == SSA_NAME
5381 && TREE_CODE (cst2) == INTEGER_CST)
5382 def_stmt = SSA_NAME_DEF_STMT (name2);
5385 /* Extract NAME2 from the (optional) sign-changing cast. */
5386 if (gimple_assign_cast_p (def_stmt))
5388 if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt))
5389 && ! TYPE_UNSIGNED (TREE_TYPE (gimple_assign_rhs1 (def_stmt)))
5390 && (TYPE_PRECISION (gimple_expr_type (def_stmt))
5391 == TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt)))))
5392 name3 = gimple_assign_rhs1 (def_stmt);
5395 /* If name3 is used later, create an ASSERT_EXPR for it. */
5396 if (name3 != NULL_TREE
5397 && TREE_CODE (name3) == SSA_NAME
5398 && (cst2 == NULL_TREE
5399 || TREE_CODE (cst2) == INTEGER_CST)
5400 && INTEGRAL_TYPE_P (TREE_TYPE (name3))
5401 && live_on_edge (e, name3))
5403 tree tmp;
5405 /* Build an expression for the range test. */
5406 tmp = build1 (NOP_EXPR, TREE_TYPE (name), name3);
5407 if (cst2 != NULL_TREE)
5408 tmp = build2 (PLUS_EXPR, TREE_TYPE (name), tmp, cst2);
5410 if (dump_file)
5412 fprintf (dump_file, "Adding assert for ");
5413 print_generic_expr (dump_file, name3, 0);
5414 fprintf (dump_file, " from ");
5415 print_generic_expr (dump_file, tmp, 0);
5416 fprintf (dump_file, "\n");
5419 register_new_assert_for (name3, tmp, comp_code, val, NULL, e, bsi);
5422 /* If name2 is used later, create an ASSERT_EXPR for it. */
5423 if (name2 != NULL_TREE
5424 && TREE_CODE (name2) == SSA_NAME
5425 && TREE_CODE (cst2) == INTEGER_CST
5426 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
5427 && live_on_edge (e, name2))
5429 tree tmp;
5431 /* Build an expression for the range test. */
5432 tmp = name2;
5433 if (TREE_TYPE (name) != TREE_TYPE (name2))
5434 tmp = build1 (NOP_EXPR, TREE_TYPE (name), tmp);
5435 if (cst2 != NULL_TREE)
5436 tmp = build2 (PLUS_EXPR, TREE_TYPE (name), tmp, cst2);
5438 if (dump_file)
5440 fprintf (dump_file, "Adding assert for ");
5441 print_generic_expr (dump_file, name2, 0);
5442 fprintf (dump_file, " from ");
5443 print_generic_expr (dump_file, tmp, 0);
5444 fprintf (dump_file, "\n");
5447 register_new_assert_for (name2, tmp, comp_code, val, NULL, e, bsi);
5451 /* In the case of post-in/decrement tests like if (i++) ... and uses
5452 of the in/decremented value on the edge the extra name we want to
5453 assert for is not on the def chain of the name compared. Instead
5454 it is in the set of use stmts.
5455 Similar cases happen for conversions that were simplified through
5456 fold_{sign_changed,widened}_comparison. */
5457 if ((comp_code == NE_EXPR
5458 || comp_code == EQ_EXPR)
5459 && TREE_CODE (val) == INTEGER_CST)
5461 imm_use_iterator ui;
5462 gimple *use_stmt;
5463 FOR_EACH_IMM_USE_STMT (use_stmt, ui, name)
5465 if (!is_gimple_assign (use_stmt))
5466 continue;
5468 /* Cut off to use-stmts that are dominating the predecessor. */
5469 if (!dominated_by_p (CDI_DOMINATORS, e->src, gimple_bb (use_stmt)))
5470 continue;
5472 tree name2 = gimple_assign_lhs (use_stmt);
5473 if (TREE_CODE (name2) != SSA_NAME
5474 || !live_on_edge (e, name2))
5475 continue;
5477 enum tree_code code = gimple_assign_rhs_code (use_stmt);
5478 tree cst;
5479 if (code == PLUS_EXPR
5480 || code == MINUS_EXPR)
5482 cst = gimple_assign_rhs2 (use_stmt);
5483 if (TREE_CODE (cst) != INTEGER_CST)
5484 continue;
5485 cst = int_const_binop (code, val, cst);
5487 else if (CONVERT_EXPR_CODE_P (code))
5489 /* For truncating conversions we cannot record
5490 an inequality. */
5491 if (comp_code == NE_EXPR
5492 && (TYPE_PRECISION (TREE_TYPE (name2))
5493 < TYPE_PRECISION (TREE_TYPE (name))))
5494 continue;
5495 cst = fold_convert (TREE_TYPE (name2), val);
5497 else
5498 continue;
5500 if (TREE_OVERFLOW_P (cst))
5501 cst = drop_tree_overflow (cst);
5502 register_new_assert_for (name2, name2, comp_code, cst,
5503 NULL, e, bsi);
5507 if (TREE_CODE_CLASS (comp_code) == tcc_comparison
5508 && TREE_CODE (val) == INTEGER_CST)
5510 gimple *def_stmt = SSA_NAME_DEF_STMT (name);
5511 tree name2 = NULL_TREE, names[2], cst2 = NULL_TREE;
5512 tree val2 = NULL_TREE;
5513 unsigned int prec = TYPE_PRECISION (TREE_TYPE (val));
5514 wide_int mask = wi::zero (prec);
5515 unsigned int nprec = prec;
5516 enum tree_code rhs_code = ERROR_MARK;
5518 if (is_gimple_assign (def_stmt))
5519 rhs_code = gimple_assign_rhs_code (def_stmt);
5521 /* In the case of NAME != CST1 where NAME = A +- CST2 we can
5522 assert that A != CST1 -+ CST2. */
5523 if ((comp_code == EQ_EXPR || comp_code == NE_EXPR)
5524 && (rhs_code == PLUS_EXPR || rhs_code == MINUS_EXPR))
5526 tree op0 = gimple_assign_rhs1 (def_stmt);
5527 tree op1 = gimple_assign_rhs2 (def_stmt);
5528 if (TREE_CODE (op0) == SSA_NAME
5529 && TREE_CODE (op1) == INTEGER_CST
5530 && live_on_edge (e, op0))
5532 enum tree_code reverse_op = (rhs_code == PLUS_EXPR
5533 ? MINUS_EXPR : PLUS_EXPR);
5534 op1 = int_const_binop (reverse_op, val, op1);
5535 if (TREE_OVERFLOW (op1))
5536 op1 = drop_tree_overflow (op1);
5537 register_new_assert_for (op0, op0, comp_code, op1, NULL, e, bsi);
5541 /* Add asserts for NAME cmp CST and NAME being defined
5542 as NAME = (int) NAME2. */
5543 if (!TYPE_UNSIGNED (TREE_TYPE (val))
5544 && (comp_code == LE_EXPR || comp_code == LT_EXPR
5545 || comp_code == GT_EXPR || comp_code == GE_EXPR)
5546 && gimple_assign_cast_p (def_stmt))
5548 name2 = gimple_assign_rhs1 (def_stmt);
5549 if (CONVERT_EXPR_CODE_P (rhs_code)
5550 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
5551 && TYPE_UNSIGNED (TREE_TYPE (name2))
5552 && prec == TYPE_PRECISION (TREE_TYPE (name2))
5553 && (comp_code == LE_EXPR || comp_code == GT_EXPR
5554 || !tree_int_cst_equal (val,
5555 TYPE_MIN_VALUE (TREE_TYPE (val))))
5556 && live_on_edge (e, name2))
5558 tree tmp, cst;
5559 enum tree_code new_comp_code = comp_code;
5561 cst = fold_convert (TREE_TYPE (name2),
5562 TYPE_MIN_VALUE (TREE_TYPE (val)));
5563 /* Build an expression for the range test. */
5564 tmp = build2 (PLUS_EXPR, TREE_TYPE (name2), name2, cst);
5565 cst = fold_build2 (PLUS_EXPR, TREE_TYPE (name2), cst,
5566 fold_convert (TREE_TYPE (name2), val));
5567 if (comp_code == LT_EXPR || comp_code == GE_EXPR)
5569 new_comp_code = comp_code == LT_EXPR ? LE_EXPR : GT_EXPR;
5570 cst = fold_build2 (MINUS_EXPR, TREE_TYPE (name2), cst,
5571 build_int_cst (TREE_TYPE (name2), 1));
5574 if (dump_file)
5576 fprintf (dump_file, "Adding assert for ");
5577 print_generic_expr (dump_file, name2, 0);
5578 fprintf (dump_file, " from ");
5579 print_generic_expr (dump_file, tmp, 0);
5580 fprintf (dump_file, "\n");
5583 register_new_assert_for (name2, tmp, new_comp_code, cst, NULL,
5584 e, bsi);
5588 /* Add asserts for NAME cmp CST and NAME being defined as
5589 NAME = NAME2 >> CST2.
5591 Extract CST2 from the right shift. */
5592 if (rhs_code == RSHIFT_EXPR)
5594 name2 = gimple_assign_rhs1 (def_stmt);
5595 cst2 = gimple_assign_rhs2 (def_stmt);
5596 if (TREE_CODE (name2) == SSA_NAME
5597 && tree_fits_uhwi_p (cst2)
5598 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
5599 && IN_RANGE (tree_to_uhwi (cst2), 1, prec - 1)
5600 && prec == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (val)))
5601 && live_on_edge (e, name2))
5603 mask = wi::mask (tree_to_uhwi (cst2), false, prec);
5604 val2 = fold_binary (LSHIFT_EXPR, TREE_TYPE (val), val, cst2);
5607 if (val2 != NULL_TREE
5608 && TREE_CODE (val2) == INTEGER_CST
5609 && simple_cst_equal (fold_build2 (RSHIFT_EXPR,
5610 TREE_TYPE (val),
5611 val2, cst2), val))
5613 enum tree_code new_comp_code = comp_code;
5614 tree tmp, new_val;
5616 tmp = name2;
5617 if (comp_code == EQ_EXPR || comp_code == NE_EXPR)
5619 if (!TYPE_UNSIGNED (TREE_TYPE (val)))
5621 tree type = build_nonstandard_integer_type (prec, 1);
5622 tmp = build1 (NOP_EXPR, type, name2);
5623 val2 = fold_convert (type, val2);
5625 tmp = fold_build2 (MINUS_EXPR, TREE_TYPE (tmp), tmp, val2);
5626 new_val = wide_int_to_tree (TREE_TYPE (tmp), mask);
5627 new_comp_code = comp_code == EQ_EXPR ? LE_EXPR : GT_EXPR;
5629 else if (comp_code == LT_EXPR || comp_code == GE_EXPR)
5631 wide_int minval
5632 = wi::min_value (prec, TYPE_SIGN (TREE_TYPE (val)));
5633 new_val = val2;
5634 if (minval == new_val)
5635 new_val = NULL_TREE;
5637 else
5639 wide_int maxval
5640 = wi::max_value (prec, TYPE_SIGN (TREE_TYPE (val)));
5641 mask |= val2;
5642 if (mask == maxval)
5643 new_val = NULL_TREE;
5644 else
5645 new_val = wide_int_to_tree (TREE_TYPE (val2), mask);
5648 if (new_val)
5650 if (dump_file)
5652 fprintf (dump_file, "Adding assert for ");
5653 print_generic_expr (dump_file, name2, 0);
5654 fprintf (dump_file, " from ");
5655 print_generic_expr (dump_file, tmp, 0);
5656 fprintf (dump_file, "\n");
5659 register_new_assert_for (name2, tmp, new_comp_code, new_val,
5660 NULL, e, bsi);
5664 /* Add asserts for NAME cmp CST and NAME being defined as
5665 NAME = NAME2 & CST2.
5667 Extract CST2 from the and.
5669 Also handle
5670 NAME = (unsigned) NAME2;
5671 casts where NAME's type is unsigned and has smaller precision
5672 than NAME2's type as if it was NAME = NAME2 & MASK. */
5673 names[0] = NULL_TREE;
5674 names[1] = NULL_TREE;
5675 cst2 = NULL_TREE;
5676 if (rhs_code == BIT_AND_EXPR
5677 || (CONVERT_EXPR_CODE_P (rhs_code)
5678 && INTEGRAL_TYPE_P (TREE_TYPE (val))
5679 && TYPE_UNSIGNED (TREE_TYPE (val))
5680 && TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt)))
5681 > prec))
5683 name2 = gimple_assign_rhs1 (def_stmt);
5684 if (rhs_code == BIT_AND_EXPR)
5685 cst2 = gimple_assign_rhs2 (def_stmt);
5686 else
5688 cst2 = TYPE_MAX_VALUE (TREE_TYPE (val));
5689 nprec = TYPE_PRECISION (TREE_TYPE (name2));
5691 if (TREE_CODE (name2) == SSA_NAME
5692 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
5693 && TREE_CODE (cst2) == INTEGER_CST
5694 && !integer_zerop (cst2)
5695 && (nprec > 1
5696 || TYPE_UNSIGNED (TREE_TYPE (val))))
5698 gimple *def_stmt2 = SSA_NAME_DEF_STMT (name2);
5699 if (gimple_assign_cast_p (def_stmt2))
5701 names[1] = gimple_assign_rhs1 (def_stmt2);
5702 if (!CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt2))
5703 || !INTEGRAL_TYPE_P (TREE_TYPE (names[1]))
5704 || (TYPE_PRECISION (TREE_TYPE (name2))
5705 != TYPE_PRECISION (TREE_TYPE (names[1])))
5706 || !live_on_edge (e, names[1]))
5707 names[1] = NULL_TREE;
5709 if (live_on_edge (e, name2))
5710 names[0] = name2;
5713 if (names[0] || names[1])
5715 wide_int minv, maxv, valv, cst2v;
5716 wide_int tem, sgnbit;
5717 bool valid_p = false, valn, cst2n;
5718 enum tree_code ccode = comp_code;
5720 valv = wide_int::from (val, nprec, UNSIGNED);
5721 cst2v = wide_int::from (cst2, nprec, UNSIGNED);
5722 valn = wi::neg_p (valv, TYPE_SIGN (TREE_TYPE (val)));
5723 cst2n = wi::neg_p (cst2v, TYPE_SIGN (TREE_TYPE (val)));
5724 /* If CST2 doesn't have most significant bit set,
5725 but VAL is negative, we have comparison like
5726 if ((x & 0x123) > -4) (always true). Just give up. */
5727 if (!cst2n && valn)
5728 ccode = ERROR_MARK;
5729 if (cst2n)
5730 sgnbit = wi::set_bit_in_zero (nprec - 1, nprec);
5731 else
5732 sgnbit = wi::zero (nprec);
5733 minv = valv & cst2v;
5734 switch (ccode)
5736 case EQ_EXPR:
5737 /* Minimum unsigned value for equality is VAL & CST2
5738 (should be equal to VAL, otherwise we probably should
5739 have folded the comparison into false) and
5740 maximum unsigned value is VAL | ~CST2. */
5741 maxv = valv | ~cst2v;
5742 valid_p = true;
5743 break;
5745 case NE_EXPR:
5746 tem = valv | ~cst2v;
5747 /* If VAL is 0, handle (X & CST2) != 0 as (X & CST2) > 0U. */
5748 if (valv == 0)
5750 cst2n = false;
5751 sgnbit = wi::zero (nprec);
5752 goto gt_expr;
5754 /* If (VAL | ~CST2) is all ones, handle it as
5755 (X & CST2) < VAL. */
5756 if (tem == -1)
5758 cst2n = false;
5759 valn = false;
5760 sgnbit = wi::zero (nprec);
5761 goto lt_expr;
5763 if (!cst2n && wi::neg_p (cst2v))
5764 sgnbit = wi::set_bit_in_zero (nprec - 1, nprec);
5765 if (sgnbit != 0)
5767 if (valv == sgnbit)
5769 cst2n = true;
5770 valn = true;
5771 goto gt_expr;
5773 if (tem == wi::mask (nprec - 1, false, nprec))
5775 cst2n = true;
5776 goto lt_expr;
5778 if (!cst2n)
5779 sgnbit = wi::zero (nprec);
5781 break;
5783 case GE_EXPR:
5784 /* Minimum unsigned value for >= if (VAL & CST2) == VAL
5785 is VAL and maximum unsigned value is ~0. For signed
5786 comparison, if CST2 doesn't have most significant bit
5787 set, handle it similarly. If CST2 has MSB set,
5788 the minimum is the same, and maximum is ~0U/2. */
5789 if (minv != valv)
5791 /* If (VAL & CST2) != VAL, X & CST2 can't be equal to
5792 VAL. */
5793 minv = masked_increment (valv, cst2v, sgnbit, nprec);
5794 if (minv == valv)
5795 break;
5797 maxv = wi::mask (nprec - (cst2n ? 1 : 0), false, nprec);
5798 valid_p = true;
5799 break;
5801 case GT_EXPR:
5802 gt_expr:
5803 /* Find out smallest MINV where MINV > VAL
5804 && (MINV & CST2) == MINV, if any. If VAL is signed and
5805 CST2 has MSB set, compute it biased by 1 << (nprec - 1). */
5806 minv = masked_increment (valv, cst2v, sgnbit, nprec);
5807 if (minv == valv)
5808 break;
5809 maxv = wi::mask (nprec - (cst2n ? 1 : 0), false, nprec);
5810 valid_p = true;
5811 break;
5813 case LE_EXPR:
5814 /* Minimum unsigned value for <= is 0 and maximum
5815 unsigned value is VAL | ~CST2 if (VAL & CST2) == VAL.
5816 Otherwise, find smallest VAL2 where VAL2 > VAL
5817 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
5818 as maximum.
5819 For signed comparison, if CST2 doesn't have most
5820 significant bit set, handle it similarly. If CST2 has
5821 MSB set, the maximum is the same and minimum is INT_MIN. */
5822 if (minv == valv)
5823 maxv = valv;
5824 else
5826 maxv = masked_increment (valv, cst2v, sgnbit, nprec);
5827 if (maxv == valv)
5828 break;
5829 maxv -= 1;
5831 maxv |= ~cst2v;
5832 minv = sgnbit;
5833 valid_p = true;
5834 break;
5836 case LT_EXPR:
5837 lt_expr:
5838 /* Minimum unsigned value for < is 0 and maximum
5839 unsigned value is (VAL-1) | ~CST2 if (VAL & CST2) == VAL.
5840 Otherwise, find smallest VAL2 where VAL2 > VAL
5841 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
5842 as maximum.
5843 For signed comparison, if CST2 doesn't have most
5844 significant bit set, handle it similarly. If CST2 has
5845 MSB set, the maximum is the same and minimum is INT_MIN. */
5846 if (minv == valv)
5848 if (valv == sgnbit)
5849 break;
5850 maxv = valv;
5852 else
5854 maxv = masked_increment (valv, cst2v, sgnbit, nprec);
5855 if (maxv == valv)
5856 break;
5858 maxv -= 1;
5859 maxv |= ~cst2v;
5860 minv = sgnbit;
5861 valid_p = true;
5862 break;
5864 default:
5865 break;
5867 if (valid_p
5868 && (maxv - minv) != -1)
5870 tree tmp, new_val, type;
5871 int i;
5873 for (i = 0; i < 2; i++)
5874 if (names[i])
5876 wide_int maxv2 = maxv;
5877 tmp = names[i];
5878 type = TREE_TYPE (names[i]);
5879 if (!TYPE_UNSIGNED (type))
5881 type = build_nonstandard_integer_type (nprec, 1);
5882 tmp = build1 (NOP_EXPR, type, names[i]);
5884 if (minv != 0)
5886 tmp = build2 (PLUS_EXPR, type, tmp,
5887 wide_int_to_tree (type, -minv));
5888 maxv2 = maxv - minv;
5890 new_val = wide_int_to_tree (type, maxv2);
5892 if (dump_file)
5894 fprintf (dump_file, "Adding assert for ");
5895 print_generic_expr (dump_file, names[i], 0);
5896 fprintf (dump_file, " from ");
5897 print_generic_expr (dump_file, tmp, 0);
5898 fprintf (dump_file, "\n");
5901 register_new_assert_for (names[i], tmp, LE_EXPR,
5902 new_val, NULL, e, bsi);
5909 /* OP is an operand of a truth value expression which is known to have
5910 a particular value. Register any asserts for OP and for any
5911 operands in OP's defining statement.
5913 If CODE is EQ_EXPR, then we want to register OP is zero (false),
5914 if CODE is NE_EXPR, then we want to register OP is nonzero (true). */
5916 static void
5917 register_edge_assert_for_1 (tree op, enum tree_code code,
5918 edge e, gimple_stmt_iterator bsi)
5920 gimple *op_def;
5921 tree val;
5922 enum tree_code rhs_code;
5924 /* We only care about SSA_NAMEs. */
5925 if (TREE_CODE (op) != SSA_NAME)
5926 return;
5928 /* We know that OP will have a zero or nonzero value. If OP is used
5929 more than once go ahead and register an assert for OP. */
5930 if (live_on_edge (e, op))
5932 val = build_int_cst (TREE_TYPE (op), 0);
5933 register_new_assert_for (op, op, code, val, NULL, e, bsi);
5936 /* Now look at how OP is set. If it's set from a comparison,
5937 a truth operation or some bit operations, then we may be able
5938 to register information about the operands of that assignment. */
5939 op_def = SSA_NAME_DEF_STMT (op);
5940 if (gimple_code (op_def) != GIMPLE_ASSIGN)
5941 return;
5943 rhs_code = gimple_assign_rhs_code (op_def);
5945 if (TREE_CODE_CLASS (rhs_code) == tcc_comparison)
5947 bool invert = (code == EQ_EXPR ? true : false);
5948 tree op0 = gimple_assign_rhs1 (op_def);
5949 tree op1 = gimple_assign_rhs2 (op_def);
5951 if (TREE_CODE (op0) == SSA_NAME)
5952 register_edge_assert_for_2 (op0, e, bsi, rhs_code, op0, op1, invert);
5953 if (TREE_CODE (op1) == SSA_NAME)
5954 register_edge_assert_for_2 (op1, e, bsi, rhs_code, op0, op1, invert);
5956 else if ((code == NE_EXPR
5957 && gimple_assign_rhs_code (op_def) == BIT_AND_EXPR)
5958 || (code == EQ_EXPR
5959 && gimple_assign_rhs_code (op_def) == BIT_IOR_EXPR))
5961 /* Recurse on each operand. */
5962 tree op0 = gimple_assign_rhs1 (op_def);
5963 tree op1 = gimple_assign_rhs2 (op_def);
5964 if (TREE_CODE (op0) == SSA_NAME
5965 && has_single_use (op0))
5966 register_edge_assert_for_1 (op0, code, e, bsi);
5967 if (TREE_CODE (op1) == SSA_NAME
5968 && has_single_use (op1))
5969 register_edge_assert_for_1 (op1, code, e, bsi);
5971 else if (gimple_assign_rhs_code (op_def) == BIT_NOT_EXPR
5972 && TYPE_PRECISION (TREE_TYPE (gimple_assign_lhs (op_def))) == 1)
5974 /* Recurse, flipping CODE. */
5975 code = invert_tree_comparison (code, false);
5976 register_edge_assert_for_1 (gimple_assign_rhs1 (op_def), code, e, bsi);
5978 else if (gimple_assign_rhs_code (op_def) == SSA_NAME)
5980 /* Recurse through the copy. */
5981 register_edge_assert_for_1 (gimple_assign_rhs1 (op_def), code, e, bsi);
5983 else if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (op_def)))
5985 /* Recurse through the type conversion, unless it is a narrowing
5986 conversion or conversion from non-integral type. */
5987 tree rhs = gimple_assign_rhs1 (op_def);
5988 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs))
5989 && (TYPE_PRECISION (TREE_TYPE (rhs))
5990 <= TYPE_PRECISION (TREE_TYPE (op))))
5991 register_edge_assert_for_1 (rhs, code, e, bsi);
5995 /* Try to register an edge assertion for SSA name NAME on edge E for
5996 the condition COND contributing to the conditional jump pointed to by
5997 SI. */
5999 static void
6000 register_edge_assert_for (tree name, edge e, gimple_stmt_iterator si,
6001 enum tree_code cond_code, tree cond_op0,
6002 tree cond_op1)
6004 tree val;
6005 enum tree_code comp_code;
6006 bool is_else_edge = (e->flags & EDGE_FALSE_VALUE) != 0;
6008 /* Do not attempt to infer anything in names that flow through
6009 abnormal edges. */
6010 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name))
6011 return;
6013 if (!extract_code_and_val_from_cond_with_ops (name, cond_code,
6014 cond_op0, cond_op1,
6015 is_else_edge,
6016 &comp_code, &val))
6017 return;
6019 /* Register ASSERT_EXPRs for name. */
6020 register_edge_assert_for_2 (name, e, si, cond_code, cond_op0,
6021 cond_op1, is_else_edge);
6024 /* If COND is effectively an equality test of an SSA_NAME against
6025 the value zero or one, then we may be able to assert values
6026 for SSA_NAMEs which flow into COND. */
6028 /* In the case of NAME == 1 or NAME != 0, for BIT_AND_EXPR defining
6029 statement of NAME we can assert both operands of the BIT_AND_EXPR
6030 have nonzero value. */
6031 if (((comp_code == EQ_EXPR && integer_onep (val))
6032 || (comp_code == NE_EXPR && integer_zerop (val))))
6034 gimple *def_stmt = SSA_NAME_DEF_STMT (name);
6036 if (is_gimple_assign (def_stmt)
6037 && gimple_assign_rhs_code (def_stmt) == BIT_AND_EXPR)
6039 tree op0 = gimple_assign_rhs1 (def_stmt);
6040 tree op1 = gimple_assign_rhs2 (def_stmt);
6041 register_edge_assert_for_1 (op0, NE_EXPR, e, si);
6042 register_edge_assert_for_1 (op1, NE_EXPR, e, si);
6046 /* In the case of NAME == 0 or NAME != 1, for BIT_IOR_EXPR defining
6047 statement of NAME we can assert both operands of the BIT_IOR_EXPR
6048 have zero value. */
6049 if (((comp_code == EQ_EXPR && integer_zerop (val))
6050 || (comp_code == NE_EXPR && integer_onep (val))))
6052 gimple *def_stmt = SSA_NAME_DEF_STMT (name);
6054 /* For BIT_IOR_EXPR only if NAME == 0 both operands have
6055 necessarily zero value, or if type-precision is one. */
6056 if (is_gimple_assign (def_stmt)
6057 && (gimple_assign_rhs_code (def_stmt) == BIT_IOR_EXPR
6058 && (TYPE_PRECISION (TREE_TYPE (name)) == 1
6059 || comp_code == EQ_EXPR)))
6061 tree op0 = gimple_assign_rhs1 (def_stmt);
6062 tree op1 = gimple_assign_rhs2 (def_stmt);
6063 register_edge_assert_for_1 (op0, EQ_EXPR, e, si);
6064 register_edge_assert_for_1 (op1, EQ_EXPR, e, si);
6070 /* Determine whether the outgoing edges of BB should receive an
6071 ASSERT_EXPR for each of the operands of BB's LAST statement.
6072 The last statement of BB must be a COND_EXPR.
6074 If any of the sub-graphs rooted at BB have an interesting use of
6075 the predicate operands, an assert location node is added to the
6076 list of assertions for the corresponding operands. */
6078 static void
6079 find_conditional_asserts (basic_block bb, gcond *last)
6081 gimple_stmt_iterator bsi;
6082 tree op;
6083 edge_iterator ei;
6084 edge e;
6085 ssa_op_iter iter;
6087 bsi = gsi_for_stmt (last);
6089 /* Look for uses of the operands in each of the sub-graphs
6090 rooted at BB. We need to check each of the outgoing edges
6091 separately, so that we know what kind of ASSERT_EXPR to
6092 insert. */
6093 FOR_EACH_EDGE (e, ei, bb->succs)
6095 if (e->dest == bb)
6096 continue;
6098 /* Register the necessary assertions for each operand in the
6099 conditional predicate. */
6100 FOR_EACH_SSA_TREE_OPERAND (op, last, iter, SSA_OP_USE)
6101 register_edge_assert_for (op, e, bsi,
6102 gimple_cond_code (last),
6103 gimple_cond_lhs (last),
6104 gimple_cond_rhs (last));
6108 struct case_info
6110 tree expr;
6111 basic_block bb;
6114 /* Compare two case labels sorting first by the destination bb index
6115 and then by the case value. */
6117 static int
6118 compare_case_labels (const void *p1, const void *p2)
6120 const struct case_info *ci1 = (const struct case_info *) p1;
6121 const struct case_info *ci2 = (const struct case_info *) p2;
6122 int idx1 = ci1->bb->index;
6123 int idx2 = ci2->bb->index;
6125 if (idx1 < idx2)
6126 return -1;
6127 else if (idx1 == idx2)
6129 /* Make sure the default label is first in a group. */
6130 if (!CASE_LOW (ci1->expr))
6131 return -1;
6132 else if (!CASE_LOW (ci2->expr))
6133 return 1;
6134 else
6135 return tree_int_cst_compare (CASE_LOW (ci1->expr),
6136 CASE_LOW (ci2->expr));
6138 else
6139 return 1;
6142 /* Determine whether the outgoing edges of BB should receive an
6143 ASSERT_EXPR for each of the operands of BB's LAST statement.
6144 The last statement of BB must be a SWITCH_EXPR.
6146 If any of the sub-graphs rooted at BB have an interesting use of
6147 the predicate operands, an assert location node is added to the
6148 list of assertions for the corresponding operands. */
6150 static void
6151 find_switch_asserts (basic_block bb, gswitch *last)
6153 gimple_stmt_iterator bsi;
6154 tree op;
6155 edge e;
6156 struct case_info *ci;
6157 size_t n = gimple_switch_num_labels (last);
6158 #if GCC_VERSION >= 4000
6159 unsigned int idx;
6160 #else
6161 /* Work around GCC 3.4 bug (PR 37086). */
6162 volatile unsigned int idx;
6163 #endif
6165 bsi = gsi_for_stmt (last);
6166 op = gimple_switch_index (last);
6167 if (TREE_CODE (op) != SSA_NAME)
6168 return;
6170 /* Build a vector of case labels sorted by destination label. */
6171 ci = XNEWVEC (struct case_info, n);
6172 for (idx = 0; idx < n; ++idx)
6174 ci[idx].expr = gimple_switch_label (last, idx);
6175 ci[idx].bb = label_to_block (CASE_LABEL (ci[idx].expr));
6177 edge default_edge = find_edge (bb, ci[0].bb);
6178 qsort (ci, n, sizeof (struct case_info), compare_case_labels);
6180 for (idx = 0; idx < n; ++idx)
6182 tree min, max;
6183 tree cl = ci[idx].expr;
6184 basic_block cbb = ci[idx].bb;
6186 min = CASE_LOW (cl);
6187 max = CASE_HIGH (cl);
6189 /* If there are multiple case labels with the same destination
6190 we need to combine them to a single value range for the edge. */
6191 if (idx + 1 < n && cbb == ci[idx + 1].bb)
6193 /* Skip labels until the last of the group. */
6194 do {
6195 ++idx;
6196 } while (idx < n && cbb == ci[idx].bb);
6197 --idx;
6199 /* Pick up the maximum of the case label range. */
6200 if (CASE_HIGH (ci[idx].expr))
6201 max = CASE_HIGH (ci[idx].expr);
6202 else
6203 max = CASE_LOW (ci[idx].expr);
6206 /* Can't extract a useful assertion out of a range that includes the
6207 default label. */
6208 if (min == NULL_TREE)
6209 continue;
6211 /* Find the edge to register the assert expr on. */
6212 e = find_edge (bb, cbb);
6214 /* Register the necessary assertions for the operand in the
6215 SWITCH_EXPR. */
6216 register_edge_assert_for (op, e, bsi,
6217 max ? GE_EXPR : EQ_EXPR,
6218 op, fold_convert (TREE_TYPE (op), min));
6219 if (max)
6220 register_edge_assert_for (op, e, bsi, LE_EXPR, op,
6221 fold_convert (TREE_TYPE (op), max));
6224 XDELETEVEC (ci);
6226 if (!live_on_edge (default_edge, op))
6227 return;
6229 /* Now register along the default label assertions that correspond to the
6230 anti-range of each label. */
6231 int insertion_limit = PARAM_VALUE (PARAM_MAX_VRP_SWITCH_ASSERTIONS);
6232 if (insertion_limit == 0)
6233 return;
6235 /* We can't do this if the default case shares a label with another case. */
6236 tree default_cl = gimple_switch_default_label (last);
6237 for (idx = 1; idx < n; idx++)
6239 tree min, max;
6240 tree cl = gimple_switch_label (last, idx);
6241 if (CASE_LABEL (cl) == CASE_LABEL (default_cl))
6242 continue;
6244 min = CASE_LOW (cl);
6245 max = CASE_HIGH (cl);
6247 /* Combine contiguous case ranges to reduce the number of assertions
6248 to insert. */
6249 for (idx = idx + 1; idx < n; idx++)
6251 tree next_min, next_max;
6252 tree next_cl = gimple_switch_label (last, idx);
6253 if (CASE_LABEL (next_cl) == CASE_LABEL (default_cl))
6254 break;
6256 next_min = CASE_LOW (next_cl);
6257 next_max = CASE_HIGH (next_cl);
6259 wide_int difference = wi::sub (next_min, max ? max : min);
6260 if (wi::eq_p (difference, 1))
6261 max = next_max ? next_max : next_min;
6262 else
6263 break;
6265 idx--;
6267 if (max == NULL_TREE)
6269 /* Register the assertion OP != MIN. */
6270 min = fold_convert (TREE_TYPE (op), min);
6271 register_edge_assert_for (op, default_edge, bsi, NE_EXPR, op, min);
6273 else
6275 /* Register the assertion (unsigned)OP - MIN > (MAX - MIN),
6276 which will give OP the anti-range ~[MIN,MAX]. */
6277 tree uop = fold_convert (unsigned_type_for (TREE_TYPE (op)), op);
6278 min = fold_convert (TREE_TYPE (uop), min);
6279 max = fold_convert (TREE_TYPE (uop), max);
6281 tree lhs = fold_build2 (MINUS_EXPR, TREE_TYPE (uop), uop, min);
6282 tree rhs = int_const_binop (MINUS_EXPR, max, min);
6283 register_new_assert_for (op, lhs, GT_EXPR, rhs,
6284 NULL, default_edge, bsi);
6287 if (--insertion_limit == 0)
6288 break;
6293 /* Traverse all the statements in block BB looking for statements that
6294 may generate useful assertions for the SSA names in their operand.
6295 If a statement produces a useful assertion A for name N_i, then the
6296 list of assertions already generated for N_i is scanned to
6297 determine if A is actually needed.
6299 If N_i already had the assertion A at a location dominating the
6300 current location, then nothing needs to be done. Otherwise, the
6301 new location for A is recorded instead.
6303 1- For every statement S in BB, all the variables used by S are
6304 added to bitmap FOUND_IN_SUBGRAPH.
6306 2- If statement S uses an operand N in a way that exposes a known
6307 value range for N, then if N was not already generated by an
6308 ASSERT_EXPR, create a new assert location for N. For instance,
6309 if N is a pointer and the statement dereferences it, we can
6310 assume that N is not NULL.
6312 3- COND_EXPRs are a special case of #2. We can derive range
6313 information from the predicate but need to insert different
6314 ASSERT_EXPRs for each of the sub-graphs rooted at the
6315 conditional block. If the last statement of BB is a conditional
6316 expression of the form 'X op Y', then
6318 a) Remove X and Y from the set FOUND_IN_SUBGRAPH.
6320 b) If the conditional is the only entry point to the sub-graph
6321 corresponding to the THEN_CLAUSE, recurse into it. On
6322 return, if X and/or Y are marked in FOUND_IN_SUBGRAPH, then
6323 an ASSERT_EXPR is added for the corresponding variable.
6325 c) Repeat step (b) on the ELSE_CLAUSE.
6327 d) Mark X and Y in FOUND_IN_SUBGRAPH.
6329 For instance,
6331 if (a == 9)
6332 b = a;
6333 else
6334 b = c + 1;
6336 In this case, an assertion on the THEN clause is useful to
6337 determine that 'a' is always 9 on that edge. However, an assertion
6338 on the ELSE clause would be unnecessary.
6340 4- If BB does not end in a conditional expression, then we recurse
6341 into BB's dominator children.
6343 At the end of the recursive traversal, every SSA name will have a
6344 list of locations where ASSERT_EXPRs should be added. When a new
6345 location for name N is found, it is registered by calling
6346 register_new_assert_for. That function keeps track of all the
6347 registered assertions to prevent adding unnecessary assertions.
6348 For instance, if a pointer P_4 is dereferenced more than once in a
6349 dominator tree, only the location dominating all the dereference of
6350 P_4 will receive an ASSERT_EXPR. */
6352 static void
6353 find_assert_locations_1 (basic_block bb, sbitmap live)
6355 gimple *last;
6357 last = last_stmt (bb);
6359 /* If BB's last statement is a conditional statement involving integer
6360 operands, determine if we need to add ASSERT_EXPRs. */
6361 if (last
6362 && gimple_code (last) == GIMPLE_COND
6363 && !fp_predicate (last)
6364 && !ZERO_SSA_OPERANDS (last, SSA_OP_USE))
6365 find_conditional_asserts (bb, as_a <gcond *> (last));
6367 /* If BB's last statement is a switch statement involving integer
6368 operands, determine if we need to add ASSERT_EXPRs. */
6369 if (last
6370 && gimple_code (last) == GIMPLE_SWITCH
6371 && !ZERO_SSA_OPERANDS (last, SSA_OP_USE))
6372 find_switch_asserts (bb, as_a <gswitch *> (last));
6374 /* Traverse all the statements in BB marking used names and looking
6375 for statements that may infer assertions for their used operands. */
6376 for (gimple_stmt_iterator si = gsi_last_bb (bb); !gsi_end_p (si);
6377 gsi_prev (&si))
6379 gimple *stmt;
6380 tree op;
6381 ssa_op_iter i;
6383 stmt = gsi_stmt (si);
6385 if (is_gimple_debug (stmt))
6386 continue;
6388 /* See if we can derive an assertion for any of STMT's operands. */
6389 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_USE)
6391 tree value;
6392 enum tree_code comp_code;
6394 /* If op is not live beyond this stmt, do not bother to insert
6395 asserts for it. */
6396 if (!bitmap_bit_p (live, SSA_NAME_VERSION (op)))
6397 continue;
6399 /* If OP is used in such a way that we can infer a value
6400 range for it, and we don't find a previous assertion for
6401 it, create a new assertion location node for OP. */
6402 if (infer_value_range (stmt, op, &comp_code, &value))
6404 /* If we are able to infer a nonzero value range for OP,
6405 then walk backwards through the use-def chain to see if OP
6406 was set via a typecast.
6408 If so, then we can also infer a nonzero value range
6409 for the operand of the NOP_EXPR. */
6410 if (comp_code == NE_EXPR && integer_zerop (value))
6412 tree t = op;
6413 gimple *def_stmt = SSA_NAME_DEF_STMT (t);
6415 while (is_gimple_assign (def_stmt)
6416 && CONVERT_EXPR_CODE_P
6417 (gimple_assign_rhs_code (def_stmt))
6418 && TREE_CODE
6419 (gimple_assign_rhs1 (def_stmt)) == SSA_NAME
6420 && POINTER_TYPE_P
6421 (TREE_TYPE (gimple_assign_rhs1 (def_stmt))))
6423 t = gimple_assign_rhs1 (def_stmt);
6424 def_stmt = SSA_NAME_DEF_STMT (t);
6426 /* Note we want to register the assert for the
6427 operand of the NOP_EXPR after SI, not after the
6428 conversion. */
6429 if (bitmap_bit_p (live, SSA_NAME_VERSION (t)))
6430 register_new_assert_for (t, t, comp_code, value,
6431 bb, NULL, si);
6435 register_new_assert_for (op, op, comp_code, value, bb, NULL, si);
6439 /* Update live. */
6440 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_USE)
6441 bitmap_set_bit (live, SSA_NAME_VERSION (op));
6442 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_DEF)
6443 bitmap_clear_bit (live, SSA_NAME_VERSION (op));
6446 /* Traverse all PHI nodes in BB, updating live. */
6447 for (gphi_iterator si = gsi_start_phis (bb); !gsi_end_p (si);
6448 gsi_next (&si))
6450 use_operand_p arg_p;
6451 ssa_op_iter i;
6452 gphi *phi = si.phi ();
6453 tree res = gimple_phi_result (phi);
6455 if (virtual_operand_p (res))
6456 continue;
6458 FOR_EACH_PHI_ARG (arg_p, phi, i, SSA_OP_USE)
6460 tree arg = USE_FROM_PTR (arg_p);
6461 if (TREE_CODE (arg) == SSA_NAME)
6462 bitmap_set_bit (live, SSA_NAME_VERSION (arg));
6465 bitmap_clear_bit (live, SSA_NAME_VERSION (res));
6469 /* Do an RPO walk over the function computing SSA name liveness
6470 on-the-fly and deciding on assert expressions to insert. */
6472 static void
6473 find_assert_locations (void)
6475 int *rpo = XNEWVEC (int, last_basic_block_for_fn (cfun));
6476 int *bb_rpo = XNEWVEC (int, last_basic_block_for_fn (cfun));
6477 int *last_rpo = XCNEWVEC (int, last_basic_block_for_fn (cfun));
6478 int rpo_cnt, i;
6480 live = XCNEWVEC (sbitmap, last_basic_block_for_fn (cfun));
6481 rpo_cnt = pre_and_rev_post_order_compute (NULL, rpo, false);
6482 for (i = 0; i < rpo_cnt; ++i)
6483 bb_rpo[rpo[i]] = i;
6485 /* Pre-seed loop latch liveness from loop header PHI nodes. Due to
6486 the order we compute liveness and insert asserts we otherwise
6487 fail to insert asserts into the loop latch. */
6488 loop_p loop;
6489 FOR_EACH_LOOP (loop, 0)
6491 i = loop->latch->index;
6492 unsigned int j = single_succ_edge (loop->latch)->dest_idx;
6493 for (gphi_iterator gsi = gsi_start_phis (loop->header);
6494 !gsi_end_p (gsi); gsi_next (&gsi))
6496 gphi *phi = gsi.phi ();
6497 if (virtual_operand_p (gimple_phi_result (phi)))
6498 continue;
6499 tree arg = gimple_phi_arg_def (phi, j);
6500 if (TREE_CODE (arg) == SSA_NAME)
6502 if (live[i] == NULL)
6504 live[i] = sbitmap_alloc (num_ssa_names);
6505 bitmap_clear (live[i]);
6507 bitmap_set_bit (live[i], SSA_NAME_VERSION (arg));
6512 for (i = rpo_cnt - 1; i >= 0; --i)
6514 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, rpo[i]);
6515 edge e;
6516 edge_iterator ei;
6518 if (!live[rpo[i]])
6520 live[rpo[i]] = sbitmap_alloc (num_ssa_names);
6521 bitmap_clear (live[rpo[i]]);
6524 /* Process BB and update the live information with uses in
6525 this block. */
6526 find_assert_locations_1 (bb, live[rpo[i]]);
6528 /* Merge liveness into the predecessor blocks and free it. */
6529 if (!bitmap_empty_p (live[rpo[i]]))
6531 int pred_rpo = i;
6532 FOR_EACH_EDGE (e, ei, bb->preds)
6534 int pred = e->src->index;
6535 if ((e->flags & EDGE_DFS_BACK) || pred == ENTRY_BLOCK)
6536 continue;
6538 if (!live[pred])
6540 live[pred] = sbitmap_alloc (num_ssa_names);
6541 bitmap_clear (live[pred]);
6543 bitmap_ior (live[pred], live[pred], live[rpo[i]]);
6545 if (bb_rpo[pred] < pred_rpo)
6546 pred_rpo = bb_rpo[pred];
6549 /* Record the RPO number of the last visited block that needs
6550 live information from this block. */
6551 last_rpo[rpo[i]] = pred_rpo;
6553 else
6555 sbitmap_free (live[rpo[i]]);
6556 live[rpo[i]] = NULL;
6559 /* We can free all successors live bitmaps if all their
6560 predecessors have been visited already. */
6561 FOR_EACH_EDGE (e, ei, bb->succs)
6562 if (last_rpo[e->dest->index] == i
6563 && live[e->dest->index])
6565 sbitmap_free (live[e->dest->index]);
6566 live[e->dest->index] = NULL;
6570 XDELETEVEC (rpo);
6571 XDELETEVEC (bb_rpo);
6572 XDELETEVEC (last_rpo);
6573 for (i = 0; i < last_basic_block_for_fn (cfun); ++i)
6574 if (live[i])
6575 sbitmap_free (live[i]);
6576 XDELETEVEC (live);
6579 /* Create an ASSERT_EXPR for NAME and insert it in the location
6580 indicated by LOC. Return true if we made any edge insertions. */
6582 static bool
6583 process_assert_insertions_for (tree name, assert_locus *loc)
6585 /* Build the comparison expression NAME_i COMP_CODE VAL. */
6586 gimple *stmt;
6587 tree cond;
6588 gimple *assert_stmt;
6589 edge_iterator ei;
6590 edge e;
6592 /* If we have X <=> X do not insert an assert expr for that. */
6593 if (loc->expr == loc->val)
6594 return false;
6596 cond = build2 (loc->comp_code, boolean_type_node, loc->expr, loc->val);
6597 assert_stmt = build_assert_expr_for (cond, name);
6598 if (loc->e)
6600 /* We have been asked to insert the assertion on an edge. This
6601 is used only by COND_EXPR and SWITCH_EXPR assertions. */
6602 gcc_checking_assert (gimple_code (gsi_stmt (loc->si)) == GIMPLE_COND
6603 || (gimple_code (gsi_stmt (loc->si))
6604 == GIMPLE_SWITCH));
6606 gsi_insert_on_edge (loc->e, assert_stmt);
6607 return true;
6610 /* If the stmt iterator points at the end then this is an insertion
6611 at the beginning of a block. */
6612 if (gsi_end_p (loc->si))
6614 gimple_stmt_iterator si = gsi_after_labels (loc->bb);
6615 gsi_insert_before (&si, assert_stmt, GSI_SAME_STMT);
6616 return false;
6619 /* Otherwise, we can insert right after LOC->SI iff the
6620 statement must not be the last statement in the block. */
6621 stmt = gsi_stmt (loc->si);
6622 if (!stmt_ends_bb_p (stmt))
6624 gsi_insert_after (&loc->si, assert_stmt, GSI_SAME_STMT);
6625 return false;
6628 /* If STMT must be the last statement in BB, we can only insert new
6629 assertions on the non-abnormal edge out of BB. Note that since
6630 STMT is not control flow, there may only be one non-abnormal/eh edge
6631 out of BB. */
6632 FOR_EACH_EDGE (e, ei, loc->bb->succs)
6633 if (!(e->flags & (EDGE_ABNORMAL|EDGE_EH)))
6635 gsi_insert_on_edge (e, assert_stmt);
6636 return true;
6639 gcc_unreachable ();
6642 /* Qsort helper for sorting assert locations. */
6644 static int
6645 compare_assert_loc (const void *pa, const void *pb)
6647 assert_locus * const a = *(assert_locus * const *)pa;
6648 assert_locus * const b = *(assert_locus * const *)pb;
6649 if (! a->e && b->e)
6650 return 1;
6651 else if (a->e && ! b->e)
6652 return -1;
6654 /* Sort after destination index. */
6655 if (! a->e && ! b->e)
6657 else if (a->e->dest->index > b->e->dest->index)
6658 return 1;
6659 else if (a->e->dest->index < b->e->dest->index)
6660 return -1;
6662 /* Sort after comp_code. */
6663 if (a->comp_code > b->comp_code)
6664 return 1;
6665 else if (a->comp_code < b->comp_code)
6666 return -1;
6668 /* Break the tie using hashing and source/bb index. */
6669 hashval_t ha = iterative_hash_expr (a->expr, iterative_hash_expr (a->val, 0));
6670 hashval_t hb = iterative_hash_expr (b->expr, iterative_hash_expr (b->val, 0));
6671 if (ha == hb)
6672 return (a->e && b->e
6673 ? a->e->src->index - b->e->src->index
6674 : a->bb->index - b->bb->index);
6675 return ha - hb;
6678 /* Process all the insertions registered for every name N_i registered
6679 in NEED_ASSERT_FOR. The list of assertions to be inserted are
6680 found in ASSERTS_FOR[i]. */
6682 static void
6683 process_assert_insertions (void)
6685 unsigned i;
6686 bitmap_iterator bi;
6687 bool update_edges_p = false;
6688 int num_asserts = 0;
6690 if (dump_file && (dump_flags & TDF_DETAILS))
6691 dump_all_asserts (dump_file);
6693 EXECUTE_IF_SET_IN_BITMAP (need_assert_for, 0, i, bi)
6695 assert_locus *loc = asserts_for[i];
6696 gcc_assert (loc);
6698 auto_vec<assert_locus *, 16> asserts;
6699 for (; loc; loc = loc->next)
6700 asserts.safe_push (loc);
6701 asserts.qsort (compare_assert_loc);
6703 /* Push down common asserts to successors and remove redundant ones. */
6704 unsigned ecnt = 0;
6705 assert_locus *common = NULL;
6706 unsigned commonj = 0;
6707 for (unsigned j = 0; j < asserts.length (); ++j)
6709 loc = asserts[j];
6710 if (! loc->e)
6711 common = NULL;
6712 else if (! common
6713 || loc->e->dest != common->e->dest
6714 || loc->comp_code != common->comp_code
6715 || ! operand_equal_p (loc->val, common->val, 0)
6716 || ! operand_equal_p (loc->expr, common->expr, 0))
6718 commonj = j;
6719 common = loc;
6720 ecnt = 1;
6722 else if (loc->e == asserts[j-1]->e)
6724 /* Remove duplicate asserts. */
6725 if (commonj == j - 1)
6727 commonj = j;
6728 common = loc;
6730 free (asserts[j-1]);
6731 asserts[j-1] = NULL;
6733 else
6735 ecnt++;
6736 if (EDGE_COUNT (common->e->dest->preds) == ecnt)
6738 /* We have the same assertion on all incoming edges of a BB.
6739 Insert it at the beginning of that block. */
6740 loc->bb = loc->e->dest;
6741 loc->e = NULL;
6742 loc->si = gsi_none ();
6743 common = NULL;
6744 /* Clear asserts commoned. */
6745 for (; commonj != j; ++commonj)
6746 if (asserts[commonj])
6748 free (asserts[commonj]);
6749 asserts[commonj] = NULL;
6755 for (unsigned j = 0; j < asserts.length (); ++j)
6757 loc = asserts[j];
6758 if (! loc)
6759 continue;
6760 update_edges_p |= process_assert_insertions_for (ssa_name (i), loc);
6761 num_asserts++;
6762 free (loc);
6766 if (update_edges_p)
6767 gsi_commit_edge_inserts ();
6769 statistics_counter_event (cfun, "Number of ASSERT_EXPR expressions inserted",
6770 num_asserts);
6774 /* Traverse the flowgraph looking for conditional jumps to insert range
6775 expressions. These range expressions are meant to provide information
6776 to optimizations that need to reason in terms of value ranges. They
6777 will not be expanded into RTL. For instance, given:
6779 x = ...
6780 y = ...
6781 if (x < y)
6782 y = x - 2;
6783 else
6784 x = y + 3;
6786 this pass will transform the code into:
6788 x = ...
6789 y = ...
6790 if (x < y)
6792 x = ASSERT_EXPR <x, x < y>
6793 y = x - 2
6795 else
6797 y = ASSERT_EXPR <y, x >= y>
6798 x = y + 3
6801 The idea is that once copy and constant propagation have run, other
6802 optimizations will be able to determine what ranges of values can 'x'
6803 take in different paths of the code, simply by checking the reaching
6804 definition of 'x'. */
6806 static void
6807 insert_range_assertions (void)
6809 need_assert_for = BITMAP_ALLOC (NULL);
6810 asserts_for = XCNEWVEC (assert_locus *, num_ssa_names);
6812 calculate_dominance_info (CDI_DOMINATORS);
6814 find_assert_locations ();
6815 if (!bitmap_empty_p (need_assert_for))
6817 process_assert_insertions ();
6818 update_ssa (TODO_update_ssa_no_phi);
6821 if (dump_file && (dump_flags & TDF_DETAILS))
6823 fprintf (dump_file, "\nSSA form after inserting ASSERT_EXPRs\n");
6824 dump_function_to_file (current_function_decl, dump_file, dump_flags);
6827 free (asserts_for);
6828 BITMAP_FREE (need_assert_for);
6831 /* Checks one ARRAY_REF in REF, located at LOCUS. Ignores flexible arrays
6832 and "struct" hacks. If VRP can determine that the
6833 array subscript is a constant, check if it is outside valid
6834 range. If the array subscript is a RANGE, warn if it is
6835 non-overlapping with valid range.
6836 IGNORE_OFF_BY_ONE is true if the ARRAY_REF is inside a ADDR_EXPR. */
6838 static void
6839 check_array_ref (location_t location, tree ref, bool ignore_off_by_one)
6841 value_range *vr = NULL;
6842 tree low_sub, up_sub;
6843 tree low_bound, up_bound, up_bound_p1;
6845 if (TREE_NO_WARNING (ref))
6846 return;
6848 low_sub = up_sub = TREE_OPERAND (ref, 1);
6849 up_bound = array_ref_up_bound (ref);
6851 /* Can not check flexible arrays. */
6852 if (!up_bound
6853 || TREE_CODE (up_bound) != INTEGER_CST)
6854 return;
6856 /* Accesses to trailing arrays via pointers may access storage
6857 beyond the types array bounds. */
6858 if (warn_array_bounds < 2
6859 && array_at_struct_end_p (ref))
6860 return;
6862 low_bound = array_ref_low_bound (ref);
6863 up_bound_p1 = int_const_binop (PLUS_EXPR, up_bound,
6864 build_int_cst (TREE_TYPE (up_bound), 1));
6866 /* Empty array. */
6867 if (tree_int_cst_equal (low_bound, up_bound_p1))
6869 warning_at (location, OPT_Warray_bounds,
6870 "array subscript is above array bounds");
6871 TREE_NO_WARNING (ref) = 1;
6874 if (TREE_CODE (low_sub) == SSA_NAME)
6876 vr = get_value_range (low_sub);
6877 if (vr->type == VR_RANGE || vr->type == VR_ANTI_RANGE)
6879 low_sub = vr->type == VR_RANGE ? vr->max : vr->min;
6880 up_sub = vr->type == VR_RANGE ? vr->min : vr->max;
6884 if (vr && vr->type == VR_ANTI_RANGE)
6886 if (TREE_CODE (up_sub) == INTEGER_CST
6887 && (ignore_off_by_one
6888 ? tree_int_cst_lt (up_bound, up_sub)
6889 : tree_int_cst_le (up_bound, up_sub))
6890 && TREE_CODE (low_sub) == INTEGER_CST
6891 && tree_int_cst_le (low_sub, low_bound))
6893 warning_at (location, OPT_Warray_bounds,
6894 "array subscript is outside array bounds");
6895 TREE_NO_WARNING (ref) = 1;
6898 else if (TREE_CODE (up_sub) == INTEGER_CST
6899 && (ignore_off_by_one
6900 ? !tree_int_cst_le (up_sub, up_bound_p1)
6901 : !tree_int_cst_le (up_sub, up_bound)))
6903 if (dump_file && (dump_flags & TDF_DETAILS))
6905 fprintf (dump_file, "Array bound warning for ");
6906 dump_generic_expr (MSG_NOTE, TDF_SLIM, ref);
6907 fprintf (dump_file, "\n");
6909 warning_at (location, OPT_Warray_bounds,
6910 "array subscript is above array bounds");
6911 TREE_NO_WARNING (ref) = 1;
6913 else if (TREE_CODE (low_sub) == INTEGER_CST
6914 && tree_int_cst_lt (low_sub, low_bound))
6916 if (dump_file && (dump_flags & TDF_DETAILS))
6918 fprintf (dump_file, "Array bound warning for ");
6919 dump_generic_expr (MSG_NOTE, TDF_SLIM, ref);
6920 fprintf (dump_file, "\n");
6922 warning_at (location, OPT_Warray_bounds,
6923 "array subscript is below array bounds");
6924 TREE_NO_WARNING (ref) = 1;
6928 /* Searches if the expr T, located at LOCATION computes
6929 address of an ARRAY_REF, and call check_array_ref on it. */
6931 static void
6932 search_for_addr_array (tree t, location_t location)
6934 /* Check each ARRAY_REFs in the reference chain. */
6937 if (TREE_CODE (t) == ARRAY_REF)
6938 check_array_ref (location, t, true /*ignore_off_by_one*/);
6940 t = TREE_OPERAND (t, 0);
6942 while (handled_component_p (t));
6944 if (TREE_CODE (t) == MEM_REF
6945 && TREE_CODE (TREE_OPERAND (t, 0)) == ADDR_EXPR
6946 && !TREE_NO_WARNING (t))
6948 tree tem = TREE_OPERAND (TREE_OPERAND (t, 0), 0);
6949 tree low_bound, up_bound, el_sz;
6950 offset_int idx;
6951 if (TREE_CODE (TREE_TYPE (tem)) != ARRAY_TYPE
6952 || TREE_CODE (TREE_TYPE (TREE_TYPE (tem))) == ARRAY_TYPE
6953 || !TYPE_DOMAIN (TREE_TYPE (tem)))
6954 return;
6956 low_bound = TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (tem)));
6957 up_bound = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (tem)));
6958 el_sz = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (tem)));
6959 if (!low_bound
6960 || TREE_CODE (low_bound) != INTEGER_CST
6961 || !up_bound
6962 || TREE_CODE (up_bound) != INTEGER_CST
6963 || !el_sz
6964 || TREE_CODE (el_sz) != INTEGER_CST)
6965 return;
6967 idx = mem_ref_offset (t);
6968 idx = wi::sdiv_trunc (idx, wi::to_offset (el_sz));
6969 if (idx < 0)
6971 if (dump_file && (dump_flags & TDF_DETAILS))
6973 fprintf (dump_file, "Array bound warning for ");
6974 dump_generic_expr (MSG_NOTE, TDF_SLIM, t);
6975 fprintf (dump_file, "\n");
6977 warning_at (location, OPT_Warray_bounds,
6978 "array subscript is below array bounds");
6979 TREE_NO_WARNING (t) = 1;
6981 else if (idx > (wi::to_offset (up_bound)
6982 - wi::to_offset (low_bound) + 1))
6984 if (dump_file && (dump_flags & TDF_DETAILS))
6986 fprintf (dump_file, "Array bound warning for ");
6987 dump_generic_expr (MSG_NOTE, TDF_SLIM, t);
6988 fprintf (dump_file, "\n");
6990 warning_at (location, OPT_Warray_bounds,
6991 "array subscript is above array bounds");
6992 TREE_NO_WARNING (t) = 1;
6997 /* walk_tree() callback that checks if *TP is
6998 an ARRAY_REF inside an ADDR_EXPR (in which an array
6999 subscript one outside the valid range is allowed). Call
7000 check_array_ref for each ARRAY_REF found. The location is
7001 passed in DATA. */
7003 static tree
7004 check_array_bounds (tree *tp, int *walk_subtree, void *data)
7006 tree t = *tp;
7007 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
7008 location_t location;
7010 if (EXPR_HAS_LOCATION (t))
7011 location = EXPR_LOCATION (t);
7012 else
7014 location_t *locp = (location_t *) wi->info;
7015 location = *locp;
7018 *walk_subtree = TRUE;
7020 if (TREE_CODE (t) == ARRAY_REF)
7021 check_array_ref (location, t, false /*ignore_off_by_one*/);
7023 else if (TREE_CODE (t) == ADDR_EXPR)
7025 search_for_addr_array (t, location);
7026 *walk_subtree = FALSE;
7029 return NULL_TREE;
7032 /* Walk over all statements of all reachable BBs and call check_array_bounds
7033 on them. */
7035 static void
7036 check_all_array_refs (void)
7038 basic_block bb;
7039 gimple_stmt_iterator si;
7041 FOR_EACH_BB_FN (bb, cfun)
7043 edge_iterator ei;
7044 edge e;
7045 bool executable = false;
7047 /* Skip blocks that were found to be unreachable. */
7048 FOR_EACH_EDGE (e, ei, bb->preds)
7049 executable |= !!(e->flags & EDGE_EXECUTABLE);
7050 if (!executable)
7051 continue;
7053 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
7055 gimple *stmt = gsi_stmt (si);
7056 struct walk_stmt_info wi;
7057 if (!gimple_has_location (stmt)
7058 || is_gimple_debug (stmt))
7059 continue;
7061 memset (&wi, 0, sizeof (wi));
7063 location_t loc = gimple_location (stmt);
7064 wi.info = &loc;
7066 walk_gimple_op (gsi_stmt (si),
7067 check_array_bounds,
7068 &wi);
7073 /* Return true if all imm uses of VAR are either in STMT, or
7074 feed (optionally through a chain of single imm uses) GIMPLE_COND
7075 in basic block COND_BB. */
7077 static bool
7078 all_imm_uses_in_stmt_or_feed_cond (tree var, gimple *stmt, basic_block cond_bb)
7080 use_operand_p use_p, use2_p;
7081 imm_use_iterator iter;
7083 FOR_EACH_IMM_USE_FAST (use_p, iter, var)
7084 if (USE_STMT (use_p) != stmt)
7086 gimple *use_stmt = USE_STMT (use_p), *use_stmt2;
7087 if (is_gimple_debug (use_stmt))
7088 continue;
7089 while (is_gimple_assign (use_stmt)
7090 && TREE_CODE (gimple_assign_lhs (use_stmt)) == SSA_NAME
7091 && single_imm_use (gimple_assign_lhs (use_stmt),
7092 &use2_p, &use_stmt2))
7093 use_stmt = use_stmt2;
7094 if (gimple_code (use_stmt) != GIMPLE_COND
7095 || gimple_bb (use_stmt) != cond_bb)
7096 return false;
7098 return true;
7101 /* Handle
7102 _4 = x_3 & 31;
7103 if (_4 != 0)
7104 goto <bb 6>;
7105 else
7106 goto <bb 7>;
7107 <bb 6>:
7108 __builtin_unreachable ();
7109 <bb 7>:
7110 x_5 = ASSERT_EXPR <x_3, ...>;
7111 If x_3 has no other immediate uses (checked by caller),
7112 var is the x_3 var from ASSERT_EXPR, we can clear low 5 bits
7113 from the non-zero bitmask. */
7115 static void
7116 maybe_set_nonzero_bits (basic_block bb, tree var)
7118 edge e = single_pred_edge (bb);
7119 basic_block cond_bb = e->src;
7120 gimple *stmt = last_stmt (cond_bb);
7121 tree cst;
7123 if (stmt == NULL
7124 || gimple_code (stmt) != GIMPLE_COND
7125 || gimple_cond_code (stmt) != ((e->flags & EDGE_TRUE_VALUE)
7126 ? EQ_EXPR : NE_EXPR)
7127 || TREE_CODE (gimple_cond_lhs (stmt)) != SSA_NAME
7128 || !integer_zerop (gimple_cond_rhs (stmt)))
7129 return;
7131 stmt = SSA_NAME_DEF_STMT (gimple_cond_lhs (stmt));
7132 if (!is_gimple_assign (stmt)
7133 || gimple_assign_rhs_code (stmt) != BIT_AND_EXPR
7134 || TREE_CODE (gimple_assign_rhs2 (stmt)) != INTEGER_CST)
7135 return;
7136 if (gimple_assign_rhs1 (stmt) != var)
7138 gimple *stmt2;
7140 if (TREE_CODE (gimple_assign_rhs1 (stmt)) != SSA_NAME)
7141 return;
7142 stmt2 = SSA_NAME_DEF_STMT (gimple_assign_rhs1 (stmt));
7143 if (!gimple_assign_cast_p (stmt2)
7144 || gimple_assign_rhs1 (stmt2) != var
7145 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (stmt2))
7146 || (TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (stmt)))
7147 != TYPE_PRECISION (TREE_TYPE (var))))
7148 return;
7150 cst = gimple_assign_rhs2 (stmt);
7151 set_nonzero_bits (var, wi::bit_and_not (get_nonzero_bits (var), cst));
7154 /* Convert range assertion expressions into the implied copies and
7155 copy propagate away the copies. Doing the trivial copy propagation
7156 here avoids the need to run the full copy propagation pass after
7157 VRP.
7159 FIXME, this will eventually lead to copy propagation removing the
7160 names that had useful range information attached to them. For
7161 instance, if we had the assertion N_i = ASSERT_EXPR <N_j, N_j > 3>,
7162 then N_i will have the range [3, +INF].
7164 However, by converting the assertion into the implied copy
7165 operation N_i = N_j, we will then copy-propagate N_j into the uses
7166 of N_i and lose the range information. We may want to hold on to
7167 ASSERT_EXPRs a little while longer as the ranges could be used in
7168 things like jump threading.
7170 The problem with keeping ASSERT_EXPRs around is that passes after
7171 VRP need to handle them appropriately.
7173 Another approach would be to make the range information a first
7174 class property of the SSA_NAME so that it can be queried from
7175 any pass. This is made somewhat more complex by the need for
7176 multiple ranges to be associated with one SSA_NAME. */
7178 static void
7179 remove_range_assertions (void)
7181 basic_block bb;
7182 gimple_stmt_iterator si;
7183 /* 1 if looking at ASSERT_EXPRs immediately at the beginning of
7184 a basic block preceeded by GIMPLE_COND branching to it and
7185 __builtin_trap, -1 if not yet checked, 0 otherwise. */
7186 int is_unreachable;
7188 /* Note that the BSI iterator bump happens at the bottom of the
7189 loop and no bump is necessary if we're removing the statement
7190 referenced by the current BSI. */
7191 FOR_EACH_BB_FN (bb, cfun)
7192 for (si = gsi_after_labels (bb), is_unreachable = -1; !gsi_end_p (si);)
7194 gimple *stmt = gsi_stmt (si);
7196 if (is_gimple_assign (stmt)
7197 && gimple_assign_rhs_code (stmt) == ASSERT_EXPR)
7199 tree lhs = gimple_assign_lhs (stmt);
7200 tree rhs = gimple_assign_rhs1 (stmt);
7201 tree var;
7203 var = ASSERT_EXPR_VAR (rhs);
7205 if (TREE_CODE (var) == SSA_NAME
7206 && !POINTER_TYPE_P (TREE_TYPE (lhs))
7207 && SSA_NAME_RANGE_INFO (lhs))
7209 if (is_unreachable == -1)
7211 is_unreachable = 0;
7212 if (single_pred_p (bb)
7213 && assert_unreachable_fallthru_edge_p
7214 (single_pred_edge (bb)))
7215 is_unreachable = 1;
7217 /* Handle
7218 if (x_7 >= 10 && x_7 < 20)
7219 __builtin_unreachable ();
7220 x_8 = ASSERT_EXPR <x_7, ...>;
7221 if the only uses of x_7 are in the ASSERT_EXPR and
7222 in the condition. In that case, we can copy the
7223 range info from x_8 computed in this pass also
7224 for x_7. */
7225 if (is_unreachable
7226 && all_imm_uses_in_stmt_or_feed_cond (var, stmt,
7227 single_pred (bb)))
7229 set_range_info (var, SSA_NAME_RANGE_TYPE (lhs),
7230 SSA_NAME_RANGE_INFO (lhs)->get_min (),
7231 SSA_NAME_RANGE_INFO (lhs)->get_max ());
7232 maybe_set_nonzero_bits (bb, var);
7236 /* Propagate the RHS into every use of the LHS. For SSA names
7237 also propagate abnormals as it merely restores the original
7238 IL in this case (an replace_uses_by would assert). */
7239 if (TREE_CODE (var) == SSA_NAME)
7241 imm_use_iterator iter;
7242 use_operand_p use_p;
7243 gimple *use_stmt;
7244 FOR_EACH_IMM_USE_STMT (use_stmt, iter, lhs)
7245 FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
7246 SET_USE (use_p, var);
7248 else
7249 replace_uses_by (lhs, var);
7251 /* And finally, remove the copy, it is not needed. */
7252 gsi_remove (&si, true);
7253 release_defs (stmt);
7255 else
7257 if (!is_gimple_debug (gsi_stmt (si)))
7258 is_unreachable = 0;
7259 gsi_next (&si);
7265 /* Return true if STMT is interesting for VRP. */
7267 static bool
7268 stmt_interesting_for_vrp (gimple *stmt)
7270 if (gimple_code (stmt) == GIMPLE_PHI)
7272 tree res = gimple_phi_result (stmt);
7273 return (!virtual_operand_p (res)
7274 && (INTEGRAL_TYPE_P (TREE_TYPE (res))
7275 || POINTER_TYPE_P (TREE_TYPE (res))));
7277 else if (is_gimple_assign (stmt) || is_gimple_call (stmt))
7279 tree lhs = gimple_get_lhs (stmt);
7281 /* In general, assignments with virtual operands are not useful
7282 for deriving ranges, with the obvious exception of calls to
7283 builtin functions. */
7284 if (lhs && TREE_CODE (lhs) == SSA_NAME
7285 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs))
7286 || POINTER_TYPE_P (TREE_TYPE (lhs)))
7287 && (is_gimple_call (stmt)
7288 || !gimple_vuse (stmt)))
7289 return true;
7290 else if (is_gimple_call (stmt) && gimple_call_internal_p (stmt))
7291 switch (gimple_call_internal_fn (stmt))
7293 case IFN_ADD_OVERFLOW:
7294 case IFN_SUB_OVERFLOW:
7295 case IFN_MUL_OVERFLOW:
7296 case IFN_ATOMIC_COMPARE_EXCHANGE:
7297 /* These internal calls return _Complex integer type,
7298 but are interesting to VRP nevertheless. */
7299 if (lhs && TREE_CODE (lhs) == SSA_NAME)
7300 return true;
7301 break;
7302 default:
7303 break;
7306 else if (gimple_code (stmt) == GIMPLE_COND
7307 || gimple_code (stmt) == GIMPLE_SWITCH)
7308 return true;
7310 return false;
7313 /* Initialize VRP lattice. */
7315 static void
7316 vrp_initialize_lattice ()
7318 values_propagated = false;
7319 num_vr_values = num_ssa_names;
7320 vr_value = XCNEWVEC (value_range *, num_vr_values);
7321 vr_phi_edge_counts = XCNEWVEC (int, num_ssa_names);
7322 bitmap_obstack_initialize (&vrp_equiv_obstack);
7325 /* Initialization required by ssa_propagate engine. */
7327 static void
7328 vrp_initialize ()
7330 basic_block bb;
7332 FOR_EACH_BB_FN (bb, cfun)
7334 for (gphi_iterator si = gsi_start_phis (bb); !gsi_end_p (si);
7335 gsi_next (&si))
7337 gphi *phi = si.phi ();
7338 if (!stmt_interesting_for_vrp (phi))
7340 tree lhs = PHI_RESULT (phi);
7341 set_value_range_to_varying (get_value_range (lhs));
7342 prop_set_simulate_again (phi, false);
7344 else
7345 prop_set_simulate_again (phi, true);
7348 for (gimple_stmt_iterator si = gsi_start_bb (bb); !gsi_end_p (si);
7349 gsi_next (&si))
7351 gimple *stmt = gsi_stmt (si);
7353 /* If the statement is a control insn, then we do not
7354 want to avoid simulating the statement once. Failure
7355 to do so means that those edges will never get added. */
7356 if (stmt_ends_bb_p (stmt))
7357 prop_set_simulate_again (stmt, true);
7358 else if (!stmt_interesting_for_vrp (stmt))
7360 set_defs_to_varying (stmt);
7361 prop_set_simulate_again (stmt, false);
7363 else
7364 prop_set_simulate_again (stmt, true);
7369 /* Return the singleton value-range for NAME or NAME. */
7371 static inline tree
7372 vrp_valueize (tree name)
7374 if (TREE_CODE (name) == SSA_NAME)
7376 value_range *vr = get_value_range (name);
7377 if (vr->type == VR_RANGE
7378 && (TREE_CODE (vr->min) == SSA_NAME
7379 || is_gimple_min_invariant (vr->min))
7380 && vrp_operand_equal_p (vr->min, vr->max))
7381 return vr->min;
7383 return name;
7386 /* Return the singleton value-range for NAME if that is a constant
7387 but signal to not follow SSA edges. */
7389 static inline tree
7390 vrp_valueize_1 (tree name)
7392 if (TREE_CODE (name) == SSA_NAME)
7394 /* If the definition may be simulated again we cannot follow
7395 this SSA edge as the SSA propagator does not necessarily
7396 re-visit the use. */
7397 gimple *def_stmt = SSA_NAME_DEF_STMT (name);
7398 if (!gimple_nop_p (def_stmt)
7399 && prop_simulate_again_p (def_stmt))
7400 return NULL_TREE;
7401 value_range *vr = get_value_range (name);
7402 if (range_int_cst_singleton_p (vr))
7403 return vr->min;
7405 return name;
7408 /* Visit assignment STMT. If it produces an interesting range, record
7409 the range in VR and set LHS to OUTPUT_P. */
7411 static void
7412 vrp_visit_assignment_or_call (gimple *stmt, tree *output_p, value_range *vr)
7414 tree lhs;
7415 enum gimple_code code = gimple_code (stmt);
7416 lhs = gimple_get_lhs (stmt);
7417 *output_p = NULL_TREE;
7419 /* We only keep track of ranges in integral and pointer types. */
7420 if (TREE_CODE (lhs) == SSA_NAME
7421 && ((INTEGRAL_TYPE_P (TREE_TYPE (lhs))
7422 /* It is valid to have NULL MIN/MAX values on a type. See
7423 build_range_type. */
7424 && TYPE_MIN_VALUE (TREE_TYPE (lhs))
7425 && TYPE_MAX_VALUE (TREE_TYPE (lhs)))
7426 || POINTER_TYPE_P (TREE_TYPE (lhs))))
7428 *output_p = lhs;
7430 /* Try folding the statement to a constant first. */
7431 tree tem = gimple_fold_stmt_to_constant_1 (stmt, vrp_valueize,
7432 vrp_valueize_1);
7433 if (tem)
7435 if (TREE_CODE (tem) == SSA_NAME
7436 && (SSA_NAME_IS_DEFAULT_DEF (tem)
7437 || ! prop_simulate_again_p (SSA_NAME_DEF_STMT (tem))))
7439 extract_range_from_ssa_name (vr, tem);
7440 return;
7442 else if (is_gimple_min_invariant (tem))
7444 set_value_range_to_value (vr, tem, NULL);
7445 return;
7448 /* Then dispatch to value-range extracting functions. */
7449 if (code == GIMPLE_CALL)
7450 extract_range_basic (vr, stmt);
7451 else
7452 extract_range_from_assignment (vr, as_a <gassign *> (stmt));
7456 /* Helper that gets the value range of the SSA_NAME with version I
7457 or a symbolic range containing the SSA_NAME only if the value range
7458 is varying or undefined. */
7460 static inline value_range
7461 get_vr_for_comparison (int i)
7463 value_range vr = *get_value_range (ssa_name (i));
7465 /* If name N_i does not have a valid range, use N_i as its own
7466 range. This allows us to compare against names that may
7467 have N_i in their ranges. */
7468 if (vr.type == VR_VARYING || vr.type == VR_UNDEFINED)
7470 vr.type = VR_RANGE;
7471 vr.min = ssa_name (i);
7472 vr.max = ssa_name (i);
7475 return vr;
7478 /* Compare all the value ranges for names equivalent to VAR with VAL
7479 using comparison code COMP. Return the same value returned by
7480 compare_range_with_value, including the setting of
7481 *STRICT_OVERFLOW_P. */
7483 static tree
7484 compare_name_with_value (enum tree_code comp, tree var, tree val,
7485 bool *strict_overflow_p, bool use_equiv_p)
7487 bitmap_iterator bi;
7488 unsigned i;
7489 bitmap e;
7490 tree retval, t;
7491 int used_strict_overflow;
7492 bool sop;
7493 value_range equiv_vr;
7495 /* Get the set of equivalences for VAR. */
7496 e = get_value_range (var)->equiv;
7498 /* Start at -1. Set it to 0 if we do a comparison without relying
7499 on overflow, or 1 if all comparisons rely on overflow. */
7500 used_strict_overflow = -1;
7502 /* Compare vars' value range with val. */
7503 equiv_vr = get_vr_for_comparison (SSA_NAME_VERSION (var));
7504 sop = false;
7505 retval = compare_range_with_value (comp, &equiv_vr, val, &sop);
7506 if (retval)
7507 used_strict_overflow = sop ? 1 : 0;
7509 /* If the equiv set is empty we have done all work we need to do. */
7510 if (e == NULL)
7512 if (retval
7513 && used_strict_overflow > 0)
7514 *strict_overflow_p = true;
7515 return retval;
7518 EXECUTE_IF_SET_IN_BITMAP (e, 0, i, bi)
7520 tree name = ssa_name (i);
7521 if (! name)
7522 continue;
7524 if (! use_equiv_p
7525 && ! SSA_NAME_IS_DEFAULT_DEF (name)
7526 && prop_simulate_again_p (SSA_NAME_DEF_STMT (name)))
7527 continue;
7529 equiv_vr = get_vr_for_comparison (i);
7530 sop = false;
7531 t = compare_range_with_value (comp, &equiv_vr, val, &sop);
7532 if (t)
7534 /* If we get different answers from different members
7535 of the equivalence set this check must be in a dead
7536 code region. Folding it to a trap representation
7537 would be correct here. For now just return don't-know. */
7538 if (retval != NULL
7539 && t != retval)
7541 retval = NULL_TREE;
7542 break;
7544 retval = t;
7546 if (!sop)
7547 used_strict_overflow = 0;
7548 else if (used_strict_overflow < 0)
7549 used_strict_overflow = 1;
7553 if (retval
7554 && used_strict_overflow > 0)
7555 *strict_overflow_p = true;
7557 return retval;
7561 /* Given a comparison code COMP and names N1 and N2, compare all the
7562 ranges equivalent to N1 against all the ranges equivalent to N2
7563 to determine the value of N1 COMP N2. Return the same value
7564 returned by compare_ranges. Set *STRICT_OVERFLOW_P to indicate
7565 whether we relied on an overflow infinity in the comparison. */
7568 static tree
7569 compare_names (enum tree_code comp, tree n1, tree n2,
7570 bool *strict_overflow_p)
7572 tree t, retval;
7573 bitmap e1, e2;
7574 bitmap_iterator bi1, bi2;
7575 unsigned i1, i2;
7576 int used_strict_overflow;
7577 static bitmap_obstack *s_obstack = NULL;
7578 static bitmap s_e1 = NULL, s_e2 = NULL;
7580 /* Compare the ranges of every name equivalent to N1 against the
7581 ranges of every name equivalent to N2. */
7582 e1 = get_value_range (n1)->equiv;
7583 e2 = get_value_range (n2)->equiv;
7585 /* Use the fake bitmaps if e1 or e2 are not available. */
7586 if (s_obstack == NULL)
7588 s_obstack = XNEW (bitmap_obstack);
7589 bitmap_obstack_initialize (s_obstack);
7590 s_e1 = BITMAP_ALLOC (s_obstack);
7591 s_e2 = BITMAP_ALLOC (s_obstack);
7593 if (e1 == NULL)
7594 e1 = s_e1;
7595 if (e2 == NULL)
7596 e2 = s_e2;
7598 /* Add N1 and N2 to their own set of equivalences to avoid
7599 duplicating the body of the loop just to check N1 and N2
7600 ranges. */
7601 bitmap_set_bit (e1, SSA_NAME_VERSION (n1));
7602 bitmap_set_bit (e2, SSA_NAME_VERSION (n2));
7604 /* If the equivalence sets have a common intersection, then the two
7605 names can be compared without checking their ranges. */
7606 if (bitmap_intersect_p (e1, e2))
7608 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
7609 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
7611 return (comp == EQ_EXPR || comp == GE_EXPR || comp == LE_EXPR)
7612 ? boolean_true_node
7613 : boolean_false_node;
7616 /* Start at -1. Set it to 0 if we do a comparison without relying
7617 on overflow, or 1 if all comparisons rely on overflow. */
7618 used_strict_overflow = -1;
7620 /* Otherwise, compare all the equivalent ranges. First, add N1 and
7621 N2 to their own set of equivalences to avoid duplicating the body
7622 of the loop just to check N1 and N2 ranges. */
7623 EXECUTE_IF_SET_IN_BITMAP (e1, 0, i1, bi1)
7625 if (! ssa_name (i1))
7626 continue;
7628 value_range vr1 = get_vr_for_comparison (i1);
7630 t = retval = NULL_TREE;
7631 EXECUTE_IF_SET_IN_BITMAP (e2, 0, i2, bi2)
7633 if (! ssa_name (i2))
7634 continue;
7636 bool sop = false;
7638 value_range vr2 = get_vr_for_comparison (i2);
7640 t = compare_ranges (comp, &vr1, &vr2, &sop);
7641 if (t)
7643 /* If we get different answers from different members
7644 of the equivalence set this check must be in a dead
7645 code region. Folding it to a trap representation
7646 would be correct here. For now just return don't-know. */
7647 if (retval != NULL
7648 && t != retval)
7650 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
7651 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
7652 return NULL_TREE;
7654 retval = t;
7656 if (!sop)
7657 used_strict_overflow = 0;
7658 else if (used_strict_overflow < 0)
7659 used_strict_overflow = 1;
7663 if (retval)
7665 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
7666 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
7667 if (used_strict_overflow > 0)
7668 *strict_overflow_p = true;
7669 return retval;
7673 /* None of the equivalent ranges are useful in computing this
7674 comparison. */
7675 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
7676 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
7677 return NULL_TREE;
7680 /* Helper function for vrp_evaluate_conditional_warnv & other
7681 optimizers. */
7683 static tree
7684 vrp_evaluate_conditional_warnv_with_ops_using_ranges (enum tree_code code,
7685 tree op0, tree op1,
7686 bool * strict_overflow_p)
7688 value_range *vr0, *vr1;
7690 vr0 = (TREE_CODE (op0) == SSA_NAME) ? get_value_range (op0) : NULL;
7691 vr1 = (TREE_CODE (op1) == SSA_NAME) ? get_value_range (op1) : NULL;
7693 tree res = NULL_TREE;
7694 if (vr0 && vr1)
7695 res = compare_ranges (code, vr0, vr1, strict_overflow_p);
7696 if (!res && vr0)
7697 res = compare_range_with_value (code, vr0, op1, strict_overflow_p);
7698 if (!res && vr1)
7699 res = (compare_range_with_value
7700 (swap_tree_comparison (code), vr1, op0, strict_overflow_p));
7701 return res;
7704 /* Helper function for vrp_evaluate_conditional_warnv. */
7706 static tree
7707 vrp_evaluate_conditional_warnv_with_ops (enum tree_code code, tree op0,
7708 tree op1, bool use_equiv_p,
7709 bool *strict_overflow_p, bool *only_ranges)
7711 tree ret;
7712 if (only_ranges)
7713 *only_ranges = true;
7715 /* We only deal with integral and pointer types. */
7716 if (!INTEGRAL_TYPE_P (TREE_TYPE (op0))
7717 && !POINTER_TYPE_P (TREE_TYPE (op0)))
7718 return NULL_TREE;
7720 /* If OP0 CODE OP1 is an overflow comparison, if it can be expressed
7721 as a simple equality test, then prefer that over its current form
7722 for evaluation.
7724 An overflow test which collapses to an equality test can always be
7725 expressed as a comparison of one argument against zero. Overflow
7726 occurs when the chosen argument is zero and does not occur if the
7727 chosen argument is not zero. */
7728 tree x;
7729 if (overflow_comparison_p (code, op0, op1, use_equiv_p, &x))
7731 wide_int max = wi::max_value (TYPE_PRECISION (TREE_TYPE (op0)), UNSIGNED);
7732 /* B = A - 1; if (A < B) -> B = A - 1; if (A == 0)
7733 B = A - 1; if (A > B) -> B = A - 1; if (A != 0)
7734 B = A + 1; if (B < A) -> B = A + 1; if (B == 0)
7735 B = A + 1; if (B > A) -> B = A + 1; if (B != 0) */
7736 if (integer_zerop (x))
7738 op1 = x;
7739 code = (code == LT_EXPR || code == LE_EXPR) ? EQ_EXPR : NE_EXPR;
7741 /* B = A + 1; if (A > B) -> B = A + 1; if (B == 0)
7742 B = A + 1; if (A < B) -> B = A + 1; if (B != 0)
7743 B = A - 1; if (B > A) -> B = A - 1; if (A == 0)
7744 B = A - 1; if (B < A) -> B = A - 1; if (A != 0) */
7745 else if (wi::eq_p (x, max - 1))
7747 op0 = op1;
7748 op1 = wide_int_to_tree (TREE_TYPE (op0), 0);
7749 code = (code == GT_EXPR || code == GE_EXPR) ? EQ_EXPR : NE_EXPR;
7753 if ((ret = vrp_evaluate_conditional_warnv_with_ops_using_ranges
7754 (code, op0, op1, strict_overflow_p)))
7755 return ret;
7756 if (only_ranges)
7757 *only_ranges = false;
7758 /* Do not use compare_names during propagation, it's quadratic. */
7759 if (TREE_CODE (op0) == SSA_NAME && TREE_CODE (op1) == SSA_NAME
7760 && use_equiv_p)
7761 return compare_names (code, op0, op1, strict_overflow_p);
7762 else if (TREE_CODE (op0) == SSA_NAME)
7763 return compare_name_with_value (code, op0, op1,
7764 strict_overflow_p, use_equiv_p);
7765 else if (TREE_CODE (op1) == SSA_NAME)
7766 return compare_name_with_value (swap_tree_comparison (code), op1, op0,
7767 strict_overflow_p, use_equiv_p);
7768 return NULL_TREE;
7771 /* Given (CODE OP0 OP1) within STMT, try to simplify it based on value range
7772 information. Return NULL if the conditional can not be evaluated.
7773 The ranges of all the names equivalent with the operands in COND
7774 will be used when trying to compute the value. If the result is
7775 based on undefined signed overflow, issue a warning if
7776 appropriate. */
7778 static tree
7779 vrp_evaluate_conditional (tree_code code, tree op0, tree op1, gimple *stmt)
7781 bool sop;
7782 tree ret;
7783 bool only_ranges;
7785 /* Some passes and foldings leak constants with overflow flag set
7786 into the IL. Avoid doing wrong things with these and bail out. */
7787 if ((TREE_CODE (op0) == INTEGER_CST
7788 && TREE_OVERFLOW (op0))
7789 || (TREE_CODE (op1) == INTEGER_CST
7790 && TREE_OVERFLOW (op1)))
7791 return NULL_TREE;
7793 sop = false;
7794 ret = vrp_evaluate_conditional_warnv_with_ops (code, op0, op1, true, &sop,
7795 &only_ranges);
7797 if (ret && sop)
7799 enum warn_strict_overflow_code wc;
7800 const char* warnmsg;
7802 if (is_gimple_min_invariant (ret))
7804 wc = WARN_STRICT_OVERFLOW_CONDITIONAL;
7805 warnmsg = G_("assuming signed overflow does not occur when "
7806 "simplifying conditional to constant");
7808 else
7810 wc = WARN_STRICT_OVERFLOW_COMPARISON;
7811 warnmsg = G_("assuming signed overflow does not occur when "
7812 "simplifying conditional");
7815 if (issue_strict_overflow_warning (wc))
7817 location_t location;
7819 if (!gimple_has_location (stmt))
7820 location = input_location;
7821 else
7822 location = gimple_location (stmt);
7823 warning_at (location, OPT_Wstrict_overflow, "%s", warnmsg);
7827 if (warn_type_limits
7828 && ret && only_ranges
7829 && TREE_CODE_CLASS (code) == tcc_comparison
7830 && TREE_CODE (op0) == SSA_NAME)
7832 /* If the comparison is being folded and the operand on the LHS
7833 is being compared against a constant value that is outside of
7834 the natural range of OP0's type, then the predicate will
7835 always fold regardless of the value of OP0. If -Wtype-limits
7836 was specified, emit a warning. */
7837 tree type = TREE_TYPE (op0);
7838 value_range *vr0 = get_value_range (op0);
7840 if (vr0->type == VR_RANGE
7841 && INTEGRAL_TYPE_P (type)
7842 && vrp_val_is_min (vr0->min)
7843 && vrp_val_is_max (vr0->max)
7844 && is_gimple_min_invariant (op1))
7846 location_t location;
7848 if (!gimple_has_location (stmt))
7849 location = input_location;
7850 else
7851 location = gimple_location (stmt);
7853 warning_at (location, OPT_Wtype_limits,
7854 integer_zerop (ret)
7855 ? G_("comparison always false "
7856 "due to limited range of data type")
7857 : G_("comparison always true "
7858 "due to limited range of data type"));
7862 return ret;
7866 /* Visit conditional statement STMT. If we can determine which edge
7867 will be taken out of STMT's basic block, record it in
7868 *TAKEN_EDGE_P. Otherwise, set *TAKEN_EDGE_P to NULL. */
7870 static void
7871 vrp_visit_cond_stmt (gcond *stmt, edge *taken_edge_p)
7873 tree val;
7874 bool sop;
7876 *taken_edge_p = NULL;
7878 if (dump_file && (dump_flags & TDF_DETAILS))
7880 tree use;
7881 ssa_op_iter i;
7883 fprintf (dump_file, "\nVisiting conditional with predicate: ");
7884 print_gimple_stmt (dump_file, stmt, 0, 0);
7885 fprintf (dump_file, "\nWith known ranges\n");
7887 FOR_EACH_SSA_TREE_OPERAND (use, stmt, i, SSA_OP_USE)
7889 fprintf (dump_file, "\t");
7890 print_generic_expr (dump_file, use, 0);
7891 fprintf (dump_file, ": ");
7892 dump_value_range (dump_file, vr_value[SSA_NAME_VERSION (use)]);
7895 fprintf (dump_file, "\n");
7898 /* Compute the value of the predicate COND by checking the known
7899 ranges of each of its operands.
7901 Note that we cannot evaluate all the equivalent ranges here
7902 because those ranges may not yet be final and with the current
7903 propagation strategy, we cannot determine when the value ranges
7904 of the names in the equivalence set have changed.
7906 For instance, given the following code fragment
7908 i_5 = PHI <8, i_13>
7910 i_14 = ASSERT_EXPR <i_5, i_5 != 0>
7911 if (i_14 == 1)
7914 Assume that on the first visit to i_14, i_5 has the temporary
7915 range [8, 8] because the second argument to the PHI function is
7916 not yet executable. We derive the range ~[0, 0] for i_14 and the
7917 equivalence set { i_5 }. So, when we visit 'if (i_14 == 1)' for
7918 the first time, since i_14 is equivalent to the range [8, 8], we
7919 determine that the predicate is always false.
7921 On the next round of propagation, i_13 is determined to be
7922 VARYING, which causes i_5 to drop down to VARYING. So, another
7923 visit to i_14 is scheduled. In this second visit, we compute the
7924 exact same range and equivalence set for i_14, namely ~[0, 0] and
7925 { i_5 }. But we did not have the previous range for i_5
7926 registered, so vrp_visit_assignment thinks that the range for
7927 i_14 has not changed. Therefore, the predicate 'if (i_14 == 1)'
7928 is not visited again, which stops propagation from visiting
7929 statements in the THEN clause of that if().
7931 To properly fix this we would need to keep the previous range
7932 value for the names in the equivalence set. This way we would've
7933 discovered that from one visit to the other i_5 changed from
7934 range [8, 8] to VR_VARYING.
7936 However, fixing this apparent limitation may not be worth the
7937 additional checking. Testing on several code bases (GCC, DLV,
7938 MICO, TRAMP3D and SPEC2000) showed that doing this results in
7939 4 more predicates folded in SPEC. */
7940 sop = false;
7942 val = vrp_evaluate_conditional_warnv_with_ops (gimple_cond_code (stmt),
7943 gimple_cond_lhs (stmt),
7944 gimple_cond_rhs (stmt),
7945 false, &sop, NULL);
7946 if (val)
7948 if (!sop)
7949 *taken_edge_p = find_taken_edge (gimple_bb (stmt), val);
7950 else
7952 if (dump_file && (dump_flags & TDF_DETAILS))
7953 fprintf (dump_file,
7954 "\nIgnoring predicate evaluation because "
7955 "it assumes that signed overflow is undefined");
7956 val = NULL_TREE;
7960 if (dump_file && (dump_flags & TDF_DETAILS))
7962 fprintf (dump_file, "\nPredicate evaluates to: ");
7963 if (val == NULL_TREE)
7964 fprintf (dump_file, "DON'T KNOW\n");
7965 else
7966 print_generic_stmt (dump_file, val, 0);
7970 /* Searches the case label vector VEC for the index *IDX of the CASE_LABEL
7971 that includes the value VAL. The search is restricted to the range
7972 [START_IDX, n - 1] where n is the size of VEC.
7974 If there is a CASE_LABEL for VAL, its index is placed in IDX and true is
7975 returned.
7977 If there is no CASE_LABEL for VAL and there is one that is larger than VAL,
7978 it is placed in IDX and false is returned.
7980 If VAL is larger than any CASE_LABEL, n is placed on IDX and false is
7981 returned. */
7983 static bool
7984 find_case_label_index (gswitch *stmt, size_t start_idx, tree val, size_t *idx)
7986 size_t n = gimple_switch_num_labels (stmt);
7987 size_t low, high;
7989 /* Find case label for minimum of the value range or the next one.
7990 At each iteration we are searching in [low, high - 1]. */
7992 for (low = start_idx, high = n; high != low; )
7994 tree t;
7995 int cmp;
7996 /* Note that i != high, so we never ask for n. */
7997 size_t i = (high + low) / 2;
7998 t = gimple_switch_label (stmt, i);
8000 /* Cache the result of comparing CASE_LOW and val. */
8001 cmp = tree_int_cst_compare (CASE_LOW (t), val);
8003 if (cmp == 0)
8005 /* Ranges cannot be empty. */
8006 *idx = i;
8007 return true;
8009 else if (cmp > 0)
8010 high = i;
8011 else
8013 low = i + 1;
8014 if (CASE_HIGH (t) != NULL
8015 && tree_int_cst_compare (CASE_HIGH (t), val) >= 0)
8017 *idx = i;
8018 return true;
8023 *idx = high;
8024 return false;
8027 /* Searches the case label vector VEC for the range of CASE_LABELs that is used
8028 for values between MIN and MAX. The first index is placed in MIN_IDX. The
8029 last index is placed in MAX_IDX. If the range of CASE_LABELs is empty
8030 then MAX_IDX < MIN_IDX.
8031 Returns true if the default label is not needed. */
8033 static bool
8034 find_case_label_range (gswitch *stmt, tree min, tree max, size_t *min_idx,
8035 size_t *max_idx)
8037 size_t i, j;
8038 bool min_take_default = !find_case_label_index (stmt, 1, min, &i);
8039 bool max_take_default = !find_case_label_index (stmt, i, max, &j);
8041 if (i == j
8042 && min_take_default
8043 && max_take_default)
8045 /* Only the default case label reached.
8046 Return an empty range. */
8047 *min_idx = 1;
8048 *max_idx = 0;
8049 return false;
8051 else
8053 bool take_default = min_take_default || max_take_default;
8054 tree low, high;
8055 size_t k;
8057 if (max_take_default)
8058 j--;
8060 /* If the case label range is continuous, we do not need
8061 the default case label. Verify that. */
8062 high = CASE_LOW (gimple_switch_label (stmt, i));
8063 if (CASE_HIGH (gimple_switch_label (stmt, i)))
8064 high = CASE_HIGH (gimple_switch_label (stmt, i));
8065 for (k = i + 1; k <= j; ++k)
8067 low = CASE_LOW (gimple_switch_label (stmt, k));
8068 if (!integer_onep (int_const_binop (MINUS_EXPR, low, high)))
8070 take_default = true;
8071 break;
8073 high = low;
8074 if (CASE_HIGH (gimple_switch_label (stmt, k)))
8075 high = CASE_HIGH (gimple_switch_label (stmt, k));
8078 *min_idx = i;
8079 *max_idx = j;
8080 return !take_default;
8084 /* Searches the case label vector VEC for the ranges of CASE_LABELs that are
8085 used in range VR. The indices are placed in MIN_IDX1, MAX_IDX, MIN_IDX2 and
8086 MAX_IDX2. If the ranges of CASE_LABELs are empty then MAX_IDX1 < MIN_IDX1.
8087 Returns true if the default label is not needed. */
8089 static bool
8090 find_case_label_ranges (gswitch *stmt, value_range *vr, size_t *min_idx1,
8091 size_t *max_idx1, size_t *min_idx2,
8092 size_t *max_idx2)
8094 size_t i, j, k, l;
8095 unsigned int n = gimple_switch_num_labels (stmt);
8096 bool take_default;
8097 tree case_low, case_high;
8098 tree min = vr->min, max = vr->max;
8100 gcc_checking_assert (vr->type == VR_RANGE || vr->type == VR_ANTI_RANGE);
8102 take_default = !find_case_label_range (stmt, min, max, &i, &j);
8104 /* Set second range to emtpy. */
8105 *min_idx2 = 1;
8106 *max_idx2 = 0;
8108 if (vr->type == VR_RANGE)
8110 *min_idx1 = i;
8111 *max_idx1 = j;
8112 return !take_default;
8115 /* Set first range to all case labels. */
8116 *min_idx1 = 1;
8117 *max_idx1 = n - 1;
8119 if (i > j)
8120 return false;
8122 /* Make sure all the values of case labels [i , j] are contained in
8123 range [MIN, MAX]. */
8124 case_low = CASE_LOW (gimple_switch_label (stmt, i));
8125 case_high = CASE_HIGH (gimple_switch_label (stmt, j));
8126 if (tree_int_cst_compare (case_low, min) < 0)
8127 i += 1;
8128 if (case_high != NULL_TREE
8129 && tree_int_cst_compare (max, case_high) < 0)
8130 j -= 1;
8132 if (i > j)
8133 return false;
8135 /* If the range spans case labels [i, j], the corresponding anti-range spans
8136 the labels [1, i - 1] and [j + 1, n - 1]. */
8137 k = j + 1;
8138 l = n - 1;
8139 if (k > l)
8141 k = 1;
8142 l = 0;
8145 j = i - 1;
8146 i = 1;
8147 if (i > j)
8149 i = k;
8150 j = l;
8151 k = 1;
8152 l = 0;
8155 *min_idx1 = i;
8156 *max_idx1 = j;
8157 *min_idx2 = k;
8158 *max_idx2 = l;
8159 return false;
8162 /* Visit switch statement STMT. If we can determine which edge
8163 will be taken out of STMT's basic block, record it in
8164 *TAKEN_EDGE_P. Otherwise, *TAKEN_EDGE_P set to NULL. */
8166 static void
8167 vrp_visit_switch_stmt (gswitch *stmt, edge *taken_edge_p)
8169 tree op, val;
8170 value_range *vr;
8171 size_t i = 0, j = 0, k, l;
8172 bool take_default;
8174 *taken_edge_p = NULL;
8175 op = gimple_switch_index (stmt);
8176 if (TREE_CODE (op) != SSA_NAME)
8177 return;
8179 vr = get_value_range (op);
8180 if (dump_file && (dump_flags & TDF_DETAILS))
8182 fprintf (dump_file, "\nVisiting switch expression with operand ");
8183 print_generic_expr (dump_file, op, 0);
8184 fprintf (dump_file, " with known range ");
8185 dump_value_range (dump_file, vr);
8186 fprintf (dump_file, "\n");
8189 if ((vr->type != VR_RANGE
8190 && vr->type != VR_ANTI_RANGE)
8191 || symbolic_range_p (vr))
8192 return;
8194 /* Find the single edge that is taken from the switch expression. */
8195 take_default = !find_case_label_ranges (stmt, vr, &i, &j, &k, &l);
8197 /* Check if the range spans no CASE_LABEL. If so, we only reach the default
8198 label */
8199 if (j < i)
8201 gcc_assert (take_default);
8202 val = gimple_switch_default_label (stmt);
8204 else
8206 /* Check if labels with index i to j and maybe the default label
8207 are all reaching the same label. */
8209 val = gimple_switch_label (stmt, i);
8210 if (take_default
8211 && CASE_LABEL (gimple_switch_default_label (stmt))
8212 != CASE_LABEL (val))
8214 if (dump_file && (dump_flags & TDF_DETAILS))
8215 fprintf (dump_file, " not a single destination for this "
8216 "range\n");
8217 return;
8219 for (++i; i <= j; ++i)
8221 if (CASE_LABEL (gimple_switch_label (stmt, i)) != CASE_LABEL (val))
8223 if (dump_file && (dump_flags & TDF_DETAILS))
8224 fprintf (dump_file, " not a single destination for this "
8225 "range\n");
8226 return;
8229 for (; k <= l; ++k)
8231 if (CASE_LABEL (gimple_switch_label (stmt, k)) != CASE_LABEL (val))
8233 if (dump_file && (dump_flags & TDF_DETAILS))
8234 fprintf (dump_file, " not a single destination for this "
8235 "range\n");
8236 return;
8241 *taken_edge_p = find_edge (gimple_bb (stmt),
8242 label_to_block (CASE_LABEL (val)));
8244 if (dump_file && (dump_flags & TDF_DETAILS))
8246 fprintf (dump_file, " will take edge to ");
8247 print_generic_stmt (dump_file, CASE_LABEL (val), 0);
8252 /* Evaluate statement STMT. If the statement produces a useful range,
8253 set VR and corepsponding OUTPUT_P.
8255 If STMT is a conditional branch and we can determine its truth
8256 value, the taken edge is recorded in *TAKEN_EDGE_P. */
8258 static void
8259 extract_range_from_stmt (gimple *stmt, edge *taken_edge_p,
8260 tree *output_p, value_range *vr)
8263 if (dump_file && (dump_flags & TDF_DETAILS))
8265 fprintf (dump_file, "\nVisiting statement:\n");
8266 print_gimple_stmt (dump_file, stmt, 0, dump_flags);
8269 if (!stmt_interesting_for_vrp (stmt))
8270 gcc_assert (stmt_ends_bb_p (stmt));
8271 else if (is_gimple_assign (stmt) || is_gimple_call (stmt))
8272 vrp_visit_assignment_or_call (stmt, output_p, vr);
8273 else if (gimple_code (stmt) == GIMPLE_COND)
8274 vrp_visit_cond_stmt (as_a <gcond *> (stmt), taken_edge_p);
8275 else if (gimple_code (stmt) == GIMPLE_SWITCH)
8276 vrp_visit_switch_stmt (as_a <gswitch *> (stmt), taken_edge_p);
8279 /* Evaluate statement STMT. If the statement produces a useful range,
8280 return SSA_PROP_INTERESTING and record the SSA name with the
8281 interesting range into *OUTPUT_P.
8283 If STMT is a conditional branch and we can determine its truth
8284 value, the taken edge is recorded in *TAKEN_EDGE_P.
8286 If STMT produces a varying value, return SSA_PROP_VARYING. */
8288 static enum ssa_prop_result
8289 vrp_visit_stmt (gimple *stmt, edge *taken_edge_p, tree *output_p)
8291 value_range vr = VR_INITIALIZER;
8292 tree lhs = gimple_get_lhs (stmt);
8293 extract_range_from_stmt (stmt, taken_edge_p, output_p, &vr);
8295 if (*output_p)
8297 if (update_value_range (*output_p, &vr))
8299 if (dump_file && (dump_flags & TDF_DETAILS))
8301 fprintf (dump_file, "Found new range for ");
8302 print_generic_expr (dump_file, *output_p, 0);
8303 fprintf (dump_file, ": ");
8304 dump_value_range (dump_file, &vr);
8305 fprintf (dump_file, "\n");
8308 if (vr.type == VR_VARYING)
8309 return SSA_PROP_VARYING;
8311 return SSA_PROP_INTERESTING;
8313 return SSA_PROP_NOT_INTERESTING;
8316 if (is_gimple_call (stmt) && gimple_call_internal_p (stmt))
8317 switch (gimple_call_internal_fn (stmt))
8319 case IFN_ADD_OVERFLOW:
8320 case IFN_SUB_OVERFLOW:
8321 case IFN_MUL_OVERFLOW:
8322 case IFN_ATOMIC_COMPARE_EXCHANGE:
8323 /* These internal calls return _Complex integer type,
8324 which VRP does not track, but the immediate uses
8325 thereof might be interesting. */
8326 if (lhs && TREE_CODE (lhs) == SSA_NAME)
8328 imm_use_iterator iter;
8329 use_operand_p use_p;
8330 enum ssa_prop_result res = SSA_PROP_VARYING;
8332 set_value_range_to_varying (get_value_range (lhs));
8334 FOR_EACH_IMM_USE_FAST (use_p, iter, lhs)
8336 gimple *use_stmt = USE_STMT (use_p);
8337 if (!is_gimple_assign (use_stmt))
8338 continue;
8339 enum tree_code rhs_code = gimple_assign_rhs_code (use_stmt);
8340 if (rhs_code != REALPART_EXPR && rhs_code != IMAGPART_EXPR)
8341 continue;
8342 tree rhs1 = gimple_assign_rhs1 (use_stmt);
8343 tree use_lhs = gimple_assign_lhs (use_stmt);
8344 if (TREE_CODE (rhs1) != rhs_code
8345 || TREE_OPERAND (rhs1, 0) != lhs
8346 || TREE_CODE (use_lhs) != SSA_NAME
8347 || !stmt_interesting_for_vrp (use_stmt)
8348 || (!INTEGRAL_TYPE_P (TREE_TYPE (use_lhs))
8349 || !TYPE_MIN_VALUE (TREE_TYPE (use_lhs))
8350 || !TYPE_MAX_VALUE (TREE_TYPE (use_lhs))))
8351 continue;
8353 /* If there is a change in the value range for any of the
8354 REALPART_EXPR/IMAGPART_EXPR immediate uses, return
8355 SSA_PROP_INTERESTING. If there are any REALPART_EXPR
8356 or IMAGPART_EXPR immediate uses, but none of them have
8357 a change in their value ranges, return
8358 SSA_PROP_NOT_INTERESTING. If there are no
8359 {REAL,IMAG}PART_EXPR uses at all,
8360 return SSA_PROP_VARYING. */
8361 value_range new_vr = VR_INITIALIZER;
8362 extract_range_basic (&new_vr, use_stmt);
8363 value_range *old_vr = get_value_range (use_lhs);
8364 if (old_vr->type != new_vr.type
8365 || !vrp_operand_equal_p (old_vr->min, new_vr.min)
8366 || !vrp_operand_equal_p (old_vr->max, new_vr.max)
8367 || !vrp_bitmap_equal_p (old_vr->equiv, new_vr.equiv))
8368 res = SSA_PROP_INTERESTING;
8369 else
8370 res = SSA_PROP_NOT_INTERESTING;
8371 BITMAP_FREE (new_vr.equiv);
8372 if (res == SSA_PROP_INTERESTING)
8374 *output_p = lhs;
8375 return res;
8379 return res;
8381 break;
8382 default:
8383 break;
8386 /* All other statements produce nothing of interest for VRP, so mark
8387 their outputs varying and prevent further simulation. */
8388 set_defs_to_varying (stmt);
8390 return (*taken_edge_p) ? SSA_PROP_INTERESTING : SSA_PROP_VARYING;
8393 /* Union the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
8394 { VR1TYPE, VR0MIN, VR0MAX } and store the result
8395 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
8396 possible such range. The resulting range is not canonicalized. */
8398 static void
8399 union_ranges (enum value_range_type *vr0type,
8400 tree *vr0min, tree *vr0max,
8401 enum value_range_type vr1type,
8402 tree vr1min, tree vr1max)
8404 bool mineq = vrp_operand_equal_p (*vr0min, vr1min);
8405 bool maxeq = vrp_operand_equal_p (*vr0max, vr1max);
8407 /* [] is vr0, () is vr1 in the following classification comments. */
8408 if (mineq && maxeq)
8410 /* [( )] */
8411 if (*vr0type == vr1type)
8412 /* Nothing to do for equal ranges. */
8414 else if ((*vr0type == VR_RANGE
8415 && vr1type == VR_ANTI_RANGE)
8416 || (*vr0type == VR_ANTI_RANGE
8417 && vr1type == VR_RANGE))
8419 /* For anti-range with range union the result is varying. */
8420 goto give_up;
8422 else
8423 gcc_unreachable ();
8425 else if (operand_less_p (*vr0max, vr1min) == 1
8426 || operand_less_p (vr1max, *vr0min) == 1)
8428 /* [ ] ( ) or ( ) [ ]
8429 If the ranges have an empty intersection, result of the union
8430 operation is the anti-range or if both are anti-ranges
8431 it covers all. */
8432 if (*vr0type == VR_ANTI_RANGE
8433 && vr1type == VR_ANTI_RANGE)
8434 goto give_up;
8435 else if (*vr0type == VR_ANTI_RANGE
8436 && vr1type == VR_RANGE)
8438 else if (*vr0type == VR_RANGE
8439 && vr1type == VR_ANTI_RANGE)
8441 *vr0type = vr1type;
8442 *vr0min = vr1min;
8443 *vr0max = vr1max;
8445 else if (*vr0type == VR_RANGE
8446 && vr1type == VR_RANGE)
8448 /* The result is the convex hull of both ranges. */
8449 if (operand_less_p (*vr0max, vr1min) == 1)
8451 /* If the result can be an anti-range, create one. */
8452 if (TREE_CODE (*vr0max) == INTEGER_CST
8453 && TREE_CODE (vr1min) == INTEGER_CST
8454 && vrp_val_is_min (*vr0min)
8455 && vrp_val_is_max (vr1max))
8457 tree min = int_const_binop (PLUS_EXPR,
8458 *vr0max,
8459 build_int_cst (TREE_TYPE (*vr0max), 1));
8460 tree max = int_const_binop (MINUS_EXPR,
8461 vr1min,
8462 build_int_cst (TREE_TYPE (vr1min), 1));
8463 if (!operand_less_p (max, min))
8465 *vr0type = VR_ANTI_RANGE;
8466 *vr0min = min;
8467 *vr0max = max;
8469 else
8470 *vr0max = vr1max;
8472 else
8473 *vr0max = vr1max;
8475 else
8477 /* If the result can be an anti-range, create one. */
8478 if (TREE_CODE (vr1max) == INTEGER_CST
8479 && TREE_CODE (*vr0min) == INTEGER_CST
8480 && vrp_val_is_min (vr1min)
8481 && vrp_val_is_max (*vr0max))
8483 tree min = int_const_binop (PLUS_EXPR,
8484 vr1max,
8485 build_int_cst (TREE_TYPE (vr1max), 1));
8486 tree max = int_const_binop (MINUS_EXPR,
8487 *vr0min,
8488 build_int_cst (TREE_TYPE (*vr0min), 1));
8489 if (!operand_less_p (max, min))
8491 *vr0type = VR_ANTI_RANGE;
8492 *vr0min = min;
8493 *vr0max = max;
8495 else
8496 *vr0min = vr1min;
8498 else
8499 *vr0min = vr1min;
8502 else
8503 gcc_unreachable ();
8505 else if ((maxeq || operand_less_p (vr1max, *vr0max) == 1)
8506 && (mineq || operand_less_p (*vr0min, vr1min) == 1))
8508 /* [ ( ) ] or [( ) ] or [ ( )] */
8509 if (*vr0type == VR_RANGE
8510 && vr1type == VR_RANGE)
8512 else if (*vr0type == VR_ANTI_RANGE
8513 && vr1type == VR_ANTI_RANGE)
8515 *vr0type = vr1type;
8516 *vr0min = vr1min;
8517 *vr0max = vr1max;
8519 else if (*vr0type == VR_ANTI_RANGE
8520 && vr1type == VR_RANGE)
8522 /* Arbitrarily choose the right or left gap. */
8523 if (!mineq && TREE_CODE (vr1min) == INTEGER_CST)
8524 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
8525 build_int_cst (TREE_TYPE (vr1min), 1));
8526 else if (!maxeq && TREE_CODE (vr1max) == INTEGER_CST)
8527 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
8528 build_int_cst (TREE_TYPE (vr1max), 1));
8529 else
8530 goto give_up;
8532 else if (*vr0type == VR_RANGE
8533 && vr1type == VR_ANTI_RANGE)
8534 /* The result covers everything. */
8535 goto give_up;
8536 else
8537 gcc_unreachable ();
8539 else if ((maxeq || operand_less_p (*vr0max, vr1max) == 1)
8540 && (mineq || operand_less_p (vr1min, *vr0min) == 1))
8542 /* ( [ ] ) or ([ ] ) or ( [ ]) */
8543 if (*vr0type == VR_RANGE
8544 && vr1type == VR_RANGE)
8546 *vr0type = vr1type;
8547 *vr0min = vr1min;
8548 *vr0max = vr1max;
8550 else if (*vr0type == VR_ANTI_RANGE
8551 && vr1type == VR_ANTI_RANGE)
8553 else if (*vr0type == VR_RANGE
8554 && vr1type == VR_ANTI_RANGE)
8556 *vr0type = VR_ANTI_RANGE;
8557 if (!mineq && TREE_CODE (*vr0min) == INTEGER_CST)
8559 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
8560 build_int_cst (TREE_TYPE (*vr0min), 1));
8561 *vr0min = vr1min;
8563 else if (!maxeq && TREE_CODE (*vr0max) == INTEGER_CST)
8565 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
8566 build_int_cst (TREE_TYPE (*vr0max), 1));
8567 *vr0max = vr1max;
8569 else
8570 goto give_up;
8572 else if (*vr0type == VR_ANTI_RANGE
8573 && vr1type == VR_RANGE)
8574 /* The result covers everything. */
8575 goto give_up;
8576 else
8577 gcc_unreachable ();
8579 else if ((operand_less_p (vr1min, *vr0max) == 1
8580 || operand_equal_p (vr1min, *vr0max, 0))
8581 && operand_less_p (*vr0min, vr1min) == 1
8582 && operand_less_p (*vr0max, vr1max) == 1)
8584 /* [ ( ] ) or [ ]( ) */
8585 if (*vr0type == VR_RANGE
8586 && vr1type == VR_RANGE)
8587 *vr0max = vr1max;
8588 else if (*vr0type == VR_ANTI_RANGE
8589 && vr1type == VR_ANTI_RANGE)
8590 *vr0min = vr1min;
8591 else if (*vr0type == VR_ANTI_RANGE
8592 && vr1type == VR_RANGE)
8594 if (TREE_CODE (vr1min) == INTEGER_CST)
8595 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
8596 build_int_cst (TREE_TYPE (vr1min), 1));
8597 else
8598 goto give_up;
8600 else if (*vr0type == VR_RANGE
8601 && vr1type == VR_ANTI_RANGE)
8603 if (TREE_CODE (*vr0max) == INTEGER_CST)
8605 *vr0type = vr1type;
8606 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
8607 build_int_cst (TREE_TYPE (*vr0max), 1));
8608 *vr0max = vr1max;
8610 else
8611 goto give_up;
8613 else
8614 gcc_unreachable ();
8616 else if ((operand_less_p (*vr0min, vr1max) == 1
8617 || operand_equal_p (*vr0min, vr1max, 0))
8618 && operand_less_p (vr1min, *vr0min) == 1
8619 && operand_less_p (vr1max, *vr0max) == 1)
8621 /* ( [ ) ] or ( )[ ] */
8622 if (*vr0type == VR_RANGE
8623 && vr1type == VR_RANGE)
8624 *vr0min = vr1min;
8625 else if (*vr0type == VR_ANTI_RANGE
8626 && vr1type == VR_ANTI_RANGE)
8627 *vr0max = vr1max;
8628 else if (*vr0type == VR_ANTI_RANGE
8629 && vr1type == VR_RANGE)
8631 if (TREE_CODE (vr1max) == INTEGER_CST)
8632 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
8633 build_int_cst (TREE_TYPE (vr1max), 1));
8634 else
8635 goto give_up;
8637 else if (*vr0type == VR_RANGE
8638 && vr1type == VR_ANTI_RANGE)
8640 if (TREE_CODE (*vr0min) == INTEGER_CST)
8642 *vr0type = vr1type;
8643 *vr0min = vr1min;
8644 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
8645 build_int_cst (TREE_TYPE (*vr0min), 1));
8647 else
8648 goto give_up;
8650 else
8651 gcc_unreachable ();
8653 else
8654 goto give_up;
8656 return;
8658 give_up:
8659 *vr0type = VR_VARYING;
8660 *vr0min = NULL_TREE;
8661 *vr0max = NULL_TREE;
8664 /* Intersect the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
8665 { VR1TYPE, VR0MIN, VR0MAX } and store the result
8666 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
8667 possible such range. The resulting range is not canonicalized. */
8669 static void
8670 intersect_ranges (enum value_range_type *vr0type,
8671 tree *vr0min, tree *vr0max,
8672 enum value_range_type vr1type,
8673 tree vr1min, tree vr1max)
8675 bool mineq = vrp_operand_equal_p (*vr0min, vr1min);
8676 bool maxeq = vrp_operand_equal_p (*vr0max, vr1max);
8678 /* [] is vr0, () is vr1 in the following classification comments. */
8679 if (mineq && maxeq)
8681 /* [( )] */
8682 if (*vr0type == vr1type)
8683 /* Nothing to do for equal ranges. */
8685 else if ((*vr0type == VR_RANGE
8686 && vr1type == VR_ANTI_RANGE)
8687 || (*vr0type == VR_ANTI_RANGE
8688 && vr1type == VR_RANGE))
8690 /* For anti-range with range intersection the result is empty. */
8691 *vr0type = VR_UNDEFINED;
8692 *vr0min = NULL_TREE;
8693 *vr0max = NULL_TREE;
8695 else
8696 gcc_unreachable ();
8698 else if (operand_less_p (*vr0max, vr1min) == 1
8699 || operand_less_p (vr1max, *vr0min) == 1)
8701 /* [ ] ( ) or ( ) [ ]
8702 If the ranges have an empty intersection, the result of the
8703 intersect operation is the range for intersecting an
8704 anti-range with a range or empty when intersecting two ranges. */
8705 if (*vr0type == VR_RANGE
8706 && vr1type == VR_ANTI_RANGE)
8708 else if (*vr0type == VR_ANTI_RANGE
8709 && vr1type == VR_RANGE)
8711 *vr0type = vr1type;
8712 *vr0min = vr1min;
8713 *vr0max = vr1max;
8715 else if (*vr0type == VR_RANGE
8716 && vr1type == VR_RANGE)
8718 *vr0type = VR_UNDEFINED;
8719 *vr0min = NULL_TREE;
8720 *vr0max = NULL_TREE;
8722 else if (*vr0type == VR_ANTI_RANGE
8723 && vr1type == VR_ANTI_RANGE)
8725 /* If the anti-ranges are adjacent to each other merge them. */
8726 if (TREE_CODE (*vr0max) == INTEGER_CST
8727 && TREE_CODE (vr1min) == INTEGER_CST
8728 && operand_less_p (*vr0max, vr1min) == 1
8729 && integer_onep (int_const_binop (MINUS_EXPR,
8730 vr1min, *vr0max)))
8731 *vr0max = vr1max;
8732 else if (TREE_CODE (vr1max) == INTEGER_CST
8733 && TREE_CODE (*vr0min) == INTEGER_CST
8734 && operand_less_p (vr1max, *vr0min) == 1
8735 && integer_onep (int_const_binop (MINUS_EXPR,
8736 *vr0min, vr1max)))
8737 *vr0min = vr1min;
8738 /* Else arbitrarily take VR0. */
8741 else if ((maxeq || operand_less_p (vr1max, *vr0max) == 1)
8742 && (mineq || operand_less_p (*vr0min, vr1min) == 1))
8744 /* [ ( ) ] or [( ) ] or [ ( )] */
8745 if (*vr0type == VR_RANGE
8746 && vr1type == VR_RANGE)
8748 /* If both are ranges the result is the inner one. */
8749 *vr0type = vr1type;
8750 *vr0min = vr1min;
8751 *vr0max = vr1max;
8753 else if (*vr0type == VR_RANGE
8754 && vr1type == VR_ANTI_RANGE)
8756 /* Choose the right gap if the left one is empty. */
8757 if (mineq)
8759 if (TREE_CODE (vr1max) == INTEGER_CST)
8760 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
8761 build_int_cst (TREE_TYPE (vr1max), 1));
8762 else
8763 *vr0min = vr1max;
8765 /* Choose the left gap if the right one is empty. */
8766 else if (maxeq)
8768 if (TREE_CODE (vr1min) == INTEGER_CST)
8769 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
8770 build_int_cst (TREE_TYPE (vr1min), 1));
8771 else
8772 *vr0max = vr1min;
8774 /* Choose the anti-range if the range is effectively varying. */
8775 else if (vrp_val_is_min (*vr0min)
8776 && vrp_val_is_max (*vr0max))
8778 *vr0type = vr1type;
8779 *vr0min = vr1min;
8780 *vr0max = vr1max;
8782 /* Else choose the range. */
8784 else if (*vr0type == VR_ANTI_RANGE
8785 && vr1type == VR_ANTI_RANGE)
8786 /* If both are anti-ranges the result is the outer one. */
8788 else if (*vr0type == VR_ANTI_RANGE
8789 && vr1type == VR_RANGE)
8791 /* The intersection is empty. */
8792 *vr0type = VR_UNDEFINED;
8793 *vr0min = NULL_TREE;
8794 *vr0max = NULL_TREE;
8796 else
8797 gcc_unreachable ();
8799 else if ((maxeq || operand_less_p (*vr0max, vr1max) == 1)
8800 && (mineq || operand_less_p (vr1min, *vr0min) == 1))
8802 /* ( [ ] ) or ([ ] ) or ( [ ]) */
8803 if (*vr0type == VR_RANGE
8804 && vr1type == VR_RANGE)
8805 /* Choose the inner range. */
8807 else if (*vr0type == VR_ANTI_RANGE
8808 && vr1type == VR_RANGE)
8810 /* Choose the right gap if the left is empty. */
8811 if (mineq)
8813 *vr0type = VR_RANGE;
8814 if (TREE_CODE (*vr0max) == INTEGER_CST)
8815 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
8816 build_int_cst (TREE_TYPE (*vr0max), 1));
8817 else
8818 *vr0min = *vr0max;
8819 *vr0max = vr1max;
8821 /* Choose the left gap if the right is empty. */
8822 else if (maxeq)
8824 *vr0type = VR_RANGE;
8825 if (TREE_CODE (*vr0min) == INTEGER_CST)
8826 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
8827 build_int_cst (TREE_TYPE (*vr0min), 1));
8828 else
8829 *vr0max = *vr0min;
8830 *vr0min = vr1min;
8832 /* Choose the anti-range if the range is effectively varying. */
8833 else if (vrp_val_is_min (vr1min)
8834 && vrp_val_is_max (vr1max))
8836 /* Choose the anti-range if it is ~[0,0], that range is special
8837 enough to special case when vr1's range is relatively wide. */
8838 else if (*vr0min == *vr0max
8839 && integer_zerop (*vr0min)
8840 && (TYPE_PRECISION (TREE_TYPE (*vr0min))
8841 == TYPE_PRECISION (ptr_type_node))
8842 && TREE_CODE (vr1max) == INTEGER_CST
8843 && TREE_CODE (vr1min) == INTEGER_CST
8844 && (wi::clz (wi::sub (vr1max, vr1min))
8845 < TYPE_PRECISION (TREE_TYPE (*vr0min)) / 2))
8847 /* Else choose the range. */
8848 else
8850 *vr0type = vr1type;
8851 *vr0min = vr1min;
8852 *vr0max = vr1max;
8855 else if (*vr0type == VR_ANTI_RANGE
8856 && vr1type == VR_ANTI_RANGE)
8858 /* If both are anti-ranges the result is the outer one. */
8859 *vr0type = vr1type;
8860 *vr0min = vr1min;
8861 *vr0max = vr1max;
8863 else if (vr1type == VR_ANTI_RANGE
8864 && *vr0type == VR_RANGE)
8866 /* The intersection is empty. */
8867 *vr0type = VR_UNDEFINED;
8868 *vr0min = NULL_TREE;
8869 *vr0max = NULL_TREE;
8871 else
8872 gcc_unreachable ();
8874 else if ((operand_less_p (vr1min, *vr0max) == 1
8875 || operand_equal_p (vr1min, *vr0max, 0))
8876 && operand_less_p (*vr0min, vr1min) == 1)
8878 /* [ ( ] ) or [ ]( ) */
8879 if (*vr0type == VR_ANTI_RANGE
8880 && vr1type == VR_ANTI_RANGE)
8881 *vr0max = vr1max;
8882 else if (*vr0type == VR_RANGE
8883 && vr1type == VR_RANGE)
8884 *vr0min = vr1min;
8885 else if (*vr0type == VR_RANGE
8886 && vr1type == VR_ANTI_RANGE)
8888 if (TREE_CODE (vr1min) == INTEGER_CST)
8889 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
8890 build_int_cst (TREE_TYPE (vr1min), 1));
8891 else
8892 *vr0max = vr1min;
8894 else if (*vr0type == VR_ANTI_RANGE
8895 && vr1type == VR_RANGE)
8897 *vr0type = VR_RANGE;
8898 if (TREE_CODE (*vr0max) == INTEGER_CST)
8899 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
8900 build_int_cst (TREE_TYPE (*vr0max), 1));
8901 else
8902 *vr0min = *vr0max;
8903 *vr0max = vr1max;
8905 else
8906 gcc_unreachable ();
8908 else if ((operand_less_p (*vr0min, vr1max) == 1
8909 || operand_equal_p (*vr0min, vr1max, 0))
8910 && operand_less_p (vr1min, *vr0min) == 1)
8912 /* ( [ ) ] or ( )[ ] */
8913 if (*vr0type == VR_ANTI_RANGE
8914 && vr1type == VR_ANTI_RANGE)
8915 *vr0min = vr1min;
8916 else if (*vr0type == VR_RANGE
8917 && vr1type == VR_RANGE)
8918 *vr0max = vr1max;
8919 else if (*vr0type == VR_RANGE
8920 && vr1type == VR_ANTI_RANGE)
8922 if (TREE_CODE (vr1max) == INTEGER_CST)
8923 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
8924 build_int_cst (TREE_TYPE (vr1max), 1));
8925 else
8926 *vr0min = vr1max;
8928 else if (*vr0type == VR_ANTI_RANGE
8929 && vr1type == VR_RANGE)
8931 *vr0type = VR_RANGE;
8932 if (TREE_CODE (*vr0min) == INTEGER_CST)
8933 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
8934 build_int_cst (TREE_TYPE (*vr0min), 1));
8935 else
8936 *vr0max = *vr0min;
8937 *vr0min = vr1min;
8939 else
8940 gcc_unreachable ();
8943 /* As a fallback simply use { *VRTYPE, *VR0MIN, *VR0MAX } as
8944 result for the intersection. That's always a conservative
8945 correct estimate unless VR1 is a constant singleton range
8946 in which case we choose that. */
8947 if (vr1type == VR_RANGE
8948 && is_gimple_min_invariant (vr1min)
8949 && vrp_operand_equal_p (vr1min, vr1max))
8951 *vr0type = vr1type;
8952 *vr0min = vr1min;
8953 *vr0max = vr1max;
8956 return;
8960 /* Intersect the two value-ranges *VR0 and *VR1 and store the result
8961 in *VR0. This may not be the smallest possible such range. */
8963 static void
8964 vrp_intersect_ranges_1 (value_range *vr0, value_range *vr1)
8966 value_range saved;
8968 /* If either range is VR_VARYING the other one wins. */
8969 if (vr1->type == VR_VARYING)
8970 return;
8971 if (vr0->type == VR_VARYING)
8973 copy_value_range (vr0, vr1);
8974 return;
8977 /* When either range is VR_UNDEFINED the resulting range is
8978 VR_UNDEFINED, too. */
8979 if (vr0->type == VR_UNDEFINED)
8980 return;
8981 if (vr1->type == VR_UNDEFINED)
8983 set_value_range_to_undefined (vr0);
8984 return;
8987 /* Save the original vr0 so we can return it as conservative intersection
8988 result when our worker turns things to varying. */
8989 saved = *vr0;
8990 intersect_ranges (&vr0->type, &vr0->min, &vr0->max,
8991 vr1->type, vr1->min, vr1->max);
8992 /* Make sure to canonicalize the result though as the inversion of a
8993 VR_RANGE can still be a VR_RANGE. */
8994 set_and_canonicalize_value_range (vr0, vr0->type,
8995 vr0->min, vr0->max, vr0->equiv);
8996 /* If that failed, use the saved original VR0. */
8997 if (vr0->type == VR_VARYING)
8999 *vr0 = saved;
9000 return;
9002 /* If the result is VR_UNDEFINED there is no need to mess with
9003 the equivalencies. */
9004 if (vr0->type == VR_UNDEFINED)
9005 return;
9007 /* The resulting set of equivalences for range intersection is the union of
9008 the two sets. */
9009 if (vr0->equiv && vr1->equiv && vr0->equiv != vr1->equiv)
9010 bitmap_ior_into (vr0->equiv, vr1->equiv);
9011 else if (vr1->equiv && !vr0->equiv)
9013 vr0->equiv = BITMAP_ALLOC (&vrp_equiv_obstack);
9014 bitmap_copy (vr0->equiv, vr1->equiv);
9018 void
9019 vrp_intersect_ranges (value_range *vr0, value_range *vr1)
9021 if (dump_file && (dump_flags & TDF_DETAILS))
9023 fprintf (dump_file, "Intersecting\n ");
9024 dump_value_range (dump_file, vr0);
9025 fprintf (dump_file, "\nand\n ");
9026 dump_value_range (dump_file, vr1);
9027 fprintf (dump_file, "\n");
9029 vrp_intersect_ranges_1 (vr0, vr1);
9030 if (dump_file && (dump_flags & TDF_DETAILS))
9032 fprintf (dump_file, "to\n ");
9033 dump_value_range (dump_file, vr0);
9034 fprintf (dump_file, "\n");
9038 /* Meet operation for value ranges. Given two value ranges VR0 and
9039 VR1, store in VR0 a range that contains both VR0 and VR1. This
9040 may not be the smallest possible such range. */
9042 static void
9043 vrp_meet_1 (value_range *vr0, const value_range *vr1)
9045 value_range saved;
9047 if (vr0->type == VR_UNDEFINED)
9049 set_value_range (vr0, vr1->type, vr1->min, vr1->max, vr1->equiv);
9050 return;
9053 if (vr1->type == VR_UNDEFINED)
9055 /* VR0 already has the resulting range. */
9056 return;
9059 if (vr0->type == VR_VARYING)
9061 /* Nothing to do. VR0 already has the resulting range. */
9062 return;
9065 if (vr1->type == VR_VARYING)
9067 set_value_range_to_varying (vr0);
9068 return;
9071 saved = *vr0;
9072 union_ranges (&vr0->type, &vr0->min, &vr0->max,
9073 vr1->type, vr1->min, vr1->max);
9074 if (vr0->type == VR_VARYING)
9076 /* Failed to find an efficient meet. Before giving up and setting
9077 the result to VARYING, see if we can at least derive a useful
9078 anti-range. FIXME, all this nonsense about distinguishing
9079 anti-ranges from ranges is necessary because of the odd
9080 semantics of range_includes_zero_p and friends. */
9081 if (((saved.type == VR_RANGE
9082 && range_includes_zero_p (saved.min, saved.max) == 0)
9083 || (saved.type == VR_ANTI_RANGE
9084 && range_includes_zero_p (saved.min, saved.max) == 1))
9085 && ((vr1->type == VR_RANGE
9086 && range_includes_zero_p (vr1->min, vr1->max) == 0)
9087 || (vr1->type == VR_ANTI_RANGE
9088 && range_includes_zero_p (vr1->min, vr1->max) == 1)))
9090 set_value_range_to_nonnull (vr0, TREE_TYPE (saved.min));
9092 /* Since this meet operation did not result from the meeting of
9093 two equivalent names, VR0 cannot have any equivalences. */
9094 if (vr0->equiv)
9095 bitmap_clear (vr0->equiv);
9096 return;
9099 set_value_range_to_varying (vr0);
9100 return;
9102 set_and_canonicalize_value_range (vr0, vr0->type, vr0->min, vr0->max,
9103 vr0->equiv);
9104 if (vr0->type == VR_VARYING)
9105 return;
9107 /* The resulting set of equivalences is always the intersection of
9108 the two sets. */
9109 if (vr0->equiv && vr1->equiv && vr0->equiv != vr1->equiv)
9110 bitmap_and_into (vr0->equiv, vr1->equiv);
9111 else if (vr0->equiv && !vr1->equiv)
9112 bitmap_clear (vr0->equiv);
9115 void
9116 vrp_meet (value_range *vr0, const value_range *vr1)
9118 if (dump_file && (dump_flags & TDF_DETAILS))
9120 fprintf (dump_file, "Meeting\n ");
9121 dump_value_range (dump_file, vr0);
9122 fprintf (dump_file, "\nand\n ");
9123 dump_value_range (dump_file, vr1);
9124 fprintf (dump_file, "\n");
9126 vrp_meet_1 (vr0, vr1);
9127 if (dump_file && (dump_flags & TDF_DETAILS))
9129 fprintf (dump_file, "to\n ");
9130 dump_value_range (dump_file, vr0);
9131 fprintf (dump_file, "\n");
9136 /* Visit all arguments for PHI node PHI that flow through executable
9137 edges. If a valid value range can be derived from all the incoming
9138 value ranges, set a new range in VR_RESULT. */
9140 static void
9141 extract_range_from_phi_node (gphi *phi, value_range *vr_result)
9143 size_t i;
9144 tree lhs = PHI_RESULT (phi);
9145 value_range *lhs_vr = get_value_range (lhs);
9146 bool first = true;
9147 int edges, old_edges;
9148 struct loop *l;
9150 if (dump_file && (dump_flags & TDF_DETAILS))
9152 fprintf (dump_file, "\nVisiting PHI node: ");
9153 print_gimple_stmt (dump_file, phi, 0, dump_flags);
9156 bool may_simulate_backedge_again = false;
9157 edges = 0;
9158 for (i = 0; i < gimple_phi_num_args (phi); i++)
9160 edge e = gimple_phi_arg_edge (phi, i);
9162 if (dump_file && (dump_flags & TDF_DETAILS))
9164 fprintf (dump_file,
9165 " Argument #%d (%d -> %d %sexecutable)\n",
9166 (int) i, e->src->index, e->dest->index,
9167 (e->flags & EDGE_EXECUTABLE) ? "" : "not ");
9170 if (e->flags & EDGE_EXECUTABLE)
9172 tree arg = PHI_ARG_DEF (phi, i);
9173 value_range vr_arg;
9175 ++edges;
9177 if (TREE_CODE (arg) == SSA_NAME)
9179 /* See if we are eventually going to change one of the args. */
9180 gimple *def_stmt = SSA_NAME_DEF_STMT (arg);
9181 if (! gimple_nop_p (def_stmt)
9182 && prop_simulate_again_p (def_stmt)
9183 && e->flags & EDGE_DFS_BACK)
9184 may_simulate_backedge_again = true;
9186 vr_arg = *(get_value_range (arg));
9187 /* Do not allow equivalences or symbolic ranges to leak in from
9188 backedges. That creates invalid equivalencies.
9189 See PR53465 and PR54767. */
9190 if (e->flags & EDGE_DFS_BACK)
9192 if (vr_arg.type == VR_RANGE
9193 || vr_arg.type == VR_ANTI_RANGE)
9195 vr_arg.equiv = NULL;
9196 if (symbolic_range_p (&vr_arg))
9198 vr_arg.type = VR_VARYING;
9199 vr_arg.min = NULL_TREE;
9200 vr_arg.max = NULL_TREE;
9204 else
9206 /* If the non-backedge arguments range is VR_VARYING then
9207 we can still try recording a simple equivalence. */
9208 if (vr_arg.type == VR_VARYING)
9210 vr_arg.type = VR_RANGE;
9211 vr_arg.min = arg;
9212 vr_arg.max = arg;
9213 vr_arg.equiv = NULL;
9217 else
9219 if (TREE_OVERFLOW_P (arg))
9220 arg = drop_tree_overflow (arg);
9222 vr_arg.type = VR_RANGE;
9223 vr_arg.min = arg;
9224 vr_arg.max = arg;
9225 vr_arg.equiv = NULL;
9228 if (dump_file && (dump_flags & TDF_DETAILS))
9230 fprintf (dump_file, "\t");
9231 print_generic_expr (dump_file, arg, dump_flags);
9232 fprintf (dump_file, ": ");
9233 dump_value_range (dump_file, &vr_arg);
9234 fprintf (dump_file, "\n");
9237 if (first)
9238 copy_value_range (vr_result, &vr_arg);
9239 else
9240 vrp_meet (vr_result, &vr_arg);
9241 first = false;
9243 if (vr_result->type == VR_VARYING)
9244 break;
9248 if (vr_result->type == VR_VARYING)
9249 goto varying;
9250 else if (vr_result->type == VR_UNDEFINED)
9251 goto update_range;
9253 old_edges = vr_phi_edge_counts[SSA_NAME_VERSION (lhs)];
9254 vr_phi_edge_counts[SSA_NAME_VERSION (lhs)] = edges;
9256 /* To prevent infinite iterations in the algorithm, derive ranges
9257 when the new value is slightly bigger or smaller than the
9258 previous one. We don't do this if we have seen a new executable
9259 edge; this helps us avoid an overflow infinity for conditionals
9260 which are not in a loop. If the old value-range was VR_UNDEFINED
9261 use the updated range and iterate one more time. If we will not
9262 simulate this PHI again via the backedge allow us to iterate. */
9263 if (edges > 0
9264 && gimple_phi_num_args (phi) > 1
9265 && edges == old_edges
9266 && lhs_vr->type != VR_UNDEFINED
9267 && may_simulate_backedge_again)
9269 /* Compare old and new ranges, fall back to varying if the
9270 values are not comparable. */
9271 int cmp_min = compare_values (lhs_vr->min, vr_result->min);
9272 if (cmp_min == -2)
9273 goto varying;
9274 int cmp_max = compare_values (lhs_vr->max, vr_result->max);
9275 if (cmp_max == -2)
9276 goto varying;
9278 /* For non VR_RANGE or for pointers fall back to varying if
9279 the range changed. */
9280 if ((lhs_vr->type != VR_RANGE || vr_result->type != VR_RANGE
9281 || POINTER_TYPE_P (TREE_TYPE (lhs)))
9282 && (cmp_min != 0 || cmp_max != 0))
9283 goto varying;
9285 /* If the new minimum is larger than the previous one
9286 retain the old value. If the new minimum value is smaller
9287 than the previous one and not -INF go all the way to -INF + 1.
9288 In the first case, to avoid infinite bouncing between different
9289 minimums, and in the other case to avoid iterating millions of
9290 times to reach -INF. Going to -INF + 1 also lets the following
9291 iteration compute whether there will be any overflow, at the
9292 expense of one additional iteration. */
9293 if (cmp_min < 0)
9294 vr_result->min = lhs_vr->min;
9295 else if (cmp_min > 0
9296 && !vrp_val_is_min (vr_result->min))
9297 vr_result->min
9298 = int_const_binop (PLUS_EXPR,
9299 vrp_val_min (TREE_TYPE (vr_result->min)),
9300 build_int_cst (TREE_TYPE (vr_result->min), 1));
9302 /* Similarly for the maximum value. */
9303 if (cmp_max > 0)
9304 vr_result->max = lhs_vr->max;
9305 else if (cmp_max < 0
9306 && !vrp_val_is_max (vr_result->max))
9307 vr_result->max
9308 = int_const_binop (MINUS_EXPR,
9309 vrp_val_max (TREE_TYPE (vr_result->min)),
9310 build_int_cst (TREE_TYPE (vr_result->min), 1));
9312 /* If we dropped either bound to +-INF then if this is a loop
9313 PHI node SCEV may known more about its value-range. */
9314 if (cmp_min > 0 || cmp_min < 0
9315 || cmp_max < 0 || cmp_max > 0)
9316 goto scev_check;
9318 goto infinite_check;
9321 goto update_range;
9323 varying:
9324 set_value_range_to_varying (vr_result);
9326 scev_check:
9327 /* If this is a loop PHI node SCEV may known more about its value-range.
9328 scev_check can be reached from two paths, one is a fall through from above
9329 "varying" label, the other is direct goto from code block which tries to
9330 avoid infinite simulation. */
9331 if ((l = loop_containing_stmt (phi))
9332 && l->header == gimple_bb (phi))
9333 adjust_range_with_scev (vr_result, l, phi, lhs);
9335 infinite_check:
9336 /* If we will end up with a (-INF, +INF) range, set it to
9337 VARYING. Same if the previous max value was invalid for
9338 the type and we end up with vr_result.min > vr_result.max. */
9339 if ((vr_result->type == VR_RANGE || vr_result->type == VR_ANTI_RANGE)
9340 && !((vrp_val_is_max (vr_result->max) && vrp_val_is_min (vr_result->min))
9341 || compare_values (vr_result->min, vr_result->max) > 0))
9343 else
9344 set_value_range_to_varying (vr_result);
9346 /* If the new range is different than the previous value, keep
9347 iterating. */
9348 update_range:
9349 return;
9352 /* Visit all arguments for PHI node PHI that flow through executable
9353 edges. If a valid value range can be derived from all the incoming
9354 value ranges, set a new range for the LHS of PHI. */
9356 static enum ssa_prop_result
9357 vrp_visit_phi_node (gphi *phi)
9359 tree lhs = PHI_RESULT (phi);
9360 value_range vr_result = VR_INITIALIZER;
9361 extract_range_from_phi_node (phi, &vr_result);
9362 if (update_value_range (lhs, &vr_result))
9364 if (dump_file && (dump_flags & TDF_DETAILS))
9366 fprintf (dump_file, "Found new range for ");
9367 print_generic_expr (dump_file, lhs, 0);
9368 fprintf (dump_file, ": ");
9369 dump_value_range (dump_file, &vr_result);
9370 fprintf (dump_file, "\n");
9373 if (vr_result.type == VR_VARYING)
9374 return SSA_PROP_VARYING;
9376 return SSA_PROP_INTERESTING;
9379 /* Nothing changed, don't add outgoing edges. */
9380 return SSA_PROP_NOT_INTERESTING;
9383 /* Simplify boolean operations if the source is known
9384 to be already a boolean. */
9385 static bool
9386 simplify_truth_ops_using_ranges (gimple_stmt_iterator *gsi, gimple *stmt)
9388 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
9389 tree lhs, op0, op1;
9390 bool need_conversion;
9392 /* We handle only !=/== case here. */
9393 gcc_assert (rhs_code == EQ_EXPR || rhs_code == NE_EXPR);
9395 op0 = gimple_assign_rhs1 (stmt);
9396 if (!op_with_boolean_value_range_p (op0))
9397 return false;
9399 op1 = gimple_assign_rhs2 (stmt);
9400 if (!op_with_boolean_value_range_p (op1))
9401 return false;
9403 /* Reduce number of cases to handle to NE_EXPR. As there is no
9404 BIT_XNOR_EXPR we cannot replace A == B with a single statement. */
9405 if (rhs_code == EQ_EXPR)
9407 if (TREE_CODE (op1) == INTEGER_CST)
9408 op1 = int_const_binop (BIT_XOR_EXPR, op1,
9409 build_int_cst (TREE_TYPE (op1), 1));
9410 else
9411 return false;
9414 lhs = gimple_assign_lhs (stmt);
9415 need_conversion
9416 = !useless_type_conversion_p (TREE_TYPE (lhs), TREE_TYPE (op0));
9418 /* Make sure to not sign-extend a 1-bit 1 when converting the result. */
9419 if (need_conversion
9420 && !TYPE_UNSIGNED (TREE_TYPE (op0))
9421 && TYPE_PRECISION (TREE_TYPE (op0)) == 1
9422 && TYPE_PRECISION (TREE_TYPE (lhs)) > 1)
9423 return false;
9425 /* For A != 0 we can substitute A itself. */
9426 if (integer_zerop (op1))
9427 gimple_assign_set_rhs_with_ops (gsi,
9428 need_conversion
9429 ? NOP_EXPR : TREE_CODE (op0), op0);
9430 /* For A != B we substitute A ^ B. Either with conversion. */
9431 else if (need_conversion)
9433 tree tem = make_ssa_name (TREE_TYPE (op0));
9434 gassign *newop
9435 = gimple_build_assign (tem, BIT_XOR_EXPR, op0, op1);
9436 gsi_insert_before (gsi, newop, GSI_SAME_STMT);
9437 if (INTEGRAL_TYPE_P (TREE_TYPE (tem))
9438 && TYPE_PRECISION (TREE_TYPE (tem)) > 1)
9439 set_range_info (tem, VR_RANGE,
9440 wi::zero (TYPE_PRECISION (TREE_TYPE (tem))),
9441 wi::one (TYPE_PRECISION (TREE_TYPE (tem))));
9442 gimple_assign_set_rhs_with_ops (gsi, NOP_EXPR, tem);
9444 /* Or without. */
9445 else
9446 gimple_assign_set_rhs_with_ops (gsi, BIT_XOR_EXPR, op0, op1);
9447 update_stmt (gsi_stmt (*gsi));
9448 fold_stmt (gsi, follow_single_use_edges);
9450 return true;
9453 /* Simplify a division or modulo operator to a right shift or bitwise and
9454 if the first operand is unsigned or is greater than zero and the second
9455 operand is an exact power of two. For TRUNC_MOD_EXPR op0 % op1 with
9456 constant op1 (op1min = op1) or with op1 in [op1min, op1max] range,
9457 optimize it into just op0 if op0's range is known to be a subset of
9458 [-op1min + 1, op1min - 1] for signed and [0, op1min - 1] for unsigned
9459 modulo. */
9461 static bool
9462 simplify_div_or_mod_using_ranges (gimple_stmt_iterator *gsi, gimple *stmt)
9464 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
9465 tree val = NULL;
9466 tree op0 = gimple_assign_rhs1 (stmt);
9467 tree op1 = gimple_assign_rhs2 (stmt);
9468 tree op0min = NULL_TREE, op0max = NULL_TREE;
9469 tree op1min = op1;
9470 value_range *vr = NULL;
9472 if (TREE_CODE (op0) == INTEGER_CST)
9474 op0min = op0;
9475 op0max = op0;
9477 else
9479 vr = get_value_range (op0);
9480 if (range_int_cst_p (vr))
9482 op0min = vr->min;
9483 op0max = vr->max;
9487 if (rhs_code == TRUNC_MOD_EXPR
9488 && TREE_CODE (op1) == SSA_NAME)
9490 value_range *vr1 = get_value_range (op1);
9491 if (range_int_cst_p (vr1))
9492 op1min = vr1->min;
9494 if (rhs_code == TRUNC_MOD_EXPR
9495 && TREE_CODE (op1min) == INTEGER_CST
9496 && tree_int_cst_sgn (op1min) == 1
9497 && op0max
9498 && tree_int_cst_lt (op0max, op1min))
9500 if (TYPE_UNSIGNED (TREE_TYPE (op0))
9501 || tree_int_cst_sgn (op0min) >= 0
9502 || tree_int_cst_lt (fold_unary (NEGATE_EXPR, TREE_TYPE (op1min), op1min),
9503 op0min))
9505 /* If op0 already has the range op0 % op1 has,
9506 then TRUNC_MOD_EXPR won't change anything. */
9507 gimple_assign_set_rhs_from_tree (gsi, op0);
9508 return true;
9512 if (TREE_CODE (op0) != SSA_NAME)
9513 return false;
9515 if (!integer_pow2p (op1))
9517 /* X % -Y can be only optimized into X % Y either if
9518 X is not INT_MIN, or Y is not -1. Fold it now, as after
9519 remove_range_assertions the range info might be not available
9520 anymore. */
9521 if (rhs_code == TRUNC_MOD_EXPR
9522 && fold_stmt (gsi, follow_single_use_edges))
9523 return true;
9524 return false;
9527 if (TYPE_UNSIGNED (TREE_TYPE (op0)))
9528 val = integer_one_node;
9529 else
9531 bool sop = false;
9533 val = compare_range_with_value (GE_EXPR, vr, integer_zero_node, &sop);
9535 if (val
9536 && sop
9537 && integer_onep (val)
9538 && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC))
9540 location_t location;
9542 if (!gimple_has_location (stmt))
9543 location = input_location;
9544 else
9545 location = gimple_location (stmt);
9546 warning_at (location, OPT_Wstrict_overflow,
9547 "assuming signed overflow does not occur when "
9548 "simplifying %</%> or %<%%%> to %<>>%> or %<&%>");
9552 if (val && integer_onep (val))
9554 tree t;
9556 if (rhs_code == TRUNC_DIV_EXPR)
9558 t = build_int_cst (integer_type_node, tree_log2 (op1));
9559 gimple_assign_set_rhs_code (stmt, RSHIFT_EXPR);
9560 gimple_assign_set_rhs1 (stmt, op0);
9561 gimple_assign_set_rhs2 (stmt, t);
9563 else
9565 t = build_int_cst (TREE_TYPE (op1), 1);
9566 t = int_const_binop (MINUS_EXPR, op1, t);
9567 t = fold_convert (TREE_TYPE (op0), t);
9569 gimple_assign_set_rhs_code (stmt, BIT_AND_EXPR);
9570 gimple_assign_set_rhs1 (stmt, op0);
9571 gimple_assign_set_rhs2 (stmt, t);
9574 update_stmt (stmt);
9575 fold_stmt (gsi, follow_single_use_edges);
9576 return true;
9579 return false;
9582 /* Simplify a min or max if the ranges of the two operands are
9583 disjoint. Return true if we do simplify. */
9585 static bool
9586 simplify_min_or_max_using_ranges (gimple_stmt_iterator *gsi, gimple *stmt)
9588 tree op0 = gimple_assign_rhs1 (stmt);
9589 tree op1 = gimple_assign_rhs2 (stmt);
9590 bool sop = false;
9591 tree val;
9593 val = (vrp_evaluate_conditional_warnv_with_ops_using_ranges
9594 (LE_EXPR, op0, op1, &sop));
9595 if (!val)
9597 sop = false;
9598 val = (vrp_evaluate_conditional_warnv_with_ops_using_ranges
9599 (LT_EXPR, op0, op1, &sop));
9602 if (val)
9604 if (sop && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC))
9606 location_t location;
9608 if (!gimple_has_location (stmt))
9609 location = input_location;
9610 else
9611 location = gimple_location (stmt);
9612 warning_at (location, OPT_Wstrict_overflow,
9613 "assuming signed overflow does not occur when "
9614 "simplifying %<min/max (X,Y)%> to %<X%> or %<Y%>");
9617 /* VAL == TRUE -> OP0 < or <= op1
9618 VAL == FALSE -> OP0 > or >= op1. */
9619 tree res = ((gimple_assign_rhs_code (stmt) == MAX_EXPR)
9620 == integer_zerop (val)) ? op0 : op1;
9621 gimple_assign_set_rhs_from_tree (gsi, res);
9622 return true;
9625 return false;
9628 /* If the operand to an ABS_EXPR is >= 0, then eliminate the
9629 ABS_EXPR. If the operand is <= 0, then simplify the
9630 ABS_EXPR into a NEGATE_EXPR. */
9632 static bool
9633 simplify_abs_using_ranges (gimple_stmt_iterator *gsi, gimple *stmt)
9635 tree op = gimple_assign_rhs1 (stmt);
9636 value_range *vr = get_value_range (op);
9638 if (vr)
9640 tree val = NULL;
9641 bool sop = false;
9643 val = compare_range_with_value (LE_EXPR, vr, integer_zero_node, &sop);
9644 if (!val)
9646 /* The range is neither <= 0 nor > 0. Now see if it is
9647 either < 0 or >= 0. */
9648 sop = false;
9649 val = compare_range_with_value (LT_EXPR, vr, integer_zero_node,
9650 &sop);
9653 if (val)
9655 if (sop && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC))
9657 location_t location;
9659 if (!gimple_has_location (stmt))
9660 location = input_location;
9661 else
9662 location = gimple_location (stmt);
9663 warning_at (location, OPT_Wstrict_overflow,
9664 "assuming signed overflow does not occur when "
9665 "simplifying %<abs (X)%> to %<X%> or %<-X%>");
9668 gimple_assign_set_rhs1 (stmt, op);
9669 if (integer_zerop (val))
9670 gimple_assign_set_rhs_code (stmt, SSA_NAME);
9671 else
9672 gimple_assign_set_rhs_code (stmt, NEGATE_EXPR);
9673 update_stmt (stmt);
9674 fold_stmt (gsi, follow_single_use_edges);
9675 return true;
9679 return false;
9682 /* Optimize away redundant BIT_AND_EXPR and BIT_IOR_EXPR.
9683 If all the bits that are being cleared by & are already
9684 known to be zero from VR, or all the bits that are being
9685 set by | are already known to be one from VR, the bit
9686 operation is redundant. */
9688 static bool
9689 simplify_bit_ops_using_ranges (gimple_stmt_iterator *gsi, gimple *stmt)
9691 tree op0 = gimple_assign_rhs1 (stmt);
9692 tree op1 = gimple_assign_rhs2 (stmt);
9693 tree op = NULL_TREE;
9694 value_range vr0 = VR_INITIALIZER;
9695 value_range vr1 = VR_INITIALIZER;
9696 wide_int may_be_nonzero0, may_be_nonzero1;
9697 wide_int must_be_nonzero0, must_be_nonzero1;
9698 wide_int mask;
9700 if (TREE_CODE (op0) == SSA_NAME)
9701 vr0 = *(get_value_range (op0));
9702 else if (is_gimple_min_invariant (op0))
9703 set_value_range_to_value (&vr0, op0, NULL);
9704 else
9705 return false;
9707 if (TREE_CODE (op1) == SSA_NAME)
9708 vr1 = *(get_value_range (op1));
9709 else if (is_gimple_min_invariant (op1))
9710 set_value_range_to_value (&vr1, op1, NULL);
9711 else
9712 return false;
9714 if (!zero_nonzero_bits_from_vr (TREE_TYPE (op0), &vr0, &may_be_nonzero0,
9715 &must_be_nonzero0))
9716 return false;
9717 if (!zero_nonzero_bits_from_vr (TREE_TYPE (op1), &vr1, &may_be_nonzero1,
9718 &must_be_nonzero1))
9719 return false;
9721 switch (gimple_assign_rhs_code (stmt))
9723 case BIT_AND_EXPR:
9724 mask = may_be_nonzero0.and_not (must_be_nonzero1);
9725 if (mask == 0)
9727 op = op0;
9728 break;
9730 mask = may_be_nonzero1.and_not (must_be_nonzero0);
9731 if (mask == 0)
9733 op = op1;
9734 break;
9736 break;
9737 case BIT_IOR_EXPR:
9738 mask = may_be_nonzero0.and_not (must_be_nonzero1);
9739 if (mask == 0)
9741 op = op1;
9742 break;
9744 mask = may_be_nonzero1.and_not (must_be_nonzero0);
9745 if (mask == 0)
9747 op = op0;
9748 break;
9750 break;
9751 default:
9752 gcc_unreachable ();
9755 if (op == NULL_TREE)
9756 return false;
9758 gimple_assign_set_rhs_with_ops (gsi, TREE_CODE (op), op);
9759 update_stmt (gsi_stmt (*gsi));
9760 return true;
9763 /* We are comparing trees OP0 and OP1 using COND_CODE. OP0 has
9764 a known value range VR.
9766 If there is one and only one value which will satisfy the
9767 conditional, then return that value. Else return NULL.
9769 If signed overflow must be undefined for the value to satisfy
9770 the conditional, then set *STRICT_OVERFLOW_P to true. */
9772 static tree
9773 test_for_singularity (enum tree_code cond_code, tree op0,
9774 tree op1, value_range *vr,
9775 bool *strict_overflow_p)
9777 tree min = NULL;
9778 tree max = NULL;
9780 /* Extract minimum/maximum values which satisfy the conditional as it was
9781 written. */
9782 if (cond_code == LE_EXPR || cond_code == LT_EXPR)
9784 /* This should not be negative infinity; there is no overflow
9785 here. */
9786 min = TYPE_MIN_VALUE (TREE_TYPE (op0));
9788 max = op1;
9789 if (cond_code == LT_EXPR && !is_overflow_infinity (max))
9791 tree one = build_int_cst (TREE_TYPE (op0), 1);
9792 max = fold_build2 (MINUS_EXPR, TREE_TYPE (op0), max, one);
9793 if (EXPR_P (max))
9794 TREE_NO_WARNING (max) = 1;
9797 else if (cond_code == GE_EXPR || cond_code == GT_EXPR)
9799 /* This should not be positive infinity; there is no overflow
9800 here. */
9801 max = TYPE_MAX_VALUE (TREE_TYPE (op0));
9803 min = op1;
9804 if (cond_code == GT_EXPR && !is_overflow_infinity (min))
9806 tree one = build_int_cst (TREE_TYPE (op0), 1);
9807 min = fold_build2 (PLUS_EXPR, TREE_TYPE (op0), min, one);
9808 if (EXPR_P (min))
9809 TREE_NO_WARNING (min) = 1;
9813 /* Now refine the minimum and maximum values using any
9814 value range information we have for op0. */
9815 if (min && max)
9817 if (compare_values (vr->min, min) == 1)
9818 min = vr->min;
9819 if (compare_values (vr->max, max) == -1)
9820 max = vr->max;
9822 /* If the new min/max values have converged to a single value,
9823 then there is only one value which can satisfy the condition,
9824 return that value. */
9825 if (operand_equal_p (min, max, 0) && is_gimple_min_invariant (min))
9827 if ((cond_code == LE_EXPR || cond_code == LT_EXPR)
9828 && is_overflow_infinity (vr->max))
9829 *strict_overflow_p = true;
9830 if ((cond_code == GE_EXPR || cond_code == GT_EXPR)
9831 && is_overflow_infinity (vr->min))
9832 *strict_overflow_p = true;
9834 return min;
9837 return NULL;
9840 /* Return whether the value range *VR fits in an integer type specified
9841 by PRECISION and UNSIGNED_P. */
9843 static bool
9844 range_fits_type_p (value_range *vr, unsigned dest_precision, signop dest_sgn)
9846 tree src_type;
9847 unsigned src_precision;
9848 widest_int tem;
9849 signop src_sgn;
9851 /* We can only handle integral and pointer types. */
9852 src_type = TREE_TYPE (vr->min);
9853 if (!INTEGRAL_TYPE_P (src_type)
9854 && !POINTER_TYPE_P (src_type))
9855 return false;
9857 /* An extension is fine unless VR is SIGNED and dest_sgn is UNSIGNED,
9858 and so is an identity transform. */
9859 src_precision = TYPE_PRECISION (TREE_TYPE (vr->min));
9860 src_sgn = TYPE_SIGN (src_type);
9861 if ((src_precision < dest_precision
9862 && !(dest_sgn == UNSIGNED && src_sgn == SIGNED))
9863 || (src_precision == dest_precision && src_sgn == dest_sgn))
9864 return true;
9866 /* Now we can only handle ranges with constant bounds. */
9867 if (vr->type != VR_RANGE
9868 || TREE_CODE (vr->min) != INTEGER_CST
9869 || TREE_CODE (vr->max) != INTEGER_CST)
9870 return false;
9872 /* For sign changes, the MSB of the wide_int has to be clear.
9873 An unsigned value with its MSB set cannot be represented by
9874 a signed wide_int, while a negative value cannot be represented
9875 by an unsigned wide_int. */
9876 if (src_sgn != dest_sgn
9877 && (wi::lts_p (vr->min, 0) || wi::lts_p (vr->max, 0)))
9878 return false;
9880 /* Then we can perform the conversion on both ends and compare
9881 the result for equality. */
9882 tem = wi::ext (wi::to_widest (vr->min), dest_precision, dest_sgn);
9883 if (tem != wi::to_widest (vr->min))
9884 return false;
9885 tem = wi::ext (wi::to_widest (vr->max), dest_precision, dest_sgn);
9886 if (tem != wi::to_widest (vr->max))
9887 return false;
9889 return true;
9892 /* Simplify a conditional using a relational operator to an equality
9893 test if the range information indicates only one value can satisfy
9894 the original conditional. */
9896 static bool
9897 simplify_cond_using_ranges (gcond *stmt)
9899 tree op0 = gimple_cond_lhs (stmt);
9900 tree op1 = gimple_cond_rhs (stmt);
9901 enum tree_code cond_code = gimple_cond_code (stmt);
9903 if (cond_code != NE_EXPR
9904 && cond_code != EQ_EXPR
9905 && TREE_CODE (op0) == SSA_NAME
9906 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
9907 && is_gimple_min_invariant (op1))
9909 value_range *vr = get_value_range (op0);
9911 /* If we have range information for OP0, then we might be
9912 able to simplify this conditional. */
9913 if (vr->type == VR_RANGE)
9915 enum warn_strict_overflow_code wc = WARN_STRICT_OVERFLOW_COMPARISON;
9916 bool sop = false;
9917 tree new_tree = test_for_singularity (cond_code, op0, op1, vr, &sop);
9919 if (new_tree
9920 && (!sop || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0))))
9922 if (dump_file)
9924 fprintf (dump_file, "Simplified relational ");
9925 print_gimple_stmt (dump_file, stmt, 0, 0);
9926 fprintf (dump_file, " into ");
9929 gimple_cond_set_code (stmt, EQ_EXPR);
9930 gimple_cond_set_lhs (stmt, op0);
9931 gimple_cond_set_rhs (stmt, new_tree);
9933 update_stmt (stmt);
9935 if (dump_file)
9937 print_gimple_stmt (dump_file, stmt, 0, 0);
9938 fprintf (dump_file, "\n");
9941 if (sop && issue_strict_overflow_warning (wc))
9943 location_t location = input_location;
9944 if (gimple_has_location (stmt))
9945 location = gimple_location (stmt);
9947 warning_at (location, OPT_Wstrict_overflow,
9948 "assuming signed overflow does not occur when "
9949 "simplifying conditional");
9952 return true;
9955 /* Try again after inverting the condition. We only deal
9956 with integral types here, so no need to worry about
9957 issues with inverting FP comparisons. */
9958 sop = false;
9959 new_tree = test_for_singularity
9960 (invert_tree_comparison (cond_code, false),
9961 op0, op1, vr, &sop);
9963 if (new_tree
9964 && (!sop || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0))))
9966 if (dump_file)
9968 fprintf (dump_file, "Simplified relational ");
9969 print_gimple_stmt (dump_file, stmt, 0, 0);
9970 fprintf (dump_file, " into ");
9973 gimple_cond_set_code (stmt, NE_EXPR);
9974 gimple_cond_set_lhs (stmt, op0);
9975 gimple_cond_set_rhs (stmt, new_tree);
9977 update_stmt (stmt);
9979 if (dump_file)
9981 print_gimple_stmt (dump_file, stmt, 0, 0);
9982 fprintf (dump_file, "\n");
9985 if (sop && issue_strict_overflow_warning (wc))
9987 location_t location = input_location;
9988 if (gimple_has_location (stmt))
9989 location = gimple_location (stmt);
9991 warning_at (location, OPT_Wstrict_overflow,
9992 "assuming signed overflow does not occur when "
9993 "simplifying conditional");
9996 return true;
10001 /* If we have a comparison of an SSA_NAME (OP0) against a constant,
10002 see if OP0 was set by a type conversion where the source of
10003 the conversion is another SSA_NAME with a range that fits
10004 into the range of OP0's type.
10006 If so, the conversion is redundant as the earlier SSA_NAME can be
10007 used for the comparison directly if we just massage the constant in the
10008 comparison. */
10009 if (TREE_CODE (op0) == SSA_NAME
10010 && TREE_CODE (op1) == INTEGER_CST)
10012 gimple *def_stmt = SSA_NAME_DEF_STMT (op0);
10013 tree innerop;
10015 if (!is_gimple_assign (def_stmt)
10016 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt)))
10017 return false;
10019 innerop = gimple_assign_rhs1 (def_stmt);
10021 if (TREE_CODE (innerop) == SSA_NAME
10022 && !POINTER_TYPE_P (TREE_TYPE (innerop))
10023 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (innerop)
10024 && desired_pro_or_demotion_p (TREE_TYPE (innerop), TREE_TYPE (op0)))
10026 value_range *vr = get_value_range (innerop);
10028 if (range_int_cst_p (vr)
10029 && range_fits_type_p (vr,
10030 TYPE_PRECISION (TREE_TYPE (op0)),
10031 TYPE_SIGN (TREE_TYPE (op0)))
10032 && int_fits_type_p (op1, TREE_TYPE (innerop))
10033 /* The range must not have overflowed, or if it did overflow
10034 we must not be wrapping/trapping overflow and optimizing
10035 with strict overflow semantics. */
10036 && ((!is_negative_overflow_infinity (vr->min)
10037 && !is_positive_overflow_infinity (vr->max))
10038 || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (innerop))))
10040 /* If the range overflowed and the user has asked for warnings
10041 when strict overflow semantics were used to optimize code,
10042 issue an appropriate warning. */
10043 if (cond_code != EQ_EXPR && cond_code != NE_EXPR
10044 && (is_negative_overflow_infinity (vr->min)
10045 || is_positive_overflow_infinity (vr->max))
10046 && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_CONDITIONAL))
10048 location_t location;
10050 if (!gimple_has_location (stmt))
10051 location = input_location;
10052 else
10053 location = gimple_location (stmt);
10054 warning_at (location, OPT_Wstrict_overflow,
10055 "assuming signed overflow does not occur when "
10056 "simplifying conditional");
10059 tree newconst = fold_convert (TREE_TYPE (innerop), op1);
10060 gimple_cond_set_lhs (stmt, innerop);
10061 gimple_cond_set_rhs (stmt, newconst);
10062 return true;
10067 return false;
10070 /* Simplify a switch statement using the value range of the switch
10071 argument. */
10073 static bool
10074 simplify_switch_using_ranges (gswitch *stmt)
10076 tree op = gimple_switch_index (stmt);
10077 value_range *vr = NULL;
10078 bool take_default;
10079 edge e;
10080 edge_iterator ei;
10081 size_t i = 0, j = 0, n, n2;
10082 tree vec2;
10083 switch_update su;
10084 size_t k = 1, l = 0;
10086 if (TREE_CODE (op) == SSA_NAME)
10088 vr = get_value_range (op);
10090 /* We can only handle integer ranges. */
10091 if ((vr->type != VR_RANGE
10092 && vr->type != VR_ANTI_RANGE)
10093 || symbolic_range_p (vr))
10094 return false;
10096 /* Find case label for min/max of the value range. */
10097 take_default = !find_case_label_ranges (stmt, vr, &i, &j, &k, &l);
10099 else if (TREE_CODE (op) == INTEGER_CST)
10101 take_default = !find_case_label_index (stmt, 1, op, &i);
10102 if (take_default)
10104 i = 1;
10105 j = 0;
10107 else
10109 j = i;
10112 else
10113 return false;
10115 n = gimple_switch_num_labels (stmt);
10117 /* We can truncate the case label ranges that partially overlap with OP's
10118 value range. */
10119 size_t min_idx = 1, max_idx = 0;
10120 if (vr != NULL)
10121 find_case_label_range (stmt, vr->min, vr->max, &min_idx, &max_idx);
10122 if (min_idx <= max_idx)
10124 tree min_label = gimple_switch_label (stmt, min_idx);
10125 tree max_label = gimple_switch_label (stmt, max_idx);
10127 /* Avoid changing the type of the case labels when truncating. */
10128 tree case_label_type = TREE_TYPE (CASE_LOW (min_label));
10129 tree vr_min = fold_convert (case_label_type, vr->min);
10130 tree vr_max = fold_convert (case_label_type, vr->max);
10132 if (vr->type == VR_RANGE)
10134 /* If OP's value range is [2,8] and the low label range is
10135 0 ... 3, truncate the label's range to 2 .. 3. */
10136 if (tree_int_cst_compare (CASE_LOW (min_label), vr_min) < 0
10137 && CASE_HIGH (min_label) != NULL_TREE
10138 && tree_int_cst_compare (CASE_HIGH (min_label), vr_min) >= 0)
10139 CASE_LOW (min_label) = vr_min;
10141 /* If OP's value range is [2,8] and the high label range is
10142 7 ... 10, truncate the label's range to 7 .. 8. */
10143 if (tree_int_cst_compare (CASE_LOW (max_label), vr_max) <= 0
10144 && CASE_HIGH (max_label) != NULL_TREE
10145 && tree_int_cst_compare (CASE_HIGH (max_label), vr_max) > 0)
10146 CASE_HIGH (max_label) = vr_max;
10148 else if (vr->type == VR_ANTI_RANGE)
10150 tree one_cst = build_one_cst (case_label_type);
10152 if (min_label == max_label)
10154 /* If OP's value range is ~[7,8] and the label's range is
10155 7 ... 10, truncate the label's range to 9 ... 10. */
10156 if (tree_int_cst_compare (CASE_LOW (min_label), vr_min) == 0
10157 && CASE_HIGH (min_label) != NULL_TREE
10158 && tree_int_cst_compare (CASE_HIGH (min_label), vr_max) > 0)
10159 CASE_LOW (min_label)
10160 = int_const_binop (PLUS_EXPR, vr_max, one_cst);
10162 /* If OP's value range is ~[7,8] and the label's range is
10163 5 ... 8, truncate the label's range to 5 ... 6. */
10164 if (tree_int_cst_compare (CASE_LOW (min_label), vr_min) < 0
10165 && CASE_HIGH (min_label) != NULL_TREE
10166 && tree_int_cst_compare (CASE_HIGH (min_label), vr_max) == 0)
10167 CASE_HIGH (min_label)
10168 = int_const_binop (MINUS_EXPR, vr_min, one_cst);
10170 else
10172 /* If OP's value range is ~[2,8] and the low label range is
10173 0 ... 3, truncate the label's range to 0 ... 1. */
10174 if (tree_int_cst_compare (CASE_LOW (min_label), vr_min) < 0
10175 && CASE_HIGH (min_label) != NULL_TREE
10176 && tree_int_cst_compare (CASE_HIGH (min_label), vr_min) >= 0)
10177 CASE_HIGH (min_label)
10178 = int_const_binop (MINUS_EXPR, vr_min, one_cst);
10180 /* If OP's value range is ~[2,8] and the high label range is
10181 7 ... 10, truncate the label's range to 9 ... 10. */
10182 if (tree_int_cst_compare (CASE_LOW (max_label), vr_max) <= 0
10183 && CASE_HIGH (max_label) != NULL_TREE
10184 && tree_int_cst_compare (CASE_HIGH (max_label), vr_max) > 0)
10185 CASE_LOW (max_label)
10186 = int_const_binop (PLUS_EXPR, vr_max, one_cst);
10190 /* Canonicalize singleton case ranges. */
10191 if (tree_int_cst_equal (CASE_LOW (min_label), CASE_HIGH (min_label)))
10192 CASE_HIGH (min_label) = NULL_TREE;
10193 if (tree_int_cst_equal (CASE_LOW (max_label), CASE_HIGH (max_label)))
10194 CASE_HIGH (max_label) = NULL_TREE;
10197 /* We can also eliminate case labels that lie completely outside OP's value
10198 range. */
10200 /* Bail out if this is just all edges taken. */
10201 if (i == 1
10202 && j == n - 1
10203 && take_default)
10204 return false;
10206 /* Build a new vector of taken case labels. */
10207 vec2 = make_tree_vec (j - i + 1 + l - k + 1 + (int)take_default);
10208 n2 = 0;
10210 /* Add the default edge, if necessary. */
10211 if (take_default)
10212 TREE_VEC_ELT (vec2, n2++) = gimple_switch_default_label (stmt);
10214 for (; i <= j; ++i, ++n2)
10215 TREE_VEC_ELT (vec2, n2) = gimple_switch_label (stmt, i);
10217 for (; k <= l; ++k, ++n2)
10218 TREE_VEC_ELT (vec2, n2) = gimple_switch_label (stmt, k);
10220 /* Mark needed edges. */
10221 for (i = 0; i < n2; ++i)
10223 e = find_edge (gimple_bb (stmt),
10224 label_to_block (CASE_LABEL (TREE_VEC_ELT (vec2, i))));
10225 e->aux = (void *)-1;
10228 /* Queue not needed edges for later removal. */
10229 FOR_EACH_EDGE (e, ei, gimple_bb (stmt)->succs)
10231 if (e->aux == (void *)-1)
10233 e->aux = NULL;
10234 continue;
10237 if (dump_file && (dump_flags & TDF_DETAILS))
10239 fprintf (dump_file, "removing unreachable case label\n");
10241 to_remove_edges.safe_push (e);
10242 e->flags &= ~EDGE_EXECUTABLE;
10245 /* And queue an update for the stmt. */
10246 su.stmt = stmt;
10247 su.vec = vec2;
10248 to_update_switch_stmts.safe_push (su);
10249 return false;
10252 /* Simplify an integral conversion from an SSA name in STMT. */
10254 static bool
10255 simplify_conversion_using_ranges (gimple_stmt_iterator *gsi, gimple *stmt)
10257 tree innerop, middleop, finaltype;
10258 gimple *def_stmt;
10259 signop inner_sgn, middle_sgn, final_sgn;
10260 unsigned inner_prec, middle_prec, final_prec;
10261 widest_int innermin, innermed, innermax, middlemin, middlemed, middlemax;
10263 finaltype = TREE_TYPE (gimple_assign_lhs (stmt));
10264 if (!INTEGRAL_TYPE_P (finaltype))
10265 return false;
10266 middleop = gimple_assign_rhs1 (stmt);
10267 def_stmt = SSA_NAME_DEF_STMT (middleop);
10268 if (!is_gimple_assign (def_stmt)
10269 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt)))
10270 return false;
10271 innerop = gimple_assign_rhs1 (def_stmt);
10272 if (TREE_CODE (innerop) != SSA_NAME
10273 || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (innerop))
10274 return false;
10276 /* Get the value-range of the inner operand. Use get_range_info in
10277 case innerop was created during substitute-and-fold. */
10278 wide_int imin, imax;
10279 if (!INTEGRAL_TYPE_P (TREE_TYPE (innerop))
10280 || get_range_info (innerop, &imin, &imax) != VR_RANGE)
10281 return false;
10282 innermin = widest_int::from (imin, TYPE_SIGN (TREE_TYPE (innerop)));
10283 innermax = widest_int::from (imax, TYPE_SIGN (TREE_TYPE (innerop)));
10285 /* Simulate the conversion chain to check if the result is equal if
10286 the middle conversion is removed. */
10287 inner_prec = TYPE_PRECISION (TREE_TYPE (innerop));
10288 middle_prec = TYPE_PRECISION (TREE_TYPE (middleop));
10289 final_prec = TYPE_PRECISION (finaltype);
10291 /* If the first conversion is not injective, the second must not
10292 be widening. */
10293 if (wi::gtu_p (innermax - innermin,
10294 wi::mask <widest_int> (middle_prec, false))
10295 && middle_prec < final_prec)
10296 return false;
10297 /* We also want a medium value so that we can track the effect that
10298 narrowing conversions with sign change have. */
10299 inner_sgn = TYPE_SIGN (TREE_TYPE (innerop));
10300 if (inner_sgn == UNSIGNED)
10301 innermed = wi::shifted_mask <widest_int> (1, inner_prec - 1, false);
10302 else
10303 innermed = 0;
10304 if (wi::cmp (innermin, innermed, inner_sgn) >= 0
10305 || wi::cmp (innermed, innermax, inner_sgn) >= 0)
10306 innermed = innermin;
10308 middle_sgn = TYPE_SIGN (TREE_TYPE (middleop));
10309 middlemin = wi::ext (innermin, middle_prec, middle_sgn);
10310 middlemed = wi::ext (innermed, middle_prec, middle_sgn);
10311 middlemax = wi::ext (innermax, middle_prec, middle_sgn);
10313 /* Require that the final conversion applied to both the original
10314 and the intermediate range produces the same result. */
10315 final_sgn = TYPE_SIGN (finaltype);
10316 if (wi::ext (middlemin, final_prec, final_sgn)
10317 != wi::ext (innermin, final_prec, final_sgn)
10318 || wi::ext (middlemed, final_prec, final_sgn)
10319 != wi::ext (innermed, final_prec, final_sgn)
10320 || wi::ext (middlemax, final_prec, final_sgn)
10321 != wi::ext (innermax, final_prec, final_sgn))
10322 return false;
10324 gimple_assign_set_rhs1 (stmt, innerop);
10325 fold_stmt (gsi, follow_single_use_edges);
10326 return true;
10329 /* Simplify a conversion from integral SSA name to float in STMT. */
10331 static bool
10332 simplify_float_conversion_using_ranges (gimple_stmt_iterator *gsi,
10333 gimple *stmt)
10335 tree rhs1 = gimple_assign_rhs1 (stmt);
10336 value_range *vr = get_value_range (rhs1);
10337 machine_mode fltmode = TYPE_MODE (TREE_TYPE (gimple_assign_lhs (stmt)));
10338 machine_mode mode;
10339 tree tem;
10340 gassign *conv;
10342 /* We can only handle constant ranges. */
10343 if (vr->type != VR_RANGE
10344 || TREE_CODE (vr->min) != INTEGER_CST
10345 || TREE_CODE (vr->max) != INTEGER_CST)
10346 return false;
10348 /* First check if we can use a signed type in place of an unsigned. */
10349 if (TYPE_UNSIGNED (TREE_TYPE (rhs1))
10350 && (can_float_p (fltmode, TYPE_MODE (TREE_TYPE (rhs1)), 0)
10351 != CODE_FOR_nothing)
10352 && range_fits_type_p (vr, TYPE_PRECISION (TREE_TYPE (rhs1)), SIGNED))
10353 mode = TYPE_MODE (TREE_TYPE (rhs1));
10354 /* If we can do the conversion in the current input mode do nothing. */
10355 else if (can_float_p (fltmode, TYPE_MODE (TREE_TYPE (rhs1)),
10356 TYPE_UNSIGNED (TREE_TYPE (rhs1))) != CODE_FOR_nothing)
10357 return false;
10358 /* Otherwise search for a mode we can use, starting from the narrowest
10359 integer mode available. */
10360 else
10362 mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
10365 /* If we cannot do a signed conversion to float from mode
10366 or if the value-range does not fit in the signed type
10367 try with a wider mode. */
10368 if (can_float_p (fltmode, mode, 0) != CODE_FOR_nothing
10369 && range_fits_type_p (vr, GET_MODE_PRECISION (mode), SIGNED))
10370 break;
10372 mode = GET_MODE_WIDER_MODE (mode);
10373 /* But do not widen the input. Instead leave that to the
10374 optabs expansion code. */
10375 if (GET_MODE_PRECISION (mode) > TYPE_PRECISION (TREE_TYPE (rhs1)))
10376 return false;
10378 while (mode != VOIDmode);
10379 if (mode == VOIDmode)
10380 return false;
10383 /* It works, insert a truncation or sign-change before the
10384 float conversion. */
10385 tem = make_ssa_name (build_nonstandard_integer_type
10386 (GET_MODE_PRECISION (mode), 0));
10387 conv = gimple_build_assign (tem, NOP_EXPR, rhs1);
10388 gsi_insert_before (gsi, conv, GSI_SAME_STMT);
10389 gimple_assign_set_rhs1 (stmt, tem);
10390 fold_stmt (gsi, follow_single_use_edges);
10392 return true;
10395 /* Simplify an internal fn call using ranges if possible. */
10397 static bool
10398 simplify_internal_call_using_ranges (gimple_stmt_iterator *gsi, gimple *stmt)
10400 enum tree_code subcode;
10401 bool is_ubsan = false;
10402 bool ovf = false;
10403 switch (gimple_call_internal_fn (stmt))
10405 case IFN_UBSAN_CHECK_ADD:
10406 subcode = PLUS_EXPR;
10407 is_ubsan = true;
10408 break;
10409 case IFN_UBSAN_CHECK_SUB:
10410 subcode = MINUS_EXPR;
10411 is_ubsan = true;
10412 break;
10413 case IFN_UBSAN_CHECK_MUL:
10414 subcode = MULT_EXPR;
10415 is_ubsan = true;
10416 break;
10417 case IFN_ADD_OVERFLOW:
10418 subcode = PLUS_EXPR;
10419 break;
10420 case IFN_SUB_OVERFLOW:
10421 subcode = MINUS_EXPR;
10422 break;
10423 case IFN_MUL_OVERFLOW:
10424 subcode = MULT_EXPR;
10425 break;
10426 default:
10427 return false;
10430 tree op0 = gimple_call_arg (stmt, 0);
10431 tree op1 = gimple_call_arg (stmt, 1);
10432 tree type;
10433 if (is_ubsan)
10435 type = TREE_TYPE (op0);
10436 if (VECTOR_TYPE_P (type))
10437 return false;
10439 else if (gimple_call_lhs (stmt) == NULL_TREE)
10440 return false;
10441 else
10442 type = TREE_TYPE (TREE_TYPE (gimple_call_lhs (stmt)));
10443 if (!check_for_binary_op_overflow (subcode, type, op0, op1, &ovf)
10444 || (is_ubsan && ovf))
10445 return false;
10447 gimple *g;
10448 location_t loc = gimple_location (stmt);
10449 if (is_ubsan)
10450 g = gimple_build_assign (gimple_call_lhs (stmt), subcode, op0, op1);
10451 else
10453 int prec = TYPE_PRECISION (type);
10454 tree utype = type;
10455 if (ovf
10456 || !useless_type_conversion_p (type, TREE_TYPE (op0))
10457 || !useless_type_conversion_p (type, TREE_TYPE (op1)))
10458 utype = build_nonstandard_integer_type (prec, 1);
10459 if (TREE_CODE (op0) == INTEGER_CST)
10460 op0 = fold_convert (utype, op0);
10461 else if (!useless_type_conversion_p (utype, TREE_TYPE (op0)))
10463 g = gimple_build_assign (make_ssa_name (utype), NOP_EXPR, op0);
10464 gimple_set_location (g, loc);
10465 gsi_insert_before (gsi, g, GSI_SAME_STMT);
10466 op0 = gimple_assign_lhs (g);
10468 if (TREE_CODE (op1) == INTEGER_CST)
10469 op1 = fold_convert (utype, op1);
10470 else if (!useless_type_conversion_p (utype, TREE_TYPE (op1)))
10472 g = gimple_build_assign (make_ssa_name (utype), NOP_EXPR, op1);
10473 gimple_set_location (g, loc);
10474 gsi_insert_before (gsi, g, GSI_SAME_STMT);
10475 op1 = gimple_assign_lhs (g);
10477 g = gimple_build_assign (make_ssa_name (utype), subcode, op0, op1);
10478 gimple_set_location (g, loc);
10479 gsi_insert_before (gsi, g, GSI_SAME_STMT);
10480 if (utype != type)
10482 g = gimple_build_assign (make_ssa_name (type), NOP_EXPR,
10483 gimple_assign_lhs (g));
10484 gimple_set_location (g, loc);
10485 gsi_insert_before (gsi, g, GSI_SAME_STMT);
10487 g = gimple_build_assign (gimple_call_lhs (stmt), COMPLEX_EXPR,
10488 gimple_assign_lhs (g),
10489 build_int_cst (type, ovf));
10491 gimple_set_location (g, loc);
10492 gsi_replace (gsi, g, false);
10493 return true;
10496 /* Return true if VAR is a two-valued variable. Set a and b with the
10497 two-values when it is true. Return false otherwise. */
10499 static bool
10500 two_valued_val_range_p (tree var, tree *a, tree *b)
10502 value_range *vr = get_value_range (var);
10503 if ((vr->type != VR_RANGE
10504 && vr->type != VR_ANTI_RANGE)
10505 || TREE_CODE (vr->min) != INTEGER_CST
10506 || TREE_CODE (vr->max) != INTEGER_CST)
10507 return false;
10509 if (vr->type == VR_RANGE
10510 && wi::sub (vr->max, vr->min) == 1)
10512 *a = vr->min;
10513 *b = vr->max;
10514 return true;
10517 /* ~[TYPE_MIN + 1, TYPE_MAX - 1] */
10518 if (vr->type == VR_ANTI_RANGE
10519 && wi::sub (vr->min, vrp_val_min (TREE_TYPE (var))) == 1
10520 && wi::sub (vrp_val_max (TREE_TYPE (var)), vr->max) == 1)
10522 *a = vrp_val_min (TREE_TYPE (var));
10523 *b = vrp_val_max (TREE_TYPE (var));
10524 return true;
10527 return false;
10530 /* Simplify STMT using ranges if possible. */
10532 static bool
10533 simplify_stmt_using_ranges (gimple_stmt_iterator *gsi)
10535 gimple *stmt = gsi_stmt (*gsi);
10536 if (is_gimple_assign (stmt))
10538 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
10539 tree rhs1 = gimple_assign_rhs1 (stmt);
10540 tree rhs2 = gimple_assign_rhs2 (stmt);
10541 tree lhs = gimple_assign_lhs (stmt);
10542 tree val1 = NULL_TREE, val2 = NULL_TREE;
10543 use_operand_p use_p;
10544 gimple *use_stmt;
10546 /* Convert:
10547 LHS = CST BINOP VAR
10548 Where VAR is two-valued and LHS is used in GIMPLE_COND only
10550 LHS = VAR == VAL1 ? (CST BINOP VAL1) : (CST BINOP VAL2)
10552 Also handles:
10553 LHS = VAR BINOP CST
10554 Where VAR is two-valued and LHS is used in GIMPLE_COND only
10556 LHS = VAR == VAL1 ? (VAL1 BINOP CST) : (VAL2 BINOP CST) */
10558 if (TREE_CODE_CLASS (rhs_code) == tcc_binary
10559 && INTEGRAL_TYPE_P (TREE_TYPE (lhs))
10560 && ((TREE_CODE (rhs1) == INTEGER_CST
10561 && TREE_CODE (rhs2) == SSA_NAME)
10562 || (TREE_CODE (rhs2) == INTEGER_CST
10563 && TREE_CODE (rhs1) == SSA_NAME))
10564 && single_imm_use (lhs, &use_p, &use_stmt)
10565 && gimple_code (use_stmt) == GIMPLE_COND)
10568 tree new_rhs1 = NULL_TREE;
10569 tree new_rhs2 = NULL_TREE;
10570 tree cmp_var = NULL_TREE;
10572 if (TREE_CODE (rhs2) == SSA_NAME
10573 && two_valued_val_range_p (rhs2, &val1, &val2))
10575 /* Optimize RHS1 OP [VAL1, VAL2]. */
10576 new_rhs1 = int_const_binop (rhs_code, rhs1, val1);
10577 new_rhs2 = int_const_binop (rhs_code, rhs1, val2);
10578 cmp_var = rhs2;
10580 else if (TREE_CODE (rhs1) == SSA_NAME
10581 && two_valued_val_range_p (rhs1, &val1, &val2))
10583 /* Optimize [VAL1, VAL2] OP RHS2. */
10584 new_rhs1 = int_const_binop (rhs_code, val1, rhs2);
10585 new_rhs2 = int_const_binop (rhs_code, val2, rhs2);
10586 cmp_var = rhs1;
10589 /* If we could not find two-vals or the optimzation is invalid as
10590 in divide by zero, new_rhs1 / new_rhs will be NULL_TREE. */
10591 if (new_rhs1 && new_rhs2)
10593 tree cond = build2 (EQ_EXPR, boolean_type_node, cmp_var, val1);
10594 gimple_assign_set_rhs_with_ops (gsi,
10595 COND_EXPR, cond,
10596 new_rhs1,
10597 new_rhs2);
10598 update_stmt (gsi_stmt (*gsi));
10599 fold_stmt (gsi, follow_single_use_edges);
10600 return true;
10604 switch (rhs_code)
10606 case EQ_EXPR:
10607 case NE_EXPR:
10608 /* Transform EQ_EXPR, NE_EXPR into BIT_XOR_EXPR or identity
10609 if the RHS is zero or one, and the LHS are known to be boolean
10610 values. */
10611 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
10612 return simplify_truth_ops_using_ranges (gsi, stmt);
10613 break;
10615 /* Transform TRUNC_DIV_EXPR and TRUNC_MOD_EXPR into RSHIFT_EXPR
10616 and BIT_AND_EXPR respectively if the first operand is greater
10617 than zero and the second operand is an exact power of two.
10618 Also optimize TRUNC_MOD_EXPR away if the second operand is
10619 constant and the first operand already has the right value
10620 range. */
10621 case TRUNC_DIV_EXPR:
10622 case TRUNC_MOD_EXPR:
10623 if ((TREE_CODE (rhs1) == SSA_NAME
10624 || TREE_CODE (rhs1) == INTEGER_CST)
10625 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
10626 return simplify_div_or_mod_using_ranges (gsi, stmt);
10627 break;
10629 /* Transform ABS (X) into X or -X as appropriate. */
10630 case ABS_EXPR:
10631 if (TREE_CODE (rhs1) == SSA_NAME
10632 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
10633 return simplify_abs_using_ranges (gsi, stmt);
10634 break;
10636 case BIT_AND_EXPR:
10637 case BIT_IOR_EXPR:
10638 /* Optimize away BIT_AND_EXPR and BIT_IOR_EXPR
10639 if all the bits being cleared are already cleared or
10640 all the bits being set are already set. */
10641 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
10642 return simplify_bit_ops_using_ranges (gsi, stmt);
10643 break;
10645 CASE_CONVERT:
10646 if (TREE_CODE (rhs1) == SSA_NAME
10647 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
10648 return simplify_conversion_using_ranges (gsi, stmt);
10649 break;
10651 case FLOAT_EXPR:
10652 if (TREE_CODE (rhs1) == SSA_NAME
10653 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
10654 return simplify_float_conversion_using_ranges (gsi, stmt);
10655 break;
10657 case MIN_EXPR:
10658 case MAX_EXPR:
10659 return simplify_min_or_max_using_ranges (gsi, stmt);
10661 default:
10662 break;
10665 else if (gimple_code (stmt) == GIMPLE_COND)
10666 return simplify_cond_using_ranges (as_a <gcond *> (stmt));
10667 else if (gimple_code (stmt) == GIMPLE_SWITCH)
10668 return simplify_switch_using_ranges (as_a <gswitch *> (stmt));
10669 else if (is_gimple_call (stmt)
10670 && gimple_call_internal_p (stmt))
10671 return simplify_internal_call_using_ranges (gsi, stmt);
10673 return false;
10676 /* If the statement pointed by SI has a predicate whose value can be
10677 computed using the value range information computed by VRP, compute
10678 its value and return true. Otherwise, return false. */
10680 static bool
10681 fold_predicate_in (gimple_stmt_iterator *si)
10683 bool assignment_p = false;
10684 tree val;
10685 gimple *stmt = gsi_stmt (*si);
10687 if (is_gimple_assign (stmt)
10688 && TREE_CODE_CLASS (gimple_assign_rhs_code (stmt)) == tcc_comparison)
10690 assignment_p = true;
10691 val = vrp_evaluate_conditional (gimple_assign_rhs_code (stmt),
10692 gimple_assign_rhs1 (stmt),
10693 gimple_assign_rhs2 (stmt),
10694 stmt);
10696 else if (gcond *cond_stmt = dyn_cast <gcond *> (stmt))
10697 val = vrp_evaluate_conditional (gimple_cond_code (cond_stmt),
10698 gimple_cond_lhs (cond_stmt),
10699 gimple_cond_rhs (cond_stmt),
10700 stmt);
10701 else
10702 return false;
10704 if (val)
10706 if (assignment_p)
10707 val = fold_convert (gimple_expr_type (stmt), val);
10709 if (dump_file)
10711 fprintf (dump_file, "Folding predicate ");
10712 print_gimple_expr (dump_file, stmt, 0, 0);
10713 fprintf (dump_file, " to ");
10714 print_generic_expr (dump_file, val, 0);
10715 fprintf (dump_file, "\n");
10718 if (is_gimple_assign (stmt))
10719 gimple_assign_set_rhs_from_tree (si, val);
10720 else
10722 gcc_assert (gimple_code (stmt) == GIMPLE_COND);
10723 gcond *cond_stmt = as_a <gcond *> (stmt);
10724 if (integer_zerop (val))
10725 gimple_cond_make_false (cond_stmt);
10726 else if (integer_onep (val))
10727 gimple_cond_make_true (cond_stmt);
10728 else
10729 gcc_unreachable ();
10732 return true;
10735 return false;
10738 /* Callback for substitute_and_fold folding the stmt at *SI. */
10740 static bool
10741 vrp_fold_stmt (gimple_stmt_iterator *si)
10743 if (fold_predicate_in (si))
10744 return true;
10746 return simplify_stmt_using_ranges (si);
10749 /* Return the LHS of any ASSERT_EXPR where OP appears as the first
10750 argument to the ASSERT_EXPR and in which the ASSERT_EXPR dominates
10751 BB. If no such ASSERT_EXPR is found, return OP. */
10753 static tree
10754 lhs_of_dominating_assert (tree op, basic_block bb, gimple *stmt)
10756 imm_use_iterator imm_iter;
10757 gimple *use_stmt;
10758 use_operand_p use_p;
10760 if (TREE_CODE (op) == SSA_NAME)
10762 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, op)
10764 use_stmt = USE_STMT (use_p);
10765 if (use_stmt != stmt
10766 && gimple_assign_single_p (use_stmt)
10767 && TREE_CODE (gimple_assign_rhs1 (use_stmt)) == ASSERT_EXPR
10768 && TREE_OPERAND (gimple_assign_rhs1 (use_stmt), 0) == op
10769 && dominated_by_p (CDI_DOMINATORS, bb, gimple_bb (use_stmt)))
10770 return gimple_assign_lhs (use_stmt);
10773 return op;
10776 /* A trivial wrapper so that we can present the generic jump threading
10777 code with a simple API for simplifying statements. STMT is the
10778 statement we want to simplify, WITHIN_STMT provides the location
10779 for any overflow warnings. */
10781 static tree
10782 simplify_stmt_for_jump_threading (gimple *stmt, gimple *within_stmt,
10783 class avail_exprs_stack *avail_exprs_stack ATTRIBUTE_UNUSED,
10784 basic_block bb)
10786 /* First see if the conditional is in the hash table. */
10787 tree cached_lhs = avail_exprs_stack->lookup_avail_expr (stmt, false, true);
10788 if (cached_lhs && is_gimple_min_invariant (cached_lhs))
10789 return cached_lhs;
10791 if (gcond *cond_stmt = dyn_cast <gcond *> (stmt))
10793 tree op0 = gimple_cond_lhs (cond_stmt);
10794 op0 = lhs_of_dominating_assert (op0, bb, stmt);
10796 tree op1 = gimple_cond_rhs (cond_stmt);
10797 op1 = lhs_of_dominating_assert (op1, bb, stmt);
10799 return vrp_evaluate_conditional (gimple_cond_code (cond_stmt),
10800 op0, op1, within_stmt);
10803 /* We simplify a switch statement by trying to determine which case label
10804 will be taken. If we are successful then we return the corresponding
10805 CASE_LABEL_EXPR. */
10806 if (gswitch *switch_stmt = dyn_cast <gswitch *> (stmt))
10808 tree op = gimple_switch_index (switch_stmt);
10809 if (TREE_CODE (op) != SSA_NAME)
10810 return NULL_TREE;
10812 op = lhs_of_dominating_assert (op, bb, stmt);
10814 value_range *vr = get_value_range (op);
10815 if ((vr->type != VR_RANGE && vr->type != VR_ANTI_RANGE)
10816 || symbolic_range_p (vr))
10817 return NULL_TREE;
10819 if (vr->type == VR_RANGE)
10821 size_t i, j;
10822 /* Get the range of labels that contain a part of the operand's
10823 value range. */
10824 find_case_label_range (switch_stmt, vr->min, vr->max, &i, &j);
10826 /* Is there only one such label? */
10827 if (i == j)
10829 tree label = gimple_switch_label (switch_stmt, i);
10831 /* The i'th label will be taken only if the value range of the
10832 operand is entirely within the bounds of this label. */
10833 if (CASE_HIGH (label) != NULL_TREE
10834 ? (tree_int_cst_compare (CASE_LOW (label), vr->min) <= 0
10835 && tree_int_cst_compare (CASE_HIGH (label), vr->max) >= 0)
10836 : (tree_int_cst_equal (CASE_LOW (label), vr->min)
10837 && tree_int_cst_equal (vr->min, vr->max)))
10838 return label;
10841 /* If there are no such labels then the default label will be
10842 taken. */
10843 if (i > j)
10844 return gimple_switch_label (switch_stmt, 0);
10847 if (vr->type == VR_ANTI_RANGE)
10849 unsigned n = gimple_switch_num_labels (switch_stmt);
10850 tree min_label = gimple_switch_label (switch_stmt, 1);
10851 tree max_label = gimple_switch_label (switch_stmt, n - 1);
10853 /* The default label will be taken only if the anti-range of the
10854 operand is entirely outside the bounds of all the (non-default)
10855 case labels. */
10856 if (tree_int_cst_compare (vr->min, CASE_LOW (min_label)) <= 0
10857 && (CASE_HIGH (max_label) != NULL_TREE
10858 ? tree_int_cst_compare (vr->max, CASE_HIGH (max_label)) >= 0
10859 : tree_int_cst_compare (vr->max, CASE_LOW (max_label)) >= 0))
10860 return gimple_switch_label (switch_stmt, 0);
10863 return NULL_TREE;
10866 if (gassign *assign_stmt = dyn_cast <gassign *> (stmt))
10868 value_range new_vr = VR_INITIALIZER;
10869 tree lhs = gimple_assign_lhs (assign_stmt);
10871 if (TREE_CODE (lhs) == SSA_NAME
10872 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs))
10873 || POINTER_TYPE_P (TREE_TYPE (lhs))))
10875 extract_range_from_assignment (&new_vr, assign_stmt);
10876 if (range_int_cst_singleton_p (&new_vr))
10877 return new_vr.min;
10881 return NULL_TREE;
10884 class vrp_dom_walker : public dom_walker
10886 public:
10887 vrp_dom_walker (cdi_direction direction,
10888 class const_and_copies *const_and_copies,
10889 class avail_exprs_stack *avail_exprs_stack)
10890 : dom_walker (direction, true),
10891 m_const_and_copies (const_and_copies),
10892 m_avail_exprs_stack (avail_exprs_stack),
10893 m_dummy_cond (NULL) {}
10895 virtual edge before_dom_children (basic_block);
10896 virtual void after_dom_children (basic_block);
10898 private:
10899 class const_and_copies *m_const_and_copies;
10900 class avail_exprs_stack *m_avail_exprs_stack;
10902 gcond *m_dummy_cond;
10905 /* Called before processing dominator children of BB. We want to look
10906 at ASSERT_EXPRs and record information from them in the appropriate
10907 tables.
10909 We could look at other statements here. It's not seen as likely
10910 to significantly increase the jump threads we discover. */
10912 edge
10913 vrp_dom_walker::before_dom_children (basic_block bb)
10915 gimple_stmt_iterator gsi;
10917 for (gsi = gsi_start_nondebug_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
10919 gimple *stmt = gsi_stmt (gsi);
10920 if (gimple_assign_single_p (stmt)
10921 && TREE_CODE (gimple_assign_rhs1 (stmt)) == ASSERT_EXPR)
10923 tree rhs1 = gimple_assign_rhs1 (stmt);
10924 tree cond = TREE_OPERAND (rhs1, 1);
10925 tree inverted = invert_truthvalue (cond);
10926 vec<cond_equivalence> p;
10927 p.create (3);
10928 record_conditions (&p, cond, inverted);
10929 for (unsigned int i = 0; i < p.length (); i++)
10930 m_avail_exprs_stack->record_cond (&p[i]);
10932 tree lhs = gimple_assign_lhs (stmt);
10933 m_const_and_copies->record_const_or_copy (lhs,
10934 TREE_OPERAND (rhs1, 0));
10935 p.release ();
10936 continue;
10938 break;
10940 return NULL;
10943 /* Called after processing dominator children of BB. This is where we
10944 actually call into the threader. */
10945 void
10946 vrp_dom_walker::after_dom_children (basic_block bb)
10948 if (!m_dummy_cond)
10949 m_dummy_cond = gimple_build_cond (NE_EXPR,
10950 integer_zero_node, integer_zero_node,
10951 NULL, NULL);
10953 thread_outgoing_edges (bb, m_dummy_cond, m_const_and_copies,
10954 m_avail_exprs_stack,
10955 simplify_stmt_for_jump_threading);
10957 m_avail_exprs_stack->pop_to_marker ();
10958 m_const_and_copies->pop_to_marker ();
10961 /* Blocks which have more than one predecessor and more than
10962 one successor present jump threading opportunities, i.e.,
10963 when the block is reached from a specific predecessor, we
10964 may be able to determine which of the outgoing edges will
10965 be traversed. When this optimization applies, we are able
10966 to avoid conditionals at runtime and we may expose secondary
10967 optimization opportunities.
10969 This routine is effectively a driver for the generic jump
10970 threading code. It basically just presents the generic code
10971 with edges that may be suitable for jump threading.
10973 Unlike DOM, we do not iterate VRP if jump threading was successful.
10974 While iterating may expose new opportunities for VRP, it is expected
10975 those opportunities would be very limited and the compile time cost
10976 to expose those opportunities would be significant.
10978 As jump threading opportunities are discovered, they are registered
10979 for later realization. */
10981 static void
10982 identify_jump_threads (void)
10984 int i;
10985 edge e;
10987 /* Ugh. When substituting values earlier in this pass we can
10988 wipe the dominance information. So rebuild the dominator
10989 information as we need it within the jump threading code. */
10990 calculate_dominance_info (CDI_DOMINATORS);
10992 /* We do not allow VRP information to be used for jump threading
10993 across a back edge in the CFG. Otherwise it becomes too
10994 difficult to avoid eliminating loop exit tests. Of course
10995 EDGE_DFS_BACK is not accurate at this time so we have to
10996 recompute it. */
10997 mark_dfs_back_edges ();
10999 /* Do not thread across edges we are about to remove. Just marking
11000 them as EDGE_IGNORE will do. */
11001 FOR_EACH_VEC_ELT (to_remove_edges, i, e)
11002 e->flags |= EDGE_IGNORE;
11004 /* Allocate our unwinder stack to unwind any temporary equivalences
11005 that might be recorded. */
11006 const_and_copies *equiv_stack = new const_and_copies ();
11008 hash_table<expr_elt_hasher> *avail_exprs
11009 = new hash_table<expr_elt_hasher> (1024);
11010 avail_exprs_stack *avail_exprs_stack
11011 = new class avail_exprs_stack (avail_exprs);
11013 vrp_dom_walker walker (CDI_DOMINATORS, equiv_stack, avail_exprs_stack);
11014 walker.walk (cfun->cfg->x_entry_block_ptr);
11016 /* Clear EDGE_IGNORE. */
11017 FOR_EACH_VEC_ELT (to_remove_edges, i, e)
11018 e->flags &= ~EDGE_IGNORE;
11020 /* We do not actually update the CFG or SSA graphs at this point as
11021 ASSERT_EXPRs are still in the IL and cfg cleanup code does not yet
11022 handle ASSERT_EXPRs gracefully. */
11023 delete equiv_stack;
11024 delete avail_exprs_stack;
11027 /* Free VRP lattice. */
11029 static void
11030 vrp_free_lattice ()
11032 /* Free allocated memory. */
11033 free (vr_value);
11034 free (vr_phi_edge_counts);
11035 bitmap_obstack_release (&vrp_equiv_obstack);
11036 vrp_value_range_pool.release ();
11038 /* So that we can distinguish between VRP data being available
11039 and not available. */
11040 vr_value = NULL;
11041 vr_phi_edge_counts = NULL;
11044 /* Traverse all the blocks folding conditionals with known ranges. */
11046 static void
11047 vrp_finalize (bool warn_array_bounds_p)
11049 size_t i;
11051 values_propagated = true;
11053 if (dump_file)
11055 fprintf (dump_file, "\nValue ranges after VRP:\n\n");
11056 dump_all_value_ranges (dump_file);
11057 fprintf (dump_file, "\n");
11060 /* Set value range to non pointer SSA_NAMEs. */
11061 for (i = 0; i < num_vr_values; i++)
11062 if (vr_value[i])
11064 tree name = ssa_name (i);
11066 if (!name
11067 || (vr_value[i]->type == VR_VARYING)
11068 || (vr_value[i]->type == VR_UNDEFINED)
11069 || (TREE_CODE (vr_value[i]->min) != INTEGER_CST)
11070 || (TREE_CODE (vr_value[i]->max) != INTEGER_CST))
11071 continue;
11073 if (POINTER_TYPE_P (TREE_TYPE (name))
11074 && ((vr_value[i]->type == VR_RANGE
11075 && range_includes_zero_p (vr_value[i]->min,
11076 vr_value[i]->max) == 0)
11077 || (vr_value[i]->type == VR_ANTI_RANGE
11078 && range_includes_zero_p (vr_value[i]->min,
11079 vr_value[i]->max) == 1)))
11080 set_ptr_nonnull (name);
11081 else if (!POINTER_TYPE_P (TREE_TYPE (name)))
11082 set_range_info (name, vr_value[i]->type, vr_value[i]->min,
11083 vr_value[i]->max);
11086 substitute_and_fold (op_with_constant_singleton_value_range, vrp_fold_stmt);
11088 if (warn_array_bounds && warn_array_bounds_p)
11089 check_all_array_refs ();
11092 /* evrp_dom_walker visits the basic blocks in the dominance order and set
11093 the Value Ranges (VR) for SSA_NAMEs in the scope. Use this VR to
11094 discover more VRs. */
11096 class evrp_dom_walker : public dom_walker
11098 public:
11099 evrp_dom_walker ()
11100 : dom_walker (CDI_DOMINATORS), stack (10)
11102 need_eh_cleanup = BITMAP_ALLOC (NULL);
11104 ~evrp_dom_walker ()
11106 BITMAP_FREE (need_eh_cleanup);
11108 virtual edge before_dom_children (basic_block);
11109 virtual void after_dom_children (basic_block);
11110 void push_value_range (tree var, value_range *vr);
11111 value_range *pop_value_range (tree var);
11112 value_range *try_find_new_range (tree op, tree_code code, tree limit);
11114 /* Cond_stack holds the old VR. */
11115 auto_vec<std::pair <tree, value_range*> > stack;
11116 bitmap need_eh_cleanup;
11117 auto_vec<gimple *> stmts_to_fixup;
11118 auto_vec<gimple *> stmts_to_remove;
11121 /* Find new range for OP such that (OP CODE LIMIT) is true. */
11123 value_range *
11124 evrp_dom_walker::try_find_new_range (tree op, tree_code code, tree limit)
11126 value_range vr = VR_INITIALIZER;
11127 value_range *old_vr = get_value_range (op);
11129 /* Discover VR when condition is true. */
11130 extract_range_for_var_from_comparison_expr (op, code, op,
11131 limit, &vr);
11132 if (old_vr->type == VR_RANGE || old_vr->type == VR_ANTI_RANGE)
11133 vrp_intersect_ranges (&vr, old_vr);
11134 /* If we found any usable VR, set the VR to ssa_name and create a
11135 PUSH old value in the stack with the old VR. */
11136 if (vr.type == VR_RANGE || vr.type == VR_ANTI_RANGE)
11138 if (old_vr->type == vr.type
11139 && vrp_operand_equal_p (old_vr->min, vr.min)
11140 && vrp_operand_equal_p (old_vr->max, vr.max))
11141 return NULL;
11142 value_range *new_vr = vrp_value_range_pool.allocate ();
11143 *new_vr = vr;
11144 return new_vr;
11146 return NULL;
11149 /* See if there is any new scope is entered with new VR and set that VR to
11150 ssa_name before visiting the statements in the scope. */
11152 edge
11153 evrp_dom_walker::before_dom_children (basic_block bb)
11155 tree op0 = NULL_TREE;
11156 edge_iterator ei;
11157 edge e;
11159 if (dump_file && (dump_flags & TDF_DETAILS))
11160 fprintf (dump_file, "Visiting BB%d\n", bb->index);
11162 stack.safe_push (std::make_pair (NULL_TREE, (value_range *)NULL));
11164 edge pred_e = NULL;
11165 FOR_EACH_EDGE (e, ei, bb->preds)
11167 /* Ignore simple backedges from this to allow recording conditions
11168 in loop headers. */
11169 if (dominated_by_p (CDI_DOMINATORS, e->src, e->dest))
11170 continue;
11171 if (! pred_e)
11172 pred_e = e;
11173 else
11175 pred_e = NULL;
11176 break;
11179 if (pred_e)
11181 gimple *stmt = last_stmt (pred_e->src);
11182 if (stmt
11183 && gimple_code (stmt) == GIMPLE_COND
11184 && (op0 = gimple_cond_lhs (stmt))
11185 && TREE_CODE (op0) == SSA_NAME
11186 && (INTEGRAL_TYPE_P (TREE_TYPE (gimple_cond_lhs (stmt)))
11187 || POINTER_TYPE_P (TREE_TYPE (gimple_cond_lhs (stmt)))))
11189 if (dump_file && (dump_flags & TDF_DETAILS))
11191 fprintf (dump_file, "Visiting controlling predicate ");
11192 print_gimple_stmt (dump_file, stmt, 0, 0);
11194 /* Entering a new scope. Try to see if we can find a VR
11195 here. */
11196 tree op1 = gimple_cond_rhs (stmt);
11197 tree_code code = gimple_cond_code (stmt);
11199 if (TREE_OVERFLOW_P (op1))
11200 op1 = drop_tree_overflow (op1);
11202 /* If condition is false, invert the cond. */
11203 if (pred_e->flags & EDGE_FALSE_VALUE)
11204 code = invert_tree_comparison (gimple_cond_code (stmt),
11205 HONOR_NANS (op0));
11206 /* Add VR when (OP0 CODE OP1) condition is true. */
11207 value_range *op0_range = try_find_new_range (op0, code, op1);
11209 /* Register ranges for y in x < y where
11210 y might have ranges that are useful. */
11211 tree limit;
11212 tree_code new_code;
11213 if (TREE_CODE (op1) == SSA_NAME
11214 && extract_code_and_val_from_cond_with_ops (op1, code,
11215 op0, op1,
11216 false,
11217 &new_code, &limit))
11219 /* Add VR when (OP1 NEW_CODE LIMIT) condition is true. */
11220 value_range *op1_range = try_find_new_range (op1, new_code, limit);
11221 if (op1_range)
11222 push_value_range (op1, op1_range);
11225 if (op0_range)
11226 push_value_range (op0, op0_range);
11230 /* Visit PHI stmts and discover any new VRs possible. */
11231 bool has_unvisited_preds = false;
11232 FOR_EACH_EDGE (e, ei, bb->preds)
11233 if (e->flags & EDGE_EXECUTABLE
11234 && !(e->src->flags & BB_VISITED))
11236 has_unvisited_preds = true;
11237 break;
11240 for (gphi_iterator gpi = gsi_start_phis (bb);
11241 !gsi_end_p (gpi); gsi_next (&gpi))
11243 gphi *phi = gpi.phi ();
11244 tree lhs = PHI_RESULT (phi);
11245 if (virtual_operand_p (lhs))
11246 continue;
11247 value_range vr_result = VR_INITIALIZER;
11248 bool interesting = stmt_interesting_for_vrp (phi);
11249 if (interesting && dump_file && (dump_flags & TDF_DETAILS))
11251 fprintf (dump_file, "Visiting PHI node ");
11252 print_gimple_stmt (dump_file, phi, 0, 0);
11254 if (!has_unvisited_preds
11255 && interesting)
11256 extract_range_from_phi_node (phi, &vr_result);
11257 else
11259 set_value_range_to_varying (&vr_result);
11260 /* When we have an unvisited executable predecessor we can't
11261 use PHI arg ranges which may be still UNDEFINED but have
11262 to use VARYING for them. But we can still resort to
11263 SCEV for loop header PHIs. */
11264 struct loop *l;
11265 if (interesting
11266 && (l = loop_containing_stmt (phi))
11267 && l->header == gimple_bb (phi))
11268 adjust_range_with_scev (&vr_result, l, phi, lhs);
11270 update_value_range (lhs, &vr_result);
11272 /* Mark PHIs whose lhs we fully propagate for removal. */
11273 tree val = op_with_constant_singleton_value_range (lhs);
11274 if (val && may_propagate_copy (lhs, val))
11276 stmts_to_remove.safe_push (phi);
11277 continue;
11280 /* Set the SSA with the value range. */
11281 if (INTEGRAL_TYPE_P (TREE_TYPE (lhs)))
11283 if ((vr_result.type == VR_RANGE
11284 || vr_result.type == VR_ANTI_RANGE)
11285 && (TREE_CODE (vr_result.min) == INTEGER_CST)
11286 && (TREE_CODE (vr_result.max) == INTEGER_CST))
11287 set_range_info (lhs,
11288 vr_result.type, vr_result.min, vr_result.max);
11290 else if (POINTER_TYPE_P (TREE_TYPE (lhs))
11291 && ((vr_result.type == VR_RANGE
11292 && range_includes_zero_p (vr_result.min,
11293 vr_result.max) == 0)
11294 || (vr_result.type == VR_ANTI_RANGE
11295 && range_includes_zero_p (vr_result.min,
11296 vr_result.max) == 1)))
11297 set_ptr_nonnull (lhs);
11300 edge taken_edge = NULL;
11302 /* Visit all other stmts and discover any new VRs possible. */
11303 for (gimple_stmt_iterator gsi = gsi_start_bb (bb);
11304 !gsi_end_p (gsi); gsi_next (&gsi))
11306 gimple *stmt = gsi_stmt (gsi);
11307 tree output = NULL_TREE;
11308 gimple *old_stmt = stmt;
11309 bool was_noreturn = (is_gimple_call (stmt)
11310 && gimple_call_noreturn_p (stmt));
11312 if (dump_file && (dump_flags & TDF_DETAILS))
11314 fprintf (dump_file, "Visiting stmt ");
11315 print_gimple_stmt (dump_file, stmt, 0, 0);
11318 if (gcond *cond = dyn_cast <gcond *> (stmt))
11320 vrp_visit_cond_stmt (cond, &taken_edge);
11321 if (taken_edge)
11323 if (taken_edge->flags & EDGE_TRUE_VALUE)
11324 gimple_cond_make_true (cond);
11325 else if (taken_edge->flags & EDGE_FALSE_VALUE)
11326 gimple_cond_make_false (cond);
11327 else
11328 gcc_unreachable ();
11329 update_stmt (stmt);
11332 else if (stmt_interesting_for_vrp (stmt))
11334 edge taken_edge;
11335 value_range vr = VR_INITIALIZER;
11336 extract_range_from_stmt (stmt, &taken_edge, &output, &vr);
11337 if (output
11338 && (vr.type == VR_RANGE || vr.type == VR_ANTI_RANGE))
11340 update_value_range (output, &vr);
11341 vr = *get_value_range (output);
11343 /* Mark stmts whose output we fully propagate for removal. */
11344 tree val;
11345 if ((val = op_with_constant_singleton_value_range (output))
11346 && may_propagate_copy (output, val)
11347 && !stmt_could_throw_p (stmt)
11348 && !gimple_has_side_effects (stmt))
11350 stmts_to_remove.safe_push (stmt);
11351 continue;
11354 /* Set the SSA with the value range. */
11355 if (INTEGRAL_TYPE_P (TREE_TYPE (output)))
11357 if ((vr.type == VR_RANGE
11358 || vr.type == VR_ANTI_RANGE)
11359 && (TREE_CODE (vr.min) == INTEGER_CST)
11360 && (TREE_CODE (vr.max) == INTEGER_CST))
11361 set_range_info (output, vr.type, vr.min, vr.max);
11363 else if (POINTER_TYPE_P (TREE_TYPE (output))
11364 && ((vr.type == VR_RANGE
11365 && range_includes_zero_p (vr.min,
11366 vr.max) == 0)
11367 || (vr.type == VR_ANTI_RANGE
11368 && range_includes_zero_p (vr.min,
11369 vr.max) == 1)))
11370 set_ptr_nonnull (output);
11372 else
11373 set_defs_to_varying (stmt);
11375 else
11376 set_defs_to_varying (stmt);
11378 /* See if we can derive a range for any of STMT's operands. */
11379 tree op;
11380 ssa_op_iter i;
11381 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_USE)
11383 tree value;
11384 enum tree_code comp_code;
11386 /* If OP is used in such a way that we can infer a value
11387 range for it, and we don't find a previous assertion for
11388 it, create a new assertion location node for OP. */
11389 if (infer_value_range (stmt, op, &comp_code, &value))
11391 /* If we are able to infer a nonzero value range for OP,
11392 then walk backwards through the use-def chain to see if OP
11393 was set via a typecast.
11394 If so, then we can also infer a nonzero value range
11395 for the operand of the NOP_EXPR. */
11396 if (comp_code == NE_EXPR && integer_zerop (value))
11398 tree t = op;
11399 gimple *def_stmt = SSA_NAME_DEF_STMT (t);
11400 while (is_gimple_assign (def_stmt)
11401 && CONVERT_EXPR_CODE_P
11402 (gimple_assign_rhs_code (def_stmt))
11403 && TREE_CODE
11404 (gimple_assign_rhs1 (def_stmt)) == SSA_NAME
11405 && POINTER_TYPE_P
11406 (TREE_TYPE (gimple_assign_rhs1 (def_stmt))))
11408 t = gimple_assign_rhs1 (def_stmt);
11409 def_stmt = SSA_NAME_DEF_STMT (t);
11411 /* Add VR when (T COMP_CODE value) condition is
11412 true. */
11413 value_range *op_range
11414 = try_find_new_range (t, comp_code, value);
11415 if (op_range)
11416 push_value_range (t, op_range);
11419 /* Add VR when (OP COMP_CODE value) condition is true. */
11420 value_range *op_range = try_find_new_range (op,
11421 comp_code, value);
11422 if (op_range)
11423 push_value_range (op, op_range);
11427 /* Try folding stmts with the VR discovered. */
11428 bool did_replace
11429 = replace_uses_in (stmt, op_with_constant_singleton_value_range);
11430 if (fold_stmt (&gsi, follow_single_use_edges)
11431 || did_replace)
11433 stmt = gsi_stmt (gsi);
11434 update_stmt (stmt);
11435 did_replace = true;
11438 if (did_replace)
11440 /* If we cleaned up EH information from the statement,
11441 remove EH edges. */
11442 if (maybe_clean_or_replace_eh_stmt (old_stmt, stmt))
11443 bitmap_set_bit (need_eh_cleanup, bb->index);
11445 /* If we turned a not noreturn call into a noreturn one
11446 schedule it for fixup. */
11447 if (!was_noreturn
11448 && is_gimple_call (stmt)
11449 && gimple_call_noreturn_p (stmt))
11450 stmts_to_fixup.safe_push (stmt);
11452 if (gimple_assign_single_p (stmt))
11454 tree rhs = gimple_assign_rhs1 (stmt);
11455 if (TREE_CODE (rhs) == ADDR_EXPR)
11456 recompute_tree_invariant_for_addr_expr (rhs);
11461 /* Visit BB successor PHI nodes and replace PHI args. */
11462 FOR_EACH_EDGE (e, ei, bb->succs)
11464 for (gphi_iterator gpi = gsi_start_phis (e->dest);
11465 !gsi_end_p (gpi); gsi_next (&gpi))
11467 gphi *phi = gpi.phi ();
11468 use_operand_p use_p = PHI_ARG_DEF_PTR_FROM_EDGE (phi, e);
11469 tree arg = USE_FROM_PTR (use_p);
11470 if (TREE_CODE (arg) != SSA_NAME
11471 || virtual_operand_p (arg))
11472 continue;
11473 tree val = op_with_constant_singleton_value_range (arg);
11474 if (val && may_propagate_copy (arg, val))
11475 propagate_value (use_p, val);
11479 bb->flags |= BB_VISITED;
11481 return taken_edge;
11484 /* Restore/pop VRs valid only for BB when we leave BB. */
11486 void
11487 evrp_dom_walker::after_dom_children (basic_block bb ATTRIBUTE_UNUSED)
11489 gcc_checking_assert (!stack.is_empty ());
11490 while (stack.last ().first != NULL_TREE)
11491 pop_value_range (stack.last ().first);
11492 stack.pop ();
11495 /* Push the Value Range of VAR to the stack and update it with new VR. */
11497 void
11498 evrp_dom_walker::push_value_range (tree var, value_range *vr)
11500 if (SSA_NAME_VERSION (var) >= num_vr_values)
11501 return;
11502 if (dump_file && (dump_flags & TDF_DETAILS))
11504 fprintf (dump_file, "pushing new range for ");
11505 print_generic_expr (dump_file, var, 0);
11506 fprintf (dump_file, ": ");
11507 dump_value_range (dump_file, vr);
11508 fprintf (dump_file, "\n");
11510 stack.safe_push (std::make_pair (var, get_value_range (var)));
11511 vr_value[SSA_NAME_VERSION (var)] = vr;
11514 /* Pop the Value Range from the vrp_stack and update VAR with it. */
11516 value_range *
11517 evrp_dom_walker::pop_value_range (tree var)
11519 value_range *vr = stack.last ().second;
11520 gcc_checking_assert (var == stack.last ().first);
11521 if (dump_file && (dump_flags & TDF_DETAILS))
11523 fprintf (dump_file, "popping range for ");
11524 print_generic_expr (dump_file, var, 0);
11525 fprintf (dump_file, ", restoring ");
11526 dump_value_range (dump_file, vr);
11527 fprintf (dump_file, "\n");
11529 vr_value[SSA_NAME_VERSION (var)] = vr;
11530 stack.pop ();
11531 return vr;
11535 /* Main entry point for the early vrp pass which is a simplified non-iterative
11536 version of vrp where basic blocks are visited in dominance order. Value
11537 ranges discovered in early vrp will also be used by ipa-vrp. */
11539 static unsigned int
11540 execute_early_vrp ()
11542 edge e;
11543 edge_iterator ei;
11544 basic_block bb;
11546 loop_optimizer_init (LOOPS_NORMAL | LOOPS_HAVE_RECORDED_EXITS);
11547 rewrite_into_loop_closed_ssa (NULL, TODO_update_ssa);
11548 scev_initialize ();
11549 calculate_dominance_info (CDI_DOMINATORS);
11550 FOR_EACH_BB_FN (bb, cfun)
11552 bb->flags &= ~BB_VISITED;
11553 FOR_EACH_EDGE (e, ei, bb->preds)
11554 e->flags |= EDGE_EXECUTABLE;
11556 vrp_initialize_lattice ();
11558 /* Walk stmts in dominance order and propagate VRP. */
11559 evrp_dom_walker walker;
11560 walker.walk (ENTRY_BLOCK_PTR_FOR_FN (cfun));
11562 if (dump_file)
11564 fprintf (dump_file, "\nValue ranges after Early VRP:\n\n");
11565 dump_all_value_ranges (dump_file);
11566 fprintf (dump_file, "\n");
11569 /* Remove stmts in reverse order to make debug stmt creation possible. */
11570 while (! walker.stmts_to_remove.is_empty ())
11572 gimple *stmt = walker.stmts_to_remove.pop ();
11573 if (dump_file && dump_flags & TDF_DETAILS)
11575 fprintf (dump_file, "Removing dead stmt ");
11576 print_gimple_stmt (dump_file, stmt, 0, 0);
11577 fprintf (dump_file, "\n");
11579 gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
11580 if (gimple_code (stmt) == GIMPLE_PHI)
11581 remove_phi_node (&gsi, true);
11582 else
11584 unlink_stmt_vdef (stmt);
11585 gsi_remove (&gsi, true);
11586 release_defs (stmt);
11590 if (!bitmap_empty_p (walker.need_eh_cleanup))
11591 gimple_purge_all_dead_eh_edges (walker.need_eh_cleanup);
11593 /* Fixup stmts that became noreturn calls. This may require splitting
11594 blocks and thus isn't possible during the dominator walk. Do this
11595 in reverse order so we don't inadvertedly remove a stmt we want to
11596 fixup by visiting a dominating now noreturn call first. */
11597 while (!walker.stmts_to_fixup.is_empty ())
11599 gimple *stmt = walker.stmts_to_fixup.pop ();
11600 fixup_noreturn_call (stmt);
11603 vrp_free_lattice ();
11604 scev_finalize ();
11605 loop_optimizer_finalize ();
11606 return 0;
11610 /* Main entry point to VRP (Value Range Propagation). This pass is
11611 loosely based on J. R. C. Patterson, ``Accurate Static Branch
11612 Prediction by Value Range Propagation,'' in SIGPLAN Conference on
11613 Programming Language Design and Implementation, pp. 67-78, 1995.
11614 Also available at http://citeseer.ist.psu.edu/patterson95accurate.html
11616 This is essentially an SSA-CCP pass modified to deal with ranges
11617 instead of constants.
11619 While propagating ranges, we may find that two or more SSA name
11620 have equivalent, though distinct ranges. For instance,
11622 1 x_9 = p_3->a;
11623 2 p_4 = ASSERT_EXPR <p_3, p_3 != 0>
11624 3 if (p_4 == q_2)
11625 4 p_5 = ASSERT_EXPR <p_4, p_4 == q_2>;
11626 5 endif
11627 6 if (q_2)
11629 In the code above, pointer p_5 has range [q_2, q_2], but from the
11630 code we can also determine that p_5 cannot be NULL and, if q_2 had
11631 a non-varying range, p_5's range should also be compatible with it.
11633 These equivalences are created by two expressions: ASSERT_EXPR and
11634 copy operations. Since p_5 is an assertion on p_4, and p_4 was the
11635 result of another assertion, then we can use the fact that p_5 and
11636 p_4 are equivalent when evaluating p_5's range.
11638 Together with value ranges, we also propagate these equivalences
11639 between names so that we can take advantage of information from
11640 multiple ranges when doing final replacement. Note that this
11641 equivalency relation is transitive but not symmetric.
11643 In the example above, p_5 is equivalent to p_4, q_2 and p_3, but we
11644 cannot assert that q_2 is equivalent to p_5 because q_2 may be used
11645 in contexts where that assertion does not hold (e.g., in line 6).
11647 TODO, the main difference between this pass and Patterson's is that
11648 we do not propagate edge probabilities. We only compute whether
11649 edges can be taken or not. That is, instead of having a spectrum
11650 of jump probabilities between 0 and 1, we only deal with 0, 1 and
11651 DON'T KNOW. In the future, it may be worthwhile to propagate
11652 probabilities to aid branch prediction. */
11654 static unsigned int
11655 execute_vrp (bool warn_array_bounds_p)
11657 int i;
11658 edge e;
11659 switch_update *su;
11661 loop_optimizer_init (LOOPS_NORMAL | LOOPS_HAVE_RECORDED_EXITS);
11662 rewrite_into_loop_closed_ssa (NULL, TODO_update_ssa);
11663 scev_initialize ();
11665 /* ??? This ends up using stale EDGE_DFS_BACK for liveness computation.
11666 Inserting assertions may split edges which will invalidate
11667 EDGE_DFS_BACK. */
11668 insert_range_assertions ();
11670 to_remove_edges.create (10);
11671 to_update_switch_stmts.create (5);
11672 threadedge_initialize_values ();
11674 /* For visiting PHI nodes we need EDGE_DFS_BACK computed. */
11675 mark_dfs_back_edges ();
11677 vrp_initialize_lattice ();
11678 vrp_initialize ();
11679 ssa_propagate (vrp_visit_stmt, vrp_visit_phi_node);
11680 vrp_finalize (warn_array_bounds_p);
11682 /* We must identify jump threading opportunities before we release
11683 the datastructures built by VRP. */
11684 identify_jump_threads ();
11686 vrp_free_lattice ();
11688 free_numbers_of_iterations_estimates (cfun);
11690 /* ASSERT_EXPRs must be removed before finalizing jump threads
11691 as finalizing jump threads calls the CFG cleanup code which
11692 does not properly handle ASSERT_EXPRs. */
11693 remove_range_assertions ();
11695 /* If we exposed any new variables, go ahead and put them into
11696 SSA form now, before we handle jump threading. This simplifies
11697 interactions between rewriting of _DECL nodes into SSA form
11698 and rewriting SSA_NAME nodes into SSA form after block
11699 duplication and CFG manipulation. */
11700 update_ssa (TODO_update_ssa);
11702 /* We identified all the jump threading opportunities earlier, but could
11703 not transform the CFG at that time. This routine transforms the
11704 CFG and arranges for the dominator tree to be rebuilt if necessary.
11706 Note the SSA graph update will occur during the normal TODO
11707 processing by the pass manager. */
11708 thread_through_all_blocks (false);
11710 /* Remove dead edges from SWITCH_EXPR optimization. This leaves the
11711 CFG in a broken state and requires a cfg_cleanup run. */
11712 FOR_EACH_VEC_ELT (to_remove_edges, i, e)
11713 remove_edge (e);
11714 /* Update SWITCH_EXPR case label vector. */
11715 FOR_EACH_VEC_ELT (to_update_switch_stmts, i, su)
11717 size_t j;
11718 size_t n = TREE_VEC_LENGTH (su->vec);
11719 tree label;
11720 gimple_switch_set_num_labels (su->stmt, n);
11721 for (j = 0; j < n; j++)
11722 gimple_switch_set_label (su->stmt, j, TREE_VEC_ELT (su->vec, j));
11723 /* As we may have replaced the default label with a regular one
11724 make sure to make it a real default label again. This ensures
11725 optimal expansion. */
11726 label = gimple_switch_label (su->stmt, 0);
11727 CASE_LOW (label) = NULL_TREE;
11728 CASE_HIGH (label) = NULL_TREE;
11731 if (to_remove_edges.length () > 0)
11733 free_dominance_info (CDI_DOMINATORS);
11734 loops_state_set (LOOPS_NEED_FIXUP);
11737 to_remove_edges.release ();
11738 to_update_switch_stmts.release ();
11739 threadedge_finalize_values ();
11741 scev_finalize ();
11742 loop_optimizer_finalize ();
11743 return 0;
11746 namespace {
11748 const pass_data pass_data_vrp =
11750 GIMPLE_PASS, /* type */
11751 "vrp", /* name */
11752 OPTGROUP_NONE, /* optinfo_flags */
11753 TV_TREE_VRP, /* tv_id */
11754 PROP_ssa, /* properties_required */
11755 0, /* properties_provided */
11756 0, /* properties_destroyed */
11757 0, /* todo_flags_start */
11758 ( TODO_cleanup_cfg | TODO_update_ssa ), /* todo_flags_finish */
11761 class pass_vrp : public gimple_opt_pass
11763 public:
11764 pass_vrp (gcc::context *ctxt)
11765 : gimple_opt_pass (pass_data_vrp, ctxt), warn_array_bounds_p (false)
11768 /* opt_pass methods: */
11769 opt_pass * clone () { return new pass_vrp (m_ctxt); }
11770 void set_pass_param (unsigned int n, bool param)
11772 gcc_assert (n == 0);
11773 warn_array_bounds_p = param;
11775 virtual bool gate (function *) { return flag_tree_vrp != 0; }
11776 virtual unsigned int execute (function *)
11777 { return execute_vrp (warn_array_bounds_p); }
11779 private:
11780 bool warn_array_bounds_p;
11781 }; // class pass_vrp
11783 } // anon namespace
11785 gimple_opt_pass *
11786 make_pass_vrp (gcc::context *ctxt)
11788 return new pass_vrp (ctxt);
11791 namespace {
11793 const pass_data pass_data_early_vrp =
11795 GIMPLE_PASS, /* type */
11796 "evrp", /* name */
11797 OPTGROUP_NONE, /* optinfo_flags */
11798 TV_TREE_EARLY_VRP, /* tv_id */
11799 PROP_ssa, /* properties_required */
11800 0, /* properties_provided */
11801 0, /* properties_destroyed */
11802 0, /* todo_flags_start */
11803 ( TODO_cleanup_cfg | TODO_update_ssa | TODO_verify_all ),
11806 class pass_early_vrp : public gimple_opt_pass
11808 public:
11809 pass_early_vrp (gcc::context *ctxt)
11810 : gimple_opt_pass (pass_data_early_vrp, ctxt)
11813 /* opt_pass methods: */
11814 opt_pass * clone () { return new pass_early_vrp (m_ctxt); }
11815 virtual bool gate (function *)
11817 return flag_tree_vrp != 0;
11819 virtual unsigned int execute (function *)
11820 { return execute_early_vrp (); }
11822 }; // class pass_vrp
11823 } // anon namespace
11825 gimple_opt_pass *
11826 make_pass_early_vrp (gcc::context *ctxt)
11828 return new pass_early_vrp (ctxt);