re PR lto/45375 ([meta-bug] Issues with building Mozilla (i.e. Firefox) with LTO)
[official-gcc.git] / gcc / tree-vrp.c
blobacf6b20fd4fac2bb516a2cfbc61dba300034aa9d
1 /* Support routines for Value Range Propagation (VRP).
2 Copyright (C) 2005-2015 Free Software Foundation, Inc.
3 Contributed by Diego Novillo <dnovillo@redhat.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "flags.h"
26 #include "hash-set.h"
27 #include "machmode.h"
28 #include "vec.h"
29 #include "double-int.h"
30 #include "input.h"
31 #include "alias.h"
32 #include "symtab.h"
33 #include "wide-int.h"
34 #include "inchash.h"
35 #include "tree.h"
36 #include "fold-const.h"
37 #include "stor-layout.h"
38 #include "calls.h"
39 #include "predict.h"
40 #include "hard-reg-set.h"
41 #include "function.h"
42 #include "dominance.h"
43 #include "cfg.h"
44 #include "cfganal.h"
45 #include "basic-block.h"
46 #include "tree-ssa-alias.h"
47 #include "internal-fn.h"
48 #include "gimple-fold.h"
49 #include "tree-eh.h"
50 #include "gimple-expr.h"
51 #include "is-a.h"
52 #include "gimple.h"
53 #include "gimple-iterator.h"
54 #include "gimple-walk.h"
55 #include "gimple-ssa.h"
56 #include "tree-cfg.h"
57 #include "tree-phinodes.h"
58 #include "ssa-iterators.h"
59 #include "stringpool.h"
60 #include "tree-ssanames.h"
61 #include "tree-ssa-loop-manip.h"
62 #include "tree-ssa-loop-niter.h"
63 #include "tree-ssa-loop.h"
64 #include "tree-into-ssa.h"
65 #include "tree-ssa.h"
66 #include "tree-pass.h"
67 #include "tree-dump.h"
68 #include "gimple-pretty-print.h"
69 #include "diagnostic-core.h"
70 #include "intl.h"
71 #include "cfgloop.h"
72 #include "tree-scalar-evolution.h"
73 #include "tree-ssa-propagate.h"
74 #include "tree-chrec.h"
75 #include "tree-ssa-threadupdate.h"
76 #include "hashtab.h"
77 #include "rtl.h"
78 #include "statistics.h"
79 #include "real.h"
80 #include "fixed-value.h"
81 #include "insn-config.h"
82 #include "expmed.h"
83 #include "dojump.h"
84 #include "explow.h"
85 #include "emit-rtl.h"
86 #include "varasm.h"
87 #include "stmt.h"
88 #include "expr.h"
89 #include "insn-codes.h"
90 #include "optabs.h"
91 #include "tree-ssa-threadedge.h"
95 /* Range of values that can be associated with an SSA_NAME after VRP
96 has executed. */
97 struct value_range_d
99 /* Lattice value represented by this range. */
100 enum value_range_type type;
102 /* Minimum and maximum values represented by this range. These
103 values should be interpreted as follows:
105 - If TYPE is VR_UNDEFINED or VR_VARYING then MIN and MAX must
106 be NULL.
108 - If TYPE == VR_RANGE then MIN holds the minimum value and
109 MAX holds the maximum value of the range [MIN, MAX].
111 - If TYPE == ANTI_RANGE the variable is known to NOT
112 take any values in the range [MIN, MAX]. */
113 tree min;
114 tree max;
116 /* Set of SSA names whose value ranges are equivalent to this one.
117 This set is only valid when TYPE is VR_RANGE or VR_ANTI_RANGE. */
118 bitmap equiv;
121 typedef struct value_range_d value_range_t;
123 #define VR_INITIALIZER { VR_UNDEFINED, NULL_TREE, NULL_TREE, NULL }
125 /* Set of SSA names found live during the RPO traversal of the function
126 for still active basic-blocks. */
127 static sbitmap *live;
129 /* Return true if the SSA name NAME is live on the edge E. */
131 static bool
132 live_on_edge (edge e, tree name)
134 return (live[e->dest->index]
135 && bitmap_bit_p (live[e->dest->index], SSA_NAME_VERSION (name)));
138 /* Local functions. */
139 static int compare_values (tree val1, tree val2);
140 static int compare_values_warnv (tree val1, tree val2, bool *);
141 static void vrp_meet (value_range_t *, value_range_t *);
142 static void vrp_intersect_ranges (value_range_t *, value_range_t *);
143 static tree vrp_evaluate_conditional_warnv_with_ops (enum tree_code,
144 tree, tree, bool, bool *,
145 bool *);
147 /* Location information for ASSERT_EXPRs. Each instance of this
148 structure describes an ASSERT_EXPR for an SSA name. Since a single
149 SSA name may have more than one assertion associated with it, these
150 locations are kept in a linked list attached to the corresponding
151 SSA name. */
152 struct assert_locus_d
154 /* Basic block where the assertion would be inserted. */
155 basic_block bb;
157 /* Some assertions need to be inserted on an edge (e.g., assertions
158 generated by COND_EXPRs). In those cases, BB will be NULL. */
159 edge e;
161 /* Pointer to the statement that generated this assertion. */
162 gimple_stmt_iterator si;
164 /* Predicate code for the ASSERT_EXPR. Must be COMPARISON_CLASS_P. */
165 enum tree_code comp_code;
167 /* Value being compared against. */
168 tree val;
170 /* Expression to compare. */
171 tree expr;
173 /* Next node in the linked list. */
174 struct assert_locus_d *next;
177 typedef struct assert_locus_d *assert_locus_t;
179 /* If bit I is present, it means that SSA name N_i has a list of
180 assertions that should be inserted in the IL. */
181 static bitmap need_assert_for;
183 /* Array of locations lists where to insert assertions. ASSERTS_FOR[I]
184 holds a list of ASSERT_LOCUS_T nodes that describe where
185 ASSERT_EXPRs for SSA name N_I should be inserted. */
186 static assert_locus_t *asserts_for;
188 /* Value range array. After propagation, VR_VALUE[I] holds the range
189 of values that SSA name N_I may take. */
190 static unsigned num_vr_values;
191 static value_range_t **vr_value;
192 static bool values_propagated;
194 /* For a PHI node which sets SSA name N_I, VR_COUNTS[I] holds the
195 number of executable edges we saw the last time we visited the
196 node. */
197 static int *vr_phi_edge_counts;
199 typedef struct {
200 gswitch *stmt;
201 tree vec;
202 } switch_update;
204 static vec<edge> to_remove_edges;
205 static vec<switch_update> to_update_switch_stmts;
208 /* Return the maximum value for TYPE. */
210 static inline tree
211 vrp_val_max (const_tree type)
213 if (!INTEGRAL_TYPE_P (type))
214 return NULL_TREE;
216 return TYPE_MAX_VALUE (type);
219 /* Return the minimum value for TYPE. */
221 static inline tree
222 vrp_val_min (const_tree type)
224 if (!INTEGRAL_TYPE_P (type))
225 return NULL_TREE;
227 return TYPE_MIN_VALUE (type);
230 /* Return whether VAL is equal to the maximum value of its type. This
231 will be true for a positive overflow infinity. We can't do a
232 simple equality comparison with TYPE_MAX_VALUE because C typedefs
233 and Ada subtypes can produce types whose TYPE_MAX_VALUE is not ==
234 to the integer constant with the same value in the type. */
236 static inline bool
237 vrp_val_is_max (const_tree val)
239 tree type_max = vrp_val_max (TREE_TYPE (val));
240 return (val == type_max
241 || (type_max != NULL_TREE
242 && operand_equal_p (val, type_max, 0)));
245 /* Return whether VAL is equal to the minimum value of its type. This
246 will be true for a negative overflow infinity. */
248 static inline bool
249 vrp_val_is_min (const_tree val)
251 tree type_min = vrp_val_min (TREE_TYPE (val));
252 return (val == type_min
253 || (type_min != NULL_TREE
254 && operand_equal_p (val, type_min, 0)));
258 /* Return whether TYPE should use an overflow infinity distinct from
259 TYPE_{MIN,MAX}_VALUE. We use an overflow infinity value to
260 represent a signed overflow during VRP computations. An infinity
261 is distinct from a half-range, which will go from some number to
262 TYPE_{MIN,MAX}_VALUE. */
264 static inline bool
265 needs_overflow_infinity (const_tree type)
267 return INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_WRAPS (type);
270 /* Return whether TYPE can support our overflow infinity
271 representation: we use the TREE_OVERFLOW flag, which only exists
272 for constants. If TYPE doesn't support this, we don't optimize
273 cases which would require signed overflow--we drop them to
274 VARYING. */
276 static inline bool
277 supports_overflow_infinity (const_tree type)
279 tree min = vrp_val_min (type), max = vrp_val_max (type);
280 #ifdef ENABLE_CHECKING
281 gcc_assert (needs_overflow_infinity (type));
282 #endif
283 return (min != NULL_TREE
284 && CONSTANT_CLASS_P (min)
285 && max != NULL_TREE
286 && CONSTANT_CLASS_P (max));
289 /* VAL is the maximum or minimum value of a type. Return a
290 corresponding overflow infinity. */
292 static inline tree
293 make_overflow_infinity (tree val)
295 gcc_checking_assert (val != NULL_TREE && CONSTANT_CLASS_P (val));
296 val = copy_node (val);
297 TREE_OVERFLOW (val) = 1;
298 return val;
301 /* Return a negative overflow infinity for TYPE. */
303 static inline tree
304 negative_overflow_infinity (tree type)
306 gcc_checking_assert (supports_overflow_infinity (type));
307 return make_overflow_infinity (vrp_val_min (type));
310 /* Return a positive overflow infinity for TYPE. */
312 static inline tree
313 positive_overflow_infinity (tree type)
315 gcc_checking_assert (supports_overflow_infinity (type));
316 return make_overflow_infinity (vrp_val_max (type));
319 /* Return whether VAL is a negative overflow infinity. */
321 static inline bool
322 is_negative_overflow_infinity (const_tree val)
324 return (TREE_OVERFLOW_P (val)
325 && needs_overflow_infinity (TREE_TYPE (val))
326 && vrp_val_is_min (val));
329 /* Return whether VAL is a positive overflow infinity. */
331 static inline bool
332 is_positive_overflow_infinity (const_tree val)
334 return (TREE_OVERFLOW_P (val)
335 && needs_overflow_infinity (TREE_TYPE (val))
336 && vrp_val_is_max (val));
339 /* Return whether VAL is a positive or negative overflow infinity. */
341 static inline bool
342 is_overflow_infinity (const_tree val)
344 return (TREE_OVERFLOW_P (val)
345 && needs_overflow_infinity (TREE_TYPE (val))
346 && (vrp_val_is_min (val) || vrp_val_is_max (val)));
349 /* Return whether STMT has a constant rhs that is_overflow_infinity. */
351 static inline bool
352 stmt_overflow_infinity (gimple stmt)
354 if (is_gimple_assign (stmt)
355 && get_gimple_rhs_class (gimple_assign_rhs_code (stmt)) ==
356 GIMPLE_SINGLE_RHS)
357 return is_overflow_infinity (gimple_assign_rhs1 (stmt));
358 return false;
361 /* If VAL is now an overflow infinity, return VAL. Otherwise, return
362 the same value with TREE_OVERFLOW clear. This can be used to avoid
363 confusing a regular value with an overflow value. */
365 static inline tree
366 avoid_overflow_infinity (tree val)
368 if (!is_overflow_infinity (val))
369 return val;
371 if (vrp_val_is_max (val))
372 return vrp_val_max (TREE_TYPE (val));
373 else
375 gcc_checking_assert (vrp_val_is_min (val));
376 return vrp_val_min (TREE_TYPE (val));
381 /* Return true if ARG is marked with the nonnull attribute in the
382 current function signature. */
384 static bool
385 nonnull_arg_p (const_tree arg)
387 tree t, attrs, fntype;
388 unsigned HOST_WIDE_INT arg_num;
390 gcc_assert (TREE_CODE (arg) == PARM_DECL && POINTER_TYPE_P (TREE_TYPE (arg)));
392 /* The static chain decl is always non null. */
393 if (arg == cfun->static_chain_decl)
394 return true;
396 fntype = TREE_TYPE (current_function_decl);
397 for (attrs = TYPE_ATTRIBUTES (fntype); attrs; attrs = TREE_CHAIN (attrs))
399 attrs = lookup_attribute ("nonnull", attrs);
401 /* If "nonnull" wasn't specified, we know nothing about the argument. */
402 if (attrs == NULL_TREE)
403 return false;
405 /* If "nonnull" applies to all the arguments, then ARG is non-null. */
406 if (TREE_VALUE (attrs) == NULL_TREE)
407 return true;
409 /* Get the position number for ARG in the function signature. */
410 for (arg_num = 1, t = DECL_ARGUMENTS (current_function_decl);
412 t = DECL_CHAIN (t), arg_num++)
414 if (t == arg)
415 break;
418 gcc_assert (t == arg);
420 /* Now see if ARG_NUM is mentioned in the nonnull list. */
421 for (t = TREE_VALUE (attrs); t; t = TREE_CHAIN (t))
423 if (compare_tree_int (TREE_VALUE (t), arg_num) == 0)
424 return true;
428 return false;
432 /* Set value range VR to VR_UNDEFINED. */
434 static inline void
435 set_value_range_to_undefined (value_range_t *vr)
437 vr->type = VR_UNDEFINED;
438 vr->min = vr->max = NULL_TREE;
439 if (vr->equiv)
440 bitmap_clear (vr->equiv);
444 /* Set value range VR to VR_VARYING. */
446 static inline void
447 set_value_range_to_varying (value_range_t *vr)
449 vr->type = VR_VARYING;
450 vr->min = vr->max = NULL_TREE;
451 if (vr->equiv)
452 bitmap_clear (vr->equiv);
456 /* Set value range VR to {T, MIN, MAX, EQUIV}. */
458 static void
459 set_value_range (value_range_t *vr, enum value_range_type t, tree min,
460 tree max, bitmap equiv)
462 #if defined ENABLE_CHECKING
463 /* Check the validity of the range. */
464 if (t == VR_RANGE || t == VR_ANTI_RANGE)
466 int cmp;
468 gcc_assert (min && max);
470 gcc_assert ((!TREE_OVERFLOW_P (min) || is_overflow_infinity (min))
471 && (!TREE_OVERFLOW_P (max) || is_overflow_infinity (max)));
473 if (INTEGRAL_TYPE_P (TREE_TYPE (min)) && t == VR_ANTI_RANGE)
474 gcc_assert (!vrp_val_is_min (min) || !vrp_val_is_max (max));
476 cmp = compare_values (min, max);
477 gcc_assert (cmp == 0 || cmp == -1 || cmp == -2);
479 if (needs_overflow_infinity (TREE_TYPE (min)))
480 gcc_assert (!is_overflow_infinity (min)
481 || !is_overflow_infinity (max));
484 if (t == VR_UNDEFINED || t == VR_VARYING)
485 gcc_assert (min == NULL_TREE && max == NULL_TREE);
487 if (t == VR_UNDEFINED || t == VR_VARYING)
488 gcc_assert (equiv == NULL || bitmap_empty_p (equiv));
489 #endif
491 vr->type = t;
492 vr->min = min;
493 vr->max = max;
495 /* Since updating the equivalence set involves deep copying the
496 bitmaps, only do it if absolutely necessary. */
497 if (vr->equiv == NULL
498 && equiv != NULL)
499 vr->equiv = BITMAP_ALLOC (NULL);
501 if (equiv != vr->equiv)
503 if (equiv && !bitmap_empty_p (equiv))
504 bitmap_copy (vr->equiv, equiv);
505 else
506 bitmap_clear (vr->equiv);
511 /* Set value range VR to the canonical form of {T, MIN, MAX, EQUIV}.
512 This means adjusting T, MIN and MAX representing the case of a
513 wrapping range with MAX < MIN covering [MIN, type_max] U [type_min, MAX]
514 as anti-rage ~[MAX+1, MIN-1]. Likewise for wrapping anti-ranges.
515 In corner cases where MAX+1 or MIN-1 wraps this will fall back
516 to varying.
517 This routine exists to ease canonicalization in the case where we
518 extract ranges from var + CST op limit. */
520 static void
521 set_and_canonicalize_value_range (value_range_t *vr, enum value_range_type t,
522 tree min, tree max, bitmap equiv)
524 /* Use the canonical setters for VR_UNDEFINED and VR_VARYING. */
525 if (t == VR_UNDEFINED)
527 set_value_range_to_undefined (vr);
528 return;
530 else if (t == VR_VARYING)
532 set_value_range_to_varying (vr);
533 return;
536 /* Nothing to canonicalize for symbolic ranges. */
537 if (TREE_CODE (min) != INTEGER_CST
538 || TREE_CODE (max) != INTEGER_CST)
540 set_value_range (vr, t, min, max, equiv);
541 return;
544 /* Wrong order for min and max, to swap them and the VR type we need
545 to adjust them. */
546 if (tree_int_cst_lt (max, min))
548 tree one, tmp;
550 /* For one bit precision if max < min, then the swapped
551 range covers all values, so for VR_RANGE it is varying and
552 for VR_ANTI_RANGE empty range, so drop to varying as well. */
553 if (TYPE_PRECISION (TREE_TYPE (min)) == 1)
555 set_value_range_to_varying (vr);
556 return;
559 one = build_int_cst (TREE_TYPE (min), 1);
560 tmp = int_const_binop (PLUS_EXPR, max, one);
561 max = int_const_binop (MINUS_EXPR, min, one);
562 min = tmp;
564 /* There's one corner case, if we had [C+1, C] before we now have
565 that again. But this represents an empty value range, so drop
566 to varying in this case. */
567 if (tree_int_cst_lt (max, min))
569 set_value_range_to_varying (vr);
570 return;
573 t = t == VR_RANGE ? VR_ANTI_RANGE : VR_RANGE;
576 /* Anti-ranges that can be represented as ranges should be so. */
577 if (t == VR_ANTI_RANGE)
579 bool is_min = vrp_val_is_min (min);
580 bool is_max = vrp_val_is_max (max);
582 if (is_min && is_max)
584 /* We cannot deal with empty ranges, drop to varying.
585 ??? This could be VR_UNDEFINED instead. */
586 set_value_range_to_varying (vr);
587 return;
589 else if (TYPE_PRECISION (TREE_TYPE (min)) == 1
590 && (is_min || is_max))
592 /* Non-empty boolean ranges can always be represented
593 as a singleton range. */
594 if (is_min)
595 min = max = vrp_val_max (TREE_TYPE (min));
596 else
597 min = max = vrp_val_min (TREE_TYPE (min));
598 t = VR_RANGE;
600 else if (is_min
601 /* As a special exception preserve non-null ranges. */
602 && !(TYPE_UNSIGNED (TREE_TYPE (min))
603 && integer_zerop (max)))
605 tree one = build_int_cst (TREE_TYPE (max), 1);
606 min = int_const_binop (PLUS_EXPR, max, one);
607 max = vrp_val_max (TREE_TYPE (max));
608 t = VR_RANGE;
610 else if (is_max)
612 tree one = build_int_cst (TREE_TYPE (min), 1);
613 max = int_const_binop (MINUS_EXPR, min, one);
614 min = vrp_val_min (TREE_TYPE (min));
615 t = VR_RANGE;
619 /* Drop [-INF(OVF), +INF(OVF)] to varying. */
620 if (needs_overflow_infinity (TREE_TYPE (min))
621 && is_overflow_infinity (min)
622 && is_overflow_infinity (max))
624 set_value_range_to_varying (vr);
625 return;
628 set_value_range (vr, t, min, max, equiv);
631 /* Copy value range FROM into value range TO. */
633 static inline void
634 copy_value_range (value_range_t *to, value_range_t *from)
636 set_value_range (to, from->type, from->min, from->max, from->equiv);
639 /* Set value range VR to a single value. This function is only called
640 with values we get from statements, and exists to clear the
641 TREE_OVERFLOW flag so that we don't think we have an overflow
642 infinity when we shouldn't. */
644 static inline void
645 set_value_range_to_value (value_range_t *vr, tree val, bitmap equiv)
647 gcc_assert (is_gimple_min_invariant (val));
648 if (TREE_OVERFLOW_P (val))
649 val = drop_tree_overflow (val);
650 set_value_range (vr, VR_RANGE, val, val, equiv);
653 /* Set value range VR to a non-negative range of type TYPE.
654 OVERFLOW_INFINITY indicates whether to use an overflow infinity
655 rather than TYPE_MAX_VALUE; this should be true if we determine
656 that the range is nonnegative based on the assumption that signed
657 overflow does not occur. */
659 static inline void
660 set_value_range_to_nonnegative (value_range_t *vr, tree type,
661 bool overflow_infinity)
663 tree zero;
665 if (overflow_infinity && !supports_overflow_infinity (type))
667 set_value_range_to_varying (vr);
668 return;
671 zero = build_int_cst (type, 0);
672 set_value_range (vr, VR_RANGE, zero,
673 (overflow_infinity
674 ? positive_overflow_infinity (type)
675 : TYPE_MAX_VALUE (type)),
676 vr->equiv);
679 /* Set value range VR to a non-NULL range of type TYPE. */
681 static inline void
682 set_value_range_to_nonnull (value_range_t *vr, tree type)
684 tree zero = build_int_cst (type, 0);
685 set_value_range (vr, VR_ANTI_RANGE, zero, zero, vr->equiv);
689 /* Set value range VR to a NULL range of type TYPE. */
691 static inline void
692 set_value_range_to_null (value_range_t *vr, tree type)
694 set_value_range_to_value (vr, build_int_cst (type, 0), vr->equiv);
698 /* Set value range VR to a range of a truthvalue of type TYPE. */
700 static inline void
701 set_value_range_to_truthvalue (value_range_t *vr, tree type)
703 if (TYPE_PRECISION (type) == 1)
704 set_value_range_to_varying (vr);
705 else
706 set_value_range (vr, VR_RANGE,
707 build_int_cst (type, 0), build_int_cst (type, 1),
708 vr->equiv);
712 /* If abs (min) < abs (max), set VR to [-max, max], if
713 abs (min) >= abs (max), set VR to [-min, min]. */
715 static void
716 abs_extent_range (value_range_t *vr, tree min, tree max)
718 int cmp;
720 gcc_assert (TREE_CODE (min) == INTEGER_CST);
721 gcc_assert (TREE_CODE (max) == INTEGER_CST);
722 gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (min)));
723 gcc_assert (!TYPE_UNSIGNED (TREE_TYPE (min)));
724 min = fold_unary (ABS_EXPR, TREE_TYPE (min), min);
725 max = fold_unary (ABS_EXPR, TREE_TYPE (max), max);
726 if (TREE_OVERFLOW (min) || TREE_OVERFLOW (max))
728 set_value_range_to_varying (vr);
729 return;
731 cmp = compare_values (min, max);
732 if (cmp == -1)
733 min = fold_unary (NEGATE_EXPR, TREE_TYPE (min), max);
734 else if (cmp == 0 || cmp == 1)
736 max = min;
737 min = fold_unary (NEGATE_EXPR, TREE_TYPE (min), min);
739 else
741 set_value_range_to_varying (vr);
742 return;
744 set_and_canonicalize_value_range (vr, VR_RANGE, min, max, NULL);
748 /* Return value range information for VAR.
750 If we have no values ranges recorded (ie, VRP is not running), then
751 return NULL. Otherwise create an empty range if none existed for VAR. */
753 static value_range_t *
754 get_value_range (const_tree var)
756 static const struct value_range_d vr_const_varying
757 = { VR_VARYING, NULL_TREE, NULL_TREE, NULL };
758 value_range_t *vr;
759 tree sym;
760 unsigned ver = SSA_NAME_VERSION (var);
762 /* If we have no recorded ranges, then return NULL. */
763 if (! vr_value)
764 return NULL;
766 /* If we query the range for a new SSA name return an unmodifiable VARYING.
767 We should get here at most from the substitute-and-fold stage which
768 will never try to change values. */
769 if (ver >= num_vr_values)
770 return CONST_CAST (value_range_t *, &vr_const_varying);
772 vr = vr_value[ver];
773 if (vr)
774 return vr;
776 /* After propagation finished do not allocate new value-ranges. */
777 if (values_propagated)
778 return CONST_CAST (value_range_t *, &vr_const_varying);
780 /* Create a default value range. */
781 vr_value[ver] = vr = XCNEW (value_range_t);
783 /* Defer allocating the equivalence set. */
784 vr->equiv = NULL;
786 /* If VAR is a default definition of a parameter, the variable can
787 take any value in VAR's type. */
788 if (SSA_NAME_IS_DEFAULT_DEF (var))
790 sym = SSA_NAME_VAR (var);
791 if (TREE_CODE (sym) == PARM_DECL)
793 /* Try to use the "nonnull" attribute to create ~[0, 0]
794 anti-ranges for pointers. Note that this is only valid with
795 default definitions of PARM_DECLs. */
796 if (POINTER_TYPE_P (TREE_TYPE (sym))
797 && nonnull_arg_p (sym))
798 set_value_range_to_nonnull (vr, TREE_TYPE (sym));
799 else
800 set_value_range_to_varying (vr);
802 else if (TREE_CODE (sym) == RESULT_DECL
803 && DECL_BY_REFERENCE (sym))
804 set_value_range_to_nonnull (vr, TREE_TYPE (sym));
807 return vr;
810 /* Return true, if VAL1 and VAL2 are equal values for VRP purposes. */
812 static inline bool
813 vrp_operand_equal_p (const_tree val1, const_tree val2)
815 if (val1 == val2)
816 return true;
817 if (!val1 || !val2 || !operand_equal_p (val1, val2, 0))
818 return false;
819 return is_overflow_infinity (val1) == is_overflow_infinity (val2);
822 /* Return true, if the bitmaps B1 and B2 are equal. */
824 static inline bool
825 vrp_bitmap_equal_p (const_bitmap b1, const_bitmap b2)
827 return (b1 == b2
828 || ((!b1 || bitmap_empty_p (b1))
829 && (!b2 || bitmap_empty_p (b2)))
830 || (b1 && b2
831 && bitmap_equal_p (b1, b2)));
834 /* Update the value range and equivalence set for variable VAR to
835 NEW_VR. Return true if NEW_VR is different from VAR's previous
836 value.
838 NOTE: This function assumes that NEW_VR is a temporary value range
839 object created for the sole purpose of updating VAR's range. The
840 storage used by the equivalence set from NEW_VR will be freed by
841 this function. Do not call update_value_range when NEW_VR
842 is the range object associated with another SSA name. */
844 static inline bool
845 update_value_range (const_tree var, value_range_t *new_vr)
847 value_range_t *old_vr;
848 bool is_new;
850 /* Update the value range, if necessary. */
851 old_vr = get_value_range (var);
852 is_new = old_vr->type != new_vr->type
853 || !vrp_operand_equal_p (old_vr->min, new_vr->min)
854 || !vrp_operand_equal_p (old_vr->max, new_vr->max)
855 || !vrp_bitmap_equal_p (old_vr->equiv, new_vr->equiv);
857 if (is_new)
859 /* Do not allow transitions up the lattice. The following
860 is slightly more awkward than just new_vr->type < old_vr->type
861 because VR_RANGE and VR_ANTI_RANGE need to be considered
862 the same. We may not have is_new when transitioning to
863 UNDEFINED or from VARYING. */
864 if (new_vr->type == VR_UNDEFINED
865 || old_vr->type == VR_VARYING)
866 set_value_range_to_varying (old_vr);
867 else
868 set_value_range (old_vr, new_vr->type, new_vr->min, new_vr->max,
869 new_vr->equiv);
872 BITMAP_FREE (new_vr->equiv);
874 return is_new;
878 /* Add VAR and VAR's equivalence set to EQUIV. This is the central
879 point where equivalence processing can be turned on/off. */
881 static void
882 add_equivalence (bitmap *equiv, const_tree var)
884 unsigned ver = SSA_NAME_VERSION (var);
885 value_range_t *vr = vr_value[ver];
887 if (*equiv == NULL)
888 *equiv = BITMAP_ALLOC (NULL);
889 bitmap_set_bit (*equiv, ver);
890 if (vr && vr->equiv)
891 bitmap_ior_into (*equiv, vr->equiv);
895 /* Return true if VR is ~[0, 0]. */
897 static inline bool
898 range_is_nonnull (value_range_t *vr)
900 return vr->type == VR_ANTI_RANGE
901 && integer_zerop (vr->min)
902 && integer_zerop (vr->max);
906 /* Return true if VR is [0, 0]. */
908 static inline bool
909 range_is_null (value_range_t *vr)
911 return vr->type == VR_RANGE
912 && integer_zerop (vr->min)
913 && integer_zerop (vr->max);
916 /* Return true if max and min of VR are INTEGER_CST. It's not necessary
917 a singleton. */
919 static inline bool
920 range_int_cst_p (value_range_t *vr)
922 return (vr->type == VR_RANGE
923 && TREE_CODE (vr->max) == INTEGER_CST
924 && TREE_CODE (vr->min) == INTEGER_CST);
927 /* Return true if VR is a INTEGER_CST singleton. */
929 static inline bool
930 range_int_cst_singleton_p (value_range_t *vr)
932 return (range_int_cst_p (vr)
933 && !is_overflow_infinity (vr->min)
934 && !is_overflow_infinity (vr->max)
935 && tree_int_cst_equal (vr->min, vr->max));
938 /* Return true if value range VR involves at least one symbol. */
940 static inline bool
941 symbolic_range_p (value_range_t *vr)
943 return (!is_gimple_min_invariant (vr->min)
944 || !is_gimple_min_invariant (vr->max));
947 /* Return the single symbol (an SSA_NAME) contained in T if any, or NULL_TREE
948 otherwise. We only handle additive operations and set NEG to true if the
949 symbol is negated and INV to the invariant part, if any. */
951 static tree
952 get_single_symbol (tree t, bool *neg, tree *inv)
954 bool neg_;
955 tree inv_;
957 if (TREE_CODE (t) == PLUS_EXPR
958 || TREE_CODE (t) == POINTER_PLUS_EXPR
959 || TREE_CODE (t) == MINUS_EXPR)
961 if (is_gimple_min_invariant (TREE_OPERAND (t, 0)))
963 neg_ = (TREE_CODE (t) == MINUS_EXPR);
964 inv_ = TREE_OPERAND (t, 0);
965 t = TREE_OPERAND (t, 1);
967 else if (is_gimple_min_invariant (TREE_OPERAND (t, 1)))
969 neg_ = false;
970 inv_ = TREE_OPERAND (t, 1);
971 t = TREE_OPERAND (t, 0);
973 else
974 return NULL_TREE;
976 else
978 neg_ = false;
979 inv_ = NULL_TREE;
982 if (TREE_CODE (t) == NEGATE_EXPR)
984 t = TREE_OPERAND (t, 0);
985 neg_ = !neg_;
988 if (TREE_CODE (t) != SSA_NAME)
989 return NULL_TREE;
991 *neg = neg_;
992 *inv = inv_;
993 return t;
996 /* The reverse operation: build a symbolic expression with TYPE
997 from symbol SYM, negated according to NEG, and invariant INV. */
999 static tree
1000 build_symbolic_expr (tree type, tree sym, bool neg, tree inv)
1002 const bool pointer_p = POINTER_TYPE_P (type);
1003 tree t = sym;
1005 if (neg)
1006 t = build1 (NEGATE_EXPR, type, t);
1008 if (integer_zerop (inv))
1009 return t;
1011 return build2 (pointer_p ? POINTER_PLUS_EXPR : PLUS_EXPR, type, t, inv);
1014 /* Return true if value range VR involves exactly one symbol SYM. */
1016 static bool
1017 symbolic_range_based_on_p (value_range_t *vr, const_tree sym)
1019 bool neg, min_has_symbol, max_has_symbol;
1020 tree inv;
1022 if (is_gimple_min_invariant (vr->min))
1023 min_has_symbol = false;
1024 else if (get_single_symbol (vr->min, &neg, &inv) == sym)
1025 min_has_symbol = true;
1026 else
1027 return false;
1029 if (is_gimple_min_invariant (vr->max))
1030 max_has_symbol = false;
1031 else if (get_single_symbol (vr->max, &neg, &inv) == sym)
1032 max_has_symbol = true;
1033 else
1034 return false;
1036 return (min_has_symbol || max_has_symbol);
1039 /* Return true if value range VR uses an overflow infinity. */
1041 static inline bool
1042 overflow_infinity_range_p (value_range_t *vr)
1044 return (vr->type == VR_RANGE
1045 && (is_overflow_infinity (vr->min)
1046 || is_overflow_infinity (vr->max)));
1049 /* Return false if we can not make a valid comparison based on VR;
1050 this will be the case if it uses an overflow infinity and overflow
1051 is not undefined (i.e., -fno-strict-overflow is in effect).
1052 Otherwise return true, and set *STRICT_OVERFLOW_P to true if VR
1053 uses an overflow infinity. */
1055 static bool
1056 usable_range_p (value_range_t *vr, bool *strict_overflow_p)
1058 gcc_assert (vr->type == VR_RANGE);
1059 if (is_overflow_infinity (vr->min))
1061 *strict_overflow_p = true;
1062 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (vr->min)))
1063 return false;
1065 if (is_overflow_infinity (vr->max))
1067 *strict_overflow_p = true;
1068 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (vr->max)))
1069 return false;
1071 return true;
1075 /* Return true if the result of assignment STMT is know to be non-negative.
1076 If the return value is based on the assumption that signed overflow is
1077 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1078 *STRICT_OVERFLOW_P.*/
1080 static bool
1081 gimple_assign_nonnegative_warnv_p (gimple stmt, bool *strict_overflow_p)
1083 enum tree_code code = gimple_assign_rhs_code (stmt);
1084 switch (get_gimple_rhs_class (code))
1086 case GIMPLE_UNARY_RHS:
1087 return tree_unary_nonnegative_warnv_p (gimple_assign_rhs_code (stmt),
1088 gimple_expr_type (stmt),
1089 gimple_assign_rhs1 (stmt),
1090 strict_overflow_p);
1091 case GIMPLE_BINARY_RHS:
1092 return tree_binary_nonnegative_warnv_p (gimple_assign_rhs_code (stmt),
1093 gimple_expr_type (stmt),
1094 gimple_assign_rhs1 (stmt),
1095 gimple_assign_rhs2 (stmt),
1096 strict_overflow_p);
1097 case GIMPLE_TERNARY_RHS:
1098 return false;
1099 case GIMPLE_SINGLE_RHS:
1100 return tree_single_nonnegative_warnv_p (gimple_assign_rhs1 (stmt),
1101 strict_overflow_p);
1102 case GIMPLE_INVALID_RHS:
1103 gcc_unreachable ();
1104 default:
1105 gcc_unreachable ();
1109 /* Return true if return value of call STMT is know to be non-negative.
1110 If the return value is based on the assumption that signed overflow is
1111 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1112 *STRICT_OVERFLOW_P.*/
1114 static bool
1115 gimple_call_nonnegative_warnv_p (gimple stmt, bool *strict_overflow_p)
1117 tree arg0 = gimple_call_num_args (stmt) > 0 ?
1118 gimple_call_arg (stmt, 0) : NULL_TREE;
1119 tree arg1 = gimple_call_num_args (stmt) > 1 ?
1120 gimple_call_arg (stmt, 1) : NULL_TREE;
1122 return tree_call_nonnegative_warnv_p (gimple_expr_type (stmt),
1123 gimple_call_fndecl (stmt),
1124 arg0,
1125 arg1,
1126 strict_overflow_p);
1129 /* Return true if STMT is know to to compute a non-negative value.
1130 If the return value is based on the assumption that signed overflow is
1131 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1132 *STRICT_OVERFLOW_P.*/
1134 static bool
1135 gimple_stmt_nonnegative_warnv_p (gimple stmt, bool *strict_overflow_p)
1137 switch (gimple_code (stmt))
1139 case GIMPLE_ASSIGN:
1140 return gimple_assign_nonnegative_warnv_p (stmt, strict_overflow_p);
1141 case GIMPLE_CALL:
1142 return gimple_call_nonnegative_warnv_p (stmt, strict_overflow_p);
1143 default:
1144 gcc_unreachable ();
1148 /* Return true if the result of assignment STMT is know to be non-zero.
1149 If the return value is based on the assumption that signed overflow is
1150 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1151 *STRICT_OVERFLOW_P.*/
1153 static bool
1154 gimple_assign_nonzero_warnv_p (gimple stmt, bool *strict_overflow_p)
1156 enum tree_code code = gimple_assign_rhs_code (stmt);
1157 switch (get_gimple_rhs_class (code))
1159 case GIMPLE_UNARY_RHS:
1160 return tree_unary_nonzero_warnv_p (gimple_assign_rhs_code (stmt),
1161 gimple_expr_type (stmt),
1162 gimple_assign_rhs1 (stmt),
1163 strict_overflow_p);
1164 case GIMPLE_BINARY_RHS:
1165 return tree_binary_nonzero_warnv_p (gimple_assign_rhs_code (stmt),
1166 gimple_expr_type (stmt),
1167 gimple_assign_rhs1 (stmt),
1168 gimple_assign_rhs2 (stmt),
1169 strict_overflow_p);
1170 case GIMPLE_TERNARY_RHS:
1171 return false;
1172 case GIMPLE_SINGLE_RHS:
1173 return tree_single_nonzero_warnv_p (gimple_assign_rhs1 (stmt),
1174 strict_overflow_p);
1175 case GIMPLE_INVALID_RHS:
1176 gcc_unreachable ();
1177 default:
1178 gcc_unreachable ();
1182 /* Return true if STMT is known to compute a non-zero value.
1183 If the return value is based on the assumption that signed overflow is
1184 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1185 *STRICT_OVERFLOW_P.*/
1187 static bool
1188 gimple_stmt_nonzero_warnv_p (gimple stmt, bool *strict_overflow_p)
1190 switch (gimple_code (stmt))
1192 case GIMPLE_ASSIGN:
1193 return gimple_assign_nonzero_warnv_p (stmt, strict_overflow_p);
1194 case GIMPLE_CALL:
1196 tree fndecl = gimple_call_fndecl (stmt);
1197 if (!fndecl) return false;
1198 if (flag_delete_null_pointer_checks && !flag_check_new
1199 && DECL_IS_OPERATOR_NEW (fndecl)
1200 && !TREE_NOTHROW (fndecl))
1201 return true;
1202 if (flag_delete_null_pointer_checks &&
1203 lookup_attribute ("returns_nonnull",
1204 TYPE_ATTRIBUTES (gimple_call_fntype (stmt))))
1205 return true;
1206 return gimple_alloca_call_p (stmt);
1208 default:
1209 gcc_unreachable ();
1213 /* Like tree_expr_nonzero_warnv_p, but this function uses value ranges
1214 obtained so far. */
1216 static bool
1217 vrp_stmt_computes_nonzero (gimple stmt, bool *strict_overflow_p)
1219 if (gimple_stmt_nonzero_warnv_p (stmt, strict_overflow_p))
1220 return true;
1222 /* If we have an expression of the form &X->a, then the expression
1223 is nonnull if X is nonnull. */
1224 if (is_gimple_assign (stmt)
1225 && gimple_assign_rhs_code (stmt) == ADDR_EXPR)
1227 tree expr = gimple_assign_rhs1 (stmt);
1228 tree base = get_base_address (TREE_OPERAND (expr, 0));
1230 if (base != NULL_TREE
1231 && TREE_CODE (base) == MEM_REF
1232 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
1234 value_range_t *vr = get_value_range (TREE_OPERAND (base, 0));
1235 if (range_is_nonnull (vr))
1236 return true;
1240 return false;
1243 /* Returns true if EXPR is a valid value (as expected by compare_values) --
1244 a gimple invariant, or SSA_NAME +- CST. */
1246 static bool
1247 valid_value_p (tree expr)
1249 if (TREE_CODE (expr) == SSA_NAME)
1250 return true;
1252 if (TREE_CODE (expr) == PLUS_EXPR
1253 || TREE_CODE (expr) == MINUS_EXPR)
1254 return (TREE_CODE (TREE_OPERAND (expr, 0)) == SSA_NAME
1255 && TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST);
1257 return is_gimple_min_invariant (expr);
1260 /* Return
1261 1 if VAL < VAL2
1262 0 if !(VAL < VAL2)
1263 -2 if those are incomparable. */
1264 static inline int
1265 operand_less_p (tree val, tree val2)
1267 /* LT is folded faster than GE and others. Inline the common case. */
1268 if (TREE_CODE (val) == INTEGER_CST && TREE_CODE (val2) == INTEGER_CST)
1269 return tree_int_cst_lt (val, val2);
1270 else
1272 tree tcmp;
1274 fold_defer_overflow_warnings ();
1276 tcmp = fold_binary_to_constant (LT_EXPR, boolean_type_node, val, val2);
1278 fold_undefer_and_ignore_overflow_warnings ();
1280 if (!tcmp
1281 || TREE_CODE (tcmp) != INTEGER_CST)
1282 return -2;
1284 if (!integer_zerop (tcmp))
1285 return 1;
1288 /* val >= val2, not considering overflow infinity. */
1289 if (is_negative_overflow_infinity (val))
1290 return is_negative_overflow_infinity (val2) ? 0 : 1;
1291 else if (is_positive_overflow_infinity (val2))
1292 return is_positive_overflow_infinity (val) ? 0 : 1;
1294 return 0;
1297 /* Compare two values VAL1 and VAL2. Return
1299 -2 if VAL1 and VAL2 cannot be compared at compile-time,
1300 -1 if VAL1 < VAL2,
1301 0 if VAL1 == VAL2,
1302 +1 if VAL1 > VAL2, and
1303 +2 if VAL1 != VAL2
1305 This is similar to tree_int_cst_compare but supports pointer values
1306 and values that cannot be compared at compile time.
1308 If STRICT_OVERFLOW_P is not NULL, then set *STRICT_OVERFLOW_P to
1309 true if the return value is only valid if we assume that signed
1310 overflow is undefined. */
1312 static int
1313 compare_values_warnv (tree val1, tree val2, bool *strict_overflow_p)
1315 if (val1 == val2)
1316 return 0;
1318 /* Below we rely on the fact that VAL1 and VAL2 are both pointers or
1319 both integers. */
1320 gcc_assert (POINTER_TYPE_P (TREE_TYPE (val1))
1321 == POINTER_TYPE_P (TREE_TYPE (val2)));
1323 /* Convert the two values into the same type. This is needed because
1324 sizetype causes sign extension even for unsigned types. */
1325 val2 = fold_convert (TREE_TYPE (val1), val2);
1326 STRIP_USELESS_TYPE_CONVERSION (val2);
1328 if ((TREE_CODE (val1) == SSA_NAME
1329 || (TREE_CODE (val1) == NEGATE_EXPR
1330 && TREE_CODE (TREE_OPERAND (val1, 0)) == SSA_NAME)
1331 || TREE_CODE (val1) == PLUS_EXPR
1332 || TREE_CODE (val1) == MINUS_EXPR)
1333 && (TREE_CODE (val2) == SSA_NAME
1334 || (TREE_CODE (val2) == NEGATE_EXPR
1335 && TREE_CODE (TREE_OPERAND (val2, 0)) == SSA_NAME)
1336 || TREE_CODE (val2) == PLUS_EXPR
1337 || TREE_CODE (val2) == MINUS_EXPR))
1339 tree n1, c1, n2, c2;
1340 enum tree_code code1, code2;
1342 /* If VAL1 and VAL2 are of the form '[-]NAME [+-] CST' or 'NAME',
1343 return -1 or +1 accordingly. If VAL1 and VAL2 don't use the
1344 same name, return -2. */
1345 if (TREE_CODE (val1) == SSA_NAME || TREE_CODE (val1) == NEGATE_EXPR)
1347 code1 = SSA_NAME;
1348 n1 = val1;
1349 c1 = NULL_TREE;
1351 else
1353 code1 = TREE_CODE (val1);
1354 n1 = TREE_OPERAND (val1, 0);
1355 c1 = TREE_OPERAND (val1, 1);
1356 if (tree_int_cst_sgn (c1) == -1)
1358 if (is_negative_overflow_infinity (c1))
1359 return -2;
1360 c1 = fold_unary_to_constant (NEGATE_EXPR, TREE_TYPE (c1), c1);
1361 if (!c1)
1362 return -2;
1363 code1 = code1 == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR;
1367 if (TREE_CODE (val2) == SSA_NAME || TREE_CODE (val2) == NEGATE_EXPR)
1369 code2 = SSA_NAME;
1370 n2 = val2;
1371 c2 = NULL_TREE;
1373 else
1375 code2 = TREE_CODE (val2);
1376 n2 = TREE_OPERAND (val2, 0);
1377 c2 = TREE_OPERAND (val2, 1);
1378 if (tree_int_cst_sgn (c2) == -1)
1380 if (is_negative_overflow_infinity (c2))
1381 return -2;
1382 c2 = fold_unary_to_constant (NEGATE_EXPR, TREE_TYPE (c2), c2);
1383 if (!c2)
1384 return -2;
1385 code2 = code2 == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR;
1389 /* Both values must use the same name. */
1390 if (TREE_CODE (n1) == NEGATE_EXPR && TREE_CODE (n2) == NEGATE_EXPR)
1392 n1 = TREE_OPERAND (n1, 0);
1393 n2 = TREE_OPERAND (n2, 0);
1395 if (n1 != n2)
1396 return -2;
1398 if (code1 == SSA_NAME && code2 == SSA_NAME)
1399 /* NAME == NAME */
1400 return 0;
1402 /* If overflow is defined we cannot simplify more. */
1403 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (val1)))
1404 return -2;
1406 if (strict_overflow_p != NULL
1407 && (code1 == SSA_NAME || !TREE_NO_WARNING (val1))
1408 && (code2 == SSA_NAME || !TREE_NO_WARNING (val2)))
1409 *strict_overflow_p = true;
1411 if (code1 == SSA_NAME)
1413 if (code2 == PLUS_EXPR)
1414 /* NAME < NAME + CST */
1415 return -1;
1416 else if (code2 == MINUS_EXPR)
1417 /* NAME > NAME - CST */
1418 return 1;
1420 else if (code1 == PLUS_EXPR)
1422 if (code2 == SSA_NAME)
1423 /* NAME + CST > NAME */
1424 return 1;
1425 else if (code2 == PLUS_EXPR)
1426 /* NAME + CST1 > NAME + CST2, if CST1 > CST2 */
1427 return compare_values_warnv (c1, c2, strict_overflow_p);
1428 else if (code2 == MINUS_EXPR)
1429 /* NAME + CST1 > NAME - CST2 */
1430 return 1;
1432 else if (code1 == MINUS_EXPR)
1434 if (code2 == SSA_NAME)
1435 /* NAME - CST < NAME */
1436 return -1;
1437 else if (code2 == PLUS_EXPR)
1438 /* NAME - CST1 < NAME + CST2 */
1439 return -1;
1440 else if (code2 == MINUS_EXPR)
1441 /* NAME - CST1 > NAME - CST2, if CST1 < CST2. Notice that
1442 C1 and C2 are swapped in the call to compare_values. */
1443 return compare_values_warnv (c2, c1, strict_overflow_p);
1446 gcc_unreachable ();
1449 /* We cannot compare non-constants. */
1450 if (!is_gimple_min_invariant (val1) || !is_gimple_min_invariant (val2))
1451 return -2;
1453 if (!POINTER_TYPE_P (TREE_TYPE (val1)))
1455 /* We cannot compare overflowed values, except for overflow
1456 infinities. */
1457 if (TREE_OVERFLOW (val1) || TREE_OVERFLOW (val2))
1459 if (strict_overflow_p != NULL)
1460 *strict_overflow_p = true;
1461 if (is_negative_overflow_infinity (val1))
1462 return is_negative_overflow_infinity (val2) ? 0 : -1;
1463 else if (is_negative_overflow_infinity (val2))
1464 return 1;
1465 else if (is_positive_overflow_infinity (val1))
1466 return is_positive_overflow_infinity (val2) ? 0 : 1;
1467 else if (is_positive_overflow_infinity (val2))
1468 return -1;
1469 return -2;
1472 return tree_int_cst_compare (val1, val2);
1474 else
1476 tree t;
1478 /* First see if VAL1 and VAL2 are not the same. */
1479 if (val1 == val2 || operand_equal_p (val1, val2, 0))
1480 return 0;
1482 /* If VAL1 is a lower address than VAL2, return -1. */
1483 if (operand_less_p (val1, val2) == 1)
1484 return -1;
1486 /* If VAL1 is a higher address than VAL2, return +1. */
1487 if (operand_less_p (val2, val1) == 1)
1488 return 1;
1490 /* If VAL1 is different than VAL2, return +2.
1491 For integer constants we either have already returned -1 or 1
1492 or they are equivalent. We still might succeed in proving
1493 something about non-trivial operands. */
1494 if (TREE_CODE (val1) != INTEGER_CST
1495 || TREE_CODE (val2) != INTEGER_CST)
1497 t = fold_binary_to_constant (NE_EXPR, boolean_type_node, val1, val2);
1498 if (t && integer_onep (t))
1499 return 2;
1502 return -2;
1506 /* Compare values like compare_values_warnv, but treat comparisons of
1507 nonconstants which rely on undefined overflow as incomparable. */
1509 static int
1510 compare_values (tree val1, tree val2)
1512 bool sop;
1513 int ret;
1515 sop = false;
1516 ret = compare_values_warnv (val1, val2, &sop);
1517 if (sop
1518 && (!is_gimple_min_invariant (val1) || !is_gimple_min_invariant (val2)))
1519 ret = -2;
1520 return ret;
1524 /* Return 1 if VAL is inside value range MIN <= VAL <= MAX,
1525 0 if VAL is not inside [MIN, MAX],
1526 -2 if we cannot tell either way.
1528 Benchmark compile/20001226-1.c compilation time after changing this
1529 function. */
1531 static inline int
1532 value_inside_range (tree val, tree min, tree max)
1534 int cmp1, cmp2;
1536 cmp1 = operand_less_p (val, min);
1537 if (cmp1 == -2)
1538 return -2;
1539 if (cmp1 == 1)
1540 return 0;
1542 cmp2 = operand_less_p (max, val);
1543 if (cmp2 == -2)
1544 return -2;
1546 return !cmp2;
1550 /* Return true if value ranges VR0 and VR1 have a non-empty
1551 intersection.
1553 Benchmark compile/20001226-1.c compilation time after changing this
1554 function.
1557 static inline bool
1558 value_ranges_intersect_p (value_range_t *vr0, value_range_t *vr1)
1560 /* The value ranges do not intersect if the maximum of the first range is
1561 less than the minimum of the second range or vice versa.
1562 When those relations are unknown, we can't do any better. */
1563 if (operand_less_p (vr0->max, vr1->min) != 0)
1564 return false;
1565 if (operand_less_p (vr1->max, vr0->min) != 0)
1566 return false;
1567 return true;
1571 /* Return 1 if [MIN, MAX] includes the value zero, 0 if it does not
1572 include the value zero, -2 if we cannot tell. */
1574 static inline int
1575 range_includes_zero_p (tree min, tree max)
1577 tree zero = build_int_cst (TREE_TYPE (min), 0);
1578 return value_inside_range (zero, min, max);
1581 /* Return true if *VR is know to only contain nonnegative values. */
1583 static inline bool
1584 value_range_nonnegative_p (value_range_t *vr)
1586 /* Testing for VR_ANTI_RANGE is not useful here as any anti-range
1587 which would return a useful value should be encoded as a
1588 VR_RANGE. */
1589 if (vr->type == VR_RANGE)
1591 int result = compare_values (vr->min, integer_zero_node);
1592 return (result == 0 || result == 1);
1595 return false;
1598 /* If *VR has a value rante that is a single constant value return that,
1599 otherwise return NULL_TREE. */
1601 static tree
1602 value_range_constant_singleton (value_range_t *vr)
1604 if (vr->type == VR_RANGE
1605 && operand_equal_p (vr->min, vr->max, 0)
1606 && is_gimple_min_invariant (vr->min))
1607 return vr->min;
1609 return NULL_TREE;
1612 /* If OP has a value range with a single constant value return that,
1613 otherwise return NULL_TREE. This returns OP itself if OP is a
1614 constant. */
1616 static tree
1617 op_with_constant_singleton_value_range (tree op)
1619 if (is_gimple_min_invariant (op))
1620 return op;
1622 if (TREE_CODE (op) != SSA_NAME)
1623 return NULL_TREE;
1625 return value_range_constant_singleton (get_value_range (op));
1628 /* Return true if op is in a boolean [0, 1] value-range. */
1630 static bool
1631 op_with_boolean_value_range_p (tree op)
1633 value_range_t *vr;
1635 if (TYPE_PRECISION (TREE_TYPE (op)) == 1)
1636 return true;
1638 if (integer_zerop (op)
1639 || integer_onep (op))
1640 return true;
1642 if (TREE_CODE (op) != SSA_NAME)
1643 return false;
1645 vr = get_value_range (op);
1646 return (vr->type == VR_RANGE
1647 && integer_zerop (vr->min)
1648 && integer_onep (vr->max));
1651 /* Extract value range information from an ASSERT_EXPR EXPR and store
1652 it in *VR_P. */
1654 static void
1655 extract_range_from_assert (value_range_t *vr_p, tree expr)
1657 tree var, cond, limit, min, max, type;
1658 value_range_t *limit_vr;
1659 enum tree_code cond_code;
1661 var = ASSERT_EXPR_VAR (expr);
1662 cond = ASSERT_EXPR_COND (expr);
1664 gcc_assert (COMPARISON_CLASS_P (cond));
1666 /* Find VAR in the ASSERT_EXPR conditional. */
1667 if (var == TREE_OPERAND (cond, 0)
1668 || TREE_CODE (TREE_OPERAND (cond, 0)) == PLUS_EXPR
1669 || TREE_CODE (TREE_OPERAND (cond, 0)) == NOP_EXPR)
1671 /* If the predicate is of the form VAR COMP LIMIT, then we just
1672 take LIMIT from the RHS and use the same comparison code. */
1673 cond_code = TREE_CODE (cond);
1674 limit = TREE_OPERAND (cond, 1);
1675 cond = TREE_OPERAND (cond, 0);
1677 else
1679 /* If the predicate is of the form LIMIT COMP VAR, then we need
1680 to flip around the comparison code to create the proper range
1681 for VAR. */
1682 cond_code = swap_tree_comparison (TREE_CODE (cond));
1683 limit = TREE_OPERAND (cond, 0);
1684 cond = TREE_OPERAND (cond, 1);
1687 limit = avoid_overflow_infinity (limit);
1689 type = TREE_TYPE (var);
1690 gcc_assert (limit != var);
1692 /* For pointer arithmetic, we only keep track of pointer equality
1693 and inequality. */
1694 if (POINTER_TYPE_P (type) && cond_code != NE_EXPR && cond_code != EQ_EXPR)
1696 set_value_range_to_varying (vr_p);
1697 return;
1700 /* If LIMIT is another SSA name and LIMIT has a range of its own,
1701 try to use LIMIT's range to avoid creating symbolic ranges
1702 unnecessarily. */
1703 limit_vr = (TREE_CODE (limit) == SSA_NAME) ? get_value_range (limit) : NULL;
1705 /* LIMIT's range is only interesting if it has any useful information. */
1706 if (limit_vr
1707 && (limit_vr->type == VR_UNDEFINED
1708 || limit_vr->type == VR_VARYING
1709 || symbolic_range_p (limit_vr)))
1710 limit_vr = NULL;
1712 /* Initially, the new range has the same set of equivalences of
1713 VAR's range. This will be revised before returning the final
1714 value. Since assertions may be chained via mutually exclusive
1715 predicates, we will need to trim the set of equivalences before
1716 we are done. */
1717 gcc_assert (vr_p->equiv == NULL);
1718 add_equivalence (&vr_p->equiv, var);
1720 /* Extract a new range based on the asserted comparison for VAR and
1721 LIMIT's value range. Notice that if LIMIT has an anti-range, we
1722 will only use it for equality comparisons (EQ_EXPR). For any
1723 other kind of assertion, we cannot derive a range from LIMIT's
1724 anti-range that can be used to describe the new range. For
1725 instance, ASSERT_EXPR <x_2, x_2 <= b_4>. If b_4 is ~[2, 10],
1726 then b_4 takes on the ranges [-INF, 1] and [11, +INF]. There is
1727 no single range for x_2 that could describe LE_EXPR, so we might
1728 as well build the range [b_4, +INF] for it.
1729 One special case we handle is extracting a range from a
1730 range test encoded as (unsigned)var + CST <= limit. */
1731 if (TREE_CODE (cond) == NOP_EXPR
1732 || TREE_CODE (cond) == PLUS_EXPR)
1734 if (TREE_CODE (cond) == PLUS_EXPR)
1736 min = fold_build1 (NEGATE_EXPR, TREE_TYPE (TREE_OPERAND (cond, 1)),
1737 TREE_OPERAND (cond, 1));
1738 max = int_const_binop (PLUS_EXPR, limit, min);
1739 cond = TREE_OPERAND (cond, 0);
1741 else
1743 min = build_int_cst (TREE_TYPE (var), 0);
1744 max = limit;
1747 /* Make sure to not set TREE_OVERFLOW on the final type
1748 conversion. We are willingly interpreting large positive
1749 unsigned values as negative signed values here. */
1750 min = force_fit_type (TREE_TYPE (var), wi::to_widest (min), 0, false);
1751 max = force_fit_type (TREE_TYPE (var), wi::to_widest (max), 0, false);
1753 /* We can transform a max, min range to an anti-range or
1754 vice-versa. Use set_and_canonicalize_value_range which does
1755 this for us. */
1756 if (cond_code == LE_EXPR)
1757 set_and_canonicalize_value_range (vr_p, VR_RANGE,
1758 min, max, vr_p->equiv);
1759 else if (cond_code == GT_EXPR)
1760 set_and_canonicalize_value_range (vr_p, VR_ANTI_RANGE,
1761 min, max, vr_p->equiv);
1762 else
1763 gcc_unreachable ();
1765 else if (cond_code == EQ_EXPR)
1767 enum value_range_type range_type;
1769 if (limit_vr)
1771 range_type = limit_vr->type;
1772 min = limit_vr->min;
1773 max = limit_vr->max;
1775 else
1777 range_type = VR_RANGE;
1778 min = limit;
1779 max = limit;
1782 set_value_range (vr_p, range_type, min, max, vr_p->equiv);
1784 /* When asserting the equality VAR == LIMIT and LIMIT is another
1785 SSA name, the new range will also inherit the equivalence set
1786 from LIMIT. */
1787 if (TREE_CODE (limit) == SSA_NAME)
1788 add_equivalence (&vr_p->equiv, limit);
1790 else if (cond_code == NE_EXPR)
1792 /* As described above, when LIMIT's range is an anti-range and
1793 this assertion is an inequality (NE_EXPR), then we cannot
1794 derive anything from the anti-range. For instance, if
1795 LIMIT's range was ~[0, 0], the assertion 'VAR != LIMIT' does
1796 not imply that VAR's range is [0, 0]. So, in the case of
1797 anti-ranges, we just assert the inequality using LIMIT and
1798 not its anti-range.
1800 If LIMIT_VR is a range, we can only use it to build a new
1801 anti-range if LIMIT_VR is a single-valued range. For
1802 instance, if LIMIT_VR is [0, 1], the predicate
1803 VAR != [0, 1] does not mean that VAR's range is ~[0, 1].
1804 Rather, it means that for value 0 VAR should be ~[0, 0]
1805 and for value 1, VAR should be ~[1, 1]. We cannot
1806 represent these ranges.
1808 The only situation in which we can build a valid
1809 anti-range is when LIMIT_VR is a single-valued range
1810 (i.e., LIMIT_VR->MIN == LIMIT_VR->MAX). In that case,
1811 build the anti-range ~[LIMIT_VR->MIN, LIMIT_VR->MAX]. */
1812 if (limit_vr
1813 && limit_vr->type == VR_RANGE
1814 && compare_values (limit_vr->min, limit_vr->max) == 0)
1816 min = limit_vr->min;
1817 max = limit_vr->max;
1819 else
1821 /* In any other case, we cannot use LIMIT's range to build a
1822 valid anti-range. */
1823 min = max = limit;
1826 /* If MIN and MAX cover the whole range for their type, then
1827 just use the original LIMIT. */
1828 if (INTEGRAL_TYPE_P (type)
1829 && vrp_val_is_min (min)
1830 && vrp_val_is_max (max))
1831 min = max = limit;
1833 set_and_canonicalize_value_range (vr_p, VR_ANTI_RANGE,
1834 min, max, vr_p->equiv);
1836 else if (cond_code == LE_EXPR || cond_code == LT_EXPR)
1838 min = TYPE_MIN_VALUE (type);
1840 if (limit_vr == NULL || limit_vr->type == VR_ANTI_RANGE)
1841 max = limit;
1842 else
1844 /* If LIMIT_VR is of the form [N1, N2], we need to build the
1845 range [MIN, N2] for LE_EXPR and [MIN, N2 - 1] for
1846 LT_EXPR. */
1847 max = limit_vr->max;
1850 /* If the maximum value forces us to be out of bounds, simply punt.
1851 It would be pointless to try and do anything more since this
1852 all should be optimized away above us. */
1853 if ((cond_code == LT_EXPR
1854 && compare_values (max, min) == 0)
1855 || is_overflow_infinity (max))
1856 set_value_range_to_varying (vr_p);
1857 else
1859 /* For LT_EXPR, we create the range [MIN, MAX - 1]. */
1860 if (cond_code == LT_EXPR)
1862 if (TYPE_PRECISION (TREE_TYPE (max)) == 1
1863 && !TYPE_UNSIGNED (TREE_TYPE (max)))
1864 max = fold_build2 (PLUS_EXPR, TREE_TYPE (max), max,
1865 build_int_cst (TREE_TYPE (max), -1));
1866 else
1867 max = fold_build2 (MINUS_EXPR, TREE_TYPE (max), max,
1868 build_int_cst (TREE_TYPE (max), 1));
1869 if (EXPR_P (max))
1870 TREE_NO_WARNING (max) = 1;
1873 set_value_range (vr_p, VR_RANGE, min, max, vr_p->equiv);
1876 else if (cond_code == GE_EXPR || cond_code == GT_EXPR)
1878 max = TYPE_MAX_VALUE (type);
1880 if (limit_vr == NULL || limit_vr->type == VR_ANTI_RANGE)
1881 min = limit;
1882 else
1884 /* If LIMIT_VR is of the form [N1, N2], we need to build the
1885 range [N1, MAX] for GE_EXPR and [N1 + 1, MAX] for
1886 GT_EXPR. */
1887 min = limit_vr->min;
1890 /* If the minimum value forces us to be out of bounds, simply punt.
1891 It would be pointless to try and do anything more since this
1892 all should be optimized away above us. */
1893 if ((cond_code == GT_EXPR
1894 && compare_values (min, max) == 0)
1895 || is_overflow_infinity (min))
1896 set_value_range_to_varying (vr_p);
1897 else
1899 /* For GT_EXPR, we create the range [MIN + 1, MAX]. */
1900 if (cond_code == GT_EXPR)
1902 if (TYPE_PRECISION (TREE_TYPE (min)) == 1
1903 && !TYPE_UNSIGNED (TREE_TYPE (min)))
1904 min = fold_build2 (MINUS_EXPR, TREE_TYPE (min), min,
1905 build_int_cst (TREE_TYPE (min), -1));
1906 else
1907 min = fold_build2 (PLUS_EXPR, TREE_TYPE (min), min,
1908 build_int_cst (TREE_TYPE (min), 1));
1909 if (EXPR_P (min))
1910 TREE_NO_WARNING (min) = 1;
1913 set_value_range (vr_p, VR_RANGE, min, max, vr_p->equiv);
1916 else
1917 gcc_unreachable ();
1919 /* Finally intersect the new range with what we already know about var. */
1920 vrp_intersect_ranges (vr_p, get_value_range (var));
1924 /* Extract range information from SSA name VAR and store it in VR. If
1925 VAR has an interesting range, use it. Otherwise, create the
1926 range [VAR, VAR] and return it. This is useful in situations where
1927 we may have conditionals testing values of VARYING names. For
1928 instance,
1930 x_3 = y_5;
1931 if (x_3 > y_5)
1934 Even if y_5 is deemed VARYING, we can determine that x_3 > y_5 is
1935 always false. */
1937 static void
1938 extract_range_from_ssa_name (value_range_t *vr, tree var)
1940 value_range_t *var_vr = get_value_range (var);
1942 if (var_vr->type != VR_VARYING)
1943 copy_value_range (vr, var_vr);
1944 else
1945 set_value_range (vr, VR_RANGE, var, var, NULL);
1947 add_equivalence (&vr->equiv, var);
1951 /* Wrapper around int_const_binop. If the operation overflows and we
1952 are not using wrapping arithmetic, then adjust the result to be
1953 -INF or +INF depending on CODE, VAL1 and VAL2. This can return
1954 NULL_TREE if we need to use an overflow infinity representation but
1955 the type does not support it. */
1957 static tree
1958 vrp_int_const_binop (enum tree_code code, tree val1, tree val2)
1960 tree res;
1962 res = int_const_binop (code, val1, val2);
1964 /* If we are using unsigned arithmetic, operate symbolically
1965 on -INF and +INF as int_const_binop only handles signed overflow. */
1966 if (TYPE_UNSIGNED (TREE_TYPE (val1)))
1968 int checkz = compare_values (res, val1);
1969 bool overflow = false;
1971 /* Ensure that res = val1 [+*] val2 >= val1
1972 or that res = val1 - val2 <= val1. */
1973 if ((code == PLUS_EXPR
1974 && !(checkz == 1 || checkz == 0))
1975 || (code == MINUS_EXPR
1976 && !(checkz == 0 || checkz == -1)))
1978 overflow = true;
1980 /* Checking for multiplication overflow is done by dividing the
1981 output of the multiplication by the first input of the
1982 multiplication. If the result of that division operation is
1983 not equal to the second input of the multiplication, then the
1984 multiplication overflowed. */
1985 else if (code == MULT_EXPR && !integer_zerop (val1))
1987 tree tmp = int_const_binop (TRUNC_DIV_EXPR,
1988 res,
1989 val1);
1990 int check = compare_values (tmp, val2);
1992 if (check != 0)
1993 overflow = true;
1996 if (overflow)
1998 res = copy_node (res);
1999 TREE_OVERFLOW (res) = 1;
2003 else if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (val1)))
2004 /* If the singed operation wraps then int_const_binop has done
2005 everything we want. */
2007 /* Signed division of -1/0 overflows and by the time it gets here
2008 returns NULL_TREE. */
2009 else if (!res)
2010 return NULL_TREE;
2011 else if ((TREE_OVERFLOW (res)
2012 && !TREE_OVERFLOW (val1)
2013 && !TREE_OVERFLOW (val2))
2014 || is_overflow_infinity (val1)
2015 || is_overflow_infinity (val2))
2017 /* If the operation overflowed but neither VAL1 nor VAL2 are
2018 overflown, return -INF or +INF depending on the operation
2019 and the combination of signs of the operands. */
2020 int sgn1 = tree_int_cst_sgn (val1);
2021 int sgn2 = tree_int_cst_sgn (val2);
2023 if (needs_overflow_infinity (TREE_TYPE (res))
2024 && !supports_overflow_infinity (TREE_TYPE (res)))
2025 return NULL_TREE;
2027 /* We have to punt on adding infinities of different signs,
2028 since we can't tell what the sign of the result should be.
2029 Likewise for subtracting infinities of the same sign. */
2030 if (((code == PLUS_EXPR && sgn1 != sgn2)
2031 || (code == MINUS_EXPR && sgn1 == sgn2))
2032 && is_overflow_infinity (val1)
2033 && is_overflow_infinity (val2))
2034 return NULL_TREE;
2036 /* Don't try to handle division or shifting of infinities. */
2037 if ((code == TRUNC_DIV_EXPR
2038 || code == FLOOR_DIV_EXPR
2039 || code == CEIL_DIV_EXPR
2040 || code == EXACT_DIV_EXPR
2041 || code == ROUND_DIV_EXPR
2042 || code == RSHIFT_EXPR)
2043 && (is_overflow_infinity (val1)
2044 || is_overflow_infinity (val2)))
2045 return NULL_TREE;
2047 /* Notice that we only need to handle the restricted set of
2048 operations handled by extract_range_from_binary_expr.
2049 Among them, only multiplication, addition and subtraction
2050 can yield overflow without overflown operands because we
2051 are working with integral types only... except in the
2052 case VAL1 = -INF and VAL2 = -1 which overflows to +INF
2053 for division too. */
2055 /* For multiplication, the sign of the overflow is given
2056 by the comparison of the signs of the operands. */
2057 if ((code == MULT_EXPR && sgn1 == sgn2)
2058 /* For addition, the operands must be of the same sign
2059 to yield an overflow. Its sign is therefore that
2060 of one of the operands, for example the first. For
2061 infinite operands X + -INF is negative, not positive. */
2062 || (code == PLUS_EXPR
2063 && (sgn1 >= 0
2064 ? !is_negative_overflow_infinity (val2)
2065 : is_positive_overflow_infinity (val2)))
2066 /* For subtraction, non-infinite operands must be of
2067 different signs to yield an overflow. Its sign is
2068 therefore that of the first operand or the opposite of
2069 that of the second operand. A first operand of 0 counts
2070 as positive here, for the corner case 0 - (-INF), which
2071 overflows, but must yield +INF. For infinite operands 0
2072 - INF is negative, not positive. */
2073 || (code == MINUS_EXPR
2074 && (sgn1 >= 0
2075 ? !is_positive_overflow_infinity (val2)
2076 : is_negative_overflow_infinity (val2)))
2077 /* We only get in here with positive shift count, so the
2078 overflow direction is the same as the sign of val1.
2079 Actually rshift does not overflow at all, but we only
2080 handle the case of shifting overflowed -INF and +INF. */
2081 || (code == RSHIFT_EXPR
2082 && sgn1 >= 0)
2083 /* For division, the only case is -INF / -1 = +INF. */
2084 || code == TRUNC_DIV_EXPR
2085 || code == FLOOR_DIV_EXPR
2086 || code == CEIL_DIV_EXPR
2087 || code == EXACT_DIV_EXPR
2088 || code == ROUND_DIV_EXPR)
2089 return (needs_overflow_infinity (TREE_TYPE (res))
2090 ? positive_overflow_infinity (TREE_TYPE (res))
2091 : TYPE_MAX_VALUE (TREE_TYPE (res)));
2092 else
2093 return (needs_overflow_infinity (TREE_TYPE (res))
2094 ? negative_overflow_infinity (TREE_TYPE (res))
2095 : TYPE_MIN_VALUE (TREE_TYPE (res)));
2098 return res;
2102 /* For range VR compute two wide_int bitmasks. In *MAY_BE_NONZERO
2103 bitmask if some bit is unset, it means for all numbers in the range
2104 the bit is 0, otherwise it might be 0 or 1. In *MUST_BE_NONZERO
2105 bitmask if some bit is set, it means for all numbers in the range
2106 the bit is 1, otherwise it might be 0 or 1. */
2108 static bool
2109 zero_nonzero_bits_from_vr (const tree expr_type,
2110 value_range_t *vr,
2111 wide_int *may_be_nonzero,
2112 wide_int *must_be_nonzero)
2114 *may_be_nonzero = wi::minus_one (TYPE_PRECISION (expr_type));
2115 *must_be_nonzero = wi::zero (TYPE_PRECISION (expr_type));
2116 if (!range_int_cst_p (vr)
2117 || is_overflow_infinity (vr->min)
2118 || is_overflow_infinity (vr->max))
2119 return false;
2121 if (range_int_cst_singleton_p (vr))
2123 *may_be_nonzero = vr->min;
2124 *must_be_nonzero = *may_be_nonzero;
2126 else if (tree_int_cst_sgn (vr->min) >= 0
2127 || tree_int_cst_sgn (vr->max) < 0)
2129 wide_int xor_mask = wi::bit_xor (vr->min, vr->max);
2130 *may_be_nonzero = wi::bit_or (vr->min, vr->max);
2131 *must_be_nonzero = wi::bit_and (vr->min, vr->max);
2132 if (xor_mask != 0)
2134 wide_int mask = wi::mask (wi::floor_log2 (xor_mask), false,
2135 may_be_nonzero->get_precision ());
2136 *may_be_nonzero = *may_be_nonzero | mask;
2137 *must_be_nonzero = must_be_nonzero->and_not (mask);
2141 return true;
2144 /* Create two value-ranges in *VR0 and *VR1 from the anti-range *AR
2145 so that *VR0 U *VR1 == *AR. Returns true if that is possible,
2146 false otherwise. If *AR can be represented with a single range
2147 *VR1 will be VR_UNDEFINED. */
2149 static bool
2150 ranges_from_anti_range (value_range_t *ar,
2151 value_range_t *vr0, value_range_t *vr1)
2153 tree type = TREE_TYPE (ar->min);
2155 vr0->type = VR_UNDEFINED;
2156 vr1->type = VR_UNDEFINED;
2158 if (ar->type != VR_ANTI_RANGE
2159 || TREE_CODE (ar->min) != INTEGER_CST
2160 || TREE_CODE (ar->max) != INTEGER_CST
2161 || !vrp_val_min (type)
2162 || !vrp_val_max (type))
2163 return false;
2165 if (!vrp_val_is_min (ar->min))
2167 vr0->type = VR_RANGE;
2168 vr0->min = vrp_val_min (type);
2169 vr0->max = wide_int_to_tree (type, wi::sub (ar->min, 1));
2171 if (!vrp_val_is_max (ar->max))
2173 vr1->type = VR_RANGE;
2174 vr1->min = wide_int_to_tree (type, wi::add (ar->max, 1));
2175 vr1->max = vrp_val_max (type);
2177 if (vr0->type == VR_UNDEFINED)
2179 *vr0 = *vr1;
2180 vr1->type = VR_UNDEFINED;
2183 return vr0->type != VR_UNDEFINED;
2186 /* Helper to extract a value-range *VR for a multiplicative operation
2187 *VR0 CODE *VR1. */
2189 static void
2190 extract_range_from_multiplicative_op_1 (value_range_t *vr,
2191 enum tree_code code,
2192 value_range_t *vr0, value_range_t *vr1)
2194 enum value_range_type type;
2195 tree val[4];
2196 size_t i;
2197 tree min, max;
2198 bool sop;
2199 int cmp;
2201 /* Multiplications, divisions and shifts are a bit tricky to handle,
2202 depending on the mix of signs we have in the two ranges, we
2203 need to operate on different values to get the minimum and
2204 maximum values for the new range. One approach is to figure
2205 out all the variations of range combinations and do the
2206 operations.
2208 However, this involves several calls to compare_values and it
2209 is pretty convoluted. It's simpler to do the 4 operations
2210 (MIN0 OP MIN1, MIN0 OP MAX1, MAX0 OP MIN1 and MAX0 OP MAX0 OP
2211 MAX1) and then figure the smallest and largest values to form
2212 the new range. */
2213 gcc_assert (code == MULT_EXPR
2214 || code == TRUNC_DIV_EXPR
2215 || code == FLOOR_DIV_EXPR
2216 || code == CEIL_DIV_EXPR
2217 || code == EXACT_DIV_EXPR
2218 || code == ROUND_DIV_EXPR
2219 || code == RSHIFT_EXPR
2220 || code == LSHIFT_EXPR);
2221 gcc_assert ((vr0->type == VR_RANGE
2222 || (code == MULT_EXPR && vr0->type == VR_ANTI_RANGE))
2223 && vr0->type == vr1->type);
2225 type = vr0->type;
2227 /* Compute the 4 cross operations. */
2228 sop = false;
2229 val[0] = vrp_int_const_binop (code, vr0->min, vr1->min);
2230 if (val[0] == NULL_TREE)
2231 sop = true;
2233 if (vr1->max == vr1->min)
2234 val[1] = NULL_TREE;
2235 else
2237 val[1] = vrp_int_const_binop (code, vr0->min, vr1->max);
2238 if (val[1] == NULL_TREE)
2239 sop = true;
2242 if (vr0->max == vr0->min)
2243 val[2] = NULL_TREE;
2244 else
2246 val[2] = vrp_int_const_binop (code, vr0->max, vr1->min);
2247 if (val[2] == NULL_TREE)
2248 sop = true;
2251 if (vr0->min == vr0->max || vr1->min == vr1->max)
2252 val[3] = NULL_TREE;
2253 else
2255 val[3] = vrp_int_const_binop (code, vr0->max, vr1->max);
2256 if (val[3] == NULL_TREE)
2257 sop = true;
2260 if (sop)
2262 set_value_range_to_varying (vr);
2263 return;
2266 /* Set MIN to the minimum of VAL[i] and MAX to the maximum
2267 of VAL[i]. */
2268 min = val[0];
2269 max = val[0];
2270 for (i = 1; i < 4; i++)
2272 if (!is_gimple_min_invariant (min)
2273 || (TREE_OVERFLOW (min) && !is_overflow_infinity (min))
2274 || !is_gimple_min_invariant (max)
2275 || (TREE_OVERFLOW (max) && !is_overflow_infinity (max)))
2276 break;
2278 if (val[i])
2280 if (!is_gimple_min_invariant (val[i])
2281 || (TREE_OVERFLOW (val[i])
2282 && !is_overflow_infinity (val[i])))
2284 /* If we found an overflowed value, set MIN and MAX
2285 to it so that we set the resulting range to
2286 VARYING. */
2287 min = max = val[i];
2288 break;
2291 if (compare_values (val[i], min) == -1)
2292 min = val[i];
2294 if (compare_values (val[i], max) == 1)
2295 max = val[i];
2299 /* If either MIN or MAX overflowed, then set the resulting range to
2300 VARYING. But we do accept an overflow infinity
2301 representation. */
2302 if (min == NULL_TREE
2303 || !is_gimple_min_invariant (min)
2304 || (TREE_OVERFLOW (min) && !is_overflow_infinity (min))
2305 || max == NULL_TREE
2306 || !is_gimple_min_invariant (max)
2307 || (TREE_OVERFLOW (max) && !is_overflow_infinity (max)))
2309 set_value_range_to_varying (vr);
2310 return;
2313 /* We punt if:
2314 1) [-INF, +INF]
2315 2) [-INF, +-INF(OVF)]
2316 3) [+-INF(OVF), +INF]
2317 4) [+-INF(OVF), +-INF(OVF)]
2318 We learn nothing when we have INF and INF(OVF) on both sides.
2319 Note that we do accept [-INF, -INF] and [+INF, +INF] without
2320 overflow. */
2321 if ((vrp_val_is_min (min) || is_overflow_infinity (min))
2322 && (vrp_val_is_max (max) || is_overflow_infinity (max)))
2324 set_value_range_to_varying (vr);
2325 return;
2328 cmp = compare_values (min, max);
2329 if (cmp == -2 || cmp == 1)
2331 /* If the new range has its limits swapped around (MIN > MAX),
2332 then the operation caused one of them to wrap around, mark
2333 the new range VARYING. */
2334 set_value_range_to_varying (vr);
2336 else
2337 set_value_range (vr, type, min, max, NULL);
2340 /* Extract range information from a binary operation CODE based on
2341 the ranges of each of its operands *VR0 and *VR1 with resulting
2342 type EXPR_TYPE. The resulting range is stored in *VR. */
2344 static void
2345 extract_range_from_binary_expr_1 (value_range_t *vr,
2346 enum tree_code code, tree expr_type,
2347 value_range_t *vr0_, value_range_t *vr1_)
2349 value_range_t vr0 = *vr0_, vr1 = *vr1_;
2350 value_range_t vrtem0 = VR_INITIALIZER, vrtem1 = VR_INITIALIZER;
2351 enum value_range_type type;
2352 tree min = NULL_TREE, max = NULL_TREE;
2353 int cmp;
2355 if (!INTEGRAL_TYPE_P (expr_type)
2356 && !POINTER_TYPE_P (expr_type))
2358 set_value_range_to_varying (vr);
2359 return;
2362 /* Not all binary expressions can be applied to ranges in a
2363 meaningful way. Handle only arithmetic operations. */
2364 if (code != PLUS_EXPR
2365 && code != MINUS_EXPR
2366 && code != POINTER_PLUS_EXPR
2367 && code != MULT_EXPR
2368 && code != TRUNC_DIV_EXPR
2369 && code != FLOOR_DIV_EXPR
2370 && code != CEIL_DIV_EXPR
2371 && code != EXACT_DIV_EXPR
2372 && code != ROUND_DIV_EXPR
2373 && code != TRUNC_MOD_EXPR
2374 && code != RSHIFT_EXPR
2375 && code != LSHIFT_EXPR
2376 && code != MIN_EXPR
2377 && code != MAX_EXPR
2378 && code != BIT_AND_EXPR
2379 && code != BIT_IOR_EXPR
2380 && code != BIT_XOR_EXPR)
2382 set_value_range_to_varying (vr);
2383 return;
2386 /* If both ranges are UNDEFINED, so is the result. */
2387 if (vr0.type == VR_UNDEFINED && vr1.type == VR_UNDEFINED)
2389 set_value_range_to_undefined (vr);
2390 return;
2392 /* If one of the ranges is UNDEFINED drop it to VARYING for the following
2393 code. At some point we may want to special-case operations that
2394 have UNDEFINED result for all or some value-ranges of the not UNDEFINED
2395 operand. */
2396 else if (vr0.type == VR_UNDEFINED)
2397 set_value_range_to_varying (&vr0);
2398 else if (vr1.type == VR_UNDEFINED)
2399 set_value_range_to_varying (&vr1);
2401 /* Now canonicalize anti-ranges to ranges when they are not symbolic
2402 and express ~[] op X as ([]' op X) U ([]'' op X). */
2403 if (vr0.type == VR_ANTI_RANGE
2404 && ranges_from_anti_range (&vr0, &vrtem0, &vrtem1))
2406 extract_range_from_binary_expr_1 (vr, code, expr_type, &vrtem0, vr1_);
2407 if (vrtem1.type != VR_UNDEFINED)
2409 value_range_t vrres = VR_INITIALIZER;
2410 extract_range_from_binary_expr_1 (&vrres, code, expr_type,
2411 &vrtem1, vr1_);
2412 vrp_meet (vr, &vrres);
2414 return;
2416 /* Likewise for X op ~[]. */
2417 if (vr1.type == VR_ANTI_RANGE
2418 && ranges_from_anti_range (&vr1, &vrtem0, &vrtem1))
2420 extract_range_from_binary_expr_1 (vr, code, expr_type, vr0_, &vrtem0);
2421 if (vrtem1.type != VR_UNDEFINED)
2423 value_range_t vrres = VR_INITIALIZER;
2424 extract_range_from_binary_expr_1 (&vrres, code, expr_type,
2425 vr0_, &vrtem1);
2426 vrp_meet (vr, &vrres);
2428 return;
2431 /* The type of the resulting value range defaults to VR0.TYPE. */
2432 type = vr0.type;
2434 /* Refuse to operate on VARYING ranges, ranges of different kinds
2435 and symbolic ranges. As an exception, we allow BIT_{AND,IOR}
2436 because we may be able to derive a useful range even if one of
2437 the operands is VR_VARYING or symbolic range. Similarly for
2438 divisions, MIN/MAX and PLUS/MINUS.
2440 TODO, we may be able to derive anti-ranges in some cases. */
2441 if (code != BIT_AND_EXPR
2442 && code != BIT_IOR_EXPR
2443 && code != TRUNC_DIV_EXPR
2444 && code != FLOOR_DIV_EXPR
2445 && code != CEIL_DIV_EXPR
2446 && code != EXACT_DIV_EXPR
2447 && code != ROUND_DIV_EXPR
2448 && code != TRUNC_MOD_EXPR
2449 && code != MIN_EXPR
2450 && code != MAX_EXPR
2451 && code != PLUS_EXPR
2452 && code != MINUS_EXPR
2453 && code != RSHIFT_EXPR
2454 && (vr0.type == VR_VARYING
2455 || vr1.type == VR_VARYING
2456 || vr0.type != vr1.type
2457 || symbolic_range_p (&vr0)
2458 || symbolic_range_p (&vr1)))
2460 set_value_range_to_varying (vr);
2461 return;
2464 /* Now evaluate the expression to determine the new range. */
2465 if (POINTER_TYPE_P (expr_type))
2467 if (code == MIN_EXPR || code == MAX_EXPR)
2469 /* For MIN/MAX expressions with pointers, we only care about
2470 nullness, if both are non null, then the result is nonnull.
2471 If both are null, then the result is null. Otherwise they
2472 are varying. */
2473 if (range_is_nonnull (&vr0) && range_is_nonnull (&vr1))
2474 set_value_range_to_nonnull (vr, expr_type);
2475 else if (range_is_null (&vr0) && range_is_null (&vr1))
2476 set_value_range_to_null (vr, expr_type);
2477 else
2478 set_value_range_to_varying (vr);
2480 else if (code == POINTER_PLUS_EXPR)
2482 /* For pointer types, we are really only interested in asserting
2483 whether the expression evaluates to non-NULL. */
2484 if (range_is_nonnull (&vr0) || range_is_nonnull (&vr1))
2485 set_value_range_to_nonnull (vr, expr_type);
2486 else if (range_is_null (&vr0) && range_is_null (&vr1))
2487 set_value_range_to_null (vr, expr_type);
2488 else
2489 set_value_range_to_varying (vr);
2491 else if (code == BIT_AND_EXPR)
2493 /* For pointer types, we are really only interested in asserting
2494 whether the expression evaluates to non-NULL. */
2495 if (range_is_nonnull (&vr0) && range_is_nonnull (&vr1))
2496 set_value_range_to_nonnull (vr, expr_type);
2497 else if (range_is_null (&vr0) || range_is_null (&vr1))
2498 set_value_range_to_null (vr, expr_type);
2499 else
2500 set_value_range_to_varying (vr);
2502 else
2503 set_value_range_to_varying (vr);
2505 return;
2508 /* For integer ranges, apply the operation to each end of the
2509 range and see what we end up with. */
2510 if (code == PLUS_EXPR || code == MINUS_EXPR)
2512 const bool minus_p = (code == MINUS_EXPR);
2513 tree min_op0 = vr0.min;
2514 tree min_op1 = minus_p ? vr1.max : vr1.min;
2515 tree max_op0 = vr0.max;
2516 tree max_op1 = minus_p ? vr1.min : vr1.max;
2517 tree sym_min_op0 = NULL_TREE;
2518 tree sym_min_op1 = NULL_TREE;
2519 tree sym_max_op0 = NULL_TREE;
2520 tree sym_max_op1 = NULL_TREE;
2521 bool neg_min_op0, neg_min_op1, neg_max_op0, neg_max_op1;
2523 /* If we have a PLUS or MINUS with two VR_RANGEs, either constant or
2524 single-symbolic ranges, try to compute the precise resulting range,
2525 but only if we know that this resulting range will also be constant
2526 or single-symbolic. */
2527 if (vr0.type == VR_RANGE && vr1.type == VR_RANGE
2528 && (TREE_CODE (min_op0) == INTEGER_CST
2529 || (sym_min_op0
2530 = get_single_symbol (min_op0, &neg_min_op0, &min_op0)))
2531 && (TREE_CODE (min_op1) == INTEGER_CST
2532 || (sym_min_op1
2533 = get_single_symbol (min_op1, &neg_min_op1, &min_op1)))
2534 && (!(sym_min_op0 && sym_min_op1)
2535 || (sym_min_op0 == sym_min_op1
2536 && neg_min_op0 == (minus_p ? neg_min_op1 : !neg_min_op1)))
2537 && (TREE_CODE (max_op0) == INTEGER_CST
2538 || (sym_max_op0
2539 = get_single_symbol (max_op0, &neg_max_op0, &max_op0)))
2540 && (TREE_CODE (max_op1) == INTEGER_CST
2541 || (sym_max_op1
2542 = get_single_symbol (max_op1, &neg_max_op1, &max_op1)))
2543 && (!(sym_max_op0 && sym_max_op1)
2544 || (sym_max_op0 == sym_max_op1
2545 && neg_max_op0 == (minus_p ? neg_max_op1 : !neg_max_op1))))
2547 const signop sgn = TYPE_SIGN (expr_type);
2548 const unsigned int prec = TYPE_PRECISION (expr_type);
2549 wide_int type_min, type_max, wmin, wmax;
2550 int min_ovf = 0;
2551 int max_ovf = 0;
2553 /* Get the lower and upper bounds of the type. */
2554 if (TYPE_OVERFLOW_WRAPS (expr_type))
2556 type_min = wi::min_value (prec, sgn);
2557 type_max = wi::max_value (prec, sgn);
2559 else
2561 type_min = vrp_val_min (expr_type);
2562 type_max = vrp_val_max (expr_type);
2565 /* Combine the lower bounds, if any. */
2566 if (min_op0 && min_op1)
2568 if (minus_p)
2570 wmin = wi::sub (min_op0, min_op1);
2572 /* Check for overflow. */
2573 if (wi::cmp (0, min_op1, sgn)
2574 != wi::cmp (wmin, min_op0, sgn))
2575 min_ovf = wi::cmp (min_op0, min_op1, sgn);
2577 else
2579 wmin = wi::add (min_op0, min_op1);
2581 /* Check for overflow. */
2582 if (wi::cmp (min_op1, 0, sgn)
2583 != wi::cmp (wmin, min_op0, sgn))
2584 min_ovf = wi::cmp (min_op0, wmin, sgn);
2587 else if (min_op0)
2588 wmin = min_op0;
2589 else if (min_op1)
2590 wmin = minus_p ? wi::neg (min_op1) : min_op1;
2591 else
2592 wmin = wi::shwi (0, prec);
2594 /* Combine the upper bounds, if any. */
2595 if (max_op0 && max_op1)
2597 if (minus_p)
2599 wmax = wi::sub (max_op0, max_op1);
2601 /* Check for overflow. */
2602 if (wi::cmp (0, max_op1, sgn)
2603 != wi::cmp (wmax, max_op0, sgn))
2604 max_ovf = wi::cmp (max_op0, max_op1, sgn);
2606 else
2608 wmax = wi::add (max_op0, max_op1);
2610 if (wi::cmp (max_op1, 0, sgn)
2611 != wi::cmp (wmax, max_op0, sgn))
2612 max_ovf = wi::cmp (max_op0, wmax, sgn);
2615 else if (max_op0)
2616 wmax = max_op0;
2617 else if (max_op1)
2618 wmax = minus_p ? wi::neg (max_op1) : max_op1;
2619 else
2620 wmax = wi::shwi (0, prec);
2622 /* Check for type overflow. */
2623 if (min_ovf == 0)
2625 if (wi::cmp (wmin, type_min, sgn) == -1)
2626 min_ovf = -1;
2627 else if (wi::cmp (wmin, type_max, sgn) == 1)
2628 min_ovf = 1;
2630 if (max_ovf == 0)
2632 if (wi::cmp (wmax, type_min, sgn) == -1)
2633 max_ovf = -1;
2634 else if (wi::cmp (wmax, type_max, sgn) == 1)
2635 max_ovf = 1;
2638 /* If we have overflow for the constant part and the resulting
2639 range will be symbolic, drop to VR_VARYING. */
2640 if ((min_ovf && sym_min_op0 != sym_min_op1)
2641 || (max_ovf && sym_max_op0 != sym_max_op1))
2643 set_value_range_to_varying (vr);
2644 return;
2647 if (TYPE_OVERFLOW_WRAPS (expr_type))
2649 /* If overflow wraps, truncate the values and adjust the
2650 range kind and bounds appropriately. */
2651 wide_int tmin = wide_int::from (wmin, prec, sgn);
2652 wide_int tmax = wide_int::from (wmax, prec, sgn);
2653 if (min_ovf == max_ovf)
2655 /* No overflow or both overflow or underflow. The
2656 range kind stays VR_RANGE. */
2657 min = wide_int_to_tree (expr_type, tmin);
2658 max = wide_int_to_tree (expr_type, tmax);
2660 else if (min_ovf == -1 && max_ovf == 1)
2662 /* Underflow and overflow, drop to VR_VARYING. */
2663 set_value_range_to_varying (vr);
2664 return;
2666 else
2668 /* Min underflow or max overflow. The range kind
2669 changes to VR_ANTI_RANGE. */
2670 bool covers = false;
2671 wide_int tem = tmin;
2672 gcc_assert ((min_ovf == -1 && max_ovf == 0)
2673 || (max_ovf == 1 && min_ovf == 0));
2674 type = VR_ANTI_RANGE;
2675 tmin = tmax + 1;
2676 if (wi::cmp (tmin, tmax, sgn) < 0)
2677 covers = true;
2678 tmax = tem - 1;
2679 if (wi::cmp (tmax, tem, sgn) > 0)
2680 covers = true;
2681 /* If the anti-range would cover nothing, drop to varying.
2682 Likewise if the anti-range bounds are outside of the
2683 types values. */
2684 if (covers || wi::cmp (tmin, tmax, sgn) > 0)
2686 set_value_range_to_varying (vr);
2687 return;
2689 min = wide_int_to_tree (expr_type, tmin);
2690 max = wide_int_to_tree (expr_type, tmax);
2693 else
2695 /* If overflow does not wrap, saturate to the types min/max
2696 value. */
2697 if (min_ovf == -1)
2699 if (needs_overflow_infinity (expr_type)
2700 && supports_overflow_infinity (expr_type))
2701 min = negative_overflow_infinity (expr_type);
2702 else
2703 min = wide_int_to_tree (expr_type, type_min);
2705 else if (min_ovf == 1)
2707 if (needs_overflow_infinity (expr_type)
2708 && supports_overflow_infinity (expr_type))
2709 min = positive_overflow_infinity (expr_type);
2710 else
2711 min = wide_int_to_tree (expr_type, type_max);
2713 else
2714 min = wide_int_to_tree (expr_type, wmin);
2716 if (max_ovf == -1)
2718 if (needs_overflow_infinity (expr_type)
2719 && supports_overflow_infinity (expr_type))
2720 max = negative_overflow_infinity (expr_type);
2721 else
2722 max = wide_int_to_tree (expr_type, type_min);
2724 else if (max_ovf == 1)
2726 if (needs_overflow_infinity (expr_type)
2727 && supports_overflow_infinity (expr_type))
2728 max = positive_overflow_infinity (expr_type);
2729 else
2730 max = wide_int_to_tree (expr_type, type_max);
2732 else
2733 max = wide_int_to_tree (expr_type, wmax);
2736 if (needs_overflow_infinity (expr_type)
2737 && supports_overflow_infinity (expr_type))
2739 if ((min_op0 && is_negative_overflow_infinity (min_op0))
2740 || (min_op1
2741 && (minus_p
2742 ? is_positive_overflow_infinity (min_op1)
2743 : is_negative_overflow_infinity (min_op1))))
2744 min = negative_overflow_infinity (expr_type);
2745 if ((max_op0 && is_positive_overflow_infinity (max_op0))
2746 || (max_op1
2747 && (minus_p
2748 ? is_negative_overflow_infinity (max_op1)
2749 : is_positive_overflow_infinity (max_op1))))
2750 max = positive_overflow_infinity (expr_type);
2753 /* If the result lower bound is constant, we're done;
2754 otherwise, build the symbolic lower bound. */
2755 if (sym_min_op0 == sym_min_op1)
2757 else if (sym_min_op0)
2758 min = build_symbolic_expr (expr_type, sym_min_op0,
2759 neg_min_op0, min);
2760 else if (sym_min_op1)
2761 min = build_symbolic_expr (expr_type, sym_min_op1,
2762 neg_min_op1 ^ minus_p, min);
2764 /* Likewise for the upper bound. */
2765 if (sym_max_op0 == sym_max_op1)
2767 else if (sym_max_op0)
2768 max = build_symbolic_expr (expr_type, sym_max_op0,
2769 neg_max_op0, max);
2770 else if (sym_max_op1)
2771 max = build_symbolic_expr (expr_type, sym_max_op1,
2772 neg_max_op1 ^ minus_p, max);
2774 else
2776 /* For other cases, for example if we have a PLUS_EXPR with two
2777 VR_ANTI_RANGEs, drop to VR_VARYING. It would take more effort
2778 to compute a precise range for such a case.
2779 ??? General even mixed range kind operations can be expressed
2780 by for example transforming ~[3, 5] + [1, 2] to range-only
2781 operations and a union primitive:
2782 [-INF, 2] + [1, 2] U [5, +INF] + [1, 2]
2783 [-INF+1, 4] U [6, +INF(OVF)]
2784 though usually the union is not exactly representable with
2785 a single range or anti-range as the above is
2786 [-INF+1, +INF(OVF)] intersected with ~[5, 5]
2787 but one could use a scheme similar to equivalences for this. */
2788 set_value_range_to_varying (vr);
2789 return;
2792 else if (code == MIN_EXPR
2793 || code == MAX_EXPR)
2795 if (vr0.type == VR_RANGE
2796 && !symbolic_range_p (&vr0))
2798 type = VR_RANGE;
2799 if (vr1.type == VR_RANGE
2800 && !symbolic_range_p (&vr1))
2802 /* For operations that make the resulting range directly
2803 proportional to the original ranges, apply the operation to
2804 the same end of each range. */
2805 min = vrp_int_const_binop (code, vr0.min, vr1.min);
2806 max = vrp_int_const_binop (code, vr0.max, vr1.max);
2808 else if (code == MIN_EXPR)
2810 min = vrp_val_min (expr_type);
2811 max = vr0.max;
2813 else if (code == MAX_EXPR)
2815 min = vr0.min;
2816 max = vrp_val_max (expr_type);
2819 else if (vr1.type == VR_RANGE
2820 && !symbolic_range_p (&vr1))
2822 type = VR_RANGE;
2823 if (code == MIN_EXPR)
2825 min = vrp_val_min (expr_type);
2826 max = vr1.max;
2828 else if (code == MAX_EXPR)
2830 min = vr1.min;
2831 max = vrp_val_max (expr_type);
2834 else
2836 set_value_range_to_varying (vr);
2837 return;
2840 else if (code == MULT_EXPR)
2842 /* Fancy code so that with unsigned, [-3,-1]*[-3,-1] does not
2843 drop to varying. This test requires 2*prec bits if both
2844 operands are signed and 2*prec + 2 bits if either is not. */
2846 signop sign = TYPE_SIGN (expr_type);
2847 unsigned int prec = TYPE_PRECISION (expr_type);
2849 if (range_int_cst_p (&vr0)
2850 && range_int_cst_p (&vr1)
2851 && TYPE_OVERFLOW_WRAPS (expr_type))
2853 typedef FIXED_WIDE_INT (WIDE_INT_MAX_PRECISION * 2) vrp_int;
2854 typedef generic_wide_int
2855 <wi::extended_tree <WIDE_INT_MAX_PRECISION * 2> > vrp_int_cst;
2856 vrp_int sizem1 = wi::mask <vrp_int> (prec, false);
2857 vrp_int size = sizem1 + 1;
2859 /* Extend the values using the sign of the result to PREC2.
2860 From here on out, everthing is just signed math no matter
2861 what the input types were. */
2862 vrp_int min0 = vrp_int_cst (vr0.min);
2863 vrp_int max0 = vrp_int_cst (vr0.max);
2864 vrp_int min1 = vrp_int_cst (vr1.min);
2865 vrp_int max1 = vrp_int_cst (vr1.max);
2866 /* Canonicalize the intervals. */
2867 if (sign == UNSIGNED)
2869 if (wi::ltu_p (size, min0 + max0))
2871 min0 -= size;
2872 max0 -= size;
2875 if (wi::ltu_p (size, min1 + max1))
2877 min1 -= size;
2878 max1 -= size;
2882 vrp_int prod0 = min0 * min1;
2883 vrp_int prod1 = min0 * max1;
2884 vrp_int prod2 = max0 * min1;
2885 vrp_int prod3 = max0 * max1;
2887 /* Sort the 4 products so that min is in prod0 and max is in
2888 prod3. */
2889 /* min0min1 > max0max1 */
2890 if (wi::gts_p (prod0, prod3))
2892 vrp_int tmp = prod3;
2893 prod3 = prod0;
2894 prod0 = tmp;
2897 /* min0max1 > max0min1 */
2898 if (wi::gts_p (prod1, prod2))
2900 vrp_int tmp = prod2;
2901 prod2 = prod1;
2902 prod1 = tmp;
2905 if (wi::gts_p (prod0, prod1))
2907 vrp_int tmp = prod1;
2908 prod1 = prod0;
2909 prod0 = tmp;
2912 if (wi::gts_p (prod2, prod3))
2914 vrp_int tmp = prod3;
2915 prod3 = prod2;
2916 prod2 = tmp;
2919 /* diff = max - min. */
2920 prod2 = prod3 - prod0;
2921 if (wi::geu_p (prod2, sizem1))
2923 /* the range covers all values. */
2924 set_value_range_to_varying (vr);
2925 return;
2928 /* The following should handle the wrapping and selecting
2929 VR_ANTI_RANGE for us. */
2930 min = wide_int_to_tree (expr_type, prod0);
2931 max = wide_int_to_tree (expr_type, prod3);
2932 set_and_canonicalize_value_range (vr, VR_RANGE, min, max, NULL);
2933 return;
2936 /* If we have an unsigned MULT_EXPR with two VR_ANTI_RANGEs,
2937 drop to VR_VARYING. It would take more effort to compute a
2938 precise range for such a case. For example, if we have
2939 op0 == 65536 and op1 == 65536 with their ranges both being
2940 ~[0,0] on a 32-bit machine, we would have op0 * op1 == 0, so
2941 we cannot claim that the product is in ~[0,0]. Note that we
2942 are guaranteed to have vr0.type == vr1.type at this
2943 point. */
2944 if (vr0.type == VR_ANTI_RANGE
2945 && !TYPE_OVERFLOW_UNDEFINED (expr_type))
2947 set_value_range_to_varying (vr);
2948 return;
2951 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
2952 return;
2954 else if (code == RSHIFT_EXPR
2955 || code == LSHIFT_EXPR)
2957 /* If we have a RSHIFT_EXPR with any shift values outside [0..prec-1],
2958 then drop to VR_VARYING. Outside of this range we get undefined
2959 behavior from the shift operation. We cannot even trust
2960 SHIFT_COUNT_TRUNCATED at this stage, because that applies to rtl
2961 shifts, and the operation at the tree level may be widened. */
2962 if (range_int_cst_p (&vr1)
2963 && compare_tree_int (vr1.min, 0) >= 0
2964 && compare_tree_int (vr1.max, TYPE_PRECISION (expr_type)) == -1)
2966 if (code == RSHIFT_EXPR)
2968 /* Even if vr0 is VARYING or otherwise not usable, we can derive
2969 useful ranges just from the shift count. E.g.
2970 x >> 63 for signed 64-bit x is always [-1, 0]. */
2971 if (vr0.type != VR_RANGE || symbolic_range_p (&vr0))
2973 vr0.type = type = VR_RANGE;
2974 vr0.min = vrp_val_min (expr_type);
2975 vr0.max = vrp_val_max (expr_type);
2977 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
2978 return;
2980 /* We can map lshifts by constants to MULT_EXPR handling. */
2981 else if (code == LSHIFT_EXPR
2982 && range_int_cst_singleton_p (&vr1))
2984 bool saved_flag_wrapv;
2985 value_range_t vr1p = VR_INITIALIZER;
2986 vr1p.type = VR_RANGE;
2987 vr1p.min = (wide_int_to_tree
2988 (expr_type,
2989 wi::set_bit_in_zero (tree_to_shwi (vr1.min),
2990 TYPE_PRECISION (expr_type))));
2991 vr1p.max = vr1p.min;
2992 /* We have to use a wrapping multiply though as signed overflow
2993 on lshifts is implementation defined in C89. */
2994 saved_flag_wrapv = flag_wrapv;
2995 flag_wrapv = 1;
2996 extract_range_from_binary_expr_1 (vr, MULT_EXPR, expr_type,
2997 &vr0, &vr1p);
2998 flag_wrapv = saved_flag_wrapv;
2999 return;
3001 else if (code == LSHIFT_EXPR
3002 && range_int_cst_p (&vr0))
3004 int prec = TYPE_PRECISION (expr_type);
3005 int overflow_pos = prec;
3006 int bound_shift;
3007 wide_int low_bound, high_bound;
3008 bool uns = TYPE_UNSIGNED (expr_type);
3009 bool in_bounds = false;
3011 if (!uns)
3012 overflow_pos -= 1;
3014 bound_shift = overflow_pos - tree_to_shwi (vr1.max);
3015 /* If bound_shift == HOST_BITS_PER_WIDE_INT, the llshift can
3016 overflow. However, for that to happen, vr1.max needs to be
3017 zero, which means vr1 is a singleton range of zero, which
3018 means it should be handled by the previous LSHIFT_EXPR
3019 if-clause. */
3020 wide_int bound = wi::set_bit_in_zero (bound_shift, prec);
3021 wide_int complement = ~(bound - 1);
3023 if (uns)
3025 low_bound = bound;
3026 high_bound = complement;
3027 if (wi::ltu_p (vr0.max, low_bound))
3029 /* [5, 6] << [1, 2] == [10, 24]. */
3030 /* We're shifting out only zeroes, the value increases
3031 monotonically. */
3032 in_bounds = true;
3034 else if (wi::ltu_p (high_bound, vr0.min))
3036 /* [0xffffff00, 0xffffffff] << [1, 2]
3037 == [0xfffffc00, 0xfffffffe]. */
3038 /* We're shifting out only ones, the value decreases
3039 monotonically. */
3040 in_bounds = true;
3043 else
3045 /* [-1, 1] << [1, 2] == [-4, 4]. */
3046 low_bound = complement;
3047 high_bound = bound;
3048 if (wi::lts_p (vr0.max, high_bound)
3049 && wi::lts_p (low_bound, vr0.min))
3051 /* For non-negative numbers, we're shifting out only
3052 zeroes, the value increases monotonically.
3053 For negative numbers, we're shifting out only ones, the
3054 value decreases monotomically. */
3055 in_bounds = true;
3059 if (in_bounds)
3061 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
3062 return;
3066 set_value_range_to_varying (vr);
3067 return;
3069 else if (code == TRUNC_DIV_EXPR
3070 || code == FLOOR_DIV_EXPR
3071 || code == CEIL_DIV_EXPR
3072 || code == EXACT_DIV_EXPR
3073 || code == ROUND_DIV_EXPR)
3075 if (vr0.type != VR_RANGE || symbolic_range_p (&vr0))
3077 /* For division, if op1 has VR_RANGE but op0 does not, something
3078 can be deduced just from that range. Say [min, max] / [4, max]
3079 gives [min / 4, max / 4] range. */
3080 if (vr1.type == VR_RANGE
3081 && !symbolic_range_p (&vr1)
3082 && range_includes_zero_p (vr1.min, vr1.max) == 0)
3084 vr0.type = type = VR_RANGE;
3085 vr0.min = vrp_val_min (expr_type);
3086 vr0.max = vrp_val_max (expr_type);
3088 else
3090 set_value_range_to_varying (vr);
3091 return;
3095 /* For divisions, if flag_non_call_exceptions is true, we must
3096 not eliminate a division by zero. */
3097 if (cfun->can_throw_non_call_exceptions
3098 && (vr1.type != VR_RANGE
3099 || range_includes_zero_p (vr1.min, vr1.max) != 0))
3101 set_value_range_to_varying (vr);
3102 return;
3105 /* For divisions, if op0 is VR_RANGE, we can deduce a range
3106 even if op1 is VR_VARYING, VR_ANTI_RANGE, symbolic or can
3107 include 0. */
3108 if (vr0.type == VR_RANGE
3109 && (vr1.type != VR_RANGE
3110 || range_includes_zero_p (vr1.min, vr1.max) != 0))
3112 tree zero = build_int_cst (TREE_TYPE (vr0.min), 0);
3113 int cmp;
3115 min = NULL_TREE;
3116 max = NULL_TREE;
3117 if (TYPE_UNSIGNED (expr_type)
3118 || value_range_nonnegative_p (&vr1))
3120 /* For unsigned division or when divisor is known
3121 to be non-negative, the range has to cover
3122 all numbers from 0 to max for positive max
3123 and all numbers from min to 0 for negative min. */
3124 cmp = compare_values (vr0.max, zero);
3125 if (cmp == -1)
3126 max = zero;
3127 else if (cmp == 0 || cmp == 1)
3128 max = vr0.max;
3129 else
3130 type = VR_VARYING;
3131 cmp = compare_values (vr0.min, zero);
3132 if (cmp == 1)
3133 min = zero;
3134 else if (cmp == 0 || cmp == -1)
3135 min = vr0.min;
3136 else
3137 type = VR_VARYING;
3139 else
3141 /* Otherwise the range is -max .. max or min .. -min
3142 depending on which bound is bigger in absolute value,
3143 as the division can change the sign. */
3144 abs_extent_range (vr, vr0.min, vr0.max);
3145 return;
3147 if (type == VR_VARYING)
3149 set_value_range_to_varying (vr);
3150 return;
3153 else
3155 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
3156 return;
3159 else if (code == TRUNC_MOD_EXPR)
3161 if (vr1.type != VR_RANGE
3162 || range_includes_zero_p (vr1.min, vr1.max) != 0
3163 || vrp_val_is_min (vr1.min))
3165 set_value_range_to_varying (vr);
3166 return;
3168 type = VR_RANGE;
3169 /* Compute MAX <|vr1.min|, |vr1.max|> - 1. */
3170 max = fold_unary_to_constant (ABS_EXPR, expr_type, vr1.min);
3171 if (tree_int_cst_lt (max, vr1.max))
3172 max = vr1.max;
3173 max = int_const_binop (MINUS_EXPR, max, build_int_cst (TREE_TYPE (max), 1));
3174 /* If the dividend is non-negative the modulus will be
3175 non-negative as well. */
3176 if (TYPE_UNSIGNED (expr_type)
3177 || value_range_nonnegative_p (&vr0))
3178 min = build_int_cst (TREE_TYPE (max), 0);
3179 else
3180 min = fold_unary_to_constant (NEGATE_EXPR, expr_type, max);
3182 else if (code == BIT_AND_EXPR || code == BIT_IOR_EXPR || code == BIT_XOR_EXPR)
3184 bool int_cst_range0, int_cst_range1;
3185 wide_int may_be_nonzero0, may_be_nonzero1;
3186 wide_int must_be_nonzero0, must_be_nonzero1;
3188 int_cst_range0 = zero_nonzero_bits_from_vr (expr_type, &vr0,
3189 &may_be_nonzero0,
3190 &must_be_nonzero0);
3191 int_cst_range1 = zero_nonzero_bits_from_vr (expr_type, &vr1,
3192 &may_be_nonzero1,
3193 &must_be_nonzero1);
3195 type = VR_RANGE;
3196 if (code == BIT_AND_EXPR)
3198 min = wide_int_to_tree (expr_type,
3199 must_be_nonzero0 & must_be_nonzero1);
3200 wide_int wmax = may_be_nonzero0 & may_be_nonzero1;
3201 /* If both input ranges contain only negative values we can
3202 truncate the result range maximum to the minimum of the
3203 input range maxima. */
3204 if (int_cst_range0 && int_cst_range1
3205 && tree_int_cst_sgn (vr0.max) < 0
3206 && tree_int_cst_sgn (vr1.max) < 0)
3208 wmax = wi::min (wmax, vr0.max, TYPE_SIGN (expr_type));
3209 wmax = wi::min (wmax, vr1.max, TYPE_SIGN (expr_type));
3211 /* If either input range contains only non-negative values
3212 we can truncate the result range maximum to the respective
3213 maximum of the input range. */
3214 if (int_cst_range0 && tree_int_cst_sgn (vr0.min) >= 0)
3215 wmax = wi::min (wmax, vr0.max, TYPE_SIGN (expr_type));
3216 if (int_cst_range1 && tree_int_cst_sgn (vr1.min) >= 0)
3217 wmax = wi::min (wmax, vr1.max, TYPE_SIGN (expr_type));
3218 max = wide_int_to_tree (expr_type, wmax);
3220 else if (code == BIT_IOR_EXPR)
3222 max = wide_int_to_tree (expr_type,
3223 may_be_nonzero0 | may_be_nonzero1);
3224 wide_int wmin = must_be_nonzero0 | must_be_nonzero1;
3225 /* If the input ranges contain only positive values we can
3226 truncate the minimum of the result range to the maximum
3227 of the input range minima. */
3228 if (int_cst_range0 && int_cst_range1
3229 && tree_int_cst_sgn (vr0.min) >= 0
3230 && tree_int_cst_sgn (vr1.min) >= 0)
3232 wmin = wi::max (wmin, vr0.min, TYPE_SIGN (expr_type));
3233 wmin = wi::max (wmin, vr1.min, TYPE_SIGN (expr_type));
3235 /* If either input range contains only negative values
3236 we can truncate the minimum of the result range to the
3237 respective minimum range. */
3238 if (int_cst_range0 && tree_int_cst_sgn (vr0.max) < 0)
3239 wmin = wi::max (wmin, vr0.min, TYPE_SIGN (expr_type));
3240 if (int_cst_range1 && tree_int_cst_sgn (vr1.max) < 0)
3241 wmin = wi::max (wmin, vr1.min, TYPE_SIGN (expr_type));
3242 min = wide_int_to_tree (expr_type, wmin);
3244 else if (code == BIT_XOR_EXPR)
3246 wide_int result_zero_bits = ((must_be_nonzero0 & must_be_nonzero1)
3247 | ~(may_be_nonzero0 | may_be_nonzero1));
3248 wide_int result_one_bits
3249 = (must_be_nonzero0.and_not (may_be_nonzero1)
3250 | must_be_nonzero1.and_not (may_be_nonzero0));
3251 max = wide_int_to_tree (expr_type, ~result_zero_bits);
3252 min = wide_int_to_tree (expr_type, result_one_bits);
3253 /* If the range has all positive or all negative values the
3254 result is better than VARYING. */
3255 if (tree_int_cst_sgn (min) < 0
3256 || tree_int_cst_sgn (max) >= 0)
3258 else
3259 max = min = NULL_TREE;
3262 else
3263 gcc_unreachable ();
3265 /* If either MIN or MAX overflowed, then set the resulting range to
3266 VARYING. But we do accept an overflow infinity representation. */
3267 if (min == NULL_TREE
3268 || (TREE_OVERFLOW_P (min) && !is_overflow_infinity (min))
3269 || max == NULL_TREE
3270 || (TREE_OVERFLOW_P (max) && !is_overflow_infinity (max)))
3272 set_value_range_to_varying (vr);
3273 return;
3276 /* We punt if:
3277 1) [-INF, +INF]
3278 2) [-INF, +-INF(OVF)]
3279 3) [+-INF(OVF), +INF]
3280 4) [+-INF(OVF), +-INF(OVF)]
3281 We learn nothing when we have INF and INF(OVF) on both sides.
3282 Note that we do accept [-INF, -INF] and [+INF, +INF] without
3283 overflow. */
3284 if ((vrp_val_is_min (min) || is_overflow_infinity (min))
3285 && (vrp_val_is_max (max) || is_overflow_infinity (max)))
3287 set_value_range_to_varying (vr);
3288 return;
3291 cmp = compare_values (min, max);
3292 if (cmp == -2 || cmp == 1)
3294 /* If the new range has its limits swapped around (MIN > MAX),
3295 then the operation caused one of them to wrap around, mark
3296 the new range VARYING. */
3297 set_value_range_to_varying (vr);
3299 else
3300 set_value_range (vr, type, min, max, NULL);
3303 /* Extract range information from a binary expression OP0 CODE OP1 based on
3304 the ranges of each of its operands with resulting type EXPR_TYPE.
3305 The resulting range is stored in *VR. */
3307 static void
3308 extract_range_from_binary_expr (value_range_t *vr,
3309 enum tree_code code,
3310 tree expr_type, tree op0, tree op1)
3312 value_range_t vr0 = VR_INITIALIZER;
3313 value_range_t vr1 = VR_INITIALIZER;
3315 /* Get value ranges for each operand. For constant operands, create
3316 a new value range with the operand to simplify processing. */
3317 if (TREE_CODE (op0) == SSA_NAME)
3318 vr0 = *(get_value_range (op0));
3319 else if (is_gimple_min_invariant (op0))
3320 set_value_range_to_value (&vr0, op0, NULL);
3321 else
3322 set_value_range_to_varying (&vr0);
3324 if (TREE_CODE (op1) == SSA_NAME)
3325 vr1 = *(get_value_range (op1));
3326 else if (is_gimple_min_invariant (op1))
3327 set_value_range_to_value (&vr1, op1, NULL);
3328 else
3329 set_value_range_to_varying (&vr1);
3331 extract_range_from_binary_expr_1 (vr, code, expr_type, &vr0, &vr1);
3333 /* Try harder for PLUS and MINUS if the range of one operand is symbolic
3334 and based on the other operand, for example if it was deduced from a
3335 symbolic comparison. When a bound of the range of the first operand
3336 is invariant, we set the corresponding bound of the new range to INF
3337 in order to avoid recursing on the range of the second operand. */
3338 if (vr->type == VR_VARYING
3339 && (code == PLUS_EXPR || code == MINUS_EXPR)
3340 && TREE_CODE (op1) == SSA_NAME
3341 && vr0.type == VR_RANGE
3342 && symbolic_range_based_on_p (&vr0, op1))
3344 const bool minus_p = (code == MINUS_EXPR);
3345 value_range_t n_vr1 = VR_INITIALIZER;
3347 /* Try with VR0 and [-INF, OP1]. */
3348 if (is_gimple_min_invariant (minus_p ? vr0.max : vr0.min))
3349 set_value_range (&n_vr1, VR_RANGE, vrp_val_min (expr_type), op1, NULL);
3351 /* Try with VR0 and [OP1, +INF]. */
3352 else if (is_gimple_min_invariant (minus_p ? vr0.min : vr0.max))
3353 set_value_range (&n_vr1, VR_RANGE, op1, vrp_val_max (expr_type), NULL);
3355 /* Try with VR0 and [OP1, OP1]. */
3356 else
3357 set_value_range (&n_vr1, VR_RANGE, op1, op1, NULL);
3359 extract_range_from_binary_expr_1 (vr, code, expr_type, &vr0, &n_vr1);
3362 if (vr->type == VR_VARYING
3363 && (code == PLUS_EXPR || code == MINUS_EXPR)
3364 && TREE_CODE (op0) == SSA_NAME
3365 && vr1.type == VR_RANGE
3366 && symbolic_range_based_on_p (&vr1, op0))
3368 const bool minus_p = (code == MINUS_EXPR);
3369 value_range_t n_vr0 = VR_INITIALIZER;
3371 /* Try with [-INF, OP0] and VR1. */
3372 if (is_gimple_min_invariant (minus_p ? vr1.max : vr1.min))
3373 set_value_range (&n_vr0, VR_RANGE, vrp_val_min (expr_type), op0, NULL);
3375 /* Try with [OP0, +INF] and VR1. */
3376 else if (is_gimple_min_invariant (minus_p ? vr1.min : vr1.max))
3377 set_value_range (&n_vr0, VR_RANGE, op0, vrp_val_max (expr_type), NULL);
3379 /* Try with [OP0, OP0] and VR1. */
3380 else
3381 set_value_range (&n_vr0, VR_RANGE, op0, op0, NULL);
3383 extract_range_from_binary_expr_1 (vr, code, expr_type, &n_vr0, &vr1);
3387 /* Extract range information from a unary operation CODE based on
3388 the range of its operand *VR0 with type OP0_TYPE with resulting type TYPE.
3389 The The resulting range is stored in *VR. */
3391 static void
3392 extract_range_from_unary_expr_1 (value_range_t *vr,
3393 enum tree_code code, tree type,
3394 value_range_t *vr0_, tree op0_type)
3396 value_range_t vr0 = *vr0_, vrtem0 = VR_INITIALIZER, vrtem1 = VR_INITIALIZER;
3398 /* VRP only operates on integral and pointer types. */
3399 if (!(INTEGRAL_TYPE_P (op0_type)
3400 || POINTER_TYPE_P (op0_type))
3401 || !(INTEGRAL_TYPE_P (type)
3402 || POINTER_TYPE_P (type)))
3404 set_value_range_to_varying (vr);
3405 return;
3408 /* If VR0 is UNDEFINED, so is the result. */
3409 if (vr0.type == VR_UNDEFINED)
3411 set_value_range_to_undefined (vr);
3412 return;
3415 /* Handle operations that we express in terms of others. */
3416 if (code == PAREN_EXPR || code == OBJ_TYPE_REF)
3418 /* PAREN_EXPR and OBJ_TYPE_REF are simple copies. */
3419 copy_value_range (vr, &vr0);
3420 return;
3422 else if (code == NEGATE_EXPR)
3424 /* -X is simply 0 - X, so re-use existing code that also handles
3425 anti-ranges fine. */
3426 value_range_t zero = VR_INITIALIZER;
3427 set_value_range_to_value (&zero, build_int_cst (type, 0), NULL);
3428 extract_range_from_binary_expr_1 (vr, MINUS_EXPR, type, &zero, &vr0);
3429 return;
3431 else if (code == BIT_NOT_EXPR)
3433 /* ~X is simply -1 - X, so re-use existing code that also handles
3434 anti-ranges fine. */
3435 value_range_t minusone = VR_INITIALIZER;
3436 set_value_range_to_value (&minusone, build_int_cst (type, -1), NULL);
3437 extract_range_from_binary_expr_1 (vr, MINUS_EXPR,
3438 type, &minusone, &vr0);
3439 return;
3442 /* Now canonicalize anti-ranges to ranges when they are not symbolic
3443 and express op ~[] as (op []') U (op []''). */
3444 if (vr0.type == VR_ANTI_RANGE
3445 && ranges_from_anti_range (&vr0, &vrtem0, &vrtem1))
3447 extract_range_from_unary_expr_1 (vr, code, type, &vrtem0, op0_type);
3448 if (vrtem1.type != VR_UNDEFINED)
3450 value_range_t vrres = VR_INITIALIZER;
3451 extract_range_from_unary_expr_1 (&vrres, code, type,
3452 &vrtem1, op0_type);
3453 vrp_meet (vr, &vrres);
3455 return;
3458 if (CONVERT_EXPR_CODE_P (code))
3460 tree inner_type = op0_type;
3461 tree outer_type = type;
3463 /* If the expression evaluates to a pointer, we are only interested in
3464 determining if it evaluates to NULL [0, 0] or non-NULL (~[0, 0]). */
3465 if (POINTER_TYPE_P (type))
3467 if (range_is_nonnull (&vr0))
3468 set_value_range_to_nonnull (vr, type);
3469 else if (range_is_null (&vr0))
3470 set_value_range_to_null (vr, type);
3471 else
3472 set_value_range_to_varying (vr);
3473 return;
3476 /* If VR0 is varying and we increase the type precision, assume
3477 a full range for the following transformation. */
3478 if (vr0.type == VR_VARYING
3479 && INTEGRAL_TYPE_P (inner_type)
3480 && TYPE_PRECISION (inner_type) < TYPE_PRECISION (outer_type))
3482 vr0.type = VR_RANGE;
3483 vr0.min = TYPE_MIN_VALUE (inner_type);
3484 vr0.max = TYPE_MAX_VALUE (inner_type);
3487 /* If VR0 is a constant range or anti-range and the conversion is
3488 not truncating we can convert the min and max values and
3489 canonicalize the resulting range. Otherwise we can do the
3490 conversion if the size of the range is less than what the
3491 precision of the target type can represent and the range is
3492 not an anti-range. */
3493 if ((vr0.type == VR_RANGE
3494 || vr0.type == VR_ANTI_RANGE)
3495 && TREE_CODE (vr0.min) == INTEGER_CST
3496 && TREE_CODE (vr0.max) == INTEGER_CST
3497 && (!is_overflow_infinity (vr0.min)
3498 || (vr0.type == VR_RANGE
3499 && TYPE_PRECISION (outer_type) > TYPE_PRECISION (inner_type)
3500 && needs_overflow_infinity (outer_type)
3501 && supports_overflow_infinity (outer_type)))
3502 && (!is_overflow_infinity (vr0.max)
3503 || (vr0.type == VR_RANGE
3504 && TYPE_PRECISION (outer_type) > TYPE_PRECISION (inner_type)
3505 && needs_overflow_infinity (outer_type)
3506 && supports_overflow_infinity (outer_type)))
3507 && (TYPE_PRECISION (outer_type) >= TYPE_PRECISION (inner_type)
3508 || (vr0.type == VR_RANGE
3509 && integer_zerop (int_const_binop (RSHIFT_EXPR,
3510 int_const_binop (MINUS_EXPR, vr0.max, vr0.min),
3511 size_int (TYPE_PRECISION (outer_type)))))))
3513 tree new_min, new_max;
3514 if (is_overflow_infinity (vr0.min))
3515 new_min = negative_overflow_infinity (outer_type);
3516 else
3517 new_min = force_fit_type (outer_type, wi::to_widest (vr0.min),
3518 0, false);
3519 if (is_overflow_infinity (vr0.max))
3520 new_max = positive_overflow_infinity (outer_type);
3521 else
3522 new_max = force_fit_type (outer_type, wi::to_widest (vr0.max),
3523 0, false);
3524 set_and_canonicalize_value_range (vr, vr0.type,
3525 new_min, new_max, NULL);
3526 return;
3529 set_value_range_to_varying (vr);
3530 return;
3532 else if (code == ABS_EXPR)
3534 tree min, max;
3535 int cmp;
3537 /* Pass through vr0 in the easy cases. */
3538 if (TYPE_UNSIGNED (type)
3539 || value_range_nonnegative_p (&vr0))
3541 copy_value_range (vr, &vr0);
3542 return;
3545 /* For the remaining varying or symbolic ranges we can't do anything
3546 useful. */
3547 if (vr0.type == VR_VARYING
3548 || symbolic_range_p (&vr0))
3550 set_value_range_to_varying (vr);
3551 return;
3554 /* -TYPE_MIN_VALUE = TYPE_MIN_VALUE with flag_wrapv so we can't get a
3555 useful range. */
3556 if (!TYPE_OVERFLOW_UNDEFINED (type)
3557 && ((vr0.type == VR_RANGE
3558 && vrp_val_is_min (vr0.min))
3559 || (vr0.type == VR_ANTI_RANGE
3560 && !vrp_val_is_min (vr0.min))))
3562 set_value_range_to_varying (vr);
3563 return;
3566 /* ABS_EXPR may flip the range around, if the original range
3567 included negative values. */
3568 if (is_overflow_infinity (vr0.min))
3569 min = positive_overflow_infinity (type);
3570 else if (!vrp_val_is_min (vr0.min))
3571 min = fold_unary_to_constant (code, type, vr0.min);
3572 else if (!needs_overflow_infinity (type))
3573 min = TYPE_MAX_VALUE (type);
3574 else if (supports_overflow_infinity (type))
3575 min = positive_overflow_infinity (type);
3576 else
3578 set_value_range_to_varying (vr);
3579 return;
3582 if (is_overflow_infinity (vr0.max))
3583 max = positive_overflow_infinity (type);
3584 else if (!vrp_val_is_min (vr0.max))
3585 max = fold_unary_to_constant (code, type, vr0.max);
3586 else if (!needs_overflow_infinity (type))
3587 max = TYPE_MAX_VALUE (type);
3588 else if (supports_overflow_infinity (type)
3589 /* We shouldn't generate [+INF, +INF] as set_value_range
3590 doesn't like this and ICEs. */
3591 && !is_positive_overflow_infinity (min))
3592 max = positive_overflow_infinity (type);
3593 else
3595 set_value_range_to_varying (vr);
3596 return;
3599 cmp = compare_values (min, max);
3601 /* If a VR_ANTI_RANGEs contains zero, then we have
3602 ~[-INF, min(MIN, MAX)]. */
3603 if (vr0.type == VR_ANTI_RANGE)
3605 if (range_includes_zero_p (vr0.min, vr0.max) == 1)
3607 /* Take the lower of the two values. */
3608 if (cmp != 1)
3609 max = min;
3611 /* Create ~[-INF, min (abs(MIN), abs(MAX))]
3612 or ~[-INF + 1, min (abs(MIN), abs(MAX))] when
3613 flag_wrapv is set and the original anti-range doesn't include
3614 TYPE_MIN_VALUE, remember -TYPE_MIN_VALUE = TYPE_MIN_VALUE. */
3615 if (TYPE_OVERFLOW_WRAPS (type))
3617 tree type_min_value = TYPE_MIN_VALUE (type);
3619 min = (vr0.min != type_min_value
3620 ? int_const_binop (PLUS_EXPR, type_min_value,
3621 build_int_cst (TREE_TYPE (type_min_value), 1))
3622 : type_min_value);
3624 else
3626 if (overflow_infinity_range_p (&vr0))
3627 min = negative_overflow_infinity (type);
3628 else
3629 min = TYPE_MIN_VALUE (type);
3632 else
3634 /* All else has failed, so create the range [0, INF], even for
3635 flag_wrapv since TYPE_MIN_VALUE is in the original
3636 anti-range. */
3637 vr0.type = VR_RANGE;
3638 min = build_int_cst (type, 0);
3639 if (needs_overflow_infinity (type))
3641 if (supports_overflow_infinity (type))
3642 max = positive_overflow_infinity (type);
3643 else
3645 set_value_range_to_varying (vr);
3646 return;
3649 else
3650 max = TYPE_MAX_VALUE (type);
3654 /* If the range contains zero then we know that the minimum value in the
3655 range will be zero. */
3656 else if (range_includes_zero_p (vr0.min, vr0.max) == 1)
3658 if (cmp == 1)
3659 max = min;
3660 min = build_int_cst (type, 0);
3662 else
3664 /* If the range was reversed, swap MIN and MAX. */
3665 if (cmp == 1)
3667 tree t = min;
3668 min = max;
3669 max = t;
3673 cmp = compare_values (min, max);
3674 if (cmp == -2 || cmp == 1)
3676 /* If the new range has its limits swapped around (MIN > MAX),
3677 then the operation caused one of them to wrap around, mark
3678 the new range VARYING. */
3679 set_value_range_to_varying (vr);
3681 else
3682 set_value_range (vr, vr0.type, min, max, NULL);
3683 return;
3686 /* For unhandled operations fall back to varying. */
3687 set_value_range_to_varying (vr);
3688 return;
3692 /* Extract range information from a unary expression CODE OP0 based on
3693 the range of its operand with resulting type TYPE.
3694 The resulting range is stored in *VR. */
3696 static void
3697 extract_range_from_unary_expr (value_range_t *vr, enum tree_code code,
3698 tree type, tree op0)
3700 value_range_t vr0 = VR_INITIALIZER;
3702 /* Get value ranges for the operand. For constant operands, create
3703 a new value range with the operand to simplify processing. */
3704 if (TREE_CODE (op0) == SSA_NAME)
3705 vr0 = *(get_value_range (op0));
3706 else if (is_gimple_min_invariant (op0))
3707 set_value_range_to_value (&vr0, op0, NULL);
3708 else
3709 set_value_range_to_varying (&vr0);
3711 extract_range_from_unary_expr_1 (vr, code, type, &vr0, TREE_TYPE (op0));
3715 /* Extract range information from a conditional expression STMT based on
3716 the ranges of each of its operands and the expression code. */
3718 static void
3719 extract_range_from_cond_expr (value_range_t *vr, gassign *stmt)
3721 tree op0, op1;
3722 value_range_t vr0 = VR_INITIALIZER;
3723 value_range_t vr1 = VR_INITIALIZER;
3725 /* Get value ranges for each operand. For constant operands, create
3726 a new value range with the operand to simplify processing. */
3727 op0 = gimple_assign_rhs2 (stmt);
3728 if (TREE_CODE (op0) == SSA_NAME)
3729 vr0 = *(get_value_range (op0));
3730 else if (is_gimple_min_invariant (op0))
3731 set_value_range_to_value (&vr0, op0, NULL);
3732 else
3733 set_value_range_to_varying (&vr0);
3735 op1 = gimple_assign_rhs3 (stmt);
3736 if (TREE_CODE (op1) == SSA_NAME)
3737 vr1 = *(get_value_range (op1));
3738 else if (is_gimple_min_invariant (op1))
3739 set_value_range_to_value (&vr1, op1, NULL);
3740 else
3741 set_value_range_to_varying (&vr1);
3743 /* The resulting value range is the union of the operand ranges */
3744 copy_value_range (vr, &vr0);
3745 vrp_meet (vr, &vr1);
3749 /* Extract range information from a comparison expression EXPR based
3750 on the range of its operand and the expression code. */
3752 static void
3753 extract_range_from_comparison (value_range_t *vr, enum tree_code code,
3754 tree type, tree op0, tree op1)
3756 bool sop = false;
3757 tree val;
3759 val = vrp_evaluate_conditional_warnv_with_ops (code, op0, op1, false, &sop,
3760 NULL);
3762 /* A disadvantage of using a special infinity as an overflow
3763 representation is that we lose the ability to record overflow
3764 when we don't have an infinity. So we have to ignore a result
3765 which relies on overflow. */
3767 if (val && !is_overflow_infinity (val) && !sop)
3769 /* Since this expression was found on the RHS of an assignment,
3770 its type may be different from _Bool. Convert VAL to EXPR's
3771 type. */
3772 val = fold_convert (type, val);
3773 if (is_gimple_min_invariant (val))
3774 set_value_range_to_value (vr, val, vr->equiv);
3775 else
3776 set_value_range (vr, VR_RANGE, val, val, vr->equiv);
3778 else
3779 /* The result of a comparison is always true or false. */
3780 set_value_range_to_truthvalue (vr, type);
3783 /* Helper function for simplify_internal_call_using_ranges and
3784 extract_range_basic. Return true if OP0 SUBCODE OP1 for
3785 SUBCODE {PLUS,MINUS,MULT}_EXPR is known to never overflow or
3786 always overflow. Set *OVF to true if it is known to always
3787 overflow. */
3789 static bool
3790 check_for_binary_op_overflow (enum tree_code subcode, tree type,
3791 tree op0, tree op1, bool *ovf)
3793 value_range_t vr0 = VR_INITIALIZER;
3794 value_range_t vr1 = VR_INITIALIZER;
3795 if (TREE_CODE (op0) == SSA_NAME)
3796 vr0 = *get_value_range (op0);
3797 else if (TREE_CODE (op0) == INTEGER_CST)
3798 set_value_range_to_value (&vr0, op0, NULL);
3799 else
3800 set_value_range_to_varying (&vr0);
3802 if (TREE_CODE (op1) == SSA_NAME)
3803 vr1 = *get_value_range (op1);
3804 else if (TREE_CODE (op1) == INTEGER_CST)
3805 set_value_range_to_value (&vr1, op1, NULL);
3806 else
3807 set_value_range_to_varying (&vr1);
3809 if (!range_int_cst_p (&vr0)
3810 || TREE_OVERFLOW (vr0.min)
3811 || TREE_OVERFLOW (vr0.max))
3813 vr0.min = vrp_val_min (TREE_TYPE (op0));
3814 vr0.max = vrp_val_max (TREE_TYPE (op0));
3816 if (!range_int_cst_p (&vr1)
3817 || TREE_OVERFLOW (vr1.min)
3818 || TREE_OVERFLOW (vr1.max))
3820 vr1.min = vrp_val_min (TREE_TYPE (op1));
3821 vr1.max = vrp_val_max (TREE_TYPE (op1));
3823 *ovf = arith_overflowed_p (subcode, type, vr0.min,
3824 subcode == MINUS_EXPR ? vr1.max : vr1.min);
3825 if (arith_overflowed_p (subcode, type, vr0.max,
3826 subcode == MINUS_EXPR ? vr1.min : vr1.max) != *ovf)
3827 return false;
3828 if (subcode == MULT_EXPR)
3830 if (arith_overflowed_p (subcode, type, vr0.min, vr1.max) != *ovf
3831 || arith_overflowed_p (subcode, type, vr0.max, vr1.min) != *ovf)
3832 return false;
3834 if (*ovf)
3836 /* So far we found that there is an overflow on the boundaries.
3837 That doesn't prove that there is an overflow even for all values
3838 in between the boundaries. For that compute widest_int range
3839 of the result and see if it doesn't overlap the range of
3840 type. */
3841 widest_int wmin, wmax;
3842 widest_int w[4];
3843 int i;
3844 w[0] = wi::to_widest (vr0.min);
3845 w[1] = wi::to_widest (vr0.max);
3846 w[2] = wi::to_widest (vr1.min);
3847 w[3] = wi::to_widest (vr1.max);
3848 for (i = 0; i < 4; i++)
3850 widest_int wt;
3851 switch (subcode)
3853 case PLUS_EXPR:
3854 wt = wi::add (w[i & 1], w[2 + (i & 2) / 2]);
3855 break;
3856 case MINUS_EXPR:
3857 wt = wi::sub (w[i & 1], w[2 + (i & 2) / 2]);
3858 break;
3859 case MULT_EXPR:
3860 wt = wi::mul (w[i & 1], w[2 + (i & 2) / 2]);
3861 break;
3862 default:
3863 gcc_unreachable ();
3865 if (i == 0)
3867 wmin = wt;
3868 wmax = wt;
3870 else
3872 wmin = wi::smin (wmin, wt);
3873 wmax = wi::smax (wmax, wt);
3876 /* The result of op0 CODE op1 is known to be in range
3877 [wmin, wmax]. */
3878 widest_int wtmin = wi::to_widest (vrp_val_min (type));
3879 widest_int wtmax = wi::to_widest (vrp_val_max (type));
3880 /* If all values in [wmin, wmax] are smaller than
3881 [wtmin, wtmax] or all are larger than [wtmin, wtmax],
3882 the arithmetic operation will always overflow. */
3883 if (wi::lts_p (wmax, wtmin) || wi::gts_p (wmin, wtmax))
3884 return true;
3885 return false;
3887 return true;
3890 /* Try to derive a nonnegative or nonzero range out of STMT relying
3891 primarily on generic routines in fold in conjunction with range data.
3892 Store the result in *VR */
3894 static void
3895 extract_range_basic (value_range_t *vr, gimple stmt)
3897 bool sop = false;
3898 tree type = gimple_expr_type (stmt);
3900 if (gimple_call_builtin_p (stmt, BUILT_IN_NORMAL))
3902 tree fndecl = gimple_call_fndecl (stmt), arg;
3903 int mini, maxi, zerov = 0, prec;
3905 switch (DECL_FUNCTION_CODE (fndecl))
3907 case BUILT_IN_CONSTANT_P:
3908 /* If the call is __builtin_constant_p and the argument is a
3909 function parameter resolve it to false. This avoids bogus
3910 array bound warnings.
3911 ??? We could do this as early as inlining is finished. */
3912 arg = gimple_call_arg (stmt, 0);
3913 if (TREE_CODE (arg) == SSA_NAME
3914 && SSA_NAME_IS_DEFAULT_DEF (arg)
3915 && TREE_CODE (SSA_NAME_VAR (arg)) == PARM_DECL)
3917 set_value_range_to_null (vr, type);
3918 return;
3920 break;
3921 /* Both __builtin_ffs* and __builtin_popcount return
3922 [0, prec]. */
3923 CASE_INT_FN (BUILT_IN_FFS):
3924 CASE_INT_FN (BUILT_IN_POPCOUNT):
3925 arg = gimple_call_arg (stmt, 0);
3926 prec = TYPE_PRECISION (TREE_TYPE (arg));
3927 mini = 0;
3928 maxi = prec;
3929 if (TREE_CODE (arg) == SSA_NAME)
3931 value_range_t *vr0 = get_value_range (arg);
3932 /* If arg is non-zero, then ffs or popcount
3933 are non-zero. */
3934 if (((vr0->type == VR_RANGE
3935 && range_includes_zero_p (vr0->min, vr0->max) == 0)
3936 || (vr0->type == VR_ANTI_RANGE
3937 && range_includes_zero_p (vr0->min, vr0->max) == 1))
3938 && !is_overflow_infinity (vr0->min)
3939 && !is_overflow_infinity (vr0->max))
3940 mini = 1;
3941 /* If some high bits are known to be zero,
3942 we can decrease the maximum. */
3943 if (vr0->type == VR_RANGE
3944 && TREE_CODE (vr0->max) == INTEGER_CST
3945 && !operand_less_p (vr0->min,
3946 build_zero_cst (TREE_TYPE (vr0->min)))
3947 && !is_overflow_infinity (vr0->max))
3948 maxi = tree_floor_log2 (vr0->max) + 1;
3950 goto bitop_builtin;
3951 /* __builtin_parity* returns [0, 1]. */
3952 CASE_INT_FN (BUILT_IN_PARITY):
3953 mini = 0;
3954 maxi = 1;
3955 goto bitop_builtin;
3956 /* __builtin_c[lt]z* return [0, prec-1], except for
3957 when the argument is 0, but that is undefined behavior.
3958 On many targets where the CLZ RTL or optab value is defined
3959 for 0 the value is prec, so include that in the range
3960 by default. */
3961 CASE_INT_FN (BUILT_IN_CLZ):
3962 arg = gimple_call_arg (stmt, 0);
3963 prec = TYPE_PRECISION (TREE_TYPE (arg));
3964 mini = 0;
3965 maxi = prec;
3966 if (optab_handler (clz_optab, TYPE_MODE (TREE_TYPE (arg)))
3967 != CODE_FOR_nothing
3968 && CLZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (TREE_TYPE (arg)),
3969 zerov)
3970 /* Handle only the single common value. */
3971 && zerov != prec)
3972 /* Magic value to give up, unless vr0 proves
3973 arg is non-zero. */
3974 mini = -2;
3975 if (TREE_CODE (arg) == SSA_NAME)
3977 value_range_t *vr0 = get_value_range (arg);
3978 /* From clz of VR_RANGE minimum we can compute
3979 result maximum. */
3980 if (vr0->type == VR_RANGE
3981 && TREE_CODE (vr0->min) == INTEGER_CST
3982 && !is_overflow_infinity (vr0->min))
3984 maxi = prec - 1 - tree_floor_log2 (vr0->min);
3985 if (maxi != prec)
3986 mini = 0;
3988 else if (vr0->type == VR_ANTI_RANGE
3989 && integer_zerop (vr0->min)
3990 && !is_overflow_infinity (vr0->min))
3992 maxi = prec - 1;
3993 mini = 0;
3995 if (mini == -2)
3996 break;
3997 /* From clz of VR_RANGE maximum we can compute
3998 result minimum. */
3999 if (vr0->type == VR_RANGE
4000 && TREE_CODE (vr0->max) == INTEGER_CST
4001 && !is_overflow_infinity (vr0->max))
4003 mini = prec - 1 - tree_floor_log2 (vr0->max);
4004 if (mini == prec)
4005 break;
4008 if (mini == -2)
4009 break;
4010 goto bitop_builtin;
4011 /* __builtin_ctz* return [0, prec-1], except for
4012 when the argument is 0, but that is undefined behavior.
4013 If there is a ctz optab for this mode and
4014 CTZ_DEFINED_VALUE_AT_ZERO, include that in the range,
4015 otherwise just assume 0 won't be seen. */
4016 CASE_INT_FN (BUILT_IN_CTZ):
4017 arg = gimple_call_arg (stmt, 0);
4018 prec = TYPE_PRECISION (TREE_TYPE (arg));
4019 mini = 0;
4020 maxi = prec - 1;
4021 if (optab_handler (ctz_optab, TYPE_MODE (TREE_TYPE (arg)))
4022 != CODE_FOR_nothing
4023 && CTZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (TREE_TYPE (arg)),
4024 zerov))
4026 /* Handle only the two common values. */
4027 if (zerov == -1)
4028 mini = -1;
4029 else if (zerov == prec)
4030 maxi = prec;
4031 else
4032 /* Magic value to give up, unless vr0 proves
4033 arg is non-zero. */
4034 mini = -2;
4036 if (TREE_CODE (arg) == SSA_NAME)
4038 value_range_t *vr0 = get_value_range (arg);
4039 /* If arg is non-zero, then use [0, prec - 1]. */
4040 if (((vr0->type == VR_RANGE
4041 && integer_nonzerop (vr0->min))
4042 || (vr0->type == VR_ANTI_RANGE
4043 && integer_zerop (vr0->min)))
4044 && !is_overflow_infinity (vr0->min))
4046 mini = 0;
4047 maxi = prec - 1;
4049 /* If some high bits are known to be zero,
4050 we can decrease the result maximum. */
4051 if (vr0->type == VR_RANGE
4052 && TREE_CODE (vr0->max) == INTEGER_CST
4053 && !is_overflow_infinity (vr0->max))
4055 maxi = tree_floor_log2 (vr0->max);
4056 /* For vr0 [0, 0] give up. */
4057 if (maxi == -1)
4058 break;
4061 if (mini == -2)
4062 break;
4063 goto bitop_builtin;
4064 /* __builtin_clrsb* returns [0, prec-1]. */
4065 CASE_INT_FN (BUILT_IN_CLRSB):
4066 arg = gimple_call_arg (stmt, 0);
4067 prec = TYPE_PRECISION (TREE_TYPE (arg));
4068 mini = 0;
4069 maxi = prec - 1;
4070 goto bitop_builtin;
4071 bitop_builtin:
4072 set_value_range (vr, VR_RANGE, build_int_cst (type, mini),
4073 build_int_cst (type, maxi), NULL);
4074 return;
4075 default:
4076 break;
4079 else if (is_gimple_call (stmt) && gimple_call_internal_p (stmt))
4081 enum tree_code subcode = ERROR_MARK;
4082 switch (gimple_call_internal_fn (stmt))
4084 case IFN_UBSAN_CHECK_ADD:
4085 subcode = PLUS_EXPR;
4086 break;
4087 case IFN_UBSAN_CHECK_SUB:
4088 subcode = MINUS_EXPR;
4089 break;
4090 case IFN_UBSAN_CHECK_MUL:
4091 subcode = MULT_EXPR;
4092 break;
4093 default:
4094 break;
4096 if (subcode != ERROR_MARK)
4098 bool saved_flag_wrapv = flag_wrapv;
4099 /* Pretend the arithmetics is wrapping. If there is
4100 any overflow, we'll complain, but will actually do
4101 wrapping operation. */
4102 flag_wrapv = 1;
4103 extract_range_from_binary_expr (vr, subcode, type,
4104 gimple_call_arg (stmt, 0),
4105 gimple_call_arg (stmt, 1));
4106 flag_wrapv = saved_flag_wrapv;
4108 /* If for both arguments vrp_valueize returned non-NULL,
4109 this should have been already folded and if not, it
4110 wasn't folded because of overflow. Avoid removing the
4111 UBSAN_CHECK_* calls in that case. */
4112 if (vr->type == VR_RANGE
4113 && (vr->min == vr->max
4114 || operand_equal_p (vr->min, vr->max, 0)))
4115 set_value_range_to_varying (vr);
4116 return;
4119 /* Handle extraction of the two results (result of arithmetics and
4120 a flag whether arithmetics overflowed) from {ADD,SUB,MUL}_OVERFLOW
4121 internal function. */
4122 else if (is_gimple_assign (stmt)
4123 && (gimple_assign_rhs_code (stmt) == REALPART_EXPR
4124 || gimple_assign_rhs_code (stmt) == IMAGPART_EXPR)
4125 && INTEGRAL_TYPE_P (type))
4127 enum tree_code code = gimple_assign_rhs_code (stmt);
4128 tree op = gimple_assign_rhs1 (stmt);
4129 if (TREE_CODE (op) == code && TREE_CODE (TREE_OPERAND (op, 0)) == SSA_NAME)
4131 gimple g = SSA_NAME_DEF_STMT (TREE_OPERAND (op, 0));
4132 if (is_gimple_call (g) && gimple_call_internal_p (g))
4134 enum tree_code subcode = ERROR_MARK;
4135 switch (gimple_call_internal_fn (g))
4137 case IFN_ADD_OVERFLOW:
4138 subcode = PLUS_EXPR;
4139 break;
4140 case IFN_SUB_OVERFLOW:
4141 subcode = MINUS_EXPR;
4142 break;
4143 case IFN_MUL_OVERFLOW:
4144 subcode = MULT_EXPR;
4145 break;
4146 default:
4147 break;
4149 if (subcode != ERROR_MARK)
4151 tree op0 = gimple_call_arg (g, 0);
4152 tree op1 = gimple_call_arg (g, 1);
4153 if (code == IMAGPART_EXPR)
4155 bool ovf = false;
4156 if (check_for_binary_op_overflow (subcode, type,
4157 op0, op1, &ovf))
4158 set_value_range_to_value (vr,
4159 build_int_cst (type, ovf),
4160 NULL);
4161 else
4162 set_value_range (vr, VR_RANGE, build_int_cst (type, 0),
4163 build_int_cst (type, 1), NULL);
4165 else if (types_compatible_p (type, TREE_TYPE (op0))
4166 && types_compatible_p (type, TREE_TYPE (op1)))
4168 bool saved_flag_wrapv = flag_wrapv;
4169 /* Pretend the arithmetics is wrapping. If there is
4170 any overflow, IMAGPART_EXPR will be set. */
4171 flag_wrapv = 1;
4172 extract_range_from_binary_expr (vr, subcode, type,
4173 op0, op1);
4174 flag_wrapv = saved_flag_wrapv;
4176 else
4178 value_range_t vr0 = VR_INITIALIZER;
4179 value_range_t vr1 = VR_INITIALIZER;
4180 bool saved_flag_wrapv = flag_wrapv;
4181 /* Pretend the arithmetics is wrapping. If there is
4182 any overflow, IMAGPART_EXPR will be set. */
4183 flag_wrapv = 1;
4184 extract_range_from_unary_expr (&vr0, NOP_EXPR,
4185 type, op0);
4186 extract_range_from_unary_expr (&vr1, NOP_EXPR,
4187 type, op1);
4188 extract_range_from_binary_expr_1 (vr, subcode, type,
4189 &vr0, &vr1);
4190 flag_wrapv = saved_flag_wrapv;
4192 return;
4197 if (INTEGRAL_TYPE_P (type)
4198 && gimple_stmt_nonnegative_warnv_p (stmt, &sop))
4199 set_value_range_to_nonnegative (vr, type,
4200 sop || stmt_overflow_infinity (stmt));
4201 else if (vrp_stmt_computes_nonzero (stmt, &sop)
4202 && !sop)
4203 set_value_range_to_nonnull (vr, type);
4204 else
4205 set_value_range_to_varying (vr);
4209 /* Try to compute a useful range out of assignment STMT and store it
4210 in *VR. */
4212 static void
4213 extract_range_from_assignment (value_range_t *vr, gassign *stmt)
4215 enum tree_code code = gimple_assign_rhs_code (stmt);
4217 if (code == ASSERT_EXPR)
4218 extract_range_from_assert (vr, gimple_assign_rhs1 (stmt));
4219 else if (code == SSA_NAME)
4220 extract_range_from_ssa_name (vr, gimple_assign_rhs1 (stmt));
4221 else if (TREE_CODE_CLASS (code) == tcc_binary)
4222 extract_range_from_binary_expr (vr, gimple_assign_rhs_code (stmt),
4223 gimple_expr_type (stmt),
4224 gimple_assign_rhs1 (stmt),
4225 gimple_assign_rhs2 (stmt));
4226 else if (TREE_CODE_CLASS (code) == tcc_unary)
4227 extract_range_from_unary_expr (vr, gimple_assign_rhs_code (stmt),
4228 gimple_expr_type (stmt),
4229 gimple_assign_rhs1 (stmt));
4230 else if (code == COND_EXPR)
4231 extract_range_from_cond_expr (vr, stmt);
4232 else if (TREE_CODE_CLASS (code) == tcc_comparison)
4233 extract_range_from_comparison (vr, gimple_assign_rhs_code (stmt),
4234 gimple_expr_type (stmt),
4235 gimple_assign_rhs1 (stmt),
4236 gimple_assign_rhs2 (stmt));
4237 else if (get_gimple_rhs_class (code) == GIMPLE_SINGLE_RHS
4238 && is_gimple_min_invariant (gimple_assign_rhs1 (stmt)))
4239 set_value_range_to_value (vr, gimple_assign_rhs1 (stmt), NULL);
4240 else
4241 set_value_range_to_varying (vr);
4243 if (vr->type == VR_VARYING)
4244 extract_range_basic (vr, stmt);
4247 /* Given a range VR, a LOOP and a variable VAR, determine whether it
4248 would be profitable to adjust VR using scalar evolution information
4249 for VAR. If so, update VR with the new limits. */
4251 static void
4252 adjust_range_with_scev (value_range_t *vr, struct loop *loop,
4253 gimple stmt, tree var)
4255 tree init, step, chrec, tmin, tmax, min, max, type, tem;
4256 enum ev_direction dir;
4258 /* TODO. Don't adjust anti-ranges. An anti-range may provide
4259 better opportunities than a regular range, but I'm not sure. */
4260 if (vr->type == VR_ANTI_RANGE)
4261 return;
4263 chrec = instantiate_parameters (loop, analyze_scalar_evolution (loop, var));
4265 /* Like in PR19590, scev can return a constant function. */
4266 if (is_gimple_min_invariant (chrec))
4268 set_value_range_to_value (vr, chrec, vr->equiv);
4269 return;
4272 if (TREE_CODE (chrec) != POLYNOMIAL_CHREC)
4273 return;
4275 init = initial_condition_in_loop_num (chrec, loop->num);
4276 tem = op_with_constant_singleton_value_range (init);
4277 if (tem)
4278 init = tem;
4279 step = evolution_part_in_loop_num (chrec, loop->num);
4280 tem = op_with_constant_singleton_value_range (step);
4281 if (tem)
4282 step = tem;
4284 /* If STEP is symbolic, we can't know whether INIT will be the
4285 minimum or maximum value in the range. Also, unless INIT is
4286 a simple expression, compare_values and possibly other functions
4287 in tree-vrp won't be able to handle it. */
4288 if (step == NULL_TREE
4289 || !is_gimple_min_invariant (step)
4290 || !valid_value_p (init))
4291 return;
4293 dir = scev_direction (chrec);
4294 if (/* Do not adjust ranges if we do not know whether the iv increases
4295 or decreases, ... */
4296 dir == EV_DIR_UNKNOWN
4297 /* ... or if it may wrap. */
4298 || scev_probably_wraps_p (init, step, stmt, get_chrec_loop (chrec),
4299 true))
4300 return;
4302 /* We use TYPE_MIN_VALUE and TYPE_MAX_VALUE here instead of
4303 negative_overflow_infinity and positive_overflow_infinity,
4304 because we have concluded that the loop probably does not
4305 wrap. */
4307 type = TREE_TYPE (var);
4308 if (POINTER_TYPE_P (type) || !TYPE_MIN_VALUE (type))
4309 tmin = lower_bound_in_type (type, type);
4310 else
4311 tmin = TYPE_MIN_VALUE (type);
4312 if (POINTER_TYPE_P (type) || !TYPE_MAX_VALUE (type))
4313 tmax = upper_bound_in_type (type, type);
4314 else
4315 tmax = TYPE_MAX_VALUE (type);
4317 /* Try to use estimated number of iterations for the loop to constrain the
4318 final value in the evolution. */
4319 if (TREE_CODE (step) == INTEGER_CST
4320 && is_gimple_val (init)
4321 && (TREE_CODE (init) != SSA_NAME
4322 || get_value_range (init)->type == VR_RANGE))
4324 widest_int nit;
4326 /* We are only entering here for loop header PHI nodes, so using
4327 the number of latch executions is the correct thing to use. */
4328 if (max_loop_iterations (loop, &nit))
4330 value_range_t maxvr = VR_INITIALIZER;
4331 signop sgn = TYPE_SIGN (TREE_TYPE (step));
4332 bool overflow;
4334 widest_int wtmp = wi::mul (wi::to_widest (step), nit, sgn,
4335 &overflow);
4336 /* If the multiplication overflowed we can't do a meaningful
4337 adjustment. Likewise if the result doesn't fit in the type
4338 of the induction variable. For a signed type we have to
4339 check whether the result has the expected signedness which
4340 is that of the step as number of iterations is unsigned. */
4341 if (!overflow
4342 && wi::fits_to_tree_p (wtmp, TREE_TYPE (init))
4343 && (sgn == UNSIGNED
4344 || wi::gts_p (wtmp, 0) == wi::gts_p (step, 0)))
4346 tem = wide_int_to_tree (TREE_TYPE (init), wtmp);
4347 extract_range_from_binary_expr (&maxvr, PLUS_EXPR,
4348 TREE_TYPE (init), init, tem);
4349 /* Likewise if the addition did. */
4350 if (maxvr.type == VR_RANGE)
4352 tmin = maxvr.min;
4353 tmax = maxvr.max;
4359 if (vr->type == VR_VARYING || vr->type == VR_UNDEFINED)
4361 min = tmin;
4362 max = tmax;
4364 /* For VARYING or UNDEFINED ranges, just about anything we get
4365 from scalar evolutions should be better. */
4367 if (dir == EV_DIR_DECREASES)
4368 max = init;
4369 else
4370 min = init;
4372 else if (vr->type == VR_RANGE)
4374 min = vr->min;
4375 max = vr->max;
4377 if (dir == EV_DIR_DECREASES)
4379 /* INIT is the maximum value. If INIT is lower than VR->MAX
4380 but no smaller than VR->MIN, set VR->MAX to INIT. */
4381 if (compare_values (init, max) == -1)
4382 max = init;
4384 /* According to the loop information, the variable does not
4385 overflow. If we think it does, probably because of an
4386 overflow due to arithmetic on a different INF value,
4387 reset now. */
4388 if (is_negative_overflow_infinity (min)
4389 || compare_values (min, tmin) == -1)
4390 min = tmin;
4393 else
4395 /* If INIT is bigger than VR->MIN, set VR->MIN to INIT. */
4396 if (compare_values (init, min) == 1)
4397 min = init;
4399 if (is_positive_overflow_infinity (max)
4400 || compare_values (tmax, max) == -1)
4401 max = tmax;
4404 else
4405 return;
4407 /* If we just created an invalid range with the minimum
4408 greater than the maximum, we fail conservatively.
4409 This should happen only in unreachable
4410 parts of code, or for invalid programs. */
4411 if (compare_values (min, max) == 1
4412 || (is_negative_overflow_infinity (min)
4413 && is_positive_overflow_infinity (max)))
4414 return;
4416 set_value_range (vr, VR_RANGE, min, max, vr->equiv);
4420 /* Given two numeric value ranges VR0, VR1 and a comparison code COMP:
4422 - Return BOOLEAN_TRUE_NODE if VR0 COMP VR1 always returns true for
4423 all the values in the ranges.
4425 - Return BOOLEAN_FALSE_NODE if the comparison always returns false.
4427 - Return NULL_TREE if it is not always possible to determine the
4428 value of the comparison.
4430 Also set *STRICT_OVERFLOW_P to indicate whether a range with an
4431 overflow infinity was used in the test. */
4434 static tree
4435 compare_ranges (enum tree_code comp, value_range_t *vr0, value_range_t *vr1,
4436 bool *strict_overflow_p)
4438 /* VARYING or UNDEFINED ranges cannot be compared. */
4439 if (vr0->type == VR_VARYING
4440 || vr0->type == VR_UNDEFINED
4441 || vr1->type == VR_VARYING
4442 || vr1->type == VR_UNDEFINED)
4443 return NULL_TREE;
4445 /* Anti-ranges need to be handled separately. */
4446 if (vr0->type == VR_ANTI_RANGE || vr1->type == VR_ANTI_RANGE)
4448 /* If both are anti-ranges, then we cannot compute any
4449 comparison. */
4450 if (vr0->type == VR_ANTI_RANGE && vr1->type == VR_ANTI_RANGE)
4451 return NULL_TREE;
4453 /* These comparisons are never statically computable. */
4454 if (comp == GT_EXPR
4455 || comp == GE_EXPR
4456 || comp == LT_EXPR
4457 || comp == LE_EXPR)
4458 return NULL_TREE;
4460 /* Equality can be computed only between a range and an
4461 anti-range. ~[VAL1, VAL2] == [VAL1, VAL2] is always false. */
4462 if (vr0->type == VR_RANGE)
4464 /* To simplify processing, make VR0 the anti-range. */
4465 value_range_t *tmp = vr0;
4466 vr0 = vr1;
4467 vr1 = tmp;
4470 gcc_assert (comp == NE_EXPR || comp == EQ_EXPR);
4472 if (compare_values_warnv (vr0->min, vr1->min, strict_overflow_p) == 0
4473 && compare_values_warnv (vr0->max, vr1->max, strict_overflow_p) == 0)
4474 return (comp == NE_EXPR) ? boolean_true_node : boolean_false_node;
4476 return NULL_TREE;
4479 if (!usable_range_p (vr0, strict_overflow_p)
4480 || !usable_range_p (vr1, strict_overflow_p))
4481 return NULL_TREE;
4483 /* Simplify processing. If COMP is GT_EXPR or GE_EXPR, switch the
4484 operands around and change the comparison code. */
4485 if (comp == GT_EXPR || comp == GE_EXPR)
4487 value_range_t *tmp;
4488 comp = (comp == GT_EXPR) ? LT_EXPR : LE_EXPR;
4489 tmp = vr0;
4490 vr0 = vr1;
4491 vr1 = tmp;
4494 if (comp == EQ_EXPR)
4496 /* Equality may only be computed if both ranges represent
4497 exactly one value. */
4498 if (compare_values_warnv (vr0->min, vr0->max, strict_overflow_p) == 0
4499 && compare_values_warnv (vr1->min, vr1->max, strict_overflow_p) == 0)
4501 int cmp_min = compare_values_warnv (vr0->min, vr1->min,
4502 strict_overflow_p);
4503 int cmp_max = compare_values_warnv (vr0->max, vr1->max,
4504 strict_overflow_p);
4505 if (cmp_min == 0 && cmp_max == 0)
4506 return boolean_true_node;
4507 else if (cmp_min != -2 && cmp_max != -2)
4508 return boolean_false_node;
4510 /* If [V0_MIN, V1_MAX] < [V1_MIN, V1_MAX] then V0 != V1. */
4511 else if (compare_values_warnv (vr0->min, vr1->max,
4512 strict_overflow_p) == 1
4513 || compare_values_warnv (vr1->min, vr0->max,
4514 strict_overflow_p) == 1)
4515 return boolean_false_node;
4517 return NULL_TREE;
4519 else if (comp == NE_EXPR)
4521 int cmp1, cmp2;
4523 /* If VR0 is completely to the left or completely to the right
4524 of VR1, they are always different. Notice that we need to
4525 make sure that both comparisons yield similar results to
4526 avoid comparing values that cannot be compared at
4527 compile-time. */
4528 cmp1 = compare_values_warnv (vr0->max, vr1->min, strict_overflow_p);
4529 cmp2 = compare_values_warnv (vr0->min, vr1->max, strict_overflow_p);
4530 if ((cmp1 == -1 && cmp2 == -1) || (cmp1 == 1 && cmp2 == 1))
4531 return boolean_true_node;
4533 /* If VR0 and VR1 represent a single value and are identical,
4534 return false. */
4535 else if (compare_values_warnv (vr0->min, vr0->max,
4536 strict_overflow_p) == 0
4537 && compare_values_warnv (vr1->min, vr1->max,
4538 strict_overflow_p) == 0
4539 && compare_values_warnv (vr0->min, vr1->min,
4540 strict_overflow_p) == 0
4541 && compare_values_warnv (vr0->max, vr1->max,
4542 strict_overflow_p) == 0)
4543 return boolean_false_node;
4545 /* Otherwise, they may or may not be different. */
4546 else
4547 return NULL_TREE;
4549 else if (comp == LT_EXPR || comp == LE_EXPR)
4551 int tst;
4553 /* If VR0 is to the left of VR1, return true. */
4554 tst = compare_values_warnv (vr0->max, vr1->min, strict_overflow_p);
4555 if ((comp == LT_EXPR && tst == -1)
4556 || (comp == LE_EXPR && (tst == -1 || tst == 0)))
4558 if (overflow_infinity_range_p (vr0)
4559 || overflow_infinity_range_p (vr1))
4560 *strict_overflow_p = true;
4561 return boolean_true_node;
4564 /* If VR0 is to the right of VR1, return false. */
4565 tst = compare_values_warnv (vr0->min, vr1->max, strict_overflow_p);
4566 if ((comp == LT_EXPR && (tst == 0 || tst == 1))
4567 || (comp == LE_EXPR && tst == 1))
4569 if (overflow_infinity_range_p (vr0)
4570 || overflow_infinity_range_p (vr1))
4571 *strict_overflow_p = true;
4572 return boolean_false_node;
4575 /* Otherwise, we don't know. */
4576 return NULL_TREE;
4579 gcc_unreachable ();
4583 /* Given a value range VR, a value VAL and a comparison code COMP, return
4584 BOOLEAN_TRUE_NODE if VR COMP VAL always returns true for all the
4585 values in VR. Return BOOLEAN_FALSE_NODE if the comparison
4586 always returns false. Return NULL_TREE if it is not always
4587 possible to determine the value of the comparison. Also set
4588 *STRICT_OVERFLOW_P to indicate whether a range with an overflow
4589 infinity was used in the test. */
4591 static tree
4592 compare_range_with_value (enum tree_code comp, value_range_t *vr, tree val,
4593 bool *strict_overflow_p)
4595 if (vr->type == VR_VARYING || vr->type == VR_UNDEFINED)
4596 return NULL_TREE;
4598 /* Anti-ranges need to be handled separately. */
4599 if (vr->type == VR_ANTI_RANGE)
4601 /* For anti-ranges, the only predicates that we can compute at
4602 compile time are equality and inequality. */
4603 if (comp == GT_EXPR
4604 || comp == GE_EXPR
4605 || comp == LT_EXPR
4606 || comp == LE_EXPR)
4607 return NULL_TREE;
4609 /* ~[VAL_1, VAL_2] OP VAL is known if VAL_1 <= VAL <= VAL_2. */
4610 if (value_inside_range (val, vr->min, vr->max) == 1)
4611 return (comp == NE_EXPR) ? boolean_true_node : boolean_false_node;
4613 return NULL_TREE;
4616 if (!usable_range_p (vr, strict_overflow_p))
4617 return NULL_TREE;
4619 if (comp == EQ_EXPR)
4621 /* EQ_EXPR may only be computed if VR represents exactly
4622 one value. */
4623 if (compare_values_warnv (vr->min, vr->max, strict_overflow_p) == 0)
4625 int cmp = compare_values_warnv (vr->min, val, strict_overflow_p);
4626 if (cmp == 0)
4627 return boolean_true_node;
4628 else if (cmp == -1 || cmp == 1 || cmp == 2)
4629 return boolean_false_node;
4631 else if (compare_values_warnv (val, vr->min, strict_overflow_p) == -1
4632 || compare_values_warnv (vr->max, val, strict_overflow_p) == -1)
4633 return boolean_false_node;
4635 return NULL_TREE;
4637 else if (comp == NE_EXPR)
4639 /* If VAL is not inside VR, then they are always different. */
4640 if (compare_values_warnv (vr->max, val, strict_overflow_p) == -1
4641 || compare_values_warnv (vr->min, val, strict_overflow_p) == 1)
4642 return boolean_true_node;
4644 /* If VR represents exactly one value equal to VAL, then return
4645 false. */
4646 if (compare_values_warnv (vr->min, vr->max, strict_overflow_p) == 0
4647 && compare_values_warnv (vr->min, val, strict_overflow_p) == 0)
4648 return boolean_false_node;
4650 /* Otherwise, they may or may not be different. */
4651 return NULL_TREE;
4653 else if (comp == LT_EXPR || comp == LE_EXPR)
4655 int tst;
4657 /* If VR is to the left of VAL, return true. */
4658 tst = compare_values_warnv (vr->max, val, strict_overflow_p);
4659 if ((comp == LT_EXPR && tst == -1)
4660 || (comp == LE_EXPR && (tst == -1 || tst == 0)))
4662 if (overflow_infinity_range_p (vr))
4663 *strict_overflow_p = true;
4664 return boolean_true_node;
4667 /* If VR is to the right of VAL, return false. */
4668 tst = compare_values_warnv (vr->min, val, strict_overflow_p);
4669 if ((comp == LT_EXPR && (tst == 0 || tst == 1))
4670 || (comp == LE_EXPR && tst == 1))
4672 if (overflow_infinity_range_p (vr))
4673 *strict_overflow_p = true;
4674 return boolean_false_node;
4677 /* Otherwise, we don't know. */
4678 return NULL_TREE;
4680 else if (comp == GT_EXPR || comp == GE_EXPR)
4682 int tst;
4684 /* If VR is to the right of VAL, return true. */
4685 tst = compare_values_warnv (vr->min, val, strict_overflow_p);
4686 if ((comp == GT_EXPR && tst == 1)
4687 || (comp == GE_EXPR && (tst == 0 || tst == 1)))
4689 if (overflow_infinity_range_p (vr))
4690 *strict_overflow_p = true;
4691 return boolean_true_node;
4694 /* If VR is to the left of VAL, return false. */
4695 tst = compare_values_warnv (vr->max, val, strict_overflow_p);
4696 if ((comp == GT_EXPR && (tst == -1 || tst == 0))
4697 || (comp == GE_EXPR && tst == -1))
4699 if (overflow_infinity_range_p (vr))
4700 *strict_overflow_p = true;
4701 return boolean_false_node;
4704 /* Otherwise, we don't know. */
4705 return NULL_TREE;
4708 gcc_unreachable ();
4712 /* Debugging dumps. */
4714 void dump_value_range (FILE *, value_range_t *);
4715 void debug_value_range (value_range_t *);
4716 void dump_all_value_ranges (FILE *);
4717 void debug_all_value_ranges (void);
4718 void dump_vr_equiv (FILE *, bitmap);
4719 void debug_vr_equiv (bitmap);
4722 /* Dump value range VR to FILE. */
4724 void
4725 dump_value_range (FILE *file, value_range_t *vr)
4727 if (vr == NULL)
4728 fprintf (file, "[]");
4729 else if (vr->type == VR_UNDEFINED)
4730 fprintf (file, "UNDEFINED");
4731 else if (vr->type == VR_RANGE || vr->type == VR_ANTI_RANGE)
4733 tree type = TREE_TYPE (vr->min);
4735 fprintf (file, "%s[", (vr->type == VR_ANTI_RANGE) ? "~" : "");
4737 if (is_negative_overflow_infinity (vr->min))
4738 fprintf (file, "-INF(OVF)");
4739 else if (INTEGRAL_TYPE_P (type)
4740 && !TYPE_UNSIGNED (type)
4741 && vrp_val_is_min (vr->min))
4742 fprintf (file, "-INF");
4743 else
4744 print_generic_expr (file, vr->min, 0);
4746 fprintf (file, ", ");
4748 if (is_positive_overflow_infinity (vr->max))
4749 fprintf (file, "+INF(OVF)");
4750 else if (INTEGRAL_TYPE_P (type)
4751 && vrp_val_is_max (vr->max))
4752 fprintf (file, "+INF");
4753 else
4754 print_generic_expr (file, vr->max, 0);
4756 fprintf (file, "]");
4758 if (vr->equiv)
4760 bitmap_iterator bi;
4761 unsigned i, c = 0;
4763 fprintf (file, " EQUIVALENCES: { ");
4765 EXECUTE_IF_SET_IN_BITMAP (vr->equiv, 0, i, bi)
4767 print_generic_expr (file, ssa_name (i), 0);
4768 fprintf (file, " ");
4769 c++;
4772 fprintf (file, "} (%u elements)", c);
4775 else if (vr->type == VR_VARYING)
4776 fprintf (file, "VARYING");
4777 else
4778 fprintf (file, "INVALID RANGE");
4782 /* Dump value range VR to stderr. */
4784 DEBUG_FUNCTION void
4785 debug_value_range (value_range_t *vr)
4787 dump_value_range (stderr, vr);
4788 fprintf (stderr, "\n");
4792 /* Dump value ranges of all SSA_NAMEs to FILE. */
4794 void
4795 dump_all_value_ranges (FILE *file)
4797 size_t i;
4799 for (i = 0; i < num_vr_values; i++)
4801 if (vr_value[i])
4803 print_generic_expr (file, ssa_name (i), 0);
4804 fprintf (file, ": ");
4805 dump_value_range (file, vr_value[i]);
4806 fprintf (file, "\n");
4810 fprintf (file, "\n");
4814 /* Dump all value ranges to stderr. */
4816 DEBUG_FUNCTION void
4817 debug_all_value_ranges (void)
4819 dump_all_value_ranges (stderr);
4823 /* Given a COND_EXPR COND of the form 'V OP W', and an SSA name V,
4824 create a new SSA name N and return the assertion assignment
4825 'N = ASSERT_EXPR <V, V OP W>'. */
4827 static gimple
4828 build_assert_expr_for (tree cond, tree v)
4830 tree a;
4831 gassign *assertion;
4833 gcc_assert (TREE_CODE (v) == SSA_NAME
4834 && COMPARISON_CLASS_P (cond));
4836 a = build2 (ASSERT_EXPR, TREE_TYPE (v), v, cond);
4837 assertion = gimple_build_assign (NULL_TREE, a);
4839 /* The new ASSERT_EXPR, creates a new SSA name that replaces the
4840 operand of the ASSERT_EXPR. Create it so the new name and the old one
4841 are registered in the replacement table so that we can fix the SSA web
4842 after adding all the ASSERT_EXPRs. */
4843 create_new_def_for (v, assertion, NULL);
4845 return assertion;
4849 /* Return false if EXPR is a predicate expression involving floating
4850 point values. */
4852 static inline bool
4853 fp_predicate (gimple stmt)
4855 GIMPLE_CHECK (stmt, GIMPLE_COND);
4857 return FLOAT_TYPE_P (TREE_TYPE (gimple_cond_lhs (stmt)));
4860 /* If the range of values taken by OP can be inferred after STMT executes,
4861 return the comparison code (COMP_CODE_P) and value (VAL_P) that
4862 describes the inferred range. Return true if a range could be
4863 inferred. */
4865 static bool
4866 infer_value_range (gimple stmt, tree op, enum tree_code *comp_code_p, tree *val_p)
4868 *val_p = NULL_TREE;
4869 *comp_code_p = ERROR_MARK;
4871 /* Do not attempt to infer anything in names that flow through
4872 abnormal edges. */
4873 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op))
4874 return false;
4876 /* Similarly, don't infer anything from statements that may throw
4877 exceptions. ??? Relax this requirement? */
4878 if (stmt_could_throw_p (stmt))
4879 return false;
4881 /* If STMT is the last statement of a basic block with no normal
4882 successors, there is no point inferring anything about any of its
4883 operands. We would not be able to find a proper insertion point
4884 for the assertion, anyway. */
4885 if (stmt_ends_bb_p (stmt))
4887 edge_iterator ei;
4888 edge e;
4890 FOR_EACH_EDGE (e, ei, gimple_bb (stmt)->succs)
4891 if (!(e->flags & EDGE_ABNORMAL))
4892 break;
4893 if (e == NULL)
4894 return false;
4897 if (infer_nonnull_range (stmt, op, true, true))
4899 *val_p = build_int_cst (TREE_TYPE (op), 0);
4900 *comp_code_p = NE_EXPR;
4901 return true;
4904 return false;
4908 void dump_asserts_for (FILE *, tree);
4909 void debug_asserts_for (tree);
4910 void dump_all_asserts (FILE *);
4911 void debug_all_asserts (void);
4913 /* Dump all the registered assertions for NAME to FILE. */
4915 void
4916 dump_asserts_for (FILE *file, tree name)
4918 assert_locus_t loc;
4920 fprintf (file, "Assertions to be inserted for ");
4921 print_generic_expr (file, name, 0);
4922 fprintf (file, "\n");
4924 loc = asserts_for[SSA_NAME_VERSION (name)];
4925 while (loc)
4927 fprintf (file, "\t");
4928 print_gimple_stmt (file, gsi_stmt (loc->si), 0, 0);
4929 fprintf (file, "\n\tBB #%d", loc->bb->index);
4930 if (loc->e)
4932 fprintf (file, "\n\tEDGE %d->%d", loc->e->src->index,
4933 loc->e->dest->index);
4934 dump_edge_info (file, loc->e, dump_flags, 0);
4936 fprintf (file, "\n\tPREDICATE: ");
4937 print_generic_expr (file, name, 0);
4938 fprintf (file, " %s ", get_tree_code_name (loc->comp_code));
4939 print_generic_expr (file, loc->val, 0);
4940 fprintf (file, "\n\n");
4941 loc = loc->next;
4944 fprintf (file, "\n");
4948 /* Dump all the registered assertions for NAME to stderr. */
4950 DEBUG_FUNCTION void
4951 debug_asserts_for (tree name)
4953 dump_asserts_for (stderr, name);
4957 /* Dump all the registered assertions for all the names to FILE. */
4959 void
4960 dump_all_asserts (FILE *file)
4962 unsigned i;
4963 bitmap_iterator bi;
4965 fprintf (file, "\nASSERT_EXPRs to be inserted\n\n");
4966 EXECUTE_IF_SET_IN_BITMAP (need_assert_for, 0, i, bi)
4967 dump_asserts_for (file, ssa_name (i));
4968 fprintf (file, "\n");
4972 /* Dump all the registered assertions for all the names to stderr. */
4974 DEBUG_FUNCTION void
4975 debug_all_asserts (void)
4977 dump_all_asserts (stderr);
4981 /* If NAME doesn't have an ASSERT_EXPR registered for asserting
4982 'EXPR COMP_CODE VAL' at a location that dominates block BB or
4983 E->DEST, then register this location as a possible insertion point
4984 for ASSERT_EXPR <NAME, EXPR COMP_CODE VAL>.
4986 BB, E and SI provide the exact insertion point for the new
4987 ASSERT_EXPR. If BB is NULL, then the ASSERT_EXPR is to be inserted
4988 on edge E. Otherwise, if E is NULL, the ASSERT_EXPR is inserted on
4989 BB. If SI points to a COND_EXPR or a SWITCH_EXPR statement, then E
4990 must not be NULL. */
4992 static void
4993 register_new_assert_for (tree name, tree expr,
4994 enum tree_code comp_code,
4995 tree val,
4996 basic_block bb,
4997 edge e,
4998 gimple_stmt_iterator si)
5000 assert_locus_t n, loc, last_loc;
5001 basic_block dest_bb;
5003 gcc_checking_assert (bb == NULL || e == NULL);
5005 if (e == NULL)
5006 gcc_checking_assert (gimple_code (gsi_stmt (si)) != GIMPLE_COND
5007 && gimple_code (gsi_stmt (si)) != GIMPLE_SWITCH);
5009 /* Never build an assert comparing against an integer constant with
5010 TREE_OVERFLOW set. This confuses our undefined overflow warning
5011 machinery. */
5012 if (TREE_OVERFLOW_P (val))
5013 val = drop_tree_overflow (val);
5015 /* The new assertion A will be inserted at BB or E. We need to
5016 determine if the new location is dominated by a previously
5017 registered location for A. If we are doing an edge insertion,
5018 assume that A will be inserted at E->DEST. Note that this is not
5019 necessarily true.
5021 If E is a critical edge, it will be split. But even if E is
5022 split, the new block will dominate the same set of blocks that
5023 E->DEST dominates.
5025 The reverse, however, is not true, blocks dominated by E->DEST
5026 will not be dominated by the new block created to split E. So,
5027 if the insertion location is on a critical edge, we will not use
5028 the new location to move another assertion previously registered
5029 at a block dominated by E->DEST. */
5030 dest_bb = (bb) ? bb : e->dest;
5032 /* If NAME already has an ASSERT_EXPR registered for COMP_CODE and
5033 VAL at a block dominating DEST_BB, then we don't need to insert a new
5034 one. Similarly, if the same assertion already exists at a block
5035 dominated by DEST_BB and the new location is not on a critical
5036 edge, then update the existing location for the assertion (i.e.,
5037 move the assertion up in the dominance tree).
5039 Note, this is implemented as a simple linked list because there
5040 should not be more than a handful of assertions registered per
5041 name. If this becomes a performance problem, a table hashed by
5042 COMP_CODE and VAL could be implemented. */
5043 loc = asserts_for[SSA_NAME_VERSION (name)];
5044 last_loc = loc;
5045 while (loc)
5047 if (loc->comp_code == comp_code
5048 && (loc->val == val
5049 || operand_equal_p (loc->val, val, 0))
5050 && (loc->expr == expr
5051 || operand_equal_p (loc->expr, expr, 0)))
5053 /* If E is not a critical edge and DEST_BB
5054 dominates the existing location for the assertion, move
5055 the assertion up in the dominance tree by updating its
5056 location information. */
5057 if ((e == NULL || !EDGE_CRITICAL_P (e))
5058 && dominated_by_p (CDI_DOMINATORS, loc->bb, dest_bb))
5060 loc->bb = dest_bb;
5061 loc->e = e;
5062 loc->si = si;
5063 return;
5067 /* Update the last node of the list and move to the next one. */
5068 last_loc = loc;
5069 loc = loc->next;
5072 /* If we didn't find an assertion already registered for
5073 NAME COMP_CODE VAL, add a new one at the end of the list of
5074 assertions associated with NAME. */
5075 n = XNEW (struct assert_locus_d);
5076 n->bb = dest_bb;
5077 n->e = e;
5078 n->si = si;
5079 n->comp_code = comp_code;
5080 n->val = val;
5081 n->expr = expr;
5082 n->next = NULL;
5084 if (last_loc)
5085 last_loc->next = n;
5086 else
5087 asserts_for[SSA_NAME_VERSION (name)] = n;
5089 bitmap_set_bit (need_assert_for, SSA_NAME_VERSION (name));
5092 /* (COND_OP0 COND_CODE COND_OP1) is a predicate which uses NAME.
5093 Extract a suitable test code and value and store them into *CODE_P and
5094 *VAL_P so the predicate is normalized to NAME *CODE_P *VAL_P.
5096 If no extraction was possible, return FALSE, otherwise return TRUE.
5098 If INVERT is true, then we invert the result stored into *CODE_P. */
5100 static bool
5101 extract_code_and_val_from_cond_with_ops (tree name, enum tree_code cond_code,
5102 tree cond_op0, tree cond_op1,
5103 bool invert, enum tree_code *code_p,
5104 tree *val_p)
5106 enum tree_code comp_code;
5107 tree val;
5109 /* Otherwise, we have a comparison of the form NAME COMP VAL
5110 or VAL COMP NAME. */
5111 if (name == cond_op1)
5113 /* If the predicate is of the form VAL COMP NAME, flip
5114 COMP around because we need to register NAME as the
5115 first operand in the predicate. */
5116 comp_code = swap_tree_comparison (cond_code);
5117 val = cond_op0;
5119 else
5121 /* The comparison is of the form NAME COMP VAL, so the
5122 comparison code remains unchanged. */
5123 comp_code = cond_code;
5124 val = cond_op1;
5127 /* Invert the comparison code as necessary. */
5128 if (invert)
5129 comp_code = invert_tree_comparison (comp_code, 0);
5131 /* VRP does not handle float types. */
5132 if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (val)))
5133 return false;
5135 /* Do not register always-false predicates.
5136 FIXME: this works around a limitation in fold() when dealing with
5137 enumerations. Given 'enum { N1, N2 } x;', fold will not
5138 fold 'if (x > N2)' to 'if (0)'. */
5139 if ((comp_code == GT_EXPR || comp_code == LT_EXPR)
5140 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
5142 tree min = TYPE_MIN_VALUE (TREE_TYPE (val));
5143 tree max = TYPE_MAX_VALUE (TREE_TYPE (val));
5145 if (comp_code == GT_EXPR
5146 && (!max
5147 || compare_values (val, max) == 0))
5148 return false;
5150 if (comp_code == LT_EXPR
5151 && (!min
5152 || compare_values (val, min) == 0))
5153 return false;
5155 *code_p = comp_code;
5156 *val_p = val;
5157 return true;
5160 /* Find out smallest RES where RES > VAL && (RES & MASK) == RES, if any
5161 (otherwise return VAL). VAL and MASK must be zero-extended for
5162 precision PREC. If SGNBIT is non-zero, first xor VAL with SGNBIT
5163 (to transform signed values into unsigned) and at the end xor
5164 SGNBIT back. */
5166 static wide_int
5167 masked_increment (const wide_int &val_in, const wide_int &mask,
5168 const wide_int &sgnbit, unsigned int prec)
5170 wide_int bit = wi::one (prec), res;
5171 unsigned int i;
5173 wide_int val = val_in ^ sgnbit;
5174 for (i = 0; i < prec; i++, bit += bit)
5176 res = mask;
5177 if ((res & bit) == 0)
5178 continue;
5179 res = bit - 1;
5180 res = (val + bit).and_not (res);
5181 res &= mask;
5182 if (wi::gtu_p (res, val))
5183 return res ^ sgnbit;
5185 return val ^ sgnbit;
5188 /* Try to register an edge assertion for SSA name NAME on edge E for
5189 the condition COND contributing to the conditional jump pointed to by BSI.
5190 Invert the condition COND if INVERT is true. */
5192 static void
5193 register_edge_assert_for_2 (tree name, edge e, gimple_stmt_iterator bsi,
5194 enum tree_code cond_code,
5195 tree cond_op0, tree cond_op1, bool invert)
5197 tree val;
5198 enum tree_code comp_code;
5200 if (!extract_code_and_val_from_cond_with_ops (name, cond_code,
5201 cond_op0,
5202 cond_op1,
5203 invert, &comp_code, &val))
5204 return;
5206 /* Only register an ASSERT_EXPR if NAME was found in the sub-graph
5207 reachable from E. */
5208 if (live_on_edge (e, name)
5209 && !has_single_use (name))
5210 register_new_assert_for (name, name, comp_code, val, NULL, e, bsi);
5212 /* In the case of NAME <= CST and NAME being defined as
5213 NAME = (unsigned) NAME2 + CST2 we can assert NAME2 >= -CST2
5214 and NAME2 <= CST - CST2. We can do the same for NAME > CST.
5215 This catches range and anti-range tests. */
5216 if ((comp_code == LE_EXPR
5217 || comp_code == GT_EXPR)
5218 && TREE_CODE (val) == INTEGER_CST
5219 && TYPE_UNSIGNED (TREE_TYPE (val)))
5221 gimple def_stmt = SSA_NAME_DEF_STMT (name);
5222 tree cst2 = NULL_TREE, name2 = NULL_TREE, name3 = NULL_TREE;
5224 /* Extract CST2 from the (optional) addition. */
5225 if (is_gimple_assign (def_stmt)
5226 && gimple_assign_rhs_code (def_stmt) == PLUS_EXPR)
5228 name2 = gimple_assign_rhs1 (def_stmt);
5229 cst2 = gimple_assign_rhs2 (def_stmt);
5230 if (TREE_CODE (name2) == SSA_NAME
5231 && TREE_CODE (cst2) == INTEGER_CST)
5232 def_stmt = SSA_NAME_DEF_STMT (name2);
5235 /* Extract NAME2 from the (optional) sign-changing cast. */
5236 if (gimple_assign_cast_p (def_stmt))
5238 if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt))
5239 && ! TYPE_UNSIGNED (TREE_TYPE (gimple_assign_rhs1 (def_stmt)))
5240 && (TYPE_PRECISION (gimple_expr_type (def_stmt))
5241 == TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt)))))
5242 name3 = gimple_assign_rhs1 (def_stmt);
5245 /* If name3 is used later, create an ASSERT_EXPR for it. */
5246 if (name3 != NULL_TREE
5247 && TREE_CODE (name3) == SSA_NAME
5248 && (cst2 == NULL_TREE
5249 || TREE_CODE (cst2) == INTEGER_CST)
5250 && INTEGRAL_TYPE_P (TREE_TYPE (name3))
5251 && live_on_edge (e, name3)
5252 && !has_single_use (name3))
5254 tree tmp;
5256 /* Build an expression for the range test. */
5257 tmp = build1 (NOP_EXPR, TREE_TYPE (name), name3);
5258 if (cst2 != NULL_TREE)
5259 tmp = build2 (PLUS_EXPR, TREE_TYPE (name), tmp, cst2);
5261 if (dump_file)
5263 fprintf (dump_file, "Adding assert for ");
5264 print_generic_expr (dump_file, name3, 0);
5265 fprintf (dump_file, " from ");
5266 print_generic_expr (dump_file, tmp, 0);
5267 fprintf (dump_file, "\n");
5270 register_new_assert_for (name3, tmp, comp_code, val, NULL, e, bsi);
5273 /* If name2 is used later, create an ASSERT_EXPR for it. */
5274 if (name2 != NULL_TREE
5275 && TREE_CODE (name2) == SSA_NAME
5276 && TREE_CODE (cst2) == INTEGER_CST
5277 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
5278 && live_on_edge (e, name2)
5279 && !has_single_use (name2))
5281 tree tmp;
5283 /* Build an expression for the range test. */
5284 tmp = name2;
5285 if (TREE_TYPE (name) != TREE_TYPE (name2))
5286 tmp = build1 (NOP_EXPR, TREE_TYPE (name), tmp);
5287 if (cst2 != NULL_TREE)
5288 tmp = build2 (PLUS_EXPR, TREE_TYPE (name), tmp, cst2);
5290 if (dump_file)
5292 fprintf (dump_file, "Adding assert for ");
5293 print_generic_expr (dump_file, name2, 0);
5294 fprintf (dump_file, " from ");
5295 print_generic_expr (dump_file, tmp, 0);
5296 fprintf (dump_file, "\n");
5299 register_new_assert_for (name2, tmp, comp_code, val, NULL, e, bsi);
5303 /* In the case of post-in/decrement tests like if (i++) ... and uses
5304 of the in/decremented value on the edge the extra name we want to
5305 assert for is not on the def chain of the name compared. Instead
5306 it is in the set of use stmts. */
5307 if ((comp_code == NE_EXPR
5308 || comp_code == EQ_EXPR)
5309 && TREE_CODE (val) == INTEGER_CST)
5311 imm_use_iterator ui;
5312 gimple use_stmt;
5313 FOR_EACH_IMM_USE_STMT (use_stmt, ui, name)
5315 /* Cut off to use-stmts that are in the predecessor. */
5316 if (gimple_bb (use_stmt) != e->src)
5317 continue;
5319 if (!is_gimple_assign (use_stmt))
5320 continue;
5322 enum tree_code code = gimple_assign_rhs_code (use_stmt);
5323 if (code != PLUS_EXPR
5324 && code != MINUS_EXPR)
5325 continue;
5327 tree cst = gimple_assign_rhs2 (use_stmt);
5328 if (TREE_CODE (cst) != INTEGER_CST)
5329 continue;
5331 tree name2 = gimple_assign_lhs (use_stmt);
5332 if (live_on_edge (e, name2))
5334 cst = int_const_binop (code, val, cst);
5335 register_new_assert_for (name2, name2, comp_code, cst,
5336 NULL, e, bsi);
5341 if (TREE_CODE_CLASS (comp_code) == tcc_comparison
5342 && TREE_CODE (val) == INTEGER_CST)
5344 gimple def_stmt = SSA_NAME_DEF_STMT (name);
5345 tree name2 = NULL_TREE, names[2], cst2 = NULL_TREE;
5346 tree val2 = NULL_TREE;
5347 unsigned int prec = TYPE_PRECISION (TREE_TYPE (val));
5348 wide_int mask = wi::zero (prec);
5349 unsigned int nprec = prec;
5350 enum tree_code rhs_code = ERROR_MARK;
5352 if (is_gimple_assign (def_stmt))
5353 rhs_code = gimple_assign_rhs_code (def_stmt);
5355 /* Add asserts for NAME cmp CST and NAME being defined
5356 as NAME = (int) NAME2. */
5357 if (!TYPE_UNSIGNED (TREE_TYPE (val))
5358 && (comp_code == LE_EXPR || comp_code == LT_EXPR
5359 || comp_code == GT_EXPR || comp_code == GE_EXPR)
5360 && gimple_assign_cast_p (def_stmt))
5362 name2 = gimple_assign_rhs1 (def_stmt);
5363 if (CONVERT_EXPR_CODE_P (rhs_code)
5364 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
5365 && TYPE_UNSIGNED (TREE_TYPE (name2))
5366 && prec == TYPE_PRECISION (TREE_TYPE (name2))
5367 && (comp_code == LE_EXPR || comp_code == GT_EXPR
5368 || !tree_int_cst_equal (val,
5369 TYPE_MIN_VALUE (TREE_TYPE (val))))
5370 && live_on_edge (e, name2)
5371 && !has_single_use (name2))
5373 tree tmp, cst;
5374 enum tree_code new_comp_code = comp_code;
5376 cst = fold_convert (TREE_TYPE (name2),
5377 TYPE_MIN_VALUE (TREE_TYPE (val)));
5378 /* Build an expression for the range test. */
5379 tmp = build2 (PLUS_EXPR, TREE_TYPE (name2), name2, cst);
5380 cst = fold_build2 (PLUS_EXPR, TREE_TYPE (name2), cst,
5381 fold_convert (TREE_TYPE (name2), val));
5382 if (comp_code == LT_EXPR || comp_code == GE_EXPR)
5384 new_comp_code = comp_code == LT_EXPR ? LE_EXPR : GT_EXPR;
5385 cst = fold_build2 (MINUS_EXPR, TREE_TYPE (name2), cst,
5386 build_int_cst (TREE_TYPE (name2), 1));
5389 if (dump_file)
5391 fprintf (dump_file, "Adding assert for ");
5392 print_generic_expr (dump_file, name2, 0);
5393 fprintf (dump_file, " from ");
5394 print_generic_expr (dump_file, tmp, 0);
5395 fprintf (dump_file, "\n");
5398 register_new_assert_for (name2, tmp, new_comp_code, cst, NULL,
5399 e, bsi);
5403 /* Add asserts for NAME cmp CST and NAME being defined as
5404 NAME = NAME2 >> CST2.
5406 Extract CST2 from the right shift. */
5407 if (rhs_code == RSHIFT_EXPR)
5409 name2 = gimple_assign_rhs1 (def_stmt);
5410 cst2 = gimple_assign_rhs2 (def_stmt);
5411 if (TREE_CODE (name2) == SSA_NAME
5412 && tree_fits_uhwi_p (cst2)
5413 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
5414 && IN_RANGE (tree_to_uhwi (cst2), 1, prec - 1)
5415 && prec == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (val)))
5416 && live_on_edge (e, name2)
5417 && !has_single_use (name2))
5419 mask = wi::mask (tree_to_uhwi (cst2), false, prec);
5420 val2 = fold_binary (LSHIFT_EXPR, TREE_TYPE (val), val, cst2);
5423 if (val2 != NULL_TREE
5424 && TREE_CODE (val2) == INTEGER_CST
5425 && simple_cst_equal (fold_build2 (RSHIFT_EXPR,
5426 TREE_TYPE (val),
5427 val2, cst2), val))
5429 enum tree_code new_comp_code = comp_code;
5430 tree tmp, new_val;
5432 tmp = name2;
5433 if (comp_code == EQ_EXPR || comp_code == NE_EXPR)
5435 if (!TYPE_UNSIGNED (TREE_TYPE (val)))
5437 tree type = build_nonstandard_integer_type (prec, 1);
5438 tmp = build1 (NOP_EXPR, type, name2);
5439 val2 = fold_convert (type, val2);
5441 tmp = fold_build2 (MINUS_EXPR, TREE_TYPE (tmp), tmp, val2);
5442 new_val = wide_int_to_tree (TREE_TYPE (tmp), mask);
5443 new_comp_code = comp_code == EQ_EXPR ? LE_EXPR : GT_EXPR;
5445 else if (comp_code == LT_EXPR || comp_code == GE_EXPR)
5447 wide_int minval
5448 = wi::min_value (prec, TYPE_SIGN (TREE_TYPE (val)));
5449 new_val = val2;
5450 if (minval == new_val)
5451 new_val = NULL_TREE;
5453 else
5455 wide_int maxval
5456 = wi::max_value (prec, TYPE_SIGN (TREE_TYPE (val)));
5457 mask |= val2;
5458 if (mask == maxval)
5459 new_val = NULL_TREE;
5460 else
5461 new_val = wide_int_to_tree (TREE_TYPE (val2), mask);
5464 if (new_val)
5466 if (dump_file)
5468 fprintf (dump_file, "Adding assert for ");
5469 print_generic_expr (dump_file, name2, 0);
5470 fprintf (dump_file, " from ");
5471 print_generic_expr (dump_file, tmp, 0);
5472 fprintf (dump_file, "\n");
5475 register_new_assert_for (name2, tmp, new_comp_code, new_val,
5476 NULL, e, bsi);
5480 /* Add asserts for NAME cmp CST and NAME being defined as
5481 NAME = NAME2 & CST2.
5483 Extract CST2 from the and.
5485 Also handle
5486 NAME = (unsigned) NAME2;
5487 casts where NAME's type is unsigned and has smaller precision
5488 than NAME2's type as if it was NAME = NAME2 & MASK. */
5489 names[0] = NULL_TREE;
5490 names[1] = NULL_TREE;
5491 cst2 = NULL_TREE;
5492 if (rhs_code == BIT_AND_EXPR
5493 || (CONVERT_EXPR_CODE_P (rhs_code)
5494 && TREE_CODE (TREE_TYPE (val)) == INTEGER_TYPE
5495 && TYPE_UNSIGNED (TREE_TYPE (val))
5496 && TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt)))
5497 > prec))
5499 name2 = gimple_assign_rhs1 (def_stmt);
5500 if (rhs_code == BIT_AND_EXPR)
5501 cst2 = gimple_assign_rhs2 (def_stmt);
5502 else
5504 cst2 = TYPE_MAX_VALUE (TREE_TYPE (val));
5505 nprec = TYPE_PRECISION (TREE_TYPE (name2));
5507 if (TREE_CODE (name2) == SSA_NAME
5508 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
5509 && TREE_CODE (cst2) == INTEGER_CST
5510 && !integer_zerop (cst2)
5511 && (nprec > 1
5512 || TYPE_UNSIGNED (TREE_TYPE (val))))
5514 gimple def_stmt2 = SSA_NAME_DEF_STMT (name2);
5515 if (gimple_assign_cast_p (def_stmt2))
5517 names[1] = gimple_assign_rhs1 (def_stmt2);
5518 if (!CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt2))
5519 || !INTEGRAL_TYPE_P (TREE_TYPE (names[1]))
5520 || (TYPE_PRECISION (TREE_TYPE (name2))
5521 != TYPE_PRECISION (TREE_TYPE (names[1])))
5522 || !live_on_edge (e, names[1])
5523 || has_single_use (names[1]))
5524 names[1] = NULL_TREE;
5526 if (live_on_edge (e, name2)
5527 && !has_single_use (name2))
5528 names[0] = name2;
5531 if (names[0] || names[1])
5533 wide_int minv, maxv, valv, cst2v;
5534 wide_int tem, sgnbit;
5535 bool valid_p = false, valn, cst2n;
5536 enum tree_code ccode = comp_code;
5538 valv = wide_int::from (val, nprec, UNSIGNED);
5539 cst2v = wide_int::from (cst2, nprec, UNSIGNED);
5540 valn = wi::neg_p (valv, TYPE_SIGN (TREE_TYPE (val)));
5541 cst2n = wi::neg_p (cst2v, TYPE_SIGN (TREE_TYPE (val)));
5542 /* If CST2 doesn't have most significant bit set,
5543 but VAL is negative, we have comparison like
5544 if ((x & 0x123) > -4) (always true). Just give up. */
5545 if (!cst2n && valn)
5546 ccode = ERROR_MARK;
5547 if (cst2n)
5548 sgnbit = wi::set_bit_in_zero (nprec - 1, nprec);
5549 else
5550 sgnbit = wi::zero (nprec);
5551 minv = valv & cst2v;
5552 switch (ccode)
5554 case EQ_EXPR:
5555 /* Minimum unsigned value for equality is VAL & CST2
5556 (should be equal to VAL, otherwise we probably should
5557 have folded the comparison into false) and
5558 maximum unsigned value is VAL | ~CST2. */
5559 maxv = valv | ~cst2v;
5560 valid_p = true;
5561 break;
5563 case NE_EXPR:
5564 tem = valv | ~cst2v;
5565 /* If VAL is 0, handle (X & CST2) != 0 as (X & CST2) > 0U. */
5566 if (valv == 0)
5568 cst2n = false;
5569 sgnbit = wi::zero (nprec);
5570 goto gt_expr;
5572 /* If (VAL | ~CST2) is all ones, handle it as
5573 (X & CST2) < VAL. */
5574 if (tem == -1)
5576 cst2n = false;
5577 valn = false;
5578 sgnbit = wi::zero (nprec);
5579 goto lt_expr;
5581 if (!cst2n && wi::neg_p (cst2v))
5582 sgnbit = wi::set_bit_in_zero (nprec - 1, nprec);
5583 if (sgnbit != 0)
5585 if (valv == sgnbit)
5587 cst2n = true;
5588 valn = true;
5589 goto gt_expr;
5591 if (tem == wi::mask (nprec - 1, false, nprec))
5593 cst2n = true;
5594 goto lt_expr;
5596 if (!cst2n)
5597 sgnbit = wi::zero (nprec);
5599 break;
5601 case GE_EXPR:
5602 /* Minimum unsigned value for >= if (VAL & CST2) == VAL
5603 is VAL and maximum unsigned value is ~0. For signed
5604 comparison, if CST2 doesn't have most significant bit
5605 set, handle it similarly. If CST2 has MSB set,
5606 the minimum is the same, and maximum is ~0U/2. */
5607 if (minv != valv)
5609 /* If (VAL & CST2) != VAL, X & CST2 can't be equal to
5610 VAL. */
5611 minv = masked_increment (valv, cst2v, sgnbit, nprec);
5612 if (minv == valv)
5613 break;
5615 maxv = wi::mask (nprec - (cst2n ? 1 : 0), false, nprec);
5616 valid_p = true;
5617 break;
5619 case GT_EXPR:
5620 gt_expr:
5621 /* Find out smallest MINV where MINV > VAL
5622 && (MINV & CST2) == MINV, if any. If VAL is signed and
5623 CST2 has MSB set, compute it biased by 1 << (nprec - 1). */
5624 minv = masked_increment (valv, cst2v, sgnbit, nprec);
5625 if (minv == valv)
5626 break;
5627 maxv = wi::mask (nprec - (cst2n ? 1 : 0), false, nprec);
5628 valid_p = true;
5629 break;
5631 case LE_EXPR:
5632 /* Minimum unsigned value for <= is 0 and maximum
5633 unsigned value is VAL | ~CST2 if (VAL & CST2) == VAL.
5634 Otherwise, find smallest VAL2 where VAL2 > VAL
5635 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
5636 as maximum.
5637 For signed comparison, if CST2 doesn't have most
5638 significant bit set, handle it similarly. If CST2 has
5639 MSB set, the maximum is the same and minimum is INT_MIN. */
5640 if (minv == valv)
5641 maxv = valv;
5642 else
5644 maxv = masked_increment (valv, cst2v, sgnbit, nprec);
5645 if (maxv == valv)
5646 break;
5647 maxv -= 1;
5649 maxv |= ~cst2v;
5650 minv = sgnbit;
5651 valid_p = true;
5652 break;
5654 case LT_EXPR:
5655 lt_expr:
5656 /* Minimum unsigned value for < is 0 and maximum
5657 unsigned value is (VAL-1) | ~CST2 if (VAL & CST2) == VAL.
5658 Otherwise, find smallest VAL2 where VAL2 > VAL
5659 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
5660 as maximum.
5661 For signed comparison, if CST2 doesn't have most
5662 significant bit set, handle it similarly. If CST2 has
5663 MSB set, the maximum is the same and minimum is INT_MIN. */
5664 if (minv == valv)
5666 if (valv == sgnbit)
5667 break;
5668 maxv = valv;
5670 else
5672 maxv = masked_increment (valv, cst2v, sgnbit, nprec);
5673 if (maxv == valv)
5674 break;
5676 maxv -= 1;
5677 maxv |= ~cst2v;
5678 minv = sgnbit;
5679 valid_p = true;
5680 break;
5682 default:
5683 break;
5685 if (valid_p
5686 && (maxv - minv) != -1)
5688 tree tmp, new_val, type;
5689 int i;
5691 for (i = 0; i < 2; i++)
5692 if (names[i])
5694 wide_int maxv2 = maxv;
5695 tmp = names[i];
5696 type = TREE_TYPE (names[i]);
5697 if (!TYPE_UNSIGNED (type))
5699 type = build_nonstandard_integer_type (nprec, 1);
5700 tmp = build1 (NOP_EXPR, type, names[i]);
5702 if (minv != 0)
5704 tmp = build2 (PLUS_EXPR, type, tmp,
5705 wide_int_to_tree (type, -minv));
5706 maxv2 = maxv - minv;
5708 new_val = wide_int_to_tree (type, maxv2);
5710 if (dump_file)
5712 fprintf (dump_file, "Adding assert for ");
5713 print_generic_expr (dump_file, names[i], 0);
5714 fprintf (dump_file, " from ");
5715 print_generic_expr (dump_file, tmp, 0);
5716 fprintf (dump_file, "\n");
5719 register_new_assert_for (names[i], tmp, LE_EXPR,
5720 new_val, NULL, e, bsi);
5727 /* OP is an operand of a truth value expression which is known to have
5728 a particular value. Register any asserts for OP and for any
5729 operands in OP's defining statement.
5731 If CODE is EQ_EXPR, then we want to register OP is zero (false),
5732 if CODE is NE_EXPR, then we want to register OP is nonzero (true). */
5734 static void
5735 register_edge_assert_for_1 (tree op, enum tree_code code,
5736 edge e, gimple_stmt_iterator bsi)
5738 gimple op_def;
5739 tree val;
5740 enum tree_code rhs_code;
5742 /* We only care about SSA_NAMEs. */
5743 if (TREE_CODE (op) != SSA_NAME)
5744 return;
5746 /* We know that OP will have a zero or nonzero value. If OP is used
5747 more than once go ahead and register an assert for OP. */
5748 if (live_on_edge (e, op)
5749 && !has_single_use (op))
5751 val = build_int_cst (TREE_TYPE (op), 0);
5752 register_new_assert_for (op, op, code, val, NULL, e, bsi);
5755 /* Now look at how OP is set. If it's set from a comparison,
5756 a truth operation or some bit operations, then we may be able
5757 to register information about the operands of that assignment. */
5758 op_def = SSA_NAME_DEF_STMT (op);
5759 if (gimple_code (op_def) != GIMPLE_ASSIGN)
5760 return;
5762 rhs_code = gimple_assign_rhs_code (op_def);
5764 if (TREE_CODE_CLASS (rhs_code) == tcc_comparison)
5766 bool invert = (code == EQ_EXPR ? true : false);
5767 tree op0 = gimple_assign_rhs1 (op_def);
5768 tree op1 = gimple_assign_rhs2 (op_def);
5770 if (TREE_CODE (op0) == SSA_NAME)
5771 register_edge_assert_for_2 (op0, e, bsi, rhs_code, op0, op1, invert);
5772 if (TREE_CODE (op1) == SSA_NAME)
5773 register_edge_assert_for_2 (op1, e, bsi, rhs_code, op0, op1, invert);
5775 else if ((code == NE_EXPR
5776 && gimple_assign_rhs_code (op_def) == BIT_AND_EXPR)
5777 || (code == EQ_EXPR
5778 && gimple_assign_rhs_code (op_def) == BIT_IOR_EXPR))
5780 /* Recurse on each operand. */
5781 tree op0 = gimple_assign_rhs1 (op_def);
5782 tree op1 = gimple_assign_rhs2 (op_def);
5783 if (TREE_CODE (op0) == SSA_NAME
5784 && has_single_use (op0))
5785 register_edge_assert_for_1 (op0, code, e, bsi);
5786 if (TREE_CODE (op1) == SSA_NAME
5787 && has_single_use (op1))
5788 register_edge_assert_for_1 (op1, code, e, bsi);
5790 else if (gimple_assign_rhs_code (op_def) == BIT_NOT_EXPR
5791 && TYPE_PRECISION (TREE_TYPE (gimple_assign_lhs (op_def))) == 1)
5793 /* Recurse, flipping CODE. */
5794 code = invert_tree_comparison (code, false);
5795 register_edge_assert_for_1 (gimple_assign_rhs1 (op_def), code, e, bsi);
5797 else if (gimple_assign_rhs_code (op_def) == SSA_NAME)
5799 /* Recurse through the copy. */
5800 register_edge_assert_for_1 (gimple_assign_rhs1 (op_def), code, e, bsi);
5802 else if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (op_def)))
5804 /* Recurse through the type conversion, unless it is a narrowing
5805 conversion or conversion from non-integral type. */
5806 tree rhs = gimple_assign_rhs1 (op_def);
5807 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs))
5808 && (TYPE_PRECISION (TREE_TYPE (rhs))
5809 <= TYPE_PRECISION (TREE_TYPE (op))))
5810 register_edge_assert_for_1 (rhs, code, e, bsi);
5814 /* Try to register an edge assertion for SSA name NAME on edge E for
5815 the condition COND contributing to the conditional jump pointed to by
5816 SI. */
5818 static void
5819 register_edge_assert_for (tree name, edge e, gimple_stmt_iterator si,
5820 enum tree_code cond_code, tree cond_op0,
5821 tree cond_op1)
5823 tree val;
5824 enum tree_code comp_code;
5825 bool is_else_edge = (e->flags & EDGE_FALSE_VALUE) != 0;
5827 /* Do not attempt to infer anything in names that flow through
5828 abnormal edges. */
5829 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name))
5830 return;
5832 if (!extract_code_and_val_from_cond_with_ops (name, cond_code,
5833 cond_op0, cond_op1,
5834 is_else_edge,
5835 &comp_code, &val))
5836 return;
5838 /* Register ASSERT_EXPRs for name. */
5839 register_edge_assert_for_2 (name, e, si, cond_code, cond_op0,
5840 cond_op1, is_else_edge);
5843 /* If COND is effectively an equality test of an SSA_NAME against
5844 the value zero or one, then we may be able to assert values
5845 for SSA_NAMEs which flow into COND. */
5847 /* In the case of NAME == 1 or NAME != 0, for BIT_AND_EXPR defining
5848 statement of NAME we can assert both operands of the BIT_AND_EXPR
5849 have nonzero value. */
5850 if (((comp_code == EQ_EXPR && integer_onep (val))
5851 || (comp_code == NE_EXPR && integer_zerop (val))))
5853 gimple def_stmt = SSA_NAME_DEF_STMT (name);
5855 if (is_gimple_assign (def_stmt)
5856 && gimple_assign_rhs_code (def_stmt) == BIT_AND_EXPR)
5858 tree op0 = gimple_assign_rhs1 (def_stmt);
5859 tree op1 = gimple_assign_rhs2 (def_stmt);
5860 register_edge_assert_for_1 (op0, NE_EXPR, e, si);
5861 register_edge_assert_for_1 (op1, NE_EXPR, e, si);
5865 /* In the case of NAME == 0 or NAME != 1, for BIT_IOR_EXPR defining
5866 statement of NAME we can assert both operands of the BIT_IOR_EXPR
5867 have zero value. */
5868 if (((comp_code == EQ_EXPR && integer_zerop (val))
5869 || (comp_code == NE_EXPR && integer_onep (val))))
5871 gimple def_stmt = SSA_NAME_DEF_STMT (name);
5873 /* For BIT_IOR_EXPR only if NAME == 0 both operands have
5874 necessarily zero value, or if type-precision is one. */
5875 if (is_gimple_assign (def_stmt)
5876 && (gimple_assign_rhs_code (def_stmt) == BIT_IOR_EXPR
5877 && (TYPE_PRECISION (TREE_TYPE (name)) == 1
5878 || comp_code == EQ_EXPR)))
5880 tree op0 = gimple_assign_rhs1 (def_stmt);
5881 tree op1 = gimple_assign_rhs2 (def_stmt);
5882 register_edge_assert_for_1 (op0, EQ_EXPR, e, si);
5883 register_edge_assert_for_1 (op1, EQ_EXPR, e, si);
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 COND_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_conditional_asserts (basic_block bb, gcond *last)
5900 gimple_stmt_iterator bsi;
5901 tree op;
5902 edge_iterator ei;
5903 edge e;
5904 ssa_op_iter iter;
5906 bsi = gsi_for_stmt (last);
5908 /* Look for uses of the operands in each of the sub-graphs
5909 rooted at BB. We need to check each of the outgoing edges
5910 separately, so that we know what kind of ASSERT_EXPR to
5911 insert. */
5912 FOR_EACH_EDGE (e, ei, bb->succs)
5914 if (e->dest == bb)
5915 continue;
5917 /* Register the necessary assertions for each operand in the
5918 conditional predicate. */
5919 FOR_EACH_SSA_TREE_OPERAND (op, last, iter, SSA_OP_USE)
5920 register_edge_assert_for (op, e, bsi,
5921 gimple_cond_code (last),
5922 gimple_cond_lhs (last),
5923 gimple_cond_rhs (last));
5927 struct case_info
5929 tree expr;
5930 basic_block bb;
5933 /* Compare two case labels sorting first by the destination bb index
5934 and then by the case value. */
5936 static int
5937 compare_case_labels (const void *p1, const void *p2)
5939 const struct case_info *ci1 = (const struct case_info *) p1;
5940 const struct case_info *ci2 = (const struct case_info *) p2;
5941 int idx1 = ci1->bb->index;
5942 int idx2 = ci2->bb->index;
5944 if (idx1 < idx2)
5945 return -1;
5946 else if (idx1 == idx2)
5948 /* Make sure the default label is first in a group. */
5949 if (!CASE_LOW (ci1->expr))
5950 return -1;
5951 else if (!CASE_LOW (ci2->expr))
5952 return 1;
5953 else
5954 return tree_int_cst_compare (CASE_LOW (ci1->expr),
5955 CASE_LOW (ci2->expr));
5957 else
5958 return 1;
5961 /* Determine whether the outgoing edges of BB should receive an
5962 ASSERT_EXPR for each of the operands of BB's LAST statement.
5963 The last statement of BB must be a SWITCH_EXPR.
5965 If any of the sub-graphs rooted at BB have an interesting use of
5966 the predicate operands, an assert location node is added to the
5967 list of assertions for the corresponding operands. */
5969 static void
5970 find_switch_asserts (basic_block bb, gswitch *last)
5972 gimple_stmt_iterator bsi;
5973 tree op;
5974 edge e;
5975 struct case_info *ci;
5976 size_t n = gimple_switch_num_labels (last);
5977 #if GCC_VERSION >= 4000
5978 unsigned int idx;
5979 #else
5980 /* Work around GCC 3.4 bug (PR 37086). */
5981 volatile unsigned int idx;
5982 #endif
5984 bsi = gsi_for_stmt (last);
5985 op = gimple_switch_index (last);
5986 if (TREE_CODE (op) != SSA_NAME)
5987 return;
5989 /* Build a vector of case labels sorted by destination label. */
5990 ci = XNEWVEC (struct case_info, n);
5991 for (idx = 0; idx < n; ++idx)
5993 ci[idx].expr = gimple_switch_label (last, idx);
5994 ci[idx].bb = label_to_block (CASE_LABEL (ci[idx].expr));
5996 qsort (ci, n, sizeof (struct case_info), compare_case_labels);
5998 for (idx = 0; idx < n; ++idx)
6000 tree min, max;
6001 tree cl = ci[idx].expr;
6002 basic_block cbb = ci[idx].bb;
6004 min = CASE_LOW (cl);
6005 max = CASE_HIGH (cl);
6007 /* If there are multiple case labels with the same destination
6008 we need to combine them to a single value range for the edge. */
6009 if (idx + 1 < n && cbb == ci[idx + 1].bb)
6011 /* Skip labels until the last of the group. */
6012 do {
6013 ++idx;
6014 } while (idx < n && cbb == ci[idx].bb);
6015 --idx;
6017 /* Pick up the maximum of the case label range. */
6018 if (CASE_HIGH (ci[idx].expr))
6019 max = CASE_HIGH (ci[idx].expr);
6020 else
6021 max = CASE_LOW (ci[idx].expr);
6024 /* Nothing to do if the range includes the default label until we
6025 can register anti-ranges. */
6026 if (min == NULL_TREE)
6027 continue;
6029 /* Find the edge to register the assert expr on. */
6030 e = find_edge (bb, cbb);
6032 /* Register the necessary assertions for the operand in the
6033 SWITCH_EXPR. */
6034 register_edge_assert_for (op, e, bsi,
6035 max ? GE_EXPR : EQ_EXPR,
6036 op, fold_convert (TREE_TYPE (op), min));
6037 if (max)
6038 register_edge_assert_for (op, e, bsi, LE_EXPR, op,
6039 fold_convert (TREE_TYPE (op), max));
6042 XDELETEVEC (ci);
6046 /* Traverse all the statements in block BB looking for statements that
6047 may generate useful assertions for the SSA names in their operand.
6048 If a statement produces a useful assertion A for name N_i, then the
6049 list of assertions already generated for N_i is scanned to
6050 determine if A is actually needed.
6052 If N_i already had the assertion A at a location dominating the
6053 current location, then nothing needs to be done. Otherwise, the
6054 new location for A is recorded instead.
6056 1- For every statement S in BB, all the variables used by S are
6057 added to bitmap FOUND_IN_SUBGRAPH.
6059 2- If statement S uses an operand N in a way that exposes a known
6060 value range for N, then if N was not already generated by an
6061 ASSERT_EXPR, create a new assert location for N. For instance,
6062 if N is a pointer and the statement dereferences it, we can
6063 assume that N is not NULL.
6065 3- COND_EXPRs are a special case of #2. We can derive range
6066 information from the predicate but need to insert different
6067 ASSERT_EXPRs for each of the sub-graphs rooted at the
6068 conditional block. If the last statement of BB is a conditional
6069 expression of the form 'X op Y', then
6071 a) Remove X and Y from the set FOUND_IN_SUBGRAPH.
6073 b) If the conditional is the only entry point to the sub-graph
6074 corresponding to the THEN_CLAUSE, recurse into it. On
6075 return, if X and/or Y are marked in FOUND_IN_SUBGRAPH, then
6076 an ASSERT_EXPR is added for the corresponding variable.
6078 c) Repeat step (b) on the ELSE_CLAUSE.
6080 d) Mark X and Y in FOUND_IN_SUBGRAPH.
6082 For instance,
6084 if (a == 9)
6085 b = a;
6086 else
6087 b = c + 1;
6089 In this case, an assertion on the THEN clause is useful to
6090 determine that 'a' is always 9 on that edge. However, an assertion
6091 on the ELSE clause would be unnecessary.
6093 4- If BB does not end in a conditional expression, then we recurse
6094 into BB's dominator children.
6096 At the end of the recursive traversal, every SSA name will have a
6097 list of locations where ASSERT_EXPRs should be added. When a new
6098 location for name N is found, it is registered by calling
6099 register_new_assert_for. That function keeps track of all the
6100 registered assertions to prevent adding unnecessary assertions.
6101 For instance, if a pointer P_4 is dereferenced more than once in a
6102 dominator tree, only the location dominating all the dereference of
6103 P_4 will receive an ASSERT_EXPR. */
6105 static void
6106 find_assert_locations_1 (basic_block bb, sbitmap live)
6108 gimple last;
6110 last = last_stmt (bb);
6112 /* If BB's last statement is a conditional statement involving integer
6113 operands, determine if we need to add ASSERT_EXPRs. */
6114 if (last
6115 && gimple_code (last) == GIMPLE_COND
6116 && !fp_predicate (last)
6117 && !ZERO_SSA_OPERANDS (last, SSA_OP_USE))
6118 find_conditional_asserts (bb, as_a <gcond *> (last));
6120 /* If BB's last statement is a switch statement involving integer
6121 operands, determine if we need to add ASSERT_EXPRs. */
6122 if (last
6123 && gimple_code (last) == GIMPLE_SWITCH
6124 && !ZERO_SSA_OPERANDS (last, SSA_OP_USE))
6125 find_switch_asserts (bb, as_a <gswitch *> (last));
6127 /* Traverse all the statements in BB marking used names and looking
6128 for statements that may infer assertions for their used operands. */
6129 for (gimple_stmt_iterator si = gsi_last_bb (bb); !gsi_end_p (si);
6130 gsi_prev (&si))
6132 gimple stmt;
6133 tree op;
6134 ssa_op_iter i;
6136 stmt = gsi_stmt (si);
6138 if (is_gimple_debug (stmt))
6139 continue;
6141 /* See if we can derive an assertion for any of STMT's operands. */
6142 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_USE)
6144 tree value;
6145 enum tree_code comp_code;
6147 /* If op is not live beyond this stmt, do not bother to insert
6148 asserts for it. */
6149 if (!bitmap_bit_p (live, SSA_NAME_VERSION (op)))
6150 continue;
6152 /* If OP is used in such a way that we can infer a value
6153 range for it, and we don't find a previous assertion for
6154 it, create a new assertion location node for OP. */
6155 if (infer_value_range (stmt, op, &comp_code, &value))
6157 /* If we are able to infer a nonzero value range for OP,
6158 then walk backwards through the use-def chain to see if OP
6159 was set via a typecast.
6161 If so, then we can also infer a nonzero value range
6162 for the operand of the NOP_EXPR. */
6163 if (comp_code == NE_EXPR && integer_zerop (value))
6165 tree t = op;
6166 gimple def_stmt = SSA_NAME_DEF_STMT (t);
6168 while (is_gimple_assign (def_stmt)
6169 && CONVERT_EXPR_CODE_P
6170 (gimple_assign_rhs_code (def_stmt))
6171 && TREE_CODE
6172 (gimple_assign_rhs1 (def_stmt)) == SSA_NAME
6173 && POINTER_TYPE_P
6174 (TREE_TYPE (gimple_assign_rhs1 (def_stmt))))
6176 t = gimple_assign_rhs1 (def_stmt);
6177 def_stmt = SSA_NAME_DEF_STMT (t);
6179 /* Note we want to register the assert for the
6180 operand of the NOP_EXPR after SI, not after the
6181 conversion. */
6182 if (! has_single_use (t))
6183 register_new_assert_for (t, t, comp_code, value,
6184 bb, NULL, si);
6188 register_new_assert_for (op, op, comp_code, value, bb, NULL, si);
6192 /* Update live. */
6193 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_USE)
6194 bitmap_set_bit (live, SSA_NAME_VERSION (op));
6195 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_DEF)
6196 bitmap_clear_bit (live, SSA_NAME_VERSION (op));
6199 /* Traverse all PHI nodes in BB, updating live. */
6200 for (gphi_iterator si = gsi_start_phis (bb); !gsi_end_p (si);
6201 gsi_next (&si))
6203 use_operand_p arg_p;
6204 ssa_op_iter i;
6205 gphi *phi = si.phi ();
6206 tree res = gimple_phi_result (phi);
6208 if (virtual_operand_p (res))
6209 continue;
6211 FOR_EACH_PHI_ARG (arg_p, phi, i, SSA_OP_USE)
6213 tree arg = USE_FROM_PTR (arg_p);
6214 if (TREE_CODE (arg) == SSA_NAME)
6215 bitmap_set_bit (live, SSA_NAME_VERSION (arg));
6218 bitmap_clear_bit (live, SSA_NAME_VERSION (res));
6222 /* Do an RPO walk over the function computing SSA name liveness
6223 on-the-fly and deciding on assert expressions to insert. */
6225 static void
6226 find_assert_locations (void)
6228 int *rpo = XNEWVEC (int, last_basic_block_for_fn (cfun));
6229 int *bb_rpo = XNEWVEC (int, last_basic_block_for_fn (cfun));
6230 int *last_rpo = XCNEWVEC (int, last_basic_block_for_fn (cfun));
6231 int rpo_cnt, i;
6233 live = XCNEWVEC (sbitmap, last_basic_block_for_fn (cfun));
6234 rpo_cnt = pre_and_rev_post_order_compute (NULL, rpo, false);
6235 for (i = 0; i < rpo_cnt; ++i)
6236 bb_rpo[rpo[i]] = i;
6238 /* Pre-seed loop latch liveness from loop header PHI nodes. Due to
6239 the order we compute liveness and insert asserts we otherwise
6240 fail to insert asserts into the loop latch. */
6241 loop_p loop;
6242 FOR_EACH_LOOP (loop, 0)
6244 i = loop->latch->index;
6245 unsigned int j = single_succ_edge (loop->latch)->dest_idx;
6246 for (gphi_iterator gsi = gsi_start_phis (loop->header);
6247 !gsi_end_p (gsi); gsi_next (&gsi))
6249 gphi *phi = gsi.phi ();
6250 if (virtual_operand_p (gimple_phi_result (phi)))
6251 continue;
6252 tree arg = gimple_phi_arg_def (phi, j);
6253 if (TREE_CODE (arg) == SSA_NAME)
6255 if (live[i] == NULL)
6257 live[i] = sbitmap_alloc (num_ssa_names);
6258 bitmap_clear (live[i]);
6260 bitmap_set_bit (live[i], SSA_NAME_VERSION (arg));
6265 for (i = rpo_cnt - 1; i >= 0; --i)
6267 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, rpo[i]);
6268 edge e;
6269 edge_iterator ei;
6271 if (!live[rpo[i]])
6273 live[rpo[i]] = sbitmap_alloc (num_ssa_names);
6274 bitmap_clear (live[rpo[i]]);
6277 /* Process BB and update the live information with uses in
6278 this block. */
6279 find_assert_locations_1 (bb, live[rpo[i]]);
6281 /* Merge liveness into the predecessor blocks and free it. */
6282 if (!bitmap_empty_p (live[rpo[i]]))
6284 int pred_rpo = i;
6285 FOR_EACH_EDGE (e, ei, bb->preds)
6287 int pred = e->src->index;
6288 if ((e->flags & EDGE_DFS_BACK) || pred == ENTRY_BLOCK)
6289 continue;
6291 if (!live[pred])
6293 live[pred] = sbitmap_alloc (num_ssa_names);
6294 bitmap_clear (live[pred]);
6296 bitmap_ior (live[pred], live[pred], live[rpo[i]]);
6298 if (bb_rpo[pred] < pred_rpo)
6299 pred_rpo = bb_rpo[pred];
6302 /* Record the RPO number of the last visited block that needs
6303 live information from this block. */
6304 last_rpo[rpo[i]] = pred_rpo;
6306 else
6308 sbitmap_free (live[rpo[i]]);
6309 live[rpo[i]] = NULL;
6312 /* We can free all successors live bitmaps if all their
6313 predecessors have been visited already. */
6314 FOR_EACH_EDGE (e, ei, bb->succs)
6315 if (last_rpo[e->dest->index] == i
6316 && live[e->dest->index])
6318 sbitmap_free (live[e->dest->index]);
6319 live[e->dest->index] = NULL;
6323 XDELETEVEC (rpo);
6324 XDELETEVEC (bb_rpo);
6325 XDELETEVEC (last_rpo);
6326 for (i = 0; i < last_basic_block_for_fn (cfun); ++i)
6327 if (live[i])
6328 sbitmap_free (live[i]);
6329 XDELETEVEC (live);
6332 /* Create an ASSERT_EXPR for NAME and insert it in the location
6333 indicated by LOC. Return true if we made any edge insertions. */
6335 static bool
6336 process_assert_insertions_for (tree name, assert_locus_t loc)
6338 /* Build the comparison expression NAME_i COMP_CODE VAL. */
6339 gimple stmt;
6340 tree cond;
6341 gimple assert_stmt;
6342 edge_iterator ei;
6343 edge e;
6345 /* If we have X <=> X do not insert an assert expr for that. */
6346 if (loc->expr == loc->val)
6347 return false;
6349 cond = build2 (loc->comp_code, boolean_type_node, loc->expr, loc->val);
6350 assert_stmt = build_assert_expr_for (cond, name);
6351 if (loc->e)
6353 /* We have been asked to insert the assertion on an edge. This
6354 is used only by COND_EXPR and SWITCH_EXPR assertions. */
6355 gcc_checking_assert (gimple_code (gsi_stmt (loc->si)) == GIMPLE_COND
6356 || (gimple_code (gsi_stmt (loc->si))
6357 == GIMPLE_SWITCH));
6359 gsi_insert_on_edge (loc->e, assert_stmt);
6360 return true;
6363 /* Otherwise, we can insert right after LOC->SI iff the
6364 statement must not be the last statement in the block. */
6365 stmt = gsi_stmt (loc->si);
6366 if (!stmt_ends_bb_p (stmt))
6368 gsi_insert_after (&loc->si, assert_stmt, GSI_SAME_STMT);
6369 return false;
6372 /* If STMT must be the last statement in BB, we can only insert new
6373 assertions on the non-abnormal edge out of BB. Note that since
6374 STMT is not control flow, there may only be one non-abnormal edge
6375 out of BB. */
6376 FOR_EACH_EDGE (e, ei, loc->bb->succs)
6377 if (!(e->flags & EDGE_ABNORMAL))
6379 gsi_insert_on_edge (e, assert_stmt);
6380 return true;
6383 gcc_unreachable ();
6387 /* Process all the insertions registered for every name N_i registered
6388 in NEED_ASSERT_FOR. The list of assertions to be inserted are
6389 found in ASSERTS_FOR[i]. */
6391 static void
6392 process_assert_insertions (void)
6394 unsigned i;
6395 bitmap_iterator bi;
6396 bool update_edges_p = false;
6397 int num_asserts = 0;
6399 if (dump_file && (dump_flags & TDF_DETAILS))
6400 dump_all_asserts (dump_file);
6402 EXECUTE_IF_SET_IN_BITMAP (need_assert_for, 0, i, bi)
6404 assert_locus_t loc = asserts_for[i];
6405 gcc_assert (loc);
6407 while (loc)
6409 assert_locus_t next = loc->next;
6410 update_edges_p |= process_assert_insertions_for (ssa_name (i), loc);
6411 free (loc);
6412 loc = next;
6413 num_asserts++;
6417 if (update_edges_p)
6418 gsi_commit_edge_inserts ();
6420 statistics_counter_event (cfun, "Number of ASSERT_EXPR expressions inserted",
6421 num_asserts);
6425 /* Traverse the flowgraph looking for conditional jumps to insert range
6426 expressions. These range expressions are meant to provide information
6427 to optimizations that need to reason in terms of value ranges. They
6428 will not be expanded into RTL. For instance, given:
6430 x = ...
6431 y = ...
6432 if (x < y)
6433 y = x - 2;
6434 else
6435 x = y + 3;
6437 this pass will transform the code into:
6439 x = ...
6440 y = ...
6441 if (x < y)
6443 x = ASSERT_EXPR <x, x < y>
6444 y = x - 2
6446 else
6448 y = ASSERT_EXPR <y, x >= y>
6449 x = y + 3
6452 The idea is that once copy and constant propagation have run, other
6453 optimizations will be able to determine what ranges of values can 'x'
6454 take in different paths of the code, simply by checking the reaching
6455 definition of 'x'. */
6457 static void
6458 insert_range_assertions (void)
6460 need_assert_for = BITMAP_ALLOC (NULL);
6461 asserts_for = XCNEWVEC (assert_locus_t, num_ssa_names);
6463 calculate_dominance_info (CDI_DOMINATORS);
6465 find_assert_locations ();
6466 if (!bitmap_empty_p (need_assert_for))
6468 process_assert_insertions ();
6469 update_ssa (TODO_update_ssa_no_phi);
6472 if (dump_file && (dump_flags & TDF_DETAILS))
6474 fprintf (dump_file, "\nSSA form after inserting ASSERT_EXPRs\n");
6475 dump_function_to_file (current_function_decl, dump_file, dump_flags);
6478 free (asserts_for);
6479 BITMAP_FREE (need_assert_for);
6482 /* Checks one ARRAY_REF in REF, located at LOCUS. Ignores flexible arrays
6483 and "struct" hacks. If VRP can determine that the
6484 array subscript is a constant, check if it is outside valid
6485 range. If the array subscript is a RANGE, warn if it is
6486 non-overlapping with valid range.
6487 IGNORE_OFF_BY_ONE is true if the ARRAY_REF is inside a ADDR_EXPR. */
6489 static void
6490 check_array_ref (location_t location, tree ref, bool ignore_off_by_one)
6492 value_range_t* vr = NULL;
6493 tree low_sub, up_sub;
6494 tree low_bound, up_bound, up_bound_p1;
6495 tree base;
6497 if (TREE_NO_WARNING (ref))
6498 return;
6500 low_sub = up_sub = TREE_OPERAND (ref, 1);
6501 up_bound = array_ref_up_bound (ref);
6503 /* Can not check flexible arrays. */
6504 if (!up_bound
6505 || TREE_CODE (up_bound) != INTEGER_CST)
6506 return;
6508 /* Accesses to trailing arrays via pointers may access storage
6509 beyond the types array bounds. */
6510 base = get_base_address (ref);
6511 if ((warn_array_bounds < 2)
6512 && base && TREE_CODE (base) == MEM_REF)
6514 tree cref, next = NULL_TREE;
6516 if (TREE_CODE (TREE_OPERAND (ref, 0)) != COMPONENT_REF)
6517 return;
6519 cref = TREE_OPERAND (ref, 0);
6520 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (cref, 0))) == RECORD_TYPE)
6521 for (next = DECL_CHAIN (TREE_OPERAND (cref, 1));
6522 next && TREE_CODE (next) != FIELD_DECL;
6523 next = DECL_CHAIN (next))
6526 /* If this is the last field in a struct type or a field in a
6527 union type do not warn. */
6528 if (!next)
6529 return;
6532 low_bound = array_ref_low_bound (ref);
6533 up_bound_p1 = int_const_binop (PLUS_EXPR, up_bound,
6534 build_int_cst (TREE_TYPE (up_bound), 1));
6536 if (TREE_CODE (low_sub) == SSA_NAME)
6538 vr = get_value_range (low_sub);
6539 if (vr->type == VR_RANGE || vr->type == VR_ANTI_RANGE)
6541 low_sub = vr->type == VR_RANGE ? vr->max : vr->min;
6542 up_sub = vr->type == VR_RANGE ? vr->min : vr->max;
6546 if (vr && vr->type == VR_ANTI_RANGE)
6548 if (TREE_CODE (up_sub) == INTEGER_CST
6549 && tree_int_cst_lt (up_bound, up_sub)
6550 && TREE_CODE (low_sub) == INTEGER_CST
6551 && tree_int_cst_lt (low_sub, low_bound))
6553 warning_at (location, OPT_Warray_bounds,
6554 "array subscript is outside array bounds");
6555 TREE_NO_WARNING (ref) = 1;
6558 else if (TREE_CODE (up_sub) == INTEGER_CST
6559 && (ignore_off_by_one
6560 ? (tree_int_cst_lt (up_bound, up_sub)
6561 && !tree_int_cst_equal (up_bound_p1, up_sub))
6562 : (tree_int_cst_lt (up_bound, up_sub)
6563 || tree_int_cst_equal (up_bound_p1, up_sub))))
6565 if (dump_file && (dump_flags & TDF_DETAILS))
6567 fprintf (dump_file, "Array bound warning for ");
6568 dump_generic_expr (MSG_NOTE, TDF_SLIM, ref);
6569 fprintf (dump_file, "\n");
6571 warning_at (location, OPT_Warray_bounds,
6572 "array subscript is above array bounds");
6573 TREE_NO_WARNING (ref) = 1;
6575 else if (TREE_CODE (low_sub) == INTEGER_CST
6576 && tree_int_cst_lt (low_sub, low_bound))
6578 if (dump_file && (dump_flags & TDF_DETAILS))
6580 fprintf (dump_file, "Array bound warning for ");
6581 dump_generic_expr (MSG_NOTE, TDF_SLIM, ref);
6582 fprintf (dump_file, "\n");
6584 warning_at (location, OPT_Warray_bounds,
6585 "array subscript is below array bounds");
6586 TREE_NO_WARNING (ref) = 1;
6590 /* Searches if the expr T, located at LOCATION computes
6591 address of an ARRAY_REF, and call check_array_ref on it. */
6593 static void
6594 search_for_addr_array (tree t, location_t location)
6596 while (TREE_CODE (t) == SSA_NAME)
6598 gimple g = SSA_NAME_DEF_STMT (t);
6600 if (gimple_code (g) != GIMPLE_ASSIGN)
6601 return;
6603 if (get_gimple_rhs_class (gimple_assign_rhs_code (g))
6604 != GIMPLE_SINGLE_RHS)
6605 return;
6607 t = gimple_assign_rhs1 (g);
6611 /* We are only interested in addresses of ARRAY_REF's. */
6612 if (TREE_CODE (t) != ADDR_EXPR)
6613 return;
6615 /* Check each ARRAY_REFs in the reference chain. */
6618 if (TREE_CODE (t) == ARRAY_REF)
6619 check_array_ref (location, t, true /*ignore_off_by_one*/);
6621 t = TREE_OPERAND (t, 0);
6623 while (handled_component_p (t));
6625 if (TREE_CODE (t) == MEM_REF
6626 && TREE_CODE (TREE_OPERAND (t, 0)) == ADDR_EXPR
6627 && !TREE_NO_WARNING (t))
6629 tree tem = TREE_OPERAND (TREE_OPERAND (t, 0), 0);
6630 tree low_bound, up_bound, el_sz;
6631 offset_int idx;
6632 if (TREE_CODE (TREE_TYPE (tem)) != ARRAY_TYPE
6633 || TREE_CODE (TREE_TYPE (TREE_TYPE (tem))) == ARRAY_TYPE
6634 || !TYPE_DOMAIN (TREE_TYPE (tem)))
6635 return;
6637 low_bound = TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (tem)));
6638 up_bound = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (tem)));
6639 el_sz = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (tem)));
6640 if (!low_bound
6641 || TREE_CODE (low_bound) != INTEGER_CST
6642 || !up_bound
6643 || TREE_CODE (up_bound) != INTEGER_CST
6644 || !el_sz
6645 || TREE_CODE (el_sz) != INTEGER_CST)
6646 return;
6648 idx = mem_ref_offset (t);
6649 idx = wi::sdiv_trunc (idx, wi::to_offset (el_sz));
6650 if (wi::lts_p (idx, 0))
6652 if (dump_file && (dump_flags & TDF_DETAILS))
6654 fprintf (dump_file, "Array bound warning for ");
6655 dump_generic_expr (MSG_NOTE, TDF_SLIM, t);
6656 fprintf (dump_file, "\n");
6658 warning_at (location, OPT_Warray_bounds,
6659 "array subscript is below array bounds");
6660 TREE_NO_WARNING (t) = 1;
6662 else if (wi::gts_p (idx, (wi::to_offset (up_bound)
6663 - wi::to_offset (low_bound) + 1)))
6665 if (dump_file && (dump_flags & TDF_DETAILS))
6667 fprintf (dump_file, "Array bound warning for ");
6668 dump_generic_expr (MSG_NOTE, TDF_SLIM, t);
6669 fprintf (dump_file, "\n");
6671 warning_at (location, OPT_Warray_bounds,
6672 "array subscript is above array bounds");
6673 TREE_NO_WARNING (t) = 1;
6678 /* walk_tree() callback that checks if *TP is
6679 an ARRAY_REF inside an ADDR_EXPR (in which an array
6680 subscript one outside the valid range is allowed). Call
6681 check_array_ref for each ARRAY_REF found. The location is
6682 passed in DATA. */
6684 static tree
6685 check_array_bounds (tree *tp, int *walk_subtree, void *data)
6687 tree t = *tp;
6688 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
6689 location_t location;
6691 if (EXPR_HAS_LOCATION (t))
6692 location = EXPR_LOCATION (t);
6693 else
6695 location_t *locp = (location_t *) wi->info;
6696 location = *locp;
6699 *walk_subtree = TRUE;
6701 if (TREE_CODE (t) == ARRAY_REF)
6702 check_array_ref (location, t, false /*ignore_off_by_one*/);
6704 if (TREE_CODE (t) == MEM_REF
6705 || (TREE_CODE (t) == RETURN_EXPR && TREE_OPERAND (t, 0)))
6706 search_for_addr_array (TREE_OPERAND (t, 0), location);
6708 if (TREE_CODE (t) == ADDR_EXPR)
6709 *walk_subtree = FALSE;
6711 return NULL_TREE;
6714 /* Walk over all statements of all reachable BBs and call check_array_bounds
6715 on them. */
6717 static void
6718 check_all_array_refs (void)
6720 basic_block bb;
6721 gimple_stmt_iterator si;
6723 FOR_EACH_BB_FN (bb, cfun)
6725 edge_iterator ei;
6726 edge e;
6727 bool executable = false;
6729 /* Skip blocks that were found to be unreachable. */
6730 FOR_EACH_EDGE (e, ei, bb->preds)
6731 executable |= !!(e->flags & EDGE_EXECUTABLE);
6732 if (!executable)
6733 continue;
6735 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
6737 gimple stmt = gsi_stmt (si);
6738 struct walk_stmt_info wi;
6739 if (!gimple_has_location (stmt))
6740 continue;
6742 if (is_gimple_call (stmt))
6744 size_t i;
6745 size_t n = gimple_call_num_args (stmt);
6746 for (i = 0; i < n; i++)
6748 tree arg = gimple_call_arg (stmt, i);
6749 search_for_addr_array (arg, gimple_location (stmt));
6752 else
6754 memset (&wi, 0, sizeof (wi));
6755 wi.info = CONST_CAST (void *, (const void *)
6756 gimple_location_ptr (stmt));
6758 walk_gimple_op (gsi_stmt (si),
6759 check_array_bounds,
6760 &wi);
6766 /* Return true if all imm uses of VAR are either in STMT, or
6767 feed (optionally through a chain of single imm uses) GIMPLE_COND
6768 in basic block COND_BB. */
6770 static bool
6771 all_imm_uses_in_stmt_or_feed_cond (tree var, gimple stmt, basic_block cond_bb)
6773 use_operand_p use_p, use2_p;
6774 imm_use_iterator iter;
6776 FOR_EACH_IMM_USE_FAST (use_p, iter, var)
6777 if (USE_STMT (use_p) != stmt)
6779 gimple use_stmt = USE_STMT (use_p), use_stmt2;
6780 if (is_gimple_debug (use_stmt))
6781 continue;
6782 while (is_gimple_assign (use_stmt)
6783 && TREE_CODE (gimple_assign_lhs (use_stmt)) == SSA_NAME
6784 && single_imm_use (gimple_assign_lhs (use_stmt),
6785 &use2_p, &use_stmt2))
6786 use_stmt = use_stmt2;
6787 if (gimple_code (use_stmt) != GIMPLE_COND
6788 || gimple_bb (use_stmt) != cond_bb)
6789 return false;
6791 return true;
6794 /* Handle
6795 _4 = x_3 & 31;
6796 if (_4 != 0)
6797 goto <bb 6>;
6798 else
6799 goto <bb 7>;
6800 <bb 6>:
6801 __builtin_unreachable ();
6802 <bb 7>:
6803 x_5 = ASSERT_EXPR <x_3, ...>;
6804 If x_3 has no other immediate uses (checked by caller),
6805 var is the x_3 var from ASSERT_EXPR, we can clear low 5 bits
6806 from the non-zero bitmask. */
6808 static void
6809 maybe_set_nonzero_bits (basic_block bb, tree var)
6811 edge e = single_pred_edge (bb);
6812 basic_block cond_bb = e->src;
6813 gimple stmt = last_stmt (cond_bb);
6814 tree cst;
6816 if (stmt == NULL
6817 || gimple_code (stmt) != GIMPLE_COND
6818 || gimple_cond_code (stmt) != ((e->flags & EDGE_TRUE_VALUE)
6819 ? EQ_EXPR : NE_EXPR)
6820 || TREE_CODE (gimple_cond_lhs (stmt)) != SSA_NAME
6821 || !integer_zerop (gimple_cond_rhs (stmt)))
6822 return;
6824 stmt = SSA_NAME_DEF_STMT (gimple_cond_lhs (stmt));
6825 if (!is_gimple_assign (stmt)
6826 || gimple_assign_rhs_code (stmt) != BIT_AND_EXPR
6827 || TREE_CODE (gimple_assign_rhs2 (stmt)) != INTEGER_CST)
6828 return;
6829 if (gimple_assign_rhs1 (stmt) != var)
6831 gimple stmt2;
6833 if (TREE_CODE (gimple_assign_rhs1 (stmt)) != SSA_NAME)
6834 return;
6835 stmt2 = SSA_NAME_DEF_STMT (gimple_assign_rhs1 (stmt));
6836 if (!gimple_assign_cast_p (stmt2)
6837 || gimple_assign_rhs1 (stmt2) != var
6838 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (stmt2))
6839 || (TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (stmt)))
6840 != TYPE_PRECISION (TREE_TYPE (var))))
6841 return;
6843 cst = gimple_assign_rhs2 (stmt);
6844 set_nonzero_bits (var, wi::bit_and_not (get_nonzero_bits (var), cst));
6847 /* Convert range assertion expressions into the implied copies and
6848 copy propagate away the copies. Doing the trivial copy propagation
6849 here avoids the need to run the full copy propagation pass after
6850 VRP.
6852 FIXME, this will eventually lead to copy propagation removing the
6853 names that had useful range information attached to them. For
6854 instance, if we had the assertion N_i = ASSERT_EXPR <N_j, N_j > 3>,
6855 then N_i will have the range [3, +INF].
6857 However, by converting the assertion into the implied copy
6858 operation N_i = N_j, we will then copy-propagate N_j into the uses
6859 of N_i and lose the range information. We may want to hold on to
6860 ASSERT_EXPRs a little while longer as the ranges could be used in
6861 things like jump threading.
6863 The problem with keeping ASSERT_EXPRs around is that passes after
6864 VRP need to handle them appropriately.
6866 Another approach would be to make the range information a first
6867 class property of the SSA_NAME so that it can be queried from
6868 any pass. This is made somewhat more complex by the need for
6869 multiple ranges to be associated with one SSA_NAME. */
6871 static void
6872 remove_range_assertions (void)
6874 basic_block bb;
6875 gimple_stmt_iterator si;
6876 /* 1 if looking at ASSERT_EXPRs immediately at the beginning of
6877 a basic block preceeded by GIMPLE_COND branching to it and
6878 __builtin_trap, -1 if not yet checked, 0 otherwise. */
6879 int is_unreachable;
6881 /* Note that the BSI iterator bump happens at the bottom of the
6882 loop and no bump is necessary if we're removing the statement
6883 referenced by the current BSI. */
6884 FOR_EACH_BB_FN (bb, cfun)
6885 for (si = gsi_after_labels (bb), is_unreachable = -1; !gsi_end_p (si);)
6887 gimple stmt = gsi_stmt (si);
6888 gimple use_stmt;
6890 if (is_gimple_assign (stmt)
6891 && gimple_assign_rhs_code (stmt) == ASSERT_EXPR)
6893 tree lhs = gimple_assign_lhs (stmt);
6894 tree rhs = gimple_assign_rhs1 (stmt);
6895 tree var;
6896 tree cond = fold (ASSERT_EXPR_COND (rhs));
6897 use_operand_p use_p;
6898 imm_use_iterator iter;
6900 gcc_assert (cond != boolean_false_node);
6902 var = ASSERT_EXPR_VAR (rhs);
6903 gcc_assert (TREE_CODE (var) == SSA_NAME);
6905 if (!POINTER_TYPE_P (TREE_TYPE (lhs))
6906 && SSA_NAME_RANGE_INFO (lhs))
6908 if (is_unreachable == -1)
6910 is_unreachable = 0;
6911 if (single_pred_p (bb)
6912 && assert_unreachable_fallthru_edge_p
6913 (single_pred_edge (bb)))
6914 is_unreachable = 1;
6916 /* Handle
6917 if (x_7 >= 10 && x_7 < 20)
6918 __builtin_unreachable ();
6919 x_8 = ASSERT_EXPR <x_7, ...>;
6920 if the only uses of x_7 are in the ASSERT_EXPR and
6921 in the condition. In that case, we can copy the
6922 range info from x_8 computed in this pass also
6923 for x_7. */
6924 if (is_unreachable
6925 && all_imm_uses_in_stmt_or_feed_cond (var, stmt,
6926 single_pred (bb)))
6928 set_range_info (var, SSA_NAME_RANGE_TYPE (lhs),
6929 SSA_NAME_RANGE_INFO (lhs)->get_min (),
6930 SSA_NAME_RANGE_INFO (lhs)->get_max ());
6931 maybe_set_nonzero_bits (bb, var);
6935 /* Propagate the RHS into every use of the LHS. */
6936 FOR_EACH_IMM_USE_STMT (use_stmt, iter, lhs)
6937 FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
6938 SET_USE (use_p, var);
6940 /* And finally, remove the copy, it is not needed. */
6941 gsi_remove (&si, true);
6942 release_defs (stmt);
6944 else
6946 if (!is_gimple_debug (gsi_stmt (si)))
6947 is_unreachable = 0;
6948 gsi_next (&si);
6954 /* Return true if STMT is interesting for VRP. */
6956 static bool
6957 stmt_interesting_for_vrp (gimple stmt)
6959 if (gimple_code (stmt) == GIMPLE_PHI)
6961 tree res = gimple_phi_result (stmt);
6962 return (!virtual_operand_p (res)
6963 && (INTEGRAL_TYPE_P (TREE_TYPE (res))
6964 || POINTER_TYPE_P (TREE_TYPE (res))));
6966 else if (is_gimple_assign (stmt) || is_gimple_call (stmt))
6968 tree lhs = gimple_get_lhs (stmt);
6970 /* In general, assignments with virtual operands are not useful
6971 for deriving ranges, with the obvious exception of calls to
6972 builtin functions. */
6973 if (lhs && TREE_CODE (lhs) == SSA_NAME
6974 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs))
6975 || POINTER_TYPE_P (TREE_TYPE (lhs)))
6976 && (is_gimple_call (stmt)
6977 || !gimple_vuse (stmt)))
6978 return true;
6979 else if (is_gimple_call (stmt) && gimple_call_internal_p (stmt))
6980 switch (gimple_call_internal_fn (stmt))
6982 case IFN_ADD_OVERFLOW:
6983 case IFN_SUB_OVERFLOW:
6984 case IFN_MUL_OVERFLOW:
6985 /* These internal calls return _Complex integer type,
6986 but are interesting to VRP nevertheless. */
6987 if (lhs && TREE_CODE (lhs) == SSA_NAME)
6988 return true;
6989 break;
6990 default:
6991 break;
6994 else if (gimple_code (stmt) == GIMPLE_COND
6995 || gimple_code (stmt) == GIMPLE_SWITCH)
6996 return true;
6998 return false;
7002 /* Initialize local data structures for VRP. */
7004 static void
7005 vrp_initialize (void)
7007 basic_block bb;
7009 values_propagated = false;
7010 num_vr_values = num_ssa_names;
7011 vr_value = XCNEWVEC (value_range_t *, num_vr_values);
7012 vr_phi_edge_counts = XCNEWVEC (int, num_ssa_names);
7014 FOR_EACH_BB_FN (bb, cfun)
7016 for (gphi_iterator si = gsi_start_phis (bb); !gsi_end_p (si);
7017 gsi_next (&si))
7019 gphi *phi = si.phi ();
7020 if (!stmt_interesting_for_vrp (phi))
7022 tree lhs = PHI_RESULT (phi);
7023 set_value_range_to_varying (get_value_range (lhs));
7024 prop_set_simulate_again (phi, false);
7026 else
7027 prop_set_simulate_again (phi, true);
7030 for (gimple_stmt_iterator si = gsi_start_bb (bb); !gsi_end_p (si);
7031 gsi_next (&si))
7033 gimple stmt = gsi_stmt (si);
7035 /* If the statement is a control insn, then we do not
7036 want to avoid simulating the statement once. Failure
7037 to do so means that those edges will never get added. */
7038 if (stmt_ends_bb_p (stmt))
7039 prop_set_simulate_again (stmt, true);
7040 else if (!stmt_interesting_for_vrp (stmt))
7042 ssa_op_iter i;
7043 tree def;
7044 FOR_EACH_SSA_TREE_OPERAND (def, stmt, i, SSA_OP_DEF)
7045 set_value_range_to_varying (get_value_range (def));
7046 prop_set_simulate_again (stmt, false);
7048 else
7049 prop_set_simulate_again (stmt, true);
7054 /* Return the singleton value-range for NAME or NAME. */
7056 static inline tree
7057 vrp_valueize (tree name)
7059 if (TREE_CODE (name) == SSA_NAME)
7061 value_range_t *vr = get_value_range (name);
7062 if (vr->type == VR_RANGE
7063 && (vr->min == vr->max
7064 || operand_equal_p (vr->min, vr->max, 0)))
7065 return vr->min;
7067 return name;
7070 /* Return the singleton value-range for NAME if that is a constant
7071 but signal to not follow SSA edges. */
7073 static inline tree
7074 vrp_valueize_1 (tree name)
7076 if (TREE_CODE (name) == SSA_NAME)
7078 value_range_t *vr = get_value_range (name);
7079 if (range_int_cst_singleton_p (vr))
7080 return vr->min;
7081 /* If the definition may be simulated again we cannot follow
7082 this SSA edge as the SSA propagator does not necessarily
7083 re-visit the use. */
7084 gimple def_stmt = SSA_NAME_DEF_STMT (name);
7085 if (prop_simulate_again_p (def_stmt))
7086 return NULL_TREE;
7088 return name;
7091 /* Visit assignment STMT. If it produces an interesting range, record
7092 the SSA name in *OUTPUT_P. */
7094 static enum ssa_prop_result
7095 vrp_visit_assignment_or_call (gimple stmt, tree *output_p)
7097 tree def, lhs;
7098 ssa_op_iter iter;
7099 enum gimple_code code = gimple_code (stmt);
7100 lhs = gimple_get_lhs (stmt);
7102 /* We only keep track of ranges in integral and pointer types. */
7103 if (TREE_CODE (lhs) == SSA_NAME
7104 && ((INTEGRAL_TYPE_P (TREE_TYPE (lhs))
7105 /* It is valid to have NULL MIN/MAX values on a type. See
7106 build_range_type. */
7107 && TYPE_MIN_VALUE (TREE_TYPE (lhs))
7108 && TYPE_MAX_VALUE (TREE_TYPE (lhs)))
7109 || POINTER_TYPE_P (TREE_TYPE (lhs))))
7111 value_range_t new_vr = VR_INITIALIZER;
7113 /* Try folding the statement to a constant first. */
7114 tree tem = gimple_fold_stmt_to_constant_1 (stmt, vrp_valueize,
7115 vrp_valueize_1);
7116 if (tem && is_gimple_min_invariant (tem))
7117 set_value_range_to_value (&new_vr, tem, NULL);
7118 /* Then dispatch to value-range extracting functions. */
7119 else if (code == GIMPLE_CALL)
7120 extract_range_basic (&new_vr, stmt);
7121 else
7122 extract_range_from_assignment (&new_vr, as_a <gassign *> (stmt));
7124 if (update_value_range (lhs, &new_vr))
7126 *output_p = lhs;
7128 if (dump_file && (dump_flags & TDF_DETAILS))
7130 fprintf (dump_file, "Found new range for ");
7131 print_generic_expr (dump_file, lhs, 0);
7132 fprintf (dump_file, ": ");
7133 dump_value_range (dump_file, &new_vr);
7134 fprintf (dump_file, "\n");
7137 if (new_vr.type == VR_VARYING)
7138 return SSA_PROP_VARYING;
7140 return SSA_PROP_INTERESTING;
7143 return SSA_PROP_NOT_INTERESTING;
7145 else if (is_gimple_call (stmt) && gimple_call_internal_p (stmt))
7146 switch (gimple_call_internal_fn (stmt))
7148 case IFN_ADD_OVERFLOW:
7149 case IFN_SUB_OVERFLOW:
7150 case IFN_MUL_OVERFLOW:
7151 /* These internal calls return _Complex integer type,
7152 which VRP does not track, but the immediate uses
7153 thereof might be interesting. */
7154 if (lhs && TREE_CODE (lhs) == SSA_NAME)
7156 imm_use_iterator iter;
7157 use_operand_p use_p;
7158 enum ssa_prop_result res = SSA_PROP_VARYING;
7160 set_value_range_to_varying (get_value_range (lhs));
7162 FOR_EACH_IMM_USE_FAST (use_p, iter, lhs)
7164 gimple use_stmt = USE_STMT (use_p);
7165 if (!is_gimple_assign (use_stmt))
7166 continue;
7167 enum tree_code rhs_code = gimple_assign_rhs_code (use_stmt);
7168 if (rhs_code != REALPART_EXPR && rhs_code != IMAGPART_EXPR)
7169 continue;
7170 tree rhs1 = gimple_assign_rhs1 (use_stmt);
7171 tree use_lhs = gimple_assign_lhs (use_stmt);
7172 if (TREE_CODE (rhs1) != rhs_code
7173 || TREE_OPERAND (rhs1, 0) != lhs
7174 || TREE_CODE (use_lhs) != SSA_NAME
7175 || !stmt_interesting_for_vrp (use_stmt)
7176 || (!INTEGRAL_TYPE_P (TREE_TYPE (use_lhs))
7177 || !TYPE_MIN_VALUE (TREE_TYPE (use_lhs))
7178 || !TYPE_MAX_VALUE (TREE_TYPE (use_lhs))))
7179 continue;
7181 /* If there is a change in the value range for any of the
7182 REALPART_EXPR/IMAGPART_EXPR immediate uses, return
7183 SSA_PROP_INTERESTING. If there are any REALPART_EXPR
7184 or IMAGPART_EXPR immediate uses, but none of them have
7185 a change in their value ranges, return
7186 SSA_PROP_NOT_INTERESTING. If there are no
7187 {REAL,IMAG}PART_EXPR uses at all,
7188 return SSA_PROP_VARYING. */
7189 value_range_t new_vr = VR_INITIALIZER;
7190 extract_range_basic (&new_vr, use_stmt);
7191 value_range_t *old_vr = get_value_range (use_lhs);
7192 if (old_vr->type != new_vr.type
7193 || !vrp_operand_equal_p (old_vr->min, new_vr.min)
7194 || !vrp_operand_equal_p (old_vr->max, new_vr.max)
7195 || !vrp_bitmap_equal_p (old_vr->equiv, new_vr.equiv))
7196 res = SSA_PROP_INTERESTING;
7197 else
7198 res = SSA_PROP_NOT_INTERESTING;
7199 BITMAP_FREE (new_vr.equiv);
7200 if (res == SSA_PROP_INTERESTING)
7202 *output_p = lhs;
7203 return res;
7207 return res;
7209 break;
7210 default:
7211 break;
7214 /* Every other statement produces no useful ranges. */
7215 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_DEF)
7216 set_value_range_to_varying (get_value_range (def));
7218 return SSA_PROP_VARYING;
7221 /* Helper that gets the value range of the SSA_NAME with version I
7222 or a symbolic range containing the SSA_NAME only if the value range
7223 is varying or undefined. */
7225 static inline value_range_t
7226 get_vr_for_comparison (int i)
7228 value_range_t vr = *get_value_range (ssa_name (i));
7230 /* If name N_i does not have a valid range, use N_i as its own
7231 range. This allows us to compare against names that may
7232 have N_i in their ranges. */
7233 if (vr.type == VR_VARYING || vr.type == VR_UNDEFINED)
7235 vr.type = VR_RANGE;
7236 vr.min = ssa_name (i);
7237 vr.max = ssa_name (i);
7240 return vr;
7243 /* Compare all the value ranges for names equivalent to VAR with VAL
7244 using comparison code COMP. Return the same value returned by
7245 compare_range_with_value, including the setting of
7246 *STRICT_OVERFLOW_P. */
7248 static tree
7249 compare_name_with_value (enum tree_code comp, tree var, tree val,
7250 bool *strict_overflow_p)
7252 bitmap_iterator bi;
7253 unsigned i;
7254 bitmap e;
7255 tree retval, t;
7256 int used_strict_overflow;
7257 bool sop;
7258 value_range_t equiv_vr;
7260 /* Get the set of equivalences for VAR. */
7261 e = get_value_range (var)->equiv;
7263 /* Start at -1. Set it to 0 if we do a comparison without relying
7264 on overflow, or 1 if all comparisons rely on overflow. */
7265 used_strict_overflow = -1;
7267 /* Compare vars' value range with val. */
7268 equiv_vr = get_vr_for_comparison (SSA_NAME_VERSION (var));
7269 sop = false;
7270 retval = compare_range_with_value (comp, &equiv_vr, val, &sop);
7271 if (retval)
7272 used_strict_overflow = sop ? 1 : 0;
7274 /* If the equiv set is empty we have done all work we need to do. */
7275 if (e == NULL)
7277 if (retval
7278 && used_strict_overflow > 0)
7279 *strict_overflow_p = true;
7280 return retval;
7283 EXECUTE_IF_SET_IN_BITMAP (e, 0, i, bi)
7285 equiv_vr = get_vr_for_comparison (i);
7286 sop = false;
7287 t = compare_range_with_value (comp, &equiv_vr, val, &sop);
7288 if (t)
7290 /* If we get different answers from different members
7291 of the equivalence set this check must be in a dead
7292 code region. Folding it to a trap representation
7293 would be correct here. For now just return don't-know. */
7294 if (retval != NULL
7295 && t != retval)
7297 retval = NULL_TREE;
7298 break;
7300 retval = t;
7302 if (!sop)
7303 used_strict_overflow = 0;
7304 else if (used_strict_overflow < 0)
7305 used_strict_overflow = 1;
7309 if (retval
7310 && used_strict_overflow > 0)
7311 *strict_overflow_p = true;
7313 return retval;
7317 /* Given a comparison code COMP and names N1 and N2, compare all the
7318 ranges equivalent to N1 against all the ranges equivalent to N2
7319 to determine the value of N1 COMP N2. Return the same value
7320 returned by compare_ranges. Set *STRICT_OVERFLOW_P to indicate
7321 whether we relied on an overflow infinity in the comparison. */
7324 static tree
7325 compare_names (enum tree_code comp, tree n1, tree n2,
7326 bool *strict_overflow_p)
7328 tree t, retval;
7329 bitmap e1, e2;
7330 bitmap_iterator bi1, bi2;
7331 unsigned i1, i2;
7332 int used_strict_overflow;
7333 static bitmap_obstack *s_obstack = NULL;
7334 static bitmap s_e1 = NULL, s_e2 = NULL;
7336 /* Compare the ranges of every name equivalent to N1 against the
7337 ranges of every name equivalent to N2. */
7338 e1 = get_value_range (n1)->equiv;
7339 e2 = get_value_range (n2)->equiv;
7341 /* Use the fake bitmaps if e1 or e2 are not available. */
7342 if (s_obstack == NULL)
7344 s_obstack = XNEW (bitmap_obstack);
7345 bitmap_obstack_initialize (s_obstack);
7346 s_e1 = BITMAP_ALLOC (s_obstack);
7347 s_e2 = BITMAP_ALLOC (s_obstack);
7349 if (e1 == NULL)
7350 e1 = s_e1;
7351 if (e2 == NULL)
7352 e2 = s_e2;
7354 /* Add N1 and N2 to their own set of equivalences to avoid
7355 duplicating the body of the loop just to check N1 and N2
7356 ranges. */
7357 bitmap_set_bit (e1, SSA_NAME_VERSION (n1));
7358 bitmap_set_bit (e2, SSA_NAME_VERSION (n2));
7360 /* If the equivalence sets have a common intersection, then the two
7361 names can be compared without checking their ranges. */
7362 if (bitmap_intersect_p (e1, e2))
7364 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
7365 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
7367 return (comp == EQ_EXPR || comp == GE_EXPR || comp == LE_EXPR)
7368 ? boolean_true_node
7369 : boolean_false_node;
7372 /* Start at -1. Set it to 0 if we do a comparison without relying
7373 on overflow, or 1 if all comparisons rely on overflow. */
7374 used_strict_overflow = -1;
7376 /* Otherwise, compare all the equivalent ranges. First, add N1 and
7377 N2 to their own set of equivalences to avoid duplicating the body
7378 of the loop just to check N1 and N2 ranges. */
7379 EXECUTE_IF_SET_IN_BITMAP (e1, 0, i1, bi1)
7381 value_range_t vr1 = get_vr_for_comparison (i1);
7383 t = retval = NULL_TREE;
7384 EXECUTE_IF_SET_IN_BITMAP (e2, 0, i2, bi2)
7386 bool sop = false;
7388 value_range_t vr2 = get_vr_for_comparison (i2);
7390 t = compare_ranges (comp, &vr1, &vr2, &sop);
7391 if (t)
7393 /* If we get different answers from different members
7394 of the equivalence set this check must be in a dead
7395 code region. Folding it to a trap representation
7396 would be correct here. For now just return don't-know. */
7397 if (retval != NULL
7398 && t != retval)
7400 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
7401 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
7402 return NULL_TREE;
7404 retval = t;
7406 if (!sop)
7407 used_strict_overflow = 0;
7408 else if (used_strict_overflow < 0)
7409 used_strict_overflow = 1;
7413 if (retval)
7415 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
7416 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
7417 if (used_strict_overflow > 0)
7418 *strict_overflow_p = true;
7419 return retval;
7423 /* None of the equivalent ranges are useful in computing this
7424 comparison. */
7425 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
7426 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
7427 return NULL_TREE;
7430 /* Helper function for vrp_evaluate_conditional_warnv. */
7432 static tree
7433 vrp_evaluate_conditional_warnv_with_ops_using_ranges (enum tree_code code,
7434 tree op0, tree op1,
7435 bool * strict_overflow_p)
7437 value_range_t *vr0, *vr1;
7439 vr0 = (TREE_CODE (op0) == SSA_NAME) ? get_value_range (op0) : NULL;
7440 vr1 = (TREE_CODE (op1) == SSA_NAME) ? get_value_range (op1) : NULL;
7442 tree res = NULL_TREE;
7443 if (vr0 && vr1)
7444 res = compare_ranges (code, vr0, vr1, strict_overflow_p);
7445 if (!res && vr0)
7446 res = compare_range_with_value (code, vr0, op1, strict_overflow_p);
7447 if (!res && vr1)
7448 res = (compare_range_with_value
7449 (swap_tree_comparison (code), vr1, op0, strict_overflow_p));
7450 return res;
7453 /* Helper function for vrp_evaluate_conditional_warnv. */
7455 static tree
7456 vrp_evaluate_conditional_warnv_with_ops (enum tree_code code, tree op0,
7457 tree op1, bool use_equiv_p,
7458 bool *strict_overflow_p, bool *only_ranges)
7460 tree ret;
7461 if (only_ranges)
7462 *only_ranges = true;
7464 /* We only deal with integral and pointer types. */
7465 if (!INTEGRAL_TYPE_P (TREE_TYPE (op0))
7466 && !POINTER_TYPE_P (TREE_TYPE (op0)))
7467 return NULL_TREE;
7469 if (use_equiv_p)
7471 if (only_ranges
7472 && (ret = vrp_evaluate_conditional_warnv_with_ops_using_ranges
7473 (code, op0, op1, strict_overflow_p)))
7474 return ret;
7475 *only_ranges = false;
7476 if (TREE_CODE (op0) == SSA_NAME && TREE_CODE (op1) == SSA_NAME)
7477 return compare_names (code, op0, op1, strict_overflow_p);
7478 else if (TREE_CODE (op0) == SSA_NAME)
7479 return compare_name_with_value (code, op0, op1, strict_overflow_p);
7480 else if (TREE_CODE (op1) == SSA_NAME)
7481 return (compare_name_with_value
7482 (swap_tree_comparison (code), op1, op0, strict_overflow_p));
7484 else
7485 return vrp_evaluate_conditional_warnv_with_ops_using_ranges (code, op0, op1,
7486 strict_overflow_p);
7487 return NULL_TREE;
7490 /* Given (CODE OP0 OP1) within STMT, try to simplify it based on value range
7491 information. Return NULL if the conditional can not be evaluated.
7492 The ranges of all the names equivalent with the operands in COND
7493 will be used when trying to compute the value. If the result is
7494 based on undefined signed overflow, issue a warning if
7495 appropriate. */
7497 static tree
7498 vrp_evaluate_conditional (enum tree_code code, tree op0, tree op1, gimple stmt)
7500 bool sop;
7501 tree ret;
7502 bool only_ranges;
7504 /* Some passes and foldings leak constants with overflow flag set
7505 into the IL. Avoid doing wrong things with these and bail out. */
7506 if ((TREE_CODE (op0) == INTEGER_CST
7507 && TREE_OVERFLOW (op0))
7508 || (TREE_CODE (op1) == INTEGER_CST
7509 && TREE_OVERFLOW (op1)))
7510 return NULL_TREE;
7512 sop = false;
7513 ret = vrp_evaluate_conditional_warnv_with_ops (code, op0, op1, true, &sop,
7514 &only_ranges);
7516 if (ret && sop)
7518 enum warn_strict_overflow_code wc;
7519 const char* warnmsg;
7521 if (is_gimple_min_invariant (ret))
7523 wc = WARN_STRICT_OVERFLOW_CONDITIONAL;
7524 warnmsg = G_("assuming signed overflow does not occur when "
7525 "simplifying conditional to constant");
7527 else
7529 wc = WARN_STRICT_OVERFLOW_COMPARISON;
7530 warnmsg = G_("assuming signed overflow does not occur when "
7531 "simplifying conditional");
7534 if (issue_strict_overflow_warning (wc))
7536 location_t location;
7538 if (!gimple_has_location (stmt))
7539 location = input_location;
7540 else
7541 location = gimple_location (stmt);
7542 warning_at (location, OPT_Wstrict_overflow, "%s", warnmsg);
7546 if (warn_type_limits
7547 && ret && only_ranges
7548 && TREE_CODE_CLASS (code) == tcc_comparison
7549 && TREE_CODE (op0) == SSA_NAME)
7551 /* If the comparison is being folded and the operand on the LHS
7552 is being compared against a constant value that is outside of
7553 the natural range of OP0's type, then the predicate will
7554 always fold regardless of the value of OP0. If -Wtype-limits
7555 was specified, emit a warning. */
7556 tree type = TREE_TYPE (op0);
7557 value_range_t *vr0 = get_value_range (op0);
7559 if (vr0->type == VR_RANGE
7560 && INTEGRAL_TYPE_P (type)
7561 && vrp_val_is_min (vr0->min)
7562 && vrp_val_is_max (vr0->max)
7563 && is_gimple_min_invariant (op1))
7565 location_t location;
7567 if (!gimple_has_location (stmt))
7568 location = input_location;
7569 else
7570 location = gimple_location (stmt);
7572 warning_at (location, OPT_Wtype_limits,
7573 integer_zerop (ret)
7574 ? G_("comparison always false "
7575 "due to limited range of data type")
7576 : G_("comparison always true "
7577 "due to limited range of data type"));
7581 return ret;
7585 /* Visit conditional statement STMT. If we can determine which edge
7586 will be taken out of STMT's basic block, record it in
7587 *TAKEN_EDGE_P and return SSA_PROP_INTERESTING. Otherwise, return
7588 SSA_PROP_VARYING. */
7590 static enum ssa_prop_result
7591 vrp_visit_cond_stmt (gcond *stmt, edge *taken_edge_p)
7593 tree val;
7594 bool sop;
7596 *taken_edge_p = NULL;
7598 if (dump_file && (dump_flags & TDF_DETAILS))
7600 tree use;
7601 ssa_op_iter i;
7603 fprintf (dump_file, "\nVisiting conditional with predicate: ");
7604 print_gimple_stmt (dump_file, stmt, 0, 0);
7605 fprintf (dump_file, "\nWith known ranges\n");
7607 FOR_EACH_SSA_TREE_OPERAND (use, stmt, i, SSA_OP_USE)
7609 fprintf (dump_file, "\t");
7610 print_generic_expr (dump_file, use, 0);
7611 fprintf (dump_file, ": ");
7612 dump_value_range (dump_file, vr_value[SSA_NAME_VERSION (use)]);
7615 fprintf (dump_file, "\n");
7618 /* Compute the value of the predicate COND by checking the known
7619 ranges of each of its operands.
7621 Note that we cannot evaluate all the equivalent ranges here
7622 because those ranges may not yet be final and with the current
7623 propagation strategy, we cannot determine when the value ranges
7624 of the names in the equivalence set have changed.
7626 For instance, given the following code fragment
7628 i_5 = PHI <8, i_13>
7630 i_14 = ASSERT_EXPR <i_5, i_5 != 0>
7631 if (i_14 == 1)
7634 Assume that on the first visit to i_14, i_5 has the temporary
7635 range [8, 8] because the second argument to the PHI function is
7636 not yet executable. We derive the range ~[0, 0] for i_14 and the
7637 equivalence set { i_5 }. So, when we visit 'if (i_14 == 1)' for
7638 the first time, since i_14 is equivalent to the range [8, 8], we
7639 determine that the predicate is always false.
7641 On the next round of propagation, i_13 is determined to be
7642 VARYING, which causes i_5 to drop down to VARYING. So, another
7643 visit to i_14 is scheduled. In this second visit, we compute the
7644 exact same range and equivalence set for i_14, namely ~[0, 0] and
7645 { i_5 }. But we did not have the previous range for i_5
7646 registered, so vrp_visit_assignment thinks that the range for
7647 i_14 has not changed. Therefore, the predicate 'if (i_14 == 1)'
7648 is not visited again, which stops propagation from visiting
7649 statements in the THEN clause of that if().
7651 To properly fix this we would need to keep the previous range
7652 value for the names in the equivalence set. This way we would've
7653 discovered that from one visit to the other i_5 changed from
7654 range [8, 8] to VR_VARYING.
7656 However, fixing this apparent limitation may not be worth the
7657 additional checking. Testing on several code bases (GCC, DLV,
7658 MICO, TRAMP3D and SPEC2000) showed that doing this results in
7659 4 more predicates folded in SPEC. */
7660 sop = false;
7662 val = vrp_evaluate_conditional_warnv_with_ops (gimple_cond_code (stmt),
7663 gimple_cond_lhs (stmt),
7664 gimple_cond_rhs (stmt),
7665 false, &sop, NULL);
7666 if (val)
7668 if (!sop)
7669 *taken_edge_p = find_taken_edge (gimple_bb (stmt), val);
7670 else
7672 if (dump_file && (dump_flags & TDF_DETAILS))
7673 fprintf (dump_file,
7674 "\nIgnoring predicate evaluation because "
7675 "it assumes that signed overflow is undefined");
7676 val = NULL_TREE;
7680 if (dump_file && (dump_flags & TDF_DETAILS))
7682 fprintf (dump_file, "\nPredicate evaluates to: ");
7683 if (val == NULL_TREE)
7684 fprintf (dump_file, "DON'T KNOW\n");
7685 else
7686 print_generic_stmt (dump_file, val, 0);
7689 return (*taken_edge_p) ? SSA_PROP_INTERESTING : SSA_PROP_VARYING;
7692 /* Searches the case label vector VEC for the index *IDX of the CASE_LABEL
7693 that includes the value VAL. The search is restricted to the range
7694 [START_IDX, n - 1] where n is the size of VEC.
7696 If there is a CASE_LABEL for VAL, its index is placed in IDX and true is
7697 returned.
7699 If there is no CASE_LABEL for VAL and there is one that is larger than VAL,
7700 it is placed in IDX and false is returned.
7702 If VAL is larger than any CASE_LABEL, n is placed on IDX and false is
7703 returned. */
7705 static bool
7706 find_case_label_index (gswitch *stmt, size_t start_idx, tree val, size_t *idx)
7708 size_t n = gimple_switch_num_labels (stmt);
7709 size_t low, high;
7711 /* Find case label for minimum of the value range or the next one.
7712 At each iteration we are searching in [low, high - 1]. */
7714 for (low = start_idx, high = n; high != low; )
7716 tree t;
7717 int cmp;
7718 /* Note that i != high, so we never ask for n. */
7719 size_t i = (high + low) / 2;
7720 t = gimple_switch_label (stmt, i);
7722 /* Cache the result of comparing CASE_LOW and val. */
7723 cmp = tree_int_cst_compare (CASE_LOW (t), val);
7725 if (cmp == 0)
7727 /* Ranges cannot be empty. */
7728 *idx = i;
7729 return true;
7731 else if (cmp > 0)
7732 high = i;
7733 else
7735 low = i + 1;
7736 if (CASE_HIGH (t) != NULL
7737 && tree_int_cst_compare (CASE_HIGH (t), val) >= 0)
7739 *idx = i;
7740 return true;
7745 *idx = high;
7746 return false;
7749 /* Searches the case label vector VEC for the range of CASE_LABELs that is used
7750 for values between MIN and MAX. The first index is placed in MIN_IDX. The
7751 last index is placed in MAX_IDX. If the range of CASE_LABELs is empty
7752 then MAX_IDX < MIN_IDX.
7753 Returns true if the default label is not needed. */
7755 static bool
7756 find_case_label_range (gswitch *stmt, tree min, tree max, size_t *min_idx,
7757 size_t *max_idx)
7759 size_t i, j;
7760 bool min_take_default = !find_case_label_index (stmt, 1, min, &i);
7761 bool max_take_default = !find_case_label_index (stmt, i, max, &j);
7763 if (i == j
7764 && min_take_default
7765 && max_take_default)
7767 /* Only the default case label reached.
7768 Return an empty range. */
7769 *min_idx = 1;
7770 *max_idx = 0;
7771 return false;
7773 else
7775 bool take_default = min_take_default || max_take_default;
7776 tree low, high;
7777 size_t k;
7779 if (max_take_default)
7780 j--;
7782 /* If the case label range is continuous, we do not need
7783 the default case label. Verify that. */
7784 high = CASE_LOW (gimple_switch_label (stmt, i));
7785 if (CASE_HIGH (gimple_switch_label (stmt, i)))
7786 high = CASE_HIGH (gimple_switch_label (stmt, i));
7787 for (k = i + 1; k <= j; ++k)
7789 low = CASE_LOW (gimple_switch_label (stmt, k));
7790 if (!integer_onep (int_const_binop (MINUS_EXPR, low, high)))
7792 take_default = true;
7793 break;
7795 high = low;
7796 if (CASE_HIGH (gimple_switch_label (stmt, k)))
7797 high = CASE_HIGH (gimple_switch_label (stmt, k));
7800 *min_idx = i;
7801 *max_idx = j;
7802 return !take_default;
7806 /* Searches the case label vector VEC for the ranges of CASE_LABELs that are
7807 used in range VR. The indices are placed in MIN_IDX1, MAX_IDX, MIN_IDX2 and
7808 MAX_IDX2. If the ranges of CASE_LABELs are empty then MAX_IDX1 < MIN_IDX1.
7809 Returns true if the default label is not needed. */
7811 static bool
7812 find_case_label_ranges (gswitch *stmt, value_range_t *vr, size_t *min_idx1,
7813 size_t *max_idx1, size_t *min_idx2,
7814 size_t *max_idx2)
7816 size_t i, j, k, l;
7817 unsigned int n = gimple_switch_num_labels (stmt);
7818 bool take_default;
7819 tree case_low, case_high;
7820 tree min = vr->min, max = vr->max;
7822 gcc_checking_assert (vr->type == VR_RANGE || vr->type == VR_ANTI_RANGE);
7824 take_default = !find_case_label_range (stmt, min, max, &i, &j);
7826 /* Set second range to emtpy. */
7827 *min_idx2 = 1;
7828 *max_idx2 = 0;
7830 if (vr->type == VR_RANGE)
7832 *min_idx1 = i;
7833 *max_idx1 = j;
7834 return !take_default;
7837 /* Set first range to all case labels. */
7838 *min_idx1 = 1;
7839 *max_idx1 = n - 1;
7841 if (i > j)
7842 return false;
7844 /* Make sure all the values of case labels [i , j] are contained in
7845 range [MIN, MAX]. */
7846 case_low = CASE_LOW (gimple_switch_label (stmt, i));
7847 case_high = CASE_HIGH (gimple_switch_label (stmt, j));
7848 if (tree_int_cst_compare (case_low, min) < 0)
7849 i += 1;
7850 if (case_high != NULL_TREE
7851 && tree_int_cst_compare (max, case_high) < 0)
7852 j -= 1;
7854 if (i > j)
7855 return false;
7857 /* If the range spans case labels [i, j], the corresponding anti-range spans
7858 the labels [1, i - 1] and [j + 1, n - 1]. */
7859 k = j + 1;
7860 l = n - 1;
7861 if (k > l)
7863 k = 1;
7864 l = 0;
7867 j = i - 1;
7868 i = 1;
7869 if (i > j)
7871 i = k;
7872 j = l;
7873 k = 1;
7874 l = 0;
7877 *min_idx1 = i;
7878 *max_idx1 = j;
7879 *min_idx2 = k;
7880 *max_idx2 = l;
7881 return false;
7884 /* Visit switch statement STMT. If we can determine which edge
7885 will be taken out of STMT's basic block, record it in
7886 *TAKEN_EDGE_P and return SSA_PROP_INTERESTING. Otherwise, return
7887 SSA_PROP_VARYING. */
7889 static enum ssa_prop_result
7890 vrp_visit_switch_stmt (gswitch *stmt, edge *taken_edge_p)
7892 tree op, val;
7893 value_range_t *vr;
7894 size_t i = 0, j = 0, k, l;
7895 bool take_default;
7897 *taken_edge_p = NULL;
7898 op = gimple_switch_index (stmt);
7899 if (TREE_CODE (op) != SSA_NAME)
7900 return SSA_PROP_VARYING;
7902 vr = get_value_range (op);
7903 if (dump_file && (dump_flags & TDF_DETAILS))
7905 fprintf (dump_file, "\nVisiting switch expression with operand ");
7906 print_generic_expr (dump_file, op, 0);
7907 fprintf (dump_file, " with known range ");
7908 dump_value_range (dump_file, vr);
7909 fprintf (dump_file, "\n");
7912 if ((vr->type != VR_RANGE
7913 && vr->type != VR_ANTI_RANGE)
7914 || symbolic_range_p (vr))
7915 return SSA_PROP_VARYING;
7917 /* Find the single edge that is taken from the switch expression. */
7918 take_default = !find_case_label_ranges (stmt, vr, &i, &j, &k, &l);
7920 /* Check if the range spans no CASE_LABEL. If so, we only reach the default
7921 label */
7922 if (j < i)
7924 gcc_assert (take_default);
7925 val = gimple_switch_default_label (stmt);
7927 else
7929 /* Check if labels with index i to j and maybe the default label
7930 are all reaching the same label. */
7932 val = gimple_switch_label (stmt, i);
7933 if (take_default
7934 && CASE_LABEL (gimple_switch_default_label (stmt))
7935 != CASE_LABEL (val))
7937 if (dump_file && (dump_flags & TDF_DETAILS))
7938 fprintf (dump_file, " not a single destination for this "
7939 "range\n");
7940 return SSA_PROP_VARYING;
7942 for (++i; i <= j; ++i)
7944 if (CASE_LABEL (gimple_switch_label (stmt, i)) != CASE_LABEL (val))
7946 if (dump_file && (dump_flags & TDF_DETAILS))
7947 fprintf (dump_file, " not a single destination for this "
7948 "range\n");
7949 return SSA_PROP_VARYING;
7952 for (; k <= l; ++k)
7954 if (CASE_LABEL (gimple_switch_label (stmt, k)) != 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 SSA_PROP_VARYING;
7964 *taken_edge_p = find_edge (gimple_bb (stmt),
7965 label_to_block (CASE_LABEL (val)));
7967 if (dump_file && (dump_flags & TDF_DETAILS))
7969 fprintf (dump_file, " will take edge to ");
7970 print_generic_stmt (dump_file, CASE_LABEL (val), 0);
7973 return SSA_PROP_INTERESTING;
7977 /* Evaluate statement STMT. If the statement produces a useful range,
7978 return SSA_PROP_INTERESTING and record the SSA name with the
7979 interesting range into *OUTPUT_P.
7981 If STMT is a conditional branch and we can determine its truth
7982 value, the taken edge is recorded in *TAKEN_EDGE_P.
7984 If STMT produces a varying value, return SSA_PROP_VARYING. */
7986 static enum ssa_prop_result
7987 vrp_visit_stmt (gimple stmt, edge *taken_edge_p, tree *output_p)
7989 tree def;
7990 ssa_op_iter iter;
7992 if (dump_file && (dump_flags & TDF_DETAILS))
7994 fprintf (dump_file, "\nVisiting statement:\n");
7995 print_gimple_stmt (dump_file, stmt, 0, dump_flags);
7998 if (!stmt_interesting_for_vrp (stmt))
7999 gcc_assert (stmt_ends_bb_p (stmt));
8000 else if (is_gimple_assign (stmt) || is_gimple_call (stmt))
8001 return vrp_visit_assignment_or_call (stmt, output_p);
8002 else if (gimple_code (stmt) == GIMPLE_COND)
8003 return vrp_visit_cond_stmt (as_a <gcond *> (stmt), taken_edge_p);
8004 else if (gimple_code (stmt) == GIMPLE_SWITCH)
8005 return vrp_visit_switch_stmt (as_a <gswitch *> (stmt), taken_edge_p);
8007 /* All other statements produce nothing of interest for VRP, so mark
8008 their outputs varying and prevent further simulation. */
8009 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_DEF)
8010 set_value_range_to_varying (get_value_range (def));
8012 return SSA_PROP_VARYING;
8015 /* Union the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
8016 { VR1TYPE, VR0MIN, VR0MAX } and store the result
8017 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
8018 possible such range. The resulting range is not canonicalized. */
8020 static void
8021 union_ranges (enum value_range_type *vr0type,
8022 tree *vr0min, tree *vr0max,
8023 enum value_range_type vr1type,
8024 tree vr1min, tree vr1max)
8026 bool mineq = operand_equal_p (*vr0min, vr1min, 0);
8027 bool maxeq = operand_equal_p (*vr0max, vr1max, 0);
8029 /* [] is vr0, () is vr1 in the following classification comments. */
8030 if (mineq && maxeq)
8032 /* [( )] */
8033 if (*vr0type == vr1type)
8034 /* Nothing to do for equal ranges. */
8036 else if ((*vr0type == VR_RANGE
8037 && vr1type == VR_ANTI_RANGE)
8038 || (*vr0type == VR_ANTI_RANGE
8039 && vr1type == VR_RANGE))
8041 /* For anti-range with range union the result is varying. */
8042 goto give_up;
8044 else
8045 gcc_unreachable ();
8047 else if (operand_less_p (*vr0max, vr1min) == 1
8048 || operand_less_p (vr1max, *vr0min) == 1)
8050 /* [ ] ( ) or ( ) [ ]
8051 If the ranges have an empty intersection, result of the union
8052 operation is the anti-range or if both are anti-ranges
8053 it covers all. */
8054 if (*vr0type == VR_ANTI_RANGE
8055 && vr1type == VR_ANTI_RANGE)
8056 goto give_up;
8057 else if (*vr0type == VR_ANTI_RANGE
8058 && vr1type == VR_RANGE)
8060 else if (*vr0type == VR_RANGE
8061 && vr1type == VR_ANTI_RANGE)
8063 *vr0type = vr1type;
8064 *vr0min = vr1min;
8065 *vr0max = vr1max;
8067 else if (*vr0type == VR_RANGE
8068 && vr1type == VR_RANGE)
8070 /* The result is the convex hull of both ranges. */
8071 if (operand_less_p (*vr0max, vr1min) == 1)
8073 /* If the result can be an anti-range, create one. */
8074 if (TREE_CODE (*vr0max) == INTEGER_CST
8075 && TREE_CODE (vr1min) == INTEGER_CST
8076 && vrp_val_is_min (*vr0min)
8077 && vrp_val_is_max (vr1max))
8079 tree min = int_const_binop (PLUS_EXPR,
8080 *vr0max,
8081 build_int_cst (TREE_TYPE (*vr0max), 1));
8082 tree max = int_const_binop (MINUS_EXPR,
8083 vr1min,
8084 build_int_cst (TREE_TYPE (vr1min), 1));
8085 if (!operand_less_p (max, min))
8087 *vr0type = VR_ANTI_RANGE;
8088 *vr0min = min;
8089 *vr0max = max;
8091 else
8092 *vr0max = vr1max;
8094 else
8095 *vr0max = vr1max;
8097 else
8099 /* If the result can be an anti-range, create one. */
8100 if (TREE_CODE (vr1max) == INTEGER_CST
8101 && TREE_CODE (*vr0min) == INTEGER_CST
8102 && vrp_val_is_min (vr1min)
8103 && vrp_val_is_max (*vr0max))
8105 tree min = int_const_binop (PLUS_EXPR,
8106 vr1max,
8107 build_int_cst (TREE_TYPE (vr1max), 1));
8108 tree max = int_const_binop (MINUS_EXPR,
8109 *vr0min,
8110 build_int_cst (TREE_TYPE (*vr0min), 1));
8111 if (!operand_less_p (max, min))
8113 *vr0type = VR_ANTI_RANGE;
8114 *vr0min = min;
8115 *vr0max = max;
8117 else
8118 *vr0min = vr1min;
8120 else
8121 *vr0min = vr1min;
8124 else
8125 gcc_unreachable ();
8127 else if ((maxeq || operand_less_p (vr1max, *vr0max) == 1)
8128 && (mineq || operand_less_p (*vr0min, vr1min) == 1))
8130 /* [ ( ) ] or [( ) ] or [ ( )] */
8131 if (*vr0type == VR_RANGE
8132 && vr1type == VR_RANGE)
8134 else if (*vr0type == VR_ANTI_RANGE
8135 && vr1type == VR_ANTI_RANGE)
8137 *vr0type = vr1type;
8138 *vr0min = vr1min;
8139 *vr0max = vr1max;
8141 else if (*vr0type == VR_ANTI_RANGE
8142 && vr1type == VR_RANGE)
8144 /* Arbitrarily choose the right or left gap. */
8145 if (!mineq && TREE_CODE (vr1min) == INTEGER_CST)
8146 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
8147 build_int_cst (TREE_TYPE (vr1min), 1));
8148 else if (!maxeq && TREE_CODE (vr1max) == INTEGER_CST)
8149 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
8150 build_int_cst (TREE_TYPE (vr1max), 1));
8151 else
8152 goto give_up;
8154 else if (*vr0type == VR_RANGE
8155 && vr1type == VR_ANTI_RANGE)
8156 /* The result covers everything. */
8157 goto give_up;
8158 else
8159 gcc_unreachable ();
8161 else if ((maxeq || operand_less_p (*vr0max, vr1max) == 1)
8162 && (mineq || operand_less_p (vr1min, *vr0min) == 1))
8164 /* ( [ ] ) or ([ ] ) or ( [ ]) */
8165 if (*vr0type == VR_RANGE
8166 && vr1type == VR_RANGE)
8168 *vr0type = vr1type;
8169 *vr0min = vr1min;
8170 *vr0max = vr1max;
8172 else if (*vr0type == VR_ANTI_RANGE
8173 && vr1type == VR_ANTI_RANGE)
8175 else if (*vr0type == VR_RANGE
8176 && vr1type == VR_ANTI_RANGE)
8178 *vr0type = VR_ANTI_RANGE;
8179 if (!mineq && TREE_CODE (*vr0min) == INTEGER_CST)
8181 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
8182 build_int_cst (TREE_TYPE (*vr0min), 1));
8183 *vr0min = vr1min;
8185 else if (!maxeq && TREE_CODE (*vr0max) == INTEGER_CST)
8187 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
8188 build_int_cst (TREE_TYPE (*vr0max), 1));
8189 *vr0max = vr1max;
8191 else
8192 goto give_up;
8194 else if (*vr0type == VR_ANTI_RANGE
8195 && vr1type == VR_RANGE)
8196 /* The result covers everything. */
8197 goto give_up;
8198 else
8199 gcc_unreachable ();
8201 else if ((operand_less_p (vr1min, *vr0max) == 1
8202 || operand_equal_p (vr1min, *vr0max, 0))
8203 && operand_less_p (*vr0min, vr1min) == 1
8204 && operand_less_p (*vr0max, vr1max) == 1)
8206 /* [ ( ] ) or [ ]( ) */
8207 if (*vr0type == VR_RANGE
8208 && vr1type == VR_RANGE)
8209 *vr0max = vr1max;
8210 else if (*vr0type == VR_ANTI_RANGE
8211 && vr1type == VR_ANTI_RANGE)
8212 *vr0min = vr1min;
8213 else if (*vr0type == VR_ANTI_RANGE
8214 && vr1type == VR_RANGE)
8216 if (TREE_CODE (vr1min) == INTEGER_CST)
8217 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
8218 build_int_cst (TREE_TYPE (vr1min), 1));
8219 else
8220 goto give_up;
8222 else if (*vr0type == VR_RANGE
8223 && vr1type == VR_ANTI_RANGE)
8225 if (TREE_CODE (*vr0max) == INTEGER_CST)
8227 *vr0type = vr1type;
8228 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
8229 build_int_cst (TREE_TYPE (*vr0max), 1));
8230 *vr0max = vr1max;
8232 else
8233 goto give_up;
8235 else
8236 gcc_unreachable ();
8238 else if ((operand_less_p (*vr0min, vr1max) == 1
8239 || operand_equal_p (*vr0min, vr1max, 0))
8240 && operand_less_p (vr1min, *vr0min) == 1
8241 && operand_less_p (vr1max, *vr0max) == 1)
8243 /* ( [ ) ] or ( )[ ] */
8244 if (*vr0type == VR_RANGE
8245 && vr1type == VR_RANGE)
8246 *vr0min = vr1min;
8247 else if (*vr0type == VR_ANTI_RANGE
8248 && vr1type == VR_ANTI_RANGE)
8249 *vr0max = vr1max;
8250 else if (*vr0type == VR_ANTI_RANGE
8251 && vr1type == VR_RANGE)
8253 if (TREE_CODE (vr1max) == INTEGER_CST)
8254 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
8255 build_int_cst (TREE_TYPE (vr1max), 1));
8256 else
8257 goto give_up;
8259 else if (*vr0type == VR_RANGE
8260 && vr1type == VR_ANTI_RANGE)
8262 if (TREE_CODE (*vr0min) == INTEGER_CST)
8264 *vr0type = vr1type;
8265 *vr0min = vr1min;
8266 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
8267 build_int_cst (TREE_TYPE (*vr0min), 1));
8269 else
8270 goto give_up;
8272 else
8273 gcc_unreachable ();
8275 else
8276 goto give_up;
8278 return;
8280 give_up:
8281 *vr0type = VR_VARYING;
8282 *vr0min = NULL_TREE;
8283 *vr0max = NULL_TREE;
8286 /* Intersect the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
8287 { VR1TYPE, VR0MIN, VR0MAX } and store the result
8288 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
8289 possible such range. The resulting range is not canonicalized. */
8291 static void
8292 intersect_ranges (enum value_range_type *vr0type,
8293 tree *vr0min, tree *vr0max,
8294 enum value_range_type vr1type,
8295 tree vr1min, tree vr1max)
8297 bool mineq = operand_equal_p (*vr0min, vr1min, 0);
8298 bool maxeq = operand_equal_p (*vr0max, vr1max, 0);
8300 /* [] is vr0, () is vr1 in the following classification comments. */
8301 if (mineq && maxeq)
8303 /* [( )] */
8304 if (*vr0type == vr1type)
8305 /* Nothing to do for equal ranges. */
8307 else if ((*vr0type == VR_RANGE
8308 && vr1type == VR_ANTI_RANGE)
8309 || (*vr0type == VR_ANTI_RANGE
8310 && vr1type == VR_RANGE))
8312 /* For anti-range with range intersection the result is empty. */
8313 *vr0type = VR_UNDEFINED;
8314 *vr0min = NULL_TREE;
8315 *vr0max = NULL_TREE;
8317 else
8318 gcc_unreachable ();
8320 else if (operand_less_p (*vr0max, vr1min) == 1
8321 || operand_less_p (vr1max, *vr0min) == 1)
8323 /* [ ] ( ) or ( ) [ ]
8324 If the ranges have an empty intersection, the result of the
8325 intersect operation is the range for intersecting an
8326 anti-range with a range or empty when intersecting two ranges. */
8327 if (*vr0type == VR_RANGE
8328 && vr1type == VR_ANTI_RANGE)
8330 else if (*vr0type == VR_ANTI_RANGE
8331 && vr1type == VR_RANGE)
8333 *vr0type = vr1type;
8334 *vr0min = vr1min;
8335 *vr0max = vr1max;
8337 else if (*vr0type == VR_RANGE
8338 && vr1type == VR_RANGE)
8340 *vr0type = VR_UNDEFINED;
8341 *vr0min = NULL_TREE;
8342 *vr0max = NULL_TREE;
8344 else if (*vr0type == VR_ANTI_RANGE
8345 && vr1type == VR_ANTI_RANGE)
8347 /* If the anti-ranges are adjacent to each other merge them. */
8348 if (TREE_CODE (*vr0max) == INTEGER_CST
8349 && TREE_CODE (vr1min) == INTEGER_CST
8350 && operand_less_p (*vr0max, vr1min) == 1
8351 && integer_onep (int_const_binop (MINUS_EXPR,
8352 vr1min, *vr0max)))
8353 *vr0max = vr1max;
8354 else if (TREE_CODE (vr1max) == INTEGER_CST
8355 && TREE_CODE (*vr0min) == INTEGER_CST
8356 && operand_less_p (vr1max, *vr0min) == 1
8357 && integer_onep (int_const_binop (MINUS_EXPR,
8358 *vr0min, vr1max)))
8359 *vr0min = vr1min;
8360 /* Else arbitrarily take VR0. */
8363 else if ((maxeq || operand_less_p (vr1max, *vr0max) == 1)
8364 && (mineq || operand_less_p (*vr0min, vr1min) == 1))
8366 /* [ ( ) ] or [( ) ] or [ ( )] */
8367 if (*vr0type == VR_RANGE
8368 && vr1type == VR_RANGE)
8370 /* If both are ranges the result is the inner one. */
8371 *vr0type = vr1type;
8372 *vr0min = vr1min;
8373 *vr0max = vr1max;
8375 else if (*vr0type == VR_RANGE
8376 && vr1type == VR_ANTI_RANGE)
8378 /* Choose the right gap if the left one is empty. */
8379 if (mineq)
8381 if (TREE_CODE (vr1max) == INTEGER_CST)
8382 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
8383 build_int_cst (TREE_TYPE (vr1max), 1));
8384 else
8385 *vr0min = vr1max;
8387 /* Choose the left gap if the right one is empty. */
8388 else if (maxeq)
8390 if (TREE_CODE (vr1min) == INTEGER_CST)
8391 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
8392 build_int_cst (TREE_TYPE (vr1min), 1));
8393 else
8394 *vr0max = vr1min;
8396 /* Choose the anti-range if the range is effectively varying. */
8397 else if (vrp_val_is_min (*vr0min)
8398 && vrp_val_is_max (*vr0max))
8400 *vr0type = vr1type;
8401 *vr0min = vr1min;
8402 *vr0max = vr1max;
8404 /* Else choose the range. */
8406 else if (*vr0type == VR_ANTI_RANGE
8407 && vr1type == VR_ANTI_RANGE)
8408 /* If both are anti-ranges the result is the outer one. */
8410 else if (*vr0type == VR_ANTI_RANGE
8411 && vr1type == VR_RANGE)
8413 /* The intersection is empty. */
8414 *vr0type = VR_UNDEFINED;
8415 *vr0min = NULL_TREE;
8416 *vr0max = NULL_TREE;
8418 else
8419 gcc_unreachable ();
8421 else if ((maxeq || operand_less_p (*vr0max, vr1max) == 1)
8422 && (mineq || operand_less_p (vr1min, *vr0min) == 1))
8424 /* ( [ ] ) or ([ ] ) or ( [ ]) */
8425 if (*vr0type == VR_RANGE
8426 && vr1type == VR_RANGE)
8427 /* Choose the inner range. */
8429 else if (*vr0type == VR_ANTI_RANGE
8430 && vr1type == VR_RANGE)
8432 /* Choose the right gap if the left is empty. */
8433 if (mineq)
8435 *vr0type = VR_RANGE;
8436 if (TREE_CODE (*vr0max) == INTEGER_CST)
8437 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
8438 build_int_cst (TREE_TYPE (*vr0max), 1));
8439 else
8440 *vr0min = *vr0max;
8441 *vr0max = vr1max;
8443 /* Choose the left gap if the right is empty. */
8444 else if (maxeq)
8446 *vr0type = VR_RANGE;
8447 if (TREE_CODE (*vr0min) == INTEGER_CST)
8448 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
8449 build_int_cst (TREE_TYPE (*vr0min), 1));
8450 else
8451 *vr0max = *vr0min;
8452 *vr0min = vr1min;
8454 /* Choose the anti-range if the range is effectively varying. */
8455 else if (vrp_val_is_min (vr1min)
8456 && vrp_val_is_max (vr1max))
8458 /* Else choose the range. */
8459 else
8461 *vr0type = vr1type;
8462 *vr0min = vr1min;
8463 *vr0max = vr1max;
8466 else if (*vr0type == VR_ANTI_RANGE
8467 && vr1type == VR_ANTI_RANGE)
8469 /* If both are anti-ranges the result is the outer one. */
8470 *vr0type = vr1type;
8471 *vr0min = vr1min;
8472 *vr0max = vr1max;
8474 else if (vr1type == VR_ANTI_RANGE
8475 && *vr0type == VR_RANGE)
8477 /* The intersection is empty. */
8478 *vr0type = VR_UNDEFINED;
8479 *vr0min = NULL_TREE;
8480 *vr0max = NULL_TREE;
8482 else
8483 gcc_unreachable ();
8485 else if ((operand_less_p (vr1min, *vr0max) == 1
8486 || operand_equal_p (vr1min, *vr0max, 0))
8487 && operand_less_p (*vr0min, vr1min) == 1)
8489 /* [ ( ] ) or [ ]( ) */
8490 if (*vr0type == VR_ANTI_RANGE
8491 && vr1type == VR_ANTI_RANGE)
8492 *vr0max = vr1max;
8493 else if (*vr0type == VR_RANGE
8494 && vr1type == VR_RANGE)
8495 *vr0min = vr1min;
8496 else if (*vr0type == VR_RANGE
8497 && vr1type == VR_ANTI_RANGE)
8499 if (TREE_CODE (vr1min) == INTEGER_CST)
8500 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
8501 build_int_cst (TREE_TYPE (vr1min), 1));
8502 else
8503 *vr0max = vr1min;
8505 else if (*vr0type == VR_ANTI_RANGE
8506 && vr1type == VR_RANGE)
8508 *vr0type = VR_RANGE;
8509 if (TREE_CODE (*vr0max) == INTEGER_CST)
8510 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
8511 build_int_cst (TREE_TYPE (*vr0max), 1));
8512 else
8513 *vr0min = *vr0max;
8514 *vr0max = vr1max;
8516 else
8517 gcc_unreachable ();
8519 else if ((operand_less_p (*vr0min, vr1max) == 1
8520 || operand_equal_p (*vr0min, vr1max, 0))
8521 && operand_less_p (vr1min, *vr0min) == 1)
8523 /* ( [ ) ] or ( )[ ] */
8524 if (*vr0type == VR_ANTI_RANGE
8525 && vr1type == VR_ANTI_RANGE)
8526 *vr0min = vr1min;
8527 else if (*vr0type == VR_RANGE
8528 && vr1type == VR_RANGE)
8529 *vr0max = vr1max;
8530 else if (*vr0type == VR_RANGE
8531 && vr1type == VR_ANTI_RANGE)
8533 if (TREE_CODE (vr1max) == INTEGER_CST)
8534 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
8535 build_int_cst (TREE_TYPE (vr1max), 1));
8536 else
8537 *vr0min = vr1max;
8539 else if (*vr0type == VR_ANTI_RANGE
8540 && vr1type == VR_RANGE)
8542 *vr0type = VR_RANGE;
8543 if (TREE_CODE (*vr0min) == INTEGER_CST)
8544 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
8545 build_int_cst (TREE_TYPE (*vr0min), 1));
8546 else
8547 *vr0max = *vr0min;
8548 *vr0min = vr1min;
8550 else
8551 gcc_unreachable ();
8554 /* As a fallback simply use { *VRTYPE, *VR0MIN, *VR0MAX } as
8555 result for the intersection. That's always a conservative
8556 correct estimate. */
8558 return;
8562 /* Intersect the two value-ranges *VR0 and *VR1 and store the result
8563 in *VR0. This may not be the smallest possible such range. */
8565 static void
8566 vrp_intersect_ranges_1 (value_range_t *vr0, value_range_t *vr1)
8568 value_range_t saved;
8570 /* If either range is VR_VARYING the other one wins. */
8571 if (vr1->type == VR_VARYING)
8572 return;
8573 if (vr0->type == VR_VARYING)
8575 copy_value_range (vr0, vr1);
8576 return;
8579 /* When either range is VR_UNDEFINED the resulting range is
8580 VR_UNDEFINED, too. */
8581 if (vr0->type == VR_UNDEFINED)
8582 return;
8583 if (vr1->type == VR_UNDEFINED)
8585 set_value_range_to_undefined (vr0);
8586 return;
8589 /* Save the original vr0 so we can return it as conservative intersection
8590 result when our worker turns things to varying. */
8591 saved = *vr0;
8592 intersect_ranges (&vr0->type, &vr0->min, &vr0->max,
8593 vr1->type, vr1->min, vr1->max);
8594 /* Make sure to canonicalize the result though as the inversion of a
8595 VR_RANGE can still be a VR_RANGE. */
8596 set_and_canonicalize_value_range (vr0, vr0->type,
8597 vr0->min, vr0->max, vr0->equiv);
8598 /* If that failed, use the saved original VR0. */
8599 if (vr0->type == VR_VARYING)
8601 *vr0 = saved;
8602 return;
8604 /* If the result is VR_UNDEFINED there is no need to mess with
8605 the equivalencies. */
8606 if (vr0->type == VR_UNDEFINED)
8607 return;
8609 /* The resulting set of equivalences for range intersection is the union of
8610 the two sets. */
8611 if (vr0->equiv && vr1->equiv && vr0->equiv != vr1->equiv)
8612 bitmap_ior_into (vr0->equiv, vr1->equiv);
8613 else if (vr1->equiv && !vr0->equiv)
8614 bitmap_copy (vr0->equiv, vr1->equiv);
8617 static void
8618 vrp_intersect_ranges (value_range_t *vr0, value_range_t *vr1)
8620 if (dump_file && (dump_flags & TDF_DETAILS))
8622 fprintf (dump_file, "Intersecting\n ");
8623 dump_value_range (dump_file, vr0);
8624 fprintf (dump_file, "\nand\n ");
8625 dump_value_range (dump_file, vr1);
8626 fprintf (dump_file, "\n");
8628 vrp_intersect_ranges_1 (vr0, vr1);
8629 if (dump_file && (dump_flags & TDF_DETAILS))
8631 fprintf (dump_file, "to\n ");
8632 dump_value_range (dump_file, vr0);
8633 fprintf (dump_file, "\n");
8637 /* Meet operation for value ranges. Given two value ranges VR0 and
8638 VR1, store in VR0 a range that contains both VR0 and VR1. This
8639 may not be the smallest possible such range. */
8641 static void
8642 vrp_meet_1 (value_range_t *vr0, value_range_t *vr1)
8644 value_range_t saved;
8646 if (vr0->type == VR_UNDEFINED)
8648 set_value_range (vr0, vr1->type, vr1->min, vr1->max, vr1->equiv);
8649 return;
8652 if (vr1->type == VR_UNDEFINED)
8654 /* VR0 already has the resulting range. */
8655 return;
8658 if (vr0->type == VR_VARYING)
8660 /* Nothing to do. VR0 already has the resulting range. */
8661 return;
8664 if (vr1->type == VR_VARYING)
8666 set_value_range_to_varying (vr0);
8667 return;
8670 saved = *vr0;
8671 union_ranges (&vr0->type, &vr0->min, &vr0->max,
8672 vr1->type, vr1->min, vr1->max);
8673 if (vr0->type == VR_VARYING)
8675 /* Failed to find an efficient meet. Before giving up and setting
8676 the result to VARYING, see if we can at least derive a useful
8677 anti-range. FIXME, all this nonsense about distinguishing
8678 anti-ranges from ranges is necessary because of the odd
8679 semantics of range_includes_zero_p and friends. */
8680 if (((saved.type == VR_RANGE
8681 && range_includes_zero_p (saved.min, saved.max) == 0)
8682 || (saved.type == VR_ANTI_RANGE
8683 && range_includes_zero_p (saved.min, saved.max) == 1))
8684 && ((vr1->type == VR_RANGE
8685 && range_includes_zero_p (vr1->min, vr1->max) == 0)
8686 || (vr1->type == VR_ANTI_RANGE
8687 && range_includes_zero_p (vr1->min, vr1->max) == 1)))
8689 set_value_range_to_nonnull (vr0, TREE_TYPE (saved.min));
8691 /* Since this meet operation did not result from the meeting of
8692 two equivalent names, VR0 cannot have any equivalences. */
8693 if (vr0->equiv)
8694 bitmap_clear (vr0->equiv);
8695 return;
8698 set_value_range_to_varying (vr0);
8699 return;
8701 set_and_canonicalize_value_range (vr0, vr0->type, vr0->min, vr0->max,
8702 vr0->equiv);
8703 if (vr0->type == VR_VARYING)
8704 return;
8706 /* The resulting set of equivalences is always the intersection of
8707 the two sets. */
8708 if (vr0->equiv && vr1->equiv && vr0->equiv != vr1->equiv)
8709 bitmap_and_into (vr0->equiv, vr1->equiv);
8710 else if (vr0->equiv && !vr1->equiv)
8711 bitmap_clear (vr0->equiv);
8714 static void
8715 vrp_meet (value_range_t *vr0, value_range_t *vr1)
8717 if (dump_file && (dump_flags & TDF_DETAILS))
8719 fprintf (dump_file, "Meeting\n ");
8720 dump_value_range (dump_file, vr0);
8721 fprintf (dump_file, "\nand\n ");
8722 dump_value_range (dump_file, vr1);
8723 fprintf (dump_file, "\n");
8725 vrp_meet_1 (vr0, vr1);
8726 if (dump_file && (dump_flags & TDF_DETAILS))
8728 fprintf (dump_file, "to\n ");
8729 dump_value_range (dump_file, vr0);
8730 fprintf (dump_file, "\n");
8735 /* Visit all arguments for PHI node PHI that flow through executable
8736 edges. If a valid value range can be derived from all the incoming
8737 value ranges, set a new range for the LHS of PHI. */
8739 static enum ssa_prop_result
8740 vrp_visit_phi_node (gphi *phi)
8742 size_t i;
8743 tree lhs = PHI_RESULT (phi);
8744 value_range_t *lhs_vr = get_value_range (lhs);
8745 value_range_t vr_result = VR_INITIALIZER;
8746 bool first = true;
8747 int edges, old_edges;
8748 struct loop *l;
8750 if (dump_file && (dump_flags & TDF_DETAILS))
8752 fprintf (dump_file, "\nVisiting PHI node: ");
8753 print_gimple_stmt (dump_file, phi, 0, dump_flags);
8756 edges = 0;
8757 for (i = 0; i < gimple_phi_num_args (phi); i++)
8759 edge e = gimple_phi_arg_edge (phi, i);
8761 if (dump_file && (dump_flags & TDF_DETAILS))
8763 fprintf (dump_file,
8764 " Argument #%d (%d -> %d %sexecutable)\n",
8765 (int) i, e->src->index, e->dest->index,
8766 (e->flags & EDGE_EXECUTABLE) ? "" : "not ");
8769 if (e->flags & EDGE_EXECUTABLE)
8771 tree arg = PHI_ARG_DEF (phi, i);
8772 value_range_t vr_arg;
8774 ++edges;
8776 if (TREE_CODE (arg) == SSA_NAME)
8778 vr_arg = *(get_value_range (arg));
8779 /* Do not allow equivalences or symbolic ranges to leak in from
8780 backedges. That creates invalid equivalencies.
8781 See PR53465 and PR54767. */
8782 if (e->flags & EDGE_DFS_BACK)
8784 if (vr_arg.type == VR_RANGE
8785 || vr_arg.type == VR_ANTI_RANGE)
8787 vr_arg.equiv = NULL;
8788 if (symbolic_range_p (&vr_arg))
8790 vr_arg.type = VR_VARYING;
8791 vr_arg.min = NULL_TREE;
8792 vr_arg.max = NULL_TREE;
8796 else
8798 /* If the non-backedge arguments range is VR_VARYING then
8799 we can still try recording a simple equivalence. */
8800 if (vr_arg.type == VR_VARYING)
8802 vr_arg.type = VR_RANGE;
8803 vr_arg.min = arg;
8804 vr_arg.max = arg;
8805 vr_arg.equiv = NULL;
8809 else
8811 if (TREE_OVERFLOW_P (arg))
8812 arg = drop_tree_overflow (arg);
8814 vr_arg.type = VR_RANGE;
8815 vr_arg.min = arg;
8816 vr_arg.max = arg;
8817 vr_arg.equiv = NULL;
8820 if (dump_file && (dump_flags & TDF_DETAILS))
8822 fprintf (dump_file, "\t");
8823 print_generic_expr (dump_file, arg, dump_flags);
8824 fprintf (dump_file, ": ");
8825 dump_value_range (dump_file, &vr_arg);
8826 fprintf (dump_file, "\n");
8829 if (first)
8830 copy_value_range (&vr_result, &vr_arg);
8831 else
8832 vrp_meet (&vr_result, &vr_arg);
8833 first = false;
8835 if (vr_result.type == VR_VARYING)
8836 break;
8840 if (vr_result.type == VR_VARYING)
8841 goto varying;
8842 else if (vr_result.type == VR_UNDEFINED)
8843 goto update_range;
8845 old_edges = vr_phi_edge_counts[SSA_NAME_VERSION (lhs)];
8846 vr_phi_edge_counts[SSA_NAME_VERSION (lhs)] = edges;
8848 /* To prevent infinite iterations in the algorithm, derive ranges
8849 when the new value is slightly bigger or smaller than the
8850 previous one. We don't do this if we have seen a new executable
8851 edge; this helps us avoid an overflow infinity for conditionals
8852 which are not in a loop. If the old value-range was VR_UNDEFINED
8853 use the updated range and iterate one more time. */
8854 if (edges > 0
8855 && gimple_phi_num_args (phi) > 1
8856 && edges == old_edges
8857 && lhs_vr->type != VR_UNDEFINED)
8859 /* Compare old and new ranges, fall back to varying if the
8860 values are not comparable. */
8861 int cmp_min = compare_values (lhs_vr->min, vr_result.min);
8862 if (cmp_min == -2)
8863 goto varying;
8864 int cmp_max = compare_values (lhs_vr->max, vr_result.max);
8865 if (cmp_max == -2)
8866 goto varying;
8868 /* For non VR_RANGE or for pointers fall back to varying if
8869 the range changed. */
8870 if ((lhs_vr->type != VR_RANGE || vr_result.type != VR_RANGE
8871 || POINTER_TYPE_P (TREE_TYPE (lhs)))
8872 && (cmp_min != 0 || cmp_max != 0))
8873 goto varying;
8875 /* If the new minimum is larger than than the previous one
8876 retain the old value. If the new minimum value is smaller
8877 than the previous one and not -INF go all the way to -INF + 1.
8878 In the first case, to avoid infinite bouncing between different
8879 minimums, and in the other case to avoid iterating millions of
8880 times to reach -INF. Going to -INF + 1 also lets the following
8881 iteration compute whether there will be any overflow, at the
8882 expense of one additional iteration. */
8883 if (cmp_min < 0)
8884 vr_result.min = lhs_vr->min;
8885 else if (cmp_min > 0
8886 && !vrp_val_is_min (vr_result.min))
8887 vr_result.min
8888 = int_const_binop (PLUS_EXPR,
8889 vrp_val_min (TREE_TYPE (vr_result.min)),
8890 build_int_cst (TREE_TYPE (vr_result.min), 1));
8892 /* Similarly for the maximum value. */
8893 if (cmp_max > 0)
8894 vr_result.max = lhs_vr->max;
8895 else if (cmp_max < 0
8896 && !vrp_val_is_max (vr_result.max))
8897 vr_result.max
8898 = int_const_binop (MINUS_EXPR,
8899 vrp_val_max (TREE_TYPE (vr_result.min)),
8900 build_int_cst (TREE_TYPE (vr_result.min), 1));
8902 /* If we dropped either bound to +-INF then if this is a loop
8903 PHI node SCEV may known more about its value-range. */
8904 if ((cmp_min > 0 || cmp_min < 0
8905 || cmp_max < 0 || cmp_max > 0)
8906 && (l = loop_containing_stmt (phi))
8907 && l->header == gimple_bb (phi))
8908 adjust_range_with_scev (&vr_result, l, phi, lhs);
8910 /* If we will end up with a (-INF, +INF) range, set it to
8911 VARYING. Same if the previous max value was invalid for
8912 the type and we end up with vr_result.min > vr_result.max. */
8913 if ((vrp_val_is_max (vr_result.max)
8914 && vrp_val_is_min (vr_result.min))
8915 || compare_values (vr_result.min,
8916 vr_result.max) > 0)
8917 goto varying;
8920 /* If the new range is different than the previous value, keep
8921 iterating. */
8922 update_range:
8923 if (update_value_range (lhs, &vr_result))
8925 if (dump_file && (dump_flags & TDF_DETAILS))
8927 fprintf (dump_file, "Found new range for ");
8928 print_generic_expr (dump_file, lhs, 0);
8929 fprintf (dump_file, ": ");
8930 dump_value_range (dump_file, &vr_result);
8931 fprintf (dump_file, "\n");
8934 return SSA_PROP_INTERESTING;
8937 /* Nothing changed, don't add outgoing edges. */
8938 return SSA_PROP_NOT_INTERESTING;
8940 /* No match found. Set the LHS to VARYING. */
8941 varying:
8942 set_value_range_to_varying (lhs_vr);
8943 return SSA_PROP_VARYING;
8946 /* Simplify boolean operations if the source is known
8947 to be already a boolean. */
8948 static bool
8949 simplify_truth_ops_using_ranges (gimple_stmt_iterator *gsi, gimple stmt)
8951 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
8952 tree lhs, op0, op1;
8953 bool need_conversion;
8955 /* We handle only !=/== case here. */
8956 gcc_assert (rhs_code == EQ_EXPR || rhs_code == NE_EXPR);
8958 op0 = gimple_assign_rhs1 (stmt);
8959 if (!op_with_boolean_value_range_p (op0))
8960 return false;
8962 op1 = gimple_assign_rhs2 (stmt);
8963 if (!op_with_boolean_value_range_p (op1))
8964 return false;
8966 /* Reduce number of cases to handle to NE_EXPR. As there is no
8967 BIT_XNOR_EXPR we cannot replace A == B with a single statement. */
8968 if (rhs_code == EQ_EXPR)
8970 if (TREE_CODE (op1) == INTEGER_CST)
8971 op1 = int_const_binop (BIT_XOR_EXPR, op1,
8972 build_int_cst (TREE_TYPE (op1), 1));
8973 else
8974 return false;
8977 lhs = gimple_assign_lhs (stmt);
8978 need_conversion
8979 = !useless_type_conversion_p (TREE_TYPE (lhs), TREE_TYPE (op0));
8981 /* Make sure to not sign-extend a 1-bit 1 when converting the result. */
8982 if (need_conversion
8983 && !TYPE_UNSIGNED (TREE_TYPE (op0))
8984 && TYPE_PRECISION (TREE_TYPE (op0)) == 1
8985 && TYPE_PRECISION (TREE_TYPE (lhs)) > 1)
8986 return false;
8988 /* For A != 0 we can substitute A itself. */
8989 if (integer_zerop (op1))
8990 gimple_assign_set_rhs_with_ops (gsi,
8991 need_conversion
8992 ? NOP_EXPR : TREE_CODE (op0), op0);
8993 /* For A != B we substitute A ^ B. Either with conversion. */
8994 else if (need_conversion)
8996 tree tem = make_ssa_name (TREE_TYPE (op0));
8997 gassign *newop
8998 = gimple_build_assign (tem, BIT_XOR_EXPR, op0, op1);
8999 gsi_insert_before (gsi, newop, GSI_SAME_STMT);
9000 gimple_assign_set_rhs_with_ops (gsi, NOP_EXPR, tem);
9002 /* Or without. */
9003 else
9004 gimple_assign_set_rhs_with_ops (gsi, BIT_XOR_EXPR, op0, op1);
9005 update_stmt (gsi_stmt (*gsi));
9007 return true;
9010 /* Simplify a division or modulo operator to a right shift or
9011 bitwise and if the first operand is unsigned or is greater
9012 than zero and the second operand is an exact power of two.
9013 For TRUNC_MOD_EXPR op0 % op1 with constant op1, optimize it
9014 into just op0 if op0's range is known to be a subset of
9015 [-op1 + 1, op1 - 1] for signed and [0, op1 - 1] for unsigned
9016 modulo. */
9018 static bool
9019 simplify_div_or_mod_using_ranges (gimple stmt)
9021 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
9022 tree val = NULL;
9023 tree op0 = gimple_assign_rhs1 (stmt);
9024 tree op1 = gimple_assign_rhs2 (stmt);
9025 value_range_t *vr = get_value_range (op0);
9027 if (rhs_code == TRUNC_MOD_EXPR
9028 && TREE_CODE (op1) == INTEGER_CST
9029 && tree_int_cst_sgn (op1) == 1
9030 && range_int_cst_p (vr)
9031 && tree_int_cst_lt (vr->max, op1))
9033 if (TYPE_UNSIGNED (TREE_TYPE (op0))
9034 || tree_int_cst_sgn (vr->min) >= 0
9035 || tree_int_cst_lt (fold_unary (NEGATE_EXPR, TREE_TYPE (op1), op1),
9036 vr->min))
9038 /* If op0 already has the range op0 % op1 has,
9039 then TRUNC_MOD_EXPR won't change anything. */
9040 gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
9041 gimple_assign_set_rhs_from_tree (&gsi, op0);
9042 update_stmt (stmt);
9043 return true;
9047 if (!integer_pow2p (op1))
9048 return false;
9050 if (TYPE_UNSIGNED (TREE_TYPE (op0)))
9052 val = integer_one_node;
9054 else
9056 bool sop = false;
9058 val = compare_range_with_value (GE_EXPR, vr, integer_zero_node, &sop);
9060 if (val
9061 && sop
9062 && integer_onep (val)
9063 && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC))
9065 location_t location;
9067 if (!gimple_has_location (stmt))
9068 location = input_location;
9069 else
9070 location = gimple_location (stmt);
9071 warning_at (location, OPT_Wstrict_overflow,
9072 "assuming signed overflow does not occur when "
9073 "simplifying %</%> or %<%%%> to %<>>%> or %<&%>");
9077 if (val && integer_onep (val))
9079 tree t;
9081 if (rhs_code == TRUNC_DIV_EXPR)
9083 t = build_int_cst (integer_type_node, tree_log2 (op1));
9084 gimple_assign_set_rhs_code (stmt, RSHIFT_EXPR);
9085 gimple_assign_set_rhs1 (stmt, op0);
9086 gimple_assign_set_rhs2 (stmt, t);
9088 else
9090 t = build_int_cst (TREE_TYPE (op1), 1);
9091 t = int_const_binop (MINUS_EXPR, op1, t);
9092 t = fold_convert (TREE_TYPE (op0), t);
9094 gimple_assign_set_rhs_code (stmt, BIT_AND_EXPR);
9095 gimple_assign_set_rhs1 (stmt, op0);
9096 gimple_assign_set_rhs2 (stmt, t);
9099 update_stmt (stmt);
9100 return true;
9103 return false;
9106 /* If the operand to an ABS_EXPR is >= 0, then eliminate the
9107 ABS_EXPR. If the operand is <= 0, then simplify the
9108 ABS_EXPR into a NEGATE_EXPR. */
9110 static bool
9111 simplify_abs_using_ranges (gimple stmt)
9113 tree val = NULL;
9114 tree op = gimple_assign_rhs1 (stmt);
9115 tree type = TREE_TYPE (op);
9116 value_range_t *vr = get_value_range (op);
9118 if (TYPE_UNSIGNED (type))
9120 val = integer_zero_node;
9122 else if (vr)
9124 bool sop = false;
9126 val = compare_range_with_value (LE_EXPR, vr, integer_zero_node, &sop);
9127 if (!val)
9129 sop = false;
9130 val = compare_range_with_value (GE_EXPR, vr, integer_zero_node,
9131 &sop);
9133 if (val)
9135 if (integer_zerop (val))
9136 val = integer_one_node;
9137 else if (integer_onep (val))
9138 val = integer_zero_node;
9142 if (val
9143 && (integer_onep (val) || integer_zerop (val)))
9145 if (sop && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC))
9147 location_t location;
9149 if (!gimple_has_location (stmt))
9150 location = input_location;
9151 else
9152 location = gimple_location (stmt);
9153 warning_at (location, OPT_Wstrict_overflow,
9154 "assuming signed overflow does not occur when "
9155 "simplifying %<abs (X)%> to %<X%> or %<-X%>");
9158 gimple_assign_set_rhs1 (stmt, op);
9159 if (integer_onep (val))
9160 gimple_assign_set_rhs_code (stmt, NEGATE_EXPR);
9161 else
9162 gimple_assign_set_rhs_code (stmt, SSA_NAME);
9163 update_stmt (stmt);
9164 return true;
9168 return false;
9171 /* Optimize away redundant BIT_AND_EXPR and BIT_IOR_EXPR.
9172 If all the bits that are being cleared by & are already
9173 known to be zero from VR, or all the bits that are being
9174 set by | are already known to be one from VR, the bit
9175 operation is redundant. */
9177 static bool
9178 simplify_bit_ops_using_ranges (gimple_stmt_iterator *gsi, gimple stmt)
9180 tree op0 = gimple_assign_rhs1 (stmt);
9181 tree op1 = gimple_assign_rhs2 (stmt);
9182 tree op = NULL_TREE;
9183 value_range_t vr0 = VR_INITIALIZER;
9184 value_range_t vr1 = VR_INITIALIZER;
9185 wide_int may_be_nonzero0, may_be_nonzero1;
9186 wide_int must_be_nonzero0, must_be_nonzero1;
9187 wide_int mask;
9189 if (TREE_CODE (op0) == SSA_NAME)
9190 vr0 = *(get_value_range (op0));
9191 else if (is_gimple_min_invariant (op0))
9192 set_value_range_to_value (&vr0, op0, NULL);
9193 else
9194 return false;
9196 if (TREE_CODE (op1) == SSA_NAME)
9197 vr1 = *(get_value_range (op1));
9198 else if (is_gimple_min_invariant (op1))
9199 set_value_range_to_value (&vr1, op1, NULL);
9200 else
9201 return false;
9203 if (!zero_nonzero_bits_from_vr (TREE_TYPE (op0), &vr0, &may_be_nonzero0,
9204 &must_be_nonzero0))
9205 return false;
9206 if (!zero_nonzero_bits_from_vr (TREE_TYPE (op1), &vr1, &may_be_nonzero1,
9207 &must_be_nonzero1))
9208 return false;
9210 switch (gimple_assign_rhs_code (stmt))
9212 case BIT_AND_EXPR:
9213 mask = may_be_nonzero0.and_not (must_be_nonzero1);
9214 if (mask == 0)
9216 op = op0;
9217 break;
9219 mask = may_be_nonzero1.and_not (must_be_nonzero0);
9220 if (mask == 0)
9222 op = op1;
9223 break;
9225 break;
9226 case BIT_IOR_EXPR:
9227 mask = may_be_nonzero0.and_not (must_be_nonzero1);
9228 if (mask == 0)
9230 op = op1;
9231 break;
9233 mask = may_be_nonzero1.and_not (must_be_nonzero0);
9234 if (mask == 0)
9236 op = op0;
9237 break;
9239 break;
9240 default:
9241 gcc_unreachable ();
9244 if (op == NULL_TREE)
9245 return false;
9247 gimple_assign_set_rhs_with_ops (gsi, TREE_CODE (op), op);
9248 update_stmt (gsi_stmt (*gsi));
9249 return true;
9252 /* We are comparing trees OP0 and OP1 using COND_CODE. OP0 has
9253 a known value range VR.
9255 If there is one and only one value which will satisfy the
9256 conditional, then return that value. Else return NULL.
9258 If signed overflow must be undefined for the value to satisfy
9259 the conditional, then set *STRICT_OVERFLOW_P to true. */
9261 static tree
9262 test_for_singularity (enum tree_code cond_code, tree op0,
9263 tree op1, value_range_t *vr,
9264 bool *strict_overflow_p)
9266 tree min = NULL;
9267 tree max = NULL;
9269 /* Extract minimum/maximum values which satisfy the
9270 the conditional as it was written. */
9271 if (cond_code == LE_EXPR || cond_code == LT_EXPR)
9273 /* This should not be negative infinity; there is no overflow
9274 here. */
9275 min = TYPE_MIN_VALUE (TREE_TYPE (op0));
9277 max = op1;
9278 if (cond_code == LT_EXPR && !is_overflow_infinity (max))
9280 tree one = build_int_cst (TREE_TYPE (op0), 1);
9281 max = fold_build2 (MINUS_EXPR, TREE_TYPE (op0), max, one);
9282 if (EXPR_P (max))
9283 TREE_NO_WARNING (max) = 1;
9286 else if (cond_code == GE_EXPR || cond_code == GT_EXPR)
9288 /* This should not be positive infinity; there is no overflow
9289 here. */
9290 max = TYPE_MAX_VALUE (TREE_TYPE (op0));
9292 min = op1;
9293 if (cond_code == GT_EXPR && !is_overflow_infinity (min))
9295 tree one = build_int_cst (TREE_TYPE (op0), 1);
9296 min = fold_build2 (PLUS_EXPR, TREE_TYPE (op0), min, one);
9297 if (EXPR_P (min))
9298 TREE_NO_WARNING (min) = 1;
9302 /* Now refine the minimum and maximum values using any
9303 value range information we have for op0. */
9304 if (min && max)
9306 if (compare_values (vr->min, min) == 1)
9307 min = vr->min;
9308 if (compare_values (vr->max, max) == -1)
9309 max = vr->max;
9311 /* If the new min/max values have converged to a single value,
9312 then there is only one value which can satisfy the condition,
9313 return that value. */
9314 if (operand_equal_p (min, max, 0) && is_gimple_min_invariant (min))
9316 if ((cond_code == LE_EXPR || cond_code == LT_EXPR)
9317 && is_overflow_infinity (vr->max))
9318 *strict_overflow_p = true;
9319 if ((cond_code == GE_EXPR || cond_code == GT_EXPR)
9320 && is_overflow_infinity (vr->min))
9321 *strict_overflow_p = true;
9323 return min;
9326 return NULL;
9329 /* Return whether the value range *VR fits in an integer type specified
9330 by PRECISION and UNSIGNED_P. */
9332 static bool
9333 range_fits_type_p (value_range_t *vr, unsigned dest_precision, signop dest_sgn)
9335 tree src_type;
9336 unsigned src_precision;
9337 widest_int tem;
9338 signop src_sgn;
9340 /* We can only handle integral and pointer types. */
9341 src_type = TREE_TYPE (vr->min);
9342 if (!INTEGRAL_TYPE_P (src_type)
9343 && !POINTER_TYPE_P (src_type))
9344 return false;
9346 /* An extension is fine unless VR is SIGNED and dest_sgn is UNSIGNED,
9347 and so is an identity transform. */
9348 src_precision = TYPE_PRECISION (TREE_TYPE (vr->min));
9349 src_sgn = TYPE_SIGN (src_type);
9350 if ((src_precision < dest_precision
9351 && !(dest_sgn == UNSIGNED && src_sgn == SIGNED))
9352 || (src_precision == dest_precision && src_sgn == dest_sgn))
9353 return true;
9355 /* Now we can only handle ranges with constant bounds. */
9356 if (vr->type != VR_RANGE
9357 || TREE_CODE (vr->min) != INTEGER_CST
9358 || TREE_CODE (vr->max) != INTEGER_CST)
9359 return false;
9361 /* For sign changes, the MSB of the wide_int has to be clear.
9362 An unsigned value with its MSB set cannot be represented by
9363 a signed wide_int, while a negative value cannot be represented
9364 by an unsigned wide_int. */
9365 if (src_sgn != dest_sgn
9366 && (wi::lts_p (vr->min, 0) || wi::lts_p (vr->max, 0)))
9367 return false;
9369 /* Then we can perform the conversion on both ends and compare
9370 the result for equality. */
9371 tem = wi::ext (wi::to_widest (vr->min), dest_precision, dest_sgn);
9372 if (tem != wi::to_widest (vr->min))
9373 return false;
9374 tem = wi::ext (wi::to_widest (vr->max), dest_precision, dest_sgn);
9375 if (tem != wi::to_widest (vr->max))
9376 return false;
9378 return true;
9381 /* Simplify a conditional using a relational operator to an equality
9382 test if the range information indicates only one value can satisfy
9383 the original conditional. */
9385 static bool
9386 simplify_cond_using_ranges (gcond *stmt)
9388 tree op0 = gimple_cond_lhs (stmt);
9389 tree op1 = gimple_cond_rhs (stmt);
9390 enum tree_code cond_code = gimple_cond_code (stmt);
9392 if (cond_code != NE_EXPR
9393 && cond_code != EQ_EXPR
9394 && TREE_CODE (op0) == SSA_NAME
9395 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
9396 && is_gimple_min_invariant (op1))
9398 value_range_t *vr = get_value_range (op0);
9400 /* If we have range information for OP0, then we might be
9401 able to simplify this conditional. */
9402 if (vr->type == VR_RANGE)
9404 enum warn_strict_overflow_code wc = WARN_STRICT_OVERFLOW_COMPARISON;
9405 bool sop = false;
9406 tree new_tree = test_for_singularity (cond_code, op0, op1, vr, &sop);
9408 if (new_tree
9409 && (!sop || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0))))
9411 if (dump_file)
9413 fprintf (dump_file, "Simplified relational ");
9414 print_gimple_stmt (dump_file, stmt, 0, 0);
9415 fprintf (dump_file, " into ");
9418 gimple_cond_set_code (stmt, EQ_EXPR);
9419 gimple_cond_set_lhs (stmt, op0);
9420 gimple_cond_set_rhs (stmt, new_tree);
9422 update_stmt (stmt);
9424 if (dump_file)
9426 print_gimple_stmt (dump_file, stmt, 0, 0);
9427 fprintf (dump_file, "\n");
9430 if (sop && issue_strict_overflow_warning (wc))
9432 location_t location = input_location;
9433 if (gimple_has_location (stmt))
9434 location = gimple_location (stmt);
9436 warning_at (location, OPT_Wstrict_overflow,
9437 "assuming signed overflow does not occur when "
9438 "simplifying conditional");
9441 return true;
9444 /* Try again after inverting the condition. We only deal
9445 with integral types here, so no need to worry about
9446 issues with inverting FP comparisons. */
9447 sop = false;
9448 new_tree = test_for_singularity
9449 (invert_tree_comparison (cond_code, false),
9450 op0, op1, vr, &sop);
9452 if (new_tree
9453 && (!sop || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0))))
9455 if (dump_file)
9457 fprintf (dump_file, "Simplified relational ");
9458 print_gimple_stmt (dump_file, stmt, 0, 0);
9459 fprintf (dump_file, " into ");
9462 gimple_cond_set_code (stmt, NE_EXPR);
9463 gimple_cond_set_lhs (stmt, op0);
9464 gimple_cond_set_rhs (stmt, new_tree);
9466 update_stmt (stmt);
9468 if (dump_file)
9470 print_gimple_stmt (dump_file, stmt, 0, 0);
9471 fprintf (dump_file, "\n");
9474 if (sop && issue_strict_overflow_warning (wc))
9476 location_t location = input_location;
9477 if (gimple_has_location (stmt))
9478 location = gimple_location (stmt);
9480 warning_at (location, OPT_Wstrict_overflow,
9481 "assuming signed overflow does not occur when "
9482 "simplifying conditional");
9485 return true;
9490 /* If we have a comparison of an SSA_NAME (OP0) against a constant,
9491 see if OP0 was set by a type conversion where the source of
9492 the conversion is another SSA_NAME with a range that fits
9493 into the range of OP0's type.
9495 If so, the conversion is redundant as the earlier SSA_NAME can be
9496 used for the comparison directly if we just massage the constant in the
9497 comparison. */
9498 if (TREE_CODE (op0) == SSA_NAME
9499 && TREE_CODE (op1) == INTEGER_CST)
9501 gimple def_stmt = SSA_NAME_DEF_STMT (op0);
9502 tree innerop;
9504 if (!is_gimple_assign (def_stmt)
9505 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt)))
9506 return false;
9508 innerop = gimple_assign_rhs1 (def_stmt);
9510 if (TREE_CODE (innerop) == SSA_NAME
9511 && !POINTER_TYPE_P (TREE_TYPE (innerop)))
9513 value_range_t *vr = get_value_range (innerop);
9515 if (range_int_cst_p (vr)
9516 && range_fits_type_p (vr,
9517 TYPE_PRECISION (TREE_TYPE (op0)),
9518 TYPE_SIGN (TREE_TYPE (op0)))
9519 && int_fits_type_p (op1, TREE_TYPE (innerop))
9520 /* The range must not have overflowed, or if it did overflow
9521 we must not be wrapping/trapping overflow and optimizing
9522 with strict overflow semantics. */
9523 && ((!is_negative_overflow_infinity (vr->min)
9524 && !is_positive_overflow_infinity (vr->max))
9525 || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (innerop))))
9527 /* If the range overflowed and the user has asked for warnings
9528 when strict overflow semantics were used to optimize code,
9529 issue an appropriate warning. */
9530 if (cond_code != EQ_EXPR && cond_code != NE_EXPR
9531 && (is_negative_overflow_infinity (vr->min)
9532 || is_positive_overflow_infinity (vr->max))
9533 && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_CONDITIONAL))
9535 location_t location;
9537 if (!gimple_has_location (stmt))
9538 location = input_location;
9539 else
9540 location = gimple_location (stmt);
9541 warning_at (location, OPT_Wstrict_overflow,
9542 "assuming signed overflow does not occur when "
9543 "simplifying conditional");
9546 tree newconst = fold_convert (TREE_TYPE (innerop), op1);
9547 gimple_cond_set_lhs (stmt, innerop);
9548 gimple_cond_set_rhs (stmt, newconst);
9549 return true;
9554 return false;
9557 /* Simplify a switch statement using the value range of the switch
9558 argument. */
9560 static bool
9561 simplify_switch_using_ranges (gswitch *stmt)
9563 tree op = gimple_switch_index (stmt);
9564 value_range_t *vr;
9565 bool take_default;
9566 edge e;
9567 edge_iterator ei;
9568 size_t i = 0, j = 0, n, n2;
9569 tree vec2;
9570 switch_update su;
9571 size_t k = 1, l = 0;
9573 if (TREE_CODE (op) == SSA_NAME)
9575 vr = get_value_range (op);
9577 /* We can only handle integer ranges. */
9578 if ((vr->type != VR_RANGE
9579 && vr->type != VR_ANTI_RANGE)
9580 || symbolic_range_p (vr))
9581 return false;
9583 /* Find case label for min/max of the value range. */
9584 take_default = !find_case_label_ranges (stmt, vr, &i, &j, &k, &l);
9586 else if (TREE_CODE (op) == INTEGER_CST)
9588 take_default = !find_case_label_index (stmt, 1, op, &i);
9589 if (take_default)
9591 i = 1;
9592 j = 0;
9594 else
9596 j = i;
9599 else
9600 return false;
9602 n = gimple_switch_num_labels (stmt);
9604 /* Bail out if this is just all edges taken. */
9605 if (i == 1
9606 && j == n - 1
9607 && take_default)
9608 return false;
9610 /* Build a new vector of taken case labels. */
9611 vec2 = make_tree_vec (j - i + 1 + l - k + 1 + (int)take_default);
9612 n2 = 0;
9614 /* Add the default edge, if necessary. */
9615 if (take_default)
9616 TREE_VEC_ELT (vec2, n2++) = gimple_switch_default_label (stmt);
9618 for (; i <= j; ++i, ++n2)
9619 TREE_VEC_ELT (vec2, n2) = gimple_switch_label (stmt, i);
9621 for (; k <= l; ++k, ++n2)
9622 TREE_VEC_ELT (vec2, n2) = gimple_switch_label (stmt, k);
9624 /* Mark needed edges. */
9625 for (i = 0; i < n2; ++i)
9627 e = find_edge (gimple_bb (stmt),
9628 label_to_block (CASE_LABEL (TREE_VEC_ELT (vec2, i))));
9629 e->aux = (void *)-1;
9632 /* Queue not needed edges for later removal. */
9633 FOR_EACH_EDGE (e, ei, gimple_bb (stmt)->succs)
9635 if (e->aux == (void *)-1)
9637 e->aux = NULL;
9638 continue;
9641 if (dump_file && (dump_flags & TDF_DETAILS))
9643 fprintf (dump_file, "removing unreachable case label\n");
9645 to_remove_edges.safe_push (e);
9646 e->flags &= ~EDGE_EXECUTABLE;
9649 /* And queue an update for the stmt. */
9650 su.stmt = stmt;
9651 su.vec = vec2;
9652 to_update_switch_stmts.safe_push (su);
9653 return false;
9656 /* Simplify an integral conversion from an SSA name in STMT. */
9658 static bool
9659 simplify_conversion_using_ranges (gimple stmt)
9661 tree innerop, middleop, finaltype;
9662 gimple def_stmt;
9663 value_range_t *innervr;
9664 signop inner_sgn, middle_sgn, final_sgn;
9665 unsigned inner_prec, middle_prec, final_prec;
9666 widest_int innermin, innermed, innermax, middlemin, middlemed, middlemax;
9668 finaltype = TREE_TYPE (gimple_assign_lhs (stmt));
9669 if (!INTEGRAL_TYPE_P (finaltype))
9670 return false;
9671 middleop = gimple_assign_rhs1 (stmt);
9672 def_stmt = SSA_NAME_DEF_STMT (middleop);
9673 if (!is_gimple_assign (def_stmt)
9674 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt)))
9675 return false;
9676 innerop = gimple_assign_rhs1 (def_stmt);
9677 if (TREE_CODE (innerop) != SSA_NAME
9678 || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (innerop))
9679 return false;
9681 /* Get the value-range of the inner operand. */
9682 innervr = get_value_range (innerop);
9683 if (innervr->type != VR_RANGE
9684 || TREE_CODE (innervr->min) != INTEGER_CST
9685 || TREE_CODE (innervr->max) != INTEGER_CST)
9686 return false;
9688 /* Simulate the conversion chain to check if the result is equal if
9689 the middle conversion is removed. */
9690 innermin = wi::to_widest (innervr->min);
9691 innermax = wi::to_widest (innervr->max);
9693 inner_prec = TYPE_PRECISION (TREE_TYPE (innerop));
9694 middle_prec = TYPE_PRECISION (TREE_TYPE (middleop));
9695 final_prec = TYPE_PRECISION (finaltype);
9697 /* If the first conversion is not injective, the second must not
9698 be widening. */
9699 if (wi::gtu_p (innermax - innermin,
9700 wi::mask <widest_int> (middle_prec, false))
9701 && middle_prec < final_prec)
9702 return false;
9703 /* We also want a medium value so that we can track the effect that
9704 narrowing conversions with sign change have. */
9705 inner_sgn = TYPE_SIGN (TREE_TYPE (innerop));
9706 if (inner_sgn == UNSIGNED)
9707 innermed = wi::shifted_mask <widest_int> (1, inner_prec - 1, false);
9708 else
9709 innermed = 0;
9710 if (wi::cmp (innermin, innermed, inner_sgn) >= 0
9711 || wi::cmp (innermed, innermax, inner_sgn) >= 0)
9712 innermed = innermin;
9714 middle_sgn = TYPE_SIGN (TREE_TYPE (middleop));
9715 middlemin = wi::ext (innermin, middle_prec, middle_sgn);
9716 middlemed = wi::ext (innermed, middle_prec, middle_sgn);
9717 middlemax = wi::ext (innermax, middle_prec, middle_sgn);
9719 /* Require that the final conversion applied to both the original
9720 and the intermediate range produces the same result. */
9721 final_sgn = TYPE_SIGN (finaltype);
9722 if (wi::ext (middlemin, final_prec, final_sgn)
9723 != wi::ext (innermin, final_prec, final_sgn)
9724 || wi::ext (middlemed, final_prec, final_sgn)
9725 != wi::ext (innermed, final_prec, final_sgn)
9726 || wi::ext (middlemax, final_prec, final_sgn)
9727 != wi::ext (innermax, final_prec, final_sgn))
9728 return false;
9730 gimple_assign_set_rhs1 (stmt, innerop);
9731 update_stmt (stmt);
9732 return true;
9735 /* Simplify a conversion from integral SSA name to float in STMT. */
9737 static bool
9738 simplify_float_conversion_using_ranges (gimple_stmt_iterator *gsi, gimple stmt)
9740 tree rhs1 = gimple_assign_rhs1 (stmt);
9741 value_range_t *vr = get_value_range (rhs1);
9742 machine_mode fltmode = TYPE_MODE (TREE_TYPE (gimple_assign_lhs (stmt)));
9743 machine_mode mode;
9744 tree tem;
9745 gassign *conv;
9747 /* We can only handle constant ranges. */
9748 if (vr->type != VR_RANGE
9749 || TREE_CODE (vr->min) != INTEGER_CST
9750 || TREE_CODE (vr->max) != INTEGER_CST)
9751 return false;
9753 /* First check if we can use a signed type in place of an unsigned. */
9754 if (TYPE_UNSIGNED (TREE_TYPE (rhs1))
9755 && (can_float_p (fltmode, TYPE_MODE (TREE_TYPE (rhs1)), 0)
9756 != CODE_FOR_nothing)
9757 && range_fits_type_p (vr, TYPE_PRECISION (TREE_TYPE (rhs1)), SIGNED))
9758 mode = TYPE_MODE (TREE_TYPE (rhs1));
9759 /* If we can do the conversion in the current input mode do nothing. */
9760 else if (can_float_p (fltmode, TYPE_MODE (TREE_TYPE (rhs1)),
9761 TYPE_UNSIGNED (TREE_TYPE (rhs1))) != CODE_FOR_nothing)
9762 return false;
9763 /* Otherwise search for a mode we can use, starting from the narrowest
9764 integer mode available. */
9765 else
9767 mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
9770 /* If we cannot do a signed conversion to float from mode
9771 or if the value-range does not fit in the signed type
9772 try with a wider mode. */
9773 if (can_float_p (fltmode, mode, 0) != CODE_FOR_nothing
9774 && range_fits_type_p (vr, GET_MODE_PRECISION (mode), SIGNED))
9775 break;
9777 mode = GET_MODE_WIDER_MODE (mode);
9778 /* But do not widen the input. Instead leave that to the
9779 optabs expansion code. */
9780 if (GET_MODE_PRECISION (mode) > TYPE_PRECISION (TREE_TYPE (rhs1)))
9781 return false;
9783 while (mode != VOIDmode);
9784 if (mode == VOIDmode)
9785 return false;
9788 /* It works, insert a truncation or sign-change before the
9789 float conversion. */
9790 tem = make_ssa_name (build_nonstandard_integer_type
9791 (GET_MODE_PRECISION (mode), 0));
9792 conv = gimple_build_assign (tem, NOP_EXPR, rhs1);
9793 gsi_insert_before (gsi, conv, GSI_SAME_STMT);
9794 gimple_assign_set_rhs1 (stmt, tem);
9795 update_stmt (stmt);
9797 return true;
9800 /* Simplify an internal fn call using ranges if possible. */
9802 static bool
9803 simplify_internal_call_using_ranges (gimple_stmt_iterator *gsi, gimple stmt)
9805 enum tree_code subcode;
9806 bool is_ubsan = false;
9807 bool ovf = false;
9808 switch (gimple_call_internal_fn (stmt))
9810 case IFN_UBSAN_CHECK_ADD:
9811 subcode = PLUS_EXPR;
9812 is_ubsan = true;
9813 break;
9814 case IFN_UBSAN_CHECK_SUB:
9815 subcode = MINUS_EXPR;
9816 is_ubsan = true;
9817 break;
9818 case IFN_UBSAN_CHECK_MUL:
9819 subcode = MULT_EXPR;
9820 is_ubsan = true;
9821 break;
9822 case IFN_ADD_OVERFLOW:
9823 subcode = PLUS_EXPR;
9824 break;
9825 case IFN_SUB_OVERFLOW:
9826 subcode = MINUS_EXPR;
9827 break;
9828 case IFN_MUL_OVERFLOW:
9829 subcode = MULT_EXPR;
9830 break;
9831 default:
9832 return false;
9835 tree op0 = gimple_call_arg (stmt, 0);
9836 tree op1 = gimple_call_arg (stmt, 1);
9837 tree type;
9838 if (is_ubsan)
9839 type = TREE_TYPE (op0);
9840 else if (gimple_call_lhs (stmt) == NULL_TREE)
9841 return false;
9842 else
9843 type = TREE_TYPE (TREE_TYPE (gimple_call_lhs (stmt)));
9844 if (!check_for_binary_op_overflow (subcode, type, op0, op1, &ovf)
9845 || (is_ubsan && ovf))
9846 return false;
9848 gimple g;
9849 location_t loc = gimple_location (stmt);
9850 if (is_ubsan)
9851 g = gimple_build_assign (gimple_call_lhs (stmt), subcode, op0, op1);
9852 else
9854 int prec = TYPE_PRECISION (type);
9855 tree utype = type;
9856 if (ovf
9857 || !useless_type_conversion_p (type, TREE_TYPE (op0))
9858 || !useless_type_conversion_p (type, TREE_TYPE (op1)))
9859 utype = build_nonstandard_integer_type (prec, 1);
9860 if (TREE_CODE (op0) == INTEGER_CST)
9861 op0 = fold_convert (utype, op0);
9862 else if (!useless_type_conversion_p (utype, TREE_TYPE (op0)))
9864 g = gimple_build_assign (make_ssa_name (utype), NOP_EXPR, op0);
9865 gimple_set_location (g, loc);
9866 gsi_insert_before (gsi, g, GSI_SAME_STMT);
9867 op0 = gimple_assign_lhs (g);
9869 if (TREE_CODE (op1) == INTEGER_CST)
9870 op1 = fold_convert (utype, op1);
9871 else if (!useless_type_conversion_p (utype, TREE_TYPE (op1)))
9873 g = gimple_build_assign (make_ssa_name (utype), NOP_EXPR, op1);
9874 gimple_set_location (g, loc);
9875 gsi_insert_before (gsi, g, GSI_SAME_STMT);
9876 op1 = gimple_assign_lhs (g);
9878 g = gimple_build_assign (make_ssa_name (utype), subcode, op0, op1);
9879 gimple_set_location (g, loc);
9880 gsi_insert_before (gsi, g, GSI_SAME_STMT);
9881 if (utype != type)
9883 g = gimple_build_assign (make_ssa_name (type), NOP_EXPR,
9884 gimple_assign_lhs (g));
9885 gimple_set_location (g, loc);
9886 gsi_insert_before (gsi, g, GSI_SAME_STMT);
9888 g = gimple_build_assign (gimple_call_lhs (stmt), COMPLEX_EXPR,
9889 gimple_assign_lhs (g),
9890 build_int_cst (type, ovf));
9892 gimple_set_location (g, loc);
9893 gsi_replace (gsi, g, false);
9894 return true;
9897 /* Simplify STMT using ranges if possible. */
9899 static bool
9900 simplify_stmt_using_ranges (gimple_stmt_iterator *gsi)
9902 gimple stmt = gsi_stmt (*gsi);
9903 if (is_gimple_assign (stmt))
9905 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
9906 tree rhs1 = gimple_assign_rhs1 (stmt);
9908 switch (rhs_code)
9910 case EQ_EXPR:
9911 case NE_EXPR:
9912 /* Transform EQ_EXPR, NE_EXPR into BIT_XOR_EXPR or identity
9913 if the RHS is zero or one, and the LHS are known to be boolean
9914 values. */
9915 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9916 return simplify_truth_ops_using_ranges (gsi, stmt);
9917 break;
9919 /* Transform TRUNC_DIV_EXPR and TRUNC_MOD_EXPR into RSHIFT_EXPR
9920 and BIT_AND_EXPR respectively if the first operand is greater
9921 than zero and the second operand is an exact power of two.
9922 Also optimize TRUNC_MOD_EXPR away if the second operand is
9923 constant and the first operand already has the right value
9924 range. */
9925 case TRUNC_DIV_EXPR:
9926 case TRUNC_MOD_EXPR:
9927 if (TREE_CODE (rhs1) == SSA_NAME
9928 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9929 return simplify_div_or_mod_using_ranges (stmt);
9930 break;
9932 /* Transform ABS (X) into X or -X as appropriate. */
9933 case ABS_EXPR:
9934 if (TREE_CODE (rhs1) == SSA_NAME
9935 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9936 return simplify_abs_using_ranges (stmt);
9937 break;
9939 case BIT_AND_EXPR:
9940 case BIT_IOR_EXPR:
9941 /* Optimize away BIT_AND_EXPR and BIT_IOR_EXPR
9942 if all the bits being cleared are already cleared or
9943 all the bits being set are already set. */
9944 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9945 return simplify_bit_ops_using_ranges (gsi, stmt);
9946 break;
9948 CASE_CONVERT:
9949 if (TREE_CODE (rhs1) == SSA_NAME
9950 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9951 return simplify_conversion_using_ranges (stmt);
9952 break;
9954 case FLOAT_EXPR:
9955 if (TREE_CODE (rhs1) == SSA_NAME
9956 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9957 return simplify_float_conversion_using_ranges (gsi, stmt);
9958 break;
9960 default:
9961 break;
9964 else if (gimple_code (stmt) == GIMPLE_COND)
9965 return simplify_cond_using_ranges (as_a <gcond *> (stmt));
9966 else if (gimple_code (stmt) == GIMPLE_SWITCH)
9967 return simplify_switch_using_ranges (as_a <gswitch *> (stmt));
9968 else if (is_gimple_call (stmt)
9969 && gimple_call_internal_p (stmt))
9970 return simplify_internal_call_using_ranges (gsi, stmt);
9972 return false;
9975 /* If the statement pointed by SI has a predicate whose value can be
9976 computed using the value range information computed by VRP, compute
9977 its value and return true. Otherwise, return false. */
9979 static bool
9980 fold_predicate_in (gimple_stmt_iterator *si)
9982 bool assignment_p = false;
9983 tree val;
9984 gimple stmt = gsi_stmt (*si);
9986 if (is_gimple_assign (stmt)
9987 && TREE_CODE_CLASS (gimple_assign_rhs_code (stmt)) == tcc_comparison)
9989 assignment_p = true;
9990 val = vrp_evaluate_conditional (gimple_assign_rhs_code (stmt),
9991 gimple_assign_rhs1 (stmt),
9992 gimple_assign_rhs2 (stmt),
9993 stmt);
9995 else if (gcond *cond_stmt = dyn_cast <gcond *> (stmt))
9996 val = vrp_evaluate_conditional (gimple_cond_code (cond_stmt),
9997 gimple_cond_lhs (cond_stmt),
9998 gimple_cond_rhs (cond_stmt),
9999 stmt);
10000 else
10001 return false;
10003 if (val)
10005 if (assignment_p)
10006 val = fold_convert (gimple_expr_type (stmt), val);
10008 if (dump_file)
10010 fprintf (dump_file, "Folding predicate ");
10011 print_gimple_expr (dump_file, stmt, 0, 0);
10012 fprintf (dump_file, " to ");
10013 print_generic_expr (dump_file, val, 0);
10014 fprintf (dump_file, "\n");
10017 if (is_gimple_assign (stmt))
10018 gimple_assign_set_rhs_from_tree (si, val);
10019 else
10021 gcc_assert (gimple_code (stmt) == GIMPLE_COND);
10022 gcond *cond_stmt = as_a <gcond *> (stmt);
10023 if (integer_zerop (val))
10024 gimple_cond_make_false (cond_stmt);
10025 else if (integer_onep (val))
10026 gimple_cond_make_true (cond_stmt);
10027 else
10028 gcc_unreachable ();
10031 return true;
10034 return false;
10037 /* Callback for substitute_and_fold folding the stmt at *SI. */
10039 static bool
10040 vrp_fold_stmt (gimple_stmt_iterator *si)
10042 if (fold_predicate_in (si))
10043 return true;
10045 return simplify_stmt_using_ranges (si);
10048 /* Stack of dest,src equivalency pairs that need to be restored after
10049 each attempt to thread a block's incoming edge to an outgoing edge.
10051 A NULL entry is used to mark the end of pairs which need to be
10052 restored. */
10053 static vec<tree> equiv_stack;
10055 /* A trivial wrapper so that we can present the generic jump threading
10056 code with a simple API for simplifying statements. STMT is the
10057 statement we want to simplify, WITHIN_STMT provides the location
10058 for any overflow warnings. */
10060 static tree
10061 simplify_stmt_for_jump_threading (gimple stmt, gimple within_stmt)
10063 if (gcond *cond_stmt = dyn_cast <gcond *> (stmt))
10064 return vrp_evaluate_conditional (gimple_cond_code (cond_stmt),
10065 gimple_cond_lhs (cond_stmt),
10066 gimple_cond_rhs (cond_stmt),
10067 within_stmt);
10069 if (gassign *assign_stmt = dyn_cast <gassign *> (stmt))
10071 value_range_t new_vr = VR_INITIALIZER;
10072 tree lhs = gimple_assign_lhs (assign_stmt);
10074 if (TREE_CODE (lhs) == SSA_NAME
10075 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs))
10076 || POINTER_TYPE_P (TREE_TYPE (lhs))))
10078 extract_range_from_assignment (&new_vr, assign_stmt);
10079 if (range_int_cst_singleton_p (&new_vr))
10080 return new_vr.min;
10084 return NULL_TREE;
10087 /* Blocks which have more than one predecessor and more than
10088 one successor present jump threading opportunities, i.e.,
10089 when the block is reached from a specific predecessor, we
10090 may be able to determine which of the outgoing edges will
10091 be traversed. When this optimization applies, we are able
10092 to avoid conditionals at runtime and we may expose secondary
10093 optimization opportunities.
10095 This routine is effectively a driver for the generic jump
10096 threading code. It basically just presents the generic code
10097 with edges that may be suitable for jump threading.
10099 Unlike DOM, we do not iterate VRP if jump threading was successful.
10100 While iterating may expose new opportunities for VRP, it is expected
10101 those opportunities would be very limited and the compile time cost
10102 to expose those opportunities would be significant.
10104 As jump threading opportunities are discovered, they are registered
10105 for later realization. */
10107 static void
10108 identify_jump_threads (void)
10110 basic_block bb;
10111 gcond *dummy;
10112 int i;
10113 edge e;
10115 /* Ugh. When substituting values earlier in this pass we can
10116 wipe the dominance information. So rebuild the dominator
10117 information as we need it within the jump threading code. */
10118 calculate_dominance_info (CDI_DOMINATORS);
10120 /* We do not allow VRP information to be used for jump threading
10121 across a back edge in the CFG. Otherwise it becomes too
10122 difficult to avoid eliminating loop exit tests. Of course
10123 EDGE_DFS_BACK is not accurate at this time so we have to
10124 recompute it. */
10125 mark_dfs_back_edges ();
10127 /* Do not thread across edges we are about to remove. Just marking
10128 them as EDGE_DFS_BACK will do. */
10129 FOR_EACH_VEC_ELT (to_remove_edges, i, e)
10130 e->flags |= EDGE_DFS_BACK;
10132 /* Allocate our unwinder stack to unwind any temporary equivalences
10133 that might be recorded. */
10134 equiv_stack.create (20);
10136 /* To avoid lots of silly node creation, we create a single
10137 conditional and just modify it in-place when attempting to
10138 thread jumps. */
10139 dummy = gimple_build_cond (EQ_EXPR,
10140 integer_zero_node, integer_zero_node,
10141 NULL, NULL);
10143 /* Walk through all the blocks finding those which present a
10144 potential jump threading opportunity. We could set this up
10145 as a dominator walker and record data during the walk, but
10146 I doubt it's worth the effort for the classes of jump
10147 threading opportunities we are trying to identify at this
10148 point in compilation. */
10149 FOR_EACH_BB_FN (bb, cfun)
10151 gimple last;
10153 /* If the generic jump threading code does not find this block
10154 interesting, then there is nothing to do. */
10155 if (! potentially_threadable_block (bb))
10156 continue;
10158 /* We only care about blocks ending in a COND_EXPR. While there
10159 may be some value in handling SWITCH_EXPR here, I doubt it's
10160 terribly important. */
10161 last = gsi_stmt (gsi_last_bb (bb));
10163 /* We're basically looking for a switch or any kind of conditional with
10164 integral or pointer type arguments. Note the type of the second
10165 argument will be the same as the first argument, so no need to
10166 check it explicitly. */
10167 if (gimple_code (last) == GIMPLE_SWITCH
10168 || (gimple_code (last) == GIMPLE_COND
10169 && TREE_CODE (gimple_cond_lhs (last)) == SSA_NAME
10170 && (INTEGRAL_TYPE_P (TREE_TYPE (gimple_cond_lhs (last)))
10171 || POINTER_TYPE_P (TREE_TYPE (gimple_cond_lhs (last))))
10172 && (TREE_CODE (gimple_cond_rhs (last)) == SSA_NAME
10173 || is_gimple_min_invariant (gimple_cond_rhs (last)))))
10175 edge_iterator ei;
10177 /* We've got a block with multiple predecessors and multiple
10178 successors which also ends in a suitable conditional or
10179 switch statement. For each predecessor, see if we can thread
10180 it to a specific successor. */
10181 FOR_EACH_EDGE (e, ei, bb->preds)
10183 /* Do not thread across back edges or abnormal edges
10184 in the CFG. */
10185 if (e->flags & (EDGE_DFS_BACK | EDGE_COMPLEX))
10186 continue;
10188 thread_across_edge (dummy, e, true, &equiv_stack,
10189 simplify_stmt_for_jump_threading);
10194 /* We do not actually update the CFG or SSA graphs at this point as
10195 ASSERT_EXPRs are still in the IL and cfg cleanup code does not yet
10196 handle ASSERT_EXPRs gracefully. */
10199 /* We identified all the jump threading opportunities earlier, but could
10200 not transform the CFG at that time. This routine transforms the
10201 CFG and arranges for the dominator tree to be rebuilt if necessary.
10203 Note the SSA graph update will occur during the normal TODO
10204 processing by the pass manager. */
10205 static void
10206 finalize_jump_threads (void)
10208 thread_through_all_blocks (false);
10209 equiv_stack.release ();
10213 /* Traverse all the blocks folding conditionals with known ranges. */
10215 static void
10216 vrp_finalize (void)
10218 size_t i;
10220 values_propagated = true;
10222 if (dump_file)
10224 fprintf (dump_file, "\nValue ranges after VRP:\n\n");
10225 dump_all_value_ranges (dump_file);
10226 fprintf (dump_file, "\n");
10229 substitute_and_fold (op_with_constant_singleton_value_range,
10230 vrp_fold_stmt, false);
10232 if (warn_array_bounds)
10233 check_all_array_refs ();
10235 /* We must identify jump threading opportunities before we release
10236 the datastructures built by VRP. */
10237 identify_jump_threads ();
10239 /* Set value range to non pointer SSA_NAMEs. */
10240 for (i = 0; i < num_vr_values; i++)
10241 if (vr_value[i])
10243 tree name = ssa_name (i);
10245 if (!name
10246 || POINTER_TYPE_P (TREE_TYPE (name))
10247 || (vr_value[i]->type == VR_VARYING)
10248 || (vr_value[i]->type == VR_UNDEFINED))
10249 continue;
10251 if ((TREE_CODE (vr_value[i]->min) == INTEGER_CST)
10252 && (TREE_CODE (vr_value[i]->max) == INTEGER_CST)
10253 && (vr_value[i]->type == VR_RANGE
10254 || vr_value[i]->type == VR_ANTI_RANGE))
10255 set_range_info (name, vr_value[i]->type, vr_value[i]->min,
10256 vr_value[i]->max);
10259 /* Free allocated memory. */
10260 for (i = 0; i < num_vr_values; i++)
10261 if (vr_value[i])
10263 BITMAP_FREE (vr_value[i]->equiv);
10264 free (vr_value[i]);
10267 free (vr_value);
10268 free (vr_phi_edge_counts);
10270 /* So that we can distinguish between VRP data being available
10271 and not available. */
10272 vr_value = NULL;
10273 vr_phi_edge_counts = NULL;
10277 /* Main entry point to VRP (Value Range Propagation). This pass is
10278 loosely based on J. R. C. Patterson, ``Accurate Static Branch
10279 Prediction by Value Range Propagation,'' in SIGPLAN Conference on
10280 Programming Language Design and Implementation, pp. 67-78, 1995.
10281 Also available at http://citeseer.ist.psu.edu/patterson95accurate.html
10283 This is essentially an SSA-CCP pass modified to deal with ranges
10284 instead of constants.
10286 While propagating ranges, we may find that two or more SSA name
10287 have equivalent, though distinct ranges. For instance,
10289 1 x_9 = p_3->a;
10290 2 p_4 = ASSERT_EXPR <p_3, p_3 != 0>
10291 3 if (p_4 == q_2)
10292 4 p_5 = ASSERT_EXPR <p_4, p_4 == q_2>;
10293 5 endif
10294 6 if (q_2)
10296 In the code above, pointer p_5 has range [q_2, q_2], but from the
10297 code we can also determine that p_5 cannot be NULL and, if q_2 had
10298 a non-varying range, p_5's range should also be compatible with it.
10300 These equivalences are created by two expressions: ASSERT_EXPR and
10301 copy operations. Since p_5 is an assertion on p_4, and p_4 was the
10302 result of another assertion, then we can use the fact that p_5 and
10303 p_4 are equivalent when evaluating p_5's range.
10305 Together with value ranges, we also propagate these equivalences
10306 between names so that we can take advantage of information from
10307 multiple ranges when doing final replacement. Note that this
10308 equivalency relation is transitive but not symmetric.
10310 In the example above, p_5 is equivalent to p_4, q_2 and p_3, but we
10311 cannot assert that q_2 is equivalent to p_5 because q_2 may be used
10312 in contexts where that assertion does not hold (e.g., in line 6).
10314 TODO, the main difference between this pass and Patterson's is that
10315 we do not propagate edge probabilities. We only compute whether
10316 edges can be taken or not. That is, instead of having a spectrum
10317 of jump probabilities between 0 and 1, we only deal with 0, 1 and
10318 DON'T KNOW. In the future, it may be worthwhile to propagate
10319 probabilities to aid branch prediction. */
10321 static unsigned int
10322 execute_vrp (void)
10324 int i;
10325 edge e;
10326 switch_update *su;
10328 loop_optimizer_init (LOOPS_NORMAL | LOOPS_HAVE_RECORDED_EXITS);
10329 rewrite_into_loop_closed_ssa (NULL, TODO_update_ssa);
10330 scev_initialize ();
10332 /* ??? This ends up using stale EDGE_DFS_BACK for liveness computation.
10333 Inserting assertions may split edges which will invalidate
10334 EDGE_DFS_BACK. */
10335 insert_range_assertions ();
10337 to_remove_edges.create (10);
10338 to_update_switch_stmts.create (5);
10339 threadedge_initialize_values ();
10341 /* For visiting PHI nodes we need EDGE_DFS_BACK computed. */
10342 mark_dfs_back_edges ();
10344 vrp_initialize ();
10345 ssa_propagate (vrp_visit_stmt, vrp_visit_phi_node);
10346 vrp_finalize ();
10348 free_numbers_of_iterations_estimates ();
10350 /* ASSERT_EXPRs must be removed before finalizing jump threads
10351 as finalizing jump threads calls the CFG cleanup code which
10352 does not properly handle ASSERT_EXPRs. */
10353 remove_range_assertions ();
10355 /* If we exposed any new variables, go ahead and put them into
10356 SSA form now, before we handle jump threading. This simplifies
10357 interactions between rewriting of _DECL nodes into SSA form
10358 and rewriting SSA_NAME nodes into SSA form after block
10359 duplication and CFG manipulation. */
10360 update_ssa (TODO_update_ssa);
10362 finalize_jump_threads ();
10364 /* Remove dead edges from SWITCH_EXPR optimization. This leaves the
10365 CFG in a broken state and requires a cfg_cleanup run. */
10366 FOR_EACH_VEC_ELT (to_remove_edges, i, e)
10367 remove_edge (e);
10368 /* Update SWITCH_EXPR case label vector. */
10369 FOR_EACH_VEC_ELT (to_update_switch_stmts, i, su)
10371 size_t j;
10372 size_t n = TREE_VEC_LENGTH (su->vec);
10373 tree label;
10374 gimple_switch_set_num_labels (su->stmt, n);
10375 for (j = 0; j < n; j++)
10376 gimple_switch_set_label (su->stmt, j, TREE_VEC_ELT (su->vec, j));
10377 /* As we may have replaced the default label with a regular one
10378 make sure to make it a real default label again. This ensures
10379 optimal expansion. */
10380 label = gimple_switch_label (su->stmt, 0);
10381 CASE_LOW (label) = NULL_TREE;
10382 CASE_HIGH (label) = NULL_TREE;
10385 if (to_remove_edges.length () > 0)
10387 free_dominance_info (CDI_DOMINATORS);
10388 loops_state_set (LOOPS_NEED_FIXUP);
10391 to_remove_edges.release ();
10392 to_update_switch_stmts.release ();
10393 threadedge_finalize_values ();
10395 scev_finalize ();
10396 loop_optimizer_finalize ();
10397 return 0;
10400 namespace {
10402 const pass_data pass_data_vrp =
10404 GIMPLE_PASS, /* type */
10405 "vrp", /* name */
10406 OPTGROUP_NONE, /* optinfo_flags */
10407 TV_TREE_VRP, /* tv_id */
10408 PROP_ssa, /* properties_required */
10409 0, /* properties_provided */
10410 0, /* properties_destroyed */
10411 0, /* todo_flags_start */
10412 ( TODO_cleanup_cfg | TODO_update_ssa ), /* todo_flags_finish */
10415 class pass_vrp : public gimple_opt_pass
10417 public:
10418 pass_vrp (gcc::context *ctxt)
10419 : gimple_opt_pass (pass_data_vrp, ctxt)
10422 /* opt_pass methods: */
10423 opt_pass * clone () { return new pass_vrp (m_ctxt); }
10424 virtual bool gate (function *) { return flag_tree_vrp != 0; }
10425 virtual unsigned int execute (function *) { return execute_vrp (); }
10427 }; // class pass_vrp
10429 } // anon namespace
10431 gimple_opt_pass *
10432 make_pass_vrp (gcc::context *ctxt)
10434 return new pass_vrp (ctxt);