Fix for ICE with -g on testcase with incomplete types.
[official-gcc.git] / gcc / tree-vrp.c
blob3138ae7a08fdfb76efd12f49b6ed98ff39507b9d
1 /* Support routines for Value Range Propagation (VRP).
2 Copyright (C) 2005-2015 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 "cfghooks.h"
26 #include "tree.h"
27 #include "gimple.h"
28 #include "rtl.h"
29 #include "ssa.h"
30 #include "flags.h"
31 #include "alias.h"
32 #include "fold-const.h"
33 #include "stor-layout.h"
34 #include "calls.h"
35 #include "cfganal.h"
36 #include "internal-fn.h"
37 #include "gimple-fold.h"
38 #include "tree-eh.h"
39 #include "gimple-iterator.h"
40 #include "gimple-walk.h"
41 #include "tree-cfg.h"
42 #include "tree-ssa-loop-manip.h"
43 #include "tree-ssa-loop-niter.h"
44 #include "tree-ssa-loop.h"
45 #include "tree-into-ssa.h"
46 #include "tree-ssa.h"
47 #include "tree-pass.h"
48 #include "tree-dump.h"
49 #include "gimple-pretty-print.h"
50 #include "diagnostic-core.h"
51 #include "intl.h"
52 #include "cfgloop.h"
53 #include "tree-scalar-evolution.h"
54 #include "tree-ssa-propagate.h"
55 #include "tree-chrec.h"
56 #include "tree-ssa-threadupdate.h"
57 #include "insn-codes.h"
58 #include "optabs-tree.h"
59 #include "tree-ssa-scopedtables.h"
60 #include "tree-ssa-threadedge.h"
64 /* Range of values that can be associated with an SSA_NAME after VRP
65 has executed. */
66 struct value_range
68 /* Lattice value represented by this range. */
69 enum value_range_type type;
71 /* Minimum and maximum values represented by this range. These
72 values should be interpreted as follows:
74 - If TYPE is VR_UNDEFINED or VR_VARYING then MIN and MAX must
75 be NULL.
77 - If TYPE == VR_RANGE then MIN holds the minimum value and
78 MAX holds the maximum value of the range [MIN, MAX].
80 - If TYPE == ANTI_RANGE the variable is known to NOT
81 take any values in the range [MIN, MAX]. */
82 tree min;
83 tree max;
85 /* Set of SSA names whose value ranges are equivalent to this one.
86 This set is only valid when TYPE is VR_RANGE or VR_ANTI_RANGE. */
87 bitmap equiv;
90 #define VR_INITIALIZER { VR_UNDEFINED, NULL_TREE, NULL_TREE, NULL }
92 /* Set of SSA names found live during the RPO traversal of the function
93 for still active basic-blocks. */
94 static sbitmap *live;
96 /* Return true if the SSA name NAME is live on the edge E. */
98 static bool
99 live_on_edge (edge e, tree name)
101 return (live[e->dest->index]
102 && bitmap_bit_p (live[e->dest->index], SSA_NAME_VERSION (name)));
105 /* Local functions. */
106 static int compare_values (tree val1, tree val2);
107 static int compare_values_warnv (tree val1, tree val2, bool *);
108 static void vrp_meet (value_range *, value_range *);
109 static void vrp_intersect_ranges (value_range *, value_range *);
110 static tree vrp_evaluate_conditional_warnv_with_ops (enum tree_code,
111 tree, tree, bool, bool *,
112 bool *);
114 /* Location information for ASSERT_EXPRs. Each instance of this
115 structure describes an ASSERT_EXPR for an SSA name. Since a single
116 SSA name may have more than one assertion associated with it, these
117 locations are kept in a linked list attached to the corresponding
118 SSA name. */
119 struct assert_locus
121 /* Basic block where the assertion would be inserted. */
122 basic_block bb;
124 /* Some assertions need to be inserted on an edge (e.g., assertions
125 generated by COND_EXPRs). In those cases, BB will be NULL. */
126 edge e;
128 /* Pointer to the statement that generated this assertion. */
129 gimple_stmt_iterator si;
131 /* Predicate code for the ASSERT_EXPR. Must be COMPARISON_CLASS_P. */
132 enum tree_code comp_code;
134 /* Value being compared against. */
135 tree val;
137 /* Expression to compare. */
138 tree expr;
140 /* Next node in the linked list. */
141 assert_locus *next;
144 /* If bit I is present, it means that SSA name N_i has a list of
145 assertions that should be inserted in the IL. */
146 static bitmap need_assert_for;
148 /* Array of locations lists where to insert assertions. ASSERTS_FOR[I]
149 holds a list of ASSERT_LOCUS_T nodes that describe where
150 ASSERT_EXPRs for SSA name N_I should be inserted. */
151 static assert_locus **asserts_for;
153 /* Value range array. After propagation, VR_VALUE[I] holds the range
154 of values that SSA name N_I may take. */
155 static unsigned num_vr_values;
156 static value_range **vr_value;
157 static bool values_propagated;
159 /* For a PHI node which sets SSA name N_I, VR_COUNTS[I] holds the
160 number of executable edges we saw the last time we visited the
161 node. */
162 static int *vr_phi_edge_counts;
164 struct switch_update {
165 gswitch *stmt;
166 tree vec;
169 static vec<edge> to_remove_edges;
170 static vec<switch_update> to_update_switch_stmts;
173 /* Return the maximum value for TYPE. */
175 static inline tree
176 vrp_val_max (const_tree type)
178 if (!INTEGRAL_TYPE_P (type))
179 return NULL_TREE;
181 return TYPE_MAX_VALUE (type);
184 /* Return the minimum value for TYPE. */
186 static inline tree
187 vrp_val_min (const_tree type)
189 if (!INTEGRAL_TYPE_P (type))
190 return NULL_TREE;
192 return TYPE_MIN_VALUE (type);
195 /* Return whether VAL is equal to the maximum value of its type. This
196 will be true for a positive overflow infinity. We can't do a
197 simple equality comparison with TYPE_MAX_VALUE because C typedefs
198 and Ada subtypes can produce types whose TYPE_MAX_VALUE is not ==
199 to the integer constant with the same value in the type. */
201 static inline bool
202 vrp_val_is_max (const_tree val)
204 tree type_max = vrp_val_max (TREE_TYPE (val));
205 return (val == type_max
206 || (type_max != NULL_TREE
207 && operand_equal_p (val, type_max, 0)));
210 /* Return whether VAL is equal to the minimum value of its type. This
211 will be true for a negative overflow infinity. */
213 static inline bool
214 vrp_val_is_min (const_tree val)
216 tree type_min = vrp_val_min (TREE_TYPE (val));
217 return (val == type_min
218 || (type_min != NULL_TREE
219 && operand_equal_p (val, type_min, 0)));
223 /* Return whether TYPE should use an overflow infinity distinct from
224 TYPE_{MIN,MAX}_VALUE. We use an overflow infinity value to
225 represent a signed overflow during VRP computations. An infinity
226 is distinct from a half-range, which will go from some number to
227 TYPE_{MIN,MAX}_VALUE. */
229 static inline bool
230 needs_overflow_infinity (const_tree type)
232 return INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_WRAPS (type);
235 /* Return whether TYPE can support our overflow infinity
236 representation: we use the TREE_OVERFLOW flag, which only exists
237 for constants. If TYPE doesn't support this, we don't optimize
238 cases which would require signed overflow--we drop them to
239 VARYING. */
241 static inline bool
242 supports_overflow_infinity (const_tree type)
244 tree min = vrp_val_min (type), max = vrp_val_max (type);
245 gcc_checking_assert (needs_overflow_infinity (type));
246 return (min != NULL_TREE
247 && CONSTANT_CLASS_P (min)
248 && max != NULL_TREE
249 && CONSTANT_CLASS_P (max));
252 /* VAL is the maximum or minimum value of a type. Return a
253 corresponding overflow infinity. */
255 static inline tree
256 make_overflow_infinity (tree val)
258 gcc_checking_assert (val != NULL_TREE && CONSTANT_CLASS_P (val));
259 val = copy_node (val);
260 TREE_OVERFLOW (val) = 1;
261 return val;
264 /* Return a negative overflow infinity for TYPE. */
266 static inline tree
267 negative_overflow_infinity (tree type)
269 gcc_checking_assert (supports_overflow_infinity (type));
270 return make_overflow_infinity (vrp_val_min (type));
273 /* Return a positive overflow infinity for TYPE. */
275 static inline tree
276 positive_overflow_infinity (tree type)
278 gcc_checking_assert (supports_overflow_infinity (type));
279 return make_overflow_infinity (vrp_val_max (type));
282 /* Return whether VAL is a negative overflow infinity. */
284 static inline bool
285 is_negative_overflow_infinity (const_tree val)
287 return (TREE_OVERFLOW_P (val)
288 && needs_overflow_infinity (TREE_TYPE (val))
289 && vrp_val_is_min (val));
292 /* Return whether VAL is a positive overflow infinity. */
294 static inline bool
295 is_positive_overflow_infinity (const_tree val)
297 return (TREE_OVERFLOW_P (val)
298 && needs_overflow_infinity (TREE_TYPE (val))
299 && vrp_val_is_max (val));
302 /* Return whether VAL is a positive or negative overflow infinity. */
304 static inline bool
305 is_overflow_infinity (const_tree val)
307 return (TREE_OVERFLOW_P (val)
308 && needs_overflow_infinity (TREE_TYPE (val))
309 && (vrp_val_is_min (val) || vrp_val_is_max (val)));
312 /* Return whether STMT has a constant rhs that is_overflow_infinity. */
314 static inline bool
315 stmt_overflow_infinity (gimple *stmt)
317 if (is_gimple_assign (stmt)
318 && get_gimple_rhs_class (gimple_assign_rhs_code (stmt)) ==
319 GIMPLE_SINGLE_RHS)
320 return is_overflow_infinity (gimple_assign_rhs1 (stmt));
321 return false;
324 /* If VAL is now an overflow infinity, return VAL. Otherwise, return
325 the same value with TREE_OVERFLOW clear. This can be used to avoid
326 confusing a regular value with an overflow value. */
328 static inline tree
329 avoid_overflow_infinity (tree val)
331 if (!is_overflow_infinity (val))
332 return val;
334 if (vrp_val_is_max (val))
335 return vrp_val_max (TREE_TYPE (val));
336 else
338 gcc_checking_assert (vrp_val_is_min (val));
339 return vrp_val_min (TREE_TYPE (val));
344 /* Set value range VR to VR_UNDEFINED. */
346 static inline void
347 set_value_range_to_undefined (value_range *vr)
349 vr->type = VR_UNDEFINED;
350 vr->min = vr->max = NULL_TREE;
351 if (vr->equiv)
352 bitmap_clear (vr->equiv);
356 /* Set value range VR to VR_VARYING. */
358 static inline void
359 set_value_range_to_varying (value_range *vr)
361 vr->type = VR_VARYING;
362 vr->min = vr->max = NULL_TREE;
363 if (vr->equiv)
364 bitmap_clear (vr->equiv);
368 /* Set value range VR to {T, MIN, MAX, EQUIV}. */
370 static void
371 set_value_range (value_range *vr, enum value_range_type t, tree min,
372 tree max, bitmap equiv)
374 /* Check the validity of the range. */
375 if (flag_checking
376 && (t == VR_RANGE || t == VR_ANTI_RANGE))
378 int cmp;
380 gcc_assert (min && max);
382 gcc_assert ((!TREE_OVERFLOW_P (min) || is_overflow_infinity (min))
383 && (!TREE_OVERFLOW_P (max) || is_overflow_infinity (max)));
385 if (INTEGRAL_TYPE_P (TREE_TYPE (min)) && t == VR_ANTI_RANGE)
386 gcc_assert (!vrp_val_is_min (min) || !vrp_val_is_max (max));
388 cmp = compare_values (min, max);
389 gcc_assert (cmp == 0 || cmp == -1 || cmp == -2);
391 if (needs_overflow_infinity (TREE_TYPE (min)))
392 gcc_assert (!is_overflow_infinity (min)
393 || !is_overflow_infinity (max));
396 if (flag_checking
397 && (t == VR_UNDEFINED || t == VR_VARYING))
399 gcc_assert (min == NULL_TREE && max == NULL_TREE);
400 gcc_assert (equiv == NULL || bitmap_empty_p (equiv));
403 vr->type = t;
404 vr->min = min;
405 vr->max = max;
407 /* Since updating the equivalence set involves deep copying the
408 bitmaps, only do it if absolutely necessary. */
409 if (vr->equiv == NULL
410 && equiv != NULL)
411 vr->equiv = BITMAP_ALLOC (NULL);
413 if (equiv != vr->equiv)
415 if (equiv && !bitmap_empty_p (equiv))
416 bitmap_copy (vr->equiv, equiv);
417 else
418 bitmap_clear (vr->equiv);
423 /* Set value range VR to the canonical form of {T, MIN, MAX, EQUIV}.
424 This means adjusting T, MIN and MAX representing the case of a
425 wrapping range with MAX < MIN covering [MIN, type_max] U [type_min, MAX]
426 as anti-rage ~[MAX+1, MIN-1]. Likewise for wrapping anti-ranges.
427 In corner cases where MAX+1 or MIN-1 wraps this will fall back
428 to varying.
429 This routine exists to ease canonicalization in the case where we
430 extract ranges from var + CST op limit. */
432 static void
433 set_and_canonicalize_value_range (value_range *vr, enum value_range_type t,
434 tree min, tree max, bitmap equiv)
436 /* Use the canonical setters for VR_UNDEFINED and VR_VARYING. */
437 if (t == VR_UNDEFINED)
439 set_value_range_to_undefined (vr);
440 return;
442 else if (t == VR_VARYING)
444 set_value_range_to_varying (vr);
445 return;
448 /* Nothing to canonicalize for symbolic ranges. */
449 if (TREE_CODE (min) != INTEGER_CST
450 || TREE_CODE (max) != INTEGER_CST)
452 set_value_range (vr, t, min, max, equiv);
453 return;
456 /* Wrong order for min and max, to swap them and the VR type we need
457 to adjust them. */
458 if (tree_int_cst_lt (max, min))
460 tree one, tmp;
462 /* For one bit precision if max < min, then the swapped
463 range covers all values, so for VR_RANGE it is varying and
464 for VR_ANTI_RANGE empty range, so drop to varying as well. */
465 if (TYPE_PRECISION (TREE_TYPE (min)) == 1)
467 set_value_range_to_varying (vr);
468 return;
471 one = build_int_cst (TREE_TYPE (min), 1);
472 tmp = int_const_binop (PLUS_EXPR, max, one);
473 max = int_const_binop (MINUS_EXPR, min, one);
474 min = tmp;
476 /* There's one corner case, if we had [C+1, C] before we now have
477 that again. But this represents an empty value range, so drop
478 to varying in this case. */
479 if (tree_int_cst_lt (max, min))
481 set_value_range_to_varying (vr);
482 return;
485 t = t == VR_RANGE ? VR_ANTI_RANGE : VR_RANGE;
488 /* Anti-ranges that can be represented as ranges should be so. */
489 if (t == VR_ANTI_RANGE)
491 bool is_min = vrp_val_is_min (min);
492 bool is_max = vrp_val_is_max (max);
494 if (is_min && is_max)
496 /* We cannot deal with empty ranges, drop to varying.
497 ??? This could be VR_UNDEFINED instead. */
498 set_value_range_to_varying (vr);
499 return;
501 else if (TYPE_PRECISION (TREE_TYPE (min)) == 1
502 && (is_min || is_max))
504 /* Non-empty boolean ranges can always be represented
505 as a singleton range. */
506 if (is_min)
507 min = max = vrp_val_max (TREE_TYPE (min));
508 else
509 min = max = vrp_val_min (TREE_TYPE (min));
510 t = VR_RANGE;
512 else if (is_min
513 /* As a special exception preserve non-null ranges. */
514 && !(TYPE_UNSIGNED (TREE_TYPE (min))
515 && integer_zerop (max)))
517 tree one = build_int_cst (TREE_TYPE (max), 1);
518 min = int_const_binop (PLUS_EXPR, max, one);
519 max = vrp_val_max (TREE_TYPE (max));
520 t = VR_RANGE;
522 else if (is_max)
524 tree one = build_int_cst (TREE_TYPE (min), 1);
525 max = int_const_binop (MINUS_EXPR, min, one);
526 min = vrp_val_min (TREE_TYPE (min));
527 t = VR_RANGE;
531 /* Drop [-INF(OVF), +INF(OVF)] to varying. */
532 if (needs_overflow_infinity (TREE_TYPE (min))
533 && is_overflow_infinity (min)
534 && is_overflow_infinity (max))
536 set_value_range_to_varying (vr);
537 return;
540 set_value_range (vr, t, min, max, equiv);
543 /* Copy value range FROM into value range TO. */
545 static inline void
546 copy_value_range (value_range *to, value_range *from)
548 set_value_range (to, from->type, from->min, from->max, from->equiv);
551 /* Set value range VR to a single value. This function is only called
552 with values we get from statements, and exists to clear the
553 TREE_OVERFLOW flag so that we don't think we have an overflow
554 infinity when we shouldn't. */
556 static inline void
557 set_value_range_to_value (value_range *vr, tree val, bitmap equiv)
559 gcc_assert (is_gimple_min_invariant (val));
560 if (TREE_OVERFLOW_P (val))
561 val = drop_tree_overflow (val);
562 set_value_range (vr, VR_RANGE, val, val, equiv);
565 /* Set value range VR to a non-negative range of type TYPE.
566 OVERFLOW_INFINITY indicates whether to use an overflow infinity
567 rather than TYPE_MAX_VALUE; this should be true if we determine
568 that the range is nonnegative based on the assumption that signed
569 overflow does not occur. */
571 static inline void
572 set_value_range_to_nonnegative (value_range *vr, tree type,
573 bool overflow_infinity)
575 tree zero;
577 if (overflow_infinity && !supports_overflow_infinity (type))
579 set_value_range_to_varying (vr);
580 return;
583 zero = build_int_cst (type, 0);
584 set_value_range (vr, VR_RANGE, zero,
585 (overflow_infinity
586 ? positive_overflow_infinity (type)
587 : TYPE_MAX_VALUE (type)),
588 vr->equiv);
591 /* Set value range VR to a non-NULL range of type TYPE. */
593 static inline void
594 set_value_range_to_nonnull (value_range *vr, tree type)
596 tree zero = build_int_cst (type, 0);
597 set_value_range (vr, VR_ANTI_RANGE, zero, zero, vr->equiv);
601 /* Set value range VR to a NULL range of type TYPE. */
603 static inline void
604 set_value_range_to_null (value_range *vr, tree type)
606 set_value_range_to_value (vr, build_int_cst (type, 0), vr->equiv);
610 /* Set value range VR to a range of a truthvalue of type TYPE. */
612 static inline void
613 set_value_range_to_truthvalue (value_range *vr, tree type)
615 if (TYPE_PRECISION (type) == 1)
616 set_value_range_to_varying (vr);
617 else
618 set_value_range (vr, VR_RANGE,
619 build_int_cst (type, 0), build_int_cst (type, 1),
620 vr->equiv);
624 /* If abs (min) < abs (max), set VR to [-max, max], if
625 abs (min) >= abs (max), set VR to [-min, min]. */
627 static void
628 abs_extent_range (value_range *vr, tree min, tree max)
630 int cmp;
632 gcc_assert (TREE_CODE (min) == INTEGER_CST);
633 gcc_assert (TREE_CODE (max) == INTEGER_CST);
634 gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (min)));
635 gcc_assert (!TYPE_UNSIGNED (TREE_TYPE (min)));
636 min = fold_unary (ABS_EXPR, TREE_TYPE (min), min);
637 max = fold_unary (ABS_EXPR, TREE_TYPE (max), max);
638 if (TREE_OVERFLOW (min) || TREE_OVERFLOW (max))
640 set_value_range_to_varying (vr);
641 return;
643 cmp = compare_values (min, max);
644 if (cmp == -1)
645 min = fold_unary (NEGATE_EXPR, TREE_TYPE (min), max);
646 else if (cmp == 0 || cmp == 1)
648 max = min;
649 min = fold_unary (NEGATE_EXPR, TREE_TYPE (min), min);
651 else
653 set_value_range_to_varying (vr);
654 return;
656 set_and_canonicalize_value_range (vr, VR_RANGE, min, max, NULL);
660 /* Return value range information for VAR.
662 If we have no values ranges recorded (ie, VRP is not running), then
663 return NULL. Otherwise create an empty range if none existed for VAR. */
665 static value_range *
666 get_value_range (const_tree var)
668 static const value_range vr_const_varying
669 = { VR_VARYING, NULL_TREE, NULL_TREE, NULL };
670 value_range *vr;
671 tree sym;
672 unsigned ver = SSA_NAME_VERSION (var);
674 /* If we have no recorded ranges, then return NULL. */
675 if (! vr_value)
676 return NULL;
678 /* If we query the range for a new SSA name return an unmodifiable VARYING.
679 We should get here at most from the substitute-and-fold stage which
680 will never try to change values. */
681 if (ver >= num_vr_values)
682 return CONST_CAST (value_range *, &vr_const_varying);
684 vr = vr_value[ver];
685 if (vr)
686 return vr;
688 /* After propagation finished do not allocate new value-ranges. */
689 if (values_propagated)
690 return CONST_CAST (value_range *, &vr_const_varying);
692 /* Create a default value range. */
693 vr_value[ver] = vr = XCNEW (value_range);
695 /* Defer allocating the equivalence set. */
696 vr->equiv = NULL;
698 /* If VAR is a default definition of a parameter, the variable can
699 take any value in VAR's type. */
700 if (SSA_NAME_IS_DEFAULT_DEF (var))
702 sym = SSA_NAME_VAR (var);
703 if (TREE_CODE (sym) == PARM_DECL)
705 /* Try to use the "nonnull" attribute to create ~[0, 0]
706 anti-ranges for pointers. Note that this is only valid with
707 default definitions of PARM_DECLs. */
708 if (POINTER_TYPE_P (TREE_TYPE (sym))
709 && nonnull_arg_p (sym))
710 set_value_range_to_nonnull (vr, TREE_TYPE (sym));
711 else
712 set_value_range_to_varying (vr);
714 else if (TREE_CODE (sym) == RESULT_DECL
715 && DECL_BY_REFERENCE (sym))
716 set_value_range_to_nonnull (vr, TREE_TYPE (sym));
719 return vr;
722 /* Return true, if VAL1 and VAL2 are equal values for VRP purposes. */
724 static inline bool
725 vrp_operand_equal_p (const_tree val1, const_tree val2)
727 if (val1 == val2)
728 return true;
729 if (!val1 || !val2 || !operand_equal_p (val1, val2, 0))
730 return false;
731 return is_overflow_infinity (val1) == is_overflow_infinity (val2);
734 /* Return true, if the bitmaps B1 and B2 are equal. */
736 static inline bool
737 vrp_bitmap_equal_p (const_bitmap b1, const_bitmap b2)
739 return (b1 == b2
740 || ((!b1 || bitmap_empty_p (b1))
741 && (!b2 || bitmap_empty_p (b2)))
742 || (b1 && b2
743 && bitmap_equal_p (b1, b2)));
746 /* Update the value range and equivalence set for variable VAR to
747 NEW_VR. Return true if NEW_VR is different from VAR's previous
748 value.
750 NOTE: This function assumes that NEW_VR is a temporary value range
751 object created for the sole purpose of updating VAR's range. The
752 storage used by the equivalence set from NEW_VR will be freed by
753 this function. Do not call update_value_range when NEW_VR
754 is the range object associated with another SSA name. */
756 static inline bool
757 update_value_range (const_tree var, value_range *new_vr)
759 value_range *old_vr;
760 bool is_new;
762 /* If there is a value-range on the SSA name from earlier analysis
763 factor that in. */
764 if (INTEGRAL_TYPE_P (TREE_TYPE (var)))
766 wide_int min, max;
767 value_range_type rtype = get_range_info (var, &min, &max);
768 if (rtype == VR_RANGE || rtype == VR_ANTI_RANGE)
770 value_range nr;
771 nr.type = rtype;
772 nr.min = wide_int_to_tree (TREE_TYPE (var), min);
773 nr.max = wide_int_to_tree (TREE_TYPE (var), max);
774 nr.equiv = NULL;
775 vrp_intersect_ranges (new_vr, &nr);
779 /* Update the value range, if necessary. */
780 old_vr = get_value_range (var);
781 is_new = old_vr->type != new_vr->type
782 || !vrp_operand_equal_p (old_vr->min, new_vr->min)
783 || !vrp_operand_equal_p (old_vr->max, new_vr->max)
784 || !vrp_bitmap_equal_p (old_vr->equiv, new_vr->equiv);
786 if (is_new)
788 /* Do not allow transitions up the lattice. The following
789 is slightly more awkward than just new_vr->type < old_vr->type
790 because VR_RANGE and VR_ANTI_RANGE need to be considered
791 the same. We may not have is_new when transitioning to
792 UNDEFINED. If old_vr->type is VARYING, we shouldn't be
793 called. */
794 if (new_vr->type == VR_UNDEFINED)
796 BITMAP_FREE (new_vr->equiv);
797 set_value_range_to_varying (old_vr);
798 set_value_range_to_varying (new_vr);
799 return true;
801 else
802 set_value_range (old_vr, new_vr->type, new_vr->min, new_vr->max,
803 new_vr->equiv);
806 BITMAP_FREE (new_vr->equiv);
808 return is_new;
812 /* Add VAR and VAR's equivalence set to EQUIV. This is the central
813 point where equivalence processing can be turned on/off. */
815 static void
816 add_equivalence (bitmap *equiv, const_tree var)
818 unsigned ver = SSA_NAME_VERSION (var);
819 value_range *vr = vr_value[ver];
821 if (*equiv == NULL)
822 *equiv = BITMAP_ALLOC (NULL);
823 bitmap_set_bit (*equiv, ver);
824 if (vr && vr->equiv)
825 bitmap_ior_into (*equiv, vr->equiv);
829 /* Return true if VR is ~[0, 0]. */
831 static inline bool
832 range_is_nonnull (value_range *vr)
834 return vr->type == VR_ANTI_RANGE
835 && integer_zerop (vr->min)
836 && integer_zerop (vr->max);
840 /* Return true if VR is [0, 0]. */
842 static inline bool
843 range_is_null (value_range *vr)
845 return vr->type == VR_RANGE
846 && integer_zerop (vr->min)
847 && integer_zerop (vr->max);
850 /* Return true if max and min of VR are INTEGER_CST. It's not necessary
851 a singleton. */
853 static inline bool
854 range_int_cst_p (value_range *vr)
856 return (vr->type == VR_RANGE
857 && TREE_CODE (vr->max) == INTEGER_CST
858 && TREE_CODE (vr->min) == INTEGER_CST);
861 /* Return true if VR is a INTEGER_CST singleton. */
863 static inline bool
864 range_int_cst_singleton_p (value_range *vr)
866 return (range_int_cst_p (vr)
867 && !is_overflow_infinity (vr->min)
868 && !is_overflow_infinity (vr->max)
869 && tree_int_cst_equal (vr->min, vr->max));
872 /* Return true if value range VR involves at least one symbol. */
874 static inline bool
875 symbolic_range_p (value_range *vr)
877 return (!is_gimple_min_invariant (vr->min)
878 || !is_gimple_min_invariant (vr->max));
881 /* Return the single symbol (an SSA_NAME) contained in T if any, or NULL_TREE
882 otherwise. We only handle additive operations and set NEG to true if the
883 symbol is negated and INV to the invariant part, if any. */
885 static tree
886 get_single_symbol (tree t, bool *neg, tree *inv)
888 bool neg_;
889 tree inv_;
891 if (TREE_CODE (t) == PLUS_EXPR
892 || TREE_CODE (t) == POINTER_PLUS_EXPR
893 || TREE_CODE (t) == MINUS_EXPR)
895 if (is_gimple_min_invariant (TREE_OPERAND (t, 0)))
897 neg_ = (TREE_CODE (t) == MINUS_EXPR);
898 inv_ = TREE_OPERAND (t, 0);
899 t = TREE_OPERAND (t, 1);
901 else if (is_gimple_min_invariant (TREE_OPERAND (t, 1)))
903 neg_ = false;
904 inv_ = TREE_OPERAND (t, 1);
905 t = TREE_OPERAND (t, 0);
907 else
908 return NULL_TREE;
910 else
912 neg_ = false;
913 inv_ = NULL_TREE;
916 if (TREE_CODE (t) == NEGATE_EXPR)
918 t = TREE_OPERAND (t, 0);
919 neg_ = !neg_;
922 if (TREE_CODE (t) != SSA_NAME)
923 return NULL_TREE;
925 *neg = neg_;
926 *inv = inv_;
927 return t;
930 /* The reverse operation: build a symbolic expression with TYPE
931 from symbol SYM, negated according to NEG, and invariant INV. */
933 static tree
934 build_symbolic_expr (tree type, tree sym, bool neg, tree inv)
936 const bool pointer_p = POINTER_TYPE_P (type);
937 tree t = sym;
939 if (neg)
940 t = build1 (NEGATE_EXPR, type, t);
942 if (integer_zerop (inv))
943 return t;
945 return build2 (pointer_p ? POINTER_PLUS_EXPR : PLUS_EXPR, type, t, inv);
948 /* Return true if value range VR involves exactly one symbol SYM. */
950 static bool
951 symbolic_range_based_on_p (value_range *vr, const_tree sym)
953 bool neg, min_has_symbol, max_has_symbol;
954 tree inv;
956 if (is_gimple_min_invariant (vr->min))
957 min_has_symbol = false;
958 else if (get_single_symbol (vr->min, &neg, &inv) == sym)
959 min_has_symbol = true;
960 else
961 return false;
963 if (is_gimple_min_invariant (vr->max))
964 max_has_symbol = false;
965 else if (get_single_symbol (vr->max, &neg, &inv) == sym)
966 max_has_symbol = true;
967 else
968 return false;
970 return (min_has_symbol || max_has_symbol);
973 /* Return true if value range VR uses an overflow infinity. */
975 static inline bool
976 overflow_infinity_range_p (value_range *vr)
978 return (vr->type == VR_RANGE
979 && (is_overflow_infinity (vr->min)
980 || is_overflow_infinity (vr->max)));
983 /* Return false if we can not make a valid comparison based on VR;
984 this will be the case if it uses an overflow infinity and overflow
985 is not undefined (i.e., -fno-strict-overflow is in effect).
986 Otherwise return true, and set *STRICT_OVERFLOW_P to true if VR
987 uses an overflow infinity. */
989 static bool
990 usable_range_p (value_range *vr, bool *strict_overflow_p)
992 gcc_assert (vr->type == VR_RANGE);
993 if (is_overflow_infinity (vr->min))
995 *strict_overflow_p = true;
996 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (vr->min)))
997 return false;
999 if (is_overflow_infinity (vr->max))
1001 *strict_overflow_p = true;
1002 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (vr->max)))
1003 return false;
1005 return true;
1008 /* Return true if the result of assignment STMT is know to be non-zero.
1009 If the return value is based on the assumption that signed overflow is
1010 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1011 *STRICT_OVERFLOW_P.*/
1013 static bool
1014 gimple_assign_nonzero_warnv_p (gimple *stmt, bool *strict_overflow_p)
1016 enum tree_code code = gimple_assign_rhs_code (stmt);
1017 switch (get_gimple_rhs_class (code))
1019 case GIMPLE_UNARY_RHS:
1020 return tree_unary_nonzero_warnv_p (gimple_assign_rhs_code (stmt),
1021 gimple_expr_type (stmt),
1022 gimple_assign_rhs1 (stmt),
1023 strict_overflow_p);
1024 case GIMPLE_BINARY_RHS:
1025 return tree_binary_nonzero_warnv_p (gimple_assign_rhs_code (stmt),
1026 gimple_expr_type (stmt),
1027 gimple_assign_rhs1 (stmt),
1028 gimple_assign_rhs2 (stmt),
1029 strict_overflow_p);
1030 case GIMPLE_TERNARY_RHS:
1031 return false;
1032 case GIMPLE_SINGLE_RHS:
1033 return tree_single_nonzero_warnv_p (gimple_assign_rhs1 (stmt),
1034 strict_overflow_p);
1035 case GIMPLE_INVALID_RHS:
1036 gcc_unreachable ();
1037 default:
1038 gcc_unreachable ();
1042 /* Return true if STMT is known to compute a non-zero value.
1043 If the return value is based on the assumption that signed overflow is
1044 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1045 *STRICT_OVERFLOW_P.*/
1047 static bool
1048 gimple_stmt_nonzero_warnv_p (gimple *stmt, bool *strict_overflow_p)
1050 switch (gimple_code (stmt))
1052 case GIMPLE_ASSIGN:
1053 return gimple_assign_nonzero_warnv_p (stmt, strict_overflow_p);
1054 case GIMPLE_CALL:
1056 tree fndecl = gimple_call_fndecl (stmt);
1057 if (!fndecl) return false;
1058 if (flag_delete_null_pointer_checks && !flag_check_new
1059 && DECL_IS_OPERATOR_NEW (fndecl)
1060 && !TREE_NOTHROW (fndecl))
1061 return true;
1062 /* References are always non-NULL. */
1063 if (flag_delete_null_pointer_checks
1064 && TREE_CODE (TREE_TYPE (fndecl)) == REFERENCE_TYPE)
1065 return true;
1066 if (flag_delete_null_pointer_checks &&
1067 lookup_attribute ("returns_nonnull",
1068 TYPE_ATTRIBUTES (gimple_call_fntype (stmt))))
1069 return true;
1070 return gimple_alloca_call_p (stmt);
1072 default:
1073 gcc_unreachable ();
1077 /* Like tree_expr_nonzero_warnv_p, but this function uses value ranges
1078 obtained so far. */
1080 static bool
1081 vrp_stmt_computes_nonzero (gimple *stmt, bool *strict_overflow_p)
1083 if (gimple_stmt_nonzero_warnv_p (stmt, strict_overflow_p))
1084 return true;
1086 /* If we have an expression of the form &X->a, then the expression
1087 is nonnull if X is nonnull. */
1088 if (is_gimple_assign (stmt)
1089 && gimple_assign_rhs_code (stmt) == ADDR_EXPR)
1091 tree expr = gimple_assign_rhs1 (stmt);
1092 tree base = get_base_address (TREE_OPERAND (expr, 0));
1094 if (base != NULL_TREE
1095 && TREE_CODE (base) == MEM_REF
1096 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
1098 value_range *vr = get_value_range (TREE_OPERAND (base, 0));
1099 if (range_is_nonnull (vr))
1100 return true;
1104 return false;
1107 /* Returns true if EXPR is a valid value (as expected by compare_values) --
1108 a gimple invariant, or SSA_NAME +- CST. */
1110 static bool
1111 valid_value_p (tree expr)
1113 if (TREE_CODE (expr) == SSA_NAME)
1114 return true;
1116 if (TREE_CODE (expr) == PLUS_EXPR
1117 || TREE_CODE (expr) == MINUS_EXPR)
1118 return (TREE_CODE (TREE_OPERAND (expr, 0)) == SSA_NAME
1119 && TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST);
1121 return is_gimple_min_invariant (expr);
1124 /* Return
1125 1 if VAL < VAL2
1126 0 if !(VAL < VAL2)
1127 -2 if those are incomparable. */
1128 static inline int
1129 operand_less_p (tree val, tree val2)
1131 /* LT is folded faster than GE and others. Inline the common case. */
1132 if (TREE_CODE (val) == INTEGER_CST && TREE_CODE (val2) == INTEGER_CST)
1133 return tree_int_cst_lt (val, val2);
1134 else
1136 tree tcmp;
1138 fold_defer_overflow_warnings ();
1140 tcmp = fold_binary_to_constant (LT_EXPR, boolean_type_node, val, val2);
1142 fold_undefer_and_ignore_overflow_warnings ();
1144 if (!tcmp
1145 || TREE_CODE (tcmp) != INTEGER_CST)
1146 return -2;
1148 if (!integer_zerop (tcmp))
1149 return 1;
1152 /* val >= val2, not considering overflow infinity. */
1153 if (is_negative_overflow_infinity (val))
1154 return is_negative_overflow_infinity (val2) ? 0 : 1;
1155 else if (is_positive_overflow_infinity (val2))
1156 return is_positive_overflow_infinity (val) ? 0 : 1;
1158 return 0;
1161 /* Compare two values VAL1 and VAL2. Return
1163 -2 if VAL1 and VAL2 cannot be compared at compile-time,
1164 -1 if VAL1 < VAL2,
1165 0 if VAL1 == VAL2,
1166 +1 if VAL1 > VAL2, and
1167 +2 if VAL1 != VAL2
1169 This is similar to tree_int_cst_compare but supports pointer values
1170 and values that cannot be compared at compile time.
1172 If STRICT_OVERFLOW_P is not NULL, then set *STRICT_OVERFLOW_P to
1173 true if the return value is only valid if we assume that signed
1174 overflow is undefined. */
1176 static int
1177 compare_values_warnv (tree val1, tree val2, bool *strict_overflow_p)
1179 if (val1 == val2)
1180 return 0;
1182 /* Below we rely on the fact that VAL1 and VAL2 are both pointers or
1183 both integers. */
1184 gcc_assert (POINTER_TYPE_P (TREE_TYPE (val1))
1185 == POINTER_TYPE_P (TREE_TYPE (val2)));
1187 /* Convert the two values into the same type. This is needed because
1188 sizetype causes sign extension even for unsigned types. */
1189 val2 = fold_convert (TREE_TYPE (val1), val2);
1190 STRIP_USELESS_TYPE_CONVERSION (val2);
1192 if ((TREE_CODE (val1) == SSA_NAME
1193 || (TREE_CODE (val1) == NEGATE_EXPR
1194 && TREE_CODE (TREE_OPERAND (val1, 0)) == SSA_NAME)
1195 || TREE_CODE (val1) == PLUS_EXPR
1196 || TREE_CODE (val1) == MINUS_EXPR)
1197 && (TREE_CODE (val2) == SSA_NAME
1198 || (TREE_CODE (val2) == NEGATE_EXPR
1199 && TREE_CODE (TREE_OPERAND (val2, 0)) == SSA_NAME)
1200 || TREE_CODE (val2) == PLUS_EXPR
1201 || TREE_CODE (val2) == MINUS_EXPR))
1203 tree n1, c1, n2, c2;
1204 enum tree_code code1, code2;
1206 /* If VAL1 and VAL2 are of the form '[-]NAME [+-] CST' or 'NAME',
1207 return -1 or +1 accordingly. If VAL1 and VAL2 don't use the
1208 same name, return -2. */
1209 if (TREE_CODE (val1) == SSA_NAME || TREE_CODE (val1) == NEGATE_EXPR)
1211 code1 = SSA_NAME;
1212 n1 = val1;
1213 c1 = NULL_TREE;
1215 else
1217 code1 = TREE_CODE (val1);
1218 n1 = TREE_OPERAND (val1, 0);
1219 c1 = TREE_OPERAND (val1, 1);
1220 if (tree_int_cst_sgn (c1) == -1)
1222 if (is_negative_overflow_infinity (c1))
1223 return -2;
1224 c1 = fold_unary_to_constant (NEGATE_EXPR, TREE_TYPE (c1), c1);
1225 if (!c1)
1226 return -2;
1227 code1 = code1 == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR;
1231 if (TREE_CODE (val2) == SSA_NAME || TREE_CODE (val2) == NEGATE_EXPR)
1233 code2 = SSA_NAME;
1234 n2 = val2;
1235 c2 = NULL_TREE;
1237 else
1239 code2 = TREE_CODE (val2);
1240 n2 = TREE_OPERAND (val2, 0);
1241 c2 = TREE_OPERAND (val2, 1);
1242 if (tree_int_cst_sgn (c2) == -1)
1244 if (is_negative_overflow_infinity (c2))
1245 return -2;
1246 c2 = fold_unary_to_constant (NEGATE_EXPR, TREE_TYPE (c2), c2);
1247 if (!c2)
1248 return -2;
1249 code2 = code2 == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR;
1253 /* Both values must use the same name. */
1254 if (TREE_CODE (n1) == NEGATE_EXPR && TREE_CODE (n2) == NEGATE_EXPR)
1256 n1 = TREE_OPERAND (n1, 0);
1257 n2 = TREE_OPERAND (n2, 0);
1259 if (n1 != n2)
1260 return -2;
1262 if (code1 == SSA_NAME && code2 == SSA_NAME)
1263 /* NAME == NAME */
1264 return 0;
1266 /* If overflow is defined we cannot simplify more. */
1267 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (val1)))
1268 return -2;
1270 if (strict_overflow_p != NULL
1271 && (code1 == SSA_NAME || !TREE_NO_WARNING (val1))
1272 && (code2 == SSA_NAME || !TREE_NO_WARNING (val2)))
1273 *strict_overflow_p = true;
1275 if (code1 == SSA_NAME)
1277 if (code2 == PLUS_EXPR)
1278 /* NAME < NAME + CST */
1279 return -1;
1280 else if (code2 == MINUS_EXPR)
1281 /* NAME > NAME - CST */
1282 return 1;
1284 else if (code1 == PLUS_EXPR)
1286 if (code2 == SSA_NAME)
1287 /* NAME + CST > NAME */
1288 return 1;
1289 else if (code2 == PLUS_EXPR)
1290 /* NAME + CST1 > NAME + CST2, if CST1 > CST2 */
1291 return compare_values_warnv (c1, c2, strict_overflow_p);
1292 else if (code2 == MINUS_EXPR)
1293 /* NAME + CST1 > NAME - CST2 */
1294 return 1;
1296 else if (code1 == MINUS_EXPR)
1298 if (code2 == SSA_NAME)
1299 /* NAME - CST < NAME */
1300 return -1;
1301 else if (code2 == PLUS_EXPR)
1302 /* NAME - CST1 < NAME + CST2 */
1303 return -1;
1304 else if (code2 == MINUS_EXPR)
1305 /* NAME - CST1 > NAME - CST2, if CST1 < CST2. Notice that
1306 C1 and C2 are swapped in the call to compare_values. */
1307 return compare_values_warnv (c2, c1, strict_overflow_p);
1310 gcc_unreachable ();
1313 /* We cannot compare non-constants. */
1314 if (!is_gimple_min_invariant (val1) || !is_gimple_min_invariant (val2))
1315 return -2;
1317 if (!POINTER_TYPE_P (TREE_TYPE (val1)))
1319 /* We cannot compare overflowed values, except for overflow
1320 infinities. */
1321 if (TREE_OVERFLOW (val1) || TREE_OVERFLOW (val2))
1323 if (strict_overflow_p != NULL)
1324 *strict_overflow_p = true;
1325 if (is_negative_overflow_infinity (val1))
1326 return is_negative_overflow_infinity (val2) ? 0 : -1;
1327 else if (is_negative_overflow_infinity (val2))
1328 return 1;
1329 else if (is_positive_overflow_infinity (val1))
1330 return is_positive_overflow_infinity (val2) ? 0 : 1;
1331 else if (is_positive_overflow_infinity (val2))
1332 return -1;
1333 return -2;
1336 return tree_int_cst_compare (val1, val2);
1338 else
1340 tree t;
1342 /* First see if VAL1 and VAL2 are not the same. */
1343 if (val1 == val2 || operand_equal_p (val1, val2, 0))
1344 return 0;
1346 /* If VAL1 is a lower address than VAL2, return -1. */
1347 if (operand_less_p (val1, val2) == 1)
1348 return -1;
1350 /* If VAL1 is a higher address than VAL2, return +1. */
1351 if (operand_less_p (val2, val1) == 1)
1352 return 1;
1354 /* If VAL1 is different than VAL2, return +2.
1355 For integer constants we either have already returned -1 or 1
1356 or they are equivalent. We still might succeed in proving
1357 something about non-trivial operands. */
1358 if (TREE_CODE (val1) != INTEGER_CST
1359 || TREE_CODE (val2) != INTEGER_CST)
1361 t = fold_binary_to_constant (NE_EXPR, boolean_type_node, val1, val2);
1362 if (t && integer_onep (t))
1363 return 2;
1366 return -2;
1370 /* Compare values like compare_values_warnv, but treat comparisons of
1371 nonconstants which rely on undefined overflow as incomparable. */
1373 static int
1374 compare_values (tree val1, tree val2)
1376 bool sop;
1377 int ret;
1379 sop = false;
1380 ret = compare_values_warnv (val1, val2, &sop);
1381 if (sop
1382 && (!is_gimple_min_invariant (val1) || !is_gimple_min_invariant (val2)))
1383 ret = -2;
1384 return ret;
1388 /* Return 1 if VAL is inside value range MIN <= VAL <= MAX,
1389 0 if VAL is not inside [MIN, MAX],
1390 -2 if we cannot tell either way.
1392 Benchmark compile/20001226-1.c compilation time after changing this
1393 function. */
1395 static inline int
1396 value_inside_range (tree val, tree min, tree max)
1398 int cmp1, cmp2;
1400 cmp1 = operand_less_p (val, min);
1401 if (cmp1 == -2)
1402 return -2;
1403 if (cmp1 == 1)
1404 return 0;
1406 cmp2 = operand_less_p (max, val);
1407 if (cmp2 == -2)
1408 return -2;
1410 return !cmp2;
1414 /* Return true if value ranges VR0 and VR1 have a non-empty
1415 intersection.
1417 Benchmark compile/20001226-1.c compilation time after changing this
1418 function.
1421 static inline bool
1422 value_ranges_intersect_p (value_range *vr0, value_range *vr1)
1424 /* The value ranges do not intersect if the maximum of the first range is
1425 less than the minimum of the second range or vice versa.
1426 When those relations are unknown, we can't do any better. */
1427 if (operand_less_p (vr0->max, vr1->min) != 0)
1428 return false;
1429 if (operand_less_p (vr1->max, vr0->min) != 0)
1430 return false;
1431 return true;
1435 /* Return 1 if [MIN, MAX] includes the value zero, 0 if it does not
1436 include the value zero, -2 if we cannot tell. */
1438 static inline int
1439 range_includes_zero_p (tree min, tree max)
1441 tree zero = build_int_cst (TREE_TYPE (min), 0);
1442 return value_inside_range (zero, min, max);
1445 /* Return true if *VR is know to only contain nonnegative values. */
1447 static inline bool
1448 value_range_nonnegative_p (value_range *vr)
1450 /* Testing for VR_ANTI_RANGE is not useful here as any anti-range
1451 which would return a useful value should be encoded as a
1452 VR_RANGE. */
1453 if (vr->type == VR_RANGE)
1455 int result = compare_values (vr->min, integer_zero_node);
1456 return (result == 0 || result == 1);
1459 return false;
1462 /* If *VR has a value rante that is a single constant value return that,
1463 otherwise return NULL_TREE. */
1465 static tree
1466 value_range_constant_singleton (value_range *vr)
1468 if (vr->type == VR_RANGE
1469 && operand_equal_p (vr->min, vr->max, 0)
1470 && is_gimple_min_invariant (vr->min))
1471 return vr->min;
1473 return NULL_TREE;
1476 /* If OP has a value range with a single constant value return that,
1477 otherwise return NULL_TREE. This returns OP itself if OP is a
1478 constant. */
1480 static tree
1481 op_with_constant_singleton_value_range (tree op)
1483 if (is_gimple_min_invariant (op))
1484 return op;
1486 if (TREE_CODE (op) != SSA_NAME)
1487 return NULL_TREE;
1489 return value_range_constant_singleton (get_value_range (op));
1492 /* Return true if op is in a boolean [0, 1] value-range. */
1494 static bool
1495 op_with_boolean_value_range_p (tree op)
1497 value_range *vr;
1499 if (TYPE_PRECISION (TREE_TYPE (op)) == 1)
1500 return true;
1502 if (integer_zerop (op)
1503 || integer_onep (op))
1504 return true;
1506 if (TREE_CODE (op) != SSA_NAME)
1507 return false;
1509 vr = get_value_range (op);
1510 return (vr->type == VR_RANGE
1511 && integer_zerop (vr->min)
1512 && integer_onep (vr->max));
1515 /* Extract value range information from an ASSERT_EXPR EXPR and store
1516 it in *VR_P. */
1518 static void
1519 extract_range_from_assert (value_range *vr_p, tree expr)
1521 tree var, cond, limit, min, max, type;
1522 value_range *limit_vr;
1523 enum tree_code cond_code;
1525 var = ASSERT_EXPR_VAR (expr);
1526 cond = ASSERT_EXPR_COND (expr);
1528 gcc_assert (COMPARISON_CLASS_P (cond));
1530 /* Find VAR in the ASSERT_EXPR conditional. */
1531 if (var == TREE_OPERAND (cond, 0)
1532 || TREE_CODE (TREE_OPERAND (cond, 0)) == PLUS_EXPR
1533 || TREE_CODE (TREE_OPERAND (cond, 0)) == NOP_EXPR)
1535 /* If the predicate is of the form VAR COMP LIMIT, then we just
1536 take LIMIT from the RHS and use the same comparison code. */
1537 cond_code = TREE_CODE (cond);
1538 limit = TREE_OPERAND (cond, 1);
1539 cond = TREE_OPERAND (cond, 0);
1541 else
1543 /* If the predicate is of the form LIMIT COMP VAR, then we need
1544 to flip around the comparison code to create the proper range
1545 for VAR. */
1546 cond_code = swap_tree_comparison (TREE_CODE (cond));
1547 limit = TREE_OPERAND (cond, 0);
1548 cond = TREE_OPERAND (cond, 1);
1551 limit = avoid_overflow_infinity (limit);
1553 type = TREE_TYPE (var);
1554 gcc_assert (limit != var);
1556 /* For pointer arithmetic, we only keep track of pointer equality
1557 and inequality. */
1558 if (POINTER_TYPE_P (type) && cond_code != NE_EXPR && cond_code != EQ_EXPR)
1560 set_value_range_to_varying (vr_p);
1561 return;
1564 /* If LIMIT is another SSA name and LIMIT has a range of its own,
1565 try to use LIMIT's range to avoid creating symbolic ranges
1566 unnecessarily. */
1567 limit_vr = (TREE_CODE (limit) == SSA_NAME) ? get_value_range (limit) : NULL;
1569 /* LIMIT's range is only interesting if it has any useful information. */
1570 if (limit_vr
1571 && (limit_vr->type == VR_UNDEFINED
1572 || limit_vr->type == VR_VARYING
1573 || symbolic_range_p (limit_vr)))
1574 limit_vr = NULL;
1576 /* Initially, the new range has the same set of equivalences of
1577 VAR's range. This will be revised before returning the final
1578 value. Since assertions may be chained via mutually exclusive
1579 predicates, we will need to trim the set of equivalences before
1580 we are done. */
1581 gcc_assert (vr_p->equiv == NULL);
1582 add_equivalence (&vr_p->equiv, var);
1584 /* Extract a new range based on the asserted comparison for VAR and
1585 LIMIT's value range. Notice that if LIMIT has an anti-range, we
1586 will only use it for equality comparisons (EQ_EXPR). For any
1587 other kind of assertion, we cannot derive a range from LIMIT's
1588 anti-range that can be used to describe the new range. For
1589 instance, ASSERT_EXPR <x_2, x_2 <= b_4>. If b_4 is ~[2, 10],
1590 then b_4 takes on the ranges [-INF, 1] and [11, +INF]. There is
1591 no single range for x_2 that could describe LE_EXPR, so we might
1592 as well build the range [b_4, +INF] for it.
1593 One special case we handle is extracting a range from a
1594 range test encoded as (unsigned)var + CST <= limit. */
1595 if (TREE_CODE (cond) == NOP_EXPR
1596 || TREE_CODE (cond) == PLUS_EXPR)
1598 if (TREE_CODE (cond) == PLUS_EXPR)
1600 min = fold_build1 (NEGATE_EXPR, TREE_TYPE (TREE_OPERAND (cond, 1)),
1601 TREE_OPERAND (cond, 1));
1602 max = int_const_binop (PLUS_EXPR, limit, min);
1603 cond = TREE_OPERAND (cond, 0);
1605 else
1607 min = build_int_cst (TREE_TYPE (var), 0);
1608 max = limit;
1611 /* Make sure to not set TREE_OVERFLOW on the final type
1612 conversion. We are willingly interpreting large positive
1613 unsigned values as negative signed values here. */
1614 min = force_fit_type (TREE_TYPE (var), wi::to_widest (min), 0, false);
1615 max = force_fit_type (TREE_TYPE (var), wi::to_widest (max), 0, false);
1617 /* We can transform a max, min range to an anti-range or
1618 vice-versa. Use set_and_canonicalize_value_range which does
1619 this for us. */
1620 if (cond_code == LE_EXPR)
1621 set_and_canonicalize_value_range (vr_p, VR_RANGE,
1622 min, max, vr_p->equiv);
1623 else if (cond_code == GT_EXPR)
1624 set_and_canonicalize_value_range (vr_p, VR_ANTI_RANGE,
1625 min, max, vr_p->equiv);
1626 else
1627 gcc_unreachable ();
1629 else if (cond_code == EQ_EXPR)
1631 enum value_range_type range_type;
1633 if (limit_vr)
1635 range_type = limit_vr->type;
1636 min = limit_vr->min;
1637 max = limit_vr->max;
1639 else
1641 range_type = VR_RANGE;
1642 min = limit;
1643 max = limit;
1646 set_value_range (vr_p, range_type, min, max, vr_p->equiv);
1648 /* When asserting the equality VAR == LIMIT and LIMIT is another
1649 SSA name, the new range will also inherit the equivalence set
1650 from LIMIT. */
1651 if (TREE_CODE (limit) == SSA_NAME)
1652 add_equivalence (&vr_p->equiv, limit);
1654 else if (cond_code == NE_EXPR)
1656 /* As described above, when LIMIT's range is an anti-range and
1657 this assertion is an inequality (NE_EXPR), then we cannot
1658 derive anything from the anti-range. For instance, if
1659 LIMIT's range was ~[0, 0], the assertion 'VAR != LIMIT' does
1660 not imply that VAR's range is [0, 0]. So, in the case of
1661 anti-ranges, we just assert the inequality using LIMIT and
1662 not its anti-range.
1664 If LIMIT_VR is a range, we can only use it to build a new
1665 anti-range if LIMIT_VR is a single-valued range. For
1666 instance, if LIMIT_VR is [0, 1], the predicate
1667 VAR != [0, 1] does not mean that VAR's range is ~[0, 1].
1668 Rather, it means that for value 0 VAR should be ~[0, 0]
1669 and for value 1, VAR should be ~[1, 1]. We cannot
1670 represent these ranges.
1672 The only situation in which we can build a valid
1673 anti-range is when LIMIT_VR is a single-valued range
1674 (i.e., LIMIT_VR->MIN == LIMIT_VR->MAX). In that case,
1675 build the anti-range ~[LIMIT_VR->MIN, LIMIT_VR->MAX]. */
1676 if (limit_vr
1677 && limit_vr->type == VR_RANGE
1678 && compare_values (limit_vr->min, limit_vr->max) == 0)
1680 min = limit_vr->min;
1681 max = limit_vr->max;
1683 else
1685 /* In any other case, we cannot use LIMIT's range to build a
1686 valid anti-range. */
1687 min = max = limit;
1690 /* If MIN and MAX cover the whole range for their type, then
1691 just use the original LIMIT. */
1692 if (INTEGRAL_TYPE_P (type)
1693 && vrp_val_is_min (min)
1694 && vrp_val_is_max (max))
1695 min = max = limit;
1697 set_and_canonicalize_value_range (vr_p, VR_ANTI_RANGE,
1698 min, max, vr_p->equiv);
1700 else if (cond_code == LE_EXPR || cond_code == LT_EXPR)
1702 min = TYPE_MIN_VALUE (type);
1704 if (limit_vr == NULL || limit_vr->type == VR_ANTI_RANGE)
1705 max = limit;
1706 else
1708 /* If LIMIT_VR is of the form [N1, N2], we need to build the
1709 range [MIN, N2] for LE_EXPR and [MIN, N2 - 1] for
1710 LT_EXPR. */
1711 max = limit_vr->max;
1714 /* If the maximum value forces us to be out of bounds, simply punt.
1715 It would be pointless to try and do anything more since this
1716 all should be optimized away above us. */
1717 if ((cond_code == LT_EXPR
1718 && compare_values (max, min) == 0)
1719 || is_overflow_infinity (max))
1720 set_value_range_to_varying (vr_p);
1721 else
1723 /* For LT_EXPR, we create the range [MIN, MAX - 1]. */
1724 if (cond_code == LT_EXPR)
1726 if (TYPE_PRECISION (TREE_TYPE (max)) == 1
1727 && !TYPE_UNSIGNED (TREE_TYPE (max)))
1728 max = fold_build2 (PLUS_EXPR, TREE_TYPE (max), max,
1729 build_int_cst (TREE_TYPE (max), -1));
1730 else
1731 max = fold_build2 (MINUS_EXPR, TREE_TYPE (max), max,
1732 build_int_cst (TREE_TYPE (max), 1));
1733 if (EXPR_P (max))
1734 TREE_NO_WARNING (max) = 1;
1737 set_value_range (vr_p, VR_RANGE, min, max, vr_p->equiv);
1740 else if (cond_code == GE_EXPR || cond_code == GT_EXPR)
1742 max = TYPE_MAX_VALUE (type);
1744 if (limit_vr == NULL || limit_vr->type == VR_ANTI_RANGE)
1745 min = limit;
1746 else
1748 /* If LIMIT_VR is of the form [N1, N2], we need to build the
1749 range [N1, MAX] for GE_EXPR and [N1 + 1, MAX] for
1750 GT_EXPR. */
1751 min = limit_vr->min;
1754 /* If the minimum value forces us to be out of bounds, simply punt.
1755 It would be pointless to try and do anything more since this
1756 all should be optimized away above us. */
1757 if ((cond_code == GT_EXPR
1758 && compare_values (min, max) == 0)
1759 || is_overflow_infinity (min))
1760 set_value_range_to_varying (vr_p);
1761 else
1763 /* For GT_EXPR, we create the range [MIN + 1, MAX]. */
1764 if (cond_code == GT_EXPR)
1766 if (TYPE_PRECISION (TREE_TYPE (min)) == 1
1767 && !TYPE_UNSIGNED (TREE_TYPE (min)))
1768 min = fold_build2 (MINUS_EXPR, TREE_TYPE (min), min,
1769 build_int_cst (TREE_TYPE (min), -1));
1770 else
1771 min = fold_build2 (PLUS_EXPR, TREE_TYPE (min), min,
1772 build_int_cst (TREE_TYPE (min), 1));
1773 if (EXPR_P (min))
1774 TREE_NO_WARNING (min) = 1;
1777 set_value_range (vr_p, VR_RANGE, min, max, vr_p->equiv);
1780 else
1781 gcc_unreachable ();
1783 /* Finally intersect the new range with what we already know about var. */
1784 vrp_intersect_ranges (vr_p, get_value_range (var));
1788 /* Extract range information from SSA name VAR and store it in VR. If
1789 VAR has an interesting range, use it. Otherwise, create the
1790 range [VAR, VAR] and return it. This is useful in situations where
1791 we may have conditionals testing values of VARYING names. For
1792 instance,
1794 x_3 = y_5;
1795 if (x_3 > y_5)
1798 Even if y_5 is deemed VARYING, we can determine that x_3 > y_5 is
1799 always false. */
1801 static void
1802 extract_range_from_ssa_name (value_range *vr, tree var)
1804 value_range *var_vr = get_value_range (var);
1806 if (var_vr->type != VR_VARYING)
1807 copy_value_range (vr, var_vr);
1808 else
1809 set_value_range (vr, VR_RANGE, var, var, NULL);
1811 add_equivalence (&vr->equiv, var);
1815 /* Wrapper around int_const_binop. If the operation overflows and we
1816 are not using wrapping arithmetic, then adjust the result to be
1817 -INF or +INF depending on CODE, VAL1 and VAL2. This can return
1818 NULL_TREE if we need to use an overflow infinity representation but
1819 the type does not support it. */
1821 static tree
1822 vrp_int_const_binop (enum tree_code code, tree val1, tree val2)
1824 tree res;
1826 res = int_const_binop (code, val1, val2);
1828 /* If we are using unsigned arithmetic, operate symbolically
1829 on -INF and +INF as int_const_binop only handles signed overflow. */
1830 if (TYPE_UNSIGNED (TREE_TYPE (val1)))
1832 int checkz = compare_values (res, val1);
1833 bool overflow = false;
1835 /* Ensure that res = val1 [+*] val2 >= val1
1836 or that res = val1 - val2 <= val1. */
1837 if ((code == PLUS_EXPR
1838 && !(checkz == 1 || checkz == 0))
1839 || (code == MINUS_EXPR
1840 && !(checkz == 0 || checkz == -1)))
1842 overflow = true;
1844 /* Checking for multiplication overflow is done by dividing the
1845 output of the multiplication by the first input of the
1846 multiplication. If the result of that division operation is
1847 not equal to the second input of the multiplication, then the
1848 multiplication overflowed. */
1849 else if (code == MULT_EXPR && !integer_zerop (val1))
1851 tree tmp = int_const_binop (TRUNC_DIV_EXPR,
1852 res,
1853 val1);
1854 int check = compare_values (tmp, val2);
1856 if (check != 0)
1857 overflow = true;
1860 if (overflow)
1862 res = copy_node (res);
1863 TREE_OVERFLOW (res) = 1;
1867 else if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (val1)))
1868 /* If the singed operation wraps then int_const_binop has done
1869 everything we want. */
1871 /* Signed division of -1/0 overflows and by the time it gets here
1872 returns NULL_TREE. */
1873 else if (!res)
1874 return NULL_TREE;
1875 else if ((TREE_OVERFLOW (res)
1876 && !TREE_OVERFLOW (val1)
1877 && !TREE_OVERFLOW (val2))
1878 || is_overflow_infinity (val1)
1879 || is_overflow_infinity (val2))
1881 /* If the operation overflowed but neither VAL1 nor VAL2 are
1882 overflown, return -INF or +INF depending on the operation
1883 and the combination of signs of the operands. */
1884 int sgn1 = tree_int_cst_sgn (val1);
1885 int sgn2 = tree_int_cst_sgn (val2);
1887 if (needs_overflow_infinity (TREE_TYPE (res))
1888 && !supports_overflow_infinity (TREE_TYPE (res)))
1889 return NULL_TREE;
1891 /* We have to punt on adding infinities of different signs,
1892 since we can't tell what the sign of the result should be.
1893 Likewise for subtracting infinities of the same sign. */
1894 if (((code == PLUS_EXPR && sgn1 != sgn2)
1895 || (code == MINUS_EXPR && sgn1 == sgn2))
1896 && is_overflow_infinity (val1)
1897 && is_overflow_infinity (val2))
1898 return NULL_TREE;
1900 /* Don't try to handle division or shifting of infinities. */
1901 if ((code == TRUNC_DIV_EXPR
1902 || code == FLOOR_DIV_EXPR
1903 || code == CEIL_DIV_EXPR
1904 || code == EXACT_DIV_EXPR
1905 || code == ROUND_DIV_EXPR
1906 || code == RSHIFT_EXPR)
1907 && (is_overflow_infinity (val1)
1908 || is_overflow_infinity (val2)))
1909 return NULL_TREE;
1911 /* Notice that we only need to handle the restricted set of
1912 operations handled by extract_range_from_binary_expr.
1913 Among them, only multiplication, addition and subtraction
1914 can yield overflow without overflown operands because we
1915 are working with integral types only... except in the
1916 case VAL1 = -INF and VAL2 = -1 which overflows to +INF
1917 for division too. */
1919 /* For multiplication, the sign of the overflow is given
1920 by the comparison of the signs of the operands. */
1921 if ((code == MULT_EXPR && sgn1 == sgn2)
1922 /* For addition, the operands must be of the same sign
1923 to yield an overflow. Its sign is therefore that
1924 of one of the operands, for example the first. For
1925 infinite operands X + -INF is negative, not positive. */
1926 || (code == PLUS_EXPR
1927 && (sgn1 >= 0
1928 ? !is_negative_overflow_infinity (val2)
1929 : is_positive_overflow_infinity (val2)))
1930 /* For subtraction, non-infinite operands must be of
1931 different signs to yield an overflow. Its sign is
1932 therefore that of the first operand or the opposite of
1933 that of the second operand. A first operand of 0 counts
1934 as positive here, for the corner case 0 - (-INF), which
1935 overflows, but must yield +INF. For infinite operands 0
1936 - INF is negative, not positive. */
1937 || (code == MINUS_EXPR
1938 && (sgn1 >= 0
1939 ? !is_positive_overflow_infinity (val2)
1940 : is_negative_overflow_infinity (val2)))
1941 /* We only get in here with positive shift count, so the
1942 overflow direction is the same as the sign of val1.
1943 Actually rshift does not overflow at all, but we only
1944 handle the case of shifting overflowed -INF and +INF. */
1945 || (code == RSHIFT_EXPR
1946 && sgn1 >= 0)
1947 /* For division, the only case is -INF / -1 = +INF. */
1948 || code == TRUNC_DIV_EXPR
1949 || code == FLOOR_DIV_EXPR
1950 || code == CEIL_DIV_EXPR
1951 || code == EXACT_DIV_EXPR
1952 || code == ROUND_DIV_EXPR)
1953 return (needs_overflow_infinity (TREE_TYPE (res))
1954 ? positive_overflow_infinity (TREE_TYPE (res))
1955 : TYPE_MAX_VALUE (TREE_TYPE (res)));
1956 else
1957 return (needs_overflow_infinity (TREE_TYPE (res))
1958 ? negative_overflow_infinity (TREE_TYPE (res))
1959 : TYPE_MIN_VALUE (TREE_TYPE (res)));
1962 return res;
1966 /* For range VR compute two wide_int bitmasks. In *MAY_BE_NONZERO
1967 bitmask if some bit is unset, it means for all numbers in the range
1968 the bit is 0, otherwise it might be 0 or 1. In *MUST_BE_NONZERO
1969 bitmask if some bit is set, it means for all numbers in the range
1970 the bit is 1, otherwise it might be 0 or 1. */
1972 static bool
1973 zero_nonzero_bits_from_vr (const tree expr_type,
1974 value_range *vr,
1975 wide_int *may_be_nonzero,
1976 wide_int *must_be_nonzero)
1978 *may_be_nonzero = wi::minus_one (TYPE_PRECISION (expr_type));
1979 *must_be_nonzero = wi::zero (TYPE_PRECISION (expr_type));
1980 if (!range_int_cst_p (vr)
1981 || is_overflow_infinity (vr->min)
1982 || is_overflow_infinity (vr->max))
1983 return false;
1985 if (range_int_cst_singleton_p (vr))
1987 *may_be_nonzero = vr->min;
1988 *must_be_nonzero = *may_be_nonzero;
1990 else if (tree_int_cst_sgn (vr->min) >= 0
1991 || tree_int_cst_sgn (vr->max) < 0)
1993 wide_int xor_mask = wi::bit_xor (vr->min, vr->max);
1994 *may_be_nonzero = wi::bit_or (vr->min, vr->max);
1995 *must_be_nonzero = wi::bit_and (vr->min, vr->max);
1996 if (xor_mask != 0)
1998 wide_int mask = wi::mask (wi::floor_log2 (xor_mask), false,
1999 may_be_nonzero->get_precision ());
2000 *may_be_nonzero = *may_be_nonzero | mask;
2001 *must_be_nonzero = must_be_nonzero->and_not (mask);
2005 return true;
2008 /* Create two value-ranges in *VR0 and *VR1 from the anti-range *AR
2009 so that *VR0 U *VR1 == *AR. Returns true if that is possible,
2010 false otherwise. If *AR can be represented with a single range
2011 *VR1 will be VR_UNDEFINED. */
2013 static bool
2014 ranges_from_anti_range (value_range *ar,
2015 value_range *vr0, value_range *vr1)
2017 tree type = TREE_TYPE (ar->min);
2019 vr0->type = VR_UNDEFINED;
2020 vr1->type = VR_UNDEFINED;
2022 if (ar->type != VR_ANTI_RANGE
2023 || TREE_CODE (ar->min) != INTEGER_CST
2024 || TREE_CODE (ar->max) != INTEGER_CST
2025 || !vrp_val_min (type)
2026 || !vrp_val_max (type))
2027 return false;
2029 if (!vrp_val_is_min (ar->min))
2031 vr0->type = VR_RANGE;
2032 vr0->min = vrp_val_min (type);
2033 vr0->max = wide_int_to_tree (type, wi::sub (ar->min, 1));
2035 if (!vrp_val_is_max (ar->max))
2037 vr1->type = VR_RANGE;
2038 vr1->min = wide_int_to_tree (type, wi::add (ar->max, 1));
2039 vr1->max = vrp_val_max (type);
2041 if (vr0->type == VR_UNDEFINED)
2043 *vr0 = *vr1;
2044 vr1->type = VR_UNDEFINED;
2047 return vr0->type != VR_UNDEFINED;
2050 /* Helper to extract a value-range *VR for a multiplicative operation
2051 *VR0 CODE *VR1. */
2053 static void
2054 extract_range_from_multiplicative_op_1 (value_range *vr,
2055 enum tree_code code,
2056 value_range *vr0, value_range *vr1)
2058 enum value_range_type type;
2059 tree val[4];
2060 size_t i;
2061 tree min, max;
2062 bool sop;
2063 int cmp;
2065 /* Multiplications, divisions and shifts are a bit tricky to handle,
2066 depending on the mix of signs we have in the two ranges, we
2067 need to operate on different values to get the minimum and
2068 maximum values for the new range. One approach is to figure
2069 out all the variations of range combinations and do the
2070 operations.
2072 However, this involves several calls to compare_values and it
2073 is pretty convoluted. It's simpler to do the 4 operations
2074 (MIN0 OP MIN1, MIN0 OP MAX1, MAX0 OP MIN1 and MAX0 OP MAX0 OP
2075 MAX1) and then figure the smallest and largest values to form
2076 the new range. */
2077 gcc_assert (code == MULT_EXPR
2078 || code == TRUNC_DIV_EXPR
2079 || code == FLOOR_DIV_EXPR
2080 || code == CEIL_DIV_EXPR
2081 || code == EXACT_DIV_EXPR
2082 || code == ROUND_DIV_EXPR
2083 || code == RSHIFT_EXPR
2084 || code == LSHIFT_EXPR);
2085 gcc_assert ((vr0->type == VR_RANGE
2086 || (code == MULT_EXPR && vr0->type == VR_ANTI_RANGE))
2087 && vr0->type == vr1->type);
2089 type = vr0->type;
2091 /* Compute the 4 cross operations. */
2092 sop = false;
2093 val[0] = vrp_int_const_binop (code, vr0->min, vr1->min);
2094 if (val[0] == NULL_TREE)
2095 sop = true;
2097 if (vr1->max == vr1->min)
2098 val[1] = NULL_TREE;
2099 else
2101 val[1] = vrp_int_const_binop (code, vr0->min, vr1->max);
2102 if (val[1] == NULL_TREE)
2103 sop = true;
2106 if (vr0->max == vr0->min)
2107 val[2] = NULL_TREE;
2108 else
2110 val[2] = vrp_int_const_binop (code, vr0->max, vr1->min);
2111 if (val[2] == NULL_TREE)
2112 sop = true;
2115 if (vr0->min == vr0->max || vr1->min == vr1->max)
2116 val[3] = NULL_TREE;
2117 else
2119 val[3] = vrp_int_const_binop (code, vr0->max, vr1->max);
2120 if (val[3] == NULL_TREE)
2121 sop = true;
2124 if (sop)
2126 set_value_range_to_varying (vr);
2127 return;
2130 /* Set MIN to the minimum of VAL[i] and MAX to the maximum
2131 of VAL[i]. */
2132 min = val[0];
2133 max = val[0];
2134 for (i = 1; i < 4; i++)
2136 if (!is_gimple_min_invariant (min)
2137 || (TREE_OVERFLOW (min) && !is_overflow_infinity (min))
2138 || !is_gimple_min_invariant (max)
2139 || (TREE_OVERFLOW (max) && !is_overflow_infinity (max)))
2140 break;
2142 if (val[i])
2144 if (!is_gimple_min_invariant (val[i])
2145 || (TREE_OVERFLOW (val[i])
2146 && !is_overflow_infinity (val[i])))
2148 /* If we found an overflowed value, set MIN and MAX
2149 to it so that we set the resulting range to
2150 VARYING. */
2151 min = max = val[i];
2152 break;
2155 if (compare_values (val[i], min) == -1)
2156 min = val[i];
2158 if (compare_values (val[i], max) == 1)
2159 max = val[i];
2163 /* If either MIN or MAX overflowed, then set the resulting range to
2164 VARYING. But we do accept an overflow infinity
2165 representation. */
2166 if (min == NULL_TREE
2167 || !is_gimple_min_invariant (min)
2168 || (TREE_OVERFLOW (min) && !is_overflow_infinity (min))
2169 || max == NULL_TREE
2170 || !is_gimple_min_invariant (max)
2171 || (TREE_OVERFLOW (max) && !is_overflow_infinity (max)))
2173 set_value_range_to_varying (vr);
2174 return;
2177 /* We punt if:
2178 1) [-INF, +INF]
2179 2) [-INF, +-INF(OVF)]
2180 3) [+-INF(OVF), +INF]
2181 4) [+-INF(OVF), +-INF(OVF)]
2182 We learn nothing when we have INF and INF(OVF) on both sides.
2183 Note that we do accept [-INF, -INF] and [+INF, +INF] without
2184 overflow. */
2185 if ((vrp_val_is_min (min) || is_overflow_infinity (min))
2186 && (vrp_val_is_max (max) || is_overflow_infinity (max)))
2188 set_value_range_to_varying (vr);
2189 return;
2192 cmp = compare_values (min, max);
2193 if (cmp == -2 || cmp == 1)
2195 /* If the new range has its limits swapped around (MIN > MAX),
2196 then the operation caused one of them to wrap around, mark
2197 the new range VARYING. */
2198 set_value_range_to_varying (vr);
2200 else
2201 set_value_range (vr, type, min, max, NULL);
2204 /* Extract range information from a binary operation CODE based on
2205 the ranges of each of its operands *VR0 and *VR1 with resulting
2206 type EXPR_TYPE. The resulting range is stored in *VR. */
2208 static void
2209 extract_range_from_binary_expr_1 (value_range *vr,
2210 enum tree_code code, tree expr_type,
2211 value_range *vr0_, value_range *vr1_)
2213 value_range vr0 = *vr0_, vr1 = *vr1_;
2214 value_range vrtem0 = VR_INITIALIZER, vrtem1 = VR_INITIALIZER;
2215 enum value_range_type type;
2216 tree min = NULL_TREE, max = NULL_TREE;
2217 int cmp;
2219 if (!INTEGRAL_TYPE_P (expr_type)
2220 && !POINTER_TYPE_P (expr_type))
2222 set_value_range_to_varying (vr);
2223 return;
2226 /* Not all binary expressions can be applied to ranges in a
2227 meaningful way. Handle only arithmetic operations. */
2228 if (code != PLUS_EXPR
2229 && code != MINUS_EXPR
2230 && code != POINTER_PLUS_EXPR
2231 && code != MULT_EXPR
2232 && code != TRUNC_DIV_EXPR
2233 && code != FLOOR_DIV_EXPR
2234 && code != CEIL_DIV_EXPR
2235 && code != EXACT_DIV_EXPR
2236 && code != ROUND_DIV_EXPR
2237 && code != TRUNC_MOD_EXPR
2238 && code != RSHIFT_EXPR
2239 && code != LSHIFT_EXPR
2240 && code != MIN_EXPR
2241 && code != MAX_EXPR
2242 && code != BIT_AND_EXPR
2243 && code != BIT_IOR_EXPR
2244 && code != BIT_XOR_EXPR)
2246 set_value_range_to_varying (vr);
2247 return;
2250 /* If both ranges are UNDEFINED, so is the result. */
2251 if (vr0.type == VR_UNDEFINED && vr1.type == VR_UNDEFINED)
2253 set_value_range_to_undefined (vr);
2254 return;
2256 /* If one of the ranges is UNDEFINED drop it to VARYING for the following
2257 code. At some point we may want to special-case operations that
2258 have UNDEFINED result for all or some value-ranges of the not UNDEFINED
2259 operand. */
2260 else if (vr0.type == VR_UNDEFINED)
2261 set_value_range_to_varying (&vr0);
2262 else if (vr1.type == VR_UNDEFINED)
2263 set_value_range_to_varying (&vr1);
2265 /* Now canonicalize anti-ranges to ranges when they are not symbolic
2266 and express ~[] op X as ([]' op X) U ([]'' op X). */
2267 if (vr0.type == VR_ANTI_RANGE
2268 && ranges_from_anti_range (&vr0, &vrtem0, &vrtem1))
2270 extract_range_from_binary_expr_1 (vr, code, expr_type, &vrtem0, vr1_);
2271 if (vrtem1.type != VR_UNDEFINED)
2273 value_range vrres = VR_INITIALIZER;
2274 extract_range_from_binary_expr_1 (&vrres, code, expr_type,
2275 &vrtem1, vr1_);
2276 vrp_meet (vr, &vrres);
2278 return;
2280 /* Likewise for X op ~[]. */
2281 if (vr1.type == VR_ANTI_RANGE
2282 && ranges_from_anti_range (&vr1, &vrtem0, &vrtem1))
2284 extract_range_from_binary_expr_1 (vr, code, expr_type, vr0_, &vrtem0);
2285 if (vrtem1.type != VR_UNDEFINED)
2287 value_range vrres = VR_INITIALIZER;
2288 extract_range_from_binary_expr_1 (&vrres, code, expr_type,
2289 vr0_, &vrtem1);
2290 vrp_meet (vr, &vrres);
2292 return;
2295 /* The type of the resulting value range defaults to VR0.TYPE. */
2296 type = vr0.type;
2298 /* Refuse to operate on VARYING ranges, ranges of different kinds
2299 and symbolic ranges. As an exception, we allow BIT_{AND,IOR}
2300 because we may be able to derive a useful range even if one of
2301 the operands is VR_VARYING or symbolic range. Similarly for
2302 divisions, MIN/MAX and PLUS/MINUS.
2304 TODO, we may be able to derive anti-ranges in some cases. */
2305 if (code != BIT_AND_EXPR
2306 && code != BIT_IOR_EXPR
2307 && code != TRUNC_DIV_EXPR
2308 && code != FLOOR_DIV_EXPR
2309 && code != CEIL_DIV_EXPR
2310 && code != EXACT_DIV_EXPR
2311 && code != ROUND_DIV_EXPR
2312 && code != TRUNC_MOD_EXPR
2313 && code != MIN_EXPR
2314 && code != MAX_EXPR
2315 && code != PLUS_EXPR
2316 && code != MINUS_EXPR
2317 && code != RSHIFT_EXPR
2318 && (vr0.type == VR_VARYING
2319 || vr1.type == VR_VARYING
2320 || vr0.type != vr1.type
2321 || symbolic_range_p (&vr0)
2322 || symbolic_range_p (&vr1)))
2324 set_value_range_to_varying (vr);
2325 return;
2328 /* Now evaluate the expression to determine the new range. */
2329 if (POINTER_TYPE_P (expr_type))
2331 if (code == MIN_EXPR || code == MAX_EXPR)
2333 /* For MIN/MAX expressions with pointers, we only care about
2334 nullness, if both are non null, then the result is nonnull.
2335 If both are null, then the result is null. Otherwise they
2336 are varying. */
2337 if (range_is_nonnull (&vr0) && range_is_nonnull (&vr1))
2338 set_value_range_to_nonnull (vr, expr_type);
2339 else if (range_is_null (&vr0) && range_is_null (&vr1))
2340 set_value_range_to_null (vr, expr_type);
2341 else
2342 set_value_range_to_varying (vr);
2344 else if (code == POINTER_PLUS_EXPR)
2346 /* For pointer types, we are really only interested in asserting
2347 whether the expression evaluates to non-NULL. */
2348 if (range_is_nonnull (&vr0) || range_is_nonnull (&vr1))
2349 set_value_range_to_nonnull (vr, expr_type);
2350 else if (range_is_null (&vr0) && range_is_null (&vr1))
2351 set_value_range_to_null (vr, expr_type);
2352 else
2353 set_value_range_to_varying (vr);
2355 else if (code == BIT_AND_EXPR)
2357 /* For pointer types, we are really only interested in asserting
2358 whether the expression evaluates to non-NULL. */
2359 if (range_is_nonnull (&vr0) && range_is_nonnull (&vr1))
2360 set_value_range_to_nonnull (vr, expr_type);
2361 else if (range_is_null (&vr0) || range_is_null (&vr1))
2362 set_value_range_to_null (vr, expr_type);
2363 else
2364 set_value_range_to_varying (vr);
2366 else
2367 set_value_range_to_varying (vr);
2369 return;
2372 /* For integer ranges, apply the operation to each end of the
2373 range and see what we end up with. */
2374 if (code == PLUS_EXPR || code == MINUS_EXPR)
2376 const bool minus_p = (code == MINUS_EXPR);
2377 tree min_op0 = vr0.min;
2378 tree min_op1 = minus_p ? vr1.max : vr1.min;
2379 tree max_op0 = vr0.max;
2380 tree max_op1 = minus_p ? vr1.min : vr1.max;
2381 tree sym_min_op0 = NULL_TREE;
2382 tree sym_min_op1 = NULL_TREE;
2383 tree sym_max_op0 = NULL_TREE;
2384 tree sym_max_op1 = NULL_TREE;
2385 bool neg_min_op0, neg_min_op1, neg_max_op0, neg_max_op1;
2387 /* If we have a PLUS or MINUS with two VR_RANGEs, either constant or
2388 single-symbolic ranges, try to compute the precise resulting range,
2389 but only if we know that this resulting range will also be constant
2390 or single-symbolic. */
2391 if (vr0.type == VR_RANGE && vr1.type == VR_RANGE
2392 && (TREE_CODE (min_op0) == INTEGER_CST
2393 || (sym_min_op0
2394 = get_single_symbol (min_op0, &neg_min_op0, &min_op0)))
2395 && (TREE_CODE (min_op1) == INTEGER_CST
2396 || (sym_min_op1
2397 = get_single_symbol (min_op1, &neg_min_op1, &min_op1)))
2398 && (!(sym_min_op0 && sym_min_op1)
2399 || (sym_min_op0 == sym_min_op1
2400 && neg_min_op0 == (minus_p ? neg_min_op1 : !neg_min_op1)))
2401 && (TREE_CODE (max_op0) == INTEGER_CST
2402 || (sym_max_op0
2403 = get_single_symbol (max_op0, &neg_max_op0, &max_op0)))
2404 && (TREE_CODE (max_op1) == INTEGER_CST
2405 || (sym_max_op1
2406 = get_single_symbol (max_op1, &neg_max_op1, &max_op1)))
2407 && (!(sym_max_op0 && sym_max_op1)
2408 || (sym_max_op0 == sym_max_op1
2409 && neg_max_op0 == (minus_p ? neg_max_op1 : !neg_max_op1))))
2411 const signop sgn = TYPE_SIGN (expr_type);
2412 const unsigned int prec = TYPE_PRECISION (expr_type);
2413 wide_int type_min, type_max, wmin, wmax;
2414 int min_ovf = 0;
2415 int max_ovf = 0;
2417 /* Get the lower and upper bounds of the type. */
2418 if (TYPE_OVERFLOW_WRAPS (expr_type))
2420 type_min = wi::min_value (prec, sgn);
2421 type_max = wi::max_value (prec, sgn);
2423 else
2425 type_min = vrp_val_min (expr_type);
2426 type_max = vrp_val_max (expr_type);
2429 /* Combine the lower bounds, if any. */
2430 if (min_op0 && min_op1)
2432 if (minus_p)
2434 wmin = wi::sub (min_op0, min_op1);
2436 /* Check for overflow. */
2437 if (wi::cmp (0, min_op1, sgn)
2438 != wi::cmp (wmin, min_op0, sgn))
2439 min_ovf = wi::cmp (min_op0, min_op1, sgn);
2441 else
2443 wmin = wi::add (min_op0, min_op1);
2445 /* Check for overflow. */
2446 if (wi::cmp (min_op1, 0, sgn)
2447 != wi::cmp (wmin, min_op0, sgn))
2448 min_ovf = wi::cmp (min_op0, wmin, sgn);
2451 else if (min_op0)
2452 wmin = min_op0;
2453 else if (min_op1)
2454 wmin = minus_p ? wi::neg (min_op1) : min_op1;
2455 else
2456 wmin = wi::shwi (0, prec);
2458 /* Combine the upper bounds, if any. */
2459 if (max_op0 && max_op1)
2461 if (minus_p)
2463 wmax = wi::sub (max_op0, max_op1);
2465 /* Check for overflow. */
2466 if (wi::cmp (0, max_op1, sgn)
2467 != wi::cmp (wmax, max_op0, sgn))
2468 max_ovf = wi::cmp (max_op0, max_op1, sgn);
2470 else
2472 wmax = wi::add (max_op0, max_op1);
2474 if (wi::cmp (max_op1, 0, sgn)
2475 != wi::cmp (wmax, max_op0, sgn))
2476 max_ovf = wi::cmp (max_op0, wmax, sgn);
2479 else if (max_op0)
2480 wmax = max_op0;
2481 else if (max_op1)
2482 wmax = minus_p ? wi::neg (max_op1) : max_op1;
2483 else
2484 wmax = wi::shwi (0, prec);
2486 /* Check for type overflow. */
2487 if (min_ovf == 0)
2489 if (wi::cmp (wmin, type_min, sgn) == -1)
2490 min_ovf = -1;
2491 else if (wi::cmp (wmin, type_max, sgn) == 1)
2492 min_ovf = 1;
2494 if (max_ovf == 0)
2496 if (wi::cmp (wmax, type_min, sgn) == -1)
2497 max_ovf = -1;
2498 else if (wi::cmp (wmax, type_max, sgn) == 1)
2499 max_ovf = 1;
2502 /* If we have overflow for the constant part and the resulting
2503 range will be symbolic, drop to VR_VARYING. */
2504 if ((min_ovf && sym_min_op0 != sym_min_op1)
2505 || (max_ovf && sym_max_op0 != sym_max_op1))
2507 set_value_range_to_varying (vr);
2508 return;
2511 if (TYPE_OVERFLOW_WRAPS (expr_type))
2513 /* If overflow wraps, truncate the values and adjust the
2514 range kind and bounds appropriately. */
2515 wide_int tmin = wide_int::from (wmin, prec, sgn);
2516 wide_int tmax = wide_int::from (wmax, prec, sgn);
2517 if (min_ovf == max_ovf)
2519 /* No overflow or both overflow or underflow. The
2520 range kind stays VR_RANGE. */
2521 min = wide_int_to_tree (expr_type, tmin);
2522 max = wide_int_to_tree (expr_type, tmax);
2524 else if (min_ovf == -1 && max_ovf == 1)
2526 /* Underflow and overflow, drop to VR_VARYING. */
2527 set_value_range_to_varying (vr);
2528 return;
2530 else
2532 /* Min underflow or max overflow. The range kind
2533 changes to VR_ANTI_RANGE. */
2534 bool covers = false;
2535 wide_int tem = tmin;
2536 gcc_assert ((min_ovf == -1 && max_ovf == 0)
2537 || (max_ovf == 1 && min_ovf == 0));
2538 type = VR_ANTI_RANGE;
2539 tmin = tmax + 1;
2540 if (wi::cmp (tmin, tmax, sgn) < 0)
2541 covers = true;
2542 tmax = tem - 1;
2543 if (wi::cmp (tmax, tem, sgn) > 0)
2544 covers = true;
2545 /* If the anti-range would cover nothing, drop to varying.
2546 Likewise if the anti-range bounds are outside of the
2547 types values. */
2548 if (covers || wi::cmp (tmin, tmax, sgn) > 0)
2550 set_value_range_to_varying (vr);
2551 return;
2553 min = wide_int_to_tree (expr_type, tmin);
2554 max = wide_int_to_tree (expr_type, tmax);
2557 else
2559 /* If overflow does not wrap, saturate to the types min/max
2560 value. */
2561 if (min_ovf == -1)
2563 if (needs_overflow_infinity (expr_type)
2564 && supports_overflow_infinity (expr_type))
2565 min = negative_overflow_infinity (expr_type);
2566 else
2567 min = wide_int_to_tree (expr_type, type_min);
2569 else if (min_ovf == 1)
2571 if (needs_overflow_infinity (expr_type)
2572 && supports_overflow_infinity (expr_type))
2573 min = positive_overflow_infinity (expr_type);
2574 else
2575 min = wide_int_to_tree (expr_type, type_max);
2577 else
2578 min = wide_int_to_tree (expr_type, wmin);
2580 if (max_ovf == -1)
2582 if (needs_overflow_infinity (expr_type)
2583 && supports_overflow_infinity (expr_type))
2584 max = negative_overflow_infinity (expr_type);
2585 else
2586 max = wide_int_to_tree (expr_type, type_min);
2588 else if (max_ovf == 1)
2590 if (needs_overflow_infinity (expr_type)
2591 && supports_overflow_infinity (expr_type))
2592 max = positive_overflow_infinity (expr_type);
2593 else
2594 max = wide_int_to_tree (expr_type, type_max);
2596 else
2597 max = wide_int_to_tree (expr_type, wmax);
2600 if (needs_overflow_infinity (expr_type)
2601 && supports_overflow_infinity (expr_type))
2603 if ((min_op0 && is_negative_overflow_infinity (min_op0))
2604 || (min_op1
2605 && (minus_p
2606 ? is_positive_overflow_infinity (min_op1)
2607 : is_negative_overflow_infinity (min_op1))))
2608 min = negative_overflow_infinity (expr_type);
2609 if ((max_op0 && is_positive_overflow_infinity (max_op0))
2610 || (max_op1
2611 && (minus_p
2612 ? is_negative_overflow_infinity (max_op1)
2613 : is_positive_overflow_infinity (max_op1))))
2614 max = positive_overflow_infinity (expr_type);
2617 /* If the result lower bound is constant, we're done;
2618 otherwise, build the symbolic lower bound. */
2619 if (sym_min_op0 == sym_min_op1)
2621 else if (sym_min_op0)
2622 min = build_symbolic_expr (expr_type, sym_min_op0,
2623 neg_min_op0, min);
2624 else if (sym_min_op1)
2625 min = build_symbolic_expr (expr_type, sym_min_op1,
2626 neg_min_op1 ^ minus_p, min);
2628 /* Likewise for the upper bound. */
2629 if (sym_max_op0 == sym_max_op1)
2631 else if (sym_max_op0)
2632 max = build_symbolic_expr (expr_type, sym_max_op0,
2633 neg_max_op0, max);
2634 else if (sym_max_op1)
2635 max = build_symbolic_expr (expr_type, sym_max_op1,
2636 neg_max_op1 ^ minus_p, max);
2638 else
2640 /* For other cases, for example if we have a PLUS_EXPR with two
2641 VR_ANTI_RANGEs, drop to VR_VARYING. It would take more effort
2642 to compute a precise range for such a case.
2643 ??? General even mixed range kind operations can be expressed
2644 by for example transforming ~[3, 5] + [1, 2] to range-only
2645 operations and a union primitive:
2646 [-INF, 2] + [1, 2] U [5, +INF] + [1, 2]
2647 [-INF+1, 4] U [6, +INF(OVF)]
2648 though usually the union is not exactly representable with
2649 a single range or anti-range as the above is
2650 [-INF+1, +INF(OVF)] intersected with ~[5, 5]
2651 but one could use a scheme similar to equivalences for this. */
2652 set_value_range_to_varying (vr);
2653 return;
2656 else if (code == MIN_EXPR
2657 || code == MAX_EXPR)
2659 if (vr0.type == VR_RANGE
2660 && !symbolic_range_p (&vr0))
2662 type = VR_RANGE;
2663 if (vr1.type == VR_RANGE
2664 && !symbolic_range_p (&vr1))
2666 /* For operations that make the resulting range directly
2667 proportional to the original ranges, apply the operation to
2668 the same end of each range. */
2669 min = vrp_int_const_binop (code, vr0.min, vr1.min);
2670 max = vrp_int_const_binop (code, vr0.max, vr1.max);
2672 else if (code == MIN_EXPR)
2674 min = vrp_val_min (expr_type);
2675 max = vr0.max;
2677 else if (code == MAX_EXPR)
2679 min = vr0.min;
2680 max = vrp_val_max (expr_type);
2683 else if (vr1.type == VR_RANGE
2684 && !symbolic_range_p (&vr1))
2686 type = VR_RANGE;
2687 if (code == MIN_EXPR)
2689 min = vrp_val_min (expr_type);
2690 max = vr1.max;
2692 else if (code == MAX_EXPR)
2694 min = vr1.min;
2695 max = vrp_val_max (expr_type);
2698 else
2700 set_value_range_to_varying (vr);
2701 return;
2704 else if (code == MULT_EXPR)
2706 /* Fancy code so that with unsigned, [-3,-1]*[-3,-1] does not
2707 drop to varying. This test requires 2*prec bits if both
2708 operands are signed and 2*prec + 2 bits if either is not. */
2710 signop sign = TYPE_SIGN (expr_type);
2711 unsigned int prec = TYPE_PRECISION (expr_type);
2713 if (range_int_cst_p (&vr0)
2714 && range_int_cst_p (&vr1)
2715 && TYPE_OVERFLOW_WRAPS (expr_type))
2717 typedef FIXED_WIDE_INT (WIDE_INT_MAX_PRECISION * 2) vrp_int;
2718 typedef generic_wide_int
2719 <wi::extended_tree <WIDE_INT_MAX_PRECISION * 2> > vrp_int_cst;
2720 vrp_int sizem1 = wi::mask <vrp_int> (prec, false);
2721 vrp_int size = sizem1 + 1;
2723 /* Extend the values using the sign of the result to PREC2.
2724 From here on out, everthing is just signed math no matter
2725 what the input types were. */
2726 vrp_int min0 = vrp_int_cst (vr0.min);
2727 vrp_int max0 = vrp_int_cst (vr0.max);
2728 vrp_int min1 = vrp_int_cst (vr1.min);
2729 vrp_int max1 = vrp_int_cst (vr1.max);
2730 /* Canonicalize the intervals. */
2731 if (sign == UNSIGNED)
2733 if (wi::ltu_p (size, min0 + max0))
2735 min0 -= size;
2736 max0 -= size;
2739 if (wi::ltu_p (size, min1 + max1))
2741 min1 -= size;
2742 max1 -= size;
2746 vrp_int prod0 = min0 * min1;
2747 vrp_int prod1 = min0 * max1;
2748 vrp_int prod2 = max0 * min1;
2749 vrp_int prod3 = max0 * max1;
2751 /* Sort the 4 products so that min is in prod0 and max is in
2752 prod3. */
2753 /* min0min1 > max0max1 */
2754 if (wi::gts_p (prod0, prod3))
2755 std::swap (prod0, prod3);
2757 /* min0max1 > max0min1 */
2758 if (wi::gts_p (prod1, prod2))
2759 std::swap (prod1, prod2);
2761 if (wi::gts_p (prod0, prod1))
2762 std::swap (prod0, prod1);
2764 if (wi::gts_p (prod2, prod3))
2765 std::swap (prod2, prod3);
2767 /* diff = max - min. */
2768 prod2 = prod3 - prod0;
2769 if (wi::geu_p (prod2, sizem1))
2771 /* the range covers all values. */
2772 set_value_range_to_varying (vr);
2773 return;
2776 /* The following should handle the wrapping and selecting
2777 VR_ANTI_RANGE for us. */
2778 min = wide_int_to_tree (expr_type, prod0);
2779 max = wide_int_to_tree (expr_type, prod3);
2780 set_and_canonicalize_value_range (vr, VR_RANGE, min, max, NULL);
2781 return;
2784 /* If we have an unsigned MULT_EXPR with two VR_ANTI_RANGEs,
2785 drop to VR_VARYING. It would take more effort to compute a
2786 precise range for such a case. For example, if we have
2787 op0 == 65536 and op1 == 65536 with their ranges both being
2788 ~[0,0] on a 32-bit machine, we would have op0 * op1 == 0, so
2789 we cannot claim that the product is in ~[0,0]. Note that we
2790 are guaranteed to have vr0.type == vr1.type at this
2791 point. */
2792 if (vr0.type == VR_ANTI_RANGE
2793 && !TYPE_OVERFLOW_UNDEFINED (expr_type))
2795 set_value_range_to_varying (vr);
2796 return;
2799 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
2800 return;
2802 else if (code == RSHIFT_EXPR
2803 || code == LSHIFT_EXPR)
2805 /* If we have a RSHIFT_EXPR with any shift values outside [0..prec-1],
2806 then drop to VR_VARYING. Outside of this range we get undefined
2807 behavior from the shift operation. We cannot even trust
2808 SHIFT_COUNT_TRUNCATED at this stage, because that applies to rtl
2809 shifts, and the operation at the tree level may be widened. */
2810 if (range_int_cst_p (&vr1)
2811 && compare_tree_int (vr1.min, 0) >= 0
2812 && compare_tree_int (vr1.max, TYPE_PRECISION (expr_type)) == -1)
2814 if (code == RSHIFT_EXPR)
2816 /* Even if vr0 is VARYING or otherwise not usable, we can derive
2817 useful ranges just from the shift count. E.g.
2818 x >> 63 for signed 64-bit x is always [-1, 0]. */
2819 if (vr0.type != VR_RANGE || symbolic_range_p (&vr0))
2821 vr0.type = type = VR_RANGE;
2822 vr0.min = vrp_val_min (expr_type);
2823 vr0.max = vrp_val_max (expr_type);
2825 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
2826 return;
2828 /* We can map lshifts by constants to MULT_EXPR handling. */
2829 else if (code == LSHIFT_EXPR
2830 && range_int_cst_singleton_p (&vr1))
2832 bool saved_flag_wrapv;
2833 value_range vr1p = VR_INITIALIZER;
2834 vr1p.type = VR_RANGE;
2835 vr1p.min = (wide_int_to_tree
2836 (expr_type,
2837 wi::set_bit_in_zero (tree_to_shwi (vr1.min),
2838 TYPE_PRECISION (expr_type))));
2839 vr1p.max = vr1p.min;
2840 /* We have to use a wrapping multiply though as signed overflow
2841 on lshifts is implementation defined in C89. */
2842 saved_flag_wrapv = flag_wrapv;
2843 flag_wrapv = 1;
2844 extract_range_from_binary_expr_1 (vr, MULT_EXPR, expr_type,
2845 &vr0, &vr1p);
2846 flag_wrapv = saved_flag_wrapv;
2847 return;
2849 else if (code == LSHIFT_EXPR
2850 && range_int_cst_p (&vr0))
2852 int prec = TYPE_PRECISION (expr_type);
2853 int overflow_pos = prec;
2854 int bound_shift;
2855 wide_int low_bound, high_bound;
2856 bool uns = TYPE_UNSIGNED (expr_type);
2857 bool in_bounds = false;
2859 if (!uns)
2860 overflow_pos -= 1;
2862 bound_shift = overflow_pos - tree_to_shwi (vr1.max);
2863 /* If bound_shift == HOST_BITS_PER_WIDE_INT, the llshift can
2864 overflow. However, for that to happen, vr1.max needs to be
2865 zero, which means vr1 is a singleton range of zero, which
2866 means it should be handled by the previous LSHIFT_EXPR
2867 if-clause. */
2868 wide_int bound = wi::set_bit_in_zero (bound_shift, prec);
2869 wide_int complement = ~(bound - 1);
2871 if (uns)
2873 low_bound = bound;
2874 high_bound = complement;
2875 if (wi::ltu_p (vr0.max, low_bound))
2877 /* [5, 6] << [1, 2] == [10, 24]. */
2878 /* We're shifting out only zeroes, the value increases
2879 monotonically. */
2880 in_bounds = true;
2882 else if (wi::ltu_p (high_bound, vr0.min))
2884 /* [0xffffff00, 0xffffffff] << [1, 2]
2885 == [0xfffffc00, 0xfffffffe]. */
2886 /* We're shifting out only ones, the value decreases
2887 monotonically. */
2888 in_bounds = true;
2891 else
2893 /* [-1, 1] << [1, 2] == [-4, 4]. */
2894 low_bound = complement;
2895 high_bound = bound;
2896 if (wi::lts_p (vr0.max, high_bound)
2897 && wi::lts_p (low_bound, vr0.min))
2899 /* For non-negative numbers, we're shifting out only
2900 zeroes, the value increases monotonically.
2901 For negative numbers, we're shifting out only ones, the
2902 value decreases monotomically. */
2903 in_bounds = true;
2907 if (in_bounds)
2909 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
2910 return;
2914 set_value_range_to_varying (vr);
2915 return;
2917 else if (code == TRUNC_DIV_EXPR
2918 || code == FLOOR_DIV_EXPR
2919 || code == CEIL_DIV_EXPR
2920 || code == EXACT_DIV_EXPR
2921 || code == ROUND_DIV_EXPR)
2923 if (vr0.type != VR_RANGE || symbolic_range_p (&vr0))
2925 /* For division, if op1 has VR_RANGE but op0 does not, something
2926 can be deduced just from that range. Say [min, max] / [4, max]
2927 gives [min / 4, max / 4] range. */
2928 if (vr1.type == VR_RANGE
2929 && !symbolic_range_p (&vr1)
2930 && range_includes_zero_p (vr1.min, vr1.max) == 0)
2932 vr0.type = type = VR_RANGE;
2933 vr0.min = vrp_val_min (expr_type);
2934 vr0.max = vrp_val_max (expr_type);
2936 else
2938 set_value_range_to_varying (vr);
2939 return;
2943 /* For divisions, if flag_non_call_exceptions is true, we must
2944 not eliminate a division by zero. */
2945 if (cfun->can_throw_non_call_exceptions
2946 && (vr1.type != VR_RANGE
2947 || range_includes_zero_p (vr1.min, vr1.max) != 0))
2949 set_value_range_to_varying (vr);
2950 return;
2953 /* For divisions, if op0 is VR_RANGE, we can deduce a range
2954 even if op1 is VR_VARYING, VR_ANTI_RANGE, symbolic or can
2955 include 0. */
2956 if (vr0.type == VR_RANGE
2957 && (vr1.type != VR_RANGE
2958 || range_includes_zero_p (vr1.min, vr1.max) != 0))
2960 tree zero = build_int_cst (TREE_TYPE (vr0.min), 0);
2961 int cmp;
2963 min = NULL_TREE;
2964 max = NULL_TREE;
2965 if (TYPE_UNSIGNED (expr_type)
2966 || value_range_nonnegative_p (&vr1))
2968 /* For unsigned division or when divisor is known
2969 to be non-negative, the range has to cover
2970 all numbers from 0 to max for positive max
2971 and all numbers from min to 0 for negative min. */
2972 cmp = compare_values (vr0.max, zero);
2973 if (cmp == -1)
2975 /* When vr0.max < 0, vr1.min != 0 and value
2976 ranges for dividend and divisor are available. */
2977 if (vr1.type == VR_RANGE
2978 && !symbolic_range_p (&vr0)
2979 && !symbolic_range_p (&vr1)
2980 && !compare_values (vr1.min, zero))
2981 max = int_const_binop (code, vr0.max, vr1.min);
2982 else
2983 max = zero;
2985 else if (cmp == 0 || cmp == 1)
2986 max = vr0.max;
2987 else
2988 type = VR_VARYING;
2989 cmp = compare_values (vr0.min, zero);
2990 if (cmp == 1)
2992 /* For unsigned division when value ranges for dividend
2993 and divisor are available. */
2994 if (vr1.type == VR_RANGE
2995 && !symbolic_range_p (&vr0)
2996 && !symbolic_range_p (&vr1))
2997 min = int_const_binop (code, vr0.min, vr1.max);
2998 else
2999 min = zero;
3001 else if (cmp == 0 || cmp == -1)
3002 min = vr0.min;
3003 else
3004 type = VR_VARYING;
3006 else
3008 /* Otherwise the range is -max .. max or min .. -min
3009 depending on which bound is bigger in absolute value,
3010 as the division can change the sign. */
3011 abs_extent_range (vr, vr0.min, vr0.max);
3012 return;
3014 if (type == VR_VARYING)
3016 set_value_range_to_varying (vr);
3017 return;
3020 else
3022 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
3023 return;
3026 else if (code == TRUNC_MOD_EXPR)
3028 if (range_is_null (&vr1))
3030 set_value_range_to_undefined (vr);
3031 return;
3033 /* ABS (A % B) < ABS (B) and either
3034 0 <= A % B <= A or A <= A % B <= 0. */
3035 type = VR_RANGE;
3036 signop sgn = TYPE_SIGN (expr_type);
3037 unsigned int prec = TYPE_PRECISION (expr_type);
3038 wide_int wmin, wmax, tmp;
3039 wide_int zero = wi::zero (prec);
3040 wide_int one = wi::one (prec);
3041 if (vr1.type == VR_RANGE && !symbolic_range_p (&vr1))
3043 wmax = wi::sub (vr1.max, one);
3044 if (sgn == SIGNED)
3046 tmp = wi::sub (wi::minus_one (prec), vr1.min);
3047 wmax = wi::smax (wmax, tmp);
3050 else
3052 wmax = wi::max_value (prec, sgn);
3053 /* X % INT_MIN may be INT_MAX. */
3054 if (sgn == UNSIGNED)
3055 wmax = wmax - one;
3058 if (sgn == UNSIGNED)
3059 wmin = zero;
3060 else
3062 wmin = -wmax;
3063 if (vr0.type == VR_RANGE && TREE_CODE (vr0.min) == INTEGER_CST)
3065 tmp = vr0.min;
3066 if (wi::gts_p (tmp, zero))
3067 tmp = zero;
3068 wmin = wi::smax (wmin, tmp);
3072 if (vr0.type == VR_RANGE && TREE_CODE (vr0.max) == INTEGER_CST)
3074 tmp = vr0.max;
3075 if (sgn == SIGNED && wi::neg_p (tmp))
3076 tmp = zero;
3077 wmax = wi::min (wmax, tmp, sgn);
3080 min = wide_int_to_tree (expr_type, wmin);
3081 max = wide_int_to_tree (expr_type, wmax);
3083 else if (code == BIT_AND_EXPR || code == BIT_IOR_EXPR || code == BIT_XOR_EXPR)
3085 bool int_cst_range0, int_cst_range1;
3086 wide_int may_be_nonzero0, may_be_nonzero1;
3087 wide_int must_be_nonzero0, must_be_nonzero1;
3089 int_cst_range0 = zero_nonzero_bits_from_vr (expr_type, &vr0,
3090 &may_be_nonzero0,
3091 &must_be_nonzero0);
3092 int_cst_range1 = zero_nonzero_bits_from_vr (expr_type, &vr1,
3093 &may_be_nonzero1,
3094 &must_be_nonzero1);
3096 type = VR_RANGE;
3097 if (code == BIT_AND_EXPR)
3099 min = wide_int_to_tree (expr_type,
3100 must_be_nonzero0 & must_be_nonzero1);
3101 wide_int wmax = may_be_nonzero0 & may_be_nonzero1;
3102 /* If both input ranges contain only negative values we can
3103 truncate the result range maximum to the minimum of the
3104 input range maxima. */
3105 if (int_cst_range0 && int_cst_range1
3106 && tree_int_cst_sgn (vr0.max) < 0
3107 && tree_int_cst_sgn (vr1.max) < 0)
3109 wmax = wi::min (wmax, vr0.max, TYPE_SIGN (expr_type));
3110 wmax = wi::min (wmax, vr1.max, TYPE_SIGN (expr_type));
3112 /* If either input range contains only non-negative values
3113 we can truncate the result range maximum to the respective
3114 maximum of the input range. */
3115 if (int_cst_range0 && tree_int_cst_sgn (vr0.min) >= 0)
3116 wmax = wi::min (wmax, vr0.max, TYPE_SIGN (expr_type));
3117 if (int_cst_range1 && tree_int_cst_sgn (vr1.min) >= 0)
3118 wmax = wi::min (wmax, vr1.max, TYPE_SIGN (expr_type));
3119 max = wide_int_to_tree (expr_type, wmax);
3121 else if (code == BIT_IOR_EXPR)
3123 max = wide_int_to_tree (expr_type,
3124 may_be_nonzero0 | may_be_nonzero1);
3125 wide_int wmin = must_be_nonzero0 | must_be_nonzero1;
3126 /* If the input ranges contain only positive values we can
3127 truncate the minimum of the result range to the maximum
3128 of the input range minima. */
3129 if (int_cst_range0 && int_cst_range1
3130 && tree_int_cst_sgn (vr0.min) >= 0
3131 && tree_int_cst_sgn (vr1.min) >= 0)
3133 wmin = wi::max (wmin, vr0.min, TYPE_SIGN (expr_type));
3134 wmin = wi::max (wmin, vr1.min, TYPE_SIGN (expr_type));
3136 /* If either input range contains only negative values
3137 we can truncate the minimum of the result range to the
3138 respective minimum range. */
3139 if (int_cst_range0 && tree_int_cst_sgn (vr0.max) < 0)
3140 wmin = wi::max (wmin, vr0.min, TYPE_SIGN (expr_type));
3141 if (int_cst_range1 && tree_int_cst_sgn (vr1.max) < 0)
3142 wmin = wi::max (wmin, vr1.min, TYPE_SIGN (expr_type));
3143 min = wide_int_to_tree (expr_type, wmin);
3145 else if (code == BIT_XOR_EXPR)
3147 wide_int result_zero_bits = ((must_be_nonzero0 & must_be_nonzero1)
3148 | ~(may_be_nonzero0 | may_be_nonzero1));
3149 wide_int result_one_bits
3150 = (must_be_nonzero0.and_not (may_be_nonzero1)
3151 | must_be_nonzero1.and_not (may_be_nonzero0));
3152 max = wide_int_to_tree (expr_type, ~result_zero_bits);
3153 min = wide_int_to_tree (expr_type, result_one_bits);
3154 /* If the range has all positive or all negative values the
3155 result is better than VARYING. */
3156 if (tree_int_cst_sgn (min) < 0
3157 || tree_int_cst_sgn (max) >= 0)
3159 else
3160 max = min = NULL_TREE;
3163 else
3164 gcc_unreachable ();
3166 /* If either MIN or MAX overflowed, then set the resulting range to
3167 VARYING. But we do accept an overflow infinity representation. */
3168 if (min == NULL_TREE
3169 || (TREE_OVERFLOW_P (min) && !is_overflow_infinity (min))
3170 || max == NULL_TREE
3171 || (TREE_OVERFLOW_P (max) && !is_overflow_infinity (max)))
3173 set_value_range_to_varying (vr);
3174 return;
3177 /* We punt if:
3178 1) [-INF, +INF]
3179 2) [-INF, +-INF(OVF)]
3180 3) [+-INF(OVF), +INF]
3181 4) [+-INF(OVF), +-INF(OVF)]
3182 We learn nothing when we have INF and INF(OVF) on both sides.
3183 Note that we do accept [-INF, -INF] and [+INF, +INF] without
3184 overflow. */
3185 if ((vrp_val_is_min (min) || is_overflow_infinity (min))
3186 && (vrp_val_is_max (max) || is_overflow_infinity (max)))
3188 set_value_range_to_varying (vr);
3189 return;
3192 cmp = compare_values (min, max);
3193 if (cmp == -2 || cmp == 1)
3195 /* If the new range has its limits swapped around (MIN > MAX),
3196 then the operation caused one of them to wrap around, mark
3197 the new range VARYING. */
3198 set_value_range_to_varying (vr);
3200 else
3201 set_value_range (vr, type, min, max, NULL);
3204 /* Extract range information from a binary expression OP0 CODE OP1 based on
3205 the ranges of each of its operands with resulting type EXPR_TYPE.
3206 The resulting range is stored in *VR. */
3208 static void
3209 extract_range_from_binary_expr (value_range *vr,
3210 enum tree_code code,
3211 tree expr_type, tree op0, tree op1)
3213 value_range vr0 = VR_INITIALIZER;
3214 value_range vr1 = VR_INITIALIZER;
3216 /* Get value ranges for each operand. For constant operands, create
3217 a new value range with the operand to simplify processing. */
3218 if (TREE_CODE (op0) == SSA_NAME)
3219 vr0 = *(get_value_range (op0));
3220 else if (is_gimple_min_invariant (op0))
3221 set_value_range_to_value (&vr0, op0, NULL);
3222 else
3223 set_value_range_to_varying (&vr0);
3225 if (TREE_CODE (op1) == SSA_NAME)
3226 vr1 = *(get_value_range (op1));
3227 else if (is_gimple_min_invariant (op1))
3228 set_value_range_to_value (&vr1, op1, NULL);
3229 else
3230 set_value_range_to_varying (&vr1);
3232 extract_range_from_binary_expr_1 (vr, code, expr_type, &vr0, &vr1);
3234 /* Try harder for PLUS and MINUS if the range of one operand is symbolic
3235 and based on the other operand, for example if it was deduced from a
3236 symbolic comparison. When a bound of the range of the first operand
3237 is invariant, we set the corresponding bound of the new range to INF
3238 in order to avoid recursing on the range of the second operand. */
3239 if (vr->type == VR_VARYING
3240 && (code == PLUS_EXPR || code == MINUS_EXPR)
3241 && TREE_CODE (op1) == SSA_NAME
3242 && vr0.type == VR_RANGE
3243 && symbolic_range_based_on_p (&vr0, op1))
3245 const bool minus_p = (code == MINUS_EXPR);
3246 value_range n_vr1 = VR_INITIALIZER;
3248 /* Try with VR0 and [-INF, OP1]. */
3249 if (is_gimple_min_invariant (minus_p ? vr0.max : vr0.min))
3250 set_value_range (&n_vr1, VR_RANGE, vrp_val_min (expr_type), op1, NULL);
3252 /* Try with VR0 and [OP1, +INF]. */
3253 else if (is_gimple_min_invariant (minus_p ? vr0.min : vr0.max))
3254 set_value_range (&n_vr1, VR_RANGE, op1, vrp_val_max (expr_type), NULL);
3256 /* Try with VR0 and [OP1, OP1]. */
3257 else
3258 set_value_range (&n_vr1, VR_RANGE, op1, op1, NULL);
3260 extract_range_from_binary_expr_1 (vr, code, expr_type, &vr0, &n_vr1);
3263 if (vr->type == VR_VARYING
3264 && (code == PLUS_EXPR || code == MINUS_EXPR)
3265 && TREE_CODE (op0) == SSA_NAME
3266 && vr1.type == VR_RANGE
3267 && symbolic_range_based_on_p (&vr1, op0))
3269 const bool minus_p = (code == MINUS_EXPR);
3270 value_range n_vr0 = VR_INITIALIZER;
3272 /* Try with [-INF, OP0] and VR1. */
3273 if (is_gimple_min_invariant (minus_p ? vr1.max : vr1.min))
3274 set_value_range (&n_vr0, VR_RANGE, vrp_val_min (expr_type), op0, NULL);
3276 /* Try with [OP0, +INF] and VR1. */
3277 else if (is_gimple_min_invariant (minus_p ? vr1.min : vr1.max))
3278 set_value_range (&n_vr0, VR_RANGE, op0, vrp_val_max (expr_type), NULL);
3280 /* Try with [OP0, OP0] and VR1. */
3281 else
3282 set_value_range (&n_vr0, VR_RANGE, op0, op0, NULL);
3284 extract_range_from_binary_expr_1 (vr, code, expr_type, &n_vr0, &vr1);
3288 /* Extract range information from a unary operation CODE based on
3289 the range of its operand *VR0 with type OP0_TYPE with resulting type TYPE.
3290 The resulting range is stored in *VR. */
3292 static void
3293 extract_range_from_unary_expr_1 (value_range *vr,
3294 enum tree_code code, tree type,
3295 value_range *vr0_, tree op0_type)
3297 value_range vr0 = *vr0_, vrtem0 = VR_INITIALIZER, vrtem1 = VR_INITIALIZER;
3299 /* VRP only operates on integral and pointer types. */
3300 if (!(INTEGRAL_TYPE_P (op0_type)
3301 || POINTER_TYPE_P (op0_type))
3302 || !(INTEGRAL_TYPE_P (type)
3303 || POINTER_TYPE_P (type)))
3305 set_value_range_to_varying (vr);
3306 return;
3309 /* If VR0 is UNDEFINED, so is the result. */
3310 if (vr0.type == VR_UNDEFINED)
3312 set_value_range_to_undefined (vr);
3313 return;
3316 /* Handle operations that we express in terms of others. */
3317 if (code == PAREN_EXPR || code == OBJ_TYPE_REF)
3319 /* PAREN_EXPR and OBJ_TYPE_REF are simple copies. */
3320 copy_value_range (vr, &vr0);
3321 return;
3323 else if (code == NEGATE_EXPR)
3325 /* -X is simply 0 - X, so re-use existing code that also handles
3326 anti-ranges fine. */
3327 value_range zero = VR_INITIALIZER;
3328 set_value_range_to_value (&zero, build_int_cst (type, 0), NULL);
3329 extract_range_from_binary_expr_1 (vr, MINUS_EXPR, type, &zero, &vr0);
3330 return;
3332 else if (code == BIT_NOT_EXPR)
3334 /* ~X is simply -1 - X, so re-use existing code that also handles
3335 anti-ranges fine. */
3336 value_range minusone = VR_INITIALIZER;
3337 set_value_range_to_value (&minusone, build_int_cst (type, -1), NULL);
3338 extract_range_from_binary_expr_1 (vr, MINUS_EXPR,
3339 type, &minusone, &vr0);
3340 return;
3343 /* Now canonicalize anti-ranges to ranges when they are not symbolic
3344 and express op ~[] as (op []') U (op []''). */
3345 if (vr0.type == VR_ANTI_RANGE
3346 && ranges_from_anti_range (&vr0, &vrtem0, &vrtem1))
3348 extract_range_from_unary_expr_1 (vr, code, type, &vrtem0, op0_type);
3349 if (vrtem1.type != VR_UNDEFINED)
3351 value_range vrres = VR_INITIALIZER;
3352 extract_range_from_unary_expr_1 (&vrres, code, type,
3353 &vrtem1, op0_type);
3354 vrp_meet (vr, &vrres);
3356 return;
3359 if (CONVERT_EXPR_CODE_P (code))
3361 tree inner_type = op0_type;
3362 tree outer_type = type;
3364 /* If the expression evaluates to a pointer, we are only interested in
3365 determining if it evaluates to NULL [0, 0] or non-NULL (~[0, 0]). */
3366 if (POINTER_TYPE_P (type))
3368 if (range_is_nonnull (&vr0))
3369 set_value_range_to_nonnull (vr, type);
3370 else if (range_is_null (&vr0))
3371 set_value_range_to_null (vr, type);
3372 else
3373 set_value_range_to_varying (vr);
3374 return;
3377 /* If VR0 is varying and we increase the type precision, assume
3378 a full range for the following transformation. */
3379 if (vr0.type == VR_VARYING
3380 && INTEGRAL_TYPE_P (inner_type)
3381 && TYPE_PRECISION (inner_type) < TYPE_PRECISION (outer_type))
3383 vr0.type = VR_RANGE;
3384 vr0.min = TYPE_MIN_VALUE (inner_type);
3385 vr0.max = TYPE_MAX_VALUE (inner_type);
3388 /* If VR0 is a constant range or anti-range and the conversion is
3389 not truncating we can convert the min and max values and
3390 canonicalize the resulting range. Otherwise we can do the
3391 conversion if the size of the range is less than what the
3392 precision of the target type can represent and the range is
3393 not an anti-range. */
3394 if ((vr0.type == VR_RANGE
3395 || vr0.type == VR_ANTI_RANGE)
3396 && TREE_CODE (vr0.min) == INTEGER_CST
3397 && TREE_CODE (vr0.max) == INTEGER_CST
3398 && (!is_overflow_infinity (vr0.min)
3399 || (vr0.type == VR_RANGE
3400 && TYPE_PRECISION (outer_type) > TYPE_PRECISION (inner_type)
3401 && needs_overflow_infinity (outer_type)
3402 && supports_overflow_infinity (outer_type)))
3403 && (!is_overflow_infinity (vr0.max)
3404 || (vr0.type == VR_RANGE
3405 && TYPE_PRECISION (outer_type) > TYPE_PRECISION (inner_type)
3406 && needs_overflow_infinity (outer_type)
3407 && supports_overflow_infinity (outer_type)))
3408 && (TYPE_PRECISION (outer_type) >= TYPE_PRECISION (inner_type)
3409 || (vr0.type == VR_RANGE
3410 && integer_zerop (int_const_binop (RSHIFT_EXPR,
3411 int_const_binop (MINUS_EXPR, vr0.max, vr0.min),
3412 size_int (TYPE_PRECISION (outer_type)))))))
3414 tree new_min, new_max;
3415 if (is_overflow_infinity (vr0.min))
3416 new_min = negative_overflow_infinity (outer_type);
3417 else
3418 new_min = force_fit_type (outer_type, wi::to_widest (vr0.min),
3419 0, false);
3420 if (is_overflow_infinity (vr0.max))
3421 new_max = positive_overflow_infinity (outer_type);
3422 else
3423 new_max = force_fit_type (outer_type, wi::to_widest (vr0.max),
3424 0, false);
3425 set_and_canonicalize_value_range (vr, vr0.type,
3426 new_min, new_max, NULL);
3427 return;
3430 set_value_range_to_varying (vr);
3431 return;
3433 else if (code == ABS_EXPR)
3435 tree min, max;
3436 int cmp;
3438 /* Pass through vr0 in the easy cases. */
3439 if (TYPE_UNSIGNED (type)
3440 || value_range_nonnegative_p (&vr0))
3442 copy_value_range (vr, &vr0);
3443 return;
3446 /* For the remaining varying or symbolic ranges we can't do anything
3447 useful. */
3448 if (vr0.type == VR_VARYING
3449 || symbolic_range_p (&vr0))
3451 set_value_range_to_varying (vr);
3452 return;
3455 /* -TYPE_MIN_VALUE = TYPE_MIN_VALUE with flag_wrapv so we can't get a
3456 useful range. */
3457 if (!TYPE_OVERFLOW_UNDEFINED (type)
3458 && ((vr0.type == VR_RANGE
3459 && vrp_val_is_min (vr0.min))
3460 || (vr0.type == VR_ANTI_RANGE
3461 && !vrp_val_is_min (vr0.min))))
3463 set_value_range_to_varying (vr);
3464 return;
3467 /* ABS_EXPR may flip the range around, if the original range
3468 included negative values. */
3469 if (is_overflow_infinity (vr0.min))
3470 min = positive_overflow_infinity (type);
3471 else if (!vrp_val_is_min (vr0.min))
3472 min = fold_unary_to_constant (code, type, vr0.min);
3473 else if (!needs_overflow_infinity (type))
3474 min = TYPE_MAX_VALUE (type);
3475 else if (supports_overflow_infinity (type))
3476 min = positive_overflow_infinity (type);
3477 else
3479 set_value_range_to_varying (vr);
3480 return;
3483 if (is_overflow_infinity (vr0.max))
3484 max = positive_overflow_infinity (type);
3485 else if (!vrp_val_is_min (vr0.max))
3486 max = fold_unary_to_constant (code, type, vr0.max);
3487 else if (!needs_overflow_infinity (type))
3488 max = TYPE_MAX_VALUE (type);
3489 else if (supports_overflow_infinity (type)
3490 /* We shouldn't generate [+INF, +INF] as set_value_range
3491 doesn't like this and ICEs. */
3492 && !is_positive_overflow_infinity (min))
3493 max = positive_overflow_infinity (type);
3494 else
3496 set_value_range_to_varying (vr);
3497 return;
3500 cmp = compare_values (min, max);
3502 /* If a VR_ANTI_RANGEs contains zero, then we have
3503 ~[-INF, min(MIN, MAX)]. */
3504 if (vr0.type == VR_ANTI_RANGE)
3506 if (range_includes_zero_p (vr0.min, vr0.max) == 1)
3508 /* Take the lower of the two values. */
3509 if (cmp != 1)
3510 max = min;
3512 /* Create ~[-INF, min (abs(MIN), abs(MAX))]
3513 or ~[-INF + 1, min (abs(MIN), abs(MAX))] when
3514 flag_wrapv is set and the original anti-range doesn't include
3515 TYPE_MIN_VALUE, remember -TYPE_MIN_VALUE = TYPE_MIN_VALUE. */
3516 if (TYPE_OVERFLOW_WRAPS (type))
3518 tree type_min_value = TYPE_MIN_VALUE (type);
3520 min = (vr0.min != type_min_value
3521 ? int_const_binop (PLUS_EXPR, type_min_value,
3522 build_int_cst (TREE_TYPE (type_min_value), 1))
3523 : type_min_value);
3525 else
3527 if (overflow_infinity_range_p (&vr0))
3528 min = negative_overflow_infinity (type);
3529 else
3530 min = TYPE_MIN_VALUE (type);
3533 else
3535 /* All else has failed, so create the range [0, INF], even for
3536 flag_wrapv since TYPE_MIN_VALUE is in the original
3537 anti-range. */
3538 vr0.type = VR_RANGE;
3539 min = build_int_cst (type, 0);
3540 if (needs_overflow_infinity (type))
3542 if (supports_overflow_infinity (type))
3543 max = positive_overflow_infinity (type);
3544 else
3546 set_value_range_to_varying (vr);
3547 return;
3550 else
3551 max = TYPE_MAX_VALUE (type);
3555 /* If the range contains zero then we know that the minimum value in the
3556 range will be zero. */
3557 else if (range_includes_zero_p (vr0.min, vr0.max) == 1)
3559 if (cmp == 1)
3560 max = min;
3561 min = build_int_cst (type, 0);
3563 else
3565 /* If the range was reversed, swap MIN and MAX. */
3566 if (cmp == 1)
3567 std::swap (min, max);
3570 cmp = compare_values (min, max);
3571 if (cmp == -2 || cmp == 1)
3573 /* If the new range has its limits swapped around (MIN > MAX),
3574 then the operation caused one of them to wrap around, mark
3575 the new range VARYING. */
3576 set_value_range_to_varying (vr);
3578 else
3579 set_value_range (vr, vr0.type, min, max, NULL);
3580 return;
3583 /* For unhandled operations fall back to varying. */
3584 set_value_range_to_varying (vr);
3585 return;
3589 /* Extract range information from a unary expression CODE OP0 based on
3590 the range of its operand with resulting type TYPE.
3591 The resulting range is stored in *VR. */
3593 static void
3594 extract_range_from_unary_expr (value_range *vr, enum tree_code code,
3595 tree type, tree op0)
3597 value_range vr0 = VR_INITIALIZER;
3599 /* Get value ranges for the operand. For constant operands, create
3600 a new value range with the operand to simplify processing. */
3601 if (TREE_CODE (op0) == SSA_NAME)
3602 vr0 = *(get_value_range (op0));
3603 else if (is_gimple_min_invariant (op0))
3604 set_value_range_to_value (&vr0, op0, NULL);
3605 else
3606 set_value_range_to_varying (&vr0);
3608 extract_range_from_unary_expr_1 (vr, code, type, &vr0, TREE_TYPE (op0));
3612 /* Extract range information from a conditional expression STMT based on
3613 the ranges of each of its operands and the expression code. */
3615 static void
3616 extract_range_from_cond_expr (value_range *vr, gassign *stmt)
3618 tree op0, op1;
3619 value_range vr0 = VR_INITIALIZER;
3620 value_range vr1 = VR_INITIALIZER;
3622 /* Get value ranges for each operand. For constant operands, create
3623 a new value range with the operand to simplify processing. */
3624 op0 = gimple_assign_rhs2 (stmt);
3625 if (TREE_CODE (op0) == SSA_NAME)
3626 vr0 = *(get_value_range (op0));
3627 else if (is_gimple_min_invariant (op0))
3628 set_value_range_to_value (&vr0, op0, NULL);
3629 else
3630 set_value_range_to_varying (&vr0);
3632 op1 = gimple_assign_rhs3 (stmt);
3633 if (TREE_CODE (op1) == SSA_NAME)
3634 vr1 = *(get_value_range (op1));
3635 else if (is_gimple_min_invariant (op1))
3636 set_value_range_to_value (&vr1, op1, NULL);
3637 else
3638 set_value_range_to_varying (&vr1);
3640 /* The resulting value range is the union of the operand ranges */
3641 copy_value_range (vr, &vr0);
3642 vrp_meet (vr, &vr1);
3646 /* Extract range information from a comparison expression EXPR based
3647 on the range of its operand and the expression code. */
3649 static void
3650 extract_range_from_comparison (value_range *vr, enum tree_code code,
3651 tree type, tree op0, tree op1)
3653 bool sop = false;
3654 tree val;
3656 val = vrp_evaluate_conditional_warnv_with_ops (code, op0, op1, false, &sop,
3657 NULL);
3659 /* A disadvantage of using a special infinity as an overflow
3660 representation is that we lose the ability to record overflow
3661 when we don't have an infinity. So we have to ignore a result
3662 which relies on overflow. */
3664 if (val && !is_overflow_infinity (val) && !sop)
3666 /* Since this expression was found on the RHS of an assignment,
3667 its type may be different from _Bool. Convert VAL to EXPR's
3668 type. */
3669 val = fold_convert (type, val);
3670 if (is_gimple_min_invariant (val))
3671 set_value_range_to_value (vr, val, vr->equiv);
3672 else
3673 set_value_range (vr, VR_RANGE, val, val, vr->equiv);
3675 else
3676 /* The result of a comparison is always true or false. */
3677 set_value_range_to_truthvalue (vr, type);
3680 /* Helper function for simplify_internal_call_using_ranges and
3681 extract_range_basic. Return true if OP0 SUBCODE OP1 for
3682 SUBCODE {PLUS,MINUS,MULT}_EXPR is known to never overflow or
3683 always overflow. Set *OVF to true if it is known to always
3684 overflow. */
3686 static bool
3687 check_for_binary_op_overflow (enum tree_code subcode, tree type,
3688 tree op0, tree op1, bool *ovf)
3690 value_range vr0 = VR_INITIALIZER;
3691 value_range vr1 = VR_INITIALIZER;
3692 if (TREE_CODE (op0) == SSA_NAME)
3693 vr0 = *get_value_range (op0);
3694 else if (TREE_CODE (op0) == INTEGER_CST)
3695 set_value_range_to_value (&vr0, op0, NULL);
3696 else
3697 set_value_range_to_varying (&vr0);
3699 if (TREE_CODE (op1) == SSA_NAME)
3700 vr1 = *get_value_range (op1);
3701 else if (TREE_CODE (op1) == INTEGER_CST)
3702 set_value_range_to_value (&vr1, op1, NULL);
3703 else
3704 set_value_range_to_varying (&vr1);
3706 if (!range_int_cst_p (&vr0)
3707 || TREE_OVERFLOW (vr0.min)
3708 || TREE_OVERFLOW (vr0.max))
3710 vr0.min = vrp_val_min (TREE_TYPE (op0));
3711 vr0.max = vrp_val_max (TREE_TYPE (op0));
3713 if (!range_int_cst_p (&vr1)
3714 || TREE_OVERFLOW (vr1.min)
3715 || TREE_OVERFLOW (vr1.max))
3717 vr1.min = vrp_val_min (TREE_TYPE (op1));
3718 vr1.max = vrp_val_max (TREE_TYPE (op1));
3720 *ovf = arith_overflowed_p (subcode, type, vr0.min,
3721 subcode == MINUS_EXPR ? vr1.max : vr1.min);
3722 if (arith_overflowed_p (subcode, type, vr0.max,
3723 subcode == MINUS_EXPR ? vr1.min : vr1.max) != *ovf)
3724 return false;
3725 if (subcode == MULT_EXPR)
3727 if (arith_overflowed_p (subcode, type, vr0.min, vr1.max) != *ovf
3728 || arith_overflowed_p (subcode, type, vr0.max, vr1.min) != *ovf)
3729 return false;
3731 if (*ovf)
3733 /* So far we found that there is an overflow on the boundaries.
3734 That doesn't prove that there is an overflow even for all values
3735 in between the boundaries. For that compute widest_int range
3736 of the result and see if it doesn't overlap the range of
3737 type. */
3738 widest_int wmin, wmax;
3739 widest_int w[4];
3740 int i;
3741 w[0] = wi::to_widest (vr0.min);
3742 w[1] = wi::to_widest (vr0.max);
3743 w[2] = wi::to_widest (vr1.min);
3744 w[3] = wi::to_widest (vr1.max);
3745 for (i = 0; i < 4; i++)
3747 widest_int wt;
3748 switch (subcode)
3750 case PLUS_EXPR:
3751 wt = wi::add (w[i & 1], w[2 + (i & 2) / 2]);
3752 break;
3753 case MINUS_EXPR:
3754 wt = wi::sub (w[i & 1], w[2 + (i & 2) / 2]);
3755 break;
3756 case MULT_EXPR:
3757 wt = wi::mul (w[i & 1], w[2 + (i & 2) / 2]);
3758 break;
3759 default:
3760 gcc_unreachable ();
3762 if (i == 0)
3764 wmin = wt;
3765 wmax = wt;
3767 else
3769 wmin = wi::smin (wmin, wt);
3770 wmax = wi::smax (wmax, wt);
3773 /* The result of op0 CODE op1 is known to be in range
3774 [wmin, wmax]. */
3775 widest_int wtmin = wi::to_widest (vrp_val_min (type));
3776 widest_int wtmax = wi::to_widest (vrp_val_max (type));
3777 /* If all values in [wmin, wmax] are smaller than
3778 [wtmin, wtmax] or all are larger than [wtmin, wtmax],
3779 the arithmetic operation will always overflow. */
3780 if (wi::lts_p (wmax, wtmin) || wi::gts_p (wmin, wtmax))
3781 return true;
3782 return false;
3784 return true;
3787 /* Try to derive a nonnegative or nonzero range out of STMT relying
3788 primarily on generic routines in fold in conjunction with range data.
3789 Store the result in *VR */
3791 static void
3792 extract_range_basic (value_range *vr, gimple *stmt)
3794 bool sop = false;
3795 tree type = gimple_expr_type (stmt);
3797 if (gimple_call_builtin_p (stmt, BUILT_IN_NORMAL))
3799 tree fndecl = gimple_call_fndecl (stmt), arg;
3800 int mini, maxi, zerov = 0, prec;
3802 switch (DECL_FUNCTION_CODE (fndecl))
3804 case BUILT_IN_CONSTANT_P:
3805 /* If the call is __builtin_constant_p and the argument is a
3806 function parameter resolve it to false. This avoids bogus
3807 array bound warnings.
3808 ??? We could do this as early as inlining is finished. */
3809 arg = gimple_call_arg (stmt, 0);
3810 if (TREE_CODE (arg) == SSA_NAME
3811 && SSA_NAME_IS_DEFAULT_DEF (arg)
3812 && TREE_CODE (SSA_NAME_VAR (arg)) == PARM_DECL)
3814 set_value_range_to_null (vr, type);
3815 return;
3817 break;
3818 /* Both __builtin_ffs* and __builtin_popcount return
3819 [0, prec]. */
3820 CASE_INT_FN (BUILT_IN_FFS):
3821 CASE_INT_FN (BUILT_IN_POPCOUNT):
3822 arg = gimple_call_arg (stmt, 0);
3823 prec = TYPE_PRECISION (TREE_TYPE (arg));
3824 mini = 0;
3825 maxi = prec;
3826 if (TREE_CODE (arg) == SSA_NAME)
3828 value_range *vr0 = get_value_range (arg);
3829 /* If arg is non-zero, then ffs or popcount
3830 are non-zero. */
3831 if (((vr0->type == VR_RANGE
3832 && range_includes_zero_p (vr0->min, vr0->max) == 0)
3833 || (vr0->type == VR_ANTI_RANGE
3834 && range_includes_zero_p (vr0->min, vr0->max) == 1))
3835 && !is_overflow_infinity (vr0->min)
3836 && !is_overflow_infinity (vr0->max))
3837 mini = 1;
3838 /* If some high bits are known to be zero,
3839 we can decrease the maximum. */
3840 if (vr0->type == VR_RANGE
3841 && TREE_CODE (vr0->max) == INTEGER_CST
3842 && !operand_less_p (vr0->min,
3843 build_zero_cst (TREE_TYPE (vr0->min)))
3844 && !is_overflow_infinity (vr0->max))
3845 maxi = tree_floor_log2 (vr0->max) + 1;
3847 goto bitop_builtin;
3848 /* __builtin_parity* returns [0, 1]. */
3849 CASE_INT_FN (BUILT_IN_PARITY):
3850 mini = 0;
3851 maxi = 1;
3852 goto bitop_builtin;
3853 /* __builtin_c[lt]z* return [0, prec-1], except for
3854 when the argument is 0, but that is undefined behavior.
3855 On many targets where the CLZ RTL or optab value is defined
3856 for 0 the value is prec, so include that in the range
3857 by default. */
3858 CASE_INT_FN (BUILT_IN_CLZ):
3859 arg = gimple_call_arg (stmt, 0);
3860 prec = TYPE_PRECISION (TREE_TYPE (arg));
3861 mini = 0;
3862 maxi = prec;
3863 if (optab_handler (clz_optab, TYPE_MODE (TREE_TYPE (arg)))
3864 != CODE_FOR_nothing
3865 && CLZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (TREE_TYPE (arg)),
3866 zerov)
3867 /* Handle only the single common value. */
3868 && zerov != prec)
3869 /* Magic value to give up, unless vr0 proves
3870 arg is non-zero. */
3871 mini = -2;
3872 if (TREE_CODE (arg) == SSA_NAME)
3874 value_range *vr0 = get_value_range (arg);
3875 /* From clz of VR_RANGE minimum we can compute
3876 result maximum. */
3877 if (vr0->type == VR_RANGE
3878 && TREE_CODE (vr0->min) == INTEGER_CST
3879 && !is_overflow_infinity (vr0->min))
3881 maxi = prec - 1 - tree_floor_log2 (vr0->min);
3882 if (maxi != prec)
3883 mini = 0;
3885 else if (vr0->type == VR_ANTI_RANGE
3886 && integer_zerop (vr0->min)
3887 && !is_overflow_infinity (vr0->min))
3889 maxi = prec - 1;
3890 mini = 0;
3892 if (mini == -2)
3893 break;
3894 /* From clz of VR_RANGE maximum we can compute
3895 result minimum. */
3896 if (vr0->type == VR_RANGE
3897 && TREE_CODE (vr0->max) == INTEGER_CST
3898 && !is_overflow_infinity (vr0->max))
3900 mini = prec - 1 - tree_floor_log2 (vr0->max);
3901 if (mini == prec)
3902 break;
3905 if (mini == -2)
3906 break;
3907 goto bitop_builtin;
3908 /* __builtin_ctz* return [0, prec-1], except for
3909 when the argument is 0, but that is undefined behavior.
3910 If there is a ctz optab for this mode and
3911 CTZ_DEFINED_VALUE_AT_ZERO, include that in the range,
3912 otherwise just assume 0 won't be seen. */
3913 CASE_INT_FN (BUILT_IN_CTZ):
3914 arg = gimple_call_arg (stmt, 0);
3915 prec = TYPE_PRECISION (TREE_TYPE (arg));
3916 mini = 0;
3917 maxi = prec - 1;
3918 if (optab_handler (ctz_optab, TYPE_MODE (TREE_TYPE (arg)))
3919 != CODE_FOR_nothing
3920 && CTZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (TREE_TYPE (arg)),
3921 zerov))
3923 /* Handle only the two common values. */
3924 if (zerov == -1)
3925 mini = -1;
3926 else if (zerov == prec)
3927 maxi = prec;
3928 else
3929 /* Magic value to give up, unless vr0 proves
3930 arg is non-zero. */
3931 mini = -2;
3933 if (TREE_CODE (arg) == SSA_NAME)
3935 value_range *vr0 = get_value_range (arg);
3936 /* If arg is non-zero, then use [0, prec - 1]. */
3937 if (((vr0->type == VR_RANGE
3938 && integer_nonzerop (vr0->min))
3939 || (vr0->type == VR_ANTI_RANGE
3940 && integer_zerop (vr0->min)))
3941 && !is_overflow_infinity (vr0->min))
3943 mini = 0;
3944 maxi = prec - 1;
3946 /* If some high bits are known to be zero,
3947 we can decrease the result maximum. */
3948 if (vr0->type == VR_RANGE
3949 && TREE_CODE (vr0->max) == INTEGER_CST
3950 && !is_overflow_infinity (vr0->max))
3952 maxi = tree_floor_log2 (vr0->max);
3953 /* For vr0 [0, 0] give up. */
3954 if (maxi == -1)
3955 break;
3958 if (mini == -2)
3959 break;
3960 goto bitop_builtin;
3961 /* __builtin_clrsb* returns [0, prec-1]. */
3962 CASE_INT_FN (BUILT_IN_CLRSB):
3963 arg = gimple_call_arg (stmt, 0);
3964 prec = TYPE_PRECISION (TREE_TYPE (arg));
3965 mini = 0;
3966 maxi = prec - 1;
3967 goto bitop_builtin;
3968 bitop_builtin:
3969 set_value_range (vr, VR_RANGE, build_int_cst (type, mini),
3970 build_int_cst (type, maxi), NULL);
3971 return;
3972 default:
3973 break;
3976 else if (is_gimple_call (stmt) && gimple_call_internal_p (stmt))
3978 enum tree_code subcode = ERROR_MARK;
3979 switch (gimple_call_internal_fn (stmt))
3981 case IFN_UBSAN_CHECK_ADD:
3982 subcode = PLUS_EXPR;
3983 break;
3984 case IFN_UBSAN_CHECK_SUB:
3985 subcode = MINUS_EXPR;
3986 break;
3987 case IFN_UBSAN_CHECK_MUL:
3988 subcode = MULT_EXPR;
3989 break;
3990 default:
3991 break;
3993 if (subcode != ERROR_MARK)
3995 bool saved_flag_wrapv = flag_wrapv;
3996 /* Pretend the arithmetics is wrapping. If there is
3997 any overflow, we'll complain, but will actually do
3998 wrapping operation. */
3999 flag_wrapv = 1;
4000 extract_range_from_binary_expr (vr, subcode, type,
4001 gimple_call_arg (stmt, 0),
4002 gimple_call_arg (stmt, 1));
4003 flag_wrapv = saved_flag_wrapv;
4005 /* If for both arguments vrp_valueize returned non-NULL,
4006 this should have been already folded and if not, it
4007 wasn't folded because of overflow. Avoid removing the
4008 UBSAN_CHECK_* calls in that case. */
4009 if (vr->type == VR_RANGE
4010 && (vr->min == vr->max
4011 || operand_equal_p (vr->min, vr->max, 0)))
4012 set_value_range_to_varying (vr);
4013 return;
4016 /* Handle extraction of the two results (result of arithmetics and
4017 a flag whether arithmetics overflowed) from {ADD,SUB,MUL}_OVERFLOW
4018 internal function. */
4019 else if (is_gimple_assign (stmt)
4020 && (gimple_assign_rhs_code (stmt) == REALPART_EXPR
4021 || gimple_assign_rhs_code (stmt) == IMAGPART_EXPR)
4022 && INTEGRAL_TYPE_P (type))
4024 enum tree_code code = gimple_assign_rhs_code (stmt);
4025 tree op = gimple_assign_rhs1 (stmt);
4026 if (TREE_CODE (op) == code && TREE_CODE (TREE_OPERAND (op, 0)) == SSA_NAME)
4028 gimple *g = SSA_NAME_DEF_STMT (TREE_OPERAND (op, 0));
4029 if (is_gimple_call (g) && gimple_call_internal_p (g))
4031 enum tree_code subcode = ERROR_MARK;
4032 switch (gimple_call_internal_fn (g))
4034 case IFN_ADD_OVERFLOW:
4035 subcode = PLUS_EXPR;
4036 break;
4037 case IFN_SUB_OVERFLOW:
4038 subcode = MINUS_EXPR;
4039 break;
4040 case IFN_MUL_OVERFLOW:
4041 subcode = MULT_EXPR;
4042 break;
4043 default:
4044 break;
4046 if (subcode != ERROR_MARK)
4048 tree op0 = gimple_call_arg (g, 0);
4049 tree op1 = gimple_call_arg (g, 1);
4050 if (code == IMAGPART_EXPR)
4052 bool ovf = false;
4053 if (check_for_binary_op_overflow (subcode, type,
4054 op0, op1, &ovf))
4055 set_value_range_to_value (vr,
4056 build_int_cst (type, ovf),
4057 NULL);
4058 else
4059 set_value_range (vr, VR_RANGE, build_int_cst (type, 0),
4060 build_int_cst (type, 1), NULL);
4062 else if (types_compatible_p (type, TREE_TYPE (op0))
4063 && types_compatible_p (type, TREE_TYPE (op1)))
4065 bool saved_flag_wrapv = flag_wrapv;
4066 /* Pretend the arithmetics is wrapping. If there is
4067 any overflow, IMAGPART_EXPR will be set. */
4068 flag_wrapv = 1;
4069 extract_range_from_binary_expr (vr, subcode, type,
4070 op0, op1);
4071 flag_wrapv = saved_flag_wrapv;
4073 else
4075 value_range vr0 = VR_INITIALIZER;
4076 value_range vr1 = VR_INITIALIZER;
4077 bool saved_flag_wrapv = flag_wrapv;
4078 /* Pretend the arithmetics is wrapping. If there is
4079 any overflow, IMAGPART_EXPR will be set. */
4080 flag_wrapv = 1;
4081 extract_range_from_unary_expr (&vr0, NOP_EXPR,
4082 type, op0);
4083 extract_range_from_unary_expr (&vr1, NOP_EXPR,
4084 type, op1);
4085 extract_range_from_binary_expr_1 (vr, subcode, type,
4086 &vr0, &vr1);
4087 flag_wrapv = saved_flag_wrapv;
4089 return;
4094 if (INTEGRAL_TYPE_P (type)
4095 && gimple_stmt_nonnegative_warnv_p (stmt, &sop))
4096 set_value_range_to_nonnegative (vr, type,
4097 sop || stmt_overflow_infinity (stmt));
4098 else if (vrp_stmt_computes_nonzero (stmt, &sop)
4099 && !sop)
4100 set_value_range_to_nonnull (vr, type);
4101 else
4102 set_value_range_to_varying (vr);
4106 /* Try to compute a useful range out of assignment STMT and store it
4107 in *VR. */
4109 static void
4110 extract_range_from_assignment (value_range *vr, gassign *stmt)
4112 enum tree_code code = gimple_assign_rhs_code (stmt);
4114 if (code == ASSERT_EXPR)
4115 extract_range_from_assert (vr, gimple_assign_rhs1 (stmt));
4116 else if (code == SSA_NAME)
4117 extract_range_from_ssa_name (vr, gimple_assign_rhs1 (stmt));
4118 else if (TREE_CODE_CLASS (code) == tcc_binary)
4119 extract_range_from_binary_expr (vr, gimple_assign_rhs_code (stmt),
4120 gimple_expr_type (stmt),
4121 gimple_assign_rhs1 (stmt),
4122 gimple_assign_rhs2 (stmt));
4123 else if (TREE_CODE_CLASS (code) == tcc_unary)
4124 extract_range_from_unary_expr (vr, gimple_assign_rhs_code (stmt),
4125 gimple_expr_type (stmt),
4126 gimple_assign_rhs1 (stmt));
4127 else if (code == COND_EXPR)
4128 extract_range_from_cond_expr (vr, stmt);
4129 else if (TREE_CODE_CLASS (code) == tcc_comparison)
4130 extract_range_from_comparison (vr, gimple_assign_rhs_code (stmt),
4131 gimple_expr_type (stmt),
4132 gimple_assign_rhs1 (stmt),
4133 gimple_assign_rhs2 (stmt));
4134 else if (get_gimple_rhs_class (code) == GIMPLE_SINGLE_RHS
4135 && is_gimple_min_invariant (gimple_assign_rhs1 (stmt)))
4136 set_value_range_to_value (vr, gimple_assign_rhs1 (stmt), NULL);
4137 else
4138 set_value_range_to_varying (vr);
4140 if (vr->type == VR_VARYING)
4141 extract_range_basic (vr, stmt);
4144 /* Given a range VR, a LOOP and a variable VAR, determine whether it
4145 would be profitable to adjust VR using scalar evolution information
4146 for VAR. If so, update VR with the new limits. */
4148 static void
4149 adjust_range_with_scev (value_range *vr, struct loop *loop,
4150 gimple *stmt, tree var)
4152 tree init, step, chrec, tmin, tmax, min, max, type, tem;
4153 enum ev_direction dir;
4155 /* TODO. Don't adjust anti-ranges. An anti-range may provide
4156 better opportunities than a regular range, but I'm not sure. */
4157 if (vr->type == VR_ANTI_RANGE)
4158 return;
4160 chrec = instantiate_parameters (loop, analyze_scalar_evolution (loop, var));
4162 /* Like in PR19590, scev can return a constant function. */
4163 if (is_gimple_min_invariant (chrec))
4165 set_value_range_to_value (vr, chrec, vr->equiv);
4166 return;
4169 if (TREE_CODE (chrec) != POLYNOMIAL_CHREC)
4170 return;
4172 init = initial_condition_in_loop_num (chrec, loop->num);
4173 tem = op_with_constant_singleton_value_range (init);
4174 if (tem)
4175 init = tem;
4176 step = evolution_part_in_loop_num (chrec, loop->num);
4177 tem = op_with_constant_singleton_value_range (step);
4178 if (tem)
4179 step = tem;
4181 /* If STEP is symbolic, we can't know whether INIT will be the
4182 minimum or maximum value in the range. Also, unless INIT is
4183 a simple expression, compare_values and possibly other functions
4184 in tree-vrp won't be able to handle it. */
4185 if (step == NULL_TREE
4186 || !is_gimple_min_invariant (step)
4187 || !valid_value_p (init))
4188 return;
4190 dir = scev_direction (chrec);
4191 if (/* Do not adjust ranges if we do not know whether the iv increases
4192 or decreases, ... */
4193 dir == EV_DIR_UNKNOWN
4194 /* ... or if it may wrap. */
4195 || scev_probably_wraps_p (init, step, stmt, get_chrec_loop (chrec),
4196 true))
4197 return;
4199 /* We use TYPE_MIN_VALUE and TYPE_MAX_VALUE here instead of
4200 negative_overflow_infinity and positive_overflow_infinity,
4201 because we have concluded that the loop probably does not
4202 wrap. */
4204 type = TREE_TYPE (var);
4205 if (POINTER_TYPE_P (type) || !TYPE_MIN_VALUE (type))
4206 tmin = lower_bound_in_type (type, type);
4207 else
4208 tmin = TYPE_MIN_VALUE (type);
4209 if (POINTER_TYPE_P (type) || !TYPE_MAX_VALUE (type))
4210 tmax = upper_bound_in_type (type, type);
4211 else
4212 tmax = TYPE_MAX_VALUE (type);
4214 /* Try to use estimated number of iterations for the loop to constrain the
4215 final value in the evolution. */
4216 if (TREE_CODE (step) == INTEGER_CST
4217 && is_gimple_val (init)
4218 && (TREE_CODE (init) != SSA_NAME
4219 || get_value_range (init)->type == VR_RANGE))
4221 widest_int nit;
4223 /* We are only entering here for loop header PHI nodes, so using
4224 the number of latch executions is the correct thing to use. */
4225 if (max_loop_iterations (loop, &nit))
4227 value_range maxvr = VR_INITIALIZER;
4228 signop sgn = TYPE_SIGN (TREE_TYPE (step));
4229 bool overflow;
4231 widest_int wtmp = wi::mul (wi::to_widest (step), nit, sgn,
4232 &overflow);
4233 /* If the multiplication overflowed we can't do a meaningful
4234 adjustment. Likewise if the result doesn't fit in the type
4235 of the induction variable. For a signed type we have to
4236 check whether the result has the expected signedness which
4237 is that of the step as number of iterations is unsigned. */
4238 if (!overflow
4239 && wi::fits_to_tree_p (wtmp, TREE_TYPE (init))
4240 && (sgn == UNSIGNED
4241 || wi::gts_p (wtmp, 0) == wi::gts_p (step, 0)))
4243 tem = wide_int_to_tree (TREE_TYPE (init), wtmp);
4244 extract_range_from_binary_expr (&maxvr, PLUS_EXPR,
4245 TREE_TYPE (init), init, tem);
4246 /* Likewise if the addition did. */
4247 if (maxvr.type == VR_RANGE)
4249 tmin = maxvr.min;
4250 tmax = maxvr.max;
4256 if (vr->type == VR_VARYING || vr->type == VR_UNDEFINED)
4258 min = tmin;
4259 max = tmax;
4261 /* For VARYING or UNDEFINED ranges, just about anything we get
4262 from scalar evolutions should be better. */
4264 if (dir == EV_DIR_DECREASES)
4265 max = init;
4266 else
4267 min = init;
4269 else if (vr->type == VR_RANGE)
4271 min = vr->min;
4272 max = vr->max;
4274 if (dir == EV_DIR_DECREASES)
4276 /* INIT is the maximum value. If INIT is lower than VR->MAX
4277 but no smaller than VR->MIN, set VR->MAX to INIT. */
4278 if (compare_values (init, max) == -1)
4279 max = init;
4281 /* According to the loop information, the variable does not
4282 overflow. If we think it does, probably because of an
4283 overflow due to arithmetic on a different INF value,
4284 reset now. */
4285 if (is_negative_overflow_infinity (min)
4286 || compare_values (min, tmin) == -1)
4287 min = tmin;
4290 else
4292 /* If INIT is bigger than VR->MIN, set VR->MIN to INIT. */
4293 if (compare_values (init, min) == 1)
4294 min = init;
4296 if (is_positive_overflow_infinity (max)
4297 || compare_values (tmax, max) == -1)
4298 max = tmax;
4301 else
4302 return;
4304 /* If we just created an invalid range with the minimum
4305 greater than the maximum, we fail conservatively.
4306 This should happen only in unreachable
4307 parts of code, or for invalid programs. */
4308 if (compare_values (min, max) == 1
4309 || (is_negative_overflow_infinity (min)
4310 && is_positive_overflow_infinity (max)))
4311 return;
4313 set_value_range (vr, VR_RANGE, min, max, vr->equiv);
4317 /* Given two numeric value ranges VR0, VR1 and a comparison code COMP:
4319 - Return BOOLEAN_TRUE_NODE if VR0 COMP VR1 always returns true for
4320 all the values in the ranges.
4322 - Return BOOLEAN_FALSE_NODE if the comparison always returns false.
4324 - Return NULL_TREE if it is not always possible to determine the
4325 value of the comparison.
4327 Also set *STRICT_OVERFLOW_P to indicate whether a range with an
4328 overflow infinity was used in the test. */
4331 static tree
4332 compare_ranges (enum tree_code comp, value_range *vr0, value_range *vr1,
4333 bool *strict_overflow_p)
4335 /* VARYING or UNDEFINED ranges cannot be compared. */
4336 if (vr0->type == VR_VARYING
4337 || vr0->type == VR_UNDEFINED
4338 || vr1->type == VR_VARYING
4339 || vr1->type == VR_UNDEFINED)
4340 return NULL_TREE;
4342 /* Anti-ranges need to be handled separately. */
4343 if (vr0->type == VR_ANTI_RANGE || vr1->type == VR_ANTI_RANGE)
4345 /* If both are anti-ranges, then we cannot compute any
4346 comparison. */
4347 if (vr0->type == VR_ANTI_RANGE && vr1->type == VR_ANTI_RANGE)
4348 return NULL_TREE;
4350 /* These comparisons are never statically computable. */
4351 if (comp == GT_EXPR
4352 || comp == GE_EXPR
4353 || comp == LT_EXPR
4354 || comp == LE_EXPR)
4355 return NULL_TREE;
4357 /* Equality can be computed only between a range and an
4358 anti-range. ~[VAL1, VAL2] == [VAL1, VAL2] is always false. */
4359 if (vr0->type == VR_RANGE)
4361 /* To simplify processing, make VR0 the anti-range. */
4362 value_range *tmp = vr0;
4363 vr0 = vr1;
4364 vr1 = tmp;
4367 gcc_assert (comp == NE_EXPR || comp == EQ_EXPR);
4369 if (compare_values_warnv (vr0->min, vr1->min, strict_overflow_p) == 0
4370 && compare_values_warnv (vr0->max, vr1->max, strict_overflow_p) == 0)
4371 return (comp == NE_EXPR) ? boolean_true_node : boolean_false_node;
4373 return NULL_TREE;
4376 if (!usable_range_p (vr0, strict_overflow_p)
4377 || !usable_range_p (vr1, strict_overflow_p))
4378 return NULL_TREE;
4380 /* Simplify processing. If COMP is GT_EXPR or GE_EXPR, switch the
4381 operands around and change the comparison code. */
4382 if (comp == GT_EXPR || comp == GE_EXPR)
4384 comp = (comp == GT_EXPR) ? LT_EXPR : LE_EXPR;
4385 std::swap (vr0, vr1);
4388 if (comp == EQ_EXPR)
4390 /* Equality may only be computed if both ranges represent
4391 exactly one value. */
4392 if (compare_values_warnv (vr0->min, vr0->max, strict_overflow_p) == 0
4393 && compare_values_warnv (vr1->min, vr1->max, strict_overflow_p) == 0)
4395 int cmp_min = compare_values_warnv (vr0->min, vr1->min,
4396 strict_overflow_p);
4397 int cmp_max = compare_values_warnv (vr0->max, vr1->max,
4398 strict_overflow_p);
4399 if (cmp_min == 0 && cmp_max == 0)
4400 return boolean_true_node;
4401 else if (cmp_min != -2 && cmp_max != -2)
4402 return boolean_false_node;
4404 /* If [V0_MIN, V1_MAX] < [V1_MIN, V1_MAX] then V0 != V1. */
4405 else if (compare_values_warnv (vr0->min, vr1->max,
4406 strict_overflow_p) == 1
4407 || compare_values_warnv (vr1->min, vr0->max,
4408 strict_overflow_p) == 1)
4409 return boolean_false_node;
4411 return NULL_TREE;
4413 else if (comp == NE_EXPR)
4415 int cmp1, cmp2;
4417 /* If VR0 is completely to the left or completely to the right
4418 of VR1, they are always different. Notice that we need to
4419 make sure that both comparisons yield similar results to
4420 avoid comparing values that cannot be compared at
4421 compile-time. */
4422 cmp1 = compare_values_warnv (vr0->max, vr1->min, strict_overflow_p);
4423 cmp2 = compare_values_warnv (vr0->min, vr1->max, strict_overflow_p);
4424 if ((cmp1 == -1 && cmp2 == -1) || (cmp1 == 1 && cmp2 == 1))
4425 return boolean_true_node;
4427 /* If VR0 and VR1 represent a single value and are identical,
4428 return false. */
4429 else if (compare_values_warnv (vr0->min, vr0->max,
4430 strict_overflow_p) == 0
4431 && compare_values_warnv (vr1->min, vr1->max,
4432 strict_overflow_p) == 0
4433 && compare_values_warnv (vr0->min, vr1->min,
4434 strict_overflow_p) == 0
4435 && compare_values_warnv (vr0->max, vr1->max,
4436 strict_overflow_p) == 0)
4437 return boolean_false_node;
4439 /* Otherwise, they may or may not be different. */
4440 else
4441 return NULL_TREE;
4443 else if (comp == LT_EXPR || comp == LE_EXPR)
4445 int tst;
4447 /* If VR0 is to the left of VR1, return true. */
4448 tst = compare_values_warnv (vr0->max, vr1->min, strict_overflow_p);
4449 if ((comp == LT_EXPR && tst == -1)
4450 || (comp == LE_EXPR && (tst == -1 || tst == 0)))
4452 if (overflow_infinity_range_p (vr0)
4453 || overflow_infinity_range_p (vr1))
4454 *strict_overflow_p = true;
4455 return boolean_true_node;
4458 /* If VR0 is to the right of VR1, return false. */
4459 tst = compare_values_warnv (vr0->min, vr1->max, strict_overflow_p);
4460 if ((comp == LT_EXPR && (tst == 0 || tst == 1))
4461 || (comp == LE_EXPR && tst == 1))
4463 if (overflow_infinity_range_p (vr0)
4464 || overflow_infinity_range_p (vr1))
4465 *strict_overflow_p = true;
4466 return boolean_false_node;
4469 /* Otherwise, we don't know. */
4470 return NULL_TREE;
4473 gcc_unreachable ();
4477 /* Given a value range VR, a value VAL and a comparison code COMP, return
4478 BOOLEAN_TRUE_NODE if VR COMP VAL always returns true for all the
4479 values in VR. Return BOOLEAN_FALSE_NODE if the comparison
4480 always returns false. Return NULL_TREE if it is not always
4481 possible to determine the value of the comparison. Also set
4482 *STRICT_OVERFLOW_P to indicate whether a range with an overflow
4483 infinity was used in the test. */
4485 static tree
4486 compare_range_with_value (enum tree_code comp, value_range *vr, tree val,
4487 bool *strict_overflow_p)
4489 if (vr->type == VR_VARYING || vr->type == VR_UNDEFINED)
4490 return NULL_TREE;
4492 /* Anti-ranges need to be handled separately. */
4493 if (vr->type == VR_ANTI_RANGE)
4495 /* For anti-ranges, the only predicates that we can compute at
4496 compile time are equality and inequality. */
4497 if (comp == GT_EXPR
4498 || comp == GE_EXPR
4499 || comp == LT_EXPR
4500 || comp == LE_EXPR)
4501 return NULL_TREE;
4503 /* ~[VAL_1, VAL_2] OP VAL is known if VAL_1 <= VAL <= VAL_2. */
4504 if (value_inside_range (val, vr->min, vr->max) == 1)
4505 return (comp == NE_EXPR) ? boolean_true_node : boolean_false_node;
4507 return NULL_TREE;
4510 if (!usable_range_p (vr, strict_overflow_p))
4511 return NULL_TREE;
4513 if (comp == EQ_EXPR)
4515 /* EQ_EXPR may only be computed if VR represents exactly
4516 one value. */
4517 if (compare_values_warnv (vr->min, vr->max, strict_overflow_p) == 0)
4519 int cmp = compare_values_warnv (vr->min, val, strict_overflow_p);
4520 if (cmp == 0)
4521 return boolean_true_node;
4522 else if (cmp == -1 || cmp == 1 || cmp == 2)
4523 return boolean_false_node;
4525 else if (compare_values_warnv (val, vr->min, strict_overflow_p) == -1
4526 || compare_values_warnv (vr->max, val, strict_overflow_p) == -1)
4527 return boolean_false_node;
4529 return NULL_TREE;
4531 else if (comp == NE_EXPR)
4533 /* If VAL is not inside VR, then they are always different. */
4534 if (compare_values_warnv (vr->max, val, strict_overflow_p) == -1
4535 || compare_values_warnv (vr->min, val, strict_overflow_p) == 1)
4536 return boolean_true_node;
4538 /* If VR represents exactly one value equal to VAL, then return
4539 false. */
4540 if (compare_values_warnv (vr->min, vr->max, strict_overflow_p) == 0
4541 && compare_values_warnv (vr->min, val, strict_overflow_p) == 0)
4542 return boolean_false_node;
4544 /* Otherwise, they may or may not be different. */
4545 return NULL_TREE;
4547 else if (comp == LT_EXPR || comp == LE_EXPR)
4549 int tst;
4551 /* If VR is to the left of VAL, return true. */
4552 tst = compare_values_warnv (vr->max, val, strict_overflow_p);
4553 if ((comp == LT_EXPR && tst == -1)
4554 || (comp == LE_EXPR && (tst == -1 || tst == 0)))
4556 if (overflow_infinity_range_p (vr))
4557 *strict_overflow_p = true;
4558 return boolean_true_node;
4561 /* If VR is to the right of VAL, return false. */
4562 tst = compare_values_warnv (vr->min, val, strict_overflow_p);
4563 if ((comp == LT_EXPR && (tst == 0 || tst == 1))
4564 || (comp == LE_EXPR && tst == 1))
4566 if (overflow_infinity_range_p (vr))
4567 *strict_overflow_p = true;
4568 return boolean_false_node;
4571 /* Otherwise, we don't know. */
4572 return NULL_TREE;
4574 else if (comp == GT_EXPR || comp == GE_EXPR)
4576 int tst;
4578 /* If VR is to the right of VAL, return true. */
4579 tst = compare_values_warnv (vr->min, val, strict_overflow_p);
4580 if ((comp == GT_EXPR && tst == 1)
4581 || (comp == GE_EXPR && (tst == 0 || tst == 1)))
4583 if (overflow_infinity_range_p (vr))
4584 *strict_overflow_p = true;
4585 return boolean_true_node;
4588 /* If VR is to the left of VAL, return false. */
4589 tst = compare_values_warnv (vr->max, val, strict_overflow_p);
4590 if ((comp == GT_EXPR && (tst == -1 || tst == 0))
4591 || (comp == GE_EXPR && tst == -1))
4593 if (overflow_infinity_range_p (vr))
4594 *strict_overflow_p = true;
4595 return boolean_false_node;
4598 /* Otherwise, we don't know. */
4599 return NULL_TREE;
4602 gcc_unreachable ();
4606 /* Debugging dumps. */
4608 void dump_value_range (FILE *, value_range *);
4609 void debug_value_range (value_range *);
4610 void dump_all_value_ranges (FILE *);
4611 void debug_all_value_ranges (void);
4612 void dump_vr_equiv (FILE *, bitmap);
4613 void debug_vr_equiv (bitmap);
4616 /* Dump value range VR to FILE. */
4618 void
4619 dump_value_range (FILE *file, value_range *vr)
4621 if (vr == NULL)
4622 fprintf (file, "[]");
4623 else if (vr->type == VR_UNDEFINED)
4624 fprintf (file, "UNDEFINED");
4625 else if (vr->type == VR_RANGE || vr->type == VR_ANTI_RANGE)
4627 tree type = TREE_TYPE (vr->min);
4629 fprintf (file, "%s[", (vr->type == VR_ANTI_RANGE) ? "~" : "");
4631 if (is_negative_overflow_infinity (vr->min))
4632 fprintf (file, "-INF(OVF)");
4633 else if (INTEGRAL_TYPE_P (type)
4634 && !TYPE_UNSIGNED (type)
4635 && vrp_val_is_min (vr->min))
4636 fprintf (file, "-INF");
4637 else
4638 print_generic_expr (file, vr->min, 0);
4640 fprintf (file, ", ");
4642 if (is_positive_overflow_infinity (vr->max))
4643 fprintf (file, "+INF(OVF)");
4644 else if (INTEGRAL_TYPE_P (type)
4645 && vrp_val_is_max (vr->max))
4646 fprintf (file, "+INF");
4647 else
4648 print_generic_expr (file, vr->max, 0);
4650 fprintf (file, "]");
4652 if (vr->equiv)
4654 bitmap_iterator bi;
4655 unsigned i, c = 0;
4657 fprintf (file, " EQUIVALENCES: { ");
4659 EXECUTE_IF_SET_IN_BITMAP (vr->equiv, 0, i, bi)
4661 print_generic_expr (file, ssa_name (i), 0);
4662 fprintf (file, " ");
4663 c++;
4666 fprintf (file, "} (%u elements)", c);
4669 else if (vr->type == VR_VARYING)
4670 fprintf (file, "VARYING");
4671 else
4672 fprintf (file, "INVALID RANGE");
4676 /* Dump value range VR to stderr. */
4678 DEBUG_FUNCTION void
4679 debug_value_range (value_range *vr)
4681 dump_value_range (stderr, vr);
4682 fprintf (stderr, "\n");
4686 /* Dump value ranges of all SSA_NAMEs to FILE. */
4688 void
4689 dump_all_value_ranges (FILE *file)
4691 size_t i;
4693 for (i = 0; i < num_vr_values; i++)
4695 if (vr_value[i])
4697 print_generic_expr (file, ssa_name (i), 0);
4698 fprintf (file, ": ");
4699 dump_value_range (file, vr_value[i]);
4700 fprintf (file, "\n");
4704 fprintf (file, "\n");
4708 /* Dump all value ranges to stderr. */
4710 DEBUG_FUNCTION void
4711 debug_all_value_ranges (void)
4713 dump_all_value_ranges (stderr);
4717 /* Given a COND_EXPR COND of the form 'V OP W', and an SSA name V,
4718 create a new SSA name N and return the assertion assignment
4719 'N = ASSERT_EXPR <V, V OP W>'. */
4721 static gimple *
4722 build_assert_expr_for (tree cond, tree v)
4724 tree a;
4725 gassign *assertion;
4727 gcc_assert (TREE_CODE (v) == SSA_NAME
4728 && COMPARISON_CLASS_P (cond));
4730 a = build2 (ASSERT_EXPR, TREE_TYPE (v), v, cond);
4731 assertion = gimple_build_assign (NULL_TREE, a);
4733 /* The new ASSERT_EXPR, creates a new SSA name that replaces the
4734 operand of the ASSERT_EXPR. Create it so the new name and the old one
4735 are registered in the replacement table so that we can fix the SSA web
4736 after adding all the ASSERT_EXPRs. */
4737 create_new_def_for (v, assertion, NULL);
4739 return assertion;
4743 /* Return false if EXPR is a predicate expression involving floating
4744 point values. */
4746 static inline bool
4747 fp_predicate (gimple *stmt)
4749 GIMPLE_CHECK (stmt, GIMPLE_COND);
4751 return FLOAT_TYPE_P (TREE_TYPE (gimple_cond_lhs (stmt)));
4754 /* If the range of values taken by OP can be inferred after STMT executes,
4755 return the comparison code (COMP_CODE_P) and value (VAL_P) that
4756 describes the inferred range. Return true if a range could be
4757 inferred. */
4759 static bool
4760 infer_value_range (gimple *stmt, tree op, tree_code *comp_code_p, tree *val_p)
4762 *val_p = NULL_TREE;
4763 *comp_code_p = ERROR_MARK;
4765 /* Do not attempt to infer anything in names that flow through
4766 abnormal edges. */
4767 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op))
4768 return false;
4770 /* Similarly, don't infer anything from statements that may throw
4771 exceptions. ??? Relax this requirement? */
4772 if (stmt_could_throw_p (stmt))
4773 return false;
4775 /* If STMT is the last statement of a basic block with no normal
4776 successors, there is no point inferring anything about any of its
4777 operands. We would not be able to find a proper insertion point
4778 for the assertion, anyway. */
4779 if (stmt_ends_bb_p (stmt))
4781 edge_iterator ei;
4782 edge e;
4784 FOR_EACH_EDGE (e, ei, gimple_bb (stmt)->succs)
4785 if (!(e->flags & EDGE_ABNORMAL))
4786 break;
4787 if (e == NULL)
4788 return false;
4791 if (infer_nonnull_range (stmt, op))
4793 *val_p = build_int_cst (TREE_TYPE (op), 0);
4794 *comp_code_p = NE_EXPR;
4795 return true;
4798 return false;
4802 void dump_asserts_for (FILE *, tree);
4803 void debug_asserts_for (tree);
4804 void dump_all_asserts (FILE *);
4805 void debug_all_asserts (void);
4807 /* Dump all the registered assertions for NAME to FILE. */
4809 void
4810 dump_asserts_for (FILE *file, tree name)
4812 assert_locus *loc;
4814 fprintf (file, "Assertions to be inserted for ");
4815 print_generic_expr (file, name, 0);
4816 fprintf (file, "\n");
4818 loc = asserts_for[SSA_NAME_VERSION (name)];
4819 while (loc)
4821 fprintf (file, "\t");
4822 print_gimple_stmt (file, gsi_stmt (loc->si), 0, 0);
4823 fprintf (file, "\n\tBB #%d", loc->bb->index);
4824 if (loc->e)
4826 fprintf (file, "\n\tEDGE %d->%d", loc->e->src->index,
4827 loc->e->dest->index);
4828 dump_edge_info (file, loc->e, dump_flags, 0);
4830 fprintf (file, "\n\tPREDICATE: ");
4831 print_generic_expr (file, name, 0);
4832 fprintf (file, " %s ", get_tree_code_name (loc->comp_code));
4833 print_generic_expr (file, loc->val, 0);
4834 fprintf (file, "\n\n");
4835 loc = loc->next;
4838 fprintf (file, "\n");
4842 /* Dump all the registered assertions for NAME to stderr. */
4844 DEBUG_FUNCTION void
4845 debug_asserts_for (tree name)
4847 dump_asserts_for (stderr, name);
4851 /* Dump all the registered assertions for all the names to FILE. */
4853 void
4854 dump_all_asserts (FILE *file)
4856 unsigned i;
4857 bitmap_iterator bi;
4859 fprintf (file, "\nASSERT_EXPRs to be inserted\n\n");
4860 EXECUTE_IF_SET_IN_BITMAP (need_assert_for, 0, i, bi)
4861 dump_asserts_for (file, ssa_name (i));
4862 fprintf (file, "\n");
4866 /* Dump all the registered assertions for all the names to stderr. */
4868 DEBUG_FUNCTION void
4869 debug_all_asserts (void)
4871 dump_all_asserts (stderr);
4875 /* If NAME doesn't have an ASSERT_EXPR registered for asserting
4876 'EXPR COMP_CODE VAL' at a location that dominates block BB or
4877 E->DEST, then register this location as a possible insertion point
4878 for ASSERT_EXPR <NAME, EXPR COMP_CODE VAL>.
4880 BB, E and SI provide the exact insertion point for the new
4881 ASSERT_EXPR. If BB is NULL, then the ASSERT_EXPR is to be inserted
4882 on edge E. Otherwise, if E is NULL, the ASSERT_EXPR is inserted on
4883 BB. If SI points to a COND_EXPR or a SWITCH_EXPR statement, then E
4884 must not be NULL. */
4886 static void
4887 register_new_assert_for (tree name, tree expr,
4888 enum tree_code comp_code,
4889 tree val,
4890 basic_block bb,
4891 edge e,
4892 gimple_stmt_iterator si)
4894 assert_locus *n, *loc, *last_loc;
4895 basic_block dest_bb;
4897 gcc_checking_assert (bb == NULL || e == NULL);
4899 if (e == NULL)
4900 gcc_checking_assert (gimple_code (gsi_stmt (si)) != GIMPLE_COND
4901 && gimple_code (gsi_stmt (si)) != GIMPLE_SWITCH);
4903 /* Never build an assert comparing against an integer constant with
4904 TREE_OVERFLOW set. This confuses our undefined overflow warning
4905 machinery. */
4906 if (TREE_OVERFLOW_P (val))
4907 val = drop_tree_overflow (val);
4909 /* The new assertion A will be inserted at BB or E. We need to
4910 determine if the new location is dominated by a previously
4911 registered location for A. If we are doing an edge insertion,
4912 assume that A will be inserted at E->DEST. Note that this is not
4913 necessarily true.
4915 If E is a critical edge, it will be split. But even if E is
4916 split, the new block will dominate the same set of blocks that
4917 E->DEST dominates.
4919 The reverse, however, is not true, blocks dominated by E->DEST
4920 will not be dominated by the new block created to split E. So,
4921 if the insertion location is on a critical edge, we will not use
4922 the new location to move another assertion previously registered
4923 at a block dominated by E->DEST. */
4924 dest_bb = (bb) ? bb : e->dest;
4926 /* If NAME already has an ASSERT_EXPR registered for COMP_CODE and
4927 VAL at a block dominating DEST_BB, then we don't need to insert a new
4928 one. Similarly, if the same assertion already exists at a block
4929 dominated by DEST_BB and the new location is not on a critical
4930 edge, then update the existing location for the assertion (i.e.,
4931 move the assertion up in the dominance tree).
4933 Note, this is implemented as a simple linked list because there
4934 should not be more than a handful of assertions registered per
4935 name. If this becomes a performance problem, a table hashed by
4936 COMP_CODE and VAL could be implemented. */
4937 loc = asserts_for[SSA_NAME_VERSION (name)];
4938 last_loc = loc;
4939 while (loc)
4941 if (loc->comp_code == comp_code
4942 && (loc->val == val
4943 || operand_equal_p (loc->val, val, 0))
4944 && (loc->expr == expr
4945 || operand_equal_p (loc->expr, expr, 0)))
4947 /* If E is not a critical edge and DEST_BB
4948 dominates the existing location for the assertion, move
4949 the assertion up in the dominance tree by updating its
4950 location information. */
4951 if ((e == NULL || !EDGE_CRITICAL_P (e))
4952 && dominated_by_p (CDI_DOMINATORS, loc->bb, dest_bb))
4954 loc->bb = dest_bb;
4955 loc->e = e;
4956 loc->si = si;
4957 return;
4961 /* Update the last node of the list and move to the next one. */
4962 last_loc = loc;
4963 loc = loc->next;
4966 /* If we didn't find an assertion already registered for
4967 NAME COMP_CODE VAL, add a new one at the end of the list of
4968 assertions associated with NAME. */
4969 n = XNEW (struct assert_locus);
4970 n->bb = dest_bb;
4971 n->e = e;
4972 n->si = si;
4973 n->comp_code = comp_code;
4974 n->val = val;
4975 n->expr = expr;
4976 n->next = NULL;
4978 if (last_loc)
4979 last_loc->next = n;
4980 else
4981 asserts_for[SSA_NAME_VERSION (name)] = n;
4983 bitmap_set_bit (need_assert_for, SSA_NAME_VERSION (name));
4986 /* (COND_OP0 COND_CODE COND_OP1) is a predicate which uses NAME.
4987 Extract a suitable test code and value and store them into *CODE_P and
4988 *VAL_P so the predicate is normalized to NAME *CODE_P *VAL_P.
4990 If no extraction was possible, return FALSE, otherwise return TRUE.
4992 If INVERT is true, then we invert the result stored into *CODE_P. */
4994 static bool
4995 extract_code_and_val_from_cond_with_ops (tree name, enum tree_code cond_code,
4996 tree cond_op0, tree cond_op1,
4997 bool invert, enum tree_code *code_p,
4998 tree *val_p)
5000 enum tree_code comp_code;
5001 tree val;
5003 /* Otherwise, we have a comparison of the form NAME COMP VAL
5004 or VAL COMP NAME. */
5005 if (name == cond_op1)
5007 /* If the predicate is of the form VAL COMP NAME, flip
5008 COMP around because we need to register NAME as the
5009 first operand in the predicate. */
5010 comp_code = swap_tree_comparison (cond_code);
5011 val = cond_op0;
5013 else
5015 /* The comparison is of the form NAME COMP VAL, so the
5016 comparison code remains unchanged. */
5017 comp_code = cond_code;
5018 val = cond_op1;
5021 /* Invert the comparison code as necessary. */
5022 if (invert)
5023 comp_code = invert_tree_comparison (comp_code, 0);
5025 /* VRP does not handle float types. */
5026 if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (val)))
5027 return false;
5029 /* Do not register always-false predicates.
5030 FIXME: this works around a limitation in fold() when dealing with
5031 enumerations. Given 'enum { N1, N2 } x;', fold will not
5032 fold 'if (x > N2)' to 'if (0)'. */
5033 if ((comp_code == GT_EXPR || comp_code == LT_EXPR)
5034 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
5036 tree min = TYPE_MIN_VALUE (TREE_TYPE (val));
5037 tree max = TYPE_MAX_VALUE (TREE_TYPE (val));
5039 if (comp_code == GT_EXPR
5040 && (!max
5041 || compare_values (val, max) == 0))
5042 return false;
5044 if (comp_code == LT_EXPR
5045 && (!min
5046 || compare_values (val, min) == 0))
5047 return false;
5049 *code_p = comp_code;
5050 *val_p = val;
5051 return true;
5054 /* Find out smallest RES where RES > VAL && (RES & MASK) == RES, if any
5055 (otherwise return VAL). VAL and MASK must be zero-extended for
5056 precision PREC. If SGNBIT is non-zero, first xor VAL with SGNBIT
5057 (to transform signed values into unsigned) and at the end xor
5058 SGNBIT back. */
5060 static wide_int
5061 masked_increment (const wide_int &val_in, const wide_int &mask,
5062 const wide_int &sgnbit, unsigned int prec)
5064 wide_int bit = wi::one (prec), res;
5065 unsigned int i;
5067 wide_int val = val_in ^ sgnbit;
5068 for (i = 0; i < prec; i++, bit += bit)
5070 res = mask;
5071 if ((res & bit) == 0)
5072 continue;
5073 res = bit - 1;
5074 res = (val + bit).and_not (res);
5075 res &= mask;
5076 if (wi::gtu_p (res, val))
5077 return res ^ sgnbit;
5079 return val ^ sgnbit;
5082 /* Try to register an edge assertion for SSA name NAME on edge E for
5083 the condition COND contributing to the conditional jump pointed to by BSI.
5084 Invert the condition COND if INVERT is true. */
5086 static void
5087 register_edge_assert_for_2 (tree name, edge e, gimple_stmt_iterator bsi,
5088 enum tree_code cond_code,
5089 tree cond_op0, tree cond_op1, bool invert)
5091 tree val;
5092 enum tree_code comp_code;
5094 if (!extract_code_and_val_from_cond_with_ops (name, cond_code,
5095 cond_op0,
5096 cond_op1,
5097 invert, &comp_code, &val))
5098 return;
5100 /* Only register an ASSERT_EXPR if NAME was found in the sub-graph
5101 reachable from E. */
5102 if (live_on_edge (e, name)
5103 && !has_single_use (name))
5104 register_new_assert_for (name, name, comp_code, val, NULL, e, bsi);
5106 /* In the case of NAME <= CST and NAME being defined as
5107 NAME = (unsigned) NAME2 + CST2 we can assert NAME2 >= -CST2
5108 and NAME2 <= CST - CST2. We can do the same for NAME > CST.
5109 This catches range and anti-range tests. */
5110 if ((comp_code == LE_EXPR
5111 || comp_code == GT_EXPR)
5112 && TREE_CODE (val) == INTEGER_CST
5113 && TYPE_UNSIGNED (TREE_TYPE (val)))
5115 gimple *def_stmt = SSA_NAME_DEF_STMT (name);
5116 tree cst2 = NULL_TREE, name2 = NULL_TREE, name3 = NULL_TREE;
5118 /* Extract CST2 from the (optional) addition. */
5119 if (is_gimple_assign (def_stmt)
5120 && gimple_assign_rhs_code (def_stmt) == PLUS_EXPR)
5122 name2 = gimple_assign_rhs1 (def_stmt);
5123 cst2 = gimple_assign_rhs2 (def_stmt);
5124 if (TREE_CODE (name2) == SSA_NAME
5125 && TREE_CODE (cst2) == INTEGER_CST)
5126 def_stmt = SSA_NAME_DEF_STMT (name2);
5129 /* Extract NAME2 from the (optional) sign-changing cast. */
5130 if (gimple_assign_cast_p (def_stmt))
5132 if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt))
5133 && ! TYPE_UNSIGNED (TREE_TYPE (gimple_assign_rhs1 (def_stmt)))
5134 && (TYPE_PRECISION (gimple_expr_type (def_stmt))
5135 == TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt)))))
5136 name3 = gimple_assign_rhs1 (def_stmt);
5139 /* If name3 is used later, create an ASSERT_EXPR for it. */
5140 if (name3 != NULL_TREE
5141 && TREE_CODE (name3) == SSA_NAME
5142 && (cst2 == NULL_TREE
5143 || TREE_CODE (cst2) == INTEGER_CST)
5144 && INTEGRAL_TYPE_P (TREE_TYPE (name3))
5145 && live_on_edge (e, name3)
5146 && !has_single_use (name3))
5148 tree tmp;
5150 /* Build an expression for the range test. */
5151 tmp = build1 (NOP_EXPR, TREE_TYPE (name), name3);
5152 if (cst2 != NULL_TREE)
5153 tmp = build2 (PLUS_EXPR, TREE_TYPE (name), tmp, cst2);
5155 if (dump_file)
5157 fprintf (dump_file, "Adding assert for ");
5158 print_generic_expr (dump_file, name3, 0);
5159 fprintf (dump_file, " from ");
5160 print_generic_expr (dump_file, tmp, 0);
5161 fprintf (dump_file, "\n");
5164 register_new_assert_for (name3, tmp, comp_code, val, NULL, e, bsi);
5167 /* If name2 is used later, create an ASSERT_EXPR for it. */
5168 if (name2 != NULL_TREE
5169 && TREE_CODE (name2) == SSA_NAME
5170 && TREE_CODE (cst2) == INTEGER_CST
5171 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
5172 && live_on_edge (e, name2)
5173 && !has_single_use (name2))
5175 tree tmp;
5177 /* Build an expression for the range test. */
5178 tmp = name2;
5179 if (TREE_TYPE (name) != TREE_TYPE (name2))
5180 tmp = build1 (NOP_EXPR, TREE_TYPE (name), tmp);
5181 if (cst2 != NULL_TREE)
5182 tmp = build2 (PLUS_EXPR, TREE_TYPE (name), tmp, cst2);
5184 if (dump_file)
5186 fprintf (dump_file, "Adding assert for ");
5187 print_generic_expr (dump_file, name2, 0);
5188 fprintf (dump_file, " from ");
5189 print_generic_expr (dump_file, tmp, 0);
5190 fprintf (dump_file, "\n");
5193 register_new_assert_for (name2, tmp, comp_code, val, NULL, e, bsi);
5197 /* In the case of post-in/decrement tests like if (i++) ... and uses
5198 of the in/decremented value on the edge the extra name we want to
5199 assert for is not on the def chain of the name compared. Instead
5200 it is in the set of use stmts.
5201 Similar cases happen for conversions that were simplified through
5202 fold_{sign_changed,widened}_comparison. */
5203 if ((comp_code == NE_EXPR
5204 || comp_code == EQ_EXPR)
5205 && TREE_CODE (val) == INTEGER_CST)
5207 imm_use_iterator ui;
5208 gimple *use_stmt;
5209 FOR_EACH_IMM_USE_STMT (use_stmt, ui, name)
5211 if (!is_gimple_assign (use_stmt))
5212 continue;
5214 /* Cut off to use-stmts that are dominating the predecessor. */
5215 if (!dominated_by_p (CDI_DOMINATORS, e->src, gimple_bb (use_stmt)))
5216 continue;
5218 tree name2 = gimple_assign_lhs (use_stmt);
5219 if (TREE_CODE (name2) != SSA_NAME
5220 || !live_on_edge (e, name2))
5221 continue;
5223 enum tree_code code = gimple_assign_rhs_code (use_stmt);
5224 tree cst;
5225 if (code == PLUS_EXPR
5226 || code == MINUS_EXPR)
5228 cst = gimple_assign_rhs2 (use_stmt);
5229 if (TREE_CODE (cst) != INTEGER_CST)
5230 continue;
5231 cst = int_const_binop (code, val, cst);
5233 else if (CONVERT_EXPR_CODE_P (code))
5235 /* For truncating conversions we cannot record
5236 an inequality. */
5237 if (comp_code == NE_EXPR
5238 && (TYPE_PRECISION (TREE_TYPE (name2))
5239 < TYPE_PRECISION (TREE_TYPE (name))))
5240 continue;
5241 cst = fold_convert (TREE_TYPE (name2), val);
5243 else
5244 continue;
5246 if (TREE_OVERFLOW_P (cst))
5247 cst = drop_tree_overflow (cst);
5248 register_new_assert_for (name2, name2, comp_code, cst,
5249 NULL, e, bsi);
5253 if (TREE_CODE_CLASS (comp_code) == tcc_comparison
5254 && TREE_CODE (val) == INTEGER_CST)
5256 gimple *def_stmt = SSA_NAME_DEF_STMT (name);
5257 tree name2 = NULL_TREE, names[2], cst2 = NULL_TREE;
5258 tree val2 = NULL_TREE;
5259 unsigned int prec = TYPE_PRECISION (TREE_TYPE (val));
5260 wide_int mask = wi::zero (prec);
5261 unsigned int nprec = prec;
5262 enum tree_code rhs_code = ERROR_MARK;
5264 if (is_gimple_assign (def_stmt))
5265 rhs_code = gimple_assign_rhs_code (def_stmt);
5267 /* Add asserts for NAME cmp CST and NAME being defined
5268 as NAME = (int) NAME2. */
5269 if (!TYPE_UNSIGNED (TREE_TYPE (val))
5270 && (comp_code == LE_EXPR || comp_code == LT_EXPR
5271 || comp_code == GT_EXPR || comp_code == GE_EXPR)
5272 && gimple_assign_cast_p (def_stmt))
5274 name2 = gimple_assign_rhs1 (def_stmt);
5275 if (CONVERT_EXPR_CODE_P (rhs_code)
5276 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
5277 && TYPE_UNSIGNED (TREE_TYPE (name2))
5278 && prec == TYPE_PRECISION (TREE_TYPE (name2))
5279 && (comp_code == LE_EXPR || comp_code == GT_EXPR
5280 || !tree_int_cst_equal (val,
5281 TYPE_MIN_VALUE (TREE_TYPE (val))))
5282 && live_on_edge (e, name2)
5283 && !has_single_use (name2))
5285 tree tmp, cst;
5286 enum tree_code new_comp_code = comp_code;
5288 cst = fold_convert (TREE_TYPE (name2),
5289 TYPE_MIN_VALUE (TREE_TYPE (val)));
5290 /* Build an expression for the range test. */
5291 tmp = build2 (PLUS_EXPR, TREE_TYPE (name2), name2, cst);
5292 cst = fold_build2 (PLUS_EXPR, TREE_TYPE (name2), cst,
5293 fold_convert (TREE_TYPE (name2), val));
5294 if (comp_code == LT_EXPR || comp_code == GE_EXPR)
5296 new_comp_code = comp_code == LT_EXPR ? LE_EXPR : GT_EXPR;
5297 cst = fold_build2 (MINUS_EXPR, TREE_TYPE (name2), cst,
5298 build_int_cst (TREE_TYPE (name2), 1));
5301 if (dump_file)
5303 fprintf (dump_file, "Adding assert for ");
5304 print_generic_expr (dump_file, name2, 0);
5305 fprintf (dump_file, " from ");
5306 print_generic_expr (dump_file, tmp, 0);
5307 fprintf (dump_file, "\n");
5310 register_new_assert_for (name2, tmp, new_comp_code, cst, NULL,
5311 e, bsi);
5315 /* Add asserts for NAME cmp CST and NAME being defined as
5316 NAME = NAME2 >> CST2.
5318 Extract CST2 from the right shift. */
5319 if (rhs_code == RSHIFT_EXPR)
5321 name2 = gimple_assign_rhs1 (def_stmt);
5322 cst2 = gimple_assign_rhs2 (def_stmt);
5323 if (TREE_CODE (name2) == SSA_NAME
5324 && tree_fits_uhwi_p (cst2)
5325 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
5326 && IN_RANGE (tree_to_uhwi (cst2), 1, prec - 1)
5327 && prec == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (val)))
5328 && live_on_edge (e, name2)
5329 && !has_single_use (name2))
5331 mask = wi::mask (tree_to_uhwi (cst2), false, prec);
5332 val2 = fold_binary (LSHIFT_EXPR, TREE_TYPE (val), val, cst2);
5335 if (val2 != NULL_TREE
5336 && TREE_CODE (val2) == INTEGER_CST
5337 && simple_cst_equal (fold_build2 (RSHIFT_EXPR,
5338 TREE_TYPE (val),
5339 val2, cst2), val))
5341 enum tree_code new_comp_code = comp_code;
5342 tree tmp, new_val;
5344 tmp = name2;
5345 if (comp_code == EQ_EXPR || comp_code == NE_EXPR)
5347 if (!TYPE_UNSIGNED (TREE_TYPE (val)))
5349 tree type = build_nonstandard_integer_type (prec, 1);
5350 tmp = build1 (NOP_EXPR, type, name2);
5351 val2 = fold_convert (type, val2);
5353 tmp = fold_build2 (MINUS_EXPR, TREE_TYPE (tmp), tmp, val2);
5354 new_val = wide_int_to_tree (TREE_TYPE (tmp), mask);
5355 new_comp_code = comp_code == EQ_EXPR ? LE_EXPR : GT_EXPR;
5357 else if (comp_code == LT_EXPR || comp_code == GE_EXPR)
5359 wide_int minval
5360 = wi::min_value (prec, TYPE_SIGN (TREE_TYPE (val)));
5361 new_val = val2;
5362 if (minval == new_val)
5363 new_val = NULL_TREE;
5365 else
5367 wide_int maxval
5368 = wi::max_value (prec, TYPE_SIGN (TREE_TYPE (val)));
5369 mask |= val2;
5370 if (mask == maxval)
5371 new_val = NULL_TREE;
5372 else
5373 new_val = wide_int_to_tree (TREE_TYPE (val2), mask);
5376 if (new_val)
5378 if (dump_file)
5380 fprintf (dump_file, "Adding assert for ");
5381 print_generic_expr (dump_file, name2, 0);
5382 fprintf (dump_file, " from ");
5383 print_generic_expr (dump_file, tmp, 0);
5384 fprintf (dump_file, "\n");
5387 register_new_assert_for (name2, tmp, new_comp_code, new_val,
5388 NULL, e, bsi);
5392 /* Add asserts for NAME cmp CST and NAME being defined as
5393 NAME = NAME2 & CST2.
5395 Extract CST2 from the and.
5397 Also handle
5398 NAME = (unsigned) NAME2;
5399 casts where NAME's type is unsigned and has smaller precision
5400 than NAME2's type as if it was NAME = NAME2 & MASK. */
5401 names[0] = NULL_TREE;
5402 names[1] = NULL_TREE;
5403 cst2 = NULL_TREE;
5404 if (rhs_code == BIT_AND_EXPR
5405 || (CONVERT_EXPR_CODE_P (rhs_code)
5406 && TREE_CODE (TREE_TYPE (val)) == INTEGER_TYPE
5407 && TYPE_UNSIGNED (TREE_TYPE (val))
5408 && TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt)))
5409 > prec))
5411 name2 = gimple_assign_rhs1 (def_stmt);
5412 if (rhs_code == BIT_AND_EXPR)
5413 cst2 = gimple_assign_rhs2 (def_stmt);
5414 else
5416 cst2 = TYPE_MAX_VALUE (TREE_TYPE (val));
5417 nprec = TYPE_PRECISION (TREE_TYPE (name2));
5419 if (TREE_CODE (name2) == SSA_NAME
5420 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
5421 && TREE_CODE (cst2) == INTEGER_CST
5422 && !integer_zerop (cst2)
5423 && (nprec > 1
5424 || TYPE_UNSIGNED (TREE_TYPE (val))))
5426 gimple *def_stmt2 = SSA_NAME_DEF_STMT (name2);
5427 if (gimple_assign_cast_p (def_stmt2))
5429 names[1] = gimple_assign_rhs1 (def_stmt2);
5430 if (!CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt2))
5431 || !INTEGRAL_TYPE_P (TREE_TYPE (names[1]))
5432 || (TYPE_PRECISION (TREE_TYPE (name2))
5433 != TYPE_PRECISION (TREE_TYPE (names[1])))
5434 || !live_on_edge (e, names[1])
5435 || has_single_use (names[1]))
5436 names[1] = NULL_TREE;
5438 if (live_on_edge (e, name2)
5439 && !has_single_use (name2))
5440 names[0] = name2;
5443 if (names[0] || names[1])
5445 wide_int minv, maxv, valv, cst2v;
5446 wide_int tem, sgnbit;
5447 bool valid_p = false, valn, cst2n;
5448 enum tree_code ccode = comp_code;
5450 valv = wide_int::from (val, nprec, UNSIGNED);
5451 cst2v = wide_int::from (cst2, nprec, UNSIGNED);
5452 valn = wi::neg_p (valv, TYPE_SIGN (TREE_TYPE (val)));
5453 cst2n = wi::neg_p (cst2v, TYPE_SIGN (TREE_TYPE (val)));
5454 /* If CST2 doesn't have most significant bit set,
5455 but VAL is negative, we have comparison like
5456 if ((x & 0x123) > -4) (always true). Just give up. */
5457 if (!cst2n && valn)
5458 ccode = ERROR_MARK;
5459 if (cst2n)
5460 sgnbit = wi::set_bit_in_zero (nprec - 1, nprec);
5461 else
5462 sgnbit = wi::zero (nprec);
5463 minv = valv & cst2v;
5464 switch (ccode)
5466 case EQ_EXPR:
5467 /* Minimum unsigned value for equality is VAL & CST2
5468 (should be equal to VAL, otherwise we probably should
5469 have folded the comparison into false) and
5470 maximum unsigned value is VAL | ~CST2. */
5471 maxv = valv | ~cst2v;
5472 valid_p = true;
5473 break;
5475 case NE_EXPR:
5476 tem = valv | ~cst2v;
5477 /* If VAL is 0, handle (X & CST2) != 0 as (X & CST2) > 0U. */
5478 if (valv == 0)
5480 cst2n = false;
5481 sgnbit = wi::zero (nprec);
5482 goto gt_expr;
5484 /* If (VAL | ~CST2) is all ones, handle it as
5485 (X & CST2) < VAL. */
5486 if (tem == -1)
5488 cst2n = false;
5489 valn = false;
5490 sgnbit = wi::zero (nprec);
5491 goto lt_expr;
5493 if (!cst2n && wi::neg_p (cst2v))
5494 sgnbit = wi::set_bit_in_zero (nprec - 1, nprec);
5495 if (sgnbit != 0)
5497 if (valv == sgnbit)
5499 cst2n = true;
5500 valn = true;
5501 goto gt_expr;
5503 if (tem == wi::mask (nprec - 1, false, nprec))
5505 cst2n = true;
5506 goto lt_expr;
5508 if (!cst2n)
5509 sgnbit = wi::zero (nprec);
5511 break;
5513 case GE_EXPR:
5514 /* Minimum unsigned value for >= if (VAL & CST2) == VAL
5515 is VAL and maximum unsigned value is ~0. For signed
5516 comparison, if CST2 doesn't have most significant bit
5517 set, handle it similarly. If CST2 has MSB set,
5518 the minimum is the same, and maximum is ~0U/2. */
5519 if (minv != valv)
5521 /* If (VAL & CST2) != VAL, X & CST2 can't be equal to
5522 VAL. */
5523 minv = masked_increment (valv, cst2v, sgnbit, nprec);
5524 if (minv == valv)
5525 break;
5527 maxv = wi::mask (nprec - (cst2n ? 1 : 0), false, nprec);
5528 valid_p = true;
5529 break;
5531 case GT_EXPR:
5532 gt_expr:
5533 /* Find out smallest MINV where MINV > VAL
5534 && (MINV & CST2) == MINV, if any. If VAL is signed and
5535 CST2 has MSB set, compute it biased by 1 << (nprec - 1). */
5536 minv = masked_increment (valv, cst2v, sgnbit, nprec);
5537 if (minv == valv)
5538 break;
5539 maxv = wi::mask (nprec - (cst2n ? 1 : 0), false, nprec);
5540 valid_p = true;
5541 break;
5543 case LE_EXPR:
5544 /* Minimum unsigned value for <= is 0 and maximum
5545 unsigned value is VAL | ~CST2 if (VAL & CST2) == VAL.
5546 Otherwise, find smallest VAL2 where VAL2 > VAL
5547 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
5548 as maximum.
5549 For signed comparison, if CST2 doesn't have most
5550 significant bit set, handle it similarly. If CST2 has
5551 MSB set, the maximum is the same and minimum is INT_MIN. */
5552 if (minv == valv)
5553 maxv = valv;
5554 else
5556 maxv = masked_increment (valv, cst2v, sgnbit, nprec);
5557 if (maxv == valv)
5558 break;
5559 maxv -= 1;
5561 maxv |= ~cst2v;
5562 minv = sgnbit;
5563 valid_p = true;
5564 break;
5566 case LT_EXPR:
5567 lt_expr:
5568 /* Minimum unsigned value for < is 0 and maximum
5569 unsigned value is (VAL-1) | ~CST2 if (VAL & CST2) == VAL.
5570 Otherwise, find smallest VAL2 where VAL2 > VAL
5571 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
5572 as maximum.
5573 For signed comparison, if CST2 doesn't have most
5574 significant bit set, handle it similarly. If CST2 has
5575 MSB set, the maximum is the same and minimum is INT_MIN. */
5576 if (minv == valv)
5578 if (valv == sgnbit)
5579 break;
5580 maxv = valv;
5582 else
5584 maxv = masked_increment (valv, cst2v, sgnbit, nprec);
5585 if (maxv == valv)
5586 break;
5588 maxv -= 1;
5589 maxv |= ~cst2v;
5590 minv = sgnbit;
5591 valid_p = true;
5592 break;
5594 default:
5595 break;
5597 if (valid_p
5598 && (maxv - minv) != -1)
5600 tree tmp, new_val, type;
5601 int i;
5603 for (i = 0; i < 2; i++)
5604 if (names[i])
5606 wide_int maxv2 = maxv;
5607 tmp = names[i];
5608 type = TREE_TYPE (names[i]);
5609 if (!TYPE_UNSIGNED (type))
5611 type = build_nonstandard_integer_type (nprec, 1);
5612 tmp = build1 (NOP_EXPR, type, names[i]);
5614 if (minv != 0)
5616 tmp = build2 (PLUS_EXPR, type, tmp,
5617 wide_int_to_tree (type, -minv));
5618 maxv2 = maxv - minv;
5620 new_val = wide_int_to_tree (type, maxv2);
5622 if (dump_file)
5624 fprintf (dump_file, "Adding assert for ");
5625 print_generic_expr (dump_file, names[i], 0);
5626 fprintf (dump_file, " from ");
5627 print_generic_expr (dump_file, tmp, 0);
5628 fprintf (dump_file, "\n");
5631 register_new_assert_for (names[i], tmp, LE_EXPR,
5632 new_val, NULL, e, bsi);
5639 /* OP is an operand of a truth value expression which is known to have
5640 a particular value. Register any asserts for OP and for any
5641 operands in OP's defining statement.
5643 If CODE is EQ_EXPR, then we want to register OP is zero (false),
5644 if CODE is NE_EXPR, then we want to register OP is nonzero (true). */
5646 static void
5647 register_edge_assert_for_1 (tree op, enum tree_code code,
5648 edge e, gimple_stmt_iterator bsi)
5650 gimple *op_def;
5651 tree val;
5652 enum tree_code rhs_code;
5654 /* We only care about SSA_NAMEs. */
5655 if (TREE_CODE (op) != SSA_NAME)
5656 return;
5658 /* We know that OP will have a zero or nonzero value. If OP is used
5659 more than once go ahead and register an assert for OP. */
5660 if (live_on_edge (e, op)
5661 && !has_single_use (op))
5663 val = build_int_cst (TREE_TYPE (op), 0);
5664 register_new_assert_for (op, op, code, val, NULL, e, bsi);
5667 /* Now look at how OP is set. If it's set from a comparison,
5668 a truth operation or some bit operations, then we may be able
5669 to register information about the operands of that assignment. */
5670 op_def = SSA_NAME_DEF_STMT (op);
5671 if (gimple_code (op_def) != GIMPLE_ASSIGN)
5672 return;
5674 rhs_code = gimple_assign_rhs_code (op_def);
5676 if (TREE_CODE_CLASS (rhs_code) == tcc_comparison)
5678 bool invert = (code == EQ_EXPR ? true : false);
5679 tree op0 = gimple_assign_rhs1 (op_def);
5680 tree op1 = gimple_assign_rhs2 (op_def);
5682 if (TREE_CODE (op0) == SSA_NAME)
5683 register_edge_assert_for_2 (op0, e, bsi, rhs_code, op0, op1, invert);
5684 if (TREE_CODE (op1) == SSA_NAME)
5685 register_edge_assert_for_2 (op1, e, bsi, rhs_code, op0, op1, invert);
5687 else if ((code == NE_EXPR
5688 && gimple_assign_rhs_code (op_def) == BIT_AND_EXPR)
5689 || (code == EQ_EXPR
5690 && gimple_assign_rhs_code (op_def) == BIT_IOR_EXPR))
5692 /* Recurse on each operand. */
5693 tree op0 = gimple_assign_rhs1 (op_def);
5694 tree op1 = gimple_assign_rhs2 (op_def);
5695 if (TREE_CODE (op0) == SSA_NAME
5696 && has_single_use (op0))
5697 register_edge_assert_for_1 (op0, code, e, bsi);
5698 if (TREE_CODE (op1) == SSA_NAME
5699 && has_single_use (op1))
5700 register_edge_assert_for_1 (op1, code, e, bsi);
5702 else if (gimple_assign_rhs_code (op_def) == BIT_NOT_EXPR
5703 && TYPE_PRECISION (TREE_TYPE (gimple_assign_lhs (op_def))) == 1)
5705 /* Recurse, flipping CODE. */
5706 code = invert_tree_comparison (code, false);
5707 register_edge_assert_for_1 (gimple_assign_rhs1 (op_def), code, e, bsi);
5709 else if (gimple_assign_rhs_code (op_def) == SSA_NAME)
5711 /* Recurse through the copy. */
5712 register_edge_assert_for_1 (gimple_assign_rhs1 (op_def), code, e, bsi);
5714 else if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (op_def)))
5716 /* Recurse through the type conversion, unless it is a narrowing
5717 conversion or conversion from non-integral type. */
5718 tree rhs = gimple_assign_rhs1 (op_def);
5719 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs))
5720 && (TYPE_PRECISION (TREE_TYPE (rhs))
5721 <= TYPE_PRECISION (TREE_TYPE (op))))
5722 register_edge_assert_for_1 (rhs, code, e, bsi);
5726 /* Try to register an edge assertion for SSA name NAME on edge E for
5727 the condition COND contributing to the conditional jump pointed to by
5728 SI. */
5730 static void
5731 register_edge_assert_for (tree name, edge e, gimple_stmt_iterator si,
5732 enum tree_code cond_code, tree cond_op0,
5733 tree cond_op1)
5735 tree val;
5736 enum tree_code comp_code;
5737 bool is_else_edge = (e->flags & EDGE_FALSE_VALUE) != 0;
5739 /* Do not attempt to infer anything in names that flow through
5740 abnormal edges. */
5741 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name))
5742 return;
5744 if (!extract_code_and_val_from_cond_with_ops (name, cond_code,
5745 cond_op0, cond_op1,
5746 is_else_edge,
5747 &comp_code, &val))
5748 return;
5750 /* Register ASSERT_EXPRs for name. */
5751 register_edge_assert_for_2 (name, e, si, cond_code, cond_op0,
5752 cond_op1, is_else_edge);
5755 /* If COND is effectively an equality test of an SSA_NAME against
5756 the value zero or one, then we may be able to assert values
5757 for SSA_NAMEs which flow into COND. */
5759 /* In the case of NAME == 1 or NAME != 0, for BIT_AND_EXPR defining
5760 statement of NAME we can assert both operands of the BIT_AND_EXPR
5761 have nonzero value. */
5762 if (((comp_code == EQ_EXPR && integer_onep (val))
5763 || (comp_code == NE_EXPR && integer_zerop (val))))
5765 gimple *def_stmt = SSA_NAME_DEF_STMT (name);
5767 if (is_gimple_assign (def_stmt)
5768 && gimple_assign_rhs_code (def_stmt) == BIT_AND_EXPR)
5770 tree op0 = gimple_assign_rhs1 (def_stmt);
5771 tree op1 = gimple_assign_rhs2 (def_stmt);
5772 register_edge_assert_for_1 (op0, NE_EXPR, e, si);
5773 register_edge_assert_for_1 (op1, NE_EXPR, e, si);
5777 /* In the case of NAME == 0 or NAME != 1, for BIT_IOR_EXPR defining
5778 statement of NAME we can assert both operands of the BIT_IOR_EXPR
5779 have zero value. */
5780 if (((comp_code == EQ_EXPR && integer_zerop (val))
5781 || (comp_code == NE_EXPR && integer_onep (val))))
5783 gimple *def_stmt = SSA_NAME_DEF_STMT (name);
5785 /* For BIT_IOR_EXPR only if NAME == 0 both operands have
5786 necessarily zero value, or if type-precision is one. */
5787 if (is_gimple_assign (def_stmt)
5788 && (gimple_assign_rhs_code (def_stmt) == BIT_IOR_EXPR
5789 && (TYPE_PRECISION (TREE_TYPE (name)) == 1
5790 || comp_code == EQ_EXPR)))
5792 tree op0 = gimple_assign_rhs1 (def_stmt);
5793 tree op1 = gimple_assign_rhs2 (def_stmt);
5794 register_edge_assert_for_1 (op0, EQ_EXPR, e, si);
5795 register_edge_assert_for_1 (op1, EQ_EXPR, e, si);
5801 /* Determine whether the outgoing edges of BB should receive an
5802 ASSERT_EXPR for each of the operands of BB's LAST statement.
5803 The last statement of BB must be a COND_EXPR.
5805 If any of the sub-graphs rooted at BB have an interesting use of
5806 the predicate operands, an assert location node is added to the
5807 list of assertions for the corresponding operands. */
5809 static void
5810 find_conditional_asserts (basic_block bb, gcond *last)
5812 gimple_stmt_iterator bsi;
5813 tree op;
5814 edge_iterator ei;
5815 edge e;
5816 ssa_op_iter iter;
5818 bsi = gsi_for_stmt (last);
5820 /* Look for uses of the operands in each of the sub-graphs
5821 rooted at BB. We need to check each of the outgoing edges
5822 separately, so that we know what kind of ASSERT_EXPR to
5823 insert. */
5824 FOR_EACH_EDGE (e, ei, bb->succs)
5826 if (e->dest == bb)
5827 continue;
5829 /* Register the necessary assertions for each operand in the
5830 conditional predicate. */
5831 FOR_EACH_SSA_TREE_OPERAND (op, last, iter, SSA_OP_USE)
5832 register_edge_assert_for (op, e, bsi,
5833 gimple_cond_code (last),
5834 gimple_cond_lhs (last),
5835 gimple_cond_rhs (last));
5839 struct case_info
5841 tree expr;
5842 basic_block bb;
5845 /* Compare two case labels sorting first by the destination bb index
5846 and then by the case value. */
5848 static int
5849 compare_case_labels (const void *p1, const void *p2)
5851 const struct case_info *ci1 = (const struct case_info *) p1;
5852 const struct case_info *ci2 = (const struct case_info *) p2;
5853 int idx1 = ci1->bb->index;
5854 int idx2 = ci2->bb->index;
5856 if (idx1 < idx2)
5857 return -1;
5858 else if (idx1 == idx2)
5860 /* Make sure the default label is first in a group. */
5861 if (!CASE_LOW (ci1->expr))
5862 return -1;
5863 else if (!CASE_LOW (ci2->expr))
5864 return 1;
5865 else
5866 return tree_int_cst_compare (CASE_LOW (ci1->expr),
5867 CASE_LOW (ci2->expr));
5869 else
5870 return 1;
5873 /* Determine whether the outgoing edges of BB should receive an
5874 ASSERT_EXPR for each of the operands of BB's LAST statement.
5875 The last statement of BB must be a SWITCH_EXPR.
5877 If any of the sub-graphs rooted at BB have an interesting use of
5878 the predicate operands, an assert location node is added to the
5879 list of assertions for the corresponding operands. */
5881 static void
5882 find_switch_asserts (basic_block bb, gswitch *last)
5884 gimple_stmt_iterator bsi;
5885 tree op;
5886 edge e;
5887 struct case_info *ci;
5888 size_t n = gimple_switch_num_labels (last);
5889 #if GCC_VERSION >= 4000
5890 unsigned int idx;
5891 #else
5892 /* Work around GCC 3.4 bug (PR 37086). */
5893 volatile unsigned int idx;
5894 #endif
5896 bsi = gsi_for_stmt (last);
5897 op = gimple_switch_index (last);
5898 if (TREE_CODE (op) != SSA_NAME)
5899 return;
5901 /* Build a vector of case labels sorted by destination label. */
5902 ci = XNEWVEC (struct case_info, n);
5903 for (idx = 0; idx < n; ++idx)
5905 ci[idx].expr = gimple_switch_label (last, idx);
5906 ci[idx].bb = label_to_block (CASE_LABEL (ci[idx].expr));
5908 qsort (ci, n, sizeof (struct case_info), compare_case_labels);
5910 for (idx = 0; idx < n; ++idx)
5912 tree min, max;
5913 tree cl = ci[idx].expr;
5914 basic_block cbb = ci[idx].bb;
5916 min = CASE_LOW (cl);
5917 max = CASE_HIGH (cl);
5919 /* If there are multiple case labels with the same destination
5920 we need to combine them to a single value range for the edge. */
5921 if (idx + 1 < n && cbb == ci[idx + 1].bb)
5923 /* Skip labels until the last of the group. */
5924 do {
5925 ++idx;
5926 } while (idx < n && cbb == ci[idx].bb);
5927 --idx;
5929 /* Pick up the maximum of the case label range. */
5930 if (CASE_HIGH (ci[idx].expr))
5931 max = CASE_HIGH (ci[idx].expr);
5932 else
5933 max = CASE_LOW (ci[idx].expr);
5936 /* Nothing to do if the range includes the default label until we
5937 can register anti-ranges. */
5938 if (min == NULL_TREE)
5939 continue;
5941 /* Find the edge to register the assert expr on. */
5942 e = find_edge (bb, cbb);
5944 /* Register the necessary assertions for the operand in the
5945 SWITCH_EXPR. */
5946 register_edge_assert_for (op, e, bsi,
5947 max ? GE_EXPR : EQ_EXPR,
5948 op, fold_convert (TREE_TYPE (op), min));
5949 if (max)
5950 register_edge_assert_for (op, e, bsi, LE_EXPR, op,
5951 fold_convert (TREE_TYPE (op), max));
5954 XDELETEVEC (ci);
5958 /* Traverse all the statements in block BB looking for statements that
5959 may generate useful assertions for the SSA names in their operand.
5960 If a statement produces a useful assertion A for name N_i, then the
5961 list of assertions already generated for N_i is scanned to
5962 determine if A is actually needed.
5964 If N_i already had the assertion A at a location dominating the
5965 current location, then nothing needs to be done. Otherwise, the
5966 new location for A is recorded instead.
5968 1- For every statement S in BB, all the variables used by S are
5969 added to bitmap FOUND_IN_SUBGRAPH.
5971 2- If statement S uses an operand N in a way that exposes a known
5972 value range for N, then if N was not already generated by an
5973 ASSERT_EXPR, create a new assert location for N. For instance,
5974 if N is a pointer and the statement dereferences it, we can
5975 assume that N is not NULL.
5977 3- COND_EXPRs are a special case of #2. We can derive range
5978 information from the predicate but need to insert different
5979 ASSERT_EXPRs for each of the sub-graphs rooted at the
5980 conditional block. If the last statement of BB is a conditional
5981 expression of the form 'X op Y', then
5983 a) Remove X and Y from the set FOUND_IN_SUBGRAPH.
5985 b) If the conditional is the only entry point to the sub-graph
5986 corresponding to the THEN_CLAUSE, recurse into it. On
5987 return, if X and/or Y are marked in FOUND_IN_SUBGRAPH, then
5988 an ASSERT_EXPR is added for the corresponding variable.
5990 c) Repeat step (b) on the ELSE_CLAUSE.
5992 d) Mark X and Y in FOUND_IN_SUBGRAPH.
5994 For instance,
5996 if (a == 9)
5997 b = a;
5998 else
5999 b = c + 1;
6001 In this case, an assertion on the THEN clause is useful to
6002 determine that 'a' is always 9 on that edge. However, an assertion
6003 on the ELSE clause would be unnecessary.
6005 4- If BB does not end in a conditional expression, then we recurse
6006 into BB's dominator children.
6008 At the end of the recursive traversal, every SSA name will have a
6009 list of locations where ASSERT_EXPRs should be added. When a new
6010 location for name N is found, it is registered by calling
6011 register_new_assert_for. That function keeps track of all the
6012 registered assertions to prevent adding unnecessary assertions.
6013 For instance, if a pointer P_4 is dereferenced more than once in a
6014 dominator tree, only the location dominating all the dereference of
6015 P_4 will receive an ASSERT_EXPR. */
6017 static void
6018 find_assert_locations_1 (basic_block bb, sbitmap live)
6020 gimple *last;
6022 last = last_stmt (bb);
6024 /* If BB's last statement is a conditional statement involving integer
6025 operands, determine if we need to add ASSERT_EXPRs. */
6026 if (last
6027 && gimple_code (last) == GIMPLE_COND
6028 && !fp_predicate (last)
6029 && !ZERO_SSA_OPERANDS (last, SSA_OP_USE))
6030 find_conditional_asserts (bb, as_a <gcond *> (last));
6032 /* If BB's last statement is a switch statement involving integer
6033 operands, determine if we need to add ASSERT_EXPRs. */
6034 if (last
6035 && gimple_code (last) == GIMPLE_SWITCH
6036 && !ZERO_SSA_OPERANDS (last, SSA_OP_USE))
6037 find_switch_asserts (bb, as_a <gswitch *> (last));
6039 /* Traverse all the statements in BB marking used names and looking
6040 for statements that may infer assertions for their used operands. */
6041 for (gimple_stmt_iterator si = gsi_last_bb (bb); !gsi_end_p (si);
6042 gsi_prev (&si))
6044 gimple *stmt;
6045 tree op;
6046 ssa_op_iter i;
6048 stmt = gsi_stmt (si);
6050 if (is_gimple_debug (stmt))
6051 continue;
6053 /* See if we can derive an assertion for any of STMT's operands. */
6054 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_USE)
6056 tree value;
6057 enum tree_code comp_code;
6059 /* If op is not live beyond this stmt, do not bother to insert
6060 asserts for it. */
6061 if (!bitmap_bit_p (live, SSA_NAME_VERSION (op)))
6062 continue;
6064 /* If OP is used in such a way that we can infer a value
6065 range for it, and we don't find a previous assertion for
6066 it, create a new assertion location node for OP. */
6067 if (infer_value_range (stmt, op, &comp_code, &value))
6069 /* If we are able to infer a nonzero value range for OP,
6070 then walk backwards through the use-def chain to see if OP
6071 was set via a typecast.
6073 If so, then we can also infer a nonzero value range
6074 for the operand of the NOP_EXPR. */
6075 if (comp_code == NE_EXPR && integer_zerop (value))
6077 tree t = op;
6078 gimple *def_stmt = SSA_NAME_DEF_STMT (t);
6080 while (is_gimple_assign (def_stmt)
6081 && CONVERT_EXPR_CODE_P
6082 (gimple_assign_rhs_code (def_stmt))
6083 && TREE_CODE
6084 (gimple_assign_rhs1 (def_stmt)) == SSA_NAME
6085 && POINTER_TYPE_P
6086 (TREE_TYPE (gimple_assign_rhs1 (def_stmt))))
6088 t = gimple_assign_rhs1 (def_stmt);
6089 def_stmt = SSA_NAME_DEF_STMT (t);
6091 /* Note we want to register the assert for the
6092 operand of the NOP_EXPR after SI, not after the
6093 conversion. */
6094 if (! has_single_use (t))
6095 register_new_assert_for (t, t, comp_code, value,
6096 bb, NULL, si);
6100 register_new_assert_for (op, op, comp_code, value, bb, NULL, si);
6104 /* Update live. */
6105 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_USE)
6106 bitmap_set_bit (live, SSA_NAME_VERSION (op));
6107 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_DEF)
6108 bitmap_clear_bit (live, SSA_NAME_VERSION (op));
6111 /* Traverse all PHI nodes in BB, updating live. */
6112 for (gphi_iterator si = gsi_start_phis (bb); !gsi_end_p (si);
6113 gsi_next (&si))
6115 use_operand_p arg_p;
6116 ssa_op_iter i;
6117 gphi *phi = si.phi ();
6118 tree res = gimple_phi_result (phi);
6120 if (virtual_operand_p (res))
6121 continue;
6123 FOR_EACH_PHI_ARG (arg_p, phi, i, SSA_OP_USE)
6125 tree arg = USE_FROM_PTR (arg_p);
6126 if (TREE_CODE (arg) == SSA_NAME)
6127 bitmap_set_bit (live, SSA_NAME_VERSION (arg));
6130 bitmap_clear_bit (live, SSA_NAME_VERSION (res));
6134 /* Do an RPO walk over the function computing SSA name liveness
6135 on-the-fly and deciding on assert expressions to insert. */
6137 static void
6138 find_assert_locations (void)
6140 int *rpo = XNEWVEC (int, last_basic_block_for_fn (cfun));
6141 int *bb_rpo = XNEWVEC (int, last_basic_block_for_fn (cfun));
6142 int *last_rpo = XCNEWVEC (int, last_basic_block_for_fn (cfun));
6143 int rpo_cnt, i;
6145 live = XCNEWVEC (sbitmap, last_basic_block_for_fn (cfun));
6146 rpo_cnt = pre_and_rev_post_order_compute (NULL, rpo, false);
6147 for (i = 0; i < rpo_cnt; ++i)
6148 bb_rpo[rpo[i]] = i;
6150 /* Pre-seed loop latch liveness from loop header PHI nodes. Due to
6151 the order we compute liveness and insert asserts we otherwise
6152 fail to insert asserts into the loop latch. */
6153 loop_p loop;
6154 FOR_EACH_LOOP (loop, 0)
6156 i = loop->latch->index;
6157 unsigned int j = single_succ_edge (loop->latch)->dest_idx;
6158 for (gphi_iterator gsi = gsi_start_phis (loop->header);
6159 !gsi_end_p (gsi); gsi_next (&gsi))
6161 gphi *phi = gsi.phi ();
6162 if (virtual_operand_p (gimple_phi_result (phi)))
6163 continue;
6164 tree arg = gimple_phi_arg_def (phi, j);
6165 if (TREE_CODE (arg) == SSA_NAME)
6167 if (live[i] == NULL)
6169 live[i] = sbitmap_alloc (num_ssa_names);
6170 bitmap_clear (live[i]);
6172 bitmap_set_bit (live[i], SSA_NAME_VERSION (arg));
6177 for (i = rpo_cnt - 1; i >= 0; --i)
6179 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, rpo[i]);
6180 edge e;
6181 edge_iterator ei;
6183 if (!live[rpo[i]])
6185 live[rpo[i]] = sbitmap_alloc (num_ssa_names);
6186 bitmap_clear (live[rpo[i]]);
6189 /* Process BB and update the live information with uses in
6190 this block. */
6191 find_assert_locations_1 (bb, live[rpo[i]]);
6193 /* Merge liveness into the predecessor blocks and free it. */
6194 if (!bitmap_empty_p (live[rpo[i]]))
6196 int pred_rpo = i;
6197 FOR_EACH_EDGE (e, ei, bb->preds)
6199 int pred = e->src->index;
6200 if ((e->flags & EDGE_DFS_BACK) || pred == ENTRY_BLOCK)
6201 continue;
6203 if (!live[pred])
6205 live[pred] = sbitmap_alloc (num_ssa_names);
6206 bitmap_clear (live[pred]);
6208 bitmap_ior (live[pred], live[pred], live[rpo[i]]);
6210 if (bb_rpo[pred] < pred_rpo)
6211 pred_rpo = bb_rpo[pred];
6214 /* Record the RPO number of the last visited block that needs
6215 live information from this block. */
6216 last_rpo[rpo[i]] = pred_rpo;
6218 else
6220 sbitmap_free (live[rpo[i]]);
6221 live[rpo[i]] = NULL;
6224 /* We can free all successors live bitmaps if all their
6225 predecessors have been visited already. */
6226 FOR_EACH_EDGE (e, ei, bb->succs)
6227 if (last_rpo[e->dest->index] == i
6228 && live[e->dest->index])
6230 sbitmap_free (live[e->dest->index]);
6231 live[e->dest->index] = NULL;
6235 XDELETEVEC (rpo);
6236 XDELETEVEC (bb_rpo);
6237 XDELETEVEC (last_rpo);
6238 for (i = 0; i < last_basic_block_for_fn (cfun); ++i)
6239 if (live[i])
6240 sbitmap_free (live[i]);
6241 XDELETEVEC (live);
6244 /* Create an ASSERT_EXPR for NAME and insert it in the location
6245 indicated by LOC. Return true if we made any edge insertions. */
6247 static bool
6248 process_assert_insertions_for (tree name, assert_locus *loc)
6250 /* Build the comparison expression NAME_i COMP_CODE VAL. */
6251 gimple *stmt;
6252 tree cond;
6253 gimple *assert_stmt;
6254 edge_iterator ei;
6255 edge e;
6257 /* If we have X <=> X do not insert an assert expr for that. */
6258 if (loc->expr == loc->val)
6259 return false;
6261 cond = build2 (loc->comp_code, boolean_type_node, loc->expr, loc->val);
6262 assert_stmt = build_assert_expr_for (cond, name);
6263 if (loc->e)
6265 /* We have been asked to insert the assertion on an edge. This
6266 is used only by COND_EXPR and SWITCH_EXPR assertions. */
6267 gcc_checking_assert (gimple_code (gsi_stmt (loc->si)) == GIMPLE_COND
6268 || (gimple_code (gsi_stmt (loc->si))
6269 == GIMPLE_SWITCH));
6271 gsi_insert_on_edge (loc->e, assert_stmt);
6272 return true;
6275 /* Otherwise, we can insert right after LOC->SI iff the
6276 statement must not be the last statement in the block. */
6277 stmt = gsi_stmt (loc->si);
6278 if (!stmt_ends_bb_p (stmt))
6280 gsi_insert_after (&loc->si, assert_stmt, GSI_SAME_STMT);
6281 return false;
6284 /* If STMT must be the last statement in BB, we can only insert new
6285 assertions on the non-abnormal edge out of BB. Note that since
6286 STMT is not control flow, there may only be one non-abnormal edge
6287 out of BB. */
6288 FOR_EACH_EDGE (e, ei, loc->bb->succs)
6289 if (!(e->flags & EDGE_ABNORMAL))
6291 gsi_insert_on_edge (e, assert_stmt);
6292 return true;
6295 gcc_unreachable ();
6299 /* Process all the insertions registered for every name N_i registered
6300 in NEED_ASSERT_FOR. The list of assertions to be inserted are
6301 found in ASSERTS_FOR[i]. */
6303 static void
6304 process_assert_insertions (void)
6306 unsigned i;
6307 bitmap_iterator bi;
6308 bool update_edges_p = false;
6309 int num_asserts = 0;
6311 if (dump_file && (dump_flags & TDF_DETAILS))
6312 dump_all_asserts (dump_file);
6314 EXECUTE_IF_SET_IN_BITMAP (need_assert_for, 0, i, bi)
6316 assert_locus *loc = asserts_for[i];
6317 gcc_assert (loc);
6319 while (loc)
6321 assert_locus *next = loc->next;
6322 update_edges_p |= process_assert_insertions_for (ssa_name (i), loc);
6323 free (loc);
6324 loc = next;
6325 num_asserts++;
6329 if (update_edges_p)
6330 gsi_commit_edge_inserts ();
6332 statistics_counter_event (cfun, "Number of ASSERT_EXPR expressions inserted",
6333 num_asserts);
6337 /* Traverse the flowgraph looking for conditional jumps to insert range
6338 expressions. These range expressions are meant to provide information
6339 to optimizations that need to reason in terms of value ranges. They
6340 will not be expanded into RTL. For instance, given:
6342 x = ...
6343 y = ...
6344 if (x < y)
6345 y = x - 2;
6346 else
6347 x = y + 3;
6349 this pass will transform the code into:
6351 x = ...
6352 y = ...
6353 if (x < y)
6355 x = ASSERT_EXPR <x, x < y>
6356 y = x - 2
6358 else
6360 y = ASSERT_EXPR <y, x >= y>
6361 x = y + 3
6364 The idea is that once copy and constant propagation have run, other
6365 optimizations will be able to determine what ranges of values can 'x'
6366 take in different paths of the code, simply by checking the reaching
6367 definition of 'x'. */
6369 static void
6370 insert_range_assertions (void)
6372 need_assert_for = BITMAP_ALLOC (NULL);
6373 asserts_for = XCNEWVEC (assert_locus *, num_ssa_names);
6375 calculate_dominance_info (CDI_DOMINATORS);
6377 find_assert_locations ();
6378 if (!bitmap_empty_p (need_assert_for))
6380 process_assert_insertions ();
6381 update_ssa (TODO_update_ssa_no_phi);
6384 if (dump_file && (dump_flags & TDF_DETAILS))
6386 fprintf (dump_file, "\nSSA form after inserting ASSERT_EXPRs\n");
6387 dump_function_to_file (current_function_decl, dump_file, dump_flags);
6390 free (asserts_for);
6391 BITMAP_FREE (need_assert_for);
6394 /* Checks one ARRAY_REF in REF, located at LOCUS. Ignores flexible arrays
6395 and "struct" hacks. If VRP can determine that the
6396 array subscript is a constant, check if it is outside valid
6397 range. If the array subscript is a RANGE, warn if it is
6398 non-overlapping with valid range.
6399 IGNORE_OFF_BY_ONE is true if the ARRAY_REF is inside a ADDR_EXPR. */
6401 static void
6402 check_array_ref (location_t location, tree ref, bool ignore_off_by_one)
6404 value_range *vr = NULL;
6405 tree low_sub, up_sub;
6406 tree low_bound, up_bound, up_bound_p1;
6407 tree base;
6409 if (TREE_NO_WARNING (ref))
6410 return;
6412 low_sub = up_sub = TREE_OPERAND (ref, 1);
6413 up_bound = array_ref_up_bound (ref);
6415 /* Can not check flexible arrays. */
6416 if (!up_bound
6417 || TREE_CODE (up_bound) != INTEGER_CST)
6418 return;
6420 /* Accesses to trailing arrays via pointers may access storage
6421 beyond the types array bounds. */
6422 base = get_base_address (ref);
6423 if ((warn_array_bounds < 2)
6424 && base && TREE_CODE (base) == MEM_REF)
6426 tree cref, next = NULL_TREE;
6428 if (TREE_CODE (TREE_OPERAND (ref, 0)) != COMPONENT_REF)
6429 return;
6431 cref = TREE_OPERAND (ref, 0);
6432 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (cref, 0))) == RECORD_TYPE)
6433 for (next = DECL_CHAIN (TREE_OPERAND (cref, 1));
6434 next && TREE_CODE (next) != FIELD_DECL;
6435 next = DECL_CHAIN (next))
6438 /* If this is the last field in a struct type or a field in a
6439 union type do not warn. */
6440 if (!next)
6441 return;
6444 low_bound = array_ref_low_bound (ref);
6445 up_bound_p1 = int_const_binop (PLUS_EXPR, up_bound,
6446 build_int_cst (TREE_TYPE (up_bound), 1));
6448 /* Empty array. */
6449 if (tree_int_cst_equal (low_bound, up_bound_p1))
6451 warning_at (location, OPT_Warray_bounds,
6452 "array subscript is above array bounds");
6453 TREE_NO_WARNING (ref) = 1;
6456 if (TREE_CODE (low_sub) == SSA_NAME)
6458 vr = get_value_range (low_sub);
6459 if (vr->type == VR_RANGE || vr->type == VR_ANTI_RANGE)
6461 low_sub = vr->type == VR_RANGE ? vr->max : vr->min;
6462 up_sub = vr->type == VR_RANGE ? vr->min : vr->max;
6466 if (vr && vr->type == VR_ANTI_RANGE)
6468 if (TREE_CODE (up_sub) == INTEGER_CST
6469 && (ignore_off_by_one
6470 ? tree_int_cst_lt (up_bound, up_sub)
6471 : tree_int_cst_le (up_bound, up_sub))
6472 && TREE_CODE (low_sub) == INTEGER_CST
6473 && tree_int_cst_le (low_sub, low_bound))
6475 warning_at (location, OPT_Warray_bounds,
6476 "array subscript is outside array bounds");
6477 TREE_NO_WARNING (ref) = 1;
6480 else if (TREE_CODE (up_sub) == INTEGER_CST
6481 && (ignore_off_by_one
6482 ? !tree_int_cst_le (up_sub, up_bound_p1)
6483 : !tree_int_cst_le (up_sub, up_bound)))
6485 if (dump_file && (dump_flags & TDF_DETAILS))
6487 fprintf (dump_file, "Array bound warning for ");
6488 dump_generic_expr (MSG_NOTE, TDF_SLIM, ref);
6489 fprintf (dump_file, "\n");
6491 warning_at (location, OPT_Warray_bounds,
6492 "array subscript is above array bounds");
6493 TREE_NO_WARNING (ref) = 1;
6495 else if (TREE_CODE (low_sub) == INTEGER_CST
6496 && tree_int_cst_lt (low_sub, low_bound))
6498 if (dump_file && (dump_flags & TDF_DETAILS))
6500 fprintf (dump_file, "Array bound warning for ");
6501 dump_generic_expr (MSG_NOTE, TDF_SLIM, ref);
6502 fprintf (dump_file, "\n");
6504 warning_at (location, OPT_Warray_bounds,
6505 "array subscript is below array bounds");
6506 TREE_NO_WARNING (ref) = 1;
6510 /* Searches if the expr T, located at LOCATION computes
6511 address of an ARRAY_REF, and call check_array_ref on it. */
6513 static void
6514 search_for_addr_array (tree t, location_t location)
6516 /* Check each ARRAY_REFs in the reference chain. */
6519 if (TREE_CODE (t) == ARRAY_REF)
6520 check_array_ref (location, t, true /*ignore_off_by_one*/);
6522 t = TREE_OPERAND (t, 0);
6524 while (handled_component_p (t));
6526 if (TREE_CODE (t) == MEM_REF
6527 && TREE_CODE (TREE_OPERAND (t, 0)) == ADDR_EXPR
6528 && !TREE_NO_WARNING (t))
6530 tree tem = TREE_OPERAND (TREE_OPERAND (t, 0), 0);
6531 tree low_bound, up_bound, el_sz;
6532 offset_int idx;
6533 if (TREE_CODE (TREE_TYPE (tem)) != ARRAY_TYPE
6534 || TREE_CODE (TREE_TYPE (TREE_TYPE (tem))) == ARRAY_TYPE
6535 || !TYPE_DOMAIN (TREE_TYPE (tem)))
6536 return;
6538 low_bound = TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (tem)));
6539 up_bound = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (tem)));
6540 el_sz = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (tem)));
6541 if (!low_bound
6542 || TREE_CODE (low_bound) != INTEGER_CST
6543 || !up_bound
6544 || TREE_CODE (up_bound) != INTEGER_CST
6545 || !el_sz
6546 || TREE_CODE (el_sz) != INTEGER_CST)
6547 return;
6549 idx = mem_ref_offset (t);
6550 idx = wi::sdiv_trunc (idx, wi::to_offset (el_sz));
6551 if (wi::lts_p (idx, 0))
6553 if (dump_file && (dump_flags & TDF_DETAILS))
6555 fprintf (dump_file, "Array bound warning for ");
6556 dump_generic_expr (MSG_NOTE, TDF_SLIM, t);
6557 fprintf (dump_file, "\n");
6559 warning_at (location, OPT_Warray_bounds,
6560 "array subscript is below array bounds");
6561 TREE_NO_WARNING (t) = 1;
6563 else if (wi::gts_p (idx, (wi::to_offset (up_bound)
6564 - wi::to_offset (low_bound) + 1)))
6566 if (dump_file && (dump_flags & TDF_DETAILS))
6568 fprintf (dump_file, "Array bound warning for ");
6569 dump_generic_expr (MSG_NOTE, TDF_SLIM, t);
6570 fprintf (dump_file, "\n");
6572 warning_at (location, OPT_Warray_bounds,
6573 "array subscript is above array bounds");
6574 TREE_NO_WARNING (t) = 1;
6579 /* walk_tree() callback that checks if *TP is
6580 an ARRAY_REF inside an ADDR_EXPR (in which an array
6581 subscript one outside the valid range is allowed). Call
6582 check_array_ref for each ARRAY_REF found. The location is
6583 passed in DATA. */
6585 static tree
6586 check_array_bounds (tree *tp, int *walk_subtree, void *data)
6588 tree t = *tp;
6589 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
6590 location_t location;
6592 if (EXPR_HAS_LOCATION (t))
6593 location = EXPR_LOCATION (t);
6594 else
6596 location_t *locp = (location_t *) wi->info;
6597 location = *locp;
6600 *walk_subtree = TRUE;
6602 if (TREE_CODE (t) == ARRAY_REF)
6603 check_array_ref (location, t, false /*ignore_off_by_one*/);
6605 else if (TREE_CODE (t) == ADDR_EXPR)
6607 search_for_addr_array (t, location);
6608 *walk_subtree = FALSE;
6611 return NULL_TREE;
6614 /* Walk over all statements of all reachable BBs and call check_array_bounds
6615 on them. */
6617 static void
6618 check_all_array_refs (void)
6620 basic_block bb;
6621 gimple_stmt_iterator si;
6623 FOR_EACH_BB_FN (bb, cfun)
6625 edge_iterator ei;
6626 edge e;
6627 bool executable = false;
6629 /* Skip blocks that were found to be unreachable. */
6630 FOR_EACH_EDGE (e, ei, bb->preds)
6631 executable |= !!(e->flags & EDGE_EXECUTABLE);
6632 if (!executable)
6633 continue;
6635 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
6637 gimple *stmt = gsi_stmt (si);
6638 struct walk_stmt_info wi;
6639 if (!gimple_has_location (stmt)
6640 || is_gimple_debug (stmt))
6641 continue;
6643 memset (&wi, 0, sizeof (wi));
6645 location_t loc = gimple_location (stmt);
6646 wi.info = &loc;
6648 walk_gimple_op (gsi_stmt (si),
6649 check_array_bounds,
6650 &wi);
6655 /* Return true if all imm uses of VAR are either in STMT, or
6656 feed (optionally through a chain of single imm uses) GIMPLE_COND
6657 in basic block COND_BB. */
6659 static bool
6660 all_imm_uses_in_stmt_or_feed_cond (tree var, gimple *stmt, basic_block cond_bb)
6662 use_operand_p use_p, use2_p;
6663 imm_use_iterator iter;
6665 FOR_EACH_IMM_USE_FAST (use_p, iter, var)
6666 if (USE_STMT (use_p) != stmt)
6668 gimple *use_stmt = USE_STMT (use_p), *use_stmt2;
6669 if (is_gimple_debug (use_stmt))
6670 continue;
6671 while (is_gimple_assign (use_stmt)
6672 && TREE_CODE (gimple_assign_lhs (use_stmt)) == SSA_NAME
6673 && single_imm_use (gimple_assign_lhs (use_stmt),
6674 &use2_p, &use_stmt2))
6675 use_stmt = use_stmt2;
6676 if (gimple_code (use_stmt) != GIMPLE_COND
6677 || gimple_bb (use_stmt) != cond_bb)
6678 return false;
6680 return true;
6683 /* Handle
6684 _4 = x_3 & 31;
6685 if (_4 != 0)
6686 goto <bb 6>;
6687 else
6688 goto <bb 7>;
6689 <bb 6>:
6690 __builtin_unreachable ();
6691 <bb 7>:
6692 x_5 = ASSERT_EXPR <x_3, ...>;
6693 If x_3 has no other immediate uses (checked by caller),
6694 var is the x_3 var from ASSERT_EXPR, we can clear low 5 bits
6695 from the non-zero bitmask. */
6697 static void
6698 maybe_set_nonzero_bits (basic_block bb, tree var)
6700 edge e = single_pred_edge (bb);
6701 basic_block cond_bb = e->src;
6702 gimple *stmt = last_stmt (cond_bb);
6703 tree cst;
6705 if (stmt == NULL
6706 || gimple_code (stmt) != GIMPLE_COND
6707 || gimple_cond_code (stmt) != ((e->flags & EDGE_TRUE_VALUE)
6708 ? EQ_EXPR : NE_EXPR)
6709 || TREE_CODE (gimple_cond_lhs (stmt)) != SSA_NAME
6710 || !integer_zerop (gimple_cond_rhs (stmt)))
6711 return;
6713 stmt = SSA_NAME_DEF_STMT (gimple_cond_lhs (stmt));
6714 if (!is_gimple_assign (stmt)
6715 || gimple_assign_rhs_code (stmt) != BIT_AND_EXPR
6716 || TREE_CODE (gimple_assign_rhs2 (stmt)) != INTEGER_CST)
6717 return;
6718 if (gimple_assign_rhs1 (stmt) != var)
6720 gimple *stmt2;
6722 if (TREE_CODE (gimple_assign_rhs1 (stmt)) != SSA_NAME)
6723 return;
6724 stmt2 = SSA_NAME_DEF_STMT (gimple_assign_rhs1 (stmt));
6725 if (!gimple_assign_cast_p (stmt2)
6726 || gimple_assign_rhs1 (stmt2) != var
6727 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (stmt2))
6728 || (TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (stmt)))
6729 != TYPE_PRECISION (TREE_TYPE (var))))
6730 return;
6732 cst = gimple_assign_rhs2 (stmt);
6733 set_nonzero_bits (var, wi::bit_and_not (get_nonzero_bits (var), cst));
6736 /* Convert range assertion expressions into the implied copies and
6737 copy propagate away the copies. Doing the trivial copy propagation
6738 here avoids the need to run the full copy propagation pass after
6739 VRP.
6741 FIXME, this will eventually lead to copy propagation removing the
6742 names that had useful range information attached to them. For
6743 instance, if we had the assertion N_i = ASSERT_EXPR <N_j, N_j > 3>,
6744 then N_i will have the range [3, +INF].
6746 However, by converting the assertion into the implied copy
6747 operation N_i = N_j, we will then copy-propagate N_j into the uses
6748 of N_i and lose the range information. We may want to hold on to
6749 ASSERT_EXPRs a little while longer as the ranges could be used in
6750 things like jump threading.
6752 The problem with keeping ASSERT_EXPRs around is that passes after
6753 VRP need to handle them appropriately.
6755 Another approach would be to make the range information a first
6756 class property of the SSA_NAME so that it can be queried from
6757 any pass. This is made somewhat more complex by the need for
6758 multiple ranges to be associated with one SSA_NAME. */
6760 static void
6761 remove_range_assertions (void)
6763 basic_block bb;
6764 gimple_stmt_iterator si;
6765 /* 1 if looking at ASSERT_EXPRs immediately at the beginning of
6766 a basic block preceeded by GIMPLE_COND branching to it and
6767 __builtin_trap, -1 if not yet checked, 0 otherwise. */
6768 int is_unreachable;
6770 /* Note that the BSI iterator bump happens at the bottom of the
6771 loop and no bump is necessary if we're removing the statement
6772 referenced by the current BSI. */
6773 FOR_EACH_BB_FN (bb, cfun)
6774 for (si = gsi_after_labels (bb), is_unreachable = -1; !gsi_end_p (si);)
6776 gimple *stmt = gsi_stmt (si);
6777 gimple *use_stmt;
6779 if (is_gimple_assign (stmt)
6780 && gimple_assign_rhs_code (stmt) == ASSERT_EXPR)
6782 tree lhs = gimple_assign_lhs (stmt);
6783 tree rhs = gimple_assign_rhs1 (stmt);
6784 tree var;
6785 use_operand_p use_p;
6786 imm_use_iterator iter;
6788 var = ASSERT_EXPR_VAR (rhs);
6789 gcc_assert (TREE_CODE (var) == SSA_NAME);
6791 if (!POINTER_TYPE_P (TREE_TYPE (lhs))
6792 && SSA_NAME_RANGE_INFO (lhs))
6794 if (is_unreachable == -1)
6796 is_unreachable = 0;
6797 if (single_pred_p (bb)
6798 && assert_unreachable_fallthru_edge_p
6799 (single_pred_edge (bb)))
6800 is_unreachable = 1;
6802 /* Handle
6803 if (x_7 >= 10 && x_7 < 20)
6804 __builtin_unreachable ();
6805 x_8 = ASSERT_EXPR <x_7, ...>;
6806 if the only uses of x_7 are in the ASSERT_EXPR and
6807 in the condition. In that case, we can copy the
6808 range info from x_8 computed in this pass also
6809 for x_7. */
6810 if (is_unreachable
6811 && all_imm_uses_in_stmt_or_feed_cond (var, stmt,
6812 single_pred (bb)))
6814 set_range_info (var, SSA_NAME_RANGE_TYPE (lhs),
6815 SSA_NAME_RANGE_INFO (lhs)->get_min (),
6816 SSA_NAME_RANGE_INFO (lhs)->get_max ());
6817 maybe_set_nonzero_bits (bb, var);
6821 /* Propagate the RHS into every use of the LHS. */
6822 FOR_EACH_IMM_USE_STMT (use_stmt, iter, lhs)
6823 FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
6824 SET_USE (use_p, var);
6826 /* And finally, remove the copy, it is not needed. */
6827 gsi_remove (&si, true);
6828 release_defs (stmt);
6830 else
6832 if (!is_gimple_debug (gsi_stmt (si)))
6833 is_unreachable = 0;
6834 gsi_next (&si);
6840 /* Return true if STMT is interesting for VRP. */
6842 static bool
6843 stmt_interesting_for_vrp (gimple *stmt)
6845 if (gimple_code (stmt) == GIMPLE_PHI)
6847 tree res = gimple_phi_result (stmt);
6848 return (!virtual_operand_p (res)
6849 && (INTEGRAL_TYPE_P (TREE_TYPE (res))
6850 || POINTER_TYPE_P (TREE_TYPE (res))));
6852 else if (is_gimple_assign (stmt) || is_gimple_call (stmt))
6854 tree lhs = gimple_get_lhs (stmt);
6856 /* In general, assignments with virtual operands are not useful
6857 for deriving ranges, with the obvious exception of calls to
6858 builtin functions. */
6859 if (lhs && TREE_CODE (lhs) == SSA_NAME
6860 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs))
6861 || POINTER_TYPE_P (TREE_TYPE (lhs)))
6862 && (is_gimple_call (stmt)
6863 || !gimple_vuse (stmt)))
6864 return true;
6865 else if (is_gimple_call (stmt) && gimple_call_internal_p (stmt))
6866 switch (gimple_call_internal_fn (stmt))
6868 case IFN_ADD_OVERFLOW:
6869 case IFN_SUB_OVERFLOW:
6870 case IFN_MUL_OVERFLOW:
6871 /* These internal calls return _Complex integer type,
6872 but are interesting to VRP nevertheless. */
6873 if (lhs && TREE_CODE (lhs) == SSA_NAME)
6874 return true;
6875 break;
6876 default:
6877 break;
6880 else if (gimple_code (stmt) == GIMPLE_COND
6881 || gimple_code (stmt) == GIMPLE_SWITCH)
6882 return true;
6884 return false;
6888 /* Initialize local data structures for VRP. */
6890 static void
6891 vrp_initialize (void)
6893 basic_block bb;
6895 values_propagated = false;
6896 num_vr_values = num_ssa_names;
6897 vr_value = XCNEWVEC (value_range *, num_vr_values);
6898 vr_phi_edge_counts = XCNEWVEC (int, num_ssa_names);
6900 FOR_EACH_BB_FN (bb, cfun)
6902 for (gphi_iterator si = gsi_start_phis (bb); !gsi_end_p (si);
6903 gsi_next (&si))
6905 gphi *phi = si.phi ();
6906 if (!stmt_interesting_for_vrp (phi))
6908 tree lhs = PHI_RESULT (phi);
6909 set_value_range_to_varying (get_value_range (lhs));
6910 prop_set_simulate_again (phi, false);
6912 else
6913 prop_set_simulate_again (phi, true);
6916 for (gimple_stmt_iterator si = gsi_start_bb (bb); !gsi_end_p (si);
6917 gsi_next (&si))
6919 gimple *stmt = gsi_stmt (si);
6921 /* If the statement is a control insn, then we do not
6922 want to avoid simulating the statement once. Failure
6923 to do so means that those edges will never get added. */
6924 if (stmt_ends_bb_p (stmt))
6925 prop_set_simulate_again (stmt, true);
6926 else if (!stmt_interesting_for_vrp (stmt))
6928 ssa_op_iter i;
6929 tree def;
6930 FOR_EACH_SSA_TREE_OPERAND (def, stmt, i, SSA_OP_DEF)
6931 set_value_range_to_varying (get_value_range (def));
6932 prop_set_simulate_again (stmt, false);
6934 else
6935 prop_set_simulate_again (stmt, true);
6940 /* Return the singleton value-range for NAME or NAME. */
6942 static inline tree
6943 vrp_valueize (tree name)
6945 if (TREE_CODE (name) == SSA_NAME)
6947 value_range *vr = get_value_range (name);
6948 if (vr->type == VR_RANGE
6949 && (vr->min == vr->max
6950 || operand_equal_p (vr->min, vr->max, 0)))
6951 return vr->min;
6953 return name;
6956 /* Return the singleton value-range for NAME if that is a constant
6957 but signal to not follow SSA edges. */
6959 static inline tree
6960 vrp_valueize_1 (tree name)
6962 if (TREE_CODE (name) == SSA_NAME)
6964 /* If the definition may be simulated again we cannot follow
6965 this SSA edge as the SSA propagator does not necessarily
6966 re-visit the use. */
6967 gimple *def_stmt = SSA_NAME_DEF_STMT (name);
6968 if (!gimple_nop_p (def_stmt)
6969 && prop_simulate_again_p (def_stmt))
6970 return NULL_TREE;
6971 value_range *vr = get_value_range (name);
6972 if (range_int_cst_singleton_p (vr))
6973 return vr->min;
6975 return name;
6978 /* Visit assignment STMT. If it produces an interesting range, record
6979 the SSA name in *OUTPUT_P. */
6981 static enum ssa_prop_result
6982 vrp_visit_assignment_or_call (gimple *stmt, tree *output_p)
6984 tree def, lhs;
6985 ssa_op_iter iter;
6986 enum gimple_code code = gimple_code (stmt);
6987 lhs = gimple_get_lhs (stmt);
6989 /* We only keep track of ranges in integral and pointer types. */
6990 if (TREE_CODE (lhs) == SSA_NAME
6991 && ((INTEGRAL_TYPE_P (TREE_TYPE (lhs))
6992 /* It is valid to have NULL MIN/MAX values on a type. See
6993 build_range_type. */
6994 && TYPE_MIN_VALUE (TREE_TYPE (lhs))
6995 && TYPE_MAX_VALUE (TREE_TYPE (lhs)))
6996 || POINTER_TYPE_P (TREE_TYPE (lhs))))
6998 value_range new_vr = VR_INITIALIZER;
7000 /* Try folding the statement to a constant first. */
7001 tree tem = gimple_fold_stmt_to_constant_1 (stmt, vrp_valueize,
7002 vrp_valueize_1);
7003 if (tem && is_gimple_min_invariant (tem))
7004 set_value_range_to_value (&new_vr, tem, NULL);
7005 /* Then dispatch to value-range extracting functions. */
7006 else if (code == GIMPLE_CALL)
7007 extract_range_basic (&new_vr, stmt);
7008 else
7009 extract_range_from_assignment (&new_vr, as_a <gassign *> (stmt));
7011 if (update_value_range (lhs, &new_vr))
7013 *output_p = lhs;
7015 if (dump_file && (dump_flags & TDF_DETAILS))
7017 fprintf (dump_file, "Found new range for ");
7018 print_generic_expr (dump_file, lhs, 0);
7019 fprintf (dump_file, ": ");
7020 dump_value_range (dump_file, &new_vr);
7021 fprintf (dump_file, "\n");
7024 if (new_vr.type == VR_VARYING)
7025 return SSA_PROP_VARYING;
7027 return SSA_PROP_INTERESTING;
7030 return SSA_PROP_NOT_INTERESTING;
7032 else if (is_gimple_call (stmt) && gimple_call_internal_p (stmt))
7033 switch (gimple_call_internal_fn (stmt))
7035 case IFN_ADD_OVERFLOW:
7036 case IFN_SUB_OVERFLOW:
7037 case IFN_MUL_OVERFLOW:
7038 /* These internal calls return _Complex integer type,
7039 which VRP does not track, but the immediate uses
7040 thereof might be interesting. */
7041 if (lhs && TREE_CODE (lhs) == SSA_NAME)
7043 imm_use_iterator iter;
7044 use_operand_p use_p;
7045 enum ssa_prop_result res = SSA_PROP_VARYING;
7047 set_value_range_to_varying (get_value_range (lhs));
7049 FOR_EACH_IMM_USE_FAST (use_p, iter, lhs)
7051 gimple *use_stmt = USE_STMT (use_p);
7052 if (!is_gimple_assign (use_stmt))
7053 continue;
7054 enum tree_code rhs_code = gimple_assign_rhs_code (use_stmt);
7055 if (rhs_code != REALPART_EXPR && rhs_code != IMAGPART_EXPR)
7056 continue;
7057 tree rhs1 = gimple_assign_rhs1 (use_stmt);
7058 tree use_lhs = gimple_assign_lhs (use_stmt);
7059 if (TREE_CODE (rhs1) != rhs_code
7060 || TREE_OPERAND (rhs1, 0) != lhs
7061 || TREE_CODE (use_lhs) != SSA_NAME
7062 || !stmt_interesting_for_vrp (use_stmt)
7063 || (!INTEGRAL_TYPE_P (TREE_TYPE (use_lhs))
7064 || !TYPE_MIN_VALUE (TREE_TYPE (use_lhs))
7065 || !TYPE_MAX_VALUE (TREE_TYPE (use_lhs))))
7066 continue;
7068 /* If there is a change in the value range for any of the
7069 REALPART_EXPR/IMAGPART_EXPR immediate uses, return
7070 SSA_PROP_INTERESTING. If there are any REALPART_EXPR
7071 or IMAGPART_EXPR immediate uses, but none of them have
7072 a change in their value ranges, return
7073 SSA_PROP_NOT_INTERESTING. If there are no
7074 {REAL,IMAG}PART_EXPR uses at all,
7075 return SSA_PROP_VARYING. */
7076 value_range new_vr = VR_INITIALIZER;
7077 extract_range_basic (&new_vr, use_stmt);
7078 value_range *old_vr = get_value_range (use_lhs);
7079 if (old_vr->type != new_vr.type
7080 || !vrp_operand_equal_p (old_vr->min, new_vr.min)
7081 || !vrp_operand_equal_p (old_vr->max, new_vr.max)
7082 || !vrp_bitmap_equal_p (old_vr->equiv, new_vr.equiv))
7083 res = SSA_PROP_INTERESTING;
7084 else
7085 res = SSA_PROP_NOT_INTERESTING;
7086 BITMAP_FREE (new_vr.equiv);
7087 if (res == SSA_PROP_INTERESTING)
7089 *output_p = lhs;
7090 return res;
7094 return res;
7096 break;
7097 default:
7098 break;
7101 /* Every other statement produces no useful ranges. */
7102 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_DEF)
7103 set_value_range_to_varying (get_value_range (def));
7105 return SSA_PROP_VARYING;
7108 /* Helper that gets the value range of the SSA_NAME with version I
7109 or a symbolic range containing the SSA_NAME only if the value range
7110 is varying or undefined. */
7112 static inline value_range
7113 get_vr_for_comparison (int i)
7115 value_range vr = *get_value_range (ssa_name (i));
7117 /* If name N_i does not have a valid range, use N_i as its own
7118 range. This allows us to compare against names that may
7119 have N_i in their ranges. */
7120 if (vr.type == VR_VARYING || vr.type == VR_UNDEFINED)
7122 vr.type = VR_RANGE;
7123 vr.min = ssa_name (i);
7124 vr.max = ssa_name (i);
7127 return vr;
7130 /* Compare all the value ranges for names equivalent to VAR with VAL
7131 using comparison code COMP. Return the same value returned by
7132 compare_range_with_value, including the setting of
7133 *STRICT_OVERFLOW_P. */
7135 static tree
7136 compare_name_with_value (enum tree_code comp, tree var, tree val,
7137 bool *strict_overflow_p)
7139 bitmap_iterator bi;
7140 unsigned i;
7141 bitmap e;
7142 tree retval, t;
7143 int used_strict_overflow;
7144 bool sop;
7145 value_range equiv_vr;
7147 /* Get the set of equivalences for VAR. */
7148 e = get_value_range (var)->equiv;
7150 /* Start at -1. Set it to 0 if we do a comparison without relying
7151 on overflow, or 1 if all comparisons rely on overflow. */
7152 used_strict_overflow = -1;
7154 /* Compare vars' value range with val. */
7155 equiv_vr = get_vr_for_comparison (SSA_NAME_VERSION (var));
7156 sop = false;
7157 retval = compare_range_with_value (comp, &equiv_vr, val, &sop);
7158 if (retval)
7159 used_strict_overflow = sop ? 1 : 0;
7161 /* If the equiv set is empty we have done all work we need to do. */
7162 if (e == NULL)
7164 if (retval
7165 && used_strict_overflow > 0)
7166 *strict_overflow_p = true;
7167 return retval;
7170 EXECUTE_IF_SET_IN_BITMAP (e, 0, i, bi)
7172 equiv_vr = get_vr_for_comparison (i);
7173 sop = false;
7174 t = compare_range_with_value (comp, &equiv_vr, val, &sop);
7175 if (t)
7177 /* If we get different answers from different members
7178 of the equivalence set this check must be in a dead
7179 code region. Folding it to a trap representation
7180 would be correct here. For now just return don't-know. */
7181 if (retval != NULL
7182 && t != retval)
7184 retval = NULL_TREE;
7185 break;
7187 retval = t;
7189 if (!sop)
7190 used_strict_overflow = 0;
7191 else if (used_strict_overflow < 0)
7192 used_strict_overflow = 1;
7196 if (retval
7197 && used_strict_overflow > 0)
7198 *strict_overflow_p = true;
7200 return retval;
7204 /* Given a comparison code COMP and names N1 and N2, compare all the
7205 ranges equivalent to N1 against all the ranges equivalent to N2
7206 to determine the value of N1 COMP N2. Return the same value
7207 returned by compare_ranges. Set *STRICT_OVERFLOW_P to indicate
7208 whether we relied on an overflow infinity in the comparison. */
7211 static tree
7212 compare_names (enum tree_code comp, tree n1, tree n2,
7213 bool *strict_overflow_p)
7215 tree t, retval;
7216 bitmap e1, e2;
7217 bitmap_iterator bi1, bi2;
7218 unsigned i1, i2;
7219 int used_strict_overflow;
7220 static bitmap_obstack *s_obstack = NULL;
7221 static bitmap s_e1 = NULL, s_e2 = NULL;
7223 /* Compare the ranges of every name equivalent to N1 against the
7224 ranges of every name equivalent to N2. */
7225 e1 = get_value_range (n1)->equiv;
7226 e2 = get_value_range (n2)->equiv;
7228 /* Use the fake bitmaps if e1 or e2 are not available. */
7229 if (s_obstack == NULL)
7231 s_obstack = XNEW (bitmap_obstack);
7232 bitmap_obstack_initialize (s_obstack);
7233 s_e1 = BITMAP_ALLOC (s_obstack);
7234 s_e2 = BITMAP_ALLOC (s_obstack);
7236 if (e1 == NULL)
7237 e1 = s_e1;
7238 if (e2 == NULL)
7239 e2 = s_e2;
7241 /* Add N1 and N2 to their own set of equivalences to avoid
7242 duplicating the body of the loop just to check N1 and N2
7243 ranges. */
7244 bitmap_set_bit (e1, SSA_NAME_VERSION (n1));
7245 bitmap_set_bit (e2, SSA_NAME_VERSION (n2));
7247 /* If the equivalence sets have a common intersection, then the two
7248 names can be compared without checking their ranges. */
7249 if (bitmap_intersect_p (e1, e2))
7251 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
7252 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
7254 return (comp == EQ_EXPR || comp == GE_EXPR || comp == LE_EXPR)
7255 ? boolean_true_node
7256 : boolean_false_node;
7259 /* Start at -1. Set it to 0 if we do a comparison without relying
7260 on overflow, or 1 if all comparisons rely on overflow. */
7261 used_strict_overflow = -1;
7263 /* Otherwise, compare all the equivalent ranges. First, add N1 and
7264 N2 to their own set of equivalences to avoid duplicating the body
7265 of the loop just to check N1 and N2 ranges. */
7266 EXECUTE_IF_SET_IN_BITMAP (e1, 0, i1, bi1)
7268 value_range vr1 = get_vr_for_comparison (i1);
7270 t = retval = NULL_TREE;
7271 EXECUTE_IF_SET_IN_BITMAP (e2, 0, i2, bi2)
7273 bool sop = false;
7275 value_range vr2 = get_vr_for_comparison (i2);
7277 t = compare_ranges (comp, &vr1, &vr2, &sop);
7278 if (t)
7280 /* If we get different answers from different members
7281 of the equivalence set this check must be in a dead
7282 code region. Folding it to a trap representation
7283 would be correct here. For now just return don't-know. */
7284 if (retval != NULL
7285 && t != retval)
7287 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
7288 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
7289 return NULL_TREE;
7291 retval = t;
7293 if (!sop)
7294 used_strict_overflow = 0;
7295 else if (used_strict_overflow < 0)
7296 used_strict_overflow = 1;
7300 if (retval)
7302 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
7303 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
7304 if (used_strict_overflow > 0)
7305 *strict_overflow_p = true;
7306 return retval;
7310 /* None of the equivalent ranges are useful in computing this
7311 comparison. */
7312 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
7313 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
7314 return NULL_TREE;
7317 /* Helper function for vrp_evaluate_conditional_warnv & other
7318 optimizers. */
7320 static tree
7321 vrp_evaluate_conditional_warnv_with_ops_using_ranges (enum tree_code code,
7322 tree op0, tree op1,
7323 bool * strict_overflow_p)
7325 value_range *vr0, *vr1;
7327 vr0 = (TREE_CODE (op0) == SSA_NAME) ? get_value_range (op0) : NULL;
7328 vr1 = (TREE_CODE (op1) == SSA_NAME) ? get_value_range (op1) : NULL;
7330 tree res = NULL_TREE;
7331 if (vr0 && vr1)
7332 res = compare_ranges (code, vr0, vr1, strict_overflow_p);
7333 if (!res && vr0)
7334 res = compare_range_with_value (code, vr0, op1, strict_overflow_p);
7335 if (!res && vr1)
7336 res = (compare_range_with_value
7337 (swap_tree_comparison (code), vr1, op0, strict_overflow_p));
7338 return res;
7341 /* Helper function for vrp_evaluate_conditional_warnv. */
7343 static tree
7344 vrp_evaluate_conditional_warnv_with_ops (enum tree_code code, tree op0,
7345 tree op1, bool use_equiv_p,
7346 bool *strict_overflow_p, bool *only_ranges)
7348 tree ret;
7349 if (only_ranges)
7350 *only_ranges = true;
7352 /* We only deal with integral and pointer types. */
7353 if (!INTEGRAL_TYPE_P (TREE_TYPE (op0))
7354 && !POINTER_TYPE_P (TREE_TYPE (op0)))
7355 return NULL_TREE;
7357 if (use_equiv_p)
7359 if (only_ranges
7360 && (ret = vrp_evaluate_conditional_warnv_with_ops_using_ranges
7361 (code, op0, op1, strict_overflow_p)))
7362 return ret;
7363 *only_ranges = false;
7364 if (TREE_CODE (op0) == SSA_NAME && TREE_CODE (op1) == SSA_NAME)
7365 return compare_names (code, op0, op1, strict_overflow_p);
7366 else if (TREE_CODE (op0) == SSA_NAME)
7367 return compare_name_with_value (code, op0, op1, strict_overflow_p);
7368 else if (TREE_CODE (op1) == SSA_NAME)
7369 return (compare_name_with_value
7370 (swap_tree_comparison (code), op1, op0, strict_overflow_p));
7372 else
7373 return vrp_evaluate_conditional_warnv_with_ops_using_ranges (code, op0, op1,
7374 strict_overflow_p);
7375 return NULL_TREE;
7378 /* Given (CODE OP0 OP1) within STMT, try to simplify it based on value range
7379 information. Return NULL if the conditional can not be evaluated.
7380 The ranges of all the names equivalent with the operands in COND
7381 will be used when trying to compute the value. If the result is
7382 based on undefined signed overflow, issue a warning if
7383 appropriate. */
7385 static tree
7386 vrp_evaluate_conditional (tree_code code, tree op0, tree op1, gimple *stmt)
7388 bool sop;
7389 tree ret;
7390 bool only_ranges;
7392 /* Some passes and foldings leak constants with overflow flag set
7393 into the IL. Avoid doing wrong things with these and bail out. */
7394 if ((TREE_CODE (op0) == INTEGER_CST
7395 && TREE_OVERFLOW (op0))
7396 || (TREE_CODE (op1) == INTEGER_CST
7397 && TREE_OVERFLOW (op1)))
7398 return NULL_TREE;
7400 sop = false;
7401 ret = vrp_evaluate_conditional_warnv_with_ops (code, op0, op1, true, &sop,
7402 &only_ranges);
7404 if (ret && sop)
7406 enum warn_strict_overflow_code wc;
7407 const char* warnmsg;
7409 if (is_gimple_min_invariant (ret))
7411 wc = WARN_STRICT_OVERFLOW_CONDITIONAL;
7412 warnmsg = G_("assuming signed overflow does not occur when "
7413 "simplifying conditional to constant");
7415 else
7417 wc = WARN_STRICT_OVERFLOW_COMPARISON;
7418 warnmsg = G_("assuming signed overflow does not occur when "
7419 "simplifying conditional");
7422 if (issue_strict_overflow_warning (wc))
7424 location_t location;
7426 if (!gimple_has_location (stmt))
7427 location = input_location;
7428 else
7429 location = gimple_location (stmt);
7430 warning_at (location, OPT_Wstrict_overflow, "%s", warnmsg);
7434 if (warn_type_limits
7435 && ret && only_ranges
7436 && TREE_CODE_CLASS (code) == tcc_comparison
7437 && TREE_CODE (op0) == SSA_NAME)
7439 /* If the comparison is being folded and the operand on the LHS
7440 is being compared against a constant value that is outside of
7441 the natural range of OP0's type, then the predicate will
7442 always fold regardless of the value of OP0. If -Wtype-limits
7443 was specified, emit a warning. */
7444 tree type = TREE_TYPE (op0);
7445 value_range *vr0 = get_value_range (op0);
7447 if (vr0->type == VR_RANGE
7448 && INTEGRAL_TYPE_P (type)
7449 && vrp_val_is_min (vr0->min)
7450 && vrp_val_is_max (vr0->max)
7451 && is_gimple_min_invariant (op1))
7453 location_t location;
7455 if (!gimple_has_location (stmt))
7456 location = input_location;
7457 else
7458 location = gimple_location (stmt);
7460 warning_at (location, OPT_Wtype_limits,
7461 integer_zerop (ret)
7462 ? G_("comparison always false "
7463 "due to limited range of data type")
7464 : G_("comparison always true "
7465 "due to limited range of data type"));
7469 return ret;
7473 /* Visit conditional statement STMT. If we can determine which edge
7474 will be taken out of STMT's basic block, record it in
7475 *TAKEN_EDGE_P and return SSA_PROP_INTERESTING. Otherwise, return
7476 SSA_PROP_VARYING. */
7478 static enum ssa_prop_result
7479 vrp_visit_cond_stmt (gcond *stmt, edge *taken_edge_p)
7481 tree val;
7482 bool sop;
7484 *taken_edge_p = NULL;
7486 if (dump_file && (dump_flags & TDF_DETAILS))
7488 tree use;
7489 ssa_op_iter i;
7491 fprintf (dump_file, "\nVisiting conditional with predicate: ");
7492 print_gimple_stmt (dump_file, stmt, 0, 0);
7493 fprintf (dump_file, "\nWith known ranges\n");
7495 FOR_EACH_SSA_TREE_OPERAND (use, stmt, i, SSA_OP_USE)
7497 fprintf (dump_file, "\t");
7498 print_generic_expr (dump_file, use, 0);
7499 fprintf (dump_file, ": ");
7500 dump_value_range (dump_file, vr_value[SSA_NAME_VERSION (use)]);
7503 fprintf (dump_file, "\n");
7506 /* Compute the value of the predicate COND by checking the known
7507 ranges of each of its operands.
7509 Note that we cannot evaluate all the equivalent ranges here
7510 because those ranges may not yet be final and with the current
7511 propagation strategy, we cannot determine when the value ranges
7512 of the names in the equivalence set have changed.
7514 For instance, given the following code fragment
7516 i_5 = PHI <8, i_13>
7518 i_14 = ASSERT_EXPR <i_5, i_5 != 0>
7519 if (i_14 == 1)
7522 Assume that on the first visit to i_14, i_5 has the temporary
7523 range [8, 8] because the second argument to the PHI function is
7524 not yet executable. We derive the range ~[0, 0] for i_14 and the
7525 equivalence set { i_5 }. So, when we visit 'if (i_14 == 1)' for
7526 the first time, since i_14 is equivalent to the range [8, 8], we
7527 determine that the predicate is always false.
7529 On the next round of propagation, i_13 is determined to be
7530 VARYING, which causes i_5 to drop down to VARYING. So, another
7531 visit to i_14 is scheduled. In this second visit, we compute the
7532 exact same range and equivalence set for i_14, namely ~[0, 0] and
7533 { i_5 }. But we did not have the previous range for i_5
7534 registered, so vrp_visit_assignment thinks that the range for
7535 i_14 has not changed. Therefore, the predicate 'if (i_14 == 1)'
7536 is not visited again, which stops propagation from visiting
7537 statements in the THEN clause of that if().
7539 To properly fix this we would need to keep the previous range
7540 value for the names in the equivalence set. This way we would've
7541 discovered that from one visit to the other i_5 changed from
7542 range [8, 8] to VR_VARYING.
7544 However, fixing this apparent limitation may not be worth the
7545 additional checking. Testing on several code bases (GCC, DLV,
7546 MICO, TRAMP3D and SPEC2000) showed that doing this results in
7547 4 more predicates folded in SPEC. */
7548 sop = false;
7550 val = vrp_evaluate_conditional_warnv_with_ops (gimple_cond_code (stmt),
7551 gimple_cond_lhs (stmt),
7552 gimple_cond_rhs (stmt),
7553 false, &sop, NULL);
7554 if (val)
7556 if (!sop)
7557 *taken_edge_p = find_taken_edge (gimple_bb (stmt), val);
7558 else
7560 if (dump_file && (dump_flags & TDF_DETAILS))
7561 fprintf (dump_file,
7562 "\nIgnoring predicate evaluation because "
7563 "it assumes that signed overflow is undefined");
7564 val = NULL_TREE;
7568 if (dump_file && (dump_flags & TDF_DETAILS))
7570 fprintf (dump_file, "\nPredicate evaluates to: ");
7571 if (val == NULL_TREE)
7572 fprintf (dump_file, "DON'T KNOW\n");
7573 else
7574 print_generic_stmt (dump_file, val, 0);
7577 return (*taken_edge_p) ? SSA_PROP_INTERESTING : SSA_PROP_VARYING;
7580 /* Searches the case label vector VEC for the index *IDX of the CASE_LABEL
7581 that includes the value VAL. The search is restricted to the range
7582 [START_IDX, n - 1] where n is the size of VEC.
7584 If there is a CASE_LABEL for VAL, its index is placed in IDX and true is
7585 returned.
7587 If there is no CASE_LABEL for VAL and there is one that is larger than VAL,
7588 it is placed in IDX and false is returned.
7590 If VAL is larger than any CASE_LABEL, n is placed on IDX and false is
7591 returned. */
7593 static bool
7594 find_case_label_index (gswitch *stmt, size_t start_idx, tree val, size_t *idx)
7596 size_t n = gimple_switch_num_labels (stmt);
7597 size_t low, high;
7599 /* Find case label for minimum of the value range or the next one.
7600 At each iteration we are searching in [low, high - 1]. */
7602 for (low = start_idx, high = n; high != low; )
7604 tree t;
7605 int cmp;
7606 /* Note that i != high, so we never ask for n. */
7607 size_t i = (high + low) / 2;
7608 t = gimple_switch_label (stmt, i);
7610 /* Cache the result of comparing CASE_LOW and val. */
7611 cmp = tree_int_cst_compare (CASE_LOW (t), val);
7613 if (cmp == 0)
7615 /* Ranges cannot be empty. */
7616 *idx = i;
7617 return true;
7619 else if (cmp > 0)
7620 high = i;
7621 else
7623 low = i + 1;
7624 if (CASE_HIGH (t) != NULL
7625 && tree_int_cst_compare (CASE_HIGH (t), val) >= 0)
7627 *idx = i;
7628 return true;
7633 *idx = high;
7634 return false;
7637 /* Searches the case label vector VEC for the range of CASE_LABELs that is used
7638 for values between MIN and MAX. The first index is placed in MIN_IDX. The
7639 last index is placed in MAX_IDX. If the range of CASE_LABELs is empty
7640 then MAX_IDX < MIN_IDX.
7641 Returns true if the default label is not needed. */
7643 static bool
7644 find_case_label_range (gswitch *stmt, tree min, tree max, size_t *min_idx,
7645 size_t *max_idx)
7647 size_t i, j;
7648 bool min_take_default = !find_case_label_index (stmt, 1, min, &i);
7649 bool max_take_default = !find_case_label_index (stmt, i, max, &j);
7651 if (i == j
7652 && min_take_default
7653 && max_take_default)
7655 /* Only the default case label reached.
7656 Return an empty range. */
7657 *min_idx = 1;
7658 *max_idx = 0;
7659 return false;
7661 else
7663 bool take_default = min_take_default || max_take_default;
7664 tree low, high;
7665 size_t k;
7667 if (max_take_default)
7668 j--;
7670 /* If the case label range is continuous, we do not need
7671 the default case label. Verify that. */
7672 high = CASE_LOW (gimple_switch_label (stmt, i));
7673 if (CASE_HIGH (gimple_switch_label (stmt, i)))
7674 high = CASE_HIGH (gimple_switch_label (stmt, i));
7675 for (k = i + 1; k <= j; ++k)
7677 low = CASE_LOW (gimple_switch_label (stmt, k));
7678 if (!integer_onep (int_const_binop (MINUS_EXPR, low, high)))
7680 take_default = true;
7681 break;
7683 high = low;
7684 if (CASE_HIGH (gimple_switch_label (stmt, k)))
7685 high = CASE_HIGH (gimple_switch_label (stmt, k));
7688 *min_idx = i;
7689 *max_idx = j;
7690 return !take_default;
7694 /* Searches the case label vector VEC for the ranges of CASE_LABELs that are
7695 used in range VR. The indices are placed in MIN_IDX1, MAX_IDX, MIN_IDX2 and
7696 MAX_IDX2. If the ranges of CASE_LABELs are empty then MAX_IDX1 < MIN_IDX1.
7697 Returns true if the default label is not needed. */
7699 static bool
7700 find_case_label_ranges (gswitch *stmt, value_range *vr, size_t *min_idx1,
7701 size_t *max_idx1, size_t *min_idx2,
7702 size_t *max_idx2)
7704 size_t i, j, k, l;
7705 unsigned int n = gimple_switch_num_labels (stmt);
7706 bool take_default;
7707 tree case_low, case_high;
7708 tree min = vr->min, max = vr->max;
7710 gcc_checking_assert (vr->type == VR_RANGE || vr->type == VR_ANTI_RANGE);
7712 take_default = !find_case_label_range (stmt, min, max, &i, &j);
7714 /* Set second range to emtpy. */
7715 *min_idx2 = 1;
7716 *max_idx2 = 0;
7718 if (vr->type == VR_RANGE)
7720 *min_idx1 = i;
7721 *max_idx1 = j;
7722 return !take_default;
7725 /* Set first range to all case labels. */
7726 *min_idx1 = 1;
7727 *max_idx1 = n - 1;
7729 if (i > j)
7730 return false;
7732 /* Make sure all the values of case labels [i , j] are contained in
7733 range [MIN, MAX]. */
7734 case_low = CASE_LOW (gimple_switch_label (stmt, i));
7735 case_high = CASE_HIGH (gimple_switch_label (stmt, j));
7736 if (tree_int_cst_compare (case_low, min) < 0)
7737 i += 1;
7738 if (case_high != NULL_TREE
7739 && tree_int_cst_compare (max, case_high) < 0)
7740 j -= 1;
7742 if (i > j)
7743 return false;
7745 /* If the range spans case labels [i, j], the corresponding anti-range spans
7746 the labels [1, i - 1] and [j + 1, n - 1]. */
7747 k = j + 1;
7748 l = n - 1;
7749 if (k > l)
7751 k = 1;
7752 l = 0;
7755 j = i - 1;
7756 i = 1;
7757 if (i > j)
7759 i = k;
7760 j = l;
7761 k = 1;
7762 l = 0;
7765 *min_idx1 = i;
7766 *max_idx1 = j;
7767 *min_idx2 = k;
7768 *max_idx2 = l;
7769 return false;
7772 /* Visit switch statement STMT. If we can determine which edge
7773 will be taken out of STMT's basic block, record it in
7774 *TAKEN_EDGE_P and return SSA_PROP_INTERESTING. Otherwise, return
7775 SSA_PROP_VARYING. */
7777 static enum ssa_prop_result
7778 vrp_visit_switch_stmt (gswitch *stmt, edge *taken_edge_p)
7780 tree op, val;
7781 value_range *vr;
7782 size_t i = 0, j = 0, k, l;
7783 bool take_default;
7785 *taken_edge_p = NULL;
7786 op = gimple_switch_index (stmt);
7787 if (TREE_CODE (op) != SSA_NAME)
7788 return SSA_PROP_VARYING;
7790 vr = get_value_range (op);
7791 if (dump_file && (dump_flags & TDF_DETAILS))
7793 fprintf (dump_file, "\nVisiting switch expression with operand ");
7794 print_generic_expr (dump_file, op, 0);
7795 fprintf (dump_file, " with known range ");
7796 dump_value_range (dump_file, vr);
7797 fprintf (dump_file, "\n");
7800 if ((vr->type != VR_RANGE
7801 && vr->type != VR_ANTI_RANGE)
7802 || symbolic_range_p (vr))
7803 return SSA_PROP_VARYING;
7805 /* Find the single edge that is taken from the switch expression. */
7806 take_default = !find_case_label_ranges (stmt, vr, &i, &j, &k, &l);
7808 /* Check if the range spans no CASE_LABEL. If so, we only reach the default
7809 label */
7810 if (j < i)
7812 gcc_assert (take_default);
7813 val = gimple_switch_default_label (stmt);
7815 else
7817 /* Check if labels with index i to j and maybe the default label
7818 are all reaching the same label. */
7820 val = gimple_switch_label (stmt, i);
7821 if (take_default
7822 && CASE_LABEL (gimple_switch_default_label (stmt))
7823 != CASE_LABEL (val))
7825 if (dump_file && (dump_flags & TDF_DETAILS))
7826 fprintf (dump_file, " not a single destination for this "
7827 "range\n");
7828 return SSA_PROP_VARYING;
7830 for (++i; i <= j; ++i)
7832 if (CASE_LABEL (gimple_switch_label (stmt, i)) != CASE_LABEL (val))
7834 if (dump_file && (dump_flags & TDF_DETAILS))
7835 fprintf (dump_file, " not a single destination for this "
7836 "range\n");
7837 return SSA_PROP_VARYING;
7840 for (; k <= l; ++k)
7842 if (CASE_LABEL (gimple_switch_label (stmt, k)) != CASE_LABEL (val))
7844 if (dump_file && (dump_flags & TDF_DETAILS))
7845 fprintf (dump_file, " not a single destination for this "
7846 "range\n");
7847 return SSA_PROP_VARYING;
7852 *taken_edge_p = find_edge (gimple_bb (stmt),
7853 label_to_block (CASE_LABEL (val)));
7855 if (dump_file && (dump_flags & TDF_DETAILS))
7857 fprintf (dump_file, " will take edge to ");
7858 print_generic_stmt (dump_file, CASE_LABEL (val), 0);
7861 return SSA_PROP_INTERESTING;
7865 /* Evaluate statement STMT. If the statement produces a useful range,
7866 return SSA_PROP_INTERESTING and record the SSA name with the
7867 interesting range into *OUTPUT_P.
7869 If STMT is a conditional branch and we can determine its truth
7870 value, the taken edge is recorded in *TAKEN_EDGE_P.
7872 If STMT produces a varying value, return SSA_PROP_VARYING. */
7874 static enum ssa_prop_result
7875 vrp_visit_stmt (gimple *stmt, edge *taken_edge_p, tree *output_p)
7877 tree def;
7878 ssa_op_iter iter;
7880 if (dump_file && (dump_flags & TDF_DETAILS))
7882 fprintf (dump_file, "\nVisiting statement:\n");
7883 print_gimple_stmt (dump_file, stmt, 0, dump_flags);
7886 if (!stmt_interesting_for_vrp (stmt))
7887 gcc_assert (stmt_ends_bb_p (stmt));
7888 else if (is_gimple_assign (stmt) || is_gimple_call (stmt))
7889 return vrp_visit_assignment_or_call (stmt, output_p);
7890 else if (gimple_code (stmt) == GIMPLE_COND)
7891 return vrp_visit_cond_stmt (as_a <gcond *> (stmt), taken_edge_p);
7892 else if (gimple_code (stmt) == GIMPLE_SWITCH)
7893 return vrp_visit_switch_stmt (as_a <gswitch *> (stmt), taken_edge_p);
7895 /* All other statements produce nothing of interest for VRP, so mark
7896 their outputs varying and prevent further simulation. */
7897 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_DEF)
7898 set_value_range_to_varying (get_value_range (def));
7900 return SSA_PROP_VARYING;
7903 /* Union the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
7904 { VR1TYPE, VR0MIN, VR0MAX } and store the result
7905 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
7906 possible such range. The resulting range is not canonicalized. */
7908 static void
7909 union_ranges (enum value_range_type *vr0type,
7910 tree *vr0min, tree *vr0max,
7911 enum value_range_type vr1type,
7912 tree vr1min, tree vr1max)
7914 bool mineq = operand_equal_p (*vr0min, vr1min, 0);
7915 bool maxeq = operand_equal_p (*vr0max, vr1max, 0);
7917 /* [] is vr0, () is vr1 in the following classification comments. */
7918 if (mineq && maxeq)
7920 /* [( )] */
7921 if (*vr0type == vr1type)
7922 /* Nothing to do for equal ranges. */
7924 else if ((*vr0type == VR_RANGE
7925 && vr1type == VR_ANTI_RANGE)
7926 || (*vr0type == VR_ANTI_RANGE
7927 && vr1type == VR_RANGE))
7929 /* For anti-range with range union the result is varying. */
7930 goto give_up;
7932 else
7933 gcc_unreachable ();
7935 else if (operand_less_p (*vr0max, vr1min) == 1
7936 || operand_less_p (vr1max, *vr0min) == 1)
7938 /* [ ] ( ) or ( ) [ ]
7939 If the ranges have an empty intersection, result of the union
7940 operation is the anti-range or if both are anti-ranges
7941 it covers all. */
7942 if (*vr0type == VR_ANTI_RANGE
7943 && vr1type == VR_ANTI_RANGE)
7944 goto give_up;
7945 else if (*vr0type == VR_ANTI_RANGE
7946 && vr1type == VR_RANGE)
7948 else if (*vr0type == VR_RANGE
7949 && vr1type == VR_ANTI_RANGE)
7951 *vr0type = vr1type;
7952 *vr0min = vr1min;
7953 *vr0max = vr1max;
7955 else if (*vr0type == VR_RANGE
7956 && vr1type == VR_RANGE)
7958 /* The result is the convex hull of both ranges. */
7959 if (operand_less_p (*vr0max, vr1min) == 1)
7961 /* If the result can be an anti-range, create one. */
7962 if (TREE_CODE (*vr0max) == INTEGER_CST
7963 && TREE_CODE (vr1min) == INTEGER_CST
7964 && vrp_val_is_min (*vr0min)
7965 && vrp_val_is_max (vr1max))
7967 tree min = int_const_binop (PLUS_EXPR,
7968 *vr0max,
7969 build_int_cst (TREE_TYPE (*vr0max), 1));
7970 tree max = int_const_binop (MINUS_EXPR,
7971 vr1min,
7972 build_int_cst (TREE_TYPE (vr1min), 1));
7973 if (!operand_less_p (max, min))
7975 *vr0type = VR_ANTI_RANGE;
7976 *vr0min = min;
7977 *vr0max = max;
7979 else
7980 *vr0max = vr1max;
7982 else
7983 *vr0max = vr1max;
7985 else
7987 /* If the result can be an anti-range, create one. */
7988 if (TREE_CODE (vr1max) == INTEGER_CST
7989 && TREE_CODE (*vr0min) == INTEGER_CST
7990 && vrp_val_is_min (vr1min)
7991 && vrp_val_is_max (*vr0max))
7993 tree min = int_const_binop (PLUS_EXPR,
7994 vr1max,
7995 build_int_cst (TREE_TYPE (vr1max), 1));
7996 tree max = int_const_binop (MINUS_EXPR,
7997 *vr0min,
7998 build_int_cst (TREE_TYPE (*vr0min), 1));
7999 if (!operand_less_p (max, min))
8001 *vr0type = VR_ANTI_RANGE;
8002 *vr0min = min;
8003 *vr0max = max;
8005 else
8006 *vr0min = vr1min;
8008 else
8009 *vr0min = vr1min;
8012 else
8013 gcc_unreachable ();
8015 else if ((maxeq || operand_less_p (vr1max, *vr0max) == 1)
8016 && (mineq || operand_less_p (*vr0min, vr1min) == 1))
8018 /* [ ( ) ] or [( ) ] or [ ( )] */
8019 if (*vr0type == VR_RANGE
8020 && vr1type == VR_RANGE)
8022 else if (*vr0type == VR_ANTI_RANGE
8023 && vr1type == VR_ANTI_RANGE)
8025 *vr0type = vr1type;
8026 *vr0min = vr1min;
8027 *vr0max = vr1max;
8029 else if (*vr0type == VR_ANTI_RANGE
8030 && vr1type == VR_RANGE)
8032 /* Arbitrarily choose the right or left gap. */
8033 if (!mineq && TREE_CODE (vr1min) == INTEGER_CST)
8034 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
8035 build_int_cst (TREE_TYPE (vr1min), 1));
8036 else if (!maxeq && TREE_CODE (vr1max) == INTEGER_CST)
8037 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
8038 build_int_cst (TREE_TYPE (vr1max), 1));
8039 else
8040 goto give_up;
8042 else if (*vr0type == VR_RANGE
8043 && vr1type == VR_ANTI_RANGE)
8044 /* The result covers everything. */
8045 goto give_up;
8046 else
8047 gcc_unreachable ();
8049 else if ((maxeq || operand_less_p (*vr0max, vr1max) == 1)
8050 && (mineq || operand_less_p (vr1min, *vr0min) == 1))
8052 /* ( [ ] ) or ([ ] ) or ( [ ]) */
8053 if (*vr0type == VR_RANGE
8054 && vr1type == VR_RANGE)
8056 *vr0type = vr1type;
8057 *vr0min = vr1min;
8058 *vr0max = vr1max;
8060 else if (*vr0type == VR_ANTI_RANGE
8061 && vr1type == VR_ANTI_RANGE)
8063 else if (*vr0type == VR_RANGE
8064 && vr1type == VR_ANTI_RANGE)
8066 *vr0type = VR_ANTI_RANGE;
8067 if (!mineq && TREE_CODE (*vr0min) == INTEGER_CST)
8069 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
8070 build_int_cst (TREE_TYPE (*vr0min), 1));
8071 *vr0min = vr1min;
8073 else if (!maxeq && TREE_CODE (*vr0max) == INTEGER_CST)
8075 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
8076 build_int_cst (TREE_TYPE (*vr0max), 1));
8077 *vr0max = vr1max;
8079 else
8080 goto give_up;
8082 else if (*vr0type == VR_ANTI_RANGE
8083 && vr1type == VR_RANGE)
8084 /* The result covers everything. */
8085 goto give_up;
8086 else
8087 gcc_unreachable ();
8089 else if ((operand_less_p (vr1min, *vr0max) == 1
8090 || operand_equal_p (vr1min, *vr0max, 0))
8091 && operand_less_p (*vr0min, vr1min) == 1
8092 && operand_less_p (*vr0max, vr1max) == 1)
8094 /* [ ( ] ) or [ ]( ) */
8095 if (*vr0type == VR_RANGE
8096 && vr1type == VR_RANGE)
8097 *vr0max = vr1max;
8098 else if (*vr0type == VR_ANTI_RANGE
8099 && vr1type == VR_ANTI_RANGE)
8100 *vr0min = vr1min;
8101 else if (*vr0type == VR_ANTI_RANGE
8102 && vr1type == VR_RANGE)
8104 if (TREE_CODE (vr1min) == INTEGER_CST)
8105 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
8106 build_int_cst (TREE_TYPE (vr1min), 1));
8107 else
8108 goto give_up;
8110 else if (*vr0type == VR_RANGE
8111 && vr1type == VR_ANTI_RANGE)
8113 if (TREE_CODE (*vr0max) == INTEGER_CST)
8115 *vr0type = vr1type;
8116 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
8117 build_int_cst (TREE_TYPE (*vr0max), 1));
8118 *vr0max = vr1max;
8120 else
8121 goto give_up;
8123 else
8124 gcc_unreachable ();
8126 else if ((operand_less_p (*vr0min, vr1max) == 1
8127 || operand_equal_p (*vr0min, vr1max, 0))
8128 && operand_less_p (vr1min, *vr0min) == 1
8129 && operand_less_p (vr1max, *vr0max) == 1)
8131 /* ( [ ) ] or ( )[ ] */
8132 if (*vr0type == VR_RANGE
8133 && vr1type == VR_RANGE)
8134 *vr0min = vr1min;
8135 else if (*vr0type == VR_ANTI_RANGE
8136 && vr1type == VR_ANTI_RANGE)
8137 *vr0max = vr1max;
8138 else if (*vr0type == VR_ANTI_RANGE
8139 && vr1type == VR_RANGE)
8141 if (TREE_CODE (vr1max) == INTEGER_CST)
8142 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
8143 build_int_cst (TREE_TYPE (vr1max), 1));
8144 else
8145 goto give_up;
8147 else if (*vr0type == VR_RANGE
8148 && vr1type == VR_ANTI_RANGE)
8150 if (TREE_CODE (*vr0min) == INTEGER_CST)
8152 *vr0type = vr1type;
8153 *vr0min = vr1min;
8154 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
8155 build_int_cst (TREE_TYPE (*vr0min), 1));
8157 else
8158 goto give_up;
8160 else
8161 gcc_unreachable ();
8163 else
8164 goto give_up;
8166 return;
8168 give_up:
8169 *vr0type = VR_VARYING;
8170 *vr0min = NULL_TREE;
8171 *vr0max = NULL_TREE;
8174 /* Intersect the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
8175 { VR1TYPE, VR0MIN, VR0MAX } and store the result
8176 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
8177 possible such range. The resulting range is not canonicalized. */
8179 static void
8180 intersect_ranges (enum value_range_type *vr0type,
8181 tree *vr0min, tree *vr0max,
8182 enum value_range_type vr1type,
8183 tree vr1min, tree vr1max)
8185 bool mineq = operand_equal_p (*vr0min, vr1min, 0);
8186 bool maxeq = operand_equal_p (*vr0max, vr1max, 0);
8188 /* [] is vr0, () is vr1 in the following classification comments. */
8189 if (mineq && maxeq)
8191 /* [( )] */
8192 if (*vr0type == vr1type)
8193 /* Nothing to do for equal ranges. */
8195 else if ((*vr0type == VR_RANGE
8196 && vr1type == VR_ANTI_RANGE)
8197 || (*vr0type == VR_ANTI_RANGE
8198 && vr1type == VR_RANGE))
8200 /* For anti-range with range intersection the result is empty. */
8201 *vr0type = VR_UNDEFINED;
8202 *vr0min = NULL_TREE;
8203 *vr0max = NULL_TREE;
8205 else
8206 gcc_unreachable ();
8208 else if (operand_less_p (*vr0max, vr1min) == 1
8209 || operand_less_p (vr1max, *vr0min) == 1)
8211 /* [ ] ( ) or ( ) [ ]
8212 If the ranges have an empty intersection, the result of the
8213 intersect operation is the range for intersecting an
8214 anti-range with a range or empty when intersecting two ranges. */
8215 if (*vr0type == VR_RANGE
8216 && vr1type == VR_ANTI_RANGE)
8218 else if (*vr0type == VR_ANTI_RANGE
8219 && vr1type == VR_RANGE)
8221 *vr0type = vr1type;
8222 *vr0min = vr1min;
8223 *vr0max = vr1max;
8225 else if (*vr0type == VR_RANGE
8226 && vr1type == VR_RANGE)
8228 *vr0type = VR_UNDEFINED;
8229 *vr0min = NULL_TREE;
8230 *vr0max = NULL_TREE;
8232 else if (*vr0type == VR_ANTI_RANGE
8233 && vr1type == VR_ANTI_RANGE)
8235 /* If the anti-ranges are adjacent to each other merge them. */
8236 if (TREE_CODE (*vr0max) == INTEGER_CST
8237 && TREE_CODE (vr1min) == INTEGER_CST
8238 && operand_less_p (*vr0max, vr1min) == 1
8239 && integer_onep (int_const_binop (MINUS_EXPR,
8240 vr1min, *vr0max)))
8241 *vr0max = vr1max;
8242 else if (TREE_CODE (vr1max) == INTEGER_CST
8243 && TREE_CODE (*vr0min) == INTEGER_CST
8244 && operand_less_p (vr1max, *vr0min) == 1
8245 && integer_onep (int_const_binop (MINUS_EXPR,
8246 *vr0min, vr1max)))
8247 *vr0min = vr1min;
8248 /* Else arbitrarily take VR0. */
8251 else if ((maxeq || operand_less_p (vr1max, *vr0max) == 1)
8252 && (mineq || operand_less_p (*vr0min, vr1min) == 1))
8254 /* [ ( ) ] or [( ) ] or [ ( )] */
8255 if (*vr0type == VR_RANGE
8256 && vr1type == VR_RANGE)
8258 /* If both are ranges the result is the inner one. */
8259 *vr0type = vr1type;
8260 *vr0min = vr1min;
8261 *vr0max = vr1max;
8263 else if (*vr0type == VR_RANGE
8264 && vr1type == VR_ANTI_RANGE)
8266 /* Choose the right gap if the left one is empty. */
8267 if (mineq)
8269 if (TREE_CODE (vr1max) == INTEGER_CST)
8270 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
8271 build_int_cst (TREE_TYPE (vr1max), 1));
8272 else
8273 *vr0min = vr1max;
8275 /* Choose the left gap if the right one is empty. */
8276 else if (maxeq)
8278 if (TREE_CODE (vr1min) == INTEGER_CST)
8279 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
8280 build_int_cst (TREE_TYPE (vr1min), 1));
8281 else
8282 *vr0max = vr1min;
8284 /* Choose the anti-range if the range is effectively varying. */
8285 else if (vrp_val_is_min (*vr0min)
8286 && vrp_val_is_max (*vr0max))
8288 *vr0type = vr1type;
8289 *vr0min = vr1min;
8290 *vr0max = vr1max;
8292 /* Else choose the range. */
8294 else if (*vr0type == VR_ANTI_RANGE
8295 && vr1type == VR_ANTI_RANGE)
8296 /* If both are anti-ranges the result is the outer one. */
8298 else if (*vr0type == VR_ANTI_RANGE
8299 && vr1type == VR_RANGE)
8301 /* The intersection is empty. */
8302 *vr0type = VR_UNDEFINED;
8303 *vr0min = NULL_TREE;
8304 *vr0max = NULL_TREE;
8306 else
8307 gcc_unreachable ();
8309 else if ((maxeq || operand_less_p (*vr0max, vr1max) == 1)
8310 && (mineq || operand_less_p (vr1min, *vr0min) == 1))
8312 /* ( [ ] ) or ([ ] ) or ( [ ]) */
8313 if (*vr0type == VR_RANGE
8314 && vr1type == VR_RANGE)
8315 /* Choose the inner range. */
8317 else if (*vr0type == VR_ANTI_RANGE
8318 && vr1type == VR_RANGE)
8320 /* Choose the right gap if the left is empty. */
8321 if (mineq)
8323 *vr0type = VR_RANGE;
8324 if (TREE_CODE (*vr0max) == INTEGER_CST)
8325 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
8326 build_int_cst (TREE_TYPE (*vr0max), 1));
8327 else
8328 *vr0min = *vr0max;
8329 *vr0max = vr1max;
8331 /* Choose the left gap if the right is empty. */
8332 else if (maxeq)
8334 *vr0type = VR_RANGE;
8335 if (TREE_CODE (*vr0min) == INTEGER_CST)
8336 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
8337 build_int_cst (TREE_TYPE (*vr0min), 1));
8338 else
8339 *vr0max = *vr0min;
8340 *vr0min = vr1min;
8342 /* Choose the anti-range if the range is effectively varying. */
8343 else if (vrp_val_is_min (vr1min)
8344 && vrp_val_is_max (vr1max))
8346 /* Else choose the range. */
8347 else
8349 *vr0type = vr1type;
8350 *vr0min = vr1min;
8351 *vr0max = vr1max;
8354 else if (*vr0type == VR_ANTI_RANGE
8355 && vr1type == VR_ANTI_RANGE)
8357 /* If both are anti-ranges the result is the outer one. */
8358 *vr0type = vr1type;
8359 *vr0min = vr1min;
8360 *vr0max = vr1max;
8362 else if (vr1type == VR_ANTI_RANGE
8363 && *vr0type == VR_RANGE)
8365 /* The intersection is empty. */
8366 *vr0type = VR_UNDEFINED;
8367 *vr0min = NULL_TREE;
8368 *vr0max = NULL_TREE;
8370 else
8371 gcc_unreachable ();
8373 else if ((operand_less_p (vr1min, *vr0max) == 1
8374 || operand_equal_p (vr1min, *vr0max, 0))
8375 && operand_less_p (*vr0min, vr1min) == 1)
8377 /* [ ( ] ) or [ ]( ) */
8378 if (*vr0type == VR_ANTI_RANGE
8379 && vr1type == VR_ANTI_RANGE)
8380 *vr0max = vr1max;
8381 else if (*vr0type == VR_RANGE
8382 && vr1type == VR_RANGE)
8383 *vr0min = vr1min;
8384 else if (*vr0type == VR_RANGE
8385 && vr1type == VR_ANTI_RANGE)
8387 if (TREE_CODE (vr1min) == INTEGER_CST)
8388 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
8389 build_int_cst (TREE_TYPE (vr1min), 1));
8390 else
8391 *vr0max = vr1min;
8393 else if (*vr0type == VR_ANTI_RANGE
8394 && vr1type == VR_RANGE)
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 else
8405 gcc_unreachable ();
8407 else if ((operand_less_p (*vr0min, vr1max) == 1
8408 || operand_equal_p (*vr0min, vr1max, 0))
8409 && operand_less_p (vr1min, *vr0min) == 1)
8411 /* ( [ ) ] or ( )[ ] */
8412 if (*vr0type == VR_ANTI_RANGE
8413 && vr1type == VR_ANTI_RANGE)
8414 *vr0min = vr1min;
8415 else if (*vr0type == VR_RANGE
8416 && vr1type == VR_RANGE)
8417 *vr0max = vr1max;
8418 else if (*vr0type == VR_RANGE
8419 && vr1type == VR_ANTI_RANGE)
8421 if (TREE_CODE (vr1max) == INTEGER_CST)
8422 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
8423 build_int_cst (TREE_TYPE (vr1max), 1));
8424 else
8425 *vr0min = vr1max;
8427 else if (*vr0type == VR_ANTI_RANGE
8428 && vr1type == VR_RANGE)
8430 *vr0type = VR_RANGE;
8431 if (TREE_CODE (*vr0min) == INTEGER_CST)
8432 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
8433 build_int_cst (TREE_TYPE (*vr0min), 1));
8434 else
8435 *vr0max = *vr0min;
8436 *vr0min = vr1min;
8438 else
8439 gcc_unreachable ();
8442 /* As a fallback simply use { *VRTYPE, *VR0MIN, *VR0MAX } as
8443 result for the intersection. That's always a conservative
8444 correct estimate. */
8446 return;
8450 /* Intersect the two value-ranges *VR0 and *VR1 and store the result
8451 in *VR0. This may not be the smallest possible such range. */
8453 static void
8454 vrp_intersect_ranges_1 (value_range *vr0, value_range *vr1)
8456 value_range saved;
8458 /* If either range is VR_VARYING the other one wins. */
8459 if (vr1->type == VR_VARYING)
8460 return;
8461 if (vr0->type == VR_VARYING)
8463 copy_value_range (vr0, vr1);
8464 return;
8467 /* When either range is VR_UNDEFINED the resulting range is
8468 VR_UNDEFINED, too. */
8469 if (vr0->type == VR_UNDEFINED)
8470 return;
8471 if (vr1->type == VR_UNDEFINED)
8473 set_value_range_to_undefined (vr0);
8474 return;
8477 /* Save the original vr0 so we can return it as conservative intersection
8478 result when our worker turns things to varying. */
8479 saved = *vr0;
8480 intersect_ranges (&vr0->type, &vr0->min, &vr0->max,
8481 vr1->type, vr1->min, vr1->max);
8482 /* Make sure to canonicalize the result though as the inversion of a
8483 VR_RANGE can still be a VR_RANGE. */
8484 set_and_canonicalize_value_range (vr0, vr0->type,
8485 vr0->min, vr0->max, vr0->equiv);
8486 /* If that failed, use the saved original VR0. */
8487 if (vr0->type == VR_VARYING)
8489 *vr0 = saved;
8490 return;
8492 /* If the result is VR_UNDEFINED there is no need to mess with
8493 the equivalencies. */
8494 if (vr0->type == VR_UNDEFINED)
8495 return;
8497 /* The resulting set of equivalences for range intersection is the union of
8498 the two sets. */
8499 if (vr0->equiv && vr1->equiv && vr0->equiv != vr1->equiv)
8500 bitmap_ior_into (vr0->equiv, vr1->equiv);
8501 else if (vr1->equiv && !vr0->equiv)
8502 bitmap_copy (vr0->equiv, vr1->equiv);
8505 static void
8506 vrp_intersect_ranges (value_range *vr0, value_range *vr1)
8508 if (dump_file && (dump_flags & TDF_DETAILS))
8510 fprintf (dump_file, "Intersecting\n ");
8511 dump_value_range (dump_file, vr0);
8512 fprintf (dump_file, "\nand\n ");
8513 dump_value_range (dump_file, vr1);
8514 fprintf (dump_file, "\n");
8516 vrp_intersect_ranges_1 (vr0, vr1);
8517 if (dump_file && (dump_flags & TDF_DETAILS))
8519 fprintf (dump_file, "to\n ");
8520 dump_value_range (dump_file, vr0);
8521 fprintf (dump_file, "\n");
8525 /* Meet operation for value ranges. Given two value ranges VR0 and
8526 VR1, store in VR0 a range that contains both VR0 and VR1. This
8527 may not be the smallest possible such range. */
8529 static void
8530 vrp_meet_1 (value_range *vr0, value_range *vr1)
8532 value_range saved;
8534 if (vr0->type == VR_UNDEFINED)
8536 set_value_range (vr0, vr1->type, vr1->min, vr1->max, vr1->equiv);
8537 return;
8540 if (vr1->type == VR_UNDEFINED)
8542 /* VR0 already has the resulting range. */
8543 return;
8546 if (vr0->type == VR_VARYING)
8548 /* Nothing to do. VR0 already has the resulting range. */
8549 return;
8552 if (vr1->type == VR_VARYING)
8554 set_value_range_to_varying (vr0);
8555 return;
8558 saved = *vr0;
8559 union_ranges (&vr0->type, &vr0->min, &vr0->max,
8560 vr1->type, vr1->min, vr1->max);
8561 if (vr0->type == VR_VARYING)
8563 /* Failed to find an efficient meet. Before giving up and setting
8564 the result to VARYING, see if we can at least derive a useful
8565 anti-range. FIXME, all this nonsense about distinguishing
8566 anti-ranges from ranges is necessary because of the odd
8567 semantics of range_includes_zero_p and friends. */
8568 if (((saved.type == VR_RANGE
8569 && range_includes_zero_p (saved.min, saved.max) == 0)
8570 || (saved.type == VR_ANTI_RANGE
8571 && range_includes_zero_p (saved.min, saved.max) == 1))
8572 && ((vr1->type == VR_RANGE
8573 && range_includes_zero_p (vr1->min, vr1->max) == 0)
8574 || (vr1->type == VR_ANTI_RANGE
8575 && range_includes_zero_p (vr1->min, vr1->max) == 1)))
8577 set_value_range_to_nonnull (vr0, TREE_TYPE (saved.min));
8579 /* Since this meet operation did not result from the meeting of
8580 two equivalent names, VR0 cannot have any equivalences. */
8581 if (vr0->equiv)
8582 bitmap_clear (vr0->equiv);
8583 return;
8586 set_value_range_to_varying (vr0);
8587 return;
8589 set_and_canonicalize_value_range (vr0, vr0->type, vr0->min, vr0->max,
8590 vr0->equiv);
8591 if (vr0->type == VR_VARYING)
8592 return;
8594 /* The resulting set of equivalences is always the intersection of
8595 the two sets. */
8596 if (vr0->equiv && vr1->equiv && vr0->equiv != vr1->equiv)
8597 bitmap_and_into (vr0->equiv, vr1->equiv);
8598 else if (vr0->equiv && !vr1->equiv)
8599 bitmap_clear (vr0->equiv);
8602 static void
8603 vrp_meet (value_range *vr0, value_range *vr1)
8605 if (dump_file && (dump_flags & TDF_DETAILS))
8607 fprintf (dump_file, "Meeting\n ");
8608 dump_value_range (dump_file, vr0);
8609 fprintf (dump_file, "\nand\n ");
8610 dump_value_range (dump_file, vr1);
8611 fprintf (dump_file, "\n");
8613 vrp_meet_1 (vr0, vr1);
8614 if (dump_file && (dump_flags & TDF_DETAILS))
8616 fprintf (dump_file, "to\n ");
8617 dump_value_range (dump_file, vr0);
8618 fprintf (dump_file, "\n");
8623 /* Visit all arguments for PHI node PHI that flow through executable
8624 edges. If a valid value range can be derived from all the incoming
8625 value ranges, set a new range for the LHS of PHI. */
8627 static enum ssa_prop_result
8628 vrp_visit_phi_node (gphi *phi)
8630 size_t i;
8631 tree lhs = PHI_RESULT (phi);
8632 value_range *lhs_vr = get_value_range (lhs);
8633 value_range vr_result = VR_INITIALIZER;
8634 bool first = true;
8635 int edges, old_edges;
8636 struct loop *l;
8638 if (dump_file && (dump_flags & TDF_DETAILS))
8640 fprintf (dump_file, "\nVisiting PHI node: ");
8641 print_gimple_stmt (dump_file, phi, 0, dump_flags);
8644 edges = 0;
8645 for (i = 0; i < gimple_phi_num_args (phi); i++)
8647 edge e = gimple_phi_arg_edge (phi, i);
8649 if (dump_file && (dump_flags & TDF_DETAILS))
8651 fprintf (dump_file,
8652 " Argument #%d (%d -> %d %sexecutable)\n",
8653 (int) i, e->src->index, e->dest->index,
8654 (e->flags & EDGE_EXECUTABLE) ? "" : "not ");
8657 if (e->flags & EDGE_EXECUTABLE)
8659 tree arg = PHI_ARG_DEF (phi, i);
8660 value_range vr_arg;
8662 ++edges;
8664 if (TREE_CODE (arg) == SSA_NAME)
8666 vr_arg = *(get_value_range (arg));
8667 /* Do not allow equivalences or symbolic ranges to leak in from
8668 backedges. That creates invalid equivalencies.
8669 See PR53465 and PR54767. */
8670 if (e->flags & EDGE_DFS_BACK)
8672 if (vr_arg.type == VR_RANGE
8673 || vr_arg.type == VR_ANTI_RANGE)
8675 vr_arg.equiv = NULL;
8676 if (symbolic_range_p (&vr_arg))
8678 vr_arg.type = VR_VARYING;
8679 vr_arg.min = NULL_TREE;
8680 vr_arg.max = NULL_TREE;
8684 else
8686 /* If the non-backedge arguments range is VR_VARYING then
8687 we can still try recording a simple equivalence. */
8688 if (vr_arg.type == VR_VARYING)
8690 vr_arg.type = VR_RANGE;
8691 vr_arg.min = arg;
8692 vr_arg.max = arg;
8693 vr_arg.equiv = NULL;
8697 else
8699 if (TREE_OVERFLOW_P (arg))
8700 arg = drop_tree_overflow (arg);
8702 vr_arg.type = VR_RANGE;
8703 vr_arg.min = arg;
8704 vr_arg.max = arg;
8705 vr_arg.equiv = NULL;
8708 if (dump_file && (dump_flags & TDF_DETAILS))
8710 fprintf (dump_file, "\t");
8711 print_generic_expr (dump_file, arg, dump_flags);
8712 fprintf (dump_file, ": ");
8713 dump_value_range (dump_file, &vr_arg);
8714 fprintf (dump_file, "\n");
8717 if (first)
8718 copy_value_range (&vr_result, &vr_arg);
8719 else
8720 vrp_meet (&vr_result, &vr_arg);
8721 first = false;
8723 if (vr_result.type == VR_VARYING)
8724 break;
8728 if (vr_result.type == VR_VARYING)
8729 goto varying;
8730 else if (vr_result.type == VR_UNDEFINED)
8731 goto update_range;
8733 old_edges = vr_phi_edge_counts[SSA_NAME_VERSION (lhs)];
8734 vr_phi_edge_counts[SSA_NAME_VERSION (lhs)] = edges;
8736 /* To prevent infinite iterations in the algorithm, derive ranges
8737 when the new value is slightly bigger or smaller than the
8738 previous one. We don't do this if we have seen a new executable
8739 edge; this helps us avoid an overflow infinity for conditionals
8740 which are not in a loop. If the old value-range was VR_UNDEFINED
8741 use the updated range and iterate one more time. */
8742 if (edges > 0
8743 && gimple_phi_num_args (phi) > 1
8744 && edges == old_edges
8745 && lhs_vr->type != VR_UNDEFINED)
8747 /* Compare old and new ranges, fall back to varying if the
8748 values are not comparable. */
8749 int cmp_min = compare_values (lhs_vr->min, vr_result.min);
8750 if (cmp_min == -2)
8751 goto varying;
8752 int cmp_max = compare_values (lhs_vr->max, vr_result.max);
8753 if (cmp_max == -2)
8754 goto varying;
8756 /* For non VR_RANGE or for pointers fall back to varying if
8757 the range changed. */
8758 if ((lhs_vr->type != VR_RANGE || vr_result.type != VR_RANGE
8759 || POINTER_TYPE_P (TREE_TYPE (lhs)))
8760 && (cmp_min != 0 || cmp_max != 0))
8761 goto varying;
8763 /* If the new minimum is larger than the previous one
8764 retain the old value. If the new minimum value is smaller
8765 than the previous one and not -INF go all the way to -INF + 1.
8766 In the first case, to avoid infinite bouncing between different
8767 minimums, and in the other case to avoid iterating millions of
8768 times to reach -INF. Going to -INF + 1 also lets the following
8769 iteration compute whether there will be any overflow, at the
8770 expense of one additional iteration. */
8771 if (cmp_min < 0)
8772 vr_result.min = lhs_vr->min;
8773 else if (cmp_min > 0
8774 && !vrp_val_is_min (vr_result.min))
8775 vr_result.min
8776 = int_const_binop (PLUS_EXPR,
8777 vrp_val_min (TREE_TYPE (vr_result.min)),
8778 build_int_cst (TREE_TYPE (vr_result.min), 1));
8780 /* Similarly for the maximum value. */
8781 if (cmp_max > 0)
8782 vr_result.max = lhs_vr->max;
8783 else if (cmp_max < 0
8784 && !vrp_val_is_max (vr_result.max))
8785 vr_result.max
8786 = int_const_binop (MINUS_EXPR,
8787 vrp_val_max (TREE_TYPE (vr_result.min)),
8788 build_int_cst (TREE_TYPE (vr_result.min), 1));
8790 /* If we dropped either bound to +-INF then if this is a loop
8791 PHI node SCEV may known more about its value-range. */
8792 if ((cmp_min > 0 || cmp_min < 0
8793 || cmp_max < 0 || cmp_max > 0)
8794 && (l = loop_containing_stmt (phi))
8795 && l->header == gimple_bb (phi))
8796 adjust_range_with_scev (&vr_result, l, phi, lhs);
8798 /* If we will end up with a (-INF, +INF) range, set it to
8799 VARYING. Same if the previous max value was invalid for
8800 the type and we end up with vr_result.min > vr_result.max. */
8801 if ((vrp_val_is_max (vr_result.max)
8802 && vrp_val_is_min (vr_result.min))
8803 || compare_values (vr_result.min,
8804 vr_result.max) > 0)
8805 goto varying;
8808 /* If the new range is different than the previous value, keep
8809 iterating. */
8810 update_range:
8811 if (update_value_range (lhs, &vr_result))
8813 if (dump_file && (dump_flags & TDF_DETAILS))
8815 fprintf (dump_file, "Found new range for ");
8816 print_generic_expr (dump_file, lhs, 0);
8817 fprintf (dump_file, ": ");
8818 dump_value_range (dump_file, &vr_result);
8819 fprintf (dump_file, "\n");
8822 if (vr_result.type == VR_VARYING)
8823 return SSA_PROP_VARYING;
8825 return SSA_PROP_INTERESTING;
8828 /* Nothing changed, don't add outgoing edges. */
8829 return SSA_PROP_NOT_INTERESTING;
8831 /* No match found. Set the LHS to VARYING. */
8832 varying:
8833 set_value_range_to_varying (lhs_vr);
8834 return SSA_PROP_VARYING;
8837 /* Simplify boolean operations if the source is known
8838 to be already a boolean. */
8839 static bool
8840 simplify_truth_ops_using_ranges (gimple_stmt_iterator *gsi, gimple *stmt)
8842 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
8843 tree lhs, op0, op1;
8844 bool need_conversion;
8846 /* We handle only !=/== case here. */
8847 gcc_assert (rhs_code == EQ_EXPR || rhs_code == NE_EXPR);
8849 op0 = gimple_assign_rhs1 (stmt);
8850 if (!op_with_boolean_value_range_p (op0))
8851 return false;
8853 op1 = gimple_assign_rhs2 (stmt);
8854 if (!op_with_boolean_value_range_p (op1))
8855 return false;
8857 /* Reduce number of cases to handle to NE_EXPR. As there is no
8858 BIT_XNOR_EXPR we cannot replace A == B with a single statement. */
8859 if (rhs_code == EQ_EXPR)
8861 if (TREE_CODE (op1) == INTEGER_CST)
8862 op1 = int_const_binop (BIT_XOR_EXPR, op1,
8863 build_int_cst (TREE_TYPE (op1), 1));
8864 else
8865 return false;
8868 lhs = gimple_assign_lhs (stmt);
8869 need_conversion
8870 = !useless_type_conversion_p (TREE_TYPE (lhs), TREE_TYPE (op0));
8872 /* Make sure to not sign-extend a 1-bit 1 when converting the result. */
8873 if (need_conversion
8874 && !TYPE_UNSIGNED (TREE_TYPE (op0))
8875 && TYPE_PRECISION (TREE_TYPE (op0)) == 1
8876 && TYPE_PRECISION (TREE_TYPE (lhs)) > 1)
8877 return false;
8879 /* For A != 0 we can substitute A itself. */
8880 if (integer_zerop (op1))
8881 gimple_assign_set_rhs_with_ops (gsi,
8882 need_conversion
8883 ? NOP_EXPR : TREE_CODE (op0), op0);
8884 /* For A != B we substitute A ^ B. Either with conversion. */
8885 else if (need_conversion)
8887 tree tem = make_ssa_name (TREE_TYPE (op0));
8888 gassign *newop
8889 = gimple_build_assign (tem, BIT_XOR_EXPR, op0, op1);
8890 gsi_insert_before (gsi, newop, GSI_SAME_STMT);
8891 gimple_assign_set_rhs_with_ops (gsi, NOP_EXPR, tem);
8893 /* Or without. */
8894 else
8895 gimple_assign_set_rhs_with_ops (gsi, BIT_XOR_EXPR, op0, op1);
8896 update_stmt (gsi_stmt (*gsi));
8898 return true;
8901 /* Simplify a division or modulo operator to a right shift or
8902 bitwise and if the first operand is unsigned or is greater
8903 than zero and the second operand is an exact power of two.
8904 For TRUNC_MOD_EXPR op0 % op1 with constant op1, optimize it
8905 into just op0 if op0's range is known to be a subset of
8906 [-op1 + 1, op1 - 1] for signed and [0, op1 - 1] for unsigned
8907 modulo. */
8909 static bool
8910 simplify_div_or_mod_using_ranges (gimple *stmt)
8912 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
8913 tree val = NULL;
8914 tree op0 = gimple_assign_rhs1 (stmt);
8915 tree op1 = gimple_assign_rhs2 (stmt);
8916 value_range *vr = get_value_range (op0);
8918 if (rhs_code == TRUNC_MOD_EXPR
8919 && TREE_CODE (op1) == INTEGER_CST
8920 && tree_int_cst_sgn (op1) == 1
8921 && range_int_cst_p (vr)
8922 && tree_int_cst_lt (vr->max, op1))
8924 if (TYPE_UNSIGNED (TREE_TYPE (op0))
8925 || tree_int_cst_sgn (vr->min) >= 0
8926 || tree_int_cst_lt (fold_unary (NEGATE_EXPR, TREE_TYPE (op1), op1),
8927 vr->min))
8929 /* If op0 already has the range op0 % op1 has,
8930 then TRUNC_MOD_EXPR won't change anything. */
8931 gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
8932 gimple_assign_set_rhs_from_tree (&gsi, op0);
8933 update_stmt (stmt);
8934 return true;
8938 if (!integer_pow2p (op1))
8939 return false;
8941 if (TYPE_UNSIGNED (TREE_TYPE (op0)))
8943 val = integer_one_node;
8945 else
8947 bool sop = false;
8949 val = compare_range_with_value (GE_EXPR, vr, integer_zero_node, &sop);
8951 if (val
8952 && sop
8953 && integer_onep (val)
8954 && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC))
8956 location_t location;
8958 if (!gimple_has_location (stmt))
8959 location = input_location;
8960 else
8961 location = gimple_location (stmt);
8962 warning_at (location, OPT_Wstrict_overflow,
8963 "assuming signed overflow does not occur when "
8964 "simplifying %</%> or %<%%%> to %<>>%> or %<&%>");
8968 if (val && integer_onep (val))
8970 tree t;
8972 if (rhs_code == TRUNC_DIV_EXPR)
8974 t = build_int_cst (integer_type_node, tree_log2 (op1));
8975 gimple_assign_set_rhs_code (stmt, RSHIFT_EXPR);
8976 gimple_assign_set_rhs1 (stmt, op0);
8977 gimple_assign_set_rhs2 (stmt, t);
8979 else
8981 t = build_int_cst (TREE_TYPE (op1), 1);
8982 t = int_const_binop (MINUS_EXPR, op1, t);
8983 t = fold_convert (TREE_TYPE (op0), t);
8985 gimple_assign_set_rhs_code (stmt, BIT_AND_EXPR);
8986 gimple_assign_set_rhs1 (stmt, op0);
8987 gimple_assign_set_rhs2 (stmt, t);
8990 update_stmt (stmt);
8991 return true;
8994 return false;
8997 /* Simplify a min or max if the ranges of the two operands are
8998 disjoint. Return true if we do simplify. */
9000 static bool
9001 simplify_min_or_max_using_ranges (gimple *stmt)
9003 tree op0 = gimple_assign_rhs1 (stmt);
9004 tree op1 = gimple_assign_rhs2 (stmt);
9005 bool sop = false;
9006 tree val;
9008 val = (vrp_evaluate_conditional_warnv_with_ops_using_ranges
9009 (LE_EXPR, op0, op1, &sop));
9010 if (!val)
9012 sop = false;
9013 val = (vrp_evaluate_conditional_warnv_with_ops_using_ranges
9014 (LT_EXPR, op0, op1, &sop));
9017 if (val)
9019 if (sop && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC))
9021 location_t location;
9023 if (!gimple_has_location (stmt))
9024 location = input_location;
9025 else
9026 location = gimple_location (stmt);
9027 warning_at (location, OPT_Wstrict_overflow,
9028 "assuming signed overflow does not occur when "
9029 "simplifying %<min/max (X,Y)%> to %<X%> or %<Y%>");
9032 /* VAL == TRUE -> OP0 < or <= op1
9033 VAL == FALSE -> OP0 > or >= op1. */
9034 tree res = ((gimple_assign_rhs_code (stmt) == MAX_EXPR)
9035 == integer_zerop (val)) ? op0 : op1;
9036 gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
9037 gimple_assign_set_rhs_from_tree (&gsi, res);
9038 update_stmt (stmt);
9039 return true;
9042 return false;
9045 /* If the operand to an ABS_EXPR is >= 0, then eliminate the
9046 ABS_EXPR. If the operand is <= 0, then simplify the
9047 ABS_EXPR into a NEGATE_EXPR. */
9049 static bool
9050 simplify_abs_using_ranges (gimple *stmt)
9052 tree op = gimple_assign_rhs1 (stmt);
9053 value_range *vr = get_value_range (op);
9055 if (vr)
9057 tree val = NULL;
9058 bool sop = false;
9060 val = compare_range_with_value (LE_EXPR, vr, integer_zero_node, &sop);
9061 if (!val)
9063 /* The range is neither <= 0 nor > 0. Now see if it is
9064 either < 0 or >= 0. */
9065 sop = false;
9066 val = compare_range_with_value (LT_EXPR, vr, integer_zero_node,
9067 &sop);
9070 if (val)
9072 if (sop && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC))
9074 location_t location;
9076 if (!gimple_has_location (stmt))
9077 location = input_location;
9078 else
9079 location = gimple_location (stmt);
9080 warning_at (location, OPT_Wstrict_overflow,
9081 "assuming signed overflow does not occur when "
9082 "simplifying %<abs (X)%> to %<X%> or %<-X%>");
9085 gimple_assign_set_rhs1 (stmt, op);
9086 if (integer_zerop (val))
9087 gimple_assign_set_rhs_code (stmt, SSA_NAME);
9088 else
9089 gimple_assign_set_rhs_code (stmt, NEGATE_EXPR);
9090 update_stmt (stmt);
9091 return true;
9095 return false;
9098 /* Optimize away redundant BIT_AND_EXPR and BIT_IOR_EXPR.
9099 If all the bits that are being cleared by & are already
9100 known to be zero from VR, or all the bits that are being
9101 set by | are already known to be one from VR, the bit
9102 operation is redundant. */
9104 static bool
9105 simplify_bit_ops_using_ranges (gimple_stmt_iterator *gsi, gimple *stmt)
9107 tree op0 = gimple_assign_rhs1 (stmt);
9108 tree op1 = gimple_assign_rhs2 (stmt);
9109 tree op = NULL_TREE;
9110 value_range vr0 = VR_INITIALIZER;
9111 value_range vr1 = VR_INITIALIZER;
9112 wide_int may_be_nonzero0, may_be_nonzero1;
9113 wide_int must_be_nonzero0, must_be_nonzero1;
9114 wide_int mask;
9116 if (TREE_CODE (op0) == SSA_NAME)
9117 vr0 = *(get_value_range (op0));
9118 else if (is_gimple_min_invariant (op0))
9119 set_value_range_to_value (&vr0, op0, NULL);
9120 else
9121 return false;
9123 if (TREE_CODE (op1) == SSA_NAME)
9124 vr1 = *(get_value_range (op1));
9125 else if (is_gimple_min_invariant (op1))
9126 set_value_range_to_value (&vr1, op1, NULL);
9127 else
9128 return false;
9130 if (!zero_nonzero_bits_from_vr (TREE_TYPE (op0), &vr0, &may_be_nonzero0,
9131 &must_be_nonzero0))
9132 return false;
9133 if (!zero_nonzero_bits_from_vr (TREE_TYPE (op1), &vr1, &may_be_nonzero1,
9134 &must_be_nonzero1))
9135 return false;
9137 switch (gimple_assign_rhs_code (stmt))
9139 case BIT_AND_EXPR:
9140 mask = may_be_nonzero0.and_not (must_be_nonzero1);
9141 if (mask == 0)
9143 op = op0;
9144 break;
9146 mask = may_be_nonzero1.and_not (must_be_nonzero0);
9147 if (mask == 0)
9149 op = op1;
9150 break;
9152 break;
9153 case BIT_IOR_EXPR:
9154 mask = may_be_nonzero0.and_not (must_be_nonzero1);
9155 if (mask == 0)
9157 op = op1;
9158 break;
9160 mask = may_be_nonzero1.and_not (must_be_nonzero0);
9161 if (mask == 0)
9163 op = op0;
9164 break;
9166 break;
9167 default:
9168 gcc_unreachable ();
9171 if (op == NULL_TREE)
9172 return false;
9174 gimple_assign_set_rhs_with_ops (gsi, TREE_CODE (op), op);
9175 update_stmt (gsi_stmt (*gsi));
9176 return true;
9179 /* We are comparing trees OP0 and OP1 using COND_CODE. OP0 has
9180 a known value range VR.
9182 If there is one and only one value which will satisfy the
9183 conditional, then return that value. Else return NULL.
9185 If signed overflow must be undefined for the value to satisfy
9186 the conditional, then set *STRICT_OVERFLOW_P to true. */
9188 static tree
9189 test_for_singularity (enum tree_code cond_code, tree op0,
9190 tree op1, value_range *vr,
9191 bool *strict_overflow_p)
9193 tree min = NULL;
9194 tree max = NULL;
9196 /* Extract minimum/maximum values which satisfy the
9197 the conditional as it was written. */
9198 if (cond_code == LE_EXPR || cond_code == LT_EXPR)
9200 /* This should not be negative infinity; there is no overflow
9201 here. */
9202 min = TYPE_MIN_VALUE (TREE_TYPE (op0));
9204 max = op1;
9205 if (cond_code == LT_EXPR && !is_overflow_infinity (max))
9207 tree one = build_int_cst (TREE_TYPE (op0), 1);
9208 max = fold_build2 (MINUS_EXPR, TREE_TYPE (op0), max, one);
9209 if (EXPR_P (max))
9210 TREE_NO_WARNING (max) = 1;
9213 else if (cond_code == GE_EXPR || cond_code == GT_EXPR)
9215 /* This should not be positive infinity; there is no overflow
9216 here. */
9217 max = TYPE_MAX_VALUE (TREE_TYPE (op0));
9219 min = op1;
9220 if (cond_code == GT_EXPR && !is_overflow_infinity (min))
9222 tree one = build_int_cst (TREE_TYPE (op0), 1);
9223 min = fold_build2 (PLUS_EXPR, TREE_TYPE (op0), min, one);
9224 if (EXPR_P (min))
9225 TREE_NO_WARNING (min) = 1;
9229 /* Now refine the minimum and maximum values using any
9230 value range information we have for op0. */
9231 if (min && max)
9233 if (compare_values (vr->min, min) == 1)
9234 min = vr->min;
9235 if (compare_values (vr->max, max) == -1)
9236 max = vr->max;
9238 /* If the new min/max values have converged to a single value,
9239 then there is only one value which can satisfy the condition,
9240 return that value. */
9241 if (operand_equal_p (min, max, 0) && is_gimple_min_invariant (min))
9243 if ((cond_code == LE_EXPR || cond_code == LT_EXPR)
9244 && is_overflow_infinity (vr->max))
9245 *strict_overflow_p = true;
9246 if ((cond_code == GE_EXPR || cond_code == GT_EXPR)
9247 && is_overflow_infinity (vr->min))
9248 *strict_overflow_p = true;
9250 return min;
9253 return NULL;
9256 /* Return whether the value range *VR fits in an integer type specified
9257 by PRECISION and UNSIGNED_P. */
9259 static bool
9260 range_fits_type_p (value_range *vr, unsigned dest_precision, signop dest_sgn)
9262 tree src_type;
9263 unsigned src_precision;
9264 widest_int tem;
9265 signop src_sgn;
9267 /* We can only handle integral and pointer types. */
9268 src_type = TREE_TYPE (vr->min);
9269 if (!INTEGRAL_TYPE_P (src_type)
9270 && !POINTER_TYPE_P (src_type))
9271 return false;
9273 /* An extension is fine unless VR is SIGNED and dest_sgn is UNSIGNED,
9274 and so is an identity transform. */
9275 src_precision = TYPE_PRECISION (TREE_TYPE (vr->min));
9276 src_sgn = TYPE_SIGN (src_type);
9277 if ((src_precision < dest_precision
9278 && !(dest_sgn == UNSIGNED && src_sgn == SIGNED))
9279 || (src_precision == dest_precision && src_sgn == dest_sgn))
9280 return true;
9282 /* Now we can only handle ranges with constant bounds. */
9283 if (vr->type != VR_RANGE
9284 || TREE_CODE (vr->min) != INTEGER_CST
9285 || TREE_CODE (vr->max) != INTEGER_CST)
9286 return false;
9288 /* For sign changes, the MSB of the wide_int has to be clear.
9289 An unsigned value with its MSB set cannot be represented by
9290 a signed wide_int, while a negative value cannot be represented
9291 by an unsigned wide_int. */
9292 if (src_sgn != dest_sgn
9293 && (wi::lts_p (vr->min, 0) || wi::lts_p (vr->max, 0)))
9294 return false;
9296 /* Then we can perform the conversion on both ends and compare
9297 the result for equality. */
9298 tem = wi::ext (wi::to_widest (vr->min), dest_precision, dest_sgn);
9299 if (tem != wi::to_widest (vr->min))
9300 return false;
9301 tem = wi::ext (wi::to_widest (vr->max), dest_precision, dest_sgn);
9302 if (tem != wi::to_widest (vr->max))
9303 return false;
9305 return true;
9308 /* Simplify a conditional using a relational operator to an equality
9309 test if the range information indicates only one value can satisfy
9310 the original conditional. */
9312 static bool
9313 simplify_cond_using_ranges (gcond *stmt)
9315 tree op0 = gimple_cond_lhs (stmt);
9316 tree op1 = gimple_cond_rhs (stmt);
9317 enum tree_code cond_code = gimple_cond_code (stmt);
9319 if (cond_code != NE_EXPR
9320 && cond_code != EQ_EXPR
9321 && TREE_CODE (op0) == SSA_NAME
9322 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
9323 && is_gimple_min_invariant (op1))
9325 value_range *vr = get_value_range (op0);
9327 /* If we have range information for OP0, then we might be
9328 able to simplify this conditional. */
9329 if (vr->type == VR_RANGE)
9331 enum warn_strict_overflow_code wc = WARN_STRICT_OVERFLOW_COMPARISON;
9332 bool sop = false;
9333 tree new_tree = test_for_singularity (cond_code, op0, op1, vr, &sop);
9335 if (new_tree
9336 && (!sop || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0))))
9338 if (dump_file)
9340 fprintf (dump_file, "Simplified relational ");
9341 print_gimple_stmt (dump_file, stmt, 0, 0);
9342 fprintf (dump_file, " into ");
9345 gimple_cond_set_code (stmt, EQ_EXPR);
9346 gimple_cond_set_lhs (stmt, op0);
9347 gimple_cond_set_rhs (stmt, new_tree);
9349 update_stmt (stmt);
9351 if (dump_file)
9353 print_gimple_stmt (dump_file, stmt, 0, 0);
9354 fprintf (dump_file, "\n");
9357 if (sop && issue_strict_overflow_warning (wc))
9359 location_t location = input_location;
9360 if (gimple_has_location (stmt))
9361 location = gimple_location (stmt);
9363 warning_at (location, OPT_Wstrict_overflow,
9364 "assuming signed overflow does not occur when "
9365 "simplifying conditional");
9368 return true;
9371 /* Try again after inverting the condition. We only deal
9372 with integral types here, so no need to worry about
9373 issues with inverting FP comparisons. */
9374 sop = false;
9375 new_tree = test_for_singularity
9376 (invert_tree_comparison (cond_code, false),
9377 op0, op1, vr, &sop);
9379 if (new_tree
9380 && (!sop || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0))))
9382 if (dump_file)
9384 fprintf (dump_file, "Simplified relational ");
9385 print_gimple_stmt (dump_file, stmt, 0, 0);
9386 fprintf (dump_file, " into ");
9389 gimple_cond_set_code (stmt, NE_EXPR);
9390 gimple_cond_set_lhs (stmt, op0);
9391 gimple_cond_set_rhs (stmt, new_tree);
9393 update_stmt (stmt);
9395 if (dump_file)
9397 print_gimple_stmt (dump_file, stmt, 0, 0);
9398 fprintf (dump_file, "\n");
9401 if (sop && issue_strict_overflow_warning (wc))
9403 location_t location = input_location;
9404 if (gimple_has_location (stmt))
9405 location = gimple_location (stmt);
9407 warning_at (location, OPT_Wstrict_overflow,
9408 "assuming signed overflow does not occur when "
9409 "simplifying conditional");
9412 return true;
9417 /* If we have a comparison of an SSA_NAME (OP0) against a constant,
9418 see if OP0 was set by a type conversion where the source of
9419 the conversion is another SSA_NAME with a range that fits
9420 into the range of OP0's type.
9422 If so, the conversion is redundant as the earlier SSA_NAME can be
9423 used for the comparison directly if we just massage the constant in the
9424 comparison. */
9425 if (TREE_CODE (op0) == SSA_NAME
9426 && TREE_CODE (op1) == INTEGER_CST)
9428 gimple *def_stmt = SSA_NAME_DEF_STMT (op0);
9429 tree innerop;
9431 if (!is_gimple_assign (def_stmt)
9432 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt)))
9433 return false;
9435 innerop = gimple_assign_rhs1 (def_stmt);
9437 if (TREE_CODE (innerop) == SSA_NAME
9438 && !POINTER_TYPE_P (TREE_TYPE (innerop)))
9440 value_range *vr = get_value_range (innerop);
9442 if (range_int_cst_p (vr)
9443 && range_fits_type_p (vr,
9444 TYPE_PRECISION (TREE_TYPE (op0)),
9445 TYPE_SIGN (TREE_TYPE (op0)))
9446 && int_fits_type_p (op1, TREE_TYPE (innerop))
9447 /* The range must not have overflowed, or if it did overflow
9448 we must not be wrapping/trapping overflow and optimizing
9449 with strict overflow semantics. */
9450 && ((!is_negative_overflow_infinity (vr->min)
9451 && !is_positive_overflow_infinity (vr->max))
9452 || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (innerop))))
9454 /* If the range overflowed and the user has asked for warnings
9455 when strict overflow semantics were used to optimize code,
9456 issue an appropriate warning. */
9457 if (cond_code != EQ_EXPR && cond_code != NE_EXPR
9458 && (is_negative_overflow_infinity (vr->min)
9459 || is_positive_overflow_infinity (vr->max))
9460 && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_CONDITIONAL))
9462 location_t location;
9464 if (!gimple_has_location (stmt))
9465 location = input_location;
9466 else
9467 location = gimple_location (stmt);
9468 warning_at (location, OPT_Wstrict_overflow,
9469 "assuming signed overflow does not occur when "
9470 "simplifying conditional");
9473 tree newconst = fold_convert (TREE_TYPE (innerop), op1);
9474 gimple_cond_set_lhs (stmt, innerop);
9475 gimple_cond_set_rhs (stmt, newconst);
9476 return true;
9481 return false;
9484 /* Simplify a switch statement using the value range of the switch
9485 argument. */
9487 static bool
9488 simplify_switch_using_ranges (gswitch *stmt)
9490 tree op = gimple_switch_index (stmt);
9491 value_range *vr;
9492 bool take_default;
9493 edge e;
9494 edge_iterator ei;
9495 size_t i = 0, j = 0, n, n2;
9496 tree vec2;
9497 switch_update su;
9498 size_t k = 1, l = 0;
9500 if (TREE_CODE (op) == SSA_NAME)
9502 vr = get_value_range (op);
9504 /* We can only handle integer ranges. */
9505 if ((vr->type != VR_RANGE
9506 && vr->type != VR_ANTI_RANGE)
9507 || symbolic_range_p (vr))
9508 return false;
9510 /* Find case label for min/max of the value range. */
9511 take_default = !find_case_label_ranges (stmt, vr, &i, &j, &k, &l);
9513 else if (TREE_CODE (op) == INTEGER_CST)
9515 take_default = !find_case_label_index (stmt, 1, op, &i);
9516 if (take_default)
9518 i = 1;
9519 j = 0;
9521 else
9523 j = i;
9526 else
9527 return false;
9529 n = gimple_switch_num_labels (stmt);
9531 /* Bail out if this is just all edges taken. */
9532 if (i == 1
9533 && j == n - 1
9534 && take_default)
9535 return false;
9537 /* Build a new vector of taken case labels. */
9538 vec2 = make_tree_vec (j - i + 1 + l - k + 1 + (int)take_default);
9539 n2 = 0;
9541 /* Add the default edge, if necessary. */
9542 if (take_default)
9543 TREE_VEC_ELT (vec2, n2++) = gimple_switch_default_label (stmt);
9545 for (; i <= j; ++i, ++n2)
9546 TREE_VEC_ELT (vec2, n2) = gimple_switch_label (stmt, i);
9548 for (; k <= l; ++k, ++n2)
9549 TREE_VEC_ELT (vec2, n2) = gimple_switch_label (stmt, k);
9551 /* Mark needed edges. */
9552 for (i = 0; i < n2; ++i)
9554 e = find_edge (gimple_bb (stmt),
9555 label_to_block (CASE_LABEL (TREE_VEC_ELT (vec2, i))));
9556 e->aux = (void *)-1;
9559 /* Queue not needed edges for later removal. */
9560 FOR_EACH_EDGE (e, ei, gimple_bb (stmt)->succs)
9562 if (e->aux == (void *)-1)
9564 e->aux = NULL;
9565 continue;
9568 if (dump_file && (dump_flags & TDF_DETAILS))
9570 fprintf (dump_file, "removing unreachable case label\n");
9572 to_remove_edges.safe_push (e);
9573 e->flags &= ~EDGE_EXECUTABLE;
9576 /* And queue an update for the stmt. */
9577 su.stmt = stmt;
9578 su.vec = vec2;
9579 to_update_switch_stmts.safe_push (su);
9580 return false;
9583 /* Simplify an integral conversion from an SSA name in STMT. */
9585 static bool
9586 simplify_conversion_using_ranges (gimple *stmt)
9588 tree innerop, middleop, finaltype;
9589 gimple *def_stmt;
9590 value_range *innervr;
9591 signop inner_sgn, middle_sgn, final_sgn;
9592 unsigned inner_prec, middle_prec, final_prec;
9593 widest_int innermin, innermed, innermax, middlemin, middlemed, middlemax;
9595 finaltype = TREE_TYPE (gimple_assign_lhs (stmt));
9596 if (!INTEGRAL_TYPE_P (finaltype))
9597 return false;
9598 middleop = gimple_assign_rhs1 (stmt);
9599 def_stmt = SSA_NAME_DEF_STMT (middleop);
9600 if (!is_gimple_assign (def_stmt)
9601 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt)))
9602 return false;
9603 innerop = gimple_assign_rhs1 (def_stmt);
9604 if (TREE_CODE (innerop) != SSA_NAME
9605 || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (innerop))
9606 return false;
9608 /* Get the value-range of the inner operand. */
9609 innervr = get_value_range (innerop);
9610 if (innervr->type != VR_RANGE
9611 || TREE_CODE (innervr->min) != INTEGER_CST
9612 || TREE_CODE (innervr->max) != INTEGER_CST)
9613 return false;
9615 /* Simulate the conversion chain to check if the result is equal if
9616 the middle conversion is removed. */
9617 innermin = wi::to_widest (innervr->min);
9618 innermax = wi::to_widest (innervr->max);
9620 inner_prec = TYPE_PRECISION (TREE_TYPE (innerop));
9621 middle_prec = TYPE_PRECISION (TREE_TYPE (middleop));
9622 final_prec = TYPE_PRECISION (finaltype);
9624 /* If the first conversion is not injective, the second must not
9625 be widening. */
9626 if (wi::gtu_p (innermax - innermin,
9627 wi::mask <widest_int> (middle_prec, false))
9628 && middle_prec < final_prec)
9629 return false;
9630 /* We also want a medium value so that we can track the effect that
9631 narrowing conversions with sign change have. */
9632 inner_sgn = TYPE_SIGN (TREE_TYPE (innerop));
9633 if (inner_sgn == UNSIGNED)
9634 innermed = wi::shifted_mask <widest_int> (1, inner_prec - 1, false);
9635 else
9636 innermed = 0;
9637 if (wi::cmp (innermin, innermed, inner_sgn) >= 0
9638 || wi::cmp (innermed, innermax, inner_sgn) >= 0)
9639 innermed = innermin;
9641 middle_sgn = TYPE_SIGN (TREE_TYPE (middleop));
9642 middlemin = wi::ext (innermin, middle_prec, middle_sgn);
9643 middlemed = wi::ext (innermed, middle_prec, middle_sgn);
9644 middlemax = wi::ext (innermax, middle_prec, middle_sgn);
9646 /* Require that the final conversion applied to both the original
9647 and the intermediate range produces the same result. */
9648 final_sgn = TYPE_SIGN (finaltype);
9649 if (wi::ext (middlemin, final_prec, final_sgn)
9650 != wi::ext (innermin, final_prec, final_sgn)
9651 || wi::ext (middlemed, final_prec, final_sgn)
9652 != wi::ext (innermed, final_prec, final_sgn)
9653 || wi::ext (middlemax, final_prec, final_sgn)
9654 != wi::ext (innermax, final_prec, final_sgn))
9655 return false;
9657 gimple_assign_set_rhs1 (stmt, innerop);
9658 update_stmt (stmt);
9659 return true;
9662 /* Simplify a conversion from integral SSA name to float in STMT. */
9664 static bool
9665 simplify_float_conversion_using_ranges (gimple_stmt_iterator *gsi,
9666 gimple *stmt)
9668 tree rhs1 = gimple_assign_rhs1 (stmt);
9669 value_range *vr = get_value_range (rhs1);
9670 machine_mode fltmode = TYPE_MODE (TREE_TYPE (gimple_assign_lhs (stmt)));
9671 machine_mode mode;
9672 tree tem;
9673 gassign *conv;
9675 /* We can only handle constant ranges. */
9676 if (vr->type != VR_RANGE
9677 || TREE_CODE (vr->min) != INTEGER_CST
9678 || TREE_CODE (vr->max) != INTEGER_CST)
9679 return false;
9681 /* First check if we can use a signed type in place of an unsigned. */
9682 if (TYPE_UNSIGNED (TREE_TYPE (rhs1))
9683 && (can_float_p (fltmode, TYPE_MODE (TREE_TYPE (rhs1)), 0)
9684 != CODE_FOR_nothing)
9685 && range_fits_type_p (vr, TYPE_PRECISION (TREE_TYPE (rhs1)), SIGNED))
9686 mode = TYPE_MODE (TREE_TYPE (rhs1));
9687 /* If we can do the conversion in the current input mode do nothing. */
9688 else if (can_float_p (fltmode, TYPE_MODE (TREE_TYPE (rhs1)),
9689 TYPE_UNSIGNED (TREE_TYPE (rhs1))) != CODE_FOR_nothing)
9690 return false;
9691 /* Otherwise search for a mode we can use, starting from the narrowest
9692 integer mode available. */
9693 else
9695 mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
9698 /* If we cannot do a signed conversion to float from mode
9699 or if the value-range does not fit in the signed type
9700 try with a wider mode. */
9701 if (can_float_p (fltmode, mode, 0) != CODE_FOR_nothing
9702 && range_fits_type_p (vr, GET_MODE_PRECISION (mode), SIGNED))
9703 break;
9705 mode = GET_MODE_WIDER_MODE (mode);
9706 /* But do not widen the input. Instead leave that to the
9707 optabs expansion code. */
9708 if (GET_MODE_PRECISION (mode) > TYPE_PRECISION (TREE_TYPE (rhs1)))
9709 return false;
9711 while (mode != VOIDmode);
9712 if (mode == VOIDmode)
9713 return false;
9716 /* It works, insert a truncation or sign-change before the
9717 float conversion. */
9718 tem = make_ssa_name (build_nonstandard_integer_type
9719 (GET_MODE_PRECISION (mode), 0));
9720 conv = gimple_build_assign (tem, NOP_EXPR, rhs1);
9721 gsi_insert_before (gsi, conv, GSI_SAME_STMT);
9722 gimple_assign_set_rhs1 (stmt, tem);
9723 update_stmt (stmt);
9725 return true;
9728 /* Simplify an internal fn call using ranges if possible. */
9730 static bool
9731 simplify_internal_call_using_ranges (gimple_stmt_iterator *gsi, gimple *stmt)
9733 enum tree_code subcode;
9734 bool is_ubsan = false;
9735 bool ovf = false;
9736 switch (gimple_call_internal_fn (stmt))
9738 case IFN_UBSAN_CHECK_ADD:
9739 subcode = PLUS_EXPR;
9740 is_ubsan = true;
9741 break;
9742 case IFN_UBSAN_CHECK_SUB:
9743 subcode = MINUS_EXPR;
9744 is_ubsan = true;
9745 break;
9746 case IFN_UBSAN_CHECK_MUL:
9747 subcode = MULT_EXPR;
9748 is_ubsan = true;
9749 break;
9750 case IFN_ADD_OVERFLOW:
9751 subcode = PLUS_EXPR;
9752 break;
9753 case IFN_SUB_OVERFLOW:
9754 subcode = MINUS_EXPR;
9755 break;
9756 case IFN_MUL_OVERFLOW:
9757 subcode = MULT_EXPR;
9758 break;
9759 default:
9760 return false;
9763 tree op0 = gimple_call_arg (stmt, 0);
9764 tree op1 = gimple_call_arg (stmt, 1);
9765 tree type;
9766 if (is_ubsan)
9767 type = TREE_TYPE (op0);
9768 else if (gimple_call_lhs (stmt) == NULL_TREE)
9769 return false;
9770 else
9771 type = TREE_TYPE (TREE_TYPE (gimple_call_lhs (stmt)));
9772 if (!check_for_binary_op_overflow (subcode, type, op0, op1, &ovf)
9773 || (is_ubsan && ovf))
9774 return false;
9776 gimple *g;
9777 location_t loc = gimple_location (stmt);
9778 if (is_ubsan)
9779 g = gimple_build_assign (gimple_call_lhs (stmt), subcode, op0, op1);
9780 else
9782 int prec = TYPE_PRECISION (type);
9783 tree utype = type;
9784 if (ovf
9785 || !useless_type_conversion_p (type, TREE_TYPE (op0))
9786 || !useless_type_conversion_p (type, TREE_TYPE (op1)))
9787 utype = build_nonstandard_integer_type (prec, 1);
9788 if (TREE_CODE (op0) == INTEGER_CST)
9789 op0 = fold_convert (utype, op0);
9790 else if (!useless_type_conversion_p (utype, TREE_TYPE (op0)))
9792 g = gimple_build_assign (make_ssa_name (utype), NOP_EXPR, op0);
9793 gimple_set_location (g, loc);
9794 gsi_insert_before (gsi, g, GSI_SAME_STMT);
9795 op0 = gimple_assign_lhs (g);
9797 if (TREE_CODE (op1) == INTEGER_CST)
9798 op1 = fold_convert (utype, op1);
9799 else if (!useless_type_conversion_p (utype, TREE_TYPE (op1)))
9801 g = gimple_build_assign (make_ssa_name (utype), NOP_EXPR, op1);
9802 gimple_set_location (g, loc);
9803 gsi_insert_before (gsi, g, GSI_SAME_STMT);
9804 op1 = gimple_assign_lhs (g);
9806 g = gimple_build_assign (make_ssa_name (utype), subcode, op0, op1);
9807 gimple_set_location (g, loc);
9808 gsi_insert_before (gsi, g, GSI_SAME_STMT);
9809 if (utype != type)
9811 g = gimple_build_assign (make_ssa_name (type), NOP_EXPR,
9812 gimple_assign_lhs (g));
9813 gimple_set_location (g, loc);
9814 gsi_insert_before (gsi, g, GSI_SAME_STMT);
9816 g = gimple_build_assign (gimple_call_lhs (stmt), COMPLEX_EXPR,
9817 gimple_assign_lhs (g),
9818 build_int_cst (type, ovf));
9820 gimple_set_location (g, loc);
9821 gsi_replace (gsi, g, false);
9822 return true;
9825 /* Simplify STMT using ranges if possible. */
9827 static bool
9828 simplify_stmt_using_ranges (gimple_stmt_iterator *gsi)
9830 gimple *stmt = gsi_stmt (*gsi);
9831 if (is_gimple_assign (stmt))
9833 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
9834 tree rhs1 = gimple_assign_rhs1 (stmt);
9836 switch (rhs_code)
9838 case EQ_EXPR:
9839 case NE_EXPR:
9840 /* Transform EQ_EXPR, NE_EXPR into BIT_XOR_EXPR or identity
9841 if the RHS is zero or one, and the LHS are known to be boolean
9842 values. */
9843 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9844 return simplify_truth_ops_using_ranges (gsi, stmt);
9845 break;
9847 /* Transform TRUNC_DIV_EXPR and TRUNC_MOD_EXPR into RSHIFT_EXPR
9848 and BIT_AND_EXPR respectively if the first operand is greater
9849 than zero and the second operand is an exact power of two.
9850 Also optimize TRUNC_MOD_EXPR away if the second operand is
9851 constant and the first operand already has the right value
9852 range. */
9853 case TRUNC_DIV_EXPR:
9854 case TRUNC_MOD_EXPR:
9855 if (TREE_CODE (rhs1) == SSA_NAME
9856 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9857 return simplify_div_or_mod_using_ranges (stmt);
9858 break;
9860 /* Transform ABS (X) into X or -X as appropriate. */
9861 case ABS_EXPR:
9862 if (TREE_CODE (rhs1) == SSA_NAME
9863 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9864 return simplify_abs_using_ranges (stmt);
9865 break;
9867 case BIT_AND_EXPR:
9868 case BIT_IOR_EXPR:
9869 /* Optimize away BIT_AND_EXPR and BIT_IOR_EXPR
9870 if all the bits being cleared are already cleared or
9871 all the bits being set are already set. */
9872 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9873 return simplify_bit_ops_using_ranges (gsi, stmt);
9874 break;
9876 CASE_CONVERT:
9877 if (TREE_CODE (rhs1) == SSA_NAME
9878 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9879 return simplify_conversion_using_ranges (stmt);
9880 break;
9882 case FLOAT_EXPR:
9883 if (TREE_CODE (rhs1) == SSA_NAME
9884 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9885 return simplify_float_conversion_using_ranges (gsi, stmt);
9886 break;
9888 case MIN_EXPR:
9889 case MAX_EXPR:
9890 return simplify_min_or_max_using_ranges (stmt);
9891 break;
9893 default:
9894 break;
9897 else if (gimple_code (stmt) == GIMPLE_COND)
9898 return simplify_cond_using_ranges (as_a <gcond *> (stmt));
9899 else if (gimple_code (stmt) == GIMPLE_SWITCH)
9900 return simplify_switch_using_ranges (as_a <gswitch *> (stmt));
9901 else if (is_gimple_call (stmt)
9902 && gimple_call_internal_p (stmt))
9903 return simplify_internal_call_using_ranges (gsi, stmt);
9905 return false;
9908 /* If the statement pointed by SI has a predicate whose value can be
9909 computed using the value range information computed by VRP, compute
9910 its value and return true. Otherwise, return false. */
9912 static bool
9913 fold_predicate_in (gimple_stmt_iterator *si)
9915 bool assignment_p = false;
9916 tree val;
9917 gimple *stmt = gsi_stmt (*si);
9919 if (is_gimple_assign (stmt)
9920 && TREE_CODE_CLASS (gimple_assign_rhs_code (stmt)) == tcc_comparison)
9922 assignment_p = true;
9923 val = vrp_evaluate_conditional (gimple_assign_rhs_code (stmt),
9924 gimple_assign_rhs1 (stmt),
9925 gimple_assign_rhs2 (stmt),
9926 stmt);
9928 else if (gcond *cond_stmt = dyn_cast <gcond *> (stmt))
9929 val = vrp_evaluate_conditional (gimple_cond_code (cond_stmt),
9930 gimple_cond_lhs (cond_stmt),
9931 gimple_cond_rhs (cond_stmt),
9932 stmt);
9933 else
9934 return false;
9936 if (val)
9938 if (assignment_p)
9939 val = fold_convert (gimple_expr_type (stmt), val);
9941 if (dump_file)
9943 fprintf (dump_file, "Folding predicate ");
9944 print_gimple_expr (dump_file, stmt, 0, 0);
9945 fprintf (dump_file, " to ");
9946 print_generic_expr (dump_file, val, 0);
9947 fprintf (dump_file, "\n");
9950 if (is_gimple_assign (stmt))
9951 gimple_assign_set_rhs_from_tree (si, val);
9952 else
9954 gcc_assert (gimple_code (stmt) == GIMPLE_COND);
9955 gcond *cond_stmt = as_a <gcond *> (stmt);
9956 if (integer_zerop (val))
9957 gimple_cond_make_false (cond_stmt);
9958 else if (integer_onep (val))
9959 gimple_cond_make_true (cond_stmt);
9960 else
9961 gcc_unreachable ();
9964 return true;
9967 return false;
9970 /* Callback for substitute_and_fold folding the stmt at *SI. */
9972 static bool
9973 vrp_fold_stmt (gimple_stmt_iterator *si)
9975 if (fold_predicate_in (si))
9976 return true;
9978 return simplify_stmt_using_ranges (si);
9981 /* Unwindable const/copy equivalences. */
9982 const_and_copies *equiv_stack;
9984 /* A trivial wrapper so that we can present the generic jump threading
9985 code with a simple API for simplifying statements. STMT is the
9986 statement we want to simplify, WITHIN_STMT provides the location
9987 for any overflow warnings. */
9989 static tree
9990 simplify_stmt_for_jump_threading (gimple *stmt, gimple *within_stmt,
9991 class avail_exprs_stack *avail_exprs_stack ATTRIBUTE_UNUSED)
9993 if (gcond *cond_stmt = dyn_cast <gcond *> (stmt))
9994 return vrp_evaluate_conditional (gimple_cond_code (cond_stmt),
9995 gimple_cond_lhs (cond_stmt),
9996 gimple_cond_rhs (cond_stmt),
9997 within_stmt);
9999 if (gassign *assign_stmt = dyn_cast <gassign *> (stmt))
10001 value_range new_vr = VR_INITIALIZER;
10002 tree lhs = gimple_assign_lhs (assign_stmt);
10004 if (TREE_CODE (lhs) == SSA_NAME
10005 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs))
10006 || POINTER_TYPE_P (TREE_TYPE (lhs))))
10008 extract_range_from_assignment (&new_vr, assign_stmt);
10009 if (range_int_cst_singleton_p (&new_vr))
10010 return new_vr.min;
10014 return NULL_TREE;
10017 /* Blocks which have more than one predecessor and more than
10018 one successor present jump threading opportunities, i.e.,
10019 when the block is reached from a specific predecessor, we
10020 may be able to determine which of the outgoing edges will
10021 be traversed. When this optimization applies, we are able
10022 to avoid conditionals at runtime and we may expose secondary
10023 optimization opportunities.
10025 This routine is effectively a driver for the generic jump
10026 threading code. It basically just presents the generic code
10027 with edges that may be suitable for jump threading.
10029 Unlike DOM, we do not iterate VRP if jump threading was successful.
10030 While iterating may expose new opportunities for VRP, it is expected
10031 those opportunities would be very limited and the compile time cost
10032 to expose those opportunities would be significant.
10034 As jump threading opportunities are discovered, they are registered
10035 for later realization. */
10037 static void
10038 identify_jump_threads (void)
10040 basic_block bb;
10041 gcond *dummy;
10042 int i;
10043 edge e;
10045 /* Ugh. When substituting values earlier in this pass we can
10046 wipe the dominance information. So rebuild the dominator
10047 information as we need it within the jump threading code. */
10048 calculate_dominance_info (CDI_DOMINATORS);
10050 /* We do not allow VRP information to be used for jump threading
10051 across a back edge in the CFG. Otherwise it becomes too
10052 difficult to avoid eliminating loop exit tests. Of course
10053 EDGE_DFS_BACK is not accurate at this time so we have to
10054 recompute it. */
10055 mark_dfs_back_edges ();
10057 /* Do not thread across edges we are about to remove. Just marking
10058 them as EDGE_DFS_BACK will do. */
10059 FOR_EACH_VEC_ELT (to_remove_edges, i, e)
10060 e->flags |= EDGE_DFS_BACK;
10062 /* Allocate our unwinder stack to unwind any temporary equivalences
10063 that might be recorded. */
10064 equiv_stack = new const_and_copies ();
10066 /* To avoid lots of silly node creation, we create a single
10067 conditional and just modify it in-place when attempting to
10068 thread jumps. */
10069 dummy = gimple_build_cond (EQ_EXPR,
10070 integer_zero_node, integer_zero_node,
10071 NULL, NULL);
10073 /* Walk through all the blocks finding those which present a
10074 potential jump threading opportunity. We could set this up
10075 as a dominator walker and record data during the walk, but
10076 I doubt it's worth the effort for the classes of jump
10077 threading opportunities we are trying to identify at this
10078 point in compilation. */
10079 FOR_EACH_BB_FN (bb, cfun)
10081 gimple *last;
10083 /* If the generic jump threading code does not find this block
10084 interesting, then there is nothing to do. */
10085 if (! potentially_threadable_block (bb))
10086 continue;
10088 last = last_stmt (bb);
10090 /* We're basically looking for a switch or any kind of conditional with
10091 integral or pointer type arguments. Note the type of the second
10092 argument will be the same as the first argument, so no need to
10093 check it explicitly.
10095 We also handle the case where there are no statements in the
10096 block. This come up with forwarder blocks that are not
10097 optimized away because they lead to a loop header. But we do
10098 want to thread through them as we can sometimes thread to the
10099 loop exit which is obviously profitable. */
10100 if (!last
10101 || gimple_code (last) == GIMPLE_SWITCH
10102 || (gimple_code (last) == GIMPLE_COND
10103 && TREE_CODE (gimple_cond_lhs (last)) == SSA_NAME
10104 && (INTEGRAL_TYPE_P (TREE_TYPE (gimple_cond_lhs (last)))
10105 || POINTER_TYPE_P (TREE_TYPE (gimple_cond_lhs (last))))
10106 && (TREE_CODE (gimple_cond_rhs (last)) == SSA_NAME
10107 || is_gimple_min_invariant (gimple_cond_rhs (last)))))
10109 edge_iterator ei;
10111 /* We've got a block with multiple predecessors and multiple
10112 successors which also ends in a suitable conditional or
10113 switch statement. For each predecessor, see if we can thread
10114 it to a specific successor. */
10115 FOR_EACH_EDGE (e, ei, bb->preds)
10117 /* Do not thread across back edges or abnormal edges
10118 in the CFG. */
10119 if (e->flags & (EDGE_DFS_BACK | EDGE_COMPLEX))
10120 continue;
10122 thread_across_edge (dummy, e, true, equiv_stack, NULL,
10123 simplify_stmt_for_jump_threading);
10128 /* We do not actually update the CFG or SSA graphs at this point as
10129 ASSERT_EXPRs are still in the IL and cfg cleanup code does not yet
10130 handle ASSERT_EXPRs gracefully. */
10133 /* We identified all the jump threading opportunities earlier, but could
10134 not transform the CFG at that time. This routine transforms the
10135 CFG and arranges for the dominator tree to be rebuilt if necessary.
10137 Note the SSA graph update will occur during the normal TODO
10138 processing by the pass manager. */
10139 static void
10140 finalize_jump_threads (void)
10142 thread_through_all_blocks (false);
10143 delete equiv_stack;
10147 /* Traverse all the blocks folding conditionals with known ranges. */
10149 static void
10150 vrp_finalize (void)
10152 size_t i;
10154 values_propagated = true;
10156 if (dump_file)
10158 fprintf (dump_file, "\nValue ranges after VRP:\n\n");
10159 dump_all_value_ranges (dump_file);
10160 fprintf (dump_file, "\n");
10163 substitute_and_fold (op_with_constant_singleton_value_range,
10164 vrp_fold_stmt, false);
10166 if (warn_array_bounds && first_pass_instance)
10167 check_all_array_refs ();
10169 /* We must identify jump threading opportunities before we release
10170 the datastructures built by VRP. */
10171 identify_jump_threads ();
10173 /* Set value range to non pointer SSA_NAMEs. */
10174 for (i = 0; i < num_vr_values; i++)
10175 if (vr_value[i])
10177 tree name = ssa_name (i);
10179 if (!name
10180 || POINTER_TYPE_P (TREE_TYPE (name))
10181 || (vr_value[i]->type == VR_VARYING)
10182 || (vr_value[i]->type == VR_UNDEFINED))
10183 continue;
10185 if ((TREE_CODE (vr_value[i]->min) == INTEGER_CST)
10186 && (TREE_CODE (vr_value[i]->max) == INTEGER_CST)
10187 && (vr_value[i]->type == VR_RANGE
10188 || vr_value[i]->type == VR_ANTI_RANGE))
10189 set_range_info (name, vr_value[i]->type, vr_value[i]->min,
10190 vr_value[i]->max);
10193 /* Free allocated memory. */
10194 for (i = 0; i < num_vr_values; i++)
10195 if (vr_value[i])
10197 BITMAP_FREE (vr_value[i]->equiv);
10198 free (vr_value[i]);
10201 free (vr_value);
10202 free (vr_phi_edge_counts);
10204 /* So that we can distinguish between VRP data being available
10205 and not available. */
10206 vr_value = NULL;
10207 vr_phi_edge_counts = NULL;
10211 /* Main entry point to VRP (Value Range Propagation). This pass is
10212 loosely based on J. R. C. Patterson, ``Accurate Static Branch
10213 Prediction by Value Range Propagation,'' in SIGPLAN Conference on
10214 Programming Language Design and Implementation, pp. 67-78, 1995.
10215 Also available at http://citeseer.ist.psu.edu/patterson95accurate.html
10217 This is essentially an SSA-CCP pass modified to deal with ranges
10218 instead of constants.
10220 While propagating ranges, we may find that two or more SSA name
10221 have equivalent, though distinct ranges. For instance,
10223 1 x_9 = p_3->a;
10224 2 p_4 = ASSERT_EXPR <p_3, p_3 != 0>
10225 3 if (p_4 == q_2)
10226 4 p_5 = ASSERT_EXPR <p_4, p_4 == q_2>;
10227 5 endif
10228 6 if (q_2)
10230 In the code above, pointer p_5 has range [q_2, q_2], but from the
10231 code we can also determine that p_5 cannot be NULL and, if q_2 had
10232 a non-varying range, p_5's range should also be compatible with it.
10234 These equivalences are created by two expressions: ASSERT_EXPR and
10235 copy operations. Since p_5 is an assertion on p_4, and p_4 was the
10236 result of another assertion, then we can use the fact that p_5 and
10237 p_4 are equivalent when evaluating p_5's range.
10239 Together with value ranges, we also propagate these equivalences
10240 between names so that we can take advantage of information from
10241 multiple ranges when doing final replacement. Note that this
10242 equivalency relation is transitive but not symmetric.
10244 In the example above, p_5 is equivalent to p_4, q_2 and p_3, but we
10245 cannot assert that q_2 is equivalent to p_5 because q_2 may be used
10246 in contexts where that assertion does not hold (e.g., in line 6).
10248 TODO, the main difference between this pass and Patterson's is that
10249 we do not propagate edge probabilities. We only compute whether
10250 edges can be taken or not. That is, instead of having a spectrum
10251 of jump probabilities between 0 and 1, we only deal with 0, 1 and
10252 DON'T KNOW. In the future, it may be worthwhile to propagate
10253 probabilities to aid branch prediction. */
10255 static unsigned int
10256 execute_vrp (void)
10258 int i;
10259 edge e;
10260 switch_update *su;
10262 loop_optimizer_init (LOOPS_NORMAL | LOOPS_HAVE_RECORDED_EXITS);
10263 rewrite_into_loop_closed_ssa (NULL, TODO_update_ssa);
10264 scev_initialize ();
10266 /* ??? This ends up using stale EDGE_DFS_BACK for liveness computation.
10267 Inserting assertions may split edges which will invalidate
10268 EDGE_DFS_BACK. */
10269 insert_range_assertions ();
10271 to_remove_edges.create (10);
10272 to_update_switch_stmts.create (5);
10273 threadedge_initialize_values ();
10275 /* For visiting PHI nodes we need EDGE_DFS_BACK computed. */
10276 mark_dfs_back_edges ();
10278 vrp_initialize ();
10279 ssa_propagate (vrp_visit_stmt, vrp_visit_phi_node);
10280 vrp_finalize ();
10282 free_numbers_of_iterations_estimates (cfun);
10284 /* ASSERT_EXPRs must be removed before finalizing jump threads
10285 as finalizing jump threads calls the CFG cleanup code which
10286 does not properly handle ASSERT_EXPRs. */
10287 remove_range_assertions ();
10289 /* If we exposed any new variables, go ahead and put them into
10290 SSA form now, before we handle jump threading. This simplifies
10291 interactions between rewriting of _DECL nodes into SSA form
10292 and rewriting SSA_NAME nodes into SSA form after block
10293 duplication and CFG manipulation. */
10294 update_ssa (TODO_update_ssa);
10296 finalize_jump_threads ();
10298 /* Remove dead edges from SWITCH_EXPR optimization. This leaves the
10299 CFG in a broken state and requires a cfg_cleanup run. */
10300 FOR_EACH_VEC_ELT (to_remove_edges, i, e)
10301 remove_edge (e);
10302 /* Update SWITCH_EXPR case label vector. */
10303 FOR_EACH_VEC_ELT (to_update_switch_stmts, i, su)
10305 size_t j;
10306 size_t n = TREE_VEC_LENGTH (su->vec);
10307 tree label;
10308 gimple_switch_set_num_labels (su->stmt, n);
10309 for (j = 0; j < n; j++)
10310 gimple_switch_set_label (su->stmt, j, TREE_VEC_ELT (su->vec, j));
10311 /* As we may have replaced the default label with a regular one
10312 make sure to make it a real default label again. This ensures
10313 optimal expansion. */
10314 label = gimple_switch_label (su->stmt, 0);
10315 CASE_LOW (label) = NULL_TREE;
10316 CASE_HIGH (label) = NULL_TREE;
10319 if (to_remove_edges.length () > 0)
10321 free_dominance_info (CDI_DOMINATORS);
10322 loops_state_set (LOOPS_NEED_FIXUP);
10325 to_remove_edges.release ();
10326 to_update_switch_stmts.release ();
10327 threadedge_finalize_values ();
10329 scev_finalize ();
10330 loop_optimizer_finalize ();
10331 return 0;
10334 namespace {
10336 const pass_data pass_data_vrp =
10338 GIMPLE_PASS, /* type */
10339 "vrp", /* name */
10340 OPTGROUP_NONE, /* optinfo_flags */
10341 TV_TREE_VRP, /* tv_id */
10342 PROP_ssa, /* properties_required */
10343 0, /* properties_provided */
10344 0, /* properties_destroyed */
10345 0, /* todo_flags_start */
10346 ( TODO_cleanup_cfg | TODO_update_ssa ), /* todo_flags_finish */
10349 class pass_vrp : public gimple_opt_pass
10351 public:
10352 pass_vrp (gcc::context *ctxt)
10353 : gimple_opt_pass (pass_data_vrp, ctxt)
10356 /* opt_pass methods: */
10357 opt_pass * clone () { return new pass_vrp (m_ctxt); }
10358 virtual bool gate (function *) { return flag_tree_vrp != 0; }
10359 virtual unsigned int execute (function *) { return execute_vrp (); }
10361 }; // class pass_vrp
10363 } // anon namespace
10365 gimple_opt_pass *
10366 make_pass_vrp (gcc::context *ctxt)
10368 return new pass_vrp (ctxt);