remove gimple_location_ptr ()
[official-gcc.git] / gcc / tree-vrp.c
blobef5ef1009589c7b40400b334179fe7f5ec76b521
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 #ifdef ENABLE_CHECKING
246 gcc_assert (needs_overflow_infinity (type));
247 #endif
248 return (min != NULL_TREE
249 && CONSTANT_CLASS_P (min)
250 && max != NULL_TREE
251 && CONSTANT_CLASS_P (max));
254 /* VAL is the maximum or minimum value of a type. Return a
255 corresponding overflow infinity. */
257 static inline tree
258 make_overflow_infinity (tree val)
260 gcc_checking_assert (val != NULL_TREE && CONSTANT_CLASS_P (val));
261 val = copy_node (val);
262 TREE_OVERFLOW (val) = 1;
263 return val;
266 /* Return a negative overflow infinity for TYPE. */
268 static inline tree
269 negative_overflow_infinity (tree type)
271 gcc_checking_assert (supports_overflow_infinity (type));
272 return make_overflow_infinity (vrp_val_min (type));
275 /* Return a positive overflow infinity for TYPE. */
277 static inline tree
278 positive_overflow_infinity (tree type)
280 gcc_checking_assert (supports_overflow_infinity (type));
281 return make_overflow_infinity (vrp_val_max (type));
284 /* Return whether VAL is a negative overflow infinity. */
286 static inline bool
287 is_negative_overflow_infinity (const_tree val)
289 return (TREE_OVERFLOW_P (val)
290 && needs_overflow_infinity (TREE_TYPE (val))
291 && vrp_val_is_min (val));
294 /* Return whether VAL is a positive overflow infinity. */
296 static inline bool
297 is_positive_overflow_infinity (const_tree val)
299 return (TREE_OVERFLOW_P (val)
300 && needs_overflow_infinity (TREE_TYPE (val))
301 && vrp_val_is_max (val));
304 /* Return whether VAL is a positive or negative overflow infinity. */
306 static inline bool
307 is_overflow_infinity (const_tree val)
309 return (TREE_OVERFLOW_P (val)
310 && needs_overflow_infinity (TREE_TYPE (val))
311 && (vrp_val_is_min (val) || vrp_val_is_max (val)));
314 /* Return whether STMT has a constant rhs that is_overflow_infinity. */
316 static inline bool
317 stmt_overflow_infinity (gimple *stmt)
319 if (is_gimple_assign (stmt)
320 && get_gimple_rhs_class (gimple_assign_rhs_code (stmt)) ==
321 GIMPLE_SINGLE_RHS)
322 return is_overflow_infinity (gimple_assign_rhs1 (stmt));
323 return false;
326 /* If VAL is now an overflow infinity, return VAL. Otherwise, return
327 the same value with TREE_OVERFLOW clear. This can be used to avoid
328 confusing a regular value with an overflow value. */
330 static inline tree
331 avoid_overflow_infinity (tree val)
333 if (!is_overflow_infinity (val))
334 return val;
336 if (vrp_val_is_max (val))
337 return vrp_val_max (TREE_TYPE (val));
338 else
340 gcc_checking_assert (vrp_val_is_min (val));
341 return vrp_val_min (TREE_TYPE (val));
346 /* Set value range VR to VR_UNDEFINED. */
348 static inline void
349 set_value_range_to_undefined (value_range *vr)
351 vr->type = VR_UNDEFINED;
352 vr->min = vr->max = NULL_TREE;
353 if (vr->equiv)
354 bitmap_clear (vr->equiv);
358 /* Set value range VR to VR_VARYING. */
360 static inline void
361 set_value_range_to_varying (value_range *vr)
363 vr->type = VR_VARYING;
364 vr->min = vr->max = NULL_TREE;
365 if (vr->equiv)
366 bitmap_clear (vr->equiv);
370 /* Set value range VR to {T, MIN, MAX, EQUIV}. */
372 static void
373 set_value_range (value_range *vr, enum value_range_type t, tree min,
374 tree max, bitmap equiv)
376 #if defined ENABLE_CHECKING
377 /* Check the validity of the range. */
378 if (t == VR_RANGE || t == VR_ANTI_RANGE)
380 int cmp;
382 gcc_assert (min && max);
384 gcc_assert ((!TREE_OVERFLOW_P (min) || is_overflow_infinity (min))
385 && (!TREE_OVERFLOW_P (max) || is_overflow_infinity (max)));
387 if (INTEGRAL_TYPE_P (TREE_TYPE (min)) && t == VR_ANTI_RANGE)
388 gcc_assert (!vrp_val_is_min (min) || !vrp_val_is_max (max));
390 cmp = compare_values (min, max);
391 gcc_assert (cmp == 0 || cmp == -1 || cmp == -2);
393 if (needs_overflow_infinity (TREE_TYPE (min)))
394 gcc_assert (!is_overflow_infinity (min)
395 || !is_overflow_infinity (max));
398 if (t == VR_UNDEFINED || t == VR_VARYING)
399 gcc_assert (min == NULL_TREE && max == NULL_TREE);
401 if (t == VR_UNDEFINED || t == VR_VARYING)
402 gcc_assert (equiv == NULL || bitmap_empty_p (equiv));
403 #endif
405 vr->type = t;
406 vr->min = min;
407 vr->max = max;
409 /* Since updating the equivalence set involves deep copying the
410 bitmaps, only do it if absolutely necessary. */
411 if (vr->equiv == NULL
412 && equiv != NULL)
413 vr->equiv = BITMAP_ALLOC (NULL);
415 if (equiv != vr->equiv)
417 if (equiv && !bitmap_empty_p (equiv))
418 bitmap_copy (vr->equiv, equiv);
419 else
420 bitmap_clear (vr->equiv);
425 /* Set value range VR to the canonical form of {T, MIN, MAX, EQUIV}.
426 This means adjusting T, MIN and MAX representing the case of a
427 wrapping range with MAX < MIN covering [MIN, type_max] U [type_min, MAX]
428 as anti-rage ~[MAX+1, MIN-1]. Likewise for wrapping anti-ranges.
429 In corner cases where MAX+1 or MIN-1 wraps this will fall back
430 to varying.
431 This routine exists to ease canonicalization in the case where we
432 extract ranges from var + CST op limit. */
434 static void
435 set_and_canonicalize_value_range (value_range *vr, enum value_range_type t,
436 tree min, tree max, bitmap equiv)
438 /* Use the canonical setters for VR_UNDEFINED and VR_VARYING. */
439 if (t == VR_UNDEFINED)
441 set_value_range_to_undefined (vr);
442 return;
444 else if (t == VR_VARYING)
446 set_value_range_to_varying (vr);
447 return;
450 /* Nothing to canonicalize for symbolic ranges. */
451 if (TREE_CODE (min) != INTEGER_CST
452 || TREE_CODE (max) != INTEGER_CST)
454 set_value_range (vr, t, min, max, equiv);
455 return;
458 /* Wrong order for min and max, to swap them and the VR type we need
459 to adjust them. */
460 if (tree_int_cst_lt (max, min))
462 tree one, tmp;
464 /* For one bit precision if max < min, then the swapped
465 range covers all values, so for VR_RANGE it is varying and
466 for VR_ANTI_RANGE empty range, so drop to varying as well. */
467 if (TYPE_PRECISION (TREE_TYPE (min)) == 1)
469 set_value_range_to_varying (vr);
470 return;
473 one = build_int_cst (TREE_TYPE (min), 1);
474 tmp = int_const_binop (PLUS_EXPR, max, one);
475 max = int_const_binop (MINUS_EXPR, min, one);
476 min = tmp;
478 /* There's one corner case, if we had [C+1, C] before we now have
479 that again. But this represents an empty value range, so drop
480 to varying in this case. */
481 if (tree_int_cst_lt (max, min))
483 set_value_range_to_varying (vr);
484 return;
487 t = t == VR_RANGE ? VR_ANTI_RANGE : VR_RANGE;
490 /* Anti-ranges that can be represented as ranges should be so. */
491 if (t == VR_ANTI_RANGE)
493 bool is_min = vrp_val_is_min (min);
494 bool is_max = vrp_val_is_max (max);
496 if (is_min && is_max)
498 /* We cannot deal with empty ranges, drop to varying.
499 ??? This could be VR_UNDEFINED instead. */
500 set_value_range_to_varying (vr);
501 return;
503 else if (TYPE_PRECISION (TREE_TYPE (min)) == 1
504 && (is_min || is_max))
506 /* Non-empty boolean ranges can always be represented
507 as a singleton range. */
508 if (is_min)
509 min = max = vrp_val_max (TREE_TYPE (min));
510 else
511 min = max = vrp_val_min (TREE_TYPE (min));
512 t = VR_RANGE;
514 else if (is_min
515 /* As a special exception preserve non-null ranges. */
516 && !(TYPE_UNSIGNED (TREE_TYPE (min))
517 && integer_zerop (max)))
519 tree one = build_int_cst (TREE_TYPE (max), 1);
520 min = int_const_binop (PLUS_EXPR, max, one);
521 max = vrp_val_max (TREE_TYPE (max));
522 t = VR_RANGE;
524 else if (is_max)
526 tree one = build_int_cst (TREE_TYPE (min), 1);
527 max = int_const_binop (MINUS_EXPR, min, one);
528 min = vrp_val_min (TREE_TYPE (min));
529 t = VR_RANGE;
533 /* Drop [-INF(OVF), +INF(OVF)] to varying. */
534 if (needs_overflow_infinity (TREE_TYPE (min))
535 && is_overflow_infinity (min)
536 && is_overflow_infinity (max))
538 set_value_range_to_varying (vr);
539 return;
542 set_value_range (vr, t, min, max, equiv);
545 /* Copy value range FROM into value range TO. */
547 static inline void
548 copy_value_range (value_range *to, value_range *from)
550 set_value_range (to, from->type, from->min, from->max, from->equiv);
553 /* Set value range VR to a single value. This function is only called
554 with values we get from statements, and exists to clear the
555 TREE_OVERFLOW flag so that we don't think we have an overflow
556 infinity when we shouldn't. */
558 static inline void
559 set_value_range_to_value (value_range *vr, tree val, bitmap equiv)
561 gcc_assert (is_gimple_min_invariant (val));
562 if (TREE_OVERFLOW_P (val))
563 val = drop_tree_overflow (val);
564 set_value_range (vr, VR_RANGE, val, val, equiv);
567 /* Set value range VR to a non-negative range of type TYPE.
568 OVERFLOW_INFINITY indicates whether to use an overflow infinity
569 rather than TYPE_MAX_VALUE; this should be true if we determine
570 that the range is nonnegative based on the assumption that signed
571 overflow does not occur. */
573 static inline void
574 set_value_range_to_nonnegative (value_range *vr, tree type,
575 bool overflow_infinity)
577 tree zero;
579 if (overflow_infinity && !supports_overflow_infinity (type))
581 set_value_range_to_varying (vr);
582 return;
585 zero = build_int_cst (type, 0);
586 set_value_range (vr, VR_RANGE, zero,
587 (overflow_infinity
588 ? positive_overflow_infinity (type)
589 : TYPE_MAX_VALUE (type)),
590 vr->equiv);
593 /* Set value range VR to a non-NULL range of type TYPE. */
595 static inline void
596 set_value_range_to_nonnull (value_range *vr, tree type)
598 tree zero = build_int_cst (type, 0);
599 set_value_range (vr, VR_ANTI_RANGE, zero, zero, vr->equiv);
603 /* Set value range VR to a NULL range of type TYPE. */
605 static inline void
606 set_value_range_to_null (value_range *vr, tree type)
608 set_value_range_to_value (vr, build_int_cst (type, 0), vr->equiv);
612 /* Set value range VR to a range of a truthvalue of type TYPE. */
614 static inline void
615 set_value_range_to_truthvalue (value_range *vr, tree type)
617 if (TYPE_PRECISION (type) == 1)
618 set_value_range_to_varying (vr);
619 else
620 set_value_range (vr, VR_RANGE,
621 build_int_cst (type, 0), build_int_cst (type, 1),
622 vr->equiv);
626 /* If abs (min) < abs (max), set VR to [-max, max], if
627 abs (min) >= abs (max), set VR to [-min, min]. */
629 static void
630 abs_extent_range (value_range *vr, tree min, tree max)
632 int cmp;
634 gcc_assert (TREE_CODE (min) == INTEGER_CST);
635 gcc_assert (TREE_CODE (max) == INTEGER_CST);
636 gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (min)));
637 gcc_assert (!TYPE_UNSIGNED (TREE_TYPE (min)));
638 min = fold_unary (ABS_EXPR, TREE_TYPE (min), min);
639 max = fold_unary (ABS_EXPR, TREE_TYPE (max), max);
640 if (TREE_OVERFLOW (min) || TREE_OVERFLOW (max))
642 set_value_range_to_varying (vr);
643 return;
645 cmp = compare_values (min, max);
646 if (cmp == -1)
647 min = fold_unary (NEGATE_EXPR, TREE_TYPE (min), max);
648 else if (cmp == 0 || cmp == 1)
650 max = min;
651 min = fold_unary (NEGATE_EXPR, TREE_TYPE (min), min);
653 else
655 set_value_range_to_varying (vr);
656 return;
658 set_and_canonicalize_value_range (vr, VR_RANGE, min, max, NULL);
662 /* Return value range information for VAR.
664 If we have no values ranges recorded (ie, VRP is not running), then
665 return NULL. Otherwise create an empty range if none existed for VAR. */
667 static value_range *
668 get_value_range (const_tree var)
670 static const value_range vr_const_varying
671 = { VR_VARYING, NULL_TREE, NULL_TREE, NULL };
672 value_range *vr;
673 tree sym;
674 unsigned ver = SSA_NAME_VERSION (var);
676 /* If we have no recorded ranges, then return NULL. */
677 if (! vr_value)
678 return NULL;
680 /* If we query the range for a new SSA name return an unmodifiable VARYING.
681 We should get here at most from the substitute-and-fold stage which
682 will never try to change values. */
683 if (ver >= num_vr_values)
684 return CONST_CAST (value_range *, &vr_const_varying);
686 vr = vr_value[ver];
687 if (vr)
688 return vr;
690 /* After propagation finished do not allocate new value-ranges. */
691 if (values_propagated)
692 return CONST_CAST (value_range *, &vr_const_varying);
694 /* Create a default value range. */
695 vr_value[ver] = vr = XCNEW (value_range);
697 /* Defer allocating the equivalence set. */
698 vr->equiv = NULL;
700 /* If VAR is a default definition of a parameter, the variable can
701 take any value in VAR's type. */
702 if (SSA_NAME_IS_DEFAULT_DEF (var))
704 sym = SSA_NAME_VAR (var);
705 if (TREE_CODE (sym) == PARM_DECL)
707 /* Try to use the "nonnull" attribute to create ~[0, 0]
708 anti-ranges for pointers. Note that this is only valid with
709 default definitions of PARM_DECLs. */
710 if (POINTER_TYPE_P (TREE_TYPE (sym))
711 && nonnull_arg_p (sym))
712 set_value_range_to_nonnull (vr, TREE_TYPE (sym));
713 else
714 set_value_range_to_varying (vr);
716 else if (TREE_CODE (sym) == RESULT_DECL
717 && DECL_BY_REFERENCE (sym))
718 set_value_range_to_nonnull (vr, TREE_TYPE (sym));
721 return vr;
724 /* Return true, if VAL1 and VAL2 are equal values for VRP purposes. */
726 static inline bool
727 vrp_operand_equal_p (const_tree val1, const_tree val2)
729 if (val1 == val2)
730 return true;
731 if (!val1 || !val2 || !operand_equal_p (val1, val2, 0))
732 return false;
733 return is_overflow_infinity (val1) == is_overflow_infinity (val2);
736 /* Return true, if the bitmaps B1 and B2 are equal. */
738 static inline bool
739 vrp_bitmap_equal_p (const_bitmap b1, const_bitmap b2)
741 return (b1 == b2
742 || ((!b1 || bitmap_empty_p (b1))
743 && (!b2 || bitmap_empty_p (b2)))
744 || (b1 && b2
745 && bitmap_equal_p (b1, b2)));
748 /* Update the value range and equivalence set for variable VAR to
749 NEW_VR. Return true if NEW_VR is different from VAR's previous
750 value.
752 NOTE: This function assumes that NEW_VR is a temporary value range
753 object created for the sole purpose of updating VAR's range. The
754 storage used by the equivalence set from NEW_VR will be freed by
755 this function. Do not call update_value_range when NEW_VR
756 is the range object associated with another SSA name. */
758 static inline bool
759 update_value_range (const_tree var, value_range *new_vr)
761 value_range *old_vr;
762 bool is_new;
764 /* If there is a value-range on the SSA name from earlier analysis
765 factor that in. */
766 if (INTEGRAL_TYPE_P (TREE_TYPE (var)))
768 wide_int min, max;
769 value_range_type rtype = get_range_info (var, &min, &max);
770 if (rtype == VR_RANGE || rtype == VR_ANTI_RANGE)
772 value_range nr;
773 nr.type = rtype;
774 nr.min = wide_int_to_tree (TREE_TYPE (var), min);
775 nr.max = wide_int_to_tree (TREE_TYPE (var), max);
776 nr.equiv = NULL;
777 vrp_intersect_ranges (new_vr, &nr);
781 /* Update the value range, if necessary. */
782 old_vr = get_value_range (var);
783 is_new = old_vr->type != new_vr->type
784 || !vrp_operand_equal_p (old_vr->min, new_vr->min)
785 || !vrp_operand_equal_p (old_vr->max, new_vr->max)
786 || !vrp_bitmap_equal_p (old_vr->equiv, new_vr->equiv);
788 if (is_new)
790 /* Do not allow transitions up the lattice. The following
791 is slightly more awkward than just new_vr->type < old_vr->type
792 because VR_RANGE and VR_ANTI_RANGE need to be considered
793 the same. We may not have is_new when transitioning to
794 UNDEFINED. If old_vr->type is VARYING, we shouldn't be
795 called. */
796 if (new_vr->type == VR_UNDEFINED)
798 BITMAP_FREE (new_vr->equiv);
799 set_value_range_to_varying (old_vr);
800 set_value_range_to_varying (new_vr);
801 return true;
803 else
804 set_value_range (old_vr, new_vr->type, new_vr->min, new_vr->max,
805 new_vr->equiv);
808 BITMAP_FREE (new_vr->equiv);
810 return is_new;
814 /* Add VAR and VAR's equivalence set to EQUIV. This is the central
815 point where equivalence processing can be turned on/off. */
817 static void
818 add_equivalence (bitmap *equiv, const_tree var)
820 unsigned ver = SSA_NAME_VERSION (var);
821 value_range *vr = vr_value[ver];
823 if (*equiv == NULL)
824 *equiv = BITMAP_ALLOC (NULL);
825 bitmap_set_bit (*equiv, ver);
826 if (vr && vr->equiv)
827 bitmap_ior_into (*equiv, vr->equiv);
831 /* Return true if VR is ~[0, 0]. */
833 static inline bool
834 range_is_nonnull (value_range *vr)
836 return vr->type == VR_ANTI_RANGE
837 && integer_zerop (vr->min)
838 && integer_zerop (vr->max);
842 /* Return true if VR is [0, 0]. */
844 static inline bool
845 range_is_null (value_range *vr)
847 return vr->type == VR_RANGE
848 && integer_zerop (vr->min)
849 && integer_zerop (vr->max);
852 /* Return true if max and min of VR are INTEGER_CST. It's not necessary
853 a singleton. */
855 static inline bool
856 range_int_cst_p (value_range *vr)
858 return (vr->type == VR_RANGE
859 && TREE_CODE (vr->max) == INTEGER_CST
860 && TREE_CODE (vr->min) == INTEGER_CST);
863 /* Return true if VR is a INTEGER_CST singleton. */
865 static inline bool
866 range_int_cst_singleton_p (value_range *vr)
868 return (range_int_cst_p (vr)
869 && !is_overflow_infinity (vr->min)
870 && !is_overflow_infinity (vr->max)
871 && tree_int_cst_equal (vr->min, vr->max));
874 /* Return true if value range VR involves at least one symbol. */
876 static inline bool
877 symbolic_range_p (value_range *vr)
879 return (!is_gimple_min_invariant (vr->min)
880 || !is_gimple_min_invariant (vr->max));
883 /* Return the single symbol (an SSA_NAME) contained in T if any, or NULL_TREE
884 otherwise. We only handle additive operations and set NEG to true if the
885 symbol is negated and INV to the invariant part, if any. */
887 static tree
888 get_single_symbol (tree t, bool *neg, tree *inv)
890 bool neg_;
891 tree inv_;
893 if (TREE_CODE (t) == PLUS_EXPR
894 || TREE_CODE (t) == POINTER_PLUS_EXPR
895 || TREE_CODE (t) == MINUS_EXPR)
897 if (is_gimple_min_invariant (TREE_OPERAND (t, 0)))
899 neg_ = (TREE_CODE (t) == MINUS_EXPR);
900 inv_ = TREE_OPERAND (t, 0);
901 t = TREE_OPERAND (t, 1);
903 else if (is_gimple_min_invariant (TREE_OPERAND (t, 1)))
905 neg_ = false;
906 inv_ = TREE_OPERAND (t, 1);
907 t = TREE_OPERAND (t, 0);
909 else
910 return NULL_TREE;
912 else
914 neg_ = false;
915 inv_ = NULL_TREE;
918 if (TREE_CODE (t) == NEGATE_EXPR)
920 t = TREE_OPERAND (t, 0);
921 neg_ = !neg_;
924 if (TREE_CODE (t) != SSA_NAME)
925 return NULL_TREE;
927 *neg = neg_;
928 *inv = inv_;
929 return t;
932 /* The reverse operation: build a symbolic expression with TYPE
933 from symbol SYM, negated according to NEG, and invariant INV. */
935 static tree
936 build_symbolic_expr (tree type, tree sym, bool neg, tree inv)
938 const bool pointer_p = POINTER_TYPE_P (type);
939 tree t = sym;
941 if (neg)
942 t = build1 (NEGATE_EXPR, type, t);
944 if (integer_zerop (inv))
945 return t;
947 return build2 (pointer_p ? POINTER_PLUS_EXPR : PLUS_EXPR, type, t, inv);
950 /* Return true if value range VR involves exactly one symbol SYM. */
952 static bool
953 symbolic_range_based_on_p (value_range *vr, const_tree sym)
955 bool neg, min_has_symbol, max_has_symbol;
956 tree inv;
958 if (is_gimple_min_invariant (vr->min))
959 min_has_symbol = false;
960 else if (get_single_symbol (vr->min, &neg, &inv) == sym)
961 min_has_symbol = true;
962 else
963 return false;
965 if (is_gimple_min_invariant (vr->max))
966 max_has_symbol = false;
967 else if (get_single_symbol (vr->max, &neg, &inv) == sym)
968 max_has_symbol = true;
969 else
970 return false;
972 return (min_has_symbol || max_has_symbol);
975 /* Return true if value range VR uses an overflow infinity. */
977 static inline bool
978 overflow_infinity_range_p (value_range *vr)
980 return (vr->type == VR_RANGE
981 && (is_overflow_infinity (vr->min)
982 || is_overflow_infinity (vr->max)));
985 /* Return false if we can not make a valid comparison based on VR;
986 this will be the case if it uses an overflow infinity and overflow
987 is not undefined (i.e., -fno-strict-overflow is in effect).
988 Otherwise return true, and set *STRICT_OVERFLOW_P to true if VR
989 uses an overflow infinity. */
991 static bool
992 usable_range_p (value_range *vr, bool *strict_overflow_p)
994 gcc_assert (vr->type == VR_RANGE);
995 if (is_overflow_infinity (vr->min))
997 *strict_overflow_p = true;
998 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (vr->min)))
999 return false;
1001 if (is_overflow_infinity (vr->max))
1003 *strict_overflow_p = true;
1004 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (vr->max)))
1005 return false;
1007 return true;
1011 /* Return true if the result of assignment STMT is know to be non-negative.
1012 If the return value is based on the assumption that signed overflow is
1013 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1014 *STRICT_OVERFLOW_P.*/
1016 static bool
1017 gimple_assign_nonnegative_warnv_p (gimple *stmt, bool *strict_overflow_p)
1019 enum tree_code code = gimple_assign_rhs_code (stmt);
1020 switch (get_gimple_rhs_class (code))
1022 case GIMPLE_UNARY_RHS:
1023 return tree_unary_nonnegative_warnv_p (gimple_assign_rhs_code (stmt),
1024 gimple_expr_type (stmt),
1025 gimple_assign_rhs1 (stmt),
1026 strict_overflow_p);
1027 case GIMPLE_BINARY_RHS:
1028 return tree_binary_nonnegative_warnv_p (gimple_assign_rhs_code (stmt),
1029 gimple_expr_type (stmt),
1030 gimple_assign_rhs1 (stmt),
1031 gimple_assign_rhs2 (stmt),
1032 strict_overflow_p);
1033 case GIMPLE_TERNARY_RHS:
1034 return false;
1035 case GIMPLE_SINGLE_RHS:
1036 return tree_single_nonnegative_warnv_p (gimple_assign_rhs1 (stmt),
1037 strict_overflow_p);
1038 case GIMPLE_INVALID_RHS:
1039 gcc_unreachable ();
1040 default:
1041 gcc_unreachable ();
1045 /* Return true if return value of call STMT is know to be non-negative.
1046 If the return value is based on the assumption that signed overflow is
1047 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1048 *STRICT_OVERFLOW_P.*/
1050 static bool
1051 gimple_call_nonnegative_warnv_p (gimple *stmt, bool *strict_overflow_p)
1053 tree arg0 = gimple_call_num_args (stmt) > 0 ?
1054 gimple_call_arg (stmt, 0) : NULL_TREE;
1055 tree arg1 = gimple_call_num_args (stmt) > 1 ?
1056 gimple_call_arg (stmt, 1) : NULL_TREE;
1058 return tree_call_nonnegative_warnv_p (gimple_expr_type (stmt),
1059 gimple_call_fndecl (stmt),
1060 arg0,
1061 arg1,
1062 strict_overflow_p);
1065 /* Return true if STMT is know to compute a non-negative value.
1066 If the return value is based on the assumption that signed overflow is
1067 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1068 *STRICT_OVERFLOW_P.*/
1070 static bool
1071 gimple_stmt_nonnegative_warnv_p (gimple *stmt, bool *strict_overflow_p)
1073 switch (gimple_code (stmt))
1075 case GIMPLE_ASSIGN:
1076 return gimple_assign_nonnegative_warnv_p (stmt, strict_overflow_p);
1077 case GIMPLE_CALL:
1078 return gimple_call_nonnegative_warnv_p (stmt, strict_overflow_p);
1079 default:
1080 gcc_unreachable ();
1084 /* Return true if the result of assignment STMT is know to be non-zero.
1085 If the return value is based on the assumption that signed overflow is
1086 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1087 *STRICT_OVERFLOW_P.*/
1089 static bool
1090 gimple_assign_nonzero_warnv_p (gimple *stmt, bool *strict_overflow_p)
1092 enum tree_code code = gimple_assign_rhs_code (stmt);
1093 switch (get_gimple_rhs_class (code))
1095 case GIMPLE_UNARY_RHS:
1096 return tree_unary_nonzero_warnv_p (gimple_assign_rhs_code (stmt),
1097 gimple_expr_type (stmt),
1098 gimple_assign_rhs1 (stmt),
1099 strict_overflow_p);
1100 case GIMPLE_BINARY_RHS:
1101 return tree_binary_nonzero_warnv_p (gimple_assign_rhs_code (stmt),
1102 gimple_expr_type (stmt),
1103 gimple_assign_rhs1 (stmt),
1104 gimple_assign_rhs2 (stmt),
1105 strict_overflow_p);
1106 case GIMPLE_TERNARY_RHS:
1107 return false;
1108 case GIMPLE_SINGLE_RHS:
1109 return tree_single_nonzero_warnv_p (gimple_assign_rhs1 (stmt),
1110 strict_overflow_p);
1111 case GIMPLE_INVALID_RHS:
1112 gcc_unreachable ();
1113 default:
1114 gcc_unreachable ();
1118 /* Return true if STMT is known to compute a non-zero value.
1119 If the return value is based on the assumption that signed overflow is
1120 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1121 *STRICT_OVERFLOW_P.*/
1123 static bool
1124 gimple_stmt_nonzero_warnv_p (gimple *stmt, bool *strict_overflow_p)
1126 switch (gimple_code (stmt))
1128 case GIMPLE_ASSIGN:
1129 return gimple_assign_nonzero_warnv_p (stmt, strict_overflow_p);
1130 case GIMPLE_CALL:
1132 tree fndecl = gimple_call_fndecl (stmt);
1133 if (!fndecl) return false;
1134 if (flag_delete_null_pointer_checks && !flag_check_new
1135 && DECL_IS_OPERATOR_NEW (fndecl)
1136 && !TREE_NOTHROW (fndecl))
1137 return true;
1138 /* References are always non-NULL. */
1139 if (flag_delete_null_pointer_checks
1140 && TREE_CODE (TREE_TYPE (fndecl)) == REFERENCE_TYPE)
1141 return true;
1142 if (flag_delete_null_pointer_checks &&
1143 lookup_attribute ("returns_nonnull",
1144 TYPE_ATTRIBUTES (gimple_call_fntype (stmt))))
1145 return true;
1146 return gimple_alloca_call_p (stmt);
1148 default:
1149 gcc_unreachable ();
1153 /* Like tree_expr_nonzero_warnv_p, but this function uses value ranges
1154 obtained so far. */
1156 static bool
1157 vrp_stmt_computes_nonzero (gimple *stmt, bool *strict_overflow_p)
1159 if (gimple_stmt_nonzero_warnv_p (stmt, strict_overflow_p))
1160 return true;
1162 /* If we have an expression of the form &X->a, then the expression
1163 is nonnull if X is nonnull. */
1164 if (is_gimple_assign (stmt)
1165 && gimple_assign_rhs_code (stmt) == ADDR_EXPR)
1167 tree expr = gimple_assign_rhs1 (stmt);
1168 tree base = get_base_address (TREE_OPERAND (expr, 0));
1170 if (base != NULL_TREE
1171 && TREE_CODE (base) == MEM_REF
1172 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
1174 value_range *vr = get_value_range (TREE_OPERAND (base, 0));
1175 if (range_is_nonnull (vr))
1176 return true;
1180 return false;
1183 /* Returns true if EXPR is a valid value (as expected by compare_values) --
1184 a gimple invariant, or SSA_NAME +- CST. */
1186 static bool
1187 valid_value_p (tree expr)
1189 if (TREE_CODE (expr) == SSA_NAME)
1190 return true;
1192 if (TREE_CODE (expr) == PLUS_EXPR
1193 || TREE_CODE (expr) == MINUS_EXPR)
1194 return (TREE_CODE (TREE_OPERAND (expr, 0)) == SSA_NAME
1195 && TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST);
1197 return is_gimple_min_invariant (expr);
1200 /* Return
1201 1 if VAL < VAL2
1202 0 if !(VAL < VAL2)
1203 -2 if those are incomparable. */
1204 static inline int
1205 operand_less_p (tree val, tree val2)
1207 /* LT is folded faster than GE and others. Inline the common case. */
1208 if (TREE_CODE (val) == INTEGER_CST && TREE_CODE (val2) == INTEGER_CST)
1209 return tree_int_cst_lt (val, val2);
1210 else
1212 tree tcmp;
1214 fold_defer_overflow_warnings ();
1216 tcmp = fold_binary_to_constant (LT_EXPR, boolean_type_node, val, val2);
1218 fold_undefer_and_ignore_overflow_warnings ();
1220 if (!tcmp
1221 || TREE_CODE (tcmp) != INTEGER_CST)
1222 return -2;
1224 if (!integer_zerop (tcmp))
1225 return 1;
1228 /* val >= val2, not considering overflow infinity. */
1229 if (is_negative_overflow_infinity (val))
1230 return is_negative_overflow_infinity (val2) ? 0 : 1;
1231 else if (is_positive_overflow_infinity (val2))
1232 return is_positive_overflow_infinity (val) ? 0 : 1;
1234 return 0;
1237 /* Compare two values VAL1 and VAL2. Return
1239 -2 if VAL1 and VAL2 cannot be compared at compile-time,
1240 -1 if VAL1 < VAL2,
1241 0 if VAL1 == VAL2,
1242 +1 if VAL1 > VAL2, and
1243 +2 if VAL1 != VAL2
1245 This is similar to tree_int_cst_compare but supports pointer values
1246 and values that cannot be compared at compile time.
1248 If STRICT_OVERFLOW_P is not NULL, then set *STRICT_OVERFLOW_P to
1249 true if the return value is only valid if we assume that signed
1250 overflow is undefined. */
1252 static int
1253 compare_values_warnv (tree val1, tree val2, bool *strict_overflow_p)
1255 if (val1 == val2)
1256 return 0;
1258 /* Below we rely on the fact that VAL1 and VAL2 are both pointers or
1259 both integers. */
1260 gcc_assert (POINTER_TYPE_P (TREE_TYPE (val1))
1261 == POINTER_TYPE_P (TREE_TYPE (val2)));
1263 /* Convert the two values into the same type. This is needed because
1264 sizetype causes sign extension even for unsigned types. */
1265 val2 = fold_convert (TREE_TYPE (val1), val2);
1266 STRIP_USELESS_TYPE_CONVERSION (val2);
1268 if ((TREE_CODE (val1) == SSA_NAME
1269 || (TREE_CODE (val1) == NEGATE_EXPR
1270 && TREE_CODE (TREE_OPERAND (val1, 0)) == SSA_NAME)
1271 || TREE_CODE (val1) == PLUS_EXPR
1272 || TREE_CODE (val1) == MINUS_EXPR)
1273 && (TREE_CODE (val2) == SSA_NAME
1274 || (TREE_CODE (val2) == NEGATE_EXPR
1275 && TREE_CODE (TREE_OPERAND (val2, 0)) == SSA_NAME)
1276 || TREE_CODE (val2) == PLUS_EXPR
1277 || TREE_CODE (val2) == MINUS_EXPR))
1279 tree n1, c1, n2, c2;
1280 enum tree_code code1, code2;
1282 /* If VAL1 and VAL2 are of the form '[-]NAME [+-] CST' or 'NAME',
1283 return -1 or +1 accordingly. If VAL1 and VAL2 don't use the
1284 same name, return -2. */
1285 if (TREE_CODE (val1) == SSA_NAME || TREE_CODE (val1) == NEGATE_EXPR)
1287 code1 = SSA_NAME;
1288 n1 = val1;
1289 c1 = NULL_TREE;
1291 else
1293 code1 = TREE_CODE (val1);
1294 n1 = TREE_OPERAND (val1, 0);
1295 c1 = TREE_OPERAND (val1, 1);
1296 if (tree_int_cst_sgn (c1) == -1)
1298 if (is_negative_overflow_infinity (c1))
1299 return -2;
1300 c1 = fold_unary_to_constant (NEGATE_EXPR, TREE_TYPE (c1), c1);
1301 if (!c1)
1302 return -2;
1303 code1 = code1 == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR;
1307 if (TREE_CODE (val2) == SSA_NAME || TREE_CODE (val2) == NEGATE_EXPR)
1309 code2 = SSA_NAME;
1310 n2 = val2;
1311 c2 = NULL_TREE;
1313 else
1315 code2 = TREE_CODE (val2);
1316 n2 = TREE_OPERAND (val2, 0);
1317 c2 = TREE_OPERAND (val2, 1);
1318 if (tree_int_cst_sgn (c2) == -1)
1320 if (is_negative_overflow_infinity (c2))
1321 return -2;
1322 c2 = fold_unary_to_constant (NEGATE_EXPR, TREE_TYPE (c2), c2);
1323 if (!c2)
1324 return -2;
1325 code2 = code2 == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR;
1329 /* Both values must use the same name. */
1330 if (TREE_CODE (n1) == NEGATE_EXPR && TREE_CODE (n2) == NEGATE_EXPR)
1332 n1 = TREE_OPERAND (n1, 0);
1333 n2 = TREE_OPERAND (n2, 0);
1335 if (n1 != n2)
1336 return -2;
1338 if (code1 == SSA_NAME && code2 == SSA_NAME)
1339 /* NAME == NAME */
1340 return 0;
1342 /* If overflow is defined we cannot simplify more. */
1343 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (val1)))
1344 return -2;
1346 if (strict_overflow_p != NULL
1347 && (code1 == SSA_NAME || !TREE_NO_WARNING (val1))
1348 && (code2 == SSA_NAME || !TREE_NO_WARNING (val2)))
1349 *strict_overflow_p = true;
1351 if (code1 == SSA_NAME)
1353 if (code2 == PLUS_EXPR)
1354 /* NAME < NAME + CST */
1355 return -1;
1356 else if (code2 == MINUS_EXPR)
1357 /* NAME > NAME - CST */
1358 return 1;
1360 else if (code1 == PLUS_EXPR)
1362 if (code2 == SSA_NAME)
1363 /* NAME + CST > NAME */
1364 return 1;
1365 else if (code2 == PLUS_EXPR)
1366 /* NAME + CST1 > NAME + CST2, if CST1 > CST2 */
1367 return compare_values_warnv (c1, c2, strict_overflow_p);
1368 else if (code2 == MINUS_EXPR)
1369 /* NAME + CST1 > NAME - CST2 */
1370 return 1;
1372 else if (code1 == MINUS_EXPR)
1374 if (code2 == SSA_NAME)
1375 /* NAME - CST < NAME */
1376 return -1;
1377 else if (code2 == PLUS_EXPR)
1378 /* NAME - CST1 < NAME + CST2 */
1379 return -1;
1380 else if (code2 == MINUS_EXPR)
1381 /* NAME - CST1 > NAME - CST2, if CST1 < CST2. Notice that
1382 C1 and C2 are swapped in the call to compare_values. */
1383 return compare_values_warnv (c2, c1, strict_overflow_p);
1386 gcc_unreachable ();
1389 /* We cannot compare non-constants. */
1390 if (!is_gimple_min_invariant (val1) || !is_gimple_min_invariant (val2))
1391 return -2;
1393 if (!POINTER_TYPE_P (TREE_TYPE (val1)))
1395 /* We cannot compare overflowed values, except for overflow
1396 infinities. */
1397 if (TREE_OVERFLOW (val1) || TREE_OVERFLOW (val2))
1399 if (strict_overflow_p != NULL)
1400 *strict_overflow_p = true;
1401 if (is_negative_overflow_infinity (val1))
1402 return is_negative_overflow_infinity (val2) ? 0 : -1;
1403 else if (is_negative_overflow_infinity (val2))
1404 return 1;
1405 else if (is_positive_overflow_infinity (val1))
1406 return is_positive_overflow_infinity (val2) ? 0 : 1;
1407 else if (is_positive_overflow_infinity (val2))
1408 return -1;
1409 return -2;
1412 return tree_int_cst_compare (val1, val2);
1414 else
1416 tree t;
1418 /* First see if VAL1 and VAL2 are not the same. */
1419 if (val1 == val2 || operand_equal_p (val1, val2, 0))
1420 return 0;
1422 /* If VAL1 is a lower address than VAL2, return -1. */
1423 if (operand_less_p (val1, val2) == 1)
1424 return -1;
1426 /* If VAL1 is a higher address than VAL2, return +1. */
1427 if (operand_less_p (val2, val1) == 1)
1428 return 1;
1430 /* If VAL1 is different than VAL2, return +2.
1431 For integer constants we either have already returned -1 or 1
1432 or they are equivalent. We still might succeed in proving
1433 something about non-trivial operands. */
1434 if (TREE_CODE (val1) != INTEGER_CST
1435 || TREE_CODE (val2) != INTEGER_CST)
1437 t = fold_binary_to_constant (NE_EXPR, boolean_type_node, val1, val2);
1438 if (t && integer_onep (t))
1439 return 2;
1442 return -2;
1446 /* Compare values like compare_values_warnv, but treat comparisons of
1447 nonconstants which rely on undefined overflow as incomparable. */
1449 static int
1450 compare_values (tree val1, tree val2)
1452 bool sop;
1453 int ret;
1455 sop = false;
1456 ret = compare_values_warnv (val1, val2, &sop);
1457 if (sop
1458 && (!is_gimple_min_invariant (val1) || !is_gimple_min_invariant (val2)))
1459 ret = -2;
1460 return ret;
1464 /* Return 1 if VAL is inside value range MIN <= VAL <= MAX,
1465 0 if VAL is not inside [MIN, MAX],
1466 -2 if we cannot tell either way.
1468 Benchmark compile/20001226-1.c compilation time after changing this
1469 function. */
1471 static inline int
1472 value_inside_range (tree val, tree min, tree max)
1474 int cmp1, cmp2;
1476 cmp1 = operand_less_p (val, min);
1477 if (cmp1 == -2)
1478 return -2;
1479 if (cmp1 == 1)
1480 return 0;
1482 cmp2 = operand_less_p (max, val);
1483 if (cmp2 == -2)
1484 return -2;
1486 return !cmp2;
1490 /* Return true if value ranges VR0 and VR1 have a non-empty
1491 intersection.
1493 Benchmark compile/20001226-1.c compilation time after changing this
1494 function.
1497 static inline bool
1498 value_ranges_intersect_p (value_range *vr0, value_range *vr1)
1500 /* The value ranges do not intersect if the maximum of the first range is
1501 less than the minimum of the second range or vice versa.
1502 When those relations are unknown, we can't do any better. */
1503 if (operand_less_p (vr0->max, vr1->min) != 0)
1504 return false;
1505 if (operand_less_p (vr1->max, vr0->min) != 0)
1506 return false;
1507 return true;
1511 /* Return 1 if [MIN, MAX] includes the value zero, 0 if it does not
1512 include the value zero, -2 if we cannot tell. */
1514 static inline int
1515 range_includes_zero_p (tree min, tree max)
1517 tree zero = build_int_cst (TREE_TYPE (min), 0);
1518 return value_inside_range (zero, min, max);
1521 /* Return true if *VR is know to only contain nonnegative values. */
1523 static inline bool
1524 value_range_nonnegative_p (value_range *vr)
1526 /* Testing for VR_ANTI_RANGE is not useful here as any anti-range
1527 which would return a useful value should be encoded as a
1528 VR_RANGE. */
1529 if (vr->type == VR_RANGE)
1531 int result = compare_values (vr->min, integer_zero_node);
1532 return (result == 0 || result == 1);
1535 return false;
1538 /* If *VR has a value rante that is a single constant value return that,
1539 otherwise return NULL_TREE. */
1541 static tree
1542 value_range_constant_singleton (value_range *vr)
1544 if (vr->type == VR_RANGE
1545 && operand_equal_p (vr->min, vr->max, 0)
1546 && is_gimple_min_invariant (vr->min))
1547 return vr->min;
1549 return NULL_TREE;
1552 /* If OP has a value range with a single constant value return that,
1553 otherwise return NULL_TREE. This returns OP itself if OP is a
1554 constant. */
1556 static tree
1557 op_with_constant_singleton_value_range (tree op)
1559 if (is_gimple_min_invariant (op))
1560 return op;
1562 if (TREE_CODE (op) != SSA_NAME)
1563 return NULL_TREE;
1565 return value_range_constant_singleton (get_value_range (op));
1568 /* Return true if op is in a boolean [0, 1] value-range. */
1570 static bool
1571 op_with_boolean_value_range_p (tree op)
1573 value_range *vr;
1575 if (TYPE_PRECISION (TREE_TYPE (op)) == 1)
1576 return true;
1578 if (integer_zerop (op)
1579 || integer_onep (op))
1580 return true;
1582 if (TREE_CODE (op) != SSA_NAME)
1583 return false;
1585 vr = get_value_range (op);
1586 return (vr->type == VR_RANGE
1587 && integer_zerop (vr->min)
1588 && integer_onep (vr->max));
1591 /* Extract value range information from an ASSERT_EXPR EXPR and store
1592 it in *VR_P. */
1594 static void
1595 extract_range_from_assert (value_range *vr_p, tree expr)
1597 tree var, cond, limit, min, max, type;
1598 value_range *limit_vr;
1599 enum tree_code cond_code;
1601 var = ASSERT_EXPR_VAR (expr);
1602 cond = ASSERT_EXPR_COND (expr);
1604 gcc_assert (COMPARISON_CLASS_P (cond));
1606 /* Find VAR in the ASSERT_EXPR conditional. */
1607 if (var == TREE_OPERAND (cond, 0)
1608 || TREE_CODE (TREE_OPERAND (cond, 0)) == PLUS_EXPR
1609 || TREE_CODE (TREE_OPERAND (cond, 0)) == NOP_EXPR)
1611 /* If the predicate is of the form VAR COMP LIMIT, then we just
1612 take LIMIT from the RHS and use the same comparison code. */
1613 cond_code = TREE_CODE (cond);
1614 limit = TREE_OPERAND (cond, 1);
1615 cond = TREE_OPERAND (cond, 0);
1617 else
1619 /* If the predicate is of the form LIMIT COMP VAR, then we need
1620 to flip around the comparison code to create the proper range
1621 for VAR. */
1622 cond_code = swap_tree_comparison (TREE_CODE (cond));
1623 limit = TREE_OPERAND (cond, 0);
1624 cond = TREE_OPERAND (cond, 1);
1627 limit = avoid_overflow_infinity (limit);
1629 type = TREE_TYPE (var);
1630 gcc_assert (limit != var);
1632 /* For pointer arithmetic, we only keep track of pointer equality
1633 and inequality. */
1634 if (POINTER_TYPE_P (type) && cond_code != NE_EXPR && cond_code != EQ_EXPR)
1636 set_value_range_to_varying (vr_p);
1637 return;
1640 /* If LIMIT is another SSA name and LIMIT has a range of its own,
1641 try to use LIMIT's range to avoid creating symbolic ranges
1642 unnecessarily. */
1643 limit_vr = (TREE_CODE (limit) == SSA_NAME) ? get_value_range (limit) : NULL;
1645 /* LIMIT's range is only interesting if it has any useful information. */
1646 if (limit_vr
1647 && (limit_vr->type == VR_UNDEFINED
1648 || limit_vr->type == VR_VARYING
1649 || symbolic_range_p (limit_vr)))
1650 limit_vr = NULL;
1652 /* Initially, the new range has the same set of equivalences of
1653 VAR's range. This will be revised before returning the final
1654 value. Since assertions may be chained via mutually exclusive
1655 predicates, we will need to trim the set of equivalences before
1656 we are done. */
1657 gcc_assert (vr_p->equiv == NULL);
1658 add_equivalence (&vr_p->equiv, var);
1660 /* Extract a new range based on the asserted comparison for VAR and
1661 LIMIT's value range. Notice that if LIMIT has an anti-range, we
1662 will only use it for equality comparisons (EQ_EXPR). For any
1663 other kind of assertion, we cannot derive a range from LIMIT's
1664 anti-range that can be used to describe the new range. For
1665 instance, ASSERT_EXPR <x_2, x_2 <= b_4>. If b_4 is ~[2, 10],
1666 then b_4 takes on the ranges [-INF, 1] and [11, +INF]. There is
1667 no single range for x_2 that could describe LE_EXPR, so we might
1668 as well build the range [b_4, +INF] for it.
1669 One special case we handle is extracting a range from a
1670 range test encoded as (unsigned)var + CST <= limit. */
1671 if (TREE_CODE (cond) == NOP_EXPR
1672 || TREE_CODE (cond) == PLUS_EXPR)
1674 if (TREE_CODE (cond) == PLUS_EXPR)
1676 min = fold_build1 (NEGATE_EXPR, TREE_TYPE (TREE_OPERAND (cond, 1)),
1677 TREE_OPERAND (cond, 1));
1678 max = int_const_binop (PLUS_EXPR, limit, min);
1679 cond = TREE_OPERAND (cond, 0);
1681 else
1683 min = build_int_cst (TREE_TYPE (var), 0);
1684 max = limit;
1687 /* Make sure to not set TREE_OVERFLOW on the final type
1688 conversion. We are willingly interpreting large positive
1689 unsigned values as negative signed values here. */
1690 min = force_fit_type (TREE_TYPE (var), wi::to_widest (min), 0, false);
1691 max = force_fit_type (TREE_TYPE (var), wi::to_widest (max), 0, false);
1693 /* We can transform a max, min range to an anti-range or
1694 vice-versa. Use set_and_canonicalize_value_range which does
1695 this for us. */
1696 if (cond_code == LE_EXPR)
1697 set_and_canonicalize_value_range (vr_p, VR_RANGE,
1698 min, max, vr_p->equiv);
1699 else if (cond_code == GT_EXPR)
1700 set_and_canonicalize_value_range (vr_p, VR_ANTI_RANGE,
1701 min, max, vr_p->equiv);
1702 else
1703 gcc_unreachable ();
1705 else if (cond_code == EQ_EXPR)
1707 enum value_range_type range_type;
1709 if (limit_vr)
1711 range_type = limit_vr->type;
1712 min = limit_vr->min;
1713 max = limit_vr->max;
1715 else
1717 range_type = VR_RANGE;
1718 min = limit;
1719 max = limit;
1722 set_value_range (vr_p, range_type, min, max, vr_p->equiv);
1724 /* When asserting the equality VAR == LIMIT and LIMIT is another
1725 SSA name, the new range will also inherit the equivalence set
1726 from LIMIT. */
1727 if (TREE_CODE (limit) == SSA_NAME)
1728 add_equivalence (&vr_p->equiv, limit);
1730 else if (cond_code == NE_EXPR)
1732 /* As described above, when LIMIT's range is an anti-range and
1733 this assertion is an inequality (NE_EXPR), then we cannot
1734 derive anything from the anti-range. For instance, if
1735 LIMIT's range was ~[0, 0], the assertion 'VAR != LIMIT' does
1736 not imply that VAR's range is [0, 0]. So, in the case of
1737 anti-ranges, we just assert the inequality using LIMIT and
1738 not its anti-range.
1740 If LIMIT_VR is a range, we can only use it to build a new
1741 anti-range if LIMIT_VR is a single-valued range. For
1742 instance, if LIMIT_VR is [0, 1], the predicate
1743 VAR != [0, 1] does not mean that VAR's range is ~[0, 1].
1744 Rather, it means that for value 0 VAR should be ~[0, 0]
1745 and for value 1, VAR should be ~[1, 1]. We cannot
1746 represent these ranges.
1748 The only situation in which we can build a valid
1749 anti-range is when LIMIT_VR is a single-valued range
1750 (i.e., LIMIT_VR->MIN == LIMIT_VR->MAX). In that case,
1751 build the anti-range ~[LIMIT_VR->MIN, LIMIT_VR->MAX]. */
1752 if (limit_vr
1753 && limit_vr->type == VR_RANGE
1754 && compare_values (limit_vr->min, limit_vr->max) == 0)
1756 min = limit_vr->min;
1757 max = limit_vr->max;
1759 else
1761 /* In any other case, we cannot use LIMIT's range to build a
1762 valid anti-range. */
1763 min = max = limit;
1766 /* If MIN and MAX cover the whole range for their type, then
1767 just use the original LIMIT. */
1768 if (INTEGRAL_TYPE_P (type)
1769 && vrp_val_is_min (min)
1770 && vrp_val_is_max (max))
1771 min = max = limit;
1773 set_and_canonicalize_value_range (vr_p, VR_ANTI_RANGE,
1774 min, max, vr_p->equiv);
1776 else if (cond_code == LE_EXPR || cond_code == LT_EXPR)
1778 min = TYPE_MIN_VALUE (type);
1780 if (limit_vr == NULL || limit_vr->type == VR_ANTI_RANGE)
1781 max = limit;
1782 else
1784 /* If LIMIT_VR is of the form [N1, N2], we need to build the
1785 range [MIN, N2] for LE_EXPR and [MIN, N2 - 1] for
1786 LT_EXPR. */
1787 max = limit_vr->max;
1790 /* If the maximum value forces us to be out of bounds, simply punt.
1791 It would be pointless to try and do anything more since this
1792 all should be optimized away above us. */
1793 if ((cond_code == LT_EXPR
1794 && compare_values (max, min) == 0)
1795 || is_overflow_infinity (max))
1796 set_value_range_to_varying (vr_p);
1797 else
1799 /* For LT_EXPR, we create the range [MIN, MAX - 1]. */
1800 if (cond_code == LT_EXPR)
1802 if (TYPE_PRECISION (TREE_TYPE (max)) == 1
1803 && !TYPE_UNSIGNED (TREE_TYPE (max)))
1804 max = fold_build2 (PLUS_EXPR, TREE_TYPE (max), max,
1805 build_int_cst (TREE_TYPE (max), -1));
1806 else
1807 max = fold_build2 (MINUS_EXPR, TREE_TYPE (max), max,
1808 build_int_cst (TREE_TYPE (max), 1));
1809 if (EXPR_P (max))
1810 TREE_NO_WARNING (max) = 1;
1813 set_value_range (vr_p, VR_RANGE, min, max, vr_p->equiv);
1816 else if (cond_code == GE_EXPR || cond_code == GT_EXPR)
1818 max = TYPE_MAX_VALUE (type);
1820 if (limit_vr == NULL || limit_vr->type == VR_ANTI_RANGE)
1821 min = limit;
1822 else
1824 /* If LIMIT_VR is of the form [N1, N2], we need to build the
1825 range [N1, MAX] for GE_EXPR and [N1 + 1, MAX] for
1826 GT_EXPR. */
1827 min = limit_vr->min;
1830 /* If the minimum value forces us to be out of bounds, simply punt.
1831 It would be pointless to try and do anything more since this
1832 all should be optimized away above us. */
1833 if ((cond_code == GT_EXPR
1834 && compare_values (min, max) == 0)
1835 || is_overflow_infinity (min))
1836 set_value_range_to_varying (vr_p);
1837 else
1839 /* For GT_EXPR, we create the range [MIN + 1, MAX]. */
1840 if (cond_code == GT_EXPR)
1842 if (TYPE_PRECISION (TREE_TYPE (min)) == 1
1843 && !TYPE_UNSIGNED (TREE_TYPE (min)))
1844 min = fold_build2 (MINUS_EXPR, TREE_TYPE (min), min,
1845 build_int_cst (TREE_TYPE (min), -1));
1846 else
1847 min = fold_build2 (PLUS_EXPR, TREE_TYPE (min), min,
1848 build_int_cst (TREE_TYPE (min), 1));
1849 if (EXPR_P (min))
1850 TREE_NO_WARNING (min) = 1;
1853 set_value_range (vr_p, VR_RANGE, min, max, vr_p->equiv);
1856 else
1857 gcc_unreachable ();
1859 /* Finally intersect the new range with what we already know about var. */
1860 vrp_intersect_ranges (vr_p, get_value_range (var));
1864 /* Extract range information from SSA name VAR and store it in VR. If
1865 VAR has an interesting range, use it. Otherwise, create the
1866 range [VAR, VAR] and return it. This is useful in situations where
1867 we may have conditionals testing values of VARYING names. For
1868 instance,
1870 x_3 = y_5;
1871 if (x_3 > y_5)
1874 Even if y_5 is deemed VARYING, we can determine that x_3 > y_5 is
1875 always false. */
1877 static void
1878 extract_range_from_ssa_name (value_range *vr, tree var)
1880 value_range *var_vr = get_value_range (var);
1882 if (var_vr->type != VR_VARYING)
1883 copy_value_range (vr, var_vr);
1884 else
1885 set_value_range (vr, VR_RANGE, var, var, NULL);
1887 add_equivalence (&vr->equiv, var);
1891 /* Wrapper around int_const_binop. If the operation overflows and we
1892 are not using wrapping arithmetic, then adjust the result to be
1893 -INF or +INF depending on CODE, VAL1 and VAL2. This can return
1894 NULL_TREE if we need to use an overflow infinity representation but
1895 the type does not support it. */
1897 static tree
1898 vrp_int_const_binop (enum tree_code code, tree val1, tree val2)
1900 tree res;
1902 res = int_const_binop (code, val1, val2);
1904 /* If we are using unsigned arithmetic, operate symbolically
1905 on -INF and +INF as int_const_binop only handles signed overflow. */
1906 if (TYPE_UNSIGNED (TREE_TYPE (val1)))
1908 int checkz = compare_values (res, val1);
1909 bool overflow = false;
1911 /* Ensure that res = val1 [+*] val2 >= val1
1912 or that res = val1 - val2 <= val1. */
1913 if ((code == PLUS_EXPR
1914 && !(checkz == 1 || checkz == 0))
1915 || (code == MINUS_EXPR
1916 && !(checkz == 0 || checkz == -1)))
1918 overflow = true;
1920 /* Checking for multiplication overflow is done by dividing the
1921 output of the multiplication by the first input of the
1922 multiplication. If the result of that division operation is
1923 not equal to the second input of the multiplication, then the
1924 multiplication overflowed. */
1925 else if (code == MULT_EXPR && !integer_zerop (val1))
1927 tree tmp = int_const_binop (TRUNC_DIV_EXPR,
1928 res,
1929 val1);
1930 int check = compare_values (tmp, val2);
1932 if (check != 0)
1933 overflow = true;
1936 if (overflow)
1938 res = copy_node (res);
1939 TREE_OVERFLOW (res) = 1;
1943 else if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (val1)))
1944 /* If the singed operation wraps then int_const_binop has done
1945 everything we want. */
1947 /* Signed division of -1/0 overflows and by the time it gets here
1948 returns NULL_TREE. */
1949 else if (!res)
1950 return NULL_TREE;
1951 else if ((TREE_OVERFLOW (res)
1952 && !TREE_OVERFLOW (val1)
1953 && !TREE_OVERFLOW (val2))
1954 || is_overflow_infinity (val1)
1955 || is_overflow_infinity (val2))
1957 /* If the operation overflowed but neither VAL1 nor VAL2 are
1958 overflown, return -INF or +INF depending on the operation
1959 and the combination of signs of the operands. */
1960 int sgn1 = tree_int_cst_sgn (val1);
1961 int sgn2 = tree_int_cst_sgn (val2);
1963 if (needs_overflow_infinity (TREE_TYPE (res))
1964 && !supports_overflow_infinity (TREE_TYPE (res)))
1965 return NULL_TREE;
1967 /* We have to punt on adding infinities of different signs,
1968 since we can't tell what the sign of the result should be.
1969 Likewise for subtracting infinities of the same sign. */
1970 if (((code == PLUS_EXPR && sgn1 != sgn2)
1971 || (code == MINUS_EXPR && sgn1 == sgn2))
1972 && is_overflow_infinity (val1)
1973 && is_overflow_infinity (val2))
1974 return NULL_TREE;
1976 /* Don't try to handle division or shifting of infinities. */
1977 if ((code == TRUNC_DIV_EXPR
1978 || code == FLOOR_DIV_EXPR
1979 || code == CEIL_DIV_EXPR
1980 || code == EXACT_DIV_EXPR
1981 || code == ROUND_DIV_EXPR
1982 || code == RSHIFT_EXPR)
1983 && (is_overflow_infinity (val1)
1984 || is_overflow_infinity (val2)))
1985 return NULL_TREE;
1987 /* Notice that we only need to handle the restricted set of
1988 operations handled by extract_range_from_binary_expr.
1989 Among them, only multiplication, addition and subtraction
1990 can yield overflow without overflown operands because we
1991 are working with integral types only... except in the
1992 case VAL1 = -INF and VAL2 = -1 which overflows to +INF
1993 for division too. */
1995 /* For multiplication, the sign of the overflow is given
1996 by the comparison of the signs of the operands. */
1997 if ((code == MULT_EXPR && sgn1 == sgn2)
1998 /* For addition, the operands must be of the same sign
1999 to yield an overflow. Its sign is therefore that
2000 of one of the operands, for example the first. For
2001 infinite operands X + -INF is negative, not positive. */
2002 || (code == PLUS_EXPR
2003 && (sgn1 >= 0
2004 ? !is_negative_overflow_infinity (val2)
2005 : is_positive_overflow_infinity (val2)))
2006 /* For subtraction, non-infinite operands must be of
2007 different signs to yield an overflow. Its sign is
2008 therefore that of the first operand or the opposite of
2009 that of the second operand. A first operand of 0 counts
2010 as positive here, for the corner case 0 - (-INF), which
2011 overflows, but must yield +INF. For infinite operands 0
2012 - INF is negative, not positive. */
2013 || (code == MINUS_EXPR
2014 && (sgn1 >= 0
2015 ? !is_positive_overflow_infinity (val2)
2016 : is_negative_overflow_infinity (val2)))
2017 /* We only get in here with positive shift count, so the
2018 overflow direction is the same as the sign of val1.
2019 Actually rshift does not overflow at all, but we only
2020 handle the case of shifting overflowed -INF and +INF. */
2021 || (code == RSHIFT_EXPR
2022 && sgn1 >= 0)
2023 /* For division, the only case is -INF / -1 = +INF. */
2024 || code == TRUNC_DIV_EXPR
2025 || code == FLOOR_DIV_EXPR
2026 || code == CEIL_DIV_EXPR
2027 || code == EXACT_DIV_EXPR
2028 || code == ROUND_DIV_EXPR)
2029 return (needs_overflow_infinity (TREE_TYPE (res))
2030 ? positive_overflow_infinity (TREE_TYPE (res))
2031 : TYPE_MAX_VALUE (TREE_TYPE (res)));
2032 else
2033 return (needs_overflow_infinity (TREE_TYPE (res))
2034 ? negative_overflow_infinity (TREE_TYPE (res))
2035 : TYPE_MIN_VALUE (TREE_TYPE (res)));
2038 return res;
2042 /* For range VR compute two wide_int bitmasks. In *MAY_BE_NONZERO
2043 bitmask if some bit is unset, it means for all numbers in the range
2044 the bit is 0, otherwise it might be 0 or 1. In *MUST_BE_NONZERO
2045 bitmask if some bit is set, it means for all numbers in the range
2046 the bit is 1, otherwise it might be 0 or 1. */
2048 static bool
2049 zero_nonzero_bits_from_vr (const tree expr_type,
2050 value_range *vr,
2051 wide_int *may_be_nonzero,
2052 wide_int *must_be_nonzero)
2054 *may_be_nonzero = wi::minus_one (TYPE_PRECISION (expr_type));
2055 *must_be_nonzero = wi::zero (TYPE_PRECISION (expr_type));
2056 if (!range_int_cst_p (vr)
2057 || is_overflow_infinity (vr->min)
2058 || is_overflow_infinity (vr->max))
2059 return false;
2061 if (range_int_cst_singleton_p (vr))
2063 *may_be_nonzero = vr->min;
2064 *must_be_nonzero = *may_be_nonzero;
2066 else if (tree_int_cst_sgn (vr->min) >= 0
2067 || tree_int_cst_sgn (vr->max) < 0)
2069 wide_int xor_mask = wi::bit_xor (vr->min, vr->max);
2070 *may_be_nonzero = wi::bit_or (vr->min, vr->max);
2071 *must_be_nonzero = wi::bit_and (vr->min, vr->max);
2072 if (xor_mask != 0)
2074 wide_int mask = wi::mask (wi::floor_log2 (xor_mask), false,
2075 may_be_nonzero->get_precision ());
2076 *may_be_nonzero = *may_be_nonzero | mask;
2077 *must_be_nonzero = must_be_nonzero->and_not (mask);
2081 return true;
2084 /* Create two value-ranges in *VR0 and *VR1 from the anti-range *AR
2085 so that *VR0 U *VR1 == *AR. Returns true if that is possible,
2086 false otherwise. If *AR can be represented with a single range
2087 *VR1 will be VR_UNDEFINED. */
2089 static bool
2090 ranges_from_anti_range (value_range *ar,
2091 value_range *vr0, value_range *vr1)
2093 tree type = TREE_TYPE (ar->min);
2095 vr0->type = VR_UNDEFINED;
2096 vr1->type = VR_UNDEFINED;
2098 if (ar->type != VR_ANTI_RANGE
2099 || TREE_CODE (ar->min) != INTEGER_CST
2100 || TREE_CODE (ar->max) != INTEGER_CST
2101 || !vrp_val_min (type)
2102 || !vrp_val_max (type))
2103 return false;
2105 if (!vrp_val_is_min (ar->min))
2107 vr0->type = VR_RANGE;
2108 vr0->min = vrp_val_min (type);
2109 vr0->max = wide_int_to_tree (type, wi::sub (ar->min, 1));
2111 if (!vrp_val_is_max (ar->max))
2113 vr1->type = VR_RANGE;
2114 vr1->min = wide_int_to_tree (type, wi::add (ar->max, 1));
2115 vr1->max = vrp_val_max (type);
2117 if (vr0->type == VR_UNDEFINED)
2119 *vr0 = *vr1;
2120 vr1->type = VR_UNDEFINED;
2123 return vr0->type != VR_UNDEFINED;
2126 /* Helper to extract a value-range *VR for a multiplicative operation
2127 *VR0 CODE *VR1. */
2129 static void
2130 extract_range_from_multiplicative_op_1 (value_range *vr,
2131 enum tree_code code,
2132 value_range *vr0, value_range *vr1)
2134 enum value_range_type type;
2135 tree val[4];
2136 size_t i;
2137 tree min, max;
2138 bool sop;
2139 int cmp;
2141 /* Multiplications, divisions and shifts are a bit tricky to handle,
2142 depending on the mix of signs we have in the two ranges, we
2143 need to operate on different values to get the minimum and
2144 maximum values for the new range. One approach is to figure
2145 out all the variations of range combinations and do the
2146 operations.
2148 However, this involves several calls to compare_values and it
2149 is pretty convoluted. It's simpler to do the 4 operations
2150 (MIN0 OP MIN1, MIN0 OP MAX1, MAX0 OP MIN1 and MAX0 OP MAX0 OP
2151 MAX1) and then figure the smallest and largest values to form
2152 the new range. */
2153 gcc_assert (code == MULT_EXPR
2154 || code == TRUNC_DIV_EXPR
2155 || code == FLOOR_DIV_EXPR
2156 || code == CEIL_DIV_EXPR
2157 || code == EXACT_DIV_EXPR
2158 || code == ROUND_DIV_EXPR
2159 || code == RSHIFT_EXPR
2160 || code == LSHIFT_EXPR);
2161 gcc_assert ((vr0->type == VR_RANGE
2162 || (code == MULT_EXPR && vr0->type == VR_ANTI_RANGE))
2163 && vr0->type == vr1->type);
2165 type = vr0->type;
2167 /* Compute the 4 cross operations. */
2168 sop = false;
2169 val[0] = vrp_int_const_binop (code, vr0->min, vr1->min);
2170 if (val[0] == NULL_TREE)
2171 sop = true;
2173 if (vr1->max == vr1->min)
2174 val[1] = NULL_TREE;
2175 else
2177 val[1] = vrp_int_const_binop (code, vr0->min, vr1->max);
2178 if (val[1] == NULL_TREE)
2179 sop = true;
2182 if (vr0->max == vr0->min)
2183 val[2] = NULL_TREE;
2184 else
2186 val[2] = vrp_int_const_binop (code, vr0->max, vr1->min);
2187 if (val[2] == NULL_TREE)
2188 sop = true;
2191 if (vr0->min == vr0->max || vr1->min == vr1->max)
2192 val[3] = NULL_TREE;
2193 else
2195 val[3] = vrp_int_const_binop (code, vr0->max, vr1->max);
2196 if (val[3] == NULL_TREE)
2197 sop = true;
2200 if (sop)
2202 set_value_range_to_varying (vr);
2203 return;
2206 /* Set MIN to the minimum of VAL[i] and MAX to the maximum
2207 of VAL[i]. */
2208 min = val[0];
2209 max = val[0];
2210 for (i = 1; i < 4; i++)
2212 if (!is_gimple_min_invariant (min)
2213 || (TREE_OVERFLOW (min) && !is_overflow_infinity (min))
2214 || !is_gimple_min_invariant (max)
2215 || (TREE_OVERFLOW (max) && !is_overflow_infinity (max)))
2216 break;
2218 if (val[i])
2220 if (!is_gimple_min_invariant (val[i])
2221 || (TREE_OVERFLOW (val[i])
2222 && !is_overflow_infinity (val[i])))
2224 /* If we found an overflowed value, set MIN and MAX
2225 to it so that we set the resulting range to
2226 VARYING. */
2227 min = max = val[i];
2228 break;
2231 if (compare_values (val[i], min) == -1)
2232 min = val[i];
2234 if (compare_values (val[i], max) == 1)
2235 max = val[i];
2239 /* If either MIN or MAX overflowed, then set the resulting range to
2240 VARYING. But we do accept an overflow infinity
2241 representation. */
2242 if (min == NULL_TREE
2243 || !is_gimple_min_invariant (min)
2244 || (TREE_OVERFLOW (min) && !is_overflow_infinity (min))
2245 || max == NULL_TREE
2246 || !is_gimple_min_invariant (max)
2247 || (TREE_OVERFLOW (max) && !is_overflow_infinity (max)))
2249 set_value_range_to_varying (vr);
2250 return;
2253 /* We punt if:
2254 1) [-INF, +INF]
2255 2) [-INF, +-INF(OVF)]
2256 3) [+-INF(OVF), +INF]
2257 4) [+-INF(OVF), +-INF(OVF)]
2258 We learn nothing when we have INF and INF(OVF) on both sides.
2259 Note that we do accept [-INF, -INF] and [+INF, +INF] without
2260 overflow. */
2261 if ((vrp_val_is_min (min) || is_overflow_infinity (min))
2262 && (vrp_val_is_max (max) || is_overflow_infinity (max)))
2264 set_value_range_to_varying (vr);
2265 return;
2268 cmp = compare_values (min, max);
2269 if (cmp == -2 || cmp == 1)
2271 /* If the new range has its limits swapped around (MIN > MAX),
2272 then the operation caused one of them to wrap around, mark
2273 the new range VARYING. */
2274 set_value_range_to_varying (vr);
2276 else
2277 set_value_range (vr, type, min, max, NULL);
2280 /* Extract range information from a binary operation CODE based on
2281 the ranges of each of its operands *VR0 and *VR1 with resulting
2282 type EXPR_TYPE. The resulting range is stored in *VR. */
2284 static void
2285 extract_range_from_binary_expr_1 (value_range *vr,
2286 enum tree_code code, tree expr_type,
2287 value_range *vr0_, value_range *vr1_)
2289 value_range vr0 = *vr0_, vr1 = *vr1_;
2290 value_range vrtem0 = VR_INITIALIZER, vrtem1 = VR_INITIALIZER;
2291 enum value_range_type type;
2292 tree min = NULL_TREE, max = NULL_TREE;
2293 int cmp;
2295 if (!INTEGRAL_TYPE_P (expr_type)
2296 && !POINTER_TYPE_P (expr_type))
2298 set_value_range_to_varying (vr);
2299 return;
2302 /* Not all binary expressions can be applied to ranges in a
2303 meaningful way. Handle only arithmetic operations. */
2304 if (code != PLUS_EXPR
2305 && code != MINUS_EXPR
2306 && code != POINTER_PLUS_EXPR
2307 && code != MULT_EXPR
2308 && code != TRUNC_DIV_EXPR
2309 && code != FLOOR_DIV_EXPR
2310 && code != CEIL_DIV_EXPR
2311 && code != EXACT_DIV_EXPR
2312 && code != ROUND_DIV_EXPR
2313 && code != TRUNC_MOD_EXPR
2314 && code != RSHIFT_EXPR
2315 && code != LSHIFT_EXPR
2316 && code != MIN_EXPR
2317 && code != MAX_EXPR
2318 && code != BIT_AND_EXPR
2319 && code != BIT_IOR_EXPR
2320 && code != BIT_XOR_EXPR)
2322 set_value_range_to_varying (vr);
2323 return;
2326 /* If both ranges are UNDEFINED, so is the result. */
2327 if (vr0.type == VR_UNDEFINED && vr1.type == VR_UNDEFINED)
2329 set_value_range_to_undefined (vr);
2330 return;
2332 /* If one of the ranges is UNDEFINED drop it to VARYING for the following
2333 code. At some point we may want to special-case operations that
2334 have UNDEFINED result for all or some value-ranges of the not UNDEFINED
2335 operand. */
2336 else if (vr0.type == VR_UNDEFINED)
2337 set_value_range_to_varying (&vr0);
2338 else if (vr1.type == VR_UNDEFINED)
2339 set_value_range_to_varying (&vr1);
2341 /* Now canonicalize anti-ranges to ranges when they are not symbolic
2342 and express ~[] op X as ([]' op X) U ([]'' op X). */
2343 if (vr0.type == VR_ANTI_RANGE
2344 && ranges_from_anti_range (&vr0, &vrtem0, &vrtem1))
2346 extract_range_from_binary_expr_1 (vr, code, expr_type, &vrtem0, vr1_);
2347 if (vrtem1.type != VR_UNDEFINED)
2349 value_range vrres = VR_INITIALIZER;
2350 extract_range_from_binary_expr_1 (&vrres, code, expr_type,
2351 &vrtem1, vr1_);
2352 vrp_meet (vr, &vrres);
2354 return;
2356 /* Likewise for X op ~[]. */
2357 if (vr1.type == VR_ANTI_RANGE
2358 && ranges_from_anti_range (&vr1, &vrtem0, &vrtem1))
2360 extract_range_from_binary_expr_1 (vr, code, expr_type, vr0_, &vrtem0);
2361 if (vrtem1.type != VR_UNDEFINED)
2363 value_range vrres = VR_INITIALIZER;
2364 extract_range_from_binary_expr_1 (&vrres, code, expr_type,
2365 vr0_, &vrtem1);
2366 vrp_meet (vr, &vrres);
2368 return;
2371 /* The type of the resulting value range defaults to VR0.TYPE. */
2372 type = vr0.type;
2374 /* Refuse to operate on VARYING ranges, ranges of different kinds
2375 and symbolic ranges. As an exception, we allow BIT_{AND,IOR}
2376 because we may be able to derive a useful range even if one of
2377 the operands is VR_VARYING or symbolic range. Similarly for
2378 divisions, MIN/MAX and PLUS/MINUS.
2380 TODO, we may be able to derive anti-ranges in some cases. */
2381 if (code != BIT_AND_EXPR
2382 && code != BIT_IOR_EXPR
2383 && code != TRUNC_DIV_EXPR
2384 && code != FLOOR_DIV_EXPR
2385 && code != CEIL_DIV_EXPR
2386 && code != EXACT_DIV_EXPR
2387 && code != ROUND_DIV_EXPR
2388 && code != TRUNC_MOD_EXPR
2389 && code != MIN_EXPR
2390 && code != MAX_EXPR
2391 && code != PLUS_EXPR
2392 && code != MINUS_EXPR
2393 && code != RSHIFT_EXPR
2394 && (vr0.type == VR_VARYING
2395 || vr1.type == VR_VARYING
2396 || vr0.type != vr1.type
2397 || symbolic_range_p (&vr0)
2398 || symbolic_range_p (&vr1)))
2400 set_value_range_to_varying (vr);
2401 return;
2404 /* Now evaluate the expression to determine the new range. */
2405 if (POINTER_TYPE_P (expr_type))
2407 if (code == MIN_EXPR || code == MAX_EXPR)
2409 /* For MIN/MAX expressions with pointers, we only care about
2410 nullness, if both are non null, then the result is nonnull.
2411 If both are null, then the result is null. Otherwise they
2412 are varying. */
2413 if (range_is_nonnull (&vr0) && range_is_nonnull (&vr1))
2414 set_value_range_to_nonnull (vr, expr_type);
2415 else if (range_is_null (&vr0) && range_is_null (&vr1))
2416 set_value_range_to_null (vr, expr_type);
2417 else
2418 set_value_range_to_varying (vr);
2420 else if (code == POINTER_PLUS_EXPR)
2422 /* For pointer types, we are really only interested in asserting
2423 whether the expression evaluates to non-NULL. */
2424 if (range_is_nonnull (&vr0) || range_is_nonnull (&vr1))
2425 set_value_range_to_nonnull (vr, expr_type);
2426 else if (range_is_null (&vr0) && range_is_null (&vr1))
2427 set_value_range_to_null (vr, expr_type);
2428 else
2429 set_value_range_to_varying (vr);
2431 else if (code == BIT_AND_EXPR)
2433 /* For pointer types, we are really only interested in asserting
2434 whether the expression evaluates to non-NULL. */
2435 if (range_is_nonnull (&vr0) && range_is_nonnull (&vr1))
2436 set_value_range_to_nonnull (vr, expr_type);
2437 else if (range_is_null (&vr0) || range_is_null (&vr1))
2438 set_value_range_to_null (vr, expr_type);
2439 else
2440 set_value_range_to_varying (vr);
2442 else
2443 set_value_range_to_varying (vr);
2445 return;
2448 /* For integer ranges, apply the operation to each end of the
2449 range and see what we end up with. */
2450 if (code == PLUS_EXPR || code == MINUS_EXPR)
2452 const bool minus_p = (code == MINUS_EXPR);
2453 tree min_op0 = vr0.min;
2454 tree min_op1 = minus_p ? vr1.max : vr1.min;
2455 tree max_op0 = vr0.max;
2456 tree max_op1 = minus_p ? vr1.min : vr1.max;
2457 tree sym_min_op0 = NULL_TREE;
2458 tree sym_min_op1 = NULL_TREE;
2459 tree sym_max_op0 = NULL_TREE;
2460 tree sym_max_op1 = NULL_TREE;
2461 bool neg_min_op0, neg_min_op1, neg_max_op0, neg_max_op1;
2463 /* If we have a PLUS or MINUS with two VR_RANGEs, either constant or
2464 single-symbolic ranges, try to compute the precise resulting range,
2465 but only if we know that this resulting range will also be constant
2466 or single-symbolic. */
2467 if (vr0.type == VR_RANGE && vr1.type == VR_RANGE
2468 && (TREE_CODE (min_op0) == INTEGER_CST
2469 || (sym_min_op0
2470 = get_single_symbol (min_op0, &neg_min_op0, &min_op0)))
2471 && (TREE_CODE (min_op1) == INTEGER_CST
2472 || (sym_min_op1
2473 = get_single_symbol (min_op1, &neg_min_op1, &min_op1)))
2474 && (!(sym_min_op0 && sym_min_op1)
2475 || (sym_min_op0 == sym_min_op1
2476 && neg_min_op0 == (minus_p ? neg_min_op1 : !neg_min_op1)))
2477 && (TREE_CODE (max_op0) == INTEGER_CST
2478 || (sym_max_op0
2479 = get_single_symbol (max_op0, &neg_max_op0, &max_op0)))
2480 && (TREE_CODE (max_op1) == INTEGER_CST
2481 || (sym_max_op1
2482 = get_single_symbol (max_op1, &neg_max_op1, &max_op1)))
2483 && (!(sym_max_op0 && sym_max_op1)
2484 || (sym_max_op0 == sym_max_op1
2485 && neg_max_op0 == (minus_p ? neg_max_op1 : !neg_max_op1))))
2487 const signop sgn = TYPE_SIGN (expr_type);
2488 const unsigned int prec = TYPE_PRECISION (expr_type);
2489 wide_int type_min, type_max, wmin, wmax;
2490 int min_ovf = 0;
2491 int max_ovf = 0;
2493 /* Get the lower and upper bounds of the type. */
2494 if (TYPE_OVERFLOW_WRAPS (expr_type))
2496 type_min = wi::min_value (prec, sgn);
2497 type_max = wi::max_value (prec, sgn);
2499 else
2501 type_min = vrp_val_min (expr_type);
2502 type_max = vrp_val_max (expr_type);
2505 /* Combine the lower bounds, if any. */
2506 if (min_op0 && min_op1)
2508 if (minus_p)
2510 wmin = wi::sub (min_op0, min_op1);
2512 /* Check for overflow. */
2513 if (wi::cmp (0, min_op1, sgn)
2514 != wi::cmp (wmin, min_op0, sgn))
2515 min_ovf = wi::cmp (min_op0, min_op1, sgn);
2517 else
2519 wmin = wi::add (min_op0, min_op1);
2521 /* Check for overflow. */
2522 if (wi::cmp (min_op1, 0, sgn)
2523 != wi::cmp (wmin, min_op0, sgn))
2524 min_ovf = wi::cmp (min_op0, wmin, sgn);
2527 else if (min_op0)
2528 wmin = min_op0;
2529 else if (min_op1)
2530 wmin = minus_p ? wi::neg (min_op1) : min_op1;
2531 else
2532 wmin = wi::shwi (0, prec);
2534 /* Combine the upper bounds, if any. */
2535 if (max_op0 && max_op1)
2537 if (minus_p)
2539 wmax = wi::sub (max_op0, max_op1);
2541 /* Check for overflow. */
2542 if (wi::cmp (0, max_op1, sgn)
2543 != wi::cmp (wmax, max_op0, sgn))
2544 max_ovf = wi::cmp (max_op0, max_op1, sgn);
2546 else
2548 wmax = wi::add (max_op0, max_op1);
2550 if (wi::cmp (max_op1, 0, sgn)
2551 != wi::cmp (wmax, max_op0, sgn))
2552 max_ovf = wi::cmp (max_op0, wmax, sgn);
2555 else if (max_op0)
2556 wmax = max_op0;
2557 else if (max_op1)
2558 wmax = minus_p ? wi::neg (max_op1) : max_op1;
2559 else
2560 wmax = wi::shwi (0, prec);
2562 /* Check for type overflow. */
2563 if (min_ovf == 0)
2565 if (wi::cmp (wmin, type_min, sgn) == -1)
2566 min_ovf = -1;
2567 else if (wi::cmp (wmin, type_max, sgn) == 1)
2568 min_ovf = 1;
2570 if (max_ovf == 0)
2572 if (wi::cmp (wmax, type_min, sgn) == -1)
2573 max_ovf = -1;
2574 else if (wi::cmp (wmax, type_max, sgn) == 1)
2575 max_ovf = 1;
2578 /* If we have overflow for the constant part and the resulting
2579 range will be symbolic, drop to VR_VARYING. */
2580 if ((min_ovf && sym_min_op0 != sym_min_op1)
2581 || (max_ovf && sym_max_op0 != sym_max_op1))
2583 set_value_range_to_varying (vr);
2584 return;
2587 if (TYPE_OVERFLOW_WRAPS (expr_type))
2589 /* If overflow wraps, truncate the values and adjust the
2590 range kind and bounds appropriately. */
2591 wide_int tmin = wide_int::from (wmin, prec, sgn);
2592 wide_int tmax = wide_int::from (wmax, prec, sgn);
2593 if (min_ovf == max_ovf)
2595 /* No overflow or both overflow or underflow. The
2596 range kind stays VR_RANGE. */
2597 min = wide_int_to_tree (expr_type, tmin);
2598 max = wide_int_to_tree (expr_type, tmax);
2600 else if (min_ovf == -1 && max_ovf == 1)
2602 /* Underflow and overflow, drop to VR_VARYING. */
2603 set_value_range_to_varying (vr);
2604 return;
2606 else
2608 /* Min underflow or max overflow. The range kind
2609 changes to VR_ANTI_RANGE. */
2610 bool covers = false;
2611 wide_int tem = tmin;
2612 gcc_assert ((min_ovf == -1 && max_ovf == 0)
2613 || (max_ovf == 1 && min_ovf == 0));
2614 type = VR_ANTI_RANGE;
2615 tmin = tmax + 1;
2616 if (wi::cmp (tmin, tmax, sgn) < 0)
2617 covers = true;
2618 tmax = tem - 1;
2619 if (wi::cmp (tmax, tem, sgn) > 0)
2620 covers = true;
2621 /* If the anti-range would cover nothing, drop to varying.
2622 Likewise if the anti-range bounds are outside of the
2623 types values. */
2624 if (covers || wi::cmp (tmin, tmax, sgn) > 0)
2626 set_value_range_to_varying (vr);
2627 return;
2629 min = wide_int_to_tree (expr_type, tmin);
2630 max = wide_int_to_tree (expr_type, tmax);
2633 else
2635 /* If overflow does not wrap, saturate to the types min/max
2636 value. */
2637 if (min_ovf == -1)
2639 if (needs_overflow_infinity (expr_type)
2640 && supports_overflow_infinity (expr_type))
2641 min = negative_overflow_infinity (expr_type);
2642 else
2643 min = wide_int_to_tree (expr_type, type_min);
2645 else if (min_ovf == 1)
2647 if (needs_overflow_infinity (expr_type)
2648 && supports_overflow_infinity (expr_type))
2649 min = positive_overflow_infinity (expr_type);
2650 else
2651 min = wide_int_to_tree (expr_type, type_max);
2653 else
2654 min = wide_int_to_tree (expr_type, wmin);
2656 if (max_ovf == -1)
2658 if (needs_overflow_infinity (expr_type)
2659 && supports_overflow_infinity (expr_type))
2660 max = negative_overflow_infinity (expr_type);
2661 else
2662 max = wide_int_to_tree (expr_type, type_min);
2664 else if (max_ovf == 1)
2666 if (needs_overflow_infinity (expr_type)
2667 && supports_overflow_infinity (expr_type))
2668 max = positive_overflow_infinity (expr_type);
2669 else
2670 max = wide_int_to_tree (expr_type, type_max);
2672 else
2673 max = wide_int_to_tree (expr_type, wmax);
2676 if (needs_overflow_infinity (expr_type)
2677 && supports_overflow_infinity (expr_type))
2679 if ((min_op0 && is_negative_overflow_infinity (min_op0))
2680 || (min_op1
2681 && (minus_p
2682 ? is_positive_overflow_infinity (min_op1)
2683 : is_negative_overflow_infinity (min_op1))))
2684 min = negative_overflow_infinity (expr_type);
2685 if ((max_op0 && is_positive_overflow_infinity (max_op0))
2686 || (max_op1
2687 && (minus_p
2688 ? is_negative_overflow_infinity (max_op1)
2689 : is_positive_overflow_infinity (max_op1))))
2690 max = positive_overflow_infinity (expr_type);
2693 /* If the result lower bound is constant, we're done;
2694 otherwise, build the symbolic lower bound. */
2695 if (sym_min_op0 == sym_min_op1)
2697 else if (sym_min_op0)
2698 min = build_symbolic_expr (expr_type, sym_min_op0,
2699 neg_min_op0, min);
2700 else if (sym_min_op1)
2701 min = build_symbolic_expr (expr_type, sym_min_op1,
2702 neg_min_op1 ^ minus_p, min);
2704 /* Likewise for the upper bound. */
2705 if (sym_max_op0 == sym_max_op1)
2707 else if (sym_max_op0)
2708 max = build_symbolic_expr (expr_type, sym_max_op0,
2709 neg_max_op0, max);
2710 else if (sym_max_op1)
2711 max = build_symbolic_expr (expr_type, sym_max_op1,
2712 neg_max_op1 ^ minus_p, max);
2714 else
2716 /* For other cases, for example if we have a PLUS_EXPR with two
2717 VR_ANTI_RANGEs, drop to VR_VARYING. It would take more effort
2718 to compute a precise range for such a case.
2719 ??? General even mixed range kind operations can be expressed
2720 by for example transforming ~[3, 5] + [1, 2] to range-only
2721 operations and a union primitive:
2722 [-INF, 2] + [1, 2] U [5, +INF] + [1, 2]
2723 [-INF+1, 4] U [6, +INF(OVF)]
2724 though usually the union is not exactly representable with
2725 a single range or anti-range as the above is
2726 [-INF+1, +INF(OVF)] intersected with ~[5, 5]
2727 but one could use a scheme similar to equivalences for this. */
2728 set_value_range_to_varying (vr);
2729 return;
2732 else if (code == MIN_EXPR
2733 || code == MAX_EXPR)
2735 if (vr0.type == VR_RANGE
2736 && !symbolic_range_p (&vr0))
2738 type = VR_RANGE;
2739 if (vr1.type == VR_RANGE
2740 && !symbolic_range_p (&vr1))
2742 /* For operations that make the resulting range directly
2743 proportional to the original ranges, apply the operation to
2744 the same end of each range. */
2745 min = vrp_int_const_binop (code, vr0.min, vr1.min);
2746 max = vrp_int_const_binop (code, vr0.max, vr1.max);
2748 else if (code == MIN_EXPR)
2750 min = vrp_val_min (expr_type);
2751 max = vr0.max;
2753 else if (code == MAX_EXPR)
2755 min = vr0.min;
2756 max = vrp_val_max (expr_type);
2759 else if (vr1.type == VR_RANGE
2760 && !symbolic_range_p (&vr1))
2762 type = VR_RANGE;
2763 if (code == MIN_EXPR)
2765 min = vrp_val_min (expr_type);
2766 max = vr1.max;
2768 else if (code == MAX_EXPR)
2770 min = vr1.min;
2771 max = vrp_val_max (expr_type);
2774 else
2776 set_value_range_to_varying (vr);
2777 return;
2780 else if (code == MULT_EXPR)
2782 /* Fancy code so that with unsigned, [-3,-1]*[-3,-1] does not
2783 drop to varying. This test requires 2*prec bits if both
2784 operands are signed and 2*prec + 2 bits if either is not. */
2786 signop sign = TYPE_SIGN (expr_type);
2787 unsigned int prec = TYPE_PRECISION (expr_type);
2789 if (range_int_cst_p (&vr0)
2790 && range_int_cst_p (&vr1)
2791 && TYPE_OVERFLOW_WRAPS (expr_type))
2793 typedef FIXED_WIDE_INT (WIDE_INT_MAX_PRECISION * 2) vrp_int;
2794 typedef generic_wide_int
2795 <wi::extended_tree <WIDE_INT_MAX_PRECISION * 2> > vrp_int_cst;
2796 vrp_int sizem1 = wi::mask <vrp_int> (prec, false);
2797 vrp_int size = sizem1 + 1;
2799 /* Extend the values using the sign of the result to PREC2.
2800 From here on out, everthing is just signed math no matter
2801 what the input types were. */
2802 vrp_int min0 = vrp_int_cst (vr0.min);
2803 vrp_int max0 = vrp_int_cst (vr0.max);
2804 vrp_int min1 = vrp_int_cst (vr1.min);
2805 vrp_int max1 = vrp_int_cst (vr1.max);
2806 /* Canonicalize the intervals. */
2807 if (sign == UNSIGNED)
2809 if (wi::ltu_p (size, min0 + max0))
2811 min0 -= size;
2812 max0 -= size;
2815 if (wi::ltu_p (size, min1 + max1))
2817 min1 -= size;
2818 max1 -= size;
2822 vrp_int prod0 = min0 * min1;
2823 vrp_int prod1 = min0 * max1;
2824 vrp_int prod2 = max0 * min1;
2825 vrp_int prod3 = max0 * max1;
2827 /* Sort the 4 products so that min is in prod0 and max is in
2828 prod3. */
2829 /* min0min1 > max0max1 */
2830 if (wi::gts_p (prod0, prod3))
2831 std::swap (prod0, prod3);
2833 /* min0max1 > max0min1 */
2834 if (wi::gts_p (prod1, prod2))
2835 std::swap (prod1, prod2);
2837 if (wi::gts_p (prod0, prod1))
2838 std::swap (prod0, prod1);
2840 if (wi::gts_p (prod2, prod3))
2841 std::swap (prod2, prod3);
2843 /* diff = max - min. */
2844 prod2 = prod3 - prod0;
2845 if (wi::geu_p (prod2, sizem1))
2847 /* the range covers all values. */
2848 set_value_range_to_varying (vr);
2849 return;
2852 /* The following should handle the wrapping and selecting
2853 VR_ANTI_RANGE for us. */
2854 min = wide_int_to_tree (expr_type, prod0);
2855 max = wide_int_to_tree (expr_type, prod3);
2856 set_and_canonicalize_value_range (vr, VR_RANGE, min, max, NULL);
2857 return;
2860 /* If we have an unsigned MULT_EXPR with two VR_ANTI_RANGEs,
2861 drop to VR_VARYING. It would take more effort to compute a
2862 precise range for such a case. For example, if we have
2863 op0 == 65536 and op1 == 65536 with their ranges both being
2864 ~[0,0] on a 32-bit machine, we would have op0 * op1 == 0, so
2865 we cannot claim that the product is in ~[0,0]. Note that we
2866 are guaranteed to have vr0.type == vr1.type at this
2867 point. */
2868 if (vr0.type == VR_ANTI_RANGE
2869 && !TYPE_OVERFLOW_UNDEFINED (expr_type))
2871 set_value_range_to_varying (vr);
2872 return;
2875 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
2876 return;
2878 else if (code == RSHIFT_EXPR
2879 || code == LSHIFT_EXPR)
2881 /* If we have a RSHIFT_EXPR with any shift values outside [0..prec-1],
2882 then drop to VR_VARYING. Outside of this range we get undefined
2883 behavior from the shift operation. We cannot even trust
2884 SHIFT_COUNT_TRUNCATED at this stage, because that applies to rtl
2885 shifts, and the operation at the tree level may be widened. */
2886 if (range_int_cst_p (&vr1)
2887 && compare_tree_int (vr1.min, 0) >= 0
2888 && compare_tree_int (vr1.max, TYPE_PRECISION (expr_type)) == -1)
2890 if (code == RSHIFT_EXPR)
2892 /* Even if vr0 is VARYING or otherwise not usable, we can derive
2893 useful ranges just from the shift count. E.g.
2894 x >> 63 for signed 64-bit x is always [-1, 0]. */
2895 if (vr0.type != VR_RANGE || symbolic_range_p (&vr0))
2897 vr0.type = type = VR_RANGE;
2898 vr0.min = vrp_val_min (expr_type);
2899 vr0.max = vrp_val_max (expr_type);
2901 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
2902 return;
2904 /* We can map lshifts by constants to MULT_EXPR handling. */
2905 else if (code == LSHIFT_EXPR
2906 && range_int_cst_singleton_p (&vr1))
2908 bool saved_flag_wrapv;
2909 value_range vr1p = VR_INITIALIZER;
2910 vr1p.type = VR_RANGE;
2911 vr1p.min = (wide_int_to_tree
2912 (expr_type,
2913 wi::set_bit_in_zero (tree_to_shwi (vr1.min),
2914 TYPE_PRECISION (expr_type))));
2915 vr1p.max = vr1p.min;
2916 /* We have to use a wrapping multiply though as signed overflow
2917 on lshifts is implementation defined in C89. */
2918 saved_flag_wrapv = flag_wrapv;
2919 flag_wrapv = 1;
2920 extract_range_from_binary_expr_1 (vr, MULT_EXPR, expr_type,
2921 &vr0, &vr1p);
2922 flag_wrapv = saved_flag_wrapv;
2923 return;
2925 else if (code == LSHIFT_EXPR
2926 && range_int_cst_p (&vr0))
2928 int prec = TYPE_PRECISION (expr_type);
2929 int overflow_pos = prec;
2930 int bound_shift;
2931 wide_int low_bound, high_bound;
2932 bool uns = TYPE_UNSIGNED (expr_type);
2933 bool in_bounds = false;
2935 if (!uns)
2936 overflow_pos -= 1;
2938 bound_shift = overflow_pos - tree_to_shwi (vr1.max);
2939 /* If bound_shift == HOST_BITS_PER_WIDE_INT, the llshift can
2940 overflow. However, for that to happen, vr1.max needs to be
2941 zero, which means vr1 is a singleton range of zero, which
2942 means it should be handled by the previous LSHIFT_EXPR
2943 if-clause. */
2944 wide_int bound = wi::set_bit_in_zero (bound_shift, prec);
2945 wide_int complement = ~(bound - 1);
2947 if (uns)
2949 low_bound = bound;
2950 high_bound = complement;
2951 if (wi::ltu_p (vr0.max, low_bound))
2953 /* [5, 6] << [1, 2] == [10, 24]. */
2954 /* We're shifting out only zeroes, the value increases
2955 monotonically. */
2956 in_bounds = true;
2958 else if (wi::ltu_p (high_bound, vr0.min))
2960 /* [0xffffff00, 0xffffffff] << [1, 2]
2961 == [0xfffffc00, 0xfffffffe]. */
2962 /* We're shifting out only ones, the value decreases
2963 monotonically. */
2964 in_bounds = true;
2967 else
2969 /* [-1, 1] << [1, 2] == [-4, 4]. */
2970 low_bound = complement;
2971 high_bound = bound;
2972 if (wi::lts_p (vr0.max, high_bound)
2973 && wi::lts_p (low_bound, vr0.min))
2975 /* For non-negative numbers, we're shifting out only
2976 zeroes, the value increases monotonically.
2977 For negative numbers, we're shifting out only ones, the
2978 value decreases monotomically. */
2979 in_bounds = true;
2983 if (in_bounds)
2985 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
2986 return;
2990 set_value_range_to_varying (vr);
2991 return;
2993 else if (code == TRUNC_DIV_EXPR
2994 || code == FLOOR_DIV_EXPR
2995 || code == CEIL_DIV_EXPR
2996 || code == EXACT_DIV_EXPR
2997 || code == ROUND_DIV_EXPR)
2999 if (vr0.type != VR_RANGE || symbolic_range_p (&vr0))
3001 /* For division, if op1 has VR_RANGE but op0 does not, something
3002 can be deduced just from that range. Say [min, max] / [4, max]
3003 gives [min / 4, max / 4] range. */
3004 if (vr1.type == VR_RANGE
3005 && !symbolic_range_p (&vr1)
3006 && range_includes_zero_p (vr1.min, vr1.max) == 0)
3008 vr0.type = type = VR_RANGE;
3009 vr0.min = vrp_val_min (expr_type);
3010 vr0.max = vrp_val_max (expr_type);
3012 else
3014 set_value_range_to_varying (vr);
3015 return;
3019 /* For divisions, if flag_non_call_exceptions is true, we must
3020 not eliminate a division by zero. */
3021 if (cfun->can_throw_non_call_exceptions
3022 && (vr1.type != VR_RANGE
3023 || range_includes_zero_p (vr1.min, vr1.max) != 0))
3025 set_value_range_to_varying (vr);
3026 return;
3029 /* For divisions, if op0 is VR_RANGE, we can deduce a range
3030 even if op1 is VR_VARYING, VR_ANTI_RANGE, symbolic or can
3031 include 0. */
3032 if (vr0.type == VR_RANGE
3033 && (vr1.type != VR_RANGE
3034 || range_includes_zero_p (vr1.min, vr1.max) != 0))
3036 tree zero = build_int_cst (TREE_TYPE (vr0.min), 0);
3037 int cmp;
3039 min = NULL_TREE;
3040 max = NULL_TREE;
3041 if (TYPE_UNSIGNED (expr_type)
3042 || value_range_nonnegative_p (&vr1))
3044 /* For unsigned division or when divisor is known
3045 to be non-negative, the range has to cover
3046 all numbers from 0 to max for positive max
3047 and all numbers from min to 0 for negative min. */
3048 cmp = compare_values (vr0.max, zero);
3049 if (cmp == -1)
3051 /* When vr0.max < 0, vr1.min != 0 and value
3052 ranges for dividend and divisor are available. */
3053 if (vr1.type == VR_RANGE
3054 && !symbolic_range_p (&vr0)
3055 && !symbolic_range_p (&vr1)
3056 && !compare_values (vr1.min, zero))
3057 max = int_const_binop (code, vr0.max, vr1.min);
3058 else
3059 max = zero;
3061 else if (cmp == 0 || cmp == 1)
3062 max = vr0.max;
3063 else
3064 type = VR_VARYING;
3065 cmp = compare_values (vr0.min, zero);
3066 if (cmp == 1)
3068 /* For unsigned division when value ranges for dividend
3069 and divisor are available. */
3070 if (vr1.type == VR_RANGE
3071 && !symbolic_range_p (&vr0)
3072 && !symbolic_range_p (&vr1))
3073 min = int_const_binop (code, vr0.min, vr1.max);
3074 else
3075 min = zero;
3077 else if (cmp == 0 || cmp == -1)
3078 min = vr0.min;
3079 else
3080 type = VR_VARYING;
3082 else
3084 /* Otherwise the range is -max .. max or min .. -min
3085 depending on which bound is bigger in absolute value,
3086 as the division can change the sign. */
3087 abs_extent_range (vr, vr0.min, vr0.max);
3088 return;
3090 if (type == VR_VARYING)
3092 set_value_range_to_varying (vr);
3093 return;
3096 else
3098 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
3099 return;
3102 else if (code == TRUNC_MOD_EXPR)
3104 if (range_is_null (&vr1))
3106 set_value_range_to_undefined (vr);
3107 return;
3109 /* ABS (A % B) < ABS (B) and either
3110 0 <= A % B <= A or A <= A % B <= 0. */
3111 type = VR_RANGE;
3112 signop sgn = TYPE_SIGN (expr_type);
3113 unsigned int prec = TYPE_PRECISION (expr_type);
3114 wide_int wmin, wmax, tmp;
3115 wide_int zero = wi::zero (prec);
3116 wide_int one = wi::one (prec);
3117 if (vr1.type == VR_RANGE && !symbolic_range_p (&vr1))
3119 wmax = wi::sub (vr1.max, one);
3120 if (sgn == SIGNED)
3122 tmp = wi::sub (wi::minus_one (prec), vr1.min);
3123 wmax = wi::smax (wmax, tmp);
3126 else
3128 wmax = wi::max_value (prec, sgn);
3129 /* X % INT_MIN may be INT_MAX. */
3130 if (sgn == UNSIGNED)
3131 wmax = wmax - one;
3134 if (sgn == UNSIGNED)
3135 wmin = zero;
3136 else
3138 wmin = -wmax;
3139 if (vr0.type == VR_RANGE && TREE_CODE (vr0.min) == INTEGER_CST)
3141 tmp = vr0.min;
3142 if (wi::gts_p (tmp, zero))
3143 tmp = zero;
3144 wmin = wi::smax (wmin, tmp);
3148 if (vr0.type == VR_RANGE && TREE_CODE (vr0.max) == INTEGER_CST)
3150 tmp = vr0.max;
3151 if (sgn == SIGNED && wi::neg_p (tmp))
3152 tmp = zero;
3153 wmax = wi::min (wmax, tmp, sgn);
3156 min = wide_int_to_tree (expr_type, wmin);
3157 max = wide_int_to_tree (expr_type, wmax);
3159 else if (code == BIT_AND_EXPR || code == BIT_IOR_EXPR || code == BIT_XOR_EXPR)
3161 bool int_cst_range0, int_cst_range1;
3162 wide_int may_be_nonzero0, may_be_nonzero1;
3163 wide_int must_be_nonzero0, must_be_nonzero1;
3165 int_cst_range0 = zero_nonzero_bits_from_vr (expr_type, &vr0,
3166 &may_be_nonzero0,
3167 &must_be_nonzero0);
3168 int_cst_range1 = zero_nonzero_bits_from_vr (expr_type, &vr1,
3169 &may_be_nonzero1,
3170 &must_be_nonzero1);
3172 type = VR_RANGE;
3173 if (code == BIT_AND_EXPR)
3175 min = wide_int_to_tree (expr_type,
3176 must_be_nonzero0 & must_be_nonzero1);
3177 wide_int wmax = may_be_nonzero0 & may_be_nonzero1;
3178 /* If both input ranges contain only negative values we can
3179 truncate the result range maximum to the minimum of the
3180 input range maxima. */
3181 if (int_cst_range0 && int_cst_range1
3182 && tree_int_cst_sgn (vr0.max) < 0
3183 && tree_int_cst_sgn (vr1.max) < 0)
3185 wmax = wi::min (wmax, vr0.max, TYPE_SIGN (expr_type));
3186 wmax = wi::min (wmax, vr1.max, TYPE_SIGN (expr_type));
3188 /* If either input range contains only non-negative values
3189 we can truncate the result range maximum to the respective
3190 maximum of the input range. */
3191 if (int_cst_range0 && tree_int_cst_sgn (vr0.min) >= 0)
3192 wmax = wi::min (wmax, vr0.max, TYPE_SIGN (expr_type));
3193 if (int_cst_range1 && tree_int_cst_sgn (vr1.min) >= 0)
3194 wmax = wi::min (wmax, vr1.max, TYPE_SIGN (expr_type));
3195 max = wide_int_to_tree (expr_type, wmax);
3197 else if (code == BIT_IOR_EXPR)
3199 max = wide_int_to_tree (expr_type,
3200 may_be_nonzero0 | may_be_nonzero1);
3201 wide_int wmin = must_be_nonzero0 | must_be_nonzero1;
3202 /* If the input ranges contain only positive values we can
3203 truncate the minimum of the result range to the maximum
3204 of the input range minima. */
3205 if (int_cst_range0 && int_cst_range1
3206 && tree_int_cst_sgn (vr0.min) >= 0
3207 && tree_int_cst_sgn (vr1.min) >= 0)
3209 wmin = wi::max (wmin, vr0.min, TYPE_SIGN (expr_type));
3210 wmin = wi::max (wmin, vr1.min, TYPE_SIGN (expr_type));
3212 /* If either input range contains only negative values
3213 we can truncate the minimum of the result range to the
3214 respective minimum range. */
3215 if (int_cst_range0 && tree_int_cst_sgn (vr0.max) < 0)
3216 wmin = wi::max (wmin, vr0.min, TYPE_SIGN (expr_type));
3217 if (int_cst_range1 && tree_int_cst_sgn (vr1.max) < 0)
3218 wmin = wi::max (wmin, vr1.min, TYPE_SIGN (expr_type));
3219 min = wide_int_to_tree (expr_type, wmin);
3221 else if (code == BIT_XOR_EXPR)
3223 wide_int result_zero_bits = ((must_be_nonzero0 & must_be_nonzero1)
3224 | ~(may_be_nonzero0 | may_be_nonzero1));
3225 wide_int result_one_bits
3226 = (must_be_nonzero0.and_not (may_be_nonzero1)
3227 | must_be_nonzero1.and_not (may_be_nonzero0));
3228 max = wide_int_to_tree (expr_type, ~result_zero_bits);
3229 min = wide_int_to_tree (expr_type, result_one_bits);
3230 /* If the range has all positive or all negative values the
3231 result is better than VARYING. */
3232 if (tree_int_cst_sgn (min) < 0
3233 || tree_int_cst_sgn (max) >= 0)
3235 else
3236 max = min = NULL_TREE;
3239 else
3240 gcc_unreachable ();
3242 /* If either MIN or MAX overflowed, then set the resulting range to
3243 VARYING. But we do accept an overflow infinity representation. */
3244 if (min == NULL_TREE
3245 || (TREE_OVERFLOW_P (min) && !is_overflow_infinity (min))
3246 || max == NULL_TREE
3247 || (TREE_OVERFLOW_P (max) && !is_overflow_infinity (max)))
3249 set_value_range_to_varying (vr);
3250 return;
3253 /* We punt if:
3254 1) [-INF, +INF]
3255 2) [-INF, +-INF(OVF)]
3256 3) [+-INF(OVF), +INF]
3257 4) [+-INF(OVF), +-INF(OVF)]
3258 We learn nothing when we have INF and INF(OVF) on both sides.
3259 Note that we do accept [-INF, -INF] and [+INF, +INF] without
3260 overflow. */
3261 if ((vrp_val_is_min (min) || is_overflow_infinity (min))
3262 && (vrp_val_is_max (max) || is_overflow_infinity (max)))
3264 set_value_range_to_varying (vr);
3265 return;
3268 cmp = compare_values (min, max);
3269 if (cmp == -2 || cmp == 1)
3271 /* If the new range has its limits swapped around (MIN > MAX),
3272 then the operation caused one of them to wrap around, mark
3273 the new range VARYING. */
3274 set_value_range_to_varying (vr);
3276 else
3277 set_value_range (vr, type, min, max, NULL);
3280 /* Extract range information from a binary expression OP0 CODE OP1 based on
3281 the ranges of each of its operands with resulting type EXPR_TYPE.
3282 The resulting range is stored in *VR. */
3284 static void
3285 extract_range_from_binary_expr (value_range *vr,
3286 enum tree_code code,
3287 tree expr_type, tree op0, tree op1)
3289 value_range vr0 = VR_INITIALIZER;
3290 value_range vr1 = VR_INITIALIZER;
3292 /* Get value ranges for each operand. For constant operands, create
3293 a new value range with the operand to simplify processing. */
3294 if (TREE_CODE (op0) == SSA_NAME)
3295 vr0 = *(get_value_range (op0));
3296 else if (is_gimple_min_invariant (op0))
3297 set_value_range_to_value (&vr0, op0, NULL);
3298 else
3299 set_value_range_to_varying (&vr0);
3301 if (TREE_CODE (op1) == SSA_NAME)
3302 vr1 = *(get_value_range (op1));
3303 else if (is_gimple_min_invariant (op1))
3304 set_value_range_to_value (&vr1, op1, NULL);
3305 else
3306 set_value_range_to_varying (&vr1);
3308 extract_range_from_binary_expr_1 (vr, code, expr_type, &vr0, &vr1);
3310 /* Try harder for PLUS and MINUS if the range of one operand is symbolic
3311 and based on the other operand, for example if it was deduced from a
3312 symbolic comparison. When a bound of the range of the first operand
3313 is invariant, we set the corresponding bound of the new range to INF
3314 in order to avoid recursing on the range of the second operand. */
3315 if (vr->type == VR_VARYING
3316 && (code == PLUS_EXPR || code == MINUS_EXPR)
3317 && TREE_CODE (op1) == SSA_NAME
3318 && vr0.type == VR_RANGE
3319 && symbolic_range_based_on_p (&vr0, op1))
3321 const bool minus_p = (code == MINUS_EXPR);
3322 value_range n_vr1 = VR_INITIALIZER;
3324 /* Try with VR0 and [-INF, OP1]. */
3325 if (is_gimple_min_invariant (minus_p ? vr0.max : vr0.min))
3326 set_value_range (&n_vr1, VR_RANGE, vrp_val_min (expr_type), op1, NULL);
3328 /* Try with VR0 and [OP1, +INF]. */
3329 else if (is_gimple_min_invariant (minus_p ? vr0.min : vr0.max))
3330 set_value_range (&n_vr1, VR_RANGE, op1, vrp_val_max (expr_type), NULL);
3332 /* Try with VR0 and [OP1, OP1]. */
3333 else
3334 set_value_range (&n_vr1, VR_RANGE, op1, op1, NULL);
3336 extract_range_from_binary_expr_1 (vr, code, expr_type, &vr0, &n_vr1);
3339 if (vr->type == VR_VARYING
3340 && (code == PLUS_EXPR || code == MINUS_EXPR)
3341 && TREE_CODE (op0) == SSA_NAME
3342 && vr1.type == VR_RANGE
3343 && symbolic_range_based_on_p (&vr1, op0))
3345 const bool minus_p = (code == MINUS_EXPR);
3346 value_range n_vr0 = VR_INITIALIZER;
3348 /* Try with [-INF, OP0] and VR1. */
3349 if (is_gimple_min_invariant (minus_p ? vr1.max : vr1.min))
3350 set_value_range (&n_vr0, VR_RANGE, vrp_val_min (expr_type), op0, NULL);
3352 /* Try with [OP0, +INF] and VR1. */
3353 else if (is_gimple_min_invariant (minus_p ? vr1.min : vr1.max))
3354 set_value_range (&n_vr0, VR_RANGE, op0, vrp_val_max (expr_type), NULL);
3356 /* Try with [OP0, OP0] and VR1. */
3357 else
3358 set_value_range (&n_vr0, VR_RANGE, op0, op0, NULL);
3360 extract_range_from_binary_expr_1 (vr, code, expr_type, &n_vr0, &vr1);
3364 /* Extract range information from a unary operation CODE based on
3365 the range of its operand *VR0 with type OP0_TYPE with resulting type TYPE.
3366 The resulting range is stored in *VR. */
3368 static void
3369 extract_range_from_unary_expr_1 (value_range *vr,
3370 enum tree_code code, tree type,
3371 value_range *vr0_, tree op0_type)
3373 value_range vr0 = *vr0_, vrtem0 = VR_INITIALIZER, vrtem1 = VR_INITIALIZER;
3375 /* VRP only operates on integral and pointer types. */
3376 if (!(INTEGRAL_TYPE_P (op0_type)
3377 || POINTER_TYPE_P (op0_type))
3378 || !(INTEGRAL_TYPE_P (type)
3379 || POINTER_TYPE_P (type)))
3381 set_value_range_to_varying (vr);
3382 return;
3385 /* If VR0 is UNDEFINED, so is the result. */
3386 if (vr0.type == VR_UNDEFINED)
3388 set_value_range_to_undefined (vr);
3389 return;
3392 /* Handle operations that we express in terms of others. */
3393 if (code == PAREN_EXPR || code == OBJ_TYPE_REF)
3395 /* PAREN_EXPR and OBJ_TYPE_REF are simple copies. */
3396 copy_value_range (vr, &vr0);
3397 return;
3399 else if (code == NEGATE_EXPR)
3401 /* -X is simply 0 - X, so re-use existing code that also handles
3402 anti-ranges fine. */
3403 value_range zero = VR_INITIALIZER;
3404 set_value_range_to_value (&zero, build_int_cst (type, 0), NULL);
3405 extract_range_from_binary_expr_1 (vr, MINUS_EXPR, type, &zero, &vr0);
3406 return;
3408 else if (code == BIT_NOT_EXPR)
3410 /* ~X is simply -1 - X, so re-use existing code that also handles
3411 anti-ranges fine. */
3412 value_range minusone = VR_INITIALIZER;
3413 set_value_range_to_value (&minusone, build_int_cst (type, -1), NULL);
3414 extract_range_from_binary_expr_1 (vr, MINUS_EXPR,
3415 type, &minusone, &vr0);
3416 return;
3419 /* Now canonicalize anti-ranges to ranges when they are not symbolic
3420 and express op ~[] as (op []') U (op []''). */
3421 if (vr0.type == VR_ANTI_RANGE
3422 && ranges_from_anti_range (&vr0, &vrtem0, &vrtem1))
3424 extract_range_from_unary_expr_1 (vr, code, type, &vrtem0, op0_type);
3425 if (vrtem1.type != VR_UNDEFINED)
3427 value_range vrres = VR_INITIALIZER;
3428 extract_range_from_unary_expr_1 (&vrres, code, type,
3429 &vrtem1, op0_type);
3430 vrp_meet (vr, &vrres);
3432 return;
3435 if (CONVERT_EXPR_CODE_P (code))
3437 tree inner_type = op0_type;
3438 tree outer_type = type;
3440 /* If the expression evaluates to a pointer, we are only interested in
3441 determining if it evaluates to NULL [0, 0] or non-NULL (~[0, 0]). */
3442 if (POINTER_TYPE_P (type))
3444 if (range_is_nonnull (&vr0))
3445 set_value_range_to_nonnull (vr, type);
3446 else if (range_is_null (&vr0))
3447 set_value_range_to_null (vr, type);
3448 else
3449 set_value_range_to_varying (vr);
3450 return;
3453 /* If VR0 is varying and we increase the type precision, assume
3454 a full range for the following transformation. */
3455 if (vr0.type == VR_VARYING
3456 && INTEGRAL_TYPE_P (inner_type)
3457 && TYPE_PRECISION (inner_type) < TYPE_PRECISION (outer_type))
3459 vr0.type = VR_RANGE;
3460 vr0.min = TYPE_MIN_VALUE (inner_type);
3461 vr0.max = TYPE_MAX_VALUE (inner_type);
3464 /* If VR0 is a constant range or anti-range and the conversion is
3465 not truncating we can convert the min and max values and
3466 canonicalize the resulting range. Otherwise we can do the
3467 conversion if the size of the range is less than what the
3468 precision of the target type can represent and the range is
3469 not an anti-range. */
3470 if ((vr0.type == VR_RANGE
3471 || vr0.type == VR_ANTI_RANGE)
3472 && TREE_CODE (vr0.min) == INTEGER_CST
3473 && TREE_CODE (vr0.max) == INTEGER_CST
3474 && (!is_overflow_infinity (vr0.min)
3475 || (vr0.type == VR_RANGE
3476 && TYPE_PRECISION (outer_type) > TYPE_PRECISION (inner_type)
3477 && needs_overflow_infinity (outer_type)
3478 && supports_overflow_infinity (outer_type)))
3479 && (!is_overflow_infinity (vr0.max)
3480 || (vr0.type == VR_RANGE
3481 && TYPE_PRECISION (outer_type) > TYPE_PRECISION (inner_type)
3482 && needs_overflow_infinity (outer_type)
3483 && supports_overflow_infinity (outer_type)))
3484 && (TYPE_PRECISION (outer_type) >= TYPE_PRECISION (inner_type)
3485 || (vr0.type == VR_RANGE
3486 && integer_zerop (int_const_binop (RSHIFT_EXPR,
3487 int_const_binop (MINUS_EXPR, vr0.max, vr0.min),
3488 size_int (TYPE_PRECISION (outer_type)))))))
3490 tree new_min, new_max;
3491 if (is_overflow_infinity (vr0.min))
3492 new_min = negative_overflow_infinity (outer_type);
3493 else
3494 new_min = force_fit_type (outer_type, wi::to_widest (vr0.min),
3495 0, false);
3496 if (is_overflow_infinity (vr0.max))
3497 new_max = positive_overflow_infinity (outer_type);
3498 else
3499 new_max = force_fit_type (outer_type, wi::to_widest (vr0.max),
3500 0, false);
3501 set_and_canonicalize_value_range (vr, vr0.type,
3502 new_min, new_max, NULL);
3503 return;
3506 set_value_range_to_varying (vr);
3507 return;
3509 else if (code == ABS_EXPR)
3511 tree min, max;
3512 int cmp;
3514 /* Pass through vr0 in the easy cases. */
3515 if (TYPE_UNSIGNED (type)
3516 || value_range_nonnegative_p (&vr0))
3518 copy_value_range (vr, &vr0);
3519 return;
3522 /* For the remaining varying or symbolic ranges we can't do anything
3523 useful. */
3524 if (vr0.type == VR_VARYING
3525 || symbolic_range_p (&vr0))
3527 set_value_range_to_varying (vr);
3528 return;
3531 /* -TYPE_MIN_VALUE = TYPE_MIN_VALUE with flag_wrapv so we can't get a
3532 useful range. */
3533 if (!TYPE_OVERFLOW_UNDEFINED (type)
3534 && ((vr0.type == VR_RANGE
3535 && vrp_val_is_min (vr0.min))
3536 || (vr0.type == VR_ANTI_RANGE
3537 && !vrp_val_is_min (vr0.min))))
3539 set_value_range_to_varying (vr);
3540 return;
3543 /* ABS_EXPR may flip the range around, if the original range
3544 included negative values. */
3545 if (is_overflow_infinity (vr0.min))
3546 min = positive_overflow_infinity (type);
3547 else if (!vrp_val_is_min (vr0.min))
3548 min = fold_unary_to_constant (code, type, vr0.min);
3549 else if (!needs_overflow_infinity (type))
3550 min = TYPE_MAX_VALUE (type);
3551 else if (supports_overflow_infinity (type))
3552 min = positive_overflow_infinity (type);
3553 else
3555 set_value_range_to_varying (vr);
3556 return;
3559 if (is_overflow_infinity (vr0.max))
3560 max = positive_overflow_infinity (type);
3561 else if (!vrp_val_is_min (vr0.max))
3562 max = fold_unary_to_constant (code, type, vr0.max);
3563 else if (!needs_overflow_infinity (type))
3564 max = TYPE_MAX_VALUE (type);
3565 else if (supports_overflow_infinity (type)
3566 /* We shouldn't generate [+INF, +INF] as set_value_range
3567 doesn't like this and ICEs. */
3568 && !is_positive_overflow_infinity (min))
3569 max = positive_overflow_infinity (type);
3570 else
3572 set_value_range_to_varying (vr);
3573 return;
3576 cmp = compare_values (min, max);
3578 /* If a VR_ANTI_RANGEs contains zero, then we have
3579 ~[-INF, min(MIN, MAX)]. */
3580 if (vr0.type == VR_ANTI_RANGE)
3582 if (range_includes_zero_p (vr0.min, vr0.max) == 1)
3584 /* Take the lower of the two values. */
3585 if (cmp != 1)
3586 max = min;
3588 /* Create ~[-INF, min (abs(MIN), abs(MAX))]
3589 or ~[-INF + 1, min (abs(MIN), abs(MAX))] when
3590 flag_wrapv is set and the original anti-range doesn't include
3591 TYPE_MIN_VALUE, remember -TYPE_MIN_VALUE = TYPE_MIN_VALUE. */
3592 if (TYPE_OVERFLOW_WRAPS (type))
3594 tree type_min_value = TYPE_MIN_VALUE (type);
3596 min = (vr0.min != type_min_value
3597 ? int_const_binop (PLUS_EXPR, type_min_value,
3598 build_int_cst (TREE_TYPE (type_min_value), 1))
3599 : type_min_value);
3601 else
3603 if (overflow_infinity_range_p (&vr0))
3604 min = negative_overflow_infinity (type);
3605 else
3606 min = TYPE_MIN_VALUE (type);
3609 else
3611 /* All else has failed, so create the range [0, INF], even for
3612 flag_wrapv since TYPE_MIN_VALUE is in the original
3613 anti-range. */
3614 vr0.type = VR_RANGE;
3615 min = build_int_cst (type, 0);
3616 if (needs_overflow_infinity (type))
3618 if (supports_overflow_infinity (type))
3619 max = positive_overflow_infinity (type);
3620 else
3622 set_value_range_to_varying (vr);
3623 return;
3626 else
3627 max = TYPE_MAX_VALUE (type);
3631 /* If the range contains zero then we know that the minimum value in the
3632 range will be zero. */
3633 else if (range_includes_zero_p (vr0.min, vr0.max) == 1)
3635 if (cmp == 1)
3636 max = min;
3637 min = build_int_cst (type, 0);
3639 else
3641 /* If the range was reversed, swap MIN and MAX. */
3642 if (cmp == 1)
3643 std::swap (min, max);
3646 cmp = compare_values (min, max);
3647 if (cmp == -2 || cmp == 1)
3649 /* If the new range has its limits swapped around (MIN > MAX),
3650 then the operation caused one of them to wrap around, mark
3651 the new range VARYING. */
3652 set_value_range_to_varying (vr);
3654 else
3655 set_value_range (vr, vr0.type, min, max, NULL);
3656 return;
3659 /* For unhandled operations fall back to varying. */
3660 set_value_range_to_varying (vr);
3661 return;
3665 /* Extract range information from a unary expression CODE OP0 based on
3666 the range of its operand with resulting type TYPE.
3667 The resulting range is stored in *VR. */
3669 static void
3670 extract_range_from_unary_expr (value_range *vr, enum tree_code code,
3671 tree type, tree op0)
3673 value_range vr0 = VR_INITIALIZER;
3675 /* Get value ranges for the operand. For constant operands, create
3676 a new value range with the operand to simplify processing. */
3677 if (TREE_CODE (op0) == SSA_NAME)
3678 vr0 = *(get_value_range (op0));
3679 else if (is_gimple_min_invariant (op0))
3680 set_value_range_to_value (&vr0, op0, NULL);
3681 else
3682 set_value_range_to_varying (&vr0);
3684 extract_range_from_unary_expr_1 (vr, code, type, &vr0, TREE_TYPE (op0));
3688 /* Extract range information from a conditional expression STMT based on
3689 the ranges of each of its operands and the expression code. */
3691 static void
3692 extract_range_from_cond_expr (value_range *vr, gassign *stmt)
3694 tree op0, op1;
3695 value_range vr0 = VR_INITIALIZER;
3696 value_range vr1 = VR_INITIALIZER;
3698 /* Get value ranges for each operand. For constant operands, create
3699 a new value range with the operand to simplify processing. */
3700 op0 = gimple_assign_rhs2 (stmt);
3701 if (TREE_CODE (op0) == SSA_NAME)
3702 vr0 = *(get_value_range (op0));
3703 else if (is_gimple_min_invariant (op0))
3704 set_value_range_to_value (&vr0, op0, NULL);
3705 else
3706 set_value_range_to_varying (&vr0);
3708 op1 = gimple_assign_rhs3 (stmt);
3709 if (TREE_CODE (op1) == SSA_NAME)
3710 vr1 = *(get_value_range (op1));
3711 else if (is_gimple_min_invariant (op1))
3712 set_value_range_to_value (&vr1, op1, NULL);
3713 else
3714 set_value_range_to_varying (&vr1);
3716 /* The resulting value range is the union of the operand ranges */
3717 copy_value_range (vr, &vr0);
3718 vrp_meet (vr, &vr1);
3722 /* Extract range information from a comparison expression EXPR based
3723 on the range of its operand and the expression code. */
3725 static void
3726 extract_range_from_comparison (value_range *vr, enum tree_code code,
3727 tree type, tree op0, tree op1)
3729 bool sop = false;
3730 tree val;
3732 val = vrp_evaluate_conditional_warnv_with_ops (code, op0, op1, false, &sop,
3733 NULL);
3735 /* A disadvantage of using a special infinity as an overflow
3736 representation is that we lose the ability to record overflow
3737 when we don't have an infinity. So we have to ignore a result
3738 which relies on overflow. */
3740 if (val && !is_overflow_infinity (val) && !sop)
3742 /* Since this expression was found on the RHS of an assignment,
3743 its type may be different from _Bool. Convert VAL to EXPR's
3744 type. */
3745 val = fold_convert (type, val);
3746 if (is_gimple_min_invariant (val))
3747 set_value_range_to_value (vr, val, vr->equiv);
3748 else
3749 set_value_range (vr, VR_RANGE, val, val, vr->equiv);
3751 else
3752 /* The result of a comparison is always true or false. */
3753 set_value_range_to_truthvalue (vr, type);
3756 /* Helper function for simplify_internal_call_using_ranges and
3757 extract_range_basic. Return true if OP0 SUBCODE OP1 for
3758 SUBCODE {PLUS,MINUS,MULT}_EXPR is known to never overflow or
3759 always overflow. Set *OVF to true if it is known to always
3760 overflow. */
3762 static bool
3763 check_for_binary_op_overflow (enum tree_code subcode, tree type,
3764 tree op0, tree op1, bool *ovf)
3766 value_range vr0 = VR_INITIALIZER;
3767 value_range vr1 = VR_INITIALIZER;
3768 if (TREE_CODE (op0) == SSA_NAME)
3769 vr0 = *get_value_range (op0);
3770 else if (TREE_CODE (op0) == INTEGER_CST)
3771 set_value_range_to_value (&vr0, op0, NULL);
3772 else
3773 set_value_range_to_varying (&vr0);
3775 if (TREE_CODE (op1) == SSA_NAME)
3776 vr1 = *get_value_range (op1);
3777 else if (TREE_CODE (op1) == INTEGER_CST)
3778 set_value_range_to_value (&vr1, op1, NULL);
3779 else
3780 set_value_range_to_varying (&vr1);
3782 if (!range_int_cst_p (&vr0)
3783 || TREE_OVERFLOW (vr0.min)
3784 || TREE_OVERFLOW (vr0.max))
3786 vr0.min = vrp_val_min (TREE_TYPE (op0));
3787 vr0.max = vrp_val_max (TREE_TYPE (op0));
3789 if (!range_int_cst_p (&vr1)
3790 || TREE_OVERFLOW (vr1.min)
3791 || TREE_OVERFLOW (vr1.max))
3793 vr1.min = vrp_val_min (TREE_TYPE (op1));
3794 vr1.max = vrp_val_max (TREE_TYPE (op1));
3796 *ovf = arith_overflowed_p (subcode, type, vr0.min,
3797 subcode == MINUS_EXPR ? vr1.max : vr1.min);
3798 if (arith_overflowed_p (subcode, type, vr0.max,
3799 subcode == MINUS_EXPR ? vr1.min : vr1.max) != *ovf)
3800 return false;
3801 if (subcode == MULT_EXPR)
3803 if (arith_overflowed_p (subcode, type, vr0.min, vr1.max) != *ovf
3804 || arith_overflowed_p (subcode, type, vr0.max, vr1.min) != *ovf)
3805 return false;
3807 if (*ovf)
3809 /* So far we found that there is an overflow on the boundaries.
3810 That doesn't prove that there is an overflow even for all values
3811 in between the boundaries. For that compute widest_int range
3812 of the result and see if it doesn't overlap the range of
3813 type. */
3814 widest_int wmin, wmax;
3815 widest_int w[4];
3816 int i;
3817 w[0] = wi::to_widest (vr0.min);
3818 w[1] = wi::to_widest (vr0.max);
3819 w[2] = wi::to_widest (vr1.min);
3820 w[3] = wi::to_widest (vr1.max);
3821 for (i = 0; i < 4; i++)
3823 widest_int wt;
3824 switch (subcode)
3826 case PLUS_EXPR:
3827 wt = wi::add (w[i & 1], w[2 + (i & 2) / 2]);
3828 break;
3829 case MINUS_EXPR:
3830 wt = wi::sub (w[i & 1], w[2 + (i & 2) / 2]);
3831 break;
3832 case MULT_EXPR:
3833 wt = wi::mul (w[i & 1], w[2 + (i & 2) / 2]);
3834 break;
3835 default:
3836 gcc_unreachable ();
3838 if (i == 0)
3840 wmin = wt;
3841 wmax = wt;
3843 else
3845 wmin = wi::smin (wmin, wt);
3846 wmax = wi::smax (wmax, wt);
3849 /* The result of op0 CODE op1 is known to be in range
3850 [wmin, wmax]. */
3851 widest_int wtmin = wi::to_widest (vrp_val_min (type));
3852 widest_int wtmax = wi::to_widest (vrp_val_max (type));
3853 /* If all values in [wmin, wmax] are smaller than
3854 [wtmin, wtmax] or all are larger than [wtmin, wtmax],
3855 the arithmetic operation will always overflow. */
3856 if (wi::lts_p (wmax, wtmin) || wi::gts_p (wmin, wtmax))
3857 return true;
3858 return false;
3860 return true;
3863 /* Try to derive a nonnegative or nonzero range out of STMT relying
3864 primarily on generic routines in fold in conjunction with range data.
3865 Store the result in *VR */
3867 static void
3868 extract_range_basic (value_range *vr, gimple *stmt)
3870 bool sop = false;
3871 tree type = gimple_expr_type (stmt);
3873 if (gimple_call_builtin_p (stmt, BUILT_IN_NORMAL))
3875 tree fndecl = gimple_call_fndecl (stmt), arg;
3876 int mini, maxi, zerov = 0, prec;
3878 switch (DECL_FUNCTION_CODE (fndecl))
3880 case BUILT_IN_CONSTANT_P:
3881 /* If the call is __builtin_constant_p and the argument is a
3882 function parameter resolve it to false. This avoids bogus
3883 array bound warnings.
3884 ??? We could do this as early as inlining is finished. */
3885 arg = gimple_call_arg (stmt, 0);
3886 if (TREE_CODE (arg) == SSA_NAME
3887 && SSA_NAME_IS_DEFAULT_DEF (arg)
3888 && TREE_CODE (SSA_NAME_VAR (arg)) == PARM_DECL)
3890 set_value_range_to_null (vr, type);
3891 return;
3893 break;
3894 /* Both __builtin_ffs* and __builtin_popcount return
3895 [0, prec]. */
3896 CASE_INT_FN (BUILT_IN_FFS):
3897 CASE_INT_FN (BUILT_IN_POPCOUNT):
3898 arg = gimple_call_arg (stmt, 0);
3899 prec = TYPE_PRECISION (TREE_TYPE (arg));
3900 mini = 0;
3901 maxi = prec;
3902 if (TREE_CODE (arg) == SSA_NAME)
3904 value_range *vr0 = get_value_range (arg);
3905 /* If arg is non-zero, then ffs or popcount
3906 are non-zero. */
3907 if (((vr0->type == VR_RANGE
3908 && range_includes_zero_p (vr0->min, vr0->max) == 0)
3909 || (vr0->type == VR_ANTI_RANGE
3910 && range_includes_zero_p (vr0->min, vr0->max) == 1))
3911 && !is_overflow_infinity (vr0->min)
3912 && !is_overflow_infinity (vr0->max))
3913 mini = 1;
3914 /* If some high bits are known to be zero,
3915 we can decrease the maximum. */
3916 if (vr0->type == VR_RANGE
3917 && TREE_CODE (vr0->max) == INTEGER_CST
3918 && !operand_less_p (vr0->min,
3919 build_zero_cst (TREE_TYPE (vr0->min)))
3920 && !is_overflow_infinity (vr0->max))
3921 maxi = tree_floor_log2 (vr0->max) + 1;
3923 goto bitop_builtin;
3924 /* __builtin_parity* returns [0, 1]. */
3925 CASE_INT_FN (BUILT_IN_PARITY):
3926 mini = 0;
3927 maxi = 1;
3928 goto bitop_builtin;
3929 /* __builtin_c[lt]z* return [0, prec-1], except for
3930 when the argument is 0, but that is undefined behavior.
3931 On many targets where the CLZ RTL or optab value is defined
3932 for 0 the value is prec, so include that in the range
3933 by default. */
3934 CASE_INT_FN (BUILT_IN_CLZ):
3935 arg = gimple_call_arg (stmt, 0);
3936 prec = TYPE_PRECISION (TREE_TYPE (arg));
3937 mini = 0;
3938 maxi = prec;
3939 if (optab_handler (clz_optab, TYPE_MODE (TREE_TYPE (arg)))
3940 != CODE_FOR_nothing
3941 && CLZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (TREE_TYPE (arg)),
3942 zerov)
3943 /* Handle only the single common value. */
3944 && zerov != prec)
3945 /* Magic value to give up, unless vr0 proves
3946 arg is non-zero. */
3947 mini = -2;
3948 if (TREE_CODE (arg) == SSA_NAME)
3950 value_range *vr0 = get_value_range (arg);
3951 /* From clz of VR_RANGE minimum we can compute
3952 result maximum. */
3953 if (vr0->type == VR_RANGE
3954 && TREE_CODE (vr0->min) == INTEGER_CST
3955 && !is_overflow_infinity (vr0->min))
3957 maxi = prec - 1 - tree_floor_log2 (vr0->min);
3958 if (maxi != prec)
3959 mini = 0;
3961 else if (vr0->type == VR_ANTI_RANGE
3962 && integer_zerop (vr0->min)
3963 && !is_overflow_infinity (vr0->min))
3965 maxi = prec - 1;
3966 mini = 0;
3968 if (mini == -2)
3969 break;
3970 /* From clz of VR_RANGE maximum we can compute
3971 result minimum. */
3972 if (vr0->type == VR_RANGE
3973 && TREE_CODE (vr0->max) == INTEGER_CST
3974 && !is_overflow_infinity (vr0->max))
3976 mini = prec - 1 - tree_floor_log2 (vr0->max);
3977 if (mini == prec)
3978 break;
3981 if (mini == -2)
3982 break;
3983 goto bitop_builtin;
3984 /* __builtin_ctz* return [0, prec-1], except for
3985 when the argument is 0, but that is undefined behavior.
3986 If there is a ctz optab for this mode and
3987 CTZ_DEFINED_VALUE_AT_ZERO, include that in the range,
3988 otherwise just assume 0 won't be seen. */
3989 CASE_INT_FN (BUILT_IN_CTZ):
3990 arg = gimple_call_arg (stmt, 0);
3991 prec = TYPE_PRECISION (TREE_TYPE (arg));
3992 mini = 0;
3993 maxi = prec - 1;
3994 if (optab_handler (ctz_optab, TYPE_MODE (TREE_TYPE (arg)))
3995 != CODE_FOR_nothing
3996 && CTZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (TREE_TYPE (arg)),
3997 zerov))
3999 /* Handle only the two common values. */
4000 if (zerov == -1)
4001 mini = -1;
4002 else if (zerov == prec)
4003 maxi = prec;
4004 else
4005 /* Magic value to give up, unless vr0 proves
4006 arg is non-zero. */
4007 mini = -2;
4009 if (TREE_CODE (arg) == SSA_NAME)
4011 value_range *vr0 = get_value_range (arg);
4012 /* If arg is non-zero, then use [0, prec - 1]. */
4013 if (((vr0->type == VR_RANGE
4014 && integer_nonzerop (vr0->min))
4015 || (vr0->type == VR_ANTI_RANGE
4016 && integer_zerop (vr0->min)))
4017 && !is_overflow_infinity (vr0->min))
4019 mini = 0;
4020 maxi = prec - 1;
4022 /* If some high bits are known to be zero,
4023 we can decrease the result maximum. */
4024 if (vr0->type == VR_RANGE
4025 && TREE_CODE (vr0->max) == INTEGER_CST
4026 && !is_overflow_infinity (vr0->max))
4028 maxi = tree_floor_log2 (vr0->max);
4029 /* For vr0 [0, 0] give up. */
4030 if (maxi == -1)
4031 break;
4034 if (mini == -2)
4035 break;
4036 goto bitop_builtin;
4037 /* __builtin_clrsb* returns [0, prec-1]. */
4038 CASE_INT_FN (BUILT_IN_CLRSB):
4039 arg = gimple_call_arg (stmt, 0);
4040 prec = TYPE_PRECISION (TREE_TYPE (arg));
4041 mini = 0;
4042 maxi = prec - 1;
4043 goto bitop_builtin;
4044 bitop_builtin:
4045 set_value_range (vr, VR_RANGE, build_int_cst (type, mini),
4046 build_int_cst (type, maxi), NULL);
4047 return;
4048 default:
4049 break;
4052 else if (is_gimple_call (stmt) && gimple_call_internal_p (stmt))
4054 enum tree_code subcode = ERROR_MARK;
4055 switch (gimple_call_internal_fn (stmt))
4057 case IFN_UBSAN_CHECK_ADD:
4058 subcode = PLUS_EXPR;
4059 break;
4060 case IFN_UBSAN_CHECK_SUB:
4061 subcode = MINUS_EXPR;
4062 break;
4063 case IFN_UBSAN_CHECK_MUL:
4064 subcode = MULT_EXPR;
4065 break;
4066 default:
4067 break;
4069 if (subcode != ERROR_MARK)
4071 bool saved_flag_wrapv = flag_wrapv;
4072 /* Pretend the arithmetics is wrapping. If there is
4073 any overflow, we'll complain, but will actually do
4074 wrapping operation. */
4075 flag_wrapv = 1;
4076 extract_range_from_binary_expr (vr, subcode, type,
4077 gimple_call_arg (stmt, 0),
4078 gimple_call_arg (stmt, 1));
4079 flag_wrapv = saved_flag_wrapv;
4081 /* If for both arguments vrp_valueize returned non-NULL,
4082 this should have been already folded and if not, it
4083 wasn't folded because of overflow. Avoid removing the
4084 UBSAN_CHECK_* calls in that case. */
4085 if (vr->type == VR_RANGE
4086 && (vr->min == vr->max
4087 || operand_equal_p (vr->min, vr->max, 0)))
4088 set_value_range_to_varying (vr);
4089 return;
4092 /* Handle extraction of the two results (result of arithmetics and
4093 a flag whether arithmetics overflowed) from {ADD,SUB,MUL}_OVERFLOW
4094 internal function. */
4095 else if (is_gimple_assign (stmt)
4096 && (gimple_assign_rhs_code (stmt) == REALPART_EXPR
4097 || gimple_assign_rhs_code (stmt) == IMAGPART_EXPR)
4098 && INTEGRAL_TYPE_P (type))
4100 enum tree_code code = gimple_assign_rhs_code (stmt);
4101 tree op = gimple_assign_rhs1 (stmt);
4102 if (TREE_CODE (op) == code && TREE_CODE (TREE_OPERAND (op, 0)) == SSA_NAME)
4104 gimple *g = SSA_NAME_DEF_STMT (TREE_OPERAND (op, 0));
4105 if (is_gimple_call (g) && gimple_call_internal_p (g))
4107 enum tree_code subcode = ERROR_MARK;
4108 switch (gimple_call_internal_fn (g))
4110 case IFN_ADD_OVERFLOW:
4111 subcode = PLUS_EXPR;
4112 break;
4113 case IFN_SUB_OVERFLOW:
4114 subcode = MINUS_EXPR;
4115 break;
4116 case IFN_MUL_OVERFLOW:
4117 subcode = MULT_EXPR;
4118 break;
4119 default:
4120 break;
4122 if (subcode != ERROR_MARK)
4124 tree op0 = gimple_call_arg (g, 0);
4125 tree op1 = gimple_call_arg (g, 1);
4126 if (code == IMAGPART_EXPR)
4128 bool ovf = false;
4129 if (check_for_binary_op_overflow (subcode, type,
4130 op0, op1, &ovf))
4131 set_value_range_to_value (vr,
4132 build_int_cst (type, ovf),
4133 NULL);
4134 else
4135 set_value_range (vr, VR_RANGE, build_int_cst (type, 0),
4136 build_int_cst (type, 1), NULL);
4138 else if (types_compatible_p (type, TREE_TYPE (op0))
4139 && types_compatible_p (type, TREE_TYPE (op1)))
4141 bool saved_flag_wrapv = flag_wrapv;
4142 /* Pretend the arithmetics is wrapping. If there is
4143 any overflow, IMAGPART_EXPR will be set. */
4144 flag_wrapv = 1;
4145 extract_range_from_binary_expr (vr, subcode, type,
4146 op0, op1);
4147 flag_wrapv = saved_flag_wrapv;
4149 else
4151 value_range vr0 = VR_INITIALIZER;
4152 value_range vr1 = VR_INITIALIZER;
4153 bool saved_flag_wrapv = flag_wrapv;
4154 /* Pretend the arithmetics is wrapping. If there is
4155 any overflow, IMAGPART_EXPR will be set. */
4156 flag_wrapv = 1;
4157 extract_range_from_unary_expr (&vr0, NOP_EXPR,
4158 type, op0);
4159 extract_range_from_unary_expr (&vr1, NOP_EXPR,
4160 type, op1);
4161 extract_range_from_binary_expr_1 (vr, subcode, type,
4162 &vr0, &vr1);
4163 flag_wrapv = saved_flag_wrapv;
4165 return;
4170 if (INTEGRAL_TYPE_P (type)
4171 && gimple_stmt_nonnegative_warnv_p (stmt, &sop))
4172 set_value_range_to_nonnegative (vr, type,
4173 sop || stmt_overflow_infinity (stmt));
4174 else if (vrp_stmt_computes_nonzero (stmt, &sop)
4175 && !sop)
4176 set_value_range_to_nonnull (vr, type);
4177 else
4178 set_value_range_to_varying (vr);
4182 /* Try to compute a useful range out of assignment STMT and store it
4183 in *VR. */
4185 static void
4186 extract_range_from_assignment (value_range *vr, gassign *stmt)
4188 enum tree_code code = gimple_assign_rhs_code (stmt);
4190 if (code == ASSERT_EXPR)
4191 extract_range_from_assert (vr, gimple_assign_rhs1 (stmt));
4192 else if (code == SSA_NAME)
4193 extract_range_from_ssa_name (vr, gimple_assign_rhs1 (stmt));
4194 else if (TREE_CODE_CLASS (code) == tcc_binary)
4195 extract_range_from_binary_expr (vr, gimple_assign_rhs_code (stmt),
4196 gimple_expr_type (stmt),
4197 gimple_assign_rhs1 (stmt),
4198 gimple_assign_rhs2 (stmt));
4199 else if (TREE_CODE_CLASS (code) == tcc_unary)
4200 extract_range_from_unary_expr (vr, gimple_assign_rhs_code (stmt),
4201 gimple_expr_type (stmt),
4202 gimple_assign_rhs1 (stmt));
4203 else if (code == COND_EXPR)
4204 extract_range_from_cond_expr (vr, stmt);
4205 else if (TREE_CODE_CLASS (code) == tcc_comparison)
4206 extract_range_from_comparison (vr, gimple_assign_rhs_code (stmt),
4207 gimple_expr_type (stmt),
4208 gimple_assign_rhs1 (stmt),
4209 gimple_assign_rhs2 (stmt));
4210 else if (get_gimple_rhs_class (code) == GIMPLE_SINGLE_RHS
4211 && is_gimple_min_invariant (gimple_assign_rhs1 (stmt)))
4212 set_value_range_to_value (vr, gimple_assign_rhs1 (stmt), NULL);
4213 else
4214 set_value_range_to_varying (vr);
4216 if (vr->type == VR_VARYING)
4217 extract_range_basic (vr, stmt);
4220 /* Given a range VR, a LOOP and a variable VAR, determine whether it
4221 would be profitable to adjust VR using scalar evolution information
4222 for VAR. If so, update VR with the new limits. */
4224 static void
4225 adjust_range_with_scev (value_range *vr, struct loop *loop,
4226 gimple *stmt, tree var)
4228 tree init, step, chrec, tmin, tmax, min, max, type, tem;
4229 enum ev_direction dir;
4231 /* TODO. Don't adjust anti-ranges. An anti-range may provide
4232 better opportunities than a regular range, but I'm not sure. */
4233 if (vr->type == VR_ANTI_RANGE)
4234 return;
4236 chrec = instantiate_parameters (loop, analyze_scalar_evolution (loop, var));
4238 /* Like in PR19590, scev can return a constant function. */
4239 if (is_gimple_min_invariant (chrec))
4241 set_value_range_to_value (vr, chrec, vr->equiv);
4242 return;
4245 if (TREE_CODE (chrec) != POLYNOMIAL_CHREC)
4246 return;
4248 init = initial_condition_in_loop_num (chrec, loop->num);
4249 tem = op_with_constant_singleton_value_range (init);
4250 if (tem)
4251 init = tem;
4252 step = evolution_part_in_loop_num (chrec, loop->num);
4253 tem = op_with_constant_singleton_value_range (step);
4254 if (tem)
4255 step = tem;
4257 /* If STEP is symbolic, we can't know whether INIT will be the
4258 minimum or maximum value in the range. Also, unless INIT is
4259 a simple expression, compare_values and possibly other functions
4260 in tree-vrp won't be able to handle it. */
4261 if (step == NULL_TREE
4262 || !is_gimple_min_invariant (step)
4263 || !valid_value_p (init))
4264 return;
4266 dir = scev_direction (chrec);
4267 if (/* Do not adjust ranges if we do not know whether the iv increases
4268 or decreases, ... */
4269 dir == EV_DIR_UNKNOWN
4270 /* ... or if it may wrap. */
4271 || scev_probably_wraps_p (init, step, stmt, get_chrec_loop (chrec),
4272 true))
4273 return;
4275 /* We use TYPE_MIN_VALUE and TYPE_MAX_VALUE here instead of
4276 negative_overflow_infinity and positive_overflow_infinity,
4277 because we have concluded that the loop probably does not
4278 wrap. */
4280 type = TREE_TYPE (var);
4281 if (POINTER_TYPE_P (type) || !TYPE_MIN_VALUE (type))
4282 tmin = lower_bound_in_type (type, type);
4283 else
4284 tmin = TYPE_MIN_VALUE (type);
4285 if (POINTER_TYPE_P (type) || !TYPE_MAX_VALUE (type))
4286 tmax = upper_bound_in_type (type, type);
4287 else
4288 tmax = TYPE_MAX_VALUE (type);
4290 /* Try to use estimated number of iterations for the loop to constrain the
4291 final value in the evolution. */
4292 if (TREE_CODE (step) == INTEGER_CST
4293 && is_gimple_val (init)
4294 && (TREE_CODE (init) != SSA_NAME
4295 || get_value_range (init)->type == VR_RANGE))
4297 widest_int nit;
4299 /* We are only entering here for loop header PHI nodes, so using
4300 the number of latch executions is the correct thing to use. */
4301 if (max_loop_iterations (loop, &nit))
4303 value_range maxvr = VR_INITIALIZER;
4304 signop sgn = TYPE_SIGN (TREE_TYPE (step));
4305 bool overflow;
4307 widest_int wtmp = wi::mul (wi::to_widest (step), nit, sgn,
4308 &overflow);
4309 /* If the multiplication overflowed we can't do a meaningful
4310 adjustment. Likewise if the result doesn't fit in the type
4311 of the induction variable. For a signed type we have to
4312 check whether the result has the expected signedness which
4313 is that of the step as number of iterations is unsigned. */
4314 if (!overflow
4315 && wi::fits_to_tree_p (wtmp, TREE_TYPE (init))
4316 && (sgn == UNSIGNED
4317 || wi::gts_p (wtmp, 0) == wi::gts_p (step, 0)))
4319 tem = wide_int_to_tree (TREE_TYPE (init), wtmp);
4320 extract_range_from_binary_expr (&maxvr, PLUS_EXPR,
4321 TREE_TYPE (init), init, tem);
4322 /* Likewise if the addition did. */
4323 if (maxvr.type == VR_RANGE)
4325 tmin = maxvr.min;
4326 tmax = maxvr.max;
4332 if (vr->type == VR_VARYING || vr->type == VR_UNDEFINED)
4334 min = tmin;
4335 max = tmax;
4337 /* For VARYING or UNDEFINED ranges, just about anything we get
4338 from scalar evolutions should be better. */
4340 if (dir == EV_DIR_DECREASES)
4341 max = init;
4342 else
4343 min = init;
4345 else if (vr->type == VR_RANGE)
4347 min = vr->min;
4348 max = vr->max;
4350 if (dir == EV_DIR_DECREASES)
4352 /* INIT is the maximum value. If INIT is lower than VR->MAX
4353 but no smaller than VR->MIN, set VR->MAX to INIT. */
4354 if (compare_values (init, max) == -1)
4355 max = init;
4357 /* According to the loop information, the variable does not
4358 overflow. If we think it does, probably because of an
4359 overflow due to arithmetic on a different INF value,
4360 reset now. */
4361 if (is_negative_overflow_infinity (min)
4362 || compare_values (min, tmin) == -1)
4363 min = tmin;
4366 else
4368 /* If INIT is bigger than VR->MIN, set VR->MIN to INIT. */
4369 if (compare_values (init, min) == 1)
4370 min = init;
4372 if (is_positive_overflow_infinity (max)
4373 || compare_values (tmax, max) == -1)
4374 max = tmax;
4377 else
4378 return;
4380 /* If we just created an invalid range with the minimum
4381 greater than the maximum, we fail conservatively.
4382 This should happen only in unreachable
4383 parts of code, or for invalid programs. */
4384 if (compare_values (min, max) == 1
4385 || (is_negative_overflow_infinity (min)
4386 && is_positive_overflow_infinity (max)))
4387 return;
4389 set_value_range (vr, VR_RANGE, min, max, vr->equiv);
4393 /* Given two numeric value ranges VR0, VR1 and a comparison code COMP:
4395 - Return BOOLEAN_TRUE_NODE if VR0 COMP VR1 always returns true for
4396 all the values in the ranges.
4398 - Return BOOLEAN_FALSE_NODE if the comparison always returns false.
4400 - Return NULL_TREE if it is not always possible to determine the
4401 value of the comparison.
4403 Also set *STRICT_OVERFLOW_P to indicate whether a range with an
4404 overflow infinity was used in the test. */
4407 static tree
4408 compare_ranges (enum tree_code comp, value_range *vr0, value_range *vr1,
4409 bool *strict_overflow_p)
4411 /* VARYING or UNDEFINED ranges cannot be compared. */
4412 if (vr0->type == VR_VARYING
4413 || vr0->type == VR_UNDEFINED
4414 || vr1->type == VR_VARYING
4415 || vr1->type == VR_UNDEFINED)
4416 return NULL_TREE;
4418 /* Anti-ranges need to be handled separately. */
4419 if (vr0->type == VR_ANTI_RANGE || vr1->type == VR_ANTI_RANGE)
4421 /* If both are anti-ranges, then we cannot compute any
4422 comparison. */
4423 if (vr0->type == VR_ANTI_RANGE && vr1->type == VR_ANTI_RANGE)
4424 return NULL_TREE;
4426 /* These comparisons are never statically computable. */
4427 if (comp == GT_EXPR
4428 || comp == GE_EXPR
4429 || comp == LT_EXPR
4430 || comp == LE_EXPR)
4431 return NULL_TREE;
4433 /* Equality can be computed only between a range and an
4434 anti-range. ~[VAL1, VAL2] == [VAL1, VAL2] is always false. */
4435 if (vr0->type == VR_RANGE)
4437 /* To simplify processing, make VR0 the anti-range. */
4438 value_range *tmp = vr0;
4439 vr0 = vr1;
4440 vr1 = tmp;
4443 gcc_assert (comp == NE_EXPR || comp == EQ_EXPR);
4445 if (compare_values_warnv (vr0->min, vr1->min, strict_overflow_p) == 0
4446 && compare_values_warnv (vr0->max, vr1->max, strict_overflow_p) == 0)
4447 return (comp == NE_EXPR) ? boolean_true_node : boolean_false_node;
4449 return NULL_TREE;
4452 if (!usable_range_p (vr0, strict_overflow_p)
4453 || !usable_range_p (vr1, strict_overflow_p))
4454 return NULL_TREE;
4456 /* Simplify processing. If COMP is GT_EXPR or GE_EXPR, switch the
4457 operands around and change the comparison code. */
4458 if (comp == GT_EXPR || comp == GE_EXPR)
4460 comp = (comp == GT_EXPR) ? LT_EXPR : LE_EXPR;
4461 std::swap (vr0, vr1);
4464 if (comp == EQ_EXPR)
4466 /* Equality may only be computed if both ranges represent
4467 exactly one value. */
4468 if (compare_values_warnv (vr0->min, vr0->max, strict_overflow_p) == 0
4469 && compare_values_warnv (vr1->min, vr1->max, strict_overflow_p) == 0)
4471 int cmp_min = compare_values_warnv (vr0->min, vr1->min,
4472 strict_overflow_p);
4473 int cmp_max = compare_values_warnv (vr0->max, vr1->max,
4474 strict_overflow_p);
4475 if (cmp_min == 0 && cmp_max == 0)
4476 return boolean_true_node;
4477 else if (cmp_min != -2 && cmp_max != -2)
4478 return boolean_false_node;
4480 /* If [V0_MIN, V1_MAX] < [V1_MIN, V1_MAX] then V0 != V1. */
4481 else if (compare_values_warnv (vr0->min, vr1->max,
4482 strict_overflow_p) == 1
4483 || compare_values_warnv (vr1->min, vr0->max,
4484 strict_overflow_p) == 1)
4485 return boolean_false_node;
4487 return NULL_TREE;
4489 else if (comp == NE_EXPR)
4491 int cmp1, cmp2;
4493 /* If VR0 is completely to the left or completely to the right
4494 of VR1, they are always different. Notice that we need to
4495 make sure that both comparisons yield similar results to
4496 avoid comparing values that cannot be compared at
4497 compile-time. */
4498 cmp1 = compare_values_warnv (vr0->max, vr1->min, strict_overflow_p);
4499 cmp2 = compare_values_warnv (vr0->min, vr1->max, strict_overflow_p);
4500 if ((cmp1 == -1 && cmp2 == -1) || (cmp1 == 1 && cmp2 == 1))
4501 return boolean_true_node;
4503 /* If VR0 and VR1 represent a single value and are identical,
4504 return false. */
4505 else if (compare_values_warnv (vr0->min, vr0->max,
4506 strict_overflow_p) == 0
4507 && compare_values_warnv (vr1->min, vr1->max,
4508 strict_overflow_p) == 0
4509 && compare_values_warnv (vr0->min, vr1->min,
4510 strict_overflow_p) == 0
4511 && compare_values_warnv (vr0->max, vr1->max,
4512 strict_overflow_p) == 0)
4513 return boolean_false_node;
4515 /* Otherwise, they may or may not be different. */
4516 else
4517 return NULL_TREE;
4519 else if (comp == LT_EXPR || comp == LE_EXPR)
4521 int tst;
4523 /* If VR0 is to the left of VR1, return true. */
4524 tst = compare_values_warnv (vr0->max, vr1->min, strict_overflow_p);
4525 if ((comp == LT_EXPR && tst == -1)
4526 || (comp == LE_EXPR && (tst == -1 || tst == 0)))
4528 if (overflow_infinity_range_p (vr0)
4529 || overflow_infinity_range_p (vr1))
4530 *strict_overflow_p = true;
4531 return boolean_true_node;
4534 /* If VR0 is to the right of VR1, return false. */
4535 tst = compare_values_warnv (vr0->min, vr1->max, strict_overflow_p);
4536 if ((comp == LT_EXPR && (tst == 0 || tst == 1))
4537 || (comp == LE_EXPR && tst == 1))
4539 if (overflow_infinity_range_p (vr0)
4540 || overflow_infinity_range_p (vr1))
4541 *strict_overflow_p = true;
4542 return boolean_false_node;
4545 /* Otherwise, we don't know. */
4546 return NULL_TREE;
4549 gcc_unreachable ();
4553 /* Given a value range VR, a value VAL and a comparison code COMP, return
4554 BOOLEAN_TRUE_NODE if VR COMP VAL always returns true for all the
4555 values in VR. Return BOOLEAN_FALSE_NODE if the comparison
4556 always returns false. Return NULL_TREE if it is not always
4557 possible to determine the value of the comparison. Also set
4558 *STRICT_OVERFLOW_P to indicate whether a range with an overflow
4559 infinity was used in the test. */
4561 static tree
4562 compare_range_with_value (enum tree_code comp, value_range *vr, tree val,
4563 bool *strict_overflow_p)
4565 if (vr->type == VR_VARYING || vr->type == VR_UNDEFINED)
4566 return NULL_TREE;
4568 /* Anti-ranges need to be handled separately. */
4569 if (vr->type == VR_ANTI_RANGE)
4571 /* For anti-ranges, the only predicates that we can compute at
4572 compile time are equality and inequality. */
4573 if (comp == GT_EXPR
4574 || comp == GE_EXPR
4575 || comp == LT_EXPR
4576 || comp == LE_EXPR)
4577 return NULL_TREE;
4579 /* ~[VAL_1, VAL_2] OP VAL is known if VAL_1 <= VAL <= VAL_2. */
4580 if (value_inside_range (val, vr->min, vr->max) == 1)
4581 return (comp == NE_EXPR) ? boolean_true_node : boolean_false_node;
4583 return NULL_TREE;
4586 if (!usable_range_p (vr, strict_overflow_p))
4587 return NULL_TREE;
4589 if (comp == EQ_EXPR)
4591 /* EQ_EXPR may only be computed if VR represents exactly
4592 one value. */
4593 if (compare_values_warnv (vr->min, vr->max, strict_overflow_p) == 0)
4595 int cmp = compare_values_warnv (vr->min, val, strict_overflow_p);
4596 if (cmp == 0)
4597 return boolean_true_node;
4598 else if (cmp == -1 || cmp == 1 || cmp == 2)
4599 return boolean_false_node;
4601 else if (compare_values_warnv (val, vr->min, strict_overflow_p) == -1
4602 || compare_values_warnv (vr->max, val, strict_overflow_p) == -1)
4603 return boolean_false_node;
4605 return NULL_TREE;
4607 else if (comp == NE_EXPR)
4609 /* If VAL is not inside VR, then they are always different. */
4610 if (compare_values_warnv (vr->max, val, strict_overflow_p) == -1
4611 || compare_values_warnv (vr->min, val, strict_overflow_p) == 1)
4612 return boolean_true_node;
4614 /* If VR represents exactly one value equal to VAL, then return
4615 false. */
4616 if (compare_values_warnv (vr->min, vr->max, strict_overflow_p) == 0
4617 && compare_values_warnv (vr->min, val, strict_overflow_p) == 0)
4618 return boolean_false_node;
4620 /* Otherwise, they may or may not be different. */
4621 return NULL_TREE;
4623 else if (comp == LT_EXPR || comp == LE_EXPR)
4625 int tst;
4627 /* If VR is to the left of VAL, return true. */
4628 tst = compare_values_warnv (vr->max, val, strict_overflow_p);
4629 if ((comp == LT_EXPR && tst == -1)
4630 || (comp == LE_EXPR && (tst == -1 || tst == 0)))
4632 if (overflow_infinity_range_p (vr))
4633 *strict_overflow_p = true;
4634 return boolean_true_node;
4637 /* If VR is to the right of VAL, return false. */
4638 tst = compare_values_warnv (vr->min, val, strict_overflow_p);
4639 if ((comp == LT_EXPR && (tst == 0 || tst == 1))
4640 || (comp == LE_EXPR && tst == 1))
4642 if (overflow_infinity_range_p (vr))
4643 *strict_overflow_p = true;
4644 return boolean_false_node;
4647 /* Otherwise, we don't know. */
4648 return NULL_TREE;
4650 else if (comp == GT_EXPR || comp == GE_EXPR)
4652 int tst;
4654 /* If VR is to the right of VAL, return true. */
4655 tst = compare_values_warnv (vr->min, val, strict_overflow_p);
4656 if ((comp == GT_EXPR && tst == 1)
4657 || (comp == GE_EXPR && (tst == 0 || tst == 1)))
4659 if (overflow_infinity_range_p (vr))
4660 *strict_overflow_p = true;
4661 return boolean_true_node;
4664 /* If VR is to the left of VAL, return false. */
4665 tst = compare_values_warnv (vr->max, val, strict_overflow_p);
4666 if ((comp == GT_EXPR && (tst == -1 || tst == 0))
4667 || (comp == GE_EXPR && tst == -1))
4669 if (overflow_infinity_range_p (vr))
4670 *strict_overflow_p = true;
4671 return boolean_false_node;
4674 /* Otherwise, we don't know. */
4675 return NULL_TREE;
4678 gcc_unreachable ();
4682 /* Debugging dumps. */
4684 void dump_value_range (FILE *, value_range *);
4685 void debug_value_range (value_range *);
4686 void dump_all_value_ranges (FILE *);
4687 void debug_all_value_ranges (void);
4688 void dump_vr_equiv (FILE *, bitmap);
4689 void debug_vr_equiv (bitmap);
4692 /* Dump value range VR to FILE. */
4694 void
4695 dump_value_range (FILE *file, value_range *vr)
4697 if (vr == NULL)
4698 fprintf (file, "[]");
4699 else if (vr->type == VR_UNDEFINED)
4700 fprintf (file, "UNDEFINED");
4701 else if (vr->type == VR_RANGE || vr->type == VR_ANTI_RANGE)
4703 tree type = TREE_TYPE (vr->min);
4705 fprintf (file, "%s[", (vr->type == VR_ANTI_RANGE) ? "~" : "");
4707 if (is_negative_overflow_infinity (vr->min))
4708 fprintf (file, "-INF(OVF)");
4709 else if (INTEGRAL_TYPE_P (type)
4710 && !TYPE_UNSIGNED (type)
4711 && vrp_val_is_min (vr->min))
4712 fprintf (file, "-INF");
4713 else
4714 print_generic_expr (file, vr->min, 0);
4716 fprintf (file, ", ");
4718 if (is_positive_overflow_infinity (vr->max))
4719 fprintf (file, "+INF(OVF)");
4720 else if (INTEGRAL_TYPE_P (type)
4721 && vrp_val_is_max (vr->max))
4722 fprintf (file, "+INF");
4723 else
4724 print_generic_expr (file, vr->max, 0);
4726 fprintf (file, "]");
4728 if (vr->equiv)
4730 bitmap_iterator bi;
4731 unsigned i, c = 0;
4733 fprintf (file, " EQUIVALENCES: { ");
4735 EXECUTE_IF_SET_IN_BITMAP (vr->equiv, 0, i, bi)
4737 print_generic_expr (file, ssa_name (i), 0);
4738 fprintf (file, " ");
4739 c++;
4742 fprintf (file, "} (%u elements)", c);
4745 else if (vr->type == VR_VARYING)
4746 fprintf (file, "VARYING");
4747 else
4748 fprintf (file, "INVALID RANGE");
4752 /* Dump value range VR to stderr. */
4754 DEBUG_FUNCTION void
4755 debug_value_range (value_range *vr)
4757 dump_value_range (stderr, vr);
4758 fprintf (stderr, "\n");
4762 /* Dump value ranges of all SSA_NAMEs to FILE. */
4764 void
4765 dump_all_value_ranges (FILE *file)
4767 size_t i;
4769 for (i = 0; i < num_vr_values; i++)
4771 if (vr_value[i])
4773 print_generic_expr (file, ssa_name (i), 0);
4774 fprintf (file, ": ");
4775 dump_value_range (file, vr_value[i]);
4776 fprintf (file, "\n");
4780 fprintf (file, "\n");
4784 /* Dump all value ranges to stderr. */
4786 DEBUG_FUNCTION void
4787 debug_all_value_ranges (void)
4789 dump_all_value_ranges (stderr);
4793 /* Given a COND_EXPR COND of the form 'V OP W', and an SSA name V,
4794 create a new SSA name N and return the assertion assignment
4795 'N = ASSERT_EXPR <V, V OP W>'. */
4797 static gimple *
4798 build_assert_expr_for (tree cond, tree v)
4800 tree a;
4801 gassign *assertion;
4803 gcc_assert (TREE_CODE (v) == SSA_NAME
4804 && COMPARISON_CLASS_P (cond));
4806 a = build2 (ASSERT_EXPR, TREE_TYPE (v), v, cond);
4807 assertion = gimple_build_assign (NULL_TREE, a);
4809 /* The new ASSERT_EXPR, creates a new SSA name that replaces the
4810 operand of the ASSERT_EXPR. Create it so the new name and the old one
4811 are registered in the replacement table so that we can fix the SSA web
4812 after adding all the ASSERT_EXPRs. */
4813 create_new_def_for (v, assertion, NULL);
4815 return assertion;
4819 /* Return false if EXPR is a predicate expression involving floating
4820 point values. */
4822 static inline bool
4823 fp_predicate (gimple *stmt)
4825 GIMPLE_CHECK (stmt, GIMPLE_COND);
4827 return FLOAT_TYPE_P (TREE_TYPE (gimple_cond_lhs (stmt)));
4830 /* If the range of values taken by OP can be inferred after STMT executes,
4831 return the comparison code (COMP_CODE_P) and value (VAL_P) that
4832 describes the inferred range. Return true if a range could be
4833 inferred. */
4835 static bool
4836 infer_value_range (gimple *stmt, tree op, tree_code *comp_code_p, tree *val_p)
4838 *val_p = NULL_TREE;
4839 *comp_code_p = ERROR_MARK;
4841 /* Do not attempt to infer anything in names that flow through
4842 abnormal edges. */
4843 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op))
4844 return false;
4846 /* Similarly, don't infer anything from statements that may throw
4847 exceptions. ??? Relax this requirement? */
4848 if (stmt_could_throw_p (stmt))
4849 return false;
4851 /* If STMT is the last statement of a basic block with no normal
4852 successors, there is no point inferring anything about any of its
4853 operands. We would not be able to find a proper insertion point
4854 for the assertion, anyway. */
4855 if (stmt_ends_bb_p (stmt))
4857 edge_iterator ei;
4858 edge e;
4860 FOR_EACH_EDGE (e, ei, gimple_bb (stmt)->succs)
4861 if (!(e->flags & EDGE_ABNORMAL))
4862 break;
4863 if (e == NULL)
4864 return false;
4867 if (infer_nonnull_range (stmt, op))
4869 *val_p = build_int_cst (TREE_TYPE (op), 0);
4870 *comp_code_p = NE_EXPR;
4871 return true;
4874 return false;
4878 void dump_asserts_for (FILE *, tree);
4879 void debug_asserts_for (tree);
4880 void dump_all_asserts (FILE *);
4881 void debug_all_asserts (void);
4883 /* Dump all the registered assertions for NAME to FILE. */
4885 void
4886 dump_asserts_for (FILE *file, tree name)
4888 assert_locus *loc;
4890 fprintf (file, "Assertions to be inserted for ");
4891 print_generic_expr (file, name, 0);
4892 fprintf (file, "\n");
4894 loc = asserts_for[SSA_NAME_VERSION (name)];
4895 while (loc)
4897 fprintf (file, "\t");
4898 print_gimple_stmt (file, gsi_stmt (loc->si), 0, 0);
4899 fprintf (file, "\n\tBB #%d", loc->bb->index);
4900 if (loc->e)
4902 fprintf (file, "\n\tEDGE %d->%d", loc->e->src->index,
4903 loc->e->dest->index);
4904 dump_edge_info (file, loc->e, dump_flags, 0);
4906 fprintf (file, "\n\tPREDICATE: ");
4907 print_generic_expr (file, name, 0);
4908 fprintf (file, " %s ", get_tree_code_name (loc->comp_code));
4909 print_generic_expr (file, loc->val, 0);
4910 fprintf (file, "\n\n");
4911 loc = loc->next;
4914 fprintf (file, "\n");
4918 /* Dump all the registered assertions for NAME to stderr. */
4920 DEBUG_FUNCTION void
4921 debug_asserts_for (tree name)
4923 dump_asserts_for (stderr, name);
4927 /* Dump all the registered assertions for all the names to FILE. */
4929 void
4930 dump_all_asserts (FILE *file)
4932 unsigned i;
4933 bitmap_iterator bi;
4935 fprintf (file, "\nASSERT_EXPRs to be inserted\n\n");
4936 EXECUTE_IF_SET_IN_BITMAP (need_assert_for, 0, i, bi)
4937 dump_asserts_for (file, ssa_name (i));
4938 fprintf (file, "\n");
4942 /* Dump all the registered assertions for all the names to stderr. */
4944 DEBUG_FUNCTION void
4945 debug_all_asserts (void)
4947 dump_all_asserts (stderr);
4951 /* If NAME doesn't have an ASSERT_EXPR registered for asserting
4952 'EXPR COMP_CODE VAL' at a location that dominates block BB or
4953 E->DEST, then register this location as a possible insertion point
4954 for ASSERT_EXPR <NAME, EXPR COMP_CODE VAL>.
4956 BB, E and SI provide the exact insertion point for the new
4957 ASSERT_EXPR. If BB is NULL, then the ASSERT_EXPR is to be inserted
4958 on edge E. Otherwise, if E is NULL, the ASSERT_EXPR is inserted on
4959 BB. If SI points to a COND_EXPR or a SWITCH_EXPR statement, then E
4960 must not be NULL. */
4962 static void
4963 register_new_assert_for (tree name, tree expr,
4964 enum tree_code comp_code,
4965 tree val,
4966 basic_block bb,
4967 edge e,
4968 gimple_stmt_iterator si)
4970 assert_locus *n, *loc, *last_loc;
4971 basic_block dest_bb;
4973 gcc_checking_assert (bb == NULL || e == NULL);
4975 if (e == NULL)
4976 gcc_checking_assert (gimple_code (gsi_stmt (si)) != GIMPLE_COND
4977 && gimple_code (gsi_stmt (si)) != GIMPLE_SWITCH);
4979 /* Never build an assert comparing against an integer constant with
4980 TREE_OVERFLOW set. This confuses our undefined overflow warning
4981 machinery. */
4982 if (TREE_OVERFLOW_P (val))
4983 val = drop_tree_overflow (val);
4985 /* The new assertion A will be inserted at BB or E. We need to
4986 determine if the new location is dominated by a previously
4987 registered location for A. If we are doing an edge insertion,
4988 assume that A will be inserted at E->DEST. Note that this is not
4989 necessarily true.
4991 If E is a critical edge, it will be split. But even if E is
4992 split, the new block will dominate the same set of blocks that
4993 E->DEST dominates.
4995 The reverse, however, is not true, blocks dominated by E->DEST
4996 will not be dominated by the new block created to split E. So,
4997 if the insertion location is on a critical edge, we will not use
4998 the new location to move another assertion previously registered
4999 at a block dominated by E->DEST. */
5000 dest_bb = (bb) ? bb : e->dest;
5002 /* If NAME already has an ASSERT_EXPR registered for COMP_CODE and
5003 VAL at a block dominating DEST_BB, then we don't need to insert a new
5004 one. Similarly, if the same assertion already exists at a block
5005 dominated by DEST_BB and the new location is not on a critical
5006 edge, then update the existing location for the assertion (i.e.,
5007 move the assertion up in the dominance tree).
5009 Note, this is implemented as a simple linked list because there
5010 should not be more than a handful of assertions registered per
5011 name. If this becomes a performance problem, a table hashed by
5012 COMP_CODE and VAL could be implemented. */
5013 loc = asserts_for[SSA_NAME_VERSION (name)];
5014 last_loc = loc;
5015 while (loc)
5017 if (loc->comp_code == comp_code
5018 && (loc->val == val
5019 || operand_equal_p (loc->val, val, 0))
5020 && (loc->expr == expr
5021 || operand_equal_p (loc->expr, expr, 0)))
5023 /* If E is not a critical edge and DEST_BB
5024 dominates the existing location for the assertion, move
5025 the assertion up in the dominance tree by updating its
5026 location information. */
5027 if ((e == NULL || !EDGE_CRITICAL_P (e))
5028 && dominated_by_p (CDI_DOMINATORS, loc->bb, dest_bb))
5030 loc->bb = dest_bb;
5031 loc->e = e;
5032 loc->si = si;
5033 return;
5037 /* Update the last node of the list and move to the next one. */
5038 last_loc = loc;
5039 loc = loc->next;
5042 /* If we didn't find an assertion already registered for
5043 NAME COMP_CODE VAL, add a new one at the end of the list of
5044 assertions associated with NAME. */
5045 n = XNEW (struct assert_locus);
5046 n->bb = dest_bb;
5047 n->e = e;
5048 n->si = si;
5049 n->comp_code = comp_code;
5050 n->val = val;
5051 n->expr = expr;
5052 n->next = NULL;
5054 if (last_loc)
5055 last_loc->next = n;
5056 else
5057 asserts_for[SSA_NAME_VERSION (name)] = n;
5059 bitmap_set_bit (need_assert_for, SSA_NAME_VERSION (name));
5062 /* (COND_OP0 COND_CODE COND_OP1) is a predicate which uses NAME.
5063 Extract a suitable test code and value and store them into *CODE_P and
5064 *VAL_P so the predicate is normalized to NAME *CODE_P *VAL_P.
5066 If no extraction was possible, return FALSE, otherwise return TRUE.
5068 If INVERT is true, then we invert the result stored into *CODE_P. */
5070 static bool
5071 extract_code_and_val_from_cond_with_ops (tree name, enum tree_code cond_code,
5072 tree cond_op0, tree cond_op1,
5073 bool invert, enum tree_code *code_p,
5074 tree *val_p)
5076 enum tree_code comp_code;
5077 tree val;
5079 /* Otherwise, we have a comparison of the form NAME COMP VAL
5080 or VAL COMP NAME. */
5081 if (name == cond_op1)
5083 /* If the predicate is of the form VAL COMP NAME, flip
5084 COMP around because we need to register NAME as the
5085 first operand in the predicate. */
5086 comp_code = swap_tree_comparison (cond_code);
5087 val = cond_op0;
5089 else
5091 /* The comparison is of the form NAME COMP VAL, so the
5092 comparison code remains unchanged. */
5093 comp_code = cond_code;
5094 val = cond_op1;
5097 /* Invert the comparison code as necessary. */
5098 if (invert)
5099 comp_code = invert_tree_comparison (comp_code, 0);
5101 /* VRP does not handle float types. */
5102 if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (val)))
5103 return false;
5105 /* Do not register always-false predicates.
5106 FIXME: this works around a limitation in fold() when dealing with
5107 enumerations. Given 'enum { N1, N2 } x;', fold will not
5108 fold 'if (x > N2)' to 'if (0)'. */
5109 if ((comp_code == GT_EXPR || comp_code == LT_EXPR)
5110 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
5112 tree min = TYPE_MIN_VALUE (TREE_TYPE (val));
5113 tree max = TYPE_MAX_VALUE (TREE_TYPE (val));
5115 if (comp_code == GT_EXPR
5116 && (!max
5117 || compare_values (val, max) == 0))
5118 return false;
5120 if (comp_code == LT_EXPR
5121 && (!min
5122 || compare_values (val, min) == 0))
5123 return false;
5125 *code_p = comp_code;
5126 *val_p = val;
5127 return true;
5130 /* Find out smallest RES where RES > VAL && (RES & MASK) == RES, if any
5131 (otherwise return VAL). VAL and MASK must be zero-extended for
5132 precision PREC. If SGNBIT is non-zero, first xor VAL with SGNBIT
5133 (to transform signed values into unsigned) and at the end xor
5134 SGNBIT back. */
5136 static wide_int
5137 masked_increment (const wide_int &val_in, const wide_int &mask,
5138 const wide_int &sgnbit, unsigned int prec)
5140 wide_int bit = wi::one (prec), res;
5141 unsigned int i;
5143 wide_int val = val_in ^ sgnbit;
5144 for (i = 0; i < prec; i++, bit += bit)
5146 res = mask;
5147 if ((res & bit) == 0)
5148 continue;
5149 res = bit - 1;
5150 res = (val + bit).and_not (res);
5151 res &= mask;
5152 if (wi::gtu_p (res, val))
5153 return res ^ sgnbit;
5155 return val ^ sgnbit;
5158 /* Try to register an edge assertion for SSA name NAME on edge E for
5159 the condition COND contributing to the conditional jump pointed to by BSI.
5160 Invert the condition COND if INVERT is true. */
5162 static void
5163 register_edge_assert_for_2 (tree name, edge e, gimple_stmt_iterator bsi,
5164 enum tree_code cond_code,
5165 tree cond_op0, tree cond_op1, bool invert)
5167 tree val;
5168 enum tree_code comp_code;
5170 if (!extract_code_and_val_from_cond_with_ops (name, cond_code,
5171 cond_op0,
5172 cond_op1,
5173 invert, &comp_code, &val))
5174 return;
5176 /* Only register an ASSERT_EXPR if NAME was found in the sub-graph
5177 reachable from E. */
5178 if (live_on_edge (e, name)
5179 && !has_single_use (name))
5180 register_new_assert_for (name, name, comp_code, val, NULL, e, bsi);
5182 /* In the case of NAME <= CST and NAME being defined as
5183 NAME = (unsigned) NAME2 + CST2 we can assert NAME2 >= -CST2
5184 and NAME2 <= CST - CST2. We can do the same for NAME > CST.
5185 This catches range and anti-range tests. */
5186 if ((comp_code == LE_EXPR
5187 || comp_code == GT_EXPR)
5188 && TREE_CODE (val) == INTEGER_CST
5189 && TYPE_UNSIGNED (TREE_TYPE (val)))
5191 gimple *def_stmt = SSA_NAME_DEF_STMT (name);
5192 tree cst2 = NULL_TREE, name2 = NULL_TREE, name3 = NULL_TREE;
5194 /* Extract CST2 from the (optional) addition. */
5195 if (is_gimple_assign (def_stmt)
5196 && gimple_assign_rhs_code (def_stmt) == PLUS_EXPR)
5198 name2 = gimple_assign_rhs1 (def_stmt);
5199 cst2 = gimple_assign_rhs2 (def_stmt);
5200 if (TREE_CODE (name2) == SSA_NAME
5201 && TREE_CODE (cst2) == INTEGER_CST)
5202 def_stmt = SSA_NAME_DEF_STMT (name2);
5205 /* Extract NAME2 from the (optional) sign-changing cast. */
5206 if (gimple_assign_cast_p (def_stmt))
5208 if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt))
5209 && ! TYPE_UNSIGNED (TREE_TYPE (gimple_assign_rhs1 (def_stmt)))
5210 && (TYPE_PRECISION (gimple_expr_type (def_stmt))
5211 == TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt)))))
5212 name3 = gimple_assign_rhs1 (def_stmt);
5215 /* If name3 is used later, create an ASSERT_EXPR for it. */
5216 if (name3 != NULL_TREE
5217 && TREE_CODE (name3) == SSA_NAME
5218 && (cst2 == NULL_TREE
5219 || TREE_CODE (cst2) == INTEGER_CST)
5220 && INTEGRAL_TYPE_P (TREE_TYPE (name3))
5221 && live_on_edge (e, name3)
5222 && !has_single_use (name3))
5224 tree tmp;
5226 /* Build an expression for the range test. */
5227 tmp = build1 (NOP_EXPR, TREE_TYPE (name), name3);
5228 if (cst2 != NULL_TREE)
5229 tmp = build2 (PLUS_EXPR, TREE_TYPE (name), tmp, cst2);
5231 if (dump_file)
5233 fprintf (dump_file, "Adding assert for ");
5234 print_generic_expr (dump_file, name3, 0);
5235 fprintf (dump_file, " from ");
5236 print_generic_expr (dump_file, tmp, 0);
5237 fprintf (dump_file, "\n");
5240 register_new_assert_for (name3, tmp, comp_code, val, NULL, e, bsi);
5243 /* If name2 is used later, create an ASSERT_EXPR for it. */
5244 if (name2 != NULL_TREE
5245 && TREE_CODE (name2) == SSA_NAME
5246 && TREE_CODE (cst2) == INTEGER_CST
5247 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
5248 && live_on_edge (e, name2)
5249 && !has_single_use (name2))
5251 tree tmp;
5253 /* Build an expression for the range test. */
5254 tmp = name2;
5255 if (TREE_TYPE (name) != TREE_TYPE (name2))
5256 tmp = build1 (NOP_EXPR, TREE_TYPE (name), tmp);
5257 if (cst2 != NULL_TREE)
5258 tmp = build2 (PLUS_EXPR, TREE_TYPE (name), tmp, cst2);
5260 if (dump_file)
5262 fprintf (dump_file, "Adding assert for ");
5263 print_generic_expr (dump_file, name2, 0);
5264 fprintf (dump_file, " from ");
5265 print_generic_expr (dump_file, tmp, 0);
5266 fprintf (dump_file, "\n");
5269 register_new_assert_for (name2, tmp, comp_code, val, NULL, e, bsi);
5273 /* In the case of post-in/decrement tests like if (i++) ... and uses
5274 of the in/decremented value on the edge the extra name we want to
5275 assert for is not on the def chain of the name compared. Instead
5276 it is in the set of use stmts.
5277 Similar cases happen for conversions that were simplified through
5278 fold_{sign_changed,widened}_comparison. */
5279 if ((comp_code == NE_EXPR
5280 || comp_code == EQ_EXPR)
5281 && TREE_CODE (val) == INTEGER_CST)
5283 imm_use_iterator ui;
5284 gimple *use_stmt;
5285 FOR_EACH_IMM_USE_STMT (use_stmt, ui, name)
5287 if (!is_gimple_assign (use_stmt))
5288 continue;
5290 /* Cut off to use-stmts that are dominating the predecessor. */
5291 if (!dominated_by_p (CDI_DOMINATORS, e->src, gimple_bb (use_stmt)))
5292 continue;
5294 tree name2 = gimple_assign_lhs (use_stmt);
5295 if (TREE_CODE (name2) != SSA_NAME
5296 || !live_on_edge (e, name2))
5297 continue;
5299 enum tree_code code = gimple_assign_rhs_code (use_stmt);
5300 tree cst;
5301 if (code == PLUS_EXPR
5302 || code == MINUS_EXPR)
5304 cst = gimple_assign_rhs2 (use_stmt);
5305 if (TREE_CODE (cst) != INTEGER_CST)
5306 continue;
5307 cst = int_const_binop (code, val, cst);
5309 else if (CONVERT_EXPR_CODE_P (code))
5311 /* For truncating conversions we cannot record
5312 an inequality. */
5313 if (comp_code == NE_EXPR
5314 && (TYPE_PRECISION (TREE_TYPE (name2))
5315 < TYPE_PRECISION (TREE_TYPE (name))))
5316 continue;
5317 cst = fold_convert (TREE_TYPE (name2), val);
5319 else
5320 continue;
5322 if (TREE_OVERFLOW_P (cst))
5323 cst = drop_tree_overflow (cst);
5324 register_new_assert_for (name2, name2, comp_code, cst,
5325 NULL, e, bsi);
5329 if (TREE_CODE_CLASS (comp_code) == tcc_comparison
5330 && TREE_CODE (val) == INTEGER_CST)
5332 gimple *def_stmt = SSA_NAME_DEF_STMT (name);
5333 tree name2 = NULL_TREE, names[2], cst2 = NULL_TREE;
5334 tree val2 = NULL_TREE;
5335 unsigned int prec = TYPE_PRECISION (TREE_TYPE (val));
5336 wide_int mask = wi::zero (prec);
5337 unsigned int nprec = prec;
5338 enum tree_code rhs_code = ERROR_MARK;
5340 if (is_gimple_assign (def_stmt))
5341 rhs_code = gimple_assign_rhs_code (def_stmt);
5343 /* Add asserts for NAME cmp CST and NAME being defined
5344 as NAME = (int) NAME2. */
5345 if (!TYPE_UNSIGNED (TREE_TYPE (val))
5346 && (comp_code == LE_EXPR || comp_code == LT_EXPR
5347 || comp_code == GT_EXPR || comp_code == GE_EXPR)
5348 && gimple_assign_cast_p (def_stmt))
5350 name2 = gimple_assign_rhs1 (def_stmt);
5351 if (CONVERT_EXPR_CODE_P (rhs_code)
5352 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
5353 && TYPE_UNSIGNED (TREE_TYPE (name2))
5354 && prec == TYPE_PRECISION (TREE_TYPE (name2))
5355 && (comp_code == LE_EXPR || comp_code == GT_EXPR
5356 || !tree_int_cst_equal (val,
5357 TYPE_MIN_VALUE (TREE_TYPE (val))))
5358 && live_on_edge (e, name2)
5359 && !has_single_use (name2))
5361 tree tmp, cst;
5362 enum tree_code new_comp_code = comp_code;
5364 cst = fold_convert (TREE_TYPE (name2),
5365 TYPE_MIN_VALUE (TREE_TYPE (val)));
5366 /* Build an expression for the range test. */
5367 tmp = build2 (PLUS_EXPR, TREE_TYPE (name2), name2, cst);
5368 cst = fold_build2 (PLUS_EXPR, TREE_TYPE (name2), cst,
5369 fold_convert (TREE_TYPE (name2), val));
5370 if (comp_code == LT_EXPR || comp_code == GE_EXPR)
5372 new_comp_code = comp_code == LT_EXPR ? LE_EXPR : GT_EXPR;
5373 cst = fold_build2 (MINUS_EXPR, TREE_TYPE (name2), cst,
5374 build_int_cst (TREE_TYPE (name2), 1));
5377 if (dump_file)
5379 fprintf (dump_file, "Adding assert for ");
5380 print_generic_expr (dump_file, name2, 0);
5381 fprintf (dump_file, " from ");
5382 print_generic_expr (dump_file, tmp, 0);
5383 fprintf (dump_file, "\n");
5386 register_new_assert_for (name2, tmp, new_comp_code, cst, NULL,
5387 e, bsi);
5391 /* Add asserts for NAME cmp CST and NAME being defined as
5392 NAME = NAME2 >> CST2.
5394 Extract CST2 from the right shift. */
5395 if (rhs_code == RSHIFT_EXPR)
5397 name2 = gimple_assign_rhs1 (def_stmt);
5398 cst2 = gimple_assign_rhs2 (def_stmt);
5399 if (TREE_CODE (name2) == SSA_NAME
5400 && tree_fits_uhwi_p (cst2)
5401 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
5402 && IN_RANGE (tree_to_uhwi (cst2), 1, prec - 1)
5403 && prec == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (val)))
5404 && live_on_edge (e, name2)
5405 && !has_single_use (name2))
5407 mask = wi::mask (tree_to_uhwi (cst2), false, prec);
5408 val2 = fold_binary (LSHIFT_EXPR, TREE_TYPE (val), val, cst2);
5411 if (val2 != NULL_TREE
5412 && TREE_CODE (val2) == INTEGER_CST
5413 && simple_cst_equal (fold_build2 (RSHIFT_EXPR,
5414 TREE_TYPE (val),
5415 val2, cst2), val))
5417 enum tree_code new_comp_code = comp_code;
5418 tree tmp, new_val;
5420 tmp = name2;
5421 if (comp_code == EQ_EXPR || comp_code == NE_EXPR)
5423 if (!TYPE_UNSIGNED (TREE_TYPE (val)))
5425 tree type = build_nonstandard_integer_type (prec, 1);
5426 tmp = build1 (NOP_EXPR, type, name2);
5427 val2 = fold_convert (type, val2);
5429 tmp = fold_build2 (MINUS_EXPR, TREE_TYPE (tmp), tmp, val2);
5430 new_val = wide_int_to_tree (TREE_TYPE (tmp), mask);
5431 new_comp_code = comp_code == EQ_EXPR ? LE_EXPR : GT_EXPR;
5433 else if (comp_code == LT_EXPR || comp_code == GE_EXPR)
5435 wide_int minval
5436 = wi::min_value (prec, TYPE_SIGN (TREE_TYPE (val)));
5437 new_val = val2;
5438 if (minval == new_val)
5439 new_val = NULL_TREE;
5441 else
5443 wide_int maxval
5444 = wi::max_value (prec, TYPE_SIGN (TREE_TYPE (val)));
5445 mask |= val2;
5446 if (mask == maxval)
5447 new_val = NULL_TREE;
5448 else
5449 new_val = wide_int_to_tree (TREE_TYPE (val2), mask);
5452 if (new_val)
5454 if (dump_file)
5456 fprintf (dump_file, "Adding assert for ");
5457 print_generic_expr (dump_file, name2, 0);
5458 fprintf (dump_file, " from ");
5459 print_generic_expr (dump_file, tmp, 0);
5460 fprintf (dump_file, "\n");
5463 register_new_assert_for (name2, tmp, new_comp_code, new_val,
5464 NULL, e, bsi);
5468 /* Add asserts for NAME cmp CST and NAME being defined as
5469 NAME = NAME2 & CST2.
5471 Extract CST2 from the and.
5473 Also handle
5474 NAME = (unsigned) NAME2;
5475 casts where NAME's type is unsigned and has smaller precision
5476 than NAME2's type as if it was NAME = NAME2 & MASK. */
5477 names[0] = NULL_TREE;
5478 names[1] = NULL_TREE;
5479 cst2 = NULL_TREE;
5480 if (rhs_code == BIT_AND_EXPR
5481 || (CONVERT_EXPR_CODE_P (rhs_code)
5482 && TREE_CODE (TREE_TYPE (val)) == INTEGER_TYPE
5483 && TYPE_UNSIGNED (TREE_TYPE (val))
5484 && TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt)))
5485 > prec))
5487 name2 = gimple_assign_rhs1 (def_stmt);
5488 if (rhs_code == BIT_AND_EXPR)
5489 cst2 = gimple_assign_rhs2 (def_stmt);
5490 else
5492 cst2 = TYPE_MAX_VALUE (TREE_TYPE (val));
5493 nprec = TYPE_PRECISION (TREE_TYPE (name2));
5495 if (TREE_CODE (name2) == SSA_NAME
5496 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
5497 && TREE_CODE (cst2) == INTEGER_CST
5498 && !integer_zerop (cst2)
5499 && (nprec > 1
5500 || TYPE_UNSIGNED (TREE_TYPE (val))))
5502 gimple *def_stmt2 = SSA_NAME_DEF_STMT (name2);
5503 if (gimple_assign_cast_p (def_stmt2))
5505 names[1] = gimple_assign_rhs1 (def_stmt2);
5506 if (!CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt2))
5507 || !INTEGRAL_TYPE_P (TREE_TYPE (names[1]))
5508 || (TYPE_PRECISION (TREE_TYPE (name2))
5509 != TYPE_PRECISION (TREE_TYPE (names[1])))
5510 || !live_on_edge (e, names[1])
5511 || has_single_use (names[1]))
5512 names[1] = NULL_TREE;
5514 if (live_on_edge (e, name2)
5515 && !has_single_use (name2))
5516 names[0] = name2;
5519 if (names[0] || names[1])
5521 wide_int minv, maxv, valv, cst2v;
5522 wide_int tem, sgnbit;
5523 bool valid_p = false, valn, cst2n;
5524 enum tree_code ccode = comp_code;
5526 valv = wide_int::from (val, nprec, UNSIGNED);
5527 cst2v = wide_int::from (cst2, nprec, UNSIGNED);
5528 valn = wi::neg_p (valv, TYPE_SIGN (TREE_TYPE (val)));
5529 cst2n = wi::neg_p (cst2v, TYPE_SIGN (TREE_TYPE (val)));
5530 /* If CST2 doesn't have most significant bit set,
5531 but VAL is negative, we have comparison like
5532 if ((x & 0x123) > -4) (always true). Just give up. */
5533 if (!cst2n && valn)
5534 ccode = ERROR_MARK;
5535 if (cst2n)
5536 sgnbit = wi::set_bit_in_zero (nprec - 1, nprec);
5537 else
5538 sgnbit = wi::zero (nprec);
5539 minv = valv & cst2v;
5540 switch (ccode)
5542 case EQ_EXPR:
5543 /* Minimum unsigned value for equality is VAL & CST2
5544 (should be equal to VAL, otherwise we probably should
5545 have folded the comparison into false) and
5546 maximum unsigned value is VAL | ~CST2. */
5547 maxv = valv | ~cst2v;
5548 valid_p = true;
5549 break;
5551 case NE_EXPR:
5552 tem = valv | ~cst2v;
5553 /* If VAL is 0, handle (X & CST2) != 0 as (X & CST2) > 0U. */
5554 if (valv == 0)
5556 cst2n = false;
5557 sgnbit = wi::zero (nprec);
5558 goto gt_expr;
5560 /* If (VAL | ~CST2) is all ones, handle it as
5561 (X & CST2) < VAL. */
5562 if (tem == -1)
5564 cst2n = false;
5565 valn = false;
5566 sgnbit = wi::zero (nprec);
5567 goto lt_expr;
5569 if (!cst2n && wi::neg_p (cst2v))
5570 sgnbit = wi::set_bit_in_zero (nprec - 1, nprec);
5571 if (sgnbit != 0)
5573 if (valv == sgnbit)
5575 cst2n = true;
5576 valn = true;
5577 goto gt_expr;
5579 if (tem == wi::mask (nprec - 1, false, nprec))
5581 cst2n = true;
5582 goto lt_expr;
5584 if (!cst2n)
5585 sgnbit = wi::zero (nprec);
5587 break;
5589 case GE_EXPR:
5590 /* Minimum unsigned value for >= if (VAL & CST2) == VAL
5591 is VAL and maximum unsigned value is ~0. For signed
5592 comparison, if CST2 doesn't have most significant bit
5593 set, handle it similarly. If CST2 has MSB set,
5594 the minimum is the same, and maximum is ~0U/2. */
5595 if (minv != valv)
5597 /* If (VAL & CST2) != VAL, X & CST2 can't be equal to
5598 VAL. */
5599 minv = masked_increment (valv, cst2v, sgnbit, nprec);
5600 if (minv == valv)
5601 break;
5603 maxv = wi::mask (nprec - (cst2n ? 1 : 0), false, nprec);
5604 valid_p = true;
5605 break;
5607 case GT_EXPR:
5608 gt_expr:
5609 /* Find out smallest MINV where MINV > VAL
5610 && (MINV & CST2) == MINV, if any. If VAL is signed and
5611 CST2 has MSB set, compute it biased by 1 << (nprec - 1). */
5612 minv = masked_increment (valv, cst2v, sgnbit, nprec);
5613 if (minv == valv)
5614 break;
5615 maxv = wi::mask (nprec - (cst2n ? 1 : 0), false, nprec);
5616 valid_p = true;
5617 break;
5619 case LE_EXPR:
5620 /* Minimum unsigned value for <= is 0 and maximum
5621 unsigned value is VAL | ~CST2 if (VAL & CST2) == VAL.
5622 Otherwise, find smallest VAL2 where VAL2 > VAL
5623 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
5624 as maximum.
5625 For signed comparison, if CST2 doesn't have most
5626 significant bit set, handle it similarly. If CST2 has
5627 MSB set, the maximum is the same and minimum is INT_MIN. */
5628 if (minv == valv)
5629 maxv = valv;
5630 else
5632 maxv = masked_increment (valv, cst2v, sgnbit, nprec);
5633 if (maxv == valv)
5634 break;
5635 maxv -= 1;
5637 maxv |= ~cst2v;
5638 minv = sgnbit;
5639 valid_p = true;
5640 break;
5642 case LT_EXPR:
5643 lt_expr:
5644 /* Minimum unsigned value for < is 0 and maximum
5645 unsigned value is (VAL-1) | ~CST2 if (VAL & CST2) == VAL.
5646 Otherwise, find smallest VAL2 where VAL2 > VAL
5647 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
5648 as maximum.
5649 For signed comparison, if CST2 doesn't have most
5650 significant bit set, handle it similarly. If CST2 has
5651 MSB set, the maximum is the same and minimum is INT_MIN. */
5652 if (minv == valv)
5654 if (valv == sgnbit)
5655 break;
5656 maxv = valv;
5658 else
5660 maxv = masked_increment (valv, cst2v, sgnbit, nprec);
5661 if (maxv == valv)
5662 break;
5664 maxv -= 1;
5665 maxv |= ~cst2v;
5666 minv = sgnbit;
5667 valid_p = true;
5668 break;
5670 default:
5671 break;
5673 if (valid_p
5674 && (maxv - minv) != -1)
5676 tree tmp, new_val, type;
5677 int i;
5679 for (i = 0; i < 2; i++)
5680 if (names[i])
5682 wide_int maxv2 = maxv;
5683 tmp = names[i];
5684 type = TREE_TYPE (names[i]);
5685 if (!TYPE_UNSIGNED (type))
5687 type = build_nonstandard_integer_type (nprec, 1);
5688 tmp = build1 (NOP_EXPR, type, names[i]);
5690 if (minv != 0)
5692 tmp = build2 (PLUS_EXPR, type, tmp,
5693 wide_int_to_tree (type, -minv));
5694 maxv2 = maxv - minv;
5696 new_val = wide_int_to_tree (type, maxv2);
5698 if (dump_file)
5700 fprintf (dump_file, "Adding assert for ");
5701 print_generic_expr (dump_file, names[i], 0);
5702 fprintf (dump_file, " from ");
5703 print_generic_expr (dump_file, tmp, 0);
5704 fprintf (dump_file, "\n");
5707 register_new_assert_for (names[i], tmp, LE_EXPR,
5708 new_val, NULL, e, bsi);
5715 /* OP is an operand of a truth value expression which is known to have
5716 a particular value. Register any asserts for OP and for any
5717 operands in OP's defining statement.
5719 If CODE is EQ_EXPR, then we want to register OP is zero (false),
5720 if CODE is NE_EXPR, then we want to register OP is nonzero (true). */
5722 static void
5723 register_edge_assert_for_1 (tree op, enum tree_code code,
5724 edge e, gimple_stmt_iterator bsi)
5726 gimple *op_def;
5727 tree val;
5728 enum tree_code rhs_code;
5730 /* We only care about SSA_NAMEs. */
5731 if (TREE_CODE (op) != SSA_NAME)
5732 return;
5734 /* We know that OP will have a zero or nonzero value. If OP is used
5735 more than once go ahead and register an assert for OP. */
5736 if (live_on_edge (e, op)
5737 && !has_single_use (op))
5739 val = build_int_cst (TREE_TYPE (op), 0);
5740 register_new_assert_for (op, op, code, val, NULL, e, bsi);
5743 /* Now look at how OP is set. If it's set from a comparison,
5744 a truth operation or some bit operations, then we may be able
5745 to register information about the operands of that assignment. */
5746 op_def = SSA_NAME_DEF_STMT (op);
5747 if (gimple_code (op_def) != GIMPLE_ASSIGN)
5748 return;
5750 rhs_code = gimple_assign_rhs_code (op_def);
5752 if (TREE_CODE_CLASS (rhs_code) == tcc_comparison)
5754 bool invert = (code == EQ_EXPR ? true : false);
5755 tree op0 = gimple_assign_rhs1 (op_def);
5756 tree op1 = gimple_assign_rhs2 (op_def);
5758 if (TREE_CODE (op0) == SSA_NAME)
5759 register_edge_assert_for_2 (op0, e, bsi, rhs_code, op0, op1, invert);
5760 if (TREE_CODE (op1) == SSA_NAME)
5761 register_edge_assert_for_2 (op1, e, bsi, rhs_code, op0, op1, invert);
5763 else if ((code == NE_EXPR
5764 && gimple_assign_rhs_code (op_def) == BIT_AND_EXPR)
5765 || (code == EQ_EXPR
5766 && gimple_assign_rhs_code (op_def) == BIT_IOR_EXPR))
5768 /* Recurse on each operand. */
5769 tree op0 = gimple_assign_rhs1 (op_def);
5770 tree op1 = gimple_assign_rhs2 (op_def);
5771 if (TREE_CODE (op0) == SSA_NAME
5772 && has_single_use (op0))
5773 register_edge_assert_for_1 (op0, code, e, bsi);
5774 if (TREE_CODE (op1) == SSA_NAME
5775 && has_single_use (op1))
5776 register_edge_assert_for_1 (op1, code, e, bsi);
5778 else if (gimple_assign_rhs_code (op_def) == BIT_NOT_EXPR
5779 && TYPE_PRECISION (TREE_TYPE (gimple_assign_lhs (op_def))) == 1)
5781 /* Recurse, flipping CODE. */
5782 code = invert_tree_comparison (code, false);
5783 register_edge_assert_for_1 (gimple_assign_rhs1 (op_def), code, e, bsi);
5785 else if (gimple_assign_rhs_code (op_def) == SSA_NAME)
5787 /* Recurse through the copy. */
5788 register_edge_assert_for_1 (gimple_assign_rhs1 (op_def), code, e, bsi);
5790 else if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (op_def)))
5792 /* Recurse through the type conversion, unless it is a narrowing
5793 conversion or conversion from non-integral type. */
5794 tree rhs = gimple_assign_rhs1 (op_def);
5795 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs))
5796 && (TYPE_PRECISION (TREE_TYPE (rhs))
5797 <= TYPE_PRECISION (TREE_TYPE (op))))
5798 register_edge_assert_for_1 (rhs, code, e, bsi);
5802 /* Try to register an edge assertion for SSA name NAME on edge E for
5803 the condition COND contributing to the conditional jump pointed to by
5804 SI. */
5806 static void
5807 register_edge_assert_for (tree name, edge e, gimple_stmt_iterator si,
5808 enum tree_code cond_code, tree cond_op0,
5809 tree cond_op1)
5811 tree val;
5812 enum tree_code comp_code;
5813 bool is_else_edge = (e->flags & EDGE_FALSE_VALUE) != 0;
5815 /* Do not attempt to infer anything in names that flow through
5816 abnormal edges. */
5817 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name))
5818 return;
5820 if (!extract_code_and_val_from_cond_with_ops (name, cond_code,
5821 cond_op0, cond_op1,
5822 is_else_edge,
5823 &comp_code, &val))
5824 return;
5826 /* Register ASSERT_EXPRs for name. */
5827 register_edge_assert_for_2 (name, e, si, cond_code, cond_op0,
5828 cond_op1, is_else_edge);
5831 /* If COND is effectively an equality test of an SSA_NAME against
5832 the value zero or one, then we may be able to assert values
5833 for SSA_NAMEs which flow into COND. */
5835 /* In the case of NAME == 1 or NAME != 0, for BIT_AND_EXPR defining
5836 statement of NAME we can assert both operands of the BIT_AND_EXPR
5837 have nonzero value. */
5838 if (((comp_code == EQ_EXPR && integer_onep (val))
5839 || (comp_code == NE_EXPR && integer_zerop (val))))
5841 gimple *def_stmt = SSA_NAME_DEF_STMT (name);
5843 if (is_gimple_assign (def_stmt)
5844 && gimple_assign_rhs_code (def_stmt) == BIT_AND_EXPR)
5846 tree op0 = gimple_assign_rhs1 (def_stmt);
5847 tree op1 = gimple_assign_rhs2 (def_stmt);
5848 register_edge_assert_for_1 (op0, NE_EXPR, e, si);
5849 register_edge_assert_for_1 (op1, NE_EXPR, e, si);
5853 /* In the case of NAME == 0 or NAME != 1, for BIT_IOR_EXPR defining
5854 statement of NAME we can assert both operands of the BIT_IOR_EXPR
5855 have zero value. */
5856 if (((comp_code == EQ_EXPR && integer_zerop (val))
5857 || (comp_code == NE_EXPR && integer_onep (val))))
5859 gimple *def_stmt = SSA_NAME_DEF_STMT (name);
5861 /* For BIT_IOR_EXPR only if NAME == 0 both operands have
5862 necessarily zero value, or if type-precision is one. */
5863 if (is_gimple_assign (def_stmt)
5864 && (gimple_assign_rhs_code (def_stmt) == BIT_IOR_EXPR
5865 && (TYPE_PRECISION (TREE_TYPE (name)) == 1
5866 || comp_code == EQ_EXPR)))
5868 tree op0 = gimple_assign_rhs1 (def_stmt);
5869 tree op1 = gimple_assign_rhs2 (def_stmt);
5870 register_edge_assert_for_1 (op0, EQ_EXPR, e, si);
5871 register_edge_assert_for_1 (op1, EQ_EXPR, e, si);
5877 /* Determine whether the outgoing edges of BB should receive an
5878 ASSERT_EXPR for each of the operands of BB's LAST statement.
5879 The last statement of BB must be a COND_EXPR.
5881 If any of the sub-graphs rooted at BB have an interesting use of
5882 the predicate operands, an assert location node is added to the
5883 list of assertions for the corresponding operands. */
5885 static void
5886 find_conditional_asserts (basic_block bb, gcond *last)
5888 gimple_stmt_iterator bsi;
5889 tree op;
5890 edge_iterator ei;
5891 edge e;
5892 ssa_op_iter iter;
5894 bsi = gsi_for_stmt (last);
5896 /* Look for uses of the operands in each of the sub-graphs
5897 rooted at BB. We need to check each of the outgoing edges
5898 separately, so that we know what kind of ASSERT_EXPR to
5899 insert. */
5900 FOR_EACH_EDGE (e, ei, bb->succs)
5902 if (e->dest == bb)
5903 continue;
5905 /* Register the necessary assertions for each operand in the
5906 conditional predicate. */
5907 FOR_EACH_SSA_TREE_OPERAND (op, last, iter, SSA_OP_USE)
5908 register_edge_assert_for (op, e, bsi,
5909 gimple_cond_code (last),
5910 gimple_cond_lhs (last),
5911 gimple_cond_rhs (last));
5915 struct case_info
5917 tree expr;
5918 basic_block bb;
5921 /* Compare two case labels sorting first by the destination bb index
5922 and then by the case value. */
5924 static int
5925 compare_case_labels (const void *p1, const void *p2)
5927 const struct case_info *ci1 = (const struct case_info *) p1;
5928 const struct case_info *ci2 = (const struct case_info *) p2;
5929 int idx1 = ci1->bb->index;
5930 int idx2 = ci2->bb->index;
5932 if (idx1 < idx2)
5933 return -1;
5934 else if (idx1 == idx2)
5936 /* Make sure the default label is first in a group. */
5937 if (!CASE_LOW (ci1->expr))
5938 return -1;
5939 else if (!CASE_LOW (ci2->expr))
5940 return 1;
5941 else
5942 return tree_int_cst_compare (CASE_LOW (ci1->expr),
5943 CASE_LOW (ci2->expr));
5945 else
5946 return 1;
5949 /* Determine whether the outgoing edges of BB should receive an
5950 ASSERT_EXPR for each of the operands of BB's LAST statement.
5951 The last statement of BB must be a SWITCH_EXPR.
5953 If any of the sub-graphs rooted at BB have an interesting use of
5954 the predicate operands, an assert location node is added to the
5955 list of assertions for the corresponding operands. */
5957 static void
5958 find_switch_asserts (basic_block bb, gswitch *last)
5960 gimple_stmt_iterator bsi;
5961 tree op;
5962 edge e;
5963 struct case_info *ci;
5964 size_t n = gimple_switch_num_labels (last);
5965 #if GCC_VERSION >= 4000
5966 unsigned int idx;
5967 #else
5968 /* Work around GCC 3.4 bug (PR 37086). */
5969 volatile unsigned int idx;
5970 #endif
5972 bsi = gsi_for_stmt (last);
5973 op = gimple_switch_index (last);
5974 if (TREE_CODE (op) != SSA_NAME)
5975 return;
5977 /* Build a vector of case labels sorted by destination label. */
5978 ci = XNEWVEC (struct case_info, n);
5979 for (idx = 0; idx < n; ++idx)
5981 ci[idx].expr = gimple_switch_label (last, idx);
5982 ci[idx].bb = label_to_block (CASE_LABEL (ci[idx].expr));
5984 qsort (ci, n, sizeof (struct case_info), compare_case_labels);
5986 for (idx = 0; idx < n; ++idx)
5988 tree min, max;
5989 tree cl = ci[idx].expr;
5990 basic_block cbb = ci[idx].bb;
5992 min = CASE_LOW (cl);
5993 max = CASE_HIGH (cl);
5995 /* If there are multiple case labels with the same destination
5996 we need to combine them to a single value range for the edge. */
5997 if (idx + 1 < n && cbb == ci[idx + 1].bb)
5999 /* Skip labels until the last of the group. */
6000 do {
6001 ++idx;
6002 } while (idx < n && cbb == ci[idx].bb);
6003 --idx;
6005 /* Pick up the maximum of the case label range. */
6006 if (CASE_HIGH (ci[idx].expr))
6007 max = CASE_HIGH (ci[idx].expr);
6008 else
6009 max = CASE_LOW (ci[idx].expr);
6012 /* Nothing to do if the range includes the default label until we
6013 can register anti-ranges. */
6014 if (min == NULL_TREE)
6015 continue;
6017 /* Find the edge to register the assert expr on. */
6018 e = find_edge (bb, cbb);
6020 /* Register the necessary assertions for the operand in the
6021 SWITCH_EXPR. */
6022 register_edge_assert_for (op, e, bsi,
6023 max ? GE_EXPR : EQ_EXPR,
6024 op, fold_convert (TREE_TYPE (op), min));
6025 if (max)
6026 register_edge_assert_for (op, e, bsi, LE_EXPR, op,
6027 fold_convert (TREE_TYPE (op), max));
6030 XDELETEVEC (ci);
6034 /* Traverse all the statements in block BB looking for statements that
6035 may generate useful assertions for the SSA names in their operand.
6036 If a statement produces a useful assertion A for name N_i, then the
6037 list of assertions already generated for N_i is scanned to
6038 determine if A is actually needed.
6040 If N_i already had the assertion A at a location dominating the
6041 current location, then nothing needs to be done. Otherwise, the
6042 new location for A is recorded instead.
6044 1- For every statement S in BB, all the variables used by S are
6045 added to bitmap FOUND_IN_SUBGRAPH.
6047 2- If statement S uses an operand N in a way that exposes a known
6048 value range for N, then if N was not already generated by an
6049 ASSERT_EXPR, create a new assert location for N. For instance,
6050 if N is a pointer and the statement dereferences it, we can
6051 assume that N is not NULL.
6053 3- COND_EXPRs are a special case of #2. We can derive range
6054 information from the predicate but need to insert different
6055 ASSERT_EXPRs for each of the sub-graphs rooted at the
6056 conditional block. If the last statement of BB is a conditional
6057 expression of the form 'X op Y', then
6059 a) Remove X and Y from the set FOUND_IN_SUBGRAPH.
6061 b) If the conditional is the only entry point to the sub-graph
6062 corresponding to the THEN_CLAUSE, recurse into it. On
6063 return, if X and/or Y are marked in FOUND_IN_SUBGRAPH, then
6064 an ASSERT_EXPR is added for the corresponding variable.
6066 c) Repeat step (b) on the ELSE_CLAUSE.
6068 d) Mark X and Y in FOUND_IN_SUBGRAPH.
6070 For instance,
6072 if (a == 9)
6073 b = a;
6074 else
6075 b = c + 1;
6077 In this case, an assertion on the THEN clause is useful to
6078 determine that 'a' is always 9 on that edge. However, an assertion
6079 on the ELSE clause would be unnecessary.
6081 4- If BB does not end in a conditional expression, then we recurse
6082 into BB's dominator children.
6084 At the end of the recursive traversal, every SSA name will have a
6085 list of locations where ASSERT_EXPRs should be added. When a new
6086 location for name N is found, it is registered by calling
6087 register_new_assert_for. That function keeps track of all the
6088 registered assertions to prevent adding unnecessary assertions.
6089 For instance, if a pointer P_4 is dereferenced more than once in a
6090 dominator tree, only the location dominating all the dereference of
6091 P_4 will receive an ASSERT_EXPR. */
6093 static void
6094 find_assert_locations_1 (basic_block bb, sbitmap live)
6096 gimple *last;
6098 last = last_stmt (bb);
6100 /* If BB's last statement is a conditional statement involving integer
6101 operands, determine if we need to add ASSERT_EXPRs. */
6102 if (last
6103 && gimple_code (last) == GIMPLE_COND
6104 && !fp_predicate (last)
6105 && !ZERO_SSA_OPERANDS (last, SSA_OP_USE))
6106 find_conditional_asserts (bb, as_a <gcond *> (last));
6108 /* If BB's last statement is a switch statement involving integer
6109 operands, determine if we need to add ASSERT_EXPRs. */
6110 if (last
6111 && gimple_code (last) == GIMPLE_SWITCH
6112 && !ZERO_SSA_OPERANDS (last, SSA_OP_USE))
6113 find_switch_asserts (bb, as_a <gswitch *> (last));
6115 /* Traverse all the statements in BB marking used names and looking
6116 for statements that may infer assertions for their used operands. */
6117 for (gimple_stmt_iterator si = gsi_last_bb (bb); !gsi_end_p (si);
6118 gsi_prev (&si))
6120 gimple *stmt;
6121 tree op;
6122 ssa_op_iter i;
6124 stmt = gsi_stmt (si);
6126 if (is_gimple_debug (stmt))
6127 continue;
6129 /* See if we can derive an assertion for any of STMT's operands. */
6130 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_USE)
6132 tree value;
6133 enum tree_code comp_code;
6135 /* If op is not live beyond this stmt, do not bother to insert
6136 asserts for it. */
6137 if (!bitmap_bit_p (live, SSA_NAME_VERSION (op)))
6138 continue;
6140 /* If OP is used in such a way that we can infer a value
6141 range for it, and we don't find a previous assertion for
6142 it, create a new assertion location node for OP. */
6143 if (infer_value_range (stmt, op, &comp_code, &value))
6145 /* If we are able to infer a nonzero value range for OP,
6146 then walk backwards through the use-def chain to see if OP
6147 was set via a typecast.
6149 If so, then we can also infer a nonzero value range
6150 for the operand of the NOP_EXPR. */
6151 if (comp_code == NE_EXPR && integer_zerop (value))
6153 tree t = op;
6154 gimple *def_stmt = SSA_NAME_DEF_STMT (t);
6156 while (is_gimple_assign (def_stmt)
6157 && CONVERT_EXPR_CODE_P
6158 (gimple_assign_rhs_code (def_stmt))
6159 && TREE_CODE
6160 (gimple_assign_rhs1 (def_stmt)) == SSA_NAME
6161 && POINTER_TYPE_P
6162 (TREE_TYPE (gimple_assign_rhs1 (def_stmt))))
6164 t = gimple_assign_rhs1 (def_stmt);
6165 def_stmt = SSA_NAME_DEF_STMT (t);
6167 /* Note we want to register the assert for the
6168 operand of the NOP_EXPR after SI, not after the
6169 conversion. */
6170 if (! has_single_use (t))
6171 register_new_assert_for (t, t, comp_code, value,
6172 bb, NULL, si);
6176 register_new_assert_for (op, op, comp_code, value, bb, NULL, si);
6180 /* Update live. */
6181 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_USE)
6182 bitmap_set_bit (live, SSA_NAME_VERSION (op));
6183 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_DEF)
6184 bitmap_clear_bit (live, SSA_NAME_VERSION (op));
6187 /* Traverse all PHI nodes in BB, updating live. */
6188 for (gphi_iterator si = gsi_start_phis (bb); !gsi_end_p (si);
6189 gsi_next (&si))
6191 use_operand_p arg_p;
6192 ssa_op_iter i;
6193 gphi *phi = si.phi ();
6194 tree res = gimple_phi_result (phi);
6196 if (virtual_operand_p (res))
6197 continue;
6199 FOR_EACH_PHI_ARG (arg_p, phi, i, SSA_OP_USE)
6201 tree arg = USE_FROM_PTR (arg_p);
6202 if (TREE_CODE (arg) == SSA_NAME)
6203 bitmap_set_bit (live, SSA_NAME_VERSION (arg));
6206 bitmap_clear_bit (live, SSA_NAME_VERSION (res));
6210 /* Do an RPO walk over the function computing SSA name liveness
6211 on-the-fly and deciding on assert expressions to insert. */
6213 static void
6214 find_assert_locations (void)
6216 int *rpo = XNEWVEC (int, last_basic_block_for_fn (cfun));
6217 int *bb_rpo = XNEWVEC (int, last_basic_block_for_fn (cfun));
6218 int *last_rpo = XCNEWVEC (int, last_basic_block_for_fn (cfun));
6219 int rpo_cnt, i;
6221 live = XCNEWVEC (sbitmap, last_basic_block_for_fn (cfun));
6222 rpo_cnt = pre_and_rev_post_order_compute (NULL, rpo, false);
6223 for (i = 0; i < rpo_cnt; ++i)
6224 bb_rpo[rpo[i]] = i;
6226 /* Pre-seed loop latch liveness from loop header PHI nodes. Due to
6227 the order we compute liveness and insert asserts we otherwise
6228 fail to insert asserts into the loop latch. */
6229 loop_p loop;
6230 FOR_EACH_LOOP (loop, 0)
6232 i = loop->latch->index;
6233 unsigned int j = single_succ_edge (loop->latch)->dest_idx;
6234 for (gphi_iterator gsi = gsi_start_phis (loop->header);
6235 !gsi_end_p (gsi); gsi_next (&gsi))
6237 gphi *phi = gsi.phi ();
6238 if (virtual_operand_p (gimple_phi_result (phi)))
6239 continue;
6240 tree arg = gimple_phi_arg_def (phi, j);
6241 if (TREE_CODE (arg) == SSA_NAME)
6243 if (live[i] == NULL)
6245 live[i] = sbitmap_alloc (num_ssa_names);
6246 bitmap_clear (live[i]);
6248 bitmap_set_bit (live[i], SSA_NAME_VERSION (arg));
6253 for (i = rpo_cnt - 1; i >= 0; --i)
6255 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, rpo[i]);
6256 edge e;
6257 edge_iterator ei;
6259 if (!live[rpo[i]])
6261 live[rpo[i]] = sbitmap_alloc (num_ssa_names);
6262 bitmap_clear (live[rpo[i]]);
6265 /* Process BB and update the live information with uses in
6266 this block. */
6267 find_assert_locations_1 (bb, live[rpo[i]]);
6269 /* Merge liveness into the predecessor blocks and free it. */
6270 if (!bitmap_empty_p (live[rpo[i]]))
6272 int pred_rpo = i;
6273 FOR_EACH_EDGE (e, ei, bb->preds)
6275 int pred = e->src->index;
6276 if ((e->flags & EDGE_DFS_BACK) || pred == ENTRY_BLOCK)
6277 continue;
6279 if (!live[pred])
6281 live[pred] = sbitmap_alloc (num_ssa_names);
6282 bitmap_clear (live[pred]);
6284 bitmap_ior (live[pred], live[pred], live[rpo[i]]);
6286 if (bb_rpo[pred] < pred_rpo)
6287 pred_rpo = bb_rpo[pred];
6290 /* Record the RPO number of the last visited block that needs
6291 live information from this block. */
6292 last_rpo[rpo[i]] = pred_rpo;
6294 else
6296 sbitmap_free (live[rpo[i]]);
6297 live[rpo[i]] = NULL;
6300 /* We can free all successors live bitmaps if all their
6301 predecessors have been visited already. */
6302 FOR_EACH_EDGE (e, ei, bb->succs)
6303 if (last_rpo[e->dest->index] == i
6304 && live[e->dest->index])
6306 sbitmap_free (live[e->dest->index]);
6307 live[e->dest->index] = NULL;
6311 XDELETEVEC (rpo);
6312 XDELETEVEC (bb_rpo);
6313 XDELETEVEC (last_rpo);
6314 for (i = 0; i < last_basic_block_for_fn (cfun); ++i)
6315 if (live[i])
6316 sbitmap_free (live[i]);
6317 XDELETEVEC (live);
6320 /* Create an ASSERT_EXPR for NAME and insert it in the location
6321 indicated by LOC. Return true if we made any edge insertions. */
6323 static bool
6324 process_assert_insertions_for (tree name, assert_locus *loc)
6326 /* Build the comparison expression NAME_i COMP_CODE VAL. */
6327 gimple *stmt;
6328 tree cond;
6329 gimple *assert_stmt;
6330 edge_iterator ei;
6331 edge e;
6333 /* If we have X <=> X do not insert an assert expr for that. */
6334 if (loc->expr == loc->val)
6335 return false;
6337 cond = build2 (loc->comp_code, boolean_type_node, loc->expr, loc->val);
6338 assert_stmt = build_assert_expr_for (cond, name);
6339 if (loc->e)
6341 /* We have been asked to insert the assertion on an edge. This
6342 is used only by COND_EXPR and SWITCH_EXPR assertions. */
6343 gcc_checking_assert (gimple_code (gsi_stmt (loc->si)) == GIMPLE_COND
6344 || (gimple_code (gsi_stmt (loc->si))
6345 == GIMPLE_SWITCH));
6347 gsi_insert_on_edge (loc->e, assert_stmt);
6348 return true;
6351 /* Otherwise, we can insert right after LOC->SI iff the
6352 statement must not be the last statement in the block. */
6353 stmt = gsi_stmt (loc->si);
6354 if (!stmt_ends_bb_p (stmt))
6356 gsi_insert_after (&loc->si, assert_stmt, GSI_SAME_STMT);
6357 return false;
6360 /* If STMT must be the last statement in BB, we can only insert new
6361 assertions on the non-abnormal edge out of BB. Note that since
6362 STMT is not control flow, there may only be one non-abnormal edge
6363 out of BB. */
6364 FOR_EACH_EDGE (e, ei, loc->bb->succs)
6365 if (!(e->flags & EDGE_ABNORMAL))
6367 gsi_insert_on_edge (e, assert_stmt);
6368 return true;
6371 gcc_unreachable ();
6375 /* Process all the insertions registered for every name N_i registered
6376 in NEED_ASSERT_FOR. The list of assertions to be inserted are
6377 found in ASSERTS_FOR[i]. */
6379 static void
6380 process_assert_insertions (void)
6382 unsigned i;
6383 bitmap_iterator bi;
6384 bool update_edges_p = false;
6385 int num_asserts = 0;
6387 if (dump_file && (dump_flags & TDF_DETAILS))
6388 dump_all_asserts (dump_file);
6390 EXECUTE_IF_SET_IN_BITMAP (need_assert_for, 0, i, bi)
6392 assert_locus *loc = asserts_for[i];
6393 gcc_assert (loc);
6395 while (loc)
6397 assert_locus *next = loc->next;
6398 update_edges_p |= process_assert_insertions_for (ssa_name (i), loc);
6399 free (loc);
6400 loc = next;
6401 num_asserts++;
6405 if (update_edges_p)
6406 gsi_commit_edge_inserts ();
6408 statistics_counter_event (cfun, "Number of ASSERT_EXPR expressions inserted",
6409 num_asserts);
6413 /* Traverse the flowgraph looking for conditional jumps to insert range
6414 expressions. These range expressions are meant to provide information
6415 to optimizations that need to reason in terms of value ranges. They
6416 will not be expanded into RTL. For instance, given:
6418 x = ...
6419 y = ...
6420 if (x < y)
6421 y = x - 2;
6422 else
6423 x = y + 3;
6425 this pass will transform the code into:
6427 x = ...
6428 y = ...
6429 if (x < y)
6431 x = ASSERT_EXPR <x, x < y>
6432 y = x - 2
6434 else
6436 y = ASSERT_EXPR <y, x >= y>
6437 x = y + 3
6440 The idea is that once copy and constant propagation have run, other
6441 optimizations will be able to determine what ranges of values can 'x'
6442 take in different paths of the code, simply by checking the reaching
6443 definition of 'x'. */
6445 static void
6446 insert_range_assertions (void)
6448 need_assert_for = BITMAP_ALLOC (NULL);
6449 asserts_for = XCNEWVEC (assert_locus *, num_ssa_names);
6451 calculate_dominance_info (CDI_DOMINATORS);
6453 find_assert_locations ();
6454 if (!bitmap_empty_p (need_assert_for))
6456 process_assert_insertions ();
6457 update_ssa (TODO_update_ssa_no_phi);
6460 if (dump_file && (dump_flags & TDF_DETAILS))
6462 fprintf (dump_file, "\nSSA form after inserting ASSERT_EXPRs\n");
6463 dump_function_to_file (current_function_decl, dump_file, dump_flags);
6466 free (asserts_for);
6467 BITMAP_FREE (need_assert_for);
6470 /* Checks one ARRAY_REF in REF, located at LOCUS. Ignores flexible arrays
6471 and "struct" hacks. If VRP can determine that the
6472 array subscript is a constant, check if it is outside valid
6473 range. If the array subscript is a RANGE, warn if it is
6474 non-overlapping with valid range.
6475 IGNORE_OFF_BY_ONE is true if the ARRAY_REF is inside a ADDR_EXPR. */
6477 static void
6478 check_array_ref (location_t location, tree ref, bool ignore_off_by_one)
6480 value_range *vr = NULL;
6481 tree low_sub, up_sub;
6482 tree low_bound, up_bound, up_bound_p1;
6483 tree base;
6485 if (TREE_NO_WARNING (ref))
6486 return;
6488 low_sub = up_sub = TREE_OPERAND (ref, 1);
6489 up_bound = array_ref_up_bound (ref);
6491 /* Can not check flexible arrays. */
6492 if (!up_bound
6493 || TREE_CODE (up_bound) != INTEGER_CST)
6494 return;
6496 /* Accesses to trailing arrays via pointers may access storage
6497 beyond the types array bounds. */
6498 base = get_base_address (ref);
6499 if ((warn_array_bounds < 2)
6500 && base && TREE_CODE (base) == MEM_REF)
6502 tree cref, next = NULL_TREE;
6504 if (TREE_CODE (TREE_OPERAND (ref, 0)) != COMPONENT_REF)
6505 return;
6507 cref = TREE_OPERAND (ref, 0);
6508 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (cref, 0))) == RECORD_TYPE)
6509 for (next = DECL_CHAIN (TREE_OPERAND (cref, 1));
6510 next && TREE_CODE (next) != FIELD_DECL;
6511 next = DECL_CHAIN (next))
6514 /* If this is the last field in a struct type or a field in a
6515 union type do not warn. */
6516 if (!next)
6517 return;
6520 low_bound = array_ref_low_bound (ref);
6521 up_bound_p1 = int_const_binop (PLUS_EXPR, up_bound,
6522 build_int_cst (TREE_TYPE (up_bound), 1));
6524 /* Empty array. */
6525 if (tree_int_cst_equal (low_bound, up_bound_p1))
6527 warning_at (location, OPT_Warray_bounds,
6528 "array subscript is above array bounds");
6529 TREE_NO_WARNING (ref) = 1;
6532 if (TREE_CODE (low_sub) == SSA_NAME)
6534 vr = get_value_range (low_sub);
6535 if (vr->type == VR_RANGE || vr->type == VR_ANTI_RANGE)
6537 low_sub = vr->type == VR_RANGE ? vr->max : vr->min;
6538 up_sub = vr->type == VR_RANGE ? vr->min : vr->max;
6542 if (vr && vr->type == VR_ANTI_RANGE)
6544 if (TREE_CODE (up_sub) == INTEGER_CST
6545 && (ignore_off_by_one
6546 ? tree_int_cst_lt (up_bound, up_sub)
6547 : tree_int_cst_le (up_bound, up_sub))
6548 && TREE_CODE (low_sub) == INTEGER_CST
6549 && tree_int_cst_le (low_sub, low_bound))
6551 warning_at (location, OPT_Warray_bounds,
6552 "array subscript is outside array bounds");
6553 TREE_NO_WARNING (ref) = 1;
6556 else if (TREE_CODE (up_sub) == INTEGER_CST
6557 && (ignore_off_by_one
6558 ? !tree_int_cst_le (up_sub, up_bound_p1)
6559 : !tree_int_cst_le (up_sub, up_bound)))
6561 if (dump_file && (dump_flags & TDF_DETAILS))
6563 fprintf (dump_file, "Array bound warning for ");
6564 dump_generic_expr (MSG_NOTE, TDF_SLIM, ref);
6565 fprintf (dump_file, "\n");
6567 warning_at (location, OPT_Warray_bounds,
6568 "array subscript is above array bounds");
6569 TREE_NO_WARNING (ref) = 1;
6571 else if (TREE_CODE (low_sub) == INTEGER_CST
6572 && tree_int_cst_lt (low_sub, low_bound))
6574 if (dump_file && (dump_flags & TDF_DETAILS))
6576 fprintf (dump_file, "Array bound warning for ");
6577 dump_generic_expr (MSG_NOTE, TDF_SLIM, ref);
6578 fprintf (dump_file, "\n");
6580 warning_at (location, OPT_Warray_bounds,
6581 "array subscript is below array bounds");
6582 TREE_NO_WARNING (ref) = 1;
6586 /* Searches if the expr T, located at LOCATION computes
6587 address of an ARRAY_REF, and call check_array_ref on it. */
6589 static void
6590 search_for_addr_array (tree t, location_t location)
6592 /* Check each ARRAY_REFs in the reference chain. */
6595 if (TREE_CODE (t) == ARRAY_REF)
6596 check_array_ref (location, t, true /*ignore_off_by_one*/);
6598 t = TREE_OPERAND (t, 0);
6600 while (handled_component_p (t));
6602 if (TREE_CODE (t) == MEM_REF
6603 && TREE_CODE (TREE_OPERAND (t, 0)) == ADDR_EXPR
6604 && !TREE_NO_WARNING (t))
6606 tree tem = TREE_OPERAND (TREE_OPERAND (t, 0), 0);
6607 tree low_bound, up_bound, el_sz;
6608 offset_int idx;
6609 if (TREE_CODE (TREE_TYPE (tem)) != ARRAY_TYPE
6610 || TREE_CODE (TREE_TYPE (TREE_TYPE (tem))) == ARRAY_TYPE
6611 || !TYPE_DOMAIN (TREE_TYPE (tem)))
6612 return;
6614 low_bound = TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (tem)));
6615 up_bound = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (tem)));
6616 el_sz = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (tem)));
6617 if (!low_bound
6618 || TREE_CODE (low_bound) != INTEGER_CST
6619 || !up_bound
6620 || TREE_CODE (up_bound) != INTEGER_CST
6621 || !el_sz
6622 || TREE_CODE (el_sz) != INTEGER_CST)
6623 return;
6625 idx = mem_ref_offset (t);
6626 idx = wi::sdiv_trunc (idx, wi::to_offset (el_sz));
6627 if (wi::lts_p (idx, 0))
6629 if (dump_file && (dump_flags & TDF_DETAILS))
6631 fprintf (dump_file, "Array bound warning for ");
6632 dump_generic_expr (MSG_NOTE, TDF_SLIM, t);
6633 fprintf (dump_file, "\n");
6635 warning_at (location, OPT_Warray_bounds,
6636 "array subscript is below array bounds");
6637 TREE_NO_WARNING (t) = 1;
6639 else if (wi::gts_p (idx, (wi::to_offset (up_bound)
6640 - wi::to_offset (low_bound) + 1)))
6642 if (dump_file && (dump_flags & TDF_DETAILS))
6644 fprintf (dump_file, "Array bound warning for ");
6645 dump_generic_expr (MSG_NOTE, TDF_SLIM, t);
6646 fprintf (dump_file, "\n");
6648 warning_at (location, OPT_Warray_bounds,
6649 "array subscript is above array bounds");
6650 TREE_NO_WARNING (t) = 1;
6655 /* walk_tree() callback that checks if *TP is
6656 an ARRAY_REF inside an ADDR_EXPR (in which an array
6657 subscript one outside the valid range is allowed). Call
6658 check_array_ref for each ARRAY_REF found. The location is
6659 passed in DATA. */
6661 static tree
6662 check_array_bounds (tree *tp, int *walk_subtree, void *data)
6664 tree t = *tp;
6665 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
6666 location_t location;
6668 if (EXPR_HAS_LOCATION (t))
6669 location = EXPR_LOCATION (t);
6670 else
6672 location_t *locp = (location_t *) wi->info;
6673 location = *locp;
6676 *walk_subtree = TRUE;
6678 if (TREE_CODE (t) == ARRAY_REF)
6679 check_array_ref (location, t, false /*ignore_off_by_one*/);
6681 else if (TREE_CODE (t) == ADDR_EXPR)
6683 search_for_addr_array (t, location);
6684 *walk_subtree = FALSE;
6687 return NULL_TREE;
6690 /* Walk over all statements of all reachable BBs and call check_array_bounds
6691 on them. */
6693 static void
6694 check_all_array_refs (void)
6696 basic_block bb;
6697 gimple_stmt_iterator si;
6699 FOR_EACH_BB_FN (bb, cfun)
6701 edge_iterator ei;
6702 edge e;
6703 bool executable = false;
6705 /* Skip blocks that were found to be unreachable. */
6706 FOR_EACH_EDGE (e, ei, bb->preds)
6707 executable |= !!(e->flags & EDGE_EXECUTABLE);
6708 if (!executable)
6709 continue;
6711 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
6713 gimple *stmt = gsi_stmt (si);
6714 struct walk_stmt_info wi;
6715 if (!gimple_has_location (stmt)
6716 || is_gimple_debug (stmt))
6717 continue;
6719 memset (&wi, 0, sizeof (wi));
6721 location_t loc = gimple_location (stmt);
6722 wi.info = &loc;
6724 walk_gimple_op (gsi_stmt (si),
6725 check_array_bounds,
6726 &wi);
6731 /* Return true if all imm uses of VAR are either in STMT, or
6732 feed (optionally through a chain of single imm uses) GIMPLE_COND
6733 in basic block COND_BB. */
6735 static bool
6736 all_imm_uses_in_stmt_or_feed_cond (tree var, gimple *stmt, basic_block cond_bb)
6738 use_operand_p use_p, use2_p;
6739 imm_use_iterator iter;
6741 FOR_EACH_IMM_USE_FAST (use_p, iter, var)
6742 if (USE_STMT (use_p) != stmt)
6744 gimple *use_stmt = USE_STMT (use_p), *use_stmt2;
6745 if (is_gimple_debug (use_stmt))
6746 continue;
6747 while (is_gimple_assign (use_stmt)
6748 && TREE_CODE (gimple_assign_lhs (use_stmt)) == SSA_NAME
6749 && single_imm_use (gimple_assign_lhs (use_stmt),
6750 &use2_p, &use_stmt2))
6751 use_stmt = use_stmt2;
6752 if (gimple_code (use_stmt) != GIMPLE_COND
6753 || gimple_bb (use_stmt) != cond_bb)
6754 return false;
6756 return true;
6759 /* Handle
6760 _4 = x_3 & 31;
6761 if (_4 != 0)
6762 goto <bb 6>;
6763 else
6764 goto <bb 7>;
6765 <bb 6>:
6766 __builtin_unreachable ();
6767 <bb 7>:
6768 x_5 = ASSERT_EXPR <x_3, ...>;
6769 If x_3 has no other immediate uses (checked by caller),
6770 var is the x_3 var from ASSERT_EXPR, we can clear low 5 bits
6771 from the non-zero bitmask. */
6773 static void
6774 maybe_set_nonzero_bits (basic_block bb, tree var)
6776 edge e = single_pred_edge (bb);
6777 basic_block cond_bb = e->src;
6778 gimple *stmt = last_stmt (cond_bb);
6779 tree cst;
6781 if (stmt == NULL
6782 || gimple_code (stmt) != GIMPLE_COND
6783 || gimple_cond_code (stmt) != ((e->flags & EDGE_TRUE_VALUE)
6784 ? EQ_EXPR : NE_EXPR)
6785 || TREE_CODE (gimple_cond_lhs (stmt)) != SSA_NAME
6786 || !integer_zerop (gimple_cond_rhs (stmt)))
6787 return;
6789 stmt = SSA_NAME_DEF_STMT (gimple_cond_lhs (stmt));
6790 if (!is_gimple_assign (stmt)
6791 || gimple_assign_rhs_code (stmt) != BIT_AND_EXPR
6792 || TREE_CODE (gimple_assign_rhs2 (stmt)) != INTEGER_CST)
6793 return;
6794 if (gimple_assign_rhs1 (stmt) != var)
6796 gimple *stmt2;
6798 if (TREE_CODE (gimple_assign_rhs1 (stmt)) != SSA_NAME)
6799 return;
6800 stmt2 = SSA_NAME_DEF_STMT (gimple_assign_rhs1 (stmt));
6801 if (!gimple_assign_cast_p (stmt2)
6802 || gimple_assign_rhs1 (stmt2) != var
6803 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (stmt2))
6804 || (TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (stmt)))
6805 != TYPE_PRECISION (TREE_TYPE (var))))
6806 return;
6808 cst = gimple_assign_rhs2 (stmt);
6809 set_nonzero_bits (var, wi::bit_and_not (get_nonzero_bits (var), cst));
6812 /* Convert range assertion expressions into the implied copies and
6813 copy propagate away the copies. Doing the trivial copy propagation
6814 here avoids the need to run the full copy propagation pass after
6815 VRP.
6817 FIXME, this will eventually lead to copy propagation removing the
6818 names that had useful range information attached to them. For
6819 instance, if we had the assertion N_i = ASSERT_EXPR <N_j, N_j > 3>,
6820 then N_i will have the range [3, +INF].
6822 However, by converting the assertion into the implied copy
6823 operation N_i = N_j, we will then copy-propagate N_j into the uses
6824 of N_i and lose the range information. We may want to hold on to
6825 ASSERT_EXPRs a little while longer as the ranges could be used in
6826 things like jump threading.
6828 The problem with keeping ASSERT_EXPRs around is that passes after
6829 VRP need to handle them appropriately.
6831 Another approach would be to make the range information a first
6832 class property of the SSA_NAME so that it can be queried from
6833 any pass. This is made somewhat more complex by the need for
6834 multiple ranges to be associated with one SSA_NAME. */
6836 static void
6837 remove_range_assertions (void)
6839 basic_block bb;
6840 gimple_stmt_iterator si;
6841 /* 1 if looking at ASSERT_EXPRs immediately at the beginning of
6842 a basic block preceeded by GIMPLE_COND branching to it and
6843 __builtin_trap, -1 if not yet checked, 0 otherwise. */
6844 int is_unreachable;
6846 /* Note that the BSI iterator bump happens at the bottom of the
6847 loop and no bump is necessary if we're removing the statement
6848 referenced by the current BSI. */
6849 FOR_EACH_BB_FN (bb, cfun)
6850 for (si = gsi_after_labels (bb), is_unreachable = -1; !gsi_end_p (si);)
6852 gimple *stmt = gsi_stmt (si);
6853 gimple *use_stmt;
6855 if (is_gimple_assign (stmt)
6856 && gimple_assign_rhs_code (stmt) == ASSERT_EXPR)
6858 tree lhs = gimple_assign_lhs (stmt);
6859 tree rhs = gimple_assign_rhs1 (stmt);
6860 tree var;
6861 tree cond = fold (ASSERT_EXPR_COND (rhs));
6862 use_operand_p use_p;
6863 imm_use_iterator iter;
6865 gcc_assert (cond != boolean_false_node);
6867 var = ASSERT_EXPR_VAR (rhs);
6868 gcc_assert (TREE_CODE (var) == SSA_NAME);
6870 if (!POINTER_TYPE_P (TREE_TYPE (lhs))
6871 && SSA_NAME_RANGE_INFO (lhs))
6873 if (is_unreachable == -1)
6875 is_unreachable = 0;
6876 if (single_pred_p (bb)
6877 && assert_unreachable_fallthru_edge_p
6878 (single_pred_edge (bb)))
6879 is_unreachable = 1;
6881 /* Handle
6882 if (x_7 >= 10 && x_7 < 20)
6883 __builtin_unreachable ();
6884 x_8 = ASSERT_EXPR <x_7, ...>;
6885 if the only uses of x_7 are in the ASSERT_EXPR and
6886 in the condition. In that case, we can copy the
6887 range info from x_8 computed in this pass also
6888 for x_7. */
6889 if (is_unreachable
6890 && all_imm_uses_in_stmt_or_feed_cond (var, stmt,
6891 single_pred (bb)))
6893 set_range_info (var, SSA_NAME_RANGE_TYPE (lhs),
6894 SSA_NAME_RANGE_INFO (lhs)->get_min (),
6895 SSA_NAME_RANGE_INFO (lhs)->get_max ());
6896 maybe_set_nonzero_bits (bb, var);
6900 /* Propagate the RHS into every use of the LHS. */
6901 FOR_EACH_IMM_USE_STMT (use_stmt, iter, lhs)
6902 FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
6903 SET_USE (use_p, var);
6905 /* And finally, remove the copy, it is not needed. */
6906 gsi_remove (&si, true);
6907 release_defs (stmt);
6909 else
6911 if (!is_gimple_debug (gsi_stmt (si)))
6912 is_unreachable = 0;
6913 gsi_next (&si);
6919 /* Return true if STMT is interesting for VRP. */
6921 static bool
6922 stmt_interesting_for_vrp (gimple *stmt)
6924 if (gimple_code (stmt) == GIMPLE_PHI)
6926 tree res = gimple_phi_result (stmt);
6927 return (!virtual_operand_p (res)
6928 && (INTEGRAL_TYPE_P (TREE_TYPE (res))
6929 || POINTER_TYPE_P (TREE_TYPE (res))));
6931 else if (is_gimple_assign (stmt) || is_gimple_call (stmt))
6933 tree lhs = gimple_get_lhs (stmt);
6935 /* In general, assignments with virtual operands are not useful
6936 for deriving ranges, with the obvious exception of calls to
6937 builtin functions. */
6938 if (lhs && TREE_CODE (lhs) == SSA_NAME
6939 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs))
6940 || POINTER_TYPE_P (TREE_TYPE (lhs)))
6941 && (is_gimple_call (stmt)
6942 || !gimple_vuse (stmt)))
6943 return true;
6944 else if (is_gimple_call (stmt) && gimple_call_internal_p (stmt))
6945 switch (gimple_call_internal_fn (stmt))
6947 case IFN_ADD_OVERFLOW:
6948 case IFN_SUB_OVERFLOW:
6949 case IFN_MUL_OVERFLOW:
6950 /* These internal calls return _Complex integer type,
6951 but are interesting to VRP nevertheless. */
6952 if (lhs && TREE_CODE (lhs) == SSA_NAME)
6953 return true;
6954 break;
6955 default:
6956 break;
6959 else if (gimple_code (stmt) == GIMPLE_COND
6960 || gimple_code (stmt) == GIMPLE_SWITCH)
6961 return true;
6963 return false;
6967 /* Initialize local data structures for VRP. */
6969 static void
6970 vrp_initialize (void)
6972 basic_block bb;
6974 values_propagated = false;
6975 num_vr_values = num_ssa_names;
6976 vr_value = XCNEWVEC (value_range *, num_vr_values);
6977 vr_phi_edge_counts = XCNEWVEC (int, num_ssa_names);
6979 FOR_EACH_BB_FN (bb, cfun)
6981 for (gphi_iterator si = gsi_start_phis (bb); !gsi_end_p (si);
6982 gsi_next (&si))
6984 gphi *phi = si.phi ();
6985 if (!stmt_interesting_for_vrp (phi))
6987 tree lhs = PHI_RESULT (phi);
6988 set_value_range_to_varying (get_value_range (lhs));
6989 prop_set_simulate_again (phi, false);
6991 else
6992 prop_set_simulate_again (phi, true);
6995 for (gimple_stmt_iterator si = gsi_start_bb (bb); !gsi_end_p (si);
6996 gsi_next (&si))
6998 gimple *stmt = gsi_stmt (si);
7000 /* If the statement is a control insn, then we do not
7001 want to avoid simulating the statement once. Failure
7002 to do so means that those edges will never get added. */
7003 if (stmt_ends_bb_p (stmt))
7004 prop_set_simulate_again (stmt, true);
7005 else if (!stmt_interesting_for_vrp (stmt))
7007 ssa_op_iter i;
7008 tree def;
7009 FOR_EACH_SSA_TREE_OPERAND (def, stmt, i, SSA_OP_DEF)
7010 set_value_range_to_varying (get_value_range (def));
7011 prop_set_simulate_again (stmt, false);
7013 else
7014 prop_set_simulate_again (stmt, true);
7019 /* Return the singleton value-range for NAME or NAME. */
7021 static inline tree
7022 vrp_valueize (tree name)
7024 if (TREE_CODE (name) == SSA_NAME)
7026 value_range *vr = get_value_range (name);
7027 if (vr->type == VR_RANGE
7028 && (vr->min == vr->max
7029 || operand_equal_p (vr->min, vr->max, 0)))
7030 return vr->min;
7032 return name;
7035 /* Return the singleton value-range for NAME if that is a constant
7036 but signal to not follow SSA edges. */
7038 static inline tree
7039 vrp_valueize_1 (tree name)
7041 if (TREE_CODE (name) == SSA_NAME)
7043 /* If the definition may be simulated again we cannot follow
7044 this SSA edge as the SSA propagator does not necessarily
7045 re-visit the use. */
7046 gimple *def_stmt = SSA_NAME_DEF_STMT (name);
7047 if (!gimple_nop_p (def_stmt)
7048 && prop_simulate_again_p (def_stmt))
7049 return NULL_TREE;
7050 value_range *vr = get_value_range (name);
7051 if (range_int_cst_singleton_p (vr))
7052 return vr->min;
7054 return name;
7057 /* Visit assignment STMT. If it produces an interesting range, record
7058 the SSA name in *OUTPUT_P. */
7060 static enum ssa_prop_result
7061 vrp_visit_assignment_or_call (gimple *stmt, tree *output_p)
7063 tree def, lhs;
7064 ssa_op_iter iter;
7065 enum gimple_code code = gimple_code (stmt);
7066 lhs = gimple_get_lhs (stmt);
7068 /* We only keep track of ranges in integral and pointer types. */
7069 if (TREE_CODE (lhs) == SSA_NAME
7070 && ((INTEGRAL_TYPE_P (TREE_TYPE (lhs))
7071 /* It is valid to have NULL MIN/MAX values on a type. See
7072 build_range_type. */
7073 && TYPE_MIN_VALUE (TREE_TYPE (lhs))
7074 && TYPE_MAX_VALUE (TREE_TYPE (lhs)))
7075 || POINTER_TYPE_P (TREE_TYPE (lhs))))
7077 value_range new_vr = VR_INITIALIZER;
7079 /* Try folding the statement to a constant first. */
7080 tree tem = gimple_fold_stmt_to_constant_1 (stmt, vrp_valueize,
7081 vrp_valueize_1);
7082 if (tem && is_gimple_min_invariant (tem))
7083 set_value_range_to_value (&new_vr, tem, NULL);
7084 /* Then dispatch to value-range extracting functions. */
7085 else if (code == GIMPLE_CALL)
7086 extract_range_basic (&new_vr, stmt);
7087 else
7088 extract_range_from_assignment (&new_vr, as_a <gassign *> (stmt));
7090 if (update_value_range (lhs, &new_vr))
7092 *output_p = lhs;
7094 if (dump_file && (dump_flags & TDF_DETAILS))
7096 fprintf (dump_file, "Found new range for ");
7097 print_generic_expr (dump_file, lhs, 0);
7098 fprintf (dump_file, ": ");
7099 dump_value_range (dump_file, &new_vr);
7100 fprintf (dump_file, "\n");
7103 if (new_vr.type == VR_VARYING)
7104 return SSA_PROP_VARYING;
7106 return SSA_PROP_INTERESTING;
7109 return SSA_PROP_NOT_INTERESTING;
7111 else if (is_gimple_call (stmt) && gimple_call_internal_p (stmt))
7112 switch (gimple_call_internal_fn (stmt))
7114 case IFN_ADD_OVERFLOW:
7115 case IFN_SUB_OVERFLOW:
7116 case IFN_MUL_OVERFLOW:
7117 /* These internal calls return _Complex integer type,
7118 which VRP does not track, but the immediate uses
7119 thereof might be interesting. */
7120 if (lhs && TREE_CODE (lhs) == SSA_NAME)
7122 imm_use_iterator iter;
7123 use_operand_p use_p;
7124 enum ssa_prop_result res = SSA_PROP_VARYING;
7126 set_value_range_to_varying (get_value_range (lhs));
7128 FOR_EACH_IMM_USE_FAST (use_p, iter, lhs)
7130 gimple *use_stmt = USE_STMT (use_p);
7131 if (!is_gimple_assign (use_stmt))
7132 continue;
7133 enum tree_code rhs_code = gimple_assign_rhs_code (use_stmt);
7134 if (rhs_code != REALPART_EXPR && rhs_code != IMAGPART_EXPR)
7135 continue;
7136 tree rhs1 = gimple_assign_rhs1 (use_stmt);
7137 tree use_lhs = gimple_assign_lhs (use_stmt);
7138 if (TREE_CODE (rhs1) != rhs_code
7139 || TREE_OPERAND (rhs1, 0) != lhs
7140 || TREE_CODE (use_lhs) != SSA_NAME
7141 || !stmt_interesting_for_vrp (use_stmt)
7142 || (!INTEGRAL_TYPE_P (TREE_TYPE (use_lhs))
7143 || !TYPE_MIN_VALUE (TREE_TYPE (use_lhs))
7144 || !TYPE_MAX_VALUE (TREE_TYPE (use_lhs))))
7145 continue;
7147 /* If there is a change in the value range for any of the
7148 REALPART_EXPR/IMAGPART_EXPR immediate uses, return
7149 SSA_PROP_INTERESTING. If there are any REALPART_EXPR
7150 or IMAGPART_EXPR immediate uses, but none of them have
7151 a change in their value ranges, return
7152 SSA_PROP_NOT_INTERESTING. If there are no
7153 {REAL,IMAG}PART_EXPR uses at all,
7154 return SSA_PROP_VARYING. */
7155 value_range new_vr = VR_INITIALIZER;
7156 extract_range_basic (&new_vr, use_stmt);
7157 value_range *old_vr = get_value_range (use_lhs);
7158 if (old_vr->type != new_vr.type
7159 || !vrp_operand_equal_p (old_vr->min, new_vr.min)
7160 || !vrp_operand_equal_p (old_vr->max, new_vr.max)
7161 || !vrp_bitmap_equal_p (old_vr->equiv, new_vr.equiv))
7162 res = SSA_PROP_INTERESTING;
7163 else
7164 res = SSA_PROP_NOT_INTERESTING;
7165 BITMAP_FREE (new_vr.equiv);
7166 if (res == SSA_PROP_INTERESTING)
7168 *output_p = lhs;
7169 return res;
7173 return res;
7175 break;
7176 default:
7177 break;
7180 /* Every other statement produces no useful ranges. */
7181 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_DEF)
7182 set_value_range_to_varying (get_value_range (def));
7184 return SSA_PROP_VARYING;
7187 /* Helper that gets the value range of the SSA_NAME with version I
7188 or a symbolic range containing the SSA_NAME only if the value range
7189 is varying or undefined. */
7191 static inline value_range
7192 get_vr_for_comparison (int i)
7194 value_range vr = *get_value_range (ssa_name (i));
7196 /* If name N_i does not have a valid range, use N_i as its own
7197 range. This allows us to compare against names that may
7198 have N_i in their ranges. */
7199 if (vr.type == VR_VARYING || vr.type == VR_UNDEFINED)
7201 vr.type = VR_RANGE;
7202 vr.min = ssa_name (i);
7203 vr.max = ssa_name (i);
7206 return vr;
7209 /* Compare all the value ranges for names equivalent to VAR with VAL
7210 using comparison code COMP. Return the same value returned by
7211 compare_range_with_value, including the setting of
7212 *STRICT_OVERFLOW_P. */
7214 static tree
7215 compare_name_with_value (enum tree_code comp, tree var, tree val,
7216 bool *strict_overflow_p)
7218 bitmap_iterator bi;
7219 unsigned i;
7220 bitmap e;
7221 tree retval, t;
7222 int used_strict_overflow;
7223 bool sop;
7224 value_range equiv_vr;
7226 /* Get the set of equivalences for VAR. */
7227 e = get_value_range (var)->equiv;
7229 /* Start at -1. Set it to 0 if we do a comparison without relying
7230 on overflow, or 1 if all comparisons rely on overflow. */
7231 used_strict_overflow = -1;
7233 /* Compare vars' value range with val. */
7234 equiv_vr = get_vr_for_comparison (SSA_NAME_VERSION (var));
7235 sop = false;
7236 retval = compare_range_with_value (comp, &equiv_vr, val, &sop);
7237 if (retval)
7238 used_strict_overflow = sop ? 1 : 0;
7240 /* If the equiv set is empty we have done all work we need to do. */
7241 if (e == NULL)
7243 if (retval
7244 && used_strict_overflow > 0)
7245 *strict_overflow_p = true;
7246 return retval;
7249 EXECUTE_IF_SET_IN_BITMAP (e, 0, i, bi)
7251 equiv_vr = get_vr_for_comparison (i);
7252 sop = false;
7253 t = compare_range_with_value (comp, &equiv_vr, val, &sop);
7254 if (t)
7256 /* If we get different answers from different members
7257 of the equivalence set this check must be in a dead
7258 code region. Folding it to a trap representation
7259 would be correct here. For now just return don't-know. */
7260 if (retval != NULL
7261 && t != retval)
7263 retval = NULL_TREE;
7264 break;
7266 retval = t;
7268 if (!sop)
7269 used_strict_overflow = 0;
7270 else if (used_strict_overflow < 0)
7271 used_strict_overflow = 1;
7275 if (retval
7276 && used_strict_overflow > 0)
7277 *strict_overflow_p = true;
7279 return retval;
7283 /* Given a comparison code COMP and names N1 and N2, compare all the
7284 ranges equivalent to N1 against all the ranges equivalent to N2
7285 to determine the value of N1 COMP N2. Return the same value
7286 returned by compare_ranges. Set *STRICT_OVERFLOW_P to indicate
7287 whether we relied on an overflow infinity in the comparison. */
7290 static tree
7291 compare_names (enum tree_code comp, tree n1, tree n2,
7292 bool *strict_overflow_p)
7294 tree t, retval;
7295 bitmap e1, e2;
7296 bitmap_iterator bi1, bi2;
7297 unsigned i1, i2;
7298 int used_strict_overflow;
7299 static bitmap_obstack *s_obstack = NULL;
7300 static bitmap s_e1 = NULL, s_e2 = NULL;
7302 /* Compare the ranges of every name equivalent to N1 against the
7303 ranges of every name equivalent to N2. */
7304 e1 = get_value_range (n1)->equiv;
7305 e2 = get_value_range (n2)->equiv;
7307 /* Use the fake bitmaps if e1 or e2 are not available. */
7308 if (s_obstack == NULL)
7310 s_obstack = XNEW (bitmap_obstack);
7311 bitmap_obstack_initialize (s_obstack);
7312 s_e1 = BITMAP_ALLOC (s_obstack);
7313 s_e2 = BITMAP_ALLOC (s_obstack);
7315 if (e1 == NULL)
7316 e1 = s_e1;
7317 if (e2 == NULL)
7318 e2 = s_e2;
7320 /* Add N1 and N2 to their own set of equivalences to avoid
7321 duplicating the body of the loop just to check N1 and N2
7322 ranges. */
7323 bitmap_set_bit (e1, SSA_NAME_VERSION (n1));
7324 bitmap_set_bit (e2, SSA_NAME_VERSION (n2));
7326 /* If the equivalence sets have a common intersection, then the two
7327 names can be compared without checking their ranges. */
7328 if (bitmap_intersect_p (e1, e2))
7330 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
7331 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
7333 return (comp == EQ_EXPR || comp == GE_EXPR || comp == LE_EXPR)
7334 ? boolean_true_node
7335 : boolean_false_node;
7338 /* Start at -1. Set it to 0 if we do a comparison without relying
7339 on overflow, or 1 if all comparisons rely on overflow. */
7340 used_strict_overflow = -1;
7342 /* Otherwise, compare all the equivalent ranges. First, add N1 and
7343 N2 to their own set of equivalences to avoid duplicating the body
7344 of the loop just to check N1 and N2 ranges. */
7345 EXECUTE_IF_SET_IN_BITMAP (e1, 0, i1, bi1)
7347 value_range vr1 = get_vr_for_comparison (i1);
7349 t = retval = NULL_TREE;
7350 EXECUTE_IF_SET_IN_BITMAP (e2, 0, i2, bi2)
7352 bool sop = false;
7354 value_range vr2 = get_vr_for_comparison (i2);
7356 t = compare_ranges (comp, &vr1, &vr2, &sop);
7357 if (t)
7359 /* If we get different answers from different members
7360 of the equivalence set this check must be in a dead
7361 code region. Folding it to a trap representation
7362 would be correct here. For now just return don't-know. */
7363 if (retval != NULL
7364 && t != retval)
7366 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
7367 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
7368 return NULL_TREE;
7370 retval = t;
7372 if (!sop)
7373 used_strict_overflow = 0;
7374 else if (used_strict_overflow < 0)
7375 used_strict_overflow = 1;
7379 if (retval)
7381 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
7382 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
7383 if (used_strict_overflow > 0)
7384 *strict_overflow_p = true;
7385 return retval;
7389 /* None of the equivalent ranges are useful in computing this
7390 comparison. */
7391 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
7392 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
7393 return NULL_TREE;
7396 /* Helper function for vrp_evaluate_conditional_warnv & other
7397 optimizers. */
7399 static tree
7400 vrp_evaluate_conditional_warnv_with_ops_using_ranges (enum tree_code code,
7401 tree op0, tree op1,
7402 bool * strict_overflow_p)
7404 value_range *vr0, *vr1;
7406 vr0 = (TREE_CODE (op0) == SSA_NAME) ? get_value_range (op0) : NULL;
7407 vr1 = (TREE_CODE (op1) == SSA_NAME) ? get_value_range (op1) : NULL;
7409 tree res = NULL_TREE;
7410 if (vr0 && vr1)
7411 res = compare_ranges (code, vr0, vr1, strict_overflow_p);
7412 if (!res && vr0)
7413 res = compare_range_with_value (code, vr0, op1, strict_overflow_p);
7414 if (!res && vr1)
7415 res = (compare_range_with_value
7416 (swap_tree_comparison (code), vr1, op0, strict_overflow_p));
7417 return res;
7420 /* Helper function for vrp_evaluate_conditional_warnv. */
7422 static tree
7423 vrp_evaluate_conditional_warnv_with_ops (enum tree_code code, tree op0,
7424 tree op1, bool use_equiv_p,
7425 bool *strict_overflow_p, bool *only_ranges)
7427 tree ret;
7428 if (only_ranges)
7429 *only_ranges = true;
7431 /* We only deal with integral and pointer types. */
7432 if (!INTEGRAL_TYPE_P (TREE_TYPE (op0))
7433 && !POINTER_TYPE_P (TREE_TYPE (op0)))
7434 return NULL_TREE;
7436 if (use_equiv_p)
7438 if (only_ranges
7439 && (ret = vrp_evaluate_conditional_warnv_with_ops_using_ranges
7440 (code, op0, op1, strict_overflow_p)))
7441 return ret;
7442 *only_ranges = false;
7443 if (TREE_CODE (op0) == SSA_NAME && TREE_CODE (op1) == SSA_NAME)
7444 return compare_names (code, op0, op1, strict_overflow_p);
7445 else if (TREE_CODE (op0) == SSA_NAME)
7446 return compare_name_with_value (code, op0, op1, strict_overflow_p);
7447 else if (TREE_CODE (op1) == SSA_NAME)
7448 return (compare_name_with_value
7449 (swap_tree_comparison (code), op1, op0, strict_overflow_p));
7451 else
7452 return vrp_evaluate_conditional_warnv_with_ops_using_ranges (code, op0, op1,
7453 strict_overflow_p);
7454 return NULL_TREE;
7457 /* Given (CODE OP0 OP1) within STMT, try to simplify it based on value range
7458 information. Return NULL if the conditional can not be evaluated.
7459 The ranges of all the names equivalent with the operands in COND
7460 will be used when trying to compute the value. If the result is
7461 based on undefined signed overflow, issue a warning if
7462 appropriate. */
7464 static tree
7465 vrp_evaluate_conditional (tree_code code, tree op0, tree op1, gimple *stmt)
7467 bool sop;
7468 tree ret;
7469 bool only_ranges;
7471 /* Some passes and foldings leak constants with overflow flag set
7472 into the IL. Avoid doing wrong things with these and bail out. */
7473 if ((TREE_CODE (op0) == INTEGER_CST
7474 && TREE_OVERFLOW (op0))
7475 || (TREE_CODE (op1) == INTEGER_CST
7476 && TREE_OVERFLOW (op1)))
7477 return NULL_TREE;
7479 sop = false;
7480 ret = vrp_evaluate_conditional_warnv_with_ops (code, op0, op1, true, &sop,
7481 &only_ranges);
7483 if (ret && sop)
7485 enum warn_strict_overflow_code wc;
7486 const char* warnmsg;
7488 if (is_gimple_min_invariant (ret))
7490 wc = WARN_STRICT_OVERFLOW_CONDITIONAL;
7491 warnmsg = G_("assuming signed overflow does not occur when "
7492 "simplifying conditional to constant");
7494 else
7496 wc = WARN_STRICT_OVERFLOW_COMPARISON;
7497 warnmsg = G_("assuming signed overflow does not occur when "
7498 "simplifying conditional");
7501 if (issue_strict_overflow_warning (wc))
7503 location_t location;
7505 if (!gimple_has_location (stmt))
7506 location = input_location;
7507 else
7508 location = gimple_location (stmt);
7509 warning_at (location, OPT_Wstrict_overflow, "%s", warnmsg);
7513 if (warn_type_limits
7514 && ret && only_ranges
7515 && TREE_CODE_CLASS (code) == tcc_comparison
7516 && TREE_CODE (op0) == SSA_NAME)
7518 /* If the comparison is being folded and the operand on the LHS
7519 is being compared against a constant value that is outside of
7520 the natural range of OP0's type, then the predicate will
7521 always fold regardless of the value of OP0. If -Wtype-limits
7522 was specified, emit a warning. */
7523 tree type = TREE_TYPE (op0);
7524 value_range *vr0 = get_value_range (op0);
7526 if (vr0->type == VR_RANGE
7527 && INTEGRAL_TYPE_P (type)
7528 && vrp_val_is_min (vr0->min)
7529 && vrp_val_is_max (vr0->max)
7530 && is_gimple_min_invariant (op1))
7532 location_t location;
7534 if (!gimple_has_location (stmt))
7535 location = input_location;
7536 else
7537 location = gimple_location (stmt);
7539 warning_at (location, OPT_Wtype_limits,
7540 integer_zerop (ret)
7541 ? G_("comparison always false "
7542 "due to limited range of data type")
7543 : G_("comparison always true "
7544 "due to limited range of data type"));
7548 return ret;
7552 /* Visit conditional statement STMT. If we can determine which edge
7553 will be taken out of STMT's basic block, record it in
7554 *TAKEN_EDGE_P and return SSA_PROP_INTERESTING. Otherwise, return
7555 SSA_PROP_VARYING. */
7557 static enum ssa_prop_result
7558 vrp_visit_cond_stmt (gcond *stmt, edge *taken_edge_p)
7560 tree val;
7561 bool sop;
7563 *taken_edge_p = NULL;
7565 if (dump_file && (dump_flags & TDF_DETAILS))
7567 tree use;
7568 ssa_op_iter i;
7570 fprintf (dump_file, "\nVisiting conditional with predicate: ");
7571 print_gimple_stmt (dump_file, stmt, 0, 0);
7572 fprintf (dump_file, "\nWith known ranges\n");
7574 FOR_EACH_SSA_TREE_OPERAND (use, stmt, i, SSA_OP_USE)
7576 fprintf (dump_file, "\t");
7577 print_generic_expr (dump_file, use, 0);
7578 fprintf (dump_file, ": ");
7579 dump_value_range (dump_file, vr_value[SSA_NAME_VERSION (use)]);
7582 fprintf (dump_file, "\n");
7585 /* Compute the value of the predicate COND by checking the known
7586 ranges of each of its operands.
7588 Note that we cannot evaluate all the equivalent ranges here
7589 because those ranges may not yet be final and with the current
7590 propagation strategy, we cannot determine when the value ranges
7591 of the names in the equivalence set have changed.
7593 For instance, given the following code fragment
7595 i_5 = PHI <8, i_13>
7597 i_14 = ASSERT_EXPR <i_5, i_5 != 0>
7598 if (i_14 == 1)
7601 Assume that on the first visit to i_14, i_5 has the temporary
7602 range [8, 8] because the second argument to the PHI function is
7603 not yet executable. We derive the range ~[0, 0] for i_14 and the
7604 equivalence set { i_5 }. So, when we visit 'if (i_14 == 1)' for
7605 the first time, since i_14 is equivalent to the range [8, 8], we
7606 determine that the predicate is always false.
7608 On the next round of propagation, i_13 is determined to be
7609 VARYING, which causes i_5 to drop down to VARYING. So, another
7610 visit to i_14 is scheduled. In this second visit, we compute the
7611 exact same range and equivalence set for i_14, namely ~[0, 0] and
7612 { i_5 }. But we did not have the previous range for i_5
7613 registered, so vrp_visit_assignment thinks that the range for
7614 i_14 has not changed. Therefore, the predicate 'if (i_14 == 1)'
7615 is not visited again, which stops propagation from visiting
7616 statements in the THEN clause of that if().
7618 To properly fix this we would need to keep the previous range
7619 value for the names in the equivalence set. This way we would've
7620 discovered that from one visit to the other i_5 changed from
7621 range [8, 8] to VR_VARYING.
7623 However, fixing this apparent limitation may not be worth the
7624 additional checking. Testing on several code bases (GCC, DLV,
7625 MICO, TRAMP3D and SPEC2000) showed that doing this results in
7626 4 more predicates folded in SPEC. */
7627 sop = false;
7629 val = vrp_evaluate_conditional_warnv_with_ops (gimple_cond_code (stmt),
7630 gimple_cond_lhs (stmt),
7631 gimple_cond_rhs (stmt),
7632 false, &sop, NULL);
7633 if (val)
7635 if (!sop)
7636 *taken_edge_p = find_taken_edge (gimple_bb (stmt), val);
7637 else
7639 if (dump_file && (dump_flags & TDF_DETAILS))
7640 fprintf (dump_file,
7641 "\nIgnoring predicate evaluation because "
7642 "it assumes that signed overflow is undefined");
7643 val = NULL_TREE;
7647 if (dump_file && (dump_flags & TDF_DETAILS))
7649 fprintf (dump_file, "\nPredicate evaluates to: ");
7650 if (val == NULL_TREE)
7651 fprintf (dump_file, "DON'T KNOW\n");
7652 else
7653 print_generic_stmt (dump_file, val, 0);
7656 return (*taken_edge_p) ? SSA_PROP_INTERESTING : SSA_PROP_VARYING;
7659 /* Searches the case label vector VEC for the index *IDX of the CASE_LABEL
7660 that includes the value VAL. The search is restricted to the range
7661 [START_IDX, n - 1] where n is the size of VEC.
7663 If there is a CASE_LABEL for VAL, its index is placed in IDX and true is
7664 returned.
7666 If there is no CASE_LABEL for VAL and there is one that is larger than VAL,
7667 it is placed in IDX and false is returned.
7669 If VAL is larger than any CASE_LABEL, n is placed on IDX and false is
7670 returned. */
7672 static bool
7673 find_case_label_index (gswitch *stmt, size_t start_idx, tree val, size_t *idx)
7675 size_t n = gimple_switch_num_labels (stmt);
7676 size_t low, high;
7678 /* Find case label for minimum of the value range or the next one.
7679 At each iteration we are searching in [low, high - 1]. */
7681 for (low = start_idx, high = n; high != low; )
7683 tree t;
7684 int cmp;
7685 /* Note that i != high, so we never ask for n. */
7686 size_t i = (high + low) / 2;
7687 t = gimple_switch_label (stmt, i);
7689 /* Cache the result of comparing CASE_LOW and val. */
7690 cmp = tree_int_cst_compare (CASE_LOW (t), val);
7692 if (cmp == 0)
7694 /* Ranges cannot be empty. */
7695 *idx = i;
7696 return true;
7698 else if (cmp > 0)
7699 high = i;
7700 else
7702 low = i + 1;
7703 if (CASE_HIGH (t) != NULL
7704 && tree_int_cst_compare (CASE_HIGH (t), val) >= 0)
7706 *idx = i;
7707 return true;
7712 *idx = high;
7713 return false;
7716 /* Searches the case label vector VEC for the range of CASE_LABELs that is used
7717 for values between MIN and MAX. The first index is placed in MIN_IDX. The
7718 last index is placed in MAX_IDX. If the range of CASE_LABELs is empty
7719 then MAX_IDX < MIN_IDX.
7720 Returns true if the default label is not needed. */
7722 static bool
7723 find_case_label_range (gswitch *stmt, tree min, tree max, size_t *min_idx,
7724 size_t *max_idx)
7726 size_t i, j;
7727 bool min_take_default = !find_case_label_index (stmt, 1, min, &i);
7728 bool max_take_default = !find_case_label_index (stmt, i, max, &j);
7730 if (i == j
7731 && min_take_default
7732 && max_take_default)
7734 /* Only the default case label reached.
7735 Return an empty range. */
7736 *min_idx = 1;
7737 *max_idx = 0;
7738 return false;
7740 else
7742 bool take_default = min_take_default || max_take_default;
7743 tree low, high;
7744 size_t k;
7746 if (max_take_default)
7747 j--;
7749 /* If the case label range is continuous, we do not need
7750 the default case label. Verify that. */
7751 high = CASE_LOW (gimple_switch_label (stmt, i));
7752 if (CASE_HIGH (gimple_switch_label (stmt, i)))
7753 high = CASE_HIGH (gimple_switch_label (stmt, i));
7754 for (k = i + 1; k <= j; ++k)
7756 low = CASE_LOW (gimple_switch_label (stmt, k));
7757 if (!integer_onep (int_const_binop (MINUS_EXPR, low, high)))
7759 take_default = true;
7760 break;
7762 high = low;
7763 if (CASE_HIGH (gimple_switch_label (stmt, k)))
7764 high = CASE_HIGH (gimple_switch_label (stmt, k));
7767 *min_idx = i;
7768 *max_idx = j;
7769 return !take_default;
7773 /* Searches the case label vector VEC for the ranges of CASE_LABELs that are
7774 used in range VR. The indices are placed in MIN_IDX1, MAX_IDX, MIN_IDX2 and
7775 MAX_IDX2. If the ranges of CASE_LABELs are empty then MAX_IDX1 < MIN_IDX1.
7776 Returns true if the default label is not needed. */
7778 static bool
7779 find_case_label_ranges (gswitch *stmt, value_range *vr, size_t *min_idx1,
7780 size_t *max_idx1, size_t *min_idx2,
7781 size_t *max_idx2)
7783 size_t i, j, k, l;
7784 unsigned int n = gimple_switch_num_labels (stmt);
7785 bool take_default;
7786 tree case_low, case_high;
7787 tree min = vr->min, max = vr->max;
7789 gcc_checking_assert (vr->type == VR_RANGE || vr->type == VR_ANTI_RANGE);
7791 take_default = !find_case_label_range (stmt, min, max, &i, &j);
7793 /* Set second range to emtpy. */
7794 *min_idx2 = 1;
7795 *max_idx2 = 0;
7797 if (vr->type == VR_RANGE)
7799 *min_idx1 = i;
7800 *max_idx1 = j;
7801 return !take_default;
7804 /* Set first range to all case labels. */
7805 *min_idx1 = 1;
7806 *max_idx1 = n - 1;
7808 if (i > j)
7809 return false;
7811 /* Make sure all the values of case labels [i , j] are contained in
7812 range [MIN, MAX]. */
7813 case_low = CASE_LOW (gimple_switch_label (stmt, i));
7814 case_high = CASE_HIGH (gimple_switch_label (stmt, j));
7815 if (tree_int_cst_compare (case_low, min) < 0)
7816 i += 1;
7817 if (case_high != NULL_TREE
7818 && tree_int_cst_compare (max, case_high) < 0)
7819 j -= 1;
7821 if (i > j)
7822 return false;
7824 /* If the range spans case labels [i, j], the corresponding anti-range spans
7825 the labels [1, i - 1] and [j + 1, n - 1]. */
7826 k = j + 1;
7827 l = n - 1;
7828 if (k > l)
7830 k = 1;
7831 l = 0;
7834 j = i - 1;
7835 i = 1;
7836 if (i > j)
7838 i = k;
7839 j = l;
7840 k = 1;
7841 l = 0;
7844 *min_idx1 = i;
7845 *max_idx1 = j;
7846 *min_idx2 = k;
7847 *max_idx2 = l;
7848 return false;
7851 /* Visit switch statement STMT. If we can determine which edge
7852 will be taken out of STMT's basic block, record it in
7853 *TAKEN_EDGE_P and return SSA_PROP_INTERESTING. Otherwise, return
7854 SSA_PROP_VARYING. */
7856 static enum ssa_prop_result
7857 vrp_visit_switch_stmt (gswitch *stmt, edge *taken_edge_p)
7859 tree op, val;
7860 value_range *vr;
7861 size_t i = 0, j = 0, k, l;
7862 bool take_default;
7864 *taken_edge_p = NULL;
7865 op = gimple_switch_index (stmt);
7866 if (TREE_CODE (op) != SSA_NAME)
7867 return SSA_PROP_VARYING;
7869 vr = get_value_range (op);
7870 if (dump_file && (dump_flags & TDF_DETAILS))
7872 fprintf (dump_file, "\nVisiting switch expression with operand ");
7873 print_generic_expr (dump_file, op, 0);
7874 fprintf (dump_file, " with known range ");
7875 dump_value_range (dump_file, vr);
7876 fprintf (dump_file, "\n");
7879 if ((vr->type != VR_RANGE
7880 && vr->type != VR_ANTI_RANGE)
7881 || symbolic_range_p (vr))
7882 return SSA_PROP_VARYING;
7884 /* Find the single edge that is taken from the switch expression. */
7885 take_default = !find_case_label_ranges (stmt, vr, &i, &j, &k, &l);
7887 /* Check if the range spans no CASE_LABEL. If so, we only reach the default
7888 label */
7889 if (j < i)
7891 gcc_assert (take_default);
7892 val = gimple_switch_default_label (stmt);
7894 else
7896 /* Check if labels with index i to j and maybe the default label
7897 are all reaching the same label. */
7899 val = gimple_switch_label (stmt, i);
7900 if (take_default
7901 && CASE_LABEL (gimple_switch_default_label (stmt))
7902 != CASE_LABEL (val))
7904 if (dump_file && (dump_flags & TDF_DETAILS))
7905 fprintf (dump_file, " not a single destination for this "
7906 "range\n");
7907 return SSA_PROP_VARYING;
7909 for (++i; i <= j; ++i)
7911 if (CASE_LABEL (gimple_switch_label (stmt, i)) != CASE_LABEL (val))
7913 if (dump_file && (dump_flags & TDF_DETAILS))
7914 fprintf (dump_file, " not a single destination for this "
7915 "range\n");
7916 return SSA_PROP_VARYING;
7919 for (; k <= l; ++k)
7921 if (CASE_LABEL (gimple_switch_label (stmt, k)) != CASE_LABEL (val))
7923 if (dump_file && (dump_flags & TDF_DETAILS))
7924 fprintf (dump_file, " not a single destination for this "
7925 "range\n");
7926 return SSA_PROP_VARYING;
7931 *taken_edge_p = find_edge (gimple_bb (stmt),
7932 label_to_block (CASE_LABEL (val)));
7934 if (dump_file && (dump_flags & TDF_DETAILS))
7936 fprintf (dump_file, " will take edge to ");
7937 print_generic_stmt (dump_file, CASE_LABEL (val), 0);
7940 return SSA_PROP_INTERESTING;
7944 /* Evaluate statement STMT. If the statement produces a useful range,
7945 return SSA_PROP_INTERESTING and record the SSA name with the
7946 interesting range into *OUTPUT_P.
7948 If STMT is a conditional branch and we can determine its truth
7949 value, the taken edge is recorded in *TAKEN_EDGE_P.
7951 If STMT produces a varying value, return SSA_PROP_VARYING. */
7953 static enum ssa_prop_result
7954 vrp_visit_stmt (gimple *stmt, edge *taken_edge_p, tree *output_p)
7956 tree def;
7957 ssa_op_iter iter;
7959 if (dump_file && (dump_flags & TDF_DETAILS))
7961 fprintf (dump_file, "\nVisiting statement:\n");
7962 print_gimple_stmt (dump_file, stmt, 0, dump_flags);
7965 if (!stmt_interesting_for_vrp (stmt))
7966 gcc_assert (stmt_ends_bb_p (stmt));
7967 else if (is_gimple_assign (stmt) || is_gimple_call (stmt))
7968 return vrp_visit_assignment_or_call (stmt, output_p);
7969 else if (gimple_code (stmt) == GIMPLE_COND)
7970 return vrp_visit_cond_stmt (as_a <gcond *> (stmt), taken_edge_p);
7971 else if (gimple_code (stmt) == GIMPLE_SWITCH)
7972 return vrp_visit_switch_stmt (as_a <gswitch *> (stmt), taken_edge_p);
7974 /* All other statements produce nothing of interest for VRP, so mark
7975 their outputs varying and prevent further simulation. */
7976 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_DEF)
7977 set_value_range_to_varying (get_value_range (def));
7979 return SSA_PROP_VARYING;
7982 /* Union the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
7983 { VR1TYPE, VR0MIN, VR0MAX } and store the result
7984 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
7985 possible such range. The resulting range is not canonicalized. */
7987 static void
7988 union_ranges (enum value_range_type *vr0type,
7989 tree *vr0min, tree *vr0max,
7990 enum value_range_type vr1type,
7991 tree vr1min, tree vr1max)
7993 bool mineq = operand_equal_p (*vr0min, vr1min, 0);
7994 bool maxeq = operand_equal_p (*vr0max, vr1max, 0);
7996 /* [] is vr0, () is vr1 in the following classification comments. */
7997 if (mineq && maxeq)
7999 /* [( )] */
8000 if (*vr0type == vr1type)
8001 /* Nothing to do for equal ranges. */
8003 else if ((*vr0type == VR_RANGE
8004 && vr1type == VR_ANTI_RANGE)
8005 || (*vr0type == VR_ANTI_RANGE
8006 && vr1type == VR_RANGE))
8008 /* For anti-range with range union the result is varying. */
8009 goto give_up;
8011 else
8012 gcc_unreachable ();
8014 else if (operand_less_p (*vr0max, vr1min) == 1
8015 || operand_less_p (vr1max, *vr0min) == 1)
8017 /* [ ] ( ) or ( ) [ ]
8018 If the ranges have an empty intersection, result of the union
8019 operation is the anti-range or if both are anti-ranges
8020 it covers all. */
8021 if (*vr0type == VR_ANTI_RANGE
8022 && vr1type == VR_ANTI_RANGE)
8023 goto give_up;
8024 else if (*vr0type == VR_ANTI_RANGE
8025 && vr1type == VR_RANGE)
8027 else if (*vr0type == VR_RANGE
8028 && vr1type == VR_ANTI_RANGE)
8030 *vr0type = vr1type;
8031 *vr0min = vr1min;
8032 *vr0max = vr1max;
8034 else if (*vr0type == VR_RANGE
8035 && vr1type == VR_RANGE)
8037 /* The result is the convex hull of both ranges. */
8038 if (operand_less_p (*vr0max, vr1min) == 1)
8040 /* If the result can be an anti-range, create one. */
8041 if (TREE_CODE (*vr0max) == INTEGER_CST
8042 && TREE_CODE (vr1min) == INTEGER_CST
8043 && vrp_val_is_min (*vr0min)
8044 && vrp_val_is_max (vr1max))
8046 tree min = int_const_binop (PLUS_EXPR,
8047 *vr0max,
8048 build_int_cst (TREE_TYPE (*vr0max), 1));
8049 tree max = int_const_binop (MINUS_EXPR,
8050 vr1min,
8051 build_int_cst (TREE_TYPE (vr1min), 1));
8052 if (!operand_less_p (max, min))
8054 *vr0type = VR_ANTI_RANGE;
8055 *vr0min = min;
8056 *vr0max = max;
8058 else
8059 *vr0max = vr1max;
8061 else
8062 *vr0max = vr1max;
8064 else
8066 /* If the result can be an anti-range, create one. */
8067 if (TREE_CODE (vr1max) == INTEGER_CST
8068 && TREE_CODE (*vr0min) == INTEGER_CST
8069 && vrp_val_is_min (vr1min)
8070 && vrp_val_is_max (*vr0max))
8072 tree min = int_const_binop (PLUS_EXPR,
8073 vr1max,
8074 build_int_cst (TREE_TYPE (vr1max), 1));
8075 tree max = int_const_binop (MINUS_EXPR,
8076 *vr0min,
8077 build_int_cst (TREE_TYPE (*vr0min), 1));
8078 if (!operand_less_p (max, min))
8080 *vr0type = VR_ANTI_RANGE;
8081 *vr0min = min;
8082 *vr0max = max;
8084 else
8085 *vr0min = vr1min;
8087 else
8088 *vr0min = vr1min;
8091 else
8092 gcc_unreachable ();
8094 else if ((maxeq || operand_less_p (vr1max, *vr0max) == 1)
8095 && (mineq || operand_less_p (*vr0min, vr1min) == 1))
8097 /* [ ( ) ] or [( ) ] or [ ( )] */
8098 if (*vr0type == VR_RANGE
8099 && vr1type == VR_RANGE)
8101 else if (*vr0type == VR_ANTI_RANGE
8102 && vr1type == VR_ANTI_RANGE)
8104 *vr0type = vr1type;
8105 *vr0min = vr1min;
8106 *vr0max = vr1max;
8108 else if (*vr0type == VR_ANTI_RANGE
8109 && vr1type == VR_RANGE)
8111 /* Arbitrarily choose the right or left gap. */
8112 if (!mineq && TREE_CODE (vr1min) == INTEGER_CST)
8113 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
8114 build_int_cst (TREE_TYPE (vr1min), 1));
8115 else if (!maxeq && TREE_CODE (vr1max) == INTEGER_CST)
8116 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
8117 build_int_cst (TREE_TYPE (vr1max), 1));
8118 else
8119 goto give_up;
8121 else if (*vr0type == VR_RANGE
8122 && vr1type == VR_ANTI_RANGE)
8123 /* The result covers everything. */
8124 goto give_up;
8125 else
8126 gcc_unreachable ();
8128 else if ((maxeq || operand_less_p (*vr0max, vr1max) == 1)
8129 && (mineq || operand_less_p (vr1min, *vr0min) == 1))
8131 /* ( [ ] ) or ([ ] ) or ( [ ]) */
8132 if (*vr0type == VR_RANGE
8133 && vr1type == VR_RANGE)
8135 *vr0type = vr1type;
8136 *vr0min = vr1min;
8137 *vr0max = vr1max;
8139 else if (*vr0type == VR_ANTI_RANGE
8140 && vr1type == VR_ANTI_RANGE)
8142 else if (*vr0type == VR_RANGE
8143 && vr1type == VR_ANTI_RANGE)
8145 *vr0type = VR_ANTI_RANGE;
8146 if (!mineq && TREE_CODE (*vr0min) == INTEGER_CST)
8148 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
8149 build_int_cst (TREE_TYPE (*vr0min), 1));
8150 *vr0min = vr1min;
8152 else if (!maxeq && TREE_CODE (*vr0max) == INTEGER_CST)
8154 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
8155 build_int_cst (TREE_TYPE (*vr0max), 1));
8156 *vr0max = vr1max;
8158 else
8159 goto give_up;
8161 else if (*vr0type == VR_ANTI_RANGE
8162 && vr1type == VR_RANGE)
8163 /* The result covers everything. */
8164 goto give_up;
8165 else
8166 gcc_unreachable ();
8168 else if ((operand_less_p (vr1min, *vr0max) == 1
8169 || operand_equal_p (vr1min, *vr0max, 0))
8170 && operand_less_p (*vr0min, vr1min) == 1
8171 && operand_less_p (*vr0max, vr1max) == 1)
8173 /* [ ( ] ) or [ ]( ) */
8174 if (*vr0type == VR_RANGE
8175 && vr1type == VR_RANGE)
8176 *vr0max = vr1max;
8177 else if (*vr0type == VR_ANTI_RANGE
8178 && vr1type == VR_ANTI_RANGE)
8179 *vr0min = vr1min;
8180 else if (*vr0type == VR_ANTI_RANGE
8181 && vr1type == VR_RANGE)
8183 if (TREE_CODE (vr1min) == INTEGER_CST)
8184 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
8185 build_int_cst (TREE_TYPE (vr1min), 1));
8186 else
8187 goto give_up;
8189 else if (*vr0type == VR_RANGE
8190 && vr1type == VR_ANTI_RANGE)
8192 if (TREE_CODE (*vr0max) == INTEGER_CST)
8194 *vr0type = vr1type;
8195 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
8196 build_int_cst (TREE_TYPE (*vr0max), 1));
8197 *vr0max = vr1max;
8199 else
8200 goto give_up;
8202 else
8203 gcc_unreachable ();
8205 else if ((operand_less_p (*vr0min, vr1max) == 1
8206 || operand_equal_p (*vr0min, vr1max, 0))
8207 && operand_less_p (vr1min, *vr0min) == 1
8208 && operand_less_p (vr1max, *vr0max) == 1)
8210 /* ( [ ) ] or ( )[ ] */
8211 if (*vr0type == VR_RANGE
8212 && vr1type == VR_RANGE)
8213 *vr0min = vr1min;
8214 else if (*vr0type == VR_ANTI_RANGE
8215 && vr1type == VR_ANTI_RANGE)
8216 *vr0max = vr1max;
8217 else if (*vr0type == VR_ANTI_RANGE
8218 && vr1type == VR_RANGE)
8220 if (TREE_CODE (vr1max) == INTEGER_CST)
8221 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
8222 build_int_cst (TREE_TYPE (vr1max), 1));
8223 else
8224 goto give_up;
8226 else if (*vr0type == VR_RANGE
8227 && vr1type == VR_ANTI_RANGE)
8229 if (TREE_CODE (*vr0min) == INTEGER_CST)
8231 *vr0type = vr1type;
8232 *vr0min = vr1min;
8233 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
8234 build_int_cst (TREE_TYPE (*vr0min), 1));
8236 else
8237 goto give_up;
8239 else
8240 gcc_unreachable ();
8242 else
8243 goto give_up;
8245 return;
8247 give_up:
8248 *vr0type = VR_VARYING;
8249 *vr0min = NULL_TREE;
8250 *vr0max = NULL_TREE;
8253 /* Intersect the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
8254 { VR1TYPE, VR0MIN, VR0MAX } and store the result
8255 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
8256 possible such range. The resulting range is not canonicalized. */
8258 static void
8259 intersect_ranges (enum value_range_type *vr0type,
8260 tree *vr0min, tree *vr0max,
8261 enum value_range_type vr1type,
8262 tree vr1min, tree vr1max)
8264 bool mineq = operand_equal_p (*vr0min, vr1min, 0);
8265 bool maxeq = operand_equal_p (*vr0max, vr1max, 0);
8267 /* [] is vr0, () is vr1 in the following classification comments. */
8268 if (mineq && maxeq)
8270 /* [( )] */
8271 if (*vr0type == vr1type)
8272 /* Nothing to do for equal ranges. */
8274 else if ((*vr0type == VR_RANGE
8275 && vr1type == VR_ANTI_RANGE)
8276 || (*vr0type == VR_ANTI_RANGE
8277 && vr1type == VR_RANGE))
8279 /* For anti-range with range intersection the result is empty. */
8280 *vr0type = VR_UNDEFINED;
8281 *vr0min = NULL_TREE;
8282 *vr0max = NULL_TREE;
8284 else
8285 gcc_unreachable ();
8287 else if (operand_less_p (*vr0max, vr1min) == 1
8288 || operand_less_p (vr1max, *vr0min) == 1)
8290 /* [ ] ( ) or ( ) [ ]
8291 If the ranges have an empty intersection, the result of the
8292 intersect operation is the range for intersecting an
8293 anti-range with a range or empty when intersecting two ranges. */
8294 if (*vr0type == VR_RANGE
8295 && vr1type == VR_ANTI_RANGE)
8297 else if (*vr0type == VR_ANTI_RANGE
8298 && vr1type == VR_RANGE)
8300 *vr0type = vr1type;
8301 *vr0min = vr1min;
8302 *vr0max = vr1max;
8304 else if (*vr0type == VR_RANGE
8305 && vr1type == VR_RANGE)
8307 *vr0type = VR_UNDEFINED;
8308 *vr0min = NULL_TREE;
8309 *vr0max = NULL_TREE;
8311 else if (*vr0type == VR_ANTI_RANGE
8312 && vr1type == VR_ANTI_RANGE)
8314 /* If the anti-ranges are adjacent to each other merge them. */
8315 if (TREE_CODE (*vr0max) == INTEGER_CST
8316 && TREE_CODE (vr1min) == INTEGER_CST
8317 && operand_less_p (*vr0max, vr1min) == 1
8318 && integer_onep (int_const_binop (MINUS_EXPR,
8319 vr1min, *vr0max)))
8320 *vr0max = vr1max;
8321 else if (TREE_CODE (vr1max) == INTEGER_CST
8322 && TREE_CODE (*vr0min) == INTEGER_CST
8323 && operand_less_p (vr1max, *vr0min) == 1
8324 && integer_onep (int_const_binop (MINUS_EXPR,
8325 *vr0min, vr1max)))
8326 *vr0min = vr1min;
8327 /* Else arbitrarily take VR0. */
8330 else if ((maxeq || operand_less_p (vr1max, *vr0max) == 1)
8331 && (mineq || operand_less_p (*vr0min, vr1min) == 1))
8333 /* [ ( ) ] or [( ) ] or [ ( )] */
8334 if (*vr0type == VR_RANGE
8335 && vr1type == VR_RANGE)
8337 /* If both are ranges the result is the inner one. */
8338 *vr0type = vr1type;
8339 *vr0min = vr1min;
8340 *vr0max = vr1max;
8342 else if (*vr0type == VR_RANGE
8343 && vr1type == VR_ANTI_RANGE)
8345 /* Choose the right gap if the left one is empty. */
8346 if (mineq)
8348 if (TREE_CODE (vr1max) == INTEGER_CST)
8349 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
8350 build_int_cst (TREE_TYPE (vr1max), 1));
8351 else
8352 *vr0min = vr1max;
8354 /* Choose the left gap if the right one is empty. */
8355 else if (maxeq)
8357 if (TREE_CODE (vr1min) == INTEGER_CST)
8358 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
8359 build_int_cst (TREE_TYPE (vr1min), 1));
8360 else
8361 *vr0max = vr1min;
8363 /* Choose the anti-range if the range is effectively varying. */
8364 else if (vrp_val_is_min (*vr0min)
8365 && vrp_val_is_max (*vr0max))
8367 *vr0type = vr1type;
8368 *vr0min = vr1min;
8369 *vr0max = vr1max;
8371 /* Else choose the range. */
8373 else if (*vr0type == VR_ANTI_RANGE
8374 && vr1type == VR_ANTI_RANGE)
8375 /* If both are anti-ranges the result is the outer one. */
8377 else if (*vr0type == VR_ANTI_RANGE
8378 && vr1type == VR_RANGE)
8380 /* The intersection is empty. */
8381 *vr0type = VR_UNDEFINED;
8382 *vr0min = NULL_TREE;
8383 *vr0max = NULL_TREE;
8385 else
8386 gcc_unreachable ();
8388 else if ((maxeq || operand_less_p (*vr0max, vr1max) == 1)
8389 && (mineq || operand_less_p (vr1min, *vr0min) == 1))
8391 /* ( [ ] ) or ([ ] ) or ( [ ]) */
8392 if (*vr0type == VR_RANGE
8393 && vr1type == VR_RANGE)
8394 /* Choose the inner range. */
8396 else if (*vr0type == VR_ANTI_RANGE
8397 && vr1type == VR_RANGE)
8399 /* Choose the right gap if the left is empty. */
8400 if (mineq)
8402 *vr0type = VR_RANGE;
8403 if (TREE_CODE (*vr0max) == INTEGER_CST)
8404 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
8405 build_int_cst (TREE_TYPE (*vr0max), 1));
8406 else
8407 *vr0min = *vr0max;
8408 *vr0max = vr1max;
8410 /* Choose the left gap if the right is empty. */
8411 else if (maxeq)
8413 *vr0type = VR_RANGE;
8414 if (TREE_CODE (*vr0min) == INTEGER_CST)
8415 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
8416 build_int_cst (TREE_TYPE (*vr0min), 1));
8417 else
8418 *vr0max = *vr0min;
8419 *vr0min = vr1min;
8421 /* Choose the anti-range if the range is effectively varying. */
8422 else if (vrp_val_is_min (vr1min)
8423 && vrp_val_is_max (vr1max))
8425 /* Else choose the range. */
8426 else
8428 *vr0type = vr1type;
8429 *vr0min = vr1min;
8430 *vr0max = vr1max;
8433 else if (*vr0type == VR_ANTI_RANGE
8434 && vr1type == VR_ANTI_RANGE)
8436 /* If both are anti-ranges the result is the outer one. */
8437 *vr0type = vr1type;
8438 *vr0min = vr1min;
8439 *vr0max = vr1max;
8441 else if (vr1type == VR_ANTI_RANGE
8442 && *vr0type == VR_RANGE)
8444 /* The intersection is empty. */
8445 *vr0type = VR_UNDEFINED;
8446 *vr0min = NULL_TREE;
8447 *vr0max = NULL_TREE;
8449 else
8450 gcc_unreachable ();
8452 else if ((operand_less_p (vr1min, *vr0max) == 1
8453 || operand_equal_p (vr1min, *vr0max, 0))
8454 && operand_less_p (*vr0min, vr1min) == 1)
8456 /* [ ( ] ) or [ ]( ) */
8457 if (*vr0type == VR_ANTI_RANGE
8458 && vr1type == VR_ANTI_RANGE)
8459 *vr0max = vr1max;
8460 else if (*vr0type == VR_RANGE
8461 && vr1type == VR_RANGE)
8462 *vr0min = vr1min;
8463 else if (*vr0type == VR_RANGE
8464 && vr1type == VR_ANTI_RANGE)
8466 if (TREE_CODE (vr1min) == INTEGER_CST)
8467 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
8468 build_int_cst (TREE_TYPE (vr1min), 1));
8469 else
8470 *vr0max = vr1min;
8472 else if (*vr0type == VR_ANTI_RANGE
8473 && vr1type == VR_RANGE)
8475 *vr0type = VR_RANGE;
8476 if (TREE_CODE (*vr0max) == INTEGER_CST)
8477 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
8478 build_int_cst (TREE_TYPE (*vr0max), 1));
8479 else
8480 *vr0min = *vr0max;
8481 *vr0max = vr1max;
8483 else
8484 gcc_unreachable ();
8486 else if ((operand_less_p (*vr0min, vr1max) == 1
8487 || operand_equal_p (*vr0min, vr1max, 0))
8488 && operand_less_p (vr1min, *vr0min) == 1)
8490 /* ( [ ) ] or ( )[ ] */
8491 if (*vr0type == VR_ANTI_RANGE
8492 && vr1type == VR_ANTI_RANGE)
8493 *vr0min = vr1min;
8494 else if (*vr0type == VR_RANGE
8495 && vr1type == VR_RANGE)
8496 *vr0max = vr1max;
8497 else if (*vr0type == VR_RANGE
8498 && vr1type == VR_ANTI_RANGE)
8500 if (TREE_CODE (vr1max) == INTEGER_CST)
8501 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
8502 build_int_cst (TREE_TYPE (vr1max), 1));
8503 else
8504 *vr0min = vr1max;
8506 else if (*vr0type == VR_ANTI_RANGE
8507 && vr1type == VR_RANGE)
8509 *vr0type = VR_RANGE;
8510 if (TREE_CODE (*vr0min) == INTEGER_CST)
8511 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
8512 build_int_cst (TREE_TYPE (*vr0min), 1));
8513 else
8514 *vr0max = *vr0min;
8515 *vr0min = vr1min;
8517 else
8518 gcc_unreachable ();
8521 /* As a fallback simply use { *VRTYPE, *VR0MIN, *VR0MAX } as
8522 result for the intersection. That's always a conservative
8523 correct estimate. */
8525 return;
8529 /* Intersect the two value-ranges *VR0 and *VR1 and store the result
8530 in *VR0. This may not be the smallest possible such range. */
8532 static void
8533 vrp_intersect_ranges_1 (value_range *vr0, value_range *vr1)
8535 value_range saved;
8537 /* If either range is VR_VARYING the other one wins. */
8538 if (vr1->type == VR_VARYING)
8539 return;
8540 if (vr0->type == VR_VARYING)
8542 copy_value_range (vr0, vr1);
8543 return;
8546 /* When either range is VR_UNDEFINED the resulting range is
8547 VR_UNDEFINED, too. */
8548 if (vr0->type == VR_UNDEFINED)
8549 return;
8550 if (vr1->type == VR_UNDEFINED)
8552 set_value_range_to_undefined (vr0);
8553 return;
8556 /* Save the original vr0 so we can return it as conservative intersection
8557 result when our worker turns things to varying. */
8558 saved = *vr0;
8559 intersect_ranges (&vr0->type, &vr0->min, &vr0->max,
8560 vr1->type, vr1->min, vr1->max);
8561 /* Make sure to canonicalize the result though as the inversion of a
8562 VR_RANGE can still be a VR_RANGE. */
8563 set_and_canonicalize_value_range (vr0, vr0->type,
8564 vr0->min, vr0->max, vr0->equiv);
8565 /* If that failed, use the saved original VR0. */
8566 if (vr0->type == VR_VARYING)
8568 *vr0 = saved;
8569 return;
8571 /* If the result is VR_UNDEFINED there is no need to mess with
8572 the equivalencies. */
8573 if (vr0->type == VR_UNDEFINED)
8574 return;
8576 /* The resulting set of equivalences for range intersection is the union of
8577 the two sets. */
8578 if (vr0->equiv && vr1->equiv && vr0->equiv != vr1->equiv)
8579 bitmap_ior_into (vr0->equiv, vr1->equiv);
8580 else if (vr1->equiv && !vr0->equiv)
8581 bitmap_copy (vr0->equiv, vr1->equiv);
8584 static void
8585 vrp_intersect_ranges (value_range *vr0, value_range *vr1)
8587 if (dump_file && (dump_flags & TDF_DETAILS))
8589 fprintf (dump_file, "Intersecting\n ");
8590 dump_value_range (dump_file, vr0);
8591 fprintf (dump_file, "\nand\n ");
8592 dump_value_range (dump_file, vr1);
8593 fprintf (dump_file, "\n");
8595 vrp_intersect_ranges_1 (vr0, vr1);
8596 if (dump_file && (dump_flags & TDF_DETAILS))
8598 fprintf (dump_file, "to\n ");
8599 dump_value_range (dump_file, vr0);
8600 fprintf (dump_file, "\n");
8604 /* Meet operation for value ranges. Given two value ranges VR0 and
8605 VR1, store in VR0 a range that contains both VR0 and VR1. This
8606 may not be the smallest possible such range. */
8608 static void
8609 vrp_meet_1 (value_range *vr0, value_range *vr1)
8611 value_range saved;
8613 if (vr0->type == VR_UNDEFINED)
8615 set_value_range (vr0, vr1->type, vr1->min, vr1->max, vr1->equiv);
8616 return;
8619 if (vr1->type == VR_UNDEFINED)
8621 /* VR0 already has the resulting range. */
8622 return;
8625 if (vr0->type == VR_VARYING)
8627 /* Nothing to do. VR0 already has the resulting range. */
8628 return;
8631 if (vr1->type == VR_VARYING)
8633 set_value_range_to_varying (vr0);
8634 return;
8637 saved = *vr0;
8638 union_ranges (&vr0->type, &vr0->min, &vr0->max,
8639 vr1->type, vr1->min, vr1->max);
8640 if (vr0->type == VR_VARYING)
8642 /* Failed to find an efficient meet. Before giving up and setting
8643 the result to VARYING, see if we can at least derive a useful
8644 anti-range. FIXME, all this nonsense about distinguishing
8645 anti-ranges from ranges is necessary because of the odd
8646 semantics of range_includes_zero_p and friends. */
8647 if (((saved.type == VR_RANGE
8648 && range_includes_zero_p (saved.min, saved.max) == 0)
8649 || (saved.type == VR_ANTI_RANGE
8650 && range_includes_zero_p (saved.min, saved.max) == 1))
8651 && ((vr1->type == VR_RANGE
8652 && range_includes_zero_p (vr1->min, vr1->max) == 0)
8653 || (vr1->type == VR_ANTI_RANGE
8654 && range_includes_zero_p (vr1->min, vr1->max) == 1)))
8656 set_value_range_to_nonnull (vr0, TREE_TYPE (saved.min));
8658 /* Since this meet operation did not result from the meeting of
8659 two equivalent names, VR0 cannot have any equivalences. */
8660 if (vr0->equiv)
8661 bitmap_clear (vr0->equiv);
8662 return;
8665 set_value_range_to_varying (vr0);
8666 return;
8668 set_and_canonicalize_value_range (vr0, vr0->type, vr0->min, vr0->max,
8669 vr0->equiv);
8670 if (vr0->type == VR_VARYING)
8671 return;
8673 /* The resulting set of equivalences is always the intersection of
8674 the two sets. */
8675 if (vr0->equiv && vr1->equiv && vr0->equiv != vr1->equiv)
8676 bitmap_and_into (vr0->equiv, vr1->equiv);
8677 else if (vr0->equiv && !vr1->equiv)
8678 bitmap_clear (vr0->equiv);
8681 static void
8682 vrp_meet (value_range *vr0, value_range *vr1)
8684 if (dump_file && (dump_flags & TDF_DETAILS))
8686 fprintf (dump_file, "Meeting\n ");
8687 dump_value_range (dump_file, vr0);
8688 fprintf (dump_file, "\nand\n ");
8689 dump_value_range (dump_file, vr1);
8690 fprintf (dump_file, "\n");
8692 vrp_meet_1 (vr0, vr1);
8693 if (dump_file && (dump_flags & TDF_DETAILS))
8695 fprintf (dump_file, "to\n ");
8696 dump_value_range (dump_file, vr0);
8697 fprintf (dump_file, "\n");
8702 /* Visit all arguments for PHI node PHI that flow through executable
8703 edges. If a valid value range can be derived from all the incoming
8704 value ranges, set a new range for the LHS of PHI. */
8706 static enum ssa_prop_result
8707 vrp_visit_phi_node (gphi *phi)
8709 size_t i;
8710 tree lhs = PHI_RESULT (phi);
8711 value_range *lhs_vr = get_value_range (lhs);
8712 value_range vr_result = VR_INITIALIZER;
8713 bool first = true;
8714 int edges, old_edges;
8715 struct loop *l;
8717 if (dump_file && (dump_flags & TDF_DETAILS))
8719 fprintf (dump_file, "\nVisiting PHI node: ");
8720 print_gimple_stmt (dump_file, phi, 0, dump_flags);
8723 edges = 0;
8724 for (i = 0; i < gimple_phi_num_args (phi); i++)
8726 edge e = gimple_phi_arg_edge (phi, i);
8728 if (dump_file && (dump_flags & TDF_DETAILS))
8730 fprintf (dump_file,
8731 " Argument #%d (%d -> %d %sexecutable)\n",
8732 (int) i, e->src->index, e->dest->index,
8733 (e->flags & EDGE_EXECUTABLE) ? "" : "not ");
8736 if (e->flags & EDGE_EXECUTABLE)
8738 tree arg = PHI_ARG_DEF (phi, i);
8739 value_range vr_arg;
8741 ++edges;
8743 if (TREE_CODE (arg) == SSA_NAME)
8745 vr_arg = *(get_value_range (arg));
8746 /* Do not allow equivalences or symbolic ranges to leak in from
8747 backedges. That creates invalid equivalencies.
8748 See PR53465 and PR54767. */
8749 if (e->flags & EDGE_DFS_BACK)
8751 if (vr_arg.type == VR_RANGE
8752 || vr_arg.type == VR_ANTI_RANGE)
8754 vr_arg.equiv = NULL;
8755 if (symbolic_range_p (&vr_arg))
8757 vr_arg.type = VR_VARYING;
8758 vr_arg.min = NULL_TREE;
8759 vr_arg.max = NULL_TREE;
8763 else
8765 /* If the non-backedge arguments range is VR_VARYING then
8766 we can still try recording a simple equivalence. */
8767 if (vr_arg.type == VR_VARYING)
8769 vr_arg.type = VR_RANGE;
8770 vr_arg.min = arg;
8771 vr_arg.max = arg;
8772 vr_arg.equiv = NULL;
8776 else
8778 if (TREE_OVERFLOW_P (arg))
8779 arg = drop_tree_overflow (arg);
8781 vr_arg.type = VR_RANGE;
8782 vr_arg.min = arg;
8783 vr_arg.max = arg;
8784 vr_arg.equiv = NULL;
8787 if (dump_file && (dump_flags & TDF_DETAILS))
8789 fprintf (dump_file, "\t");
8790 print_generic_expr (dump_file, arg, dump_flags);
8791 fprintf (dump_file, ": ");
8792 dump_value_range (dump_file, &vr_arg);
8793 fprintf (dump_file, "\n");
8796 if (first)
8797 copy_value_range (&vr_result, &vr_arg);
8798 else
8799 vrp_meet (&vr_result, &vr_arg);
8800 first = false;
8802 if (vr_result.type == VR_VARYING)
8803 break;
8807 if (vr_result.type == VR_VARYING)
8808 goto varying;
8809 else if (vr_result.type == VR_UNDEFINED)
8810 goto update_range;
8812 old_edges = vr_phi_edge_counts[SSA_NAME_VERSION (lhs)];
8813 vr_phi_edge_counts[SSA_NAME_VERSION (lhs)] = edges;
8815 /* To prevent infinite iterations in the algorithm, derive ranges
8816 when the new value is slightly bigger or smaller than the
8817 previous one. We don't do this if we have seen a new executable
8818 edge; this helps us avoid an overflow infinity for conditionals
8819 which are not in a loop. If the old value-range was VR_UNDEFINED
8820 use the updated range and iterate one more time. */
8821 if (edges > 0
8822 && gimple_phi_num_args (phi) > 1
8823 && edges == old_edges
8824 && lhs_vr->type != VR_UNDEFINED)
8826 /* Compare old and new ranges, fall back to varying if the
8827 values are not comparable. */
8828 int cmp_min = compare_values (lhs_vr->min, vr_result.min);
8829 if (cmp_min == -2)
8830 goto varying;
8831 int cmp_max = compare_values (lhs_vr->max, vr_result.max);
8832 if (cmp_max == -2)
8833 goto varying;
8835 /* For non VR_RANGE or for pointers fall back to varying if
8836 the range changed. */
8837 if ((lhs_vr->type != VR_RANGE || vr_result.type != VR_RANGE
8838 || POINTER_TYPE_P (TREE_TYPE (lhs)))
8839 && (cmp_min != 0 || cmp_max != 0))
8840 goto varying;
8842 /* If the new minimum is larger than the previous one
8843 retain the old value. If the new minimum value is smaller
8844 than the previous one and not -INF go all the way to -INF + 1.
8845 In the first case, to avoid infinite bouncing between different
8846 minimums, and in the other case to avoid iterating millions of
8847 times to reach -INF. Going to -INF + 1 also lets the following
8848 iteration compute whether there will be any overflow, at the
8849 expense of one additional iteration. */
8850 if (cmp_min < 0)
8851 vr_result.min = lhs_vr->min;
8852 else if (cmp_min > 0
8853 && !vrp_val_is_min (vr_result.min))
8854 vr_result.min
8855 = int_const_binop (PLUS_EXPR,
8856 vrp_val_min (TREE_TYPE (vr_result.min)),
8857 build_int_cst (TREE_TYPE (vr_result.min), 1));
8859 /* Similarly for the maximum value. */
8860 if (cmp_max > 0)
8861 vr_result.max = lhs_vr->max;
8862 else if (cmp_max < 0
8863 && !vrp_val_is_max (vr_result.max))
8864 vr_result.max
8865 = int_const_binop (MINUS_EXPR,
8866 vrp_val_max (TREE_TYPE (vr_result.min)),
8867 build_int_cst (TREE_TYPE (vr_result.min), 1));
8869 /* If we dropped either bound to +-INF then if this is a loop
8870 PHI node SCEV may known more about its value-range. */
8871 if ((cmp_min > 0 || cmp_min < 0
8872 || cmp_max < 0 || cmp_max > 0)
8873 && (l = loop_containing_stmt (phi))
8874 && l->header == gimple_bb (phi))
8875 adjust_range_with_scev (&vr_result, l, phi, lhs);
8877 /* If we will end up with a (-INF, +INF) range, set it to
8878 VARYING. Same if the previous max value was invalid for
8879 the type and we end up with vr_result.min > vr_result.max. */
8880 if ((vrp_val_is_max (vr_result.max)
8881 && vrp_val_is_min (vr_result.min))
8882 || compare_values (vr_result.min,
8883 vr_result.max) > 0)
8884 goto varying;
8887 /* If the new range is different than the previous value, keep
8888 iterating. */
8889 update_range:
8890 if (update_value_range (lhs, &vr_result))
8892 if (dump_file && (dump_flags & TDF_DETAILS))
8894 fprintf (dump_file, "Found new range for ");
8895 print_generic_expr (dump_file, lhs, 0);
8896 fprintf (dump_file, ": ");
8897 dump_value_range (dump_file, &vr_result);
8898 fprintf (dump_file, "\n");
8901 if (vr_result.type == VR_VARYING)
8902 return SSA_PROP_VARYING;
8904 return SSA_PROP_INTERESTING;
8907 /* Nothing changed, don't add outgoing edges. */
8908 return SSA_PROP_NOT_INTERESTING;
8910 /* No match found. Set the LHS to VARYING. */
8911 varying:
8912 set_value_range_to_varying (lhs_vr);
8913 return SSA_PROP_VARYING;
8916 /* Simplify boolean operations if the source is known
8917 to be already a boolean. */
8918 static bool
8919 simplify_truth_ops_using_ranges (gimple_stmt_iterator *gsi, gimple *stmt)
8921 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
8922 tree lhs, op0, op1;
8923 bool need_conversion;
8925 /* We handle only !=/== case here. */
8926 gcc_assert (rhs_code == EQ_EXPR || rhs_code == NE_EXPR);
8928 op0 = gimple_assign_rhs1 (stmt);
8929 if (!op_with_boolean_value_range_p (op0))
8930 return false;
8932 op1 = gimple_assign_rhs2 (stmt);
8933 if (!op_with_boolean_value_range_p (op1))
8934 return false;
8936 /* Reduce number of cases to handle to NE_EXPR. As there is no
8937 BIT_XNOR_EXPR we cannot replace A == B with a single statement. */
8938 if (rhs_code == EQ_EXPR)
8940 if (TREE_CODE (op1) == INTEGER_CST)
8941 op1 = int_const_binop (BIT_XOR_EXPR, op1,
8942 build_int_cst (TREE_TYPE (op1), 1));
8943 else
8944 return false;
8947 lhs = gimple_assign_lhs (stmt);
8948 need_conversion
8949 = !useless_type_conversion_p (TREE_TYPE (lhs), TREE_TYPE (op0));
8951 /* Make sure to not sign-extend a 1-bit 1 when converting the result. */
8952 if (need_conversion
8953 && !TYPE_UNSIGNED (TREE_TYPE (op0))
8954 && TYPE_PRECISION (TREE_TYPE (op0)) == 1
8955 && TYPE_PRECISION (TREE_TYPE (lhs)) > 1)
8956 return false;
8958 /* For A != 0 we can substitute A itself. */
8959 if (integer_zerop (op1))
8960 gimple_assign_set_rhs_with_ops (gsi,
8961 need_conversion
8962 ? NOP_EXPR : TREE_CODE (op0), op0);
8963 /* For A != B we substitute A ^ B. Either with conversion. */
8964 else if (need_conversion)
8966 tree tem = make_ssa_name (TREE_TYPE (op0));
8967 gassign *newop
8968 = gimple_build_assign (tem, BIT_XOR_EXPR, op0, op1);
8969 gsi_insert_before (gsi, newop, GSI_SAME_STMT);
8970 gimple_assign_set_rhs_with_ops (gsi, NOP_EXPR, tem);
8972 /* Or without. */
8973 else
8974 gimple_assign_set_rhs_with_ops (gsi, BIT_XOR_EXPR, op0, op1);
8975 update_stmt (gsi_stmt (*gsi));
8977 return true;
8980 /* Simplify a division or modulo operator to a right shift or
8981 bitwise and if the first operand is unsigned or is greater
8982 than zero and the second operand is an exact power of two.
8983 For TRUNC_MOD_EXPR op0 % op1 with constant op1, optimize it
8984 into just op0 if op0's range is known to be a subset of
8985 [-op1 + 1, op1 - 1] for signed and [0, op1 - 1] for unsigned
8986 modulo. */
8988 static bool
8989 simplify_div_or_mod_using_ranges (gimple *stmt)
8991 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
8992 tree val = NULL;
8993 tree op0 = gimple_assign_rhs1 (stmt);
8994 tree op1 = gimple_assign_rhs2 (stmt);
8995 value_range *vr = get_value_range (op0);
8997 if (rhs_code == TRUNC_MOD_EXPR
8998 && TREE_CODE (op1) == INTEGER_CST
8999 && tree_int_cst_sgn (op1) == 1
9000 && range_int_cst_p (vr)
9001 && tree_int_cst_lt (vr->max, op1))
9003 if (TYPE_UNSIGNED (TREE_TYPE (op0))
9004 || tree_int_cst_sgn (vr->min) >= 0
9005 || tree_int_cst_lt (fold_unary (NEGATE_EXPR, TREE_TYPE (op1), op1),
9006 vr->min))
9008 /* If op0 already has the range op0 % op1 has,
9009 then TRUNC_MOD_EXPR won't change anything. */
9010 gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
9011 gimple_assign_set_rhs_from_tree (&gsi, op0);
9012 update_stmt (stmt);
9013 return true;
9017 if (!integer_pow2p (op1))
9018 return false;
9020 if (TYPE_UNSIGNED (TREE_TYPE (op0)))
9022 val = integer_one_node;
9024 else
9026 bool sop = false;
9028 val = compare_range_with_value (GE_EXPR, vr, integer_zero_node, &sop);
9030 if (val
9031 && sop
9032 && integer_onep (val)
9033 && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC))
9035 location_t location;
9037 if (!gimple_has_location (stmt))
9038 location = input_location;
9039 else
9040 location = gimple_location (stmt);
9041 warning_at (location, OPT_Wstrict_overflow,
9042 "assuming signed overflow does not occur when "
9043 "simplifying %</%> or %<%%%> to %<>>%> or %<&%>");
9047 if (val && integer_onep (val))
9049 tree t;
9051 if (rhs_code == TRUNC_DIV_EXPR)
9053 t = build_int_cst (integer_type_node, tree_log2 (op1));
9054 gimple_assign_set_rhs_code (stmt, RSHIFT_EXPR);
9055 gimple_assign_set_rhs1 (stmt, op0);
9056 gimple_assign_set_rhs2 (stmt, t);
9058 else
9060 t = build_int_cst (TREE_TYPE (op1), 1);
9061 t = int_const_binop (MINUS_EXPR, op1, t);
9062 t = fold_convert (TREE_TYPE (op0), t);
9064 gimple_assign_set_rhs_code (stmt, BIT_AND_EXPR);
9065 gimple_assign_set_rhs1 (stmt, op0);
9066 gimple_assign_set_rhs2 (stmt, t);
9069 update_stmt (stmt);
9070 return true;
9073 return false;
9076 /* Simplify a min or max if the ranges of the two operands are
9077 disjoint. Return true if we do simplify. */
9079 static bool
9080 simplify_min_or_max_using_ranges (gimple *stmt)
9082 tree op0 = gimple_assign_rhs1 (stmt);
9083 tree op1 = gimple_assign_rhs2 (stmt);
9084 bool sop = false;
9085 tree val;
9087 val = (vrp_evaluate_conditional_warnv_with_ops_using_ranges
9088 (LE_EXPR, op0, op1, &sop));
9089 if (!val)
9091 sop = false;
9092 val = (vrp_evaluate_conditional_warnv_with_ops_using_ranges
9093 (LT_EXPR, op0, op1, &sop));
9096 if (val)
9098 if (sop && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC))
9100 location_t location;
9102 if (!gimple_has_location (stmt))
9103 location = input_location;
9104 else
9105 location = gimple_location (stmt);
9106 warning_at (location, OPT_Wstrict_overflow,
9107 "assuming signed overflow does not occur when "
9108 "simplifying %<min/max (X,Y)%> to %<X%> or %<Y%>");
9111 /* VAL == TRUE -> OP0 < or <= op1
9112 VAL == FALSE -> OP0 > or >= op1. */
9113 tree res = ((gimple_assign_rhs_code (stmt) == MAX_EXPR)
9114 == integer_zerop (val)) ? op0 : op1;
9115 gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
9116 gimple_assign_set_rhs_from_tree (&gsi, res);
9117 update_stmt (stmt);
9118 return true;
9121 return false;
9124 /* If the operand to an ABS_EXPR is >= 0, then eliminate the
9125 ABS_EXPR. If the operand is <= 0, then simplify the
9126 ABS_EXPR into a NEGATE_EXPR. */
9128 static bool
9129 simplify_abs_using_ranges (gimple *stmt)
9131 tree op = gimple_assign_rhs1 (stmt);
9132 value_range *vr = get_value_range (op);
9134 if (vr)
9136 tree val = NULL;
9137 bool sop = false;
9139 val = compare_range_with_value (LE_EXPR, vr, integer_zero_node, &sop);
9140 if (!val)
9142 /* The range is neither <= 0 nor > 0. Now see if it is
9143 either < 0 or >= 0. */
9144 sop = false;
9145 val = compare_range_with_value (LT_EXPR, vr, integer_zero_node,
9146 &sop);
9149 if (val)
9151 if (sop && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC))
9153 location_t location;
9155 if (!gimple_has_location (stmt))
9156 location = input_location;
9157 else
9158 location = gimple_location (stmt);
9159 warning_at (location, OPT_Wstrict_overflow,
9160 "assuming signed overflow does not occur when "
9161 "simplifying %<abs (X)%> to %<X%> or %<-X%>");
9164 gimple_assign_set_rhs1 (stmt, op);
9165 if (integer_zerop (val))
9166 gimple_assign_set_rhs_code (stmt, SSA_NAME);
9167 else
9168 gimple_assign_set_rhs_code (stmt, NEGATE_EXPR);
9169 update_stmt (stmt);
9170 return true;
9174 return false;
9177 /* Optimize away redundant BIT_AND_EXPR and BIT_IOR_EXPR.
9178 If all the bits that are being cleared by & are already
9179 known to be zero from VR, or all the bits that are being
9180 set by | are already known to be one from VR, the bit
9181 operation is redundant. */
9183 static bool
9184 simplify_bit_ops_using_ranges (gimple_stmt_iterator *gsi, gimple *stmt)
9186 tree op0 = gimple_assign_rhs1 (stmt);
9187 tree op1 = gimple_assign_rhs2 (stmt);
9188 tree op = NULL_TREE;
9189 value_range vr0 = VR_INITIALIZER;
9190 value_range vr1 = VR_INITIALIZER;
9191 wide_int may_be_nonzero0, may_be_nonzero1;
9192 wide_int must_be_nonzero0, must_be_nonzero1;
9193 wide_int mask;
9195 if (TREE_CODE (op0) == SSA_NAME)
9196 vr0 = *(get_value_range (op0));
9197 else if (is_gimple_min_invariant (op0))
9198 set_value_range_to_value (&vr0, op0, NULL);
9199 else
9200 return false;
9202 if (TREE_CODE (op1) == SSA_NAME)
9203 vr1 = *(get_value_range (op1));
9204 else if (is_gimple_min_invariant (op1))
9205 set_value_range_to_value (&vr1, op1, NULL);
9206 else
9207 return false;
9209 if (!zero_nonzero_bits_from_vr (TREE_TYPE (op0), &vr0, &may_be_nonzero0,
9210 &must_be_nonzero0))
9211 return false;
9212 if (!zero_nonzero_bits_from_vr (TREE_TYPE (op1), &vr1, &may_be_nonzero1,
9213 &must_be_nonzero1))
9214 return false;
9216 switch (gimple_assign_rhs_code (stmt))
9218 case BIT_AND_EXPR:
9219 mask = may_be_nonzero0.and_not (must_be_nonzero1);
9220 if (mask == 0)
9222 op = op0;
9223 break;
9225 mask = may_be_nonzero1.and_not (must_be_nonzero0);
9226 if (mask == 0)
9228 op = op1;
9229 break;
9231 break;
9232 case BIT_IOR_EXPR:
9233 mask = may_be_nonzero0.and_not (must_be_nonzero1);
9234 if (mask == 0)
9236 op = op1;
9237 break;
9239 mask = may_be_nonzero1.and_not (must_be_nonzero0);
9240 if (mask == 0)
9242 op = op0;
9243 break;
9245 break;
9246 default:
9247 gcc_unreachable ();
9250 if (op == NULL_TREE)
9251 return false;
9253 gimple_assign_set_rhs_with_ops (gsi, TREE_CODE (op), op);
9254 update_stmt (gsi_stmt (*gsi));
9255 return true;
9258 /* We are comparing trees OP0 and OP1 using COND_CODE. OP0 has
9259 a known value range VR.
9261 If there is one and only one value which will satisfy the
9262 conditional, then return that value. Else return NULL.
9264 If signed overflow must be undefined for the value to satisfy
9265 the conditional, then set *STRICT_OVERFLOW_P to true. */
9267 static tree
9268 test_for_singularity (enum tree_code cond_code, tree op0,
9269 tree op1, value_range *vr,
9270 bool *strict_overflow_p)
9272 tree min = NULL;
9273 tree max = NULL;
9275 /* Extract minimum/maximum values which satisfy the
9276 the conditional as it was written. */
9277 if (cond_code == LE_EXPR || cond_code == LT_EXPR)
9279 /* This should not be negative infinity; there is no overflow
9280 here. */
9281 min = TYPE_MIN_VALUE (TREE_TYPE (op0));
9283 max = op1;
9284 if (cond_code == LT_EXPR && !is_overflow_infinity (max))
9286 tree one = build_int_cst (TREE_TYPE (op0), 1);
9287 max = fold_build2 (MINUS_EXPR, TREE_TYPE (op0), max, one);
9288 if (EXPR_P (max))
9289 TREE_NO_WARNING (max) = 1;
9292 else if (cond_code == GE_EXPR || cond_code == GT_EXPR)
9294 /* This should not be positive infinity; there is no overflow
9295 here. */
9296 max = TYPE_MAX_VALUE (TREE_TYPE (op0));
9298 min = op1;
9299 if (cond_code == GT_EXPR && !is_overflow_infinity (min))
9301 tree one = build_int_cst (TREE_TYPE (op0), 1);
9302 min = fold_build2 (PLUS_EXPR, TREE_TYPE (op0), min, one);
9303 if (EXPR_P (min))
9304 TREE_NO_WARNING (min) = 1;
9308 /* Now refine the minimum and maximum values using any
9309 value range information we have for op0. */
9310 if (min && max)
9312 if (compare_values (vr->min, min) == 1)
9313 min = vr->min;
9314 if (compare_values (vr->max, max) == -1)
9315 max = vr->max;
9317 /* If the new min/max values have converged to a single value,
9318 then there is only one value which can satisfy the condition,
9319 return that value. */
9320 if (operand_equal_p (min, max, 0) && is_gimple_min_invariant (min))
9322 if ((cond_code == LE_EXPR || cond_code == LT_EXPR)
9323 && is_overflow_infinity (vr->max))
9324 *strict_overflow_p = true;
9325 if ((cond_code == GE_EXPR || cond_code == GT_EXPR)
9326 && is_overflow_infinity (vr->min))
9327 *strict_overflow_p = true;
9329 return min;
9332 return NULL;
9335 /* Return whether the value range *VR fits in an integer type specified
9336 by PRECISION and UNSIGNED_P. */
9338 static bool
9339 range_fits_type_p (value_range *vr, unsigned dest_precision, signop dest_sgn)
9341 tree src_type;
9342 unsigned src_precision;
9343 widest_int tem;
9344 signop src_sgn;
9346 /* We can only handle integral and pointer types. */
9347 src_type = TREE_TYPE (vr->min);
9348 if (!INTEGRAL_TYPE_P (src_type)
9349 && !POINTER_TYPE_P (src_type))
9350 return false;
9352 /* An extension is fine unless VR is SIGNED and dest_sgn is UNSIGNED,
9353 and so is an identity transform. */
9354 src_precision = TYPE_PRECISION (TREE_TYPE (vr->min));
9355 src_sgn = TYPE_SIGN (src_type);
9356 if ((src_precision < dest_precision
9357 && !(dest_sgn == UNSIGNED && src_sgn == SIGNED))
9358 || (src_precision == dest_precision && src_sgn == dest_sgn))
9359 return true;
9361 /* Now we can only handle ranges with constant bounds. */
9362 if (vr->type != VR_RANGE
9363 || TREE_CODE (vr->min) != INTEGER_CST
9364 || TREE_CODE (vr->max) != INTEGER_CST)
9365 return false;
9367 /* For sign changes, the MSB of the wide_int has to be clear.
9368 An unsigned value with its MSB set cannot be represented by
9369 a signed wide_int, while a negative value cannot be represented
9370 by an unsigned wide_int. */
9371 if (src_sgn != dest_sgn
9372 && (wi::lts_p (vr->min, 0) || wi::lts_p (vr->max, 0)))
9373 return false;
9375 /* Then we can perform the conversion on both ends and compare
9376 the result for equality. */
9377 tem = wi::ext (wi::to_widest (vr->min), dest_precision, dest_sgn);
9378 if (tem != wi::to_widest (vr->min))
9379 return false;
9380 tem = wi::ext (wi::to_widest (vr->max), dest_precision, dest_sgn);
9381 if (tem != wi::to_widest (vr->max))
9382 return false;
9384 return true;
9387 /* Simplify a conditional using a relational operator to an equality
9388 test if the range information indicates only one value can satisfy
9389 the original conditional. */
9391 static bool
9392 simplify_cond_using_ranges (gcond *stmt)
9394 tree op0 = gimple_cond_lhs (stmt);
9395 tree op1 = gimple_cond_rhs (stmt);
9396 enum tree_code cond_code = gimple_cond_code (stmt);
9398 if (cond_code != NE_EXPR
9399 && cond_code != EQ_EXPR
9400 && TREE_CODE (op0) == SSA_NAME
9401 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
9402 && is_gimple_min_invariant (op1))
9404 value_range *vr = get_value_range (op0);
9406 /* If we have range information for OP0, then we might be
9407 able to simplify this conditional. */
9408 if (vr->type == VR_RANGE)
9410 enum warn_strict_overflow_code wc = WARN_STRICT_OVERFLOW_COMPARISON;
9411 bool sop = false;
9412 tree new_tree = test_for_singularity (cond_code, op0, op1, vr, &sop);
9414 if (new_tree
9415 && (!sop || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0))))
9417 if (dump_file)
9419 fprintf (dump_file, "Simplified relational ");
9420 print_gimple_stmt (dump_file, stmt, 0, 0);
9421 fprintf (dump_file, " into ");
9424 gimple_cond_set_code (stmt, EQ_EXPR);
9425 gimple_cond_set_lhs (stmt, op0);
9426 gimple_cond_set_rhs (stmt, new_tree);
9428 update_stmt (stmt);
9430 if (dump_file)
9432 print_gimple_stmt (dump_file, stmt, 0, 0);
9433 fprintf (dump_file, "\n");
9436 if (sop && issue_strict_overflow_warning (wc))
9438 location_t location = input_location;
9439 if (gimple_has_location (stmt))
9440 location = gimple_location (stmt);
9442 warning_at (location, OPT_Wstrict_overflow,
9443 "assuming signed overflow does not occur when "
9444 "simplifying conditional");
9447 return true;
9450 /* Try again after inverting the condition. We only deal
9451 with integral types here, so no need to worry about
9452 issues with inverting FP comparisons. */
9453 sop = false;
9454 new_tree = test_for_singularity
9455 (invert_tree_comparison (cond_code, false),
9456 op0, op1, vr, &sop);
9458 if (new_tree
9459 && (!sop || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0))))
9461 if (dump_file)
9463 fprintf (dump_file, "Simplified relational ");
9464 print_gimple_stmt (dump_file, stmt, 0, 0);
9465 fprintf (dump_file, " into ");
9468 gimple_cond_set_code (stmt, NE_EXPR);
9469 gimple_cond_set_lhs (stmt, op0);
9470 gimple_cond_set_rhs (stmt, new_tree);
9472 update_stmt (stmt);
9474 if (dump_file)
9476 print_gimple_stmt (dump_file, stmt, 0, 0);
9477 fprintf (dump_file, "\n");
9480 if (sop && issue_strict_overflow_warning (wc))
9482 location_t location = input_location;
9483 if (gimple_has_location (stmt))
9484 location = gimple_location (stmt);
9486 warning_at (location, OPT_Wstrict_overflow,
9487 "assuming signed overflow does not occur when "
9488 "simplifying conditional");
9491 return true;
9496 /* If we have a comparison of an SSA_NAME (OP0) against a constant,
9497 see if OP0 was set by a type conversion where the source of
9498 the conversion is another SSA_NAME with a range that fits
9499 into the range of OP0's type.
9501 If so, the conversion is redundant as the earlier SSA_NAME can be
9502 used for the comparison directly if we just massage the constant in the
9503 comparison. */
9504 if (TREE_CODE (op0) == SSA_NAME
9505 && TREE_CODE (op1) == INTEGER_CST)
9507 gimple *def_stmt = SSA_NAME_DEF_STMT (op0);
9508 tree innerop;
9510 if (!is_gimple_assign (def_stmt)
9511 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt)))
9512 return false;
9514 innerop = gimple_assign_rhs1 (def_stmt);
9516 if (TREE_CODE (innerop) == SSA_NAME
9517 && !POINTER_TYPE_P (TREE_TYPE (innerop)))
9519 value_range *vr = get_value_range (innerop);
9521 if (range_int_cst_p (vr)
9522 && range_fits_type_p (vr,
9523 TYPE_PRECISION (TREE_TYPE (op0)),
9524 TYPE_SIGN (TREE_TYPE (op0)))
9525 && int_fits_type_p (op1, TREE_TYPE (innerop))
9526 /* The range must not have overflowed, or if it did overflow
9527 we must not be wrapping/trapping overflow and optimizing
9528 with strict overflow semantics. */
9529 && ((!is_negative_overflow_infinity (vr->min)
9530 && !is_positive_overflow_infinity (vr->max))
9531 || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (innerop))))
9533 /* If the range overflowed and the user has asked for warnings
9534 when strict overflow semantics were used to optimize code,
9535 issue an appropriate warning. */
9536 if (cond_code != EQ_EXPR && cond_code != NE_EXPR
9537 && (is_negative_overflow_infinity (vr->min)
9538 || is_positive_overflow_infinity (vr->max))
9539 && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_CONDITIONAL))
9541 location_t location;
9543 if (!gimple_has_location (stmt))
9544 location = input_location;
9545 else
9546 location = gimple_location (stmt);
9547 warning_at (location, OPT_Wstrict_overflow,
9548 "assuming signed overflow does not occur when "
9549 "simplifying conditional");
9552 tree newconst = fold_convert (TREE_TYPE (innerop), op1);
9553 gimple_cond_set_lhs (stmt, innerop);
9554 gimple_cond_set_rhs (stmt, newconst);
9555 return true;
9560 return false;
9563 /* Simplify a switch statement using the value range of the switch
9564 argument. */
9566 static bool
9567 simplify_switch_using_ranges (gswitch *stmt)
9569 tree op = gimple_switch_index (stmt);
9570 value_range *vr;
9571 bool take_default;
9572 edge e;
9573 edge_iterator ei;
9574 size_t i = 0, j = 0, n, n2;
9575 tree vec2;
9576 switch_update su;
9577 size_t k = 1, l = 0;
9579 if (TREE_CODE (op) == SSA_NAME)
9581 vr = get_value_range (op);
9583 /* We can only handle integer ranges. */
9584 if ((vr->type != VR_RANGE
9585 && vr->type != VR_ANTI_RANGE)
9586 || symbolic_range_p (vr))
9587 return false;
9589 /* Find case label for min/max of the value range. */
9590 take_default = !find_case_label_ranges (stmt, vr, &i, &j, &k, &l);
9592 else if (TREE_CODE (op) == INTEGER_CST)
9594 take_default = !find_case_label_index (stmt, 1, op, &i);
9595 if (take_default)
9597 i = 1;
9598 j = 0;
9600 else
9602 j = i;
9605 else
9606 return false;
9608 n = gimple_switch_num_labels (stmt);
9610 /* Bail out if this is just all edges taken. */
9611 if (i == 1
9612 && j == n - 1
9613 && take_default)
9614 return false;
9616 /* Build a new vector of taken case labels. */
9617 vec2 = make_tree_vec (j - i + 1 + l - k + 1 + (int)take_default);
9618 n2 = 0;
9620 /* Add the default edge, if necessary. */
9621 if (take_default)
9622 TREE_VEC_ELT (vec2, n2++) = gimple_switch_default_label (stmt);
9624 for (; i <= j; ++i, ++n2)
9625 TREE_VEC_ELT (vec2, n2) = gimple_switch_label (stmt, i);
9627 for (; k <= l; ++k, ++n2)
9628 TREE_VEC_ELT (vec2, n2) = gimple_switch_label (stmt, k);
9630 /* Mark needed edges. */
9631 for (i = 0; i < n2; ++i)
9633 e = find_edge (gimple_bb (stmt),
9634 label_to_block (CASE_LABEL (TREE_VEC_ELT (vec2, i))));
9635 e->aux = (void *)-1;
9638 /* Queue not needed edges for later removal. */
9639 FOR_EACH_EDGE (e, ei, gimple_bb (stmt)->succs)
9641 if (e->aux == (void *)-1)
9643 e->aux = NULL;
9644 continue;
9647 if (dump_file && (dump_flags & TDF_DETAILS))
9649 fprintf (dump_file, "removing unreachable case label\n");
9651 to_remove_edges.safe_push (e);
9652 e->flags &= ~EDGE_EXECUTABLE;
9655 /* And queue an update for the stmt. */
9656 su.stmt = stmt;
9657 su.vec = vec2;
9658 to_update_switch_stmts.safe_push (su);
9659 return false;
9662 /* Simplify an integral conversion from an SSA name in STMT. */
9664 static bool
9665 simplify_conversion_using_ranges (gimple *stmt)
9667 tree innerop, middleop, finaltype;
9668 gimple *def_stmt;
9669 value_range *innervr;
9670 signop inner_sgn, middle_sgn, final_sgn;
9671 unsigned inner_prec, middle_prec, final_prec;
9672 widest_int innermin, innermed, innermax, middlemin, middlemed, middlemax;
9674 finaltype = TREE_TYPE (gimple_assign_lhs (stmt));
9675 if (!INTEGRAL_TYPE_P (finaltype))
9676 return false;
9677 middleop = gimple_assign_rhs1 (stmt);
9678 def_stmt = SSA_NAME_DEF_STMT (middleop);
9679 if (!is_gimple_assign (def_stmt)
9680 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt)))
9681 return false;
9682 innerop = gimple_assign_rhs1 (def_stmt);
9683 if (TREE_CODE (innerop) != SSA_NAME
9684 || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (innerop))
9685 return false;
9687 /* Get the value-range of the inner operand. */
9688 innervr = get_value_range (innerop);
9689 if (innervr->type != VR_RANGE
9690 || TREE_CODE (innervr->min) != INTEGER_CST
9691 || TREE_CODE (innervr->max) != INTEGER_CST)
9692 return false;
9694 /* Simulate the conversion chain to check if the result is equal if
9695 the middle conversion is removed. */
9696 innermin = wi::to_widest (innervr->min);
9697 innermax = wi::to_widest (innervr->max);
9699 inner_prec = TYPE_PRECISION (TREE_TYPE (innerop));
9700 middle_prec = TYPE_PRECISION (TREE_TYPE (middleop));
9701 final_prec = TYPE_PRECISION (finaltype);
9703 /* If the first conversion is not injective, the second must not
9704 be widening. */
9705 if (wi::gtu_p (innermax - innermin,
9706 wi::mask <widest_int> (middle_prec, false))
9707 && middle_prec < final_prec)
9708 return false;
9709 /* We also want a medium value so that we can track the effect that
9710 narrowing conversions with sign change have. */
9711 inner_sgn = TYPE_SIGN (TREE_TYPE (innerop));
9712 if (inner_sgn == UNSIGNED)
9713 innermed = wi::shifted_mask <widest_int> (1, inner_prec - 1, false);
9714 else
9715 innermed = 0;
9716 if (wi::cmp (innermin, innermed, inner_sgn) >= 0
9717 || wi::cmp (innermed, innermax, inner_sgn) >= 0)
9718 innermed = innermin;
9720 middle_sgn = TYPE_SIGN (TREE_TYPE (middleop));
9721 middlemin = wi::ext (innermin, middle_prec, middle_sgn);
9722 middlemed = wi::ext (innermed, middle_prec, middle_sgn);
9723 middlemax = wi::ext (innermax, middle_prec, middle_sgn);
9725 /* Require that the final conversion applied to both the original
9726 and the intermediate range produces the same result. */
9727 final_sgn = TYPE_SIGN (finaltype);
9728 if (wi::ext (middlemin, final_prec, final_sgn)
9729 != wi::ext (innermin, final_prec, final_sgn)
9730 || wi::ext (middlemed, final_prec, final_sgn)
9731 != wi::ext (innermed, final_prec, final_sgn)
9732 || wi::ext (middlemax, final_prec, final_sgn)
9733 != wi::ext (innermax, final_prec, final_sgn))
9734 return false;
9736 gimple_assign_set_rhs1 (stmt, innerop);
9737 update_stmt (stmt);
9738 return true;
9741 /* Simplify a conversion from integral SSA name to float in STMT. */
9743 static bool
9744 simplify_float_conversion_using_ranges (gimple_stmt_iterator *gsi,
9745 gimple *stmt)
9747 tree rhs1 = gimple_assign_rhs1 (stmt);
9748 value_range *vr = get_value_range (rhs1);
9749 machine_mode fltmode = TYPE_MODE (TREE_TYPE (gimple_assign_lhs (stmt)));
9750 machine_mode mode;
9751 tree tem;
9752 gassign *conv;
9754 /* We can only handle constant ranges. */
9755 if (vr->type != VR_RANGE
9756 || TREE_CODE (vr->min) != INTEGER_CST
9757 || TREE_CODE (vr->max) != INTEGER_CST)
9758 return false;
9760 /* First check if we can use a signed type in place of an unsigned. */
9761 if (TYPE_UNSIGNED (TREE_TYPE (rhs1))
9762 && (can_float_p (fltmode, TYPE_MODE (TREE_TYPE (rhs1)), 0)
9763 != CODE_FOR_nothing)
9764 && range_fits_type_p (vr, TYPE_PRECISION (TREE_TYPE (rhs1)), SIGNED))
9765 mode = TYPE_MODE (TREE_TYPE (rhs1));
9766 /* If we can do the conversion in the current input mode do nothing. */
9767 else if (can_float_p (fltmode, TYPE_MODE (TREE_TYPE (rhs1)),
9768 TYPE_UNSIGNED (TREE_TYPE (rhs1))) != CODE_FOR_nothing)
9769 return false;
9770 /* Otherwise search for a mode we can use, starting from the narrowest
9771 integer mode available. */
9772 else
9774 mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
9777 /* If we cannot do a signed conversion to float from mode
9778 or if the value-range does not fit in the signed type
9779 try with a wider mode. */
9780 if (can_float_p (fltmode, mode, 0) != CODE_FOR_nothing
9781 && range_fits_type_p (vr, GET_MODE_PRECISION (mode), SIGNED))
9782 break;
9784 mode = GET_MODE_WIDER_MODE (mode);
9785 /* But do not widen the input. Instead leave that to the
9786 optabs expansion code. */
9787 if (GET_MODE_PRECISION (mode) > TYPE_PRECISION (TREE_TYPE (rhs1)))
9788 return false;
9790 while (mode != VOIDmode);
9791 if (mode == VOIDmode)
9792 return false;
9795 /* It works, insert a truncation or sign-change before the
9796 float conversion. */
9797 tem = make_ssa_name (build_nonstandard_integer_type
9798 (GET_MODE_PRECISION (mode), 0));
9799 conv = gimple_build_assign (tem, NOP_EXPR, rhs1);
9800 gsi_insert_before (gsi, conv, GSI_SAME_STMT);
9801 gimple_assign_set_rhs1 (stmt, tem);
9802 update_stmt (stmt);
9804 return true;
9807 /* Simplify an internal fn call using ranges if possible. */
9809 static bool
9810 simplify_internal_call_using_ranges (gimple_stmt_iterator *gsi, gimple *stmt)
9812 enum tree_code subcode;
9813 bool is_ubsan = false;
9814 bool ovf = false;
9815 switch (gimple_call_internal_fn (stmt))
9817 case IFN_UBSAN_CHECK_ADD:
9818 subcode = PLUS_EXPR;
9819 is_ubsan = true;
9820 break;
9821 case IFN_UBSAN_CHECK_SUB:
9822 subcode = MINUS_EXPR;
9823 is_ubsan = true;
9824 break;
9825 case IFN_UBSAN_CHECK_MUL:
9826 subcode = MULT_EXPR;
9827 is_ubsan = true;
9828 break;
9829 case IFN_ADD_OVERFLOW:
9830 subcode = PLUS_EXPR;
9831 break;
9832 case IFN_SUB_OVERFLOW:
9833 subcode = MINUS_EXPR;
9834 break;
9835 case IFN_MUL_OVERFLOW:
9836 subcode = MULT_EXPR;
9837 break;
9838 default:
9839 return false;
9842 tree op0 = gimple_call_arg (stmt, 0);
9843 tree op1 = gimple_call_arg (stmt, 1);
9844 tree type;
9845 if (is_ubsan)
9846 type = TREE_TYPE (op0);
9847 else if (gimple_call_lhs (stmt) == NULL_TREE)
9848 return false;
9849 else
9850 type = TREE_TYPE (TREE_TYPE (gimple_call_lhs (stmt)));
9851 if (!check_for_binary_op_overflow (subcode, type, op0, op1, &ovf)
9852 || (is_ubsan && ovf))
9853 return false;
9855 gimple *g;
9856 location_t loc = gimple_location (stmt);
9857 if (is_ubsan)
9858 g = gimple_build_assign (gimple_call_lhs (stmt), subcode, op0, op1);
9859 else
9861 int prec = TYPE_PRECISION (type);
9862 tree utype = type;
9863 if (ovf
9864 || !useless_type_conversion_p (type, TREE_TYPE (op0))
9865 || !useless_type_conversion_p (type, TREE_TYPE (op1)))
9866 utype = build_nonstandard_integer_type (prec, 1);
9867 if (TREE_CODE (op0) == INTEGER_CST)
9868 op0 = fold_convert (utype, op0);
9869 else if (!useless_type_conversion_p (utype, TREE_TYPE (op0)))
9871 g = gimple_build_assign (make_ssa_name (utype), NOP_EXPR, op0);
9872 gimple_set_location (g, loc);
9873 gsi_insert_before (gsi, g, GSI_SAME_STMT);
9874 op0 = gimple_assign_lhs (g);
9876 if (TREE_CODE (op1) == INTEGER_CST)
9877 op1 = fold_convert (utype, op1);
9878 else if (!useless_type_conversion_p (utype, TREE_TYPE (op1)))
9880 g = gimple_build_assign (make_ssa_name (utype), NOP_EXPR, op1);
9881 gimple_set_location (g, loc);
9882 gsi_insert_before (gsi, g, GSI_SAME_STMT);
9883 op1 = gimple_assign_lhs (g);
9885 g = gimple_build_assign (make_ssa_name (utype), subcode, op0, op1);
9886 gimple_set_location (g, loc);
9887 gsi_insert_before (gsi, g, GSI_SAME_STMT);
9888 if (utype != type)
9890 g = gimple_build_assign (make_ssa_name (type), NOP_EXPR,
9891 gimple_assign_lhs (g));
9892 gimple_set_location (g, loc);
9893 gsi_insert_before (gsi, g, GSI_SAME_STMT);
9895 g = gimple_build_assign (gimple_call_lhs (stmt), COMPLEX_EXPR,
9896 gimple_assign_lhs (g),
9897 build_int_cst (type, ovf));
9899 gimple_set_location (g, loc);
9900 gsi_replace (gsi, g, false);
9901 return true;
9904 /* Simplify STMT using ranges if possible. */
9906 static bool
9907 simplify_stmt_using_ranges (gimple_stmt_iterator *gsi)
9909 gimple *stmt = gsi_stmt (*gsi);
9910 if (is_gimple_assign (stmt))
9912 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
9913 tree rhs1 = gimple_assign_rhs1 (stmt);
9915 switch (rhs_code)
9917 case EQ_EXPR:
9918 case NE_EXPR:
9919 /* Transform EQ_EXPR, NE_EXPR into BIT_XOR_EXPR or identity
9920 if the RHS is zero or one, and the LHS are known to be boolean
9921 values. */
9922 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9923 return simplify_truth_ops_using_ranges (gsi, stmt);
9924 break;
9926 /* Transform TRUNC_DIV_EXPR and TRUNC_MOD_EXPR into RSHIFT_EXPR
9927 and BIT_AND_EXPR respectively if the first operand is greater
9928 than zero and the second operand is an exact power of two.
9929 Also optimize TRUNC_MOD_EXPR away if the second operand is
9930 constant and the first operand already has the right value
9931 range. */
9932 case TRUNC_DIV_EXPR:
9933 case TRUNC_MOD_EXPR:
9934 if (TREE_CODE (rhs1) == SSA_NAME
9935 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9936 return simplify_div_or_mod_using_ranges (stmt);
9937 break;
9939 /* Transform ABS (X) into X or -X as appropriate. */
9940 case ABS_EXPR:
9941 if (TREE_CODE (rhs1) == SSA_NAME
9942 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9943 return simplify_abs_using_ranges (stmt);
9944 break;
9946 case BIT_AND_EXPR:
9947 case BIT_IOR_EXPR:
9948 /* Optimize away BIT_AND_EXPR and BIT_IOR_EXPR
9949 if all the bits being cleared are already cleared or
9950 all the bits being set are already set. */
9951 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9952 return simplify_bit_ops_using_ranges (gsi, stmt);
9953 break;
9955 CASE_CONVERT:
9956 if (TREE_CODE (rhs1) == SSA_NAME
9957 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9958 return simplify_conversion_using_ranges (stmt);
9959 break;
9961 case FLOAT_EXPR:
9962 if (TREE_CODE (rhs1) == SSA_NAME
9963 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9964 return simplify_float_conversion_using_ranges (gsi, stmt);
9965 break;
9967 case MIN_EXPR:
9968 case MAX_EXPR:
9969 return simplify_min_or_max_using_ranges (stmt);
9970 break;
9972 default:
9973 break;
9976 else if (gimple_code (stmt) == GIMPLE_COND)
9977 return simplify_cond_using_ranges (as_a <gcond *> (stmt));
9978 else if (gimple_code (stmt) == GIMPLE_SWITCH)
9979 return simplify_switch_using_ranges (as_a <gswitch *> (stmt));
9980 else if (is_gimple_call (stmt)
9981 && gimple_call_internal_p (stmt))
9982 return simplify_internal_call_using_ranges (gsi, stmt);
9984 return false;
9987 /* If the statement pointed by SI has a predicate whose value can be
9988 computed using the value range information computed by VRP, compute
9989 its value and return true. Otherwise, return false. */
9991 static bool
9992 fold_predicate_in (gimple_stmt_iterator *si)
9994 bool assignment_p = false;
9995 tree val;
9996 gimple *stmt = gsi_stmt (*si);
9998 if (is_gimple_assign (stmt)
9999 && TREE_CODE_CLASS (gimple_assign_rhs_code (stmt)) == tcc_comparison)
10001 assignment_p = true;
10002 val = vrp_evaluate_conditional (gimple_assign_rhs_code (stmt),
10003 gimple_assign_rhs1 (stmt),
10004 gimple_assign_rhs2 (stmt),
10005 stmt);
10007 else if (gcond *cond_stmt = dyn_cast <gcond *> (stmt))
10008 val = vrp_evaluate_conditional (gimple_cond_code (cond_stmt),
10009 gimple_cond_lhs (cond_stmt),
10010 gimple_cond_rhs (cond_stmt),
10011 stmt);
10012 else
10013 return false;
10015 if (val)
10017 if (assignment_p)
10018 val = fold_convert (gimple_expr_type (stmt), val);
10020 if (dump_file)
10022 fprintf (dump_file, "Folding predicate ");
10023 print_gimple_expr (dump_file, stmt, 0, 0);
10024 fprintf (dump_file, " to ");
10025 print_generic_expr (dump_file, val, 0);
10026 fprintf (dump_file, "\n");
10029 if (is_gimple_assign (stmt))
10030 gimple_assign_set_rhs_from_tree (si, val);
10031 else
10033 gcc_assert (gimple_code (stmt) == GIMPLE_COND);
10034 gcond *cond_stmt = as_a <gcond *> (stmt);
10035 if (integer_zerop (val))
10036 gimple_cond_make_false (cond_stmt);
10037 else if (integer_onep (val))
10038 gimple_cond_make_true (cond_stmt);
10039 else
10040 gcc_unreachable ();
10043 return true;
10046 return false;
10049 /* Callback for substitute_and_fold folding the stmt at *SI. */
10051 static bool
10052 vrp_fold_stmt (gimple_stmt_iterator *si)
10054 if (fold_predicate_in (si))
10055 return true;
10057 return simplify_stmt_using_ranges (si);
10060 /* Unwindable const/copy equivalences. */
10061 const_and_copies *equiv_stack;
10063 /* A trivial wrapper so that we can present the generic jump threading
10064 code with a simple API for simplifying statements. STMT is the
10065 statement we want to simplify, WITHIN_STMT provides the location
10066 for any overflow warnings. */
10068 static tree
10069 simplify_stmt_for_jump_threading (gimple *stmt, gimple *within_stmt,
10070 class avail_exprs_stack *avail_exprs_stack ATTRIBUTE_UNUSED)
10072 if (gcond *cond_stmt = dyn_cast <gcond *> (stmt))
10073 return vrp_evaluate_conditional (gimple_cond_code (cond_stmt),
10074 gimple_cond_lhs (cond_stmt),
10075 gimple_cond_rhs (cond_stmt),
10076 within_stmt);
10078 if (gassign *assign_stmt = dyn_cast <gassign *> (stmt))
10080 value_range new_vr = VR_INITIALIZER;
10081 tree lhs = gimple_assign_lhs (assign_stmt);
10083 if (TREE_CODE (lhs) == SSA_NAME
10084 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs))
10085 || POINTER_TYPE_P (TREE_TYPE (lhs))))
10087 extract_range_from_assignment (&new_vr, assign_stmt);
10088 if (range_int_cst_singleton_p (&new_vr))
10089 return new_vr.min;
10093 return NULL_TREE;
10096 /* Blocks which have more than one predecessor and more than
10097 one successor present jump threading opportunities, i.e.,
10098 when the block is reached from a specific predecessor, we
10099 may be able to determine which of the outgoing edges will
10100 be traversed. When this optimization applies, we are able
10101 to avoid conditionals at runtime and we may expose secondary
10102 optimization opportunities.
10104 This routine is effectively a driver for the generic jump
10105 threading code. It basically just presents the generic code
10106 with edges that may be suitable for jump threading.
10108 Unlike DOM, we do not iterate VRP if jump threading was successful.
10109 While iterating may expose new opportunities for VRP, it is expected
10110 those opportunities would be very limited and the compile time cost
10111 to expose those opportunities would be significant.
10113 As jump threading opportunities are discovered, they are registered
10114 for later realization. */
10116 static void
10117 identify_jump_threads (void)
10119 basic_block bb;
10120 gcond *dummy;
10121 int i;
10122 edge e;
10124 /* Ugh. When substituting values earlier in this pass we can
10125 wipe the dominance information. So rebuild the dominator
10126 information as we need it within the jump threading code. */
10127 calculate_dominance_info (CDI_DOMINATORS);
10129 /* We do not allow VRP information to be used for jump threading
10130 across a back edge in the CFG. Otherwise it becomes too
10131 difficult to avoid eliminating loop exit tests. Of course
10132 EDGE_DFS_BACK is not accurate at this time so we have to
10133 recompute it. */
10134 mark_dfs_back_edges ();
10136 /* Do not thread across edges we are about to remove. Just marking
10137 them as EDGE_DFS_BACK will do. */
10138 FOR_EACH_VEC_ELT (to_remove_edges, i, e)
10139 e->flags |= EDGE_DFS_BACK;
10141 /* Allocate our unwinder stack to unwind any temporary equivalences
10142 that might be recorded. */
10143 equiv_stack = new const_and_copies ();
10145 /* To avoid lots of silly node creation, we create a single
10146 conditional and just modify it in-place when attempting to
10147 thread jumps. */
10148 dummy = gimple_build_cond (EQ_EXPR,
10149 integer_zero_node, integer_zero_node,
10150 NULL, NULL);
10152 /* Walk through all the blocks finding those which present a
10153 potential jump threading opportunity. We could set this up
10154 as a dominator walker and record data during the walk, but
10155 I doubt it's worth the effort for the classes of jump
10156 threading opportunities we are trying to identify at this
10157 point in compilation. */
10158 FOR_EACH_BB_FN (bb, cfun)
10160 gimple *last;
10162 /* If the generic jump threading code does not find this block
10163 interesting, then there is nothing to do. */
10164 if (! potentially_threadable_block (bb))
10165 continue;
10167 last = last_stmt (bb);
10169 /* We're basically looking for a switch or any kind of conditional with
10170 integral or pointer type arguments. Note the type of the second
10171 argument will be the same as the first argument, so no need to
10172 check it explicitly.
10174 We also handle the case where there are no statements in the
10175 block. This come up with forwarder blocks that are not
10176 optimized away because they lead to a loop header. But we do
10177 want to thread through them as we can sometimes thread to the
10178 loop exit which is obviously profitable. */
10179 if (!last
10180 || gimple_code (last) == GIMPLE_SWITCH
10181 || (gimple_code (last) == GIMPLE_COND
10182 && TREE_CODE (gimple_cond_lhs (last)) == SSA_NAME
10183 && (INTEGRAL_TYPE_P (TREE_TYPE (gimple_cond_lhs (last)))
10184 || POINTER_TYPE_P (TREE_TYPE (gimple_cond_lhs (last))))
10185 && (TREE_CODE (gimple_cond_rhs (last)) == SSA_NAME
10186 || is_gimple_min_invariant (gimple_cond_rhs (last)))))
10188 edge_iterator ei;
10190 /* We've got a block with multiple predecessors and multiple
10191 successors which also ends in a suitable conditional or
10192 switch statement. For each predecessor, see if we can thread
10193 it to a specific successor. */
10194 FOR_EACH_EDGE (e, ei, bb->preds)
10196 /* Do not thread across back edges or abnormal edges
10197 in the CFG. */
10198 if (e->flags & (EDGE_DFS_BACK | EDGE_COMPLEX))
10199 continue;
10201 thread_across_edge (dummy, e, true, equiv_stack, NULL,
10202 simplify_stmt_for_jump_threading);
10207 /* We do not actually update the CFG or SSA graphs at this point as
10208 ASSERT_EXPRs are still in the IL and cfg cleanup code does not yet
10209 handle ASSERT_EXPRs gracefully. */
10212 /* We identified all the jump threading opportunities earlier, but could
10213 not transform the CFG at that time. This routine transforms the
10214 CFG and arranges for the dominator tree to be rebuilt if necessary.
10216 Note the SSA graph update will occur during the normal TODO
10217 processing by the pass manager. */
10218 static void
10219 finalize_jump_threads (void)
10221 thread_through_all_blocks (false);
10222 delete equiv_stack;
10226 /* Traverse all the blocks folding conditionals with known ranges. */
10228 static void
10229 vrp_finalize (void)
10231 size_t i;
10233 values_propagated = true;
10235 if (dump_file)
10237 fprintf (dump_file, "\nValue ranges after VRP:\n\n");
10238 dump_all_value_ranges (dump_file);
10239 fprintf (dump_file, "\n");
10242 substitute_and_fold (op_with_constant_singleton_value_range,
10243 vrp_fold_stmt, false);
10245 if (warn_array_bounds && first_pass_instance)
10246 check_all_array_refs ();
10248 /* We must identify jump threading opportunities before we release
10249 the datastructures built by VRP. */
10250 identify_jump_threads ();
10252 /* Set value range to non pointer SSA_NAMEs. */
10253 for (i = 0; i < num_vr_values; i++)
10254 if (vr_value[i])
10256 tree name = ssa_name (i);
10258 if (!name
10259 || POINTER_TYPE_P (TREE_TYPE (name))
10260 || (vr_value[i]->type == VR_VARYING)
10261 || (vr_value[i]->type == VR_UNDEFINED))
10262 continue;
10264 if ((TREE_CODE (vr_value[i]->min) == INTEGER_CST)
10265 && (TREE_CODE (vr_value[i]->max) == INTEGER_CST)
10266 && (vr_value[i]->type == VR_RANGE
10267 || vr_value[i]->type == VR_ANTI_RANGE))
10268 set_range_info (name, vr_value[i]->type, vr_value[i]->min,
10269 vr_value[i]->max);
10272 /* Free allocated memory. */
10273 for (i = 0; i < num_vr_values; i++)
10274 if (vr_value[i])
10276 BITMAP_FREE (vr_value[i]->equiv);
10277 free (vr_value[i]);
10280 free (vr_value);
10281 free (vr_phi_edge_counts);
10283 /* So that we can distinguish between VRP data being available
10284 and not available. */
10285 vr_value = NULL;
10286 vr_phi_edge_counts = NULL;
10290 /* Main entry point to VRP (Value Range Propagation). This pass is
10291 loosely based on J. R. C. Patterson, ``Accurate Static Branch
10292 Prediction by Value Range Propagation,'' in SIGPLAN Conference on
10293 Programming Language Design and Implementation, pp. 67-78, 1995.
10294 Also available at http://citeseer.ist.psu.edu/patterson95accurate.html
10296 This is essentially an SSA-CCP pass modified to deal with ranges
10297 instead of constants.
10299 While propagating ranges, we may find that two or more SSA name
10300 have equivalent, though distinct ranges. For instance,
10302 1 x_9 = p_3->a;
10303 2 p_4 = ASSERT_EXPR <p_3, p_3 != 0>
10304 3 if (p_4 == q_2)
10305 4 p_5 = ASSERT_EXPR <p_4, p_4 == q_2>;
10306 5 endif
10307 6 if (q_2)
10309 In the code above, pointer p_5 has range [q_2, q_2], but from the
10310 code we can also determine that p_5 cannot be NULL and, if q_2 had
10311 a non-varying range, p_5's range should also be compatible with it.
10313 These equivalences are created by two expressions: ASSERT_EXPR and
10314 copy operations. Since p_5 is an assertion on p_4, and p_4 was the
10315 result of another assertion, then we can use the fact that p_5 and
10316 p_4 are equivalent when evaluating p_5's range.
10318 Together with value ranges, we also propagate these equivalences
10319 between names so that we can take advantage of information from
10320 multiple ranges when doing final replacement. Note that this
10321 equivalency relation is transitive but not symmetric.
10323 In the example above, p_5 is equivalent to p_4, q_2 and p_3, but we
10324 cannot assert that q_2 is equivalent to p_5 because q_2 may be used
10325 in contexts where that assertion does not hold (e.g., in line 6).
10327 TODO, the main difference between this pass and Patterson's is that
10328 we do not propagate edge probabilities. We only compute whether
10329 edges can be taken or not. That is, instead of having a spectrum
10330 of jump probabilities between 0 and 1, we only deal with 0, 1 and
10331 DON'T KNOW. In the future, it may be worthwhile to propagate
10332 probabilities to aid branch prediction. */
10334 static unsigned int
10335 execute_vrp (void)
10337 int i;
10338 edge e;
10339 switch_update *su;
10341 loop_optimizer_init (LOOPS_NORMAL | LOOPS_HAVE_RECORDED_EXITS);
10342 rewrite_into_loop_closed_ssa (NULL, TODO_update_ssa);
10343 scev_initialize ();
10345 /* ??? This ends up using stale EDGE_DFS_BACK for liveness computation.
10346 Inserting assertions may split edges which will invalidate
10347 EDGE_DFS_BACK. */
10348 insert_range_assertions ();
10350 to_remove_edges.create (10);
10351 to_update_switch_stmts.create (5);
10352 threadedge_initialize_values ();
10354 /* For visiting PHI nodes we need EDGE_DFS_BACK computed. */
10355 mark_dfs_back_edges ();
10357 vrp_initialize ();
10358 ssa_propagate (vrp_visit_stmt, vrp_visit_phi_node);
10359 vrp_finalize ();
10361 free_numbers_of_iterations_estimates ();
10363 /* ASSERT_EXPRs must be removed before finalizing jump threads
10364 as finalizing jump threads calls the CFG cleanup code which
10365 does not properly handle ASSERT_EXPRs. */
10366 remove_range_assertions ();
10368 /* If we exposed any new variables, go ahead and put them into
10369 SSA form now, before we handle jump threading. This simplifies
10370 interactions between rewriting of _DECL nodes into SSA form
10371 and rewriting SSA_NAME nodes into SSA form after block
10372 duplication and CFG manipulation. */
10373 update_ssa (TODO_update_ssa);
10375 finalize_jump_threads ();
10377 /* Remove dead edges from SWITCH_EXPR optimization. This leaves the
10378 CFG in a broken state and requires a cfg_cleanup run. */
10379 FOR_EACH_VEC_ELT (to_remove_edges, i, e)
10380 remove_edge (e);
10381 /* Update SWITCH_EXPR case label vector. */
10382 FOR_EACH_VEC_ELT (to_update_switch_stmts, i, su)
10384 size_t j;
10385 size_t n = TREE_VEC_LENGTH (su->vec);
10386 tree label;
10387 gimple_switch_set_num_labels (su->stmt, n);
10388 for (j = 0; j < n; j++)
10389 gimple_switch_set_label (su->stmt, j, TREE_VEC_ELT (su->vec, j));
10390 /* As we may have replaced the default label with a regular one
10391 make sure to make it a real default label again. This ensures
10392 optimal expansion. */
10393 label = gimple_switch_label (su->stmt, 0);
10394 CASE_LOW (label) = NULL_TREE;
10395 CASE_HIGH (label) = NULL_TREE;
10398 if (to_remove_edges.length () > 0)
10400 free_dominance_info (CDI_DOMINATORS);
10401 loops_state_set (LOOPS_NEED_FIXUP);
10404 to_remove_edges.release ();
10405 to_update_switch_stmts.release ();
10406 threadedge_finalize_values ();
10408 scev_finalize ();
10409 loop_optimizer_finalize ();
10410 return 0;
10413 namespace {
10415 const pass_data pass_data_vrp =
10417 GIMPLE_PASS, /* type */
10418 "vrp", /* name */
10419 OPTGROUP_NONE, /* optinfo_flags */
10420 TV_TREE_VRP, /* tv_id */
10421 PROP_ssa, /* properties_required */
10422 0, /* properties_provided */
10423 0, /* properties_destroyed */
10424 0, /* todo_flags_start */
10425 ( TODO_cleanup_cfg | TODO_update_ssa ), /* todo_flags_finish */
10428 class pass_vrp : public gimple_opt_pass
10430 public:
10431 pass_vrp (gcc::context *ctxt)
10432 : gimple_opt_pass (pass_data_vrp, ctxt)
10435 /* opt_pass methods: */
10436 opt_pass * clone () { return new pass_vrp (m_ctxt); }
10437 virtual bool gate (function *) { return flag_tree_vrp != 0; }
10438 virtual unsigned int execute (function *) { return execute_vrp (); }
10440 }; // class pass_vrp
10442 } // anon namespace
10444 gimple_opt_pass *
10445 make_pass_vrp (gcc::context *ctxt)
10447 return new pass_vrp (ctxt);