gcc/
[official-gcc.git] / gcc / tree-vrp.c
blob41c870f111c4f7fbe0fb6631e3c8e239cbd833ce
1 /* Support routines for Value Range Propagation (VRP).
2 Copyright (C) 2005-2016 Free Software Foundation, Inc.
3 Contributed by Diego Novillo <dnovillo@redhat.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "backend.h"
25 #include "insn-codes.h"
26 #include "rtl.h"
27 #include "tree.h"
28 #include "gimple.h"
29 #include "cfghooks.h"
30 #include "tree-pass.h"
31 #include "ssa.h"
32 #include "optabs-tree.h"
33 #include "gimple-pretty-print.h"
34 #include "diagnostic-core.h"
35 #include "flags.h"
36 #include "fold-const.h"
37 #include "stor-layout.h"
38 #include "calls.h"
39 #include "cfganal.h"
40 #include "gimple-fold.h"
41 #include "tree-eh.h"
42 #include "gimple-iterator.h"
43 #include "gimple-walk.h"
44 #include "tree-cfg.h"
45 #include "tree-ssa-loop-manip.h"
46 #include "tree-ssa-loop-niter.h"
47 #include "tree-ssa-loop.h"
48 #include "tree-into-ssa.h"
49 #include "tree-ssa.h"
50 #include "intl.h"
51 #include "cfgloop.h"
52 #include "tree-scalar-evolution.h"
53 #include "tree-ssa-propagate.h"
54 #include "tree-chrec.h"
55 #include "tree-ssa-threadupdate.h"
56 #include "tree-ssa-scopedtables.h"
57 #include "tree-ssa-threadedge.h"
58 #include "omp-low.h"
59 #include "target.h"
60 #include "case-cfn-macros.h"
61 #include "params.h"
63 /* Range of values that can be associated with an SSA_NAME after VRP
64 has executed. */
65 struct value_range
67 /* Lattice value represented by this range. */
68 enum value_range_type type;
70 /* Minimum and maximum values represented by this range. These
71 values should be interpreted as follows:
73 - If TYPE is VR_UNDEFINED or VR_VARYING then MIN and MAX must
74 be NULL.
76 - If TYPE == VR_RANGE then MIN holds the minimum value and
77 MAX holds the maximum value of the range [MIN, MAX].
79 - If TYPE == ANTI_RANGE the variable is known to NOT
80 take any values in the range [MIN, MAX]. */
81 tree min;
82 tree max;
84 /* Set of SSA names whose value ranges are equivalent to this one.
85 This set is only valid when TYPE is VR_RANGE or VR_ANTI_RANGE. */
86 bitmap equiv;
89 #define VR_INITIALIZER { VR_UNDEFINED, NULL_TREE, NULL_TREE, NULL }
91 /* Set of SSA names found live during the RPO traversal of the function
92 for still active basic-blocks. */
93 static sbitmap *live;
95 /* Return true if the SSA name NAME is live on the edge E. */
97 static bool
98 live_on_edge (edge e, tree name)
100 return (live[e->dest->index]
101 && bitmap_bit_p (live[e->dest->index], SSA_NAME_VERSION (name)));
104 /* Local functions. */
105 static int compare_values (tree val1, tree val2);
106 static int compare_values_warnv (tree val1, tree val2, bool *);
107 static void vrp_meet (value_range *, value_range *);
108 static void vrp_intersect_ranges (value_range *, value_range *);
109 static tree vrp_evaluate_conditional_warnv_with_ops (enum tree_code,
110 tree, tree, bool, bool *,
111 bool *);
113 /* Location information for ASSERT_EXPRs. Each instance of this
114 structure describes an ASSERT_EXPR for an SSA name. Since a single
115 SSA name may have more than one assertion associated with it, these
116 locations are kept in a linked list attached to the corresponding
117 SSA name. */
118 struct assert_locus
120 /* Basic block where the assertion would be inserted. */
121 basic_block bb;
123 /* Some assertions need to be inserted on an edge (e.g., assertions
124 generated by COND_EXPRs). In those cases, BB will be NULL. */
125 edge e;
127 /* Pointer to the statement that generated this assertion. */
128 gimple_stmt_iterator si;
130 /* Predicate code for the ASSERT_EXPR. Must be COMPARISON_CLASS_P. */
131 enum tree_code comp_code;
133 /* Value being compared against. */
134 tree val;
136 /* Expression to compare. */
137 tree expr;
139 /* Next node in the linked list. */
140 assert_locus *next;
143 /* If bit I is present, it means that SSA name N_i has a list of
144 assertions that should be inserted in the IL. */
145 static bitmap need_assert_for;
147 /* Array of locations lists where to insert assertions. ASSERTS_FOR[I]
148 holds a list of ASSERT_LOCUS_T nodes that describe where
149 ASSERT_EXPRs for SSA name N_I should be inserted. */
150 static assert_locus **asserts_for;
152 /* Value range array. After propagation, VR_VALUE[I] holds the range
153 of values that SSA name N_I may take. */
154 static unsigned num_vr_values;
155 static value_range **vr_value;
156 static bool values_propagated;
158 /* For a PHI node which sets SSA name N_I, VR_COUNTS[I] holds the
159 number of executable edges we saw the last time we visited the
160 node. */
161 static int *vr_phi_edge_counts;
163 struct switch_update {
164 gswitch *stmt;
165 tree vec;
168 static vec<edge> to_remove_edges;
169 static vec<switch_update> to_update_switch_stmts;
172 /* Return the maximum value for TYPE. */
174 static inline tree
175 vrp_val_max (const_tree type)
177 if (!INTEGRAL_TYPE_P (type))
178 return NULL_TREE;
180 return TYPE_MAX_VALUE (type);
183 /* Return the minimum value for TYPE. */
185 static inline tree
186 vrp_val_min (const_tree type)
188 if (!INTEGRAL_TYPE_P (type))
189 return NULL_TREE;
191 return TYPE_MIN_VALUE (type);
194 /* Return whether VAL is equal to the maximum value of its type. This
195 will be true for a positive overflow infinity. We can't do a
196 simple equality comparison with TYPE_MAX_VALUE because C typedefs
197 and Ada subtypes can produce types whose TYPE_MAX_VALUE is not ==
198 to the integer constant with the same value in the type. */
200 static inline bool
201 vrp_val_is_max (const_tree val)
203 tree type_max = vrp_val_max (TREE_TYPE (val));
204 return (val == type_max
205 || (type_max != NULL_TREE
206 && operand_equal_p (val, type_max, 0)));
209 /* Return whether VAL is equal to the minimum value of its type. This
210 will be true for a negative overflow infinity. */
212 static inline bool
213 vrp_val_is_min (const_tree val)
215 tree type_min = vrp_val_min (TREE_TYPE (val));
216 return (val == type_min
217 || (type_min != NULL_TREE
218 && operand_equal_p (val, type_min, 0)));
222 /* Return whether TYPE should use an overflow infinity distinct from
223 TYPE_{MIN,MAX}_VALUE. We use an overflow infinity value to
224 represent a signed overflow during VRP computations. An infinity
225 is distinct from a half-range, which will go from some number to
226 TYPE_{MIN,MAX}_VALUE. */
228 static inline bool
229 needs_overflow_infinity (const_tree type)
231 return INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_WRAPS (type);
234 /* Return whether TYPE can support our overflow infinity
235 representation: we use the TREE_OVERFLOW flag, which only exists
236 for constants. If TYPE doesn't support this, we don't optimize
237 cases which would require signed overflow--we drop them to
238 VARYING. */
240 static inline bool
241 supports_overflow_infinity (const_tree type)
243 tree min = vrp_val_min (type), max = vrp_val_max (type);
244 gcc_checking_assert (needs_overflow_infinity (type));
245 return (min != NULL_TREE
246 && CONSTANT_CLASS_P (min)
247 && max != NULL_TREE
248 && CONSTANT_CLASS_P (max));
251 /* VAL is the maximum or minimum value of a type. Return a
252 corresponding overflow infinity. */
254 static inline tree
255 make_overflow_infinity (tree val)
257 gcc_checking_assert (val != NULL_TREE && CONSTANT_CLASS_P (val));
258 val = copy_node (val);
259 TREE_OVERFLOW (val) = 1;
260 return val;
263 /* Return a negative overflow infinity for TYPE. */
265 static inline tree
266 negative_overflow_infinity (tree type)
268 gcc_checking_assert (supports_overflow_infinity (type));
269 return make_overflow_infinity (vrp_val_min (type));
272 /* Return a positive overflow infinity for TYPE. */
274 static inline tree
275 positive_overflow_infinity (tree type)
277 gcc_checking_assert (supports_overflow_infinity (type));
278 return make_overflow_infinity (vrp_val_max (type));
281 /* Return whether VAL is a negative overflow infinity. */
283 static inline bool
284 is_negative_overflow_infinity (const_tree val)
286 return (TREE_OVERFLOW_P (val)
287 && needs_overflow_infinity (TREE_TYPE (val))
288 && vrp_val_is_min (val));
291 /* Return whether VAL is a positive overflow infinity. */
293 static inline bool
294 is_positive_overflow_infinity (const_tree val)
296 return (TREE_OVERFLOW_P (val)
297 && needs_overflow_infinity (TREE_TYPE (val))
298 && vrp_val_is_max (val));
301 /* Return whether VAL is a positive or negative overflow infinity. */
303 static inline bool
304 is_overflow_infinity (const_tree val)
306 return (TREE_OVERFLOW_P (val)
307 && needs_overflow_infinity (TREE_TYPE (val))
308 && (vrp_val_is_min (val) || vrp_val_is_max (val)));
311 /* Return whether STMT has a constant rhs that is_overflow_infinity. */
313 static inline bool
314 stmt_overflow_infinity (gimple *stmt)
316 if (is_gimple_assign (stmt)
317 && get_gimple_rhs_class (gimple_assign_rhs_code (stmt)) ==
318 GIMPLE_SINGLE_RHS)
319 return is_overflow_infinity (gimple_assign_rhs1 (stmt));
320 return false;
323 /* If VAL is now an overflow infinity, return VAL. Otherwise, return
324 the same value with TREE_OVERFLOW clear. This can be used to avoid
325 confusing a regular value with an overflow value. */
327 static inline tree
328 avoid_overflow_infinity (tree val)
330 if (!is_overflow_infinity (val))
331 return val;
333 if (vrp_val_is_max (val))
334 return vrp_val_max (TREE_TYPE (val));
335 else
337 gcc_checking_assert (vrp_val_is_min (val));
338 return vrp_val_min (TREE_TYPE (val));
343 /* Set value range VR to VR_UNDEFINED. */
345 static inline void
346 set_value_range_to_undefined (value_range *vr)
348 vr->type = VR_UNDEFINED;
349 vr->min = vr->max = NULL_TREE;
350 if (vr->equiv)
351 bitmap_clear (vr->equiv);
355 /* Set value range VR to VR_VARYING. */
357 static inline void
358 set_value_range_to_varying (value_range *vr)
360 vr->type = VR_VARYING;
361 vr->min = vr->max = NULL_TREE;
362 if (vr->equiv)
363 bitmap_clear (vr->equiv);
367 /* Set value range VR to {T, MIN, MAX, EQUIV}. */
369 static void
370 set_value_range (value_range *vr, enum value_range_type t, tree min,
371 tree max, bitmap equiv)
373 /* Check the validity of the range. */
374 if (flag_checking
375 && (t == VR_RANGE || t == VR_ANTI_RANGE))
377 int cmp;
379 gcc_assert (min && max);
381 gcc_assert ((!TREE_OVERFLOW_P (min) || is_overflow_infinity (min))
382 && (!TREE_OVERFLOW_P (max) || is_overflow_infinity (max)));
384 if (INTEGRAL_TYPE_P (TREE_TYPE (min)) && t == VR_ANTI_RANGE)
385 gcc_assert (!vrp_val_is_min (min) || !vrp_val_is_max (max));
387 cmp = compare_values (min, max);
388 gcc_assert (cmp == 0 || cmp == -1 || cmp == -2);
390 if (needs_overflow_infinity (TREE_TYPE (min)))
391 gcc_assert (!is_overflow_infinity (min)
392 || !is_overflow_infinity (max));
395 if (flag_checking
396 && (t == VR_UNDEFINED || t == VR_VARYING))
398 gcc_assert (min == NULL_TREE && max == NULL_TREE);
399 gcc_assert (equiv == NULL || bitmap_empty_p (equiv));
402 vr->type = t;
403 vr->min = min;
404 vr->max = max;
406 /* Since updating the equivalence set involves deep copying the
407 bitmaps, only do it if absolutely necessary. */
408 if (vr->equiv == NULL
409 && equiv != NULL)
410 vr->equiv = BITMAP_ALLOC (NULL);
412 if (equiv != vr->equiv)
414 if (equiv && !bitmap_empty_p (equiv))
415 bitmap_copy (vr->equiv, equiv);
416 else
417 bitmap_clear (vr->equiv);
422 /* Set value range VR to the canonical form of {T, MIN, MAX, EQUIV}.
423 This means adjusting T, MIN and MAX representing the case of a
424 wrapping range with MAX < MIN covering [MIN, type_max] U [type_min, MAX]
425 as anti-rage ~[MAX+1, MIN-1]. Likewise for wrapping anti-ranges.
426 In corner cases where MAX+1 or MIN-1 wraps this will fall back
427 to varying.
428 This routine exists to ease canonicalization in the case where we
429 extract ranges from var + CST op limit. */
431 static void
432 set_and_canonicalize_value_range (value_range *vr, enum value_range_type t,
433 tree min, tree max, bitmap equiv)
435 /* Use the canonical setters for VR_UNDEFINED and VR_VARYING. */
436 if (t == VR_UNDEFINED)
438 set_value_range_to_undefined (vr);
439 return;
441 else if (t == VR_VARYING)
443 set_value_range_to_varying (vr);
444 return;
447 /* Nothing to canonicalize for symbolic ranges. */
448 if (TREE_CODE (min) != INTEGER_CST
449 || TREE_CODE (max) != INTEGER_CST)
451 set_value_range (vr, t, min, max, equiv);
452 return;
455 /* Wrong order for min and max, to swap them and the VR type we need
456 to adjust them. */
457 if (tree_int_cst_lt (max, min))
459 tree one, tmp;
461 /* For one bit precision if max < min, then the swapped
462 range covers all values, so for VR_RANGE it is varying and
463 for VR_ANTI_RANGE empty range, so drop to varying as well. */
464 if (TYPE_PRECISION (TREE_TYPE (min)) == 1)
466 set_value_range_to_varying (vr);
467 return;
470 one = build_int_cst (TREE_TYPE (min), 1);
471 tmp = int_const_binop (PLUS_EXPR, max, one);
472 max = int_const_binop (MINUS_EXPR, min, one);
473 min = tmp;
475 /* There's one corner case, if we had [C+1, C] before we now have
476 that again. But this represents an empty value range, so drop
477 to varying in this case. */
478 if (tree_int_cst_lt (max, min))
480 set_value_range_to_varying (vr);
481 return;
484 t = t == VR_RANGE ? VR_ANTI_RANGE : VR_RANGE;
487 /* Anti-ranges that can be represented as ranges should be so. */
488 if (t == VR_ANTI_RANGE)
490 bool is_min = vrp_val_is_min (min);
491 bool is_max = vrp_val_is_max (max);
493 if (is_min && is_max)
495 /* We cannot deal with empty ranges, drop to varying.
496 ??? This could be VR_UNDEFINED instead. */
497 set_value_range_to_varying (vr);
498 return;
500 else if (TYPE_PRECISION (TREE_TYPE (min)) == 1
501 && (is_min || is_max))
503 /* Non-empty boolean ranges can always be represented
504 as a singleton range. */
505 if (is_min)
506 min = max = vrp_val_max (TREE_TYPE (min));
507 else
508 min = max = vrp_val_min (TREE_TYPE (min));
509 t = VR_RANGE;
511 else if (is_min
512 /* As a special exception preserve non-null ranges. */
513 && !(TYPE_UNSIGNED (TREE_TYPE (min))
514 && integer_zerop (max)))
516 tree one = build_int_cst (TREE_TYPE (max), 1);
517 min = int_const_binop (PLUS_EXPR, max, one);
518 max = vrp_val_max (TREE_TYPE (max));
519 t = VR_RANGE;
521 else if (is_max)
523 tree one = build_int_cst (TREE_TYPE (min), 1);
524 max = int_const_binop (MINUS_EXPR, min, one);
525 min = vrp_val_min (TREE_TYPE (min));
526 t = VR_RANGE;
530 /* Drop [-INF(OVF), +INF(OVF)] to varying. */
531 if (needs_overflow_infinity (TREE_TYPE (min))
532 && is_overflow_infinity (min)
533 && is_overflow_infinity (max))
535 set_value_range_to_varying (vr);
536 return;
539 set_value_range (vr, t, min, max, equiv);
542 /* Copy value range FROM into value range TO. */
544 static inline void
545 copy_value_range (value_range *to, value_range *from)
547 set_value_range (to, from->type, from->min, from->max, from->equiv);
550 /* Set value range VR to a single value. This function is only called
551 with values we get from statements, and exists to clear the
552 TREE_OVERFLOW flag so that we don't think we have an overflow
553 infinity when we shouldn't. */
555 static inline void
556 set_value_range_to_value (value_range *vr, tree val, bitmap equiv)
558 gcc_assert (is_gimple_min_invariant (val));
559 if (TREE_OVERFLOW_P (val))
560 val = drop_tree_overflow (val);
561 set_value_range (vr, VR_RANGE, val, val, equiv);
564 /* Set value range VR to a non-negative range of type TYPE.
565 OVERFLOW_INFINITY indicates whether to use an overflow infinity
566 rather than TYPE_MAX_VALUE; this should be true if we determine
567 that the range is nonnegative based on the assumption that signed
568 overflow does not occur. */
570 static inline void
571 set_value_range_to_nonnegative (value_range *vr, tree type,
572 bool overflow_infinity)
574 tree zero;
576 if (overflow_infinity && !supports_overflow_infinity (type))
578 set_value_range_to_varying (vr);
579 return;
582 zero = build_int_cst (type, 0);
583 set_value_range (vr, VR_RANGE, zero,
584 (overflow_infinity
585 ? positive_overflow_infinity (type)
586 : TYPE_MAX_VALUE (type)),
587 vr->equiv);
590 /* Set value range VR to a non-NULL range of type TYPE. */
592 static inline void
593 set_value_range_to_nonnull (value_range *vr, tree type)
595 tree zero = build_int_cst (type, 0);
596 set_value_range (vr, VR_ANTI_RANGE, zero, zero, vr->equiv);
600 /* Set value range VR to a NULL range of type TYPE. */
602 static inline void
603 set_value_range_to_null (value_range *vr, tree type)
605 set_value_range_to_value (vr, build_int_cst (type, 0), vr->equiv);
609 /* Set value range VR to a range of a truthvalue of type TYPE. */
611 static inline void
612 set_value_range_to_truthvalue (value_range *vr, tree type)
614 if (TYPE_PRECISION (type) == 1)
615 set_value_range_to_varying (vr);
616 else
617 set_value_range (vr, VR_RANGE,
618 build_int_cst (type, 0), build_int_cst (type, 1),
619 vr->equiv);
623 /* If abs (min) < abs (max), set VR to [-max, max], if
624 abs (min) >= abs (max), set VR to [-min, min]. */
626 static void
627 abs_extent_range (value_range *vr, tree min, tree max)
629 int cmp;
631 gcc_assert (TREE_CODE (min) == INTEGER_CST);
632 gcc_assert (TREE_CODE (max) == INTEGER_CST);
633 gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (min)));
634 gcc_assert (!TYPE_UNSIGNED (TREE_TYPE (min)));
635 min = fold_unary (ABS_EXPR, TREE_TYPE (min), min);
636 max = fold_unary (ABS_EXPR, TREE_TYPE (max), max);
637 if (TREE_OVERFLOW (min) || TREE_OVERFLOW (max))
639 set_value_range_to_varying (vr);
640 return;
642 cmp = compare_values (min, max);
643 if (cmp == -1)
644 min = fold_unary (NEGATE_EXPR, TREE_TYPE (min), max);
645 else if (cmp == 0 || cmp == 1)
647 max = min;
648 min = fold_unary (NEGATE_EXPR, TREE_TYPE (min), min);
650 else
652 set_value_range_to_varying (vr);
653 return;
655 set_and_canonicalize_value_range (vr, VR_RANGE, min, max, NULL);
659 /* Return value range information for VAR.
661 If we have no values ranges recorded (ie, VRP is not running), then
662 return NULL. Otherwise create an empty range if none existed for VAR. */
664 static value_range *
665 get_value_range (const_tree var)
667 static const value_range vr_const_varying
668 = { VR_VARYING, NULL_TREE, NULL_TREE, NULL };
669 value_range *vr;
670 tree sym;
671 unsigned ver = SSA_NAME_VERSION (var);
673 /* If we have no recorded ranges, then return NULL. */
674 if (! vr_value)
675 return NULL;
677 /* If we query the range for a new SSA name return an unmodifiable VARYING.
678 We should get here at most from the substitute-and-fold stage which
679 will never try to change values. */
680 if (ver >= num_vr_values)
681 return CONST_CAST (value_range *, &vr_const_varying);
683 vr = vr_value[ver];
684 if (vr)
685 return vr;
687 /* After propagation finished do not allocate new value-ranges. */
688 if (values_propagated)
689 return CONST_CAST (value_range *, &vr_const_varying);
691 /* Create a default value range. */
692 vr_value[ver] = vr = XCNEW (value_range);
694 /* Defer allocating the equivalence set. */
695 vr->equiv = NULL;
697 /* If VAR is a default definition of a parameter, the variable can
698 take any value in VAR's type. */
699 if (SSA_NAME_IS_DEFAULT_DEF (var))
701 sym = SSA_NAME_VAR (var);
702 if (TREE_CODE (sym) == PARM_DECL)
704 /* Try to use the "nonnull" attribute to create ~[0, 0]
705 anti-ranges for pointers. Note that this is only valid with
706 default definitions of PARM_DECLs. */
707 if (POINTER_TYPE_P (TREE_TYPE (sym))
708 && nonnull_arg_p (sym))
709 set_value_range_to_nonnull (vr, TREE_TYPE (sym));
710 else
711 set_value_range_to_varying (vr);
713 else if (TREE_CODE (sym) == RESULT_DECL
714 && DECL_BY_REFERENCE (sym))
715 set_value_range_to_nonnull (vr, TREE_TYPE (sym));
718 return vr;
721 /* Return true, if VAL1 and VAL2 are equal values for VRP purposes. */
723 static inline bool
724 vrp_operand_equal_p (const_tree val1, const_tree val2)
726 if (val1 == val2)
727 return true;
728 if (!val1 || !val2 || !operand_equal_p (val1, val2, 0))
729 return false;
730 return is_overflow_infinity (val1) == is_overflow_infinity (val2);
733 /* Return true, if the bitmaps B1 and B2 are equal. */
735 static inline bool
736 vrp_bitmap_equal_p (const_bitmap b1, const_bitmap b2)
738 return (b1 == b2
739 || ((!b1 || bitmap_empty_p (b1))
740 && (!b2 || bitmap_empty_p (b2)))
741 || (b1 && b2
742 && bitmap_equal_p (b1, b2)));
745 /* Update the value range and equivalence set for variable VAR to
746 NEW_VR. Return true if NEW_VR is different from VAR's previous
747 value.
749 NOTE: This function assumes that NEW_VR is a temporary value range
750 object created for the sole purpose of updating VAR's range. The
751 storage used by the equivalence set from NEW_VR will be freed by
752 this function. Do not call update_value_range when NEW_VR
753 is the range object associated with another SSA name. */
755 static inline bool
756 update_value_range (const_tree var, value_range *new_vr)
758 value_range *old_vr;
759 bool is_new;
761 /* If there is a value-range on the SSA name from earlier analysis
762 factor that in. */
763 if (INTEGRAL_TYPE_P (TREE_TYPE (var)))
765 wide_int min, max;
766 value_range_type rtype = get_range_info (var, &min, &max);
767 if (rtype == VR_RANGE || rtype == VR_ANTI_RANGE)
769 value_range nr;
770 nr.type = rtype;
771 nr.min = wide_int_to_tree (TREE_TYPE (var), min);
772 nr.max = wide_int_to_tree (TREE_TYPE (var), max);
773 nr.equiv = NULL;
774 vrp_intersect_ranges (new_vr, &nr);
778 /* Update the value range, if necessary. */
779 old_vr = get_value_range (var);
780 is_new = old_vr->type != new_vr->type
781 || !vrp_operand_equal_p (old_vr->min, new_vr->min)
782 || !vrp_operand_equal_p (old_vr->max, new_vr->max)
783 || !vrp_bitmap_equal_p (old_vr->equiv, new_vr->equiv);
785 if (is_new)
787 /* Do not allow transitions up the lattice. The following
788 is slightly more awkward than just new_vr->type < old_vr->type
789 because VR_RANGE and VR_ANTI_RANGE need to be considered
790 the same. We may not have is_new when transitioning to
791 UNDEFINED. If old_vr->type is VARYING, we shouldn't be
792 called. */
793 if (new_vr->type == VR_UNDEFINED)
795 BITMAP_FREE (new_vr->equiv);
796 set_value_range_to_varying (old_vr);
797 set_value_range_to_varying (new_vr);
798 return true;
800 else
801 set_value_range (old_vr, new_vr->type, new_vr->min, new_vr->max,
802 new_vr->equiv);
805 BITMAP_FREE (new_vr->equiv);
807 return is_new;
811 /* Add VAR and VAR's equivalence set to EQUIV. This is the central
812 point where equivalence processing can be turned on/off. */
814 static void
815 add_equivalence (bitmap *equiv, const_tree var)
817 unsigned ver = SSA_NAME_VERSION (var);
818 value_range *vr = vr_value[ver];
820 if (*equiv == NULL)
821 *equiv = BITMAP_ALLOC (NULL);
822 bitmap_set_bit (*equiv, ver);
823 if (vr && vr->equiv)
824 bitmap_ior_into (*equiv, vr->equiv);
828 /* Return true if VR is ~[0, 0]. */
830 static inline bool
831 range_is_nonnull (value_range *vr)
833 return vr->type == VR_ANTI_RANGE
834 && integer_zerop (vr->min)
835 && integer_zerop (vr->max);
839 /* Return true if VR is [0, 0]. */
841 static inline bool
842 range_is_null (value_range *vr)
844 return vr->type == VR_RANGE
845 && integer_zerop (vr->min)
846 && integer_zerop (vr->max);
849 /* Return true if max and min of VR are INTEGER_CST. It's not necessary
850 a singleton. */
852 static inline bool
853 range_int_cst_p (value_range *vr)
855 return (vr->type == VR_RANGE
856 && TREE_CODE (vr->max) == INTEGER_CST
857 && TREE_CODE (vr->min) == INTEGER_CST);
860 /* Return true if VR is a INTEGER_CST singleton. */
862 static inline bool
863 range_int_cst_singleton_p (value_range *vr)
865 return (range_int_cst_p (vr)
866 && !is_overflow_infinity (vr->min)
867 && !is_overflow_infinity (vr->max)
868 && tree_int_cst_equal (vr->min, vr->max));
871 /* Return true if value range VR involves at least one symbol. */
873 static inline bool
874 symbolic_range_p (value_range *vr)
876 return (!is_gimple_min_invariant (vr->min)
877 || !is_gimple_min_invariant (vr->max));
880 /* Return the single symbol (an SSA_NAME) contained in T if any, or NULL_TREE
881 otherwise. We only handle additive operations and set NEG to true if the
882 symbol is negated and INV to the invariant part, if any. */
884 static tree
885 get_single_symbol (tree t, bool *neg, tree *inv)
887 bool neg_;
888 tree inv_;
890 if (TREE_CODE (t) == PLUS_EXPR
891 || TREE_CODE (t) == POINTER_PLUS_EXPR
892 || TREE_CODE (t) == MINUS_EXPR)
894 if (is_gimple_min_invariant (TREE_OPERAND (t, 0)))
896 neg_ = (TREE_CODE (t) == MINUS_EXPR);
897 inv_ = TREE_OPERAND (t, 0);
898 t = TREE_OPERAND (t, 1);
900 else if (is_gimple_min_invariant (TREE_OPERAND (t, 1)))
902 neg_ = false;
903 inv_ = TREE_OPERAND (t, 1);
904 t = TREE_OPERAND (t, 0);
906 else
907 return NULL_TREE;
909 else
911 neg_ = false;
912 inv_ = NULL_TREE;
915 if (TREE_CODE (t) == NEGATE_EXPR)
917 t = TREE_OPERAND (t, 0);
918 neg_ = !neg_;
921 if (TREE_CODE (t) != SSA_NAME)
922 return NULL_TREE;
924 *neg = neg_;
925 *inv = inv_;
926 return t;
929 /* The reverse operation: build a symbolic expression with TYPE
930 from symbol SYM, negated according to NEG, and invariant INV. */
932 static tree
933 build_symbolic_expr (tree type, tree sym, bool neg, tree inv)
935 const bool pointer_p = POINTER_TYPE_P (type);
936 tree t = sym;
938 if (neg)
939 t = build1 (NEGATE_EXPR, type, t);
941 if (integer_zerop (inv))
942 return t;
944 return build2 (pointer_p ? POINTER_PLUS_EXPR : PLUS_EXPR, type, t, inv);
947 /* Return true if value range VR involves exactly one symbol SYM. */
949 static bool
950 symbolic_range_based_on_p (value_range *vr, const_tree sym)
952 bool neg, min_has_symbol, max_has_symbol;
953 tree inv;
955 if (is_gimple_min_invariant (vr->min))
956 min_has_symbol = false;
957 else if (get_single_symbol (vr->min, &neg, &inv) == sym)
958 min_has_symbol = true;
959 else
960 return false;
962 if (is_gimple_min_invariant (vr->max))
963 max_has_symbol = false;
964 else if (get_single_symbol (vr->max, &neg, &inv) == sym)
965 max_has_symbol = true;
966 else
967 return false;
969 return (min_has_symbol || max_has_symbol);
972 /* Return true if value range VR uses an overflow infinity. */
974 static inline bool
975 overflow_infinity_range_p (value_range *vr)
977 return (vr->type == VR_RANGE
978 && (is_overflow_infinity (vr->min)
979 || is_overflow_infinity (vr->max)));
982 /* Return false if we can not make a valid comparison based on VR;
983 this will be the case if it uses an overflow infinity and overflow
984 is not undefined (i.e., -fno-strict-overflow is in effect).
985 Otherwise return true, and set *STRICT_OVERFLOW_P to true if VR
986 uses an overflow infinity. */
988 static bool
989 usable_range_p (value_range *vr, bool *strict_overflow_p)
991 gcc_assert (vr->type == VR_RANGE);
992 if (is_overflow_infinity (vr->min))
994 *strict_overflow_p = true;
995 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (vr->min)))
996 return false;
998 if (is_overflow_infinity (vr->max))
1000 *strict_overflow_p = true;
1001 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (vr->max)))
1002 return false;
1004 return true;
1007 /* Return true if the result of assignment STMT is know to be non-zero.
1008 If the return value is based on the assumption that signed overflow is
1009 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1010 *STRICT_OVERFLOW_P.*/
1012 static bool
1013 gimple_assign_nonzero_warnv_p (gimple *stmt, bool *strict_overflow_p)
1015 enum tree_code code = gimple_assign_rhs_code (stmt);
1016 switch (get_gimple_rhs_class (code))
1018 case GIMPLE_UNARY_RHS:
1019 return tree_unary_nonzero_warnv_p (gimple_assign_rhs_code (stmt),
1020 gimple_expr_type (stmt),
1021 gimple_assign_rhs1 (stmt),
1022 strict_overflow_p);
1023 case GIMPLE_BINARY_RHS:
1024 return tree_binary_nonzero_warnv_p (gimple_assign_rhs_code (stmt),
1025 gimple_expr_type (stmt),
1026 gimple_assign_rhs1 (stmt),
1027 gimple_assign_rhs2 (stmt),
1028 strict_overflow_p);
1029 case GIMPLE_TERNARY_RHS:
1030 return false;
1031 case GIMPLE_SINGLE_RHS:
1032 return tree_single_nonzero_warnv_p (gimple_assign_rhs1 (stmt),
1033 strict_overflow_p);
1034 case GIMPLE_INVALID_RHS:
1035 gcc_unreachable ();
1036 default:
1037 gcc_unreachable ();
1041 /* Return true if STMT is known to compute a non-zero value.
1042 If the return value is based on the assumption that signed overflow is
1043 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1044 *STRICT_OVERFLOW_P.*/
1046 static bool
1047 gimple_stmt_nonzero_warnv_p (gimple *stmt, bool *strict_overflow_p)
1049 switch (gimple_code (stmt))
1051 case GIMPLE_ASSIGN:
1052 return gimple_assign_nonzero_warnv_p (stmt, strict_overflow_p);
1053 case GIMPLE_CALL:
1055 tree fndecl = gimple_call_fndecl (stmt);
1056 if (!fndecl) return false;
1057 if (flag_delete_null_pointer_checks && !flag_check_new
1058 && DECL_IS_OPERATOR_NEW (fndecl)
1059 && !TREE_NOTHROW (fndecl))
1060 return true;
1061 /* References are always non-NULL. */
1062 if (flag_delete_null_pointer_checks
1063 && TREE_CODE (TREE_TYPE (fndecl)) == REFERENCE_TYPE)
1064 return true;
1065 if (flag_delete_null_pointer_checks &&
1066 lookup_attribute ("returns_nonnull",
1067 TYPE_ATTRIBUTES (gimple_call_fntype (stmt))))
1068 return true;
1069 return gimple_alloca_call_p (stmt);
1071 default:
1072 gcc_unreachable ();
1076 /* Like tree_expr_nonzero_warnv_p, but this function uses value ranges
1077 obtained so far. */
1079 static bool
1080 vrp_stmt_computes_nonzero (gimple *stmt, bool *strict_overflow_p)
1082 if (gimple_stmt_nonzero_warnv_p (stmt, strict_overflow_p))
1083 return true;
1085 /* If we have an expression of the form &X->a, then the expression
1086 is nonnull if X is nonnull. */
1087 if (is_gimple_assign (stmt)
1088 && gimple_assign_rhs_code (stmt) == ADDR_EXPR)
1090 tree expr = gimple_assign_rhs1 (stmt);
1091 tree base = get_base_address (TREE_OPERAND (expr, 0));
1093 if (base != NULL_TREE
1094 && TREE_CODE (base) == MEM_REF
1095 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
1097 value_range *vr = get_value_range (TREE_OPERAND (base, 0));
1098 if (range_is_nonnull (vr))
1099 return true;
1103 return false;
1106 /* Returns true if EXPR is a valid value (as expected by compare_values) --
1107 a gimple invariant, or SSA_NAME +- CST. */
1109 static bool
1110 valid_value_p (tree expr)
1112 if (TREE_CODE (expr) == SSA_NAME)
1113 return true;
1115 if (TREE_CODE (expr) == PLUS_EXPR
1116 || TREE_CODE (expr) == MINUS_EXPR)
1117 return (TREE_CODE (TREE_OPERAND (expr, 0)) == SSA_NAME
1118 && TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST);
1120 return is_gimple_min_invariant (expr);
1123 /* Return
1124 1 if VAL < VAL2
1125 0 if !(VAL < VAL2)
1126 -2 if those are incomparable. */
1127 static inline int
1128 operand_less_p (tree val, tree val2)
1130 /* LT is folded faster than GE and others. Inline the common case. */
1131 if (TREE_CODE (val) == INTEGER_CST && TREE_CODE (val2) == INTEGER_CST)
1132 return tree_int_cst_lt (val, val2);
1133 else
1135 tree tcmp;
1137 fold_defer_overflow_warnings ();
1139 tcmp = fold_binary_to_constant (LT_EXPR, boolean_type_node, val, val2);
1141 fold_undefer_and_ignore_overflow_warnings ();
1143 if (!tcmp
1144 || TREE_CODE (tcmp) != INTEGER_CST)
1145 return -2;
1147 if (!integer_zerop (tcmp))
1148 return 1;
1151 /* val >= val2, not considering overflow infinity. */
1152 if (is_negative_overflow_infinity (val))
1153 return is_negative_overflow_infinity (val2) ? 0 : 1;
1154 else if (is_positive_overflow_infinity (val2))
1155 return is_positive_overflow_infinity (val) ? 0 : 1;
1157 return 0;
1160 /* Compare two values VAL1 and VAL2. Return
1162 -2 if VAL1 and VAL2 cannot be compared at compile-time,
1163 -1 if VAL1 < VAL2,
1164 0 if VAL1 == VAL2,
1165 +1 if VAL1 > VAL2, and
1166 +2 if VAL1 != VAL2
1168 This is similar to tree_int_cst_compare but supports pointer values
1169 and values that cannot be compared at compile time.
1171 If STRICT_OVERFLOW_P is not NULL, then set *STRICT_OVERFLOW_P to
1172 true if the return value is only valid if we assume that signed
1173 overflow is undefined. */
1175 static int
1176 compare_values_warnv (tree val1, tree val2, bool *strict_overflow_p)
1178 if (val1 == val2)
1179 return 0;
1181 /* Below we rely on the fact that VAL1 and VAL2 are both pointers or
1182 both integers. */
1183 gcc_assert (POINTER_TYPE_P (TREE_TYPE (val1))
1184 == POINTER_TYPE_P (TREE_TYPE (val2)));
1186 /* Convert the two values into the same type. This is needed because
1187 sizetype causes sign extension even for unsigned types. */
1188 val2 = fold_convert (TREE_TYPE (val1), val2);
1189 STRIP_USELESS_TYPE_CONVERSION (val2);
1191 const bool overflow_undefined
1192 = INTEGRAL_TYPE_P (TREE_TYPE (val1))
1193 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (val1));
1194 tree inv1, inv2;
1195 bool neg1, neg2;
1196 tree sym1 = get_single_symbol (val1, &neg1, &inv1);
1197 tree sym2 = get_single_symbol (val2, &neg2, &inv2);
1199 /* If VAL1 and VAL2 are of the form '[-]NAME [+ CST]', return -1 or +1
1200 accordingly. If VAL1 and VAL2 don't use the same name, return -2. */
1201 if (sym1 && sym2)
1203 /* Both values must use the same name with the same sign. */
1204 if (sym1 != sym2 || neg1 != neg2)
1205 return -2;
1207 /* [-]NAME + CST == [-]NAME + CST. */
1208 if (inv1 == inv2)
1209 return 0;
1211 /* If overflow is defined we cannot simplify more. */
1212 if (!overflow_undefined)
1213 return -2;
1215 if (strict_overflow_p != NULL
1216 && (!inv1 || !TREE_NO_WARNING (val1))
1217 && (!inv2 || !TREE_NO_WARNING (val2)))
1218 *strict_overflow_p = true;
1220 if (!inv1)
1221 inv1 = build_int_cst (TREE_TYPE (val1), 0);
1222 if (!inv2)
1223 inv2 = build_int_cst (TREE_TYPE (val2), 0);
1225 return compare_values_warnv (inv1, inv2, strict_overflow_p);
1228 const bool cst1 = is_gimple_min_invariant (val1);
1229 const bool cst2 = is_gimple_min_invariant (val2);
1231 /* If one is of the form '[-]NAME + CST' and the other is constant, then
1232 it might be possible to say something depending on the constants. */
1233 if ((sym1 && inv1 && cst2) || (sym2 && inv2 && cst1))
1235 if (!overflow_undefined)
1236 return -2;
1238 if (strict_overflow_p != NULL
1239 && (!sym1 || !TREE_NO_WARNING (val1))
1240 && (!sym2 || !TREE_NO_WARNING (val2)))
1241 *strict_overflow_p = true;
1243 const signop sgn = TYPE_SIGN (TREE_TYPE (val1));
1244 tree cst = cst1 ? val1 : val2;
1245 tree inv = cst1 ? inv2 : inv1;
1247 /* Compute the difference between the constants. If it overflows or
1248 underflows, this means that we can trivially compare the NAME with
1249 it and, consequently, the two values with each other. */
1250 wide_int diff = wi::sub (cst, inv);
1251 if (wi::cmp (0, inv, sgn) != wi::cmp (diff, cst, sgn))
1253 const int res = wi::cmp (cst, inv, sgn);
1254 return cst1 ? res : -res;
1257 return -2;
1260 /* We cannot say anything more for non-constants. */
1261 if (!cst1 || !cst2)
1262 return -2;
1264 if (!POINTER_TYPE_P (TREE_TYPE (val1)))
1266 /* We cannot compare overflowed values, except for overflow
1267 infinities. */
1268 if (TREE_OVERFLOW (val1) || TREE_OVERFLOW (val2))
1270 if (strict_overflow_p != NULL)
1271 *strict_overflow_p = true;
1272 if (is_negative_overflow_infinity (val1))
1273 return is_negative_overflow_infinity (val2) ? 0 : -1;
1274 else if (is_negative_overflow_infinity (val2))
1275 return 1;
1276 else if (is_positive_overflow_infinity (val1))
1277 return is_positive_overflow_infinity (val2) ? 0 : 1;
1278 else if (is_positive_overflow_infinity (val2))
1279 return -1;
1280 return -2;
1283 return tree_int_cst_compare (val1, val2);
1285 else
1287 tree t;
1289 /* First see if VAL1 and VAL2 are not the same. */
1290 if (val1 == val2 || operand_equal_p (val1, val2, 0))
1291 return 0;
1293 /* If VAL1 is a lower address than VAL2, return -1. */
1294 if (operand_less_p (val1, val2) == 1)
1295 return -1;
1297 /* If VAL1 is a higher address than VAL2, return +1. */
1298 if (operand_less_p (val2, val1) == 1)
1299 return 1;
1301 /* If VAL1 is different than VAL2, return +2.
1302 For integer constants we either have already returned -1 or 1
1303 or they are equivalent. We still might succeed in proving
1304 something about non-trivial operands. */
1305 if (TREE_CODE (val1) != INTEGER_CST
1306 || TREE_CODE (val2) != INTEGER_CST)
1308 t = fold_binary_to_constant (NE_EXPR, boolean_type_node, val1, val2);
1309 if (t && integer_onep (t))
1310 return 2;
1313 return -2;
1317 /* Compare values like compare_values_warnv, but treat comparisons of
1318 nonconstants which rely on undefined overflow as incomparable. */
1320 static int
1321 compare_values (tree val1, tree val2)
1323 bool sop;
1324 int ret;
1326 sop = false;
1327 ret = compare_values_warnv (val1, val2, &sop);
1328 if (sop
1329 && (!is_gimple_min_invariant (val1) || !is_gimple_min_invariant (val2)))
1330 ret = -2;
1331 return ret;
1335 /* Return 1 if VAL is inside value range MIN <= VAL <= MAX,
1336 0 if VAL is not inside [MIN, MAX],
1337 -2 if we cannot tell either way.
1339 Benchmark compile/20001226-1.c compilation time after changing this
1340 function. */
1342 static inline int
1343 value_inside_range (tree val, tree min, tree max)
1345 int cmp1, cmp2;
1347 cmp1 = operand_less_p (val, min);
1348 if (cmp1 == -2)
1349 return -2;
1350 if (cmp1 == 1)
1351 return 0;
1353 cmp2 = operand_less_p (max, val);
1354 if (cmp2 == -2)
1355 return -2;
1357 return !cmp2;
1361 /* Return true if value ranges VR0 and VR1 have a non-empty
1362 intersection.
1364 Benchmark compile/20001226-1.c compilation time after changing this
1365 function.
1368 static inline bool
1369 value_ranges_intersect_p (value_range *vr0, value_range *vr1)
1371 /* The value ranges do not intersect if the maximum of the first range is
1372 less than the minimum of the second range or vice versa.
1373 When those relations are unknown, we can't do any better. */
1374 if (operand_less_p (vr0->max, vr1->min) != 0)
1375 return false;
1376 if (operand_less_p (vr1->max, vr0->min) != 0)
1377 return false;
1378 return true;
1382 /* Return 1 if [MIN, MAX] includes the value zero, 0 if it does not
1383 include the value zero, -2 if we cannot tell. */
1385 static inline int
1386 range_includes_zero_p (tree min, tree max)
1388 tree zero = build_int_cst (TREE_TYPE (min), 0);
1389 return value_inside_range (zero, min, max);
1392 /* Return true if *VR is know to only contain nonnegative values. */
1394 static inline bool
1395 value_range_nonnegative_p (value_range *vr)
1397 /* Testing for VR_ANTI_RANGE is not useful here as any anti-range
1398 which would return a useful value should be encoded as a
1399 VR_RANGE. */
1400 if (vr->type == VR_RANGE)
1402 int result = compare_values (vr->min, integer_zero_node);
1403 return (result == 0 || result == 1);
1406 return false;
1409 /* If *VR has a value rante that is a single constant value return that,
1410 otherwise return NULL_TREE. */
1412 static tree
1413 value_range_constant_singleton (value_range *vr)
1415 if (vr->type == VR_RANGE
1416 && operand_equal_p (vr->min, vr->max, 0)
1417 && is_gimple_min_invariant (vr->min))
1418 return vr->min;
1420 return NULL_TREE;
1423 /* If OP has a value range with a single constant value return that,
1424 otherwise return NULL_TREE. This returns OP itself if OP is a
1425 constant. */
1427 static tree
1428 op_with_constant_singleton_value_range (tree op)
1430 if (is_gimple_min_invariant (op))
1431 return op;
1433 if (TREE_CODE (op) != SSA_NAME)
1434 return NULL_TREE;
1436 return value_range_constant_singleton (get_value_range (op));
1439 /* Return true if op is in a boolean [0, 1] value-range. */
1441 static bool
1442 op_with_boolean_value_range_p (tree op)
1444 value_range *vr;
1446 if (TYPE_PRECISION (TREE_TYPE (op)) == 1)
1447 return true;
1449 if (integer_zerop (op)
1450 || integer_onep (op))
1451 return true;
1453 if (TREE_CODE (op) != SSA_NAME)
1454 return false;
1456 vr = get_value_range (op);
1457 return (vr->type == VR_RANGE
1458 && integer_zerop (vr->min)
1459 && integer_onep (vr->max));
1462 /* Extract value range information from an ASSERT_EXPR EXPR and store
1463 it in *VR_P. */
1465 static void
1466 extract_range_from_assert (value_range *vr_p, tree expr)
1468 tree var, cond, limit, min, max, type;
1469 value_range *limit_vr;
1470 enum tree_code cond_code;
1472 var = ASSERT_EXPR_VAR (expr);
1473 cond = ASSERT_EXPR_COND (expr);
1475 gcc_assert (COMPARISON_CLASS_P (cond));
1477 /* Find VAR in the ASSERT_EXPR conditional. */
1478 if (var == TREE_OPERAND (cond, 0)
1479 || TREE_CODE (TREE_OPERAND (cond, 0)) == PLUS_EXPR
1480 || TREE_CODE (TREE_OPERAND (cond, 0)) == NOP_EXPR)
1482 /* If the predicate is of the form VAR COMP LIMIT, then we just
1483 take LIMIT from the RHS and use the same comparison code. */
1484 cond_code = TREE_CODE (cond);
1485 limit = TREE_OPERAND (cond, 1);
1486 cond = TREE_OPERAND (cond, 0);
1488 else
1490 /* If the predicate is of the form LIMIT COMP VAR, then we need
1491 to flip around the comparison code to create the proper range
1492 for VAR. */
1493 cond_code = swap_tree_comparison (TREE_CODE (cond));
1494 limit = TREE_OPERAND (cond, 0);
1495 cond = TREE_OPERAND (cond, 1);
1498 limit = avoid_overflow_infinity (limit);
1500 type = TREE_TYPE (var);
1501 gcc_assert (limit != var);
1503 /* For pointer arithmetic, we only keep track of pointer equality
1504 and inequality. */
1505 if (POINTER_TYPE_P (type) && cond_code != NE_EXPR && cond_code != EQ_EXPR)
1507 set_value_range_to_varying (vr_p);
1508 return;
1511 /* If LIMIT is another SSA name and LIMIT has a range of its own,
1512 try to use LIMIT's range to avoid creating symbolic ranges
1513 unnecessarily. */
1514 limit_vr = (TREE_CODE (limit) == SSA_NAME) ? get_value_range (limit) : NULL;
1516 /* LIMIT's range is only interesting if it has any useful information. */
1517 if (! limit_vr
1518 || limit_vr->type == VR_UNDEFINED
1519 || limit_vr->type == VR_VARYING
1520 || (symbolic_range_p (limit_vr)
1521 && ! (limit_vr->type == VR_RANGE
1522 && (limit_vr->min == limit_vr->max
1523 || operand_equal_p (limit_vr->min, limit_vr->max, 0)))))
1524 limit_vr = NULL;
1526 /* Initially, the new range has the same set of equivalences of
1527 VAR's range. This will be revised before returning the final
1528 value. Since assertions may be chained via mutually exclusive
1529 predicates, we will need to trim the set of equivalences before
1530 we are done. */
1531 gcc_assert (vr_p->equiv == NULL);
1532 add_equivalence (&vr_p->equiv, var);
1534 /* Extract a new range based on the asserted comparison for VAR and
1535 LIMIT's value range. Notice that if LIMIT has an anti-range, we
1536 will only use it for equality comparisons (EQ_EXPR). For any
1537 other kind of assertion, we cannot derive a range from LIMIT's
1538 anti-range that can be used to describe the new range. For
1539 instance, ASSERT_EXPR <x_2, x_2 <= b_4>. If b_4 is ~[2, 10],
1540 then b_4 takes on the ranges [-INF, 1] and [11, +INF]. There is
1541 no single range for x_2 that could describe LE_EXPR, so we might
1542 as well build the range [b_4, +INF] for it.
1543 One special case we handle is extracting a range from a
1544 range test encoded as (unsigned)var + CST <= limit. */
1545 if (TREE_CODE (cond) == NOP_EXPR
1546 || TREE_CODE (cond) == PLUS_EXPR)
1548 if (TREE_CODE (cond) == PLUS_EXPR)
1550 min = fold_build1 (NEGATE_EXPR, TREE_TYPE (TREE_OPERAND (cond, 1)),
1551 TREE_OPERAND (cond, 1));
1552 max = int_const_binop (PLUS_EXPR, limit, min);
1553 cond = TREE_OPERAND (cond, 0);
1555 else
1557 min = build_int_cst (TREE_TYPE (var), 0);
1558 max = limit;
1561 /* Make sure to not set TREE_OVERFLOW on the final type
1562 conversion. We are willingly interpreting large positive
1563 unsigned values as negative signed values here. */
1564 min = force_fit_type (TREE_TYPE (var), wi::to_widest (min), 0, false);
1565 max = force_fit_type (TREE_TYPE (var), wi::to_widest (max), 0, false);
1567 /* We can transform a max, min range to an anti-range or
1568 vice-versa. Use set_and_canonicalize_value_range which does
1569 this for us. */
1570 if (cond_code == LE_EXPR)
1571 set_and_canonicalize_value_range (vr_p, VR_RANGE,
1572 min, max, vr_p->equiv);
1573 else if (cond_code == GT_EXPR)
1574 set_and_canonicalize_value_range (vr_p, VR_ANTI_RANGE,
1575 min, max, vr_p->equiv);
1576 else
1577 gcc_unreachable ();
1579 else if (cond_code == EQ_EXPR)
1581 enum value_range_type range_type;
1583 if (limit_vr)
1585 range_type = limit_vr->type;
1586 min = limit_vr->min;
1587 max = limit_vr->max;
1589 else
1591 range_type = VR_RANGE;
1592 min = limit;
1593 max = limit;
1596 set_value_range (vr_p, range_type, min, max, vr_p->equiv);
1598 /* When asserting the equality VAR == LIMIT and LIMIT is another
1599 SSA name, the new range will also inherit the equivalence set
1600 from LIMIT. */
1601 if (TREE_CODE (limit) == SSA_NAME)
1602 add_equivalence (&vr_p->equiv, limit);
1604 else if (cond_code == NE_EXPR)
1606 /* As described above, when LIMIT's range is an anti-range and
1607 this assertion is an inequality (NE_EXPR), then we cannot
1608 derive anything from the anti-range. For instance, if
1609 LIMIT's range was ~[0, 0], the assertion 'VAR != LIMIT' does
1610 not imply that VAR's range is [0, 0]. So, in the case of
1611 anti-ranges, we just assert the inequality using LIMIT and
1612 not its anti-range.
1614 If LIMIT_VR is a range, we can only use it to build a new
1615 anti-range if LIMIT_VR is a single-valued range. For
1616 instance, if LIMIT_VR is [0, 1], the predicate
1617 VAR != [0, 1] does not mean that VAR's range is ~[0, 1].
1618 Rather, it means that for value 0 VAR should be ~[0, 0]
1619 and for value 1, VAR should be ~[1, 1]. We cannot
1620 represent these ranges.
1622 The only situation in which we can build a valid
1623 anti-range is when LIMIT_VR is a single-valued range
1624 (i.e., LIMIT_VR->MIN == LIMIT_VR->MAX). In that case,
1625 build the anti-range ~[LIMIT_VR->MIN, LIMIT_VR->MAX]. */
1626 if (limit_vr
1627 && limit_vr->type == VR_RANGE
1628 && compare_values (limit_vr->min, limit_vr->max) == 0)
1630 min = limit_vr->min;
1631 max = limit_vr->max;
1633 else
1635 /* In any other case, we cannot use LIMIT's range to build a
1636 valid anti-range. */
1637 min = max = limit;
1640 /* If MIN and MAX cover the whole range for their type, then
1641 just use the original LIMIT. */
1642 if (INTEGRAL_TYPE_P (type)
1643 && vrp_val_is_min (min)
1644 && vrp_val_is_max (max))
1645 min = max = limit;
1647 set_and_canonicalize_value_range (vr_p, VR_ANTI_RANGE,
1648 min, max, vr_p->equiv);
1650 else if (cond_code == LE_EXPR || cond_code == LT_EXPR)
1652 min = TYPE_MIN_VALUE (type);
1654 if (limit_vr == NULL || limit_vr->type == VR_ANTI_RANGE)
1655 max = limit;
1656 else
1658 /* If LIMIT_VR is of the form [N1, N2], we need to build the
1659 range [MIN, N2] for LE_EXPR and [MIN, N2 - 1] for
1660 LT_EXPR. */
1661 max = limit_vr->max;
1664 /* If the maximum value forces us to be out of bounds, simply punt.
1665 It would be pointless to try and do anything more since this
1666 all should be optimized away above us. */
1667 if ((cond_code == LT_EXPR
1668 && compare_values (max, min) == 0)
1669 || is_overflow_infinity (max))
1670 set_value_range_to_varying (vr_p);
1671 else
1673 /* For LT_EXPR, we create the range [MIN, MAX - 1]. */
1674 if (cond_code == LT_EXPR)
1676 if (TYPE_PRECISION (TREE_TYPE (max)) == 1
1677 && !TYPE_UNSIGNED (TREE_TYPE (max)))
1678 max = fold_build2 (PLUS_EXPR, TREE_TYPE (max), max,
1679 build_int_cst (TREE_TYPE (max), -1));
1680 else
1681 max = fold_build2 (MINUS_EXPR, TREE_TYPE (max), max,
1682 build_int_cst (TREE_TYPE (max), 1));
1683 if (EXPR_P (max))
1684 TREE_NO_WARNING (max) = 1;
1687 set_value_range (vr_p, VR_RANGE, min, max, vr_p->equiv);
1690 else if (cond_code == GE_EXPR || cond_code == GT_EXPR)
1692 max = TYPE_MAX_VALUE (type);
1694 if (limit_vr == NULL || limit_vr->type == VR_ANTI_RANGE)
1695 min = limit;
1696 else
1698 /* If LIMIT_VR is of the form [N1, N2], we need to build the
1699 range [N1, MAX] for GE_EXPR and [N1 + 1, MAX] for
1700 GT_EXPR. */
1701 min = limit_vr->min;
1704 /* If the minimum value forces us to be out of bounds, simply punt.
1705 It would be pointless to try and do anything more since this
1706 all should be optimized away above us. */
1707 if ((cond_code == GT_EXPR
1708 && compare_values (min, max) == 0)
1709 || is_overflow_infinity (min))
1710 set_value_range_to_varying (vr_p);
1711 else
1713 /* For GT_EXPR, we create the range [MIN + 1, MAX]. */
1714 if (cond_code == GT_EXPR)
1716 if (TYPE_PRECISION (TREE_TYPE (min)) == 1
1717 && !TYPE_UNSIGNED (TREE_TYPE (min)))
1718 min = fold_build2 (MINUS_EXPR, TREE_TYPE (min), min,
1719 build_int_cst (TREE_TYPE (min), -1));
1720 else
1721 min = fold_build2 (PLUS_EXPR, TREE_TYPE (min), min,
1722 build_int_cst (TREE_TYPE (min), 1));
1723 if (EXPR_P (min))
1724 TREE_NO_WARNING (min) = 1;
1727 set_value_range (vr_p, VR_RANGE, min, max, vr_p->equiv);
1730 else
1731 gcc_unreachable ();
1733 /* Finally intersect the new range with what we already know about var. */
1734 vrp_intersect_ranges (vr_p, get_value_range (var));
1738 /* Extract range information from SSA name VAR and store it in VR. If
1739 VAR has an interesting range, use it. Otherwise, create the
1740 range [VAR, VAR] and return it. This is useful in situations where
1741 we may have conditionals testing values of VARYING names. For
1742 instance,
1744 x_3 = y_5;
1745 if (x_3 > y_5)
1748 Even if y_5 is deemed VARYING, we can determine that x_3 > y_5 is
1749 always false. */
1751 static void
1752 extract_range_from_ssa_name (value_range *vr, tree var)
1754 value_range *var_vr = get_value_range (var);
1756 if (var_vr->type != VR_VARYING)
1757 copy_value_range (vr, var_vr);
1758 else
1759 set_value_range (vr, VR_RANGE, var, var, NULL);
1761 add_equivalence (&vr->equiv, var);
1765 /* Wrapper around int_const_binop. If the operation overflows and we
1766 are not using wrapping arithmetic, then adjust the result to be
1767 -INF or +INF depending on CODE, VAL1 and VAL2. This can return
1768 NULL_TREE if we need to use an overflow infinity representation but
1769 the type does not support it. */
1771 static tree
1772 vrp_int_const_binop (enum tree_code code, tree val1, tree val2)
1774 tree res;
1776 res = int_const_binop (code, val1, val2);
1778 /* If we are using unsigned arithmetic, operate symbolically
1779 on -INF and +INF as int_const_binop only handles signed overflow. */
1780 if (TYPE_UNSIGNED (TREE_TYPE (val1)))
1782 int checkz = compare_values (res, val1);
1783 bool overflow = false;
1785 /* Ensure that res = val1 [+*] val2 >= val1
1786 or that res = val1 - val2 <= val1. */
1787 if ((code == PLUS_EXPR
1788 && !(checkz == 1 || checkz == 0))
1789 || (code == MINUS_EXPR
1790 && !(checkz == 0 || checkz == -1)))
1792 overflow = true;
1794 /* Checking for multiplication overflow is done by dividing the
1795 output of the multiplication by the first input of the
1796 multiplication. If the result of that division operation is
1797 not equal to the second input of the multiplication, then the
1798 multiplication overflowed. */
1799 else if (code == MULT_EXPR && !integer_zerop (val1))
1801 tree tmp = int_const_binop (TRUNC_DIV_EXPR,
1802 res,
1803 val1);
1804 int check = compare_values (tmp, val2);
1806 if (check != 0)
1807 overflow = true;
1810 if (overflow)
1812 res = copy_node (res);
1813 TREE_OVERFLOW (res) = 1;
1817 else if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (val1)))
1818 /* If the singed operation wraps then int_const_binop has done
1819 everything we want. */
1821 /* Signed division of -1/0 overflows and by the time it gets here
1822 returns NULL_TREE. */
1823 else if (!res)
1824 return NULL_TREE;
1825 else if ((TREE_OVERFLOW (res)
1826 && !TREE_OVERFLOW (val1)
1827 && !TREE_OVERFLOW (val2))
1828 || is_overflow_infinity (val1)
1829 || is_overflow_infinity (val2))
1831 /* If the operation overflowed but neither VAL1 nor VAL2 are
1832 overflown, return -INF or +INF depending on the operation
1833 and the combination of signs of the operands. */
1834 int sgn1 = tree_int_cst_sgn (val1);
1835 int sgn2 = tree_int_cst_sgn (val2);
1837 if (needs_overflow_infinity (TREE_TYPE (res))
1838 && !supports_overflow_infinity (TREE_TYPE (res)))
1839 return NULL_TREE;
1841 /* We have to punt on adding infinities of different signs,
1842 since we can't tell what the sign of the result should be.
1843 Likewise for subtracting infinities of the same sign. */
1844 if (((code == PLUS_EXPR && sgn1 != sgn2)
1845 || (code == MINUS_EXPR && sgn1 == sgn2))
1846 && is_overflow_infinity (val1)
1847 && is_overflow_infinity (val2))
1848 return NULL_TREE;
1850 /* Don't try to handle division or shifting of infinities. */
1851 if ((code == TRUNC_DIV_EXPR
1852 || code == FLOOR_DIV_EXPR
1853 || code == CEIL_DIV_EXPR
1854 || code == EXACT_DIV_EXPR
1855 || code == ROUND_DIV_EXPR
1856 || code == RSHIFT_EXPR)
1857 && (is_overflow_infinity (val1)
1858 || is_overflow_infinity (val2)))
1859 return NULL_TREE;
1861 /* Notice that we only need to handle the restricted set of
1862 operations handled by extract_range_from_binary_expr.
1863 Among them, only multiplication, addition and subtraction
1864 can yield overflow without overflown operands because we
1865 are working with integral types only... except in the
1866 case VAL1 = -INF and VAL2 = -1 which overflows to +INF
1867 for division too. */
1869 /* For multiplication, the sign of the overflow is given
1870 by the comparison of the signs of the operands. */
1871 if ((code == MULT_EXPR && sgn1 == sgn2)
1872 /* For addition, the operands must be of the same sign
1873 to yield an overflow. Its sign is therefore that
1874 of one of the operands, for example the first. For
1875 infinite operands X + -INF is negative, not positive. */
1876 || (code == PLUS_EXPR
1877 && (sgn1 >= 0
1878 ? !is_negative_overflow_infinity (val2)
1879 : is_positive_overflow_infinity (val2)))
1880 /* For subtraction, non-infinite operands must be of
1881 different signs to yield an overflow. Its sign is
1882 therefore that of the first operand or the opposite of
1883 that of the second operand. A first operand of 0 counts
1884 as positive here, for the corner case 0 - (-INF), which
1885 overflows, but must yield +INF. For infinite operands 0
1886 - INF is negative, not positive. */
1887 || (code == MINUS_EXPR
1888 && (sgn1 >= 0
1889 ? !is_positive_overflow_infinity (val2)
1890 : is_negative_overflow_infinity (val2)))
1891 /* We only get in here with positive shift count, so the
1892 overflow direction is the same as the sign of val1.
1893 Actually rshift does not overflow at all, but we only
1894 handle the case of shifting overflowed -INF and +INF. */
1895 || (code == RSHIFT_EXPR
1896 && sgn1 >= 0)
1897 /* For division, the only case is -INF / -1 = +INF. */
1898 || 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 return (needs_overflow_infinity (TREE_TYPE (res))
1904 ? positive_overflow_infinity (TREE_TYPE (res))
1905 : TYPE_MAX_VALUE (TREE_TYPE (res)));
1906 else
1907 return (needs_overflow_infinity (TREE_TYPE (res))
1908 ? negative_overflow_infinity (TREE_TYPE (res))
1909 : TYPE_MIN_VALUE (TREE_TYPE (res)));
1912 return res;
1916 /* For range VR compute two wide_int bitmasks. In *MAY_BE_NONZERO
1917 bitmask if some bit is unset, it means for all numbers in the range
1918 the bit is 0, otherwise it might be 0 or 1. In *MUST_BE_NONZERO
1919 bitmask if some bit is set, it means for all numbers in the range
1920 the bit is 1, otherwise it might be 0 or 1. */
1922 static bool
1923 zero_nonzero_bits_from_vr (const tree expr_type,
1924 value_range *vr,
1925 wide_int *may_be_nonzero,
1926 wide_int *must_be_nonzero)
1928 *may_be_nonzero = wi::minus_one (TYPE_PRECISION (expr_type));
1929 *must_be_nonzero = wi::zero (TYPE_PRECISION (expr_type));
1930 if (!range_int_cst_p (vr)
1931 || is_overflow_infinity (vr->min)
1932 || is_overflow_infinity (vr->max))
1933 return false;
1935 if (range_int_cst_singleton_p (vr))
1937 *may_be_nonzero = vr->min;
1938 *must_be_nonzero = *may_be_nonzero;
1940 else if (tree_int_cst_sgn (vr->min) >= 0
1941 || tree_int_cst_sgn (vr->max) < 0)
1943 wide_int xor_mask = wi::bit_xor (vr->min, vr->max);
1944 *may_be_nonzero = wi::bit_or (vr->min, vr->max);
1945 *must_be_nonzero = wi::bit_and (vr->min, vr->max);
1946 if (xor_mask != 0)
1948 wide_int mask = wi::mask (wi::floor_log2 (xor_mask), false,
1949 may_be_nonzero->get_precision ());
1950 *may_be_nonzero = *may_be_nonzero | mask;
1951 *must_be_nonzero = must_be_nonzero->and_not (mask);
1955 return true;
1958 /* Create two value-ranges in *VR0 and *VR1 from the anti-range *AR
1959 so that *VR0 U *VR1 == *AR. Returns true if that is possible,
1960 false otherwise. If *AR can be represented with a single range
1961 *VR1 will be VR_UNDEFINED. */
1963 static bool
1964 ranges_from_anti_range (value_range *ar,
1965 value_range *vr0, value_range *vr1)
1967 tree type = TREE_TYPE (ar->min);
1969 vr0->type = VR_UNDEFINED;
1970 vr1->type = VR_UNDEFINED;
1972 if (ar->type != VR_ANTI_RANGE
1973 || TREE_CODE (ar->min) != INTEGER_CST
1974 || TREE_CODE (ar->max) != INTEGER_CST
1975 || !vrp_val_min (type)
1976 || !vrp_val_max (type))
1977 return false;
1979 if (!vrp_val_is_min (ar->min))
1981 vr0->type = VR_RANGE;
1982 vr0->min = vrp_val_min (type);
1983 vr0->max = wide_int_to_tree (type, wi::sub (ar->min, 1));
1985 if (!vrp_val_is_max (ar->max))
1987 vr1->type = VR_RANGE;
1988 vr1->min = wide_int_to_tree (type, wi::add (ar->max, 1));
1989 vr1->max = vrp_val_max (type);
1991 if (vr0->type == VR_UNDEFINED)
1993 *vr0 = *vr1;
1994 vr1->type = VR_UNDEFINED;
1997 return vr0->type != VR_UNDEFINED;
2000 /* Helper to extract a value-range *VR for a multiplicative operation
2001 *VR0 CODE *VR1. */
2003 static void
2004 extract_range_from_multiplicative_op_1 (value_range *vr,
2005 enum tree_code code,
2006 value_range *vr0, value_range *vr1)
2008 enum value_range_type type;
2009 tree val[4];
2010 size_t i;
2011 tree min, max;
2012 bool sop;
2013 int cmp;
2015 /* Multiplications, divisions and shifts are a bit tricky to handle,
2016 depending on the mix of signs we have in the two ranges, we
2017 need to operate on different values to get the minimum and
2018 maximum values for the new range. One approach is to figure
2019 out all the variations of range combinations and do the
2020 operations.
2022 However, this involves several calls to compare_values and it
2023 is pretty convoluted. It's simpler to do the 4 operations
2024 (MIN0 OP MIN1, MIN0 OP MAX1, MAX0 OP MIN1 and MAX0 OP MAX0 OP
2025 MAX1) and then figure the smallest and largest values to form
2026 the new range. */
2027 gcc_assert (code == MULT_EXPR
2028 || code == TRUNC_DIV_EXPR
2029 || code == FLOOR_DIV_EXPR
2030 || code == CEIL_DIV_EXPR
2031 || code == EXACT_DIV_EXPR
2032 || code == ROUND_DIV_EXPR
2033 || code == RSHIFT_EXPR
2034 || code == LSHIFT_EXPR);
2035 gcc_assert ((vr0->type == VR_RANGE
2036 || (code == MULT_EXPR && vr0->type == VR_ANTI_RANGE))
2037 && vr0->type == vr1->type);
2039 type = vr0->type;
2041 /* Compute the 4 cross operations. */
2042 sop = false;
2043 val[0] = vrp_int_const_binop (code, vr0->min, vr1->min);
2044 if (val[0] == NULL_TREE)
2045 sop = true;
2047 if (vr1->max == vr1->min)
2048 val[1] = NULL_TREE;
2049 else
2051 val[1] = vrp_int_const_binop (code, vr0->min, vr1->max);
2052 if (val[1] == NULL_TREE)
2053 sop = true;
2056 if (vr0->max == vr0->min)
2057 val[2] = NULL_TREE;
2058 else
2060 val[2] = vrp_int_const_binop (code, vr0->max, vr1->min);
2061 if (val[2] == NULL_TREE)
2062 sop = true;
2065 if (vr0->min == vr0->max || vr1->min == vr1->max)
2066 val[3] = NULL_TREE;
2067 else
2069 val[3] = vrp_int_const_binop (code, vr0->max, vr1->max);
2070 if (val[3] == NULL_TREE)
2071 sop = true;
2074 if (sop)
2076 set_value_range_to_varying (vr);
2077 return;
2080 /* Set MIN to the minimum of VAL[i] and MAX to the maximum
2081 of VAL[i]. */
2082 min = val[0];
2083 max = val[0];
2084 for (i = 1; i < 4; i++)
2086 if (!is_gimple_min_invariant (min)
2087 || (TREE_OVERFLOW (min) && !is_overflow_infinity (min))
2088 || !is_gimple_min_invariant (max)
2089 || (TREE_OVERFLOW (max) && !is_overflow_infinity (max)))
2090 break;
2092 if (val[i])
2094 if (!is_gimple_min_invariant (val[i])
2095 || (TREE_OVERFLOW (val[i])
2096 && !is_overflow_infinity (val[i])))
2098 /* If we found an overflowed value, set MIN and MAX
2099 to it so that we set the resulting range to
2100 VARYING. */
2101 min = max = val[i];
2102 break;
2105 if (compare_values (val[i], min) == -1)
2106 min = val[i];
2108 if (compare_values (val[i], max) == 1)
2109 max = val[i];
2113 /* If either MIN or MAX overflowed, then set the resulting range to
2114 VARYING. But we do accept an overflow infinity
2115 representation. */
2116 if (min == NULL_TREE
2117 || !is_gimple_min_invariant (min)
2118 || (TREE_OVERFLOW (min) && !is_overflow_infinity (min))
2119 || max == NULL_TREE
2120 || !is_gimple_min_invariant (max)
2121 || (TREE_OVERFLOW (max) && !is_overflow_infinity (max)))
2123 set_value_range_to_varying (vr);
2124 return;
2127 /* We punt if:
2128 1) [-INF, +INF]
2129 2) [-INF, +-INF(OVF)]
2130 3) [+-INF(OVF), +INF]
2131 4) [+-INF(OVF), +-INF(OVF)]
2132 We learn nothing when we have INF and INF(OVF) on both sides.
2133 Note that we do accept [-INF, -INF] and [+INF, +INF] without
2134 overflow. */
2135 if ((vrp_val_is_min (min) || is_overflow_infinity (min))
2136 && (vrp_val_is_max (max) || is_overflow_infinity (max)))
2138 set_value_range_to_varying (vr);
2139 return;
2142 cmp = compare_values (min, max);
2143 if (cmp == -2 || cmp == 1)
2145 /* If the new range has its limits swapped around (MIN > MAX),
2146 then the operation caused one of them to wrap around, mark
2147 the new range VARYING. */
2148 set_value_range_to_varying (vr);
2150 else
2151 set_value_range (vr, type, min, max, NULL);
2154 /* Extract range information from a binary operation CODE based on
2155 the ranges of each of its operands *VR0 and *VR1 with resulting
2156 type EXPR_TYPE. The resulting range is stored in *VR. */
2158 static void
2159 extract_range_from_binary_expr_1 (value_range *vr,
2160 enum tree_code code, tree expr_type,
2161 value_range *vr0_, value_range *vr1_)
2163 value_range vr0 = *vr0_, vr1 = *vr1_;
2164 value_range vrtem0 = VR_INITIALIZER, vrtem1 = VR_INITIALIZER;
2165 enum value_range_type type;
2166 tree min = NULL_TREE, max = NULL_TREE;
2167 int cmp;
2169 if (!INTEGRAL_TYPE_P (expr_type)
2170 && !POINTER_TYPE_P (expr_type))
2172 set_value_range_to_varying (vr);
2173 return;
2176 /* Not all binary expressions can be applied to ranges in a
2177 meaningful way. Handle only arithmetic operations. */
2178 if (code != PLUS_EXPR
2179 && code != MINUS_EXPR
2180 && code != POINTER_PLUS_EXPR
2181 && code != MULT_EXPR
2182 && code != TRUNC_DIV_EXPR
2183 && code != FLOOR_DIV_EXPR
2184 && code != CEIL_DIV_EXPR
2185 && code != EXACT_DIV_EXPR
2186 && code != ROUND_DIV_EXPR
2187 && code != TRUNC_MOD_EXPR
2188 && code != RSHIFT_EXPR
2189 && code != LSHIFT_EXPR
2190 && code != MIN_EXPR
2191 && code != MAX_EXPR
2192 && code != BIT_AND_EXPR
2193 && code != BIT_IOR_EXPR
2194 && code != BIT_XOR_EXPR)
2196 set_value_range_to_varying (vr);
2197 return;
2200 /* If both ranges are UNDEFINED, so is the result. */
2201 if (vr0.type == VR_UNDEFINED && vr1.type == VR_UNDEFINED)
2203 set_value_range_to_undefined (vr);
2204 return;
2206 /* If one of the ranges is UNDEFINED drop it to VARYING for the following
2207 code. At some point we may want to special-case operations that
2208 have UNDEFINED result for all or some value-ranges of the not UNDEFINED
2209 operand. */
2210 else if (vr0.type == VR_UNDEFINED)
2211 set_value_range_to_varying (&vr0);
2212 else if (vr1.type == VR_UNDEFINED)
2213 set_value_range_to_varying (&vr1);
2215 /* Now canonicalize anti-ranges to ranges when they are not symbolic
2216 and express ~[] op X as ([]' op X) U ([]'' op X). */
2217 if (vr0.type == VR_ANTI_RANGE
2218 && ranges_from_anti_range (&vr0, &vrtem0, &vrtem1))
2220 extract_range_from_binary_expr_1 (vr, code, expr_type, &vrtem0, vr1_);
2221 if (vrtem1.type != VR_UNDEFINED)
2223 value_range vrres = VR_INITIALIZER;
2224 extract_range_from_binary_expr_1 (&vrres, code, expr_type,
2225 &vrtem1, vr1_);
2226 vrp_meet (vr, &vrres);
2228 return;
2230 /* Likewise for X op ~[]. */
2231 if (vr1.type == VR_ANTI_RANGE
2232 && ranges_from_anti_range (&vr1, &vrtem0, &vrtem1))
2234 extract_range_from_binary_expr_1 (vr, code, expr_type, vr0_, &vrtem0);
2235 if (vrtem1.type != VR_UNDEFINED)
2237 value_range vrres = VR_INITIALIZER;
2238 extract_range_from_binary_expr_1 (&vrres, code, expr_type,
2239 vr0_, &vrtem1);
2240 vrp_meet (vr, &vrres);
2242 return;
2245 /* The type of the resulting value range defaults to VR0.TYPE. */
2246 type = vr0.type;
2248 /* Refuse to operate on VARYING ranges, ranges of different kinds
2249 and symbolic ranges. As an exception, we allow BIT_{AND,IOR}
2250 because we may be able to derive a useful range even if one of
2251 the operands is VR_VARYING or symbolic range. Similarly for
2252 divisions, MIN/MAX and PLUS/MINUS.
2254 TODO, we may be able to derive anti-ranges in some cases. */
2255 if (code != BIT_AND_EXPR
2256 && code != BIT_IOR_EXPR
2257 && code != TRUNC_DIV_EXPR
2258 && code != FLOOR_DIV_EXPR
2259 && code != CEIL_DIV_EXPR
2260 && code != EXACT_DIV_EXPR
2261 && code != ROUND_DIV_EXPR
2262 && code != TRUNC_MOD_EXPR
2263 && code != MIN_EXPR
2264 && code != MAX_EXPR
2265 && code != PLUS_EXPR
2266 && code != MINUS_EXPR
2267 && code != RSHIFT_EXPR
2268 && (vr0.type == VR_VARYING
2269 || vr1.type == VR_VARYING
2270 || vr0.type != vr1.type
2271 || symbolic_range_p (&vr0)
2272 || symbolic_range_p (&vr1)))
2274 set_value_range_to_varying (vr);
2275 return;
2278 /* Now evaluate the expression to determine the new range. */
2279 if (POINTER_TYPE_P (expr_type))
2281 if (code == MIN_EXPR || code == MAX_EXPR)
2283 /* For MIN/MAX expressions with pointers, we only care about
2284 nullness, if both are non null, then the result is nonnull.
2285 If both are null, then the result is null. Otherwise they
2286 are varying. */
2287 if (range_is_nonnull (&vr0) && range_is_nonnull (&vr1))
2288 set_value_range_to_nonnull (vr, expr_type);
2289 else if (range_is_null (&vr0) && range_is_null (&vr1))
2290 set_value_range_to_null (vr, expr_type);
2291 else
2292 set_value_range_to_varying (vr);
2294 else if (code == POINTER_PLUS_EXPR)
2296 /* For pointer types, we are really only interested in asserting
2297 whether the expression evaluates to non-NULL. */
2298 if (range_is_nonnull (&vr0) || range_is_nonnull (&vr1))
2299 set_value_range_to_nonnull (vr, expr_type);
2300 else if (range_is_null (&vr0) && range_is_null (&vr1))
2301 set_value_range_to_null (vr, expr_type);
2302 else
2303 set_value_range_to_varying (vr);
2305 else if (code == BIT_AND_EXPR)
2307 /* For pointer types, we are really only interested in asserting
2308 whether the expression evaluates to non-NULL. */
2309 if (range_is_nonnull (&vr0) && range_is_nonnull (&vr1))
2310 set_value_range_to_nonnull (vr, expr_type);
2311 else if (range_is_null (&vr0) || range_is_null (&vr1))
2312 set_value_range_to_null (vr, expr_type);
2313 else
2314 set_value_range_to_varying (vr);
2316 else
2317 set_value_range_to_varying (vr);
2319 return;
2322 /* For integer ranges, apply the operation to each end of the
2323 range and see what we end up with. */
2324 if (code == PLUS_EXPR || code == MINUS_EXPR)
2326 const bool minus_p = (code == MINUS_EXPR);
2327 tree min_op0 = vr0.min;
2328 tree min_op1 = minus_p ? vr1.max : vr1.min;
2329 tree max_op0 = vr0.max;
2330 tree max_op1 = minus_p ? vr1.min : vr1.max;
2331 tree sym_min_op0 = NULL_TREE;
2332 tree sym_min_op1 = NULL_TREE;
2333 tree sym_max_op0 = NULL_TREE;
2334 tree sym_max_op1 = NULL_TREE;
2335 bool neg_min_op0, neg_min_op1, neg_max_op0, neg_max_op1;
2337 /* If we have a PLUS or MINUS with two VR_RANGEs, either constant or
2338 single-symbolic ranges, try to compute the precise resulting range,
2339 but only if we know that this resulting range will also be constant
2340 or single-symbolic. */
2341 if (vr0.type == VR_RANGE && vr1.type == VR_RANGE
2342 && (TREE_CODE (min_op0) == INTEGER_CST
2343 || (sym_min_op0
2344 = get_single_symbol (min_op0, &neg_min_op0, &min_op0)))
2345 && (TREE_CODE (min_op1) == INTEGER_CST
2346 || (sym_min_op1
2347 = get_single_symbol (min_op1, &neg_min_op1, &min_op1)))
2348 && (!(sym_min_op0 && sym_min_op1)
2349 || (sym_min_op0 == sym_min_op1
2350 && neg_min_op0 == (minus_p ? neg_min_op1 : !neg_min_op1)))
2351 && (TREE_CODE (max_op0) == INTEGER_CST
2352 || (sym_max_op0
2353 = get_single_symbol (max_op0, &neg_max_op0, &max_op0)))
2354 && (TREE_CODE (max_op1) == INTEGER_CST
2355 || (sym_max_op1
2356 = get_single_symbol (max_op1, &neg_max_op1, &max_op1)))
2357 && (!(sym_max_op0 && sym_max_op1)
2358 || (sym_max_op0 == sym_max_op1
2359 && neg_max_op0 == (minus_p ? neg_max_op1 : !neg_max_op1))))
2361 const signop sgn = TYPE_SIGN (expr_type);
2362 const unsigned int prec = TYPE_PRECISION (expr_type);
2363 wide_int type_min, type_max, wmin, wmax;
2364 int min_ovf = 0;
2365 int max_ovf = 0;
2367 /* Get the lower and upper bounds of the type. */
2368 if (TYPE_OVERFLOW_WRAPS (expr_type))
2370 type_min = wi::min_value (prec, sgn);
2371 type_max = wi::max_value (prec, sgn);
2373 else
2375 type_min = vrp_val_min (expr_type);
2376 type_max = vrp_val_max (expr_type);
2379 /* Combine the lower bounds, if any. */
2380 if (min_op0 && min_op1)
2382 if (minus_p)
2384 wmin = wi::sub (min_op0, min_op1);
2386 /* Check for overflow. */
2387 if (wi::cmp (0, min_op1, sgn)
2388 != wi::cmp (wmin, min_op0, sgn))
2389 min_ovf = wi::cmp (min_op0, min_op1, sgn);
2391 else
2393 wmin = wi::add (min_op0, min_op1);
2395 /* Check for overflow. */
2396 if (wi::cmp (min_op1, 0, sgn)
2397 != wi::cmp (wmin, min_op0, sgn))
2398 min_ovf = wi::cmp (min_op0, wmin, sgn);
2401 else if (min_op0)
2402 wmin = min_op0;
2403 else if (min_op1)
2404 wmin = minus_p ? wi::neg (min_op1) : min_op1;
2405 else
2406 wmin = wi::shwi (0, prec);
2408 /* Combine the upper bounds, if any. */
2409 if (max_op0 && max_op1)
2411 if (minus_p)
2413 wmax = wi::sub (max_op0, max_op1);
2415 /* Check for overflow. */
2416 if (wi::cmp (0, max_op1, sgn)
2417 != wi::cmp (wmax, max_op0, sgn))
2418 max_ovf = wi::cmp (max_op0, max_op1, sgn);
2420 else
2422 wmax = wi::add (max_op0, max_op1);
2424 if (wi::cmp (max_op1, 0, sgn)
2425 != wi::cmp (wmax, max_op0, sgn))
2426 max_ovf = wi::cmp (max_op0, wmax, sgn);
2429 else if (max_op0)
2430 wmax = max_op0;
2431 else if (max_op1)
2432 wmax = minus_p ? wi::neg (max_op1) : max_op1;
2433 else
2434 wmax = wi::shwi (0, prec);
2436 /* Check for type overflow. */
2437 if (min_ovf == 0)
2439 if (wi::cmp (wmin, type_min, sgn) == -1)
2440 min_ovf = -1;
2441 else if (wi::cmp (wmin, type_max, sgn) == 1)
2442 min_ovf = 1;
2444 if (max_ovf == 0)
2446 if (wi::cmp (wmax, type_min, sgn) == -1)
2447 max_ovf = -1;
2448 else if (wi::cmp (wmax, type_max, sgn) == 1)
2449 max_ovf = 1;
2452 /* If we have overflow for the constant part and the resulting
2453 range will be symbolic, drop to VR_VARYING. */
2454 if ((min_ovf && sym_min_op0 != sym_min_op1)
2455 || (max_ovf && sym_max_op0 != sym_max_op1))
2457 set_value_range_to_varying (vr);
2458 return;
2461 if (TYPE_OVERFLOW_WRAPS (expr_type))
2463 /* If overflow wraps, truncate the values and adjust the
2464 range kind and bounds appropriately. */
2465 wide_int tmin = wide_int::from (wmin, prec, sgn);
2466 wide_int tmax = wide_int::from (wmax, prec, sgn);
2467 if (min_ovf == max_ovf)
2469 /* No overflow or both overflow or underflow. The
2470 range kind stays VR_RANGE. */
2471 min = wide_int_to_tree (expr_type, tmin);
2472 max = wide_int_to_tree (expr_type, tmax);
2474 else if ((min_ovf == -1 && max_ovf == 0)
2475 || (max_ovf == 1 && min_ovf == 0))
2477 /* Min underflow or max overflow. The range kind
2478 changes to VR_ANTI_RANGE. */
2479 bool covers = false;
2480 wide_int tem = tmin;
2481 type = VR_ANTI_RANGE;
2482 tmin = tmax + 1;
2483 if (wi::cmp (tmin, tmax, sgn) < 0)
2484 covers = true;
2485 tmax = tem - 1;
2486 if (wi::cmp (tmax, tem, sgn) > 0)
2487 covers = true;
2488 /* If the anti-range would cover nothing, drop to varying.
2489 Likewise if the anti-range bounds are outside of the
2490 types values. */
2491 if (covers || wi::cmp (tmin, tmax, sgn) > 0)
2493 set_value_range_to_varying (vr);
2494 return;
2496 min = wide_int_to_tree (expr_type, tmin);
2497 max = wide_int_to_tree (expr_type, tmax);
2499 else
2501 /* Other underflow and/or overflow, drop to VR_VARYING. */
2502 set_value_range_to_varying (vr);
2503 return;
2506 else
2508 /* If overflow does not wrap, saturate to the types min/max
2509 value. */
2510 if (min_ovf == -1)
2512 if (needs_overflow_infinity (expr_type)
2513 && supports_overflow_infinity (expr_type))
2514 min = negative_overflow_infinity (expr_type);
2515 else
2516 min = wide_int_to_tree (expr_type, type_min);
2518 else if (min_ovf == 1)
2520 if (needs_overflow_infinity (expr_type)
2521 && supports_overflow_infinity (expr_type))
2522 min = positive_overflow_infinity (expr_type);
2523 else
2524 min = wide_int_to_tree (expr_type, type_max);
2526 else
2527 min = wide_int_to_tree (expr_type, wmin);
2529 if (max_ovf == -1)
2531 if (needs_overflow_infinity (expr_type)
2532 && supports_overflow_infinity (expr_type))
2533 max = negative_overflow_infinity (expr_type);
2534 else
2535 max = wide_int_to_tree (expr_type, type_min);
2537 else if (max_ovf == 1)
2539 if (needs_overflow_infinity (expr_type)
2540 && supports_overflow_infinity (expr_type))
2541 max = positive_overflow_infinity (expr_type);
2542 else
2543 max = wide_int_to_tree (expr_type, type_max);
2545 else
2546 max = wide_int_to_tree (expr_type, wmax);
2549 if (needs_overflow_infinity (expr_type)
2550 && supports_overflow_infinity (expr_type))
2552 if ((min_op0 && is_negative_overflow_infinity (min_op0))
2553 || (min_op1
2554 && (minus_p
2555 ? is_positive_overflow_infinity (min_op1)
2556 : is_negative_overflow_infinity (min_op1))))
2557 min = negative_overflow_infinity (expr_type);
2558 if ((max_op0 && is_positive_overflow_infinity (max_op0))
2559 || (max_op1
2560 && (minus_p
2561 ? is_negative_overflow_infinity (max_op1)
2562 : is_positive_overflow_infinity (max_op1))))
2563 max = positive_overflow_infinity (expr_type);
2566 /* If the result lower bound is constant, we're done;
2567 otherwise, build the symbolic lower bound. */
2568 if (sym_min_op0 == sym_min_op1)
2570 else if (sym_min_op0)
2571 min = build_symbolic_expr (expr_type, sym_min_op0,
2572 neg_min_op0, min);
2573 else if (sym_min_op1)
2574 min = build_symbolic_expr (expr_type, sym_min_op1,
2575 neg_min_op1 ^ minus_p, min);
2577 /* Likewise for the upper bound. */
2578 if (sym_max_op0 == sym_max_op1)
2580 else if (sym_max_op0)
2581 max = build_symbolic_expr (expr_type, sym_max_op0,
2582 neg_max_op0, max);
2583 else if (sym_max_op1)
2584 max = build_symbolic_expr (expr_type, sym_max_op1,
2585 neg_max_op1 ^ minus_p, max);
2587 else
2589 /* For other cases, for example if we have a PLUS_EXPR with two
2590 VR_ANTI_RANGEs, drop to VR_VARYING. It would take more effort
2591 to compute a precise range for such a case.
2592 ??? General even mixed range kind operations can be expressed
2593 by for example transforming ~[3, 5] + [1, 2] to range-only
2594 operations and a union primitive:
2595 [-INF, 2] + [1, 2] U [5, +INF] + [1, 2]
2596 [-INF+1, 4] U [6, +INF(OVF)]
2597 though usually the union is not exactly representable with
2598 a single range or anti-range as the above is
2599 [-INF+1, +INF(OVF)] intersected with ~[5, 5]
2600 but one could use a scheme similar to equivalences for this. */
2601 set_value_range_to_varying (vr);
2602 return;
2605 else if (code == MIN_EXPR
2606 || code == MAX_EXPR)
2608 if (vr0.type == VR_RANGE
2609 && !symbolic_range_p (&vr0))
2611 type = VR_RANGE;
2612 if (vr1.type == VR_RANGE
2613 && !symbolic_range_p (&vr1))
2615 /* For operations that make the resulting range directly
2616 proportional to the original ranges, apply the operation to
2617 the same end of each range. */
2618 min = vrp_int_const_binop (code, vr0.min, vr1.min);
2619 max = vrp_int_const_binop (code, vr0.max, vr1.max);
2621 else if (code == MIN_EXPR)
2623 min = vrp_val_min (expr_type);
2624 max = vr0.max;
2626 else if (code == MAX_EXPR)
2628 min = vr0.min;
2629 max = vrp_val_max (expr_type);
2632 else if (vr1.type == VR_RANGE
2633 && !symbolic_range_p (&vr1))
2635 type = VR_RANGE;
2636 if (code == MIN_EXPR)
2638 min = vrp_val_min (expr_type);
2639 max = vr1.max;
2641 else if (code == MAX_EXPR)
2643 min = vr1.min;
2644 max = vrp_val_max (expr_type);
2647 else
2649 set_value_range_to_varying (vr);
2650 return;
2653 else if (code == MULT_EXPR)
2655 /* Fancy code so that with unsigned, [-3,-1]*[-3,-1] does not
2656 drop to varying. This test requires 2*prec bits if both
2657 operands are signed and 2*prec + 2 bits if either is not. */
2659 signop sign = TYPE_SIGN (expr_type);
2660 unsigned int prec = TYPE_PRECISION (expr_type);
2662 if (range_int_cst_p (&vr0)
2663 && range_int_cst_p (&vr1)
2664 && TYPE_OVERFLOW_WRAPS (expr_type))
2666 typedef FIXED_WIDE_INT (WIDE_INT_MAX_PRECISION * 2) vrp_int;
2667 typedef generic_wide_int
2668 <wi::extended_tree <WIDE_INT_MAX_PRECISION * 2> > vrp_int_cst;
2669 vrp_int sizem1 = wi::mask <vrp_int> (prec, false);
2670 vrp_int size = sizem1 + 1;
2672 /* Extend the values using the sign of the result to PREC2.
2673 From here on out, everthing is just signed math no matter
2674 what the input types were. */
2675 vrp_int min0 = vrp_int_cst (vr0.min);
2676 vrp_int max0 = vrp_int_cst (vr0.max);
2677 vrp_int min1 = vrp_int_cst (vr1.min);
2678 vrp_int max1 = vrp_int_cst (vr1.max);
2679 /* Canonicalize the intervals. */
2680 if (sign == UNSIGNED)
2682 if (wi::ltu_p (size, min0 + max0))
2684 min0 -= size;
2685 max0 -= size;
2688 if (wi::ltu_p (size, min1 + max1))
2690 min1 -= size;
2691 max1 -= size;
2695 vrp_int prod0 = min0 * min1;
2696 vrp_int prod1 = min0 * max1;
2697 vrp_int prod2 = max0 * min1;
2698 vrp_int prod3 = max0 * max1;
2700 /* Sort the 4 products so that min is in prod0 and max is in
2701 prod3. */
2702 /* min0min1 > max0max1 */
2703 if (prod0 > prod3)
2704 std::swap (prod0, prod3);
2706 /* min0max1 > max0min1 */
2707 if (prod1 > prod2)
2708 std::swap (prod1, prod2);
2710 if (prod0 > prod1)
2711 std::swap (prod0, prod1);
2713 if (prod2 > prod3)
2714 std::swap (prod2, prod3);
2716 /* diff = max - min. */
2717 prod2 = prod3 - prod0;
2718 if (wi::geu_p (prod2, sizem1))
2720 /* the range covers all values. */
2721 set_value_range_to_varying (vr);
2722 return;
2725 /* The following should handle the wrapping and selecting
2726 VR_ANTI_RANGE for us. */
2727 min = wide_int_to_tree (expr_type, prod0);
2728 max = wide_int_to_tree (expr_type, prod3);
2729 set_and_canonicalize_value_range (vr, VR_RANGE, min, max, NULL);
2730 return;
2733 /* If we have an unsigned MULT_EXPR with two VR_ANTI_RANGEs,
2734 drop to VR_VARYING. It would take more effort to compute a
2735 precise range for such a case. For example, if we have
2736 op0 == 65536 and op1 == 65536 with their ranges both being
2737 ~[0,0] on a 32-bit machine, we would have op0 * op1 == 0, so
2738 we cannot claim that the product is in ~[0,0]. Note that we
2739 are guaranteed to have vr0.type == vr1.type at this
2740 point. */
2741 if (vr0.type == VR_ANTI_RANGE
2742 && !TYPE_OVERFLOW_UNDEFINED (expr_type))
2744 set_value_range_to_varying (vr);
2745 return;
2748 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
2749 return;
2751 else if (code == RSHIFT_EXPR
2752 || code == LSHIFT_EXPR)
2754 /* If we have a RSHIFT_EXPR with any shift values outside [0..prec-1],
2755 then drop to VR_VARYING. Outside of this range we get undefined
2756 behavior from the shift operation. We cannot even trust
2757 SHIFT_COUNT_TRUNCATED at this stage, because that applies to rtl
2758 shifts, and the operation at the tree level may be widened. */
2759 if (range_int_cst_p (&vr1)
2760 && compare_tree_int (vr1.min, 0) >= 0
2761 && compare_tree_int (vr1.max, TYPE_PRECISION (expr_type)) == -1)
2763 if (code == RSHIFT_EXPR)
2765 /* Even if vr0 is VARYING or otherwise not usable, we can derive
2766 useful ranges just from the shift count. E.g.
2767 x >> 63 for signed 64-bit x is always [-1, 0]. */
2768 if (vr0.type != VR_RANGE || symbolic_range_p (&vr0))
2770 vr0.type = type = VR_RANGE;
2771 vr0.min = vrp_val_min (expr_type);
2772 vr0.max = vrp_val_max (expr_type);
2774 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
2775 return;
2777 /* We can map lshifts by constants to MULT_EXPR handling. */
2778 else if (code == LSHIFT_EXPR
2779 && range_int_cst_singleton_p (&vr1))
2781 bool saved_flag_wrapv;
2782 value_range vr1p = VR_INITIALIZER;
2783 vr1p.type = VR_RANGE;
2784 vr1p.min = (wide_int_to_tree
2785 (expr_type,
2786 wi::set_bit_in_zero (tree_to_shwi (vr1.min),
2787 TYPE_PRECISION (expr_type))));
2788 vr1p.max = vr1p.min;
2789 /* We have to use a wrapping multiply though as signed overflow
2790 on lshifts is implementation defined in C89. */
2791 saved_flag_wrapv = flag_wrapv;
2792 flag_wrapv = 1;
2793 extract_range_from_binary_expr_1 (vr, MULT_EXPR, expr_type,
2794 &vr0, &vr1p);
2795 flag_wrapv = saved_flag_wrapv;
2796 return;
2798 else if (code == LSHIFT_EXPR
2799 && range_int_cst_p (&vr0))
2801 int prec = TYPE_PRECISION (expr_type);
2802 int overflow_pos = prec;
2803 int bound_shift;
2804 wide_int low_bound, high_bound;
2805 bool uns = TYPE_UNSIGNED (expr_type);
2806 bool in_bounds = false;
2808 if (!uns)
2809 overflow_pos -= 1;
2811 bound_shift = overflow_pos - tree_to_shwi (vr1.max);
2812 /* If bound_shift == HOST_BITS_PER_WIDE_INT, the llshift can
2813 overflow. However, for that to happen, vr1.max needs to be
2814 zero, which means vr1 is a singleton range of zero, which
2815 means it should be handled by the previous LSHIFT_EXPR
2816 if-clause. */
2817 wide_int bound = wi::set_bit_in_zero (bound_shift, prec);
2818 wide_int complement = ~(bound - 1);
2820 if (uns)
2822 low_bound = bound;
2823 high_bound = complement;
2824 if (wi::ltu_p (vr0.max, low_bound))
2826 /* [5, 6] << [1, 2] == [10, 24]. */
2827 /* We're shifting out only zeroes, the value increases
2828 monotonically. */
2829 in_bounds = true;
2831 else if (wi::ltu_p (high_bound, vr0.min))
2833 /* [0xffffff00, 0xffffffff] << [1, 2]
2834 == [0xfffffc00, 0xfffffffe]. */
2835 /* We're shifting out only ones, the value decreases
2836 monotonically. */
2837 in_bounds = true;
2840 else
2842 /* [-1, 1] << [1, 2] == [-4, 4]. */
2843 low_bound = complement;
2844 high_bound = bound;
2845 if (wi::lts_p (vr0.max, high_bound)
2846 && wi::lts_p (low_bound, vr0.min))
2848 /* For non-negative numbers, we're shifting out only
2849 zeroes, the value increases monotonically.
2850 For negative numbers, we're shifting out only ones, the
2851 value decreases monotomically. */
2852 in_bounds = true;
2856 if (in_bounds)
2858 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
2859 return;
2863 set_value_range_to_varying (vr);
2864 return;
2866 else if (code == TRUNC_DIV_EXPR
2867 || code == FLOOR_DIV_EXPR
2868 || code == CEIL_DIV_EXPR
2869 || code == EXACT_DIV_EXPR
2870 || code == ROUND_DIV_EXPR)
2872 if (vr0.type != VR_RANGE || symbolic_range_p (&vr0))
2874 /* For division, if op1 has VR_RANGE but op0 does not, something
2875 can be deduced just from that range. Say [min, max] / [4, max]
2876 gives [min / 4, max / 4] range. */
2877 if (vr1.type == VR_RANGE
2878 && !symbolic_range_p (&vr1)
2879 && range_includes_zero_p (vr1.min, vr1.max) == 0)
2881 vr0.type = type = VR_RANGE;
2882 vr0.min = vrp_val_min (expr_type);
2883 vr0.max = vrp_val_max (expr_type);
2885 else
2887 set_value_range_to_varying (vr);
2888 return;
2892 /* For divisions, if flag_non_call_exceptions is true, we must
2893 not eliminate a division by zero. */
2894 if (cfun->can_throw_non_call_exceptions
2895 && (vr1.type != VR_RANGE
2896 || range_includes_zero_p (vr1.min, vr1.max) != 0))
2898 set_value_range_to_varying (vr);
2899 return;
2902 /* For divisions, if op0 is VR_RANGE, we can deduce a range
2903 even if op1 is VR_VARYING, VR_ANTI_RANGE, symbolic or can
2904 include 0. */
2905 if (vr0.type == VR_RANGE
2906 && (vr1.type != VR_RANGE
2907 || range_includes_zero_p (vr1.min, vr1.max) != 0))
2909 tree zero = build_int_cst (TREE_TYPE (vr0.min), 0);
2910 int cmp;
2912 min = NULL_TREE;
2913 max = NULL_TREE;
2914 if (TYPE_UNSIGNED (expr_type)
2915 || value_range_nonnegative_p (&vr1))
2917 /* For unsigned division or when divisor is known
2918 to be non-negative, the range has to cover
2919 all numbers from 0 to max for positive max
2920 and all numbers from min to 0 for negative min. */
2921 cmp = compare_values (vr0.max, zero);
2922 if (cmp == -1)
2924 /* When vr0.max < 0, vr1.min != 0 and value
2925 ranges for dividend and divisor are available. */
2926 if (vr1.type == VR_RANGE
2927 && !symbolic_range_p (&vr0)
2928 && !symbolic_range_p (&vr1)
2929 && compare_values (vr1.min, zero) != 0)
2930 max = int_const_binop (code, vr0.max, vr1.min);
2931 else
2932 max = zero;
2934 else if (cmp == 0 || cmp == 1)
2935 max = vr0.max;
2936 else
2937 type = VR_VARYING;
2938 cmp = compare_values (vr0.min, zero);
2939 if (cmp == 1)
2941 /* For unsigned division when value ranges for dividend
2942 and divisor are available. */
2943 if (vr1.type == VR_RANGE
2944 && !symbolic_range_p (&vr0)
2945 && !symbolic_range_p (&vr1)
2946 && compare_values (vr1.max, zero) != 0)
2947 min = int_const_binop (code, vr0.min, vr1.max);
2948 else
2949 min = zero;
2951 else if (cmp == 0 || cmp == -1)
2952 min = vr0.min;
2953 else
2954 type = VR_VARYING;
2956 else
2958 /* Otherwise the range is -max .. max or min .. -min
2959 depending on which bound is bigger in absolute value,
2960 as the division can change the sign. */
2961 abs_extent_range (vr, vr0.min, vr0.max);
2962 return;
2964 if (type == VR_VARYING)
2966 set_value_range_to_varying (vr);
2967 return;
2970 else if (!symbolic_range_p (&vr0) && !symbolic_range_p (&vr1))
2972 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
2973 return;
2976 else if (code == TRUNC_MOD_EXPR)
2978 if (range_is_null (&vr1))
2980 set_value_range_to_undefined (vr);
2981 return;
2983 /* ABS (A % B) < ABS (B) and either
2984 0 <= A % B <= A or A <= A % B <= 0. */
2985 type = VR_RANGE;
2986 signop sgn = TYPE_SIGN (expr_type);
2987 unsigned int prec = TYPE_PRECISION (expr_type);
2988 wide_int wmin, wmax, tmp;
2989 wide_int zero = wi::zero (prec);
2990 wide_int one = wi::one (prec);
2991 if (vr1.type == VR_RANGE && !symbolic_range_p (&vr1))
2993 wmax = wi::sub (vr1.max, one);
2994 if (sgn == SIGNED)
2996 tmp = wi::sub (wi::minus_one (prec), vr1.min);
2997 wmax = wi::smax (wmax, tmp);
3000 else
3002 wmax = wi::max_value (prec, sgn);
3003 /* X % INT_MIN may be INT_MAX. */
3004 if (sgn == UNSIGNED)
3005 wmax = wmax - one;
3008 if (sgn == UNSIGNED)
3009 wmin = zero;
3010 else
3012 wmin = -wmax;
3013 if (vr0.type == VR_RANGE && TREE_CODE (vr0.min) == INTEGER_CST)
3015 tmp = vr0.min;
3016 if (wi::gts_p (tmp, zero))
3017 tmp = zero;
3018 wmin = wi::smax (wmin, tmp);
3022 if (vr0.type == VR_RANGE && TREE_CODE (vr0.max) == INTEGER_CST)
3024 tmp = vr0.max;
3025 if (sgn == SIGNED && wi::neg_p (tmp))
3026 tmp = zero;
3027 wmax = wi::min (wmax, tmp, sgn);
3030 min = wide_int_to_tree (expr_type, wmin);
3031 max = wide_int_to_tree (expr_type, wmax);
3033 else if (code == BIT_AND_EXPR || code == BIT_IOR_EXPR || code == BIT_XOR_EXPR)
3035 bool int_cst_range0, int_cst_range1;
3036 wide_int may_be_nonzero0, may_be_nonzero1;
3037 wide_int must_be_nonzero0, must_be_nonzero1;
3039 int_cst_range0 = zero_nonzero_bits_from_vr (expr_type, &vr0,
3040 &may_be_nonzero0,
3041 &must_be_nonzero0);
3042 int_cst_range1 = zero_nonzero_bits_from_vr (expr_type, &vr1,
3043 &may_be_nonzero1,
3044 &must_be_nonzero1);
3046 type = VR_RANGE;
3047 if (code == BIT_AND_EXPR)
3049 min = wide_int_to_tree (expr_type,
3050 must_be_nonzero0 & must_be_nonzero1);
3051 wide_int wmax = may_be_nonzero0 & may_be_nonzero1;
3052 /* If both input ranges contain only negative values we can
3053 truncate the result range maximum to the minimum of the
3054 input range maxima. */
3055 if (int_cst_range0 && int_cst_range1
3056 && tree_int_cst_sgn (vr0.max) < 0
3057 && tree_int_cst_sgn (vr1.max) < 0)
3059 wmax = wi::min (wmax, vr0.max, TYPE_SIGN (expr_type));
3060 wmax = wi::min (wmax, vr1.max, TYPE_SIGN (expr_type));
3062 /* If either input range contains only non-negative values
3063 we can truncate the result range maximum to the respective
3064 maximum of the input range. */
3065 if (int_cst_range0 && tree_int_cst_sgn (vr0.min) >= 0)
3066 wmax = wi::min (wmax, vr0.max, TYPE_SIGN (expr_type));
3067 if (int_cst_range1 && tree_int_cst_sgn (vr1.min) >= 0)
3068 wmax = wi::min (wmax, vr1.max, TYPE_SIGN (expr_type));
3069 max = wide_int_to_tree (expr_type, wmax);
3070 cmp = compare_values (min, max);
3071 /* PR68217: In case of signed & sign-bit-CST should
3072 result in [-INF, 0] instead of [-INF, INF]. */
3073 if (cmp == -2 || cmp == 1)
3075 wide_int sign_bit
3076 = wi::set_bit_in_zero (TYPE_PRECISION (expr_type) - 1,
3077 TYPE_PRECISION (expr_type));
3078 if (!TYPE_UNSIGNED (expr_type)
3079 && ((value_range_constant_singleton (&vr0)
3080 && !wi::cmps (vr0.min, sign_bit))
3081 || (value_range_constant_singleton (&vr1)
3082 && !wi::cmps (vr1.min, sign_bit))))
3084 min = TYPE_MIN_VALUE (expr_type);
3085 max = build_int_cst (expr_type, 0);
3089 else if (code == BIT_IOR_EXPR)
3091 max = wide_int_to_tree (expr_type,
3092 may_be_nonzero0 | may_be_nonzero1);
3093 wide_int wmin = must_be_nonzero0 | must_be_nonzero1;
3094 /* If the input ranges contain only positive values we can
3095 truncate the minimum of the result range to the maximum
3096 of the input range minima. */
3097 if (int_cst_range0 && int_cst_range1
3098 && tree_int_cst_sgn (vr0.min) >= 0
3099 && tree_int_cst_sgn (vr1.min) >= 0)
3101 wmin = wi::max (wmin, vr0.min, TYPE_SIGN (expr_type));
3102 wmin = wi::max (wmin, vr1.min, TYPE_SIGN (expr_type));
3104 /* If either input range contains only negative values
3105 we can truncate the minimum of the result range to the
3106 respective minimum range. */
3107 if (int_cst_range0 && tree_int_cst_sgn (vr0.max) < 0)
3108 wmin = wi::max (wmin, vr0.min, TYPE_SIGN (expr_type));
3109 if (int_cst_range1 && tree_int_cst_sgn (vr1.max) < 0)
3110 wmin = wi::max (wmin, vr1.min, TYPE_SIGN (expr_type));
3111 min = wide_int_to_tree (expr_type, wmin);
3113 else if (code == BIT_XOR_EXPR)
3115 wide_int result_zero_bits = ((must_be_nonzero0 & must_be_nonzero1)
3116 | ~(may_be_nonzero0 | may_be_nonzero1));
3117 wide_int result_one_bits
3118 = (must_be_nonzero0.and_not (may_be_nonzero1)
3119 | must_be_nonzero1.and_not (may_be_nonzero0));
3120 max = wide_int_to_tree (expr_type, ~result_zero_bits);
3121 min = wide_int_to_tree (expr_type, result_one_bits);
3122 /* If the range has all positive or all negative values the
3123 result is better than VARYING. */
3124 if (tree_int_cst_sgn (min) < 0
3125 || tree_int_cst_sgn (max) >= 0)
3127 else
3128 max = min = NULL_TREE;
3131 else
3132 gcc_unreachable ();
3134 /* If either MIN or MAX overflowed, then set the resulting range to
3135 VARYING. But we do accept an overflow infinity representation. */
3136 if (min == NULL_TREE
3137 || (TREE_OVERFLOW_P (min) && !is_overflow_infinity (min))
3138 || max == NULL_TREE
3139 || (TREE_OVERFLOW_P (max) && !is_overflow_infinity (max)))
3141 set_value_range_to_varying (vr);
3142 return;
3145 /* We punt if:
3146 1) [-INF, +INF]
3147 2) [-INF, +-INF(OVF)]
3148 3) [+-INF(OVF), +INF]
3149 4) [+-INF(OVF), +-INF(OVF)]
3150 We learn nothing when we have INF and INF(OVF) on both sides.
3151 Note that we do accept [-INF, -INF] and [+INF, +INF] without
3152 overflow. */
3153 if ((vrp_val_is_min (min) || is_overflow_infinity (min))
3154 && (vrp_val_is_max (max) || is_overflow_infinity (max)))
3156 set_value_range_to_varying (vr);
3157 return;
3160 cmp = compare_values (min, max);
3161 if (cmp == -2 || cmp == 1)
3163 /* If the new range has its limits swapped around (MIN > MAX),
3164 then the operation caused one of them to wrap around, mark
3165 the new range VARYING. */
3166 set_value_range_to_varying (vr);
3168 else
3169 set_value_range (vr, type, min, max, NULL);
3172 /* Extract range information from a binary expression OP0 CODE OP1 based on
3173 the ranges of each of its operands with resulting type EXPR_TYPE.
3174 The resulting range is stored in *VR. */
3176 static void
3177 extract_range_from_binary_expr (value_range *vr,
3178 enum tree_code code,
3179 tree expr_type, tree op0, tree op1)
3181 value_range vr0 = VR_INITIALIZER;
3182 value_range vr1 = VR_INITIALIZER;
3184 /* Get value ranges for each operand. For constant operands, create
3185 a new value range with the operand to simplify processing. */
3186 if (TREE_CODE (op0) == SSA_NAME)
3187 vr0 = *(get_value_range (op0));
3188 else if (is_gimple_min_invariant (op0))
3189 set_value_range_to_value (&vr0, op0, NULL);
3190 else
3191 set_value_range_to_varying (&vr0);
3193 if (TREE_CODE (op1) == SSA_NAME)
3194 vr1 = *(get_value_range (op1));
3195 else if (is_gimple_min_invariant (op1))
3196 set_value_range_to_value (&vr1, op1, NULL);
3197 else
3198 set_value_range_to_varying (&vr1);
3200 extract_range_from_binary_expr_1 (vr, code, expr_type, &vr0, &vr1);
3202 /* Try harder for PLUS and MINUS if the range of one operand is symbolic
3203 and based on the other operand, for example if it was deduced from a
3204 symbolic comparison. When a bound of the range of the first operand
3205 is invariant, we set the corresponding bound of the new range to INF
3206 in order to avoid recursing on the range of the second operand. */
3207 if (vr->type == VR_VARYING
3208 && (code == PLUS_EXPR || code == MINUS_EXPR)
3209 && TREE_CODE (op1) == SSA_NAME
3210 && vr0.type == VR_RANGE
3211 && symbolic_range_based_on_p (&vr0, op1))
3213 const bool minus_p = (code == MINUS_EXPR);
3214 value_range n_vr1 = VR_INITIALIZER;
3216 /* Try with VR0 and [-INF, OP1]. */
3217 if (is_gimple_min_invariant (minus_p ? vr0.max : vr0.min))
3218 set_value_range (&n_vr1, VR_RANGE, vrp_val_min (expr_type), op1, NULL);
3220 /* Try with VR0 and [OP1, +INF]. */
3221 else if (is_gimple_min_invariant (minus_p ? vr0.min : vr0.max))
3222 set_value_range (&n_vr1, VR_RANGE, op1, vrp_val_max (expr_type), NULL);
3224 /* Try with VR0 and [OP1, OP1]. */
3225 else
3226 set_value_range (&n_vr1, VR_RANGE, op1, op1, NULL);
3228 extract_range_from_binary_expr_1 (vr, code, expr_type, &vr0, &n_vr1);
3231 if (vr->type == VR_VARYING
3232 && (code == PLUS_EXPR || code == MINUS_EXPR)
3233 && TREE_CODE (op0) == SSA_NAME
3234 && vr1.type == VR_RANGE
3235 && symbolic_range_based_on_p (&vr1, op0))
3237 const bool minus_p = (code == MINUS_EXPR);
3238 value_range n_vr0 = VR_INITIALIZER;
3240 /* Try with [-INF, OP0] and VR1. */
3241 if (is_gimple_min_invariant (minus_p ? vr1.max : vr1.min))
3242 set_value_range (&n_vr0, VR_RANGE, vrp_val_min (expr_type), op0, NULL);
3244 /* Try with [OP0, +INF] and VR1. */
3245 else if (is_gimple_min_invariant (minus_p ? vr1.min : vr1.max))
3246 set_value_range (&n_vr0, VR_RANGE, op0, vrp_val_max (expr_type), NULL);
3248 /* Try with [OP0, OP0] and VR1. */
3249 else
3250 set_value_range (&n_vr0, VR_RANGE, op0, op0, NULL);
3252 extract_range_from_binary_expr_1 (vr, code, expr_type, &n_vr0, &vr1);
3256 /* Extract range information from a unary operation CODE based on
3257 the range of its operand *VR0 with type OP0_TYPE with resulting type TYPE.
3258 The resulting range is stored in *VR. */
3260 static void
3261 extract_range_from_unary_expr_1 (value_range *vr,
3262 enum tree_code code, tree type,
3263 value_range *vr0_, tree op0_type)
3265 value_range vr0 = *vr0_, vrtem0 = VR_INITIALIZER, vrtem1 = VR_INITIALIZER;
3267 /* VRP only operates on integral and pointer types. */
3268 if (!(INTEGRAL_TYPE_P (op0_type)
3269 || POINTER_TYPE_P (op0_type))
3270 || !(INTEGRAL_TYPE_P (type)
3271 || POINTER_TYPE_P (type)))
3273 set_value_range_to_varying (vr);
3274 return;
3277 /* If VR0 is UNDEFINED, so is the result. */
3278 if (vr0.type == VR_UNDEFINED)
3280 set_value_range_to_undefined (vr);
3281 return;
3284 /* Handle operations that we express in terms of others. */
3285 if (code == PAREN_EXPR || code == OBJ_TYPE_REF)
3287 /* PAREN_EXPR and OBJ_TYPE_REF are simple copies. */
3288 copy_value_range (vr, &vr0);
3289 return;
3291 else if (code == NEGATE_EXPR)
3293 /* -X is simply 0 - X, so re-use existing code that also handles
3294 anti-ranges fine. */
3295 value_range zero = VR_INITIALIZER;
3296 set_value_range_to_value (&zero, build_int_cst (type, 0), NULL);
3297 extract_range_from_binary_expr_1 (vr, MINUS_EXPR, type, &zero, &vr0);
3298 return;
3300 else if (code == BIT_NOT_EXPR)
3302 /* ~X is simply -1 - X, so re-use existing code that also handles
3303 anti-ranges fine. */
3304 value_range minusone = VR_INITIALIZER;
3305 set_value_range_to_value (&minusone, build_int_cst (type, -1), NULL);
3306 extract_range_from_binary_expr_1 (vr, MINUS_EXPR,
3307 type, &minusone, &vr0);
3308 return;
3311 /* Now canonicalize anti-ranges to ranges when they are not symbolic
3312 and express op ~[] as (op []') U (op []''). */
3313 if (vr0.type == VR_ANTI_RANGE
3314 && ranges_from_anti_range (&vr0, &vrtem0, &vrtem1))
3316 extract_range_from_unary_expr_1 (vr, code, type, &vrtem0, op0_type);
3317 if (vrtem1.type != VR_UNDEFINED)
3319 value_range vrres = VR_INITIALIZER;
3320 extract_range_from_unary_expr_1 (&vrres, code, type,
3321 &vrtem1, op0_type);
3322 vrp_meet (vr, &vrres);
3324 return;
3327 if (CONVERT_EXPR_CODE_P (code))
3329 tree inner_type = op0_type;
3330 tree outer_type = type;
3332 /* If the expression evaluates to a pointer, we are only interested in
3333 determining if it evaluates to NULL [0, 0] or non-NULL (~[0, 0]). */
3334 if (POINTER_TYPE_P (type))
3336 if (range_is_nonnull (&vr0))
3337 set_value_range_to_nonnull (vr, type);
3338 else if (range_is_null (&vr0))
3339 set_value_range_to_null (vr, type);
3340 else
3341 set_value_range_to_varying (vr);
3342 return;
3345 /* If VR0 is varying and we increase the type precision, assume
3346 a full range for the following transformation. */
3347 if (vr0.type == VR_VARYING
3348 && INTEGRAL_TYPE_P (inner_type)
3349 && TYPE_PRECISION (inner_type) < TYPE_PRECISION (outer_type))
3351 vr0.type = VR_RANGE;
3352 vr0.min = TYPE_MIN_VALUE (inner_type);
3353 vr0.max = TYPE_MAX_VALUE (inner_type);
3356 /* If VR0 is a constant range or anti-range and the conversion is
3357 not truncating we can convert the min and max values and
3358 canonicalize the resulting range. Otherwise we can do the
3359 conversion if the size of the range is less than what the
3360 precision of the target type can represent and the range is
3361 not an anti-range. */
3362 if ((vr0.type == VR_RANGE
3363 || vr0.type == VR_ANTI_RANGE)
3364 && TREE_CODE (vr0.min) == INTEGER_CST
3365 && TREE_CODE (vr0.max) == INTEGER_CST
3366 && (!is_overflow_infinity (vr0.min)
3367 || (vr0.type == VR_RANGE
3368 && TYPE_PRECISION (outer_type) > TYPE_PRECISION (inner_type)
3369 && needs_overflow_infinity (outer_type)
3370 && supports_overflow_infinity (outer_type)))
3371 && (!is_overflow_infinity (vr0.max)
3372 || (vr0.type == VR_RANGE
3373 && TYPE_PRECISION (outer_type) > TYPE_PRECISION (inner_type)
3374 && needs_overflow_infinity (outer_type)
3375 && supports_overflow_infinity (outer_type)))
3376 && (TYPE_PRECISION (outer_type) >= TYPE_PRECISION (inner_type)
3377 || (vr0.type == VR_RANGE
3378 && integer_zerop (int_const_binop (RSHIFT_EXPR,
3379 int_const_binop (MINUS_EXPR, vr0.max, vr0.min),
3380 size_int (TYPE_PRECISION (outer_type)))))))
3382 tree new_min, new_max;
3383 if (is_overflow_infinity (vr0.min))
3384 new_min = negative_overflow_infinity (outer_type);
3385 else
3386 new_min = force_fit_type (outer_type, wi::to_widest (vr0.min),
3387 0, false);
3388 if (is_overflow_infinity (vr0.max))
3389 new_max = positive_overflow_infinity (outer_type);
3390 else
3391 new_max = force_fit_type (outer_type, wi::to_widest (vr0.max),
3392 0, false);
3393 set_and_canonicalize_value_range (vr, vr0.type,
3394 new_min, new_max, NULL);
3395 return;
3398 set_value_range_to_varying (vr);
3399 return;
3401 else if (code == ABS_EXPR)
3403 tree min, max;
3404 int cmp;
3406 /* Pass through vr0 in the easy cases. */
3407 if (TYPE_UNSIGNED (type)
3408 || value_range_nonnegative_p (&vr0))
3410 copy_value_range (vr, &vr0);
3411 return;
3414 /* For the remaining varying or symbolic ranges we can't do anything
3415 useful. */
3416 if (vr0.type == VR_VARYING
3417 || symbolic_range_p (&vr0))
3419 set_value_range_to_varying (vr);
3420 return;
3423 /* -TYPE_MIN_VALUE = TYPE_MIN_VALUE with flag_wrapv so we can't get a
3424 useful range. */
3425 if (!TYPE_OVERFLOW_UNDEFINED (type)
3426 && ((vr0.type == VR_RANGE
3427 && vrp_val_is_min (vr0.min))
3428 || (vr0.type == VR_ANTI_RANGE
3429 && !vrp_val_is_min (vr0.min))))
3431 set_value_range_to_varying (vr);
3432 return;
3435 /* ABS_EXPR may flip the range around, if the original range
3436 included negative values. */
3437 if (is_overflow_infinity (vr0.min))
3438 min = positive_overflow_infinity (type);
3439 else if (!vrp_val_is_min (vr0.min))
3440 min = fold_unary_to_constant (code, type, vr0.min);
3441 else if (!needs_overflow_infinity (type))
3442 min = TYPE_MAX_VALUE (type);
3443 else if (supports_overflow_infinity (type))
3444 min = positive_overflow_infinity (type);
3445 else
3447 set_value_range_to_varying (vr);
3448 return;
3451 if (is_overflow_infinity (vr0.max))
3452 max = positive_overflow_infinity (type);
3453 else if (!vrp_val_is_min (vr0.max))
3454 max = fold_unary_to_constant (code, type, vr0.max);
3455 else if (!needs_overflow_infinity (type))
3456 max = TYPE_MAX_VALUE (type);
3457 else if (supports_overflow_infinity (type)
3458 /* We shouldn't generate [+INF, +INF] as set_value_range
3459 doesn't like this and ICEs. */
3460 && !is_positive_overflow_infinity (min))
3461 max = positive_overflow_infinity (type);
3462 else
3464 set_value_range_to_varying (vr);
3465 return;
3468 cmp = compare_values (min, max);
3470 /* If a VR_ANTI_RANGEs contains zero, then we have
3471 ~[-INF, min(MIN, MAX)]. */
3472 if (vr0.type == VR_ANTI_RANGE)
3474 if (range_includes_zero_p (vr0.min, vr0.max) == 1)
3476 /* Take the lower of the two values. */
3477 if (cmp != 1)
3478 max = min;
3480 /* Create ~[-INF, min (abs(MIN), abs(MAX))]
3481 or ~[-INF + 1, min (abs(MIN), abs(MAX))] when
3482 flag_wrapv is set and the original anti-range doesn't include
3483 TYPE_MIN_VALUE, remember -TYPE_MIN_VALUE = TYPE_MIN_VALUE. */
3484 if (TYPE_OVERFLOW_WRAPS (type))
3486 tree type_min_value = TYPE_MIN_VALUE (type);
3488 min = (vr0.min != type_min_value
3489 ? int_const_binop (PLUS_EXPR, type_min_value,
3490 build_int_cst (TREE_TYPE (type_min_value), 1))
3491 : type_min_value);
3493 else
3495 if (overflow_infinity_range_p (&vr0))
3496 min = negative_overflow_infinity (type);
3497 else
3498 min = TYPE_MIN_VALUE (type);
3501 else
3503 /* All else has failed, so create the range [0, INF], even for
3504 flag_wrapv since TYPE_MIN_VALUE is in the original
3505 anti-range. */
3506 vr0.type = VR_RANGE;
3507 min = build_int_cst (type, 0);
3508 if (needs_overflow_infinity (type))
3510 if (supports_overflow_infinity (type))
3511 max = positive_overflow_infinity (type);
3512 else
3514 set_value_range_to_varying (vr);
3515 return;
3518 else
3519 max = TYPE_MAX_VALUE (type);
3523 /* If the range contains zero then we know that the minimum value in the
3524 range will be zero. */
3525 else if (range_includes_zero_p (vr0.min, vr0.max) == 1)
3527 if (cmp == 1)
3528 max = min;
3529 min = build_int_cst (type, 0);
3531 else
3533 /* If the range was reversed, swap MIN and MAX. */
3534 if (cmp == 1)
3535 std::swap (min, max);
3538 cmp = compare_values (min, max);
3539 if (cmp == -2 || cmp == 1)
3541 /* If the new range has its limits swapped around (MIN > MAX),
3542 then the operation caused one of them to wrap around, mark
3543 the new range VARYING. */
3544 set_value_range_to_varying (vr);
3546 else
3547 set_value_range (vr, vr0.type, min, max, NULL);
3548 return;
3551 /* For unhandled operations fall back to varying. */
3552 set_value_range_to_varying (vr);
3553 return;
3557 /* Extract range information from a unary expression CODE OP0 based on
3558 the range of its operand with resulting type TYPE.
3559 The resulting range is stored in *VR. */
3561 static void
3562 extract_range_from_unary_expr (value_range *vr, enum tree_code code,
3563 tree type, tree op0)
3565 value_range vr0 = VR_INITIALIZER;
3567 /* Get value ranges for the operand. For constant operands, create
3568 a new value range with the operand to simplify processing. */
3569 if (TREE_CODE (op0) == SSA_NAME)
3570 vr0 = *(get_value_range (op0));
3571 else if (is_gimple_min_invariant (op0))
3572 set_value_range_to_value (&vr0, op0, NULL);
3573 else
3574 set_value_range_to_varying (&vr0);
3576 extract_range_from_unary_expr_1 (vr, code, type, &vr0, TREE_TYPE (op0));
3580 /* Extract range information from a conditional expression STMT based on
3581 the ranges of each of its operands and the expression code. */
3583 static void
3584 extract_range_from_cond_expr (value_range *vr, gassign *stmt)
3586 tree op0, op1;
3587 value_range vr0 = VR_INITIALIZER;
3588 value_range vr1 = VR_INITIALIZER;
3590 /* Get value ranges for each operand. For constant operands, create
3591 a new value range with the operand to simplify processing. */
3592 op0 = gimple_assign_rhs2 (stmt);
3593 if (TREE_CODE (op0) == SSA_NAME)
3594 vr0 = *(get_value_range (op0));
3595 else if (is_gimple_min_invariant (op0))
3596 set_value_range_to_value (&vr0, op0, NULL);
3597 else
3598 set_value_range_to_varying (&vr0);
3600 op1 = gimple_assign_rhs3 (stmt);
3601 if (TREE_CODE (op1) == SSA_NAME)
3602 vr1 = *(get_value_range (op1));
3603 else if (is_gimple_min_invariant (op1))
3604 set_value_range_to_value (&vr1, op1, NULL);
3605 else
3606 set_value_range_to_varying (&vr1);
3608 /* The resulting value range is the union of the operand ranges */
3609 copy_value_range (vr, &vr0);
3610 vrp_meet (vr, &vr1);
3614 /* Extract range information from a comparison expression EXPR based
3615 on the range of its operand and the expression code. */
3617 static void
3618 extract_range_from_comparison (value_range *vr, enum tree_code code,
3619 tree type, tree op0, tree op1)
3621 bool sop = false;
3622 tree val;
3624 val = vrp_evaluate_conditional_warnv_with_ops (code, op0, op1, false, &sop,
3625 NULL);
3627 /* A disadvantage of using a special infinity as an overflow
3628 representation is that we lose the ability to record overflow
3629 when we don't have an infinity. So we have to ignore a result
3630 which relies on overflow. */
3632 if (val && !is_overflow_infinity (val) && !sop)
3634 /* Since this expression was found on the RHS of an assignment,
3635 its type may be different from _Bool. Convert VAL to EXPR's
3636 type. */
3637 val = fold_convert (type, val);
3638 if (is_gimple_min_invariant (val))
3639 set_value_range_to_value (vr, val, vr->equiv);
3640 else
3641 set_value_range (vr, VR_RANGE, val, val, vr->equiv);
3643 else
3644 /* The result of a comparison is always true or false. */
3645 set_value_range_to_truthvalue (vr, type);
3648 /* Helper function for simplify_internal_call_using_ranges and
3649 extract_range_basic. Return true if OP0 SUBCODE OP1 for
3650 SUBCODE {PLUS,MINUS,MULT}_EXPR is known to never overflow or
3651 always overflow. Set *OVF to true if it is known to always
3652 overflow. */
3654 static bool
3655 check_for_binary_op_overflow (enum tree_code subcode, tree type,
3656 tree op0, tree op1, bool *ovf)
3658 value_range vr0 = VR_INITIALIZER;
3659 value_range vr1 = VR_INITIALIZER;
3660 if (TREE_CODE (op0) == SSA_NAME)
3661 vr0 = *get_value_range (op0);
3662 else if (TREE_CODE (op0) == INTEGER_CST)
3663 set_value_range_to_value (&vr0, op0, NULL);
3664 else
3665 set_value_range_to_varying (&vr0);
3667 if (TREE_CODE (op1) == SSA_NAME)
3668 vr1 = *get_value_range (op1);
3669 else if (TREE_CODE (op1) == INTEGER_CST)
3670 set_value_range_to_value (&vr1, op1, NULL);
3671 else
3672 set_value_range_to_varying (&vr1);
3674 if (!range_int_cst_p (&vr0)
3675 || TREE_OVERFLOW (vr0.min)
3676 || TREE_OVERFLOW (vr0.max))
3678 vr0.min = vrp_val_min (TREE_TYPE (op0));
3679 vr0.max = vrp_val_max (TREE_TYPE (op0));
3681 if (!range_int_cst_p (&vr1)
3682 || TREE_OVERFLOW (vr1.min)
3683 || TREE_OVERFLOW (vr1.max))
3685 vr1.min = vrp_val_min (TREE_TYPE (op1));
3686 vr1.max = vrp_val_max (TREE_TYPE (op1));
3688 *ovf = arith_overflowed_p (subcode, type, vr0.min,
3689 subcode == MINUS_EXPR ? vr1.max : vr1.min);
3690 if (arith_overflowed_p (subcode, type, vr0.max,
3691 subcode == MINUS_EXPR ? vr1.min : vr1.max) != *ovf)
3692 return false;
3693 if (subcode == MULT_EXPR)
3695 if (arith_overflowed_p (subcode, type, vr0.min, vr1.max) != *ovf
3696 || arith_overflowed_p (subcode, type, vr0.max, vr1.min) != *ovf)
3697 return false;
3699 if (*ovf)
3701 /* So far we found that there is an overflow on the boundaries.
3702 That doesn't prove that there is an overflow even for all values
3703 in between the boundaries. For that compute widest_int range
3704 of the result and see if it doesn't overlap the range of
3705 type. */
3706 widest_int wmin, wmax;
3707 widest_int w[4];
3708 int i;
3709 w[0] = wi::to_widest (vr0.min);
3710 w[1] = wi::to_widest (vr0.max);
3711 w[2] = wi::to_widest (vr1.min);
3712 w[3] = wi::to_widest (vr1.max);
3713 for (i = 0; i < 4; i++)
3715 widest_int wt;
3716 switch (subcode)
3718 case PLUS_EXPR:
3719 wt = wi::add (w[i & 1], w[2 + (i & 2) / 2]);
3720 break;
3721 case MINUS_EXPR:
3722 wt = wi::sub (w[i & 1], w[2 + (i & 2) / 2]);
3723 break;
3724 case MULT_EXPR:
3725 wt = wi::mul (w[i & 1], w[2 + (i & 2) / 2]);
3726 break;
3727 default:
3728 gcc_unreachable ();
3730 if (i == 0)
3732 wmin = wt;
3733 wmax = wt;
3735 else
3737 wmin = wi::smin (wmin, wt);
3738 wmax = wi::smax (wmax, wt);
3741 /* The result of op0 CODE op1 is known to be in range
3742 [wmin, wmax]. */
3743 widest_int wtmin = wi::to_widest (vrp_val_min (type));
3744 widest_int wtmax = wi::to_widest (vrp_val_max (type));
3745 /* If all values in [wmin, wmax] are smaller than
3746 [wtmin, wtmax] or all are larger than [wtmin, wtmax],
3747 the arithmetic operation will always overflow. */
3748 if (wmax < wtmin || wmin > wtmax)
3749 return true;
3750 return false;
3752 return true;
3755 /* Try to derive a nonnegative or nonzero range out of STMT relying
3756 primarily on generic routines in fold in conjunction with range data.
3757 Store the result in *VR */
3759 static void
3760 extract_range_basic (value_range *vr, gimple *stmt)
3762 bool sop = false;
3763 tree type = gimple_expr_type (stmt);
3765 if (is_gimple_call (stmt))
3767 tree arg;
3768 int mini, maxi, zerov = 0, prec;
3769 enum tree_code subcode = ERROR_MARK;
3770 combined_fn cfn = gimple_call_combined_fn (stmt);
3772 switch (cfn)
3774 case CFN_BUILT_IN_CONSTANT_P:
3775 /* If the call is __builtin_constant_p and the argument is a
3776 function parameter resolve it to false. This avoids bogus
3777 array bound warnings.
3778 ??? We could do this as early as inlining is finished. */
3779 arg = gimple_call_arg (stmt, 0);
3780 if (TREE_CODE (arg) == SSA_NAME
3781 && SSA_NAME_IS_DEFAULT_DEF (arg)
3782 && TREE_CODE (SSA_NAME_VAR (arg)) == PARM_DECL)
3784 set_value_range_to_null (vr, type);
3785 return;
3787 break;
3788 /* Both __builtin_ffs* and __builtin_popcount return
3789 [0, prec]. */
3790 CASE_CFN_FFS:
3791 CASE_CFN_POPCOUNT:
3792 arg = gimple_call_arg (stmt, 0);
3793 prec = TYPE_PRECISION (TREE_TYPE (arg));
3794 mini = 0;
3795 maxi = prec;
3796 if (TREE_CODE (arg) == SSA_NAME)
3798 value_range *vr0 = get_value_range (arg);
3799 /* If arg is non-zero, then ffs or popcount
3800 are non-zero. */
3801 if (((vr0->type == VR_RANGE
3802 && range_includes_zero_p (vr0->min, vr0->max) == 0)
3803 || (vr0->type == VR_ANTI_RANGE
3804 && range_includes_zero_p (vr0->min, vr0->max) == 1))
3805 && !is_overflow_infinity (vr0->min)
3806 && !is_overflow_infinity (vr0->max))
3807 mini = 1;
3808 /* If some high bits are known to be zero,
3809 we can decrease the maximum. */
3810 if (vr0->type == VR_RANGE
3811 && TREE_CODE (vr0->max) == INTEGER_CST
3812 && !operand_less_p (vr0->min,
3813 build_zero_cst (TREE_TYPE (vr0->min)))
3814 && !is_overflow_infinity (vr0->max))
3815 maxi = tree_floor_log2 (vr0->max) + 1;
3817 goto bitop_builtin;
3818 /* __builtin_parity* returns [0, 1]. */
3819 CASE_CFN_PARITY:
3820 mini = 0;
3821 maxi = 1;
3822 goto bitop_builtin;
3823 /* __builtin_c[lt]z* return [0, prec-1], except for
3824 when the argument is 0, but that is undefined behavior.
3825 On many targets where the CLZ RTL or optab value is defined
3826 for 0 the value is prec, so include that in the range
3827 by default. */
3828 CASE_CFN_CLZ:
3829 arg = gimple_call_arg (stmt, 0);
3830 prec = TYPE_PRECISION (TREE_TYPE (arg));
3831 mini = 0;
3832 maxi = prec;
3833 if (optab_handler (clz_optab, TYPE_MODE (TREE_TYPE (arg)))
3834 != CODE_FOR_nothing
3835 && CLZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (TREE_TYPE (arg)),
3836 zerov)
3837 /* Handle only the single common value. */
3838 && zerov != prec)
3839 /* Magic value to give up, unless vr0 proves
3840 arg is non-zero. */
3841 mini = -2;
3842 if (TREE_CODE (arg) == SSA_NAME)
3844 value_range *vr0 = get_value_range (arg);
3845 /* From clz of VR_RANGE minimum we can compute
3846 result maximum. */
3847 if (vr0->type == VR_RANGE
3848 && TREE_CODE (vr0->min) == INTEGER_CST
3849 && !is_overflow_infinity (vr0->min))
3851 maxi = prec - 1 - tree_floor_log2 (vr0->min);
3852 if (maxi != prec)
3853 mini = 0;
3855 else if (vr0->type == VR_ANTI_RANGE
3856 && integer_zerop (vr0->min)
3857 && !is_overflow_infinity (vr0->min))
3859 maxi = prec - 1;
3860 mini = 0;
3862 if (mini == -2)
3863 break;
3864 /* From clz of VR_RANGE maximum we can compute
3865 result minimum. */
3866 if (vr0->type == VR_RANGE
3867 && TREE_CODE (vr0->max) == INTEGER_CST
3868 && !is_overflow_infinity (vr0->max))
3870 mini = prec - 1 - tree_floor_log2 (vr0->max);
3871 if (mini == prec)
3872 break;
3875 if (mini == -2)
3876 break;
3877 goto bitop_builtin;
3878 /* __builtin_ctz* return [0, prec-1], except for
3879 when the argument is 0, but that is undefined behavior.
3880 If there is a ctz optab for this mode and
3881 CTZ_DEFINED_VALUE_AT_ZERO, include that in the range,
3882 otherwise just assume 0 won't be seen. */
3883 CASE_CFN_CTZ:
3884 arg = gimple_call_arg (stmt, 0);
3885 prec = TYPE_PRECISION (TREE_TYPE (arg));
3886 mini = 0;
3887 maxi = prec - 1;
3888 if (optab_handler (ctz_optab, TYPE_MODE (TREE_TYPE (arg)))
3889 != CODE_FOR_nothing
3890 && CTZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (TREE_TYPE (arg)),
3891 zerov))
3893 /* Handle only the two common values. */
3894 if (zerov == -1)
3895 mini = -1;
3896 else if (zerov == prec)
3897 maxi = prec;
3898 else
3899 /* Magic value to give up, unless vr0 proves
3900 arg is non-zero. */
3901 mini = -2;
3903 if (TREE_CODE (arg) == SSA_NAME)
3905 value_range *vr0 = get_value_range (arg);
3906 /* If arg is non-zero, then use [0, prec - 1]. */
3907 if (((vr0->type == VR_RANGE
3908 && integer_nonzerop (vr0->min))
3909 || (vr0->type == VR_ANTI_RANGE
3910 && integer_zerop (vr0->min)))
3911 && !is_overflow_infinity (vr0->min))
3913 mini = 0;
3914 maxi = prec - 1;
3916 /* If some high bits are known to be zero,
3917 we can decrease the result maximum. */
3918 if (vr0->type == VR_RANGE
3919 && TREE_CODE (vr0->max) == INTEGER_CST
3920 && !is_overflow_infinity (vr0->max))
3922 maxi = tree_floor_log2 (vr0->max);
3923 /* For vr0 [0, 0] give up. */
3924 if (maxi == -1)
3925 break;
3928 if (mini == -2)
3929 break;
3930 goto bitop_builtin;
3931 /* __builtin_clrsb* returns [0, prec-1]. */
3932 CASE_CFN_CLRSB:
3933 arg = gimple_call_arg (stmt, 0);
3934 prec = TYPE_PRECISION (TREE_TYPE (arg));
3935 mini = 0;
3936 maxi = prec - 1;
3937 goto bitop_builtin;
3938 bitop_builtin:
3939 set_value_range (vr, VR_RANGE, build_int_cst (type, mini),
3940 build_int_cst (type, maxi), NULL);
3941 return;
3942 case CFN_UBSAN_CHECK_ADD:
3943 subcode = PLUS_EXPR;
3944 break;
3945 case CFN_UBSAN_CHECK_SUB:
3946 subcode = MINUS_EXPR;
3947 break;
3948 case CFN_UBSAN_CHECK_MUL:
3949 subcode = MULT_EXPR;
3950 break;
3951 case CFN_GOACC_DIM_SIZE:
3952 case CFN_GOACC_DIM_POS:
3953 /* Optimizing these two internal functions helps the loop
3954 optimizer eliminate outer comparisons. Size is [1,N]
3955 and pos is [0,N-1]. */
3957 bool is_pos = cfn == CFN_GOACC_DIM_POS;
3958 int axis = get_oacc_ifn_dim_arg (stmt);
3959 int size = get_oacc_fn_dim_size (current_function_decl, axis);
3961 if (!size)
3962 /* If it's dynamic, the backend might know a hardware
3963 limitation. */
3964 size = targetm.goacc.dim_limit (axis);
3966 tree type = TREE_TYPE (gimple_call_lhs (stmt));
3967 set_value_range (vr, VR_RANGE,
3968 build_int_cst (type, is_pos ? 0 : 1),
3969 size ? build_int_cst (type, size - is_pos)
3970 : vrp_val_max (type), NULL);
3972 return;
3973 default:
3974 break;
3976 if (subcode != ERROR_MARK)
3978 bool saved_flag_wrapv = flag_wrapv;
3979 /* Pretend the arithmetics is wrapping. If there is
3980 any overflow, we'll complain, but will actually do
3981 wrapping operation. */
3982 flag_wrapv = 1;
3983 extract_range_from_binary_expr (vr, subcode, type,
3984 gimple_call_arg (stmt, 0),
3985 gimple_call_arg (stmt, 1));
3986 flag_wrapv = saved_flag_wrapv;
3988 /* If for both arguments vrp_valueize returned non-NULL,
3989 this should have been already folded and if not, it
3990 wasn't folded because of overflow. Avoid removing the
3991 UBSAN_CHECK_* calls in that case. */
3992 if (vr->type == VR_RANGE
3993 && (vr->min == vr->max
3994 || operand_equal_p (vr->min, vr->max, 0)))
3995 set_value_range_to_varying (vr);
3996 return;
3999 /* Handle extraction of the two results (result of arithmetics and
4000 a flag whether arithmetics overflowed) from {ADD,SUB,MUL}_OVERFLOW
4001 internal function. */
4002 else if (is_gimple_assign (stmt)
4003 && (gimple_assign_rhs_code (stmt) == REALPART_EXPR
4004 || gimple_assign_rhs_code (stmt) == IMAGPART_EXPR)
4005 && INTEGRAL_TYPE_P (type))
4007 enum tree_code code = gimple_assign_rhs_code (stmt);
4008 tree op = gimple_assign_rhs1 (stmt);
4009 if (TREE_CODE (op) == code && TREE_CODE (TREE_OPERAND (op, 0)) == SSA_NAME)
4011 gimple *g = SSA_NAME_DEF_STMT (TREE_OPERAND (op, 0));
4012 if (is_gimple_call (g) && gimple_call_internal_p (g))
4014 enum tree_code subcode = ERROR_MARK;
4015 switch (gimple_call_internal_fn (g))
4017 case IFN_ADD_OVERFLOW:
4018 subcode = PLUS_EXPR;
4019 break;
4020 case IFN_SUB_OVERFLOW:
4021 subcode = MINUS_EXPR;
4022 break;
4023 case IFN_MUL_OVERFLOW:
4024 subcode = MULT_EXPR;
4025 break;
4026 default:
4027 break;
4029 if (subcode != ERROR_MARK)
4031 tree op0 = gimple_call_arg (g, 0);
4032 tree op1 = gimple_call_arg (g, 1);
4033 if (code == IMAGPART_EXPR)
4035 bool ovf = false;
4036 if (check_for_binary_op_overflow (subcode, type,
4037 op0, op1, &ovf))
4038 set_value_range_to_value (vr,
4039 build_int_cst (type, ovf),
4040 NULL);
4041 else if (TYPE_PRECISION (type) == 1
4042 && !TYPE_UNSIGNED (type))
4043 set_value_range_to_varying (vr);
4044 else
4045 set_value_range (vr, VR_RANGE, build_int_cst (type, 0),
4046 build_int_cst (type, 1), NULL);
4048 else if (types_compatible_p (type, TREE_TYPE (op0))
4049 && types_compatible_p (type, TREE_TYPE (op1)))
4051 bool saved_flag_wrapv = flag_wrapv;
4052 /* Pretend the arithmetics is wrapping. If there is
4053 any overflow, IMAGPART_EXPR will be set. */
4054 flag_wrapv = 1;
4055 extract_range_from_binary_expr (vr, subcode, type,
4056 op0, op1);
4057 flag_wrapv = saved_flag_wrapv;
4059 else
4061 value_range vr0 = VR_INITIALIZER;
4062 value_range vr1 = VR_INITIALIZER;
4063 bool saved_flag_wrapv = flag_wrapv;
4064 /* Pretend the arithmetics is wrapping. If there is
4065 any overflow, IMAGPART_EXPR will be set. */
4066 flag_wrapv = 1;
4067 extract_range_from_unary_expr (&vr0, NOP_EXPR,
4068 type, op0);
4069 extract_range_from_unary_expr (&vr1, NOP_EXPR,
4070 type, op1);
4071 extract_range_from_binary_expr_1 (vr, subcode, type,
4072 &vr0, &vr1);
4073 flag_wrapv = saved_flag_wrapv;
4075 return;
4080 if (INTEGRAL_TYPE_P (type)
4081 && gimple_stmt_nonnegative_warnv_p (stmt, &sop))
4082 set_value_range_to_nonnegative (vr, type,
4083 sop || stmt_overflow_infinity (stmt));
4084 else if (vrp_stmt_computes_nonzero (stmt, &sop)
4085 && !sop)
4086 set_value_range_to_nonnull (vr, type);
4087 else
4088 set_value_range_to_varying (vr);
4092 /* Try to compute a useful range out of assignment STMT and store it
4093 in *VR. */
4095 static void
4096 extract_range_from_assignment (value_range *vr, gassign *stmt)
4098 enum tree_code code = gimple_assign_rhs_code (stmt);
4100 if (code == ASSERT_EXPR)
4101 extract_range_from_assert (vr, gimple_assign_rhs1 (stmt));
4102 else if (code == SSA_NAME)
4103 extract_range_from_ssa_name (vr, gimple_assign_rhs1 (stmt));
4104 else if (TREE_CODE_CLASS (code) == tcc_binary)
4105 extract_range_from_binary_expr (vr, gimple_assign_rhs_code (stmt),
4106 gimple_expr_type (stmt),
4107 gimple_assign_rhs1 (stmt),
4108 gimple_assign_rhs2 (stmt));
4109 else if (TREE_CODE_CLASS (code) == tcc_unary)
4110 extract_range_from_unary_expr (vr, gimple_assign_rhs_code (stmt),
4111 gimple_expr_type (stmt),
4112 gimple_assign_rhs1 (stmt));
4113 else if (code == COND_EXPR)
4114 extract_range_from_cond_expr (vr, stmt);
4115 else if (TREE_CODE_CLASS (code) == tcc_comparison)
4116 extract_range_from_comparison (vr, gimple_assign_rhs_code (stmt),
4117 gimple_expr_type (stmt),
4118 gimple_assign_rhs1 (stmt),
4119 gimple_assign_rhs2 (stmt));
4120 else if (get_gimple_rhs_class (code) == GIMPLE_SINGLE_RHS
4121 && is_gimple_min_invariant (gimple_assign_rhs1 (stmt)))
4122 set_value_range_to_value (vr, gimple_assign_rhs1 (stmt), NULL);
4123 else
4124 set_value_range_to_varying (vr);
4126 if (vr->type == VR_VARYING)
4127 extract_range_basic (vr, stmt);
4130 /* Given a range VR, a LOOP and a variable VAR, determine whether it
4131 would be profitable to adjust VR using scalar evolution information
4132 for VAR. If so, update VR with the new limits. */
4134 static void
4135 adjust_range_with_scev (value_range *vr, struct loop *loop,
4136 gimple *stmt, tree var)
4138 tree init, step, chrec, tmin, tmax, min, max, type, tem;
4139 enum ev_direction dir;
4141 /* TODO. Don't adjust anti-ranges. An anti-range may provide
4142 better opportunities than a regular range, but I'm not sure. */
4143 if (vr->type == VR_ANTI_RANGE)
4144 return;
4146 chrec = instantiate_parameters (loop, analyze_scalar_evolution (loop, var));
4148 /* Like in PR19590, scev can return a constant function. */
4149 if (is_gimple_min_invariant (chrec))
4151 set_value_range_to_value (vr, chrec, vr->equiv);
4152 return;
4155 if (TREE_CODE (chrec) != POLYNOMIAL_CHREC)
4156 return;
4158 init = initial_condition_in_loop_num (chrec, loop->num);
4159 tem = op_with_constant_singleton_value_range (init);
4160 if (tem)
4161 init = tem;
4162 step = evolution_part_in_loop_num (chrec, loop->num);
4163 tem = op_with_constant_singleton_value_range (step);
4164 if (tem)
4165 step = tem;
4167 /* If STEP is symbolic, we can't know whether INIT will be the
4168 minimum or maximum value in the range. Also, unless INIT is
4169 a simple expression, compare_values and possibly other functions
4170 in tree-vrp won't be able to handle it. */
4171 if (step == NULL_TREE
4172 || !is_gimple_min_invariant (step)
4173 || !valid_value_p (init))
4174 return;
4176 dir = scev_direction (chrec);
4177 if (/* Do not adjust ranges if we do not know whether the iv increases
4178 or decreases, ... */
4179 dir == EV_DIR_UNKNOWN
4180 /* ... or if it may wrap. */
4181 || scev_probably_wraps_p (NULL_TREE, init, step, stmt,
4182 get_chrec_loop (chrec), true))
4183 return;
4185 /* We use TYPE_MIN_VALUE and TYPE_MAX_VALUE here instead of
4186 negative_overflow_infinity and positive_overflow_infinity,
4187 because we have concluded that the loop probably does not
4188 wrap. */
4190 type = TREE_TYPE (var);
4191 if (POINTER_TYPE_P (type) || !TYPE_MIN_VALUE (type))
4192 tmin = lower_bound_in_type (type, type);
4193 else
4194 tmin = TYPE_MIN_VALUE (type);
4195 if (POINTER_TYPE_P (type) || !TYPE_MAX_VALUE (type))
4196 tmax = upper_bound_in_type (type, type);
4197 else
4198 tmax = TYPE_MAX_VALUE (type);
4200 /* Try to use estimated number of iterations for the loop to constrain the
4201 final value in the evolution. */
4202 if (TREE_CODE (step) == INTEGER_CST
4203 && is_gimple_val (init)
4204 && (TREE_CODE (init) != SSA_NAME
4205 || get_value_range (init)->type == VR_RANGE))
4207 widest_int nit;
4209 /* We are only entering here for loop header PHI nodes, so using
4210 the number of latch executions is the correct thing to use. */
4211 if (max_loop_iterations (loop, &nit))
4213 value_range maxvr = VR_INITIALIZER;
4214 signop sgn = TYPE_SIGN (TREE_TYPE (step));
4215 bool overflow;
4217 widest_int wtmp = wi::mul (wi::to_widest (step), nit, sgn,
4218 &overflow);
4219 /* If the multiplication overflowed we can't do a meaningful
4220 adjustment. Likewise if the result doesn't fit in the type
4221 of the induction variable. For a signed type we have to
4222 check whether the result has the expected signedness which
4223 is that of the step as number of iterations is unsigned. */
4224 if (!overflow
4225 && wi::fits_to_tree_p (wtmp, TREE_TYPE (init))
4226 && (sgn == UNSIGNED
4227 || wi::gts_p (wtmp, 0) == wi::gts_p (step, 0)))
4229 tem = wide_int_to_tree (TREE_TYPE (init), wtmp);
4230 extract_range_from_binary_expr (&maxvr, PLUS_EXPR,
4231 TREE_TYPE (init), init, tem);
4232 /* Likewise if the addition did. */
4233 if (maxvr.type == VR_RANGE)
4235 value_range initvr = VR_INITIALIZER;
4237 if (TREE_CODE (init) == SSA_NAME)
4238 initvr = *(get_value_range (init));
4239 else if (is_gimple_min_invariant (init))
4240 set_value_range_to_value (&initvr, init, NULL);
4241 else
4242 return;
4244 /* Check if init + nit * step overflows. Though we checked
4245 scev {init, step}_loop doesn't wrap, it is not enough
4246 because the loop may exit immediately. Overflow could
4247 happen in the plus expression in this case. */
4248 if ((dir == EV_DIR_DECREASES
4249 && (is_negative_overflow_infinity (maxvr.min)
4250 || compare_values (maxvr.min, initvr.min) != -1))
4251 || (dir == EV_DIR_GROWS
4252 && (is_positive_overflow_infinity (maxvr.max)
4253 || compare_values (maxvr.max, initvr.max) != 1)))
4254 return;
4256 tmin = maxvr.min;
4257 tmax = maxvr.max;
4263 if (vr->type == VR_VARYING || vr->type == VR_UNDEFINED)
4265 min = tmin;
4266 max = tmax;
4268 /* For VARYING or UNDEFINED ranges, just about anything we get
4269 from scalar evolutions should be better. */
4271 if (dir == EV_DIR_DECREASES)
4272 max = init;
4273 else
4274 min = init;
4276 else if (vr->type == VR_RANGE)
4278 min = vr->min;
4279 max = vr->max;
4281 if (dir == EV_DIR_DECREASES)
4283 /* INIT is the maximum value. If INIT is lower than VR->MAX
4284 but no smaller than VR->MIN, set VR->MAX to INIT. */
4285 if (compare_values (init, max) == -1)
4286 max = init;
4288 /* According to the loop information, the variable does not
4289 overflow. If we think it does, probably because of an
4290 overflow due to arithmetic on a different INF value,
4291 reset now. */
4292 if (is_negative_overflow_infinity (min)
4293 || compare_values (min, tmin) == -1)
4294 min = tmin;
4297 else
4299 /* If INIT is bigger than VR->MIN, set VR->MIN to INIT. */
4300 if (compare_values (init, min) == 1)
4301 min = init;
4303 if (is_positive_overflow_infinity (max)
4304 || compare_values (tmax, max) == -1)
4305 max = tmax;
4308 else
4309 return;
4311 /* If we just created an invalid range with the minimum
4312 greater than the maximum, we fail conservatively.
4313 This should happen only in unreachable
4314 parts of code, or for invalid programs. */
4315 if (compare_values (min, max) == 1
4316 || (is_negative_overflow_infinity (min)
4317 && is_positive_overflow_infinity (max)))
4318 return;
4320 /* Even for valid range info, sometimes overflow flag will leak in.
4321 As GIMPLE IL should have no constants with TREE_OVERFLOW set, we
4322 drop them except for +-overflow_infinity which still need special
4323 handling in vrp pass. */
4324 if (TREE_OVERFLOW_P (min)
4325 && ! is_negative_overflow_infinity (min))
4326 min = drop_tree_overflow (min);
4327 if (TREE_OVERFLOW_P (max)
4328 && ! is_positive_overflow_infinity (max))
4329 max = drop_tree_overflow (max);
4331 set_value_range (vr, VR_RANGE, min, max, vr->equiv);
4335 /* Given two numeric value ranges VR0, VR1 and a comparison code COMP:
4337 - Return BOOLEAN_TRUE_NODE if VR0 COMP VR1 always returns true for
4338 all the values in the ranges.
4340 - Return BOOLEAN_FALSE_NODE if the comparison always returns false.
4342 - Return NULL_TREE if it is not always possible to determine the
4343 value of the comparison.
4345 Also set *STRICT_OVERFLOW_P to indicate whether a range with an
4346 overflow infinity was used in the test. */
4349 static tree
4350 compare_ranges (enum tree_code comp, value_range *vr0, value_range *vr1,
4351 bool *strict_overflow_p)
4353 /* VARYING or UNDEFINED ranges cannot be compared. */
4354 if (vr0->type == VR_VARYING
4355 || vr0->type == VR_UNDEFINED
4356 || vr1->type == VR_VARYING
4357 || vr1->type == VR_UNDEFINED)
4358 return NULL_TREE;
4360 /* Anti-ranges need to be handled separately. */
4361 if (vr0->type == VR_ANTI_RANGE || vr1->type == VR_ANTI_RANGE)
4363 /* If both are anti-ranges, then we cannot compute any
4364 comparison. */
4365 if (vr0->type == VR_ANTI_RANGE && vr1->type == VR_ANTI_RANGE)
4366 return NULL_TREE;
4368 /* These comparisons are never statically computable. */
4369 if (comp == GT_EXPR
4370 || comp == GE_EXPR
4371 || comp == LT_EXPR
4372 || comp == LE_EXPR)
4373 return NULL_TREE;
4375 /* Equality can be computed only between a range and an
4376 anti-range. ~[VAL1, VAL2] == [VAL1, VAL2] is always false. */
4377 if (vr0->type == VR_RANGE)
4379 /* To simplify processing, make VR0 the anti-range. */
4380 value_range *tmp = vr0;
4381 vr0 = vr1;
4382 vr1 = tmp;
4385 gcc_assert (comp == NE_EXPR || comp == EQ_EXPR);
4387 if (compare_values_warnv (vr0->min, vr1->min, strict_overflow_p) == 0
4388 && compare_values_warnv (vr0->max, vr1->max, strict_overflow_p) == 0)
4389 return (comp == NE_EXPR) ? boolean_true_node : boolean_false_node;
4391 return NULL_TREE;
4394 if (!usable_range_p (vr0, strict_overflow_p)
4395 || !usable_range_p (vr1, strict_overflow_p))
4396 return NULL_TREE;
4398 /* Simplify processing. If COMP is GT_EXPR or GE_EXPR, switch the
4399 operands around and change the comparison code. */
4400 if (comp == GT_EXPR || comp == GE_EXPR)
4402 comp = (comp == GT_EXPR) ? LT_EXPR : LE_EXPR;
4403 std::swap (vr0, vr1);
4406 if (comp == EQ_EXPR)
4408 /* Equality may only be computed if both ranges represent
4409 exactly one value. */
4410 if (compare_values_warnv (vr0->min, vr0->max, strict_overflow_p) == 0
4411 && compare_values_warnv (vr1->min, vr1->max, strict_overflow_p) == 0)
4413 int cmp_min = compare_values_warnv (vr0->min, vr1->min,
4414 strict_overflow_p);
4415 int cmp_max = compare_values_warnv (vr0->max, vr1->max,
4416 strict_overflow_p);
4417 if (cmp_min == 0 && cmp_max == 0)
4418 return boolean_true_node;
4419 else if (cmp_min != -2 && cmp_max != -2)
4420 return boolean_false_node;
4422 /* If [V0_MIN, V1_MAX] < [V1_MIN, V1_MAX] then V0 != V1. */
4423 else if (compare_values_warnv (vr0->min, vr1->max,
4424 strict_overflow_p) == 1
4425 || compare_values_warnv (vr1->min, vr0->max,
4426 strict_overflow_p) == 1)
4427 return boolean_false_node;
4429 return NULL_TREE;
4431 else if (comp == NE_EXPR)
4433 int cmp1, cmp2;
4435 /* If VR0 is completely to the left or completely to the right
4436 of VR1, they are always different. Notice that we need to
4437 make sure that both comparisons yield similar results to
4438 avoid comparing values that cannot be compared at
4439 compile-time. */
4440 cmp1 = compare_values_warnv (vr0->max, vr1->min, strict_overflow_p);
4441 cmp2 = compare_values_warnv (vr0->min, vr1->max, strict_overflow_p);
4442 if ((cmp1 == -1 && cmp2 == -1) || (cmp1 == 1 && cmp2 == 1))
4443 return boolean_true_node;
4445 /* If VR0 and VR1 represent a single value and are identical,
4446 return false. */
4447 else if (compare_values_warnv (vr0->min, vr0->max,
4448 strict_overflow_p) == 0
4449 && compare_values_warnv (vr1->min, vr1->max,
4450 strict_overflow_p) == 0
4451 && compare_values_warnv (vr0->min, vr1->min,
4452 strict_overflow_p) == 0
4453 && compare_values_warnv (vr0->max, vr1->max,
4454 strict_overflow_p) == 0)
4455 return boolean_false_node;
4457 /* Otherwise, they may or may not be different. */
4458 else
4459 return NULL_TREE;
4461 else if (comp == LT_EXPR || comp == LE_EXPR)
4463 int tst;
4465 /* If VR0 is to the left of VR1, return true. */
4466 tst = compare_values_warnv (vr0->max, vr1->min, strict_overflow_p);
4467 if ((comp == LT_EXPR && tst == -1)
4468 || (comp == LE_EXPR && (tst == -1 || tst == 0)))
4470 if (overflow_infinity_range_p (vr0)
4471 || overflow_infinity_range_p (vr1))
4472 *strict_overflow_p = true;
4473 return boolean_true_node;
4476 /* If VR0 is to the right of VR1, return false. */
4477 tst = compare_values_warnv (vr0->min, vr1->max, strict_overflow_p);
4478 if ((comp == LT_EXPR && (tst == 0 || tst == 1))
4479 || (comp == LE_EXPR && tst == 1))
4481 if (overflow_infinity_range_p (vr0)
4482 || overflow_infinity_range_p (vr1))
4483 *strict_overflow_p = true;
4484 return boolean_false_node;
4487 /* Otherwise, we don't know. */
4488 return NULL_TREE;
4491 gcc_unreachable ();
4495 /* Given a value range VR, a value VAL and a comparison code COMP, return
4496 BOOLEAN_TRUE_NODE if VR COMP VAL always returns true for all the
4497 values in VR. Return BOOLEAN_FALSE_NODE if the comparison
4498 always returns false. Return NULL_TREE if it is not always
4499 possible to determine the value of the comparison. Also set
4500 *STRICT_OVERFLOW_P to indicate whether a range with an overflow
4501 infinity was used in the test. */
4503 static tree
4504 compare_range_with_value (enum tree_code comp, value_range *vr, tree val,
4505 bool *strict_overflow_p)
4507 if (vr->type == VR_VARYING || vr->type == VR_UNDEFINED)
4508 return NULL_TREE;
4510 /* Anti-ranges need to be handled separately. */
4511 if (vr->type == VR_ANTI_RANGE)
4513 /* For anti-ranges, the only predicates that we can compute at
4514 compile time are equality and inequality. */
4515 if (comp == GT_EXPR
4516 || comp == GE_EXPR
4517 || comp == LT_EXPR
4518 || comp == LE_EXPR)
4519 return NULL_TREE;
4521 /* ~[VAL_1, VAL_2] OP VAL is known if VAL_1 <= VAL <= VAL_2. */
4522 if (value_inside_range (val, vr->min, vr->max) == 1)
4523 return (comp == NE_EXPR) ? boolean_true_node : boolean_false_node;
4525 return NULL_TREE;
4528 if (!usable_range_p (vr, strict_overflow_p))
4529 return NULL_TREE;
4531 if (comp == EQ_EXPR)
4533 /* EQ_EXPR may only be computed if VR represents exactly
4534 one value. */
4535 if (compare_values_warnv (vr->min, vr->max, strict_overflow_p) == 0)
4537 int cmp = compare_values_warnv (vr->min, val, strict_overflow_p);
4538 if (cmp == 0)
4539 return boolean_true_node;
4540 else if (cmp == -1 || cmp == 1 || cmp == 2)
4541 return boolean_false_node;
4543 else if (compare_values_warnv (val, vr->min, strict_overflow_p) == -1
4544 || compare_values_warnv (vr->max, val, strict_overflow_p) == -1)
4545 return boolean_false_node;
4547 return NULL_TREE;
4549 else if (comp == NE_EXPR)
4551 /* If VAL is not inside VR, then they are always different. */
4552 if (compare_values_warnv (vr->max, val, strict_overflow_p) == -1
4553 || compare_values_warnv (vr->min, val, strict_overflow_p) == 1)
4554 return boolean_true_node;
4556 /* If VR represents exactly one value equal to VAL, then return
4557 false. */
4558 if (compare_values_warnv (vr->min, vr->max, strict_overflow_p) == 0
4559 && compare_values_warnv (vr->min, val, strict_overflow_p) == 0)
4560 return boolean_false_node;
4562 /* Otherwise, they may or may not be different. */
4563 return NULL_TREE;
4565 else if (comp == LT_EXPR || comp == LE_EXPR)
4567 int tst;
4569 /* If VR is to the left of VAL, return true. */
4570 tst = compare_values_warnv (vr->max, val, strict_overflow_p);
4571 if ((comp == LT_EXPR && tst == -1)
4572 || (comp == LE_EXPR && (tst == -1 || tst == 0)))
4574 if (overflow_infinity_range_p (vr))
4575 *strict_overflow_p = true;
4576 return boolean_true_node;
4579 /* If VR is to the right of VAL, return false. */
4580 tst = compare_values_warnv (vr->min, val, strict_overflow_p);
4581 if ((comp == LT_EXPR && (tst == 0 || tst == 1))
4582 || (comp == LE_EXPR && tst == 1))
4584 if (overflow_infinity_range_p (vr))
4585 *strict_overflow_p = true;
4586 return boolean_false_node;
4589 /* Otherwise, we don't know. */
4590 return NULL_TREE;
4592 else if (comp == GT_EXPR || comp == GE_EXPR)
4594 int tst;
4596 /* If VR is to the right of VAL, return true. */
4597 tst = compare_values_warnv (vr->min, val, strict_overflow_p);
4598 if ((comp == GT_EXPR && tst == 1)
4599 || (comp == GE_EXPR && (tst == 0 || tst == 1)))
4601 if (overflow_infinity_range_p (vr))
4602 *strict_overflow_p = true;
4603 return boolean_true_node;
4606 /* If VR is to the left of VAL, return false. */
4607 tst = compare_values_warnv (vr->max, val, strict_overflow_p);
4608 if ((comp == GT_EXPR && (tst == -1 || tst == 0))
4609 || (comp == GE_EXPR && tst == -1))
4611 if (overflow_infinity_range_p (vr))
4612 *strict_overflow_p = true;
4613 return boolean_false_node;
4616 /* Otherwise, we don't know. */
4617 return NULL_TREE;
4620 gcc_unreachable ();
4624 /* Debugging dumps. */
4626 void dump_value_range (FILE *, value_range *);
4627 void debug_value_range (value_range *);
4628 void dump_all_value_ranges (FILE *);
4629 void debug_all_value_ranges (void);
4630 void dump_vr_equiv (FILE *, bitmap);
4631 void debug_vr_equiv (bitmap);
4634 /* Dump value range VR to FILE. */
4636 void
4637 dump_value_range (FILE *file, value_range *vr)
4639 if (vr == NULL)
4640 fprintf (file, "[]");
4641 else if (vr->type == VR_UNDEFINED)
4642 fprintf (file, "UNDEFINED");
4643 else if (vr->type == VR_RANGE || vr->type == VR_ANTI_RANGE)
4645 tree type = TREE_TYPE (vr->min);
4647 fprintf (file, "%s[", (vr->type == VR_ANTI_RANGE) ? "~" : "");
4649 if (is_negative_overflow_infinity (vr->min))
4650 fprintf (file, "-INF(OVF)");
4651 else if (INTEGRAL_TYPE_P (type)
4652 && !TYPE_UNSIGNED (type)
4653 && vrp_val_is_min (vr->min))
4654 fprintf (file, "-INF");
4655 else
4656 print_generic_expr (file, vr->min, 0);
4658 fprintf (file, ", ");
4660 if (is_positive_overflow_infinity (vr->max))
4661 fprintf (file, "+INF(OVF)");
4662 else if (INTEGRAL_TYPE_P (type)
4663 && vrp_val_is_max (vr->max))
4664 fprintf (file, "+INF");
4665 else
4666 print_generic_expr (file, vr->max, 0);
4668 fprintf (file, "]");
4670 if (vr->equiv)
4672 bitmap_iterator bi;
4673 unsigned i, c = 0;
4675 fprintf (file, " EQUIVALENCES: { ");
4677 EXECUTE_IF_SET_IN_BITMAP (vr->equiv, 0, i, bi)
4679 print_generic_expr (file, ssa_name (i), 0);
4680 fprintf (file, " ");
4681 c++;
4684 fprintf (file, "} (%u elements)", c);
4687 else if (vr->type == VR_VARYING)
4688 fprintf (file, "VARYING");
4689 else
4690 fprintf (file, "INVALID RANGE");
4694 /* Dump value range VR to stderr. */
4696 DEBUG_FUNCTION void
4697 debug_value_range (value_range *vr)
4699 dump_value_range (stderr, vr);
4700 fprintf (stderr, "\n");
4704 /* Dump value ranges of all SSA_NAMEs to FILE. */
4706 void
4707 dump_all_value_ranges (FILE *file)
4709 size_t i;
4711 for (i = 0; i < num_vr_values; i++)
4713 if (vr_value[i])
4715 print_generic_expr (file, ssa_name (i), 0);
4716 fprintf (file, ": ");
4717 dump_value_range (file, vr_value[i]);
4718 fprintf (file, "\n");
4722 fprintf (file, "\n");
4726 /* Dump all value ranges to stderr. */
4728 DEBUG_FUNCTION void
4729 debug_all_value_ranges (void)
4731 dump_all_value_ranges (stderr);
4735 /* Given a COND_EXPR COND of the form 'V OP W', and an SSA name V,
4736 create a new SSA name N and return the assertion assignment
4737 'N = ASSERT_EXPR <V, V OP W>'. */
4739 static gimple *
4740 build_assert_expr_for (tree cond, tree v)
4742 tree a;
4743 gassign *assertion;
4745 gcc_assert (TREE_CODE (v) == SSA_NAME
4746 && COMPARISON_CLASS_P (cond));
4748 a = build2 (ASSERT_EXPR, TREE_TYPE (v), v, cond);
4749 assertion = gimple_build_assign (NULL_TREE, a);
4751 /* The new ASSERT_EXPR, creates a new SSA name that replaces the
4752 operand of the ASSERT_EXPR. Create it so the new name and the old one
4753 are registered in the replacement table so that we can fix the SSA web
4754 after adding all the ASSERT_EXPRs. */
4755 create_new_def_for (v, assertion, NULL);
4757 return assertion;
4761 /* Return false if EXPR is a predicate expression involving floating
4762 point values. */
4764 static inline bool
4765 fp_predicate (gimple *stmt)
4767 GIMPLE_CHECK (stmt, GIMPLE_COND);
4769 return FLOAT_TYPE_P (TREE_TYPE (gimple_cond_lhs (stmt)));
4772 /* If the range of values taken by OP can be inferred after STMT executes,
4773 return the comparison code (COMP_CODE_P) and value (VAL_P) that
4774 describes the inferred range. Return true if a range could be
4775 inferred. */
4777 static bool
4778 infer_value_range (gimple *stmt, tree op, tree_code *comp_code_p, tree *val_p)
4780 *val_p = NULL_TREE;
4781 *comp_code_p = ERROR_MARK;
4783 /* Do not attempt to infer anything in names that flow through
4784 abnormal edges. */
4785 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op))
4786 return false;
4788 /* Similarly, don't infer anything from statements that may throw
4789 exceptions. ??? Relax this requirement? */
4790 if (stmt_could_throw_p (stmt))
4791 return false;
4793 /* If STMT is the last statement of a basic block with no normal
4794 successors, there is no point inferring anything about any of its
4795 operands. We would not be able to find a proper insertion point
4796 for the assertion, anyway. */
4797 if (stmt_ends_bb_p (stmt))
4799 edge_iterator ei;
4800 edge e;
4802 FOR_EACH_EDGE (e, ei, gimple_bb (stmt)->succs)
4803 if (!(e->flags & EDGE_ABNORMAL))
4804 break;
4805 if (e == NULL)
4806 return false;
4809 if (infer_nonnull_range (stmt, op))
4811 *val_p = build_int_cst (TREE_TYPE (op), 0);
4812 *comp_code_p = NE_EXPR;
4813 return true;
4816 return false;
4820 void dump_asserts_for (FILE *, tree);
4821 void debug_asserts_for (tree);
4822 void dump_all_asserts (FILE *);
4823 void debug_all_asserts (void);
4825 /* Dump all the registered assertions for NAME to FILE. */
4827 void
4828 dump_asserts_for (FILE *file, tree name)
4830 assert_locus *loc;
4832 fprintf (file, "Assertions to be inserted for ");
4833 print_generic_expr (file, name, 0);
4834 fprintf (file, "\n");
4836 loc = asserts_for[SSA_NAME_VERSION (name)];
4837 while (loc)
4839 fprintf (file, "\t");
4840 print_gimple_stmt (file, gsi_stmt (loc->si), 0, 0);
4841 fprintf (file, "\n\tBB #%d", loc->bb->index);
4842 if (loc->e)
4844 fprintf (file, "\n\tEDGE %d->%d", loc->e->src->index,
4845 loc->e->dest->index);
4846 dump_edge_info (file, loc->e, dump_flags, 0);
4848 fprintf (file, "\n\tPREDICATE: ");
4849 print_generic_expr (file, loc->expr, 0);
4850 fprintf (file, " %s ", get_tree_code_name (loc->comp_code));
4851 print_generic_expr (file, loc->val, 0);
4852 fprintf (file, "\n\n");
4853 loc = loc->next;
4856 fprintf (file, "\n");
4860 /* Dump all the registered assertions for NAME to stderr. */
4862 DEBUG_FUNCTION void
4863 debug_asserts_for (tree name)
4865 dump_asserts_for (stderr, name);
4869 /* Dump all the registered assertions for all the names to FILE. */
4871 void
4872 dump_all_asserts (FILE *file)
4874 unsigned i;
4875 bitmap_iterator bi;
4877 fprintf (file, "\nASSERT_EXPRs to be inserted\n\n");
4878 EXECUTE_IF_SET_IN_BITMAP (need_assert_for, 0, i, bi)
4879 dump_asserts_for (file, ssa_name (i));
4880 fprintf (file, "\n");
4884 /* Dump all the registered assertions for all the names to stderr. */
4886 DEBUG_FUNCTION void
4887 debug_all_asserts (void)
4889 dump_all_asserts (stderr);
4893 /* If NAME doesn't have an ASSERT_EXPR registered for asserting
4894 'EXPR COMP_CODE VAL' at a location that dominates block BB or
4895 E->DEST, then register this location as a possible insertion point
4896 for ASSERT_EXPR <NAME, EXPR COMP_CODE VAL>.
4898 BB, E and SI provide the exact insertion point for the new
4899 ASSERT_EXPR. If BB is NULL, then the ASSERT_EXPR is to be inserted
4900 on edge E. Otherwise, if E is NULL, the ASSERT_EXPR is inserted on
4901 BB. If SI points to a COND_EXPR or a SWITCH_EXPR statement, then E
4902 must not be NULL. */
4904 static void
4905 register_new_assert_for (tree name, tree expr,
4906 enum tree_code comp_code,
4907 tree val,
4908 basic_block bb,
4909 edge e,
4910 gimple_stmt_iterator si)
4912 assert_locus *n, *loc, *last_loc;
4913 basic_block dest_bb;
4915 gcc_checking_assert (bb == NULL || e == NULL);
4917 if (e == NULL)
4918 gcc_checking_assert (gimple_code (gsi_stmt (si)) != GIMPLE_COND
4919 && gimple_code (gsi_stmt (si)) != GIMPLE_SWITCH);
4921 /* Never build an assert comparing against an integer constant with
4922 TREE_OVERFLOW set. This confuses our undefined overflow warning
4923 machinery. */
4924 if (TREE_OVERFLOW_P (val))
4925 val = drop_tree_overflow (val);
4927 /* The new assertion A will be inserted at BB or E. We need to
4928 determine if the new location is dominated by a previously
4929 registered location for A. If we are doing an edge insertion,
4930 assume that A will be inserted at E->DEST. Note that this is not
4931 necessarily true.
4933 If E is a critical edge, it will be split. But even if E is
4934 split, the new block will dominate the same set of blocks that
4935 E->DEST dominates.
4937 The reverse, however, is not true, blocks dominated by E->DEST
4938 will not be dominated by the new block created to split E. So,
4939 if the insertion location is on a critical edge, we will not use
4940 the new location to move another assertion previously registered
4941 at a block dominated by E->DEST. */
4942 dest_bb = (bb) ? bb : e->dest;
4944 /* If NAME already has an ASSERT_EXPR registered for COMP_CODE and
4945 VAL at a block dominating DEST_BB, then we don't need to insert a new
4946 one. Similarly, if the same assertion already exists at a block
4947 dominated by DEST_BB and the new location is not on a critical
4948 edge, then update the existing location for the assertion (i.e.,
4949 move the assertion up in the dominance tree).
4951 Note, this is implemented as a simple linked list because there
4952 should not be more than a handful of assertions registered per
4953 name. If this becomes a performance problem, a table hashed by
4954 COMP_CODE and VAL could be implemented. */
4955 loc = asserts_for[SSA_NAME_VERSION (name)];
4956 last_loc = loc;
4957 while (loc)
4959 if (loc->comp_code == comp_code
4960 && (loc->val == val
4961 || operand_equal_p (loc->val, val, 0))
4962 && (loc->expr == expr
4963 || operand_equal_p (loc->expr, expr, 0)))
4965 /* If E is not a critical edge and DEST_BB
4966 dominates the existing location for the assertion, move
4967 the assertion up in the dominance tree by updating its
4968 location information. */
4969 if ((e == NULL || !EDGE_CRITICAL_P (e))
4970 && dominated_by_p (CDI_DOMINATORS, loc->bb, dest_bb))
4972 loc->bb = dest_bb;
4973 loc->e = e;
4974 loc->si = si;
4975 return;
4979 /* Update the last node of the list and move to the next one. */
4980 last_loc = loc;
4981 loc = loc->next;
4984 /* If we didn't find an assertion already registered for
4985 NAME COMP_CODE VAL, add a new one at the end of the list of
4986 assertions associated with NAME. */
4987 n = XNEW (struct assert_locus);
4988 n->bb = dest_bb;
4989 n->e = e;
4990 n->si = si;
4991 n->comp_code = comp_code;
4992 n->val = val;
4993 n->expr = expr;
4994 n->next = NULL;
4996 if (last_loc)
4997 last_loc->next = n;
4998 else
4999 asserts_for[SSA_NAME_VERSION (name)] = n;
5001 bitmap_set_bit (need_assert_for, SSA_NAME_VERSION (name));
5004 /* (COND_OP0 COND_CODE COND_OP1) is a predicate which uses NAME.
5005 Extract a suitable test code and value and store them into *CODE_P and
5006 *VAL_P so the predicate is normalized to NAME *CODE_P *VAL_P.
5008 If no extraction was possible, return FALSE, otherwise return TRUE.
5010 If INVERT is true, then we invert the result stored into *CODE_P. */
5012 static bool
5013 extract_code_and_val_from_cond_with_ops (tree name, enum tree_code cond_code,
5014 tree cond_op0, tree cond_op1,
5015 bool invert, enum tree_code *code_p,
5016 tree *val_p)
5018 enum tree_code comp_code;
5019 tree val;
5021 /* Otherwise, we have a comparison of the form NAME COMP VAL
5022 or VAL COMP NAME. */
5023 if (name == cond_op1)
5025 /* If the predicate is of the form VAL COMP NAME, flip
5026 COMP around because we need to register NAME as the
5027 first operand in the predicate. */
5028 comp_code = swap_tree_comparison (cond_code);
5029 val = cond_op0;
5031 else if (name == cond_op0)
5033 /* The comparison is of the form NAME COMP VAL, so the
5034 comparison code remains unchanged. */
5035 comp_code = cond_code;
5036 val = cond_op1;
5038 else
5039 gcc_unreachable ();
5041 /* Invert the comparison code as necessary. */
5042 if (invert)
5043 comp_code = invert_tree_comparison (comp_code, 0);
5045 /* VRP only handles integral and pointer types. */
5046 if (! INTEGRAL_TYPE_P (TREE_TYPE (val))
5047 && ! POINTER_TYPE_P (TREE_TYPE (val)))
5048 return false;
5050 /* Do not register always-false predicates.
5051 FIXME: this works around a limitation in fold() when dealing with
5052 enumerations. Given 'enum { N1, N2 } x;', fold will not
5053 fold 'if (x > N2)' to 'if (0)'. */
5054 if ((comp_code == GT_EXPR || comp_code == LT_EXPR)
5055 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
5057 tree min = TYPE_MIN_VALUE (TREE_TYPE (val));
5058 tree max = TYPE_MAX_VALUE (TREE_TYPE (val));
5060 if (comp_code == GT_EXPR
5061 && (!max
5062 || compare_values (val, max) == 0))
5063 return false;
5065 if (comp_code == LT_EXPR
5066 && (!min
5067 || compare_values (val, min) == 0))
5068 return false;
5070 *code_p = comp_code;
5071 *val_p = val;
5072 return true;
5075 /* Find out smallest RES where RES > VAL && (RES & MASK) == RES, if any
5076 (otherwise return VAL). VAL and MASK must be zero-extended for
5077 precision PREC. If SGNBIT is non-zero, first xor VAL with SGNBIT
5078 (to transform signed values into unsigned) and at the end xor
5079 SGNBIT back. */
5081 static wide_int
5082 masked_increment (const wide_int &val_in, const wide_int &mask,
5083 const wide_int &sgnbit, unsigned int prec)
5085 wide_int bit = wi::one (prec), res;
5086 unsigned int i;
5088 wide_int val = val_in ^ sgnbit;
5089 for (i = 0; i < prec; i++, bit += bit)
5091 res = mask;
5092 if ((res & bit) == 0)
5093 continue;
5094 res = bit - 1;
5095 res = (val + bit).and_not (res);
5096 res &= mask;
5097 if (wi::gtu_p (res, val))
5098 return res ^ sgnbit;
5100 return val ^ sgnbit;
5103 /* Try to register an edge assertion for SSA name NAME on edge E for
5104 the condition COND contributing to the conditional jump pointed to by BSI.
5105 Invert the condition COND if INVERT is true. */
5107 static void
5108 register_edge_assert_for_2 (tree name, edge e, gimple_stmt_iterator bsi,
5109 enum tree_code cond_code,
5110 tree cond_op0, tree cond_op1, bool invert)
5112 tree val;
5113 enum tree_code comp_code;
5115 if (!extract_code_and_val_from_cond_with_ops (name, cond_code,
5116 cond_op0,
5117 cond_op1,
5118 invert, &comp_code, &val))
5119 return;
5121 /* Only register an ASSERT_EXPR if NAME was found in the sub-graph
5122 reachable from E. */
5123 if (live_on_edge (e, name))
5124 register_new_assert_for (name, name, comp_code, val, NULL, e, bsi);
5126 /* In the case of NAME <= CST and NAME being defined as
5127 NAME = (unsigned) NAME2 + CST2 we can assert NAME2 >= -CST2
5128 and NAME2 <= CST - CST2. We can do the same for NAME > CST.
5129 This catches range and anti-range tests. */
5130 if ((comp_code == LE_EXPR
5131 || comp_code == GT_EXPR)
5132 && TREE_CODE (val) == INTEGER_CST
5133 && TYPE_UNSIGNED (TREE_TYPE (val)))
5135 gimple *def_stmt = SSA_NAME_DEF_STMT (name);
5136 tree cst2 = NULL_TREE, name2 = NULL_TREE, name3 = NULL_TREE;
5138 /* Extract CST2 from the (optional) addition. */
5139 if (is_gimple_assign (def_stmt)
5140 && gimple_assign_rhs_code (def_stmt) == PLUS_EXPR)
5142 name2 = gimple_assign_rhs1 (def_stmt);
5143 cst2 = gimple_assign_rhs2 (def_stmt);
5144 if (TREE_CODE (name2) == SSA_NAME
5145 && TREE_CODE (cst2) == INTEGER_CST)
5146 def_stmt = SSA_NAME_DEF_STMT (name2);
5149 /* Extract NAME2 from the (optional) sign-changing cast. */
5150 if (gimple_assign_cast_p (def_stmt))
5152 if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt))
5153 && ! TYPE_UNSIGNED (TREE_TYPE (gimple_assign_rhs1 (def_stmt)))
5154 && (TYPE_PRECISION (gimple_expr_type (def_stmt))
5155 == TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt)))))
5156 name3 = gimple_assign_rhs1 (def_stmt);
5159 /* If name3 is used later, create an ASSERT_EXPR for it. */
5160 if (name3 != NULL_TREE
5161 && TREE_CODE (name3) == SSA_NAME
5162 && (cst2 == NULL_TREE
5163 || TREE_CODE (cst2) == INTEGER_CST)
5164 && INTEGRAL_TYPE_P (TREE_TYPE (name3))
5165 && live_on_edge (e, name3))
5167 tree tmp;
5169 /* Build an expression for the range test. */
5170 tmp = build1 (NOP_EXPR, TREE_TYPE (name), name3);
5171 if (cst2 != NULL_TREE)
5172 tmp = build2 (PLUS_EXPR, TREE_TYPE (name), tmp, cst2);
5174 if (dump_file)
5176 fprintf (dump_file, "Adding assert for ");
5177 print_generic_expr (dump_file, name3, 0);
5178 fprintf (dump_file, " from ");
5179 print_generic_expr (dump_file, tmp, 0);
5180 fprintf (dump_file, "\n");
5183 register_new_assert_for (name3, tmp, comp_code, val, NULL, e, bsi);
5186 /* If name2 is used later, create an ASSERT_EXPR for it. */
5187 if (name2 != NULL_TREE
5188 && TREE_CODE (name2) == SSA_NAME
5189 && TREE_CODE (cst2) == INTEGER_CST
5190 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
5191 && live_on_edge (e, name2))
5193 tree tmp;
5195 /* Build an expression for the range test. */
5196 tmp = name2;
5197 if (TREE_TYPE (name) != TREE_TYPE (name2))
5198 tmp = build1 (NOP_EXPR, TREE_TYPE (name), tmp);
5199 if (cst2 != NULL_TREE)
5200 tmp = build2 (PLUS_EXPR, TREE_TYPE (name), tmp, cst2);
5202 if (dump_file)
5204 fprintf (dump_file, "Adding assert for ");
5205 print_generic_expr (dump_file, name2, 0);
5206 fprintf (dump_file, " from ");
5207 print_generic_expr (dump_file, tmp, 0);
5208 fprintf (dump_file, "\n");
5211 register_new_assert_for (name2, tmp, comp_code, val, NULL, e, bsi);
5215 /* In the case of post-in/decrement tests like if (i++) ... and uses
5216 of the in/decremented value on the edge the extra name we want to
5217 assert for is not on the def chain of the name compared. Instead
5218 it is in the set of use stmts.
5219 Similar cases happen for conversions that were simplified through
5220 fold_{sign_changed,widened}_comparison. */
5221 if ((comp_code == NE_EXPR
5222 || comp_code == EQ_EXPR)
5223 && TREE_CODE (val) == INTEGER_CST)
5225 imm_use_iterator ui;
5226 gimple *use_stmt;
5227 FOR_EACH_IMM_USE_STMT (use_stmt, ui, name)
5229 if (!is_gimple_assign (use_stmt))
5230 continue;
5232 /* Cut off to use-stmts that are dominating the predecessor. */
5233 if (!dominated_by_p (CDI_DOMINATORS, e->src, gimple_bb (use_stmt)))
5234 continue;
5236 tree name2 = gimple_assign_lhs (use_stmt);
5237 if (TREE_CODE (name2) != SSA_NAME
5238 || !live_on_edge (e, name2))
5239 continue;
5241 enum tree_code code = gimple_assign_rhs_code (use_stmt);
5242 tree cst;
5243 if (code == PLUS_EXPR
5244 || code == MINUS_EXPR)
5246 cst = gimple_assign_rhs2 (use_stmt);
5247 if (TREE_CODE (cst) != INTEGER_CST)
5248 continue;
5249 cst = int_const_binop (code, val, cst);
5251 else if (CONVERT_EXPR_CODE_P (code))
5253 /* For truncating conversions we cannot record
5254 an inequality. */
5255 if (comp_code == NE_EXPR
5256 && (TYPE_PRECISION (TREE_TYPE (name2))
5257 < TYPE_PRECISION (TREE_TYPE (name))))
5258 continue;
5259 cst = fold_convert (TREE_TYPE (name2), val);
5261 else
5262 continue;
5264 if (TREE_OVERFLOW_P (cst))
5265 cst = drop_tree_overflow (cst);
5266 register_new_assert_for (name2, name2, comp_code, cst,
5267 NULL, e, bsi);
5271 if (TREE_CODE_CLASS (comp_code) == tcc_comparison
5272 && TREE_CODE (val) == INTEGER_CST)
5274 gimple *def_stmt = SSA_NAME_DEF_STMT (name);
5275 tree name2 = NULL_TREE, names[2], cst2 = NULL_TREE;
5276 tree val2 = NULL_TREE;
5277 unsigned int prec = TYPE_PRECISION (TREE_TYPE (val));
5278 wide_int mask = wi::zero (prec);
5279 unsigned int nprec = prec;
5280 enum tree_code rhs_code = ERROR_MARK;
5282 if (is_gimple_assign (def_stmt))
5283 rhs_code = gimple_assign_rhs_code (def_stmt);
5285 /* In the case of NAME != CST1 where NAME = A +- CST2 we can
5286 assert that A != CST1 -+ CST2. */
5287 if ((comp_code == EQ_EXPR || comp_code == NE_EXPR)
5288 && (rhs_code == PLUS_EXPR || rhs_code == MINUS_EXPR))
5290 tree op0 = gimple_assign_rhs1 (def_stmt);
5291 tree op1 = gimple_assign_rhs2 (def_stmt);
5292 if (TREE_CODE (op0) == SSA_NAME
5293 && TREE_CODE (op1) == INTEGER_CST
5294 && live_on_edge (e, op0))
5296 enum tree_code reverse_op = (rhs_code == PLUS_EXPR
5297 ? MINUS_EXPR : PLUS_EXPR);
5298 op1 = int_const_binop (reverse_op, val, op1);
5299 if (TREE_OVERFLOW (op1))
5300 op1 = drop_tree_overflow (op1);
5301 register_new_assert_for (op0, op0, comp_code, op1, NULL, e, bsi);
5305 /* Add asserts for NAME cmp CST and NAME being defined
5306 as NAME = (int) NAME2. */
5307 if (!TYPE_UNSIGNED (TREE_TYPE (val))
5308 && (comp_code == LE_EXPR || comp_code == LT_EXPR
5309 || comp_code == GT_EXPR || comp_code == GE_EXPR)
5310 && gimple_assign_cast_p (def_stmt))
5312 name2 = gimple_assign_rhs1 (def_stmt);
5313 if (CONVERT_EXPR_CODE_P (rhs_code)
5314 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
5315 && TYPE_UNSIGNED (TREE_TYPE (name2))
5316 && prec == TYPE_PRECISION (TREE_TYPE (name2))
5317 && (comp_code == LE_EXPR || comp_code == GT_EXPR
5318 || !tree_int_cst_equal (val,
5319 TYPE_MIN_VALUE (TREE_TYPE (val))))
5320 && live_on_edge (e, name2))
5322 tree tmp, cst;
5323 enum tree_code new_comp_code = comp_code;
5325 cst = fold_convert (TREE_TYPE (name2),
5326 TYPE_MIN_VALUE (TREE_TYPE (val)));
5327 /* Build an expression for the range test. */
5328 tmp = build2 (PLUS_EXPR, TREE_TYPE (name2), name2, cst);
5329 cst = fold_build2 (PLUS_EXPR, TREE_TYPE (name2), cst,
5330 fold_convert (TREE_TYPE (name2), val));
5331 if (comp_code == LT_EXPR || comp_code == GE_EXPR)
5333 new_comp_code = comp_code == LT_EXPR ? LE_EXPR : GT_EXPR;
5334 cst = fold_build2 (MINUS_EXPR, TREE_TYPE (name2), cst,
5335 build_int_cst (TREE_TYPE (name2), 1));
5338 if (dump_file)
5340 fprintf (dump_file, "Adding assert for ");
5341 print_generic_expr (dump_file, name2, 0);
5342 fprintf (dump_file, " from ");
5343 print_generic_expr (dump_file, tmp, 0);
5344 fprintf (dump_file, "\n");
5347 register_new_assert_for (name2, tmp, new_comp_code, cst, NULL,
5348 e, bsi);
5352 /* Add asserts for NAME cmp CST and NAME being defined as
5353 NAME = NAME2 >> CST2.
5355 Extract CST2 from the right shift. */
5356 if (rhs_code == RSHIFT_EXPR)
5358 name2 = gimple_assign_rhs1 (def_stmt);
5359 cst2 = gimple_assign_rhs2 (def_stmt);
5360 if (TREE_CODE (name2) == SSA_NAME
5361 && tree_fits_uhwi_p (cst2)
5362 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
5363 && IN_RANGE (tree_to_uhwi (cst2), 1, prec - 1)
5364 && prec == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (val)))
5365 && live_on_edge (e, name2))
5367 mask = wi::mask (tree_to_uhwi (cst2), false, prec);
5368 val2 = fold_binary (LSHIFT_EXPR, TREE_TYPE (val), val, cst2);
5371 if (val2 != NULL_TREE
5372 && TREE_CODE (val2) == INTEGER_CST
5373 && simple_cst_equal (fold_build2 (RSHIFT_EXPR,
5374 TREE_TYPE (val),
5375 val2, cst2), val))
5377 enum tree_code new_comp_code = comp_code;
5378 tree tmp, new_val;
5380 tmp = name2;
5381 if (comp_code == EQ_EXPR || comp_code == NE_EXPR)
5383 if (!TYPE_UNSIGNED (TREE_TYPE (val)))
5385 tree type = build_nonstandard_integer_type (prec, 1);
5386 tmp = build1 (NOP_EXPR, type, name2);
5387 val2 = fold_convert (type, val2);
5389 tmp = fold_build2 (MINUS_EXPR, TREE_TYPE (tmp), tmp, val2);
5390 new_val = wide_int_to_tree (TREE_TYPE (tmp), mask);
5391 new_comp_code = comp_code == EQ_EXPR ? LE_EXPR : GT_EXPR;
5393 else if (comp_code == LT_EXPR || comp_code == GE_EXPR)
5395 wide_int minval
5396 = wi::min_value (prec, TYPE_SIGN (TREE_TYPE (val)));
5397 new_val = val2;
5398 if (minval == new_val)
5399 new_val = NULL_TREE;
5401 else
5403 wide_int maxval
5404 = wi::max_value (prec, TYPE_SIGN (TREE_TYPE (val)));
5405 mask |= val2;
5406 if (mask == maxval)
5407 new_val = NULL_TREE;
5408 else
5409 new_val = wide_int_to_tree (TREE_TYPE (val2), mask);
5412 if (new_val)
5414 if (dump_file)
5416 fprintf (dump_file, "Adding assert for ");
5417 print_generic_expr (dump_file, name2, 0);
5418 fprintf (dump_file, " from ");
5419 print_generic_expr (dump_file, tmp, 0);
5420 fprintf (dump_file, "\n");
5423 register_new_assert_for (name2, tmp, new_comp_code, new_val,
5424 NULL, e, bsi);
5428 /* Add asserts for NAME cmp CST and NAME being defined as
5429 NAME = NAME2 & CST2.
5431 Extract CST2 from the and.
5433 Also handle
5434 NAME = (unsigned) NAME2;
5435 casts where NAME's type is unsigned and has smaller precision
5436 than NAME2's type as if it was NAME = NAME2 & MASK. */
5437 names[0] = NULL_TREE;
5438 names[1] = NULL_TREE;
5439 cst2 = NULL_TREE;
5440 if (rhs_code == BIT_AND_EXPR
5441 || (CONVERT_EXPR_CODE_P (rhs_code)
5442 && INTEGRAL_TYPE_P (TREE_TYPE (val))
5443 && TYPE_UNSIGNED (TREE_TYPE (val))
5444 && TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt)))
5445 > prec))
5447 name2 = gimple_assign_rhs1 (def_stmt);
5448 if (rhs_code == BIT_AND_EXPR)
5449 cst2 = gimple_assign_rhs2 (def_stmt);
5450 else
5452 cst2 = TYPE_MAX_VALUE (TREE_TYPE (val));
5453 nprec = TYPE_PRECISION (TREE_TYPE (name2));
5455 if (TREE_CODE (name2) == SSA_NAME
5456 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
5457 && TREE_CODE (cst2) == INTEGER_CST
5458 && !integer_zerop (cst2)
5459 && (nprec > 1
5460 || TYPE_UNSIGNED (TREE_TYPE (val))))
5462 gimple *def_stmt2 = SSA_NAME_DEF_STMT (name2);
5463 if (gimple_assign_cast_p (def_stmt2))
5465 names[1] = gimple_assign_rhs1 (def_stmt2);
5466 if (!CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt2))
5467 || !INTEGRAL_TYPE_P (TREE_TYPE (names[1]))
5468 || (TYPE_PRECISION (TREE_TYPE (name2))
5469 != TYPE_PRECISION (TREE_TYPE (names[1])))
5470 || !live_on_edge (e, names[1]))
5471 names[1] = NULL_TREE;
5473 if (live_on_edge (e, name2))
5474 names[0] = name2;
5477 if (names[0] || names[1])
5479 wide_int minv, maxv, valv, cst2v;
5480 wide_int tem, sgnbit;
5481 bool valid_p = false, valn, cst2n;
5482 enum tree_code ccode = comp_code;
5484 valv = wide_int::from (val, nprec, UNSIGNED);
5485 cst2v = wide_int::from (cst2, nprec, UNSIGNED);
5486 valn = wi::neg_p (valv, TYPE_SIGN (TREE_TYPE (val)));
5487 cst2n = wi::neg_p (cst2v, TYPE_SIGN (TREE_TYPE (val)));
5488 /* If CST2 doesn't have most significant bit set,
5489 but VAL is negative, we have comparison like
5490 if ((x & 0x123) > -4) (always true). Just give up. */
5491 if (!cst2n && valn)
5492 ccode = ERROR_MARK;
5493 if (cst2n)
5494 sgnbit = wi::set_bit_in_zero (nprec - 1, nprec);
5495 else
5496 sgnbit = wi::zero (nprec);
5497 minv = valv & cst2v;
5498 switch (ccode)
5500 case EQ_EXPR:
5501 /* Minimum unsigned value for equality is VAL & CST2
5502 (should be equal to VAL, otherwise we probably should
5503 have folded the comparison into false) and
5504 maximum unsigned value is VAL | ~CST2. */
5505 maxv = valv | ~cst2v;
5506 valid_p = true;
5507 break;
5509 case NE_EXPR:
5510 tem = valv | ~cst2v;
5511 /* If VAL is 0, handle (X & CST2) != 0 as (X & CST2) > 0U. */
5512 if (valv == 0)
5514 cst2n = false;
5515 sgnbit = wi::zero (nprec);
5516 goto gt_expr;
5518 /* If (VAL | ~CST2) is all ones, handle it as
5519 (X & CST2) < VAL. */
5520 if (tem == -1)
5522 cst2n = false;
5523 valn = false;
5524 sgnbit = wi::zero (nprec);
5525 goto lt_expr;
5527 if (!cst2n && wi::neg_p (cst2v))
5528 sgnbit = wi::set_bit_in_zero (nprec - 1, nprec);
5529 if (sgnbit != 0)
5531 if (valv == sgnbit)
5533 cst2n = true;
5534 valn = true;
5535 goto gt_expr;
5537 if (tem == wi::mask (nprec - 1, false, nprec))
5539 cst2n = true;
5540 goto lt_expr;
5542 if (!cst2n)
5543 sgnbit = wi::zero (nprec);
5545 break;
5547 case GE_EXPR:
5548 /* Minimum unsigned value for >= if (VAL & CST2) == VAL
5549 is VAL and maximum unsigned value is ~0. For signed
5550 comparison, if CST2 doesn't have most significant bit
5551 set, handle it similarly. If CST2 has MSB set,
5552 the minimum is the same, and maximum is ~0U/2. */
5553 if (minv != valv)
5555 /* If (VAL & CST2) != VAL, X & CST2 can't be equal to
5556 VAL. */
5557 minv = masked_increment (valv, cst2v, sgnbit, nprec);
5558 if (minv == valv)
5559 break;
5561 maxv = wi::mask (nprec - (cst2n ? 1 : 0), false, nprec);
5562 valid_p = true;
5563 break;
5565 case GT_EXPR:
5566 gt_expr:
5567 /* Find out smallest MINV where MINV > VAL
5568 && (MINV & CST2) == MINV, if any. If VAL is signed and
5569 CST2 has MSB set, compute it biased by 1 << (nprec - 1). */
5570 minv = masked_increment (valv, cst2v, sgnbit, nprec);
5571 if (minv == valv)
5572 break;
5573 maxv = wi::mask (nprec - (cst2n ? 1 : 0), false, nprec);
5574 valid_p = true;
5575 break;
5577 case LE_EXPR:
5578 /* Minimum unsigned value for <= is 0 and maximum
5579 unsigned value is VAL | ~CST2 if (VAL & CST2) == VAL.
5580 Otherwise, find smallest VAL2 where VAL2 > VAL
5581 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
5582 as maximum.
5583 For signed comparison, if CST2 doesn't have most
5584 significant bit set, handle it similarly. If CST2 has
5585 MSB set, the maximum is the same and minimum is INT_MIN. */
5586 if (minv == valv)
5587 maxv = valv;
5588 else
5590 maxv = masked_increment (valv, cst2v, sgnbit, nprec);
5591 if (maxv == valv)
5592 break;
5593 maxv -= 1;
5595 maxv |= ~cst2v;
5596 minv = sgnbit;
5597 valid_p = true;
5598 break;
5600 case LT_EXPR:
5601 lt_expr:
5602 /* Minimum unsigned value for < is 0 and maximum
5603 unsigned value is (VAL-1) | ~CST2 if (VAL & CST2) == VAL.
5604 Otherwise, find smallest VAL2 where VAL2 > VAL
5605 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
5606 as maximum.
5607 For signed comparison, if CST2 doesn't have most
5608 significant bit set, handle it similarly. If CST2 has
5609 MSB set, the maximum is the same and minimum is INT_MIN. */
5610 if (minv == valv)
5612 if (valv == sgnbit)
5613 break;
5614 maxv = valv;
5616 else
5618 maxv = masked_increment (valv, cst2v, sgnbit, nprec);
5619 if (maxv == valv)
5620 break;
5622 maxv -= 1;
5623 maxv |= ~cst2v;
5624 minv = sgnbit;
5625 valid_p = true;
5626 break;
5628 default:
5629 break;
5631 if (valid_p
5632 && (maxv - minv) != -1)
5634 tree tmp, new_val, type;
5635 int i;
5637 for (i = 0; i < 2; i++)
5638 if (names[i])
5640 wide_int maxv2 = maxv;
5641 tmp = names[i];
5642 type = TREE_TYPE (names[i]);
5643 if (!TYPE_UNSIGNED (type))
5645 type = build_nonstandard_integer_type (nprec, 1);
5646 tmp = build1 (NOP_EXPR, type, names[i]);
5648 if (minv != 0)
5650 tmp = build2 (PLUS_EXPR, type, tmp,
5651 wide_int_to_tree (type, -minv));
5652 maxv2 = maxv - minv;
5654 new_val = wide_int_to_tree (type, maxv2);
5656 if (dump_file)
5658 fprintf (dump_file, "Adding assert for ");
5659 print_generic_expr (dump_file, names[i], 0);
5660 fprintf (dump_file, " from ");
5661 print_generic_expr (dump_file, tmp, 0);
5662 fprintf (dump_file, "\n");
5665 register_new_assert_for (names[i], tmp, LE_EXPR,
5666 new_val, NULL, e, bsi);
5673 /* OP is an operand of a truth value expression which is known to have
5674 a particular value. Register any asserts for OP and for any
5675 operands in OP's defining statement.
5677 If CODE is EQ_EXPR, then we want to register OP is zero (false),
5678 if CODE is NE_EXPR, then we want to register OP is nonzero (true). */
5680 static void
5681 register_edge_assert_for_1 (tree op, enum tree_code code,
5682 edge e, gimple_stmt_iterator bsi)
5684 gimple *op_def;
5685 tree val;
5686 enum tree_code rhs_code;
5688 /* We only care about SSA_NAMEs. */
5689 if (TREE_CODE (op) != SSA_NAME)
5690 return;
5692 /* We know that OP will have a zero or nonzero value. If OP is used
5693 more than once go ahead and register an assert for OP. */
5694 if (live_on_edge (e, op))
5696 val = build_int_cst (TREE_TYPE (op), 0);
5697 register_new_assert_for (op, op, code, val, NULL, e, bsi);
5700 /* Now look at how OP is set. If it's set from a comparison,
5701 a truth operation or some bit operations, then we may be able
5702 to register information about the operands of that assignment. */
5703 op_def = SSA_NAME_DEF_STMT (op);
5704 if (gimple_code (op_def) != GIMPLE_ASSIGN)
5705 return;
5707 rhs_code = gimple_assign_rhs_code (op_def);
5709 if (TREE_CODE_CLASS (rhs_code) == tcc_comparison)
5711 bool invert = (code == EQ_EXPR ? true : false);
5712 tree op0 = gimple_assign_rhs1 (op_def);
5713 tree op1 = gimple_assign_rhs2 (op_def);
5715 if (TREE_CODE (op0) == SSA_NAME)
5716 register_edge_assert_for_2 (op0, e, bsi, rhs_code, op0, op1, invert);
5717 if (TREE_CODE (op1) == SSA_NAME)
5718 register_edge_assert_for_2 (op1, e, bsi, rhs_code, op0, op1, invert);
5720 else if ((code == NE_EXPR
5721 && gimple_assign_rhs_code (op_def) == BIT_AND_EXPR)
5722 || (code == EQ_EXPR
5723 && gimple_assign_rhs_code (op_def) == BIT_IOR_EXPR))
5725 /* Recurse on each operand. */
5726 tree op0 = gimple_assign_rhs1 (op_def);
5727 tree op1 = gimple_assign_rhs2 (op_def);
5728 if (TREE_CODE (op0) == SSA_NAME
5729 && has_single_use (op0))
5730 register_edge_assert_for_1 (op0, code, e, bsi);
5731 if (TREE_CODE (op1) == SSA_NAME
5732 && has_single_use (op1))
5733 register_edge_assert_for_1 (op1, code, e, bsi);
5735 else if (gimple_assign_rhs_code (op_def) == BIT_NOT_EXPR
5736 && TYPE_PRECISION (TREE_TYPE (gimple_assign_lhs (op_def))) == 1)
5738 /* Recurse, flipping CODE. */
5739 code = invert_tree_comparison (code, false);
5740 register_edge_assert_for_1 (gimple_assign_rhs1 (op_def), code, e, bsi);
5742 else if (gimple_assign_rhs_code (op_def) == SSA_NAME)
5744 /* Recurse through the copy. */
5745 register_edge_assert_for_1 (gimple_assign_rhs1 (op_def), code, e, bsi);
5747 else if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (op_def)))
5749 /* Recurse through the type conversion, unless it is a narrowing
5750 conversion or conversion from non-integral type. */
5751 tree rhs = gimple_assign_rhs1 (op_def);
5752 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs))
5753 && (TYPE_PRECISION (TREE_TYPE (rhs))
5754 <= TYPE_PRECISION (TREE_TYPE (op))))
5755 register_edge_assert_for_1 (rhs, code, e, bsi);
5759 /* Try to register an edge assertion for SSA name NAME on edge E for
5760 the condition COND contributing to the conditional jump pointed to by
5761 SI. */
5763 static void
5764 register_edge_assert_for (tree name, edge e, gimple_stmt_iterator si,
5765 enum tree_code cond_code, tree cond_op0,
5766 tree cond_op1)
5768 tree val;
5769 enum tree_code comp_code;
5770 bool is_else_edge = (e->flags & EDGE_FALSE_VALUE) != 0;
5772 /* Do not attempt to infer anything in names that flow through
5773 abnormal edges. */
5774 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name))
5775 return;
5777 if (!extract_code_and_val_from_cond_with_ops (name, cond_code,
5778 cond_op0, cond_op1,
5779 is_else_edge,
5780 &comp_code, &val))
5781 return;
5783 /* Register ASSERT_EXPRs for name. */
5784 register_edge_assert_for_2 (name, e, si, cond_code, cond_op0,
5785 cond_op1, is_else_edge);
5788 /* If COND is effectively an equality test of an SSA_NAME against
5789 the value zero or one, then we may be able to assert values
5790 for SSA_NAMEs which flow into COND. */
5792 /* In the case of NAME == 1 or NAME != 0, for BIT_AND_EXPR defining
5793 statement of NAME we can assert both operands of the BIT_AND_EXPR
5794 have nonzero value. */
5795 if (((comp_code == EQ_EXPR && integer_onep (val))
5796 || (comp_code == NE_EXPR && integer_zerop (val))))
5798 gimple *def_stmt = SSA_NAME_DEF_STMT (name);
5800 if (is_gimple_assign (def_stmt)
5801 && gimple_assign_rhs_code (def_stmt) == BIT_AND_EXPR)
5803 tree op0 = gimple_assign_rhs1 (def_stmt);
5804 tree op1 = gimple_assign_rhs2 (def_stmt);
5805 register_edge_assert_for_1 (op0, NE_EXPR, e, si);
5806 register_edge_assert_for_1 (op1, NE_EXPR, e, si);
5810 /* In the case of NAME == 0 or NAME != 1, for BIT_IOR_EXPR defining
5811 statement of NAME we can assert both operands of the BIT_IOR_EXPR
5812 have zero value. */
5813 if (((comp_code == EQ_EXPR && integer_zerop (val))
5814 || (comp_code == NE_EXPR && integer_onep (val))))
5816 gimple *def_stmt = SSA_NAME_DEF_STMT (name);
5818 /* For BIT_IOR_EXPR only if NAME == 0 both operands have
5819 necessarily zero value, or if type-precision is one. */
5820 if (is_gimple_assign (def_stmt)
5821 && (gimple_assign_rhs_code (def_stmt) == BIT_IOR_EXPR
5822 && (TYPE_PRECISION (TREE_TYPE (name)) == 1
5823 || comp_code == EQ_EXPR)))
5825 tree op0 = gimple_assign_rhs1 (def_stmt);
5826 tree op1 = gimple_assign_rhs2 (def_stmt);
5827 register_edge_assert_for_1 (op0, EQ_EXPR, e, si);
5828 register_edge_assert_for_1 (op1, EQ_EXPR, e, si);
5834 /* Determine whether the outgoing edges of BB should receive an
5835 ASSERT_EXPR for each of the operands of BB's LAST statement.
5836 The last statement of BB must be a COND_EXPR.
5838 If any of the sub-graphs rooted at BB have an interesting use of
5839 the predicate operands, an assert location node is added to the
5840 list of assertions for the corresponding operands. */
5842 static void
5843 find_conditional_asserts (basic_block bb, gcond *last)
5845 gimple_stmt_iterator bsi;
5846 tree op;
5847 edge_iterator ei;
5848 edge e;
5849 ssa_op_iter iter;
5851 bsi = gsi_for_stmt (last);
5853 /* Look for uses of the operands in each of the sub-graphs
5854 rooted at BB. We need to check each of the outgoing edges
5855 separately, so that we know what kind of ASSERT_EXPR to
5856 insert. */
5857 FOR_EACH_EDGE (e, ei, bb->succs)
5859 if (e->dest == bb)
5860 continue;
5862 /* Register the necessary assertions for each operand in the
5863 conditional predicate. */
5864 FOR_EACH_SSA_TREE_OPERAND (op, last, iter, SSA_OP_USE)
5865 register_edge_assert_for (op, e, bsi,
5866 gimple_cond_code (last),
5867 gimple_cond_lhs (last),
5868 gimple_cond_rhs (last));
5872 struct case_info
5874 tree expr;
5875 basic_block bb;
5878 /* Compare two case labels sorting first by the destination bb index
5879 and then by the case value. */
5881 static int
5882 compare_case_labels (const void *p1, const void *p2)
5884 const struct case_info *ci1 = (const struct case_info *) p1;
5885 const struct case_info *ci2 = (const struct case_info *) p2;
5886 int idx1 = ci1->bb->index;
5887 int idx2 = ci2->bb->index;
5889 if (idx1 < idx2)
5890 return -1;
5891 else if (idx1 == idx2)
5893 /* Make sure the default label is first in a group. */
5894 if (!CASE_LOW (ci1->expr))
5895 return -1;
5896 else if (!CASE_LOW (ci2->expr))
5897 return 1;
5898 else
5899 return tree_int_cst_compare (CASE_LOW (ci1->expr),
5900 CASE_LOW (ci2->expr));
5902 else
5903 return 1;
5906 /* Determine whether the outgoing edges of BB should receive an
5907 ASSERT_EXPR for each of the operands of BB's LAST statement.
5908 The last statement of BB must be a SWITCH_EXPR.
5910 If any of the sub-graphs rooted at BB have an interesting use of
5911 the predicate operands, an assert location node is added to the
5912 list of assertions for the corresponding operands. */
5914 static void
5915 find_switch_asserts (basic_block bb, gswitch *last)
5917 gimple_stmt_iterator bsi;
5918 tree op;
5919 edge e;
5920 struct case_info *ci;
5921 size_t n = gimple_switch_num_labels (last);
5922 #if GCC_VERSION >= 4000
5923 unsigned int idx;
5924 #else
5925 /* Work around GCC 3.4 bug (PR 37086). */
5926 volatile unsigned int idx;
5927 #endif
5929 bsi = gsi_for_stmt (last);
5930 op = gimple_switch_index (last);
5931 if (TREE_CODE (op) != SSA_NAME)
5932 return;
5934 /* Build a vector of case labels sorted by destination label. */
5935 ci = XNEWVEC (struct case_info, n);
5936 for (idx = 0; idx < n; ++idx)
5938 ci[idx].expr = gimple_switch_label (last, idx);
5939 ci[idx].bb = label_to_block (CASE_LABEL (ci[idx].expr));
5941 edge default_edge = find_edge (bb, ci[0].bb);
5942 qsort (ci, n, sizeof (struct case_info), compare_case_labels);
5944 for (idx = 0; idx < n; ++idx)
5946 tree min, max;
5947 tree cl = ci[idx].expr;
5948 basic_block cbb = ci[idx].bb;
5950 min = CASE_LOW (cl);
5951 max = CASE_HIGH (cl);
5953 /* If there are multiple case labels with the same destination
5954 we need to combine them to a single value range for the edge. */
5955 if (idx + 1 < n && cbb == ci[idx + 1].bb)
5957 /* Skip labels until the last of the group. */
5958 do {
5959 ++idx;
5960 } while (idx < n && cbb == ci[idx].bb);
5961 --idx;
5963 /* Pick up the maximum of the case label range. */
5964 if (CASE_HIGH (ci[idx].expr))
5965 max = CASE_HIGH (ci[idx].expr);
5966 else
5967 max = CASE_LOW (ci[idx].expr);
5970 /* Can't extract a useful assertion out of a range that includes the
5971 default label. */
5972 if (min == NULL_TREE)
5973 continue;
5975 /* Find the edge to register the assert expr on. */
5976 e = find_edge (bb, cbb);
5978 /* Register the necessary assertions for the operand in the
5979 SWITCH_EXPR. */
5980 register_edge_assert_for (op, e, bsi,
5981 max ? GE_EXPR : EQ_EXPR,
5982 op, fold_convert (TREE_TYPE (op), min));
5983 if (max)
5984 register_edge_assert_for (op, e, bsi, LE_EXPR, op,
5985 fold_convert (TREE_TYPE (op), max));
5988 XDELETEVEC (ci);
5990 if (!live_on_edge (default_edge, op))
5991 return;
5993 /* Now register along the default label assertions that correspond to the
5994 anti-range of each label. */
5995 int insertion_limit = PARAM_VALUE (PARAM_MAX_VRP_SWITCH_ASSERTIONS);
5996 for (idx = 1; idx < n; idx++)
5998 tree min, max;
5999 tree cl = gimple_switch_label (last, idx);
6001 min = CASE_LOW (cl);
6002 max = CASE_HIGH (cl);
6004 /* Combine contiguous case ranges to reduce the number of assertions
6005 to insert. */
6006 for (idx = idx + 1; idx < n; idx++)
6008 tree next_min, next_max;
6009 tree next_cl = gimple_switch_label (last, idx);
6011 next_min = CASE_LOW (next_cl);
6012 next_max = CASE_HIGH (next_cl);
6014 wide_int difference = wi::sub (next_min, max ? max : min);
6015 if (wi::eq_p (difference, 1))
6016 max = next_max ? next_max : next_min;
6017 else
6018 break;
6020 idx--;
6022 if (max == NULL_TREE)
6024 /* Register the assertion OP != MIN. */
6025 min = fold_convert (TREE_TYPE (op), min);
6026 register_edge_assert_for (op, default_edge, bsi, NE_EXPR, op, min);
6028 else
6030 /* Register the assertion (unsigned)OP - MIN > (MAX - MIN),
6031 which will give OP the anti-range ~[MIN,MAX]. */
6032 tree uop = fold_convert (unsigned_type_for (TREE_TYPE (op)), op);
6033 min = fold_convert (TREE_TYPE (uop), min);
6034 max = fold_convert (TREE_TYPE (uop), max);
6036 tree lhs = fold_build2 (MINUS_EXPR, TREE_TYPE (uop), uop, min);
6037 tree rhs = int_const_binop (MINUS_EXPR, max, min);
6038 register_new_assert_for (op, lhs, GT_EXPR, rhs,
6039 NULL, default_edge, bsi);
6042 if (--insertion_limit == 0)
6043 break;
6048 /* Traverse all the statements in block BB looking for statements that
6049 may generate useful assertions for the SSA names in their operand.
6050 If a statement produces a useful assertion A for name N_i, then the
6051 list of assertions already generated for N_i is scanned to
6052 determine if A is actually needed.
6054 If N_i already had the assertion A at a location dominating the
6055 current location, then nothing needs to be done. Otherwise, the
6056 new location for A is recorded instead.
6058 1- For every statement S in BB, all the variables used by S are
6059 added to bitmap FOUND_IN_SUBGRAPH.
6061 2- If statement S uses an operand N in a way that exposes a known
6062 value range for N, then if N was not already generated by an
6063 ASSERT_EXPR, create a new assert location for N. For instance,
6064 if N is a pointer and the statement dereferences it, we can
6065 assume that N is not NULL.
6067 3- COND_EXPRs are a special case of #2. We can derive range
6068 information from the predicate but need to insert different
6069 ASSERT_EXPRs for each of the sub-graphs rooted at the
6070 conditional block. If the last statement of BB is a conditional
6071 expression of the form 'X op Y', then
6073 a) Remove X and Y from the set FOUND_IN_SUBGRAPH.
6075 b) If the conditional is the only entry point to the sub-graph
6076 corresponding to the THEN_CLAUSE, recurse into it. On
6077 return, if X and/or Y are marked in FOUND_IN_SUBGRAPH, then
6078 an ASSERT_EXPR is added for the corresponding variable.
6080 c) Repeat step (b) on the ELSE_CLAUSE.
6082 d) Mark X and Y in FOUND_IN_SUBGRAPH.
6084 For instance,
6086 if (a == 9)
6087 b = a;
6088 else
6089 b = c + 1;
6091 In this case, an assertion on the THEN clause is useful to
6092 determine that 'a' is always 9 on that edge. However, an assertion
6093 on the ELSE clause would be unnecessary.
6095 4- If BB does not end in a conditional expression, then we recurse
6096 into BB's dominator children.
6098 At the end of the recursive traversal, every SSA name will have a
6099 list of locations where ASSERT_EXPRs should be added. When a new
6100 location for name N is found, it is registered by calling
6101 register_new_assert_for. That function keeps track of all the
6102 registered assertions to prevent adding unnecessary assertions.
6103 For instance, if a pointer P_4 is dereferenced more than once in a
6104 dominator tree, only the location dominating all the dereference of
6105 P_4 will receive an ASSERT_EXPR. */
6107 static void
6108 find_assert_locations_1 (basic_block bb, sbitmap live)
6110 gimple *last;
6112 last = last_stmt (bb);
6114 /* If BB's last statement is a conditional statement involving integer
6115 operands, determine if we need to add ASSERT_EXPRs. */
6116 if (last
6117 && gimple_code (last) == GIMPLE_COND
6118 && !fp_predicate (last)
6119 && !ZERO_SSA_OPERANDS (last, SSA_OP_USE))
6120 find_conditional_asserts (bb, as_a <gcond *> (last));
6122 /* If BB's last statement is a switch statement involving integer
6123 operands, determine if we need to add ASSERT_EXPRs. */
6124 if (last
6125 && gimple_code (last) == GIMPLE_SWITCH
6126 && !ZERO_SSA_OPERANDS (last, SSA_OP_USE))
6127 find_switch_asserts (bb, as_a <gswitch *> (last));
6129 /* Traverse all the statements in BB marking used names and looking
6130 for statements that may infer assertions for their used operands. */
6131 for (gimple_stmt_iterator si = gsi_last_bb (bb); !gsi_end_p (si);
6132 gsi_prev (&si))
6134 gimple *stmt;
6135 tree op;
6136 ssa_op_iter i;
6138 stmt = gsi_stmt (si);
6140 if (is_gimple_debug (stmt))
6141 continue;
6143 /* See if we can derive an assertion for any of STMT's operands. */
6144 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_USE)
6146 tree value;
6147 enum tree_code comp_code;
6149 /* If op is not live beyond this stmt, do not bother to insert
6150 asserts for it. */
6151 if (!bitmap_bit_p (live, SSA_NAME_VERSION (op)))
6152 continue;
6154 /* If OP is used in such a way that we can infer a value
6155 range for it, and we don't find a previous assertion for
6156 it, create a new assertion location node for OP. */
6157 if (infer_value_range (stmt, op, &comp_code, &value))
6159 /* If we are able to infer a nonzero value range for OP,
6160 then walk backwards through the use-def chain to see if OP
6161 was set via a typecast.
6163 If so, then we can also infer a nonzero value range
6164 for the operand of the NOP_EXPR. */
6165 if (comp_code == NE_EXPR && integer_zerop (value))
6167 tree t = op;
6168 gimple *def_stmt = SSA_NAME_DEF_STMT (t);
6170 while (is_gimple_assign (def_stmt)
6171 && CONVERT_EXPR_CODE_P
6172 (gimple_assign_rhs_code (def_stmt))
6173 && TREE_CODE
6174 (gimple_assign_rhs1 (def_stmt)) == SSA_NAME
6175 && POINTER_TYPE_P
6176 (TREE_TYPE (gimple_assign_rhs1 (def_stmt))))
6178 t = gimple_assign_rhs1 (def_stmt);
6179 def_stmt = SSA_NAME_DEF_STMT (t);
6181 /* Note we want to register the assert for the
6182 operand of the NOP_EXPR after SI, not after the
6183 conversion. */
6184 if (bitmap_bit_p (live, SSA_NAME_VERSION (t)))
6185 register_new_assert_for (t, t, comp_code, value,
6186 bb, NULL, si);
6190 register_new_assert_for (op, op, comp_code, value, bb, NULL, si);
6194 /* Update live. */
6195 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_USE)
6196 bitmap_set_bit (live, SSA_NAME_VERSION (op));
6197 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_DEF)
6198 bitmap_clear_bit (live, SSA_NAME_VERSION (op));
6201 /* Traverse all PHI nodes in BB, updating live. */
6202 for (gphi_iterator si = gsi_start_phis (bb); !gsi_end_p (si);
6203 gsi_next (&si))
6205 use_operand_p arg_p;
6206 ssa_op_iter i;
6207 gphi *phi = si.phi ();
6208 tree res = gimple_phi_result (phi);
6210 if (virtual_operand_p (res))
6211 continue;
6213 FOR_EACH_PHI_ARG (arg_p, phi, i, SSA_OP_USE)
6215 tree arg = USE_FROM_PTR (arg_p);
6216 if (TREE_CODE (arg) == SSA_NAME)
6217 bitmap_set_bit (live, SSA_NAME_VERSION (arg));
6220 bitmap_clear_bit (live, SSA_NAME_VERSION (res));
6224 /* Do an RPO walk over the function computing SSA name liveness
6225 on-the-fly and deciding on assert expressions to insert. */
6227 static void
6228 find_assert_locations (void)
6230 int *rpo = XNEWVEC (int, last_basic_block_for_fn (cfun));
6231 int *bb_rpo = XNEWVEC (int, last_basic_block_for_fn (cfun));
6232 int *last_rpo = XCNEWVEC (int, last_basic_block_for_fn (cfun));
6233 int rpo_cnt, i;
6235 live = XCNEWVEC (sbitmap, last_basic_block_for_fn (cfun));
6236 rpo_cnt = pre_and_rev_post_order_compute (NULL, rpo, false);
6237 for (i = 0; i < rpo_cnt; ++i)
6238 bb_rpo[rpo[i]] = i;
6240 /* Pre-seed loop latch liveness from loop header PHI nodes. Due to
6241 the order we compute liveness and insert asserts we otherwise
6242 fail to insert asserts into the loop latch. */
6243 loop_p loop;
6244 FOR_EACH_LOOP (loop, 0)
6246 i = loop->latch->index;
6247 unsigned int j = single_succ_edge (loop->latch)->dest_idx;
6248 for (gphi_iterator gsi = gsi_start_phis (loop->header);
6249 !gsi_end_p (gsi); gsi_next (&gsi))
6251 gphi *phi = gsi.phi ();
6252 if (virtual_operand_p (gimple_phi_result (phi)))
6253 continue;
6254 tree arg = gimple_phi_arg_def (phi, j);
6255 if (TREE_CODE (arg) == SSA_NAME)
6257 if (live[i] == NULL)
6259 live[i] = sbitmap_alloc (num_ssa_names);
6260 bitmap_clear (live[i]);
6262 bitmap_set_bit (live[i], SSA_NAME_VERSION (arg));
6267 for (i = rpo_cnt - 1; i >= 0; --i)
6269 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, rpo[i]);
6270 edge e;
6271 edge_iterator ei;
6273 if (!live[rpo[i]])
6275 live[rpo[i]] = sbitmap_alloc (num_ssa_names);
6276 bitmap_clear (live[rpo[i]]);
6279 /* Process BB and update the live information with uses in
6280 this block. */
6281 find_assert_locations_1 (bb, live[rpo[i]]);
6283 /* Merge liveness into the predecessor blocks and free it. */
6284 if (!bitmap_empty_p (live[rpo[i]]))
6286 int pred_rpo = i;
6287 FOR_EACH_EDGE (e, ei, bb->preds)
6289 int pred = e->src->index;
6290 if ((e->flags & EDGE_DFS_BACK) || pred == ENTRY_BLOCK)
6291 continue;
6293 if (!live[pred])
6295 live[pred] = sbitmap_alloc (num_ssa_names);
6296 bitmap_clear (live[pred]);
6298 bitmap_ior (live[pred], live[pred], live[rpo[i]]);
6300 if (bb_rpo[pred] < pred_rpo)
6301 pred_rpo = bb_rpo[pred];
6304 /* Record the RPO number of the last visited block that needs
6305 live information from this block. */
6306 last_rpo[rpo[i]] = pred_rpo;
6308 else
6310 sbitmap_free (live[rpo[i]]);
6311 live[rpo[i]] = NULL;
6314 /* We can free all successors live bitmaps if all their
6315 predecessors have been visited already. */
6316 FOR_EACH_EDGE (e, ei, bb->succs)
6317 if (last_rpo[e->dest->index] == i
6318 && live[e->dest->index])
6320 sbitmap_free (live[e->dest->index]);
6321 live[e->dest->index] = NULL;
6325 XDELETEVEC (rpo);
6326 XDELETEVEC (bb_rpo);
6327 XDELETEVEC (last_rpo);
6328 for (i = 0; i < last_basic_block_for_fn (cfun); ++i)
6329 if (live[i])
6330 sbitmap_free (live[i]);
6331 XDELETEVEC (live);
6334 /* Create an ASSERT_EXPR for NAME and insert it in the location
6335 indicated by LOC. Return true if we made any edge insertions. */
6337 static bool
6338 process_assert_insertions_for (tree name, assert_locus *loc)
6340 /* Build the comparison expression NAME_i COMP_CODE VAL. */
6341 gimple *stmt;
6342 tree cond;
6343 gimple *assert_stmt;
6344 edge_iterator ei;
6345 edge e;
6347 /* If we have X <=> X do not insert an assert expr for that. */
6348 if (loc->expr == loc->val)
6349 return false;
6351 cond = build2 (loc->comp_code, boolean_type_node, loc->expr, loc->val);
6352 assert_stmt = build_assert_expr_for (cond, name);
6353 if (loc->e)
6355 /* We have been asked to insert the assertion on an edge. This
6356 is used only by COND_EXPR and SWITCH_EXPR assertions. */
6357 gcc_checking_assert (gimple_code (gsi_stmt (loc->si)) == GIMPLE_COND
6358 || (gimple_code (gsi_stmt (loc->si))
6359 == GIMPLE_SWITCH));
6361 gsi_insert_on_edge (loc->e, assert_stmt);
6362 return true;
6365 /* Otherwise, we can insert right after LOC->SI iff the
6366 statement must not be the last statement in the block. */
6367 stmt = gsi_stmt (loc->si);
6368 if (!stmt_ends_bb_p (stmt))
6370 gsi_insert_after (&loc->si, assert_stmt, GSI_SAME_STMT);
6371 return false;
6374 /* If STMT must be the last statement in BB, we can only insert new
6375 assertions on the non-abnormal edge out of BB. Note that since
6376 STMT is not control flow, there may only be one non-abnormal edge
6377 out of BB. */
6378 FOR_EACH_EDGE (e, ei, loc->bb->succs)
6379 if (!(e->flags & EDGE_ABNORMAL))
6381 gsi_insert_on_edge (e, assert_stmt);
6382 return true;
6385 gcc_unreachable ();
6389 /* Process all the insertions registered for every name N_i registered
6390 in NEED_ASSERT_FOR. The list of assertions to be inserted are
6391 found in ASSERTS_FOR[i]. */
6393 static void
6394 process_assert_insertions (void)
6396 unsigned i;
6397 bitmap_iterator bi;
6398 bool update_edges_p = false;
6399 int num_asserts = 0;
6401 if (dump_file && (dump_flags & TDF_DETAILS))
6402 dump_all_asserts (dump_file);
6404 EXECUTE_IF_SET_IN_BITMAP (need_assert_for, 0, i, bi)
6406 assert_locus *loc = asserts_for[i];
6407 gcc_assert (loc);
6409 while (loc)
6411 assert_locus *next = loc->next;
6412 update_edges_p |= process_assert_insertions_for (ssa_name (i), loc);
6413 free (loc);
6414 loc = next;
6415 num_asserts++;
6419 if (update_edges_p)
6420 gsi_commit_edge_inserts ();
6422 statistics_counter_event (cfun, "Number of ASSERT_EXPR expressions inserted",
6423 num_asserts);
6427 /* Traverse the flowgraph looking for conditional jumps to insert range
6428 expressions. These range expressions are meant to provide information
6429 to optimizations that need to reason in terms of value ranges. They
6430 will not be expanded into RTL. For instance, given:
6432 x = ...
6433 y = ...
6434 if (x < y)
6435 y = x - 2;
6436 else
6437 x = y + 3;
6439 this pass will transform the code into:
6441 x = ...
6442 y = ...
6443 if (x < y)
6445 x = ASSERT_EXPR <x, x < y>
6446 y = x - 2
6448 else
6450 y = ASSERT_EXPR <y, x >= y>
6451 x = y + 3
6454 The idea is that once copy and constant propagation have run, other
6455 optimizations will be able to determine what ranges of values can 'x'
6456 take in different paths of the code, simply by checking the reaching
6457 definition of 'x'. */
6459 static void
6460 insert_range_assertions (void)
6462 need_assert_for = BITMAP_ALLOC (NULL);
6463 asserts_for = XCNEWVEC (assert_locus *, num_ssa_names);
6465 calculate_dominance_info (CDI_DOMINATORS);
6467 find_assert_locations ();
6468 if (!bitmap_empty_p (need_assert_for))
6470 process_assert_insertions ();
6471 update_ssa (TODO_update_ssa_no_phi);
6474 if (dump_file && (dump_flags & TDF_DETAILS))
6476 fprintf (dump_file, "\nSSA form after inserting ASSERT_EXPRs\n");
6477 dump_function_to_file (current_function_decl, dump_file, dump_flags);
6480 free (asserts_for);
6481 BITMAP_FREE (need_assert_for);
6484 /* Checks one ARRAY_REF in REF, located at LOCUS. Ignores flexible arrays
6485 and "struct" hacks. If VRP can determine that the
6486 array subscript is a constant, check if it is outside valid
6487 range. If the array subscript is a RANGE, warn if it is
6488 non-overlapping with valid range.
6489 IGNORE_OFF_BY_ONE is true if the ARRAY_REF is inside a ADDR_EXPR. */
6491 static void
6492 check_array_ref (location_t location, tree ref, bool ignore_off_by_one)
6494 value_range *vr = NULL;
6495 tree low_sub, up_sub;
6496 tree low_bound, up_bound, up_bound_p1;
6498 if (TREE_NO_WARNING (ref))
6499 return;
6501 low_sub = up_sub = TREE_OPERAND (ref, 1);
6502 up_bound = array_ref_up_bound (ref);
6504 /* Can not check flexible arrays. */
6505 if (!up_bound
6506 || TREE_CODE (up_bound) != INTEGER_CST)
6507 return;
6509 /* Accesses to trailing arrays via pointers may access storage
6510 beyond the types array bounds. */
6511 if (warn_array_bounds < 2
6512 && array_at_struct_end_p (ref))
6513 return;
6515 low_bound = array_ref_low_bound (ref);
6516 up_bound_p1 = int_const_binop (PLUS_EXPR, up_bound,
6517 build_int_cst (TREE_TYPE (up_bound), 1));
6519 /* Empty array. */
6520 if (tree_int_cst_equal (low_bound, up_bound_p1))
6522 warning_at (location, OPT_Warray_bounds,
6523 "array subscript is above array bounds");
6524 TREE_NO_WARNING (ref) = 1;
6527 if (TREE_CODE (low_sub) == SSA_NAME)
6529 vr = get_value_range (low_sub);
6530 if (vr->type == VR_RANGE || vr->type == VR_ANTI_RANGE)
6532 low_sub = vr->type == VR_RANGE ? vr->max : vr->min;
6533 up_sub = vr->type == VR_RANGE ? vr->min : vr->max;
6537 if (vr && vr->type == VR_ANTI_RANGE)
6539 if (TREE_CODE (up_sub) == INTEGER_CST
6540 && (ignore_off_by_one
6541 ? tree_int_cst_lt (up_bound, up_sub)
6542 : tree_int_cst_le (up_bound, up_sub))
6543 && TREE_CODE (low_sub) == INTEGER_CST
6544 && tree_int_cst_le (low_sub, low_bound))
6546 warning_at (location, OPT_Warray_bounds,
6547 "array subscript is outside array bounds");
6548 TREE_NO_WARNING (ref) = 1;
6551 else if (TREE_CODE (up_sub) == INTEGER_CST
6552 && (ignore_off_by_one
6553 ? !tree_int_cst_le (up_sub, up_bound_p1)
6554 : !tree_int_cst_le (up_sub, up_bound)))
6556 if (dump_file && (dump_flags & TDF_DETAILS))
6558 fprintf (dump_file, "Array bound warning for ");
6559 dump_generic_expr (MSG_NOTE, TDF_SLIM, ref);
6560 fprintf (dump_file, "\n");
6562 warning_at (location, OPT_Warray_bounds,
6563 "array subscript is above array bounds");
6564 TREE_NO_WARNING (ref) = 1;
6566 else if (TREE_CODE (low_sub) == INTEGER_CST
6567 && tree_int_cst_lt (low_sub, low_bound))
6569 if (dump_file && (dump_flags & TDF_DETAILS))
6571 fprintf (dump_file, "Array bound warning for ");
6572 dump_generic_expr (MSG_NOTE, TDF_SLIM, ref);
6573 fprintf (dump_file, "\n");
6575 warning_at (location, OPT_Warray_bounds,
6576 "array subscript is below array bounds");
6577 TREE_NO_WARNING (ref) = 1;
6581 /* Searches if the expr T, located at LOCATION computes
6582 address of an ARRAY_REF, and call check_array_ref on it. */
6584 static void
6585 search_for_addr_array (tree t, location_t location)
6587 /* Check each ARRAY_REFs in the reference chain. */
6590 if (TREE_CODE (t) == ARRAY_REF)
6591 check_array_ref (location, t, true /*ignore_off_by_one*/);
6593 t = TREE_OPERAND (t, 0);
6595 while (handled_component_p (t));
6597 if (TREE_CODE (t) == MEM_REF
6598 && TREE_CODE (TREE_OPERAND (t, 0)) == ADDR_EXPR
6599 && !TREE_NO_WARNING (t))
6601 tree tem = TREE_OPERAND (TREE_OPERAND (t, 0), 0);
6602 tree low_bound, up_bound, el_sz;
6603 offset_int idx;
6604 if (TREE_CODE (TREE_TYPE (tem)) != ARRAY_TYPE
6605 || TREE_CODE (TREE_TYPE (TREE_TYPE (tem))) == ARRAY_TYPE
6606 || !TYPE_DOMAIN (TREE_TYPE (tem)))
6607 return;
6609 low_bound = TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (tem)));
6610 up_bound = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (tem)));
6611 el_sz = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (tem)));
6612 if (!low_bound
6613 || TREE_CODE (low_bound) != INTEGER_CST
6614 || !up_bound
6615 || TREE_CODE (up_bound) != INTEGER_CST
6616 || !el_sz
6617 || TREE_CODE (el_sz) != INTEGER_CST)
6618 return;
6620 idx = mem_ref_offset (t);
6621 idx = wi::sdiv_trunc (idx, wi::to_offset (el_sz));
6622 if (idx < 0)
6624 if (dump_file && (dump_flags & TDF_DETAILS))
6626 fprintf (dump_file, "Array bound warning for ");
6627 dump_generic_expr (MSG_NOTE, TDF_SLIM, t);
6628 fprintf (dump_file, "\n");
6630 warning_at (location, OPT_Warray_bounds,
6631 "array subscript is below array bounds");
6632 TREE_NO_WARNING (t) = 1;
6634 else if (idx > (wi::to_offset (up_bound)
6635 - wi::to_offset (low_bound) + 1))
6637 if (dump_file && (dump_flags & TDF_DETAILS))
6639 fprintf (dump_file, "Array bound warning for ");
6640 dump_generic_expr (MSG_NOTE, TDF_SLIM, t);
6641 fprintf (dump_file, "\n");
6643 warning_at (location, OPT_Warray_bounds,
6644 "array subscript is above array bounds");
6645 TREE_NO_WARNING (t) = 1;
6650 /* walk_tree() callback that checks if *TP is
6651 an ARRAY_REF inside an ADDR_EXPR (in which an array
6652 subscript one outside the valid range is allowed). Call
6653 check_array_ref for each ARRAY_REF found. The location is
6654 passed in DATA. */
6656 static tree
6657 check_array_bounds (tree *tp, int *walk_subtree, void *data)
6659 tree t = *tp;
6660 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
6661 location_t location;
6663 if (EXPR_HAS_LOCATION (t))
6664 location = EXPR_LOCATION (t);
6665 else
6667 location_t *locp = (location_t *) wi->info;
6668 location = *locp;
6671 *walk_subtree = TRUE;
6673 if (TREE_CODE (t) == ARRAY_REF)
6674 check_array_ref (location, t, false /*ignore_off_by_one*/);
6676 else if (TREE_CODE (t) == ADDR_EXPR)
6678 search_for_addr_array (t, location);
6679 *walk_subtree = FALSE;
6682 return NULL_TREE;
6685 /* Walk over all statements of all reachable BBs and call check_array_bounds
6686 on them. */
6688 static void
6689 check_all_array_refs (void)
6691 basic_block bb;
6692 gimple_stmt_iterator si;
6694 FOR_EACH_BB_FN (bb, cfun)
6696 edge_iterator ei;
6697 edge e;
6698 bool executable = false;
6700 /* Skip blocks that were found to be unreachable. */
6701 FOR_EACH_EDGE (e, ei, bb->preds)
6702 executable |= !!(e->flags & EDGE_EXECUTABLE);
6703 if (!executable)
6704 continue;
6706 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
6708 gimple *stmt = gsi_stmt (si);
6709 struct walk_stmt_info wi;
6710 if (!gimple_has_location (stmt)
6711 || is_gimple_debug (stmt))
6712 continue;
6714 memset (&wi, 0, sizeof (wi));
6716 location_t loc = gimple_location (stmt);
6717 wi.info = &loc;
6719 walk_gimple_op (gsi_stmt (si),
6720 check_array_bounds,
6721 &wi);
6726 /* Return true if all imm uses of VAR are either in STMT, or
6727 feed (optionally through a chain of single imm uses) GIMPLE_COND
6728 in basic block COND_BB. */
6730 static bool
6731 all_imm_uses_in_stmt_or_feed_cond (tree var, gimple *stmt, basic_block cond_bb)
6733 use_operand_p use_p, use2_p;
6734 imm_use_iterator iter;
6736 FOR_EACH_IMM_USE_FAST (use_p, iter, var)
6737 if (USE_STMT (use_p) != stmt)
6739 gimple *use_stmt = USE_STMT (use_p), *use_stmt2;
6740 if (is_gimple_debug (use_stmt))
6741 continue;
6742 while (is_gimple_assign (use_stmt)
6743 && TREE_CODE (gimple_assign_lhs (use_stmt)) == SSA_NAME
6744 && single_imm_use (gimple_assign_lhs (use_stmt),
6745 &use2_p, &use_stmt2))
6746 use_stmt = use_stmt2;
6747 if (gimple_code (use_stmt) != GIMPLE_COND
6748 || gimple_bb (use_stmt) != cond_bb)
6749 return false;
6751 return true;
6754 /* Handle
6755 _4 = x_3 & 31;
6756 if (_4 != 0)
6757 goto <bb 6>;
6758 else
6759 goto <bb 7>;
6760 <bb 6>:
6761 __builtin_unreachable ();
6762 <bb 7>:
6763 x_5 = ASSERT_EXPR <x_3, ...>;
6764 If x_3 has no other immediate uses (checked by caller),
6765 var is the x_3 var from ASSERT_EXPR, we can clear low 5 bits
6766 from the non-zero bitmask. */
6768 static void
6769 maybe_set_nonzero_bits (basic_block bb, tree var)
6771 edge e = single_pred_edge (bb);
6772 basic_block cond_bb = e->src;
6773 gimple *stmt = last_stmt (cond_bb);
6774 tree cst;
6776 if (stmt == NULL
6777 || gimple_code (stmt) != GIMPLE_COND
6778 || gimple_cond_code (stmt) != ((e->flags & EDGE_TRUE_VALUE)
6779 ? EQ_EXPR : NE_EXPR)
6780 || TREE_CODE (gimple_cond_lhs (stmt)) != SSA_NAME
6781 || !integer_zerop (gimple_cond_rhs (stmt)))
6782 return;
6784 stmt = SSA_NAME_DEF_STMT (gimple_cond_lhs (stmt));
6785 if (!is_gimple_assign (stmt)
6786 || gimple_assign_rhs_code (stmt) != BIT_AND_EXPR
6787 || TREE_CODE (gimple_assign_rhs2 (stmt)) != INTEGER_CST)
6788 return;
6789 if (gimple_assign_rhs1 (stmt) != var)
6791 gimple *stmt2;
6793 if (TREE_CODE (gimple_assign_rhs1 (stmt)) != SSA_NAME)
6794 return;
6795 stmt2 = SSA_NAME_DEF_STMT (gimple_assign_rhs1 (stmt));
6796 if (!gimple_assign_cast_p (stmt2)
6797 || gimple_assign_rhs1 (stmt2) != var
6798 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (stmt2))
6799 || (TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (stmt)))
6800 != TYPE_PRECISION (TREE_TYPE (var))))
6801 return;
6803 cst = gimple_assign_rhs2 (stmt);
6804 set_nonzero_bits (var, wi::bit_and_not (get_nonzero_bits (var), cst));
6807 /* Convert range assertion expressions into the implied copies and
6808 copy propagate away the copies. Doing the trivial copy propagation
6809 here avoids the need to run the full copy propagation pass after
6810 VRP.
6812 FIXME, this will eventually lead to copy propagation removing the
6813 names that had useful range information attached to them. For
6814 instance, if we had the assertion N_i = ASSERT_EXPR <N_j, N_j > 3>,
6815 then N_i will have the range [3, +INF].
6817 However, by converting the assertion into the implied copy
6818 operation N_i = N_j, we will then copy-propagate N_j into the uses
6819 of N_i and lose the range information. We may want to hold on to
6820 ASSERT_EXPRs a little while longer as the ranges could be used in
6821 things like jump threading.
6823 The problem with keeping ASSERT_EXPRs around is that passes after
6824 VRP need to handle them appropriately.
6826 Another approach would be to make the range information a first
6827 class property of the SSA_NAME so that it can be queried from
6828 any pass. This is made somewhat more complex by the need for
6829 multiple ranges to be associated with one SSA_NAME. */
6831 static void
6832 remove_range_assertions (void)
6834 basic_block bb;
6835 gimple_stmt_iterator si;
6836 /* 1 if looking at ASSERT_EXPRs immediately at the beginning of
6837 a basic block preceeded by GIMPLE_COND branching to it and
6838 __builtin_trap, -1 if not yet checked, 0 otherwise. */
6839 int is_unreachable;
6841 /* Note that the BSI iterator bump happens at the bottom of the
6842 loop and no bump is necessary if we're removing the statement
6843 referenced by the current BSI. */
6844 FOR_EACH_BB_FN (bb, cfun)
6845 for (si = gsi_after_labels (bb), is_unreachable = -1; !gsi_end_p (si);)
6847 gimple *stmt = gsi_stmt (si);
6848 gimple *use_stmt;
6850 if (is_gimple_assign (stmt)
6851 && gimple_assign_rhs_code (stmt) == ASSERT_EXPR)
6853 tree lhs = gimple_assign_lhs (stmt);
6854 tree rhs = gimple_assign_rhs1 (stmt);
6855 tree var;
6856 use_operand_p use_p;
6857 imm_use_iterator iter;
6859 var = ASSERT_EXPR_VAR (rhs);
6860 gcc_assert (TREE_CODE (var) == SSA_NAME);
6862 if (!POINTER_TYPE_P (TREE_TYPE (lhs))
6863 && SSA_NAME_RANGE_INFO (lhs))
6865 if (is_unreachable == -1)
6867 is_unreachable = 0;
6868 if (single_pred_p (bb)
6869 && assert_unreachable_fallthru_edge_p
6870 (single_pred_edge (bb)))
6871 is_unreachable = 1;
6873 /* Handle
6874 if (x_7 >= 10 && x_7 < 20)
6875 __builtin_unreachable ();
6876 x_8 = ASSERT_EXPR <x_7, ...>;
6877 if the only uses of x_7 are in the ASSERT_EXPR and
6878 in the condition. In that case, we can copy the
6879 range info from x_8 computed in this pass also
6880 for x_7. */
6881 if (is_unreachable
6882 && all_imm_uses_in_stmt_or_feed_cond (var, stmt,
6883 single_pred (bb)))
6885 set_range_info (var, SSA_NAME_RANGE_TYPE (lhs),
6886 SSA_NAME_RANGE_INFO (lhs)->get_min (),
6887 SSA_NAME_RANGE_INFO (lhs)->get_max ());
6888 maybe_set_nonzero_bits (bb, var);
6892 /* Propagate the RHS into every use of the LHS. */
6893 FOR_EACH_IMM_USE_STMT (use_stmt, iter, lhs)
6894 FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
6895 SET_USE (use_p, var);
6897 /* And finally, remove the copy, it is not needed. */
6898 gsi_remove (&si, true);
6899 release_defs (stmt);
6901 else
6903 if (!is_gimple_debug (gsi_stmt (si)))
6904 is_unreachable = 0;
6905 gsi_next (&si);
6911 /* Return true if STMT is interesting for VRP. */
6913 static bool
6914 stmt_interesting_for_vrp (gimple *stmt)
6916 if (gimple_code (stmt) == GIMPLE_PHI)
6918 tree res = gimple_phi_result (stmt);
6919 return (!virtual_operand_p (res)
6920 && (INTEGRAL_TYPE_P (TREE_TYPE (res))
6921 || POINTER_TYPE_P (TREE_TYPE (res))));
6923 else if (is_gimple_assign (stmt) || is_gimple_call (stmt))
6925 tree lhs = gimple_get_lhs (stmt);
6927 /* In general, assignments with virtual operands are not useful
6928 for deriving ranges, with the obvious exception of calls to
6929 builtin functions. */
6930 if (lhs && TREE_CODE (lhs) == SSA_NAME
6931 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs))
6932 || POINTER_TYPE_P (TREE_TYPE (lhs)))
6933 && (is_gimple_call (stmt)
6934 || !gimple_vuse (stmt)))
6935 return true;
6936 else if (is_gimple_call (stmt) && gimple_call_internal_p (stmt))
6937 switch (gimple_call_internal_fn (stmt))
6939 case IFN_ADD_OVERFLOW:
6940 case IFN_SUB_OVERFLOW:
6941 case IFN_MUL_OVERFLOW:
6942 /* These internal calls return _Complex integer type,
6943 but are interesting to VRP nevertheless. */
6944 if (lhs && TREE_CODE (lhs) == SSA_NAME)
6945 return true;
6946 break;
6947 default:
6948 break;
6951 else if (gimple_code (stmt) == GIMPLE_COND
6952 || gimple_code (stmt) == GIMPLE_SWITCH)
6953 return true;
6955 return false;
6959 /* Initialize local data structures for VRP. */
6961 static void
6962 vrp_initialize (void)
6964 basic_block bb;
6966 values_propagated = false;
6967 num_vr_values = num_ssa_names;
6968 vr_value = XCNEWVEC (value_range *, num_vr_values);
6969 vr_phi_edge_counts = XCNEWVEC (int, num_ssa_names);
6971 FOR_EACH_BB_FN (bb, cfun)
6973 for (gphi_iterator si = gsi_start_phis (bb); !gsi_end_p (si);
6974 gsi_next (&si))
6976 gphi *phi = si.phi ();
6977 if (!stmt_interesting_for_vrp (phi))
6979 tree lhs = PHI_RESULT (phi);
6980 set_value_range_to_varying (get_value_range (lhs));
6981 prop_set_simulate_again (phi, false);
6983 else
6984 prop_set_simulate_again (phi, true);
6987 for (gimple_stmt_iterator si = gsi_start_bb (bb); !gsi_end_p (si);
6988 gsi_next (&si))
6990 gimple *stmt = gsi_stmt (si);
6992 /* If the statement is a control insn, then we do not
6993 want to avoid simulating the statement once. Failure
6994 to do so means that those edges will never get added. */
6995 if (stmt_ends_bb_p (stmt))
6996 prop_set_simulate_again (stmt, true);
6997 else if (!stmt_interesting_for_vrp (stmt))
6999 ssa_op_iter i;
7000 tree def;
7001 FOR_EACH_SSA_TREE_OPERAND (def, stmt, i, SSA_OP_DEF)
7002 set_value_range_to_varying (get_value_range (def));
7003 prop_set_simulate_again (stmt, false);
7005 else
7006 prop_set_simulate_again (stmt, true);
7011 /* Return the singleton value-range for NAME or NAME. */
7013 static inline tree
7014 vrp_valueize (tree name)
7016 if (TREE_CODE (name) == SSA_NAME)
7018 value_range *vr = get_value_range (name);
7019 if (vr->type == VR_RANGE
7020 && (vr->min == vr->max
7021 || operand_equal_p (vr->min, vr->max, 0)))
7022 return vr->min;
7024 return name;
7027 /* Return the singleton value-range for NAME if that is a constant
7028 but signal to not follow SSA edges. */
7030 static inline tree
7031 vrp_valueize_1 (tree name)
7033 if (TREE_CODE (name) == SSA_NAME)
7035 /* If the definition may be simulated again we cannot follow
7036 this SSA edge as the SSA propagator does not necessarily
7037 re-visit the use. */
7038 gimple *def_stmt = SSA_NAME_DEF_STMT (name);
7039 if (!gimple_nop_p (def_stmt)
7040 && prop_simulate_again_p (def_stmt))
7041 return NULL_TREE;
7042 value_range *vr = get_value_range (name);
7043 if (range_int_cst_singleton_p (vr))
7044 return vr->min;
7046 return name;
7049 /* Visit assignment STMT. If it produces an interesting range, record
7050 the SSA name in *OUTPUT_P. */
7052 static enum ssa_prop_result
7053 vrp_visit_assignment_or_call (gimple *stmt, tree *output_p)
7055 tree def, lhs;
7056 ssa_op_iter iter;
7057 enum gimple_code code = gimple_code (stmt);
7058 lhs = gimple_get_lhs (stmt);
7060 /* We only keep track of ranges in integral and pointer types. */
7061 if (TREE_CODE (lhs) == SSA_NAME
7062 && ((INTEGRAL_TYPE_P (TREE_TYPE (lhs))
7063 /* It is valid to have NULL MIN/MAX values on a type. See
7064 build_range_type. */
7065 && TYPE_MIN_VALUE (TREE_TYPE (lhs))
7066 && TYPE_MAX_VALUE (TREE_TYPE (lhs)))
7067 || POINTER_TYPE_P (TREE_TYPE (lhs))))
7069 value_range new_vr = VR_INITIALIZER;
7071 /* Try folding the statement to a constant first. */
7072 tree tem = gimple_fold_stmt_to_constant_1 (stmt, vrp_valueize,
7073 vrp_valueize_1);
7074 if (tem && is_gimple_min_invariant (tem))
7075 set_value_range_to_value (&new_vr, tem, NULL);
7076 /* Then dispatch to value-range extracting functions. */
7077 else if (code == GIMPLE_CALL)
7078 extract_range_basic (&new_vr, stmt);
7079 else
7080 extract_range_from_assignment (&new_vr, as_a <gassign *> (stmt));
7082 if (update_value_range (lhs, &new_vr))
7084 *output_p = lhs;
7086 if (dump_file && (dump_flags & TDF_DETAILS))
7088 fprintf (dump_file, "Found new range for ");
7089 print_generic_expr (dump_file, lhs, 0);
7090 fprintf (dump_file, ": ");
7091 dump_value_range (dump_file, &new_vr);
7092 fprintf (dump_file, "\n");
7095 if (new_vr.type == VR_VARYING)
7096 return SSA_PROP_VARYING;
7098 return SSA_PROP_INTERESTING;
7101 return SSA_PROP_NOT_INTERESTING;
7103 else if (is_gimple_call (stmt) && gimple_call_internal_p (stmt))
7104 switch (gimple_call_internal_fn (stmt))
7106 case IFN_ADD_OVERFLOW:
7107 case IFN_SUB_OVERFLOW:
7108 case IFN_MUL_OVERFLOW:
7109 /* These internal calls return _Complex integer type,
7110 which VRP does not track, but the immediate uses
7111 thereof might be interesting. */
7112 if (lhs && TREE_CODE (lhs) == SSA_NAME)
7114 imm_use_iterator iter;
7115 use_operand_p use_p;
7116 enum ssa_prop_result res = SSA_PROP_VARYING;
7118 set_value_range_to_varying (get_value_range (lhs));
7120 FOR_EACH_IMM_USE_FAST (use_p, iter, lhs)
7122 gimple *use_stmt = USE_STMT (use_p);
7123 if (!is_gimple_assign (use_stmt))
7124 continue;
7125 enum tree_code rhs_code = gimple_assign_rhs_code (use_stmt);
7126 if (rhs_code != REALPART_EXPR && rhs_code != IMAGPART_EXPR)
7127 continue;
7128 tree rhs1 = gimple_assign_rhs1 (use_stmt);
7129 tree use_lhs = gimple_assign_lhs (use_stmt);
7130 if (TREE_CODE (rhs1) != rhs_code
7131 || TREE_OPERAND (rhs1, 0) != lhs
7132 || TREE_CODE (use_lhs) != SSA_NAME
7133 || !stmt_interesting_for_vrp (use_stmt)
7134 || (!INTEGRAL_TYPE_P (TREE_TYPE (use_lhs))
7135 || !TYPE_MIN_VALUE (TREE_TYPE (use_lhs))
7136 || !TYPE_MAX_VALUE (TREE_TYPE (use_lhs))))
7137 continue;
7139 /* If there is a change in the value range for any of the
7140 REALPART_EXPR/IMAGPART_EXPR immediate uses, return
7141 SSA_PROP_INTERESTING. If there are any REALPART_EXPR
7142 or IMAGPART_EXPR immediate uses, but none of them have
7143 a change in their value ranges, return
7144 SSA_PROP_NOT_INTERESTING. If there are no
7145 {REAL,IMAG}PART_EXPR uses at all,
7146 return SSA_PROP_VARYING. */
7147 value_range new_vr = VR_INITIALIZER;
7148 extract_range_basic (&new_vr, use_stmt);
7149 value_range *old_vr = get_value_range (use_lhs);
7150 if (old_vr->type != new_vr.type
7151 || !vrp_operand_equal_p (old_vr->min, new_vr.min)
7152 || !vrp_operand_equal_p (old_vr->max, new_vr.max)
7153 || !vrp_bitmap_equal_p (old_vr->equiv, new_vr.equiv))
7154 res = SSA_PROP_INTERESTING;
7155 else
7156 res = SSA_PROP_NOT_INTERESTING;
7157 BITMAP_FREE (new_vr.equiv);
7158 if (res == SSA_PROP_INTERESTING)
7160 *output_p = lhs;
7161 return res;
7165 return res;
7167 break;
7168 default:
7169 break;
7172 /* Every other statement produces no useful ranges. */
7173 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_DEF)
7174 set_value_range_to_varying (get_value_range (def));
7176 return SSA_PROP_VARYING;
7179 /* Helper that gets the value range of the SSA_NAME with version I
7180 or a symbolic range containing the SSA_NAME only if the value range
7181 is varying or undefined. */
7183 static inline value_range
7184 get_vr_for_comparison (int i)
7186 value_range vr = *get_value_range (ssa_name (i));
7188 /* If name N_i does not have a valid range, use N_i as its own
7189 range. This allows us to compare against names that may
7190 have N_i in their ranges. */
7191 if (vr.type == VR_VARYING || vr.type == VR_UNDEFINED)
7193 vr.type = VR_RANGE;
7194 vr.min = ssa_name (i);
7195 vr.max = ssa_name (i);
7198 return vr;
7201 /* Compare all the value ranges for names equivalent to VAR with VAL
7202 using comparison code COMP. Return the same value returned by
7203 compare_range_with_value, including the setting of
7204 *STRICT_OVERFLOW_P. */
7206 static tree
7207 compare_name_with_value (enum tree_code comp, tree var, tree val,
7208 bool *strict_overflow_p, bool use_equiv_p)
7210 bitmap_iterator bi;
7211 unsigned i;
7212 bitmap e;
7213 tree retval, t;
7214 int used_strict_overflow;
7215 bool sop;
7216 value_range equiv_vr;
7218 /* Get the set of equivalences for VAR. */
7219 e = get_value_range (var)->equiv;
7221 /* Start at -1. Set it to 0 if we do a comparison without relying
7222 on overflow, or 1 if all comparisons rely on overflow. */
7223 used_strict_overflow = -1;
7225 /* Compare vars' value range with val. */
7226 equiv_vr = get_vr_for_comparison (SSA_NAME_VERSION (var));
7227 sop = false;
7228 retval = compare_range_with_value (comp, &equiv_vr, val, &sop);
7229 if (retval)
7230 used_strict_overflow = sop ? 1 : 0;
7232 /* If the equiv set is empty we have done all work we need to do. */
7233 if (e == NULL)
7235 if (retval
7236 && used_strict_overflow > 0)
7237 *strict_overflow_p = true;
7238 return retval;
7241 EXECUTE_IF_SET_IN_BITMAP (e, 0, i, bi)
7243 if (! use_equiv_p
7244 && ! SSA_NAME_IS_DEFAULT_DEF (ssa_name (i))
7245 && prop_simulate_again_p (SSA_NAME_DEF_STMT (ssa_name (i))))
7246 continue;
7248 equiv_vr = get_vr_for_comparison (i);
7249 sop = false;
7250 t = compare_range_with_value (comp, &equiv_vr, val, &sop);
7251 if (t)
7253 /* If we get different answers from different members
7254 of the equivalence set this check must be in a dead
7255 code region. Folding it to a trap representation
7256 would be correct here. For now just return don't-know. */
7257 if (retval != NULL
7258 && t != retval)
7260 retval = NULL_TREE;
7261 break;
7263 retval = t;
7265 if (!sop)
7266 used_strict_overflow = 0;
7267 else if (used_strict_overflow < 0)
7268 used_strict_overflow = 1;
7272 if (retval
7273 && used_strict_overflow > 0)
7274 *strict_overflow_p = true;
7276 return retval;
7280 /* Given a comparison code COMP and names N1 and N2, compare all the
7281 ranges equivalent to N1 against all the ranges equivalent to N2
7282 to determine the value of N1 COMP N2. Return the same value
7283 returned by compare_ranges. Set *STRICT_OVERFLOW_P to indicate
7284 whether we relied on an overflow infinity in the comparison. */
7287 static tree
7288 compare_names (enum tree_code comp, tree n1, tree n2,
7289 bool *strict_overflow_p)
7291 tree t, retval;
7292 bitmap e1, e2;
7293 bitmap_iterator bi1, bi2;
7294 unsigned i1, i2;
7295 int used_strict_overflow;
7296 static bitmap_obstack *s_obstack = NULL;
7297 static bitmap s_e1 = NULL, s_e2 = NULL;
7299 /* Compare the ranges of every name equivalent to N1 against the
7300 ranges of every name equivalent to N2. */
7301 e1 = get_value_range (n1)->equiv;
7302 e2 = get_value_range (n2)->equiv;
7304 /* Use the fake bitmaps if e1 or e2 are not available. */
7305 if (s_obstack == NULL)
7307 s_obstack = XNEW (bitmap_obstack);
7308 bitmap_obstack_initialize (s_obstack);
7309 s_e1 = BITMAP_ALLOC (s_obstack);
7310 s_e2 = BITMAP_ALLOC (s_obstack);
7312 if (e1 == NULL)
7313 e1 = s_e1;
7314 if (e2 == NULL)
7315 e2 = s_e2;
7317 /* Add N1 and N2 to their own set of equivalences to avoid
7318 duplicating the body of the loop just to check N1 and N2
7319 ranges. */
7320 bitmap_set_bit (e1, SSA_NAME_VERSION (n1));
7321 bitmap_set_bit (e2, SSA_NAME_VERSION (n2));
7323 /* If the equivalence sets have a common intersection, then the two
7324 names can be compared without checking their ranges. */
7325 if (bitmap_intersect_p (e1, e2))
7327 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
7328 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
7330 return (comp == EQ_EXPR || comp == GE_EXPR || comp == LE_EXPR)
7331 ? boolean_true_node
7332 : boolean_false_node;
7335 /* Start at -1. Set it to 0 if we do a comparison without relying
7336 on overflow, or 1 if all comparisons rely on overflow. */
7337 used_strict_overflow = -1;
7339 /* Otherwise, compare all the equivalent ranges. First, add N1 and
7340 N2 to their own set of equivalences to avoid duplicating the body
7341 of the loop just to check N1 and N2 ranges. */
7342 EXECUTE_IF_SET_IN_BITMAP (e1, 0, i1, bi1)
7344 value_range vr1 = get_vr_for_comparison (i1);
7346 t = retval = NULL_TREE;
7347 EXECUTE_IF_SET_IN_BITMAP (e2, 0, i2, bi2)
7349 bool sop = false;
7351 value_range vr2 = get_vr_for_comparison (i2);
7353 t = compare_ranges (comp, &vr1, &vr2, &sop);
7354 if (t)
7356 /* If we get different answers from different members
7357 of the equivalence set this check must be in a dead
7358 code region. Folding it to a trap representation
7359 would be correct here. For now just return don't-know. */
7360 if (retval != NULL
7361 && t != retval)
7363 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
7364 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
7365 return NULL_TREE;
7367 retval = t;
7369 if (!sop)
7370 used_strict_overflow = 0;
7371 else if (used_strict_overflow < 0)
7372 used_strict_overflow = 1;
7376 if (retval)
7378 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
7379 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
7380 if (used_strict_overflow > 0)
7381 *strict_overflow_p = true;
7382 return retval;
7386 /* None of the equivalent ranges are useful in computing this
7387 comparison. */
7388 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
7389 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
7390 return NULL_TREE;
7393 /* Helper function for vrp_evaluate_conditional_warnv & other
7394 optimizers. */
7396 static tree
7397 vrp_evaluate_conditional_warnv_with_ops_using_ranges (enum tree_code code,
7398 tree op0, tree op1,
7399 bool * strict_overflow_p)
7401 value_range *vr0, *vr1;
7403 vr0 = (TREE_CODE (op0) == SSA_NAME) ? get_value_range (op0) : NULL;
7404 vr1 = (TREE_CODE (op1) == SSA_NAME) ? get_value_range (op1) : NULL;
7406 tree res = NULL_TREE;
7407 if (vr0 && vr1)
7408 res = compare_ranges (code, vr0, vr1, strict_overflow_p);
7409 if (!res && vr0)
7410 res = compare_range_with_value (code, vr0, op1, strict_overflow_p);
7411 if (!res && vr1)
7412 res = (compare_range_with_value
7413 (swap_tree_comparison (code), vr1, op0, strict_overflow_p));
7414 return res;
7417 /* Helper function for vrp_evaluate_conditional_warnv. */
7419 static tree
7420 vrp_evaluate_conditional_warnv_with_ops (enum tree_code code, tree op0,
7421 tree op1, bool use_equiv_p,
7422 bool *strict_overflow_p, bool *only_ranges)
7424 tree ret;
7425 if (only_ranges)
7426 *only_ranges = true;
7428 /* We only deal with integral and pointer types. */
7429 if (!INTEGRAL_TYPE_P (TREE_TYPE (op0))
7430 && !POINTER_TYPE_P (TREE_TYPE (op0)))
7431 return NULL_TREE;
7433 if ((ret = vrp_evaluate_conditional_warnv_with_ops_using_ranges
7434 (code, op0, op1, strict_overflow_p)))
7435 return ret;
7436 if (only_ranges)
7437 *only_ranges = false;
7438 /* Do not use compare_names during propagation, it's quadratic. */
7439 if (TREE_CODE (op0) == SSA_NAME && TREE_CODE (op1) == SSA_NAME
7440 && use_equiv_p)
7441 return compare_names (code, op0, op1, strict_overflow_p);
7442 else if (TREE_CODE (op0) == SSA_NAME)
7443 return compare_name_with_value (code, op0, op1,
7444 strict_overflow_p, use_equiv_p);
7445 else if (TREE_CODE (op1) == SSA_NAME)
7446 return compare_name_with_value (swap_tree_comparison (code), op1, op0,
7447 strict_overflow_p, use_equiv_p);
7448 return NULL_TREE;
7451 /* Given (CODE OP0 OP1) within STMT, try to simplify it based on value range
7452 information. Return NULL if the conditional can not be evaluated.
7453 The ranges of all the names equivalent with the operands in COND
7454 will be used when trying to compute the value. If the result is
7455 based on undefined signed overflow, issue a warning if
7456 appropriate. */
7458 static tree
7459 vrp_evaluate_conditional (tree_code code, tree op0, tree op1, gimple *stmt)
7461 bool sop;
7462 tree ret;
7463 bool only_ranges;
7465 /* Some passes and foldings leak constants with overflow flag set
7466 into the IL. Avoid doing wrong things with these and bail out. */
7467 if ((TREE_CODE (op0) == INTEGER_CST
7468 && TREE_OVERFLOW (op0))
7469 || (TREE_CODE (op1) == INTEGER_CST
7470 && TREE_OVERFLOW (op1)))
7471 return NULL_TREE;
7473 sop = false;
7474 ret = vrp_evaluate_conditional_warnv_with_ops (code, op0, op1, true, &sop,
7475 &only_ranges);
7477 if (ret && sop)
7479 enum warn_strict_overflow_code wc;
7480 const char* warnmsg;
7482 if (is_gimple_min_invariant (ret))
7484 wc = WARN_STRICT_OVERFLOW_CONDITIONAL;
7485 warnmsg = G_("assuming signed overflow does not occur when "
7486 "simplifying conditional to constant");
7488 else
7490 wc = WARN_STRICT_OVERFLOW_COMPARISON;
7491 warnmsg = G_("assuming signed overflow does not occur when "
7492 "simplifying conditional");
7495 if (issue_strict_overflow_warning (wc))
7497 location_t location;
7499 if (!gimple_has_location (stmt))
7500 location = input_location;
7501 else
7502 location = gimple_location (stmt);
7503 warning_at (location, OPT_Wstrict_overflow, "%s", warnmsg);
7507 if (warn_type_limits
7508 && ret && only_ranges
7509 && TREE_CODE_CLASS (code) == tcc_comparison
7510 && TREE_CODE (op0) == SSA_NAME)
7512 /* If the comparison is being folded and the operand on the LHS
7513 is being compared against a constant value that is outside of
7514 the natural range of OP0's type, then the predicate will
7515 always fold regardless of the value of OP0. If -Wtype-limits
7516 was specified, emit a warning. */
7517 tree type = TREE_TYPE (op0);
7518 value_range *vr0 = get_value_range (op0);
7520 if (vr0->type == VR_RANGE
7521 && INTEGRAL_TYPE_P (type)
7522 && vrp_val_is_min (vr0->min)
7523 && vrp_val_is_max (vr0->max)
7524 && is_gimple_min_invariant (op1))
7526 location_t location;
7528 if (!gimple_has_location (stmt))
7529 location = input_location;
7530 else
7531 location = gimple_location (stmt);
7533 warning_at (location, OPT_Wtype_limits,
7534 integer_zerop (ret)
7535 ? G_("comparison always false "
7536 "due to limited range of data type")
7537 : G_("comparison always true "
7538 "due to limited range of data type"));
7542 return ret;
7546 /* Visit conditional statement STMT. If we can determine which edge
7547 will be taken out of STMT's basic block, record it in
7548 *TAKEN_EDGE_P and return SSA_PROP_INTERESTING. Otherwise, return
7549 SSA_PROP_VARYING. */
7551 static enum ssa_prop_result
7552 vrp_visit_cond_stmt (gcond *stmt, edge *taken_edge_p)
7554 tree val;
7555 bool sop;
7557 *taken_edge_p = NULL;
7559 if (dump_file && (dump_flags & TDF_DETAILS))
7561 tree use;
7562 ssa_op_iter i;
7564 fprintf (dump_file, "\nVisiting conditional with predicate: ");
7565 print_gimple_stmt (dump_file, stmt, 0, 0);
7566 fprintf (dump_file, "\nWith known ranges\n");
7568 FOR_EACH_SSA_TREE_OPERAND (use, stmt, i, SSA_OP_USE)
7570 fprintf (dump_file, "\t");
7571 print_generic_expr (dump_file, use, 0);
7572 fprintf (dump_file, ": ");
7573 dump_value_range (dump_file, vr_value[SSA_NAME_VERSION (use)]);
7576 fprintf (dump_file, "\n");
7579 /* Compute the value of the predicate COND by checking the known
7580 ranges of each of its operands.
7582 Note that we cannot evaluate all the equivalent ranges here
7583 because those ranges may not yet be final and with the current
7584 propagation strategy, we cannot determine when the value ranges
7585 of the names in the equivalence set have changed.
7587 For instance, given the following code fragment
7589 i_5 = PHI <8, i_13>
7591 i_14 = ASSERT_EXPR <i_5, i_5 != 0>
7592 if (i_14 == 1)
7595 Assume that on the first visit to i_14, i_5 has the temporary
7596 range [8, 8] because the second argument to the PHI function is
7597 not yet executable. We derive the range ~[0, 0] for i_14 and the
7598 equivalence set { i_5 }. So, when we visit 'if (i_14 == 1)' for
7599 the first time, since i_14 is equivalent to the range [8, 8], we
7600 determine that the predicate is always false.
7602 On the next round of propagation, i_13 is determined to be
7603 VARYING, which causes i_5 to drop down to VARYING. So, another
7604 visit to i_14 is scheduled. In this second visit, we compute the
7605 exact same range and equivalence set for i_14, namely ~[0, 0] and
7606 { i_5 }. But we did not have the previous range for i_5
7607 registered, so vrp_visit_assignment thinks that the range for
7608 i_14 has not changed. Therefore, the predicate 'if (i_14 == 1)'
7609 is not visited again, which stops propagation from visiting
7610 statements in the THEN clause of that if().
7612 To properly fix this we would need to keep the previous range
7613 value for the names in the equivalence set. This way we would've
7614 discovered that from one visit to the other i_5 changed from
7615 range [8, 8] to VR_VARYING.
7617 However, fixing this apparent limitation may not be worth the
7618 additional checking. Testing on several code bases (GCC, DLV,
7619 MICO, TRAMP3D and SPEC2000) showed that doing this results in
7620 4 more predicates folded in SPEC. */
7621 sop = false;
7623 val = vrp_evaluate_conditional_warnv_with_ops (gimple_cond_code (stmt),
7624 gimple_cond_lhs (stmt),
7625 gimple_cond_rhs (stmt),
7626 false, &sop, NULL);
7627 if (val)
7629 if (!sop)
7630 *taken_edge_p = find_taken_edge (gimple_bb (stmt), val);
7631 else
7633 if (dump_file && (dump_flags & TDF_DETAILS))
7634 fprintf (dump_file,
7635 "\nIgnoring predicate evaluation because "
7636 "it assumes that signed overflow is undefined");
7637 val = NULL_TREE;
7641 if (dump_file && (dump_flags & TDF_DETAILS))
7643 fprintf (dump_file, "\nPredicate evaluates to: ");
7644 if (val == NULL_TREE)
7645 fprintf (dump_file, "DON'T KNOW\n");
7646 else
7647 print_generic_stmt (dump_file, val, 0);
7650 return (*taken_edge_p) ? SSA_PROP_INTERESTING : SSA_PROP_VARYING;
7653 /* Searches the case label vector VEC for the index *IDX of the CASE_LABEL
7654 that includes the value VAL. The search is restricted to the range
7655 [START_IDX, n - 1] where n is the size of VEC.
7657 If there is a CASE_LABEL for VAL, its index is placed in IDX and true is
7658 returned.
7660 If there is no CASE_LABEL for VAL and there is one that is larger than VAL,
7661 it is placed in IDX and false is returned.
7663 If VAL is larger than any CASE_LABEL, n is placed on IDX and false is
7664 returned. */
7666 static bool
7667 find_case_label_index (gswitch *stmt, size_t start_idx, tree val, size_t *idx)
7669 size_t n = gimple_switch_num_labels (stmt);
7670 size_t low, high;
7672 /* Find case label for minimum of the value range or the next one.
7673 At each iteration we are searching in [low, high - 1]. */
7675 for (low = start_idx, high = n; high != low; )
7677 tree t;
7678 int cmp;
7679 /* Note that i != high, so we never ask for n. */
7680 size_t i = (high + low) / 2;
7681 t = gimple_switch_label (stmt, i);
7683 /* Cache the result of comparing CASE_LOW and val. */
7684 cmp = tree_int_cst_compare (CASE_LOW (t), val);
7686 if (cmp == 0)
7688 /* Ranges cannot be empty. */
7689 *idx = i;
7690 return true;
7692 else if (cmp > 0)
7693 high = i;
7694 else
7696 low = i + 1;
7697 if (CASE_HIGH (t) != NULL
7698 && tree_int_cst_compare (CASE_HIGH (t), val) >= 0)
7700 *idx = i;
7701 return true;
7706 *idx = high;
7707 return false;
7710 /* Searches the case label vector VEC for the range of CASE_LABELs that is used
7711 for values between MIN and MAX. The first index is placed in MIN_IDX. The
7712 last index is placed in MAX_IDX. If the range of CASE_LABELs is empty
7713 then MAX_IDX < MIN_IDX.
7714 Returns true if the default label is not needed. */
7716 static bool
7717 find_case_label_range (gswitch *stmt, tree min, tree max, size_t *min_idx,
7718 size_t *max_idx)
7720 size_t i, j;
7721 bool min_take_default = !find_case_label_index (stmt, 1, min, &i);
7722 bool max_take_default = !find_case_label_index (stmt, i, max, &j);
7724 if (i == j
7725 && min_take_default
7726 && max_take_default)
7728 /* Only the default case label reached.
7729 Return an empty range. */
7730 *min_idx = 1;
7731 *max_idx = 0;
7732 return false;
7734 else
7736 bool take_default = min_take_default || max_take_default;
7737 tree low, high;
7738 size_t k;
7740 if (max_take_default)
7741 j--;
7743 /* If the case label range is continuous, we do not need
7744 the default case label. Verify that. */
7745 high = CASE_LOW (gimple_switch_label (stmt, i));
7746 if (CASE_HIGH (gimple_switch_label (stmt, i)))
7747 high = CASE_HIGH (gimple_switch_label (stmt, i));
7748 for (k = i + 1; k <= j; ++k)
7750 low = CASE_LOW (gimple_switch_label (stmt, k));
7751 if (!integer_onep (int_const_binop (MINUS_EXPR, low, high)))
7753 take_default = true;
7754 break;
7756 high = low;
7757 if (CASE_HIGH (gimple_switch_label (stmt, k)))
7758 high = CASE_HIGH (gimple_switch_label (stmt, k));
7761 *min_idx = i;
7762 *max_idx = j;
7763 return !take_default;
7767 /* Searches the case label vector VEC for the ranges of CASE_LABELs that are
7768 used in range VR. The indices are placed in MIN_IDX1, MAX_IDX, MIN_IDX2 and
7769 MAX_IDX2. If the ranges of CASE_LABELs are empty then MAX_IDX1 < MIN_IDX1.
7770 Returns true if the default label is not needed. */
7772 static bool
7773 find_case_label_ranges (gswitch *stmt, value_range *vr, size_t *min_idx1,
7774 size_t *max_idx1, size_t *min_idx2,
7775 size_t *max_idx2)
7777 size_t i, j, k, l;
7778 unsigned int n = gimple_switch_num_labels (stmt);
7779 bool take_default;
7780 tree case_low, case_high;
7781 tree min = vr->min, max = vr->max;
7783 gcc_checking_assert (vr->type == VR_RANGE || vr->type == VR_ANTI_RANGE);
7785 take_default = !find_case_label_range (stmt, min, max, &i, &j);
7787 /* Set second range to emtpy. */
7788 *min_idx2 = 1;
7789 *max_idx2 = 0;
7791 if (vr->type == VR_RANGE)
7793 *min_idx1 = i;
7794 *max_idx1 = j;
7795 return !take_default;
7798 /* Set first range to all case labels. */
7799 *min_idx1 = 1;
7800 *max_idx1 = n - 1;
7802 if (i > j)
7803 return false;
7805 /* Make sure all the values of case labels [i , j] are contained in
7806 range [MIN, MAX]. */
7807 case_low = CASE_LOW (gimple_switch_label (stmt, i));
7808 case_high = CASE_HIGH (gimple_switch_label (stmt, j));
7809 if (tree_int_cst_compare (case_low, min) < 0)
7810 i += 1;
7811 if (case_high != NULL_TREE
7812 && tree_int_cst_compare (max, case_high) < 0)
7813 j -= 1;
7815 if (i > j)
7816 return false;
7818 /* If the range spans case labels [i, j], the corresponding anti-range spans
7819 the labels [1, i - 1] and [j + 1, n - 1]. */
7820 k = j + 1;
7821 l = n - 1;
7822 if (k > l)
7824 k = 1;
7825 l = 0;
7828 j = i - 1;
7829 i = 1;
7830 if (i > j)
7832 i = k;
7833 j = l;
7834 k = 1;
7835 l = 0;
7838 *min_idx1 = i;
7839 *max_idx1 = j;
7840 *min_idx2 = k;
7841 *max_idx2 = l;
7842 return false;
7845 /* Visit switch statement STMT. If we can determine which edge
7846 will be taken out of STMT's basic block, record it in
7847 *TAKEN_EDGE_P and return SSA_PROP_INTERESTING. Otherwise, return
7848 SSA_PROP_VARYING. */
7850 static enum ssa_prop_result
7851 vrp_visit_switch_stmt (gswitch *stmt, edge *taken_edge_p)
7853 tree op, val;
7854 value_range *vr;
7855 size_t i = 0, j = 0, k, l;
7856 bool take_default;
7858 *taken_edge_p = NULL;
7859 op = gimple_switch_index (stmt);
7860 if (TREE_CODE (op) != SSA_NAME)
7861 return SSA_PROP_VARYING;
7863 vr = get_value_range (op);
7864 if (dump_file && (dump_flags & TDF_DETAILS))
7866 fprintf (dump_file, "\nVisiting switch expression with operand ");
7867 print_generic_expr (dump_file, op, 0);
7868 fprintf (dump_file, " with known range ");
7869 dump_value_range (dump_file, vr);
7870 fprintf (dump_file, "\n");
7873 if ((vr->type != VR_RANGE
7874 && vr->type != VR_ANTI_RANGE)
7875 || symbolic_range_p (vr))
7876 return SSA_PROP_VARYING;
7878 /* Find the single edge that is taken from the switch expression. */
7879 take_default = !find_case_label_ranges (stmt, vr, &i, &j, &k, &l);
7881 /* Check if the range spans no CASE_LABEL. If so, we only reach the default
7882 label */
7883 if (j < i)
7885 gcc_assert (take_default);
7886 val = gimple_switch_default_label (stmt);
7888 else
7890 /* Check if labels with index i to j and maybe the default label
7891 are all reaching the same label. */
7893 val = gimple_switch_label (stmt, i);
7894 if (take_default
7895 && CASE_LABEL (gimple_switch_default_label (stmt))
7896 != CASE_LABEL (val))
7898 if (dump_file && (dump_flags & TDF_DETAILS))
7899 fprintf (dump_file, " not a single destination for this "
7900 "range\n");
7901 return SSA_PROP_VARYING;
7903 for (++i; i <= j; ++i)
7905 if (CASE_LABEL (gimple_switch_label (stmt, i)) != CASE_LABEL (val))
7907 if (dump_file && (dump_flags & TDF_DETAILS))
7908 fprintf (dump_file, " not a single destination for this "
7909 "range\n");
7910 return SSA_PROP_VARYING;
7913 for (; k <= l; ++k)
7915 if (CASE_LABEL (gimple_switch_label (stmt, k)) != CASE_LABEL (val))
7917 if (dump_file && (dump_flags & TDF_DETAILS))
7918 fprintf (dump_file, " not a single destination for this "
7919 "range\n");
7920 return SSA_PROP_VARYING;
7925 *taken_edge_p = find_edge (gimple_bb (stmt),
7926 label_to_block (CASE_LABEL (val)));
7928 if (dump_file && (dump_flags & TDF_DETAILS))
7930 fprintf (dump_file, " will take edge to ");
7931 print_generic_stmt (dump_file, CASE_LABEL (val), 0);
7934 return SSA_PROP_INTERESTING;
7938 /* Evaluate statement STMT. If the statement produces a useful range,
7939 return SSA_PROP_INTERESTING and record the SSA name with the
7940 interesting range into *OUTPUT_P.
7942 If STMT is a conditional branch and we can determine its truth
7943 value, the taken edge is recorded in *TAKEN_EDGE_P.
7945 If STMT produces a varying value, return SSA_PROP_VARYING. */
7947 static enum ssa_prop_result
7948 vrp_visit_stmt (gimple *stmt, edge *taken_edge_p, tree *output_p)
7950 tree def;
7951 ssa_op_iter iter;
7953 if (dump_file && (dump_flags & TDF_DETAILS))
7955 fprintf (dump_file, "\nVisiting statement:\n");
7956 print_gimple_stmt (dump_file, stmt, 0, dump_flags);
7959 if (!stmt_interesting_for_vrp (stmt))
7960 gcc_assert (stmt_ends_bb_p (stmt));
7961 else if (is_gimple_assign (stmt) || is_gimple_call (stmt))
7962 return vrp_visit_assignment_or_call (stmt, output_p);
7963 else if (gimple_code (stmt) == GIMPLE_COND)
7964 return vrp_visit_cond_stmt (as_a <gcond *> (stmt), taken_edge_p);
7965 else if (gimple_code (stmt) == GIMPLE_SWITCH)
7966 return vrp_visit_switch_stmt (as_a <gswitch *> (stmt), taken_edge_p);
7968 /* All other statements produce nothing of interest for VRP, so mark
7969 their outputs varying and prevent further simulation. */
7970 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_DEF)
7971 set_value_range_to_varying (get_value_range (def));
7973 return SSA_PROP_VARYING;
7976 /* Union the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
7977 { VR1TYPE, VR0MIN, VR0MAX } and store the result
7978 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
7979 possible such range. The resulting range is not canonicalized. */
7981 static void
7982 union_ranges (enum value_range_type *vr0type,
7983 tree *vr0min, tree *vr0max,
7984 enum value_range_type vr1type,
7985 tree vr1min, tree vr1max)
7987 bool mineq = operand_equal_p (*vr0min, vr1min, 0);
7988 bool maxeq = operand_equal_p (*vr0max, vr1max, 0);
7990 /* [] is vr0, () is vr1 in the following classification comments. */
7991 if (mineq && maxeq)
7993 /* [( )] */
7994 if (*vr0type == vr1type)
7995 /* Nothing to do for equal ranges. */
7997 else if ((*vr0type == VR_RANGE
7998 && vr1type == VR_ANTI_RANGE)
7999 || (*vr0type == VR_ANTI_RANGE
8000 && vr1type == VR_RANGE))
8002 /* For anti-range with range union the result is varying. */
8003 goto give_up;
8005 else
8006 gcc_unreachable ();
8008 else if (operand_less_p (*vr0max, vr1min) == 1
8009 || operand_less_p (vr1max, *vr0min) == 1)
8011 /* [ ] ( ) or ( ) [ ]
8012 If the ranges have an empty intersection, result of the union
8013 operation is the anti-range or if both are anti-ranges
8014 it covers all. */
8015 if (*vr0type == VR_ANTI_RANGE
8016 && vr1type == VR_ANTI_RANGE)
8017 goto give_up;
8018 else if (*vr0type == VR_ANTI_RANGE
8019 && vr1type == VR_RANGE)
8021 else if (*vr0type == VR_RANGE
8022 && vr1type == VR_ANTI_RANGE)
8024 *vr0type = vr1type;
8025 *vr0min = vr1min;
8026 *vr0max = vr1max;
8028 else if (*vr0type == VR_RANGE
8029 && vr1type == VR_RANGE)
8031 /* The result is the convex hull of both ranges. */
8032 if (operand_less_p (*vr0max, vr1min) == 1)
8034 /* If the result can be an anti-range, create one. */
8035 if (TREE_CODE (*vr0max) == INTEGER_CST
8036 && TREE_CODE (vr1min) == INTEGER_CST
8037 && vrp_val_is_min (*vr0min)
8038 && vrp_val_is_max (vr1max))
8040 tree min = int_const_binop (PLUS_EXPR,
8041 *vr0max,
8042 build_int_cst (TREE_TYPE (*vr0max), 1));
8043 tree max = int_const_binop (MINUS_EXPR,
8044 vr1min,
8045 build_int_cst (TREE_TYPE (vr1min), 1));
8046 if (!operand_less_p (max, min))
8048 *vr0type = VR_ANTI_RANGE;
8049 *vr0min = min;
8050 *vr0max = max;
8052 else
8053 *vr0max = vr1max;
8055 else
8056 *vr0max = vr1max;
8058 else
8060 /* If the result can be an anti-range, create one. */
8061 if (TREE_CODE (vr1max) == INTEGER_CST
8062 && TREE_CODE (*vr0min) == INTEGER_CST
8063 && vrp_val_is_min (vr1min)
8064 && vrp_val_is_max (*vr0max))
8066 tree min = int_const_binop (PLUS_EXPR,
8067 vr1max,
8068 build_int_cst (TREE_TYPE (vr1max), 1));
8069 tree max = int_const_binop (MINUS_EXPR,
8070 *vr0min,
8071 build_int_cst (TREE_TYPE (*vr0min), 1));
8072 if (!operand_less_p (max, min))
8074 *vr0type = VR_ANTI_RANGE;
8075 *vr0min = min;
8076 *vr0max = max;
8078 else
8079 *vr0min = vr1min;
8081 else
8082 *vr0min = vr1min;
8085 else
8086 gcc_unreachable ();
8088 else if ((maxeq || operand_less_p (vr1max, *vr0max) == 1)
8089 && (mineq || operand_less_p (*vr0min, vr1min) == 1))
8091 /* [ ( ) ] or [( ) ] or [ ( )] */
8092 if (*vr0type == VR_RANGE
8093 && vr1type == VR_RANGE)
8095 else if (*vr0type == VR_ANTI_RANGE
8096 && vr1type == VR_ANTI_RANGE)
8098 *vr0type = vr1type;
8099 *vr0min = vr1min;
8100 *vr0max = vr1max;
8102 else if (*vr0type == VR_ANTI_RANGE
8103 && vr1type == VR_RANGE)
8105 /* Arbitrarily choose the right or left gap. */
8106 if (!mineq && TREE_CODE (vr1min) == INTEGER_CST)
8107 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
8108 build_int_cst (TREE_TYPE (vr1min), 1));
8109 else if (!maxeq && TREE_CODE (vr1max) == INTEGER_CST)
8110 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
8111 build_int_cst (TREE_TYPE (vr1max), 1));
8112 else
8113 goto give_up;
8115 else if (*vr0type == VR_RANGE
8116 && vr1type == VR_ANTI_RANGE)
8117 /* The result covers everything. */
8118 goto give_up;
8119 else
8120 gcc_unreachable ();
8122 else if ((maxeq || operand_less_p (*vr0max, vr1max) == 1)
8123 && (mineq || operand_less_p (vr1min, *vr0min) == 1))
8125 /* ( [ ] ) or ([ ] ) or ( [ ]) */
8126 if (*vr0type == VR_RANGE
8127 && vr1type == VR_RANGE)
8129 *vr0type = vr1type;
8130 *vr0min = vr1min;
8131 *vr0max = vr1max;
8133 else if (*vr0type == VR_ANTI_RANGE
8134 && vr1type == VR_ANTI_RANGE)
8136 else if (*vr0type == VR_RANGE
8137 && vr1type == VR_ANTI_RANGE)
8139 *vr0type = VR_ANTI_RANGE;
8140 if (!mineq && TREE_CODE (*vr0min) == INTEGER_CST)
8142 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
8143 build_int_cst (TREE_TYPE (*vr0min), 1));
8144 *vr0min = vr1min;
8146 else if (!maxeq && TREE_CODE (*vr0max) == INTEGER_CST)
8148 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
8149 build_int_cst (TREE_TYPE (*vr0max), 1));
8150 *vr0max = vr1max;
8152 else
8153 goto give_up;
8155 else if (*vr0type == VR_ANTI_RANGE
8156 && vr1type == VR_RANGE)
8157 /* The result covers everything. */
8158 goto give_up;
8159 else
8160 gcc_unreachable ();
8162 else if ((operand_less_p (vr1min, *vr0max) == 1
8163 || operand_equal_p (vr1min, *vr0max, 0))
8164 && operand_less_p (*vr0min, vr1min) == 1
8165 && operand_less_p (*vr0max, vr1max) == 1)
8167 /* [ ( ] ) or [ ]( ) */
8168 if (*vr0type == VR_RANGE
8169 && vr1type == VR_RANGE)
8170 *vr0max = vr1max;
8171 else if (*vr0type == VR_ANTI_RANGE
8172 && vr1type == VR_ANTI_RANGE)
8173 *vr0min = vr1min;
8174 else if (*vr0type == VR_ANTI_RANGE
8175 && vr1type == VR_RANGE)
8177 if (TREE_CODE (vr1min) == INTEGER_CST)
8178 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
8179 build_int_cst (TREE_TYPE (vr1min), 1));
8180 else
8181 goto give_up;
8183 else if (*vr0type == VR_RANGE
8184 && vr1type == VR_ANTI_RANGE)
8186 if (TREE_CODE (*vr0max) == INTEGER_CST)
8188 *vr0type = vr1type;
8189 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
8190 build_int_cst (TREE_TYPE (*vr0max), 1));
8191 *vr0max = vr1max;
8193 else
8194 goto give_up;
8196 else
8197 gcc_unreachable ();
8199 else if ((operand_less_p (*vr0min, vr1max) == 1
8200 || operand_equal_p (*vr0min, vr1max, 0))
8201 && operand_less_p (vr1min, *vr0min) == 1
8202 && operand_less_p (vr1max, *vr0max) == 1)
8204 /* ( [ ) ] or ( )[ ] */
8205 if (*vr0type == VR_RANGE
8206 && vr1type == VR_RANGE)
8207 *vr0min = vr1min;
8208 else if (*vr0type == VR_ANTI_RANGE
8209 && vr1type == VR_ANTI_RANGE)
8210 *vr0max = vr1max;
8211 else if (*vr0type == VR_ANTI_RANGE
8212 && vr1type == VR_RANGE)
8214 if (TREE_CODE (vr1max) == INTEGER_CST)
8215 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
8216 build_int_cst (TREE_TYPE (vr1max), 1));
8217 else
8218 goto give_up;
8220 else if (*vr0type == VR_RANGE
8221 && vr1type == VR_ANTI_RANGE)
8223 if (TREE_CODE (*vr0min) == INTEGER_CST)
8225 *vr0type = vr1type;
8226 *vr0min = vr1min;
8227 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
8228 build_int_cst (TREE_TYPE (*vr0min), 1));
8230 else
8231 goto give_up;
8233 else
8234 gcc_unreachable ();
8236 else
8237 goto give_up;
8239 return;
8241 give_up:
8242 *vr0type = VR_VARYING;
8243 *vr0min = NULL_TREE;
8244 *vr0max = NULL_TREE;
8247 /* Intersect the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
8248 { VR1TYPE, VR0MIN, VR0MAX } and store the result
8249 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
8250 possible such range. The resulting range is not canonicalized. */
8252 static void
8253 intersect_ranges (enum value_range_type *vr0type,
8254 tree *vr0min, tree *vr0max,
8255 enum value_range_type vr1type,
8256 tree vr1min, tree vr1max)
8258 bool mineq = operand_equal_p (*vr0min, vr1min, 0);
8259 bool maxeq = operand_equal_p (*vr0max, vr1max, 0);
8261 /* [] is vr0, () is vr1 in the following classification comments. */
8262 if (mineq && maxeq)
8264 /* [( )] */
8265 if (*vr0type == vr1type)
8266 /* Nothing to do for equal ranges. */
8268 else if ((*vr0type == VR_RANGE
8269 && vr1type == VR_ANTI_RANGE)
8270 || (*vr0type == VR_ANTI_RANGE
8271 && vr1type == VR_RANGE))
8273 /* For anti-range with range intersection the result is empty. */
8274 *vr0type = VR_UNDEFINED;
8275 *vr0min = NULL_TREE;
8276 *vr0max = NULL_TREE;
8278 else
8279 gcc_unreachable ();
8281 else if (operand_less_p (*vr0max, vr1min) == 1
8282 || operand_less_p (vr1max, *vr0min) == 1)
8284 /* [ ] ( ) or ( ) [ ]
8285 If the ranges have an empty intersection, the result of the
8286 intersect operation is the range for intersecting an
8287 anti-range with a range or empty when intersecting two ranges. */
8288 if (*vr0type == VR_RANGE
8289 && vr1type == VR_ANTI_RANGE)
8291 else if (*vr0type == VR_ANTI_RANGE
8292 && vr1type == VR_RANGE)
8294 *vr0type = vr1type;
8295 *vr0min = vr1min;
8296 *vr0max = vr1max;
8298 else if (*vr0type == VR_RANGE
8299 && vr1type == VR_RANGE)
8301 *vr0type = VR_UNDEFINED;
8302 *vr0min = NULL_TREE;
8303 *vr0max = NULL_TREE;
8305 else if (*vr0type == VR_ANTI_RANGE
8306 && vr1type == VR_ANTI_RANGE)
8308 /* If the anti-ranges are adjacent to each other merge them. */
8309 if (TREE_CODE (*vr0max) == INTEGER_CST
8310 && TREE_CODE (vr1min) == INTEGER_CST
8311 && operand_less_p (*vr0max, vr1min) == 1
8312 && integer_onep (int_const_binop (MINUS_EXPR,
8313 vr1min, *vr0max)))
8314 *vr0max = vr1max;
8315 else if (TREE_CODE (vr1max) == INTEGER_CST
8316 && TREE_CODE (*vr0min) == INTEGER_CST
8317 && operand_less_p (vr1max, *vr0min) == 1
8318 && integer_onep (int_const_binop (MINUS_EXPR,
8319 *vr0min, vr1max)))
8320 *vr0min = vr1min;
8321 /* Else arbitrarily take VR0. */
8324 else if ((maxeq || operand_less_p (vr1max, *vr0max) == 1)
8325 && (mineq || operand_less_p (*vr0min, vr1min) == 1))
8327 /* [ ( ) ] or [( ) ] or [ ( )] */
8328 if (*vr0type == VR_RANGE
8329 && vr1type == VR_RANGE)
8331 /* If both are ranges the result is the inner one. */
8332 *vr0type = vr1type;
8333 *vr0min = vr1min;
8334 *vr0max = vr1max;
8336 else if (*vr0type == VR_RANGE
8337 && vr1type == VR_ANTI_RANGE)
8339 /* Choose the right gap if the left one is empty. */
8340 if (mineq)
8342 if (TREE_CODE (vr1max) == INTEGER_CST)
8343 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
8344 build_int_cst (TREE_TYPE (vr1max), 1));
8345 else
8346 *vr0min = vr1max;
8348 /* Choose the left gap if the right one is empty. */
8349 else if (maxeq)
8351 if (TREE_CODE (vr1min) == INTEGER_CST)
8352 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
8353 build_int_cst (TREE_TYPE (vr1min), 1));
8354 else
8355 *vr0max = vr1min;
8357 /* Choose the anti-range if the range is effectively varying. */
8358 else if (vrp_val_is_min (*vr0min)
8359 && vrp_val_is_max (*vr0max))
8361 *vr0type = vr1type;
8362 *vr0min = vr1min;
8363 *vr0max = vr1max;
8365 /* Else choose the range. */
8367 else if (*vr0type == VR_ANTI_RANGE
8368 && vr1type == VR_ANTI_RANGE)
8369 /* If both are anti-ranges the result is the outer one. */
8371 else if (*vr0type == VR_ANTI_RANGE
8372 && vr1type == VR_RANGE)
8374 /* The intersection is empty. */
8375 *vr0type = VR_UNDEFINED;
8376 *vr0min = NULL_TREE;
8377 *vr0max = NULL_TREE;
8379 else
8380 gcc_unreachable ();
8382 else if ((maxeq || operand_less_p (*vr0max, vr1max) == 1)
8383 && (mineq || operand_less_p (vr1min, *vr0min) == 1))
8385 /* ( [ ] ) or ([ ] ) or ( [ ]) */
8386 if (*vr0type == VR_RANGE
8387 && vr1type == VR_RANGE)
8388 /* Choose the inner range. */
8390 else if (*vr0type == VR_ANTI_RANGE
8391 && vr1type == VR_RANGE)
8393 /* Choose the right gap if the left is empty. */
8394 if (mineq)
8396 *vr0type = VR_RANGE;
8397 if (TREE_CODE (*vr0max) == INTEGER_CST)
8398 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
8399 build_int_cst (TREE_TYPE (*vr0max), 1));
8400 else
8401 *vr0min = *vr0max;
8402 *vr0max = vr1max;
8404 /* Choose the left gap if the right is empty. */
8405 else if (maxeq)
8407 *vr0type = VR_RANGE;
8408 if (TREE_CODE (*vr0min) == INTEGER_CST)
8409 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
8410 build_int_cst (TREE_TYPE (*vr0min), 1));
8411 else
8412 *vr0max = *vr0min;
8413 *vr0min = vr1min;
8415 /* Choose the anti-range if the range is effectively varying. */
8416 else if (vrp_val_is_min (vr1min)
8417 && vrp_val_is_max (vr1max))
8419 /* Else choose the range. */
8420 else
8422 *vr0type = vr1type;
8423 *vr0min = vr1min;
8424 *vr0max = vr1max;
8427 else if (*vr0type == VR_ANTI_RANGE
8428 && vr1type == VR_ANTI_RANGE)
8430 /* If both are anti-ranges the result is the outer one. */
8431 *vr0type = vr1type;
8432 *vr0min = vr1min;
8433 *vr0max = vr1max;
8435 else if (vr1type == VR_ANTI_RANGE
8436 && *vr0type == VR_RANGE)
8438 /* The intersection is empty. */
8439 *vr0type = VR_UNDEFINED;
8440 *vr0min = NULL_TREE;
8441 *vr0max = NULL_TREE;
8443 else
8444 gcc_unreachable ();
8446 else if ((operand_less_p (vr1min, *vr0max) == 1
8447 || operand_equal_p (vr1min, *vr0max, 0))
8448 && operand_less_p (*vr0min, vr1min) == 1)
8450 /* [ ( ] ) or [ ]( ) */
8451 if (*vr0type == VR_ANTI_RANGE
8452 && vr1type == VR_ANTI_RANGE)
8453 *vr0max = vr1max;
8454 else if (*vr0type == VR_RANGE
8455 && vr1type == VR_RANGE)
8456 *vr0min = vr1min;
8457 else if (*vr0type == VR_RANGE
8458 && vr1type == VR_ANTI_RANGE)
8460 if (TREE_CODE (vr1min) == INTEGER_CST)
8461 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
8462 build_int_cst (TREE_TYPE (vr1min), 1));
8463 else
8464 *vr0max = vr1min;
8466 else if (*vr0type == VR_ANTI_RANGE
8467 && vr1type == VR_RANGE)
8469 *vr0type = VR_RANGE;
8470 if (TREE_CODE (*vr0max) == INTEGER_CST)
8471 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
8472 build_int_cst (TREE_TYPE (*vr0max), 1));
8473 else
8474 *vr0min = *vr0max;
8475 *vr0max = vr1max;
8477 else
8478 gcc_unreachable ();
8480 else if ((operand_less_p (*vr0min, vr1max) == 1
8481 || operand_equal_p (*vr0min, vr1max, 0))
8482 && operand_less_p (vr1min, *vr0min) == 1)
8484 /* ( [ ) ] or ( )[ ] */
8485 if (*vr0type == VR_ANTI_RANGE
8486 && vr1type == VR_ANTI_RANGE)
8487 *vr0min = vr1min;
8488 else if (*vr0type == VR_RANGE
8489 && vr1type == VR_RANGE)
8490 *vr0max = vr1max;
8491 else if (*vr0type == VR_RANGE
8492 && vr1type == VR_ANTI_RANGE)
8494 if (TREE_CODE (vr1max) == INTEGER_CST)
8495 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
8496 build_int_cst (TREE_TYPE (vr1max), 1));
8497 else
8498 *vr0min = vr1max;
8500 else if (*vr0type == VR_ANTI_RANGE
8501 && vr1type == VR_RANGE)
8503 *vr0type = VR_RANGE;
8504 if (TREE_CODE (*vr0min) == INTEGER_CST)
8505 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
8506 build_int_cst (TREE_TYPE (*vr0min), 1));
8507 else
8508 *vr0max = *vr0min;
8509 *vr0min = vr1min;
8511 else
8512 gcc_unreachable ();
8515 /* As a fallback simply use { *VRTYPE, *VR0MIN, *VR0MAX } as
8516 result for the intersection. That's always a conservative
8517 correct estimate. */
8519 return;
8523 /* Intersect the two value-ranges *VR0 and *VR1 and store the result
8524 in *VR0. This may not be the smallest possible such range. */
8526 static void
8527 vrp_intersect_ranges_1 (value_range *vr0, value_range *vr1)
8529 value_range saved;
8531 /* If either range is VR_VARYING the other one wins. */
8532 if (vr1->type == VR_VARYING)
8533 return;
8534 if (vr0->type == VR_VARYING)
8536 copy_value_range (vr0, vr1);
8537 return;
8540 /* When either range is VR_UNDEFINED the resulting range is
8541 VR_UNDEFINED, too. */
8542 if (vr0->type == VR_UNDEFINED)
8543 return;
8544 if (vr1->type == VR_UNDEFINED)
8546 set_value_range_to_undefined (vr0);
8547 return;
8550 /* Save the original vr0 so we can return it as conservative intersection
8551 result when our worker turns things to varying. */
8552 saved = *vr0;
8553 intersect_ranges (&vr0->type, &vr0->min, &vr0->max,
8554 vr1->type, vr1->min, vr1->max);
8555 /* Make sure to canonicalize the result though as the inversion of a
8556 VR_RANGE can still be a VR_RANGE. */
8557 set_and_canonicalize_value_range (vr0, vr0->type,
8558 vr0->min, vr0->max, vr0->equiv);
8559 /* If that failed, use the saved original VR0. */
8560 if (vr0->type == VR_VARYING)
8562 *vr0 = saved;
8563 return;
8565 /* If the result is VR_UNDEFINED there is no need to mess with
8566 the equivalencies. */
8567 if (vr0->type == VR_UNDEFINED)
8568 return;
8570 /* The resulting set of equivalences for range intersection is the union of
8571 the two sets. */
8572 if (vr0->equiv && vr1->equiv && vr0->equiv != vr1->equiv)
8573 bitmap_ior_into (vr0->equiv, vr1->equiv);
8574 else if (vr1->equiv && !vr0->equiv)
8575 bitmap_copy (vr0->equiv, vr1->equiv);
8578 static void
8579 vrp_intersect_ranges (value_range *vr0, value_range *vr1)
8581 if (dump_file && (dump_flags & TDF_DETAILS))
8583 fprintf (dump_file, "Intersecting\n ");
8584 dump_value_range (dump_file, vr0);
8585 fprintf (dump_file, "\nand\n ");
8586 dump_value_range (dump_file, vr1);
8587 fprintf (dump_file, "\n");
8589 vrp_intersect_ranges_1 (vr0, vr1);
8590 if (dump_file && (dump_flags & TDF_DETAILS))
8592 fprintf (dump_file, "to\n ");
8593 dump_value_range (dump_file, vr0);
8594 fprintf (dump_file, "\n");
8598 /* Meet operation for value ranges. Given two value ranges VR0 and
8599 VR1, store in VR0 a range that contains both VR0 and VR1. This
8600 may not be the smallest possible such range. */
8602 static void
8603 vrp_meet_1 (value_range *vr0, value_range *vr1)
8605 value_range saved;
8607 if (vr0->type == VR_UNDEFINED)
8609 set_value_range (vr0, vr1->type, vr1->min, vr1->max, vr1->equiv);
8610 return;
8613 if (vr1->type == VR_UNDEFINED)
8615 /* VR0 already has the resulting range. */
8616 return;
8619 if (vr0->type == VR_VARYING)
8621 /* Nothing to do. VR0 already has the resulting range. */
8622 return;
8625 if (vr1->type == VR_VARYING)
8627 set_value_range_to_varying (vr0);
8628 return;
8631 saved = *vr0;
8632 union_ranges (&vr0->type, &vr0->min, &vr0->max,
8633 vr1->type, vr1->min, vr1->max);
8634 if (vr0->type == VR_VARYING)
8636 /* Failed to find an efficient meet. Before giving up and setting
8637 the result to VARYING, see if we can at least derive a useful
8638 anti-range. FIXME, all this nonsense about distinguishing
8639 anti-ranges from ranges is necessary because of the odd
8640 semantics of range_includes_zero_p and friends. */
8641 if (((saved.type == VR_RANGE
8642 && range_includes_zero_p (saved.min, saved.max) == 0)
8643 || (saved.type == VR_ANTI_RANGE
8644 && range_includes_zero_p (saved.min, saved.max) == 1))
8645 && ((vr1->type == VR_RANGE
8646 && range_includes_zero_p (vr1->min, vr1->max) == 0)
8647 || (vr1->type == VR_ANTI_RANGE
8648 && range_includes_zero_p (vr1->min, vr1->max) == 1)))
8650 set_value_range_to_nonnull (vr0, TREE_TYPE (saved.min));
8652 /* Since this meet operation did not result from the meeting of
8653 two equivalent names, VR0 cannot have any equivalences. */
8654 if (vr0->equiv)
8655 bitmap_clear (vr0->equiv);
8656 return;
8659 set_value_range_to_varying (vr0);
8660 return;
8662 set_and_canonicalize_value_range (vr0, vr0->type, vr0->min, vr0->max,
8663 vr0->equiv);
8664 if (vr0->type == VR_VARYING)
8665 return;
8667 /* The resulting set of equivalences is always the intersection of
8668 the two sets. */
8669 if (vr0->equiv && vr1->equiv && vr0->equiv != vr1->equiv)
8670 bitmap_and_into (vr0->equiv, vr1->equiv);
8671 else if (vr0->equiv && !vr1->equiv)
8672 bitmap_clear (vr0->equiv);
8675 static void
8676 vrp_meet (value_range *vr0, value_range *vr1)
8678 if (dump_file && (dump_flags & TDF_DETAILS))
8680 fprintf (dump_file, "Meeting\n ");
8681 dump_value_range (dump_file, vr0);
8682 fprintf (dump_file, "\nand\n ");
8683 dump_value_range (dump_file, vr1);
8684 fprintf (dump_file, "\n");
8686 vrp_meet_1 (vr0, vr1);
8687 if (dump_file && (dump_flags & TDF_DETAILS))
8689 fprintf (dump_file, "to\n ");
8690 dump_value_range (dump_file, vr0);
8691 fprintf (dump_file, "\n");
8696 /* Visit all arguments for PHI node PHI that flow through executable
8697 edges. If a valid value range can be derived from all the incoming
8698 value ranges, set a new range for the LHS of PHI. */
8700 static enum ssa_prop_result
8701 vrp_visit_phi_node (gphi *phi)
8703 size_t i;
8704 tree lhs = PHI_RESULT (phi);
8705 value_range *lhs_vr = get_value_range (lhs);
8706 value_range vr_result = VR_INITIALIZER;
8707 bool first = true;
8708 int edges, old_edges;
8709 struct loop *l;
8711 if (dump_file && (dump_flags & TDF_DETAILS))
8713 fprintf (dump_file, "\nVisiting PHI node: ");
8714 print_gimple_stmt (dump_file, phi, 0, dump_flags);
8717 edges = 0;
8718 for (i = 0; i < gimple_phi_num_args (phi); i++)
8720 edge e = gimple_phi_arg_edge (phi, i);
8722 if (dump_file && (dump_flags & TDF_DETAILS))
8724 fprintf (dump_file,
8725 " Argument #%d (%d -> %d %sexecutable)\n",
8726 (int) i, e->src->index, e->dest->index,
8727 (e->flags & EDGE_EXECUTABLE) ? "" : "not ");
8730 if (e->flags & EDGE_EXECUTABLE)
8732 tree arg = PHI_ARG_DEF (phi, i);
8733 value_range vr_arg;
8735 ++edges;
8737 if (TREE_CODE (arg) == SSA_NAME)
8739 vr_arg = *(get_value_range (arg));
8740 /* Do not allow equivalences or symbolic ranges to leak in from
8741 backedges. That creates invalid equivalencies.
8742 See PR53465 and PR54767. */
8743 if (e->flags & EDGE_DFS_BACK)
8745 if (vr_arg.type == VR_RANGE
8746 || vr_arg.type == VR_ANTI_RANGE)
8748 vr_arg.equiv = NULL;
8749 if (symbolic_range_p (&vr_arg))
8751 vr_arg.type = VR_VARYING;
8752 vr_arg.min = NULL_TREE;
8753 vr_arg.max = NULL_TREE;
8757 else
8759 /* If the non-backedge arguments range is VR_VARYING then
8760 we can still try recording a simple equivalence. */
8761 if (vr_arg.type == VR_VARYING)
8763 vr_arg.type = VR_RANGE;
8764 vr_arg.min = arg;
8765 vr_arg.max = arg;
8766 vr_arg.equiv = NULL;
8770 else
8772 if (TREE_OVERFLOW_P (arg))
8773 arg = drop_tree_overflow (arg);
8775 vr_arg.type = VR_RANGE;
8776 vr_arg.min = arg;
8777 vr_arg.max = arg;
8778 vr_arg.equiv = NULL;
8781 if (dump_file && (dump_flags & TDF_DETAILS))
8783 fprintf (dump_file, "\t");
8784 print_generic_expr (dump_file, arg, dump_flags);
8785 fprintf (dump_file, ": ");
8786 dump_value_range (dump_file, &vr_arg);
8787 fprintf (dump_file, "\n");
8790 if (first)
8791 copy_value_range (&vr_result, &vr_arg);
8792 else
8793 vrp_meet (&vr_result, &vr_arg);
8794 first = false;
8796 if (vr_result.type == VR_VARYING)
8797 break;
8801 if (vr_result.type == VR_VARYING)
8802 goto varying;
8803 else if (vr_result.type == VR_UNDEFINED)
8804 goto update_range;
8806 old_edges = vr_phi_edge_counts[SSA_NAME_VERSION (lhs)];
8807 vr_phi_edge_counts[SSA_NAME_VERSION (lhs)] = edges;
8809 /* To prevent infinite iterations in the algorithm, derive ranges
8810 when the new value is slightly bigger or smaller than the
8811 previous one. We don't do this if we have seen a new executable
8812 edge; this helps us avoid an overflow infinity for conditionals
8813 which are not in a loop. If the old value-range was VR_UNDEFINED
8814 use the updated range and iterate one more time. */
8815 if (edges > 0
8816 && gimple_phi_num_args (phi) > 1
8817 && edges == old_edges
8818 && lhs_vr->type != VR_UNDEFINED)
8820 /* Compare old and new ranges, fall back to varying if the
8821 values are not comparable. */
8822 int cmp_min = compare_values (lhs_vr->min, vr_result.min);
8823 if (cmp_min == -2)
8824 goto varying;
8825 int cmp_max = compare_values (lhs_vr->max, vr_result.max);
8826 if (cmp_max == -2)
8827 goto varying;
8829 /* For non VR_RANGE or for pointers fall back to varying if
8830 the range changed. */
8831 if ((lhs_vr->type != VR_RANGE || vr_result.type != VR_RANGE
8832 || POINTER_TYPE_P (TREE_TYPE (lhs)))
8833 && (cmp_min != 0 || cmp_max != 0))
8834 goto varying;
8836 /* If the new minimum is larger than the previous one
8837 retain the old value. If the new minimum value is smaller
8838 than the previous one and not -INF go all the way to -INF + 1.
8839 In the first case, to avoid infinite bouncing between different
8840 minimums, and in the other case to avoid iterating millions of
8841 times to reach -INF. Going to -INF + 1 also lets the following
8842 iteration compute whether there will be any overflow, at the
8843 expense of one additional iteration. */
8844 if (cmp_min < 0)
8845 vr_result.min = lhs_vr->min;
8846 else if (cmp_min > 0
8847 && !vrp_val_is_min (vr_result.min))
8848 vr_result.min
8849 = int_const_binop (PLUS_EXPR,
8850 vrp_val_min (TREE_TYPE (vr_result.min)),
8851 build_int_cst (TREE_TYPE (vr_result.min), 1));
8853 /* Similarly for the maximum value. */
8854 if (cmp_max > 0)
8855 vr_result.max = lhs_vr->max;
8856 else if (cmp_max < 0
8857 && !vrp_val_is_max (vr_result.max))
8858 vr_result.max
8859 = int_const_binop (MINUS_EXPR,
8860 vrp_val_max (TREE_TYPE (vr_result.min)),
8861 build_int_cst (TREE_TYPE (vr_result.min), 1));
8863 /* If we dropped either bound to +-INF then if this is a loop
8864 PHI node SCEV may known more about its value-range. */
8865 if (cmp_min > 0 || cmp_min < 0
8866 || cmp_max < 0 || cmp_max > 0)
8867 goto scev_check;
8869 goto infinite_check;
8872 /* If the new range is different than the previous value, keep
8873 iterating. */
8874 update_range:
8875 if (update_value_range (lhs, &vr_result))
8877 if (dump_file && (dump_flags & TDF_DETAILS))
8879 fprintf (dump_file, "Found new range for ");
8880 print_generic_expr (dump_file, lhs, 0);
8881 fprintf (dump_file, ": ");
8882 dump_value_range (dump_file, &vr_result);
8883 fprintf (dump_file, "\n");
8886 if (vr_result.type == VR_VARYING)
8887 return SSA_PROP_VARYING;
8889 return SSA_PROP_INTERESTING;
8892 /* Nothing changed, don't add outgoing edges. */
8893 return SSA_PROP_NOT_INTERESTING;
8895 varying:
8896 set_value_range_to_varying (&vr_result);
8898 scev_check:
8899 /* If this is a loop PHI node SCEV may known more about its value-range.
8900 scev_check can be reached from two paths, one is a fall through from above
8901 "varying" label, the other is direct goto from code block which tries to
8902 avoid infinite simulation. */
8903 if ((l = loop_containing_stmt (phi))
8904 && l->header == gimple_bb (phi))
8905 adjust_range_with_scev (&vr_result, l, phi, lhs);
8907 infinite_check:
8908 /* If we will end up with a (-INF, +INF) range, set it to
8909 VARYING. Same if the previous max value was invalid for
8910 the type and we end up with vr_result.min > vr_result.max. */
8911 if ((vr_result.type == VR_RANGE || vr_result.type == VR_ANTI_RANGE)
8912 && !((vrp_val_is_max (vr_result.max) && vrp_val_is_min (vr_result.min))
8913 || compare_values (vr_result.min, vr_result.max) > 0))
8914 goto update_range;
8916 /* No match found. Set the LHS to VARYING. */
8917 set_value_range_to_varying (lhs_vr);
8918 return SSA_PROP_VARYING;
8921 /* Simplify boolean operations if the source is known
8922 to be already a boolean. */
8923 static bool
8924 simplify_truth_ops_using_ranges (gimple_stmt_iterator *gsi, gimple *stmt)
8926 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
8927 tree lhs, op0, op1;
8928 bool need_conversion;
8930 /* We handle only !=/== case here. */
8931 gcc_assert (rhs_code == EQ_EXPR || rhs_code == NE_EXPR);
8933 op0 = gimple_assign_rhs1 (stmt);
8934 if (!op_with_boolean_value_range_p (op0))
8935 return false;
8937 op1 = gimple_assign_rhs2 (stmt);
8938 if (!op_with_boolean_value_range_p (op1))
8939 return false;
8941 /* Reduce number of cases to handle to NE_EXPR. As there is no
8942 BIT_XNOR_EXPR we cannot replace A == B with a single statement. */
8943 if (rhs_code == EQ_EXPR)
8945 if (TREE_CODE (op1) == INTEGER_CST)
8946 op1 = int_const_binop (BIT_XOR_EXPR, op1,
8947 build_int_cst (TREE_TYPE (op1), 1));
8948 else
8949 return false;
8952 lhs = gimple_assign_lhs (stmt);
8953 need_conversion
8954 = !useless_type_conversion_p (TREE_TYPE (lhs), TREE_TYPE (op0));
8956 /* Make sure to not sign-extend a 1-bit 1 when converting the result. */
8957 if (need_conversion
8958 && !TYPE_UNSIGNED (TREE_TYPE (op0))
8959 && TYPE_PRECISION (TREE_TYPE (op0)) == 1
8960 && TYPE_PRECISION (TREE_TYPE (lhs)) > 1)
8961 return false;
8963 /* For A != 0 we can substitute A itself. */
8964 if (integer_zerop (op1))
8965 gimple_assign_set_rhs_with_ops (gsi,
8966 need_conversion
8967 ? NOP_EXPR : TREE_CODE (op0), op0);
8968 /* For A != B we substitute A ^ B. Either with conversion. */
8969 else if (need_conversion)
8971 tree tem = make_ssa_name (TREE_TYPE (op0));
8972 gassign *newop
8973 = gimple_build_assign (tem, BIT_XOR_EXPR, op0, op1);
8974 gsi_insert_before (gsi, newop, GSI_SAME_STMT);
8975 if (INTEGRAL_TYPE_P (TREE_TYPE (tem))
8976 && TYPE_PRECISION (TREE_TYPE (tem)) > 1)
8977 set_range_info (tem, VR_RANGE,
8978 wi::zero (TYPE_PRECISION (TREE_TYPE (tem))),
8979 wi::one (TYPE_PRECISION (TREE_TYPE (tem))));
8980 gimple_assign_set_rhs_with_ops (gsi, NOP_EXPR, tem);
8982 /* Or without. */
8983 else
8984 gimple_assign_set_rhs_with_ops (gsi, BIT_XOR_EXPR, op0, op1);
8985 update_stmt (gsi_stmt (*gsi));
8987 return true;
8990 /* Simplify a division or modulo operator to a right shift or
8991 bitwise and if the first operand is unsigned or is greater
8992 than zero and the second operand is an exact power of two.
8993 For TRUNC_MOD_EXPR op0 % op1 with constant op1, optimize it
8994 into just op0 if op0's range is known to be a subset of
8995 [-op1 + 1, op1 - 1] for signed and [0, op1 - 1] for unsigned
8996 modulo. */
8998 static bool
8999 simplify_div_or_mod_using_ranges (gimple_stmt_iterator *gsi, gimple *stmt)
9001 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
9002 tree val = NULL;
9003 tree op0 = gimple_assign_rhs1 (stmt);
9004 tree op1 = gimple_assign_rhs2 (stmt);
9005 value_range *vr = get_value_range (op0);
9007 if (rhs_code == TRUNC_MOD_EXPR
9008 && TREE_CODE (op1) == INTEGER_CST
9009 && tree_int_cst_sgn (op1) == 1
9010 && range_int_cst_p (vr)
9011 && tree_int_cst_lt (vr->max, op1))
9013 if (TYPE_UNSIGNED (TREE_TYPE (op0))
9014 || tree_int_cst_sgn (vr->min) >= 0
9015 || tree_int_cst_lt (fold_unary (NEGATE_EXPR, TREE_TYPE (op1), op1),
9016 vr->min))
9018 /* If op0 already has the range op0 % op1 has,
9019 then TRUNC_MOD_EXPR won't change anything. */
9020 gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
9021 gimple_assign_set_rhs_from_tree (&gsi, op0);
9022 update_stmt (stmt);
9023 return true;
9027 if (!integer_pow2p (op1))
9029 /* X % -Y can be only optimized into X % Y either if
9030 X is not INT_MIN, or Y is not -1. Fold it now, as after
9031 remove_range_assertions the range info might be not available
9032 anymore. */
9033 if (rhs_code == TRUNC_MOD_EXPR
9034 && fold_stmt (gsi, follow_single_use_edges))
9035 return true;
9036 return false;
9039 if (TYPE_UNSIGNED (TREE_TYPE (op0)))
9040 val = integer_one_node;
9041 else
9043 bool sop = false;
9045 val = compare_range_with_value (GE_EXPR, vr, integer_zero_node, &sop);
9047 if (val
9048 && sop
9049 && integer_onep (val)
9050 && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC))
9052 location_t location;
9054 if (!gimple_has_location (stmt))
9055 location = input_location;
9056 else
9057 location = gimple_location (stmt);
9058 warning_at (location, OPT_Wstrict_overflow,
9059 "assuming signed overflow does not occur when "
9060 "simplifying %</%> or %<%%%> to %<>>%> or %<&%>");
9064 if (val && integer_onep (val))
9066 tree t;
9068 if (rhs_code == TRUNC_DIV_EXPR)
9070 t = build_int_cst (integer_type_node, tree_log2 (op1));
9071 gimple_assign_set_rhs_code (stmt, RSHIFT_EXPR);
9072 gimple_assign_set_rhs1 (stmt, op0);
9073 gimple_assign_set_rhs2 (stmt, t);
9075 else
9077 t = build_int_cst (TREE_TYPE (op1), 1);
9078 t = int_const_binop (MINUS_EXPR, op1, t);
9079 t = fold_convert (TREE_TYPE (op0), t);
9081 gimple_assign_set_rhs_code (stmt, BIT_AND_EXPR);
9082 gimple_assign_set_rhs1 (stmt, op0);
9083 gimple_assign_set_rhs2 (stmt, t);
9086 update_stmt (stmt);
9087 return true;
9090 return false;
9093 /* Simplify a min or max if the ranges of the two operands are
9094 disjoint. Return true if we do simplify. */
9096 static bool
9097 simplify_min_or_max_using_ranges (gimple *stmt)
9099 tree op0 = gimple_assign_rhs1 (stmt);
9100 tree op1 = gimple_assign_rhs2 (stmt);
9101 bool sop = false;
9102 tree val;
9104 val = (vrp_evaluate_conditional_warnv_with_ops_using_ranges
9105 (LE_EXPR, op0, op1, &sop));
9106 if (!val)
9108 sop = false;
9109 val = (vrp_evaluate_conditional_warnv_with_ops_using_ranges
9110 (LT_EXPR, op0, op1, &sop));
9113 if (val)
9115 if (sop && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC))
9117 location_t location;
9119 if (!gimple_has_location (stmt))
9120 location = input_location;
9121 else
9122 location = gimple_location (stmt);
9123 warning_at (location, OPT_Wstrict_overflow,
9124 "assuming signed overflow does not occur when "
9125 "simplifying %<min/max (X,Y)%> to %<X%> or %<Y%>");
9128 /* VAL == TRUE -> OP0 < or <= op1
9129 VAL == FALSE -> OP0 > or >= op1. */
9130 tree res = ((gimple_assign_rhs_code (stmt) == MAX_EXPR)
9131 == integer_zerop (val)) ? op0 : op1;
9132 gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
9133 gimple_assign_set_rhs_from_tree (&gsi, res);
9134 update_stmt (stmt);
9135 return true;
9138 return false;
9141 /* If the operand to an ABS_EXPR is >= 0, then eliminate the
9142 ABS_EXPR. If the operand is <= 0, then simplify the
9143 ABS_EXPR into a NEGATE_EXPR. */
9145 static bool
9146 simplify_abs_using_ranges (gimple *stmt)
9148 tree op = gimple_assign_rhs1 (stmt);
9149 value_range *vr = get_value_range (op);
9151 if (vr)
9153 tree val = NULL;
9154 bool sop = false;
9156 val = compare_range_with_value (LE_EXPR, vr, integer_zero_node, &sop);
9157 if (!val)
9159 /* The range is neither <= 0 nor > 0. Now see if it is
9160 either < 0 or >= 0. */
9161 sop = false;
9162 val = compare_range_with_value (LT_EXPR, vr, integer_zero_node,
9163 &sop);
9166 if (val)
9168 if (sop && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC))
9170 location_t location;
9172 if (!gimple_has_location (stmt))
9173 location = input_location;
9174 else
9175 location = gimple_location (stmt);
9176 warning_at (location, OPT_Wstrict_overflow,
9177 "assuming signed overflow does not occur when "
9178 "simplifying %<abs (X)%> to %<X%> or %<-X%>");
9181 gimple_assign_set_rhs1 (stmt, op);
9182 if (integer_zerop (val))
9183 gimple_assign_set_rhs_code (stmt, SSA_NAME);
9184 else
9185 gimple_assign_set_rhs_code (stmt, NEGATE_EXPR);
9186 update_stmt (stmt);
9187 return true;
9191 return false;
9194 /* Optimize away redundant BIT_AND_EXPR and BIT_IOR_EXPR.
9195 If all the bits that are being cleared by & are already
9196 known to be zero from VR, or all the bits that are being
9197 set by | are already known to be one from VR, the bit
9198 operation is redundant. */
9200 static bool
9201 simplify_bit_ops_using_ranges (gimple_stmt_iterator *gsi, gimple *stmt)
9203 tree op0 = gimple_assign_rhs1 (stmt);
9204 tree op1 = gimple_assign_rhs2 (stmt);
9205 tree op = NULL_TREE;
9206 value_range vr0 = VR_INITIALIZER;
9207 value_range vr1 = VR_INITIALIZER;
9208 wide_int may_be_nonzero0, may_be_nonzero1;
9209 wide_int must_be_nonzero0, must_be_nonzero1;
9210 wide_int mask;
9212 if (TREE_CODE (op0) == SSA_NAME)
9213 vr0 = *(get_value_range (op0));
9214 else if (is_gimple_min_invariant (op0))
9215 set_value_range_to_value (&vr0, op0, NULL);
9216 else
9217 return false;
9219 if (TREE_CODE (op1) == SSA_NAME)
9220 vr1 = *(get_value_range (op1));
9221 else if (is_gimple_min_invariant (op1))
9222 set_value_range_to_value (&vr1, op1, NULL);
9223 else
9224 return false;
9226 if (!zero_nonzero_bits_from_vr (TREE_TYPE (op0), &vr0, &may_be_nonzero0,
9227 &must_be_nonzero0))
9228 return false;
9229 if (!zero_nonzero_bits_from_vr (TREE_TYPE (op1), &vr1, &may_be_nonzero1,
9230 &must_be_nonzero1))
9231 return false;
9233 switch (gimple_assign_rhs_code (stmt))
9235 case BIT_AND_EXPR:
9236 mask = may_be_nonzero0.and_not (must_be_nonzero1);
9237 if (mask == 0)
9239 op = op0;
9240 break;
9242 mask = may_be_nonzero1.and_not (must_be_nonzero0);
9243 if (mask == 0)
9245 op = op1;
9246 break;
9248 break;
9249 case BIT_IOR_EXPR:
9250 mask = may_be_nonzero0.and_not (must_be_nonzero1);
9251 if (mask == 0)
9253 op = op1;
9254 break;
9256 mask = may_be_nonzero1.and_not (must_be_nonzero0);
9257 if (mask == 0)
9259 op = op0;
9260 break;
9262 break;
9263 default:
9264 gcc_unreachable ();
9267 if (op == NULL_TREE)
9268 return false;
9270 gimple_assign_set_rhs_with_ops (gsi, TREE_CODE (op), op);
9271 update_stmt (gsi_stmt (*gsi));
9272 return true;
9275 /* We are comparing trees OP0 and OP1 using COND_CODE. OP0 has
9276 a known value range VR.
9278 If there is one and only one value which will satisfy the
9279 conditional, then return that value. Else return NULL.
9281 If signed overflow must be undefined for the value to satisfy
9282 the conditional, then set *STRICT_OVERFLOW_P to true. */
9284 static tree
9285 test_for_singularity (enum tree_code cond_code, tree op0,
9286 tree op1, value_range *vr,
9287 bool *strict_overflow_p)
9289 tree min = NULL;
9290 tree max = NULL;
9292 /* Extract minimum/maximum values which satisfy the conditional as it was
9293 written. */
9294 if (cond_code == LE_EXPR || cond_code == LT_EXPR)
9296 /* This should not be negative infinity; there is no overflow
9297 here. */
9298 min = TYPE_MIN_VALUE (TREE_TYPE (op0));
9300 max = op1;
9301 if (cond_code == LT_EXPR && !is_overflow_infinity (max))
9303 tree one = build_int_cst (TREE_TYPE (op0), 1);
9304 max = fold_build2 (MINUS_EXPR, TREE_TYPE (op0), max, one);
9305 if (EXPR_P (max))
9306 TREE_NO_WARNING (max) = 1;
9309 else if (cond_code == GE_EXPR || cond_code == GT_EXPR)
9311 /* This should not be positive infinity; there is no overflow
9312 here. */
9313 max = TYPE_MAX_VALUE (TREE_TYPE (op0));
9315 min = op1;
9316 if (cond_code == GT_EXPR && !is_overflow_infinity (min))
9318 tree one = build_int_cst (TREE_TYPE (op0), 1);
9319 min = fold_build2 (PLUS_EXPR, TREE_TYPE (op0), min, one);
9320 if (EXPR_P (min))
9321 TREE_NO_WARNING (min) = 1;
9325 /* Now refine the minimum and maximum values using any
9326 value range information we have for op0. */
9327 if (min && max)
9329 if (compare_values (vr->min, min) == 1)
9330 min = vr->min;
9331 if (compare_values (vr->max, max) == -1)
9332 max = vr->max;
9334 /* If the new min/max values have converged to a single value,
9335 then there is only one value which can satisfy the condition,
9336 return that value. */
9337 if (operand_equal_p (min, max, 0) && is_gimple_min_invariant (min))
9339 if ((cond_code == LE_EXPR || cond_code == LT_EXPR)
9340 && is_overflow_infinity (vr->max))
9341 *strict_overflow_p = true;
9342 if ((cond_code == GE_EXPR || cond_code == GT_EXPR)
9343 && is_overflow_infinity (vr->min))
9344 *strict_overflow_p = true;
9346 return min;
9349 return NULL;
9352 /* Return whether the value range *VR fits in an integer type specified
9353 by PRECISION and UNSIGNED_P. */
9355 static bool
9356 range_fits_type_p (value_range *vr, unsigned dest_precision, signop dest_sgn)
9358 tree src_type;
9359 unsigned src_precision;
9360 widest_int tem;
9361 signop src_sgn;
9363 /* We can only handle integral and pointer types. */
9364 src_type = TREE_TYPE (vr->min);
9365 if (!INTEGRAL_TYPE_P (src_type)
9366 && !POINTER_TYPE_P (src_type))
9367 return false;
9369 /* An extension is fine unless VR is SIGNED and dest_sgn is UNSIGNED,
9370 and so is an identity transform. */
9371 src_precision = TYPE_PRECISION (TREE_TYPE (vr->min));
9372 src_sgn = TYPE_SIGN (src_type);
9373 if ((src_precision < dest_precision
9374 && !(dest_sgn == UNSIGNED && src_sgn == SIGNED))
9375 || (src_precision == dest_precision && src_sgn == dest_sgn))
9376 return true;
9378 /* Now we can only handle ranges with constant bounds. */
9379 if (vr->type != VR_RANGE
9380 || TREE_CODE (vr->min) != INTEGER_CST
9381 || TREE_CODE (vr->max) != INTEGER_CST)
9382 return false;
9384 /* For sign changes, the MSB of the wide_int has to be clear.
9385 An unsigned value with its MSB set cannot be represented by
9386 a signed wide_int, while a negative value cannot be represented
9387 by an unsigned wide_int. */
9388 if (src_sgn != dest_sgn
9389 && (wi::lts_p (vr->min, 0) || wi::lts_p (vr->max, 0)))
9390 return false;
9392 /* Then we can perform the conversion on both ends and compare
9393 the result for equality. */
9394 tem = wi::ext (wi::to_widest (vr->min), dest_precision, dest_sgn);
9395 if (tem != wi::to_widest (vr->min))
9396 return false;
9397 tem = wi::ext (wi::to_widest (vr->max), dest_precision, dest_sgn);
9398 if (tem != wi::to_widest (vr->max))
9399 return false;
9401 return true;
9404 /* Simplify a conditional using a relational operator to an equality
9405 test if the range information indicates only one value can satisfy
9406 the original conditional. */
9408 static bool
9409 simplify_cond_using_ranges (gcond *stmt)
9411 tree op0 = gimple_cond_lhs (stmt);
9412 tree op1 = gimple_cond_rhs (stmt);
9413 enum tree_code cond_code = gimple_cond_code (stmt);
9415 if (cond_code != NE_EXPR
9416 && cond_code != EQ_EXPR
9417 && TREE_CODE (op0) == SSA_NAME
9418 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
9419 && is_gimple_min_invariant (op1))
9421 value_range *vr = get_value_range (op0);
9423 /* If we have range information for OP0, then we might be
9424 able to simplify this conditional. */
9425 if (vr->type == VR_RANGE)
9427 enum warn_strict_overflow_code wc = WARN_STRICT_OVERFLOW_COMPARISON;
9428 bool sop = false;
9429 tree new_tree = test_for_singularity (cond_code, op0, op1, vr, &sop);
9431 if (new_tree
9432 && (!sop || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0))))
9434 if (dump_file)
9436 fprintf (dump_file, "Simplified relational ");
9437 print_gimple_stmt (dump_file, stmt, 0, 0);
9438 fprintf (dump_file, " into ");
9441 gimple_cond_set_code (stmt, EQ_EXPR);
9442 gimple_cond_set_lhs (stmt, op0);
9443 gimple_cond_set_rhs (stmt, new_tree);
9445 update_stmt (stmt);
9447 if (dump_file)
9449 print_gimple_stmt (dump_file, stmt, 0, 0);
9450 fprintf (dump_file, "\n");
9453 if (sop && issue_strict_overflow_warning (wc))
9455 location_t location = input_location;
9456 if (gimple_has_location (stmt))
9457 location = gimple_location (stmt);
9459 warning_at (location, OPT_Wstrict_overflow,
9460 "assuming signed overflow does not occur when "
9461 "simplifying conditional");
9464 return true;
9467 /* Try again after inverting the condition. We only deal
9468 with integral types here, so no need to worry about
9469 issues with inverting FP comparisons. */
9470 sop = false;
9471 new_tree = test_for_singularity
9472 (invert_tree_comparison (cond_code, false),
9473 op0, op1, vr, &sop);
9475 if (new_tree
9476 && (!sop || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0))))
9478 if (dump_file)
9480 fprintf (dump_file, "Simplified relational ");
9481 print_gimple_stmt (dump_file, stmt, 0, 0);
9482 fprintf (dump_file, " into ");
9485 gimple_cond_set_code (stmt, NE_EXPR);
9486 gimple_cond_set_lhs (stmt, op0);
9487 gimple_cond_set_rhs (stmt, new_tree);
9489 update_stmt (stmt);
9491 if (dump_file)
9493 print_gimple_stmt (dump_file, stmt, 0, 0);
9494 fprintf (dump_file, "\n");
9497 if (sop && issue_strict_overflow_warning (wc))
9499 location_t location = input_location;
9500 if (gimple_has_location (stmt))
9501 location = gimple_location (stmt);
9503 warning_at (location, OPT_Wstrict_overflow,
9504 "assuming signed overflow does not occur when "
9505 "simplifying conditional");
9508 return true;
9513 /* If we have a comparison of an SSA_NAME (OP0) against a constant,
9514 see if OP0 was set by a type conversion where the source of
9515 the conversion is another SSA_NAME with a range that fits
9516 into the range of OP0's type.
9518 If so, the conversion is redundant as the earlier SSA_NAME can be
9519 used for the comparison directly if we just massage the constant in the
9520 comparison. */
9521 if (TREE_CODE (op0) == SSA_NAME
9522 && TREE_CODE (op1) == INTEGER_CST)
9524 gimple *def_stmt = SSA_NAME_DEF_STMT (op0);
9525 tree innerop;
9527 if (!is_gimple_assign (def_stmt)
9528 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt)))
9529 return false;
9531 innerop = gimple_assign_rhs1 (def_stmt);
9533 if (TREE_CODE (innerop) == SSA_NAME
9534 && !POINTER_TYPE_P (TREE_TYPE (innerop))
9535 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (innerop)
9536 && desired_pro_or_demotion_p (TREE_TYPE (innerop), TREE_TYPE (op0)))
9538 value_range *vr = get_value_range (innerop);
9540 if (range_int_cst_p (vr)
9541 && range_fits_type_p (vr,
9542 TYPE_PRECISION (TREE_TYPE (op0)),
9543 TYPE_SIGN (TREE_TYPE (op0)))
9544 && int_fits_type_p (op1, TREE_TYPE (innerop))
9545 /* The range must not have overflowed, or if it did overflow
9546 we must not be wrapping/trapping overflow and optimizing
9547 with strict overflow semantics. */
9548 && ((!is_negative_overflow_infinity (vr->min)
9549 && !is_positive_overflow_infinity (vr->max))
9550 || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (innerop))))
9552 /* If the range overflowed and the user has asked for warnings
9553 when strict overflow semantics were used to optimize code,
9554 issue an appropriate warning. */
9555 if (cond_code != EQ_EXPR && cond_code != NE_EXPR
9556 && (is_negative_overflow_infinity (vr->min)
9557 || is_positive_overflow_infinity (vr->max))
9558 && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_CONDITIONAL))
9560 location_t location;
9562 if (!gimple_has_location (stmt))
9563 location = input_location;
9564 else
9565 location = gimple_location (stmt);
9566 warning_at (location, OPT_Wstrict_overflow,
9567 "assuming signed overflow does not occur when "
9568 "simplifying conditional");
9571 tree newconst = fold_convert (TREE_TYPE (innerop), op1);
9572 gimple_cond_set_lhs (stmt, innerop);
9573 gimple_cond_set_rhs (stmt, newconst);
9574 return true;
9579 return false;
9582 /* Simplify a switch statement using the value range of the switch
9583 argument. */
9585 static bool
9586 simplify_switch_using_ranges (gswitch *stmt)
9588 tree op = gimple_switch_index (stmt);
9589 value_range *vr;
9590 bool take_default;
9591 edge e;
9592 edge_iterator ei;
9593 size_t i = 0, j = 0, n, n2;
9594 tree vec2;
9595 switch_update su;
9596 size_t k = 1, l = 0;
9598 if (TREE_CODE (op) == SSA_NAME)
9600 vr = get_value_range (op);
9602 /* We can only handle integer ranges. */
9603 if ((vr->type != VR_RANGE
9604 && vr->type != VR_ANTI_RANGE)
9605 || symbolic_range_p (vr))
9606 return false;
9608 /* Find case label for min/max of the value range. */
9609 take_default = !find_case_label_ranges (stmt, vr, &i, &j, &k, &l);
9611 else if (TREE_CODE (op) == INTEGER_CST)
9613 take_default = !find_case_label_index (stmt, 1, op, &i);
9614 if (take_default)
9616 i = 1;
9617 j = 0;
9619 else
9621 j = i;
9624 else
9625 return false;
9627 n = gimple_switch_num_labels (stmt);
9629 /* Bail out if this is just all edges taken. */
9630 if (i == 1
9631 && j == n - 1
9632 && take_default)
9633 return false;
9635 /* Build a new vector of taken case labels. */
9636 vec2 = make_tree_vec (j - i + 1 + l - k + 1 + (int)take_default);
9637 n2 = 0;
9639 /* Add the default edge, if necessary. */
9640 if (take_default)
9641 TREE_VEC_ELT (vec2, n2++) = gimple_switch_default_label (stmt);
9643 for (; i <= j; ++i, ++n2)
9644 TREE_VEC_ELT (vec2, n2) = gimple_switch_label (stmt, i);
9646 for (; k <= l; ++k, ++n2)
9647 TREE_VEC_ELT (vec2, n2) = gimple_switch_label (stmt, k);
9649 /* Mark needed edges. */
9650 for (i = 0; i < n2; ++i)
9652 e = find_edge (gimple_bb (stmt),
9653 label_to_block (CASE_LABEL (TREE_VEC_ELT (vec2, i))));
9654 e->aux = (void *)-1;
9657 /* Queue not needed edges for later removal. */
9658 FOR_EACH_EDGE (e, ei, gimple_bb (stmt)->succs)
9660 if (e->aux == (void *)-1)
9662 e->aux = NULL;
9663 continue;
9666 if (dump_file && (dump_flags & TDF_DETAILS))
9668 fprintf (dump_file, "removing unreachable case label\n");
9670 to_remove_edges.safe_push (e);
9671 e->flags &= ~EDGE_EXECUTABLE;
9674 /* And queue an update for the stmt. */
9675 su.stmt = stmt;
9676 su.vec = vec2;
9677 to_update_switch_stmts.safe_push (su);
9678 return false;
9681 /* Simplify an integral conversion from an SSA name in STMT. */
9683 static bool
9684 simplify_conversion_using_ranges (gimple *stmt)
9686 tree innerop, middleop, finaltype;
9687 gimple *def_stmt;
9688 signop inner_sgn, middle_sgn, final_sgn;
9689 unsigned inner_prec, middle_prec, final_prec;
9690 widest_int innermin, innermed, innermax, middlemin, middlemed, middlemax;
9692 finaltype = TREE_TYPE (gimple_assign_lhs (stmt));
9693 if (!INTEGRAL_TYPE_P (finaltype))
9694 return false;
9695 middleop = gimple_assign_rhs1 (stmt);
9696 def_stmt = SSA_NAME_DEF_STMT (middleop);
9697 if (!is_gimple_assign (def_stmt)
9698 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt)))
9699 return false;
9700 innerop = gimple_assign_rhs1 (def_stmt);
9701 if (TREE_CODE (innerop) != SSA_NAME
9702 || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (innerop))
9703 return false;
9705 /* Get the value-range of the inner operand. Use get_range_info in
9706 case innerop was created during substitute-and-fold. */
9707 wide_int imin, imax;
9708 if (!INTEGRAL_TYPE_P (TREE_TYPE (innerop))
9709 || get_range_info (innerop, &imin, &imax) != VR_RANGE)
9710 return false;
9711 innermin = widest_int::from (imin, TYPE_SIGN (TREE_TYPE (innerop)));
9712 innermax = widest_int::from (imax, TYPE_SIGN (TREE_TYPE (innerop)));
9714 /* Simulate the conversion chain to check if the result is equal if
9715 the middle conversion is removed. */
9716 inner_prec = TYPE_PRECISION (TREE_TYPE (innerop));
9717 middle_prec = TYPE_PRECISION (TREE_TYPE (middleop));
9718 final_prec = TYPE_PRECISION (finaltype);
9720 /* If the first conversion is not injective, the second must not
9721 be widening. */
9722 if (wi::gtu_p (innermax - innermin,
9723 wi::mask <widest_int> (middle_prec, false))
9724 && middle_prec < final_prec)
9725 return false;
9726 /* We also want a medium value so that we can track the effect that
9727 narrowing conversions with sign change have. */
9728 inner_sgn = TYPE_SIGN (TREE_TYPE (innerop));
9729 if (inner_sgn == UNSIGNED)
9730 innermed = wi::shifted_mask <widest_int> (1, inner_prec - 1, false);
9731 else
9732 innermed = 0;
9733 if (wi::cmp (innermin, innermed, inner_sgn) >= 0
9734 || wi::cmp (innermed, innermax, inner_sgn) >= 0)
9735 innermed = innermin;
9737 middle_sgn = TYPE_SIGN (TREE_TYPE (middleop));
9738 middlemin = wi::ext (innermin, middle_prec, middle_sgn);
9739 middlemed = wi::ext (innermed, middle_prec, middle_sgn);
9740 middlemax = wi::ext (innermax, middle_prec, middle_sgn);
9742 /* Require that the final conversion applied to both the original
9743 and the intermediate range produces the same result. */
9744 final_sgn = TYPE_SIGN (finaltype);
9745 if (wi::ext (middlemin, final_prec, final_sgn)
9746 != wi::ext (innermin, final_prec, final_sgn)
9747 || wi::ext (middlemed, final_prec, final_sgn)
9748 != wi::ext (innermed, final_prec, final_sgn)
9749 || wi::ext (middlemax, final_prec, final_sgn)
9750 != wi::ext (innermax, final_prec, final_sgn))
9751 return false;
9753 gimple_assign_set_rhs1 (stmt, innerop);
9754 update_stmt (stmt);
9755 return true;
9758 /* Simplify a conversion from integral SSA name to float in STMT. */
9760 static bool
9761 simplify_float_conversion_using_ranges (gimple_stmt_iterator *gsi,
9762 gimple *stmt)
9764 tree rhs1 = gimple_assign_rhs1 (stmt);
9765 value_range *vr = get_value_range (rhs1);
9766 machine_mode fltmode = TYPE_MODE (TREE_TYPE (gimple_assign_lhs (stmt)));
9767 machine_mode mode;
9768 tree tem;
9769 gassign *conv;
9771 /* We can only handle constant ranges. */
9772 if (vr->type != VR_RANGE
9773 || TREE_CODE (vr->min) != INTEGER_CST
9774 || TREE_CODE (vr->max) != INTEGER_CST)
9775 return false;
9777 /* First check if we can use a signed type in place of an unsigned. */
9778 if (TYPE_UNSIGNED (TREE_TYPE (rhs1))
9779 && (can_float_p (fltmode, TYPE_MODE (TREE_TYPE (rhs1)), 0)
9780 != CODE_FOR_nothing)
9781 && range_fits_type_p (vr, TYPE_PRECISION (TREE_TYPE (rhs1)), SIGNED))
9782 mode = TYPE_MODE (TREE_TYPE (rhs1));
9783 /* If we can do the conversion in the current input mode do nothing. */
9784 else if (can_float_p (fltmode, TYPE_MODE (TREE_TYPE (rhs1)),
9785 TYPE_UNSIGNED (TREE_TYPE (rhs1))) != CODE_FOR_nothing)
9786 return false;
9787 /* Otherwise search for a mode we can use, starting from the narrowest
9788 integer mode available. */
9789 else
9791 mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
9794 /* If we cannot do a signed conversion to float from mode
9795 or if the value-range does not fit in the signed type
9796 try with a wider mode. */
9797 if (can_float_p (fltmode, mode, 0) != CODE_FOR_nothing
9798 && range_fits_type_p (vr, GET_MODE_PRECISION (mode), SIGNED))
9799 break;
9801 mode = GET_MODE_WIDER_MODE (mode);
9802 /* But do not widen the input. Instead leave that to the
9803 optabs expansion code. */
9804 if (GET_MODE_PRECISION (mode) > TYPE_PRECISION (TREE_TYPE (rhs1)))
9805 return false;
9807 while (mode != VOIDmode);
9808 if (mode == VOIDmode)
9809 return false;
9812 /* It works, insert a truncation or sign-change before the
9813 float conversion. */
9814 tem = make_ssa_name (build_nonstandard_integer_type
9815 (GET_MODE_PRECISION (mode), 0));
9816 conv = gimple_build_assign (tem, NOP_EXPR, rhs1);
9817 gsi_insert_before (gsi, conv, GSI_SAME_STMT);
9818 gimple_assign_set_rhs1 (stmt, tem);
9819 update_stmt (stmt);
9821 return true;
9824 /* Simplify an internal fn call using ranges if possible. */
9826 static bool
9827 simplify_internal_call_using_ranges (gimple_stmt_iterator *gsi, gimple *stmt)
9829 enum tree_code subcode;
9830 bool is_ubsan = false;
9831 bool ovf = false;
9832 switch (gimple_call_internal_fn (stmt))
9834 case IFN_UBSAN_CHECK_ADD:
9835 subcode = PLUS_EXPR;
9836 is_ubsan = true;
9837 break;
9838 case IFN_UBSAN_CHECK_SUB:
9839 subcode = MINUS_EXPR;
9840 is_ubsan = true;
9841 break;
9842 case IFN_UBSAN_CHECK_MUL:
9843 subcode = MULT_EXPR;
9844 is_ubsan = true;
9845 break;
9846 case IFN_ADD_OVERFLOW:
9847 subcode = PLUS_EXPR;
9848 break;
9849 case IFN_SUB_OVERFLOW:
9850 subcode = MINUS_EXPR;
9851 break;
9852 case IFN_MUL_OVERFLOW:
9853 subcode = MULT_EXPR;
9854 break;
9855 default:
9856 return false;
9859 tree op0 = gimple_call_arg (stmt, 0);
9860 tree op1 = gimple_call_arg (stmt, 1);
9861 tree type;
9862 if (is_ubsan)
9863 type = TREE_TYPE (op0);
9864 else if (gimple_call_lhs (stmt) == NULL_TREE)
9865 return false;
9866 else
9867 type = TREE_TYPE (TREE_TYPE (gimple_call_lhs (stmt)));
9868 if (!check_for_binary_op_overflow (subcode, type, op0, op1, &ovf)
9869 || (is_ubsan && ovf))
9870 return false;
9872 gimple *g;
9873 location_t loc = gimple_location (stmt);
9874 if (is_ubsan)
9875 g = gimple_build_assign (gimple_call_lhs (stmt), subcode, op0, op1);
9876 else
9878 int prec = TYPE_PRECISION (type);
9879 tree utype = type;
9880 if (ovf
9881 || !useless_type_conversion_p (type, TREE_TYPE (op0))
9882 || !useless_type_conversion_p (type, TREE_TYPE (op1)))
9883 utype = build_nonstandard_integer_type (prec, 1);
9884 if (TREE_CODE (op0) == INTEGER_CST)
9885 op0 = fold_convert (utype, op0);
9886 else if (!useless_type_conversion_p (utype, TREE_TYPE (op0)))
9888 g = gimple_build_assign (make_ssa_name (utype), NOP_EXPR, op0);
9889 gimple_set_location (g, loc);
9890 gsi_insert_before (gsi, g, GSI_SAME_STMT);
9891 op0 = gimple_assign_lhs (g);
9893 if (TREE_CODE (op1) == INTEGER_CST)
9894 op1 = fold_convert (utype, op1);
9895 else if (!useless_type_conversion_p (utype, TREE_TYPE (op1)))
9897 g = gimple_build_assign (make_ssa_name (utype), NOP_EXPR, op1);
9898 gimple_set_location (g, loc);
9899 gsi_insert_before (gsi, g, GSI_SAME_STMT);
9900 op1 = gimple_assign_lhs (g);
9902 g = gimple_build_assign (make_ssa_name (utype), subcode, op0, op1);
9903 gimple_set_location (g, loc);
9904 gsi_insert_before (gsi, g, GSI_SAME_STMT);
9905 if (utype != type)
9907 g = gimple_build_assign (make_ssa_name (type), NOP_EXPR,
9908 gimple_assign_lhs (g));
9909 gimple_set_location (g, loc);
9910 gsi_insert_before (gsi, g, GSI_SAME_STMT);
9912 g = gimple_build_assign (gimple_call_lhs (stmt), COMPLEX_EXPR,
9913 gimple_assign_lhs (g),
9914 build_int_cst (type, ovf));
9916 gimple_set_location (g, loc);
9917 gsi_replace (gsi, g, false);
9918 return true;
9921 /* Simplify STMT using ranges if possible. */
9923 static bool
9924 simplify_stmt_using_ranges (gimple_stmt_iterator *gsi)
9926 gimple *stmt = gsi_stmt (*gsi);
9927 if (is_gimple_assign (stmt))
9929 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
9930 tree rhs1 = gimple_assign_rhs1 (stmt);
9932 switch (rhs_code)
9934 case EQ_EXPR:
9935 case NE_EXPR:
9936 /* Transform EQ_EXPR, NE_EXPR into BIT_XOR_EXPR or identity
9937 if the RHS is zero or one, and the LHS are known to be boolean
9938 values. */
9939 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9940 return simplify_truth_ops_using_ranges (gsi, stmt);
9941 break;
9943 /* Transform TRUNC_DIV_EXPR and TRUNC_MOD_EXPR into RSHIFT_EXPR
9944 and BIT_AND_EXPR respectively if the first operand is greater
9945 than zero and the second operand is an exact power of two.
9946 Also optimize TRUNC_MOD_EXPR away if the second operand is
9947 constant and the first operand already has the right value
9948 range. */
9949 case TRUNC_DIV_EXPR:
9950 case TRUNC_MOD_EXPR:
9951 if (TREE_CODE (rhs1) == SSA_NAME
9952 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9953 return simplify_div_or_mod_using_ranges (gsi, stmt);
9954 break;
9956 /* Transform ABS (X) into X or -X as appropriate. */
9957 case ABS_EXPR:
9958 if (TREE_CODE (rhs1) == SSA_NAME
9959 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9960 return simplify_abs_using_ranges (stmt);
9961 break;
9963 case BIT_AND_EXPR:
9964 case BIT_IOR_EXPR:
9965 /* Optimize away BIT_AND_EXPR and BIT_IOR_EXPR
9966 if all the bits being cleared are already cleared or
9967 all the bits being set are already set. */
9968 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9969 return simplify_bit_ops_using_ranges (gsi, stmt);
9970 break;
9972 CASE_CONVERT:
9973 if (TREE_CODE (rhs1) == SSA_NAME
9974 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9975 return simplify_conversion_using_ranges (stmt);
9976 break;
9978 case FLOAT_EXPR:
9979 if (TREE_CODE (rhs1) == SSA_NAME
9980 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9981 return simplify_float_conversion_using_ranges (gsi, stmt);
9982 break;
9984 case MIN_EXPR:
9985 case MAX_EXPR:
9986 return simplify_min_or_max_using_ranges (stmt);
9987 break;
9989 default:
9990 break;
9993 else if (gimple_code (stmt) == GIMPLE_COND)
9994 return simplify_cond_using_ranges (as_a <gcond *> (stmt));
9995 else if (gimple_code (stmt) == GIMPLE_SWITCH)
9996 return simplify_switch_using_ranges (as_a <gswitch *> (stmt));
9997 else if (is_gimple_call (stmt)
9998 && gimple_call_internal_p (stmt))
9999 return simplify_internal_call_using_ranges (gsi, stmt);
10001 return false;
10004 /* If the statement pointed by SI has a predicate whose value can be
10005 computed using the value range information computed by VRP, compute
10006 its value and return true. Otherwise, return false. */
10008 static bool
10009 fold_predicate_in (gimple_stmt_iterator *si)
10011 bool assignment_p = false;
10012 tree val;
10013 gimple *stmt = gsi_stmt (*si);
10015 if (is_gimple_assign (stmt)
10016 && TREE_CODE_CLASS (gimple_assign_rhs_code (stmt)) == tcc_comparison)
10018 assignment_p = true;
10019 val = vrp_evaluate_conditional (gimple_assign_rhs_code (stmt),
10020 gimple_assign_rhs1 (stmt),
10021 gimple_assign_rhs2 (stmt),
10022 stmt);
10024 else if (gcond *cond_stmt = dyn_cast <gcond *> (stmt))
10025 val = vrp_evaluate_conditional (gimple_cond_code (cond_stmt),
10026 gimple_cond_lhs (cond_stmt),
10027 gimple_cond_rhs (cond_stmt),
10028 stmt);
10029 else
10030 return false;
10032 if (val)
10034 if (assignment_p)
10035 val = fold_convert (gimple_expr_type (stmt), val);
10037 if (dump_file)
10039 fprintf (dump_file, "Folding predicate ");
10040 print_gimple_expr (dump_file, stmt, 0, 0);
10041 fprintf (dump_file, " to ");
10042 print_generic_expr (dump_file, val, 0);
10043 fprintf (dump_file, "\n");
10046 if (is_gimple_assign (stmt))
10047 gimple_assign_set_rhs_from_tree (si, val);
10048 else
10050 gcc_assert (gimple_code (stmt) == GIMPLE_COND);
10051 gcond *cond_stmt = as_a <gcond *> (stmt);
10052 if (integer_zerop (val))
10053 gimple_cond_make_false (cond_stmt);
10054 else if (integer_onep (val))
10055 gimple_cond_make_true (cond_stmt);
10056 else
10057 gcc_unreachable ();
10060 return true;
10063 return false;
10066 /* Callback for substitute_and_fold folding the stmt at *SI. */
10068 static bool
10069 vrp_fold_stmt (gimple_stmt_iterator *si)
10071 if (fold_predicate_in (si))
10072 return true;
10074 return simplify_stmt_using_ranges (si);
10077 /* Unwindable const/copy equivalences. */
10078 const_and_copies *equiv_stack;
10080 /* A trivial wrapper so that we can present the generic jump threading
10081 code with a simple API for simplifying statements. STMT is the
10082 statement we want to simplify, WITHIN_STMT provides the location
10083 for any overflow warnings. */
10085 static tree
10086 simplify_stmt_for_jump_threading (gimple *stmt, gimple *within_stmt,
10087 class avail_exprs_stack *avail_exprs_stack ATTRIBUTE_UNUSED)
10089 if (gcond *cond_stmt = dyn_cast <gcond *> (stmt))
10090 return vrp_evaluate_conditional (gimple_cond_code (cond_stmt),
10091 gimple_cond_lhs (cond_stmt),
10092 gimple_cond_rhs (cond_stmt),
10093 within_stmt);
10095 if (gassign *assign_stmt = dyn_cast <gassign *> (stmt))
10097 value_range new_vr = VR_INITIALIZER;
10098 tree lhs = gimple_assign_lhs (assign_stmt);
10100 if (TREE_CODE (lhs) == SSA_NAME
10101 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs))
10102 || POINTER_TYPE_P (TREE_TYPE (lhs))))
10104 extract_range_from_assignment (&new_vr, assign_stmt);
10105 if (range_int_cst_singleton_p (&new_vr))
10106 return new_vr.min;
10110 return NULL_TREE;
10113 /* Blocks which have more than one predecessor and more than
10114 one successor present jump threading opportunities, i.e.,
10115 when the block is reached from a specific predecessor, we
10116 may be able to determine which of the outgoing edges will
10117 be traversed. When this optimization applies, we are able
10118 to avoid conditionals at runtime and we may expose secondary
10119 optimization opportunities.
10121 This routine is effectively a driver for the generic jump
10122 threading code. It basically just presents the generic code
10123 with edges that may be suitable for jump threading.
10125 Unlike DOM, we do not iterate VRP if jump threading was successful.
10126 While iterating may expose new opportunities for VRP, it is expected
10127 those opportunities would be very limited and the compile time cost
10128 to expose those opportunities would be significant.
10130 As jump threading opportunities are discovered, they are registered
10131 for later realization. */
10133 static void
10134 identify_jump_threads (void)
10136 basic_block bb;
10137 gcond *dummy;
10138 int i;
10139 edge e;
10141 /* Ugh. When substituting values earlier in this pass we can
10142 wipe the dominance information. So rebuild the dominator
10143 information as we need it within the jump threading code. */
10144 calculate_dominance_info (CDI_DOMINATORS);
10146 /* We do not allow VRP information to be used for jump threading
10147 across a back edge in the CFG. Otherwise it becomes too
10148 difficult to avoid eliminating loop exit tests. Of course
10149 EDGE_DFS_BACK is not accurate at this time so we have to
10150 recompute it. */
10151 mark_dfs_back_edges ();
10153 /* Do not thread across edges we are about to remove. Just marking
10154 them as EDGE_IGNORE will do. */
10155 FOR_EACH_VEC_ELT (to_remove_edges, i, e)
10156 e->flags |= EDGE_IGNORE;
10158 /* Allocate our unwinder stack to unwind any temporary equivalences
10159 that might be recorded. */
10160 equiv_stack = new const_and_copies ();
10162 /* To avoid lots of silly node creation, we create a single
10163 conditional and just modify it in-place when attempting to
10164 thread jumps. */
10165 dummy = gimple_build_cond (EQ_EXPR,
10166 integer_zero_node, integer_zero_node,
10167 NULL, NULL);
10169 /* Walk through all the blocks finding those which present a
10170 potential jump threading opportunity. We could set this up
10171 as a dominator walker and record data during the walk, but
10172 I doubt it's worth the effort for the classes of jump
10173 threading opportunities we are trying to identify at this
10174 point in compilation. */
10175 FOR_EACH_BB_FN (bb, cfun)
10177 gimple *last;
10179 /* If the generic jump threading code does not find this block
10180 interesting, then there is nothing to do. */
10181 if (! potentially_threadable_block (bb))
10182 continue;
10184 last = last_stmt (bb);
10186 /* We're basically looking for a switch or any kind of conditional with
10187 integral or pointer type arguments. Note the type of the second
10188 argument will be the same as the first argument, so no need to
10189 check it explicitly.
10191 We also handle the case where there are no statements in the
10192 block. This come up with forwarder blocks that are not
10193 optimized away because they lead to a loop header. But we do
10194 want to thread through them as we can sometimes thread to the
10195 loop exit which is obviously profitable. */
10196 if (!last
10197 || gimple_code (last) == GIMPLE_SWITCH
10198 || (gimple_code (last) == GIMPLE_COND
10199 && TREE_CODE (gimple_cond_lhs (last)) == SSA_NAME
10200 && (INTEGRAL_TYPE_P (TREE_TYPE (gimple_cond_lhs (last)))
10201 || POINTER_TYPE_P (TREE_TYPE (gimple_cond_lhs (last))))
10202 && (TREE_CODE (gimple_cond_rhs (last)) == SSA_NAME
10203 || is_gimple_min_invariant (gimple_cond_rhs (last)))))
10205 edge_iterator ei;
10207 /* We've got a block with multiple predecessors and multiple
10208 successors which also ends in a suitable conditional or
10209 switch statement. For each predecessor, see if we can thread
10210 it to a specific successor. */
10211 FOR_EACH_EDGE (e, ei, bb->preds)
10213 /* Do not thread across edges marked to ignoreor abnormal
10214 edges in the CFG. */
10215 if (e->flags & (EDGE_IGNORE | EDGE_COMPLEX))
10216 continue;
10218 thread_across_edge (dummy, e, true, equiv_stack, NULL,
10219 simplify_stmt_for_jump_threading);
10224 /* Clear EDGE_IGNORE. */
10225 FOR_EACH_VEC_ELT (to_remove_edges, i, e)
10226 e->flags &= ~EDGE_IGNORE;
10228 /* We do not actually update the CFG or SSA graphs at this point as
10229 ASSERT_EXPRs are still in the IL and cfg cleanup code does not yet
10230 handle ASSERT_EXPRs gracefully. */
10233 /* We identified all the jump threading opportunities earlier, but could
10234 not transform the CFG at that time. This routine transforms the
10235 CFG and arranges for the dominator tree to be rebuilt if necessary.
10237 Note the SSA graph update will occur during the normal TODO
10238 processing by the pass manager. */
10239 static void
10240 finalize_jump_threads (void)
10242 thread_through_all_blocks (false);
10243 delete equiv_stack;
10247 /* Traverse all the blocks folding conditionals with known ranges. */
10249 static void
10250 vrp_finalize (bool warn_array_bounds_p)
10252 size_t i;
10254 values_propagated = true;
10256 if (dump_file)
10258 fprintf (dump_file, "\nValue ranges after VRP:\n\n");
10259 dump_all_value_ranges (dump_file);
10260 fprintf (dump_file, "\n");
10263 /* Set value range to non pointer SSA_NAMEs. */
10264 for (i = 0; i < num_vr_values; i++)
10265 if (vr_value[i])
10267 tree name = ssa_name (i);
10269 if (!name
10270 || POINTER_TYPE_P (TREE_TYPE (name))
10271 || (vr_value[i]->type == VR_VARYING)
10272 || (vr_value[i]->type == VR_UNDEFINED))
10273 continue;
10275 if ((TREE_CODE (vr_value[i]->min) == INTEGER_CST)
10276 && (TREE_CODE (vr_value[i]->max) == INTEGER_CST)
10277 && (vr_value[i]->type == VR_RANGE
10278 || vr_value[i]->type == VR_ANTI_RANGE))
10279 set_range_info (name, vr_value[i]->type, vr_value[i]->min,
10280 vr_value[i]->max);
10283 substitute_and_fold (op_with_constant_singleton_value_range,
10284 vrp_fold_stmt, false);
10286 if (warn_array_bounds && warn_array_bounds_p)
10287 check_all_array_refs ();
10289 /* We must identify jump threading opportunities before we release
10290 the datastructures built by VRP. */
10291 identify_jump_threads ();
10293 /* Free allocated memory. */
10294 for (i = 0; i < num_vr_values; i++)
10295 if (vr_value[i])
10297 BITMAP_FREE (vr_value[i]->equiv);
10298 free (vr_value[i]);
10301 free (vr_value);
10302 free (vr_phi_edge_counts);
10304 /* So that we can distinguish between VRP data being available
10305 and not available. */
10306 vr_value = NULL;
10307 vr_phi_edge_counts = NULL;
10311 /* Main entry point to VRP (Value Range Propagation). This pass is
10312 loosely based on J. R. C. Patterson, ``Accurate Static Branch
10313 Prediction by Value Range Propagation,'' in SIGPLAN Conference on
10314 Programming Language Design and Implementation, pp. 67-78, 1995.
10315 Also available at http://citeseer.ist.psu.edu/patterson95accurate.html
10317 This is essentially an SSA-CCP pass modified to deal with ranges
10318 instead of constants.
10320 While propagating ranges, we may find that two or more SSA name
10321 have equivalent, though distinct ranges. For instance,
10323 1 x_9 = p_3->a;
10324 2 p_4 = ASSERT_EXPR <p_3, p_3 != 0>
10325 3 if (p_4 == q_2)
10326 4 p_5 = ASSERT_EXPR <p_4, p_4 == q_2>;
10327 5 endif
10328 6 if (q_2)
10330 In the code above, pointer p_5 has range [q_2, q_2], but from the
10331 code we can also determine that p_5 cannot be NULL and, if q_2 had
10332 a non-varying range, p_5's range should also be compatible with it.
10334 These equivalences are created by two expressions: ASSERT_EXPR and
10335 copy operations. Since p_5 is an assertion on p_4, and p_4 was the
10336 result of another assertion, then we can use the fact that p_5 and
10337 p_4 are equivalent when evaluating p_5's range.
10339 Together with value ranges, we also propagate these equivalences
10340 between names so that we can take advantage of information from
10341 multiple ranges when doing final replacement. Note that this
10342 equivalency relation is transitive but not symmetric.
10344 In the example above, p_5 is equivalent to p_4, q_2 and p_3, but we
10345 cannot assert that q_2 is equivalent to p_5 because q_2 may be used
10346 in contexts where that assertion does not hold (e.g., in line 6).
10348 TODO, the main difference between this pass and Patterson's is that
10349 we do not propagate edge probabilities. We only compute whether
10350 edges can be taken or not. That is, instead of having a spectrum
10351 of jump probabilities between 0 and 1, we only deal with 0, 1 and
10352 DON'T KNOW. In the future, it may be worthwhile to propagate
10353 probabilities to aid branch prediction. */
10355 static unsigned int
10356 execute_vrp (bool warn_array_bounds_p)
10358 int i;
10359 edge e;
10360 switch_update *su;
10362 loop_optimizer_init (LOOPS_NORMAL | LOOPS_HAVE_RECORDED_EXITS);
10363 rewrite_into_loop_closed_ssa (NULL, TODO_update_ssa);
10364 scev_initialize ();
10366 /* ??? This ends up using stale EDGE_DFS_BACK for liveness computation.
10367 Inserting assertions may split edges which will invalidate
10368 EDGE_DFS_BACK. */
10369 insert_range_assertions ();
10371 to_remove_edges.create (10);
10372 to_update_switch_stmts.create (5);
10373 threadedge_initialize_values ();
10375 /* For visiting PHI nodes we need EDGE_DFS_BACK computed. */
10376 mark_dfs_back_edges ();
10378 vrp_initialize ();
10379 ssa_propagate (vrp_visit_stmt, vrp_visit_phi_node);
10380 vrp_finalize (warn_array_bounds_p);
10382 free_numbers_of_iterations_estimates (cfun);
10384 /* ASSERT_EXPRs must be removed before finalizing jump threads
10385 as finalizing jump threads calls the CFG cleanup code which
10386 does not properly handle ASSERT_EXPRs. */
10387 remove_range_assertions ();
10389 /* If we exposed any new variables, go ahead and put them into
10390 SSA form now, before we handle jump threading. This simplifies
10391 interactions between rewriting of _DECL nodes into SSA form
10392 and rewriting SSA_NAME nodes into SSA form after block
10393 duplication and CFG manipulation. */
10394 update_ssa (TODO_update_ssa);
10396 finalize_jump_threads ();
10398 /* Remove dead edges from SWITCH_EXPR optimization. This leaves the
10399 CFG in a broken state and requires a cfg_cleanup run. */
10400 FOR_EACH_VEC_ELT (to_remove_edges, i, e)
10401 remove_edge (e);
10402 /* Update SWITCH_EXPR case label vector. */
10403 FOR_EACH_VEC_ELT (to_update_switch_stmts, i, su)
10405 size_t j;
10406 size_t n = TREE_VEC_LENGTH (su->vec);
10407 tree label;
10408 gimple_switch_set_num_labels (su->stmt, n);
10409 for (j = 0; j < n; j++)
10410 gimple_switch_set_label (su->stmt, j, TREE_VEC_ELT (su->vec, j));
10411 /* As we may have replaced the default label with a regular one
10412 make sure to make it a real default label again. This ensures
10413 optimal expansion. */
10414 label = gimple_switch_label (su->stmt, 0);
10415 CASE_LOW (label) = NULL_TREE;
10416 CASE_HIGH (label) = NULL_TREE;
10419 if (to_remove_edges.length () > 0)
10421 free_dominance_info (CDI_DOMINATORS);
10422 loops_state_set (LOOPS_NEED_FIXUP);
10425 to_remove_edges.release ();
10426 to_update_switch_stmts.release ();
10427 threadedge_finalize_values ();
10429 scev_finalize ();
10430 loop_optimizer_finalize ();
10431 return 0;
10434 namespace {
10436 const pass_data pass_data_vrp =
10438 GIMPLE_PASS, /* type */
10439 "vrp", /* name */
10440 OPTGROUP_NONE, /* optinfo_flags */
10441 TV_TREE_VRP, /* tv_id */
10442 PROP_ssa, /* properties_required */
10443 0, /* properties_provided */
10444 0, /* properties_destroyed */
10445 0, /* todo_flags_start */
10446 ( TODO_cleanup_cfg | TODO_update_ssa ), /* todo_flags_finish */
10449 class pass_vrp : public gimple_opt_pass
10451 public:
10452 pass_vrp (gcc::context *ctxt)
10453 : gimple_opt_pass (pass_data_vrp, ctxt), warn_array_bounds_p (false)
10456 /* opt_pass methods: */
10457 opt_pass * clone () { return new pass_vrp (m_ctxt); }
10458 void set_pass_param (unsigned int n, bool param)
10460 gcc_assert (n == 0);
10461 warn_array_bounds_p = param;
10463 virtual bool gate (function *) { return flag_tree_vrp != 0; }
10464 virtual unsigned int execute (function *)
10465 { return execute_vrp (warn_array_bounds_p); }
10467 private:
10468 bool warn_array_bounds_p;
10469 }; // class pass_vrp
10471 } // anon namespace
10473 gimple_opt_pass *
10474 make_pass_vrp (gcc::context *ctxt)
10476 return new pass_vrp (ctxt);