Update my email address.
[official-gcc.git] / gcc / tree-vrp.c
blob00923c091a22a436bc6297cb920407b46e1427f3
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-config.h"
58 #include "expmed.h"
59 #include "dojump.h"
60 #include "explow.h"
61 #include "emit-rtl.h"
62 #include "varasm.h"
63 #include "stmt.h"
64 #include "expr.h"
65 #include "insn-codes.h"
66 #include "optabs.h"
67 #include "tree-ssa-scopedtables.h"
68 #include "tree-ssa-threadedge.h"
72 /* Range of values that can be associated with an SSA_NAME after VRP
73 has executed. */
74 struct value_range_d
76 /* Lattice value represented by this range. */
77 enum value_range_type type;
79 /* Minimum and maximum values represented by this range. These
80 values should be interpreted as follows:
82 - If TYPE is VR_UNDEFINED or VR_VARYING then MIN and MAX must
83 be NULL.
85 - If TYPE == VR_RANGE then MIN holds the minimum value and
86 MAX holds the maximum value of the range [MIN, MAX].
88 - If TYPE == ANTI_RANGE the variable is known to NOT
89 take any values in the range [MIN, MAX]. */
90 tree min;
91 tree max;
93 /* Set of SSA names whose value ranges are equivalent to this one.
94 This set is only valid when TYPE is VR_RANGE or VR_ANTI_RANGE. */
95 bitmap equiv;
98 typedef struct value_range_d value_range_t;
100 #define VR_INITIALIZER { VR_UNDEFINED, NULL_TREE, NULL_TREE, NULL }
102 /* Set of SSA names found live during the RPO traversal of the function
103 for still active basic-blocks. */
104 static sbitmap *live;
106 /* Return true if the SSA name NAME is live on the edge E. */
108 static bool
109 live_on_edge (edge e, tree name)
111 return (live[e->dest->index]
112 && bitmap_bit_p (live[e->dest->index], SSA_NAME_VERSION (name)));
115 /* Local functions. */
116 static int compare_values (tree val1, tree val2);
117 static int compare_values_warnv (tree val1, tree val2, bool *);
118 static void vrp_meet (value_range_t *, value_range_t *);
119 static void vrp_intersect_ranges (value_range_t *, value_range_t *);
120 static tree vrp_evaluate_conditional_warnv_with_ops (enum tree_code,
121 tree, tree, bool, bool *,
122 bool *);
124 /* Location information for ASSERT_EXPRs. Each instance of this
125 structure describes an ASSERT_EXPR for an SSA name. Since a single
126 SSA name may have more than one assertion associated with it, these
127 locations are kept in a linked list attached to the corresponding
128 SSA name. */
129 struct assert_locus
131 /* Basic block where the assertion would be inserted. */
132 basic_block bb;
134 /* Some assertions need to be inserted on an edge (e.g., assertions
135 generated by COND_EXPRs). In those cases, BB will be NULL. */
136 edge e;
138 /* Pointer to the statement that generated this assertion. */
139 gimple_stmt_iterator si;
141 /* Predicate code for the ASSERT_EXPR. Must be COMPARISON_CLASS_P. */
142 enum tree_code comp_code;
144 /* Value being compared against. */
145 tree val;
147 /* Expression to compare. */
148 tree expr;
150 /* Next node in the linked list. */
151 assert_locus *next;
154 /* If bit I is present, it means that SSA name N_i has a list of
155 assertions that should be inserted in the IL. */
156 static bitmap need_assert_for;
158 /* Array of locations lists where to insert assertions. ASSERTS_FOR[I]
159 holds a list of ASSERT_LOCUS_T nodes that describe where
160 ASSERT_EXPRs for SSA name N_I should be inserted. */
161 static assert_locus **asserts_for;
163 /* Value range array. After propagation, VR_VALUE[I] holds the range
164 of values that SSA name N_I may take. */
165 static unsigned num_vr_values;
166 static value_range_t **vr_value;
167 static bool values_propagated;
169 /* For a PHI node which sets SSA name N_I, VR_COUNTS[I] holds the
170 number of executable edges we saw the last time we visited the
171 node. */
172 static int *vr_phi_edge_counts;
174 struct switch_update {
175 gswitch *stmt;
176 tree vec;
179 static vec<edge> to_remove_edges;
180 static vec<switch_update> to_update_switch_stmts;
183 /* Return the maximum value for TYPE. */
185 static inline tree
186 vrp_val_max (const_tree type)
188 if (!INTEGRAL_TYPE_P (type))
189 return NULL_TREE;
191 return TYPE_MAX_VALUE (type);
194 /* Return the minimum value for TYPE. */
196 static inline tree
197 vrp_val_min (const_tree type)
199 if (!INTEGRAL_TYPE_P (type))
200 return NULL_TREE;
202 return TYPE_MIN_VALUE (type);
205 /* Return whether VAL is equal to the maximum value of its type. This
206 will be true for a positive overflow infinity. We can't do a
207 simple equality comparison with TYPE_MAX_VALUE because C typedefs
208 and Ada subtypes can produce types whose TYPE_MAX_VALUE is not ==
209 to the integer constant with the same value in the type. */
211 static inline bool
212 vrp_val_is_max (const_tree val)
214 tree type_max = vrp_val_max (TREE_TYPE (val));
215 return (val == type_max
216 || (type_max != NULL_TREE
217 && operand_equal_p (val, type_max, 0)));
220 /* Return whether VAL is equal to the minimum value of its type. This
221 will be true for a negative overflow infinity. */
223 static inline bool
224 vrp_val_is_min (const_tree val)
226 tree type_min = vrp_val_min (TREE_TYPE (val));
227 return (val == type_min
228 || (type_min != NULL_TREE
229 && operand_equal_p (val, type_min, 0)));
233 /* Return whether TYPE should use an overflow infinity distinct from
234 TYPE_{MIN,MAX}_VALUE. We use an overflow infinity value to
235 represent a signed overflow during VRP computations. An infinity
236 is distinct from a half-range, which will go from some number to
237 TYPE_{MIN,MAX}_VALUE. */
239 static inline bool
240 needs_overflow_infinity (const_tree type)
242 return INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_WRAPS (type);
245 /* Return whether TYPE can support our overflow infinity
246 representation: we use the TREE_OVERFLOW flag, which only exists
247 for constants. If TYPE doesn't support this, we don't optimize
248 cases which would require signed overflow--we drop them to
249 VARYING. */
251 static inline bool
252 supports_overflow_infinity (const_tree type)
254 tree min = vrp_val_min (type), max = vrp_val_max (type);
255 #ifdef ENABLE_CHECKING
256 gcc_assert (needs_overflow_infinity (type));
257 #endif
258 return (min != NULL_TREE
259 && CONSTANT_CLASS_P (min)
260 && max != NULL_TREE
261 && CONSTANT_CLASS_P (max));
264 /* VAL is the maximum or minimum value of a type. Return a
265 corresponding overflow infinity. */
267 static inline tree
268 make_overflow_infinity (tree val)
270 gcc_checking_assert (val != NULL_TREE && CONSTANT_CLASS_P (val));
271 val = copy_node (val);
272 TREE_OVERFLOW (val) = 1;
273 return val;
276 /* Return a negative overflow infinity for TYPE. */
278 static inline tree
279 negative_overflow_infinity (tree type)
281 gcc_checking_assert (supports_overflow_infinity (type));
282 return make_overflow_infinity (vrp_val_min (type));
285 /* Return a positive overflow infinity for TYPE. */
287 static inline tree
288 positive_overflow_infinity (tree type)
290 gcc_checking_assert (supports_overflow_infinity (type));
291 return make_overflow_infinity (vrp_val_max (type));
294 /* Return whether VAL is a negative overflow infinity. */
296 static inline bool
297 is_negative_overflow_infinity (const_tree val)
299 return (TREE_OVERFLOW_P (val)
300 && needs_overflow_infinity (TREE_TYPE (val))
301 && vrp_val_is_min (val));
304 /* Return whether VAL is a positive overflow infinity. */
306 static inline bool
307 is_positive_overflow_infinity (const_tree val)
309 return (TREE_OVERFLOW_P (val)
310 && needs_overflow_infinity (TREE_TYPE (val))
311 && vrp_val_is_max (val));
314 /* Return whether VAL is a positive or negative overflow infinity. */
316 static inline bool
317 is_overflow_infinity (const_tree val)
319 return (TREE_OVERFLOW_P (val)
320 && needs_overflow_infinity (TREE_TYPE (val))
321 && (vrp_val_is_min (val) || vrp_val_is_max (val)));
324 /* Return whether STMT has a constant rhs that is_overflow_infinity. */
326 static inline bool
327 stmt_overflow_infinity (gimple stmt)
329 if (is_gimple_assign (stmt)
330 && get_gimple_rhs_class (gimple_assign_rhs_code (stmt)) ==
331 GIMPLE_SINGLE_RHS)
332 return is_overflow_infinity (gimple_assign_rhs1 (stmt));
333 return false;
336 /* If VAL is now an overflow infinity, return VAL. Otherwise, return
337 the same value with TREE_OVERFLOW clear. This can be used to avoid
338 confusing a regular value with an overflow value. */
340 static inline tree
341 avoid_overflow_infinity (tree val)
343 if (!is_overflow_infinity (val))
344 return val;
346 if (vrp_val_is_max (val))
347 return vrp_val_max (TREE_TYPE (val));
348 else
350 gcc_checking_assert (vrp_val_is_min (val));
351 return vrp_val_min (TREE_TYPE (val));
356 /* Set value range VR to VR_UNDEFINED. */
358 static inline void
359 set_value_range_to_undefined (value_range_t *vr)
361 vr->type = VR_UNDEFINED;
362 vr->min = vr->max = NULL_TREE;
363 if (vr->equiv)
364 bitmap_clear (vr->equiv);
368 /* Set value range VR to VR_VARYING. */
370 static inline void
371 set_value_range_to_varying (value_range_t *vr)
373 vr->type = VR_VARYING;
374 vr->min = vr->max = NULL_TREE;
375 if (vr->equiv)
376 bitmap_clear (vr->equiv);
380 /* Set value range VR to {T, MIN, MAX, EQUIV}. */
382 static void
383 set_value_range (value_range_t *vr, enum value_range_type t, tree min,
384 tree max, bitmap equiv)
386 #if defined ENABLE_CHECKING
387 /* Check the validity of the range. */
388 if (t == VR_RANGE || t == VR_ANTI_RANGE)
390 int cmp;
392 gcc_assert (min && max);
394 gcc_assert ((!TREE_OVERFLOW_P (min) || is_overflow_infinity (min))
395 && (!TREE_OVERFLOW_P (max) || is_overflow_infinity (max)));
397 if (INTEGRAL_TYPE_P (TREE_TYPE (min)) && t == VR_ANTI_RANGE)
398 gcc_assert (!vrp_val_is_min (min) || !vrp_val_is_max (max));
400 cmp = compare_values (min, max);
401 gcc_assert (cmp == 0 || cmp == -1 || cmp == -2);
403 if (needs_overflow_infinity (TREE_TYPE (min)))
404 gcc_assert (!is_overflow_infinity (min)
405 || !is_overflow_infinity (max));
408 if (t == VR_UNDEFINED || t == VR_VARYING)
409 gcc_assert (min == NULL_TREE && max == NULL_TREE);
411 if (t == VR_UNDEFINED || t == VR_VARYING)
412 gcc_assert (equiv == NULL || bitmap_empty_p (equiv));
413 #endif
415 vr->type = t;
416 vr->min = min;
417 vr->max = max;
419 /* Since updating the equivalence set involves deep copying the
420 bitmaps, only do it if absolutely necessary. */
421 if (vr->equiv == NULL
422 && equiv != NULL)
423 vr->equiv = BITMAP_ALLOC (NULL);
425 if (equiv != vr->equiv)
427 if (equiv && !bitmap_empty_p (equiv))
428 bitmap_copy (vr->equiv, equiv);
429 else
430 bitmap_clear (vr->equiv);
435 /* Set value range VR to the canonical form of {T, MIN, MAX, EQUIV}.
436 This means adjusting T, MIN and MAX representing the case of a
437 wrapping range with MAX < MIN covering [MIN, type_max] U [type_min, MAX]
438 as anti-rage ~[MAX+1, MIN-1]. Likewise for wrapping anti-ranges.
439 In corner cases where MAX+1 or MIN-1 wraps this will fall back
440 to varying.
441 This routine exists to ease canonicalization in the case where we
442 extract ranges from var + CST op limit. */
444 static void
445 set_and_canonicalize_value_range (value_range_t *vr, enum value_range_type t,
446 tree min, tree max, bitmap equiv)
448 /* Use the canonical setters for VR_UNDEFINED and VR_VARYING. */
449 if (t == VR_UNDEFINED)
451 set_value_range_to_undefined (vr);
452 return;
454 else if (t == VR_VARYING)
456 set_value_range_to_varying (vr);
457 return;
460 /* Nothing to canonicalize for symbolic ranges. */
461 if (TREE_CODE (min) != INTEGER_CST
462 || TREE_CODE (max) != INTEGER_CST)
464 set_value_range (vr, t, min, max, equiv);
465 return;
468 /* Wrong order for min and max, to swap them and the VR type we need
469 to adjust them. */
470 if (tree_int_cst_lt (max, min))
472 tree one, tmp;
474 /* For one bit precision if max < min, then the swapped
475 range covers all values, so for VR_RANGE it is varying and
476 for VR_ANTI_RANGE empty range, so drop to varying as well. */
477 if (TYPE_PRECISION (TREE_TYPE (min)) == 1)
479 set_value_range_to_varying (vr);
480 return;
483 one = build_int_cst (TREE_TYPE (min), 1);
484 tmp = int_const_binop (PLUS_EXPR, max, one);
485 max = int_const_binop (MINUS_EXPR, min, one);
486 min = tmp;
488 /* There's one corner case, if we had [C+1, C] before we now have
489 that again. But this represents an empty value range, so drop
490 to varying in this case. */
491 if (tree_int_cst_lt (max, min))
493 set_value_range_to_varying (vr);
494 return;
497 t = t == VR_RANGE ? VR_ANTI_RANGE : VR_RANGE;
500 /* Anti-ranges that can be represented as ranges should be so. */
501 if (t == VR_ANTI_RANGE)
503 bool is_min = vrp_val_is_min (min);
504 bool is_max = vrp_val_is_max (max);
506 if (is_min && is_max)
508 /* We cannot deal with empty ranges, drop to varying.
509 ??? This could be VR_UNDEFINED instead. */
510 set_value_range_to_varying (vr);
511 return;
513 else if (TYPE_PRECISION (TREE_TYPE (min)) == 1
514 && (is_min || is_max))
516 /* Non-empty boolean ranges can always be represented
517 as a singleton range. */
518 if (is_min)
519 min = max = vrp_val_max (TREE_TYPE (min));
520 else
521 min = max = vrp_val_min (TREE_TYPE (min));
522 t = VR_RANGE;
524 else if (is_min
525 /* As a special exception preserve non-null ranges. */
526 && !(TYPE_UNSIGNED (TREE_TYPE (min))
527 && integer_zerop (max)))
529 tree one = build_int_cst (TREE_TYPE (max), 1);
530 min = int_const_binop (PLUS_EXPR, max, one);
531 max = vrp_val_max (TREE_TYPE (max));
532 t = VR_RANGE;
534 else if (is_max)
536 tree one = build_int_cst (TREE_TYPE (min), 1);
537 max = int_const_binop (MINUS_EXPR, min, one);
538 min = vrp_val_min (TREE_TYPE (min));
539 t = VR_RANGE;
543 /* Drop [-INF(OVF), +INF(OVF)] to varying. */
544 if (needs_overflow_infinity (TREE_TYPE (min))
545 && is_overflow_infinity (min)
546 && is_overflow_infinity (max))
548 set_value_range_to_varying (vr);
549 return;
552 set_value_range (vr, t, min, max, equiv);
555 /* Copy value range FROM into value range TO. */
557 static inline void
558 copy_value_range (value_range_t *to, value_range_t *from)
560 set_value_range (to, from->type, from->min, from->max, from->equiv);
563 /* Set value range VR to a single value. This function is only called
564 with values we get from statements, and exists to clear the
565 TREE_OVERFLOW flag so that we don't think we have an overflow
566 infinity when we shouldn't. */
568 static inline void
569 set_value_range_to_value (value_range_t *vr, tree val, bitmap equiv)
571 gcc_assert (is_gimple_min_invariant (val));
572 if (TREE_OVERFLOW_P (val))
573 val = drop_tree_overflow (val);
574 set_value_range (vr, VR_RANGE, val, val, equiv);
577 /* Set value range VR to a non-negative range of type TYPE.
578 OVERFLOW_INFINITY indicates whether to use an overflow infinity
579 rather than TYPE_MAX_VALUE; this should be true if we determine
580 that the range is nonnegative based on the assumption that signed
581 overflow does not occur. */
583 static inline void
584 set_value_range_to_nonnegative (value_range_t *vr, tree type,
585 bool overflow_infinity)
587 tree zero;
589 if (overflow_infinity && !supports_overflow_infinity (type))
591 set_value_range_to_varying (vr);
592 return;
595 zero = build_int_cst (type, 0);
596 set_value_range (vr, VR_RANGE, zero,
597 (overflow_infinity
598 ? positive_overflow_infinity (type)
599 : TYPE_MAX_VALUE (type)),
600 vr->equiv);
603 /* Set value range VR to a non-NULL range of type TYPE. */
605 static inline void
606 set_value_range_to_nonnull (value_range_t *vr, tree type)
608 tree zero = build_int_cst (type, 0);
609 set_value_range (vr, VR_ANTI_RANGE, zero, zero, vr->equiv);
613 /* Set value range VR to a NULL range of type TYPE. */
615 static inline void
616 set_value_range_to_null (value_range_t *vr, tree type)
618 set_value_range_to_value (vr, build_int_cst (type, 0), vr->equiv);
622 /* Set value range VR to a range of a truthvalue of type TYPE. */
624 static inline void
625 set_value_range_to_truthvalue (value_range_t *vr, tree type)
627 if (TYPE_PRECISION (type) == 1)
628 set_value_range_to_varying (vr);
629 else
630 set_value_range (vr, VR_RANGE,
631 build_int_cst (type, 0), build_int_cst (type, 1),
632 vr->equiv);
636 /* If abs (min) < abs (max), set VR to [-max, max], if
637 abs (min) >= abs (max), set VR to [-min, min]. */
639 static void
640 abs_extent_range (value_range_t *vr, tree min, tree max)
642 int cmp;
644 gcc_assert (TREE_CODE (min) == INTEGER_CST);
645 gcc_assert (TREE_CODE (max) == INTEGER_CST);
646 gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (min)));
647 gcc_assert (!TYPE_UNSIGNED (TREE_TYPE (min)));
648 min = fold_unary (ABS_EXPR, TREE_TYPE (min), min);
649 max = fold_unary (ABS_EXPR, TREE_TYPE (max), max);
650 if (TREE_OVERFLOW (min) || TREE_OVERFLOW (max))
652 set_value_range_to_varying (vr);
653 return;
655 cmp = compare_values (min, max);
656 if (cmp == -1)
657 min = fold_unary (NEGATE_EXPR, TREE_TYPE (min), max);
658 else if (cmp == 0 || cmp == 1)
660 max = min;
661 min = fold_unary (NEGATE_EXPR, TREE_TYPE (min), min);
663 else
665 set_value_range_to_varying (vr);
666 return;
668 set_and_canonicalize_value_range (vr, VR_RANGE, min, max, NULL);
672 /* Return value range information for VAR.
674 If we have no values ranges recorded (ie, VRP is not running), then
675 return NULL. Otherwise create an empty range if none existed for VAR. */
677 static value_range_t *
678 get_value_range (const_tree var)
680 static const struct value_range_d vr_const_varying
681 = { VR_VARYING, NULL_TREE, NULL_TREE, NULL };
682 value_range_t *vr;
683 tree sym;
684 unsigned ver = SSA_NAME_VERSION (var);
686 /* If we have no recorded ranges, then return NULL. */
687 if (! vr_value)
688 return NULL;
690 /* If we query the range for a new SSA name return an unmodifiable VARYING.
691 We should get here at most from the substitute-and-fold stage which
692 will never try to change values. */
693 if (ver >= num_vr_values)
694 return CONST_CAST (value_range_t *, &vr_const_varying);
696 vr = vr_value[ver];
697 if (vr)
698 return vr;
700 /* After propagation finished do not allocate new value-ranges. */
701 if (values_propagated)
702 return CONST_CAST (value_range_t *, &vr_const_varying);
704 /* Create a default value range. */
705 vr_value[ver] = vr = XCNEW (value_range_t);
707 /* Defer allocating the equivalence set. */
708 vr->equiv = NULL;
710 /* If VAR is a default definition of a parameter, the variable can
711 take any value in VAR's type. */
712 if (SSA_NAME_IS_DEFAULT_DEF (var))
714 sym = SSA_NAME_VAR (var);
715 if (TREE_CODE (sym) == PARM_DECL)
717 /* Try to use the "nonnull" attribute to create ~[0, 0]
718 anti-ranges for pointers. Note that this is only valid with
719 default definitions of PARM_DECLs. */
720 if (POINTER_TYPE_P (TREE_TYPE (sym))
721 && nonnull_arg_p (sym))
722 set_value_range_to_nonnull (vr, TREE_TYPE (sym));
723 else
724 set_value_range_to_varying (vr);
726 else if (TREE_CODE (sym) == RESULT_DECL
727 && DECL_BY_REFERENCE (sym))
728 set_value_range_to_nonnull (vr, TREE_TYPE (sym));
731 return vr;
734 /* Return true, if VAL1 and VAL2 are equal values for VRP purposes. */
736 static inline bool
737 vrp_operand_equal_p (const_tree val1, const_tree val2)
739 if (val1 == val2)
740 return true;
741 if (!val1 || !val2 || !operand_equal_p (val1, val2, 0))
742 return false;
743 return is_overflow_infinity (val1) == is_overflow_infinity (val2);
746 /* Return true, if the bitmaps B1 and B2 are equal. */
748 static inline bool
749 vrp_bitmap_equal_p (const_bitmap b1, const_bitmap b2)
751 return (b1 == b2
752 || ((!b1 || bitmap_empty_p (b1))
753 && (!b2 || bitmap_empty_p (b2)))
754 || (b1 && b2
755 && bitmap_equal_p (b1, b2)));
758 /* Update the value range and equivalence set for variable VAR to
759 NEW_VR. Return true if NEW_VR is different from VAR's previous
760 value.
762 NOTE: This function assumes that NEW_VR is a temporary value range
763 object created for the sole purpose of updating VAR's range. The
764 storage used by the equivalence set from NEW_VR will be freed by
765 this function. Do not call update_value_range when NEW_VR
766 is the range object associated with another SSA name. */
768 static inline bool
769 update_value_range (const_tree var, value_range_t *new_vr)
771 value_range_t *old_vr;
772 bool is_new;
774 /* If there is a value-range on the SSA name from earlier analysis
775 factor that in. */
776 if (INTEGRAL_TYPE_P (TREE_TYPE (var)))
778 wide_int min, max;
779 value_range_type rtype = get_range_info (var, &min, &max);
780 if (rtype == VR_RANGE || rtype == VR_ANTI_RANGE)
782 value_range_d nr;
783 nr.type = rtype;
784 nr.min = wide_int_to_tree (TREE_TYPE (var), min);
785 nr.max = wide_int_to_tree (TREE_TYPE (var), max);
786 nr.equiv = NULL;
787 vrp_intersect_ranges (new_vr, &nr);
791 /* Update the value range, if necessary. */
792 old_vr = get_value_range (var);
793 is_new = old_vr->type != new_vr->type
794 || !vrp_operand_equal_p (old_vr->min, new_vr->min)
795 || !vrp_operand_equal_p (old_vr->max, new_vr->max)
796 || !vrp_bitmap_equal_p (old_vr->equiv, new_vr->equiv);
798 if (is_new)
800 /* Do not allow transitions up the lattice. The following
801 is slightly more awkward than just new_vr->type < old_vr->type
802 because VR_RANGE and VR_ANTI_RANGE need to be considered
803 the same. We may not have is_new when transitioning to
804 UNDEFINED. If old_vr->type is VARYING, we shouldn't be
805 called. */
806 if (new_vr->type == VR_UNDEFINED)
808 BITMAP_FREE (new_vr->equiv);
809 set_value_range_to_varying (old_vr);
810 set_value_range_to_varying (new_vr);
811 return true;
813 else
814 set_value_range (old_vr, new_vr->type, new_vr->min, new_vr->max,
815 new_vr->equiv);
818 BITMAP_FREE (new_vr->equiv);
820 return is_new;
824 /* Add VAR and VAR's equivalence set to EQUIV. This is the central
825 point where equivalence processing can be turned on/off. */
827 static void
828 add_equivalence (bitmap *equiv, const_tree var)
830 unsigned ver = SSA_NAME_VERSION (var);
831 value_range_t *vr = vr_value[ver];
833 if (*equiv == NULL)
834 *equiv = BITMAP_ALLOC (NULL);
835 bitmap_set_bit (*equiv, ver);
836 if (vr && vr->equiv)
837 bitmap_ior_into (*equiv, vr->equiv);
841 /* Return true if VR is ~[0, 0]. */
843 static inline bool
844 range_is_nonnull (value_range_t *vr)
846 return vr->type == VR_ANTI_RANGE
847 && integer_zerop (vr->min)
848 && integer_zerop (vr->max);
852 /* Return true if VR is [0, 0]. */
854 static inline bool
855 range_is_null (value_range_t *vr)
857 return vr->type == VR_RANGE
858 && integer_zerop (vr->min)
859 && integer_zerop (vr->max);
862 /* Return true if max and min of VR are INTEGER_CST. It's not necessary
863 a singleton. */
865 static inline bool
866 range_int_cst_p (value_range_t *vr)
868 return (vr->type == VR_RANGE
869 && TREE_CODE (vr->max) == INTEGER_CST
870 && TREE_CODE (vr->min) == INTEGER_CST);
873 /* Return true if VR is a INTEGER_CST singleton. */
875 static inline bool
876 range_int_cst_singleton_p (value_range_t *vr)
878 return (range_int_cst_p (vr)
879 && !is_overflow_infinity (vr->min)
880 && !is_overflow_infinity (vr->max)
881 && tree_int_cst_equal (vr->min, vr->max));
884 /* Return true if value range VR involves at least one symbol. */
886 static inline bool
887 symbolic_range_p (value_range_t *vr)
889 return (!is_gimple_min_invariant (vr->min)
890 || !is_gimple_min_invariant (vr->max));
893 /* Return the single symbol (an SSA_NAME) contained in T if any, or NULL_TREE
894 otherwise. We only handle additive operations and set NEG to true if the
895 symbol is negated and INV to the invariant part, if any. */
897 static tree
898 get_single_symbol (tree t, bool *neg, tree *inv)
900 bool neg_;
901 tree inv_;
903 if (TREE_CODE (t) == PLUS_EXPR
904 || TREE_CODE (t) == POINTER_PLUS_EXPR
905 || TREE_CODE (t) == MINUS_EXPR)
907 if (is_gimple_min_invariant (TREE_OPERAND (t, 0)))
909 neg_ = (TREE_CODE (t) == MINUS_EXPR);
910 inv_ = TREE_OPERAND (t, 0);
911 t = TREE_OPERAND (t, 1);
913 else if (is_gimple_min_invariant (TREE_OPERAND (t, 1)))
915 neg_ = false;
916 inv_ = TREE_OPERAND (t, 1);
917 t = TREE_OPERAND (t, 0);
919 else
920 return NULL_TREE;
922 else
924 neg_ = false;
925 inv_ = NULL_TREE;
928 if (TREE_CODE (t) == NEGATE_EXPR)
930 t = TREE_OPERAND (t, 0);
931 neg_ = !neg_;
934 if (TREE_CODE (t) != SSA_NAME)
935 return NULL_TREE;
937 *neg = neg_;
938 *inv = inv_;
939 return t;
942 /* The reverse operation: build a symbolic expression with TYPE
943 from symbol SYM, negated according to NEG, and invariant INV. */
945 static tree
946 build_symbolic_expr (tree type, tree sym, bool neg, tree inv)
948 const bool pointer_p = POINTER_TYPE_P (type);
949 tree t = sym;
951 if (neg)
952 t = build1 (NEGATE_EXPR, type, t);
954 if (integer_zerop (inv))
955 return t;
957 return build2 (pointer_p ? POINTER_PLUS_EXPR : PLUS_EXPR, type, t, inv);
960 /* Return true if value range VR involves exactly one symbol SYM. */
962 static bool
963 symbolic_range_based_on_p (value_range_t *vr, const_tree sym)
965 bool neg, min_has_symbol, max_has_symbol;
966 tree inv;
968 if (is_gimple_min_invariant (vr->min))
969 min_has_symbol = false;
970 else if (get_single_symbol (vr->min, &neg, &inv) == sym)
971 min_has_symbol = true;
972 else
973 return false;
975 if (is_gimple_min_invariant (vr->max))
976 max_has_symbol = false;
977 else if (get_single_symbol (vr->max, &neg, &inv) == sym)
978 max_has_symbol = true;
979 else
980 return false;
982 return (min_has_symbol || max_has_symbol);
985 /* Return true if value range VR uses an overflow infinity. */
987 static inline bool
988 overflow_infinity_range_p (value_range_t *vr)
990 return (vr->type == VR_RANGE
991 && (is_overflow_infinity (vr->min)
992 || is_overflow_infinity (vr->max)));
995 /* Return false if we can not make a valid comparison based on VR;
996 this will be the case if it uses an overflow infinity and overflow
997 is not undefined (i.e., -fno-strict-overflow is in effect).
998 Otherwise return true, and set *STRICT_OVERFLOW_P to true if VR
999 uses an overflow infinity. */
1001 static bool
1002 usable_range_p (value_range_t *vr, bool *strict_overflow_p)
1004 gcc_assert (vr->type == VR_RANGE);
1005 if (is_overflow_infinity (vr->min))
1007 *strict_overflow_p = true;
1008 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (vr->min)))
1009 return false;
1011 if (is_overflow_infinity (vr->max))
1013 *strict_overflow_p = true;
1014 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (vr->max)))
1015 return false;
1017 return true;
1021 /* Return true if the result of assignment STMT is know to be non-negative.
1022 If the return value is based on the assumption that signed overflow is
1023 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1024 *STRICT_OVERFLOW_P.*/
1026 static bool
1027 gimple_assign_nonnegative_warnv_p (gimple stmt, bool *strict_overflow_p)
1029 enum tree_code code = gimple_assign_rhs_code (stmt);
1030 switch (get_gimple_rhs_class (code))
1032 case GIMPLE_UNARY_RHS:
1033 return tree_unary_nonnegative_warnv_p (gimple_assign_rhs_code (stmt),
1034 gimple_expr_type (stmt),
1035 gimple_assign_rhs1 (stmt),
1036 strict_overflow_p);
1037 case GIMPLE_BINARY_RHS:
1038 return tree_binary_nonnegative_warnv_p (gimple_assign_rhs_code (stmt),
1039 gimple_expr_type (stmt),
1040 gimple_assign_rhs1 (stmt),
1041 gimple_assign_rhs2 (stmt),
1042 strict_overflow_p);
1043 case GIMPLE_TERNARY_RHS:
1044 return false;
1045 case GIMPLE_SINGLE_RHS:
1046 return tree_single_nonnegative_warnv_p (gimple_assign_rhs1 (stmt),
1047 strict_overflow_p);
1048 case GIMPLE_INVALID_RHS:
1049 gcc_unreachable ();
1050 default:
1051 gcc_unreachable ();
1055 /* Return true if return value of call STMT is know to be non-negative.
1056 If the return value is based on the assumption that signed overflow is
1057 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1058 *STRICT_OVERFLOW_P.*/
1060 static bool
1061 gimple_call_nonnegative_warnv_p (gimple stmt, bool *strict_overflow_p)
1063 tree arg0 = gimple_call_num_args (stmt) > 0 ?
1064 gimple_call_arg (stmt, 0) : NULL_TREE;
1065 tree arg1 = gimple_call_num_args (stmt) > 1 ?
1066 gimple_call_arg (stmt, 1) : NULL_TREE;
1068 return tree_call_nonnegative_warnv_p (gimple_expr_type (stmt),
1069 gimple_call_fndecl (stmt),
1070 arg0,
1071 arg1,
1072 strict_overflow_p);
1075 /* Return true if STMT is know to compute a non-negative value.
1076 If the return value is based on the assumption that signed overflow is
1077 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1078 *STRICT_OVERFLOW_P.*/
1080 static bool
1081 gimple_stmt_nonnegative_warnv_p (gimple stmt, bool *strict_overflow_p)
1083 switch (gimple_code (stmt))
1085 case GIMPLE_ASSIGN:
1086 return gimple_assign_nonnegative_warnv_p (stmt, strict_overflow_p);
1087 case GIMPLE_CALL:
1088 return gimple_call_nonnegative_warnv_p (stmt, strict_overflow_p);
1089 default:
1090 gcc_unreachable ();
1094 /* Return true if the result of assignment STMT is know to be non-zero.
1095 If the return value is based on the assumption that signed overflow is
1096 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1097 *STRICT_OVERFLOW_P.*/
1099 static bool
1100 gimple_assign_nonzero_warnv_p (gimple stmt, bool *strict_overflow_p)
1102 enum tree_code code = gimple_assign_rhs_code (stmt);
1103 switch (get_gimple_rhs_class (code))
1105 case GIMPLE_UNARY_RHS:
1106 return tree_unary_nonzero_warnv_p (gimple_assign_rhs_code (stmt),
1107 gimple_expr_type (stmt),
1108 gimple_assign_rhs1 (stmt),
1109 strict_overflow_p);
1110 case GIMPLE_BINARY_RHS:
1111 return tree_binary_nonzero_warnv_p (gimple_assign_rhs_code (stmt),
1112 gimple_expr_type (stmt),
1113 gimple_assign_rhs1 (stmt),
1114 gimple_assign_rhs2 (stmt),
1115 strict_overflow_p);
1116 case GIMPLE_TERNARY_RHS:
1117 return false;
1118 case GIMPLE_SINGLE_RHS:
1119 return tree_single_nonzero_warnv_p (gimple_assign_rhs1 (stmt),
1120 strict_overflow_p);
1121 case GIMPLE_INVALID_RHS:
1122 gcc_unreachable ();
1123 default:
1124 gcc_unreachable ();
1128 /* Return true if STMT is known to compute a non-zero value.
1129 If the return value is based on the assumption that signed overflow is
1130 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1131 *STRICT_OVERFLOW_P.*/
1133 static bool
1134 gimple_stmt_nonzero_warnv_p (gimple stmt, bool *strict_overflow_p)
1136 switch (gimple_code (stmt))
1138 case GIMPLE_ASSIGN:
1139 return gimple_assign_nonzero_warnv_p (stmt, strict_overflow_p);
1140 case GIMPLE_CALL:
1142 tree fndecl = gimple_call_fndecl (stmt);
1143 if (!fndecl) return false;
1144 if (flag_delete_null_pointer_checks && !flag_check_new
1145 && DECL_IS_OPERATOR_NEW (fndecl)
1146 && !TREE_NOTHROW (fndecl))
1147 return true;
1148 /* References are always non-NULL. */
1149 if (flag_delete_null_pointer_checks
1150 && TREE_CODE (TREE_TYPE (fndecl)) == REFERENCE_TYPE)
1151 return true;
1152 if (flag_delete_null_pointer_checks &&
1153 lookup_attribute ("returns_nonnull",
1154 TYPE_ATTRIBUTES (gimple_call_fntype (stmt))))
1155 return true;
1156 return gimple_alloca_call_p (stmt);
1158 default:
1159 gcc_unreachable ();
1163 /* Like tree_expr_nonzero_warnv_p, but this function uses value ranges
1164 obtained so far. */
1166 static bool
1167 vrp_stmt_computes_nonzero (gimple stmt, bool *strict_overflow_p)
1169 if (gimple_stmt_nonzero_warnv_p (stmt, strict_overflow_p))
1170 return true;
1172 /* If we have an expression of the form &X->a, then the expression
1173 is nonnull if X is nonnull. */
1174 if (is_gimple_assign (stmt)
1175 && gimple_assign_rhs_code (stmt) == ADDR_EXPR)
1177 tree expr = gimple_assign_rhs1 (stmt);
1178 tree base = get_base_address (TREE_OPERAND (expr, 0));
1180 if (base != NULL_TREE
1181 && TREE_CODE (base) == MEM_REF
1182 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
1184 value_range_t *vr = get_value_range (TREE_OPERAND (base, 0));
1185 if (range_is_nonnull (vr))
1186 return true;
1190 return false;
1193 /* Returns true if EXPR is a valid value (as expected by compare_values) --
1194 a gimple invariant, or SSA_NAME +- CST. */
1196 static bool
1197 valid_value_p (tree expr)
1199 if (TREE_CODE (expr) == SSA_NAME)
1200 return true;
1202 if (TREE_CODE (expr) == PLUS_EXPR
1203 || TREE_CODE (expr) == MINUS_EXPR)
1204 return (TREE_CODE (TREE_OPERAND (expr, 0)) == SSA_NAME
1205 && TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST);
1207 return is_gimple_min_invariant (expr);
1210 /* Return
1211 1 if VAL < VAL2
1212 0 if !(VAL < VAL2)
1213 -2 if those are incomparable. */
1214 static inline int
1215 operand_less_p (tree val, tree val2)
1217 /* LT is folded faster than GE and others. Inline the common case. */
1218 if (TREE_CODE (val) == INTEGER_CST && TREE_CODE (val2) == INTEGER_CST)
1219 return tree_int_cst_lt (val, val2);
1220 else
1222 tree tcmp;
1224 fold_defer_overflow_warnings ();
1226 tcmp = fold_binary_to_constant (LT_EXPR, boolean_type_node, val, val2);
1228 fold_undefer_and_ignore_overflow_warnings ();
1230 if (!tcmp
1231 || TREE_CODE (tcmp) != INTEGER_CST)
1232 return -2;
1234 if (!integer_zerop (tcmp))
1235 return 1;
1238 /* val >= val2, not considering overflow infinity. */
1239 if (is_negative_overflow_infinity (val))
1240 return is_negative_overflow_infinity (val2) ? 0 : 1;
1241 else if (is_positive_overflow_infinity (val2))
1242 return is_positive_overflow_infinity (val) ? 0 : 1;
1244 return 0;
1247 /* Compare two values VAL1 and VAL2. Return
1249 -2 if VAL1 and VAL2 cannot be compared at compile-time,
1250 -1 if VAL1 < VAL2,
1251 0 if VAL1 == VAL2,
1252 +1 if VAL1 > VAL2, and
1253 +2 if VAL1 != VAL2
1255 This is similar to tree_int_cst_compare but supports pointer values
1256 and values that cannot be compared at compile time.
1258 If STRICT_OVERFLOW_P is not NULL, then set *STRICT_OVERFLOW_P to
1259 true if the return value is only valid if we assume that signed
1260 overflow is undefined. */
1262 static int
1263 compare_values_warnv (tree val1, tree val2, bool *strict_overflow_p)
1265 if (val1 == val2)
1266 return 0;
1268 /* Below we rely on the fact that VAL1 and VAL2 are both pointers or
1269 both integers. */
1270 gcc_assert (POINTER_TYPE_P (TREE_TYPE (val1))
1271 == POINTER_TYPE_P (TREE_TYPE (val2)));
1273 /* Convert the two values into the same type. This is needed because
1274 sizetype causes sign extension even for unsigned types. */
1275 val2 = fold_convert (TREE_TYPE (val1), val2);
1276 STRIP_USELESS_TYPE_CONVERSION (val2);
1278 if ((TREE_CODE (val1) == SSA_NAME
1279 || (TREE_CODE (val1) == NEGATE_EXPR
1280 && TREE_CODE (TREE_OPERAND (val1, 0)) == SSA_NAME)
1281 || TREE_CODE (val1) == PLUS_EXPR
1282 || TREE_CODE (val1) == MINUS_EXPR)
1283 && (TREE_CODE (val2) == SSA_NAME
1284 || (TREE_CODE (val2) == NEGATE_EXPR
1285 && TREE_CODE (TREE_OPERAND (val2, 0)) == SSA_NAME)
1286 || TREE_CODE (val2) == PLUS_EXPR
1287 || TREE_CODE (val2) == MINUS_EXPR))
1289 tree n1, c1, n2, c2;
1290 enum tree_code code1, code2;
1292 /* If VAL1 and VAL2 are of the form '[-]NAME [+-] CST' or 'NAME',
1293 return -1 or +1 accordingly. If VAL1 and VAL2 don't use the
1294 same name, return -2. */
1295 if (TREE_CODE (val1) == SSA_NAME || TREE_CODE (val1) == NEGATE_EXPR)
1297 code1 = SSA_NAME;
1298 n1 = val1;
1299 c1 = NULL_TREE;
1301 else
1303 code1 = TREE_CODE (val1);
1304 n1 = TREE_OPERAND (val1, 0);
1305 c1 = TREE_OPERAND (val1, 1);
1306 if (tree_int_cst_sgn (c1) == -1)
1308 if (is_negative_overflow_infinity (c1))
1309 return -2;
1310 c1 = fold_unary_to_constant (NEGATE_EXPR, TREE_TYPE (c1), c1);
1311 if (!c1)
1312 return -2;
1313 code1 = code1 == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR;
1317 if (TREE_CODE (val2) == SSA_NAME || TREE_CODE (val2) == NEGATE_EXPR)
1319 code2 = SSA_NAME;
1320 n2 = val2;
1321 c2 = NULL_TREE;
1323 else
1325 code2 = TREE_CODE (val2);
1326 n2 = TREE_OPERAND (val2, 0);
1327 c2 = TREE_OPERAND (val2, 1);
1328 if (tree_int_cst_sgn (c2) == -1)
1330 if (is_negative_overflow_infinity (c2))
1331 return -2;
1332 c2 = fold_unary_to_constant (NEGATE_EXPR, TREE_TYPE (c2), c2);
1333 if (!c2)
1334 return -2;
1335 code2 = code2 == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR;
1339 /* Both values must use the same name. */
1340 if (TREE_CODE (n1) == NEGATE_EXPR && TREE_CODE (n2) == NEGATE_EXPR)
1342 n1 = TREE_OPERAND (n1, 0);
1343 n2 = TREE_OPERAND (n2, 0);
1345 if (n1 != n2)
1346 return -2;
1348 if (code1 == SSA_NAME && code2 == SSA_NAME)
1349 /* NAME == NAME */
1350 return 0;
1352 /* If overflow is defined we cannot simplify more. */
1353 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (val1)))
1354 return -2;
1356 if (strict_overflow_p != NULL
1357 && (code1 == SSA_NAME || !TREE_NO_WARNING (val1))
1358 && (code2 == SSA_NAME || !TREE_NO_WARNING (val2)))
1359 *strict_overflow_p = true;
1361 if (code1 == SSA_NAME)
1363 if (code2 == PLUS_EXPR)
1364 /* NAME < NAME + CST */
1365 return -1;
1366 else if (code2 == MINUS_EXPR)
1367 /* NAME > NAME - CST */
1368 return 1;
1370 else if (code1 == PLUS_EXPR)
1372 if (code2 == SSA_NAME)
1373 /* NAME + CST > NAME */
1374 return 1;
1375 else if (code2 == PLUS_EXPR)
1376 /* NAME + CST1 > NAME + CST2, if CST1 > CST2 */
1377 return compare_values_warnv (c1, c2, strict_overflow_p);
1378 else if (code2 == MINUS_EXPR)
1379 /* NAME + CST1 > NAME - CST2 */
1380 return 1;
1382 else if (code1 == MINUS_EXPR)
1384 if (code2 == SSA_NAME)
1385 /* NAME - CST < NAME */
1386 return -1;
1387 else if (code2 == PLUS_EXPR)
1388 /* NAME - CST1 < NAME + CST2 */
1389 return -1;
1390 else if (code2 == MINUS_EXPR)
1391 /* NAME - CST1 > NAME - CST2, if CST1 < CST2. Notice that
1392 C1 and C2 are swapped in the call to compare_values. */
1393 return compare_values_warnv (c2, c1, strict_overflow_p);
1396 gcc_unreachable ();
1399 /* We cannot compare non-constants. */
1400 if (!is_gimple_min_invariant (val1) || !is_gimple_min_invariant (val2))
1401 return -2;
1403 if (!POINTER_TYPE_P (TREE_TYPE (val1)))
1405 /* We cannot compare overflowed values, except for overflow
1406 infinities. */
1407 if (TREE_OVERFLOW (val1) || TREE_OVERFLOW (val2))
1409 if (strict_overflow_p != NULL)
1410 *strict_overflow_p = true;
1411 if (is_negative_overflow_infinity (val1))
1412 return is_negative_overflow_infinity (val2) ? 0 : -1;
1413 else if (is_negative_overflow_infinity (val2))
1414 return 1;
1415 else if (is_positive_overflow_infinity (val1))
1416 return is_positive_overflow_infinity (val2) ? 0 : 1;
1417 else if (is_positive_overflow_infinity (val2))
1418 return -1;
1419 return -2;
1422 return tree_int_cst_compare (val1, val2);
1424 else
1426 tree t;
1428 /* First see if VAL1 and VAL2 are not the same. */
1429 if (val1 == val2 || operand_equal_p (val1, val2, 0))
1430 return 0;
1432 /* If VAL1 is a lower address than VAL2, return -1. */
1433 if (operand_less_p (val1, val2) == 1)
1434 return -1;
1436 /* If VAL1 is a higher address than VAL2, return +1. */
1437 if (operand_less_p (val2, val1) == 1)
1438 return 1;
1440 /* If VAL1 is different than VAL2, return +2.
1441 For integer constants we either have already returned -1 or 1
1442 or they are equivalent. We still might succeed in proving
1443 something about non-trivial operands. */
1444 if (TREE_CODE (val1) != INTEGER_CST
1445 || TREE_CODE (val2) != INTEGER_CST)
1447 t = fold_binary_to_constant (NE_EXPR, boolean_type_node, val1, val2);
1448 if (t && integer_onep (t))
1449 return 2;
1452 return -2;
1456 /* Compare values like compare_values_warnv, but treat comparisons of
1457 nonconstants which rely on undefined overflow as incomparable. */
1459 static int
1460 compare_values (tree val1, tree val2)
1462 bool sop;
1463 int ret;
1465 sop = false;
1466 ret = compare_values_warnv (val1, val2, &sop);
1467 if (sop
1468 && (!is_gimple_min_invariant (val1) || !is_gimple_min_invariant (val2)))
1469 ret = -2;
1470 return ret;
1474 /* Return 1 if VAL is inside value range MIN <= VAL <= MAX,
1475 0 if VAL is not inside [MIN, MAX],
1476 -2 if we cannot tell either way.
1478 Benchmark compile/20001226-1.c compilation time after changing this
1479 function. */
1481 static inline int
1482 value_inside_range (tree val, tree min, tree max)
1484 int cmp1, cmp2;
1486 cmp1 = operand_less_p (val, min);
1487 if (cmp1 == -2)
1488 return -2;
1489 if (cmp1 == 1)
1490 return 0;
1492 cmp2 = operand_less_p (max, val);
1493 if (cmp2 == -2)
1494 return -2;
1496 return !cmp2;
1500 /* Return true if value ranges VR0 and VR1 have a non-empty
1501 intersection.
1503 Benchmark compile/20001226-1.c compilation time after changing this
1504 function.
1507 static inline bool
1508 value_ranges_intersect_p (value_range_t *vr0, value_range_t *vr1)
1510 /* The value ranges do not intersect if the maximum of the first range is
1511 less than the minimum of the second range or vice versa.
1512 When those relations are unknown, we can't do any better. */
1513 if (operand_less_p (vr0->max, vr1->min) != 0)
1514 return false;
1515 if (operand_less_p (vr1->max, vr0->min) != 0)
1516 return false;
1517 return true;
1521 /* Return 1 if [MIN, MAX] includes the value zero, 0 if it does not
1522 include the value zero, -2 if we cannot tell. */
1524 static inline int
1525 range_includes_zero_p (tree min, tree max)
1527 tree zero = build_int_cst (TREE_TYPE (min), 0);
1528 return value_inside_range (zero, min, max);
1531 /* Return true if *VR is know to only contain nonnegative values. */
1533 static inline bool
1534 value_range_nonnegative_p (value_range_t *vr)
1536 /* Testing for VR_ANTI_RANGE is not useful here as any anti-range
1537 which would return a useful value should be encoded as a
1538 VR_RANGE. */
1539 if (vr->type == VR_RANGE)
1541 int result = compare_values (vr->min, integer_zero_node);
1542 return (result == 0 || result == 1);
1545 return false;
1548 /* If *VR has a value rante that is a single constant value return that,
1549 otherwise return NULL_TREE. */
1551 static tree
1552 value_range_constant_singleton (value_range_t *vr)
1554 if (vr->type == VR_RANGE
1555 && operand_equal_p (vr->min, vr->max, 0)
1556 && is_gimple_min_invariant (vr->min))
1557 return vr->min;
1559 return NULL_TREE;
1562 /* If OP has a value range with a single constant value return that,
1563 otherwise return NULL_TREE. This returns OP itself if OP is a
1564 constant. */
1566 static tree
1567 op_with_constant_singleton_value_range (tree op)
1569 if (is_gimple_min_invariant (op))
1570 return op;
1572 if (TREE_CODE (op) != SSA_NAME)
1573 return NULL_TREE;
1575 return value_range_constant_singleton (get_value_range (op));
1578 /* Return true if op is in a boolean [0, 1] value-range. */
1580 static bool
1581 op_with_boolean_value_range_p (tree op)
1583 value_range_t *vr;
1585 if (TYPE_PRECISION (TREE_TYPE (op)) == 1)
1586 return true;
1588 if (integer_zerop (op)
1589 || integer_onep (op))
1590 return true;
1592 if (TREE_CODE (op) != SSA_NAME)
1593 return false;
1595 vr = get_value_range (op);
1596 return (vr->type == VR_RANGE
1597 && integer_zerop (vr->min)
1598 && integer_onep (vr->max));
1601 /* Extract value range information from an ASSERT_EXPR EXPR and store
1602 it in *VR_P. */
1604 static void
1605 extract_range_from_assert (value_range_t *vr_p, tree expr)
1607 tree var, cond, limit, min, max, type;
1608 value_range_t *limit_vr;
1609 enum tree_code cond_code;
1611 var = ASSERT_EXPR_VAR (expr);
1612 cond = ASSERT_EXPR_COND (expr);
1614 gcc_assert (COMPARISON_CLASS_P (cond));
1616 /* Find VAR in the ASSERT_EXPR conditional. */
1617 if (var == TREE_OPERAND (cond, 0)
1618 || TREE_CODE (TREE_OPERAND (cond, 0)) == PLUS_EXPR
1619 || TREE_CODE (TREE_OPERAND (cond, 0)) == NOP_EXPR)
1621 /* If the predicate is of the form VAR COMP LIMIT, then we just
1622 take LIMIT from the RHS and use the same comparison code. */
1623 cond_code = TREE_CODE (cond);
1624 limit = TREE_OPERAND (cond, 1);
1625 cond = TREE_OPERAND (cond, 0);
1627 else
1629 /* If the predicate is of the form LIMIT COMP VAR, then we need
1630 to flip around the comparison code to create the proper range
1631 for VAR. */
1632 cond_code = swap_tree_comparison (TREE_CODE (cond));
1633 limit = TREE_OPERAND (cond, 0);
1634 cond = TREE_OPERAND (cond, 1);
1637 limit = avoid_overflow_infinity (limit);
1639 type = TREE_TYPE (var);
1640 gcc_assert (limit != var);
1642 /* For pointer arithmetic, we only keep track of pointer equality
1643 and inequality. */
1644 if (POINTER_TYPE_P (type) && cond_code != NE_EXPR && cond_code != EQ_EXPR)
1646 set_value_range_to_varying (vr_p);
1647 return;
1650 /* If LIMIT is another SSA name and LIMIT has a range of its own,
1651 try to use LIMIT's range to avoid creating symbolic ranges
1652 unnecessarily. */
1653 limit_vr = (TREE_CODE (limit) == SSA_NAME) ? get_value_range (limit) : NULL;
1655 /* LIMIT's range is only interesting if it has any useful information. */
1656 if (limit_vr
1657 && (limit_vr->type == VR_UNDEFINED
1658 || limit_vr->type == VR_VARYING
1659 || symbolic_range_p (limit_vr)))
1660 limit_vr = NULL;
1662 /* Initially, the new range has the same set of equivalences of
1663 VAR's range. This will be revised before returning the final
1664 value. Since assertions may be chained via mutually exclusive
1665 predicates, we will need to trim the set of equivalences before
1666 we are done. */
1667 gcc_assert (vr_p->equiv == NULL);
1668 add_equivalence (&vr_p->equiv, var);
1670 /* Extract a new range based on the asserted comparison for VAR and
1671 LIMIT's value range. Notice that if LIMIT has an anti-range, we
1672 will only use it for equality comparisons (EQ_EXPR). For any
1673 other kind of assertion, we cannot derive a range from LIMIT's
1674 anti-range that can be used to describe the new range. For
1675 instance, ASSERT_EXPR <x_2, x_2 <= b_4>. If b_4 is ~[2, 10],
1676 then b_4 takes on the ranges [-INF, 1] and [11, +INF]. There is
1677 no single range for x_2 that could describe LE_EXPR, so we might
1678 as well build the range [b_4, +INF] for it.
1679 One special case we handle is extracting a range from a
1680 range test encoded as (unsigned)var + CST <= limit. */
1681 if (TREE_CODE (cond) == NOP_EXPR
1682 || TREE_CODE (cond) == PLUS_EXPR)
1684 if (TREE_CODE (cond) == PLUS_EXPR)
1686 min = fold_build1 (NEGATE_EXPR, TREE_TYPE (TREE_OPERAND (cond, 1)),
1687 TREE_OPERAND (cond, 1));
1688 max = int_const_binop (PLUS_EXPR, limit, min);
1689 cond = TREE_OPERAND (cond, 0);
1691 else
1693 min = build_int_cst (TREE_TYPE (var), 0);
1694 max = limit;
1697 /* Make sure to not set TREE_OVERFLOW on the final type
1698 conversion. We are willingly interpreting large positive
1699 unsigned values as negative signed values here. */
1700 min = force_fit_type (TREE_TYPE (var), wi::to_widest (min), 0, false);
1701 max = force_fit_type (TREE_TYPE (var), wi::to_widest (max), 0, false);
1703 /* We can transform a max, min range to an anti-range or
1704 vice-versa. Use set_and_canonicalize_value_range which does
1705 this for us. */
1706 if (cond_code == LE_EXPR)
1707 set_and_canonicalize_value_range (vr_p, VR_RANGE,
1708 min, max, vr_p->equiv);
1709 else if (cond_code == GT_EXPR)
1710 set_and_canonicalize_value_range (vr_p, VR_ANTI_RANGE,
1711 min, max, vr_p->equiv);
1712 else
1713 gcc_unreachable ();
1715 else if (cond_code == EQ_EXPR)
1717 enum value_range_type range_type;
1719 if (limit_vr)
1721 range_type = limit_vr->type;
1722 min = limit_vr->min;
1723 max = limit_vr->max;
1725 else
1727 range_type = VR_RANGE;
1728 min = limit;
1729 max = limit;
1732 set_value_range (vr_p, range_type, min, max, vr_p->equiv);
1734 /* When asserting the equality VAR == LIMIT and LIMIT is another
1735 SSA name, the new range will also inherit the equivalence set
1736 from LIMIT. */
1737 if (TREE_CODE (limit) == SSA_NAME)
1738 add_equivalence (&vr_p->equiv, limit);
1740 else if (cond_code == NE_EXPR)
1742 /* As described above, when LIMIT's range is an anti-range and
1743 this assertion is an inequality (NE_EXPR), then we cannot
1744 derive anything from the anti-range. For instance, if
1745 LIMIT's range was ~[0, 0], the assertion 'VAR != LIMIT' does
1746 not imply that VAR's range is [0, 0]. So, in the case of
1747 anti-ranges, we just assert the inequality using LIMIT and
1748 not its anti-range.
1750 If LIMIT_VR is a range, we can only use it to build a new
1751 anti-range if LIMIT_VR is a single-valued range. For
1752 instance, if LIMIT_VR is [0, 1], the predicate
1753 VAR != [0, 1] does not mean that VAR's range is ~[0, 1].
1754 Rather, it means that for value 0 VAR should be ~[0, 0]
1755 and for value 1, VAR should be ~[1, 1]. We cannot
1756 represent these ranges.
1758 The only situation in which we can build a valid
1759 anti-range is when LIMIT_VR is a single-valued range
1760 (i.e., LIMIT_VR->MIN == LIMIT_VR->MAX). In that case,
1761 build the anti-range ~[LIMIT_VR->MIN, LIMIT_VR->MAX]. */
1762 if (limit_vr
1763 && limit_vr->type == VR_RANGE
1764 && compare_values (limit_vr->min, limit_vr->max) == 0)
1766 min = limit_vr->min;
1767 max = limit_vr->max;
1769 else
1771 /* In any other case, we cannot use LIMIT's range to build a
1772 valid anti-range. */
1773 min = max = limit;
1776 /* If MIN and MAX cover the whole range for their type, then
1777 just use the original LIMIT. */
1778 if (INTEGRAL_TYPE_P (type)
1779 && vrp_val_is_min (min)
1780 && vrp_val_is_max (max))
1781 min = max = limit;
1783 set_and_canonicalize_value_range (vr_p, VR_ANTI_RANGE,
1784 min, max, vr_p->equiv);
1786 else if (cond_code == LE_EXPR || cond_code == LT_EXPR)
1788 min = TYPE_MIN_VALUE (type);
1790 if (limit_vr == NULL || limit_vr->type == VR_ANTI_RANGE)
1791 max = limit;
1792 else
1794 /* If LIMIT_VR is of the form [N1, N2], we need to build the
1795 range [MIN, N2] for LE_EXPR and [MIN, N2 - 1] for
1796 LT_EXPR. */
1797 max = limit_vr->max;
1800 /* If the maximum value forces us to be out of bounds, simply punt.
1801 It would be pointless to try and do anything more since this
1802 all should be optimized away above us. */
1803 if ((cond_code == LT_EXPR
1804 && compare_values (max, min) == 0)
1805 || is_overflow_infinity (max))
1806 set_value_range_to_varying (vr_p);
1807 else
1809 /* For LT_EXPR, we create the range [MIN, MAX - 1]. */
1810 if (cond_code == LT_EXPR)
1812 if (TYPE_PRECISION (TREE_TYPE (max)) == 1
1813 && !TYPE_UNSIGNED (TREE_TYPE (max)))
1814 max = fold_build2 (PLUS_EXPR, TREE_TYPE (max), max,
1815 build_int_cst (TREE_TYPE (max), -1));
1816 else
1817 max = fold_build2 (MINUS_EXPR, TREE_TYPE (max), max,
1818 build_int_cst (TREE_TYPE (max), 1));
1819 if (EXPR_P (max))
1820 TREE_NO_WARNING (max) = 1;
1823 set_value_range (vr_p, VR_RANGE, min, max, vr_p->equiv);
1826 else if (cond_code == GE_EXPR || cond_code == GT_EXPR)
1828 max = TYPE_MAX_VALUE (type);
1830 if (limit_vr == NULL || limit_vr->type == VR_ANTI_RANGE)
1831 min = limit;
1832 else
1834 /* If LIMIT_VR is of the form [N1, N2], we need to build the
1835 range [N1, MAX] for GE_EXPR and [N1 + 1, MAX] for
1836 GT_EXPR. */
1837 min = limit_vr->min;
1840 /* If the minimum value forces us to be out of bounds, simply punt.
1841 It would be pointless to try and do anything more since this
1842 all should be optimized away above us. */
1843 if ((cond_code == GT_EXPR
1844 && compare_values (min, max) == 0)
1845 || is_overflow_infinity (min))
1846 set_value_range_to_varying (vr_p);
1847 else
1849 /* For GT_EXPR, we create the range [MIN + 1, MAX]. */
1850 if (cond_code == GT_EXPR)
1852 if (TYPE_PRECISION (TREE_TYPE (min)) == 1
1853 && !TYPE_UNSIGNED (TREE_TYPE (min)))
1854 min = fold_build2 (MINUS_EXPR, TREE_TYPE (min), min,
1855 build_int_cst (TREE_TYPE (min), -1));
1856 else
1857 min = fold_build2 (PLUS_EXPR, TREE_TYPE (min), min,
1858 build_int_cst (TREE_TYPE (min), 1));
1859 if (EXPR_P (min))
1860 TREE_NO_WARNING (min) = 1;
1863 set_value_range (vr_p, VR_RANGE, min, max, vr_p->equiv);
1866 else
1867 gcc_unreachable ();
1869 /* Finally intersect the new range with what we already know about var. */
1870 vrp_intersect_ranges (vr_p, get_value_range (var));
1874 /* Extract range information from SSA name VAR and store it in VR. If
1875 VAR has an interesting range, use it. Otherwise, create the
1876 range [VAR, VAR] and return it. This is useful in situations where
1877 we may have conditionals testing values of VARYING names. For
1878 instance,
1880 x_3 = y_5;
1881 if (x_3 > y_5)
1884 Even if y_5 is deemed VARYING, we can determine that x_3 > y_5 is
1885 always false. */
1887 static void
1888 extract_range_from_ssa_name (value_range_t *vr, tree var)
1890 value_range_t *var_vr = get_value_range (var);
1892 if (var_vr->type != VR_VARYING)
1893 copy_value_range (vr, var_vr);
1894 else
1895 set_value_range (vr, VR_RANGE, var, var, NULL);
1897 add_equivalence (&vr->equiv, var);
1901 /* Wrapper around int_const_binop. If the operation overflows and we
1902 are not using wrapping arithmetic, then adjust the result to be
1903 -INF or +INF depending on CODE, VAL1 and VAL2. This can return
1904 NULL_TREE if we need to use an overflow infinity representation but
1905 the type does not support it. */
1907 static tree
1908 vrp_int_const_binop (enum tree_code code, tree val1, tree val2)
1910 tree res;
1912 res = int_const_binop (code, val1, val2);
1914 /* If we are using unsigned arithmetic, operate symbolically
1915 on -INF and +INF as int_const_binop only handles signed overflow. */
1916 if (TYPE_UNSIGNED (TREE_TYPE (val1)))
1918 int checkz = compare_values (res, val1);
1919 bool overflow = false;
1921 /* Ensure that res = val1 [+*] val2 >= val1
1922 or that res = val1 - val2 <= val1. */
1923 if ((code == PLUS_EXPR
1924 && !(checkz == 1 || checkz == 0))
1925 || (code == MINUS_EXPR
1926 && !(checkz == 0 || checkz == -1)))
1928 overflow = true;
1930 /* Checking for multiplication overflow is done by dividing the
1931 output of the multiplication by the first input of the
1932 multiplication. If the result of that division operation is
1933 not equal to the second input of the multiplication, then the
1934 multiplication overflowed. */
1935 else if (code == MULT_EXPR && !integer_zerop (val1))
1937 tree tmp = int_const_binop (TRUNC_DIV_EXPR,
1938 res,
1939 val1);
1940 int check = compare_values (tmp, val2);
1942 if (check != 0)
1943 overflow = true;
1946 if (overflow)
1948 res = copy_node (res);
1949 TREE_OVERFLOW (res) = 1;
1953 else if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (val1)))
1954 /* If the singed operation wraps then int_const_binop has done
1955 everything we want. */
1957 /* Signed division of -1/0 overflows and by the time it gets here
1958 returns NULL_TREE. */
1959 else if (!res)
1960 return NULL_TREE;
1961 else if ((TREE_OVERFLOW (res)
1962 && !TREE_OVERFLOW (val1)
1963 && !TREE_OVERFLOW (val2))
1964 || is_overflow_infinity (val1)
1965 || is_overflow_infinity (val2))
1967 /* If the operation overflowed but neither VAL1 nor VAL2 are
1968 overflown, return -INF or +INF depending on the operation
1969 and the combination of signs of the operands. */
1970 int sgn1 = tree_int_cst_sgn (val1);
1971 int sgn2 = tree_int_cst_sgn (val2);
1973 if (needs_overflow_infinity (TREE_TYPE (res))
1974 && !supports_overflow_infinity (TREE_TYPE (res)))
1975 return NULL_TREE;
1977 /* We have to punt on adding infinities of different signs,
1978 since we can't tell what the sign of the result should be.
1979 Likewise for subtracting infinities of the same sign. */
1980 if (((code == PLUS_EXPR && sgn1 != sgn2)
1981 || (code == MINUS_EXPR && sgn1 == sgn2))
1982 && is_overflow_infinity (val1)
1983 && is_overflow_infinity (val2))
1984 return NULL_TREE;
1986 /* Don't try to handle division or shifting of infinities. */
1987 if ((code == TRUNC_DIV_EXPR
1988 || code == FLOOR_DIV_EXPR
1989 || code == CEIL_DIV_EXPR
1990 || code == EXACT_DIV_EXPR
1991 || code == ROUND_DIV_EXPR
1992 || code == RSHIFT_EXPR)
1993 && (is_overflow_infinity (val1)
1994 || is_overflow_infinity (val2)))
1995 return NULL_TREE;
1997 /* Notice that we only need to handle the restricted set of
1998 operations handled by extract_range_from_binary_expr.
1999 Among them, only multiplication, addition and subtraction
2000 can yield overflow without overflown operands because we
2001 are working with integral types only... except in the
2002 case VAL1 = -INF and VAL2 = -1 which overflows to +INF
2003 for division too. */
2005 /* For multiplication, the sign of the overflow is given
2006 by the comparison of the signs of the operands. */
2007 if ((code == MULT_EXPR && sgn1 == sgn2)
2008 /* For addition, the operands must be of the same sign
2009 to yield an overflow. Its sign is therefore that
2010 of one of the operands, for example the first. For
2011 infinite operands X + -INF is negative, not positive. */
2012 || (code == PLUS_EXPR
2013 && (sgn1 >= 0
2014 ? !is_negative_overflow_infinity (val2)
2015 : is_positive_overflow_infinity (val2)))
2016 /* For subtraction, non-infinite operands must be of
2017 different signs to yield an overflow. Its sign is
2018 therefore that of the first operand or the opposite of
2019 that of the second operand. A first operand of 0 counts
2020 as positive here, for the corner case 0 - (-INF), which
2021 overflows, but must yield +INF. For infinite operands 0
2022 - INF is negative, not positive. */
2023 || (code == MINUS_EXPR
2024 && (sgn1 >= 0
2025 ? !is_positive_overflow_infinity (val2)
2026 : is_negative_overflow_infinity (val2)))
2027 /* We only get in here with positive shift count, so the
2028 overflow direction is the same as the sign of val1.
2029 Actually rshift does not overflow at all, but we only
2030 handle the case of shifting overflowed -INF and +INF. */
2031 || (code == RSHIFT_EXPR
2032 && sgn1 >= 0)
2033 /* For division, the only case is -INF / -1 = +INF. */
2034 || code == TRUNC_DIV_EXPR
2035 || code == FLOOR_DIV_EXPR
2036 || code == CEIL_DIV_EXPR
2037 || code == EXACT_DIV_EXPR
2038 || code == ROUND_DIV_EXPR)
2039 return (needs_overflow_infinity (TREE_TYPE (res))
2040 ? positive_overflow_infinity (TREE_TYPE (res))
2041 : TYPE_MAX_VALUE (TREE_TYPE (res)));
2042 else
2043 return (needs_overflow_infinity (TREE_TYPE (res))
2044 ? negative_overflow_infinity (TREE_TYPE (res))
2045 : TYPE_MIN_VALUE (TREE_TYPE (res)));
2048 return res;
2052 /* For range VR compute two wide_int bitmasks. In *MAY_BE_NONZERO
2053 bitmask if some bit is unset, it means for all numbers in the range
2054 the bit is 0, otherwise it might be 0 or 1. In *MUST_BE_NONZERO
2055 bitmask if some bit is set, it means for all numbers in the range
2056 the bit is 1, otherwise it might be 0 or 1. */
2058 static bool
2059 zero_nonzero_bits_from_vr (const tree expr_type,
2060 value_range_t *vr,
2061 wide_int *may_be_nonzero,
2062 wide_int *must_be_nonzero)
2064 *may_be_nonzero = wi::minus_one (TYPE_PRECISION (expr_type));
2065 *must_be_nonzero = wi::zero (TYPE_PRECISION (expr_type));
2066 if (!range_int_cst_p (vr)
2067 || is_overflow_infinity (vr->min)
2068 || is_overflow_infinity (vr->max))
2069 return false;
2071 if (range_int_cst_singleton_p (vr))
2073 *may_be_nonzero = vr->min;
2074 *must_be_nonzero = *may_be_nonzero;
2076 else if (tree_int_cst_sgn (vr->min) >= 0
2077 || tree_int_cst_sgn (vr->max) < 0)
2079 wide_int xor_mask = wi::bit_xor (vr->min, vr->max);
2080 *may_be_nonzero = wi::bit_or (vr->min, vr->max);
2081 *must_be_nonzero = wi::bit_and (vr->min, vr->max);
2082 if (xor_mask != 0)
2084 wide_int mask = wi::mask (wi::floor_log2 (xor_mask), false,
2085 may_be_nonzero->get_precision ());
2086 *may_be_nonzero = *may_be_nonzero | mask;
2087 *must_be_nonzero = must_be_nonzero->and_not (mask);
2091 return true;
2094 /* Create two value-ranges in *VR0 and *VR1 from the anti-range *AR
2095 so that *VR0 U *VR1 == *AR. Returns true if that is possible,
2096 false otherwise. If *AR can be represented with a single range
2097 *VR1 will be VR_UNDEFINED. */
2099 static bool
2100 ranges_from_anti_range (value_range_t *ar,
2101 value_range_t *vr0, value_range_t *vr1)
2103 tree type = TREE_TYPE (ar->min);
2105 vr0->type = VR_UNDEFINED;
2106 vr1->type = VR_UNDEFINED;
2108 if (ar->type != VR_ANTI_RANGE
2109 || TREE_CODE (ar->min) != INTEGER_CST
2110 || TREE_CODE (ar->max) != INTEGER_CST
2111 || !vrp_val_min (type)
2112 || !vrp_val_max (type))
2113 return false;
2115 if (!vrp_val_is_min (ar->min))
2117 vr0->type = VR_RANGE;
2118 vr0->min = vrp_val_min (type);
2119 vr0->max = wide_int_to_tree (type, wi::sub (ar->min, 1));
2121 if (!vrp_val_is_max (ar->max))
2123 vr1->type = VR_RANGE;
2124 vr1->min = wide_int_to_tree (type, wi::add (ar->max, 1));
2125 vr1->max = vrp_val_max (type);
2127 if (vr0->type == VR_UNDEFINED)
2129 *vr0 = *vr1;
2130 vr1->type = VR_UNDEFINED;
2133 return vr0->type != VR_UNDEFINED;
2136 /* Helper to extract a value-range *VR for a multiplicative operation
2137 *VR0 CODE *VR1. */
2139 static void
2140 extract_range_from_multiplicative_op_1 (value_range_t *vr,
2141 enum tree_code code,
2142 value_range_t *vr0, value_range_t *vr1)
2144 enum value_range_type type;
2145 tree val[4];
2146 size_t i;
2147 tree min, max;
2148 bool sop;
2149 int cmp;
2151 /* Multiplications, divisions and shifts are a bit tricky to handle,
2152 depending on the mix of signs we have in the two ranges, we
2153 need to operate on different values to get the minimum and
2154 maximum values for the new range. One approach is to figure
2155 out all the variations of range combinations and do the
2156 operations.
2158 However, this involves several calls to compare_values and it
2159 is pretty convoluted. It's simpler to do the 4 operations
2160 (MIN0 OP MIN1, MIN0 OP MAX1, MAX0 OP MIN1 and MAX0 OP MAX0 OP
2161 MAX1) and then figure the smallest and largest values to form
2162 the new range. */
2163 gcc_assert (code == MULT_EXPR
2164 || code == TRUNC_DIV_EXPR
2165 || code == FLOOR_DIV_EXPR
2166 || code == CEIL_DIV_EXPR
2167 || code == EXACT_DIV_EXPR
2168 || code == ROUND_DIV_EXPR
2169 || code == RSHIFT_EXPR
2170 || code == LSHIFT_EXPR);
2171 gcc_assert ((vr0->type == VR_RANGE
2172 || (code == MULT_EXPR && vr0->type == VR_ANTI_RANGE))
2173 && vr0->type == vr1->type);
2175 type = vr0->type;
2177 /* Compute the 4 cross operations. */
2178 sop = false;
2179 val[0] = vrp_int_const_binop (code, vr0->min, vr1->min);
2180 if (val[0] == NULL_TREE)
2181 sop = true;
2183 if (vr1->max == vr1->min)
2184 val[1] = NULL_TREE;
2185 else
2187 val[1] = vrp_int_const_binop (code, vr0->min, vr1->max);
2188 if (val[1] == NULL_TREE)
2189 sop = true;
2192 if (vr0->max == vr0->min)
2193 val[2] = NULL_TREE;
2194 else
2196 val[2] = vrp_int_const_binop (code, vr0->max, vr1->min);
2197 if (val[2] == NULL_TREE)
2198 sop = true;
2201 if (vr0->min == vr0->max || vr1->min == vr1->max)
2202 val[3] = NULL_TREE;
2203 else
2205 val[3] = vrp_int_const_binop (code, vr0->max, vr1->max);
2206 if (val[3] == NULL_TREE)
2207 sop = true;
2210 if (sop)
2212 set_value_range_to_varying (vr);
2213 return;
2216 /* Set MIN to the minimum of VAL[i] and MAX to the maximum
2217 of VAL[i]. */
2218 min = val[0];
2219 max = val[0];
2220 for (i = 1; i < 4; i++)
2222 if (!is_gimple_min_invariant (min)
2223 || (TREE_OVERFLOW (min) && !is_overflow_infinity (min))
2224 || !is_gimple_min_invariant (max)
2225 || (TREE_OVERFLOW (max) && !is_overflow_infinity (max)))
2226 break;
2228 if (val[i])
2230 if (!is_gimple_min_invariant (val[i])
2231 || (TREE_OVERFLOW (val[i])
2232 && !is_overflow_infinity (val[i])))
2234 /* If we found an overflowed value, set MIN and MAX
2235 to it so that we set the resulting range to
2236 VARYING. */
2237 min = max = val[i];
2238 break;
2241 if (compare_values (val[i], min) == -1)
2242 min = val[i];
2244 if (compare_values (val[i], max) == 1)
2245 max = val[i];
2249 /* If either MIN or MAX overflowed, then set the resulting range to
2250 VARYING. But we do accept an overflow infinity
2251 representation. */
2252 if (min == NULL_TREE
2253 || !is_gimple_min_invariant (min)
2254 || (TREE_OVERFLOW (min) && !is_overflow_infinity (min))
2255 || max == NULL_TREE
2256 || !is_gimple_min_invariant (max)
2257 || (TREE_OVERFLOW (max) && !is_overflow_infinity (max)))
2259 set_value_range_to_varying (vr);
2260 return;
2263 /* We punt if:
2264 1) [-INF, +INF]
2265 2) [-INF, +-INF(OVF)]
2266 3) [+-INF(OVF), +INF]
2267 4) [+-INF(OVF), +-INF(OVF)]
2268 We learn nothing when we have INF and INF(OVF) on both sides.
2269 Note that we do accept [-INF, -INF] and [+INF, +INF] without
2270 overflow. */
2271 if ((vrp_val_is_min (min) || is_overflow_infinity (min))
2272 && (vrp_val_is_max (max) || is_overflow_infinity (max)))
2274 set_value_range_to_varying (vr);
2275 return;
2278 cmp = compare_values (min, max);
2279 if (cmp == -2 || cmp == 1)
2281 /* If the new range has its limits swapped around (MIN > MAX),
2282 then the operation caused one of them to wrap around, mark
2283 the new range VARYING. */
2284 set_value_range_to_varying (vr);
2286 else
2287 set_value_range (vr, type, min, max, NULL);
2290 /* Extract range information from a binary operation CODE based on
2291 the ranges of each of its operands *VR0 and *VR1 with resulting
2292 type EXPR_TYPE. The resulting range is stored in *VR. */
2294 static void
2295 extract_range_from_binary_expr_1 (value_range_t *vr,
2296 enum tree_code code, tree expr_type,
2297 value_range_t *vr0_, value_range_t *vr1_)
2299 value_range_t vr0 = *vr0_, vr1 = *vr1_;
2300 value_range_t vrtem0 = VR_INITIALIZER, vrtem1 = VR_INITIALIZER;
2301 enum value_range_type type;
2302 tree min = NULL_TREE, max = NULL_TREE;
2303 int cmp;
2305 if (!INTEGRAL_TYPE_P (expr_type)
2306 && !POINTER_TYPE_P (expr_type))
2308 set_value_range_to_varying (vr);
2309 return;
2312 /* Not all binary expressions can be applied to ranges in a
2313 meaningful way. Handle only arithmetic operations. */
2314 if (code != PLUS_EXPR
2315 && code != MINUS_EXPR
2316 && code != POINTER_PLUS_EXPR
2317 && code != MULT_EXPR
2318 && code != TRUNC_DIV_EXPR
2319 && code != FLOOR_DIV_EXPR
2320 && code != CEIL_DIV_EXPR
2321 && code != EXACT_DIV_EXPR
2322 && code != ROUND_DIV_EXPR
2323 && code != TRUNC_MOD_EXPR
2324 && code != RSHIFT_EXPR
2325 && code != LSHIFT_EXPR
2326 && code != MIN_EXPR
2327 && code != MAX_EXPR
2328 && code != BIT_AND_EXPR
2329 && code != BIT_IOR_EXPR
2330 && code != BIT_XOR_EXPR)
2332 set_value_range_to_varying (vr);
2333 return;
2336 /* If both ranges are UNDEFINED, so is the result. */
2337 if (vr0.type == VR_UNDEFINED && vr1.type == VR_UNDEFINED)
2339 set_value_range_to_undefined (vr);
2340 return;
2342 /* If one of the ranges is UNDEFINED drop it to VARYING for the following
2343 code. At some point we may want to special-case operations that
2344 have UNDEFINED result for all or some value-ranges of the not UNDEFINED
2345 operand. */
2346 else if (vr0.type == VR_UNDEFINED)
2347 set_value_range_to_varying (&vr0);
2348 else if (vr1.type == VR_UNDEFINED)
2349 set_value_range_to_varying (&vr1);
2351 /* Now canonicalize anti-ranges to ranges when they are not symbolic
2352 and express ~[] op X as ([]' op X) U ([]'' op X). */
2353 if (vr0.type == VR_ANTI_RANGE
2354 && ranges_from_anti_range (&vr0, &vrtem0, &vrtem1))
2356 extract_range_from_binary_expr_1 (vr, code, expr_type, &vrtem0, vr1_);
2357 if (vrtem1.type != VR_UNDEFINED)
2359 value_range_t vrres = VR_INITIALIZER;
2360 extract_range_from_binary_expr_1 (&vrres, code, expr_type,
2361 &vrtem1, vr1_);
2362 vrp_meet (vr, &vrres);
2364 return;
2366 /* Likewise for X op ~[]. */
2367 if (vr1.type == VR_ANTI_RANGE
2368 && ranges_from_anti_range (&vr1, &vrtem0, &vrtem1))
2370 extract_range_from_binary_expr_1 (vr, code, expr_type, vr0_, &vrtem0);
2371 if (vrtem1.type != VR_UNDEFINED)
2373 value_range_t vrres = VR_INITIALIZER;
2374 extract_range_from_binary_expr_1 (&vrres, code, expr_type,
2375 vr0_, &vrtem1);
2376 vrp_meet (vr, &vrres);
2378 return;
2381 /* The type of the resulting value range defaults to VR0.TYPE. */
2382 type = vr0.type;
2384 /* Refuse to operate on VARYING ranges, ranges of different kinds
2385 and symbolic ranges. As an exception, we allow BIT_{AND,IOR}
2386 because we may be able to derive a useful range even if one of
2387 the operands is VR_VARYING or symbolic range. Similarly for
2388 divisions, MIN/MAX and PLUS/MINUS.
2390 TODO, we may be able to derive anti-ranges in some cases. */
2391 if (code != BIT_AND_EXPR
2392 && code != BIT_IOR_EXPR
2393 && code != TRUNC_DIV_EXPR
2394 && code != FLOOR_DIV_EXPR
2395 && code != CEIL_DIV_EXPR
2396 && code != EXACT_DIV_EXPR
2397 && code != ROUND_DIV_EXPR
2398 && code != TRUNC_MOD_EXPR
2399 && code != MIN_EXPR
2400 && code != MAX_EXPR
2401 && code != PLUS_EXPR
2402 && code != MINUS_EXPR
2403 && code != RSHIFT_EXPR
2404 && (vr0.type == VR_VARYING
2405 || vr1.type == VR_VARYING
2406 || vr0.type != vr1.type
2407 || symbolic_range_p (&vr0)
2408 || symbolic_range_p (&vr1)))
2410 set_value_range_to_varying (vr);
2411 return;
2414 /* Now evaluate the expression to determine the new range. */
2415 if (POINTER_TYPE_P (expr_type))
2417 if (code == MIN_EXPR || code == MAX_EXPR)
2419 /* For MIN/MAX expressions with pointers, we only care about
2420 nullness, if both are non null, then the result is nonnull.
2421 If both are null, then the result is null. Otherwise they
2422 are varying. */
2423 if (range_is_nonnull (&vr0) && range_is_nonnull (&vr1))
2424 set_value_range_to_nonnull (vr, expr_type);
2425 else if (range_is_null (&vr0) && range_is_null (&vr1))
2426 set_value_range_to_null (vr, expr_type);
2427 else
2428 set_value_range_to_varying (vr);
2430 else if (code == POINTER_PLUS_EXPR)
2432 /* For pointer types, we are really only interested in asserting
2433 whether the expression evaluates to non-NULL. */
2434 if (range_is_nonnull (&vr0) || range_is_nonnull (&vr1))
2435 set_value_range_to_nonnull (vr, expr_type);
2436 else if (range_is_null (&vr0) && range_is_null (&vr1))
2437 set_value_range_to_null (vr, expr_type);
2438 else
2439 set_value_range_to_varying (vr);
2441 else if (code == BIT_AND_EXPR)
2443 /* For pointer types, we are really only interested in asserting
2444 whether the expression evaluates to non-NULL. */
2445 if (range_is_nonnull (&vr0) && range_is_nonnull (&vr1))
2446 set_value_range_to_nonnull (vr, expr_type);
2447 else if (range_is_null (&vr0) || range_is_null (&vr1))
2448 set_value_range_to_null (vr, expr_type);
2449 else
2450 set_value_range_to_varying (vr);
2452 else
2453 set_value_range_to_varying (vr);
2455 return;
2458 /* For integer ranges, apply the operation to each end of the
2459 range and see what we end up with. */
2460 if (code == PLUS_EXPR || code == MINUS_EXPR)
2462 const bool minus_p = (code == MINUS_EXPR);
2463 tree min_op0 = vr0.min;
2464 tree min_op1 = minus_p ? vr1.max : vr1.min;
2465 tree max_op0 = vr0.max;
2466 tree max_op1 = minus_p ? vr1.min : vr1.max;
2467 tree sym_min_op0 = NULL_TREE;
2468 tree sym_min_op1 = NULL_TREE;
2469 tree sym_max_op0 = NULL_TREE;
2470 tree sym_max_op1 = NULL_TREE;
2471 bool neg_min_op0, neg_min_op1, neg_max_op0, neg_max_op1;
2473 /* If we have a PLUS or MINUS with two VR_RANGEs, either constant or
2474 single-symbolic ranges, try to compute the precise resulting range,
2475 but only if we know that this resulting range will also be constant
2476 or single-symbolic. */
2477 if (vr0.type == VR_RANGE && vr1.type == VR_RANGE
2478 && (TREE_CODE (min_op0) == INTEGER_CST
2479 || (sym_min_op0
2480 = get_single_symbol (min_op0, &neg_min_op0, &min_op0)))
2481 && (TREE_CODE (min_op1) == INTEGER_CST
2482 || (sym_min_op1
2483 = get_single_symbol (min_op1, &neg_min_op1, &min_op1)))
2484 && (!(sym_min_op0 && sym_min_op1)
2485 || (sym_min_op0 == sym_min_op1
2486 && neg_min_op0 == (minus_p ? neg_min_op1 : !neg_min_op1)))
2487 && (TREE_CODE (max_op0) == INTEGER_CST
2488 || (sym_max_op0
2489 = get_single_symbol (max_op0, &neg_max_op0, &max_op0)))
2490 && (TREE_CODE (max_op1) == INTEGER_CST
2491 || (sym_max_op1
2492 = get_single_symbol (max_op1, &neg_max_op1, &max_op1)))
2493 && (!(sym_max_op0 && sym_max_op1)
2494 || (sym_max_op0 == sym_max_op1
2495 && neg_max_op0 == (minus_p ? neg_max_op1 : !neg_max_op1))))
2497 const signop sgn = TYPE_SIGN (expr_type);
2498 const unsigned int prec = TYPE_PRECISION (expr_type);
2499 wide_int type_min, type_max, wmin, wmax;
2500 int min_ovf = 0;
2501 int max_ovf = 0;
2503 /* Get the lower and upper bounds of the type. */
2504 if (TYPE_OVERFLOW_WRAPS (expr_type))
2506 type_min = wi::min_value (prec, sgn);
2507 type_max = wi::max_value (prec, sgn);
2509 else
2511 type_min = vrp_val_min (expr_type);
2512 type_max = vrp_val_max (expr_type);
2515 /* Combine the lower bounds, if any. */
2516 if (min_op0 && min_op1)
2518 if (minus_p)
2520 wmin = wi::sub (min_op0, min_op1);
2522 /* Check for overflow. */
2523 if (wi::cmp (0, min_op1, sgn)
2524 != wi::cmp (wmin, min_op0, sgn))
2525 min_ovf = wi::cmp (min_op0, min_op1, sgn);
2527 else
2529 wmin = wi::add (min_op0, min_op1);
2531 /* Check for overflow. */
2532 if (wi::cmp (min_op1, 0, sgn)
2533 != wi::cmp (wmin, min_op0, sgn))
2534 min_ovf = wi::cmp (min_op0, wmin, sgn);
2537 else if (min_op0)
2538 wmin = min_op0;
2539 else if (min_op1)
2540 wmin = minus_p ? wi::neg (min_op1) : min_op1;
2541 else
2542 wmin = wi::shwi (0, prec);
2544 /* Combine the upper bounds, if any. */
2545 if (max_op0 && max_op1)
2547 if (minus_p)
2549 wmax = wi::sub (max_op0, max_op1);
2551 /* Check for overflow. */
2552 if (wi::cmp (0, max_op1, sgn)
2553 != wi::cmp (wmax, max_op0, sgn))
2554 max_ovf = wi::cmp (max_op0, max_op1, sgn);
2556 else
2558 wmax = wi::add (max_op0, max_op1);
2560 if (wi::cmp (max_op1, 0, sgn)
2561 != wi::cmp (wmax, max_op0, sgn))
2562 max_ovf = wi::cmp (max_op0, wmax, sgn);
2565 else if (max_op0)
2566 wmax = max_op0;
2567 else if (max_op1)
2568 wmax = minus_p ? wi::neg (max_op1) : max_op1;
2569 else
2570 wmax = wi::shwi (0, prec);
2572 /* Check for type overflow. */
2573 if (min_ovf == 0)
2575 if (wi::cmp (wmin, type_min, sgn) == -1)
2576 min_ovf = -1;
2577 else if (wi::cmp (wmin, type_max, sgn) == 1)
2578 min_ovf = 1;
2580 if (max_ovf == 0)
2582 if (wi::cmp (wmax, type_min, sgn) == -1)
2583 max_ovf = -1;
2584 else if (wi::cmp (wmax, type_max, sgn) == 1)
2585 max_ovf = 1;
2588 /* If we have overflow for the constant part and the resulting
2589 range will be symbolic, drop to VR_VARYING. */
2590 if ((min_ovf && sym_min_op0 != sym_min_op1)
2591 || (max_ovf && sym_max_op0 != sym_max_op1))
2593 set_value_range_to_varying (vr);
2594 return;
2597 if (TYPE_OVERFLOW_WRAPS (expr_type))
2599 /* If overflow wraps, truncate the values and adjust the
2600 range kind and bounds appropriately. */
2601 wide_int tmin = wide_int::from (wmin, prec, sgn);
2602 wide_int tmax = wide_int::from (wmax, prec, sgn);
2603 if (min_ovf == max_ovf)
2605 /* No overflow or both overflow or underflow. The
2606 range kind stays VR_RANGE. */
2607 min = wide_int_to_tree (expr_type, tmin);
2608 max = wide_int_to_tree (expr_type, tmax);
2610 else if (min_ovf == -1 && max_ovf == 1)
2612 /* Underflow and overflow, drop to VR_VARYING. */
2613 set_value_range_to_varying (vr);
2614 return;
2616 else
2618 /* Min underflow or max overflow. The range kind
2619 changes to VR_ANTI_RANGE. */
2620 bool covers = false;
2621 wide_int tem = tmin;
2622 gcc_assert ((min_ovf == -1 && max_ovf == 0)
2623 || (max_ovf == 1 && min_ovf == 0));
2624 type = VR_ANTI_RANGE;
2625 tmin = tmax + 1;
2626 if (wi::cmp (tmin, tmax, sgn) < 0)
2627 covers = true;
2628 tmax = tem - 1;
2629 if (wi::cmp (tmax, tem, sgn) > 0)
2630 covers = true;
2631 /* If the anti-range would cover nothing, drop to varying.
2632 Likewise if the anti-range bounds are outside of the
2633 types values. */
2634 if (covers || wi::cmp (tmin, tmax, sgn) > 0)
2636 set_value_range_to_varying (vr);
2637 return;
2639 min = wide_int_to_tree (expr_type, tmin);
2640 max = wide_int_to_tree (expr_type, tmax);
2643 else
2645 /* If overflow does not wrap, saturate to the types min/max
2646 value. */
2647 if (min_ovf == -1)
2649 if (needs_overflow_infinity (expr_type)
2650 && supports_overflow_infinity (expr_type))
2651 min = negative_overflow_infinity (expr_type);
2652 else
2653 min = wide_int_to_tree (expr_type, type_min);
2655 else if (min_ovf == 1)
2657 if (needs_overflow_infinity (expr_type)
2658 && supports_overflow_infinity (expr_type))
2659 min = positive_overflow_infinity (expr_type);
2660 else
2661 min = wide_int_to_tree (expr_type, type_max);
2663 else
2664 min = wide_int_to_tree (expr_type, wmin);
2666 if (max_ovf == -1)
2668 if (needs_overflow_infinity (expr_type)
2669 && supports_overflow_infinity (expr_type))
2670 max = negative_overflow_infinity (expr_type);
2671 else
2672 max = wide_int_to_tree (expr_type, type_min);
2674 else if (max_ovf == 1)
2676 if (needs_overflow_infinity (expr_type)
2677 && supports_overflow_infinity (expr_type))
2678 max = positive_overflow_infinity (expr_type);
2679 else
2680 max = wide_int_to_tree (expr_type, type_max);
2682 else
2683 max = wide_int_to_tree (expr_type, wmax);
2686 if (needs_overflow_infinity (expr_type)
2687 && supports_overflow_infinity (expr_type))
2689 if ((min_op0 && is_negative_overflow_infinity (min_op0))
2690 || (min_op1
2691 && (minus_p
2692 ? is_positive_overflow_infinity (min_op1)
2693 : is_negative_overflow_infinity (min_op1))))
2694 min = negative_overflow_infinity (expr_type);
2695 if ((max_op0 && is_positive_overflow_infinity (max_op0))
2696 || (max_op1
2697 && (minus_p
2698 ? is_negative_overflow_infinity (max_op1)
2699 : is_positive_overflow_infinity (max_op1))))
2700 max = positive_overflow_infinity (expr_type);
2703 /* If the result lower bound is constant, we're done;
2704 otherwise, build the symbolic lower bound. */
2705 if (sym_min_op0 == sym_min_op1)
2707 else if (sym_min_op0)
2708 min = build_symbolic_expr (expr_type, sym_min_op0,
2709 neg_min_op0, min);
2710 else if (sym_min_op1)
2711 min = build_symbolic_expr (expr_type, sym_min_op1,
2712 neg_min_op1 ^ minus_p, min);
2714 /* Likewise for the upper bound. */
2715 if (sym_max_op0 == sym_max_op1)
2717 else if (sym_max_op0)
2718 max = build_symbolic_expr (expr_type, sym_max_op0,
2719 neg_max_op0, max);
2720 else if (sym_max_op1)
2721 max = build_symbolic_expr (expr_type, sym_max_op1,
2722 neg_max_op1 ^ minus_p, max);
2724 else
2726 /* For other cases, for example if we have a PLUS_EXPR with two
2727 VR_ANTI_RANGEs, drop to VR_VARYING. It would take more effort
2728 to compute a precise range for such a case.
2729 ??? General even mixed range kind operations can be expressed
2730 by for example transforming ~[3, 5] + [1, 2] to range-only
2731 operations and a union primitive:
2732 [-INF, 2] + [1, 2] U [5, +INF] + [1, 2]
2733 [-INF+1, 4] U [6, +INF(OVF)]
2734 though usually the union is not exactly representable with
2735 a single range or anti-range as the above is
2736 [-INF+1, +INF(OVF)] intersected with ~[5, 5]
2737 but one could use a scheme similar to equivalences for this. */
2738 set_value_range_to_varying (vr);
2739 return;
2742 else if (code == MIN_EXPR
2743 || code == MAX_EXPR)
2745 if (vr0.type == VR_RANGE
2746 && !symbolic_range_p (&vr0))
2748 type = VR_RANGE;
2749 if (vr1.type == VR_RANGE
2750 && !symbolic_range_p (&vr1))
2752 /* For operations that make the resulting range directly
2753 proportional to the original ranges, apply the operation to
2754 the same end of each range. */
2755 min = vrp_int_const_binop (code, vr0.min, vr1.min);
2756 max = vrp_int_const_binop (code, vr0.max, vr1.max);
2758 else if (code == MIN_EXPR)
2760 min = vrp_val_min (expr_type);
2761 max = vr0.max;
2763 else if (code == MAX_EXPR)
2765 min = vr0.min;
2766 max = vrp_val_max (expr_type);
2769 else if (vr1.type == VR_RANGE
2770 && !symbolic_range_p (&vr1))
2772 type = VR_RANGE;
2773 if (code == MIN_EXPR)
2775 min = vrp_val_min (expr_type);
2776 max = vr1.max;
2778 else if (code == MAX_EXPR)
2780 min = vr1.min;
2781 max = vrp_val_max (expr_type);
2784 else
2786 set_value_range_to_varying (vr);
2787 return;
2790 else if (code == MULT_EXPR)
2792 /* Fancy code so that with unsigned, [-3,-1]*[-3,-1] does not
2793 drop to varying. This test requires 2*prec bits if both
2794 operands are signed and 2*prec + 2 bits if either is not. */
2796 signop sign = TYPE_SIGN (expr_type);
2797 unsigned int prec = TYPE_PRECISION (expr_type);
2799 if (range_int_cst_p (&vr0)
2800 && range_int_cst_p (&vr1)
2801 && TYPE_OVERFLOW_WRAPS (expr_type))
2803 typedef FIXED_WIDE_INT (WIDE_INT_MAX_PRECISION * 2) vrp_int;
2804 typedef generic_wide_int
2805 <wi::extended_tree <WIDE_INT_MAX_PRECISION * 2> > vrp_int_cst;
2806 vrp_int sizem1 = wi::mask <vrp_int> (prec, false);
2807 vrp_int size = sizem1 + 1;
2809 /* Extend the values using the sign of the result to PREC2.
2810 From here on out, everthing is just signed math no matter
2811 what the input types were. */
2812 vrp_int min0 = vrp_int_cst (vr0.min);
2813 vrp_int max0 = vrp_int_cst (vr0.max);
2814 vrp_int min1 = vrp_int_cst (vr1.min);
2815 vrp_int max1 = vrp_int_cst (vr1.max);
2816 /* Canonicalize the intervals. */
2817 if (sign == UNSIGNED)
2819 if (wi::ltu_p (size, min0 + max0))
2821 min0 -= size;
2822 max0 -= size;
2825 if (wi::ltu_p (size, min1 + max1))
2827 min1 -= size;
2828 max1 -= size;
2832 vrp_int prod0 = min0 * min1;
2833 vrp_int prod1 = min0 * max1;
2834 vrp_int prod2 = max0 * min1;
2835 vrp_int prod3 = max0 * max1;
2837 /* Sort the 4 products so that min is in prod0 and max is in
2838 prod3. */
2839 /* min0min1 > max0max1 */
2840 if (wi::gts_p (prod0, prod3))
2841 std::swap (prod0, prod3);
2843 /* min0max1 > max0min1 */
2844 if (wi::gts_p (prod1, prod2))
2845 std::swap (prod1, prod2);
2847 if (wi::gts_p (prod0, prod1))
2848 std::swap (prod0, prod1);
2850 if (wi::gts_p (prod2, prod3))
2851 std::swap (prod2, prod3);
2853 /* diff = max - min. */
2854 prod2 = prod3 - prod0;
2855 if (wi::geu_p (prod2, sizem1))
2857 /* the range covers all values. */
2858 set_value_range_to_varying (vr);
2859 return;
2862 /* The following should handle the wrapping and selecting
2863 VR_ANTI_RANGE for us. */
2864 min = wide_int_to_tree (expr_type, prod0);
2865 max = wide_int_to_tree (expr_type, prod3);
2866 set_and_canonicalize_value_range (vr, VR_RANGE, min, max, NULL);
2867 return;
2870 /* If we have an unsigned MULT_EXPR with two VR_ANTI_RANGEs,
2871 drop to VR_VARYING. It would take more effort to compute a
2872 precise range for such a case. For example, if we have
2873 op0 == 65536 and op1 == 65536 with their ranges both being
2874 ~[0,0] on a 32-bit machine, we would have op0 * op1 == 0, so
2875 we cannot claim that the product is in ~[0,0]. Note that we
2876 are guaranteed to have vr0.type == vr1.type at this
2877 point. */
2878 if (vr0.type == VR_ANTI_RANGE
2879 && !TYPE_OVERFLOW_UNDEFINED (expr_type))
2881 set_value_range_to_varying (vr);
2882 return;
2885 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
2886 return;
2888 else if (code == RSHIFT_EXPR
2889 || code == LSHIFT_EXPR)
2891 /* If we have a RSHIFT_EXPR with any shift values outside [0..prec-1],
2892 then drop to VR_VARYING. Outside of this range we get undefined
2893 behavior from the shift operation. We cannot even trust
2894 SHIFT_COUNT_TRUNCATED at this stage, because that applies to rtl
2895 shifts, and the operation at the tree level may be widened. */
2896 if (range_int_cst_p (&vr1)
2897 && compare_tree_int (vr1.min, 0) >= 0
2898 && compare_tree_int (vr1.max, TYPE_PRECISION (expr_type)) == -1)
2900 if (code == RSHIFT_EXPR)
2902 /* Even if vr0 is VARYING or otherwise not usable, we can derive
2903 useful ranges just from the shift count. E.g.
2904 x >> 63 for signed 64-bit x is always [-1, 0]. */
2905 if (vr0.type != VR_RANGE || symbolic_range_p (&vr0))
2907 vr0.type = type = VR_RANGE;
2908 vr0.min = vrp_val_min (expr_type);
2909 vr0.max = vrp_val_max (expr_type);
2911 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
2912 return;
2914 /* We can map lshifts by constants to MULT_EXPR handling. */
2915 else if (code == LSHIFT_EXPR
2916 && range_int_cst_singleton_p (&vr1))
2918 bool saved_flag_wrapv;
2919 value_range_t vr1p = VR_INITIALIZER;
2920 vr1p.type = VR_RANGE;
2921 vr1p.min = (wide_int_to_tree
2922 (expr_type,
2923 wi::set_bit_in_zero (tree_to_shwi (vr1.min),
2924 TYPE_PRECISION (expr_type))));
2925 vr1p.max = vr1p.min;
2926 /* We have to use a wrapping multiply though as signed overflow
2927 on lshifts is implementation defined in C89. */
2928 saved_flag_wrapv = flag_wrapv;
2929 flag_wrapv = 1;
2930 extract_range_from_binary_expr_1 (vr, MULT_EXPR, expr_type,
2931 &vr0, &vr1p);
2932 flag_wrapv = saved_flag_wrapv;
2933 return;
2935 else if (code == LSHIFT_EXPR
2936 && range_int_cst_p (&vr0))
2938 int prec = TYPE_PRECISION (expr_type);
2939 int overflow_pos = prec;
2940 int bound_shift;
2941 wide_int low_bound, high_bound;
2942 bool uns = TYPE_UNSIGNED (expr_type);
2943 bool in_bounds = false;
2945 if (!uns)
2946 overflow_pos -= 1;
2948 bound_shift = overflow_pos - tree_to_shwi (vr1.max);
2949 /* If bound_shift == HOST_BITS_PER_WIDE_INT, the llshift can
2950 overflow. However, for that to happen, vr1.max needs to be
2951 zero, which means vr1 is a singleton range of zero, which
2952 means it should be handled by the previous LSHIFT_EXPR
2953 if-clause. */
2954 wide_int bound = wi::set_bit_in_zero (bound_shift, prec);
2955 wide_int complement = ~(bound - 1);
2957 if (uns)
2959 low_bound = bound;
2960 high_bound = complement;
2961 if (wi::ltu_p (vr0.max, low_bound))
2963 /* [5, 6] << [1, 2] == [10, 24]. */
2964 /* We're shifting out only zeroes, the value increases
2965 monotonically. */
2966 in_bounds = true;
2968 else if (wi::ltu_p (high_bound, vr0.min))
2970 /* [0xffffff00, 0xffffffff] << [1, 2]
2971 == [0xfffffc00, 0xfffffffe]. */
2972 /* We're shifting out only ones, the value decreases
2973 monotonically. */
2974 in_bounds = true;
2977 else
2979 /* [-1, 1] << [1, 2] == [-4, 4]. */
2980 low_bound = complement;
2981 high_bound = bound;
2982 if (wi::lts_p (vr0.max, high_bound)
2983 && wi::lts_p (low_bound, vr0.min))
2985 /* For non-negative numbers, we're shifting out only
2986 zeroes, the value increases monotonically.
2987 For negative numbers, we're shifting out only ones, the
2988 value decreases monotomically. */
2989 in_bounds = true;
2993 if (in_bounds)
2995 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
2996 return;
3000 set_value_range_to_varying (vr);
3001 return;
3003 else if (code == TRUNC_DIV_EXPR
3004 || code == FLOOR_DIV_EXPR
3005 || code == CEIL_DIV_EXPR
3006 || code == EXACT_DIV_EXPR
3007 || code == ROUND_DIV_EXPR)
3009 if (vr0.type != VR_RANGE || symbolic_range_p (&vr0))
3011 /* For division, if op1 has VR_RANGE but op0 does not, something
3012 can be deduced just from that range. Say [min, max] / [4, max]
3013 gives [min / 4, max / 4] range. */
3014 if (vr1.type == VR_RANGE
3015 && !symbolic_range_p (&vr1)
3016 && range_includes_zero_p (vr1.min, vr1.max) == 0)
3018 vr0.type = type = VR_RANGE;
3019 vr0.min = vrp_val_min (expr_type);
3020 vr0.max = vrp_val_max (expr_type);
3022 else
3024 set_value_range_to_varying (vr);
3025 return;
3029 /* For divisions, if flag_non_call_exceptions is true, we must
3030 not eliminate a division by zero. */
3031 if (cfun->can_throw_non_call_exceptions
3032 && (vr1.type != VR_RANGE
3033 || range_includes_zero_p (vr1.min, vr1.max) != 0))
3035 set_value_range_to_varying (vr);
3036 return;
3039 /* For divisions, if op0 is VR_RANGE, we can deduce a range
3040 even if op1 is VR_VARYING, VR_ANTI_RANGE, symbolic or can
3041 include 0. */
3042 if (vr0.type == VR_RANGE
3043 && (vr1.type != VR_RANGE
3044 || range_includes_zero_p (vr1.min, vr1.max) != 0))
3046 tree zero = build_int_cst (TREE_TYPE (vr0.min), 0);
3047 int cmp;
3049 min = NULL_TREE;
3050 max = NULL_TREE;
3051 if (TYPE_UNSIGNED (expr_type)
3052 || value_range_nonnegative_p (&vr1))
3054 /* For unsigned division or when divisor is known
3055 to be non-negative, the range has to cover
3056 all numbers from 0 to max for positive max
3057 and all numbers from min to 0 for negative min. */
3058 cmp = compare_values (vr0.max, zero);
3059 if (cmp == -1)
3061 /* When vr0.max < 0, vr1.min != 0 and value
3062 ranges for dividend and divisor are available. */
3063 if (vr1.type == VR_RANGE
3064 && !symbolic_range_p (&vr0)
3065 && !symbolic_range_p (&vr1)
3066 && !compare_values (vr1.min, zero))
3067 max = int_const_binop (code, vr0.max, vr1.min);
3068 else
3069 max = zero;
3071 else if (cmp == 0 || cmp == 1)
3072 max = vr0.max;
3073 else
3074 type = VR_VARYING;
3075 cmp = compare_values (vr0.min, zero);
3076 if (cmp == 1)
3078 /* For unsigned division when value ranges for dividend
3079 and divisor are available. */
3080 if (vr1.type == VR_RANGE
3081 && !symbolic_range_p (&vr0)
3082 && !symbolic_range_p (&vr1))
3083 min = int_const_binop (code, vr0.min, vr1.max);
3084 else
3085 min = zero;
3087 else if (cmp == 0 || cmp == -1)
3088 min = vr0.min;
3089 else
3090 type = VR_VARYING;
3092 else
3094 /* Otherwise the range is -max .. max or min .. -min
3095 depending on which bound is bigger in absolute value,
3096 as the division can change the sign. */
3097 abs_extent_range (vr, vr0.min, vr0.max);
3098 return;
3100 if (type == VR_VARYING)
3102 set_value_range_to_varying (vr);
3103 return;
3106 else
3108 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
3109 return;
3112 else if (code == TRUNC_MOD_EXPR)
3114 if (range_is_null (&vr1))
3116 set_value_range_to_undefined (vr);
3117 return;
3119 /* ABS (A % B) < ABS (B) and either
3120 0 <= A % B <= A or A <= A % B <= 0. */
3121 type = VR_RANGE;
3122 signop sgn = TYPE_SIGN (expr_type);
3123 unsigned int prec = TYPE_PRECISION (expr_type);
3124 wide_int wmin, wmax, tmp;
3125 wide_int zero = wi::zero (prec);
3126 wide_int one = wi::one (prec);
3127 if (vr1.type == VR_RANGE && !symbolic_range_p (&vr1))
3129 wmax = wi::sub (vr1.max, one);
3130 if (sgn == SIGNED)
3132 tmp = wi::sub (wi::minus_one (prec), vr1.min);
3133 wmax = wi::smax (wmax, tmp);
3136 else
3138 wmax = wi::max_value (prec, sgn);
3139 /* X % INT_MIN may be INT_MAX. */
3140 if (sgn == UNSIGNED)
3141 wmax = wmax - one;
3144 if (sgn == UNSIGNED)
3145 wmin = zero;
3146 else
3148 wmin = -wmax;
3149 if (vr0.type == VR_RANGE && TREE_CODE (vr0.min) == INTEGER_CST)
3151 tmp = vr0.min;
3152 if (wi::gts_p (tmp, zero))
3153 tmp = zero;
3154 wmin = wi::smax (wmin, tmp);
3158 if (vr0.type == VR_RANGE && TREE_CODE (vr0.max) == INTEGER_CST)
3160 tmp = vr0.max;
3161 if (sgn == SIGNED && wi::neg_p (tmp))
3162 tmp = zero;
3163 wmax = wi::min (wmax, tmp, sgn);
3166 min = wide_int_to_tree (expr_type, wmin);
3167 max = wide_int_to_tree (expr_type, wmax);
3169 else if (code == BIT_AND_EXPR || code == BIT_IOR_EXPR || code == BIT_XOR_EXPR)
3171 bool int_cst_range0, int_cst_range1;
3172 wide_int may_be_nonzero0, may_be_nonzero1;
3173 wide_int must_be_nonzero0, must_be_nonzero1;
3175 int_cst_range0 = zero_nonzero_bits_from_vr (expr_type, &vr0,
3176 &may_be_nonzero0,
3177 &must_be_nonzero0);
3178 int_cst_range1 = zero_nonzero_bits_from_vr (expr_type, &vr1,
3179 &may_be_nonzero1,
3180 &must_be_nonzero1);
3182 type = VR_RANGE;
3183 if (code == BIT_AND_EXPR)
3185 min = wide_int_to_tree (expr_type,
3186 must_be_nonzero0 & must_be_nonzero1);
3187 wide_int wmax = may_be_nonzero0 & may_be_nonzero1;
3188 /* If both input ranges contain only negative values we can
3189 truncate the result range maximum to the minimum of the
3190 input range maxima. */
3191 if (int_cst_range0 && int_cst_range1
3192 && tree_int_cst_sgn (vr0.max) < 0
3193 && tree_int_cst_sgn (vr1.max) < 0)
3195 wmax = wi::min (wmax, vr0.max, TYPE_SIGN (expr_type));
3196 wmax = wi::min (wmax, vr1.max, TYPE_SIGN (expr_type));
3198 /* If either input range contains only non-negative values
3199 we can truncate the result range maximum to the respective
3200 maximum of the input range. */
3201 if (int_cst_range0 && tree_int_cst_sgn (vr0.min) >= 0)
3202 wmax = wi::min (wmax, vr0.max, TYPE_SIGN (expr_type));
3203 if (int_cst_range1 && tree_int_cst_sgn (vr1.min) >= 0)
3204 wmax = wi::min (wmax, vr1.max, TYPE_SIGN (expr_type));
3205 max = wide_int_to_tree (expr_type, wmax);
3207 else if (code == BIT_IOR_EXPR)
3209 max = wide_int_to_tree (expr_type,
3210 may_be_nonzero0 | may_be_nonzero1);
3211 wide_int wmin = must_be_nonzero0 | must_be_nonzero1;
3212 /* If the input ranges contain only positive values we can
3213 truncate the minimum of the result range to the maximum
3214 of the input range minima. */
3215 if (int_cst_range0 && int_cst_range1
3216 && tree_int_cst_sgn (vr0.min) >= 0
3217 && tree_int_cst_sgn (vr1.min) >= 0)
3219 wmin = wi::max (wmin, vr0.min, TYPE_SIGN (expr_type));
3220 wmin = wi::max (wmin, vr1.min, TYPE_SIGN (expr_type));
3222 /* If either input range contains only negative values
3223 we can truncate the minimum of the result range to the
3224 respective minimum range. */
3225 if (int_cst_range0 && tree_int_cst_sgn (vr0.max) < 0)
3226 wmin = wi::max (wmin, vr0.min, TYPE_SIGN (expr_type));
3227 if (int_cst_range1 && tree_int_cst_sgn (vr1.max) < 0)
3228 wmin = wi::max (wmin, vr1.min, TYPE_SIGN (expr_type));
3229 min = wide_int_to_tree (expr_type, wmin);
3231 else if (code == BIT_XOR_EXPR)
3233 wide_int result_zero_bits = ((must_be_nonzero0 & must_be_nonzero1)
3234 | ~(may_be_nonzero0 | may_be_nonzero1));
3235 wide_int result_one_bits
3236 = (must_be_nonzero0.and_not (may_be_nonzero1)
3237 | must_be_nonzero1.and_not (may_be_nonzero0));
3238 max = wide_int_to_tree (expr_type, ~result_zero_bits);
3239 min = wide_int_to_tree (expr_type, result_one_bits);
3240 /* If the range has all positive or all negative values the
3241 result is better than VARYING. */
3242 if (tree_int_cst_sgn (min) < 0
3243 || tree_int_cst_sgn (max) >= 0)
3245 else
3246 max = min = NULL_TREE;
3249 else
3250 gcc_unreachable ();
3252 /* If either MIN or MAX overflowed, then set the resulting range to
3253 VARYING. But we do accept an overflow infinity representation. */
3254 if (min == NULL_TREE
3255 || (TREE_OVERFLOW_P (min) && !is_overflow_infinity (min))
3256 || max == NULL_TREE
3257 || (TREE_OVERFLOW_P (max) && !is_overflow_infinity (max)))
3259 set_value_range_to_varying (vr);
3260 return;
3263 /* We punt if:
3264 1) [-INF, +INF]
3265 2) [-INF, +-INF(OVF)]
3266 3) [+-INF(OVF), +INF]
3267 4) [+-INF(OVF), +-INF(OVF)]
3268 We learn nothing when we have INF and INF(OVF) on both sides.
3269 Note that we do accept [-INF, -INF] and [+INF, +INF] without
3270 overflow. */
3271 if ((vrp_val_is_min (min) || is_overflow_infinity (min))
3272 && (vrp_val_is_max (max) || is_overflow_infinity (max)))
3274 set_value_range_to_varying (vr);
3275 return;
3278 cmp = compare_values (min, max);
3279 if (cmp == -2 || cmp == 1)
3281 /* If the new range has its limits swapped around (MIN > MAX),
3282 then the operation caused one of them to wrap around, mark
3283 the new range VARYING. */
3284 set_value_range_to_varying (vr);
3286 else
3287 set_value_range (vr, type, min, max, NULL);
3290 /* Extract range information from a binary expression OP0 CODE OP1 based on
3291 the ranges of each of its operands with resulting type EXPR_TYPE.
3292 The resulting range is stored in *VR. */
3294 static void
3295 extract_range_from_binary_expr (value_range_t *vr,
3296 enum tree_code code,
3297 tree expr_type, tree op0, tree op1)
3299 value_range_t vr0 = VR_INITIALIZER;
3300 value_range_t vr1 = VR_INITIALIZER;
3302 /* Get value ranges for each operand. For constant operands, create
3303 a new value range with the operand to simplify processing. */
3304 if (TREE_CODE (op0) == SSA_NAME)
3305 vr0 = *(get_value_range (op0));
3306 else if (is_gimple_min_invariant (op0))
3307 set_value_range_to_value (&vr0, op0, NULL);
3308 else
3309 set_value_range_to_varying (&vr0);
3311 if (TREE_CODE (op1) == SSA_NAME)
3312 vr1 = *(get_value_range (op1));
3313 else if (is_gimple_min_invariant (op1))
3314 set_value_range_to_value (&vr1, op1, NULL);
3315 else
3316 set_value_range_to_varying (&vr1);
3318 extract_range_from_binary_expr_1 (vr, code, expr_type, &vr0, &vr1);
3320 /* Try harder for PLUS and MINUS if the range of one operand is symbolic
3321 and based on the other operand, for example if it was deduced from a
3322 symbolic comparison. When a bound of the range of the first operand
3323 is invariant, we set the corresponding bound of the new range to INF
3324 in order to avoid recursing on the range of the second operand. */
3325 if (vr->type == VR_VARYING
3326 && (code == PLUS_EXPR || code == MINUS_EXPR)
3327 && TREE_CODE (op1) == SSA_NAME
3328 && vr0.type == VR_RANGE
3329 && symbolic_range_based_on_p (&vr0, op1))
3331 const bool minus_p = (code == MINUS_EXPR);
3332 value_range_t n_vr1 = VR_INITIALIZER;
3334 /* Try with VR0 and [-INF, OP1]. */
3335 if (is_gimple_min_invariant (minus_p ? vr0.max : vr0.min))
3336 set_value_range (&n_vr1, VR_RANGE, vrp_val_min (expr_type), op1, NULL);
3338 /* Try with VR0 and [OP1, +INF]. */
3339 else if (is_gimple_min_invariant (minus_p ? vr0.min : vr0.max))
3340 set_value_range (&n_vr1, VR_RANGE, op1, vrp_val_max (expr_type), NULL);
3342 /* Try with VR0 and [OP1, OP1]. */
3343 else
3344 set_value_range (&n_vr1, VR_RANGE, op1, op1, NULL);
3346 extract_range_from_binary_expr_1 (vr, code, expr_type, &vr0, &n_vr1);
3349 if (vr->type == VR_VARYING
3350 && (code == PLUS_EXPR || code == MINUS_EXPR)
3351 && TREE_CODE (op0) == SSA_NAME
3352 && vr1.type == VR_RANGE
3353 && symbolic_range_based_on_p (&vr1, op0))
3355 const bool minus_p = (code == MINUS_EXPR);
3356 value_range_t n_vr0 = VR_INITIALIZER;
3358 /* Try with [-INF, OP0] and VR1. */
3359 if (is_gimple_min_invariant (minus_p ? vr1.max : vr1.min))
3360 set_value_range (&n_vr0, VR_RANGE, vrp_val_min (expr_type), op0, NULL);
3362 /* Try with [OP0, +INF] and VR1. */
3363 else if (is_gimple_min_invariant (minus_p ? vr1.min : vr1.max))
3364 set_value_range (&n_vr0, VR_RANGE, op0, vrp_val_max (expr_type), NULL);
3366 /* Try with [OP0, OP0] and VR1. */
3367 else
3368 set_value_range (&n_vr0, VR_RANGE, op0, op0, NULL);
3370 extract_range_from_binary_expr_1 (vr, code, expr_type, &n_vr0, &vr1);
3374 /* Extract range information from a unary operation CODE based on
3375 the range of its operand *VR0 with type OP0_TYPE with resulting type TYPE.
3376 The resulting range is stored in *VR. */
3378 static void
3379 extract_range_from_unary_expr_1 (value_range_t *vr,
3380 enum tree_code code, tree type,
3381 value_range_t *vr0_, tree op0_type)
3383 value_range_t vr0 = *vr0_, vrtem0 = VR_INITIALIZER, vrtem1 = VR_INITIALIZER;
3385 /* VRP only operates on integral and pointer types. */
3386 if (!(INTEGRAL_TYPE_P (op0_type)
3387 || POINTER_TYPE_P (op0_type))
3388 || !(INTEGRAL_TYPE_P (type)
3389 || POINTER_TYPE_P (type)))
3391 set_value_range_to_varying (vr);
3392 return;
3395 /* If VR0 is UNDEFINED, so is the result. */
3396 if (vr0.type == VR_UNDEFINED)
3398 set_value_range_to_undefined (vr);
3399 return;
3402 /* Handle operations that we express in terms of others. */
3403 if (code == PAREN_EXPR || code == OBJ_TYPE_REF)
3405 /* PAREN_EXPR and OBJ_TYPE_REF are simple copies. */
3406 copy_value_range (vr, &vr0);
3407 return;
3409 else if (code == NEGATE_EXPR)
3411 /* -X is simply 0 - X, so re-use existing code that also handles
3412 anti-ranges fine. */
3413 value_range_t zero = VR_INITIALIZER;
3414 set_value_range_to_value (&zero, build_int_cst (type, 0), NULL);
3415 extract_range_from_binary_expr_1 (vr, MINUS_EXPR, type, &zero, &vr0);
3416 return;
3418 else if (code == BIT_NOT_EXPR)
3420 /* ~X is simply -1 - X, so re-use existing code that also handles
3421 anti-ranges fine. */
3422 value_range_t minusone = VR_INITIALIZER;
3423 set_value_range_to_value (&minusone, build_int_cst (type, -1), NULL);
3424 extract_range_from_binary_expr_1 (vr, MINUS_EXPR,
3425 type, &minusone, &vr0);
3426 return;
3429 /* Now canonicalize anti-ranges to ranges when they are not symbolic
3430 and express op ~[] as (op []') U (op []''). */
3431 if (vr0.type == VR_ANTI_RANGE
3432 && ranges_from_anti_range (&vr0, &vrtem0, &vrtem1))
3434 extract_range_from_unary_expr_1 (vr, code, type, &vrtem0, op0_type);
3435 if (vrtem1.type != VR_UNDEFINED)
3437 value_range_t vrres = VR_INITIALIZER;
3438 extract_range_from_unary_expr_1 (&vrres, code, type,
3439 &vrtem1, op0_type);
3440 vrp_meet (vr, &vrres);
3442 return;
3445 if (CONVERT_EXPR_CODE_P (code))
3447 tree inner_type = op0_type;
3448 tree outer_type = type;
3450 /* If the expression evaluates to a pointer, we are only interested in
3451 determining if it evaluates to NULL [0, 0] or non-NULL (~[0, 0]). */
3452 if (POINTER_TYPE_P (type))
3454 if (range_is_nonnull (&vr0))
3455 set_value_range_to_nonnull (vr, type);
3456 else if (range_is_null (&vr0))
3457 set_value_range_to_null (vr, type);
3458 else
3459 set_value_range_to_varying (vr);
3460 return;
3463 /* If VR0 is varying and we increase the type precision, assume
3464 a full range for the following transformation. */
3465 if (vr0.type == VR_VARYING
3466 && INTEGRAL_TYPE_P (inner_type)
3467 && TYPE_PRECISION (inner_type) < TYPE_PRECISION (outer_type))
3469 vr0.type = VR_RANGE;
3470 vr0.min = TYPE_MIN_VALUE (inner_type);
3471 vr0.max = TYPE_MAX_VALUE (inner_type);
3474 /* If VR0 is a constant range or anti-range and the conversion is
3475 not truncating we can convert the min and max values and
3476 canonicalize the resulting range. Otherwise we can do the
3477 conversion if the size of the range is less than what the
3478 precision of the target type can represent and the range is
3479 not an anti-range. */
3480 if ((vr0.type == VR_RANGE
3481 || vr0.type == VR_ANTI_RANGE)
3482 && TREE_CODE (vr0.min) == INTEGER_CST
3483 && TREE_CODE (vr0.max) == INTEGER_CST
3484 && (!is_overflow_infinity (vr0.min)
3485 || (vr0.type == VR_RANGE
3486 && TYPE_PRECISION (outer_type) > TYPE_PRECISION (inner_type)
3487 && needs_overflow_infinity (outer_type)
3488 && supports_overflow_infinity (outer_type)))
3489 && (!is_overflow_infinity (vr0.max)
3490 || (vr0.type == VR_RANGE
3491 && TYPE_PRECISION (outer_type) > TYPE_PRECISION (inner_type)
3492 && needs_overflow_infinity (outer_type)
3493 && supports_overflow_infinity (outer_type)))
3494 && (TYPE_PRECISION (outer_type) >= TYPE_PRECISION (inner_type)
3495 || (vr0.type == VR_RANGE
3496 && integer_zerop (int_const_binop (RSHIFT_EXPR,
3497 int_const_binop (MINUS_EXPR, vr0.max, vr0.min),
3498 size_int (TYPE_PRECISION (outer_type)))))))
3500 tree new_min, new_max;
3501 if (is_overflow_infinity (vr0.min))
3502 new_min = negative_overflow_infinity (outer_type);
3503 else
3504 new_min = force_fit_type (outer_type, wi::to_widest (vr0.min),
3505 0, false);
3506 if (is_overflow_infinity (vr0.max))
3507 new_max = positive_overflow_infinity (outer_type);
3508 else
3509 new_max = force_fit_type (outer_type, wi::to_widest (vr0.max),
3510 0, false);
3511 set_and_canonicalize_value_range (vr, vr0.type,
3512 new_min, new_max, NULL);
3513 return;
3516 set_value_range_to_varying (vr);
3517 return;
3519 else if (code == ABS_EXPR)
3521 tree min, max;
3522 int cmp;
3524 /* Pass through vr0 in the easy cases. */
3525 if (TYPE_UNSIGNED (type)
3526 || value_range_nonnegative_p (&vr0))
3528 copy_value_range (vr, &vr0);
3529 return;
3532 /* For the remaining varying or symbolic ranges we can't do anything
3533 useful. */
3534 if (vr0.type == VR_VARYING
3535 || symbolic_range_p (&vr0))
3537 set_value_range_to_varying (vr);
3538 return;
3541 /* -TYPE_MIN_VALUE = TYPE_MIN_VALUE with flag_wrapv so we can't get a
3542 useful range. */
3543 if (!TYPE_OVERFLOW_UNDEFINED (type)
3544 && ((vr0.type == VR_RANGE
3545 && vrp_val_is_min (vr0.min))
3546 || (vr0.type == VR_ANTI_RANGE
3547 && !vrp_val_is_min (vr0.min))))
3549 set_value_range_to_varying (vr);
3550 return;
3553 /* ABS_EXPR may flip the range around, if the original range
3554 included negative values. */
3555 if (is_overflow_infinity (vr0.min))
3556 min = positive_overflow_infinity (type);
3557 else if (!vrp_val_is_min (vr0.min))
3558 min = fold_unary_to_constant (code, type, vr0.min);
3559 else if (!needs_overflow_infinity (type))
3560 min = TYPE_MAX_VALUE (type);
3561 else if (supports_overflow_infinity (type))
3562 min = positive_overflow_infinity (type);
3563 else
3565 set_value_range_to_varying (vr);
3566 return;
3569 if (is_overflow_infinity (vr0.max))
3570 max = positive_overflow_infinity (type);
3571 else if (!vrp_val_is_min (vr0.max))
3572 max = fold_unary_to_constant (code, type, vr0.max);
3573 else if (!needs_overflow_infinity (type))
3574 max = TYPE_MAX_VALUE (type);
3575 else if (supports_overflow_infinity (type)
3576 /* We shouldn't generate [+INF, +INF] as set_value_range
3577 doesn't like this and ICEs. */
3578 && !is_positive_overflow_infinity (min))
3579 max = positive_overflow_infinity (type);
3580 else
3582 set_value_range_to_varying (vr);
3583 return;
3586 cmp = compare_values (min, max);
3588 /* If a VR_ANTI_RANGEs contains zero, then we have
3589 ~[-INF, min(MIN, MAX)]. */
3590 if (vr0.type == VR_ANTI_RANGE)
3592 if (range_includes_zero_p (vr0.min, vr0.max) == 1)
3594 /* Take the lower of the two values. */
3595 if (cmp != 1)
3596 max = min;
3598 /* Create ~[-INF, min (abs(MIN), abs(MAX))]
3599 or ~[-INF + 1, min (abs(MIN), abs(MAX))] when
3600 flag_wrapv is set and the original anti-range doesn't include
3601 TYPE_MIN_VALUE, remember -TYPE_MIN_VALUE = TYPE_MIN_VALUE. */
3602 if (TYPE_OVERFLOW_WRAPS (type))
3604 tree type_min_value = TYPE_MIN_VALUE (type);
3606 min = (vr0.min != type_min_value
3607 ? int_const_binop (PLUS_EXPR, type_min_value,
3608 build_int_cst (TREE_TYPE (type_min_value), 1))
3609 : type_min_value);
3611 else
3613 if (overflow_infinity_range_p (&vr0))
3614 min = negative_overflow_infinity (type);
3615 else
3616 min = TYPE_MIN_VALUE (type);
3619 else
3621 /* All else has failed, so create the range [0, INF], even for
3622 flag_wrapv since TYPE_MIN_VALUE is in the original
3623 anti-range. */
3624 vr0.type = VR_RANGE;
3625 min = build_int_cst (type, 0);
3626 if (needs_overflow_infinity (type))
3628 if (supports_overflow_infinity (type))
3629 max = positive_overflow_infinity (type);
3630 else
3632 set_value_range_to_varying (vr);
3633 return;
3636 else
3637 max = TYPE_MAX_VALUE (type);
3641 /* If the range contains zero then we know that the minimum value in the
3642 range will be zero. */
3643 else if (range_includes_zero_p (vr0.min, vr0.max) == 1)
3645 if (cmp == 1)
3646 max = min;
3647 min = build_int_cst (type, 0);
3649 else
3651 /* If the range was reversed, swap MIN and MAX. */
3652 if (cmp == 1)
3653 std::swap (min, max);
3656 cmp = compare_values (min, max);
3657 if (cmp == -2 || cmp == 1)
3659 /* If the new range has its limits swapped around (MIN > MAX),
3660 then the operation caused one of them to wrap around, mark
3661 the new range VARYING. */
3662 set_value_range_to_varying (vr);
3664 else
3665 set_value_range (vr, vr0.type, min, max, NULL);
3666 return;
3669 /* For unhandled operations fall back to varying. */
3670 set_value_range_to_varying (vr);
3671 return;
3675 /* Extract range information from a unary expression CODE OP0 based on
3676 the range of its operand with resulting type TYPE.
3677 The resulting range is stored in *VR. */
3679 static void
3680 extract_range_from_unary_expr (value_range_t *vr, enum tree_code code,
3681 tree type, tree op0)
3683 value_range_t vr0 = VR_INITIALIZER;
3685 /* Get value ranges for the operand. For constant operands, create
3686 a new value range with the operand to simplify processing. */
3687 if (TREE_CODE (op0) == SSA_NAME)
3688 vr0 = *(get_value_range (op0));
3689 else if (is_gimple_min_invariant (op0))
3690 set_value_range_to_value (&vr0, op0, NULL);
3691 else
3692 set_value_range_to_varying (&vr0);
3694 extract_range_from_unary_expr_1 (vr, code, type, &vr0, TREE_TYPE (op0));
3698 /* Extract range information from a conditional expression STMT based on
3699 the ranges of each of its operands and the expression code. */
3701 static void
3702 extract_range_from_cond_expr (value_range_t *vr, gassign *stmt)
3704 tree op0, op1;
3705 value_range_t vr0 = VR_INITIALIZER;
3706 value_range_t vr1 = VR_INITIALIZER;
3708 /* Get value ranges for each operand. For constant operands, create
3709 a new value range with the operand to simplify processing. */
3710 op0 = gimple_assign_rhs2 (stmt);
3711 if (TREE_CODE (op0) == SSA_NAME)
3712 vr0 = *(get_value_range (op0));
3713 else if (is_gimple_min_invariant (op0))
3714 set_value_range_to_value (&vr0, op0, NULL);
3715 else
3716 set_value_range_to_varying (&vr0);
3718 op1 = gimple_assign_rhs3 (stmt);
3719 if (TREE_CODE (op1) == SSA_NAME)
3720 vr1 = *(get_value_range (op1));
3721 else if (is_gimple_min_invariant (op1))
3722 set_value_range_to_value (&vr1, op1, NULL);
3723 else
3724 set_value_range_to_varying (&vr1);
3726 /* The resulting value range is the union of the operand ranges */
3727 copy_value_range (vr, &vr0);
3728 vrp_meet (vr, &vr1);
3732 /* Extract range information from a comparison expression EXPR based
3733 on the range of its operand and the expression code. */
3735 static void
3736 extract_range_from_comparison (value_range_t *vr, enum tree_code code,
3737 tree type, tree op0, tree op1)
3739 bool sop = false;
3740 tree val;
3742 val = vrp_evaluate_conditional_warnv_with_ops (code, op0, op1, false, &sop,
3743 NULL);
3745 /* A disadvantage of using a special infinity as an overflow
3746 representation is that we lose the ability to record overflow
3747 when we don't have an infinity. So we have to ignore a result
3748 which relies on overflow. */
3750 if (val && !is_overflow_infinity (val) && !sop)
3752 /* Since this expression was found on the RHS of an assignment,
3753 its type may be different from _Bool. Convert VAL to EXPR's
3754 type. */
3755 val = fold_convert (type, val);
3756 if (is_gimple_min_invariant (val))
3757 set_value_range_to_value (vr, val, vr->equiv);
3758 else
3759 set_value_range (vr, VR_RANGE, val, val, vr->equiv);
3761 else
3762 /* The result of a comparison is always true or false. */
3763 set_value_range_to_truthvalue (vr, type);
3766 /* Helper function for simplify_internal_call_using_ranges and
3767 extract_range_basic. Return true if OP0 SUBCODE OP1 for
3768 SUBCODE {PLUS,MINUS,MULT}_EXPR is known to never overflow or
3769 always overflow. Set *OVF to true if it is known to always
3770 overflow. */
3772 static bool
3773 check_for_binary_op_overflow (enum tree_code subcode, tree type,
3774 tree op0, tree op1, bool *ovf)
3776 value_range_t vr0 = VR_INITIALIZER;
3777 value_range_t vr1 = VR_INITIALIZER;
3778 if (TREE_CODE (op0) == SSA_NAME)
3779 vr0 = *get_value_range (op0);
3780 else if (TREE_CODE (op0) == INTEGER_CST)
3781 set_value_range_to_value (&vr0, op0, NULL);
3782 else
3783 set_value_range_to_varying (&vr0);
3785 if (TREE_CODE (op1) == SSA_NAME)
3786 vr1 = *get_value_range (op1);
3787 else if (TREE_CODE (op1) == INTEGER_CST)
3788 set_value_range_to_value (&vr1, op1, NULL);
3789 else
3790 set_value_range_to_varying (&vr1);
3792 if (!range_int_cst_p (&vr0)
3793 || TREE_OVERFLOW (vr0.min)
3794 || TREE_OVERFLOW (vr0.max))
3796 vr0.min = vrp_val_min (TREE_TYPE (op0));
3797 vr0.max = vrp_val_max (TREE_TYPE (op0));
3799 if (!range_int_cst_p (&vr1)
3800 || TREE_OVERFLOW (vr1.min)
3801 || TREE_OVERFLOW (vr1.max))
3803 vr1.min = vrp_val_min (TREE_TYPE (op1));
3804 vr1.max = vrp_val_max (TREE_TYPE (op1));
3806 *ovf = arith_overflowed_p (subcode, type, vr0.min,
3807 subcode == MINUS_EXPR ? vr1.max : vr1.min);
3808 if (arith_overflowed_p (subcode, type, vr0.max,
3809 subcode == MINUS_EXPR ? vr1.min : vr1.max) != *ovf)
3810 return false;
3811 if (subcode == MULT_EXPR)
3813 if (arith_overflowed_p (subcode, type, vr0.min, vr1.max) != *ovf
3814 || arith_overflowed_p (subcode, type, vr0.max, vr1.min) != *ovf)
3815 return false;
3817 if (*ovf)
3819 /* So far we found that there is an overflow on the boundaries.
3820 That doesn't prove that there is an overflow even for all values
3821 in between the boundaries. For that compute widest_int range
3822 of the result and see if it doesn't overlap the range of
3823 type. */
3824 widest_int wmin, wmax;
3825 widest_int w[4];
3826 int i;
3827 w[0] = wi::to_widest (vr0.min);
3828 w[1] = wi::to_widest (vr0.max);
3829 w[2] = wi::to_widest (vr1.min);
3830 w[3] = wi::to_widest (vr1.max);
3831 for (i = 0; i < 4; i++)
3833 widest_int wt;
3834 switch (subcode)
3836 case PLUS_EXPR:
3837 wt = wi::add (w[i & 1], w[2 + (i & 2) / 2]);
3838 break;
3839 case MINUS_EXPR:
3840 wt = wi::sub (w[i & 1], w[2 + (i & 2) / 2]);
3841 break;
3842 case MULT_EXPR:
3843 wt = wi::mul (w[i & 1], w[2 + (i & 2) / 2]);
3844 break;
3845 default:
3846 gcc_unreachable ();
3848 if (i == 0)
3850 wmin = wt;
3851 wmax = wt;
3853 else
3855 wmin = wi::smin (wmin, wt);
3856 wmax = wi::smax (wmax, wt);
3859 /* The result of op0 CODE op1 is known to be in range
3860 [wmin, wmax]. */
3861 widest_int wtmin = wi::to_widest (vrp_val_min (type));
3862 widest_int wtmax = wi::to_widest (vrp_val_max (type));
3863 /* If all values in [wmin, wmax] are smaller than
3864 [wtmin, wtmax] or all are larger than [wtmin, wtmax],
3865 the arithmetic operation will always overflow. */
3866 if (wi::lts_p (wmax, wtmin) || wi::gts_p (wmin, wtmax))
3867 return true;
3868 return false;
3870 return true;
3873 /* Try to derive a nonnegative or nonzero range out of STMT relying
3874 primarily on generic routines in fold in conjunction with range data.
3875 Store the result in *VR */
3877 static void
3878 extract_range_basic (value_range_t *vr, gimple stmt)
3880 bool sop = false;
3881 tree type = gimple_expr_type (stmt);
3883 if (gimple_call_builtin_p (stmt, BUILT_IN_NORMAL))
3885 tree fndecl = gimple_call_fndecl (stmt), arg;
3886 int mini, maxi, zerov = 0, prec;
3888 switch (DECL_FUNCTION_CODE (fndecl))
3890 case BUILT_IN_CONSTANT_P:
3891 /* If the call is __builtin_constant_p and the argument is a
3892 function parameter resolve it to false. This avoids bogus
3893 array bound warnings.
3894 ??? We could do this as early as inlining is finished. */
3895 arg = gimple_call_arg (stmt, 0);
3896 if (TREE_CODE (arg) == SSA_NAME
3897 && SSA_NAME_IS_DEFAULT_DEF (arg)
3898 && TREE_CODE (SSA_NAME_VAR (arg)) == PARM_DECL)
3900 set_value_range_to_null (vr, type);
3901 return;
3903 break;
3904 /* Both __builtin_ffs* and __builtin_popcount return
3905 [0, prec]. */
3906 CASE_INT_FN (BUILT_IN_FFS):
3907 CASE_INT_FN (BUILT_IN_POPCOUNT):
3908 arg = gimple_call_arg (stmt, 0);
3909 prec = TYPE_PRECISION (TREE_TYPE (arg));
3910 mini = 0;
3911 maxi = prec;
3912 if (TREE_CODE (arg) == SSA_NAME)
3914 value_range_t *vr0 = get_value_range (arg);
3915 /* If arg is non-zero, then ffs or popcount
3916 are non-zero. */
3917 if (((vr0->type == VR_RANGE
3918 && range_includes_zero_p (vr0->min, vr0->max) == 0)
3919 || (vr0->type == VR_ANTI_RANGE
3920 && range_includes_zero_p (vr0->min, vr0->max) == 1))
3921 && !is_overflow_infinity (vr0->min)
3922 && !is_overflow_infinity (vr0->max))
3923 mini = 1;
3924 /* If some high bits are known to be zero,
3925 we can decrease the maximum. */
3926 if (vr0->type == VR_RANGE
3927 && TREE_CODE (vr0->max) == INTEGER_CST
3928 && !operand_less_p (vr0->min,
3929 build_zero_cst (TREE_TYPE (vr0->min)))
3930 && !is_overflow_infinity (vr0->max))
3931 maxi = tree_floor_log2 (vr0->max) + 1;
3933 goto bitop_builtin;
3934 /* __builtin_parity* returns [0, 1]. */
3935 CASE_INT_FN (BUILT_IN_PARITY):
3936 mini = 0;
3937 maxi = 1;
3938 goto bitop_builtin;
3939 /* __builtin_c[lt]z* return [0, prec-1], except for
3940 when the argument is 0, but that is undefined behavior.
3941 On many targets where the CLZ RTL or optab value is defined
3942 for 0 the value is prec, so include that in the range
3943 by default. */
3944 CASE_INT_FN (BUILT_IN_CLZ):
3945 arg = gimple_call_arg (stmt, 0);
3946 prec = TYPE_PRECISION (TREE_TYPE (arg));
3947 mini = 0;
3948 maxi = prec;
3949 if (optab_handler (clz_optab, TYPE_MODE (TREE_TYPE (arg)))
3950 != CODE_FOR_nothing
3951 && CLZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (TREE_TYPE (arg)),
3952 zerov)
3953 /* Handle only the single common value. */
3954 && zerov != prec)
3955 /* Magic value to give up, unless vr0 proves
3956 arg is non-zero. */
3957 mini = -2;
3958 if (TREE_CODE (arg) == SSA_NAME)
3960 value_range_t *vr0 = get_value_range (arg);
3961 /* From clz of VR_RANGE minimum we can compute
3962 result maximum. */
3963 if (vr0->type == VR_RANGE
3964 && TREE_CODE (vr0->min) == INTEGER_CST
3965 && !is_overflow_infinity (vr0->min))
3967 maxi = prec - 1 - tree_floor_log2 (vr0->min);
3968 if (maxi != prec)
3969 mini = 0;
3971 else if (vr0->type == VR_ANTI_RANGE
3972 && integer_zerop (vr0->min)
3973 && !is_overflow_infinity (vr0->min))
3975 maxi = prec - 1;
3976 mini = 0;
3978 if (mini == -2)
3979 break;
3980 /* From clz of VR_RANGE maximum we can compute
3981 result minimum. */
3982 if (vr0->type == VR_RANGE
3983 && TREE_CODE (vr0->max) == INTEGER_CST
3984 && !is_overflow_infinity (vr0->max))
3986 mini = prec - 1 - tree_floor_log2 (vr0->max);
3987 if (mini == prec)
3988 break;
3991 if (mini == -2)
3992 break;
3993 goto bitop_builtin;
3994 /* __builtin_ctz* return [0, prec-1], except for
3995 when the argument is 0, but that is undefined behavior.
3996 If there is a ctz optab for this mode and
3997 CTZ_DEFINED_VALUE_AT_ZERO, include that in the range,
3998 otherwise just assume 0 won't be seen. */
3999 CASE_INT_FN (BUILT_IN_CTZ):
4000 arg = gimple_call_arg (stmt, 0);
4001 prec = TYPE_PRECISION (TREE_TYPE (arg));
4002 mini = 0;
4003 maxi = prec - 1;
4004 if (optab_handler (ctz_optab, TYPE_MODE (TREE_TYPE (arg)))
4005 != CODE_FOR_nothing
4006 && CTZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (TREE_TYPE (arg)),
4007 zerov))
4009 /* Handle only the two common values. */
4010 if (zerov == -1)
4011 mini = -1;
4012 else if (zerov == prec)
4013 maxi = prec;
4014 else
4015 /* Magic value to give up, unless vr0 proves
4016 arg is non-zero. */
4017 mini = -2;
4019 if (TREE_CODE (arg) == SSA_NAME)
4021 value_range_t *vr0 = get_value_range (arg);
4022 /* If arg is non-zero, then use [0, prec - 1]. */
4023 if (((vr0->type == VR_RANGE
4024 && integer_nonzerop (vr0->min))
4025 || (vr0->type == VR_ANTI_RANGE
4026 && integer_zerop (vr0->min)))
4027 && !is_overflow_infinity (vr0->min))
4029 mini = 0;
4030 maxi = prec - 1;
4032 /* If some high bits are known to be zero,
4033 we can decrease the result maximum. */
4034 if (vr0->type == VR_RANGE
4035 && TREE_CODE (vr0->max) == INTEGER_CST
4036 && !is_overflow_infinity (vr0->max))
4038 maxi = tree_floor_log2 (vr0->max);
4039 /* For vr0 [0, 0] give up. */
4040 if (maxi == -1)
4041 break;
4044 if (mini == -2)
4045 break;
4046 goto bitop_builtin;
4047 /* __builtin_clrsb* returns [0, prec-1]. */
4048 CASE_INT_FN (BUILT_IN_CLRSB):
4049 arg = gimple_call_arg (stmt, 0);
4050 prec = TYPE_PRECISION (TREE_TYPE (arg));
4051 mini = 0;
4052 maxi = prec - 1;
4053 goto bitop_builtin;
4054 bitop_builtin:
4055 set_value_range (vr, VR_RANGE, build_int_cst (type, mini),
4056 build_int_cst (type, maxi), NULL);
4057 return;
4058 default:
4059 break;
4062 else if (is_gimple_call (stmt) && gimple_call_internal_p (stmt))
4064 enum tree_code subcode = ERROR_MARK;
4065 switch (gimple_call_internal_fn (stmt))
4067 case IFN_UBSAN_CHECK_ADD:
4068 subcode = PLUS_EXPR;
4069 break;
4070 case IFN_UBSAN_CHECK_SUB:
4071 subcode = MINUS_EXPR;
4072 break;
4073 case IFN_UBSAN_CHECK_MUL:
4074 subcode = MULT_EXPR;
4075 break;
4076 default:
4077 break;
4079 if (subcode != ERROR_MARK)
4081 bool saved_flag_wrapv = flag_wrapv;
4082 /* Pretend the arithmetics is wrapping. If there is
4083 any overflow, we'll complain, but will actually do
4084 wrapping operation. */
4085 flag_wrapv = 1;
4086 extract_range_from_binary_expr (vr, subcode, type,
4087 gimple_call_arg (stmt, 0),
4088 gimple_call_arg (stmt, 1));
4089 flag_wrapv = saved_flag_wrapv;
4091 /* If for both arguments vrp_valueize returned non-NULL,
4092 this should have been already folded and if not, it
4093 wasn't folded because of overflow. Avoid removing the
4094 UBSAN_CHECK_* calls in that case. */
4095 if (vr->type == VR_RANGE
4096 && (vr->min == vr->max
4097 || operand_equal_p (vr->min, vr->max, 0)))
4098 set_value_range_to_varying (vr);
4099 return;
4102 /* Handle extraction of the two results (result of arithmetics and
4103 a flag whether arithmetics overflowed) from {ADD,SUB,MUL}_OVERFLOW
4104 internal function. */
4105 else if (is_gimple_assign (stmt)
4106 && (gimple_assign_rhs_code (stmt) == REALPART_EXPR
4107 || gimple_assign_rhs_code (stmt) == IMAGPART_EXPR)
4108 && INTEGRAL_TYPE_P (type))
4110 enum tree_code code = gimple_assign_rhs_code (stmt);
4111 tree op = gimple_assign_rhs1 (stmt);
4112 if (TREE_CODE (op) == code && TREE_CODE (TREE_OPERAND (op, 0)) == SSA_NAME)
4114 gimple g = SSA_NAME_DEF_STMT (TREE_OPERAND (op, 0));
4115 if (is_gimple_call (g) && gimple_call_internal_p (g))
4117 enum tree_code subcode = ERROR_MARK;
4118 switch (gimple_call_internal_fn (g))
4120 case IFN_ADD_OVERFLOW:
4121 subcode = PLUS_EXPR;
4122 break;
4123 case IFN_SUB_OVERFLOW:
4124 subcode = MINUS_EXPR;
4125 break;
4126 case IFN_MUL_OVERFLOW:
4127 subcode = MULT_EXPR;
4128 break;
4129 default:
4130 break;
4132 if (subcode != ERROR_MARK)
4134 tree op0 = gimple_call_arg (g, 0);
4135 tree op1 = gimple_call_arg (g, 1);
4136 if (code == IMAGPART_EXPR)
4138 bool ovf = false;
4139 if (check_for_binary_op_overflow (subcode, type,
4140 op0, op1, &ovf))
4141 set_value_range_to_value (vr,
4142 build_int_cst (type, ovf),
4143 NULL);
4144 else
4145 set_value_range (vr, VR_RANGE, build_int_cst (type, 0),
4146 build_int_cst (type, 1), NULL);
4148 else if (types_compatible_p (type, TREE_TYPE (op0))
4149 && types_compatible_p (type, TREE_TYPE (op1)))
4151 bool saved_flag_wrapv = flag_wrapv;
4152 /* Pretend the arithmetics is wrapping. If there is
4153 any overflow, IMAGPART_EXPR will be set. */
4154 flag_wrapv = 1;
4155 extract_range_from_binary_expr (vr, subcode, type,
4156 op0, op1);
4157 flag_wrapv = saved_flag_wrapv;
4159 else
4161 value_range_t vr0 = VR_INITIALIZER;
4162 value_range_t vr1 = VR_INITIALIZER;
4163 bool saved_flag_wrapv = flag_wrapv;
4164 /* Pretend the arithmetics is wrapping. If there is
4165 any overflow, IMAGPART_EXPR will be set. */
4166 flag_wrapv = 1;
4167 extract_range_from_unary_expr (&vr0, NOP_EXPR,
4168 type, op0);
4169 extract_range_from_unary_expr (&vr1, NOP_EXPR,
4170 type, op1);
4171 extract_range_from_binary_expr_1 (vr, subcode, type,
4172 &vr0, &vr1);
4173 flag_wrapv = saved_flag_wrapv;
4175 return;
4180 if (INTEGRAL_TYPE_P (type)
4181 && gimple_stmt_nonnegative_warnv_p (stmt, &sop))
4182 set_value_range_to_nonnegative (vr, type,
4183 sop || stmt_overflow_infinity (stmt));
4184 else if (vrp_stmt_computes_nonzero (stmt, &sop)
4185 && !sop)
4186 set_value_range_to_nonnull (vr, type);
4187 else
4188 set_value_range_to_varying (vr);
4192 /* Try to compute a useful range out of assignment STMT and store it
4193 in *VR. */
4195 static void
4196 extract_range_from_assignment (value_range_t *vr, gassign *stmt)
4198 enum tree_code code = gimple_assign_rhs_code (stmt);
4200 if (code == ASSERT_EXPR)
4201 extract_range_from_assert (vr, gimple_assign_rhs1 (stmt));
4202 else if (code == SSA_NAME)
4203 extract_range_from_ssa_name (vr, gimple_assign_rhs1 (stmt));
4204 else if (TREE_CODE_CLASS (code) == tcc_binary)
4205 extract_range_from_binary_expr (vr, gimple_assign_rhs_code (stmt),
4206 gimple_expr_type (stmt),
4207 gimple_assign_rhs1 (stmt),
4208 gimple_assign_rhs2 (stmt));
4209 else if (TREE_CODE_CLASS (code) == tcc_unary)
4210 extract_range_from_unary_expr (vr, gimple_assign_rhs_code (stmt),
4211 gimple_expr_type (stmt),
4212 gimple_assign_rhs1 (stmt));
4213 else if (code == COND_EXPR)
4214 extract_range_from_cond_expr (vr, stmt);
4215 else if (TREE_CODE_CLASS (code) == tcc_comparison)
4216 extract_range_from_comparison (vr, gimple_assign_rhs_code (stmt),
4217 gimple_expr_type (stmt),
4218 gimple_assign_rhs1 (stmt),
4219 gimple_assign_rhs2 (stmt));
4220 else if (get_gimple_rhs_class (code) == GIMPLE_SINGLE_RHS
4221 && is_gimple_min_invariant (gimple_assign_rhs1 (stmt)))
4222 set_value_range_to_value (vr, gimple_assign_rhs1 (stmt), NULL);
4223 else
4224 set_value_range_to_varying (vr);
4226 if (vr->type == VR_VARYING)
4227 extract_range_basic (vr, stmt);
4230 /* Given a range VR, a LOOP and a variable VAR, determine whether it
4231 would be profitable to adjust VR using scalar evolution information
4232 for VAR. If so, update VR with the new limits. */
4234 static void
4235 adjust_range_with_scev (value_range_t *vr, struct loop *loop,
4236 gimple stmt, tree var)
4238 tree init, step, chrec, tmin, tmax, min, max, type, tem;
4239 enum ev_direction dir;
4241 /* TODO. Don't adjust anti-ranges. An anti-range may provide
4242 better opportunities than a regular range, but I'm not sure. */
4243 if (vr->type == VR_ANTI_RANGE)
4244 return;
4246 chrec = instantiate_parameters (loop, analyze_scalar_evolution (loop, var));
4248 /* Like in PR19590, scev can return a constant function. */
4249 if (is_gimple_min_invariant (chrec))
4251 set_value_range_to_value (vr, chrec, vr->equiv);
4252 return;
4255 if (TREE_CODE (chrec) != POLYNOMIAL_CHREC)
4256 return;
4258 init = initial_condition_in_loop_num (chrec, loop->num);
4259 tem = op_with_constant_singleton_value_range (init);
4260 if (tem)
4261 init = tem;
4262 step = evolution_part_in_loop_num (chrec, loop->num);
4263 tem = op_with_constant_singleton_value_range (step);
4264 if (tem)
4265 step = tem;
4267 /* If STEP is symbolic, we can't know whether INIT will be the
4268 minimum or maximum value in the range. Also, unless INIT is
4269 a simple expression, compare_values and possibly other functions
4270 in tree-vrp won't be able to handle it. */
4271 if (step == NULL_TREE
4272 || !is_gimple_min_invariant (step)
4273 || !valid_value_p (init))
4274 return;
4276 dir = scev_direction (chrec);
4277 if (/* Do not adjust ranges if we do not know whether the iv increases
4278 or decreases, ... */
4279 dir == EV_DIR_UNKNOWN
4280 /* ... or if it may wrap. */
4281 || scev_probably_wraps_p (init, step, stmt, get_chrec_loop (chrec),
4282 true))
4283 return;
4285 /* We use TYPE_MIN_VALUE and TYPE_MAX_VALUE here instead of
4286 negative_overflow_infinity and positive_overflow_infinity,
4287 because we have concluded that the loop probably does not
4288 wrap. */
4290 type = TREE_TYPE (var);
4291 if (POINTER_TYPE_P (type) || !TYPE_MIN_VALUE (type))
4292 tmin = lower_bound_in_type (type, type);
4293 else
4294 tmin = TYPE_MIN_VALUE (type);
4295 if (POINTER_TYPE_P (type) || !TYPE_MAX_VALUE (type))
4296 tmax = upper_bound_in_type (type, type);
4297 else
4298 tmax = TYPE_MAX_VALUE (type);
4300 /* Try to use estimated number of iterations for the loop to constrain the
4301 final value in the evolution. */
4302 if (TREE_CODE (step) == INTEGER_CST
4303 && is_gimple_val (init)
4304 && (TREE_CODE (init) != SSA_NAME
4305 || get_value_range (init)->type == VR_RANGE))
4307 widest_int nit;
4309 /* We are only entering here for loop header PHI nodes, so using
4310 the number of latch executions is the correct thing to use. */
4311 if (max_loop_iterations (loop, &nit))
4313 value_range_t maxvr = VR_INITIALIZER;
4314 signop sgn = TYPE_SIGN (TREE_TYPE (step));
4315 bool overflow;
4317 widest_int wtmp = wi::mul (wi::to_widest (step), nit, sgn,
4318 &overflow);
4319 /* If the multiplication overflowed we can't do a meaningful
4320 adjustment. Likewise if the result doesn't fit in the type
4321 of the induction variable. For a signed type we have to
4322 check whether the result has the expected signedness which
4323 is that of the step as number of iterations is unsigned. */
4324 if (!overflow
4325 && wi::fits_to_tree_p (wtmp, TREE_TYPE (init))
4326 && (sgn == UNSIGNED
4327 || wi::gts_p (wtmp, 0) == wi::gts_p (step, 0)))
4329 tem = wide_int_to_tree (TREE_TYPE (init), wtmp);
4330 extract_range_from_binary_expr (&maxvr, PLUS_EXPR,
4331 TREE_TYPE (init), init, tem);
4332 /* Likewise if the addition did. */
4333 if (maxvr.type == VR_RANGE)
4335 tmin = maxvr.min;
4336 tmax = maxvr.max;
4342 if (vr->type == VR_VARYING || vr->type == VR_UNDEFINED)
4344 min = tmin;
4345 max = tmax;
4347 /* For VARYING or UNDEFINED ranges, just about anything we get
4348 from scalar evolutions should be better. */
4350 if (dir == EV_DIR_DECREASES)
4351 max = init;
4352 else
4353 min = init;
4355 else if (vr->type == VR_RANGE)
4357 min = vr->min;
4358 max = vr->max;
4360 if (dir == EV_DIR_DECREASES)
4362 /* INIT is the maximum value. If INIT is lower than VR->MAX
4363 but no smaller than VR->MIN, set VR->MAX to INIT. */
4364 if (compare_values (init, max) == -1)
4365 max = init;
4367 /* According to the loop information, the variable does not
4368 overflow. If we think it does, probably because of an
4369 overflow due to arithmetic on a different INF value,
4370 reset now. */
4371 if (is_negative_overflow_infinity (min)
4372 || compare_values (min, tmin) == -1)
4373 min = tmin;
4376 else
4378 /* If INIT is bigger than VR->MIN, set VR->MIN to INIT. */
4379 if (compare_values (init, min) == 1)
4380 min = init;
4382 if (is_positive_overflow_infinity (max)
4383 || compare_values (tmax, max) == -1)
4384 max = tmax;
4387 else
4388 return;
4390 /* If we just created an invalid range with the minimum
4391 greater than the maximum, we fail conservatively.
4392 This should happen only in unreachable
4393 parts of code, or for invalid programs. */
4394 if (compare_values (min, max) == 1
4395 || (is_negative_overflow_infinity (min)
4396 && is_positive_overflow_infinity (max)))
4397 return;
4399 set_value_range (vr, VR_RANGE, min, max, vr->equiv);
4403 /* Given two numeric value ranges VR0, VR1 and a comparison code COMP:
4405 - Return BOOLEAN_TRUE_NODE if VR0 COMP VR1 always returns true for
4406 all the values in the ranges.
4408 - Return BOOLEAN_FALSE_NODE if the comparison always returns false.
4410 - Return NULL_TREE if it is not always possible to determine the
4411 value of the comparison.
4413 Also set *STRICT_OVERFLOW_P to indicate whether a range with an
4414 overflow infinity was used in the test. */
4417 static tree
4418 compare_ranges (enum tree_code comp, value_range_t *vr0, value_range_t *vr1,
4419 bool *strict_overflow_p)
4421 /* VARYING or UNDEFINED ranges cannot be compared. */
4422 if (vr0->type == VR_VARYING
4423 || vr0->type == VR_UNDEFINED
4424 || vr1->type == VR_VARYING
4425 || vr1->type == VR_UNDEFINED)
4426 return NULL_TREE;
4428 /* Anti-ranges need to be handled separately. */
4429 if (vr0->type == VR_ANTI_RANGE || vr1->type == VR_ANTI_RANGE)
4431 /* If both are anti-ranges, then we cannot compute any
4432 comparison. */
4433 if (vr0->type == VR_ANTI_RANGE && vr1->type == VR_ANTI_RANGE)
4434 return NULL_TREE;
4436 /* These comparisons are never statically computable. */
4437 if (comp == GT_EXPR
4438 || comp == GE_EXPR
4439 || comp == LT_EXPR
4440 || comp == LE_EXPR)
4441 return NULL_TREE;
4443 /* Equality can be computed only between a range and an
4444 anti-range. ~[VAL1, VAL2] == [VAL1, VAL2] is always false. */
4445 if (vr0->type == VR_RANGE)
4447 /* To simplify processing, make VR0 the anti-range. */
4448 value_range_t *tmp = vr0;
4449 vr0 = vr1;
4450 vr1 = tmp;
4453 gcc_assert (comp == NE_EXPR || comp == EQ_EXPR);
4455 if (compare_values_warnv (vr0->min, vr1->min, strict_overflow_p) == 0
4456 && compare_values_warnv (vr0->max, vr1->max, strict_overflow_p) == 0)
4457 return (comp == NE_EXPR) ? boolean_true_node : boolean_false_node;
4459 return NULL_TREE;
4462 if (!usable_range_p (vr0, strict_overflow_p)
4463 || !usable_range_p (vr1, strict_overflow_p))
4464 return NULL_TREE;
4466 /* Simplify processing. If COMP is GT_EXPR or GE_EXPR, switch the
4467 operands around and change the comparison code. */
4468 if (comp == GT_EXPR || comp == GE_EXPR)
4470 comp = (comp == GT_EXPR) ? LT_EXPR : LE_EXPR;
4471 std::swap (vr0, vr1);
4474 if (comp == EQ_EXPR)
4476 /* Equality may only be computed if both ranges represent
4477 exactly one value. */
4478 if (compare_values_warnv (vr0->min, vr0->max, strict_overflow_p) == 0
4479 && compare_values_warnv (vr1->min, vr1->max, strict_overflow_p) == 0)
4481 int cmp_min = compare_values_warnv (vr0->min, vr1->min,
4482 strict_overflow_p);
4483 int cmp_max = compare_values_warnv (vr0->max, vr1->max,
4484 strict_overflow_p);
4485 if (cmp_min == 0 && cmp_max == 0)
4486 return boolean_true_node;
4487 else if (cmp_min != -2 && cmp_max != -2)
4488 return boolean_false_node;
4490 /* If [V0_MIN, V1_MAX] < [V1_MIN, V1_MAX] then V0 != V1. */
4491 else if (compare_values_warnv (vr0->min, vr1->max,
4492 strict_overflow_p) == 1
4493 || compare_values_warnv (vr1->min, vr0->max,
4494 strict_overflow_p) == 1)
4495 return boolean_false_node;
4497 return NULL_TREE;
4499 else if (comp == NE_EXPR)
4501 int cmp1, cmp2;
4503 /* If VR0 is completely to the left or completely to the right
4504 of VR1, they are always different. Notice that we need to
4505 make sure that both comparisons yield similar results to
4506 avoid comparing values that cannot be compared at
4507 compile-time. */
4508 cmp1 = compare_values_warnv (vr0->max, vr1->min, strict_overflow_p);
4509 cmp2 = compare_values_warnv (vr0->min, vr1->max, strict_overflow_p);
4510 if ((cmp1 == -1 && cmp2 == -1) || (cmp1 == 1 && cmp2 == 1))
4511 return boolean_true_node;
4513 /* If VR0 and VR1 represent a single value and are identical,
4514 return false. */
4515 else if (compare_values_warnv (vr0->min, vr0->max,
4516 strict_overflow_p) == 0
4517 && compare_values_warnv (vr1->min, vr1->max,
4518 strict_overflow_p) == 0
4519 && compare_values_warnv (vr0->min, vr1->min,
4520 strict_overflow_p) == 0
4521 && compare_values_warnv (vr0->max, vr1->max,
4522 strict_overflow_p) == 0)
4523 return boolean_false_node;
4525 /* Otherwise, they may or may not be different. */
4526 else
4527 return NULL_TREE;
4529 else if (comp == LT_EXPR || comp == LE_EXPR)
4531 int tst;
4533 /* If VR0 is to the left of VR1, return true. */
4534 tst = compare_values_warnv (vr0->max, vr1->min, strict_overflow_p);
4535 if ((comp == LT_EXPR && tst == -1)
4536 || (comp == LE_EXPR && (tst == -1 || tst == 0)))
4538 if (overflow_infinity_range_p (vr0)
4539 || overflow_infinity_range_p (vr1))
4540 *strict_overflow_p = true;
4541 return boolean_true_node;
4544 /* If VR0 is to the right of VR1, return false. */
4545 tst = compare_values_warnv (vr0->min, vr1->max, strict_overflow_p);
4546 if ((comp == LT_EXPR && (tst == 0 || tst == 1))
4547 || (comp == LE_EXPR && tst == 1))
4549 if (overflow_infinity_range_p (vr0)
4550 || overflow_infinity_range_p (vr1))
4551 *strict_overflow_p = true;
4552 return boolean_false_node;
4555 /* Otherwise, we don't know. */
4556 return NULL_TREE;
4559 gcc_unreachable ();
4563 /* Given a value range VR, a value VAL and a comparison code COMP, return
4564 BOOLEAN_TRUE_NODE if VR COMP VAL always returns true for all the
4565 values in VR. Return BOOLEAN_FALSE_NODE if the comparison
4566 always returns false. Return NULL_TREE if it is not always
4567 possible to determine the value of the comparison. Also set
4568 *STRICT_OVERFLOW_P to indicate whether a range with an overflow
4569 infinity was used in the test. */
4571 static tree
4572 compare_range_with_value (enum tree_code comp, value_range_t *vr, tree val,
4573 bool *strict_overflow_p)
4575 if (vr->type == VR_VARYING || vr->type == VR_UNDEFINED)
4576 return NULL_TREE;
4578 /* Anti-ranges need to be handled separately. */
4579 if (vr->type == VR_ANTI_RANGE)
4581 /* For anti-ranges, the only predicates that we can compute at
4582 compile time are equality and inequality. */
4583 if (comp == GT_EXPR
4584 || comp == GE_EXPR
4585 || comp == LT_EXPR
4586 || comp == LE_EXPR)
4587 return NULL_TREE;
4589 /* ~[VAL_1, VAL_2] OP VAL is known if VAL_1 <= VAL <= VAL_2. */
4590 if (value_inside_range (val, vr->min, vr->max) == 1)
4591 return (comp == NE_EXPR) ? boolean_true_node : boolean_false_node;
4593 return NULL_TREE;
4596 if (!usable_range_p (vr, strict_overflow_p))
4597 return NULL_TREE;
4599 if (comp == EQ_EXPR)
4601 /* EQ_EXPR may only be computed if VR represents exactly
4602 one value. */
4603 if (compare_values_warnv (vr->min, vr->max, strict_overflow_p) == 0)
4605 int cmp = compare_values_warnv (vr->min, val, strict_overflow_p);
4606 if (cmp == 0)
4607 return boolean_true_node;
4608 else if (cmp == -1 || cmp == 1 || cmp == 2)
4609 return boolean_false_node;
4611 else if (compare_values_warnv (val, vr->min, strict_overflow_p) == -1
4612 || compare_values_warnv (vr->max, val, strict_overflow_p) == -1)
4613 return boolean_false_node;
4615 return NULL_TREE;
4617 else if (comp == NE_EXPR)
4619 /* If VAL is not inside VR, then they are always different. */
4620 if (compare_values_warnv (vr->max, val, strict_overflow_p) == -1
4621 || compare_values_warnv (vr->min, val, strict_overflow_p) == 1)
4622 return boolean_true_node;
4624 /* If VR represents exactly one value equal to VAL, then return
4625 false. */
4626 if (compare_values_warnv (vr->min, vr->max, strict_overflow_p) == 0
4627 && compare_values_warnv (vr->min, val, strict_overflow_p) == 0)
4628 return boolean_false_node;
4630 /* Otherwise, they may or may not be different. */
4631 return NULL_TREE;
4633 else if (comp == LT_EXPR || comp == LE_EXPR)
4635 int tst;
4637 /* If VR is to the left of VAL, return true. */
4638 tst = compare_values_warnv (vr->max, val, strict_overflow_p);
4639 if ((comp == LT_EXPR && tst == -1)
4640 || (comp == LE_EXPR && (tst == -1 || tst == 0)))
4642 if (overflow_infinity_range_p (vr))
4643 *strict_overflow_p = true;
4644 return boolean_true_node;
4647 /* If VR is to the right of VAL, return false. */
4648 tst = compare_values_warnv (vr->min, val, strict_overflow_p);
4649 if ((comp == LT_EXPR && (tst == 0 || tst == 1))
4650 || (comp == LE_EXPR && tst == 1))
4652 if (overflow_infinity_range_p (vr))
4653 *strict_overflow_p = true;
4654 return boolean_false_node;
4657 /* Otherwise, we don't know. */
4658 return NULL_TREE;
4660 else if (comp == GT_EXPR || comp == GE_EXPR)
4662 int tst;
4664 /* If VR is to the right of VAL, return true. */
4665 tst = compare_values_warnv (vr->min, val, strict_overflow_p);
4666 if ((comp == GT_EXPR && tst == 1)
4667 || (comp == GE_EXPR && (tst == 0 || tst == 1)))
4669 if (overflow_infinity_range_p (vr))
4670 *strict_overflow_p = true;
4671 return boolean_true_node;
4674 /* If VR is to the left of VAL, return false. */
4675 tst = compare_values_warnv (vr->max, val, strict_overflow_p);
4676 if ((comp == GT_EXPR && (tst == -1 || tst == 0))
4677 || (comp == GE_EXPR && tst == -1))
4679 if (overflow_infinity_range_p (vr))
4680 *strict_overflow_p = true;
4681 return boolean_false_node;
4684 /* Otherwise, we don't know. */
4685 return NULL_TREE;
4688 gcc_unreachable ();
4692 /* Debugging dumps. */
4694 void dump_value_range (FILE *, value_range_t *);
4695 void debug_value_range (value_range_t *);
4696 void dump_all_value_ranges (FILE *);
4697 void debug_all_value_ranges (void);
4698 void dump_vr_equiv (FILE *, bitmap);
4699 void debug_vr_equiv (bitmap);
4702 /* Dump value range VR to FILE. */
4704 void
4705 dump_value_range (FILE *file, value_range_t *vr)
4707 if (vr == NULL)
4708 fprintf (file, "[]");
4709 else if (vr->type == VR_UNDEFINED)
4710 fprintf (file, "UNDEFINED");
4711 else if (vr->type == VR_RANGE || vr->type == VR_ANTI_RANGE)
4713 tree type = TREE_TYPE (vr->min);
4715 fprintf (file, "%s[", (vr->type == VR_ANTI_RANGE) ? "~" : "");
4717 if (is_negative_overflow_infinity (vr->min))
4718 fprintf (file, "-INF(OVF)");
4719 else if (INTEGRAL_TYPE_P (type)
4720 && !TYPE_UNSIGNED (type)
4721 && vrp_val_is_min (vr->min))
4722 fprintf (file, "-INF");
4723 else
4724 print_generic_expr (file, vr->min, 0);
4726 fprintf (file, ", ");
4728 if (is_positive_overflow_infinity (vr->max))
4729 fprintf (file, "+INF(OVF)");
4730 else if (INTEGRAL_TYPE_P (type)
4731 && vrp_val_is_max (vr->max))
4732 fprintf (file, "+INF");
4733 else
4734 print_generic_expr (file, vr->max, 0);
4736 fprintf (file, "]");
4738 if (vr->equiv)
4740 bitmap_iterator bi;
4741 unsigned i, c = 0;
4743 fprintf (file, " EQUIVALENCES: { ");
4745 EXECUTE_IF_SET_IN_BITMAP (vr->equiv, 0, i, bi)
4747 print_generic_expr (file, ssa_name (i), 0);
4748 fprintf (file, " ");
4749 c++;
4752 fprintf (file, "} (%u elements)", c);
4755 else if (vr->type == VR_VARYING)
4756 fprintf (file, "VARYING");
4757 else
4758 fprintf (file, "INVALID RANGE");
4762 /* Dump value range VR to stderr. */
4764 DEBUG_FUNCTION void
4765 debug_value_range (value_range_t *vr)
4767 dump_value_range (stderr, vr);
4768 fprintf (stderr, "\n");
4772 /* Dump value ranges of all SSA_NAMEs to FILE. */
4774 void
4775 dump_all_value_ranges (FILE *file)
4777 size_t i;
4779 for (i = 0; i < num_vr_values; i++)
4781 if (vr_value[i])
4783 print_generic_expr (file, ssa_name (i), 0);
4784 fprintf (file, ": ");
4785 dump_value_range (file, vr_value[i]);
4786 fprintf (file, "\n");
4790 fprintf (file, "\n");
4794 /* Dump all value ranges to stderr. */
4796 DEBUG_FUNCTION void
4797 debug_all_value_ranges (void)
4799 dump_all_value_ranges (stderr);
4803 /* Given a COND_EXPR COND of the form 'V OP W', and an SSA name V,
4804 create a new SSA name N and return the assertion assignment
4805 'N = ASSERT_EXPR <V, V OP W>'. */
4807 static gimple
4808 build_assert_expr_for (tree cond, tree v)
4810 tree a;
4811 gassign *assertion;
4813 gcc_assert (TREE_CODE (v) == SSA_NAME
4814 && COMPARISON_CLASS_P (cond));
4816 a = build2 (ASSERT_EXPR, TREE_TYPE (v), v, cond);
4817 assertion = gimple_build_assign (NULL_TREE, a);
4819 /* The new ASSERT_EXPR, creates a new SSA name that replaces the
4820 operand of the ASSERT_EXPR. Create it so the new name and the old one
4821 are registered in the replacement table so that we can fix the SSA web
4822 after adding all the ASSERT_EXPRs. */
4823 create_new_def_for (v, assertion, NULL);
4825 return assertion;
4829 /* Return false if EXPR is a predicate expression involving floating
4830 point values. */
4832 static inline bool
4833 fp_predicate (gimple stmt)
4835 GIMPLE_CHECK (stmt, GIMPLE_COND);
4837 return FLOAT_TYPE_P (TREE_TYPE (gimple_cond_lhs (stmt)));
4840 /* If the range of values taken by OP can be inferred after STMT executes,
4841 return the comparison code (COMP_CODE_P) and value (VAL_P) that
4842 describes the inferred range. Return true if a range could be
4843 inferred. */
4845 static bool
4846 infer_value_range (gimple stmt, tree op, enum tree_code *comp_code_p, tree *val_p)
4848 *val_p = NULL_TREE;
4849 *comp_code_p = ERROR_MARK;
4851 /* Do not attempt to infer anything in names that flow through
4852 abnormal edges. */
4853 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op))
4854 return false;
4856 /* Similarly, don't infer anything from statements that may throw
4857 exceptions. ??? Relax this requirement? */
4858 if (stmt_could_throw_p (stmt))
4859 return false;
4861 /* If STMT is the last statement of a basic block with no normal
4862 successors, there is no point inferring anything about any of its
4863 operands. We would not be able to find a proper insertion point
4864 for the assertion, anyway. */
4865 if (stmt_ends_bb_p (stmt))
4867 edge_iterator ei;
4868 edge e;
4870 FOR_EACH_EDGE (e, ei, gimple_bb (stmt)->succs)
4871 if (!(e->flags & EDGE_ABNORMAL))
4872 break;
4873 if (e == NULL)
4874 return false;
4877 if (infer_nonnull_range (stmt, op))
4879 *val_p = build_int_cst (TREE_TYPE (op), 0);
4880 *comp_code_p = NE_EXPR;
4881 return true;
4884 return false;
4888 void dump_asserts_for (FILE *, tree);
4889 void debug_asserts_for (tree);
4890 void dump_all_asserts (FILE *);
4891 void debug_all_asserts (void);
4893 /* Dump all the registered assertions for NAME to FILE. */
4895 void
4896 dump_asserts_for (FILE *file, tree name)
4898 assert_locus *loc;
4900 fprintf (file, "Assertions to be inserted for ");
4901 print_generic_expr (file, name, 0);
4902 fprintf (file, "\n");
4904 loc = asserts_for[SSA_NAME_VERSION (name)];
4905 while (loc)
4907 fprintf (file, "\t");
4908 print_gimple_stmt (file, gsi_stmt (loc->si), 0, 0);
4909 fprintf (file, "\n\tBB #%d", loc->bb->index);
4910 if (loc->e)
4912 fprintf (file, "\n\tEDGE %d->%d", loc->e->src->index,
4913 loc->e->dest->index);
4914 dump_edge_info (file, loc->e, dump_flags, 0);
4916 fprintf (file, "\n\tPREDICATE: ");
4917 print_generic_expr (file, name, 0);
4918 fprintf (file, " %s ", get_tree_code_name (loc->comp_code));
4919 print_generic_expr (file, loc->val, 0);
4920 fprintf (file, "\n\n");
4921 loc = loc->next;
4924 fprintf (file, "\n");
4928 /* Dump all the registered assertions for NAME to stderr. */
4930 DEBUG_FUNCTION void
4931 debug_asserts_for (tree name)
4933 dump_asserts_for (stderr, name);
4937 /* Dump all the registered assertions for all the names to FILE. */
4939 void
4940 dump_all_asserts (FILE *file)
4942 unsigned i;
4943 bitmap_iterator bi;
4945 fprintf (file, "\nASSERT_EXPRs to be inserted\n\n");
4946 EXECUTE_IF_SET_IN_BITMAP (need_assert_for, 0, i, bi)
4947 dump_asserts_for (file, ssa_name (i));
4948 fprintf (file, "\n");
4952 /* Dump all the registered assertions for all the names to stderr. */
4954 DEBUG_FUNCTION void
4955 debug_all_asserts (void)
4957 dump_all_asserts (stderr);
4961 /* If NAME doesn't have an ASSERT_EXPR registered for asserting
4962 'EXPR COMP_CODE VAL' at a location that dominates block BB or
4963 E->DEST, then register this location as a possible insertion point
4964 for ASSERT_EXPR <NAME, EXPR COMP_CODE VAL>.
4966 BB, E and SI provide the exact insertion point for the new
4967 ASSERT_EXPR. If BB is NULL, then the ASSERT_EXPR is to be inserted
4968 on edge E. Otherwise, if E is NULL, the ASSERT_EXPR is inserted on
4969 BB. If SI points to a COND_EXPR or a SWITCH_EXPR statement, then E
4970 must not be NULL. */
4972 static void
4973 register_new_assert_for (tree name, tree expr,
4974 enum tree_code comp_code,
4975 tree val,
4976 basic_block bb,
4977 edge e,
4978 gimple_stmt_iterator si)
4980 assert_locus *n, *loc, *last_loc;
4981 basic_block dest_bb;
4983 gcc_checking_assert (bb == NULL || e == NULL);
4985 if (e == NULL)
4986 gcc_checking_assert (gimple_code (gsi_stmt (si)) != GIMPLE_COND
4987 && gimple_code (gsi_stmt (si)) != GIMPLE_SWITCH);
4989 /* Never build an assert comparing against an integer constant with
4990 TREE_OVERFLOW set. This confuses our undefined overflow warning
4991 machinery. */
4992 if (TREE_OVERFLOW_P (val))
4993 val = drop_tree_overflow (val);
4995 /* The new assertion A will be inserted at BB or E. We need to
4996 determine if the new location is dominated by a previously
4997 registered location for A. If we are doing an edge insertion,
4998 assume that A will be inserted at E->DEST. Note that this is not
4999 necessarily true.
5001 If E is a critical edge, it will be split. But even if E is
5002 split, the new block will dominate the same set of blocks that
5003 E->DEST dominates.
5005 The reverse, however, is not true, blocks dominated by E->DEST
5006 will not be dominated by the new block created to split E. So,
5007 if the insertion location is on a critical edge, we will not use
5008 the new location to move another assertion previously registered
5009 at a block dominated by E->DEST. */
5010 dest_bb = (bb) ? bb : e->dest;
5012 /* If NAME already has an ASSERT_EXPR registered for COMP_CODE and
5013 VAL at a block dominating DEST_BB, then we don't need to insert a new
5014 one. Similarly, if the same assertion already exists at a block
5015 dominated by DEST_BB and the new location is not on a critical
5016 edge, then update the existing location for the assertion (i.e.,
5017 move the assertion up in the dominance tree).
5019 Note, this is implemented as a simple linked list because there
5020 should not be more than a handful of assertions registered per
5021 name. If this becomes a performance problem, a table hashed by
5022 COMP_CODE and VAL could be implemented. */
5023 loc = asserts_for[SSA_NAME_VERSION (name)];
5024 last_loc = loc;
5025 while (loc)
5027 if (loc->comp_code == comp_code
5028 && (loc->val == val
5029 || operand_equal_p (loc->val, val, 0))
5030 && (loc->expr == expr
5031 || operand_equal_p (loc->expr, expr, 0)))
5033 /* If E is not a critical edge and DEST_BB
5034 dominates the existing location for the assertion, move
5035 the assertion up in the dominance tree by updating its
5036 location information. */
5037 if ((e == NULL || !EDGE_CRITICAL_P (e))
5038 && dominated_by_p (CDI_DOMINATORS, loc->bb, dest_bb))
5040 loc->bb = dest_bb;
5041 loc->e = e;
5042 loc->si = si;
5043 return;
5047 /* Update the last node of the list and move to the next one. */
5048 last_loc = loc;
5049 loc = loc->next;
5052 /* If we didn't find an assertion already registered for
5053 NAME COMP_CODE VAL, add a new one at the end of the list of
5054 assertions associated with NAME. */
5055 n = XNEW (struct assert_locus);
5056 n->bb = dest_bb;
5057 n->e = e;
5058 n->si = si;
5059 n->comp_code = comp_code;
5060 n->val = val;
5061 n->expr = expr;
5062 n->next = NULL;
5064 if (last_loc)
5065 last_loc->next = n;
5066 else
5067 asserts_for[SSA_NAME_VERSION (name)] = n;
5069 bitmap_set_bit (need_assert_for, SSA_NAME_VERSION (name));
5072 /* (COND_OP0 COND_CODE COND_OP1) is a predicate which uses NAME.
5073 Extract a suitable test code and value and store them into *CODE_P and
5074 *VAL_P so the predicate is normalized to NAME *CODE_P *VAL_P.
5076 If no extraction was possible, return FALSE, otherwise return TRUE.
5078 If INVERT is true, then we invert the result stored into *CODE_P. */
5080 static bool
5081 extract_code_and_val_from_cond_with_ops (tree name, enum tree_code cond_code,
5082 tree cond_op0, tree cond_op1,
5083 bool invert, enum tree_code *code_p,
5084 tree *val_p)
5086 enum tree_code comp_code;
5087 tree val;
5089 /* Otherwise, we have a comparison of the form NAME COMP VAL
5090 or VAL COMP NAME. */
5091 if (name == cond_op1)
5093 /* If the predicate is of the form VAL COMP NAME, flip
5094 COMP around because we need to register NAME as the
5095 first operand in the predicate. */
5096 comp_code = swap_tree_comparison (cond_code);
5097 val = cond_op0;
5099 else
5101 /* The comparison is of the form NAME COMP VAL, so the
5102 comparison code remains unchanged. */
5103 comp_code = cond_code;
5104 val = cond_op1;
5107 /* Invert the comparison code as necessary. */
5108 if (invert)
5109 comp_code = invert_tree_comparison (comp_code, 0);
5111 /* VRP does not handle float types. */
5112 if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (val)))
5113 return false;
5115 /* Do not register always-false predicates.
5116 FIXME: this works around a limitation in fold() when dealing with
5117 enumerations. Given 'enum { N1, N2 } x;', fold will not
5118 fold 'if (x > N2)' to 'if (0)'. */
5119 if ((comp_code == GT_EXPR || comp_code == LT_EXPR)
5120 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
5122 tree min = TYPE_MIN_VALUE (TREE_TYPE (val));
5123 tree max = TYPE_MAX_VALUE (TREE_TYPE (val));
5125 if (comp_code == GT_EXPR
5126 && (!max
5127 || compare_values (val, max) == 0))
5128 return false;
5130 if (comp_code == LT_EXPR
5131 && (!min
5132 || compare_values (val, min) == 0))
5133 return false;
5135 *code_p = comp_code;
5136 *val_p = val;
5137 return true;
5140 /* Find out smallest RES where RES > VAL && (RES & MASK) == RES, if any
5141 (otherwise return VAL). VAL and MASK must be zero-extended for
5142 precision PREC. If SGNBIT is non-zero, first xor VAL with SGNBIT
5143 (to transform signed values into unsigned) and at the end xor
5144 SGNBIT back. */
5146 static wide_int
5147 masked_increment (const wide_int &val_in, const wide_int &mask,
5148 const wide_int &sgnbit, unsigned int prec)
5150 wide_int bit = wi::one (prec), res;
5151 unsigned int i;
5153 wide_int val = val_in ^ sgnbit;
5154 for (i = 0; i < prec; i++, bit += bit)
5156 res = mask;
5157 if ((res & bit) == 0)
5158 continue;
5159 res = bit - 1;
5160 res = (val + bit).and_not (res);
5161 res &= mask;
5162 if (wi::gtu_p (res, val))
5163 return res ^ sgnbit;
5165 return val ^ sgnbit;
5168 /* Try to register an edge assertion for SSA name NAME on edge E for
5169 the condition COND contributing to the conditional jump pointed to by BSI.
5170 Invert the condition COND if INVERT is true. */
5172 static void
5173 register_edge_assert_for_2 (tree name, edge e, gimple_stmt_iterator bsi,
5174 enum tree_code cond_code,
5175 tree cond_op0, tree cond_op1, bool invert)
5177 tree val;
5178 enum tree_code comp_code;
5180 if (!extract_code_and_val_from_cond_with_ops (name, cond_code,
5181 cond_op0,
5182 cond_op1,
5183 invert, &comp_code, &val))
5184 return;
5186 /* Only register an ASSERT_EXPR if NAME was found in the sub-graph
5187 reachable from E. */
5188 if (live_on_edge (e, name)
5189 && !has_single_use (name))
5190 register_new_assert_for (name, name, comp_code, val, NULL, e, bsi);
5192 /* In the case of NAME <= CST and NAME being defined as
5193 NAME = (unsigned) NAME2 + CST2 we can assert NAME2 >= -CST2
5194 and NAME2 <= CST - CST2. We can do the same for NAME > CST.
5195 This catches range and anti-range tests. */
5196 if ((comp_code == LE_EXPR
5197 || comp_code == GT_EXPR)
5198 && TREE_CODE (val) == INTEGER_CST
5199 && TYPE_UNSIGNED (TREE_TYPE (val)))
5201 gimple def_stmt = SSA_NAME_DEF_STMT (name);
5202 tree cst2 = NULL_TREE, name2 = NULL_TREE, name3 = NULL_TREE;
5204 /* Extract CST2 from the (optional) addition. */
5205 if (is_gimple_assign (def_stmt)
5206 && gimple_assign_rhs_code (def_stmt) == PLUS_EXPR)
5208 name2 = gimple_assign_rhs1 (def_stmt);
5209 cst2 = gimple_assign_rhs2 (def_stmt);
5210 if (TREE_CODE (name2) == SSA_NAME
5211 && TREE_CODE (cst2) == INTEGER_CST)
5212 def_stmt = SSA_NAME_DEF_STMT (name2);
5215 /* Extract NAME2 from the (optional) sign-changing cast. */
5216 if (gimple_assign_cast_p (def_stmt))
5218 if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt))
5219 && ! TYPE_UNSIGNED (TREE_TYPE (gimple_assign_rhs1 (def_stmt)))
5220 && (TYPE_PRECISION (gimple_expr_type (def_stmt))
5221 == TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt)))))
5222 name3 = gimple_assign_rhs1 (def_stmt);
5225 /* If name3 is used later, create an ASSERT_EXPR for it. */
5226 if (name3 != NULL_TREE
5227 && TREE_CODE (name3) == SSA_NAME
5228 && (cst2 == NULL_TREE
5229 || TREE_CODE (cst2) == INTEGER_CST)
5230 && INTEGRAL_TYPE_P (TREE_TYPE (name3))
5231 && live_on_edge (e, name3)
5232 && !has_single_use (name3))
5234 tree tmp;
5236 /* Build an expression for the range test. */
5237 tmp = build1 (NOP_EXPR, TREE_TYPE (name), name3);
5238 if (cst2 != NULL_TREE)
5239 tmp = build2 (PLUS_EXPR, TREE_TYPE (name), tmp, cst2);
5241 if (dump_file)
5243 fprintf (dump_file, "Adding assert for ");
5244 print_generic_expr (dump_file, name3, 0);
5245 fprintf (dump_file, " from ");
5246 print_generic_expr (dump_file, tmp, 0);
5247 fprintf (dump_file, "\n");
5250 register_new_assert_for (name3, tmp, comp_code, val, NULL, e, bsi);
5253 /* If name2 is used later, create an ASSERT_EXPR for it. */
5254 if (name2 != NULL_TREE
5255 && TREE_CODE (name2) == SSA_NAME
5256 && TREE_CODE (cst2) == INTEGER_CST
5257 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
5258 && live_on_edge (e, name2)
5259 && !has_single_use (name2))
5261 tree tmp;
5263 /* Build an expression for the range test. */
5264 tmp = name2;
5265 if (TREE_TYPE (name) != TREE_TYPE (name2))
5266 tmp = build1 (NOP_EXPR, TREE_TYPE (name), tmp);
5267 if (cst2 != NULL_TREE)
5268 tmp = build2 (PLUS_EXPR, TREE_TYPE (name), tmp, cst2);
5270 if (dump_file)
5272 fprintf (dump_file, "Adding assert for ");
5273 print_generic_expr (dump_file, name2, 0);
5274 fprintf (dump_file, " from ");
5275 print_generic_expr (dump_file, tmp, 0);
5276 fprintf (dump_file, "\n");
5279 register_new_assert_for (name2, tmp, comp_code, val, NULL, e, bsi);
5283 /* In the case of post-in/decrement tests like if (i++) ... and uses
5284 of the in/decremented value on the edge the extra name we want to
5285 assert for is not on the def chain of the name compared. Instead
5286 it is in the set of use stmts.
5287 Similar cases happen for conversions that were simplified through
5288 fold_{sign_changed,widened}_comparison. */
5289 if ((comp_code == NE_EXPR
5290 || comp_code == EQ_EXPR)
5291 && TREE_CODE (val) == INTEGER_CST)
5293 imm_use_iterator ui;
5294 gimple use_stmt;
5295 FOR_EACH_IMM_USE_STMT (use_stmt, ui, name)
5297 if (!is_gimple_assign (use_stmt))
5298 continue;
5300 /* Cut off to use-stmts that are dominating the predecessor. */
5301 if (!dominated_by_p (CDI_DOMINATORS, e->src, gimple_bb (use_stmt)))
5302 continue;
5304 tree name2 = gimple_assign_lhs (use_stmt);
5305 if (TREE_CODE (name2) != SSA_NAME
5306 || !live_on_edge (e, name2))
5307 continue;
5309 enum tree_code code = gimple_assign_rhs_code (use_stmt);
5310 tree cst;
5311 if (code == PLUS_EXPR
5312 || code == MINUS_EXPR)
5314 cst = gimple_assign_rhs2 (use_stmt);
5315 if (TREE_CODE (cst) != INTEGER_CST)
5316 continue;
5317 cst = int_const_binop (code, val, cst);
5319 else if (CONVERT_EXPR_CODE_P (code))
5321 /* For truncating conversions we cannot record
5322 an inequality. */
5323 if (comp_code == NE_EXPR
5324 && (TYPE_PRECISION (TREE_TYPE (name2))
5325 < TYPE_PRECISION (TREE_TYPE (name))))
5326 continue;
5327 cst = fold_convert (TREE_TYPE (name2), val);
5329 else
5330 continue;
5332 if (TREE_OVERFLOW_P (cst))
5333 cst = drop_tree_overflow (cst);
5334 register_new_assert_for (name2, name2, comp_code, cst,
5335 NULL, e, bsi);
5339 if (TREE_CODE_CLASS (comp_code) == tcc_comparison
5340 && TREE_CODE (val) == INTEGER_CST)
5342 gimple def_stmt = SSA_NAME_DEF_STMT (name);
5343 tree name2 = NULL_TREE, names[2], cst2 = NULL_TREE;
5344 tree val2 = NULL_TREE;
5345 unsigned int prec = TYPE_PRECISION (TREE_TYPE (val));
5346 wide_int mask = wi::zero (prec);
5347 unsigned int nprec = prec;
5348 enum tree_code rhs_code = ERROR_MARK;
5350 if (is_gimple_assign (def_stmt))
5351 rhs_code = gimple_assign_rhs_code (def_stmt);
5353 /* Add asserts for NAME cmp CST and NAME being defined
5354 as NAME = (int) NAME2. */
5355 if (!TYPE_UNSIGNED (TREE_TYPE (val))
5356 && (comp_code == LE_EXPR || comp_code == LT_EXPR
5357 || comp_code == GT_EXPR || comp_code == GE_EXPR)
5358 && gimple_assign_cast_p (def_stmt))
5360 name2 = gimple_assign_rhs1 (def_stmt);
5361 if (CONVERT_EXPR_CODE_P (rhs_code)
5362 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
5363 && TYPE_UNSIGNED (TREE_TYPE (name2))
5364 && prec == TYPE_PRECISION (TREE_TYPE (name2))
5365 && (comp_code == LE_EXPR || comp_code == GT_EXPR
5366 || !tree_int_cst_equal (val,
5367 TYPE_MIN_VALUE (TREE_TYPE (val))))
5368 && live_on_edge (e, name2)
5369 && !has_single_use (name2))
5371 tree tmp, cst;
5372 enum tree_code new_comp_code = comp_code;
5374 cst = fold_convert (TREE_TYPE (name2),
5375 TYPE_MIN_VALUE (TREE_TYPE (val)));
5376 /* Build an expression for the range test. */
5377 tmp = build2 (PLUS_EXPR, TREE_TYPE (name2), name2, cst);
5378 cst = fold_build2 (PLUS_EXPR, TREE_TYPE (name2), cst,
5379 fold_convert (TREE_TYPE (name2), val));
5380 if (comp_code == LT_EXPR || comp_code == GE_EXPR)
5382 new_comp_code = comp_code == LT_EXPR ? LE_EXPR : GT_EXPR;
5383 cst = fold_build2 (MINUS_EXPR, TREE_TYPE (name2), cst,
5384 build_int_cst (TREE_TYPE (name2), 1));
5387 if (dump_file)
5389 fprintf (dump_file, "Adding assert for ");
5390 print_generic_expr (dump_file, name2, 0);
5391 fprintf (dump_file, " from ");
5392 print_generic_expr (dump_file, tmp, 0);
5393 fprintf (dump_file, "\n");
5396 register_new_assert_for (name2, tmp, new_comp_code, cst, NULL,
5397 e, bsi);
5401 /* Add asserts for NAME cmp CST and NAME being defined as
5402 NAME = NAME2 >> CST2.
5404 Extract CST2 from the right shift. */
5405 if (rhs_code == RSHIFT_EXPR)
5407 name2 = gimple_assign_rhs1 (def_stmt);
5408 cst2 = gimple_assign_rhs2 (def_stmt);
5409 if (TREE_CODE (name2) == SSA_NAME
5410 && tree_fits_uhwi_p (cst2)
5411 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
5412 && IN_RANGE (tree_to_uhwi (cst2), 1, prec - 1)
5413 && prec == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (val)))
5414 && live_on_edge (e, name2)
5415 && !has_single_use (name2))
5417 mask = wi::mask (tree_to_uhwi (cst2), false, prec);
5418 val2 = fold_binary (LSHIFT_EXPR, TREE_TYPE (val), val, cst2);
5421 if (val2 != NULL_TREE
5422 && TREE_CODE (val2) == INTEGER_CST
5423 && simple_cst_equal (fold_build2 (RSHIFT_EXPR,
5424 TREE_TYPE (val),
5425 val2, cst2), val))
5427 enum tree_code new_comp_code = comp_code;
5428 tree tmp, new_val;
5430 tmp = name2;
5431 if (comp_code == EQ_EXPR || comp_code == NE_EXPR)
5433 if (!TYPE_UNSIGNED (TREE_TYPE (val)))
5435 tree type = build_nonstandard_integer_type (prec, 1);
5436 tmp = build1 (NOP_EXPR, type, name2);
5437 val2 = fold_convert (type, val2);
5439 tmp = fold_build2 (MINUS_EXPR, TREE_TYPE (tmp), tmp, val2);
5440 new_val = wide_int_to_tree (TREE_TYPE (tmp), mask);
5441 new_comp_code = comp_code == EQ_EXPR ? LE_EXPR : GT_EXPR;
5443 else if (comp_code == LT_EXPR || comp_code == GE_EXPR)
5445 wide_int minval
5446 = wi::min_value (prec, TYPE_SIGN (TREE_TYPE (val)));
5447 new_val = val2;
5448 if (minval == new_val)
5449 new_val = NULL_TREE;
5451 else
5453 wide_int maxval
5454 = wi::max_value (prec, TYPE_SIGN (TREE_TYPE (val)));
5455 mask |= val2;
5456 if (mask == maxval)
5457 new_val = NULL_TREE;
5458 else
5459 new_val = wide_int_to_tree (TREE_TYPE (val2), mask);
5462 if (new_val)
5464 if (dump_file)
5466 fprintf (dump_file, "Adding assert for ");
5467 print_generic_expr (dump_file, name2, 0);
5468 fprintf (dump_file, " from ");
5469 print_generic_expr (dump_file, tmp, 0);
5470 fprintf (dump_file, "\n");
5473 register_new_assert_for (name2, tmp, new_comp_code, new_val,
5474 NULL, e, bsi);
5478 /* Add asserts for NAME cmp CST and NAME being defined as
5479 NAME = NAME2 & CST2.
5481 Extract CST2 from the and.
5483 Also handle
5484 NAME = (unsigned) NAME2;
5485 casts where NAME's type is unsigned and has smaller precision
5486 than NAME2's type as if it was NAME = NAME2 & MASK. */
5487 names[0] = NULL_TREE;
5488 names[1] = NULL_TREE;
5489 cst2 = NULL_TREE;
5490 if (rhs_code == BIT_AND_EXPR
5491 || (CONVERT_EXPR_CODE_P (rhs_code)
5492 && TREE_CODE (TREE_TYPE (val)) == INTEGER_TYPE
5493 && TYPE_UNSIGNED (TREE_TYPE (val))
5494 && TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt)))
5495 > prec))
5497 name2 = gimple_assign_rhs1 (def_stmt);
5498 if (rhs_code == BIT_AND_EXPR)
5499 cst2 = gimple_assign_rhs2 (def_stmt);
5500 else
5502 cst2 = TYPE_MAX_VALUE (TREE_TYPE (val));
5503 nprec = TYPE_PRECISION (TREE_TYPE (name2));
5505 if (TREE_CODE (name2) == SSA_NAME
5506 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
5507 && TREE_CODE (cst2) == INTEGER_CST
5508 && !integer_zerop (cst2)
5509 && (nprec > 1
5510 || TYPE_UNSIGNED (TREE_TYPE (val))))
5512 gimple def_stmt2 = SSA_NAME_DEF_STMT (name2);
5513 if (gimple_assign_cast_p (def_stmt2))
5515 names[1] = gimple_assign_rhs1 (def_stmt2);
5516 if (!CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt2))
5517 || !INTEGRAL_TYPE_P (TREE_TYPE (names[1]))
5518 || (TYPE_PRECISION (TREE_TYPE (name2))
5519 != TYPE_PRECISION (TREE_TYPE (names[1])))
5520 || !live_on_edge (e, names[1])
5521 || has_single_use (names[1]))
5522 names[1] = NULL_TREE;
5524 if (live_on_edge (e, name2)
5525 && !has_single_use (name2))
5526 names[0] = name2;
5529 if (names[0] || names[1])
5531 wide_int minv, maxv, valv, cst2v;
5532 wide_int tem, sgnbit;
5533 bool valid_p = false, valn, cst2n;
5534 enum tree_code ccode = comp_code;
5536 valv = wide_int::from (val, nprec, UNSIGNED);
5537 cst2v = wide_int::from (cst2, nprec, UNSIGNED);
5538 valn = wi::neg_p (valv, TYPE_SIGN (TREE_TYPE (val)));
5539 cst2n = wi::neg_p (cst2v, TYPE_SIGN (TREE_TYPE (val)));
5540 /* If CST2 doesn't have most significant bit set,
5541 but VAL is negative, we have comparison like
5542 if ((x & 0x123) > -4) (always true). Just give up. */
5543 if (!cst2n && valn)
5544 ccode = ERROR_MARK;
5545 if (cst2n)
5546 sgnbit = wi::set_bit_in_zero (nprec - 1, nprec);
5547 else
5548 sgnbit = wi::zero (nprec);
5549 minv = valv & cst2v;
5550 switch (ccode)
5552 case EQ_EXPR:
5553 /* Minimum unsigned value for equality is VAL & CST2
5554 (should be equal to VAL, otherwise we probably should
5555 have folded the comparison into false) and
5556 maximum unsigned value is VAL | ~CST2. */
5557 maxv = valv | ~cst2v;
5558 valid_p = true;
5559 break;
5561 case NE_EXPR:
5562 tem = valv | ~cst2v;
5563 /* If VAL is 0, handle (X & CST2) != 0 as (X & CST2) > 0U. */
5564 if (valv == 0)
5566 cst2n = false;
5567 sgnbit = wi::zero (nprec);
5568 goto gt_expr;
5570 /* If (VAL | ~CST2) is all ones, handle it as
5571 (X & CST2) < VAL. */
5572 if (tem == -1)
5574 cst2n = false;
5575 valn = false;
5576 sgnbit = wi::zero (nprec);
5577 goto lt_expr;
5579 if (!cst2n && wi::neg_p (cst2v))
5580 sgnbit = wi::set_bit_in_zero (nprec - 1, nprec);
5581 if (sgnbit != 0)
5583 if (valv == sgnbit)
5585 cst2n = true;
5586 valn = true;
5587 goto gt_expr;
5589 if (tem == wi::mask (nprec - 1, false, nprec))
5591 cst2n = true;
5592 goto lt_expr;
5594 if (!cst2n)
5595 sgnbit = wi::zero (nprec);
5597 break;
5599 case GE_EXPR:
5600 /* Minimum unsigned value for >= if (VAL & CST2) == VAL
5601 is VAL and maximum unsigned value is ~0. For signed
5602 comparison, if CST2 doesn't have most significant bit
5603 set, handle it similarly. If CST2 has MSB set,
5604 the minimum is the same, and maximum is ~0U/2. */
5605 if (minv != valv)
5607 /* If (VAL & CST2) != VAL, X & CST2 can't be equal to
5608 VAL. */
5609 minv = masked_increment (valv, cst2v, sgnbit, nprec);
5610 if (minv == valv)
5611 break;
5613 maxv = wi::mask (nprec - (cst2n ? 1 : 0), false, nprec);
5614 valid_p = true;
5615 break;
5617 case GT_EXPR:
5618 gt_expr:
5619 /* Find out smallest MINV where MINV > VAL
5620 && (MINV & CST2) == MINV, if any. If VAL is signed and
5621 CST2 has MSB set, compute it biased by 1 << (nprec - 1). */
5622 minv = masked_increment (valv, cst2v, sgnbit, nprec);
5623 if (minv == valv)
5624 break;
5625 maxv = wi::mask (nprec - (cst2n ? 1 : 0), false, nprec);
5626 valid_p = true;
5627 break;
5629 case LE_EXPR:
5630 /* Minimum unsigned value for <= is 0 and maximum
5631 unsigned value is VAL | ~CST2 if (VAL & CST2) == VAL.
5632 Otherwise, find smallest VAL2 where VAL2 > VAL
5633 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
5634 as maximum.
5635 For signed comparison, if CST2 doesn't have most
5636 significant bit set, handle it similarly. If CST2 has
5637 MSB set, the maximum is the same and minimum is INT_MIN. */
5638 if (minv == valv)
5639 maxv = valv;
5640 else
5642 maxv = masked_increment (valv, cst2v, sgnbit, nprec);
5643 if (maxv == valv)
5644 break;
5645 maxv -= 1;
5647 maxv |= ~cst2v;
5648 minv = sgnbit;
5649 valid_p = true;
5650 break;
5652 case LT_EXPR:
5653 lt_expr:
5654 /* Minimum unsigned value for < is 0 and maximum
5655 unsigned value is (VAL-1) | ~CST2 if (VAL & CST2) == VAL.
5656 Otherwise, find smallest VAL2 where VAL2 > VAL
5657 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
5658 as maximum.
5659 For signed comparison, if CST2 doesn't have most
5660 significant bit set, handle it similarly. If CST2 has
5661 MSB set, the maximum is the same and minimum is INT_MIN. */
5662 if (minv == valv)
5664 if (valv == sgnbit)
5665 break;
5666 maxv = valv;
5668 else
5670 maxv = masked_increment (valv, cst2v, sgnbit, nprec);
5671 if (maxv == valv)
5672 break;
5674 maxv -= 1;
5675 maxv |= ~cst2v;
5676 minv = sgnbit;
5677 valid_p = true;
5678 break;
5680 default:
5681 break;
5683 if (valid_p
5684 && (maxv - minv) != -1)
5686 tree tmp, new_val, type;
5687 int i;
5689 for (i = 0; i < 2; i++)
5690 if (names[i])
5692 wide_int maxv2 = maxv;
5693 tmp = names[i];
5694 type = TREE_TYPE (names[i]);
5695 if (!TYPE_UNSIGNED (type))
5697 type = build_nonstandard_integer_type (nprec, 1);
5698 tmp = build1 (NOP_EXPR, type, names[i]);
5700 if (minv != 0)
5702 tmp = build2 (PLUS_EXPR, type, tmp,
5703 wide_int_to_tree (type, -minv));
5704 maxv2 = maxv - minv;
5706 new_val = wide_int_to_tree (type, maxv2);
5708 if (dump_file)
5710 fprintf (dump_file, "Adding assert for ");
5711 print_generic_expr (dump_file, names[i], 0);
5712 fprintf (dump_file, " from ");
5713 print_generic_expr (dump_file, tmp, 0);
5714 fprintf (dump_file, "\n");
5717 register_new_assert_for (names[i], tmp, LE_EXPR,
5718 new_val, NULL, e, bsi);
5725 /* OP is an operand of a truth value expression which is known to have
5726 a particular value. Register any asserts for OP and for any
5727 operands in OP's defining statement.
5729 If CODE is EQ_EXPR, then we want to register OP is zero (false),
5730 if CODE is NE_EXPR, then we want to register OP is nonzero (true). */
5732 static void
5733 register_edge_assert_for_1 (tree op, enum tree_code code,
5734 edge e, gimple_stmt_iterator bsi)
5736 gimple op_def;
5737 tree val;
5738 enum tree_code rhs_code;
5740 /* We only care about SSA_NAMEs. */
5741 if (TREE_CODE (op) != SSA_NAME)
5742 return;
5744 /* We know that OP will have a zero or nonzero value. If OP is used
5745 more than once go ahead and register an assert for OP. */
5746 if (live_on_edge (e, op)
5747 && !has_single_use (op))
5749 val = build_int_cst (TREE_TYPE (op), 0);
5750 register_new_assert_for (op, op, code, val, NULL, e, bsi);
5753 /* Now look at how OP is set. If it's set from a comparison,
5754 a truth operation or some bit operations, then we may be able
5755 to register information about the operands of that assignment. */
5756 op_def = SSA_NAME_DEF_STMT (op);
5757 if (gimple_code (op_def) != GIMPLE_ASSIGN)
5758 return;
5760 rhs_code = gimple_assign_rhs_code (op_def);
5762 if (TREE_CODE_CLASS (rhs_code) == tcc_comparison)
5764 bool invert = (code == EQ_EXPR ? true : false);
5765 tree op0 = gimple_assign_rhs1 (op_def);
5766 tree op1 = gimple_assign_rhs2 (op_def);
5768 if (TREE_CODE (op0) == SSA_NAME)
5769 register_edge_assert_for_2 (op0, e, bsi, rhs_code, op0, op1, invert);
5770 if (TREE_CODE (op1) == SSA_NAME)
5771 register_edge_assert_for_2 (op1, e, bsi, rhs_code, op0, op1, invert);
5773 else if ((code == NE_EXPR
5774 && gimple_assign_rhs_code (op_def) == BIT_AND_EXPR)
5775 || (code == EQ_EXPR
5776 && gimple_assign_rhs_code (op_def) == BIT_IOR_EXPR))
5778 /* Recurse on each operand. */
5779 tree op0 = gimple_assign_rhs1 (op_def);
5780 tree op1 = gimple_assign_rhs2 (op_def);
5781 if (TREE_CODE (op0) == SSA_NAME
5782 && has_single_use (op0))
5783 register_edge_assert_for_1 (op0, code, e, bsi);
5784 if (TREE_CODE (op1) == SSA_NAME
5785 && has_single_use (op1))
5786 register_edge_assert_for_1 (op1, code, e, bsi);
5788 else if (gimple_assign_rhs_code (op_def) == BIT_NOT_EXPR
5789 && TYPE_PRECISION (TREE_TYPE (gimple_assign_lhs (op_def))) == 1)
5791 /* Recurse, flipping CODE. */
5792 code = invert_tree_comparison (code, false);
5793 register_edge_assert_for_1 (gimple_assign_rhs1 (op_def), code, e, bsi);
5795 else if (gimple_assign_rhs_code (op_def) == SSA_NAME)
5797 /* Recurse through the copy. */
5798 register_edge_assert_for_1 (gimple_assign_rhs1 (op_def), code, e, bsi);
5800 else if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (op_def)))
5802 /* Recurse through the type conversion, unless it is a narrowing
5803 conversion or conversion from non-integral type. */
5804 tree rhs = gimple_assign_rhs1 (op_def);
5805 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs))
5806 && (TYPE_PRECISION (TREE_TYPE (rhs))
5807 <= TYPE_PRECISION (TREE_TYPE (op))))
5808 register_edge_assert_for_1 (rhs, code, e, bsi);
5812 /* Try to register an edge assertion for SSA name NAME on edge E for
5813 the condition COND contributing to the conditional jump pointed to by
5814 SI. */
5816 static void
5817 register_edge_assert_for (tree name, edge e, gimple_stmt_iterator si,
5818 enum tree_code cond_code, tree cond_op0,
5819 tree cond_op1)
5821 tree val;
5822 enum tree_code comp_code;
5823 bool is_else_edge = (e->flags & EDGE_FALSE_VALUE) != 0;
5825 /* Do not attempt to infer anything in names that flow through
5826 abnormal edges. */
5827 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name))
5828 return;
5830 if (!extract_code_and_val_from_cond_with_ops (name, cond_code,
5831 cond_op0, cond_op1,
5832 is_else_edge,
5833 &comp_code, &val))
5834 return;
5836 /* Register ASSERT_EXPRs for name. */
5837 register_edge_assert_for_2 (name, e, si, cond_code, cond_op0,
5838 cond_op1, is_else_edge);
5841 /* If COND is effectively an equality test of an SSA_NAME against
5842 the value zero or one, then we may be able to assert values
5843 for SSA_NAMEs which flow into COND. */
5845 /* In the case of NAME == 1 or NAME != 0, for BIT_AND_EXPR defining
5846 statement of NAME we can assert both operands of the BIT_AND_EXPR
5847 have nonzero value. */
5848 if (((comp_code == EQ_EXPR && integer_onep (val))
5849 || (comp_code == NE_EXPR && integer_zerop (val))))
5851 gimple def_stmt = SSA_NAME_DEF_STMT (name);
5853 if (is_gimple_assign (def_stmt)
5854 && gimple_assign_rhs_code (def_stmt) == BIT_AND_EXPR)
5856 tree op0 = gimple_assign_rhs1 (def_stmt);
5857 tree op1 = gimple_assign_rhs2 (def_stmt);
5858 register_edge_assert_for_1 (op0, NE_EXPR, e, si);
5859 register_edge_assert_for_1 (op1, NE_EXPR, e, si);
5863 /* In the case of NAME == 0 or NAME != 1, for BIT_IOR_EXPR defining
5864 statement of NAME we can assert both operands of the BIT_IOR_EXPR
5865 have zero value. */
5866 if (((comp_code == EQ_EXPR && integer_zerop (val))
5867 || (comp_code == NE_EXPR && integer_onep (val))))
5869 gimple def_stmt = SSA_NAME_DEF_STMT (name);
5871 /* For BIT_IOR_EXPR only if NAME == 0 both operands have
5872 necessarily zero value, or if type-precision is one. */
5873 if (is_gimple_assign (def_stmt)
5874 && (gimple_assign_rhs_code (def_stmt) == BIT_IOR_EXPR
5875 && (TYPE_PRECISION (TREE_TYPE (name)) == 1
5876 || comp_code == EQ_EXPR)))
5878 tree op0 = gimple_assign_rhs1 (def_stmt);
5879 tree op1 = gimple_assign_rhs2 (def_stmt);
5880 register_edge_assert_for_1 (op0, EQ_EXPR, e, si);
5881 register_edge_assert_for_1 (op1, EQ_EXPR, e, si);
5887 /* Determine whether the outgoing edges of BB should receive an
5888 ASSERT_EXPR for each of the operands of BB's LAST statement.
5889 The last statement of BB must be a COND_EXPR.
5891 If any of the sub-graphs rooted at BB have an interesting use of
5892 the predicate operands, an assert location node is added to the
5893 list of assertions for the corresponding operands. */
5895 static void
5896 find_conditional_asserts (basic_block bb, gcond *last)
5898 gimple_stmt_iterator bsi;
5899 tree op;
5900 edge_iterator ei;
5901 edge e;
5902 ssa_op_iter iter;
5904 bsi = gsi_for_stmt (last);
5906 /* Look for uses of the operands in each of the sub-graphs
5907 rooted at BB. We need to check each of the outgoing edges
5908 separately, so that we know what kind of ASSERT_EXPR to
5909 insert. */
5910 FOR_EACH_EDGE (e, ei, bb->succs)
5912 if (e->dest == bb)
5913 continue;
5915 /* Register the necessary assertions for each operand in the
5916 conditional predicate. */
5917 FOR_EACH_SSA_TREE_OPERAND (op, last, iter, SSA_OP_USE)
5918 register_edge_assert_for (op, e, bsi,
5919 gimple_cond_code (last),
5920 gimple_cond_lhs (last),
5921 gimple_cond_rhs (last));
5925 struct case_info
5927 tree expr;
5928 basic_block bb;
5931 /* Compare two case labels sorting first by the destination bb index
5932 and then by the case value. */
5934 static int
5935 compare_case_labels (const void *p1, const void *p2)
5937 const struct case_info *ci1 = (const struct case_info *) p1;
5938 const struct case_info *ci2 = (const struct case_info *) p2;
5939 int idx1 = ci1->bb->index;
5940 int idx2 = ci2->bb->index;
5942 if (idx1 < idx2)
5943 return -1;
5944 else if (idx1 == idx2)
5946 /* Make sure the default label is first in a group. */
5947 if (!CASE_LOW (ci1->expr))
5948 return -1;
5949 else if (!CASE_LOW (ci2->expr))
5950 return 1;
5951 else
5952 return tree_int_cst_compare (CASE_LOW (ci1->expr),
5953 CASE_LOW (ci2->expr));
5955 else
5956 return 1;
5959 /* Determine whether the outgoing edges of BB should receive an
5960 ASSERT_EXPR for each of the operands of BB's LAST statement.
5961 The last statement of BB must be a SWITCH_EXPR.
5963 If any of the sub-graphs rooted at BB have an interesting use of
5964 the predicate operands, an assert location node is added to the
5965 list of assertions for the corresponding operands. */
5967 static void
5968 find_switch_asserts (basic_block bb, gswitch *last)
5970 gimple_stmt_iterator bsi;
5971 tree op;
5972 edge e;
5973 struct case_info *ci;
5974 size_t n = gimple_switch_num_labels (last);
5975 #if GCC_VERSION >= 4000
5976 unsigned int idx;
5977 #else
5978 /* Work around GCC 3.4 bug (PR 37086). */
5979 volatile unsigned int idx;
5980 #endif
5982 bsi = gsi_for_stmt (last);
5983 op = gimple_switch_index (last);
5984 if (TREE_CODE (op) != SSA_NAME)
5985 return;
5987 /* Build a vector of case labels sorted by destination label. */
5988 ci = XNEWVEC (struct case_info, n);
5989 for (idx = 0; idx < n; ++idx)
5991 ci[idx].expr = gimple_switch_label (last, idx);
5992 ci[idx].bb = label_to_block (CASE_LABEL (ci[idx].expr));
5994 qsort (ci, n, sizeof (struct case_info), compare_case_labels);
5996 for (idx = 0; idx < n; ++idx)
5998 tree min, max;
5999 tree cl = ci[idx].expr;
6000 basic_block cbb = ci[idx].bb;
6002 min = CASE_LOW (cl);
6003 max = CASE_HIGH (cl);
6005 /* If there are multiple case labels with the same destination
6006 we need to combine them to a single value range for the edge. */
6007 if (idx + 1 < n && cbb == ci[idx + 1].bb)
6009 /* Skip labels until the last of the group. */
6010 do {
6011 ++idx;
6012 } while (idx < n && cbb == ci[idx].bb);
6013 --idx;
6015 /* Pick up the maximum of the case label range. */
6016 if (CASE_HIGH (ci[idx].expr))
6017 max = CASE_HIGH (ci[idx].expr);
6018 else
6019 max = CASE_LOW (ci[idx].expr);
6022 /* Nothing to do if the range includes the default label until we
6023 can register anti-ranges. */
6024 if (min == NULL_TREE)
6025 continue;
6027 /* Find the edge to register the assert expr on. */
6028 e = find_edge (bb, cbb);
6030 /* Register the necessary assertions for the operand in the
6031 SWITCH_EXPR. */
6032 register_edge_assert_for (op, e, bsi,
6033 max ? GE_EXPR : EQ_EXPR,
6034 op, fold_convert (TREE_TYPE (op), min));
6035 if (max)
6036 register_edge_assert_for (op, e, bsi, LE_EXPR, op,
6037 fold_convert (TREE_TYPE (op), max));
6040 XDELETEVEC (ci);
6044 /* Traverse all the statements in block BB looking for statements that
6045 may generate useful assertions for the SSA names in their operand.
6046 If a statement produces a useful assertion A for name N_i, then the
6047 list of assertions already generated for N_i is scanned to
6048 determine if A is actually needed.
6050 If N_i already had the assertion A at a location dominating the
6051 current location, then nothing needs to be done. Otherwise, the
6052 new location for A is recorded instead.
6054 1- For every statement S in BB, all the variables used by S are
6055 added to bitmap FOUND_IN_SUBGRAPH.
6057 2- If statement S uses an operand N in a way that exposes a known
6058 value range for N, then if N was not already generated by an
6059 ASSERT_EXPR, create a new assert location for N. For instance,
6060 if N is a pointer and the statement dereferences it, we can
6061 assume that N is not NULL.
6063 3- COND_EXPRs are a special case of #2. We can derive range
6064 information from the predicate but need to insert different
6065 ASSERT_EXPRs for each of the sub-graphs rooted at the
6066 conditional block. If the last statement of BB is a conditional
6067 expression of the form 'X op Y', then
6069 a) Remove X and Y from the set FOUND_IN_SUBGRAPH.
6071 b) If the conditional is the only entry point to the sub-graph
6072 corresponding to the THEN_CLAUSE, recurse into it. On
6073 return, if X and/or Y are marked in FOUND_IN_SUBGRAPH, then
6074 an ASSERT_EXPR is added for the corresponding variable.
6076 c) Repeat step (b) on the ELSE_CLAUSE.
6078 d) Mark X and Y in FOUND_IN_SUBGRAPH.
6080 For instance,
6082 if (a == 9)
6083 b = a;
6084 else
6085 b = c + 1;
6087 In this case, an assertion on the THEN clause is useful to
6088 determine that 'a' is always 9 on that edge. However, an assertion
6089 on the ELSE clause would be unnecessary.
6091 4- If BB does not end in a conditional expression, then we recurse
6092 into BB's dominator children.
6094 At the end of the recursive traversal, every SSA name will have a
6095 list of locations where ASSERT_EXPRs should be added. When a new
6096 location for name N is found, it is registered by calling
6097 register_new_assert_for. That function keeps track of all the
6098 registered assertions to prevent adding unnecessary assertions.
6099 For instance, if a pointer P_4 is dereferenced more than once in a
6100 dominator tree, only the location dominating all the dereference of
6101 P_4 will receive an ASSERT_EXPR. */
6103 static void
6104 find_assert_locations_1 (basic_block bb, sbitmap live)
6106 gimple last;
6108 last = last_stmt (bb);
6110 /* If BB's last statement is a conditional statement involving integer
6111 operands, determine if we need to add ASSERT_EXPRs. */
6112 if (last
6113 && gimple_code (last) == GIMPLE_COND
6114 && !fp_predicate (last)
6115 && !ZERO_SSA_OPERANDS (last, SSA_OP_USE))
6116 find_conditional_asserts (bb, as_a <gcond *> (last));
6118 /* If BB's last statement is a switch statement involving integer
6119 operands, determine if we need to add ASSERT_EXPRs. */
6120 if (last
6121 && gimple_code (last) == GIMPLE_SWITCH
6122 && !ZERO_SSA_OPERANDS (last, SSA_OP_USE))
6123 find_switch_asserts (bb, as_a <gswitch *> (last));
6125 /* Traverse all the statements in BB marking used names and looking
6126 for statements that may infer assertions for their used operands. */
6127 for (gimple_stmt_iterator si = gsi_last_bb (bb); !gsi_end_p (si);
6128 gsi_prev (&si))
6130 gimple stmt;
6131 tree op;
6132 ssa_op_iter i;
6134 stmt = gsi_stmt (si);
6136 if (is_gimple_debug (stmt))
6137 continue;
6139 /* See if we can derive an assertion for any of STMT's operands. */
6140 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_USE)
6142 tree value;
6143 enum tree_code comp_code;
6145 /* If op is not live beyond this stmt, do not bother to insert
6146 asserts for it. */
6147 if (!bitmap_bit_p (live, SSA_NAME_VERSION (op)))
6148 continue;
6150 /* If OP is used in such a way that we can infer a value
6151 range for it, and we don't find a previous assertion for
6152 it, create a new assertion location node for OP. */
6153 if (infer_value_range (stmt, op, &comp_code, &value))
6155 /* If we are able to infer a nonzero value range for OP,
6156 then walk backwards through the use-def chain to see if OP
6157 was set via a typecast.
6159 If so, then we can also infer a nonzero value range
6160 for the operand of the NOP_EXPR. */
6161 if (comp_code == NE_EXPR && integer_zerop (value))
6163 tree t = op;
6164 gimple def_stmt = SSA_NAME_DEF_STMT (t);
6166 while (is_gimple_assign (def_stmt)
6167 && CONVERT_EXPR_CODE_P
6168 (gimple_assign_rhs_code (def_stmt))
6169 && TREE_CODE
6170 (gimple_assign_rhs1 (def_stmt)) == SSA_NAME
6171 && POINTER_TYPE_P
6172 (TREE_TYPE (gimple_assign_rhs1 (def_stmt))))
6174 t = gimple_assign_rhs1 (def_stmt);
6175 def_stmt = SSA_NAME_DEF_STMT (t);
6177 /* Note we want to register the assert for the
6178 operand of the NOP_EXPR after SI, not after the
6179 conversion. */
6180 if (! has_single_use (t))
6181 register_new_assert_for (t, t, comp_code, value,
6182 bb, NULL, si);
6186 register_new_assert_for (op, op, comp_code, value, bb, NULL, si);
6190 /* Update live. */
6191 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_USE)
6192 bitmap_set_bit (live, SSA_NAME_VERSION (op));
6193 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_DEF)
6194 bitmap_clear_bit (live, SSA_NAME_VERSION (op));
6197 /* Traverse all PHI nodes in BB, updating live. */
6198 for (gphi_iterator si = gsi_start_phis (bb); !gsi_end_p (si);
6199 gsi_next (&si))
6201 use_operand_p arg_p;
6202 ssa_op_iter i;
6203 gphi *phi = si.phi ();
6204 tree res = gimple_phi_result (phi);
6206 if (virtual_operand_p (res))
6207 continue;
6209 FOR_EACH_PHI_ARG (arg_p, phi, i, SSA_OP_USE)
6211 tree arg = USE_FROM_PTR (arg_p);
6212 if (TREE_CODE (arg) == SSA_NAME)
6213 bitmap_set_bit (live, SSA_NAME_VERSION (arg));
6216 bitmap_clear_bit (live, SSA_NAME_VERSION (res));
6220 /* Do an RPO walk over the function computing SSA name liveness
6221 on-the-fly and deciding on assert expressions to insert. */
6223 static void
6224 find_assert_locations (void)
6226 int *rpo = XNEWVEC (int, last_basic_block_for_fn (cfun));
6227 int *bb_rpo = XNEWVEC (int, last_basic_block_for_fn (cfun));
6228 int *last_rpo = XCNEWVEC (int, last_basic_block_for_fn (cfun));
6229 int rpo_cnt, i;
6231 live = XCNEWVEC (sbitmap, last_basic_block_for_fn (cfun));
6232 rpo_cnt = pre_and_rev_post_order_compute (NULL, rpo, false);
6233 for (i = 0; i < rpo_cnt; ++i)
6234 bb_rpo[rpo[i]] = i;
6236 /* Pre-seed loop latch liveness from loop header PHI nodes. Due to
6237 the order we compute liveness and insert asserts we otherwise
6238 fail to insert asserts into the loop latch. */
6239 loop_p loop;
6240 FOR_EACH_LOOP (loop, 0)
6242 i = loop->latch->index;
6243 unsigned int j = single_succ_edge (loop->latch)->dest_idx;
6244 for (gphi_iterator gsi = gsi_start_phis (loop->header);
6245 !gsi_end_p (gsi); gsi_next (&gsi))
6247 gphi *phi = gsi.phi ();
6248 if (virtual_operand_p (gimple_phi_result (phi)))
6249 continue;
6250 tree arg = gimple_phi_arg_def (phi, j);
6251 if (TREE_CODE (arg) == SSA_NAME)
6253 if (live[i] == NULL)
6255 live[i] = sbitmap_alloc (num_ssa_names);
6256 bitmap_clear (live[i]);
6258 bitmap_set_bit (live[i], SSA_NAME_VERSION (arg));
6263 for (i = rpo_cnt - 1; i >= 0; --i)
6265 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, rpo[i]);
6266 edge e;
6267 edge_iterator ei;
6269 if (!live[rpo[i]])
6271 live[rpo[i]] = sbitmap_alloc (num_ssa_names);
6272 bitmap_clear (live[rpo[i]]);
6275 /* Process BB and update the live information with uses in
6276 this block. */
6277 find_assert_locations_1 (bb, live[rpo[i]]);
6279 /* Merge liveness into the predecessor blocks and free it. */
6280 if (!bitmap_empty_p (live[rpo[i]]))
6282 int pred_rpo = i;
6283 FOR_EACH_EDGE (e, ei, bb->preds)
6285 int pred = e->src->index;
6286 if ((e->flags & EDGE_DFS_BACK) || pred == ENTRY_BLOCK)
6287 continue;
6289 if (!live[pred])
6291 live[pred] = sbitmap_alloc (num_ssa_names);
6292 bitmap_clear (live[pred]);
6294 bitmap_ior (live[pred], live[pred], live[rpo[i]]);
6296 if (bb_rpo[pred] < pred_rpo)
6297 pred_rpo = bb_rpo[pred];
6300 /* Record the RPO number of the last visited block that needs
6301 live information from this block. */
6302 last_rpo[rpo[i]] = pred_rpo;
6304 else
6306 sbitmap_free (live[rpo[i]]);
6307 live[rpo[i]] = NULL;
6310 /* We can free all successors live bitmaps if all their
6311 predecessors have been visited already. */
6312 FOR_EACH_EDGE (e, ei, bb->succs)
6313 if (last_rpo[e->dest->index] == i
6314 && live[e->dest->index])
6316 sbitmap_free (live[e->dest->index]);
6317 live[e->dest->index] = NULL;
6321 XDELETEVEC (rpo);
6322 XDELETEVEC (bb_rpo);
6323 XDELETEVEC (last_rpo);
6324 for (i = 0; i < last_basic_block_for_fn (cfun); ++i)
6325 if (live[i])
6326 sbitmap_free (live[i]);
6327 XDELETEVEC (live);
6330 /* Create an ASSERT_EXPR for NAME and insert it in the location
6331 indicated by LOC. Return true if we made any edge insertions. */
6333 static bool
6334 process_assert_insertions_for (tree name, assert_locus *loc)
6336 /* Build the comparison expression NAME_i COMP_CODE VAL. */
6337 gimple stmt;
6338 tree cond;
6339 gimple assert_stmt;
6340 edge_iterator ei;
6341 edge e;
6343 /* If we have X <=> X do not insert an assert expr for that. */
6344 if (loc->expr == loc->val)
6345 return false;
6347 cond = build2 (loc->comp_code, boolean_type_node, loc->expr, loc->val);
6348 assert_stmt = build_assert_expr_for (cond, name);
6349 if (loc->e)
6351 /* We have been asked to insert the assertion on an edge. This
6352 is used only by COND_EXPR and SWITCH_EXPR assertions. */
6353 gcc_checking_assert (gimple_code (gsi_stmt (loc->si)) == GIMPLE_COND
6354 || (gimple_code (gsi_stmt (loc->si))
6355 == GIMPLE_SWITCH));
6357 gsi_insert_on_edge (loc->e, assert_stmt);
6358 return true;
6361 /* Otherwise, we can insert right after LOC->SI iff the
6362 statement must not be the last statement in the block. */
6363 stmt = gsi_stmt (loc->si);
6364 if (!stmt_ends_bb_p (stmt))
6366 gsi_insert_after (&loc->si, assert_stmt, GSI_SAME_STMT);
6367 return false;
6370 /* If STMT must be the last statement in BB, we can only insert new
6371 assertions on the non-abnormal edge out of BB. Note that since
6372 STMT is not control flow, there may only be one non-abnormal edge
6373 out of BB. */
6374 FOR_EACH_EDGE (e, ei, loc->bb->succs)
6375 if (!(e->flags & EDGE_ABNORMAL))
6377 gsi_insert_on_edge (e, assert_stmt);
6378 return true;
6381 gcc_unreachable ();
6385 /* Process all the insertions registered for every name N_i registered
6386 in NEED_ASSERT_FOR. The list of assertions to be inserted are
6387 found in ASSERTS_FOR[i]. */
6389 static void
6390 process_assert_insertions (void)
6392 unsigned i;
6393 bitmap_iterator bi;
6394 bool update_edges_p = false;
6395 int num_asserts = 0;
6397 if (dump_file && (dump_flags & TDF_DETAILS))
6398 dump_all_asserts (dump_file);
6400 EXECUTE_IF_SET_IN_BITMAP (need_assert_for, 0, i, bi)
6402 assert_locus *loc = asserts_for[i];
6403 gcc_assert (loc);
6405 while (loc)
6407 assert_locus *next = loc->next;
6408 update_edges_p |= process_assert_insertions_for (ssa_name (i), loc);
6409 free (loc);
6410 loc = next;
6411 num_asserts++;
6415 if (update_edges_p)
6416 gsi_commit_edge_inserts ();
6418 statistics_counter_event (cfun, "Number of ASSERT_EXPR expressions inserted",
6419 num_asserts);
6423 /* Traverse the flowgraph looking for conditional jumps to insert range
6424 expressions. These range expressions are meant to provide information
6425 to optimizations that need to reason in terms of value ranges. They
6426 will not be expanded into RTL. For instance, given:
6428 x = ...
6429 y = ...
6430 if (x < y)
6431 y = x - 2;
6432 else
6433 x = y + 3;
6435 this pass will transform the code into:
6437 x = ...
6438 y = ...
6439 if (x < y)
6441 x = ASSERT_EXPR <x, x < y>
6442 y = x - 2
6444 else
6446 y = ASSERT_EXPR <y, x >= y>
6447 x = y + 3
6450 The idea is that once copy and constant propagation have run, other
6451 optimizations will be able to determine what ranges of values can 'x'
6452 take in different paths of the code, simply by checking the reaching
6453 definition of 'x'. */
6455 static void
6456 insert_range_assertions (void)
6458 need_assert_for = BITMAP_ALLOC (NULL);
6459 asserts_for = XCNEWVEC (assert_locus *, num_ssa_names);
6461 calculate_dominance_info (CDI_DOMINATORS);
6463 find_assert_locations ();
6464 if (!bitmap_empty_p (need_assert_for))
6466 process_assert_insertions ();
6467 update_ssa (TODO_update_ssa_no_phi);
6470 if (dump_file && (dump_flags & TDF_DETAILS))
6472 fprintf (dump_file, "\nSSA form after inserting ASSERT_EXPRs\n");
6473 dump_function_to_file (current_function_decl, dump_file, dump_flags);
6476 free (asserts_for);
6477 BITMAP_FREE (need_assert_for);
6480 /* Checks one ARRAY_REF in REF, located at LOCUS. Ignores flexible arrays
6481 and "struct" hacks. If VRP can determine that the
6482 array subscript is a constant, check if it is outside valid
6483 range. If the array subscript is a RANGE, warn if it is
6484 non-overlapping with valid range.
6485 IGNORE_OFF_BY_ONE is true if the ARRAY_REF is inside a ADDR_EXPR. */
6487 static void
6488 check_array_ref (location_t location, tree ref, bool ignore_off_by_one)
6490 value_range_t* vr = NULL;
6491 tree low_sub, up_sub;
6492 tree low_bound, up_bound, up_bound_p1;
6493 tree base;
6495 if (TREE_NO_WARNING (ref))
6496 return;
6498 low_sub = up_sub = TREE_OPERAND (ref, 1);
6499 up_bound = array_ref_up_bound (ref);
6501 /* Can not check flexible arrays. */
6502 if (!up_bound
6503 || TREE_CODE (up_bound) != INTEGER_CST)
6504 return;
6506 /* Accesses to trailing arrays via pointers may access storage
6507 beyond the types array bounds. */
6508 base = get_base_address (ref);
6509 if ((warn_array_bounds < 2)
6510 && base && TREE_CODE (base) == MEM_REF)
6512 tree cref, next = NULL_TREE;
6514 if (TREE_CODE (TREE_OPERAND (ref, 0)) != COMPONENT_REF)
6515 return;
6517 cref = TREE_OPERAND (ref, 0);
6518 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (cref, 0))) == RECORD_TYPE)
6519 for (next = DECL_CHAIN (TREE_OPERAND (cref, 1));
6520 next && TREE_CODE (next) != FIELD_DECL;
6521 next = DECL_CHAIN (next))
6524 /* If this is the last field in a struct type or a field in a
6525 union type do not warn. */
6526 if (!next)
6527 return;
6530 low_bound = array_ref_low_bound (ref);
6531 up_bound_p1 = int_const_binop (PLUS_EXPR, up_bound,
6532 build_int_cst (TREE_TYPE (up_bound), 1));
6534 /* Empty array. */
6535 if (tree_int_cst_equal (low_bound, up_bound_p1))
6537 warning_at (location, OPT_Warray_bounds,
6538 "array subscript is above array bounds");
6539 TREE_NO_WARNING (ref) = 1;
6542 if (TREE_CODE (low_sub) == SSA_NAME)
6544 vr = get_value_range (low_sub);
6545 if (vr->type == VR_RANGE || vr->type == VR_ANTI_RANGE)
6547 low_sub = vr->type == VR_RANGE ? vr->max : vr->min;
6548 up_sub = vr->type == VR_RANGE ? vr->min : vr->max;
6552 if (vr && vr->type == VR_ANTI_RANGE)
6554 if (TREE_CODE (up_sub) == INTEGER_CST
6555 && (ignore_off_by_one
6556 ? tree_int_cst_lt (up_bound, up_sub)
6557 : tree_int_cst_le (up_bound, up_sub))
6558 && TREE_CODE (low_sub) == INTEGER_CST
6559 && tree_int_cst_le (low_sub, low_bound))
6561 warning_at (location, OPT_Warray_bounds,
6562 "array subscript is outside array bounds");
6563 TREE_NO_WARNING (ref) = 1;
6566 else if (TREE_CODE (up_sub) == INTEGER_CST
6567 && (ignore_off_by_one
6568 ? !tree_int_cst_le (up_sub, up_bound_p1)
6569 : !tree_int_cst_le (up_sub, up_bound)))
6571 if (dump_file && (dump_flags & TDF_DETAILS))
6573 fprintf (dump_file, "Array bound warning for ");
6574 dump_generic_expr (MSG_NOTE, TDF_SLIM, ref);
6575 fprintf (dump_file, "\n");
6577 warning_at (location, OPT_Warray_bounds,
6578 "array subscript is above array bounds");
6579 TREE_NO_WARNING (ref) = 1;
6581 else if (TREE_CODE (low_sub) == INTEGER_CST
6582 && tree_int_cst_lt (low_sub, low_bound))
6584 if (dump_file && (dump_flags & TDF_DETAILS))
6586 fprintf (dump_file, "Array bound warning for ");
6587 dump_generic_expr (MSG_NOTE, TDF_SLIM, ref);
6588 fprintf (dump_file, "\n");
6590 warning_at (location, OPT_Warray_bounds,
6591 "array subscript is below array bounds");
6592 TREE_NO_WARNING (ref) = 1;
6596 /* Searches if the expr T, located at LOCATION computes
6597 address of an ARRAY_REF, and call check_array_ref on it. */
6599 static void
6600 search_for_addr_array (tree t, location_t location)
6602 /* Check each ARRAY_REFs in the reference chain. */
6605 if (TREE_CODE (t) == ARRAY_REF)
6606 check_array_ref (location, t, true /*ignore_off_by_one*/);
6608 t = TREE_OPERAND (t, 0);
6610 while (handled_component_p (t));
6612 if (TREE_CODE (t) == MEM_REF
6613 && TREE_CODE (TREE_OPERAND (t, 0)) == ADDR_EXPR
6614 && !TREE_NO_WARNING (t))
6616 tree tem = TREE_OPERAND (TREE_OPERAND (t, 0), 0);
6617 tree low_bound, up_bound, el_sz;
6618 offset_int idx;
6619 if (TREE_CODE (TREE_TYPE (tem)) != ARRAY_TYPE
6620 || TREE_CODE (TREE_TYPE (TREE_TYPE (tem))) == ARRAY_TYPE
6621 || !TYPE_DOMAIN (TREE_TYPE (tem)))
6622 return;
6624 low_bound = TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (tem)));
6625 up_bound = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (tem)));
6626 el_sz = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (tem)));
6627 if (!low_bound
6628 || TREE_CODE (low_bound) != INTEGER_CST
6629 || !up_bound
6630 || TREE_CODE (up_bound) != INTEGER_CST
6631 || !el_sz
6632 || TREE_CODE (el_sz) != INTEGER_CST)
6633 return;
6635 idx = mem_ref_offset (t);
6636 idx = wi::sdiv_trunc (idx, wi::to_offset (el_sz));
6637 if (wi::lts_p (idx, 0))
6639 if (dump_file && (dump_flags & TDF_DETAILS))
6641 fprintf (dump_file, "Array bound warning for ");
6642 dump_generic_expr (MSG_NOTE, TDF_SLIM, t);
6643 fprintf (dump_file, "\n");
6645 warning_at (location, OPT_Warray_bounds,
6646 "array subscript is below array bounds");
6647 TREE_NO_WARNING (t) = 1;
6649 else if (wi::gts_p (idx, (wi::to_offset (up_bound)
6650 - wi::to_offset (low_bound) + 1)))
6652 if (dump_file && (dump_flags & TDF_DETAILS))
6654 fprintf (dump_file, "Array bound warning for ");
6655 dump_generic_expr (MSG_NOTE, TDF_SLIM, t);
6656 fprintf (dump_file, "\n");
6658 warning_at (location, OPT_Warray_bounds,
6659 "array subscript is above array bounds");
6660 TREE_NO_WARNING (t) = 1;
6665 /* walk_tree() callback that checks if *TP is
6666 an ARRAY_REF inside an ADDR_EXPR (in which an array
6667 subscript one outside the valid range is allowed). Call
6668 check_array_ref for each ARRAY_REF found. The location is
6669 passed in DATA. */
6671 static tree
6672 check_array_bounds (tree *tp, int *walk_subtree, void *data)
6674 tree t = *tp;
6675 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
6676 location_t location;
6678 if (EXPR_HAS_LOCATION (t))
6679 location = EXPR_LOCATION (t);
6680 else
6682 location_t *locp = (location_t *) wi->info;
6683 location = *locp;
6686 *walk_subtree = TRUE;
6688 if (TREE_CODE (t) == ARRAY_REF)
6689 check_array_ref (location, t, false /*ignore_off_by_one*/);
6691 else if (TREE_CODE (t) == ADDR_EXPR)
6693 search_for_addr_array (t, location);
6694 *walk_subtree = FALSE;
6697 return NULL_TREE;
6700 /* Walk over all statements of all reachable BBs and call check_array_bounds
6701 on them. */
6703 static void
6704 check_all_array_refs (void)
6706 basic_block bb;
6707 gimple_stmt_iterator si;
6709 FOR_EACH_BB_FN (bb, cfun)
6711 edge_iterator ei;
6712 edge e;
6713 bool executable = false;
6715 /* Skip blocks that were found to be unreachable. */
6716 FOR_EACH_EDGE (e, ei, bb->preds)
6717 executable |= !!(e->flags & EDGE_EXECUTABLE);
6718 if (!executable)
6719 continue;
6721 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
6723 gimple stmt = gsi_stmt (si);
6724 struct walk_stmt_info wi;
6725 if (!gimple_has_location (stmt)
6726 || is_gimple_debug (stmt))
6727 continue;
6729 memset (&wi, 0, sizeof (wi));
6730 wi.info = CONST_CAST (void *, (const void *)
6731 gimple_location_ptr (stmt));
6733 walk_gimple_op (gsi_stmt (si),
6734 check_array_bounds,
6735 &wi);
6740 /* Return true if all imm uses of VAR are either in STMT, or
6741 feed (optionally through a chain of single imm uses) GIMPLE_COND
6742 in basic block COND_BB. */
6744 static bool
6745 all_imm_uses_in_stmt_or_feed_cond (tree var, gimple stmt, basic_block cond_bb)
6747 use_operand_p use_p, use2_p;
6748 imm_use_iterator iter;
6750 FOR_EACH_IMM_USE_FAST (use_p, iter, var)
6751 if (USE_STMT (use_p) != stmt)
6753 gimple use_stmt = USE_STMT (use_p), use_stmt2;
6754 if (is_gimple_debug (use_stmt))
6755 continue;
6756 while (is_gimple_assign (use_stmt)
6757 && TREE_CODE (gimple_assign_lhs (use_stmt)) == SSA_NAME
6758 && single_imm_use (gimple_assign_lhs (use_stmt),
6759 &use2_p, &use_stmt2))
6760 use_stmt = use_stmt2;
6761 if (gimple_code (use_stmt) != GIMPLE_COND
6762 || gimple_bb (use_stmt) != cond_bb)
6763 return false;
6765 return true;
6768 /* Handle
6769 _4 = x_3 & 31;
6770 if (_4 != 0)
6771 goto <bb 6>;
6772 else
6773 goto <bb 7>;
6774 <bb 6>:
6775 __builtin_unreachable ();
6776 <bb 7>:
6777 x_5 = ASSERT_EXPR <x_3, ...>;
6778 If x_3 has no other immediate uses (checked by caller),
6779 var is the x_3 var from ASSERT_EXPR, we can clear low 5 bits
6780 from the non-zero bitmask. */
6782 static void
6783 maybe_set_nonzero_bits (basic_block bb, tree var)
6785 edge e = single_pred_edge (bb);
6786 basic_block cond_bb = e->src;
6787 gimple stmt = last_stmt (cond_bb);
6788 tree cst;
6790 if (stmt == NULL
6791 || gimple_code (stmt) != GIMPLE_COND
6792 || gimple_cond_code (stmt) != ((e->flags & EDGE_TRUE_VALUE)
6793 ? EQ_EXPR : NE_EXPR)
6794 || TREE_CODE (gimple_cond_lhs (stmt)) != SSA_NAME
6795 || !integer_zerop (gimple_cond_rhs (stmt)))
6796 return;
6798 stmt = SSA_NAME_DEF_STMT (gimple_cond_lhs (stmt));
6799 if (!is_gimple_assign (stmt)
6800 || gimple_assign_rhs_code (stmt) != BIT_AND_EXPR
6801 || TREE_CODE (gimple_assign_rhs2 (stmt)) != INTEGER_CST)
6802 return;
6803 if (gimple_assign_rhs1 (stmt) != var)
6805 gimple stmt2;
6807 if (TREE_CODE (gimple_assign_rhs1 (stmt)) != SSA_NAME)
6808 return;
6809 stmt2 = SSA_NAME_DEF_STMT (gimple_assign_rhs1 (stmt));
6810 if (!gimple_assign_cast_p (stmt2)
6811 || gimple_assign_rhs1 (stmt2) != var
6812 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (stmt2))
6813 || (TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (stmt)))
6814 != TYPE_PRECISION (TREE_TYPE (var))))
6815 return;
6817 cst = gimple_assign_rhs2 (stmt);
6818 set_nonzero_bits (var, wi::bit_and_not (get_nonzero_bits (var), cst));
6821 /* Convert range assertion expressions into the implied copies and
6822 copy propagate away the copies. Doing the trivial copy propagation
6823 here avoids the need to run the full copy propagation pass after
6824 VRP.
6826 FIXME, this will eventually lead to copy propagation removing the
6827 names that had useful range information attached to them. For
6828 instance, if we had the assertion N_i = ASSERT_EXPR <N_j, N_j > 3>,
6829 then N_i will have the range [3, +INF].
6831 However, by converting the assertion into the implied copy
6832 operation N_i = N_j, we will then copy-propagate N_j into the uses
6833 of N_i and lose the range information. We may want to hold on to
6834 ASSERT_EXPRs a little while longer as the ranges could be used in
6835 things like jump threading.
6837 The problem with keeping ASSERT_EXPRs around is that passes after
6838 VRP need to handle them appropriately.
6840 Another approach would be to make the range information a first
6841 class property of the SSA_NAME so that it can be queried from
6842 any pass. This is made somewhat more complex by the need for
6843 multiple ranges to be associated with one SSA_NAME. */
6845 static void
6846 remove_range_assertions (void)
6848 basic_block bb;
6849 gimple_stmt_iterator si;
6850 /* 1 if looking at ASSERT_EXPRs immediately at the beginning of
6851 a basic block preceeded by GIMPLE_COND branching to it and
6852 __builtin_trap, -1 if not yet checked, 0 otherwise. */
6853 int is_unreachable;
6855 /* Note that the BSI iterator bump happens at the bottom of the
6856 loop and no bump is necessary if we're removing the statement
6857 referenced by the current BSI. */
6858 FOR_EACH_BB_FN (bb, cfun)
6859 for (si = gsi_after_labels (bb), is_unreachable = -1; !gsi_end_p (si);)
6861 gimple stmt = gsi_stmt (si);
6862 gimple use_stmt;
6864 if (is_gimple_assign (stmt)
6865 && gimple_assign_rhs_code (stmt) == ASSERT_EXPR)
6867 tree lhs = gimple_assign_lhs (stmt);
6868 tree rhs = gimple_assign_rhs1 (stmt);
6869 tree var;
6870 tree cond = fold (ASSERT_EXPR_COND (rhs));
6871 use_operand_p use_p;
6872 imm_use_iterator iter;
6874 gcc_assert (cond != boolean_false_node);
6876 var = ASSERT_EXPR_VAR (rhs);
6877 gcc_assert (TREE_CODE (var) == SSA_NAME);
6879 if (!POINTER_TYPE_P (TREE_TYPE (lhs))
6880 && SSA_NAME_RANGE_INFO (lhs))
6882 if (is_unreachable == -1)
6884 is_unreachable = 0;
6885 if (single_pred_p (bb)
6886 && assert_unreachable_fallthru_edge_p
6887 (single_pred_edge (bb)))
6888 is_unreachable = 1;
6890 /* Handle
6891 if (x_7 >= 10 && x_7 < 20)
6892 __builtin_unreachable ();
6893 x_8 = ASSERT_EXPR <x_7, ...>;
6894 if the only uses of x_7 are in the ASSERT_EXPR and
6895 in the condition. In that case, we can copy the
6896 range info from x_8 computed in this pass also
6897 for x_7. */
6898 if (is_unreachable
6899 && all_imm_uses_in_stmt_or_feed_cond (var, stmt,
6900 single_pred (bb)))
6902 set_range_info (var, SSA_NAME_RANGE_TYPE (lhs),
6903 SSA_NAME_RANGE_INFO (lhs)->get_min (),
6904 SSA_NAME_RANGE_INFO (lhs)->get_max ());
6905 maybe_set_nonzero_bits (bb, var);
6909 /* Propagate the RHS into every use of the LHS. */
6910 FOR_EACH_IMM_USE_STMT (use_stmt, iter, lhs)
6911 FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
6912 SET_USE (use_p, var);
6914 /* And finally, remove the copy, it is not needed. */
6915 gsi_remove (&si, true);
6916 release_defs (stmt);
6918 else
6920 if (!is_gimple_debug (gsi_stmt (si)))
6921 is_unreachable = 0;
6922 gsi_next (&si);
6928 /* Return true if STMT is interesting for VRP. */
6930 static bool
6931 stmt_interesting_for_vrp (gimple stmt)
6933 if (gimple_code (stmt) == GIMPLE_PHI)
6935 tree res = gimple_phi_result (stmt);
6936 return (!virtual_operand_p (res)
6937 && (INTEGRAL_TYPE_P (TREE_TYPE (res))
6938 || POINTER_TYPE_P (TREE_TYPE (res))));
6940 else if (is_gimple_assign (stmt) || is_gimple_call (stmt))
6942 tree lhs = gimple_get_lhs (stmt);
6944 /* In general, assignments with virtual operands are not useful
6945 for deriving ranges, with the obvious exception of calls to
6946 builtin functions. */
6947 if (lhs && TREE_CODE (lhs) == SSA_NAME
6948 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs))
6949 || POINTER_TYPE_P (TREE_TYPE (lhs)))
6950 && (is_gimple_call (stmt)
6951 || !gimple_vuse (stmt)))
6952 return true;
6953 else if (is_gimple_call (stmt) && gimple_call_internal_p (stmt))
6954 switch (gimple_call_internal_fn (stmt))
6956 case IFN_ADD_OVERFLOW:
6957 case IFN_SUB_OVERFLOW:
6958 case IFN_MUL_OVERFLOW:
6959 /* These internal calls return _Complex integer type,
6960 but are interesting to VRP nevertheless. */
6961 if (lhs && TREE_CODE (lhs) == SSA_NAME)
6962 return true;
6963 break;
6964 default:
6965 break;
6968 else if (gimple_code (stmt) == GIMPLE_COND
6969 || gimple_code (stmt) == GIMPLE_SWITCH)
6970 return true;
6972 return false;
6976 /* Initialize local data structures for VRP. */
6978 static void
6979 vrp_initialize (void)
6981 basic_block bb;
6983 values_propagated = false;
6984 num_vr_values = num_ssa_names;
6985 vr_value = XCNEWVEC (value_range_t *, num_vr_values);
6986 vr_phi_edge_counts = XCNEWVEC (int, num_ssa_names);
6988 FOR_EACH_BB_FN (bb, cfun)
6990 for (gphi_iterator si = gsi_start_phis (bb); !gsi_end_p (si);
6991 gsi_next (&si))
6993 gphi *phi = si.phi ();
6994 if (!stmt_interesting_for_vrp (phi))
6996 tree lhs = PHI_RESULT (phi);
6997 set_value_range_to_varying (get_value_range (lhs));
6998 prop_set_simulate_again (phi, false);
7000 else
7001 prop_set_simulate_again (phi, true);
7004 for (gimple_stmt_iterator si = gsi_start_bb (bb); !gsi_end_p (si);
7005 gsi_next (&si))
7007 gimple stmt = gsi_stmt (si);
7009 /* If the statement is a control insn, then we do not
7010 want to avoid simulating the statement once. Failure
7011 to do so means that those edges will never get added. */
7012 if (stmt_ends_bb_p (stmt))
7013 prop_set_simulate_again (stmt, true);
7014 else if (!stmt_interesting_for_vrp (stmt))
7016 ssa_op_iter i;
7017 tree def;
7018 FOR_EACH_SSA_TREE_OPERAND (def, stmt, i, SSA_OP_DEF)
7019 set_value_range_to_varying (get_value_range (def));
7020 prop_set_simulate_again (stmt, false);
7022 else
7023 prop_set_simulate_again (stmt, true);
7028 /* Return the singleton value-range for NAME or NAME. */
7030 static inline tree
7031 vrp_valueize (tree name)
7033 if (TREE_CODE (name) == SSA_NAME)
7035 value_range_t *vr = get_value_range (name);
7036 if (vr->type == VR_RANGE
7037 && (vr->min == vr->max
7038 || operand_equal_p (vr->min, vr->max, 0)))
7039 return vr->min;
7041 return name;
7044 /* Return the singleton value-range for NAME if that is a constant
7045 but signal to not follow SSA edges. */
7047 static inline tree
7048 vrp_valueize_1 (tree name)
7050 if (TREE_CODE (name) == SSA_NAME)
7052 /* If the definition may be simulated again we cannot follow
7053 this SSA edge as the SSA propagator does not necessarily
7054 re-visit the use. */
7055 gimple def_stmt = SSA_NAME_DEF_STMT (name);
7056 if (!gimple_nop_p (def_stmt)
7057 && prop_simulate_again_p (def_stmt))
7058 return NULL_TREE;
7059 value_range_t *vr = get_value_range (name);
7060 if (range_int_cst_singleton_p (vr))
7061 return vr->min;
7063 return name;
7066 /* Visit assignment STMT. If it produces an interesting range, record
7067 the SSA name in *OUTPUT_P. */
7069 static enum ssa_prop_result
7070 vrp_visit_assignment_or_call (gimple stmt, tree *output_p)
7072 tree def, lhs;
7073 ssa_op_iter iter;
7074 enum gimple_code code = gimple_code (stmt);
7075 lhs = gimple_get_lhs (stmt);
7077 /* We only keep track of ranges in integral and pointer types. */
7078 if (TREE_CODE (lhs) == SSA_NAME
7079 && ((INTEGRAL_TYPE_P (TREE_TYPE (lhs))
7080 /* It is valid to have NULL MIN/MAX values on a type. See
7081 build_range_type. */
7082 && TYPE_MIN_VALUE (TREE_TYPE (lhs))
7083 && TYPE_MAX_VALUE (TREE_TYPE (lhs)))
7084 || POINTER_TYPE_P (TREE_TYPE (lhs))))
7086 value_range_t new_vr = VR_INITIALIZER;
7088 /* Try folding the statement to a constant first. */
7089 tree tem = gimple_fold_stmt_to_constant_1 (stmt, vrp_valueize,
7090 vrp_valueize_1);
7091 if (tem && is_gimple_min_invariant (tem))
7092 set_value_range_to_value (&new_vr, tem, NULL);
7093 /* Then dispatch to value-range extracting functions. */
7094 else if (code == GIMPLE_CALL)
7095 extract_range_basic (&new_vr, stmt);
7096 else
7097 extract_range_from_assignment (&new_vr, as_a <gassign *> (stmt));
7099 if (update_value_range (lhs, &new_vr))
7101 *output_p = lhs;
7103 if (dump_file && (dump_flags & TDF_DETAILS))
7105 fprintf (dump_file, "Found new range for ");
7106 print_generic_expr (dump_file, lhs, 0);
7107 fprintf (dump_file, ": ");
7108 dump_value_range (dump_file, &new_vr);
7109 fprintf (dump_file, "\n");
7112 if (new_vr.type == VR_VARYING)
7113 return SSA_PROP_VARYING;
7115 return SSA_PROP_INTERESTING;
7118 return SSA_PROP_NOT_INTERESTING;
7120 else if (is_gimple_call (stmt) && gimple_call_internal_p (stmt))
7121 switch (gimple_call_internal_fn (stmt))
7123 case IFN_ADD_OVERFLOW:
7124 case IFN_SUB_OVERFLOW:
7125 case IFN_MUL_OVERFLOW:
7126 /* These internal calls return _Complex integer type,
7127 which VRP does not track, but the immediate uses
7128 thereof might be interesting. */
7129 if (lhs && TREE_CODE (lhs) == SSA_NAME)
7131 imm_use_iterator iter;
7132 use_operand_p use_p;
7133 enum ssa_prop_result res = SSA_PROP_VARYING;
7135 set_value_range_to_varying (get_value_range (lhs));
7137 FOR_EACH_IMM_USE_FAST (use_p, iter, lhs)
7139 gimple use_stmt = USE_STMT (use_p);
7140 if (!is_gimple_assign (use_stmt))
7141 continue;
7142 enum tree_code rhs_code = gimple_assign_rhs_code (use_stmt);
7143 if (rhs_code != REALPART_EXPR && rhs_code != IMAGPART_EXPR)
7144 continue;
7145 tree rhs1 = gimple_assign_rhs1 (use_stmt);
7146 tree use_lhs = gimple_assign_lhs (use_stmt);
7147 if (TREE_CODE (rhs1) != rhs_code
7148 || TREE_OPERAND (rhs1, 0) != lhs
7149 || TREE_CODE (use_lhs) != SSA_NAME
7150 || !stmt_interesting_for_vrp (use_stmt)
7151 || (!INTEGRAL_TYPE_P (TREE_TYPE (use_lhs))
7152 || !TYPE_MIN_VALUE (TREE_TYPE (use_lhs))
7153 || !TYPE_MAX_VALUE (TREE_TYPE (use_lhs))))
7154 continue;
7156 /* If there is a change in the value range for any of the
7157 REALPART_EXPR/IMAGPART_EXPR immediate uses, return
7158 SSA_PROP_INTERESTING. If there are any REALPART_EXPR
7159 or IMAGPART_EXPR immediate uses, but none of them have
7160 a change in their value ranges, return
7161 SSA_PROP_NOT_INTERESTING. If there are no
7162 {REAL,IMAG}PART_EXPR uses at all,
7163 return SSA_PROP_VARYING. */
7164 value_range_t new_vr = VR_INITIALIZER;
7165 extract_range_basic (&new_vr, use_stmt);
7166 value_range_t *old_vr = get_value_range (use_lhs);
7167 if (old_vr->type != new_vr.type
7168 || !vrp_operand_equal_p (old_vr->min, new_vr.min)
7169 || !vrp_operand_equal_p (old_vr->max, new_vr.max)
7170 || !vrp_bitmap_equal_p (old_vr->equiv, new_vr.equiv))
7171 res = SSA_PROP_INTERESTING;
7172 else
7173 res = SSA_PROP_NOT_INTERESTING;
7174 BITMAP_FREE (new_vr.equiv);
7175 if (res == SSA_PROP_INTERESTING)
7177 *output_p = lhs;
7178 return res;
7182 return res;
7184 break;
7185 default:
7186 break;
7189 /* Every other statement produces no useful ranges. */
7190 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_DEF)
7191 set_value_range_to_varying (get_value_range (def));
7193 return SSA_PROP_VARYING;
7196 /* Helper that gets the value range of the SSA_NAME with version I
7197 or a symbolic range containing the SSA_NAME only if the value range
7198 is varying or undefined. */
7200 static inline value_range_t
7201 get_vr_for_comparison (int i)
7203 value_range_t vr = *get_value_range (ssa_name (i));
7205 /* If name N_i does not have a valid range, use N_i as its own
7206 range. This allows us to compare against names that may
7207 have N_i in their ranges. */
7208 if (vr.type == VR_VARYING || vr.type == VR_UNDEFINED)
7210 vr.type = VR_RANGE;
7211 vr.min = ssa_name (i);
7212 vr.max = ssa_name (i);
7215 return vr;
7218 /* Compare all the value ranges for names equivalent to VAR with VAL
7219 using comparison code COMP. Return the same value returned by
7220 compare_range_with_value, including the setting of
7221 *STRICT_OVERFLOW_P. */
7223 static tree
7224 compare_name_with_value (enum tree_code comp, tree var, tree val,
7225 bool *strict_overflow_p)
7227 bitmap_iterator bi;
7228 unsigned i;
7229 bitmap e;
7230 tree retval, t;
7231 int used_strict_overflow;
7232 bool sop;
7233 value_range_t equiv_vr;
7235 /* Get the set of equivalences for VAR. */
7236 e = get_value_range (var)->equiv;
7238 /* Start at -1. Set it to 0 if we do a comparison without relying
7239 on overflow, or 1 if all comparisons rely on overflow. */
7240 used_strict_overflow = -1;
7242 /* Compare vars' value range with val. */
7243 equiv_vr = get_vr_for_comparison (SSA_NAME_VERSION (var));
7244 sop = false;
7245 retval = compare_range_with_value (comp, &equiv_vr, val, &sop);
7246 if (retval)
7247 used_strict_overflow = sop ? 1 : 0;
7249 /* If the equiv set is empty we have done all work we need to do. */
7250 if (e == NULL)
7252 if (retval
7253 && used_strict_overflow > 0)
7254 *strict_overflow_p = true;
7255 return retval;
7258 EXECUTE_IF_SET_IN_BITMAP (e, 0, i, bi)
7260 equiv_vr = get_vr_for_comparison (i);
7261 sop = false;
7262 t = compare_range_with_value (comp, &equiv_vr, val, &sop);
7263 if (t)
7265 /* If we get different answers from different members
7266 of the equivalence set this check must be in a dead
7267 code region. Folding it to a trap representation
7268 would be correct here. For now just return don't-know. */
7269 if (retval != NULL
7270 && t != retval)
7272 retval = NULL_TREE;
7273 break;
7275 retval = t;
7277 if (!sop)
7278 used_strict_overflow = 0;
7279 else if (used_strict_overflow < 0)
7280 used_strict_overflow = 1;
7284 if (retval
7285 && used_strict_overflow > 0)
7286 *strict_overflow_p = true;
7288 return retval;
7292 /* Given a comparison code COMP and names N1 and N2, compare all the
7293 ranges equivalent to N1 against all the ranges equivalent to N2
7294 to determine the value of N1 COMP N2. Return the same value
7295 returned by compare_ranges. Set *STRICT_OVERFLOW_P to indicate
7296 whether we relied on an overflow infinity in the comparison. */
7299 static tree
7300 compare_names (enum tree_code comp, tree n1, tree n2,
7301 bool *strict_overflow_p)
7303 tree t, retval;
7304 bitmap e1, e2;
7305 bitmap_iterator bi1, bi2;
7306 unsigned i1, i2;
7307 int used_strict_overflow;
7308 static bitmap_obstack *s_obstack = NULL;
7309 static bitmap s_e1 = NULL, s_e2 = NULL;
7311 /* Compare the ranges of every name equivalent to N1 against the
7312 ranges of every name equivalent to N2. */
7313 e1 = get_value_range (n1)->equiv;
7314 e2 = get_value_range (n2)->equiv;
7316 /* Use the fake bitmaps if e1 or e2 are not available. */
7317 if (s_obstack == NULL)
7319 s_obstack = XNEW (bitmap_obstack);
7320 bitmap_obstack_initialize (s_obstack);
7321 s_e1 = BITMAP_ALLOC (s_obstack);
7322 s_e2 = BITMAP_ALLOC (s_obstack);
7324 if (e1 == NULL)
7325 e1 = s_e1;
7326 if (e2 == NULL)
7327 e2 = s_e2;
7329 /* Add N1 and N2 to their own set of equivalences to avoid
7330 duplicating the body of the loop just to check N1 and N2
7331 ranges. */
7332 bitmap_set_bit (e1, SSA_NAME_VERSION (n1));
7333 bitmap_set_bit (e2, SSA_NAME_VERSION (n2));
7335 /* If the equivalence sets have a common intersection, then the two
7336 names can be compared without checking their ranges. */
7337 if (bitmap_intersect_p (e1, e2))
7339 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
7340 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
7342 return (comp == EQ_EXPR || comp == GE_EXPR || comp == LE_EXPR)
7343 ? boolean_true_node
7344 : boolean_false_node;
7347 /* Start at -1. Set it to 0 if we do a comparison without relying
7348 on overflow, or 1 if all comparisons rely on overflow. */
7349 used_strict_overflow = -1;
7351 /* Otherwise, compare all the equivalent ranges. First, add N1 and
7352 N2 to their own set of equivalences to avoid duplicating the body
7353 of the loop just to check N1 and N2 ranges. */
7354 EXECUTE_IF_SET_IN_BITMAP (e1, 0, i1, bi1)
7356 value_range_t vr1 = get_vr_for_comparison (i1);
7358 t = retval = NULL_TREE;
7359 EXECUTE_IF_SET_IN_BITMAP (e2, 0, i2, bi2)
7361 bool sop = false;
7363 value_range_t vr2 = get_vr_for_comparison (i2);
7365 t = compare_ranges (comp, &vr1, &vr2, &sop);
7366 if (t)
7368 /* If we get different answers from different members
7369 of the equivalence set this check must be in a dead
7370 code region. Folding it to a trap representation
7371 would be correct here. For now just return don't-know. */
7372 if (retval != NULL
7373 && t != retval)
7375 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
7376 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
7377 return NULL_TREE;
7379 retval = t;
7381 if (!sop)
7382 used_strict_overflow = 0;
7383 else if (used_strict_overflow < 0)
7384 used_strict_overflow = 1;
7388 if (retval)
7390 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
7391 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
7392 if (used_strict_overflow > 0)
7393 *strict_overflow_p = true;
7394 return retval;
7398 /* None of the equivalent ranges are useful in computing this
7399 comparison. */
7400 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
7401 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
7402 return NULL_TREE;
7405 /* Helper function for vrp_evaluate_conditional_warnv & other
7406 optimizers. */
7408 static tree
7409 vrp_evaluate_conditional_warnv_with_ops_using_ranges (enum tree_code code,
7410 tree op0, tree op1,
7411 bool * strict_overflow_p)
7413 value_range_t *vr0, *vr1;
7415 vr0 = (TREE_CODE (op0) == SSA_NAME) ? get_value_range (op0) : NULL;
7416 vr1 = (TREE_CODE (op1) == SSA_NAME) ? get_value_range (op1) : NULL;
7418 tree res = NULL_TREE;
7419 if (vr0 && vr1)
7420 res = compare_ranges (code, vr0, vr1, strict_overflow_p);
7421 if (!res && vr0)
7422 res = compare_range_with_value (code, vr0, op1, strict_overflow_p);
7423 if (!res && vr1)
7424 res = (compare_range_with_value
7425 (swap_tree_comparison (code), vr1, op0, strict_overflow_p));
7426 return res;
7429 /* Helper function for vrp_evaluate_conditional_warnv. */
7431 static tree
7432 vrp_evaluate_conditional_warnv_with_ops (enum tree_code code, tree op0,
7433 tree op1, bool use_equiv_p,
7434 bool *strict_overflow_p, bool *only_ranges)
7436 tree ret;
7437 if (only_ranges)
7438 *only_ranges = true;
7440 /* We only deal with integral and pointer types. */
7441 if (!INTEGRAL_TYPE_P (TREE_TYPE (op0))
7442 && !POINTER_TYPE_P (TREE_TYPE (op0)))
7443 return NULL_TREE;
7445 if (use_equiv_p)
7447 if (only_ranges
7448 && (ret = vrp_evaluate_conditional_warnv_with_ops_using_ranges
7449 (code, op0, op1, strict_overflow_p)))
7450 return ret;
7451 *only_ranges = false;
7452 if (TREE_CODE (op0) == SSA_NAME && TREE_CODE (op1) == SSA_NAME)
7453 return compare_names (code, op0, op1, strict_overflow_p);
7454 else if (TREE_CODE (op0) == SSA_NAME)
7455 return compare_name_with_value (code, op0, op1, strict_overflow_p);
7456 else if (TREE_CODE (op1) == SSA_NAME)
7457 return (compare_name_with_value
7458 (swap_tree_comparison (code), op1, op0, strict_overflow_p));
7460 else
7461 return vrp_evaluate_conditional_warnv_with_ops_using_ranges (code, op0, op1,
7462 strict_overflow_p);
7463 return NULL_TREE;
7466 /* Given (CODE OP0 OP1) within STMT, try to simplify it based on value range
7467 information. Return NULL if the conditional can not be evaluated.
7468 The ranges of all the names equivalent with the operands in COND
7469 will be used when trying to compute the value. If the result is
7470 based on undefined signed overflow, issue a warning if
7471 appropriate. */
7473 static tree
7474 vrp_evaluate_conditional (enum tree_code code, tree op0, tree op1, gimple stmt)
7476 bool sop;
7477 tree ret;
7478 bool only_ranges;
7480 /* Some passes and foldings leak constants with overflow flag set
7481 into the IL. Avoid doing wrong things with these and bail out. */
7482 if ((TREE_CODE (op0) == INTEGER_CST
7483 && TREE_OVERFLOW (op0))
7484 || (TREE_CODE (op1) == INTEGER_CST
7485 && TREE_OVERFLOW (op1)))
7486 return NULL_TREE;
7488 sop = false;
7489 ret = vrp_evaluate_conditional_warnv_with_ops (code, op0, op1, true, &sop,
7490 &only_ranges);
7492 if (ret && sop)
7494 enum warn_strict_overflow_code wc;
7495 const char* warnmsg;
7497 if (is_gimple_min_invariant (ret))
7499 wc = WARN_STRICT_OVERFLOW_CONDITIONAL;
7500 warnmsg = G_("assuming signed overflow does not occur when "
7501 "simplifying conditional to constant");
7503 else
7505 wc = WARN_STRICT_OVERFLOW_COMPARISON;
7506 warnmsg = G_("assuming signed overflow does not occur when "
7507 "simplifying conditional");
7510 if (issue_strict_overflow_warning (wc))
7512 location_t location;
7514 if (!gimple_has_location (stmt))
7515 location = input_location;
7516 else
7517 location = gimple_location (stmt);
7518 warning_at (location, OPT_Wstrict_overflow, "%s", warnmsg);
7522 if (warn_type_limits
7523 && ret && only_ranges
7524 && TREE_CODE_CLASS (code) == tcc_comparison
7525 && TREE_CODE (op0) == SSA_NAME)
7527 /* If the comparison is being folded and the operand on the LHS
7528 is being compared against a constant value that is outside of
7529 the natural range of OP0's type, then the predicate will
7530 always fold regardless of the value of OP0. If -Wtype-limits
7531 was specified, emit a warning. */
7532 tree type = TREE_TYPE (op0);
7533 value_range_t *vr0 = get_value_range (op0);
7535 if (vr0->type == VR_RANGE
7536 && INTEGRAL_TYPE_P (type)
7537 && vrp_val_is_min (vr0->min)
7538 && vrp_val_is_max (vr0->max)
7539 && is_gimple_min_invariant (op1))
7541 location_t location;
7543 if (!gimple_has_location (stmt))
7544 location = input_location;
7545 else
7546 location = gimple_location (stmt);
7548 warning_at (location, OPT_Wtype_limits,
7549 integer_zerop (ret)
7550 ? G_("comparison always false "
7551 "due to limited range of data type")
7552 : G_("comparison always true "
7553 "due to limited range of data type"));
7557 return ret;
7561 /* Visit conditional statement STMT. If we can determine which edge
7562 will be taken out of STMT's basic block, record it in
7563 *TAKEN_EDGE_P and return SSA_PROP_INTERESTING. Otherwise, return
7564 SSA_PROP_VARYING. */
7566 static enum ssa_prop_result
7567 vrp_visit_cond_stmt (gcond *stmt, edge *taken_edge_p)
7569 tree val;
7570 bool sop;
7572 *taken_edge_p = NULL;
7574 if (dump_file && (dump_flags & TDF_DETAILS))
7576 tree use;
7577 ssa_op_iter i;
7579 fprintf (dump_file, "\nVisiting conditional with predicate: ");
7580 print_gimple_stmt (dump_file, stmt, 0, 0);
7581 fprintf (dump_file, "\nWith known ranges\n");
7583 FOR_EACH_SSA_TREE_OPERAND (use, stmt, i, SSA_OP_USE)
7585 fprintf (dump_file, "\t");
7586 print_generic_expr (dump_file, use, 0);
7587 fprintf (dump_file, ": ");
7588 dump_value_range (dump_file, vr_value[SSA_NAME_VERSION (use)]);
7591 fprintf (dump_file, "\n");
7594 /* Compute the value of the predicate COND by checking the known
7595 ranges of each of its operands.
7597 Note that we cannot evaluate all the equivalent ranges here
7598 because those ranges may not yet be final and with the current
7599 propagation strategy, we cannot determine when the value ranges
7600 of the names in the equivalence set have changed.
7602 For instance, given the following code fragment
7604 i_5 = PHI <8, i_13>
7606 i_14 = ASSERT_EXPR <i_5, i_5 != 0>
7607 if (i_14 == 1)
7610 Assume that on the first visit to i_14, i_5 has the temporary
7611 range [8, 8] because the second argument to the PHI function is
7612 not yet executable. We derive the range ~[0, 0] for i_14 and the
7613 equivalence set { i_5 }. So, when we visit 'if (i_14 == 1)' for
7614 the first time, since i_14 is equivalent to the range [8, 8], we
7615 determine that the predicate is always false.
7617 On the next round of propagation, i_13 is determined to be
7618 VARYING, which causes i_5 to drop down to VARYING. So, another
7619 visit to i_14 is scheduled. In this second visit, we compute the
7620 exact same range and equivalence set for i_14, namely ~[0, 0] and
7621 { i_5 }. But we did not have the previous range for i_5
7622 registered, so vrp_visit_assignment thinks that the range for
7623 i_14 has not changed. Therefore, the predicate 'if (i_14 == 1)'
7624 is not visited again, which stops propagation from visiting
7625 statements in the THEN clause of that if().
7627 To properly fix this we would need to keep the previous range
7628 value for the names in the equivalence set. This way we would've
7629 discovered that from one visit to the other i_5 changed from
7630 range [8, 8] to VR_VARYING.
7632 However, fixing this apparent limitation may not be worth the
7633 additional checking. Testing on several code bases (GCC, DLV,
7634 MICO, TRAMP3D and SPEC2000) showed that doing this results in
7635 4 more predicates folded in SPEC. */
7636 sop = false;
7638 val = vrp_evaluate_conditional_warnv_with_ops (gimple_cond_code (stmt),
7639 gimple_cond_lhs (stmt),
7640 gimple_cond_rhs (stmt),
7641 false, &sop, NULL);
7642 if (val)
7644 if (!sop)
7645 *taken_edge_p = find_taken_edge (gimple_bb (stmt), val);
7646 else
7648 if (dump_file && (dump_flags & TDF_DETAILS))
7649 fprintf (dump_file,
7650 "\nIgnoring predicate evaluation because "
7651 "it assumes that signed overflow is undefined");
7652 val = NULL_TREE;
7656 if (dump_file && (dump_flags & TDF_DETAILS))
7658 fprintf (dump_file, "\nPredicate evaluates to: ");
7659 if (val == NULL_TREE)
7660 fprintf (dump_file, "DON'T KNOW\n");
7661 else
7662 print_generic_stmt (dump_file, val, 0);
7665 return (*taken_edge_p) ? SSA_PROP_INTERESTING : SSA_PROP_VARYING;
7668 /* Searches the case label vector VEC for the index *IDX of the CASE_LABEL
7669 that includes the value VAL. The search is restricted to the range
7670 [START_IDX, n - 1] where n is the size of VEC.
7672 If there is a CASE_LABEL for VAL, its index is placed in IDX and true is
7673 returned.
7675 If there is no CASE_LABEL for VAL and there is one that is larger than VAL,
7676 it is placed in IDX and false is returned.
7678 If VAL is larger than any CASE_LABEL, n is placed on IDX and false is
7679 returned. */
7681 static bool
7682 find_case_label_index (gswitch *stmt, size_t start_idx, tree val, size_t *idx)
7684 size_t n = gimple_switch_num_labels (stmt);
7685 size_t low, high;
7687 /* Find case label for minimum of the value range or the next one.
7688 At each iteration we are searching in [low, high - 1]. */
7690 for (low = start_idx, high = n; high != low; )
7692 tree t;
7693 int cmp;
7694 /* Note that i != high, so we never ask for n. */
7695 size_t i = (high + low) / 2;
7696 t = gimple_switch_label (stmt, i);
7698 /* Cache the result of comparing CASE_LOW and val. */
7699 cmp = tree_int_cst_compare (CASE_LOW (t), val);
7701 if (cmp == 0)
7703 /* Ranges cannot be empty. */
7704 *idx = i;
7705 return true;
7707 else if (cmp > 0)
7708 high = i;
7709 else
7711 low = i + 1;
7712 if (CASE_HIGH (t) != NULL
7713 && tree_int_cst_compare (CASE_HIGH (t), val) >= 0)
7715 *idx = i;
7716 return true;
7721 *idx = high;
7722 return false;
7725 /* Searches the case label vector VEC for the range of CASE_LABELs that is used
7726 for values between MIN and MAX. The first index is placed in MIN_IDX. The
7727 last index is placed in MAX_IDX. If the range of CASE_LABELs is empty
7728 then MAX_IDX < MIN_IDX.
7729 Returns true if the default label is not needed. */
7731 static bool
7732 find_case_label_range (gswitch *stmt, tree min, tree max, size_t *min_idx,
7733 size_t *max_idx)
7735 size_t i, j;
7736 bool min_take_default = !find_case_label_index (stmt, 1, min, &i);
7737 bool max_take_default = !find_case_label_index (stmt, i, max, &j);
7739 if (i == j
7740 && min_take_default
7741 && max_take_default)
7743 /* Only the default case label reached.
7744 Return an empty range. */
7745 *min_idx = 1;
7746 *max_idx = 0;
7747 return false;
7749 else
7751 bool take_default = min_take_default || max_take_default;
7752 tree low, high;
7753 size_t k;
7755 if (max_take_default)
7756 j--;
7758 /* If the case label range is continuous, we do not need
7759 the default case label. Verify that. */
7760 high = CASE_LOW (gimple_switch_label (stmt, i));
7761 if (CASE_HIGH (gimple_switch_label (stmt, i)))
7762 high = CASE_HIGH (gimple_switch_label (stmt, i));
7763 for (k = i + 1; k <= j; ++k)
7765 low = CASE_LOW (gimple_switch_label (stmt, k));
7766 if (!integer_onep (int_const_binop (MINUS_EXPR, low, high)))
7768 take_default = true;
7769 break;
7771 high = low;
7772 if (CASE_HIGH (gimple_switch_label (stmt, k)))
7773 high = CASE_HIGH (gimple_switch_label (stmt, k));
7776 *min_idx = i;
7777 *max_idx = j;
7778 return !take_default;
7782 /* Searches the case label vector VEC for the ranges of CASE_LABELs that are
7783 used in range VR. The indices are placed in MIN_IDX1, MAX_IDX, MIN_IDX2 and
7784 MAX_IDX2. If the ranges of CASE_LABELs are empty then MAX_IDX1 < MIN_IDX1.
7785 Returns true if the default label is not needed. */
7787 static bool
7788 find_case_label_ranges (gswitch *stmt, value_range_t *vr, size_t *min_idx1,
7789 size_t *max_idx1, size_t *min_idx2,
7790 size_t *max_idx2)
7792 size_t i, j, k, l;
7793 unsigned int n = gimple_switch_num_labels (stmt);
7794 bool take_default;
7795 tree case_low, case_high;
7796 tree min = vr->min, max = vr->max;
7798 gcc_checking_assert (vr->type == VR_RANGE || vr->type == VR_ANTI_RANGE);
7800 take_default = !find_case_label_range (stmt, min, max, &i, &j);
7802 /* Set second range to emtpy. */
7803 *min_idx2 = 1;
7804 *max_idx2 = 0;
7806 if (vr->type == VR_RANGE)
7808 *min_idx1 = i;
7809 *max_idx1 = j;
7810 return !take_default;
7813 /* Set first range to all case labels. */
7814 *min_idx1 = 1;
7815 *max_idx1 = n - 1;
7817 if (i > j)
7818 return false;
7820 /* Make sure all the values of case labels [i , j] are contained in
7821 range [MIN, MAX]. */
7822 case_low = CASE_LOW (gimple_switch_label (stmt, i));
7823 case_high = CASE_HIGH (gimple_switch_label (stmt, j));
7824 if (tree_int_cst_compare (case_low, min) < 0)
7825 i += 1;
7826 if (case_high != NULL_TREE
7827 && tree_int_cst_compare (max, case_high) < 0)
7828 j -= 1;
7830 if (i > j)
7831 return false;
7833 /* If the range spans case labels [i, j], the corresponding anti-range spans
7834 the labels [1, i - 1] and [j + 1, n - 1]. */
7835 k = j + 1;
7836 l = n - 1;
7837 if (k > l)
7839 k = 1;
7840 l = 0;
7843 j = i - 1;
7844 i = 1;
7845 if (i > j)
7847 i = k;
7848 j = l;
7849 k = 1;
7850 l = 0;
7853 *min_idx1 = i;
7854 *max_idx1 = j;
7855 *min_idx2 = k;
7856 *max_idx2 = l;
7857 return false;
7860 /* Visit switch statement STMT. If we can determine which edge
7861 will be taken out of STMT's basic block, record it in
7862 *TAKEN_EDGE_P and return SSA_PROP_INTERESTING. Otherwise, return
7863 SSA_PROP_VARYING. */
7865 static enum ssa_prop_result
7866 vrp_visit_switch_stmt (gswitch *stmt, edge *taken_edge_p)
7868 tree op, val;
7869 value_range_t *vr;
7870 size_t i = 0, j = 0, k, l;
7871 bool take_default;
7873 *taken_edge_p = NULL;
7874 op = gimple_switch_index (stmt);
7875 if (TREE_CODE (op) != SSA_NAME)
7876 return SSA_PROP_VARYING;
7878 vr = get_value_range (op);
7879 if (dump_file && (dump_flags & TDF_DETAILS))
7881 fprintf (dump_file, "\nVisiting switch expression with operand ");
7882 print_generic_expr (dump_file, op, 0);
7883 fprintf (dump_file, " with known range ");
7884 dump_value_range (dump_file, vr);
7885 fprintf (dump_file, "\n");
7888 if ((vr->type != VR_RANGE
7889 && vr->type != VR_ANTI_RANGE)
7890 || symbolic_range_p (vr))
7891 return SSA_PROP_VARYING;
7893 /* Find the single edge that is taken from the switch expression. */
7894 take_default = !find_case_label_ranges (stmt, vr, &i, &j, &k, &l);
7896 /* Check if the range spans no CASE_LABEL. If so, we only reach the default
7897 label */
7898 if (j < i)
7900 gcc_assert (take_default);
7901 val = gimple_switch_default_label (stmt);
7903 else
7905 /* Check if labels with index i to j and maybe the default label
7906 are all reaching the same label. */
7908 val = gimple_switch_label (stmt, i);
7909 if (take_default
7910 && CASE_LABEL (gimple_switch_default_label (stmt))
7911 != 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;
7918 for (++i; i <= j; ++i)
7920 if (CASE_LABEL (gimple_switch_label (stmt, i)) != CASE_LABEL (val))
7922 if (dump_file && (dump_flags & TDF_DETAILS))
7923 fprintf (dump_file, " not a single destination for this "
7924 "range\n");
7925 return SSA_PROP_VARYING;
7928 for (; k <= l; ++k)
7930 if (CASE_LABEL (gimple_switch_label (stmt, k)) != CASE_LABEL (val))
7932 if (dump_file && (dump_flags & TDF_DETAILS))
7933 fprintf (dump_file, " not a single destination for this "
7934 "range\n");
7935 return SSA_PROP_VARYING;
7940 *taken_edge_p = find_edge (gimple_bb (stmt),
7941 label_to_block (CASE_LABEL (val)));
7943 if (dump_file && (dump_flags & TDF_DETAILS))
7945 fprintf (dump_file, " will take edge to ");
7946 print_generic_stmt (dump_file, CASE_LABEL (val), 0);
7949 return SSA_PROP_INTERESTING;
7953 /* Evaluate statement STMT. If the statement produces a useful range,
7954 return SSA_PROP_INTERESTING and record the SSA name with the
7955 interesting range into *OUTPUT_P.
7957 If STMT is a conditional branch and we can determine its truth
7958 value, the taken edge is recorded in *TAKEN_EDGE_P.
7960 If STMT produces a varying value, return SSA_PROP_VARYING. */
7962 static enum ssa_prop_result
7963 vrp_visit_stmt (gimple stmt, edge *taken_edge_p, tree *output_p)
7965 tree def;
7966 ssa_op_iter iter;
7968 if (dump_file && (dump_flags & TDF_DETAILS))
7970 fprintf (dump_file, "\nVisiting statement:\n");
7971 print_gimple_stmt (dump_file, stmt, 0, dump_flags);
7974 if (!stmt_interesting_for_vrp (stmt))
7975 gcc_assert (stmt_ends_bb_p (stmt));
7976 else if (is_gimple_assign (stmt) || is_gimple_call (stmt))
7977 return vrp_visit_assignment_or_call (stmt, output_p);
7978 else if (gimple_code (stmt) == GIMPLE_COND)
7979 return vrp_visit_cond_stmt (as_a <gcond *> (stmt), taken_edge_p);
7980 else if (gimple_code (stmt) == GIMPLE_SWITCH)
7981 return vrp_visit_switch_stmt (as_a <gswitch *> (stmt), taken_edge_p);
7983 /* All other statements produce nothing of interest for VRP, so mark
7984 their outputs varying and prevent further simulation. */
7985 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_DEF)
7986 set_value_range_to_varying (get_value_range (def));
7988 return SSA_PROP_VARYING;
7991 /* Union the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
7992 { VR1TYPE, VR0MIN, VR0MAX } and store the result
7993 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
7994 possible such range. The resulting range is not canonicalized. */
7996 static void
7997 union_ranges (enum value_range_type *vr0type,
7998 tree *vr0min, tree *vr0max,
7999 enum value_range_type vr1type,
8000 tree vr1min, tree vr1max)
8002 bool mineq = operand_equal_p (*vr0min, vr1min, 0);
8003 bool maxeq = operand_equal_p (*vr0max, vr1max, 0);
8005 /* [] is vr0, () is vr1 in the following classification comments. */
8006 if (mineq && maxeq)
8008 /* [( )] */
8009 if (*vr0type == vr1type)
8010 /* Nothing to do for equal ranges. */
8012 else if ((*vr0type == VR_RANGE
8013 && vr1type == VR_ANTI_RANGE)
8014 || (*vr0type == VR_ANTI_RANGE
8015 && vr1type == VR_RANGE))
8017 /* For anti-range with range union the result is varying. */
8018 goto give_up;
8020 else
8021 gcc_unreachable ();
8023 else if (operand_less_p (*vr0max, vr1min) == 1
8024 || operand_less_p (vr1max, *vr0min) == 1)
8026 /* [ ] ( ) or ( ) [ ]
8027 If the ranges have an empty intersection, result of the union
8028 operation is the anti-range or if both are anti-ranges
8029 it covers all. */
8030 if (*vr0type == VR_ANTI_RANGE
8031 && vr1type == VR_ANTI_RANGE)
8032 goto give_up;
8033 else if (*vr0type == VR_ANTI_RANGE
8034 && vr1type == VR_RANGE)
8036 else if (*vr0type == VR_RANGE
8037 && vr1type == VR_ANTI_RANGE)
8039 *vr0type = vr1type;
8040 *vr0min = vr1min;
8041 *vr0max = vr1max;
8043 else if (*vr0type == VR_RANGE
8044 && vr1type == VR_RANGE)
8046 /* The result is the convex hull of both ranges. */
8047 if (operand_less_p (*vr0max, vr1min) == 1)
8049 /* If the result can be an anti-range, create one. */
8050 if (TREE_CODE (*vr0max) == INTEGER_CST
8051 && TREE_CODE (vr1min) == INTEGER_CST
8052 && vrp_val_is_min (*vr0min)
8053 && vrp_val_is_max (vr1max))
8055 tree min = int_const_binop (PLUS_EXPR,
8056 *vr0max,
8057 build_int_cst (TREE_TYPE (*vr0max), 1));
8058 tree max = int_const_binop (MINUS_EXPR,
8059 vr1min,
8060 build_int_cst (TREE_TYPE (vr1min), 1));
8061 if (!operand_less_p (max, min))
8063 *vr0type = VR_ANTI_RANGE;
8064 *vr0min = min;
8065 *vr0max = max;
8067 else
8068 *vr0max = vr1max;
8070 else
8071 *vr0max = vr1max;
8073 else
8075 /* If the result can be an anti-range, create one. */
8076 if (TREE_CODE (vr1max) == INTEGER_CST
8077 && TREE_CODE (*vr0min) == INTEGER_CST
8078 && vrp_val_is_min (vr1min)
8079 && vrp_val_is_max (*vr0max))
8081 tree min = int_const_binop (PLUS_EXPR,
8082 vr1max,
8083 build_int_cst (TREE_TYPE (vr1max), 1));
8084 tree max = int_const_binop (MINUS_EXPR,
8085 *vr0min,
8086 build_int_cst (TREE_TYPE (*vr0min), 1));
8087 if (!operand_less_p (max, min))
8089 *vr0type = VR_ANTI_RANGE;
8090 *vr0min = min;
8091 *vr0max = max;
8093 else
8094 *vr0min = vr1min;
8096 else
8097 *vr0min = vr1min;
8100 else
8101 gcc_unreachable ();
8103 else if ((maxeq || operand_less_p (vr1max, *vr0max) == 1)
8104 && (mineq || operand_less_p (*vr0min, vr1min) == 1))
8106 /* [ ( ) ] or [( ) ] or [ ( )] */
8107 if (*vr0type == VR_RANGE
8108 && vr1type == VR_RANGE)
8110 else if (*vr0type == VR_ANTI_RANGE
8111 && vr1type == VR_ANTI_RANGE)
8113 *vr0type = vr1type;
8114 *vr0min = vr1min;
8115 *vr0max = vr1max;
8117 else if (*vr0type == VR_ANTI_RANGE
8118 && vr1type == VR_RANGE)
8120 /* Arbitrarily choose the right or left gap. */
8121 if (!mineq && TREE_CODE (vr1min) == INTEGER_CST)
8122 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
8123 build_int_cst (TREE_TYPE (vr1min), 1));
8124 else if (!maxeq && TREE_CODE (vr1max) == INTEGER_CST)
8125 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
8126 build_int_cst (TREE_TYPE (vr1max), 1));
8127 else
8128 goto give_up;
8130 else if (*vr0type == VR_RANGE
8131 && vr1type == VR_ANTI_RANGE)
8132 /* The result covers everything. */
8133 goto give_up;
8134 else
8135 gcc_unreachable ();
8137 else if ((maxeq || operand_less_p (*vr0max, vr1max) == 1)
8138 && (mineq || operand_less_p (vr1min, *vr0min) == 1))
8140 /* ( [ ] ) or ([ ] ) or ( [ ]) */
8141 if (*vr0type == VR_RANGE
8142 && vr1type == VR_RANGE)
8144 *vr0type = vr1type;
8145 *vr0min = vr1min;
8146 *vr0max = vr1max;
8148 else if (*vr0type == VR_ANTI_RANGE
8149 && vr1type == VR_ANTI_RANGE)
8151 else if (*vr0type == VR_RANGE
8152 && vr1type == VR_ANTI_RANGE)
8154 *vr0type = VR_ANTI_RANGE;
8155 if (!mineq && TREE_CODE (*vr0min) == INTEGER_CST)
8157 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
8158 build_int_cst (TREE_TYPE (*vr0min), 1));
8159 *vr0min = vr1min;
8161 else if (!maxeq && TREE_CODE (*vr0max) == INTEGER_CST)
8163 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
8164 build_int_cst (TREE_TYPE (*vr0max), 1));
8165 *vr0max = vr1max;
8167 else
8168 goto give_up;
8170 else if (*vr0type == VR_ANTI_RANGE
8171 && vr1type == VR_RANGE)
8172 /* The result covers everything. */
8173 goto give_up;
8174 else
8175 gcc_unreachable ();
8177 else if ((operand_less_p (vr1min, *vr0max) == 1
8178 || operand_equal_p (vr1min, *vr0max, 0))
8179 && operand_less_p (*vr0min, vr1min) == 1
8180 && operand_less_p (*vr0max, vr1max) == 1)
8182 /* [ ( ] ) or [ ]( ) */
8183 if (*vr0type == VR_RANGE
8184 && vr1type == VR_RANGE)
8185 *vr0max = vr1max;
8186 else if (*vr0type == VR_ANTI_RANGE
8187 && vr1type == VR_ANTI_RANGE)
8188 *vr0min = vr1min;
8189 else if (*vr0type == VR_ANTI_RANGE
8190 && vr1type == VR_RANGE)
8192 if (TREE_CODE (vr1min) == INTEGER_CST)
8193 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
8194 build_int_cst (TREE_TYPE (vr1min), 1));
8195 else
8196 goto give_up;
8198 else if (*vr0type == VR_RANGE
8199 && vr1type == VR_ANTI_RANGE)
8201 if (TREE_CODE (*vr0max) == INTEGER_CST)
8203 *vr0type = vr1type;
8204 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
8205 build_int_cst (TREE_TYPE (*vr0max), 1));
8206 *vr0max = vr1max;
8208 else
8209 goto give_up;
8211 else
8212 gcc_unreachable ();
8214 else if ((operand_less_p (*vr0min, vr1max) == 1
8215 || operand_equal_p (*vr0min, vr1max, 0))
8216 && operand_less_p (vr1min, *vr0min) == 1
8217 && operand_less_p (vr1max, *vr0max) == 1)
8219 /* ( [ ) ] or ( )[ ] */
8220 if (*vr0type == VR_RANGE
8221 && vr1type == VR_RANGE)
8222 *vr0min = vr1min;
8223 else if (*vr0type == VR_ANTI_RANGE
8224 && vr1type == VR_ANTI_RANGE)
8225 *vr0max = vr1max;
8226 else if (*vr0type == VR_ANTI_RANGE
8227 && vr1type == VR_RANGE)
8229 if (TREE_CODE (vr1max) == INTEGER_CST)
8230 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
8231 build_int_cst (TREE_TYPE (vr1max), 1));
8232 else
8233 goto give_up;
8235 else if (*vr0type == VR_RANGE
8236 && vr1type == VR_ANTI_RANGE)
8238 if (TREE_CODE (*vr0min) == INTEGER_CST)
8240 *vr0type = vr1type;
8241 *vr0min = vr1min;
8242 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
8243 build_int_cst (TREE_TYPE (*vr0min), 1));
8245 else
8246 goto give_up;
8248 else
8249 gcc_unreachable ();
8251 else
8252 goto give_up;
8254 return;
8256 give_up:
8257 *vr0type = VR_VARYING;
8258 *vr0min = NULL_TREE;
8259 *vr0max = NULL_TREE;
8262 /* Intersect the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
8263 { VR1TYPE, VR0MIN, VR0MAX } and store the result
8264 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
8265 possible such range. The resulting range is not canonicalized. */
8267 static void
8268 intersect_ranges (enum value_range_type *vr0type,
8269 tree *vr0min, tree *vr0max,
8270 enum value_range_type vr1type,
8271 tree vr1min, tree vr1max)
8273 bool mineq = operand_equal_p (*vr0min, vr1min, 0);
8274 bool maxeq = operand_equal_p (*vr0max, vr1max, 0);
8276 /* [] is vr0, () is vr1 in the following classification comments. */
8277 if (mineq && maxeq)
8279 /* [( )] */
8280 if (*vr0type == vr1type)
8281 /* Nothing to do for equal ranges. */
8283 else if ((*vr0type == VR_RANGE
8284 && vr1type == VR_ANTI_RANGE)
8285 || (*vr0type == VR_ANTI_RANGE
8286 && vr1type == VR_RANGE))
8288 /* For anti-range with range intersection the result is empty. */
8289 *vr0type = VR_UNDEFINED;
8290 *vr0min = NULL_TREE;
8291 *vr0max = NULL_TREE;
8293 else
8294 gcc_unreachable ();
8296 else if (operand_less_p (*vr0max, vr1min) == 1
8297 || operand_less_p (vr1max, *vr0min) == 1)
8299 /* [ ] ( ) or ( ) [ ]
8300 If the ranges have an empty intersection, the result of the
8301 intersect operation is the range for intersecting an
8302 anti-range with a range or empty when intersecting two ranges. */
8303 if (*vr0type == VR_RANGE
8304 && vr1type == VR_ANTI_RANGE)
8306 else if (*vr0type == VR_ANTI_RANGE
8307 && vr1type == VR_RANGE)
8309 *vr0type = vr1type;
8310 *vr0min = vr1min;
8311 *vr0max = vr1max;
8313 else if (*vr0type == VR_RANGE
8314 && vr1type == VR_RANGE)
8316 *vr0type = VR_UNDEFINED;
8317 *vr0min = NULL_TREE;
8318 *vr0max = NULL_TREE;
8320 else if (*vr0type == VR_ANTI_RANGE
8321 && vr1type == VR_ANTI_RANGE)
8323 /* If the anti-ranges are adjacent to each other merge them. */
8324 if (TREE_CODE (*vr0max) == INTEGER_CST
8325 && TREE_CODE (vr1min) == INTEGER_CST
8326 && operand_less_p (*vr0max, vr1min) == 1
8327 && integer_onep (int_const_binop (MINUS_EXPR,
8328 vr1min, *vr0max)))
8329 *vr0max = vr1max;
8330 else if (TREE_CODE (vr1max) == INTEGER_CST
8331 && TREE_CODE (*vr0min) == INTEGER_CST
8332 && operand_less_p (vr1max, *vr0min) == 1
8333 && integer_onep (int_const_binop (MINUS_EXPR,
8334 *vr0min, vr1max)))
8335 *vr0min = vr1min;
8336 /* Else arbitrarily take VR0. */
8339 else if ((maxeq || operand_less_p (vr1max, *vr0max) == 1)
8340 && (mineq || operand_less_p (*vr0min, vr1min) == 1))
8342 /* [ ( ) ] or [( ) ] or [ ( )] */
8343 if (*vr0type == VR_RANGE
8344 && vr1type == VR_RANGE)
8346 /* If both are ranges the result is the inner one. */
8347 *vr0type = vr1type;
8348 *vr0min = vr1min;
8349 *vr0max = vr1max;
8351 else if (*vr0type == VR_RANGE
8352 && vr1type == VR_ANTI_RANGE)
8354 /* Choose the right gap if the left one is empty. */
8355 if (mineq)
8357 if (TREE_CODE (vr1max) == INTEGER_CST)
8358 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
8359 build_int_cst (TREE_TYPE (vr1max), 1));
8360 else
8361 *vr0min = vr1max;
8363 /* Choose the left gap if the right one is empty. */
8364 else if (maxeq)
8366 if (TREE_CODE (vr1min) == INTEGER_CST)
8367 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
8368 build_int_cst (TREE_TYPE (vr1min), 1));
8369 else
8370 *vr0max = vr1min;
8372 /* Choose the anti-range if the range is effectively varying. */
8373 else if (vrp_val_is_min (*vr0min)
8374 && vrp_val_is_max (*vr0max))
8376 *vr0type = vr1type;
8377 *vr0min = vr1min;
8378 *vr0max = vr1max;
8380 /* Else choose the range. */
8382 else if (*vr0type == VR_ANTI_RANGE
8383 && vr1type == VR_ANTI_RANGE)
8384 /* If both are anti-ranges the result is the outer one. */
8386 else if (*vr0type == VR_ANTI_RANGE
8387 && vr1type == VR_RANGE)
8389 /* The intersection is empty. */
8390 *vr0type = VR_UNDEFINED;
8391 *vr0min = NULL_TREE;
8392 *vr0max = NULL_TREE;
8394 else
8395 gcc_unreachable ();
8397 else if ((maxeq || operand_less_p (*vr0max, vr1max) == 1)
8398 && (mineq || operand_less_p (vr1min, *vr0min) == 1))
8400 /* ( [ ] ) or ([ ] ) or ( [ ]) */
8401 if (*vr0type == VR_RANGE
8402 && vr1type == VR_RANGE)
8403 /* Choose the inner range. */
8405 else if (*vr0type == VR_ANTI_RANGE
8406 && vr1type == VR_RANGE)
8408 /* Choose the right gap if the left is empty. */
8409 if (mineq)
8411 *vr0type = VR_RANGE;
8412 if (TREE_CODE (*vr0max) == INTEGER_CST)
8413 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
8414 build_int_cst (TREE_TYPE (*vr0max), 1));
8415 else
8416 *vr0min = *vr0max;
8417 *vr0max = vr1max;
8419 /* Choose the left gap if the right is empty. */
8420 else if (maxeq)
8422 *vr0type = VR_RANGE;
8423 if (TREE_CODE (*vr0min) == INTEGER_CST)
8424 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
8425 build_int_cst (TREE_TYPE (*vr0min), 1));
8426 else
8427 *vr0max = *vr0min;
8428 *vr0min = vr1min;
8430 /* Choose the anti-range if the range is effectively varying. */
8431 else if (vrp_val_is_min (vr1min)
8432 && vrp_val_is_max (vr1max))
8434 /* Else choose the range. */
8435 else
8437 *vr0type = vr1type;
8438 *vr0min = vr1min;
8439 *vr0max = vr1max;
8442 else if (*vr0type == VR_ANTI_RANGE
8443 && vr1type == VR_ANTI_RANGE)
8445 /* If both are anti-ranges the result is the outer one. */
8446 *vr0type = vr1type;
8447 *vr0min = vr1min;
8448 *vr0max = vr1max;
8450 else if (vr1type == VR_ANTI_RANGE
8451 && *vr0type == VR_RANGE)
8453 /* The intersection is empty. */
8454 *vr0type = VR_UNDEFINED;
8455 *vr0min = NULL_TREE;
8456 *vr0max = NULL_TREE;
8458 else
8459 gcc_unreachable ();
8461 else if ((operand_less_p (vr1min, *vr0max) == 1
8462 || operand_equal_p (vr1min, *vr0max, 0))
8463 && operand_less_p (*vr0min, vr1min) == 1)
8465 /* [ ( ] ) or [ ]( ) */
8466 if (*vr0type == VR_ANTI_RANGE
8467 && vr1type == VR_ANTI_RANGE)
8468 *vr0max = vr1max;
8469 else if (*vr0type == VR_RANGE
8470 && vr1type == VR_RANGE)
8471 *vr0min = vr1min;
8472 else if (*vr0type == VR_RANGE
8473 && vr1type == VR_ANTI_RANGE)
8475 if (TREE_CODE (vr1min) == INTEGER_CST)
8476 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
8477 build_int_cst (TREE_TYPE (vr1min), 1));
8478 else
8479 *vr0max = vr1min;
8481 else if (*vr0type == VR_ANTI_RANGE
8482 && vr1type == VR_RANGE)
8484 *vr0type = VR_RANGE;
8485 if (TREE_CODE (*vr0max) == INTEGER_CST)
8486 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
8487 build_int_cst (TREE_TYPE (*vr0max), 1));
8488 else
8489 *vr0min = *vr0max;
8490 *vr0max = vr1max;
8492 else
8493 gcc_unreachable ();
8495 else if ((operand_less_p (*vr0min, vr1max) == 1
8496 || operand_equal_p (*vr0min, vr1max, 0))
8497 && operand_less_p (vr1min, *vr0min) == 1)
8499 /* ( [ ) ] or ( )[ ] */
8500 if (*vr0type == VR_ANTI_RANGE
8501 && vr1type == VR_ANTI_RANGE)
8502 *vr0min = vr1min;
8503 else if (*vr0type == VR_RANGE
8504 && vr1type == VR_RANGE)
8505 *vr0max = vr1max;
8506 else if (*vr0type == VR_RANGE
8507 && vr1type == VR_ANTI_RANGE)
8509 if (TREE_CODE (vr1max) == INTEGER_CST)
8510 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
8511 build_int_cst (TREE_TYPE (vr1max), 1));
8512 else
8513 *vr0min = vr1max;
8515 else if (*vr0type == VR_ANTI_RANGE
8516 && vr1type == VR_RANGE)
8518 *vr0type = VR_RANGE;
8519 if (TREE_CODE (*vr0min) == INTEGER_CST)
8520 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
8521 build_int_cst (TREE_TYPE (*vr0min), 1));
8522 else
8523 *vr0max = *vr0min;
8524 *vr0min = vr1min;
8526 else
8527 gcc_unreachable ();
8530 /* As a fallback simply use { *VRTYPE, *VR0MIN, *VR0MAX } as
8531 result for the intersection. That's always a conservative
8532 correct estimate. */
8534 return;
8538 /* Intersect the two value-ranges *VR0 and *VR1 and store the result
8539 in *VR0. This may not be the smallest possible such range. */
8541 static void
8542 vrp_intersect_ranges_1 (value_range_t *vr0, value_range_t *vr1)
8544 value_range_t saved;
8546 /* If either range is VR_VARYING the other one wins. */
8547 if (vr1->type == VR_VARYING)
8548 return;
8549 if (vr0->type == VR_VARYING)
8551 copy_value_range (vr0, vr1);
8552 return;
8555 /* When either range is VR_UNDEFINED the resulting range is
8556 VR_UNDEFINED, too. */
8557 if (vr0->type == VR_UNDEFINED)
8558 return;
8559 if (vr1->type == VR_UNDEFINED)
8561 set_value_range_to_undefined (vr0);
8562 return;
8565 /* Save the original vr0 so we can return it as conservative intersection
8566 result when our worker turns things to varying. */
8567 saved = *vr0;
8568 intersect_ranges (&vr0->type, &vr0->min, &vr0->max,
8569 vr1->type, vr1->min, vr1->max);
8570 /* Make sure to canonicalize the result though as the inversion of a
8571 VR_RANGE can still be a VR_RANGE. */
8572 set_and_canonicalize_value_range (vr0, vr0->type,
8573 vr0->min, vr0->max, vr0->equiv);
8574 /* If that failed, use the saved original VR0. */
8575 if (vr0->type == VR_VARYING)
8577 *vr0 = saved;
8578 return;
8580 /* If the result is VR_UNDEFINED there is no need to mess with
8581 the equivalencies. */
8582 if (vr0->type == VR_UNDEFINED)
8583 return;
8585 /* The resulting set of equivalences for range intersection is the union of
8586 the two sets. */
8587 if (vr0->equiv && vr1->equiv && vr0->equiv != vr1->equiv)
8588 bitmap_ior_into (vr0->equiv, vr1->equiv);
8589 else if (vr1->equiv && !vr0->equiv)
8590 bitmap_copy (vr0->equiv, vr1->equiv);
8593 static void
8594 vrp_intersect_ranges (value_range_t *vr0, value_range_t *vr1)
8596 if (dump_file && (dump_flags & TDF_DETAILS))
8598 fprintf (dump_file, "Intersecting\n ");
8599 dump_value_range (dump_file, vr0);
8600 fprintf (dump_file, "\nand\n ");
8601 dump_value_range (dump_file, vr1);
8602 fprintf (dump_file, "\n");
8604 vrp_intersect_ranges_1 (vr0, vr1);
8605 if (dump_file && (dump_flags & TDF_DETAILS))
8607 fprintf (dump_file, "to\n ");
8608 dump_value_range (dump_file, vr0);
8609 fprintf (dump_file, "\n");
8613 /* Meet operation for value ranges. Given two value ranges VR0 and
8614 VR1, store in VR0 a range that contains both VR0 and VR1. This
8615 may not be the smallest possible such range. */
8617 static void
8618 vrp_meet_1 (value_range_t *vr0, value_range_t *vr1)
8620 value_range_t saved;
8622 if (vr0->type == VR_UNDEFINED)
8624 set_value_range (vr0, vr1->type, vr1->min, vr1->max, vr1->equiv);
8625 return;
8628 if (vr1->type == VR_UNDEFINED)
8630 /* VR0 already has the resulting range. */
8631 return;
8634 if (vr0->type == VR_VARYING)
8636 /* Nothing to do. VR0 already has the resulting range. */
8637 return;
8640 if (vr1->type == VR_VARYING)
8642 set_value_range_to_varying (vr0);
8643 return;
8646 saved = *vr0;
8647 union_ranges (&vr0->type, &vr0->min, &vr0->max,
8648 vr1->type, vr1->min, vr1->max);
8649 if (vr0->type == VR_VARYING)
8651 /* Failed to find an efficient meet. Before giving up and setting
8652 the result to VARYING, see if we can at least derive a useful
8653 anti-range. FIXME, all this nonsense about distinguishing
8654 anti-ranges from ranges is necessary because of the odd
8655 semantics of range_includes_zero_p and friends. */
8656 if (((saved.type == VR_RANGE
8657 && range_includes_zero_p (saved.min, saved.max) == 0)
8658 || (saved.type == VR_ANTI_RANGE
8659 && range_includes_zero_p (saved.min, saved.max) == 1))
8660 && ((vr1->type == VR_RANGE
8661 && range_includes_zero_p (vr1->min, vr1->max) == 0)
8662 || (vr1->type == VR_ANTI_RANGE
8663 && range_includes_zero_p (vr1->min, vr1->max) == 1)))
8665 set_value_range_to_nonnull (vr0, TREE_TYPE (saved.min));
8667 /* Since this meet operation did not result from the meeting of
8668 two equivalent names, VR0 cannot have any equivalences. */
8669 if (vr0->equiv)
8670 bitmap_clear (vr0->equiv);
8671 return;
8674 set_value_range_to_varying (vr0);
8675 return;
8677 set_and_canonicalize_value_range (vr0, vr0->type, vr0->min, vr0->max,
8678 vr0->equiv);
8679 if (vr0->type == VR_VARYING)
8680 return;
8682 /* The resulting set of equivalences is always the intersection of
8683 the two sets. */
8684 if (vr0->equiv && vr1->equiv && vr0->equiv != vr1->equiv)
8685 bitmap_and_into (vr0->equiv, vr1->equiv);
8686 else if (vr0->equiv && !vr1->equiv)
8687 bitmap_clear (vr0->equiv);
8690 static void
8691 vrp_meet (value_range_t *vr0, value_range_t *vr1)
8693 if (dump_file && (dump_flags & TDF_DETAILS))
8695 fprintf (dump_file, "Meeting\n ");
8696 dump_value_range (dump_file, vr0);
8697 fprintf (dump_file, "\nand\n ");
8698 dump_value_range (dump_file, vr1);
8699 fprintf (dump_file, "\n");
8701 vrp_meet_1 (vr0, vr1);
8702 if (dump_file && (dump_flags & TDF_DETAILS))
8704 fprintf (dump_file, "to\n ");
8705 dump_value_range (dump_file, vr0);
8706 fprintf (dump_file, "\n");
8711 /* Visit all arguments for PHI node PHI that flow through executable
8712 edges. If a valid value range can be derived from all the incoming
8713 value ranges, set a new range for the LHS of PHI. */
8715 static enum ssa_prop_result
8716 vrp_visit_phi_node (gphi *phi)
8718 size_t i;
8719 tree lhs = PHI_RESULT (phi);
8720 value_range_t *lhs_vr = get_value_range (lhs);
8721 value_range_t vr_result = VR_INITIALIZER;
8722 bool first = true;
8723 int edges, old_edges;
8724 struct loop *l;
8726 if (dump_file && (dump_flags & TDF_DETAILS))
8728 fprintf (dump_file, "\nVisiting PHI node: ");
8729 print_gimple_stmt (dump_file, phi, 0, dump_flags);
8732 edges = 0;
8733 for (i = 0; i < gimple_phi_num_args (phi); i++)
8735 edge e = gimple_phi_arg_edge (phi, i);
8737 if (dump_file && (dump_flags & TDF_DETAILS))
8739 fprintf (dump_file,
8740 " Argument #%d (%d -> %d %sexecutable)\n",
8741 (int) i, e->src->index, e->dest->index,
8742 (e->flags & EDGE_EXECUTABLE) ? "" : "not ");
8745 if (e->flags & EDGE_EXECUTABLE)
8747 tree arg = PHI_ARG_DEF (phi, i);
8748 value_range_t vr_arg;
8750 ++edges;
8752 if (TREE_CODE (arg) == SSA_NAME)
8754 vr_arg = *(get_value_range (arg));
8755 /* Do not allow equivalences or symbolic ranges to leak in from
8756 backedges. That creates invalid equivalencies.
8757 See PR53465 and PR54767. */
8758 if (e->flags & EDGE_DFS_BACK)
8760 if (vr_arg.type == VR_RANGE
8761 || vr_arg.type == VR_ANTI_RANGE)
8763 vr_arg.equiv = NULL;
8764 if (symbolic_range_p (&vr_arg))
8766 vr_arg.type = VR_VARYING;
8767 vr_arg.min = NULL_TREE;
8768 vr_arg.max = NULL_TREE;
8772 else
8774 /* If the non-backedge arguments range is VR_VARYING then
8775 we can still try recording a simple equivalence. */
8776 if (vr_arg.type == VR_VARYING)
8778 vr_arg.type = VR_RANGE;
8779 vr_arg.min = arg;
8780 vr_arg.max = arg;
8781 vr_arg.equiv = NULL;
8785 else
8787 if (TREE_OVERFLOW_P (arg))
8788 arg = drop_tree_overflow (arg);
8790 vr_arg.type = VR_RANGE;
8791 vr_arg.min = arg;
8792 vr_arg.max = arg;
8793 vr_arg.equiv = NULL;
8796 if (dump_file && (dump_flags & TDF_DETAILS))
8798 fprintf (dump_file, "\t");
8799 print_generic_expr (dump_file, arg, dump_flags);
8800 fprintf (dump_file, ": ");
8801 dump_value_range (dump_file, &vr_arg);
8802 fprintf (dump_file, "\n");
8805 if (first)
8806 copy_value_range (&vr_result, &vr_arg);
8807 else
8808 vrp_meet (&vr_result, &vr_arg);
8809 first = false;
8811 if (vr_result.type == VR_VARYING)
8812 break;
8816 if (vr_result.type == VR_VARYING)
8817 goto varying;
8818 else if (vr_result.type == VR_UNDEFINED)
8819 goto update_range;
8821 old_edges = vr_phi_edge_counts[SSA_NAME_VERSION (lhs)];
8822 vr_phi_edge_counts[SSA_NAME_VERSION (lhs)] = edges;
8824 /* To prevent infinite iterations in the algorithm, derive ranges
8825 when the new value is slightly bigger or smaller than the
8826 previous one. We don't do this if we have seen a new executable
8827 edge; this helps us avoid an overflow infinity for conditionals
8828 which are not in a loop. If the old value-range was VR_UNDEFINED
8829 use the updated range and iterate one more time. */
8830 if (edges > 0
8831 && gimple_phi_num_args (phi) > 1
8832 && edges == old_edges
8833 && lhs_vr->type != VR_UNDEFINED)
8835 /* Compare old and new ranges, fall back to varying if the
8836 values are not comparable. */
8837 int cmp_min = compare_values (lhs_vr->min, vr_result.min);
8838 if (cmp_min == -2)
8839 goto varying;
8840 int cmp_max = compare_values (lhs_vr->max, vr_result.max);
8841 if (cmp_max == -2)
8842 goto varying;
8844 /* For non VR_RANGE or for pointers fall back to varying if
8845 the range changed. */
8846 if ((lhs_vr->type != VR_RANGE || vr_result.type != VR_RANGE
8847 || POINTER_TYPE_P (TREE_TYPE (lhs)))
8848 && (cmp_min != 0 || cmp_max != 0))
8849 goto varying;
8851 /* If the new minimum is larger than the previous one
8852 retain the old value. If the new minimum value is smaller
8853 than the previous one and not -INF go all the way to -INF + 1.
8854 In the first case, to avoid infinite bouncing between different
8855 minimums, and in the other case to avoid iterating millions of
8856 times to reach -INF. Going to -INF + 1 also lets the following
8857 iteration compute whether there will be any overflow, at the
8858 expense of one additional iteration. */
8859 if (cmp_min < 0)
8860 vr_result.min = lhs_vr->min;
8861 else if (cmp_min > 0
8862 && !vrp_val_is_min (vr_result.min))
8863 vr_result.min
8864 = int_const_binop (PLUS_EXPR,
8865 vrp_val_min (TREE_TYPE (vr_result.min)),
8866 build_int_cst (TREE_TYPE (vr_result.min), 1));
8868 /* Similarly for the maximum value. */
8869 if (cmp_max > 0)
8870 vr_result.max = lhs_vr->max;
8871 else if (cmp_max < 0
8872 && !vrp_val_is_max (vr_result.max))
8873 vr_result.max
8874 = int_const_binop (MINUS_EXPR,
8875 vrp_val_max (TREE_TYPE (vr_result.min)),
8876 build_int_cst (TREE_TYPE (vr_result.min), 1));
8878 /* If we dropped either bound to +-INF then if this is a loop
8879 PHI node SCEV may known more about its value-range. */
8880 if ((cmp_min > 0 || cmp_min < 0
8881 || cmp_max < 0 || cmp_max > 0)
8882 && (l = loop_containing_stmt (phi))
8883 && l->header == gimple_bb (phi))
8884 adjust_range_with_scev (&vr_result, l, phi, lhs);
8886 /* If we will end up with a (-INF, +INF) range, set it to
8887 VARYING. Same if the previous max value was invalid for
8888 the type and we end up with vr_result.min > vr_result.max. */
8889 if ((vrp_val_is_max (vr_result.max)
8890 && vrp_val_is_min (vr_result.min))
8891 || compare_values (vr_result.min,
8892 vr_result.max) > 0)
8893 goto varying;
8896 /* If the new range is different than the previous value, keep
8897 iterating. */
8898 update_range:
8899 if (update_value_range (lhs, &vr_result))
8901 if (dump_file && (dump_flags & TDF_DETAILS))
8903 fprintf (dump_file, "Found new range for ");
8904 print_generic_expr (dump_file, lhs, 0);
8905 fprintf (dump_file, ": ");
8906 dump_value_range (dump_file, &vr_result);
8907 fprintf (dump_file, "\n");
8910 if (vr_result.type == VR_VARYING)
8911 return SSA_PROP_VARYING;
8913 return SSA_PROP_INTERESTING;
8916 /* Nothing changed, don't add outgoing edges. */
8917 return SSA_PROP_NOT_INTERESTING;
8919 /* No match found. Set the LHS to VARYING. */
8920 varying:
8921 set_value_range_to_varying (lhs_vr);
8922 return SSA_PROP_VARYING;
8925 /* Simplify boolean operations if the source is known
8926 to be already a boolean. */
8927 static bool
8928 simplify_truth_ops_using_ranges (gimple_stmt_iterator *gsi, gimple stmt)
8930 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
8931 tree lhs, op0, op1;
8932 bool need_conversion;
8934 /* We handle only !=/== case here. */
8935 gcc_assert (rhs_code == EQ_EXPR || rhs_code == NE_EXPR);
8937 op0 = gimple_assign_rhs1 (stmt);
8938 if (!op_with_boolean_value_range_p (op0))
8939 return false;
8941 op1 = gimple_assign_rhs2 (stmt);
8942 if (!op_with_boolean_value_range_p (op1))
8943 return false;
8945 /* Reduce number of cases to handle to NE_EXPR. As there is no
8946 BIT_XNOR_EXPR we cannot replace A == B with a single statement. */
8947 if (rhs_code == EQ_EXPR)
8949 if (TREE_CODE (op1) == INTEGER_CST)
8950 op1 = int_const_binop (BIT_XOR_EXPR, op1,
8951 build_int_cst (TREE_TYPE (op1), 1));
8952 else
8953 return false;
8956 lhs = gimple_assign_lhs (stmt);
8957 need_conversion
8958 = !useless_type_conversion_p (TREE_TYPE (lhs), TREE_TYPE (op0));
8960 /* Make sure to not sign-extend a 1-bit 1 when converting the result. */
8961 if (need_conversion
8962 && !TYPE_UNSIGNED (TREE_TYPE (op0))
8963 && TYPE_PRECISION (TREE_TYPE (op0)) == 1
8964 && TYPE_PRECISION (TREE_TYPE (lhs)) > 1)
8965 return false;
8967 /* For A != 0 we can substitute A itself. */
8968 if (integer_zerop (op1))
8969 gimple_assign_set_rhs_with_ops (gsi,
8970 need_conversion
8971 ? NOP_EXPR : TREE_CODE (op0), op0);
8972 /* For A != B we substitute A ^ B. Either with conversion. */
8973 else if (need_conversion)
8975 tree tem = make_ssa_name (TREE_TYPE (op0));
8976 gassign *newop
8977 = gimple_build_assign (tem, BIT_XOR_EXPR, op0, op1);
8978 gsi_insert_before (gsi, newop, GSI_SAME_STMT);
8979 gimple_assign_set_rhs_with_ops (gsi, NOP_EXPR, tem);
8981 /* Or without. */
8982 else
8983 gimple_assign_set_rhs_with_ops (gsi, BIT_XOR_EXPR, op0, op1);
8984 update_stmt (gsi_stmt (*gsi));
8986 return true;
8989 /* Simplify a division or modulo operator to a right shift or
8990 bitwise and if the first operand is unsigned or is greater
8991 than zero and the second operand is an exact power of two.
8992 For TRUNC_MOD_EXPR op0 % op1 with constant op1, optimize it
8993 into just op0 if op0's range is known to be a subset of
8994 [-op1 + 1, op1 - 1] for signed and [0, op1 - 1] for unsigned
8995 modulo. */
8997 static bool
8998 simplify_div_or_mod_using_ranges (gimple stmt)
9000 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
9001 tree val = NULL;
9002 tree op0 = gimple_assign_rhs1 (stmt);
9003 tree op1 = gimple_assign_rhs2 (stmt);
9004 value_range_t *vr = get_value_range (op0);
9006 if (rhs_code == TRUNC_MOD_EXPR
9007 && TREE_CODE (op1) == INTEGER_CST
9008 && tree_int_cst_sgn (op1) == 1
9009 && range_int_cst_p (vr)
9010 && tree_int_cst_lt (vr->max, op1))
9012 if (TYPE_UNSIGNED (TREE_TYPE (op0))
9013 || tree_int_cst_sgn (vr->min) >= 0
9014 || tree_int_cst_lt (fold_unary (NEGATE_EXPR, TREE_TYPE (op1), op1),
9015 vr->min))
9017 /* If op0 already has the range op0 % op1 has,
9018 then TRUNC_MOD_EXPR won't change anything. */
9019 gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
9020 gimple_assign_set_rhs_from_tree (&gsi, op0);
9021 update_stmt (stmt);
9022 return true;
9026 if (!integer_pow2p (op1))
9027 return false;
9029 if (TYPE_UNSIGNED (TREE_TYPE (op0)))
9031 val = integer_one_node;
9033 else
9035 bool sop = false;
9037 val = compare_range_with_value (GE_EXPR, vr, integer_zero_node, &sop);
9039 if (val
9040 && sop
9041 && integer_onep (val)
9042 && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC))
9044 location_t location;
9046 if (!gimple_has_location (stmt))
9047 location = input_location;
9048 else
9049 location = gimple_location (stmt);
9050 warning_at (location, OPT_Wstrict_overflow,
9051 "assuming signed overflow does not occur when "
9052 "simplifying %</%> or %<%%%> to %<>>%> or %<&%>");
9056 if (val && integer_onep (val))
9058 tree t;
9060 if (rhs_code == TRUNC_DIV_EXPR)
9062 t = build_int_cst (integer_type_node, tree_log2 (op1));
9063 gimple_assign_set_rhs_code (stmt, RSHIFT_EXPR);
9064 gimple_assign_set_rhs1 (stmt, op0);
9065 gimple_assign_set_rhs2 (stmt, t);
9067 else
9069 t = build_int_cst (TREE_TYPE (op1), 1);
9070 t = int_const_binop (MINUS_EXPR, op1, t);
9071 t = fold_convert (TREE_TYPE (op0), t);
9073 gimple_assign_set_rhs_code (stmt, BIT_AND_EXPR);
9074 gimple_assign_set_rhs1 (stmt, op0);
9075 gimple_assign_set_rhs2 (stmt, t);
9078 update_stmt (stmt);
9079 return true;
9082 return false;
9085 /* Simplify a min or max if the ranges of the two operands are
9086 disjoint. Return true if we do simplify. */
9088 static bool
9089 simplify_min_or_max_using_ranges (gimple stmt)
9091 tree op0 = gimple_assign_rhs1 (stmt);
9092 tree op1 = gimple_assign_rhs2 (stmt);
9093 bool sop = false;
9094 tree val;
9096 val = (vrp_evaluate_conditional_warnv_with_ops_using_ranges
9097 (LE_EXPR, op0, op1, &sop));
9098 if (!val)
9100 sop = false;
9101 val = (vrp_evaluate_conditional_warnv_with_ops_using_ranges
9102 (LT_EXPR, op0, op1, &sop));
9105 if (val)
9107 if (sop && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC))
9109 location_t location;
9111 if (!gimple_has_location (stmt))
9112 location = input_location;
9113 else
9114 location = gimple_location (stmt);
9115 warning_at (location, OPT_Wstrict_overflow,
9116 "assuming signed overflow does not occur when "
9117 "simplifying %<min/max (X,Y)%> to %<X%> or %<Y%>");
9120 /* VAL == TRUE -> OP0 < or <= op1
9121 VAL == FALSE -> OP0 > or >= op1. */
9122 tree res = ((gimple_assign_rhs_code (stmt) == MAX_EXPR)
9123 == integer_zerop (val)) ? op0 : op1;
9124 gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
9125 gimple_assign_set_rhs_from_tree (&gsi, res);
9126 update_stmt (stmt);
9127 return true;
9130 return false;
9133 /* If the operand to an ABS_EXPR is >= 0, then eliminate the
9134 ABS_EXPR. If the operand is <= 0, then simplify the
9135 ABS_EXPR into a NEGATE_EXPR. */
9137 static bool
9138 simplify_abs_using_ranges (gimple stmt)
9140 tree op = gimple_assign_rhs1 (stmt);
9141 value_range_t *vr = get_value_range (op);
9143 if (vr)
9145 tree val = NULL;
9146 bool sop = false;
9148 val = compare_range_with_value (LE_EXPR, vr, integer_zero_node, &sop);
9149 if (!val)
9151 /* The range is neither <= 0 nor > 0. Now see if it is
9152 either < 0 or >= 0. */
9153 sop = false;
9154 val = compare_range_with_value (LT_EXPR, vr, integer_zero_node,
9155 &sop);
9158 if (val)
9160 if (sop && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC))
9162 location_t location;
9164 if (!gimple_has_location (stmt))
9165 location = input_location;
9166 else
9167 location = gimple_location (stmt);
9168 warning_at (location, OPT_Wstrict_overflow,
9169 "assuming signed overflow does not occur when "
9170 "simplifying %<abs (X)%> to %<X%> or %<-X%>");
9173 gimple_assign_set_rhs1 (stmt, op);
9174 if (integer_zerop (val))
9175 gimple_assign_set_rhs_code (stmt, SSA_NAME);
9176 else
9177 gimple_assign_set_rhs_code (stmt, NEGATE_EXPR);
9178 update_stmt (stmt);
9179 return true;
9183 return false;
9186 /* Optimize away redundant BIT_AND_EXPR and BIT_IOR_EXPR.
9187 If all the bits that are being cleared by & are already
9188 known to be zero from VR, or all the bits that are being
9189 set by | are already known to be one from VR, the bit
9190 operation is redundant. */
9192 static bool
9193 simplify_bit_ops_using_ranges (gimple_stmt_iterator *gsi, gimple stmt)
9195 tree op0 = gimple_assign_rhs1 (stmt);
9196 tree op1 = gimple_assign_rhs2 (stmt);
9197 tree op = NULL_TREE;
9198 value_range_t vr0 = VR_INITIALIZER;
9199 value_range_t vr1 = VR_INITIALIZER;
9200 wide_int may_be_nonzero0, may_be_nonzero1;
9201 wide_int must_be_nonzero0, must_be_nonzero1;
9202 wide_int mask;
9204 if (TREE_CODE (op0) == SSA_NAME)
9205 vr0 = *(get_value_range (op0));
9206 else if (is_gimple_min_invariant (op0))
9207 set_value_range_to_value (&vr0, op0, NULL);
9208 else
9209 return false;
9211 if (TREE_CODE (op1) == SSA_NAME)
9212 vr1 = *(get_value_range (op1));
9213 else if (is_gimple_min_invariant (op1))
9214 set_value_range_to_value (&vr1, op1, NULL);
9215 else
9216 return false;
9218 if (!zero_nonzero_bits_from_vr (TREE_TYPE (op0), &vr0, &may_be_nonzero0,
9219 &must_be_nonzero0))
9220 return false;
9221 if (!zero_nonzero_bits_from_vr (TREE_TYPE (op1), &vr1, &may_be_nonzero1,
9222 &must_be_nonzero1))
9223 return false;
9225 switch (gimple_assign_rhs_code (stmt))
9227 case BIT_AND_EXPR:
9228 mask = may_be_nonzero0.and_not (must_be_nonzero1);
9229 if (mask == 0)
9231 op = op0;
9232 break;
9234 mask = may_be_nonzero1.and_not (must_be_nonzero0);
9235 if (mask == 0)
9237 op = op1;
9238 break;
9240 break;
9241 case BIT_IOR_EXPR:
9242 mask = may_be_nonzero0.and_not (must_be_nonzero1);
9243 if (mask == 0)
9245 op = op1;
9246 break;
9248 mask = may_be_nonzero1.and_not (must_be_nonzero0);
9249 if (mask == 0)
9251 op = op0;
9252 break;
9254 break;
9255 default:
9256 gcc_unreachable ();
9259 if (op == NULL_TREE)
9260 return false;
9262 gimple_assign_set_rhs_with_ops (gsi, TREE_CODE (op), op);
9263 update_stmt (gsi_stmt (*gsi));
9264 return true;
9267 /* We are comparing trees OP0 and OP1 using COND_CODE. OP0 has
9268 a known value range VR.
9270 If there is one and only one value which will satisfy the
9271 conditional, then return that value. Else return NULL.
9273 If signed overflow must be undefined for the value to satisfy
9274 the conditional, then set *STRICT_OVERFLOW_P to true. */
9276 static tree
9277 test_for_singularity (enum tree_code cond_code, tree op0,
9278 tree op1, value_range_t *vr,
9279 bool *strict_overflow_p)
9281 tree min = NULL;
9282 tree max = NULL;
9284 /* Extract minimum/maximum values which satisfy the
9285 the conditional as it was written. */
9286 if (cond_code == LE_EXPR || cond_code == LT_EXPR)
9288 /* This should not be negative infinity; there is no overflow
9289 here. */
9290 min = TYPE_MIN_VALUE (TREE_TYPE (op0));
9292 max = op1;
9293 if (cond_code == LT_EXPR && !is_overflow_infinity (max))
9295 tree one = build_int_cst (TREE_TYPE (op0), 1);
9296 max = fold_build2 (MINUS_EXPR, TREE_TYPE (op0), max, one);
9297 if (EXPR_P (max))
9298 TREE_NO_WARNING (max) = 1;
9301 else if (cond_code == GE_EXPR || cond_code == GT_EXPR)
9303 /* This should not be positive infinity; there is no overflow
9304 here. */
9305 max = TYPE_MAX_VALUE (TREE_TYPE (op0));
9307 min = op1;
9308 if (cond_code == GT_EXPR && !is_overflow_infinity (min))
9310 tree one = build_int_cst (TREE_TYPE (op0), 1);
9311 min = fold_build2 (PLUS_EXPR, TREE_TYPE (op0), min, one);
9312 if (EXPR_P (min))
9313 TREE_NO_WARNING (min) = 1;
9317 /* Now refine the minimum and maximum values using any
9318 value range information we have for op0. */
9319 if (min && max)
9321 if (compare_values (vr->min, min) == 1)
9322 min = vr->min;
9323 if (compare_values (vr->max, max) == -1)
9324 max = vr->max;
9326 /* If the new min/max values have converged to a single value,
9327 then there is only one value which can satisfy the condition,
9328 return that value. */
9329 if (operand_equal_p (min, max, 0) && is_gimple_min_invariant (min))
9331 if ((cond_code == LE_EXPR || cond_code == LT_EXPR)
9332 && is_overflow_infinity (vr->max))
9333 *strict_overflow_p = true;
9334 if ((cond_code == GE_EXPR || cond_code == GT_EXPR)
9335 && is_overflow_infinity (vr->min))
9336 *strict_overflow_p = true;
9338 return min;
9341 return NULL;
9344 /* Return whether the value range *VR fits in an integer type specified
9345 by PRECISION and UNSIGNED_P. */
9347 static bool
9348 range_fits_type_p (value_range_t *vr, unsigned dest_precision, signop dest_sgn)
9350 tree src_type;
9351 unsigned src_precision;
9352 widest_int tem;
9353 signop src_sgn;
9355 /* We can only handle integral and pointer types. */
9356 src_type = TREE_TYPE (vr->min);
9357 if (!INTEGRAL_TYPE_P (src_type)
9358 && !POINTER_TYPE_P (src_type))
9359 return false;
9361 /* An extension is fine unless VR is SIGNED and dest_sgn is UNSIGNED,
9362 and so is an identity transform. */
9363 src_precision = TYPE_PRECISION (TREE_TYPE (vr->min));
9364 src_sgn = TYPE_SIGN (src_type);
9365 if ((src_precision < dest_precision
9366 && !(dest_sgn == UNSIGNED && src_sgn == SIGNED))
9367 || (src_precision == dest_precision && src_sgn == dest_sgn))
9368 return true;
9370 /* Now we can only handle ranges with constant bounds. */
9371 if (vr->type != VR_RANGE
9372 || TREE_CODE (vr->min) != INTEGER_CST
9373 || TREE_CODE (vr->max) != INTEGER_CST)
9374 return false;
9376 /* For sign changes, the MSB of the wide_int has to be clear.
9377 An unsigned value with its MSB set cannot be represented by
9378 a signed wide_int, while a negative value cannot be represented
9379 by an unsigned wide_int. */
9380 if (src_sgn != dest_sgn
9381 && (wi::lts_p (vr->min, 0) || wi::lts_p (vr->max, 0)))
9382 return false;
9384 /* Then we can perform the conversion on both ends and compare
9385 the result for equality. */
9386 tem = wi::ext (wi::to_widest (vr->min), dest_precision, dest_sgn);
9387 if (tem != wi::to_widest (vr->min))
9388 return false;
9389 tem = wi::ext (wi::to_widest (vr->max), dest_precision, dest_sgn);
9390 if (tem != wi::to_widest (vr->max))
9391 return false;
9393 return true;
9396 /* Simplify a conditional using a relational operator to an equality
9397 test if the range information indicates only one value can satisfy
9398 the original conditional. */
9400 static bool
9401 simplify_cond_using_ranges (gcond *stmt)
9403 tree op0 = gimple_cond_lhs (stmt);
9404 tree op1 = gimple_cond_rhs (stmt);
9405 enum tree_code cond_code = gimple_cond_code (stmt);
9407 if (cond_code != NE_EXPR
9408 && cond_code != EQ_EXPR
9409 && TREE_CODE (op0) == SSA_NAME
9410 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
9411 && is_gimple_min_invariant (op1))
9413 value_range_t *vr = get_value_range (op0);
9415 /* If we have range information for OP0, then we might be
9416 able to simplify this conditional. */
9417 if (vr->type == VR_RANGE)
9419 enum warn_strict_overflow_code wc = WARN_STRICT_OVERFLOW_COMPARISON;
9420 bool sop = false;
9421 tree new_tree = test_for_singularity (cond_code, op0, op1, vr, &sop);
9423 if (new_tree
9424 && (!sop || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0))))
9426 if (dump_file)
9428 fprintf (dump_file, "Simplified relational ");
9429 print_gimple_stmt (dump_file, stmt, 0, 0);
9430 fprintf (dump_file, " into ");
9433 gimple_cond_set_code (stmt, EQ_EXPR);
9434 gimple_cond_set_lhs (stmt, op0);
9435 gimple_cond_set_rhs (stmt, new_tree);
9437 update_stmt (stmt);
9439 if (dump_file)
9441 print_gimple_stmt (dump_file, stmt, 0, 0);
9442 fprintf (dump_file, "\n");
9445 if (sop && issue_strict_overflow_warning (wc))
9447 location_t location = input_location;
9448 if (gimple_has_location (stmt))
9449 location = gimple_location (stmt);
9451 warning_at (location, OPT_Wstrict_overflow,
9452 "assuming signed overflow does not occur when "
9453 "simplifying conditional");
9456 return true;
9459 /* Try again after inverting the condition. We only deal
9460 with integral types here, so no need to worry about
9461 issues with inverting FP comparisons. */
9462 sop = false;
9463 new_tree = test_for_singularity
9464 (invert_tree_comparison (cond_code, false),
9465 op0, op1, vr, &sop);
9467 if (new_tree
9468 && (!sop || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0))))
9470 if (dump_file)
9472 fprintf (dump_file, "Simplified relational ");
9473 print_gimple_stmt (dump_file, stmt, 0, 0);
9474 fprintf (dump_file, " into ");
9477 gimple_cond_set_code (stmt, NE_EXPR);
9478 gimple_cond_set_lhs (stmt, op0);
9479 gimple_cond_set_rhs (stmt, new_tree);
9481 update_stmt (stmt);
9483 if (dump_file)
9485 print_gimple_stmt (dump_file, stmt, 0, 0);
9486 fprintf (dump_file, "\n");
9489 if (sop && issue_strict_overflow_warning (wc))
9491 location_t location = input_location;
9492 if (gimple_has_location (stmt))
9493 location = gimple_location (stmt);
9495 warning_at (location, OPT_Wstrict_overflow,
9496 "assuming signed overflow does not occur when "
9497 "simplifying conditional");
9500 return true;
9505 /* If we have a comparison of an SSA_NAME (OP0) against a constant,
9506 see if OP0 was set by a type conversion where the source of
9507 the conversion is another SSA_NAME with a range that fits
9508 into the range of OP0's type.
9510 If so, the conversion is redundant as the earlier SSA_NAME can be
9511 used for the comparison directly if we just massage the constant in the
9512 comparison. */
9513 if (TREE_CODE (op0) == SSA_NAME
9514 && TREE_CODE (op1) == INTEGER_CST)
9516 gimple def_stmt = SSA_NAME_DEF_STMT (op0);
9517 tree innerop;
9519 if (!is_gimple_assign (def_stmt)
9520 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt)))
9521 return false;
9523 innerop = gimple_assign_rhs1 (def_stmt);
9525 if (TREE_CODE (innerop) == SSA_NAME
9526 && !POINTER_TYPE_P (TREE_TYPE (innerop)))
9528 value_range_t *vr = get_value_range (innerop);
9530 if (range_int_cst_p (vr)
9531 && range_fits_type_p (vr,
9532 TYPE_PRECISION (TREE_TYPE (op0)),
9533 TYPE_SIGN (TREE_TYPE (op0)))
9534 && int_fits_type_p (op1, TREE_TYPE (innerop))
9535 /* The range must not have overflowed, or if it did overflow
9536 we must not be wrapping/trapping overflow and optimizing
9537 with strict overflow semantics. */
9538 && ((!is_negative_overflow_infinity (vr->min)
9539 && !is_positive_overflow_infinity (vr->max))
9540 || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (innerop))))
9542 /* If the range overflowed and the user has asked for warnings
9543 when strict overflow semantics were used to optimize code,
9544 issue an appropriate warning. */
9545 if (cond_code != EQ_EXPR && cond_code != NE_EXPR
9546 && (is_negative_overflow_infinity (vr->min)
9547 || is_positive_overflow_infinity (vr->max))
9548 && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_CONDITIONAL))
9550 location_t location;
9552 if (!gimple_has_location (stmt))
9553 location = input_location;
9554 else
9555 location = gimple_location (stmt);
9556 warning_at (location, OPT_Wstrict_overflow,
9557 "assuming signed overflow does not occur when "
9558 "simplifying conditional");
9561 tree newconst = fold_convert (TREE_TYPE (innerop), op1);
9562 gimple_cond_set_lhs (stmt, innerop);
9563 gimple_cond_set_rhs (stmt, newconst);
9564 return true;
9569 return false;
9572 /* Simplify a switch statement using the value range of the switch
9573 argument. */
9575 static bool
9576 simplify_switch_using_ranges (gswitch *stmt)
9578 tree op = gimple_switch_index (stmt);
9579 value_range_t *vr;
9580 bool take_default;
9581 edge e;
9582 edge_iterator ei;
9583 size_t i = 0, j = 0, n, n2;
9584 tree vec2;
9585 switch_update su;
9586 size_t k = 1, l = 0;
9588 if (TREE_CODE (op) == SSA_NAME)
9590 vr = get_value_range (op);
9592 /* We can only handle integer ranges. */
9593 if ((vr->type != VR_RANGE
9594 && vr->type != VR_ANTI_RANGE)
9595 || symbolic_range_p (vr))
9596 return false;
9598 /* Find case label for min/max of the value range. */
9599 take_default = !find_case_label_ranges (stmt, vr, &i, &j, &k, &l);
9601 else if (TREE_CODE (op) == INTEGER_CST)
9603 take_default = !find_case_label_index (stmt, 1, op, &i);
9604 if (take_default)
9606 i = 1;
9607 j = 0;
9609 else
9611 j = i;
9614 else
9615 return false;
9617 n = gimple_switch_num_labels (stmt);
9619 /* Bail out if this is just all edges taken. */
9620 if (i == 1
9621 && j == n - 1
9622 && take_default)
9623 return false;
9625 /* Build a new vector of taken case labels. */
9626 vec2 = make_tree_vec (j - i + 1 + l - k + 1 + (int)take_default);
9627 n2 = 0;
9629 /* Add the default edge, if necessary. */
9630 if (take_default)
9631 TREE_VEC_ELT (vec2, n2++) = gimple_switch_default_label (stmt);
9633 for (; i <= j; ++i, ++n2)
9634 TREE_VEC_ELT (vec2, n2) = gimple_switch_label (stmt, i);
9636 for (; k <= l; ++k, ++n2)
9637 TREE_VEC_ELT (vec2, n2) = gimple_switch_label (stmt, k);
9639 /* Mark needed edges. */
9640 for (i = 0; i < n2; ++i)
9642 e = find_edge (gimple_bb (stmt),
9643 label_to_block (CASE_LABEL (TREE_VEC_ELT (vec2, i))));
9644 e->aux = (void *)-1;
9647 /* Queue not needed edges for later removal. */
9648 FOR_EACH_EDGE (e, ei, gimple_bb (stmt)->succs)
9650 if (e->aux == (void *)-1)
9652 e->aux = NULL;
9653 continue;
9656 if (dump_file && (dump_flags & TDF_DETAILS))
9658 fprintf (dump_file, "removing unreachable case label\n");
9660 to_remove_edges.safe_push (e);
9661 e->flags &= ~EDGE_EXECUTABLE;
9664 /* And queue an update for the stmt. */
9665 su.stmt = stmt;
9666 su.vec = vec2;
9667 to_update_switch_stmts.safe_push (su);
9668 return false;
9671 /* Simplify an integral conversion from an SSA name in STMT. */
9673 static bool
9674 simplify_conversion_using_ranges (gimple stmt)
9676 tree innerop, middleop, finaltype;
9677 gimple def_stmt;
9678 value_range_t *innervr;
9679 signop inner_sgn, middle_sgn, final_sgn;
9680 unsigned inner_prec, middle_prec, final_prec;
9681 widest_int innermin, innermed, innermax, middlemin, middlemed, middlemax;
9683 finaltype = TREE_TYPE (gimple_assign_lhs (stmt));
9684 if (!INTEGRAL_TYPE_P (finaltype))
9685 return false;
9686 middleop = gimple_assign_rhs1 (stmt);
9687 def_stmt = SSA_NAME_DEF_STMT (middleop);
9688 if (!is_gimple_assign (def_stmt)
9689 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt)))
9690 return false;
9691 innerop = gimple_assign_rhs1 (def_stmt);
9692 if (TREE_CODE (innerop) != SSA_NAME
9693 || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (innerop))
9694 return false;
9696 /* Get the value-range of the inner operand. */
9697 innervr = get_value_range (innerop);
9698 if (innervr->type != VR_RANGE
9699 || TREE_CODE (innervr->min) != INTEGER_CST
9700 || TREE_CODE (innervr->max) != INTEGER_CST)
9701 return false;
9703 /* Simulate the conversion chain to check if the result is equal if
9704 the middle conversion is removed. */
9705 innermin = wi::to_widest (innervr->min);
9706 innermax = wi::to_widest (innervr->max);
9708 inner_prec = TYPE_PRECISION (TREE_TYPE (innerop));
9709 middle_prec = TYPE_PRECISION (TREE_TYPE (middleop));
9710 final_prec = TYPE_PRECISION (finaltype);
9712 /* If the first conversion is not injective, the second must not
9713 be widening. */
9714 if (wi::gtu_p (innermax - innermin,
9715 wi::mask <widest_int> (middle_prec, false))
9716 && middle_prec < final_prec)
9717 return false;
9718 /* We also want a medium value so that we can track the effect that
9719 narrowing conversions with sign change have. */
9720 inner_sgn = TYPE_SIGN (TREE_TYPE (innerop));
9721 if (inner_sgn == UNSIGNED)
9722 innermed = wi::shifted_mask <widest_int> (1, inner_prec - 1, false);
9723 else
9724 innermed = 0;
9725 if (wi::cmp (innermin, innermed, inner_sgn) >= 0
9726 || wi::cmp (innermed, innermax, inner_sgn) >= 0)
9727 innermed = innermin;
9729 middle_sgn = TYPE_SIGN (TREE_TYPE (middleop));
9730 middlemin = wi::ext (innermin, middle_prec, middle_sgn);
9731 middlemed = wi::ext (innermed, middle_prec, middle_sgn);
9732 middlemax = wi::ext (innermax, middle_prec, middle_sgn);
9734 /* Require that the final conversion applied to both the original
9735 and the intermediate range produces the same result. */
9736 final_sgn = TYPE_SIGN (finaltype);
9737 if (wi::ext (middlemin, final_prec, final_sgn)
9738 != wi::ext (innermin, final_prec, final_sgn)
9739 || wi::ext (middlemed, final_prec, final_sgn)
9740 != wi::ext (innermed, final_prec, final_sgn)
9741 || wi::ext (middlemax, final_prec, final_sgn)
9742 != wi::ext (innermax, final_prec, final_sgn))
9743 return false;
9745 gimple_assign_set_rhs1 (stmt, innerop);
9746 update_stmt (stmt);
9747 return true;
9750 /* Simplify a conversion from integral SSA name to float in STMT. */
9752 static bool
9753 simplify_float_conversion_using_ranges (gimple_stmt_iterator *gsi, gimple stmt)
9755 tree rhs1 = gimple_assign_rhs1 (stmt);
9756 value_range_t *vr = get_value_range (rhs1);
9757 machine_mode fltmode = TYPE_MODE (TREE_TYPE (gimple_assign_lhs (stmt)));
9758 machine_mode mode;
9759 tree tem;
9760 gassign *conv;
9762 /* We can only handle constant ranges. */
9763 if (vr->type != VR_RANGE
9764 || TREE_CODE (vr->min) != INTEGER_CST
9765 || TREE_CODE (vr->max) != INTEGER_CST)
9766 return false;
9768 /* First check if we can use a signed type in place of an unsigned. */
9769 if (TYPE_UNSIGNED (TREE_TYPE (rhs1))
9770 && (can_float_p (fltmode, TYPE_MODE (TREE_TYPE (rhs1)), 0)
9771 != CODE_FOR_nothing)
9772 && range_fits_type_p (vr, TYPE_PRECISION (TREE_TYPE (rhs1)), SIGNED))
9773 mode = TYPE_MODE (TREE_TYPE (rhs1));
9774 /* If we can do the conversion in the current input mode do nothing. */
9775 else if (can_float_p (fltmode, TYPE_MODE (TREE_TYPE (rhs1)),
9776 TYPE_UNSIGNED (TREE_TYPE (rhs1))) != CODE_FOR_nothing)
9777 return false;
9778 /* Otherwise search for a mode we can use, starting from the narrowest
9779 integer mode available. */
9780 else
9782 mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
9785 /* If we cannot do a signed conversion to float from mode
9786 or if the value-range does not fit in the signed type
9787 try with a wider mode. */
9788 if (can_float_p (fltmode, mode, 0) != CODE_FOR_nothing
9789 && range_fits_type_p (vr, GET_MODE_PRECISION (mode), SIGNED))
9790 break;
9792 mode = GET_MODE_WIDER_MODE (mode);
9793 /* But do not widen the input. Instead leave that to the
9794 optabs expansion code. */
9795 if (GET_MODE_PRECISION (mode) > TYPE_PRECISION (TREE_TYPE (rhs1)))
9796 return false;
9798 while (mode != VOIDmode);
9799 if (mode == VOIDmode)
9800 return false;
9803 /* It works, insert a truncation or sign-change before the
9804 float conversion. */
9805 tem = make_ssa_name (build_nonstandard_integer_type
9806 (GET_MODE_PRECISION (mode), 0));
9807 conv = gimple_build_assign (tem, NOP_EXPR, rhs1);
9808 gsi_insert_before (gsi, conv, GSI_SAME_STMT);
9809 gimple_assign_set_rhs1 (stmt, tem);
9810 update_stmt (stmt);
9812 return true;
9815 /* Simplify an internal fn call using ranges if possible. */
9817 static bool
9818 simplify_internal_call_using_ranges (gimple_stmt_iterator *gsi, gimple stmt)
9820 enum tree_code subcode;
9821 bool is_ubsan = false;
9822 bool ovf = false;
9823 switch (gimple_call_internal_fn (stmt))
9825 case IFN_UBSAN_CHECK_ADD:
9826 subcode = PLUS_EXPR;
9827 is_ubsan = true;
9828 break;
9829 case IFN_UBSAN_CHECK_SUB:
9830 subcode = MINUS_EXPR;
9831 is_ubsan = true;
9832 break;
9833 case IFN_UBSAN_CHECK_MUL:
9834 subcode = MULT_EXPR;
9835 is_ubsan = true;
9836 break;
9837 case IFN_ADD_OVERFLOW:
9838 subcode = PLUS_EXPR;
9839 break;
9840 case IFN_SUB_OVERFLOW:
9841 subcode = MINUS_EXPR;
9842 break;
9843 case IFN_MUL_OVERFLOW:
9844 subcode = MULT_EXPR;
9845 break;
9846 default:
9847 return false;
9850 tree op0 = gimple_call_arg (stmt, 0);
9851 tree op1 = gimple_call_arg (stmt, 1);
9852 tree type;
9853 if (is_ubsan)
9854 type = TREE_TYPE (op0);
9855 else if (gimple_call_lhs (stmt) == NULL_TREE)
9856 return false;
9857 else
9858 type = TREE_TYPE (TREE_TYPE (gimple_call_lhs (stmt)));
9859 if (!check_for_binary_op_overflow (subcode, type, op0, op1, &ovf)
9860 || (is_ubsan && ovf))
9861 return false;
9863 gimple g;
9864 location_t loc = gimple_location (stmt);
9865 if (is_ubsan)
9866 g = gimple_build_assign (gimple_call_lhs (stmt), subcode, op0, op1);
9867 else
9869 int prec = TYPE_PRECISION (type);
9870 tree utype = type;
9871 if (ovf
9872 || !useless_type_conversion_p (type, TREE_TYPE (op0))
9873 || !useless_type_conversion_p (type, TREE_TYPE (op1)))
9874 utype = build_nonstandard_integer_type (prec, 1);
9875 if (TREE_CODE (op0) == INTEGER_CST)
9876 op0 = fold_convert (utype, op0);
9877 else if (!useless_type_conversion_p (utype, TREE_TYPE (op0)))
9879 g = gimple_build_assign (make_ssa_name (utype), NOP_EXPR, op0);
9880 gimple_set_location (g, loc);
9881 gsi_insert_before (gsi, g, GSI_SAME_STMT);
9882 op0 = gimple_assign_lhs (g);
9884 if (TREE_CODE (op1) == INTEGER_CST)
9885 op1 = fold_convert (utype, op1);
9886 else if (!useless_type_conversion_p (utype, TREE_TYPE (op1)))
9888 g = gimple_build_assign (make_ssa_name (utype), NOP_EXPR, op1);
9889 gimple_set_location (g, loc);
9890 gsi_insert_before (gsi, g, GSI_SAME_STMT);
9891 op1 = gimple_assign_lhs (g);
9893 g = gimple_build_assign (make_ssa_name (utype), subcode, op0, op1);
9894 gimple_set_location (g, loc);
9895 gsi_insert_before (gsi, g, GSI_SAME_STMT);
9896 if (utype != type)
9898 g = gimple_build_assign (make_ssa_name (type), NOP_EXPR,
9899 gimple_assign_lhs (g));
9900 gimple_set_location (g, loc);
9901 gsi_insert_before (gsi, g, GSI_SAME_STMT);
9903 g = gimple_build_assign (gimple_call_lhs (stmt), COMPLEX_EXPR,
9904 gimple_assign_lhs (g),
9905 build_int_cst (type, ovf));
9907 gimple_set_location (g, loc);
9908 gsi_replace (gsi, g, false);
9909 return true;
9912 /* Simplify STMT using ranges if possible. */
9914 static bool
9915 simplify_stmt_using_ranges (gimple_stmt_iterator *gsi)
9917 gimple stmt = gsi_stmt (*gsi);
9918 if (is_gimple_assign (stmt))
9920 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
9921 tree rhs1 = gimple_assign_rhs1 (stmt);
9923 switch (rhs_code)
9925 case EQ_EXPR:
9926 case NE_EXPR:
9927 /* Transform EQ_EXPR, NE_EXPR into BIT_XOR_EXPR or identity
9928 if the RHS is zero or one, and the LHS are known to be boolean
9929 values. */
9930 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9931 return simplify_truth_ops_using_ranges (gsi, stmt);
9932 break;
9934 /* Transform TRUNC_DIV_EXPR and TRUNC_MOD_EXPR into RSHIFT_EXPR
9935 and BIT_AND_EXPR respectively if the first operand is greater
9936 than zero and the second operand is an exact power of two.
9937 Also optimize TRUNC_MOD_EXPR away if the second operand is
9938 constant and the first operand already has the right value
9939 range. */
9940 case TRUNC_DIV_EXPR:
9941 case TRUNC_MOD_EXPR:
9942 if (TREE_CODE (rhs1) == SSA_NAME
9943 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9944 return simplify_div_or_mod_using_ranges (stmt);
9945 break;
9947 /* Transform ABS (X) into X or -X as appropriate. */
9948 case ABS_EXPR:
9949 if (TREE_CODE (rhs1) == SSA_NAME
9950 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9951 return simplify_abs_using_ranges (stmt);
9952 break;
9954 case BIT_AND_EXPR:
9955 case BIT_IOR_EXPR:
9956 /* Optimize away BIT_AND_EXPR and BIT_IOR_EXPR
9957 if all the bits being cleared are already cleared or
9958 all the bits being set are already set. */
9959 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9960 return simplify_bit_ops_using_ranges (gsi, stmt);
9961 break;
9963 CASE_CONVERT:
9964 if (TREE_CODE (rhs1) == SSA_NAME
9965 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9966 return simplify_conversion_using_ranges (stmt);
9967 break;
9969 case FLOAT_EXPR:
9970 if (TREE_CODE (rhs1) == SSA_NAME
9971 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9972 return simplify_float_conversion_using_ranges (gsi, stmt);
9973 break;
9975 case MIN_EXPR:
9976 case MAX_EXPR:
9977 return simplify_min_or_max_using_ranges (stmt);
9978 break;
9980 default:
9981 break;
9984 else if (gimple_code (stmt) == GIMPLE_COND)
9985 return simplify_cond_using_ranges (as_a <gcond *> (stmt));
9986 else if (gimple_code (stmt) == GIMPLE_SWITCH)
9987 return simplify_switch_using_ranges (as_a <gswitch *> (stmt));
9988 else if (is_gimple_call (stmt)
9989 && gimple_call_internal_p (stmt))
9990 return simplify_internal_call_using_ranges (gsi, stmt);
9992 return false;
9995 /* If the statement pointed by SI has a predicate whose value can be
9996 computed using the value range information computed by VRP, compute
9997 its value and return true. Otherwise, return false. */
9999 static bool
10000 fold_predicate_in (gimple_stmt_iterator *si)
10002 bool assignment_p = false;
10003 tree val;
10004 gimple stmt = gsi_stmt (*si);
10006 if (is_gimple_assign (stmt)
10007 && TREE_CODE_CLASS (gimple_assign_rhs_code (stmt)) == tcc_comparison)
10009 assignment_p = true;
10010 val = vrp_evaluate_conditional (gimple_assign_rhs_code (stmt),
10011 gimple_assign_rhs1 (stmt),
10012 gimple_assign_rhs2 (stmt),
10013 stmt);
10015 else if (gcond *cond_stmt = dyn_cast <gcond *> (stmt))
10016 val = vrp_evaluate_conditional (gimple_cond_code (cond_stmt),
10017 gimple_cond_lhs (cond_stmt),
10018 gimple_cond_rhs (cond_stmt),
10019 stmt);
10020 else
10021 return false;
10023 if (val)
10025 if (assignment_p)
10026 val = fold_convert (gimple_expr_type (stmt), val);
10028 if (dump_file)
10030 fprintf (dump_file, "Folding predicate ");
10031 print_gimple_expr (dump_file, stmt, 0, 0);
10032 fprintf (dump_file, " to ");
10033 print_generic_expr (dump_file, val, 0);
10034 fprintf (dump_file, "\n");
10037 if (is_gimple_assign (stmt))
10038 gimple_assign_set_rhs_from_tree (si, val);
10039 else
10041 gcc_assert (gimple_code (stmt) == GIMPLE_COND);
10042 gcond *cond_stmt = as_a <gcond *> (stmt);
10043 if (integer_zerop (val))
10044 gimple_cond_make_false (cond_stmt);
10045 else if (integer_onep (val))
10046 gimple_cond_make_true (cond_stmt);
10047 else
10048 gcc_unreachable ();
10051 return true;
10054 return false;
10057 /* Callback for substitute_and_fold folding the stmt at *SI. */
10059 static bool
10060 vrp_fold_stmt (gimple_stmt_iterator *si)
10062 if (fold_predicate_in (si))
10063 return true;
10065 return simplify_stmt_using_ranges (si);
10068 /* Unwindable const/copy equivalences. */
10069 const_and_copies *equiv_stack;
10071 /* A trivial wrapper so that we can present the generic jump threading
10072 code with a simple API for simplifying statements. STMT is the
10073 statement we want to simplify, WITHIN_STMT provides the location
10074 for any overflow warnings. */
10076 static tree
10077 simplify_stmt_for_jump_threading (gimple stmt, gimple within_stmt)
10079 if (gcond *cond_stmt = dyn_cast <gcond *> (stmt))
10080 return vrp_evaluate_conditional (gimple_cond_code (cond_stmt),
10081 gimple_cond_lhs (cond_stmt),
10082 gimple_cond_rhs (cond_stmt),
10083 within_stmt);
10085 if (gassign *assign_stmt = dyn_cast <gassign *> (stmt))
10087 value_range_t new_vr = VR_INITIALIZER;
10088 tree lhs = gimple_assign_lhs (assign_stmt);
10090 if (TREE_CODE (lhs) == SSA_NAME
10091 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs))
10092 || POINTER_TYPE_P (TREE_TYPE (lhs))))
10094 extract_range_from_assignment (&new_vr, assign_stmt);
10095 if (range_int_cst_singleton_p (&new_vr))
10096 return new_vr.min;
10100 return NULL_TREE;
10103 /* Blocks which have more than one predecessor and more than
10104 one successor present jump threading opportunities, i.e.,
10105 when the block is reached from a specific predecessor, we
10106 may be able to determine which of the outgoing edges will
10107 be traversed. When this optimization applies, we are able
10108 to avoid conditionals at runtime and we may expose secondary
10109 optimization opportunities.
10111 This routine is effectively a driver for the generic jump
10112 threading code. It basically just presents the generic code
10113 with edges that may be suitable for jump threading.
10115 Unlike DOM, we do not iterate VRP if jump threading was successful.
10116 While iterating may expose new opportunities for VRP, it is expected
10117 those opportunities would be very limited and the compile time cost
10118 to expose those opportunities would be significant.
10120 As jump threading opportunities are discovered, they are registered
10121 for later realization. */
10123 static void
10124 identify_jump_threads (void)
10126 basic_block bb;
10127 gcond *dummy;
10128 int i;
10129 edge e;
10131 /* Ugh. When substituting values earlier in this pass we can
10132 wipe the dominance information. So rebuild the dominator
10133 information as we need it within the jump threading code. */
10134 calculate_dominance_info (CDI_DOMINATORS);
10136 /* We do not allow VRP information to be used for jump threading
10137 across a back edge in the CFG. Otherwise it becomes too
10138 difficult to avoid eliminating loop exit tests. Of course
10139 EDGE_DFS_BACK is not accurate at this time so we have to
10140 recompute it. */
10141 mark_dfs_back_edges ();
10143 /* Do not thread across edges we are about to remove. Just marking
10144 them as EDGE_DFS_BACK will do. */
10145 FOR_EACH_VEC_ELT (to_remove_edges, i, e)
10146 e->flags |= EDGE_DFS_BACK;
10148 /* Allocate our unwinder stack to unwind any temporary equivalences
10149 that might be recorded. */
10150 equiv_stack = new const_and_copies ();
10152 /* To avoid lots of silly node creation, we create a single
10153 conditional and just modify it in-place when attempting to
10154 thread jumps. */
10155 dummy = gimple_build_cond (EQ_EXPR,
10156 integer_zero_node, integer_zero_node,
10157 NULL, NULL);
10159 /* Walk through all the blocks finding those which present a
10160 potential jump threading opportunity. We could set this up
10161 as a dominator walker and record data during the walk, but
10162 I doubt it's worth the effort for the classes of jump
10163 threading opportunities we are trying to identify at this
10164 point in compilation. */
10165 FOR_EACH_BB_FN (bb, cfun)
10167 gimple last;
10169 /* If the generic jump threading code does not find this block
10170 interesting, then there is nothing to do. */
10171 if (! potentially_threadable_block (bb))
10172 continue;
10174 last = last_stmt (bb);
10176 /* We're basically looking for a switch or any kind of conditional with
10177 integral or pointer type arguments. Note the type of the second
10178 argument will be the same as the first argument, so no need to
10179 check it explicitly.
10181 We also handle the case where there are no statements in the
10182 block. This come up with forwarder blocks that are not
10183 optimized away because they lead to a loop header. But we do
10184 want to thread through them as we can sometimes thread to the
10185 loop exit which is obviously profitable. */
10186 if (!last
10187 || gimple_code (last) == GIMPLE_SWITCH
10188 || (gimple_code (last) == GIMPLE_COND
10189 && TREE_CODE (gimple_cond_lhs (last)) == SSA_NAME
10190 && (INTEGRAL_TYPE_P (TREE_TYPE (gimple_cond_lhs (last)))
10191 || POINTER_TYPE_P (TREE_TYPE (gimple_cond_lhs (last))))
10192 && (TREE_CODE (gimple_cond_rhs (last)) == SSA_NAME
10193 || is_gimple_min_invariant (gimple_cond_rhs (last)))))
10195 edge_iterator ei;
10197 /* We've got a block with multiple predecessors and multiple
10198 successors which also ends in a suitable conditional or
10199 switch statement. For each predecessor, see if we can thread
10200 it to a specific successor. */
10201 FOR_EACH_EDGE (e, ei, bb->preds)
10203 /* Do not thread across back edges or abnormal edges
10204 in the CFG. */
10205 if (e->flags & (EDGE_DFS_BACK | EDGE_COMPLEX))
10206 continue;
10208 thread_across_edge (dummy, e, true, equiv_stack,
10209 simplify_stmt_for_jump_threading);
10214 /* We do not actually update the CFG or SSA graphs at this point as
10215 ASSERT_EXPRs are still in the IL and cfg cleanup code does not yet
10216 handle ASSERT_EXPRs gracefully. */
10219 /* We identified all the jump threading opportunities earlier, but could
10220 not transform the CFG at that time. This routine transforms the
10221 CFG and arranges for the dominator tree to be rebuilt if necessary.
10223 Note the SSA graph update will occur during the normal TODO
10224 processing by the pass manager. */
10225 static void
10226 finalize_jump_threads (void)
10228 thread_through_all_blocks (false);
10229 delete equiv_stack;
10233 /* Traverse all the blocks folding conditionals with known ranges. */
10235 static void
10236 vrp_finalize (void)
10238 size_t i;
10240 values_propagated = true;
10242 if (dump_file)
10244 fprintf (dump_file, "\nValue ranges after VRP:\n\n");
10245 dump_all_value_ranges (dump_file);
10246 fprintf (dump_file, "\n");
10249 substitute_and_fold (op_with_constant_singleton_value_range,
10250 vrp_fold_stmt, false);
10252 if (warn_array_bounds && first_pass_instance)
10253 check_all_array_refs ();
10255 /* We must identify jump threading opportunities before we release
10256 the datastructures built by VRP. */
10257 identify_jump_threads ();
10259 /* Set value range to non pointer SSA_NAMEs. */
10260 for (i = 0; i < num_vr_values; i++)
10261 if (vr_value[i])
10263 tree name = ssa_name (i);
10265 if (!name
10266 || POINTER_TYPE_P (TREE_TYPE (name))
10267 || (vr_value[i]->type == VR_VARYING)
10268 || (vr_value[i]->type == VR_UNDEFINED))
10269 continue;
10271 if ((TREE_CODE (vr_value[i]->min) == INTEGER_CST)
10272 && (TREE_CODE (vr_value[i]->max) == INTEGER_CST)
10273 && (vr_value[i]->type == VR_RANGE
10274 || vr_value[i]->type == VR_ANTI_RANGE))
10275 set_range_info (name, vr_value[i]->type, vr_value[i]->min,
10276 vr_value[i]->max);
10279 /* Free allocated memory. */
10280 for (i = 0; i < num_vr_values; i++)
10281 if (vr_value[i])
10283 BITMAP_FREE (vr_value[i]->equiv);
10284 free (vr_value[i]);
10287 free (vr_value);
10288 free (vr_phi_edge_counts);
10290 /* So that we can distinguish between VRP data being available
10291 and not available. */
10292 vr_value = NULL;
10293 vr_phi_edge_counts = NULL;
10297 /* Main entry point to VRP (Value Range Propagation). This pass is
10298 loosely based on J. R. C. Patterson, ``Accurate Static Branch
10299 Prediction by Value Range Propagation,'' in SIGPLAN Conference on
10300 Programming Language Design and Implementation, pp. 67-78, 1995.
10301 Also available at http://citeseer.ist.psu.edu/patterson95accurate.html
10303 This is essentially an SSA-CCP pass modified to deal with ranges
10304 instead of constants.
10306 While propagating ranges, we may find that two or more SSA name
10307 have equivalent, though distinct ranges. For instance,
10309 1 x_9 = p_3->a;
10310 2 p_4 = ASSERT_EXPR <p_3, p_3 != 0>
10311 3 if (p_4 == q_2)
10312 4 p_5 = ASSERT_EXPR <p_4, p_4 == q_2>;
10313 5 endif
10314 6 if (q_2)
10316 In the code above, pointer p_5 has range [q_2, q_2], but from the
10317 code we can also determine that p_5 cannot be NULL and, if q_2 had
10318 a non-varying range, p_5's range should also be compatible with it.
10320 These equivalences are created by two expressions: ASSERT_EXPR and
10321 copy operations. Since p_5 is an assertion on p_4, and p_4 was the
10322 result of another assertion, then we can use the fact that p_5 and
10323 p_4 are equivalent when evaluating p_5's range.
10325 Together with value ranges, we also propagate these equivalences
10326 between names so that we can take advantage of information from
10327 multiple ranges when doing final replacement. Note that this
10328 equivalency relation is transitive but not symmetric.
10330 In the example above, p_5 is equivalent to p_4, q_2 and p_3, but we
10331 cannot assert that q_2 is equivalent to p_5 because q_2 may be used
10332 in contexts where that assertion does not hold (e.g., in line 6).
10334 TODO, the main difference between this pass and Patterson's is that
10335 we do not propagate edge probabilities. We only compute whether
10336 edges can be taken or not. That is, instead of having a spectrum
10337 of jump probabilities between 0 and 1, we only deal with 0, 1 and
10338 DON'T KNOW. In the future, it may be worthwhile to propagate
10339 probabilities to aid branch prediction. */
10341 static unsigned int
10342 execute_vrp (void)
10344 int i;
10345 edge e;
10346 switch_update *su;
10348 loop_optimizer_init (LOOPS_NORMAL | LOOPS_HAVE_RECORDED_EXITS);
10349 rewrite_into_loop_closed_ssa (NULL, TODO_update_ssa);
10350 scev_initialize ();
10352 /* ??? This ends up using stale EDGE_DFS_BACK for liveness computation.
10353 Inserting assertions may split edges which will invalidate
10354 EDGE_DFS_BACK. */
10355 insert_range_assertions ();
10357 to_remove_edges.create (10);
10358 to_update_switch_stmts.create (5);
10359 threadedge_initialize_values ();
10361 /* For visiting PHI nodes we need EDGE_DFS_BACK computed. */
10362 mark_dfs_back_edges ();
10364 vrp_initialize ();
10365 ssa_propagate (vrp_visit_stmt, vrp_visit_phi_node);
10366 vrp_finalize ();
10368 free_numbers_of_iterations_estimates ();
10370 /* ASSERT_EXPRs must be removed before finalizing jump threads
10371 as finalizing jump threads calls the CFG cleanup code which
10372 does not properly handle ASSERT_EXPRs. */
10373 remove_range_assertions ();
10375 /* If we exposed any new variables, go ahead and put them into
10376 SSA form now, before we handle jump threading. This simplifies
10377 interactions between rewriting of _DECL nodes into SSA form
10378 and rewriting SSA_NAME nodes into SSA form after block
10379 duplication and CFG manipulation. */
10380 update_ssa (TODO_update_ssa);
10382 finalize_jump_threads ();
10384 /* Remove dead edges from SWITCH_EXPR optimization. This leaves the
10385 CFG in a broken state and requires a cfg_cleanup run. */
10386 FOR_EACH_VEC_ELT (to_remove_edges, i, e)
10387 remove_edge (e);
10388 /* Update SWITCH_EXPR case label vector. */
10389 FOR_EACH_VEC_ELT (to_update_switch_stmts, i, su)
10391 size_t j;
10392 size_t n = TREE_VEC_LENGTH (su->vec);
10393 tree label;
10394 gimple_switch_set_num_labels (su->stmt, n);
10395 for (j = 0; j < n; j++)
10396 gimple_switch_set_label (su->stmt, j, TREE_VEC_ELT (su->vec, j));
10397 /* As we may have replaced the default label with a regular one
10398 make sure to make it a real default label again. This ensures
10399 optimal expansion. */
10400 label = gimple_switch_label (su->stmt, 0);
10401 CASE_LOW (label) = NULL_TREE;
10402 CASE_HIGH (label) = NULL_TREE;
10405 if (to_remove_edges.length () > 0)
10407 free_dominance_info (CDI_DOMINATORS);
10408 loops_state_set (LOOPS_NEED_FIXUP);
10411 to_remove_edges.release ();
10412 to_update_switch_stmts.release ();
10413 threadedge_finalize_values ();
10415 scev_finalize ();
10416 loop_optimizer_finalize ();
10417 return 0;
10420 namespace {
10422 const pass_data pass_data_vrp =
10424 GIMPLE_PASS, /* type */
10425 "vrp", /* name */
10426 OPTGROUP_NONE, /* optinfo_flags */
10427 TV_TREE_VRP, /* tv_id */
10428 PROP_ssa, /* properties_required */
10429 0, /* properties_provided */
10430 0, /* properties_destroyed */
10431 0, /* todo_flags_start */
10432 ( TODO_cleanup_cfg | TODO_update_ssa ), /* todo_flags_finish */
10435 class pass_vrp : public gimple_opt_pass
10437 public:
10438 pass_vrp (gcc::context *ctxt)
10439 : gimple_opt_pass (pass_data_vrp, ctxt)
10442 /* opt_pass methods: */
10443 opt_pass * clone () { return new pass_vrp (m_ctxt); }
10444 virtual bool gate (function *) { return flag_tree_vrp != 0; }
10445 virtual unsigned int execute (function *) { return execute_vrp (); }
10447 }; // class pass_vrp
10449 } // anon namespace
10451 gimple_opt_pass *
10452 make_pass_vrp (gcc::context *ctxt)
10454 return new pass_vrp (ctxt);