P0409R2 - allow lambda capture [=, this]
[official-gcc.git] / gcc / tree-vrp.c
blob221a07b6b3fc6ae3cc737d4fc0c811d614e9a705
1 /* Support routines for Value Range Propagation (VRP).
2 Copyright (C) 2005-2017 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-general.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"
65 #include "stringpool.h"
66 #include "attribs.h"
68 #define VR_INITIALIZER { VR_UNDEFINED, NULL_TREE, NULL_TREE, NULL }
70 /* Allocation pools for tree-vrp allocations. */
71 static object_allocator<value_range> vrp_value_range_pool ("Tree VRP value ranges");
72 static bitmap_obstack vrp_equiv_obstack;
74 /* Set of SSA names found live during the RPO traversal of the function
75 for still active basic-blocks. */
76 static sbitmap *live;
78 /* Return true if the SSA name NAME is live on the edge E. */
80 static bool
81 live_on_edge (edge e, tree name)
83 return (live[e->dest->index]
84 && bitmap_bit_p (live[e->dest->index], SSA_NAME_VERSION (name)));
87 /* Local functions. */
88 static int compare_values (tree val1, tree val2);
89 static int compare_values_warnv (tree val1, tree val2, bool *);
90 static tree vrp_evaluate_conditional_warnv_with_ops (enum tree_code,
91 tree, tree, bool, bool *,
92 bool *);
94 struct assert_info
96 /* Predicate code for the ASSERT_EXPR. Must be COMPARISON_CLASS_P. */
97 enum tree_code comp_code;
99 /* Name to register the assert for. */
100 tree name;
102 /* Value being compared against. */
103 tree val;
105 /* Expression to compare. */
106 tree expr;
109 /* Location information for ASSERT_EXPRs. Each instance of this
110 structure describes an ASSERT_EXPR for an SSA name. Since a single
111 SSA name may have more than one assertion associated with it, these
112 locations are kept in a linked list attached to the corresponding
113 SSA name. */
114 struct assert_locus
116 /* Basic block where the assertion would be inserted. */
117 basic_block bb;
119 /* Some assertions need to be inserted on an edge (e.g., assertions
120 generated by COND_EXPRs). In those cases, BB will be NULL. */
121 edge e;
123 /* Pointer to the statement that generated this assertion. */
124 gimple_stmt_iterator si;
126 /* Predicate code for the ASSERT_EXPR. Must be COMPARISON_CLASS_P. */
127 enum tree_code comp_code;
129 /* Value being compared against. */
130 tree val;
132 /* Expression to compare. */
133 tree expr;
135 /* Next node in the linked list. */
136 assert_locus *next;
139 /* If bit I is present, it means that SSA name N_i has a list of
140 assertions that should be inserted in the IL. */
141 static bitmap need_assert_for;
143 /* Array of locations lists where to insert assertions. ASSERTS_FOR[I]
144 holds a list of ASSERT_LOCUS_T nodes that describe where
145 ASSERT_EXPRs for SSA name N_I should be inserted. */
146 static assert_locus **asserts_for;
148 /* Value range array. After propagation, VR_VALUE[I] holds the range
149 of values that SSA name N_I may take. */
150 static unsigned num_vr_values;
151 static value_range **vr_value;
152 static bool values_propagated;
154 /* For a PHI node which sets SSA name N_I, VR_COUNTS[I] holds the
155 number of executable edges we saw the last time we visited the
156 node. */
157 static int *vr_phi_edge_counts;
159 struct switch_update {
160 gswitch *stmt;
161 tree vec;
164 static vec<edge> to_remove_edges;
165 static vec<switch_update> to_update_switch_stmts;
168 /* Return the maximum value for TYPE. */
170 static inline tree
171 vrp_val_max (const_tree type)
173 if (!INTEGRAL_TYPE_P (type))
174 return NULL_TREE;
176 return TYPE_MAX_VALUE (type);
179 /* Return the minimum value for TYPE. */
181 static inline tree
182 vrp_val_min (const_tree type)
184 if (!INTEGRAL_TYPE_P (type))
185 return NULL_TREE;
187 return TYPE_MIN_VALUE (type);
190 /* Return whether VAL is equal to the maximum value of its type.
191 We can't do a simple equality comparison with TYPE_MAX_VALUE because
192 C typedefs and Ada subtypes can produce types whose TYPE_MAX_VALUE
193 is not == to the integer constant with the same value in the type. */
195 static inline bool
196 vrp_val_is_max (const_tree val)
198 tree type_max = vrp_val_max (TREE_TYPE (val));
199 return (val == type_max
200 || (type_max != NULL_TREE
201 && operand_equal_p (val, type_max, 0)));
204 /* Return whether VAL is equal to the minimum value of its type. */
206 static inline bool
207 vrp_val_is_min (const_tree val)
209 tree type_min = vrp_val_min (TREE_TYPE (val));
210 return (val == type_min
211 || (type_min != NULL_TREE
212 && operand_equal_p (val, type_min, 0)));
216 /* Set value range VR to VR_UNDEFINED. */
218 static inline void
219 set_value_range_to_undefined (value_range *vr)
221 vr->type = VR_UNDEFINED;
222 vr->min = vr->max = NULL_TREE;
223 if (vr->equiv)
224 bitmap_clear (vr->equiv);
228 /* Set value range VR to VR_VARYING. */
230 static inline void
231 set_value_range_to_varying (value_range *vr)
233 vr->type = VR_VARYING;
234 vr->min = vr->max = NULL_TREE;
235 if (vr->equiv)
236 bitmap_clear (vr->equiv);
240 /* Set value range VR to {T, MIN, MAX, EQUIV}. */
242 static void
243 set_value_range (value_range *vr, enum value_range_type t, tree min,
244 tree max, bitmap equiv)
246 /* Check the validity of the range. */
247 if (flag_checking
248 && (t == VR_RANGE || t == VR_ANTI_RANGE))
250 int cmp;
252 gcc_assert (min && max);
254 gcc_assert (!TREE_OVERFLOW_P (min) && !TREE_OVERFLOW_P (max));
256 if (INTEGRAL_TYPE_P (TREE_TYPE (min)) && t == VR_ANTI_RANGE)
257 gcc_assert (!vrp_val_is_min (min) || !vrp_val_is_max (max));
259 cmp = compare_values (min, max);
260 gcc_assert (cmp == 0 || cmp == -1 || cmp == -2);
263 if (flag_checking
264 && (t == VR_UNDEFINED || t == VR_VARYING))
266 gcc_assert (min == NULL_TREE && max == NULL_TREE);
267 gcc_assert (equiv == NULL || bitmap_empty_p (equiv));
270 vr->type = t;
271 vr->min = min;
272 vr->max = max;
274 /* Since updating the equivalence set involves deep copying the
275 bitmaps, only do it if absolutely necessary. */
276 if (vr->equiv == NULL
277 && equiv != NULL)
278 vr->equiv = BITMAP_ALLOC (&vrp_equiv_obstack);
280 if (equiv != vr->equiv)
282 if (equiv && !bitmap_empty_p (equiv))
283 bitmap_copy (vr->equiv, equiv);
284 else
285 bitmap_clear (vr->equiv);
290 /* Set value range VR to the canonical form of {T, MIN, MAX, EQUIV}.
291 This means adjusting T, MIN and MAX representing the case of a
292 wrapping range with MAX < MIN covering [MIN, type_max] U [type_min, MAX]
293 as anti-rage ~[MAX+1, MIN-1]. Likewise for wrapping anti-ranges.
294 In corner cases where MAX+1 or MIN-1 wraps this will fall back
295 to varying.
296 This routine exists to ease canonicalization in the case where we
297 extract ranges from var + CST op limit. */
299 static void
300 set_and_canonicalize_value_range (value_range *vr, enum value_range_type t,
301 tree min, tree max, bitmap equiv)
303 /* Use the canonical setters for VR_UNDEFINED and VR_VARYING. */
304 if (t == VR_UNDEFINED)
306 set_value_range_to_undefined (vr);
307 return;
309 else if (t == VR_VARYING)
311 set_value_range_to_varying (vr);
312 return;
315 /* Nothing to canonicalize for symbolic ranges. */
316 if (TREE_CODE (min) != INTEGER_CST
317 || TREE_CODE (max) != INTEGER_CST)
319 set_value_range (vr, t, min, max, equiv);
320 return;
323 /* Wrong order for min and max, to swap them and the VR type we need
324 to adjust them. */
325 if (tree_int_cst_lt (max, min))
327 tree one, tmp;
329 /* For one bit precision if max < min, then the swapped
330 range covers all values, so for VR_RANGE it is varying and
331 for VR_ANTI_RANGE empty range, so drop to varying as well. */
332 if (TYPE_PRECISION (TREE_TYPE (min)) == 1)
334 set_value_range_to_varying (vr);
335 return;
338 one = build_int_cst (TREE_TYPE (min), 1);
339 tmp = int_const_binop (PLUS_EXPR, max, one);
340 max = int_const_binop (MINUS_EXPR, min, one);
341 min = tmp;
343 /* There's one corner case, if we had [C+1, C] before we now have
344 that again. But this represents an empty value range, so drop
345 to varying in this case. */
346 if (tree_int_cst_lt (max, min))
348 set_value_range_to_varying (vr);
349 return;
352 t = t == VR_RANGE ? VR_ANTI_RANGE : VR_RANGE;
355 /* Anti-ranges that can be represented as ranges should be so. */
356 if (t == VR_ANTI_RANGE)
358 bool is_min = vrp_val_is_min (min);
359 bool is_max = vrp_val_is_max (max);
361 if (is_min && is_max)
363 /* We cannot deal with empty ranges, drop to varying.
364 ??? This could be VR_UNDEFINED instead. */
365 set_value_range_to_varying (vr);
366 return;
368 else if (TYPE_PRECISION (TREE_TYPE (min)) == 1
369 && (is_min || is_max))
371 /* Non-empty boolean ranges can always be represented
372 as a singleton range. */
373 if (is_min)
374 min = max = vrp_val_max (TREE_TYPE (min));
375 else
376 min = max = vrp_val_min (TREE_TYPE (min));
377 t = VR_RANGE;
379 else if (is_min
380 /* As a special exception preserve non-null ranges. */
381 && !(TYPE_UNSIGNED (TREE_TYPE (min))
382 && integer_zerop (max)))
384 tree one = build_int_cst (TREE_TYPE (max), 1);
385 min = int_const_binop (PLUS_EXPR, max, one);
386 max = vrp_val_max (TREE_TYPE (max));
387 t = VR_RANGE;
389 else if (is_max)
391 tree one = build_int_cst (TREE_TYPE (min), 1);
392 max = int_const_binop (MINUS_EXPR, min, one);
393 min = vrp_val_min (TREE_TYPE (min));
394 t = VR_RANGE;
398 /* Do not drop [-INF(OVF), +INF(OVF)] to varying. (OVF) has to be sticky
399 to make sure VRP iteration terminates, otherwise we can get into
400 oscillations. */
402 set_value_range (vr, t, min, max, equiv);
405 /* Copy value range FROM into value range TO. */
407 static inline void
408 copy_value_range (value_range *to, value_range *from)
410 set_value_range (to, from->type, from->min, from->max, from->equiv);
413 /* Set value range VR to a single value. This function is only called
414 with values we get from statements, and exists to clear the
415 TREE_OVERFLOW flag. */
417 static inline void
418 set_value_range_to_value (value_range *vr, tree val, bitmap equiv)
420 gcc_assert (is_gimple_min_invariant (val));
421 if (TREE_OVERFLOW_P (val))
422 val = drop_tree_overflow (val);
423 set_value_range (vr, VR_RANGE, val, val, equiv);
426 /* Set value range VR to a non-negative range of type TYPE. */
428 static inline void
429 set_value_range_to_nonnegative (value_range *vr, tree type)
431 tree zero = build_int_cst (type, 0);
432 set_value_range (vr, VR_RANGE, zero, vrp_val_max (type), vr->equiv);
435 /* Set value range VR to a non-NULL range of type TYPE. */
437 static inline void
438 set_value_range_to_nonnull (value_range *vr, tree type)
440 tree zero = build_int_cst (type, 0);
441 set_value_range (vr, VR_ANTI_RANGE, zero, zero, vr->equiv);
445 /* Set value range VR to a NULL range of type TYPE. */
447 static inline void
448 set_value_range_to_null (value_range *vr, tree type)
450 set_value_range_to_value (vr, build_int_cst (type, 0), vr->equiv);
454 /* Set value range VR to a range of a truthvalue of type TYPE. */
456 static inline void
457 set_value_range_to_truthvalue (value_range *vr, tree type)
459 if (TYPE_PRECISION (type) == 1)
460 set_value_range_to_varying (vr);
461 else
462 set_value_range (vr, VR_RANGE,
463 build_int_cst (type, 0), build_int_cst (type, 1),
464 vr->equiv);
468 /* If abs (min) < abs (max), set VR to [-max, max], if
469 abs (min) >= abs (max), set VR to [-min, min]. */
471 static void
472 abs_extent_range (value_range *vr, tree min, tree max)
474 int cmp;
476 gcc_assert (TREE_CODE (min) == INTEGER_CST);
477 gcc_assert (TREE_CODE (max) == INTEGER_CST);
478 gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (min)));
479 gcc_assert (!TYPE_UNSIGNED (TREE_TYPE (min)));
480 min = fold_unary (ABS_EXPR, TREE_TYPE (min), min);
481 max = fold_unary (ABS_EXPR, TREE_TYPE (max), max);
482 if (TREE_OVERFLOW (min) || TREE_OVERFLOW (max))
484 set_value_range_to_varying (vr);
485 return;
487 cmp = compare_values (min, max);
488 if (cmp == -1)
489 min = fold_unary (NEGATE_EXPR, TREE_TYPE (min), max);
490 else if (cmp == 0 || cmp == 1)
492 max = min;
493 min = fold_unary (NEGATE_EXPR, TREE_TYPE (min), min);
495 else
497 set_value_range_to_varying (vr);
498 return;
500 set_and_canonicalize_value_range (vr, VR_RANGE, min, max, NULL);
504 /* Return value range information for VAR.
506 If we have no values ranges recorded (ie, VRP is not running), then
507 return NULL. Otherwise create an empty range if none existed for VAR. */
509 static value_range *
510 get_value_range (const_tree var)
512 static const value_range vr_const_varying
513 = { VR_VARYING, NULL_TREE, NULL_TREE, NULL };
514 value_range *vr;
515 tree sym;
516 unsigned ver = SSA_NAME_VERSION (var);
518 /* If we have no recorded ranges, then return NULL. */
519 if (! vr_value)
520 return NULL;
522 /* If we query the range for a new SSA name return an unmodifiable VARYING.
523 We should get here at most from the substitute-and-fold stage which
524 will never try to change values. */
525 if (ver >= num_vr_values)
526 return CONST_CAST (value_range *, &vr_const_varying);
528 vr = vr_value[ver];
529 if (vr)
530 return vr;
532 /* After propagation finished do not allocate new value-ranges. */
533 if (values_propagated)
534 return CONST_CAST (value_range *, &vr_const_varying);
536 /* Create a default value range. */
537 vr_value[ver] = vr = vrp_value_range_pool.allocate ();
538 memset (vr, 0, sizeof (*vr));
540 /* Defer allocating the equivalence set. */
541 vr->equiv = NULL;
543 /* If VAR is a default definition of a parameter, the variable can
544 take any value in VAR's type. */
545 if (SSA_NAME_IS_DEFAULT_DEF (var))
547 sym = SSA_NAME_VAR (var);
548 if (TREE_CODE (sym) == PARM_DECL)
550 /* Try to use the "nonnull" attribute to create ~[0, 0]
551 anti-ranges for pointers. Note that this is only valid with
552 default definitions of PARM_DECLs. */
553 if (POINTER_TYPE_P (TREE_TYPE (sym))
554 && (nonnull_arg_p (sym)
555 || get_ptr_nonnull (var)))
556 set_value_range_to_nonnull (vr, TREE_TYPE (sym));
557 else if (INTEGRAL_TYPE_P (TREE_TYPE (sym)))
559 wide_int min, max;
560 value_range_type rtype = get_range_info (var, &min, &max);
561 if (rtype == VR_RANGE || rtype == VR_ANTI_RANGE)
562 set_value_range (vr, rtype,
563 wide_int_to_tree (TREE_TYPE (var), min),
564 wide_int_to_tree (TREE_TYPE (var), max),
565 NULL);
566 else
567 set_value_range_to_varying (vr);
569 else
570 set_value_range_to_varying (vr);
572 else if (TREE_CODE (sym) == RESULT_DECL
573 && DECL_BY_REFERENCE (sym))
574 set_value_range_to_nonnull (vr, TREE_TYPE (sym));
577 return vr;
580 /* Set value-ranges of all SSA names defined by STMT to varying. */
582 static void
583 set_defs_to_varying (gimple *stmt)
585 ssa_op_iter i;
586 tree def;
587 FOR_EACH_SSA_TREE_OPERAND (def, stmt, i, SSA_OP_DEF)
589 value_range *vr = get_value_range (def);
590 /* Avoid writing to vr_const_varying get_value_range may return. */
591 if (vr->type != VR_VARYING)
592 set_value_range_to_varying (vr);
597 /* Return true, if VAL1 and VAL2 are equal values for VRP purposes. */
599 static inline bool
600 vrp_operand_equal_p (const_tree val1, const_tree val2)
602 if (val1 == val2)
603 return true;
604 if (!val1 || !val2 || !operand_equal_p (val1, val2, 0))
605 return false;
606 return true;
609 /* Return true, if the bitmaps B1 and B2 are equal. */
611 static inline bool
612 vrp_bitmap_equal_p (const_bitmap b1, const_bitmap b2)
614 return (b1 == b2
615 || ((!b1 || bitmap_empty_p (b1))
616 && (!b2 || bitmap_empty_p (b2)))
617 || (b1 && b2
618 && bitmap_equal_p (b1, b2)));
621 /* Update the value range and equivalence set for variable VAR to
622 NEW_VR. Return true if NEW_VR is different from VAR's previous
623 value.
625 NOTE: This function assumes that NEW_VR is a temporary value range
626 object created for the sole purpose of updating VAR's range. The
627 storage used by the equivalence set from NEW_VR will be freed by
628 this function. Do not call update_value_range when NEW_VR
629 is the range object associated with another SSA name. */
631 static inline bool
632 update_value_range (const_tree var, value_range *new_vr)
634 value_range *old_vr;
635 bool is_new;
637 /* If there is a value-range on the SSA name from earlier analysis
638 factor that in. */
639 if (INTEGRAL_TYPE_P (TREE_TYPE (var)))
641 wide_int min, max;
642 value_range_type rtype = get_range_info (var, &min, &max);
643 if (rtype == VR_RANGE || rtype == VR_ANTI_RANGE)
645 tree nr_min, nr_max;
646 nr_min = wide_int_to_tree (TREE_TYPE (var), min);
647 nr_max = wide_int_to_tree (TREE_TYPE (var), max);
648 value_range nr = VR_INITIALIZER;
649 set_and_canonicalize_value_range (&nr, rtype, nr_min, nr_max, NULL);
650 vrp_intersect_ranges (new_vr, &nr);
654 /* Update the value range, if necessary. */
655 old_vr = get_value_range (var);
656 is_new = old_vr->type != new_vr->type
657 || !vrp_operand_equal_p (old_vr->min, new_vr->min)
658 || !vrp_operand_equal_p (old_vr->max, new_vr->max)
659 || !vrp_bitmap_equal_p (old_vr->equiv, new_vr->equiv);
661 if (is_new)
663 /* Do not allow transitions up the lattice. The following
664 is slightly more awkward than just new_vr->type < old_vr->type
665 because VR_RANGE and VR_ANTI_RANGE need to be considered
666 the same. We may not have is_new when transitioning to
667 UNDEFINED. If old_vr->type is VARYING, we shouldn't be
668 called. */
669 if (new_vr->type == VR_UNDEFINED)
671 BITMAP_FREE (new_vr->equiv);
672 set_value_range_to_varying (old_vr);
673 set_value_range_to_varying (new_vr);
674 return true;
676 else
677 set_value_range (old_vr, new_vr->type, new_vr->min, new_vr->max,
678 new_vr->equiv);
681 BITMAP_FREE (new_vr->equiv);
683 return is_new;
687 /* Add VAR and VAR's equivalence set to EQUIV. This is the central
688 point where equivalence processing can be turned on/off. */
690 static void
691 add_equivalence (bitmap *equiv, const_tree var)
693 unsigned ver = SSA_NAME_VERSION (var);
694 value_range *vr = get_value_range (var);
696 if (*equiv == NULL)
697 *equiv = BITMAP_ALLOC (&vrp_equiv_obstack);
698 bitmap_set_bit (*equiv, ver);
699 if (vr && vr->equiv)
700 bitmap_ior_into (*equiv, vr->equiv);
704 /* Return true if VR is ~[0, 0]. */
706 static inline bool
707 range_is_nonnull (value_range *vr)
709 return vr->type == VR_ANTI_RANGE
710 && integer_zerop (vr->min)
711 && integer_zerop (vr->max);
715 /* Return true if VR is [0, 0]. */
717 static inline bool
718 range_is_null (value_range *vr)
720 return vr->type == VR_RANGE
721 && integer_zerop (vr->min)
722 && integer_zerop (vr->max);
725 /* Return true if max and min of VR are INTEGER_CST. It's not necessary
726 a singleton. */
728 static inline bool
729 range_int_cst_p (value_range *vr)
731 return (vr->type == VR_RANGE
732 && TREE_CODE (vr->max) == INTEGER_CST
733 && TREE_CODE (vr->min) == INTEGER_CST);
736 /* Return true if VR is a INTEGER_CST singleton. */
738 static inline bool
739 range_int_cst_singleton_p (value_range *vr)
741 return (range_int_cst_p (vr)
742 && tree_int_cst_equal (vr->min, vr->max));
745 /* Return true if value range VR involves at least one symbol. */
747 static inline bool
748 symbolic_range_p (value_range *vr)
750 return (!is_gimple_min_invariant (vr->min)
751 || !is_gimple_min_invariant (vr->max));
754 /* Return the single symbol (an SSA_NAME) contained in T if any, or NULL_TREE
755 otherwise. We only handle additive operations and set NEG to true if the
756 symbol is negated and INV to the invariant part, if any. */
758 static tree
759 get_single_symbol (tree t, bool *neg, tree *inv)
761 bool neg_;
762 tree inv_;
764 *inv = NULL_TREE;
765 *neg = false;
767 if (TREE_CODE (t) == PLUS_EXPR
768 || TREE_CODE (t) == POINTER_PLUS_EXPR
769 || TREE_CODE (t) == MINUS_EXPR)
771 if (is_gimple_min_invariant (TREE_OPERAND (t, 0)))
773 neg_ = (TREE_CODE (t) == MINUS_EXPR);
774 inv_ = TREE_OPERAND (t, 0);
775 t = TREE_OPERAND (t, 1);
777 else if (is_gimple_min_invariant (TREE_OPERAND (t, 1)))
779 neg_ = false;
780 inv_ = TREE_OPERAND (t, 1);
781 t = TREE_OPERAND (t, 0);
783 else
784 return NULL_TREE;
786 else
788 neg_ = false;
789 inv_ = NULL_TREE;
792 if (TREE_CODE (t) == NEGATE_EXPR)
794 t = TREE_OPERAND (t, 0);
795 neg_ = !neg_;
798 if (TREE_CODE (t) != SSA_NAME)
799 return NULL_TREE;
801 if (inv_ && TREE_OVERFLOW_P (inv_))
802 inv_ = drop_tree_overflow (inv_);
804 *neg = neg_;
805 *inv = inv_;
806 return t;
809 /* The reverse operation: build a symbolic expression with TYPE
810 from symbol SYM, negated according to NEG, and invariant INV. */
812 static tree
813 build_symbolic_expr (tree type, tree sym, bool neg, tree inv)
815 const bool pointer_p = POINTER_TYPE_P (type);
816 tree t = sym;
818 if (neg)
819 t = build1 (NEGATE_EXPR, type, t);
821 if (integer_zerop (inv))
822 return t;
824 return build2 (pointer_p ? POINTER_PLUS_EXPR : PLUS_EXPR, type, t, inv);
827 /* Return true if value range VR involves exactly one symbol SYM. */
829 static bool
830 symbolic_range_based_on_p (value_range *vr, const_tree sym)
832 bool neg, min_has_symbol, max_has_symbol;
833 tree inv;
835 if (is_gimple_min_invariant (vr->min))
836 min_has_symbol = false;
837 else if (get_single_symbol (vr->min, &neg, &inv) == sym)
838 min_has_symbol = true;
839 else
840 return false;
842 if (is_gimple_min_invariant (vr->max))
843 max_has_symbol = false;
844 else if (get_single_symbol (vr->max, &neg, &inv) == sym)
845 max_has_symbol = true;
846 else
847 return false;
849 return (min_has_symbol || max_has_symbol);
852 /* Return true if the result of assignment STMT is know to be non-zero. */
854 static bool
855 gimple_assign_nonzero_p (gimple *stmt)
857 enum tree_code code = gimple_assign_rhs_code (stmt);
858 bool strict_overflow_p;
859 switch (get_gimple_rhs_class (code))
861 case GIMPLE_UNARY_RHS:
862 return tree_unary_nonzero_warnv_p (gimple_assign_rhs_code (stmt),
863 gimple_expr_type (stmt),
864 gimple_assign_rhs1 (stmt),
865 &strict_overflow_p);
866 case GIMPLE_BINARY_RHS:
867 return tree_binary_nonzero_warnv_p (gimple_assign_rhs_code (stmt),
868 gimple_expr_type (stmt),
869 gimple_assign_rhs1 (stmt),
870 gimple_assign_rhs2 (stmt),
871 &strict_overflow_p);
872 case GIMPLE_TERNARY_RHS:
873 return false;
874 case GIMPLE_SINGLE_RHS:
875 return tree_single_nonzero_warnv_p (gimple_assign_rhs1 (stmt),
876 &strict_overflow_p);
877 case GIMPLE_INVALID_RHS:
878 gcc_unreachable ();
879 default:
880 gcc_unreachable ();
884 /* Return true if STMT is known to compute a non-zero value. */
886 static bool
887 gimple_stmt_nonzero_p (gimple *stmt)
889 switch (gimple_code (stmt))
891 case GIMPLE_ASSIGN:
892 return gimple_assign_nonzero_p (stmt);
893 case GIMPLE_CALL:
895 tree fndecl = gimple_call_fndecl (stmt);
896 if (!fndecl) return false;
897 if (flag_delete_null_pointer_checks && !flag_check_new
898 && DECL_IS_OPERATOR_NEW (fndecl)
899 && !TREE_NOTHROW (fndecl))
900 return true;
901 /* References are always non-NULL. */
902 if (flag_delete_null_pointer_checks
903 && TREE_CODE (TREE_TYPE (fndecl)) == REFERENCE_TYPE)
904 return true;
905 if (flag_delete_null_pointer_checks &&
906 lookup_attribute ("returns_nonnull",
907 TYPE_ATTRIBUTES (gimple_call_fntype (stmt))))
908 return true;
910 gcall *call_stmt = as_a<gcall *> (stmt);
911 unsigned rf = gimple_call_return_flags (call_stmt);
912 if (rf & ERF_RETURNS_ARG)
914 unsigned argnum = rf & ERF_RETURN_ARG_MASK;
915 if (argnum < gimple_call_num_args (call_stmt))
917 tree arg = gimple_call_arg (call_stmt, argnum);
918 if (SSA_VAR_P (arg)
919 && infer_nonnull_range_by_attribute (stmt, arg))
920 return true;
923 return gimple_alloca_call_p (stmt);
925 default:
926 gcc_unreachable ();
930 /* Like tree_expr_nonzero_p, but this function uses value ranges
931 obtained so far. */
933 static bool
934 vrp_stmt_computes_nonzero (gimple *stmt)
936 if (gimple_stmt_nonzero_p (stmt))
937 return true;
939 /* If we have an expression of the form &X->a, then the expression
940 is nonnull if X is nonnull. */
941 if (is_gimple_assign (stmt)
942 && gimple_assign_rhs_code (stmt) == ADDR_EXPR)
944 tree expr = gimple_assign_rhs1 (stmt);
945 tree base = get_base_address (TREE_OPERAND (expr, 0));
947 if (base != NULL_TREE
948 && TREE_CODE (base) == MEM_REF
949 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
951 value_range *vr = get_value_range (TREE_OPERAND (base, 0));
952 if (range_is_nonnull (vr))
953 return true;
957 return false;
960 /* Returns true if EXPR is a valid value (as expected by compare_values) --
961 a gimple invariant, or SSA_NAME +- CST. */
963 static bool
964 valid_value_p (tree expr)
966 if (TREE_CODE (expr) == SSA_NAME)
967 return true;
969 if (TREE_CODE (expr) == PLUS_EXPR
970 || TREE_CODE (expr) == MINUS_EXPR)
971 return (TREE_CODE (TREE_OPERAND (expr, 0)) == SSA_NAME
972 && TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST);
974 return is_gimple_min_invariant (expr);
977 /* Return
978 1 if VAL < VAL2
979 0 if !(VAL < VAL2)
980 -2 if those are incomparable. */
981 static inline int
982 operand_less_p (tree val, tree val2)
984 /* LT is folded faster than GE and others. Inline the common case. */
985 if (TREE_CODE (val) == INTEGER_CST && TREE_CODE (val2) == INTEGER_CST)
986 return tree_int_cst_lt (val, val2);
987 else
989 tree tcmp;
991 fold_defer_overflow_warnings ();
993 tcmp = fold_binary_to_constant (LT_EXPR, boolean_type_node, val, val2);
995 fold_undefer_and_ignore_overflow_warnings ();
997 if (!tcmp
998 || TREE_CODE (tcmp) != INTEGER_CST)
999 return -2;
1001 if (!integer_zerop (tcmp))
1002 return 1;
1005 return 0;
1008 /* Compare two values VAL1 and VAL2. Return
1010 -2 if VAL1 and VAL2 cannot be compared at compile-time,
1011 -1 if VAL1 < VAL2,
1012 0 if VAL1 == VAL2,
1013 +1 if VAL1 > VAL2, and
1014 +2 if VAL1 != VAL2
1016 This is similar to tree_int_cst_compare but supports pointer values
1017 and values that cannot be compared at compile time.
1019 If STRICT_OVERFLOW_P is not NULL, then set *STRICT_OVERFLOW_P to
1020 true if the return value is only valid if we assume that signed
1021 overflow is undefined. */
1023 static int
1024 compare_values_warnv (tree val1, tree val2, bool *strict_overflow_p)
1026 if (val1 == val2)
1027 return 0;
1029 /* Below we rely on the fact that VAL1 and VAL2 are both pointers or
1030 both integers. */
1031 gcc_assert (POINTER_TYPE_P (TREE_TYPE (val1))
1032 == POINTER_TYPE_P (TREE_TYPE (val2)));
1034 /* Convert the two values into the same type. This is needed because
1035 sizetype causes sign extension even for unsigned types. */
1036 val2 = fold_convert (TREE_TYPE (val1), val2);
1037 STRIP_USELESS_TYPE_CONVERSION (val2);
1039 const bool overflow_undefined
1040 = INTEGRAL_TYPE_P (TREE_TYPE (val1))
1041 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (val1));
1042 tree inv1, inv2;
1043 bool neg1, neg2;
1044 tree sym1 = get_single_symbol (val1, &neg1, &inv1);
1045 tree sym2 = get_single_symbol (val2, &neg2, &inv2);
1047 /* If VAL1 and VAL2 are of the form '[-]NAME [+ CST]', return -1 or +1
1048 accordingly. If VAL1 and VAL2 don't use the same name, return -2. */
1049 if (sym1 && sym2)
1051 /* Both values must use the same name with the same sign. */
1052 if (sym1 != sym2 || neg1 != neg2)
1053 return -2;
1055 /* [-]NAME + CST == [-]NAME + CST. */
1056 if (inv1 == inv2)
1057 return 0;
1059 /* If overflow is defined we cannot simplify more. */
1060 if (!overflow_undefined)
1061 return -2;
1063 if (strict_overflow_p != NULL
1064 /* Symbolic range building sets TREE_NO_WARNING to declare
1065 that overflow doesn't happen. */
1066 && (!inv1 || !TREE_NO_WARNING (val1))
1067 && (!inv2 || !TREE_NO_WARNING (val2)))
1068 *strict_overflow_p = true;
1070 if (!inv1)
1071 inv1 = build_int_cst (TREE_TYPE (val1), 0);
1072 if (!inv2)
1073 inv2 = build_int_cst (TREE_TYPE (val2), 0);
1075 return wi::cmp (inv1, inv2, TYPE_SIGN (TREE_TYPE (val1)));
1078 const bool cst1 = is_gimple_min_invariant (val1);
1079 const bool cst2 = is_gimple_min_invariant (val2);
1081 /* If one is of the form '[-]NAME + CST' and the other is constant, then
1082 it might be possible to say something depending on the constants. */
1083 if ((sym1 && inv1 && cst2) || (sym2 && inv2 && cst1))
1085 if (!overflow_undefined)
1086 return -2;
1088 if (strict_overflow_p != NULL
1089 /* Symbolic range building sets TREE_NO_WARNING to declare
1090 that overflow doesn't happen. */
1091 && (!sym1 || !TREE_NO_WARNING (val1))
1092 && (!sym2 || !TREE_NO_WARNING (val2)))
1093 *strict_overflow_p = true;
1095 const signop sgn = TYPE_SIGN (TREE_TYPE (val1));
1096 tree cst = cst1 ? val1 : val2;
1097 tree inv = cst1 ? inv2 : inv1;
1099 /* Compute the difference between the constants. If it overflows or
1100 underflows, this means that we can trivially compare the NAME with
1101 it and, consequently, the two values with each other. */
1102 wide_int diff = wi::sub (cst, inv);
1103 if (wi::cmp (0, inv, sgn) != wi::cmp (diff, cst, sgn))
1105 const int res = wi::cmp (cst, inv, sgn);
1106 return cst1 ? res : -res;
1109 return -2;
1112 /* We cannot say anything more for non-constants. */
1113 if (!cst1 || !cst2)
1114 return -2;
1116 if (!POINTER_TYPE_P (TREE_TYPE (val1)))
1118 /* We cannot compare overflowed values. */
1119 if (TREE_OVERFLOW (val1) || TREE_OVERFLOW (val2))
1120 return -2;
1122 return tree_int_cst_compare (val1, val2);
1124 else
1126 tree t;
1128 /* First see if VAL1 and VAL2 are not the same. */
1129 if (val1 == val2 || operand_equal_p (val1, val2, 0))
1130 return 0;
1132 /* If VAL1 is a lower address than VAL2, return -1. */
1133 if (operand_less_p (val1, val2) == 1)
1134 return -1;
1136 /* If VAL1 is a higher address than VAL2, return +1. */
1137 if (operand_less_p (val2, val1) == 1)
1138 return 1;
1140 /* If VAL1 is different than VAL2, return +2.
1141 For integer constants we either have already returned -1 or 1
1142 or they are equivalent. We still might succeed in proving
1143 something about non-trivial operands. */
1144 if (TREE_CODE (val1) != INTEGER_CST
1145 || TREE_CODE (val2) != INTEGER_CST)
1147 t = fold_binary_to_constant (NE_EXPR, boolean_type_node, val1, val2);
1148 if (t && integer_onep (t))
1149 return 2;
1152 return -2;
1156 /* Compare values like compare_values_warnv. */
1158 static int
1159 compare_values (tree val1, tree val2)
1161 bool sop;
1162 return compare_values_warnv (val1, val2, &sop);
1166 /* Return 1 if VAL is inside value range MIN <= VAL <= MAX,
1167 0 if VAL is not inside [MIN, MAX],
1168 -2 if we cannot tell either way.
1170 Benchmark compile/20001226-1.c compilation time after changing this
1171 function. */
1173 static inline int
1174 value_inside_range (tree val, tree min, tree max)
1176 int cmp1, cmp2;
1178 cmp1 = operand_less_p (val, min);
1179 if (cmp1 == -2)
1180 return -2;
1181 if (cmp1 == 1)
1182 return 0;
1184 cmp2 = operand_less_p (max, val);
1185 if (cmp2 == -2)
1186 return -2;
1188 return !cmp2;
1192 /* Return true if value ranges VR0 and VR1 have a non-empty
1193 intersection.
1195 Benchmark compile/20001226-1.c compilation time after changing this
1196 function.
1199 static inline bool
1200 value_ranges_intersect_p (value_range *vr0, value_range *vr1)
1202 /* The value ranges do not intersect if the maximum of the first range is
1203 less than the minimum of the second range or vice versa.
1204 When those relations are unknown, we can't do any better. */
1205 if (operand_less_p (vr0->max, vr1->min) != 0)
1206 return false;
1207 if (operand_less_p (vr1->max, vr0->min) != 0)
1208 return false;
1209 return true;
1213 /* Return 1 if [MIN, MAX] includes the value zero, 0 if it does not
1214 include the value zero, -2 if we cannot tell. */
1216 static inline int
1217 range_includes_zero_p (tree min, tree max)
1219 tree zero = build_int_cst (TREE_TYPE (min), 0);
1220 return value_inside_range (zero, min, max);
1223 /* Return true if *VR is know to only contain nonnegative values. */
1225 static inline bool
1226 value_range_nonnegative_p (value_range *vr)
1228 /* Testing for VR_ANTI_RANGE is not useful here as any anti-range
1229 which would return a useful value should be encoded as a
1230 VR_RANGE. */
1231 if (vr->type == VR_RANGE)
1233 int result = compare_values (vr->min, integer_zero_node);
1234 return (result == 0 || result == 1);
1237 return false;
1240 /* If *VR has a value rante that is a single constant value return that,
1241 otherwise return NULL_TREE. */
1243 static tree
1244 value_range_constant_singleton (value_range *vr)
1246 if (vr->type == VR_RANGE
1247 && vrp_operand_equal_p (vr->min, vr->max)
1248 && is_gimple_min_invariant (vr->min))
1249 return vr->min;
1251 return NULL_TREE;
1254 /* If OP has a value range with a single constant value return that,
1255 otherwise return NULL_TREE. This returns OP itself if OP is a
1256 constant. */
1258 static tree
1259 op_with_constant_singleton_value_range (tree op)
1261 if (is_gimple_min_invariant (op))
1262 return op;
1264 if (TREE_CODE (op) != SSA_NAME)
1265 return NULL_TREE;
1267 return value_range_constant_singleton (get_value_range (op));
1270 /* Return true if op is in a boolean [0, 1] value-range. */
1272 static bool
1273 op_with_boolean_value_range_p (tree op)
1275 value_range *vr;
1277 if (TYPE_PRECISION (TREE_TYPE (op)) == 1)
1278 return true;
1280 if (integer_zerop (op)
1281 || integer_onep (op))
1282 return true;
1284 if (TREE_CODE (op) != SSA_NAME)
1285 return false;
1287 vr = get_value_range (op);
1288 return (vr->type == VR_RANGE
1289 && integer_zerop (vr->min)
1290 && integer_onep (vr->max));
1293 /* Extract value range information for VAR when (OP COND_CODE LIMIT) is
1294 true and store it in *VR_P. */
1296 static void
1297 extract_range_for_var_from_comparison_expr (tree var, enum tree_code cond_code,
1298 tree op, tree limit,
1299 value_range *vr_p)
1301 tree min, max, type;
1302 value_range *limit_vr;
1303 type = TREE_TYPE (var);
1304 gcc_assert (limit != var);
1306 /* For pointer arithmetic, we only keep track of pointer equality
1307 and inequality. */
1308 if (POINTER_TYPE_P (type) && cond_code != NE_EXPR && cond_code != EQ_EXPR)
1310 set_value_range_to_varying (vr_p);
1311 return;
1314 /* If LIMIT is another SSA name and LIMIT has a range of its own,
1315 try to use LIMIT's range to avoid creating symbolic ranges
1316 unnecessarily. */
1317 limit_vr = (TREE_CODE (limit) == SSA_NAME) ? get_value_range (limit) : NULL;
1319 /* LIMIT's range is only interesting if it has any useful information. */
1320 if (! limit_vr
1321 || limit_vr->type == VR_UNDEFINED
1322 || limit_vr->type == VR_VARYING
1323 || (symbolic_range_p (limit_vr)
1324 && ! (limit_vr->type == VR_RANGE
1325 && (limit_vr->min == limit_vr->max
1326 || operand_equal_p (limit_vr->min, limit_vr->max, 0)))))
1327 limit_vr = NULL;
1329 /* Initially, the new range has the same set of equivalences of
1330 VAR's range. This will be revised before returning the final
1331 value. Since assertions may be chained via mutually exclusive
1332 predicates, we will need to trim the set of equivalences before
1333 we are done. */
1334 gcc_assert (vr_p->equiv == NULL);
1335 add_equivalence (&vr_p->equiv, var);
1337 /* Extract a new range based on the asserted comparison for VAR and
1338 LIMIT's value range. Notice that if LIMIT has an anti-range, we
1339 will only use it for equality comparisons (EQ_EXPR). For any
1340 other kind of assertion, we cannot derive a range from LIMIT's
1341 anti-range that can be used to describe the new range. For
1342 instance, ASSERT_EXPR <x_2, x_2 <= b_4>. If b_4 is ~[2, 10],
1343 then b_4 takes on the ranges [-INF, 1] and [11, +INF]. There is
1344 no single range for x_2 that could describe LE_EXPR, so we might
1345 as well build the range [b_4, +INF] for it.
1346 One special case we handle is extracting a range from a
1347 range test encoded as (unsigned)var + CST <= limit. */
1348 if (TREE_CODE (op) == NOP_EXPR
1349 || TREE_CODE (op) == PLUS_EXPR)
1351 if (TREE_CODE (op) == PLUS_EXPR)
1353 min = fold_build1 (NEGATE_EXPR, TREE_TYPE (TREE_OPERAND (op, 1)),
1354 TREE_OPERAND (op, 1));
1355 max = int_const_binop (PLUS_EXPR, limit, min);
1356 op = TREE_OPERAND (op, 0);
1358 else
1360 min = build_int_cst (TREE_TYPE (var), 0);
1361 max = limit;
1364 /* Make sure to not set TREE_OVERFLOW on the final type
1365 conversion. We are willingly interpreting large positive
1366 unsigned values as negative signed values here. */
1367 min = force_fit_type (TREE_TYPE (var), wi::to_widest (min), 0, false);
1368 max = force_fit_type (TREE_TYPE (var), wi::to_widest (max), 0, false);
1370 /* We can transform a max, min range to an anti-range or
1371 vice-versa. Use set_and_canonicalize_value_range which does
1372 this for us. */
1373 if (cond_code == LE_EXPR)
1374 set_and_canonicalize_value_range (vr_p, VR_RANGE,
1375 min, max, vr_p->equiv);
1376 else if (cond_code == GT_EXPR)
1377 set_and_canonicalize_value_range (vr_p, VR_ANTI_RANGE,
1378 min, max, vr_p->equiv);
1379 else
1380 gcc_unreachable ();
1382 else if (cond_code == EQ_EXPR)
1384 enum value_range_type range_type;
1386 if (limit_vr)
1388 range_type = limit_vr->type;
1389 min = limit_vr->min;
1390 max = limit_vr->max;
1392 else
1394 range_type = VR_RANGE;
1395 min = limit;
1396 max = limit;
1399 set_value_range (vr_p, range_type, min, max, vr_p->equiv);
1401 /* When asserting the equality VAR == LIMIT and LIMIT is another
1402 SSA name, the new range will also inherit the equivalence set
1403 from LIMIT. */
1404 if (TREE_CODE (limit) == SSA_NAME)
1405 add_equivalence (&vr_p->equiv, limit);
1407 else if (cond_code == NE_EXPR)
1409 /* As described above, when LIMIT's range is an anti-range and
1410 this assertion is an inequality (NE_EXPR), then we cannot
1411 derive anything from the anti-range. For instance, if
1412 LIMIT's range was ~[0, 0], the assertion 'VAR != LIMIT' does
1413 not imply that VAR's range is [0, 0]. So, in the case of
1414 anti-ranges, we just assert the inequality using LIMIT and
1415 not its anti-range.
1417 If LIMIT_VR is a range, we can only use it to build a new
1418 anti-range if LIMIT_VR is a single-valued range. For
1419 instance, if LIMIT_VR is [0, 1], the predicate
1420 VAR != [0, 1] does not mean that VAR's range is ~[0, 1].
1421 Rather, it means that for value 0 VAR should be ~[0, 0]
1422 and for value 1, VAR should be ~[1, 1]. We cannot
1423 represent these ranges.
1425 The only situation in which we can build a valid
1426 anti-range is when LIMIT_VR is a single-valued range
1427 (i.e., LIMIT_VR->MIN == LIMIT_VR->MAX). In that case,
1428 build the anti-range ~[LIMIT_VR->MIN, LIMIT_VR->MAX]. */
1429 if (limit_vr
1430 && limit_vr->type == VR_RANGE
1431 && compare_values (limit_vr->min, limit_vr->max) == 0)
1433 min = limit_vr->min;
1434 max = limit_vr->max;
1436 else
1438 /* In any other case, we cannot use LIMIT's range to build a
1439 valid anti-range. */
1440 min = max = limit;
1443 /* If MIN and MAX cover the whole range for their type, then
1444 just use the original LIMIT. */
1445 if (INTEGRAL_TYPE_P (type)
1446 && vrp_val_is_min (min)
1447 && vrp_val_is_max (max))
1448 min = max = limit;
1450 set_and_canonicalize_value_range (vr_p, VR_ANTI_RANGE,
1451 min, max, vr_p->equiv);
1453 else if (cond_code == LE_EXPR || cond_code == LT_EXPR)
1455 min = TYPE_MIN_VALUE (type);
1457 if (limit_vr == NULL || limit_vr->type == VR_ANTI_RANGE)
1458 max = limit;
1459 else
1461 /* If LIMIT_VR is of the form [N1, N2], we need to build the
1462 range [MIN, N2] for LE_EXPR and [MIN, N2 - 1] for
1463 LT_EXPR. */
1464 max = limit_vr->max;
1467 /* If the maximum value forces us to be out of bounds, simply punt.
1468 It would be pointless to try and do anything more since this
1469 all should be optimized away above us. */
1470 if (cond_code == LT_EXPR
1471 && compare_values (max, min) == 0)
1472 set_value_range_to_varying (vr_p);
1473 else
1475 /* For LT_EXPR, we create the range [MIN, MAX - 1]. */
1476 if (cond_code == LT_EXPR)
1478 if (TYPE_PRECISION (TREE_TYPE (max)) == 1
1479 && !TYPE_UNSIGNED (TREE_TYPE (max)))
1480 max = fold_build2 (PLUS_EXPR, TREE_TYPE (max), max,
1481 build_int_cst (TREE_TYPE (max), -1));
1482 else
1483 max = fold_build2 (MINUS_EXPR, TREE_TYPE (max), max,
1484 build_int_cst (TREE_TYPE (max), 1));
1485 /* Signal to compare_values_warnv this expr doesn't overflow. */
1486 if (EXPR_P (max))
1487 TREE_NO_WARNING (max) = 1;
1490 set_value_range (vr_p, VR_RANGE, min, max, vr_p->equiv);
1493 else if (cond_code == GE_EXPR || cond_code == GT_EXPR)
1495 max = TYPE_MAX_VALUE (type);
1497 if (limit_vr == NULL || limit_vr->type == VR_ANTI_RANGE)
1498 min = limit;
1499 else
1501 /* If LIMIT_VR is of the form [N1, N2], we need to build the
1502 range [N1, MAX] for GE_EXPR and [N1 + 1, MAX] for
1503 GT_EXPR. */
1504 min = limit_vr->min;
1507 /* If the minimum value forces us to be out of bounds, simply punt.
1508 It would be pointless to try and do anything more since this
1509 all should be optimized away above us. */
1510 if (cond_code == GT_EXPR
1511 && compare_values (min, max) == 0)
1512 set_value_range_to_varying (vr_p);
1513 else
1515 /* For GT_EXPR, we create the range [MIN + 1, MAX]. */
1516 if (cond_code == GT_EXPR)
1518 if (TYPE_PRECISION (TREE_TYPE (min)) == 1
1519 && !TYPE_UNSIGNED (TREE_TYPE (min)))
1520 min = fold_build2 (MINUS_EXPR, TREE_TYPE (min), min,
1521 build_int_cst (TREE_TYPE (min), -1));
1522 else
1523 min = fold_build2 (PLUS_EXPR, TREE_TYPE (min), min,
1524 build_int_cst (TREE_TYPE (min), 1));
1525 /* Signal to compare_values_warnv this expr doesn't overflow. */
1526 if (EXPR_P (min))
1527 TREE_NO_WARNING (min) = 1;
1530 set_value_range (vr_p, VR_RANGE, min, max, vr_p->equiv);
1533 else
1534 gcc_unreachable ();
1536 /* Finally intersect the new range with what we already know about var. */
1537 vrp_intersect_ranges (vr_p, get_value_range (var));
1540 /* Extract value range information from an ASSERT_EXPR EXPR and store
1541 it in *VR_P. */
1543 static void
1544 extract_range_from_assert (value_range *vr_p, tree expr)
1546 tree var = ASSERT_EXPR_VAR (expr);
1547 tree cond = ASSERT_EXPR_COND (expr);
1548 tree limit, op;
1549 enum tree_code cond_code;
1550 gcc_assert (COMPARISON_CLASS_P (cond));
1552 /* Find VAR in the ASSERT_EXPR conditional. */
1553 if (var == TREE_OPERAND (cond, 0)
1554 || TREE_CODE (TREE_OPERAND (cond, 0)) == PLUS_EXPR
1555 || TREE_CODE (TREE_OPERAND (cond, 0)) == NOP_EXPR)
1557 /* If the predicate is of the form VAR COMP LIMIT, then we just
1558 take LIMIT from the RHS and use the same comparison code. */
1559 cond_code = TREE_CODE (cond);
1560 limit = TREE_OPERAND (cond, 1);
1561 op = TREE_OPERAND (cond, 0);
1563 else
1565 /* If the predicate is of the form LIMIT COMP VAR, then we need
1566 to flip around the comparison code to create the proper range
1567 for VAR. */
1568 cond_code = swap_tree_comparison (TREE_CODE (cond));
1569 limit = TREE_OPERAND (cond, 0);
1570 op = TREE_OPERAND (cond, 1);
1572 extract_range_for_var_from_comparison_expr (var, cond_code, op,
1573 limit, vr_p);
1576 /* Extract range information from SSA name VAR and store it in VR. If
1577 VAR has an interesting range, use it. Otherwise, create the
1578 range [VAR, VAR] and return it. This is useful in situations where
1579 we may have conditionals testing values of VARYING names. For
1580 instance,
1582 x_3 = y_5;
1583 if (x_3 > y_5)
1586 Even if y_5 is deemed VARYING, we can determine that x_3 > y_5 is
1587 always false. */
1589 static void
1590 extract_range_from_ssa_name (value_range *vr, tree var)
1592 value_range *var_vr = get_value_range (var);
1594 if (var_vr->type != VR_VARYING)
1595 copy_value_range (vr, var_vr);
1596 else
1597 set_value_range (vr, VR_RANGE, var, var, NULL);
1599 add_equivalence (&vr->equiv, var);
1603 /* Wrapper around int_const_binop. If the operation overflows and
1604 overflow is undefined, then adjust the result to be
1605 -INF or +INF depending on CODE, VAL1 and VAL2. Sets *OVERFLOW_P
1606 to whether the operation overflowed. For division by zero
1607 the result is indeterminate but *OVERFLOW_P is set. */
1609 static wide_int
1610 vrp_int_const_binop (enum tree_code code, tree val1, tree val2,
1611 bool *overflow_p)
1613 bool overflow = false;
1614 signop sign = TYPE_SIGN (TREE_TYPE (val1));
1615 wide_int res;
1617 *overflow_p = false;
1619 switch (code)
1621 case RSHIFT_EXPR:
1622 case LSHIFT_EXPR:
1624 wide_int wval2 = wi::to_wide (val2, TYPE_PRECISION (TREE_TYPE (val1)));
1625 if (wi::neg_p (wval2))
1627 wval2 = -wval2;
1628 if (code == RSHIFT_EXPR)
1629 code = LSHIFT_EXPR;
1630 else
1631 code = RSHIFT_EXPR;
1634 if (code == RSHIFT_EXPR)
1635 /* It's unclear from the C standard whether shifts can overflow.
1636 The following code ignores overflow; perhaps a C standard
1637 interpretation ruling is needed. */
1638 res = wi::rshift (val1, wval2, sign);
1639 else
1640 res = wi::lshift (val1, wval2);
1641 break;
1644 case MULT_EXPR:
1645 res = wi::mul (val1, val2, sign, &overflow);
1646 break;
1648 case TRUNC_DIV_EXPR:
1649 case EXACT_DIV_EXPR:
1650 if (val2 == 0)
1652 *overflow_p = true;
1653 return res;
1655 else
1656 res = wi::div_trunc (val1, val2, sign, &overflow);
1657 break;
1659 case FLOOR_DIV_EXPR:
1660 if (val2 == 0)
1662 *overflow_p = true;
1663 return res;
1665 res = wi::div_floor (val1, val2, sign, &overflow);
1666 break;
1668 case CEIL_DIV_EXPR:
1669 if (val2 == 0)
1671 *overflow_p = true;
1672 return res;
1674 res = wi::div_ceil (val1, val2, sign, &overflow);
1675 break;
1677 case ROUND_DIV_EXPR:
1678 if (val2 == 0)
1680 *overflow_p = 0;
1681 return res;
1683 res = wi::div_round (val1, val2, sign, &overflow);
1684 break;
1686 default:
1687 gcc_unreachable ();
1690 if (overflow
1691 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (val1)))
1693 /* If the operation overflowed return -INF or +INF depending
1694 on the operation and the combination of signs of the operands. */
1695 int sgn1 = tree_int_cst_sgn (val1);
1696 int sgn2 = tree_int_cst_sgn (val2);
1698 /* Notice that we only need to handle the restricted set of
1699 operations handled by extract_range_from_binary_expr.
1700 Among them, only multiplication, addition and subtraction
1701 can yield overflow without overflown operands because we
1702 are working with integral types only... except in the
1703 case VAL1 = -INF and VAL2 = -1 which overflows to +INF
1704 for division too. */
1706 /* For multiplication, the sign of the overflow is given
1707 by the comparison of the signs of the operands. */
1708 if ((code == MULT_EXPR && sgn1 == sgn2)
1709 /* For addition, the operands must be of the same sign
1710 to yield an overflow. Its sign is therefore that
1711 of one of the operands, for example the first. */
1712 || (code == PLUS_EXPR && sgn1 >= 0)
1713 /* For subtraction, operands must be of
1714 different signs to yield an overflow. Its sign is
1715 therefore that of the first operand or the opposite of
1716 that of the second operand. A first operand of 0 counts
1717 as positive here, for the corner case 0 - (-INF), which
1718 overflows, but must yield +INF. */
1719 || (code == MINUS_EXPR && sgn1 >= 0)
1720 /* For division, the only case is -INF / -1 = +INF. */
1721 || code == TRUNC_DIV_EXPR
1722 || code == FLOOR_DIV_EXPR
1723 || code == CEIL_DIV_EXPR
1724 || code == EXACT_DIV_EXPR
1725 || code == ROUND_DIV_EXPR)
1726 return wi::max_value (TYPE_PRECISION (TREE_TYPE (val1)),
1727 TYPE_SIGN (TREE_TYPE (val1)));
1728 else
1729 return wi::min_value (TYPE_PRECISION (TREE_TYPE (val1)),
1730 TYPE_SIGN (TREE_TYPE (val1)));
1733 *overflow_p = overflow;
1735 return res;
1739 /* For range VR compute two wide_int bitmasks. In *MAY_BE_NONZERO
1740 bitmask if some bit is unset, it means for all numbers in the range
1741 the bit is 0, otherwise it might be 0 or 1. In *MUST_BE_NONZERO
1742 bitmask if some bit is set, it means for all numbers in the range
1743 the bit is 1, otherwise it might be 0 or 1. */
1745 static bool
1746 zero_nonzero_bits_from_vr (const tree expr_type,
1747 value_range *vr,
1748 wide_int *may_be_nonzero,
1749 wide_int *must_be_nonzero)
1751 *may_be_nonzero = wi::minus_one (TYPE_PRECISION (expr_type));
1752 *must_be_nonzero = wi::zero (TYPE_PRECISION (expr_type));
1753 if (!range_int_cst_p (vr))
1754 return false;
1756 if (range_int_cst_singleton_p (vr))
1758 *may_be_nonzero = vr->min;
1759 *must_be_nonzero = *may_be_nonzero;
1761 else if (tree_int_cst_sgn (vr->min) >= 0
1762 || tree_int_cst_sgn (vr->max) < 0)
1764 wide_int xor_mask = wi::bit_xor (vr->min, vr->max);
1765 *may_be_nonzero = wi::bit_or (vr->min, vr->max);
1766 *must_be_nonzero = wi::bit_and (vr->min, vr->max);
1767 if (xor_mask != 0)
1769 wide_int mask = wi::mask (wi::floor_log2 (xor_mask), false,
1770 may_be_nonzero->get_precision ());
1771 *may_be_nonzero = *may_be_nonzero | mask;
1772 *must_be_nonzero = must_be_nonzero->and_not (mask);
1776 return true;
1779 /* Create two value-ranges in *VR0 and *VR1 from the anti-range *AR
1780 so that *VR0 U *VR1 == *AR. Returns true if that is possible,
1781 false otherwise. If *AR can be represented with a single range
1782 *VR1 will be VR_UNDEFINED. */
1784 static bool
1785 ranges_from_anti_range (value_range *ar,
1786 value_range *vr0, value_range *vr1)
1788 tree type = TREE_TYPE (ar->min);
1790 vr0->type = VR_UNDEFINED;
1791 vr1->type = VR_UNDEFINED;
1793 if (ar->type != VR_ANTI_RANGE
1794 || TREE_CODE (ar->min) != INTEGER_CST
1795 || TREE_CODE (ar->max) != INTEGER_CST
1796 || !vrp_val_min (type)
1797 || !vrp_val_max (type))
1798 return false;
1800 if (!vrp_val_is_min (ar->min))
1802 vr0->type = VR_RANGE;
1803 vr0->min = vrp_val_min (type);
1804 vr0->max = wide_int_to_tree (type, wi::sub (ar->min, 1));
1806 if (!vrp_val_is_max (ar->max))
1808 vr1->type = VR_RANGE;
1809 vr1->min = wide_int_to_tree (type, wi::add (ar->max, 1));
1810 vr1->max = vrp_val_max (type);
1812 if (vr0->type == VR_UNDEFINED)
1814 *vr0 = *vr1;
1815 vr1->type = VR_UNDEFINED;
1818 return vr0->type != VR_UNDEFINED;
1821 /* Helper to extract a value-range *VR for a multiplicative operation
1822 *VR0 CODE *VR1. */
1824 static void
1825 extract_range_from_multiplicative_op_1 (value_range *vr,
1826 enum tree_code code,
1827 value_range *vr0, value_range *vr1)
1829 enum value_range_type rtype;
1830 wide_int val, min, max;
1831 bool sop;
1832 tree type;
1834 /* Multiplications, divisions and shifts are a bit tricky to handle,
1835 depending on the mix of signs we have in the two ranges, we
1836 need to operate on different values to get the minimum and
1837 maximum values for the new range. One approach is to figure
1838 out all the variations of range combinations and do the
1839 operations.
1841 However, this involves several calls to compare_values and it
1842 is pretty convoluted. It's simpler to do the 4 operations
1843 (MIN0 OP MIN1, MIN0 OP MAX1, MAX0 OP MIN1 and MAX0 OP MAX0 OP
1844 MAX1) and then figure the smallest and largest values to form
1845 the new range. */
1846 gcc_assert (code == MULT_EXPR
1847 || code == TRUNC_DIV_EXPR
1848 || code == FLOOR_DIV_EXPR
1849 || code == CEIL_DIV_EXPR
1850 || code == EXACT_DIV_EXPR
1851 || code == ROUND_DIV_EXPR
1852 || code == RSHIFT_EXPR
1853 || code == LSHIFT_EXPR);
1854 gcc_assert ((vr0->type == VR_RANGE
1855 || (code == MULT_EXPR && vr0->type == VR_ANTI_RANGE))
1856 && vr0->type == vr1->type);
1858 rtype = vr0->type;
1859 type = TREE_TYPE (vr0->min);
1860 signop sgn = TYPE_SIGN (type);
1862 /* Compute the 4 cross operations and their minimum and maximum value. */
1863 sop = false;
1864 val = vrp_int_const_binop (code, vr0->min, vr1->min, &sop);
1865 if (! sop)
1866 min = max = val;
1868 if (vr1->max == vr1->min)
1870 else if (! sop)
1872 val = vrp_int_const_binop (code, vr0->min, vr1->max, &sop);
1873 if (! sop)
1875 if (wi::lt_p (val, min, sgn))
1876 min = val;
1877 else if (wi::gt_p (val, max, sgn))
1878 max = val;
1882 if (vr0->max == vr0->min)
1884 else if (! sop)
1886 val = vrp_int_const_binop (code, vr0->max, vr1->min, &sop);
1887 if (! sop)
1889 if (wi::lt_p (val, min, sgn))
1890 min = val;
1891 else if (wi::gt_p (val, max, sgn))
1892 max = val;
1896 if (vr0->min == vr0->max || vr1->min == vr1->max)
1898 else if (! sop)
1900 val = vrp_int_const_binop (code, vr0->max, vr1->max, &sop);
1901 if (! sop)
1903 if (wi::lt_p (val, min, sgn))
1904 min = val;
1905 else if (wi::gt_p (val, max, sgn))
1906 max = val;
1910 /* If either operation overflowed, drop to VARYING. */
1911 if (sop)
1913 set_value_range_to_varying (vr);
1914 return;
1917 /* If the new range has its limits swapped around (MIN > MAX),
1918 then the operation caused one of them to wrap around, mark
1919 the new range VARYING. */
1920 if (wi::gt_p (min, max, sgn))
1922 set_value_range_to_varying (vr);
1923 return;
1926 /* We punt for [-INF, +INF].
1927 We learn nothing when we have INF on both sides.
1928 Note that we do accept [-INF, -INF] and [+INF, +INF]. */
1929 if (wi::eq_p (min, wi::min_value (TYPE_PRECISION (type), sgn))
1930 && wi::eq_p (max, wi::max_value (TYPE_PRECISION (type), sgn)))
1932 set_value_range_to_varying (vr);
1933 return;
1936 set_value_range (vr, rtype,
1937 wide_int_to_tree (type, min),
1938 wide_int_to_tree (type, max), NULL);
1941 /* Extract range information from a binary operation CODE based on
1942 the ranges of each of its operands *VR0 and *VR1 with resulting
1943 type EXPR_TYPE. The resulting range is stored in *VR. */
1945 static void
1946 extract_range_from_binary_expr_1 (value_range *vr,
1947 enum tree_code code, tree expr_type,
1948 value_range *vr0_, value_range *vr1_)
1950 value_range vr0 = *vr0_, vr1 = *vr1_;
1951 value_range vrtem0 = VR_INITIALIZER, vrtem1 = VR_INITIALIZER;
1952 enum value_range_type type;
1953 tree min = NULL_TREE, max = NULL_TREE;
1954 int cmp;
1956 if (!INTEGRAL_TYPE_P (expr_type)
1957 && !POINTER_TYPE_P (expr_type))
1959 set_value_range_to_varying (vr);
1960 return;
1963 /* Not all binary expressions can be applied to ranges in a
1964 meaningful way. Handle only arithmetic operations. */
1965 if (code != PLUS_EXPR
1966 && code != MINUS_EXPR
1967 && code != POINTER_PLUS_EXPR
1968 && code != MULT_EXPR
1969 && code != TRUNC_DIV_EXPR
1970 && code != FLOOR_DIV_EXPR
1971 && code != CEIL_DIV_EXPR
1972 && code != EXACT_DIV_EXPR
1973 && code != ROUND_DIV_EXPR
1974 && code != TRUNC_MOD_EXPR
1975 && code != RSHIFT_EXPR
1976 && code != LSHIFT_EXPR
1977 && code != MIN_EXPR
1978 && code != MAX_EXPR
1979 && code != BIT_AND_EXPR
1980 && code != BIT_IOR_EXPR
1981 && code != BIT_XOR_EXPR)
1983 set_value_range_to_varying (vr);
1984 return;
1987 /* If both ranges are UNDEFINED, so is the result. */
1988 if (vr0.type == VR_UNDEFINED && vr1.type == VR_UNDEFINED)
1990 set_value_range_to_undefined (vr);
1991 return;
1993 /* If one of the ranges is UNDEFINED drop it to VARYING for the following
1994 code. At some point we may want to special-case operations that
1995 have UNDEFINED result for all or some value-ranges of the not UNDEFINED
1996 operand. */
1997 else if (vr0.type == VR_UNDEFINED)
1998 set_value_range_to_varying (&vr0);
1999 else if (vr1.type == VR_UNDEFINED)
2000 set_value_range_to_varying (&vr1);
2002 /* We get imprecise results from ranges_from_anti_range when
2003 code is EXACT_DIV_EXPR. We could mask out bits in the resulting
2004 range, but then we also need to hack up vrp_meet. It's just
2005 easier to special case when vr0 is ~[0,0] for EXACT_DIV_EXPR. */
2006 if (code == EXACT_DIV_EXPR
2007 && vr0.type == VR_ANTI_RANGE
2008 && vr0.min == vr0.max
2009 && integer_zerop (vr0.min))
2011 set_value_range_to_nonnull (vr, expr_type);
2012 return;
2015 /* Now canonicalize anti-ranges to ranges when they are not symbolic
2016 and express ~[] op X as ([]' op X) U ([]'' op X). */
2017 if (vr0.type == VR_ANTI_RANGE
2018 && ranges_from_anti_range (&vr0, &vrtem0, &vrtem1))
2020 extract_range_from_binary_expr_1 (vr, code, expr_type, &vrtem0, vr1_);
2021 if (vrtem1.type != VR_UNDEFINED)
2023 value_range vrres = VR_INITIALIZER;
2024 extract_range_from_binary_expr_1 (&vrres, code, expr_type,
2025 &vrtem1, vr1_);
2026 vrp_meet (vr, &vrres);
2028 return;
2030 /* Likewise for X op ~[]. */
2031 if (vr1.type == VR_ANTI_RANGE
2032 && ranges_from_anti_range (&vr1, &vrtem0, &vrtem1))
2034 extract_range_from_binary_expr_1 (vr, code, expr_type, vr0_, &vrtem0);
2035 if (vrtem1.type != VR_UNDEFINED)
2037 value_range vrres = VR_INITIALIZER;
2038 extract_range_from_binary_expr_1 (&vrres, code, expr_type,
2039 vr0_, &vrtem1);
2040 vrp_meet (vr, &vrres);
2042 return;
2045 /* The type of the resulting value range defaults to VR0.TYPE. */
2046 type = vr0.type;
2048 /* Refuse to operate on VARYING ranges, ranges of different kinds
2049 and symbolic ranges. As an exception, we allow BIT_{AND,IOR}
2050 because we may be able to derive a useful range even if one of
2051 the operands is VR_VARYING or symbolic range. Similarly for
2052 divisions, MIN/MAX and PLUS/MINUS.
2054 TODO, we may be able to derive anti-ranges in some cases. */
2055 if (code != BIT_AND_EXPR
2056 && code != BIT_IOR_EXPR
2057 && code != TRUNC_DIV_EXPR
2058 && code != FLOOR_DIV_EXPR
2059 && code != CEIL_DIV_EXPR
2060 && code != EXACT_DIV_EXPR
2061 && code != ROUND_DIV_EXPR
2062 && code != TRUNC_MOD_EXPR
2063 && code != MIN_EXPR
2064 && code != MAX_EXPR
2065 && code != PLUS_EXPR
2066 && code != MINUS_EXPR
2067 && code != RSHIFT_EXPR
2068 && (vr0.type == VR_VARYING
2069 || vr1.type == VR_VARYING
2070 || vr0.type != vr1.type
2071 || symbolic_range_p (&vr0)
2072 || symbolic_range_p (&vr1)))
2074 set_value_range_to_varying (vr);
2075 return;
2078 /* Now evaluate the expression to determine the new range. */
2079 if (POINTER_TYPE_P (expr_type))
2081 if (code == MIN_EXPR || code == MAX_EXPR)
2083 /* For MIN/MAX expressions with pointers, we only care about
2084 nullness, if both are non null, then the result is nonnull.
2085 If both are null, then the result is null. Otherwise they
2086 are varying. */
2087 if (range_is_nonnull (&vr0) && range_is_nonnull (&vr1))
2088 set_value_range_to_nonnull (vr, expr_type);
2089 else if (range_is_null (&vr0) && range_is_null (&vr1))
2090 set_value_range_to_null (vr, expr_type);
2091 else
2092 set_value_range_to_varying (vr);
2094 else if (code == POINTER_PLUS_EXPR)
2096 /* For pointer types, we are really only interested in asserting
2097 whether the expression evaluates to non-NULL. */
2098 if (range_is_nonnull (&vr0) || range_is_nonnull (&vr1))
2099 set_value_range_to_nonnull (vr, expr_type);
2100 else if (range_is_null (&vr0) && range_is_null (&vr1))
2101 set_value_range_to_null (vr, expr_type);
2102 else
2103 set_value_range_to_varying (vr);
2105 else if (code == BIT_AND_EXPR)
2107 /* For pointer types, we are really only interested in asserting
2108 whether the expression evaluates to non-NULL. */
2109 if (range_is_nonnull (&vr0) && range_is_nonnull (&vr1))
2110 set_value_range_to_nonnull (vr, expr_type);
2111 else if (range_is_null (&vr0) || range_is_null (&vr1))
2112 set_value_range_to_null (vr, expr_type);
2113 else
2114 set_value_range_to_varying (vr);
2116 else
2117 set_value_range_to_varying (vr);
2119 return;
2122 /* For integer ranges, apply the operation to each end of the
2123 range and see what we end up with. */
2124 if (code == PLUS_EXPR || code == MINUS_EXPR)
2126 const bool minus_p = (code == MINUS_EXPR);
2127 tree min_op0 = vr0.min;
2128 tree min_op1 = minus_p ? vr1.max : vr1.min;
2129 tree max_op0 = vr0.max;
2130 tree max_op1 = minus_p ? vr1.min : vr1.max;
2131 tree sym_min_op0 = NULL_TREE;
2132 tree sym_min_op1 = NULL_TREE;
2133 tree sym_max_op0 = NULL_TREE;
2134 tree sym_max_op1 = NULL_TREE;
2135 bool neg_min_op0, neg_min_op1, neg_max_op0, neg_max_op1;
2137 /* If we have a PLUS or MINUS with two VR_RANGEs, either constant or
2138 single-symbolic ranges, try to compute the precise resulting range,
2139 but only if we know that this resulting range will also be constant
2140 or single-symbolic. */
2141 if (vr0.type == VR_RANGE && vr1.type == VR_RANGE
2142 && (TREE_CODE (min_op0) == INTEGER_CST
2143 || (sym_min_op0
2144 = get_single_symbol (min_op0, &neg_min_op0, &min_op0)))
2145 && (TREE_CODE (min_op1) == INTEGER_CST
2146 || (sym_min_op1
2147 = get_single_symbol (min_op1, &neg_min_op1, &min_op1)))
2148 && (!(sym_min_op0 && sym_min_op1)
2149 || (sym_min_op0 == sym_min_op1
2150 && neg_min_op0 == (minus_p ? neg_min_op1 : !neg_min_op1)))
2151 && (TREE_CODE (max_op0) == INTEGER_CST
2152 || (sym_max_op0
2153 = get_single_symbol (max_op0, &neg_max_op0, &max_op0)))
2154 && (TREE_CODE (max_op1) == INTEGER_CST
2155 || (sym_max_op1
2156 = get_single_symbol (max_op1, &neg_max_op1, &max_op1)))
2157 && (!(sym_max_op0 && sym_max_op1)
2158 || (sym_max_op0 == sym_max_op1
2159 && neg_max_op0 == (minus_p ? neg_max_op1 : !neg_max_op1))))
2161 const signop sgn = TYPE_SIGN (expr_type);
2162 const unsigned int prec = TYPE_PRECISION (expr_type);
2163 wide_int type_min, type_max, wmin, wmax;
2164 int min_ovf = 0;
2165 int max_ovf = 0;
2167 /* Get the lower and upper bounds of the type. */
2168 if (TYPE_OVERFLOW_WRAPS (expr_type))
2170 type_min = wi::min_value (prec, sgn);
2171 type_max = wi::max_value (prec, sgn);
2173 else
2175 type_min = vrp_val_min (expr_type);
2176 type_max = vrp_val_max (expr_type);
2179 /* Combine the lower bounds, if any. */
2180 if (min_op0 && min_op1)
2182 if (minus_p)
2184 wmin = wi::sub (min_op0, min_op1);
2186 /* Check for overflow. */
2187 if (wi::cmp (0, min_op1, sgn)
2188 != wi::cmp (wmin, min_op0, sgn))
2189 min_ovf = wi::cmp (min_op0, min_op1, sgn);
2191 else
2193 wmin = wi::add (min_op0, min_op1);
2195 /* Check for overflow. */
2196 if (wi::cmp (min_op1, 0, sgn)
2197 != wi::cmp (wmin, min_op0, sgn))
2198 min_ovf = wi::cmp (min_op0, wmin, sgn);
2201 else if (min_op0)
2202 wmin = min_op0;
2203 else if (min_op1)
2205 if (minus_p)
2207 wmin = wi::neg (min_op1);
2209 /* Check for overflow. */
2210 if (sgn == SIGNED && wi::neg_p (min_op1) && wi::neg_p (wmin))
2211 min_ovf = 1;
2212 else if (sgn == UNSIGNED && wi::ne_p (min_op1, 0))
2213 min_ovf = -1;
2215 else
2216 wmin = min_op1;
2218 else
2219 wmin = wi::shwi (0, prec);
2221 /* Combine the upper bounds, if any. */
2222 if (max_op0 && max_op1)
2224 if (minus_p)
2226 wmax = wi::sub (max_op0, max_op1);
2228 /* Check for overflow. */
2229 if (wi::cmp (0, max_op1, sgn)
2230 != wi::cmp (wmax, max_op0, sgn))
2231 max_ovf = wi::cmp (max_op0, max_op1, sgn);
2233 else
2235 wmax = wi::add (max_op0, max_op1);
2237 if (wi::cmp (max_op1, 0, sgn)
2238 != wi::cmp (wmax, max_op0, sgn))
2239 max_ovf = wi::cmp (max_op0, wmax, sgn);
2242 else if (max_op0)
2243 wmax = max_op0;
2244 else if (max_op1)
2246 if (minus_p)
2248 wmax = wi::neg (max_op1);
2250 /* Check for overflow. */
2251 if (sgn == SIGNED && wi::neg_p (max_op1) && wi::neg_p (wmax))
2252 max_ovf = 1;
2253 else if (sgn == UNSIGNED && wi::ne_p (max_op1, 0))
2254 max_ovf = -1;
2256 else
2257 wmax = max_op1;
2259 else
2260 wmax = wi::shwi (0, prec);
2262 /* Check for type overflow. */
2263 if (min_ovf == 0)
2265 if (wi::cmp (wmin, type_min, sgn) == -1)
2266 min_ovf = -1;
2267 else if (wi::cmp (wmin, type_max, sgn) == 1)
2268 min_ovf = 1;
2270 if (max_ovf == 0)
2272 if (wi::cmp (wmax, type_min, sgn) == -1)
2273 max_ovf = -1;
2274 else if (wi::cmp (wmax, type_max, sgn) == 1)
2275 max_ovf = 1;
2278 /* If we have overflow for the constant part and the resulting
2279 range will be symbolic, drop to VR_VARYING. */
2280 if ((min_ovf && sym_min_op0 != sym_min_op1)
2281 || (max_ovf && sym_max_op0 != sym_max_op1))
2283 set_value_range_to_varying (vr);
2284 return;
2287 if (TYPE_OVERFLOW_WRAPS (expr_type))
2289 /* If overflow wraps, truncate the values and adjust the
2290 range kind and bounds appropriately. */
2291 wide_int tmin = wide_int::from (wmin, prec, sgn);
2292 wide_int tmax = wide_int::from (wmax, prec, sgn);
2293 if (min_ovf == max_ovf)
2295 /* No overflow or both overflow or underflow. The
2296 range kind stays VR_RANGE. */
2297 min = wide_int_to_tree (expr_type, tmin);
2298 max = wide_int_to_tree (expr_type, tmax);
2300 else if ((min_ovf == -1 && max_ovf == 0)
2301 || (max_ovf == 1 && min_ovf == 0))
2303 /* Min underflow or max overflow. The range kind
2304 changes to VR_ANTI_RANGE. */
2305 bool covers = false;
2306 wide_int tem = tmin;
2307 type = VR_ANTI_RANGE;
2308 tmin = tmax + 1;
2309 if (wi::cmp (tmin, tmax, sgn) < 0)
2310 covers = true;
2311 tmax = tem - 1;
2312 if (wi::cmp (tmax, tem, sgn) > 0)
2313 covers = true;
2314 /* If the anti-range would cover nothing, drop to varying.
2315 Likewise if the anti-range bounds are outside of the
2316 types values. */
2317 if (covers || wi::cmp (tmin, tmax, sgn) > 0)
2319 set_value_range_to_varying (vr);
2320 return;
2322 min = wide_int_to_tree (expr_type, tmin);
2323 max = wide_int_to_tree (expr_type, tmax);
2325 else
2327 /* Other underflow and/or overflow, drop to VR_VARYING. */
2328 set_value_range_to_varying (vr);
2329 return;
2332 else
2334 /* If overflow does not wrap, saturate to the types min/max
2335 value. */
2336 if (min_ovf == -1)
2337 min = wide_int_to_tree (expr_type, type_min);
2338 else if (min_ovf == 1)
2339 min = wide_int_to_tree (expr_type, type_max);
2340 else
2341 min = wide_int_to_tree (expr_type, wmin);
2343 if (max_ovf == -1)
2344 max = wide_int_to_tree (expr_type, type_min);
2345 else if (max_ovf == 1)
2346 max = wide_int_to_tree (expr_type, type_max);
2347 else
2348 max = wide_int_to_tree (expr_type, wmax);
2351 /* If the result lower bound is constant, we're done;
2352 otherwise, build the symbolic lower bound. */
2353 if (sym_min_op0 == sym_min_op1)
2355 else if (sym_min_op0)
2356 min = build_symbolic_expr (expr_type, sym_min_op0,
2357 neg_min_op0, min);
2358 else if (sym_min_op1)
2360 /* We may not negate if that might introduce
2361 undefined overflow. */
2362 if (! minus_p
2363 || neg_min_op1
2364 || TYPE_OVERFLOW_WRAPS (expr_type))
2365 min = build_symbolic_expr (expr_type, sym_min_op1,
2366 neg_min_op1 ^ minus_p, min);
2367 else
2368 min = NULL_TREE;
2371 /* Likewise for the upper bound. */
2372 if (sym_max_op0 == sym_max_op1)
2374 else if (sym_max_op0)
2375 max = build_symbolic_expr (expr_type, sym_max_op0,
2376 neg_max_op0, max);
2377 else if (sym_max_op1)
2379 /* We may not negate if that might introduce
2380 undefined overflow. */
2381 if (! minus_p
2382 || neg_max_op1
2383 || TYPE_OVERFLOW_WRAPS (expr_type))
2384 max = build_symbolic_expr (expr_type, sym_max_op1,
2385 neg_max_op1 ^ minus_p, max);
2386 else
2387 max = NULL_TREE;
2390 else
2392 /* For other cases, for example if we have a PLUS_EXPR with two
2393 VR_ANTI_RANGEs, drop to VR_VARYING. It would take more effort
2394 to compute a precise range for such a case.
2395 ??? General even mixed range kind operations can be expressed
2396 by for example transforming ~[3, 5] + [1, 2] to range-only
2397 operations and a union primitive:
2398 [-INF, 2] + [1, 2] U [5, +INF] + [1, 2]
2399 [-INF+1, 4] U [6, +INF(OVF)]
2400 though usually the union is not exactly representable with
2401 a single range or anti-range as the above is
2402 [-INF+1, +INF(OVF)] intersected with ~[5, 5]
2403 but one could use a scheme similar to equivalences for this. */
2404 set_value_range_to_varying (vr);
2405 return;
2408 else if (code == MIN_EXPR
2409 || code == MAX_EXPR)
2411 if (vr0.type == VR_RANGE
2412 && !symbolic_range_p (&vr0))
2414 type = VR_RANGE;
2415 if (vr1.type == VR_RANGE
2416 && !symbolic_range_p (&vr1))
2418 /* For operations that make the resulting range directly
2419 proportional to the original ranges, apply the operation to
2420 the same end of each range. */
2421 min = int_const_binop (code, vr0.min, vr1.min);
2422 max = int_const_binop (code, vr0.max, vr1.max);
2424 else if (code == MIN_EXPR)
2426 min = vrp_val_min (expr_type);
2427 max = vr0.max;
2429 else if (code == MAX_EXPR)
2431 min = vr0.min;
2432 max = vrp_val_max (expr_type);
2435 else if (vr1.type == VR_RANGE
2436 && !symbolic_range_p (&vr1))
2438 type = VR_RANGE;
2439 if (code == MIN_EXPR)
2441 min = vrp_val_min (expr_type);
2442 max = vr1.max;
2444 else if (code == MAX_EXPR)
2446 min = vr1.min;
2447 max = vrp_val_max (expr_type);
2450 else
2452 set_value_range_to_varying (vr);
2453 return;
2456 else if (code == MULT_EXPR)
2458 /* Fancy code so that with unsigned, [-3,-1]*[-3,-1] does not
2459 drop to varying. This test requires 2*prec bits if both
2460 operands are signed and 2*prec + 2 bits if either is not. */
2462 signop sign = TYPE_SIGN (expr_type);
2463 unsigned int prec = TYPE_PRECISION (expr_type);
2465 if (range_int_cst_p (&vr0)
2466 && range_int_cst_p (&vr1)
2467 && TYPE_OVERFLOW_WRAPS (expr_type))
2469 typedef FIXED_WIDE_INT (WIDE_INT_MAX_PRECISION * 2) vrp_int;
2470 typedef generic_wide_int
2471 <wi::extended_tree <WIDE_INT_MAX_PRECISION * 2> > vrp_int_cst;
2472 vrp_int sizem1 = wi::mask <vrp_int> (prec, false);
2473 vrp_int size = sizem1 + 1;
2475 /* Extend the values using the sign of the result to PREC2.
2476 From here on out, everthing is just signed math no matter
2477 what the input types were. */
2478 vrp_int min0 = vrp_int_cst (vr0.min);
2479 vrp_int max0 = vrp_int_cst (vr0.max);
2480 vrp_int min1 = vrp_int_cst (vr1.min);
2481 vrp_int max1 = vrp_int_cst (vr1.max);
2482 /* Canonicalize the intervals. */
2483 if (sign == UNSIGNED)
2485 if (wi::ltu_p (size, min0 + max0))
2487 min0 -= size;
2488 max0 -= size;
2491 if (wi::ltu_p (size, min1 + max1))
2493 min1 -= size;
2494 max1 -= size;
2498 vrp_int prod0 = min0 * min1;
2499 vrp_int prod1 = min0 * max1;
2500 vrp_int prod2 = max0 * min1;
2501 vrp_int prod3 = max0 * max1;
2503 /* Sort the 4 products so that min is in prod0 and max is in
2504 prod3. */
2505 /* min0min1 > max0max1 */
2506 if (prod0 > prod3)
2507 std::swap (prod0, prod3);
2509 /* min0max1 > max0min1 */
2510 if (prod1 > prod2)
2511 std::swap (prod1, prod2);
2513 if (prod0 > prod1)
2514 std::swap (prod0, prod1);
2516 if (prod2 > prod3)
2517 std::swap (prod2, prod3);
2519 /* diff = max - min. */
2520 prod2 = prod3 - prod0;
2521 if (wi::geu_p (prod2, sizem1))
2523 /* the range covers all values. */
2524 set_value_range_to_varying (vr);
2525 return;
2528 /* The following should handle the wrapping and selecting
2529 VR_ANTI_RANGE for us. */
2530 min = wide_int_to_tree (expr_type, prod0);
2531 max = wide_int_to_tree (expr_type, prod3);
2532 set_and_canonicalize_value_range (vr, VR_RANGE, min, max, NULL);
2533 return;
2536 /* If we have an unsigned MULT_EXPR with two VR_ANTI_RANGEs,
2537 drop to VR_VARYING. It would take more effort to compute a
2538 precise range for such a case. For example, if we have
2539 op0 == 65536 and op1 == 65536 with their ranges both being
2540 ~[0,0] on a 32-bit machine, we would have op0 * op1 == 0, so
2541 we cannot claim that the product is in ~[0,0]. Note that we
2542 are guaranteed to have vr0.type == vr1.type at this
2543 point. */
2544 if (vr0.type == VR_ANTI_RANGE
2545 && !TYPE_OVERFLOW_UNDEFINED (expr_type))
2547 set_value_range_to_varying (vr);
2548 return;
2551 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
2552 return;
2554 else if (code == RSHIFT_EXPR
2555 || code == LSHIFT_EXPR)
2557 /* If we have a RSHIFT_EXPR with any shift values outside [0..prec-1],
2558 then drop to VR_VARYING. Outside of this range we get undefined
2559 behavior from the shift operation. We cannot even trust
2560 SHIFT_COUNT_TRUNCATED at this stage, because that applies to rtl
2561 shifts, and the operation at the tree level may be widened. */
2562 if (range_int_cst_p (&vr1)
2563 && compare_tree_int (vr1.min, 0) >= 0
2564 && compare_tree_int (vr1.max, TYPE_PRECISION (expr_type)) == -1)
2566 if (code == RSHIFT_EXPR)
2568 /* Even if vr0 is VARYING or otherwise not usable, we can derive
2569 useful ranges just from the shift count. E.g.
2570 x >> 63 for signed 64-bit x is always [-1, 0]. */
2571 if (vr0.type != VR_RANGE || symbolic_range_p (&vr0))
2573 vr0.type = type = VR_RANGE;
2574 vr0.min = vrp_val_min (expr_type);
2575 vr0.max = vrp_val_max (expr_type);
2577 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
2578 return;
2580 /* We can map lshifts by constants to MULT_EXPR handling. */
2581 else if (code == LSHIFT_EXPR
2582 && range_int_cst_singleton_p (&vr1))
2584 bool saved_flag_wrapv;
2585 value_range vr1p = VR_INITIALIZER;
2586 vr1p.type = VR_RANGE;
2587 vr1p.min = (wide_int_to_tree
2588 (expr_type,
2589 wi::set_bit_in_zero (tree_to_shwi (vr1.min),
2590 TYPE_PRECISION (expr_type))));
2591 vr1p.max = vr1p.min;
2592 /* We have to use a wrapping multiply though as signed overflow
2593 on lshifts is implementation defined in C89. */
2594 saved_flag_wrapv = flag_wrapv;
2595 flag_wrapv = 1;
2596 extract_range_from_binary_expr_1 (vr, MULT_EXPR, expr_type,
2597 &vr0, &vr1p);
2598 flag_wrapv = saved_flag_wrapv;
2599 return;
2601 else if (code == LSHIFT_EXPR
2602 && range_int_cst_p (&vr0))
2604 int prec = TYPE_PRECISION (expr_type);
2605 int overflow_pos = prec;
2606 int bound_shift;
2607 wide_int low_bound, high_bound;
2608 bool uns = TYPE_UNSIGNED (expr_type);
2609 bool in_bounds = false;
2611 if (!uns)
2612 overflow_pos -= 1;
2614 bound_shift = overflow_pos - tree_to_shwi (vr1.max);
2615 /* If bound_shift == HOST_BITS_PER_WIDE_INT, the llshift can
2616 overflow. However, for that to happen, vr1.max needs to be
2617 zero, which means vr1 is a singleton range of zero, which
2618 means it should be handled by the previous LSHIFT_EXPR
2619 if-clause. */
2620 wide_int bound = wi::set_bit_in_zero (bound_shift, prec);
2621 wide_int complement = ~(bound - 1);
2623 if (uns)
2625 low_bound = bound;
2626 high_bound = complement;
2627 if (wi::ltu_p (vr0.max, low_bound))
2629 /* [5, 6] << [1, 2] == [10, 24]. */
2630 /* We're shifting out only zeroes, the value increases
2631 monotonically. */
2632 in_bounds = true;
2634 else if (wi::ltu_p (high_bound, vr0.min))
2636 /* [0xffffff00, 0xffffffff] << [1, 2]
2637 == [0xfffffc00, 0xfffffffe]. */
2638 /* We're shifting out only ones, the value decreases
2639 monotonically. */
2640 in_bounds = true;
2643 else
2645 /* [-1, 1] << [1, 2] == [-4, 4]. */
2646 low_bound = complement;
2647 high_bound = bound;
2648 if (wi::lts_p (vr0.max, high_bound)
2649 && wi::lts_p (low_bound, vr0.min))
2651 /* For non-negative numbers, we're shifting out only
2652 zeroes, the value increases monotonically.
2653 For negative numbers, we're shifting out only ones, the
2654 value decreases monotomically. */
2655 in_bounds = true;
2659 if (in_bounds)
2661 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
2662 return;
2666 set_value_range_to_varying (vr);
2667 return;
2669 else if (code == TRUNC_DIV_EXPR
2670 || code == FLOOR_DIV_EXPR
2671 || code == CEIL_DIV_EXPR
2672 || code == EXACT_DIV_EXPR
2673 || code == ROUND_DIV_EXPR)
2675 if (vr0.type != VR_RANGE || symbolic_range_p (&vr0))
2677 /* For division, if op1 has VR_RANGE but op0 does not, something
2678 can be deduced just from that range. Say [min, max] / [4, max]
2679 gives [min / 4, max / 4] range. */
2680 if (vr1.type == VR_RANGE
2681 && !symbolic_range_p (&vr1)
2682 && range_includes_zero_p (vr1.min, vr1.max) == 0)
2684 vr0.type = type = VR_RANGE;
2685 vr0.min = vrp_val_min (expr_type);
2686 vr0.max = vrp_val_max (expr_type);
2688 else
2690 set_value_range_to_varying (vr);
2691 return;
2695 /* For divisions, if flag_non_call_exceptions is true, we must
2696 not eliminate a division by zero. */
2697 if (cfun->can_throw_non_call_exceptions
2698 && (vr1.type != VR_RANGE
2699 || range_includes_zero_p (vr1.min, vr1.max) != 0))
2701 set_value_range_to_varying (vr);
2702 return;
2705 /* For divisions, if op0 is VR_RANGE, we can deduce a range
2706 even if op1 is VR_VARYING, VR_ANTI_RANGE, symbolic or can
2707 include 0. */
2708 if (vr0.type == VR_RANGE
2709 && (vr1.type != VR_RANGE
2710 || range_includes_zero_p (vr1.min, vr1.max) != 0))
2712 tree zero = build_int_cst (TREE_TYPE (vr0.min), 0);
2713 int cmp;
2715 min = NULL_TREE;
2716 max = NULL_TREE;
2717 if (TYPE_UNSIGNED (expr_type)
2718 || value_range_nonnegative_p (&vr1))
2720 /* For unsigned division or when divisor is known
2721 to be non-negative, the range has to cover
2722 all numbers from 0 to max for positive max
2723 and all numbers from min to 0 for negative min. */
2724 cmp = compare_values (vr0.max, zero);
2725 if (cmp == -1)
2727 /* When vr0.max < 0, vr1.min != 0 and value
2728 ranges for dividend and divisor are available. */
2729 if (vr1.type == VR_RANGE
2730 && !symbolic_range_p (&vr0)
2731 && !symbolic_range_p (&vr1)
2732 && compare_values (vr1.min, zero) != 0)
2733 max = int_const_binop (code, vr0.max, vr1.min);
2734 else
2735 max = zero;
2737 else if (cmp == 0 || cmp == 1)
2738 max = vr0.max;
2739 else
2740 type = VR_VARYING;
2741 cmp = compare_values (vr0.min, zero);
2742 if (cmp == 1)
2744 /* For unsigned division when value ranges for dividend
2745 and divisor are available. */
2746 if (vr1.type == VR_RANGE
2747 && !symbolic_range_p (&vr0)
2748 && !symbolic_range_p (&vr1)
2749 && compare_values (vr1.max, zero) != 0)
2750 min = int_const_binop (code, vr0.min, vr1.max);
2751 else
2752 min = zero;
2754 else if (cmp == 0 || cmp == -1)
2755 min = vr0.min;
2756 else
2757 type = VR_VARYING;
2759 else
2761 /* Otherwise the range is -max .. max or min .. -min
2762 depending on which bound is bigger in absolute value,
2763 as the division can change the sign. */
2764 abs_extent_range (vr, vr0.min, vr0.max);
2765 return;
2767 if (type == VR_VARYING)
2769 set_value_range_to_varying (vr);
2770 return;
2773 else if (!symbolic_range_p (&vr0) && !symbolic_range_p (&vr1))
2775 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
2776 return;
2779 else if (code == TRUNC_MOD_EXPR)
2781 if (range_is_null (&vr1))
2783 set_value_range_to_undefined (vr);
2784 return;
2786 /* ABS (A % B) < ABS (B) and either
2787 0 <= A % B <= A or A <= A % B <= 0. */
2788 type = VR_RANGE;
2789 signop sgn = TYPE_SIGN (expr_type);
2790 unsigned int prec = TYPE_PRECISION (expr_type);
2791 wide_int wmin, wmax, tmp;
2792 wide_int zero = wi::zero (prec);
2793 wide_int one = wi::one (prec);
2794 if (vr1.type == VR_RANGE && !symbolic_range_p (&vr1))
2796 wmax = wi::sub (vr1.max, one);
2797 if (sgn == SIGNED)
2799 tmp = wi::sub (wi::minus_one (prec), vr1.min);
2800 wmax = wi::smax (wmax, tmp);
2803 else
2805 wmax = wi::max_value (prec, sgn);
2806 /* X % INT_MIN may be INT_MAX. */
2807 if (sgn == UNSIGNED)
2808 wmax = wmax - one;
2811 if (sgn == UNSIGNED)
2812 wmin = zero;
2813 else
2815 wmin = -wmax;
2816 if (vr0.type == VR_RANGE && TREE_CODE (vr0.min) == INTEGER_CST)
2818 tmp = vr0.min;
2819 if (wi::gts_p (tmp, zero))
2820 tmp = zero;
2821 wmin = wi::smax (wmin, tmp);
2825 if (vr0.type == VR_RANGE && TREE_CODE (vr0.max) == INTEGER_CST)
2827 tmp = vr0.max;
2828 if (sgn == SIGNED && wi::neg_p (tmp))
2829 tmp = zero;
2830 wmax = wi::min (wmax, tmp, sgn);
2833 min = wide_int_to_tree (expr_type, wmin);
2834 max = wide_int_to_tree (expr_type, wmax);
2836 else if (code == BIT_AND_EXPR || code == BIT_IOR_EXPR || code == BIT_XOR_EXPR)
2838 bool int_cst_range0, int_cst_range1;
2839 wide_int may_be_nonzero0, may_be_nonzero1;
2840 wide_int must_be_nonzero0, must_be_nonzero1;
2842 int_cst_range0 = zero_nonzero_bits_from_vr (expr_type, &vr0,
2843 &may_be_nonzero0,
2844 &must_be_nonzero0);
2845 int_cst_range1 = zero_nonzero_bits_from_vr (expr_type, &vr1,
2846 &may_be_nonzero1,
2847 &must_be_nonzero1);
2849 if (code == BIT_AND_EXPR || code == BIT_IOR_EXPR)
2851 value_range *vr0p = NULL, *vr1p = NULL;
2852 if (range_int_cst_singleton_p (&vr1))
2854 vr0p = &vr0;
2855 vr1p = &vr1;
2857 else if (range_int_cst_singleton_p (&vr0))
2859 vr0p = &vr1;
2860 vr1p = &vr0;
2862 /* For op & or | attempt to optimize:
2863 [x, y] op z into [x op z, y op z]
2864 if z is a constant which (for op | its bitwise not) has n
2865 consecutive least significant bits cleared followed by m 1
2866 consecutive bits set immediately above it and either
2867 m + n == precision, or (x >> (m + n)) == (y >> (m + n)).
2868 The least significant n bits of all the values in the range are
2869 cleared or set, the m bits above it are preserved and any bits
2870 above these are required to be the same for all values in the
2871 range. */
2872 if (vr0p && range_int_cst_p (vr0p))
2874 wide_int w = vr1p->min;
2875 int m = 0, n = 0;
2876 if (code == BIT_IOR_EXPR)
2877 w = ~w;
2878 if (wi::eq_p (w, 0))
2879 n = TYPE_PRECISION (expr_type);
2880 else
2882 n = wi::ctz (w);
2883 w = ~(w | wi::mask (n, false, w.get_precision ()));
2884 if (wi::eq_p (w, 0))
2885 m = TYPE_PRECISION (expr_type) - n;
2886 else
2887 m = wi::ctz (w) - n;
2889 wide_int mask = wi::mask (m + n, true, w.get_precision ());
2890 if (wi::eq_p (mask & vr0p->min, mask & vr0p->max))
2892 min = int_const_binop (code, vr0p->min, vr1p->min);
2893 max = int_const_binop (code, vr0p->max, vr1p->min);
2898 type = VR_RANGE;
2899 if (min && max)
2900 /* Optimized above already. */;
2901 else if (code == BIT_AND_EXPR)
2903 min = wide_int_to_tree (expr_type,
2904 must_be_nonzero0 & must_be_nonzero1);
2905 wide_int wmax = may_be_nonzero0 & may_be_nonzero1;
2906 /* If both input ranges contain only negative values we can
2907 truncate the result range maximum to the minimum of the
2908 input range maxima. */
2909 if (int_cst_range0 && int_cst_range1
2910 && tree_int_cst_sgn (vr0.max) < 0
2911 && tree_int_cst_sgn (vr1.max) < 0)
2913 wmax = wi::min (wmax, vr0.max, TYPE_SIGN (expr_type));
2914 wmax = wi::min (wmax, vr1.max, TYPE_SIGN (expr_type));
2916 /* If either input range contains only non-negative values
2917 we can truncate the result range maximum to the respective
2918 maximum of the input range. */
2919 if (int_cst_range0 && tree_int_cst_sgn (vr0.min) >= 0)
2920 wmax = wi::min (wmax, vr0.max, TYPE_SIGN (expr_type));
2921 if (int_cst_range1 && tree_int_cst_sgn (vr1.min) >= 0)
2922 wmax = wi::min (wmax, vr1.max, TYPE_SIGN (expr_type));
2923 max = wide_int_to_tree (expr_type, wmax);
2924 cmp = compare_values (min, max);
2925 /* PR68217: In case of signed & sign-bit-CST should
2926 result in [-INF, 0] instead of [-INF, INF]. */
2927 if (cmp == -2 || cmp == 1)
2929 wide_int sign_bit
2930 = wi::set_bit_in_zero (TYPE_PRECISION (expr_type) - 1,
2931 TYPE_PRECISION (expr_type));
2932 if (!TYPE_UNSIGNED (expr_type)
2933 && ((value_range_constant_singleton (&vr0)
2934 && !wi::cmps (vr0.min, sign_bit))
2935 || (value_range_constant_singleton (&vr1)
2936 && !wi::cmps (vr1.min, sign_bit))))
2938 min = TYPE_MIN_VALUE (expr_type);
2939 max = build_int_cst (expr_type, 0);
2943 else if (code == BIT_IOR_EXPR)
2945 max = wide_int_to_tree (expr_type,
2946 may_be_nonzero0 | may_be_nonzero1);
2947 wide_int wmin = must_be_nonzero0 | must_be_nonzero1;
2948 /* If the input ranges contain only positive values we can
2949 truncate the minimum of the result range to the maximum
2950 of the input range minima. */
2951 if (int_cst_range0 && int_cst_range1
2952 && tree_int_cst_sgn (vr0.min) >= 0
2953 && tree_int_cst_sgn (vr1.min) >= 0)
2955 wmin = wi::max (wmin, vr0.min, TYPE_SIGN (expr_type));
2956 wmin = wi::max (wmin, vr1.min, TYPE_SIGN (expr_type));
2958 /* If either input range contains only negative values
2959 we can truncate the minimum of the result range to the
2960 respective minimum range. */
2961 if (int_cst_range0 && tree_int_cst_sgn (vr0.max) < 0)
2962 wmin = wi::max (wmin, vr0.min, TYPE_SIGN (expr_type));
2963 if (int_cst_range1 && tree_int_cst_sgn (vr1.max) < 0)
2964 wmin = wi::max (wmin, vr1.min, TYPE_SIGN (expr_type));
2965 min = wide_int_to_tree (expr_type, wmin);
2967 else if (code == BIT_XOR_EXPR)
2969 wide_int result_zero_bits = ((must_be_nonzero0 & must_be_nonzero1)
2970 | ~(may_be_nonzero0 | may_be_nonzero1));
2971 wide_int result_one_bits
2972 = (must_be_nonzero0.and_not (may_be_nonzero1)
2973 | must_be_nonzero1.and_not (may_be_nonzero0));
2974 max = wide_int_to_tree (expr_type, ~result_zero_bits);
2975 min = wide_int_to_tree (expr_type, result_one_bits);
2976 /* If the range has all positive or all negative values the
2977 result is better than VARYING. */
2978 if (tree_int_cst_sgn (min) < 0
2979 || tree_int_cst_sgn (max) >= 0)
2981 else
2982 max = min = NULL_TREE;
2985 else
2986 gcc_unreachable ();
2988 /* If either MIN or MAX overflowed, then set the resulting range to
2989 VARYING. */
2990 if (min == NULL_TREE
2991 || TREE_OVERFLOW_P (min)
2992 || max == NULL_TREE
2993 || TREE_OVERFLOW_P (max))
2995 set_value_range_to_varying (vr);
2996 return;
2999 /* We punt for [-INF, +INF].
3000 We learn nothing when we have INF on both sides.
3001 Note that we do accept [-INF, -INF] and [+INF, +INF]. */
3002 if (vrp_val_is_min (min) && vrp_val_is_max (max))
3004 set_value_range_to_varying (vr);
3005 return;
3008 cmp = compare_values (min, max);
3009 if (cmp == -2 || cmp == 1)
3011 /* If the new range has its limits swapped around (MIN > MAX),
3012 then the operation caused one of them to wrap around, mark
3013 the new range VARYING. */
3014 set_value_range_to_varying (vr);
3016 else
3017 set_value_range (vr, type, min, max, NULL);
3020 /* Extract range information from a binary expression OP0 CODE OP1 based on
3021 the ranges of each of its operands with resulting type EXPR_TYPE.
3022 The resulting range is stored in *VR. */
3024 static void
3025 extract_range_from_binary_expr (value_range *vr,
3026 enum tree_code code,
3027 tree expr_type, tree op0, tree op1)
3029 value_range vr0 = VR_INITIALIZER;
3030 value_range vr1 = VR_INITIALIZER;
3032 /* Get value ranges for each operand. For constant operands, create
3033 a new value range with the operand to simplify processing. */
3034 if (TREE_CODE (op0) == SSA_NAME)
3035 vr0 = *(get_value_range (op0));
3036 else if (is_gimple_min_invariant (op0))
3037 set_value_range_to_value (&vr0, op0, NULL);
3038 else
3039 set_value_range_to_varying (&vr0);
3041 if (TREE_CODE (op1) == SSA_NAME)
3042 vr1 = *(get_value_range (op1));
3043 else if (is_gimple_min_invariant (op1))
3044 set_value_range_to_value (&vr1, op1, NULL);
3045 else
3046 set_value_range_to_varying (&vr1);
3048 extract_range_from_binary_expr_1 (vr, code, expr_type, &vr0, &vr1);
3050 /* Try harder for PLUS and MINUS if the range of one operand is symbolic
3051 and based on the other operand, for example if it was deduced from a
3052 symbolic comparison. When a bound of the range of the first operand
3053 is invariant, we set the corresponding bound of the new range to INF
3054 in order to avoid recursing on the range of the second operand. */
3055 if (vr->type == VR_VARYING
3056 && (code == PLUS_EXPR || code == MINUS_EXPR)
3057 && TREE_CODE (op1) == SSA_NAME
3058 && vr0.type == VR_RANGE
3059 && symbolic_range_based_on_p (&vr0, op1))
3061 const bool minus_p = (code == MINUS_EXPR);
3062 value_range n_vr1 = VR_INITIALIZER;
3064 /* Try with VR0 and [-INF, OP1]. */
3065 if (is_gimple_min_invariant (minus_p ? vr0.max : vr0.min))
3066 set_value_range (&n_vr1, VR_RANGE, vrp_val_min (expr_type), op1, NULL);
3068 /* Try with VR0 and [OP1, +INF]. */
3069 else if (is_gimple_min_invariant (minus_p ? vr0.min : vr0.max))
3070 set_value_range (&n_vr1, VR_RANGE, op1, vrp_val_max (expr_type), NULL);
3072 /* Try with VR0 and [OP1, OP1]. */
3073 else
3074 set_value_range (&n_vr1, VR_RANGE, op1, op1, NULL);
3076 extract_range_from_binary_expr_1 (vr, code, expr_type, &vr0, &n_vr1);
3079 if (vr->type == VR_VARYING
3080 && (code == PLUS_EXPR || code == MINUS_EXPR)
3081 && TREE_CODE (op0) == SSA_NAME
3082 && vr1.type == VR_RANGE
3083 && symbolic_range_based_on_p (&vr1, op0))
3085 const bool minus_p = (code == MINUS_EXPR);
3086 value_range n_vr0 = VR_INITIALIZER;
3088 /* Try with [-INF, OP0] and VR1. */
3089 if (is_gimple_min_invariant (minus_p ? vr1.max : vr1.min))
3090 set_value_range (&n_vr0, VR_RANGE, vrp_val_min (expr_type), op0, NULL);
3092 /* Try with [OP0, +INF] and VR1. */
3093 else if (is_gimple_min_invariant (minus_p ? vr1.min : vr1.max))
3094 set_value_range (&n_vr0, VR_RANGE, op0, vrp_val_max (expr_type), NULL);
3096 /* Try with [OP0, OP0] and VR1. */
3097 else
3098 set_value_range (&n_vr0, VR_RANGE, op0, op0, NULL);
3100 extract_range_from_binary_expr_1 (vr, code, expr_type, &n_vr0, &vr1);
3103 /* If we didn't derive a range for MINUS_EXPR, and
3104 op1's range is ~[op0,op0] or vice-versa, then we
3105 can derive a non-null range. This happens often for
3106 pointer subtraction. */
3107 if (vr->type == VR_VARYING
3108 && code == MINUS_EXPR
3109 && TREE_CODE (op0) == SSA_NAME
3110 && ((vr0.type == VR_ANTI_RANGE
3111 && vr0.min == op1
3112 && vr0.min == vr0.max)
3113 || (vr1.type == VR_ANTI_RANGE
3114 && vr1.min == op0
3115 && vr1.min == vr1.max)))
3116 set_value_range_to_nonnull (vr, TREE_TYPE (op0));
3119 /* Extract range information from a unary operation CODE based on
3120 the range of its operand *VR0 with type OP0_TYPE with resulting type TYPE.
3121 The resulting range is stored in *VR. */
3123 void
3124 extract_range_from_unary_expr (value_range *vr,
3125 enum tree_code code, tree type,
3126 value_range *vr0_, tree op0_type)
3128 value_range vr0 = *vr0_, vrtem0 = VR_INITIALIZER, vrtem1 = VR_INITIALIZER;
3130 /* VRP only operates on integral and pointer types. */
3131 if (!(INTEGRAL_TYPE_P (op0_type)
3132 || POINTER_TYPE_P (op0_type))
3133 || !(INTEGRAL_TYPE_P (type)
3134 || POINTER_TYPE_P (type)))
3136 set_value_range_to_varying (vr);
3137 return;
3140 /* If VR0 is UNDEFINED, so is the result. */
3141 if (vr0.type == VR_UNDEFINED)
3143 set_value_range_to_undefined (vr);
3144 return;
3147 /* Handle operations that we express in terms of others. */
3148 if (code == PAREN_EXPR || code == OBJ_TYPE_REF)
3150 /* PAREN_EXPR and OBJ_TYPE_REF are simple copies. */
3151 copy_value_range (vr, &vr0);
3152 return;
3154 else if (code == NEGATE_EXPR)
3156 /* -X is simply 0 - X, so re-use existing code that also handles
3157 anti-ranges fine. */
3158 value_range zero = VR_INITIALIZER;
3159 set_value_range_to_value (&zero, build_int_cst (type, 0), NULL);
3160 extract_range_from_binary_expr_1 (vr, MINUS_EXPR, type, &zero, &vr0);
3161 return;
3163 else if (code == BIT_NOT_EXPR)
3165 /* ~X is simply -1 - X, so re-use existing code that also handles
3166 anti-ranges fine. */
3167 value_range minusone = VR_INITIALIZER;
3168 set_value_range_to_value (&minusone, build_int_cst (type, -1), NULL);
3169 extract_range_from_binary_expr_1 (vr, MINUS_EXPR,
3170 type, &minusone, &vr0);
3171 return;
3174 /* Now canonicalize anti-ranges to ranges when they are not symbolic
3175 and express op ~[] as (op []') U (op []''). */
3176 if (vr0.type == VR_ANTI_RANGE
3177 && ranges_from_anti_range (&vr0, &vrtem0, &vrtem1))
3179 extract_range_from_unary_expr (vr, code, type, &vrtem0, op0_type);
3180 if (vrtem1.type != VR_UNDEFINED)
3182 value_range vrres = VR_INITIALIZER;
3183 extract_range_from_unary_expr (&vrres, code, type,
3184 &vrtem1, op0_type);
3185 vrp_meet (vr, &vrres);
3187 return;
3190 if (CONVERT_EXPR_CODE_P (code))
3192 tree inner_type = op0_type;
3193 tree outer_type = type;
3195 /* If the expression evaluates to a pointer, we are only interested in
3196 determining if it evaluates to NULL [0, 0] or non-NULL (~[0, 0]). */
3197 if (POINTER_TYPE_P (type))
3199 if (range_is_nonnull (&vr0))
3200 set_value_range_to_nonnull (vr, type);
3201 else if (range_is_null (&vr0))
3202 set_value_range_to_null (vr, type);
3203 else
3204 set_value_range_to_varying (vr);
3205 return;
3208 /* If VR0 is varying and we increase the type precision, assume
3209 a full range for the following transformation. */
3210 if (vr0.type == VR_VARYING
3211 && INTEGRAL_TYPE_P (inner_type)
3212 && TYPE_PRECISION (inner_type) < TYPE_PRECISION (outer_type))
3214 vr0.type = VR_RANGE;
3215 vr0.min = TYPE_MIN_VALUE (inner_type);
3216 vr0.max = TYPE_MAX_VALUE (inner_type);
3219 /* If VR0 is a constant range or anti-range and the conversion is
3220 not truncating we can convert the min and max values and
3221 canonicalize the resulting range. Otherwise we can do the
3222 conversion if the size of the range is less than what the
3223 precision of the target type can represent and the range is
3224 not an anti-range. */
3225 if ((vr0.type == VR_RANGE
3226 || vr0.type == VR_ANTI_RANGE)
3227 && TREE_CODE (vr0.min) == INTEGER_CST
3228 && TREE_CODE (vr0.max) == INTEGER_CST
3229 && (TYPE_PRECISION (outer_type) >= TYPE_PRECISION (inner_type)
3230 || (vr0.type == VR_RANGE
3231 && integer_zerop (int_const_binop (RSHIFT_EXPR,
3232 int_const_binop (MINUS_EXPR, vr0.max, vr0.min),
3233 size_int (TYPE_PRECISION (outer_type)))))))
3235 tree new_min, new_max;
3236 new_min = force_fit_type (outer_type, wi::to_widest (vr0.min),
3237 0, false);
3238 new_max = force_fit_type (outer_type, wi::to_widest (vr0.max),
3239 0, false);
3240 set_and_canonicalize_value_range (vr, vr0.type,
3241 new_min, new_max, NULL);
3242 return;
3245 set_value_range_to_varying (vr);
3246 return;
3248 else if (code == ABS_EXPR)
3250 tree min, max;
3251 int cmp;
3253 /* Pass through vr0 in the easy cases. */
3254 if (TYPE_UNSIGNED (type)
3255 || value_range_nonnegative_p (&vr0))
3257 copy_value_range (vr, &vr0);
3258 return;
3261 /* For the remaining varying or symbolic ranges we can't do anything
3262 useful. */
3263 if (vr0.type == VR_VARYING
3264 || symbolic_range_p (&vr0))
3266 set_value_range_to_varying (vr);
3267 return;
3270 /* -TYPE_MIN_VALUE = TYPE_MIN_VALUE with flag_wrapv so we can't get a
3271 useful range. */
3272 if (!TYPE_OVERFLOW_UNDEFINED (type)
3273 && ((vr0.type == VR_RANGE
3274 && vrp_val_is_min (vr0.min))
3275 || (vr0.type == VR_ANTI_RANGE
3276 && !vrp_val_is_min (vr0.min))))
3278 set_value_range_to_varying (vr);
3279 return;
3282 /* ABS_EXPR may flip the range around, if the original range
3283 included negative values. */
3284 if (!vrp_val_is_min (vr0.min))
3285 min = fold_unary_to_constant (code, type, vr0.min);
3286 else
3287 min = TYPE_MAX_VALUE (type);
3289 if (!vrp_val_is_min (vr0.max))
3290 max = fold_unary_to_constant (code, type, vr0.max);
3291 else
3292 max = TYPE_MAX_VALUE (type);
3294 cmp = compare_values (min, max);
3296 /* If a VR_ANTI_RANGEs contains zero, then we have
3297 ~[-INF, min(MIN, MAX)]. */
3298 if (vr0.type == VR_ANTI_RANGE)
3300 if (range_includes_zero_p (vr0.min, vr0.max) == 1)
3302 /* Take the lower of the two values. */
3303 if (cmp != 1)
3304 max = min;
3306 /* Create ~[-INF, min (abs(MIN), abs(MAX))]
3307 or ~[-INF + 1, min (abs(MIN), abs(MAX))] when
3308 flag_wrapv is set and the original anti-range doesn't include
3309 TYPE_MIN_VALUE, remember -TYPE_MIN_VALUE = TYPE_MIN_VALUE. */
3310 if (TYPE_OVERFLOW_WRAPS (type))
3312 tree type_min_value = TYPE_MIN_VALUE (type);
3314 min = (vr0.min != type_min_value
3315 ? int_const_binop (PLUS_EXPR, type_min_value,
3316 build_int_cst (TREE_TYPE (type_min_value), 1))
3317 : type_min_value);
3319 else
3320 min = TYPE_MIN_VALUE (type);
3322 else
3324 /* All else has failed, so create the range [0, INF], even for
3325 flag_wrapv since TYPE_MIN_VALUE is in the original
3326 anti-range. */
3327 vr0.type = VR_RANGE;
3328 min = build_int_cst (type, 0);
3329 max = TYPE_MAX_VALUE (type);
3333 /* If the range contains zero then we know that the minimum value in the
3334 range will be zero. */
3335 else if (range_includes_zero_p (vr0.min, vr0.max) == 1)
3337 if (cmp == 1)
3338 max = min;
3339 min = build_int_cst (type, 0);
3341 else
3343 /* If the range was reversed, swap MIN and MAX. */
3344 if (cmp == 1)
3345 std::swap (min, max);
3348 cmp = compare_values (min, max);
3349 if (cmp == -2 || cmp == 1)
3351 /* If the new range has its limits swapped around (MIN > MAX),
3352 then the operation caused one of them to wrap around, mark
3353 the new range VARYING. */
3354 set_value_range_to_varying (vr);
3356 else
3357 set_value_range (vr, vr0.type, min, max, NULL);
3358 return;
3361 /* For unhandled operations fall back to varying. */
3362 set_value_range_to_varying (vr);
3363 return;
3367 /* Extract range information from a unary expression CODE OP0 based on
3368 the range of its operand with resulting type TYPE.
3369 The resulting range is stored in *VR. */
3371 static void
3372 extract_range_from_unary_expr (value_range *vr, enum tree_code code,
3373 tree type, tree op0)
3375 value_range vr0 = VR_INITIALIZER;
3377 /* Get value ranges for the operand. For constant operands, create
3378 a new value range with the operand to simplify processing. */
3379 if (TREE_CODE (op0) == SSA_NAME)
3380 vr0 = *(get_value_range (op0));
3381 else if (is_gimple_min_invariant (op0))
3382 set_value_range_to_value (&vr0, op0, NULL);
3383 else
3384 set_value_range_to_varying (&vr0);
3386 extract_range_from_unary_expr (vr, code, type, &vr0, TREE_TYPE (op0));
3390 /* Extract range information from a conditional expression STMT based on
3391 the ranges of each of its operands and the expression code. */
3393 static void
3394 extract_range_from_cond_expr (value_range *vr, gassign *stmt)
3396 tree op0, op1;
3397 value_range vr0 = VR_INITIALIZER;
3398 value_range vr1 = VR_INITIALIZER;
3400 /* Get value ranges for each operand. For constant operands, create
3401 a new value range with the operand to simplify processing. */
3402 op0 = gimple_assign_rhs2 (stmt);
3403 if (TREE_CODE (op0) == SSA_NAME)
3404 vr0 = *(get_value_range (op0));
3405 else if (is_gimple_min_invariant (op0))
3406 set_value_range_to_value (&vr0, op0, NULL);
3407 else
3408 set_value_range_to_varying (&vr0);
3410 op1 = gimple_assign_rhs3 (stmt);
3411 if (TREE_CODE (op1) == SSA_NAME)
3412 vr1 = *(get_value_range (op1));
3413 else if (is_gimple_min_invariant (op1))
3414 set_value_range_to_value (&vr1, op1, NULL);
3415 else
3416 set_value_range_to_varying (&vr1);
3418 /* The resulting value range is the union of the operand ranges */
3419 copy_value_range (vr, &vr0);
3420 vrp_meet (vr, &vr1);
3424 /* Extract range information from a comparison expression EXPR based
3425 on the range of its operand and the expression code. */
3427 static void
3428 extract_range_from_comparison (value_range *vr, enum tree_code code,
3429 tree type, tree op0, tree op1)
3431 bool sop;
3432 tree val;
3434 val = vrp_evaluate_conditional_warnv_with_ops (code, op0, op1, false, &sop,
3435 NULL);
3436 if (val)
3438 /* Since this expression was found on the RHS of an assignment,
3439 its type may be different from _Bool. Convert VAL to EXPR's
3440 type. */
3441 val = fold_convert (type, val);
3442 if (is_gimple_min_invariant (val))
3443 set_value_range_to_value (vr, val, vr->equiv);
3444 else
3445 set_value_range (vr, VR_RANGE, val, val, vr->equiv);
3447 else
3448 /* The result of a comparison is always true or false. */
3449 set_value_range_to_truthvalue (vr, type);
3452 /* Helper function for simplify_internal_call_using_ranges and
3453 extract_range_basic. Return true if OP0 SUBCODE OP1 for
3454 SUBCODE {PLUS,MINUS,MULT}_EXPR is known to never overflow or
3455 always overflow. Set *OVF to true if it is known to always
3456 overflow. */
3458 static bool
3459 check_for_binary_op_overflow (enum tree_code subcode, tree type,
3460 tree op0, tree op1, bool *ovf)
3462 value_range vr0 = VR_INITIALIZER;
3463 value_range vr1 = VR_INITIALIZER;
3464 if (TREE_CODE (op0) == SSA_NAME)
3465 vr0 = *get_value_range (op0);
3466 else if (TREE_CODE (op0) == INTEGER_CST)
3467 set_value_range_to_value (&vr0, op0, NULL);
3468 else
3469 set_value_range_to_varying (&vr0);
3471 if (TREE_CODE (op1) == SSA_NAME)
3472 vr1 = *get_value_range (op1);
3473 else if (TREE_CODE (op1) == INTEGER_CST)
3474 set_value_range_to_value (&vr1, op1, NULL);
3475 else
3476 set_value_range_to_varying (&vr1);
3478 if (!range_int_cst_p (&vr0)
3479 || TREE_OVERFLOW (vr0.min)
3480 || TREE_OVERFLOW (vr0.max))
3482 vr0.min = vrp_val_min (TREE_TYPE (op0));
3483 vr0.max = vrp_val_max (TREE_TYPE (op0));
3485 if (!range_int_cst_p (&vr1)
3486 || TREE_OVERFLOW (vr1.min)
3487 || TREE_OVERFLOW (vr1.max))
3489 vr1.min = vrp_val_min (TREE_TYPE (op1));
3490 vr1.max = vrp_val_max (TREE_TYPE (op1));
3492 *ovf = arith_overflowed_p (subcode, type, vr0.min,
3493 subcode == MINUS_EXPR ? vr1.max : vr1.min);
3494 if (arith_overflowed_p (subcode, type, vr0.max,
3495 subcode == MINUS_EXPR ? vr1.min : vr1.max) != *ovf)
3496 return false;
3497 if (subcode == MULT_EXPR)
3499 if (arith_overflowed_p (subcode, type, vr0.min, vr1.max) != *ovf
3500 || arith_overflowed_p (subcode, type, vr0.max, vr1.min) != *ovf)
3501 return false;
3503 if (*ovf)
3505 /* So far we found that there is an overflow on the boundaries.
3506 That doesn't prove that there is an overflow even for all values
3507 in between the boundaries. For that compute widest_int range
3508 of the result and see if it doesn't overlap the range of
3509 type. */
3510 widest_int wmin, wmax;
3511 widest_int w[4];
3512 int i;
3513 w[0] = wi::to_widest (vr0.min);
3514 w[1] = wi::to_widest (vr0.max);
3515 w[2] = wi::to_widest (vr1.min);
3516 w[3] = wi::to_widest (vr1.max);
3517 for (i = 0; i < 4; i++)
3519 widest_int wt;
3520 switch (subcode)
3522 case PLUS_EXPR:
3523 wt = wi::add (w[i & 1], w[2 + (i & 2) / 2]);
3524 break;
3525 case MINUS_EXPR:
3526 wt = wi::sub (w[i & 1], w[2 + (i & 2) / 2]);
3527 break;
3528 case MULT_EXPR:
3529 wt = wi::mul (w[i & 1], w[2 + (i & 2) / 2]);
3530 break;
3531 default:
3532 gcc_unreachable ();
3534 if (i == 0)
3536 wmin = wt;
3537 wmax = wt;
3539 else
3541 wmin = wi::smin (wmin, wt);
3542 wmax = wi::smax (wmax, wt);
3545 /* The result of op0 CODE op1 is known to be in range
3546 [wmin, wmax]. */
3547 widest_int wtmin = wi::to_widest (vrp_val_min (type));
3548 widest_int wtmax = wi::to_widest (vrp_val_max (type));
3549 /* If all values in [wmin, wmax] are smaller than
3550 [wtmin, wtmax] or all are larger than [wtmin, wtmax],
3551 the arithmetic operation will always overflow. */
3552 if (wmax < wtmin || wmin > wtmax)
3553 return true;
3554 return false;
3556 return true;
3559 /* Try to derive a nonnegative or nonzero range out of STMT relying
3560 primarily on generic routines in fold in conjunction with range data.
3561 Store the result in *VR */
3563 static void
3564 extract_range_basic (value_range *vr, gimple *stmt)
3566 bool sop;
3567 tree type = gimple_expr_type (stmt);
3569 if (is_gimple_call (stmt))
3571 tree arg;
3572 int mini, maxi, zerov = 0, prec;
3573 enum tree_code subcode = ERROR_MARK;
3574 combined_fn cfn = gimple_call_combined_fn (stmt);
3575 scalar_int_mode mode;
3577 switch (cfn)
3579 case CFN_BUILT_IN_CONSTANT_P:
3580 /* If the call is __builtin_constant_p and the argument is a
3581 function parameter resolve it to false. This avoids bogus
3582 array bound warnings.
3583 ??? We could do this as early as inlining is finished. */
3584 arg = gimple_call_arg (stmt, 0);
3585 if (TREE_CODE (arg) == SSA_NAME
3586 && SSA_NAME_IS_DEFAULT_DEF (arg)
3587 && TREE_CODE (SSA_NAME_VAR (arg)) == PARM_DECL
3588 && cfun->after_inlining)
3590 set_value_range_to_null (vr, type);
3591 return;
3593 break;
3594 /* Both __builtin_ffs* and __builtin_popcount return
3595 [0, prec]. */
3596 CASE_CFN_FFS:
3597 CASE_CFN_POPCOUNT:
3598 arg = gimple_call_arg (stmt, 0);
3599 prec = TYPE_PRECISION (TREE_TYPE (arg));
3600 mini = 0;
3601 maxi = prec;
3602 if (TREE_CODE (arg) == SSA_NAME)
3604 value_range *vr0 = get_value_range (arg);
3605 /* If arg is non-zero, then ffs or popcount
3606 are non-zero. */
3607 if ((vr0->type == VR_RANGE
3608 && range_includes_zero_p (vr0->min, vr0->max) == 0)
3609 || (vr0->type == VR_ANTI_RANGE
3610 && range_includes_zero_p (vr0->min, vr0->max) == 1))
3611 mini = 1;
3612 /* If some high bits are known to be zero,
3613 we can decrease the maximum. */
3614 if (vr0->type == VR_RANGE
3615 && TREE_CODE (vr0->max) == INTEGER_CST
3616 && !operand_less_p (vr0->min,
3617 build_zero_cst (TREE_TYPE (vr0->min))))
3618 maxi = tree_floor_log2 (vr0->max) + 1;
3620 goto bitop_builtin;
3621 /* __builtin_parity* returns [0, 1]. */
3622 CASE_CFN_PARITY:
3623 mini = 0;
3624 maxi = 1;
3625 goto bitop_builtin;
3626 /* __builtin_c[lt]z* return [0, prec-1], except for
3627 when the argument is 0, but that is undefined behavior.
3628 On many targets where the CLZ RTL or optab value is defined
3629 for 0 the value is prec, so include that in the range
3630 by default. */
3631 CASE_CFN_CLZ:
3632 arg = gimple_call_arg (stmt, 0);
3633 prec = TYPE_PRECISION (TREE_TYPE (arg));
3634 mini = 0;
3635 maxi = prec;
3636 mode = SCALAR_INT_TYPE_MODE (TREE_TYPE (arg));
3637 if (optab_handler (clz_optab, mode) != CODE_FOR_nothing
3638 && CLZ_DEFINED_VALUE_AT_ZERO (mode, zerov)
3639 /* Handle only the single common value. */
3640 && zerov != prec)
3641 /* Magic value to give up, unless vr0 proves
3642 arg is non-zero. */
3643 mini = -2;
3644 if (TREE_CODE (arg) == SSA_NAME)
3646 value_range *vr0 = get_value_range (arg);
3647 /* From clz of VR_RANGE minimum we can compute
3648 result maximum. */
3649 if (vr0->type == VR_RANGE
3650 && TREE_CODE (vr0->min) == INTEGER_CST)
3652 maxi = prec - 1 - tree_floor_log2 (vr0->min);
3653 if (maxi != prec)
3654 mini = 0;
3656 else if (vr0->type == VR_ANTI_RANGE
3657 && integer_zerop (vr0->min))
3659 maxi = prec - 1;
3660 mini = 0;
3662 if (mini == -2)
3663 break;
3664 /* From clz of VR_RANGE maximum we can compute
3665 result minimum. */
3666 if (vr0->type == VR_RANGE
3667 && TREE_CODE (vr0->max) == INTEGER_CST)
3669 mini = prec - 1 - tree_floor_log2 (vr0->max);
3670 if (mini == prec)
3671 break;
3674 if (mini == -2)
3675 break;
3676 goto bitop_builtin;
3677 /* __builtin_ctz* return [0, prec-1], except for
3678 when the argument is 0, but that is undefined behavior.
3679 If there is a ctz optab for this mode and
3680 CTZ_DEFINED_VALUE_AT_ZERO, include that in the range,
3681 otherwise just assume 0 won't be seen. */
3682 CASE_CFN_CTZ:
3683 arg = gimple_call_arg (stmt, 0);
3684 prec = TYPE_PRECISION (TREE_TYPE (arg));
3685 mini = 0;
3686 maxi = prec - 1;
3687 mode = SCALAR_INT_TYPE_MODE (TREE_TYPE (arg));
3688 if (optab_handler (ctz_optab, mode) != CODE_FOR_nothing
3689 && CTZ_DEFINED_VALUE_AT_ZERO (mode, zerov))
3691 /* Handle only the two common values. */
3692 if (zerov == -1)
3693 mini = -1;
3694 else if (zerov == prec)
3695 maxi = prec;
3696 else
3697 /* Magic value to give up, unless vr0 proves
3698 arg is non-zero. */
3699 mini = -2;
3701 if (TREE_CODE (arg) == SSA_NAME)
3703 value_range *vr0 = get_value_range (arg);
3704 /* If arg is non-zero, then use [0, prec - 1]. */
3705 if ((vr0->type == VR_RANGE
3706 && integer_nonzerop (vr0->min))
3707 || (vr0->type == VR_ANTI_RANGE
3708 && integer_zerop (vr0->min)))
3710 mini = 0;
3711 maxi = prec - 1;
3713 /* If some high bits are known to be zero,
3714 we can decrease the result maximum. */
3715 if (vr0->type == VR_RANGE
3716 && TREE_CODE (vr0->max) == INTEGER_CST)
3718 maxi = tree_floor_log2 (vr0->max);
3719 /* For vr0 [0, 0] give up. */
3720 if (maxi == -1)
3721 break;
3724 if (mini == -2)
3725 break;
3726 goto bitop_builtin;
3727 /* __builtin_clrsb* returns [0, prec-1]. */
3728 CASE_CFN_CLRSB:
3729 arg = gimple_call_arg (stmt, 0);
3730 prec = TYPE_PRECISION (TREE_TYPE (arg));
3731 mini = 0;
3732 maxi = prec - 1;
3733 goto bitop_builtin;
3734 bitop_builtin:
3735 set_value_range (vr, VR_RANGE, build_int_cst (type, mini),
3736 build_int_cst (type, maxi), NULL);
3737 return;
3738 case CFN_UBSAN_CHECK_ADD:
3739 subcode = PLUS_EXPR;
3740 break;
3741 case CFN_UBSAN_CHECK_SUB:
3742 subcode = MINUS_EXPR;
3743 break;
3744 case CFN_UBSAN_CHECK_MUL:
3745 subcode = MULT_EXPR;
3746 break;
3747 case CFN_GOACC_DIM_SIZE:
3748 case CFN_GOACC_DIM_POS:
3749 /* Optimizing these two internal functions helps the loop
3750 optimizer eliminate outer comparisons. Size is [1,N]
3751 and pos is [0,N-1]. */
3753 bool is_pos = cfn == CFN_GOACC_DIM_POS;
3754 int axis = oacc_get_ifn_dim_arg (stmt);
3755 int size = oacc_get_fn_dim_size (current_function_decl, axis);
3757 if (!size)
3758 /* If it's dynamic, the backend might know a hardware
3759 limitation. */
3760 size = targetm.goacc.dim_limit (axis);
3762 tree type = TREE_TYPE (gimple_call_lhs (stmt));
3763 set_value_range (vr, VR_RANGE,
3764 build_int_cst (type, is_pos ? 0 : 1),
3765 size ? build_int_cst (type, size - is_pos)
3766 : vrp_val_max (type), NULL);
3768 return;
3769 case CFN_BUILT_IN_STRLEN:
3770 if (tree lhs = gimple_call_lhs (stmt))
3771 if (ptrdiff_type_node
3772 && (TYPE_PRECISION (ptrdiff_type_node)
3773 == TYPE_PRECISION (TREE_TYPE (lhs))))
3775 tree type = TREE_TYPE (lhs);
3776 tree max = vrp_val_max (ptrdiff_type_node);
3777 wide_int wmax = wi::to_wide (max, TYPE_PRECISION (TREE_TYPE (max)));
3778 tree range_min = build_zero_cst (type);
3779 tree range_max = wide_int_to_tree (type, wmax - 1);
3780 set_value_range (vr, VR_RANGE, range_min, range_max, NULL);
3781 return;
3783 break;
3784 default:
3785 break;
3787 if (subcode != ERROR_MARK)
3789 bool saved_flag_wrapv = flag_wrapv;
3790 /* Pretend the arithmetics is wrapping. If there is
3791 any overflow, we'll complain, but will actually do
3792 wrapping operation. */
3793 flag_wrapv = 1;
3794 extract_range_from_binary_expr (vr, subcode, type,
3795 gimple_call_arg (stmt, 0),
3796 gimple_call_arg (stmt, 1));
3797 flag_wrapv = saved_flag_wrapv;
3799 /* If for both arguments vrp_valueize returned non-NULL,
3800 this should have been already folded and if not, it
3801 wasn't folded because of overflow. Avoid removing the
3802 UBSAN_CHECK_* calls in that case. */
3803 if (vr->type == VR_RANGE
3804 && (vr->min == vr->max
3805 || operand_equal_p (vr->min, vr->max, 0)))
3806 set_value_range_to_varying (vr);
3807 return;
3810 /* Handle extraction of the two results (result of arithmetics and
3811 a flag whether arithmetics overflowed) from {ADD,SUB,MUL}_OVERFLOW
3812 internal function. Similarly from ATOMIC_COMPARE_EXCHANGE. */
3813 else if (is_gimple_assign (stmt)
3814 && (gimple_assign_rhs_code (stmt) == REALPART_EXPR
3815 || gimple_assign_rhs_code (stmt) == IMAGPART_EXPR)
3816 && INTEGRAL_TYPE_P (type))
3818 enum tree_code code = gimple_assign_rhs_code (stmt);
3819 tree op = gimple_assign_rhs1 (stmt);
3820 if (TREE_CODE (op) == code && TREE_CODE (TREE_OPERAND (op, 0)) == SSA_NAME)
3822 gimple *g = SSA_NAME_DEF_STMT (TREE_OPERAND (op, 0));
3823 if (is_gimple_call (g) && gimple_call_internal_p (g))
3825 enum tree_code subcode = ERROR_MARK;
3826 switch (gimple_call_internal_fn (g))
3828 case IFN_ADD_OVERFLOW:
3829 subcode = PLUS_EXPR;
3830 break;
3831 case IFN_SUB_OVERFLOW:
3832 subcode = MINUS_EXPR;
3833 break;
3834 case IFN_MUL_OVERFLOW:
3835 subcode = MULT_EXPR;
3836 break;
3837 case IFN_ATOMIC_COMPARE_EXCHANGE:
3838 if (code == IMAGPART_EXPR)
3840 /* This is the boolean return value whether compare and
3841 exchange changed anything or not. */
3842 set_value_range (vr, VR_RANGE, build_int_cst (type, 0),
3843 build_int_cst (type, 1), NULL);
3844 return;
3846 break;
3847 default:
3848 break;
3850 if (subcode != ERROR_MARK)
3852 tree op0 = gimple_call_arg (g, 0);
3853 tree op1 = gimple_call_arg (g, 1);
3854 if (code == IMAGPART_EXPR)
3856 bool ovf = false;
3857 if (check_for_binary_op_overflow (subcode, type,
3858 op0, op1, &ovf))
3859 set_value_range_to_value (vr,
3860 build_int_cst (type, ovf),
3861 NULL);
3862 else if (TYPE_PRECISION (type) == 1
3863 && !TYPE_UNSIGNED (type))
3864 set_value_range_to_varying (vr);
3865 else
3866 set_value_range (vr, VR_RANGE, build_int_cst (type, 0),
3867 build_int_cst (type, 1), NULL);
3869 else if (types_compatible_p (type, TREE_TYPE (op0))
3870 && types_compatible_p (type, TREE_TYPE (op1)))
3872 bool saved_flag_wrapv = flag_wrapv;
3873 /* Pretend the arithmetics is wrapping. If there is
3874 any overflow, IMAGPART_EXPR will be set. */
3875 flag_wrapv = 1;
3876 extract_range_from_binary_expr (vr, subcode, type,
3877 op0, op1);
3878 flag_wrapv = saved_flag_wrapv;
3880 else
3882 value_range vr0 = VR_INITIALIZER;
3883 value_range vr1 = VR_INITIALIZER;
3884 bool saved_flag_wrapv = flag_wrapv;
3885 /* Pretend the arithmetics is wrapping. If there is
3886 any overflow, IMAGPART_EXPR will be set. */
3887 flag_wrapv = 1;
3888 extract_range_from_unary_expr (&vr0, NOP_EXPR,
3889 type, op0);
3890 extract_range_from_unary_expr (&vr1, NOP_EXPR,
3891 type, op1);
3892 extract_range_from_binary_expr_1 (vr, subcode, type,
3893 &vr0, &vr1);
3894 flag_wrapv = saved_flag_wrapv;
3896 return;
3901 if (INTEGRAL_TYPE_P (type)
3902 && gimple_stmt_nonnegative_warnv_p (stmt, &sop))
3903 set_value_range_to_nonnegative (vr, type);
3904 else if (vrp_stmt_computes_nonzero (stmt))
3905 set_value_range_to_nonnull (vr, type);
3906 else
3907 set_value_range_to_varying (vr);
3911 /* Try to compute a useful range out of assignment STMT and store it
3912 in *VR. */
3914 static void
3915 extract_range_from_assignment (value_range *vr, gassign *stmt)
3917 enum tree_code code = gimple_assign_rhs_code (stmt);
3919 if (code == ASSERT_EXPR)
3920 extract_range_from_assert (vr, gimple_assign_rhs1 (stmt));
3921 else if (code == SSA_NAME)
3922 extract_range_from_ssa_name (vr, gimple_assign_rhs1 (stmt));
3923 else if (TREE_CODE_CLASS (code) == tcc_binary)
3924 extract_range_from_binary_expr (vr, gimple_assign_rhs_code (stmt),
3925 gimple_expr_type (stmt),
3926 gimple_assign_rhs1 (stmt),
3927 gimple_assign_rhs2 (stmt));
3928 else if (TREE_CODE_CLASS (code) == tcc_unary)
3929 extract_range_from_unary_expr (vr, gimple_assign_rhs_code (stmt),
3930 gimple_expr_type (stmt),
3931 gimple_assign_rhs1 (stmt));
3932 else if (code == COND_EXPR)
3933 extract_range_from_cond_expr (vr, stmt);
3934 else if (TREE_CODE_CLASS (code) == tcc_comparison)
3935 extract_range_from_comparison (vr, gimple_assign_rhs_code (stmt),
3936 gimple_expr_type (stmt),
3937 gimple_assign_rhs1 (stmt),
3938 gimple_assign_rhs2 (stmt));
3939 else if (get_gimple_rhs_class (code) == GIMPLE_SINGLE_RHS
3940 && is_gimple_min_invariant (gimple_assign_rhs1 (stmt)))
3941 set_value_range_to_value (vr, gimple_assign_rhs1 (stmt), NULL);
3942 else
3943 set_value_range_to_varying (vr);
3945 if (vr->type == VR_VARYING)
3946 extract_range_basic (vr, stmt);
3949 /* Given a range VR, a LOOP and a variable VAR, determine whether it
3950 would be profitable to adjust VR using scalar evolution information
3951 for VAR. If so, update VR with the new limits. */
3953 static void
3954 adjust_range_with_scev (value_range *vr, struct loop *loop,
3955 gimple *stmt, tree var)
3957 tree init, step, chrec, tmin, tmax, min, max, type, tem;
3958 enum ev_direction dir;
3960 /* TODO. Don't adjust anti-ranges. An anti-range may provide
3961 better opportunities than a regular range, but I'm not sure. */
3962 if (vr->type == VR_ANTI_RANGE)
3963 return;
3965 chrec = instantiate_parameters (loop, analyze_scalar_evolution (loop, var));
3967 /* Like in PR19590, scev can return a constant function. */
3968 if (is_gimple_min_invariant (chrec))
3970 set_value_range_to_value (vr, chrec, vr->equiv);
3971 return;
3974 if (TREE_CODE (chrec) != POLYNOMIAL_CHREC)
3975 return;
3977 init = initial_condition_in_loop_num (chrec, loop->num);
3978 tem = op_with_constant_singleton_value_range (init);
3979 if (tem)
3980 init = tem;
3981 step = evolution_part_in_loop_num (chrec, loop->num);
3982 tem = op_with_constant_singleton_value_range (step);
3983 if (tem)
3984 step = tem;
3986 /* If STEP is symbolic, we can't know whether INIT will be the
3987 minimum or maximum value in the range. Also, unless INIT is
3988 a simple expression, compare_values and possibly other functions
3989 in tree-vrp won't be able to handle it. */
3990 if (step == NULL_TREE
3991 || !is_gimple_min_invariant (step)
3992 || !valid_value_p (init))
3993 return;
3995 dir = scev_direction (chrec);
3996 if (/* Do not adjust ranges if we do not know whether the iv increases
3997 or decreases, ... */
3998 dir == EV_DIR_UNKNOWN
3999 /* ... or if it may wrap. */
4000 || scev_probably_wraps_p (NULL_TREE, init, step, stmt,
4001 get_chrec_loop (chrec), true))
4002 return;
4004 type = TREE_TYPE (var);
4005 if (POINTER_TYPE_P (type) || !TYPE_MIN_VALUE (type))
4006 tmin = lower_bound_in_type (type, type);
4007 else
4008 tmin = TYPE_MIN_VALUE (type);
4009 if (POINTER_TYPE_P (type) || !TYPE_MAX_VALUE (type))
4010 tmax = upper_bound_in_type (type, type);
4011 else
4012 tmax = TYPE_MAX_VALUE (type);
4014 /* Try to use estimated number of iterations for the loop to constrain the
4015 final value in the evolution. */
4016 if (TREE_CODE (step) == INTEGER_CST
4017 && is_gimple_val (init)
4018 && (TREE_CODE (init) != SSA_NAME
4019 || get_value_range (init)->type == VR_RANGE))
4021 widest_int nit;
4023 /* We are only entering here for loop header PHI nodes, so using
4024 the number of latch executions is the correct thing to use. */
4025 if (max_loop_iterations (loop, &nit))
4027 value_range maxvr = VR_INITIALIZER;
4028 signop sgn = TYPE_SIGN (TREE_TYPE (step));
4029 bool overflow;
4031 widest_int wtmp = wi::mul (wi::to_widest (step), nit, sgn,
4032 &overflow);
4033 /* If the multiplication overflowed we can't do a meaningful
4034 adjustment. Likewise if the result doesn't fit in the type
4035 of the induction variable. For a signed type we have to
4036 check whether the result has the expected signedness which
4037 is that of the step as number of iterations is unsigned. */
4038 if (!overflow
4039 && wi::fits_to_tree_p (wtmp, TREE_TYPE (init))
4040 && (sgn == UNSIGNED
4041 || wi::gts_p (wtmp, 0) == wi::gts_p (step, 0)))
4043 tem = wide_int_to_tree (TREE_TYPE (init), wtmp);
4044 extract_range_from_binary_expr (&maxvr, PLUS_EXPR,
4045 TREE_TYPE (init), init, tem);
4046 /* Likewise if the addition did. */
4047 if (maxvr.type == VR_RANGE)
4049 value_range initvr = VR_INITIALIZER;
4051 if (TREE_CODE (init) == SSA_NAME)
4052 initvr = *(get_value_range (init));
4053 else if (is_gimple_min_invariant (init))
4054 set_value_range_to_value (&initvr, init, NULL);
4055 else
4056 return;
4058 /* Check if init + nit * step overflows. Though we checked
4059 scev {init, step}_loop doesn't wrap, it is not enough
4060 because the loop may exit immediately. Overflow could
4061 happen in the plus expression in this case. */
4062 if ((dir == EV_DIR_DECREASES
4063 && compare_values (maxvr.min, initvr.min) != -1)
4064 || (dir == EV_DIR_GROWS
4065 && compare_values (maxvr.max, initvr.max) != 1))
4066 return;
4068 tmin = maxvr.min;
4069 tmax = maxvr.max;
4075 if (vr->type == VR_VARYING || vr->type == VR_UNDEFINED)
4077 min = tmin;
4078 max = tmax;
4080 /* For VARYING or UNDEFINED ranges, just about anything we get
4081 from scalar evolutions should be better. */
4083 if (dir == EV_DIR_DECREASES)
4084 max = init;
4085 else
4086 min = init;
4088 else if (vr->type == VR_RANGE)
4090 min = vr->min;
4091 max = vr->max;
4093 if (dir == EV_DIR_DECREASES)
4095 /* INIT is the maximum value. If INIT is lower than VR->MAX
4096 but no smaller than VR->MIN, set VR->MAX to INIT. */
4097 if (compare_values (init, max) == -1)
4098 max = init;
4100 /* According to the loop information, the variable does not
4101 overflow. */
4102 if (compare_values (min, tmin) == -1)
4103 min = tmin;
4106 else
4108 /* If INIT is bigger than VR->MIN, set VR->MIN to INIT. */
4109 if (compare_values (init, min) == 1)
4110 min = init;
4112 if (compare_values (tmax, max) == -1)
4113 max = tmax;
4116 else
4117 return;
4119 /* If we just created an invalid range with the minimum
4120 greater than the maximum, we fail conservatively.
4121 This should happen only in unreachable
4122 parts of code, or for invalid programs. */
4123 if (compare_values (min, max) == 1)
4124 return;
4126 /* Even for valid range info, sometimes overflow flag will leak in.
4127 As GIMPLE IL should have no constants with TREE_OVERFLOW set, we
4128 drop them. */
4129 if (TREE_OVERFLOW_P (min))
4130 min = drop_tree_overflow (min);
4131 if (TREE_OVERFLOW_P (max))
4132 max = drop_tree_overflow (max);
4134 set_value_range (vr, VR_RANGE, min, max, vr->equiv);
4138 /* Given two numeric value ranges VR0, VR1 and a comparison code COMP:
4140 - Return BOOLEAN_TRUE_NODE if VR0 COMP VR1 always returns true for
4141 all the values in the ranges.
4143 - Return BOOLEAN_FALSE_NODE if the comparison always returns false.
4145 - Return NULL_TREE if it is not always possible to determine the
4146 value of the comparison.
4148 Also set *STRICT_OVERFLOW_P to indicate whether comparision evaluation
4149 assumed signed overflow is undefined. */
4152 static tree
4153 compare_ranges (enum tree_code comp, value_range *vr0, value_range *vr1,
4154 bool *strict_overflow_p)
4156 /* VARYING or UNDEFINED ranges cannot be compared. */
4157 if (vr0->type == VR_VARYING
4158 || vr0->type == VR_UNDEFINED
4159 || vr1->type == VR_VARYING
4160 || vr1->type == VR_UNDEFINED)
4161 return NULL_TREE;
4163 /* Anti-ranges need to be handled separately. */
4164 if (vr0->type == VR_ANTI_RANGE || vr1->type == VR_ANTI_RANGE)
4166 /* If both are anti-ranges, then we cannot compute any
4167 comparison. */
4168 if (vr0->type == VR_ANTI_RANGE && vr1->type == VR_ANTI_RANGE)
4169 return NULL_TREE;
4171 /* These comparisons are never statically computable. */
4172 if (comp == GT_EXPR
4173 || comp == GE_EXPR
4174 || comp == LT_EXPR
4175 || comp == LE_EXPR)
4176 return NULL_TREE;
4178 /* Equality can be computed only between a range and an
4179 anti-range. ~[VAL1, VAL2] == [VAL1, VAL2] is always false. */
4180 if (vr0->type == VR_RANGE)
4182 /* To simplify processing, make VR0 the anti-range. */
4183 value_range *tmp = vr0;
4184 vr0 = vr1;
4185 vr1 = tmp;
4188 gcc_assert (comp == NE_EXPR || comp == EQ_EXPR);
4190 if (compare_values_warnv (vr0->min, vr1->min, strict_overflow_p) == 0
4191 && compare_values_warnv (vr0->max, vr1->max, strict_overflow_p) == 0)
4192 return (comp == NE_EXPR) ? boolean_true_node : boolean_false_node;
4194 return NULL_TREE;
4197 /* Simplify processing. If COMP is GT_EXPR or GE_EXPR, switch the
4198 operands around and change the comparison code. */
4199 if (comp == GT_EXPR || comp == GE_EXPR)
4201 comp = (comp == GT_EXPR) ? LT_EXPR : LE_EXPR;
4202 std::swap (vr0, vr1);
4205 if (comp == EQ_EXPR)
4207 /* Equality may only be computed if both ranges represent
4208 exactly one value. */
4209 if (compare_values_warnv (vr0->min, vr0->max, strict_overflow_p) == 0
4210 && compare_values_warnv (vr1->min, vr1->max, strict_overflow_p) == 0)
4212 int cmp_min = compare_values_warnv (vr0->min, vr1->min,
4213 strict_overflow_p);
4214 int cmp_max = compare_values_warnv (vr0->max, vr1->max,
4215 strict_overflow_p);
4216 if (cmp_min == 0 && cmp_max == 0)
4217 return boolean_true_node;
4218 else if (cmp_min != -2 && cmp_max != -2)
4219 return boolean_false_node;
4221 /* If [V0_MIN, V1_MAX] < [V1_MIN, V1_MAX] then V0 != V1. */
4222 else if (compare_values_warnv (vr0->min, vr1->max,
4223 strict_overflow_p) == 1
4224 || compare_values_warnv (vr1->min, vr0->max,
4225 strict_overflow_p) == 1)
4226 return boolean_false_node;
4228 return NULL_TREE;
4230 else if (comp == NE_EXPR)
4232 int cmp1, cmp2;
4234 /* If VR0 is completely to the left or completely to the right
4235 of VR1, they are always different. Notice that we need to
4236 make sure that both comparisons yield similar results to
4237 avoid comparing values that cannot be compared at
4238 compile-time. */
4239 cmp1 = compare_values_warnv (vr0->max, vr1->min, strict_overflow_p);
4240 cmp2 = compare_values_warnv (vr0->min, vr1->max, strict_overflow_p);
4241 if ((cmp1 == -1 && cmp2 == -1) || (cmp1 == 1 && cmp2 == 1))
4242 return boolean_true_node;
4244 /* If VR0 and VR1 represent a single value and are identical,
4245 return false. */
4246 else if (compare_values_warnv (vr0->min, vr0->max,
4247 strict_overflow_p) == 0
4248 && compare_values_warnv (vr1->min, vr1->max,
4249 strict_overflow_p) == 0
4250 && compare_values_warnv (vr0->min, vr1->min,
4251 strict_overflow_p) == 0
4252 && compare_values_warnv (vr0->max, vr1->max,
4253 strict_overflow_p) == 0)
4254 return boolean_false_node;
4256 /* Otherwise, they may or may not be different. */
4257 else
4258 return NULL_TREE;
4260 else if (comp == LT_EXPR || comp == LE_EXPR)
4262 int tst;
4264 /* If VR0 is to the left of VR1, return true. */
4265 tst = compare_values_warnv (vr0->max, vr1->min, strict_overflow_p);
4266 if ((comp == LT_EXPR && tst == -1)
4267 || (comp == LE_EXPR && (tst == -1 || tst == 0)))
4268 return boolean_true_node;
4270 /* If VR0 is to the right of VR1, return false. */
4271 tst = compare_values_warnv (vr0->min, vr1->max, strict_overflow_p);
4272 if ((comp == LT_EXPR && (tst == 0 || tst == 1))
4273 || (comp == LE_EXPR && tst == 1))
4274 return boolean_false_node;
4276 /* Otherwise, we don't know. */
4277 return NULL_TREE;
4280 gcc_unreachable ();
4284 /* Given a value range VR, a value VAL and a comparison code COMP, return
4285 BOOLEAN_TRUE_NODE if VR COMP VAL always returns true for all the
4286 values in VR. Return BOOLEAN_FALSE_NODE if the comparison
4287 always returns false. Return NULL_TREE if it is not always
4288 possible to determine the value of the comparison. Also set
4289 *STRICT_OVERFLOW_P to indicate whether comparision evaluation
4290 assumed signed overflow is undefined. */
4292 static tree
4293 compare_range_with_value (enum tree_code comp, value_range *vr, tree val,
4294 bool *strict_overflow_p)
4296 if (vr->type == VR_VARYING || vr->type == VR_UNDEFINED)
4297 return NULL_TREE;
4299 /* Anti-ranges need to be handled separately. */
4300 if (vr->type == VR_ANTI_RANGE)
4302 /* For anti-ranges, the only predicates that we can compute at
4303 compile time are equality and inequality. */
4304 if (comp == GT_EXPR
4305 || comp == GE_EXPR
4306 || comp == LT_EXPR
4307 || comp == LE_EXPR)
4308 return NULL_TREE;
4310 /* ~[VAL_1, VAL_2] OP VAL is known if VAL_1 <= VAL <= VAL_2. */
4311 if (value_inside_range (val, vr->min, vr->max) == 1)
4312 return (comp == NE_EXPR) ? boolean_true_node : boolean_false_node;
4314 return NULL_TREE;
4317 if (comp == EQ_EXPR)
4319 /* EQ_EXPR may only be computed if VR represents exactly
4320 one value. */
4321 if (compare_values_warnv (vr->min, vr->max, strict_overflow_p) == 0)
4323 int cmp = compare_values_warnv (vr->min, val, strict_overflow_p);
4324 if (cmp == 0)
4325 return boolean_true_node;
4326 else if (cmp == -1 || cmp == 1 || cmp == 2)
4327 return boolean_false_node;
4329 else if (compare_values_warnv (val, vr->min, strict_overflow_p) == -1
4330 || compare_values_warnv (vr->max, val, strict_overflow_p) == -1)
4331 return boolean_false_node;
4333 return NULL_TREE;
4335 else if (comp == NE_EXPR)
4337 /* If VAL is not inside VR, then they are always different. */
4338 if (compare_values_warnv (vr->max, val, strict_overflow_p) == -1
4339 || compare_values_warnv (vr->min, val, strict_overflow_p) == 1)
4340 return boolean_true_node;
4342 /* If VR represents exactly one value equal to VAL, then return
4343 false. */
4344 if (compare_values_warnv (vr->min, vr->max, strict_overflow_p) == 0
4345 && compare_values_warnv (vr->min, val, strict_overflow_p) == 0)
4346 return boolean_false_node;
4348 /* Otherwise, they may or may not be different. */
4349 return NULL_TREE;
4351 else if (comp == LT_EXPR || comp == LE_EXPR)
4353 int tst;
4355 /* If VR is to the left of VAL, return true. */
4356 tst = compare_values_warnv (vr->max, val, strict_overflow_p);
4357 if ((comp == LT_EXPR && tst == -1)
4358 || (comp == LE_EXPR && (tst == -1 || tst == 0)))
4359 return boolean_true_node;
4361 /* If VR is to the right of VAL, return false. */
4362 tst = compare_values_warnv (vr->min, val, strict_overflow_p);
4363 if ((comp == LT_EXPR && (tst == 0 || tst == 1))
4364 || (comp == LE_EXPR && tst == 1))
4365 return boolean_false_node;
4367 /* Otherwise, we don't know. */
4368 return NULL_TREE;
4370 else if (comp == GT_EXPR || comp == GE_EXPR)
4372 int tst;
4374 /* If VR is to the right of VAL, return true. */
4375 tst = compare_values_warnv (vr->min, val, strict_overflow_p);
4376 if ((comp == GT_EXPR && tst == 1)
4377 || (comp == GE_EXPR && (tst == 0 || tst == 1)))
4378 return boolean_true_node;
4380 /* If VR is to the left of VAL, return false. */
4381 tst = compare_values_warnv (vr->max, val, strict_overflow_p);
4382 if ((comp == GT_EXPR && (tst == -1 || tst == 0))
4383 || (comp == GE_EXPR && tst == -1))
4384 return boolean_false_node;
4386 /* Otherwise, we don't know. */
4387 return NULL_TREE;
4390 gcc_unreachable ();
4394 /* Debugging dumps. */
4396 void dump_value_range (FILE *, const value_range *);
4397 void debug_value_range (value_range *);
4398 void dump_all_value_ranges (FILE *);
4399 void debug_all_value_ranges (void);
4400 void dump_vr_equiv (FILE *, bitmap);
4401 void debug_vr_equiv (bitmap);
4404 /* Dump value range VR to FILE. */
4406 void
4407 dump_value_range (FILE *file, const value_range *vr)
4409 if (vr == NULL)
4410 fprintf (file, "[]");
4411 else if (vr->type == VR_UNDEFINED)
4412 fprintf (file, "UNDEFINED");
4413 else if (vr->type == VR_RANGE || vr->type == VR_ANTI_RANGE)
4415 tree type = TREE_TYPE (vr->min);
4417 fprintf (file, "%s[", (vr->type == VR_ANTI_RANGE) ? "~" : "");
4419 if (INTEGRAL_TYPE_P (type)
4420 && !TYPE_UNSIGNED (type)
4421 && vrp_val_is_min (vr->min))
4422 fprintf (file, "-INF");
4423 else
4424 print_generic_expr (file, vr->min);
4426 fprintf (file, ", ");
4428 if (INTEGRAL_TYPE_P (type)
4429 && vrp_val_is_max (vr->max))
4430 fprintf (file, "+INF");
4431 else
4432 print_generic_expr (file, vr->max);
4434 fprintf (file, "]");
4436 if (vr->equiv)
4438 bitmap_iterator bi;
4439 unsigned i, c = 0;
4441 fprintf (file, " EQUIVALENCES: { ");
4443 EXECUTE_IF_SET_IN_BITMAP (vr->equiv, 0, i, bi)
4445 print_generic_expr (file, ssa_name (i));
4446 fprintf (file, " ");
4447 c++;
4450 fprintf (file, "} (%u elements)", c);
4453 else if (vr->type == VR_VARYING)
4454 fprintf (file, "VARYING");
4455 else
4456 fprintf (file, "INVALID RANGE");
4460 /* Dump value range VR to stderr. */
4462 DEBUG_FUNCTION void
4463 debug_value_range (value_range *vr)
4465 dump_value_range (stderr, vr);
4466 fprintf (stderr, "\n");
4470 /* Dump value ranges of all SSA_NAMEs to FILE. */
4472 void
4473 dump_all_value_ranges (FILE *file)
4475 size_t i;
4477 for (i = 0; i < num_vr_values; i++)
4479 if (vr_value[i])
4481 print_generic_expr (file, ssa_name (i));
4482 fprintf (file, ": ");
4483 dump_value_range (file, vr_value[i]);
4484 fprintf (file, "\n");
4488 fprintf (file, "\n");
4492 /* Dump all value ranges to stderr. */
4494 DEBUG_FUNCTION void
4495 debug_all_value_ranges (void)
4497 dump_all_value_ranges (stderr);
4501 /* Given a COND_EXPR COND of the form 'V OP W', and an SSA name V,
4502 create a new SSA name N and return the assertion assignment
4503 'N = ASSERT_EXPR <V, V OP W>'. */
4505 static gimple *
4506 build_assert_expr_for (tree cond, tree v)
4508 tree a;
4509 gassign *assertion;
4511 gcc_assert (TREE_CODE (v) == SSA_NAME
4512 && COMPARISON_CLASS_P (cond));
4514 a = build2 (ASSERT_EXPR, TREE_TYPE (v), v, cond);
4515 assertion = gimple_build_assign (NULL_TREE, a);
4517 /* The new ASSERT_EXPR, creates a new SSA name that replaces the
4518 operand of the ASSERT_EXPR. Create it so the new name and the old one
4519 are registered in the replacement table so that we can fix the SSA web
4520 after adding all the ASSERT_EXPRs. */
4521 create_new_def_for (v, assertion, NULL);
4523 return assertion;
4527 /* Return false if EXPR is a predicate expression involving floating
4528 point values. */
4530 static inline bool
4531 fp_predicate (gimple *stmt)
4533 GIMPLE_CHECK (stmt, GIMPLE_COND);
4535 return FLOAT_TYPE_P (TREE_TYPE (gimple_cond_lhs (stmt)));
4538 /* If the range of values taken by OP can be inferred after STMT executes,
4539 return the comparison code (COMP_CODE_P) and value (VAL_P) that
4540 describes the inferred range. Return true if a range could be
4541 inferred. */
4543 static bool
4544 infer_value_range (gimple *stmt, tree op, tree_code *comp_code_p, tree *val_p)
4546 *val_p = NULL_TREE;
4547 *comp_code_p = ERROR_MARK;
4549 /* Do not attempt to infer anything in names that flow through
4550 abnormal edges. */
4551 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op))
4552 return false;
4554 /* If STMT is the last statement of a basic block with no normal
4555 successors, there is no point inferring anything about any of its
4556 operands. We would not be able to find a proper insertion point
4557 for the assertion, anyway. */
4558 if (stmt_ends_bb_p (stmt))
4560 edge_iterator ei;
4561 edge e;
4563 FOR_EACH_EDGE (e, ei, gimple_bb (stmt)->succs)
4564 if (!(e->flags & (EDGE_ABNORMAL|EDGE_EH)))
4565 break;
4566 if (e == NULL)
4567 return false;
4570 if (infer_nonnull_range (stmt, op))
4572 *val_p = build_int_cst (TREE_TYPE (op), 0);
4573 *comp_code_p = NE_EXPR;
4574 return true;
4577 return false;
4581 void dump_asserts_for (FILE *, tree);
4582 void debug_asserts_for (tree);
4583 void dump_all_asserts (FILE *);
4584 void debug_all_asserts (void);
4586 /* Dump all the registered assertions for NAME to FILE. */
4588 void
4589 dump_asserts_for (FILE *file, tree name)
4591 assert_locus *loc;
4593 fprintf (file, "Assertions to be inserted for ");
4594 print_generic_expr (file, name);
4595 fprintf (file, "\n");
4597 loc = asserts_for[SSA_NAME_VERSION (name)];
4598 while (loc)
4600 fprintf (file, "\t");
4601 print_gimple_stmt (file, gsi_stmt (loc->si), 0);
4602 fprintf (file, "\n\tBB #%d", loc->bb->index);
4603 if (loc->e)
4605 fprintf (file, "\n\tEDGE %d->%d", loc->e->src->index,
4606 loc->e->dest->index);
4607 dump_edge_info (file, loc->e, dump_flags, 0);
4609 fprintf (file, "\n\tPREDICATE: ");
4610 print_generic_expr (file, loc->expr);
4611 fprintf (file, " %s ", get_tree_code_name (loc->comp_code));
4612 print_generic_expr (file, loc->val);
4613 fprintf (file, "\n\n");
4614 loc = loc->next;
4617 fprintf (file, "\n");
4621 /* Dump all the registered assertions for NAME to stderr. */
4623 DEBUG_FUNCTION void
4624 debug_asserts_for (tree name)
4626 dump_asserts_for (stderr, name);
4630 /* Dump all the registered assertions for all the names to FILE. */
4632 void
4633 dump_all_asserts (FILE *file)
4635 unsigned i;
4636 bitmap_iterator bi;
4638 fprintf (file, "\nASSERT_EXPRs to be inserted\n\n");
4639 EXECUTE_IF_SET_IN_BITMAP (need_assert_for, 0, i, bi)
4640 dump_asserts_for (file, ssa_name (i));
4641 fprintf (file, "\n");
4645 /* Dump all the registered assertions for all the names to stderr. */
4647 DEBUG_FUNCTION void
4648 debug_all_asserts (void)
4650 dump_all_asserts (stderr);
4653 /* Push the assert info for NAME, EXPR, COMP_CODE and VAL to ASSERTS. */
4655 static void
4656 add_assert_info (vec<assert_info> &asserts,
4657 tree name, tree expr, enum tree_code comp_code, tree val)
4659 assert_info info;
4660 info.comp_code = comp_code;
4661 info.name = name;
4662 info.val = val;
4663 info.expr = expr;
4664 asserts.safe_push (info);
4667 /* If NAME doesn't have an ASSERT_EXPR registered for asserting
4668 'EXPR COMP_CODE VAL' at a location that dominates block BB or
4669 E->DEST, then register this location as a possible insertion point
4670 for ASSERT_EXPR <NAME, EXPR COMP_CODE VAL>.
4672 BB, E and SI provide the exact insertion point for the new
4673 ASSERT_EXPR. If BB is NULL, then the ASSERT_EXPR is to be inserted
4674 on edge E. Otherwise, if E is NULL, the ASSERT_EXPR is inserted on
4675 BB. If SI points to a COND_EXPR or a SWITCH_EXPR statement, then E
4676 must not be NULL. */
4678 static void
4679 register_new_assert_for (tree name, tree expr,
4680 enum tree_code comp_code,
4681 tree val,
4682 basic_block bb,
4683 edge e,
4684 gimple_stmt_iterator si)
4686 assert_locus *n, *loc, *last_loc;
4687 basic_block dest_bb;
4689 gcc_checking_assert (bb == NULL || e == NULL);
4691 if (e == NULL)
4692 gcc_checking_assert (gimple_code (gsi_stmt (si)) != GIMPLE_COND
4693 && gimple_code (gsi_stmt (si)) != GIMPLE_SWITCH);
4695 /* Never build an assert comparing against an integer constant with
4696 TREE_OVERFLOW set. This confuses our undefined overflow warning
4697 machinery. */
4698 if (TREE_OVERFLOW_P (val))
4699 val = drop_tree_overflow (val);
4701 /* The new assertion A will be inserted at BB or E. We need to
4702 determine if the new location is dominated by a previously
4703 registered location for A. If we are doing an edge insertion,
4704 assume that A will be inserted at E->DEST. Note that this is not
4705 necessarily true.
4707 If E is a critical edge, it will be split. But even if E is
4708 split, the new block will dominate the same set of blocks that
4709 E->DEST dominates.
4711 The reverse, however, is not true, blocks dominated by E->DEST
4712 will not be dominated by the new block created to split E. So,
4713 if the insertion location is on a critical edge, we will not use
4714 the new location to move another assertion previously registered
4715 at a block dominated by E->DEST. */
4716 dest_bb = (bb) ? bb : e->dest;
4718 /* If NAME already has an ASSERT_EXPR registered for COMP_CODE and
4719 VAL at a block dominating DEST_BB, then we don't need to insert a new
4720 one. Similarly, if the same assertion already exists at a block
4721 dominated by DEST_BB and the new location is not on a critical
4722 edge, then update the existing location for the assertion (i.e.,
4723 move the assertion up in the dominance tree).
4725 Note, this is implemented as a simple linked list because there
4726 should not be more than a handful of assertions registered per
4727 name. If this becomes a performance problem, a table hashed by
4728 COMP_CODE and VAL could be implemented. */
4729 loc = asserts_for[SSA_NAME_VERSION (name)];
4730 last_loc = loc;
4731 while (loc)
4733 if (loc->comp_code == comp_code
4734 && (loc->val == val
4735 || operand_equal_p (loc->val, val, 0))
4736 && (loc->expr == expr
4737 || operand_equal_p (loc->expr, expr, 0)))
4739 /* If E is not a critical edge and DEST_BB
4740 dominates the existing location for the assertion, move
4741 the assertion up in the dominance tree by updating its
4742 location information. */
4743 if ((e == NULL || !EDGE_CRITICAL_P (e))
4744 && dominated_by_p (CDI_DOMINATORS, loc->bb, dest_bb))
4746 loc->bb = dest_bb;
4747 loc->e = e;
4748 loc->si = si;
4749 return;
4753 /* Update the last node of the list and move to the next one. */
4754 last_loc = loc;
4755 loc = loc->next;
4758 /* If we didn't find an assertion already registered for
4759 NAME COMP_CODE VAL, add a new one at the end of the list of
4760 assertions associated with NAME. */
4761 n = XNEW (struct assert_locus);
4762 n->bb = dest_bb;
4763 n->e = e;
4764 n->si = si;
4765 n->comp_code = comp_code;
4766 n->val = val;
4767 n->expr = expr;
4768 n->next = NULL;
4770 if (last_loc)
4771 last_loc->next = n;
4772 else
4773 asserts_for[SSA_NAME_VERSION (name)] = n;
4775 bitmap_set_bit (need_assert_for, SSA_NAME_VERSION (name));
4778 /* (COND_OP0 COND_CODE COND_OP1) is a predicate which uses NAME.
4779 Extract a suitable test code and value and store them into *CODE_P and
4780 *VAL_P so the predicate is normalized to NAME *CODE_P *VAL_P.
4782 If no extraction was possible, return FALSE, otherwise return TRUE.
4784 If INVERT is true, then we invert the result stored into *CODE_P. */
4786 static bool
4787 extract_code_and_val_from_cond_with_ops (tree name, enum tree_code cond_code,
4788 tree cond_op0, tree cond_op1,
4789 bool invert, enum tree_code *code_p,
4790 tree *val_p)
4792 enum tree_code comp_code;
4793 tree val;
4795 /* Otherwise, we have a comparison of the form NAME COMP VAL
4796 or VAL COMP NAME. */
4797 if (name == cond_op1)
4799 /* If the predicate is of the form VAL COMP NAME, flip
4800 COMP around because we need to register NAME as the
4801 first operand in the predicate. */
4802 comp_code = swap_tree_comparison (cond_code);
4803 val = cond_op0;
4805 else if (name == cond_op0)
4807 /* The comparison is of the form NAME COMP VAL, so the
4808 comparison code remains unchanged. */
4809 comp_code = cond_code;
4810 val = cond_op1;
4812 else
4813 gcc_unreachable ();
4815 /* Invert the comparison code as necessary. */
4816 if (invert)
4817 comp_code = invert_tree_comparison (comp_code, 0);
4819 /* VRP only handles integral and pointer types. */
4820 if (! INTEGRAL_TYPE_P (TREE_TYPE (val))
4821 && ! POINTER_TYPE_P (TREE_TYPE (val)))
4822 return false;
4824 /* Do not register always-false predicates.
4825 FIXME: this works around a limitation in fold() when dealing with
4826 enumerations. Given 'enum { N1, N2 } x;', fold will not
4827 fold 'if (x > N2)' to 'if (0)'. */
4828 if ((comp_code == GT_EXPR || comp_code == LT_EXPR)
4829 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
4831 tree min = TYPE_MIN_VALUE (TREE_TYPE (val));
4832 tree max = TYPE_MAX_VALUE (TREE_TYPE (val));
4834 if (comp_code == GT_EXPR
4835 && (!max
4836 || compare_values (val, max) == 0))
4837 return false;
4839 if (comp_code == LT_EXPR
4840 && (!min
4841 || compare_values (val, min) == 0))
4842 return false;
4844 *code_p = comp_code;
4845 *val_p = val;
4846 return true;
4849 /* Find out smallest RES where RES > VAL && (RES & MASK) == RES, if any
4850 (otherwise return VAL). VAL and MASK must be zero-extended for
4851 precision PREC. If SGNBIT is non-zero, first xor VAL with SGNBIT
4852 (to transform signed values into unsigned) and at the end xor
4853 SGNBIT back. */
4855 static wide_int
4856 masked_increment (const wide_int &val_in, const wide_int &mask,
4857 const wide_int &sgnbit, unsigned int prec)
4859 wide_int bit = wi::one (prec), res;
4860 unsigned int i;
4862 wide_int val = val_in ^ sgnbit;
4863 for (i = 0; i < prec; i++, bit += bit)
4865 res = mask;
4866 if ((res & bit) == 0)
4867 continue;
4868 res = bit - 1;
4869 res = (val + bit).and_not (res);
4870 res &= mask;
4871 if (wi::gtu_p (res, val))
4872 return res ^ sgnbit;
4874 return val ^ sgnbit;
4877 /* Helper for overflow_comparison_p
4879 OP0 CODE OP1 is a comparison. Examine the comparison and potentially
4880 OP1's defining statement to see if it ultimately has the form
4881 OP0 CODE (OP0 PLUS INTEGER_CST)
4883 If so, return TRUE indicating this is an overflow test and store into
4884 *NEW_CST an updated constant that can be used in a narrowed range test.
4886 REVERSED indicates if the comparison was originally:
4888 OP1 CODE' OP0.
4890 This affects how we build the updated constant. */
4892 static bool
4893 overflow_comparison_p_1 (enum tree_code code, tree op0, tree op1,
4894 bool follow_assert_exprs, bool reversed, tree *new_cst)
4896 /* See if this is a relational operation between two SSA_NAMES with
4897 unsigned, overflow wrapping values. If so, check it more deeply. */
4898 if ((code == LT_EXPR || code == LE_EXPR
4899 || code == GE_EXPR || code == GT_EXPR)
4900 && TREE_CODE (op0) == SSA_NAME
4901 && TREE_CODE (op1) == SSA_NAME
4902 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
4903 && TYPE_UNSIGNED (TREE_TYPE (op0))
4904 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (op0)))
4906 gimple *op1_def = SSA_NAME_DEF_STMT (op1);
4908 /* If requested, follow any ASSERT_EXPRs backwards for OP1. */
4909 if (follow_assert_exprs)
4911 while (gimple_assign_single_p (op1_def)
4912 && TREE_CODE (gimple_assign_rhs1 (op1_def)) == ASSERT_EXPR)
4914 op1 = TREE_OPERAND (gimple_assign_rhs1 (op1_def), 0);
4915 if (TREE_CODE (op1) != SSA_NAME)
4916 break;
4917 op1_def = SSA_NAME_DEF_STMT (op1);
4921 /* Now look at the defining statement of OP1 to see if it adds
4922 or subtracts a nonzero constant from another operand. */
4923 if (op1_def
4924 && is_gimple_assign (op1_def)
4925 && gimple_assign_rhs_code (op1_def) == PLUS_EXPR
4926 && TREE_CODE (gimple_assign_rhs2 (op1_def)) == INTEGER_CST
4927 && !integer_zerop (gimple_assign_rhs2 (op1_def)))
4929 tree target = gimple_assign_rhs1 (op1_def);
4931 /* If requested, follow ASSERT_EXPRs backwards for op0 looking
4932 for one where TARGET appears on the RHS. */
4933 if (follow_assert_exprs)
4935 /* Now see if that "other operand" is op0, following the chain
4936 of ASSERT_EXPRs if necessary. */
4937 gimple *op0_def = SSA_NAME_DEF_STMT (op0);
4938 while (op0 != target
4939 && gimple_assign_single_p (op0_def)
4940 && TREE_CODE (gimple_assign_rhs1 (op0_def)) == ASSERT_EXPR)
4942 op0 = TREE_OPERAND (gimple_assign_rhs1 (op0_def), 0);
4943 if (TREE_CODE (op0) != SSA_NAME)
4944 break;
4945 op0_def = SSA_NAME_DEF_STMT (op0);
4949 /* If we did not find our target SSA_NAME, then this is not
4950 an overflow test. */
4951 if (op0 != target)
4952 return false;
4954 tree type = TREE_TYPE (op0);
4955 wide_int max = wi::max_value (TYPE_PRECISION (type), UNSIGNED);
4956 tree inc = gimple_assign_rhs2 (op1_def);
4957 if (reversed)
4958 *new_cst = wide_int_to_tree (type, max + inc);
4959 else
4960 *new_cst = wide_int_to_tree (type, max - inc);
4961 return true;
4964 return false;
4967 /* OP0 CODE OP1 is a comparison. Examine the comparison and potentially
4968 OP1's defining statement to see if it ultimately has the form
4969 OP0 CODE (OP0 PLUS INTEGER_CST)
4971 If so, return TRUE indicating this is an overflow test and store into
4972 *NEW_CST an updated constant that can be used in a narrowed range test.
4974 These statements are left as-is in the IL to facilitate discovery of
4975 {ADD,SUB}_OVERFLOW sequences later in the optimizer pipeline. But
4976 the alternate range representation is often useful within VRP. */
4978 static bool
4979 overflow_comparison_p (tree_code code, tree name, tree val,
4980 bool use_equiv_p, tree *new_cst)
4982 if (overflow_comparison_p_1 (code, name, val, use_equiv_p, false, new_cst))
4983 return true;
4984 return overflow_comparison_p_1 (swap_tree_comparison (code), val, name,
4985 use_equiv_p, true, new_cst);
4989 /* Try to register an edge assertion for SSA name NAME on edge E for
4990 the condition COND contributing to the conditional jump pointed to by BSI.
4991 Invert the condition COND if INVERT is true. */
4993 static void
4994 register_edge_assert_for_2 (tree name, edge e,
4995 enum tree_code cond_code,
4996 tree cond_op0, tree cond_op1, bool invert,
4997 vec<assert_info> &asserts)
4999 tree val;
5000 enum tree_code comp_code;
5002 if (!extract_code_and_val_from_cond_with_ops (name, cond_code,
5003 cond_op0,
5004 cond_op1,
5005 invert, &comp_code, &val))
5006 return;
5008 /* Queue the assert. */
5009 tree x;
5010 if (overflow_comparison_p (comp_code, name, val, false, &x))
5012 enum tree_code new_code = ((comp_code == GT_EXPR || comp_code == GE_EXPR)
5013 ? GT_EXPR : LE_EXPR);
5014 add_assert_info (asserts, name, name, new_code, x);
5016 add_assert_info (asserts, name, name, comp_code, val);
5018 /* In the case of NAME <= CST and NAME being defined as
5019 NAME = (unsigned) NAME2 + CST2 we can assert NAME2 >= -CST2
5020 and NAME2 <= CST - CST2. We can do the same for NAME > CST.
5021 This catches range and anti-range tests. */
5022 if ((comp_code == LE_EXPR
5023 || comp_code == GT_EXPR)
5024 && TREE_CODE (val) == INTEGER_CST
5025 && TYPE_UNSIGNED (TREE_TYPE (val)))
5027 gimple *def_stmt = SSA_NAME_DEF_STMT (name);
5028 tree cst2 = NULL_TREE, name2 = NULL_TREE, name3 = NULL_TREE;
5030 /* Extract CST2 from the (optional) addition. */
5031 if (is_gimple_assign (def_stmt)
5032 && gimple_assign_rhs_code (def_stmt) == PLUS_EXPR)
5034 name2 = gimple_assign_rhs1 (def_stmt);
5035 cst2 = gimple_assign_rhs2 (def_stmt);
5036 if (TREE_CODE (name2) == SSA_NAME
5037 && TREE_CODE (cst2) == INTEGER_CST)
5038 def_stmt = SSA_NAME_DEF_STMT (name2);
5041 /* Extract NAME2 from the (optional) sign-changing cast. */
5042 if (gimple_assign_cast_p (def_stmt))
5044 if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt))
5045 && ! TYPE_UNSIGNED (TREE_TYPE (gimple_assign_rhs1 (def_stmt)))
5046 && (TYPE_PRECISION (gimple_expr_type (def_stmt))
5047 == TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt)))))
5048 name3 = gimple_assign_rhs1 (def_stmt);
5051 /* If name3 is used later, create an ASSERT_EXPR for it. */
5052 if (name3 != NULL_TREE
5053 && TREE_CODE (name3) == SSA_NAME
5054 && (cst2 == NULL_TREE
5055 || TREE_CODE (cst2) == INTEGER_CST)
5056 && INTEGRAL_TYPE_P (TREE_TYPE (name3)))
5058 tree tmp;
5060 /* Build an expression for the range test. */
5061 tmp = build1 (NOP_EXPR, TREE_TYPE (name), name3);
5062 if (cst2 != NULL_TREE)
5063 tmp = build2 (PLUS_EXPR, TREE_TYPE (name), tmp, cst2);
5065 if (dump_file)
5067 fprintf (dump_file, "Adding assert for ");
5068 print_generic_expr (dump_file, name3);
5069 fprintf (dump_file, " from ");
5070 print_generic_expr (dump_file, tmp);
5071 fprintf (dump_file, "\n");
5074 add_assert_info (asserts, name3, tmp, comp_code, val);
5077 /* If name2 is used later, create an ASSERT_EXPR for it. */
5078 if (name2 != NULL_TREE
5079 && TREE_CODE (name2) == SSA_NAME
5080 && TREE_CODE (cst2) == INTEGER_CST
5081 && INTEGRAL_TYPE_P (TREE_TYPE (name2)))
5083 tree tmp;
5085 /* Build an expression for the range test. */
5086 tmp = name2;
5087 if (TREE_TYPE (name) != TREE_TYPE (name2))
5088 tmp = build1 (NOP_EXPR, TREE_TYPE (name), tmp);
5089 if (cst2 != NULL_TREE)
5090 tmp = build2 (PLUS_EXPR, TREE_TYPE (name), tmp, cst2);
5092 if (dump_file)
5094 fprintf (dump_file, "Adding assert for ");
5095 print_generic_expr (dump_file, name2);
5096 fprintf (dump_file, " from ");
5097 print_generic_expr (dump_file, tmp);
5098 fprintf (dump_file, "\n");
5101 add_assert_info (asserts, name2, tmp, comp_code, val);
5105 /* In the case of post-in/decrement tests like if (i++) ... and uses
5106 of the in/decremented value on the edge the extra name we want to
5107 assert for is not on the def chain of the name compared. Instead
5108 it is in the set of use stmts.
5109 Similar cases happen for conversions that were simplified through
5110 fold_{sign_changed,widened}_comparison. */
5111 if ((comp_code == NE_EXPR
5112 || comp_code == EQ_EXPR)
5113 && TREE_CODE (val) == INTEGER_CST)
5115 imm_use_iterator ui;
5116 gimple *use_stmt;
5117 FOR_EACH_IMM_USE_STMT (use_stmt, ui, name)
5119 if (!is_gimple_assign (use_stmt))
5120 continue;
5122 /* Cut off to use-stmts that are dominating the predecessor. */
5123 if (!dominated_by_p (CDI_DOMINATORS, e->src, gimple_bb (use_stmt)))
5124 continue;
5126 tree name2 = gimple_assign_lhs (use_stmt);
5127 if (TREE_CODE (name2) != SSA_NAME)
5128 continue;
5130 enum tree_code code = gimple_assign_rhs_code (use_stmt);
5131 tree cst;
5132 if (code == PLUS_EXPR
5133 || code == MINUS_EXPR)
5135 cst = gimple_assign_rhs2 (use_stmt);
5136 if (TREE_CODE (cst) != INTEGER_CST)
5137 continue;
5138 cst = int_const_binop (code, val, cst);
5140 else if (CONVERT_EXPR_CODE_P (code))
5142 /* For truncating conversions we cannot record
5143 an inequality. */
5144 if (comp_code == NE_EXPR
5145 && (TYPE_PRECISION (TREE_TYPE (name2))
5146 < TYPE_PRECISION (TREE_TYPE (name))))
5147 continue;
5148 cst = fold_convert (TREE_TYPE (name2), val);
5150 else
5151 continue;
5153 if (TREE_OVERFLOW_P (cst))
5154 cst = drop_tree_overflow (cst);
5155 add_assert_info (asserts, name2, name2, comp_code, cst);
5159 if (TREE_CODE_CLASS (comp_code) == tcc_comparison
5160 && TREE_CODE (val) == INTEGER_CST)
5162 gimple *def_stmt = SSA_NAME_DEF_STMT (name);
5163 tree name2 = NULL_TREE, names[2], cst2 = NULL_TREE;
5164 tree val2 = NULL_TREE;
5165 unsigned int prec = TYPE_PRECISION (TREE_TYPE (val));
5166 wide_int mask = wi::zero (prec);
5167 unsigned int nprec = prec;
5168 enum tree_code rhs_code = ERROR_MARK;
5170 if (is_gimple_assign (def_stmt))
5171 rhs_code = gimple_assign_rhs_code (def_stmt);
5173 /* In the case of NAME != CST1 where NAME = A +- CST2 we can
5174 assert that A != CST1 -+ CST2. */
5175 if ((comp_code == EQ_EXPR || comp_code == NE_EXPR)
5176 && (rhs_code == PLUS_EXPR || rhs_code == MINUS_EXPR))
5178 tree op0 = gimple_assign_rhs1 (def_stmt);
5179 tree op1 = gimple_assign_rhs2 (def_stmt);
5180 if (TREE_CODE (op0) == SSA_NAME
5181 && TREE_CODE (op1) == INTEGER_CST)
5183 enum tree_code reverse_op = (rhs_code == PLUS_EXPR
5184 ? MINUS_EXPR : PLUS_EXPR);
5185 op1 = int_const_binop (reverse_op, val, op1);
5186 if (TREE_OVERFLOW (op1))
5187 op1 = drop_tree_overflow (op1);
5188 add_assert_info (asserts, op0, op0, comp_code, op1);
5192 /* Add asserts for NAME cmp CST and NAME being defined
5193 as NAME = (int) NAME2. */
5194 if (!TYPE_UNSIGNED (TREE_TYPE (val))
5195 && (comp_code == LE_EXPR || comp_code == LT_EXPR
5196 || comp_code == GT_EXPR || comp_code == GE_EXPR)
5197 && gimple_assign_cast_p (def_stmt))
5199 name2 = gimple_assign_rhs1 (def_stmt);
5200 if (CONVERT_EXPR_CODE_P (rhs_code)
5201 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
5202 && TYPE_UNSIGNED (TREE_TYPE (name2))
5203 && prec == TYPE_PRECISION (TREE_TYPE (name2))
5204 && (comp_code == LE_EXPR || comp_code == GT_EXPR
5205 || !tree_int_cst_equal (val,
5206 TYPE_MIN_VALUE (TREE_TYPE (val)))))
5208 tree tmp, cst;
5209 enum tree_code new_comp_code = comp_code;
5211 cst = fold_convert (TREE_TYPE (name2),
5212 TYPE_MIN_VALUE (TREE_TYPE (val)));
5213 /* Build an expression for the range test. */
5214 tmp = build2 (PLUS_EXPR, TREE_TYPE (name2), name2, cst);
5215 cst = fold_build2 (PLUS_EXPR, TREE_TYPE (name2), cst,
5216 fold_convert (TREE_TYPE (name2), val));
5217 if (comp_code == LT_EXPR || comp_code == GE_EXPR)
5219 new_comp_code = comp_code == LT_EXPR ? LE_EXPR : GT_EXPR;
5220 cst = fold_build2 (MINUS_EXPR, TREE_TYPE (name2), cst,
5221 build_int_cst (TREE_TYPE (name2), 1));
5224 if (dump_file)
5226 fprintf (dump_file, "Adding assert for ");
5227 print_generic_expr (dump_file, name2);
5228 fprintf (dump_file, " from ");
5229 print_generic_expr (dump_file, tmp);
5230 fprintf (dump_file, "\n");
5233 add_assert_info (asserts, name2, tmp, new_comp_code, cst);
5237 /* Add asserts for NAME cmp CST and NAME being defined as
5238 NAME = NAME2 >> CST2.
5240 Extract CST2 from the right shift. */
5241 if (rhs_code == RSHIFT_EXPR)
5243 name2 = gimple_assign_rhs1 (def_stmt);
5244 cst2 = gimple_assign_rhs2 (def_stmt);
5245 if (TREE_CODE (name2) == SSA_NAME
5246 && tree_fits_uhwi_p (cst2)
5247 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
5248 && IN_RANGE (tree_to_uhwi (cst2), 1, prec - 1)
5249 && type_has_mode_precision_p (TREE_TYPE (val)))
5251 mask = wi::mask (tree_to_uhwi (cst2), false, prec);
5252 val2 = fold_binary (LSHIFT_EXPR, TREE_TYPE (val), val, cst2);
5255 if (val2 != NULL_TREE
5256 && TREE_CODE (val2) == INTEGER_CST
5257 && simple_cst_equal (fold_build2 (RSHIFT_EXPR,
5258 TREE_TYPE (val),
5259 val2, cst2), val))
5261 enum tree_code new_comp_code = comp_code;
5262 tree tmp, new_val;
5264 tmp = name2;
5265 if (comp_code == EQ_EXPR || comp_code == NE_EXPR)
5267 if (!TYPE_UNSIGNED (TREE_TYPE (val)))
5269 tree type = build_nonstandard_integer_type (prec, 1);
5270 tmp = build1 (NOP_EXPR, type, name2);
5271 val2 = fold_convert (type, val2);
5273 tmp = fold_build2 (MINUS_EXPR, TREE_TYPE (tmp), tmp, val2);
5274 new_val = wide_int_to_tree (TREE_TYPE (tmp), mask);
5275 new_comp_code = comp_code == EQ_EXPR ? LE_EXPR : GT_EXPR;
5277 else if (comp_code == LT_EXPR || comp_code == GE_EXPR)
5279 wide_int minval
5280 = wi::min_value (prec, TYPE_SIGN (TREE_TYPE (val)));
5281 new_val = val2;
5282 if (minval == new_val)
5283 new_val = NULL_TREE;
5285 else
5287 wide_int maxval
5288 = wi::max_value (prec, TYPE_SIGN (TREE_TYPE (val)));
5289 mask |= val2;
5290 if (mask == maxval)
5291 new_val = NULL_TREE;
5292 else
5293 new_val = wide_int_to_tree (TREE_TYPE (val2), mask);
5296 if (new_val)
5298 if (dump_file)
5300 fprintf (dump_file, "Adding assert for ");
5301 print_generic_expr (dump_file, name2);
5302 fprintf (dump_file, " from ");
5303 print_generic_expr (dump_file, tmp);
5304 fprintf (dump_file, "\n");
5307 add_assert_info (asserts, name2, tmp, new_comp_code, new_val);
5311 /* Add asserts for NAME cmp CST and NAME being defined as
5312 NAME = NAME2 & CST2.
5314 Extract CST2 from the and.
5316 Also handle
5317 NAME = (unsigned) NAME2;
5318 casts where NAME's type is unsigned and has smaller precision
5319 than NAME2's type as if it was NAME = NAME2 & MASK. */
5320 names[0] = NULL_TREE;
5321 names[1] = NULL_TREE;
5322 cst2 = NULL_TREE;
5323 if (rhs_code == BIT_AND_EXPR
5324 || (CONVERT_EXPR_CODE_P (rhs_code)
5325 && INTEGRAL_TYPE_P (TREE_TYPE (val))
5326 && TYPE_UNSIGNED (TREE_TYPE (val))
5327 && TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt)))
5328 > prec))
5330 name2 = gimple_assign_rhs1 (def_stmt);
5331 if (rhs_code == BIT_AND_EXPR)
5332 cst2 = gimple_assign_rhs2 (def_stmt);
5333 else
5335 cst2 = TYPE_MAX_VALUE (TREE_TYPE (val));
5336 nprec = TYPE_PRECISION (TREE_TYPE (name2));
5338 if (TREE_CODE (name2) == SSA_NAME
5339 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
5340 && TREE_CODE (cst2) == INTEGER_CST
5341 && !integer_zerop (cst2)
5342 && (nprec > 1
5343 || TYPE_UNSIGNED (TREE_TYPE (val))))
5345 gimple *def_stmt2 = SSA_NAME_DEF_STMT (name2);
5346 if (gimple_assign_cast_p (def_stmt2))
5348 names[1] = gimple_assign_rhs1 (def_stmt2);
5349 if (!CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt2))
5350 || !INTEGRAL_TYPE_P (TREE_TYPE (names[1]))
5351 || (TYPE_PRECISION (TREE_TYPE (name2))
5352 != TYPE_PRECISION (TREE_TYPE (names[1]))))
5353 names[1] = NULL_TREE;
5355 names[0] = name2;
5358 if (names[0] || names[1])
5360 wide_int minv, maxv, valv, cst2v;
5361 wide_int tem, sgnbit;
5362 bool valid_p = false, valn, cst2n;
5363 enum tree_code ccode = comp_code;
5365 valv = wide_int::from (val, nprec, UNSIGNED);
5366 cst2v = wide_int::from (cst2, nprec, UNSIGNED);
5367 valn = wi::neg_p (valv, TYPE_SIGN (TREE_TYPE (val)));
5368 cst2n = wi::neg_p (cst2v, TYPE_SIGN (TREE_TYPE (val)));
5369 /* If CST2 doesn't have most significant bit set,
5370 but VAL is negative, we have comparison like
5371 if ((x & 0x123) > -4) (always true). Just give up. */
5372 if (!cst2n && valn)
5373 ccode = ERROR_MARK;
5374 if (cst2n)
5375 sgnbit = wi::set_bit_in_zero (nprec - 1, nprec);
5376 else
5377 sgnbit = wi::zero (nprec);
5378 minv = valv & cst2v;
5379 switch (ccode)
5381 case EQ_EXPR:
5382 /* Minimum unsigned value for equality is VAL & CST2
5383 (should be equal to VAL, otherwise we probably should
5384 have folded the comparison into false) and
5385 maximum unsigned value is VAL | ~CST2. */
5386 maxv = valv | ~cst2v;
5387 valid_p = true;
5388 break;
5390 case NE_EXPR:
5391 tem = valv | ~cst2v;
5392 /* If VAL is 0, handle (X & CST2) != 0 as (X & CST2) > 0U. */
5393 if (valv == 0)
5395 cst2n = false;
5396 sgnbit = wi::zero (nprec);
5397 goto gt_expr;
5399 /* If (VAL | ~CST2) is all ones, handle it as
5400 (X & CST2) < VAL. */
5401 if (tem == -1)
5403 cst2n = false;
5404 valn = false;
5405 sgnbit = wi::zero (nprec);
5406 goto lt_expr;
5408 if (!cst2n && wi::neg_p (cst2v))
5409 sgnbit = wi::set_bit_in_zero (nprec - 1, nprec);
5410 if (sgnbit != 0)
5412 if (valv == sgnbit)
5414 cst2n = true;
5415 valn = true;
5416 goto gt_expr;
5418 if (tem == wi::mask (nprec - 1, false, nprec))
5420 cst2n = true;
5421 goto lt_expr;
5423 if (!cst2n)
5424 sgnbit = wi::zero (nprec);
5426 break;
5428 case GE_EXPR:
5429 /* Minimum unsigned value for >= if (VAL & CST2) == VAL
5430 is VAL and maximum unsigned value is ~0. For signed
5431 comparison, if CST2 doesn't have most significant bit
5432 set, handle it similarly. If CST2 has MSB set,
5433 the minimum is the same, and maximum is ~0U/2. */
5434 if (minv != valv)
5436 /* If (VAL & CST2) != VAL, X & CST2 can't be equal to
5437 VAL. */
5438 minv = masked_increment (valv, cst2v, sgnbit, nprec);
5439 if (minv == valv)
5440 break;
5442 maxv = wi::mask (nprec - (cst2n ? 1 : 0), false, nprec);
5443 valid_p = true;
5444 break;
5446 case GT_EXPR:
5447 gt_expr:
5448 /* Find out smallest MINV where MINV > VAL
5449 && (MINV & CST2) == MINV, if any. If VAL is signed and
5450 CST2 has MSB set, compute it biased by 1 << (nprec - 1). */
5451 minv = masked_increment (valv, cst2v, sgnbit, nprec);
5452 if (minv == valv)
5453 break;
5454 maxv = wi::mask (nprec - (cst2n ? 1 : 0), false, nprec);
5455 valid_p = true;
5456 break;
5458 case LE_EXPR:
5459 /* Minimum unsigned value for <= is 0 and maximum
5460 unsigned value is VAL | ~CST2 if (VAL & CST2) == VAL.
5461 Otherwise, find smallest VAL2 where VAL2 > VAL
5462 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
5463 as maximum.
5464 For signed comparison, if CST2 doesn't have most
5465 significant bit set, handle it similarly. If CST2 has
5466 MSB set, the maximum is the same and minimum is INT_MIN. */
5467 if (minv == valv)
5468 maxv = valv;
5469 else
5471 maxv = masked_increment (valv, cst2v, sgnbit, nprec);
5472 if (maxv == valv)
5473 break;
5474 maxv -= 1;
5476 maxv |= ~cst2v;
5477 minv = sgnbit;
5478 valid_p = true;
5479 break;
5481 case LT_EXPR:
5482 lt_expr:
5483 /* Minimum unsigned value for < is 0 and maximum
5484 unsigned value is (VAL-1) | ~CST2 if (VAL & CST2) == VAL.
5485 Otherwise, find smallest VAL2 where VAL2 > VAL
5486 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
5487 as maximum.
5488 For signed comparison, if CST2 doesn't have most
5489 significant bit set, handle it similarly. If CST2 has
5490 MSB set, the maximum is the same and minimum is INT_MIN. */
5491 if (minv == valv)
5493 if (valv == sgnbit)
5494 break;
5495 maxv = valv;
5497 else
5499 maxv = masked_increment (valv, cst2v, sgnbit, nprec);
5500 if (maxv == valv)
5501 break;
5503 maxv -= 1;
5504 maxv |= ~cst2v;
5505 minv = sgnbit;
5506 valid_p = true;
5507 break;
5509 default:
5510 break;
5512 if (valid_p
5513 && (maxv - minv) != -1)
5515 tree tmp, new_val, type;
5516 int i;
5518 for (i = 0; i < 2; i++)
5519 if (names[i])
5521 wide_int maxv2 = maxv;
5522 tmp = names[i];
5523 type = TREE_TYPE (names[i]);
5524 if (!TYPE_UNSIGNED (type))
5526 type = build_nonstandard_integer_type (nprec, 1);
5527 tmp = build1 (NOP_EXPR, type, names[i]);
5529 if (minv != 0)
5531 tmp = build2 (PLUS_EXPR, type, tmp,
5532 wide_int_to_tree (type, -minv));
5533 maxv2 = maxv - minv;
5535 new_val = wide_int_to_tree (type, maxv2);
5537 if (dump_file)
5539 fprintf (dump_file, "Adding assert for ");
5540 print_generic_expr (dump_file, names[i]);
5541 fprintf (dump_file, " from ");
5542 print_generic_expr (dump_file, tmp);
5543 fprintf (dump_file, "\n");
5546 add_assert_info (asserts, names[i], tmp, LE_EXPR, new_val);
5553 /* OP is an operand of a truth value expression which is known to have
5554 a particular value. Register any asserts for OP and for any
5555 operands in OP's defining statement.
5557 If CODE is EQ_EXPR, then we want to register OP is zero (false),
5558 if CODE is NE_EXPR, then we want to register OP is nonzero (true). */
5560 static void
5561 register_edge_assert_for_1 (tree op, enum tree_code code,
5562 edge e, vec<assert_info> &asserts)
5564 gimple *op_def;
5565 tree val;
5566 enum tree_code rhs_code;
5568 /* We only care about SSA_NAMEs. */
5569 if (TREE_CODE (op) != SSA_NAME)
5570 return;
5572 /* We know that OP will have a zero or nonzero value. */
5573 val = build_int_cst (TREE_TYPE (op), 0);
5574 add_assert_info (asserts, op, op, code, val);
5576 /* Now look at how OP is set. If it's set from a comparison,
5577 a truth operation or some bit operations, then we may be able
5578 to register information about the operands of that assignment. */
5579 op_def = SSA_NAME_DEF_STMT (op);
5580 if (gimple_code (op_def) != GIMPLE_ASSIGN)
5581 return;
5583 rhs_code = gimple_assign_rhs_code (op_def);
5585 if (TREE_CODE_CLASS (rhs_code) == tcc_comparison)
5587 bool invert = (code == EQ_EXPR ? true : false);
5588 tree op0 = gimple_assign_rhs1 (op_def);
5589 tree op1 = gimple_assign_rhs2 (op_def);
5591 if (TREE_CODE (op0) == SSA_NAME)
5592 register_edge_assert_for_2 (op0, e, rhs_code, op0, op1, invert, asserts);
5593 if (TREE_CODE (op1) == SSA_NAME)
5594 register_edge_assert_for_2 (op1, e, rhs_code, op0, op1, invert, asserts);
5596 else if ((code == NE_EXPR
5597 && gimple_assign_rhs_code (op_def) == BIT_AND_EXPR)
5598 || (code == EQ_EXPR
5599 && gimple_assign_rhs_code (op_def) == BIT_IOR_EXPR))
5601 /* Recurse on each operand. */
5602 tree op0 = gimple_assign_rhs1 (op_def);
5603 tree op1 = gimple_assign_rhs2 (op_def);
5604 if (TREE_CODE (op0) == SSA_NAME
5605 && has_single_use (op0))
5606 register_edge_assert_for_1 (op0, code, e, asserts);
5607 if (TREE_CODE (op1) == SSA_NAME
5608 && has_single_use (op1))
5609 register_edge_assert_for_1 (op1, code, e, asserts);
5611 else if (gimple_assign_rhs_code (op_def) == BIT_NOT_EXPR
5612 && TYPE_PRECISION (TREE_TYPE (gimple_assign_lhs (op_def))) == 1)
5614 /* Recurse, flipping CODE. */
5615 code = invert_tree_comparison (code, false);
5616 register_edge_assert_for_1 (gimple_assign_rhs1 (op_def), code, e, asserts);
5618 else if (gimple_assign_rhs_code (op_def) == SSA_NAME)
5620 /* Recurse through the copy. */
5621 register_edge_assert_for_1 (gimple_assign_rhs1 (op_def), code, e, asserts);
5623 else if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (op_def)))
5625 /* Recurse through the type conversion, unless it is a narrowing
5626 conversion or conversion from non-integral type. */
5627 tree rhs = gimple_assign_rhs1 (op_def);
5628 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs))
5629 && (TYPE_PRECISION (TREE_TYPE (rhs))
5630 <= TYPE_PRECISION (TREE_TYPE (op))))
5631 register_edge_assert_for_1 (rhs, code, e, asserts);
5635 /* Check if comparison
5636 NAME COND_OP INTEGER_CST
5637 has a form of
5638 (X & 11...100..0) COND_OP XX...X00...0
5639 Such comparison can yield assertions like
5640 X >= XX...X00...0
5641 X <= XX...X11...1
5642 in case of COND_OP being NE_EXPR or
5643 X < XX...X00...0
5644 X > XX...X11...1
5645 in case of EQ_EXPR. */
5647 static bool
5648 is_masked_range_test (tree name, tree valt, enum tree_code cond_code,
5649 tree *new_name, tree *low, enum tree_code *low_code,
5650 tree *high, enum tree_code *high_code)
5652 gimple *def_stmt = SSA_NAME_DEF_STMT (name);
5654 if (!is_gimple_assign (def_stmt)
5655 || gimple_assign_rhs_code (def_stmt) != BIT_AND_EXPR)
5656 return false;
5658 tree t = gimple_assign_rhs1 (def_stmt);
5659 tree maskt = gimple_assign_rhs2 (def_stmt);
5660 if (TREE_CODE (t) != SSA_NAME || TREE_CODE (maskt) != INTEGER_CST)
5661 return false;
5663 wide_int mask = maskt;
5664 wide_int inv_mask = ~mask;
5665 wide_int val = valt; // Assume VALT is INTEGER_CST
5667 if ((inv_mask & (inv_mask + 1)) != 0
5668 || (val & mask) != val)
5669 return false;
5671 bool is_range = cond_code == EQ_EXPR;
5673 tree type = TREE_TYPE (t);
5674 wide_int min = wi::min_value (type),
5675 max = wi::max_value (type);
5677 if (is_range)
5679 *low_code = val == min ? ERROR_MARK : GE_EXPR;
5680 *high_code = val == max ? ERROR_MARK : LE_EXPR;
5682 else
5684 /* We can still generate assertion if one of alternatives
5685 is known to always be false. */
5686 if (val == min)
5688 *low_code = (enum tree_code) 0;
5689 *high_code = GT_EXPR;
5691 else if ((val | inv_mask) == max)
5693 *low_code = LT_EXPR;
5694 *high_code = (enum tree_code) 0;
5696 else
5697 return false;
5700 *new_name = t;
5701 *low = wide_int_to_tree (type, val);
5702 *high = wide_int_to_tree (type, val | inv_mask);
5704 if (wi::neg_p (val, TYPE_SIGN (type)))
5705 std::swap (*low, *high);
5707 return true;
5710 /* Try to register an edge assertion for SSA name NAME on edge E for
5711 the condition COND contributing to the conditional jump pointed to by
5712 SI. */
5714 static void
5715 register_edge_assert_for (tree name, edge e,
5716 enum tree_code cond_code, tree cond_op0,
5717 tree cond_op1, vec<assert_info> &asserts)
5719 tree val;
5720 enum tree_code comp_code;
5721 bool is_else_edge = (e->flags & EDGE_FALSE_VALUE) != 0;
5723 /* Do not attempt to infer anything in names that flow through
5724 abnormal edges. */
5725 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name))
5726 return;
5728 if (!extract_code_and_val_from_cond_with_ops (name, cond_code,
5729 cond_op0, cond_op1,
5730 is_else_edge,
5731 &comp_code, &val))
5732 return;
5734 /* Register ASSERT_EXPRs for name. */
5735 register_edge_assert_for_2 (name, e, cond_code, cond_op0,
5736 cond_op1, is_else_edge, asserts);
5739 /* If COND is effectively an equality test of an SSA_NAME against
5740 the value zero or one, then we may be able to assert values
5741 for SSA_NAMEs which flow into COND. */
5743 /* In the case of NAME == 1 or NAME != 0, for BIT_AND_EXPR defining
5744 statement of NAME we can assert both operands of the BIT_AND_EXPR
5745 have nonzero value. */
5746 if (((comp_code == EQ_EXPR && integer_onep (val))
5747 || (comp_code == NE_EXPR && integer_zerop (val))))
5749 gimple *def_stmt = SSA_NAME_DEF_STMT (name);
5751 if (is_gimple_assign (def_stmt)
5752 && gimple_assign_rhs_code (def_stmt) == BIT_AND_EXPR)
5754 tree op0 = gimple_assign_rhs1 (def_stmt);
5755 tree op1 = gimple_assign_rhs2 (def_stmt);
5756 register_edge_assert_for_1 (op0, NE_EXPR, e, asserts);
5757 register_edge_assert_for_1 (op1, NE_EXPR, e, asserts);
5761 /* In the case of NAME == 0 or NAME != 1, for BIT_IOR_EXPR defining
5762 statement of NAME we can assert both operands of the BIT_IOR_EXPR
5763 have zero value. */
5764 if (((comp_code == EQ_EXPR && integer_zerop (val))
5765 || (comp_code == NE_EXPR && integer_onep (val))))
5767 gimple *def_stmt = SSA_NAME_DEF_STMT (name);
5769 /* For BIT_IOR_EXPR only if NAME == 0 both operands have
5770 necessarily zero value, or if type-precision is one. */
5771 if (is_gimple_assign (def_stmt)
5772 && (gimple_assign_rhs_code (def_stmt) == BIT_IOR_EXPR
5773 && (TYPE_PRECISION (TREE_TYPE (name)) == 1
5774 || comp_code == EQ_EXPR)))
5776 tree op0 = gimple_assign_rhs1 (def_stmt);
5777 tree op1 = gimple_assign_rhs2 (def_stmt);
5778 register_edge_assert_for_1 (op0, EQ_EXPR, e, asserts);
5779 register_edge_assert_for_1 (op1, EQ_EXPR, e, asserts);
5783 /* Sometimes we can infer ranges from (NAME & MASK) == VALUE. */
5784 if ((comp_code == EQ_EXPR || comp_code == NE_EXPR)
5785 && TREE_CODE (val) == INTEGER_CST)
5787 enum tree_code low_code, high_code;
5788 tree low, high;
5789 if (is_masked_range_test (name, val, comp_code, &name, &low,
5790 &low_code, &high, &high_code))
5792 if (low_code != ERROR_MARK)
5793 register_edge_assert_for_2 (name, e, low_code, name,
5794 low, /*invert*/false, asserts);
5795 if (high_code != ERROR_MARK)
5796 register_edge_assert_for_2 (name, e, high_code, name,
5797 high, /*invert*/false, asserts);
5802 /* Finish found ASSERTS for E and register them at GSI. */
5804 static void
5805 finish_register_edge_assert_for (edge e, gimple_stmt_iterator gsi,
5806 vec<assert_info> &asserts)
5808 for (unsigned i = 0; i < asserts.length (); ++i)
5809 /* Only register an ASSERT_EXPR if NAME was found in the sub-graph
5810 reachable from E. */
5811 if (live_on_edge (e, asserts[i].name))
5812 register_new_assert_for (asserts[i].name, asserts[i].expr,
5813 asserts[i].comp_code, asserts[i].val,
5814 NULL, e, gsi);
5819 /* Determine whether the outgoing edges of BB should receive an
5820 ASSERT_EXPR for each of the operands of BB's LAST statement.
5821 The last statement of BB must be a COND_EXPR.
5823 If any of the sub-graphs rooted at BB have an interesting use of
5824 the predicate operands, an assert location node is added to the
5825 list of assertions for the corresponding operands. */
5827 static void
5828 find_conditional_asserts (basic_block bb, gcond *last)
5830 gimple_stmt_iterator bsi;
5831 tree op;
5832 edge_iterator ei;
5833 edge e;
5834 ssa_op_iter iter;
5836 bsi = gsi_for_stmt (last);
5838 /* Look for uses of the operands in each of the sub-graphs
5839 rooted at BB. We need to check each of the outgoing edges
5840 separately, so that we know what kind of ASSERT_EXPR to
5841 insert. */
5842 FOR_EACH_EDGE (e, ei, bb->succs)
5844 if (e->dest == bb)
5845 continue;
5847 /* Register the necessary assertions for each operand in the
5848 conditional predicate. */
5849 auto_vec<assert_info, 8> asserts;
5850 FOR_EACH_SSA_TREE_OPERAND (op, last, iter, SSA_OP_USE)
5851 register_edge_assert_for (op, e,
5852 gimple_cond_code (last),
5853 gimple_cond_lhs (last),
5854 gimple_cond_rhs (last), asserts);
5855 finish_register_edge_assert_for (e, bsi, asserts);
5859 struct case_info
5861 tree expr;
5862 basic_block bb;
5865 /* Compare two case labels sorting first by the destination bb index
5866 and then by the case value. */
5868 static int
5869 compare_case_labels (const void *p1, const void *p2)
5871 const struct case_info *ci1 = (const struct case_info *) p1;
5872 const struct case_info *ci2 = (const struct case_info *) p2;
5873 int idx1 = ci1->bb->index;
5874 int idx2 = ci2->bb->index;
5876 if (idx1 < idx2)
5877 return -1;
5878 else if (idx1 == idx2)
5880 /* Make sure the default label is first in a group. */
5881 if (!CASE_LOW (ci1->expr))
5882 return -1;
5883 else if (!CASE_LOW (ci2->expr))
5884 return 1;
5885 else
5886 return tree_int_cst_compare (CASE_LOW (ci1->expr),
5887 CASE_LOW (ci2->expr));
5889 else
5890 return 1;
5893 /* Determine whether the outgoing edges of BB should receive an
5894 ASSERT_EXPR for each of the operands of BB's LAST statement.
5895 The last statement of BB must be a SWITCH_EXPR.
5897 If any of the sub-graphs rooted at BB have an interesting use of
5898 the predicate operands, an assert location node is added to the
5899 list of assertions for the corresponding operands. */
5901 static void
5902 find_switch_asserts (basic_block bb, gswitch *last)
5904 gimple_stmt_iterator bsi;
5905 tree op;
5906 edge e;
5907 struct case_info *ci;
5908 size_t n = gimple_switch_num_labels (last);
5909 #if GCC_VERSION >= 4000
5910 unsigned int idx;
5911 #else
5912 /* Work around GCC 3.4 bug (PR 37086). */
5913 volatile unsigned int idx;
5914 #endif
5916 bsi = gsi_for_stmt (last);
5917 op = gimple_switch_index (last);
5918 if (TREE_CODE (op) != SSA_NAME)
5919 return;
5921 /* Build a vector of case labels sorted by destination label. */
5922 ci = XNEWVEC (struct case_info, n);
5923 for (idx = 0; idx < n; ++idx)
5925 ci[idx].expr = gimple_switch_label (last, idx);
5926 ci[idx].bb = label_to_block (CASE_LABEL (ci[idx].expr));
5928 edge default_edge = find_edge (bb, ci[0].bb);
5929 qsort (ci, n, sizeof (struct case_info), compare_case_labels);
5931 for (idx = 0; idx < n; ++idx)
5933 tree min, max;
5934 tree cl = ci[idx].expr;
5935 basic_block cbb = ci[idx].bb;
5937 min = CASE_LOW (cl);
5938 max = CASE_HIGH (cl);
5940 /* If there are multiple case labels with the same destination
5941 we need to combine them to a single value range for the edge. */
5942 if (idx + 1 < n && cbb == ci[idx + 1].bb)
5944 /* Skip labels until the last of the group. */
5945 do {
5946 ++idx;
5947 } while (idx < n && cbb == ci[idx].bb);
5948 --idx;
5950 /* Pick up the maximum of the case label range. */
5951 if (CASE_HIGH (ci[idx].expr))
5952 max = CASE_HIGH (ci[idx].expr);
5953 else
5954 max = CASE_LOW (ci[idx].expr);
5957 /* Can't extract a useful assertion out of a range that includes the
5958 default label. */
5959 if (min == NULL_TREE)
5960 continue;
5962 /* Find the edge to register the assert expr on. */
5963 e = find_edge (bb, cbb);
5965 /* Register the necessary assertions for the operand in the
5966 SWITCH_EXPR. */
5967 auto_vec<assert_info, 8> asserts;
5968 register_edge_assert_for (op, e,
5969 max ? GE_EXPR : EQ_EXPR,
5970 op, fold_convert (TREE_TYPE (op), min),
5971 asserts);
5972 if (max)
5973 register_edge_assert_for (op, e, LE_EXPR, op,
5974 fold_convert (TREE_TYPE (op), max),
5975 asserts);
5976 finish_register_edge_assert_for (e, bsi, asserts);
5979 XDELETEVEC (ci);
5981 if (!live_on_edge (default_edge, op))
5982 return;
5984 /* Now register along the default label assertions that correspond to the
5985 anti-range of each label. */
5986 int insertion_limit = PARAM_VALUE (PARAM_MAX_VRP_SWITCH_ASSERTIONS);
5987 if (insertion_limit == 0)
5988 return;
5990 /* We can't do this if the default case shares a label with another case. */
5991 tree default_cl = gimple_switch_default_label (last);
5992 for (idx = 1; idx < n; idx++)
5994 tree min, max;
5995 tree cl = gimple_switch_label (last, idx);
5996 if (CASE_LABEL (cl) == CASE_LABEL (default_cl))
5997 continue;
5999 min = CASE_LOW (cl);
6000 max = CASE_HIGH (cl);
6002 /* Combine contiguous case ranges to reduce the number of assertions
6003 to insert. */
6004 for (idx = idx + 1; idx < n; idx++)
6006 tree next_min, next_max;
6007 tree next_cl = gimple_switch_label (last, idx);
6008 if (CASE_LABEL (next_cl) == CASE_LABEL (default_cl))
6009 break;
6011 next_min = CASE_LOW (next_cl);
6012 next_max = CASE_HIGH (next_cl);
6014 wide_int difference = wi::sub (next_min, max ? max : min);
6015 if (wi::eq_p (difference, 1))
6016 max = next_max ? next_max : next_min;
6017 else
6018 break;
6020 idx--;
6022 if (max == NULL_TREE)
6024 /* Register the assertion OP != MIN. */
6025 auto_vec<assert_info, 8> asserts;
6026 min = fold_convert (TREE_TYPE (op), min);
6027 register_edge_assert_for (op, default_edge, NE_EXPR, op, min,
6028 asserts);
6029 finish_register_edge_assert_for (default_edge, bsi, asserts);
6031 else
6033 /* Register the assertion (unsigned)OP - MIN > (MAX - MIN),
6034 which will give OP the anti-range ~[MIN,MAX]. */
6035 tree uop = fold_convert (unsigned_type_for (TREE_TYPE (op)), op);
6036 min = fold_convert (TREE_TYPE (uop), min);
6037 max = fold_convert (TREE_TYPE (uop), max);
6039 tree lhs = fold_build2 (MINUS_EXPR, TREE_TYPE (uop), uop, min);
6040 tree rhs = int_const_binop (MINUS_EXPR, max, min);
6041 register_new_assert_for (op, lhs, GT_EXPR, rhs,
6042 NULL, default_edge, bsi);
6045 if (--insertion_limit == 0)
6046 break;
6051 /* Traverse all the statements in block BB looking for statements that
6052 may generate useful assertions for the SSA names in their operand.
6053 If a statement produces a useful assertion A for name N_i, then the
6054 list of assertions already generated for N_i is scanned to
6055 determine if A is actually needed.
6057 If N_i already had the assertion A at a location dominating the
6058 current location, then nothing needs to be done. Otherwise, the
6059 new location for A is recorded instead.
6061 1- For every statement S in BB, all the variables used by S are
6062 added to bitmap FOUND_IN_SUBGRAPH.
6064 2- If statement S uses an operand N in a way that exposes a known
6065 value range for N, then if N was not already generated by an
6066 ASSERT_EXPR, create a new assert location for N. For instance,
6067 if N is a pointer and the statement dereferences it, we can
6068 assume that N is not NULL.
6070 3- COND_EXPRs are a special case of #2. We can derive range
6071 information from the predicate but need to insert different
6072 ASSERT_EXPRs for each of the sub-graphs rooted at the
6073 conditional block. If the last statement of BB is a conditional
6074 expression of the form 'X op Y', then
6076 a) Remove X and Y from the set FOUND_IN_SUBGRAPH.
6078 b) If the conditional is the only entry point to the sub-graph
6079 corresponding to the THEN_CLAUSE, recurse into it. On
6080 return, if X and/or Y are marked in FOUND_IN_SUBGRAPH, then
6081 an ASSERT_EXPR is added for the corresponding variable.
6083 c) Repeat step (b) on the ELSE_CLAUSE.
6085 d) Mark X and Y in FOUND_IN_SUBGRAPH.
6087 For instance,
6089 if (a == 9)
6090 b = a;
6091 else
6092 b = c + 1;
6094 In this case, an assertion on the THEN clause is useful to
6095 determine that 'a' is always 9 on that edge. However, an assertion
6096 on the ELSE clause would be unnecessary.
6098 4- If BB does not end in a conditional expression, then we recurse
6099 into BB's dominator children.
6101 At the end of the recursive traversal, every SSA name will have a
6102 list of locations where ASSERT_EXPRs should be added. When a new
6103 location for name N is found, it is registered by calling
6104 register_new_assert_for. That function keeps track of all the
6105 registered assertions to prevent adding unnecessary assertions.
6106 For instance, if a pointer P_4 is dereferenced more than once in a
6107 dominator tree, only the location dominating all the dereference of
6108 P_4 will receive an ASSERT_EXPR. */
6110 static void
6111 find_assert_locations_1 (basic_block bb, sbitmap live)
6113 gimple *last;
6115 last = last_stmt (bb);
6117 /* If BB's last statement is a conditional statement involving integer
6118 operands, determine if we need to add ASSERT_EXPRs. */
6119 if (last
6120 && gimple_code (last) == GIMPLE_COND
6121 && !fp_predicate (last)
6122 && !ZERO_SSA_OPERANDS (last, SSA_OP_USE))
6123 find_conditional_asserts (bb, as_a <gcond *> (last));
6125 /* If BB's last statement is a switch statement involving integer
6126 operands, determine if we need to add ASSERT_EXPRs. */
6127 if (last
6128 && gimple_code (last) == GIMPLE_SWITCH
6129 && !ZERO_SSA_OPERANDS (last, SSA_OP_USE))
6130 find_switch_asserts (bb, as_a <gswitch *> (last));
6132 /* Traverse all the statements in BB marking used names and looking
6133 for statements that may infer assertions for their used operands. */
6134 for (gimple_stmt_iterator si = gsi_last_bb (bb); !gsi_end_p (si);
6135 gsi_prev (&si))
6137 gimple *stmt;
6138 tree op;
6139 ssa_op_iter i;
6141 stmt = gsi_stmt (si);
6143 if (is_gimple_debug (stmt))
6144 continue;
6146 /* See if we can derive an assertion for any of STMT's operands. */
6147 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_USE)
6149 tree value;
6150 enum tree_code comp_code;
6152 /* If op is not live beyond this stmt, do not bother to insert
6153 asserts for it. */
6154 if (!bitmap_bit_p (live, SSA_NAME_VERSION (op)))
6155 continue;
6157 /* If OP is used in such a way that we can infer a value
6158 range for it, and we don't find a previous assertion for
6159 it, create a new assertion location node for OP. */
6160 if (infer_value_range (stmt, op, &comp_code, &value))
6162 /* If we are able to infer a nonzero value range for OP,
6163 then walk backwards through the use-def chain to see if OP
6164 was set via a typecast.
6166 If so, then we can also infer a nonzero value range
6167 for the operand of the NOP_EXPR. */
6168 if (comp_code == NE_EXPR && integer_zerop (value))
6170 tree t = op;
6171 gimple *def_stmt = SSA_NAME_DEF_STMT (t);
6173 while (is_gimple_assign (def_stmt)
6174 && CONVERT_EXPR_CODE_P
6175 (gimple_assign_rhs_code (def_stmt))
6176 && TREE_CODE
6177 (gimple_assign_rhs1 (def_stmt)) == SSA_NAME
6178 && POINTER_TYPE_P
6179 (TREE_TYPE (gimple_assign_rhs1 (def_stmt))))
6181 t = gimple_assign_rhs1 (def_stmt);
6182 def_stmt = SSA_NAME_DEF_STMT (t);
6184 /* Note we want to register the assert for the
6185 operand of the NOP_EXPR after SI, not after the
6186 conversion. */
6187 if (bitmap_bit_p (live, SSA_NAME_VERSION (t)))
6188 register_new_assert_for (t, t, comp_code, value,
6189 bb, NULL, si);
6193 register_new_assert_for (op, op, comp_code, value, bb, NULL, si);
6197 /* Update live. */
6198 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_USE)
6199 bitmap_set_bit (live, SSA_NAME_VERSION (op));
6200 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_DEF)
6201 bitmap_clear_bit (live, SSA_NAME_VERSION (op));
6204 /* Traverse all PHI nodes in BB, updating live. */
6205 for (gphi_iterator si = gsi_start_phis (bb); !gsi_end_p (si);
6206 gsi_next (&si))
6208 use_operand_p arg_p;
6209 ssa_op_iter i;
6210 gphi *phi = si.phi ();
6211 tree res = gimple_phi_result (phi);
6213 if (virtual_operand_p (res))
6214 continue;
6216 FOR_EACH_PHI_ARG (arg_p, phi, i, SSA_OP_USE)
6218 tree arg = USE_FROM_PTR (arg_p);
6219 if (TREE_CODE (arg) == SSA_NAME)
6220 bitmap_set_bit (live, SSA_NAME_VERSION (arg));
6223 bitmap_clear_bit (live, SSA_NAME_VERSION (res));
6227 /* Do an RPO walk over the function computing SSA name liveness
6228 on-the-fly and deciding on assert expressions to insert. */
6230 static void
6231 find_assert_locations (void)
6233 int *rpo = XNEWVEC (int, last_basic_block_for_fn (cfun));
6234 int *bb_rpo = XNEWVEC (int, last_basic_block_for_fn (cfun));
6235 int *last_rpo = XCNEWVEC (int, last_basic_block_for_fn (cfun));
6236 int rpo_cnt, i;
6238 live = XCNEWVEC (sbitmap, last_basic_block_for_fn (cfun));
6239 rpo_cnt = pre_and_rev_post_order_compute (NULL, rpo, false);
6240 for (i = 0; i < rpo_cnt; ++i)
6241 bb_rpo[rpo[i]] = i;
6243 /* Pre-seed loop latch liveness from loop header PHI nodes. Due to
6244 the order we compute liveness and insert asserts we otherwise
6245 fail to insert asserts into the loop latch. */
6246 loop_p loop;
6247 FOR_EACH_LOOP (loop, 0)
6249 i = loop->latch->index;
6250 unsigned int j = single_succ_edge (loop->latch)->dest_idx;
6251 for (gphi_iterator gsi = gsi_start_phis (loop->header);
6252 !gsi_end_p (gsi); gsi_next (&gsi))
6254 gphi *phi = gsi.phi ();
6255 if (virtual_operand_p (gimple_phi_result (phi)))
6256 continue;
6257 tree arg = gimple_phi_arg_def (phi, j);
6258 if (TREE_CODE (arg) == SSA_NAME)
6260 if (live[i] == NULL)
6262 live[i] = sbitmap_alloc (num_ssa_names);
6263 bitmap_clear (live[i]);
6265 bitmap_set_bit (live[i], SSA_NAME_VERSION (arg));
6270 for (i = rpo_cnt - 1; i >= 0; --i)
6272 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, rpo[i]);
6273 edge e;
6274 edge_iterator ei;
6276 if (!live[rpo[i]])
6278 live[rpo[i]] = sbitmap_alloc (num_ssa_names);
6279 bitmap_clear (live[rpo[i]]);
6282 /* Process BB and update the live information with uses in
6283 this block. */
6284 find_assert_locations_1 (bb, live[rpo[i]]);
6286 /* Merge liveness into the predecessor blocks and free it. */
6287 if (!bitmap_empty_p (live[rpo[i]]))
6289 int pred_rpo = i;
6290 FOR_EACH_EDGE (e, ei, bb->preds)
6292 int pred = e->src->index;
6293 if ((e->flags & EDGE_DFS_BACK) || pred == ENTRY_BLOCK)
6294 continue;
6296 if (!live[pred])
6298 live[pred] = sbitmap_alloc (num_ssa_names);
6299 bitmap_clear (live[pred]);
6301 bitmap_ior (live[pred], live[pred], live[rpo[i]]);
6303 if (bb_rpo[pred] < pred_rpo)
6304 pred_rpo = bb_rpo[pred];
6307 /* Record the RPO number of the last visited block that needs
6308 live information from this block. */
6309 last_rpo[rpo[i]] = pred_rpo;
6311 else
6313 sbitmap_free (live[rpo[i]]);
6314 live[rpo[i]] = NULL;
6317 /* We can free all successors live bitmaps if all their
6318 predecessors have been visited already. */
6319 FOR_EACH_EDGE (e, ei, bb->succs)
6320 if (last_rpo[e->dest->index] == i
6321 && live[e->dest->index])
6323 sbitmap_free (live[e->dest->index]);
6324 live[e->dest->index] = NULL;
6328 XDELETEVEC (rpo);
6329 XDELETEVEC (bb_rpo);
6330 XDELETEVEC (last_rpo);
6331 for (i = 0; i < last_basic_block_for_fn (cfun); ++i)
6332 if (live[i])
6333 sbitmap_free (live[i]);
6334 XDELETEVEC (live);
6337 /* Create an ASSERT_EXPR for NAME and insert it in the location
6338 indicated by LOC. Return true if we made any edge insertions. */
6340 static bool
6341 process_assert_insertions_for (tree name, assert_locus *loc)
6343 /* Build the comparison expression NAME_i COMP_CODE VAL. */
6344 gimple *stmt;
6345 tree cond;
6346 gimple *assert_stmt;
6347 edge_iterator ei;
6348 edge e;
6350 /* If we have X <=> X do not insert an assert expr for that. */
6351 if (loc->expr == loc->val)
6352 return false;
6354 cond = build2 (loc->comp_code, boolean_type_node, loc->expr, loc->val);
6355 assert_stmt = build_assert_expr_for (cond, name);
6356 if (loc->e)
6358 /* We have been asked to insert the assertion on an edge. This
6359 is used only by COND_EXPR and SWITCH_EXPR assertions. */
6360 gcc_checking_assert (gimple_code (gsi_stmt (loc->si)) == GIMPLE_COND
6361 || (gimple_code (gsi_stmt (loc->si))
6362 == GIMPLE_SWITCH));
6364 gsi_insert_on_edge (loc->e, assert_stmt);
6365 return true;
6368 /* If the stmt iterator points at the end then this is an insertion
6369 at the beginning of a block. */
6370 if (gsi_end_p (loc->si))
6372 gimple_stmt_iterator si = gsi_after_labels (loc->bb);
6373 gsi_insert_before (&si, assert_stmt, GSI_SAME_STMT);
6374 return false;
6377 /* Otherwise, we can insert right after LOC->SI iff the
6378 statement must not be the last statement in the block. */
6379 stmt = gsi_stmt (loc->si);
6380 if (!stmt_ends_bb_p (stmt))
6382 gsi_insert_after (&loc->si, assert_stmt, GSI_SAME_STMT);
6383 return false;
6386 /* If STMT must be the last statement in BB, we can only insert new
6387 assertions on the non-abnormal edge out of BB. Note that since
6388 STMT is not control flow, there may only be one non-abnormal/eh edge
6389 out of BB. */
6390 FOR_EACH_EDGE (e, ei, loc->bb->succs)
6391 if (!(e->flags & (EDGE_ABNORMAL|EDGE_EH)))
6393 gsi_insert_on_edge (e, assert_stmt);
6394 return true;
6397 gcc_unreachable ();
6400 /* Qsort helper for sorting assert locations. If stable is true, don't
6401 use iterative_hash_expr because it can be unstable for -fcompare-debug,
6402 on the other side some pointers might be NULL. */
6404 template <bool stable>
6405 static int
6406 compare_assert_loc (const void *pa, const void *pb)
6408 assert_locus * const a = *(assert_locus * const *)pa;
6409 assert_locus * const b = *(assert_locus * const *)pb;
6411 /* If stable, some asserts might be optimized away already, sort
6412 them last. */
6413 if (stable)
6415 if (a == NULL)
6416 return b != NULL;
6417 else if (b == NULL)
6418 return -1;
6421 if (a->e == NULL && b->e != NULL)
6422 return 1;
6423 else if (a->e != NULL && b->e == NULL)
6424 return -1;
6426 /* After the above checks, we know that (a->e == NULL) == (b->e == NULL),
6427 no need to test both a->e and b->e. */
6429 /* Sort after destination index. */
6430 if (a->e == NULL)
6432 else if (a->e->dest->index > b->e->dest->index)
6433 return 1;
6434 else if (a->e->dest->index < b->e->dest->index)
6435 return -1;
6437 /* Sort after comp_code. */
6438 if (a->comp_code > b->comp_code)
6439 return 1;
6440 else if (a->comp_code < b->comp_code)
6441 return -1;
6443 hashval_t ha, hb;
6445 /* E.g. if a->val is ADDR_EXPR of a VAR_DECL, iterative_hash_expr
6446 uses DECL_UID of the VAR_DECL, so sorting might differ between
6447 -g and -g0. When doing the removal of redundant assert exprs
6448 and commonization to successors, this does not matter, but for
6449 the final sort needs to be stable. */
6450 if (stable)
6452 ha = 0;
6453 hb = 0;
6455 else
6457 ha = iterative_hash_expr (a->expr, iterative_hash_expr (a->val, 0));
6458 hb = iterative_hash_expr (b->expr, iterative_hash_expr (b->val, 0));
6461 /* Break the tie using hashing and source/bb index. */
6462 if (ha == hb)
6463 return (a->e != NULL
6464 ? a->e->src->index - b->e->src->index
6465 : a->bb->index - b->bb->index);
6466 return ha > hb ? 1 : -1;
6469 /* Process all the insertions registered for every name N_i registered
6470 in NEED_ASSERT_FOR. The list of assertions to be inserted are
6471 found in ASSERTS_FOR[i]. */
6473 static void
6474 process_assert_insertions (void)
6476 unsigned i;
6477 bitmap_iterator bi;
6478 bool update_edges_p = false;
6479 int num_asserts = 0;
6481 if (dump_file && (dump_flags & TDF_DETAILS))
6482 dump_all_asserts (dump_file);
6484 EXECUTE_IF_SET_IN_BITMAP (need_assert_for, 0, i, bi)
6486 assert_locus *loc = asserts_for[i];
6487 gcc_assert (loc);
6489 auto_vec<assert_locus *, 16> asserts;
6490 for (; loc; loc = loc->next)
6491 asserts.safe_push (loc);
6492 asserts.qsort (compare_assert_loc<false>);
6494 /* Push down common asserts to successors and remove redundant ones. */
6495 unsigned ecnt = 0;
6496 assert_locus *common = NULL;
6497 unsigned commonj = 0;
6498 for (unsigned j = 0; j < asserts.length (); ++j)
6500 loc = asserts[j];
6501 if (! loc->e)
6502 common = NULL;
6503 else if (! common
6504 || loc->e->dest != common->e->dest
6505 || loc->comp_code != common->comp_code
6506 || ! operand_equal_p (loc->val, common->val, 0)
6507 || ! operand_equal_p (loc->expr, common->expr, 0))
6509 commonj = j;
6510 common = loc;
6511 ecnt = 1;
6513 else if (loc->e == asserts[j-1]->e)
6515 /* Remove duplicate asserts. */
6516 if (commonj == j - 1)
6518 commonj = j;
6519 common = loc;
6521 free (asserts[j-1]);
6522 asserts[j-1] = NULL;
6524 else
6526 ecnt++;
6527 if (EDGE_COUNT (common->e->dest->preds) == ecnt)
6529 /* We have the same assertion on all incoming edges of a BB.
6530 Insert it at the beginning of that block. */
6531 loc->bb = loc->e->dest;
6532 loc->e = NULL;
6533 loc->si = gsi_none ();
6534 common = NULL;
6535 /* Clear asserts commoned. */
6536 for (; commonj != j; ++commonj)
6537 if (asserts[commonj])
6539 free (asserts[commonj]);
6540 asserts[commonj] = NULL;
6546 /* The asserts vector sorting above might be unstable for
6547 -fcompare-debug, sort again to ensure a stable sort. */
6548 asserts.qsort (compare_assert_loc<true>);
6549 for (unsigned j = 0; j < asserts.length (); ++j)
6551 loc = asserts[j];
6552 if (! loc)
6553 break;
6554 update_edges_p |= process_assert_insertions_for (ssa_name (i), loc);
6555 num_asserts++;
6556 free (loc);
6560 if (update_edges_p)
6561 gsi_commit_edge_inserts ();
6563 statistics_counter_event (cfun, "Number of ASSERT_EXPR expressions inserted",
6564 num_asserts);
6568 /* Traverse the flowgraph looking for conditional jumps to insert range
6569 expressions. These range expressions are meant to provide information
6570 to optimizations that need to reason in terms of value ranges. They
6571 will not be expanded into RTL. For instance, given:
6573 x = ...
6574 y = ...
6575 if (x < y)
6576 y = x - 2;
6577 else
6578 x = y + 3;
6580 this pass will transform the code into:
6582 x = ...
6583 y = ...
6584 if (x < y)
6586 x = ASSERT_EXPR <x, x < y>
6587 y = x - 2
6589 else
6591 y = ASSERT_EXPR <y, x >= y>
6592 x = y + 3
6595 The idea is that once copy and constant propagation have run, other
6596 optimizations will be able to determine what ranges of values can 'x'
6597 take in different paths of the code, simply by checking the reaching
6598 definition of 'x'. */
6600 static void
6601 insert_range_assertions (void)
6603 need_assert_for = BITMAP_ALLOC (NULL);
6604 asserts_for = XCNEWVEC (assert_locus *, num_ssa_names);
6606 calculate_dominance_info (CDI_DOMINATORS);
6608 find_assert_locations ();
6609 if (!bitmap_empty_p (need_assert_for))
6611 process_assert_insertions ();
6612 update_ssa (TODO_update_ssa_no_phi);
6615 if (dump_file && (dump_flags & TDF_DETAILS))
6617 fprintf (dump_file, "\nSSA form after inserting ASSERT_EXPRs\n");
6618 dump_function_to_file (current_function_decl, dump_file, dump_flags);
6621 free (asserts_for);
6622 BITMAP_FREE (need_assert_for);
6625 /* Checks one ARRAY_REF in REF, located at LOCUS. Ignores flexible arrays
6626 and "struct" hacks. If VRP can determine that the
6627 array subscript is a constant, check if it is outside valid
6628 range. If the array subscript is a RANGE, warn if it is
6629 non-overlapping with valid range.
6630 IGNORE_OFF_BY_ONE is true if the ARRAY_REF is inside a ADDR_EXPR. */
6632 static void
6633 check_array_ref (location_t location, tree ref, bool ignore_off_by_one)
6635 value_range *vr = NULL;
6636 tree low_sub, up_sub;
6637 tree low_bound, up_bound, up_bound_p1;
6639 if (TREE_NO_WARNING (ref))
6640 return;
6642 low_sub = up_sub = TREE_OPERAND (ref, 1);
6643 up_bound = array_ref_up_bound (ref);
6645 /* Can not check flexible arrays. */
6646 if (!up_bound
6647 || TREE_CODE (up_bound) != INTEGER_CST)
6648 return;
6650 /* Accesses to trailing arrays via pointers may access storage
6651 beyond the types array bounds. */
6652 if (warn_array_bounds < 2
6653 && array_at_struct_end_p (ref))
6654 return;
6656 low_bound = array_ref_low_bound (ref);
6657 up_bound_p1 = int_const_binop (PLUS_EXPR, up_bound,
6658 build_int_cst (TREE_TYPE (up_bound), 1));
6660 /* Empty array. */
6661 if (tree_int_cst_equal (low_bound, up_bound_p1))
6663 warning_at (location, OPT_Warray_bounds,
6664 "array subscript is above array bounds");
6665 TREE_NO_WARNING (ref) = 1;
6668 if (TREE_CODE (low_sub) == SSA_NAME)
6670 vr = get_value_range (low_sub);
6671 if (vr->type == VR_RANGE || vr->type == VR_ANTI_RANGE)
6673 low_sub = vr->type == VR_RANGE ? vr->max : vr->min;
6674 up_sub = vr->type == VR_RANGE ? vr->min : vr->max;
6678 if (vr && vr->type == VR_ANTI_RANGE)
6680 if (TREE_CODE (up_sub) == INTEGER_CST
6681 && (ignore_off_by_one
6682 ? tree_int_cst_lt (up_bound, up_sub)
6683 : tree_int_cst_le (up_bound, up_sub))
6684 && TREE_CODE (low_sub) == INTEGER_CST
6685 && tree_int_cst_le (low_sub, low_bound))
6687 warning_at (location, OPT_Warray_bounds,
6688 "array subscript is outside array bounds");
6689 TREE_NO_WARNING (ref) = 1;
6692 else if (TREE_CODE (up_sub) == INTEGER_CST
6693 && (ignore_off_by_one
6694 ? !tree_int_cst_le (up_sub, up_bound_p1)
6695 : !tree_int_cst_le (up_sub, up_bound)))
6697 if (dump_file && (dump_flags & TDF_DETAILS))
6699 fprintf (dump_file, "Array bound warning for ");
6700 dump_generic_expr (MSG_NOTE, TDF_SLIM, ref);
6701 fprintf (dump_file, "\n");
6703 warning_at (location, OPT_Warray_bounds,
6704 "array subscript is above array bounds");
6705 TREE_NO_WARNING (ref) = 1;
6707 else if (TREE_CODE (low_sub) == INTEGER_CST
6708 && tree_int_cst_lt (low_sub, low_bound))
6710 if (dump_file && (dump_flags & TDF_DETAILS))
6712 fprintf (dump_file, "Array bound warning for ");
6713 dump_generic_expr (MSG_NOTE, TDF_SLIM, ref);
6714 fprintf (dump_file, "\n");
6716 warning_at (location, OPT_Warray_bounds,
6717 "array subscript is below array bounds");
6718 TREE_NO_WARNING (ref) = 1;
6722 /* Searches if the expr T, located at LOCATION computes
6723 address of an ARRAY_REF, and call check_array_ref on it. */
6725 static void
6726 search_for_addr_array (tree t, location_t location)
6728 /* Check each ARRAY_REFs in the reference chain. */
6731 if (TREE_CODE (t) == ARRAY_REF)
6732 check_array_ref (location, t, true /*ignore_off_by_one*/);
6734 t = TREE_OPERAND (t, 0);
6736 while (handled_component_p (t));
6738 if (TREE_CODE (t) == MEM_REF
6739 && TREE_CODE (TREE_OPERAND (t, 0)) == ADDR_EXPR
6740 && !TREE_NO_WARNING (t))
6742 tree tem = TREE_OPERAND (TREE_OPERAND (t, 0), 0);
6743 tree low_bound, up_bound, el_sz;
6744 offset_int idx;
6745 if (TREE_CODE (TREE_TYPE (tem)) != ARRAY_TYPE
6746 || TREE_CODE (TREE_TYPE (TREE_TYPE (tem))) == ARRAY_TYPE
6747 || !TYPE_DOMAIN (TREE_TYPE (tem)))
6748 return;
6750 low_bound = TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (tem)));
6751 up_bound = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (tem)));
6752 el_sz = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (tem)));
6753 if (!low_bound
6754 || TREE_CODE (low_bound) != INTEGER_CST
6755 || !up_bound
6756 || TREE_CODE (up_bound) != INTEGER_CST
6757 || !el_sz
6758 || TREE_CODE (el_sz) != INTEGER_CST)
6759 return;
6761 idx = mem_ref_offset (t);
6762 idx = wi::sdiv_trunc (idx, wi::to_offset (el_sz));
6763 if (idx < 0)
6765 if (dump_file && (dump_flags & TDF_DETAILS))
6767 fprintf (dump_file, "Array bound warning for ");
6768 dump_generic_expr (MSG_NOTE, TDF_SLIM, t);
6769 fprintf (dump_file, "\n");
6771 warning_at (location, OPT_Warray_bounds,
6772 "array subscript is below array bounds");
6773 TREE_NO_WARNING (t) = 1;
6775 else if (idx > (wi::to_offset (up_bound)
6776 - wi::to_offset (low_bound) + 1))
6778 if (dump_file && (dump_flags & TDF_DETAILS))
6780 fprintf (dump_file, "Array bound warning for ");
6781 dump_generic_expr (MSG_NOTE, TDF_SLIM, t);
6782 fprintf (dump_file, "\n");
6784 warning_at (location, OPT_Warray_bounds,
6785 "array subscript is above array bounds");
6786 TREE_NO_WARNING (t) = 1;
6791 /* walk_tree() callback that checks if *TP is
6792 an ARRAY_REF inside an ADDR_EXPR (in which an array
6793 subscript one outside the valid range is allowed). Call
6794 check_array_ref for each ARRAY_REF found. The location is
6795 passed in DATA. */
6797 static tree
6798 check_array_bounds (tree *tp, int *walk_subtree, void *data)
6800 tree t = *tp;
6801 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
6802 location_t location;
6804 if (EXPR_HAS_LOCATION (t))
6805 location = EXPR_LOCATION (t);
6806 else
6808 location_t *locp = (location_t *) wi->info;
6809 location = *locp;
6812 *walk_subtree = TRUE;
6814 if (TREE_CODE (t) == ARRAY_REF)
6815 check_array_ref (location, t, false /*ignore_off_by_one*/);
6817 else if (TREE_CODE (t) == ADDR_EXPR)
6819 search_for_addr_array (t, location);
6820 *walk_subtree = FALSE;
6823 return NULL_TREE;
6826 /* Walk over all statements of all reachable BBs and call check_array_bounds
6827 on them. */
6829 static void
6830 check_all_array_refs (void)
6832 basic_block bb;
6833 gimple_stmt_iterator si;
6835 FOR_EACH_BB_FN (bb, cfun)
6837 edge_iterator ei;
6838 edge e;
6839 bool executable = false;
6841 /* Skip blocks that were found to be unreachable. */
6842 FOR_EACH_EDGE (e, ei, bb->preds)
6843 executable |= !!(e->flags & EDGE_EXECUTABLE);
6844 if (!executable)
6845 continue;
6847 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
6849 gimple *stmt = gsi_stmt (si);
6850 struct walk_stmt_info wi;
6851 if (!gimple_has_location (stmt)
6852 || is_gimple_debug (stmt))
6853 continue;
6855 memset (&wi, 0, sizeof (wi));
6857 location_t loc = gimple_location (stmt);
6858 wi.info = &loc;
6860 walk_gimple_op (gsi_stmt (si),
6861 check_array_bounds,
6862 &wi);
6867 /* Return true if all imm uses of VAR are either in STMT, or
6868 feed (optionally through a chain of single imm uses) GIMPLE_COND
6869 in basic block COND_BB. */
6871 static bool
6872 all_imm_uses_in_stmt_or_feed_cond (tree var, gimple *stmt, basic_block cond_bb)
6874 use_operand_p use_p, use2_p;
6875 imm_use_iterator iter;
6877 FOR_EACH_IMM_USE_FAST (use_p, iter, var)
6878 if (USE_STMT (use_p) != stmt)
6880 gimple *use_stmt = USE_STMT (use_p), *use_stmt2;
6881 if (is_gimple_debug (use_stmt))
6882 continue;
6883 while (is_gimple_assign (use_stmt)
6884 && TREE_CODE (gimple_assign_lhs (use_stmt)) == SSA_NAME
6885 && single_imm_use (gimple_assign_lhs (use_stmt),
6886 &use2_p, &use_stmt2))
6887 use_stmt = use_stmt2;
6888 if (gimple_code (use_stmt) != GIMPLE_COND
6889 || gimple_bb (use_stmt) != cond_bb)
6890 return false;
6892 return true;
6895 /* Handle
6896 _4 = x_3 & 31;
6897 if (_4 != 0)
6898 goto <bb 6>;
6899 else
6900 goto <bb 7>;
6901 <bb 6>:
6902 __builtin_unreachable ();
6903 <bb 7>:
6904 x_5 = ASSERT_EXPR <x_3, ...>;
6905 If x_3 has no other immediate uses (checked by caller),
6906 var is the x_3 var from ASSERT_EXPR, we can clear low 5 bits
6907 from the non-zero bitmask. */
6909 static void
6910 maybe_set_nonzero_bits (basic_block bb, tree var)
6912 edge e = single_pred_edge (bb);
6913 basic_block cond_bb = e->src;
6914 gimple *stmt = last_stmt (cond_bb);
6915 tree cst;
6917 if (stmt == NULL
6918 || gimple_code (stmt) != GIMPLE_COND
6919 || gimple_cond_code (stmt) != ((e->flags & EDGE_TRUE_VALUE)
6920 ? EQ_EXPR : NE_EXPR)
6921 || TREE_CODE (gimple_cond_lhs (stmt)) != SSA_NAME
6922 || !integer_zerop (gimple_cond_rhs (stmt)))
6923 return;
6925 stmt = SSA_NAME_DEF_STMT (gimple_cond_lhs (stmt));
6926 if (!is_gimple_assign (stmt)
6927 || gimple_assign_rhs_code (stmt) != BIT_AND_EXPR
6928 || TREE_CODE (gimple_assign_rhs2 (stmt)) != INTEGER_CST)
6929 return;
6930 if (gimple_assign_rhs1 (stmt) != var)
6932 gimple *stmt2;
6934 if (TREE_CODE (gimple_assign_rhs1 (stmt)) != SSA_NAME)
6935 return;
6936 stmt2 = SSA_NAME_DEF_STMT (gimple_assign_rhs1 (stmt));
6937 if (!gimple_assign_cast_p (stmt2)
6938 || gimple_assign_rhs1 (stmt2) != var
6939 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (stmt2))
6940 || (TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (stmt)))
6941 != TYPE_PRECISION (TREE_TYPE (var))))
6942 return;
6944 cst = gimple_assign_rhs2 (stmt);
6945 set_nonzero_bits (var, wi::bit_and_not (get_nonzero_bits (var), cst));
6948 /* Convert range assertion expressions into the implied copies and
6949 copy propagate away the copies. Doing the trivial copy propagation
6950 here avoids the need to run the full copy propagation pass after
6951 VRP.
6953 FIXME, this will eventually lead to copy propagation removing the
6954 names that had useful range information attached to them. For
6955 instance, if we had the assertion N_i = ASSERT_EXPR <N_j, N_j > 3>,
6956 then N_i will have the range [3, +INF].
6958 However, by converting the assertion into the implied copy
6959 operation N_i = N_j, we will then copy-propagate N_j into the uses
6960 of N_i and lose the range information. We may want to hold on to
6961 ASSERT_EXPRs a little while longer as the ranges could be used in
6962 things like jump threading.
6964 The problem with keeping ASSERT_EXPRs around is that passes after
6965 VRP need to handle them appropriately.
6967 Another approach would be to make the range information a first
6968 class property of the SSA_NAME so that it can be queried from
6969 any pass. This is made somewhat more complex by the need for
6970 multiple ranges to be associated with one SSA_NAME. */
6972 static void
6973 remove_range_assertions (void)
6975 basic_block bb;
6976 gimple_stmt_iterator si;
6977 /* 1 if looking at ASSERT_EXPRs immediately at the beginning of
6978 a basic block preceeded by GIMPLE_COND branching to it and
6979 __builtin_trap, -1 if not yet checked, 0 otherwise. */
6980 int is_unreachable;
6982 /* Note that the BSI iterator bump happens at the bottom of the
6983 loop and no bump is necessary if we're removing the statement
6984 referenced by the current BSI. */
6985 FOR_EACH_BB_FN (bb, cfun)
6986 for (si = gsi_after_labels (bb), is_unreachable = -1; !gsi_end_p (si);)
6988 gimple *stmt = gsi_stmt (si);
6990 if (is_gimple_assign (stmt)
6991 && gimple_assign_rhs_code (stmt) == ASSERT_EXPR)
6993 tree lhs = gimple_assign_lhs (stmt);
6994 tree rhs = gimple_assign_rhs1 (stmt);
6995 tree var;
6997 var = ASSERT_EXPR_VAR (rhs);
6999 if (TREE_CODE (var) == SSA_NAME
7000 && !POINTER_TYPE_P (TREE_TYPE (lhs))
7001 && SSA_NAME_RANGE_INFO (lhs))
7003 if (is_unreachable == -1)
7005 is_unreachable = 0;
7006 if (single_pred_p (bb)
7007 && assert_unreachable_fallthru_edge_p
7008 (single_pred_edge (bb)))
7009 is_unreachable = 1;
7011 /* Handle
7012 if (x_7 >= 10 && x_7 < 20)
7013 __builtin_unreachable ();
7014 x_8 = ASSERT_EXPR <x_7, ...>;
7015 if the only uses of x_7 are in the ASSERT_EXPR and
7016 in the condition. In that case, we can copy the
7017 range info from x_8 computed in this pass also
7018 for x_7. */
7019 if (is_unreachable
7020 && all_imm_uses_in_stmt_or_feed_cond (var, stmt,
7021 single_pred (bb)))
7023 set_range_info (var, SSA_NAME_RANGE_TYPE (lhs),
7024 SSA_NAME_RANGE_INFO (lhs)->get_min (),
7025 SSA_NAME_RANGE_INFO (lhs)->get_max ());
7026 maybe_set_nonzero_bits (bb, var);
7030 /* Propagate the RHS into every use of the LHS. For SSA names
7031 also propagate abnormals as it merely restores the original
7032 IL in this case (an replace_uses_by would assert). */
7033 if (TREE_CODE (var) == SSA_NAME)
7035 imm_use_iterator iter;
7036 use_operand_p use_p;
7037 gimple *use_stmt;
7038 FOR_EACH_IMM_USE_STMT (use_stmt, iter, lhs)
7039 FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
7040 SET_USE (use_p, var);
7042 /* But do not propagate constants as that is invalid. */
7043 else if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs))
7045 gassign *ass = gimple_build_assign (lhs, var);
7046 gsi_replace (&si, ass, true);
7047 gsi_next (&si);
7048 continue;
7050 else
7051 replace_uses_by (lhs, var);
7053 /* And finally, remove the copy, it is not needed. */
7054 gsi_remove (&si, true);
7055 release_defs (stmt);
7057 else
7059 if (!is_gimple_debug (gsi_stmt (si)))
7060 is_unreachable = 0;
7061 gsi_next (&si);
7067 /* Return true if STMT is interesting for VRP. */
7069 static bool
7070 stmt_interesting_for_vrp (gimple *stmt)
7072 if (gimple_code (stmt) == GIMPLE_PHI)
7074 tree res = gimple_phi_result (stmt);
7075 return (!virtual_operand_p (res)
7076 && (INTEGRAL_TYPE_P (TREE_TYPE (res))
7077 || POINTER_TYPE_P (TREE_TYPE (res))));
7079 else if (is_gimple_assign (stmt) || is_gimple_call (stmt))
7081 tree lhs = gimple_get_lhs (stmt);
7083 /* In general, assignments with virtual operands are not useful
7084 for deriving ranges, with the obvious exception of calls to
7085 builtin functions. */
7086 if (lhs && TREE_CODE (lhs) == SSA_NAME
7087 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs))
7088 || POINTER_TYPE_P (TREE_TYPE (lhs)))
7089 && (is_gimple_call (stmt)
7090 || !gimple_vuse (stmt)))
7091 return true;
7092 else if (is_gimple_call (stmt) && gimple_call_internal_p (stmt))
7093 switch (gimple_call_internal_fn (stmt))
7095 case IFN_ADD_OVERFLOW:
7096 case IFN_SUB_OVERFLOW:
7097 case IFN_MUL_OVERFLOW:
7098 case IFN_ATOMIC_COMPARE_EXCHANGE:
7099 /* These internal calls return _Complex integer type,
7100 but are interesting to VRP nevertheless. */
7101 if (lhs && TREE_CODE (lhs) == SSA_NAME)
7102 return true;
7103 break;
7104 default:
7105 break;
7108 else if (gimple_code (stmt) == GIMPLE_COND
7109 || gimple_code (stmt) == GIMPLE_SWITCH)
7110 return true;
7112 return false;
7115 /* Initialize VRP lattice. */
7117 static void
7118 vrp_initialize_lattice ()
7120 values_propagated = false;
7121 num_vr_values = num_ssa_names;
7122 vr_value = XCNEWVEC (value_range *, num_vr_values);
7123 vr_phi_edge_counts = XCNEWVEC (int, num_ssa_names);
7124 bitmap_obstack_initialize (&vrp_equiv_obstack);
7127 /* Initialization required by ssa_propagate engine. */
7129 static void
7130 vrp_initialize ()
7132 basic_block bb;
7134 FOR_EACH_BB_FN (bb, cfun)
7136 for (gphi_iterator si = gsi_start_phis (bb); !gsi_end_p (si);
7137 gsi_next (&si))
7139 gphi *phi = si.phi ();
7140 if (!stmt_interesting_for_vrp (phi))
7142 tree lhs = PHI_RESULT (phi);
7143 set_value_range_to_varying (get_value_range (lhs));
7144 prop_set_simulate_again (phi, false);
7146 else
7147 prop_set_simulate_again (phi, true);
7150 for (gimple_stmt_iterator si = gsi_start_bb (bb); !gsi_end_p (si);
7151 gsi_next (&si))
7153 gimple *stmt = gsi_stmt (si);
7155 /* If the statement is a control insn, then we do not
7156 want to avoid simulating the statement once. Failure
7157 to do so means that those edges will never get added. */
7158 if (stmt_ends_bb_p (stmt))
7159 prop_set_simulate_again (stmt, true);
7160 else if (!stmt_interesting_for_vrp (stmt))
7162 set_defs_to_varying (stmt);
7163 prop_set_simulate_again (stmt, false);
7165 else
7166 prop_set_simulate_again (stmt, true);
7171 /* Return the singleton value-range for NAME or NAME. */
7173 static inline tree
7174 vrp_valueize (tree name)
7176 if (TREE_CODE (name) == SSA_NAME)
7178 value_range *vr = get_value_range (name);
7179 if (vr->type == VR_RANGE
7180 && (TREE_CODE (vr->min) == SSA_NAME
7181 || is_gimple_min_invariant (vr->min))
7182 && vrp_operand_equal_p (vr->min, vr->max))
7183 return vr->min;
7185 return name;
7188 /* Return the singleton value-range for NAME if that is a constant
7189 but signal to not follow SSA edges. */
7191 static inline tree
7192 vrp_valueize_1 (tree name)
7194 if (TREE_CODE (name) == SSA_NAME)
7196 /* If the definition may be simulated again we cannot follow
7197 this SSA edge as the SSA propagator does not necessarily
7198 re-visit the use. */
7199 gimple *def_stmt = SSA_NAME_DEF_STMT (name);
7200 if (!gimple_nop_p (def_stmt)
7201 && prop_simulate_again_p (def_stmt))
7202 return NULL_TREE;
7203 value_range *vr = get_value_range (name);
7204 if (range_int_cst_singleton_p (vr))
7205 return vr->min;
7207 return name;
7210 /* Visit assignment STMT. If it produces an interesting range, record
7211 the range in VR and set LHS to OUTPUT_P. */
7213 static void
7214 vrp_visit_assignment_or_call (gimple *stmt, tree *output_p, value_range *vr)
7216 tree lhs;
7217 enum gimple_code code = gimple_code (stmt);
7218 lhs = gimple_get_lhs (stmt);
7219 *output_p = NULL_TREE;
7221 /* We only keep track of ranges in integral and pointer types. */
7222 if (TREE_CODE (lhs) == SSA_NAME
7223 && ((INTEGRAL_TYPE_P (TREE_TYPE (lhs))
7224 /* It is valid to have NULL MIN/MAX values on a type. See
7225 build_range_type. */
7226 && TYPE_MIN_VALUE (TREE_TYPE (lhs))
7227 && TYPE_MAX_VALUE (TREE_TYPE (lhs)))
7228 || POINTER_TYPE_P (TREE_TYPE (lhs))))
7230 *output_p = lhs;
7232 /* Try folding the statement to a constant first. */
7233 tree tem = gimple_fold_stmt_to_constant_1 (stmt, vrp_valueize,
7234 vrp_valueize_1);
7235 if (tem)
7237 if (TREE_CODE (tem) == SSA_NAME
7238 && (SSA_NAME_IS_DEFAULT_DEF (tem)
7239 || ! prop_simulate_again_p (SSA_NAME_DEF_STMT (tem))))
7241 extract_range_from_ssa_name (vr, tem);
7242 return;
7244 else if (is_gimple_min_invariant (tem))
7246 set_value_range_to_value (vr, tem, NULL);
7247 return;
7250 /* Then dispatch to value-range extracting functions. */
7251 if (code == GIMPLE_CALL)
7252 extract_range_basic (vr, stmt);
7253 else
7254 extract_range_from_assignment (vr, as_a <gassign *> (stmt));
7258 /* Helper that gets the value range of the SSA_NAME with version I
7259 or a symbolic range containing the SSA_NAME only if the value range
7260 is varying or undefined. */
7262 static inline value_range
7263 get_vr_for_comparison (int i)
7265 value_range vr = *get_value_range (ssa_name (i));
7267 /* If name N_i does not have a valid range, use N_i as its own
7268 range. This allows us to compare against names that may
7269 have N_i in their ranges. */
7270 if (vr.type == VR_VARYING || vr.type == VR_UNDEFINED)
7272 vr.type = VR_RANGE;
7273 vr.min = ssa_name (i);
7274 vr.max = ssa_name (i);
7277 return vr;
7280 /* Compare all the value ranges for names equivalent to VAR with VAL
7281 using comparison code COMP. Return the same value returned by
7282 compare_range_with_value, including the setting of
7283 *STRICT_OVERFLOW_P. */
7285 static tree
7286 compare_name_with_value (enum tree_code comp, tree var, tree val,
7287 bool *strict_overflow_p, bool use_equiv_p)
7289 bitmap_iterator bi;
7290 unsigned i;
7291 bitmap e;
7292 tree retval, t;
7293 int used_strict_overflow;
7294 bool sop;
7295 value_range equiv_vr;
7297 /* Get the set of equivalences for VAR. */
7298 e = get_value_range (var)->equiv;
7300 /* Start at -1. Set it to 0 if we do a comparison without relying
7301 on overflow, or 1 if all comparisons rely on overflow. */
7302 used_strict_overflow = -1;
7304 /* Compare vars' value range with val. */
7305 equiv_vr = get_vr_for_comparison (SSA_NAME_VERSION (var));
7306 sop = false;
7307 retval = compare_range_with_value (comp, &equiv_vr, val, &sop);
7308 if (retval)
7309 used_strict_overflow = sop ? 1 : 0;
7311 /* If the equiv set is empty we have done all work we need to do. */
7312 if (e == NULL)
7314 if (retval
7315 && used_strict_overflow > 0)
7316 *strict_overflow_p = true;
7317 return retval;
7320 EXECUTE_IF_SET_IN_BITMAP (e, 0, i, bi)
7322 tree name = ssa_name (i);
7323 if (! name)
7324 continue;
7326 if (! use_equiv_p
7327 && ! SSA_NAME_IS_DEFAULT_DEF (name)
7328 && prop_simulate_again_p (SSA_NAME_DEF_STMT (name)))
7329 continue;
7331 equiv_vr = get_vr_for_comparison (i);
7332 sop = false;
7333 t = compare_range_with_value (comp, &equiv_vr, val, &sop);
7334 if (t)
7336 /* If we get different answers from different members
7337 of the equivalence set this check must be in a dead
7338 code region. Folding it to a trap representation
7339 would be correct here. For now just return don't-know. */
7340 if (retval != NULL
7341 && t != retval)
7343 retval = NULL_TREE;
7344 break;
7346 retval = t;
7348 if (!sop)
7349 used_strict_overflow = 0;
7350 else if (used_strict_overflow < 0)
7351 used_strict_overflow = 1;
7355 if (retval
7356 && used_strict_overflow > 0)
7357 *strict_overflow_p = true;
7359 return retval;
7363 /* Given a comparison code COMP and names N1 and N2, compare all the
7364 ranges equivalent to N1 against all the ranges equivalent to N2
7365 to determine the value of N1 COMP N2. Return the same value
7366 returned by compare_ranges. Set *STRICT_OVERFLOW_P to indicate
7367 whether we relied on undefined signed overflow in the comparison. */
7370 static tree
7371 compare_names (enum tree_code comp, tree n1, tree n2,
7372 bool *strict_overflow_p)
7374 tree t, retval;
7375 bitmap e1, e2;
7376 bitmap_iterator bi1, bi2;
7377 unsigned i1, i2;
7378 int used_strict_overflow;
7379 static bitmap_obstack *s_obstack = NULL;
7380 static bitmap s_e1 = NULL, s_e2 = NULL;
7382 /* Compare the ranges of every name equivalent to N1 against the
7383 ranges of every name equivalent to N2. */
7384 e1 = get_value_range (n1)->equiv;
7385 e2 = get_value_range (n2)->equiv;
7387 /* Use the fake bitmaps if e1 or e2 are not available. */
7388 if (s_obstack == NULL)
7390 s_obstack = XNEW (bitmap_obstack);
7391 bitmap_obstack_initialize (s_obstack);
7392 s_e1 = BITMAP_ALLOC (s_obstack);
7393 s_e2 = BITMAP_ALLOC (s_obstack);
7395 if (e1 == NULL)
7396 e1 = s_e1;
7397 if (e2 == NULL)
7398 e2 = s_e2;
7400 /* Add N1 and N2 to their own set of equivalences to avoid
7401 duplicating the body of the loop just to check N1 and N2
7402 ranges. */
7403 bitmap_set_bit (e1, SSA_NAME_VERSION (n1));
7404 bitmap_set_bit (e2, SSA_NAME_VERSION (n2));
7406 /* If the equivalence sets have a common intersection, then the two
7407 names can be compared without checking their ranges. */
7408 if (bitmap_intersect_p (e1, e2))
7410 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
7411 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
7413 return (comp == EQ_EXPR || comp == GE_EXPR || comp == LE_EXPR)
7414 ? boolean_true_node
7415 : boolean_false_node;
7418 /* Start at -1. Set it to 0 if we do a comparison without relying
7419 on overflow, or 1 if all comparisons rely on overflow. */
7420 used_strict_overflow = -1;
7422 /* Otherwise, compare all the equivalent ranges. First, add N1 and
7423 N2 to their own set of equivalences to avoid duplicating the body
7424 of the loop just to check N1 and N2 ranges. */
7425 EXECUTE_IF_SET_IN_BITMAP (e1, 0, i1, bi1)
7427 if (! ssa_name (i1))
7428 continue;
7430 value_range vr1 = get_vr_for_comparison (i1);
7432 t = retval = NULL_TREE;
7433 EXECUTE_IF_SET_IN_BITMAP (e2, 0, i2, bi2)
7435 if (! ssa_name (i2))
7436 continue;
7438 bool sop = false;
7440 value_range vr2 = get_vr_for_comparison (i2);
7442 t = compare_ranges (comp, &vr1, &vr2, &sop);
7443 if (t)
7445 /* If we get different answers from different members
7446 of the equivalence set this check must be in a dead
7447 code region. Folding it to a trap representation
7448 would be correct here. For now just return don't-know. */
7449 if (retval != NULL
7450 && t != retval)
7452 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
7453 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
7454 return NULL_TREE;
7456 retval = t;
7458 if (!sop)
7459 used_strict_overflow = 0;
7460 else if (used_strict_overflow < 0)
7461 used_strict_overflow = 1;
7465 if (retval)
7467 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
7468 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
7469 if (used_strict_overflow > 0)
7470 *strict_overflow_p = true;
7471 return retval;
7475 /* None of the equivalent ranges are useful in computing this
7476 comparison. */
7477 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
7478 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
7479 return NULL_TREE;
7482 /* Helper function for vrp_evaluate_conditional_warnv & other
7483 optimizers. */
7485 static tree
7486 vrp_evaluate_conditional_warnv_with_ops_using_ranges (enum tree_code code,
7487 tree op0, tree op1,
7488 bool * strict_overflow_p)
7490 value_range *vr0, *vr1;
7492 vr0 = (TREE_CODE (op0) == SSA_NAME) ? get_value_range (op0) : NULL;
7493 vr1 = (TREE_CODE (op1) == SSA_NAME) ? get_value_range (op1) : NULL;
7495 tree res = NULL_TREE;
7496 if (vr0 && vr1)
7497 res = compare_ranges (code, vr0, vr1, strict_overflow_p);
7498 if (!res && vr0)
7499 res = compare_range_with_value (code, vr0, op1, strict_overflow_p);
7500 if (!res && vr1)
7501 res = (compare_range_with_value
7502 (swap_tree_comparison (code), vr1, op0, strict_overflow_p));
7503 return res;
7506 /* Helper function for vrp_evaluate_conditional_warnv. */
7508 static tree
7509 vrp_evaluate_conditional_warnv_with_ops (enum tree_code code, tree op0,
7510 tree op1, bool use_equiv_p,
7511 bool *strict_overflow_p, bool *only_ranges)
7513 tree ret;
7514 if (only_ranges)
7515 *only_ranges = true;
7517 /* We only deal with integral and pointer types. */
7518 if (!INTEGRAL_TYPE_P (TREE_TYPE (op0))
7519 && !POINTER_TYPE_P (TREE_TYPE (op0)))
7520 return NULL_TREE;
7522 /* If OP0 CODE OP1 is an overflow comparison, if it can be expressed
7523 as a simple equality test, then prefer that over its current form
7524 for evaluation.
7526 An overflow test which collapses to an equality test can always be
7527 expressed as a comparison of one argument against zero. Overflow
7528 occurs when the chosen argument is zero and does not occur if the
7529 chosen argument is not zero. */
7530 tree x;
7531 if (overflow_comparison_p (code, op0, op1, use_equiv_p, &x))
7533 wide_int max = wi::max_value (TYPE_PRECISION (TREE_TYPE (op0)), UNSIGNED);
7534 /* B = A - 1; if (A < B) -> B = A - 1; if (A == 0)
7535 B = A - 1; if (A > B) -> B = A - 1; if (A != 0)
7536 B = A + 1; if (B < A) -> B = A + 1; if (B == 0)
7537 B = A + 1; if (B > A) -> B = A + 1; if (B != 0) */
7538 if (integer_zerop (x))
7540 op1 = x;
7541 code = (code == LT_EXPR || code == LE_EXPR) ? EQ_EXPR : NE_EXPR;
7543 /* B = A + 1; if (A > B) -> B = A + 1; if (B == 0)
7544 B = A + 1; if (A < B) -> B = A + 1; if (B != 0)
7545 B = A - 1; if (B > A) -> B = A - 1; if (A == 0)
7546 B = A - 1; if (B < A) -> B = A - 1; if (A != 0) */
7547 else if (wi::eq_p (x, max - 1))
7549 op0 = op1;
7550 op1 = wide_int_to_tree (TREE_TYPE (op0), 0);
7551 code = (code == GT_EXPR || code == GE_EXPR) ? EQ_EXPR : NE_EXPR;
7555 if ((ret = vrp_evaluate_conditional_warnv_with_ops_using_ranges
7556 (code, op0, op1, strict_overflow_p)))
7557 return ret;
7558 if (only_ranges)
7559 *only_ranges = false;
7560 /* Do not use compare_names during propagation, it's quadratic. */
7561 if (TREE_CODE (op0) == SSA_NAME && TREE_CODE (op1) == SSA_NAME
7562 && use_equiv_p)
7563 return compare_names (code, op0, op1, strict_overflow_p);
7564 else if (TREE_CODE (op0) == SSA_NAME)
7565 return compare_name_with_value (code, op0, op1,
7566 strict_overflow_p, use_equiv_p);
7567 else if (TREE_CODE (op1) == SSA_NAME)
7568 return compare_name_with_value (swap_tree_comparison (code), op1, op0,
7569 strict_overflow_p, use_equiv_p);
7570 return NULL_TREE;
7573 /* Given (CODE OP0 OP1) within STMT, try to simplify it based on value range
7574 information. Return NULL if the conditional can not be evaluated.
7575 The ranges of all the names equivalent with the operands in COND
7576 will be used when trying to compute the value. If the result is
7577 based on undefined signed overflow, issue a warning if
7578 appropriate. */
7580 static tree
7581 vrp_evaluate_conditional (tree_code code, tree op0, tree op1, gimple *stmt)
7583 bool sop;
7584 tree ret;
7585 bool only_ranges;
7587 /* Some passes and foldings leak constants with overflow flag set
7588 into the IL. Avoid doing wrong things with these and bail out. */
7589 if ((TREE_CODE (op0) == INTEGER_CST
7590 && TREE_OVERFLOW (op0))
7591 || (TREE_CODE (op1) == INTEGER_CST
7592 && TREE_OVERFLOW (op1)))
7593 return NULL_TREE;
7595 sop = false;
7596 ret = vrp_evaluate_conditional_warnv_with_ops (code, op0, op1, true, &sop,
7597 &only_ranges);
7599 if (ret && sop)
7601 enum warn_strict_overflow_code wc;
7602 const char* warnmsg;
7604 if (is_gimple_min_invariant (ret))
7606 wc = WARN_STRICT_OVERFLOW_CONDITIONAL;
7607 warnmsg = G_("assuming signed overflow does not occur when "
7608 "simplifying conditional to constant");
7610 else
7612 wc = WARN_STRICT_OVERFLOW_COMPARISON;
7613 warnmsg = G_("assuming signed overflow does not occur when "
7614 "simplifying conditional");
7617 if (issue_strict_overflow_warning (wc))
7619 location_t location;
7621 if (!gimple_has_location (stmt))
7622 location = input_location;
7623 else
7624 location = gimple_location (stmt);
7625 warning_at (location, OPT_Wstrict_overflow, "%s", warnmsg);
7629 if (warn_type_limits
7630 && ret && only_ranges
7631 && TREE_CODE_CLASS (code) == tcc_comparison
7632 && TREE_CODE (op0) == SSA_NAME)
7634 /* If the comparison is being folded and the operand on the LHS
7635 is being compared against a constant value that is outside of
7636 the natural range of OP0's type, then the predicate will
7637 always fold regardless of the value of OP0. If -Wtype-limits
7638 was specified, emit a warning. */
7639 tree type = TREE_TYPE (op0);
7640 value_range *vr0 = get_value_range (op0);
7642 if (vr0->type == VR_RANGE
7643 && INTEGRAL_TYPE_P (type)
7644 && vrp_val_is_min (vr0->min)
7645 && vrp_val_is_max (vr0->max)
7646 && is_gimple_min_invariant (op1))
7648 location_t location;
7650 if (!gimple_has_location (stmt))
7651 location = input_location;
7652 else
7653 location = gimple_location (stmt);
7655 warning_at (location, OPT_Wtype_limits,
7656 integer_zerop (ret)
7657 ? G_("comparison always false "
7658 "due to limited range of data type")
7659 : G_("comparison always true "
7660 "due to limited range of data type"));
7664 return ret;
7668 /* Visit conditional statement STMT. If we can determine which edge
7669 will be taken out of STMT's basic block, record it in
7670 *TAKEN_EDGE_P. Otherwise, set *TAKEN_EDGE_P to NULL. */
7672 static void
7673 vrp_visit_cond_stmt (gcond *stmt, edge *taken_edge_p)
7675 tree val;
7677 *taken_edge_p = NULL;
7679 if (dump_file && (dump_flags & TDF_DETAILS))
7681 tree use;
7682 ssa_op_iter i;
7684 fprintf (dump_file, "\nVisiting conditional with predicate: ");
7685 print_gimple_stmt (dump_file, stmt, 0);
7686 fprintf (dump_file, "\nWith known ranges\n");
7688 FOR_EACH_SSA_TREE_OPERAND (use, stmt, i, SSA_OP_USE)
7690 fprintf (dump_file, "\t");
7691 print_generic_expr (dump_file, use);
7692 fprintf (dump_file, ": ");
7693 dump_value_range (dump_file, vr_value[SSA_NAME_VERSION (use)]);
7696 fprintf (dump_file, "\n");
7699 /* Compute the value of the predicate COND by checking the known
7700 ranges of each of its operands.
7702 Note that we cannot evaluate all the equivalent ranges here
7703 because those ranges may not yet be final and with the current
7704 propagation strategy, we cannot determine when the value ranges
7705 of the names in the equivalence set have changed.
7707 For instance, given the following code fragment
7709 i_5 = PHI <8, i_13>
7711 i_14 = ASSERT_EXPR <i_5, i_5 != 0>
7712 if (i_14 == 1)
7715 Assume that on the first visit to i_14, i_5 has the temporary
7716 range [8, 8] because the second argument to the PHI function is
7717 not yet executable. We derive the range ~[0, 0] for i_14 and the
7718 equivalence set { i_5 }. So, when we visit 'if (i_14 == 1)' for
7719 the first time, since i_14 is equivalent to the range [8, 8], we
7720 determine that the predicate is always false.
7722 On the next round of propagation, i_13 is determined to be
7723 VARYING, which causes i_5 to drop down to VARYING. So, another
7724 visit to i_14 is scheduled. In this second visit, we compute the
7725 exact same range and equivalence set for i_14, namely ~[0, 0] and
7726 { i_5 }. But we did not have the previous range for i_5
7727 registered, so vrp_visit_assignment thinks that the range for
7728 i_14 has not changed. Therefore, the predicate 'if (i_14 == 1)'
7729 is not visited again, which stops propagation from visiting
7730 statements in the THEN clause of that if().
7732 To properly fix this we would need to keep the previous range
7733 value for the names in the equivalence set. This way we would've
7734 discovered that from one visit to the other i_5 changed from
7735 range [8, 8] to VR_VARYING.
7737 However, fixing this apparent limitation may not be worth the
7738 additional checking. Testing on several code bases (GCC, DLV,
7739 MICO, TRAMP3D and SPEC2000) showed that doing this results in
7740 4 more predicates folded in SPEC. */
7742 bool sop;
7743 val = vrp_evaluate_conditional_warnv_with_ops (gimple_cond_code (stmt),
7744 gimple_cond_lhs (stmt),
7745 gimple_cond_rhs (stmt),
7746 false, &sop, NULL);
7747 if (val)
7748 *taken_edge_p = find_taken_edge (gimple_bb (stmt), val);
7750 if (dump_file && (dump_flags & TDF_DETAILS))
7752 fprintf (dump_file, "\nPredicate evaluates to: ");
7753 if (val == NULL_TREE)
7754 fprintf (dump_file, "DON'T KNOW\n");
7755 else
7756 print_generic_stmt (dump_file, val);
7760 /* Searches the case label vector VEC for the index *IDX of the CASE_LABEL
7761 that includes the value VAL. The search is restricted to the range
7762 [START_IDX, n - 1] where n is the size of VEC.
7764 If there is a CASE_LABEL for VAL, its index is placed in IDX and true is
7765 returned.
7767 If there is no CASE_LABEL for VAL and there is one that is larger than VAL,
7768 it is placed in IDX and false is returned.
7770 If VAL is larger than any CASE_LABEL, n is placed on IDX and false is
7771 returned. */
7773 static bool
7774 find_case_label_index (gswitch *stmt, size_t start_idx, tree val, size_t *idx)
7776 size_t n = gimple_switch_num_labels (stmt);
7777 size_t low, high;
7779 /* Find case label for minimum of the value range or the next one.
7780 At each iteration we are searching in [low, high - 1]. */
7782 for (low = start_idx, high = n; high != low; )
7784 tree t;
7785 int cmp;
7786 /* Note that i != high, so we never ask for n. */
7787 size_t i = (high + low) / 2;
7788 t = gimple_switch_label (stmt, i);
7790 /* Cache the result of comparing CASE_LOW and val. */
7791 cmp = tree_int_cst_compare (CASE_LOW (t), val);
7793 if (cmp == 0)
7795 /* Ranges cannot be empty. */
7796 *idx = i;
7797 return true;
7799 else if (cmp > 0)
7800 high = i;
7801 else
7803 low = i + 1;
7804 if (CASE_HIGH (t) != NULL
7805 && tree_int_cst_compare (CASE_HIGH (t), val) >= 0)
7807 *idx = i;
7808 return true;
7813 *idx = high;
7814 return false;
7817 /* Searches the case label vector VEC for the range of CASE_LABELs that is used
7818 for values between MIN and MAX. The first index is placed in MIN_IDX. The
7819 last index is placed in MAX_IDX. If the range of CASE_LABELs is empty
7820 then MAX_IDX < MIN_IDX.
7821 Returns true if the default label is not needed. */
7823 static bool
7824 find_case_label_range (gswitch *stmt, tree min, tree max, size_t *min_idx,
7825 size_t *max_idx)
7827 size_t i, j;
7828 bool min_take_default = !find_case_label_index (stmt, 1, min, &i);
7829 bool max_take_default = !find_case_label_index (stmt, i, max, &j);
7831 if (i == j
7832 && min_take_default
7833 && max_take_default)
7835 /* Only the default case label reached.
7836 Return an empty range. */
7837 *min_idx = 1;
7838 *max_idx = 0;
7839 return false;
7841 else
7843 bool take_default = min_take_default || max_take_default;
7844 tree low, high;
7845 size_t k;
7847 if (max_take_default)
7848 j--;
7850 /* If the case label range is continuous, we do not need
7851 the default case label. Verify that. */
7852 high = CASE_LOW (gimple_switch_label (stmt, i));
7853 if (CASE_HIGH (gimple_switch_label (stmt, i)))
7854 high = CASE_HIGH (gimple_switch_label (stmt, i));
7855 for (k = i + 1; k <= j; ++k)
7857 low = CASE_LOW (gimple_switch_label (stmt, k));
7858 if (!integer_onep (int_const_binop (MINUS_EXPR, low, high)))
7860 take_default = true;
7861 break;
7863 high = low;
7864 if (CASE_HIGH (gimple_switch_label (stmt, k)))
7865 high = CASE_HIGH (gimple_switch_label (stmt, k));
7868 *min_idx = i;
7869 *max_idx = j;
7870 return !take_default;
7874 /* Searches the case label vector VEC for the ranges of CASE_LABELs that are
7875 used in range VR. The indices are placed in MIN_IDX1, MAX_IDX, MIN_IDX2 and
7876 MAX_IDX2. If the ranges of CASE_LABELs are empty then MAX_IDX1 < MIN_IDX1.
7877 Returns true if the default label is not needed. */
7879 static bool
7880 find_case_label_ranges (gswitch *stmt, value_range *vr, size_t *min_idx1,
7881 size_t *max_idx1, size_t *min_idx2,
7882 size_t *max_idx2)
7884 size_t i, j, k, l;
7885 unsigned int n = gimple_switch_num_labels (stmt);
7886 bool take_default;
7887 tree case_low, case_high;
7888 tree min = vr->min, max = vr->max;
7890 gcc_checking_assert (vr->type == VR_RANGE || vr->type == VR_ANTI_RANGE);
7892 take_default = !find_case_label_range (stmt, min, max, &i, &j);
7894 /* Set second range to emtpy. */
7895 *min_idx2 = 1;
7896 *max_idx2 = 0;
7898 if (vr->type == VR_RANGE)
7900 *min_idx1 = i;
7901 *max_idx1 = j;
7902 return !take_default;
7905 /* Set first range to all case labels. */
7906 *min_idx1 = 1;
7907 *max_idx1 = n - 1;
7909 if (i > j)
7910 return false;
7912 /* Make sure all the values of case labels [i , j] are contained in
7913 range [MIN, MAX]. */
7914 case_low = CASE_LOW (gimple_switch_label (stmt, i));
7915 case_high = CASE_HIGH (gimple_switch_label (stmt, j));
7916 if (tree_int_cst_compare (case_low, min) < 0)
7917 i += 1;
7918 if (case_high != NULL_TREE
7919 && tree_int_cst_compare (max, case_high) < 0)
7920 j -= 1;
7922 if (i > j)
7923 return false;
7925 /* If the range spans case labels [i, j], the corresponding anti-range spans
7926 the labels [1, i - 1] and [j + 1, n - 1]. */
7927 k = j + 1;
7928 l = n - 1;
7929 if (k > l)
7931 k = 1;
7932 l = 0;
7935 j = i - 1;
7936 i = 1;
7937 if (i > j)
7939 i = k;
7940 j = l;
7941 k = 1;
7942 l = 0;
7945 *min_idx1 = i;
7946 *max_idx1 = j;
7947 *min_idx2 = k;
7948 *max_idx2 = l;
7949 return false;
7952 /* Visit switch statement STMT. If we can determine which edge
7953 will be taken out of STMT's basic block, record it in
7954 *TAKEN_EDGE_P. Otherwise, *TAKEN_EDGE_P set to NULL. */
7956 static void
7957 vrp_visit_switch_stmt (gswitch *stmt, edge *taken_edge_p)
7959 tree op, val;
7960 value_range *vr;
7961 size_t i = 0, j = 0, k, l;
7962 bool take_default;
7964 *taken_edge_p = NULL;
7965 op = gimple_switch_index (stmt);
7966 if (TREE_CODE (op) != SSA_NAME)
7967 return;
7969 vr = get_value_range (op);
7970 if (dump_file && (dump_flags & TDF_DETAILS))
7972 fprintf (dump_file, "\nVisiting switch expression with operand ");
7973 print_generic_expr (dump_file, op);
7974 fprintf (dump_file, " with known range ");
7975 dump_value_range (dump_file, vr);
7976 fprintf (dump_file, "\n");
7979 if ((vr->type != VR_RANGE
7980 && vr->type != VR_ANTI_RANGE)
7981 || symbolic_range_p (vr))
7982 return;
7984 /* Find the single edge that is taken from the switch expression. */
7985 take_default = !find_case_label_ranges (stmt, vr, &i, &j, &k, &l);
7987 /* Check if the range spans no CASE_LABEL. If so, we only reach the default
7988 label */
7989 if (j < i)
7991 gcc_assert (take_default);
7992 val = gimple_switch_default_label (stmt);
7994 else
7996 /* Check if labels with index i to j and maybe the default label
7997 are all reaching the same label. */
7999 val = gimple_switch_label (stmt, i);
8000 if (take_default
8001 && CASE_LABEL (gimple_switch_default_label (stmt))
8002 != CASE_LABEL (val))
8004 if (dump_file && (dump_flags & TDF_DETAILS))
8005 fprintf (dump_file, " not a single destination for this "
8006 "range\n");
8007 return;
8009 for (++i; i <= j; ++i)
8011 if (CASE_LABEL (gimple_switch_label (stmt, i)) != CASE_LABEL (val))
8013 if (dump_file && (dump_flags & TDF_DETAILS))
8014 fprintf (dump_file, " not a single destination for this "
8015 "range\n");
8016 return;
8019 for (; k <= l; ++k)
8021 if (CASE_LABEL (gimple_switch_label (stmt, k)) != CASE_LABEL (val))
8023 if (dump_file && (dump_flags & TDF_DETAILS))
8024 fprintf (dump_file, " not a single destination for this "
8025 "range\n");
8026 return;
8031 *taken_edge_p = find_edge (gimple_bb (stmt),
8032 label_to_block (CASE_LABEL (val)));
8034 if (dump_file && (dump_flags & TDF_DETAILS))
8036 fprintf (dump_file, " will take edge to ");
8037 print_generic_stmt (dump_file, CASE_LABEL (val));
8042 /* Evaluate statement STMT. If the statement produces a useful range,
8043 set VR and corepsponding OUTPUT_P.
8045 If STMT is a conditional branch and we can determine its truth
8046 value, the taken edge is recorded in *TAKEN_EDGE_P. */
8048 static void
8049 extract_range_from_stmt (gimple *stmt, edge *taken_edge_p,
8050 tree *output_p, value_range *vr)
8053 if (dump_file && (dump_flags & TDF_DETAILS))
8055 fprintf (dump_file, "\nVisiting statement:\n");
8056 print_gimple_stmt (dump_file, stmt, 0, dump_flags);
8059 if (!stmt_interesting_for_vrp (stmt))
8060 gcc_assert (stmt_ends_bb_p (stmt));
8061 else if (is_gimple_assign (stmt) || is_gimple_call (stmt))
8062 vrp_visit_assignment_or_call (stmt, output_p, vr);
8063 else if (gimple_code (stmt) == GIMPLE_COND)
8064 vrp_visit_cond_stmt (as_a <gcond *> (stmt), taken_edge_p);
8065 else if (gimple_code (stmt) == GIMPLE_SWITCH)
8066 vrp_visit_switch_stmt (as_a <gswitch *> (stmt), taken_edge_p);
8069 /* Evaluate statement STMT. If the statement produces a useful range,
8070 return SSA_PROP_INTERESTING and record the SSA name with the
8071 interesting range into *OUTPUT_P.
8073 If STMT is a conditional branch and we can determine its truth
8074 value, the taken edge is recorded in *TAKEN_EDGE_P.
8076 If STMT produces a varying value, return SSA_PROP_VARYING. */
8078 static enum ssa_prop_result
8079 vrp_visit_stmt (gimple *stmt, edge *taken_edge_p, tree *output_p)
8081 value_range vr = VR_INITIALIZER;
8082 tree lhs = gimple_get_lhs (stmt);
8083 extract_range_from_stmt (stmt, taken_edge_p, output_p, &vr);
8085 if (*output_p)
8087 if (update_value_range (*output_p, &vr))
8089 if (dump_file && (dump_flags & TDF_DETAILS))
8091 fprintf (dump_file, "Found new range for ");
8092 print_generic_expr (dump_file, *output_p);
8093 fprintf (dump_file, ": ");
8094 dump_value_range (dump_file, &vr);
8095 fprintf (dump_file, "\n");
8098 if (vr.type == VR_VARYING)
8099 return SSA_PROP_VARYING;
8101 return SSA_PROP_INTERESTING;
8103 return SSA_PROP_NOT_INTERESTING;
8106 if (is_gimple_call (stmt) && gimple_call_internal_p (stmt))
8107 switch (gimple_call_internal_fn (stmt))
8109 case IFN_ADD_OVERFLOW:
8110 case IFN_SUB_OVERFLOW:
8111 case IFN_MUL_OVERFLOW:
8112 case IFN_ATOMIC_COMPARE_EXCHANGE:
8113 /* These internal calls return _Complex integer type,
8114 which VRP does not track, but the immediate uses
8115 thereof might be interesting. */
8116 if (lhs && TREE_CODE (lhs) == SSA_NAME)
8118 imm_use_iterator iter;
8119 use_operand_p use_p;
8120 enum ssa_prop_result res = SSA_PROP_VARYING;
8122 set_value_range_to_varying (get_value_range (lhs));
8124 FOR_EACH_IMM_USE_FAST (use_p, iter, lhs)
8126 gimple *use_stmt = USE_STMT (use_p);
8127 if (!is_gimple_assign (use_stmt))
8128 continue;
8129 enum tree_code rhs_code = gimple_assign_rhs_code (use_stmt);
8130 if (rhs_code != REALPART_EXPR && rhs_code != IMAGPART_EXPR)
8131 continue;
8132 tree rhs1 = gimple_assign_rhs1 (use_stmt);
8133 tree use_lhs = gimple_assign_lhs (use_stmt);
8134 if (TREE_CODE (rhs1) != rhs_code
8135 || TREE_OPERAND (rhs1, 0) != lhs
8136 || TREE_CODE (use_lhs) != SSA_NAME
8137 || !stmt_interesting_for_vrp (use_stmt)
8138 || (!INTEGRAL_TYPE_P (TREE_TYPE (use_lhs))
8139 || !TYPE_MIN_VALUE (TREE_TYPE (use_lhs))
8140 || !TYPE_MAX_VALUE (TREE_TYPE (use_lhs))))
8141 continue;
8143 /* If there is a change in the value range for any of the
8144 REALPART_EXPR/IMAGPART_EXPR immediate uses, return
8145 SSA_PROP_INTERESTING. If there are any REALPART_EXPR
8146 or IMAGPART_EXPR immediate uses, but none of them have
8147 a change in their value ranges, return
8148 SSA_PROP_NOT_INTERESTING. If there are no
8149 {REAL,IMAG}PART_EXPR uses at all,
8150 return SSA_PROP_VARYING. */
8151 value_range new_vr = VR_INITIALIZER;
8152 extract_range_basic (&new_vr, use_stmt);
8153 value_range *old_vr = get_value_range (use_lhs);
8154 if (old_vr->type != new_vr.type
8155 || !vrp_operand_equal_p (old_vr->min, new_vr.min)
8156 || !vrp_operand_equal_p (old_vr->max, new_vr.max)
8157 || !vrp_bitmap_equal_p (old_vr->equiv, new_vr.equiv))
8158 res = SSA_PROP_INTERESTING;
8159 else
8160 res = SSA_PROP_NOT_INTERESTING;
8161 BITMAP_FREE (new_vr.equiv);
8162 if (res == SSA_PROP_INTERESTING)
8164 *output_p = lhs;
8165 return res;
8169 return res;
8171 break;
8172 default:
8173 break;
8176 /* All other statements produce nothing of interest for VRP, so mark
8177 their outputs varying and prevent further simulation. */
8178 set_defs_to_varying (stmt);
8180 return (*taken_edge_p) ? SSA_PROP_INTERESTING : SSA_PROP_VARYING;
8183 /* Union the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
8184 { VR1TYPE, VR0MIN, VR0MAX } and store the result
8185 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
8186 possible such range. The resulting range is not canonicalized. */
8188 static void
8189 union_ranges (enum value_range_type *vr0type,
8190 tree *vr0min, tree *vr0max,
8191 enum value_range_type vr1type,
8192 tree vr1min, tree vr1max)
8194 bool mineq = vrp_operand_equal_p (*vr0min, vr1min);
8195 bool maxeq = vrp_operand_equal_p (*vr0max, vr1max);
8197 /* [] is vr0, () is vr1 in the following classification comments. */
8198 if (mineq && maxeq)
8200 /* [( )] */
8201 if (*vr0type == vr1type)
8202 /* Nothing to do for equal ranges. */
8204 else if ((*vr0type == VR_RANGE
8205 && vr1type == VR_ANTI_RANGE)
8206 || (*vr0type == VR_ANTI_RANGE
8207 && vr1type == VR_RANGE))
8209 /* For anti-range with range union the result is varying. */
8210 goto give_up;
8212 else
8213 gcc_unreachable ();
8215 else if (operand_less_p (*vr0max, vr1min) == 1
8216 || operand_less_p (vr1max, *vr0min) == 1)
8218 /* [ ] ( ) or ( ) [ ]
8219 If the ranges have an empty intersection, result of the union
8220 operation is the anti-range or if both are anti-ranges
8221 it covers all. */
8222 if (*vr0type == VR_ANTI_RANGE
8223 && vr1type == VR_ANTI_RANGE)
8224 goto give_up;
8225 else if (*vr0type == VR_ANTI_RANGE
8226 && vr1type == VR_RANGE)
8228 else if (*vr0type == VR_RANGE
8229 && vr1type == VR_ANTI_RANGE)
8231 *vr0type = vr1type;
8232 *vr0min = vr1min;
8233 *vr0max = vr1max;
8235 else if (*vr0type == VR_RANGE
8236 && vr1type == VR_RANGE)
8238 /* The result is the convex hull of both ranges. */
8239 if (operand_less_p (*vr0max, vr1min) == 1)
8241 /* If the result can be an anti-range, create one. */
8242 if (TREE_CODE (*vr0max) == INTEGER_CST
8243 && TREE_CODE (vr1min) == INTEGER_CST
8244 && vrp_val_is_min (*vr0min)
8245 && vrp_val_is_max (vr1max))
8247 tree min = int_const_binop (PLUS_EXPR,
8248 *vr0max,
8249 build_int_cst (TREE_TYPE (*vr0max), 1));
8250 tree max = int_const_binop (MINUS_EXPR,
8251 vr1min,
8252 build_int_cst (TREE_TYPE (vr1min), 1));
8253 if (!operand_less_p (max, min))
8255 *vr0type = VR_ANTI_RANGE;
8256 *vr0min = min;
8257 *vr0max = max;
8259 else
8260 *vr0max = vr1max;
8262 else
8263 *vr0max = vr1max;
8265 else
8267 /* If the result can be an anti-range, create one. */
8268 if (TREE_CODE (vr1max) == INTEGER_CST
8269 && TREE_CODE (*vr0min) == INTEGER_CST
8270 && vrp_val_is_min (vr1min)
8271 && vrp_val_is_max (*vr0max))
8273 tree min = int_const_binop (PLUS_EXPR,
8274 vr1max,
8275 build_int_cst (TREE_TYPE (vr1max), 1));
8276 tree max = int_const_binop (MINUS_EXPR,
8277 *vr0min,
8278 build_int_cst (TREE_TYPE (*vr0min), 1));
8279 if (!operand_less_p (max, min))
8281 *vr0type = VR_ANTI_RANGE;
8282 *vr0min = min;
8283 *vr0max = max;
8285 else
8286 *vr0min = vr1min;
8288 else
8289 *vr0min = vr1min;
8292 else
8293 gcc_unreachable ();
8295 else if ((maxeq || operand_less_p (vr1max, *vr0max) == 1)
8296 && (mineq || operand_less_p (*vr0min, vr1min) == 1))
8298 /* [ ( ) ] or [( ) ] or [ ( )] */
8299 if (*vr0type == VR_RANGE
8300 && vr1type == VR_RANGE)
8302 else if (*vr0type == VR_ANTI_RANGE
8303 && vr1type == VR_ANTI_RANGE)
8305 *vr0type = vr1type;
8306 *vr0min = vr1min;
8307 *vr0max = vr1max;
8309 else if (*vr0type == VR_ANTI_RANGE
8310 && vr1type == VR_RANGE)
8312 /* Arbitrarily choose the right or left gap. */
8313 if (!mineq && TREE_CODE (vr1min) == INTEGER_CST)
8314 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
8315 build_int_cst (TREE_TYPE (vr1min), 1));
8316 else if (!maxeq && TREE_CODE (vr1max) == INTEGER_CST)
8317 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
8318 build_int_cst (TREE_TYPE (vr1max), 1));
8319 else
8320 goto give_up;
8322 else if (*vr0type == VR_RANGE
8323 && vr1type == VR_ANTI_RANGE)
8324 /* The result covers everything. */
8325 goto give_up;
8326 else
8327 gcc_unreachable ();
8329 else if ((maxeq || operand_less_p (*vr0max, vr1max) == 1)
8330 && (mineq || operand_less_p (vr1min, *vr0min) == 1))
8332 /* ( [ ] ) or ([ ] ) or ( [ ]) */
8333 if (*vr0type == VR_RANGE
8334 && vr1type == VR_RANGE)
8336 *vr0type = vr1type;
8337 *vr0min = vr1min;
8338 *vr0max = vr1max;
8340 else if (*vr0type == VR_ANTI_RANGE
8341 && vr1type == VR_ANTI_RANGE)
8343 else if (*vr0type == VR_RANGE
8344 && vr1type == VR_ANTI_RANGE)
8346 *vr0type = VR_ANTI_RANGE;
8347 if (!mineq && TREE_CODE (*vr0min) == INTEGER_CST)
8349 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
8350 build_int_cst (TREE_TYPE (*vr0min), 1));
8351 *vr0min = vr1min;
8353 else if (!maxeq && TREE_CODE (*vr0max) == INTEGER_CST)
8355 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
8356 build_int_cst (TREE_TYPE (*vr0max), 1));
8357 *vr0max = vr1max;
8359 else
8360 goto give_up;
8362 else if (*vr0type == VR_ANTI_RANGE
8363 && vr1type == VR_RANGE)
8364 /* The result covers everything. */
8365 goto give_up;
8366 else
8367 gcc_unreachable ();
8369 else if ((operand_less_p (vr1min, *vr0max) == 1
8370 || operand_equal_p (vr1min, *vr0max, 0))
8371 && operand_less_p (*vr0min, vr1min) == 1
8372 && operand_less_p (*vr0max, vr1max) == 1)
8374 /* [ ( ] ) or [ ]( ) */
8375 if (*vr0type == VR_RANGE
8376 && vr1type == VR_RANGE)
8377 *vr0max = vr1max;
8378 else if (*vr0type == VR_ANTI_RANGE
8379 && vr1type == VR_ANTI_RANGE)
8380 *vr0min = vr1min;
8381 else if (*vr0type == VR_ANTI_RANGE
8382 && vr1type == VR_RANGE)
8384 if (TREE_CODE (vr1min) == INTEGER_CST)
8385 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
8386 build_int_cst (TREE_TYPE (vr1min), 1));
8387 else
8388 goto give_up;
8390 else if (*vr0type == VR_RANGE
8391 && vr1type == VR_ANTI_RANGE)
8393 if (TREE_CODE (*vr0max) == INTEGER_CST)
8395 *vr0type = vr1type;
8396 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
8397 build_int_cst (TREE_TYPE (*vr0max), 1));
8398 *vr0max = vr1max;
8400 else
8401 goto give_up;
8403 else
8404 gcc_unreachable ();
8406 else if ((operand_less_p (*vr0min, vr1max) == 1
8407 || operand_equal_p (*vr0min, vr1max, 0))
8408 && operand_less_p (vr1min, *vr0min) == 1
8409 && operand_less_p (vr1max, *vr0max) == 1)
8411 /* ( [ ) ] or ( )[ ] */
8412 if (*vr0type == VR_RANGE
8413 && vr1type == VR_RANGE)
8414 *vr0min = vr1min;
8415 else if (*vr0type == VR_ANTI_RANGE
8416 && vr1type == VR_ANTI_RANGE)
8417 *vr0max = vr1max;
8418 else if (*vr0type == VR_ANTI_RANGE
8419 && vr1type == VR_RANGE)
8421 if (TREE_CODE (vr1max) == INTEGER_CST)
8422 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
8423 build_int_cst (TREE_TYPE (vr1max), 1));
8424 else
8425 goto give_up;
8427 else if (*vr0type == VR_RANGE
8428 && vr1type == VR_ANTI_RANGE)
8430 if (TREE_CODE (*vr0min) == INTEGER_CST)
8432 *vr0type = vr1type;
8433 *vr0min = vr1min;
8434 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
8435 build_int_cst (TREE_TYPE (*vr0min), 1));
8437 else
8438 goto give_up;
8440 else
8441 gcc_unreachable ();
8443 else
8444 goto give_up;
8446 return;
8448 give_up:
8449 *vr0type = VR_VARYING;
8450 *vr0min = NULL_TREE;
8451 *vr0max = NULL_TREE;
8454 /* Intersect the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
8455 { VR1TYPE, VR0MIN, VR0MAX } and store the result
8456 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
8457 possible such range. The resulting range is not canonicalized. */
8459 static void
8460 intersect_ranges (enum value_range_type *vr0type,
8461 tree *vr0min, tree *vr0max,
8462 enum value_range_type vr1type,
8463 tree vr1min, tree vr1max)
8465 bool mineq = vrp_operand_equal_p (*vr0min, vr1min);
8466 bool maxeq = vrp_operand_equal_p (*vr0max, vr1max);
8468 /* [] is vr0, () is vr1 in the following classification comments. */
8469 if (mineq && maxeq)
8471 /* [( )] */
8472 if (*vr0type == vr1type)
8473 /* Nothing to do for equal ranges. */
8475 else if ((*vr0type == VR_RANGE
8476 && vr1type == VR_ANTI_RANGE)
8477 || (*vr0type == VR_ANTI_RANGE
8478 && vr1type == VR_RANGE))
8480 /* For anti-range with range intersection the result is empty. */
8481 *vr0type = VR_UNDEFINED;
8482 *vr0min = NULL_TREE;
8483 *vr0max = NULL_TREE;
8485 else
8486 gcc_unreachable ();
8488 else if (operand_less_p (*vr0max, vr1min) == 1
8489 || operand_less_p (vr1max, *vr0min) == 1)
8491 /* [ ] ( ) or ( ) [ ]
8492 If the ranges have an empty intersection, the result of the
8493 intersect operation is the range for intersecting an
8494 anti-range with a range or empty when intersecting two ranges. */
8495 if (*vr0type == VR_RANGE
8496 && vr1type == VR_ANTI_RANGE)
8498 else if (*vr0type == VR_ANTI_RANGE
8499 && vr1type == VR_RANGE)
8501 *vr0type = vr1type;
8502 *vr0min = vr1min;
8503 *vr0max = vr1max;
8505 else if (*vr0type == VR_RANGE
8506 && vr1type == VR_RANGE)
8508 *vr0type = VR_UNDEFINED;
8509 *vr0min = NULL_TREE;
8510 *vr0max = NULL_TREE;
8512 else if (*vr0type == VR_ANTI_RANGE
8513 && vr1type == VR_ANTI_RANGE)
8515 /* If the anti-ranges are adjacent to each other merge them. */
8516 if (TREE_CODE (*vr0max) == INTEGER_CST
8517 && TREE_CODE (vr1min) == INTEGER_CST
8518 && operand_less_p (*vr0max, vr1min) == 1
8519 && integer_onep (int_const_binop (MINUS_EXPR,
8520 vr1min, *vr0max)))
8521 *vr0max = vr1max;
8522 else if (TREE_CODE (vr1max) == INTEGER_CST
8523 && TREE_CODE (*vr0min) == INTEGER_CST
8524 && operand_less_p (vr1max, *vr0min) == 1
8525 && integer_onep (int_const_binop (MINUS_EXPR,
8526 *vr0min, vr1max)))
8527 *vr0min = vr1min;
8528 /* Else arbitrarily take VR0. */
8531 else if ((maxeq || operand_less_p (vr1max, *vr0max) == 1)
8532 && (mineq || operand_less_p (*vr0min, vr1min) == 1))
8534 /* [ ( ) ] or [( ) ] or [ ( )] */
8535 if (*vr0type == VR_RANGE
8536 && vr1type == VR_RANGE)
8538 /* If both are ranges the result is the inner one. */
8539 *vr0type = vr1type;
8540 *vr0min = vr1min;
8541 *vr0max = vr1max;
8543 else if (*vr0type == VR_RANGE
8544 && vr1type == VR_ANTI_RANGE)
8546 /* Choose the right gap if the left one is empty. */
8547 if (mineq)
8549 if (TREE_CODE (vr1max) != INTEGER_CST)
8550 *vr0min = vr1max;
8551 else if (TYPE_PRECISION (TREE_TYPE (vr1max)) == 1
8552 && !TYPE_UNSIGNED (TREE_TYPE (vr1max)))
8553 *vr0min
8554 = int_const_binop (MINUS_EXPR, vr1max,
8555 build_int_cst (TREE_TYPE (vr1max), -1));
8556 else
8557 *vr0min
8558 = int_const_binop (PLUS_EXPR, vr1max,
8559 build_int_cst (TREE_TYPE (vr1max), 1));
8561 /* Choose the left gap if the right one is empty. */
8562 else if (maxeq)
8564 if (TREE_CODE (vr1min) != INTEGER_CST)
8565 *vr0max = vr1min;
8566 else if (TYPE_PRECISION (TREE_TYPE (vr1min)) == 1
8567 && !TYPE_UNSIGNED (TREE_TYPE (vr1min)))
8568 *vr0max
8569 = int_const_binop (PLUS_EXPR, vr1min,
8570 build_int_cst (TREE_TYPE (vr1min), -1));
8571 else
8572 *vr0max
8573 = int_const_binop (MINUS_EXPR, vr1min,
8574 build_int_cst (TREE_TYPE (vr1min), 1));
8576 /* Choose the anti-range if the range is effectively varying. */
8577 else if (vrp_val_is_min (*vr0min)
8578 && vrp_val_is_max (*vr0max))
8580 *vr0type = vr1type;
8581 *vr0min = vr1min;
8582 *vr0max = vr1max;
8584 /* Else choose the range. */
8586 else if (*vr0type == VR_ANTI_RANGE
8587 && vr1type == VR_ANTI_RANGE)
8588 /* If both are anti-ranges the result is the outer one. */
8590 else if (*vr0type == VR_ANTI_RANGE
8591 && vr1type == VR_RANGE)
8593 /* The intersection is empty. */
8594 *vr0type = VR_UNDEFINED;
8595 *vr0min = NULL_TREE;
8596 *vr0max = NULL_TREE;
8598 else
8599 gcc_unreachable ();
8601 else if ((maxeq || operand_less_p (*vr0max, vr1max) == 1)
8602 && (mineq || operand_less_p (vr1min, *vr0min) == 1))
8604 /* ( [ ] ) or ([ ] ) or ( [ ]) */
8605 if (*vr0type == VR_RANGE
8606 && vr1type == VR_RANGE)
8607 /* Choose the inner range. */
8609 else if (*vr0type == VR_ANTI_RANGE
8610 && vr1type == VR_RANGE)
8612 /* Choose the right gap if the left is empty. */
8613 if (mineq)
8615 *vr0type = VR_RANGE;
8616 if (TREE_CODE (*vr0max) != INTEGER_CST)
8617 *vr0min = *vr0max;
8618 else if (TYPE_PRECISION (TREE_TYPE (*vr0max)) == 1
8619 && !TYPE_UNSIGNED (TREE_TYPE (*vr0max)))
8620 *vr0min
8621 = int_const_binop (MINUS_EXPR, *vr0max,
8622 build_int_cst (TREE_TYPE (*vr0max), -1));
8623 else
8624 *vr0min
8625 = int_const_binop (PLUS_EXPR, *vr0max,
8626 build_int_cst (TREE_TYPE (*vr0max), 1));
8627 *vr0max = vr1max;
8629 /* Choose the left gap if the right is empty. */
8630 else if (maxeq)
8632 *vr0type = VR_RANGE;
8633 if (TREE_CODE (*vr0min) != INTEGER_CST)
8634 *vr0max = *vr0min;
8635 else if (TYPE_PRECISION (TREE_TYPE (*vr0min)) == 1
8636 && !TYPE_UNSIGNED (TREE_TYPE (*vr0min)))
8637 *vr0max
8638 = int_const_binop (PLUS_EXPR, *vr0min,
8639 build_int_cst (TREE_TYPE (*vr0min), -1));
8640 else
8641 *vr0max
8642 = int_const_binop (MINUS_EXPR, *vr0min,
8643 build_int_cst (TREE_TYPE (*vr0min), 1));
8644 *vr0min = vr1min;
8646 /* Choose the anti-range if the range is effectively varying. */
8647 else if (vrp_val_is_min (vr1min)
8648 && vrp_val_is_max (vr1max))
8650 /* Choose the anti-range if it is ~[0,0], that range is special
8651 enough to special case when vr1's range is relatively wide. */
8652 else if (*vr0min == *vr0max
8653 && integer_zerop (*vr0min)
8654 && (TYPE_PRECISION (TREE_TYPE (*vr0min))
8655 == TYPE_PRECISION (ptr_type_node))
8656 && TREE_CODE (vr1max) == INTEGER_CST
8657 && TREE_CODE (vr1min) == INTEGER_CST
8658 && (wi::clz (wi::sub (vr1max, vr1min))
8659 < TYPE_PRECISION (TREE_TYPE (*vr0min)) / 2))
8661 /* Else choose the range. */
8662 else
8664 *vr0type = vr1type;
8665 *vr0min = vr1min;
8666 *vr0max = vr1max;
8669 else if (*vr0type == VR_ANTI_RANGE
8670 && vr1type == VR_ANTI_RANGE)
8672 /* If both are anti-ranges the result is the outer one. */
8673 *vr0type = vr1type;
8674 *vr0min = vr1min;
8675 *vr0max = vr1max;
8677 else if (vr1type == VR_ANTI_RANGE
8678 && *vr0type == VR_RANGE)
8680 /* The intersection is empty. */
8681 *vr0type = VR_UNDEFINED;
8682 *vr0min = NULL_TREE;
8683 *vr0max = NULL_TREE;
8685 else
8686 gcc_unreachable ();
8688 else if ((operand_less_p (vr1min, *vr0max) == 1
8689 || operand_equal_p (vr1min, *vr0max, 0))
8690 && operand_less_p (*vr0min, vr1min) == 1)
8692 /* [ ( ] ) or [ ]( ) */
8693 if (*vr0type == VR_ANTI_RANGE
8694 && vr1type == VR_ANTI_RANGE)
8695 *vr0max = vr1max;
8696 else if (*vr0type == VR_RANGE
8697 && vr1type == VR_RANGE)
8698 *vr0min = vr1min;
8699 else if (*vr0type == VR_RANGE
8700 && vr1type == VR_ANTI_RANGE)
8702 if (TREE_CODE (vr1min) == INTEGER_CST)
8703 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
8704 build_int_cst (TREE_TYPE (vr1min), 1));
8705 else
8706 *vr0max = vr1min;
8708 else if (*vr0type == VR_ANTI_RANGE
8709 && vr1type == VR_RANGE)
8711 *vr0type = VR_RANGE;
8712 if (TREE_CODE (*vr0max) == INTEGER_CST)
8713 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
8714 build_int_cst (TREE_TYPE (*vr0max), 1));
8715 else
8716 *vr0min = *vr0max;
8717 *vr0max = vr1max;
8719 else
8720 gcc_unreachable ();
8722 else if ((operand_less_p (*vr0min, vr1max) == 1
8723 || operand_equal_p (*vr0min, vr1max, 0))
8724 && operand_less_p (vr1min, *vr0min) == 1)
8726 /* ( [ ) ] or ( )[ ] */
8727 if (*vr0type == VR_ANTI_RANGE
8728 && vr1type == VR_ANTI_RANGE)
8729 *vr0min = vr1min;
8730 else if (*vr0type == VR_RANGE
8731 && vr1type == VR_RANGE)
8732 *vr0max = vr1max;
8733 else if (*vr0type == VR_RANGE
8734 && vr1type == VR_ANTI_RANGE)
8736 if (TREE_CODE (vr1max) == INTEGER_CST)
8737 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
8738 build_int_cst (TREE_TYPE (vr1max), 1));
8739 else
8740 *vr0min = vr1max;
8742 else if (*vr0type == VR_ANTI_RANGE
8743 && vr1type == VR_RANGE)
8745 *vr0type = VR_RANGE;
8746 if (TREE_CODE (*vr0min) == INTEGER_CST)
8747 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
8748 build_int_cst (TREE_TYPE (*vr0min), 1));
8749 else
8750 *vr0max = *vr0min;
8751 *vr0min = vr1min;
8753 else
8754 gcc_unreachable ();
8757 /* As a fallback simply use { *VRTYPE, *VR0MIN, *VR0MAX } as
8758 result for the intersection. That's always a conservative
8759 correct estimate unless VR1 is a constant singleton range
8760 in which case we choose that. */
8761 if (vr1type == VR_RANGE
8762 && is_gimple_min_invariant (vr1min)
8763 && vrp_operand_equal_p (vr1min, vr1max))
8765 *vr0type = vr1type;
8766 *vr0min = vr1min;
8767 *vr0max = vr1max;
8770 return;
8774 /* Intersect the two value-ranges *VR0 and *VR1 and store the result
8775 in *VR0. This may not be the smallest possible such range. */
8777 static void
8778 vrp_intersect_ranges_1 (value_range *vr0, value_range *vr1)
8780 value_range saved;
8782 /* If either range is VR_VARYING the other one wins. */
8783 if (vr1->type == VR_VARYING)
8784 return;
8785 if (vr0->type == VR_VARYING)
8787 copy_value_range (vr0, vr1);
8788 return;
8791 /* When either range is VR_UNDEFINED the resulting range is
8792 VR_UNDEFINED, too. */
8793 if (vr0->type == VR_UNDEFINED)
8794 return;
8795 if (vr1->type == VR_UNDEFINED)
8797 set_value_range_to_undefined (vr0);
8798 return;
8801 /* Save the original vr0 so we can return it as conservative intersection
8802 result when our worker turns things to varying. */
8803 saved = *vr0;
8804 intersect_ranges (&vr0->type, &vr0->min, &vr0->max,
8805 vr1->type, vr1->min, vr1->max);
8806 /* Make sure to canonicalize the result though as the inversion of a
8807 VR_RANGE can still be a VR_RANGE. */
8808 set_and_canonicalize_value_range (vr0, vr0->type,
8809 vr0->min, vr0->max, vr0->equiv);
8810 /* If that failed, use the saved original VR0. */
8811 if (vr0->type == VR_VARYING)
8813 *vr0 = saved;
8814 return;
8816 /* If the result is VR_UNDEFINED there is no need to mess with
8817 the equivalencies. */
8818 if (vr0->type == VR_UNDEFINED)
8819 return;
8821 /* The resulting set of equivalences for range intersection is the union of
8822 the two sets. */
8823 if (vr0->equiv && vr1->equiv && vr0->equiv != vr1->equiv)
8824 bitmap_ior_into (vr0->equiv, vr1->equiv);
8825 else if (vr1->equiv && !vr0->equiv)
8827 vr0->equiv = BITMAP_ALLOC (&vrp_equiv_obstack);
8828 bitmap_copy (vr0->equiv, vr1->equiv);
8832 void
8833 vrp_intersect_ranges (value_range *vr0, value_range *vr1)
8835 if (dump_file && (dump_flags & TDF_DETAILS))
8837 fprintf (dump_file, "Intersecting\n ");
8838 dump_value_range (dump_file, vr0);
8839 fprintf (dump_file, "\nand\n ");
8840 dump_value_range (dump_file, vr1);
8841 fprintf (dump_file, "\n");
8843 vrp_intersect_ranges_1 (vr0, vr1);
8844 if (dump_file && (dump_flags & TDF_DETAILS))
8846 fprintf (dump_file, "to\n ");
8847 dump_value_range (dump_file, vr0);
8848 fprintf (dump_file, "\n");
8852 /* Meet operation for value ranges. Given two value ranges VR0 and
8853 VR1, store in VR0 a range that contains both VR0 and VR1. This
8854 may not be the smallest possible such range. */
8856 static void
8857 vrp_meet_1 (value_range *vr0, const value_range *vr1)
8859 value_range saved;
8861 if (vr0->type == VR_UNDEFINED)
8863 set_value_range (vr0, vr1->type, vr1->min, vr1->max, vr1->equiv);
8864 return;
8867 if (vr1->type == VR_UNDEFINED)
8869 /* VR0 already has the resulting range. */
8870 return;
8873 if (vr0->type == VR_VARYING)
8875 /* Nothing to do. VR0 already has the resulting range. */
8876 return;
8879 if (vr1->type == VR_VARYING)
8881 set_value_range_to_varying (vr0);
8882 return;
8885 saved = *vr0;
8886 union_ranges (&vr0->type, &vr0->min, &vr0->max,
8887 vr1->type, vr1->min, vr1->max);
8888 if (vr0->type == VR_VARYING)
8890 /* Failed to find an efficient meet. Before giving up and setting
8891 the result to VARYING, see if we can at least derive a useful
8892 anti-range. FIXME, all this nonsense about distinguishing
8893 anti-ranges from ranges is necessary because of the odd
8894 semantics of range_includes_zero_p and friends. */
8895 if (((saved.type == VR_RANGE
8896 && range_includes_zero_p (saved.min, saved.max) == 0)
8897 || (saved.type == VR_ANTI_RANGE
8898 && range_includes_zero_p (saved.min, saved.max) == 1))
8899 && ((vr1->type == VR_RANGE
8900 && range_includes_zero_p (vr1->min, vr1->max) == 0)
8901 || (vr1->type == VR_ANTI_RANGE
8902 && range_includes_zero_p (vr1->min, vr1->max) == 1)))
8904 set_value_range_to_nonnull (vr0, TREE_TYPE (saved.min));
8906 /* Since this meet operation did not result from the meeting of
8907 two equivalent names, VR0 cannot have any equivalences. */
8908 if (vr0->equiv)
8909 bitmap_clear (vr0->equiv);
8910 return;
8913 set_value_range_to_varying (vr0);
8914 return;
8916 set_and_canonicalize_value_range (vr0, vr0->type, vr0->min, vr0->max,
8917 vr0->equiv);
8918 if (vr0->type == VR_VARYING)
8919 return;
8921 /* The resulting set of equivalences is always the intersection of
8922 the two sets. */
8923 if (vr0->equiv && vr1->equiv && vr0->equiv != vr1->equiv)
8924 bitmap_and_into (vr0->equiv, vr1->equiv);
8925 else if (vr0->equiv && !vr1->equiv)
8926 bitmap_clear (vr0->equiv);
8929 void
8930 vrp_meet (value_range *vr0, const value_range *vr1)
8932 if (dump_file && (dump_flags & TDF_DETAILS))
8934 fprintf (dump_file, "Meeting\n ");
8935 dump_value_range (dump_file, vr0);
8936 fprintf (dump_file, "\nand\n ");
8937 dump_value_range (dump_file, vr1);
8938 fprintf (dump_file, "\n");
8940 vrp_meet_1 (vr0, vr1);
8941 if (dump_file && (dump_flags & TDF_DETAILS))
8943 fprintf (dump_file, "to\n ");
8944 dump_value_range (dump_file, vr0);
8945 fprintf (dump_file, "\n");
8950 /* Visit all arguments for PHI node PHI that flow through executable
8951 edges. If a valid value range can be derived from all the incoming
8952 value ranges, set a new range in VR_RESULT. */
8954 static void
8955 extract_range_from_phi_node (gphi *phi, value_range *vr_result)
8957 size_t i;
8958 tree lhs = PHI_RESULT (phi);
8959 value_range *lhs_vr = get_value_range (lhs);
8960 bool first = true;
8961 int edges, old_edges;
8962 struct loop *l;
8964 if (dump_file && (dump_flags & TDF_DETAILS))
8966 fprintf (dump_file, "\nVisiting PHI node: ");
8967 print_gimple_stmt (dump_file, phi, 0, dump_flags);
8970 bool may_simulate_backedge_again = false;
8971 edges = 0;
8972 for (i = 0; i < gimple_phi_num_args (phi); i++)
8974 edge e = gimple_phi_arg_edge (phi, i);
8976 if (dump_file && (dump_flags & TDF_DETAILS))
8978 fprintf (dump_file,
8979 " Argument #%d (%d -> %d %sexecutable)\n",
8980 (int) i, e->src->index, e->dest->index,
8981 (e->flags & EDGE_EXECUTABLE) ? "" : "not ");
8984 if (e->flags & EDGE_EXECUTABLE)
8986 tree arg = PHI_ARG_DEF (phi, i);
8987 value_range vr_arg;
8989 ++edges;
8991 if (TREE_CODE (arg) == SSA_NAME)
8993 /* See if we are eventually going to change one of the args. */
8994 gimple *def_stmt = SSA_NAME_DEF_STMT (arg);
8995 if (! gimple_nop_p (def_stmt)
8996 && prop_simulate_again_p (def_stmt)
8997 && e->flags & EDGE_DFS_BACK)
8998 may_simulate_backedge_again = true;
9000 vr_arg = *(get_value_range (arg));
9001 /* Do not allow equivalences or symbolic ranges to leak in from
9002 backedges. That creates invalid equivalencies.
9003 See PR53465 and PR54767. */
9004 if (e->flags & EDGE_DFS_BACK)
9006 if (vr_arg.type == VR_RANGE
9007 || vr_arg.type == VR_ANTI_RANGE)
9009 vr_arg.equiv = NULL;
9010 if (symbolic_range_p (&vr_arg))
9012 vr_arg.type = VR_VARYING;
9013 vr_arg.min = NULL_TREE;
9014 vr_arg.max = NULL_TREE;
9018 else
9020 /* If the non-backedge arguments range is VR_VARYING then
9021 we can still try recording a simple equivalence. */
9022 if (vr_arg.type == VR_VARYING)
9024 vr_arg.type = VR_RANGE;
9025 vr_arg.min = arg;
9026 vr_arg.max = arg;
9027 vr_arg.equiv = NULL;
9031 else
9033 if (TREE_OVERFLOW_P (arg))
9034 arg = drop_tree_overflow (arg);
9036 vr_arg.type = VR_RANGE;
9037 vr_arg.min = arg;
9038 vr_arg.max = arg;
9039 vr_arg.equiv = NULL;
9042 if (dump_file && (dump_flags & TDF_DETAILS))
9044 fprintf (dump_file, "\t");
9045 print_generic_expr (dump_file, arg, dump_flags);
9046 fprintf (dump_file, ": ");
9047 dump_value_range (dump_file, &vr_arg);
9048 fprintf (dump_file, "\n");
9051 if (first)
9052 copy_value_range (vr_result, &vr_arg);
9053 else
9054 vrp_meet (vr_result, &vr_arg);
9055 first = false;
9057 if (vr_result->type == VR_VARYING)
9058 break;
9062 if (vr_result->type == VR_VARYING)
9063 goto varying;
9064 else if (vr_result->type == VR_UNDEFINED)
9065 goto update_range;
9067 old_edges = vr_phi_edge_counts[SSA_NAME_VERSION (lhs)];
9068 vr_phi_edge_counts[SSA_NAME_VERSION (lhs)] = edges;
9070 /* To prevent infinite iterations in the algorithm, derive ranges
9071 when the new value is slightly bigger or smaller than the
9072 previous one. We don't do this if we have seen a new executable
9073 edge; this helps us avoid an infinity for conditionals
9074 which are not in a loop. If the old value-range was VR_UNDEFINED
9075 use the updated range and iterate one more time. If we will not
9076 simulate this PHI again via the backedge allow us to iterate. */
9077 if (edges > 0
9078 && gimple_phi_num_args (phi) > 1
9079 && edges == old_edges
9080 && lhs_vr->type != VR_UNDEFINED
9081 && may_simulate_backedge_again)
9083 /* Compare old and new ranges, fall back to varying if the
9084 values are not comparable. */
9085 int cmp_min = compare_values (lhs_vr->min, vr_result->min);
9086 if (cmp_min == -2)
9087 goto varying;
9088 int cmp_max = compare_values (lhs_vr->max, vr_result->max);
9089 if (cmp_max == -2)
9090 goto varying;
9092 /* For non VR_RANGE or for pointers fall back to varying if
9093 the range changed. */
9094 if ((lhs_vr->type != VR_RANGE || vr_result->type != VR_RANGE
9095 || POINTER_TYPE_P (TREE_TYPE (lhs)))
9096 && (cmp_min != 0 || cmp_max != 0))
9097 goto varying;
9099 /* If the new minimum is larger than the previous one
9100 retain the old value. If the new minimum value is smaller
9101 than the previous one and not -INF go all the way to -INF + 1.
9102 In the first case, to avoid infinite bouncing between different
9103 minimums, and in the other case to avoid iterating millions of
9104 times to reach -INF. Going to -INF + 1 also lets the following
9105 iteration compute whether there will be any overflow, at the
9106 expense of one additional iteration. */
9107 if (cmp_min < 0)
9108 vr_result->min = lhs_vr->min;
9109 else if (cmp_min > 0
9110 && !vrp_val_is_min (vr_result->min))
9111 vr_result->min
9112 = int_const_binop (PLUS_EXPR,
9113 vrp_val_min (TREE_TYPE (vr_result->min)),
9114 build_int_cst (TREE_TYPE (vr_result->min), 1));
9116 /* Similarly for the maximum value. */
9117 if (cmp_max > 0)
9118 vr_result->max = lhs_vr->max;
9119 else if (cmp_max < 0
9120 && !vrp_val_is_max (vr_result->max))
9121 vr_result->max
9122 = int_const_binop (MINUS_EXPR,
9123 vrp_val_max (TREE_TYPE (vr_result->min)),
9124 build_int_cst (TREE_TYPE (vr_result->min), 1));
9126 /* If we dropped either bound to +-INF then if this is a loop
9127 PHI node SCEV may known more about its value-range. */
9128 if (cmp_min > 0 || cmp_min < 0
9129 || cmp_max < 0 || cmp_max > 0)
9130 goto scev_check;
9132 goto infinite_check;
9135 goto update_range;
9137 varying:
9138 set_value_range_to_varying (vr_result);
9140 scev_check:
9141 /* If this is a loop PHI node SCEV may known more about its value-range.
9142 scev_check can be reached from two paths, one is a fall through from above
9143 "varying" label, the other is direct goto from code block which tries to
9144 avoid infinite simulation. */
9145 if ((l = loop_containing_stmt (phi))
9146 && l->header == gimple_bb (phi))
9147 adjust_range_with_scev (vr_result, l, phi, lhs);
9149 infinite_check:
9150 /* If we will end up with a (-INF, +INF) range, set it to
9151 VARYING. Same if the previous max value was invalid for
9152 the type and we end up with vr_result.min > vr_result.max. */
9153 if ((vr_result->type == VR_RANGE || vr_result->type == VR_ANTI_RANGE)
9154 && !((vrp_val_is_max (vr_result->max) && vrp_val_is_min (vr_result->min))
9155 || compare_values (vr_result->min, vr_result->max) > 0))
9157 else
9158 set_value_range_to_varying (vr_result);
9160 /* If the new range is different than the previous value, keep
9161 iterating. */
9162 update_range:
9163 return;
9166 /* Visit all arguments for PHI node PHI that flow through executable
9167 edges. If a valid value range can be derived from all the incoming
9168 value ranges, set a new range for the LHS of PHI. */
9170 static enum ssa_prop_result
9171 vrp_visit_phi_node (gphi *phi)
9173 tree lhs = PHI_RESULT (phi);
9174 value_range vr_result = VR_INITIALIZER;
9175 extract_range_from_phi_node (phi, &vr_result);
9176 if (update_value_range (lhs, &vr_result))
9178 if (dump_file && (dump_flags & TDF_DETAILS))
9180 fprintf (dump_file, "Found new range for ");
9181 print_generic_expr (dump_file, lhs);
9182 fprintf (dump_file, ": ");
9183 dump_value_range (dump_file, &vr_result);
9184 fprintf (dump_file, "\n");
9187 if (vr_result.type == VR_VARYING)
9188 return SSA_PROP_VARYING;
9190 return SSA_PROP_INTERESTING;
9193 /* Nothing changed, don't add outgoing edges. */
9194 return SSA_PROP_NOT_INTERESTING;
9197 /* Simplify boolean operations if the source is known
9198 to be already a boolean. */
9199 static bool
9200 simplify_truth_ops_using_ranges (gimple_stmt_iterator *gsi, gimple *stmt)
9202 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
9203 tree lhs, op0, op1;
9204 bool need_conversion;
9206 /* We handle only !=/== case here. */
9207 gcc_assert (rhs_code == EQ_EXPR || rhs_code == NE_EXPR);
9209 op0 = gimple_assign_rhs1 (stmt);
9210 if (!op_with_boolean_value_range_p (op0))
9211 return false;
9213 op1 = gimple_assign_rhs2 (stmt);
9214 if (!op_with_boolean_value_range_p (op1))
9215 return false;
9217 /* Reduce number of cases to handle to NE_EXPR. As there is no
9218 BIT_XNOR_EXPR we cannot replace A == B with a single statement. */
9219 if (rhs_code == EQ_EXPR)
9221 if (TREE_CODE (op1) == INTEGER_CST)
9222 op1 = int_const_binop (BIT_XOR_EXPR, op1,
9223 build_int_cst (TREE_TYPE (op1), 1));
9224 else
9225 return false;
9228 lhs = gimple_assign_lhs (stmt);
9229 need_conversion
9230 = !useless_type_conversion_p (TREE_TYPE (lhs), TREE_TYPE (op0));
9232 /* Make sure to not sign-extend a 1-bit 1 when converting the result. */
9233 if (need_conversion
9234 && !TYPE_UNSIGNED (TREE_TYPE (op0))
9235 && TYPE_PRECISION (TREE_TYPE (op0)) == 1
9236 && TYPE_PRECISION (TREE_TYPE (lhs)) > 1)
9237 return false;
9239 /* For A != 0 we can substitute A itself. */
9240 if (integer_zerop (op1))
9241 gimple_assign_set_rhs_with_ops (gsi,
9242 need_conversion
9243 ? NOP_EXPR : TREE_CODE (op0), op0);
9244 /* For A != B we substitute A ^ B. Either with conversion. */
9245 else if (need_conversion)
9247 tree tem = make_ssa_name (TREE_TYPE (op0));
9248 gassign *newop
9249 = gimple_build_assign (tem, BIT_XOR_EXPR, op0, op1);
9250 gsi_insert_before (gsi, newop, GSI_SAME_STMT);
9251 if (INTEGRAL_TYPE_P (TREE_TYPE (tem))
9252 && TYPE_PRECISION (TREE_TYPE (tem)) > 1)
9253 set_range_info (tem, VR_RANGE,
9254 wi::zero (TYPE_PRECISION (TREE_TYPE (tem))),
9255 wi::one (TYPE_PRECISION (TREE_TYPE (tem))));
9256 gimple_assign_set_rhs_with_ops (gsi, NOP_EXPR, tem);
9258 /* Or without. */
9259 else
9260 gimple_assign_set_rhs_with_ops (gsi, BIT_XOR_EXPR, op0, op1);
9261 update_stmt (gsi_stmt (*gsi));
9262 fold_stmt (gsi, follow_single_use_edges);
9264 return true;
9267 /* Simplify a division or modulo operator to a right shift or bitwise and
9268 if the first operand is unsigned or is greater than zero and the second
9269 operand is an exact power of two. For TRUNC_MOD_EXPR op0 % op1 with
9270 constant op1 (op1min = op1) or with op1 in [op1min, op1max] range,
9271 optimize it into just op0 if op0's range is known to be a subset of
9272 [-op1min + 1, op1min - 1] for signed and [0, op1min - 1] for unsigned
9273 modulo. */
9275 static bool
9276 simplify_div_or_mod_using_ranges (gimple_stmt_iterator *gsi, gimple *stmt)
9278 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
9279 tree val = NULL;
9280 tree op0 = gimple_assign_rhs1 (stmt);
9281 tree op1 = gimple_assign_rhs2 (stmt);
9282 tree op0min = NULL_TREE, op0max = NULL_TREE;
9283 tree op1min = op1;
9284 value_range *vr = NULL;
9286 if (TREE_CODE (op0) == INTEGER_CST)
9288 op0min = op0;
9289 op0max = op0;
9291 else
9293 vr = get_value_range (op0);
9294 if (range_int_cst_p (vr))
9296 op0min = vr->min;
9297 op0max = vr->max;
9301 if (rhs_code == TRUNC_MOD_EXPR
9302 && TREE_CODE (op1) == SSA_NAME)
9304 value_range *vr1 = get_value_range (op1);
9305 if (range_int_cst_p (vr1))
9306 op1min = vr1->min;
9308 if (rhs_code == TRUNC_MOD_EXPR
9309 && TREE_CODE (op1min) == INTEGER_CST
9310 && tree_int_cst_sgn (op1min) == 1
9311 && op0max
9312 && tree_int_cst_lt (op0max, op1min))
9314 if (TYPE_UNSIGNED (TREE_TYPE (op0))
9315 || tree_int_cst_sgn (op0min) >= 0
9316 || tree_int_cst_lt (fold_unary (NEGATE_EXPR, TREE_TYPE (op1min), op1min),
9317 op0min))
9319 /* If op0 already has the range op0 % op1 has,
9320 then TRUNC_MOD_EXPR won't change anything. */
9321 gimple_assign_set_rhs_from_tree (gsi, op0);
9322 return true;
9326 if (TREE_CODE (op0) != SSA_NAME)
9327 return false;
9329 if (!integer_pow2p (op1))
9331 /* X % -Y can be only optimized into X % Y either if
9332 X is not INT_MIN, or Y is not -1. Fold it now, as after
9333 remove_range_assertions the range info might be not available
9334 anymore. */
9335 if (rhs_code == TRUNC_MOD_EXPR
9336 && fold_stmt (gsi, follow_single_use_edges))
9337 return true;
9338 return false;
9341 if (TYPE_UNSIGNED (TREE_TYPE (op0)))
9342 val = integer_one_node;
9343 else
9345 bool sop = false;
9347 val = compare_range_with_value (GE_EXPR, vr, integer_zero_node, &sop);
9349 if (val
9350 && sop
9351 && integer_onep (val)
9352 && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC))
9354 location_t location;
9356 if (!gimple_has_location (stmt))
9357 location = input_location;
9358 else
9359 location = gimple_location (stmt);
9360 warning_at (location, OPT_Wstrict_overflow,
9361 "assuming signed overflow does not occur when "
9362 "simplifying %</%> or %<%%%> to %<>>%> or %<&%>");
9366 if (val && integer_onep (val))
9368 tree t;
9370 if (rhs_code == TRUNC_DIV_EXPR)
9372 t = build_int_cst (integer_type_node, tree_log2 (op1));
9373 gimple_assign_set_rhs_code (stmt, RSHIFT_EXPR);
9374 gimple_assign_set_rhs1 (stmt, op0);
9375 gimple_assign_set_rhs2 (stmt, t);
9377 else
9379 t = build_int_cst (TREE_TYPE (op1), 1);
9380 t = int_const_binop (MINUS_EXPR, op1, t);
9381 t = fold_convert (TREE_TYPE (op0), t);
9383 gimple_assign_set_rhs_code (stmt, BIT_AND_EXPR);
9384 gimple_assign_set_rhs1 (stmt, op0);
9385 gimple_assign_set_rhs2 (stmt, t);
9388 update_stmt (stmt);
9389 fold_stmt (gsi, follow_single_use_edges);
9390 return true;
9393 return false;
9396 /* Simplify a min or max if the ranges of the two operands are
9397 disjoint. Return true if we do simplify. */
9399 static bool
9400 simplify_min_or_max_using_ranges (gimple_stmt_iterator *gsi, gimple *stmt)
9402 tree op0 = gimple_assign_rhs1 (stmt);
9403 tree op1 = gimple_assign_rhs2 (stmt);
9404 bool sop = false;
9405 tree val;
9407 val = (vrp_evaluate_conditional_warnv_with_ops_using_ranges
9408 (LE_EXPR, op0, op1, &sop));
9409 if (!val)
9411 sop = false;
9412 val = (vrp_evaluate_conditional_warnv_with_ops_using_ranges
9413 (LT_EXPR, op0, op1, &sop));
9416 if (val)
9418 if (sop && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC))
9420 location_t location;
9422 if (!gimple_has_location (stmt))
9423 location = input_location;
9424 else
9425 location = gimple_location (stmt);
9426 warning_at (location, OPT_Wstrict_overflow,
9427 "assuming signed overflow does not occur when "
9428 "simplifying %<min/max (X,Y)%> to %<X%> or %<Y%>");
9431 /* VAL == TRUE -> OP0 < or <= op1
9432 VAL == FALSE -> OP0 > or >= op1. */
9433 tree res = ((gimple_assign_rhs_code (stmt) == MAX_EXPR)
9434 == integer_zerop (val)) ? op0 : op1;
9435 gimple_assign_set_rhs_from_tree (gsi, res);
9436 return true;
9439 return false;
9442 /* If the operand to an ABS_EXPR is >= 0, then eliminate the
9443 ABS_EXPR. If the operand is <= 0, then simplify the
9444 ABS_EXPR into a NEGATE_EXPR. */
9446 static bool
9447 simplify_abs_using_ranges (gimple_stmt_iterator *gsi, gimple *stmt)
9449 tree op = gimple_assign_rhs1 (stmt);
9450 value_range *vr = get_value_range (op);
9452 if (vr)
9454 tree val = NULL;
9455 bool sop = false;
9457 val = compare_range_with_value (LE_EXPR, vr, integer_zero_node, &sop);
9458 if (!val)
9460 /* The range is neither <= 0 nor > 0. Now see if it is
9461 either < 0 or >= 0. */
9462 sop = false;
9463 val = compare_range_with_value (LT_EXPR, vr, integer_zero_node,
9464 &sop);
9467 if (val)
9469 if (sop && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC))
9471 location_t location;
9473 if (!gimple_has_location (stmt))
9474 location = input_location;
9475 else
9476 location = gimple_location (stmt);
9477 warning_at (location, OPT_Wstrict_overflow,
9478 "assuming signed overflow does not occur when "
9479 "simplifying %<abs (X)%> to %<X%> or %<-X%>");
9482 gimple_assign_set_rhs1 (stmt, op);
9483 if (integer_zerop (val))
9484 gimple_assign_set_rhs_code (stmt, SSA_NAME);
9485 else
9486 gimple_assign_set_rhs_code (stmt, NEGATE_EXPR);
9487 update_stmt (stmt);
9488 fold_stmt (gsi, follow_single_use_edges);
9489 return true;
9493 return false;
9496 /* Optimize away redundant BIT_AND_EXPR and BIT_IOR_EXPR.
9497 If all the bits that are being cleared by & are already
9498 known to be zero from VR, or all the bits that are being
9499 set by | are already known to be one from VR, the bit
9500 operation is redundant. */
9502 static bool
9503 simplify_bit_ops_using_ranges (gimple_stmt_iterator *gsi, gimple *stmt)
9505 tree op0 = gimple_assign_rhs1 (stmt);
9506 tree op1 = gimple_assign_rhs2 (stmt);
9507 tree op = NULL_TREE;
9508 value_range vr0 = VR_INITIALIZER;
9509 value_range vr1 = VR_INITIALIZER;
9510 wide_int may_be_nonzero0, may_be_nonzero1;
9511 wide_int must_be_nonzero0, must_be_nonzero1;
9512 wide_int mask;
9514 if (TREE_CODE (op0) == SSA_NAME)
9515 vr0 = *(get_value_range (op0));
9516 else if (is_gimple_min_invariant (op0))
9517 set_value_range_to_value (&vr0, op0, NULL);
9518 else
9519 return false;
9521 if (TREE_CODE (op1) == SSA_NAME)
9522 vr1 = *(get_value_range (op1));
9523 else if (is_gimple_min_invariant (op1))
9524 set_value_range_to_value (&vr1, op1, NULL);
9525 else
9526 return false;
9528 if (!zero_nonzero_bits_from_vr (TREE_TYPE (op0), &vr0, &may_be_nonzero0,
9529 &must_be_nonzero0))
9530 return false;
9531 if (!zero_nonzero_bits_from_vr (TREE_TYPE (op1), &vr1, &may_be_nonzero1,
9532 &must_be_nonzero1))
9533 return false;
9535 switch (gimple_assign_rhs_code (stmt))
9537 case BIT_AND_EXPR:
9538 mask = may_be_nonzero0.and_not (must_be_nonzero1);
9539 if (mask == 0)
9541 op = op0;
9542 break;
9544 mask = may_be_nonzero1.and_not (must_be_nonzero0);
9545 if (mask == 0)
9547 op = op1;
9548 break;
9550 break;
9551 case BIT_IOR_EXPR:
9552 mask = may_be_nonzero0.and_not (must_be_nonzero1);
9553 if (mask == 0)
9555 op = op1;
9556 break;
9558 mask = may_be_nonzero1.and_not (must_be_nonzero0);
9559 if (mask == 0)
9561 op = op0;
9562 break;
9564 break;
9565 default:
9566 gcc_unreachable ();
9569 if (op == NULL_TREE)
9570 return false;
9572 gimple_assign_set_rhs_with_ops (gsi, TREE_CODE (op), op);
9573 update_stmt (gsi_stmt (*gsi));
9574 return true;
9577 /* We are comparing trees OP0 and OP1 using COND_CODE. OP0 has
9578 a known value range VR.
9580 If there is one and only one value which will satisfy the
9581 conditional, then return that value. Else return NULL.
9583 If signed overflow must be undefined for the value to satisfy
9584 the conditional, then set *STRICT_OVERFLOW_P to true. */
9586 static tree
9587 test_for_singularity (enum tree_code cond_code, tree op0,
9588 tree op1, value_range *vr)
9590 tree min = NULL;
9591 tree max = NULL;
9593 /* Extract minimum/maximum values which satisfy the conditional as it was
9594 written. */
9595 if (cond_code == LE_EXPR || cond_code == LT_EXPR)
9597 min = TYPE_MIN_VALUE (TREE_TYPE (op0));
9599 max = op1;
9600 if (cond_code == LT_EXPR)
9602 tree one = build_int_cst (TREE_TYPE (op0), 1);
9603 max = fold_build2 (MINUS_EXPR, TREE_TYPE (op0), max, one);
9604 /* Signal to compare_values_warnv this expr doesn't overflow. */
9605 if (EXPR_P (max))
9606 TREE_NO_WARNING (max) = 1;
9609 else if (cond_code == GE_EXPR || cond_code == GT_EXPR)
9611 max = TYPE_MAX_VALUE (TREE_TYPE (op0));
9613 min = op1;
9614 if (cond_code == GT_EXPR)
9616 tree one = build_int_cst (TREE_TYPE (op0), 1);
9617 min = fold_build2 (PLUS_EXPR, TREE_TYPE (op0), min, one);
9618 /* Signal to compare_values_warnv this expr doesn't overflow. */
9619 if (EXPR_P (min))
9620 TREE_NO_WARNING (min) = 1;
9624 /* Now refine the minimum and maximum values using any
9625 value range information we have for op0. */
9626 if (min && max)
9628 if (compare_values (vr->min, min) == 1)
9629 min = vr->min;
9630 if (compare_values (vr->max, max) == -1)
9631 max = vr->max;
9633 /* If the new min/max values have converged to a single value,
9634 then there is only one value which can satisfy the condition,
9635 return that value. */
9636 if (operand_equal_p (min, max, 0) && is_gimple_min_invariant (min))
9637 return min;
9639 return NULL;
9642 /* Return whether the value range *VR fits in an integer type specified
9643 by PRECISION and UNSIGNED_P. */
9645 static bool
9646 range_fits_type_p (value_range *vr, unsigned dest_precision, signop dest_sgn)
9648 tree src_type;
9649 unsigned src_precision;
9650 widest_int tem;
9651 signop src_sgn;
9653 /* We can only handle integral and pointer types. */
9654 src_type = TREE_TYPE (vr->min);
9655 if (!INTEGRAL_TYPE_P (src_type)
9656 && !POINTER_TYPE_P (src_type))
9657 return false;
9659 /* An extension is fine unless VR is SIGNED and dest_sgn is UNSIGNED,
9660 and so is an identity transform. */
9661 src_precision = TYPE_PRECISION (TREE_TYPE (vr->min));
9662 src_sgn = TYPE_SIGN (src_type);
9663 if ((src_precision < dest_precision
9664 && !(dest_sgn == UNSIGNED && src_sgn == SIGNED))
9665 || (src_precision == dest_precision && src_sgn == dest_sgn))
9666 return true;
9668 /* Now we can only handle ranges with constant bounds. */
9669 if (vr->type != VR_RANGE
9670 || TREE_CODE (vr->min) != INTEGER_CST
9671 || TREE_CODE (vr->max) != INTEGER_CST)
9672 return false;
9674 /* For sign changes, the MSB of the wide_int has to be clear.
9675 An unsigned value with its MSB set cannot be represented by
9676 a signed wide_int, while a negative value cannot be represented
9677 by an unsigned wide_int. */
9678 if (src_sgn != dest_sgn
9679 && (wi::lts_p (vr->min, 0) || wi::lts_p (vr->max, 0)))
9680 return false;
9682 /* Then we can perform the conversion on both ends and compare
9683 the result for equality. */
9684 tem = wi::ext (wi::to_widest (vr->min), dest_precision, dest_sgn);
9685 if (tem != wi::to_widest (vr->min))
9686 return false;
9687 tem = wi::ext (wi::to_widest (vr->max), dest_precision, dest_sgn);
9688 if (tem != wi::to_widest (vr->max))
9689 return false;
9691 return true;
9694 /* Simplify a conditional using a relational operator to an equality
9695 test if the range information indicates only one value can satisfy
9696 the original conditional. */
9698 static bool
9699 simplify_cond_using_ranges_1 (gcond *stmt)
9701 tree op0 = gimple_cond_lhs (stmt);
9702 tree op1 = gimple_cond_rhs (stmt);
9703 enum tree_code cond_code = gimple_cond_code (stmt);
9705 if (cond_code != NE_EXPR
9706 && cond_code != EQ_EXPR
9707 && TREE_CODE (op0) == SSA_NAME
9708 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
9709 && is_gimple_min_invariant (op1))
9711 value_range *vr = get_value_range (op0);
9713 /* If we have range information for OP0, then we might be
9714 able to simplify this conditional. */
9715 if (vr->type == VR_RANGE)
9717 tree new_tree = test_for_singularity (cond_code, op0, op1, vr);
9718 if (new_tree)
9720 if (dump_file)
9722 fprintf (dump_file, "Simplified relational ");
9723 print_gimple_stmt (dump_file, stmt, 0);
9724 fprintf (dump_file, " into ");
9727 gimple_cond_set_code (stmt, EQ_EXPR);
9728 gimple_cond_set_lhs (stmt, op0);
9729 gimple_cond_set_rhs (stmt, new_tree);
9731 update_stmt (stmt);
9733 if (dump_file)
9735 print_gimple_stmt (dump_file, stmt, 0);
9736 fprintf (dump_file, "\n");
9739 return true;
9742 /* Try again after inverting the condition. We only deal
9743 with integral types here, so no need to worry about
9744 issues with inverting FP comparisons. */
9745 new_tree = test_for_singularity
9746 (invert_tree_comparison (cond_code, false),
9747 op0, op1, vr);
9748 if (new_tree)
9750 if (dump_file)
9752 fprintf (dump_file, "Simplified relational ");
9753 print_gimple_stmt (dump_file, stmt, 0);
9754 fprintf (dump_file, " into ");
9757 gimple_cond_set_code (stmt, NE_EXPR);
9758 gimple_cond_set_lhs (stmt, op0);
9759 gimple_cond_set_rhs (stmt, new_tree);
9761 update_stmt (stmt);
9763 if (dump_file)
9765 print_gimple_stmt (dump_file, stmt, 0);
9766 fprintf (dump_file, "\n");
9769 return true;
9773 return false;
9776 /* STMT is a conditional at the end of a basic block.
9778 If the conditional is of the form SSA_NAME op constant and the SSA_NAME
9779 was set via a type conversion, try to replace the SSA_NAME with the RHS
9780 of the type conversion. Doing so makes the conversion dead which helps
9781 subsequent passes. */
9783 static void
9784 simplify_cond_using_ranges_2 (gcond *stmt)
9786 tree op0 = gimple_cond_lhs (stmt);
9787 tree op1 = gimple_cond_rhs (stmt);
9789 /* If we have a comparison of an SSA_NAME (OP0) against a constant,
9790 see if OP0 was set by a type conversion where the source of
9791 the conversion is another SSA_NAME with a range that fits
9792 into the range of OP0's type.
9794 If so, the conversion is redundant as the earlier SSA_NAME can be
9795 used for the comparison directly if we just massage the constant in the
9796 comparison. */
9797 if (TREE_CODE (op0) == SSA_NAME
9798 && TREE_CODE (op1) == INTEGER_CST)
9800 gimple *def_stmt = SSA_NAME_DEF_STMT (op0);
9801 tree innerop;
9803 if (!is_gimple_assign (def_stmt)
9804 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt)))
9805 return;
9807 innerop = gimple_assign_rhs1 (def_stmt);
9809 if (TREE_CODE (innerop) == SSA_NAME
9810 && !POINTER_TYPE_P (TREE_TYPE (innerop))
9811 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (innerop)
9812 && desired_pro_or_demotion_p (TREE_TYPE (innerop), TREE_TYPE (op0)))
9814 value_range *vr = get_value_range (innerop);
9816 if (range_int_cst_p (vr)
9817 && range_fits_type_p (vr,
9818 TYPE_PRECISION (TREE_TYPE (op0)),
9819 TYPE_SIGN (TREE_TYPE (op0)))
9820 && int_fits_type_p (op1, TREE_TYPE (innerop)))
9822 tree newconst = fold_convert (TREE_TYPE (innerop), op1);
9823 gimple_cond_set_lhs (stmt, innerop);
9824 gimple_cond_set_rhs (stmt, newconst);
9825 update_stmt (stmt);
9826 if (dump_file && (dump_flags & TDF_DETAILS))
9828 fprintf (dump_file, "Folded into: ");
9829 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
9830 fprintf (dump_file, "\n");
9837 /* Simplify a switch statement using the value range of the switch
9838 argument. */
9840 static bool
9841 simplify_switch_using_ranges (gswitch *stmt)
9843 tree op = gimple_switch_index (stmt);
9844 value_range *vr = NULL;
9845 bool take_default;
9846 edge e;
9847 edge_iterator ei;
9848 size_t i = 0, j = 0, n, n2;
9849 tree vec2;
9850 switch_update su;
9851 size_t k = 1, l = 0;
9853 if (TREE_CODE (op) == SSA_NAME)
9855 vr = get_value_range (op);
9857 /* We can only handle integer ranges. */
9858 if ((vr->type != VR_RANGE
9859 && vr->type != VR_ANTI_RANGE)
9860 || symbolic_range_p (vr))
9861 return false;
9863 /* Find case label for min/max of the value range. */
9864 take_default = !find_case_label_ranges (stmt, vr, &i, &j, &k, &l);
9866 else if (TREE_CODE (op) == INTEGER_CST)
9868 take_default = !find_case_label_index (stmt, 1, op, &i);
9869 if (take_default)
9871 i = 1;
9872 j = 0;
9874 else
9876 j = i;
9879 else
9880 return false;
9882 n = gimple_switch_num_labels (stmt);
9884 /* We can truncate the case label ranges that partially overlap with OP's
9885 value range. */
9886 size_t min_idx = 1, max_idx = 0;
9887 if (vr != NULL)
9888 find_case_label_range (stmt, vr->min, vr->max, &min_idx, &max_idx);
9889 if (min_idx <= max_idx)
9891 tree min_label = gimple_switch_label (stmt, min_idx);
9892 tree max_label = gimple_switch_label (stmt, max_idx);
9894 /* Avoid changing the type of the case labels when truncating. */
9895 tree case_label_type = TREE_TYPE (CASE_LOW (min_label));
9896 tree vr_min = fold_convert (case_label_type, vr->min);
9897 tree vr_max = fold_convert (case_label_type, vr->max);
9899 if (vr->type == VR_RANGE)
9901 /* If OP's value range is [2,8] and the low label range is
9902 0 ... 3, truncate the label's range to 2 .. 3. */
9903 if (tree_int_cst_compare (CASE_LOW (min_label), vr_min) < 0
9904 && CASE_HIGH (min_label) != NULL_TREE
9905 && tree_int_cst_compare (CASE_HIGH (min_label), vr_min) >= 0)
9906 CASE_LOW (min_label) = vr_min;
9908 /* If OP's value range is [2,8] and the high label range is
9909 7 ... 10, truncate the label's range to 7 .. 8. */
9910 if (tree_int_cst_compare (CASE_LOW (max_label), vr_max) <= 0
9911 && CASE_HIGH (max_label) != NULL_TREE
9912 && tree_int_cst_compare (CASE_HIGH (max_label), vr_max) > 0)
9913 CASE_HIGH (max_label) = vr_max;
9915 else if (vr->type == VR_ANTI_RANGE)
9917 tree one_cst = build_one_cst (case_label_type);
9919 if (min_label == max_label)
9921 /* If OP's value range is ~[7,8] and the label's range is
9922 7 ... 10, truncate the label's range to 9 ... 10. */
9923 if (tree_int_cst_compare (CASE_LOW (min_label), vr_min) == 0
9924 && CASE_HIGH (min_label) != NULL_TREE
9925 && tree_int_cst_compare (CASE_HIGH (min_label), vr_max) > 0)
9926 CASE_LOW (min_label)
9927 = int_const_binop (PLUS_EXPR, vr_max, one_cst);
9929 /* If OP's value range is ~[7,8] and the label's range is
9930 5 ... 8, truncate the label's range to 5 ... 6. */
9931 if (tree_int_cst_compare (CASE_LOW (min_label), vr_min) < 0
9932 && CASE_HIGH (min_label) != NULL_TREE
9933 && tree_int_cst_compare (CASE_HIGH (min_label), vr_max) == 0)
9934 CASE_HIGH (min_label)
9935 = int_const_binop (MINUS_EXPR, vr_min, one_cst);
9937 else
9939 /* If OP's value range is ~[2,8] and the low label range is
9940 0 ... 3, truncate the label's range to 0 ... 1. */
9941 if (tree_int_cst_compare (CASE_LOW (min_label), vr_min) < 0
9942 && CASE_HIGH (min_label) != NULL_TREE
9943 && tree_int_cst_compare (CASE_HIGH (min_label), vr_min) >= 0)
9944 CASE_HIGH (min_label)
9945 = int_const_binop (MINUS_EXPR, vr_min, one_cst);
9947 /* If OP's value range is ~[2,8] and the high label range is
9948 7 ... 10, truncate the label's range to 9 ... 10. */
9949 if (tree_int_cst_compare (CASE_LOW (max_label), vr_max) <= 0
9950 && CASE_HIGH (max_label) != NULL_TREE
9951 && tree_int_cst_compare (CASE_HIGH (max_label), vr_max) > 0)
9952 CASE_LOW (max_label)
9953 = int_const_binop (PLUS_EXPR, vr_max, one_cst);
9957 /* Canonicalize singleton case ranges. */
9958 if (tree_int_cst_equal (CASE_LOW (min_label), CASE_HIGH (min_label)))
9959 CASE_HIGH (min_label) = NULL_TREE;
9960 if (tree_int_cst_equal (CASE_LOW (max_label), CASE_HIGH (max_label)))
9961 CASE_HIGH (max_label) = NULL_TREE;
9964 /* We can also eliminate case labels that lie completely outside OP's value
9965 range. */
9967 /* Bail out if this is just all edges taken. */
9968 if (i == 1
9969 && j == n - 1
9970 && take_default)
9971 return false;
9973 /* Build a new vector of taken case labels. */
9974 vec2 = make_tree_vec (j - i + 1 + l - k + 1 + (int)take_default);
9975 n2 = 0;
9977 /* Add the default edge, if necessary. */
9978 if (take_default)
9979 TREE_VEC_ELT (vec2, n2++) = gimple_switch_default_label (stmt);
9981 for (; i <= j; ++i, ++n2)
9982 TREE_VEC_ELT (vec2, n2) = gimple_switch_label (stmt, i);
9984 for (; k <= l; ++k, ++n2)
9985 TREE_VEC_ELT (vec2, n2) = gimple_switch_label (stmt, k);
9987 /* Mark needed edges. */
9988 for (i = 0; i < n2; ++i)
9990 e = find_edge (gimple_bb (stmt),
9991 label_to_block (CASE_LABEL (TREE_VEC_ELT (vec2, i))));
9992 e->aux = (void *)-1;
9995 /* Queue not needed edges for later removal. */
9996 FOR_EACH_EDGE (e, ei, gimple_bb (stmt)->succs)
9998 if (e->aux == (void *)-1)
10000 e->aux = NULL;
10001 continue;
10004 if (dump_file && (dump_flags & TDF_DETAILS))
10006 fprintf (dump_file, "removing unreachable case label\n");
10008 to_remove_edges.safe_push (e);
10009 e->flags &= ~EDGE_EXECUTABLE;
10012 /* And queue an update for the stmt. */
10013 su.stmt = stmt;
10014 su.vec = vec2;
10015 to_update_switch_stmts.safe_push (su);
10016 return false;
10019 /* Simplify an integral conversion from an SSA name in STMT. */
10021 static bool
10022 simplify_conversion_using_ranges (gimple_stmt_iterator *gsi, gimple *stmt)
10024 tree innerop, middleop, finaltype;
10025 gimple *def_stmt;
10026 signop inner_sgn, middle_sgn, final_sgn;
10027 unsigned inner_prec, middle_prec, final_prec;
10028 widest_int innermin, innermed, innermax, middlemin, middlemed, middlemax;
10030 finaltype = TREE_TYPE (gimple_assign_lhs (stmt));
10031 if (!INTEGRAL_TYPE_P (finaltype))
10032 return false;
10033 middleop = gimple_assign_rhs1 (stmt);
10034 def_stmt = SSA_NAME_DEF_STMT (middleop);
10035 if (!is_gimple_assign (def_stmt)
10036 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt)))
10037 return false;
10038 innerop = gimple_assign_rhs1 (def_stmt);
10039 if (TREE_CODE (innerop) != SSA_NAME
10040 || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (innerop))
10041 return false;
10043 /* Get the value-range of the inner operand. Use get_range_info in
10044 case innerop was created during substitute-and-fold. */
10045 wide_int imin, imax;
10046 if (!INTEGRAL_TYPE_P (TREE_TYPE (innerop))
10047 || get_range_info (innerop, &imin, &imax) != VR_RANGE)
10048 return false;
10049 innermin = widest_int::from (imin, TYPE_SIGN (TREE_TYPE (innerop)));
10050 innermax = widest_int::from (imax, TYPE_SIGN (TREE_TYPE (innerop)));
10052 /* Simulate the conversion chain to check if the result is equal if
10053 the middle conversion is removed. */
10054 inner_prec = TYPE_PRECISION (TREE_TYPE (innerop));
10055 middle_prec = TYPE_PRECISION (TREE_TYPE (middleop));
10056 final_prec = TYPE_PRECISION (finaltype);
10058 /* If the first conversion is not injective, the second must not
10059 be widening. */
10060 if (wi::gtu_p (innermax - innermin,
10061 wi::mask <widest_int> (middle_prec, false))
10062 && middle_prec < final_prec)
10063 return false;
10064 /* We also want a medium value so that we can track the effect that
10065 narrowing conversions with sign change have. */
10066 inner_sgn = TYPE_SIGN (TREE_TYPE (innerop));
10067 if (inner_sgn == UNSIGNED)
10068 innermed = wi::shifted_mask <widest_int> (1, inner_prec - 1, false);
10069 else
10070 innermed = 0;
10071 if (wi::cmp (innermin, innermed, inner_sgn) >= 0
10072 || wi::cmp (innermed, innermax, inner_sgn) >= 0)
10073 innermed = innermin;
10075 middle_sgn = TYPE_SIGN (TREE_TYPE (middleop));
10076 middlemin = wi::ext (innermin, middle_prec, middle_sgn);
10077 middlemed = wi::ext (innermed, middle_prec, middle_sgn);
10078 middlemax = wi::ext (innermax, middle_prec, middle_sgn);
10080 /* Require that the final conversion applied to both the original
10081 and the intermediate range produces the same result. */
10082 final_sgn = TYPE_SIGN (finaltype);
10083 if (wi::ext (middlemin, final_prec, final_sgn)
10084 != wi::ext (innermin, final_prec, final_sgn)
10085 || wi::ext (middlemed, final_prec, final_sgn)
10086 != wi::ext (innermed, final_prec, final_sgn)
10087 || wi::ext (middlemax, final_prec, final_sgn)
10088 != wi::ext (innermax, final_prec, final_sgn))
10089 return false;
10091 gimple_assign_set_rhs1 (stmt, innerop);
10092 fold_stmt (gsi, follow_single_use_edges);
10093 return true;
10096 /* Simplify a conversion from integral SSA name to float in STMT. */
10098 static bool
10099 simplify_float_conversion_using_ranges (gimple_stmt_iterator *gsi,
10100 gimple *stmt)
10102 tree rhs1 = gimple_assign_rhs1 (stmt);
10103 value_range *vr = get_value_range (rhs1);
10104 scalar_float_mode fltmode
10105 = SCALAR_FLOAT_TYPE_MODE (TREE_TYPE (gimple_assign_lhs (stmt)));
10106 scalar_int_mode mode;
10107 tree tem;
10108 gassign *conv;
10110 /* We can only handle constant ranges. */
10111 if (vr->type != VR_RANGE
10112 || TREE_CODE (vr->min) != INTEGER_CST
10113 || TREE_CODE (vr->max) != INTEGER_CST)
10114 return false;
10116 /* First check if we can use a signed type in place of an unsigned. */
10117 scalar_int_mode rhs_mode = SCALAR_INT_TYPE_MODE (TREE_TYPE (rhs1));
10118 if (TYPE_UNSIGNED (TREE_TYPE (rhs1))
10119 && can_float_p (fltmode, rhs_mode, 0) != CODE_FOR_nothing
10120 && range_fits_type_p (vr, TYPE_PRECISION (TREE_TYPE (rhs1)), SIGNED))
10121 mode = rhs_mode;
10122 /* If we can do the conversion in the current input mode do nothing. */
10123 else if (can_float_p (fltmode, rhs_mode,
10124 TYPE_UNSIGNED (TREE_TYPE (rhs1))) != CODE_FOR_nothing)
10125 return false;
10126 /* Otherwise search for a mode we can use, starting from the narrowest
10127 integer mode available. */
10128 else
10130 mode = NARROWEST_INT_MODE;
10131 for (;;)
10133 /* If we cannot do a signed conversion to float from mode
10134 or if the value-range does not fit in the signed type
10135 try with a wider mode. */
10136 if (can_float_p (fltmode, mode, 0) != CODE_FOR_nothing
10137 && range_fits_type_p (vr, GET_MODE_PRECISION (mode), SIGNED))
10138 break;
10140 /* But do not widen the input. Instead leave that to the
10141 optabs expansion code. */
10142 if (!GET_MODE_WIDER_MODE (mode).exists (&mode)
10143 || GET_MODE_PRECISION (mode) > TYPE_PRECISION (TREE_TYPE (rhs1)))
10144 return false;
10148 /* It works, insert a truncation or sign-change before the
10149 float conversion. */
10150 tem = make_ssa_name (build_nonstandard_integer_type
10151 (GET_MODE_PRECISION (mode), 0));
10152 conv = gimple_build_assign (tem, NOP_EXPR, rhs1);
10153 gsi_insert_before (gsi, conv, GSI_SAME_STMT);
10154 gimple_assign_set_rhs1 (stmt, tem);
10155 fold_stmt (gsi, follow_single_use_edges);
10157 return true;
10160 /* Simplify an internal fn call using ranges if possible. */
10162 static bool
10163 simplify_internal_call_using_ranges (gimple_stmt_iterator *gsi, gimple *stmt)
10165 enum tree_code subcode;
10166 bool is_ubsan = false;
10167 bool ovf = false;
10168 switch (gimple_call_internal_fn (stmt))
10170 case IFN_UBSAN_CHECK_ADD:
10171 subcode = PLUS_EXPR;
10172 is_ubsan = true;
10173 break;
10174 case IFN_UBSAN_CHECK_SUB:
10175 subcode = MINUS_EXPR;
10176 is_ubsan = true;
10177 break;
10178 case IFN_UBSAN_CHECK_MUL:
10179 subcode = MULT_EXPR;
10180 is_ubsan = true;
10181 break;
10182 case IFN_ADD_OVERFLOW:
10183 subcode = PLUS_EXPR;
10184 break;
10185 case IFN_SUB_OVERFLOW:
10186 subcode = MINUS_EXPR;
10187 break;
10188 case IFN_MUL_OVERFLOW:
10189 subcode = MULT_EXPR;
10190 break;
10191 default:
10192 return false;
10195 tree op0 = gimple_call_arg (stmt, 0);
10196 tree op1 = gimple_call_arg (stmt, 1);
10197 tree type;
10198 if (is_ubsan)
10200 type = TREE_TYPE (op0);
10201 if (VECTOR_TYPE_P (type))
10202 return false;
10204 else if (gimple_call_lhs (stmt) == NULL_TREE)
10205 return false;
10206 else
10207 type = TREE_TYPE (TREE_TYPE (gimple_call_lhs (stmt)));
10208 if (!check_for_binary_op_overflow (subcode, type, op0, op1, &ovf)
10209 || (is_ubsan && ovf))
10210 return false;
10212 gimple *g;
10213 location_t loc = gimple_location (stmt);
10214 if (is_ubsan)
10215 g = gimple_build_assign (gimple_call_lhs (stmt), subcode, op0, op1);
10216 else
10218 int prec = TYPE_PRECISION (type);
10219 tree utype = type;
10220 if (ovf
10221 || !useless_type_conversion_p (type, TREE_TYPE (op0))
10222 || !useless_type_conversion_p (type, TREE_TYPE (op1)))
10223 utype = build_nonstandard_integer_type (prec, 1);
10224 if (TREE_CODE (op0) == INTEGER_CST)
10225 op0 = fold_convert (utype, op0);
10226 else if (!useless_type_conversion_p (utype, TREE_TYPE (op0)))
10228 g = gimple_build_assign (make_ssa_name (utype), NOP_EXPR, op0);
10229 gimple_set_location (g, loc);
10230 gsi_insert_before (gsi, g, GSI_SAME_STMT);
10231 op0 = gimple_assign_lhs (g);
10233 if (TREE_CODE (op1) == INTEGER_CST)
10234 op1 = fold_convert (utype, op1);
10235 else if (!useless_type_conversion_p (utype, TREE_TYPE (op1)))
10237 g = gimple_build_assign (make_ssa_name (utype), NOP_EXPR, op1);
10238 gimple_set_location (g, loc);
10239 gsi_insert_before (gsi, g, GSI_SAME_STMT);
10240 op1 = gimple_assign_lhs (g);
10242 g = gimple_build_assign (make_ssa_name (utype), subcode, op0, op1);
10243 gimple_set_location (g, loc);
10244 gsi_insert_before (gsi, g, GSI_SAME_STMT);
10245 if (utype != type)
10247 g = gimple_build_assign (make_ssa_name (type), NOP_EXPR,
10248 gimple_assign_lhs (g));
10249 gimple_set_location (g, loc);
10250 gsi_insert_before (gsi, g, GSI_SAME_STMT);
10252 g = gimple_build_assign (gimple_call_lhs (stmt), COMPLEX_EXPR,
10253 gimple_assign_lhs (g),
10254 build_int_cst (type, ovf));
10256 gimple_set_location (g, loc);
10257 gsi_replace (gsi, g, false);
10258 return true;
10261 /* Return true if VAR is a two-valued variable. Set a and b with the
10262 two-values when it is true. Return false otherwise. */
10264 static bool
10265 two_valued_val_range_p (tree var, tree *a, tree *b)
10267 value_range *vr = get_value_range (var);
10268 if ((vr->type != VR_RANGE
10269 && vr->type != VR_ANTI_RANGE)
10270 || TREE_CODE (vr->min) != INTEGER_CST
10271 || TREE_CODE (vr->max) != INTEGER_CST)
10272 return false;
10274 if (vr->type == VR_RANGE
10275 && wi::sub (vr->max, vr->min) == 1)
10277 *a = vr->min;
10278 *b = vr->max;
10279 return true;
10282 /* ~[TYPE_MIN + 1, TYPE_MAX - 1] */
10283 if (vr->type == VR_ANTI_RANGE
10284 && wi::sub (vr->min, vrp_val_min (TREE_TYPE (var))) == 1
10285 && wi::sub (vrp_val_max (TREE_TYPE (var)), vr->max) == 1)
10287 *a = vrp_val_min (TREE_TYPE (var));
10288 *b = vrp_val_max (TREE_TYPE (var));
10289 return true;
10292 return false;
10295 /* Simplify STMT using ranges if possible. */
10297 static bool
10298 simplify_stmt_using_ranges (gimple_stmt_iterator *gsi)
10300 gimple *stmt = gsi_stmt (*gsi);
10301 if (is_gimple_assign (stmt))
10303 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
10304 tree rhs1 = gimple_assign_rhs1 (stmt);
10305 tree rhs2 = gimple_assign_rhs2 (stmt);
10306 tree lhs = gimple_assign_lhs (stmt);
10307 tree val1 = NULL_TREE, val2 = NULL_TREE;
10308 use_operand_p use_p;
10309 gimple *use_stmt;
10311 /* Convert:
10312 LHS = CST BINOP VAR
10313 Where VAR is two-valued and LHS is used in GIMPLE_COND only
10315 LHS = VAR == VAL1 ? (CST BINOP VAL1) : (CST BINOP VAL2)
10317 Also handles:
10318 LHS = VAR BINOP CST
10319 Where VAR is two-valued and LHS is used in GIMPLE_COND only
10321 LHS = VAR == VAL1 ? (VAL1 BINOP CST) : (VAL2 BINOP CST) */
10323 if (TREE_CODE_CLASS (rhs_code) == tcc_binary
10324 && INTEGRAL_TYPE_P (TREE_TYPE (lhs))
10325 && ((TREE_CODE (rhs1) == INTEGER_CST
10326 && TREE_CODE (rhs2) == SSA_NAME)
10327 || (TREE_CODE (rhs2) == INTEGER_CST
10328 && TREE_CODE (rhs1) == SSA_NAME))
10329 && single_imm_use (lhs, &use_p, &use_stmt)
10330 && gimple_code (use_stmt) == GIMPLE_COND)
10333 tree new_rhs1 = NULL_TREE;
10334 tree new_rhs2 = NULL_TREE;
10335 tree cmp_var = NULL_TREE;
10337 if (TREE_CODE (rhs2) == SSA_NAME
10338 && two_valued_val_range_p (rhs2, &val1, &val2))
10340 /* Optimize RHS1 OP [VAL1, VAL2]. */
10341 new_rhs1 = int_const_binop (rhs_code, rhs1, val1);
10342 new_rhs2 = int_const_binop (rhs_code, rhs1, val2);
10343 cmp_var = rhs2;
10345 else if (TREE_CODE (rhs1) == SSA_NAME
10346 && two_valued_val_range_p (rhs1, &val1, &val2))
10348 /* Optimize [VAL1, VAL2] OP RHS2. */
10349 new_rhs1 = int_const_binop (rhs_code, val1, rhs2);
10350 new_rhs2 = int_const_binop (rhs_code, val2, rhs2);
10351 cmp_var = rhs1;
10354 /* If we could not find two-vals or the optimzation is invalid as
10355 in divide by zero, new_rhs1 / new_rhs will be NULL_TREE. */
10356 if (new_rhs1 && new_rhs2)
10358 tree cond = build2 (EQ_EXPR, boolean_type_node, cmp_var, val1);
10359 gimple_assign_set_rhs_with_ops (gsi,
10360 COND_EXPR, cond,
10361 new_rhs1,
10362 new_rhs2);
10363 update_stmt (gsi_stmt (*gsi));
10364 fold_stmt (gsi, follow_single_use_edges);
10365 return true;
10369 switch (rhs_code)
10371 case EQ_EXPR:
10372 case NE_EXPR:
10373 /* Transform EQ_EXPR, NE_EXPR into BIT_XOR_EXPR or identity
10374 if the RHS is zero or one, and the LHS are known to be boolean
10375 values. */
10376 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
10377 return simplify_truth_ops_using_ranges (gsi, stmt);
10378 break;
10380 /* Transform TRUNC_DIV_EXPR and TRUNC_MOD_EXPR into RSHIFT_EXPR
10381 and BIT_AND_EXPR respectively if the first operand is greater
10382 than zero and the second operand is an exact power of two.
10383 Also optimize TRUNC_MOD_EXPR away if the second operand is
10384 constant and the first operand already has the right value
10385 range. */
10386 case TRUNC_DIV_EXPR:
10387 case TRUNC_MOD_EXPR:
10388 if ((TREE_CODE (rhs1) == SSA_NAME
10389 || TREE_CODE (rhs1) == INTEGER_CST)
10390 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
10391 return simplify_div_or_mod_using_ranges (gsi, stmt);
10392 break;
10394 /* Transform ABS (X) into X or -X as appropriate. */
10395 case ABS_EXPR:
10396 if (TREE_CODE (rhs1) == SSA_NAME
10397 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
10398 return simplify_abs_using_ranges (gsi, stmt);
10399 break;
10401 case BIT_AND_EXPR:
10402 case BIT_IOR_EXPR:
10403 /* Optimize away BIT_AND_EXPR and BIT_IOR_EXPR
10404 if all the bits being cleared are already cleared or
10405 all the bits being set are already set. */
10406 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
10407 return simplify_bit_ops_using_ranges (gsi, stmt);
10408 break;
10410 CASE_CONVERT:
10411 if (TREE_CODE (rhs1) == SSA_NAME
10412 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
10413 return simplify_conversion_using_ranges (gsi, stmt);
10414 break;
10416 case FLOAT_EXPR:
10417 if (TREE_CODE (rhs1) == SSA_NAME
10418 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
10419 return simplify_float_conversion_using_ranges (gsi, stmt);
10420 break;
10422 case MIN_EXPR:
10423 case MAX_EXPR:
10424 return simplify_min_or_max_using_ranges (gsi, stmt);
10426 default:
10427 break;
10430 else if (gimple_code (stmt) == GIMPLE_COND)
10431 return simplify_cond_using_ranges_1 (as_a <gcond *> (stmt));
10432 else if (gimple_code (stmt) == GIMPLE_SWITCH)
10433 return simplify_switch_using_ranges (as_a <gswitch *> (stmt));
10434 else if (is_gimple_call (stmt)
10435 && gimple_call_internal_p (stmt))
10436 return simplify_internal_call_using_ranges (gsi, stmt);
10438 return false;
10441 /* If the statement pointed by SI has a predicate whose value can be
10442 computed using the value range information computed by VRP, compute
10443 its value and return true. Otherwise, return false. */
10445 static bool
10446 fold_predicate_in (gimple_stmt_iterator *si)
10448 bool assignment_p = false;
10449 tree val;
10450 gimple *stmt = gsi_stmt (*si);
10452 if (is_gimple_assign (stmt)
10453 && TREE_CODE_CLASS (gimple_assign_rhs_code (stmt)) == tcc_comparison)
10455 assignment_p = true;
10456 val = vrp_evaluate_conditional (gimple_assign_rhs_code (stmt),
10457 gimple_assign_rhs1 (stmt),
10458 gimple_assign_rhs2 (stmt),
10459 stmt);
10461 else if (gcond *cond_stmt = dyn_cast <gcond *> (stmt))
10462 val = vrp_evaluate_conditional (gimple_cond_code (cond_stmt),
10463 gimple_cond_lhs (cond_stmt),
10464 gimple_cond_rhs (cond_stmt),
10465 stmt);
10466 else
10467 return false;
10469 if (val)
10471 if (assignment_p)
10472 val = fold_convert (gimple_expr_type (stmt), val);
10474 if (dump_file)
10476 fprintf (dump_file, "Folding predicate ");
10477 print_gimple_expr (dump_file, stmt, 0);
10478 fprintf (dump_file, " to ");
10479 print_generic_expr (dump_file, val);
10480 fprintf (dump_file, "\n");
10483 if (is_gimple_assign (stmt))
10484 gimple_assign_set_rhs_from_tree (si, val);
10485 else
10487 gcc_assert (gimple_code (stmt) == GIMPLE_COND);
10488 gcond *cond_stmt = as_a <gcond *> (stmt);
10489 if (integer_zerop (val))
10490 gimple_cond_make_false (cond_stmt);
10491 else if (integer_onep (val))
10492 gimple_cond_make_true (cond_stmt);
10493 else
10494 gcc_unreachable ();
10497 return true;
10500 return false;
10503 /* Callback for substitute_and_fold folding the stmt at *SI. */
10505 static bool
10506 vrp_fold_stmt (gimple_stmt_iterator *si)
10508 if (fold_predicate_in (si))
10509 return true;
10511 return simplify_stmt_using_ranges (si);
10514 /* Return the LHS of any ASSERT_EXPR where OP appears as the first
10515 argument to the ASSERT_EXPR and in which the ASSERT_EXPR dominates
10516 BB. If no such ASSERT_EXPR is found, return OP. */
10518 static tree
10519 lhs_of_dominating_assert (tree op, basic_block bb, gimple *stmt)
10521 imm_use_iterator imm_iter;
10522 gimple *use_stmt;
10523 use_operand_p use_p;
10525 if (TREE_CODE (op) == SSA_NAME)
10527 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, op)
10529 use_stmt = USE_STMT (use_p);
10530 if (use_stmt != stmt
10531 && gimple_assign_single_p (use_stmt)
10532 && TREE_CODE (gimple_assign_rhs1 (use_stmt)) == ASSERT_EXPR
10533 && TREE_OPERAND (gimple_assign_rhs1 (use_stmt), 0) == op
10534 && dominated_by_p (CDI_DOMINATORS, bb, gimple_bb (use_stmt)))
10535 return gimple_assign_lhs (use_stmt);
10538 return op;
10541 /* A trivial wrapper so that we can present the generic jump threading
10542 code with a simple API for simplifying statements. STMT is the
10543 statement we want to simplify, WITHIN_STMT provides the location
10544 for any overflow warnings. */
10546 static tree
10547 simplify_stmt_for_jump_threading (gimple *stmt, gimple *within_stmt,
10548 class avail_exprs_stack *avail_exprs_stack ATTRIBUTE_UNUSED,
10549 basic_block bb)
10551 /* First see if the conditional is in the hash table. */
10552 tree cached_lhs = avail_exprs_stack->lookup_avail_expr (stmt, false, true);
10553 if (cached_lhs && is_gimple_min_invariant (cached_lhs))
10554 return cached_lhs;
10556 if (gcond *cond_stmt = dyn_cast <gcond *> (stmt))
10558 tree op0 = gimple_cond_lhs (cond_stmt);
10559 op0 = lhs_of_dominating_assert (op0, bb, stmt);
10561 tree op1 = gimple_cond_rhs (cond_stmt);
10562 op1 = lhs_of_dominating_assert (op1, bb, stmt);
10564 return vrp_evaluate_conditional (gimple_cond_code (cond_stmt),
10565 op0, op1, within_stmt);
10568 /* We simplify a switch statement by trying to determine which case label
10569 will be taken. If we are successful then we return the corresponding
10570 CASE_LABEL_EXPR. */
10571 if (gswitch *switch_stmt = dyn_cast <gswitch *> (stmt))
10573 tree op = gimple_switch_index (switch_stmt);
10574 if (TREE_CODE (op) != SSA_NAME)
10575 return NULL_TREE;
10577 op = lhs_of_dominating_assert (op, bb, stmt);
10579 value_range *vr = get_value_range (op);
10580 if ((vr->type != VR_RANGE && vr->type != VR_ANTI_RANGE)
10581 || symbolic_range_p (vr))
10582 return NULL_TREE;
10584 if (vr->type == VR_RANGE)
10586 size_t i, j;
10587 /* Get the range of labels that contain a part of the operand's
10588 value range. */
10589 find_case_label_range (switch_stmt, vr->min, vr->max, &i, &j);
10591 /* Is there only one such label? */
10592 if (i == j)
10594 tree label = gimple_switch_label (switch_stmt, i);
10596 /* The i'th label will be taken only if the value range of the
10597 operand is entirely within the bounds of this label. */
10598 if (CASE_HIGH (label) != NULL_TREE
10599 ? (tree_int_cst_compare (CASE_LOW (label), vr->min) <= 0
10600 && tree_int_cst_compare (CASE_HIGH (label), vr->max) >= 0)
10601 : (tree_int_cst_equal (CASE_LOW (label), vr->min)
10602 && tree_int_cst_equal (vr->min, vr->max)))
10603 return label;
10606 /* If there are no such labels then the default label will be
10607 taken. */
10608 if (i > j)
10609 return gimple_switch_label (switch_stmt, 0);
10612 if (vr->type == VR_ANTI_RANGE)
10614 unsigned n = gimple_switch_num_labels (switch_stmt);
10615 tree min_label = gimple_switch_label (switch_stmt, 1);
10616 tree max_label = gimple_switch_label (switch_stmt, n - 1);
10618 /* The default label will be taken only if the anti-range of the
10619 operand is entirely outside the bounds of all the (non-default)
10620 case labels. */
10621 if (tree_int_cst_compare (vr->min, CASE_LOW (min_label)) <= 0
10622 && (CASE_HIGH (max_label) != NULL_TREE
10623 ? tree_int_cst_compare (vr->max, CASE_HIGH (max_label)) >= 0
10624 : tree_int_cst_compare (vr->max, CASE_LOW (max_label)) >= 0))
10625 return gimple_switch_label (switch_stmt, 0);
10628 return NULL_TREE;
10631 if (gassign *assign_stmt = dyn_cast <gassign *> (stmt))
10633 value_range new_vr = VR_INITIALIZER;
10634 tree lhs = gimple_assign_lhs (assign_stmt);
10636 if (TREE_CODE (lhs) == SSA_NAME
10637 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs))
10638 || POINTER_TYPE_P (TREE_TYPE (lhs))))
10640 extract_range_from_assignment (&new_vr, assign_stmt);
10641 if (range_int_cst_singleton_p (&new_vr))
10642 return new_vr.min;
10646 return NULL_TREE;
10649 class vrp_dom_walker : public dom_walker
10651 public:
10652 vrp_dom_walker (cdi_direction direction,
10653 class const_and_copies *const_and_copies,
10654 class avail_exprs_stack *avail_exprs_stack)
10655 : dom_walker (direction, true),
10656 m_const_and_copies (const_and_copies),
10657 m_avail_exprs_stack (avail_exprs_stack),
10658 m_dummy_cond (NULL) {}
10660 virtual edge before_dom_children (basic_block);
10661 virtual void after_dom_children (basic_block);
10663 private:
10664 class const_and_copies *m_const_and_copies;
10665 class avail_exprs_stack *m_avail_exprs_stack;
10667 gcond *m_dummy_cond;
10670 /* Called before processing dominator children of BB. We want to look
10671 at ASSERT_EXPRs and record information from them in the appropriate
10672 tables.
10674 We could look at other statements here. It's not seen as likely
10675 to significantly increase the jump threads we discover. */
10677 edge
10678 vrp_dom_walker::before_dom_children (basic_block bb)
10680 gimple_stmt_iterator gsi;
10682 m_avail_exprs_stack->push_marker ();
10683 m_const_and_copies->push_marker ();
10684 for (gsi = gsi_start_nondebug_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
10686 gimple *stmt = gsi_stmt (gsi);
10687 if (gimple_assign_single_p (stmt)
10688 && TREE_CODE (gimple_assign_rhs1 (stmt)) == ASSERT_EXPR)
10690 tree rhs1 = gimple_assign_rhs1 (stmt);
10691 tree cond = TREE_OPERAND (rhs1, 1);
10692 tree inverted = invert_truthvalue (cond);
10693 vec<cond_equivalence> p;
10694 p.create (3);
10695 record_conditions (&p, cond, inverted);
10696 for (unsigned int i = 0; i < p.length (); i++)
10697 m_avail_exprs_stack->record_cond (&p[i]);
10699 tree lhs = gimple_assign_lhs (stmt);
10700 m_const_and_copies->record_const_or_copy (lhs,
10701 TREE_OPERAND (rhs1, 0));
10702 p.release ();
10703 continue;
10705 break;
10707 return NULL;
10710 /* Called after processing dominator children of BB. This is where we
10711 actually call into the threader. */
10712 void
10713 vrp_dom_walker::after_dom_children (basic_block bb)
10715 if (!m_dummy_cond)
10716 m_dummy_cond = gimple_build_cond (NE_EXPR,
10717 integer_zero_node, integer_zero_node,
10718 NULL, NULL);
10720 thread_outgoing_edges (bb, m_dummy_cond, m_const_and_copies,
10721 m_avail_exprs_stack,
10722 simplify_stmt_for_jump_threading);
10724 m_avail_exprs_stack->pop_to_marker ();
10725 m_const_and_copies->pop_to_marker ();
10728 /* Blocks which have more than one predecessor and more than
10729 one successor present jump threading opportunities, i.e.,
10730 when the block is reached from a specific predecessor, we
10731 may be able to determine which of the outgoing edges will
10732 be traversed. When this optimization applies, we are able
10733 to avoid conditionals at runtime and we may expose secondary
10734 optimization opportunities.
10736 This routine is effectively a driver for the generic jump
10737 threading code. It basically just presents the generic code
10738 with edges that may be suitable for jump threading.
10740 Unlike DOM, we do not iterate VRP if jump threading was successful.
10741 While iterating may expose new opportunities for VRP, it is expected
10742 those opportunities would be very limited and the compile time cost
10743 to expose those opportunities would be significant.
10745 As jump threading opportunities are discovered, they are registered
10746 for later realization. */
10748 static void
10749 identify_jump_threads (void)
10751 int i;
10752 edge e;
10754 /* Ugh. When substituting values earlier in this pass we can
10755 wipe the dominance information. So rebuild the dominator
10756 information as we need it within the jump threading code. */
10757 calculate_dominance_info (CDI_DOMINATORS);
10759 /* We do not allow VRP information to be used for jump threading
10760 across a back edge in the CFG. Otherwise it becomes too
10761 difficult to avoid eliminating loop exit tests. Of course
10762 EDGE_DFS_BACK is not accurate at this time so we have to
10763 recompute it. */
10764 mark_dfs_back_edges ();
10766 /* Do not thread across edges we are about to remove. Just marking
10767 them as EDGE_IGNORE will do. */
10768 FOR_EACH_VEC_ELT (to_remove_edges, i, e)
10769 e->flags |= EDGE_IGNORE;
10771 /* Allocate our unwinder stack to unwind any temporary equivalences
10772 that might be recorded. */
10773 const_and_copies *equiv_stack = new const_and_copies ();
10775 hash_table<expr_elt_hasher> *avail_exprs
10776 = new hash_table<expr_elt_hasher> (1024);
10777 avail_exprs_stack *avail_exprs_stack
10778 = new class avail_exprs_stack (avail_exprs);
10780 vrp_dom_walker walker (CDI_DOMINATORS, equiv_stack, avail_exprs_stack);
10781 walker.walk (cfun->cfg->x_entry_block_ptr);
10783 /* Clear EDGE_IGNORE. */
10784 FOR_EACH_VEC_ELT (to_remove_edges, i, e)
10785 e->flags &= ~EDGE_IGNORE;
10787 /* We do not actually update the CFG or SSA graphs at this point as
10788 ASSERT_EXPRs are still in the IL and cfg cleanup code does not yet
10789 handle ASSERT_EXPRs gracefully. */
10790 delete equiv_stack;
10791 delete avail_exprs;
10792 delete avail_exprs_stack;
10795 /* Free VRP lattice. */
10797 static void
10798 vrp_free_lattice ()
10800 /* Free allocated memory. */
10801 free (vr_value);
10802 free (vr_phi_edge_counts);
10803 bitmap_obstack_release (&vrp_equiv_obstack);
10804 vrp_value_range_pool.release ();
10806 /* So that we can distinguish between VRP data being available
10807 and not available. */
10808 vr_value = NULL;
10809 vr_phi_edge_counts = NULL;
10812 /* Traverse all the blocks folding conditionals with known ranges. */
10814 static void
10815 vrp_finalize (bool warn_array_bounds_p)
10817 size_t i;
10819 values_propagated = true;
10821 if (dump_file)
10823 fprintf (dump_file, "\nValue ranges after VRP:\n\n");
10824 dump_all_value_ranges (dump_file);
10825 fprintf (dump_file, "\n");
10828 /* Set value range to non pointer SSA_NAMEs. */
10829 for (i = 0; i < num_vr_values; i++)
10830 if (vr_value[i])
10832 tree name = ssa_name (i);
10834 if (!name
10835 || (vr_value[i]->type == VR_VARYING)
10836 || (vr_value[i]->type == VR_UNDEFINED)
10837 || (TREE_CODE (vr_value[i]->min) != INTEGER_CST)
10838 || (TREE_CODE (vr_value[i]->max) != INTEGER_CST))
10839 continue;
10841 if (POINTER_TYPE_P (TREE_TYPE (name))
10842 && ((vr_value[i]->type == VR_RANGE
10843 && range_includes_zero_p (vr_value[i]->min,
10844 vr_value[i]->max) == 0)
10845 || (vr_value[i]->type == VR_ANTI_RANGE
10846 && range_includes_zero_p (vr_value[i]->min,
10847 vr_value[i]->max) == 1)))
10848 set_ptr_nonnull (name);
10849 else if (!POINTER_TYPE_P (TREE_TYPE (name)))
10850 set_range_info (name, vr_value[i]->type, vr_value[i]->min,
10851 vr_value[i]->max);
10854 substitute_and_fold (op_with_constant_singleton_value_range, vrp_fold_stmt);
10856 if (warn_array_bounds && warn_array_bounds_p)
10857 check_all_array_refs ();
10860 /* evrp_dom_walker visits the basic blocks in the dominance order and set
10861 the Value Ranges (VR) for SSA_NAMEs in the scope. Use this VR to
10862 discover more VRs. */
10864 class evrp_dom_walker : public dom_walker
10866 public:
10867 evrp_dom_walker ()
10868 : dom_walker (CDI_DOMINATORS), stack (10)
10870 need_eh_cleanup = BITMAP_ALLOC (NULL);
10872 ~evrp_dom_walker ()
10874 BITMAP_FREE (need_eh_cleanup);
10876 virtual edge before_dom_children (basic_block);
10877 virtual void after_dom_children (basic_block);
10878 void push_value_range (tree var, value_range *vr);
10879 value_range *pop_value_range (tree var);
10880 value_range *try_find_new_range (tree, tree op, tree_code code, tree limit);
10882 /* Cond_stack holds the old VR. */
10883 auto_vec<std::pair <tree, value_range*> > stack;
10884 bitmap need_eh_cleanup;
10885 auto_vec<gimple *> stmts_to_fixup;
10886 auto_vec<gimple *> stmts_to_remove;
10889 /* Find new range for NAME such that (OP CODE LIMIT) is true. */
10891 value_range *
10892 evrp_dom_walker::try_find_new_range (tree name,
10893 tree op, tree_code code, tree limit)
10895 value_range vr = VR_INITIALIZER;
10896 value_range *old_vr = get_value_range (name);
10898 /* Discover VR when condition is true. */
10899 extract_range_for_var_from_comparison_expr (name, code, op,
10900 limit, &vr);
10901 /* If we found any usable VR, set the VR to ssa_name and create a
10902 PUSH old value in the stack with the old VR. */
10903 if (vr.type == VR_RANGE || vr.type == VR_ANTI_RANGE)
10905 if (old_vr->type == vr.type
10906 && vrp_operand_equal_p (old_vr->min, vr.min)
10907 && vrp_operand_equal_p (old_vr->max, vr.max))
10908 return NULL;
10909 value_range *new_vr = vrp_value_range_pool.allocate ();
10910 *new_vr = vr;
10911 return new_vr;
10913 return NULL;
10916 /* See if there is any new scope is entered with new VR and set that VR to
10917 ssa_name before visiting the statements in the scope. */
10919 edge
10920 evrp_dom_walker::before_dom_children (basic_block bb)
10922 tree op0 = NULL_TREE;
10923 edge_iterator ei;
10924 edge e;
10926 if (dump_file && (dump_flags & TDF_DETAILS))
10927 fprintf (dump_file, "Visiting BB%d\n", bb->index);
10929 stack.safe_push (std::make_pair (NULL_TREE, (value_range *)NULL));
10931 edge pred_e = NULL;
10932 FOR_EACH_EDGE (e, ei, bb->preds)
10934 /* Ignore simple backedges from this to allow recording conditions
10935 in loop headers. */
10936 if (dominated_by_p (CDI_DOMINATORS, e->src, e->dest))
10937 continue;
10938 if (! pred_e)
10939 pred_e = e;
10940 else
10942 pred_e = NULL;
10943 break;
10946 if (pred_e)
10948 gimple *stmt = last_stmt (pred_e->src);
10949 if (stmt
10950 && gimple_code (stmt) == GIMPLE_COND
10951 && (op0 = gimple_cond_lhs (stmt))
10952 && TREE_CODE (op0) == SSA_NAME
10953 && (INTEGRAL_TYPE_P (TREE_TYPE (gimple_cond_lhs (stmt)))
10954 || POINTER_TYPE_P (TREE_TYPE (gimple_cond_lhs (stmt)))))
10956 if (dump_file && (dump_flags & TDF_DETAILS))
10958 fprintf (dump_file, "Visiting controlling predicate ");
10959 print_gimple_stmt (dump_file, stmt, 0);
10961 /* Entering a new scope. Try to see if we can find a VR
10962 here. */
10963 tree op1 = gimple_cond_rhs (stmt);
10964 if (TREE_OVERFLOW_P (op1))
10965 op1 = drop_tree_overflow (op1);
10966 tree_code code = gimple_cond_code (stmt);
10968 auto_vec<assert_info, 8> asserts;
10969 register_edge_assert_for (op0, pred_e, code, op0, op1, asserts);
10970 if (TREE_CODE (op1) == SSA_NAME)
10971 register_edge_assert_for (op1, pred_e, code, op0, op1, asserts);
10973 auto_vec<std::pair<tree, value_range *>, 8> vrs;
10974 for (unsigned i = 0; i < asserts.length (); ++i)
10976 value_range *vr = try_find_new_range (asserts[i].name,
10977 asserts[i].expr,
10978 asserts[i].comp_code,
10979 asserts[i].val);
10980 if (vr)
10981 vrs.safe_push (std::make_pair (asserts[i].name, vr));
10983 /* Push updated ranges only after finding all of them to avoid
10984 ordering issues that can lead to worse ranges. */
10985 for (unsigned i = 0; i < vrs.length (); ++i)
10986 push_value_range (vrs[i].first, vrs[i].second);
10990 /* Visit PHI stmts and discover any new VRs possible. */
10991 bool has_unvisited_preds = false;
10992 FOR_EACH_EDGE (e, ei, bb->preds)
10993 if (e->flags & EDGE_EXECUTABLE
10994 && !(e->src->flags & BB_VISITED))
10996 has_unvisited_preds = true;
10997 break;
11000 for (gphi_iterator gpi = gsi_start_phis (bb);
11001 !gsi_end_p (gpi); gsi_next (&gpi))
11003 gphi *phi = gpi.phi ();
11004 tree lhs = PHI_RESULT (phi);
11005 if (virtual_operand_p (lhs))
11006 continue;
11007 value_range vr_result = VR_INITIALIZER;
11008 bool interesting = stmt_interesting_for_vrp (phi);
11009 if (interesting && dump_file && (dump_flags & TDF_DETAILS))
11011 fprintf (dump_file, "Visiting PHI node ");
11012 print_gimple_stmt (dump_file, phi, 0);
11014 if (!has_unvisited_preds
11015 && interesting)
11016 extract_range_from_phi_node (phi, &vr_result);
11017 else
11019 set_value_range_to_varying (&vr_result);
11020 /* When we have an unvisited executable predecessor we can't
11021 use PHI arg ranges which may be still UNDEFINED but have
11022 to use VARYING for them. But we can still resort to
11023 SCEV for loop header PHIs. */
11024 struct loop *l;
11025 if (interesting
11026 && (l = loop_containing_stmt (phi))
11027 && l->header == gimple_bb (phi))
11028 adjust_range_with_scev (&vr_result, l, phi, lhs);
11030 update_value_range (lhs, &vr_result);
11032 /* Mark PHIs whose lhs we fully propagate for removal. */
11033 tree val = op_with_constant_singleton_value_range (lhs);
11034 if (val && may_propagate_copy (lhs, val))
11036 stmts_to_remove.safe_push (phi);
11037 continue;
11040 /* Set the SSA with the value range. */
11041 if (INTEGRAL_TYPE_P (TREE_TYPE (lhs)))
11043 if ((vr_result.type == VR_RANGE
11044 || vr_result.type == VR_ANTI_RANGE)
11045 && (TREE_CODE (vr_result.min) == INTEGER_CST)
11046 && (TREE_CODE (vr_result.max) == INTEGER_CST))
11047 set_range_info (lhs,
11048 vr_result.type, vr_result.min, vr_result.max);
11050 else if (POINTER_TYPE_P (TREE_TYPE (lhs))
11051 && ((vr_result.type == VR_RANGE
11052 && range_includes_zero_p (vr_result.min,
11053 vr_result.max) == 0)
11054 || (vr_result.type == VR_ANTI_RANGE
11055 && range_includes_zero_p (vr_result.min,
11056 vr_result.max) == 1)))
11057 set_ptr_nonnull (lhs);
11060 edge taken_edge = NULL;
11062 /* Visit all other stmts and discover any new VRs possible. */
11063 for (gimple_stmt_iterator gsi = gsi_start_bb (bb);
11064 !gsi_end_p (gsi); gsi_next (&gsi))
11066 gimple *stmt = gsi_stmt (gsi);
11067 tree output = NULL_TREE;
11068 gimple *old_stmt = stmt;
11069 bool was_noreturn = (is_gimple_call (stmt)
11070 && gimple_call_noreturn_p (stmt));
11072 if (dump_file && (dump_flags & TDF_DETAILS))
11074 fprintf (dump_file, "Visiting stmt ");
11075 print_gimple_stmt (dump_file, stmt, 0);
11078 if (gcond *cond = dyn_cast <gcond *> (stmt))
11080 vrp_visit_cond_stmt (cond, &taken_edge);
11081 if (taken_edge)
11083 if (taken_edge->flags & EDGE_TRUE_VALUE)
11084 gimple_cond_make_true (cond);
11085 else if (taken_edge->flags & EDGE_FALSE_VALUE)
11086 gimple_cond_make_false (cond);
11087 else
11088 gcc_unreachable ();
11089 update_stmt (stmt);
11092 else if (stmt_interesting_for_vrp (stmt))
11094 edge taken_edge;
11095 value_range vr = VR_INITIALIZER;
11096 extract_range_from_stmt (stmt, &taken_edge, &output, &vr);
11097 if (output
11098 && (vr.type == VR_RANGE || vr.type == VR_ANTI_RANGE))
11100 update_value_range (output, &vr);
11101 vr = *get_value_range (output);
11103 /* Mark stmts whose output we fully propagate for removal. */
11104 tree val;
11105 if ((val = op_with_constant_singleton_value_range (output))
11106 && may_propagate_copy (output, val)
11107 && !stmt_could_throw_p (stmt)
11108 && !gimple_has_side_effects (stmt))
11110 stmts_to_remove.safe_push (stmt);
11111 continue;
11114 /* Set the SSA with the value range. */
11115 if (INTEGRAL_TYPE_P (TREE_TYPE (output)))
11117 if ((vr.type == VR_RANGE
11118 || vr.type == VR_ANTI_RANGE)
11119 && (TREE_CODE (vr.min) == INTEGER_CST)
11120 && (TREE_CODE (vr.max) == INTEGER_CST))
11121 set_range_info (output, vr.type, vr.min, vr.max);
11123 else if (POINTER_TYPE_P (TREE_TYPE (output))
11124 && ((vr.type == VR_RANGE
11125 && range_includes_zero_p (vr.min,
11126 vr.max) == 0)
11127 || (vr.type == VR_ANTI_RANGE
11128 && range_includes_zero_p (vr.min,
11129 vr.max) == 1)))
11130 set_ptr_nonnull (output);
11132 else
11133 set_defs_to_varying (stmt);
11135 else
11136 set_defs_to_varying (stmt);
11138 /* See if we can derive a range for any of STMT's operands. */
11139 tree op;
11140 ssa_op_iter i;
11141 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_USE)
11143 tree value;
11144 enum tree_code comp_code;
11146 /* If OP is used in such a way that we can infer a value
11147 range for it, and we don't find a previous assertion for
11148 it, create a new assertion location node for OP. */
11149 if (infer_value_range (stmt, op, &comp_code, &value))
11151 /* If we are able to infer a nonzero value range for OP,
11152 then walk backwards through the use-def chain to see if OP
11153 was set via a typecast.
11154 If so, then we can also infer a nonzero value range
11155 for the operand of the NOP_EXPR. */
11156 if (comp_code == NE_EXPR && integer_zerop (value))
11158 tree t = op;
11159 gimple *def_stmt = SSA_NAME_DEF_STMT (t);
11160 while (is_gimple_assign (def_stmt)
11161 && CONVERT_EXPR_CODE_P
11162 (gimple_assign_rhs_code (def_stmt))
11163 && TREE_CODE
11164 (gimple_assign_rhs1 (def_stmt)) == SSA_NAME
11165 && POINTER_TYPE_P
11166 (TREE_TYPE (gimple_assign_rhs1 (def_stmt))))
11168 t = gimple_assign_rhs1 (def_stmt);
11169 def_stmt = SSA_NAME_DEF_STMT (t);
11171 /* Add VR when (T COMP_CODE value) condition is
11172 true. */
11173 value_range *op_range
11174 = try_find_new_range (t, t, comp_code, value);
11175 if (op_range)
11176 push_value_range (t, op_range);
11179 /* Add VR when (OP COMP_CODE value) condition is true. */
11180 value_range *op_range = try_find_new_range (op, op,
11181 comp_code, value);
11182 if (op_range)
11183 push_value_range (op, op_range);
11187 /* Try folding stmts with the VR discovered. */
11188 bool did_replace
11189 = replace_uses_in (stmt, op_with_constant_singleton_value_range);
11190 if (fold_stmt (&gsi, follow_single_use_edges)
11191 || did_replace)
11193 stmt = gsi_stmt (gsi);
11194 update_stmt (stmt);
11195 did_replace = true;
11198 if (did_replace)
11200 /* If we cleaned up EH information from the statement,
11201 remove EH edges. */
11202 if (maybe_clean_or_replace_eh_stmt (old_stmt, stmt))
11203 bitmap_set_bit (need_eh_cleanup, bb->index);
11205 /* If we turned a not noreturn call into a noreturn one
11206 schedule it for fixup. */
11207 if (!was_noreturn
11208 && is_gimple_call (stmt)
11209 && gimple_call_noreturn_p (stmt))
11210 stmts_to_fixup.safe_push (stmt);
11212 if (gimple_assign_single_p (stmt))
11214 tree rhs = gimple_assign_rhs1 (stmt);
11215 if (TREE_CODE (rhs) == ADDR_EXPR)
11216 recompute_tree_invariant_for_addr_expr (rhs);
11221 /* Visit BB successor PHI nodes and replace PHI args. */
11222 FOR_EACH_EDGE (e, ei, bb->succs)
11224 for (gphi_iterator gpi = gsi_start_phis (e->dest);
11225 !gsi_end_p (gpi); gsi_next (&gpi))
11227 gphi *phi = gpi.phi ();
11228 use_operand_p use_p = PHI_ARG_DEF_PTR_FROM_EDGE (phi, e);
11229 tree arg = USE_FROM_PTR (use_p);
11230 if (TREE_CODE (arg) != SSA_NAME
11231 || virtual_operand_p (arg))
11232 continue;
11233 tree val = op_with_constant_singleton_value_range (arg);
11234 if (val && may_propagate_copy (arg, val))
11235 propagate_value (use_p, val);
11239 bb->flags |= BB_VISITED;
11241 return taken_edge;
11244 /* Restore/pop VRs valid only for BB when we leave BB. */
11246 void
11247 evrp_dom_walker::after_dom_children (basic_block bb ATTRIBUTE_UNUSED)
11249 gcc_checking_assert (!stack.is_empty ());
11250 while (stack.last ().first != NULL_TREE)
11251 pop_value_range (stack.last ().first);
11252 stack.pop ();
11255 /* Push the Value Range of VAR to the stack and update it with new VR. */
11257 void
11258 evrp_dom_walker::push_value_range (tree var, value_range *vr)
11260 if (SSA_NAME_VERSION (var) >= num_vr_values)
11261 return;
11262 if (dump_file && (dump_flags & TDF_DETAILS))
11264 fprintf (dump_file, "pushing new range for ");
11265 print_generic_expr (dump_file, var);
11266 fprintf (dump_file, ": ");
11267 dump_value_range (dump_file, vr);
11268 fprintf (dump_file, "\n");
11270 stack.safe_push (std::make_pair (var, get_value_range (var)));
11271 vr_value[SSA_NAME_VERSION (var)] = vr;
11274 /* Pop the Value Range from the vrp_stack and update VAR with it. */
11276 value_range *
11277 evrp_dom_walker::pop_value_range (tree var)
11279 value_range *vr = stack.last ().second;
11280 gcc_checking_assert (var == stack.last ().first);
11281 if (dump_file && (dump_flags & TDF_DETAILS))
11283 fprintf (dump_file, "popping range for ");
11284 print_generic_expr (dump_file, var);
11285 fprintf (dump_file, ", restoring ");
11286 dump_value_range (dump_file, vr);
11287 fprintf (dump_file, "\n");
11289 vr_value[SSA_NAME_VERSION (var)] = vr;
11290 stack.pop ();
11291 return vr;
11295 /* Main entry point for the early vrp pass which is a simplified non-iterative
11296 version of vrp where basic blocks are visited in dominance order. Value
11297 ranges discovered in early vrp will also be used by ipa-vrp. */
11299 static unsigned int
11300 execute_early_vrp ()
11302 edge e;
11303 edge_iterator ei;
11304 basic_block bb;
11306 loop_optimizer_init (LOOPS_NORMAL | LOOPS_HAVE_RECORDED_EXITS);
11307 rewrite_into_loop_closed_ssa (NULL, TODO_update_ssa);
11308 scev_initialize ();
11309 calculate_dominance_info (CDI_DOMINATORS);
11310 FOR_EACH_BB_FN (bb, cfun)
11312 bb->flags &= ~BB_VISITED;
11313 FOR_EACH_EDGE (e, ei, bb->preds)
11314 e->flags |= EDGE_EXECUTABLE;
11316 vrp_initialize_lattice ();
11318 /* Walk stmts in dominance order and propagate VRP. */
11319 evrp_dom_walker walker;
11320 walker.walk (ENTRY_BLOCK_PTR_FOR_FN (cfun));
11322 if (dump_file)
11324 fprintf (dump_file, "\nValue ranges after Early VRP:\n\n");
11325 dump_all_value_ranges (dump_file);
11326 fprintf (dump_file, "\n");
11329 /* Remove stmts in reverse order to make debug stmt creation possible. */
11330 while (! walker.stmts_to_remove.is_empty ())
11332 gimple *stmt = walker.stmts_to_remove.pop ();
11333 if (dump_file && dump_flags & TDF_DETAILS)
11335 fprintf (dump_file, "Removing dead stmt ");
11336 print_gimple_stmt (dump_file, stmt, 0);
11337 fprintf (dump_file, "\n");
11339 gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
11340 if (gimple_code (stmt) == GIMPLE_PHI)
11341 remove_phi_node (&gsi, true);
11342 else
11344 unlink_stmt_vdef (stmt);
11345 gsi_remove (&gsi, true);
11346 release_defs (stmt);
11350 if (!bitmap_empty_p (walker.need_eh_cleanup))
11351 gimple_purge_all_dead_eh_edges (walker.need_eh_cleanup);
11353 /* Fixup stmts that became noreturn calls. This may require splitting
11354 blocks and thus isn't possible during the dominator walk. Do this
11355 in reverse order so we don't inadvertedly remove a stmt we want to
11356 fixup by visiting a dominating now noreturn call first. */
11357 while (!walker.stmts_to_fixup.is_empty ())
11359 gimple *stmt = walker.stmts_to_fixup.pop ();
11360 fixup_noreturn_call (stmt);
11363 vrp_free_lattice ();
11364 scev_finalize ();
11365 loop_optimizer_finalize ();
11366 return 0;
11370 /* Main entry point to VRP (Value Range Propagation). This pass is
11371 loosely based on J. R. C. Patterson, ``Accurate Static Branch
11372 Prediction by Value Range Propagation,'' in SIGPLAN Conference on
11373 Programming Language Design and Implementation, pp. 67-78, 1995.
11374 Also available at http://citeseer.ist.psu.edu/patterson95accurate.html
11376 This is essentially an SSA-CCP pass modified to deal with ranges
11377 instead of constants.
11379 While propagating ranges, we may find that two or more SSA name
11380 have equivalent, though distinct ranges. For instance,
11382 1 x_9 = p_3->a;
11383 2 p_4 = ASSERT_EXPR <p_3, p_3 != 0>
11384 3 if (p_4 == q_2)
11385 4 p_5 = ASSERT_EXPR <p_4, p_4 == q_2>;
11386 5 endif
11387 6 if (q_2)
11389 In the code above, pointer p_5 has range [q_2, q_2], but from the
11390 code we can also determine that p_5 cannot be NULL and, if q_2 had
11391 a non-varying range, p_5's range should also be compatible with it.
11393 These equivalences are created by two expressions: ASSERT_EXPR and
11394 copy operations. Since p_5 is an assertion on p_4, and p_4 was the
11395 result of another assertion, then we can use the fact that p_5 and
11396 p_4 are equivalent when evaluating p_5's range.
11398 Together with value ranges, we also propagate these equivalences
11399 between names so that we can take advantage of information from
11400 multiple ranges when doing final replacement. Note that this
11401 equivalency relation is transitive but not symmetric.
11403 In the example above, p_5 is equivalent to p_4, q_2 and p_3, but we
11404 cannot assert that q_2 is equivalent to p_5 because q_2 may be used
11405 in contexts where that assertion does not hold (e.g., in line 6).
11407 TODO, the main difference between this pass and Patterson's is that
11408 we do not propagate edge probabilities. We only compute whether
11409 edges can be taken or not. That is, instead of having a spectrum
11410 of jump probabilities between 0 and 1, we only deal with 0, 1 and
11411 DON'T KNOW. In the future, it may be worthwhile to propagate
11412 probabilities to aid branch prediction. */
11414 static unsigned int
11415 execute_vrp (bool warn_array_bounds_p)
11417 int i;
11418 edge e;
11419 switch_update *su;
11421 loop_optimizer_init (LOOPS_NORMAL | LOOPS_HAVE_RECORDED_EXITS);
11422 rewrite_into_loop_closed_ssa (NULL, TODO_update_ssa);
11423 scev_initialize ();
11425 /* ??? This ends up using stale EDGE_DFS_BACK for liveness computation.
11426 Inserting assertions may split edges which will invalidate
11427 EDGE_DFS_BACK. */
11428 insert_range_assertions ();
11430 to_remove_edges.create (10);
11431 to_update_switch_stmts.create (5);
11432 threadedge_initialize_values ();
11434 /* For visiting PHI nodes we need EDGE_DFS_BACK computed. */
11435 mark_dfs_back_edges ();
11437 vrp_initialize_lattice ();
11438 vrp_initialize ();
11439 ssa_propagate (vrp_visit_stmt, vrp_visit_phi_node);
11440 vrp_finalize (warn_array_bounds_p);
11442 /* We must identify jump threading opportunities before we release
11443 the datastructures built by VRP. */
11444 identify_jump_threads ();
11446 /* A comparison of an SSA_NAME against a constant where the SSA_NAME
11447 was set by a type conversion can often be rewritten to use the
11448 RHS of the type conversion.
11450 However, doing so inhibits jump threading through the comparison.
11451 So that transformation is not performed until after jump threading
11452 is complete. */
11453 basic_block bb;
11454 FOR_EACH_BB_FN (bb, cfun)
11456 gimple *last = last_stmt (bb);
11457 if (last && gimple_code (last) == GIMPLE_COND)
11458 simplify_cond_using_ranges_2 (as_a <gcond *> (last));
11461 vrp_free_lattice ();
11463 free_numbers_of_iterations_estimates (cfun);
11465 /* ASSERT_EXPRs must be removed before finalizing jump threads
11466 as finalizing jump threads calls the CFG cleanup code which
11467 does not properly handle ASSERT_EXPRs. */
11468 remove_range_assertions ();
11470 /* If we exposed any new variables, go ahead and put them into
11471 SSA form now, before we handle jump threading. This simplifies
11472 interactions between rewriting of _DECL nodes into SSA form
11473 and rewriting SSA_NAME nodes into SSA form after block
11474 duplication and CFG manipulation. */
11475 update_ssa (TODO_update_ssa);
11477 /* We identified all the jump threading opportunities earlier, but could
11478 not transform the CFG at that time. This routine transforms the
11479 CFG and arranges for the dominator tree to be rebuilt if necessary.
11481 Note the SSA graph update will occur during the normal TODO
11482 processing by the pass manager. */
11483 thread_through_all_blocks (false);
11485 /* Remove dead edges from SWITCH_EXPR optimization. This leaves the
11486 CFG in a broken state and requires a cfg_cleanup run. */
11487 FOR_EACH_VEC_ELT (to_remove_edges, i, e)
11488 remove_edge (e);
11489 /* Update SWITCH_EXPR case label vector. */
11490 FOR_EACH_VEC_ELT (to_update_switch_stmts, i, su)
11492 size_t j;
11493 size_t n = TREE_VEC_LENGTH (su->vec);
11494 tree label;
11495 gimple_switch_set_num_labels (su->stmt, n);
11496 for (j = 0; j < n; j++)
11497 gimple_switch_set_label (su->stmt, j, TREE_VEC_ELT (su->vec, j));
11498 /* As we may have replaced the default label with a regular one
11499 make sure to make it a real default label again. This ensures
11500 optimal expansion. */
11501 label = gimple_switch_label (su->stmt, 0);
11502 CASE_LOW (label) = NULL_TREE;
11503 CASE_HIGH (label) = NULL_TREE;
11506 if (to_remove_edges.length () > 0)
11508 free_dominance_info (CDI_DOMINATORS);
11509 loops_state_set (LOOPS_NEED_FIXUP);
11512 to_remove_edges.release ();
11513 to_update_switch_stmts.release ();
11514 threadedge_finalize_values ();
11516 scev_finalize ();
11517 loop_optimizer_finalize ();
11518 return 0;
11521 namespace {
11523 const pass_data pass_data_vrp =
11525 GIMPLE_PASS, /* type */
11526 "vrp", /* name */
11527 OPTGROUP_NONE, /* optinfo_flags */
11528 TV_TREE_VRP, /* tv_id */
11529 PROP_ssa, /* properties_required */
11530 0, /* properties_provided */
11531 0, /* properties_destroyed */
11532 0, /* todo_flags_start */
11533 ( TODO_cleanup_cfg | TODO_update_ssa ), /* todo_flags_finish */
11536 class pass_vrp : public gimple_opt_pass
11538 public:
11539 pass_vrp (gcc::context *ctxt)
11540 : gimple_opt_pass (pass_data_vrp, ctxt), warn_array_bounds_p (false)
11543 /* opt_pass methods: */
11544 opt_pass * clone () { return new pass_vrp (m_ctxt); }
11545 void set_pass_param (unsigned int n, bool param)
11547 gcc_assert (n == 0);
11548 warn_array_bounds_p = param;
11550 virtual bool gate (function *) { return flag_tree_vrp != 0; }
11551 virtual unsigned int execute (function *)
11552 { return execute_vrp (warn_array_bounds_p); }
11554 private:
11555 bool warn_array_bounds_p;
11556 }; // class pass_vrp
11558 } // anon namespace
11560 gimple_opt_pass *
11561 make_pass_vrp (gcc::context *ctxt)
11563 return new pass_vrp (ctxt);
11566 namespace {
11568 const pass_data pass_data_early_vrp =
11570 GIMPLE_PASS, /* type */
11571 "evrp", /* name */
11572 OPTGROUP_NONE, /* optinfo_flags */
11573 TV_TREE_EARLY_VRP, /* tv_id */
11574 PROP_ssa, /* properties_required */
11575 0, /* properties_provided */
11576 0, /* properties_destroyed */
11577 0, /* todo_flags_start */
11578 ( TODO_cleanup_cfg | TODO_update_ssa | TODO_verify_all ),
11581 class pass_early_vrp : public gimple_opt_pass
11583 public:
11584 pass_early_vrp (gcc::context *ctxt)
11585 : gimple_opt_pass (pass_data_early_vrp, ctxt)
11588 /* opt_pass methods: */
11589 opt_pass * clone () { return new pass_early_vrp (m_ctxt); }
11590 virtual bool gate (function *)
11592 return flag_tree_vrp != 0;
11594 virtual unsigned int execute (function *)
11595 { return execute_early_vrp (); }
11597 }; // class pass_vrp
11598 } // anon namespace
11600 gimple_opt_pass *
11601 make_pass_early_vrp (gcc::context *ctxt)
11603 return new pass_early_vrp (ctxt);