* tree-vect-loop.c (vectorizable_live_operation): Support handling
[official-gcc.git] / gcc / tree-vrp.c
blob941c0fd34d816178be5f8ab3525d78e19c2fd61c
1 /* Support routines for Value Range Propagation (VRP).
2 Copyright (C) 2005-2016 Free Software Foundation, Inc.
3 Contributed by Diego Novillo <dnovillo@redhat.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "backend.h"
25 #include "insn-codes.h"
26 #include "rtl.h"
27 #include "tree.h"
28 #include "gimple.h"
29 #include "cfghooks.h"
30 #include "tree-pass.h"
31 #include "ssa.h"
32 #include "optabs-tree.h"
33 #include "gimple-pretty-print.h"
34 #include "diagnostic-core.h"
35 #include "flags.h"
36 #include "fold-const.h"
37 #include "stor-layout.h"
38 #include "calls.h"
39 #include "cfganal.h"
40 #include "gimple-fold.h"
41 #include "tree-eh.h"
42 #include "gimple-iterator.h"
43 #include "gimple-walk.h"
44 #include "tree-cfg.h"
45 #include "tree-ssa-loop-manip.h"
46 #include "tree-ssa-loop-niter.h"
47 #include "tree-ssa-loop.h"
48 #include "tree-into-ssa.h"
49 #include "tree-ssa.h"
50 #include "intl.h"
51 #include "cfgloop.h"
52 #include "tree-scalar-evolution.h"
53 #include "tree-ssa-propagate.h"
54 #include "tree-chrec.h"
55 #include "tree-ssa-threadupdate.h"
56 #include "tree-ssa-scopedtables.h"
57 #include "tree-ssa-threadedge.h"
58 #include "omp-low.h"
59 #include "target.h"
60 #include "case-cfn-macros.h"
61 #include "params.h"
62 #include "alloc-pool.h"
63 #include "domwalk.h"
64 #include "tree-cfgcleanup.h"
66 #define VR_INITIALIZER { VR_UNDEFINED, NULL_TREE, NULL_TREE, NULL }
68 /* Allocation pools for tree-vrp allocations. */
69 static object_allocator<value_range> vrp_value_range_pool ("Tree VRP value ranges");
70 static bitmap_obstack vrp_equiv_obstack;
72 /* Set of SSA names found live during the RPO traversal of the function
73 for still active basic-blocks. */
74 static sbitmap *live;
76 /* Return true if the SSA name NAME is live on the edge E. */
78 static bool
79 live_on_edge (edge e, tree name)
81 return (live[e->dest->index]
82 && bitmap_bit_p (live[e->dest->index], SSA_NAME_VERSION (name)));
85 /* Local functions. */
86 static int compare_values (tree val1, tree val2);
87 static int compare_values_warnv (tree val1, tree val2, bool *);
88 static tree vrp_evaluate_conditional_warnv_with_ops (enum tree_code,
89 tree, tree, bool, bool *,
90 bool *);
92 /* Location information for ASSERT_EXPRs. Each instance of this
93 structure describes an ASSERT_EXPR for an SSA name. Since a single
94 SSA name may have more than one assertion associated with it, these
95 locations are kept in a linked list attached to the corresponding
96 SSA name. */
97 struct assert_locus
99 /* Basic block where the assertion would be inserted. */
100 basic_block bb;
102 /* Some assertions need to be inserted on an edge (e.g., assertions
103 generated by COND_EXPRs). In those cases, BB will be NULL. */
104 edge e;
106 /* Pointer to the statement that generated this assertion. */
107 gimple_stmt_iterator si;
109 /* Predicate code for the ASSERT_EXPR. Must be COMPARISON_CLASS_P. */
110 enum tree_code comp_code;
112 /* Value being compared against. */
113 tree val;
115 /* Expression to compare. */
116 tree expr;
118 /* Next node in the linked list. */
119 assert_locus *next;
122 /* If bit I is present, it means that SSA name N_i has a list of
123 assertions that should be inserted in the IL. */
124 static bitmap need_assert_for;
126 /* Array of locations lists where to insert assertions. ASSERTS_FOR[I]
127 holds a list of ASSERT_LOCUS_T nodes that describe where
128 ASSERT_EXPRs for SSA name N_I should be inserted. */
129 static assert_locus **asserts_for;
131 /* Value range array. After propagation, VR_VALUE[I] holds the range
132 of values that SSA name N_I may take. */
133 static unsigned num_vr_values;
134 static value_range **vr_value;
135 static bool values_propagated;
137 /* For a PHI node which sets SSA name N_I, VR_COUNTS[I] holds the
138 number of executable edges we saw the last time we visited the
139 node. */
140 static int *vr_phi_edge_counts;
142 struct switch_update {
143 gswitch *stmt;
144 tree vec;
147 static vec<edge> to_remove_edges;
148 static vec<switch_update> to_update_switch_stmts;
151 /* Return the maximum value for TYPE. */
153 static inline tree
154 vrp_val_max (const_tree type)
156 if (!INTEGRAL_TYPE_P (type))
157 return NULL_TREE;
159 return TYPE_MAX_VALUE (type);
162 /* Return the minimum value for TYPE. */
164 static inline tree
165 vrp_val_min (const_tree type)
167 if (!INTEGRAL_TYPE_P (type))
168 return NULL_TREE;
170 return TYPE_MIN_VALUE (type);
173 /* Return whether VAL is equal to the maximum value of its type. This
174 will be true for a positive overflow infinity. We can't do a
175 simple equality comparison with TYPE_MAX_VALUE because C typedefs
176 and Ada subtypes can produce types whose TYPE_MAX_VALUE is not ==
177 to the integer constant with the same value in the type. */
179 static inline bool
180 vrp_val_is_max (const_tree val)
182 tree type_max = vrp_val_max (TREE_TYPE (val));
183 return (val == type_max
184 || (type_max != NULL_TREE
185 && operand_equal_p (val, type_max, 0)));
188 /* Return whether VAL is equal to the minimum value of its type. This
189 will be true for a negative overflow infinity. */
191 static inline bool
192 vrp_val_is_min (const_tree val)
194 tree type_min = vrp_val_min (TREE_TYPE (val));
195 return (val == type_min
196 || (type_min != NULL_TREE
197 && operand_equal_p (val, type_min, 0)));
201 /* Return whether TYPE should use an overflow infinity distinct from
202 TYPE_{MIN,MAX}_VALUE. We use an overflow infinity value to
203 represent a signed overflow during VRP computations. An infinity
204 is distinct from a half-range, which will go from some number to
205 TYPE_{MIN,MAX}_VALUE. */
207 static inline bool
208 needs_overflow_infinity (const_tree type)
210 return INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_WRAPS (type);
213 /* Return whether TYPE can support our overflow infinity
214 representation: we use the TREE_OVERFLOW flag, which only exists
215 for constants. If TYPE doesn't support this, we don't optimize
216 cases which would require signed overflow--we drop them to
217 VARYING. */
219 static inline bool
220 supports_overflow_infinity (const_tree type)
222 tree min = vrp_val_min (type), max = vrp_val_max (type);
223 gcc_checking_assert (needs_overflow_infinity (type));
224 return (min != NULL_TREE
225 && CONSTANT_CLASS_P (min)
226 && max != NULL_TREE
227 && CONSTANT_CLASS_P (max));
230 /* VAL is the maximum or minimum value of a type. Return a
231 corresponding overflow infinity. */
233 static inline tree
234 make_overflow_infinity (tree val)
236 gcc_checking_assert (val != NULL_TREE && CONSTANT_CLASS_P (val));
237 val = copy_node (val);
238 TREE_OVERFLOW (val) = 1;
239 return val;
242 /* Return a negative overflow infinity for TYPE. */
244 static inline tree
245 negative_overflow_infinity (tree type)
247 gcc_checking_assert (supports_overflow_infinity (type));
248 return make_overflow_infinity (vrp_val_min (type));
251 /* Return a positive overflow infinity for TYPE. */
253 static inline tree
254 positive_overflow_infinity (tree type)
256 gcc_checking_assert (supports_overflow_infinity (type));
257 return make_overflow_infinity (vrp_val_max (type));
260 /* Return whether VAL is a negative overflow infinity. */
262 static inline bool
263 is_negative_overflow_infinity (const_tree val)
265 return (TREE_OVERFLOW_P (val)
266 && needs_overflow_infinity (TREE_TYPE (val))
267 && vrp_val_is_min (val));
270 /* Return whether VAL is a positive overflow infinity. */
272 static inline bool
273 is_positive_overflow_infinity (const_tree val)
275 return (TREE_OVERFLOW_P (val)
276 && needs_overflow_infinity (TREE_TYPE (val))
277 && vrp_val_is_max (val));
280 /* Return whether VAL is a positive or negative overflow infinity. */
282 static inline bool
283 is_overflow_infinity (const_tree val)
285 return (TREE_OVERFLOW_P (val)
286 && needs_overflow_infinity (TREE_TYPE (val))
287 && (vrp_val_is_min (val) || vrp_val_is_max (val)));
290 /* Return whether STMT has a constant rhs that is_overflow_infinity. */
292 static inline bool
293 stmt_overflow_infinity (gimple *stmt)
295 if (is_gimple_assign (stmt)
296 && get_gimple_rhs_class (gimple_assign_rhs_code (stmt)) ==
297 GIMPLE_SINGLE_RHS)
298 return is_overflow_infinity (gimple_assign_rhs1 (stmt));
299 return false;
302 /* If VAL is now an overflow infinity, return VAL. Otherwise, return
303 the same value with TREE_OVERFLOW clear. This can be used to avoid
304 confusing a regular value with an overflow value. */
306 static inline tree
307 avoid_overflow_infinity (tree val)
309 if (!is_overflow_infinity (val))
310 return val;
312 if (vrp_val_is_max (val))
313 return vrp_val_max (TREE_TYPE (val));
314 else
316 gcc_checking_assert (vrp_val_is_min (val));
317 return vrp_val_min (TREE_TYPE (val));
322 /* Set value range VR to VR_UNDEFINED. */
324 static inline void
325 set_value_range_to_undefined (value_range *vr)
327 vr->type = VR_UNDEFINED;
328 vr->min = vr->max = NULL_TREE;
329 if (vr->equiv)
330 bitmap_clear (vr->equiv);
334 /* Set value range VR to VR_VARYING. */
336 static inline void
337 set_value_range_to_varying (value_range *vr)
339 vr->type = VR_VARYING;
340 vr->min = vr->max = NULL_TREE;
341 if (vr->equiv)
342 bitmap_clear (vr->equiv);
346 /* Set value range VR to {T, MIN, MAX, EQUIV}. */
348 static void
349 set_value_range (value_range *vr, enum value_range_type t, tree min,
350 tree max, bitmap equiv)
352 /* Check the validity of the range. */
353 if (flag_checking
354 && (t == VR_RANGE || t == VR_ANTI_RANGE))
356 int cmp;
358 gcc_assert (min && max);
360 gcc_assert ((!TREE_OVERFLOW_P (min) || is_overflow_infinity (min))
361 && (!TREE_OVERFLOW_P (max) || is_overflow_infinity (max)));
363 if (INTEGRAL_TYPE_P (TREE_TYPE (min)) && t == VR_ANTI_RANGE)
364 gcc_assert (!vrp_val_is_min (min) || !vrp_val_is_max (max));
366 cmp = compare_values (min, max);
367 gcc_assert (cmp == 0 || cmp == -1 || cmp == -2);
369 if (needs_overflow_infinity (TREE_TYPE (min)))
370 gcc_assert (!is_overflow_infinity (min)
371 || !is_overflow_infinity (max));
374 if (flag_checking
375 && (t == VR_UNDEFINED || t == VR_VARYING))
377 gcc_assert (min == NULL_TREE && max == NULL_TREE);
378 gcc_assert (equiv == NULL || bitmap_empty_p (equiv));
381 vr->type = t;
382 vr->min = min;
383 vr->max = max;
385 /* Since updating the equivalence set involves deep copying the
386 bitmaps, only do it if absolutely necessary. */
387 if (vr->equiv == NULL
388 && equiv != NULL)
389 vr->equiv = BITMAP_ALLOC (&vrp_equiv_obstack);
391 if (equiv != vr->equiv)
393 if (equiv && !bitmap_empty_p (equiv))
394 bitmap_copy (vr->equiv, equiv);
395 else
396 bitmap_clear (vr->equiv);
401 /* Set value range VR to the canonical form of {T, MIN, MAX, EQUIV}.
402 This means adjusting T, MIN and MAX representing the case of a
403 wrapping range with MAX < MIN covering [MIN, type_max] U [type_min, MAX]
404 as anti-rage ~[MAX+1, MIN-1]. Likewise for wrapping anti-ranges.
405 In corner cases where MAX+1 or MIN-1 wraps this will fall back
406 to varying.
407 This routine exists to ease canonicalization in the case where we
408 extract ranges from var + CST op limit. */
410 static void
411 set_and_canonicalize_value_range (value_range *vr, enum value_range_type t,
412 tree min, tree max, bitmap equiv)
414 /* Use the canonical setters for VR_UNDEFINED and VR_VARYING. */
415 if (t == VR_UNDEFINED)
417 set_value_range_to_undefined (vr);
418 return;
420 else if (t == VR_VARYING)
422 set_value_range_to_varying (vr);
423 return;
426 /* Nothing to canonicalize for symbolic ranges. */
427 if (TREE_CODE (min) != INTEGER_CST
428 || TREE_CODE (max) != INTEGER_CST)
430 set_value_range (vr, t, min, max, equiv);
431 return;
434 /* Wrong order for min and max, to swap them and the VR type we need
435 to adjust them. */
436 if (tree_int_cst_lt (max, min))
438 tree one, tmp;
440 /* For one bit precision if max < min, then the swapped
441 range covers all values, so for VR_RANGE it is varying and
442 for VR_ANTI_RANGE empty range, so drop to varying as well. */
443 if (TYPE_PRECISION (TREE_TYPE (min)) == 1)
445 set_value_range_to_varying (vr);
446 return;
449 one = build_int_cst (TREE_TYPE (min), 1);
450 tmp = int_const_binop (PLUS_EXPR, max, one);
451 max = int_const_binop (MINUS_EXPR, min, one);
452 min = tmp;
454 /* There's one corner case, if we had [C+1, C] before we now have
455 that again. But this represents an empty value range, so drop
456 to varying in this case. */
457 if (tree_int_cst_lt (max, min))
459 set_value_range_to_varying (vr);
460 return;
463 t = t == VR_RANGE ? VR_ANTI_RANGE : VR_RANGE;
466 /* Anti-ranges that can be represented as ranges should be so. */
467 if (t == VR_ANTI_RANGE)
469 bool is_min = vrp_val_is_min (min);
470 bool is_max = vrp_val_is_max (max);
472 if (is_min && is_max)
474 /* We cannot deal with empty ranges, drop to varying.
475 ??? This could be VR_UNDEFINED instead. */
476 set_value_range_to_varying (vr);
477 return;
479 else if (TYPE_PRECISION (TREE_TYPE (min)) == 1
480 && (is_min || is_max))
482 /* Non-empty boolean ranges can always be represented
483 as a singleton range. */
484 if (is_min)
485 min = max = vrp_val_max (TREE_TYPE (min));
486 else
487 min = max = vrp_val_min (TREE_TYPE (min));
488 t = VR_RANGE;
490 else if (is_min
491 /* As a special exception preserve non-null ranges. */
492 && !(TYPE_UNSIGNED (TREE_TYPE (min))
493 && integer_zerop (max)))
495 tree one = build_int_cst (TREE_TYPE (max), 1);
496 min = int_const_binop (PLUS_EXPR, max, one);
497 max = vrp_val_max (TREE_TYPE (max));
498 t = VR_RANGE;
500 else if (is_max)
502 tree one = build_int_cst (TREE_TYPE (min), 1);
503 max = int_const_binop (MINUS_EXPR, min, one);
504 min = vrp_val_min (TREE_TYPE (min));
505 t = VR_RANGE;
509 /* Drop [-INF(OVF), +INF(OVF)] to varying. */
510 if (needs_overflow_infinity (TREE_TYPE (min))
511 && is_overflow_infinity (min)
512 && is_overflow_infinity (max))
514 set_value_range_to_varying (vr);
515 return;
518 set_value_range (vr, t, min, max, equiv);
521 /* Copy value range FROM into value range TO. */
523 static inline void
524 copy_value_range (value_range *to, value_range *from)
526 set_value_range (to, from->type, from->min, from->max, from->equiv);
529 /* Set value range VR to a single value. This function is only called
530 with values we get from statements, and exists to clear the
531 TREE_OVERFLOW flag so that we don't think we have an overflow
532 infinity when we shouldn't. */
534 static inline void
535 set_value_range_to_value (value_range *vr, tree val, bitmap equiv)
537 gcc_assert (is_gimple_min_invariant (val));
538 if (TREE_OVERFLOW_P (val))
539 val = drop_tree_overflow (val);
540 set_value_range (vr, VR_RANGE, val, val, equiv);
543 /* Set value range VR to a non-negative range of type TYPE.
544 OVERFLOW_INFINITY indicates whether to use an overflow infinity
545 rather than TYPE_MAX_VALUE; this should be true if we determine
546 that the range is nonnegative based on the assumption that signed
547 overflow does not occur. */
549 static inline void
550 set_value_range_to_nonnegative (value_range *vr, tree type,
551 bool overflow_infinity)
553 tree zero;
555 if (overflow_infinity && !supports_overflow_infinity (type))
557 set_value_range_to_varying (vr);
558 return;
561 zero = build_int_cst (type, 0);
562 set_value_range (vr, VR_RANGE, zero,
563 (overflow_infinity
564 ? positive_overflow_infinity (type)
565 : TYPE_MAX_VALUE (type)),
566 vr->equiv);
569 /* Set value range VR to a non-NULL range of type TYPE. */
571 static inline void
572 set_value_range_to_nonnull (value_range *vr, tree type)
574 tree zero = build_int_cst (type, 0);
575 set_value_range (vr, VR_ANTI_RANGE, zero, zero, vr->equiv);
579 /* Set value range VR to a NULL range of type TYPE. */
581 static inline void
582 set_value_range_to_null (value_range *vr, tree type)
584 set_value_range_to_value (vr, build_int_cst (type, 0), vr->equiv);
588 /* Set value range VR to a range of a truthvalue of type TYPE. */
590 static inline void
591 set_value_range_to_truthvalue (value_range *vr, tree type)
593 if (TYPE_PRECISION (type) == 1)
594 set_value_range_to_varying (vr);
595 else
596 set_value_range (vr, VR_RANGE,
597 build_int_cst (type, 0), build_int_cst (type, 1),
598 vr->equiv);
602 /* If abs (min) < abs (max), set VR to [-max, max], if
603 abs (min) >= abs (max), set VR to [-min, min]. */
605 static void
606 abs_extent_range (value_range *vr, tree min, tree max)
608 int cmp;
610 gcc_assert (TREE_CODE (min) == INTEGER_CST);
611 gcc_assert (TREE_CODE (max) == INTEGER_CST);
612 gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (min)));
613 gcc_assert (!TYPE_UNSIGNED (TREE_TYPE (min)));
614 min = fold_unary (ABS_EXPR, TREE_TYPE (min), min);
615 max = fold_unary (ABS_EXPR, TREE_TYPE (max), max);
616 if (TREE_OVERFLOW (min) || TREE_OVERFLOW (max))
618 set_value_range_to_varying (vr);
619 return;
621 cmp = compare_values (min, max);
622 if (cmp == -1)
623 min = fold_unary (NEGATE_EXPR, TREE_TYPE (min), max);
624 else if (cmp == 0 || cmp == 1)
626 max = min;
627 min = fold_unary (NEGATE_EXPR, TREE_TYPE (min), min);
629 else
631 set_value_range_to_varying (vr);
632 return;
634 set_and_canonicalize_value_range (vr, VR_RANGE, min, max, NULL);
638 /* Return value range information for VAR.
640 If we have no values ranges recorded (ie, VRP is not running), then
641 return NULL. Otherwise create an empty range if none existed for VAR. */
643 static value_range *
644 get_value_range (const_tree var)
646 static const value_range vr_const_varying
647 = { VR_VARYING, NULL_TREE, NULL_TREE, NULL };
648 value_range *vr;
649 tree sym;
650 unsigned ver = SSA_NAME_VERSION (var);
652 /* If we have no recorded ranges, then return NULL. */
653 if (! vr_value)
654 return NULL;
656 /* If we query the range for a new SSA name return an unmodifiable VARYING.
657 We should get here at most from the substitute-and-fold stage which
658 will never try to change values. */
659 if (ver >= num_vr_values)
660 return CONST_CAST (value_range *, &vr_const_varying);
662 vr = vr_value[ver];
663 if (vr)
664 return vr;
666 /* After propagation finished do not allocate new value-ranges. */
667 if (values_propagated)
668 return CONST_CAST (value_range *, &vr_const_varying);
670 /* Create a default value range. */
671 vr_value[ver] = vr = vrp_value_range_pool.allocate ();
672 memset (vr, 0, sizeof (*vr));
674 /* Defer allocating the equivalence set. */
675 vr->equiv = NULL;
677 /* If VAR is a default definition of a parameter, the variable can
678 take any value in VAR's type. */
679 if (SSA_NAME_IS_DEFAULT_DEF (var))
681 sym = SSA_NAME_VAR (var);
682 if (TREE_CODE (sym) == PARM_DECL)
684 /* Try to use the "nonnull" attribute to create ~[0, 0]
685 anti-ranges for pointers. Note that this is only valid with
686 default definitions of PARM_DECLs. */
687 if (POINTER_TYPE_P (TREE_TYPE (sym))
688 && nonnull_arg_p (sym))
689 set_value_range_to_nonnull (vr, TREE_TYPE (sym));
690 else if (INTEGRAL_TYPE_P (TREE_TYPE (sym)))
692 wide_int min, max;
693 value_range_type rtype = get_range_info (var, &min, &max);
694 if (rtype == VR_RANGE || rtype == VR_ANTI_RANGE)
695 set_value_range (vr, rtype,
696 wide_int_to_tree (TREE_TYPE (var), min),
697 wide_int_to_tree (TREE_TYPE (var), max),
698 NULL);
699 else
700 set_value_range_to_varying (vr);
702 else
703 set_value_range_to_varying (vr);
705 else if (TREE_CODE (sym) == RESULT_DECL
706 && DECL_BY_REFERENCE (sym))
707 set_value_range_to_nonnull (vr, TREE_TYPE (sym));
710 return vr;
713 /* Set value-ranges of all SSA names defined by STMT to varying. */
715 static void
716 set_defs_to_varying (gimple *stmt)
718 ssa_op_iter i;
719 tree def;
720 FOR_EACH_SSA_TREE_OPERAND (def, stmt, i, SSA_OP_DEF)
722 value_range *vr = get_value_range (def);
723 /* Avoid writing to vr_const_varying get_value_range may return. */
724 if (vr->type != VR_VARYING)
725 set_value_range_to_varying (vr);
730 /* Return true, if VAL1 and VAL2 are equal values for VRP purposes. */
732 static inline bool
733 vrp_operand_equal_p (const_tree val1, const_tree val2)
735 if (val1 == val2)
736 return true;
737 if (!val1 || !val2 || !operand_equal_p (val1, val2, 0))
738 return false;
739 return is_overflow_infinity (val1) == is_overflow_infinity (val2);
742 /* Return true, if the bitmaps B1 and B2 are equal. */
744 static inline bool
745 vrp_bitmap_equal_p (const_bitmap b1, const_bitmap b2)
747 return (b1 == b2
748 || ((!b1 || bitmap_empty_p (b1))
749 && (!b2 || bitmap_empty_p (b2)))
750 || (b1 && b2
751 && bitmap_equal_p (b1, b2)));
754 /* Update the value range and equivalence set for variable VAR to
755 NEW_VR. Return true if NEW_VR is different from VAR's previous
756 value.
758 NOTE: This function assumes that NEW_VR is a temporary value range
759 object created for the sole purpose of updating VAR's range. The
760 storage used by the equivalence set from NEW_VR will be freed by
761 this function. Do not call update_value_range when NEW_VR
762 is the range object associated with another SSA name. */
764 static inline bool
765 update_value_range (const_tree var, value_range *new_vr)
767 value_range *old_vr;
768 bool is_new;
770 /* If there is a value-range on the SSA name from earlier analysis
771 factor that in. */
772 if (INTEGRAL_TYPE_P (TREE_TYPE (var)))
774 wide_int min, max;
775 value_range_type rtype = get_range_info (var, &min, &max);
776 if (rtype == VR_RANGE || rtype == VR_ANTI_RANGE)
778 tree nr_min, nr_max;
779 /* Range info on SSA names doesn't carry overflow information
780 so make sure to preserve the overflow bit on the lattice. */
781 if (rtype == VR_RANGE
782 && needs_overflow_infinity (TREE_TYPE (var))
783 && (new_vr->type == VR_VARYING
784 || (new_vr->type == VR_RANGE
785 && is_negative_overflow_infinity (new_vr->min)))
786 && wi::eq_p (vrp_val_min (TREE_TYPE (var)), min))
787 nr_min = negative_overflow_infinity (TREE_TYPE (var));
788 else
789 nr_min = wide_int_to_tree (TREE_TYPE (var), min);
790 if (rtype == VR_RANGE
791 && needs_overflow_infinity (TREE_TYPE (var))
792 && (new_vr->type == VR_VARYING
793 || (new_vr->type == VR_RANGE
794 && is_positive_overflow_infinity (new_vr->max)))
795 && wi::eq_p (vrp_val_max (TREE_TYPE (var)), max))
796 nr_max = positive_overflow_infinity (TREE_TYPE (var));
797 else
798 nr_max = wide_int_to_tree (TREE_TYPE (var), max);
799 value_range nr = VR_INITIALIZER;
800 set_and_canonicalize_value_range (&nr, rtype, nr_min, nr_max, NULL);
801 vrp_intersect_ranges (new_vr, &nr);
805 /* Update the value range, if necessary. */
806 old_vr = get_value_range (var);
807 is_new = old_vr->type != new_vr->type
808 || !vrp_operand_equal_p (old_vr->min, new_vr->min)
809 || !vrp_operand_equal_p (old_vr->max, new_vr->max)
810 || !vrp_bitmap_equal_p (old_vr->equiv, new_vr->equiv);
812 if (is_new)
814 /* Do not allow transitions up the lattice. The following
815 is slightly more awkward than just new_vr->type < old_vr->type
816 because VR_RANGE and VR_ANTI_RANGE need to be considered
817 the same. We may not have is_new when transitioning to
818 UNDEFINED. If old_vr->type is VARYING, we shouldn't be
819 called. */
820 if (new_vr->type == VR_UNDEFINED)
822 BITMAP_FREE (new_vr->equiv);
823 set_value_range_to_varying (old_vr);
824 set_value_range_to_varying (new_vr);
825 return true;
827 else
828 set_value_range (old_vr, new_vr->type, new_vr->min, new_vr->max,
829 new_vr->equiv);
832 BITMAP_FREE (new_vr->equiv);
834 return is_new;
838 /* Add VAR and VAR's equivalence set to EQUIV. This is the central
839 point where equivalence processing can be turned on/off. */
841 static void
842 add_equivalence (bitmap *equiv, const_tree var)
844 unsigned ver = SSA_NAME_VERSION (var);
845 value_range *vr = get_value_range (var);
847 if (*equiv == NULL)
848 *equiv = BITMAP_ALLOC (&vrp_equiv_obstack);
849 bitmap_set_bit (*equiv, ver);
850 if (vr && vr->equiv)
851 bitmap_ior_into (*equiv, vr->equiv);
855 /* Return true if VR is ~[0, 0]. */
857 static inline bool
858 range_is_nonnull (value_range *vr)
860 return vr->type == VR_ANTI_RANGE
861 && integer_zerop (vr->min)
862 && integer_zerop (vr->max);
866 /* Return true if VR is [0, 0]. */
868 static inline bool
869 range_is_null (value_range *vr)
871 return vr->type == VR_RANGE
872 && integer_zerop (vr->min)
873 && integer_zerop (vr->max);
876 /* Return true if max and min of VR are INTEGER_CST. It's not necessary
877 a singleton. */
879 static inline bool
880 range_int_cst_p (value_range *vr)
882 return (vr->type == VR_RANGE
883 && TREE_CODE (vr->max) == INTEGER_CST
884 && TREE_CODE (vr->min) == INTEGER_CST);
887 /* Return true if VR is a INTEGER_CST singleton. */
889 static inline bool
890 range_int_cst_singleton_p (value_range *vr)
892 return (range_int_cst_p (vr)
893 && !is_overflow_infinity (vr->min)
894 && !is_overflow_infinity (vr->max)
895 && tree_int_cst_equal (vr->min, vr->max));
898 /* Return true if value range VR involves at least one symbol. */
900 static inline bool
901 symbolic_range_p (value_range *vr)
903 return (!is_gimple_min_invariant (vr->min)
904 || !is_gimple_min_invariant (vr->max));
907 /* Return the single symbol (an SSA_NAME) contained in T if any, or NULL_TREE
908 otherwise. We only handle additive operations and set NEG to true if the
909 symbol is negated and INV to the invariant part, if any. */
911 static tree
912 get_single_symbol (tree t, bool *neg, tree *inv)
914 bool neg_;
915 tree inv_;
917 *inv = NULL_TREE;
918 *neg = false;
920 if (TREE_CODE (t) == PLUS_EXPR
921 || TREE_CODE (t) == POINTER_PLUS_EXPR
922 || TREE_CODE (t) == MINUS_EXPR)
924 if (is_gimple_min_invariant (TREE_OPERAND (t, 0)))
926 neg_ = (TREE_CODE (t) == MINUS_EXPR);
927 inv_ = TREE_OPERAND (t, 0);
928 t = TREE_OPERAND (t, 1);
930 else if (is_gimple_min_invariant (TREE_OPERAND (t, 1)))
932 neg_ = false;
933 inv_ = TREE_OPERAND (t, 1);
934 t = TREE_OPERAND (t, 0);
936 else
937 return NULL_TREE;
939 else
941 neg_ = false;
942 inv_ = NULL_TREE;
945 if (TREE_CODE (t) == NEGATE_EXPR)
947 t = TREE_OPERAND (t, 0);
948 neg_ = !neg_;
951 if (TREE_CODE (t) != SSA_NAME)
952 return NULL_TREE;
954 *neg = neg_;
955 *inv = inv_;
956 return t;
959 /* The reverse operation: build a symbolic expression with TYPE
960 from symbol SYM, negated according to NEG, and invariant INV. */
962 static tree
963 build_symbolic_expr (tree type, tree sym, bool neg, tree inv)
965 const bool pointer_p = POINTER_TYPE_P (type);
966 tree t = sym;
968 if (neg)
969 t = build1 (NEGATE_EXPR, type, t);
971 if (integer_zerop (inv))
972 return t;
974 return build2 (pointer_p ? POINTER_PLUS_EXPR : PLUS_EXPR, type, t, inv);
977 /* Return true if value range VR involves exactly one symbol SYM. */
979 static bool
980 symbolic_range_based_on_p (value_range *vr, const_tree sym)
982 bool neg, min_has_symbol, max_has_symbol;
983 tree inv;
985 if (is_gimple_min_invariant (vr->min))
986 min_has_symbol = false;
987 else if (get_single_symbol (vr->min, &neg, &inv) == sym)
988 min_has_symbol = true;
989 else
990 return false;
992 if (is_gimple_min_invariant (vr->max))
993 max_has_symbol = false;
994 else if (get_single_symbol (vr->max, &neg, &inv) == sym)
995 max_has_symbol = true;
996 else
997 return false;
999 return (min_has_symbol || max_has_symbol);
1002 /* Return true if value range VR uses an overflow infinity. */
1004 static inline bool
1005 overflow_infinity_range_p (value_range *vr)
1007 return (vr->type == VR_RANGE
1008 && (is_overflow_infinity (vr->min)
1009 || is_overflow_infinity (vr->max)));
1012 /* Return false if we can not make a valid comparison based on VR;
1013 this will be the case if it uses an overflow infinity and overflow
1014 is not undefined (i.e., -fno-strict-overflow is in effect).
1015 Otherwise return true, and set *STRICT_OVERFLOW_P to true if VR
1016 uses an overflow infinity. */
1018 static bool
1019 usable_range_p (value_range *vr, bool *strict_overflow_p)
1021 gcc_assert (vr->type == VR_RANGE);
1022 if (is_overflow_infinity (vr->min))
1024 *strict_overflow_p = true;
1025 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (vr->min)))
1026 return false;
1028 if (is_overflow_infinity (vr->max))
1030 *strict_overflow_p = true;
1031 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (vr->max)))
1032 return false;
1034 return true;
1037 /* Return true if the result of assignment STMT is know to be non-zero.
1038 If the return value is based on the assumption that signed overflow is
1039 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1040 *STRICT_OVERFLOW_P.*/
1042 static bool
1043 gimple_assign_nonzero_warnv_p (gimple *stmt, bool *strict_overflow_p)
1045 enum tree_code code = gimple_assign_rhs_code (stmt);
1046 switch (get_gimple_rhs_class (code))
1048 case GIMPLE_UNARY_RHS:
1049 return tree_unary_nonzero_warnv_p (gimple_assign_rhs_code (stmt),
1050 gimple_expr_type (stmt),
1051 gimple_assign_rhs1 (stmt),
1052 strict_overflow_p);
1053 case GIMPLE_BINARY_RHS:
1054 return tree_binary_nonzero_warnv_p (gimple_assign_rhs_code (stmt),
1055 gimple_expr_type (stmt),
1056 gimple_assign_rhs1 (stmt),
1057 gimple_assign_rhs2 (stmt),
1058 strict_overflow_p);
1059 case GIMPLE_TERNARY_RHS:
1060 return false;
1061 case GIMPLE_SINGLE_RHS:
1062 return tree_single_nonzero_warnv_p (gimple_assign_rhs1 (stmt),
1063 strict_overflow_p);
1064 case GIMPLE_INVALID_RHS:
1065 gcc_unreachable ();
1066 default:
1067 gcc_unreachable ();
1071 /* Return true if STMT is known to compute a non-zero value.
1072 If the return value is based on the assumption that signed overflow is
1073 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1074 *STRICT_OVERFLOW_P.*/
1076 static bool
1077 gimple_stmt_nonzero_warnv_p (gimple *stmt, bool *strict_overflow_p)
1079 switch (gimple_code (stmt))
1081 case GIMPLE_ASSIGN:
1082 return gimple_assign_nonzero_warnv_p (stmt, strict_overflow_p);
1083 case GIMPLE_CALL:
1085 tree fndecl = gimple_call_fndecl (stmt);
1086 if (!fndecl) return false;
1087 if (flag_delete_null_pointer_checks && !flag_check_new
1088 && DECL_IS_OPERATOR_NEW (fndecl)
1089 && !TREE_NOTHROW (fndecl))
1090 return true;
1091 /* References are always non-NULL. */
1092 if (flag_delete_null_pointer_checks
1093 && TREE_CODE (TREE_TYPE (fndecl)) == REFERENCE_TYPE)
1094 return true;
1095 if (flag_delete_null_pointer_checks &&
1096 lookup_attribute ("returns_nonnull",
1097 TYPE_ATTRIBUTES (gimple_call_fntype (stmt))))
1098 return true;
1099 return gimple_alloca_call_p (stmt);
1101 default:
1102 gcc_unreachable ();
1106 /* Like tree_expr_nonzero_warnv_p, but this function uses value ranges
1107 obtained so far. */
1109 static bool
1110 vrp_stmt_computes_nonzero (gimple *stmt, bool *strict_overflow_p)
1112 if (gimple_stmt_nonzero_warnv_p (stmt, strict_overflow_p))
1113 return true;
1115 /* If we have an expression of the form &X->a, then the expression
1116 is nonnull if X is nonnull. */
1117 if (is_gimple_assign (stmt)
1118 && gimple_assign_rhs_code (stmt) == ADDR_EXPR)
1120 tree expr = gimple_assign_rhs1 (stmt);
1121 tree base = get_base_address (TREE_OPERAND (expr, 0));
1123 if (base != NULL_TREE
1124 && TREE_CODE (base) == MEM_REF
1125 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
1127 value_range *vr = get_value_range (TREE_OPERAND (base, 0));
1128 if (range_is_nonnull (vr))
1129 return true;
1133 return false;
1136 /* Returns true if EXPR is a valid value (as expected by compare_values) --
1137 a gimple invariant, or SSA_NAME +- CST. */
1139 static bool
1140 valid_value_p (tree expr)
1142 if (TREE_CODE (expr) == SSA_NAME)
1143 return true;
1145 if (TREE_CODE (expr) == PLUS_EXPR
1146 || TREE_CODE (expr) == MINUS_EXPR)
1147 return (TREE_CODE (TREE_OPERAND (expr, 0)) == SSA_NAME
1148 && TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST);
1150 return is_gimple_min_invariant (expr);
1153 /* Return
1154 1 if VAL < VAL2
1155 0 if !(VAL < VAL2)
1156 -2 if those are incomparable. */
1157 static inline int
1158 operand_less_p (tree val, tree val2)
1160 /* LT is folded faster than GE and others. Inline the common case. */
1161 if (TREE_CODE (val) == INTEGER_CST && TREE_CODE (val2) == INTEGER_CST)
1163 if (! is_positive_overflow_infinity (val2))
1164 return tree_int_cst_lt (val, val2);
1166 else
1168 tree tcmp;
1170 fold_defer_overflow_warnings ();
1172 tcmp = fold_binary_to_constant (LT_EXPR, boolean_type_node, val, val2);
1174 fold_undefer_and_ignore_overflow_warnings ();
1176 if (!tcmp
1177 || TREE_CODE (tcmp) != INTEGER_CST)
1178 return -2;
1180 if (!integer_zerop (tcmp))
1181 return 1;
1184 /* val >= val2, not considering overflow infinity. */
1185 if (is_negative_overflow_infinity (val))
1186 return is_negative_overflow_infinity (val2) ? 0 : 1;
1187 else if (is_positive_overflow_infinity (val2))
1188 return is_positive_overflow_infinity (val) ? 0 : 1;
1190 return 0;
1193 /* Compare two values VAL1 and VAL2. Return
1195 -2 if VAL1 and VAL2 cannot be compared at compile-time,
1196 -1 if VAL1 < VAL2,
1197 0 if VAL1 == VAL2,
1198 +1 if VAL1 > VAL2, and
1199 +2 if VAL1 != VAL2
1201 This is similar to tree_int_cst_compare but supports pointer values
1202 and values that cannot be compared at compile time.
1204 If STRICT_OVERFLOW_P is not NULL, then set *STRICT_OVERFLOW_P to
1205 true if the return value is only valid if we assume that signed
1206 overflow is undefined. */
1208 static int
1209 compare_values_warnv (tree val1, tree val2, bool *strict_overflow_p)
1211 if (val1 == val2)
1212 return 0;
1214 /* Below we rely on the fact that VAL1 and VAL2 are both pointers or
1215 both integers. */
1216 gcc_assert (POINTER_TYPE_P (TREE_TYPE (val1))
1217 == POINTER_TYPE_P (TREE_TYPE (val2)));
1219 /* Convert the two values into the same type. This is needed because
1220 sizetype causes sign extension even for unsigned types. */
1221 val2 = fold_convert (TREE_TYPE (val1), val2);
1222 STRIP_USELESS_TYPE_CONVERSION (val2);
1224 const bool overflow_undefined
1225 = INTEGRAL_TYPE_P (TREE_TYPE (val1))
1226 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (val1));
1227 tree inv1, inv2;
1228 bool neg1, neg2;
1229 tree sym1 = get_single_symbol (val1, &neg1, &inv1);
1230 tree sym2 = get_single_symbol (val2, &neg2, &inv2);
1232 /* If VAL1 and VAL2 are of the form '[-]NAME [+ CST]', return -1 or +1
1233 accordingly. If VAL1 and VAL2 don't use the same name, return -2. */
1234 if (sym1 && sym2)
1236 /* Both values must use the same name with the same sign. */
1237 if (sym1 != sym2 || neg1 != neg2)
1238 return -2;
1240 /* [-]NAME + CST == [-]NAME + CST. */
1241 if (inv1 == inv2)
1242 return 0;
1244 /* If overflow is defined we cannot simplify more. */
1245 if (!overflow_undefined)
1246 return -2;
1248 if (strict_overflow_p != NULL
1249 && (!inv1 || !TREE_NO_WARNING (val1))
1250 && (!inv2 || !TREE_NO_WARNING (val2)))
1251 *strict_overflow_p = true;
1253 if (!inv1)
1254 inv1 = build_int_cst (TREE_TYPE (val1), 0);
1255 if (!inv2)
1256 inv2 = build_int_cst (TREE_TYPE (val2), 0);
1258 return compare_values_warnv (inv1, inv2, strict_overflow_p);
1261 const bool cst1 = is_gimple_min_invariant (val1);
1262 const bool cst2 = is_gimple_min_invariant (val2);
1264 /* If one is of the form '[-]NAME + CST' and the other is constant, then
1265 it might be possible to say something depending on the constants. */
1266 if ((sym1 && inv1 && cst2) || (sym2 && inv2 && cst1))
1268 if (!overflow_undefined)
1269 return -2;
1271 if (strict_overflow_p != NULL
1272 && (!sym1 || !TREE_NO_WARNING (val1))
1273 && (!sym2 || !TREE_NO_WARNING (val2)))
1274 *strict_overflow_p = true;
1276 const signop sgn = TYPE_SIGN (TREE_TYPE (val1));
1277 tree cst = cst1 ? val1 : val2;
1278 tree inv = cst1 ? inv2 : inv1;
1280 /* Compute the difference between the constants. If it overflows or
1281 underflows, this means that we can trivially compare the NAME with
1282 it and, consequently, the two values with each other. */
1283 wide_int diff = wi::sub (cst, inv);
1284 if (wi::cmp (0, inv, sgn) != wi::cmp (diff, cst, sgn))
1286 const int res = wi::cmp (cst, inv, sgn);
1287 return cst1 ? res : -res;
1290 return -2;
1293 /* We cannot say anything more for non-constants. */
1294 if (!cst1 || !cst2)
1295 return -2;
1297 if (!POINTER_TYPE_P (TREE_TYPE (val1)))
1299 /* We cannot compare overflowed values, except for overflow
1300 infinities. */
1301 if (TREE_OVERFLOW (val1) || TREE_OVERFLOW (val2))
1303 if (strict_overflow_p != NULL)
1304 *strict_overflow_p = true;
1305 if (is_negative_overflow_infinity (val1))
1306 return is_negative_overflow_infinity (val2) ? 0 : -1;
1307 else if (is_negative_overflow_infinity (val2))
1308 return 1;
1309 else if (is_positive_overflow_infinity (val1))
1310 return is_positive_overflow_infinity (val2) ? 0 : 1;
1311 else if (is_positive_overflow_infinity (val2))
1312 return -1;
1313 return -2;
1316 return tree_int_cst_compare (val1, val2);
1318 else
1320 tree t;
1322 /* First see if VAL1 and VAL2 are not the same. */
1323 if (val1 == val2 || operand_equal_p (val1, val2, 0))
1324 return 0;
1326 /* If VAL1 is a lower address than VAL2, return -1. */
1327 if (operand_less_p (val1, val2) == 1)
1328 return -1;
1330 /* If VAL1 is a higher address than VAL2, return +1. */
1331 if (operand_less_p (val2, val1) == 1)
1332 return 1;
1334 /* If VAL1 is different than VAL2, return +2.
1335 For integer constants we either have already returned -1 or 1
1336 or they are equivalent. We still might succeed in proving
1337 something about non-trivial operands. */
1338 if (TREE_CODE (val1) != INTEGER_CST
1339 || TREE_CODE (val2) != INTEGER_CST)
1341 t = fold_binary_to_constant (NE_EXPR, boolean_type_node, val1, val2);
1342 if (t && integer_onep (t))
1343 return 2;
1346 return -2;
1350 /* Compare values like compare_values_warnv, but treat comparisons of
1351 nonconstants which rely on undefined overflow as incomparable. */
1353 static int
1354 compare_values (tree val1, tree val2)
1356 bool sop;
1357 int ret;
1359 sop = false;
1360 ret = compare_values_warnv (val1, val2, &sop);
1361 if (sop
1362 && (!is_gimple_min_invariant (val1) || !is_gimple_min_invariant (val2)))
1363 ret = -2;
1364 return ret;
1368 /* Return 1 if VAL is inside value range MIN <= VAL <= MAX,
1369 0 if VAL is not inside [MIN, MAX],
1370 -2 if we cannot tell either way.
1372 Benchmark compile/20001226-1.c compilation time after changing this
1373 function. */
1375 static inline int
1376 value_inside_range (tree val, tree min, tree max)
1378 int cmp1, cmp2;
1380 cmp1 = operand_less_p (val, min);
1381 if (cmp1 == -2)
1382 return -2;
1383 if (cmp1 == 1)
1384 return 0;
1386 cmp2 = operand_less_p (max, val);
1387 if (cmp2 == -2)
1388 return -2;
1390 return !cmp2;
1394 /* Return true if value ranges VR0 and VR1 have a non-empty
1395 intersection.
1397 Benchmark compile/20001226-1.c compilation time after changing this
1398 function.
1401 static inline bool
1402 value_ranges_intersect_p (value_range *vr0, value_range *vr1)
1404 /* The value ranges do not intersect if the maximum of the first range is
1405 less than the minimum of the second range or vice versa.
1406 When those relations are unknown, we can't do any better. */
1407 if (operand_less_p (vr0->max, vr1->min) != 0)
1408 return false;
1409 if (operand_less_p (vr1->max, vr0->min) != 0)
1410 return false;
1411 return true;
1415 /* Return 1 if [MIN, MAX] includes the value zero, 0 if it does not
1416 include the value zero, -2 if we cannot tell. */
1418 static inline int
1419 range_includes_zero_p (tree min, tree max)
1421 tree zero = build_int_cst (TREE_TYPE (min), 0);
1422 return value_inside_range (zero, min, max);
1425 /* Return true if *VR is know to only contain nonnegative values. */
1427 static inline bool
1428 value_range_nonnegative_p (value_range *vr)
1430 /* Testing for VR_ANTI_RANGE is not useful here as any anti-range
1431 which would return a useful value should be encoded as a
1432 VR_RANGE. */
1433 if (vr->type == VR_RANGE)
1435 int result = compare_values (vr->min, integer_zero_node);
1436 return (result == 0 || result == 1);
1439 return false;
1442 /* If *VR has a value rante that is a single constant value return that,
1443 otherwise return NULL_TREE. */
1445 static tree
1446 value_range_constant_singleton (value_range *vr)
1448 if (vr->type == VR_RANGE
1449 && vrp_operand_equal_p (vr->min, vr->max)
1450 && is_gimple_min_invariant (vr->min))
1451 return vr->min;
1453 return NULL_TREE;
1456 /* If OP has a value range with a single constant value return that,
1457 otherwise return NULL_TREE. This returns OP itself if OP is a
1458 constant. */
1460 static tree
1461 op_with_constant_singleton_value_range (tree op)
1463 if (is_gimple_min_invariant (op))
1464 return op;
1466 if (TREE_CODE (op) != SSA_NAME)
1467 return NULL_TREE;
1469 return value_range_constant_singleton (get_value_range (op));
1472 /* Return true if op is in a boolean [0, 1] value-range. */
1474 static bool
1475 op_with_boolean_value_range_p (tree op)
1477 value_range *vr;
1479 if (TYPE_PRECISION (TREE_TYPE (op)) == 1)
1480 return true;
1482 if (integer_zerop (op)
1483 || integer_onep (op))
1484 return true;
1486 if (TREE_CODE (op) != SSA_NAME)
1487 return false;
1489 vr = get_value_range (op);
1490 return (vr->type == VR_RANGE
1491 && integer_zerop (vr->min)
1492 && integer_onep (vr->max));
1495 /* Extract value range information for VAR when (OP COND_CODE LIMIT) is
1496 true and store it in *VR_P. */
1498 static void
1499 extract_range_for_var_from_comparison_expr (tree var, enum tree_code cond_code,
1500 tree op, tree limit,
1501 value_range *vr_p)
1503 tree min, max, type;
1504 value_range *limit_vr;
1505 limit = avoid_overflow_infinity (limit);
1506 type = TREE_TYPE (var);
1507 gcc_assert (limit != var);
1509 /* For pointer arithmetic, we only keep track of pointer equality
1510 and inequality. */
1511 if (POINTER_TYPE_P (type) && cond_code != NE_EXPR && cond_code != EQ_EXPR)
1513 set_value_range_to_varying (vr_p);
1514 return;
1517 /* If LIMIT is another SSA name and LIMIT has a range of its own,
1518 try to use LIMIT's range to avoid creating symbolic ranges
1519 unnecessarily. */
1520 limit_vr = (TREE_CODE (limit) == SSA_NAME) ? get_value_range (limit) : NULL;
1522 /* LIMIT's range is only interesting if it has any useful information. */
1523 if (! limit_vr
1524 || limit_vr->type == VR_UNDEFINED
1525 || limit_vr->type == VR_VARYING
1526 || (symbolic_range_p (limit_vr)
1527 && ! (limit_vr->type == VR_RANGE
1528 && (limit_vr->min == limit_vr->max
1529 || operand_equal_p (limit_vr->min, limit_vr->max, 0)))))
1530 limit_vr = NULL;
1532 /* Initially, the new range has the same set of equivalences of
1533 VAR's range. This will be revised before returning the final
1534 value. Since assertions may be chained via mutually exclusive
1535 predicates, we will need to trim the set of equivalences before
1536 we are done. */
1537 gcc_assert (vr_p->equiv == NULL);
1538 add_equivalence (&vr_p->equiv, var);
1540 /* Extract a new range based on the asserted comparison for VAR and
1541 LIMIT's value range. Notice that if LIMIT has an anti-range, we
1542 will only use it for equality comparisons (EQ_EXPR). For any
1543 other kind of assertion, we cannot derive a range from LIMIT's
1544 anti-range that can be used to describe the new range. For
1545 instance, ASSERT_EXPR <x_2, x_2 <= b_4>. If b_4 is ~[2, 10],
1546 then b_4 takes on the ranges [-INF, 1] and [11, +INF]. There is
1547 no single range for x_2 that could describe LE_EXPR, so we might
1548 as well build the range [b_4, +INF] for it.
1549 One special case we handle is extracting a range from a
1550 range test encoded as (unsigned)var + CST <= limit. */
1551 if (TREE_CODE (op) == NOP_EXPR
1552 || TREE_CODE (op) == PLUS_EXPR)
1554 if (TREE_CODE (op) == PLUS_EXPR)
1556 min = fold_build1 (NEGATE_EXPR, TREE_TYPE (TREE_OPERAND (op, 1)),
1557 TREE_OPERAND (op, 1));
1558 max = int_const_binop (PLUS_EXPR, limit, min);
1559 op = TREE_OPERAND (op, 0);
1561 else
1563 min = build_int_cst (TREE_TYPE (var), 0);
1564 max = limit;
1567 /* Make sure to not set TREE_OVERFLOW on the final type
1568 conversion. We are willingly interpreting large positive
1569 unsigned values as negative signed values here. */
1570 min = force_fit_type (TREE_TYPE (var), wi::to_widest (min), 0, false);
1571 max = force_fit_type (TREE_TYPE (var), wi::to_widest (max), 0, false);
1573 /* We can transform a max, min range to an anti-range or
1574 vice-versa. Use set_and_canonicalize_value_range which does
1575 this for us. */
1576 if (cond_code == LE_EXPR)
1577 set_and_canonicalize_value_range (vr_p, VR_RANGE,
1578 min, max, vr_p->equiv);
1579 else if (cond_code == GT_EXPR)
1580 set_and_canonicalize_value_range (vr_p, VR_ANTI_RANGE,
1581 min, max, vr_p->equiv);
1582 else
1583 gcc_unreachable ();
1585 else if (cond_code == EQ_EXPR)
1587 enum value_range_type range_type;
1589 if (limit_vr)
1591 range_type = limit_vr->type;
1592 min = limit_vr->min;
1593 max = limit_vr->max;
1595 else
1597 range_type = VR_RANGE;
1598 min = limit;
1599 max = limit;
1602 set_value_range (vr_p, range_type, min, max, vr_p->equiv);
1604 /* When asserting the equality VAR == LIMIT and LIMIT is another
1605 SSA name, the new range will also inherit the equivalence set
1606 from LIMIT. */
1607 if (TREE_CODE (limit) == SSA_NAME)
1608 add_equivalence (&vr_p->equiv, limit);
1610 else if (cond_code == NE_EXPR)
1612 /* As described above, when LIMIT's range is an anti-range and
1613 this assertion is an inequality (NE_EXPR), then we cannot
1614 derive anything from the anti-range. For instance, if
1615 LIMIT's range was ~[0, 0], the assertion 'VAR != LIMIT' does
1616 not imply that VAR's range is [0, 0]. So, in the case of
1617 anti-ranges, we just assert the inequality using LIMIT and
1618 not its anti-range.
1620 If LIMIT_VR is a range, we can only use it to build a new
1621 anti-range if LIMIT_VR is a single-valued range. For
1622 instance, if LIMIT_VR is [0, 1], the predicate
1623 VAR != [0, 1] does not mean that VAR's range is ~[0, 1].
1624 Rather, it means that for value 0 VAR should be ~[0, 0]
1625 and for value 1, VAR should be ~[1, 1]. We cannot
1626 represent these ranges.
1628 The only situation in which we can build a valid
1629 anti-range is when LIMIT_VR is a single-valued range
1630 (i.e., LIMIT_VR->MIN == LIMIT_VR->MAX). In that case,
1631 build the anti-range ~[LIMIT_VR->MIN, LIMIT_VR->MAX]. */
1632 if (limit_vr
1633 && limit_vr->type == VR_RANGE
1634 && compare_values (limit_vr->min, limit_vr->max) == 0)
1636 min = limit_vr->min;
1637 max = limit_vr->max;
1639 else
1641 /* In any other case, we cannot use LIMIT's range to build a
1642 valid anti-range. */
1643 min = max = limit;
1646 /* If MIN and MAX cover the whole range for their type, then
1647 just use the original LIMIT. */
1648 if (INTEGRAL_TYPE_P (type)
1649 && vrp_val_is_min (min)
1650 && vrp_val_is_max (max))
1651 min = max = limit;
1653 set_and_canonicalize_value_range (vr_p, VR_ANTI_RANGE,
1654 min, max, vr_p->equiv);
1656 else if (cond_code == LE_EXPR || cond_code == LT_EXPR)
1658 min = TYPE_MIN_VALUE (type);
1660 if (limit_vr == NULL || limit_vr->type == VR_ANTI_RANGE)
1661 max = limit;
1662 else
1664 /* If LIMIT_VR is of the form [N1, N2], we need to build the
1665 range [MIN, N2] for LE_EXPR and [MIN, N2 - 1] for
1666 LT_EXPR. */
1667 max = limit_vr->max;
1670 /* If the maximum value forces us to be out of bounds, simply punt.
1671 It would be pointless to try and do anything more since this
1672 all should be optimized away above us. */
1673 if ((cond_code == LT_EXPR
1674 && compare_values (max, min) == 0)
1675 || is_overflow_infinity (max))
1676 set_value_range_to_varying (vr_p);
1677 else
1679 /* For LT_EXPR, we create the range [MIN, MAX - 1]. */
1680 if (cond_code == LT_EXPR)
1682 if (TYPE_PRECISION (TREE_TYPE (max)) == 1
1683 && !TYPE_UNSIGNED (TREE_TYPE (max)))
1684 max = fold_build2 (PLUS_EXPR, TREE_TYPE (max), max,
1685 build_int_cst (TREE_TYPE (max), -1));
1686 else
1687 max = fold_build2 (MINUS_EXPR, TREE_TYPE (max), max,
1688 build_int_cst (TREE_TYPE (max), 1));
1689 if (EXPR_P (max))
1690 TREE_NO_WARNING (max) = 1;
1693 set_value_range (vr_p, VR_RANGE, min, max, vr_p->equiv);
1696 else if (cond_code == GE_EXPR || cond_code == GT_EXPR)
1698 max = TYPE_MAX_VALUE (type);
1700 if (limit_vr == NULL || limit_vr->type == VR_ANTI_RANGE)
1701 min = limit;
1702 else
1704 /* If LIMIT_VR is of the form [N1, N2], we need to build the
1705 range [N1, MAX] for GE_EXPR and [N1 + 1, MAX] for
1706 GT_EXPR. */
1707 min = limit_vr->min;
1710 /* If the minimum value forces us to be out of bounds, simply punt.
1711 It would be pointless to try and do anything more since this
1712 all should be optimized away above us. */
1713 if ((cond_code == GT_EXPR
1714 && compare_values (min, max) == 0)
1715 || is_overflow_infinity (min))
1716 set_value_range_to_varying (vr_p);
1717 else
1719 /* For GT_EXPR, we create the range [MIN + 1, MAX]. */
1720 if (cond_code == GT_EXPR)
1722 if (TYPE_PRECISION (TREE_TYPE (min)) == 1
1723 && !TYPE_UNSIGNED (TREE_TYPE (min)))
1724 min = fold_build2 (MINUS_EXPR, TREE_TYPE (min), min,
1725 build_int_cst (TREE_TYPE (min), -1));
1726 else
1727 min = fold_build2 (PLUS_EXPR, TREE_TYPE (min), min,
1728 build_int_cst (TREE_TYPE (min), 1));
1729 if (EXPR_P (min))
1730 TREE_NO_WARNING (min) = 1;
1733 set_value_range (vr_p, VR_RANGE, min, max, vr_p->equiv);
1736 else
1737 gcc_unreachable ();
1739 /* Finally intersect the new range with what we already know about var. */
1740 vrp_intersect_ranges (vr_p, get_value_range (var));
1743 /* Extract value range information from an ASSERT_EXPR EXPR and store
1744 it in *VR_P. */
1746 static void
1747 extract_range_from_assert (value_range *vr_p, tree expr)
1749 tree var = ASSERT_EXPR_VAR (expr);
1750 tree cond = ASSERT_EXPR_COND (expr);
1751 tree limit, op;
1752 enum tree_code cond_code;
1753 gcc_assert (COMPARISON_CLASS_P (cond));
1755 /* Find VAR in the ASSERT_EXPR conditional. */
1756 if (var == TREE_OPERAND (cond, 0)
1757 || TREE_CODE (TREE_OPERAND (cond, 0)) == PLUS_EXPR
1758 || TREE_CODE (TREE_OPERAND (cond, 0)) == NOP_EXPR)
1760 /* If the predicate is of the form VAR COMP LIMIT, then we just
1761 take LIMIT from the RHS and use the same comparison code. */
1762 cond_code = TREE_CODE (cond);
1763 limit = TREE_OPERAND (cond, 1);
1764 op = TREE_OPERAND (cond, 0);
1766 else
1768 /* If the predicate is of the form LIMIT COMP VAR, then we need
1769 to flip around the comparison code to create the proper range
1770 for VAR. */
1771 cond_code = swap_tree_comparison (TREE_CODE (cond));
1772 limit = TREE_OPERAND (cond, 0);
1773 op = TREE_OPERAND (cond, 1);
1775 extract_range_for_var_from_comparison_expr (var, cond_code, op,
1776 limit, vr_p);
1779 /* Extract range information from SSA name VAR and store it in VR. If
1780 VAR has an interesting range, use it. Otherwise, create the
1781 range [VAR, VAR] and return it. This is useful in situations where
1782 we may have conditionals testing values of VARYING names. For
1783 instance,
1785 x_3 = y_5;
1786 if (x_3 > y_5)
1789 Even if y_5 is deemed VARYING, we can determine that x_3 > y_5 is
1790 always false. */
1792 static void
1793 extract_range_from_ssa_name (value_range *vr, tree var)
1795 value_range *var_vr = get_value_range (var);
1797 if (var_vr->type != VR_VARYING)
1798 copy_value_range (vr, var_vr);
1799 else
1800 set_value_range (vr, VR_RANGE, var, var, NULL);
1802 add_equivalence (&vr->equiv, var);
1806 /* Wrapper around int_const_binop. If the operation overflows and we
1807 are not using wrapping arithmetic, then adjust the result to be
1808 -INF or +INF depending on CODE, VAL1 and VAL2. This can return
1809 NULL_TREE if we need to use an overflow infinity representation but
1810 the type does not support it. */
1812 static tree
1813 vrp_int_const_binop (enum tree_code code, tree val1, tree val2)
1815 tree res;
1817 res = int_const_binop (code, val1, val2);
1819 /* If we are using unsigned arithmetic, operate symbolically
1820 on -INF and +INF as int_const_binop only handles signed overflow. */
1821 if (TYPE_UNSIGNED (TREE_TYPE (val1)))
1823 int checkz = compare_values (res, val1);
1824 bool overflow = false;
1826 /* Ensure that res = val1 [+*] val2 >= val1
1827 or that res = val1 - val2 <= val1. */
1828 if ((code == PLUS_EXPR
1829 && !(checkz == 1 || checkz == 0))
1830 || (code == MINUS_EXPR
1831 && !(checkz == 0 || checkz == -1)))
1833 overflow = true;
1835 /* Checking for multiplication overflow is done by dividing the
1836 output of the multiplication by the first input of the
1837 multiplication. If the result of that division operation is
1838 not equal to the second input of the multiplication, then the
1839 multiplication overflowed. */
1840 else if (code == MULT_EXPR && !integer_zerop (val1))
1842 tree tmp = int_const_binop (TRUNC_DIV_EXPR,
1843 res,
1844 val1);
1845 int check = compare_values (tmp, val2);
1847 if (check != 0)
1848 overflow = true;
1851 if (overflow)
1853 res = copy_node (res);
1854 TREE_OVERFLOW (res) = 1;
1858 else if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (val1)))
1859 /* If the singed operation wraps then int_const_binop has done
1860 everything we want. */
1862 /* Signed division of -1/0 overflows and by the time it gets here
1863 returns NULL_TREE. */
1864 else if (!res)
1865 return NULL_TREE;
1866 else if ((TREE_OVERFLOW (res)
1867 && !TREE_OVERFLOW (val1)
1868 && !TREE_OVERFLOW (val2))
1869 || is_overflow_infinity (val1)
1870 || is_overflow_infinity (val2))
1872 /* If the operation overflowed but neither VAL1 nor VAL2 are
1873 overflown, return -INF or +INF depending on the operation
1874 and the combination of signs of the operands. */
1875 int sgn1 = tree_int_cst_sgn (val1);
1876 int sgn2 = tree_int_cst_sgn (val2);
1878 if (needs_overflow_infinity (TREE_TYPE (res))
1879 && !supports_overflow_infinity (TREE_TYPE (res)))
1880 return NULL_TREE;
1882 /* We have to punt on adding infinities of different signs,
1883 since we can't tell what the sign of the result should be.
1884 Likewise for subtracting infinities of the same sign. */
1885 if (((code == PLUS_EXPR && sgn1 != sgn2)
1886 || (code == MINUS_EXPR && sgn1 == sgn2))
1887 && is_overflow_infinity (val1)
1888 && is_overflow_infinity (val2))
1889 return NULL_TREE;
1891 /* Don't try to handle division or shifting of infinities. */
1892 if ((code == TRUNC_DIV_EXPR
1893 || code == FLOOR_DIV_EXPR
1894 || code == CEIL_DIV_EXPR
1895 || code == EXACT_DIV_EXPR
1896 || code == ROUND_DIV_EXPR
1897 || code == RSHIFT_EXPR)
1898 && (is_overflow_infinity (val1)
1899 || is_overflow_infinity (val2)))
1900 return NULL_TREE;
1902 /* Notice that we only need to handle the restricted set of
1903 operations handled by extract_range_from_binary_expr.
1904 Among them, only multiplication, addition and subtraction
1905 can yield overflow without overflown operands because we
1906 are working with integral types only... except in the
1907 case VAL1 = -INF and VAL2 = -1 which overflows to +INF
1908 for division too. */
1910 /* For multiplication, the sign of the overflow is given
1911 by the comparison of the signs of the operands. */
1912 if ((code == MULT_EXPR && sgn1 == sgn2)
1913 /* For addition, the operands must be of the same sign
1914 to yield an overflow. Its sign is therefore that
1915 of one of the operands, for example the first. For
1916 infinite operands X + -INF is negative, not positive. */
1917 || (code == PLUS_EXPR
1918 && (sgn1 >= 0
1919 ? !is_negative_overflow_infinity (val2)
1920 : is_positive_overflow_infinity (val2)))
1921 /* For subtraction, non-infinite operands must be of
1922 different signs to yield an overflow. Its sign is
1923 therefore that of the first operand or the opposite of
1924 that of the second operand. A first operand of 0 counts
1925 as positive here, for the corner case 0 - (-INF), which
1926 overflows, but must yield +INF. For infinite operands 0
1927 - INF is negative, not positive. */
1928 || (code == MINUS_EXPR
1929 && (sgn1 >= 0
1930 ? !is_positive_overflow_infinity (val2)
1931 : is_negative_overflow_infinity (val2)))
1932 /* We only get in here with positive shift count, so the
1933 overflow direction is the same as the sign of val1.
1934 Actually rshift does not overflow at all, but we only
1935 handle the case of shifting overflowed -INF and +INF. */
1936 || (code == RSHIFT_EXPR
1937 && sgn1 >= 0)
1938 /* For division, the only case is -INF / -1 = +INF. */
1939 || code == TRUNC_DIV_EXPR
1940 || code == FLOOR_DIV_EXPR
1941 || code == CEIL_DIV_EXPR
1942 || code == EXACT_DIV_EXPR
1943 || code == ROUND_DIV_EXPR)
1944 return (needs_overflow_infinity (TREE_TYPE (res))
1945 ? positive_overflow_infinity (TREE_TYPE (res))
1946 : TYPE_MAX_VALUE (TREE_TYPE (res)));
1947 else
1948 return (needs_overflow_infinity (TREE_TYPE (res))
1949 ? negative_overflow_infinity (TREE_TYPE (res))
1950 : TYPE_MIN_VALUE (TREE_TYPE (res)));
1953 return res;
1957 /* For range VR compute two wide_int bitmasks. In *MAY_BE_NONZERO
1958 bitmask if some bit is unset, it means for all numbers in the range
1959 the bit is 0, otherwise it might be 0 or 1. In *MUST_BE_NONZERO
1960 bitmask if some bit is set, it means for all numbers in the range
1961 the bit is 1, otherwise it might be 0 or 1. */
1963 static bool
1964 zero_nonzero_bits_from_vr (const tree expr_type,
1965 value_range *vr,
1966 wide_int *may_be_nonzero,
1967 wide_int *must_be_nonzero)
1969 *may_be_nonzero = wi::minus_one (TYPE_PRECISION (expr_type));
1970 *must_be_nonzero = wi::zero (TYPE_PRECISION (expr_type));
1971 if (!range_int_cst_p (vr)
1972 || is_overflow_infinity (vr->min)
1973 || is_overflow_infinity (vr->max))
1974 return false;
1976 if (range_int_cst_singleton_p (vr))
1978 *may_be_nonzero = vr->min;
1979 *must_be_nonzero = *may_be_nonzero;
1981 else if (tree_int_cst_sgn (vr->min) >= 0
1982 || tree_int_cst_sgn (vr->max) < 0)
1984 wide_int xor_mask = wi::bit_xor (vr->min, vr->max);
1985 *may_be_nonzero = wi::bit_or (vr->min, vr->max);
1986 *must_be_nonzero = wi::bit_and (vr->min, vr->max);
1987 if (xor_mask != 0)
1989 wide_int mask = wi::mask (wi::floor_log2 (xor_mask), false,
1990 may_be_nonzero->get_precision ());
1991 *may_be_nonzero = *may_be_nonzero | mask;
1992 *must_be_nonzero = must_be_nonzero->and_not (mask);
1996 return true;
1999 /* Create two value-ranges in *VR0 and *VR1 from the anti-range *AR
2000 so that *VR0 U *VR1 == *AR. Returns true if that is possible,
2001 false otherwise. If *AR can be represented with a single range
2002 *VR1 will be VR_UNDEFINED. */
2004 static bool
2005 ranges_from_anti_range (value_range *ar,
2006 value_range *vr0, value_range *vr1)
2008 tree type = TREE_TYPE (ar->min);
2010 vr0->type = VR_UNDEFINED;
2011 vr1->type = VR_UNDEFINED;
2013 if (ar->type != VR_ANTI_RANGE
2014 || TREE_CODE (ar->min) != INTEGER_CST
2015 || TREE_CODE (ar->max) != INTEGER_CST
2016 || !vrp_val_min (type)
2017 || !vrp_val_max (type))
2018 return false;
2020 if (!vrp_val_is_min (ar->min))
2022 vr0->type = VR_RANGE;
2023 vr0->min = vrp_val_min (type);
2024 vr0->max = wide_int_to_tree (type, wi::sub (ar->min, 1));
2026 if (!vrp_val_is_max (ar->max))
2028 vr1->type = VR_RANGE;
2029 vr1->min = wide_int_to_tree (type, wi::add (ar->max, 1));
2030 vr1->max = vrp_val_max (type);
2032 if (vr0->type == VR_UNDEFINED)
2034 *vr0 = *vr1;
2035 vr1->type = VR_UNDEFINED;
2038 return vr0->type != VR_UNDEFINED;
2041 /* Helper to extract a value-range *VR for a multiplicative operation
2042 *VR0 CODE *VR1. */
2044 static void
2045 extract_range_from_multiplicative_op_1 (value_range *vr,
2046 enum tree_code code,
2047 value_range *vr0, value_range *vr1)
2049 enum value_range_type type;
2050 tree val[4];
2051 size_t i;
2052 tree min, max;
2053 bool sop;
2054 int cmp;
2056 /* Multiplications, divisions and shifts are a bit tricky to handle,
2057 depending on the mix of signs we have in the two ranges, we
2058 need to operate on different values to get the minimum and
2059 maximum values for the new range. One approach is to figure
2060 out all the variations of range combinations and do the
2061 operations.
2063 However, this involves several calls to compare_values and it
2064 is pretty convoluted. It's simpler to do the 4 operations
2065 (MIN0 OP MIN1, MIN0 OP MAX1, MAX0 OP MIN1 and MAX0 OP MAX0 OP
2066 MAX1) and then figure the smallest and largest values to form
2067 the new range. */
2068 gcc_assert (code == MULT_EXPR
2069 || code == TRUNC_DIV_EXPR
2070 || code == FLOOR_DIV_EXPR
2071 || code == CEIL_DIV_EXPR
2072 || code == EXACT_DIV_EXPR
2073 || code == ROUND_DIV_EXPR
2074 || code == RSHIFT_EXPR
2075 || code == LSHIFT_EXPR);
2076 gcc_assert ((vr0->type == VR_RANGE
2077 || (code == MULT_EXPR && vr0->type == VR_ANTI_RANGE))
2078 && vr0->type == vr1->type);
2080 type = vr0->type;
2082 /* Compute the 4 cross operations. */
2083 sop = false;
2084 val[0] = vrp_int_const_binop (code, vr0->min, vr1->min);
2085 if (val[0] == NULL_TREE)
2086 sop = true;
2088 if (vr1->max == vr1->min)
2089 val[1] = NULL_TREE;
2090 else
2092 val[1] = vrp_int_const_binop (code, vr0->min, vr1->max);
2093 if (val[1] == NULL_TREE)
2094 sop = true;
2097 if (vr0->max == vr0->min)
2098 val[2] = NULL_TREE;
2099 else
2101 val[2] = vrp_int_const_binop (code, vr0->max, vr1->min);
2102 if (val[2] == NULL_TREE)
2103 sop = true;
2106 if (vr0->min == vr0->max || vr1->min == vr1->max)
2107 val[3] = NULL_TREE;
2108 else
2110 val[3] = vrp_int_const_binop (code, vr0->max, vr1->max);
2111 if (val[3] == NULL_TREE)
2112 sop = true;
2115 if (sop)
2117 set_value_range_to_varying (vr);
2118 return;
2121 /* Set MIN to the minimum of VAL[i] and MAX to the maximum
2122 of VAL[i]. */
2123 min = val[0];
2124 max = val[0];
2125 for (i = 1; i < 4; i++)
2127 if (!is_gimple_min_invariant (min)
2128 || (TREE_OVERFLOW (min) && !is_overflow_infinity (min))
2129 || !is_gimple_min_invariant (max)
2130 || (TREE_OVERFLOW (max) && !is_overflow_infinity (max)))
2131 break;
2133 if (val[i])
2135 if (!is_gimple_min_invariant (val[i])
2136 || (TREE_OVERFLOW (val[i])
2137 && !is_overflow_infinity (val[i])))
2139 /* If we found an overflowed value, set MIN and MAX
2140 to it so that we set the resulting range to
2141 VARYING. */
2142 min = max = val[i];
2143 break;
2146 if (compare_values (val[i], min) == -1)
2147 min = val[i];
2149 if (compare_values (val[i], max) == 1)
2150 max = val[i];
2154 /* If either MIN or MAX overflowed, then set the resulting range to
2155 VARYING. But we do accept an overflow infinity
2156 representation. */
2157 if (min == NULL_TREE
2158 || !is_gimple_min_invariant (min)
2159 || (TREE_OVERFLOW (min) && !is_overflow_infinity (min))
2160 || max == NULL_TREE
2161 || !is_gimple_min_invariant (max)
2162 || (TREE_OVERFLOW (max) && !is_overflow_infinity (max)))
2164 set_value_range_to_varying (vr);
2165 return;
2168 /* We punt if:
2169 1) [-INF, +INF]
2170 2) [-INF, +-INF(OVF)]
2171 3) [+-INF(OVF), +INF]
2172 4) [+-INF(OVF), +-INF(OVF)]
2173 We learn nothing when we have INF and INF(OVF) on both sides.
2174 Note that we do accept [-INF, -INF] and [+INF, +INF] without
2175 overflow. */
2176 if ((vrp_val_is_min (min) || is_overflow_infinity (min))
2177 && (vrp_val_is_max (max) || is_overflow_infinity (max)))
2179 set_value_range_to_varying (vr);
2180 return;
2183 cmp = compare_values (min, max);
2184 if (cmp == -2 || cmp == 1)
2186 /* If the new range has its limits swapped around (MIN > MAX),
2187 then the operation caused one of them to wrap around, mark
2188 the new range VARYING. */
2189 set_value_range_to_varying (vr);
2191 else
2192 set_value_range (vr, type, min, max, NULL);
2195 /* Extract range information from a binary operation CODE based on
2196 the ranges of each of its operands *VR0 and *VR1 with resulting
2197 type EXPR_TYPE. The resulting range is stored in *VR. */
2199 static void
2200 extract_range_from_binary_expr_1 (value_range *vr,
2201 enum tree_code code, tree expr_type,
2202 value_range *vr0_, value_range *vr1_)
2204 value_range vr0 = *vr0_, vr1 = *vr1_;
2205 value_range vrtem0 = VR_INITIALIZER, vrtem1 = VR_INITIALIZER;
2206 enum value_range_type type;
2207 tree min = NULL_TREE, max = NULL_TREE;
2208 int cmp;
2210 if (!INTEGRAL_TYPE_P (expr_type)
2211 && !POINTER_TYPE_P (expr_type))
2213 set_value_range_to_varying (vr);
2214 return;
2217 /* Not all binary expressions can be applied to ranges in a
2218 meaningful way. Handle only arithmetic operations. */
2219 if (code != PLUS_EXPR
2220 && code != MINUS_EXPR
2221 && code != POINTER_PLUS_EXPR
2222 && code != MULT_EXPR
2223 && code != TRUNC_DIV_EXPR
2224 && code != FLOOR_DIV_EXPR
2225 && code != CEIL_DIV_EXPR
2226 && code != EXACT_DIV_EXPR
2227 && code != ROUND_DIV_EXPR
2228 && code != TRUNC_MOD_EXPR
2229 && code != RSHIFT_EXPR
2230 && code != LSHIFT_EXPR
2231 && code != MIN_EXPR
2232 && code != MAX_EXPR
2233 && code != BIT_AND_EXPR
2234 && code != BIT_IOR_EXPR
2235 && code != BIT_XOR_EXPR)
2237 set_value_range_to_varying (vr);
2238 return;
2241 /* If both ranges are UNDEFINED, so is the result. */
2242 if (vr0.type == VR_UNDEFINED && vr1.type == VR_UNDEFINED)
2244 set_value_range_to_undefined (vr);
2245 return;
2247 /* If one of the ranges is UNDEFINED drop it to VARYING for the following
2248 code. At some point we may want to special-case operations that
2249 have UNDEFINED result for all or some value-ranges of the not UNDEFINED
2250 operand. */
2251 else if (vr0.type == VR_UNDEFINED)
2252 set_value_range_to_varying (&vr0);
2253 else if (vr1.type == VR_UNDEFINED)
2254 set_value_range_to_varying (&vr1);
2256 /* Now canonicalize anti-ranges to ranges when they are not symbolic
2257 and express ~[] op X as ([]' op X) U ([]'' op X). */
2258 if (vr0.type == VR_ANTI_RANGE
2259 && ranges_from_anti_range (&vr0, &vrtem0, &vrtem1))
2261 extract_range_from_binary_expr_1 (vr, code, expr_type, &vrtem0, vr1_);
2262 if (vrtem1.type != VR_UNDEFINED)
2264 value_range vrres = VR_INITIALIZER;
2265 extract_range_from_binary_expr_1 (&vrres, code, expr_type,
2266 &vrtem1, vr1_);
2267 vrp_meet (vr, &vrres);
2269 return;
2271 /* Likewise for X op ~[]. */
2272 if (vr1.type == VR_ANTI_RANGE
2273 && ranges_from_anti_range (&vr1, &vrtem0, &vrtem1))
2275 extract_range_from_binary_expr_1 (vr, code, expr_type, vr0_, &vrtem0);
2276 if (vrtem1.type != VR_UNDEFINED)
2278 value_range vrres = VR_INITIALIZER;
2279 extract_range_from_binary_expr_1 (&vrres, code, expr_type,
2280 vr0_, &vrtem1);
2281 vrp_meet (vr, &vrres);
2283 return;
2286 /* The type of the resulting value range defaults to VR0.TYPE. */
2287 type = vr0.type;
2289 /* Refuse to operate on VARYING ranges, ranges of different kinds
2290 and symbolic ranges. As an exception, we allow BIT_{AND,IOR}
2291 because we may be able to derive a useful range even if one of
2292 the operands is VR_VARYING or symbolic range. Similarly for
2293 divisions, MIN/MAX and PLUS/MINUS.
2295 TODO, we may be able to derive anti-ranges in some cases. */
2296 if (code != BIT_AND_EXPR
2297 && code != BIT_IOR_EXPR
2298 && code != TRUNC_DIV_EXPR
2299 && code != FLOOR_DIV_EXPR
2300 && code != CEIL_DIV_EXPR
2301 && code != EXACT_DIV_EXPR
2302 && code != ROUND_DIV_EXPR
2303 && code != TRUNC_MOD_EXPR
2304 && code != MIN_EXPR
2305 && code != MAX_EXPR
2306 && code != PLUS_EXPR
2307 && code != MINUS_EXPR
2308 && code != RSHIFT_EXPR
2309 && (vr0.type == VR_VARYING
2310 || vr1.type == VR_VARYING
2311 || vr0.type != vr1.type
2312 || symbolic_range_p (&vr0)
2313 || symbolic_range_p (&vr1)))
2315 set_value_range_to_varying (vr);
2316 return;
2319 /* Now evaluate the expression to determine the new range. */
2320 if (POINTER_TYPE_P (expr_type))
2322 if (code == MIN_EXPR || code == MAX_EXPR)
2324 /* For MIN/MAX expressions with pointers, we only care about
2325 nullness, if both are non null, then the result is nonnull.
2326 If both are null, then the result is null. Otherwise they
2327 are varying. */
2328 if (range_is_nonnull (&vr0) && range_is_nonnull (&vr1))
2329 set_value_range_to_nonnull (vr, expr_type);
2330 else if (range_is_null (&vr0) && range_is_null (&vr1))
2331 set_value_range_to_null (vr, expr_type);
2332 else
2333 set_value_range_to_varying (vr);
2335 else if (code == POINTER_PLUS_EXPR)
2337 /* For pointer types, we are really only interested in asserting
2338 whether the expression evaluates to non-NULL. */
2339 if (range_is_nonnull (&vr0) || range_is_nonnull (&vr1))
2340 set_value_range_to_nonnull (vr, expr_type);
2341 else if (range_is_null (&vr0) && range_is_null (&vr1))
2342 set_value_range_to_null (vr, expr_type);
2343 else
2344 set_value_range_to_varying (vr);
2346 else if (code == BIT_AND_EXPR)
2348 /* For pointer types, we are really only interested in asserting
2349 whether the expression evaluates to non-NULL. */
2350 if (range_is_nonnull (&vr0) && range_is_nonnull (&vr1))
2351 set_value_range_to_nonnull (vr, expr_type);
2352 else if (range_is_null (&vr0) || range_is_null (&vr1))
2353 set_value_range_to_null (vr, expr_type);
2354 else
2355 set_value_range_to_varying (vr);
2357 else
2358 set_value_range_to_varying (vr);
2360 return;
2363 /* For integer ranges, apply the operation to each end of the
2364 range and see what we end up with. */
2365 if (code == PLUS_EXPR || code == MINUS_EXPR)
2367 const bool minus_p = (code == MINUS_EXPR);
2368 tree min_op0 = vr0.min;
2369 tree min_op1 = minus_p ? vr1.max : vr1.min;
2370 tree max_op0 = vr0.max;
2371 tree max_op1 = minus_p ? vr1.min : vr1.max;
2372 tree sym_min_op0 = NULL_TREE;
2373 tree sym_min_op1 = NULL_TREE;
2374 tree sym_max_op0 = NULL_TREE;
2375 tree sym_max_op1 = NULL_TREE;
2376 bool neg_min_op0, neg_min_op1, neg_max_op0, neg_max_op1;
2378 /* If we have a PLUS or MINUS with two VR_RANGEs, either constant or
2379 single-symbolic ranges, try to compute the precise resulting range,
2380 but only if we know that this resulting range will also be constant
2381 or single-symbolic. */
2382 if (vr0.type == VR_RANGE && vr1.type == VR_RANGE
2383 && (TREE_CODE (min_op0) == INTEGER_CST
2384 || (sym_min_op0
2385 = get_single_symbol (min_op0, &neg_min_op0, &min_op0)))
2386 && (TREE_CODE (min_op1) == INTEGER_CST
2387 || (sym_min_op1
2388 = get_single_symbol (min_op1, &neg_min_op1, &min_op1)))
2389 && (!(sym_min_op0 && sym_min_op1)
2390 || (sym_min_op0 == sym_min_op1
2391 && neg_min_op0 == (minus_p ? neg_min_op1 : !neg_min_op1)))
2392 && (TREE_CODE (max_op0) == INTEGER_CST
2393 || (sym_max_op0
2394 = get_single_symbol (max_op0, &neg_max_op0, &max_op0)))
2395 && (TREE_CODE (max_op1) == INTEGER_CST
2396 || (sym_max_op1
2397 = get_single_symbol (max_op1, &neg_max_op1, &max_op1)))
2398 && (!(sym_max_op0 && sym_max_op1)
2399 || (sym_max_op0 == sym_max_op1
2400 && neg_max_op0 == (minus_p ? neg_max_op1 : !neg_max_op1))))
2402 const signop sgn = TYPE_SIGN (expr_type);
2403 const unsigned int prec = TYPE_PRECISION (expr_type);
2404 wide_int type_min, type_max, wmin, wmax;
2405 int min_ovf = 0;
2406 int max_ovf = 0;
2408 /* Get the lower and upper bounds of the type. */
2409 if (TYPE_OVERFLOW_WRAPS (expr_type))
2411 type_min = wi::min_value (prec, sgn);
2412 type_max = wi::max_value (prec, sgn);
2414 else
2416 type_min = vrp_val_min (expr_type);
2417 type_max = vrp_val_max (expr_type);
2420 /* Combine the lower bounds, if any. */
2421 if (min_op0 && min_op1)
2423 if (minus_p)
2425 wmin = wi::sub (min_op0, min_op1);
2427 /* Check for overflow. */
2428 if (wi::cmp (0, min_op1, sgn)
2429 != wi::cmp (wmin, min_op0, sgn))
2430 min_ovf = wi::cmp (min_op0, min_op1, sgn);
2432 else
2434 wmin = wi::add (min_op0, min_op1);
2436 /* Check for overflow. */
2437 if (wi::cmp (min_op1, 0, sgn)
2438 != wi::cmp (wmin, min_op0, sgn))
2439 min_ovf = wi::cmp (min_op0, wmin, sgn);
2442 else if (min_op0)
2443 wmin = min_op0;
2444 else if (min_op1)
2445 wmin = minus_p ? wi::neg (min_op1) : min_op1;
2446 else
2447 wmin = wi::shwi (0, prec);
2449 /* Combine the upper bounds, if any. */
2450 if (max_op0 && max_op1)
2452 if (minus_p)
2454 wmax = wi::sub (max_op0, max_op1);
2456 /* Check for overflow. */
2457 if (wi::cmp (0, max_op1, sgn)
2458 != wi::cmp (wmax, max_op0, sgn))
2459 max_ovf = wi::cmp (max_op0, max_op1, sgn);
2461 else
2463 wmax = wi::add (max_op0, max_op1);
2465 if (wi::cmp (max_op1, 0, sgn)
2466 != wi::cmp (wmax, max_op0, sgn))
2467 max_ovf = wi::cmp (max_op0, wmax, sgn);
2470 else if (max_op0)
2471 wmax = max_op0;
2472 else if (max_op1)
2473 wmax = minus_p ? wi::neg (max_op1) : max_op1;
2474 else
2475 wmax = wi::shwi (0, prec);
2477 /* Check for type overflow. */
2478 if (min_ovf == 0)
2480 if (wi::cmp (wmin, type_min, sgn) == -1)
2481 min_ovf = -1;
2482 else if (wi::cmp (wmin, type_max, sgn) == 1)
2483 min_ovf = 1;
2485 if (max_ovf == 0)
2487 if (wi::cmp (wmax, type_min, sgn) == -1)
2488 max_ovf = -1;
2489 else if (wi::cmp (wmax, type_max, sgn) == 1)
2490 max_ovf = 1;
2493 /* If we have overflow for the constant part and the resulting
2494 range will be symbolic, drop to VR_VARYING. */
2495 if ((min_ovf && sym_min_op0 != sym_min_op1)
2496 || (max_ovf && sym_max_op0 != sym_max_op1))
2498 set_value_range_to_varying (vr);
2499 return;
2502 if (TYPE_OVERFLOW_WRAPS (expr_type))
2504 /* If overflow wraps, truncate the values and adjust the
2505 range kind and bounds appropriately. */
2506 wide_int tmin = wide_int::from (wmin, prec, sgn);
2507 wide_int tmax = wide_int::from (wmax, prec, sgn);
2508 if (min_ovf == max_ovf)
2510 /* No overflow or both overflow or underflow. The
2511 range kind stays VR_RANGE. */
2512 min = wide_int_to_tree (expr_type, tmin);
2513 max = wide_int_to_tree (expr_type, tmax);
2515 else if ((min_ovf == -1 && max_ovf == 0)
2516 || (max_ovf == 1 && min_ovf == 0))
2518 /* Min underflow or max overflow. The range kind
2519 changes to VR_ANTI_RANGE. */
2520 bool covers = false;
2521 wide_int tem = tmin;
2522 type = VR_ANTI_RANGE;
2523 tmin = tmax + 1;
2524 if (wi::cmp (tmin, tmax, sgn) < 0)
2525 covers = true;
2526 tmax = tem - 1;
2527 if (wi::cmp (tmax, tem, sgn) > 0)
2528 covers = true;
2529 /* If the anti-range would cover nothing, drop to varying.
2530 Likewise if the anti-range bounds are outside of the
2531 types values. */
2532 if (covers || wi::cmp (tmin, tmax, sgn) > 0)
2534 set_value_range_to_varying (vr);
2535 return;
2537 min = wide_int_to_tree (expr_type, tmin);
2538 max = wide_int_to_tree (expr_type, tmax);
2540 else
2542 /* Other underflow and/or overflow, drop to VR_VARYING. */
2543 set_value_range_to_varying (vr);
2544 return;
2547 else
2549 /* If overflow does not wrap, saturate to the types min/max
2550 value. */
2551 if (min_ovf == -1)
2553 if (needs_overflow_infinity (expr_type)
2554 && supports_overflow_infinity (expr_type))
2555 min = negative_overflow_infinity (expr_type);
2556 else
2557 min = wide_int_to_tree (expr_type, type_min);
2559 else if (min_ovf == 1)
2561 if (needs_overflow_infinity (expr_type)
2562 && supports_overflow_infinity (expr_type))
2563 min = positive_overflow_infinity (expr_type);
2564 else
2565 min = wide_int_to_tree (expr_type, type_max);
2567 else
2568 min = wide_int_to_tree (expr_type, wmin);
2570 if (max_ovf == -1)
2572 if (needs_overflow_infinity (expr_type)
2573 && supports_overflow_infinity (expr_type))
2574 max = negative_overflow_infinity (expr_type);
2575 else
2576 max = wide_int_to_tree (expr_type, type_min);
2578 else if (max_ovf == 1)
2580 if (needs_overflow_infinity (expr_type)
2581 && supports_overflow_infinity (expr_type))
2582 max = positive_overflow_infinity (expr_type);
2583 else
2584 max = wide_int_to_tree (expr_type, type_max);
2586 else
2587 max = wide_int_to_tree (expr_type, wmax);
2590 if (needs_overflow_infinity (expr_type)
2591 && supports_overflow_infinity (expr_type))
2593 if ((min_op0 && is_negative_overflow_infinity (min_op0))
2594 || (min_op1
2595 && (minus_p
2596 ? is_positive_overflow_infinity (min_op1)
2597 : is_negative_overflow_infinity (min_op1))))
2598 min = negative_overflow_infinity (expr_type);
2599 if ((max_op0 && is_positive_overflow_infinity (max_op0))
2600 || (max_op1
2601 && (minus_p
2602 ? is_negative_overflow_infinity (max_op1)
2603 : is_positive_overflow_infinity (max_op1))))
2604 max = positive_overflow_infinity (expr_type);
2607 /* If the result lower bound is constant, we're done;
2608 otherwise, build the symbolic lower bound. */
2609 if (sym_min_op0 == sym_min_op1)
2611 else if (sym_min_op0)
2612 min = build_symbolic_expr (expr_type, sym_min_op0,
2613 neg_min_op0, min);
2614 else if (sym_min_op1)
2615 min = build_symbolic_expr (expr_type, sym_min_op1,
2616 neg_min_op1 ^ minus_p, min);
2618 /* Likewise for the upper bound. */
2619 if (sym_max_op0 == sym_max_op1)
2621 else if (sym_max_op0)
2622 max = build_symbolic_expr (expr_type, sym_max_op0,
2623 neg_max_op0, max);
2624 else if (sym_max_op1)
2625 max = build_symbolic_expr (expr_type, sym_max_op1,
2626 neg_max_op1 ^ minus_p, max);
2628 else
2630 /* For other cases, for example if we have a PLUS_EXPR with two
2631 VR_ANTI_RANGEs, drop to VR_VARYING. It would take more effort
2632 to compute a precise range for such a case.
2633 ??? General even mixed range kind operations can be expressed
2634 by for example transforming ~[3, 5] + [1, 2] to range-only
2635 operations and a union primitive:
2636 [-INF, 2] + [1, 2] U [5, +INF] + [1, 2]
2637 [-INF+1, 4] U [6, +INF(OVF)]
2638 though usually the union is not exactly representable with
2639 a single range or anti-range as the above is
2640 [-INF+1, +INF(OVF)] intersected with ~[5, 5]
2641 but one could use a scheme similar to equivalences for this. */
2642 set_value_range_to_varying (vr);
2643 return;
2646 else if (code == MIN_EXPR
2647 || code == MAX_EXPR)
2649 if (vr0.type == VR_RANGE
2650 && !symbolic_range_p (&vr0))
2652 type = VR_RANGE;
2653 if (vr1.type == VR_RANGE
2654 && !symbolic_range_p (&vr1))
2656 /* For operations that make the resulting range directly
2657 proportional to the original ranges, apply the operation to
2658 the same end of each range. */
2659 min = vrp_int_const_binop (code, vr0.min, vr1.min);
2660 max = vrp_int_const_binop (code, vr0.max, vr1.max);
2662 else if (code == MIN_EXPR)
2664 min = vrp_val_min (expr_type);
2665 max = vr0.max;
2667 else if (code == MAX_EXPR)
2669 min = vr0.min;
2670 max = vrp_val_max (expr_type);
2673 else if (vr1.type == VR_RANGE
2674 && !symbolic_range_p (&vr1))
2676 type = VR_RANGE;
2677 if (code == MIN_EXPR)
2679 min = vrp_val_min (expr_type);
2680 max = vr1.max;
2682 else if (code == MAX_EXPR)
2684 min = vr1.min;
2685 max = vrp_val_max (expr_type);
2688 else
2690 set_value_range_to_varying (vr);
2691 return;
2694 else if (code == MULT_EXPR)
2696 /* Fancy code so that with unsigned, [-3,-1]*[-3,-1] does not
2697 drop to varying. This test requires 2*prec bits if both
2698 operands are signed and 2*prec + 2 bits if either is not. */
2700 signop sign = TYPE_SIGN (expr_type);
2701 unsigned int prec = TYPE_PRECISION (expr_type);
2703 if (range_int_cst_p (&vr0)
2704 && range_int_cst_p (&vr1)
2705 && TYPE_OVERFLOW_WRAPS (expr_type))
2707 typedef FIXED_WIDE_INT (WIDE_INT_MAX_PRECISION * 2) vrp_int;
2708 typedef generic_wide_int
2709 <wi::extended_tree <WIDE_INT_MAX_PRECISION * 2> > vrp_int_cst;
2710 vrp_int sizem1 = wi::mask <vrp_int> (prec, false);
2711 vrp_int size = sizem1 + 1;
2713 /* Extend the values using the sign of the result to PREC2.
2714 From here on out, everthing is just signed math no matter
2715 what the input types were. */
2716 vrp_int min0 = vrp_int_cst (vr0.min);
2717 vrp_int max0 = vrp_int_cst (vr0.max);
2718 vrp_int min1 = vrp_int_cst (vr1.min);
2719 vrp_int max1 = vrp_int_cst (vr1.max);
2720 /* Canonicalize the intervals. */
2721 if (sign == UNSIGNED)
2723 if (wi::ltu_p (size, min0 + max0))
2725 min0 -= size;
2726 max0 -= size;
2729 if (wi::ltu_p (size, min1 + max1))
2731 min1 -= size;
2732 max1 -= size;
2736 vrp_int prod0 = min0 * min1;
2737 vrp_int prod1 = min0 * max1;
2738 vrp_int prod2 = max0 * min1;
2739 vrp_int prod3 = max0 * max1;
2741 /* Sort the 4 products so that min is in prod0 and max is in
2742 prod3. */
2743 /* min0min1 > max0max1 */
2744 if (prod0 > prod3)
2745 std::swap (prod0, prod3);
2747 /* min0max1 > max0min1 */
2748 if (prod1 > prod2)
2749 std::swap (prod1, prod2);
2751 if (prod0 > prod1)
2752 std::swap (prod0, prod1);
2754 if (prod2 > prod3)
2755 std::swap (prod2, prod3);
2757 /* diff = max - min. */
2758 prod2 = prod3 - prod0;
2759 if (wi::geu_p (prod2, sizem1))
2761 /* the range covers all values. */
2762 set_value_range_to_varying (vr);
2763 return;
2766 /* The following should handle the wrapping and selecting
2767 VR_ANTI_RANGE for us. */
2768 min = wide_int_to_tree (expr_type, prod0);
2769 max = wide_int_to_tree (expr_type, prod3);
2770 set_and_canonicalize_value_range (vr, VR_RANGE, min, max, NULL);
2771 return;
2774 /* If we have an unsigned MULT_EXPR with two VR_ANTI_RANGEs,
2775 drop to VR_VARYING. It would take more effort to compute a
2776 precise range for such a case. For example, if we have
2777 op0 == 65536 and op1 == 65536 with their ranges both being
2778 ~[0,0] on a 32-bit machine, we would have op0 * op1 == 0, so
2779 we cannot claim that the product is in ~[0,0]. Note that we
2780 are guaranteed to have vr0.type == vr1.type at this
2781 point. */
2782 if (vr0.type == VR_ANTI_RANGE
2783 && !TYPE_OVERFLOW_UNDEFINED (expr_type))
2785 set_value_range_to_varying (vr);
2786 return;
2789 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
2790 return;
2792 else if (code == RSHIFT_EXPR
2793 || code == LSHIFT_EXPR)
2795 /* If we have a RSHIFT_EXPR with any shift values outside [0..prec-1],
2796 then drop to VR_VARYING. Outside of this range we get undefined
2797 behavior from the shift operation. We cannot even trust
2798 SHIFT_COUNT_TRUNCATED at this stage, because that applies to rtl
2799 shifts, and the operation at the tree level may be widened. */
2800 if (range_int_cst_p (&vr1)
2801 && compare_tree_int (vr1.min, 0) >= 0
2802 && compare_tree_int (vr1.max, TYPE_PRECISION (expr_type)) == -1)
2804 if (code == RSHIFT_EXPR)
2806 /* Even if vr0 is VARYING or otherwise not usable, we can derive
2807 useful ranges just from the shift count. E.g.
2808 x >> 63 for signed 64-bit x is always [-1, 0]. */
2809 if (vr0.type != VR_RANGE || symbolic_range_p (&vr0))
2811 vr0.type = type = VR_RANGE;
2812 vr0.min = vrp_val_min (expr_type);
2813 vr0.max = vrp_val_max (expr_type);
2815 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
2816 return;
2818 /* We can map lshifts by constants to MULT_EXPR handling. */
2819 else if (code == LSHIFT_EXPR
2820 && range_int_cst_singleton_p (&vr1))
2822 bool saved_flag_wrapv;
2823 value_range vr1p = VR_INITIALIZER;
2824 vr1p.type = VR_RANGE;
2825 vr1p.min = (wide_int_to_tree
2826 (expr_type,
2827 wi::set_bit_in_zero (tree_to_shwi (vr1.min),
2828 TYPE_PRECISION (expr_type))));
2829 vr1p.max = vr1p.min;
2830 /* We have to use a wrapping multiply though as signed overflow
2831 on lshifts is implementation defined in C89. */
2832 saved_flag_wrapv = flag_wrapv;
2833 flag_wrapv = 1;
2834 extract_range_from_binary_expr_1 (vr, MULT_EXPR, expr_type,
2835 &vr0, &vr1p);
2836 flag_wrapv = saved_flag_wrapv;
2837 return;
2839 else if (code == LSHIFT_EXPR
2840 && range_int_cst_p (&vr0))
2842 int prec = TYPE_PRECISION (expr_type);
2843 int overflow_pos = prec;
2844 int bound_shift;
2845 wide_int low_bound, high_bound;
2846 bool uns = TYPE_UNSIGNED (expr_type);
2847 bool in_bounds = false;
2849 if (!uns)
2850 overflow_pos -= 1;
2852 bound_shift = overflow_pos - tree_to_shwi (vr1.max);
2853 /* If bound_shift == HOST_BITS_PER_WIDE_INT, the llshift can
2854 overflow. However, for that to happen, vr1.max needs to be
2855 zero, which means vr1 is a singleton range of zero, which
2856 means it should be handled by the previous LSHIFT_EXPR
2857 if-clause. */
2858 wide_int bound = wi::set_bit_in_zero (bound_shift, prec);
2859 wide_int complement = ~(bound - 1);
2861 if (uns)
2863 low_bound = bound;
2864 high_bound = complement;
2865 if (wi::ltu_p (vr0.max, low_bound))
2867 /* [5, 6] << [1, 2] == [10, 24]. */
2868 /* We're shifting out only zeroes, the value increases
2869 monotonically. */
2870 in_bounds = true;
2872 else if (wi::ltu_p (high_bound, vr0.min))
2874 /* [0xffffff00, 0xffffffff] << [1, 2]
2875 == [0xfffffc00, 0xfffffffe]. */
2876 /* We're shifting out only ones, the value decreases
2877 monotonically. */
2878 in_bounds = true;
2881 else
2883 /* [-1, 1] << [1, 2] == [-4, 4]. */
2884 low_bound = complement;
2885 high_bound = bound;
2886 if (wi::lts_p (vr0.max, high_bound)
2887 && wi::lts_p (low_bound, vr0.min))
2889 /* For non-negative numbers, we're shifting out only
2890 zeroes, the value increases monotonically.
2891 For negative numbers, we're shifting out only ones, the
2892 value decreases monotomically. */
2893 in_bounds = true;
2897 if (in_bounds)
2899 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
2900 return;
2904 set_value_range_to_varying (vr);
2905 return;
2907 else if (code == TRUNC_DIV_EXPR
2908 || code == FLOOR_DIV_EXPR
2909 || code == CEIL_DIV_EXPR
2910 || code == EXACT_DIV_EXPR
2911 || code == ROUND_DIV_EXPR)
2913 if (vr0.type != VR_RANGE || symbolic_range_p (&vr0))
2915 /* For division, if op1 has VR_RANGE but op0 does not, something
2916 can be deduced just from that range. Say [min, max] / [4, max]
2917 gives [min / 4, max / 4] range. */
2918 if (vr1.type == VR_RANGE
2919 && !symbolic_range_p (&vr1)
2920 && range_includes_zero_p (vr1.min, vr1.max) == 0)
2922 vr0.type = type = VR_RANGE;
2923 vr0.min = vrp_val_min (expr_type);
2924 vr0.max = vrp_val_max (expr_type);
2926 else
2928 set_value_range_to_varying (vr);
2929 return;
2933 /* For divisions, if flag_non_call_exceptions is true, we must
2934 not eliminate a division by zero. */
2935 if (cfun->can_throw_non_call_exceptions
2936 && (vr1.type != VR_RANGE
2937 || range_includes_zero_p (vr1.min, vr1.max) != 0))
2939 set_value_range_to_varying (vr);
2940 return;
2943 /* For divisions, if op0 is VR_RANGE, we can deduce a range
2944 even if op1 is VR_VARYING, VR_ANTI_RANGE, symbolic or can
2945 include 0. */
2946 if (vr0.type == VR_RANGE
2947 && (vr1.type != VR_RANGE
2948 || range_includes_zero_p (vr1.min, vr1.max) != 0))
2950 tree zero = build_int_cst (TREE_TYPE (vr0.min), 0);
2951 int cmp;
2953 min = NULL_TREE;
2954 max = NULL_TREE;
2955 if (TYPE_UNSIGNED (expr_type)
2956 || value_range_nonnegative_p (&vr1))
2958 /* For unsigned division or when divisor is known
2959 to be non-negative, the range has to cover
2960 all numbers from 0 to max for positive max
2961 and all numbers from min to 0 for negative min. */
2962 cmp = compare_values (vr0.max, zero);
2963 if (cmp == -1)
2965 /* When vr0.max < 0, vr1.min != 0 and value
2966 ranges for dividend and divisor are available. */
2967 if (vr1.type == VR_RANGE
2968 && !symbolic_range_p (&vr0)
2969 && !symbolic_range_p (&vr1)
2970 && compare_values (vr1.min, zero) != 0)
2971 max = int_const_binop (code, vr0.max, vr1.min);
2972 else
2973 max = zero;
2975 else if (cmp == 0 || cmp == 1)
2976 max = vr0.max;
2977 else
2978 type = VR_VARYING;
2979 cmp = compare_values (vr0.min, zero);
2980 if (cmp == 1)
2982 /* For unsigned division when value ranges for dividend
2983 and divisor are available. */
2984 if (vr1.type == VR_RANGE
2985 && !symbolic_range_p (&vr0)
2986 && !symbolic_range_p (&vr1)
2987 && compare_values (vr1.max, zero) != 0)
2988 min = int_const_binop (code, vr0.min, vr1.max);
2989 else
2990 min = zero;
2992 else if (cmp == 0 || cmp == -1)
2993 min = vr0.min;
2994 else
2995 type = VR_VARYING;
2997 else
2999 /* Otherwise the range is -max .. max or min .. -min
3000 depending on which bound is bigger in absolute value,
3001 as the division can change the sign. */
3002 abs_extent_range (vr, vr0.min, vr0.max);
3003 return;
3005 if (type == VR_VARYING)
3007 set_value_range_to_varying (vr);
3008 return;
3011 else if (!symbolic_range_p (&vr0) && !symbolic_range_p (&vr1))
3013 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
3014 return;
3017 else if (code == TRUNC_MOD_EXPR)
3019 if (range_is_null (&vr1))
3021 set_value_range_to_undefined (vr);
3022 return;
3024 /* ABS (A % B) < ABS (B) and either
3025 0 <= A % B <= A or A <= A % B <= 0. */
3026 type = VR_RANGE;
3027 signop sgn = TYPE_SIGN (expr_type);
3028 unsigned int prec = TYPE_PRECISION (expr_type);
3029 wide_int wmin, wmax, tmp;
3030 wide_int zero = wi::zero (prec);
3031 wide_int one = wi::one (prec);
3032 if (vr1.type == VR_RANGE && !symbolic_range_p (&vr1))
3034 wmax = wi::sub (vr1.max, one);
3035 if (sgn == SIGNED)
3037 tmp = wi::sub (wi::minus_one (prec), vr1.min);
3038 wmax = wi::smax (wmax, tmp);
3041 else
3043 wmax = wi::max_value (prec, sgn);
3044 /* X % INT_MIN may be INT_MAX. */
3045 if (sgn == UNSIGNED)
3046 wmax = wmax - one;
3049 if (sgn == UNSIGNED)
3050 wmin = zero;
3051 else
3053 wmin = -wmax;
3054 if (vr0.type == VR_RANGE && TREE_CODE (vr0.min) == INTEGER_CST)
3056 tmp = vr0.min;
3057 if (wi::gts_p (tmp, zero))
3058 tmp = zero;
3059 wmin = wi::smax (wmin, tmp);
3063 if (vr0.type == VR_RANGE && TREE_CODE (vr0.max) == INTEGER_CST)
3065 tmp = vr0.max;
3066 if (sgn == SIGNED && wi::neg_p (tmp))
3067 tmp = zero;
3068 wmax = wi::min (wmax, tmp, sgn);
3071 min = wide_int_to_tree (expr_type, wmin);
3072 max = wide_int_to_tree (expr_type, wmax);
3074 else if (code == BIT_AND_EXPR || code == BIT_IOR_EXPR || code == BIT_XOR_EXPR)
3076 bool int_cst_range0, int_cst_range1;
3077 wide_int may_be_nonzero0, may_be_nonzero1;
3078 wide_int must_be_nonzero0, must_be_nonzero1;
3080 int_cst_range0 = zero_nonzero_bits_from_vr (expr_type, &vr0,
3081 &may_be_nonzero0,
3082 &must_be_nonzero0);
3083 int_cst_range1 = zero_nonzero_bits_from_vr (expr_type, &vr1,
3084 &may_be_nonzero1,
3085 &must_be_nonzero1);
3087 type = VR_RANGE;
3088 if (code == BIT_AND_EXPR)
3090 min = wide_int_to_tree (expr_type,
3091 must_be_nonzero0 & must_be_nonzero1);
3092 wide_int wmax = may_be_nonzero0 & may_be_nonzero1;
3093 /* If both input ranges contain only negative values we can
3094 truncate the result range maximum to the minimum of the
3095 input range maxima. */
3096 if (int_cst_range0 && int_cst_range1
3097 && tree_int_cst_sgn (vr0.max) < 0
3098 && tree_int_cst_sgn (vr1.max) < 0)
3100 wmax = wi::min (wmax, vr0.max, TYPE_SIGN (expr_type));
3101 wmax = wi::min (wmax, vr1.max, TYPE_SIGN (expr_type));
3103 /* If either input range contains only non-negative values
3104 we can truncate the result range maximum to the respective
3105 maximum of the input range. */
3106 if (int_cst_range0 && tree_int_cst_sgn (vr0.min) >= 0)
3107 wmax = wi::min (wmax, vr0.max, TYPE_SIGN (expr_type));
3108 if (int_cst_range1 && tree_int_cst_sgn (vr1.min) >= 0)
3109 wmax = wi::min (wmax, vr1.max, TYPE_SIGN (expr_type));
3110 max = wide_int_to_tree (expr_type, wmax);
3111 cmp = compare_values (min, max);
3112 /* PR68217: In case of signed & sign-bit-CST should
3113 result in [-INF, 0] instead of [-INF, INF]. */
3114 if (cmp == -2 || cmp == 1)
3116 wide_int sign_bit
3117 = wi::set_bit_in_zero (TYPE_PRECISION (expr_type) - 1,
3118 TYPE_PRECISION (expr_type));
3119 if (!TYPE_UNSIGNED (expr_type)
3120 && ((value_range_constant_singleton (&vr0)
3121 && !wi::cmps (vr0.min, sign_bit))
3122 || (value_range_constant_singleton (&vr1)
3123 && !wi::cmps (vr1.min, sign_bit))))
3125 min = TYPE_MIN_VALUE (expr_type);
3126 max = build_int_cst (expr_type, 0);
3130 else if (code == BIT_IOR_EXPR)
3132 max = wide_int_to_tree (expr_type,
3133 may_be_nonzero0 | may_be_nonzero1);
3134 wide_int wmin = must_be_nonzero0 | must_be_nonzero1;
3135 /* If the input ranges contain only positive values we can
3136 truncate the minimum of the result range to the maximum
3137 of the input range minima. */
3138 if (int_cst_range0 && int_cst_range1
3139 && tree_int_cst_sgn (vr0.min) >= 0
3140 && tree_int_cst_sgn (vr1.min) >= 0)
3142 wmin = wi::max (wmin, vr0.min, TYPE_SIGN (expr_type));
3143 wmin = wi::max (wmin, vr1.min, TYPE_SIGN (expr_type));
3145 /* If either input range contains only negative values
3146 we can truncate the minimum of the result range to the
3147 respective minimum range. */
3148 if (int_cst_range0 && tree_int_cst_sgn (vr0.max) < 0)
3149 wmin = wi::max (wmin, vr0.min, TYPE_SIGN (expr_type));
3150 if (int_cst_range1 && tree_int_cst_sgn (vr1.max) < 0)
3151 wmin = wi::max (wmin, vr1.min, TYPE_SIGN (expr_type));
3152 min = wide_int_to_tree (expr_type, wmin);
3154 else if (code == BIT_XOR_EXPR)
3156 wide_int result_zero_bits = ((must_be_nonzero0 & must_be_nonzero1)
3157 | ~(may_be_nonzero0 | may_be_nonzero1));
3158 wide_int result_one_bits
3159 = (must_be_nonzero0.and_not (may_be_nonzero1)
3160 | must_be_nonzero1.and_not (may_be_nonzero0));
3161 max = wide_int_to_tree (expr_type, ~result_zero_bits);
3162 min = wide_int_to_tree (expr_type, result_one_bits);
3163 /* If the range has all positive or all negative values the
3164 result is better than VARYING. */
3165 if (tree_int_cst_sgn (min) < 0
3166 || tree_int_cst_sgn (max) >= 0)
3168 else
3169 max = min = NULL_TREE;
3172 else
3173 gcc_unreachable ();
3175 /* If either MIN or MAX overflowed, then set the resulting range to
3176 VARYING. But we do accept an overflow infinity representation. */
3177 if (min == NULL_TREE
3178 || (TREE_OVERFLOW_P (min) && !is_overflow_infinity (min))
3179 || max == NULL_TREE
3180 || (TREE_OVERFLOW_P (max) && !is_overflow_infinity (max)))
3182 set_value_range_to_varying (vr);
3183 return;
3186 /* We punt if:
3187 1) [-INF, +INF]
3188 2) [-INF, +-INF(OVF)]
3189 3) [+-INF(OVF), +INF]
3190 4) [+-INF(OVF), +-INF(OVF)]
3191 We learn nothing when we have INF and INF(OVF) on both sides.
3192 Note that we do accept [-INF, -INF] and [+INF, +INF] without
3193 overflow. */
3194 if ((vrp_val_is_min (min) || is_overflow_infinity (min))
3195 && (vrp_val_is_max (max) || is_overflow_infinity (max)))
3197 set_value_range_to_varying (vr);
3198 return;
3201 cmp = compare_values (min, max);
3202 if (cmp == -2 || cmp == 1)
3204 /* If the new range has its limits swapped around (MIN > MAX),
3205 then the operation caused one of them to wrap around, mark
3206 the new range VARYING. */
3207 set_value_range_to_varying (vr);
3209 else
3210 set_value_range (vr, type, min, max, NULL);
3213 /* Extract range information from a binary expression OP0 CODE OP1 based on
3214 the ranges of each of its operands with resulting type EXPR_TYPE.
3215 The resulting range is stored in *VR. */
3217 static void
3218 extract_range_from_binary_expr (value_range *vr,
3219 enum tree_code code,
3220 tree expr_type, tree op0, tree op1)
3222 value_range vr0 = VR_INITIALIZER;
3223 value_range vr1 = VR_INITIALIZER;
3225 /* Get value ranges for each operand. For constant operands, create
3226 a new value range with the operand to simplify processing. */
3227 if (TREE_CODE (op0) == SSA_NAME)
3228 vr0 = *(get_value_range (op0));
3229 else if (is_gimple_min_invariant (op0))
3230 set_value_range_to_value (&vr0, op0, NULL);
3231 else
3232 set_value_range_to_varying (&vr0);
3234 if (TREE_CODE (op1) == SSA_NAME)
3235 vr1 = *(get_value_range (op1));
3236 else if (is_gimple_min_invariant (op1))
3237 set_value_range_to_value (&vr1, op1, NULL);
3238 else
3239 set_value_range_to_varying (&vr1);
3241 extract_range_from_binary_expr_1 (vr, code, expr_type, &vr0, &vr1);
3243 /* Try harder for PLUS and MINUS if the range of one operand is symbolic
3244 and based on the other operand, for example if it was deduced from a
3245 symbolic comparison. When a bound of the range of the first operand
3246 is invariant, we set the corresponding bound of the new range to INF
3247 in order to avoid recursing on the range of the second operand. */
3248 if (vr->type == VR_VARYING
3249 && (code == PLUS_EXPR || code == MINUS_EXPR)
3250 && TREE_CODE (op1) == SSA_NAME
3251 && vr0.type == VR_RANGE
3252 && symbolic_range_based_on_p (&vr0, op1))
3254 const bool minus_p = (code == MINUS_EXPR);
3255 value_range n_vr1 = VR_INITIALIZER;
3257 /* Try with VR0 and [-INF, OP1]. */
3258 if (is_gimple_min_invariant (minus_p ? vr0.max : vr0.min))
3259 set_value_range (&n_vr1, VR_RANGE, vrp_val_min (expr_type), op1, NULL);
3261 /* Try with VR0 and [OP1, +INF]. */
3262 else if (is_gimple_min_invariant (minus_p ? vr0.min : vr0.max))
3263 set_value_range (&n_vr1, VR_RANGE, op1, vrp_val_max (expr_type), NULL);
3265 /* Try with VR0 and [OP1, OP1]. */
3266 else
3267 set_value_range (&n_vr1, VR_RANGE, op1, op1, NULL);
3269 extract_range_from_binary_expr_1 (vr, code, expr_type, &vr0, &n_vr1);
3272 if (vr->type == VR_VARYING
3273 && (code == PLUS_EXPR || code == MINUS_EXPR)
3274 && TREE_CODE (op0) == SSA_NAME
3275 && vr1.type == VR_RANGE
3276 && symbolic_range_based_on_p (&vr1, op0))
3278 const bool minus_p = (code == MINUS_EXPR);
3279 value_range n_vr0 = VR_INITIALIZER;
3281 /* Try with [-INF, OP0] and VR1. */
3282 if (is_gimple_min_invariant (minus_p ? vr1.max : vr1.min))
3283 set_value_range (&n_vr0, VR_RANGE, vrp_val_min (expr_type), op0, NULL);
3285 /* Try with [OP0, +INF] and VR1. */
3286 else if (is_gimple_min_invariant (minus_p ? vr1.min : vr1.max))
3287 set_value_range (&n_vr0, VR_RANGE, op0, vrp_val_max (expr_type), NULL);
3289 /* Try with [OP0, OP0] and VR1. */
3290 else
3291 set_value_range (&n_vr0, VR_RANGE, op0, op0, NULL);
3293 extract_range_from_binary_expr_1 (vr, code, expr_type, &n_vr0, &vr1);
3297 /* Extract range information from a unary operation CODE based on
3298 the range of its operand *VR0 with type OP0_TYPE with resulting type TYPE.
3299 The resulting range is stored in *VR. */
3301 void
3302 extract_range_from_unary_expr (value_range *vr,
3303 enum tree_code code, tree type,
3304 value_range *vr0_, tree op0_type)
3306 value_range vr0 = *vr0_, vrtem0 = VR_INITIALIZER, vrtem1 = VR_INITIALIZER;
3308 /* VRP only operates on integral and pointer types. */
3309 if (!(INTEGRAL_TYPE_P (op0_type)
3310 || POINTER_TYPE_P (op0_type))
3311 || !(INTEGRAL_TYPE_P (type)
3312 || POINTER_TYPE_P (type)))
3314 set_value_range_to_varying (vr);
3315 return;
3318 /* If VR0 is UNDEFINED, so is the result. */
3319 if (vr0.type == VR_UNDEFINED)
3321 set_value_range_to_undefined (vr);
3322 return;
3325 /* Handle operations that we express in terms of others. */
3326 if (code == PAREN_EXPR || code == OBJ_TYPE_REF)
3328 /* PAREN_EXPR and OBJ_TYPE_REF are simple copies. */
3329 copy_value_range (vr, &vr0);
3330 return;
3332 else if (code == NEGATE_EXPR)
3334 /* -X is simply 0 - X, so re-use existing code that also handles
3335 anti-ranges fine. */
3336 value_range zero = VR_INITIALIZER;
3337 set_value_range_to_value (&zero, build_int_cst (type, 0), NULL);
3338 extract_range_from_binary_expr_1 (vr, MINUS_EXPR, type, &zero, &vr0);
3339 return;
3341 else if (code == BIT_NOT_EXPR)
3343 /* ~X is simply -1 - X, so re-use existing code that also handles
3344 anti-ranges fine. */
3345 value_range minusone = VR_INITIALIZER;
3346 set_value_range_to_value (&minusone, build_int_cst (type, -1), NULL);
3347 extract_range_from_binary_expr_1 (vr, MINUS_EXPR,
3348 type, &minusone, &vr0);
3349 return;
3352 /* Now canonicalize anti-ranges to ranges when they are not symbolic
3353 and express op ~[] as (op []') U (op []''). */
3354 if (vr0.type == VR_ANTI_RANGE
3355 && ranges_from_anti_range (&vr0, &vrtem0, &vrtem1))
3357 extract_range_from_unary_expr (vr, code, type, &vrtem0, op0_type);
3358 if (vrtem1.type != VR_UNDEFINED)
3360 value_range vrres = VR_INITIALIZER;
3361 extract_range_from_unary_expr (&vrres, code, type,
3362 &vrtem1, op0_type);
3363 vrp_meet (vr, &vrres);
3365 return;
3368 if (CONVERT_EXPR_CODE_P (code))
3370 tree inner_type = op0_type;
3371 tree outer_type = type;
3373 /* If the expression evaluates to a pointer, we are only interested in
3374 determining if it evaluates to NULL [0, 0] or non-NULL (~[0, 0]). */
3375 if (POINTER_TYPE_P (type))
3377 if (range_is_nonnull (&vr0))
3378 set_value_range_to_nonnull (vr, type);
3379 else if (range_is_null (&vr0))
3380 set_value_range_to_null (vr, type);
3381 else
3382 set_value_range_to_varying (vr);
3383 return;
3386 /* If VR0 is varying and we increase the type precision, assume
3387 a full range for the following transformation. */
3388 if (vr0.type == VR_VARYING
3389 && INTEGRAL_TYPE_P (inner_type)
3390 && TYPE_PRECISION (inner_type) < TYPE_PRECISION (outer_type))
3392 vr0.type = VR_RANGE;
3393 vr0.min = TYPE_MIN_VALUE (inner_type);
3394 vr0.max = TYPE_MAX_VALUE (inner_type);
3397 /* If VR0 is a constant range or anti-range and the conversion is
3398 not truncating we can convert the min and max values and
3399 canonicalize the resulting range. Otherwise we can do the
3400 conversion if the size of the range is less than what the
3401 precision of the target type can represent and the range is
3402 not an anti-range. */
3403 if ((vr0.type == VR_RANGE
3404 || vr0.type == VR_ANTI_RANGE)
3405 && TREE_CODE (vr0.min) == INTEGER_CST
3406 && TREE_CODE (vr0.max) == INTEGER_CST
3407 && (!is_overflow_infinity (vr0.min)
3408 || (vr0.type == VR_RANGE
3409 && TYPE_PRECISION (outer_type) > TYPE_PRECISION (inner_type)
3410 && needs_overflow_infinity (outer_type)
3411 && supports_overflow_infinity (outer_type)))
3412 && (!is_overflow_infinity (vr0.max)
3413 || (vr0.type == VR_RANGE
3414 && TYPE_PRECISION (outer_type) > TYPE_PRECISION (inner_type)
3415 && needs_overflow_infinity (outer_type)
3416 && supports_overflow_infinity (outer_type)))
3417 && (TYPE_PRECISION (outer_type) >= TYPE_PRECISION (inner_type)
3418 || (vr0.type == VR_RANGE
3419 && integer_zerop (int_const_binop (RSHIFT_EXPR,
3420 int_const_binop (MINUS_EXPR, vr0.max, vr0.min),
3421 size_int (TYPE_PRECISION (outer_type)))))))
3423 tree new_min, new_max;
3424 if (is_overflow_infinity (vr0.min))
3425 new_min = negative_overflow_infinity (outer_type);
3426 else
3427 new_min = force_fit_type (outer_type, wi::to_widest (vr0.min),
3428 0, false);
3429 if (is_overflow_infinity (vr0.max))
3430 new_max = positive_overflow_infinity (outer_type);
3431 else
3432 new_max = force_fit_type (outer_type, wi::to_widest (vr0.max),
3433 0, false);
3434 set_and_canonicalize_value_range (vr, vr0.type,
3435 new_min, new_max, NULL);
3436 return;
3439 set_value_range_to_varying (vr);
3440 return;
3442 else if (code == ABS_EXPR)
3444 tree min, max;
3445 int cmp;
3447 /* Pass through vr0 in the easy cases. */
3448 if (TYPE_UNSIGNED (type)
3449 || value_range_nonnegative_p (&vr0))
3451 copy_value_range (vr, &vr0);
3452 return;
3455 /* For the remaining varying or symbolic ranges we can't do anything
3456 useful. */
3457 if (vr0.type == VR_VARYING
3458 || symbolic_range_p (&vr0))
3460 set_value_range_to_varying (vr);
3461 return;
3464 /* -TYPE_MIN_VALUE = TYPE_MIN_VALUE with flag_wrapv so we can't get a
3465 useful range. */
3466 if (!TYPE_OVERFLOW_UNDEFINED (type)
3467 && ((vr0.type == VR_RANGE
3468 && vrp_val_is_min (vr0.min))
3469 || (vr0.type == VR_ANTI_RANGE
3470 && !vrp_val_is_min (vr0.min))))
3472 set_value_range_to_varying (vr);
3473 return;
3476 /* ABS_EXPR may flip the range around, if the original range
3477 included negative values. */
3478 if (is_overflow_infinity (vr0.min))
3479 min = positive_overflow_infinity (type);
3480 else if (!vrp_val_is_min (vr0.min))
3481 min = fold_unary_to_constant (code, type, vr0.min);
3482 else if (!needs_overflow_infinity (type))
3483 min = TYPE_MAX_VALUE (type);
3484 else if (supports_overflow_infinity (type))
3485 min = positive_overflow_infinity (type);
3486 else
3488 set_value_range_to_varying (vr);
3489 return;
3492 if (is_overflow_infinity (vr0.max))
3493 max = positive_overflow_infinity (type);
3494 else if (!vrp_val_is_min (vr0.max))
3495 max = fold_unary_to_constant (code, type, vr0.max);
3496 else if (!needs_overflow_infinity (type))
3497 max = TYPE_MAX_VALUE (type);
3498 else if (supports_overflow_infinity (type)
3499 /* We shouldn't generate [+INF, +INF] as set_value_range
3500 doesn't like this and ICEs. */
3501 && !is_positive_overflow_infinity (min))
3502 max = positive_overflow_infinity (type);
3503 else
3505 set_value_range_to_varying (vr);
3506 return;
3509 cmp = compare_values (min, max);
3511 /* If a VR_ANTI_RANGEs contains zero, then we have
3512 ~[-INF, min(MIN, MAX)]. */
3513 if (vr0.type == VR_ANTI_RANGE)
3515 if (range_includes_zero_p (vr0.min, vr0.max) == 1)
3517 /* Take the lower of the two values. */
3518 if (cmp != 1)
3519 max = min;
3521 /* Create ~[-INF, min (abs(MIN), abs(MAX))]
3522 or ~[-INF + 1, min (abs(MIN), abs(MAX))] when
3523 flag_wrapv is set and the original anti-range doesn't include
3524 TYPE_MIN_VALUE, remember -TYPE_MIN_VALUE = TYPE_MIN_VALUE. */
3525 if (TYPE_OVERFLOW_WRAPS (type))
3527 tree type_min_value = TYPE_MIN_VALUE (type);
3529 min = (vr0.min != type_min_value
3530 ? int_const_binop (PLUS_EXPR, type_min_value,
3531 build_int_cst (TREE_TYPE (type_min_value), 1))
3532 : type_min_value);
3534 else
3536 if (overflow_infinity_range_p (&vr0))
3537 min = negative_overflow_infinity (type);
3538 else
3539 min = TYPE_MIN_VALUE (type);
3542 else
3544 /* All else has failed, so create the range [0, INF], even for
3545 flag_wrapv since TYPE_MIN_VALUE is in the original
3546 anti-range. */
3547 vr0.type = VR_RANGE;
3548 min = build_int_cst (type, 0);
3549 if (needs_overflow_infinity (type))
3551 if (supports_overflow_infinity (type))
3552 max = positive_overflow_infinity (type);
3553 else
3555 set_value_range_to_varying (vr);
3556 return;
3559 else
3560 max = TYPE_MAX_VALUE (type);
3564 /* If the range contains zero then we know that the minimum value in the
3565 range will be zero. */
3566 else if (range_includes_zero_p (vr0.min, vr0.max) == 1)
3568 if (cmp == 1)
3569 max = min;
3570 min = build_int_cst (type, 0);
3572 else
3574 /* If the range was reversed, swap MIN and MAX. */
3575 if (cmp == 1)
3576 std::swap (min, max);
3579 cmp = compare_values (min, max);
3580 if (cmp == -2 || cmp == 1)
3582 /* If the new range has its limits swapped around (MIN > MAX),
3583 then the operation caused one of them to wrap around, mark
3584 the new range VARYING. */
3585 set_value_range_to_varying (vr);
3587 else
3588 set_value_range (vr, vr0.type, min, max, NULL);
3589 return;
3592 /* For unhandled operations fall back to varying. */
3593 set_value_range_to_varying (vr);
3594 return;
3598 /* Extract range information from a unary expression CODE OP0 based on
3599 the range of its operand with resulting type TYPE.
3600 The resulting range is stored in *VR. */
3602 static void
3603 extract_range_from_unary_expr (value_range *vr, enum tree_code code,
3604 tree type, tree op0)
3606 value_range vr0 = VR_INITIALIZER;
3608 /* Get value ranges for the operand. For constant operands, create
3609 a new value range with the operand to simplify processing. */
3610 if (TREE_CODE (op0) == SSA_NAME)
3611 vr0 = *(get_value_range (op0));
3612 else if (is_gimple_min_invariant (op0))
3613 set_value_range_to_value (&vr0, op0, NULL);
3614 else
3615 set_value_range_to_varying (&vr0);
3617 extract_range_from_unary_expr (vr, code, type, &vr0, TREE_TYPE (op0));
3621 /* Extract range information from a conditional expression STMT based on
3622 the ranges of each of its operands and the expression code. */
3624 static void
3625 extract_range_from_cond_expr (value_range *vr, gassign *stmt)
3627 tree op0, op1;
3628 value_range vr0 = VR_INITIALIZER;
3629 value_range vr1 = VR_INITIALIZER;
3631 /* Get value ranges for each operand. For constant operands, create
3632 a new value range with the operand to simplify processing. */
3633 op0 = gimple_assign_rhs2 (stmt);
3634 if (TREE_CODE (op0) == SSA_NAME)
3635 vr0 = *(get_value_range (op0));
3636 else if (is_gimple_min_invariant (op0))
3637 set_value_range_to_value (&vr0, op0, NULL);
3638 else
3639 set_value_range_to_varying (&vr0);
3641 op1 = gimple_assign_rhs3 (stmt);
3642 if (TREE_CODE (op1) == SSA_NAME)
3643 vr1 = *(get_value_range (op1));
3644 else if (is_gimple_min_invariant (op1))
3645 set_value_range_to_value (&vr1, op1, NULL);
3646 else
3647 set_value_range_to_varying (&vr1);
3649 /* The resulting value range is the union of the operand ranges */
3650 copy_value_range (vr, &vr0);
3651 vrp_meet (vr, &vr1);
3655 /* Extract range information from a comparison expression EXPR based
3656 on the range of its operand and the expression code. */
3658 static void
3659 extract_range_from_comparison (value_range *vr, enum tree_code code,
3660 tree type, tree op0, tree op1)
3662 bool sop = false;
3663 tree val;
3665 val = vrp_evaluate_conditional_warnv_with_ops (code, op0, op1, false, &sop,
3666 NULL);
3668 /* A disadvantage of using a special infinity as an overflow
3669 representation is that we lose the ability to record overflow
3670 when we don't have an infinity. So we have to ignore a result
3671 which relies on overflow. */
3673 if (val && !is_overflow_infinity (val) && !sop)
3675 /* Since this expression was found on the RHS of an assignment,
3676 its type may be different from _Bool. Convert VAL to EXPR's
3677 type. */
3678 val = fold_convert (type, val);
3679 if (is_gimple_min_invariant (val))
3680 set_value_range_to_value (vr, val, vr->equiv);
3681 else
3682 set_value_range (vr, VR_RANGE, val, val, vr->equiv);
3684 else
3685 /* The result of a comparison is always true or false. */
3686 set_value_range_to_truthvalue (vr, type);
3689 /* Helper function for simplify_internal_call_using_ranges and
3690 extract_range_basic. Return true if OP0 SUBCODE OP1 for
3691 SUBCODE {PLUS,MINUS,MULT}_EXPR is known to never overflow or
3692 always overflow. Set *OVF to true if it is known to always
3693 overflow. */
3695 static bool
3696 check_for_binary_op_overflow (enum tree_code subcode, tree type,
3697 tree op0, tree op1, bool *ovf)
3699 value_range vr0 = VR_INITIALIZER;
3700 value_range vr1 = VR_INITIALIZER;
3701 if (TREE_CODE (op0) == SSA_NAME)
3702 vr0 = *get_value_range (op0);
3703 else if (TREE_CODE (op0) == INTEGER_CST)
3704 set_value_range_to_value (&vr0, op0, NULL);
3705 else
3706 set_value_range_to_varying (&vr0);
3708 if (TREE_CODE (op1) == SSA_NAME)
3709 vr1 = *get_value_range (op1);
3710 else if (TREE_CODE (op1) == INTEGER_CST)
3711 set_value_range_to_value (&vr1, op1, NULL);
3712 else
3713 set_value_range_to_varying (&vr1);
3715 if (!range_int_cst_p (&vr0)
3716 || TREE_OVERFLOW (vr0.min)
3717 || TREE_OVERFLOW (vr0.max))
3719 vr0.min = vrp_val_min (TREE_TYPE (op0));
3720 vr0.max = vrp_val_max (TREE_TYPE (op0));
3722 if (!range_int_cst_p (&vr1)
3723 || TREE_OVERFLOW (vr1.min)
3724 || TREE_OVERFLOW (vr1.max))
3726 vr1.min = vrp_val_min (TREE_TYPE (op1));
3727 vr1.max = vrp_val_max (TREE_TYPE (op1));
3729 *ovf = arith_overflowed_p (subcode, type, vr0.min,
3730 subcode == MINUS_EXPR ? vr1.max : vr1.min);
3731 if (arith_overflowed_p (subcode, type, vr0.max,
3732 subcode == MINUS_EXPR ? vr1.min : vr1.max) != *ovf)
3733 return false;
3734 if (subcode == MULT_EXPR)
3736 if (arith_overflowed_p (subcode, type, vr0.min, vr1.max) != *ovf
3737 || arith_overflowed_p (subcode, type, vr0.max, vr1.min) != *ovf)
3738 return false;
3740 if (*ovf)
3742 /* So far we found that there is an overflow on the boundaries.
3743 That doesn't prove that there is an overflow even for all values
3744 in between the boundaries. For that compute widest_int range
3745 of the result and see if it doesn't overlap the range of
3746 type. */
3747 widest_int wmin, wmax;
3748 widest_int w[4];
3749 int i;
3750 w[0] = wi::to_widest (vr0.min);
3751 w[1] = wi::to_widest (vr0.max);
3752 w[2] = wi::to_widest (vr1.min);
3753 w[3] = wi::to_widest (vr1.max);
3754 for (i = 0; i < 4; i++)
3756 widest_int wt;
3757 switch (subcode)
3759 case PLUS_EXPR:
3760 wt = wi::add (w[i & 1], w[2 + (i & 2) / 2]);
3761 break;
3762 case MINUS_EXPR:
3763 wt = wi::sub (w[i & 1], w[2 + (i & 2) / 2]);
3764 break;
3765 case MULT_EXPR:
3766 wt = wi::mul (w[i & 1], w[2 + (i & 2) / 2]);
3767 break;
3768 default:
3769 gcc_unreachable ();
3771 if (i == 0)
3773 wmin = wt;
3774 wmax = wt;
3776 else
3778 wmin = wi::smin (wmin, wt);
3779 wmax = wi::smax (wmax, wt);
3782 /* The result of op0 CODE op1 is known to be in range
3783 [wmin, wmax]. */
3784 widest_int wtmin = wi::to_widest (vrp_val_min (type));
3785 widest_int wtmax = wi::to_widest (vrp_val_max (type));
3786 /* If all values in [wmin, wmax] are smaller than
3787 [wtmin, wtmax] or all are larger than [wtmin, wtmax],
3788 the arithmetic operation will always overflow. */
3789 if (wmax < wtmin || wmin > wtmax)
3790 return true;
3791 return false;
3793 return true;
3796 /* Try to derive a nonnegative or nonzero range out of STMT relying
3797 primarily on generic routines in fold in conjunction with range data.
3798 Store the result in *VR */
3800 static void
3801 extract_range_basic (value_range *vr, gimple *stmt)
3803 bool sop = false;
3804 tree type = gimple_expr_type (stmt);
3806 if (is_gimple_call (stmt))
3808 tree arg;
3809 int mini, maxi, zerov = 0, prec;
3810 enum tree_code subcode = ERROR_MARK;
3811 combined_fn cfn = gimple_call_combined_fn (stmt);
3813 switch (cfn)
3815 case CFN_BUILT_IN_CONSTANT_P:
3816 /* If the call is __builtin_constant_p and the argument is a
3817 function parameter resolve it to false. This avoids bogus
3818 array bound warnings.
3819 ??? We could do this as early as inlining is finished. */
3820 arg = gimple_call_arg (stmt, 0);
3821 if (TREE_CODE (arg) == SSA_NAME
3822 && SSA_NAME_IS_DEFAULT_DEF (arg)
3823 && TREE_CODE (SSA_NAME_VAR (arg)) == PARM_DECL
3824 && cfun->after_inlining)
3826 set_value_range_to_null (vr, type);
3827 return;
3829 break;
3830 /* Both __builtin_ffs* and __builtin_popcount return
3831 [0, prec]. */
3832 CASE_CFN_FFS:
3833 CASE_CFN_POPCOUNT:
3834 arg = gimple_call_arg (stmt, 0);
3835 prec = TYPE_PRECISION (TREE_TYPE (arg));
3836 mini = 0;
3837 maxi = prec;
3838 if (TREE_CODE (arg) == SSA_NAME)
3840 value_range *vr0 = get_value_range (arg);
3841 /* If arg is non-zero, then ffs or popcount
3842 are non-zero. */
3843 if (((vr0->type == VR_RANGE
3844 && range_includes_zero_p (vr0->min, vr0->max) == 0)
3845 || (vr0->type == VR_ANTI_RANGE
3846 && range_includes_zero_p (vr0->min, vr0->max) == 1))
3847 && !is_overflow_infinity (vr0->min)
3848 && !is_overflow_infinity (vr0->max))
3849 mini = 1;
3850 /* If some high bits are known to be zero,
3851 we can decrease the maximum. */
3852 if (vr0->type == VR_RANGE
3853 && TREE_CODE (vr0->max) == INTEGER_CST
3854 && !operand_less_p (vr0->min,
3855 build_zero_cst (TREE_TYPE (vr0->min)))
3856 && !is_overflow_infinity (vr0->max))
3857 maxi = tree_floor_log2 (vr0->max) + 1;
3859 goto bitop_builtin;
3860 /* __builtin_parity* returns [0, 1]. */
3861 CASE_CFN_PARITY:
3862 mini = 0;
3863 maxi = 1;
3864 goto bitop_builtin;
3865 /* __builtin_c[lt]z* return [0, prec-1], except for
3866 when the argument is 0, but that is undefined behavior.
3867 On many targets where the CLZ RTL or optab value is defined
3868 for 0 the value is prec, so include that in the range
3869 by default. */
3870 CASE_CFN_CLZ:
3871 arg = gimple_call_arg (stmt, 0);
3872 prec = TYPE_PRECISION (TREE_TYPE (arg));
3873 mini = 0;
3874 maxi = prec;
3875 if (optab_handler (clz_optab, TYPE_MODE (TREE_TYPE (arg)))
3876 != CODE_FOR_nothing
3877 && CLZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (TREE_TYPE (arg)),
3878 zerov)
3879 /* Handle only the single common value. */
3880 && zerov != prec)
3881 /* Magic value to give up, unless vr0 proves
3882 arg is non-zero. */
3883 mini = -2;
3884 if (TREE_CODE (arg) == SSA_NAME)
3886 value_range *vr0 = get_value_range (arg);
3887 /* From clz of VR_RANGE minimum we can compute
3888 result maximum. */
3889 if (vr0->type == VR_RANGE
3890 && TREE_CODE (vr0->min) == INTEGER_CST
3891 && !is_overflow_infinity (vr0->min))
3893 maxi = prec - 1 - tree_floor_log2 (vr0->min);
3894 if (maxi != prec)
3895 mini = 0;
3897 else if (vr0->type == VR_ANTI_RANGE
3898 && integer_zerop (vr0->min)
3899 && !is_overflow_infinity (vr0->min))
3901 maxi = prec - 1;
3902 mini = 0;
3904 if (mini == -2)
3905 break;
3906 /* From clz of VR_RANGE maximum we can compute
3907 result minimum. */
3908 if (vr0->type == VR_RANGE
3909 && TREE_CODE (vr0->max) == INTEGER_CST
3910 && !is_overflow_infinity (vr0->max))
3912 mini = prec - 1 - tree_floor_log2 (vr0->max);
3913 if (mini == prec)
3914 break;
3917 if (mini == -2)
3918 break;
3919 goto bitop_builtin;
3920 /* __builtin_ctz* return [0, prec-1], except for
3921 when the argument is 0, but that is undefined behavior.
3922 If there is a ctz optab for this mode and
3923 CTZ_DEFINED_VALUE_AT_ZERO, include that in the range,
3924 otherwise just assume 0 won't be seen. */
3925 CASE_CFN_CTZ:
3926 arg = gimple_call_arg (stmt, 0);
3927 prec = TYPE_PRECISION (TREE_TYPE (arg));
3928 mini = 0;
3929 maxi = prec - 1;
3930 if (optab_handler (ctz_optab, TYPE_MODE (TREE_TYPE (arg)))
3931 != CODE_FOR_nothing
3932 && CTZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (TREE_TYPE (arg)),
3933 zerov))
3935 /* Handle only the two common values. */
3936 if (zerov == -1)
3937 mini = -1;
3938 else if (zerov == prec)
3939 maxi = prec;
3940 else
3941 /* Magic value to give up, unless vr0 proves
3942 arg is non-zero. */
3943 mini = -2;
3945 if (TREE_CODE (arg) == SSA_NAME)
3947 value_range *vr0 = get_value_range (arg);
3948 /* If arg is non-zero, then use [0, prec - 1]. */
3949 if (((vr0->type == VR_RANGE
3950 && integer_nonzerop (vr0->min))
3951 || (vr0->type == VR_ANTI_RANGE
3952 && integer_zerop (vr0->min)))
3953 && !is_overflow_infinity (vr0->min))
3955 mini = 0;
3956 maxi = prec - 1;
3958 /* If some high bits are known to be zero,
3959 we can decrease the result maximum. */
3960 if (vr0->type == VR_RANGE
3961 && TREE_CODE (vr0->max) == INTEGER_CST
3962 && !is_overflow_infinity (vr0->max))
3964 maxi = tree_floor_log2 (vr0->max);
3965 /* For vr0 [0, 0] give up. */
3966 if (maxi == -1)
3967 break;
3970 if (mini == -2)
3971 break;
3972 goto bitop_builtin;
3973 /* __builtin_clrsb* returns [0, prec-1]. */
3974 CASE_CFN_CLRSB:
3975 arg = gimple_call_arg (stmt, 0);
3976 prec = TYPE_PRECISION (TREE_TYPE (arg));
3977 mini = 0;
3978 maxi = prec - 1;
3979 goto bitop_builtin;
3980 bitop_builtin:
3981 set_value_range (vr, VR_RANGE, build_int_cst (type, mini),
3982 build_int_cst (type, maxi), NULL);
3983 return;
3984 case CFN_UBSAN_CHECK_ADD:
3985 subcode = PLUS_EXPR;
3986 break;
3987 case CFN_UBSAN_CHECK_SUB:
3988 subcode = MINUS_EXPR;
3989 break;
3990 case CFN_UBSAN_CHECK_MUL:
3991 subcode = MULT_EXPR;
3992 break;
3993 case CFN_GOACC_DIM_SIZE:
3994 case CFN_GOACC_DIM_POS:
3995 /* Optimizing these two internal functions helps the loop
3996 optimizer eliminate outer comparisons. Size is [1,N]
3997 and pos is [0,N-1]. */
3999 bool is_pos = cfn == CFN_GOACC_DIM_POS;
4000 int axis = get_oacc_ifn_dim_arg (stmt);
4001 int size = get_oacc_fn_dim_size (current_function_decl, axis);
4003 if (!size)
4004 /* If it's dynamic, the backend might know a hardware
4005 limitation. */
4006 size = targetm.goacc.dim_limit (axis);
4008 tree type = TREE_TYPE (gimple_call_lhs (stmt));
4009 set_value_range (vr, VR_RANGE,
4010 build_int_cst (type, is_pos ? 0 : 1),
4011 size ? build_int_cst (type, size - is_pos)
4012 : vrp_val_max (type), NULL);
4014 return;
4015 default:
4016 break;
4018 if (subcode != ERROR_MARK)
4020 bool saved_flag_wrapv = flag_wrapv;
4021 /* Pretend the arithmetics is wrapping. If there is
4022 any overflow, we'll complain, but will actually do
4023 wrapping operation. */
4024 flag_wrapv = 1;
4025 extract_range_from_binary_expr (vr, subcode, type,
4026 gimple_call_arg (stmt, 0),
4027 gimple_call_arg (stmt, 1));
4028 flag_wrapv = saved_flag_wrapv;
4030 /* If for both arguments vrp_valueize returned non-NULL,
4031 this should have been already folded and if not, it
4032 wasn't folded because of overflow. Avoid removing the
4033 UBSAN_CHECK_* calls in that case. */
4034 if (vr->type == VR_RANGE
4035 && (vr->min == vr->max
4036 || operand_equal_p (vr->min, vr->max, 0)))
4037 set_value_range_to_varying (vr);
4038 return;
4041 /* Handle extraction of the two results (result of arithmetics and
4042 a flag whether arithmetics overflowed) from {ADD,SUB,MUL}_OVERFLOW
4043 internal function. */
4044 else if (is_gimple_assign (stmt)
4045 && (gimple_assign_rhs_code (stmt) == REALPART_EXPR
4046 || gimple_assign_rhs_code (stmt) == IMAGPART_EXPR)
4047 && INTEGRAL_TYPE_P (type))
4049 enum tree_code code = gimple_assign_rhs_code (stmt);
4050 tree op = gimple_assign_rhs1 (stmt);
4051 if (TREE_CODE (op) == code && TREE_CODE (TREE_OPERAND (op, 0)) == SSA_NAME)
4053 gimple *g = SSA_NAME_DEF_STMT (TREE_OPERAND (op, 0));
4054 if (is_gimple_call (g) && gimple_call_internal_p (g))
4056 enum tree_code subcode = ERROR_MARK;
4057 switch (gimple_call_internal_fn (g))
4059 case IFN_ADD_OVERFLOW:
4060 subcode = PLUS_EXPR;
4061 break;
4062 case IFN_SUB_OVERFLOW:
4063 subcode = MINUS_EXPR;
4064 break;
4065 case IFN_MUL_OVERFLOW:
4066 subcode = MULT_EXPR;
4067 break;
4068 default:
4069 break;
4071 if (subcode != ERROR_MARK)
4073 tree op0 = gimple_call_arg (g, 0);
4074 tree op1 = gimple_call_arg (g, 1);
4075 if (code == IMAGPART_EXPR)
4077 bool ovf = false;
4078 if (check_for_binary_op_overflow (subcode, type,
4079 op0, op1, &ovf))
4080 set_value_range_to_value (vr,
4081 build_int_cst (type, ovf),
4082 NULL);
4083 else if (TYPE_PRECISION (type) == 1
4084 && !TYPE_UNSIGNED (type))
4085 set_value_range_to_varying (vr);
4086 else
4087 set_value_range (vr, VR_RANGE, build_int_cst (type, 0),
4088 build_int_cst (type, 1), NULL);
4090 else if (types_compatible_p (type, TREE_TYPE (op0))
4091 && types_compatible_p (type, TREE_TYPE (op1)))
4093 bool saved_flag_wrapv = flag_wrapv;
4094 /* Pretend the arithmetics is wrapping. If there is
4095 any overflow, IMAGPART_EXPR will be set. */
4096 flag_wrapv = 1;
4097 extract_range_from_binary_expr (vr, subcode, type,
4098 op0, op1);
4099 flag_wrapv = saved_flag_wrapv;
4101 else
4103 value_range vr0 = VR_INITIALIZER;
4104 value_range vr1 = VR_INITIALIZER;
4105 bool saved_flag_wrapv = flag_wrapv;
4106 /* Pretend the arithmetics is wrapping. If there is
4107 any overflow, IMAGPART_EXPR will be set. */
4108 flag_wrapv = 1;
4109 extract_range_from_unary_expr (&vr0, NOP_EXPR,
4110 type, op0);
4111 extract_range_from_unary_expr (&vr1, NOP_EXPR,
4112 type, op1);
4113 extract_range_from_binary_expr_1 (vr, subcode, type,
4114 &vr0, &vr1);
4115 flag_wrapv = saved_flag_wrapv;
4117 return;
4122 if (INTEGRAL_TYPE_P (type)
4123 && gimple_stmt_nonnegative_warnv_p (stmt, &sop))
4124 set_value_range_to_nonnegative (vr, type,
4125 sop || stmt_overflow_infinity (stmt));
4126 else if (vrp_stmt_computes_nonzero (stmt, &sop)
4127 && !sop)
4128 set_value_range_to_nonnull (vr, type);
4129 else
4130 set_value_range_to_varying (vr);
4134 /* Try to compute a useful range out of assignment STMT and store it
4135 in *VR. */
4137 static void
4138 extract_range_from_assignment (value_range *vr, gassign *stmt)
4140 enum tree_code code = gimple_assign_rhs_code (stmt);
4142 if (code == ASSERT_EXPR)
4143 extract_range_from_assert (vr, gimple_assign_rhs1 (stmt));
4144 else if (code == SSA_NAME)
4145 extract_range_from_ssa_name (vr, gimple_assign_rhs1 (stmt));
4146 else if (TREE_CODE_CLASS (code) == tcc_binary)
4147 extract_range_from_binary_expr (vr, gimple_assign_rhs_code (stmt),
4148 gimple_expr_type (stmt),
4149 gimple_assign_rhs1 (stmt),
4150 gimple_assign_rhs2 (stmt));
4151 else if (TREE_CODE_CLASS (code) == tcc_unary)
4152 extract_range_from_unary_expr (vr, gimple_assign_rhs_code (stmt),
4153 gimple_expr_type (stmt),
4154 gimple_assign_rhs1 (stmt));
4155 else if (code == COND_EXPR)
4156 extract_range_from_cond_expr (vr, stmt);
4157 else if (TREE_CODE_CLASS (code) == tcc_comparison)
4158 extract_range_from_comparison (vr, gimple_assign_rhs_code (stmt),
4159 gimple_expr_type (stmt),
4160 gimple_assign_rhs1 (stmt),
4161 gimple_assign_rhs2 (stmt));
4162 else if (get_gimple_rhs_class (code) == GIMPLE_SINGLE_RHS
4163 && is_gimple_min_invariant (gimple_assign_rhs1 (stmt)))
4164 set_value_range_to_value (vr, gimple_assign_rhs1 (stmt), NULL);
4165 else
4166 set_value_range_to_varying (vr);
4168 if (vr->type == VR_VARYING)
4169 extract_range_basic (vr, stmt);
4172 /* Given a range VR, a LOOP and a variable VAR, determine whether it
4173 would be profitable to adjust VR using scalar evolution information
4174 for VAR. If so, update VR with the new limits. */
4176 static void
4177 adjust_range_with_scev (value_range *vr, struct loop *loop,
4178 gimple *stmt, tree var)
4180 tree init, step, chrec, tmin, tmax, min, max, type, tem;
4181 enum ev_direction dir;
4183 /* TODO. Don't adjust anti-ranges. An anti-range may provide
4184 better opportunities than a regular range, but I'm not sure. */
4185 if (vr->type == VR_ANTI_RANGE)
4186 return;
4188 chrec = instantiate_parameters (loop, analyze_scalar_evolution (loop, var));
4190 /* Like in PR19590, scev can return a constant function. */
4191 if (is_gimple_min_invariant (chrec))
4193 set_value_range_to_value (vr, chrec, vr->equiv);
4194 return;
4197 if (TREE_CODE (chrec) != POLYNOMIAL_CHREC)
4198 return;
4200 init = initial_condition_in_loop_num (chrec, loop->num);
4201 tem = op_with_constant_singleton_value_range (init);
4202 if (tem)
4203 init = tem;
4204 step = evolution_part_in_loop_num (chrec, loop->num);
4205 tem = op_with_constant_singleton_value_range (step);
4206 if (tem)
4207 step = tem;
4209 /* If STEP is symbolic, we can't know whether INIT will be the
4210 minimum or maximum value in the range. Also, unless INIT is
4211 a simple expression, compare_values and possibly other functions
4212 in tree-vrp won't be able to handle it. */
4213 if (step == NULL_TREE
4214 || !is_gimple_min_invariant (step)
4215 || !valid_value_p (init))
4216 return;
4218 dir = scev_direction (chrec);
4219 if (/* Do not adjust ranges if we do not know whether the iv increases
4220 or decreases, ... */
4221 dir == EV_DIR_UNKNOWN
4222 /* ... or if it may wrap. */
4223 || scev_probably_wraps_p (NULL_TREE, init, step, stmt,
4224 get_chrec_loop (chrec), true))
4225 return;
4227 /* We use TYPE_MIN_VALUE and TYPE_MAX_VALUE here instead of
4228 negative_overflow_infinity and positive_overflow_infinity,
4229 because we have concluded that the loop probably does not
4230 wrap. */
4232 type = TREE_TYPE (var);
4233 if (POINTER_TYPE_P (type) || !TYPE_MIN_VALUE (type))
4234 tmin = lower_bound_in_type (type, type);
4235 else
4236 tmin = TYPE_MIN_VALUE (type);
4237 if (POINTER_TYPE_P (type) || !TYPE_MAX_VALUE (type))
4238 tmax = upper_bound_in_type (type, type);
4239 else
4240 tmax = TYPE_MAX_VALUE (type);
4242 /* Try to use estimated number of iterations for the loop to constrain the
4243 final value in the evolution. */
4244 if (TREE_CODE (step) == INTEGER_CST
4245 && is_gimple_val (init)
4246 && (TREE_CODE (init) != SSA_NAME
4247 || get_value_range (init)->type == VR_RANGE))
4249 widest_int nit;
4251 /* We are only entering here for loop header PHI nodes, so using
4252 the number of latch executions is the correct thing to use. */
4253 if (max_loop_iterations (loop, &nit))
4255 value_range maxvr = VR_INITIALIZER;
4256 signop sgn = TYPE_SIGN (TREE_TYPE (step));
4257 bool overflow;
4259 widest_int wtmp = wi::mul (wi::to_widest (step), nit, sgn,
4260 &overflow);
4261 /* If the multiplication overflowed we can't do a meaningful
4262 adjustment. Likewise if the result doesn't fit in the type
4263 of the induction variable. For a signed type we have to
4264 check whether the result has the expected signedness which
4265 is that of the step as number of iterations is unsigned. */
4266 if (!overflow
4267 && wi::fits_to_tree_p (wtmp, TREE_TYPE (init))
4268 && (sgn == UNSIGNED
4269 || wi::gts_p (wtmp, 0) == wi::gts_p (step, 0)))
4271 tem = wide_int_to_tree (TREE_TYPE (init), wtmp);
4272 extract_range_from_binary_expr (&maxvr, PLUS_EXPR,
4273 TREE_TYPE (init), init, tem);
4274 /* Likewise if the addition did. */
4275 if (maxvr.type == VR_RANGE)
4277 value_range initvr = VR_INITIALIZER;
4279 if (TREE_CODE (init) == SSA_NAME)
4280 initvr = *(get_value_range (init));
4281 else if (is_gimple_min_invariant (init))
4282 set_value_range_to_value (&initvr, init, NULL);
4283 else
4284 return;
4286 /* Check if init + nit * step overflows. Though we checked
4287 scev {init, step}_loop doesn't wrap, it is not enough
4288 because the loop may exit immediately. Overflow could
4289 happen in the plus expression in this case. */
4290 if ((dir == EV_DIR_DECREASES
4291 && (is_negative_overflow_infinity (maxvr.min)
4292 || compare_values (maxvr.min, initvr.min) != -1))
4293 || (dir == EV_DIR_GROWS
4294 && (is_positive_overflow_infinity (maxvr.max)
4295 || compare_values (maxvr.max, initvr.max) != 1)))
4296 return;
4298 tmin = maxvr.min;
4299 tmax = maxvr.max;
4305 if (vr->type == VR_VARYING || vr->type == VR_UNDEFINED)
4307 min = tmin;
4308 max = tmax;
4310 /* For VARYING or UNDEFINED ranges, just about anything we get
4311 from scalar evolutions should be better. */
4313 if (dir == EV_DIR_DECREASES)
4314 max = init;
4315 else
4316 min = init;
4318 else if (vr->type == VR_RANGE)
4320 min = vr->min;
4321 max = vr->max;
4323 if (dir == EV_DIR_DECREASES)
4325 /* INIT is the maximum value. If INIT is lower than VR->MAX
4326 but no smaller than VR->MIN, set VR->MAX to INIT. */
4327 if (compare_values (init, max) == -1)
4328 max = init;
4330 /* According to the loop information, the variable does not
4331 overflow. If we think it does, probably because of an
4332 overflow due to arithmetic on a different INF value,
4333 reset now. */
4334 if (is_negative_overflow_infinity (min)
4335 || compare_values (min, tmin) == -1)
4336 min = tmin;
4339 else
4341 /* If INIT is bigger than VR->MIN, set VR->MIN to INIT. */
4342 if (compare_values (init, min) == 1)
4343 min = init;
4345 if (is_positive_overflow_infinity (max)
4346 || compare_values (tmax, max) == -1)
4347 max = tmax;
4350 else
4351 return;
4353 /* If we just created an invalid range with the minimum
4354 greater than the maximum, we fail conservatively.
4355 This should happen only in unreachable
4356 parts of code, or for invalid programs. */
4357 if (compare_values (min, max) == 1
4358 || (is_negative_overflow_infinity (min)
4359 && is_positive_overflow_infinity (max)))
4360 return;
4362 /* Even for valid range info, sometimes overflow flag will leak in.
4363 As GIMPLE IL should have no constants with TREE_OVERFLOW set, we
4364 drop them except for +-overflow_infinity which still need special
4365 handling in vrp pass. */
4366 if (TREE_OVERFLOW_P (min)
4367 && ! is_negative_overflow_infinity (min))
4368 min = drop_tree_overflow (min);
4369 if (TREE_OVERFLOW_P (max)
4370 && ! is_positive_overflow_infinity (max))
4371 max = drop_tree_overflow (max);
4373 set_value_range (vr, VR_RANGE, min, max, vr->equiv);
4377 /* Given two numeric value ranges VR0, VR1 and a comparison code COMP:
4379 - Return BOOLEAN_TRUE_NODE if VR0 COMP VR1 always returns true for
4380 all the values in the ranges.
4382 - Return BOOLEAN_FALSE_NODE if the comparison always returns false.
4384 - Return NULL_TREE if it is not always possible to determine the
4385 value of the comparison.
4387 Also set *STRICT_OVERFLOW_P to indicate whether a range with an
4388 overflow infinity was used in the test. */
4391 static tree
4392 compare_ranges (enum tree_code comp, value_range *vr0, value_range *vr1,
4393 bool *strict_overflow_p)
4395 /* VARYING or UNDEFINED ranges cannot be compared. */
4396 if (vr0->type == VR_VARYING
4397 || vr0->type == VR_UNDEFINED
4398 || vr1->type == VR_VARYING
4399 || vr1->type == VR_UNDEFINED)
4400 return NULL_TREE;
4402 /* Anti-ranges need to be handled separately. */
4403 if (vr0->type == VR_ANTI_RANGE || vr1->type == VR_ANTI_RANGE)
4405 /* If both are anti-ranges, then we cannot compute any
4406 comparison. */
4407 if (vr0->type == VR_ANTI_RANGE && vr1->type == VR_ANTI_RANGE)
4408 return NULL_TREE;
4410 /* These comparisons are never statically computable. */
4411 if (comp == GT_EXPR
4412 || comp == GE_EXPR
4413 || comp == LT_EXPR
4414 || comp == LE_EXPR)
4415 return NULL_TREE;
4417 /* Equality can be computed only between a range and an
4418 anti-range. ~[VAL1, VAL2] == [VAL1, VAL2] is always false. */
4419 if (vr0->type == VR_RANGE)
4421 /* To simplify processing, make VR0 the anti-range. */
4422 value_range *tmp = vr0;
4423 vr0 = vr1;
4424 vr1 = tmp;
4427 gcc_assert (comp == NE_EXPR || comp == EQ_EXPR);
4429 if (compare_values_warnv (vr0->min, vr1->min, strict_overflow_p) == 0
4430 && compare_values_warnv (vr0->max, vr1->max, strict_overflow_p) == 0)
4431 return (comp == NE_EXPR) ? boolean_true_node : boolean_false_node;
4433 return NULL_TREE;
4436 if (!usable_range_p (vr0, strict_overflow_p)
4437 || !usable_range_p (vr1, strict_overflow_p))
4438 return NULL_TREE;
4440 /* Simplify processing. If COMP is GT_EXPR or GE_EXPR, switch the
4441 operands around and change the comparison code. */
4442 if (comp == GT_EXPR || comp == GE_EXPR)
4444 comp = (comp == GT_EXPR) ? LT_EXPR : LE_EXPR;
4445 std::swap (vr0, vr1);
4448 if (comp == EQ_EXPR)
4450 /* Equality may only be computed if both ranges represent
4451 exactly one value. */
4452 if (compare_values_warnv (vr0->min, vr0->max, strict_overflow_p) == 0
4453 && compare_values_warnv (vr1->min, vr1->max, strict_overflow_p) == 0)
4455 int cmp_min = compare_values_warnv (vr0->min, vr1->min,
4456 strict_overflow_p);
4457 int cmp_max = compare_values_warnv (vr0->max, vr1->max,
4458 strict_overflow_p);
4459 if (cmp_min == 0 && cmp_max == 0)
4460 return boolean_true_node;
4461 else if (cmp_min != -2 && cmp_max != -2)
4462 return boolean_false_node;
4464 /* If [V0_MIN, V1_MAX] < [V1_MIN, V1_MAX] then V0 != V1. */
4465 else if (compare_values_warnv (vr0->min, vr1->max,
4466 strict_overflow_p) == 1
4467 || compare_values_warnv (vr1->min, vr0->max,
4468 strict_overflow_p) == 1)
4469 return boolean_false_node;
4471 return NULL_TREE;
4473 else if (comp == NE_EXPR)
4475 int cmp1, cmp2;
4477 /* If VR0 is completely to the left or completely to the right
4478 of VR1, they are always different. Notice that we need to
4479 make sure that both comparisons yield similar results to
4480 avoid comparing values that cannot be compared at
4481 compile-time. */
4482 cmp1 = compare_values_warnv (vr0->max, vr1->min, strict_overflow_p);
4483 cmp2 = compare_values_warnv (vr0->min, vr1->max, strict_overflow_p);
4484 if ((cmp1 == -1 && cmp2 == -1) || (cmp1 == 1 && cmp2 == 1))
4485 return boolean_true_node;
4487 /* If VR0 and VR1 represent a single value and are identical,
4488 return false. */
4489 else if (compare_values_warnv (vr0->min, vr0->max,
4490 strict_overflow_p) == 0
4491 && compare_values_warnv (vr1->min, vr1->max,
4492 strict_overflow_p) == 0
4493 && compare_values_warnv (vr0->min, vr1->min,
4494 strict_overflow_p) == 0
4495 && compare_values_warnv (vr0->max, vr1->max,
4496 strict_overflow_p) == 0)
4497 return boolean_false_node;
4499 /* Otherwise, they may or may not be different. */
4500 else
4501 return NULL_TREE;
4503 else if (comp == LT_EXPR || comp == LE_EXPR)
4505 int tst;
4507 /* If VR0 is to the left of VR1, return true. */
4508 tst = compare_values_warnv (vr0->max, vr1->min, strict_overflow_p);
4509 if ((comp == LT_EXPR && tst == -1)
4510 || (comp == LE_EXPR && (tst == -1 || tst == 0)))
4512 if (overflow_infinity_range_p (vr0)
4513 || overflow_infinity_range_p (vr1))
4514 *strict_overflow_p = true;
4515 return boolean_true_node;
4518 /* If VR0 is to the right of VR1, return false. */
4519 tst = compare_values_warnv (vr0->min, vr1->max, strict_overflow_p);
4520 if ((comp == LT_EXPR && (tst == 0 || tst == 1))
4521 || (comp == LE_EXPR && tst == 1))
4523 if (overflow_infinity_range_p (vr0)
4524 || overflow_infinity_range_p (vr1))
4525 *strict_overflow_p = true;
4526 return boolean_false_node;
4529 /* Otherwise, we don't know. */
4530 return NULL_TREE;
4533 gcc_unreachable ();
4537 /* Given a value range VR, a value VAL and a comparison code COMP, return
4538 BOOLEAN_TRUE_NODE if VR COMP VAL always returns true for all the
4539 values in VR. Return BOOLEAN_FALSE_NODE if the comparison
4540 always returns false. Return NULL_TREE if it is not always
4541 possible to determine the value of the comparison. Also set
4542 *STRICT_OVERFLOW_P to indicate whether a range with an overflow
4543 infinity was used in the test. */
4545 static tree
4546 compare_range_with_value (enum tree_code comp, value_range *vr, tree val,
4547 bool *strict_overflow_p)
4549 if (vr->type == VR_VARYING || vr->type == VR_UNDEFINED)
4550 return NULL_TREE;
4552 /* Anti-ranges need to be handled separately. */
4553 if (vr->type == VR_ANTI_RANGE)
4555 /* For anti-ranges, the only predicates that we can compute at
4556 compile time are equality and inequality. */
4557 if (comp == GT_EXPR
4558 || comp == GE_EXPR
4559 || comp == LT_EXPR
4560 || comp == LE_EXPR)
4561 return NULL_TREE;
4563 /* ~[VAL_1, VAL_2] OP VAL is known if VAL_1 <= VAL <= VAL_2. */
4564 if (value_inside_range (val, vr->min, vr->max) == 1)
4565 return (comp == NE_EXPR) ? boolean_true_node : boolean_false_node;
4567 return NULL_TREE;
4570 if (!usable_range_p (vr, strict_overflow_p))
4571 return NULL_TREE;
4573 if (comp == EQ_EXPR)
4575 /* EQ_EXPR may only be computed if VR represents exactly
4576 one value. */
4577 if (compare_values_warnv (vr->min, vr->max, strict_overflow_p) == 0)
4579 int cmp = compare_values_warnv (vr->min, val, strict_overflow_p);
4580 if (cmp == 0)
4581 return boolean_true_node;
4582 else if (cmp == -1 || cmp == 1 || cmp == 2)
4583 return boolean_false_node;
4585 else if (compare_values_warnv (val, vr->min, strict_overflow_p) == -1
4586 || compare_values_warnv (vr->max, val, strict_overflow_p) == -1)
4587 return boolean_false_node;
4589 return NULL_TREE;
4591 else if (comp == NE_EXPR)
4593 /* If VAL is not inside VR, then they are always different. */
4594 if (compare_values_warnv (vr->max, val, strict_overflow_p) == -1
4595 || compare_values_warnv (vr->min, val, strict_overflow_p) == 1)
4596 return boolean_true_node;
4598 /* If VR represents exactly one value equal to VAL, then return
4599 false. */
4600 if (compare_values_warnv (vr->min, vr->max, strict_overflow_p) == 0
4601 && compare_values_warnv (vr->min, val, strict_overflow_p) == 0)
4602 return boolean_false_node;
4604 /* Otherwise, they may or may not be different. */
4605 return NULL_TREE;
4607 else if (comp == LT_EXPR || comp == LE_EXPR)
4609 int tst;
4611 /* If VR is to the left of VAL, return true. */
4612 tst = compare_values_warnv (vr->max, val, strict_overflow_p);
4613 if ((comp == LT_EXPR && tst == -1)
4614 || (comp == LE_EXPR && (tst == -1 || tst == 0)))
4616 if (overflow_infinity_range_p (vr))
4617 *strict_overflow_p = true;
4618 return boolean_true_node;
4621 /* If VR is to the right of VAL, return false. */
4622 tst = compare_values_warnv (vr->min, val, strict_overflow_p);
4623 if ((comp == LT_EXPR && (tst == 0 || tst == 1))
4624 || (comp == LE_EXPR && tst == 1))
4626 if (overflow_infinity_range_p (vr))
4627 *strict_overflow_p = true;
4628 return boolean_false_node;
4631 /* Otherwise, we don't know. */
4632 return NULL_TREE;
4634 else if (comp == GT_EXPR || comp == GE_EXPR)
4636 int tst;
4638 /* If VR is to the right of VAL, return true. */
4639 tst = compare_values_warnv (vr->min, val, strict_overflow_p);
4640 if ((comp == GT_EXPR && tst == 1)
4641 || (comp == GE_EXPR && (tst == 0 || tst == 1)))
4643 if (overflow_infinity_range_p (vr))
4644 *strict_overflow_p = true;
4645 return boolean_true_node;
4648 /* If VR is to the left of VAL, return false. */
4649 tst = compare_values_warnv (vr->max, val, strict_overflow_p);
4650 if ((comp == GT_EXPR && (tst == -1 || tst == 0))
4651 || (comp == GE_EXPR && tst == -1))
4653 if (overflow_infinity_range_p (vr))
4654 *strict_overflow_p = true;
4655 return boolean_false_node;
4658 /* Otherwise, we don't know. */
4659 return NULL_TREE;
4662 gcc_unreachable ();
4666 /* Debugging dumps. */
4668 void dump_value_range (FILE *, const value_range *);
4669 void debug_value_range (value_range *);
4670 void dump_all_value_ranges (FILE *);
4671 void debug_all_value_ranges (void);
4672 void dump_vr_equiv (FILE *, bitmap);
4673 void debug_vr_equiv (bitmap);
4676 /* Dump value range VR to FILE. */
4678 void
4679 dump_value_range (FILE *file, const value_range *vr)
4681 if (vr == NULL)
4682 fprintf (file, "[]");
4683 else if (vr->type == VR_UNDEFINED)
4684 fprintf (file, "UNDEFINED");
4685 else if (vr->type == VR_RANGE || vr->type == VR_ANTI_RANGE)
4687 tree type = TREE_TYPE (vr->min);
4689 fprintf (file, "%s[", (vr->type == VR_ANTI_RANGE) ? "~" : "");
4691 if (is_negative_overflow_infinity (vr->min))
4692 fprintf (file, "-INF(OVF)");
4693 else if (INTEGRAL_TYPE_P (type)
4694 && !TYPE_UNSIGNED (type)
4695 && vrp_val_is_min (vr->min))
4696 fprintf (file, "-INF");
4697 else
4698 print_generic_expr (file, vr->min, 0);
4700 fprintf (file, ", ");
4702 if (is_positive_overflow_infinity (vr->max))
4703 fprintf (file, "+INF(OVF)");
4704 else if (INTEGRAL_TYPE_P (type)
4705 && vrp_val_is_max (vr->max))
4706 fprintf (file, "+INF");
4707 else
4708 print_generic_expr (file, vr->max, 0);
4710 fprintf (file, "]");
4712 if (vr->equiv)
4714 bitmap_iterator bi;
4715 unsigned i, c = 0;
4717 fprintf (file, " EQUIVALENCES: { ");
4719 EXECUTE_IF_SET_IN_BITMAP (vr->equiv, 0, i, bi)
4721 print_generic_expr (file, ssa_name (i), 0);
4722 fprintf (file, " ");
4723 c++;
4726 fprintf (file, "} (%u elements)", c);
4729 else if (vr->type == VR_VARYING)
4730 fprintf (file, "VARYING");
4731 else
4732 fprintf (file, "INVALID RANGE");
4736 /* Dump value range VR to stderr. */
4738 DEBUG_FUNCTION void
4739 debug_value_range (value_range *vr)
4741 dump_value_range (stderr, vr);
4742 fprintf (stderr, "\n");
4746 /* Dump value ranges of all SSA_NAMEs to FILE. */
4748 void
4749 dump_all_value_ranges (FILE *file)
4751 size_t i;
4753 for (i = 0; i < num_vr_values; i++)
4755 if (vr_value[i])
4757 print_generic_expr (file, ssa_name (i), 0);
4758 fprintf (file, ": ");
4759 dump_value_range (file, vr_value[i]);
4760 fprintf (file, "\n");
4764 fprintf (file, "\n");
4768 /* Dump all value ranges to stderr. */
4770 DEBUG_FUNCTION void
4771 debug_all_value_ranges (void)
4773 dump_all_value_ranges (stderr);
4777 /* Given a COND_EXPR COND of the form 'V OP W', and an SSA name V,
4778 create a new SSA name N and return the assertion assignment
4779 'N = ASSERT_EXPR <V, V OP W>'. */
4781 static gimple *
4782 build_assert_expr_for (tree cond, tree v)
4784 tree a;
4785 gassign *assertion;
4787 gcc_assert (TREE_CODE (v) == SSA_NAME
4788 && COMPARISON_CLASS_P (cond));
4790 a = build2 (ASSERT_EXPR, TREE_TYPE (v), v, cond);
4791 assertion = gimple_build_assign (NULL_TREE, a);
4793 /* The new ASSERT_EXPR, creates a new SSA name that replaces the
4794 operand of the ASSERT_EXPR. Create it so the new name and the old one
4795 are registered in the replacement table so that we can fix the SSA web
4796 after adding all the ASSERT_EXPRs. */
4797 create_new_def_for (v, assertion, NULL);
4799 return assertion;
4803 /* Return false if EXPR is a predicate expression involving floating
4804 point values. */
4806 static inline bool
4807 fp_predicate (gimple *stmt)
4809 GIMPLE_CHECK (stmt, GIMPLE_COND);
4811 return FLOAT_TYPE_P (TREE_TYPE (gimple_cond_lhs (stmt)));
4814 /* If the range of values taken by OP can be inferred after STMT executes,
4815 return the comparison code (COMP_CODE_P) and value (VAL_P) that
4816 describes the inferred range. Return true if a range could be
4817 inferred. */
4819 static bool
4820 infer_value_range (gimple *stmt, tree op, tree_code *comp_code_p, tree *val_p)
4822 *val_p = NULL_TREE;
4823 *comp_code_p = ERROR_MARK;
4825 /* Do not attempt to infer anything in names that flow through
4826 abnormal edges. */
4827 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op))
4828 return false;
4830 /* If STMT is the last statement of a basic block with no normal
4831 successors, there is no point inferring anything about any of its
4832 operands. We would not be able to find a proper insertion point
4833 for the assertion, anyway. */
4834 if (stmt_ends_bb_p (stmt))
4836 edge_iterator ei;
4837 edge e;
4839 FOR_EACH_EDGE (e, ei, gimple_bb (stmt)->succs)
4840 if (!(e->flags & (EDGE_ABNORMAL|EDGE_EH)))
4841 break;
4842 if (e == NULL)
4843 return false;
4846 if (infer_nonnull_range (stmt, op))
4848 *val_p = build_int_cst (TREE_TYPE (op), 0);
4849 *comp_code_p = NE_EXPR;
4850 return true;
4853 return false;
4857 void dump_asserts_for (FILE *, tree);
4858 void debug_asserts_for (tree);
4859 void dump_all_asserts (FILE *);
4860 void debug_all_asserts (void);
4862 /* Dump all the registered assertions for NAME to FILE. */
4864 void
4865 dump_asserts_for (FILE *file, tree name)
4867 assert_locus *loc;
4869 fprintf (file, "Assertions to be inserted for ");
4870 print_generic_expr (file, name, 0);
4871 fprintf (file, "\n");
4873 loc = asserts_for[SSA_NAME_VERSION (name)];
4874 while (loc)
4876 fprintf (file, "\t");
4877 print_gimple_stmt (file, gsi_stmt (loc->si), 0, 0);
4878 fprintf (file, "\n\tBB #%d", loc->bb->index);
4879 if (loc->e)
4881 fprintf (file, "\n\tEDGE %d->%d", loc->e->src->index,
4882 loc->e->dest->index);
4883 dump_edge_info (file, loc->e, dump_flags, 0);
4885 fprintf (file, "\n\tPREDICATE: ");
4886 print_generic_expr (file, loc->expr, 0);
4887 fprintf (file, " %s ", get_tree_code_name (loc->comp_code));
4888 print_generic_expr (file, loc->val, 0);
4889 fprintf (file, "\n\n");
4890 loc = loc->next;
4893 fprintf (file, "\n");
4897 /* Dump all the registered assertions for NAME to stderr. */
4899 DEBUG_FUNCTION void
4900 debug_asserts_for (tree name)
4902 dump_asserts_for (stderr, name);
4906 /* Dump all the registered assertions for all the names to FILE. */
4908 void
4909 dump_all_asserts (FILE *file)
4911 unsigned i;
4912 bitmap_iterator bi;
4914 fprintf (file, "\nASSERT_EXPRs to be inserted\n\n");
4915 EXECUTE_IF_SET_IN_BITMAP (need_assert_for, 0, i, bi)
4916 dump_asserts_for (file, ssa_name (i));
4917 fprintf (file, "\n");
4921 /* Dump all the registered assertions for all the names to stderr. */
4923 DEBUG_FUNCTION void
4924 debug_all_asserts (void)
4926 dump_all_asserts (stderr);
4930 /* If NAME doesn't have an ASSERT_EXPR registered for asserting
4931 'EXPR COMP_CODE VAL' at a location that dominates block BB or
4932 E->DEST, then register this location as a possible insertion point
4933 for ASSERT_EXPR <NAME, EXPR COMP_CODE VAL>.
4935 BB, E and SI provide the exact insertion point for the new
4936 ASSERT_EXPR. If BB is NULL, then the ASSERT_EXPR is to be inserted
4937 on edge E. Otherwise, if E is NULL, the ASSERT_EXPR is inserted on
4938 BB. If SI points to a COND_EXPR or a SWITCH_EXPR statement, then E
4939 must not be NULL. */
4941 static void
4942 register_new_assert_for (tree name, tree expr,
4943 enum tree_code comp_code,
4944 tree val,
4945 basic_block bb,
4946 edge e,
4947 gimple_stmt_iterator si)
4949 assert_locus *n, *loc, *last_loc;
4950 basic_block dest_bb;
4952 gcc_checking_assert (bb == NULL || e == NULL);
4954 if (e == NULL)
4955 gcc_checking_assert (gimple_code (gsi_stmt (si)) != GIMPLE_COND
4956 && gimple_code (gsi_stmt (si)) != GIMPLE_SWITCH);
4958 /* Never build an assert comparing against an integer constant with
4959 TREE_OVERFLOW set. This confuses our undefined overflow warning
4960 machinery. */
4961 if (TREE_OVERFLOW_P (val))
4962 val = drop_tree_overflow (val);
4964 /* The new assertion A will be inserted at BB or E. We need to
4965 determine if the new location is dominated by a previously
4966 registered location for A. If we are doing an edge insertion,
4967 assume that A will be inserted at E->DEST. Note that this is not
4968 necessarily true.
4970 If E is a critical edge, it will be split. But even if E is
4971 split, the new block will dominate the same set of blocks that
4972 E->DEST dominates.
4974 The reverse, however, is not true, blocks dominated by E->DEST
4975 will not be dominated by the new block created to split E. So,
4976 if the insertion location is on a critical edge, we will not use
4977 the new location to move another assertion previously registered
4978 at a block dominated by E->DEST. */
4979 dest_bb = (bb) ? bb : e->dest;
4981 /* If NAME already has an ASSERT_EXPR registered for COMP_CODE and
4982 VAL at a block dominating DEST_BB, then we don't need to insert a new
4983 one. Similarly, if the same assertion already exists at a block
4984 dominated by DEST_BB and the new location is not on a critical
4985 edge, then update the existing location for the assertion (i.e.,
4986 move the assertion up in the dominance tree).
4988 Note, this is implemented as a simple linked list because there
4989 should not be more than a handful of assertions registered per
4990 name. If this becomes a performance problem, a table hashed by
4991 COMP_CODE and VAL could be implemented. */
4992 loc = asserts_for[SSA_NAME_VERSION (name)];
4993 last_loc = loc;
4994 while (loc)
4996 if (loc->comp_code == comp_code
4997 && (loc->val == val
4998 || operand_equal_p (loc->val, val, 0))
4999 && (loc->expr == expr
5000 || operand_equal_p (loc->expr, expr, 0)))
5002 /* If E is not a critical edge and DEST_BB
5003 dominates the existing location for the assertion, move
5004 the assertion up in the dominance tree by updating its
5005 location information. */
5006 if ((e == NULL || !EDGE_CRITICAL_P (e))
5007 && dominated_by_p (CDI_DOMINATORS, loc->bb, dest_bb))
5009 loc->bb = dest_bb;
5010 loc->e = e;
5011 loc->si = si;
5012 return;
5016 /* Update the last node of the list and move to the next one. */
5017 last_loc = loc;
5018 loc = loc->next;
5021 /* If we didn't find an assertion already registered for
5022 NAME COMP_CODE VAL, add a new one at the end of the list of
5023 assertions associated with NAME. */
5024 n = XNEW (struct assert_locus);
5025 n->bb = dest_bb;
5026 n->e = e;
5027 n->si = si;
5028 n->comp_code = comp_code;
5029 n->val = val;
5030 n->expr = expr;
5031 n->next = NULL;
5033 if (last_loc)
5034 last_loc->next = n;
5035 else
5036 asserts_for[SSA_NAME_VERSION (name)] = n;
5038 bitmap_set_bit (need_assert_for, SSA_NAME_VERSION (name));
5041 /* (COND_OP0 COND_CODE COND_OP1) is a predicate which uses NAME.
5042 Extract a suitable test code and value and store them into *CODE_P and
5043 *VAL_P so the predicate is normalized to NAME *CODE_P *VAL_P.
5045 If no extraction was possible, return FALSE, otherwise return TRUE.
5047 If INVERT is true, then we invert the result stored into *CODE_P. */
5049 static bool
5050 extract_code_and_val_from_cond_with_ops (tree name, enum tree_code cond_code,
5051 tree cond_op0, tree cond_op1,
5052 bool invert, enum tree_code *code_p,
5053 tree *val_p)
5055 enum tree_code comp_code;
5056 tree val;
5058 /* Otherwise, we have a comparison of the form NAME COMP VAL
5059 or VAL COMP NAME. */
5060 if (name == cond_op1)
5062 /* If the predicate is of the form VAL COMP NAME, flip
5063 COMP around because we need to register NAME as the
5064 first operand in the predicate. */
5065 comp_code = swap_tree_comparison (cond_code);
5066 val = cond_op0;
5068 else if (name == cond_op0)
5070 /* The comparison is of the form NAME COMP VAL, so the
5071 comparison code remains unchanged. */
5072 comp_code = cond_code;
5073 val = cond_op1;
5075 else
5076 gcc_unreachable ();
5078 /* Invert the comparison code as necessary. */
5079 if (invert)
5080 comp_code = invert_tree_comparison (comp_code, 0);
5082 /* VRP only handles integral and pointer types. */
5083 if (! INTEGRAL_TYPE_P (TREE_TYPE (val))
5084 && ! POINTER_TYPE_P (TREE_TYPE (val)))
5085 return false;
5087 /* Do not register always-false predicates.
5088 FIXME: this works around a limitation in fold() when dealing with
5089 enumerations. Given 'enum { N1, N2 } x;', fold will not
5090 fold 'if (x > N2)' to 'if (0)'. */
5091 if ((comp_code == GT_EXPR || comp_code == LT_EXPR)
5092 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
5094 tree min = TYPE_MIN_VALUE (TREE_TYPE (val));
5095 tree max = TYPE_MAX_VALUE (TREE_TYPE (val));
5097 if (comp_code == GT_EXPR
5098 && (!max
5099 || compare_values (val, max) == 0))
5100 return false;
5102 if (comp_code == LT_EXPR
5103 && (!min
5104 || compare_values (val, min) == 0))
5105 return false;
5107 *code_p = comp_code;
5108 *val_p = val;
5109 return true;
5112 /* Find out smallest RES where RES > VAL && (RES & MASK) == RES, if any
5113 (otherwise return VAL). VAL and MASK must be zero-extended for
5114 precision PREC. If SGNBIT is non-zero, first xor VAL with SGNBIT
5115 (to transform signed values into unsigned) and at the end xor
5116 SGNBIT back. */
5118 static wide_int
5119 masked_increment (const wide_int &val_in, const wide_int &mask,
5120 const wide_int &sgnbit, unsigned int prec)
5122 wide_int bit = wi::one (prec), res;
5123 unsigned int i;
5125 wide_int val = val_in ^ sgnbit;
5126 for (i = 0; i < prec; i++, bit += bit)
5128 res = mask;
5129 if ((res & bit) == 0)
5130 continue;
5131 res = bit - 1;
5132 res = (val + bit).and_not (res);
5133 res &= mask;
5134 if (wi::gtu_p (res, val))
5135 return res ^ sgnbit;
5137 return val ^ sgnbit;
5140 /* Try to register an edge assertion for SSA name NAME on edge E for
5141 the condition COND contributing to the conditional jump pointed to by BSI.
5142 Invert the condition COND if INVERT is true. */
5144 static void
5145 register_edge_assert_for_2 (tree name, edge e, gimple_stmt_iterator bsi,
5146 enum tree_code cond_code,
5147 tree cond_op0, tree cond_op1, bool invert)
5149 tree val;
5150 enum tree_code comp_code;
5152 if (!extract_code_and_val_from_cond_with_ops (name, cond_code,
5153 cond_op0,
5154 cond_op1,
5155 invert, &comp_code, &val))
5156 return;
5158 /* Only register an ASSERT_EXPR if NAME was found in the sub-graph
5159 reachable from E. */
5160 if (live_on_edge (e, name))
5161 register_new_assert_for (name, name, comp_code, val, NULL, e, bsi);
5163 /* In the case of NAME <= CST and NAME being defined as
5164 NAME = (unsigned) NAME2 + CST2 we can assert NAME2 >= -CST2
5165 and NAME2 <= CST - CST2. We can do the same for NAME > CST.
5166 This catches range and anti-range tests. */
5167 if ((comp_code == LE_EXPR
5168 || comp_code == GT_EXPR)
5169 && TREE_CODE (val) == INTEGER_CST
5170 && TYPE_UNSIGNED (TREE_TYPE (val)))
5172 gimple *def_stmt = SSA_NAME_DEF_STMT (name);
5173 tree cst2 = NULL_TREE, name2 = NULL_TREE, name3 = NULL_TREE;
5175 /* Extract CST2 from the (optional) addition. */
5176 if (is_gimple_assign (def_stmt)
5177 && gimple_assign_rhs_code (def_stmt) == PLUS_EXPR)
5179 name2 = gimple_assign_rhs1 (def_stmt);
5180 cst2 = gimple_assign_rhs2 (def_stmt);
5181 if (TREE_CODE (name2) == SSA_NAME
5182 && TREE_CODE (cst2) == INTEGER_CST)
5183 def_stmt = SSA_NAME_DEF_STMT (name2);
5186 /* Extract NAME2 from the (optional) sign-changing cast. */
5187 if (gimple_assign_cast_p (def_stmt))
5189 if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt))
5190 && ! TYPE_UNSIGNED (TREE_TYPE (gimple_assign_rhs1 (def_stmt)))
5191 && (TYPE_PRECISION (gimple_expr_type (def_stmt))
5192 == TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt)))))
5193 name3 = gimple_assign_rhs1 (def_stmt);
5196 /* If name3 is used later, create an ASSERT_EXPR for it. */
5197 if (name3 != NULL_TREE
5198 && TREE_CODE (name3) == SSA_NAME
5199 && (cst2 == NULL_TREE
5200 || TREE_CODE (cst2) == INTEGER_CST)
5201 && INTEGRAL_TYPE_P (TREE_TYPE (name3))
5202 && live_on_edge (e, name3))
5204 tree tmp;
5206 /* Build an expression for the range test. */
5207 tmp = build1 (NOP_EXPR, TREE_TYPE (name), name3);
5208 if (cst2 != NULL_TREE)
5209 tmp = build2 (PLUS_EXPR, TREE_TYPE (name), tmp, cst2);
5211 if (dump_file)
5213 fprintf (dump_file, "Adding assert for ");
5214 print_generic_expr (dump_file, name3, 0);
5215 fprintf (dump_file, " from ");
5216 print_generic_expr (dump_file, tmp, 0);
5217 fprintf (dump_file, "\n");
5220 register_new_assert_for (name3, tmp, comp_code, val, NULL, e, bsi);
5223 /* If name2 is used later, create an ASSERT_EXPR for it. */
5224 if (name2 != NULL_TREE
5225 && TREE_CODE (name2) == SSA_NAME
5226 && TREE_CODE (cst2) == INTEGER_CST
5227 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
5228 && live_on_edge (e, name2))
5230 tree tmp;
5232 /* Build an expression for the range test. */
5233 tmp = name2;
5234 if (TREE_TYPE (name) != TREE_TYPE (name2))
5235 tmp = build1 (NOP_EXPR, TREE_TYPE (name), tmp);
5236 if (cst2 != NULL_TREE)
5237 tmp = build2 (PLUS_EXPR, TREE_TYPE (name), tmp, cst2);
5239 if (dump_file)
5241 fprintf (dump_file, "Adding assert for ");
5242 print_generic_expr (dump_file, name2, 0);
5243 fprintf (dump_file, " from ");
5244 print_generic_expr (dump_file, tmp, 0);
5245 fprintf (dump_file, "\n");
5248 register_new_assert_for (name2, tmp, comp_code, val, NULL, e, bsi);
5252 /* In the case of post-in/decrement tests like if (i++) ... and uses
5253 of the in/decremented value on the edge the extra name we want to
5254 assert for is not on the def chain of the name compared. Instead
5255 it is in the set of use stmts.
5256 Similar cases happen for conversions that were simplified through
5257 fold_{sign_changed,widened}_comparison. */
5258 if ((comp_code == NE_EXPR
5259 || comp_code == EQ_EXPR)
5260 && TREE_CODE (val) == INTEGER_CST)
5262 imm_use_iterator ui;
5263 gimple *use_stmt;
5264 FOR_EACH_IMM_USE_STMT (use_stmt, ui, name)
5266 if (!is_gimple_assign (use_stmt))
5267 continue;
5269 /* Cut off to use-stmts that are dominating the predecessor. */
5270 if (!dominated_by_p (CDI_DOMINATORS, e->src, gimple_bb (use_stmt)))
5271 continue;
5273 tree name2 = gimple_assign_lhs (use_stmt);
5274 if (TREE_CODE (name2) != SSA_NAME
5275 || !live_on_edge (e, name2))
5276 continue;
5278 enum tree_code code = gimple_assign_rhs_code (use_stmt);
5279 tree cst;
5280 if (code == PLUS_EXPR
5281 || code == MINUS_EXPR)
5283 cst = gimple_assign_rhs2 (use_stmt);
5284 if (TREE_CODE (cst) != INTEGER_CST)
5285 continue;
5286 cst = int_const_binop (code, val, cst);
5288 else if (CONVERT_EXPR_CODE_P (code))
5290 /* For truncating conversions we cannot record
5291 an inequality. */
5292 if (comp_code == NE_EXPR
5293 && (TYPE_PRECISION (TREE_TYPE (name2))
5294 < TYPE_PRECISION (TREE_TYPE (name))))
5295 continue;
5296 cst = fold_convert (TREE_TYPE (name2), val);
5298 else
5299 continue;
5301 if (TREE_OVERFLOW_P (cst))
5302 cst = drop_tree_overflow (cst);
5303 register_new_assert_for (name2, name2, comp_code, cst,
5304 NULL, e, bsi);
5308 if (TREE_CODE_CLASS (comp_code) == tcc_comparison
5309 && TREE_CODE (val) == INTEGER_CST)
5311 gimple *def_stmt = SSA_NAME_DEF_STMT (name);
5312 tree name2 = NULL_TREE, names[2], cst2 = NULL_TREE;
5313 tree val2 = NULL_TREE;
5314 unsigned int prec = TYPE_PRECISION (TREE_TYPE (val));
5315 wide_int mask = wi::zero (prec);
5316 unsigned int nprec = prec;
5317 enum tree_code rhs_code = ERROR_MARK;
5319 if (is_gimple_assign (def_stmt))
5320 rhs_code = gimple_assign_rhs_code (def_stmt);
5322 /* In the case of NAME != CST1 where NAME = A +- CST2 we can
5323 assert that A != CST1 -+ CST2. */
5324 if ((comp_code == EQ_EXPR || comp_code == NE_EXPR)
5325 && (rhs_code == PLUS_EXPR || rhs_code == MINUS_EXPR))
5327 tree op0 = gimple_assign_rhs1 (def_stmt);
5328 tree op1 = gimple_assign_rhs2 (def_stmt);
5329 if (TREE_CODE (op0) == SSA_NAME
5330 && TREE_CODE (op1) == INTEGER_CST
5331 && live_on_edge (e, op0))
5333 enum tree_code reverse_op = (rhs_code == PLUS_EXPR
5334 ? MINUS_EXPR : PLUS_EXPR);
5335 op1 = int_const_binop (reverse_op, val, op1);
5336 if (TREE_OVERFLOW (op1))
5337 op1 = drop_tree_overflow (op1);
5338 register_new_assert_for (op0, op0, comp_code, op1, NULL, e, bsi);
5342 /* Add asserts for NAME cmp CST and NAME being defined
5343 as NAME = (int) NAME2. */
5344 if (!TYPE_UNSIGNED (TREE_TYPE (val))
5345 && (comp_code == LE_EXPR || comp_code == LT_EXPR
5346 || comp_code == GT_EXPR || comp_code == GE_EXPR)
5347 && gimple_assign_cast_p (def_stmt))
5349 name2 = gimple_assign_rhs1 (def_stmt);
5350 if (CONVERT_EXPR_CODE_P (rhs_code)
5351 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
5352 && TYPE_UNSIGNED (TREE_TYPE (name2))
5353 && prec == TYPE_PRECISION (TREE_TYPE (name2))
5354 && (comp_code == LE_EXPR || comp_code == GT_EXPR
5355 || !tree_int_cst_equal (val,
5356 TYPE_MIN_VALUE (TREE_TYPE (val))))
5357 && live_on_edge (e, name2))
5359 tree tmp, cst;
5360 enum tree_code new_comp_code = comp_code;
5362 cst = fold_convert (TREE_TYPE (name2),
5363 TYPE_MIN_VALUE (TREE_TYPE (val)));
5364 /* Build an expression for the range test. */
5365 tmp = build2 (PLUS_EXPR, TREE_TYPE (name2), name2, cst);
5366 cst = fold_build2 (PLUS_EXPR, TREE_TYPE (name2), cst,
5367 fold_convert (TREE_TYPE (name2), val));
5368 if (comp_code == LT_EXPR || comp_code == GE_EXPR)
5370 new_comp_code = comp_code == LT_EXPR ? LE_EXPR : GT_EXPR;
5371 cst = fold_build2 (MINUS_EXPR, TREE_TYPE (name2), cst,
5372 build_int_cst (TREE_TYPE (name2), 1));
5375 if (dump_file)
5377 fprintf (dump_file, "Adding assert for ");
5378 print_generic_expr (dump_file, name2, 0);
5379 fprintf (dump_file, " from ");
5380 print_generic_expr (dump_file, tmp, 0);
5381 fprintf (dump_file, "\n");
5384 register_new_assert_for (name2, tmp, new_comp_code, cst, NULL,
5385 e, bsi);
5389 /* Add asserts for NAME cmp CST and NAME being defined as
5390 NAME = NAME2 >> CST2.
5392 Extract CST2 from the right shift. */
5393 if (rhs_code == RSHIFT_EXPR)
5395 name2 = gimple_assign_rhs1 (def_stmt);
5396 cst2 = gimple_assign_rhs2 (def_stmt);
5397 if (TREE_CODE (name2) == SSA_NAME
5398 && tree_fits_uhwi_p (cst2)
5399 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
5400 && IN_RANGE (tree_to_uhwi (cst2), 1, prec - 1)
5401 && prec == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (val)))
5402 && live_on_edge (e, name2))
5404 mask = wi::mask (tree_to_uhwi (cst2), false, prec);
5405 val2 = fold_binary (LSHIFT_EXPR, TREE_TYPE (val), val, cst2);
5408 if (val2 != NULL_TREE
5409 && TREE_CODE (val2) == INTEGER_CST
5410 && simple_cst_equal (fold_build2 (RSHIFT_EXPR,
5411 TREE_TYPE (val),
5412 val2, cst2), val))
5414 enum tree_code new_comp_code = comp_code;
5415 tree tmp, new_val;
5417 tmp = name2;
5418 if (comp_code == EQ_EXPR || comp_code == NE_EXPR)
5420 if (!TYPE_UNSIGNED (TREE_TYPE (val)))
5422 tree type = build_nonstandard_integer_type (prec, 1);
5423 tmp = build1 (NOP_EXPR, type, name2);
5424 val2 = fold_convert (type, val2);
5426 tmp = fold_build2 (MINUS_EXPR, TREE_TYPE (tmp), tmp, val2);
5427 new_val = wide_int_to_tree (TREE_TYPE (tmp), mask);
5428 new_comp_code = comp_code == EQ_EXPR ? LE_EXPR : GT_EXPR;
5430 else if (comp_code == LT_EXPR || comp_code == GE_EXPR)
5432 wide_int minval
5433 = wi::min_value (prec, TYPE_SIGN (TREE_TYPE (val)));
5434 new_val = val2;
5435 if (minval == new_val)
5436 new_val = NULL_TREE;
5438 else
5440 wide_int maxval
5441 = wi::max_value (prec, TYPE_SIGN (TREE_TYPE (val)));
5442 mask |= val2;
5443 if (mask == maxval)
5444 new_val = NULL_TREE;
5445 else
5446 new_val = wide_int_to_tree (TREE_TYPE (val2), mask);
5449 if (new_val)
5451 if (dump_file)
5453 fprintf (dump_file, "Adding assert for ");
5454 print_generic_expr (dump_file, name2, 0);
5455 fprintf (dump_file, " from ");
5456 print_generic_expr (dump_file, tmp, 0);
5457 fprintf (dump_file, "\n");
5460 register_new_assert_for (name2, tmp, new_comp_code, new_val,
5461 NULL, e, bsi);
5465 /* Add asserts for NAME cmp CST and NAME being defined as
5466 NAME = NAME2 & CST2.
5468 Extract CST2 from the and.
5470 Also handle
5471 NAME = (unsigned) NAME2;
5472 casts where NAME's type is unsigned and has smaller precision
5473 than NAME2's type as if it was NAME = NAME2 & MASK. */
5474 names[0] = NULL_TREE;
5475 names[1] = NULL_TREE;
5476 cst2 = NULL_TREE;
5477 if (rhs_code == BIT_AND_EXPR
5478 || (CONVERT_EXPR_CODE_P (rhs_code)
5479 && INTEGRAL_TYPE_P (TREE_TYPE (val))
5480 && TYPE_UNSIGNED (TREE_TYPE (val))
5481 && TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt)))
5482 > prec))
5484 name2 = gimple_assign_rhs1 (def_stmt);
5485 if (rhs_code == BIT_AND_EXPR)
5486 cst2 = gimple_assign_rhs2 (def_stmt);
5487 else
5489 cst2 = TYPE_MAX_VALUE (TREE_TYPE (val));
5490 nprec = TYPE_PRECISION (TREE_TYPE (name2));
5492 if (TREE_CODE (name2) == SSA_NAME
5493 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
5494 && TREE_CODE (cst2) == INTEGER_CST
5495 && !integer_zerop (cst2)
5496 && (nprec > 1
5497 || TYPE_UNSIGNED (TREE_TYPE (val))))
5499 gimple *def_stmt2 = SSA_NAME_DEF_STMT (name2);
5500 if (gimple_assign_cast_p (def_stmt2))
5502 names[1] = gimple_assign_rhs1 (def_stmt2);
5503 if (!CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt2))
5504 || !INTEGRAL_TYPE_P (TREE_TYPE (names[1]))
5505 || (TYPE_PRECISION (TREE_TYPE (name2))
5506 != TYPE_PRECISION (TREE_TYPE (names[1])))
5507 || !live_on_edge (e, names[1]))
5508 names[1] = NULL_TREE;
5510 if (live_on_edge (e, name2))
5511 names[0] = name2;
5514 if (names[0] || names[1])
5516 wide_int minv, maxv, valv, cst2v;
5517 wide_int tem, sgnbit;
5518 bool valid_p = false, valn, cst2n;
5519 enum tree_code ccode = comp_code;
5521 valv = wide_int::from (val, nprec, UNSIGNED);
5522 cst2v = wide_int::from (cst2, nprec, UNSIGNED);
5523 valn = wi::neg_p (valv, TYPE_SIGN (TREE_TYPE (val)));
5524 cst2n = wi::neg_p (cst2v, TYPE_SIGN (TREE_TYPE (val)));
5525 /* If CST2 doesn't have most significant bit set,
5526 but VAL is negative, we have comparison like
5527 if ((x & 0x123) > -4) (always true). Just give up. */
5528 if (!cst2n && valn)
5529 ccode = ERROR_MARK;
5530 if (cst2n)
5531 sgnbit = wi::set_bit_in_zero (nprec - 1, nprec);
5532 else
5533 sgnbit = wi::zero (nprec);
5534 minv = valv & cst2v;
5535 switch (ccode)
5537 case EQ_EXPR:
5538 /* Minimum unsigned value for equality is VAL & CST2
5539 (should be equal to VAL, otherwise we probably should
5540 have folded the comparison into false) and
5541 maximum unsigned value is VAL | ~CST2. */
5542 maxv = valv | ~cst2v;
5543 valid_p = true;
5544 break;
5546 case NE_EXPR:
5547 tem = valv | ~cst2v;
5548 /* If VAL is 0, handle (X & CST2) != 0 as (X & CST2) > 0U. */
5549 if (valv == 0)
5551 cst2n = false;
5552 sgnbit = wi::zero (nprec);
5553 goto gt_expr;
5555 /* If (VAL | ~CST2) is all ones, handle it as
5556 (X & CST2) < VAL. */
5557 if (tem == -1)
5559 cst2n = false;
5560 valn = false;
5561 sgnbit = wi::zero (nprec);
5562 goto lt_expr;
5564 if (!cst2n && wi::neg_p (cst2v))
5565 sgnbit = wi::set_bit_in_zero (nprec - 1, nprec);
5566 if (sgnbit != 0)
5568 if (valv == sgnbit)
5570 cst2n = true;
5571 valn = true;
5572 goto gt_expr;
5574 if (tem == wi::mask (nprec - 1, false, nprec))
5576 cst2n = true;
5577 goto lt_expr;
5579 if (!cst2n)
5580 sgnbit = wi::zero (nprec);
5582 break;
5584 case GE_EXPR:
5585 /* Minimum unsigned value for >= if (VAL & CST2) == VAL
5586 is VAL and maximum unsigned value is ~0. For signed
5587 comparison, if CST2 doesn't have most significant bit
5588 set, handle it similarly. If CST2 has MSB set,
5589 the minimum is the same, and maximum is ~0U/2. */
5590 if (minv != valv)
5592 /* If (VAL & CST2) != VAL, X & CST2 can't be equal to
5593 VAL. */
5594 minv = masked_increment (valv, cst2v, sgnbit, nprec);
5595 if (minv == valv)
5596 break;
5598 maxv = wi::mask (nprec - (cst2n ? 1 : 0), false, nprec);
5599 valid_p = true;
5600 break;
5602 case GT_EXPR:
5603 gt_expr:
5604 /* Find out smallest MINV where MINV > VAL
5605 && (MINV & CST2) == MINV, if any. If VAL is signed and
5606 CST2 has MSB set, compute it biased by 1 << (nprec - 1). */
5607 minv = masked_increment (valv, cst2v, sgnbit, nprec);
5608 if (minv == valv)
5609 break;
5610 maxv = wi::mask (nprec - (cst2n ? 1 : 0), false, nprec);
5611 valid_p = true;
5612 break;
5614 case LE_EXPR:
5615 /* Minimum unsigned value for <= is 0 and maximum
5616 unsigned value is VAL | ~CST2 if (VAL & CST2) == VAL.
5617 Otherwise, find smallest VAL2 where VAL2 > VAL
5618 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
5619 as maximum.
5620 For signed comparison, if CST2 doesn't have most
5621 significant bit set, handle it similarly. If CST2 has
5622 MSB set, the maximum is the same and minimum is INT_MIN. */
5623 if (minv == valv)
5624 maxv = valv;
5625 else
5627 maxv = masked_increment (valv, cst2v, sgnbit, nprec);
5628 if (maxv == valv)
5629 break;
5630 maxv -= 1;
5632 maxv |= ~cst2v;
5633 minv = sgnbit;
5634 valid_p = true;
5635 break;
5637 case LT_EXPR:
5638 lt_expr:
5639 /* Minimum unsigned value for < is 0 and maximum
5640 unsigned value is (VAL-1) | ~CST2 if (VAL & CST2) == VAL.
5641 Otherwise, find smallest VAL2 where VAL2 > VAL
5642 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
5643 as maximum.
5644 For signed comparison, if CST2 doesn't have most
5645 significant bit set, handle it similarly. If CST2 has
5646 MSB set, the maximum is the same and minimum is INT_MIN. */
5647 if (minv == valv)
5649 if (valv == sgnbit)
5650 break;
5651 maxv = valv;
5653 else
5655 maxv = masked_increment (valv, cst2v, sgnbit, nprec);
5656 if (maxv == valv)
5657 break;
5659 maxv -= 1;
5660 maxv |= ~cst2v;
5661 minv = sgnbit;
5662 valid_p = true;
5663 break;
5665 default:
5666 break;
5668 if (valid_p
5669 && (maxv - minv) != -1)
5671 tree tmp, new_val, type;
5672 int i;
5674 for (i = 0; i < 2; i++)
5675 if (names[i])
5677 wide_int maxv2 = maxv;
5678 tmp = names[i];
5679 type = TREE_TYPE (names[i]);
5680 if (!TYPE_UNSIGNED (type))
5682 type = build_nonstandard_integer_type (nprec, 1);
5683 tmp = build1 (NOP_EXPR, type, names[i]);
5685 if (minv != 0)
5687 tmp = build2 (PLUS_EXPR, type, tmp,
5688 wide_int_to_tree (type, -minv));
5689 maxv2 = maxv - minv;
5691 new_val = wide_int_to_tree (type, maxv2);
5693 if (dump_file)
5695 fprintf (dump_file, "Adding assert for ");
5696 print_generic_expr (dump_file, names[i], 0);
5697 fprintf (dump_file, " from ");
5698 print_generic_expr (dump_file, tmp, 0);
5699 fprintf (dump_file, "\n");
5702 register_new_assert_for (names[i], tmp, LE_EXPR,
5703 new_val, NULL, e, bsi);
5710 /* OP is an operand of a truth value expression which is known to have
5711 a particular value. Register any asserts for OP and for any
5712 operands in OP's defining statement.
5714 If CODE is EQ_EXPR, then we want to register OP is zero (false),
5715 if CODE is NE_EXPR, then we want to register OP is nonzero (true). */
5717 static void
5718 register_edge_assert_for_1 (tree op, enum tree_code code,
5719 edge e, gimple_stmt_iterator bsi)
5721 gimple *op_def;
5722 tree val;
5723 enum tree_code rhs_code;
5725 /* We only care about SSA_NAMEs. */
5726 if (TREE_CODE (op) != SSA_NAME)
5727 return;
5729 /* We know that OP will have a zero or nonzero value. If OP is used
5730 more than once go ahead and register an assert for OP. */
5731 if (live_on_edge (e, op))
5733 val = build_int_cst (TREE_TYPE (op), 0);
5734 register_new_assert_for (op, op, code, val, NULL, e, bsi);
5737 /* Now look at how OP is set. If it's set from a comparison,
5738 a truth operation or some bit operations, then we may be able
5739 to register information about the operands of that assignment. */
5740 op_def = SSA_NAME_DEF_STMT (op);
5741 if (gimple_code (op_def) != GIMPLE_ASSIGN)
5742 return;
5744 rhs_code = gimple_assign_rhs_code (op_def);
5746 if (TREE_CODE_CLASS (rhs_code) == tcc_comparison)
5748 bool invert = (code == EQ_EXPR ? true : false);
5749 tree op0 = gimple_assign_rhs1 (op_def);
5750 tree op1 = gimple_assign_rhs2 (op_def);
5752 if (TREE_CODE (op0) == SSA_NAME)
5753 register_edge_assert_for_2 (op0, e, bsi, rhs_code, op0, op1, invert);
5754 if (TREE_CODE (op1) == SSA_NAME)
5755 register_edge_assert_for_2 (op1, e, bsi, rhs_code, op0, op1, invert);
5757 else if ((code == NE_EXPR
5758 && gimple_assign_rhs_code (op_def) == BIT_AND_EXPR)
5759 || (code == EQ_EXPR
5760 && gimple_assign_rhs_code (op_def) == BIT_IOR_EXPR))
5762 /* Recurse on each operand. */
5763 tree op0 = gimple_assign_rhs1 (op_def);
5764 tree op1 = gimple_assign_rhs2 (op_def);
5765 if (TREE_CODE (op0) == SSA_NAME
5766 && has_single_use (op0))
5767 register_edge_assert_for_1 (op0, code, e, bsi);
5768 if (TREE_CODE (op1) == SSA_NAME
5769 && has_single_use (op1))
5770 register_edge_assert_for_1 (op1, code, e, bsi);
5772 else if (gimple_assign_rhs_code (op_def) == BIT_NOT_EXPR
5773 && TYPE_PRECISION (TREE_TYPE (gimple_assign_lhs (op_def))) == 1)
5775 /* Recurse, flipping CODE. */
5776 code = invert_tree_comparison (code, false);
5777 register_edge_assert_for_1 (gimple_assign_rhs1 (op_def), code, e, bsi);
5779 else if (gimple_assign_rhs_code (op_def) == SSA_NAME)
5781 /* Recurse through the copy. */
5782 register_edge_assert_for_1 (gimple_assign_rhs1 (op_def), code, e, bsi);
5784 else if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (op_def)))
5786 /* Recurse through the type conversion, unless it is a narrowing
5787 conversion or conversion from non-integral type. */
5788 tree rhs = gimple_assign_rhs1 (op_def);
5789 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs))
5790 && (TYPE_PRECISION (TREE_TYPE (rhs))
5791 <= TYPE_PRECISION (TREE_TYPE (op))))
5792 register_edge_assert_for_1 (rhs, code, e, bsi);
5796 /* Try to register an edge assertion for SSA name NAME on edge E for
5797 the condition COND contributing to the conditional jump pointed to by
5798 SI. */
5800 static void
5801 register_edge_assert_for (tree name, edge e, gimple_stmt_iterator si,
5802 enum tree_code cond_code, tree cond_op0,
5803 tree cond_op1)
5805 tree val;
5806 enum tree_code comp_code;
5807 bool is_else_edge = (e->flags & EDGE_FALSE_VALUE) != 0;
5809 /* Do not attempt to infer anything in names that flow through
5810 abnormal edges. */
5811 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name))
5812 return;
5814 if (!extract_code_and_val_from_cond_with_ops (name, cond_code,
5815 cond_op0, cond_op1,
5816 is_else_edge,
5817 &comp_code, &val))
5818 return;
5820 /* Register ASSERT_EXPRs for name. */
5821 register_edge_assert_for_2 (name, e, si, cond_code, cond_op0,
5822 cond_op1, is_else_edge);
5825 /* If COND is effectively an equality test of an SSA_NAME against
5826 the value zero or one, then we may be able to assert values
5827 for SSA_NAMEs which flow into COND. */
5829 /* In the case of NAME == 1 or NAME != 0, for BIT_AND_EXPR defining
5830 statement of NAME we can assert both operands of the BIT_AND_EXPR
5831 have nonzero value. */
5832 if (((comp_code == EQ_EXPR && integer_onep (val))
5833 || (comp_code == NE_EXPR && integer_zerop (val))))
5835 gimple *def_stmt = SSA_NAME_DEF_STMT (name);
5837 if (is_gimple_assign (def_stmt)
5838 && gimple_assign_rhs_code (def_stmt) == BIT_AND_EXPR)
5840 tree op0 = gimple_assign_rhs1 (def_stmt);
5841 tree op1 = gimple_assign_rhs2 (def_stmt);
5842 register_edge_assert_for_1 (op0, NE_EXPR, e, si);
5843 register_edge_assert_for_1 (op1, NE_EXPR, e, si);
5847 /* In the case of NAME == 0 or NAME != 1, for BIT_IOR_EXPR defining
5848 statement of NAME we can assert both operands of the BIT_IOR_EXPR
5849 have zero value. */
5850 if (((comp_code == EQ_EXPR && integer_zerop (val))
5851 || (comp_code == NE_EXPR && integer_onep (val))))
5853 gimple *def_stmt = SSA_NAME_DEF_STMT (name);
5855 /* For BIT_IOR_EXPR only if NAME == 0 both operands have
5856 necessarily zero value, or if type-precision is one. */
5857 if (is_gimple_assign (def_stmt)
5858 && (gimple_assign_rhs_code (def_stmt) == BIT_IOR_EXPR
5859 && (TYPE_PRECISION (TREE_TYPE (name)) == 1
5860 || comp_code == EQ_EXPR)))
5862 tree op0 = gimple_assign_rhs1 (def_stmt);
5863 tree op1 = gimple_assign_rhs2 (def_stmt);
5864 register_edge_assert_for_1 (op0, EQ_EXPR, e, si);
5865 register_edge_assert_for_1 (op1, EQ_EXPR, e, si);
5871 /* Determine whether the outgoing edges of BB should receive an
5872 ASSERT_EXPR for each of the operands of BB's LAST statement.
5873 The last statement of BB must be a COND_EXPR.
5875 If any of the sub-graphs rooted at BB have an interesting use of
5876 the predicate operands, an assert location node is added to the
5877 list of assertions for the corresponding operands. */
5879 static void
5880 find_conditional_asserts (basic_block bb, gcond *last)
5882 gimple_stmt_iterator bsi;
5883 tree op;
5884 edge_iterator ei;
5885 edge e;
5886 ssa_op_iter iter;
5888 bsi = gsi_for_stmt (last);
5890 /* Look for uses of the operands in each of the sub-graphs
5891 rooted at BB. We need to check each of the outgoing edges
5892 separately, so that we know what kind of ASSERT_EXPR to
5893 insert. */
5894 FOR_EACH_EDGE (e, ei, bb->succs)
5896 if (e->dest == bb)
5897 continue;
5899 /* Register the necessary assertions for each operand in the
5900 conditional predicate. */
5901 FOR_EACH_SSA_TREE_OPERAND (op, last, iter, SSA_OP_USE)
5902 register_edge_assert_for (op, e, bsi,
5903 gimple_cond_code (last),
5904 gimple_cond_lhs (last),
5905 gimple_cond_rhs (last));
5909 struct case_info
5911 tree expr;
5912 basic_block bb;
5915 /* Compare two case labels sorting first by the destination bb index
5916 and then by the case value. */
5918 static int
5919 compare_case_labels (const void *p1, const void *p2)
5921 const struct case_info *ci1 = (const struct case_info *) p1;
5922 const struct case_info *ci2 = (const struct case_info *) p2;
5923 int idx1 = ci1->bb->index;
5924 int idx2 = ci2->bb->index;
5926 if (idx1 < idx2)
5927 return -1;
5928 else if (idx1 == idx2)
5930 /* Make sure the default label is first in a group. */
5931 if (!CASE_LOW (ci1->expr))
5932 return -1;
5933 else if (!CASE_LOW (ci2->expr))
5934 return 1;
5935 else
5936 return tree_int_cst_compare (CASE_LOW (ci1->expr),
5937 CASE_LOW (ci2->expr));
5939 else
5940 return 1;
5943 /* Determine whether the outgoing edges of BB should receive an
5944 ASSERT_EXPR for each of the operands of BB's LAST statement.
5945 The last statement of BB must be a SWITCH_EXPR.
5947 If any of the sub-graphs rooted at BB have an interesting use of
5948 the predicate operands, an assert location node is added to the
5949 list of assertions for the corresponding operands. */
5951 static void
5952 find_switch_asserts (basic_block bb, gswitch *last)
5954 gimple_stmt_iterator bsi;
5955 tree op;
5956 edge e;
5957 struct case_info *ci;
5958 size_t n = gimple_switch_num_labels (last);
5959 #if GCC_VERSION >= 4000
5960 unsigned int idx;
5961 #else
5962 /* Work around GCC 3.4 bug (PR 37086). */
5963 volatile unsigned int idx;
5964 #endif
5966 bsi = gsi_for_stmt (last);
5967 op = gimple_switch_index (last);
5968 if (TREE_CODE (op) != SSA_NAME)
5969 return;
5971 /* Build a vector of case labels sorted by destination label. */
5972 ci = XNEWVEC (struct case_info, n);
5973 for (idx = 0; idx < n; ++idx)
5975 ci[idx].expr = gimple_switch_label (last, idx);
5976 ci[idx].bb = label_to_block (CASE_LABEL (ci[idx].expr));
5978 edge default_edge = find_edge (bb, ci[0].bb);
5979 qsort (ci, n, sizeof (struct case_info), compare_case_labels);
5981 for (idx = 0; idx < n; ++idx)
5983 tree min, max;
5984 tree cl = ci[idx].expr;
5985 basic_block cbb = ci[idx].bb;
5987 min = CASE_LOW (cl);
5988 max = CASE_HIGH (cl);
5990 /* If there are multiple case labels with the same destination
5991 we need to combine them to a single value range for the edge. */
5992 if (idx + 1 < n && cbb == ci[idx + 1].bb)
5994 /* Skip labels until the last of the group. */
5995 do {
5996 ++idx;
5997 } while (idx < n && cbb == ci[idx].bb);
5998 --idx;
6000 /* Pick up the maximum of the case label range. */
6001 if (CASE_HIGH (ci[idx].expr))
6002 max = CASE_HIGH (ci[idx].expr);
6003 else
6004 max = CASE_LOW (ci[idx].expr);
6007 /* Can't extract a useful assertion out of a range that includes the
6008 default label. */
6009 if (min == NULL_TREE)
6010 continue;
6012 /* Find the edge to register the assert expr on. */
6013 e = find_edge (bb, cbb);
6015 /* Register the necessary assertions for the operand in the
6016 SWITCH_EXPR. */
6017 register_edge_assert_for (op, e, bsi,
6018 max ? GE_EXPR : EQ_EXPR,
6019 op, fold_convert (TREE_TYPE (op), min));
6020 if (max)
6021 register_edge_assert_for (op, e, bsi, LE_EXPR, op,
6022 fold_convert (TREE_TYPE (op), max));
6025 XDELETEVEC (ci);
6027 if (!live_on_edge (default_edge, op))
6028 return;
6030 /* Now register along the default label assertions that correspond to the
6031 anti-range of each label. */
6032 int insertion_limit = PARAM_VALUE (PARAM_MAX_VRP_SWITCH_ASSERTIONS);
6033 for (idx = 1; idx < n; idx++)
6035 tree min, max;
6036 tree cl = gimple_switch_label (last, idx);
6038 min = CASE_LOW (cl);
6039 max = CASE_HIGH (cl);
6041 /* Combine contiguous case ranges to reduce the number of assertions
6042 to insert. */
6043 for (idx = idx + 1; idx < n; idx++)
6045 tree next_min, next_max;
6046 tree next_cl = gimple_switch_label (last, idx);
6048 next_min = CASE_LOW (next_cl);
6049 next_max = CASE_HIGH (next_cl);
6051 wide_int difference = wi::sub (next_min, max ? max : min);
6052 if (wi::eq_p (difference, 1))
6053 max = next_max ? next_max : next_min;
6054 else
6055 break;
6057 idx--;
6059 if (max == NULL_TREE)
6061 /* Register the assertion OP != MIN. */
6062 min = fold_convert (TREE_TYPE (op), min);
6063 register_edge_assert_for (op, default_edge, bsi, NE_EXPR, op, min);
6065 else
6067 /* Register the assertion (unsigned)OP - MIN > (MAX - MIN),
6068 which will give OP the anti-range ~[MIN,MAX]. */
6069 tree uop = fold_convert (unsigned_type_for (TREE_TYPE (op)), op);
6070 min = fold_convert (TREE_TYPE (uop), min);
6071 max = fold_convert (TREE_TYPE (uop), max);
6073 tree lhs = fold_build2 (MINUS_EXPR, TREE_TYPE (uop), uop, min);
6074 tree rhs = int_const_binop (MINUS_EXPR, max, min);
6075 register_new_assert_for (op, lhs, GT_EXPR, rhs,
6076 NULL, default_edge, bsi);
6079 if (--insertion_limit == 0)
6080 break;
6085 /* Traverse all the statements in block BB looking for statements that
6086 may generate useful assertions for the SSA names in their operand.
6087 If a statement produces a useful assertion A for name N_i, then the
6088 list of assertions already generated for N_i is scanned to
6089 determine if A is actually needed.
6091 If N_i already had the assertion A at a location dominating the
6092 current location, then nothing needs to be done. Otherwise, the
6093 new location for A is recorded instead.
6095 1- For every statement S in BB, all the variables used by S are
6096 added to bitmap FOUND_IN_SUBGRAPH.
6098 2- If statement S uses an operand N in a way that exposes a known
6099 value range for N, then if N was not already generated by an
6100 ASSERT_EXPR, create a new assert location for N. For instance,
6101 if N is a pointer and the statement dereferences it, we can
6102 assume that N is not NULL.
6104 3- COND_EXPRs are a special case of #2. We can derive range
6105 information from the predicate but need to insert different
6106 ASSERT_EXPRs for each of the sub-graphs rooted at the
6107 conditional block. If the last statement of BB is a conditional
6108 expression of the form 'X op Y', then
6110 a) Remove X and Y from the set FOUND_IN_SUBGRAPH.
6112 b) If the conditional is the only entry point to the sub-graph
6113 corresponding to the THEN_CLAUSE, recurse into it. On
6114 return, if X and/or Y are marked in FOUND_IN_SUBGRAPH, then
6115 an ASSERT_EXPR is added for the corresponding variable.
6117 c) Repeat step (b) on the ELSE_CLAUSE.
6119 d) Mark X and Y in FOUND_IN_SUBGRAPH.
6121 For instance,
6123 if (a == 9)
6124 b = a;
6125 else
6126 b = c + 1;
6128 In this case, an assertion on the THEN clause is useful to
6129 determine that 'a' is always 9 on that edge. However, an assertion
6130 on the ELSE clause would be unnecessary.
6132 4- If BB does not end in a conditional expression, then we recurse
6133 into BB's dominator children.
6135 At the end of the recursive traversal, every SSA name will have a
6136 list of locations where ASSERT_EXPRs should be added. When a new
6137 location for name N is found, it is registered by calling
6138 register_new_assert_for. That function keeps track of all the
6139 registered assertions to prevent adding unnecessary assertions.
6140 For instance, if a pointer P_4 is dereferenced more than once in a
6141 dominator tree, only the location dominating all the dereference of
6142 P_4 will receive an ASSERT_EXPR. */
6144 static void
6145 find_assert_locations_1 (basic_block bb, sbitmap live)
6147 gimple *last;
6149 last = last_stmt (bb);
6151 /* If BB's last statement is a conditional statement involving integer
6152 operands, determine if we need to add ASSERT_EXPRs. */
6153 if (last
6154 && gimple_code (last) == GIMPLE_COND
6155 && !fp_predicate (last)
6156 && !ZERO_SSA_OPERANDS (last, SSA_OP_USE))
6157 find_conditional_asserts (bb, as_a <gcond *> (last));
6159 /* If BB's last statement is a switch statement involving integer
6160 operands, determine if we need to add ASSERT_EXPRs. */
6161 if (last
6162 && gimple_code (last) == GIMPLE_SWITCH
6163 && !ZERO_SSA_OPERANDS (last, SSA_OP_USE))
6164 find_switch_asserts (bb, as_a <gswitch *> (last));
6166 /* Traverse all the statements in BB marking used names and looking
6167 for statements that may infer assertions for their used operands. */
6168 for (gimple_stmt_iterator si = gsi_last_bb (bb); !gsi_end_p (si);
6169 gsi_prev (&si))
6171 gimple *stmt;
6172 tree op;
6173 ssa_op_iter i;
6175 stmt = gsi_stmt (si);
6177 if (is_gimple_debug (stmt))
6178 continue;
6180 /* See if we can derive an assertion for any of STMT's operands. */
6181 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_USE)
6183 tree value;
6184 enum tree_code comp_code;
6186 /* If op is not live beyond this stmt, do not bother to insert
6187 asserts for it. */
6188 if (!bitmap_bit_p (live, SSA_NAME_VERSION (op)))
6189 continue;
6191 /* If OP is used in such a way that we can infer a value
6192 range for it, and we don't find a previous assertion for
6193 it, create a new assertion location node for OP. */
6194 if (infer_value_range (stmt, op, &comp_code, &value))
6196 /* If we are able to infer a nonzero value range for OP,
6197 then walk backwards through the use-def chain to see if OP
6198 was set via a typecast.
6200 If so, then we can also infer a nonzero value range
6201 for the operand of the NOP_EXPR. */
6202 if (comp_code == NE_EXPR && integer_zerop (value))
6204 tree t = op;
6205 gimple *def_stmt = SSA_NAME_DEF_STMT (t);
6207 while (is_gimple_assign (def_stmt)
6208 && CONVERT_EXPR_CODE_P
6209 (gimple_assign_rhs_code (def_stmt))
6210 && TREE_CODE
6211 (gimple_assign_rhs1 (def_stmt)) == SSA_NAME
6212 && POINTER_TYPE_P
6213 (TREE_TYPE (gimple_assign_rhs1 (def_stmt))))
6215 t = gimple_assign_rhs1 (def_stmt);
6216 def_stmt = SSA_NAME_DEF_STMT (t);
6218 /* Note we want to register the assert for the
6219 operand of the NOP_EXPR after SI, not after the
6220 conversion. */
6221 if (bitmap_bit_p (live, SSA_NAME_VERSION (t)))
6222 register_new_assert_for (t, t, comp_code, value,
6223 bb, NULL, si);
6227 register_new_assert_for (op, op, comp_code, value, bb, NULL, si);
6231 /* Update live. */
6232 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_USE)
6233 bitmap_set_bit (live, SSA_NAME_VERSION (op));
6234 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_DEF)
6235 bitmap_clear_bit (live, SSA_NAME_VERSION (op));
6238 /* Traverse all PHI nodes in BB, updating live. */
6239 for (gphi_iterator si = gsi_start_phis (bb); !gsi_end_p (si);
6240 gsi_next (&si))
6242 use_operand_p arg_p;
6243 ssa_op_iter i;
6244 gphi *phi = si.phi ();
6245 tree res = gimple_phi_result (phi);
6247 if (virtual_operand_p (res))
6248 continue;
6250 FOR_EACH_PHI_ARG (arg_p, phi, i, SSA_OP_USE)
6252 tree arg = USE_FROM_PTR (arg_p);
6253 if (TREE_CODE (arg) == SSA_NAME)
6254 bitmap_set_bit (live, SSA_NAME_VERSION (arg));
6257 bitmap_clear_bit (live, SSA_NAME_VERSION (res));
6261 /* Do an RPO walk over the function computing SSA name liveness
6262 on-the-fly and deciding on assert expressions to insert. */
6264 static void
6265 find_assert_locations (void)
6267 int *rpo = XNEWVEC (int, last_basic_block_for_fn (cfun));
6268 int *bb_rpo = XNEWVEC (int, last_basic_block_for_fn (cfun));
6269 int *last_rpo = XCNEWVEC (int, last_basic_block_for_fn (cfun));
6270 int rpo_cnt, i;
6272 live = XCNEWVEC (sbitmap, last_basic_block_for_fn (cfun));
6273 rpo_cnt = pre_and_rev_post_order_compute (NULL, rpo, false);
6274 for (i = 0; i < rpo_cnt; ++i)
6275 bb_rpo[rpo[i]] = i;
6277 /* Pre-seed loop latch liveness from loop header PHI nodes. Due to
6278 the order we compute liveness and insert asserts we otherwise
6279 fail to insert asserts into the loop latch. */
6280 loop_p loop;
6281 FOR_EACH_LOOP (loop, 0)
6283 i = loop->latch->index;
6284 unsigned int j = single_succ_edge (loop->latch)->dest_idx;
6285 for (gphi_iterator gsi = gsi_start_phis (loop->header);
6286 !gsi_end_p (gsi); gsi_next (&gsi))
6288 gphi *phi = gsi.phi ();
6289 if (virtual_operand_p (gimple_phi_result (phi)))
6290 continue;
6291 tree arg = gimple_phi_arg_def (phi, j);
6292 if (TREE_CODE (arg) == SSA_NAME)
6294 if (live[i] == NULL)
6296 live[i] = sbitmap_alloc (num_ssa_names);
6297 bitmap_clear (live[i]);
6299 bitmap_set_bit (live[i], SSA_NAME_VERSION (arg));
6304 for (i = rpo_cnt - 1; i >= 0; --i)
6306 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, rpo[i]);
6307 edge e;
6308 edge_iterator ei;
6310 if (!live[rpo[i]])
6312 live[rpo[i]] = sbitmap_alloc (num_ssa_names);
6313 bitmap_clear (live[rpo[i]]);
6316 /* Process BB and update the live information with uses in
6317 this block. */
6318 find_assert_locations_1 (bb, live[rpo[i]]);
6320 /* Merge liveness into the predecessor blocks and free it. */
6321 if (!bitmap_empty_p (live[rpo[i]]))
6323 int pred_rpo = i;
6324 FOR_EACH_EDGE (e, ei, bb->preds)
6326 int pred = e->src->index;
6327 if ((e->flags & EDGE_DFS_BACK) || pred == ENTRY_BLOCK)
6328 continue;
6330 if (!live[pred])
6332 live[pred] = sbitmap_alloc (num_ssa_names);
6333 bitmap_clear (live[pred]);
6335 bitmap_ior (live[pred], live[pred], live[rpo[i]]);
6337 if (bb_rpo[pred] < pred_rpo)
6338 pred_rpo = bb_rpo[pred];
6341 /* Record the RPO number of the last visited block that needs
6342 live information from this block. */
6343 last_rpo[rpo[i]] = pred_rpo;
6345 else
6347 sbitmap_free (live[rpo[i]]);
6348 live[rpo[i]] = NULL;
6351 /* We can free all successors live bitmaps if all their
6352 predecessors have been visited already. */
6353 FOR_EACH_EDGE (e, ei, bb->succs)
6354 if (last_rpo[e->dest->index] == i
6355 && live[e->dest->index])
6357 sbitmap_free (live[e->dest->index]);
6358 live[e->dest->index] = NULL;
6362 XDELETEVEC (rpo);
6363 XDELETEVEC (bb_rpo);
6364 XDELETEVEC (last_rpo);
6365 for (i = 0; i < last_basic_block_for_fn (cfun); ++i)
6366 if (live[i])
6367 sbitmap_free (live[i]);
6368 XDELETEVEC (live);
6371 /* Create an ASSERT_EXPR for NAME and insert it in the location
6372 indicated by LOC. Return true if we made any edge insertions. */
6374 static bool
6375 process_assert_insertions_for (tree name, assert_locus *loc)
6377 /* Build the comparison expression NAME_i COMP_CODE VAL. */
6378 gimple *stmt;
6379 tree cond;
6380 gimple *assert_stmt;
6381 edge_iterator ei;
6382 edge e;
6384 /* If we have X <=> X do not insert an assert expr for that. */
6385 if (loc->expr == loc->val)
6386 return false;
6388 cond = build2 (loc->comp_code, boolean_type_node, loc->expr, loc->val);
6389 assert_stmt = build_assert_expr_for (cond, name);
6390 if (loc->e)
6392 /* We have been asked to insert the assertion on an edge. This
6393 is used only by COND_EXPR and SWITCH_EXPR assertions. */
6394 gcc_checking_assert (gimple_code (gsi_stmt (loc->si)) == GIMPLE_COND
6395 || (gimple_code (gsi_stmt (loc->si))
6396 == GIMPLE_SWITCH));
6398 gsi_insert_on_edge (loc->e, assert_stmt);
6399 return true;
6402 /* Otherwise, we can insert right after LOC->SI iff the
6403 statement must not be the last statement in the block. */
6404 stmt = gsi_stmt (loc->si);
6405 if (!stmt_ends_bb_p (stmt))
6407 gsi_insert_after (&loc->si, assert_stmt, GSI_SAME_STMT);
6408 return false;
6411 /* If STMT must be the last statement in BB, we can only insert new
6412 assertions on the non-abnormal edge out of BB. Note that since
6413 STMT is not control flow, there may only be one non-abnormal/eh edge
6414 out of BB. */
6415 FOR_EACH_EDGE (e, ei, loc->bb->succs)
6416 if (!(e->flags & (EDGE_ABNORMAL|EDGE_EH)))
6418 gsi_insert_on_edge (e, assert_stmt);
6419 return true;
6422 gcc_unreachable ();
6426 /* Process all the insertions registered for every name N_i registered
6427 in NEED_ASSERT_FOR. The list of assertions to be inserted are
6428 found in ASSERTS_FOR[i]. */
6430 static void
6431 process_assert_insertions (void)
6433 unsigned i;
6434 bitmap_iterator bi;
6435 bool update_edges_p = false;
6436 int num_asserts = 0;
6438 if (dump_file && (dump_flags & TDF_DETAILS))
6439 dump_all_asserts (dump_file);
6441 EXECUTE_IF_SET_IN_BITMAP (need_assert_for, 0, i, bi)
6443 assert_locus *loc = asserts_for[i];
6444 gcc_assert (loc);
6446 while (loc)
6448 assert_locus *next = loc->next;
6449 update_edges_p |= process_assert_insertions_for (ssa_name (i), loc);
6450 free (loc);
6451 loc = next;
6452 num_asserts++;
6456 if (update_edges_p)
6457 gsi_commit_edge_inserts ();
6459 statistics_counter_event (cfun, "Number of ASSERT_EXPR expressions inserted",
6460 num_asserts);
6464 /* Traverse the flowgraph looking for conditional jumps to insert range
6465 expressions. These range expressions are meant to provide information
6466 to optimizations that need to reason in terms of value ranges. They
6467 will not be expanded into RTL. For instance, given:
6469 x = ...
6470 y = ...
6471 if (x < y)
6472 y = x - 2;
6473 else
6474 x = y + 3;
6476 this pass will transform the code into:
6478 x = ...
6479 y = ...
6480 if (x < y)
6482 x = ASSERT_EXPR <x, x < y>
6483 y = x - 2
6485 else
6487 y = ASSERT_EXPR <y, x >= y>
6488 x = y + 3
6491 The idea is that once copy and constant propagation have run, other
6492 optimizations will be able to determine what ranges of values can 'x'
6493 take in different paths of the code, simply by checking the reaching
6494 definition of 'x'. */
6496 static void
6497 insert_range_assertions (void)
6499 need_assert_for = BITMAP_ALLOC (NULL);
6500 asserts_for = XCNEWVEC (assert_locus *, num_ssa_names);
6502 calculate_dominance_info (CDI_DOMINATORS);
6504 find_assert_locations ();
6505 if (!bitmap_empty_p (need_assert_for))
6507 process_assert_insertions ();
6508 update_ssa (TODO_update_ssa_no_phi);
6511 if (dump_file && (dump_flags & TDF_DETAILS))
6513 fprintf (dump_file, "\nSSA form after inserting ASSERT_EXPRs\n");
6514 dump_function_to_file (current_function_decl, dump_file, dump_flags);
6517 free (asserts_for);
6518 BITMAP_FREE (need_assert_for);
6521 /* Checks one ARRAY_REF in REF, located at LOCUS. Ignores flexible arrays
6522 and "struct" hacks. If VRP can determine that the
6523 array subscript is a constant, check if it is outside valid
6524 range. If the array subscript is a RANGE, warn if it is
6525 non-overlapping with valid range.
6526 IGNORE_OFF_BY_ONE is true if the ARRAY_REF is inside a ADDR_EXPR. */
6528 static void
6529 check_array_ref (location_t location, tree ref, bool ignore_off_by_one)
6531 value_range *vr = NULL;
6532 tree low_sub, up_sub;
6533 tree low_bound, up_bound, up_bound_p1;
6535 if (TREE_NO_WARNING (ref))
6536 return;
6538 low_sub = up_sub = TREE_OPERAND (ref, 1);
6539 up_bound = array_ref_up_bound (ref);
6541 /* Can not check flexible arrays. */
6542 if (!up_bound
6543 || TREE_CODE (up_bound) != INTEGER_CST)
6544 return;
6546 /* Accesses to trailing arrays via pointers may access storage
6547 beyond the types array bounds. */
6548 if (warn_array_bounds < 2
6549 && array_at_struct_end_p (ref))
6550 return;
6552 low_bound = array_ref_low_bound (ref);
6553 up_bound_p1 = int_const_binop (PLUS_EXPR, up_bound,
6554 build_int_cst (TREE_TYPE (up_bound), 1));
6556 /* Empty array. */
6557 if (tree_int_cst_equal (low_bound, up_bound_p1))
6559 warning_at (location, OPT_Warray_bounds,
6560 "array subscript is above array bounds");
6561 TREE_NO_WARNING (ref) = 1;
6564 if (TREE_CODE (low_sub) == SSA_NAME)
6566 vr = get_value_range (low_sub);
6567 if (vr->type == VR_RANGE || vr->type == VR_ANTI_RANGE)
6569 low_sub = vr->type == VR_RANGE ? vr->max : vr->min;
6570 up_sub = vr->type == VR_RANGE ? vr->min : vr->max;
6574 if (vr && vr->type == VR_ANTI_RANGE)
6576 if (TREE_CODE (up_sub) == INTEGER_CST
6577 && (ignore_off_by_one
6578 ? tree_int_cst_lt (up_bound, up_sub)
6579 : tree_int_cst_le (up_bound, up_sub))
6580 && TREE_CODE (low_sub) == INTEGER_CST
6581 && tree_int_cst_le (low_sub, low_bound))
6583 warning_at (location, OPT_Warray_bounds,
6584 "array subscript is outside array bounds");
6585 TREE_NO_WARNING (ref) = 1;
6588 else if (TREE_CODE (up_sub) == INTEGER_CST
6589 && (ignore_off_by_one
6590 ? !tree_int_cst_le (up_sub, up_bound_p1)
6591 : !tree_int_cst_le (up_sub, up_bound)))
6593 if (dump_file && (dump_flags & TDF_DETAILS))
6595 fprintf (dump_file, "Array bound warning for ");
6596 dump_generic_expr (MSG_NOTE, TDF_SLIM, ref);
6597 fprintf (dump_file, "\n");
6599 warning_at (location, OPT_Warray_bounds,
6600 "array subscript is above array bounds");
6601 TREE_NO_WARNING (ref) = 1;
6603 else if (TREE_CODE (low_sub) == INTEGER_CST
6604 && tree_int_cst_lt (low_sub, low_bound))
6606 if (dump_file && (dump_flags & TDF_DETAILS))
6608 fprintf (dump_file, "Array bound warning for ");
6609 dump_generic_expr (MSG_NOTE, TDF_SLIM, ref);
6610 fprintf (dump_file, "\n");
6612 warning_at (location, OPT_Warray_bounds,
6613 "array subscript is below array bounds");
6614 TREE_NO_WARNING (ref) = 1;
6618 /* Searches if the expr T, located at LOCATION computes
6619 address of an ARRAY_REF, and call check_array_ref on it. */
6621 static void
6622 search_for_addr_array (tree t, location_t location)
6624 /* Check each ARRAY_REFs in the reference chain. */
6627 if (TREE_CODE (t) == ARRAY_REF)
6628 check_array_ref (location, t, true /*ignore_off_by_one*/);
6630 t = TREE_OPERAND (t, 0);
6632 while (handled_component_p (t));
6634 if (TREE_CODE (t) == MEM_REF
6635 && TREE_CODE (TREE_OPERAND (t, 0)) == ADDR_EXPR
6636 && !TREE_NO_WARNING (t))
6638 tree tem = TREE_OPERAND (TREE_OPERAND (t, 0), 0);
6639 tree low_bound, up_bound, el_sz;
6640 offset_int idx;
6641 if (TREE_CODE (TREE_TYPE (tem)) != ARRAY_TYPE
6642 || TREE_CODE (TREE_TYPE (TREE_TYPE (tem))) == ARRAY_TYPE
6643 || !TYPE_DOMAIN (TREE_TYPE (tem)))
6644 return;
6646 low_bound = TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (tem)));
6647 up_bound = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (tem)));
6648 el_sz = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (tem)));
6649 if (!low_bound
6650 || TREE_CODE (low_bound) != INTEGER_CST
6651 || !up_bound
6652 || TREE_CODE (up_bound) != INTEGER_CST
6653 || !el_sz
6654 || TREE_CODE (el_sz) != INTEGER_CST)
6655 return;
6657 idx = mem_ref_offset (t);
6658 idx = wi::sdiv_trunc (idx, wi::to_offset (el_sz));
6659 if (idx < 0)
6661 if (dump_file && (dump_flags & TDF_DETAILS))
6663 fprintf (dump_file, "Array bound warning for ");
6664 dump_generic_expr (MSG_NOTE, TDF_SLIM, t);
6665 fprintf (dump_file, "\n");
6667 warning_at (location, OPT_Warray_bounds,
6668 "array subscript is below array bounds");
6669 TREE_NO_WARNING (t) = 1;
6671 else if (idx > (wi::to_offset (up_bound)
6672 - wi::to_offset (low_bound) + 1))
6674 if (dump_file && (dump_flags & TDF_DETAILS))
6676 fprintf (dump_file, "Array bound warning for ");
6677 dump_generic_expr (MSG_NOTE, TDF_SLIM, t);
6678 fprintf (dump_file, "\n");
6680 warning_at (location, OPT_Warray_bounds,
6681 "array subscript is above array bounds");
6682 TREE_NO_WARNING (t) = 1;
6687 /* walk_tree() callback that checks if *TP is
6688 an ARRAY_REF inside an ADDR_EXPR (in which an array
6689 subscript one outside the valid range is allowed). Call
6690 check_array_ref for each ARRAY_REF found. The location is
6691 passed in DATA. */
6693 static tree
6694 check_array_bounds (tree *tp, int *walk_subtree, void *data)
6696 tree t = *tp;
6697 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
6698 location_t location;
6700 if (EXPR_HAS_LOCATION (t))
6701 location = EXPR_LOCATION (t);
6702 else
6704 location_t *locp = (location_t *) wi->info;
6705 location = *locp;
6708 *walk_subtree = TRUE;
6710 if (TREE_CODE (t) == ARRAY_REF)
6711 check_array_ref (location, t, false /*ignore_off_by_one*/);
6713 else if (TREE_CODE (t) == ADDR_EXPR)
6715 search_for_addr_array (t, location);
6716 *walk_subtree = FALSE;
6719 return NULL_TREE;
6722 /* Walk over all statements of all reachable BBs and call check_array_bounds
6723 on them. */
6725 static void
6726 check_all_array_refs (void)
6728 basic_block bb;
6729 gimple_stmt_iterator si;
6731 FOR_EACH_BB_FN (bb, cfun)
6733 edge_iterator ei;
6734 edge e;
6735 bool executable = false;
6737 /* Skip blocks that were found to be unreachable. */
6738 FOR_EACH_EDGE (e, ei, bb->preds)
6739 executable |= !!(e->flags & EDGE_EXECUTABLE);
6740 if (!executable)
6741 continue;
6743 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
6745 gimple *stmt = gsi_stmt (si);
6746 struct walk_stmt_info wi;
6747 if (!gimple_has_location (stmt)
6748 || is_gimple_debug (stmt))
6749 continue;
6751 memset (&wi, 0, sizeof (wi));
6753 location_t loc = gimple_location (stmt);
6754 wi.info = &loc;
6756 walk_gimple_op (gsi_stmt (si),
6757 check_array_bounds,
6758 &wi);
6763 /* Return true if all imm uses of VAR are either in STMT, or
6764 feed (optionally through a chain of single imm uses) GIMPLE_COND
6765 in basic block COND_BB. */
6767 static bool
6768 all_imm_uses_in_stmt_or_feed_cond (tree var, gimple *stmt, basic_block cond_bb)
6770 use_operand_p use_p, use2_p;
6771 imm_use_iterator iter;
6773 FOR_EACH_IMM_USE_FAST (use_p, iter, var)
6774 if (USE_STMT (use_p) != stmt)
6776 gimple *use_stmt = USE_STMT (use_p), *use_stmt2;
6777 if (is_gimple_debug (use_stmt))
6778 continue;
6779 while (is_gimple_assign (use_stmt)
6780 && TREE_CODE (gimple_assign_lhs (use_stmt)) == SSA_NAME
6781 && single_imm_use (gimple_assign_lhs (use_stmt),
6782 &use2_p, &use_stmt2))
6783 use_stmt = use_stmt2;
6784 if (gimple_code (use_stmt) != GIMPLE_COND
6785 || gimple_bb (use_stmt) != cond_bb)
6786 return false;
6788 return true;
6791 /* Handle
6792 _4 = x_3 & 31;
6793 if (_4 != 0)
6794 goto <bb 6>;
6795 else
6796 goto <bb 7>;
6797 <bb 6>:
6798 __builtin_unreachable ();
6799 <bb 7>:
6800 x_5 = ASSERT_EXPR <x_3, ...>;
6801 If x_3 has no other immediate uses (checked by caller),
6802 var is the x_3 var from ASSERT_EXPR, we can clear low 5 bits
6803 from the non-zero bitmask. */
6805 static void
6806 maybe_set_nonzero_bits (basic_block bb, tree var)
6808 edge e = single_pred_edge (bb);
6809 basic_block cond_bb = e->src;
6810 gimple *stmt = last_stmt (cond_bb);
6811 tree cst;
6813 if (stmt == NULL
6814 || gimple_code (stmt) != GIMPLE_COND
6815 || gimple_cond_code (stmt) != ((e->flags & EDGE_TRUE_VALUE)
6816 ? EQ_EXPR : NE_EXPR)
6817 || TREE_CODE (gimple_cond_lhs (stmt)) != SSA_NAME
6818 || !integer_zerop (gimple_cond_rhs (stmt)))
6819 return;
6821 stmt = SSA_NAME_DEF_STMT (gimple_cond_lhs (stmt));
6822 if (!is_gimple_assign (stmt)
6823 || gimple_assign_rhs_code (stmt) != BIT_AND_EXPR
6824 || TREE_CODE (gimple_assign_rhs2 (stmt)) != INTEGER_CST)
6825 return;
6826 if (gimple_assign_rhs1 (stmt) != var)
6828 gimple *stmt2;
6830 if (TREE_CODE (gimple_assign_rhs1 (stmt)) != SSA_NAME)
6831 return;
6832 stmt2 = SSA_NAME_DEF_STMT (gimple_assign_rhs1 (stmt));
6833 if (!gimple_assign_cast_p (stmt2)
6834 || gimple_assign_rhs1 (stmt2) != var
6835 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (stmt2))
6836 || (TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (stmt)))
6837 != TYPE_PRECISION (TREE_TYPE (var))))
6838 return;
6840 cst = gimple_assign_rhs2 (stmt);
6841 set_nonzero_bits (var, wi::bit_and_not (get_nonzero_bits (var), cst));
6844 /* Convert range assertion expressions into the implied copies and
6845 copy propagate away the copies. Doing the trivial copy propagation
6846 here avoids the need to run the full copy propagation pass after
6847 VRP.
6849 FIXME, this will eventually lead to copy propagation removing the
6850 names that had useful range information attached to them. For
6851 instance, if we had the assertion N_i = ASSERT_EXPR <N_j, N_j > 3>,
6852 then N_i will have the range [3, +INF].
6854 However, by converting the assertion into the implied copy
6855 operation N_i = N_j, we will then copy-propagate N_j into the uses
6856 of N_i and lose the range information. We may want to hold on to
6857 ASSERT_EXPRs a little while longer as the ranges could be used in
6858 things like jump threading.
6860 The problem with keeping ASSERT_EXPRs around is that passes after
6861 VRP need to handle them appropriately.
6863 Another approach would be to make the range information a first
6864 class property of the SSA_NAME so that it can be queried from
6865 any pass. This is made somewhat more complex by the need for
6866 multiple ranges to be associated with one SSA_NAME. */
6868 static void
6869 remove_range_assertions (void)
6871 basic_block bb;
6872 gimple_stmt_iterator si;
6873 /* 1 if looking at ASSERT_EXPRs immediately at the beginning of
6874 a basic block preceeded by GIMPLE_COND branching to it and
6875 __builtin_trap, -1 if not yet checked, 0 otherwise. */
6876 int is_unreachable;
6878 /* Note that the BSI iterator bump happens at the bottom of the
6879 loop and no bump is necessary if we're removing the statement
6880 referenced by the current BSI. */
6881 FOR_EACH_BB_FN (bb, cfun)
6882 for (si = gsi_after_labels (bb), is_unreachable = -1; !gsi_end_p (si);)
6884 gimple *stmt = gsi_stmt (si);
6885 gimple *use_stmt;
6887 if (is_gimple_assign (stmt)
6888 && gimple_assign_rhs_code (stmt) == ASSERT_EXPR)
6890 tree lhs = gimple_assign_lhs (stmt);
6891 tree rhs = gimple_assign_rhs1 (stmt);
6892 tree var;
6893 use_operand_p use_p;
6894 imm_use_iterator iter;
6896 var = ASSERT_EXPR_VAR (rhs);
6898 if (TREE_CODE (var) == SSA_NAME
6899 && !POINTER_TYPE_P (TREE_TYPE (lhs))
6900 && SSA_NAME_RANGE_INFO (lhs))
6902 if (is_unreachable == -1)
6904 is_unreachable = 0;
6905 if (single_pred_p (bb)
6906 && assert_unreachable_fallthru_edge_p
6907 (single_pred_edge (bb)))
6908 is_unreachable = 1;
6910 /* Handle
6911 if (x_7 >= 10 && x_7 < 20)
6912 __builtin_unreachable ();
6913 x_8 = ASSERT_EXPR <x_7, ...>;
6914 if the only uses of x_7 are in the ASSERT_EXPR and
6915 in the condition. In that case, we can copy the
6916 range info from x_8 computed in this pass also
6917 for x_7. */
6918 if (is_unreachable
6919 && all_imm_uses_in_stmt_or_feed_cond (var, stmt,
6920 single_pred (bb)))
6922 set_range_info (var, SSA_NAME_RANGE_TYPE (lhs),
6923 SSA_NAME_RANGE_INFO (lhs)->get_min (),
6924 SSA_NAME_RANGE_INFO (lhs)->get_max ());
6925 maybe_set_nonzero_bits (bb, var);
6929 /* Propagate the RHS into every use of the LHS. */
6930 FOR_EACH_IMM_USE_STMT (use_stmt, iter, lhs)
6932 FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
6933 SET_USE (use_p, var);
6934 update_stmt (use_stmt);
6937 /* And finally, remove the copy, it is not needed. */
6938 gsi_remove (&si, true);
6939 release_defs (stmt);
6941 else
6943 if (!is_gimple_debug (gsi_stmt (si)))
6944 is_unreachable = 0;
6945 gsi_next (&si);
6951 /* Return true if STMT is interesting for VRP. */
6953 static bool
6954 stmt_interesting_for_vrp (gimple *stmt)
6956 if (gimple_code (stmt) == GIMPLE_PHI)
6958 tree res = gimple_phi_result (stmt);
6959 return (!virtual_operand_p (res)
6960 && (INTEGRAL_TYPE_P (TREE_TYPE (res))
6961 || POINTER_TYPE_P (TREE_TYPE (res))));
6963 else if (is_gimple_assign (stmt) || is_gimple_call (stmt))
6965 tree lhs = gimple_get_lhs (stmt);
6967 /* In general, assignments with virtual operands are not useful
6968 for deriving ranges, with the obvious exception of calls to
6969 builtin functions. */
6970 if (lhs && TREE_CODE (lhs) == SSA_NAME
6971 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs))
6972 || POINTER_TYPE_P (TREE_TYPE (lhs)))
6973 && (is_gimple_call (stmt)
6974 || !gimple_vuse (stmt)))
6975 return true;
6976 else if (is_gimple_call (stmt) && gimple_call_internal_p (stmt))
6977 switch (gimple_call_internal_fn (stmt))
6979 case IFN_ADD_OVERFLOW:
6980 case IFN_SUB_OVERFLOW:
6981 case IFN_MUL_OVERFLOW:
6982 /* These internal calls return _Complex integer type,
6983 but are interesting to VRP nevertheless. */
6984 if (lhs && TREE_CODE (lhs) == SSA_NAME)
6985 return true;
6986 break;
6987 default:
6988 break;
6991 else if (gimple_code (stmt) == GIMPLE_COND
6992 || gimple_code (stmt) == GIMPLE_SWITCH)
6993 return true;
6995 return false;
6998 /* Initialize VRP lattice. */
7000 static void
7001 vrp_initialize_lattice ()
7003 values_propagated = false;
7004 num_vr_values = num_ssa_names;
7005 vr_value = XCNEWVEC (value_range *, num_vr_values);
7006 vr_phi_edge_counts = XCNEWVEC (int, num_ssa_names);
7007 bitmap_obstack_initialize (&vrp_equiv_obstack);
7010 /* Initialization required by ssa_propagate engine. */
7012 static void
7013 vrp_initialize ()
7015 basic_block bb;
7017 FOR_EACH_BB_FN (bb, cfun)
7019 for (gphi_iterator si = gsi_start_phis (bb); !gsi_end_p (si);
7020 gsi_next (&si))
7022 gphi *phi = si.phi ();
7023 if (!stmt_interesting_for_vrp (phi))
7025 tree lhs = PHI_RESULT (phi);
7026 set_value_range_to_varying (get_value_range (lhs));
7027 prop_set_simulate_again (phi, false);
7029 else
7030 prop_set_simulate_again (phi, true);
7033 for (gimple_stmt_iterator si = gsi_start_bb (bb); !gsi_end_p (si);
7034 gsi_next (&si))
7036 gimple *stmt = gsi_stmt (si);
7038 /* If the statement is a control insn, then we do not
7039 want to avoid simulating the statement once. Failure
7040 to do so means that those edges will never get added. */
7041 if (stmt_ends_bb_p (stmt))
7042 prop_set_simulate_again (stmt, true);
7043 else if (!stmt_interesting_for_vrp (stmt))
7045 set_defs_to_varying (stmt);
7046 prop_set_simulate_again (stmt, false);
7048 else
7049 prop_set_simulate_again (stmt, true);
7054 /* Return the singleton value-range for NAME or NAME. */
7056 static inline tree
7057 vrp_valueize (tree name)
7059 if (TREE_CODE (name) == SSA_NAME)
7061 value_range *vr = get_value_range (name);
7062 if (vr->type == VR_RANGE
7063 && (TREE_CODE (vr->min) == SSA_NAME
7064 || is_gimple_min_invariant (vr->min))
7065 && vrp_operand_equal_p (vr->min, vr->max))
7066 return vr->min;
7068 return name;
7071 /* Return the singleton value-range for NAME if that is a constant
7072 but signal to not follow SSA edges. */
7074 static inline tree
7075 vrp_valueize_1 (tree name)
7077 if (TREE_CODE (name) == SSA_NAME)
7079 /* If the definition may be simulated again we cannot follow
7080 this SSA edge as the SSA propagator does not necessarily
7081 re-visit the use. */
7082 gimple *def_stmt = SSA_NAME_DEF_STMT (name);
7083 if (!gimple_nop_p (def_stmt)
7084 && prop_simulate_again_p (def_stmt))
7085 return NULL_TREE;
7086 value_range *vr = get_value_range (name);
7087 if (range_int_cst_singleton_p (vr))
7088 return vr->min;
7090 return name;
7093 /* Visit assignment STMT. If it produces an interesting range, record
7094 the range in VR and set LHS to OUTPUT_P. */
7096 static void
7097 vrp_visit_assignment_or_call (gimple *stmt, tree *output_p, value_range *vr)
7099 tree lhs;
7100 enum gimple_code code = gimple_code (stmt);
7101 lhs = gimple_get_lhs (stmt);
7102 *output_p = NULL_TREE;
7104 /* We only keep track of ranges in integral and pointer types. */
7105 if (TREE_CODE (lhs) == SSA_NAME
7106 && ((INTEGRAL_TYPE_P (TREE_TYPE (lhs))
7107 /* It is valid to have NULL MIN/MAX values on a type. See
7108 build_range_type. */
7109 && TYPE_MIN_VALUE (TREE_TYPE (lhs))
7110 && TYPE_MAX_VALUE (TREE_TYPE (lhs)))
7111 || POINTER_TYPE_P (TREE_TYPE (lhs))))
7113 /* Try folding the statement to a constant first. */
7114 tree tem = gimple_fold_stmt_to_constant_1 (stmt, vrp_valueize,
7115 vrp_valueize_1);
7116 if (tem && is_gimple_min_invariant (tem))
7117 set_value_range_to_value (vr, tem, NULL);
7118 /* Then dispatch to value-range extracting functions. */
7119 else if (code == GIMPLE_CALL)
7120 extract_range_basic (vr, stmt);
7121 else
7122 extract_range_from_assignment (vr, as_a <gassign *> (stmt));
7123 *output_p = lhs;
7127 /* Helper that gets the value range of the SSA_NAME with version I
7128 or a symbolic range containing the SSA_NAME only if the value range
7129 is varying or undefined. */
7131 static inline value_range
7132 get_vr_for_comparison (int i)
7134 value_range vr = *get_value_range (ssa_name (i));
7136 /* If name N_i does not have a valid range, use N_i as its own
7137 range. This allows us to compare against names that may
7138 have N_i in their ranges. */
7139 if (vr.type == VR_VARYING || vr.type == VR_UNDEFINED)
7141 vr.type = VR_RANGE;
7142 vr.min = ssa_name (i);
7143 vr.max = ssa_name (i);
7146 return vr;
7149 /* Compare all the value ranges for names equivalent to VAR with VAL
7150 using comparison code COMP. Return the same value returned by
7151 compare_range_with_value, including the setting of
7152 *STRICT_OVERFLOW_P. */
7154 static tree
7155 compare_name_with_value (enum tree_code comp, tree var, tree val,
7156 bool *strict_overflow_p, bool use_equiv_p)
7158 bitmap_iterator bi;
7159 unsigned i;
7160 bitmap e;
7161 tree retval, t;
7162 int used_strict_overflow;
7163 bool sop;
7164 value_range equiv_vr;
7166 /* Get the set of equivalences for VAR. */
7167 e = get_value_range (var)->equiv;
7169 /* Start at -1. Set it to 0 if we do a comparison without relying
7170 on overflow, or 1 if all comparisons rely on overflow. */
7171 used_strict_overflow = -1;
7173 /* Compare vars' value range with val. */
7174 equiv_vr = get_vr_for_comparison (SSA_NAME_VERSION (var));
7175 sop = false;
7176 retval = compare_range_with_value (comp, &equiv_vr, val, &sop);
7177 if (retval)
7178 used_strict_overflow = sop ? 1 : 0;
7180 /* If the equiv set is empty we have done all work we need to do. */
7181 if (e == NULL)
7183 if (retval
7184 && used_strict_overflow > 0)
7185 *strict_overflow_p = true;
7186 return retval;
7189 EXECUTE_IF_SET_IN_BITMAP (e, 0, i, bi)
7191 if (! use_equiv_p
7192 && ! SSA_NAME_IS_DEFAULT_DEF (ssa_name (i))
7193 && prop_simulate_again_p (SSA_NAME_DEF_STMT (ssa_name (i))))
7194 continue;
7196 equiv_vr = get_vr_for_comparison (i);
7197 sop = false;
7198 t = compare_range_with_value (comp, &equiv_vr, val, &sop);
7199 if (t)
7201 /* If we get different answers from different members
7202 of the equivalence set this check must be in a dead
7203 code region. Folding it to a trap representation
7204 would be correct here. For now just return don't-know. */
7205 if (retval != NULL
7206 && t != retval)
7208 retval = NULL_TREE;
7209 break;
7211 retval = t;
7213 if (!sop)
7214 used_strict_overflow = 0;
7215 else if (used_strict_overflow < 0)
7216 used_strict_overflow = 1;
7220 if (retval
7221 && used_strict_overflow > 0)
7222 *strict_overflow_p = true;
7224 return retval;
7228 /* Given a comparison code COMP and names N1 and N2, compare all the
7229 ranges equivalent to N1 against all the ranges equivalent to N2
7230 to determine the value of N1 COMP N2. Return the same value
7231 returned by compare_ranges. Set *STRICT_OVERFLOW_P to indicate
7232 whether we relied on an overflow infinity in the comparison. */
7235 static tree
7236 compare_names (enum tree_code comp, tree n1, tree n2,
7237 bool *strict_overflow_p)
7239 tree t, retval;
7240 bitmap e1, e2;
7241 bitmap_iterator bi1, bi2;
7242 unsigned i1, i2;
7243 int used_strict_overflow;
7244 static bitmap_obstack *s_obstack = NULL;
7245 static bitmap s_e1 = NULL, s_e2 = NULL;
7247 /* Compare the ranges of every name equivalent to N1 against the
7248 ranges of every name equivalent to N2. */
7249 e1 = get_value_range (n1)->equiv;
7250 e2 = get_value_range (n2)->equiv;
7252 /* Use the fake bitmaps if e1 or e2 are not available. */
7253 if (s_obstack == NULL)
7255 s_obstack = XNEW (bitmap_obstack);
7256 bitmap_obstack_initialize (s_obstack);
7257 s_e1 = BITMAP_ALLOC (s_obstack);
7258 s_e2 = BITMAP_ALLOC (s_obstack);
7260 if (e1 == NULL)
7261 e1 = s_e1;
7262 if (e2 == NULL)
7263 e2 = s_e2;
7265 /* Add N1 and N2 to their own set of equivalences to avoid
7266 duplicating the body of the loop just to check N1 and N2
7267 ranges. */
7268 bitmap_set_bit (e1, SSA_NAME_VERSION (n1));
7269 bitmap_set_bit (e2, SSA_NAME_VERSION (n2));
7271 /* If the equivalence sets have a common intersection, then the two
7272 names can be compared without checking their ranges. */
7273 if (bitmap_intersect_p (e1, e2))
7275 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
7276 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
7278 return (comp == EQ_EXPR || comp == GE_EXPR || comp == LE_EXPR)
7279 ? boolean_true_node
7280 : boolean_false_node;
7283 /* Start at -1. Set it to 0 if we do a comparison without relying
7284 on overflow, or 1 if all comparisons rely on overflow. */
7285 used_strict_overflow = -1;
7287 /* Otherwise, compare all the equivalent ranges. First, add N1 and
7288 N2 to their own set of equivalences to avoid duplicating the body
7289 of the loop just to check N1 and N2 ranges. */
7290 EXECUTE_IF_SET_IN_BITMAP (e1, 0, i1, bi1)
7292 value_range vr1 = get_vr_for_comparison (i1);
7294 t = retval = NULL_TREE;
7295 EXECUTE_IF_SET_IN_BITMAP (e2, 0, i2, bi2)
7297 bool sop = false;
7299 value_range vr2 = get_vr_for_comparison (i2);
7301 t = compare_ranges (comp, &vr1, &vr2, &sop);
7302 if (t)
7304 /* If we get different answers from different members
7305 of the equivalence set this check must be in a dead
7306 code region. Folding it to a trap representation
7307 would be correct here. For now just return don't-know. */
7308 if (retval != NULL
7309 && t != retval)
7311 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
7312 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
7313 return NULL_TREE;
7315 retval = t;
7317 if (!sop)
7318 used_strict_overflow = 0;
7319 else if (used_strict_overflow < 0)
7320 used_strict_overflow = 1;
7324 if (retval)
7326 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
7327 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
7328 if (used_strict_overflow > 0)
7329 *strict_overflow_p = true;
7330 return retval;
7334 /* None of the equivalent ranges are useful in computing this
7335 comparison. */
7336 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
7337 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
7338 return NULL_TREE;
7341 /* Helper function for vrp_evaluate_conditional_warnv & other
7342 optimizers. */
7344 static tree
7345 vrp_evaluate_conditional_warnv_with_ops_using_ranges (enum tree_code code,
7346 tree op0, tree op1,
7347 bool * strict_overflow_p)
7349 value_range *vr0, *vr1;
7351 vr0 = (TREE_CODE (op0) == SSA_NAME) ? get_value_range (op0) : NULL;
7352 vr1 = (TREE_CODE (op1) == SSA_NAME) ? get_value_range (op1) : NULL;
7354 tree res = NULL_TREE;
7355 if (vr0 && vr1)
7356 res = compare_ranges (code, vr0, vr1, strict_overflow_p);
7357 if (!res && vr0)
7358 res = compare_range_with_value (code, vr0, op1, strict_overflow_p);
7359 if (!res && vr1)
7360 res = (compare_range_with_value
7361 (swap_tree_comparison (code), vr1, op0, strict_overflow_p));
7362 return res;
7365 /* Helper function for vrp_evaluate_conditional_warnv. */
7367 static tree
7368 vrp_evaluate_conditional_warnv_with_ops (enum tree_code code, tree op0,
7369 tree op1, bool use_equiv_p,
7370 bool *strict_overflow_p, bool *only_ranges)
7372 tree ret;
7373 if (only_ranges)
7374 *only_ranges = true;
7376 /* We only deal with integral and pointer types. */
7377 if (!INTEGRAL_TYPE_P (TREE_TYPE (op0))
7378 && !POINTER_TYPE_P (TREE_TYPE (op0)))
7379 return NULL_TREE;
7381 if ((ret = vrp_evaluate_conditional_warnv_with_ops_using_ranges
7382 (code, op0, op1, strict_overflow_p)))
7383 return ret;
7384 if (only_ranges)
7385 *only_ranges = false;
7386 /* Do not use compare_names during propagation, it's quadratic. */
7387 if (TREE_CODE (op0) == SSA_NAME && TREE_CODE (op1) == SSA_NAME
7388 && use_equiv_p)
7389 return compare_names (code, op0, op1, strict_overflow_p);
7390 else if (TREE_CODE (op0) == SSA_NAME)
7391 return compare_name_with_value (code, op0, op1,
7392 strict_overflow_p, use_equiv_p);
7393 else if (TREE_CODE (op1) == SSA_NAME)
7394 return compare_name_with_value (swap_tree_comparison (code), op1, op0,
7395 strict_overflow_p, use_equiv_p);
7396 return NULL_TREE;
7399 /* Given (CODE OP0 OP1) within STMT, try to simplify it based on value range
7400 information. Return NULL if the conditional can not be evaluated.
7401 The ranges of all the names equivalent with the operands in COND
7402 will be used when trying to compute the value. If the result is
7403 based on undefined signed overflow, issue a warning if
7404 appropriate. */
7406 static tree
7407 vrp_evaluate_conditional (tree_code code, tree op0, tree op1, gimple *stmt)
7409 bool sop;
7410 tree ret;
7411 bool only_ranges;
7413 /* Some passes and foldings leak constants with overflow flag set
7414 into the IL. Avoid doing wrong things with these and bail out. */
7415 if ((TREE_CODE (op0) == INTEGER_CST
7416 && TREE_OVERFLOW (op0))
7417 || (TREE_CODE (op1) == INTEGER_CST
7418 && TREE_OVERFLOW (op1)))
7419 return NULL_TREE;
7421 sop = false;
7422 ret = vrp_evaluate_conditional_warnv_with_ops (code, op0, op1, true, &sop,
7423 &only_ranges);
7425 if (ret && sop)
7427 enum warn_strict_overflow_code wc;
7428 const char* warnmsg;
7430 if (is_gimple_min_invariant (ret))
7432 wc = WARN_STRICT_OVERFLOW_CONDITIONAL;
7433 warnmsg = G_("assuming signed overflow does not occur when "
7434 "simplifying conditional to constant");
7436 else
7438 wc = WARN_STRICT_OVERFLOW_COMPARISON;
7439 warnmsg = G_("assuming signed overflow does not occur when "
7440 "simplifying conditional");
7443 if (issue_strict_overflow_warning (wc))
7445 location_t location;
7447 if (!gimple_has_location (stmt))
7448 location = input_location;
7449 else
7450 location = gimple_location (stmt);
7451 warning_at (location, OPT_Wstrict_overflow, "%s", warnmsg);
7455 if (warn_type_limits
7456 && ret && only_ranges
7457 && TREE_CODE_CLASS (code) == tcc_comparison
7458 && TREE_CODE (op0) == SSA_NAME)
7460 /* If the comparison is being folded and the operand on the LHS
7461 is being compared against a constant value that is outside of
7462 the natural range of OP0's type, then the predicate will
7463 always fold regardless of the value of OP0. If -Wtype-limits
7464 was specified, emit a warning. */
7465 tree type = TREE_TYPE (op0);
7466 value_range *vr0 = get_value_range (op0);
7468 if (vr0->type == VR_RANGE
7469 && INTEGRAL_TYPE_P (type)
7470 && vrp_val_is_min (vr0->min)
7471 && vrp_val_is_max (vr0->max)
7472 && is_gimple_min_invariant (op1))
7474 location_t location;
7476 if (!gimple_has_location (stmt))
7477 location = input_location;
7478 else
7479 location = gimple_location (stmt);
7481 warning_at (location, OPT_Wtype_limits,
7482 integer_zerop (ret)
7483 ? G_("comparison always false "
7484 "due to limited range of data type")
7485 : G_("comparison always true "
7486 "due to limited range of data type"));
7490 return ret;
7494 /* Visit conditional statement STMT. If we can determine which edge
7495 will be taken out of STMT's basic block, record it in
7496 *TAKEN_EDGE_P. Otherwise, set *TAKEN_EDGE_P to NULL. */
7498 static void
7499 vrp_visit_cond_stmt (gcond *stmt, edge *taken_edge_p)
7501 tree val;
7502 bool sop;
7504 *taken_edge_p = NULL;
7506 if (dump_file && (dump_flags & TDF_DETAILS))
7508 tree use;
7509 ssa_op_iter i;
7511 fprintf (dump_file, "\nVisiting conditional with predicate: ");
7512 print_gimple_stmt (dump_file, stmt, 0, 0);
7513 fprintf (dump_file, "\nWith known ranges\n");
7515 FOR_EACH_SSA_TREE_OPERAND (use, stmt, i, SSA_OP_USE)
7517 fprintf (dump_file, "\t");
7518 print_generic_expr (dump_file, use, 0);
7519 fprintf (dump_file, ": ");
7520 dump_value_range (dump_file, vr_value[SSA_NAME_VERSION (use)]);
7523 fprintf (dump_file, "\n");
7526 /* Compute the value of the predicate COND by checking the known
7527 ranges of each of its operands.
7529 Note that we cannot evaluate all the equivalent ranges here
7530 because those ranges may not yet be final and with the current
7531 propagation strategy, we cannot determine when the value ranges
7532 of the names in the equivalence set have changed.
7534 For instance, given the following code fragment
7536 i_5 = PHI <8, i_13>
7538 i_14 = ASSERT_EXPR <i_5, i_5 != 0>
7539 if (i_14 == 1)
7542 Assume that on the first visit to i_14, i_5 has the temporary
7543 range [8, 8] because the second argument to the PHI function is
7544 not yet executable. We derive the range ~[0, 0] for i_14 and the
7545 equivalence set { i_5 }. So, when we visit 'if (i_14 == 1)' for
7546 the first time, since i_14 is equivalent to the range [8, 8], we
7547 determine that the predicate is always false.
7549 On the next round of propagation, i_13 is determined to be
7550 VARYING, which causes i_5 to drop down to VARYING. So, another
7551 visit to i_14 is scheduled. In this second visit, we compute the
7552 exact same range and equivalence set for i_14, namely ~[0, 0] and
7553 { i_5 }. But we did not have the previous range for i_5
7554 registered, so vrp_visit_assignment thinks that the range for
7555 i_14 has not changed. Therefore, the predicate 'if (i_14 == 1)'
7556 is not visited again, which stops propagation from visiting
7557 statements in the THEN clause of that if().
7559 To properly fix this we would need to keep the previous range
7560 value for the names in the equivalence set. This way we would've
7561 discovered that from one visit to the other i_5 changed from
7562 range [8, 8] to VR_VARYING.
7564 However, fixing this apparent limitation may not be worth the
7565 additional checking. Testing on several code bases (GCC, DLV,
7566 MICO, TRAMP3D and SPEC2000) showed that doing this results in
7567 4 more predicates folded in SPEC. */
7568 sop = false;
7570 val = vrp_evaluate_conditional_warnv_with_ops (gimple_cond_code (stmt),
7571 gimple_cond_lhs (stmt),
7572 gimple_cond_rhs (stmt),
7573 false, &sop, NULL);
7574 if (val)
7576 if (!sop)
7577 *taken_edge_p = find_taken_edge (gimple_bb (stmt), val);
7578 else
7580 if (dump_file && (dump_flags & TDF_DETAILS))
7581 fprintf (dump_file,
7582 "\nIgnoring predicate evaluation because "
7583 "it assumes that signed overflow is undefined");
7584 val = NULL_TREE;
7588 if (dump_file && (dump_flags & TDF_DETAILS))
7590 fprintf (dump_file, "\nPredicate evaluates to: ");
7591 if (val == NULL_TREE)
7592 fprintf (dump_file, "DON'T KNOW\n");
7593 else
7594 print_generic_stmt (dump_file, val, 0);
7598 /* Searches the case label vector VEC for the index *IDX of the CASE_LABEL
7599 that includes the value VAL. The search is restricted to the range
7600 [START_IDX, n - 1] where n is the size of VEC.
7602 If there is a CASE_LABEL for VAL, its index is placed in IDX and true is
7603 returned.
7605 If there is no CASE_LABEL for VAL and there is one that is larger than VAL,
7606 it is placed in IDX and false is returned.
7608 If VAL is larger than any CASE_LABEL, n is placed on IDX and false is
7609 returned. */
7611 static bool
7612 find_case_label_index (gswitch *stmt, size_t start_idx, tree val, size_t *idx)
7614 size_t n = gimple_switch_num_labels (stmt);
7615 size_t low, high;
7617 /* Find case label for minimum of the value range or the next one.
7618 At each iteration we are searching in [low, high - 1]. */
7620 for (low = start_idx, high = n; high != low; )
7622 tree t;
7623 int cmp;
7624 /* Note that i != high, so we never ask for n. */
7625 size_t i = (high + low) / 2;
7626 t = gimple_switch_label (stmt, i);
7628 /* Cache the result of comparing CASE_LOW and val. */
7629 cmp = tree_int_cst_compare (CASE_LOW (t), val);
7631 if (cmp == 0)
7633 /* Ranges cannot be empty. */
7634 *idx = i;
7635 return true;
7637 else if (cmp > 0)
7638 high = i;
7639 else
7641 low = i + 1;
7642 if (CASE_HIGH (t) != NULL
7643 && tree_int_cst_compare (CASE_HIGH (t), val) >= 0)
7645 *idx = i;
7646 return true;
7651 *idx = high;
7652 return false;
7655 /* Searches the case label vector VEC for the range of CASE_LABELs that is used
7656 for values between MIN and MAX. The first index is placed in MIN_IDX. The
7657 last index is placed in MAX_IDX. If the range of CASE_LABELs is empty
7658 then MAX_IDX < MIN_IDX.
7659 Returns true if the default label is not needed. */
7661 static bool
7662 find_case_label_range (gswitch *stmt, tree min, tree max, size_t *min_idx,
7663 size_t *max_idx)
7665 size_t i, j;
7666 bool min_take_default = !find_case_label_index (stmt, 1, min, &i);
7667 bool max_take_default = !find_case_label_index (stmt, i, max, &j);
7669 if (i == j
7670 && min_take_default
7671 && max_take_default)
7673 /* Only the default case label reached.
7674 Return an empty range. */
7675 *min_idx = 1;
7676 *max_idx = 0;
7677 return false;
7679 else
7681 bool take_default = min_take_default || max_take_default;
7682 tree low, high;
7683 size_t k;
7685 if (max_take_default)
7686 j--;
7688 /* If the case label range is continuous, we do not need
7689 the default case label. Verify that. */
7690 high = CASE_LOW (gimple_switch_label (stmt, i));
7691 if (CASE_HIGH (gimple_switch_label (stmt, i)))
7692 high = CASE_HIGH (gimple_switch_label (stmt, i));
7693 for (k = i + 1; k <= j; ++k)
7695 low = CASE_LOW (gimple_switch_label (stmt, k));
7696 if (!integer_onep (int_const_binop (MINUS_EXPR, low, high)))
7698 take_default = true;
7699 break;
7701 high = low;
7702 if (CASE_HIGH (gimple_switch_label (stmt, k)))
7703 high = CASE_HIGH (gimple_switch_label (stmt, k));
7706 *min_idx = i;
7707 *max_idx = j;
7708 return !take_default;
7712 /* Searches the case label vector VEC for the ranges of CASE_LABELs that are
7713 used in range VR. The indices are placed in MIN_IDX1, MAX_IDX, MIN_IDX2 and
7714 MAX_IDX2. If the ranges of CASE_LABELs are empty then MAX_IDX1 < MIN_IDX1.
7715 Returns true if the default label is not needed. */
7717 static bool
7718 find_case_label_ranges (gswitch *stmt, value_range *vr, size_t *min_idx1,
7719 size_t *max_idx1, size_t *min_idx2,
7720 size_t *max_idx2)
7722 size_t i, j, k, l;
7723 unsigned int n = gimple_switch_num_labels (stmt);
7724 bool take_default;
7725 tree case_low, case_high;
7726 tree min = vr->min, max = vr->max;
7728 gcc_checking_assert (vr->type == VR_RANGE || vr->type == VR_ANTI_RANGE);
7730 take_default = !find_case_label_range (stmt, min, max, &i, &j);
7732 /* Set second range to emtpy. */
7733 *min_idx2 = 1;
7734 *max_idx2 = 0;
7736 if (vr->type == VR_RANGE)
7738 *min_idx1 = i;
7739 *max_idx1 = j;
7740 return !take_default;
7743 /* Set first range to all case labels. */
7744 *min_idx1 = 1;
7745 *max_idx1 = n - 1;
7747 if (i > j)
7748 return false;
7750 /* Make sure all the values of case labels [i , j] are contained in
7751 range [MIN, MAX]. */
7752 case_low = CASE_LOW (gimple_switch_label (stmt, i));
7753 case_high = CASE_HIGH (gimple_switch_label (stmt, j));
7754 if (tree_int_cst_compare (case_low, min) < 0)
7755 i += 1;
7756 if (case_high != NULL_TREE
7757 && tree_int_cst_compare (max, case_high) < 0)
7758 j -= 1;
7760 if (i > j)
7761 return false;
7763 /* If the range spans case labels [i, j], the corresponding anti-range spans
7764 the labels [1, i - 1] and [j + 1, n - 1]. */
7765 k = j + 1;
7766 l = n - 1;
7767 if (k > l)
7769 k = 1;
7770 l = 0;
7773 j = i - 1;
7774 i = 1;
7775 if (i > j)
7777 i = k;
7778 j = l;
7779 k = 1;
7780 l = 0;
7783 *min_idx1 = i;
7784 *max_idx1 = j;
7785 *min_idx2 = k;
7786 *max_idx2 = l;
7787 return false;
7790 /* Visit switch statement STMT. If we can determine which edge
7791 will be taken out of STMT's basic block, record it in
7792 *TAKEN_EDGE_P. Otherwise, *TAKEN_EDGE_P set to NULL. */
7794 static void
7795 vrp_visit_switch_stmt (gswitch *stmt, edge *taken_edge_p)
7797 tree op, val;
7798 value_range *vr;
7799 size_t i = 0, j = 0, k, l;
7800 bool take_default;
7802 *taken_edge_p = NULL;
7803 op = gimple_switch_index (stmt);
7804 if (TREE_CODE (op) != SSA_NAME)
7805 return;
7807 vr = get_value_range (op);
7808 if (dump_file && (dump_flags & TDF_DETAILS))
7810 fprintf (dump_file, "\nVisiting switch expression with operand ");
7811 print_generic_expr (dump_file, op, 0);
7812 fprintf (dump_file, " with known range ");
7813 dump_value_range (dump_file, vr);
7814 fprintf (dump_file, "\n");
7817 if ((vr->type != VR_RANGE
7818 && vr->type != VR_ANTI_RANGE)
7819 || symbolic_range_p (vr))
7820 return;
7822 /* Find the single edge that is taken from the switch expression. */
7823 take_default = !find_case_label_ranges (stmt, vr, &i, &j, &k, &l);
7825 /* Check if the range spans no CASE_LABEL. If so, we only reach the default
7826 label */
7827 if (j < i)
7829 gcc_assert (take_default);
7830 val = gimple_switch_default_label (stmt);
7832 else
7834 /* Check if labels with index i to j and maybe the default label
7835 are all reaching the same label. */
7837 val = gimple_switch_label (stmt, i);
7838 if (take_default
7839 && CASE_LABEL (gimple_switch_default_label (stmt))
7840 != CASE_LABEL (val))
7842 if (dump_file && (dump_flags & TDF_DETAILS))
7843 fprintf (dump_file, " not a single destination for this "
7844 "range\n");
7845 return;
7847 for (++i; i <= j; ++i)
7849 if (CASE_LABEL (gimple_switch_label (stmt, i)) != CASE_LABEL (val))
7851 if (dump_file && (dump_flags & TDF_DETAILS))
7852 fprintf (dump_file, " not a single destination for this "
7853 "range\n");
7854 return;
7857 for (; k <= l; ++k)
7859 if (CASE_LABEL (gimple_switch_label (stmt, k)) != CASE_LABEL (val))
7861 if (dump_file && (dump_flags & TDF_DETAILS))
7862 fprintf (dump_file, " not a single destination for this "
7863 "range\n");
7864 return;
7869 *taken_edge_p = find_edge (gimple_bb (stmt),
7870 label_to_block (CASE_LABEL (val)));
7872 if (dump_file && (dump_flags & TDF_DETAILS))
7874 fprintf (dump_file, " will take edge to ");
7875 print_generic_stmt (dump_file, CASE_LABEL (val), 0);
7880 /* Evaluate statement STMT. If the statement produces a useful range,
7881 set VR and corepsponding OUTPUT_P.
7883 If STMT is a conditional branch and we can determine its truth
7884 value, the taken edge is recorded in *TAKEN_EDGE_P. */
7886 static void
7887 extract_range_from_stmt (gimple *stmt, edge *taken_edge_p,
7888 tree *output_p, value_range *vr)
7891 if (dump_file && (dump_flags & TDF_DETAILS))
7893 fprintf (dump_file, "\nVisiting statement:\n");
7894 print_gimple_stmt (dump_file, stmt, 0, dump_flags);
7897 if (!stmt_interesting_for_vrp (stmt))
7898 gcc_assert (stmt_ends_bb_p (stmt));
7899 else if (is_gimple_assign (stmt) || is_gimple_call (stmt))
7900 vrp_visit_assignment_or_call (stmt, output_p, vr);
7901 else if (gimple_code (stmt) == GIMPLE_COND)
7902 vrp_visit_cond_stmt (as_a <gcond *> (stmt), taken_edge_p);
7903 else if (gimple_code (stmt) == GIMPLE_SWITCH)
7904 vrp_visit_switch_stmt (as_a <gswitch *> (stmt), taken_edge_p);
7907 /* Evaluate statement STMT. If the statement produces a useful range,
7908 return SSA_PROP_INTERESTING and record the SSA name with the
7909 interesting range into *OUTPUT_P.
7911 If STMT is a conditional branch and we can determine its truth
7912 value, the taken edge is recorded in *TAKEN_EDGE_P.
7914 If STMT produces a varying value, return SSA_PROP_VARYING. */
7916 static enum ssa_prop_result
7917 vrp_visit_stmt (gimple *stmt, edge *taken_edge_p, tree *output_p)
7919 value_range vr = VR_INITIALIZER;
7920 tree lhs = gimple_get_lhs (stmt);
7921 extract_range_from_stmt (stmt, taken_edge_p, output_p, &vr);
7923 if (*output_p)
7925 if (update_value_range (*output_p, &vr))
7927 if (dump_file && (dump_flags & TDF_DETAILS))
7929 fprintf (dump_file, "Found new range for ");
7930 print_generic_expr (dump_file, *output_p, 0);
7931 fprintf (dump_file, ": ");
7932 dump_value_range (dump_file, &vr);
7933 fprintf (dump_file, "\n");
7936 if (vr.type == VR_VARYING)
7937 return SSA_PROP_VARYING;
7939 return SSA_PROP_INTERESTING;
7941 return SSA_PROP_NOT_INTERESTING;
7944 if (is_gimple_call (stmt) && gimple_call_internal_p (stmt))
7945 switch (gimple_call_internal_fn (stmt))
7947 case IFN_ADD_OVERFLOW:
7948 case IFN_SUB_OVERFLOW:
7949 case IFN_MUL_OVERFLOW:
7950 /* These internal calls return _Complex integer type,
7951 which VRP does not track, but the immediate uses
7952 thereof might be interesting. */
7953 if (lhs && TREE_CODE (lhs) == SSA_NAME)
7955 imm_use_iterator iter;
7956 use_operand_p use_p;
7957 enum ssa_prop_result res = SSA_PROP_VARYING;
7959 set_value_range_to_varying (get_value_range (lhs));
7961 FOR_EACH_IMM_USE_FAST (use_p, iter, lhs)
7963 gimple *use_stmt = USE_STMT (use_p);
7964 if (!is_gimple_assign (use_stmt))
7965 continue;
7966 enum tree_code rhs_code = gimple_assign_rhs_code (use_stmt);
7967 if (rhs_code != REALPART_EXPR && rhs_code != IMAGPART_EXPR)
7968 continue;
7969 tree rhs1 = gimple_assign_rhs1 (use_stmt);
7970 tree use_lhs = gimple_assign_lhs (use_stmt);
7971 if (TREE_CODE (rhs1) != rhs_code
7972 || TREE_OPERAND (rhs1, 0) != lhs
7973 || TREE_CODE (use_lhs) != SSA_NAME
7974 || !stmt_interesting_for_vrp (use_stmt)
7975 || (!INTEGRAL_TYPE_P (TREE_TYPE (use_lhs))
7976 || !TYPE_MIN_VALUE (TREE_TYPE (use_lhs))
7977 || !TYPE_MAX_VALUE (TREE_TYPE (use_lhs))))
7978 continue;
7980 /* If there is a change in the value range for any of the
7981 REALPART_EXPR/IMAGPART_EXPR immediate uses, return
7982 SSA_PROP_INTERESTING. If there are any REALPART_EXPR
7983 or IMAGPART_EXPR immediate uses, but none of them have
7984 a change in their value ranges, return
7985 SSA_PROP_NOT_INTERESTING. If there are no
7986 {REAL,IMAG}PART_EXPR uses at all,
7987 return SSA_PROP_VARYING. */
7988 value_range new_vr = VR_INITIALIZER;
7989 extract_range_basic (&new_vr, use_stmt);
7990 value_range *old_vr = get_value_range (use_lhs);
7991 if (old_vr->type != new_vr.type
7992 || !vrp_operand_equal_p (old_vr->min, new_vr.min)
7993 || !vrp_operand_equal_p (old_vr->max, new_vr.max)
7994 || !vrp_bitmap_equal_p (old_vr->equiv, new_vr.equiv))
7995 res = SSA_PROP_INTERESTING;
7996 else
7997 res = SSA_PROP_NOT_INTERESTING;
7998 BITMAP_FREE (new_vr.equiv);
7999 if (res == SSA_PROP_INTERESTING)
8001 *output_p = lhs;
8002 return res;
8006 return res;
8008 break;
8009 default:
8010 break;
8013 /* All other statements produce nothing of interest for VRP, so mark
8014 their outputs varying and prevent further simulation. */
8015 set_defs_to_varying (stmt);
8017 return (*taken_edge_p) ? SSA_PROP_INTERESTING : SSA_PROP_VARYING;
8020 /* Union the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
8021 { VR1TYPE, VR0MIN, VR0MAX } and store the result
8022 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
8023 possible such range. The resulting range is not canonicalized. */
8025 static void
8026 union_ranges (enum value_range_type *vr0type,
8027 tree *vr0min, tree *vr0max,
8028 enum value_range_type vr1type,
8029 tree vr1min, tree vr1max)
8031 bool mineq = vrp_operand_equal_p (*vr0min, vr1min);
8032 bool maxeq = vrp_operand_equal_p (*vr0max, vr1max);
8034 /* [] is vr0, () is vr1 in the following classification comments. */
8035 if (mineq && maxeq)
8037 /* [( )] */
8038 if (*vr0type == vr1type)
8039 /* Nothing to do for equal ranges. */
8041 else if ((*vr0type == VR_RANGE
8042 && vr1type == VR_ANTI_RANGE)
8043 || (*vr0type == VR_ANTI_RANGE
8044 && vr1type == VR_RANGE))
8046 /* For anti-range with range union the result is varying. */
8047 goto give_up;
8049 else
8050 gcc_unreachable ();
8052 else if (operand_less_p (*vr0max, vr1min) == 1
8053 || operand_less_p (vr1max, *vr0min) == 1)
8055 /* [ ] ( ) or ( ) [ ]
8056 If the ranges have an empty intersection, result of the union
8057 operation is the anti-range or if both are anti-ranges
8058 it covers all. */
8059 if (*vr0type == VR_ANTI_RANGE
8060 && vr1type == VR_ANTI_RANGE)
8061 goto give_up;
8062 else if (*vr0type == VR_ANTI_RANGE
8063 && vr1type == VR_RANGE)
8065 else if (*vr0type == VR_RANGE
8066 && vr1type == VR_ANTI_RANGE)
8068 *vr0type = vr1type;
8069 *vr0min = vr1min;
8070 *vr0max = vr1max;
8072 else if (*vr0type == VR_RANGE
8073 && vr1type == VR_RANGE)
8075 /* The result is the convex hull of both ranges. */
8076 if (operand_less_p (*vr0max, vr1min) == 1)
8078 /* If the result can be an anti-range, create one. */
8079 if (TREE_CODE (*vr0max) == INTEGER_CST
8080 && TREE_CODE (vr1min) == INTEGER_CST
8081 && vrp_val_is_min (*vr0min)
8082 && vrp_val_is_max (vr1max))
8084 tree min = int_const_binop (PLUS_EXPR,
8085 *vr0max,
8086 build_int_cst (TREE_TYPE (*vr0max), 1));
8087 tree max = int_const_binop (MINUS_EXPR,
8088 vr1min,
8089 build_int_cst (TREE_TYPE (vr1min), 1));
8090 if (!operand_less_p (max, min))
8092 *vr0type = VR_ANTI_RANGE;
8093 *vr0min = min;
8094 *vr0max = max;
8096 else
8097 *vr0max = vr1max;
8099 else
8100 *vr0max = vr1max;
8102 else
8104 /* If the result can be an anti-range, create one. */
8105 if (TREE_CODE (vr1max) == INTEGER_CST
8106 && TREE_CODE (*vr0min) == INTEGER_CST
8107 && vrp_val_is_min (vr1min)
8108 && vrp_val_is_max (*vr0max))
8110 tree min = int_const_binop (PLUS_EXPR,
8111 vr1max,
8112 build_int_cst (TREE_TYPE (vr1max), 1));
8113 tree max = int_const_binop (MINUS_EXPR,
8114 *vr0min,
8115 build_int_cst (TREE_TYPE (*vr0min), 1));
8116 if (!operand_less_p (max, min))
8118 *vr0type = VR_ANTI_RANGE;
8119 *vr0min = min;
8120 *vr0max = max;
8122 else
8123 *vr0min = vr1min;
8125 else
8126 *vr0min = vr1min;
8129 else
8130 gcc_unreachable ();
8132 else if ((maxeq || operand_less_p (vr1max, *vr0max) == 1)
8133 && (mineq || operand_less_p (*vr0min, vr1min) == 1))
8135 /* [ ( ) ] or [( ) ] or [ ( )] */
8136 if (*vr0type == VR_RANGE
8137 && vr1type == VR_RANGE)
8139 else if (*vr0type == VR_ANTI_RANGE
8140 && vr1type == VR_ANTI_RANGE)
8142 *vr0type = vr1type;
8143 *vr0min = vr1min;
8144 *vr0max = vr1max;
8146 else if (*vr0type == VR_ANTI_RANGE
8147 && vr1type == VR_RANGE)
8149 /* Arbitrarily choose the right or left gap. */
8150 if (!mineq && TREE_CODE (vr1min) == INTEGER_CST)
8151 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
8152 build_int_cst (TREE_TYPE (vr1min), 1));
8153 else if (!maxeq && TREE_CODE (vr1max) == INTEGER_CST)
8154 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
8155 build_int_cst (TREE_TYPE (vr1max), 1));
8156 else
8157 goto give_up;
8159 else if (*vr0type == VR_RANGE
8160 && vr1type == VR_ANTI_RANGE)
8161 /* The result covers everything. */
8162 goto give_up;
8163 else
8164 gcc_unreachable ();
8166 else if ((maxeq || operand_less_p (*vr0max, vr1max) == 1)
8167 && (mineq || operand_less_p (vr1min, *vr0min) == 1))
8169 /* ( [ ] ) or ([ ] ) or ( [ ]) */
8170 if (*vr0type == VR_RANGE
8171 && vr1type == VR_RANGE)
8173 *vr0type = vr1type;
8174 *vr0min = vr1min;
8175 *vr0max = vr1max;
8177 else if (*vr0type == VR_ANTI_RANGE
8178 && vr1type == VR_ANTI_RANGE)
8180 else if (*vr0type == VR_RANGE
8181 && vr1type == VR_ANTI_RANGE)
8183 *vr0type = VR_ANTI_RANGE;
8184 if (!mineq && TREE_CODE (*vr0min) == INTEGER_CST)
8186 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
8187 build_int_cst (TREE_TYPE (*vr0min), 1));
8188 *vr0min = vr1min;
8190 else if (!maxeq && TREE_CODE (*vr0max) == INTEGER_CST)
8192 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
8193 build_int_cst (TREE_TYPE (*vr0max), 1));
8194 *vr0max = vr1max;
8196 else
8197 goto give_up;
8199 else if (*vr0type == VR_ANTI_RANGE
8200 && vr1type == VR_RANGE)
8201 /* The result covers everything. */
8202 goto give_up;
8203 else
8204 gcc_unreachable ();
8206 else if ((operand_less_p (vr1min, *vr0max) == 1
8207 || operand_equal_p (vr1min, *vr0max, 0))
8208 && operand_less_p (*vr0min, vr1min) == 1
8209 && operand_less_p (*vr0max, vr1max) == 1)
8211 /* [ ( ] ) or [ ]( ) */
8212 if (*vr0type == VR_RANGE
8213 && vr1type == VR_RANGE)
8214 *vr0max = vr1max;
8215 else if (*vr0type == VR_ANTI_RANGE
8216 && vr1type == VR_ANTI_RANGE)
8217 *vr0min = vr1min;
8218 else if (*vr0type == VR_ANTI_RANGE
8219 && vr1type == VR_RANGE)
8221 if (TREE_CODE (vr1min) == INTEGER_CST)
8222 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
8223 build_int_cst (TREE_TYPE (vr1min), 1));
8224 else
8225 goto give_up;
8227 else if (*vr0type == VR_RANGE
8228 && vr1type == VR_ANTI_RANGE)
8230 if (TREE_CODE (*vr0max) == INTEGER_CST)
8232 *vr0type = vr1type;
8233 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
8234 build_int_cst (TREE_TYPE (*vr0max), 1));
8235 *vr0max = vr1max;
8237 else
8238 goto give_up;
8240 else
8241 gcc_unreachable ();
8243 else if ((operand_less_p (*vr0min, vr1max) == 1
8244 || operand_equal_p (*vr0min, vr1max, 0))
8245 && operand_less_p (vr1min, *vr0min) == 1
8246 && operand_less_p (vr1max, *vr0max) == 1)
8248 /* ( [ ) ] or ( )[ ] */
8249 if (*vr0type == VR_RANGE
8250 && vr1type == VR_RANGE)
8251 *vr0min = vr1min;
8252 else if (*vr0type == VR_ANTI_RANGE
8253 && vr1type == VR_ANTI_RANGE)
8254 *vr0max = vr1max;
8255 else if (*vr0type == VR_ANTI_RANGE
8256 && vr1type == VR_RANGE)
8258 if (TREE_CODE (vr1max) == INTEGER_CST)
8259 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
8260 build_int_cst (TREE_TYPE (vr1max), 1));
8261 else
8262 goto give_up;
8264 else if (*vr0type == VR_RANGE
8265 && vr1type == VR_ANTI_RANGE)
8267 if (TREE_CODE (*vr0min) == INTEGER_CST)
8269 *vr0type = vr1type;
8270 *vr0min = vr1min;
8271 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
8272 build_int_cst (TREE_TYPE (*vr0min), 1));
8274 else
8275 goto give_up;
8277 else
8278 gcc_unreachable ();
8280 else
8281 goto give_up;
8283 return;
8285 give_up:
8286 *vr0type = VR_VARYING;
8287 *vr0min = NULL_TREE;
8288 *vr0max = NULL_TREE;
8291 /* Intersect the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
8292 { VR1TYPE, VR0MIN, VR0MAX } and store the result
8293 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
8294 possible such range. The resulting range is not canonicalized. */
8296 static void
8297 intersect_ranges (enum value_range_type *vr0type,
8298 tree *vr0min, tree *vr0max,
8299 enum value_range_type vr1type,
8300 tree vr1min, tree vr1max)
8302 bool mineq = vrp_operand_equal_p (*vr0min, vr1min);
8303 bool maxeq = vrp_operand_equal_p (*vr0max, vr1max);
8305 /* [] is vr0, () is vr1 in the following classification comments. */
8306 if (mineq && maxeq)
8308 /* [( )] */
8309 if (*vr0type == vr1type)
8310 /* Nothing to do for equal ranges. */
8312 else if ((*vr0type == VR_RANGE
8313 && vr1type == VR_ANTI_RANGE)
8314 || (*vr0type == VR_ANTI_RANGE
8315 && vr1type == VR_RANGE))
8317 /* For anti-range with range intersection the result is empty. */
8318 *vr0type = VR_UNDEFINED;
8319 *vr0min = NULL_TREE;
8320 *vr0max = NULL_TREE;
8322 else
8323 gcc_unreachable ();
8325 else if (operand_less_p (*vr0max, vr1min) == 1
8326 || operand_less_p (vr1max, *vr0min) == 1)
8328 /* [ ] ( ) or ( ) [ ]
8329 If the ranges have an empty intersection, the result of the
8330 intersect operation is the range for intersecting an
8331 anti-range with a range or empty when intersecting two ranges. */
8332 if (*vr0type == VR_RANGE
8333 && vr1type == VR_ANTI_RANGE)
8335 else if (*vr0type == VR_ANTI_RANGE
8336 && vr1type == VR_RANGE)
8338 *vr0type = vr1type;
8339 *vr0min = vr1min;
8340 *vr0max = vr1max;
8342 else if (*vr0type == VR_RANGE
8343 && vr1type == VR_RANGE)
8345 *vr0type = VR_UNDEFINED;
8346 *vr0min = NULL_TREE;
8347 *vr0max = NULL_TREE;
8349 else if (*vr0type == VR_ANTI_RANGE
8350 && vr1type == VR_ANTI_RANGE)
8352 /* If the anti-ranges are adjacent to each other merge them. */
8353 if (TREE_CODE (*vr0max) == INTEGER_CST
8354 && TREE_CODE (vr1min) == INTEGER_CST
8355 && operand_less_p (*vr0max, vr1min) == 1
8356 && integer_onep (int_const_binop (MINUS_EXPR,
8357 vr1min, *vr0max)))
8358 *vr0max = vr1max;
8359 else if (TREE_CODE (vr1max) == INTEGER_CST
8360 && TREE_CODE (*vr0min) == INTEGER_CST
8361 && operand_less_p (vr1max, *vr0min) == 1
8362 && integer_onep (int_const_binop (MINUS_EXPR,
8363 *vr0min, vr1max)))
8364 *vr0min = vr1min;
8365 /* Else arbitrarily take VR0. */
8368 else if ((maxeq || operand_less_p (vr1max, *vr0max) == 1)
8369 && (mineq || operand_less_p (*vr0min, vr1min) == 1))
8371 /* [ ( ) ] or [( ) ] or [ ( )] */
8372 if (*vr0type == VR_RANGE
8373 && vr1type == VR_RANGE)
8375 /* If both are ranges the result is the inner one. */
8376 *vr0type = vr1type;
8377 *vr0min = vr1min;
8378 *vr0max = vr1max;
8380 else if (*vr0type == VR_RANGE
8381 && vr1type == VR_ANTI_RANGE)
8383 /* Choose the right gap if the left one is empty. */
8384 if (mineq)
8386 if (TREE_CODE (vr1max) == INTEGER_CST)
8387 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
8388 build_int_cst (TREE_TYPE (vr1max), 1));
8389 else
8390 *vr0min = vr1max;
8392 /* Choose the left gap if the right one is empty. */
8393 else if (maxeq)
8395 if (TREE_CODE (vr1min) == INTEGER_CST)
8396 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
8397 build_int_cst (TREE_TYPE (vr1min), 1));
8398 else
8399 *vr0max = vr1min;
8401 /* Choose the anti-range if the range is effectively varying. */
8402 else if (vrp_val_is_min (*vr0min)
8403 && vrp_val_is_max (*vr0max))
8405 *vr0type = vr1type;
8406 *vr0min = vr1min;
8407 *vr0max = vr1max;
8409 /* Else choose the range. */
8411 else if (*vr0type == VR_ANTI_RANGE
8412 && vr1type == VR_ANTI_RANGE)
8413 /* If both are anti-ranges the result is the outer one. */
8415 else if (*vr0type == VR_ANTI_RANGE
8416 && vr1type == VR_RANGE)
8418 /* The intersection is empty. */
8419 *vr0type = VR_UNDEFINED;
8420 *vr0min = NULL_TREE;
8421 *vr0max = NULL_TREE;
8423 else
8424 gcc_unreachable ();
8426 else if ((maxeq || operand_less_p (*vr0max, vr1max) == 1)
8427 && (mineq || operand_less_p (vr1min, *vr0min) == 1))
8429 /* ( [ ] ) or ([ ] ) or ( [ ]) */
8430 if (*vr0type == VR_RANGE
8431 && vr1type == VR_RANGE)
8432 /* Choose the inner range. */
8434 else if (*vr0type == VR_ANTI_RANGE
8435 && vr1type == VR_RANGE)
8437 /* Choose the right gap if the left is empty. */
8438 if (mineq)
8440 *vr0type = VR_RANGE;
8441 if (TREE_CODE (*vr0max) == INTEGER_CST)
8442 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
8443 build_int_cst (TREE_TYPE (*vr0max), 1));
8444 else
8445 *vr0min = *vr0max;
8446 *vr0max = vr1max;
8448 /* Choose the left gap if the right is empty. */
8449 else if (maxeq)
8451 *vr0type = VR_RANGE;
8452 if (TREE_CODE (*vr0min) == INTEGER_CST)
8453 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
8454 build_int_cst (TREE_TYPE (*vr0min), 1));
8455 else
8456 *vr0max = *vr0min;
8457 *vr0min = vr1min;
8459 /* Choose the anti-range if the range is effectively varying. */
8460 else if (vrp_val_is_min (vr1min)
8461 && vrp_val_is_max (vr1max))
8463 /* Else choose the range. */
8464 else
8466 *vr0type = vr1type;
8467 *vr0min = vr1min;
8468 *vr0max = vr1max;
8471 else if (*vr0type == VR_ANTI_RANGE
8472 && vr1type == VR_ANTI_RANGE)
8474 /* If both are anti-ranges the result is the outer one. */
8475 *vr0type = vr1type;
8476 *vr0min = vr1min;
8477 *vr0max = vr1max;
8479 else if (vr1type == VR_ANTI_RANGE
8480 && *vr0type == VR_RANGE)
8482 /* The intersection is empty. */
8483 *vr0type = VR_UNDEFINED;
8484 *vr0min = NULL_TREE;
8485 *vr0max = NULL_TREE;
8487 else
8488 gcc_unreachable ();
8490 else if ((operand_less_p (vr1min, *vr0max) == 1
8491 || operand_equal_p (vr1min, *vr0max, 0))
8492 && operand_less_p (*vr0min, vr1min) == 1)
8494 /* [ ( ] ) or [ ]( ) */
8495 if (*vr0type == VR_ANTI_RANGE
8496 && vr1type == VR_ANTI_RANGE)
8497 *vr0max = vr1max;
8498 else if (*vr0type == VR_RANGE
8499 && vr1type == VR_RANGE)
8500 *vr0min = vr1min;
8501 else if (*vr0type == VR_RANGE
8502 && vr1type == VR_ANTI_RANGE)
8504 if (TREE_CODE (vr1min) == INTEGER_CST)
8505 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
8506 build_int_cst (TREE_TYPE (vr1min), 1));
8507 else
8508 *vr0max = vr1min;
8510 else if (*vr0type == VR_ANTI_RANGE
8511 && vr1type == VR_RANGE)
8513 *vr0type = VR_RANGE;
8514 if (TREE_CODE (*vr0max) == INTEGER_CST)
8515 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
8516 build_int_cst (TREE_TYPE (*vr0max), 1));
8517 else
8518 *vr0min = *vr0max;
8519 *vr0max = vr1max;
8521 else
8522 gcc_unreachable ();
8524 else if ((operand_less_p (*vr0min, vr1max) == 1
8525 || operand_equal_p (*vr0min, vr1max, 0))
8526 && operand_less_p (vr1min, *vr0min) == 1)
8528 /* ( [ ) ] or ( )[ ] */
8529 if (*vr0type == VR_ANTI_RANGE
8530 && vr1type == VR_ANTI_RANGE)
8531 *vr0min = vr1min;
8532 else if (*vr0type == VR_RANGE
8533 && vr1type == VR_RANGE)
8534 *vr0max = vr1max;
8535 else if (*vr0type == VR_RANGE
8536 && vr1type == VR_ANTI_RANGE)
8538 if (TREE_CODE (vr1max) == INTEGER_CST)
8539 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
8540 build_int_cst (TREE_TYPE (vr1max), 1));
8541 else
8542 *vr0min = vr1max;
8544 else if (*vr0type == VR_ANTI_RANGE
8545 && vr1type == VR_RANGE)
8547 *vr0type = VR_RANGE;
8548 if (TREE_CODE (*vr0min) == INTEGER_CST)
8549 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
8550 build_int_cst (TREE_TYPE (*vr0min), 1));
8551 else
8552 *vr0max = *vr0min;
8553 *vr0min = vr1min;
8555 else
8556 gcc_unreachable ();
8559 /* As a fallback simply use { *VRTYPE, *VR0MIN, *VR0MAX } as
8560 result for the intersection. That's always a conservative
8561 correct estimate unless VR1 is a constant singleton range
8562 in which case we choose that. */
8563 if (vr1type == VR_RANGE
8564 && is_gimple_min_invariant (vr1min)
8565 && vrp_operand_equal_p (vr1min, vr1max))
8567 *vr0type = vr1type;
8568 *vr0min = vr1min;
8569 *vr0max = vr1max;
8572 return;
8576 /* Intersect the two value-ranges *VR0 and *VR1 and store the result
8577 in *VR0. This may not be the smallest possible such range. */
8579 static void
8580 vrp_intersect_ranges_1 (value_range *vr0, value_range *vr1)
8582 value_range saved;
8584 /* If either range is VR_VARYING the other one wins. */
8585 if (vr1->type == VR_VARYING)
8586 return;
8587 if (vr0->type == VR_VARYING)
8589 copy_value_range (vr0, vr1);
8590 return;
8593 /* When either range is VR_UNDEFINED the resulting range is
8594 VR_UNDEFINED, too. */
8595 if (vr0->type == VR_UNDEFINED)
8596 return;
8597 if (vr1->type == VR_UNDEFINED)
8599 set_value_range_to_undefined (vr0);
8600 return;
8603 /* Save the original vr0 so we can return it as conservative intersection
8604 result when our worker turns things to varying. */
8605 saved = *vr0;
8606 intersect_ranges (&vr0->type, &vr0->min, &vr0->max,
8607 vr1->type, vr1->min, vr1->max);
8608 /* Make sure to canonicalize the result though as the inversion of a
8609 VR_RANGE can still be a VR_RANGE. */
8610 set_and_canonicalize_value_range (vr0, vr0->type,
8611 vr0->min, vr0->max, vr0->equiv);
8612 /* If that failed, use the saved original VR0. */
8613 if (vr0->type == VR_VARYING)
8615 *vr0 = saved;
8616 return;
8618 /* If the result is VR_UNDEFINED there is no need to mess with
8619 the equivalencies. */
8620 if (vr0->type == VR_UNDEFINED)
8621 return;
8623 /* The resulting set of equivalences for range intersection is the union of
8624 the two sets. */
8625 if (vr0->equiv && vr1->equiv && vr0->equiv != vr1->equiv)
8626 bitmap_ior_into (vr0->equiv, vr1->equiv);
8627 else if (vr1->equiv && !vr0->equiv)
8629 vr0->equiv = BITMAP_ALLOC (&vrp_equiv_obstack);
8630 bitmap_copy (vr0->equiv, vr1->equiv);
8634 void
8635 vrp_intersect_ranges (value_range *vr0, value_range *vr1)
8637 if (dump_file && (dump_flags & TDF_DETAILS))
8639 fprintf (dump_file, "Intersecting\n ");
8640 dump_value_range (dump_file, vr0);
8641 fprintf (dump_file, "\nand\n ");
8642 dump_value_range (dump_file, vr1);
8643 fprintf (dump_file, "\n");
8645 vrp_intersect_ranges_1 (vr0, vr1);
8646 if (dump_file && (dump_flags & TDF_DETAILS))
8648 fprintf (dump_file, "to\n ");
8649 dump_value_range (dump_file, vr0);
8650 fprintf (dump_file, "\n");
8654 /* Meet operation for value ranges. Given two value ranges VR0 and
8655 VR1, store in VR0 a range that contains both VR0 and VR1. This
8656 may not be the smallest possible such range. */
8658 static void
8659 vrp_meet_1 (value_range *vr0, const value_range *vr1)
8661 value_range saved;
8663 if (vr0->type == VR_UNDEFINED)
8665 set_value_range (vr0, vr1->type, vr1->min, vr1->max, vr1->equiv);
8666 return;
8669 if (vr1->type == VR_UNDEFINED)
8671 /* VR0 already has the resulting range. */
8672 return;
8675 if (vr0->type == VR_VARYING)
8677 /* Nothing to do. VR0 already has the resulting range. */
8678 return;
8681 if (vr1->type == VR_VARYING)
8683 set_value_range_to_varying (vr0);
8684 return;
8687 saved = *vr0;
8688 union_ranges (&vr0->type, &vr0->min, &vr0->max,
8689 vr1->type, vr1->min, vr1->max);
8690 if (vr0->type == VR_VARYING)
8692 /* Failed to find an efficient meet. Before giving up and setting
8693 the result to VARYING, see if we can at least derive a useful
8694 anti-range. FIXME, all this nonsense about distinguishing
8695 anti-ranges from ranges is necessary because of the odd
8696 semantics of range_includes_zero_p and friends. */
8697 if (((saved.type == VR_RANGE
8698 && range_includes_zero_p (saved.min, saved.max) == 0)
8699 || (saved.type == VR_ANTI_RANGE
8700 && range_includes_zero_p (saved.min, saved.max) == 1))
8701 && ((vr1->type == VR_RANGE
8702 && range_includes_zero_p (vr1->min, vr1->max) == 0)
8703 || (vr1->type == VR_ANTI_RANGE
8704 && range_includes_zero_p (vr1->min, vr1->max) == 1)))
8706 set_value_range_to_nonnull (vr0, TREE_TYPE (saved.min));
8708 /* Since this meet operation did not result from the meeting of
8709 two equivalent names, VR0 cannot have any equivalences. */
8710 if (vr0->equiv)
8711 bitmap_clear (vr0->equiv);
8712 return;
8715 set_value_range_to_varying (vr0);
8716 return;
8718 set_and_canonicalize_value_range (vr0, vr0->type, vr0->min, vr0->max,
8719 vr0->equiv);
8720 if (vr0->type == VR_VARYING)
8721 return;
8723 /* The resulting set of equivalences is always the intersection of
8724 the two sets. */
8725 if (vr0->equiv && vr1->equiv && vr0->equiv != vr1->equiv)
8726 bitmap_and_into (vr0->equiv, vr1->equiv);
8727 else if (vr0->equiv && !vr1->equiv)
8728 bitmap_clear (vr0->equiv);
8731 void
8732 vrp_meet (value_range *vr0, const value_range *vr1)
8734 if (dump_file && (dump_flags & TDF_DETAILS))
8736 fprintf (dump_file, "Meeting\n ");
8737 dump_value_range (dump_file, vr0);
8738 fprintf (dump_file, "\nand\n ");
8739 dump_value_range (dump_file, vr1);
8740 fprintf (dump_file, "\n");
8742 vrp_meet_1 (vr0, vr1);
8743 if (dump_file && (dump_flags & TDF_DETAILS))
8745 fprintf (dump_file, "to\n ");
8746 dump_value_range (dump_file, vr0);
8747 fprintf (dump_file, "\n");
8752 /* Visit all arguments for PHI node PHI that flow through executable
8753 edges. If a valid value range can be derived from all the incoming
8754 value ranges, set a new range in VR_RESULT. */
8756 static void
8757 extract_range_from_phi_node (gphi *phi, value_range *vr_result)
8759 size_t i;
8760 tree lhs = PHI_RESULT (phi);
8761 value_range *lhs_vr = get_value_range (lhs);
8762 bool first = true;
8763 int edges, old_edges;
8764 struct loop *l;
8766 if (dump_file && (dump_flags & TDF_DETAILS))
8768 fprintf (dump_file, "\nVisiting PHI node: ");
8769 print_gimple_stmt (dump_file, phi, 0, dump_flags);
8772 bool may_simulate_backedge_again = false;
8773 edges = 0;
8774 for (i = 0; i < gimple_phi_num_args (phi); i++)
8776 edge e = gimple_phi_arg_edge (phi, i);
8778 if (dump_file && (dump_flags & TDF_DETAILS))
8780 fprintf (dump_file,
8781 " Argument #%d (%d -> %d %sexecutable)\n",
8782 (int) i, e->src->index, e->dest->index,
8783 (e->flags & EDGE_EXECUTABLE) ? "" : "not ");
8786 if (e->flags & EDGE_EXECUTABLE)
8788 tree arg = PHI_ARG_DEF (phi, i);
8789 value_range vr_arg;
8791 ++edges;
8793 if (TREE_CODE (arg) == SSA_NAME)
8795 /* See if we are eventually going to change one of the args. */
8796 gimple *def_stmt = SSA_NAME_DEF_STMT (arg);
8797 if (! gimple_nop_p (def_stmt)
8798 && prop_simulate_again_p (def_stmt)
8799 && e->flags & EDGE_DFS_BACK)
8800 may_simulate_backedge_again = true;
8802 vr_arg = *(get_value_range (arg));
8803 /* Do not allow equivalences or symbolic ranges to leak in from
8804 backedges. That creates invalid equivalencies.
8805 See PR53465 and PR54767. */
8806 if (e->flags & EDGE_DFS_BACK)
8808 if (vr_arg.type == VR_RANGE
8809 || vr_arg.type == VR_ANTI_RANGE)
8811 vr_arg.equiv = NULL;
8812 if (symbolic_range_p (&vr_arg))
8814 vr_arg.type = VR_VARYING;
8815 vr_arg.min = NULL_TREE;
8816 vr_arg.max = NULL_TREE;
8820 else
8822 /* If the non-backedge arguments range is VR_VARYING then
8823 we can still try recording a simple equivalence. */
8824 if (vr_arg.type == VR_VARYING)
8826 vr_arg.type = VR_RANGE;
8827 vr_arg.min = arg;
8828 vr_arg.max = arg;
8829 vr_arg.equiv = NULL;
8833 else
8835 if (TREE_OVERFLOW_P (arg))
8836 arg = drop_tree_overflow (arg);
8838 vr_arg.type = VR_RANGE;
8839 vr_arg.min = arg;
8840 vr_arg.max = arg;
8841 vr_arg.equiv = NULL;
8844 if (dump_file && (dump_flags & TDF_DETAILS))
8846 fprintf (dump_file, "\t");
8847 print_generic_expr (dump_file, arg, dump_flags);
8848 fprintf (dump_file, ": ");
8849 dump_value_range (dump_file, &vr_arg);
8850 fprintf (dump_file, "\n");
8853 if (first)
8854 copy_value_range (vr_result, &vr_arg);
8855 else
8856 vrp_meet (vr_result, &vr_arg);
8857 first = false;
8859 if (vr_result->type == VR_VARYING)
8860 break;
8864 if (vr_result->type == VR_VARYING)
8865 goto varying;
8866 else if (vr_result->type == VR_UNDEFINED)
8867 goto update_range;
8869 old_edges = vr_phi_edge_counts[SSA_NAME_VERSION (lhs)];
8870 vr_phi_edge_counts[SSA_NAME_VERSION (lhs)] = edges;
8872 /* To prevent infinite iterations in the algorithm, derive ranges
8873 when the new value is slightly bigger or smaller than the
8874 previous one. We don't do this if we have seen a new executable
8875 edge; this helps us avoid an overflow infinity for conditionals
8876 which are not in a loop. If the old value-range was VR_UNDEFINED
8877 use the updated range and iterate one more time. If we will not
8878 simulate this PHI again via the backedge allow us to iterate. */
8879 if (edges > 0
8880 && gimple_phi_num_args (phi) > 1
8881 && edges == old_edges
8882 && lhs_vr->type != VR_UNDEFINED
8883 && may_simulate_backedge_again)
8885 /* Compare old and new ranges, fall back to varying if the
8886 values are not comparable. */
8887 int cmp_min = compare_values (lhs_vr->min, vr_result->min);
8888 if (cmp_min == -2)
8889 goto varying;
8890 int cmp_max = compare_values (lhs_vr->max, vr_result->max);
8891 if (cmp_max == -2)
8892 goto varying;
8894 /* For non VR_RANGE or for pointers fall back to varying if
8895 the range changed. */
8896 if ((lhs_vr->type != VR_RANGE || vr_result->type != VR_RANGE
8897 || POINTER_TYPE_P (TREE_TYPE (lhs)))
8898 && (cmp_min != 0 || cmp_max != 0))
8899 goto varying;
8901 /* If the new minimum is larger than the previous one
8902 retain the old value. If the new minimum value is smaller
8903 than the previous one and not -INF go all the way to -INF + 1.
8904 In the first case, to avoid infinite bouncing between different
8905 minimums, and in the other case to avoid iterating millions of
8906 times to reach -INF. Going to -INF + 1 also lets the following
8907 iteration compute whether there will be any overflow, at the
8908 expense of one additional iteration. */
8909 if (cmp_min < 0)
8910 vr_result->min = lhs_vr->min;
8911 else if (cmp_min > 0
8912 && !vrp_val_is_min (vr_result->min))
8913 vr_result->min
8914 = int_const_binop (PLUS_EXPR,
8915 vrp_val_min (TREE_TYPE (vr_result->min)),
8916 build_int_cst (TREE_TYPE (vr_result->min), 1));
8918 /* Similarly for the maximum value. */
8919 if (cmp_max > 0)
8920 vr_result->max = lhs_vr->max;
8921 else if (cmp_max < 0
8922 && !vrp_val_is_max (vr_result->max))
8923 vr_result->max
8924 = int_const_binop (MINUS_EXPR,
8925 vrp_val_max (TREE_TYPE (vr_result->min)),
8926 build_int_cst (TREE_TYPE (vr_result->min), 1));
8928 /* If we dropped either bound to +-INF then if this is a loop
8929 PHI node SCEV may known more about its value-range. */
8930 if (cmp_min > 0 || cmp_min < 0
8931 || cmp_max < 0 || cmp_max > 0)
8932 goto scev_check;
8934 goto infinite_check;
8937 goto update_range;
8939 varying:
8940 set_value_range_to_varying (vr_result);
8942 scev_check:
8943 /* If this is a loop PHI node SCEV may known more about its value-range.
8944 scev_check can be reached from two paths, one is a fall through from above
8945 "varying" label, the other is direct goto from code block which tries to
8946 avoid infinite simulation. */
8947 if ((l = loop_containing_stmt (phi))
8948 && l->header == gimple_bb (phi))
8949 adjust_range_with_scev (vr_result, l, phi, lhs);
8951 infinite_check:
8952 /* If we will end up with a (-INF, +INF) range, set it to
8953 VARYING. Same if the previous max value was invalid for
8954 the type and we end up with vr_result.min > vr_result.max. */
8955 if ((vr_result->type == VR_RANGE || vr_result->type == VR_ANTI_RANGE)
8956 && !((vrp_val_is_max (vr_result->max) && vrp_val_is_min (vr_result->min))
8957 || compare_values (vr_result->min, vr_result->max) > 0))
8959 else
8960 set_value_range_to_varying (vr_result);
8962 /* If the new range is different than the previous value, keep
8963 iterating. */
8964 update_range:
8965 return;
8968 /* Visit all arguments for PHI node PHI that flow through executable
8969 edges. If a valid value range can be derived from all the incoming
8970 value ranges, set a new range for the LHS of PHI. */
8972 static enum ssa_prop_result
8973 vrp_visit_phi_node (gphi *phi)
8975 tree lhs = PHI_RESULT (phi);
8976 value_range vr_result = VR_INITIALIZER;
8977 extract_range_from_phi_node (phi, &vr_result);
8978 if (update_value_range (lhs, &vr_result))
8980 if (dump_file && (dump_flags & TDF_DETAILS))
8982 fprintf (dump_file, "Found new range for ");
8983 print_generic_expr (dump_file, lhs, 0);
8984 fprintf (dump_file, ": ");
8985 dump_value_range (dump_file, &vr_result);
8986 fprintf (dump_file, "\n");
8989 if (vr_result.type == VR_VARYING)
8990 return SSA_PROP_VARYING;
8992 return SSA_PROP_INTERESTING;
8995 /* Nothing changed, don't add outgoing edges. */
8996 return SSA_PROP_NOT_INTERESTING;
8999 /* Simplify boolean operations if the source is known
9000 to be already a boolean. */
9001 static bool
9002 simplify_truth_ops_using_ranges (gimple_stmt_iterator *gsi, gimple *stmt)
9004 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
9005 tree lhs, op0, op1;
9006 bool need_conversion;
9008 /* We handle only !=/== case here. */
9009 gcc_assert (rhs_code == EQ_EXPR || rhs_code == NE_EXPR);
9011 op0 = gimple_assign_rhs1 (stmt);
9012 if (!op_with_boolean_value_range_p (op0))
9013 return false;
9015 op1 = gimple_assign_rhs2 (stmt);
9016 if (!op_with_boolean_value_range_p (op1))
9017 return false;
9019 /* Reduce number of cases to handle to NE_EXPR. As there is no
9020 BIT_XNOR_EXPR we cannot replace A == B with a single statement. */
9021 if (rhs_code == EQ_EXPR)
9023 if (TREE_CODE (op1) == INTEGER_CST)
9024 op1 = int_const_binop (BIT_XOR_EXPR, op1,
9025 build_int_cst (TREE_TYPE (op1), 1));
9026 else
9027 return false;
9030 lhs = gimple_assign_lhs (stmt);
9031 need_conversion
9032 = !useless_type_conversion_p (TREE_TYPE (lhs), TREE_TYPE (op0));
9034 /* Make sure to not sign-extend a 1-bit 1 when converting the result. */
9035 if (need_conversion
9036 && !TYPE_UNSIGNED (TREE_TYPE (op0))
9037 && TYPE_PRECISION (TREE_TYPE (op0)) == 1
9038 && TYPE_PRECISION (TREE_TYPE (lhs)) > 1)
9039 return false;
9041 /* For A != 0 we can substitute A itself. */
9042 if (integer_zerop (op1))
9043 gimple_assign_set_rhs_with_ops (gsi,
9044 need_conversion
9045 ? NOP_EXPR : TREE_CODE (op0), op0);
9046 /* For A != B we substitute A ^ B. Either with conversion. */
9047 else if (need_conversion)
9049 tree tem = make_ssa_name (TREE_TYPE (op0));
9050 gassign *newop
9051 = gimple_build_assign (tem, BIT_XOR_EXPR, op0, op1);
9052 gsi_insert_before (gsi, newop, GSI_SAME_STMT);
9053 if (INTEGRAL_TYPE_P (TREE_TYPE (tem))
9054 && TYPE_PRECISION (TREE_TYPE (tem)) > 1)
9055 set_range_info (tem, VR_RANGE,
9056 wi::zero (TYPE_PRECISION (TREE_TYPE (tem))),
9057 wi::one (TYPE_PRECISION (TREE_TYPE (tem))));
9058 gimple_assign_set_rhs_with_ops (gsi, NOP_EXPR, tem);
9060 /* Or without. */
9061 else
9062 gimple_assign_set_rhs_with_ops (gsi, BIT_XOR_EXPR, op0, op1);
9063 update_stmt (gsi_stmt (*gsi));
9064 fold_stmt (gsi, follow_single_use_edges);
9066 return true;
9069 /* Simplify a division or modulo operator to a right shift or
9070 bitwise and if the first operand is unsigned or is greater
9071 than zero and the second operand is an exact power of two.
9072 For TRUNC_MOD_EXPR op0 % op1 with constant op1, optimize it
9073 into just op0 if op0's range is known to be a subset of
9074 [-op1 + 1, op1 - 1] for signed and [0, op1 - 1] for unsigned
9075 modulo. */
9077 static bool
9078 simplify_div_or_mod_using_ranges (gimple_stmt_iterator *gsi, gimple *stmt)
9080 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
9081 tree val = NULL;
9082 tree op0 = gimple_assign_rhs1 (stmt);
9083 tree op1 = gimple_assign_rhs2 (stmt);
9084 value_range *vr = get_value_range (op0);
9086 if (rhs_code == TRUNC_MOD_EXPR
9087 && TREE_CODE (op1) == INTEGER_CST
9088 && tree_int_cst_sgn (op1) == 1
9089 && range_int_cst_p (vr)
9090 && tree_int_cst_lt (vr->max, op1))
9092 if (TYPE_UNSIGNED (TREE_TYPE (op0))
9093 || tree_int_cst_sgn (vr->min) >= 0
9094 || tree_int_cst_lt (fold_unary (NEGATE_EXPR, TREE_TYPE (op1), op1),
9095 vr->min))
9097 /* If op0 already has the range op0 % op1 has,
9098 then TRUNC_MOD_EXPR won't change anything. */
9099 gimple_assign_set_rhs_from_tree (gsi, op0);
9100 return true;
9104 if (!integer_pow2p (op1))
9106 /* X % -Y can be only optimized into X % Y either if
9107 X is not INT_MIN, or Y is not -1. Fold it now, as after
9108 remove_range_assertions the range info might be not available
9109 anymore. */
9110 if (rhs_code == TRUNC_MOD_EXPR
9111 && fold_stmt (gsi, follow_single_use_edges))
9112 return true;
9113 return false;
9116 if (TYPE_UNSIGNED (TREE_TYPE (op0)))
9117 val = integer_one_node;
9118 else
9120 bool sop = false;
9122 val = compare_range_with_value (GE_EXPR, vr, integer_zero_node, &sop);
9124 if (val
9125 && sop
9126 && integer_onep (val)
9127 && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC))
9129 location_t location;
9131 if (!gimple_has_location (stmt))
9132 location = input_location;
9133 else
9134 location = gimple_location (stmt);
9135 warning_at (location, OPT_Wstrict_overflow,
9136 "assuming signed overflow does not occur when "
9137 "simplifying %</%> or %<%%%> to %<>>%> or %<&%>");
9141 if (val && integer_onep (val))
9143 tree t;
9145 if (rhs_code == TRUNC_DIV_EXPR)
9147 t = build_int_cst (integer_type_node, tree_log2 (op1));
9148 gimple_assign_set_rhs_code (stmt, RSHIFT_EXPR);
9149 gimple_assign_set_rhs1 (stmt, op0);
9150 gimple_assign_set_rhs2 (stmt, t);
9152 else
9154 t = build_int_cst (TREE_TYPE (op1), 1);
9155 t = int_const_binop (MINUS_EXPR, op1, t);
9156 t = fold_convert (TREE_TYPE (op0), t);
9158 gimple_assign_set_rhs_code (stmt, BIT_AND_EXPR);
9159 gimple_assign_set_rhs1 (stmt, op0);
9160 gimple_assign_set_rhs2 (stmt, t);
9163 update_stmt (stmt);
9164 fold_stmt (gsi, follow_single_use_edges);
9165 return true;
9168 return false;
9171 /* Simplify a min or max if the ranges of the two operands are
9172 disjoint. Return true if we do simplify. */
9174 static bool
9175 simplify_min_or_max_using_ranges (gimple_stmt_iterator *gsi, gimple *stmt)
9177 tree op0 = gimple_assign_rhs1 (stmt);
9178 tree op1 = gimple_assign_rhs2 (stmt);
9179 bool sop = false;
9180 tree val;
9182 val = (vrp_evaluate_conditional_warnv_with_ops_using_ranges
9183 (LE_EXPR, op0, op1, &sop));
9184 if (!val)
9186 sop = false;
9187 val = (vrp_evaluate_conditional_warnv_with_ops_using_ranges
9188 (LT_EXPR, op0, op1, &sop));
9191 if (val)
9193 if (sop && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC))
9195 location_t location;
9197 if (!gimple_has_location (stmt))
9198 location = input_location;
9199 else
9200 location = gimple_location (stmt);
9201 warning_at (location, OPT_Wstrict_overflow,
9202 "assuming signed overflow does not occur when "
9203 "simplifying %<min/max (X,Y)%> to %<X%> or %<Y%>");
9206 /* VAL == TRUE -> OP0 < or <= op1
9207 VAL == FALSE -> OP0 > or >= op1. */
9208 tree res = ((gimple_assign_rhs_code (stmt) == MAX_EXPR)
9209 == integer_zerop (val)) ? op0 : op1;
9210 gimple_assign_set_rhs_from_tree (gsi, res);
9211 return true;
9214 return false;
9217 /* If the operand to an ABS_EXPR is >= 0, then eliminate the
9218 ABS_EXPR. If the operand is <= 0, then simplify the
9219 ABS_EXPR into a NEGATE_EXPR. */
9221 static bool
9222 simplify_abs_using_ranges (gimple_stmt_iterator *gsi, gimple *stmt)
9224 tree op = gimple_assign_rhs1 (stmt);
9225 value_range *vr = get_value_range (op);
9227 if (vr)
9229 tree val = NULL;
9230 bool sop = false;
9232 val = compare_range_with_value (LE_EXPR, vr, integer_zero_node, &sop);
9233 if (!val)
9235 /* The range is neither <= 0 nor > 0. Now see if it is
9236 either < 0 or >= 0. */
9237 sop = false;
9238 val = compare_range_with_value (LT_EXPR, vr, integer_zero_node,
9239 &sop);
9242 if (val)
9244 if (sop && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC))
9246 location_t location;
9248 if (!gimple_has_location (stmt))
9249 location = input_location;
9250 else
9251 location = gimple_location (stmt);
9252 warning_at (location, OPT_Wstrict_overflow,
9253 "assuming signed overflow does not occur when "
9254 "simplifying %<abs (X)%> to %<X%> or %<-X%>");
9257 gimple_assign_set_rhs1 (stmt, op);
9258 if (integer_zerop (val))
9259 gimple_assign_set_rhs_code (stmt, SSA_NAME);
9260 else
9261 gimple_assign_set_rhs_code (stmt, NEGATE_EXPR);
9262 update_stmt (stmt);
9263 fold_stmt (gsi, follow_single_use_edges);
9264 return true;
9268 return false;
9271 /* Optimize away redundant BIT_AND_EXPR and BIT_IOR_EXPR.
9272 If all the bits that are being cleared by & are already
9273 known to be zero from VR, or all the bits that are being
9274 set by | are already known to be one from VR, the bit
9275 operation is redundant. */
9277 static bool
9278 simplify_bit_ops_using_ranges (gimple_stmt_iterator *gsi, gimple *stmt)
9280 tree op0 = gimple_assign_rhs1 (stmt);
9281 tree op1 = gimple_assign_rhs2 (stmt);
9282 tree op = NULL_TREE;
9283 value_range vr0 = VR_INITIALIZER;
9284 value_range vr1 = VR_INITIALIZER;
9285 wide_int may_be_nonzero0, may_be_nonzero1;
9286 wide_int must_be_nonzero0, must_be_nonzero1;
9287 wide_int mask;
9289 if (TREE_CODE (op0) == SSA_NAME)
9290 vr0 = *(get_value_range (op0));
9291 else if (is_gimple_min_invariant (op0))
9292 set_value_range_to_value (&vr0, op0, NULL);
9293 else
9294 return false;
9296 if (TREE_CODE (op1) == SSA_NAME)
9297 vr1 = *(get_value_range (op1));
9298 else if (is_gimple_min_invariant (op1))
9299 set_value_range_to_value (&vr1, op1, NULL);
9300 else
9301 return false;
9303 if (!zero_nonzero_bits_from_vr (TREE_TYPE (op0), &vr0, &may_be_nonzero0,
9304 &must_be_nonzero0))
9305 return false;
9306 if (!zero_nonzero_bits_from_vr (TREE_TYPE (op1), &vr1, &may_be_nonzero1,
9307 &must_be_nonzero1))
9308 return false;
9310 switch (gimple_assign_rhs_code (stmt))
9312 case BIT_AND_EXPR:
9313 mask = may_be_nonzero0.and_not (must_be_nonzero1);
9314 if (mask == 0)
9316 op = op0;
9317 break;
9319 mask = may_be_nonzero1.and_not (must_be_nonzero0);
9320 if (mask == 0)
9322 op = op1;
9323 break;
9325 break;
9326 case BIT_IOR_EXPR:
9327 mask = may_be_nonzero0.and_not (must_be_nonzero1);
9328 if (mask == 0)
9330 op = op1;
9331 break;
9333 mask = may_be_nonzero1.and_not (must_be_nonzero0);
9334 if (mask == 0)
9336 op = op0;
9337 break;
9339 break;
9340 default:
9341 gcc_unreachable ();
9344 if (op == NULL_TREE)
9345 return false;
9347 gimple_assign_set_rhs_with_ops (gsi, TREE_CODE (op), op);
9348 update_stmt (gsi_stmt (*gsi));
9349 return true;
9352 /* We are comparing trees OP0 and OP1 using COND_CODE. OP0 has
9353 a known value range VR.
9355 If there is one and only one value which will satisfy the
9356 conditional, then return that value. Else return NULL.
9358 If signed overflow must be undefined for the value to satisfy
9359 the conditional, then set *STRICT_OVERFLOW_P to true. */
9361 static tree
9362 test_for_singularity (enum tree_code cond_code, tree op0,
9363 tree op1, value_range *vr,
9364 bool *strict_overflow_p)
9366 tree min = NULL;
9367 tree max = NULL;
9369 /* Extract minimum/maximum values which satisfy the conditional as it was
9370 written. */
9371 if (cond_code == LE_EXPR || cond_code == LT_EXPR)
9373 /* This should not be negative infinity; there is no overflow
9374 here. */
9375 min = TYPE_MIN_VALUE (TREE_TYPE (op0));
9377 max = op1;
9378 if (cond_code == LT_EXPR && !is_overflow_infinity (max))
9380 tree one = build_int_cst (TREE_TYPE (op0), 1);
9381 max = fold_build2 (MINUS_EXPR, TREE_TYPE (op0), max, one);
9382 if (EXPR_P (max))
9383 TREE_NO_WARNING (max) = 1;
9386 else if (cond_code == GE_EXPR || cond_code == GT_EXPR)
9388 /* This should not be positive infinity; there is no overflow
9389 here. */
9390 max = TYPE_MAX_VALUE (TREE_TYPE (op0));
9392 min = op1;
9393 if (cond_code == GT_EXPR && !is_overflow_infinity (min))
9395 tree one = build_int_cst (TREE_TYPE (op0), 1);
9396 min = fold_build2 (PLUS_EXPR, TREE_TYPE (op0), min, one);
9397 if (EXPR_P (min))
9398 TREE_NO_WARNING (min) = 1;
9402 /* Now refine the minimum and maximum values using any
9403 value range information we have for op0. */
9404 if (min && max)
9406 if (compare_values (vr->min, min) == 1)
9407 min = vr->min;
9408 if (compare_values (vr->max, max) == -1)
9409 max = vr->max;
9411 /* If the new min/max values have converged to a single value,
9412 then there is only one value which can satisfy the condition,
9413 return that value. */
9414 if (operand_equal_p (min, max, 0) && is_gimple_min_invariant (min))
9416 if ((cond_code == LE_EXPR || cond_code == LT_EXPR)
9417 && is_overflow_infinity (vr->max))
9418 *strict_overflow_p = true;
9419 if ((cond_code == GE_EXPR || cond_code == GT_EXPR)
9420 && is_overflow_infinity (vr->min))
9421 *strict_overflow_p = true;
9423 return min;
9426 return NULL;
9429 /* Return whether the value range *VR fits in an integer type specified
9430 by PRECISION and UNSIGNED_P. */
9432 static bool
9433 range_fits_type_p (value_range *vr, unsigned dest_precision, signop dest_sgn)
9435 tree src_type;
9436 unsigned src_precision;
9437 widest_int tem;
9438 signop src_sgn;
9440 /* We can only handle integral and pointer types. */
9441 src_type = TREE_TYPE (vr->min);
9442 if (!INTEGRAL_TYPE_P (src_type)
9443 && !POINTER_TYPE_P (src_type))
9444 return false;
9446 /* An extension is fine unless VR is SIGNED and dest_sgn is UNSIGNED,
9447 and so is an identity transform. */
9448 src_precision = TYPE_PRECISION (TREE_TYPE (vr->min));
9449 src_sgn = TYPE_SIGN (src_type);
9450 if ((src_precision < dest_precision
9451 && !(dest_sgn == UNSIGNED && src_sgn == SIGNED))
9452 || (src_precision == dest_precision && src_sgn == dest_sgn))
9453 return true;
9455 /* Now we can only handle ranges with constant bounds. */
9456 if (vr->type != VR_RANGE
9457 || TREE_CODE (vr->min) != INTEGER_CST
9458 || TREE_CODE (vr->max) != INTEGER_CST)
9459 return false;
9461 /* For sign changes, the MSB of the wide_int has to be clear.
9462 An unsigned value with its MSB set cannot be represented by
9463 a signed wide_int, while a negative value cannot be represented
9464 by an unsigned wide_int. */
9465 if (src_sgn != dest_sgn
9466 && (wi::lts_p (vr->min, 0) || wi::lts_p (vr->max, 0)))
9467 return false;
9469 /* Then we can perform the conversion on both ends and compare
9470 the result for equality. */
9471 tem = wi::ext (wi::to_widest (vr->min), dest_precision, dest_sgn);
9472 if (tem != wi::to_widest (vr->min))
9473 return false;
9474 tem = wi::ext (wi::to_widest (vr->max), dest_precision, dest_sgn);
9475 if (tem != wi::to_widest (vr->max))
9476 return false;
9478 return true;
9481 /* Simplify a conditional using a relational operator to an equality
9482 test if the range information indicates only one value can satisfy
9483 the original conditional. */
9485 static bool
9486 simplify_cond_using_ranges (gcond *stmt)
9488 tree op0 = gimple_cond_lhs (stmt);
9489 tree op1 = gimple_cond_rhs (stmt);
9490 enum tree_code cond_code = gimple_cond_code (stmt);
9492 if (cond_code != NE_EXPR
9493 && cond_code != EQ_EXPR
9494 && TREE_CODE (op0) == SSA_NAME
9495 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
9496 && is_gimple_min_invariant (op1))
9498 value_range *vr = get_value_range (op0);
9500 /* If we have range information for OP0, then we might be
9501 able to simplify this conditional. */
9502 if (vr->type == VR_RANGE)
9504 enum warn_strict_overflow_code wc = WARN_STRICT_OVERFLOW_COMPARISON;
9505 bool sop = false;
9506 tree new_tree = test_for_singularity (cond_code, op0, op1, vr, &sop);
9508 if (new_tree
9509 && (!sop || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0))))
9511 if (dump_file)
9513 fprintf (dump_file, "Simplified relational ");
9514 print_gimple_stmt (dump_file, stmt, 0, 0);
9515 fprintf (dump_file, " into ");
9518 gimple_cond_set_code (stmt, EQ_EXPR);
9519 gimple_cond_set_lhs (stmt, op0);
9520 gimple_cond_set_rhs (stmt, new_tree);
9522 update_stmt (stmt);
9524 if (dump_file)
9526 print_gimple_stmt (dump_file, stmt, 0, 0);
9527 fprintf (dump_file, "\n");
9530 if (sop && issue_strict_overflow_warning (wc))
9532 location_t location = input_location;
9533 if (gimple_has_location (stmt))
9534 location = gimple_location (stmt);
9536 warning_at (location, OPT_Wstrict_overflow,
9537 "assuming signed overflow does not occur when "
9538 "simplifying conditional");
9541 return true;
9544 /* Try again after inverting the condition. We only deal
9545 with integral types here, so no need to worry about
9546 issues with inverting FP comparisons. */
9547 sop = false;
9548 new_tree = test_for_singularity
9549 (invert_tree_comparison (cond_code, false),
9550 op0, op1, vr, &sop);
9552 if (new_tree
9553 && (!sop || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0))))
9555 if (dump_file)
9557 fprintf (dump_file, "Simplified relational ");
9558 print_gimple_stmt (dump_file, stmt, 0, 0);
9559 fprintf (dump_file, " into ");
9562 gimple_cond_set_code (stmt, NE_EXPR);
9563 gimple_cond_set_lhs (stmt, op0);
9564 gimple_cond_set_rhs (stmt, new_tree);
9566 update_stmt (stmt);
9568 if (dump_file)
9570 print_gimple_stmt (dump_file, stmt, 0, 0);
9571 fprintf (dump_file, "\n");
9574 if (sop && issue_strict_overflow_warning (wc))
9576 location_t location = input_location;
9577 if (gimple_has_location (stmt))
9578 location = gimple_location (stmt);
9580 warning_at (location, OPT_Wstrict_overflow,
9581 "assuming signed overflow does not occur when "
9582 "simplifying conditional");
9585 return true;
9590 /* If we have a comparison of an SSA_NAME (OP0) against a constant,
9591 see if OP0 was set by a type conversion where the source of
9592 the conversion is another SSA_NAME with a range that fits
9593 into the range of OP0's type.
9595 If so, the conversion is redundant as the earlier SSA_NAME can be
9596 used for the comparison directly if we just massage the constant in the
9597 comparison. */
9598 if (TREE_CODE (op0) == SSA_NAME
9599 && TREE_CODE (op1) == INTEGER_CST)
9601 gimple *def_stmt = SSA_NAME_DEF_STMT (op0);
9602 tree innerop;
9604 if (!is_gimple_assign (def_stmt)
9605 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt)))
9606 return false;
9608 innerop = gimple_assign_rhs1 (def_stmt);
9610 if (TREE_CODE (innerop) == SSA_NAME
9611 && !POINTER_TYPE_P (TREE_TYPE (innerop))
9612 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (innerop)
9613 && desired_pro_or_demotion_p (TREE_TYPE (innerop), TREE_TYPE (op0)))
9615 value_range *vr = get_value_range (innerop);
9617 if (range_int_cst_p (vr)
9618 && range_fits_type_p (vr,
9619 TYPE_PRECISION (TREE_TYPE (op0)),
9620 TYPE_SIGN (TREE_TYPE (op0)))
9621 && int_fits_type_p (op1, TREE_TYPE (innerop))
9622 /* The range must not have overflowed, or if it did overflow
9623 we must not be wrapping/trapping overflow and optimizing
9624 with strict overflow semantics. */
9625 && ((!is_negative_overflow_infinity (vr->min)
9626 && !is_positive_overflow_infinity (vr->max))
9627 || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (innerop))))
9629 /* If the range overflowed and the user has asked for warnings
9630 when strict overflow semantics were used to optimize code,
9631 issue an appropriate warning. */
9632 if (cond_code != EQ_EXPR && cond_code != NE_EXPR
9633 && (is_negative_overflow_infinity (vr->min)
9634 || is_positive_overflow_infinity (vr->max))
9635 && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_CONDITIONAL))
9637 location_t location;
9639 if (!gimple_has_location (stmt))
9640 location = input_location;
9641 else
9642 location = gimple_location (stmt);
9643 warning_at (location, OPT_Wstrict_overflow,
9644 "assuming signed overflow does not occur when "
9645 "simplifying conditional");
9648 tree newconst = fold_convert (TREE_TYPE (innerop), op1);
9649 gimple_cond_set_lhs (stmt, innerop);
9650 gimple_cond_set_rhs (stmt, newconst);
9651 return true;
9656 return false;
9659 /* Simplify a switch statement using the value range of the switch
9660 argument. */
9662 static bool
9663 simplify_switch_using_ranges (gswitch *stmt)
9665 tree op = gimple_switch_index (stmt);
9666 value_range *vr = NULL;
9667 bool take_default;
9668 edge e;
9669 edge_iterator ei;
9670 size_t i = 0, j = 0, n, n2;
9671 tree vec2;
9672 switch_update su;
9673 size_t k = 1, l = 0;
9675 if (TREE_CODE (op) == SSA_NAME)
9677 vr = get_value_range (op);
9679 /* We can only handle integer ranges. */
9680 if ((vr->type != VR_RANGE
9681 && vr->type != VR_ANTI_RANGE)
9682 || symbolic_range_p (vr))
9683 return false;
9685 /* Find case label for min/max of the value range. */
9686 take_default = !find_case_label_ranges (stmt, vr, &i, &j, &k, &l);
9688 else if (TREE_CODE (op) == INTEGER_CST)
9690 take_default = !find_case_label_index (stmt, 1, op, &i);
9691 if (take_default)
9693 i = 1;
9694 j = 0;
9696 else
9698 j = i;
9701 else
9702 return false;
9704 n = gimple_switch_num_labels (stmt);
9706 /* We can truncate the case label ranges that partially overlap with OP's
9707 value range. */
9708 size_t min_idx = 1, max_idx = 0;
9709 if (vr != NULL)
9710 find_case_label_range (stmt, vr->min, vr->max, &min_idx, &max_idx);
9711 if (min_idx <= max_idx)
9713 tree min_label = gimple_switch_label (stmt, min_idx);
9714 tree max_label = gimple_switch_label (stmt, max_idx);
9716 /* Avoid changing the type of the case labels when truncating. */
9717 tree case_label_type = TREE_TYPE (CASE_LOW (min_label));
9718 tree vr_min = fold_convert (case_label_type, vr->min);
9719 tree vr_max = fold_convert (case_label_type, vr->max);
9721 if (vr->type == VR_RANGE)
9723 /* If OP's value range is [2,8] and the low label range is
9724 0 ... 3, truncate the label's range to 2 .. 3. */
9725 if (tree_int_cst_compare (CASE_LOW (min_label), vr_min) < 0
9726 && CASE_HIGH (min_label) != NULL_TREE
9727 && tree_int_cst_compare (CASE_HIGH (min_label), vr_min) >= 0)
9728 CASE_LOW (min_label) = vr_min;
9730 /* If OP's value range is [2,8] and the high label range is
9731 7 ... 10, truncate the label's range to 7 .. 8. */
9732 if (tree_int_cst_compare (CASE_LOW (max_label), vr_max) <= 0
9733 && CASE_HIGH (max_label) != NULL_TREE
9734 && tree_int_cst_compare (CASE_HIGH (max_label), vr_max) > 0)
9735 CASE_HIGH (max_label) = vr_max;
9737 else if (vr->type == VR_ANTI_RANGE)
9739 tree one_cst = build_one_cst (case_label_type);
9741 if (min_label == max_label)
9743 /* If OP's value range is ~[7,8] and the label's range is
9744 7 ... 10, truncate the label's range to 9 ... 10. */
9745 if (tree_int_cst_compare (CASE_LOW (min_label), vr_min) == 0
9746 && CASE_HIGH (min_label) != NULL_TREE
9747 && tree_int_cst_compare (CASE_HIGH (min_label), vr_max) > 0)
9748 CASE_LOW (min_label)
9749 = int_const_binop (PLUS_EXPR, vr_max, one_cst);
9751 /* If OP's value range is ~[7,8] and the label's range is
9752 5 ... 8, truncate the label's range to 5 ... 6. */
9753 if (tree_int_cst_compare (CASE_LOW (min_label), vr_min) < 0
9754 && CASE_HIGH (min_label) != NULL_TREE
9755 && tree_int_cst_compare (CASE_HIGH (min_label), vr_max) == 0)
9756 CASE_HIGH (min_label)
9757 = int_const_binop (MINUS_EXPR, vr_min, one_cst);
9759 else
9761 /* If OP's value range is ~[2,8] and the low label range is
9762 0 ... 3, truncate the label's range to 0 ... 1. */
9763 if (tree_int_cst_compare (CASE_LOW (min_label), vr_min) < 0
9764 && CASE_HIGH (min_label) != NULL_TREE
9765 && tree_int_cst_compare (CASE_HIGH (min_label), vr_min) >= 0)
9766 CASE_HIGH (min_label)
9767 = int_const_binop (MINUS_EXPR, vr_min, one_cst);
9769 /* If OP's value range is ~[2,8] and the high label range is
9770 7 ... 10, truncate the label's range to 9 ... 10. */
9771 if (tree_int_cst_compare (CASE_LOW (max_label), vr_max) <= 0
9772 && CASE_HIGH (max_label) != NULL_TREE
9773 && tree_int_cst_compare (CASE_HIGH (max_label), vr_max) > 0)
9774 CASE_LOW (max_label)
9775 = int_const_binop (PLUS_EXPR, vr_max, one_cst);
9779 /* Canonicalize singleton case ranges. */
9780 if (tree_int_cst_equal (CASE_LOW (min_label), CASE_HIGH (min_label)))
9781 CASE_HIGH (min_label) = NULL_TREE;
9782 if (tree_int_cst_equal (CASE_LOW (max_label), CASE_HIGH (max_label)))
9783 CASE_HIGH (max_label) = NULL_TREE;
9786 /* We can also eliminate case labels that lie completely outside OP's value
9787 range. */
9789 /* Bail out if this is just all edges taken. */
9790 if (i == 1
9791 && j == n - 1
9792 && take_default)
9793 return false;
9795 /* Build a new vector of taken case labels. */
9796 vec2 = make_tree_vec (j - i + 1 + l - k + 1 + (int)take_default);
9797 n2 = 0;
9799 /* Add the default edge, if necessary. */
9800 if (take_default)
9801 TREE_VEC_ELT (vec2, n2++) = gimple_switch_default_label (stmt);
9803 for (; i <= j; ++i, ++n2)
9804 TREE_VEC_ELT (vec2, n2) = gimple_switch_label (stmt, i);
9806 for (; k <= l; ++k, ++n2)
9807 TREE_VEC_ELT (vec2, n2) = gimple_switch_label (stmt, k);
9809 /* Mark needed edges. */
9810 for (i = 0; i < n2; ++i)
9812 e = find_edge (gimple_bb (stmt),
9813 label_to_block (CASE_LABEL (TREE_VEC_ELT (vec2, i))));
9814 e->aux = (void *)-1;
9817 /* Queue not needed edges for later removal. */
9818 FOR_EACH_EDGE (e, ei, gimple_bb (stmt)->succs)
9820 if (e->aux == (void *)-1)
9822 e->aux = NULL;
9823 continue;
9826 if (dump_file && (dump_flags & TDF_DETAILS))
9828 fprintf (dump_file, "removing unreachable case label\n");
9830 to_remove_edges.safe_push (e);
9831 e->flags &= ~EDGE_EXECUTABLE;
9834 /* And queue an update for the stmt. */
9835 su.stmt = stmt;
9836 su.vec = vec2;
9837 to_update_switch_stmts.safe_push (su);
9838 return false;
9841 /* Simplify an integral conversion from an SSA name in STMT. */
9843 static bool
9844 simplify_conversion_using_ranges (gimple_stmt_iterator *gsi, gimple *stmt)
9846 tree innerop, middleop, finaltype;
9847 gimple *def_stmt;
9848 signop inner_sgn, middle_sgn, final_sgn;
9849 unsigned inner_prec, middle_prec, final_prec;
9850 widest_int innermin, innermed, innermax, middlemin, middlemed, middlemax;
9852 finaltype = TREE_TYPE (gimple_assign_lhs (stmt));
9853 if (!INTEGRAL_TYPE_P (finaltype))
9854 return false;
9855 middleop = gimple_assign_rhs1 (stmt);
9856 def_stmt = SSA_NAME_DEF_STMT (middleop);
9857 if (!is_gimple_assign (def_stmt)
9858 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt)))
9859 return false;
9860 innerop = gimple_assign_rhs1 (def_stmt);
9861 if (TREE_CODE (innerop) != SSA_NAME
9862 || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (innerop))
9863 return false;
9865 /* Get the value-range of the inner operand. Use get_range_info in
9866 case innerop was created during substitute-and-fold. */
9867 wide_int imin, imax;
9868 if (!INTEGRAL_TYPE_P (TREE_TYPE (innerop))
9869 || get_range_info (innerop, &imin, &imax) != VR_RANGE)
9870 return false;
9871 innermin = widest_int::from (imin, TYPE_SIGN (TREE_TYPE (innerop)));
9872 innermax = widest_int::from (imax, TYPE_SIGN (TREE_TYPE (innerop)));
9874 /* Simulate the conversion chain to check if the result is equal if
9875 the middle conversion is removed. */
9876 inner_prec = TYPE_PRECISION (TREE_TYPE (innerop));
9877 middle_prec = TYPE_PRECISION (TREE_TYPE (middleop));
9878 final_prec = TYPE_PRECISION (finaltype);
9880 /* If the first conversion is not injective, the second must not
9881 be widening. */
9882 if (wi::gtu_p (innermax - innermin,
9883 wi::mask <widest_int> (middle_prec, false))
9884 && middle_prec < final_prec)
9885 return false;
9886 /* We also want a medium value so that we can track the effect that
9887 narrowing conversions with sign change have. */
9888 inner_sgn = TYPE_SIGN (TREE_TYPE (innerop));
9889 if (inner_sgn == UNSIGNED)
9890 innermed = wi::shifted_mask <widest_int> (1, inner_prec - 1, false);
9891 else
9892 innermed = 0;
9893 if (wi::cmp (innermin, innermed, inner_sgn) >= 0
9894 || wi::cmp (innermed, innermax, inner_sgn) >= 0)
9895 innermed = innermin;
9897 middle_sgn = TYPE_SIGN (TREE_TYPE (middleop));
9898 middlemin = wi::ext (innermin, middle_prec, middle_sgn);
9899 middlemed = wi::ext (innermed, middle_prec, middle_sgn);
9900 middlemax = wi::ext (innermax, middle_prec, middle_sgn);
9902 /* Require that the final conversion applied to both the original
9903 and the intermediate range produces the same result. */
9904 final_sgn = TYPE_SIGN (finaltype);
9905 if (wi::ext (middlemin, final_prec, final_sgn)
9906 != wi::ext (innermin, final_prec, final_sgn)
9907 || wi::ext (middlemed, final_prec, final_sgn)
9908 != wi::ext (innermed, final_prec, final_sgn)
9909 || wi::ext (middlemax, final_prec, final_sgn)
9910 != wi::ext (innermax, final_prec, final_sgn))
9911 return false;
9913 gimple_assign_set_rhs1 (stmt, innerop);
9914 fold_stmt (gsi, follow_single_use_edges);
9915 return true;
9918 /* Simplify a conversion from integral SSA name to float in STMT. */
9920 static bool
9921 simplify_float_conversion_using_ranges (gimple_stmt_iterator *gsi,
9922 gimple *stmt)
9924 tree rhs1 = gimple_assign_rhs1 (stmt);
9925 value_range *vr = get_value_range (rhs1);
9926 machine_mode fltmode = TYPE_MODE (TREE_TYPE (gimple_assign_lhs (stmt)));
9927 machine_mode mode;
9928 tree tem;
9929 gassign *conv;
9931 /* We can only handle constant ranges. */
9932 if (vr->type != VR_RANGE
9933 || TREE_CODE (vr->min) != INTEGER_CST
9934 || TREE_CODE (vr->max) != INTEGER_CST)
9935 return false;
9937 /* First check if we can use a signed type in place of an unsigned. */
9938 if (TYPE_UNSIGNED (TREE_TYPE (rhs1))
9939 && (can_float_p (fltmode, TYPE_MODE (TREE_TYPE (rhs1)), 0)
9940 != CODE_FOR_nothing)
9941 && range_fits_type_p (vr, TYPE_PRECISION (TREE_TYPE (rhs1)), SIGNED))
9942 mode = TYPE_MODE (TREE_TYPE (rhs1));
9943 /* If we can do the conversion in the current input mode do nothing. */
9944 else if (can_float_p (fltmode, TYPE_MODE (TREE_TYPE (rhs1)),
9945 TYPE_UNSIGNED (TREE_TYPE (rhs1))) != CODE_FOR_nothing)
9946 return false;
9947 /* Otherwise search for a mode we can use, starting from the narrowest
9948 integer mode available. */
9949 else
9951 mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
9954 /* If we cannot do a signed conversion to float from mode
9955 or if the value-range does not fit in the signed type
9956 try with a wider mode. */
9957 if (can_float_p (fltmode, mode, 0) != CODE_FOR_nothing
9958 && range_fits_type_p (vr, GET_MODE_PRECISION (mode), SIGNED))
9959 break;
9961 mode = GET_MODE_WIDER_MODE (mode);
9962 /* But do not widen the input. Instead leave that to the
9963 optabs expansion code. */
9964 if (GET_MODE_PRECISION (mode) > TYPE_PRECISION (TREE_TYPE (rhs1)))
9965 return false;
9967 while (mode != VOIDmode);
9968 if (mode == VOIDmode)
9969 return false;
9972 /* It works, insert a truncation or sign-change before the
9973 float conversion. */
9974 tem = make_ssa_name (build_nonstandard_integer_type
9975 (GET_MODE_PRECISION (mode), 0));
9976 conv = gimple_build_assign (tem, NOP_EXPR, rhs1);
9977 gsi_insert_before (gsi, conv, GSI_SAME_STMT);
9978 gimple_assign_set_rhs1 (stmt, tem);
9979 fold_stmt (gsi, follow_single_use_edges);
9981 return true;
9984 /* Simplify an internal fn call using ranges if possible. */
9986 static bool
9987 simplify_internal_call_using_ranges (gimple_stmt_iterator *gsi, gimple *stmt)
9989 enum tree_code subcode;
9990 bool is_ubsan = false;
9991 bool ovf = false;
9992 switch (gimple_call_internal_fn (stmt))
9994 case IFN_UBSAN_CHECK_ADD:
9995 subcode = PLUS_EXPR;
9996 is_ubsan = true;
9997 break;
9998 case IFN_UBSAN_CHECK_SUB:
9999 subcode = MINUS_EXPR;
10000 is_ubsan = true;
10001 break;
10002 case IFN_UBSAN_CHECK_MUL:
10003 subcode = MULT_EXPR;
10004 is_ubsan = true;
10005 break;
10006 case IFN_ADD_OVERFLOW:
10007 subcode = PLUS_EXPR;
10008 break;
10009 case IFN_SUB_OVERFLOW:
10010 subcode = MINUS_EXPR;
10011 break;
10012 case IFN_MUL_OVERFLOW:
10013 subcode = MULT_EXPR;
10014 break;
10015 default:
10016 return false;
10019 tree op0 = gimple_call_arg (stmt, 0);
10020 tree op1 = gimple_call_arg (stmt, 1);
10021 tree type;
10022 if (is_ubsan)
10023 type = TREE_TYPE (op0);
10024 else if (gimple_call_lhs (stmt) == NULL_TREE)
10025 return false;
10026 else
10027 type = TREE_TYPE (TREE_TYPE (gimple_call_lhs (stmt)));
10028 if (!check_for_binary_op_overflow (subcode, type, op0, op1, &ovf)
10029 || (is_ubsan && ovf))
10030 return false;
10032 gimple *g;
10033 location_t loc = gimple_location (stmt);
10034 if (is_ubsan)
10035 g = gimple_build_assign (gimple_call_lhs (stmt), subcode, op0, op1);
10036 else
10038 int prec = TYPE_PRECISION (type);
10039 tree utype = type;
10040 if (ovf
10041 || !useless_type_conversion_p (type, TREE_TYPE (op0))
10042 || !useless_type_conversion_p (type, TREE_TYPE (op1)))
10043 utype = build_nonstandard_integer_type (prec, 1);
10044 if (TREE_CODE (op0) == INTEGER_CST)
10045 op0 = fold_convert (utype, op0);
10046 else if (!useless_type_conversion_p (utype, TREE_TYPE (op0)))
10048 g = gimple_build_assign (make_ssa_name (utype), NOP_EXPR, op0);
10049 gimple_set_location (g, loc);
10050 gsi_insert_before (gsi, g, GSI_SAME_STMT);
10051 op0 = gimple_assign_lhs (g);
10053 if (TREE_CODE (op1) == INTEGER_CST)
10054 op1 = fold_convert (utype, op1);
10055 else if (!useless_type_conversion_p (utype, TREE_TYPE (op1)))
10057 g = gimple_build_assign (make_ssa_name (utype), NOP_EXPR, op1);
10058 gimple_set_location (g, loc);
10059 gsi_insert_before (gsi, g, GSI_SAME_STMT);
10060 op1 = gimple_assign_lhs (g);
10062 g = gimple_build_assign (make_ssa_name (utype), subcode, op0, op1);
10063 gimple_set_location (g, loc);
10064 gsi_insert_before (gsi, g, GSI_SAME_STMT);
10065 if (utype != type)
10067 g = gimple_build_assign (make_ssa_name (type), NOP_EXPR,
10068 gimple_assign_lhs (g));
10069 gimple_set_location (g, loc);
10070 gsi_insert_before (gsi, g, GSI_SAME_STMT);
10072 g = gimple_build_assign (gimple_call_lhs (stmt), COMPLEX_EXPR,
10073 gimple_assign_lhs (g),
10074 build_int_cst (type, ovf));
10076 gimple_set_location (g, loc);
10077 gsi_replace (gsi, g, false);
10078 return true;
10081 /* Return true if VAR is a two-valued variable. Set a and b with the
10082 two-values when it is true. Return false otherwise. */
10084 static bool
10085 two_valued_val_range_p (tree var, tree *a, tree *b)
10087 value_range *vr = get_value_range (var);
10088 if ((vr->type != VR_RANGE
10089 && vr->type != VR_ANTI_RANGE)
10090 || TREE_CODE (vr->min) != INTEGER_CST
10091 || TREE_CODE (vr->max) != INTEGER_CST)
10092 return false;
10094 if (vr->type == VR_RANGE
10095 && wi::sub (vr->max, vr->min) == 1)
10097 *a = vr->min;
10098 *b = vr->max;
10099 return true;
10102 /* ~[TYPE_MIN + 1, TYPE_MAX - 1] */
10103 if (vr->type == VR_ANTI_RANGE
10104 && wi::sub (vr->min, vrp_val_min (TREE_TYPE (var))) == 1
10105 && wi::sub (vrp_val_max (TREE_TYPE (var)), vr->max) == 1)
10107 *a = vrp_val_min (TREE_TYPE (var));
10108 *b = vrp_val_max (TREE_TYPE (var));
10109 return true;
10112 return false;
10115 /* Simplify STMT using ranges if possible. */
10117 static bool
10118 simplify_stmt_using_ranges (gimple_stmt_iterator *gsi)
10120 gimple *stmt = gsi_stmt (*gsi);
10121 if (is_gimple_assign (stmt))
10123 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
10124 tree rhs1 = gimple_assign_rhs1 (stmt);
10125 tree rhs2 = gimple_assign_rhs2 (stmt);
10126 tree lhs = gimple_assign_lhs (stmt);
10127 tree val1 = NULL_TREE, val2 = NULL_TREE;
10128 use_operand_p use_p;
10129 gimple *use_stmt;
10131 /* Convert:
10132 LHS = CST BINOP VAR
10133 Where VAR is two-valued and LHS is used in GIMPLE_COND only
10135 LHS = VAR == VAL1 ? (CST BINOP VAL1) : (CST BINOP VAL2)
10137 Also handles:
10138 LHS = VAR BINOP CST
10139 Where VAR is two-valued and LHS is used in GIMPLE_COND only
10141 LHS = VAR == VAL1 ? (VAL1 BINOP CST) : (VAL2 BINOP CST) */
10143 if (TREE_CODE_CLASS (rhs_code) == tcc_binary
10144 && INTEGRAL_TYPE_P (TREE_TYPE (lhs))
10145 && ((TREE_CODE (rhs1) == INTEGER_CST
10146 && TREE_CODE (rhs2) == SSA_NAME)
10147 || (TREE_CODE (rhs2) == INTEGER_CST
10148 && TREE_CODE (rhs1) == SSA_NAME))
10149 && single_imm_use (lhs, &use_p, &use_stmt)
10150 && gimple_code (use_stmt) == GIMPLE_COND)
10153 tree new_rhs1 = NULL_TREE;
10154 tree new_rhs2 = NULL_TREE;
10155 tree cmp_var = NULL_TREE;
10157 if (TREE_CODE (rhs2) == SSA_NAME
10158 && two_valued_val_range_p (rhs2, &val1, &val2))
10160 /* Optimize RHS1 OP [VAL1, VAL2]. */
10161 new_rhs1 = int_const_binop (rhs_code, rhs1, val1);
10162 new_rhs2 = int_const_binop (rhs_code, rhs1, val2);
10163 cmp_var = rhs2;
10165 else if (TREE_CODE (rhs1) == SSA_NAME
10166 && two_valued_val_range_p (rhs1, &val1, &val2))
10168 /* Optimize [VAL1, VAL2] OP RHS2. */
10169 new_rhs1 = int_const_binop (rhs_code, val1, rhs2);
10170 new_rhs2 = int_const_binop (rhs_code, val2, rhs2);
10171 cmp_var = rhs1;
10174 /* If we could not find two-vals or the optimzation is invalid as
10175 in divide by zero, new_rhs1 / new_rhs will be NULL_TREE. */
10176 if (new_rhs1 && new_rhs2)
10178 tree cond = build2 (EQ_EXPR, TREE_TYPE (cmp_var), cmp_var, val1);
10179 gimple_assign_set_rhs_with_ops (gsi,
10180 COND_EXPR, cond,
10181 new_rhs1,
10182 new_rhs2);
10183 update_stmt (gsi_stmt (*gsi));
10184 fold_stmt (gsi, follow_single_use_edges);
10185 return true;
10189 switch (rhs_code)
10191 case EQ_EXPR:
10192 case NE_EXPR:
10193 /* Transform EQ_EXPR, NE_EXPR into BIT_XOR_EXPR or identity
10194 if the RHS is zero or one, and the LHS are known to be boolean
10195 values. */
10196 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
10197 return simplify_truth_ops_using_ranges (gsi, stmt);
10198 break;
10200 /* Transform TRUNC_DIV_EXPR and TRUNC_MOD_EXPR into RSHIFT_EXPR
10201 and BIT_AND_EXPR respectively if the first operand is greater
10202 than zero and the second operand is an exact power of two.
10203 Also optimize TRUNC_MOD_EXPR away if the second operand is
10204 constant and the first operand already has the right value
10205 range. */
10206 case TRUNC_DIV_EXPR:
10207 case TRUNC_MOD_EXPR:
10208 if (TREE_CODE (rhs1) == SSA_NAME
10209 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
10210 return simplify_div_or_mod_using_ranges (gsi, stmt);
10211 break;
10213 /* Transform ABS (X) into X or -X as appropriate. */
10214 case ABS_EXPR:
10215 if (TREE_CODE (rhs1) == SSA_NAME
10216 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
10217 return simplify_abs_using_ranges (gsi, stmt);
10218 break;
10220 case BIT_AND_EXPR:
10221 case BIT_IOR_EXPR:
10222 /* Optimize away BIT_AND_EXPR and BIT_IOR_EXPR
10223 if all the bits being cleared are already cleared or
10224 all the bits being set are already set. */
10225 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
10226 return simplify_bit_ops_using_ranges (gsi, stmt);
10227 break;
10229 CASE_CONVERT:
10230 if (TREE_CODE (rhs1) == SSA_NAME
10231 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
10232 return simplify_conversion_using_ranges (gsi, stmt);
10233 break;
10235 case FLOAT_EXPR:
10236 if (TREE_CODE (rhs1) == SSA_NAME
10237 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
10238 return simplify_float_conversion_using_ranges (gsi, stmt);
10239 break;
10241 case MIN_EXPR:
10242 case MAX_EXPR:
10243 return simplify_min_or_max_using_ranges (gsi, stmt);
10245 default:
10246 break;
10249 else if (gimple_code (stmt) == GIMPLE_COND)
10250 return simplify_cond_using_ranges (as_a <gcond *> (stmt));
10251 else if (gimple_code (stmt) == GIMPLE_SWITCH)
10252 return simplify_switch_using_ranges (as_a <gswitch *> (stmt));
10253 else if (is_gimple_call (stmt)
10254 && gimple_call_internal_p (stmt))
10255 return simplify_internal_call_using_ranges (gsi, stmt);
10257 return false;
10260 /* If the statement pointed by SI has a predicate whose value can be
10261 computed using the value range information computed by VRP, compute
10262 its value and return true. Otherwise, return false. */
10264 static bool
10265 fold_predicate_in (gimple_stmt_iterator *si)
10267 bool assignment_p = false;
10268 tree val;
10269 gimple *stmt = gsi_stmt (*si);
10271 if (is_gimple_assign (stmt)
10272 && TREE_CODE_CLASS (gimple_assign_rhs_code (stmt)) == tcc_comparison)
10274 assignment_p = true;
10275 val = vrp_evaluate_conditional (gimple_assign_rhs_code (stmt),
10276 gimple_assign_rhs1 (stmt),
10277 gimple_assign_rhs2 (stmt),
10278 stmt);
10280 else if (gcond *cond_stmt = dyn_cast <gcond *> (stmt))
10281 val = vrp_evaluate_conditional (gimple_cond_code (cond_stmt),
10282 gimple_cond_lhs (cond_stmt),
10283 gimple_cond_rhs (cond_stmt),
10284 stmt);
10285 else
10286 return false;
10288 if (val)
10290 if (assignment_p)
10291 val = fold_convert (gimple_expr_type (stmt), val);
10293 if (dump_file)
10295 fprintf (dump_file, "Folding predicate ");
10296 print_gimple_expr (dump_file, stmt, 0, 0);
10297 fprintf (dump_file, " to ");
10298 print_generic_expr (dump_file, val, 0);
10299 fprintf (dump_file, "\n");
10302 if (is_gimple_assign (stmt))
10303 gimple_assign_set_rhs_from_tree (si, val);
10304 else
10306 gcc_assert (gimple_code (stmt) == GIMPLE_COND);
10307 gcond *cond_stmt = as_a <gcond *> (stmt);
10308 if (integer_zerop (val))
10309 gimple_cond_make_false (cond_stmt);
10310 else if (integer_onep (val))
10311 gimple_cond_make_true (cond_stmt);
10312 else
10313 gcc_unreachable ();
10316 return true;
10319 return false;
10322 /* Callback for substitute_and_fold folding the stmt at *SI. */
10324 static bool
10325 vrp_fold_stmt (gimple_stmt_iterator *si)
10327 if (fold_predicate_in (si))
10328 return true;
10330 return simplify_stmt_using_ranges (si);
10333 /* Unwindable const/copy equivalences. */
10334 const_and_copies *equiv_stack;
10336 /* A trivial wrapper so that we can present the generic jump threading
10337 code with a simple API for simplifying statements. STMT is the
10338 statement we want to simplify, WITHIN_STMT provides the location
10339 for any overflow warnings. */
10341 static tree
10342 simplify_stmt_for_jump_threading (gimple *stmt, gimple *within_stmt,
10343 class avail_exprs_stack *avail_exprs_stack ATTRIBUTE_UNUSED)
10345 if (gcond *cond_stmt = dyn_cast <gcond *> (stmt))
10346 return vrp_evaluate_conditional (gimple_cond_code (cond_stmt),
10347 gimple_cond_lhs (cond_stmt),
10348 gimple_cond_rhs (cond_stmt),
10349 within_stmt);
10351 /* We simplify a switch statement by trying to determine which case label
10352 will be taken. If we are successful then we return the corresponding
10353 CASE_LABEL_EXPR. */
10354 if (gswitch *switch_stmt = dyn_cast <gswitch *> (stmt))
10356 tree op = gimple_switch_index (switch_stmt);
10357 if (TREE_CODE (op) != SSA_NAME)
10358 return NULL_TREE;
10360 value_range *vr = get_value_range (op);
10361 if ((vr->type != VR_RANGE && vr->type != VR_ANTI_RANGE)
10362 || symbolic_range_p (vr))
10363 return NULL_TREE;
10365 if (vr->type == VR_RANGE)
10367 size_t i, j;
10368 /* Get the range of labels that contain a part of the operand's
10369 value range. */
10370 find_case_label_range (switch_stmt, vr->min, vr->max, &i, &j);
10372 /* Is there only one such label? */
10373 if (i == j)
10375 tree label = gimple_switch_label (switch_stmt, i);
10377 /* The i'th label will be taken only if the value range of the
10378 operand is entirely within the bounds of this label. */
10379 if (CASE_HIGH (label) != NULL_TREE
10380 ? (tree_int_cst_compare (CASE_LOW (label), vr->min) <= 0
10381 && tree_int_cst_compare (CASE_HIGH (label), vr->max) >= 0)
10382 : (tree_int_cst_equal (CASE_LOW (label), vr->min)
10383 && tree_int_cst_equal (vr->min, vr->max)))
10384 return label;
10387 /* If there are no such labels then the default label will be
10388 taken. */
10389 if (i > j)
10390 return gimple_switch_label (switch_stmt, 0);
10393 if (vr->type == VR_ANTI_RANGE)
10395 unsigned n = gimple_switch_num_labels (switch_stmt);
10396 tree min_label = gimple_switch_label (switch_stmt, 1);
10397 tree max_label = gimple_switch_label (switch_stmt, n - 1);
10399 /* The default label will be taken only if the anti-range of the
10400 operand is entirely outside the bounds of all the (non-default)
10401 case labels. */
10402 if (tree_int_cst_compare (vr->min, CASE_LOW (min_label)) <= 0
10403 && (CASE_HIGH (max_label) != NULL_TREE
10404 ? tree_int_cst_compare (vr->max, CASE_HIGH (max_label)) >= 0
10405 : tree_int_cst_compare (vr->max, CASE_LOW (max_label)) >= 0))
10406 return gimple_switch_label (switch_stmt, 0);
10409 return NULL_TREE;
10412 if (gassign *assign_stmt = dyn_cast <gassign *> (stmt))
10414 value_range new_vr = VR_INITIALIZER;
10415 tree lhs = gimple_assign_lhs (assign_stmt);
10417 if (TREE_CODE (lhs) == SSA_NAME
10418 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs))
10419 || POINTER_TYPE_P (TREE_TYPE (lhs))))
10421 extract_range_from_assignment (&new_vr, assign_stmt);
10422 if (range_int_cst_singleton_p (&new_vr))
10423 return new_vr.min;
10427 return NULL_TREE;
10430 /* Blocks which have more than one predecessor and more than
10431 one successor present jump threading opportunities, i.e.,
10432 when the block is reached from a specific predecessor, we
10433 may be able to determine which of the outgoing edges will
10434 be traversed. When this optimization applies, we are able
10435 to avoid conditionals at runtime and we may expose secondary
10436 optimization opportunities.
10438 This routine is effectively a driver for the generic jump
10439 threading code. It basically just presents the generic code
10440 with edges that may be suitable for jump threading.
10442 Unlike DOM, we do not iterate VRP if jump threading was successful.
10443 While iterating may expose new opportunities for VRP, it is expected
10444 those opportunities would be very limited and the compile time cost
10445 to expose those opportunities would be significant.
10447 As jump threading opportunities are discovered, they are registered
10448 for later realization. */
10450 static void
10451 identify_jump_threads (void)
10453 basic_block bb;
10454 gcond *dummy;
10455 int i;
10456 edge e;
10458 /* Ugh. When substituting values earlier in this pass we can
10459 wipe the dominance information. So rebuild the dominator
10460 information as we need it within the jump threading code. */
10461 calculate_dominance_info (CDI_DOMINATORS);
10463 /* We do not allow VRP information to be used for jump threading
10464 across a back edge in the CFG. Otherwise it becomes too
10465 difficult to avoid eliminating loop exit tests. Of course
10466 EDGE_DFS_BACK is not accurate at this time so we have to
10467 recompute it. */
10468 mark_dfs_back_edges ();
10470 /* Do not thread across edges we are about to remove. Just marking
10471 them as EDGE_IGNORE will do. */
10472 FOR_EACH_VEC_ELT (to_remove_edges, i, e)
10473 e->flags |= EDGE_IGNORE;
10475 /* Allocate our unwinder stack to unwind any temporary equivalences
10476 that might be recorded. */
10477 equiv_stack = new const_and_copies ();
10479 /* To avoid lots of silly node creation, we create a single
10480 conditional and just modify it in-place when attempting to
10481 thread jumps. */
10482 dummy = gimple_build_cond (EQ_EXPR,
10483 integer_zero_node, integer_zero_node,
10484 NULL, NULL);
10486 /* Walk through all the blocks finding those which present a
10487 potential jump threading opportunity. We could set this up
10488 as a dominator walker and record data during the walk, but
10489 I doubt it's worth the effort for the classes of jump
10490 threading opportunities we are trying to identify at this
10491 point in compilation. */
10492 FOR_EACH_BB_FN (bb, cfun)
10494 gimple *last;
10496 /* If the generic jump threading code does not find this block
10497 interesting, then there is nothing to do. */
10498 if (! potentially_threadable_block (bb))
10499 continue;
10501 last = last_stmt (bb);
10503 /* We're basically looking for a switch or any kind of conditional with
10504 integral or pointer type arguments. Note the type of the second
10505 argument will be the same as the first argument, so no need to
10506 check it explicitly.
10508 We also handle the case where there are no statements in the
10509 block. This come up with forwarder blocks that are not
10510 optimized away because they lead to a loop header. But we do
10511 want to thread through them as we can sometimes thread to the
10512 loop exit which is obviously profitable. */
10513 if (!last
10514 || gimple_code (last) == GIMPLE_SWITCH
10515 || (gimple_code (last) == GIMPLE_COND
10516 && TREE_CODE (gimple_cond_lhs (last)) == SSA_NAME
10517 && (INTEGRAL_TYPE_P (TREE_TYPE (gimple_cond_lhs (last)))
10518 || POINTER_TYPE_P (TREE_TYPE (gimple_cond_lhs (last))))
10519 && (TREE_CODE (gimple_cond_rhs (last)) == SSA_NAME
10520 || is_gimple_min_invariant (gimple_cond_rhs (last)))))
10522 edge_iterator ei;
10524 /* We've got a block with multiple predecessors and multiple
10525 successors which also ends in a suitable conditional or
10526 switch statement. For each predecessor, see if we can thread
10527 it to a specific successor. */
10528 FOR_EACH_EDGE (e, ei, bb->preds)
10530 /* Do not thread across edges marked to ignoreor abnormal
10531 edges in the CFG. */
10532 if (e->flags & (EDGE_IGNORE | EDGE_COMPLEX))
10533 continue;
10535 thread_across_edge (dummy, e, true, equiv_stack, NULL,
10536 simplify_stmt_for_jump_threading);
10541 /* Clear EDGE_IGNORE. */
10542 FOR_EACH_VEC_ELT (to_remove_edges, i, e)
10543 e->flags &= ~EDGE_IGNORE;
10545 /* We do not actually update the CFG or SSA graphs at this point as
10546 ASSERT_EXPRs are still in the IL and cfg cleanup code does not yet
10547 handle ASSERT_EXPRs gracefully. */
10550 /* We identified all the jump threading opportunities earlier, but could
10551 not transform the CFG at that time. This routine transforms the
10552 CFG and arranges for the dominator tree to be rebuilt if necessary.
10554 Note the SSA graph update will occur during the normal TODO
10555 processing by the pass manager. */
10556 static void
10557 finalize_jump_threads (void)
10559 thread_through_all_blocks (false);
10560 delete equiv_stack;
10563 /* Free VRP lattice. */
10565 static void
10566 vrp_free_lattice ()
10568 /* Free allocated memory. */
10569 free (vr_value);
10570 free (vr_phi_edge_counts);
10571 bitmap_obstack_release (&vrp_equiv_obstack);
10572 vrp_value_range_pool.release ();
10574 /* So that we can distinguish between VRP data being available
10575 and not available. */
10576 vr_value = NULL;
10577 vr_phi_edge_counts = NULL;
10580 /* Traverse all the blocks folding conditionals with known ranges. */
10582 static void
10583 vrp_finalize (bool warn_array_bounds_p)
10585 size_t i;
10587 values_propagated = true;
10589 if (dump_file)
10591 fprintf (dump_file, "\nValue ranges after VRP:\n\n");
10592 dump_all_value_ranges (dump_file);
10593 fprintf (dump_file, "\n");
10596 /* Set value range to non pointer SSA_NAMEs. */
10597 for (i = 0; i < num_vr_values; i++)
10598 if (vr_value[i])
10600 tree name = ssa_name (i);
10602 if (!name
10603 || POINTER_TYPE_P (TREE_TYPE (name))
10604 || (vr_value[i]->type == VR_VARYING)
10605 || (vr_value[i]->type == VR_UNDEFINED))
10606 continue;
10608 if ((TREE_CODE (vr_value[i]->min) == INTEGER_CST)
10609 && (TREE_CODE (vr_value[i]->max) == INTEGER_CST)
10610 && (vr_value[i]->type == VR_RANGE
10611 || vr_value[i]->type == VR_ANTI_RANGE))
10612 set_range_info (name, vr_value[i]->type, vr_value[i]->min,
10613 vr_value[i]->max);
10616 substitute_and_fold (op_with_constant_singleton_value_range,
10617 vrp_fold_stmt, true);
10619 if (warn_array_bounds && warn_array_bounds_p)
10620 check_all_array_refs ();
10622 /* We must identify jump threading opportunities before we release
10623 the datastructures built by VRP. */
10624 identify_jump_threads ();
10627 /* evrp_dom_walker visits the basic blocks in the dominance order and set
10628 the Value Ranges (VR) for SSA_NAMEs in the scope. Use this VR to
10629 discover more VRs. */
10631 class evrp_dom_walker : public dom_walker
10633 public:
10634 evrp_dom_walker ()
10635 : dom_walker (CDI_DOMINATORS), stack (10)
10637 stmts_to_fixup.create (0);
10638 need_eh_cleanup = BITMAP_ALLOC (NULL);
10640 ~evrp_dom_walker ()
10642 stmts_to_fixup.release ();
10643 BITMAP_FREE (need_eh_cleanup);
10645 virtual edge before_dom_children (basic_block);
10646 virtual void after_dom_children (basic_block);
10647 void push_value_range (const_tree var, value_range *vr);
10648 value_range *pop_value_range (const_tree var);
10649 value_range *try_find_new_range (tree op, tree_code code, tree limit);
10651 /* Cond_stack holds the old VR. */
10652 auto_vec<std::pair <const_tree, value_range*> > stack;
10653 bitmap need_eh_cleanup;
10654 vec<gimple *> stmts_to_fixup;
10658 /* Find new range for OP such that (OP CODE LIMIT) is true. */
10660 value_range *
10661 evrp_dom_walker::try_find_new_range (tree op, tree_code code, tree limit)
10663 value_range vr = VR_INITIALIZER;
10664 value_range *old_vr = get_value_range (op);
10666 /* Discover VR when condition is true. */
10667 extract_range_for_var_from_comparison_expr (op, code, op,
10668 limit, &vr);
10669 if (old_vr->type == VR_RANGE || old_vr->type == VR_ANTI_RANGE)
10670 vrp_intersect_ranges (&vr, old_vr);
10671 /* If we found any usable VR, set the VR to ssa_name and create a
10672 PUSH old value in the stack with the old VR. */
10673 if (vr.type == VR_RANGE || vr.type == VR_ANTI_RANGE)
10675 value_range *new_vr = vrp_value_range_pool.allocate ();
10676 *new_vr = vr;
10677 return new_vr;
10679 return NULL;
10682 /* See if there is any new scope is entered with new VR and set that VR to
10683 ssa_name before visiting the statements in the scope. */
10685 edge
10686 evrp_dom_walker::before_dom_children (basic_block bb)
10688 tree op0 = NULL_TREE;
10690 push_value_range (NULL_TREE, NULL);
10691 if (single_pred_p (bb))
10693 edge e = single_pred_edge (bb);
10694 gimple *stmt = last_stmt (e->src);
10695 if (stmt
10696 && gimple_code (stmt) == GIMPLE_COND
10697 && (op0 = gimple_cond_lhs (stmt))
10698 && TREE_CODE (op0) == SSA_NAME
10699 && (INTEGRAL_TYPE_P (TREE_TYPE (gimple_cond_lhs (stmt)))
10700 || POINTER_TYPE_P (TREE_TYPE (gimple_cond_lhs (stmt)))))
10702 /* Entering a new scope. Try to see if we can find a VR
10703 here. */
10704 tree op1 = gimple_cond_rhs (stmt);
10705 tree_code code = gimple_cond_code (stmt);
10707 if (TREE_OVERFLOW_P (op1))
10708 op1 = drop_tree_overflow (op1);
10710 /* If condition is false, invert the cond. */
10711 if (e->flags & EDGE_FALSE_VALUE)
10712 code = invert_tree_comparison (gimple_cond_code (stmt),
10713 HONOR_NANS (op0));
10714 /* Add VR when (OP0 CODE OP1) condition is true. */
10715 value_range *op0_range = try_find_new_range (op0, code, op1);
10717 /* Register ranges for y in x < y where
10718 y might have ranges that are useful. */
10719 tree limit;
10720 tree_code new_code;
10721 if (TREE_CODE (op1) == SSA_NAME
10722 && extract_code_and_val_from_cond_with_ops (op1, code,
10723 op0, op1,
10724 false,
10725 &new_code, &limit))
10727 /* Add VR when (OP1 NEW_CODE LIMIT) condition is true. */
10728 value_range *op1_range = try_find_new_range (op1, new_code, limit);
10729 if (op1_range)
10730 push_value_range (op1, op1_range);
10733 if (op0_range)
10734 push_value_range (op0, op0_range);
10738 /* Visit PHI stmts and discover any new VRs possible. */
10739 gimple_stmt_iterator gsi;
10740 edge e;
10741 edge_iterator ei;
10742 bool has_unvisived_preds = false;
10744 FOR_EACH_EDGE (e, ei, bb->preds)
10745 if (!(e->src->flags & BB_VISITED))
10747 has_unvisived_preds = true;
10748 break;
10751 for (gphi_iterator gpi = gsi_start_phis (bb);
10752 !gsi_end_p (gpi); gsi_next (&gpi))
10754 gphi *phi = gpi.phi ();
10755 tree lhs = PHI_RESULT (phi);
10756 value_range vr_result = VR_INITIALIZER;
10757 if (!has_unvisived_preds
10758 && stmt_interesting_for_vrp (phi))
10759 extract_range_from_phi_node (phi, &vr_result);
10760 else
10761 set_value_range_to_varying (&vr_result);
10762 update_value_range (lhs, &vr_result);
10765 /* Visit all other stmts and discover any new VRs possible. */
10766 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
10768 gimple *stmt = gsi_stmt (gsi);
10769 edge taken_edge;
10770 tree output = NULL_TREE;
10771 gimple *old_stmt = stmt;
10772 bool was_noreturn = (is_gimple_call (stmt)
10773 && gimple_call_noreturn_p (stmt));
10775 /* TODO, if found taken_edge, we should visit (return it) and travel
10776 again to improve VR as done in DOM/SCCVN optimizations. It should
10777 be done carefully as stmts might prematurely leave a BB like
10778 in EH. */
10779 if (stmt_interesting_for_vrp (stmt))
10781 value_range vr = VR_INITIALIZER;
10782 extract_range_from_stmt (stmt, &taken_edge, &output, &vr);
10783 if (output
10784 && (vr.type == VR_RANGE || vr.type == VR_ANTI_RANGE))
10785 update_value_range (output, &vr);
10786 else
10787 set_defs_to_varying (stmt);
10789 /* Try folding stmts with the VR discovered. */
10790 bool did_replace
10791 = replace_uses_in (stmt,
10792 op_with_constant_singleton_value_range);
10793 if (fold_stmt (&gsi, follow_single_use_edges)
10794 || did_replace)
10795 update_stmt (gsi_stmt (gsi));
10797 if (did_replace)
10799 /* If we cleaned up EH information from the statement,
10800 remove EH edges. */
10801 if (maybe_clean_or_replace_eh_stmt (old_stmt, stmt))
10802 bitmap_set_bit (need_eh_cleanup, bb->index);
10804 /* If we turned a not noreturn call into a noreturn one
10805 schedule it for fixup. */
10806 if (!was_noreturn
10807 && is_gimple_call (stmt)
10808 && gimple_call_noreturn_p (stmt))
10809 stmts_to_fixup.safe_push (stmt);
10811 if (gimple_assign_single_p (stmt))
10813 tree rhs = gimple_assign_rhs1 (stmt);
10814 if (TREE_CODE (rhs) == ADDR_EXPR)
10815 recompute_tree_invariant_for_addr_expr (rhs);
10819 def_operand_p def_p = SINGLE_SSA_DEF_OPERAND (stmt, SSA_OP_DEF);
10820 /* Set the SSA with the value range. */
10821 if (def_p
10822 && TREE_CODE (DEF_FROM_PTR (def_p)) == SSA_NAME
10823 && INTEGRAL_TYPE_P (TREE_TYPE (DEF_FROM_PTR (def_p))))
10825 tree def = DEF_FROM_PTR (def_p);
10826 value_range *vr = get_value_range (def);
10828 if ((vr->type == VR_RANGE
10829 || vr->type == VR_ANTI_RANGE)
10830 && (TREE_CODE (vr->min) == INTEGER_CST)
10831 && (TREE_CODE (vr->max) == INTEGER_CST))
10832 set_range_info (def, vr->type, vr->min, vr->max);
10835 else
10836 set_defs_to_varying (stmt);
10838 bb->flags |= BB_VISITED;
10839 return NULL;
10842 /* Restore/pop VRs valid only for BB when we leave BB. */
10844 void
10845 evrp_dom_walker::after_dom_children (basic_block bb ATTRIBUTE_UNUSED)
10847 gcc_checking_assert (!stack.is_empty ());
10848 while (stack.last ().first != NULL_TREE)
10849 pop_value_range (stack.last ().first);
10850 pop_value_range (stack.last ().first);
10853 /* Push the Value Range of VAR to the stack and update it with new VR. */
10855 void
10856 evrp_dom_walker::push_value_range (const_tree var, value_range *vr)
10858 if (vr != NULL)
10860 unsigned ver = SSA_NAME_VERSION (var);
10861 gcc_checking_assert (vr_value);
10862 stack.safe_push (std::make_pair (var, vr_value[ver]));
10864 if (ver < num_vr_values)
10865 vr_value[ver] = vr;
10867 else
10868 stack.safe_push (std::make_pair (var, vr));
10871 /* Pop the Value Range from the vrp_stack and update VAR with it. */
10873 value_range *
10874 evrp_dom_walker::pop_value_range (const_tree var)
10876 value_range *vr = stack.last ().second;
10877 if (vr != NULL)
10879 unsigned ver = SSA_NAME_VERSION (var);
10880 gcc_checking_assert (var == stack.last ().first);
10881 gcc_checking_assert (vr_value);
10883 if (ver < num_vr_values)
10884 vr_value[ver] = vr;
10886 stack.pop ();
10887 return vr;
10891 /* Main entry point for the early vrp pass which is a simplified non-iterative
10892 version of vrp where basic blocks are visited in dominance order. Value
10893 ranges discovered in early vrp will also be used by ipa-vrp. */
10895 static unsigned int
10896 execute_early_vrp ()
10898 edge e;
10899 edge_iterator ei;
10900 basic_block bb;
10902 loop_optimizer_init (LOOPS_NORMAL | LOOPS_HAVE_RECORDED_EXITS);
10903 rewrite_into_loop_closed_ssa (NULL, TODO_update_ssa);
10904 scev_initialize ();
10905 calculate_dominance_info (CDI_DOMINATORS);
10906 FOR_EACH_BB_FN (bb, cfun)
10908 bb->flags &= ~BB_VISITED;
10909 FOR_EACH_EDGE (e, ei, bb->preds)
10910 e->flags |= EDGE_EXECUTABLE;
10912 vrp_initialize_lattice ();
10914 /* Walk stmts in dominance order and propagate VRP. */
10915 evrp_dom_walker walker;
10916 walker.walk (ENTRY_BLOCK_PTR_FOR_FN (cfun));
10918 if (!bitmap_empty_p (walker.need_eh_cleanup))
10919 gimple_purge_all_dead_eh_edges (walker.need_eh_cleanup);
10921 /* Fixup stmts that became noreturn calls. This may require splitting
10922 blocks and thus isn't possible during the dominator walk. Do this
10923 in reverse order so we don't inadvertedly remove a stmt we want to
10924 fixup by visiting a dominating now noreturn call first. */
10925 while (!walker.stmts_to_fixup.is_empty ())
10927 gimple *stmt = walker.stmts_to_fixup.pop ();
10928 fixup_noreturn_call (stmt);
10931 if (dump_file)
10933 fprintf (dump_file, "\nValue ranges after Early VRP:\n\n");
10934 dump_all_value_ranges (dump_file);
10935 fprintf (dump_file, "\n");
10937 vrp_free_lattice ();
10938 scev_finalize ();
10939 loop_optimizer_finalize ();
10940 FOR_EACH_BB_FN (bb, cfun)
10941 bb->flags &= ~BB_VISITED;
10942 return 0;
10946 /* Main entry point to VRP (Value Range Propagation). This pass is
10947 loosely based on J. R. C. Patterson, ``Accurate Static Branch
10948 Prediction by Value Range Propagation,'' in SIGPLAN Conference on
10949 Programming Language Design and Implementation, pp. 67-78, 1995.
10950 Also available at http://citeseer.ist.psu.edu/patterson95accurate.html
10952 This is essentially an SSA-CCP pass modified to deal with ranges
10953 instead of constants.
10955 While propagating ranges, we may find that two or more SSA name
10956 have equivalent, though distinct ranges. For instance,
10958 1 x_9 = p_3->a;
10959 2 p_4 = ASSERT_EXPR <p_3, p_3 != 0>
10960 3 if (p_4 == q_2)
10961 4 p_5 = ASSERT_EXPR <p_4, p_4 == q_2>;
10962 5 endif
10963 6 if (q_2)
10965 In the code above, pointer p_5 has range [q_2, q_2], but from the
10966 code we can also determine that p_5 cannot be NULL and, if q_2 had
10967 a non-varying range, p_5's range should also be compatible with it.
10969 These equivalences are created by two expressions: ASSERT_EXPR and
10970 copy operations. Since p_5 is an assertion on p_4, and p_4 was the
10971 result of another assertion, then we can use the fact that p_5 and
10972 p_4 are equivalent when evaluating p_5's range.
10974 Together with value ranges, we also propagate these equivalences
10975 between names so that we can take advantage of information from
10976 multiple ranges when doing final replacement. Note that this
10977 equivalency relation is transitive but not symmetric.
10979 In the example above, p_5 is equivalent to p_4, q_2 and p_3, but we
10980 cannot assert that q_2 is equivalent to p_5 because q_2 may be used
10981 in contexts where that assertion does not hold (e.g., in line 6).
10983 TODO, the main difference between this pass and Patterson's is that
10984 we do not propagate edge probabilities. We only compute whether
10985 edges can be taken or not. That is, instead of having a spectrum
10986 of jump probabilities between 0 and 1, we only deal with 0, 1 and
10987 DON'T KNOW. In the future, it may be worthwhile to propagate
10988 probabilities to aid branch prediction. */
10990 static unsigned int
10991 execute_vrp (bool warn_array_bounds_p)
10993 int i;
10994 edge e;
10995 switch_update *su;
10997 loop_optimizer_init (LOOPS_NORMAL | LOOPS_HAVE_RECORDED_EXITS);
10998 rewrite_into_loop_closed_ssa (NULL, TODO_update_ssa);
10999 scev_initialize ();
11001 /* ??? This ends up using stale EDGE_DFS_BACK for liveness computation.
11002 Inserting assertions may split edges which will invalidate
11003 EDGE_DFS_BACK. */
11004 insert_range_assertions ();
11006 to_remove_edges.create (10);
11007 to_update_switch_stmts.create (5);
11008 threadedge_initialize_values ();
11010 /* For visiting PHI nodes we need EDGE_DFS_BACK computed. */
11011 mark_dfs_back_edges ();
11013 vrp_initialize_lattice ();
11014 vrp_initialize ();
11015 ssa_propagate (vrp_visit_stmt, vrp_visit_phi_node);
11016 vrp_finalize (warn_array_bounds_p);
11017 vrp_free_lattice ();
11019 free_numbers_of_iterations_estimates (cfun);
11021 /* ASSERT_EXPRs must be removed before finalizing jump threads
11022 as finalizing jump threads calls the CFG cleanup code which
11023 does not properly handle ASSERT_EXPRs. */
11024 remove_range_assertions ();
11026 /* If we exposed any new variables, go ahead and put them into
11027 SSA form now, before we handle jump threading. This simplifies
11028 interactions between rewriting of _DECL nodes into SSA form
11029 and rewriting SSA_NAME nodes into SSA form after block
11030 duplication and CFG manipulation. */
11031 update_ssa (TODO_update_ssa);
11033 finalize_jump_threads ();
11035 /* Remove dead edges from SWITCH_EXPR optimization. This leaves the
11036 CFG in a broken state and requires a cfg_cleanup run. */
11037 FOR_EACH_VEC_ELT (to_remove_edges, i, e)
11038 remove_edge (e);
11039 /* Update SWITCH_EXPR case label vector. */
11040 FOR_EACH_VEC_ELT (to_update_switch_stmts, i, su)
11042 size_t j;
11043 size_t n = TREE_VEC_LENGTH (su->vec);
11044 tree label;
11045 gimple_switch_set_num_labels (su->stmt, n);
11046 for (j = 0; j < n; j++)
11047 gimple_switch_set_label (su->stmt, j, TREE_VEC_ELT (su->vec, j));
11048 /* As we may have replaced the default label with a regular one
11049 make sure to make it a real default label again. This ensures
11050 optimal expansion. */
11051 label = gimple_switch_label (su->stmt, 0);
11052 CASE_LOW (label) = NULL_TREE;
11053 CASE_HIGH (label) = NULL_TREE;
11056 if (to_remove_edges.length () > 0)
11058 free_dominance_info (CDI_DOMINATORS);
11059 loops_state_set (LOOPS_NEED_FIXUP);
11062 to_remove_edges.release ();
11063 to_update_switch_stmts.release ();
11064 threadedge_finalize_values ();
11066 scev_finalize ();
11067 loop_optimizer_finalize ();
11068 return 0;
11071 namespace {
11073 const pass_data pass_data_vrp =
11075 GIMPLE_PASS, /* type */
11076 "vrp", /* name */
11077 OPTGROUP_NONE, /* optinfo_flags */
11078 TV_TREE_VRP, /* tv_id */
11079 PROP_ssa, /* properties_required */
11080 0, /* properties_provided */
11081 0, /* properties_destroyed */
11082 0, /* todo_flags_start */
11083 ( TODO_cleanup_cfg | TODO_update_ssa ), /* todo_flags_finish */
11086 class pass_vrp : public gimple_opt_pass
11088 public:
11089 pass_vrp (gcc::context *ctxt)
11090 : gimple_opt_pass (pass_data_vrp, ctxt), warn_array_bounds_p (false)
11093 /* opt_pass methods: */
11094 opt_pass * clone () { return new pass_vrp (m_ctxt); }
11095 void set_pass_param (unsigned int n, bool param)
11097 gcc_assert (n == 0);
11098 warn_array_bounds_p = param;
11100 virtual bool gate (function *) { return flag_tree_vrp != 0; }
11101 virtual unsigned int execute (function *)
11102 { return execute_vrp (warn_array_bounds_p); }
11104 private:
11105 bool warn_array_bounds_p;
11106 }; // class pass_vrp
11108 } // anon namespace
11110 gimple_opt_pass *
11111 make_pass_vrp (gcc::context *ctxt)
11113 return new pass_vrp (ctxt);
11116 namespace {
11118 const pass_data pass_data_early_vrp =
11120 GIMPLE_PASS, /* type */
11121 "evrp", /* name */
11122 OPTGROUP_NONE, /* optinfo_flags */
11123 TV_TREE_EARLY_VRP, /* tv_id */
11124 PROP_ssa, /* properties_required */
11125 0, /* properties_provided */
11126 0, /* properties_destroyed */
11127 0, /* todo_flags_start */
11128 ( TODO_cleanup_cfg | TODO_update_ssa | TODO_verify_all ),
11131 class pass_early_vrp : public gimple_opt_pass
11133 public:
11134 pass_early_vrp (gcc::context *ctxt)
11135 : gimple_opt_pass (pass_data_early_vrp, ctxt)
11138 /* opt_pass methods: */
11139 opt_pass * clone () { return new pass_early_vrp (m_ctxt); }
11140 virtual bool gate (function *)
11142 return flag_tree_vrp != 0;
11144 virtual unsigned int execute (function *)
11145 { return execute_early_vrp (); }
11147 }; // class pass_vrp
11148 } // anon namespace
11150 gimple_opt_pass *
11151 make_pass_early_vrp (gcc::context *ctxt)
11153 return new pass_early_vrp (ctxt);