2017-06-16 Yury Gribov <tetra2005@gmail.com>
[official-gcc.git] / gcc / tree-vrp.c
blob9ca3924ea89c21645152347aabea48e606f60121
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"
66 #define VR_INITIALIZER { VR_UNDEFINED, NULL_TREE, NULL_TREE, NULL }
68 /* Allocation pools for tree-vrp allocations. */
69 static object_allocator<value_range> vrp_value_range_pool ("Tree VRP value ranges");
70 static bitmap_obstack vrp_equiv_obstack;
72 /* Set of SSA names found live during the RPO traversal of the function
73 for still active basic-blocks. */
74 static sbitmap *live;
76 /* Return true if the SSA name NAME is live on the edge E. */
78 static bool
79 live_on_edge (edge e, tree name)
81 return (live[e->dest->index]
82 && bitmap_bit_p (live[e->dest->index], SSA_NAME_VERSION (name)));
85 /* Local functions. */
86 static int compare_values (tree val1, tree val2);
87 static int compare_values_warnv (tree val1, tree val2, bool *);
88 static tree vrp_evaluate_conditional_warnv_with_ops (enum tree_code,
89 tree, tree, bool, bool *,
90 bool *);
92 struct assert_info
94 /* Predicate code for the ASSERT_EXPR. Must be COMPARISON_CLASS_P. */
95 enum tree_code comp_code;
97 /* Name to register the assert for. */
98 tree name;
100 /* Value being compared against. */
101 tree val;
103 /* Expression to compare. */
104 tree expr;
107 /* Location information for ASSERT_EXPRs. Each instance of this
108 structure describes an ASSERT_EXPR for an SSA name. Since a single
109 SSA name may have more than one assertion associated with it, these
110 locations are kept in a linked list attached to the corresponding
111 SSA name. */
112 struct assert_locus
114 /* Basic block where the assertion would be inserted. */
115 basic_block bb;
117 /* Some assertions need to be inserted on an edge (e.g., assertions
118 generated by COND_EXPRs). In those cases, BB will be NULL. */
119 edge e;
121 /* Pointer to the statement that generated this assertion. */
122 gimple_stmt_iterator si;
124 /* Predicate code for the ASSERT_EXPR. Must be COMPARISON_CLASS_P. */
125 enum tree_code comp_code;
127 /* Value being compared against. */
128 tree val;
130 /* Expression to compare. */
131 tree expr;
133 /* Next node in the linked list. */
134 assert_locus *next;
137 /* If bit I is present, it means that SSA name N_i has a list of
138 assertions that should be inserted in the IL. */
139 static bitmap need_assert_for;
141 /* Array of locations lists where to insert assertions. ASSERTS_FOR[I]
142 holds a list of ASSERT_LOCUS_T nodes that describe where
143 ASSERT_EXPRs for SSA name N_I should be inserted. */
144 static assert_locus **asserts_for;
146 /* Value range array. After propagation, VR_VALUE[I] holds the range
147 of values that SSA name N_I may take. */
148 static unsigned num_vr_values;
149 static value_range **vr_value;
150 static bool values_propagated;
152 /* For a PHI node which sets SSA name N_I, VR_COUNTS[I] holds the
153 number of executable edges we saw the last time we visited the
154 node. */
155 static int *vr_phi_edge_counts;
157 struct switch_update {
158 gswitch *stmt;
159 tree vec;
162 static vec<edge> to_remove_edges;
163 static vec<switch_update> to_update_switch_stmts;
166 /* Return the maximum value for TYPE. */
168 static inline tree
169 vrp_val_max (const_tree type)
171 if (!INTEGRAL_TYPE_P (type))
172 return NULL_TREE;
174 return TYPE_MAX_VALUE (type);
177 /* Return the minimum value for TYPE. */
179 static inline tree
180 vrp_val_min (const_tree type)
182 if (!INTEGRAL_TYPE_P (type))
183 return NULL_TREE;
185 return TYPE_MIN_VALUE (type);
188 /* Return whether VAL is equal to the maximum value of its type.
189 We can't do a simple equality comparison with TYPE_MAX_VALUE because
190 C typedefs and Ada subtypes can produce types whose TYPE_MAX_VALUE
191 is not == to the integer constant with the same value in the type. */
193 static inline bool
194 vrp_val_is_max (const_tree val)
196 tree type_max = vrp_val_max (TREE_TYPE (val));
197 return (val == type_max
198 || (type_max != NULL_TREE
199 && operand_equal_p (val, type_max, 0)));
202 /* Return whether VAL is equal to the minimum value of its type. */
204 static inline bool
205 vrp_val_is_min (const_tree val)
207 tree type_min = vrp_val_min (TREE_TYPE (val));
208 return (val == type_min
209 || (type_min != NULL_TREE
210 && operand_equal_p (val, type_min, 0)));
214 /* Set value range VR to VR_UNDEFINED. */
216 static inline void
217 set_value_range_to_undefined (value_range *vr)
219 vr->type = VR_UNDEFINED;
220 vr->min = vr->max = NULL_TREE;
221 if (vr->equiv)
222 bitmap_clear (vr->equiv);
226 /* Set value range VR to VR_VARYING. */
228 static inline void
229 set_value_range_to_varying (value_range *vr)
231 vr->type = VR_VARYING;
232 vr->min = vr->max = NULL_TREE;
233 if (vr->equiv)
234 bitmap_clear (vr->equiv);
238 /* Set value range VR to {T, MIN, MAX, EQUIV}. */
240 static void
241 set_value_range (value_range *vr, enum value_range_type t, tree min,
242 tree max, bitmap equiv)
244 /* Check the validity of the range. */
245 if (flag_checking
246 && (t == VR_RANGE || t == VR_ANTI_RANGE))
248 int cmp;
250 gcc_assert (min && max);
252 gcc_assert (!TREE_OVERFLOW_P (min) && !TREE_OVERFLOW_P (max));
254 if (INTEGRAL_TYPE_P (TREE_TYPE (min)) && t == VR_ANTI_RANGE)
255 gcc_assert (!vrp_val_is_min (min) || !vrp_val_is_max (max));
257 cmp = compare_values (min, max);
258 gcc_assert (cmp == 0 || cmp == -1 || cmp == -2);
261 if (flag_checking
262 && (t == VR_UNDEFINED || t == VR_VARYING))
264 gcc_assert (min == NULL_TREE && max == NULL_TREE);
265 gcc_assert (equiv == NULL || bitmap_empty_p (equiv));
268 vr->type = t;
269 vr->min = min;
270 vr->max = max;
272 /* Since updating the equivalence set involves deep copying the
273 bitmaps, only do it if absolutely necessary. */
274 if (vr->equiv == NULL
275 && equiv != NULL)
276 vr->equiv = BITMAP_ALLOC (&vrp_equiv_obstack);
278 if (equiv != vr->equiv)
280 if (equiv && !bitmap_empty_p (equiv))
281 bitmap_copy (vr->equiv, equiv);
282 else
283 bitmap_clear (vr->equiv);
288 /* Set value range VR to the canonical form of {T, MIN, MAX, EQUIV}.
289 This means adjusting T, MIN and MAX representing the case of a
290 wrapping range with MAX < MIN covering [MIN, type_max] U [type_min, MAX]
291 as anti-rage ~[MAX+1, MIN-1]. Likewise for wrapping anti-ranges.
292 In corner cases where MAX+1 or MIN-1 wraps this will fall back
293 to varying.
294 This routine exists to ease canonicalization in the case where we
295 extract ranges from var + CST op limit. */
297 static void
298 set_and_canonicalize_value_range (value_range *vr, enum value_range_type t,
299 tree min, tree max, bitmap equiv)
301 /* Use the canonical setters for VR_UNDEFINED and VR_VARYING. */
302 if (t == VR_UNDEFINED)
304 set_value_range_to_undefined (vr);
305 return;
307 else if (t == VR_VARYING)
309 set_value_range_to_varying (vr);
310 return;
313 /* Nothing to canonicalize for symbolic ranges. */
314 if (TREE_CODE (min) != INTEGER_CST
315 || TREE_CODE (max) != INTEGER_CST)
317 set_value_range (vr, t, min, max, equiv);
318 return;
321 /* Wrong order for min and max, to swap them and the VR type we need
322 to adjust them. */
323 if (tree_int_cst_lt (max, min))
325 tree one, tmp;
327 /* For one bit precision if max < min, then the swapped
328 range covers all values, so for VR_RANGE it is varying and
329 for VR_ANTI_RANGE empty range, so drop to varying as well. */
330 if (TYPE_PRECISION (TREE_TYPE (min)) == 1)
332 set_value_range_to_varying (vr);
333 return;
336 one = build_int_cst (TREE_TYPE (min), 1);
337 tmp = int_const_binop (PLUS_EXPR, max, one);
338 max = int_const_binop (MINUS_EXPR, min, one);
339 min = tmp;
341 /* There's one corner case, if we had [C+1, C] before we now have
342 that again. But this represents an empty value range, so drop
343 to varying in this case. */
344 if (tree_int_cst_lt (max, min))
346 set_value_range_to_varying (vr);
347 return;
350 t = t == VR_RANGE ? VR_ANTI_RANGE : VR_RANGE;
353 /* Anti-ranges that can be represented as ranges should be so. */
354 if (t == VR_ANTI_RANGE)
356 bool is_min = vrp_val_is_min (min);
357 bool is_max = vrp_val_is_max (max);
359 if (is_min && is_max)
361 /* We cannot deal with empty ranges, drop to varying.
362 ??? This could be VR_UNDEFINED instead. */
363 set_value_range_to_varying (vr);
364 return;
366 else if (TYPE_PRECISION (TREE_TYPE (min)) == 1
367 && (is_min || is_max))
369 /* Non-empty boolean ranges can always be represented
370 as a singleton range. */
371 if (is_min)
372 min = max = vrp_val_max (TREE_TYPE (min));
373 else
374 min = max = vrp_val_min (TREE_TYPE (min));
375 t = VR_RANGE;
377 else if (is_min
378 /* As a special exception preserve non-null ranges. */
379 && !(TYPE_UNSIGNED (TREE_TYPE (min))
380 && integer_zerop (max)))
382 tree one = build_int_cst (TREE_TYPE (max), 1);
383 min = int_const_binop (PLUS_EXPR, max, one);
384 max = vrp_val_max (TREE_TYPE (max));
385 t = VR_RANGE;
387 else if (is_max)
389 tree one = build_int_cst (TREE_TYPE (min), 1);
390 max = int_const_binop (MINUS_EXPR, min, one);
391 min = vrp_val_min (TREE_TYPE (min));
392 t = VR_RANGE;
396 /* Do not drop [-INF(OVF), +INF(OVF)] to varying. (OVF) has to be sticky
397 to make sure VRP iteration terminates, otherwise we can get into
398 oscillations. */
400 set_value_range (vr, t, min, max, equiv);
403 /* Copy value range FROM into value range TO. */
405 static inline void
406 copy_value_range (value_range *to, value_range *from)
408 set_value_range (to, from->type, from->min, from->max, from->equiv);
411 /* Set value range VR to a single value. This function is only called
412 with values we get from statements, and exists to clear the
413 TREE_OVERFLOW flag. */
415 static inline void
416 set_value_range_to_value (value_range *vr, tree val, bitmap equiv)
418 gcc_assert (is_gimple_min_invariant (val));
419 if (TREE_OVERFLOW_P (val))
420 val = drop_tree_overflow (val);
421 set_value_range (vr, VR_RANGE, val, val, equiv);
424 /* Set value range VR to a non-negative range of type TYPE. */
426 static inline void
427 set_value_range_to_nonnegative (value_range *vr, tree type)
429 tree zero = build_int_cst (type, 0);
430 set_value_range (vr, VR_RANGE, zero, vrp_val_max (type), vr->equiv);
433 /* Set value range VR to a non-NULL range of type TYPE. */
435 static inline void
436 set_value_range_to_nonnull (value_range *vr, tree type)
438 tree zero = build_int_cst (type, 0);
439 set_value_range (vr, VR_ANTI_RANGE, zero, zero, vr->equiv);
443 /* Set value range VR to a NULL range of type TYPE. */
445 static inline void
446 set_value_range_to_null (value_range *vr, tree type)
448 set_value_range_to_value (vr, build_int_cst (type, 0), vr->equiv);
452 /* Set value range VR to a range of a truthvalue of type TYPE. */
454 static inline void
455 set_value_range_to_truthvalue (value_range *vr, tree type)
457 if (TYPE_PRECISION (type) == 1)
458 set_value_range_to_varying (vr);
459 else
460 set_value_range (vr, VR_RANGE,
461 build_int_cst (type, 0), build_int_cst (type, 1),
462 vr->equiv);
466 /* If abs (min) < abs (max), set VR to [-max, max], if
467 abs (min) >= abs (max), set VR to [-min, min]. */
469 static void
470 abs_extent_range (value_range *vr, tree min, tree max)
472 int cmp;
474 gcc_assert (TREE_CODE (min) == INTEGER_CST);
475 gcc_assert (TREE_CODE (max) == INTEGER_CST);
476 gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (min)));
477 gcc_assert (!TYPE_UNSIGNED (TREE_TYPE (min)));
478 min = fold_unary (ABS_EXPR, TREE_TYPE (min), min);
479 max = fold_unary (ABS_EXPR, TREE_TYPE (max), max);
480 if (TREE_OVERFLOW (min) || TREE_OVERFLOW (max))
482 set_value_range_to_varying (vr);
483 return;
485 cmp = compare_values (min, max);
486 if (cmp == -1)
487 min = fold_unary (NEGATE_EXPR, TREE_TYPE (min), max);
488 else if (cmp == 0 || cmp == 1)
490 max = min;
491 min = fold_unary (NEGATE_EXPR, TREE_TYPE (min), min);
493 else
495 set_value_range_to_varying (vr);
496 return;
498 set_and_canonicalize_value_range (vr, VR_RANGE, min, max, NULL);
502 /* Return value range information for VAR.
504 If we have no values ranges recorded (ie, VRP is not running), then
505 return NULL. Otherwise create an empty range if none existed for VAR. */
507 static value_range *
508 get_value_range (const_tree var)
510 static const value_range vr_const_varying
511 = { VR_VARYING, NULL_TREE, NULL_TREE, NULL };
512 value_range *vr;
513 tree sym;
514 unsigned ver = SSA_NAME_VERSION (var);
516 /* If we have no recorded ranges, then return NULL. */
517 if (! vr_value)
518 return NULL;
520 /* If we query the range for a new SSA name return an unmodifiable VARYING.
521 We should get here at most from the substitute-and-fold stage which
522 will never try to change values. */
523 if (ver >= num_vr_values)
524 return CONST_CAST (value_range *, &vr_const_varying);
526 vr = vr_value[ver];
527 if (vr)
528 return vr;
530 /* After propagation finished do not allocate new value-ranges. */
531 if (values_propagated)
532 return CONST_CAST (value_range *, &vr_const_varying);
534 /* Create a default value range. */
535 vr_value[ver] = vr = vrp_value_range_pool.allocate ();
536 memset (vr, 0, sizeof (*vr));
538 /* Defer allocating the equivalence set. */
539 vr->equiv = NULL;
541 /* If VAR is a default definition of a parameter, the variable can
542 take any value in VAR's type. */
543 if (SSA_NAME_IS_DEFAULT_DEF (var))
545 sym = SSA_NAME_VAR (var);
546 if (TREE_CODE (sym) == PARM_DECL)
548 /* Try to use the "nonnull" attribute to create ~[0, 0]
549 anti-ranges for pointers. Note that this is only valid with
550 default definitions of PARM_DECLs. */
551 if (POINTER_TYPE_P (TREE_TYPE (sym))
552 && (nonnull_arg_p (sym)
553 || get_ptr_nonnull (var)))
554 set_value_range_to_nonnull (vr, TREE_TYPE (sym));
555 else if (INTEGRAL_TYPE_P (TREE_TYPE (sym)))
557 wide_int min, max;
558 value_range_type rtype = get_range_info (var, &min, &max);
559 if (rtype == VR_RANGE || rtype == VR_ANTI_RANGE)
560 set_value_range (vr, rtype,
561 wide_int_to_tree (TREE_TYPE (var), min),
562 wide_int_to_tree (TREE_TYPE (var), max),
563 NULL);
564 else
565 set_value_range_to_varying (vr);
567 else
568 set_value_range_to_varying (vr);
570 else if (TREE_CODE (sym) == RESULT_DECL
571 && DECL_BY_REFERENCE (sym))
572 set_value_range_to_nonnull (vr, TREE_TYPE (sym));
575 return vr;
578 /* Set value-ranges of all SSA names defined by STMT to varying. */
580 static void
581 set_defs_to_varying (gimple *stmt)
583 ssa_op_iter i;
584 tree def;
585 FOR_EACH_SSA_TREE_OPERAND (def, stmt, i, SSA_OP_DEF)
587 value_range *vr = get_value_range (def);
588 /* Avoid writing to vr_const_varying get_value_range may return. */
589 if (vr->type != VR_VARYING)
590 set_value_range_to_varying (vr);
595 /* Return true, if VAL1 and VAL2 are equal values for VRP purposes. */
597 static inline bool
598 vrp_operand_equal_p (const_tree val1, const_tree val2)
600 if (val1 == val2)
601 return true;
602 if (!val1 || !val2 || !operand_equal_p (val1, val2, 0))
603 return false;
604 return true;
607 /* Return true, if the bitmaps B1 and B2 are equal. */
609 static inline bool
610 vrp_bitmap_equal_p (const_bitmap b1, const_bitmap b2)
612 return (b1 == b2
613 || ((!b1 || bitmap_empty_p (b1))
614 && (!b2 || bitmap_empty_p (b2)))
615 || (b1 && b2
616 && bitmap_equal_p (b1, b2)));
619 /* Update the value range and equivalence set for variable VAR to
620 NEW_VR. Return true if NEW_VR is different from VAR's previous
621 value.
623 NOTE: This function assumes that NEW_VR is a temporary value range
624 object created for the sole purpose of updating VAR's range. The
625 storage used by the equivalence set from NEW_VR will be freed by
626 this function. Do not call update_value_range when NEW_VR
627 is the range object associated with another SSA name. */
629 static inline bool
630 update_value_range (const_tree var, value_range *new_vr)
632 value_range *old_vr;
633 bool is_new;
635 /* If there is a value-range on the SSA name from earlier analysis
636 factor that in. */
637 if (INTEGRAL_TYPE_P (TREE_TYPE (var)))
639 wide_int min, max;
640 value_range_type rtype = get_range_info (var, &min, &max);
641 if (rtype == VR_RANGE || rtype == VR_ANTI_RANGE)
643 tree nr_min, nr_max;
644 nr_min = wide_int_to_tree (TREE_TYPE (var), min);
645 nr_max = wide_int_to_tree (TREE_TYPE (var), max);
646 value_range nr = VR_INITIALIZER;
647 set_and_canonicalize_value_range (&nr, rtype, nr_min, nr_max, NULL);
648 vrp_intersect_ranges (new_vr, &nr);
652 /* Update the value range, if necessary. */
653 old_vr = get_value_range (var);
654 is_new = old_vr->type != new_vr->type
655 || !vrp_operand_equal_p (old_vr->min, new_vr->min)
656 || !vrp_operand_equal_p (old_vr->max, new_vr->max)
657 || !vrp_bitmap_equal_p (old_vr->equiv, new_vr->equiv);
659 if (is_new)
661 /* Do not allow transitions up the lattice. The following
662 is slightly more awkward than just new_vr->type < old_vr->type
663 because VR_RANGE and VR_ANTI_RANGE need to be considered
664 the same. We may not have is_new when transitioning to
665 UNDEFINED. If old_vr->type is VARYING, we shouldn't be
666 called. */
667 if (new_vr->type == VR_UNDEFINED)
669 BITMAP_FREE (new_vr->equiv);
670 set_value_range_to_varying (old_vr);
671 set_value_range_to_varying (new_vr);
672 return true;
674 else
675 set_value_range (old_vr, new_vr->type, new_vr->min, new_vr->max,
676 new_vr->equiv);
679 BITMAP_FREE (new_vr->equiv);
681 return is_new;
685 /* Add VAR and VAR's equivalence set to EQUIV. This is the central
686 point where equivalence processing can be turned on/off. */
688 static void
689 add_equivalence (bitmap *equiv, const_tree var)
691 unsigned ver = SSA_NAME_VERSION (var);
692 value_range *vr = get_value_range (var);
694 if (*equiv == NULL)
695 *equiv = BITMAP_ALLOC (&vrp_equiv_obstack);
696 bitmap_set_bit (*equiv, ver);
697 if (vr && vr->equiv)
698 bitmap_ior_into (*equiv, vr->equiv);
702 /* Return true if VR is ~[0, 0]. */
704 static inline bool
705 range_is_nonnull (value_range *vr)
707 return vr->type == VR_ANTI_RANGE
708 && integer_zerop (vr->min)
709 && integer_zerop (vr->max);
713 /* Return true if VR is [0, 0]. */
715 static inline bool
716 range_is_null (value_range *vr)
718 return vr->type == VR_RANGE
719 && integer_zerop (vr->min)
720 && integer_zerop (vr->max);
723 /* Return true if max and min of VR are INTEGER_CST. It's not necessary
724 a singleton. */
726 static inline bool
727 range_int_cst_p (value_range *vr)
729 return (vr->type == VR_RANGE
730 && TREE_CODE (vr->max) == INTEGER_CST
731 && TREE_CODE (vr->min) == INTEGER_CST);
734 /* Return true if VR is a INTEGER_CST singleton. */
736 static inline bool
737 range_int_cst_singleton_p (value_range *vr)
739 return (range_int_cst_p (vr)
740 && tree_int_cst_equal (vr->min, vr->max));
743 /* Return true if value range VR involves at least one symbol. */
745 static inline bool
746 symbolic_range_p (value_range *vr)
748 return (!is_gimple_min_invariant (vr->min)
749 || !is_gimple_min_invariant (vr->max));
752 /* Return the single symbol (an SSA_NAME) contained in T if any, or NULL_TREE
753 otherwise. We only handle additive operations and set NEG to true if the
754 symbol is negated and INV to the invariant part, if any. */
756 static tree
757 get_single_symbol (tree t, bool *neg, tree *inv)
759 bool neg_;
760 tree inv_;
762 *inv = NULL_TREE;
763 *neg = false;
765 if (TREE_CODE (t) == PLUS_EXPR
766 || TREE_CODE (t) == POINTER_PLUS_EXPR
767 || TREE_CODE (t) == MINUS_EXPR)
769 if (is_gimple_min_invariant (TREE_OPERAND (t, 0)))
771 neg_ = (TREE_CODE (t) == MINUS_EXPR);
772 inv_ = TREE_OPERAND (t, 0);
773 t = TREE_OPERAND (t, 1);
775 else if (is_gimple_min_invariant (TREE_OPERAND (t, 1)))
777 neg_ = false;
778 inv_ = TREE_OPERAND (t, 1);
779 t = TREE_OPERAND (t, 0);
781 else
782 return NULL_TREE;
784 else
786 neg_ = false;
787 inv_ = NULL_TREE;
790 if (TREE_CODE (t) == NEGATE_EXPR)
792 t = TREE_OPERAND (t, 0);
793 neg_ = !neg_;
796 if (TREE_CODE (t) != SSA_NAME)
797 return NULL_TREE;
799 gcc_assert (! inv_ || ! TREE_OVERFLOW_P (inv_));
801 *neg = neg_;
802 *inv = inv_;
803 return t;
806 /* The reverse operation: build a symbolic expression with TYPE
807 from symbol SYM, negated according to NEG, and invariant INV. */
809 static tree
810 build_symbolic_expr (tree type, tree sym, bool neg, tree inv)
812 const bool pointer_p = POINTER_TYPE_P (type);
813 tree t = sym;
815 if (neg)
816 t = build1 (NEGATE_EXPR, type, t);
818 if (integer_zerop (inv))
819 return t;
821 return build2 (pointer_p ? POINTER_PLUS_EXPR : PLUS_EXPR, type, t, inv);
824 /* Return true if value range VR involves exactly one symbol SYM. */
826 static bool
827 symbolic_range_based_on_p (value_range *vr, const_tree sym)
829 bool neg, min_has_symbol, max_has_symbol;
830 tree inv;
832 if (is_gimple_min_invariant (vr->min))
833 min_has_symbol = false;
834 else if (get_single_symbol (vr->min, &neg, &inv) == sym)
835 min_has_symbol = true;
836 else
837 return false;
839 if (is_gimple_min_invariant (vr->max))
840 max_has_symbol = false;
841 else if (get_single_symbol (vr->max, &neg, &inv) == sym)
842 max_has_symbol = true;
843 else
844 return false;
846 return (min_has_symbol || max_has_symbol);
849 /* Return true if the result of assignment STMT is know to be non-zero. */
851 static bool
852 gimple_assign_nonzero_p (gimple *stmt)
854 enum tree_code code = gimple_assign_rhs_code (stmt);
855 bool strict_overflow_p;
856 switch (get_gimple_rhs_class (code))
858 case GIMPLE_UNARY_RHS:
859 return tree_unary_nonzero_warnv_p (gimple_assign_rhs_code (stmt),
860 gimple_expr_type (stmt),
861 gimple_assign_rhs1 (stmt),
862 &strict_overflow_p);
863 case GIMPLE_BINARY_RHS:
864 return tree_binary_nonzero_warnv_p (gimple_assign_rhs_code (stmt),
865 gimple_expr_type (stmt),
866 gimple_assign_rhs1 (stmt),
867 gimple_assign_rhs2 (stmt),
868 &strict_overflow_p);
869 case GIMPLE_TERNARY_RHS:
870 return false;
871 case GIMPLE_SINGLE_RHS:
872 return tree_single_nonzero_warnv_p (gimple_assign_rhs1 (stmt),
873 &strict_overflow_p);
874 case GIMPLE_INVALID_RHS:
875 gcc_unreachable ();
876 default:
877 gcc_unreachable ();
881 /* Return true if STMT is known to compute a non-zero value. */
883 static bool
884 gimple_stmt_nonzero_p (gimple *stmt)
886 switch (gimple_code (stmt))
888 case GIMPLE_ASSIGN:
889 return gimple_assign_nonzero_p (stmt);
890 case GIMPLE_CALL:
892 tree fndecl = gimple_call_fndecl (stmt);
893 if (!fndecl) return false;
894 if (flag_delete_null_pointer_checks && !flag_check_new
895 && DECL_IS_OPERATOR_NEW (fndecl)
896 && !TREE_NOTHROW (fndecl))
897 return true;
898 /* References are always non-NULL. */
899 if (flag_delete_null_pointer_checks
900 && TREE_CODE (TREE_TYPE (fndecl)) == REFERENCE_TYPE)
901 return true;
902 if (flag_delete_null_pointer_checks &&
903 lookup_attribute ("returns_nonnull",
904 TYPE_ATTRIBUTES (gimple_call_fntype (stmt))))
905 return true;
907 gcall *call_stmt = as_a<gcall *> (stmt);
908 unsigned rf = gimple_call_return_flags (call_stmt);
909 if (rf & ERF_RETURNS_ARG)
911 unsigned argnum = rf & ERF_RETURN_ARG_MASK;
912 if (argnum < gimple_call_num_args (call_stmt))
914 tree arg = gimple_call_arg (call_stmt, argnum);
915 if (SSA_VAR_P (arg)
916 && infer_nonnull_range_by_attribute (stmt, arg))
917 return true;
920 return gimple_alloca_call_p (stmt);
922 default:
923 gcc_unreachable ();
927 /* Like tree_expr_nonzero_p, but this function uses value ranges
928 obtained so far. */
930 static bool
931 vrp_stmt_computes_nonzero (gimple *stmt)
933 if (gimple_stmt_nonzero_p (stmt))
934 return true;
936 /* If we have an expression of the form &X->a, then the expression
937 is nonnull if X is nonnull. */
938 if (is_gimple_assign (stmt)
939 && gimple_assign_rhs_code (stmt) == ADDR_EXPR)
941 tree expr = gimple_assign_rhs1 (stmt);
942 tree base = get_base_address (TREE_OPERAND (expr, 0));
944 if (base != NULL_TREE
945 && TREE_CODE (base) == MEM_REF
946 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
948 value_range *vr = get_value_range (TREE_OPERAND (base, 0));
949 if (range_is_nonnull (vr))
950 return true;
954 return false;
957 /* Returns true if EXPR is a valid value (as expected by compare_values) --
958 a gimple invariant, or SSA_NAME +- CST. */
960 static bool
961 valid_value_p (tree expr)
963 if (TREE_CODE (expr) == SSA_NAME)
964 return true;
966 if (TREE_CODE (expr) == PLUS_EXPR
967 || TREE_CODE (expr) == MINUS_EXPR)
968 return (TREE_CODE (TREE_OPERAND (expr, 0)) == SSA_NAME
969 && TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST);
971 return is_gimple_min_invariant (expr);
974 /* Return
975 1 if VAL < VAL2
976 0 if !(VAL < VAL2)
977 -2 if those are incomparable. */
978 static inline int
979 operand_less_p (tree val, tree val2)
981 /* LT is folded faster than GE and others. Inline the common case. */
982 if (TREE_CODE (val) == INTEGER_CST && TREE_CODE (val2) == INTEGER_CST)
983 return tree_int_cst_lt (val, val2);
984 else
986 tree tcmp;
988 fold_defer_overflow_warnings ();
990 tcmp = fold_binary_to_constant (LT_EXPR, boolean_type_node, val, val2);
992 fold_undefer_and_ignore_overflow_warnings ();
994 if (!tcmp
995 || TREE_CODE (tcmp) != INTEGER_CST)
996 return -2;
998 if (!integer_zerop (tcmp))
999 return 1;
1002 return 0;
1005 /* Compare two values VAL1 and VAL2. Return
1007 -2 if VAL1 and VAL2 cannot be compared at compile-time,
1008 -1 if VAL1 < VAL2,
1009 0 if VAL1 == VAL2,
1010 +1 if VAL1 > VAL2, and
1011 +2 if VAL1 != VAL2
1013 This is similar to tree_int_cst_compare but supports pointer values
1014 and values that cannot be compared at compile time.
1016 If STRICT_OVERFLOW_P is not NULL, then set *STRICT_OVERFLOW_P to
1017 true if the return value is only valid if we assume that signed
1018 overflow is undefined. */
1020 static int
1021 compare_values_warnv (tree val1, tree val2, bool *strict_overflow_p)
1023 if (val1 == val2)
1024 return 0;
1026 /* Below we rely on the fact that VAL1 and VAL2 are both pointers or
1027 both integers. */
1028 gcc_assert (POINTER_TYPE_P (TREE_TYPE (val1))
1029 == POINTER_TYPE_P (TREE_TYPE (val2)));
1031 /* Convert the two values into the same type. This is needed because
1032 sizetype causes sign extension even for unsigned types. */
1033 val2 = fold_convert (TREE_TYPE (val1), val2);
1034 STRIP_USELESS_TYPE_CONVERSION (val2);
1036 const bool overflow_undefined
1037 = INTEGRAL_TYPE_P (TREE_TYPE (val1))
1038 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (val1));
1039 tree inv1, inv2;
1040 bool neg1, neg2;
1041 tree sym1 = get_single_symbol (val1, &neg1, &inv1);
1042 tree sym2 = get_single_symbol (val2, &neg2, &inv2);
1044 /* If VAL1 and VAL2 are of the form '[-]NAME [+ CST]', return -1 or +1
1045 accordingly. If VAL1 and VAL2 don't use the same name, return -2. */
1046 if (sym1 && sym2)
1048 /* Both values must use the same name with the same sign. */
1049 if (sym1 != sym2 || neg1 != neg2)
1050 return -2;
1052 /* [-]NAME + CST == [-]NAME + CST. */
1053 if (inv1 == inv2)
1054 return 0;
1056 /* If overflow is defined we cannot simplify more. */
1057 if (!overflow_undefined)
1058 return -2;
1060 if (strict_overflow_p != NULL
1061 /* Symbolic range building sets TREE_NO_WARNING to declare
1062 that overflow doesn't happen. */
1063 && (!inv1 || !TREE_NO_WARNING (val1))
1064 && (!inv2 || !TREE_NO_WARNING (val2)))
1065 *strict_overflow_p = true;
1067 if (!inv1)
1068 inv1 = build_int_cst (TREE_TYPE (val1), 0);
1069 if (!inv2)
1070 inv2 = build_int_cst (TREE_TYPE (val2), 0);
1072 return wi::cmp (inv1, inv2, TYPE_SIGN (TREE_TYPE (val1)));
1075 const bool cst1 = is_gimple_min_invariant (val1);
1076 const bool cst2 = is_gimple_min_invariant (val2);
1078 /* If one is of the form '[-]NAME + CST' and the other is constant, then
1079 it might be possible to say something depending on the constants. */
1080 if ((sym1 && inv1 && cst2) || (sym2 && inv2 && cst1))
1082 if (!overflow_undefined)
1083 return -2;
1085 if (strict_overflow_p != NULL
1086 /* Symbolic range building sets TREE_NO_WARNING to declare
1087 that overflow doesn't happen. */
1088 && (!sym1 || !TREE_NO_WARNING (val1))
1089 && (!sym2 || !TREE_NO_WARNING (val2)))
1090 *strict_overflow_p = true;
1092 const signop sgn = TYPE_SIGN (TREE_TYPE (val1));
1093 tree cst = cst1 ? val1 : val2;
1094 tree inv = cst1 ? inv2 : inv1;
1096 /* Compute the difference between the constants. If it overflows or
1097 underflows, this means that we can trivially compare the NAME with
1098 it and, consequently, the two values with each other. */
1099 wide_int diff = wi::sub (cst, inv);
1100 if (wi::cmp (0, inv, sgn) != wi::cmp (diff, cst, sgn))
1102 const int res = wi::cmp (cst, inv, sgn);
1103 return cst1 ? res : -res;
1106 return -2;
1109 /* We cannot say anything more for non-constants. */
1110 if (!cst1 || !cst2)
1111 return -2;
1113 if (!POINTER_TYPE_P (TREE_TYPE (val1)))
1115 /* We cannot compare overflowed values. */
1116 if (TREE_OVERFLOW (val1) || TREE_OVERFLOW (val2))
1117 return -2;
1119 return tree_int_cst_compare (val1, val2);
1121 else
1123 tree t;
1125 /* First see if VAL1 and VAL2 are not the same. */
1126 if (val1 == val2 || operand_equal_p (val1, val2, 0))
1127 return 0;
1129 /* If VAL1 is a lower address than VAL2, return -1. */
1130 if (operand_less_p (val1, val2) == 1)
1131 return -1;
1133 /* If VAL1 is a higher address than VAL2, return +1. */
1134 if (operand_less_p (val2, val1) == 1)
1135 return 1;
1137 /* If VAL1 is different than VAL2, return +2.
1138 For integer constants we either have already returned -1 or 1
1139 or they are equivalent. We still might succeed in proving
1140 something about non-trivial operands. */
1141 if (TREE_CODE (val1) != INTEGER_CST
1142 || TREE_CODE (val2) != INTEGER_CST)
1144 t = fold_binary_to_constant (NE_EXPR, boolean_type_node, val1, val2);
1145 if (t && integer_onep (t))
1146 return 2;
1149 return -2;
1153 /* Compare values like compare_values_warnv. */
1155 static int
1156 compare_values (tree val1, tree val2)
1158 bool sop;
1159 return compare_values_warnv (val1, val2, &sop);
1163 /* Return 1 if VAL is inside value range MIN <= VAL <= MAX,
1164 0 if VAL is not inside [MIN, MAX],
1165 -2 if we cannot tell either way.
1167 Benchmark compile/20001226-1.c compilation time after changing this
1168 function. */
1170 static inline int
1171 value_inside_range (tree val, tree min, tree max)
1173 int cmp1, cmp2;
1175 cmp1 = operand_less_p (val, min);
1176 if (cmp1 == -2)
1177 return -2;
1178 if (cmp1 == 1)
1179 return 0;
1181 cmp2 = operand_less_p (max, val);
1182 if (cmp2 == -2)
1183 return -2;
1185 return !cmp2;
1189 /* Return true if value ranges VR0 and VR1 have a non-empty
1190 intersection.
1192 Benchmark compile/20001226-1.c compilation time after changing this
1193 function.
1196 static inline bool
1197 value_ranges_intersect_p (value_range *vr0, value_range *vr1)
1199 /* The value ranges do not intersect if the maximum of the first range is
1200 less than the minimum of the second range or vice versa.
1201 When those relations are unknown, we can't do any better. */
1202 if (operand_less_p (vr0->max, vr1->min) != 0)
1203 return false;
1204 if (operand_less_p (vr1->max, vr0->min) != 0)
1205 return false;
1206 return true;
1210 /* Return 1 if [MIN, MAX] includes the value zero, 0 if it does not
1211 include the value zero, -2 if we cannot tell. */
1213 static inline int
1214 range_includes_zero_p (tree min, tree max)
1216 tree zero = build_int_cst (TREE_TYPE (min), 0);
1217 return value_inside_range (zero, min, max);
1220 /* Return true if *VR is know to only contain nonnegative values. */
1222 static inline bool
1223 value_range_nonnegative_p (value_range *vr)
1225 /* Testing for VR_ANTI_RANGE is not useful here as any anti-range
1226 which would return a useful value should be encoded as a
1227 VR_RANGE. */
1228 if (vr->type == VR_RANGE)
1230 int result = compare_values (vr->min, integer_zero_node);
1231 return (result == 0 || result == 1);
1234 return false;
1237 /* If *VR has a value rante that is a single constant value return that,
1238 otherwise return NULL_TREE. */
1240 static tree
1241 value_range_constant_singleton (value_range *vr)
1243 if (vr->type == VR_RANGE
1244 && vrp_operand_equal_p (vr->min, vr->max)
1245 && is_gimple_min_invariant (vr->min))
1246 return vr->min;
1248 return NULL_TREE;
1251 /* If OP has a value range with a single constant value return that,
1252 otherwise return NULL_TREE. This returns OP itself if OP is a
1253 constant. */
1255 static tree
1256 op_with_constant_singleton_value_range (tree op)
1258 if (is_gimple_min_invariant (op))
1259 return op;
1261 if (TREE_CODE (op) != SSA_NAME)
1262 return NULL_TREE;
1264 return value_range_constant_singleton (get_value_range (op));
1267 /* Return true if op is in a boolean [0, 1] value-range. */
1269 static bool
1270 op_with_boolean_value_range_p (tree op)
1272 value_range *vr;
1274 if (TYPE_PRECISION (TREE_TYPE (op)) == 1)
1275 return true;
1277 if (integer_zerop (op)
1278 || integer_onep (op))
1279 return true;
1281 if (TREE_CODE (op) != SSA_NAME)
1282 return false;
1284 vr = get_value_range (op);
1285 return (vr->type == VR_RANGE
1286 && integer_zerop (vr->min)
1287 && integer_onep (vr->max));
1290 /* Extract value range information for VAR when (OP COND_CODE LIMIT) is
1291 true and store it in *VR_P. */
1293 static void
1294 extract_range_for_var_from_comparison_expr (tree var, enum tree_code cond_code,
1295 tree op, tree limit,
1296 value_range *vr_p)
1298 tree min, max, type;
1299 value_range *limit_vr;
1300 type = TREE_TYPE (var);
1301 gcc_assert (limit != var);
1303 /* For pointer arithmetic, we only keep track of pointer equality
1304 and inequality. */
1305 if (POINTER_TYPE_P (type) && cond_code != NE_EXPR && cond_code != EQ_EXPR)
1307 set_value_range_to_varying (vr_p);
1308 return;
1311 /* If LIMIT is another SSA name and LIMIT has a range of its own,
1312 try to use LIMIT's range to avoid creating symbolic ranges
1313 unnecessarily. */
1314 limit_vr = (TREE_CODE (limit) == SSA_NAME) ? get_value_range (limit) : NULL;
1316 /* LIMIT's range is only interesting if it has any useful information. */
1317 if (! limit_vr
1318 || limit_vr->type == VR_UNDEFINED
1319 || limit_vr->type == VR_VARYING
1320 || (symbolic_range_p (limit_vr)
1321 && ! (limit_vr->type == VR_RANGE
1322 && (limit_vr->min == limit_vr->max
1323 || operand_equal_p (limit_vr->min, limit_vr->max, 0)))))
1324 limit_vr = NULL;
1326 /* Initially, the new range has the same set of equivalences of
1327 VAR's range. This will be revised before returning the final
1328 value. Since assertions may be chained via mutually exclusive
1329 predicates, we will need to trim the set of equivalences before
1330 we are done. */
1331 gcc_assert (vr_p->equiv == NULL);
1332 add_equivalence (&vr_p->equiv, var);
1334 /* Extract a new range based on the asserted comparison for VAR and
1335 LIMIT's value range. Notice that if LIMIT has an anti-range, we
1336 will only use it for equality comparisons (EQ_EXPR). For any
1337 other kind of assertion, we cannot derive a range from LIMIT's
1338 anti-range that can be used to describe the new range. For
1339 instance, ASSERT_EXPR <x_2, x_2 <= b_4>. If b_4 is ~[2, 10],
1340 then b_4 takes on the ranges [-INF, 1] and [11, +INF]. There is
1341 no single range for x_2 that could describe LE_EXPR, so we might
1342 as well build the range [b_4, +INF] for it.
1343 One special case we handle is extracting a range from a
1344 range test encoded as (unsigned)var + CST <= limit. */
1345 if (TREE_CODE (op) == NOP_EXPR
1346 || TREE_CODE (op) == PLUS_EXPR)
1348 if (TREE_CODE (op) == PLUS_EXPR)
1350 min = fold_build1 (NEGATE_EXPR, TREE_TYPE (TREE_OPERAND (op, 1)),
1351 TREE_OPERAND (op, 1));
1352 max = int_const_binop (PLUS_EXPR, limit, min);
1353 op = TREE_OPERAND (op, 0);
1355 else
1357 min = build_int_cst (TREE_TYPE (var), 0);
1358 max = limit;
1361 /* Make sure to not set TREE_OVERFLOW on the final type
1362 conversion. We are willingly interpreting large positive
1363 unsigned values as negative signed values here. */
1364 min = force_fit_type (TREE_TYPE (var), wi::to_widest (min), 0, false);
1365 max = force_fit_type (TREE_TYPE (var), wi::to_widest (max), 0, false);
1367 /* We can transform a max, min range to an anti-range or
1368 vice-versa. Use set_and_canonicalize_value_range which does
1369 this for us. */
1370 if (cond_code == LE_EXPR)
1371 set_and_canonicalize_value_range (vr_p, VR_RANGE,
1372 min, max, vr_p->equiv);
1373 else if (cond_code == GT_EXPR)
1374 set_and_canonicalize_value_range (vr_p, VR_ANTI_RANGE,
1375 min, max, vr_p->equiv);
1376 else
1377 gcc_unreachable ();
1379 else if (cond_code == EQ_EXPR)
1381 enum value_range_type range_type;
1383 if (limit_vr)
1385 range_type = limit_vr->type;
1386 min = limit_vr->min;
1387 max = limit_vr->max;
1389 else
1391 range_type = VR_RANGE;
1392 min = limit;
1393 max = limit;
1396 set_value_range (vr_p, range_type, min, max, vr_p->equiv);
1398 /* When asserting the equality VAR == LIMIT and LIMIT is another
1399 SSA name, the new range will also inherit the equivalence set
1400 from LIMIT. */
1401 if (TREE_CODE (limit) == SSA_NAME)
1402 add_equivalence (&vr_p->equiv, limit);
1404 else if (cond_code == NE_EXPR)
1406 /* As described above, when LIMIT's range is an anti-range and
1407 this assertion is an inequality (NE_EXPR), then we cannot
1408 derive anything from the anti-range. For instance, if
1409 LIMIT's range was ~[0, 0], the assertion 'VAR != LIMIT' does
1410 not imply that VAR's range is [0, 0]. So, in the case of
1411 anti-ranges, we just assert the inequality using LIMIT and
1412 not its anti-range.
1414 If LIMIT_VR is a range, we can only use it to build a new
1415 anti-range if LIMIT_VR is a single-valued range. For
1416 instance, if LIMIT_VR is [0, 1], the predicate
1417 VAR != [0, 1] does not mean that VAR's range is ~[0, 1].
1418 Rather, it means that for value 0 VAR should be ~[0, 0]
1419 and for value 1, VAR should be ~[1, 1]. We cannot
1420 represent these ranges.
1422 The only situation in which we can build a valid
1423 anti-range is when LIMIT_VR is a single-valued range
1424 (i.e., LIMIT_VR->MIN == LIMIT_VR->MAX). In that case,
1425 build the anti-range ~[LIMIT_VR->MIN, LIMIT_VR->MAX]. */
1426 if (limit_vr
1427 && limit_vr->type == VR_RANGE
1428 && compare_values (limit_vr->min, limit_vr->max) == 0)
1430 min = limit_vr->min;
1431 max = limit_vr->max;
1433 else
1435 /* In any other case, we cannot use LIMIT's range to build a
1436 valid anti-range. */
1437 min = max = limit;
1440 /* If MIN and MAX cover the whole range for their type, then
1441 just use the original LIMIT. */
1442 if (INTEGRAL_TYPE_P (type)
1443 && vrp_val_is_min (min)
1444 && vrp_val_is_max (max))
1445 min = max = limit;
1447 set_and_canonicalize_value_range (vr_p, VR_ANTI_RANGE,
1448 min, max, vr_p->equiv);
1450 else if (cond_code == LE_EXPR || cond_code == LT_EXPR)
1452 min = TYPE_MIN_VALUE (type);
1454 if (limit_vr == NULL || limit_vr->type == VR_ANTI_RANGE)
1455 max = limit;
1456 else
1458 /* If LIMIT_VR is of the form [N1, N2], we need to build the
1459 range [MIN, N2] for LE_EXPR and [MIN, N2 - 1] for
1460 LT_EXPR. */
1461 max = limit_vr->max;
1464 /* If the maximum value forces us to be out of bounds, simply punt.
1465 It would be pointless to try and do anything more since this
1466 all should be optimized away above us. */
1467 if (cond_code == LT_EXPR
1468 && compare_values (max, min) == 0)
1469 set_value_range_to_varying (vr_p);
1470 else
1472 /* For LT_EXPR, we create the range [MIN, MAX - 1]. */
1473 if (cond_code == LT_EXPR)
1475 if (TYPE_PRECISION (TREE_TYPE (max)) == 1
1476 && !TYPE_UNSIGNED (TREE_TYPE (max)))
1477 max = fold_build2 (PLUS_EXPR, TREE_TYPE (max), max,
1478 build_int_cst (TREE_TYPE (max), -1));
1479 else
1480 max = fold_build2 (MINUS_EXPR, TREE_TYPE (max), max,
1481 build_int_cst (TREE_TYPE (max), 1));
1482 /* Signal to compare_values_warnv this expr doesn't overflow. */
1483 if (EXPR_P (max))
1484 TREE_NO_WARNING (max) = 1;
1487 set_value_range (vr_p, VR_RANGE, min, max, vr_p->equiv);
1490 else if (cond_code == GE_EXPR || cond_code == GT_EXPR)
1492 max = TYPE_MAX_VALUE (type);
1494 if (limit_vr == NULL || limit_vr->type == VR_ANTI_RANGE)
1495 min = limit;
1496 else
1498 /* If LIMIT_VR is of the form [N1, N2], we need to build the
1499 range [N1, MAX] for GE_EXPR and [N1 + 1, MAX] for
1500 GT_EXPR. */
1501 min = limit_vr->min;
1504 /* If the minimum value forces us to be out of bounds, simply punt.
1505 It would be pointless to try and do anything more since this
1506 all should be optimized away above us. */
1507 if (cond_code == GT_EXPR
1508 && compare_values (min, max) == 0)
1509 set_value_range_to_varying (vr_p);
1510 else
1512 /* For GT_EXPR, we create the range [MIN + 1, MAX]. */
1513 if (cond_code == GT_EXPR)
1515 if (TYPE_PRECISION (TREE_TYPE (min)) == 1
1516 && !TYPE_UNSIGNED (TREE_TYPE (min)))
1517 min = fold_build2 (MINUS_EXPR, TREE_TYPE (min), min,
1518 build_int_cst (TREE_TYPE (min), -1));
1519 else
1520 min = fold_build2 (PLUS_EXPR, TREE_TYPE (min), min,
1521 build_int_cst (TREE_TYPE (min), 1));
1522 /* Signal to compare_values_warnv this expr doesn't overflow. */
1523 if (EXPR_P (min))
1524 TREE_NO_WARNING (min) = 1;
1527 set_value_range (vr_p, VR_RANGE, min, max, vr_p->equiv);
1530 else
1531 gcc_unreachable ();
1533 /* Finally intersect the new range with what we already know about var. */
1534 vrp_intersect_ranges (vr_p, get_value_range (var));
1537 /* Extract value range information from an ASSERT_EXPR EXPR and store
1538 it in *VR_P. */
1540 static void
1541 extract_range_from_assert (value_range *vr_p, tree expr)
1543 tree var = ASSERT_EXPR_VAR (expr);
1544 tree cond = ASSERT_EXPR_COND (expr);
1545 tree limit, op;
1546 enum tree_code cond_code;
1547 gcc_assert (COMPARISON_CLASS_P (cond));
1549 /* Find VAR in the ASSERT_EXPR conditional. */
1550 if (var == TREE_OPERAND (cond, 0)
1551 || TREE_CODE (TREE_OPERAND (cond, 0)) == PLUS_EXPR
1552 || TREE_CODE (TREE_OPERAND (cond, 0)) == NOP_EXPR)
1554 /* If the predicate is of the form VAR COMP LIMIT, then we just
1555 take LIMIT from the RHS and use the same comparison code. */
1556 cond_code = TREE_CODE (cond);
1557 limit = TREE_OPERAND (cond, 1);
1558 op = TREE_OPERAND (cond, 0);
1560 else
1562 /* If the predicate is of the form LIMIT COMP VAR, then we need
1563 to flip around the comparison code to create the proper range
1564 for VAR. */
1565 cond_code = swap_tree_comparison (TREE_CODE (cond));
1566 limit = TREE_OPERAND (cond, 0);
1567 op = TREE_OPERAND (cond, 1);
1569 extract_range_for_var_from_comparison_expr (var, cond_code, op,
1570 limit, vr_p);
1573 /* Extract range information from SSA name VAR and store it in VR. If
1574 VAR has an interesting range, use it. Otherwise, create the
1575 range [VAR, VAR] and return it. This is useful in situations where
1576 we may have conditionals testing values of VARYING names. For
1577 instance,
1579 x_3 = y_5;
1580 if (x_3 > y_5)
1583 Even if y_5 is deemed VARYING, we can determine that x_3 > y_5 is
1584 always false. */
1586 static void
1587 extract_range_from_ssa_name (value_range *vr, tree var)
1589 value_range *var_vr = get_value_range (var);
1591 if (var_vr->type != VR_VARYING)
1592 copy_value_range (vr, var_vr);
1593 else
1594 set_value_range (vr, VR_RANGE, var, var, NULL);
1596 add_equivalence (&vr->equiv, var);
1600 /* Wrapper around int_const_binop. If the operation overflows and
1601 overflow is undefined, then adjust the result to be
1602 -INF or +INF depending on CODE, VAL1 and VAL2. Sets *OVERFLOW_P
1603 to whether the operation overflowed. For division by zero
1604 the result is indeterminate but *OVERFLOW_P is set. */
1606 static wide_int
1607 vrp_int_const_binop (enum tree_code code, tree val1, tree val2,
1608 bool *overflow_p)
1610 bool overflow = false;
1611 signop sign = TYPE_SIGN (TREE_TYPE (val1));
1612 wide_int res;
1614 switch (code)
1616 case RSHIFT_EXPR:
1617 case LSHIFT_EXPR:
1619 wide_int wval2 = wi::to_wide (val2, TYPE_PRECISION (TREE_TYPE (val1)));
1620 if (wi::neg_p (wval2))
1622 wval2 = -wval2;
1623 if (code == RSHIFT_EXPR)
1624 code = LSHIFT_EXPR;
1625 else
1626 code = RSHIFT_EXPR;
1629 if (code == RSHIFT_EXPR)
1630 /* It's unclear from the C standard whether shifts can overflow.
1631 The following code ignores overflow; perhaps a C standard
1632 interpretation ruling is needed. */
1633 res = wi::rshift (val1, wval2, sign);
1634 else
1635 res = wi::lshift (val1, wval2);
1636 break;
1639 case MULT_EXPR:
1640 res = wi::mul (val1, val2, sign, &overflow);
1641 break;
1643 case TRUNC_DIV_EXPR:
1644 case EXACT_DIV_EXPR:
1645 if (val2 == 0)
1647 *overflow_p = true;
1648 return res;
1650 else
1651 res = wi::div_trunc (val1, val2, sign, &overflow);
1652 break;
1654 case FLOOR_DIV_EXPR:
1655 if (val2 == 0)
1657 *overflow_p = true;
1658 return res;
1660 res = wi::div_floor (val1, val2, sign, &overflow);
1661 break;
1663 case CEIL_DIV_EXPR:
1664 if (val2 == 0)
1666 *overflow_p = true;
1667 return res;
1669 res = wi::div_ceil (val1, val2, sign, &overflow);
1670 break;
1672 case ROUND_DIV_EXPR:
1673 if (val2 == 0)
1675 *overflow_p = 0;
1676 return res;
1678 res = wi::div_round (val1, val2, sign, &overflow);
1679 break;
1681 default:
1682 gcc_unreachable ();
1685 *overflow_p = overflow;
1687 if (overflow
1688 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (val1)))
1690 /* If the operation overflowed return -INF or +INF depending
1691 on the operation and the combination of signs of the operands. */
1692 int sgn1 = tree_int_cst_sgn (val1);
1693 int sgn2 = tree_int_cst_sgn (val2);
1695 /* Notice that we only need to handle the restricted set of
1696 operations handled by extract_range_from_binary_expr.
1697 Among them, only multiplication, addition and subtraction
1698 can yield overflow without overflown operands because we
1699 are working with integral types only... except in the
1700 case VAL1 = -INF and VAL2 = -1 which overflows to +INF
1701 for division too. */
1703 /* For multiplication, the sign of the overflow is given
1704 by the comparison of the signs of the operands. */
1705 if ((code == MULT_EXPR && sgn1 == sgn2)
1706 /* For addition, the operands must be of the same sign
1707 to yield an overflow. Its sign is therefore that
1708 of one of the operands, for example the first. */
1709 || (code == PLUS_EXPR && sgn1 >= 0)
1710 /* For subtraction, operands must be of
1711 different signs to yield an overflow. Its sign is
1712 therefore that of the first operand or the opposite of
1713 that of the second operand. A first operand of 0 counts
1714 as positive here, for the corner case 0 - (-INF), which
1715 overflows, but must yield +INF. */
1716 || (code == MINUS_EXPR && sgn1 >= 0)
1717 /* For division, the only case is -INF / -1 = +INF. */
1718 || code == TRUNC_DIV_EXPR
1719 || code == FLOOR_DIV_EXPR
1720 || code == CEIL_DIV_EXPR
1721 || code == EXACT_DIV_EXPR
1722 || code == ROUND_DIV_EXPR)
1723 return wi::max_value (TYPE_PRECISION (TREE_TYPE (val1)),
1724 TYPE_SIGN (TREE_TYPE (val1)));
1725 else
1726 return wi::min_value (TYPE_PRECISION (TREE_TYPE (val1)),
1727 TYPE_SIGN (TREE_TYPE (val1)));
1730 return res;
1734 /* For range VR compute two wide_int bitmasks. In *MAY_BE_NONZERO
1735 bitmask if some bit is unset, it means for all numbers in the range
1736 the bit is 0, otherwise it might be 0 or 1. In *MUST_BE_NONZERO
1737 bitmask if some bit is set, it means for all numbers in the range
1738 the bit is 1, otherwise it might be 0 or 1. */
1740 static bool
1741 zero_nonzero_bits_from_vr (const tree expr_type,
1742 value_range *vr,
1743 wide_int *may_be_nonzero,
1744 wide_int *must_be_nonzero)
1746 *may_be_nonzero = wi::minus_one (TYPE_PRECISION (expr_type));
1747 *must_be_nonzero = wi::zero (TYPE_PRECISION (expr_type));
1748 if (!range_int_cst_p (vr))
1749 return false;
1751 if (range_int_cst_singleton_p (vr))
1753 *may_be_nonzero = vr->min;
1754 *must_be_nonzero = *may_be_nonzero;
1756 else if (tree_int_cst_sgn (vr->min) >= 0
1757 || tree_int_cst_sgn (vr->max) < 0)
1759 wide_int xor_mask = wi::bit_xor (vr->min, vr->max);
1760 *may_be_nonzero = wi::bit_or (vr->min, vr->max);
1761 *must_be_nonzero = wi::bit_and (vr->min, vr->max);
1762 if (xor_mask != 0)
1764 wide_int mask = wi::mask (wi::floor_log2 (xor_mask), false,
1765 may_be_nonzero->get_precision ());
1766 *may_be_nonzero = *may_be_nonzero | mask;
1767 *must_be_nonzero = must_be_nonzero->and_not (mask);
1771 return true;
1774 /* Create two value-ranges in *VR0 and *VR1 from the anti-range *AR
1775 so that *VR0 U *VR1 == *AR. Returns true if that is possible,
1776 false otherwise. If *AR can be represented with a single range
1777 *VR1 will be VR_UNDEFINED. */
1779 static bool
1780 ranges_from_anti_range (value_range *ar,
1781 value_range *vr0, value_range *vr1)
1783 tree type = TREE_TYPE (ar->min);
1785 vr0->type = VR_UNDEFINED;
1786 vr1->type = VR_UNDEFINED;
1788 if (ar->type != VR_ANTI_RANGE
1789 || TREE_CODE (ar->min) != INTEGER_CST
1790 || TREE_CODE (ar->max) != INTEGER_CST
1791 || !vrp_val_min (type)
1792 || !vrp_val_max (type))
1793 return false;
1795 if (!vrp_val_is_min (ar->min))
1797 vr0->type = VR_RANGE;
1798 vr0->min = vrp_val_min (type);
1799 vr0->max = wide_int_to_tree (type, wi::sub (ar->min, 1));
1801 if (!vrp_val_is_max (ar->max))
1803 vr1->type = VR_RANGE;
1804 vr1->min = wide_int_to_tree (type, wi::add (ar->max, 1));
1805 vr1->max = vrp_val_max (type);
1807 if (vr0->type == VR_UNDEFINED)
1809 *vr0 = *vr1;
1810 vr1->type = VR_UNDEFINED;
1813 return vr0->type != VR_UNDEFINED;
1816 /* Helper to extract a value-range *VR for a multiplicative operation
1817 *VR0 CODE *VR1. */
1819 static void
1820 extract_range_from_multiplicative_op_1 (value_range *vr,
1821 enum tree_code code,
1822 value_range *vr0, value_range *vr1)
1824 enum value_range_type rtype;
1825 wide_int val, min, max;
1826 bool sop;
1827 tree type;
1829 /* Multiplications, divisions and shifts are a bit tricky to handle,
1830 depending on the mix of signs we have in the two ranges, we
1831 need to operate on different values to get the minimum and
1832 maximum values for the new range. One approach is to figure
1833 out all the variations of range combinations and do the
1834 operations.
1836 However, this involves several calls to compare_values and it
1837 is pretty convoluted. It's simpler to do the 4 operations
1838 (MIN0 OP MIN1, MIN0 OP MAX1, MAX0 OP MIN1 and MAX0 OP MAX0 OP
1839 MAX1) and then figure the smallest and largest values to form
1840 the new range. */
1841 gcc_assert (code == MULT_EXPR
1842 || code == TRUNC_DIV_EXPR
1843 || code == FLOOR_DIV_EXPR
1844 || code == CEIL_DIV_EXPR
1845 || code == EXACT_DIV_EXPR
1846 || code == ROUND_DIV_EXPR
1847 || code == RSHIFT_EXPR
1848 || code == LSHIFT_EXPR);
1849 gcc_assert ((vr0->type == VR_RANGE
1850 || (code == MULT_EXPR && vr0->type == VR_ANTI_RANGE))
1851 && vr0->type == vr1->type);
1853 rtype = vr0->type;
1854 type = TREE_TYPE (vr0->min);
1855 signop sgn = TYPE_SIGN (type);
1857 /* Compute the 4 cross operations and their minimum and maximum value. */
1858 sop = false;
1859 val = vrp_int_const_binop (code, vr0->min, vr1->min, &sop);
1860 if (! sop)
1861 min = max = val;
1863 if (vr1->max == vr1->min)
1865 else if (! sop)
1867 val = vrp_int_const_binop (code, vr0->min, vr1->max, &sop);
1868 if (! sop)
1870 if (wi::lt_p (val, min, sgn))
1871 min = val;
1872 else if (wi::gt_p (val, max, sgn))
1873 max = val;
1877 if (vr0->max == vr0->min)
1879 else if (! sop)
1881 val = vrp_int_const_binop (code, vr0->max, vr1->min, &sop);
1882 if (! sop)
1884 if (wi::lt_p (val, min, sgn))
1885 min = val;
1886 else if (wi::gt_p (val, max, sgn))
1887 max = val;
1891 if (vr0->min == vr0->max || vr1->min == vr1->max)
1893 else if (! sop)
1895 val = vrp_int_const_binop (code, vr0->max, vr1->max, &sop);
1896 if (! sop)
1898 if (wi::lt_p (val, min, sgn))
1899 min = val;
1900 else if (wi::gt_p (val, max, sgn))
1901 max = val;
1905 /* If either operation overflowed, drop to VARYING. */
1906 if (sop)
1908 set_value_range_to_varying (vr);
1909 return;
1912 /* If the new range has its limits swapped around (MIN > MAX),
1913 then the operation caused one of them to wrap around, mark
1914 the new range VARYING. */
1915 if (wi::gt_p (min, max, sgn))
1917 set_value_range_to_varying (vr);
1918 return;
1921 /* We punt for [-INF, +INF].
1922 We learn nothing when we have INF on both sides.
1923 Note that we do accept [-INF, -INF] and [+INF, +INF]. */
1924 if (wi::eq_p (min, wi::min_value (TYPE_PRECISION (type), sgn))
1925 && wi::eq_p (max, wi::max_value (TYPE_PRECISION (type), sgn)))
1927 set_value_range_to_varying (vr);
1928 return;
1931 set_value_range (vr, rtype,
1932 wide_int_to_tree (type, min),
1933 wide_int_to_tree (type, max), NULL);
1936 /* Extract range information from a binary operation CODE based on
1937 the ranges of each of its operands *VR0 and *VR1 with resulting
1938 type EXPR_TYPE. The resulting range is stored in *VR. */
1940 static void
1941 extract_range_from_binary_expr_1 (value_range *vr,
1942 enum tree_code code, tree expr_type,
1943 value_range *vr0_, value_range *vr1_)
1945 value_range vr0 = *vr0_, vr1 = *vr1_;
1946 value_range vrtem0 = VR_INITIALIZER, vrtem1 = VR_INITIALIZER;
1947 enum value_range_type type;
1948 tree min = NULL_TREE, max = NULL_TREE;
1949 int cmp;
1951 if (!INTEGRAL_TYPE_P (expr_type)
1952 && !POINTER_TYPE_P (expr_type))
1954 set_value_range_to_varying (vr);
1955 return;
1958 /* Not all binary expressions can be applied to ranges in a
1959 meaningful way. Handle only arithmetic operations. */
1960 if (code != PLUS_EXPR
1961 && code != MINUS_EXPR
1962 && code != POINTER_PLUS_EXPR
1963 && code != MULT_EXPR
1964 && code != TRUNC_DIV_EXPR
1965 && code != FLOOR_DIV_EXPR
1966 && code != CEIL_DIV_EXPR
1967 && code != EXACT_DIV_EXPR
1968 && code != ROUND_DIV_EXPR
1969 && code != TRUNC_MOD_EXPR
1970 && code != RSHIFT_EXPR
1971 && code != LSHIFT_EXPR
1972 && code != MIN_EXPR
1973 && code != MAX_EXPR
1974 && code != BIT_AND_EXPR
1975 && code != BIT_IOR_EXPR
1976 && code != BIT_XOR_EXPR)
1978 set_value_range_to_varying (vr);
1979 return;
1982 /* If both ranges are UNDEFINED, so is the result. */
1983 if (vr0.type == VR_UNDEFINED && vr1.type == VR_UNDEFINED)
1985 set_value_range_to_undefined (vr);
1986 return;
1988 /* If one of the ranges is UNDEFINED drop it to VARYING for the following
1989 code. At some point we may want to special-case operations that
1990 have UNDEFINED result for all or some value-ranges of the not UNDEFINED
1991 operand. */
1992 else if (vr0.type == VR_UNDEFINED)
1993 set_value_range_to_varying (&vr0);
1994 else if (vr1.type == VR_UNDEFINED)
1995 set_value_range_to_varying (&vr1);
1997 /* We get imprecise results from ranges_from_anti_range when
1998 code is EXACT_DIV_EXPR. We could mask out bits in the resulting
1999 range, but then we also need to hack up vrp_meet. It's just
2000 easier to special case when vr0 is ~[0,0] for EXACT_DIV_EXPR. */
2001 if (code == EXACT_DIV_EXPR
2002 && vr0.type == VR_ANTI_RANGE
2003 && vr0.min == vr0.max
2004 && integer_zerop (vr0.min))
2006 set_value_range_to_nonnull (vr, expr_type);
2007 return;
2010 /* Now canonicalize anti-ranges to ranges when they are not symbolic
2011 and express ~[] op X as ([]' op X) U ([]'' op X). */
2012 if (vr0.type == VR_ANTI_RANGE
2013 && ranges_from_anti_range (&vr0, &vrtem0, &vrtem1))
2015 extract_range_from_binary_expr_1 (vr, code, expr_type, &vrtem0, vr1_);
2016 if (vrtem1.type != VR_UNDEFINED)
2018 value_range vrres = VR_INITIALIZER;
2019 extract_range_from_binary_expr_1 (&vrres, code, expr_type,
2020 &vrtem1, vr1_);
2021 vrp_meet (vr, &vrres);
2023 return;
2025 /* Likewise for X op ~[]. */
2026 if (vr1.type == VR_ANTI_RANGE
2027 && ranges_from_anti_range (&vr1, &vrtem0, &vrtem1))
2029 extract_range_from_binary_expr_1 (vr, code, expr_type, vr0_, &vrtem0);
2030 if (vrtem1.type != VR_UNDEFINED)
2032 value_range vrres = VR_INITIALIZER;
2033 extract_range_from_binary_expr_1 (&vrres, code, expr_type,
2034 vr0_, &vrtem1);
2035 vrp_meet (vr, &vrres);
2037 return;
2040 /* The type of the resulting value range defaults to VR0.TYPE. */
2041 type = vr0.type;
2043 /* Refuse to operate on VARYING ranges, ranges of different kinds
2044 and symbolic ranges. As an exception, we allow BIT_{AND,IOR}
2045 because we may be able to derive a useful range even if one of
2046 the operands is VR_VARYING or symbolic range. Similarly for
2047 divisions, MIN/MAX and PLUS/MINUS.
2049 TODO, we may be able to derive anti-ranges in some cases. */
2050 if (code != BIT_AND_EXPR
2051 && code != BIT_IOR_EXPR
2052 && code != TRUNC_DIV_EXPR
2053 && code != FLOOR_DIV_EXPR
2054 && code != CEIL_DIV_EXPR
2055 && code != EXACT_DIV_EXPR
2056 && code != ROUND_DIV_EXPR
2057 && code != TRUNC_MOD_EXPR
2058 && code != MIN_EXPR
2059 && code != MAX_EXPR
2060 && code != PLUS_EXPR
2061 && code != MINUS_EXPR
2062 && code != RSHIFT_EXPR
2063 && (vr0.type == VR_VARYING
2064 || vr1.type == VR_VARYING
2065 || vr0.type != vr1.type
2066 || symbolic_range_p (&vr0)
2067 || symbolic_range_p (&vr1)))
2069 set_value_range_to_varying (vr);
2070 return;
2073 /* Now evaluate the expression to determine the new range. */
2074 if (POINTER_TYPE_P (expr_type))
2076 if (code == MIN_EXPR || code == MAX_EXPR)
2078 /* For MIN/MAX expressions with pointers, we only care about
2079 nullness, if both are non null, then the result is nonnull.
2080 If both are null, then the result is null. Otherwise they
2081 are varying. */
2082 if (range_is_nonnull (&vr0) && range_is_nonnull (&vr1))
2083 set_value_range_to_nonnull (vr, expr_type);
2084 else if (range_is_null (&vr0) && range_is_null (&vr1))
2085 set_value_range_to_null (vr, expr_type);
2086 else
2087 set_value_range_to_varying (vr);
2089 else if (code == POINTER_PLUS_EXPR)
2091 /* For pointer types, we are really only interested in asserting
2092 whether the expression evaluates to non-NULL. */
2093 if (range_is_nonnull (&vr0) || range_is_nonnull (&vr1))
2094 set_value_range_to_nonnull (vr, expr_type);
2095 else if (range_is_null (&vr0) && range_is_null (&vr1))
2096 set_value_range_to_null (vr, expr_type);
2097 else
2098 set_value_range_to_varying (vr);
2100 else if (code == BIT_AND_EXPR)
2102 /* For pointer types, we are really only interested in asserting
2103 whether the expression evaluates to non-NULL. */
2104 if (range_is_nonnull (&vr0) && range_is_nonnull (&vr1))
2105 set_value_range_to_nonnull (vr, expr_type);
2106 else if (range_is_null (&vr0) || range_is_null (&vr1))
2107 set_value_range_to_null (vr, expr_type);
2108 else
2109 set_value_range_to_varying (vr);
2111 else
2112 set_value_range_to_varying (vr);
2114 return;
2117 /* For integer ranges, apply the operation to each end of the
2118 range and see what we end up with. */
2119 if (code == PLUS_EXPR || code == MINUS_EXPR)
2121 const bool minus_p = (code == MINUS_EXPR);
2122 tree min_op0 = vr0.min;
2123 tree min_op1 = minus_p ? vr1.max : vr1.min;
2124 tree max_op0 = vr0.max;
2125 tree max_op1 = minus_p ? vr1.min : vr1.max;
2126 tree sym_min_op0 = NULL_TREE;
2127 tree sym_min_op1 = NULL_TREE;
2128 tree sym_max_op0 = NULL_TREE;
2129 tree sym_max_op1 = NULL_TREE;
2130 bool neg_min_op0, neg_min_op1, neg_max_op0, neg_max_op1;
2132 /* If we have a PLUS or MINUS with two VR_RANGEs, either constant or
2133 single-symbolic ranges, try to compute the precise resulting range,
2134 but only if we know that this resulting range will also be constant
2135 or single-symbolic. */
2136 if (vr0.type == VR_RANGE && vr1.type == VR_RANGE
2137 && (TREE_CODE (min_op0) == INTEGER_CST
2138 || (sym_min_op0
2139 = get_single_symbol (min_op0, &neg_min_op0, &min_op0)))
2140 && (TREE_CODE (min_op1) == INTEGER_CST
2141 || (sym_min_op1
2142 = get_single_symbol (min_op1, &neg_min_op1, &min_op1)))
2143 && (!(sym_min_op0 && sym_min_op1)
2144 || (sym_min_op0 == sym_min_op1
2145 && neg_min_op0 == (minus_p ? neg_min_op1 : !neg_min_op1)))
2146 && (TREE_CODE (max_op0) == INTEGER_CST
2147 || (sym_max_op0
2148 = get_single_symbol (max_op0, &neg_max_op0, &max_op0)))
2149 && (TREE_CODE (max_op1) == INTEGER_CST
2150 || (sym_max_op1
2151 = get_single_symbol (max_op1, &neg_max_op1, &max_op1)))
2152 && (!(sym_max_op0 && sym_max_op1)
2153 || (sym_max_op0 == sym_max_op1
2154 && neg_max_op0 == (minus_p ? neg_max_op1 : !neg_max_op1))))
2156 const signop sgn = TYPE_SIGN (expr_type);
2157 const unsigned int prec = TYPE_PRECISION (expr_type);
2158 wide_int type_min, type_max, wmin, wmax;
2159 int min_ovf = 0;
2160 int max_ovf = 0;
2162 /* Get the lower and upper bounds of the type. */
2163 if (TYPE_OVERFLOW_WRAPS (expr_type))
2165 type_min = wi::min_value (prec, sgn);
2166 type_max = wi::max_value (prec, sgn);
2168 else
2170 type_min = vrp_val_min (expr_type);
2171 type_max = vrp_val_max (expr_type);
2174 /* Combine the lower bounds, if any. */
2175 if (min_op0 && min_op1)
2177 if (minus_p)
2179 wmin = wi::sub (min_op0, min_op1);
2181 /* Check for overflow. */
2182 if (wi::cmp (0, min_op1, sgn)
2183 != wi::cmp (wmin, min_op0, sgn))
2184 min_ovf = wi::cmp (min_op0, min_op1, sgn);
2186 else
2188 wmin = wi::add (min_op0, min_op1);
2190 /* Check for overflow. */
2191 if (wi::cmp (min_op1, 0, sgn)
2192 != wi::cmp (wmin, min_op0, sgn))
2193 min_ovf = wi::cmp (min_op0, wmin, sgn);
2196 else if (min_op0)
2197 wmin = min_op0;
2198 else if (min_op1)
2200 if (minus_p)
2202 wmin = wi::neg (min_op1);
2204 /* Check for overflow. */
2205 if (sgn == SIGNED && wi::neg_p (min_op1) && wi::neg_p (wmin))
2206 min_ovf = 1;
2207 else if (sgn == UNSIGNED && wi::ne_p (min_op1, 0))
2208 min_ovf = -1;
2210 else
2211 wmin = min_op1;
2213 else
2214 wmin = wi::shwi (0, prec);
2216 /* Combine the upper bounds, if any. */
2217 if (max_op0 && max_op1)
2219 if (minus_p)
2221 wmax = wi::sub (max_op0, max_op1);
2223 /* Check for overflow. */
2224 if (wi::cmp (0, max_op1, sgn)
2225 != wi::cmp (wmax, max_op0, sgn))
2226 max_ovf = wi::cmp (max_op0, max_op1, sgn);
2228 else
2230 wmax = wi::add (max_op0, max_op1);
2232 if (wi::cmp (max_op1, 0, sgn)
2233 != wi::cmp (wmax, max_op0, sgn))
2234 max_ovf = wi::cmp (max_op0, wmax, sgn);
2237 else if (max_op0)
2238 wmax = max_op0;
2239 else if (max_op1)
2241 if (minus_p)
2243 wmax = wi::neg (max_op1);
2245 /* Check for overflow. */
2246 if (sgn == SIGNED && wi::neg_p (max_op1) && wi::neg_p (wmax))
2247 max_ovf = 1;
2248 else if (sgn == UNSIGNED && wi::ne_p (max_op1, 0))
2249 max_ovf = -1;
2251 else
2252 wmax = max_op1;
2254 else
2255 wmax = wi::shwi (0, prec);
2257 /* Check for type overflow. */
2258 if (min_ovf == 0)
2260 if (wi::cmp (wmin, type_min, sgn) == -1)
2261 min_ovf = -1;
2262 else if (wi::cmp (wmin, type_max, sgn) == 1)
2263 min_ovf = 1;
2265 if (max_ovf == 0)
2267 if (wi::cmp (wmax, type_min, sgn) == -1)
2268 max_ovf = -1;
2269 else if (wi::cmp (wmax, type_max, sgn) == 1)
2270 max_ovf = 1;
2273 /* If we have overflow for the constant part and the resulting
2274 range will be symbolic, drop to VR_VARYING. */
2275 if ((min_ovf && sym_min_op0 != sym_min_op1)
2276 || (max_ovf && sym_max_op0 != sym_max_op1))
2278 set_value_range_to_varying (vr);
2279 return;
2282 if (TYPE_OVERFLOW_WRAPS (expr_type))
2284 /* If overflow wraps, truncate the values and adjust the
2285 range kind and bounds appropriately. */
2286 wide_int tmin = wide_int::from (wmin, prec, sgn);
2287 wide_int tmax = wide_int::from (wmax, prec, sgn);
2288 if (min_ovf == max_ovf)
2290 /* No overflow or both overflow or underflow. The
2291 range kind stays VR_RANGE. */
2292 min = wide_int_to_tree (expr_type, tmin);
2293 max = wide_int_to_tree (expr_type, tmax);
2295 else if ((min_ovf == -1 && max_ovf == 0)
2296 || (max_ovf == 1 && min_ovf == 0))
2298 /* Min underflow or max overflow. The range kind
2299 changes to VR_ANTI_RANGE. */
2300 bool covers = false;
2301 wide_int tem = tmin;
2302 type = VR_ANTI_RANGE;
2303 tmin = tmax + 1;
2304 if (wi::cmp (tmin, tmax, sgn) < 0)
2305 covers = true;
2306 tmax = tem - 1;
2307 if (wi::cmp (tmax, tem, sgn) > 0)
2308 covers = true;
2309 /* If the anti-range would cover nothing, drop to varying.
2310 Likewise if the anti-range bounds are outside of the
2311 types values. */
2312 if (covers || wi::cmp (tmin, tmax, sgn) > 0)
2314 set_value_range_to_varying (vr);
2315 return;
2317 min = wide_int_to_tree (expr_type, tmin);
2318 max = wide_int_to_tree (expr_type, tmax);
2320 else
2322 /* Other underflow and/or overflow, drop to VR_VARYING. */
2323 set_value_range_to_varying (vr);
2324 return;
2327 else
2329 /* If overflow does not wrap, saturate to the types min/max
2330 value. */
2331 if (min_ovf == -1)
2332 min = wide_int_to_tree (expr_type, type_min);
2333 else if (min_ovf == 1)
2334 min = wide_int_to_tree (expr_type, type_max);
2335 else
2336 min = wide_int_to_tree (expr_type, wmin);
2338 if (max_ovf == -1)
2339 max = wide_int_to_tree (expr_type, type_min);
2340 else if (max_ovf == 1)
2341 max = wide_int_to_tree (expr_type, type_max);
2342 else
2343 max = wide_int_to_tree (expr_type, wmax);
2346 /* If the result lower bound is constant, we're done;
2347 otherwise, build the symbolic lower bound. */
2348 if (sym_min_op0 == sym_min_op1)
2350 else if (sym_min_op0)
2351 min = build_symbolic_expr (expr_type, sym_min_op0,
2352 neg_min_op0, min);
2353 else if (sym_min_op1)
2355 /* We may not negate if that might introduce
2356 undefined overflow. */
2357 if (! minus_p
2358 || neg_min_op1
2359 || TYPE_OVERFLOW_WRAPS (expr_type))
2360 min = build_symbolic_expr (expr_type, sym_min_op1,
2361 neg_min_op1 ^ minus_p, min);
2362 else
2363 min = NULL_TREE;
2366 /* Likewise for the upper bound. */
2367 if (sym_max_op0 == sym_max_op1)
2369 else if (sym_max_op0)
2370 max = build_symbolic_expr (expr_type, sym_max_op0,
2371 neg_max_op0, max);
2372 else if (sym_max_op1)
2374 /* We may not negate if that might introduce
2375 undefined overflow. */
2376 if (! minus_p
2377 || neg_max_op1
2378 || TYPE_OVERFLOW_WRAPS (expr_type))
2379 max = build_symbolic_expr (expr_type, sym_max_op1,
2380 neg_max_op1 ^ minus_p, max);
2381 else
2382 max = NULL_TREE;
2385 else
2387 /* For other cases, for example if we have a PLUS_EXPR with two
2388 VR_ANTI_RANGEs, drop to VR_VARYING. It would take more effort
2389 to compute a precise range for such a case.
2390 ??? General even mixed range kind operations can be expressed
2391 by for example transforming ~[3, 5] + [1, 2] to range-only
2392 operations and a union primitive:
2393 [-INF, 2] + [1, 2] U [5, +INF] + [1, 2]
2394 [-INF+1, 4] U [6, +INF(OVF)]
2395 though usually the union is not exactly representable with
2396 a single range or anti-range as the above is
2397 [-INF+1, +INF(OVF)] intersected with ~[5, 5]
2398 but one could use a scheme similar to equivalences for this. */
2399 set_value_range_to_varying (vr);
2400 return;
2403 else if (code == MIN_EXPR
2404 || code == MAX_EXPR)
2406 if (vr0.type == VR_RANGE
2407 && !symbolic_range_p (&vr0))
2409 type = VR_RANGE;
2410 if (vr1.type == VR_RANGE
2411 && !symbolic_range_p (&vr1))
2413 /* For operations that make the resulting range directly
2414 proportional to the original ranges, apply the operation to
2415 the same end of each range. */
2416 min = int_const_binop (code, vr0.min, vr1.min);
2417 max = int_const_binop (code, vr0.max, vr1.max);
2419 else if (code == MIN_EXPR)
2421 min = vrp_val_min (expr_type);
2422 max = vr0.max;
2424 else if (code == MAX_EXPR)
2426 min = vr0.min;
2427 max = vrp_val_max (expr_type);
2430 else if (vr1.type == VR_RANGE
2431 && !symbolic_range_p (&vr1))
2433 type = VR_RANGE;
2434 if (code == MIN_EXPR)
2436 min = vrp_val_min (expr_type);
2437 max = vr1.max;
2439 else if (code == MAX_EXPR)
2441 min = vr1.min;
2442 max = vrp_val_max (expr_type);
2445 else
2447 set_value_range_to_varying (vr);
2448 return;
2451 else if (code == MULT_EXPR)
2453 /* Fancy code so that with unsigned, [-3,-1]*[-3,-1] does not
2454 drop to varying. This test requires 2*prec bits if both
2455 operands are signed and 2*prec + 2 bits if either is not. */
2457 signop sign = TYPE_SIGN (expr_type);
2458 unsigned int prec = TYPE_PRECISION (expr_type);
2460 if (range_int_cst_p (&vr0)
2461 && range_int_cst_p (&vr1)
2462 && TYPE_OVERFLOW_WRAPS (expr_type))
2464 typedef FIXED_WIDE_INT (WIDE_INT_MAX_PRECISION * 2) vrp_int;
2465 typedef generic_wide_int
2466 <wi::extended_tree <WIDE_INT_MAX_PRECISION * 2> > vrp_int_cst;
2467 vrp_int sizem1 = wi::mask <vrp_int> (prec, false);
2468 vrp_int size = sizem1 + 1;
2470 /* Extend the values using the sign of the result to PREC2.
2471 From here on out, everthing is just signed math no matter
2472 what the input types were. */
2473 vrp_int min0 = vrp_int_cst (vr0.min);
2474 vrp_int max0 = vrp_int_cst (vr0.max);
2475 vrp_int min1 = vrp_int_cst (vr1.min);
2476 vrp_int max1 = vrp_int_cst (vr1.max);
2477 /* Canonicalize the intervals. */
2478 if (sign == UNSIGNED)
2480 if (wi::ltu_p (size, min0 + max0))
2482 min0 -= size;
2483 max0 -= size;
2486 if (wi::ltu_p (size, min1 + max1))
2488 min1 -= size;
2489 max1 -= size;
2493 vrp_int prod0 = min0 * min1;
2494 vrp_int prod1 = min0 * max1;
2495 vrp_int prod2 = max0 * min1;
2496 vrp_int prod3 = max0 * max1;
2498 /* Sort the 4 products so that min is in prod0 and max is in
2499 prod3. */
2500 /* min0min1 > max0max1 */
2501 if (prod0 > prod3)
2502 std::swap (prod0, prod3);
2504 /* min0max1 > max0min1 */
2505 if (prod1 > prod2)
2506 std::swap (prod1, prod2);
2508 if (prod0 > prod1)
2509 std::swap (prod0, prod1);
2511 if (prod2 > prod3)
2512 std::swap (prod2, prod3);
2514 /* diff = max - min. */
2515 prod2 = prod3 - prod0;
2516 if (wi::geu_p (prod2, sizem1))
2518 /* the range covers all values. */
2519 set_value_range_to_varying (vr);
2520 return;
2523 /* The following should handle the wrapping and selecting
2524 VR_ANTI_RANGE for us. */
2525 min = wide_int_to_tree (expr_type, prod0);
2526 max = wide_int_to_tree (expr_type, prod3);
2527 set_and_canonicalize_value_range (vr, VR_RANGE, min, max, NULL);
2528 return;
2531 /* If we have an unsigned MULT_EXPR with two VR_ANTI_RANGEs,
2532 drop to VR_VARYING. It would take more effort to compute a
2533 precise range for such a case. For example, if we have
2534 op0 == 65536 and op1 == 65536 with their ranges both being
2535 ~[0,0] on a 32-bit machine, we would have op0 * op1 == 0, so
2536 we cannot claim that the product is in ~[0,0]. Note that we
2537 are guaranteed to have vr0.type == vr1.type at this
2538 point. */
2539 if (vr0.type == VR_ANTI_RANGE
2540 && !TYPE_OVERFLOW_UNDEFINED (expr_type))
2542 set_value_range_to_varying (vr);
2543 return;
2546 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
2547 return;
2549 else if (code == RSHIFT_EXPR
2550 || code == LSHIFT_EXPR)
2552 /* If we have a RSHIFT_EXPR with any shift values outside [0..prec-1],
2553 then drop to VR_VARYING. Outside of this range we get undefined
2554 behavior from the shift operation. We cannot even trust
2555 SHIFT_COUNT_TRUNCATED at this stage, because that applies to rtl
2556 shifts, and the operation at the tree level may be widened. */
2557 if (range_int_cst_p (&vr1)
2558 && compare_tree_int (vr1.min, 0) >= 0
2559 && compare_tree_int (vr1.max, TYPE_PRECISION (expr_type)) == -1)
2561 if (code == RSHIFT_EXPR)
2563 /* Even if vr0 is VARYING or otherwise not usable, we can derive
2564 useful ranges just from the shift count. E.g.
2565 x >> 63 for signed 64-bit x is always [-1, 0]. */
2566 if (vr0.type != VR_RANGE || symbolic_range_p (&vr0))
2568 vr0.type = type = VR_RANGE;
2569 vr0.min = vrp_val_min (expr_type);
2570 vr0.max = vrp_val_max (expr_type);
2572 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
2573 return;
2575 /* We can map lshifts by constants to MULT_EXPR handling. */
2576 else if (code == LSHIFT_EXPR
2577 && range_int_cst_singleton_p (&vr1))
2579 bool saved_flag_wrapv;
2580 value_range vr1p = VR_INITIALIZER;
2581 vr1p.type = VR_RANGE;
2582 vr1p.min = (wide_int_to_tree
2583 (expr_type,
2584 wi::set_bit_in_zero (tree_to_shwi (vr1.min),
2585 TYPE_PRECISION (expr_type))));
2586 vr1p.max = vr1p.min;
2587 /* We have to use a wrapping multiply though as signed overflow
2588 on lshifts is implementation defined in C89. */
2589 saved_flag_wrapv = flag_wrapv;
2590 flag_wrapv = 1;
2591 extract_range_from_binary_expr_1 (vr, MULT_EXPR, expr_type,
2592 &vr0, &vr1p);
2593 flag_wrapv = saved_flag_wrapv;
2594 return;
2596 else if (code == LSHIFT_EXPR
2597 && range_int_cst_p (&vr0))
2599 int prec = TYPE_PRECISION (expr_type);
2600 int overflow_pos = prec;
2601 int bound_shift;
2602 wide_int low_bound, high_bound;
2603 bool uns = TYPE_UNSIGNED (expr_type);
2604 bool in_bounds = false;
2606 if (!uns)
2607 overflow_pos -= 1;
2609 bound_shift = overflow_pos - tree_to_shwi (vr1.max);
2610 /* If bound_shift == HOST_BITS_PER_WIDE_INT, the llshift can
2611 overflow. However, for that to happen, vr1.max needs to be
2612 zero, which means vr1 is a singleton range of zero, which
2613 means it should be handled by the previous LSHIFT_EXPR
2614 if-clause. */
2615 wide_int bound = wi::set_bit_in_zero (bound_shift, prec);
2616 wide_int complement = ~(bound - 1);
2618 if (uns)
2620 low_bound = bound;
2621 high_bound = complement;
2622 if (wi::ltu_p (vr0.max, low_bound))
2624 /* [5, 6] << [1, 2] == [10, 24]. */
2625 /* We're shifting out only zeroes, the value increases
2626 monotonically. */
2627 in_bounds = true;
2629 else if (wi::ltu_p (high_bound, vr0.min))
2631 /* [0xffffff00, 0xffffffff] << [1, 2]
2632 == [0xfffffc00, 0xfffffffe]. */
2633 /* We're shifting out only ones, the value decreases
2634 monotonically. */
2635 in_bounds = true;
2638 else
2640 /* [-1, 1] << [1, 2] == [-4, 4]. */
2641 low_bound = complement;
2642 high_bound = bound;
2643 if (wi::lts_p (vr0.max, high_bound)
2644 && wi::lts_p (low_bound, vr0.min))
2646 /* For non-negative numbers, we're shifting out only
2647 zeroes, the value increases monotonically.
2648 For negative numbers, we're shifting out only ones, the
2649 value decreases monotomically. */
2650 in_bounds = true;
2654 if (in_bounds)
2656 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
2657 return;
2661 set_value_range_to_varying (vr);
2662 return;
2664 else if (code == TRUNC_DIV_EXPR
2665 || code == FLOOR_DIV_EXPR
2666 || code == CEIL_DIV_EXPR
2667 || code == EXACT_DIV_EXPR
2668 || code == ROUND_DIV_EXPR)
2670 if (vr0.type != VR_RANGE || symbolic_range_p (&vr0))
2672 /* For division, if op1 has VR_RANGE but op0 does not, something
2673 can be deduced just from that range. Say [min, max] / [4, max]
2674 gives [min / 4, max / 4] range. */
2675 if (vr1.type == VR_RANGE
2676 && !symbolic_range_p (&vr1)
2677 && range_includes_zero_p (vr1.min, vr1.max) == 0)
2679 vr0.type = type = VR_RANGE;
2680 vr0.min = vrp_val_min (expr_type);
2681 vr0.max = vrp_val_max (expr_type);
2683 else
2685 set_value_range_to_varying (vr);
2686 return;
2690 /* For divisions, if flag_non_call_exceptions is true, we must
2691 not eliminate a division by zero. */
2692 if (cfun->can_throw_non_call_exceptions
2693 && (vr1.type != VR_RANGE
2694 || range_includes_zero_p (vr1.min, vr1.max) != 0))
2696 set_value_range_to_varying (vr);
2697 return;
2700 /* For divisions, if op0 is VR_RANGE, we can deduce a range
2701 even if op1 is VR_VARYING, VR_ANTI_RANGE, symbolic or can
2702 include 0. */
2703 if (vr0.type == VR_RANGE
2704 && (vr1.type != VR_RANGE
2705 || range_includes_zero_p (vr1.min, vr1.max) != 0))
2707 tree zero = build_int_cst (TREE_TYPE (vr0.min), 0);
2708 int cmp;
2710 min = NULL_TREE;
2711 max = NULL_TREE;
2712 if (TYPE_UNSIGNED (expr_type)
2713 || value_range_nonnegative_p (&vr1))
2715 /* For unsigned division or when divisor is known
2716 to be non-negative, the range has to cover
2717 all numbers from 0 to max for positive max
2718 and all numbers from min to 0 for negative min. */
2719 cmp = compare_values (vr0.max, zero);
2720 if (cmp == -1)
2722 /* When vr0.max < 0, vr1.min != 0 and value
2723 ranges for dividend and divisor are available. */
2724 if (vr1.type == VR_RANGE
2725 && !symbolic_range_p (&vr0)
2726 && !symbolic_range_p (&vr1)
2727 && compare_values (vr1.min, zero) != 0)
2728 max = int_const_binop (code, vr0.max, vr1.min);
2729 else
2730 max = zero;
2732 else if (cmp == 0 || cmp == 1)
2733 max = vr0.max;
2734 else
2735 type = VR_VARYING;
2736 cmp = compare_values (vr0.min, zero);
2737 if (cmp == 1)
2739 /* For unsigned division when value ranges for dividend
2740 and divisor are available. */
2741 if (vr1.type == VR_RANGE
2742 && !symbolic_range_p (&vr0)
2743 && !symbolic_range_p (&vr1)
2744 && compare_values (vr1.max, zero) != 0)
2745 min = int_const_binop (code, vr0.min, vr1.max);
2746 else
2747 min = zero;
2749 else if (cmp == 0 || cmp == -1)
2750 min = vr0.min;
2751 else
2752 type = VR_VARYING;
2754 else
2756 /* Otherwise the range is -max .. max or min .. -min
2757 depending on which bound is bigger in absolute value,
2758 as the division can change the sign. */
2759 abs_extent_range (vr, vr0.min, vr0.max);
2760 return;
2762 if (type == VR_VARYING)
2764 set_value_range_to_varying (vr);
2765 return;
2768 else if (!symbolic_range_p (&vr0) && !symbolic_range_p (&vr1))
2770 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
2771 return;
2774 else if (code == TRUNC_MOD_EXPR)
2776 if (range_is_null (&vr1))
2778 set_value_range_to_undefined (vr);
2779 return;
2781 /* ABS (A % B) < ABS (B) and either
2782 0 <= A % B <= A or A <= A % B <= 0. */
2783 type = VR_RANGE;
2784 signop sgn = TYPE_SIGN (expr_type);
2785 unsigned int prec = TYPE_PRECISION (expr_type);
2786 wide_int wmin, wmax, tmp;
2787 wide_int zero = wi::zero (prec);
2788 wide_int one = wi::one (prec);
2789 if (vr1.type == VR_RANGE && !symbolic_range_p (&vr1))
2791 wmax = wi::sub (vr1.max, one);
2792 if (sgn == SIGNED)
2794 tmp = wi::sub (wi::minus_one (prec), vr1.min);
2795 wmax = wi::smax (wmax, tmp);
2798 else
2800 wmax = wi::max_value (prec, sgn);
2801 /* X % INT_MIN may be INT_MAX. */
2802 if (sgn == UNSIGNED)
2803 wmax = wmax - one;
2806 if (sgn == UNSIGNED)
2807 wmin = zero;
2808 else
2810 wmin = -wmax;
2811 if (vr0.type == VR_RANGE && TREE_CODE (vr0.min) == INTEGER_CST)
2813 tmp = vr0.min;
2814 if (wi::gts_p (tmp, zero))
2815 tmp = zero;
2816 wmin = wi::smax (wmin, tmp);
2820 if (vr0.type == VR_RANGE && TREE_CODE (vr0.max) == INTEGER_CST)
2822 tmp = vr0.max;
2823 if (sgn == SIGNED && wi::neg_p (tmp))
2824 tmp = zero;
2825 wmax = wi::min (wmax, tmp, sgn);
2828 min = wide_int_to_tree (expr_type, wmin);
2829 max = wide_int_to_tree (expr_type, wmax);
2831 else if (code == BIT_AND_EXPR || code == BIT_IOR_EXPR || code == BIT_XOR_EXPR)
2833 bool int_cst_range0, int_cst_range1;
2834 wide_int may_be_nonzero0, may_be_nonzero1;
2835 wide_int must_be_nonzero0, must_be_nonzero1;
2837 int_cst_range0 = zero_nonzero_bits_from_vr (expr_type, &vr0,
2838 &may_be_nonzero0,
2839 &must_be_nonzero0);
2840 int_cst_range1 = zero_nonzero_bits_from_vr (expr_type, &vr1,
2841 &may_be_nonzero1,
2842 &must_be_nonzero1);
2844 if (code == BIT_AND_EXPR || code == BIT_IOR_EXPR)
2846 value_range *vr0p = NULL, *vr1p = NULL;
2847 if (range_int_cst_singleton_p (&vr1))
2849 vr0p = &vr0;
2850 vr1p = &vr1;
2852 else if (range_int_cst_singleton_p (&vr0))
2854 vr0p = &vr1;
2855 vr1p = &vr0;
2857 /* For op & or | attempt to optimize:
2858 [x, y] op z into [x op z, y op z]
2859 if z is a constant which (for op | its bitwise not) has n
2860 consecutive least significant bits cleared followed by m 1
2861 consecutive bits set immediately above it and either
2862 m + n == precision, or (x >> (m + n)) == (y >> (m + n)).
2863 The least significant n bits of all the values in the range are
2864 cleared or set, the m bits above it are preserved and any bits
2865 above these are required to be the same for all values in the
2866 range. */
2867 if (vr0p && range_int_cst_p (vr0p))
2869 wide_int w = vr1p->min;
2870 int m = 0, n = 0;
2871 if (code == BIT_IOR_EXPR)
2872 w = ~w;
2873 if (wi::eq_p (w, 0))
2874 n = TYPE_PRECISION (expr_type);
2875 else
2877 n = wi::ctz (w);
2878 w = ~(w | wi::mask (n, false, w.get_precision ()));
2879 if (wi::eq_p (w, 0))
2880 m = TYPE_PRECISION (expr_type) - n;
2881 else
2882 m = wi::ctz (w) - n;
2884 wide_int mask = wi::mask (m + n, true, w.get_precision ());
2885 if (wi::eq_p (mask & vr0p->min, mask & vr0p->max))
2887 min = int_const_binop (code, vr0p->min, vr1p->min);
2888 max = int_const_binop (code, vr0p->max, vr1p->min);
2893 type = VR_RANGE;
2894 if (min && max)
2895 /* Optimized above already. */;
2896 else if (code == BIT_AND_EXPR)
2898 min = wide_int_to_tree (expr_type,
2899 must_be_nonzero0 & must_be_nonzero1);
2900 wide_int wmax = may_be_nonzero0 & may_be_nonzero1;
2901 /* If both input ranges contain only negative values we can
2902 truncate the result range maximum to the minimum of the
2903 input range maxima. */
2904 if (int_cst_range0 && int_cst_range1
2905 && tree_int_cst_sgn (vr0.max) < 0
2906 && tree_int_cst_sgn (vr1.max) < 0)
2908 wmax = wi::min (wmax, vr0.max, TYPE_SIGN (expr_type));
2909 wmax = wi::min (wmax, vr1.max, TYPE_SIGN (expr_type));
2911 /* If either input range contains only non-negative values
2912 we can truncate the result range maximum to the respective
2913 maximum of the input range. */
2914 if (int_cst_range0 && tree_int_cst_sgn (vr0.min) >= 0)
2915 wmax = wi::min (wmax, vr0.max, TYPE_SIGN (expr_type));
2916 if (int_cst_range1 && tree_int_cst_sgn (vr1.min) >= 0)
2917 wmax = wi::min (wmax, vr1.max, TYPE_SIGN (expr_type));
2918 max = wide_int_to_tree (expr_type, wmax);
2919 cmp = compare_values (min, max);
2920 /* PR68217: In case of signed & sign-bit-CST should
2921 result in [-INF, 0] instead of [-INF, INF]. */
2922 if (cmp == -2 || cmp == 1)
2924 wide_int sign_bit
2925 = wi::set_bit_in_zero (TYPE_PRECISION (expr_type) - 1,
2926 TYPE_PRECISION (expr_type));
2927 if (!TYPE_UNSIGNED (expr_type)
2928 && ((value_range_constant_singleton (&vr0)
2929 && !wi::cmps (vr0.min, sign_bit))
2930 || (value_range_constant_singleton (&vr1)
2931 && !wi::cmps (vr1.min, sign_bit))))
2933 min = TYPE_MIN_VALUE (expr_type);
2934 max = build_int_cst (expr_type, 0);
2938 else if (code == BIT_IOR_EXPR)
2940 max = wide_int_to_tree (expr_type,
2941 may_be_nonzero0 | may_be_nonzero1);
2942 wide_int wmin = must_be_nonzero0 | must_be_nonzero1;
2943 /* If the input ranges contain only positive values we can
2944 truncate the minimum of the result range to the maximum
2945 of the input range minima. */
2946 if (int_cst_range0 && int_cst_range1
2947 && tree_int_cst_sgn (vr0.min) >= 0
2948 && tree_int_cst_sgn (vr1.min) >= 0)
2950 wmin = wi::max (wmin, vr0.min, TYPE_SIGN (expr_type));
2951 wmin = wi::max (wmin, vr1.min, TYPE_SIGN (expr_type));
2953 /* If either input range contains only negative values
2954 we can truncate the minimum of the result range to the
2955 respective minimum range. */
2956 if (int_cst_range0 && tree_int_cst_sgn (vr0.max) < 0)
2957 wmin = wi::max (wmin, vr0.min, TYPE_SIGN (expr_type));
2958 if (int_cst_range1 && tree_int_cst_sgn (vr1.max) < 0)
2959 wmin = wi::max (wmin, vr1.min, TYPE_SIGN (expr_type));
2960 min = wide_int_to_tree (expr_type, wmin);
2962 else if (code == BIT_XOR_EXPR)
2964 wide_int result_zero_bits = ((must_be_nonzero0 & must_be_nonzero1)
2965 | ~(may_be_nonzero0 | may_be_nonzero1));
2966 wide_int result_one_bits
2967 = (must_be_nonzero0.and_not (may_be_nonzero1)
2968 | must_be_nonzero1.and_not (may_be_nonzero0));
2969 max = wide_int_to_tree (expr_type, ~result_zero_bits);
2970 min = wide_int_to_tree (expr_type, result_one_bits);
2971 /* If the range has all positive or all negative values the
2972 result is better than VARYING. */
2973 if (tree_int_cst_sgn (min) < 0
2974 || tree_int_cst_sgn (max) >= 0)
2976 else
2977 max = min = NULL_TREE;
2980 else
2981 gcc_unreachable ();
2983 /* If either MIN or MAX overflowed, then set the resulting range to
2984 VARYING. */
2985 if (min == NULL_TREE
2986 || TREE_OVERFLOW_P (min)
2987 || max == NULL_TREE
2988 || TREE_OVERFLOW_P (max))
2990 set_value_range_to_varying (vr);
2991 return;
2994 /* We punt for [-INF, +INF].
2995 We learn nothing when we have INF on both sides.
2996 Note that we do accept [-INF, -INF] and [+INF, +INF]. */
2997 if (vrp_val_is_min (min) && vrp_val_is_max (max))
2999 set_value_range_to_varying (vr);
3000 return;
3003 cmp = compare_values (min, max);
3004 if (cmp == -2 || cmp == 1)
3006 /* If the new range has its limits swapped around (MIN > MAX),
3007 then the operation caused one of them to wrap around, mark
3008 the new range VARYING. */
3009 set_value_range_to_varying (vr);
3011 else
3012 set_value_range (vr, type, min, max, NULL);
3015 /* Extract range information from a binary expression OP0 CODE OP1 based on
3016 the ranges of each of its operands with resulting type EXPR_TYPE.
3017 The resulting range is stored in *VR. */
3019 static void
3020 extract_range_from_binary_expr (value_range *vr,
3021 enum tree_code code,
3022 tree expr_type, tree op0, tree op1)
3024 value_range vr0 = VR_INITIALIZER;
3025 value_range vr1 = VR_INITIALIZER;
3027 /* Get value ranges for each operand. For constant operands, create
3028 a new value range with the operand to simplify processing. */
3029 if (TREE_CODE (op0) == SSA_NAME)
3030 vr0 = *(get_value_range (op0));
3031 else if (is_gimple_min_invariant (op0))
3032 set_value_range_to_value (&vr0, op0, NULL);
3033 else
3034 set_value_range_to_varying (&vr0);
3036 if (TREE_CODE (op1) == SSA_NAME)
3037 vr1 = *(get_value_range (op1));
3038 else if (is_gimple_min_invariant (op1))
3039 set_value_range_to_value (&vr1, op1, NULL);
3040 else
3041 set_value_range_to_varying (&vr1);
3043 extract_range_from_binary_expr_1 (vr, code, expr_type, &vr0, &vr1);
3045 /* Try harder for PLUS and MINUS if the range of one operand is symbolic
3046 and based on the other operand, for example if it was deduced from a
3047 symbolic comparison. When a bound of the range of the first operand
3048 is invariant, we set the corresponding bound of the new range to INF
3049 in order to avoid recursing on the range of the second operand. */
3050 if (vr->type == VR_VARYING
3051 && (code == PLUS_EXPR || code == MINUS_EXPR)
3052 && TREE_CODE (op1) == SSA_NAME
3053 && vr0.type == VR_RANGE
3054 && symbolic_range_based_on_p (&vr0, op1))
3056 const bool minus_p = (code == MINUS_EXPR);
3057 value_range n_vr1 = VR_INITIALIZER;
3059 /* Try with VR0 and [-INF, OP1]. */
3060 if (is_gimple_min_invariant (minus_p ? vr0.max : vr0.min))
3061 set_value_range (&n_vr1, VR_RANGE, vrp_val_min (expr_type), op1, NULL);
3063 /* Try with VR0 and [OP1, +INF]. */
3064 else if (is_gimple_min_invariant (minus_p ? vr0.min : vr0.max))
3065 set_value_range (&n_vr1, VR_RANGE, op1, vrp_val_max (expr_type), NULL);
3067 /* Try with VR0 and [OP1, OP1]. */
3068 else
3069 set_value_range (&n_vr1, VR_RANGE, op1, op1, NULL);
3071 extract_range_from_binary_expr_1 (vr, code, expr_type, &vr0, &n_vr1);
3074 if (vr->type == VR_VARYING
3075 && (code == PLUS_EXPR || code == MINUS_EXPR)
3076 && TREE_CODE (op0) == SSA_NAME
3077 && vr1.type == VR_RANGE
3078 && symbolic_range_based_on_p (&vr1, op0))
3080 const bool minus_p = (code == MINUS_EXPR);
3081 value_range n_vr0 = VR_INITIALIZER;
3083 /* Try with [-INF, OP0] and VR1. */
3084 if (is_gimple_min_invariant (minus_p ? vr1.max : vr1.min))
3085 set_value_range (&n_vr0, VR_RANGE, vrp_val_min (expr_type), op0, NULL);
3087 /* Try with [OP0, +INF] and VR1. */
3088 else if (is_gimple_min_invariant (minus_p ? vr1.min : vr1.max))
3089 set_value_range (&n_vr0, VR_RANGE, op0, vrp_val_max (expr_type), NULL);
3091 /* Try with [OP0, OP0] and VR1. */
3092 else
3093 set_value_range (&n_vr0, VR_RANGE, op0, op0, NULL);
3095 extract_range_from_binary_expr_1 (vr, code, expr_type, &n_vr0, &vr1);
3098 /* If we didn't derive a range for MINUS_EXPR, and
3099 op1's range is ~[op0,op0] or vice-versa, then we
3100 can derive a non-null range. This happens often for
3101 pointer subtraction. */
3102 if (vr->type == VR_VARYING
3103 && code == MINUS_EXPR
3104 && TREE_CODE (op0) == SSA_NAME
3105 && ((vr0.type == VR_ANTI_RANGE
3106 && vr0.min == op1
3107 && vr0.min == vr0.max)
3108 || (vr1.type == VR_ANTI_RANGE
3109 && vr1.min == op0
3110 && vr1.min == vr1.max)))
3111 set_value_range_to_nonnull (vr, TREE_TYPE (op0));
3114 /* Extract range information from a unary operation CODE based on
3115 the range of its operand *VR0 with type OP0_TYPE with resulting type TYPE.
3116 The resulting range is stored in *VR. */
3118 void
3119 extract_range_from_unary_expr (value_range *vr,
3120 enum tree_code code, tree type,
3121 value_range *vr0_, tree op0_type)
3123 value_range vr0 = *vr0_, vrtem0 = VR_INITIALIZER, vrtem1 = VR_INITIALIZER;
3125 /* VRP only operates on integral and pointer types. */
3126 if (!(INTEGRAL_TYPE_P (op0_type)
3127 || POINTER_TYPE_P (op0_type))
3128 || !(INTEGRAL_TYPE_P (type)
3129 || POINTER_TYPE_P (type)))
3131 set_value_range_to_varying (vr);
3132 return;
3135 /* If VR0 is UNDEFINED, so is the result. */
3136 if (vr0.type == VR_UNDEFINED)
3138 set_value_range_to_undefined (vr);
3139 return;
3142 /* Handle operations that we express in terms of others. */
3143 if (code == PAREN_EXPR || code == OBJ_TYPE_REF)
3145 /* PAREN_EXPR and OBJ_TYPE_REF are simple copies. */
3146 copy_value_range (vr, &vr0);
3147 return;
3149 else if (code == NEGATE_EXPR)
3151 /* -X is simply 0 - X, so re-use existing code that also handles
3152 anti-ranges fine. */
3153 value_range zero = VR_INITIALIZER;
3154 set_value_range_to_value (&zero, build_int_cst (type, 0), NULL);
3155 extract_range_from_binary_expr_1 (vr, MINUS_EXPR, type, &zero, &vr0);
3156 return;
3158 else if (code == BIT_NOT_EXPR)
3160 /* ~X is simply -1 - X, so re-use existing code that also handles
3161 anti-ranges fine. */
3162 value_range minusone = VR_INITIALIZER;
3163 set_value_range_to_value (&minusone, build_int_cst (type, -1), NULL);
3164 extract_range_from_binary_expr_1 (vr, MINUS_EXPR,
3165 type, &minusone, &vr0);
3166 return;
3169 /* Now canonicalize anti-ranges to ranges when they are not symbolic
3170 and express op ~[] as (op []') U (op []''). */
3171 if (vr0.type == VR_ANTI_RANGE
3172 && ranges_from_anti_range (&vr0, &vrtem0, &vrtem1))
3174 extract_range_from_unary_expr (vr, code, type, &vrtem0, op0_type);
3175 if (vrtem1.type != VR_UNDEFINED)
3177 value_range vrres = VR_INITIALIZER;
3178 extract_range_from_unary_expr (&vrres, code, type,
3179 &vrtem1, op0_type);
3180 vrp_meet (vr, &vrres);
3182 return;
3185 if (CONVERT_EXPR_CODE_P (code))
3187 tree inner_type = op0_type;
3188 tree outer_type = type;
3190 /* If the expression evaluates to a pointer, we are only interested in
3191 determining if it evaluates to NULL [0, 0] or non-NULL (~[0, 0]). */
3192 if (POINTER_TYPE_P (type))
3194 if (range_is_nonnull (&vr0))
3195 set_value_range_to_nonnull (vr, type);
3196 else if (range_is_null (&vr0))
3197 set_value_range_to_null (vr, type);
3198 else
3199 set_value_range_to_varying (vr);
3200 return;
3203 /* If VR0 is varying and we increase the type precision, assume
3204 a full range for the following transformation. */
3205 if (vr0.type == VR_VARYING
3206 && INTEGRAL_TYPE_P (inner_type)
3207 && TYPE_PRECISION (inner_type) < TYPE_PRECISION (outer_type))
3209 vr0.type = VR_RANGE;
3210 vr0.min = TYPE_MIN_VALUE (inner_type);
3211 vr0.max = TYPE_MAX_VALUE (inner_type);
3214 /* If VR0 is a constant range or anti-range and the conversion is
3215 not truncating we can convert the min and max values and
3216 canonicalize the resulting range. Otherwise we can do the
3217 conversion if the size of the range is less than what the
3218 precision of the target type can represent and the range is
3219 not an anti-range. */
3220 if ((vr0.type == VR_RANGE
3221 || vr0.type == VR_ANTI_RANGE)
3222 && TREE_CODE (vr0.min) == INTEGER_CST
3223 && TREE_CODE (vr0.max) == INTEGER_CST
3224 && (TYPE_PRECISION (outer_type) >= TYPE_PRECISION (inner_type)
3225 || (vr0.type == VR_RANGE
3226 && integer_zerop (int_const_binop (RSHIFT_EXPR,
3227 int_const_binop (MINUS_EXPR, vr0.max, vr0.min),
3228 size_int (TYPE_PRECISION (outer_type)))))))
3230 tree new_min, new_max;
3231 new_min = force_fit_type (outer_type, wi::to_widest (vr0.min),
3232 0, false);
3233 new_max = force_fit_type (outer_type, wi::to_widest (vr0.max),
3234 0, false);
3235 set_and_canonicalize_value_range (vr, vr0.type,
3236 new_min, new_max, NULL);
3237 return;
3240 set_value_range_to_varying (vr);
3241 return;
3243 else if (code == ABS_EXPR)
3245 tree min, max;
3246 int cmp;
3248 /* Pass through vr0 in the easy cases. */
3249 if (TYPE_UNSIGNED (type)
3250 || value_range_nonnegative_p (&vr0))
3252 copy_value_range (vr, &vr0);
3253 return;
3256 /* For the remaining varying or symbolic ranges we can't do anything
3257 useful. */
3258 if (vr0.type == VR_VARYING
3259 || symbolic_range_p (&vr0))
3261 set_value_range_to_varying (vr);
3262 return;
3265 /* -TYPE_MIN_VALUE = TYPE_MIN_VALUE with flag_wrapv so we can't get a
3266 useful range. */
3267 if (!TYPE_OVERFLOW_UNDEFINED (type)
3268 && ((vr0.type == VR_RANGE
3269 && vrp_val_is_min (vr0.min))
3270 || (vr0.type == VR_ANTI_RANGE
3271 && !vrp_val_is_min (vr0.min))))
3273 set_value_range_to_varying (vr);
3274 return;
3277 /* ABS_EXPR may flip the range around, if the original range
3278 included negative values. */
3279 if (!vrp_val_is_min (vr0.min))
3280 min = fold_unary_to_constant (code, type, vr0.min);
3281 else
3282 min = TYPE_MAX_VALUE (type);
3284 if (!vrp_val_is_min (vr0.max))
3285 max = fold_unary_to_constant (code, type, vr0.max);
3286 else
3287 max = TYPE_MAX_VALUE (type);
3289 cmp = compare_values (min, max);
3291 /* If a VR_ANTI_RANGEs contains zero, then we have
3292 ~[-INF, min(MIN, MAX)]. */
3293 if (vr0.type == VR_ANTI_RANGE)
3295 if (range_includes_zero_p (vr0.min, vr0.max) == 1)
3297 /* Take the lower of the two values. */
3298 if (cmp != 1)
3299 max = min;
3301 /* Create ~[-INF, min (abs(MIN), abs(MAX))]
3302 or ~[-INF + 1, min (abs(MIN), abs(MAX))] when
3303 flag_wrapv is set and the original anti-range doesn't include
3304 TYPE_MIN_VALUE, remember -TYPE_MIN_VALUE = TYPE_MIN_VALUE. */
3305 if (TYPE_OVERFLOW_WRAPS (type))
3307 tree type_min_value = TYPE_MIN_VALUE (type);
3309 min = (vr0.min != type_min_value
3310 ? int_const_binop (PLUS_EXPR, type_min_value,
3311 build_int_cst (TREE_TYPE (type_min_value), 1))
3312 : type_min_value);
3314 else
3315 min = TYPE_MIN_VALUE (type);
3317 else
3319 /* All else has failed, so create the range [0, INF], even for
3320 flag_wrapv since TYPE_MIN_VALUE is in the original
3321 anti-range. */
3322 vr0.type = VR_RANGE;
3323 min = build_int_cst (type, 0);
3324 max = TYPE_MAX_VALUE (type);
3328 /* If the range contains zero then we know that the minimum value in the
3329 range will be zero. */
3330 else if (range_includes_zero_p (vr0.min, vr0.max) == 1)
3332 if (cmp == 1)
3333 max = min;
3334 min = build_int_cst (type, 0);
3336 else
3338 /* If the range was reversed, swap MIN and MAX. */
3339 if (cmp == 1)
3340 std::swap (min, max);
3343 cmp = compare_values (min, max);
3344 if (cmp == -2 || cmp == 1)
3346 /* If the new range has its limits swapped around (MIN > MAX),
3347 then the operation caused one of them to wrap around, mark
3348 the new range VARYING. */
3349 set_value_range_to_varying (vr);
3351 else
3352 set_value_range (vr, vr0.type, min, max, NULL);
3353 return;
3356 /* For unhandled operations fall back to varying. */
3357 set_value_range_to_varying (vr);
3358 return;
3362 /* Extract range information from a unary expression CODE OP0 based on
3363 the range of its operand with resulting type TYPE.
3364 The resulting range is stored in *VR. */
3366 static void
3367 extract_range_from_unary_expr (value_range *vr, enum tree_code code,
3368 tree type, tree op0)
3370 value_range vr0 = VR_INITIALIZER;
3372 /* Get value ranges for the operand. For constant operands, create
3373 a new value range with the operand to simplify processing. */
3374 if (TREE_CODE (op0) == SSA_NAME)
3375 vr0 = *(get_value_range (op0));
3376 else if (is_gimple_min_invariant (op0))
3377 set_value_range_to_value (&vr0, op0, NULL);
3378 else
3379 set_value_range_to_varying (&vr0);
3381 extract_range_from_unary_expr (vr, code, type, &vr0, TREE_TYPE (op0));
3385 /* Extract range information from a conditional expression STMT based on
3386 the ranges of each of its operands and the expression code. */
3388 static void
3389 extract_range_from_cond_expr (value_range *vr, gassign *stmt)
3391 tree op0, op1;
3392 value_range vr0 = VR_INITIALIZER;
3393 value_range vr1 = VR_INITIALIZER;
3395 /* Get value ranges for each operand. For constant operands, create
3396 a new value range with the operand to simplify processing. */
3397 op0 = gimple_assign_rhs2 (stmt);
3398 if (TREE_CODE (op0) == SSA_NAME)
3399 vr0 = *(get_value_range (op0));
3400 else if (is_gimple_min_invariant (op0))
3401 set_value_range_to_value (&vr0, op0, NULL);
3402 else
3403 set_value_range_to_varying (&vr0);
3405 op1 = gimple_assign_rhs3 (stmt);
3406 if (TREE_CODE (op1) == SSA_NAME)
3407 vr1 = *(get_value_range (op1));
3408 else if (is_gimple_min_invariant (op1))
3409 set_value_range_to_value (&vr1, op1, NULL);
3410 else
3411 set_value_range_to_varying (&vr1);
3413 /* The resulting value range is the union of the operand ranges */
3414 copy_value_range (vr, &vr0);
3415 vrp_meet (vr, &vr1);
3419 /* Extract range information from a comparison expression EXPR based
3420 on the range of its operand and the expression code. */
3422 static void
3423 extract_range_from_comparison (value_range *vr, enum tree_code code,
3424 tree type, tree op0, tree op1)
3426 bool sop;
3427 tree val;
3429 val = vrp_evaluate_conditional_warnv_with_ops (code, op0, op1, false, &sop,
3430 NULL);
3431 if (val)
3433 /* Since this expression was found on the RHS of an assignment,
3434 its type may be different from _Bool. Convert VAL to EXPR's
3435 type. */
3436 val = fold_convert (type, val);
3437 if (is_gimple_min_invariant (val))
3438 set_value_range_to_value (vr, val, vr->equiv);
3439 else
3440 set_value_range (vr, VR_RANGE, val, val, vr->equiv);
3442 else
3443 /* The result of a comparison is always true or false. */
3444 set_value_range_to_truthvalue (vr, type);
3447 /* Helper function for simplify_internal_call_using_ranges and
3448 extract_range_basic. Return true if OP0 SUBCODE OP1 for
3449 SUBCODE {PLUS,MINUS,MULT}_EXPR is known to never overflow or
3450 always overflow. Set *OVF to true if it is known to always
3451 overflow. */
3453 static bool
3454 check_for_binary_op_overflow (enum tree_code subcode, tree type,
3455 tree op0, tree op1, bool *ovf)
3457 value_range vr0 = VR_INITIALIZER;
3458 value_range vr1 = VR_INITIALIZER;
3459 if (TREE_CODE (op0) == SSA_NAME)
3460 vr0 = *get_value_range (op0);
3461 else if (TREE_CODE (op0) == INTEGER_CST)
3462 set_value_range_to_value (&vr0, op0, NULL);
3463 else
3464 set_value_range_to_varying (&vr0);
3466 if (TREE_CODE (op1) == SSA_NAME)
3467 vr1 = *get_value_range (op1);
3468 else if (TREE_CODE (op1) == INTEGER_CST)
3469 set_value_range_to_value (&vr1, op1, NULL);
3470 else
3471 set_value_range_to_varying (&vr1);
3473 if (!range_int_cst_p (&vr0)
3474 || TREE_OVERFLOW (vr0.min)
3475 || TREE_OVERFLOW (vr0.max))
3477 vr0.min = vrp_val_min (TREE_TYPE (op0));
3478 vr0.max = vrp_val_max (TREE_TYPE (op0));
3480 if (!range_int_cst_p (&vr1)
3481 || TREE_OVERFLOW (vr1.min)
3482 || TREE_OVERFLOW (vr1.max))
3484 vr1.min = vrp_val_min (TREE_TYPE (op1));
3485 vr1.max = vrp_val_max (TREE_TYPE (op1));
3487 *ovf = arith_overflowed_p (subcode, type, vr0.min,
3488 subcode == MINUS_EXPR ? vr1.max : vr1.min);
3489 if (arith_overflowed_p (subcode, type, vr0.max,
3490 subcode == MINUS_EXPR ? vr1.min : vr1.max) != *ovf)
3491 return false;
3492 if (subcode == MULT_EXPR)
3494 if (arith_overflowed_p (subcode, type, vr0.min, vr1.max) != *ovf
3495 || arith_overflowed_p (subcode, type, vr0.max, vr1.min) != *ovf)
3496 return false;
3498 if (*ovf)
3500 /* So far we found that there is an overflow on the boundaries.
3501 That doesn't prove that there is an overflow even for all values
3502 in between the boundaries. For that compute widest_int range
3503 of the result and see if it doesn't overlap the range of
3504 type. */
3505 widest_int wmin, wmax;
3506 widest_int w[4];
3507 int i;
3508 w[0] = wi::to_widest (vr0.min);
3509 w[1] = wi::to_widest (vr0.max);
3510 w[2] = wi::to_widest (vr1.min);
3511 w[3] = wi::to_widest (vr1.max);
3512 for (i = 0; i < 4; i++)
3514 widest_int wt;
3515 switch (subcode)
3517 case PLUS_EXPR:
3518 wt = wi::add (w[i & 1], w[2 + (i & 2) / 2]);
3519 break;
3520 case MINUS_EXPR:
3521 wt = wi::sub (w[i & 1], w[2 + (i & 2) / 2]);
3522 break;
3523 case MULT_EXPR:
3524 wt = wi::mul (w[i & 1], w[2 + (i & 2) / 2]);
3525 break;
3526 default:
3527 gcc_unreachable ();
3529 if (i == 0)
3531 wmin = wt;
3532 wmax = wt;
3534 else
3536 wmin = wi::smin (wmin, wt);
3537 wmax = wi::smax (wmax, wt);
3540 /* The result of op0 CODE op1 is known to be in range
3541 [wmin, wmax]. */
3542 widest_int wtmin = wi::to_widest (vrp_val_min (type));
3543 widest_int wtmax = wi::to_widest (vrp_val_max (type));
3544 /* If all values in [wmin, wmax] are smaller than
3545 [wtmin, wtmax] or all are larger than [wtmin, wtmax],
3546 the arithmetic operation will always overflow. */
3547 if (wmax < wtmin || wmin > wtmax)
3548 return true;
3549 return false;
3551 return true;
3554 /* Try to derive a nonnegative or nonzero range out of STMT relying
3555 primarily on generic routines in fold in conjunction with range data.
3556 Store the result in *VR */
3558 static void
3559 extract_range_basic (value_range *vr, gimple *stmt)
3561 bool sop;
3562 tree type = gimple_expr_type (stmt);
3564 if (is_gimple_call (stmt))
3566 tree arg;
3567 int mini, maxi, zerov = 0, prec;
3568 enum tree_code subcode = ERROR_MARK;
3569 combined_fn cfn = gimple_call_combined_fn (stmt);
3571 switch (cfn)
3573 case CFN_BUILT_IN_CONSTANT_P:
3574 /* If the call is __builtin_constant_p and the argument is a
3575 function parameter resolve it to false. This avoids bogus
3576 array bound warnings.
3577 ??? We could do this as early as inlining is finished. */
3578 arg = gimple_call_arg (stmt, 0);
3579 if (TREE_CODE (arg) == SSA_NAME
3580 && SSA_NAME_IS_DEFAULT_DEF (arg)
3581 && TREE_CODE (SSA_NAME_VAR (arg)) == PARM_DECL
3582 && cfun->after_inlining)
3584 set_value_range_to_null (vr, type);
3585 return;
3587 break;
3588 /* Both __builtin_ffs* and __builtin_popcount return
3589 [0, prec]. */
3590 CASE_CFN_FFS:
3591 CASE_CFN_POPCOUNT:
3592 arg = gimple_call_arg (stmt, 0);
3593 prec = TYPE_PRECISION (TREE_TYPE (arg));
3594 mini = 0;
3595 maxi = prec;
3596 if (TREE_CODE (arg) == SSA_NAME)
3598 value_range *vr0 = get_value_range (arg);
3599 /* If arg is non-zero, then ffs or popcount
3600 are non-zero. */
3601 if ((vr0->type == VR_RANGE
3602 && range_includes_zero_p (vr0->min, vr0->max) == 0)
3603 || (vr0->type == VR_ANTI_RANGE
3604 && range_includes_zero_p (vr0->min, vr0->max) == 1))
3605 mini = 1;
3606 /* If some high bits are known to be zero,
3607 we can decrease the maximum. */
3608 if (vr0->type == VR_RANGE
3609 && TREE_CODE (vr0->max) == INTEGER_CST
3610 && !operand_less_p (vr0->min,
3611 build_zero_cst (TREE_TYPE (vr0->min))))
3612 maxi = tree_floor_log2 (vr0->max) + 1;
3614 goto bitop_builtin;
3615 /* __builtin_parity* returns [0, 1]. */
3616 CASE_CFN_PARITY:
3617 mini = 0;
3618 maxi = 1;
3619 goto bitop_builtin;
3620 /* __builtin_c[lt]z* return [0, prec-1], except for
3621 when the argument is 0, but that is undefined behavior.
3622 On many targets where the CLZ RTL or optab value is defined
3623 for 0 the value is prec, so include that in the range
3624 by default. */
3625 CASE_CFN_CLZ:
3626 arg = gimple_call_arg (stmt, 0);
3627 prec = TYPE_PRECISION (TREE_TYPE (arg));
3628 mini = 0;
3629 maxi = prec;
3630 if (optab_handler (clz_optab, TYPE_MODE (TREE_TYPE (arg)))
3631 != CODE_FOR_nothing
3632 && CLZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (TREE_TYPE (arg)),
3633 zerov)
3634 /* Handle only the single common value. */
3635 && zerov != prec)
3636 /* Magic value to give up, unless vr0 proves
3637 arg is non-zero. */
3638 mini = -2;
3639 if (TREE_CODE (arg) == SSA_NAME)
3641 value_range *vr0 = get_value_range (arg);
3642 /* From clz of VR_RANGE minimum we can compute
3643 result maximum. */
3644 if (vr0->type == VR_RANGE
3645 && TREE_CODE (vr0->min) == INTEGER_CST)
3647 maxi = prec - 1 - tree_floor_log2 (vr0->min);
3648 if (maxi != prec)
3649 mini = 0;
3651 else if (vr0->type == VR_ANTI_RANGE
3652 && integer_zerop (vr0->min))
3654 maxi = prec - 1;
3655 mini = 0;
3657 if (mini == -2)
3658 break;
3659 /* From clz of VR_RANGE maximum we can compute
3660 result minimum. */
3661 if (vr0->type == VR_RANGE
3662 && TREE_CODE (vr0->max) == INTEGER_CST)
3664 mini = prec - 1 - tree_floor_log2 (vr0->max);
3665 if (mini == prec)
3666 break;
3669 if (mini == -2)
3670 break;
3671 goto bitop_builtin;
3672 /* __builtin_ctz* return [0, prec-1], except for
3673 when the argument is 0, but that is undefined behavior.
3674 If there is a ctz optab for this mode and
3675 CTZ_DEFINED_VALUE_AT_ZERO, include that in the range,
3676 otherwise just assume 0 won't be seen. */
3677 CASE_CFN_CTZ:
3678 arg = gimple_call_arg (stmt, 0);
3679 prec = TYPE_PRECISION (TREE_TYPE (arg));
3680 mini = 0;
3681 maxi = prec - 1;
3682 if (optab_handler (ctz_optab, TYPE_MODE (TREE_TYPE (arg)))
3683 != CODE_FOR_nothing
3684 && CTZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (TREE_TYPE (arg)),
3685 zerov))
3687 /* Handle only the two common values. */
3688 if (zerov == -1)
3689 mini = -1;
3690 else if (zerov == prec)
3691 maxi = prec;
3692 else
3693 /* Magic value to give up, unless vr0 proves
3694 arg is non-zero. */
3695 mini = -2;
3697 if (TREE_CODE (arg) == SSA_NAME)
3699 value_range *vr0 = get_value_range (arg);
3700 /* If arg is non-zero, then use [0, prec - 1]. */
3701 if ((vr0->type == VR_RANGE
3702 && integer_nonzerop (vr0->min))
3703 || (vr0->type == VR_ANTI_RANGE
3704 && integer_zerop (vr0->min)))
3706 mini = 0;
3707 maxi = prec - 1;
3709 /* If some high bits are known to be zero,
3710 we can decrease the result maximum. */
3711 if (vr0->type == VR_RANGE
3712 && TREE_CODE (vr0->max) == INTEGER_CST)
3714 maxi = tree_floor_log2 (vr0->max);
3715 /* For vr0 [0, 0] give up. */
3716 if (maxi == -1)
3717 break;
3720 if (mini == -2)
3721 break;
3722 goto bitop_builtin;
3723 /* __builtin_clrsb* returns [0, prec-1]. */
3724 CASE_CFN_CLRSB:
3725 arg = gimple_call_arg (stmt, 0);
3726 prec = TYPE_PRECISION (TREE_TYPE (arg));
3727 mini = 0;
3728 maxi = prec - 1;
3729 goto bitop_builtin;
3730 bitop_builtin:
3731 set_value_range (vr, VR_RANGE, build_int_cst (type, mini),
3732 build_int_cst (type, maxi), NULL);
3733 return;
3734 case CFN_UBSAN_CHECK_ADD:
3735 subcode = PLUS_EXPR;
3736 break;
3737 case CFN_UBSAN_CHECK_SUB:
3738 subcode = MINUS_EXPR;
3739 break;
3740 case CFN_UBSAN_CHECK_MUL:
3741 subcode = MULT_EXPR;
3742 break;
3743 case CFN_GOACC_DIM_SIZE:
3744 case CFN_GOACC_DIM_POS:
3745 /* Optimizing these two internal functions helps the loop
3746 optimizer eliminate outer comparisons. Size is [1,N]
3747 and pos is [0,N-1]. */
3749 bool is_pos = cfn == CFN_GOACC_DIM_POS;
3750 int axis = oacc_get_ifn_dim_arg (stmt);
3751 int size = oacc_get_fn_dim_size (current_function_decl, axis);
3753 if (!size)
3754 /* If it's dynamic, the backend might know a hardware
3755 limitation. */
3756 size = targetm.goacc.dim_limit (axis);
3758 tree type = TREE_TYPE (gimple_call_lhs (stmt));
3759 set_value_range (vr, VR_RANGE,
3760 build_int_cst (type, is_pos ? 0 : 1),
3761 size ? build_int_cst (type, size - is_pos)
3762 : vrp_val_max (type), NULL);
3764 return;
3765 case CFN_BUILT_IN_STRLEN:
3766 if (tree lhs = gimple_call_lhs (stmt))
3767 if (ptrdiff_type_node
3768 && (TYPE_PRECISION (ptrdiff_type_node)
3769 == TYPE_PRECISION (TREE_TYPE (lhs))))
3771 tree type = TREE_TYPE (lhs);
3772 tree max = vrp_val_max (ptrdiff_type_node);
3773 wide_int wmax = wi::to_wide (max, TYPE_PRECISION (TREE_TYPE (max)));
3774 tree range_min = build_zero_cst (type);
3775 tree range_max = wide_int_to_tree (type, wmax - 1);
3776 set_value_range (vr, VR_RANGE, range_min, range_max, NULL);
3777 return;
3779 break;
3780 default:
3781 break;
3783 if (subcode != ERROR_MARK)
3785 bool saved_flag_wrapv = flag_wrapv;
3786 /* Pretend the arithmetics is wrapping. If there is
3787 any overflow, we'll complain, but will actually do
3788 wrapping operation. */
3789 flag_wrapv = 1;
3790 extract_range_from_binary_expr (vr, subcode, type,
3791 gimple_call_arg (stmt, 0),
3792 gimple_call_arg (stmt, 1));
3793 flag_wrapv = saved_flag_wrapv;
3795 /* If for both arguments vrp_valueize returned non-NULL,
3796 this should have been already folded and if not, it
3797 wasn't folded because of overflow. Avoid removing the
3798 UBSAN_CHECK_* calls in that case. */
3799 if (vr->type == VR_RANGE
3800 && (vr->min == vr->max
3801 || operand_equal_p (vr->min, vr->max, 0)))
3802 set_value_range_to_varying (vr);
3803 return;
3806 /* Handle extraction of the two results (result of arithmetics and
3807 a flag whether arithmetics overflowed) from {ADD,SUB,MUL}_OVERFLOW
3808 internal function. Similarly from ATOMIC_COMPARE_EXCHANGE. */
3809 else if (is_gimple_assign (stmt)
3810 && (gimple_assign_rhs_code (stmt) == REALPART_EXPR
3811 || gimple_assign_rhs_code (stmt) == IMAGPART_EXPR)
3812 && INTEGRAL_TYPE_P (type))
3814 enum tree_code code = gimple_assign_rhs_code (stmt);
3815 tree op = gimple_assign_rhs1 (stmt);
3816 if (TREE_CODE (op) == code && TREE_CODE (TREE_OPERAND (op, 0)) == SSA_NAME)
3818 gimple *g = SSA_NAME_DEF_STMT (TREE_OPERAND (op, 0));
3819 if (is_gimple_call (g) && gimple_call_internal_p (g))
3821 enum tree_code subcode = ERROR_MARK;
3822 switch (gimple_call_internal_fn (g))
3824 case IFN_ADD_OVERFLOW:
3825 subcode = PLUS_EXPR;
3826 break;
3827 case IFN_SUB_OVERFLOW:
3828 subcode = MINUS_EXPR;
3829 break;
3830 case IFN_MUL_OVERFLOW:
3831 subcode = MULT_EXPR;
3832 break;
3833 case IFN_ATOMIC_COMPARE_EXCHANGE:
3834 if (code == IMAGPART_EXPR)
3836 /* This is the boolean return value whether compare and
3837 exchange changed anything or not. */
3838 set_value_range (vr, VR_RANGE, build_int_cst (type, 0),
3839 build_int_cst (type, 1), NULL);
3840 return;
3842 break;
3843 default:
3844 break;
3846 if (subcode != ERROR_MARK)
3848 tree op0 = gimple_call_arg (g, 0);
3849 tree op1 = gimple_call_arg (g, 1);
3850 if (code == IMAGPART_EXPR)
3852 bool ovf = false;
3853 if (check_for_binary_op_overflow (subcode, type,
3854 op0, op1, &ovf))
3855 set_value_range_to_value (vr,
3856 build_int_cst (type, ovf),
3857 NULL);
3858 else if (TYPE_PRECISION (type) == 1
3859 && !TYPE_UNSIGNED (type))
3860 set_value_range_to_varying (vr);
3861 else
3862 set_value_range (vr, VR_RANGE, build_int_cst (type, 0),
3863 build_int_cst (type, 1), NULL);
3865 else if (types_compatible_p (type, TREE_TYPE (op0))
3866 && types_compatible_p (type, TREE_TYPE (op1)))
3868 bool saved_flag_wrapv = flag_wrapv;
3869 /* Pretend the arithmetics is wrapping. If there is
3870 any overflow, IMAGPART_EXPR will be set. */
3871 flag_wrapv = 1;
3872 extract_range_from_binary_expr (vr, subcode, type,
3873 op0, op1);
3874 flag_wrapv = saved_flag_wrapv;
3876 else
3878 value_range vr0 = VR_INITIALIZER;
3879 value_range vr1 = VR_INITIALIZER;
3880 bool saved_flag_wrapv = flag_wrapv;
3881 /* Pretend the arithmetics is wrapping. If there is
3882 any overflow, IMAGPART_EXPR will be set. */
3883 flag_wrapv = 1;
3884 extract_range_from_unary_expr (&vr0, NOP_EXPR,
3885 type, op0);
3886 extract_range_from_unary_expr (&vr1, NOP_EXPR,
3887 type, op1);
3888 extract_range_from_binary_expr_1 (vr, subcode, type,
3889 &vr0, &vr1);
3890 flag_wrapv = saved_flag_wrapv;
3892 return;
3897 if (INTEGRAL_TYPE_P (type)
3898 && gimple_stmt_nonnegative_warnv_p (stmt, &sop))
3899 set_value_range_to_nonnegative (vr, type);
3900 else if (vrp_stmt_computes_nonzero (stmt))
3901 set_value_range_to_nonnull (vr, type);
3902 else
3903 set_value_range_to_varying (vr);
3907 /* Try to compute a useful range out of assignment STMT and store it
3908 in *VR. */
3910 static void
3911 extract_range_from_assignment (value_range *vr, gassign *stmt)
3913 enum tree_code code = gimple_assign_rhs_code (stmt);
3915 if (code == ASSERT_EXPR)
3916 extract_range_from_assert (vr, gimple_assign_rhs1 (stmt));
3917 else if (code == SSA_NAME)
3918 extract_range_from_ssa_name (vr, gimple_assign_rhs1 (stmt));
3919 else if (TREE_CODE_CLASS (code) == tcc_binary)
3920 extract_range_from_binary_expr (vr, gimple_assign_rhs_code (stmt),
3921 gimple_expr_type (stmt),
3922 gimple_assign_rhs1 (stmt),
3923 gimple_assign_rhs2 (stmt));
3924 else if (TREE_CODE_CLASS (code) == tcc_unary)
3925 extract_range_from_unary_expr (vr, gimple_assign_rhs_code (stmt),
3926 gimple_expr_type (stmt),
3927 gimple_assign_rhs1 (stmt));
3928 else if (code == COND_EXPR)
3929 extract_range_from_cond_expr (vr, stmt);
3930 else if (TREE_CODE_CLASS (code) == tcc_comparison)
3931 extract_range_from_comparison (vr, gimple_assign_rhs_code (stmt),
3932 gimple_expr_type (stmt),
3933 gimple_assign_rhs1 (stmt),
3934 gimple_assign_rhs2 (stmt));
3935 else if (get_gimple_rhs_class (code) == GIMPLE_SINGLE_RHS
3936 && is_gimple_min_invariant (gimple_assign_rhs1 (stmt)))
3937 set_value_range_to_value (vr, gimple_assign_rhs1 (stmt), NULL);
3938 else
3939 set_value_range_to_varying (vr);
3941 if (vr->type == VR_VARYING)
3942 extract_range_basic (vr, stmt);
3945 /* Given a range VR, a LOOP and a variable VAR, determine whether it
3946 would be profitable to adjust VR using scalar evolution information
3947 for VAR. If so, update VR with the new limits. */
3949 static void
3950 adjust_range_with_scev (value_range *vr, struct loop *loop,
3951 gimple *stmt, tree var)
3953 tree init, step, chrec, tmin, tmax, min, max, type, tem;
3954 enum ev_direction dir;
3956 /* TODO. Don't adjust anti-ranges. An anti-range may provide
3957 better opportunities than a regular range, but I'm not sure. */
3958 if (vr->type == VR_ANTI_RANGE)
3959 return;
3961 chrec = instantiate_parameters (loop, analyze_scalar_evolution (loop, var));
3963 /* Like in PR19590, scev can return a constant function. */
3964 if (is_gimple_min_invariant (chrec))
3966 set_value_range_to_value (vr, chrec, vr->equiv);
3967 return;
3970 if (TREE_CODE (chrec) != POLYNOMIAL_CHREC)
3971 return;
3973 init = initial_condition_in_loop_num (chrec, loop->num);
3974 tem = op_with_constant_singleton_value_range (init);
3975 if (tem)
3976 init = tem;
3977 step = evolution_part_in_loop_num (chrec, loop->num);
3978 tem = op_with_constant_singleton_value_range (step);
3979 if (tem)
3980 step = tem;
3982 /* If STEP is symbolic, we can't know whether INIT will be the
3983 minimum or maximum value in the range. Also, unless INIT is
3984 a simple expression, compare_values and possibly other functions
3985 in tree-vrp won't be able to handle it. */
3986 if (step == NULL_TREE
3987 || !is_gimple_min_invariant (step)
3988 || !valid_value_p (init))
3989 return;
3991 dir = scev_direction (chrec);
3992 if (/* Do not adjust ranges if we do not know whether the iv increases
3993 or decreases, ... */
3994 dir == EV_DIR_UNKNOWN
3995 /* ... or if it may wrap. */
3996 || scev_probably_wraps_p (NULL_TREE, init, step, stmt,
3997 get_chrec_loop (chrec), true))
3998 return;
4000 type = TREE_TYPE (var);
4001 if (POINTER_TYPE_P (type) || !TYPE_MIN_VALUE (type))
4002 tmin = lower_bound_in_type (type, type);
4003 else
4004 tmin = TYPE_MIN_VALUE (type);
4005 if (POINTER_TYPE_P (type) || !TYPE_MAX_VALUE (type))
4006 tmax = upper_bound_in_type (type, type);
4007 else
4008 tmax = TYPE_MAX_VALUE (type);
4010 /* Try to use estimated number of iterations for the loop to constrain the
4011 final value in the evolution. */
4012 if (TREE_CODE (step) == INTEGER_CST
4013 && is_gimple_val (init)
4014 && (TREE_CODE (init) != SSA_NAME
4015 || get_value_range (init)->type == VR_RANGE))
4017 widest_int nit;
4019 /* We are only entering here for loop header PHI nodes, so using
4020 the number of latch executions is the correct thing to use. */
4021 if (max_loop_iterations (loop, &nit))
4023 value_range maxvr = VR_INITIALIZER;
4024 signop sgn = TYPE_SIGN (TREE_TYPE (step));
4025 bool overflow;
4027 widest_int wtmp = wi::mul (wi::to_widest (step), nit, sgn,
4028 &overflow);
4029 /* If the multiplication overflowed we can't do a meaningful
4030 adjustment. Likewise if the result doesn't fit in the type
4031 of the induction variable. For a signed type we have to
4032 check whether the result has the expected signedness which
4033 is that of the step as number of iterations is unsigned. */
4034 if (!overflow
4035 && wi::fits_to_tree_p (wtmp, TREE_TYPE (init))
4036 && (sgn == UNSIGNED
4037 || wi::gts_p (wtmp, 0) == wi::gts_p (step, 0)))
4039 tem = wide_int_to_tree (TREE_TYPE (init), wtmp);
4040 extract_range_from_binary_expr (&maxvr, PLUS_EXPR,
4041 TREE_TYPE (init), init, tem);
4042 /* Likewise if the addition did. */
4043 if (maxvr.type == VR_RANGE)
4045 value_range initvr = VR_INITIALIZER;
4047 if (TREE_CODE (init) == SSA_NAME)
4048 initvr = *(get_value_range (init));
4049 else if (is_gimple_min_invariant (init))
4050 set_value_range_to_value (&initvr, init, NULL);
4051 else
4052 return;
4054 /* Check if init + nit * step overflows. Though we checked
4055 scev {init, step}_loop doesn't wrap, it is not enough
4056 because the loop may exit immediately. Overflow could
4057 happen in the plus expression in this case. */
4058 if ((dir == EV_DIR_DECREASES
4059 && compare_values (maxvr.min, initvr.min) != -1)
4060 || (dir == EV_DIR_GROWS
4061 && compare_values (maxvr.max, initvr.max) != 1))
4062 return;
4064 tmin = maxvr.min;
4065 tmax = maxvr.max;
4071 if (vr->type == VR_VARYING || vr->type == VR_UNDEFINED)
4073 min = tmin;
4074 max = tmax;
4076 /* For VARYING or UNDEFINED ranges, just about anything we get
4077 from scalar evolutions should be better. */
4079 if (dir == EV_DIR_DECREASES)
4080 max = init;
4081 else
4082 min = init;
4084 else if (vr->type == VR_RANGE)
4086 min = vr->min;
4087 max = vr->max;
4089 if (dir == EV_DIR_DECREASES)
4091 /* INIT is the maximum value. If INIT is lower than VR->MAX
4092 but no smaller than VR->MIN, set VR->MAX to INIT. */
4093 if (compare_values (init, max) == -1)
4094 max = init;
4096 /* According to the loop information, the variable does not
4097 overflow. */
4098 if (compare_values (min, tmin) == -1)
4099 min = tmin;
4102 else
4104 /* If INIT is bigger than VR->MIN, set VR->MIN to INIT. */
4105 if (compare_values (init, min) == 1)
4106 min = init;
4108 if (compare_values (tmax, max) == -1)
4109 max = tmax;
4112 else
4113 return;
4115 /* If we just created an invalid range with the minimum
4116 greater than the maximum, we fail conservatively.
4117 This should happen only in unreachable
4118 parts of code, or for invalid programs. */
4119 if (compare_values (min, max) == 1)
4120 return;
4122 /* Even for valid range info, sometimes overflow flag will leak in.
4123 As GIMPLE IL should have no constants with TREE_OVERFLOW set, we
4124 drop them. */
4125 if (TREE_OVERFLOW_P (min))
4126 min = drop_tree_overflow (min);
4127 if (TREE_OVERFLOW_P (max))
4128 max = drop_tree_overflow (max);
4130 set_value_range (vr, VR_RANGE, min, max, vr->equiv);
4134 /* Given two numeric value ranges VR0, VR1 and a comparison code COMP:
4136 - Return BOOLEAN_TRUE_NODE if VR0 COMP VR1 always returns true for
4137 all the values in the ranges.
4139 - Return BOOLEAN_FALSE_NODE if the comparison always returns false.
4141 - Return NULL_TREE if it is not always possible to determine the
4142 value of the comparison.
4144 Also set *STRICT_OVERFLOW_P to indicate whether comparision evaluation
4145 assumed signed overflow is undefined. */
4148 static tree
4149 compare_ranges (enum tree_code comp, value_range *vr0, value_range *vr1,
4150 bool *strict_overflow_p)
4152 /* VARYING or UNDEFINED ranges cannot be compared. */
4153 if (vr0->type == VR_VARYING
4154 || vr0->type == VR_UNDEFINED
4155 || vr1->type == VR_VARYING
4156 || vr1->type == VR_UNDEFINED)
4157 return NULL_TREE;
4159 /* Anti-ranges need to be handled separately. */
4160 if (vr0->type == VR_ANTI_RANGE || vr1->type == VR_ANTI_RANGE)
4162 /* If both are anti-ranges, then we cannot compute any
4163 comparison. */
4164 if (vr0->type == VR_ANTI_RANGE && vr1->type == VR_ANTI_RANGE)
4165 return NULL_TREE;
4167 /* These comparisons are never statically computable. */
4168 if (comp == GT_EXPR
4169 || comp == GE_EXPR
4170 || comp == LT_EXPR
4171 || comp == LE_EXPR)
4172 return NULL_TREE;
4174 /* Equality can be computed only between a range and an
4175 anti-range. ~[VAL1, VAL2] == [VAL1, VAL2] is always false. */
4176 if (vr0->type == VR_RANGE)
4178 /* To simplify processing, make VR0 the anti-range. */
4179 value_range *tmp = vr0;
4180 vr0 = vr1;
4181 vr1 = tmp;
4184 gcc_assert (comp == NE_EXPR || comp == EQ_EXPR);
4186 if (compare_values_warnv (vr0->min, vr1->min, strict_overflow_p) == 0
4187 && compare_values_warnv (vr0->max, vr1->max, strict_overflow_p) == 0)
4188 return (comp == NE_EXPR) ? boolean_true_node : boolean_false_node;
4190 return NULL_TREE;
4193 /* Simplify processing. If COMP is GT_EXPR or GE_EXPR, switch the
4194 operands around and change the comparison code. */
4195 if (comp == GT_EXPR || comp == GE_EXPR)
4197 comp = (comp == GT_EXPR) ? LT_EXPR : LE_EXPR;
4198 std::swap (vr0, vr1);
4201 if (comp == EQ_EXPR)
4203 /* Equality may only be computed if both ranges represent
4204 exactly one value. */
4205 if (compare_values_warnv (vr0->min, vr0->max, strict_overflow_p) == 0
4206 && compare_values_warnv (vr1->min, vr1->max, strict_overflow_p) == 0)
4208 int cmp_min = compare_values_warnv (vr0->min, vr1->min,
4209 strict_overflow_p);
4210 int cmp_max = compare_values_warnv (vr0->max, vr1->max,
4211 strict_overflow_p);
4212 if (cmp_min == 0 && cmp_max == 0)
4213 return boolean_true_node;
4214 else if (cmp_min != -2 && cmp_max != -2)
4215 return boolean_false_node;
4217 /* If [V0_MIN, V1_MAX] < [V1_MIN, V1_MAX] then V0 != V1. */
4218 else if (compare_values_warnv (vr0->min, vr1->max,
4219 strict_overflow_p) == 1
4220 || compare_values_warnv (vr1->min, vr0->max,
4221 strict_overflow_p) == 1)
4222 return boolean_false_node;
4224 return NULL_TREE;
4226 else if (comp == NE_EXPR)
4228 int cmp1, cmp2;
4230 /* If VR0 is completely to the left or completely to the right
4231 of VR1, they are always different. Notice that we need to
4232 make sure that both comparisons yield similar results to
4233 avoid comparing values that cannot be compared at
4234 compile-time. */
4235 cmp1 = compare_values_warnv (vr0->max, vr1->min, strict_overflow_p);
4236 cmp2 = compare_values_warnv (vr0->min, vr1->max, strict_overflow_p);
4237 if ((cmp1 == -1 && cmp2 == -1) || (cmp1 == 1 && cmp2 == 1))
4238 return boolean_true_node;
4240 /* If VR0 and VR1 represent a single value and are identical,
4241 return false. */
4242 else if (compare_values_warnv (vr0->min, vr0->max,
4243 strict_overflow_p) == 0
4244 && compare_values_warnv (vr1->min, vr1->max,
4245 strict_overflow_p) == 0
4246 && compare_values_warnv (vr0->min, vr1->min,
4247 strict_overflow_p) == 0
4248 && compare_values_warnv (vr0->max, vr1->max,
4249 strict_overflow_p) == 0)
4250 return boolean_false_node;
4252 /* Otherwise, they may or may not be different. */
4253 else
4254 return NULL_TREE;
4256 else if (comp == LT_EXPR || comp == LE_EXPR)
4258 int tst;
4260 /* If VR0 is to the left of VR1, return true. */
4261 tst = compare_values_warnv (vr0->max, vr1->min, strict_overflow_p);
4262 if ((comp == LT_EXPR && tst == -1)
4263 || (comp == LE_EXPR && (tst == -1 || tst == 0)))
4264 return boolean_true_node;
4266 /* If VR0 is to the right of VR1, return false. */
4267 tst = compare_values_warnv (vr0->min, vr1->max, strict_overflow_p);
4268 if ((comp == LT_EXPR && (tst == 0 || tst == 1))
4269 || (comp == LE_EXPR && tst == 1))
4270 return boolean_false_node;
4272 /* Otherwise, we don't know. */
4273 return NULL_TREE;
4276 gcc_unreachable ();
4280 /* Given a value range VR, a value VAL and a comparison code COMP, return
4281 BOOLEAN_TRUE_NODE if VR COMP VAL always returns true for all the
4282 values in VR. Return BOOLEAN_FALSE_NODE if the comparison
4283 always returns false. Return NULL_TREE if it is not always
4284 possible to determine the value of the comparison. Also set
4285 *STRICT_OVERFLOW_P to indicate whether comparision evaluation
4286 assumed signed overflow is undefined. */
4288 static tree
4289 compare_range_with_value (enum tree_code comp, value_range *vr, tree val,
4290 bool *strict_overflow_p)
4292 if (vr->type == VR_VARYING || vr->type == VR_UNDEFINED)
4293 return NULL_TREE;
4295 /* Anti-ranges need to be handled separately. */
4296 if (vr->type == VR_ANTI_RANGE)
4298 /* For anti-ranges, the only predicates that we can compute at
4299 compile time are equality and inequality. */
4300 if (comp == GT_EXPR
4301 || comp == GE_EXPR
4302 || comp == LT_EXPR
4303 || comp == LE_EXPR)
4304 return NULL_TREE;
4306 /* ~[VAL_1, VAL_2] OP VAL is known if VAL_1 <= VAL <= VAL_2. */
4307 if (value_inside_range (val, vr->min, vr->max) == 1)
4308 return (comp == NE_EXPR) ? boolean_true_node : boolean_false_node;
4310 return NULL_TREE;
4313 if (comp == EQ_EXPR)
4315 /* EQ_EXPR may only be computed if VR represents exactly
4316 one value. */
4317 if (compare_values_warnv (vr->min, vr->max, strict_overflow_p) == 0)
4319 int cmp = compare_values_warnv (vr->min, val, strict_overflow_p);
4320 if (cmp == 0)
4321 return boolean_true_node;
4322 else if (cmp == -1 || cmp == 1 || cmp == 2)
4323 return boolean_false_node;
4325 else if (compare_values_warnv (val, vr->min, strict_overflow_p) == -1
4326 || compare_values_warnv (vr->max, val, strict_overflow_p) == -1)
4327 return boolean_false_node;
4329 return NULL_TREE;
4331 else if (comp == NE_EXPR)
4333 /* If VAL is not inside VR, then they are always different. */
4334 if (compare_values_warnv (vr->max, val, strict_overflow_p) == -1
4335 || compare_values_warnv (vr->min, val, strict_overflow_p) == 1)
4336 return boolean_true_node;
4338 /* If VR represents exactly one value equal to VAL, then return
4339 false. */
4340 if (compare_values_warnv (vr->min, vr->max, strict_overflow_p) == 0
4341 && compare_values_warnv (vr->min, val, strict_overflow_p) == 0)
4342 return boolean_false_node;
4344 /* Otherwise, they may or may not be different. */
4345 return NULL_TREE;
4347 else if (comp == LT_EXPR || comp == LE_EXPR)
4349 int tst;
4351 /* If VR is to the left of VAL, return true. */
4352 tst = compare_values_warnv (vr->max, val, strict_overflow_p);
4353 if ((comp == LT_EXPR && tst == -1)
4354 || (comp == LE_EXPR && (tst == -1 || tst == 0)))
4355 return boolean_true_node;
4357 /* If VR is to the right of VAL, return false. */
4358 tst = compare_values_warnv (vr->min, val, strict_overflow_p);
4359 if ((comp == LT_EXPR && (tst == 0 || tst == 1))
4360 || (comp == LE_EXPR && tst == 1))
4361 return boolean_false_node;
4363 /* Otherwise, we don't know. */
4364 return NULL_TREE;
4366 else if (comp == GT_EXPR || comp == GE_EXPR)
4368 int tst;
4370 /* If VR is to the right of VAL, return true. */
4371 tst = compare_values_warnv (vr->min, val, strict_overflow_p);
4372 if ((comp == GT_EXPR && tst == 1)
4373 || (comp == GE_EXPR && (tst == 0 || tst == 1)))
4374 return boolean_true_node;
4376 /* If VR is to the left of VAL, return false. */
4377 tst = compare_values_warnv (vr->max, val, strict_overflow_p);
4378 if ((comp == GT_EXPR && (tst == -1 || tst == 0))
4379 || (comp == GE_EXPR && tst == -1))
4380 return boolean_false_node;
4382 /* Otherwise, we don't know. */
4383 return NULL_TREE;
4386 gcc_unreachable ();
4390 /* Debugging dumps. */
4392 void dump_value_range (FILE *, const value_range *);
4393 void debug_value_range (value_range *);
4394 void dump_all_value_ranges (FILE *);
4395 void debug_all_value_ranges (void);
4396 void dump_vr_equiv (FILE *, bitmap);
4397 void debug_vr_equiv (bitmap);
4400 /* Dump value range VR to FILE. */
4402 void
4403 dump_value_range (FILE *file, const value_range *vr)
4405 if (vr == NULL)
4406 fprintf (file, "[]");
4407 else if (vr->type == VR_UNDEFINED)
4408 fprintf (file, "UNDEFINED");
4409 else if (vr->type == VR_RANGE || vr->type == VR_ANTI_RANGE)
4411 tree type = TREE_TYPE (vr->min);
4413 fprintf (file, "%s[", (vr->type == VR_ANTI_RANGE) ? "~" : "");
4415 if (INTEGRAL_TYPE_P (type)
4416 && !TYPE_UNSIGNED (type)
4417 && vrp_val_is_min (vr->min))
4418 fprintf (file, "-INF");
4419 else
4420 print_generic_expr (file, vr->min);
4422 fprintf (file, ", ");
4424 if (INTEGRAL_TYPE_P (type)
4425 && vrp_val_is_max (vr->max))
4426 fprintf (file, "+INF");
4427 else
4428 print_generic_expr (file, vr->max);
4430 fprintf (file, "]");
4432 if (vr->equiv)
4434 bitmap_iterator bi;
4435 unsigned i, c = 0;
4437 fprintf (file, " EQUIVALENCES: { ");
4439 EXECUTE_IF_SET_IN_BITMAP (vr->equiv, 0, i, bi)
4441 print_generic_expr (file, ssa_name (i));
4442 fprintf (file, " ");
4443 c++;
4446 fprintf (file, "} (%u elements)", c);
4449 else if (vr->type == VR_VARYING)
4450 fprintf (file, "VARYING");
4451 else
4452 fprintf (file, "INVALID RANGE");
4456 /* Dump value range VR to stderr. */
4458 DEBUG_FUNCTION void
4459 debug_value_range (value_range *vr)
4461 dump_value_range (stderr, vr);
4462 fprintf (stderr, "\n");
4466 /* Dump value ranges of all SSA_NAMEs to FILE. */
4468 void
4469 dump_all_value_ranges (FILE *file)
4471 size_t i;
4473 for (i = 0; i < num_vr_values; i++)
4475 if (vr_value[i])
4477 print_generic_expr (file, ssa_name (i));
4478 fprintf (file, ": ");
4479 dump_value_range (file, vr_value[i]);
4480 fprintf (file, "\n");
4484 fprintf (file, "\n");
4488 /* Dump all value ranges to stderr. */
4490 DEBUG_FUNCTION void
4491 debug_all_value_ranges (void)
4493 dump_all_value_ranges (stderr);
4497 /* Given a COND_EXPR COND of the form 'V OP W', and an SSA name V,
4498 create a new SSA name N and return the assertion assignment
4499 'N = ASSERT_EXPR <V, V OP W>'. */
4501 static gimple *
4502 build_assert_expr_for (tree cond, tree v)
4504 tree a;
4505 gassign *assertion;
4507 gcc_assert (TREE_CODE (v) == SSA_NAME
4508 && COMPARISON_CLASS_P (cond));
4510 a = build2 (ASSERT_EXPR, TREE_TYPE (v), v, cond);
4511 assertion = gimple_build_assign (NULL_TREE, a);
4513 /* The new ASSERT_EXPR, creates a new SSA name that replaces the
4514 operand of the ASSERT_EXPR. Create it so the new name and the old one
4515 are registered in the replacement table so that we can fix the SSA web
4516 after adding all the ASSERT_EXPRs. */
4517 create_new_def_for (v, assertion, NULL);
4519 return assertion;
4523 /* Return false if EXPR is a predicate expression involving floating
4524 point values. */
4526 static inline bool
4527 fp_predicate (gimple *stmt)
4529 GIMPLE_CHECK (stmt, GIMPLE_COND);
4531 return FLOAT_TYPE_P (TREE_TYPE (gimple_cond_lhs (stmt)));
4534 /* If the range of values taken by OP can be inferred after STMT executes,
4535 return the comparison code (COMP_CODE_P) and value (VAL_P) that
4536 describes the inferred range. Return true if a range could be
4537 inferred. */
4539 static bool
4540 infer_value_range (gimple *stmt, tree op, tree_code *comp_code_p, tree *val_p)
4542 *val_p = NULL_TREE;
4543 *comp_code_p = ERROR_MARK;
4545 /* Do not attempt to infer anything in names that flow through
4546 abnormal edges. */
4547 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op))
4548 return false;
4550 /* If STMT is the last statement of a basic block with no normal
4551 successors, there is no point inferring anything about any of its
4552 operands. We would not be able to find a proper insertion point
4553 for the assertion, anyway. */
4554 if (stmt_ends_bb_p (stmt))
4556 edge_iterator ei;
4557 edge e;
4559 FOR_EACH_EDGE (e, ei, gimple_bb (stmt)->succs)
4560 if (!(e->flags & (EDGE_ABNORMAL|EDGE_EH)))
4561 break;
4562 if (e == NULL)
4563 return false;
4566 if (infer_nonnull_range (stmt, op))
4568 *val_p = build_int_cst (TREE_TYPE (op), 0);
4569 *comp_code_p = NE_EXPR;
4570 return true;
4573 return false;
4577 void dump_asserts_for (FILE *, tree);
4578 void debug_asserts_for (tree);
4579 void dump_all_asserts (FILE *);
4580 void debug_all_asserts (void);
4582 /* Dump all the registered assertions for NAME to FILE. */
4584 void
4585 dump_asserts_for (FILE *file, tree name)
4587 assert_locus *loc;
4589 fprintf (file, "Assertions to be inserted for ");
4590 print_generic_expr (file, name);
4591 fprintf (file, "\n");
4593 loc = asserts_for[SSA_NAME_VERSION (name)];
4594 while (loc)
4596 fprintf (file, "\t");
4597 print_gimple_stmt (file, gsi_stmt (loc->si), 0);
4598 fprintf (file, "\n\tBB #%d", loc->bb->index);
4599 if (loc->e)
4601 fprintf (file, "\n\tEDGE %d->%d", loc->e->src->index,
4602 loc->e->dest->index);
4603 dump_edge_info (file, loc->e, dump_flags, 0);
4605 fprintf (file, "\n\tPREDICATE: ");
4606 print_generic_expr (file, loc->expr);
4607 fprintf (file, " %s ", get_tree_code_name (loc->comp_code));
4608 print_generic_expr (file, loc->val);
4609 fprintf (file, "\n\n");
4610 loc = loc->next;
4613 fprintf (file, "\n");
4617 /* Dump all the registered assertions for NAME to stderr. */
4619 DEBUG_FUNCTION void
4620 debug_asserts_for (tree name)
4622 dump_asserts_for (stderr, name);
4626 /* Dump all the registered assertions for all the names to FILE. */
4628 void
4629 dump_all_asserts (FILE *file)
4631 unsigned i;
4632 bitmap_iterator bi;
4634 fprintf (file, "\nASSERT_EXPRs to be inserted\n\n");
4635 EXECUTE_IF_SET_IN_BITMAP (need_assert_for, 0, i, bi)
4636 dump_asserts_for (file, ssa_name (i));
4637 fprintf (file, "\n");
4641 /* Dump all the registered assertions for all the names to stderr. */
4643 DEBUG_FUNCTION void
4644 debug_all_asserts (void)
4646 dump_all_asserts (stderr);
4649 /* Push the assert info for NAME, EXPR, COMP_CODE and VAL to ASSERTS. */
4651 static void
4652 add_assert_info (vec<assert_info> &asserts,
4653 tree name, tree expr, enum tree_code comp_code, tree val)
4655 assert_info info;
4656 info.comp_code = comp_code;
4657 info.name = name;
4658 info.val = val;
4659 info.expr = expr;
4660 asserts.safe_push (info);
4663 /* If NAME doesn't have an ASSERT_EXPR registered for asserting
4664 'EXPR COMP_CODE VAL' at a location that dominates block BB or
4665 E->DEST, then register this location as a possible insertion point
4666 for ASSERT_EXPR <NAME, EXPR COMP_CODE VAL>.
4668 BB, E and SI provide the exact insertion point for the new
4669 ASSERT_EXPR. If BB is NULL, then the ASSERT_EXPR is to be inserted
4670 on edge E. Otherwise, if E is NULL, the ASSERT_EXPR is inserted on
4671 BB. If SI points to a COND_EXPR or a SWITCH_EXPR statement, then E
4672 must not be NULL. */
4674 static void
4675 register_new_assert_for (tree name, tree expr,
4676 enum tree_code comp_code,
4677 tree val,
4678 basic_block bb,
4679 edge e,
4680 gimple_stmt_iterator si)
4682 assert_locus *n, *loc, *last_loc;
4683 basic_block dest_bb;
4685 gcc_checking_assert (bb == NULL || e == NULL);
4687 if (e == NULL)
4688 gcc_checking_assert (gimple_code (gsi_stmt (si)) != GIMPLE_COND
4689 && gimple_code (gsi_stmt (si)) != GIMPLE_SWITCH);
4691 /* Never build an assert comparing against an integer constant with
4692 TREE_OVERFLOW set. This confuses our undefined overflow warning
4693 machinery. */
4694 if (TREE_OVERFLOW_P (val))
4695 val = drop_tree_overflow (val);
4697 /* The new assertion A will be inserted at BB or E. We need to
4698 determine if the new location is dominated by a previously
4699 registered location for A. If we are doing an edge insertion,
4700 assume that A will be inserted at E->DEST. Note that this is not
4701 necessarily true.
4703 If E is a critical edge, it will be split. But even if E is
4704 split, the new block will dominate the same set of blocks that
4705 E->DEST dominates.
4707 The reverse, however, is not true, blocks dominated by E->DEST
4708 will not be dominated by the new block created to split E. So,
4709 if the insertion location is on a critical edge, we will not use
4710 the new location to move another assertion previously registered
4711 at a block dominated by E->DEST. */
4712 dest_bb = (bb) ? bb : e->dest;
4714 /* If NAME already has an ASSERT_EXPR registered for COMP_CODE and
4715 VAL at a block dominating DEST_BB, then we don't need to insert a new
4716 one. Similarly, if the same assertion already exists at a block
4717 dominated by DEST_BB and the new location is not on a critical
4718 edge, then update the existing location for the assertion (i.e.,
4719 move the assertion up in the dominance tree).
4721 Note, this is implemented as a simple linked list because there
4722 should not be more than a handful of assertions registered per
4723 name. If this becomes a performance problem, a table hashed by
4724 COMP_CODE and VAL could be implemented. */
4725 loc = asserts_for[SSA_NAME_VERSION (name)];
4726 last_loc = loc;
4727 while (loc)
4729 if (loc->comp_code == comp_code
4730 && (loc->val == val
4731 || operand_equal_p (loc->val, val, 0))
4732 && (loc->expr == expr
4733 || operand_equal_p (loc->expr, expr, 0)))
4735 /* If E is not a critical edge and DEST_BB
4736 dominates the existing location for the assertion, move
4737 the assertion up in the dominance tree by updating its
4738 location information. */
4739 if ((e == NULL || !EDGE_CRITICAL_P (e))
4740 && dominated_by_p (CDI_DOMINATORS, loc->bb, dest_bb))
4742 loc->bb = dest_bb;
4743 loc->e = e;
4744 loc->si = si;
4745 return;
4749 /* Update the last node of the list and move to the next one. */
4750 last_loc = loc;
4751 loc = loc->next;
4754 /* If we didn't find an assertion already registered for
4755 NAME COMP_CODE VAL, add a new one at the end of the list of
4756 assertions associated with NAME. */
4757 n = XNEW (struct assert_locus);
4758 n->bb = dest_bb;
4759 n->e = e;
4760 n->si = si;
4761 n->comp_code = comp_code;
4762 n->val = val;
4763 n->expr = expr;
4764 n->next = NULL;
4766 if (last_loc)
4767 last_loc->next = n;
4768 else
4769 asserts_for[SSA_NAME_VERSION (name)] = n;
4771 bitmap_set_bit (need_assert_for, SSA_NAME_VERSION (name));
4774 /* (COND_OP0 COND_CODE COND_OP1) is a predicate which uses NAME.
4775 Extract a suitable test code and value and store them into *CODE_P and
4776 *VAL_P so the predicate is normalized to NAME *CODE_P *VAL_P.
4778 If no extraction was possible, return FALSE, otherwise return TRUE.
4780 If INVERT is true, then we invert the result stored into *CODE_P. */
4782 static bool
4783 extract_code_and_val_from_cond_with_ops (tree name, enum tree_code cond_code,
4784 tree cond_op0, tree cond_op1,
4785 bool invert, enum tree_code *code_p,
4786 tree *val_p)
4788 enum tree_code comp_code;
4789 tree val;
4791 /* Otherwise, we have a comparison of the form NAME COMP VAL
4792 or VAL COMP NAME. */
4793 if (name == cond_op1)
4795 /* If the predicate is of the form VAL COMP NAME, flip
4796 COMP around because we need to register NAME as the
4797 first operand in the predicate. */
4798 comp_code = swap_tree_comparison (cond_code);
4799 val = cond_op0;
4801 else if (name == cond_op0)
4803 /* The comparison is of the form NAME COMP VAL, so the
4804 comparison code remains unchanged. */
4805 comp_code = cond_code;
4806 val = cond_op1;
4808 else
4809 gcc_unreachable ();
4811 /* Invert the comparison code as necessary. */
4812 if (invert)
4813 comp_code = invert_tree_comparison (comp_code, 0);
4815 /* VRP only handles integral and pointer types. */
4816 if (! INTEGRAL_TYPE_P (TREE_TYPE (val))
4817 && ! POINTER_TYPE_P (TREE_TYPE (val)))
4818 return false;
4820 /* Do not register always-false predicates.
4821 FIXME: this works around a limitation in fold() when dealing with
4822 enumerations. Given 'enum { N1, N2 } x;', fold will not
4823 fold 'if (x > N2)' to 'if (0)'. */
4824 if ((comp_code == GT_EXPR || comp_code == LT_EXPR)
4825 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
4827 tree min = TYPE_MIN_VALUE (TREE_TYPE (val));
4828 tree max = TYPE_MAX_VALUE (TREE_TYPE (val));
4830 if (comp_code == GT_EXPR
4831 && (!max
4832 || compare_values (val, max) == 0))
4833 return false;
4835 if (comp_code == LT_EXPR
4836 && (!min
4837 || compare_values (val, min) == 0))
4838 return false;
4840 *code_p = comp_code;
4841 *val_p = val;
4842 return true;
4845 /* Find out smallest RES where RES > VAL && (RES & MASK) == RES, if any
4846 (otherwise return VAL). VAL and MASK must be zero-extended for
4847 precision PREC. If SGNBIT is non-zero, first xor VAL with SGNBIT
4848 (to transform signed values into unsigned) and at the end xor
4849 SGNBIT back. */
4851 static wide_int
4852 masked_increment (const wide_int &val_in, const wide_int &mask,
4853 const wide_int &sgnbit, unsigned int prec)
4855 wide_int bit = wi::one (prec), res;
4856 unsigned int i;
4858 wide_int val = val_in ^ sgnbit;
4859 for (i = 0; i < prec; i++, bit += bit)
4861 res = mask;
4862 if ((res & bit) == 0)
4863 continue;
4864 res = bit - 1;
4865 res = (val + bit).and_not (res);
4866 res &= mask;
4867 if (wi::gtu_p (res, val))
4868 return res ^ sgnbit;
4870 return val ^ sgnbit;
4873 /* Helper for overflow_comparison_p
4875 OP0 CODE OP1 is a comparison. Examine the comparison and potentially
4876 OP1's defining statement to see if it ultimately has the form
4877 OP0 CODE (OP0 PLUS INTEGER_CST)
4879 If so, return TRUE indicating this is an overflow test and store into
4880 *NEW_CST an updated constant that can be used in a narrowed range test.
4882 REVERSED indicates if the comparison was originally:
4884 OP1 CODE' OP0.
4886 This affects how we build the updated constant. */
4888 static bool
4889 overflow_comparison_p_1 (enum tree_code code, tree op0, tree op1,
4890 bool follow_assert_exprs, bool reversed, tree *new_cst)
4892 /* See if this is a relational operation between two SSA_NAMES with
4893 unsigned, overflow wrapping values. If so, check it more deeply. */
4894 if ((code == LT_EXPR || code == LE_EXPR
4895 || code == GE_EXPR || code == GT_EXPR)
4896 && TREE_CODE (op0) == SSA_NAME
4897 && TREE_CODE (op1) == SSA_NAME
4898 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
4899 && TYPE_UNSIGNED (TREE_TYPE (op0))
4900 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (op0)))
4902 gimple *op1_def = SSA_NAME_DEF_STMT (op1);
4904 /* If requested, follow any ASSERT_EXPRs backwards for OP1. */
4905 if (follow_assert_exprs)
4907 while (gimple_assign_single_p (op1_def)
4908 && TREE_CODE (gimple_assign_rhs1 (op1_def)) == ASSERT_EXPR)
4910 op1 = TREE_OPERAND (gimple_assign_rhs1 (op1_def), 0);
4911 if (TREE_CODE (op1) != SSA_NAME)
4912 break;
4913 op1_def = SSA_NAME_DEF_STMT (op1);
4917 /* Now look at the defining statement of OP1 to see if it adds
4918 or subtracts a nonzero constant from another operand. */
4919 if (op1_def
4920 && is_gimple_assign (op1_def)
4921 && gimple_assign_rhs_code (op1_def) == PLUS_EXPR
4922 && TREE_CODE (gimple_assign_rhs2 (op1_def)) == INTEGER_CST
4923 && !integer_zerop (gimple_assign_rhs2 (op1_def)))
4925 tree target = gimple_assign_rhs1 (op1_def);
4927 /* If requested, follow ASSERT_EXPRs backwards for op0 looking
4928 for one where TARGET appears on the RHS. */
4929 if (follow_assert_exprs)
4931 /* Now see if that "other operand" is op0, following the chain
4932 of ASSERT_EXPRs if necessary. */
4933 gimple *op0_def = SSA_NAME_DEF_STMT (op0);
4934 while (op0 != target
4935 && gimple_assign_single_p (op0_def)
4936 && TREE_CODE (gimple_assign_rhs1 (op0_def)) == ASSERT_EXPR)
4938 op0 = TREE_OPERAND (gimple_assign_rhs1 (op0_def), 0);
4939 if (TREE_CODE (op0) != SSA_NAME)
4940 break;
4941 op0_def = SSA_NAME_DEF_STMT (op0);
4945 /* If we did not find our target SSA_NAME, then this is not
4946 an overflow test. */
4947 if (op0 != target)
4948 return false;
4950 tree type = TREE_TYPE (op0);
4951 wide_int max = wi::max_value (TYPE_PRECISION (type), UNSIGNED);
4952 tree inc = gimple_assign_rhs2 (op1_def);
4953 if (reversed)
4954 *new_cst = wide_int_to_tree (type, max + inc);
4955 else
4956 *new_cst = wide_int_to_tree (type, max - inc);
4957 return true;
4960 return false;
4963 /* OP0 CODE OP1 is a comparison. Examine the comparison and potentially
4964 OP1's defining statement to see if it ultimately has the form
4965 OP0 CODE (OP0 PLUS INTEGER_CST)
4967 If so, return TRUE indicating this is an overflow test and store into
4968 *NEW_CST an updated constant that can be used in a narrowed range test.
4970 These statements are left as-is in the IL to facilitate discovery of
4971 {ADD,SUB}_OVERFLOW sequences later in the optimizer pipeline. But
4972 the alternate range representation is often useful within VRP. */
4974 static bool
4975 overflow_comparison_p (tree_code code, tree name, tree val,
4976 bool use_equiv_p, tree *new_cst)
4978 if (overflow_comparison_p_1 (code, name, val, use_equiv_p, false, new_cst))
4979 return true;
4980 return overflow_comparison_p_1 (swap_tree_comparison (code), val, name,
4981 use_equiv_p, true, new_cst);
4985 /* Try to register an edge assertion for SSA name NAME on edge E for
4986 the condition COND contributing to the conditional jump pointed to by BSI.
4987 Invert the condition COND if INVERT is true. */
4989 static void
4990 register_edge_assert_for_2 (tree name, edge e,
4991 enum tree_code cond_code,
4992 tree cond_op0, tree cond_op1, bool invert,
4993 vec<assert_info> &asserts)
4995 tree val;
4996 enum tree_code comp_code;
4998 if (!extract_code_and_val_from_cond_with_ops (name, cond_code,
4999 cond_op0,
5000 cond_op1,
5001 invert, &comp_code, &val))
5002 return;
5004 /* Queue the assert. */
5005 tree x;
5006 if (overflow_comparison_p (comp_code, name, val, false, &x))
5008 enum tree_code new_code = ((comp_code == GT_EXPR || comp_code == GE_EXPR)
5009 ? GT_EXPR : LE_EXPR);
5010 add_assert_info (asserts, name, name, new_code, x);
5012 add_assert_info (asserts, name, name, comp_code, val);
5014 /* In the case of NAME <= CST and NAME being defined as
5015 NAME = (unsigned) NAME2 + CST2 we can assert NAME2 >= -CST2
5016 and NAME2 <= CST - CST2. We can do the same for NAME > CST.
5017 This catches range and anti-range tests. */
5018 if ((comp_code == LE_EXPR
5019 || comp_code == GT_EXPR)
5020 && TREE_CODE (val) == INTEGER_CST
5021 && TYPE_UNSIGNED (TREE_TYPE (val)))
5023 gimple *def_stmt = SSA_NAME_DEF_STMT (name);
5024 tree cst2 = NULL_TREE, name2 = NULL_TREE, name3 = NULL_TREE;
5026 /* Extract CST2 from the (optional) addition. */
5027 if (is_gimple_assign (def_stmt)
5028 && gimple_assign_rhs_code (def_stmt) == PLUS_EXPR)
5030 name2 = gimple_assign_rhs1 (def_stmt);
5031 cst2 = gimple_assign_rhs2 (def_stmt);
5032 if (TREE_CODE (name2) == SSA_NAME
5033 && TREE_CODE (cst2) == INTEGER_CST)
5034 def_stmt = SSA_NAME_DEF_STMT (name2);
5037 /* Extract NAME2 from the (optional) sign-changing cast. */
5038 if (gimple_assign_cast_p (def_stmt))
5040 if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt))
5041 && ! TYPE_UNSIGNED (TREE_TYPE (gimple_assign_rhs1 (def_stmt)))
5042 && (TYPE_PRECISION (gimple_expr_type (def_stmt))
5043 == TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt)))))
5044 name3 = gimple_assign_rhs1 (def_stmt);
5047 /* If name3 is used later, create an ASSERT_EXPR for it. */
5048 if (name3 != NULL_TREE
5049 && TREE_CODE (name3) == SSA_NAME
5050 && (cst2 == NULL_TREE
5051 || TREE_CODE (cst2) == INTEGER_CST)
5052 && INTEGRAL_TYPE_P (TREE_TYPE (name3)))
5054 tree tmp;
5056 /* Build an expression for the range test. */
5057 tmp = build1 (NOP_EXPR, TREE_TYPE (name), name3);
5058 if (cst2 != NULL_TREE)
5059 tmp = build2 (PLUS_EXPR, TREE_TYPE (name), tmp, cst2);
5061 if (dump_file)
5063 fprintf (dump_file, "Adding assert for ");
5064 print_generic_expr (dump_file, name3);
5065 fprintf (dump_file, " from ");
5066 print_generic_expr (dump_file, tmp);
5067 fprintf (dump_file, "\n");
5070 add_assert_info (asserts, name3, tmp, comp_code, val);
5073 /* If name2 is used later, create an ASSERT_EXPR for it. */
5074 if (name2 != NULL_TREE
5075 && TREE_CODE (name2) == SSA_NAME
5076 && TREE_CODE (cst2) == INTEGER_CST
5077 && INTEGRAL_TYPE_P (TREE_TYPE (name2)))
5079 tree tmp;
5081 /* Build an expression for the range test. */
5082 tmp = name2;
5083 if (TREE_TYPE (name) != TREE_TYPE (name2))
5084 tmp = build1 (NOP_EXPR, TREE_TYPE (name), tmp);
5085 if (cst2 != NULL_TREE)
5086 tmp = build2 (PLUS_EXPR, TREE_TYPE (name), tmp, cst2);
5088 if (dump_file)
5090 fprintf (dump_file, "Adding assert for ");
5091 print_generic_expr (dump_file, name2);
5092 fprintf (dump_file, " from ");
5093 print_generic_expr (dump_file, tmp);
5094 fprintf (dump_file, "\n");
5097 add_assert_info (asserts, name2, tmp, comp_code, val);
5101 /* In the case of post-in/decrement tests like if (i++) ... and uses
5102 of the in/decremented value on the edge the extra name we want to
5103 assert for is not on the def chain of the name compared. Instead
5104 it is in the set of use stmts.
5105 Similar cases happen for conversions that were simplified through
5106 fold_{sign_changed,widened}_comparison. */
5107 if ((comp_code == NE_EXPR
5108 || comp_code == EQ_EXPR)
5109 && TREE_CODE (val) == INTEGER_CST)
5111 imm_use_iterator ui;
5112 gimple *use_stmt;
5113 FOR_EACH_IMM_USE_STMT (use_stmt, ui, name)
5115 if (!is_gimple_assign (use_stmt))
5116 continue;
5118 /* Cut off to use-stmts that are dominating the predecessor. */
5119 if (!dominated_by_p (CDI_DOMINATORS, e->src, gimple_bb (use_stmt)))
5120 continue;
5122 tree name2 = gimple_assign_lhs (use_stmt);
5123 if (TREE_CODE (name2) != SSA_NAME)
5124 continue;
5126 enum tree_code code = gimple_assign_rhs_code (use_stmt);
5127 tree cst;
5128 if (code == PLUS_EXPR
5129 || code == MINUS_EXPR)
5131 cst = gimple_assign_rhs2 (use_stmt);
5132 if (TREE_CODE (cst) != INTEGER_CST)
5133 continue;
5134 cst = int_const_binop (code, val, cst);
5136 else if (CONVERT_EXPR_CODE_P (code))
5138 /* For truncating conversions we cannot record
5139 an inequality. */
5140 if (comp_code == NE_EXPR
5141 && (TYPE_PRECISION (TREE_TYPE (name2))
5142 < TYPE_PRECISION (TREE_TYPE (name))))
5143 continue;
5144 cst = fold_convert (TREE_TYPE (name2), val);
5146 else
5147 continue;
5149 if (TREE_OVERFLOW_P (cst))
5150 cst = drop_tree_overflow (cst);
5151 add_assert_info (asserts, name2, name2, comp_code, cst);
5155 if (TREE_CODE_CLASS (comp_code) == tcc_comparison
5156 && TREE_CODE (val) == INTEGER_CST)
5158 gimple *def_stmt = SSA_NAME_DEF_STMT (name);
5159 tree name2 = NULL_TREE, names[2], cst2 = NULL_TREE;
5160 tree val2 = NULL_TREE;
5161 unsigned int prec = TYPE_PRECISION (TREE_TYPE (val));
5162 wide_int mask = wi::zero (prec);
5163 unsigned int nprec = prec;
5164 enum tree_code rhs_code = ERROR_MARK;
5166 if (is_gimple_assign (def_stmt))
5167 rhs_code = gimple_assign_rhs_code (def_stmt);
5169 /* In the case of NAME != CST1 where NAME = A +- CST2 we can
5170 assert that A != CST1 -+ CST2. */
5171 if ((comp_code == EQ_EXPR || comp_code == NE_EXPR)
5172 && (rhs_code == PLUS_EXPR || rhs_code == MINUS_EXPR))
5174 tree op0 = gimple_assign_rhs1 (def_stmt);
5175 tree op1 = gimple_assign_rhs2 (def_stmt);
5176 if (TREE_CODE (op0) == SSA_NAME
5177 && TREE_CODE (op1) == INTEGER_CST)
5179 enum tree_code reverse_op = (rhs_code == PLUS_EXPR
5180 ? MINUS_EXPR : PLUS_EXPR);
5181 op1 = int_const_binop (reverse_op, val, op1);
5182 if (TREE_OVERFLOW (op1))
5183 op1 = drop_tree_overflow (op1);
5184 add_assert_info (asserts, op0, op0, comp_code, op1);
5188 /* Add asserts for NAME cmp CST and NAME being defined
5189 as NAME = (int) NAME2. */
5190 if (!TYPE_UNSIGNED (TREE_TYPE (val))
5191 && (comp_code == LE_EXPR || comp_code == LT_EXPR
5192 || comp_code == GT_EXPR || comp_code == GE_EXPR)
5193 && gimple_assign_cast_p (def_stmt))
5195 name2 = gimple_assign_rhs1 (def_stmt);
5196 if (CONVERT_EXPR_CODE_P (rhs_code)
5197 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
5198 && TYPE_UNSIGNED (TREE_TYPE (name2))
5199 && prec == TYPE_PRECISION (TREE_TYPE (name2))
5200 && (comp_code == LE_EXPR || comp_code == GT_EXPR
5201 || !tree_int_cst_equal (val,
5202 TYPE_MIN_VALUE (TREE_TYPE (val)))))
5204 tree tmp, cst;
5205 enum tree_code new_comp_code = comp_code;
5207 cst = fold_convert (TREE_TYPE (name2),
5208 TYPE_MIN_VALUE (TREE_TYPE (val)));
5209 /* Build an expression for the range test. */
5210 tmp = build2 (PLUS_EXPR, TREE_TYPE (name2), name2, cst);
5211 cst = fold_build2 (PLUS_EXPR, TREE_TYPE (name2), cst,
5212 fold_convert (TREE_TYPE (name2), val));
5213 if (comp_code == LT_EXPR || comp_code == GE_EXPR)
5215 new_comp_code = comp_code == LT_EXPR ? LE_EXPR : GT_EXPR;
5216 cst = fold_build2 (MINUS_EXPR, TREE_TYPE (name2), cst,
5217 build_int_cst (TREE_TYPE (name2), 1));
5220 if (dump_file)
5222 fprintf (dump_file, "Adding assert for ");
5223 print_generic_expr (dump_file, name2);
5224 fprintf (dump_file, " from ");
5225 print_generic_expr (dump_file, tmp);
5226 fprintf (dump_file, "\n");
5229 add_assert_info (asserts, name2, tmp, new_comp_code, cst);
5233 /* Add asserts for NAME cmp CST and NAME being defined as
5234 NAME = NAME2 >> CST2.
5236 Extract CST2 from the right shift. */
5237 if (rhs_code == RSHIFT_EXPR)
5239 name2 = gimple_assign_rhs1 (def_stmt);
5240 cst2 = gimple_assign_rhs2 (def_stmt);
5241 if (TREE_CODE (name2) == SSA_NAME
5242 && tree_fits_uhwi_p (cst2)
5243 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
5244 && IN_RANGE (tree_to_uhwi (cst2), 1, prec - 1)
5245 && prec == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (val))))
5247 mask = wi::mask (tree_to_uhwi (cst2), false, prec);
5248 val2 = fold_binary (LSHIFT_EXPR, TREE_TYPE (val), val, cst2);
5251 if (val2 != NULL_TREE
5252 && TREE_CODE (val2) == INTEGER_CST
5253 && simple_cst_equal (fold_build2 (RSHIFT_EXPR,
5254 TREE_TYPE (val),
5255 val2, cst2), val))
5257 enum tree_code new_comp_code = comp_code;
5258 tree tmp, new_val;
5260 tmp = name2;
5261 if (comp_code == EQ_EXPR || comp_code == NE_EXPR)
5263 if (!TYPE_UNSIGNED (TREE_TYPE (val)))
5265 tree type = build_nonstandard_integer_type (prec, 1);
5266 tmp = build1 (NOP_EXPR, type, name2);
5267 val2 = fold_convert (type, val2);
5269 tmp = fold_build2 (MINUS_EXPR, TREE_TYPE (tmp), tmp, val2);
5270 new_val = wide_int_to_tree (TREE_TYPE (tmp), mask);
5271 new_comp_code = comp_code == EQ_EXPR ? LE_EXPR : GT_EXPR;
5273 else if (comp_code == LT_EXPR || comp_code == GE_EXPR)
5275 wide_int minval
5276 = wi::min_value (prec, TYPE_SIGN (TREE_TYPE (val)));
5277 new_val = val2;
5278 if (minval == new_val)
5279 new_val = NULL_TREE;
5281 else
5283 wide_int maxval
5284 = wi::max_value (prec, TYPE_SIGN (TREE_TYPE (val)));
5285 mask |= val2;
5286 if (mask == maxval)
5287 new_val = NULL_TREE;
5288 else
5289 new_val = wide_int_to_tree (TREE_TYPE (val2), mask);
5292 if (new_val)
5294 if (dump_file)
5296 fprintf (dump_file, "Adding assert for ");
5297 print_generic_expr (dump_file, name2);
5298 fprintf (dump_file, " from ");
5299 print_generic_expr (dump_file, tmp);
5300 fprintf (dump_file, "\n");
5303 add_assert_info (asserts, name2, tmp, new_comp_code, new_val);
5307 /* Add asserts for NAME cmp CST and NAME being defined as
5308 NAME = NAME2 & CST2.
5310 Extract CST2 from the and.
5312 Also handle
5313 NAME = (unsigned) NAME2;
5314 casts where NAME's type is unsigned and has smaller precision
5315 than NAME2's type as if it was NAME = NAME2 & MASK. */
5316 names[0] = NULL_TREE;
5317 names[1] = NULL_TREE;
5318 cst2 = NULL_TREE;
5319 if (rhs_code == BIT_AND_EXPR
5320 || (CONVERT_EXPR_CODE_P (rhs_code)
5321 && INTEGRAL_TYPE_P (TREE_TYPE (val))
5322 && TYPE_UNSIGNED (TREE_TYPE (val))
5323 && TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt)))
5324 > prec))
5326 name2 = gimple_assign_rhs1 (def_stmt);
5327 if (rhs_code == BIT_AND_EXPR)
5328 cst2 = gimple_assign_rhs2 (def_stmt);
5329 else
5331 cst2 = TYPE_MAX_VALUE (TREE_TYPE (val));
5332 nprec = TYPE_PRECISION (TREE_TYPE (name2));
5334 if (TREE_CODE (name2) == SSA_NAME
5335 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
5336 && TREE_CODE (cst2) == INTEGER_CST
5337 && !integer_zerop (cst2)
5338 && (nprec > 1
5339 || TYPE_UNSIGNED (TREE_TYPE (val))))
5341 gimple *def_stmt2 = SSA_NAME_DEF_STMT (name2);
5342 if (gimple_assign_cast_p (def_stmt2))
5344 names[1] = gimple_assign_rhs1 (def_stmt2);
5345 if (!CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt2))
5346 || !INTEGRAL_TYPE_P (TREE_TYPE (names[1]))
5347 || (TYPE_PRECISION (TREE_TYPE (name2))
5348 != TYPE_PRECISION (TREE_TYPE (names[1]))))
5349 names[1] = NULL_TREE;
5351 names[0] = name2;
5354 if (names[0] || names[1])
5356 wide_int minv, maxv, valv, cst2v;
5357 wide_int tem, sgnbit;
5358 bool valid_p = false, valn, cst2n;
5359 enum tree_code ccode = comp_code;
5361 valv = wide_int::from (val, nprec, UNSIGNED);
5362 cst2v = wide_int::from (cst2, nprec, UNSIGNED);
5363 valn = wi::neg_p (valv, TYPE_SIGN (TREE_TYPE (val)));
5364 cst2n = wi::neg_p (cst2v, TYPE_SIGN (TREE_TYPE (val)));
5365 /* If CST2 doesn't have most significant bit set,
5366 but VAL is negative, we have comparison like
5367 if ((x & 0x123) > -4) (always true). Just give up. */
5368 if (!cst2n && valn)
5369 ccode = ERROR_MARK;
5370 if (cst2n)
5371 sgnbit = wi::set_bit_in_zero (nprec - 1, nprec);
5372 else
5373 sgnbit = wi::zero (nprec);
5374 minv = valv & cst2v;
5375 switch (ccode)
5377 case EQ_EXPR:
5378 /* Minimum unsigned value for equality is VAL & CST2
5379 (should be equal to VAL, otherwise we probably should
5380 have folded the comparison into false) and
5381 maximum unsigned value is VAL | ~CST2. */
5382 maxv = valv | ~cst2v;
5383 valid_p = true;
5384 break;
5386 case NE_EXPR:
5387 tem = valv | ~cst2v;
5388 /* If VAL is 0, handle (X & CST2) != 0 as (X & CST2) > 0U. */
5389 if (valv == 0)
5391 cst2n = false;
5392 sgnbit = wi::zero (nprec);
5393 goto gt_expr;
5395 /* If (VAL | ~CST2) is all ones, handle it as
5396 (X & CST2) < VAL. */
5397 if (tem == -1)
5399 cst2n = false;
5400 valn = false;
5401 sgnbit = wi::zero (nprec);
5402 goto lt_expr;
5404 if (!cst2n && wi::neg_p (cst2v))
5405 sgnbit = wi::set_bit_in_zero (nprec - 1, nprec);
5406 if (sgnbit != 0)
5408 if (valv == sgnbit)
5410 cst2n = true;
5411 valn = true;
5412 goto gt_expr;
5414 if (tem == wi::mask (nprec - 1, false, nprec))
5416 cst2n = true;
5417 goto lt_expr;
5419 if (!cst2n)
5420 sgnbit = wi::zero (nprec);
5422 break;
5424 case GE_EXPR:
5425 /* Minimum unsigned value for >= if (VAL & CST2) == VAL
5426 is VAL and maximum unsigned value is ~0. For signed
5427 comparison, if CST2 doesn't have most significant bit
5428 set, handle it similarly. If CST2 has MSB set,
5429 the minimum is the same, and maximum is ~0U/2. */
5430 if (minv != valv)
5432 /* If (VAL & CST2) != VAL, X & CST2 can't be equal to
5433 VAL. */
5434 minv = masked_increment (valv, cst2v, sgnbit, nprec);
5435 if (minv == valv)
5436 break;
5438 maxv = wi::mask (nprec - (cst2n ? 1 : 0), false, nprec);
5439 valid_p = true;
5440 break;
5442 case GT_EXPR:
5443 gt_expr:
5444 /* Find out smallest MINV where MINV > VAL
5445 && (MINV & CST2) == MINV, if any. If VAL is signed and
5446 CST2 has MSB set, compute it biased by 1 << (nprec - 1). */
5447 minv = masked_increment (valv, cst2v, sgnbit, nprec);
5448 if (minv == valv)
5449 break;
5450 maxv = wi::mask (nprec - (cst2n ? 1 : 0), false, nprec);
5451 valid_p = true;
5452 break;
5454 case LE_EXPR:
5455 /* Minimum unsigned value for <= is 0 and maximum
5456 unsigned value is VAL | ~CST2 if (VAL & CST2) == VAL.
5457 Otherwise, find smallest VAL2 where VAL2 > VAL
5458 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
5459 as maximum.
5460 For signed comparison, if CST2 doesn't have most
5461 significant bit set, handle it similarly. If CST2 has
5462 MSB set, the maximum is the same and minimum is INT_MIN. */
5463 if (minv == valv)
5464 maxv = valv;
5465 else
5467 maxv = masked_increment (valv, cst2v, sgnbit, nprec);
5468 if (maxv == valv)
5469 break;
5470 maxv -= 1;
5472 maxv |= ~cst2v;
5473 minv = sgnbit;
5474 valid_p = true;
5475 break;
5477 case LT_EXPR:
5478 lt_expr:
5479 /* Minimum unsigned value for < is 0 and maximum
5480 unsigned value is (VAL-1) | ~CST2 if (VAL & CST2) == VAL.
5481 Otherwise, find smallest VAL2 where VAL2 > VAL
5482 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
5483 as maximum.
5484 For signed comparison, if CST2 doesn't have most
5485 significant bit set, handle it similarly. If CST2 has
5486 MSB set, the maximum is the same and minimum is INT_MIN. */
5487 if (minv == valv)
5489 if (valv == sgnbit)
5490 break;
5491 maxv = valv;
5493 else
5495 maxv = masked_increment (valv, cst2v, sgnbit, nprec);
5496 if (maxv == valv)
5497 break;
5499 maxv -= 1;
5500 maxv |= ~cst2v;
5501 minv = sgnbit;
5502 valid_p = true;
5503 break;
5505 default:
5506 break;
5508 if (valid_p
5509 && (maxv - minv) != -1)
5511 tree tmp, new_val, type;
5512 int i;
5514 for (i = 0; i < 2; i++)
5515 if (names[i])
5517 wide_int maxv2 = maxv;
5518 tmp = names[i];
5519 type = TREE_TYPE (names[i]);
5520 if (!TYPE_UNSIGNED (type))
5522 type = build_nonstandard_integer_type (nprec, 1);
5523 tmp = build1 (NOP_EXPR, type, names[i]);
5525 if (minv != 0)
5527 tmp = build2 (PLUS_EXPR, type, tmp,
5528 wide_int_to_tree (type, -minv));
5529 maxv2 = maxv - minv;
5531 new_val = wide_int_to_tree (type, maxv2);
5533 if (dump_file)
5535 fprintf (dump_file, "Adding assert for ");
5536 print_generic_expr (dump_file, names[i]);
5537 fprintf (dump_file, " from ");
5538 print_generic_expr (dump_file, tmp);
5539 fprintf (dump_file, "\n");
5542 add_assert_info (asserts, names[i], tmp, LE_EXPR, new_val);
5549 /* OP is an operand of a truth value expression which is known to have
5550 a particular value. Register any asserts for OP and for any
5551 operands in OP's defining statement.
5553 If CODE is EQ_EXPR, then we want to register OP is zero (false),
5554 if CODE is NE_EXPR, then we want to register OP is nonzero (true). */
5556 static void
5557 register_edge_assert_for_1 (tree op, enum tree_code code,
5558 edge e, vec<assert_info> &asserts)
5560 gimple *op_def;
5561 tree val;
5562 enum tree_code rhs_code;
5564 /* We only care about SSA_NAMEs. */
5565 if (TREE_CODE (op) != SSA_NAME)
5566 return;
5568 /* We know that OP will have a zero or nonzero value. */
5569 val = build_int_cst (TREE_TYPE (op), 0);
5570 add_assert_info (asserts, op, op, code, val);
5572 /* Now look at how OP is set. If it's set from a comparison,
5573 a truth operation or some bit operations, then we may be able
5574 to register information about the operands of that assignment. */
5575 op_def = SSA_NAME_DEF_STMT (op);
5576 if (gimple_code (op_def) != GIMPLE_ASSIGN)
5577 return;
5579 rhs_code = gimple_assign_rhs_code (op_def);
5581 if (TREE_CODE_CLASS (rhs_code) == tcc_comparison)
5583 bool invert = (code == EQ_EXPR ? true : false);
5584 tree op0 = gimple_assign_rhs1 (op_def);
5585 tree op1 = gimple_assign_rhs2 (op_def);
5587 if (TREE_CODE (op0) == SSA_NAME)
5588 register_edge_assert_for_2 (op0, e, rhs_code, op0, op1, invert, asserts);
5589 if (TREE_CODE (op1) == SSA_NAME)
5590 register_edge_assert_for_2 (op1, e, rhs_code, op0, op1, invert, asserts);
5592 else if ((code == NE_EXPR
5593 && gimple_assign_rhs_code (op_def) == BIT_AND_EXPR)
5594 || (code == EQ_EXPR
5595 && gimple_assign_rhs_code (op_def) == BIT_IOR_EXPR))
5597 /* Recurse on each operand. */
5598 tree op0 = gimple_assign_rhs1 (op_def);
5599 tree op1 = gimple_assign_rhs2 (op_def);
5600 if (TREE_CODE (op0) == SSA_NAME
5601 && has_single_use (op0))
5602 register_edge_assert_for_1 (op0, code, e, asserts);
5603 if (TREE_CODE (op1) == SSA_NAME
5604 && has_single_use (op1))
5605 register_edge_assert_for_1 (op1, code, e, asserts);
5607 else if (gimple_assign_rhs_code (op_def) == BIT_NOT_EXPR
5608 && TYPE_PRECISION (TREE_TYPE (gimple_assign_lhs (op_def))) == 1)
5610 /* Recurse, flipping CODE. */
5611 code = invert_tree_comparison (code, false);
5612 register_edge_assert_for_1 (gimple_assign_rhs1 (op_def), code, e, asserts);
5614 else if (gimple_assign_rhs_code (op_def) == SSA_NAME)
5616 /* Recurse through the copy. */
5617 register_edge_assert_for_1 (gimple_assign_rhs1 (op_def), code, e, asserts);
5619 else if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (op_def)))
5621 /* Recurse through the type conversion, unless it is a narrowing
5622 conversion or conversion from non-integral type. */
5623 tree rhs = gimple_assign_rhs1 (op_def);
5624 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs))
5625 && (TYPE_PRECISION (TREE_TYPE (rhs))
5626 <= TYPE_PRECISION (TREE_TYPE (op))))
5627 register_edge_assert_for_1 (rhs, code, e, asserts);
5631 /* Check if comparison
5632 NAME COND_OP INTEGER_CST
5633 has a form of
5634 (X & 11...100..0) COND_OP XX...X00...0
5635 Such comparison can yield assertions like
5636 X >= XX...X00...0
5637 X <= XX...X11...1
5638 in case of COND_OP being NE_EXPR or
5639 X < XX...X00...0
5640 X > XX...X11...1
5641 in case of EQ_EXPR. */
5643 static bool
5644 is_masked_range_test (tree name, tree valt, enum tree_code cond_code,
5645 tree *new_name, tree *low, enum tree_code *low_code,
5646 tree *high, enum tree_code *high_code)
5648 gimple *def_stmt = SSA_NAME_DEF_STMT (name);
5650 if (!is_gimple_assign (def_stmt)
5651 || gimple_assign_rhs_code (def_stmt) != BIT_AND_EXPR)
5652 return false;
5654 tree t = gimple_assign_rhs1 (def_stmt);
5655 tree maskt = gimple_assign_rhs2 (def_stmt);
5656 if (TREE_CODE (t) != SSA_NAME || TREE_CODE (maskt) != INTEGER_CST)
5657 return false;
5659 wide_int mask = maskt;
5660 wide_int inv_mask = ~mask;
5661 wide_int val = valt; // Assume VALT is INTEGER_CST
5663 if ((inv_mask & (inv_mask + 1)) != 0
5664 || (val & mask) != val)
5665 return false;
5667 bool is_range = cond_code == EQ_EXPR;
5669 tree type = TREE_TYPE (t);
5670 wide_int min = wi::min_value (type),
5671 max = wi::max_value (type);
5673 if (is_range)
5675 *low_code = val == min ? ERROR_MARK : GE_EXPR;
5676 *high_code = val == max ? ERROR_MARK : LE_EXPR;
5678 else
5680 /* We can still generate assertion if one of alternatives
5681 is known to always be false. */
5682 if (val == min)
5684 *low_code = (enum tree_code) 0;
5685 *high_code = GT_EXPR;
5687 else if ((val | inv_mask) == max)
5689 *low_code = LT_EXPR;
5690 *high_code = (enum tree_code) 0;
5692 else
5693 return false;
5696 *new_name = t;
5697 *low = wide_int_to_tree (type, val);
5698 *high = wide_int_to_tree (type, val | inv_mask);
5700 if (wi::neg_p (val, TYPE_SIGN (type)))
5701 std::swap (*low, *high);
5703 return true;
5706 /* Try to register an edge assertion for SSA name NAME on edge E for
5707 the condition COND contributing to the conditional jump pointed to by
5708 SI. */
5710 static void
5711 register_edge_assert_for (tree name, edge e,
5712 enum tree_code cond_code, tree cond_op0,
5713 tree cond_op1, vec<assert_info> &asserts)
5715 tree val;
5716 enum tree_code comp_code;
5717 bool is_else_edge = (e->flags & EDGE_FALSE_VALUE) != 0;
5719 /* Do not attempt to infer anything in names that flow through
5720 abnormal edges. */
5721 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name))
5722 return;
5724 if (!extract_code_and_val_from_cond_with_ops (name, cond_code,
5725 cond_op0, cond_op1,
5726 is_else_edge,
5727 &comp_code, &val))
5728 return;
5730 /* Register ASSERT_EXPRs for name. */
5731 register_edge_assert_for_2 (name, e, cond_code, cond_op0,
5732 cond_op1, is_else_edge, asserts);
5735 /* If COND is effectively an equality test of an SSA_NAME against
5736 the value zero or one, then we may be able to assert values
5737 for SSA_NAMEs which flow into COND. */
5739 /* In the case of NAME == 1 or NAME != 0, for BIT_AND_EXPR defining
5740 statement of NAME we can assert both operands of the BIT_AND_EXPR
5741 have nonzero value. */
5742 if (((comp_code == EQ_EXPR && integer_onep (val))
5743 || (comp_code == NE_EXPR && integer_zerop (val))))
5745 gimple *def_stmt = SSA_NAME_DEF_STMT (name);
5747 if (is_gimple_assign (def_stmt)
5748 && gimple_assign_rhs_code (def_stmt) == BIT_AND_EXPR)
5750 tree op0 = gimple_assign_rhs1 (def_stmt);
5751 tree op1 = gimple_assign_rhs2 (def_stmt);
5752 register_edge_assert_for_1 (op0, NE_EXPR, e, asserts);
5753 register_edge_assert_for_1 (op1, NE_EXPR, e, asserts);
5757 /* In the case of NAME == 0 or NAME != 1, for BIT_IOR_EXPR defining
5758 statement of NAME we can assert both operands of the BIT_IOR_EXPR
5759 have zero value. */
5760 if (((comp_code == EQ_EXPR && integer_zerop (val))
5761 || (comp_code == NE_EXPR && integer_onep (val))))
5763 gimple *def_stmt = SSA_NAME_DEF_STMT (name);
5765 /* For BIT_IOR_EXPR only if NAME == 0 both operands have
5766 necessarily zero value, or if type-precision is one. */
5767 if (is_gimple_assign (def_stmt)
5768 && (gimple_assign_rhs_code (def_stmt) == BIT_IOR_EXPR
5769 && (TYPE_PRECISION (TREE_TYPE (name)) == 1
5770 || comp_code == EQ_EXPR)))
5772 tree op0 = gimple_assign_rhs1 (def_stmt);
5773 tree op1 = gimple_assign_rhs2 (def_stmt);
5774 register_edge_assert_for_1 (op0, EQ_EXPR, e, asserts);
5775 register_edge_assert_for_1 (op1, EQ_EXPR, e, asserts);
5779 /* Sometimes we can infer ranges from (NAME & MASK) == VALUE. */
5780 if ((comp_code == EQ_EXPR || comp_code == NE_EXPR)
5781 && TREE_CODE (val) == INTEGER_CST)
5783 enum tree_code low_code, high_code;
5784 tree low, high;
5785 if (is_masked_range_test (name, val, comp_code, &name, &low,
5786 &low_code, &high, &high_code))
5788 if (low_code != ERROR_MARK)
5789 register_edge_assert_for_2 (name, e, low_code, name,
5790 low, /*invert*/false, asserts);
5791 if (high_code != ERROR_MARK)
5792 register_edge_assert_for_2 (name, e, high_code, name,
5793 high, /*invert*/false, asserts);
5798 /* Finish found ASSERTS for E and register them at GSI. */
5800 static void
5801 finish_register_edge_assert_for (edge e, gimple_stmt_iterator gsi,
5802 vec<assert_info> &asserts)
5804 for (unsigned i = 0; i < asserts.length (); ++i)
5805 /* Only register an ASSERT_EXPR if NAME was found in the sub-graph
5806 reachable from E. */
5807 if (live_on_edge (e, asserts[i].name))
5808 register_new_assert_for (asserts[i].name, asserts[i].expr,
5809 asserts[i].comp_code, asserts[i].val,
5810 NULL, e, gsi);
5815 /* Determine whether the outgoing edges of BB should receive an
5816 ASSERT_EXPR for each of the operands of BB's LAST statement.
5817 The last statement of BB must be a COND_EXPR.
5819 If any of the sub-graphs rooted at BB have an interesting use of
5820 the predicate operands, an assert location node is added to the
5821 list of assertions for the corresponding operands. */
5823 static void
5824 find_conditional_asserts (basic_block bb, gcond *last)
5826 gimple_stmt_iterator bsi;
5827 tree op;
5828 edge_iterator ei;
5829 edge e;
5830 ssa_op_iter iter;
5832 bsi = gsi_for_stmt (last);
5834 /* Look for uses of the operands in each of the sub-graphs
5835 rooted at BB. We need to check each of the outgoing edges
5836 separately, so that we know what kind of ASSERT_EXPR to
5837 insert. */
5838 FOR_EACH_EDGE (e, ei, bb->succs)
5840 if (e->dest == bb)
5841 continue;
5843 /* Register the necessary assertions for each operand in the
5844 conditional predicate. */
5845 auto_vec<assert_info, 8> asserts;
5846 FOR_EACH_SSA_TREE_OPERAND (op, last, iter, SSA_OP_USE)
5847 register_edge_assert_for (op, e,
5848 gimple_cond_code (last),
5849 gimple_cond_lhs (last),
5850 gimple_cond_rhs (last), asserts);
5851 finish_register_edge_assert_for (e, bsi, asserts);
5855 struct case_info
5857 tree expr;
5858 basic_block bb;
5861 /* Compare two case labels sorting first by the destination bb index
5862 and then by the case value. */
5864 static int
5865 compare_case_labels (const void *p1, const void *p2)
5867 const struct case_info *ci1 = (const struct case_info *) p1;
5868 const struct case_info *ci2 = (const struct case_info *) p2;
5869 int idx1 = ci1->bb->index;
5870 int idx2 = ci2->bb->index;
5872 if (idx1 < idx2)
5873 return -1;
5874 else if (idx1 == idx2)
5876 /* Make sure the default label is first in a group. */
5877 if (!CASE_LOW (ci1->expr))
5878 return -1;
5879 else if (!CASE_LOW (ci2->expr))
5880 return 1;
5881 else
5882 return tree_int_cst_compare (CASE_LOW (ci1->expr),
5883 CASE_LOW (ci2->expr));
5885 else
5886 return 1;
5889 /* Determine whether the outgoing edges of BB should receive an
5890 ASSERT_EXPR for each of the operands of BB's LAST statement.
5891 The last statement of BB must be a SWITCH_EXPR.
5893 If any of the sub-graphs rooted at BB have an interesting use of
5894 the predicate operands, an assert location node is added to the
5895 list of assertions for the corresponding operands. */
5897 static void
5898 find_switch_asserts (basic_block bb, gswitch *last)
5900 gimple_stmt_iterator bsi;
5901 tree op;
5902 edge e;
5903 struct case_info *ci;
5904 size_t n = gimple_switch_num_labels (last);
5905 #if GCC_VERSION >= 4000
5906 unsigned int idx;
5907 #else
5908 /* Work around GCC 3.4 bug (PR 37086). */
5909 volatile unsigned int idx;
5910 #endif
5912 bsi = gsi_for_stmt (last);
5913 op = gimple_switch_index (last);
5914 if (TREE_CODE (op) != SSA_NAME)
5915 return;
5917 /* Build a vector of case labels sorted by destination label. */
5918 ci = XNEWVEC (struct case_info, n);
5919 for (idx = 0; idx < n; ++idx)
5921 ci[idx].expr = gimple_switch_label (last, idx);
5922 ci[idx].bb = label_to_block (CASE_LABEL (ci[idx].expr));
5924 edge default_edge = find_edge (bb, ci[0].bb);
5925 qsort (ci, n, sizeof (struct case_info), compare_case_labels);
5927 for (idx = 0; idx < n; ++idx)
5929 tree min, max;
5930 tree cl = ci[idx].expr;
5931 basic_block cbb = ci[idx].bb;
5933 min = CASE_LOW (cl);
5934 max = CASE_HIGH (cl);
5936 /* If there are multiple case labels with the same destination
5937 we need to combine them to a single value range for the edge. */
5938 if (idx + 1 < n && cbb == ci[idx + 1].bb)
5940 /* Skip labels until the last of the group. */
5941 do {
5942 ++idx;
5943 } while (idx < n && cbb == ci[idx].bb);
5944 --idx;
5946 /* Pick up the maximum of the case label range. */
5947 if (CASE_HIGH (ci[idx].expr))
5948 max = CASE_HIGH (ci[idx].expr);
5949 else
5950 max = CASE_LOW (ci[idx].expr);
5953 /* Can't extract a useful assertion out of a range that includes the
5954 default label. */
5955 if (min == NULL_TREE)
5956 continue;
5958 /* Find the edge to register the assert expr on. */
5959 e = find_edge (bb, cbb);
5961 /* Register the necessary assertions for the operand in the
5962 SWITCH_EXPR. */
5963 auto_vec<assert_info, 8> asserts;
5964 register_edge_assert_for (op, e,
5965 max ? GE_EXPR : EQ_EXPR,
5966 op, fold_convert (TREE_TYPE (op), min),
5967 asserts);
5968 if (max)
5969 register_edge_assert_for (op, e, LE_EXPR, op,
5970 fold_convert (TREE_TYPE (op), max),
5971 asserts);
5972 finish_register_edge_assert_for (e, bsi, asserts);
5975 XDELETEVEC (ci);
5977 if (!live_on_edge (default_edge, op))
5978 return;
5980 /* Now register along the default label assertions that correspond to the
5981 anti-range of each label. */
5982 int insertion_limit = PARAM_VALUE (PARAM_MAX_VRP_SWITCH_ASSERTIONS);
5983 if (insertion_limit == 0)
5984 return;
5986 /* We can't do this if the default case shares a label with another case. */
5987 tree default_cl = gimple_switch_default_label (last);
5988 for (idx = 1; idx < n; idx++)
5990 tree min, max;
5991 tree cl = gimple_switch_label (last, idx);
5992 if (CASE_LABEL (cl) == CASE_LABEL (default_cl))
5993 continue;
5995 min = CASE_LOW (cl);
5996 max = CASE_HIGH (cl);
5998 /* Combine contiguous case ranges to reduce the number of assertions
5999 to insert. */
6000 for (idx = idx + 1; idx < n; idx++)
6002 tree next_min, next_max;
6003 tree next_cl = gimple_switch_label (last, idx);
6004 if (CASE_LABEL (next_cl) == CASE_LABEL (default_cl))
6005 break;
6007 next_min = CASE_LOW (next_cl);
6008 next_max = CASE_HIGH (next_cl);
6010 wide_int difference = wi::sub (next_min, max ? max : min);
6011 if (wi::eq_p (difference, 1))
6012 max = next_max ? next_max : next_min;
6013 else
6014 break;
6016 idx--;
6018 if (max == NULL_TREE)
6020 /* Register the assertion OP != MIN. */
6021 auto_vec<assert_info, 8> asserts;
6022 min = fold_convert (TREE_TYPE (op), min);
6023 register_edge_assert_for (op, default_edge, NE_EXPR, op, min,
6024 asserts);
6025 finish_register_edge_assert_for (default_edge, bsi, asserts);
6027 else
6029 /* Register the assertion (unsigned)OP - MIN > (MAX - MIN),
6030 which will give OP the anti-range ~[MIN,MAX]. */
6031 tree uop = fold_convert (unsigned_type_for (TREE_TYPE (op)), op);
6032 min = fold_convert (TREE_TYPE (uop), min);
6033 max = fold_convert (TREE_TYPE (uop), max);
6035 tree lhs = fold_build2 (MINUS_EXPR, TREE_TYPE (uop), uop, min);
6036 tree rhs = int_const_binop (MINUS_EXPR, max, min);
6037 register_new_assert_for (op, lhs, GT_EXPR, rhs,
6038 NULL, default_edge, bsi);
6041 if (--insertion_limit == 0)
6042 break;
6047 /* Traverse all the statements in block BB looking for statements that
6048 may generate useful assertions for the SSA names in their operand.
6049 If a statement produces a useful assertion A for name N_i, then the
6050 list of assertions already generated for N_i is scanned to
6051 determine if A is actually needed.
6053 If N_i already had the assertion A at a location dominating the
6054 current location, then nothing needs to be done. Otherwise, the
6055 new location for A is recorded instead.
6057 1- For every statement S in BB, all the variables used by S are
6058 added to bitmap FOUND_IN_SUBGRAPH.
6060 2- If statement S uses an operand N in a way that exposes a known
6061 value range for N, then if N was not already generated by an
6062 ASSERT_EXPR, create a new assert location for N. For instance,
6063 if N is a pointer and the statement dereferences it, we can
6064 assume that N is not NULL.
6066 3- COND_EXPRs are a special case of #2. We can derive range
6067 information from the predicate but need to insert different
6068 ASSERT_EXPRs for each of the sub-graphs rooted at the
6069 conditional block. If the last statement of BB is a conditional
6070 expression of the form 'X op Y', then
6072 a) Remove X and Y from the set FOUND_IN_SUBGRAPH.
6074 b) If the conditional is the only entry point to the sub-graph
6075 corresponding to the THEN_CLAUSE, recurse into it. On
6076 return, if X and/or Y are marked in FOUND_IN_SUBGRAPH, then
6077 an ASSERT_EXPR is added for the corresponding variable.
6079 c) Repeat step (b) on the ELSE_CLAUSE.
6081 d) Mark X and Y in FOUND_IN_SUBGRAPH.
6083 For instance,
6085 if (a == 9)
6086 b = a;
6087 else
6088 b = c + 1;
6090 In this case, an assertion on the THEN clause is useful to
6091 determine that 'a' is always 9 on that edge. However, an assertion
6092 on the ELSE clause would be unnecessary.
6094 4- If BB does not end in a conditional expression, then we recurse
6095 into BB's dominator children.
6097 At the end of the recursive traversal, every SSA name will have a
6098 list of locations where ASSERT_EXPRs should be added. When a new
6099 location for name N is found, it is registered by calling
6100 register_new_assert_for. That function keeps track of all the
6101 registered assertions to prevent adding unnecessary assertions.
6102 For instance, if a pointer P_4 is dereferenced more than once in a
6103 dominator tree, only the location dominating all the dereference of
6104 P_4 will receive an ASSERT_EXPR. */
6106 static void
6107 find_assert_locations_1 (basic_block bb, sbitmap live)
6109 gimple *last;
6111 last = last_stmt (bb);
6113 /* If BB's last statement is a conditional statement involving integer
6114 operands, determine if we need to add ASSERT_EXPRs. */
6115 if (last
6116 && gimple_code (last) == GIMPLE_COND
6117 && !fp_predicate (last)
6118 && !ZERO_SSA_OPERANDS (last, SSA_OP_USE))
6119 find_conditional_asserts (bb, as_a <gcond *> (last));
6121 /* If BB's last statement is a switch statement involving integer
6122 operands, determine if we need to add ASSERT_EXPRs. */
6123 if (last
6124 && gimple_code (last) == GIMPLE_SWITCH
6125 && !ZERO_SSA_OPERANDS (last, SSA_OP_USE))
6126 find_switch_asserts (bb, as_a <gswitch *> (last));
6128 /* Traverse all the statements in BB marking used names and looking
6129 for statements that may infer assertions for their used operands. */
6130 for (gimple_stmt_iterator si = gsi_last_bb (bb); !gsi_end_p (si);
6131 gsi_prev (&si))
6133 gimple *stmt;
6134 tree op;
6135 ssa_op_iter i;
6137 stmt = gsi_stmt (si);
6139 if (is_gimple_debug (stmt))
6140 continue;
6142 /* See if we can derive an assertion for any of STMT's operands. */
6143 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_USE)
6145 tree value;
6146 enum tree_code comp_code;
6148 /* If op is not live beyond this stmt, do not bother to insert
6149 asserts for it. */
6150 if (!bitmap_bit_p (live, SSA_NAME_VERSION (op)))
6151 continue;
6153 /* If OP is used in such a way that we can infer a value
6154 range for it, and we don't find a previous assertion for
6155 it, create a new assertion location node for OP. */
6156 if (infer_value_range (stmt, op, &comp_code, &value))
6158 /* If we are able to infer a nonzero value range for OP,
6159 then walk backwards through the use-def chain to see if OP
6160 was set via a typecast.
6162 If so, then we can also infer a nonzero value range
6163 for the operand of the NOP_EXPR. */
6164 if (comp_code == NE_EXPR && integer_zerop (value))
6166 tree t = op;
6167 gimple *def_stmt = SSA_NAME_DEF_STMT (t);
6169 while (is_gimple_assign (def_stmt)
6170 && CONVERT_EXPR_CODE_P
6171 (gimple_assign_rhs_code (def_stmt))
6172 && TREE_CODE
6173 (gimple_assign_rhs1 (def_stmt)) == SSA_NAME
6174 && POINTER_TYPE_P
6175 (TREE_TYPE (gimple_assign_rhs1 (def_stmt))))
6177 t = gimple_assign_rhs1 (def_stmt);
6178 def_stmt = SSA_NAME_DEF_STMT (t);
6180 /* Note we want to register the assert for the
6181 operand of the NOP_EXPR after SI, not after the
6182 conversion. */
6183 if (bitmap_bit_p (live, SSA_NAME_VERSION (t)))
6184 register_new_assert_for (t, t, comp_code, value,
6185 bb, NULL, si);
6189 register_new_assert_for (op, op, comp_code, value, bb, NULL, si);
6193 /* Update live. */
6194 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_USE)
6195 bitmap_set_bit (live, SSA_NAME_VERSION (op));
6196 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_DEF)
6197 bitmap_clear_bit (live, SSA_NAME_VERSION (op));
6200 /* Traverse all PHI nodes in BB, updating live. */
6201 for (gphi_iterator si = gsi_start_phis (bb); !gsi_end_p (si);
6202 gsi_next (&si))
6204 use_operand_p arg_p;
6205 ssa_op_iter i;
6206 gphi *phi = si.phi ();
6207 tree res = gimple_phi_result (phi);
6209 if (virtual_operand_p (res))
6210 continue;
6212 FOR_EACH_PHI_ARG (arg_p, phi, i, SSA_OP_USE)
6214 tree arg = USE_FROM_PTR (arg_p);
6215 if (TREE_CODE (arg) == SSA_NAME)
6216 bitmap_set_bit (live, SSA_NAME_VERSION (arg));
6219 bitmap_clear_bit (live, SSA_NAME_VERSION (res));
6223 /* Do an RPO walk over the function computing SSA name liveness
6224 on-the-fly and deciding on assert expressions to insert. */
6226 static void
6227 find_assert_locations (void)
6229 int *rpo = XNEWVEC (int, last_basic_block_for_fn (cfun));
6230 int *bb_rpo = XNEWVEC (int, last_basic_block_for_fn (cfun));
6231 int *last_rpo = XCNEWVEC (int, last_basic_block_for_fn (cfun));
6232 int rpo_cnt, i;
6234 live = XCNEWVEC (sbitmap, last_basic_block_for_fn (cfun));
6235 rpo_cnt = pre_and_rev_post_order_compute (NULL, rpo, false);
6236 for (i = 0; i < rpo_cnt; ++i)
6237 bb_rpo[rpo[i]] = i;
6239 /* Pre-seed loop latch liveness from loop header PHI nodes. Due to
6240 the order we compute liveness and insert asserts we otherwise
6241 fail to insert asserts into the loop latch. */
6242 loop_p loop;
6243 FOR_EACH_LOOP (loop, 0)
6245 i = loop->latch->index;
6246 unsigned int j = single_succ_edge (loop->latch)->dest_idx;
6247 for (gphi_iterator gsi = gsi_start_phis (loop->header);
6248 !gsi_end_p (gsi); gsi_next (&gsi))
6250 gphi *phi = gsi.phi ();
6251 if (virtual_operand_p (gimple_phi_result (phi)))
6252 continue;
6253 tree arg = gimple_phi_arg_def (phi, j);
6254 if (TREE_CODE (arg) == SSA_NAME)
6256 if (live[i] == NULL)
6258 live[i] = sbitmap_alloc (num_ssa_names);
6259 bitmap_clear (live[i]);
6261 bitmap_set_bit (live[i], SSA_NAME_VERSION (arg));
6266 for (i = rpo_cnt - 1; i >= 0; --i)
6268 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, rpo[i]);
6269 edge e;
6270 edge_iterator ei;
6272 if (!live[rpo[i]])
6274 live[rpo[i]] = sbitmap_alloc (num_ssa_names);
6275 bitmap_clear (live[rpo[i]]);
6278 /* Process BB and update the live information with uses in
6279 this block. */
6280 find_assert_locations_1 (bb, live[rpo[i]]);
6282 /* Merge liveness into the predecessor blocks and free it. */
6283 if (!bitmap_empty_p (live[rpo[i]]))
6285 int pred_rpo = i;
6286 FOR_EACH_EDGE (e, ei, bb->preds)
6288 int pred = e->src->index;
6289 if ((e->flags & EDGE_DFS_BACK) || pred == ENTRY_BLOCK)
6290 continue;
6292 if (!live[pred])
6294 live[pred] = sbitmap_alloc (num_ssa_names);
6295 bitmap_clear (live[pred]);
6297 bitmap_ior (live[pred], live[pred], live[rpo[i]]);
6299 if (bb_rpo[pred] < pred_rpo)
6300 pred_rpo = bb_rpo[pred];
6303 /* Record the RPO number of the last visited block that needs
6304 live information from this block. */
6305 last_rpo[rpo[i]] = pred_rpo;
6307 else
6309 sbitmap_free (live[rpo[i]]);
6310 live[rpo[i]] = NULL;
6313 /* We can free all successors live bitmaps if all their
6314 predecessors have been visited already. */
6315 FOR_EACH_EDGE (e, ei, bb->succs)
6316 if (last_rpo[e->dest->index] == i
6317 && live[e->dest->index])
6319 sbitmap_free (live[e->dest->index]);
6320 live[e->dest->index] = NULL;
6324 XDELETEVEC (rpo);
6325 XDELETEVEC (bb_rpo);
6326 XDELETEVEC (last_rpo);
6327 for (i = 0; i < last_basic_block_for_fn (cfun); ++i)
6328 if (live[i])
6329 sbitmap_free (live[i]);
6330 XDELETEVEC (live);
6333 /* Create an ASSERT_EXPR for NAME and insert it in the location
6334 indicated by LOC. Return true if we made any edge insertions. */
6336 static bool
6337 process_assert_insertions_for (tree name, assert_locus *loc)
6339 /* Build the comparison expression NAME_i COMP_CODE VAL. */
6340 gimple *stmt;
6341 tree cond;
6342 gimple *assert_stmt;
6343 edge_iterator ei;
6344 edge e;
6346 /* If we have X <=> X do not insert an assert expr for that. */
6347 if (loc->expr == loc->val)
6348 return false;
6350 cond = build2 (loc->comp_code, boolean_type_node, loc->expr, loc->val);
6351 assert_stmt = build_assert_expr_for (cond, name);
6352 if (loc->e)
6354 /* We have been asked to insert the assertion on an edge. This
6355 is used only by COND_EXPR and SWITCH_EXPR assertions. */
6356 gcc_checking_assert (gimple_code (gsi_stmt (loc->si)) == GIMPLE_COND
6357 || (gimple_code (gsi_stmt (loc->si))
6358 == GIMPLE_SWITCH));
6360 gsi_insert_on_edge (loc->e, assert_stmt);
6361 return true;
6364 /* If the stmt iterator points at the end then this is an insertion
6365 at the beginning of a block. */
6366 if (gsi_end_p (loc->si))
6368 gimple_stmt_iterator si = gsi_after_labels (loc->bb);
6369 gsi_insert_before (&si, assert_stmt, GSI_SAME_STMT);
6370 return false;
6373 /* Otherwise, we can insert right after LOC->SI iff the
6374 statement must not be the last statement in the block. */
6375 stmt = gsi_stmt (loc->si);
6376 if (!stmt_ends_bb_p (stmt))
6378 gsi_insert_after (&loc->si, assert_stmt, GSI_SAME_STMT);
6379 return false;
6382 /* If STMT must be the last statement in BB, we can only insert new
6383 assertions on the non-abnormal edge out of BB. Note that since
6384 STMT is not control flow, there may only be one non-abnormal/eh edge
6385 out of BB. */
6386 FOR_EACH_EDGE (e, ei, loc->bb->succs)
6387 if (!(e->flags & (EDGE_ABNORMAL|EDGE_EH)))
6389 gsi_insert_on_edge (e, assert_stmt);
6390 return true;
6393 gcc_unreachable ();
6396 /* Qsort helper for sorting assert locations. */
6398 static int
6399 compare_assert_loc (const void *pa, const void *pb)
6401 assert_locus * const a = *(assert_locus * const *)pa;
6402 assert_locus * const b = *(assert_locus * const *)pb;
6403 if (! a->e && b->e)
6404 return 1;
6405 else if (a->e && ! b->e)
6406 return -1;
6408 /* Sort after destination index. */
6409 if (! a->e && ! b->e)
6411 else if (a->e->dest->index > b->e->dest->index)
6412 return 1;
6413 else if (a->e->dest->index < b->e->dest->index)
6414 return -1;
6416 /* Sort after comp_code. */
6417 if (a->comp_code > b->comp_code)
6418 return 1;
6419 else if (a->comp_code < b->comp_code)
6420 return -1;
6422 /* Break the tie using hashing and source/bb index. */
6423 hashval_t ha = iterative_hash_expr (a->expr, iterative_hash_expr (a->val, 0));
6424 hashval_t hb = iterative_hash_expr (b->expr, iterative_hash_expr (b->val, 0));
6425 if (ha == hb)
6426 return (a->e && b->e
6427 ? a->e->src->index - b->e->src->index
6428 : a->bb->index - b->bb->index);
6429 return ha - hb;
6432 /* Process all the insertions registered for every name N_i registered
6433 in NEED_ASSERT_FOR. The list of assertions to be inserted are
6434 found in ASSERTS_FOR[i]. */
6436 static void
6437 process_assert_insertions (void)
6439 unsigned i;
6440 bitmap_iterator bi;
6441 bool update_edges_p = false;
6442 int num_asserts = 0;
6444 if (dump_file && (dump_flags & TDF_DETAILS))
6445 dump_all_asserts (dump_file);
6447 EXECUTE_IF_SET_IN_BITMAP (need_assert_for, 0, i, bi)
6449 assert_locus *loc = asserts_for[i];
6450 gcc_assert (loc);
6452 auto_vec<assert_locus *, 16> asserts;
6453 for (; loc; loc = loc->next)
6454 asserts.safe_push (loc);
6455 asserts.qsort (compare_assert_loc);
6457 /* Push down common asserts to successors and remove redundant ones. */
6458 unsigned ecnt = 0;
6459 assert_locus *common = NULL;
6460 unsigned commonj = 0;
6461 for (unsigned j = 0; j < asserts.length (); ++j)
6463 loc = asserts[j];
6464 if (! loc->e)
6465 common = NULL;
6466 else if (! common
6467 || loc->e->dest != common->e->dest
6468 || loc->comp_code != common->comp_code
6469 || ! operand_equal_p (loc->val, common->val, 0)
6470 || ! operand_equal_p (loc->expr, common->expr, 0))
6472 commonj = j;
6473 common = loc;
6474 ecnt = 1;
6476 else if (loc->e == asserts[j-1]->e)
6478 /* Remove duplicate asserts. */
6479 if (commonj == j - 1)
6481 commonj = j;
6482 common = loc;
6484 free (asserts[j-1]);
6485 asserts[j-1] = NULL;
6487 else
6489 ecnt++;
6490 if (EDGE_COUNT (common->e->dest->preds) == ecnt)
6492 /* We have the same assertion on all incoming edges of a BB.
6493 Insert it at the beginning of that block. */
6494 loc->bb = loc->e->dest;
6495 loc->e = NULL;
6496 loc->si = gsi_none ();
6497 common = NULL;
6498 /* Clear asserts commoned. */
6499 for (; commonj != j; ++commonj)
6500 if (asserts[commonj])
6502 free (asserts[commonj]);
6503 asserts[commonj] = NULL;
6509 for (unsigned j = 0; j < asserts.length (); ++j)
6511 loc = asserts[j];
6512 if (! loc)
6513 continue;
6514 update_edges_p |= process_assert_insertions_for (ssa_name (i), loc);
6515 num_asserts++;
6516 free (loc);
6520 if (update_edges_p)
6521 gsi_commit_edge_inserts ();
6523 statistics_counter_event (cfun, "Number of ASSERT_EXPR expressions inserted",
6524 num_asserts);
6528 /* Traverse the flowgraph looking for conditional jumps to insert range
6529 expressions. These range expressions are meant to provide information
6530 to optimizations that need to reason in terms of value ranges. They
6531 will not be expanded into RTL. For instance, given:
6533 x = ...
6534 y = ...
6535 if (x < y)
6536 y = x - 2;
6537 else
6538 x = y + 3;
6540 this pass will transform the code into:
6542 x = ...
6543 y = ...
6544 if (x < y)
6546 x = ASSERT_EXPR <x, x < y>
6547 y = x - 2
6549 else
6551 y = ASSERT_EXPR <y, x >= y>
6552 x = y + 3
6555 The idea is that once copy and constant propagation have run, other
6556 optimizations will be able to determine what ranges of values can 'x'
6557 take in different paths of the code, simply by checking the reaching
6558 definition of 'x'. */
6560 static void
6561 insert_range_assertions (void)
6563 need_assert_for = BITMAP_ALLOC (NULL);
6564 asserts_for = XCNEWVEC (assert_locus *, num_ssa_names);
6566 calculate_dominance_info (CDI_DOMINATORS);
6568 find_assert_locations ();
6569 if (!bitmap_empty_p (need_assert_for))
6571 process_assert_insertions ();
6572 update_ssa (TODO_update_ssa_no_phi);
6575 if (dump_file && (dump_flags & TDF_DETAILS))
6577 fprintf (dump_file, "\nSSA form after inserting ASSERT_EXPRs\n");
6578 dump_function_to_file (current_function_decl, dump_file, dump_flags);
6581 free (asserts_for);
6582 BITMAP_FREE (need_assert_for);
6585 /* Checks one ARRAY_REF in REF, located at LOCUS. Ignores flexible arrays
6586 and "struct" hacks. If VRP can determine that the
6587 array subscript is a constant, check if it is outside valid
6588 range. If the array subscript is a RANGE, warn if it is
6589 non-overlapping with valid range.
6590 IGNORE_OFF_BY_ONE is true if the ARRAY_REF is inside a ADDR_EXPR. */
6592 static void
6593 check_array_ref (location_t location, tree ref, bool ignore_off_by_one)
6595 value_range *vr = NULL;
6596 tree low_sub, up_sub;
6597 tree low_bound, up_bound, up_bound_p1;
6599 if (TREE_NO_WARNING (ref))
6600 return;
6602 low_sub = up_sub = TREE_OPERAND (ref, 1);
6603 up_bound = array_ref_up_bound (ref);
6605 /* Can not check flexible arrays. */
6606 if (!up_bound
6607 || TREE_CODE (up_bound) != INTEGER_CST)
6608 return;
6610 /* Accesses to trailing arrays via pointers may access storage
6611 beyond the types array bounds. */
6612 if (warn_array_bounds < 2
6613 && array_at_struct_end_p (ref))
6614 return;
6616 low_bound = array_ref_low_bound (ref);
6617 up_bound_p1 = int_const_binop (PLUS_EXPR, up_bound,
6618 build_int_cst (TREE_TYPE (up_bound), 1));
6620 /* Empty array. */
6621 if (tree_int_cst_equal (low_bound, up_bound_p1))
6623 warning_at (location, OPT_Warray_bounds,
6624 "array subscript is above array bounds");
6625 TREE_NO_WARNING (ref) = 1;
6628 if (TREE_CODE (low_sub) == SSA_NAME)
6630 vr = get_value_range (low_sub);
6631 if (vr->type == VR_RANGE || vr->type == VR_ANTI_RANGE)
6633 low_sub = vr->type == VR_RANGE ? vr->max : vr->min;
6634 up_sub = vr->type == VR_RANGE ? vr->min : vr->max;
6638 if (vr && vr->type == VR_ANTI_RANGE)
6640 if (TREE_CODE (up_sub) == INTEGER_CST
6641 && (ignore_off_by_one
6642 ? tree_int_cst_lt (up_bound, up_sub)
6643 : tree_int_cst_le (up_bound, up_sub))
6644 && TREE_CODE (low_sub) == INTEGER_CST
6645 && tree_int_cst_le (low_sub, low_bound))
6647 warning_at (location, OPT_Warray_bounds,
6648 "array subscript is outside array bounds");
6649 TREE_NO_WARNING (ref) = 1;
6652 else if (TREE_CODE (up_sub) == INTEGER_CST
6653 && (ignore_off_by_one
6654 ? !tree_int_cst_le (up_sub, up_bound_p1)
6655 : !tree_int_cst_le (up_sub, up_bound)))
6657 if (dump_file && (dump_flags & TDF_DETAILS))
6659 fprintf (dump_file, "Array bound warning for ");
6660 dump_generic_expr (MSG_NOTE, TDF_SLIM, ref);
6661 fprintf (dump_file, "\n");
6663 warning_at (location, OPT_Warray_bounds,
6664 "array subscript is above array bounds");
6665 TREE_NO_WARNING (ref) = 1;
6667 else if (TREE_CODE (low_sub) == INTEGER_CST
6668 && tree_int_cst_lt (low_sub, low_bound))
6670 if (dump_file && (dump_flags & TDF_DETAILS))
6672 fprintf (dump_file, "Array bound warning for ");
6673 dump_generic_expr (MSG_NOTE, TDF_SLIM, ref);
6674 fprintf (dump_file, "\n");
6676 warning_at (location, OPT_Warray_bounds,
6677 "array subscript is below array bounds");
6678 TREE_NO_WARNING (ref) = 1;
6682 /* Searches if the expr T, located at LOCATION computes
6683 address of an ARRAY_REF, and call check_array_ref on it. */
6685 static void
6686 search_for_addr_array (tree t, location_t location)
6688 /* Check each ARRAY_REFs in the reference chain. */
6691 if (TREE_CODE (t) == ARRAY_REF)
6692 check_array_ref (location, t, true /*ignore_off_by_one*/);
6694 t = TREE_OPERAND (t, 0);
6696 while (handled_component_p (t));
6698 if (TREE_CODE (t) == MEM_REF
6699 && TREE_CODE (TREE_OPERAND (t, 0)) == ADDR_EXPR
6700 && !TREE_NO_WARNING (t))
6702 tree tem = TREE_OPERAND (TREE_OPERAND (t, 0), 0);
6703 tree low_bound, up_bound, el_sz;
6704 offset_int idx;
6705 if (TREE_CODE (TREE_TYPE (tem)) != ARRAY_TYPE
6706 || TREE_CODE (TREE_TYPE (TREE_TYPE (tem))) == ARRAY_TYPE
6707 || !TYPE_DOMAIN (TREE_TYPE (tem)))
6708 return;
6710 low_bound = TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (tem)));
6711 up_bound = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (tem)));
6712 el_sz = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (tem)));
6713 if (!low_bound
6714 || TREE_CODE (low_bound) != INTEGER_CST
6715 || !up_bound
6716 || TREE_CODE (up_bound) != INTEGER_CST
6717 || !el_sz
6718 || TREE_CODE (el_sz) != INTEGER_CST)
6719 return;
6721 idx = mem_ref_offset (t);
6722 idx = wi::sdiv_trunc (idx, wi::to_offset (el_sz));
6723 if (idx < 0)
6725 if (dump_file && (dump_flags & TDF_DETAILS))
6727 fprintf (dump_file, "Array bound warning for ");
6728 dump_generic_expr (MSG_NOTE, TDF_SLIM, t);
6729 fprintf (dump_file, "\n");
6731 warning_at (location, OPT_Warray_bounds,
6732 "array subscript is below array bounds");
6733 TREE_NO_WARNING (t) = 1;
6735 else if (idx > (wi::to_offset (up_bound)
6736 - wi::to_offset (low_bound) + 1))
6738 if (dump_file && (dump_flags & TDF_DETAILS))
6740 fprintf (dump_file, "Array bound warning for ");
6741 dump_generic_expr (MSG_NOTE, TDF_SLIM, t);
6742 fprintf (dump_file, "\n");
6744 warning_at (location, OPT_Warray_bounds,
6745 "array subscript is above array bounds");
6746 TREE_NO_WARNING (t) = 1;
6751 /* walk_tree() callback that checks if *TP is
6752 an ARRAY_REF inside an ADDR_EXPR (in which an array
6753 subscript one outside the valid range is allowed). Call
6754 check_array_ref for each ARRAY_REF found. The location is
6755 passed in DATA. */
6757 static tree
6758 check_array_bounds (tree *tp, int *walk_subtree, void *data)
6760 tree t = *tp;
6761 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
6762 location_t location;
6764 if (EXPR_HAS_LOCATION (t))
6765 location = EXPR_LOCATION (t);
6766 else
6768 location_t *locp = (location_t *) wi->info;
6769 location = *locp;
6772 *walk_subtree = TRUE;
6774 if (TREE_CODE (t) == ARRAY_REF)
6775 check_array_ref (location, t, false /*ignore_off_by_one*/);
6777 else if (TREE_CODE (t) == ADDR_EXPR)
6779 search_for_addr_array (t, location);
6780 *walk_subtree = FALSE;
6783 return NULL_TREE;
6786 /* Walk over all statements of all reachable BBs and call check_array_bounds
6787 on them. */
6789 static void
6790 check_all_array_refs (void)
6792 basic_block bb;
6793 gimple_stmt_iterator si;
6795 FOR_EACH_BB_FN (bb, cfun)
6797 edge_iterator ei;
6798 edge e;
6799 bool executable = false;
6801 /* Skip blocks that were found to be unreachable. */
6802 FOR_EACH_EDGE (e, ei, bb->preds)
6803 executable |= !!(e->flags & EDGE_EXECUTABLE);
6804 if (!executable)
6805 continue;
6807 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
6809 gimple *stmt = gsi_stmt (si);
6810 struct walk_stmt_info wi;
6811 if (!gimple_has_location (stmt)
6812 || is_gimple_debug (stmt))
6813 continue;
6815 memset (&wi, 0, sizeof (wi));
6817 location_t loc = gimple_location (stmt);
6818 wi.info = &loc;
6820 walk_gimple_op (gsi_stmt (si),
6821 check_array_bounds,
6822 &wi);
6827 /* Return true if all imm uses of VAR are either in STMT, or
6828 feed (optionally through a chain of single imm uses) GIMPLE_COND
6829 in basic block COND_BB. */
6831 static bool
6832 all_imm_uses_in_stmt_or_feed_cond (tree var, gimple *stmt, basic_block cond_bb)
6834 use_operand_p use_p, use2_p;
6835 imm_use_iterator iter;
6837 FOR_EACH_IMM_USE_FAST (use_p, iter, var)
6838 if (USE_STMT (use_p) != stmt)
6840 gimple *use_stmt = USE_STMT (use_p), *use_stmt2;
6841 if (is_gimple_debug (use_stmt))
6842 continue;
6843 while (is_gimple_assign (use_stmt)
6844 && TREE_CODE (gimple_assign_lhs (use_stmt)) == SSA_NAME
6845 && single_imm_use (gimple_assign_lhs (use_stmt),
6846 &use2_p, &use_stmt2))
6847 use_stmt = use_stmt2;
6848 if (gimple_code (use_stmt) != GIMPLE_COND
6849 || gimple_bb (use_stmt) != cond_bb)
6850 return false;
6852 return true;
6855 /* Handle
6856 _4 = x_3 & 31;
6857 if (_4 != 0)
6858 goto <bb 6>;
6859 else
6860 goto <bb 7>;
6861 <bb 6>:
6862 __builtin_unreachable ();
6863 <bb 7>:
6864 x_5 = ASSERT_EXPR <x_3, ...>;
6865 If x_3 has no other immediate uses (checked by caller),
6866 var is the x_3 var from ASSERT_EXPR, we can clear low 5 bits
6867 from the non-zero bitmask. */
6869 static void
6870 maybe_set_nonzero_bits (basic_block bb, tree var)
6872 edge e = single_pred_edge (bb);
6873 basic_block cond_bb = e->src;
6874 gimple *stmt = last_stmt (cond_bb);
6875 tree cst;
6877 if (stmt == NULL
6878 || gimple_code (stmt) != GIMPLE_COND
6879 || gimple_cond_code (stmt) != ((e->flags & EDGE_TRUE_VALUE)
6880 ? EQ_EXPR : NE_EXPR)
6881 || TREE_CODE (gimple_cond_lhs (stmt)) != SSA_NAME
6882 || !integer_zerop (gimple_cond_rhs (stmt)))
6883 return;
6885 stmt = SSA_NAME_DEF_STMT (gimple_cond_lhs (stmt));
6886 if (!is_gimple_assign (stmt)
6887 || gimple_assign_rhs_code (stmt) != BIT_AND_EXPR
6888 || TREE_CODE (gimple_assign_rhs2 (stmt)) != INTEGER_CST)
6889 return;
6890 if (gimple_assign_rhs1 (stmt) != var)
6892 gimple *stmt2;
6894 if (TREE_CODE (gimple_assign_rhs1 (stmt)) != SSA_NAME)
6895 return;
6896 stmt2 = SSA_NAME_DEF_STMT (gimple_assign_rhs1 (stmt));
6897 if (!gimple_assign_cast_p (stmt2)
6898 || gimple_assign_rhs1 (stmt2) != var
6899 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (stmt2))
6900 || (TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (stmt)))
6901 != TYPE_PRECISION (TREE_TYPE (var))))
6902 return;
6904 cst = gimple_assign_rhs2 (stmt);
6905 set_nonzero_bits (var, wi::bit_and_not (get_nonzero_bits (var), cst));
6908 /* Convert range assertion expressions into the implied copies and
6909 copy propagate away the copies. Doing the trivial copy propagation
6910 here avoids the need to run the full copy propagation pass after
6911 VRP.
6913 FIXME, this will eventually lead to copy propagation removing the
6914 names that had useful range information attached to them. For
6915 instance, if we had the assertion N_i = ASSERT_EXPR <N_j, N_j > 3>,
6916 then N_i will have the range [3, +INF].
6918 However, by converting the assertion into the implied copy
6919 operation N_i = N_j, we will then copy-propagate N_j into the uses
6920 of N_i and lose the range information. We may want to hold on to
6921 ASSERT_EXPRs a little while longer as the ranges could be used in
6922 things like jump threading.
6924 The problem with keeping ASSERT_EXPRs around is that passes after
6925 VRP need to handle them appropriately.
6927 Another approach would be to make the range information a first
6928 class property of the SSA_NAME so that it can be queried from
6929 any pass. This is made somewhat more complex by the need for
6930 multiple ranges to be associated with one SSA_NAME. */
6932 static void
6933 remove_range_assertions (void)
6935 basic_block bb;
6936 gimple_stmt_iterator si;
6937 /* 1 if looking at ASSERT_EXPRs immediately at the beginning of
6938 a basic block preceeded by GIMPLE_COND branching to it and
6939 __builtin_trap, -1 if not yet checked, 0 otherwise. */
6940 int is_unreachable;
6942 /* Note that the BSI iterator bump happens at the bottom of the
6943 loop and no bump is necessary if we're removing the statement
6944 referenced by the current BSI. */
6945 FOR_EACH_BB_FN (bb, cfun)
6946 for (si = gsi_after_labels (bb), is_unreachable = -1; !gsi_end_p (si);)
6948 gimple *stmt = gsi_stmt (si);
6950 if (is_gimple_assign (stmt)
6951 && gimple_assign_rhs_code (stmt) == ASSERT_EXPR)
6953 tree lhs = gimple_assign_lhs (stmt);
6954 tree rhs = gimple_assign_rhs1 (stmt);
6955 tree var;
6957 var = ASSERT_EXPR_VAR (rhs);
6959 if (TREE_CODE (var) == SSA_NAME
6960 && !POINTER_TYPE_P (TREE_TYPE (lhs))
6961 && SSA_NAME_RANGE_INFO (lhs))
6963 if (is_unreachable == -1)
6965 is_unreachable = 0;
6966 if (single_pred_p (bb)
6967 && assert_unreachable_fallthru_edge_p
6968 (single_pred_edge (bb)))
6969 is_unreachable = 1;
6971 /* Handle
6972 if (x_7 >= 10 && x_7 < 20)
6973 __builtin_unreachable ();
6974 x_8 = ASSERT_EXPR <x_7, ...>;
6975 if the only uses of x_7 are in the ASSERT_EXPR and
6976 in the condition. In that case, we can copy the
6977 range info from x_8 computed in this pass also
6978 for x_7. */
6979 if (is_unreachable
6980 && all_imm_uses_in_stmt_or_feed_cond (var, stmt,
6981 single_pred (bb)))
6983 set_range_info (var, SSA_NAME_RANGE_TYPE (lhs),
6984 SSA_NAME_RANGE_INFO (lhs)->get_min (),
6985 SSA_NAME_RANGE_INFO (lhs)->get_max ());
6986 maybe_set_nonzero_bits (bb, var);
6990 /* Propagate the RHS into every use of the LHS. For SSA names
6991 also propagate abnormals as it merely restores the original
6992 IL in this case (an replace_uses_by would assert). */
6993 if (TREE_CODE (var) == SSA_NAME)
6995 imm_use_iterator iter;
6996 use_operand_p use_p;
6997 gimple *use_stmt;
6998 FOR_EACH_IMM_USE_STMT (use_stmt, iter, lhs)
6999 FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
7000 SET_USE (use_p, var);
7002 else
7003 replace_uses_by (lhs, var);
7005 /* And finally, remove the copy, it is not needed. */
7006 gsi_remove (&si, true);
7007 release_defs (stmt);
7009 else
7011 if (!is_gimple_debug (gsi_stmt (si)))
7012 is_unreachable = 0;
7013 gsi_next (&si);
7019 /* Return true if STMT is interesting for VRP. */
7021 static bool
7022 stmt_interesting_for_vrp (gimple *stmt)
7024 if (gimple_code (stmt) == GIMPLE_PHI)
7026 tree res = gimple_phi_result (stmt);
7027 return (!virtual_operand_p (res)
7028 && (INTEGRAL_TYPE_P (TREE_TYPE (res))
7029 || POINTER_TYPE_P (TREE_TYPE (res))));
7031 else if (is_gimple_assign (stmt) || is_gimple_call (stmt))
7033 tree lhs = gimple_get_lhs (stmt);
7035 /* In general, assignments with virtual operands are not useful
7036 for deriving ranges, with the obvious exception of calls to
7037 builtin functions. */
7038 if (lhs && TREE_CODE (lhs) == SSA_NAME
7039 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs))
7040 || POINTER_TYPE_P (TREE_TYPE (lhs)))
7041 && (is_gimple_call (stmt)
7042 || !gimple_vuse (stmt)))
7043 return true;
7044 else if (is_gimple_call (stmt) && gimple_call_internal_p (stmt))
7045 switch (gimple_call_internal_fn (stmt))
7047 case IFN_ADD_OVERFLOW:
7048 case IFN_SUB_OVERFLOW:
7049 case IFN_MUL_OVERFLOW:
7050 case IFN_ATOMIC_COMPARE_EXCHANGE:
7051 /* These internal calls return _Complex integer type,
7052 but are interesting to VRP nevertheless. */
7053 if (lhs && TREE_CODE (lhs) == SSA_NAME)
7054 return true;
7055 break;
7056 default:
7057 break;
7060 else if (gimple_code (stmt) == GIMPLE_COND
7061 || gimple_code (stmt) == GIMPLE_SWITCH)
7062 return true;
7064 return false;
7067 /* Initialize VRP lattice. */
7069 static void
7070 vrp_initialize_lattice ()
7072 values_propagated = false;
7073 num_vr_values = num_ssa_names;
7074 vr_value = XCNEWVEC (value_range *, num_vr_values);
7075 vr_phi_edge_counts = XCNEWVEC (int, num_ssa_names);
7076 bitmap_obstack_initialize (&vrp_equiv_obstack);
7079 /* Initialization required by ssa_propagate engine. */
7081 static void
7082 vrp_initialize ()
7084 basic_block bb;
7086 FOR_EACH_BB_FN (bb, cfun)
7088 for (gphi_iterator si = gsi_start_phis (bb); !gsi_end_p (si);
7089 gsi_next (&si))
7091 gphi *phi = si.phi ();
7092 if (!stmt_interesting_for_vrp (phi))
7094 tree lhs = PHI_RESULT (phi);
7095 set_value_range_to_varying (get_value_range (lhs));
7096 prop_set_simulate_again (phi, false);
7098 else
7099 prop_set_simulate_again (phi, true);
7102 for (gimple_stmt_iterator si = gsi_start_bb (bb); !gsi_end_p (si);
7103 gsi_next (&si))
7105 gimple *stmt = gsi_stmt (si);
7107 /* If the statement is a control insn, then we do not
7108 want to avoid simulating the statement once. Failure
7109 to do so means that those edges will never get added. */
7110 if (stmt_ends_bb_p (stmt))
7111 prop_set_simulate_again (stmt, true);
7112 else if (!stmt_interesting_for_vrp (stmt))
7114 set_defs_to_varying (stmt);
7115 prop_set_simulate_again (stmt, false);
7117 else
7118 prop_set_simulate_again (stmt, true);
7123 /* Return the singleton value-range for NAME or NAME. */
7125 static inline tree
7126 vrp_valueize (tree name)
7128 if (TREE_CODE (name) == SSA_NAME)
7130 value_range *vr = get_value_range (name);
7131 if (vr->type == VR_RANGE
7132 && (TREE_CODE (vr->min) == SSA_NAME
7133 || is_gimple_min_invariant (vr->min))
7134 && vrp_operand_equal_p (vr->min, vr->max))
7135 return vr->min;
7137 return name;
7140 /* Return the singleton value-range for NAME if that is a constant
7141 but signal to not follow SSA edges. */
7143 static inline tree
7144 vrp_valueize_1 (tree name)
7146 if (TREE_CODE (name) == SSA_NAME)
7148 /* If the definition may be simulated again we cannot follow
7149 this SSA edge as the SSA propagator does not necessarily
7150 re-visit the use. */
7151 gimple *def_stmt = SSA_NAME_DEF_STMT (name);
7152 if (!gimple_nop_p (def_stmt)
7153 && prop_simulate_again_p (def_stmt))
7154 return NULL_TREE;
7155 value_range *vr = get_value_range (name);
7156 if (range_int_cst_singleton_p (vr))
7157 return vr->min;
7159 return name;
7162 /* Visit assignment STMT. If it produces an interesting range, record
7163 the range in VR and set LHS to OUTPUT_P. */
7165 static void
7166 vrp_visit_assignment_or_call (gimple *stmt, tree *output_p, value_range *vr)
7168 tree lhs;
7169 enum gimple_code code = gimple_code (stmt);
7170 lhs = gimple_get_lhs (stmt);
7171 *output_p = NULL_TREE;
7173 /* We only keep track of ranges in integral and pointer types. */
7174 if (TREE_CODE (lhs) == SSA_NAME
7175 && ((INTEGRAL_TYPE_P (TREE_TYPE (lhs))
7176 /* It is valid to have NULL MIN/MAX values on a type. See
7177 build_range_type. */
7178 && TYPE_MIN_VALUE (TREE_TYPE (lhs))
7179 && TYPE_MAX_VALUE (TREE_TYPE (lhs)))
7180 || POINTER_TYPE_P (TREE_TYPE (lhs))))
7182 *output_p = lhs;
7184 /* Try folding the statement to a constant first. */
7185 tree tem = gimple_fold_stmt_to_constant_1 (stmt, vrp_valueize,
7186 vrp_valueize_1);
7187 if (tem)
7189 if (TREE_CODE (tem) == SSA_NAME
7190 && (SSA_NAME_IS_DEFAULT_DEF (tem)
7191 || ! prop_simulate_again_p (SSA_NAME_DEF_STMT (tem))))
7193 extract_range_from_ssa_name (vr, tem);
7194 return;
7196 else if (is_gimple_min_invariant (tem))
7198 set_value_range_to_value (vr, tem, NULL);
7199 return;
7202 /* Then dispatch to value-range extracting functions. */
7203 if (code == GIMPLE_CALL)
7204 extract_range_basic (vr, stmt);
7205 else
7206 extract_range_from_assignment (vr, as_a <gassign *> (stmt));
7210 /* Helper that gets the value range of the SSA_NAME with version I
7211 or a symbolic range containing the SSA_NAME only if the value range
7212 is varying or undefined. */
7214 static inline value_range
7215 get_vr_for_comparison (int i)
7217 value_range vr = *get_value_range (ssa_name (i));
7219 /* If name N_i does not have a valid range, use N_i as its own
7220 range. This allows us to compare against names that may
7221 have N_i in their ranges. */
7222 if (vr.type == VR_VARYING || vr.type == VR_UNDEFINED)
7224 vr.type = VR_RANGE;
7225 vr.min = ssa_name (i);
7226 vr.max = ssa_name (i);
7229 return vr;
7232 /* Compare all the value ranges for names equivalent to VAR with VAL
7233 using comparison code COMP. Return the same value returned by
7234 compare_range_with_value, including the setting of
7235 *STRICT_OVERFLOW_P. */
7237 static tree
7238 compare_name_with_value (enum tree_code comp, tree var, tree val,
7239 bool *strict_overflow_p, bool use_equiv_p)
7241 bitmap_iterator bi;
7242 unsigned i;
7243 bitmap e;
7244 tree retval, t;
7245 int used_strict_overflow;
7246 bool sop;
7247 value_range equiv_vr;
7249 /* Get the set of equivalences for VAR. */
7250 e = get_value_range (var)->equiv;
7252 /* Start at -1. Set it to 0 if we do a comparison without relying
7253 on overflow, or 1 if all comparisons rely on overflow. */
7254 used_strict_overflow = -1;
7256 /* Compare vars' value range with val. */
7257 equiv_vr = get_vr_for_comparison (SSA_NAME_VERSION (var));
7258 sop = false;
7259 retval = compare_range_with_value (comp, &equiv_vr, val, &sop);
7260 if (retval)
7261 used_strict_overflow = sop ? 1 : 0;
7263 /* If the equiv set is empty we have done all work we need to do. */
7264 if (e == NULL)
7266 if (retval
7267 && used_strict_overflow > 0)
7268 *strict_overflow_p = true;
7269 return retval;
7272 EXECUTE_IF_SET_IN_BITMAP (e, 0, i, bi)
7274 tree name = ssa_name (i);
7275 if (! name)
7276 continue;
7278 if (! use_equiv_p
7279 && ! SSA_NAME_IS_DEFAULT_DEF (name)
7280 && prop_simulate_again_p (SSA_NAME_DEF_STMT (name)))
7281 continue;
7283 equiv_vr = get_vr_for_comparison (i);
7284 sop = false;
7285 t = compare_range_with_value (comp, &equiv_vr, val, &sop);
7286 if (t)
7288 /* If we get different answers from different members
7289 of the equivalence set this check must be in a dead
7290 code region. Folding it to a trap representation
7291 would be correct here. For now just return don't-know. */
7292 if (retval != NULL
7293 && t != retval)
7295 retval = NULL_TREE;
7296 break;
7298 retval = t;
7300 if (!sop)
7301 used_strict_overflow = 0;
7302 else if (used_strict_overflow < 0)
7303 used_strict_overflow = 1;
7307 if (retval
7308 && used_strict_overflow > 0)
7309 *strict_overflow_p = true;
7311 return retval;
7315 /* Given a comparison code COMP and names N1 and N2, compare all the
7316 ranges equivalent to N1 against all the ranges equivalent to N2
7317 to determine the value of N1 COMP N2. Return the same value
7318 returned by compare_ranges. Set *STRICT_OVERFLOW_P to indicate
7319 whether we relied on undefined signed overflow in the comparison. */
7322 static tree
7323 compare_names (enum tree_code comp, tree n1, tree n2,
7324 bool *strict_overflow_p)
7326 tree t, retval;
7327 bitmap e1, e2;
7328 bitmap_iterator bi1, bi2;
7329 unsigned i1, i2;
7330 int used_strict_overflow;
7331 static bitmap_obstack *s_obstack = NULL;
7332 static bitmap s_e1 = NULL, s_e2 = NULL;
7334 /* Compare the ranges of every name equivalent to N1 against the
7335 ranges of every name equivalent to N2. */
7336 e1 = get_value_range (n1)->equiv;
7337 e2 = get_value_range (n2)->equiv;
7339 /* Use the fake bitmaps if e1 or e2 are not available. */
7340 if (s_obstack == NULL)
7342 s_obstack = XNEW (bitmap_obstack);
7343 bitmap_obstack_initialize (s_obstack);
7344 s_e1 = BITMAP_ALLOC (s_obstack);
7345 s_e2 = BITMAP_ALLOC (s_obstack);
7347 if (e1 == NULL)
7348 e1 = s_e1;
7349 if (e2 == NULL)
7350 e2 = s_e2;
7352 /* Add N1 and N2 to their own set of equivalences to avoid
7353 duplicating the body of the loop just to check N1 and N2
7354 ranges. */
7355 bitmap_set_bit (e1, SSA_NAME_VERSION (n1));
7356 bitmap_set_bit (e2, SSA_NAME_VERSION (n2));
7358 /* If the equivalence sets have a common intersection, then the two
7359 names can be compared without checking their ranges. */
7360 if (bitmap_intersect_p (e1, e2))
7362 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
7363 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
7365 return (comp == EQ_EXPR || comp == GE_EXPR || comp == LE_EXPR)
7366 ? boolean_true_node
7367 : boolean_false_node;
7370 /* Start at -1. Set it to 0 if we do a comparison without relying
7371 on overflow, or 1 if all comparisons rely on overflow. */
7372 used_strict_overflow = -1;
7374 /* Otherwise, compare all the equivalent ranges. First, add N1 and
7375 N2 to their own set of equivalences to avoid duplicating the body
7376 of the loop just to check N1 and N2 ranges. */
7377 EXECUTE_IF_SET_IN_BITMAP (e1, 0, i1, bi1)
7379 if (! ssa_name (i1))
7380 continue;
7382 value_range vr1 = get_vr_for_comparison (i1);
7384 t = retval = NULL_TREE;
7385 EXECUTE_IF_SET_IN_BITMAP (e2, 0, i2, bi2)
7387 if (! ssa_name (i2))
7388 continue;
7390 bool sop = false;
7392 value_range vr2 = get_vr_for_comparison (i2);
7394 t = compare_ranges (comp, &vr1, &vr2, &sop);
7395 if (t)
7397 /* If we get different answers from different members
7398 of the equivalence set this check must be in a dead
7399 code region. Folding it to a trap representation
7400 would be correct here. For now just return don't-know. */
7401 if (retval != NULL
7402 && t != retval)
7404 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
7405 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
7406 return NULL_TREE;
7408 retval = t;
7410 if (!sop)
7411 used_strict_overflow = 0;
7412 else if (used_strict_overflow < 0)
7413 used_strict_overflow = 1;
7417 if (retval)
7419 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
7420 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
7421 if (used_strict_overflow > 0)
7422 *strict_overflow_p = true;
7423 return retval;
7427 /* None of the equivalent ranges are useful in computing this
7428 comparison. */
7429 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
7430 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
7431 return NULL_TREE;
7434 /* Helper function for vrp_evaluate_conditional_warnv & other
7435 optimizers. */
7437 static tree
7438 vrp_evaluate_conditional_warnv_with_ops_using_ranges (enum tree_code code,
7439 tree op0, tree op1,
7440 bool * strict_overflow_p)
7442 value_range *vr0, *vr1;
7444 vr0 = (TREE_CODE (op0) == SSA_NAME) ? get_value_range (op0) : NULL;
7445 vr1 = (TREE_CODE (op1) == SSA_NAME) ? get_value_range (op1) : NULL;
7447 tree res = NULL_TREE;
7448 if (vr0 && vr1)
7449 res = compare_ranges (code, vr0, vr1, strict_overflow_p);
7450 if (!res && vr0)
7451 res = compare_range_with_value (code, vr0, op1, strict_overflow_p);
7452 if (!res && vr1)
7453 res = (compare_range_with_value
7454 (swap_tree_comparison (code), vr1, op0, strict_overflow_p));
7455 return res;
7458 /* Helper function for vrp_evaluate_conditional_warnv. */
7460 static tree
7461 vrp_evaluate_conditional_warnv_with_ops (enum tree_code code, tree op0,
7462 tree op1, bool use_equiv_p,
7463 bool *strict_overflow_p, bool *only_ranges)
7465 tree ret;
7466 if (only_ranges)
7467 *only_ranges = true;
7469 /* We only deal with integral and pointer types. */
7470 if (!INTEGRAL_TYPE_P (TREE_TYPE (op0))
7471 && !POINTER_TYPE_P (TREE_TYPE (op0)))
7472 return NULL_TREE;
7474 /* If OP0 CODE OP1 is an overflow comparison, if it can be expressed
7475 as a simple equality test, then prefer that over its current form
7476 for evaluation.
7478 An overflow test which collapses to an equality test can always be
7479 expressed as a comparison of one argument against zero. Overflow
7480 occurs when the chosen argument is zero and does not occur if the
7481 chosen argument is not zero. */
7482 tree x;
7483 if (overflow_comparison_p (code, op0, op1, use_equiv_p, &x))
7485 wide_int max = wi::max_value (TYPE_PRECISION (TREE_TYPE (op0)), UNSIGNED);
7486 /* B = A - 1; if (A < B) -> B = A - 1; if (A == 0)
7487 B = A - 1; if (A > B) -> B = A - 1; if (A != 0)
7488 B = A + 1; if (B < A) -> B = A + 1; if (B == 0)
7489 B = A + 1; if (B > A) -> B = A + 1; if (B != 0) */
7490 if (integer_zerop (x))
7492 op1 = x;
7493 code = (code == LT_EXPR || code == LE_EXPR) ? EQ_EXPR : NE_EXPR;
7495 /* B = A + 1; if (A > B) -> B = A + 1; if (B == 0)
7496 B = A + 1; if (A < B) -> B = A + 1; if (B != 0)
7497 B = A - 1; if (B > A) -> B = A - 1; if (A == 0)
7498 B = A - 1; if (B < A) -> B = A - 1; if (A != 0) */
7499 else if (wi::eq_p (x, max - 1))
7501 op0 = op1;
7502 op1 = wide_int_to_tree (TREE_TYPE (op0), 0);
7503 code = (code == GT_EXPR || code == GE_EXPR) ? EQ_EXPR : NE_EXPR;
7507 if ((ret = vrp_evaluate_conditional_warnv_with_ops_using_ranges
7508 (code, op0, op1, strict_overflow_p)))
7509 return ret;
7510 if (only_ranges)
7511 *only_ranges = false;
7512 /* Do not use compare_names during propagation, it's quadratic. */
7513 if (TREE_CODE (op0) == SSA_NAME && TREE_CODE (op1) == SSA_NAME
7514 && use_equiv_p)
7515 return compare_names (code, op0, op1, strict_overflow_p);
7516 else if (TREE_CODE (op0) == SSA_NAME)
7517 return compare_name_with_value (code, op0, op1,
7518 strict_overflow_p, use_equiv_p);
7519 else if (TREE_CODE (op1) == SSA_NAME)
7520 return compare_name_with_value (swap_tree_comparison (code), op1, op0,
7521 strict_overflow_p, use_equiv_p);
7522 return NULL_TREE;
7525 /* Given (CODE OP0 OP1) within STMT, try to simplify it based on value range
7526 information. Return NULL if the conditional can not be evaluated.
7527 The ranges of all the names equivalent with the operands in COND
7528 will be used when trying to compute the value. If the result is
7529 based on undefined signed overflow, issue a warning if
7530 appropriate. */
7532 static tree
7533 vrp_evaluate_conditional (tree_code code, tree op0, tree op1, gimple *stmt)
7535 bool sop;
7536 tree ret;
7537 bool only_ranges;
7539 /* Some passes and foldings leak constants with overflow flag set
7540 into the IL. Avoid doing wrong things with these and bail out. */
7541 if ((TREE_CODE (op0) == INTEGER_CST
7542 && TREE_OVERFLOW (op0))
7543 || (TREE_CODE (op1) == INTEGER_CST
7544 && TREE_OVERFLOW (op1)))
7545 return NULL_TREE;
7547 sop = false;
7548 ret = vrp_evaluate_conditional_warnv_with_ops (code, op0, op1, true, &sop,
7549 &only_ranges);
7551 if (ret && sop)
7553 enum warn_strict_overflow_code wc;
7554 const char* warnmsg;
7556 if (is_gimple_min_invariant (ret))
7558 wc = WARN_STRICT_OVERFLOW_CONDITIONAL;
7559 warnmsg = G_("assuming signed overflow does not occur when "
7560 "simplifying conditional to constant");
7562 else
7564 wc = WARN_STRICT_OVERFLOW_COMPARISON;
7565 warnmsg = G_("assuming signed overflow does not occur when "
7566 "simplifying conditional");
7569 if (issue_strict_overflow_warning (wc))
7571 location_t location;
7573 if (!gimple_has_location (stmt))
7574 location = input_location;
7575 else
7576 location = gimple_location (stmt);
7577 warning_at (location, OPT_Wstrict_overflow, "%s", warnmsg);
7581 if (warn_type_limits
7582 && ret && only_ranges
7583 && TREE_CODE_CLASS (code) == tcc_comparison
7584 && TREE_CODE (op0) == SSA_NAME)
7586 /* If the comparison is being folded and the operand on the LHS
7587 is being compared against a constant value that is outside of
7588 the natural range of OP0's type, then the predicate will
7589 always fold regardless of the value of OP0. If -Wtype-limits
7590 was specified, emit a warning. */
7591 tree type = TREE_TYPE (op0);
7592 value_range *vr0 = get_value_range (op0);
7594 if (vr0->type == VR_RANGE
7595 && INTEGRAL_TYPE_P (type)
7596 && vrp_val_is_min (vr0->min)
7597 && vrp_val_is_max (vr0->max)
7598 && is_gimple_min_invariant (op1))
7600 location_t location;
7602 if (!gimple_has_location (stmt))
7603 location = input_location;
7604 else
7605 location = gimple_location (stmt);
7607 warning_at (location, OPT_Wtype_limits,
7608 integer_zerop (ret)
7609 ? G_("comparison always false "
7610 "due to limited range of data type")
7611 : G_("comparison always true "
7612 "due to limited range of data type"));
7616 return ret;
7620 /* Visit conditional statement STMT. If we can determine which edge
7621 will be taken out of STMT's basic block, record it in
7622 *TAKEN_EDGE_P. Otherwise, set *TAKEN_EDGE_P to NULL. */
7624 static void
7625 vrp_visit_cond_stmt (gcond *stmt, edge *taken_edge_p)
7627 tree val;
7629 *taken_edge_p = NULL;
7631 if (dump_file && (dump_flags & TDF_DETAILS))
7633 tree use;
7634 ssa_op_iter i;
7636 fprintf (dump_file, "\nVisiting conditional with predicate: ");
7637 print_gimple_stmt (dump_file, stmt, 0);
7638 fprintf (dump_file, "\nWith known ranges\n");
7640 FOR_EACH_SSA_TREE_OPERAND (use, stmt, i, SSA_OP_USE)
7642 fprintf (dump_file, "\t");
7643 print_generic_expr (dump_file, use);
7644 fprintf (dump_file, ": ");
7645 dump_value_range (dump_file, vr_value[SSA_NAME_VERSION (use)]);
7648 fprintf (dump_file, "\n");
7651 /* Compute the value of the predicate COND by checking the known
7652 ranges of each of its operands.
7654 Note that we cannot evaluate all the equivalent ranges here
7655 because those ranges may not yet be final and with the current
7656 propagation strategy, we cannot determine when the value ranges
7657 of the names in the equivalence set have changed.
7659 For instance, given the following code fragment
7661 i_5 = PHI <8, i_13>
7663 i_14 = ASSERT_EXPR <i_5, i_5 != 0>
7664 if (i_14 == 1)
7667 Assume that on the first visit to i_14, i_5 has the temporary
7668 range [8, 8] because the second argument to the PHI function is
7669 not yet executable. We derive the range ~[0, 0] for i_14 and the
7670 equivalence set { i_5 }. So, when we visit 'if (i_14 == 1)' for
7671 the first time, since i_14 is equivalent to the range [8, 8], we
7672 determine that the predicate is always false.
7674 On the next round of propagation, i_13 is determined to be
7675 VARYING, which causes i_5 to drop down to VARYING. So, another
7676 visit to i_14 is scheduled. In this second visit, we compute the
7677 exact same range and equivalence set for i_14, namely ~[0, 0] and
7678 { i_5 }. But we did not have the previous range for i_5
7679 registered, so vrp_visit_assignment thinks that the range for
7680 i_14 has not changed. Therefore, the predicate 'if (i_14 == 1)'
7681 is not visited again, which stops propagation from visiting
7682 statements in the THEN clause of that if().
7684 To properly fix this we would need to keep the previous range
7685 value for the names in the equivalence set. This way we would've
7686 discovered that from one visit to the other i_5 changed from
7687 range [8, 8] to VR_VARYING.
7689 However, fixing this apparent limitation may not be worth the
7690 additional checking. Testing on several code bases (GCC, DLV,
7691 MICO, TRAMP3D and SPEC2000) showed that doing this results in
7692 4 more predicates folded in SPEC. */
7694 bool sop;
7695 val = vrp_evaluate_conditional_warnv_with_ops (gimple_cond_code (stmt),
7696 gimple_cond_lhs (stmt),
7697 gimple_cond_rhs (stmt),
7698 false, &sop, NULL);
7699 if (val)
7700 *taken_edge_p = find_taken_edge (gimple_bb (stmt), val);
7702 if (dump_file && (dump_flags & TDF_DETAILS))
7704 fprintf (dump_file, "\nPredicate evaluates to: ");
7705 if (val == NULL_TREE)
7706 fprintf (dump_file, "DON'T KNOW\n");
7707 else
7708 print_generic_stmt (dump_file, val);
7712 /* Searches the case label vector VEC for the index *IDX of the CASE_LABEL
7713 that includes the value VAL. The search is restricted to the range
7714 [START_IDX, n - 1] where n is the size of VEC.
7716 If there is a CASE_LABEL for VAL, its index is placed in IDX and true is
7717 returned.
7719 If there is no CASE_LABEL for VAL and there is one that is larger than VAL,
7720 it is placed in IDX and false is returned.
7722 If VAL is larger than any CASE_LABEL, n is placed on IDX and false is
7723 returned. */
7725 static bool
7726 find_case_label_index (gswitch *stmt, size_t start_idx, tree val, size_t *idx)
7728 size_t n = gimple_switch_num_labels (stmt);
7729 size_t low, high;
7731 /* Find case label for minimum of the value range or the next one.
7732 At each iteration we are searching in [low, high - 1]. */
7734 for (low = start_idx, high = n; high != low; )
7736 tree t;
7737 int cmp;
7738 /* Note that i != high, so we never ask for n. */
7739 size_t i = (high + low) / 2;
7740 t = gimple_switch_label (stmt, i);
7742 /* Cache the result of comparing CASE_LOW and val. */
7743 cmp = tree_int_cst_compare (CASE_LOW (t), val);
7745 if (cmp == 0)
7747 /* Ranges cannot be empty. */
7748 *idx = i;
7749 return true;
7751 else if (cmp > 0)
7752 high = i;
7753 else
7755 low = i + 1;
7756 if (CASE_HIGH (t) != NULL
7757 && tree_int_cst_compare (CASE_HIGH (t), val) >= 0)
7759 *idx = i;
7760 return true;
7765 *idx = high;
7766 return false;
7769 /* Searches the case label vector VEC for the range of CASE_LABELs that is used
7770 for values between MIN and MAX. The first index is placed in MIN_IDX. The
7771 last index is placed in MAX_IDX. If the range of CASE_LABELs is empty
7772 then MAX_IDX < MIN_IDX.
7773 Returns true if the default label is not needed. */
7775 static bool
7776 find_case_label_range (gswitch *stmt, tree min, tree max, size_t *min_idx,
7777 size_t *max_idx)
7779 size_t i, j;
7780 bool min_take_default = !find_case_label_index (stmt, 1, min, &i);
7781 bool max_take_default = !find_case_label_index (stmt, i, max, &j);
7783 if (i == j
7784 && min_take_default
7785 && max_take_default)
7787 /* Only the default case label reached.
7788 Return an empty range. */
7789 *min_idx = 1;
7790 *max_idx = 0;
7791 return false;
7793 else
7795 bool take_default = min_take_default || max_take_default;
7796 tree low, high;
7797 size_t k;
7799 if (max_take_default)
7800 j--;
7802 /* If the case label range is continuous, we do not need
7803 the default case label. Verify that. */
7804 high = CASE_LOW (gimple_switch_label (stmt, i));
7805 if (CASE_HIGH (gimple_switch_label (stmt, i)))
7806 high = CASE_HIGH (gimple_switch_label (stmt, i));
7807 for (k = i + 1; k <= j; ++k)
7809 low = CASE_LOW (gimple_switch_label (stmt, k));
7810 if (!integer_onep (int_const_binop (MINUS_EXPR, low, high)))
7812 take_default = true;
7813 break;
7815 high = low;
7816 if (CASE_HIGH (gimple_switch_label (stmt, k)))
7817 high = CASE_HIGH (gimple_switch_label (stmt, k));
7820 *min_idx = i;
7821 *max_idx = j;
7822 return !take_default;
7826 /* Searches the case label vector VEC for the ranges of CASE_LABELs that are
7827 used in range VR. The indices are placed in MIN_IDX1, MAX_IDX, MIN_IDX2 and
7828 MAX_IDX2. If the ranges of CASE_LABELs are empty then MAX_IDX1 < MIN_IDX1.
7829 Returns true if the default label is not needed. */
7831 static bool
7832 find_case_label_ranges (gswitch *stmt, value_range *vr, size_t *min_idx1,
7833 size_t *max_idx1, size_t *min_idx2,
7834 size_t *max_idx2)
7836 size_t i, j, k, l;
7837 unsigned int n = gimple_switch_num_labels (stmt);
7838 bool take_default;
7839 tree case_low, case_high;
7840 tree min = vr->min, max = vr->max;
7842 gcc_checking_assert (vr->type == VR_RANGE || vr->type == VR_ANTI_RANGE);
7844 take_default = !find_case_label_range (stmt, min, max, &i, &j);
7846 /* Set second range to emtpy. */
7847 *min_idx2 = 1;
7848 *max_idx2 = 0;
7850 if (vr->type == VR_RANGE)
7852 *min_idx1 = i;
7853 *max_idx1 = j;
7854 return !take_default;
7857 /* Set first range to all case labels. */
7858 *min_idx1 = 1;
7859 *max_idx1 = n - 1;
7861 if (i > j)
7862 return false;
7864 /* Make sure all the values of case labels [i , j] are contained in
7865 range [MIN, MAX]. */
7866 case_low = CASE_LOW (gimple_switch_label (stmt, i));
7867 case_high = CASE_HIGH (gimple_switch_label (stmt, j));
7868 if (tree_int_cst_compare (case_low, min) < 0)
7869 i += 1;
7870 if (case_high != NULL_TREE
7871 && tree_int_cst_compare (max, case_high) < 0)
7872 j -= 1;
7874 if (i > j)
7875 return false;
7877 /* If the range spans case labels [i, j], the corresponding anti-range spans
7878 the labels [1, i - 1] and [j + 1, n - 1]. */
7879 k = j + 1;
7880 l = n - 1;
7881 if (k > l)
7883 k = 1;
7884 l = 0;
7887 j = i - 1;
7888 i = 1;
7889 if (i > j)
7891 i = k;
7892 j = l;
7893 k = 1;
7894 l = 0;
7897 *min_idx1 = i;
7898 *max_idx1 = j;
7899 *min_idx2 = k;
7900 *max_idx2 = l;
7901 return false;
7904 /* Visit switch statement STMT. If we can determine which edge
7905 will be taken out of STMT's basic block, record it in
7906 *TAKEN_EDGE_P. Otherwise, *TAKEN_EDGE_P set to NULL. */
7908 static void
7909 vrp_visit_switch_stmt (gswitch *stmt, edge *taken_edge_p)
7911 tree op, val;
7912 value_range *vr;
7913 size_t i = 0, j = 0, k, l;
7914 bool take_default;
7916 *taken_edge_p = NULL;
7917 op = gimple_switch_index (stmt);
7918 if (TREE_CODE (op) != SSA_NAME)
7919 return;
7921 vr = get_value_range (op);
7922 if (dump_file && (dump_flags & TDF_DETAILS))
7924 fprintf (dump_file, "\nVisiting switch expression with operand ");
7925 print_generic_expr (dump_file, op);
7926 fprintf (dump_file, " with known range ");
7927 dump_value_range (dump_file, vr);
7928 fprintf (dump_file, "\n");
7931 if ((vr->type != VR_RANGE
7932 && vr->type != VR_ANTI_RANGE)
7933 || symbolic_range_p (vr))
7934 return;
7936 /* Find the single edge that is taken from the switch expression. */
7937 take_default = !find_case_label_ranges (stmt, vr, &i, &j, &k, &l);
7939 /* Check if the range spans no CASE_LABEL. If so, we only reach the default
7940 label */
7941 if (j < i)
7943 gcc_assert (take_default);
7944 val = gimple_switch_default_label (stmt);
7946 else
7948 /* Check if labels with index i to j and maybe the default label
7949 are all reaching the same label. */
7951 val = gimple_switch_label (stmt, i);
7952 if (take_default
7953 && CASE_LABEL (gimple_switch_default_label (stmt))
7954 != CASE_LABEL (val))
7956 if (dump_file && (dump_flags & TDF_DETAILS))
7957 fprintf (dump_file, " not a single destination for this "
7958 "range\n");
7959 return;
7961 for (++i; i <= j; ++i)
7963 if (CASE_LABEL (gimple_switch_label (stmt, i)) != CASE_LABEL (val))
7965 if (dump_file && (dump_flags & TDF_DETAILS))
7966 fprintf (dump_file, " not a single destination for this "
7967 "range\n");
7968 return;
7971 for (; k <= l; ++k)
7973 if (CASE_LABEL (gimple_switch_label (stmt, k)) != CASE_LABEL (val))
7975 if (dump_file && (dump_flags & TDF_DETAILS))
7976 fprintf (dump_file, " not a single destination for this "
7977 "range\n");
7978 return;
7983 *taken_edge_p = find_edge (gimple_bb (stmt),
7984 label_to_block (CASE_LABEL (val)));
7986 if (dump_file && (dump_flags & TDF_DETAILS))
7988 fprintf (dump_file, " will take edge to ");
7989 print_generic_stmt (dump_file, CASE_LABEL (val));
7994 /* Evaluate statement STMT. If the statement produces a useful range,
7995 set VR and corepsponding OUTPUT_P.
7997 If STMT is a conditional branch and we can determine its truth
7998 value, the taken edge is recorded in *TAKEN_EDGE_P. */
8000 static void
8001 extract_range_from_stmt (gimple *stmt, edge *taken_edge_p,
8002 tree *output_p, value_range *vr)
8005 if (dump_file && (dump_flags & TDF_DETAILS))
8007 fprintf (dump_file, "\nVisiting statement:\n");
8008 print_gimple_stmt (dump_file, stmt, 0, dump_flags);
8011 if (!stmt_interesting_for_vrp (stmt))
8012 gcc_assert (stmt_ends_bb_p (stmt));
8013 else if (is_gimple_assign (stmt) || is_gimple_call (stmt))
8014 vrp_visit_assignment_or_call (stmt, output_p, vr);
8015 else if (gimple_code (stmt) == GIMPLE_COND)
8016 vrp_visit_cond_stmt (as_a <gcond *> (stmt), taken_edge_p);
8017 else if (gimple_code (stmt) == GIMPLE_SWITCH)
8018 vrp_visit_switch_stmt (as_a <gswitch *> (stmt), taken_edge_p);
8021 /* Evaluate statement STMT. If the statement produces a useful range,
8022 return SSA_PROP_INTERESTING and record the SSA name with the
8023 interesting range into *OUTPUT_P.
8025 If STMT is a conditional branch and we can determine its truth
8026 value, the taken edge is recorded in *TAKEN_EDGE_P.
8028 If STMT produces a varying value, return SSA_PROP_VARYING. */
8030 static enum ssa_prop_result
8031 vrp_visit_stmt (gimple *stmt, edge *taken_edge_p, tree *output_p)
8033 value_range vr = VR_INITIALIZER;
8034 tree lhs = gimple_get_lhs (stmt);
8035 extract_range_from_stmt (stmt, taken_edge_p, output_p, &vr);
8037 if (*output_p)
8039 if (update_value_range (*output_p, &vr))
8041 if (dump_file && (dump_flags & TDF_DETAILS))
8043 fprintf (dump_file, "Found new range for ");
8044 print_generic_expr (dump_file, *output_p);
8045 fprintf (dump_file, ": ");
8046 dump_value_range (dump_file, &vr);
8047 fprintf (dump_file, "\n");
8050 if (vr.type == VR_VARYING)
8051 return SSA_PROP_VARYING;
8053 return SSA_PROP_INTERESTING;
8055 return SSA_PROP_NOT_INTERESTING;
8058 if (is_gimple_call (stmt) && gimple_call_internal_p (stmt))
8059 switch (gimple_call_internal_fn (stmt))
8061 case IFN_ADD_OVERFLOW:
8062 case IFN_SUB_OVERFLOW:
8063 case IFN_MUL_OVERFLOW:
8064 case IFN_ATOMIC_COMPARE_EXCHANGE:
8065 /* These internal calls return _Complex integer type,
8066 which VRP does not track, but the immediate uses
8067 thereof might be interesting. */
8068 if (lhs && TREE_CODE (lhs) == SSA_NAME)
8070 imm_use_iterator iter;
8071 use_operand_p use_p;
8072 enum ssa_prop_result res = SSA_PROP_VARYING;
8074 set_value_range_to_varying (get_value_range (lhs));
8076 FOR_EACH_IMM_USE_FAST (use_p, iter, lhs)
8078 gimple *use_stmt = USE_STMT (use_p);
8079 if (!is_gimple_assign (use_stmt))
8080 continue;
8081 enum tree_code rhs_code = gimple_assign_rhs_code (use_stmt);
8082 if (rhs_code != REALPART_EXPR && rhs_code != IMAGPART_EXPR)
8083 continue;
8084 tree rhs1 = gimple_assign_rhs1 (use_stmt);
8085 tree use_lhs = gimple_assign_lhs (use_stmt);
8086 if (TREE_CODE (rhs1) != rhs_code
8087 || TREE_OPERAND (rhs1, 0) != lhs
8088 || TREE_CODE (use_lhs) != SSA_NAME
8089 || !stmt_interesting_for_vrp (use_stmt)
8090 || (!INTEGRAL_TYPE_P (TREE_TYPE (use_lhs))
8091 || !TYPE_MIN_VALUE (TREE_TYPE (use_lhs))
8092 || !TYPE_MAX_VALUE (TREE_TYPE (use_lhs))))
8093 continue;
8095 /* If there is a change in the value range for any of the
8096 REALPART_EXPR/IMAGPART_EXPR immediate uses, return
8097 SSA_PROP_INTERESTING. If there are any REALPART_EXPR
8098 or IMAGPART_EXPR immediate uses, but none of them have
8099 a change in their value ranges, return
8100 SSA_PROP_NOT_INTERESTING. If there are no
8101 {REAL,IMAG}PART_EXPR uses at all,
8102 return SSA_PROP_VARYING. */
8103 value_range new_vr = VR_INITIALIZER;
8104 extract_range_basic (&new_vr, use_stmt);
8105 value_range *old_vr = get_value_range (use_lhs);
8106 if (old_vr->type != new_vr.type
8107 || !vrp_operand_equal_p (old_vr->min, new_vr.min)
8108 || !vrp_operand_equal_p (old_vr->max, new_vr.max)
8109 || !vrp_bitmap_equal_p (old_vr->equiv, new_vr.equiv))
8110 res = SSA_PROP_INTERESTING;
8111 else
8112 res = SSA_PROP_NOT_INTERESTING;
8113 BITMAP_FREE (new_vr.equiv);
8114 if (res == SSA_PROP_INTERESTING)
8116 *output_p = lhs;
8117 return res;
8121 return res;
8123 break;
8124 default:
8125 break;
8128 /* All other statements produce nothing of interest for VRP, so mark
8129 their outputs varying and prevent further simulation. */
8130 set_defs_to_varying (stmt);
8132 return (*taken_edge_p) ? SSA_PROP_INTERESTING : SSA_PROP_VARYING;
8135 /* Union the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
8136 { VR1TYPE, VR0MIN, VR0MAX } and store the result
8137 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
8138 possible such range. The resulting range is not canonicalized. */
8140 static void
8141 union_ranges (enum value_range_type *vr0type,
8142 tree *vr0min, tree *vr0max,
8143 enum value_range_type vr1type,
8144 tree vr1min, tree vr1max)
8146 bool mineq = vrp_operand_equal_p (*vr0min, vr1min);
8147 bool maxeq = vrp_operand_equal_p (*vr0max, vr1max);
8149 /* [] is vr0, () is vr1 in the following classification comments. */
8150 if (mineq && maxeq)
8152 /* [( )] */
8153 if (*vr0type == vr1type)
8154 /* Nothing to do for equal ranges. */
8156 else if ((*vr0type == VR_RANGE
8157 && vr1type == VR_ANTI_RANGE)
8158 || (*vr0type == VR_ANTI_RANGE
8159 && vr1type == VR_RANGE))
8161 /* For anti-range with range union the result is varying. */
8162 goto give_up;
8164 else
8165 gcc_unreachable ();
8167 else if (operand_less_p (*vr0max, vr1min) == 1
8168 || operand_less_p (vr1max, *vr0min) == 1)
8170 /* [ ] ( ) or ( ) [ ]
8171 If the ranges have an empty intersection, result of the union
8172 operation is the anti-range or if both are anti-ranges
8173 it covers all. */
8174 if (*vr0type == VR_ANTI_RANGE
8175 && vr1type == VR_ANTI_RANGE)
8176 goto give_up;
8177 else if (*vr0type == VR_ANTI_RANGE
8178 && vr1type == VR_RANGE)
8180 else if (*vr0type == VR_RANGE
8181 && vr1type == VR_ANTI_RANGE)
8183 *vr0type = vr1type;
8184 *vr0min = vr1min;
8185 *vr0max = vr1max;
8187 else if (*vr0type == VR_RANGE
8188 && vr1type == VR_RANGE)
8190 /* The result is the convex hull of both ranges. */
8191 if (operand_less_p (*vr0max, vr1min) == 1)
8193 /* If the result can be an anti-range, create one. */
8194 if (TREE_CODE (*vr0max) == INTEGER_CST
8195 && TREE_CODE (vr1min) == INTEGER_CST
8196 && vrp_val_is_min (*vr0min)
8197 && vrp_val_is_max (vr1max))
8199 tree min = int_const_binop (PLUS_EXPR,
8200 *vr0max,
8201 build_int_cst (TREE_TYPE (*vr0max), 1));
8202 tree max = int_const_binop (MINUS_EXPR,
8203 vr1min,
8204 build_int_cst (TREE_TYPE (vr1min), 1));
8205 if (!operand_less_p (max, min))
8207 *vr0type = VR_ANTI_RANGE;
8208 *vr0min = min;
8209 *vr0max = max;
8211 else
8212 *vr0max = vr1max;
8214 else
8215 *vr0max = vr1max;
8217 else
8219 /* If the result can be an anti-range, create one. */
8220 if (TREE_CODE (vr1max) == INTEGER_CST
8221 && TREE_CODE (*vr0min) == INTEGER_CST
8222 && vrp_val_is_min (vr1min)
8223 && vrp_val_is_max (*vr0max))
8225 tree min = int_const_binop (PLUS_EXPR,
8226 vr1max,
8227 build_int_cst (TREE_TYPE (vr1max), 1));
8228 tree max = int_const_binop (MINUS_EXPR,
8229 *vr0min,
8230 build_int_cst (TREE_TYPE (*vr0min), 1));
8231 if (!operand_less_p (max, min))
8233 *vr0type = VR_ANTI_RANGE;
8234 *vr0min = min;
8235 *vr0max = max;
8237 else
8238 *vr0min = vr1min;
8240 else
8241 *vr0min = vr1min;
8244 else
8245 gcc_unreachable ();
8247 else if ((maxeq || operand_less_p (vr1max, *vr0max) == 1)
8248 && (mineq || operand_less_p (*vr0min, vr1min) == 1))
8250 /* [ ( ) ] or [( ) ] or [ ( )] */
8251 if (*vr0type == VR_RANGE
8252 && vr1type == VR_RANGE)
8254 else if (*vr0type == VR_ANTI_RANGE
8255 && vr1type == VR_ANTI_RANGE)
8257 *vr0type = vr1type;
8258 *vr0min = vr1min;
8259 *vr0max = vr1max;
8261 else if (*vr0type == VR_ANTI_RANGE
8262 && vr1type == VR_RANGE)
8264 /* Arbitrarily choose the right or left gap. */
8265 if (!mineq && TREE_CODE (vr1min) == INTEGER_CST)
8266 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
8267 build_int_cst (TREE_TYPE (vr1min), 1));
8268 else if (!maxeq && TREE_CODE (vr1max) == INTEGER_CST)
8269 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
8270 build_int_cst (TREE_TYPE (vr1max), 1));
8271 else
8272 goto give_up;
8274 else if (*vr0type == VR_RANGE
8275 && vr1type == VR_ANTI_RANGE)
8276 /* The result covers everything. */
8277 goto give_up;
8278 else
8279 gcc_unreachable ();
8281 else if ((maxeq || operand_less_p (*vr0max, vr1max) == 1)
8282 && (mineq || operand_less_p (vr1min, *vr0min) == 1))
8284 /* ( [ ] ) or ([ ] ) or ( [ ]) */
8285 if (*vr0type == VR_RANGE
8286 && vr1type == VR_RANGE)
8288 *vr0type = vr1type;
8289 *vr0min = vr1min;
8290 *vr0max = vr1max;
8292 else if (*vr0type == VR_ANTI_RANGE
8293 && vr1type == VR_ANTI_RANGE)
8295 else if (*vr0type == VR_RANGE
8296 && vr1type == VR_ANTI_RANGE)
8298 *vr0type = VR_ANTI_RANGE;
8299 if (!mineq && TREE_CODE (*vr0min) == INTEGER_CST)
8301 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
8302 build_int_cst (TREE_TYPE (*vr0min), 1));
8303 *vr0min = vr1min;
8305 else if (!maxeq && TREE_CODE (*vr0max) == INTEGER_CST)
8307 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
8308 build_int_cst (TREE_TYPE (*vr0max), 1));
8309 *vr0max = vr1max;
8311 else
8312 goto give_up;
8314 else if (*vr0type == VR_ANTI_RANGE
8315 && vr1type == VR_RANGE)
8316 /* The result covers everything. */
8317 goto give_up;
8318 else
8319 gcc_unreachable ();
8321 else if ((operand_less_p (vr1min, *vr0max) == 1
8322 || operand_equal_p (vr1min, *vr0max, 0))
8323 && operand_less_p (*vr0min, vr1min) == 1
8324 && operand_less_p (*vr0max, vr1max) == 1)
8326 /* [ ( ] ) or [ ]( ) */
8327 if (*vr0type == VR_RANGE
8328 && vr1type == VR_RANGE)
8329 *vr0max = vr1max;
8330 else if (*vr0type == VR_ANTI_RANGE
8331 && vr1type == VR_ANTI_RANGE)
8332 *vr0min = vr1min;
8333 else if (*vr0type == VR_ANTI_RANGE
8334 && vr1type == VR_RANGE)
8336 if (TREE_CODE (vr1min) == INTEGER_CST)
8337 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
8338 build_int_cst (TREE_TYPE (vr1min), 1));
8339 else
8340 goto give_up;
8342 else if (*vr0type == VR_RANGE
8343 && vr1type == VR_ANTI_RANGE)
8345 if (TREE_CODE (*vr0max) == INTEGER_CST)
8347 *vr0type = vr1type;
8348 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
8349 build_int_cst (TREE_TYPE (*vr0max), 1));
8350 *vr0max = vr1max;
8352 else
8353 goto give_up;
8355 else
8356 gcc_unreachable ();
8358 else if ((operand_less_p (*vr0min, vr1max) == 1
8359 || operand_equal_p (*vr0min, vr1max, 0))
8360 && operand_less_p (vr1min, *vr0min) == 1
8361 && operand_less_p (vr1max, *vr0max) == 1)
8363 /* ( [ ) ] or ( )[ ] */
8364 if (*vr0type == VR_RANGE
8365 && vr1type == VR_RANGE)
8366 *vr0min = vr1min;
8367 else if (*vr0type == VR_ANTI_RANGE
8368 && vr1type == VR_ANTI_RANGE)
8369 *vr0max = vr1max;
8370 else if (*vr0type == VR_ANTI_RANGE
8371 && vr1type == VR_RANGE)
8373 if (TREE_CODE (vr1max) == INTEGER_CST)
8374 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
8375 build_int_cst (TREE_TYPE (vr1max), 1));
8376 else
8377 goto give_up;
8379 else if (*vr0type == VR_RANGE
8380 && vr1type == VR_ANTI_RANGE)
8382 if (TREE_CODE (*vr0min) == INTEGER_CST)
8384 *vr0type = vr1type;
8385 *vr0min = vr1min;
8386 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
8387 build_int_cst (TREE_TYPE (*vr0min), 1));
8389 else
8390 goto give_up;
8392 else
8393 gcc_unreachable ();
8395 else
8396 goto give_up;
8398 return;
8400 give_up:
8401 *vr0type = VR_VARYING;
8402 *vr0min = NULL_TREE;
8403 *vr0max = NULL_TREE;
8406 /* Intersect the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
8407 { VR1TYPE, VR0MIN, VR0MAX } and store the result
8408 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
8409 possible such range. The resulting range is not canonicalized. */
8411 static void
8412 intersect_ranges (enum value_range_type *vr0type,
8413 tree *vr0min, tree *vr0max,
8414 enum value_range_type vr1type,
8415 tree vr1min, tree vr1max)
8417 bool mineq = vrp_operand_equal_p (*vr0min, vr1min);
8418 bool maxeq = vrp_operand_equal_p (*vr0max, vr1max);
8420 /* [] is vr0, () is vr1 in the following classification comments. */
8421 if (mineq && maxeq)
8423 /* [( )] */
8424 if (*vr0type == vr1type)
8425 /* Nothing to do for equal ranges. */
8427 else if ((*vr0type == VR_RANGE
8428 && vr1type == VR_ANTI_RANGE)
8429 || (*vr0type == VR_ANTI_RANGE
8430 && vr1type == VR_RANGE))
8432 /* For anti-range with range intersection the result is empty. */
8433 *vr0type = VR_UNDEFINED;
8434 *vr0min = NULL_TREE;
8435 *vr0max = NULL_TREE;
8437 else
8438 gcc_unreachable ();
8440 else if (operand_less_p (*vr0max, vr1min) == 1
8441 || operand_less_p (vr1max, *vr0min) == 1)
8443 /* [ ] ( ) or ( ) [ ]
8444 If the ranges have an empty intersection, the result of the
8445 intersect operation is the range for intersecting an
8446 anti-range with a range or empty when intersecting two ranges. */
8447 if (*vr0type == VR_RANGE
8448 && vr1type == VR_ANTI_RANGE)
8450 else if (*vr0type == VR_ANTI_RANGE
8451 && vr1type == VR_RANGE)
8453 *vr0type = vr1type;
8454 *vr0min = vr1min;
8455 *vr0max = vr1max;
8457 else if (*vr0type == VR_RANGE
8458 && vr1type == VR_RANGE)
8460 *vr0type = VR_UNDEFINED;
8461 *vr0min = NULL_TREE;
8462 *vr0max = NULL_TREE;
8464 else if (*vr0type == VR_ANTI_RANGE
8465 && vr1type == VR_ANTI_RANGE)
8467 /* If the anti-ranges are adjacent to each other merge them. */
8468 if (TREE_CODE (*vr0max) == INTEGER_CST
8469 && TREE_CODE (vr1min) == INTEGER_CST
8470 && operand_less_p (*vr0max, vr1min) == 1
8471 && integer_onep (int_const_binop (MINUS_EXPR,
8472 vr1min, *vr0max)))
8473 *vr0max = vr1max;
8474 else if (TREE_CODE (vr1max) == INTEGER_CST
8475 && TREE_CODE (*vr0min) == INTEGER_CST
8476 && operand_less_p (vr1max, *vr0min) == 1
8477 && integer_onep (int_const_binop (MINUS_EXPR,
8478 *vr0min, vr1max)))
8479 *vr0min = vr1min;
8480 /* Else arbitrarily take VR0. */
8483 else if ((maxeq || operand_less_p (vr1max, *vr0max) == 1)
8484 && (mineq || operand_less_p (*vr0min, vr1min) == 1))
8486 /* [ ( ) ] or [( ) ] or [ ( )] */
8487 if (*vr0type == VR_RANGE
8488 && vr1type == VR_RANGE)
8490 /* If both are ranges the result is the inner one. */
8491 *vr0type = vr1type;
8492 *vr0min = vr1min;
8493 *vr0max = vr1max;
8495 else if (*vr0type == VR_RANGE
8496 && vr1type == VR_ANTI_RANGE)
8498 /* Choose the right gap if the left one is empty. */
8499 if (mineq)
8501 if (TREE_CODE (vr1max) != INTEGER_CST)
8502 *vr0min = vr1max;
8503 else if (TYPE_PRECISION (TREE_TYPE (vr1max)) == 1
8504 && !TYPE_UNSIGNED (TREE_TYPE (vr1max)))
8505 *vr0min
8506 = int_const_binop (MINUS_EXPR, vr1max,
8507 build_int_cst (TREE_TYPE (vr1max), -1));
8508 else
8509 *vr0min
8510 = int_const_binop (PLUS_EXPR, vr1max,
8511 build_int_cst (TREE_TYPE (vr1max), 1));
8513 /* Choose the left gap if the right one is empty. */
8514 else if (maxeq)
8516 if (TREE_CODE (vr1min) != INTEGER_CST)
8517 *vr0max = vr1min;
8518 else if (TYPE_PRECISION (TREE_TYPE (vr1min)) == 1
8519 && !TYPE_UNSIGNED (TREE_TYPE (vr1min)))
8520 *vr0max
8521 = int_const_binop (PLUS_EXPR, vr1min,
8522 build_int_cst (TREE_TYPE (vr1min), -1));
8523 else
8524 *vr0max
8525 = int_const_binop (MINUS_EXPR, vr1min,
8526 build_int_cst (TREE_TYPE (vr1min), 1));
8528 /* Choose the anti-range if the range is effectively varying. */
8529 else if (vrp_val_is_min (*vr0min)
8530 && vrp_val_is_max (*vr0max))
8532 *vr0type = vr1type;
8533 *vr0min = vr1min;
8534 *vr0max = vr1max;
8536 /* Else choose the range. */
8538 else if (*vr0type == VR_ANTI_RANGE
8539 && vr1type == VR_ANTI_RANGE)
8540 /* If both are anti-ranges the result is the outer one. */
8542 else if (*vr0type == VR_ANTI_RANGE
8543 && vr1type == VR_RANGE)
8545 /* The intersection is empty. */
8546 *vr0type = VR_UNDEFINED;
8547 *vr0min = NULL_TREE;
8548 *vr0max = NULL_TREE;
8550 else
8551 gcc_unreachable ();
8553 else if ((maxeq || operand_less_p (*vr0max, vr1max) == 1)
8554 && (mineq || operand_less_p (vr1min, *vr0min) == 1))
8556 /* ( [ ] ) or ([ ] ) or ( [ ]) */
8557 if (*vr0type == VR_RANGE
8558 && vr1type == VR_RANGE)
8559 /* Choose the inner range. */
8561 else if (*vr0type == VR_ANTI_RANGE
8562 && vr1type == VR_RANGE)
8564 /* Choose the right gap if the left is empty. */
8565 if (mineq)
8567 *vr0type = VR_RANGE;
8568 if (TREE_CODE (*vr0max) != INTEGER_CST)
8569 *vr0min = *vr0max;
8570 else if (TYPE_PRECISION (TREE_TYPE (*vr0max)) == 1
8571 && !TYPE_UNSIGNED (TREE_TYPE (*vr0max)))
8572 *vr0min
8573 = int_const_binop (MINUS_EXPR, *vr0max,
8574 build_int_cst (TREE_TYPE (*vr0max), -1));
8575 else
8576 *vr0min
8577 = int_const_binop (PLUS_EXPR, *vr0max,
8578 build_int_cst (TREE_TYPE (*vr0max), 1));
8579 *vr0max = vr1max;
8581 /* Choose the left gap if the right is empty. */
8582 else if (maxeq)
8584 *vr0type = VR_RANGE;
8585 if (TREE_CODE (*vr0min) != INTEGER_CST)
8586 *vr0max = *vr0min;
8587 else if (TYPE_PRECISION (TREE_TYPE (*vr0min)) == 1
8588 && !TYPE_UNSIGNED (TREE_TYPE (*vr0min)))
8589 *vr0max
8590 = int_const_binop (PLUS_EXPR, *vr0min,
8591 build_int_cst (TREE_TYPE (*vr0min), -1));
8592 else
8593 *vr0max
8594 = int_const_binop (MINUS_EXPR, *vr0min,
8595 build_int_cst (TREE_TYPE (*vr0min), 1));
8596 *vr0min = vr1min;
8598 /* Choose the anti-range if the range is effectively varying. */
8599 else if (vrp_val_is_min (vr1min)
8600 && vrp_val_is_max (vr1max))
8602 /* Choose the anti-range if it is ~[0,0], that range is special
8603 enough to special case when vr1's range is relatively wide. */
8604 else if (*vr0min == *vr0max
8605 && integer_zerop (*vr0min)
8606 && (TYPE_PRECISION (TREE_TYPE (*vr0min))
8607 == TYPE_PRECISION (ptr_type_node))
8608 && TREE_CODE (vr1max) == INTEGER_CST
8609 && TREE_CODE (vr1min) == INTEGER_CST
8610 && (wi::clz (wi::sub (vr1max, vr1min))
8611 < TYPE_PRECISION (TREE_TYPE (*vr0min)) / 2))
8613 /* Else choose the range. */
8614 else
8616 *vr0type = vr1type;
8617 *vr0min = vr1min;
8618 *vr0max = vr1max;
8621 else if (*vr0type == VR_ANTI_RANGE
8622 && vr1type == VR_ANTI_RANGE)
8624 /* If both are anti-ranges the result is the outer one. */
8625 *vr0type = vr1type;
8626 *vr0min = vr1min;
8627 *vr0max = vr1max;
8629 else if (vr1type == VR_ANTI_RANGE
8630 && *vr0type == VR_RANGE)
8632 /* The intersection is empty. */
8633 *vr0type = VR_UNDEFINED;
8634 *vr0min = NULL_TREE;
8635 *vr0max = NULL_TREE;
8637 else
8638 gcc_unreachable ();
8640 else if ((operand_less_p (vr1min, *vr0max) == 1
8641 || operand_equal_p (vr1min, *vr0max, 0))
8642 && operand_less_p (*vr0min, vr1min) == 1)
8644 /* [ ( ] ) or [ ]( ) */
8645 if (*vr0type == VR_ANTI_RANGE
8646 && vr1type == VR_ANTI_RANGE)
8647 *vr0max = vr1max;
8648 else if (*vr0type == VR_RANGE
8649 && vr1type == VR_RANGE)
8650 *vr0min = vr1min;
8651 else if (*vr0type == VR_RANGE
8652 && vr1type == VR_ANTI_RANGE)
8654 if (TREE_CODE (vr1min) == INTEGER_CST)
8655 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
8656 build_int_cst (TREE_TYPE (vr1min), 1));
8657 else
8658 *vr0max = vr1min;
8660 else if (*vr0type == VR_ANTI_RANGE
8661 && vr1type == VR_RANGE)
8663 *vr0type = VR_RANGE;
8664 if (TREE_CODE (*vr0max) == INTEGER_CST)
8665 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
8666 build_int_cst (TREE_TYPE (*vr0max), 1));
8667 else
8668 *vr0min = *vr0max;
8669 *vr0max = vr1max;
8671 else
8672 gcc_unreachable ();
8674 else if ((operand_less_p (*vr0min, vr1max) == 1
8675 || operand_equal_p (*vr0min, vr1max, 0))
8676 && operand_less_p (vr1min, *vr0min) == 1)
8678 /* ( [ ) ] or ( )[ ] */
8679 if (*vr0type == VR_ANTI_RANGE
8680 && vr1type == VR_ANTI_RANGE)
8681 *vr0min = vr1min;
8682 else if (*vr0type == VR_RANGE
8683 && vr1type == VR_RANGE)
8684 *vr0max = vr1max;
8685 else if (*vr0type == VR_RANGE
8686 && vr1type == VR_ANTI_RANGE)
8688 if (TREE_CODE (vr1max) == INTEGER_CST)
8689 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
8690 build_int_cst (TREE_TYPE (vr1max), 1));
8691 else
8692 *vr0min = vr1max;
8694 else if (*vr0type == VR_ANTI_RANGE
8695 && vr1type == VR_RANGE)
8697 *vr0type = VR_RANGE;
8698 if (TREE_CODE (*vr0min) == INTEGER_CST)
8699 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
8700 build_int_cst (TREE_TYPE (*vr0min), 1));
8701 else
8702 *vr0max = *vr0min;
8703 *vr0min = vr1min;
8705 else
8706 gcc_unreachable ();
8709 /* As a fallback simply use { *VRTYPE, *VR0MIN, *VR0MAX } as
8710 result for the intersection. That's always a conservative
8711 correct estimate unless VR1 is a constant singleton range
8712 in which case we choose that. */
8713 if (vr1type == VR_RANGE
8714 && is_gimple_min_invariant (vr1min)
8715 && vrp_operand_equal_p (vr1min, vr1max))
8717 *vr0type = vr1type;
8718 *vr0min = vr1min;
8719 *vr0max = vr1max;
8722 return;
8726 /* Intersect the two value-ranges *VR0 and *VR1 and store the result
8727 in *VR0. This may not be the smallest possible such range. */
8729 static void
8730 vrp_intersect_ranges_1 (value_range *vr0, value_range *vr1)
8732 value_range saved;
8734 /* If either range is VR_VARYING the other one wins. */
8735 if (vr1->type == VR_VARYING)
8736 return;
8737 if (vr0->type == VR_VARYING)
8739 copy_value_range (vr0, vr1);
8740 return;
8743 /* When either range is VR_UNDEFINED the resulting range is
8744 VR_UNDEFINED, too. */
8745 if (vr0->type == VR_UNDEFINED)
8746 return;
8747 if (vr1->type == VR_UNDEFINED)
8749 set_value_range_to_undefined (vr0);
8750 return;
8753 /* Save the original vr0 so we can return it as conservative intersection
8754 result when our worker turns things to varying. */
8755 saved = *vr0;
8756 intersect_ranges (&vr0->type, &vr0->min, &vr0->max,
8757 vr1->type, vr1->min, vr1->max);
8758 /* Make sure to canonicalize the result though as the inversion of a
8759 VR_RANGE can still be a VR_RANGE. */
8760 set_and_canonicalize_value_range (vr0, vr0->type,
8761 vr0->min, vr0->max, vr0->equiv);
8762 /* If that failed, use the saved original VR0. */
8763 if (vr0->type == VR_VARYING)
8765 *vr0 = saved;
8766 return;
8768 /* If the result is VR_UNDEFINED there is no need to mess with
8769 the equivalencies. */
8770 if (vr0->type == VR_UNDEFINED)
8771 return;
8773 /* The resulting set of equivalences for range intersection is the union of
8774 the two sets. */
8775 if (vr0->equiv && vr1->equiv && vr0->equiv != vr1->equiv)
8776 bitmap_ior_into (vr0->equiv, vr1->equiv);
8777 else if (vr1->equiv && !vr0->equiv)
8779 vr0->equiv = BITMAP_ALLOC (&vrp_equiv_obstack);
8780 bitmap_copy (vr0->equiv, vr1->equiv);
8784 void
8785 vrp_intersect_ranges (value_range *vr0, value_range *vr1)
8787 if (dump_file && (dump_flags & TDF_DETAILS))
8789 fprintf (dump_file, "Intersecting\n ");
8790 dump_value_range (dump_file, vr0);
8791 fprintf (dump_file, "\nand\n ");
8792 dump_value_range (dump_file, vr1);
8793 fprintf (dump_file, "\n");
8795 vrp_intersect_ranges_1 (vr0, vr1);
8796 if (dump_file && (dump_flags & TDF_DETAILS))
8798 fprintf (dump_file, "to\n ");
8799 dump_value_range (dump_file, vr0);
8800 fprintf (dump_file, "\n");
8804 /* Meet operation for value ranges. Given two value ranges VR0 and
8805 VR1, store in VR0 a range that contains both VR0 and VR1. This
8806 may not be the smallest possible such range. */
8808 static void
8809 vrp_meet_1 (value_range *vr0, const value_range *vr1)
8811 value_range saved;
8813 if (vr0->type == VR_UNDEFINED)
8815 set_value_range (vr0, vr1->type, vr1->min, vr1->max, vr1->equiv);
8816 return;
8819 if (vr1->type == VR_UNDEFINED)
8821 /* VR0 already has the resulting range. */
8822 return;
8825 if (vr0->type == VR_VARYING)
8827 /* Nothing to do. VR0 already has the resulting range. */
8828 return;
8831 if (vr1->type == VR_VARYING)
8833 set_value_range_to_varying (vr0);
8834 return;
8837 saved = *vr0;
8838 union_ranges (&vr0->type, &vr0->min, &vr0->max,
8839 vr1->type, vr1->min, vr1->max);
8840 if (vr0->type == VR_VARYING)
8842 /* Failed to find an efficient meet. Before giving up and setting
8843 the result to VARYING, see if we can at least derive a useful
8844 anti-range. FIXME, all this nonsense about distinguishing
8845 anti-ranges from ranges is necessary because of the odd
8846 semantics of range_includes_zero_p and friends. */
8847 if (((saved.type == VR_RANGE
8848 && range_includes_zero_p (saved.min, saved.max) == 0)
8849 || (saved.type == VR_ANTI_RANGE
8850 && range_includes_zero_p (saved.min, saved.max) == 1))
8851 && ((vr1->type == VR_RANGE
8852 && range_includes_zero_p (vr1->min, vr1->max) == 0)
8853 || (vr1->type == VR_ANTI_RANGE
8854 && range_includes_zero_p (vr1->min, vr1->max) == 1)))
8856 set_value_range_to_nonnull (vr0, TREE_TYPE (saved.min));
8858 /* Since this meet operation did not result from the meeting of
8859 two equivalent names, VR0 cannot have any equivalences. */
8860 if (vr0->equiv)
8861 bitmap_clear (vr0->equiv);
8862 return;
8865 set_value_range_to_varying (vr0);
8866 return;
8868 set_and_canonicalize_value_range (vr0, vr0->type, vr0->min, vr0->max,
8869 vr0->equiv);
8870 if (vr0->type == VR_VARYING)
8871 return;
8873 /* The resulting set of equivalences is always the intersection of
8874 the two sets. */
8875 if (vr0->equiv && vr1->equiv && vr0->equiv != vr1->equiv)
8876 bitmap_and_into (vr0->equiv, vr1->equiv);
8877 else if (vr0->equiv && !vr1->equiv)
8878 bitmap_clear (vr0->equiv);
8881 void
8882 vrp_meet (value_range *vr0, const value_range *vr1)
8884 if (dump_file && (dump_flags & TDF_DETAILS))
8886 fprintf (dump_file, "Meeting\n ");
8887 dump_value_range (dump_file, vr0);
8888 fprintf (dump_file, "\nand\n ");
8889 dump_value_range (dump_file, vr1);
8890 fprintf (dump_file, "\n");
8892 vrp_meet_1 (vr0, vr1);
8893 if (dump_file && (dump_flags & TDF_DETAILS))
8895 fprintf (dump_file, "to\n ");
8896 dump_value_range (dump_file, vr0);
8897 fprintf (dump_file, "\n");
8902 /* Visit all arguments for PHI node PHI that flow through executable
8903 edges. If a valid value range can be derived from all the incoming
8904 value ranges, set a new range in VR_RESULT. */
8906 static void
8907 extract_range_from_phi_node (gphi *phi, value_range *vr_result)
8909 size_t i;
8910 tree lhs = PHI_RESULT (phi);
8911 value_range *lhs_vr = get_value_range (lhs);
8912 bool first = true;
8913 int edges, old_edges;
8914 struct loop *l;
8916 if (dump_file && (dump_flags & TDF_DETAILS))
8918 fprintf (dump_file, "\nVisiting PHI node: ");
8919 print_gimple_stmt (dump_file, phi, 0, dump_flags);
8922 bool may_simulate_backedge_again = false;
8923 edges = 0;
8924 for (i = 0; i < gimple_phi_num_args (phi); i++)
8926 edge e = gimple_phi_arg_edge (phi, i);
8928 if (dump_file && (dump_flags & TDF_DETAILS))
8930 fprintf (dump_file,
8931 " Argument #%d (%d -> %d %sexecutable)\n",
8932 (int) i, e->src->index, e->dest->index,
8933 (e->flags & EDGE_EXECUTABLE) ? "" : "not ");
8936 if (e->flags & EDGE_EXECUTABLE)
8938 tree arg = PHI_ARG_DEF (phi, i);
8939 value_range vr_arg;
8941 ++edges;
8943 if (TREE_CODE (arg) == SSA_NAME)
8945 /* See if we are eventually going to change one of the args. */
8946 gimple *def_stmt = SSA_NAME_DEF_STMT (arg);
8947 if (! gimple_nop_p (def_stmt)
8948 && prop_simulate_again_p (def_stmt)
8949 && e->flags & EDGE_DFS_BACK)
8950 may_simulate_backedge_again = true;
8952 vr_arg = *(get_value_range (arg));
8953 /* Do not allow equivalences or symbolic ranges to leak in from
8954 backedges. That creates invalid equivalencies.
8955 See PR53465 and PR54767. */
8956 if (e->flags & EDGE_DFS_BACK)
8958 if (vr_arg.type == VR_RANGE
8959 || vr_arg.type == VR_ANTI_RANGE)
8961 vr_arg.equiv = NULL;
8962 if (symbolic_range_p (&vr_arg))
8964 vr_arg.type = VR_VARYING;
8965 vr_arg.min = NULL_TREE;
8966 vr_arg.max = NULL_TREE;
8970 else
8972 /* If the non-backedge arguments range is VR_VARYING then
8973 we can still try recording a simple equivalence. */
8974 if (vr_arg.type == VR_VARYING)
8976 vr_arg.type = VR_RANGE;
8977 vr_arg.min = arg;
8978 vr_arg.max = arg;
8979 vr_arg.equiv = NULL;
8983 else
8985 if (TREE_OVERFLOW_P (arg))
8986 arg = drop_tree_overflow (arg);
8988 vr_arg.type = VR_RANGE;
8989 vr_arg.min = arg;
8990 vr_arg.max = arg;
8991 vr_arg.equiv = NULL;
8994 if (dump_file && (dump_flags & TDF_DETAILS))
8996 fprintf (dump_file, "\t");
8997 print_generic_expr (dump_file, arg, dump_flags);
8998 fprintf (dump_file, ": ");
8999 dump_value_range (dump_file, &vr_arg);
9000 fprintf (dump_file, "\n");
9003 if (first)
9004 copy_value_range (vr_result, &vr_arg);
9005 else
9006 vrp_meet (vr_result, &vr_arg);
9007 first = false;
9009 if (vr_result->type == VR_VARYING)
9010 break;
9014 if (vr_result->type == VR_VARYING)
9015 goto varying;
9016 else if (vr_result->type == VR_UNDEFINED)
9017 goto update_range;
9019 old_edges = vr_phi_edge_counts[SSA_NAME_VERSION (lhs)];
9020 vr_phi_edge_counts[SSA_NAME_VERSION (lhs)] = edges;
9022 /* To prevent infinite iterations in the algorithm, derive ranges
9023 when the new value is slightly bigger or smaller than the
9024 previous one. We don't do this if we have seen a new executable
9025 edge; this helps us avoid an infinity for conditionals
9026 which are not in a loop. If the old value-range was VR_UNDEFINED
9027 use the updated range and iterate one more time. If we will not
9028 simulate this PHI again via the backedge allow us to iterate. */
9029 if (edges > 0
9030 && gimple_phi_num_args (phi) > 1
9031 && edges == old_edges
9032 && lhs_vr->type != VR_UNDEFINED
9033 && may_simulate_backedge_again)
9035 /* Compare old and new ranges, fall back to varying if the
9036 values are not comparable. */
9037 int cmp_min = compare_values (lhs_vr->min, vr_result->min);
9038 if (cmp_min == -2)
9039 goto varying;
9040 int cmp_max = compare_values (lhs_vr->max, vr_result->max);
9041 if (cmp_max == -2)
9042 goto varying;
9044 /* For non VR_RANGE or for pointers fall back to varying if
9045 the range changed. */
9046 if ((lhs_vr->type != VR_RANGE || vr_result->type != VR_RANGE
9047 || POINTER_TYPE_P (TREE_TYPE (lhs)))
9048 && (cmp_min != 0 || cmp_max != 0))
9049 goto varying;
9051 /* If the new minimum is larger than the previous one
9052 retain the old value. If the new minimum value is smaller
9053 than the previous one and not -INF go all the way to -INF + 1.
9054 In the first case, to avoid infinite bouncing between different
9055 minimums, and in the other case to avoid iterating millions of
9056 times to reach -INF. Going to -INF + 1 also lets the following
9057 iteration compute whether there will be any overflow, at the
9058 expense of one additional iteration. */
9059 if (cmp_min < 0)
9060 vr_result->min = lhs_vr->min;
9061 else if (cmp_min > 0
9062 && !vrp_val_is_min (vr_result->min))
9063 vr_result->min
9064 = int_const_binop (PLUS_EXPR,
9065 vrp_val_min (TREE_TYPE (vr_result->min)),
9066 build_int_cst (TREE_TYPE (vr_result->min), 1));
9068 /* Similarly for the maximum value. */
9069 if (cmp_max > 0)
9070 vr_result->max = lhs_vr->max;
9071 else if (cmp_max < 0
9072 && !vrp_val_is_max (vr_result->max))
9073 vr_result->max
9074 = int_const_binop (MINUS_EXPR,
9075 vrp_val_max (TREE_TYPE (vr_result->min)),
9076 build_int_cst (TREE_TYPE (vr_result->min), 1));
9078 /* If we dropped either bound to +-INF then if this is a loop
9079 PHI node SCEV may known more about its value-range. */
9080 if (cmp_min > 0 || cmp_min < 0
9081 || cmp_max < 0 || cmp_max > 0)
9082 goto scev_check;
9084 goto infinite_check;
9087 goto update_range;
9089 varying:
9090 set_value_range_to_varying (vr_result);
9092 scev_check:
9093 /* If this is a loop PHI node SCEV may known more about its value-range.
9094 scev_check can be reached from two paths, one is a fall through from above
9095 "varying" label, the other is direct goto from code block which tries to
9096 avoid infinite simulation. */
9097 if ((l = loop_containing_stmt (phi))
9098 && l->header == gimple_bb (phi))
9099 adjust_range_with_scev (vr_result, l, phi, lhs);
9101 infinite_check:
9102 /* If we will end up with a (-INF, +INF) range, set it to
9103 VARYING. Same if the previous max value was invalid for
9104 the type and we end up with vr_result.min > vr_result.max. */
9105 if ((vr_result->type == VR_RANGE || vr_result->type == VR_ANTI_RANGE)
9106 && !((vrp_val_is_max (vr_result->max) && vrp_val_is_min (vr_result->min))
9107 || compare_values (vr_result->min, vr_result->max) > 0))
9109 else
9110 set_value_range_to_varying (vr_result);
9112 /* If the new range is different than the previous value, keep
9113 iterating. */
9114 update_range:
9115 return;
9118 /* Visit all arguments for PHI node PHI that flow through executable
9119 edges. If a valid value range can be derived from all the incoming
9120 value ranges, set a new range for the LHS of PHI. */
9122 static enum ssa_prop_result
9123 vrp_visit_phi_node (gphi *phi)
9125 tree lhs = PHI_RESULT (phi);
9126 value_range vr_result = VR_INITIALIZER;
9127 extract_range_from_phi_node (phi, &vr_result);
9128 if (update_value_range (lhs, &vr_result))
9130 if (dump_file && (dump_flags & TDF_DETAILS))
9132 fprintf (dump_file, "Found new range for ");
9133 print_generic_expr (dump_file, lhs);
9134 fprintf (dump_file, ": ");
9135 dump_value_range (dump_file, &vr_result);
9136 fprintf (dump_file, "\n");
9139 if (vr_result.type == VR_VARYING)
9140 return SSA_PROP_VARYING;
9142 return SSA_PROP_INTERESTING;
9145 /* Nothing changed, don't add outgoing edges. */
9146 return SSA_PROP_NOT_INTERESTING;
9149 /* Simplify boolean operations if the source is known
9150 to be already a boolean. */
9151 static bool
9152 simplify_truth_ops_using_ranges (gimple_stmt_iterator *gsi, gimple *stmt)
9154 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
9155 tree lhs, op0, op1;
9156 bool need_conversion;
9158 /* We handle only !=/== case here. */
9159 gcc_assert (rhs_code == EQ_EXPR || rhs_code == NE_EXPR);
9161 op0 = gimple_assign_rhs1 (stmt);
9162 if (!op_with_boolean_value_range_p (op0))
9163 return false;
9165 op1 = gimple_assign_rhs2 (stmt);
9166 if (!op_with_boolean_value_range_p (op1))
9167 return false;
9169 /* Reduce number of cases to handle to NE_EXPR. As there is no
9170 BIT_XNOR_EXPR we cannot replace A == B with a single statement. */
9171 if (rhs_code == EQ_EXPR)
9173 if (TREE_CODE (op1) == INTEGER_CST)
9174 op1 = int_const_binop (BIT_XOR_EXPR, op1,
9175 build_int_cst (TREE_TYPE (op1), 1));
9176 else
9177 return false;
9180 lhs = gimple_assign_lhs (stmt);
9181 need_conversion
9182 = !useless_type_conversion_p (TREE_TYPE (lhs), TREE_TYPE (op0));
9184 /* Make sure to not sign-extend a 1-bit 1 when converting the result. */
9185 if (need_conversion
9186 && !TYPE_UNSIGNED (TREE_TYPE (op0))
9187 && TYPE_PRECISION (TREE_TYPE (op0)) == 1
9188 && TYPE_PRECISION (TREE_TYPE (lhs)) > 1)
9189 return false;
9191 /* For A != 0 we can substitute A itself. */
9192 if (integer_zerop (op1))
9193 gimple_assign_set_rhs_with_ops (gsi,
9194 need_conversion
9195 ? NOP_EXPR : TREE_CODE (op0), op0);
9196 /* For A != B we substitute A ^ B. Either with conversion. */
9197 else if (need_conversion)
9199 tree tem = make_ssa_name (TREE_TYPE (op0));
9200 gassign *newop
9201 = gimple_build_assign (tem, BIT_XOR_EXPR, op0, op1);
9202 gsi_insert_before (gsi, newop, GSI_SAME_STMT);
9203 if (INTEGRAL_TYPE_P (TREE_TYPE (tem))
9204 && TYPE_PRECISION (TREE_TYPE (tem)) > 1)
9205 set_range_info (tem, VR_RANGE,
9206 wi::zero (TYPE_PRECISION (TREE_TYPE (tem))),
9207 wi::one (TYPE_PRECISION (TREE_TYPE (tem))));
9208 gimple_assign_set_rhs_with_ops (gsi, NOP_EXPR, tem);
9210 /* Or without. */
9211 else
9212 gimple_assign_set_rhs_with_ops (gsi, BIT_XOR_EXPR, op0, op1);
9213 update_stmt (gsi_stmt (*gsi));
9214 fold_stmt (gsi, follow_single_use_edges);
9216 return true;
9219 /* Simplify a division or modulo operator to a right shift or bitwise and
9220 if the first operand is unsigned or is greater than zero and the second
9221 operand is an exact power of two. For TRUNC_MOD_EXPR op0 % op1 with
9222 constant op1 (op1min = op1) or with op1 in [op1min, op1max] range,
9223 optimize it into just op0 if op0's range is known to be a subset of
9224 [-op1min + 1, op1min - 1] for signed and [0, op1min - 1] for unsigned
9225 modulo. */
9227 static bool
9228 simplify_div_or_mod_using_ranges (gimple_stmt_iterator *gsi, gimple *stmt)
9230 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
9231 tree val = NULL;
9232 tree op0 = gimple_assign_rhs1 (stmt);
9233 tree op1 = gimple_assign_rhs2 (stmt);
9234 tree op0min = NULL_TREE, op0max = NULL_TREE;
9235 tree op1min = op1;
9236 value_range *vr = NULL;
9238 if (TREE_CODE (op0) == INTEGER_CST)
9240 op0min = op0;
9241 op0max = op0;
9243 else
9245 vr = get_value_range (op0);
9246 if (range_int_cst_p (vr))
9248 op0min = vr->min;
9249 op0max = vr->max;
9253 if (rhs_code == TRUNC_MOD_EXPR
9254 && TREE_CODE (op1) == SSA_NAME)
9256 value_range *vr1 = get_value_range (op1);
9257 if (range_int_cst_p (vr1))
9258 op1min = vr1->min;
9260 if (rhs_code == TRUNC_MOD_EXPR
9261 && TREE_CODE (op1min) == INTEGER_CST
9262 && tree_int_cst_sgn (op1min) == 1
9263 && op0max
9264 && tree_int_cst_lt (op0max, op1min))
9266 if (TYPE_UNSIGNED (TREE_TYPE (op0))
9267 || tree_int_cst_sgn (op0min) >= 0
9268 || tree_int_cst_lt (fold_unary (NEGATE_EXPR, TREE_TYPE (op1min), op1min),
9269 op0min))
9271 /* If op0 already has the range op0 % op1 has,
9272 then TRUNC_MOD_EXPR won't change anything. */
9273 gimple_assign_set_rhs_from_tree (gsi, op0);
9274 return true;
9278 if (TREE_CODE (op0) != SSA_NAME)
9279 return false;
9281 if (!integer_pow2p (op1))
9283 /* X % -Y can be only optimized into X % Y either if
9284 X is not INT_MIN, or Y is not -1. Fold it now, as after
9285 remove_range_assertions the range info might be not available
9286 anymore. */
9287 if (rhs_code == TRUNC_MOD_EXPR
9288 && fold_stmt (gsi, follow_single_use_edges))
9289 return true;
9290 return false;
9293 if (TYPE_UNSIGNED (TREE_TYPE (op0)))
9294 val = integer_one_node;
9295 else
9297 bool sop = false;
9299 val = compare_range_with_value (GE_EXPR, vr, integer_zero_node, &sop);
9301 if (val
9302 && sop
9303 && integer_onep (val)
9304 && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC))
9306 location_t location;
9308 if (!gimple_has_location (stmt))
9309 location = input_location;
9310 else
9311 location = gimple_location (stmt);
9312 warning_at (location, OPT_Wstrict_overflow,
9313 "assuming signed overflow does not occur when "
9314 "simplifying %</%> or %<%%%> to %<>>%> or %<&%>");
9318 if (val && integer_onep (val))
9320 tree t;
9322 if (rhs_code == TRUNC_DIV_EXPR)
9324 t = build_int_cst (integer_type_node, tree_log2 (op1));
9325 gimple_assign_set_rhs_code (stmt, RSHIFT_EXPR);
9326 gimple_assign_set_rhs1 (stmt, op0);
9327 gimple_assign_set_rhs2 (stmt, t);
9329 else
9331 t = build_int_cst (TREE_TYPE (op1), 1);
9332 t = int_const_binop (MINUS_EXPR, op1, t);
9333 t = fold_convert (TREE_TYPE (op0), t);
9335 gimple_assign_set_rhs_code (stmt, BIT_AND_EXPR);
9336 gimple_assign_set_rhs1 (stmt, op0);
9337 gimple_assign_set_rhs2 (stmt, t);
9340 update_stmt (stmt);
9341 fold_stmt (gsi, follow_single_use_edges);
9342 return true;
9345 return false;
9348 /* Simplify a min or max if the ranges of the two operands are
9349 disjoint. Return true if we do simplify. */
9351 static bool
9352 simplify_min_or_max_using_ranges (gimple_stmt_iterator *gsi, gimple *stmt)
9354 tree op0 = gimple_assign_rhs1 (stmt);
9355 tree op1 = gimple_assign_rhs2 (stmt);
9356 bool sop = false;
9357 tree val;
9359 val = (vrp_evaluate_conditional_warnv_with_ops_using_ranges
9360 (LE_EXPR, op0, op1, &sop));
9361 if (!val)
9363 sop = false;
9364 val = (vrp_evaluate_conditional_warnv_with_ops_using_ranges
9365 (LT_EXPR, op0, op1, &sop));
9368 if (val)
9370 if (sop && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC))
9372 location_t location;
9374 if (!gimple_has_location (stmt))
9375 location = input_location;
9376 else
9377 location = gimple_location (stmt);
9378 warning_at (location, OPT_Wstrict_overflow,
9379 "assuming signed overflow does not occur when "
9380 "simplifying %<min/max (X,Y)%> to %<X%> or %<Y%>");
9383 /* VAL == TRUE -> OP0 < or <= op1
9384 VAL == FALSE -> OP0 > or >= op1. */
9385 tree res = ((gimple_assign_rhs_code (stmt) == MAX_EXPR)
9386 == integer_zerop (val)) ? op0 : op1;
9387 gimple_assign_set_rhs_from_tree (gsi, res);
9388 return true;
9391 return false;
9394 /* If the operand to an ABS_EXPR is >= 0, then eliminate the
9395 ABS_EXPR. If the operand is <= 0, then simplify the
9396 ABS_EXPR into a NEGATE_EXPR. */
9398 static bool
9399 simplify_abs_using_ranges (gimple_stmt_iterator *gsi, gimple *stmt)
9401 tree op = gimple_assign_rhs1 (stmt);
9402 value_range *vr = get_value_range (op);
9404 if (vr)
9406 tree val = NULL;
9407 bool sop = false;
9409 val = compare_range_with_value (LE_EXPR, vr, integer_zero_node, &sop);
9410 if (!val)
9412 /* The range is neither <= 0 nor > 0. Now see if it is
9413 either < 0 or >= 0. */
9414 sop = false;
9415 val = compare_range_with_value (LT_EXPR, vr, integer_zero_node,
9416 &sop);
9419 if (val)
9421 if (sop && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC))
9423 location_t location;
9425 if (!gimple_has_location (stmt))
9426 location = input_location;
9427 else
9428 location = gimple_location (stmt);
9429 warning_at (location, OPT_Wstrict_overflow,
9430 "assuming signed overflow does not occur when "
9431 "simplifying %<abs (X)%> to %<X%> or %<-X%>");
9434 gimple_assign_set_rhs1 (stmt, op);
9435 if (integer_zerop (val))
9436 gimple_assign_set_rhs_code (stmt, SSA_NAME);
9437 else
9438 gimple_assign_set_rhs_code (stmt, NEGATE_EXPR);
9439 update_stmt (stmt);
9440 fold_stmt (gsi, follow_single_use_edges);
9441 return true;
9445 return false;
9448 /* Optimize away redundant BIT_AND_EXPR and BIT_IOR_EXPR.
9449 If all the bits that are being cleared by & are already
9450 known to be zero from VR, or all the bits that are being
9451 set by | are already known to be one from VR, the bit
9452 operation is redundant. */
9454 static bool
9455 simplify_bit_ops_using_ranges (gimple_stmt_iterator *gsi, gimple *stmt)
9457 tree op0 = gimple_assign_rhs1 (stmt);
9458 tree op1 = gimple_assign_rhs2 (stmt);
9459 tree op = NULL_TREE;
9460 value_range vr0 = VR_INITIALIZER;
9461 value_range vr1 = VR_INITIALIZER;
9462 wide_int may_be_nonzero0, may_be_nonzero1;
9463 wide_int must_be_nonzero0, must_be_nonzero1;
9464 wide_int mask;
9466 if (TREE_CODE (op0) == SSA_NAME)
9467 vr0 = *(get_value_range (op0));
9468 else if (is_gimple_min_invariant (op0))
9469 set_value_range_to_value (&vr0, op0, NULL);
9470 else
9471 return false;
9473 if (TREE_CODE (op1) == SSA_NAME)
9474 vr1 = *(get_value_range (op1));
9475 else if (is_gimple_min_invariant (op1))
9476 set_value_range_to_value (&vr1, op1, NULL);
9477 else
9478 return false;
9480 if (!zero_nonzero_bits_from_vr (TREE_TYPE (op0), &vr0, &may_be_nonzero0,
9481 &must_be_nonzero0))
9482 return false;
9483 if (!zero_nonzero_bits_from_vr (TREE_TYPE (op1), &vr1, &may_be_nonzero1,
9484 &must_be_nonzero1))
9485 return false;
9487 switch (gimple_assign_rhs_code (stmt))
9489 case BIT_AND_EXPR:
9490 mask = may_be_nonzero0.and_not (must_be_nonzero1);
9491 if (mask == 0)
9493 op = op0;
9494 break;
9496 mask = may_be_nonzero1.and_not (must_be_nonzero0);
9497 if (mask == 0)
9499 op = op1;
9500 break;
9502 break;
9503 case BIT_IOR_EXPR:
9504 mask = may_be_nonzero0.and_not (must_be_nonzero1);
9505 if (mask == 0)
9507 op = op1;
9508 break;
9510 mask = may_be_nonzero1.and_not (must_be_nonzero0);
9511 if (mask == 0)
9513 op = op0;
9514 break;
9516 break;
9517 default:
9518 gcc_unreachable ();
9521 if (op == NULL_TREE)
9522 return false;
9524 gimple_assign_set_rhs_with_ops (gsi, TREE_CODE (op), op);
9525 update_stmt (gsi_stmt (*gsi));
9526 return true;
9529 /* We are comparing trees OP0 and OP1 using COND_CODE. OP0 has
9530 a known value range VR.
9532 If there is one and only one value which will satisfy the
9533 conditional, then return that value. Else return NULL.
9535 If signed overflow must be undefined for the value to satisfy
9536 the conditional, then set *STRICT_OVERFLOW_P to true. */
9538 static tree
9539 test_for_singularity (enum tree_code cond_code, tree op0,
9540 tree op1, value_range *vr)
9542 tree min = NULL;
9543 tree max = NULL;
9545 /* Extract minimum/maximum values which satisfy the conditional as it was
9546 written. */
9547 if (cond_code == LE_EXPR || cond_code == LT_EXPR)
9549 min = TYPE_MIN_VALUE (TREE_TYPE (op0));
9551 max = op1;
9552 if (cond_code == LT_EXPR)
9554 tree one = build_int_cst (TREE_TYPE (op0), 1);
9555 max = fold_build2 (MINUS_EXPR, TREE_TYPE (op0), max, one);
9556 /* Signal to compare_values_warnv this expr doesn't overflow. */
9557 if (EXPR_P (max))
9558 TREE_NO_WARNING (max) = 1;
9561 else if (cond_code == GE_EXPR || cond_code == GT_EXPR)
9563 max = TYPE_MAX_VALUE (TREE_TYPE (op0));
9565 min = op1;
9566 if (cond_code == GT_EXPR)
9568 tree one = build_int_cst (TREE_TYPE (op0), 1);
9569 min = fold_build2 (PLUS_EXPR, TREE_TYPE (op0), min, one);
9570 /* Signal to compare_values_warnv this expr doesn't overflow. */
9571 if (EXPR_P (min))
9572 TREE_NO_WARNING (min) = 1;
9576 /* Now refine the minimum and maximum values using any
9577 value range information we have for op0. */
9578 if (min && max)
9580 if (compare_values (vr->min, min) == 1)
9581 min = vr->min;
9582 if (compare_values (vr->max, max) == -1)
9583 max = vr->max;
9585 /* If the new min/max values have converged to a single value,
9586 then there is only one value which can satisfy the condition,
9587 return that value. */
9588 if (operand_equal_p (min, max, 0) && is_gimple_min_invariant (min))
9589 return min;
9591 return NULL;
9594 /* Return whether the value range *VR fits in an integer type specified
9595 by PRECISION and UNSIGNED_P. */
9597 static bool
9598 range_fits_type_p (value_range *vr, unsigned dest_precision, signop dest_sgn)
9600 tree src_type;
9601 unsigned src_precision;
9602 widest_int tem;
9603 signop src_sgn;
9605 /* We can only handle integral and pointer types. */
9606 src_type = TREE_TYPE (vr->min);
9607 if (!INTEGRAL_TYPE_P (src_type)
9608 && !POINTER_TYPE_P (src_type))
9609 return false;
9611 /* An extension is fine unless VR is SIGNED and dest_sgn is UNSIGNED,
9612 and so is an identity transform. */
9613 src_precision = TYPE_PRECISION (TREE_TYPE (vr->min));
9614 src_sgn = TYPE_SIGN (src_type);
9615 if ((src_precision < dest_precision
9616 && !(dest_sgn == UNSIGNED && src_sgn == SIGNED))
9617 || (src_precision == dest_precision && src_sgn == dest_sgn))
9618 return true;
9620 /* Now we can only handle ranges with constant bounds. */
9621 if (vr->type != VR_RANGE
9622 || TREE_CODE (vr->min) != INTEGER_CST
9623 || TREE_CODE (vr->max) != INTEGER_CST)
9624 return false;
9626 /* For sign changes, the MSB of the wide_int has to be clear.
9627 An unsigned value with its MSB set cannot be represented by
9628 a signed wide_int, while a negative value cannot be represented
9629 by an unsigned wide_int. */
9630 if (src_sgn != dest_sgn
9631 && (wi::lts_p (vr->min, 0) || wi::lts_p (vr->max, 0)))
9632 return false;
9634 /* Then we can perform the conversion on both ends and compare
9635 the result for equality. */
9636 tem = wi::ext (wi::to_widest (vr->min), dest_precision, dest_sgn);
9637 if (tem != wi::to_widest (vr->min))
9638 return false;
9639 tem = wi::ext (wi::to_widest (vr->max), dest_precision, dest_sgn);
9640 if (tem != wi::to_widest (vr->max))
9641 return false;
9643 return true;
9646 /* Simplify a conditional using a relational operator to an equality
9647 test if the range information indicates only one value can satisfy
9648 the original conditional. */
9650 static bool
9651 simplify_cond_using_ranges_1 (gcond *stmt)
9653 tree op0 = gimple_cond_lhs (stmt);
9654 tree op1 = gimple_cond_rhs (stmt);
9655 enum tree_code cond_code = gimple_cond_code (stmt);
9657 if (cond_code != NE_EXPR
9658 && cond_code != EQ_EXPR
9659 && TREE_CODE (op0) == SSA_NAME
9660 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
9661 && is_gimple_min_invariant (op1))
9663 value_range *vr = get_value_range (op0);
9665 /* If we have range information for OP0, then we might be
9666 able to simplify this conditional. */
9667 if (vr->type == VR_RANGE)
9669 tree new_tree = test_for_singularity (cond_code, op0, op1, vr);
9670 if (new_tree)
9672 if (dump_file)
9674 fprintf (dump_file, "Simplified relational ");
9675 print_gimple_stmt (dump_file, stmt, 0);
9676 fprintf (dump_file, " into ");
9679 gimple_cond_set_code (stmt, EQ_EXPR);
9680 gimple_cond_set_lhs (stmt, op0);
9681 gimple_cond_set_rhs (stmt, new_tree);
9683 update_stmt (stmt);
9685 if (dump_file)
9687 print_gimple_stmt (dump_file, stmt, 0);
9688 fprintf (dump_file, "\n");
9691 return true;
9694 /* Try again after inverting the condition. We only deal
9695 with integral types here, so no need to worry about
9696 issues with inverting FP comparisons. */
9697 new_tree = test_for_singularity
9698 (invert_tree_comparison (cond_code, false),
9699 op0, op1, vr);
9700 if (new_tree)
9702 if (dump_file)
9704 fprintf (dump_file, "Simplified relational ");
9705 print_gimple_stmt (dump_file, stmt, 0);
9706 fprintf (dump_file, " into ");
9709 gimple_cond_set_code (stmt, NE_EXPR);
9710 gimple_cond_set_lhs (stmt, op0);
9711 gimple_cond_set_rhs (stmt, new_tree);
9713 update_stmt (stmt);
9715 if (dump_file)
9717 print_gimple_stmt (dump_file, stmt, 0);
9718 fprintf (dump_file, "\n");
9721 return true;
9725 return false;
9728 /* STMT is a conditional at the end of a basic block.
9730 If the conditional is of the form SSA_NAME op constant and the SSA_NAME
9731 was set via a type conversion, try to replace the SSA_NAME with the RHS
9732 of the type conversion. Doing so makes the conversion dead which helps
9733 subsequent passes. */
9735 static void
9736 simplify_cond_using_ranges_2 (gcond *stmt)
9738 tree op0 = gimple_cond_lhs (stmt);
9739 tree op1 = gimple_cond_rhs (stmt);
9741 /* If we have a comparison of an SSA_NAME (OP0) against a constant,
9742 see if OP0 was set by a type conversion where the source of
9743 the conversion is another SSA_NAME with a range that fits
9744 into the range of OP0's type.
9746 If so, the conversion is redundant as the earlier SSA_NAME can be
9747 used for the comparison directly if we just massage the constant in the
9748 comparison. */
9749 if (TREE_CODE (op0) == SSA_NAME
9750 && TREE_CODE (op1) == INTEGER_CST)
9752 gimple *def_stmt = SSA_NAME_DEF_STMT (op0);
9753 tree innerop;
9755 if (!is_gimple_assign (def_stmt)
9756 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt)))
9757 return;
9759 innerop = gimple_assign_rhs1 (def_stmt);
9761 if (TREE_CODE (innerop) == SSA_NAME
9762 && !POINTER_TYPE_P (TREE_TYPE (innerop))
9763 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (innerop)
9764 && desired_pro_or_demotion_p (TREE_TYPE (innerop), TREE_TYPE (op0)))
9766 value_range *vr = get_value_range (innerop);
9768 if (range_int_cst_p (vr)
9769 && range_fits_type_p (vr,
9770 TYPE_PRECISION (TREE_TYPE (op0)),
9771 TYPE_SIGN (TREE_TYPE (op0)))
9772 && int_fits_type_p (op1, TREE_TYPE (innerop)))
9774 tree newconst = fold_convert (TREE_TYPE (innerop), op1);
9775 gimple_cond_set_lhs (stmt, innerop);
9776 gimple_cond_set_rhs (stmt, newconst);
9777 update_stmt (stmt);
9778 if (dump_file && (dump_flags & TDF_DETAILS))
9780 fprintf (dump_file, "Folded into: ");
9781 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
9782 fprintf (dump_file, "\n");
9789 /* Simplify a switch statement using the value range of the switch
9790 argument. */
9792 static bool
9793 simplify_switch_using_ranges (gswitch *stmt)
9795 tree op = gimple_switch_index (stmt);
9796 value_range *vr = NULL;
9797 bool take_default;
9798 edge e;
9799 edge_iterator ei;
9800 size_t i = 0, j = 0, n, n2;
9801 tree vec2;
9802 switch_update su;
9803 size_t k = 1, l = 0;
9805 if (TREE_CODE (op) == SSA_NAME)
9807 vr = get_value_range (op);
9809 /* We can only handle integer ranges. */
9810 if ((vr->type != VR_RANGE
9811 && vr->type != VR_ANTI_RANGE)
9812 || symbolic_range_p (vr))
9813 return false;
9815 /* Find case label for min/max of the value range. */
9816 take_default = !find_case_label_ranges (stmt, vr, &i, &j, &k, &l);
9818 else if (TREE_CODE (op) == INTEGER_CST)
9820 take_default = !find_case_label_index (stmt, 1, op, &i);
9821 if (take_default)
9823 i = 1;
9824 j = 0;
9826 else
9828 j = i;
9831 else
9832 return false;
9834 n = gimple_switch_num_labels (stmt);
9836 /* We can truncate the case label ranges that partially overlap with OP's
9837 value range. */
9838 size_t min_idx = 1, max_idx = 0;
9839 if (vr != NULL)
9840 find_case_label_range (stmt, vr->min, vr->max, &min_idx, &max_idx);
9841 if (min_idx <= max_idx)
9843 tree min_label = gimple_switch_label (stmt, min_idx);
9844 tree max_label = gimple_switch_label (stmt, max_idx);
9846 /* Avoid changing the type of the case labels when truncating. */
9847 tree case_label_type = TREE_TYPE (CASE_LOW (min_label));
9848 tree vr_min = fold_convert (case_label_type, vr->min);
9849 tree vr_max = fold_convert (case_label_type, vr->max);
9851 if (vr->type == VR_RANGE)
9853 /* If OP's value range is [2,8] and the low label range is
9854 0 ... 3, truncate the label's range to 2 .. 3. */
9855 if (tree_int_cst_compare (CASE_LOW (min_label), vr_min) < 0
9856 && CASE_HIGH (min_label) != NULL_TREE
9857 && tree_int_cst_compare (CASE_HIGH (min_label), vr_min) >= 0)
9858 CASE_LOW (min_label) = vr_min;
9860 /* If OP's value range is [2,8] and the high label range is
9861 7 ... 10, truncate the label's range to 7 .. 8. */
9862 if (tree_int_cst_compare (CASE_LOW (max_label), vr_max) <= 0
9863 && CASE_HIGH (max_label) != NULL_TREE
9864 && tree_int_cst_compare (CASE_HIGH (max_label), vr_max) > 0)
9865 CASE_HIGH (max_label) = vr_max;
9867 else if (vr->type == VR_ANTI_RANGE)
9869 tree one_cst = build_one_cst (case_label_type);
9871 if (min_label == max_label)
9873 /* If OP's value range is ~[7,8] and the label's range is
9874 7 ... 10, truncate the label's range to 9 ... 10. */
9875 if (tree_int_cst_compare (CASE_LOW (min_label), vr_min) == 0
9876 && CASE_HIGH (min_label) != NULL_TREE
9877 && tree_int_cst_compare (CASE_HIGH (min_label), vr_max) > 0)
9878 CASE_LOW (min_label)
9879 = int_const_binop (PLUS_EXPR, vr_max, one_cst);
9881 /* If OP's value range is ~[7,8] and the label's range is
9882 5 ... 8, truncate the label's range to 5 ... 6. */
9883 if (tree_int_cst_compare (CASE_LOW (min_label), vr_min) < 0
9884 && CASE_HIGH (min_label) != NULL_TREE
9885 && tree_int_cst_compare (CASE_HIGH (min_label), vr_max) == 0)
9886 CASE_HIGH (min_label)
9887 = int_const_binop (MINUS_EXPR, vr_min, one_cst);
9889 else
9891 /* If OP's value range is ~[2,8] and the low label range is
9892 0 ... 3, truncate the label's range to 0 ... 1. */
9893 if (tree_int_cst_compare (CASE_LOW (min_label), vr_min) < 0
9894 && CASE_HIGH (min_label) != NULL_TREE
9895 && tree_int_cst_compare (CASE_HIGH (min_label), vr_min) >= 0)
9896 CASE_HIGH (min_label)
9897 = int_const_binop (MINUS_EXPR, vr_min, one_cst);
9899 /* If OP's value range is ~[2,8] and the high label range is
9900 7 ... 10, truncate the label's range to 9 ... 10. */
9901 if (tree_int_cst_compare (CASE_LOW (max_label), vr_max) <= 0
9902 && CASE_HIGH (max_label) != NULL_TREE
9903 && tree_int_cst_compare (CASE_HIGH (max_label), vr_max) > 0)
9904 CASE_LOW (max_label)
9905 = int_const_binop (PLUS_EXPR, vr_max, one_cst);
9909 /* Canonicalize singleton case ranges. */
9910 if (tree_int_cst_equal (CASE_LOW (min_label), CASE_HIGH (min_label)))
9911 CASE_HIGH (min_label) = NULL_TREE;
9912 if (tree_int_cst_equal (CASE_LOW (max_label), CASE_HIGH (max_label)))
9913 CASE_HIGH (max_label) = NULL_TREE;
9916 /* We can also eliminate case labels that lie completely outside OP's value
9917 range. */
9919 /* Bail out if this is just all edges taken. */
9920 if (i == 1
9921 && j == n - 1
9922 && take_default)
9923 return false;
9925 /* Build a new vector of taken case labels. */
9926 vec2 = make_tree_vec (j - i + 1 + l - k + 1 + (int)take_default);
9927 n2 = 0;
9929 /* Add the default edge, if necessary. */
9930 if (take_default)
9931 TREE_VEC_ELT (vec2, n2++) = gimple_switch_default_label (stmt);
9933 for (; i <= j; ++i, ++n2)
9934 TREE_VEC_ELT (vec2, n2) = gimple_switch_label (stmt, i);
9936 for (; k <= l; ++k, ++n2)
9937 TREE_VEC_ELT (vec2, n2) = gimple_switch_label (stmt, k);
9939 /* Mark needed edges. */
9940 for (i = 0; i < n2; ++i)
9942 e = find_edge (gimple_bb (stmt),
9943 label_to_block (CASE_LABEL (TREE_VEC_ELT (vec2, i))));
9944 e->aux = (void *)-1;
9947 /* Queue not needed edges for later removal. */
9948 FOR_EACH_EDGE (e, ei, gimple_bb (stmt)->succs)
9950 if (e->aux == (void *)-1)
9952 e->aux = NULL;
9953 continue;
9956 if (dump_file && (dump_flags & TDF_DETAILS))
9958 fprintf (dump_file, "removing unreachable case label\n");
9960 to_remove_edges.safe_push (e);
9961 e->flags &= ~EDGE_EXECUTABLE;
9964 /* And queue an update for the stmt. */
9965 su.stmt = stmt;
9966 su.vec = vec2;
9967 to_update_switch_stmts.safe_push (su);
9968 return false;
9971 /* Simplify an integral conversion from an SSA name in STMT. */
9973 static bool
9974 simplify_conversion_using_ranges (gimple_stmt_iterator *gsi, gimple *stmt)
9976 tree innerop, middleop, finaltype;
9977 gimple *def_stmt;
9978 signop inner_sgn, middle_sgn, final_sgn;
9979 unsigned inner_prec, middle_prec, final_prec;
9980 widest_int innermin, innermed, innermax, middlemin, middlemed, middlemax;
9982 finaltype = TREE_TYPE (gimple_assign_lhs (stmt));
9983 if (!INTEGRAL_TYPE_P (finaltype))
9984 return false;
9985 middleop = gimple_assign_rhs1 (stmt);
9986 def_stmt = SSA_NAME_DEF_STMT (middleop);
9987 if (!is_gimple_assign (def_stmt)
9988 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt)))
9989 return false;
9990 innerop = gimple_assign_rhs1 (def_stmt);
9991 if (TREE_CODE (innerop) != SSA_NAME
9992 || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (innerop))
9993 return false;
9995 /* Get the value-range of the inner operand. Use get_range_info in
9996 case innerop was created during substitute-and-fold. */
9997 wide_int imin, imax;
9998 if (!INTEGRAL_TYPE_P (TREE_TYPE (innerop))
9999 || get_range_info (innerop, &imin, &imax) != VR_RANGE)
10000 return false;
10001 innermin = widest_int::from (imin, TYPE_SIGN (TREE_TYPE (innerop)));
10002 innermax = widest_int::from (imax, TYPE_SIGN (TREE_TYPE (innerop)));
10004 /* Simulate the conversion chain to check if the result is equal if
10005 the middle conversion is removed. */
10006 inner_prec = TYPE_PRECISION (TREE_TYPE (innerop));
10007 middle_prec = TYPE_PRECISION (TREE_TYPE (middleop));
10008 final_prec = TYPE_PRECISION (finaltype);
10010 /* If the first conversion is not injective, the second must not
10011 be widening. */
10012 if (wi::gtu_p (innermax - innermin,
10013 wi::mask <widest_int> (middle_prec, false))
10014 && middle_prec < final_prec)
10015 return false;
10016 /* We also want a medium value so that we can track the effect that
10017 narrowing conversions with sign change have. */
10018 inner_sgn = TYPE_SIGN (TREE_TYPE (innerop));
10019 if (inner_sgn == UNSIGNED)
10020 innermed = wi::shifted_mask <widest_int> (1, inner_prec - 1, false);
10021 else
10022 innermed = 0;
10023 if (wi::cmp (innermin, innermed, inner_sgn) >= 0
10024 || wi::cmp (innermed, innermax, inner_sgn) >= 0)
10025 innermed = innermin;
10027 middle_sgn = TYPE_SIGN (TREE_TYPE (middleop));
10028 middlemin = wi::ext (innermin, middle_prec, middle_sgn);
10029 middlemed = wi::ext (innermed, middle_prec, middle_sgn);
10030 middlemax = wi::ext (innermax, middle_prec, middle_sgn);
10032 /* Require that the final conversion applied to both the original
10033 and the intermediate range produces the same result. */
10034 final_sgn = TYPE_SIGN (finaltype);
10035 if (wi::ext (middlemin, final_prec, final_sgn)
10036 != wi::ext (innermin, final_prec, final_sgn)
10037 || wi::ext (middlemed, final_prec, final_sgn)
10038 != wi::ext (innermed, final_prec, final_sgn)
10039 || wi::ext (middlemax, final_prec, final_sgn)
10040 != wi::ext (innermax, final_prec, final_sgn))
10041 return false;
10043 gimple_assign_set_rhs1 (stmt, innerop);
10044 fold_stmt (gsi, follow_single_use_edges);
10045 return true;
10048 /* Simplify a conversion from integral SSA name to float in STMT. */
10050 static bool
10051 simplify_float_conversion_using_ranges (gimple_stmt_iterator *gsi,
10052 gimple *stmt)
10054 tree rhs1 = gimple_assign_rhs1 (stmt);
10055 value_range *vr = get_value_range (rhs1);
10056 machine_mode fltmode = TYPE_MODE (TREE_TYPE (gimple_assign_lhs (stmt)));
10057 machine_mode mode;
10058 tree tem;
10059 gassign *conv;
10061 /* We can only handle constant ranges. */
10062 if (vr->type != VR_RANGE
10063 || TREE_CODE (vr->min) != INTEGER_CST
10064 || TREE_CODE (vr->max) != INTEGER_CST)
10065 return false;
10067 /* First check if we can use a signed type in place of an unsigned. */
10068 if (TYPE_UNSIGNED (TREE_TYPE (rhs1))
10069 && (can_float_p (fltmode, TYPE_MODE (TREE_TYPE (rhs1)), 0)
10070 != CODE_FOR_nothing)
10071 && range_fits_type_p (vr, TYPE_PRECISION (TREE_TYPE (rhs1)), SIGNED))
10072 mode = TYPE_MODE (TREE_TYPE (rhs1));
10073 /* If we can do the conversion in the current input mode do nothing. */
10074 else if (can_float_p (fltmode, TYPE_MODE (TREE_TYPE (rhs1)),
10075 TYPE_UNSIGNED (TREE_TYPE (rhs1))) != CODE_FOR_nothing)
10076 return false;
10077 /* Otherwise search for a mode we can use, starting from the narrowest
10078 integer mode available. */
10079 else
10081 mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
10084 /* If we cannot do a signed conversion to float from mode
10085 or if the value-range does not fit in the signed type
10086 try with a wider mode. */
10087 if (can_float_p (fltmode, mode, 0) != CODE_FOR_nothing
10088 && range_fits_type_p (vr, GET_MODE_PRECISION (mode), SIGNED))
10089 break;
10091 mode = GET_MODE_WIDER_MODE (mode);
10092 /* But do not widen the input. Instead leave that to the
10093 optabs expansion code. */
10094 if (GET_MODE_PRECISION (mode) > TYPE_PRECISION (TREE_TYPE (rhs1)))
10095 return false;
10097 while (mode != VOIDmode);
10098 if (mode == VOIDmode)
10099 return false;
10102 /* It works, insert a truncation or sign-change before the
10103 float conversion. */
10104 tem = make_ssa_name (build_nonstandard_integer_type
10105 (GET_MODE_PRECISION (mode), 0));
10106 conv = gimple_build_assign (tem, NOP_EXPR, rhs1);
10107 gsi_insert_before (gsi, conv, GSI_SAME_STMT);
10108 gimple_assign_set_rhs1 (stmt, tem);
10109 fold_stmt (gsi, follow_single_use_edges);
10111 return true;
10114 /* Simplify an internal fn call using ranges if possible. */
10116 static bool
10117 simplify_internal_call_using_ranges (gimple_stmt_iterator *gsi, gimple *stmt)
10119 enum tree_code subcode;
10120 bool is_ubsan = false;
10121 bool ovf = false;
10122 switch (gimple_call_internal_fn (stmt))
10124 case IFN_UBSAN_CHECK_ADD:
10125 subcode = PLUS_EXPR;
10126 is_ubsan = true;
10127 break;
10128 case IFN_UBSAN_CHECK_SUB:
10129 subcode = MINUS_EXPR;
10130 is_ubsan = true;
10131 break;
10132 case IFN_UBSAN_CHECK_MUL:
10133 subcode = MULT_EXPR;
10134 is_ubsan = true;
10135 break;
10136 case IFN_ADD_OVERFLOW:
10137 subcode = PLUS_EXPR;
10138 break;
10139 case IFN_SUB_OVERFLOW:
10140 subcode = MINUS_EXPR;
10141 break;
10142 case IFN_MUL_OVERFLOW:
10143 subcode = MULT_EXPR;
10144 break;
10145 default:
10146 return false;
10149 tree op0 = gimple_call_arg (stmt, 0);
10150 tree op1 = gimple_call_arg (stmt, 1);
10151 tree type;
10152 if (is_ubsan)
10154 type = TREE_TYPE (op0);
10155 if (VECTOR_TYPE_P (type))
10156 return false;
10158 else if (gimple_call_lhs (stmt) == NULL_TREE)
10159 return false;
10160 else
10161 type = TREE_TYPE (TREE_TYPE (gimple_call_lhs (stmt)));
10162 if (!check_for_binary_op_overflow (subcode, type, op0, op1, &ovf)
10163 || (is_ubsan && ovf))
10164 return false;
10166 gimple *g;
10167 location_t loc = gimple_location (stmt);
10168 if (is_ubsan)
10169 g = gimple_build_assign (gimple_call_lhs (stmt), subcode, op0, op1);
10170 else
10172 int prec = TYPE_PRECISION (type);
10173 tree utype = type;
10174 if (ovf
10175 || !useless_type_conversion_p (type, TREE_TYPE (op0))
10176 || !useless_type_conversion_p (type, TREE_TYPE (op1)))
10177 utype = build_nonstandard_integer_type (prec, 1);
10178 if (TREE_CODE (op0) == INTEGER_CST)
10179 op0 = fold_convert (utype, op0);
10180 else if (!useless_type_conversion_p (utype, TREE_TYPE (op0)))
10182 g = gimple_build_assign (make_ssa_name (utype), NOP_EXPR, op0);
10183 gimple_set_location (g, loc);
10184 gsi_insert_before (gsi, g, GSI_SAME_STMT);
10185 op0 = gimple_assign_lhs (g);
10187 if (TREE_CODE (op1) == INTEGER_CST)
10188 op1 = fold_convert (utype, op1);
10189 else if (!useless_type_conversion_p (utype, TREE_TYPE (op1)))
10191 g = gimple_build_assign (make_ssa_name (utype), NOP_EXPR, op1);
10192 gimple_set_location (g, loc);
10193 gsi_insert_before (gsi, g, GSI_SAME_STMT);
10194 op1 = gimple_assign_lhs (g);
10196 g = gimple_build_assign (make_ssa_name (utype), subcode, op0, op1);
10197 gimple_set_location (g, loc);
10198 gsi_insert_before (gsi, g, GSI_SAME_STMT);
10199 if (utype != type)
10201 g = gimple_build_assign (make_ssa_name (type), NOP_EXPR,
10202 gimple_assign_lhs (g));
10203 gimple_set_location (g, loc);
10204 gsi_insert_before (gsi, g, GSI_SAME_STMT);
10206 g = gimple_build_assign (gimple_call_lhs (stmt), COMPLEX_EXPR,
10207 gimple_assign_lhs (g),
10208 build_int_cst (type, ovf));
10210 gimple_set_location (g, loc);
10211 gsi_replace (gsi, g, false);
10212 return true;
10215 /* Return true if VAR is a two-valued variable. Set a and b with the
10216 two-values when it is true. Return false otherwise. */
10218 static bool
10219 two_valued_val_range_p (tree var, tree *a, tree *b)
10221 value_range *vr = get_value_range (var);
10222 if ((vr->type != VR_RANGE
10223 && vr->type != VR_ANTI_RANGE)
10224 || TREE_CODE (vr->min) != INTEGER_CST
10225 || TREE_CODE (vr->max) != INTEGER_CST)
10226 return false;
10228 if (vr->type == VR_RANGE
10229 && wi::sub (vr->max, vr->min) == 1)
10231 *a = vr->min;
10232 *b = vr->max;
10233 return true;
10236 /* ~[TYPE_MIN + 1, TYPE_MAX - 1] */
10237 if (vr->type == VR_ANTI_RANGE
10238 && wi::sub (vr->min, vrp_val_min (TREE_TYPE (var))) == 1
10239 && wi::sub (vrp_val_max (TREE_TYPE (var)), vr->max) == 1)
10241 *a = vrp_val_min (TREE_TYPE (var));
10242 *b = vrp_val_max (TREE_TYPE (var));
10243 return true;
10246 return false;
10249 /* Simplify STMT using ranges if possible. */
10251 static bool
10252 simplify_stmt_using_ranges (gimple_stmt_iterator *gsi)
10254 gimple *stmt = gsi_stmt (*gsi);
10255 if (is_gimple_assign (stmt))
10257 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
10258 tree rhs1 = gimple_assign_rhs1 (stmt);
10259 tree rhs2 = gimple_assign_rhs2 (stmt);
10260 tree lhs = gimple_assign_lhs (stmt);
10261 tree val1 = NULL_TREE, val2 = NULL_TREE;
10262 use_operand_p use_p;
10263 gimple *use_stmt;
10265 /* Convert:
10266 LHS = CST BINOP VAR
10267 Where VAR is two-valued and LHS is used in GIMPLE_COND only
10269 LHS = VAR == VAL1 ? (CST BINOP VAL1) : (CST BINOP VAL2)
10271 Also handles:
10272 LHS = VAR BINOP CST
10273 Where VAR is two-valued and LHS is used in GIMPLE_COND only
10275 LHS = VAR == VAL1 ? (VAL1 BINOP CST) : (VAL2 BINOP CST) */
10277 if (TREE_CODE_CLASS (rhs_code) == tcc_binary
10278 && INTEGRAL_TYPE_P (TREE_TYPE (lhs))
10279 && ((TREE_CODE (rhs1) == INTEGER_CST
10280 && TREE_CODE (rhs2) == SSA_NAME)
10281 || (TREE_CODE (rhs2) == INTEGER_CST
10282 && TREE_CODE (rhs1) == SSA_NAME))
10283 && single_imm_use (lhs, &use_p, &use_stmt)
10284 && gimple_code (use_stmt) == GIMPLE_COND)
10287 tree new_rhs1 = NULL_TREE;
10288 tree new_rhs2 = NULL_TREE;
10289 tree cmp_var = NULL_TREE;
10291 if (TREE_CODE (rhs2) == SSA_NAME
10292 && two_valued_val_range_p (rhs2, &val1, &val2))
10294 /* Optimize RHS1 OP [VAL1, VAL2]. */
10295 new_rhs1 = int_const_binop (rhs_code, rhs1, val1);
10296 new_rhs2 = int_const_binop (rhs_code, rhs1, val2);
10297 cmp_var = rhs2;
10299 else if (TREE_CODE (rhs1) == SSA_NAME
10300 && two_valued_val_range_p (rhs1, &val1, &val2))
10302 /* Optimize [VAL1, VAL2] OP RHS2. */
10303 new_rhs1 = int_const_binop (rhs_code, val1, rhs2);
10304 new_rhs2 = int_const_binop (rhs_code, val2, rhs2);
10305 cmp_var = rhs1;
10308 /* If we could not find two-vals or the optimzation is invalid as
10309 in divide by zero, new_rhs1 / new_rhs will be NULL_TREE. */
10310 if (new_rhs1 && new_rhs2)
10312 tree cond = build2 (EQ_EXPR, boolean_type_node, cmp_var, val1);
10313 gimple_assign_set_rhs_with_ops (gsi,
10314 COND_EXPR, cond,
10315 new_rhs1,
10316 new_rhs2);
10317 update_stmt (gsi_stmt (*gsi));
10318 fold_stmt (gsi, follow_single_use_edges);
10319 return true;
10323 switch (rhs_code)
10325 case EQ_EXPR:
10326 case NE_EXPR:
10327 /* Transform EQ_EXPR, NE_EXPR into BIT_XOR_EXPR or identity
10328 if the RHS is zero or one, and the LHS are known to be boolean
10329 values. */
10330 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
10331 return simplify_truth_ops_using_ranges (gsi, stmt);
10332 break;
10334 /* Transform TRUNC_DIV_EXPR and TRUNC_MOD_EXPR into RSHIFT_EXPR
10335 and BIT_AND_EXPR respectively if the first operand is greater
10336 than zero and the second operand is an exact power of two.
10337 Also optimize TRUNC_MOD_EXPR away if the second operand is
10338 constant and the first operand already has the right value
10339 range. */
10340 case TRUNC_DIV_EXPR:
10341 case TRUNC_MOD_EXPR:
10342 if ((TREE_CODE (rhs1) == SSA_NAME
10343 || TREE_CODE (rhs1) == INTEGER_CST)
10344 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
10345 return simplify_div_or_mod_using_ranges (gsi, stmt);
10346 break;
10348 /* Transform ABS (X) into X or -X as appropriate. */
10349 case ABS_EXPR:
10350 if (TREE_CODE (rhs1) == SSA_NAME
10351 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
10352 return simplify_abs_using_ranges (gsi, stmt);
10353 break;
10355 case BIT_AND_EXPR:
10356 case BIT_IOR_EXPR:
10357 /* Optimize away BIT_AND_EXPR and BIT_IOR_EXPR
10358 if all the bits being cleared are already cleared or
10359 all the bits being set are already set. */
10360 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
10361 return simplify_bit_ops_using_ranges (gsi, stmt);
10362 break;
10364 CASE_CONVERT:
10365 if (TREE_CODE (rhs1) == SSA_NAME
10366 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
10367 return simplify_conversion_using_ranges (gsi, stmt);
10368 break;
10370 case FLOAT_EXPR:
10371 if (TREE_CODE (rhs1) == SSA_NAME
10372 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
10373 return simplify_float_conversion_using_ranges (gsi, stmt);
10374 break;
10376 case MIN_EXPR:
10377 case MAX_EXPR:
10378 return simplify_min_or_max_using_ranges (gsi, stmt);
10380 default:
10381 break;
10384 else if (gimple_code (stmt) == GIMPLE_COND)
10385 return simplify_cond_using_ranges_1 (as_a <gcond *> (stmt));
10386 else if (gimple_code (stmt) == GIMPLE_SWITCH)
10387 return simplify_switch_using_ranges (as_a <gswitch *> (stmt));
10388 else if (is_gimple_call (stmt)
10389 && gimple_call_internal_p (stmt))
10390 return simplify_internal_call_using_ranges (gsi, stmt);
10392 return false;
10395 /* If the statement pointed by SI has a predicate whose value can be
10396 computed using the value range information computed by VRP, compute
10397 its value and return true. Otherwise, return false. */
10399 static bool
10400 fold_predicate_in (gimple_stmt_iterator *si)
10402 bool assignment_p = false;
10403 tree val;
10404 gimple *stmt = gsi_stmt (*si);
10406 if (is_gimple_assign (stmt)
10407 && TREE_CODE_CLASS (gimple_assign_rhs_code (stmt)) == tcc_comparison)
10409 assignment_p = true;
10410 val = vrp_evaluate_conditional (gimple_assign_rhs_code (stmt),
10411 gimple_assign_rhs1 (stmt),
10412 gimple_assign_rhs2 (stmt),
10413 stmt);
10415 else if (gcond *cond_stmt = dyn_cast <gcond *> (stmt))
10416 val = vrp_evaluate_conditional (gimple_cond_code (cond_stmt),
10417 gimple_cond_lhs (cond_stmt),
10418 gimple_cond_rhs (cond_stmt),
10419 stmt);
10420 else
10421 return false;
10423 if (val)
10425 if (assignment_p)
10426 val = fold_convert (gimple_expr_type (stmt), val);
10428 if (dump_file)
10430 fprintf (dump_file, "Folding predicate ");
10431 print_gimple_expr (dump_file, stmt, 0);
10432 fprintf (dump_file, " to ");
10433 print_generic_expr (dump_file, val);
10434 fprintf (dump_file, "\n");
10437 if (is_gimple_assign (stmt))
10438 gimple_assign_set_rhs_from_tree (si, val);
10439 else
10441 gcc_assert (gimple_code (stmt) == GIMPLE_COND);
10442 gcond *cond_stmt = as_a <gcond *> (stmt);
10443 if (integer_zerop (val))
10444 gimple_cond_make_false (cond_stmt);
10445 else if (integer_onep (val))
10446 gimple_cond_make_true (cond_stmt);
10447 else
10448 gcc_unreachable ();
10451 return true;
10454 return false;
10457 /* Callback for substitute_and_fold folding the stmt at *SI. */
10459 static bool
10460 vrp_fold_stmt (gimple_stmt_iterator *si)
10462 if (fold_predicate_in (si))
10463 return true;
10465 return simplify_stmt_using_ranges (si);
10468 /* Return the LHS of any ASSERT_EXPR where OP appears as the first
10469 argument to the ASSERT_EXPR and in which the ASSERT_EXPR dominates
10470 BB. If no such ASSERT_EXPR is found, return OP. */
10472 static tree
10473 lhs_of_dominating_assert (tree op, basic_block bb, gimple *stmt)
10475 imm_use_iterator imm_iter;
10476 gimple *use_stmt;
10477 use_operand_p use_p;
10479 if (TREE_CODE (op) == SSA_NAME)
10481 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, op)
10483 use_stmt = USE_STMT (use_p);
10484 if (use_stmt != stmt
10485 && gimple_assign_single_p (use_stmt)
10486 && TREE_CODE (gimple_assign_rhs1 (use_stmt)) == ASSERT_EXPR
10487 && TREE_OPERAND (gimple_assign_rhs1 (use_stmt), 0) == op
10488 && dominated_by_p (CDI_DOMINATORS, bb, gimple_bb (use_stmt)))
10489 return gimple_assign_lhs (use_stmt);
10492 return op;
10495 /* A trivial wrapper so that we can present the generic jump threading
10496 code with a simple API for simplifying statements. STMT is the
10497 statement we want to simplify, WITHIN_STMT provides the location
10498 for any overflow warnings. */
10500 static tree
10501 simplify_stmt_for_jump_threading (gimple *stmt, gimple *within_stmt,
10502 class avail_exprs_stack *avail_exprs_stack ATTRIBUTE_UNUSED,
10503 basic_block bb)
10505 /* First see if the conditional is in the hash table. */
10506 tree cached_lhs = avail_exprs_stack->lookup_avail_expr (stmt, false, true);
10507 if (cached_lhs && is_gimple_min_invariant (cached_lhs))
10508 return cached_lhs;
10510 if (gcond *cond_stmt = dyn_cast <gcond *> (stmt))
10512 tree op0 = gimple_cond_lhs (cond_stmt);
10513 op0 = lhs_of_dominating_assert (op0, bb, stmt);
10515 tree op1 = gimple_cond_rhs (cond_stmt);
10516 op1 = lhs_of_dominating_assert (op1, bb, stmt);
10518 return vrp_evaluate_conditional (gimple_cond_code (cond_stmt),
10519 op0, op1, within_stmt);
10522 /* We simplify a switch statement by trying to determine which case label
10523 will be taken. If we are successful then we return the corresponding
10524 CASE_LABEL_EXPR. */
10525 if (gswitch *switch_stmt = dyn_cast <gswitch *> (stmt))
10527 tree op = gimple_switch_index (switch_stmt);
10528 if (TREE_CODE (op) != SSA_NAME)
10529 return NULL_TREE;
10531 op = lhs_of_dominating_assert (op, bb, stmt);
10533 value_range *vr = get_value_range (op);
10534 if ((vr->type != VR_RANGE && vr->type != VR_ANTI_RANGE)
10535 || symbolic_range_p (vr))
10536 return NULL_TREE;
10538 if (vr->type == VR_RANGE)
10540 size_t i, j;
10541 /* Get the range of labels that contain a part of the operand's
10542 value range. */
10543 find_case_label_range (switch_stmt, vr->min, vr->max, &i, &j);
10545 /* Is there only one such label? */
10546 if (i == j)
10548 tree label = gimple_switch_label (switch_stmt, i);
10550 /* The i'th label will be taken only if the value range of the
10551 operand is entirely within the bounds of this label. */
10552 if (CASE_HIGH (label) != NULL_TREE
10553 ? (tree_int_cst_compare (CASE_LOW (label), vr->min) <= 0
10554 && tree_int_cst_compare (CASE_HIGH (label), vr->max) >= 0)
10555 : (tree_int_cst_equal (CASE_LOW (label), vr->min)
10556 && tree_int_cst_equal (vr->min, vr->max)))
10557 return label;
10560 /* If there are no such labels then the default label will be
10561 taken. */
10562 if (i > j)
10563 return gimple_switch_label (switch_stmt, 0);
10566 if (vr->type == VR_ANTI_RANGE)
10568 unsigned n = gimple_switch_num_labels (switch_stmt);
10569 tree min_label = gimple_switch_label (switch_stmt, 1);
10570 tree max_label = gimple_switch_label (switch_stmt, n - 1);
10572 /* The default label will be taken only if the anti-range of the
10573 operand is entirely outside the bounds of all the (non-default)
10574 case labels. */
10575 if (tree_int_cst_compare (vr->min, CASE_LOW (min_label)) <= 0
10576 && (CASE_HIGH (max_label) != NULL_TREE
10577 ? tree_int_cst_compare (vr->max, CASE_HIGH (max_label)) >= 0
10578 : tree_int_cst_compare (vr->max, CASE_LOW (max_label)) >= 0))
10579 return gimple_switch_label (switch_stmt, 0);
10582 return NULL_TREE;
10585 if (gassign *assign_stmt = dyn_cast <gassign *> (stmt))
10587 value_range new_vr = VR_INITIALIZER;
10588 tree lhs = gimple_assign_lhs (assign_stmt);
10590 if (TREE_CODE (lhs) == SSA_NAME
10591 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs))
10592 || POINTER_TYPE_P (TREE_TYPE (lhs))))
10594 extract_range_from_assignment (&new_vr, assign_stmt);
10595 if (range_int_cst_singleton_p (&new_vr))
10596 return new_vr.min;
10600 return NULL_TREE;
10603 class vrp_dom_walker : public dom_walker
10605 public:
10606 vrp_dom_walker (cdi_direction direction,
10607 class const_and_copies *const_and_copies,
10608 class avail_exprs_stack *avail_exprs_stack)
10609 : dom_walker (direction, true),
10610 m_const_and_copies (const_and_copies),
10611 m_avail_exprs_stack (avail_exprs_stack),
10612 m_dummy_cond (NULL) {}
10614 virtual edge before_dom_children (basic_block);
10615 virtual void after_dom_children (basic_block);
10617 private:
10618 class const_and_copies *m_const_and_copies;
10619 class avail_exprs_stack *m_avail_exprs_stack;
10621 gcond *m_dummy_cond;
10624 /* Called before processing dominator children of BB. We want to look
10625 at ASSERT_EXPRs and record information from them in the appropriate
10626 tables.
10628 We could look at other statements here. It's not seen as likely
10629 to significantly increase the jump threads we discover. */
10631 edge
10632 vrp_dom_walker::before_dom_children (basic_block bb)
10634 gimple_stmt_iterator gsi;
10636 m_avail_exprs_stack->push_marker ();
10637 m_const_and_copies->push_marker ();
10638 for (gsi = gsi_start_nondebug_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
10640 gimple *stmt = gsi_stmt (gsi);
10641 if (gimple_assign_single_p (stmt)
10642 && TREE_CODE (gimple_assign_rhs1 (stmt)) == ASSERT_EXPR)
10644 tree rhs1 = gimple_assign_rhs1 (stmt);
10645 tree cond = TREE_OPERAND (rhs1, 1);
10646 tree inverted = invert_truthvalue (cond);
10647 vec<cond_equivalence> p;
10648 p.create (3);
10649 record_conditions (&p, cond, inverted);
10650 for (unsigned int i = 0; i < p.length (); i++)
10651 m_avail_exprs_stack->record_cond (&p[i]);
10653 tree lhs = gimple_assign_lhs (stmt);
10654 m_const_and_copies->record_const_or_copy (lhs,
10655 TREE_OPERAND (rhs1, 0));
10656 p.release ();
10657 continue;
10659 break;
10661 return NULL;
10664 /* Called after processing dominator children of BB. This is where we
10665 actually call into the threader. */
10666 void
10667 vrp_dom_walker::after_dom_children (basic_block bb)
10669 if (!m_dummy_cond)
10670 m_dummy_cond = gimple_build_cond (NE_EXPR,
10671 integer_zero_node, integer_zero_node,
10672 NULL, NULL);
10674 thread_outgoing_edges (bb, m_dummy_cond, m_const_and_copies,
10675 m_avail_exprs_stack,
10676 simplify_stmt_for_jump_threading);
10678 m_avail_exprs_stack->pop_to_marker ();
10679 m_const_and_copies->pop_to_marker ();
10682 /* Blocks which have more than one predecessor and more than
10683 one successor present jump threading opportunities, i.e.,
10684 when the block is reached from a specific predecessor, we
10685 may be able to determine which of the outgoing edges will
10686 be traversed. When this optimization applies, we are able
10687 to avoid conditionals at runtime and we may expose secondary
10688 optimization opportunities.
10690 This routine is effectively a driver for the generic jump
10691 threading code. It basically just presents the generic code
10692 with edges that may be suitable for jump threading.
10694 Unlike DOM, we do not iterate VRP if jump threading was successful.
10695 While iterating may expose new opportunities for VRP, it is expected
10696 those opportunities would be very limited and the compile time cost
10697 to expose those opportunities would be significant.
10699 As jump threading opportunities are discovered, they are registered
10700 for later realization. */
10702 static void
10703 identify_jump_threads (void)
10705 int i;
10706 edge e;
10708 /* Ugh. When substituting values earlier in this pass we can
10709 wipe the dominance information. So rebuild the dominator
10710 information as we need it within the jump threading code. */
10711 calculate_dominance_info (CDI_DOMINATORS);
10713 /* We do not allow VRP information to be used for jump threading
10714 across a back edge in the CFG. Otherwise it becomes too
10715 difficult to avoid eliminating loop exit tests. Of course
10716 EDGE_DFS_BACK is not accurate at this time so we have to
10717 recompute it. */
10718 mark_dfs_back_edges ();
10720 /* Do not thread across edges we are about to remove. Just marking
10721 them as EDGE_IGNORE will do. */
10722 FOR_EACH_VEC_ELT (to_remove_edges, i, e)
10723 e->flags |= EDGE_IGNORE;
10725 /* Allocate our unwinder stack to unwind any temporary equivalences
10726 that might be recorded. */
10727 const_and_copies *equiv_stack = new const_and_copies ();
10729 hash_table<expr_elt_hasher> *avail_exprs
10730 = new hash_table<expr_elt_hasher> (1024);
10731 avail_exprs_stack *avail_exprs_stack
10732 = new class avail_exprs_stack (avail_exprs);
10734 vrp_dom_walker walker (CDI_DOMINATORS, equiv_stack, avail_exprs_stack);
10735 walker.walk (cfun->cfg->x_entry_block_ptr);
10737 /* Clear EDGE_IGNORE. */
10738 FOR_EACH_VEC_ELT (to_remove_edges, i, e)
10739 e->flags &= ~EDGE_IGNORE;
10741 /* We do not actually update the CFG or SSA graphs at this point as
10742 ASSERT_EXPRs are still in the IL and cfg cleanup code does not yet
10743 handle ASSERT_EXPRs gracefully. */
10744 delete equiv_stack;
10745 delete avail_exprs;
10746 delete avail_exprs_stack;
10749 /* Free VRP lattice. */
10751 static void
10752 vrp_free_lattice ()
10754 /* Free allocated memory. */
10755 free (vr_value);
10756 free (vr_phi_edge_counts);
10757 bitmap_obstack_release (&vrp_equiv_obstack);
10758 vrp_value_range_pool.release ();
10760 /* So that we can distinguish between VRP data being available
10761 and not available. */
10762 vr_value = NULL;
10763 vr_phi_edge_counts = NULL;
10766 /* Traverse all the blocks folding conditionals with known ranges. */
10768 static void
10769 vrp_finalize (bool warn_array_bounds_p)
10771 size_t i;
10773 values_propagated = true;
10775 if (dump_file)
10777 fprintf (dump_file, "\nValue ranges after VRP:\n\n");
10778 dump_all_value_ranges (dump_file);
10779 fprintf (dump_file, "\n");
10782 /* Set value range to non pointer SSA_NAMEs. */
10783 for (i = 0; i < num_vr_values; i++)
10784 if (vr_value[i])
10786 tree name = ssa_name (i);
10788 if (!name
10789 || (vr_value[i]->type == VR_VARYING)
10790 || (vr_value[i]->type == VR_UNDEFINED)
10791 || (TREE_CODE (vr_value[i]->min) != INTEGER_CST)
10792 || (TREE_CODE (vr_value[i]->max) != INTEGER_CST))
10793 continue;
10795 if (POINTER_TYPE_P (TREE_TYPE (name))
10796 && ((vr_value[i]->type == VR_RANGE
10797 && range_includes_zero_p (vr_value[i]->min,
10798 vr_value[i]->max) == 0)
10799 || (vr_value[i]->type == VR_ANTI_RANGE
10800 && range_includes_zero_p (vr_value[i]->min,
10801 vr_value[i]->max) == 1)))
10802 set_ptr_nonnull (name);
10803 else if (!POINTER_TYPE_P (TREE_TYPE (name)))
10804 set_range_info (name, vr_value[i]->type, vr_value[i]->min,
10805 vr_value[i]->max);
10808 substitute_and_fold (op_with_constant_singleton_value_range, vrp_fold_stmt);
10810 if (warn_array_bounds && warn_array_bounds_p)
10811 check_all_array_refs ();
10814 /* evrp_dom_walker visits the basic blocks in the dominance order and set
10815 the Value Ranges (VR) for SSA_NAMEs in the scope. Use this VR to
10816 discover more VRs. */
10818 class evrp_dom_walker : public dom_walker
10820 public:
10821 evrp_dom_walker ()
10822 : dom_walker (CDI_DOMINATORS), stack (10)
10824 need_eh_cleanup = BITMAP_ALLOC (NULL);
10826 ~evrp_dom_walker ()
10828 BITMAP_FREE (need_eh_cleanup);
10830 virtual edge before_dom_children (basic_block);
10831 virtual void after_dom_children (basic_block);
10832 void push_value_range (tree var, value_range *vr);
10833 value_range *pop_value_range (tree var);
10834 value_range *try_find_new_range (tree, tree op, tree_code code, tree limit);
10836 /* Cond_stack holds the old VR. */
10837 auto_vec<std::pair <tree, value_range*> > stack;
10838 bitmap need_eh_cleanup;
10839 auto_vec<gimple *> stmts_to_fixup;
10840 auto_vec<gimple *> stmts_to_remove;
10843 /* Find new range for NAME such that (OP CODE LIMIT) is true. */
10845 value_range *
10846 evrp_dom_walker::try_find_new_range (tree name,
10847 tree op, tree_code code, tree limit)
10849 value_range vr = VR_INITIALIZER;
10850 value_range *old_vr = get_value_range (name);
10852 /* Discover VR when condition is true. */
10853 extract_range_for_var_from_comparison_expr (name, code, op,
10854 limit, &vr);
10855 /* If we found any usable VR, set the VR to ssa_name and create a
10856 PUSH old value in the stack with the old VR. */
10857 if (vr.type == VR_RANGE || vr.type == VR_ANTI_RANGE)
10859 if (old_vr->type == vr.type
10860 && vrp_operand_equal_p (old_vr->min, vr.min)
10861 && vrp_operand_equal_p (old_vr->max, vr.max))
10862 return NULL;
10863 value_range *new_vr = vrp_value_range_pool.allocate ();
10864 *new_vr = vr;
10865 return new_vr;
10867 return NULL;
10870 /* See if there is any new scope is entered with new VR and set that VR to
10871 ssa_name before visiting the statements in the scope. */
10873 edge
10874 evrp_dom_walker::before_dom_children (basic_block bb)
10876 tree op0 = NULL_TREE;
10877 edge_iterator ei;
10878 edge e;
10880 if (dump_file && (dump_flags & TDF_DETAILS))
10881 fprintf (dump_file, "Visiting BB%d\n", bb->index);
10883 stack.safe_push (std::make_pair (NULL_TREE, (value_range *)NULL));
10885 edge pred_e = NULL;
10886 FOR_EACH_EDGE (e, ei, bb->preds)
10888 /* Ignore simple backedges from this to allow recording conditions
10889 in loop headers. */
10890 if (dominated_by_p (CDI_DOMINATORS, e->src, e->dest))
10891 continue;
10892 if (! pred_e)
10893 pred_e = e;
10894 else
10896 pred_e = NULL;
10897 break;
10900 if (pred_e)
10902 gimple *stmt = last_stmt (pred_e->src);
10903 if (stmt
10904 && gimple_code (stmt) == GIMPLE_COND
10905 && (op0 = gimple_cond_lhs (stmt))
10906 && TREE_CODE (op0) == SSA_NAME
10907 && (INTEGRAL_TYPE_P (TREE_TYPE (gimple_cond_lhs (stmt)))
10908 || POINTER_TYPE_P (TREE_TYPE (gimple_cond_lhs (stmt)))))
10910 if (dump_file && (dump_flags & TDF_DETAILS))
10912 fprintf (dump_file, "Visiting controlling predicate ");
10913 print_gimple_stmt (dump_file, stmt, 0);
10915 /* Entering a new scope. Try to see if we can find a VR
10916 here. */
10917 tree op1 = gimple_cond_rhs (stmt);
10918 if (TREE_OVERFLOW_P (op1))
10919 op1 = drop_tree_overflow (op1);
10920 tree_code code = gimple_cond_code (stmt);
10922 auto_vec<assert_info, 8> asserts;
10923 register_edge_assert_for (op0, pred_e, code, op0, op1, asserts);
10924 if (TREE_CODE (op1) == SSA_NAME)
10925 register_edge_assert_for (op1, pred_e, code, op0, op1, asserts);
10927 auto_vec<std::pair<tree, value_range *>, 8> vrs;
10928 for (unsigned i = 0; i < asserts.length (); ++i)
10930 value_range *vr = try_find_new_range (asserts[i].name,
10931 asserts[i].expr,
10932 asserts[i].comp_code,
10933 asserts[i].val);
10934 if (vr)
10935 vrs.safe_push (std::make_pair (asserts[i].name, vr));
10937 /* Push updated ranges only after finding all of them to avoid
10938 ordering issues that can lead to worse ranges. */
10939 for (unsigned i = 0; i < vrs.length (); ++i)
10940 push_value_range (vrs[i].first, vrs[i].second);
10944 /* Visit PHI stmts and discover any new VRs possible. */
10945 bool has_unvisited_preds = false;
10946 FOR_EACH_EDGE (e, ei, bb->preds)
10947 if (e->flags & EDGE_EXECUTABLE
10948 && !(e->src->flags & BB_VISITED))
10950 has_unvisited_preds = true;
10951 break;
10954 for (gphi_iterator gpi = gsi_start_phis (bb);
10955 !gsi_end_p (gpi); gsi_next (&gpi))
10957 gphi *phi = gpi.phi ();
10958 tree lhs = PHI_RESULT (phi);
10959 if (virtual_operand_p (lhs))
10960 continue;
10961 value_range vr_result = VR_INITIALIZER;
10962 bool interesting = stmt_interesting_for_vrp (phi);
10963 if (interesting && dump_file && (dump_flags & TDF_DETAILS))
10965 fprintf (dump_file, "Visiting PHI node ");
10966 print_gimple_stmt (dump_file, phi, 0);
10968 if (!has_unvisited_preds
10969 && interesting)
10970 extract_range_from_phi_node (phi, &vr_result);
10971 else
10973 set_value_range_to_varying (&vr_result);
10974 /* When we have an unvisited executable predecessor we can't
10975 use PHI arg ranges which may be still UNDEFINED but have
10976 to use VARYING for them. But we can still resort to
10977 SCEV for loop header PHIs. */
10978 struct loop *l;
10979 if (interesting
10980 && (l = loop_containing_stmt (phi))
10981 && l->header == gimple_bb (phi))
10982 adjust_range_with_scev (&vr_result, l, phi, lhs);
10984 update_value_range (lhs, &vr_result);
10986 /* Mark PHIs whose lhs we fully propagate for removal. */
10987 tree val = op_with_constant_singleton_value_range (lhs);
10988 if (val && may_propagate_copy (lhs, val))
10990 stmts_to_remove.safe_push (phi);
10991 continue;
10994 /* Set the SSA with the value range. */
10995 if (INTEGRAL_TYPE_P (TREE_TYPE (lhs)))
10997 if ((vr_result.type == VR_RANGE
10998 || vr_result.type == VR_ANTI_RANGE)
10999 && (TREE_CODE (vr_result.min) == INTEGER_CST)
11000 && (TREE_CODE (vr_result.max) == INTEGER_CST))
11001 set_range_info (lhs,
11002 vr_result.type, vr_result.min, vr_result.max);
11004 else if (POINTER_TYPE_P (TREE_TYPE (lhs))
11005 && ((vr_result.type == VR_RANGE
11006 && range_includes_zero_p (vr_result.min,
11007 vr_result.max) == 0)
11008 || (vr_result.type == VR_ANTI_RANGE
11009 && range_includes_zero_p (vr_result.min,
11010 vr_result.max) == 1)))
11011 set_ptr_nonnull (lhs);
11014 edge taken_edge = NULL;
11016 /* Visit all other stmts and discover any new VRs possible. */
11017 for (gimple_stmt_iterator gsi = gsi_start_bb (bb);
11018 !gsi_end_p (gsi); gsi_next (&gsi))
11020 gimple *stmt = gsi_stmt (gsi);
11021 tree output = NULL_TREE;
11022 gimple *old_stmt = stmt;
11023 bool was_noreturn = (is_gimple_call (stmt)
11024 && gimple_call_noreturn_p (stmt));
11026 if (dump_file && (dump_flags & TDF_DETAILS))
11028 fprintf (dump_file, "Visiting stmt ");
11029 print_gimple_stmt (dump_file, stmt, 0);
11032 if (gcond *cond = dyn_cast <gcond *> (stmt))
11034 vrp_visit_cond_stmt (cond, &taken_edge);
11035 if (taken_edge)
11037 if (taken_edge->flags & EDGE_TRUE_VALUE)
11038 gimple_cond_make_true (cond);
11039 else if (taken_edge->flags & EDGE_FALSE_VALUE)
11040 gimple_cond_make_false (cond);
11041 else
11042 gcc_unreachable ();
11043 update_stmt (stmt);
11046 else if (stmt_interesting_for_vrp (stmt))
11048 edge taken_edge;
11049 value_range vr = VR_INITIALIZER;
11050 extract_range_from_stmt (stmt, &taken_edge, &output, &vr);
11051 if (output
11052 && (vr.type == VR_RANGE || vr.type == VR_ANTI_RANGE))
11054 update_value_range (output, &vr);
11055 vr = *get_value_range (output);
11057 /* Mark stmts whose output we fully propagate for removal. */
11058 tree val;
11059 if ((val = op_with_constant_singleton_value_range (output))
11060 && may_propagate_copy (output, val)
11061 && !stmt_could_throw_p (stmt)
11062 && !gimple_has_side_effects (stmt))
11064 stmts_to_remove.safe_push (stmt);
11065 continue;
11068 /* Set the SSA with the value range. */
11069 if (INTEGRAL_TYPE_P (TREE_TYPE (output)))
11071 if ((vr.type == VR_RANGE
11072 || vr.type == VR_ANTI_RANGE)
11073 && (TREE_CODE (vr.min) == INTEGER_CST)
11074 && (TREE_CODE (vr.max) == INTEGER_CST))
11075 set_range_info (output, vr.type, vr.min, vr.max);
11077 else if (POINTER_TYPE_P (TREE_TYPE (output))
11078 && ((vr.type == VR_RANGE
11079 && range_includes_zero_p (vr.min,
11080 vr.max) == 0)
11081 || (vr.type == VR_ANTI_RANGE
11082 && range_includes_zero_p (vr.min,
11083 vr.max) == 1)))
11084 set_ptr_nonnull (output);
11086 else
11087 set_defs_to_varying (stmt);
11089 else
11090 set_defs_to_varying (stmt);
11092 /* See if we can derive a range for any of STMT's operands. */
11093 tree op;
11094 ssa_op_iter i;
11095 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_USE)
11097 tree value;
11098 enum tree_code comp_code;
11100 /* If OP is used in such a way that we can infer a value
11101 range for it, and we don't find a previous assertion for
11102 it, create a new assertion location node for OP. */
11103 if (infer_value_range (stmt, op, &comp_code, &value))
11105 /* If we are able to infer a nonzero value range for OP,
11106 then walk backwards through the use-def chain to see if OP
11107 was set via a typecast.
11108 If so, then we can also infer a nonzero value range
11109 for the operand of the NOP_EXPR. */
11110 if (comp_code == NE_EXPR && integer_zerop (value))
11112 tree t = op;
11113 gimple *def_stmt = SSA_NAME_DEF_STMT (t);
11114 while (is_gimple_assign (def_stmt)
11115 && CONVERT_EXPR_CODE_P
11116 (gimple_assign_rhs_code (def_stmt))
11117 && TREE_CODE
11118 (gimple_assign_rhs1 (def_stmt)) == SSA_NAME
11119 && POINTER_TYPE_P
11120 (TREE_TYPE (gimple_assign_rhs1 (def_stmt))))
11122 t = gimple_assign_rhs1 (def_stmt);
11123 def_stmt = SSA_NAME_DEF_STMT (t);
11125 /* Add VR when (T COMP_CODE value) condition is
11126 true. */
11127 value_range *op_range
11128 = try_find_new_range (t, t, comp_code, value);
11129 if (op_range)
11130 push_value_range (t, op_range);
11133 /* Add VR when (OP COMP_CODE value) condition is true. */
11134 value_range *op_range = try_find_new_range (op, op,
11135 comp_code, value);
11136 if (op_range)
11137 push_value_range (op, op_range);
11141 /* Try folding stmts with the VR discovered. */
11142 bool did_replace
11143 = replace_uses_in (stmt, op_with_constant_singleton_value_range);
11144 if (fold_stmt (&gsi, follow_single_use_edges)
11145 || did_replace)
11147 stmt = gsi_stmt (gsi);
11148 update_stmt (stmt);
11149 did_replace = true;
11152 if (did_replace)
11154 /* If we cleaned up EH information from the statement,
11155 remove EH edges. */
11156 if (maybe_clean_or_replace_eh_stmt (old_stmt, stmt))
11157 bitmap_set_bit (need_eh_cleanup, bb->index);
11159 /* If we turned a not noreturn call into a noreturn one
11160 schedule it for fixup. */
11161 if (!was_noreturn
11162 && is_gimple_call (stmt)
11163 && gimple_call_noreturn_p (stmt))
11164 stmts_to_fixup.safe_push (stmt);
11166 if (gimple_assign_single_p (stmt))
11168 tree rhs = gimple_assign_rhs1 (stmt);
11169 if (TREE_CODE (rhs) == ADDR_EXPR)
11170 recompute_tree_invariant_for_addr_expr (rhs);
11175 /* Visit BB successor PHI nodes and replace PHI args. */
11176 FOR_EACH_EDGE (e, ei, bb->succs)
11178 for (gphi_iterator gpi = gsi_start_phis (e->dest);
11179 !gsi_end_p (gpi); gsi_next (&gpi))
11181 gphi *phi = gpi.phi ();
11182 use_operand_p use_p = PHI_ARG_DEF_PTR_FROM_EDGE (phi, e);
11183 tree arg = USE_FROM_PTR (use_p);
11184 if (TREE_CODE (arg) != SSA_NAME
11185 || virtual_operand_p (arg))
11186 continue;
11187 tree val = op_with_constant_singleton_value_range (arg);
11188 if (val && may_propagate_copy (arg, val))
11189 propagate_value (use_p, val);
11193 bb->flags |= BB_VISITED;
11195 return taken_edge;
11198 /* Restore/pop VRs valid only for BB when we leave BB. */
11200 void
11201 evrp_dom_walker::after_dom_children (basic_block bb ATTRIBUTE_UNUSED)
11203 gcc_checking_assert (!stack.is_empty ());
11204 while (stack.last ().first != NULL_TREE)
11205 pop_value_range (stack.last ().first);
11206 stack.pop ();
11209 /* Push the Value Range of VAR to the stack and update it with new VR. */
11211 void
11212 evrp_dom_walker::push_value_range (tree var, value_range *vr)
11214 if (SSA_NAME_VERSION (var) >= num_vr_values)
11215 return;
11216 if (dump_file && (dump_flags & TDF_DETAILS))
11218 fprintf (dump_file, "pushing new range for ");
11219 print_generic_expr (dump_file, var);
11220 fprintf (dump_file, ": ");
11221 dump_value_range (dump_file, vr);
11222 fprintf (dump_file, "\n");
11224 stack.safe_push (std::make_pair (var, get_value_range (var)));
11225 vr_value[SSA_NAME_VERSION (var)] = vr;
11228 /* Pop the Value Range from the vrp_stack and update VAR with it. */
11230 value_range *
11231 evrp_dom_walker::pop_value_range (tree var)
11233 value_range *vr = stack.last ().second;
11234 gcc_checking_assert (var == stack.last ().first);
11235 if (dump_file && (dump_flags & TDF_DETAILS))
11237 fprintf (dump_file, "popping range for ");
11238 print_generic_expr (dump_file, var);
11239 fprintf (dump_file, ", restoring ");
11240 dump_value_range (dump_file, vr);
11241 fprintf (dump_file, "\n");
11243 vr_value[SSA_NAME_VERSION (var)] = vr;
11244 stack.pop ();
11245 return vr;
11249 /* Main entry point for the early vrp pass which is a simplified non-iterative
11250 version of vrp where basic blocks are visited in dominance order. Value
11251 ranges discovered in early vrp will also be used by ipa-vrp. */
11253 static unsigned int
11254 execute_early_vrp ()
11256 edge e;
11257 edge_iterator ei;
11258 basic_block bb;
11260 loop_optimizer_init (LOOPS_NORMAL | LOOPS_HAVE_RECORDED_EXITS);
11261 rewrite_into_loop_closed_ssa (NULL, TODO_update_ssa);
11262 scev_initialize ();
11263 calculate_dominance_info (CDI_DOMINATORS);
11264 FOR_EACH_BB_FN (bb, cfun)
11266 bb->flags &= ~BB_VISITED;
11267 FOR_EACH_EDGE (e, ei, bb->preds)
11268 e->flags |= EDGE_EXECUTABLE;
11270 vrp_initialize_lattice ();
11272 /* Walk stmts in dominance order and propagate VRP. */
11273 evrp_dom_walker walker;
11274 walker.walk (ENTRY_BLOCK_PTR_FOR_FN (cfun));
11276 if (dump_file)
11278 fprintf (dump_file, "\nValue ranges after Early VRP:\n\n");
11279 dump_all_value_ranges (dump_file);
11280 fprintf (dump_file, "\n");
11283 /* Remove stmts in reverse order to make debug stmt creation possible. */
11284 while (! walker.stmts_to_remove.is_empty ())
11286 gimple *stmt = walker.stmts_to_remove.pop ();
11287 if (dump_file && dump_flags & TDF_DETAILS)
11289 fprintf (dump_file, "Removing dead stmt ");
11290 print_gimple_stmt (dump_file, stmt, 0);
11291 fprintf (dump_file, "\n");
11293 gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
11294 if (gimple_code (stmt) == GIMPLE_PHI)
11295 remove_phi_node (&gsi, true);
11296 else
11298 unlink_stmt_vdef (stmt);
11299 gsi_remove (&gsi, true);
11300 release_defs (stmt);
11304 if (!bitmap_empty_p (walker.need_eh_cleanup))
11305 gimple_purge_all_dead_eh_edges (walker.need_eh_cleanup);
11307 /* Fixup stmts that became noreturn calls. This may require splitting
11308 blocks and thus isn't possible during the dominator walk. Do this
11309 in reverse order so we don't inadvertedly remove a stmt we want to
11310 fixup by visiting a dominating now noreturn call first. */
11311 while (!walker.stmts_to_fixup.is_empty ())
11313 gimple *stmt = walker.stmts_to_fixup.pop ();
11314 fixup_noreturn_call (stmt);
11317 vrp_free_lattice ();
11318 scev_finalize ();
11319 loop_optimizer_finalize ();
11320 return 0;
11324 /* Main entry point to VRP (Value Range Propagation). This pass is
11325 loosely based on J. R. C. Patterson, ``Accurate Static Branch
11326 Prediction by Value Range Propagation,'' in SIGPLAN Conference on
11327 Programming Language Design and Implementation, pp. 67-78, 1995.
11328 Also available at http://citeseer.ist.psu.edu/patterson95accurate.html
11330 This is essentially an SSA-CCP pass modified to deal with ranges
11331 instead of constants.
11333 While propagating ranges, we may find that two or more SSA name
11334 have equivalent, though distinct ranges. For instance,
11336 1 x_9 = p_3->a;
11337 2 p_4 = ASSERT_EXPR <p_3, p_3 != 0>
11338 3 if (p_4 == q_2)
11339 4 p_5 = ASSERT_EXPR <p_4, p_4 == q_2>;
11340 5 endif
11341 6 if (q_2)
11343 In the code above, pointer p_5 has range [q_2, q_2], but from the
11344 code we can also determine that p_5 cannot be NULL and, if q_2 had
11345 a non-varying range, p_5's range should also be compatible with it.
11347 These equivalences are created by two expressions: ASSERT_EXPR and
11348 copy operations. Since p_5 is an assertion on p_4, and p_4 was the
11349 result of another assertion, then we can use the fact that p_5 and
11350 p_4 are equivalent when evaluating p_5's range.
11352 Together with value ranges, we also propagate these equivalences
11353 between names so that we can take advantage of information from
11354 multiple ranges when doing final replacement. Note that this
11355 equivalency relation is transitive but not symmetric.
11357 In the example above, p_5 is equivalent to p_4, q_2 and p_3, but we
11358 cannot assert that q_2 is equivalent to p_5 because q_2 may be used
11359 in contexts where that assertion does not hold (e.g., in line 6).
11361 TODO, the main difference between this pass and Patterson's is that
11362 we do not propagate edge probabilities. We only compute whether
11363 edges can be taken or not. That is, instead of having a spectrum
11364 of jump probabilities between 0 and 1, we only deal with 0, 1 and
11365 DON'T KNOW. In the future, it may be worthwhile to propagate
11366 probabilities to aid branch prediction. */
11368 static unsigned int
11369 execute_vrp (bool warn_array_bounds_p)
11371 int i;
11372 edge e;
11373 switch_update *su;
11375 loop_optimizer_init (LOOPS_NORMAL | LOOPS_HAVE_RECORDED_EXITS);
11376 rewrite_into_loop_closed_ssa (NULL, TODO_update_ssa);
11377 scev_initialize ();
11379 /* ??? This ends up using stale EDGE_DFS_BACK for liveness computation.
11380 Inserting assertions may split edges which will invalidate
11381 EDGE_DFS_BACK. */
11382 insert_range_assertions ();
11384 to_remove_edges.create (10);
11385 to_update_switch_stmts.create (5);
11386 threadedge_initialize_values ();
11388 /* For visiting PHI nodes we need EDGE_DFS_BACK computed. */
11389 mark_dfs_back_edges ();
11391 vrp_initialize_lattice ();
11392 vrp_initialize ();
11393 ssa_propagate (vrp_visit_stmt, vrp_visit_phi_node);
11394 vrp_finalize (warn_array_bounds_p);
11396 /* We must identify jump threading opportunities before we release
11397 the datastructures built by VRP. */
11398 identify_jump_threads ();
11400 /* A comparison of an SSA_NAME against a constant where the SSA_NAME
11401 was set by a type conversion can often be rewritten to use the
11402 RHS of the type conversion.
11404 However, doing so inhibits jump threading through the comparison.
11405 So that transformation is not performed until after jump threading
11406 is complete. */
11407 basic_block bb;
11408 FOR_EACH_BB_FN (bb, cfun)
11410 gimple *last = last_stmt (bb);
11411 if (last && gimple_code (last) == GIMPLE_COND)
11412 simplify_cond_using_ranges_2 (as_a <gcond *> (last));
11415 vrp_free_lattice ();
11417 free_numbers_of_iterations_estimates (cfun);
11419 /* ASSERT_EXPRs must be removed before finalizing jump threads
11420 as finalizing jump threads calls the CFG cleanup code which
11421 does not properly handle ASSERT_EXPRs. */
11422 remove_range_assertions ();
11424 /* If we exposed any new variables, go ahead and put them into
11425 SSA form now, before we handle jump threading. This simplifies
11426 interactions between rewriting of _DECL nodes into SSA form
11427 and rewriting SSA_NAME nodes into SSA form after block
11428 duplication and CFG manipulation. */
11429 update_ssa (TODO_update_ssa);
11431 /* We identified all the jump threading opportunities earlier, but could
11432 not transform the CFG at that time. This routine transforms the
11433 CFG and arranges for the dominator tree to be rebuilt if necessary.
11435 Note the SSA graph update will occur during the normal TODO
11436 processing by the pass manager. */
11437 thread_through_all_blocks (false);
11439 /* Remove dead edges from SWITCH_EXPR optimization. This leaves the
11440 CFG in a broken state and requires a cfg_cleanup run. */
11441 FOR_EACH_VEC_ELT (to_remove_edges, i, e)
11442 remove_edge (e);
11443 /* Update SWITCH_EXPR case label vector. */
11444 FOR_EACH_VEC_ELT (to_update_switch_stmts, i, su)
11446 size_t j;
11447 size_t n = TREE_VEC_LENGTH (su->vec);
11448 tree label;
11449 gimple_switch_set_num_labels (su->stmt, n);
11450 for (j = 0; j < n; j++)
11451 gimple_switch_set_label (su->stmt, j, TREE_VEC_ELT (su->vec, j));
11452 /* As we may have replaced the default label with a regular one
11453 make sure to make it a real default label again. This ensures
11454 optimal expansion. */
11455 label = gimple_switch_label (su->stmt, 0);
11456 CASE_LOW (label) = NULL_TREE;
11457 CASE_HIGH (label) = NULL_TREE;
11460 if (to_remove_edges.length () > 0)
11462 free_dominance_info (CDI_DOMINATORS);
11463 loops_state_set (LOOPS_NEED_FIXUP);
11466 to_remove_edges.release ();
11467 to_update_switch_stmts.release ();
11468 threadedge_finalize_values ();
11470 scev_finalize ();
11471 loop_optimizer_finalize ();
11472 return 0;
11475 namespace {
11477 const pass_data pass_data_vrp =
11479 GIMPLE_PASS, /* type */
11480 "vrp", /* name */
11481 OPTGROUP_NONE, /* optinfo_flags */
11482 TV_TREE_VRP, /* tv_id */
11483 PROP_ssa, /* properties_required */
11484 0, /* properties_provided */
11485 0, /* properties_destroyed */
11486 0, /* todo_flags_start */
11487 ( TODO_cleanup_cfg | TODO_update_ssa ), /* todo_flags_finish */
11490 class pass_vrp : public gimple_opt_pass
11492 public:
11493 pass_vrp (gcc::context *ctxt)
11494 : gimple_opt_pass (pass_data_vrp, ctxt), warn_array_bounds_p (false)
11497 /* opt_pass methods: */
11498 opt_pass * clone () { return new pass_vrp (m_ctxt); }
11499 void set_pass_param (unsigned int n, bool param)
11501 gcc_assert (n == 0);
11502 warn_array_bounds_p = param;
11504 virtual bool gate (function *) { return flag_tree_vrp != 0; }
11505 virtual unsigned int execute (function *)
11506 { return execute_vrp (warn_array_bounds_p); }
11508 private:
11509 bool warn_array_bounds_p;
11510 }; // class pass_vrp
11512 } // anon namespace
11514 gimple_opt_pass *
11515 make_pass_vrp (gcc::context *ctxt)
11517 return new pass_vrp (ctxt);
11520 namespace {
11522 const pass_data pass_data_early_vrp =
11524 GIMPLE_PASS, /* type */
11525 "evrp", /* name */
11526 OPTGROUP_NONE, /* optinfo_flags */
11527 TV_TREE_EARLY_VRP, /* tv_id */
11528 PROP_ssa, /* properties_required */
11529 0, /* properties_provided */
11530 0, /* properties_destroyed */
11531 0, /* todo_flags_start */
11532 ( TODO_cleanup_cfg | TODO_update_ssa | TODO_verify_all ),
11535 class pass_early_vrp : public gimple_opt_pass
11537 public:
11538 pass_early_vrp (gcc::context *ctxt)
11539 : gimple_opt_pass (pass_data_early_vrp, ctxt)
11542 /* opt_pass methods: */
11543 opt_pass * clone () { return new pass_early_vrp (m_ctxt); }
11544 virtual bool gate (function *)
11546 return flag_tree_vrp != 0;
11548 virtual unsigned int execute (function *)
11549 { return execute_early_vrp (); }
11551 }; // class pass_vrp
11552 } // anon namespace
11554 gimple_opt_pass *
11555 make_pass_early_vrp (gcc::context *ctxt)
11557 return new pass_early_vrp (ctxt);