tree-core.h: Include symtab.h.
[official-gcc.git] / gcc / tree-vrp.c
blobdb824c435bffab3044764033e44170178e1888f9
1 /* Support routines for Value Range Propagation (VRP).
2 Copyright (C) 2005-2015 Free Software Foundation, Inc.
3 Contributed by Diego Novillo <dnovillo@redhat.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "backend.h"
25 #include "tree.h"
26 #include "gimple.h"
27 #include "rtl.h"
28 #include "ssa.h"
29 #include "flags.h"
30 #include "alias.h"
31 #include "fold-const.h"
32 #include "stor-layout.h"
33 #include "calls.h"
34 #include "cfganal.h"
35 #include "internal-fn.h"
36 #include "gimple-fold.h"
37 #include "tree-eh.h"
38 #include "gimple-iterator.h"
39 #include "gimple-walk.h"
40 #include "tree-cfg.h"
41 #include "tree-ssa-loop-manip.h"
42 #include "tree-ssa-loop-niter.h"
43 #include "tree-ssa-loop.h"
44 #include "tree-into-ssa.h"
45 #include "tree-ssa.h"
46 #include "tree-pass.h"
47 #include "tree-dump.h"
48 #include "gimple-pretty-print.h"
49 #include "diagnostic-core.h"
50 #include "intl.h"
51 #include "cfgloop.h"
52 #include "tree-scalar-evolution.h"
53 #include "tree-ssa-propagate.h"
54 #include "tree-chrec.h"
55 #include "tree-ssa-threadupdate.h"
56 #include "insn-config.h"
57 #include "expmed.h"
58 #include "dojump.h"
59 #include "explow.h"
60 #include "emit-rtl.h"
61 #include "varasm.h"
62 #include "stmt.h"
63 #include "expr.h"
64 #include "insn-codes.h"
65 #include "optabs.h"
66 #include "tree-ssa-scopedtables.h"
67 #include "tree-ssa-threadedge.h"
71 /* Range of values that can be associated with an SSA_NAME after VRP
72 has executed. */
73 struct value_range_d
75 /* Lattice value represented by this range. */
76 enum value_range_type type;
78 /* Minimum and maximum values represented by this range. These
79 values should be interpreted as follows:
81 - If TYPE is VR_UNDEFINED or VR_VARYING then MIN and MAX must
82 be NULL.
84 - If TYPE == VR_RANGE then MIN holds the minimum value and
85 MAX holds the maximum value of the range [MIN, MAX].
87 - If TYPE == ANTI_RANGE the variable is known to NOT
88 take any values in the range [MIN, MAX]. */
89 tree min;
90 tree max;
92 /* Set of SSA names whose value ranges are equivalent to this one.
93 This set is only valid when TYPE is VR_RANGE or VR_ANTI_RANGE. */
94 bitmap equiv;
97 typedef struct value_range_d value_range_t;
99 #define VR_INITIALIZER { VR_UNDEFINED, NULL_TREE, NULL_TREE, NULL }
101 /* Set of SSA names found live during the RPO traversal of the function
102 for still active basic-blocks. */
103 static sbitmap *live;
105 /* Return true if the SSA name NAME is live on the edge E. */
107 static bool
108 live_on_edge (edge e, tree name)
110 return (live[e->dest->index]
111 && bitmap_bit_p (live[e->dest->index], SSA_NAME_VERSION (name)));
114 /* Local functions. */
115 static int compare_values (tree val1, tree val2);
116 static int compare_values_warnv (tree val1, tree val2, bool *);
117 static void vrp_meet (value_range_t *, value_range_t *);
118 static void vrp_intersect_ranges (value_range_t *, value_range_t *);
119 static tree vrp_evaluate_conditional_warnv_with_ops (enum tree_code,
120 tree, tree, bool, bool *,
121 bool *);
123 /* Location information for ASSERT_EXPRs. Each instance of this
124 structure describes an ASSERT_EXPR for an SSA name. Since a single
125 SSA name may have more than one assertion associated with it, these
126 locations are kept in a linked list attached to the corresponding
127 SSA name. */
128 struct assert_locus_d
130 /* Basic block where the assertion would be inserted. */
131 basic_block bb;
133 /* Some assertions need to be inserted on an edge (e.g., assertions
134 generated by COND_EXPRs). In those cases, BB will be NULL. */
135 edge e;
137 /* Pointer to the statement that generated this assertion. */
138 gimple_stmt_iterator si;
140 /* Predicate code for the ASSERT_EXPR. Must be COMPARISON_CLASS_P. */
141 enum tree_code comp_code;
143 /* Value being compared against. */
144 tree val;
146 /* Expression to compare. */
147 tree expr;
149 /* Next node in the linked list. */
150 struct assert_locus_d *next;
153 typedef struct assert_locus_d *assert_locus_t;
155 /* If bit I is present, it means that SSA name N_i has a list of
156 assertions that should be inserted in the IL. */
157 static bitmap need_assert_for;
159 /* Array of locations lists where to insert assertions. ASSERTS_FOR[I]
160 holds a list of ASSERT_LOCUS_T nodes that describe where
161 ASSERT_EXPRs for SSA name N_I should be inserted. */
162 static assert_locus_t *asserts_for;
164 /* Value range array. After propagation, VR_VALUE[I] holds the range
165 of values that SSA name N_I may take. */
166 static unsigned num_vr_values;
167 static value_range_t **vr_value;
168 static bool values_propagated;
170 /* For a PHI node which sets SSA name N_I, VR_COUNTS[I] holds the
171 number of executable edges we saw the last time we visited the
172 node. */
173 static int *vr_phi_edge_counts;
175 typedef struct {
176 gswitch *stmt;
177 tree vec;
178 } switch_update;
180 static vec<edge> to_remove_edges;
181 static vec<switch_update> to_update_switch_stmts;
184 /* Return the maximum value for TYPE. */
186 static inline tree
187 vrp_val_max (const_tree type)
189 if (!INTEGRAL_TYPE_P (type))
190 return NULL_TREE;
192 return TYPE_MAX_VALUE (type);
195 /* Return the minimum value for TYPE. */
197 static inline tree
198 vrp_val_min (const_tree type)
200 if (!INTEGRAL_TYPE_P (type))
201 return NULL_TREE;
203 return TYPE_MIN_VALUE (type);
206 /* Return whether VAL is equal to the maximum value of its type. This
207 will be true for a positive overflow infinity. We can't do a
208 simple equality comparison with TYPE_MAX_VALUE because C typedefs
209 and Ada subtypes can produce types whose TYPE_MAX_VALUE is not ==
210 to the integer constant with the same value in the type. */
212 static inline bool
213 vrp_val_is_max (const_tree val)
215 tree type_max = vrp_val_max (TREE_TYPE (val));
216 return (val == type_max
217 || (type_max != NULL_TREE
218 && operand_equal_p (val, type_max, 0)));
221 /* Return whether VAL is equal to the minimum value of its type. This
222 will be true for a negative overflow infinity. */
224 static inline bool
225 vrp_val_is_min (const_tree val)
227 tree type_min = vrp_val_min (TREE_TYPE (val));
228 return (val == type_min
229 || (type_min != NULL_TREE
230 && operand_equal_p (val, type_min, 0)));
234 /* Return whether TYPE should use an overflow infinity distinct from
235 TYPE_{MIN,MAX}_VALUE. We use an overflow infinity value to
236 represent a signed overflow during VRP computations. An infinity
237 is distinct from a half-range, which will go from some number to
238 TYPE_{MIN,MAX}_VALUE. */
240 static inline bool
241 needs_overflow_infinity (const_tree type)
243 return INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_WRAPS (type);
246 /* Return whether TYPE can support our overflow infinity
247 representation: we use the TREE_OVERFLOW flag, which only exists
248 for constants. If TYPE doesn't support this, we don't optimize
249 cases which would require signed overflow--we drop them to
250 VARYING. */
252 static inline bool
253 supports_overflow_infinity (const_tree type)
255 tree min = vrp_val_min (type), max = vrp_val_max (type);
256 #ifdef ENABLE_CHECKING
257 gcc_assert (needs_overflow_infinity (type));
258 #endif
259 return (min != NULL_TREE
260 && CONSTANT_CLASS_P (min)
261 && max != NULL_TREE
262 && CONSTANT_CLASS_P (max));
265 /* VAL is the maximum or minimum value of a type. Return a
266 corresponding overflow infinity. */
268 static inline tree
269 make_overflow_infinity (tree val)
271 gcc_checking_assert (val != NULL_TREE && CONSTANT_CLASS_P (val));
272 val = copy_node (val);
273 TREE_OVERFLOW (val) = 1;
274 return val;
277 /* Return a negative overflow infinity for TYPE. */
279 static inline tree
280 negative_overflow_infinity (tree type)
282 gcc_checking_assert (supports_overflow_infinity (type));
283 return make_overflow_infinity (vrp_val_min (type));
286 /* Return a positive overflow infinity for TYPE. */
288 static inline tree
289 positive_overflow_infinity (tree type)
291 gcc_checking_assert (supports_overflow_infinity (type));
292 return make_overflow_infinity (vrp_val_max (type));
295 /* Return whether VAL is a negative overflow infinity. */
297 static inline bool
298 is_negative_overflow_infinity (const_tree val)
300 return (TREE_OVERFLOW_P (val)
301 && needs_overflow_infinity (TREE_TYPE (val))
302 && vrp_val_is_min (val));
305 /* Return whether VAL is a positive overflow infinity. */
307 static inline bool
308 is_positive_overflow_infinity (const_tree val)
310 return (TREE_OVERFLOW_P (val)
311 && needs_overflow_infinity (TREE_TYPE (val))
312 && vrp_val_is_max (val));
315 /* Return whether VAL is a positive or negative overflow infinity. */
317 static inline bool
318 is_overflow_infinity (const_tree val)
320 return (TREE_OVERFLOW_P (val)
321 && needs_overflow_infinity (TREE_TYPE (val))
322 && (vrp_val_is_min (val) || vrp_val_is_max (val)));
325 /* Return whether STMT has a constant rhs that is_overflow_infinity. */
327 static inline bool
328 stmt_overflow_infinity (gimple stmt)
330 if (is_gimple_assign (stmt)
331 && get_gimple_rhs_class (gimple_assign_rhs_code (stmt)) ==
332 GIMPLE_SINGLE_RHS)
333 return is_overflow_infinity (gimple_assign_rhs1 (stmt));
334 return false;
337 /* If VAL is now an overflow infinity, return VAL. Otherwise, return
338 the same value with TREE_OVERFLOW clear. This can be used to avoid
339 confusing a regular value with an overflow value. */
341 static inline tree
342 avoid_overflow_infinity (tree val)
344 if (!is_overflow_infinity (val))
345 return val;
347 if (vrp_val_is_max (val))
348 return vrp_val_max (TREE_TYPE (val));
349 else
351 gcc_checking_assert (vrp_val_is_min (val));
352 return vrp_val_min (TREE_TYPE (val));
357 /* Return true if ARG is marked with the nonnull attribute in the
358 current function signature. */
360 static bool
361 nonnull_arg_p (const_tree arg)
363 tree t, attrs, fntype;
364 unsigned HOST_WIDE_INT arg_num;
366 gcc_assert (TREE_CODE (arg) == PARM_DECL && POINTER_TYPE_P (TREE_TYPE (arg)));
368 /* The static chain decl is always non null. */
369 if (arg == cfun->static_chain_decl)
370 return true;
372 /* THIS argument of method is always non-NULL. */
373 if (TREE_CODE (TREE_TYPE (current_function_decl)) == METHOD_TYPE
374 && arg == DECL_ARGUMENTS (current_function_decl)
375 && flag_delete_null_pointer_checks)
376 return true;
378 /* Values passed by reference are always non-NULL. */
379 if (TREE_CODE (TREE_TYPE (arg)) == REFERENCE_TYPE
380 && flag_delete_null_pointer_checks)
381 return true;
383 fntype = TREE_TYPE (current_function_decl);
384 for (attrs = TYPE_ATTRIBUTES (fntype); attrs; attrs = TREE_CHAIN (attrs))
386 attrs = lookup_attribute ("nonnull", attrs);
388 /* If "nonnull" wasn't specified, we know nothing about the argument. */
389 if (attrs == NULL_TREE)
390 return false;
392 /* If "nonnull" applies to all the arguments, then ARG is non-null. */
393 if (TREE_VALUE (attrs) == NULL_TREE)
394 return true;
396 /* Get the position number for ARG in the function signature. */
397 for (arg_num = 1, t = DECL_ARGUMENTS (current_function_decl);
399 t = DECL_CHAIN (t), arg_num++)
401 if (t == arg)
402 break;
405 gcc_assert (t == arg);
407 /* Now see if ARG_NUM is mentioned in the nonnull list. */
408 for (t = TREE_VALUE (attrs); t; t = TREE_CHAIN (t))
410 if (compare_tree_int (TREE_VALUE (t), arg_num) == 0)
411 return true;
415 return false;
419 /* Set value range VR to VR_UNDEFINED. */
421 static inline void
422 set_value_range_to_undefined (value_range_t *vr)
424 vr->type = VR_UNDEFINED;
425 vr->min = vr->max = NULL_TREE;
426 if (vr->equiv)
427 bitmap_clear (vr->equiv);
431 /* Set value range VR to VR_VARYING. */
433 static inline void
434 set_value_range_to_varying (value_range_t *vr)
436 vr->type = VR_VARYING;
437 vr->min = vr->max = NULL_TREE;
438 if (vr->equiv)
439 bitmap_clear (vr->equiv);
443 /* Set value range VR to {T, MIN, MAX, EQUIV}. */
445 static void
446 set_value_range (value_range_t *vr, enum value_range_type t, tree min,
447 tree max, bitmap equiv)
449 #if defined ENABLE_CHECKING
450 /* Check the validity of the range. */
451 if (t == VR_RANGE || t == VR_ANTI_RANGE)
453 int cmp;
455 gcc_assert (min && max);
457 gcc_assert ((!TREE_OVERFLOW_P (min) || is_overflow_infinity (min))
458 && (!TREE_OVERFLOW_P (max) || is_overflow_infinity (max)));
460 if (INTEGRAL_TYPE_P (TREE_TYPE (min)) && t == VR_ANTI_RANGE)
461 gcc_assert (!vrp_val_is_min (min) || !vrp_val_is_max (max));
463 cmp = compare_values (min, max);
464 gcc_assert (cmp == 0 || cmp == -1 || cmp == -2);
466 if (needs_overflow_infinity (TREE_TYPE (min)))
467 gcc_assert (!is_overflow_infinity (min)
468 || !is_overflow_infinity (max));
471 if (t == VR_UNDEFINED || t == VR_VARYING)
472 gcc_assert (min == NULL_TREE && max == NULL_TREE);
474 if (t == VR_UNDEFINED || t == VR_VARYING)
475 gcc_assert (equiv == NULL || bitmap_empty_p (equiv));
476 #endif
478 vr->type = t;
479 vr->min = min;
480 vr->max = max;
482 /* Since updating the equivalence set involves deep copying the
483 bitmaps, only do it if absolutely necessary. */
484 if (vr->equiv == NULL
485 && equiv != NULL)
486 vr->equiv = BITMAP_ALLOC (NULL);
488 if (equiv != vr->equiv)
490 if (equiv && !bitmap_empty_p (equiv))
491 bitmap_copy (vr->equiv, equiv);
492 else
493 bitmap_clear (vr->equiv);
498 /* Set value range VR to the canonical form of {T, MIN, MAX, EQUIV}.
499 This means adjusting T, MIN and MAX representing the case of a
500 wrapping range with MAX < MIN covering [MIN, type_max] U [type_min, MAX]
501 as anti-rage ~[MAX+1, MIN-1]. Likewise for wrapping anti-ranges.
502 In corner cases where MAX+1 or MIN-1 wraps this will fall back
503 to varying.
504 This routine exists to ease canonicalization in the case where we
505 extract ranges from var + CST op limit. */
507 static void
508 set_and_canonicalize_value_range (value_range_t *vr, enum value_range_type t,
509 tree min, tree max, bitmap equiv)
511 /* Use the canonical setters for VR_UNDEFINED and VR_VARYING. */
512 if (t == VR_UNDEFINED)
514 set_value_range_to_undefined (vr);
515 return;
517 else if (t == VR_VARYING)
519 set_value_range_to_varying (vr);
520 return;
523 /* Nothing to canonicalize for symbolic ranges. */
524 if (TREE_CODE (min) != INTEGER_CST
525 || TREE_CODE (max) != INTEGER_CST)
527 set_value_range (vr, t, min, max, equiv);
528 return;
531 /* Wrong order for min and max, to swap them and the VR type we need
532 to adjust them. */
533 if (tree_int_cst_lt (max, min))
535 tree one, tmp;
537 /* For one bit precision if max < min, then the swapped
538 range covers all values, so for VR_RANGE it is varying and
539 for VR_ANTI_RANGE empty range, so drop to varying as well. */
540 if (TYPE_PRECISION (TREE_TYPE (min)) == 1)
542 set_value_range_to_varying (vr);
543 return;
546 one = build_int_cst (TREE_TYPE (min), 1);
547 tmp = int_const_binop (PLUS_EXPR, max, one);
548 max = int_const_binop (MINUS_EXPR, min, one);
549 min = tmp;
551 /* There's one corner case, if we had [C+1, C] before we now have
552 that again. But this represents an empty value range, so drop
553 to varying in this case. */
554 if (tree_int_cst_lt (max, min))
556 set_value_range_to_varying (vr);
557 return;
560 t = t == VR_RANGE ? VR_ANTI_RANGE : VR_RANGE;
563 /* Anti-ranges that can be represented as ranges should be so. */
564 if (t == VR_ANTI_RANGE)
566 bool is_min = vrp_val_is_min (min);
567 bool is_max = vrp_val_is_max (max);
569 if (is_min && is_max)
571 /* We cannot deal with empty ranges, drop to varying.
572 ??? This could be VR_UNDEFINED instead. */
573 set_value_range_to_varying (vr);
574 return;
576 else if (TYPE_PRECISION (TREE_TYPE (min)) == 1
577 && (is_min || is_max))
579 /* Non-empty boolean ranges can always be represented
580 as a singleton range. */
581 if (is_min)
582 min = max = vrp_val_max (TREE_TYPE (min));
583 else
584 min = max = vrp_val_min (TREE_TYPE (min));
585 t = VR_RANGE;
587 else if (is_min
588 /* As a special exception preserve non-null ranges. */
589 && !(TYPE_UNSIGNED (TREE_TYPE (min))
590 && integer_zerop (max)))
592 tree one = build_int_cst (TREE_TYPE (max), 1);
593 min = int_const_binop (PLUS_EXPR, max, one);
594 max = vrp_val_max (TREE_TYPE (max));
595 t = VR_RANGE;
597 else if (is_max)
599 tree one = build_int_cst (TREE_TYPE (min), 1);
600 max = int_const_binop (MINUS_EXPR, min, one);
601 min = vrp_val_min (TREE_TYPE (min));
602 t = VR_RANGE;
606 /* Drop [-INF(OVF), +INF(OVF)] to varying. */
607 if (needs_overflow_infinity (TREE_TYPE (min))
608 && is_overflow_infinity (min)
609 && is_overflow_infinity (max))
611 set_value_range_to_varying (vr);
612 return;
615 set_value_range (vr, t, min, max, equiv);
618 /* Copy value range FROM into value range TO. */
620 static inline void
621 copy_value_range (value_range_t *to, value_range_t *from)
623 set_value_range (to, from->type, from->min, from->max, from->equiv);
626 /* Set value range VR to a single value. This function is only called
627 with values we get from statements, and exists to clear the
628 TREE_OVERFLOW flag so that we don't think we have an overflow
629 infinity when we shouldn't. */
631 static inline void
632 set_value_range_to_value (value_range_t *vr, tree val, bitmap equiv)
634 gcc_assert (is_gimple_min_invariant (val));
635 if (TREE_OVERFLOW_P (val))
636 val = drop_tree_overflow (val);
637 set_value_range (vr, VR_RANGE, val, val, equiv);
640 /* Set value range VR to a non-negative range of type TYPE.
641 OVERFLOW_INFINITY indicates whether to use an overflow infinity
642 rather than TYPE_MAX_VALUE; this should be true if we determine
643 that the range is nonnegative based on the assumption that signed
644 overflow does not occur. */
646 static inline void
647 set_value_range_to_nonnegative (value_range_t *vr, tree type,
648 bool overflow_infinity)
650 tree zero;
652 if (overflow_infinity && !supports_overflow_infinity (type))
654 set_value_range_to_varying (vr);
655 return;
658 zero = build_int_cst (type, 0);
659 set_value_range (vr, VR_RANGE, zero,
660 (overflow_infinity
661 ? positive_overflow_infinity (type)
662 : TYPE_MAX_VALUE (type)),
663 vr->equiv);
666 /* Set value range VR to a non-NULL range of type TYPE. */
668 static inline void
669 set_value_range_to_nonnull (value_range_t *vr, tree type)
671 tree zero = build_int_cst (type, 0);
672 set_value_range (vr, VR_ANTI_RANGE, zero, zero, vr->equiv);
676 /* Set value range VR to a NULL range of type TYPE. */
678 static inline void
679 set_value_range_to_null (value_range_t *vr, tree type)
681 set_value_range_to_value (vr, build_int_cst (type, 0), vr->equiv);
685 /* Set value range VR to a range of a truthvalue of type TYPE. */
687 static inline void
688 set_value_range_to_truthvalue (value_range_t *vr, tree type)
690 if (TYPE_PRECISION (type) == 1)
691 set_value_range_to_varying (vr);
692 else
693 set_value_range (vr, VR_RANGE,
694 build_int_cst (type, 0), build_int_cst (type, 1),
695 vr->equiv);
699 /* If abs (min) < abs (max), set VR to [-max, max], if
700 abs (min) >= abs (max), set VR to [-min, min]. */
702 static void
703 abs_extent_range (value_range_t *vr, tree min, tree max)
705 int cmp;
707 gcc_assert (TREE_CODE (min) == INTEGER_CST);
708 gcc_assert (TREE_CODE (max) == INTEGER_CST);
709 gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (min)));
710 gcc_assert (!TYPE_UNSIGNED (TREE_TYPE (min)));
711 min = fold_unary (ABS_EXPR, TREE_TYPE (min), min);
712 max = fold_unary (ABS_EXPR, TREE_TYPE (max), max);
713 if (TREE_OVERFLOW (min) || TREE_OVERFLOW (max))
715 set_value_range_to_varying (vr);
716 return;
718 cmp = compare_values (min, max);
719 if (cmp == -1)
720 min = fold_unary (NEGATE_EXPR, TREE_TYPE (min), max);
721 else if (cmp == 0 || cmp == 1)
723 max = min;
724 min = fold_unary (NEGATE_EXPR, TREE_TYPE (min), min);
726 else
728 set_value_range_to_varying (vr);
729 return;
731 set_and_canonicalize_value_range (vr, VR_RANGE, min, max, NULL);
735 /* Return value range information for VAR.
737 If we have no values ranges recorded (ie, VRP is not running), then
738 return NULL. Otherwise create an empty range if none existed for VAR. */
740 static value_range_t *
741 get_value_range (const_tree var)
743 static const struct value_range_d vr_const_varying
744 = { VR_VARYING, NULL_TREE, NULL_TREE, NULL };
745 value_range_t *vr;
746 tree sym;
747 unsigned ver = SSA_NAME_VERSION (var);
749 /* If we have no recorded ranges, then return NULL. */
750 if (! vr_value)
751 return NULL;
753 /* If we query the range for a new SSA name return an unmodifiable VARYING.
754 We should get here at most from the substitute-and-fold stage which
755 will never try to change values. */
756 if (ver >= num_vr_values)
757 return CONST_CAST (value_range_t *, &vr_const_varying);
759 vr = vr_value[ver];
760 if (vr)
761 return vr;
763 /* After propagation finished do not allocate new value-ranges. */
764 if (values_propagated)
765 return CONST_CAST (value_range_t *, &vr_const_varying);
767 /* Create a default value range. */
768 vr_value[ver] = vr = XCNEW (value_range_t);
770 /* Defer allocating the equivalence set. */
771 vr->equiv = NULL;
773 /* If VAR is a default definition of a parameter, the variable can
774 take any value in VAR's type. */
775 if (SSA_NAME_IS_DEFAULT_DEF (var))
777 sym = SSA_NAME_VAR (var);
778 if (TREE_CODE (sym) == PARM_DECL)
780 /* Try to use the "nonnull" attribute to create ~[0, 0]
781 anti-ranges for pointers. Note that this is only valid with
782 default definitions of PARM_DECLs. */
783 if (POINTER_TYPE_P (TREE_TYPE (sym))
784 && nonnull_arg_p (sym))
785 set_value_range_to_nonnull (vr, TREE_TYPE (sym));
786 else
787 set_value_range_to_varying (vr);
789 else if (TREE_CODE (sym) == RESULT_DECL
790 && DECL_BY_REFERENCE (sym))
791 set_value_range_to_nonnull (vr, TREE_TYPE (sym));
794 return vr;
797 /* Return true, if VAL1 and VAL2 are equal values for VRP purposes. */
799 static inline bool
800 vrp_operand_equal_p (const_tree val1, const_tree val2)
802 if (val1 == val2)
803 return true;
804 if (!val1 || !val2 || !operand_equal_p (val1, val2, 0))
805 return false;
806 return is_overflow_infinity (val1) == is_overflow_infinity (val2);
809 /* Return true, if the bitmaps B1 and B2 are equal. */
811 static inline bool
812 vrp_bitmap_equal_p (const_bitmap b1, const_bitmap b2)
814 return (b1 == b2
815 || ((!b1 || bitmap_empty_p (b1))
816 && (!b2 || bitmap_empty_p (b2)))
817 || (b1 && b2
818 && bitmap_equal_p (b1, b2)));
821 /* Update the value range and equivalence set for variable VAR to
822 NEW_VR. Return true if NEW_VR is different from VAR's previous
823 value.
825 NOTE: This function assumes that NEW_VR is a temporary value range
826 object created for the sole purpose of updating VAR's range. The
827 storage used by the equivalence set from NEW_VR will be freed by
828 this function. Do not call update_value_range when NEW_VR
829 is the range object associated with another SSA name. */
831 static inline bool
832 update_value_range (const_tree var, value_range_t *new_vr)
834 value_range_t *old_vr;
835 bool is_new;
837 /* If there is a value-range on the SSA name from earlier analysis
838 factor that in. */
839 if (INTEGRAL_TYPE_P (TREE_TYPE (var)))
841 wide_int min, max;
842 value_range_type rtype = get_range_info (var, &min, &max);
843 if (rtype == VR_RANGE || rtype == VR_ANTI_RANGE)
845 value_range_d nr;
846 nr.type = rtype;
847 nr.min = wide_int_to_tree (TREE_TYPE (var), min);
848 nr.max = wide_int_to_tree (TREE_TYPE (var), max);
849 nr.equiv = NULL;
850 vrp_intersect_ranges (new_vr, &nr);
854 /* Update the value range, if necessary. */
855 old_vr = get_value_range (var);
856 is_new = old_vr->type != new_vr->type
857 || !vrp_operand_equal_p (old_vr->min, new_vr->min)
858 || !vrp_operand_equal_p (old_vr->max, new_vr->max)
859 || !vrp_bitmap_equal_p (old_vr->equiv, new_vr->equiv);
861 if (is_new)
863 /* Do not allow transitions up the lattice. The following
864 is slightly more awkward than just new_vr->type < old_vr->type
865 because VR_RANGE and VR_ANTI_RANGE need to be considered
866 the same. We may not have is_new when transitioning to
867 UNDEFINED. If old_vr->type is VARYING, we shouldn't be
868 called. */
869 if (new_vr->type == VR_UNDEFINED)
871 BITMAP_FREE (new_vr->equiv);
872 set_value_range_to_varying (old_vr);
873 set_value_range_to_varying (new_vr);
874 return true;
876 else
877 set_value_range (old_vr, new_vr->type, new_vr->min, new_vr->max,
878 new_vr->equiv);
881 BITMAP_FREE (new_vr->equiv);
883 return is_new;
887 /* Add VAR and VAR's equivalence set to EQUIV. This is the central
888 point where equivalence processing can be turned on/off. */
890 static void
891 add_equivalence (bitmap *equiv, const_tree var)
893 unsigned ver = SSA_NAME_VERSION (var);
894 value_range_t *vr = vr_value[ver];
896 if (*equiv == NULL)
897 *equiv = BITMAP_ALLOC (NULL);
898 bitmap_set_bit (*equiv, ver);
899 if (vr && vr->equiv)
900 bitmap_ior_into (*equiv, vr->equiv);
904 /* Return true if VR is ~[0, 0]. */
906 static inline bool
907 range_is_nonnull (value_range_t *vr)
909 return vr->type == VR_ANTI_RANGE
910 && integer_zerop (vr->min)
911 && integer_zerop (vr->max);
915 /* Return true if VR is [0, 0]. */
917 static inline bool
918 range_is_null (value_range_t *vr)
920 return vr->type == VR_RANGE
921 && integer_zerop (vr->min)
922 && integer_zerop (vr->max);
925 /* Return true if max and min of VR are INTEGER_CST. It's not necessary
926 a singleton. */
928 static inline bool
929 range_int_cst_p (value_range_t *vr)
931 return (vr->type == VR_RANGE
932 && TREE_CODE (vr->max) == INTEGER_CST
933 && TREE_CODE (vr->min) == INTEGER_CST);
936 /* Return true if VR is a INTEGER_CST singleton. */
938 static inline bool
939 range_int_cst_singleton_p (value_range_t *vr)
941 return (range_int_cst_p (vr)
942 && !is_overflow_infinity (vr->min)
943 && !is_overflow_infinity (vr->max)
944 && tree_int_cst_equal (vr->min, vr->max));
947 /* Return true if value range VR involves at least one symbol. */
949 static inline bool
950 symbolic_range_p (value_range_t *vr)
952 return (!is_gimple_min_invariant (vr->min)
953 || !is_gimple_min_invariant (vr->max));
956 /* Return the single symbol (an SSA_NAME) contained in T if any, or NULL_TREE
957 otherwise. We only handle additive operations and set NEG to true if the
958 symbol is negated and INV to the invariant part, if any. */
960 static tree
961 get_single_symbol (tree t, bool *neg, tree *inv)
963 bool neg_;
964 tree inv_;
966 if (TREE_CODE (t) == PLUS_EXPR
967 || TREE_CODE (t) == POINTER_PLUS_EXPR
968 || TREE_CODE (t) == MINUS_EXPR)
970 if (is_gimple_min_invariant (TREE_OPERAND (t, 0)))
972 neg_ = (TREE_CODE (t) == MINUS_EXPR);
973 inv_ = TREE_OPERAND (t, 0);
974 t = TREE_OPERAND (t, 1);
976 else if (is_gimple_min_invariant (TREE_OPERAND (t, 1)))
978 neg_ = false;
979 inv_ = TREE_OPERAND (t, 1);
980 t = TREE_OPERAND (t, 0);
982 else
983 return NULL_TREE;
985 else
987 neg_ = false;
988 inv_ = NULL_TREE;
991 if (TREE_CODE (t) == NEGATE_EXPR)
993 t = TREE_OPERAND (t, 0);
994 neg_ = !neg_;
997 if (TREE_CODE (t) != SSA_NAME)
998 return NULL_TREE;
1000 *neg = neg_;
1001 *inv = inv_;
1002 return t;
1005 /* The reverse operation: build a symbolic expression with TYPE
1006 from symbol SYM, negated according to NEG, and invariant INV. */
1008 static tree
1009 build_symbolic_expr (tree type, tree sym, bool neg, tree inv)
1011 const bool pointer_p = POINTER_TYPE_P (type);
1012 tree t = sym;
1014 if (neg)
1015 t = build1 (NEGATE_EXPR, type, t);
1017 if (integer_zerop (inv))
1018 return t;
1020 return build2 (pointer_p ? POINTER_PLUS_EXPR : PLUS_EXPR, type, t, inv);
1023 /* Return true if value range VR involves exactly one symbol SYM. */
1025 static bool
1026 symbolic_range_based_on_p (value_range_t *vr, const_tree sym)
1028 bool neg, min_has_symbol, max_has_symbol;
1029 tree inv;
1031 if (is_gimple_min_invariant (vr->min))
1032 min_has_symbol = false;
1033 else if (get_single_symbol (vr->min, &neg, &inv) == sym)
1034 min_has_symbol = true;
1035 else
1036 return false;
1038 if (is_gimple_min_invariant (vr->max))
1039 max_has_symbol = false;
1040 else if (get_single_symbol (vr->max, &neg, &inv) == sym)
1041 max_has_symbol = true;
1042 else
1043 return false;
1045 return (min_has_symbol || max_has_symbol);
1048 /* Return true if value range VR uses an overflow infinity. */
1050 static inline bool
1051 overflow_infinity_range_p (value_range_t *vr)
1053 return (vr->type == VR_RANGE
1054 && (is_overflow_infinity (vr->min)
1055 || is_overflow_infinity (vr->max)));
1058 /* Return false if we can not make a valid comparison based on VR;
1059 this will be the case if it uses an overflow infinity and overflow
1060 is not undefined (i.e., -fno-strict-overflow is in effect).
1061 Otherwise return true, and set *STRICT_OVERFLOW_P to true if VR
1062 uses an overflow infinity. */
1064 static bool
1065 usable_range_p (value_range_t *vr, bool *strict_overflow_p)
1067 gcc_assert (vr->type == VR_RANGE);
1068 if (is_overflow_infinity (vr->min))
1070 *strict_overflow_p = true;
1071 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (vr->min)))
1072 return false;
1074 if (is_overflow_infinity (vr->max))
1076 *strict_overflow_p = true;
1077 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (vr->max)))
1078 return false;
1080 return true;
1084 /* Return true if the result of assignment STMT is know to be non-negative.
1085 If the return value is based on the assumption that signed overflow is
1086 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1087 *STRICT_OVERFLOW_P.*/
1089 static bool
1090 gimple_assign_nonnegative_warnv_p (gimple stmt, bool *strict_overflow_p)
1092 enum tree_code code = gimple_assign_rhs_code (stmt);
1093 switch (get_gimple_rhs_class (code))
1095 case GIMPLE_UNARY_RHS:
1096 return tree_unary_nonnegative_warnv_p (gimple_assign_rhs_code (stmt),
1097 gimple_expr_type (stmt),
1098 gimple_assign_rhs1 (stmt),
1099 strict_overflow_p);
1100 case GIMPLE_BINARY_RHS:
1101 return tree_binary_nonnegative_warnv_p (gimple_assign_rhs_code (stmt),
1102 gimple_expr_type (stmt),
1103 gimple_assign_rhs1 (stmt),
1104 gimple_assign_rhs2 (stmt),
1105 strict_overflow_p);
1106 case GIMPLE_TERNARY_RHS:
1107 return false;
1108 case GIMPLE_SINGLE_RHS:
1109 return tree_single_nonnegative_warnv_p (gimple_assign_rhs1 (stmt),
1110 strict_overflow_p);
1111 case GIMPLE_INVALID_RHS:
1112 gcc_unreachable ();
1113 default:
1114 gcc_unreachable ();
1118 /* Return true if return value of call STMT is know to be non-negative.
1119 If the return value is based on the assumption that signed overflow is
1120 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1121 *STRICT_OVERFLOW_P.*/
1123 static bool
1124 gimple_call_nonnegative_warnv_p (gimple stmt, bool *strict_overflow_p)
1126 tree arg0 = gimple_call_num_args (stmt) > 0 ?
1127 gimple_call_arg (stmt, 0) : NULL_TREE;
1128 tree arg1 = gimple_call_num_args (stmt) > 1 ?
1129 gimple_call_arg (stmt, 1) : NULL_TREE;
1131 return tree_call_nonnegative_warnv_p (gimple_expr_type (stmt),
1132 gimple_call_fndecl (stmt),
1133 arg0,
1134 arg1,
1135 strict_overflow_p);
1138 /* Return true if STMT is know to to compute a non-negative value.
1139 If the return value is based on the assumption that signed overflow is
1140 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1141 *STRICT_OVERFLOW_P.*/
1143 static bool
1144 gimple_stmt_nonnegative_warnv_p (gimple stmt, bool *strict_overflow_p)
1146 switch (gimple_code (stmt))
1148 case GIMPLE_ASSIGN:
1149 return gimple_assign_nonnegative_warnv_p (stmt, strict_overflow_p);
1150 case GIMPLE_CALL:
1151 return gimple_call_nonnegative_warnv_p (stmt, strict_overflow_p);
1152 default:
1153 gcc_unreachable ();
1157 /* Return true if the result of assignment STMT is know to be non-zero.
1158 If the return value is based on the assumption that signed overflow is
1159 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1160 *STRICT_OVERFLOW_P.*/
1162 static bool
1163 gimple_assign_nonzero_warnv_p (gimple stmt, bool *strict_overflow_p)
1165 enum tree_code code = gimple_assign_rhs_code (stmt);
1166 switch (get_gimple_rhs_class (code))
1168 case GIMPLE_UNARY_RHS:
1169 return tree_unary_nonzero_warnv_p (gimple_assign_rhs_code (stmt),
1170 gimple_expr_type (stmt),
1171 gimple_assign_rhs1 (stmt),
1172 strict_overflow_p);
1173 case GIMPLE_BINARY_RHS:
1174 return tree_binary_nonzero_warnv_p (gimple_assign_rhs_code (stmt),
1175 gimple_expr_type (stmt),
1176 gimple_assign_rhs1 (stmt),
1177 gimple_assign_rhs2 (stmt),
1178 strict_overflow_p);
1179 case GIMPLE_TERNARY_RHS:
1180 return false;
1181 case GIMPLE_SINGLE_RHS:
1182 return tree_single_nonzero_warnv_p (gimple_assign_rhs1 (stmt),
1183 strict_overflow_p);
1184 case GIMPLE_INVALID_RHS:
1185 gcc_unreachable ();
1186 default:
1187 gcc_unreachable ();
1191 /* Return true if STMT is known to compute a non-zero value.
1192 If the return value is based on the assumption that signed overflow is
1193 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1194 *STRICT_OVERFLOW_P.*/
1196 static bool
1197 gimple_stmt_nonzero_warnv_p (gimple stmt, bool *strict_overflow_p)
1199 switch (gimple_code (stmt))
1201 case GIMPLE_ASSIGN:
1202 return gimple_assign_nonzero_warnv_p (stmt, strict_overflow_p);
1203 case GIMPLE_CALL:
1205 tree fndecl = gimple_call_fndecl (stmt);
1206 if (!fndecl) return false;
1207 if (flag_delete_null_pointer_checks && !flag_check_new
1208 && DECL_IS_OPERATOR_NEW (fndecl)
1209 && !TREE_NOTHROW (fndecl))
1210 return true;
1211 /* References are always non-NULL. */
1212 if (flag_delete_null_pointer_checks
1213 && TREE_CODE (TREE_TYPE (fndecl)) == REFERENCE_TYPE)
1214 return true;
1215 if (flag_delete_null_pointer_checks &&
1216 lookup_attribute ("returns_nonnull",
1217 TYPE_ATTRIBUTES (gimple_call_fntype (stmt))))
1218 return true;
1219 return gimple_alloca_call_p (stmt);
1221 default:
1222 gcc_unreachable ();
1226 /* Like tree_expr_nonzero_warnv_p, but this function uses value ranges
1227 obtained so far. */
1229 static bool
1230 vrp_stmt_computes_nonzero (gimple stmt, bool *strict_overflow_p)
1232 if (gimple_stmt_nonzero_warnv_p (stmt, strict_overflow_p))
1233 return true;
1235 /* If we have an expression of the form &X->a, then the expression
1236 is nonnull if X is nonnull. */
1237 if (is_gimple_assign (stmt)
1238 && gimple_assign_rhs_code (stmt) == ADDR_EXPR)
1240 tree expr = gimple_assign_rhs1 (stmt);
1241 tree base = get_base_address (TREE_OPERAND (expr, 0));
1243 if (base != NULL_TREE
1244 && TREE_CODE (base) == MEM_REF
1245 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
1247 value_range_t *vr = get_value_range (TREE_OPERAND (base, 0));
1248 if (range_is_nonnull (vr))
1249 return true;
1253 return false;
1256 /* Returns true if EXPR is a valid value (as expected by compare_values) --
1257 a gimple invariant, or SSA_NAME +- CST. */
1259 static bool
1260 valid_value_p (tree expr)
1262 if (TREE_CODE (expr) == SSA_NAME)
1263 return true;
1265 if (TREE_CODE (expr) == PLUS_EXPR
1266 || TREE_CODE (expr) == MINUS_EXPR)
1267 return (TREE_CODE (TREE_OPERAND (expr, 0)) == SSA_NAME
1268 && TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST);
1270 return is_gimple_min_invariant (expr);
1273 /* Return
1274 1 if VAL < VAL2
1275 0 if !(VAL < VAL2)
1276 -2 if those are incomparable. */
1277 static inline int
1278 operand_less_p (tree val, tree val2)
1280 /* LT is folded faster than GE and others. Inline the common case. */
1281 if (TREE_CODE (val) == INTEGER_CST && TREE_CODE (val2) == INTEGER_CST)
1282 return tree_int_cst_lt (val, val2);
1283 else
1285 tree tcmp;
1287 fold_defer_overflow_warnings ();
1289 tcmp = fold_binary_to_constant (LT_EXPR, boolean_type_node, val, val2);
1291 fold_undefer_and_ignore_overflow_warnings ();
1293 if (!tcmp
1294 || TREE_CODE (tcmp) != INTEGER_CST)
1295 return -2;
1297 if (!integer_zerop (tcmp))
1298 return 1;
1301 /* val >= val2, not considering overflow infinity. */
1302 if (is_negative_overflow_infinity (val))
1303 return is_negative_overflow_infinity (val2) ? 0 : 1;
1304 else if (is_positive_overflow_infinity (val2))
1305 return is_positive_overflow_infinity (val) ? 0 : 1;
1307 return 0;
1310 /* Compare two values VAL1 and VAL2. Return
1312 -2 if VAL1 and VAL2 cannot be compared at compile-time,
1313 -1 if VAL1 < VAL2,
1314 0 if VAL1 == VAL2,
1315 +1 if VAL1 > VAL2, and
1316 +2 if VAL1 != VAL2
1318 This is similar to tree_int_cst_compare but supports pointer values
1319 and values that cannot be compared at compile time.
1321 If STRICT_OVERFLOW_P is not NULL, then set *STRICT_OVERFLOW_P to
1322 true if the return value is only valid if we assume that signed
1323 overflow is undefined. */
1325 static int
1326 compare_values_warnv (tree val1, tree val2, bool *strict_overflow_p)
1328 if (val1 == val2)
1329 return 0;
1331 /* Below we rely on the fact that VAL1 and VAL2 are both pointers or
1332 both integers. */
1333 gcc_assert (POINTER_TYPE_P (TREE_TYPE (val1))
1334 == POINTER_TYPE_P (TREE_TYPE (val2)));
1336 /* Convert the two values into the same type. This is needed because
1337 sizetype causes sign extension even for unsigned types. */
1338 val2 = fold_convert (TREE_TYPE (val1), val2);
1339 STRIP_USELESS_TYPE_CONVERSION (val2);
1341 if ((TREE_CODE (val1) == SSA_NAME
1342 || (TREE_CODE (val1) == NEGATE_EXPR
1343 && TREE_CODE (TREE_OPERAND (val1, 0)) == SSA_NAME)
1344 || TREE_CODE (val1) == PLUS_EXPR
1345 || TREE_CODE (val1) == MINUS_EXPR)
1346 && (TREE_CODE (val2) == SSA_NAME
1347 || (TREE_CODE (val2) == NEGATE_EXPR
1348 && TREE_CODE (TREE_OPERAND (val2, 0)) == SSA_NAME)
1349 || TREE_CODE (val2) == PLUS_EXPR
1350 || TREE_CODE (val2) == MINUS_EXPR))
1352 tree n1, c1, n2, c2;
1353 enum tree_code code1, code2;
1355 /* If VAL1 and VAL2 are of the form '[-]NAME [+-] CST' or 'NAME',
1356 return -1 or +1 accordingly. If VAL1 and VAL2 don't use the
1357 same name, return -2. */
1358 if (TREE_CODE (val1) == SSA_NAME || TREE_CODE (val1) == NEGATE_EXPR)
1360 code1 = SSA_NAME;
1361 n1 = val1;
1362 c1 = NULL_TREE;
1364 else
1366 code1 = TREE_CODE (val1);
1367 n1 = TREE_OPERAND (val1, 0);
1368 c1 = TREE_OPERAND (val1, 1);
1369 if (tree_int_cst_sgn (c1) == -1)
1371 if (is_negative_overflow_infinity (c1))
1372 return -2;
1373 c1 = fold_unary_to_constant (NEGATE_EXPR, TREE_TYPE (c1), c1);
1374 if (!c1)
1375 return -2;
1376 code1 = code1 == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR;
1380 if (TREE_CODE (val2) == SSA_NAME || TREE_CODE (val2) == NEGATE_EXPR)
1382 code2 = SSA_NAME;
1383 n2 = val2;
1384 c2 = NULL_TREE;
1386 else
1388 code2 = TREE_CODE (val2);
1389 n2 = TREE_OPERAND (val2, 0);
1390 c2 = TREE_OPERAND (val2, 1);
1391 if (tree_int_cst_sgn (c2) == -1)
1393 if (is_negative_overflow_infinity (c2))
1394 return -2;
1395 c2 = fold_unary_to_constant (NEGATE_EXPR, TREE_TYPE (c2), c2);
1396 if (!c2)
1397 return -2;
1398 code2 = code2 == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR;
1402 /* Both values must use the same name. */
1403 if (TREE_CODE (n1) == NEGATE_EXPR && TREE_CODE (n2) == NEGATE_EXPR)
1405 n1 = TREE_OPERAND (n1, 0);
1406 n2 = TREE_OPERAND (n2, 0);
1408 if (n1 != n2)
1409 return -2;
1411 if (code1 == SSA_NAME && code2 == SSA_NAME)
1412 /* NAME == NAME */
1413 return 0;
1415 /* If overflow is defined we cannot simplify more. */
1416 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (val1)))
1417 return -2;
1419 if (strict_overflow_p != NULL
1420 && (code1 == SSA_NAME || !TREE_NO_WARNING (val1))
1421 && (code2 == SSA_NAME || !TREE_NO_WARNING (val2)))
1422 *strict_overflow_p = true;
1424 if (code1 == SSA_NAME)
1426 if (code2 == PLUS_EXPR)
1427 /* NAME < NAME + CST */
1428 return -1;
1429 else if (code2 == MINUS_EXPR)
1430 /* NAME > NAME - CST */
1431 return 1;
1433 else if (code1 == PLUS_EXPR)
1435 if (code2 == SSA_NAME)
1436 /* NAME + CST > NAME */
1437 return 1;
1438 else if (code2 == PLUS_EXPR)
1439 /* NAME + CST1 > NAME + CST2, if CST1 > CST2 */
1440 return compare_values_warnv (c1, c2, strict_overflow_p);
1441 else if (code2 == MINUS_EXPR)
1442 /* NAME + CST1 > NAME - CST2 */
1443 return 1;
1445 else if (code1 == MINUS_EXPR)
1447 if (code2 == SSA_NAME)
1448 /* NAME - CST < NAME */
1449 return -1;
1450 else if (code2 == PLUS_EXPR)
1451 /* NAME - CST1 < NAME + CST2 */
1452 return -1;
1453 else if (code2 == MINUS_EXPR)
1454 /* NAME - CST1 > NAME - CST2, if CST1 < CST2. Notice that
1455 C1 and C2 are swapped in the call to compare_values. */
1456 return compare_values_warnv (c2, c1, strict_overflow_p);
1459 gcc_unreachable ();
1462 /* We cannot compare non-constants. */
1463 if (!is_gimple_min_invariant (val1) || !is_gimple_min_invariant (val2))
1464 return -2;
1466 if (!POINTER_TYPE_P (TREE_TYPE (val1)))
1468 /* We cannot compare overflowed values, except for overflow
1469 infinities. */
1470 if (TREE_OVERFLOW (val1) || TREE_OVERFLOW (val2))
1472 if (strict_overflow_p != NULL)
1473 *strict_overflow_p = true;
1474 if (is_negative_overflow_infinity (val1))
1475 return is_negative_overflow_infinity (val2) ? 0 : -1;
1476 else if (is_negative_overflow_infinity (val2))
1477 return 1;
1478 else if (is_positive_overflow_infinity (val1))
1479 return is_positive_overflow_infinity (val2) ? 0 : 1;
1480 else if (is_positive_overflow_infinity (val2))
1481 return -1;
1482 return -2;
1485 return tree_int_cst_compare (val1, val2);
1487 else
1489 tree t;
1491 /* First see if VAL1 and VAL2 are not the same. */
1492 if (val1 == val2 || operand_equal_p (val1, val2, 0))
1493 return 0;
1495 /* If VAL1 is a lower address than VAL2, return -1. */
1496 if (operand_less_p (val1, val2) == 1)
1497 return -1;
1499 /* If VAL1 is a higher address than VAL2, return +1. */
1500 if (operand_less_p (val2, val1) == 1)
1501 return 1;
1503 /* If VAL1 is different than VAL2, return +2.
1504 For integer constants we either have already returned -1 or 1
1505 or they are equivalent. We still might succeed in proving
1506 something about non-trivial operands. */
1507 if (TREE_CODE (val1) != INTEGER_CST
1508 || TREE_CODE (val2) != INTEGER_CST)
1510 t = fold_binary_to_constant (NE_EXPR, boolean_type_node, val1, val2);
1511 if (t && integer_onep (t))
1512 return 2;
1515 return -2;
1519 /* Compare values like compare_values_warnv, but treat comparisons of
1520 nonconstants which rely on undefined overflow as incomparable. */
1522 static int
1523 compare_values (tree val1, tree val2)
1525 bool sop;
1526 int ret;
1528 sop = false;
1529 ret = compare_values_warnv (val1, val2, &sop);
1530 if (sop
1531 && (!is_gimple_min_invariant (val1) || !is_gimple_min_invariant (val2)))
1532 ret = -2;
1533 return ret;
1537 /* Return 1 if VAL is inside value range MIN <= VAL <= MAX,
1538 0 if VAL is not inside [MIN, MAX],
1539 -2 if we cannot tell either way.
1541 Benchmark compile/20001226-1.c compilation time after changing this
1542 function. */
1544 static inline int
1545 value_inside_range (tree val, tree min, tree max)
1547 int cmp1, cmp2;
1549 cmp1 = operand_less_p (val, min);
1550 if (cmp1 == -2)
1551 return -2;
1552 if (cmp1 == 1)
1553 return 0;
1555 cmp2 = operand_less_p (max, val);
1556 if (cmp2 == -2)
1557 return -2;
1559 return !cmp2;
1563 /* Return true if value ranges VR0 and VR1 have a non-empty
1564 intersection.
1566 Benchmark compile/20001226-1.c compilation time after changing this
1567 function.
1570 static inline bool
1571 value_ranges_intersect_p (value_range_t *vr0, value_range_t *vr1)
1573 /* The value ranges do not intersect if the maximum of the first range is
1574 less than the minimum of the second range or vice versa.
1575 When those relations are unknown, we can't do any better. */
1576 if (operand_less_p (vr0->max, vr1->min) != 0)
1577 return false;
1578 if (operand_less_p (vr1->max, vr0->min) != 0)
1579 return false;
1580 return true;
1584 /* Return 1 if [MIN, MAX] includes the value zero, 0 if it does not
1585 include the value zero, -2 if we cannot tell. */
1587 static inline int
1588 range_includes_zero_p (tree min, tree max)
1590 tree zero = build_int_cst (TREE_TYPE (min), 0);
1591 return value_inside_range (zero, min, max);
1594 /* Return true if *VR is know to only contain nonnegative values. */
1596 static inline bool
1597 value_range_nonnegative_p (value_range_t *vr)
1599 /* Testing for VR_ANTI_RANGE is not useful here as any anti-range
1600 which would return a useful value should be encoded as a
1601 VR_RANGE. */
1602 if (vr->type == VR_RANGE)
1604 int result = compare_values (vr->min, integer_zero_node);
1605 return (result == 0 || result == 1);
1608 return false;
1611 /* If *VR has a value rante that is a single constant value return that,
1612 otherwise return NULL_TREE. */
1614 static tree
1615 value_range_constant_singleton (value_range_t *vr)
1617 if (vr->type == VR_RANGE
1618 && operand_equal_p (vr->min, vr->max, 0)
1619 && is_gimple_min_invariant (vr->min))
1620 return vr->min;
1622 return NULL_TREE;
1625 /* If OP has a value range with a single constant value return that,
1626 otherwise return NULL_TREE. This returns OP itself if OP is a
1627 constant. */
1629 static tree
1630 op_with_constant_singleton_value_range (tree op)
1632 if (is_gimple_min_invariant (op))
1633 return op;
1635 if (TREE_CODE (op) != SSA_NAME)
1636 return NULL_TREE;
1638 return value_range_constant_singleton (get_value_range (op));
1641 /* Return true if op is in a boolean [0, 1] value-range. */
1643 static bool
1644 op_with_boolean_value_range_p (tree op)
1646 value_range_t *vr;
1648 if (TYPE_PRECISION (TREE_TYPE (op)) == 1)
1649 return true;
1651 if (integer_zerop (op)
1652 || integer_onep (op))
1653 return true;
1655 if (TREE_CODE (op) != SSA_NAME)
1656 return false;
1658 vr = get_value_range (op);
1659 return (vr->type == VR_RANGE
1660 && integer_zerop (vr->min)
1661 && integer_onep (vr->max));
1664 /* Extract value range information from an ASSERT_EXPR EXPR and store
1665 it in *VR_P. */
1667 static void
1668 extract_range_from_assert (value_range_t *vr_p, tree expr)
1670 tree var, cond, limit, min, max, type;
1671 value_range_t *limit_vr;
1672 enum tree_code cond_code;
1674 var = ASSERT_EXPR_VAR (expr);
1675 cond = ASSERT_EXPR_COND (expr);
1677 gcc_assert (COMPARISON_CLASS_P (cond));
1679 /* Find VAR in the ASSERT_EXPR conditional. */
1680 if (var == TREE_OPERAND (cond, 0)
1681 || TREE_CODE (TREE_OPERAND (cond, 0)) == PLUS_EXPR
1682 || TREE_CODE (TREE_OPERAND (cond, 0)) == NOP_EXPR)
1684 /* If the predicate is of the form VAR COMP LIMIT, then we just
1685 take LIMIT from the RHS and use the same comparison code. */
1686 cond_code = TREE_CODE (cond);
1687 limit = TREE_OPERAND (cond, 1);
1688 cond = TREE_OPERAND (cond, 0);
1690 else
1692 /* If the predicate is of the form LIMIT COMP VAR, then we need
1693 to flip around the comparison code to create the proper range
1694 for VAR. */
1695 cond_code = swap_tree_comparison (TREE_CODE (cond));
1696 limit = TREE_OPERAND (cond, 0);
1697 cond = TREE_OPERAND (cond, 1);
1700 limit = avoid_overflow_infinity (limit);
1702 type = TREE_TYPE (var);
1703 gcc_assert (limit != var);
1705 /* For pointer arithmetic, we only keep track of pointer equality
1706 and inequality. */
1707 if (POINTER_TYPE_P (type) && cond_code != NE_EXPR && cond_code != EQ_EXPR)
1709 set_value_range_to_varying (vr_p);
1710 return;
1713 /* If LIMIT is another SSA name and LIMIT has a range of its own,
1714 try to use LIMIT's range to avoid creating symbolic ranges
1715 unnecessarily. */
1716 limit_vr = (TREE_CODE (limit) == SSA_NAME) ? get_value_range (limit) : NULL;
1718 /* LIMIT's range is only interesting if it has any useful information. */
1719 if (limit_vr
1720 && (limit_vr->type == VR_UNDEFINED
1721 || limit_vr->type == VR_VARYING
1722 || symbolic_range_p (limit_vr)))
1723 limit_vr = NULL;
1725 /* Initially, the new range has the same set of equivalences of
1726 VAR's range. This will be revised before returning the final
1727 value. Since assertions may be chained via mutually exclusive
1728 predicates, we will need to trim the set of equivalences before
1729 we are done. */
1730 gcc_assert (vr_p->equiv == NULL);
1731 add_equivalence (&vr_p->equiv, var);
1733 /* Extract a new range based on the asserted comparison for VAR and
1734 LIMIT's value range. Notice that if LIMIT has an anti-range, we
1735 will only use it for equality comparisons (EQ_EXPR). For any
1736 other kind of assertion, we cannot derive a range from LIMIT's
1737 anti-range that can be used to describe the new range. For
1738 instance, ASSERT_EXPR <x_2, x_2 <= b_4>. If b_4 is ~[2, 10],
1739 then b_4 takes on the ranges [-INF, 1] and [11, +INF]. There is
1740 no single range for x_2 that could describe LE_EXPR, so we might
1741 as well build the range [b_4, +INF] for it.
1742 One special case we handle is extracting a range from a
1743 range test encoded as (unsigned)var + CST <= limit. */
1744 if (TREE_CODE (cond) == NOP_EXPR
1745 || TREE_CODE (cond) == PLUS_EXPR)
1747 if (TREE_CODE (cond) == PLUS_EXPR)
1749 min = fold_build1 (NEGATE_EXPR, TREE_TYPE (TREE_OPERAND (cond, 1)),
1750 TREE_OPERAND (cond, 1));
1751 max = int_const_binop (PLUS_EXPR, limit, min);
1752 cond = TREE_OPERAND (cond, 0);
1754 else
1756 min = build_int_cst (TREE_TYPE (var), 0);
1757 max = limit;
1760 /* Make sure to not set TREE_OVERFLOW on the final type
1761 conversion. We are willingly interpreting large positive
1762 unsigned values as negative signed values here. */
1763 min = force_fit_type (TREE_TYPE (var), wi::to_widest (min), 0, false);
1764 max = force_fit_type (TREE_TYPE (var), wi::to_widest (max), 0, false);
1766 /* We can transform a max, min range to an anti-range or
1767 vice-versa. Use set_and_canonicalize_value_range which does
1768 this for us. */
1769 if (cond_code == LE_EXPR)
1770 set_and_canonicalize_value_range (vr_p, VR_RANGE,
1771 min, max, vr_p->equiv);
1772 else if (cond_code == GT_EXPR)
1773 set_and_canonicalize_value_range (vr_p, VR_ANTI_RANGE,
1774 min, max, vr_p->equiv);
1775 else
1776 gcc_unreachable ();
1778 else if (cond_code == EQ_EXPR)
1780 enum value_range_type range_type;
1782 if (limit_vr)
1784 range_type = limit_vr->type;
1785 min = limit_vr->min;
1786 max = limit_vr->max;
1788 else
1790 range_type = VR_RANGE;
1791 min = limit;
1792 max = limit;
1795 set_value_range (vr_p, range_type, min, max, vr_p->equiv);
1797 /* When asserting the equality VAR == LIMIT and LIMIT is another
1798 SSA name, the new range will also inherit the equivalence set
1799 from LIMIT. */
1800 if (TREE_CODE (limit) == SSA_NAME)
1801 add_equivalence (&vr_p->equiv, limit);
1803 else if (cond_code == NE_EXPR)
1805 /* As described above, when LIMIT's range is an anti-range and
1806 this assertion is an inequality (NE_EXPR), then we cannot
1807 derive anything from the anti-range. For instance, if
1808 LIMIT's range was ~[0, 0], the assertion 'VAR != LIMIT' does
1809 not imply that VAR's range is [0, 0]. So, in the case of
1810 anti-ranges, we just assert the inequality using LIMIT and
1811 not its anti-range.
1813 If LIMIT_VR is a range, we can only use it to build a new
1814 anti-range if LIMIT_VR is a single-valued range. For
1815 instance, if LIMIT_VR is [0, 1], the predicate
1816 VAR != [0, 1] does not mean that VAR's range is ~[0, 1].
1817 Rather, it means that for value 0 VAR should be ~[0, 0]
1818 and for value 1, VAR should be ~[1, 1]. We cannot
1819 represent these ranges.
1821 The only situation in which we can build a valid
1822 anti-range is when LIMIT_VR is a single-valued range
1823 (i.e., LIMIT_VR->MIN == LIMIT_VR->MAX). In that case,
1824 build the anti-range ~[LIMIT_VR->MIN, LIMIT_VR->MAX]. */
1825 if (limit_vr
1826 && limit_vr->type == VR_RANGE
1827 && compare_values (limit_vr->min, limit_vr->max) == 0)
1829 min = limit_vr->min;
1830 max = limit_vr->max;
1832 else
1834 /* In any other case, we cannot use LIMIT's range to build a
1835 valid anti-range. */
1836 min = max = limit;
1839 /* If MIN and MAX cover the whole range for their type, then
1840 just use the original LIMIT. */
1841 if (INTEGRAL_TYPE_P (type)
1842 && vrp_val_is_min (min)
1843 && vrp_val_is_max (max))
1844 min = max = limit;
1846 set_and_canonicalize_value_range (vr_p, VR_ANTI_RANGE,
1847 min, max, vr_p->equiv);
1849 else if (cond_code == LE_EXPR || cond_code == LT_EXPR)
1851 min = TYPE_MIN_VALUE (type);
1853 if (limit_vr == NULL || limit_vr->type == VR_ANTI_RANGE)
1854 max = limit;
1855 else
1857 /* If LIMIT_VR is of the form [N1, N2], we need to build the
1858 range [MIN, N2] for LE_EXPR and [MIN, N2 - 1] for
1859 LT_EXPR. */
1860 max = limit_vr->max;
1863 /* If the maximum value forces us to be out of bounds, simply punt.
1864 It would be pointless to try and do anything more since this
1865 all should be optimized away above us. */
1866 if ((cond_code == LT_EXPR
1867 && compare_values (max, min) == 0)
1868 || is_overflow_infinity (max))
1869 set_value_range_to_varying (vr_p);
1870 else
1872 /* For LT_EXPR, we create the range [MIN, MAX - 1]. */
1873 if (cond_code == LT_EXPR)
1875 if (TYPE_PRECISION (TREE_TYPE (max)) == 1
1876 && !TYPE_UNSIGNED (TREE_TYPE (max)))
1877 max = fold_build2 (PLUS_EXPR, TREE_TYPE (max), max,
1878 build_int_cst (TREE_TYPE (max), -1));
1879 else
1880 max = fold_build2 (MINUS_EXPR, TREE_TYPE (max), max,
1881 build_int_cst (TREE_TYPE (max), 1));
1882 if (EXPR_P (max))
1883 TREE_NO_WARNING (max) = 1;
1886 set_value_range (vr_p, VR_RANGE, min, max, vr_p->equiv);
1889 else if (cond_code == GE_EXPR || cond_code == GT_EXPR)
1891 max = TYPE_MAX_VALUE (type);
1893 if (limit_vr == NULL || limit_vr->type == VR_ANTI_RANGE)
1894 min = limit;
1895 else
1897 /* If LIMIT_VR is of the form [N1, N2], we need to build the
1898 range [N1, MAX] for GE_EXPR and [N1 + 1, MAX] for
1899 GT_EXPR. */
1900 min = limit_vr->min;
1903 /* If the minimum value forces us to be out of bounds, simply punt.
1904 It would be pointless to try and do anything more since this
1905 all should be optimized away above us. */
1906 if ((cond_code == GT_EXPR
1907 && compare_values (min, max) == 0)
1908 || is_overflow_infinity (min))
1909 set_value_range_to_varying (vr_p);
1910 else
1912 /* For GT_EXPR, we create the range [MIN + 1, MAX]. */
1913 if (cond_code == GT_EXPR)
1915 if (TYPE_PRECISION (TREE_TYPE (min)) == 1
1916 && !TYPE_UNSIGNED (TREE_TYPE (min)))
1917 min = fold_build2 (MINUS_EXPR, TREE_TYPE (min), min,
1918 build_int_cst (TREE_TYPE (min), -1));
1919 else
1920 min = fold_build2 (PLUS_EXPR, TREE_TYPE (min), min,
1921 build_int_cst (TREE_TYPE (min), 1));
1922 if (EXPR_P (min))
1923 TREE_NO_WARNING (min) = 1;
1926 set_value_range (vr_p, VR_RANGE, min, max, vr_p->equiv);
1929 else
1930 gcc_unreachable ();
1932 /* Finally intersect the new range with what we already know about var. */
1933 vrp_intersect_ranges (vr_p, get_value_range (var));
1937 /* Extract range information from SSA name VAR and store it in VR. If
1938 VAR has an interesting range, use it. Otherwise, create the
1939 range [VAR, VAR] and return it. This is useful in situations where
1940 we may have conditionals testing values of VARYING names. For
1941 instance,
1943 x_3 = y_5;
1944 if (x_3 > y_5)
1947 Even if y_5 is deemed VARYING, we can determine that x_3 > y_5 is
1948 always false. */
1950 static void
1951 extract_range_from_ssa_name (value_range_t *vr, tree var)
1953 value_range_t *var_vr = get_value_range (var);
1955 if (var_vr->type != VR_VARYING)
1956 copy_value_range (vr, var_vr);
1957 else
1958 set_value_range (vr, VR_RANGE, var, var, NULL);
1960 add_equivalence (&vr->equiv, var);
1964 /* Wrapper around int_const_binop. If the operation overflows and we
1965 are not using wrapping arithmetic, then adjust the result to be
1966 -INF or +INF depending on CODE, VAL1 and VAL2. This can return
1967 NULL_TREE if we need to use an overflow infinity representation but
1968 the type does not support it. */
1970 static tree
1971 vrp_int_const_binop (enum tree_code code, tree val1, tree val2)
1973 tree res;
1975 res = int_const_binop (code, val1, val2);
1977 /* If we are using unsigned arithmetic, operate symbolically
1978 on -INF and +INF as int_const_binop only handles signed overflow. */
1979 if (TYPE_UNSIGNED (TREE_TYPE (val1)))
1981 int checkz = compare_values (res, val1);
1982 bool overflow = false;
1984 /* Ensure that res = val1 [+*] val2 >= val1
1985 or that res = val1 - val2 <= val1. */
1986 if ((code == PLUS_EXPR
1987 && !(checkz == 1 || checkz == 0))
1988 || (code == MINUS_EXPR
1989 && !(checkz == 0 || checkz == -1)))
1991 overflow = true;
1993 /* Checking for multiplication overflow is done by dividing the
1994 output of the multiplication by the first input of the
1995 multiplication. If the result of that division operation is
1996 not equal to the second input of the multiplication, then the
1997 multiplication overflowed. */
1998 else if (code == MULT_EXPR && !integer_zerop (val1))
2000 tree tmp = int_const_binop (TRUNC_DIV_EXPR,
2001 res,
2002 val1);
2003 int check = compare_values (tmp, val2);
2005 if (check != 0)
2006 overflow = true;
2009 if (overflow)
2011 res = copy_node (res);
2012 TREE_OVERFLOW (res) = 1;
2016 else if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (val1)))
2017 /* If the singed operation wraps then int_const_binop has done
2018 everything we want. */
2020 /* Signed division of -1/0 overflows and by the time it gets here
2021 returns NULL_TREE. */
2022 else if (!res)
2023 return NULL_TREE;
2024 else if ((TREE_OVERFLOW (res)
2025 && !TREE_OVERFLOW (val1)
2026 && !TREE_OVERFLOW (val2))
2027 || is_overflow_infinity (val1)
2028 || is_overflow_infinity (val2))
2030 /* If the operation overflowed but neither VAL1 nor VAL2 are
2031 overflown, return -INF or +INF depending on the operation
2032 and the combination of signs of the operands. */
2033 int sgn1 = tree_int_cst_sgn (val1);
2034 int sgn2 = tree_int_cst_sgn (val2);
2036 if (needs_overflow_infinity (TREE_TYPE (res))
2037 && !supports_overflow_infinity (TREE_TYPE (res)))
2038 return NULL_TREE;
2040 /* We have to punt on adding infinities of different signs,
2041 since we can't tell what the sign of the result should be.
2042 Likewise for subtracting infinities of the same sign. */
2043 if (((code == PLUS_EXPR && sgn1 != sgn2)
2044 || (code == MINUS_EXPR && sgn1 == sgn2))
2045 && is_overflow_infinity (val1)
2046 && is_overflow_infinity (val2))
2047 return NULL_TREE;
2049 /* Don't try to handle division or shifting of infinities. */
2050 if ((code == TRUNC_DIV_EXPR
2051 || code == FLOOR_DIV_EXPR
2052 || code == CEIL_DIV_EXPR
2053 || code == EXACT_DIV_EXPR
2054 || code == ROUND_DIV_EXPR
2055 || code == RSHIFT_EXPR)
2056 && (is_overflow_infinity (val1)
2057 || is_overflow_infinity (val2)))
2058 return NULL_TREE;
2060 /* Notice that we only need to handle the restricted set of
2061 operations handled by extract_range_from_binary_expr.
2062 Among them, only multiplication, addition and subtraction
2063 can yield overflow without overflown operands because we
2064 are working with integral types only... except in the
2065 case VAL1 = -INF and VAL2 = -1 which overflows to +INF
2066 for division too. */
2068 /* For multiplication, the sign of the overflow is given
2069 by the comparison of the signs of the operands. */
2070 if ((code == MULT_EXPR && sgn1 == sgn2)
2071 /* For addition, the operands must be of the same sign
2072 to yield an overflow. Its sign is therefore that
2073 of one of the operands, for example the first. For
2074 infinite operands X + -INF is negative, not positive. */
2075 || (code == PLUS_EXPR
2076 && (sgn1 >= 0
2077 ? !is_negative_overflow_infinity (val2)
2078 : is_positive_overflow_infinity (val2)))
2079 /* For subtraction, non-infinite operands must be of
2080 different signs to yield an overflow. Its sign is
2081 therefore that of the first operand or the opposite of
2082 that of the second operand. A first operand of 0 counts
2083 as positive here, for the corner case 0 - (-INF), which
2084 overflows, but must yield +INF. For infinite operands 0
2085 - INF is negative, not positive. */
2086 || (code == MINUS_EXPR
2087 && (sgn1 >= 0
2088 ? !is_positive_overflow_infinity (val2)
2089 : is_negative_overflow_infinity (val2)))
2090 /* We only get in here with positive shift count, so the
2091 overflow direction is the same as the sign of val1.
2092 Actually rshift does not overflow at all, but we only
2093 handle the case of shifting overflowed -INF and +INF. */
2094 || (code == RSHIFT_EXPR
2095 && sgn1 >= 0)
2096 /* For division, the only case is -INF / -1 = +INF. */
2097 || code == TRUNC_DIV_EXPR
2098 || code == FLOOR_DIV_EXPR
2099 || code == CEIL_DIV_EXPR
2100 || code == EXACT_DIV_EXPR
2101 || code == ROUND_DIV_EXPR)
2102 return (needs_overflow_infinity (TREE_TYPE (res))
2103 ? positive_overflow_infinity (TREE_TYPE (res))
2104 : TYPE_MAX_VALUE (TREE_TYPE (res)));
2105 else
2106 return (needs_overflow_infinity (TREE_TYPE (res))
2107 ? negative_overflow_infinity (TREE_TYPE (res))
2108 : TYPE_MIN_VALUE (TREE_TYPE (res)));
2111 return res;
2115 /* For range VR compute two wide_int bitmasks. In *MAY_BE_NONZERO
2116 bitmask if some bit is unset, it means for all numbers in the range
2117 the bit is 0, otherwise it might be 0 or 1. In *MUST_BE_NONZERO
2118 bitmask if some bit is set, it means for all numbers in the range
2119 the bit is 1, otherwise it might be 0 or 1. */
2121 static bool
2122 zero_nonzero_bits_from_vr (const tree expr_type,
2123 value_range_t *vr,
2124 wide_int *may_be_nonzero,
2125 wide_int *must_be_nonzero)
2127 *may_be_nonzero = wi::minus_one (TYPE_PRECISION (expr_type));
2128 *must_be_nonzero = wi::zero (TYPE_PRECISION (expr_type));
2129 if (!range_int_cst_p (vr)
2130 || is_overflow_infinity (vr->min)
2131 || is_overflow_infinity (vr->max))
2132 return false;
2134 if (range_int_cst_singleton_p (vr))
2136 *may_be_nonzero = vr->min;
2137 *must_be_nonzero = *may_be_nonzero;
2139 else if (tree_int_cst_sgn (vr->min) >= 0
2140 || tree_int_cst_sgn (vr->max) < 0)
2142 wide_int xor_mask = wi::bit_xor (vr->min, vr->max);
2143 *may_be_nonzero = wi::bit_or (vr->min, vr->max);
2144 *must_be_nonzero = wi::bit_and (vr->min, vr->max);
2145 if (xor_mask != 0)
2147 wide_int mask = wi::mask (wi::floor_log2 (xor_mask), false,
2148 may_be_nonzero->get_precision ());
2149 *may_be_nonzero = *may_be_nonzero | mask;
2150 *must_be_nonzero = must_be_nonzero->and_not (mask);
2154 return true;
2157 /* Create two value-ranges in *VR0 and *VR1 from the anti-range *AR
2158 so that *VR0 U *VR1 == *AR. Returns true if that is possible,
2159 false otherwise. If *AR can be represented with a single range
2160 *VR1 will be VR_UNDEFINED. */
2162 static bool
2163 ranges_from_anti_range (value_range_t *ar,
2164 value_range_t *vr0, value_range_t *vr1)
2166 tree type = TREE_TYPE (ar->min);
2168 vr0->type = VR_UNDEFINED;
2169 vr1->type = VR_UNDEFINED;
2171 if (ar->type != VR_ANTI_RANGE
2172 || TREE_CODE (ar->min) != INTEGER_CST
2173 || TREE_CODE (ar->max) != INTEGER_CST
2174 || !vrp_val_min (type)
2175 || !vrp_val_max (type))
2176 return false;
2178 if (!vrp_val_is_min (ar->min))
2180 vr0->type = VR_RANGE;
2181 vr0->min = vrp_val_min (type);
2182 vr0->max = wide_int_to_tree (type, wi::sub (ar->min, 1));
2184 if (!vrp_val_is_max (ar->max))
2186 vr1->type = VR_RANGE;
2187 vr1->min = wide_int_to_tree (type, wi::add (ar->max, 1));
2188 vr1->max = vrp_val_max (type);
2190 if (vr0->type == VR_UNDEFINED)
2192 *vr0 = *vr1;
2193 vr1->type = VR_UNDEFINED;
2196 return vr0->type != VR_UNDEFINED;
2199 /* Helper to extract a value-range *VR for a multiplicative operation
2200 *VR0 CODE *VR1. */
2202 static void
2203 extract_range_from_multiplicative_op_1 (value_range_t *vr,
2204 enum tree_code code,
2205 value_range_t *vr0, value_range_t *vr1)
2207 enum value_range_type type;
2208 tree val[4];
2209 size_t i;
2210 tree min, max;
2211 bool sop;
2212 int cmp;
2214 /* Multiplications, divisions and shifts are a bit tricky to handle,
2215 depending on the mix of signs we have in the two ranges, we
2216 need to operate on different values to get the minimum and
2217 maximum values for the new range. One approach is to figure
2218 out all the variations of range combinations and do the
2219 operations.
2221 However, this involves several calls to compare_values and it
2222 is pretty convoluted. It's simpler to do the 4 operations
2223 (MIN0 OP MIN1, MIN0 OP MAX1, MAX0 OP MIN1 and MAX0 OP MAX0 OP
2224 MAX1) and then figure the smallest and largest values to form
2225 the new range. */
2226 gcc_assert (code == MULT_EXPR
2227 || code == TRUNC_DIV_EXPR
2228 || code == FLOOR_DIV_EXPR
2229 || code == CEIL_DIV_EXPR
2230 || code == EXACT_DIV_EXPR
2231 || code == ROUND_DIV_EXPR
2232 || code == RSHIFT_EXPR
2233 || code == LSHIFT_EXPR);
2234 gcc_assert ((vr0->type == VR_RANGE
2235 || (code == MULT_EXPR && vr0->type == VR_ANTI_RANGE))
2236 && vr0->type == vr1->type);
2238 type = vr0->type;
2240 /* Compute the 4 cross operations. */
2241 sop = false;
2242 val[0] = vrp_int_const_binop (code, vr0->min, vr1->min);
2243 if (val[0] == NULL_TREE)
2244 sop = true;
2246 if (vr1->max == vr1->min)
2247 val[1] = NULL_TREE;
2248 else
2250 val[1] = vrp_int_const_binop (code, vr0->min, vr1->max);
2251 if (val[1] == NULL_TREE)
2252 sop = true;
2255 if (vr0->max == vr0->min)
2256 val[2] = NULL_TREE;
2257 else
2259 val[2] = vrp_int_const_binop (code, vr0->max, vr1->min);
2260 if (val[2] == NULL_TREE)
2261 sop = true;
2264 if (vr0->min == vr0->max || vr1->min == vr1->max)
2265 val[3] = NULL_TREE;
2266 else
2268 val[3] = vrp_int_const_binop (code, vr0->max, vr1->max);
2269 if (val[3] == NULL_TREE)
2270 sop = true;
2273 if (sop)
2275 set_value_range_to_varying (vr);
2276 return;
2279 /* Set MIN to the minimum of VAL[i] and MAX to the maximum
2280 of VAL[i]. */
2281 min = val[0];
2282 max = val[0];
2283 for (i = 1; i < 4; i++)
2285 if (!is_gimple_min_invariant (min)
2286 || (TREE_OVERFLOW (min) && !is_overflow_infinity (min))
2287 || !is_gimple_min_invariant (max)
2288 || (TREE_OVERFLOW (max) && !is_overflow_infinity (max)))
2289 break;
2291 if (val[i])
2293 if (!is_gimple_min_invariant (val[i])
2294 || (TREE_OVERFLOW (val[i])
2295 && !is_overflow_infinity (val[i])))
2297 /* If we found an overflowed value, set MIN and MAX
2298 to it so that we set the resulting range to
2299 VARYING. */
2300 min = max = val[i];
2301 break;
2304 if (compare_values (val[i], min) == -1)
2305 min = val[i];
2307 if (compare_values (val[i], max) == 1)
2308 max = val[i];
2312 /* If either MIN or MAX overflowed, then set the resulting range to
2313 VARYING. But we do accept an overflow infinity
2314 representation. */
2315 if (min == NULL_TREE
2316 || !is_gimple_min_invariant (min)
2317 || (TREE_OVERFLOW (min) && !is_overflow_infinity (min))
2318 || max == NULL_TREE
2319 || !is_gimple_min_invariant (max)
2320 || (TREE_OVERFLOW (max) && !is_overflow_infinity (max)))
2322 set_value_range_to_varying (vr);
2323 return;
2326 /* We punt if:
2327 1) [-INF, +INF]
2328 2) [-INF, +-INF(OVF)]
2329 3) [+-INF(OVF), +INF]
2330 4) [+-INF(OVF), +-INF(OVF)]
2331 We learn nothing when we have INF and INF(OVF) on both sides.
2332 Note that we do accept [-INF, -INF] and [+INF, +INF] without
2333 overflow. */
2334 if ((vrp_val_is_min (min) || is_overflow_infinity (min))
2335 && (vrp_val_is_max (max) || is_overflow_infinity (max)))
2337 set_value_range_to_varying (vr);
2338 return;
2341 cmp = compare_values (min, max);
2342 if (cmp == -2 || cmp == 1)
2344 /* If the new range has its limits swapped around (MIN > MAX),
2345 then the operation caused one of them to wrap around, mark
2346 the new range VARYING. */
2347 set_value_range_to_varying (vr);
2349 else
2350 set_value_range (vr, type, min, max, NULL);
2353 /* Extract range information from a binary operation CODE based on
2354 the ranges of each of its operands *VR0 and *VR1 with resulting
2355 type EXPR_TYPE. The resulting range is stored in *VR. */
2357 static void
2358 extract_range_from_binary_expr_1 (value_range_t *vr,
2359 enum tree_code code, tree expr_type,
2360 value_range_t *vr0_, value_range_t *vr1_)
2362 value_range_t vr0 = *vr0_, vr1 = *vr1_;
2363 value_range_t vrtem0 = VR_INITIALIZER, vrtem1 = VR_INITIALIZER;
2364 enum value_range_type type;
2365 tree min = NULL_TREE, max = NULL_TREE;
2366 int cmp;
2368 if (!INTEGRAL_TYPE_P (expr_type)
2369 && !POINTER_TYPE_P (expr_type))
2371 set_value_range_to_varying (vr);
2372 return;
2375 /* Not all binary expressions can be applied to ranges in a
2376 meaningful way. Handle only arithmetic operations. */
2377 if (code != PLUS_EXPR
2378 && code != MINUS_EXPR
2379 && code != POINTER_PLUS_EXPR
2380 && code != MULT_EXPR
2381 && code != TRUNC_DIV_EXPR
2382 && code != FLOOR_DIV_EXPR
2383 && code != CEIL_DIV_EXPR
2384 && code != EXACT_DIV_EXPR
2385 && code != ROUND_DIV_EXPR
2386 && code != TRUNC_MOD_EXPR
2387 && code != RSHIFT_EXPR
2388 && code != LSHIFT_EXPR
2389 && code != MIN_EXPR
2390 && code != MAX_EXPR
2391 && code != BIT_AND_EXPR
2392 && code != BIT_IOR_EXPR
2393 && code != BIT_XOR_EXPR)
2395 set_value_range_to_varying (vr);
2396 return;
2399 /* If both ranges are UNDEFINED, so is the result. */
2400 if (vr0.type == VR_UNDEFINED && vr1.type == VR_UNDEFINED)
2402 set_value_range_to_undefined (vr);
2403 return;
2405 /* If one of the ranges is UNDEFINED drop it to VARYING for the following
2406 code. At some point we may want to special-case operations that
2407 have UNDEFINED result for all or some value-ranges of the not UNDEFINED
2408 operand. */
2409 else if (vr0.type == VR_UNDEFINED)
2410 set_value_range_to_varying (&vr0);
2411 else if (vr1.type == VR_UNDEFINED)
2412 set_value_range_to_varying (&vr1);
2414 /* Now canonicalize anti-ranges to ranges when they are not symbolic
2415 and express ~[] op X as ([]' op X) U ([]'' op X). */
2416 if (vr0.type == VR_ANTI_RANGE
2417 && ranges_from_anti_range (&vr0, &vrtem0, &vrtem1))
2419 extract_range_from_binary_expr_1 (vr, code, expr_type, &vrtem0, vr1_);
2420 if (vrtem1.type != VR_UNDEFINED)
2422 value_range_t vrres = VR_INITIALIZER;
2423 extract_range_from_binary_expr_1 (&vrres, code, expr_type,
2424 &vrtem1, vr1_);
2425 vrp_meet (vr, &vrres);
2427 return;
2429 /* Likewise for X op ~[]. */
2430 if (vr1.type == VR_ANTI_RANGE
2431 && ranges_from_anti_range (&vr1, &vrtem0, &vrtem1))
2433 extract_range_from_binary_expr_1 (vr, code, expr_type, vr0_, &vrtem0);
2434 if (vrtem1.type != VR_UNDEFINED)
2436 value_range_t vrres = VR_INITIALIZER;
2437 extract_range_from_binary_expr_1 (&vrres, code, expr_type,
2438 vr0_, &vrtem1);
2439 vrp_meet (vr, &vrres);
2441 return;
2444 /* The type of the resulting value range defaults to VR0.TYPE. */
2445 type = vr0.type;
2447 /* Refuse to operate on VARYING ranges, ranges of different kinds
2448 and symbolic ranges. As an exception, we allow BIT_{AND,IOR}
2449 because we may be able to derive a useful range even if one of
2450 the operands is VR_VARYING or symbolic range. Similarly for
2451 divisions, MIN/MAX and PLUS/MINUS.
2453 TODO, we may be able to derive anti-ranges in some cases. */
2454 if (code != BIT_AND_EXPR
2455 && code != BIT_IOR_EXPR
2456 && code != TRUNC_DIV_EXPR
2457 && code != FLOOR_DIV_EXPR
2458 && code != CEIL_DIV_EXPR
2459 && code != EXACT_DIV_EXPR
2460 && code != ROUND_DIV_EXPR
2461 && code != TRUNC_MOD_EXPR
2462 && code != MIN_EXPR
2463 && code != MAX_EXPR
2464 && code != PLUS_EXPR
2465 && code != MINUS_EXPR
2466 && code != RSHIFT_EXPR
2467 && (vr0.type == VR_VARYING
2468 || vr1.type == VR_VARYING
2469 || vr0.type != vr1.type
2470 || symbolic_range_p (&vr0)
2471 || symbolic_range_p (&vr1)))
2473 set_value_range_to_varying (vr);
2474 return;
2477 /* Now evaluate the expression to determine the new range. */
2478 if (POINTER_TYPE_P (expr_type))
2480 if (code == MIN_EXPR || code == MAX_EXPR)
2482 /* For MIN/MAX expressions with pointers, we only care about
2483 nullness, if both are non null, then the result is nonnull.
2484 If both are null, then the result is null. Otherwise they
2485 are varying. */
2486 if (range_is_nonnull (&vr0) && range_is_nonnull (&vr1))
2487 set_value_range_to_nonnull (vr, expr_type);
2488 else if (range_is_null (&vr0) && range_is_null (&vr1))
2489 set_value_range_to_null (vr, expr_type);
2490 else
2491 set_value_range_to_varying (vr);
2493 else if (code == POINTER_PLUS_EXPR)
2495 /* For pointer types, we are really only interested in asserting
2496 whether the expression evaluates to non-NULL. */
2497 if (range_is_nonnull (&vr0) || range_is_nonnull (&vr1))
2498 set_value_range_to_nonnull (vr, expr_type);
2499 else if (range_is_null (&vr0) && range_is_null (&vr1))
2500 set_value_range_to_null (vr, expr_type);
2501 else
2502 set_value_range_to_varying (vr);
2504 else if (code == BIT_AND_EXPR)
2506 /* For pointer types, we are really only interested in asserting
2507 whether the expression evaluates to non-NULL. */
2508 if (range_is_nonnull (&vr0) && range_is_nonnull (&vr1))
2509 set_value_range_to_nonnull (vr, expr_type);
2510 else if (range_is_null (&vr0) || range_is_null (&vr1))
2511 set_value_range_to_null (vr, expr_type);
2512 else
2513 set_value_range_to_varying (vr);
2515 else
2516 set_value_range_to_varying (vr);
2518 return;
2521 /* For integer ranges, apply the operation to each end of the
2522 range and see what we end up with. */
2523 if (code == PLUS_EXPR || code == MINUS_EXPR)
2525 const bool minus_p = (code == MINUS_EXPR);
2526 tree min_op0 = vr0.min;
2527 tree min_op1 = minus_p ? vr1.max : vr1.min;
2528 tree max_op0 = vr0.max;
2529 tree max_op1 = minus_p ? vr1.min : vr1.max;
2530 tree sym_min_op0 = NULL_TREE;
2531 tree sym_min_op1 = NULL_TREE;
2532 tree sym_max_op0 = NULL_TREE;
2533 tree sym_max_op1 = NULL_TREE;
2534 bool neg_min_op0, neg_min_op1, neg_max_op0, neg_max_op1;
2536 /* If we have a PLUS or MINUS with two VR_RANGEs, either constant or
2537 single-symbolic ranges, try to compute the precise resulting range,
2538 but only if we know that this resulting range will also be constant
2539 or single-symbolic. */
2540 if (vr0.type == VR_RANGE && vr1.type == VR_RANGE
2541 && (TREE_CODE (min_op0) == INTEGER_CST
2542 || (sym_min_op0
2543 = get_single_symbol (min_op0, &neg_min_op0, &min_op0)))
2544 && (TREE_CODE (min_op1) == INTEGER_CST
2545 || (sym_min_op1
2546 = get_single_symbol (min_op1, &neg_min_op1, &min_op1)))
2547 && (!(sym_min_op0 && sym_min_op1)
2548 || (sym_min_op0 == sym_min_op1
2549 && neg_min_op0 == (minus_p ? neg_min_op1 : !neg_min_op1)))
2550 && (TREE_CODE (max_op0) == INTEGER_CST
2551 || (sym_max_op0
2552 = get_single_symbol (max_op0, &neg_max_op0, &max_op0)))
2553 && (TREE_CODE (max_op1) == INTEGER_CST
2554 || (sym_max_op1
2555 = get_single_symbol (max_op1, &neg_max_op1, &max_op1)))
2556 && (!(sym_max_op0 && sym_max_op1)
2557 || (sym_max_op0 == sym_max_op1
2558 && neg_max_op0 == (minus_p ? neg_max_op1 : !neg_max_op1))))
2560 const signop sgn = TYPE_SIGN (expr_type);
2561 const unsigned int prec = TYPE_PRECISION (expr_type);
2562 wide_int type_min, type_max, wmin, wmax;
2563 int min_ovf = 0;
2564 int max_ovf = 0;
2566 /* Get the lower and upper bounds of the type. */
2567 if (TYPE_OVERFLOW_WRAPS (expr_type))
2569 type_min = wi::min_value (prec, sgn);
2570 type_max = wi::max_value (prec, sgn);
2572 else
2574 type_min = vrp_val_min (expr_type);
2575 type_max = vrp_val_max (expr_type);
2578 /* Combine the lower bounds, if any. */
2579 if (min_op0 && min_op1)
2581 if (minus_p)
2583 wmin = wi::sub (min_op0, min_op1);
2585 /* Check for overflow. */
2586 if (wi::cmp (0, min_op1, sgn)
2587 != wi::cmp (wmin, min_op0, sgn))
2588 min_ovf = wi::cmp (min_op0, min_op1, sgn);
2590 else
2592 wmin = wi::add (min_op0, min_op1);
2594 /* Check for overflow. */
2595 if (wi::cmp (min_op1, 0, sgn)
2596 != wi::cmp (wmin, min_op0, sgn))
2597 min_ovf = wi::cmp (min_op0, wmin, sgn);
2600 else if (min_op0)
2601 wmin = min_op0;
2602 else if (min_op1)
2603 wmin = minus_p ? wi::neg (min_op1) : min_op1;
2604 else
2605 wmin = wi::shwi (0, prec);
2607 /* Combine the upper bounds, if any. */
2608 if (max_op0 && max_op1)
2610 if (minus_p)
2612 wmax = wi::sub (max_op0, max_op1);
2614 /* Check for overflow. */
2615 if (wi::cmp (0, max_op1, sgn)
2616 != wi::cmp (wmax, max_op0, sgn))
2617 max_ovf = wi::cmp (max_op0, max_op1, sgn);
2619 else
2621 wmax = wi::add (max_op0, max_op1);
2623 if (wi::cmp (max_op1, 0, sgn)
2624 != wi::cmp (wmax, max_op0, sgn))
2625 max_ovf = wi::cmp (max_op0, wmax, sgn);
2628 else if (max_op0)
2629 wmax = max_op0;
2630 else if (max_op1)
2631 wmax = minus_p ? wi::neg (max_op1) : max_op1;
2632 else
2633 wmax = wi::shwi (0, prec);
2635 /* Check for type overflow. */
2636 if (min_ovf == 0)
2638 if (wi::cmp (wmin, type_min, sgn) == -1)
2639 min_ovf = -1;
2640 else if (wi::cmp (wmin, type_max, sgn) == 1)
2641 min_ovf = 1;
2643 if (max_ovf == 0)
2645 if (wi::cmp (wmax, type_min, sgn) == -1)
2646 max_ovf = -1;
2647 else if (wi::cmp (wmax, type_max, sgn) == 1)
2648 max_ovf = 1;
2651 /* If we have overflow for the constant part and the resulting
2652 range will be symbolic, drop to VR_VARYING. */
2653 if ((min_ovf && sym_min_op0 != sym_min_op1)
2654 || (max_ovf && sym_max_op0 != sym_max_op1))
2656 set_value_range_to_varying (vr);
2657 return;
2660 if (TYPE_OVERFLOW_WRAPS (expr_type))
2662 /* If overflow wraps, truncate the values and adjust the
2663 range kind and bounds appropriately. */
2664 wide_int tmin = wide_int::from (wmin, prec, sgn);
2665 wide_int tmax = wide_int::from (wmax, prec, sgn);
2666 if (min_ovf == max_ovf)
2668 /* No overflow or both overflow or underflow. The
2669 range kind stays VR_RANGE. */
2670 min = wide_int_to_tree (expr_type, tmin);
2671 max = wide_int_to_tree (expr_type, tmax);
2673 else if (min_ovf == -1 && max_ovf == 1)
2675 /* Underflow and overflow, drop to VR_VARYING. */
2676 set_value_range_to_varying (vr);
2677 return;
2679 else
2681 /* Min underflow or max overflow. The range kind
2682 changes to VR_ANTI_RANGE. */
2683 bool covers = false;
2684 wide_int tem = tmin;
2685 gcc_assert ((min_ovf == -1 && max_ovf == 0)
2686 || (max_ovf == 1 && min_ovf == 0));
2687 type = VR_ANTI_RANGE;
2688 tmin = tmax + 1;
2689 if (wi::cmp (tmin, tmax, sgn) < 0)
2690 covers = true;
2691 tmax = tem - 1;
2692 if (wi::cmp (tmax, tem, sgn) > 0)
2693 covers = true;
2694 /* If the anti-range would cover nothing, drop to varying.
2695 Likewise if the anti-range bounds are outside of the
2696 types values. */
2697 if (covers || wi::cmp (tmin, tmax, sgn) > 0)
2699 set_value_range_to_varying (vr);
2700 return;
2702 min = wide_int_to_tree (expr_type, tmin);
2703 max = wide_int_to_tree (expr_type, tmax);
2706 else
2708 /* If overflow does not wrap, saturate to the types min/max
2709 value. */
2710 if (min_ovf == -1)
2712 if (needs_overflow_infinity (expr_type)
2713 && supports_overflow_infinity (expr_type))
2714 min = negative_overflow_infinity (expr_type);
2715 else
2716 min = wide_int_to_tree (expr_type, type_min);
2718 else if (min_ovf == 1)
2720 if (needs_overflow_infinity (expr_type)
2721 && supports_overflow_infinity (expr_type))
2722 min = positive_overflow_infinity (expr_type);
2723 else
2724 min = wide_int_to_tree (expr_type, type_max);
2726 else
2727 min = wide_int_to_tree (expr_type, wmin);
2729 if (max_ovf == -1)
2731 if (needs_overflow_infinity (expr_type)
2732 && supports_overflow_infinity (expr_type))
2733 max = negative_overflow_infinity (expr_type);
2734 else
2735 max = wide_int_to_tree (expr_type, type_min);
2737 else if (max_ovf == 1)
2739 if (needs_overflow_infinity (expr_type)
2740 && supports_overflow_infinity (expr_type))
2741 max = positive_overflow_infinity (expr_type);
2742 else
2743 max = wide_int_to_tree (expr_type, type_max);
2745 else
2746 max = wide_int_to_tree (expr_type, wmax);
2749 if (needs_overflow_infinity (expr_type)
2750 && supports_overflow_infinity (expr_type))
2752 if ((min_op0 && is_negative_overflow_infinity (min_op0))
2753 || (min_op1
2754 && (minus_p
2755 ? is_positive_overflow_infinity (min_op1)
2756 : is_negative_overflow_infinity (min_op1))))
2757 min = negative_overflow_infinity (expr_type);
2758 if ((max_op0 && is_positive_overflow_infinity (max_op0))
2759 || (max_op1
2760 && (minus_p
2761 ? is_negative_overflow_infinity (max_op1)
2762 : is_positive_overflow_infinity (max_op1))))
2763 max = positive_overflow_infinity (expr_type);
2766 /* If the result lower bound is constant, we're done;
2767 otherwise, build the symbolic lower bound. */
2768 if (sym_min_op0 == sym_min_op1)
2770 else if (sym_min_op0)
2771 min = build_symbolic_expr (expr_type, sym_min_op0,
2772 neg_min_op0, min);
2773 else if (sym_min_op1)
2774 min = build_symbolic_expr (expr_type, sym_min_op1,
2775 neg_min_op1 ^ minus_p, min);
2777 /* Likewise for the upper bound. */
2778 if (sym_max_op0 == sym_max_op1)
2780 else if (sym_max_op0)
2781 max = build_symbolic_expr (expr_type, sym_max_op0,
2782 neg_max_op0, max);
2783 else if (sym_max_op1)
2784 max = build_symbolic_expr (expr_type, sym_max_op1,
2785 neg_max_op1 ^ minus_p, max);
2787 else
2789 /* For other cases, for example if we have a PLUS_EXPR with two
2790 VR_ANTI_RANGEs, drop to VR_VARYING. It would take more effort
2791 to compute a precise range for such a case.
2792 ??? General even mixed range kind operations can be expressed
2793 by for example transforming ~[3, 5] + [1, 2] to range-only
2794 operations and a union primitive:
2795 [-INF, 2] + [1, 2] U [5, +INF] + [1, 2]
2796 [-INF+1, 4] U [6, +INF(OVF)]
2797 though usually the union is not exactly representable with
2798 a single range or anti-range as the above is
2799 [-INF+1, +INF(OVF)] intersected with ~[5, 5]
2800 but one could use a scheme similar to equivalences for this. */
2801 set_value_range_to_varying (vr);
2802 return;
2805 else if (code == MIN_EXPR
2806 || code == MAX_EXPR)
2808 if (vr0.type == VR_RANGE
2809 && !symbolic_range_p (&vr0))
2811 type = VR_RANGE;
2812 if (vr1.type == VR_RANGE
2813 && !symbolic_range_p (&vr1))
2815 /* For operations that make the resulting range directly
2816 proportional to the original ranges, apply the operation to
2817 the same end of each range. */
2818 min = vrp_int_const_binop (code, vr0.min, vr1.min);
2819 max = vrp_int_const_binop (code, vr0.max, vr1.max);
2821 else if (code == MIN_EXPR)
2823 min = vrp_val_min (expr_type);
2824 max = vr0.max;
2826 else if (code == MAX_EXPR)
2828 min = vr0.min;
2829 max = vrp_val_max (expr_type);
2832 else if (vr1.type == VR_RANGE
2833 && !symbolic_range_p (&vr1))
2835 type = VR_RANGE;
2836 if (code == MIN_EXPR)
2838 min = vrp_val_min (expr_type);
2839 max = vr1.max;
2841 else if (code == MAX_EXPR)
2843 min = vr1.min;
2844 max = vrp_val_max (expr_type);
2847 else
2849 set_value_range_to_varying (vr);
2850 return;
2853 else if (code == MULT_EXPR)
2855 /* Fancy code so that with unsigned, [-3,-1]*[-3,-1] does not
2856 drop to varying. This test requires 2*prec bits if both
2857 operands are signed and 2*prec + 2 bits if either is not. */
2859 signop sign = TYPE_SIGN (expr_type);
2860 unsigned int prec = TYPE_PRECISION (expr_type);
2862 if (range_int_cst_p (&vr0)
2863 && range_int_cst_p (&vr1)
2864 && TYPE_OVERFLOW_WRAPS (expr_type))
2866 typedef FIXED_WIDE_INT (WIDE_INT_MAX_PRECISION * 2) vrp_int;
2867 typedef generic_wide_int
2868 <wi::extended_tree <WIDE_INT_MAX_PRECISION * 2> > vrp_int_cst;
2869 vrp_int sizem1 = wi::mask <vrp_int> (prec, false);
2870 vrp_int size = sizem1 + 1;
2872 /* Extend the values using the sign of the result to PREC2.
2873 From here on out, everthing is just signed math no matter
2874 what the input types were. */
2875 vrp_int min0 = vrp_int_cst (vr0.min);
2876 vrp_int max0 = vrp_int_cst (vr0.max);
2877 vrp_int min1 = vrp_int_cst (vr1.min);
2878 vrp_int max1 = vrp_int_cst (vr1.max);
2879 /* Canonicalize the intervals. */
2880 if (sign == UNSIGNED)
2882 if (wi::ltu_p (size, min0 + max0))
2884 min0 -= size;
2885 max0 -= size;
2888 if (wi::ltu_p (size, min1 + max1))
2890 min1 -= size;
2891 max1 -= size;
2895 vrp_int prod0 = min0 * min1;
2896 vrp_int prod1 = min0 * max1;
2897 vrp_int prod2 = max0 * min1;
2898 vrp_int prod3 = max0 * max1;
2900 /* Sort the 4 products so that min is in prod0 and max is in
2901 prod3. */
2902 /* min0min1 > max0max1 */
2903 if (wi::gts_p (prod0, prod3))
2904 std::swap (prod0, prod3);
2906 /* min0max1 > max0min1 */
2907 if (wi::gts_p (prod1, prod2))
2908 std::swap (prod1, prod2);
2910 if (wi::gts_p (prod0, prod1))
2911 std::swap (prod0, prod1);
2913 if (wi::gts_p (prod2, prod3))
2914 std::swap (prod2, prod3);
2916 /* diff = max - min. */
2917 prod2 = prod3 - prod0;
2918 if (wi::geu_p (prod2, sizem1))
2920 /* the range covers all values. */
2921 set_value_range_to_varying (vr);
2922 return;
2925 /* The following should handle the wrapping and selecting
2926 VR_ANTI_RANGE for us. */
2927 min = wide_int_to_tree (expr_type, prod0);
2928 max = wide_int_to_tree (expr_type, prod3);
2929 set_and_canonicalize_value_range (vr, VR_RANGE, min, max, NULL);
2930 return;
2933 /* If we have an unsigned MULT_EXPR with two VR_ANTI_RANGEs,
2934 drop to VR_VARYING. It would take more effort to compute a
2935 precise range for such a case. For example, if we have
2936 op0 == 65536 and op1 == 65536 with their ranges both being
2937 ~[0,0] on a 32-bit machine, we would have op0 * op1 == 0, so
2938 we cannot claim that the product is in ~[0,0]. Note that we
2939 are guaranteed to have vr0.type == vr1.type at this
2940 point. */
2941 if (vr0.type == VR_ANTI_RANGE
2942 && !TYPE_OVERFLOW_UNDEFINED (expr_type))
2944 set_value_range_to_varying (vr);
2945 return;
2948 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
2949 return;
2951 else if (code == RSHIFT_EXPR
2952 || code == LSHIFT_EXPR)
2954 /* If we have a RSHIFT_EXPR with any shift values outside [0..prec-1],
2955 then drop to VR_VARYING. Outside of this range we get undefined
2956 behavior from the shift operation. We cannot even trust
2957 SHIFT_COUNT_TRUNCATED at this stage, because that applies to rtl
2958 shifts, and the operation at the tree level may be widened. */
2959 if (range_int_cst_p (&vr1)
2960 && compare_tree_int (vr1.min, 0) >= 0
2961 && compare_tree_int (vr1.max, TYPE_PRECISION (expr_type)) == -1)
2963 if (code == RSHIFT_EXPR)
2965 /* Even if vr0 is VARYING or otherwise not usable, we can derive
2966 useful ranges just from the shift count. E.g.
2967 x >> 63 for signed 64-bit x is always [-1, 0]. */
2968 if (vr0.type != VR_RANGE || symbolic_range_p (&vr0))
2970 vr0.type = type = VR_RANGE;
2971 vr0.min = vrp_val_min (expr_type);
2972 vr0.max = vrp_val_max (expr_type);
2974 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
2975 return;
2977 /* We can map lshifts by constants to MULT_EXPR handling. */
2978 else if (code == LSHIFT_EXPR
2979 && range_int_cst_singleton_p (&vr1))
2981 bool saved_flag_wrapv;
2982 value_range_t vr1p = VR_INITIALIZER;
2983 vr1p.type = VR_RANGE;
2984 vr1p.min = (wide_int_to_tree
2985 (expr_type,
2986 wi::set_bit_in_zero (tree_to_shwi (vr1.min),
2987 TYPE_PRECISION (expr_type))));
2988 vr1p.max = vr1p.min;
2989 /* We have to use a wrapping multiply though as signed overflow
2990 on lshifts is implementation defined in C89. */
2991 saved_flag_wrapv = flag_wrapv;
2992 flag_wrapv = 1;
2993 extract_range_from_binary_expr_1 (vr, MULT_EXPR, expr_type,
2994 &vr0, &vr1p);
2995 flag_wrapv = saved_flag_wrapv;
2996 return;
2998 else if (code == LSHIFT_EXPR
2999 && range_int_cst_p (&vr0))
3001 int prec = TYPE_PRECISION (expr_type);
3002 int overflow_pos = prec;
3003 int bound_shift;
3004 wide_int low_bound, high_bound;
3005 bool uns = TYPE_UNSIGNED (expr_type);
3006 bool in_bounds = false;
3008 if (!uns)
3009 overflow_pos -= 1;
3011 bound_shift = overflow_pos - tree_to_shwi (vr1.max);
3012 /* If bound_shift == HOST_BITS_PER_WIDE_INT, the llshift can
3013 overflow. However, for that to happen, vr1.max needs to be
3014 zero, which means vr1 is a singleton range of zero, which
3015 means it should be handled by the previous LSHIFT_EXPR
3016 if-clause. */
3017 wide_int bound = wi::set_bit_in_zero (bound_shift, prec);
3018 wide_int complement = ~(bound - 1);
3020 if (uns)
3022 low_bound = bound;
3023 high_bound = complement;
3024 if (wi::ltu_p (vr0.max, low_bound))
3026 /* [5, 6] << [1, 2] == [10, 24]. */
3027 /* We're shifting out only zeroes, the value increases
3028 monotonically. */
3029 in_bounds = true;
3031 else if (wi::ltu_p (high_bound, vr0.min))
3033 /* [0xffffff00, 0xffffffff] << [1, 2]
3034 == [0xfffffc00, 0xfffffffe]. */
3035 /* We're shifting out only ones, the value decreases
3036 monotonically. */
3037 in_bounds = true;
3040 else
3042 /* [-1, 1] << [1, 2] == [-4, 4]. */
3043 low_bound = complement;
3044 high_bound = bound;
3045 if (wi::lts_p (vr0.max, high_bound)
3046 && wi::lts_p (low_bound, vr0.min))
3048 /* For non-negative numbers, we're shifting out only
3049 zeroes, the value increases monotonically.
3050 For negative numbers, we're shifting out only ones, the
3051 value decreases monotomically. */
3052 in_bounds = true;
3056 if (in_bounds)
3058 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
3059 return;
3063 set_value_range_to_varying (vr);
3064 return;
3066 else if (code == TRUNC_DIV_EXPR
3067 || code == FLOOR_DIV_EXPR
3068 || code == CEIL_DIV_EXPR
3069 || code == EXACT_DIV_EXPR
3070 || code == ROUND_DIV_EXPR)
3072 if (vr0.type != VR_RANGE || symbolic_range_p (&vr0))
3074 /* For division, if op1 has VR_RANGE but op0 does not, something
3075 can be deduced just from that range. Say [min, max] / [4, max]
3076 gives [min / 4, max / 4] range. */
3077 if (vr1.type == VR_RANGE
3078 && !symbolic_range_p (&vr1)
3079 && range_includes_zero_p (vr1.min, vr1.max) == 0)
3081 vr0.type = type = VR_RANGE;
3082 vr0.min = vrp_val_min (expr_type);
3083 vr0.max = vrp_val_max (expr_type);
3085 else
3087 set_value_range_to_varying (vr);
3088 return;
3092 /* For divisions, if flag_non_call_exceptions is true, we must
3093 not eliminate a division by zero. */
3094 if (cfun->can_throw_non_call_exceptions
3095 && (vr1.type != VR_RANGE
3096 || range_includes_zero_p (vr1.min, vr1.max) != 0))
3098 set_value_range_to_varying (vr);
3099 return;
3102 /* For divisions, if op0 is VR_RANGE, we can deduce a range
3103 even if op1 is VR_VARYING, VR_ANTI_RANGE, symbolic or can
3104 include 0. */
3105 if (vr0.type == VR_RANGE
3106 && (vr1.type != VR_RANGE
3107 || range_includes_zero_p (vr1.min, vr1.max) != 0))
3109 tree zero = build_int_cst (TREE_TYPE (vr0.min), 0);
3110 int cmp;
3112 min = NULL_TREE;
3113 max = NULL_TREE;
3114 if (TYPE_UNSIGNED (expr_type)
3115 || value_range_nonnegative_p (&vr1))
3117 /* For unsigned division or when divisor is known
3118 to be non-negative, the range has to cover
3119 all numbers from 0 to max for positive max
3120 and all numbers from min to 0 for negative min. */
3121 cmp = compare_values (vr0.max, zero);
3122 if (cmp == -1)
3124 /* When vr0.max < 0, vr1.min != 0 and value
3125 ranges for dividend and divisor are available. */
3126 if (vr1.type == VR_RANGE
3127 && !symbolic_range_p (&vr0)
3128 && !symbolic_range_p (&vr1)
3129 && !compare_values (vr1.min, zero))
3130 max = int_const_binop (code, vr0.max, vr1.min);
3131 else
3132 max = zero;
3134 else if (cmp == 0 || cmp == 1)
3135 max = vr0.max;
3136 else
3137 type = VR_VARYING;
3138 cmp = compare_values (vr0.min, zero);
3139 if (cmp == 1)
3141 /* For unsigned division when value ranges for dividend
3142 and divisor are available. */
3143 if (vr1.type == VR_RANGE
3144 && !symbolic_range_p (&vr0)
3145 && !symbolic_range_p (&vr1))
3146 min = int_const_binop (code, vr0.min, vr1.max);
3147 else
3148 min = zero;
3150 else if (cmp == 0 || cmp == -1)
3151 min = vr0.min;
3152 else
3153 type = VR_VARYING;
3155 else
3157 /* Otherwise the range is -max .. max or min .. -min
3158 depending on which bound is bigger in absolute value,
3159 as the division can change the sign. */
3160 abs_extent_range (vr, vr0.min, vr0.max);
3161 return;
3163 if (type == VR_VARYING)
3165 set_value_range_to_varying (vr);
3166 return;
3169 else
3171 extract_range_from_multiplicative_op_1 (vr, code, &vr0, &vr1);
3172 return;
3175 else if (code == TRUNC_MOD_EXPR)
3177 if (range_is_null (&vr1))
3179 set_value_range_to_undefined (vr);
3180 return;
3182 /* ABS (A % B) < ABS (B) and either
3183 0 <= A % B <= A or A <= A % B <= 0. */
3184 type = VR_RANGE;
3185 signop sgn = TYPE_SIGN (expr_type);
3186 unsigned int prec = TYPE_PRECISION (expr_type);
3187 wide_int wmin, wmax, tmp;
3188 wide_int zero = wi::zero (prec);
3189 wide_int one = wi::one (prec);
3190 if (vr1.type == VR_RANGE && !symbolic_range_p (&vr1))
3192 wmax = wi::sub (vr1.max, one);
3193 if (sgn == SIGNED)
3195 tmp = wi::sub (wi::minus_one (prec), vr1.min);
3196 wmax = wi::smax (wmax, tmp);
3199 else
3201 wmax = wi::max_value (prec, sgn);
3202 /* X % INT_MIN may be INT_MAX. */
3203 if (sgn == UNSIGNED)
3204 wmax = wmax - one;
3207 if (sgn == UNSIGNED)
3208 wmin = zero;
3209 else
3211 wmin = -wmax;
3212 if (vr0.type == VR_RANGE && TREE_CODE (vr0.min) == INTEGER_CST)
3214 tmp = vr0.min;
3215 if (wi::gts_p (tmp, zero))
3216 tmp = zero;
3217 wmin = wi::smax (wmin, tmp);
3221 if (vr0.type == VR_RANGE && TREE_CODE (vr0.max) == INTEGER_CST)
3223 tmp = vr0.max;
3224 if (sgn == SIGNED && wi::neg_p (tmp))
3225 tmp = zero;
3226 wmax = wi::min (wmax, tmp, sgn);
3229 min = wide_int_to_tree (expr_type, wmin);
3230 max = wide_int_to_tree (expr_type, wmax);
3232 else if (code == BIT_AND_EXPR || code == BIT_IOR_EXPR || code == BIT_XOR_EXPR)
3234 bool int_cst_range0, int_cst_range1;
3235 wide_int may_be_nonzero0, may_be_nonzero1;
3236 wide_int must_be_nonzero0, must_be_nonzero1;
3238 int_cst_range0 = zero_nonzero_bits_from_vr (expr_type, &vr0,
3239 &may_be_nonzero0,
3240 &must_be_nonzero0);
3241 int_cst_range1 = zero_nonzero_bits_from_vr (expr_type, &vr1,
3242 &may_be_nonzero1,
3243 &must_be_nonzero1);
3245 type = VR_RANGE;
3246 if (code == BIT_AND_EXPR)
3248 min = wide_int_to_tree (expr_type,
3249 must_be_nonzero0 & must_be_nonzero1);
3250 wide_int wmax = may_be_nonzero0 & may_be_nonzero1;
3251 /* If both input ranges contain only negative values we can
3252 truncate the result range maximum to the minimum of the
3253 input range maxima. */
3254 if (int_cst_range0 && int_cst_range1
3255 && tree_int_cst_sgn (vr0.max) < 0
3256 && tree_int_cst_sgn (vr1.max) < 0)
3258 wmax = wi::min (wmax, vr0.max, TYPE_SIGN (expr_type));
3259 wmax = wi::min (wmax, vr1.max, TYPE_SIGN (expr_type));
3261 /* If either input range contains only non-negative values
3262 we can truncate the result range maximum to the respective
3263 maximum of the input range. */
3264 if (int_cst_range0 && tree_int_cst_sgn (vr0.min) >= 0)
3265 wmax = wi::min (wmax, vr0.max, TYPE_SIGN (expr_type));
3266 if (int_cst_range1 && tree_int_cst_sgn (vr1.min) >= 0)
3267 wmax = wi::min (wmax, vr1.max, TYPE_SIGN (expr_type));
3268 max = wide_int_to_tree (expr_type, wmax);
3270 else if (code == BIT_IOR_EXPR)
3272 max = wide_int_to_tree (expr_type,
3273 may_be_nonzero0 | may_be_nonzero1);
3274 wide_int wmin = must_be_nonzero0 | must_be_nonzero1;
3275 /* If the input ranges contain only positive values we can
3276 truncate the minimum of the result range to the maximum
3277 of the input range minima. */
3278 if (int_cst_range0 && int_cst_range1
3279 && tree_int_cst_sgn (vr0.min) >= 0
3280 && tree_int_cst_sgn (vr1.min) >= 0)
3282 wmin = wi::max (wmin, vr0.min, TYPE_SIGN (expr_type));
3283 wmin = wi::max (wmin, vr1.min, TYPE_SIGN (expr_type));
3285 /* If either input range contains only negative values
3286 we can truncate the minimum of the result range to the
3287 respective minimum range. */
3288 if (int_cst_range0 && tree_int_cst_sgn (vr0.max) < 0)
3289 wmin = wi::max (wmin, vr0.min, TYPE_SIGN (expr_type));
3290 if (int_cst_range1 && tree_int_cst_sgn (vr1.max) < 0)
3291 wmin = wi::max (wmin, vr1.min, TYPE_SIGN (expr_type));
3292 min = wide_int_to_tree (expr_type, wmin);
3294 else if (code == BIT_XOR_EXPR)
3296 wide_int result_zero_bits = ((must_be_nonzero0 & must_be_nonzero1)
3297 | ~(may_be_nonzero0 | may_be_nonzero1));
3298 wide_int result_one_bits
3299 = (must_be_nonzero0.and_not (may_be_nonzero1)
3300 | must_be_nonzero1.and_not (may_be_nonzero0));
3301 max = wide_int_to_tree (expr_type, ~result_zero_bits);
3302 min = wide_int_to_tree (expr_type, result_one_bits);
3303 /* If the range has all positive or all negative values the
3304 result is better than VARYING. */
3305 if (tree_int_cst_sgn (min) < 0
3306 || tree_int_cst_sgn (max) >= 0)
3308 else
3309 max = min = NULL_TREE;
3312 else
3313 gcc_unreachable ();
3315 /* If either MIN or MAX overflowed, then set the resulting range to
3316 VARYING. But we do accept an overflow infinity representation. */
3317 if (min == NULL_TREE
3318 || (TREE_OVERFLOW_P (min) && !is_overflow_infinity (min))
3319 || max == NULL_TREE
3320 || (TREE_OVERFLOW_P (max) && !is_overflow_infinity (max)))
3322 set_value_range_to_varying (vr);
3323 return;
3326 /* We punt if:
3327 1) [-INF, +INF]
3328 2) [-INF, +-INF(OVF)]
3329 3) [+-INF(OVF), +INF]
3330 4) [+-INF(OVF), +-INF(OVF)]
3331 We learn nothing when we have INF and INF(OVF) on both sides.
3332 Note that we do accept [-INF, -INF] and [+INF, +INF] without
3333 overflow. */
3334 if ((vrp_val_is_min (min) || is_overflow_infinity (min))
3335 && (vrp_val_is_max (max) || is_overflow_infinity (max)))
3337 set_value_range_to_varying (vr);
3338 return;
3341 cmp = compare_values (min, max);
3342 if (cmp == -2 || cmp == 1)
3344 /* If the new range has its limits swapped around (MIN > MAX),
3345 then the operation caused one of them to wrap around, mark
3346 the new range VARYING. */
3347 set_value_range_to_varying (vr);
3349 else
3350 set_value_range (vr, type, min, max, NULL);
3353 /* Extract range information from a binary expression OP0 CODE OP1 based on
3354 the ranges of each of its operands with resulting type EXPR_TYPE.
3355 The resulting range is stored in *VR. */
3357 static void
3358 extract_range_from_binary_expr (value_range_t *vr,
3359 enum tree_code code,
3360 tree expr_type, tree op0, tree op1)
3362 value_range_t vr0 = VR_INITIALIZER;
3363 value_range_t vr1 = VR_INITIALIZER;
3365 /* Get value ranges for each operand. For constant operands, create
3366 a new value range with the operand to simplify processing. */
3367 if (TREE_CODE (op0) == SSA_NAME)
3368 vr0 = *(get_value_range (op0));
3369 else if (is_gimple_min_invariant (op0))
3370 set_value_range_to_value (&vr0, op0, NULL);
3371 else
3372 set_value_range_to_varying (&vr0);
3374 if (TREE_CODE (op1) == SSA_NAME)
3375 vr1 = *(get_value_range (op1));
3376 else if (is_gimple_min_invariant (op1))
3377 set_value_range_to_value (&vr1, op1, NULL);
3378 else
3379 set_value_range_to_varying (&vr1);
3381 extract_range_from_binary_expr_1 (vr, code, expr_type, &vr0, &vr1);
3383 /* Try harder for PLUS and MINUS if the range of one operand is symbolic
3384 and based on the other operand, for example if it was deduced from a
3385 symbolic comparison. When a bound of the range of the first operand
3386 is invariant, we set the corresponding bound of the new range to INF
3387 in order to avoid recursing on the range of the second operand. */
3388 if (vr->type == VR_VARYING
3389 && (code == PLUS_EXPR || code == MINUS_EXPR)
3390 && TREE_CODE (op1) == SSA_NAME
3391 && vr0.type == VR_RANGE
3392 && symbolic_range_based_on_p (&vr0, op1))
3394 const bool minus_p = (code == MINUS_EXPR);
3395 value_range_t n_vr1 = VR_INITIALIZER;
3397 /* Try with VR0 and [-INF, OP1]. */
3398 if (is_gimple_min_invariant (minus_p ? vr0.max : vr0.min))
3399 set_value_range (&n_vr1, VR_RANGE, vrp_val_min (expr_type), op1, NULL);
3401 /* Try with VR0 and [OP1, +INF]. */
3402 else if (is_gimple_min_invariant (minus_p ? vr0.min : vr0.max))
3403 set_value_range (&n_vr1, VR_RANGE, op1, vrp_val_max (expr_type), NULL);
3405 /* Try with VR0 and [OP1, OP1]. */
3406 else
3407 set_value_range (&n_vr1, VR_RANGE, op1, op1, NULL);
3409 extract_range_from_binary_expr_1 (vr, code, expr_type, &vr0, &n_vr1);
3412 if (vr->type == VR_VARYING
3413 && (code == PLUS_EXPR || code == MINUS_EXPR)
3414 && TREE_CODE (op0) == SSA_NAME
3415 && vr1.type == VR_RANGE
3416 && symbolic_range_based_on_p (&vr1, op0))
3418 const bool minus_p = (code == MINUS_EXPR);
3419 value_range_t n_vr0 = VR_INITIALIZER;
3421 /* Try with [-INF, OP0] and VR1. */
3422 if (is_gimple_min_invariant (minus_p ? vr1.max : vr1.min))
3423 set_value_range (&n_vr0, VR_RANGE, vrp_val_min (expr_type), op0, NULL);
3425 /* Try with [OP0, +INF] and VR1. */
3426 else if (is_gimple_min_invariant (minus_p ? vr1.min : vr1.max))
3427 set_value_range (&n_vr0, VR_RANGE, op0, vrp_val_max (expr_type), NULL);
3429 /* Try with [OP0, OP0] and VR1. */
3430 else
3431 set_value_range (&n_vr0, VR_RANGE, op0, op0, NULL);
3433 extract_range_from_binary_expr_1 (vr, code, expr_type, &n_vr0, &vr1);
3437 /* Extract range information from a unary operation CODE based on
3438 the range of its operand *VR0 with type OP0_TYPE with resulting type TYPE.
3439 The The resulting range is stored in *VR. */
3441 static void
3442 extract_range_from_unary_expr_1 (value_range_t *vr,
3443 enum tree_code code, tree type,
3444 value_range_t *vr0_, tree op0_type)
3446 value_range_t vr0 = *vr0_, vrtem0 = VR_INITIALIZER, vrtem1 = VR_INITIALIZER;
3448 /* VRP only operates on integral and pointer types. */
3449 if (!(INTEGRAL_TYPE_P (op0_type)
3450 || POINTER_TYPE_P (op0_type))
3451 || !(INTEGRAL_TYPE_P (type)
3452 || POINTER_TYPE_P (type)))
3454 set_value_range_to_varying (vr);
3455 return;
3458 /* If VR0 is UNDEFINED, so is the result. */
3459 if (vr0.type == VR_UNDEFINED)
3461 set_value_range_to_undefined (vr);
3462 return;
3465 /* Handle operations that we express in terms of others. */
3466 if (code == PAREN_EXPR || code == OBJ_TYPE_REF)
3468 /* PAREN_EXPR and OBJ_TYPE_REF are simple copies. */
3469 copy_value_range (vr, &vr0);
3470 return;
3472 else if (code == NEGATE_EXPR)
3474 /* -X is simply 0 - X, so re-use existing code that also handles
3475 anti-ranges fine. */
3476 value_range_t zero = VR_INITIALIZER;
3477 set_value_range_to_value (&zero, build_int_cst (type, 0), NULL);
3478 extract_range_from_binary_expr_1 (vr, MINUS_EXPR, type, &zero, &vr0);
3479 return;
3481 else if (code == BIT_NOT_EXPR)
3483 /* ~X is simply -1 - X, so re-use existing code that also handles
3484 anti-ranges fine. */
3485 value_range_t minusone = VR_INITIALIZER;
3486 set_value_range_to_value (&minusone, build_int_cst (type, -1), NULL);
3487 extract_range_from_binary_expr_1 (vr, MINUS_EXPR,
3488 type, &minusone, &vr0);
3489 return;
3492 /* Now canonicalize anti-ranges to ranges when they are not symbolic
3493 and express op ~[] as (op []') U (op []''). */
3494 if (vr0.type == VR_ANTI_RANGE
3495 && ranges_from_anti_range (&vr0, &vrtem0, &vrtem1))
3497 extract_range_from_unary_expr_1 (vr, code, type, &vrtem0, op0_type);
3498 if (vrtem1.type != VR_UNDEFINED)
3500 value_range_t vrres = VR_INITIALIZER;
3501 extract_range_from_unary_expr_1 (&vrres, code, type,
3502 &vrtem1, op0_type);
3503 vrp_meet (vr, &vrres);
3505 return;
3508 if (CONVERT_EXPR_CODE_P (code))
3510 tree inner_type = op0_type;
3511 tree outer_type = type;
3513 /* If the expression evaluates to a pointer, we are only interested in
3514 determining if it evaluates to NULL [0, 0] or non-NULL (~[0, 0]). */
3515 if (POINTER_TYPE_P (type))
3517 if (range_is_nonnull (&vr0))
3518 set_value_range_to_nonnull (vr, type);
3519 else if (range_is_null (&vr0))
3520 set_value_range_to_null (vr, type);
3521 else
3522 set_value_range_to_varying (vr);
3523 return;
3526 /* If VR0 is varying and we increase the type precision, assume
3527 a full range for the following transformation. */
3528 if (vr0.type == VR_VARYING
3529 && INTEGRAL_TYPE_P (inner_type)
3530 && TYPE_PRECISION (inner_type) < TYPE_PRECISION (outer_type))
3532 vr0.type = VR_RANGE;
3533 vr0.min = TYPE_MIN_VALUE (inner_type);
3534 vr0.max = TYPE_MAX_VALUE (inner_type);
3537 /* If VR0 is a constant range or anti-range and the conversion is
3538 not truncating we can convert the min and max values and
3539 canonicalize the resulting range. Otherwise we can do the
3540 conversion if the size of the range is less than what the
3541 precision of the target type can represent and the range is
3542 not an anti-range. */
3543 if ((vr0.type == VR_RANGE
3544 || vr0.type == VR_ANTI_RANGE)
3545 && TREE_CODE (vr0.min) == INTEGER_CST
3546 && TREE_CODE (vr0.max) == INTEGER_CST
3547 && (!is_overflow_infinity (vr0.min)
3548 || (vr0.type == VR_RANGE
3549 && TYPE_PRECISION (outer_type) > TYPE_PRECISION (inner_type)
3550 && needs_overflow_infinity (outer_type)
3551 && supports_overflow_infinity (outer_type)))
3552 && (!is_overflow_infinity (vr0.max)
3553 || (vr0.type == VR_RANGE
3554 && TYPE_PRECISION (outer_type) > TYPE_PRECISION (inner_type)
3555 && needs_overflow_infinity (outer_type)
3556 && supports_overflow_infinity (outer_type)))
3557 && (TYPE_PRECISION (outer_type) >= TYPE_PRECISION (inner_type)
3558 || (vr0.type == VR_RANGE
3559 && integer_zerop (int_const_binop (RSHIFT_EXPR,
3560 int_const_binop (MINUS_EXPR, vr0.max, vr0.min),
3561 size_int (TYPE_PRECISION (outer_type)))))))
3563 tree new_min, new_max;
3564 if (is_overflow_infinity (vr0.min))
3565 new_min = negative_overflow_infinity (outer_type);
3566 else
3567 new_min = force_fit_type (outer_type, wi::to_widest (vr0.min),
3568 0, false);
3569 if (is_overflow_infinity (vr0.max))
3570 new_max = positive_overflow_infinity (outer_type);
3571 else
3572 new_max = force_fit_type (outer_type, wi::to_widest (vr0.max),
3573 0, false);
3574 set_and_canonicalize_value_range (vr, vr0.type,
3575 new_min, new_max, NULL);
3576 return;
3579 set_value_range_to_varying (vr);
3580 return;
3582 else if (code == ABS_EXPR)
3584 tree min, max;
3585 int cmp;
3587 /* Pass through vr0 in the easy cases. */
3588 if (TYPE_UNSIGNED (type)
3589 || value_range_nonnegative_p (&vr0))
3591 copy_value_range (vr, &vr0);
3592 return;
3595 /* For the remaining varying or symbolic ranges we can't do anything
3596 useful. */
3597 if (vr0.type == VR_VARYING
3598 || symbolic_range_p (&vr0))
3600 set_value_range_to_varying (vr);
3601 return;
3604 /* -TYPE_MIN_VALUE = TYPE_MIN_VALUE with flag_wrapv so we can't get a
3605 useful range. */
3606 if (!TYPE_OVERFLOW_UNDEFINED (type)
3607 && ((vr0.type == VR_RANGE
3608 && vrp_val_is_min (vr0.min))
3609 || (vr0.type == VR_ANTI_RANGE
3610 && !vrp_val_is_min (vr0.min))))
3612 set_value_range_to_varying (vr);
3613 return;
3616 /* ABS_EXPR may flip the range around, if the original range
3617 included negative values. */
3618 if (is_overflow_infinity (vr0.min))
3619 min = positive_overflow_infinity (type);
3620 else if (!vrp_val_is_min (vr0.min))
3621 min = fold_unary_to_constant (code, type, vr0.min);
3622 else if (!needs_overflow_infinity (type))
3623 min = TYPE_MAX_VALUE (type);
3624 else if (supports_overflow_infinity (type))
3625 min = positive_overflow_infinity (type);
3626 else
3628 set_value_range_to_varying (vr);
3629 return;
3632 if (is_overflow_infinity (vr0.max))
3633 max = positive_overflow_infinity (type);
3634 else if (!vrp_val_is_min (vr0.max))
3635 max = fold_unary_to_constant (code, type, vr0.max);
3636 else if (!needs_overflow_infinity (type))
3637 max = TYPE_MAX_VALUE (type);
3638 else if (supports_overflow_infinity (type)
3639 /* We shouldn't generate [+INF, +INF] as set_value_range
3640 doesn't like this and ICEs. */
3641 && !is_positive_overflow_infinity (min))
3642 max = positive_overflow_infinity (type);
3643 else
3645 set_value_range_to_varying (vr);
3646 return;
3649 cmp = compare_values (min, max);
3651 /* If a VR_ANTI_RANGEs contains zero, then we have
3652 ~[-INF, min(MIN, MAX)]. */
3653 if (vr0.type == VR_ANTI_RANGE)
3655 if (range_includes_zero_p (vr0.min, vr0.max) == 1)
3657 /* Take the lower of the two values. */
3658 if (cmp != 1)
3659 max = min;
3661 /* Create ~[-INF, min (abs(MIN), abs(MAX))]
3662 or ~[-INF + 1, min (abs(MIN), abs(MAX))] when
3663 flag_wrapv is set and the original anti-range doesn't include
3664 TYPE_MIN_VALUE, remember -TYPE_MIN_VALUE = TYPE_MIN_VALUE. */
3665 if (TYPE_OVERFLOW_WRAPS (type))
3667 tree type_min_value = TYPE_MIN_VALUE (type);
3669 min = (vr0.min != type_min_value
3670 ? int_const_binop (PLUS_EXPR, type_min_value,
3671 build_int_cst (TREE_TYPE (type_min_value), 1))
3672 : type_min_value);
3674 else
3676 if (overflow_infinity_range_p (&vr0))
3677 min = negative_overflow_infinity (type);
3678 else
3679 min = TYPE_MIN_VALUE (type);
3682 else
3684 /* All else has failed, so create the range [0, INF], even for
3685 flag_wrapv since TYPE_MIN_VALUE is in the original
3686 anti-range. */
3687 vr0.type = VR_RANGE;
3688 min = build_int_cst (type, 0);
3689 if (needs_overflow_infinity (type))
3691 if (supports_overflow_infinity (type))
3692 max = positive_overflow_infinity (type);
3693 else
3695 set_value_range_to_varying (vr);
3696 return;
3699 else
3700 max = TYPE_MAX_VALUE (type);
3704 /* If the range contains zero then we know that the minimum value in the
3705 range will be zero. */
3706 else if (range_includes_zero_p (vr0.min, vr0.max) == 1)
3708 if (cmp == 1)
3709 max = min;
3710 min = build_int_cst (type, 0);
3712 else
3714 /* If the range was reversed, swap MIN and MAX. */
3715 if (cmp == 1)
3716 std::swap (min, max);
3719 cmp = compare_values (min, max);
3720 if (cmp == -2 || cmp == 1)
3722 /* If the new range has its limits swapped around (MIN > MAX),
3723 then the operation caused one of them to wrap around, mark
3724 the new range VARYING. */
3725 set_value_range_to_varying (vr);
3727 else
3728 set_value_range (vr, vr0.type, min, max, NULL);
3729 return;
3732 /* For unhandled operations fall back to varying. */
3733 set_value_range_to_varying (vr);
3734 return;
3738 /* Extract range information from a unary expression CODE OP0 based on
3739 the range of its operand with resulting type TYPE.
3740 The resulting range is stored in *VR. */
3742 static void
3743 extract_range_from_unary_expr (value_range_t *vr, enum tree_code code,
3744 tree type, tree op0)
3746 value_range_t vr0 = VR_INITIALIZER;
3748 /* Get value ranges for the operand. For constant operands, create
3749 a new value range with the operand to simplify processing. */
3750 if (TREE_CODE (op0) == SSA_NAME)
3751 vr0 = *(get_value_range (op0));
3752 else if (is_gimple_min_invariant (op0))
3753 set_value_range_to_value (&vr0, op0, NULL);
3754 else
3755 set_value_range_to_varying (&vr0);
3757 extract_range_from_unary_expr_1 (vr, code, type, &vr0, TREE_TYPE (op0));
3761 /* Extract range information from a conditional expression STMT based on
3762 the ranges of each of its operands and the expression code. */
3764 static void
3765 extract_range_from_cond_expr (value_range_t *vr, gassign *stmt)
3767 tree op0, op1;
3768 value_range_t vr0 = VR_INITIALIZER;
3769 value_range_t vr1 = VR_INITIALIZER;
3771 /* Get value ranges for each operand. For constant operands, create
3772 a new value range with the operand to simplify processing. */
3773 op0 = gimple_assign_rhs2 (stmt);
3774 if (TREE_CODE (op0) == SSA_NAME)
3775 vr0 = *(get_value_range (op0));
3776 else if (is_gimple_min_invariant (op0))
3777 set_value_range_to_value (&vr0, op0, NULL);
3778 else
3779 set_value_range_to_varying (&vr0);
3781 op1 = gimple_assign_rhs3 (stmt);
3782 if (TREE_CODE (op1) == SSA_NAME)
3783 vr1 = *(get_value_range (op1));
3784 else if (is_gimple_min_invariant (op1))
3785 set_value_range_to_value (&vr1, op1, NULL);
3786 else
3787 set_value_range_to_varying (&vr1);
3789 /* The resulting value range is the union of the operand ranges */
3790 copy_value_range (vr, &vr0);
3791 vrp_meet (vr, &vr1);
3795 /* Extract range information from a comparison expression EXPR based
3796 on the range of its operand and the expression code. */
3798 static void
3799 extract_range_from_comparison (value_range_t *vr, enum tree_code code,
3800 tree type, tree op0, tree op1)
3802 bool sop = false;
3803 tree val;
3805 val = vrp_evaluate_conditional_warnv_with_ops (code, op0, op1, false, &sop,
3806 NULL);
3808 /* A disadvantage of using a special infinity as an overflow
3809 representation is that we lose the ability to record overflow
3810 when we don't have an infinity. So we have to ignore a result
3811 which relies on overflow. */
3813 if (val && !is_overflow_infinity (val) && !sop)
3815 /* Since this expression was found on the RHS of an assignment,
3816 its type may be different from _Bool. Convert VAL to EXPR's
3817 type. */
3818 val = fold_convert (type, val);
3819 if (is_gimple_min_invariant (val))
3820 set_value_range_to_value (vr, val, vr->equiv);
3821 else
3822 set_value_range (vr, VR_RANGE, val, val, vr->equiv);
3824 else
3825 /* The result of a comparison is always true or false. */
3826 set_value_range_to_truthvalue (vr, type);
3829 /* Helper function for simplify_internal_call_using_ranges and
3830 extract_range_basic. Return true if OP0 SUBCODE OP1 for
3831 SUBCODE {PLUS,MINUS,MULT}_EXPR is known to never overflow or
3832 always overflow. Set *OVF to true if it is known to always
3833 overflow. */
3835 static bool
3836 check_for_binary_op_overflow (enum tree_code subcode, tree type,
3837 tree op0, tree op1, bool *ovf)
3839 value_range_t vr0 = VR_INITIALIZER;
3840 value_range_t vr1 = VR_INITIALIZER;
3841 if (TREE_CODE (op0) == SSA_NAME)
3842 vr0 = *get_value_range (op0);
3843 else if (TREE_CODE (op0) == INTEGER_CST)
3844 set_value_range_to_value (&vr0, op0, NULL);
3845 else
3846 set_value_range_to_varying (&vr0);
3848 if (TREE_CODE (op1) == SSA_NAME)
3849 vr1 = *get_value_range (op1);
3850 else if (TREE_CODE (op1) == INTEGER_CST)
3851 set_value_range_to_value (&vr1, op1, NULL);
3852 else
3853 set_value_range_to_varying (&vr1);
3855 if (!range_int_cst_p (&vr0)
3856 || TREE_OVERFLOW (vr0.min)
3857 || TREE_OVERFLOW (vr0.max))
3859 vr0.min = vrp_val_min (TREE_TYPE (op0));
3860 vr0.max = vrp_val_max (TREE_TYPE (op0));
3862 if (!range_int_cst_p (&vr1)
3863 || TREE_OVERFLOW (vr1.min)
3864 || TREE_OVERFLOW (vr1.max))
3866 vr1.min = vrp_val_min (TREE_TYPE (op1));
3867 vr1.max = vrp_val_max (TREE_TYPE (op1));
3869 *ovf = arith_overflowed_p (subcode, type, vr0.min,
3870 subcode == MINUS_EXPR ? vr1.max : vr1.min);
3871 if (arith_overflowed_p (subcode, type, vr0.max,
3872 subcode == MINUS_EXPR ? vr1.min : vr1.max) != *ovf)
3873 return false;
3874 if (subcode == MULT_EXPR)
3876 if (arith_overflowed_p (subcode, type, vr0.min, vr1.max) != *ovf
3877 || arith_overflowed_p (subcode, type, vr0.max, vr1.min) != *ovf)
3878 return false;
3880 if (*ovf)
3882 /* So far we found that there is an overflow on the boundaries.
3883 That doesn't prove that there is an overflow even for all values
3884 in between the boundaries. For that compute widest_int range
3885 of the result and see if it doesn't overlap the range of
3886 type. */
3887 widest_int wmin, wmax;
3888 widest_int w[4];
3889 int i;
3890 w[0] = wi::to_widest (vr0.min);
3891 w[1] = wi::to_widest (vr0.max);
3892 w[2] = wi::to_widest (vr1.min);
3893 w[3] = wi::to_widest (vr1.max);
3894 for (i = 0; i < 4; i++)
3896 widest_int wt;
3897 switch (subcode)
3899 case PLUS_EXPR:
3900 wt = wi::add (w[i & 1], w[2 + (i & 2) / 2]);
3901 break;
3902 case MINUS_EXPR:
3903 wt = wi::sub (w[i & 1], w[2 + (i & 2) / 2]);
3904 break;
3905 case MULT_EXPR:
3906 wt = wi::mul (w[i & 1], w[2 + (i & 2) / 2]);
3907 break;
3908 default:
3909 gcc_unreachable ();
3911 if (i == 0)
3913 wmin = wt;
3914 wmax = wt;
3916 else
3918 wmin = wi::smin (wmin, wt);
3919 wmax = wi::smax (wmax, wt);
3922 /* The result of op0 CODE op1 is known to be in range
3923 [wmin, wmax]. */
3924 widest_int wtmin = wi::to_widest (vrp_val_min (type));
3925 widest_int wtmax = wi::to_widest (vrp_val_max (type));
3926 /* If all values in [wmin, wmax] are smaller than
3927 [wtmin, wtmax] or all are larger than [wtmin, wtmax],
3928 the arithmetic operation will always overflow. */
3929 if (wi::lts_p (wmax, wtmin) || wi::gts_p (wmin, wtmax))
3930 return true;
3931 return false;
3933 return true;
3936 /* Try to derive a nonnegative or nonzero range out of STMT relying
3937 primarily on generic routines in fold in conjunction with range data.
3938 Store the result in *VR */
3940 static void
3941 extract_range_basic (value_range_t *vr, gimple stmt)
3943 bool sop = false;
3944 tree type = gimple_expr_type (stmt);
3946 if (gimple_call_builtin_p (stmt, BUILT_IN_NORMAL))
3948 tree fndecl = gimple_call_fndecl (stmt), arg;
3949 int mini, maxi, zerov = 0, prec;
3951 switch (DECL_FUNCTION_CODE (fndecl))
3953 case BUILT_IN_CONSTANT_P:
3954 /* If the call is __builtin_constant_p and the argument is a
3955 function parameter resolve it to false. This avoids bogus
3956 array bound warnings.
3957 ??? We could do this as early as inlining is finished. */
3958 arg = gimple_call_arg (stmt, 0);
3959 if (TREE_CODE (arg) == SSA_NAME
3960 && SSA_NAME_IS_DEFAULT_DEF (arg)
3961 && TREE_CODE (SSA_NAME_VAR (arg)) == PARM_DECL)
3963 set_value_range_to_null (vr, type);
3964 return;
3966 break;
3967 /* Both __builtin_ffs* and __builtin_popcount return
3968 [0, prec]. */
3969 CASE_INT_FN (BUILT_IN_FFS):
3970 CASE_INT_FN (BUILT_IN_POPCOUNT):
3971 arg = gimple_call_arg (stmt, 0);
3972 prec = TYPE_PRECISION (TREE_TYPE (arg));
3973 mini = 0;
3974 maxi = prec;
3975 if (TREE_CODE (arg) == SSA_NAME)
3977 value_range_t *vr0 = get_value_range (arg);
3978 /* If arg is non-zero, then ffs or popcount
3979 are non-zero. */
3980 if (((vr0->type == VR_RANGE
3981 && range_includes_zero_p (vr0->min, vr0->max) == 0)
3982 || (vr0->type == VR_ANTI_RANGE
3983 && range_includes_zero_p (vr0->min, vr0->max) == 1))
3984 && !is_overflow_infinity (vr0->min)
3985 && !is_overflow_infinity (vr0->max))
3986 mini = 1;
3987 /* If some high bits are known to be zero,
3988 we can decrease the maximum. */
3989 if (vr0->type == VR_RANGE
3990 && TREE_CODE (vr0->max) == INTEGER_CST
3991 && !operand_less_p (vr0->min,
3992 build_zero_cst (TREE_TYPE (vr0->min)))
3993 && !is_overflow_infinity (vr0->max))
3994 maxi = tree_floor_log2 (vr0->max) + 1;
3996 goto bitop_builtin;
3997 /* __builtin_parity* returns [0, 1]. */
3998 CASE_INT_FN (BUILT_IN_PARITY):
3999 mini = 0;
4000 maxi = 1;
4001 goto bitop_builtin;
4002 /* __builtin_c[lt]z* return [0, prec-1], except for
4003 when the argument is 0, but that is undefined behavior.
4004 On many targets where the CLZ RTL or optab value is defined
4005 for 0 the value is prec, so include that in the range
4006 by default. */
4007 CASE_INT_FN (BUILT_IN_CLZ):
4008 arg = gimple_call_arg (stmt, 0);
4009 prec = TYPE_PRECISION (TREE_TYPE (arg));
4010 mini = 0;
4011 maxi = prec;
4012 if (optab_handler (clz_optab, TYPE_MODE (TREE_TYPE (arg)))
4013 != CODE_FOR_nothing
4014 && CLZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (TREE_TYPE (arg)),
4015 zerov)
4016 /* Handle only the single common value. */
4017 && zerov != prec)
4018 /* Magic value to give up, unless vr0 proves
4019 arg is non-zero. */
4020 mini = -2;
4021 if (TREE_CODE (arg) == SSA_NAME)
4023 value_range_t *vr0 = get_value_range (arg);
4024 /* From clz of VR_RANGE minimum we can compute
4025 result maximum. */
4026 if (vr0->type == VR_RANGE
4027 && TREE_CODE (vr0->min) == INTEGER_CST
4028 && !is_overflow_infinity (vr0->min))
4030 maxi = prec - 1 - tree_floor_log2 (vr0->min);
4031 if (maxi != prec)
4032 mini = 0;
4034 else if (vr0->type == VR_ANTI_RANGE
4035 && integer_zerop (vr0->min)
4036 && !is_overflow_infinity (vr0->min))
4038 maxi = prec - 1;
4039 mini = 0;
4041 if (mini == -2)
4042 break;
4043 /* From clz of VR_RANGE maximum we can compute
4044 result minimum. */
4045 if (vr0->type == VR_RANGE
4046 && TREE_CODE (vr0->max) == INTEGER_CST
4047 && !is_overflow_infinity (vr0->max))
4049 mini = prec - 1 - tree_floor_log2 (vr0->max);
4050 if (mini == prec)
4051 break;
4054 if (mini == -2)
4055 break;
4056 goto bitop_builtin;
4057 /* __builtin_ctz* return [0, prec-1], except for
4058 when the argument is 0, but that is undefined behavior.
4059 If there is a ctz optab for this mode and
4060 CTZ_DEFINED_VALUE_AT_ZERO, include that in the range,
4061 otherwise just assume 0 won't be seen. */
4062 CASE_INT_FN (BUILT_IN_CTZ):
4063 arg = gimple_call_arg (stmt, 0);
4064 prec = TYPE_PRECISION (TREE_TYPE (arg));
4065 mini = 0;
4066 maxi = prec - 1;
4067 if (optab_handler (ctz_optab, TYPE_MODE (TREE_TYPE (arg)))
4068 != CODE_FOR_nothing
4069 && CTZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (TREE_TYPE (arg)),
4070 zerov))
4072 /* Handle only the two common values. */
4073 if (zerov == -1)
4074 mini = -1;
4075 else if (zerov == prec)
4076 maxi = prec;
4077 else
4078 /* Magic value to give up, unless vr0 proves
4079 arg is non-zero. */
4080 mini = -2;
4082 if (TREE_CODE (arg) == SSA_NAME)
4084 value_range_t *vr0 = get_value_range (arg);
4085 /* If arg is non-zero, then use [0, prec - 1]. */
4086 if (((vr0->type == VR_RANGE
4087 && integer_nonzerop (vr0->min))
4088 || (vr0->type == VR_ANTI_RANGE
4089 && integer_zerop (vr0->min)))
4090 && !is_overflow_infinity (vr0->min))
4092 mini = 0;
4093 maxi = prec - 1;
4095 /* If some high bits are known to be zero,
4096 we can decrease the result maximum. */
4097 if (vr0->type == VR_RANGE
4098 && TREE_CODE (vr0->max) == INTEGER_CST
4099 && !is_overflow_infinity (vr0->max))
4101 maxi = tree_floor_log2 (vr0->max);
4102 /* For vr0 [0, 0] give up. */
4103 if (maxi == -1)
4104 break;
4107 if (mini == -2)
4108 break;
4109 goto bitop_builtin;
4110 /* __builtin_clrsb* returns [0, prec-1]. */
4111 CASE_INT_FN (BUILT_IN_CLRSB):
4112 arg = gimple_call_arg (stmt, 0);
4113 prec = TYPE_PRECISION (TREE_TYPE (arg));
4114 mini = 0;
4115 maxi = prec - 1;
4116 goto bitop_builtin;
4117 bitop_builtin:
4118 set_value_range (vr, VR_RANGE, build_int_cst (type, mini),
4119 build_int_cst (type, maxi), NULL);
4120 return;
4121 default:
4122 break;
4125 else if (is_gimple_call (stmt) && gimple_call_internal_p (stmt))
4127 enum tree_code subcode = ERROR_MARK;
4128 switch (gimple_call_internal_fn (stmt))
4130 case IFN_UBSAN_CHECK_ADD:
4131 subcode = PLUS_EXPR;
4132 break;
4133 case IFN_UBSAN_CHECK_SUB:
4134 subcode = MINUS_EXPR;
4135 break;
4136 case IFN_UBSAN_CHECK_MUL:
4137 subcode = MULT_EXPR;
4138 break;
4139 default:
4140 break;
4142 if (subcode != ERROR_MARK)
4144 bool saved_flag_wrapv = flag_wrapv;
4145 /* Pretend the arithmetics is wrapping. If there is
4146 any overflow, we'll complain, but will actually do
4147 wrapping operation. */
4148 flag_wrapv = 1;
4149 extract_range_from_binary_expr (vr, subcode, type,
4150 gimple_call_arg (stmt, 0),
4151 gimple_call_arg (stmt, 1));
4152 flag_wrapv = saved_flag_wrapv;
4154 /* If for both arguments vrp_valueize returned non-NULL,
4155 this should have been already folded and if not, it
4156 wasn't folded because of overflow. Avoid removing the
4157 UBSAN_CHECK_* calls in that case. */
4158 if (vr->type == VR_RANGE
4159 && (vr->min == vr->max
4160 || operand_equal_p (vr->min, vr->max, 0)))
4161 set_value_range_to_varying (vr);
4162 return;
4165 /* Handle extraction of the two results (result of arithmetics and
4166 a flag whether arithmetics overflowed) from {ADD,SUB,MUL}_OVERFLOW
4167 internal function. */
4168 else if (is_gimple_assign (stmt)
4169 && (gimple_assign_rhs_code (stmt) == REALPART_EXPR
4170 || gimple_assign_rhs_code (stmt) == IMAGPART_EXPR)
4171 && INTEGRAL_TYPE_P (type))
4173 enum tree_code code = gimple_assign_rhs_code (stmt);
4174 tree op = gimple_assign_rhs1 (stmt);
4175 if (TREE_CODE (op) == code && TREE_CODE (TREE_OPERAND (op, 0)) == SSA_NAME)
4177 gimple g = SSA_NAME_DEF_STMT (TREE_OPERAND (op, 0));
4178 if (is_gimple_call (g) && gimple_call_internal_p (g))
4180 enum tree_code subcode = ERROR_MARK;
4181 switch (gimple_call_internal_fn (g))
4183 case IFN_ADD_OVERFLOW:
4184 subcode = PLUS_EXPR;
4185 break;
4186 case IFN_SUB_OVERFLOW:
4187 subcode = MINUS_EXPR;
4188 break;
4189 case IFN_MUL_OVERFLOW:
4190 subcode = MULT_EXPR;
4191 break;
4192 default:
4193 break;
4195 if (subcode != ERROR_MARK)
4197 tree op0 = gimple_call_arg (g, 0);
4198 tree op1 = gimple_call_arg (g, 1);
4199 if (code == IMAGPART_EXPR)
4201 bool ovf = false;
4202 if (check_for_binary_op_overflow (subcode, type,
4203 op0, op1, &ovf))
4204 set_value_range_to_value (vr,
4205 build_int_cst (type, ovf),
4206 NULL);
4207 else
4208 set_value_range (vr, VR_RANGE, build_int_cst (type, 0),
4209 build_int_cst (type, 1), NULL);
4211 else if (types_compatible_p (type, TREE_TYPE (op0))
4212 && types_compatible_p (type, TREE_TYPE (op1)))
4214 bool saved_flag_wrapv = flag_wrapv;
4215 /* Pretend the arithmetics is wrapping. If there is
4216 any overflow, IMAGPART_EXPR will be set. */
4217 flag_wrapv = 1;
4218 extract_range_from_binary_expr (vr, subcode, type,
4219 op0, op1);
4220 flag_wrapv = saved_flag_wrapv;
4222 else
4224 value_range_t vr0 = VR_INITIALIZER;
4225 value_range_t vr1 = VR_INITIALIZER;
4226 bool saved_flag_wrapv = flag_wrapv;
4227 /* Pretend the arithmetics is wrapping. If there is
4228 any overflow, IMAGPART_EXPR will be set. */
4229 flag_wrapv = 1;
4230 extract_range_from_unary_expr (&vr0, NOP_EXPR,
4231 type, op0);
4232 extract_range_from_unary_expr (&vr1, NOP_EXPR,
4233 type, op1);
4234 extract_range_from_binary_expr_1 (vr, subcode, type,
4235 &vr0, &vr1);
4236 flag_wrapv = saved_flag_wrapv;
4238 return;
4243 if (INTEGRAL_TYPE_P (type)
4244 && gimple_stmt_nonnegative_warnv_p (stmt, &sop))
4245 set_value_range_to_nonnegative (vr, type,
4246 sop || stmt_overflow_infinity (stmt));
4247 else if (vrp_stmt_computes_nonzero (stmt, &sop)
4248 && !sop)
4249 set_value_range_to_nonnull (vr, type);
4250 else
4251 set_value_range_to_varying (vr);
4255 /* Try to compute a useful range out of assignment STMT and store it
4256 in *VR. */
4258 static void
4259 extract_range_from_assignment (value_range_t *vr, gassign *stmt)
4261 enum tree_code code = gimple_assign_rhs_code (stmt);
4263 if (code == ASSERT_EXPR)
4264 extract_range_from_assert (vr, gimple_assign_rhs1 (stmt));
4265 else if (code == SSA_NAME)
4266 extract_range_from_ssa_name (vr, gimple_assign_rhs1 (stmt));
4267 else if (TREE_CODE_CLASS (code) == tcc_binary)
4268 extract_range_from_binary_expr (vr, gimple_assign_rhs_code (stmt),
4269 gimple_expr_type (stmt),
4270 gimple_assign_rhs1 (stmt),
4271 gimple_assign_rhs2 (stmt));
4272 else if (TREE_CODE_CLASS (code) == tcc_unary)
4273 extract_range_from_unary_expr (vr, gimple_assign_rhs_code (stmt),
4274 gimple_expr_type (stmt),
4275 gimple_assign_rhs1 (stmt));
4276 else if (code == COND_EXPR)
4277 extract_range_from_cond_expr (vr, stmt);
4278 else if (TREE_CODE_CLASS (code) == tcc_comparison)
4279 extract_range_from_comparison (vr, gimple_assign_rhs_code (stmt),
4280 gimple_expr_type (stmt),
4281 gimple_assign_rhs1 (stmt),
4282 gimple_assign_rhs2 (stmt));
4283 else if (get_gimple_rhs_class (code) == GIMPLE_SINGLE_RHS
4284 && is_gimple_min_invariant (gimple_assign_rhs1 (stmt)))
4285 set_value_range_to_value (vr, gimple_assign_rhs1 (stmt), NULL);
4286 else
4287 set_value_range_to_varying (vr);
4289 if (vr->type == VR_VARYING)
4290 extract_range_basic (vr, stmt);
4293 /* Given a range VR, a LOOP and a variable VAR, determine whether it
4294 would be profitable to adjust VR using scalar evolution information
4295 for VAR. If so, update VR with the new limits. */
4297 static void
4298 adjust_range_with_scev (value_range_t *vr, struct loop *loop,
4299 gimple stmt, tree var)
4301 tree init, step, chrec, tmin, tmax, min, max, type, tem;
4302 enum ev_direction dir;
4304 /* TODO. Don't adjust anti-ranges. An anti-range may provide
4305 better opportunities than a regular range, but I'm not sure. */
4306 if (vr->type == VR_ANTI_RANGE)
4307 return;
4309 chrec = instantiate_parameters (loop, analyze_scalar_evolution (loop, var));
4311 /* Like in PR19590, scev can return a constant function. */
4312 if (is_gimple_min_invariant (chrec))
4314 set_value_range_to_value (vr, chrec, vr->equiv);
4315 return;
4318 if (TREE_CODE (chrec) != POLYNOMIAL_CHREC)
4319 return;
4321 init = initial_condition_in_loop_num (chrec, loop->num);
4322 tem = op_with_constant_singleton_value_range (init);
4323 if (tem)
4324 init = tem;
4325 step = evolution_part_in_loop_num (chrec, loop->num);
4326 tem = op_with_constant_singleton_value_range (step);
4327 if (tem)
4328 step = tem;
4330 /* If STEP is symbolic, we can't know whether INIT will be the
4331 minimum or maximum value in the range. Also, unless INIT is
4332 a simple expression, compare_values and possibly other functions
4333 in tree-vrp won't be able to handle it. */
4334 if (step == NULL_TREE
4335 || !is_gimple_min_invariant (step)
4336 || !valid_value_p (init))
4337 return;
4339 dir = scev_direction (chrec);
4340 if (/* Do not adjust ranges if we do not know whether the iv increases
4341 or decreases, ... */
4342 dir == EV_DIR_UNKNOWN
4343 /* ... or if it may wrap. */
4344 || scev_probably_wraps_p (init, step, stmt, get_chrec_loop (chrec),
4345 true))
4346 return;
4348 /* We use TYPE_MIN_VALUE and TYPE_MAX_VALUE here instead of
4349 negative_overflow_infinity and positive_overflow_infinity,
4350 because we have concluded that the loop probably does not
4351 wrap. */
4353 type = TREE_TYPE (var);
4354 if (POINTER_TYPE_P (type) || !TYPE_MIN_VALUE (type))
4355 tmin = lower_bound_in_type (type, type);
4356 else
4357 tmin = TYPE_MIN_VALUE (type);
4358 if (POINTER_TYPE_P (type) || !TYPE_MAX_VALUE (type))
4359 tmax = upper_bound_in_type (type, type);
4360 else
4361 tmax = TYPE_MAX_VALUE (type);
4363 /* Try to use estimated number of iterations for the loop to constrain the
4364 final value in the evolution. */
4365 if (TREE_CODE (step) == INTEGER_CST
4366 && is_gimple_val (init)
4367 && (TREE_CODE (init) != SSA_NAME
4368 || get_value_range (init)->type == VR_RANGE))
4370 widest_int nit;
4372 /* We are only entering here for loop header PHI nodes, so using
4373 the number of latch executions is the correct thing to use. */
4374 if (max_loop_iterations (loop, &nit))
4376 value_range_t maxvr = VR_INITIALIZER;
4377 signop sgn = TYPE_SIGN (TREE_TYPE (step));
4378 bool overflow;
4380 widest_int wtmp = wi::mul (wi::to_widest (step), nit, sgn,
4381 &overflow);
4382 /* If the multiplication overflowed we can't do a meaningful
4383 adjustment. Likewise if the result doesn't fit in the type
4384 of the induction variable. For a signed type we have to
4385 check whether the result has the expected signedness which
4386 is that of the step as number of iterations is unsigned. */
4387 if (!overflow
4388 && wi::fits_to_tree_p (wtmp, TREE_TYPE (init))
4389 && (sgn == UNSIGNED
4390 || wi::gts_p (wtmp, 0) == wi::gts_p (step, 0)))
4392 tem = wide_int_to_tree (TREE_TYPE (init), wtmp);
4393 extract_range_from_binary_expr (&maxvr, PLUS_EXPR,
4394 TREE_TYPE (init), init, tem);
4395 /* Likewise if the addition did. */
4396 if (maxvr.type == VR_RANGE)
4398 tmin = maxvr.min;
4399 tmax = maxvr.max;
4405 if (vr->type == VR_VARYING || vr->type == VR_UNDEFINED)
4407 min = tmin;
4408 max = tmax;
4410 /* For VARYING or UNDEFINED ranges, just about anything we get
4411 from scalar evolutions should be better. */
4413 if (dir == EV_DIR_DECREASES)
4414 max = init;
4415 else
4416 min = init;
4418 else if (vr->type == VR_RANGE)
4420 min = vr->min;
4421 max = vr->max;
4423 if (dir == EV_DIR_DECREASES)
4425 /* INIT is the maximum value. If INIT is lower than VR->MAX
4426 but no smaller than VR->MIN, set VR->MAX to INIT. */
4427 if (compare_values (init, max) == -1)
4428 max = init;
4430 /* According to the loop information, the variable does not
4431 overflow. If we think it does, probably because of an
4432 overflow due to arithmetic on a different INF value,
4433 reset now. */
4434 if (is_negative_overflow_infinity (min)
4435 || compare_values (min, tmin) == -1)
4436 min = tmin;
4439 else
4441 /* If INIT is bigger than VR->MIN, set VR->MIN to INIT. */
4442 if (compare_values (init, min) == 1)
4443 min = init;
4445 if (is_positive_overflow_infinity (max)
4446 || compare_values (tmax, max) == -1)
4447 max = tmax;
4450 else
4451 return;
4453 /* If we just created an invalid range with the minimum
4454 greater than the maximum, we fail conservatively.
4455 This should happen only in unreachable
4456 parts of code, or for invalid programs. */
4457 if (compare_values (min, max) == 1
4458 || (is_negative_overflow_infinity (min)
4459 && is_positive_overflow_infinity (max)))
4460 return;
4462 set_value_range (vr, VR_RANGE, min, max, vr->equiv);
4466 /* Given two numeric value ranges VR0, VR1 and a comparison code COMP:
4468 - Return BOOLEAN_TRUE_NODE if VR0 COMP VR1 always returns true for
4469 all the values in the ranges.
4471 - Return BOOLEAN_FALSE_NODE if the comparison always returns false.
4473 - Return NULL_TREE if it is not always possible to determine the
4474 value of the comparison.
4476 Also set *STRICT_OVERFLOW_P to indicate whether a range with an
4477 overflow infinity was used in the test. */
4480 static tree
4481 compare_ranges (enum tree_code comp, value_range_t *vr0, value_range_t *vr1,
4482 bool *strict_overflow_p)
4484 /* VARYING or UNDEFINED ranges cannot be compared. */
4485 if (vr0->type == VR_VARYING
4486 || vr0->type == VR_UNDEFINED
4487 || vr1->type == VR_VARYING
4488 || vr1->type == VR_UNDEFINED)
4489 return NULL_TREE;
4491 /* Anti-ranges need to be handled separately. */
4492 if (vr0->type == VR_ANTI_RANGE || vr1->type == VR_ANTI_RANGE)
4494 /* If both are anti-ranges, then we cannot compute any
4495 comparison. */
4496 if (vr0->type == VR_ANTI_RANGE && vr1->type == VR_ANTI_RANGE)
4497 return NULL_TREE;
4499 /* These comparisons are never statically computable. */
4500 if (comp == GT_EXPR
4501 || comp == GE_EXPR
4502 || comp == LT_EXPR
4503 || comp == LE_EXPR)
4504 return NULL_TREE;
4506 /* Equality can be computed only between a range and an
4507 anti-range. ~[VAL1, VAL2] == [VAL1, VAL2] is always false. */
4508 if (vr0->type == VR_RANGE)
4510 /* To simplify processing, make VR0 the anti-range. */
4511 value_range_t *tmp = vr0;
4512 vr0 = vr1;
4513 vr1 = tmp;
4516 gcc_assert (comp == NE_EXPR || comp == EQ_EXPR);
4518 if (compare_values_warnv (vr0->min, vr1->min, strict_overflow_p) == 0
4519 && compare_values_warnv (vr0->max, vr1->max, strict_overflow_p) == 0)
4520 return (comp == NE_EXPR) ? boolean_true_node : boolean_false_node;
4522 return NULL_TREE;
4525 if (!usable_range_p (vr0, strict_overflow_p)
4526 || !usable_range_p (vr1, strict_overflow_p))
4527 return NULL_TREE;
4529 /* Simplify processing. If COMP is GT_EXPR or GE_EXPR, switch the
4530 operands around and change the comparison code. */
4531 if (comp == GT_EXPR || comp == GE_EXPR)
4533 comp = (comp == GT_EXPR) ? LT_EXPR : LE_EXPR;
4534 std::swap (vr0, vr1);
4537 if (comp == EQ_EXPR)
4539 /* Equality may only be computed if both ranges represent
4540 exactly one value. */
4541 if (compare_values_warnv (vr0->min, vr0->max, strict_overflow_p) == 0
4542 && compare_values_warnv (vr1->min, vr1->max, strict_overflow_p) == 0)
4544 int cmp_min = compare_values_warnv (vr0->min, vr1->min,
4545 strict_overflow_p);
4546 int cmp_max = compare_values_warnv (vr0->max, vr1->max,
4547 strict_overflow_p);
4548 if (cmp_min == 0 && cmp_max == 0)
4549 return boolean_true_node;
4550 else if (cmp_min != -2 && cmp_max != -2)
4551 return boolean_false_node;
4553 /* If [V0_MIN, V1_MAX] < [V1_MIN, V1_MAX] then V0 != V1. */
4554 else if (compare_values_warnv (vr0->min, vr1->max,
4555 strict_overflow_p) == 1
4556 || compare_values_warnv (vr1->min, vr0->max,
4557 strict_overflow_p) == 1)
4558 return boolean_false_node;
4560 return NULL_TREE;
4562 else if (comp == NE_EXPR)
4564 int cmp1, cmp2;
4566 /* If VR0 is completely to the left or completely to the right
4567 of VR1, they are always different. Notice that we need to
4568 make sure that both comparisons yield similar results to
4569 avoid comparing values that cannot be compared at
4570 compile-time. */
4571 cmp1 = compare_values_warnv (vr0->max, vr1->min, strict_overflow_p);
4572 cmp2 = compare_values_warnv (vr0->min, vr1->max, strict_overflow_p);
4573 if ((cmp1 == -1 && cmp2 == -1) || (cmp1 == 1 && cmp2 == 1))
4574 return boolean_true_node;
4576 /* If VR0 and VR1 represent a single value and are identical,
4577 return false. */
4578 else if (compare_values_warnv (vr0->min, vr0->max,
4579 strict_overflow_p) == 0
4580 && compare_values_warnv (vr1->min, vr1->max,
4581 strict_overflow_p) == 0
4582 && compare_values_warnv (vr0->min, vr1->min,
4583 strict_overflow_p) == 0
4584 && compare_values_warnv (vr0->max, vr1->max,
4585 strict_overflow_p) == 0)
4586 return boolean_false_node;
4588 /* Otherwise, they may or may not be different. */
4589 else
4590 return NULL_TREE;
4592 else if (comp == LT_EXPR || comp == LE_EXPR)
4594 int tst;
4596 /* If VR0 is to the left of VR1, return true. */
4597 tst = compare_values_warnv (vr0->max, vr1->min, strict_overflow_p);
4598 if ((comp == LT_EXPR && tst == -1)
4599 || (comp == LE_EXPR && (tst == -1 || tst == 0)))
4601 if (overflow_infinity_range_p (vr0)
4602 || overflow_infinity_range_p (vr1))
4603 *strict_overflow_p = true;
4604 return boolean_true_node;
4607 /* If VR0 is to the right of VR1, return false. */
4608 tst = compare_values_warnv (vr0->min, vr1->max, strict_overflow_p);
4609 if ((comp == LT_EXPR && (tst == 0 || tst == 1))
4610 || (comp == LE_EXPR && tst == 1))
4612 if (overflow_infinity_range_p (vr0)
4613 || overflow_infinity_range_p (vr1))
4614 *strict_overflow_p = true;
4615 return boolean_false_node;
4618 /* Otherwise, we don't know. */
4619 return NULL_TREE;
4622 gcc_unreachable ();
4626 /* Given a value range VR, a value VAL and a comparison code COMP, return
4627 BOOLEAN_TRUE_NODE if VR COMP VAL always returns true for all the
4628 values in VR. Return BOOLEAN_FALSE_NODE if the comparison
4629 always returns false. Return NULL_TREE if it is not always
4630 possible to determine the value of the comparison. Also set
4631 *STRICT_OVERFLOW_P to indicate whether a range with an overflow
4632 infinity was used in the test. */
4634 static tree
4635 compare_range_with_value (enum tree_code comp, value_range_t *vr, tree val,
4636 bool *strict_overflow_p)
4638 if (vr->type == VR_VARYING || vr->type == VR_UNDEFINED)
4639 return NULL_TREE;
4641 /* Anti-ranges need to be handled separately. */
4642 if (vr->type == VR_ANTI_RANGE)
4644 /* For anti-ranges, the only predicates that we can compute at
4645 compile time are equality and inequality. */
4646 if (comp == GT_EXPR
4647 || comp == GE_EXPR
4648 || comp == LT_EXPR
4649 || comp == LE_EXPR)
4650 return NULL_TREE;
4652 /* ~[VAL_1, VAL_2] OP VAL is known if VAL_1 <= VAL <= VAL_2. */
4653 if (value_inside_range (val, vr->min, vr->max) == 1)
4654 return (comp == NE_EXPR) ? boolean_true_node : boolean_false_node;
4656 return NULL_TREE;
4659 if (!usable_range_p (vr, strict_overflow_p))
4660 return NULL_TREE;
4662 if (comp == EQ_EXPR)
4664 /* EQ_EXPR may only be computed if VR represents exactly
4665 one value. */
4666 if (compare_values_warnv (vr->min, vr->max, strict_overflow_p) == 0)
4668 int cmp = compare_values_warnv (vr->min, val, strict_overflow_p);
4669 if (cmp == 0)
4670 return boolean_true_node;
4671 else if (cmp == -1 || cmp == 1 || cmp == 2)
4672 return boolean_false_node;
4674 else if (compare_values_warnv (val, vr->min, strict_overflow_p) == -1
4675 || compare_values_warnv (vr->max, val, strict_overflow_p) == -1)
4676 return boolean_false_node;
4678 return NULL_TREE;
4680 else if (comp == NE_EXPR)
4682 /* If VAL is not inside VR, then they are always different. */
4683 if (compare_values_warnv (vr->max, val, strict_overflow_p) == -1
4684 || compare_values_warnv (vr->min, val, strict_overflow_p) == 1)
4685 return boolean_true_node;
4687 /* If VR represents exactly one value equal to VAL, then return
4688 false. */
4689 if (compare_values_warnv (vr->min, vr->max, strict_overflow_p) == 0
4690 && compare_values_warnv (vr->min, val, strict_overflow_p) == 0)
4691 return boolean_false_node;
4693 /* Otherwise, they may or may not be different. */
4694 return NULL_TREE;
4696 else if (comp == LT_EXPR || comp == LE_EXPR)
4698 int tst;
4700 /* If VR is to the left of VAL, return true. */
4701 tst = compare_values_warnv (vr->max, val, strict_overflow_p);
4702 if ((comp == LT_EXPR && tst == -1)
4703 || (comp == LE_EXPR && (tst == -1 || tst == 0)))
4705 if (overflow_infinity_range_p (vr))
4706 *strict_overflow_p = true;
4707 return boolean_true_node;
4710 /* If VR is to the right of VAL, return false. */
4711 tst = compare_values_warnv (vr->min, val, strict_overflow_p);
4712 if ((comp == LT_EXPR && (tst == 0 || tst == 1))
4713 || (comp == LE_EXPR && tst == 1))
4715 if (overflow_infinity_range_p (vr))
4716 *strict_overflow_p = true;
4717 return boolean_false_node;
4720 /* Otherwise, we don't know. */
4721 return NULL_TREE;
4723 else if (comp == GT_EXPR || comp == GE_EXPR)
4725 int tst;
4727 /* If VR is to the right of VAL, return true. */
4728 tst = compare_values_warnv (vr->min, val, strict_overflow_p);
4729 if ((comp == GT_EXPR && tst == 1)
4730 || (comp == GE_EXPR && (tst == 0 || tst == 1)))
4732 if (overflow_infinity_range_p (vr))
4733 *strict_overflow_p = true;
4734 return boolean_true_node;
4737 /* If VR is to the left of VAL, return false. */
4738 tst = compare_values_warnv (vr->max, val, strict_overflow_p);
4739 if ((comp == GT_EXPR && (tst == -1 || tst == 0))
4740 || (comp == GE_EXPR && tst == -1))
4742 if (overflow_infinity_range_p (vr))
4743 *strict_overflow_p = true;
4744 return boolean_false_node;
4747 /* Otherwise, we don't know. */
4748 return NULL_TREE;
4751 gcc_unreachable ();
4755 /* Debugging dumps. */
4757 void dump_value_range (FILE *, value_range_t *);
4758 void debug_value_range (value_range_t *);
4759 void dump_all_value_ranges (FILE *);
4760 void debug_all_value_ranges (void);
4761 void dump_vr_equiv (FILE *, bitmap);
4762 void debug_vr_equiv (bitmap);
4765 /* Dump value range VR to FILE. */
4767 void
4768 dump_value_range (FILE *file, value_range_t *vr)
4770 if (vr == NULL)
4771 fprintf (file, "[]");
4772 else if (vr->type == VR_UNDEFINED)
4773 fprintf (file, "UNDEFINED");
4774 else if (vr->type == VR_RANGE || vr->type == VR_ANTI_RANGE)
4776 tree type = TREE_TYPE (vr->min);
4778 fprintf (file, "%s[", (vr->type == VR_ANTI_RANGE) ? "~" : "");
4780 if (is_negative_overflow_infinity (vr->min))
4781 fprintf (file, "-INF(OVF)");
4782 else if (INTEGRAL_TYPE_P (type)
4783 && !TYPE_UNSIGNED (type)
4784 && vrp_val_is_min (vr->min))
4785 fprintf (file, "-INF");
4786 else
4787 print_generic_expr (file, vr->min, 0);
4789 fprintf (file, ", ");
4791 if (is_positive_overflow_infinity (vr->max))
4792 fprintf (file, "+INF(OVF)");
4793 else if (INTEGRAL_TYPE_P (type)
4794 && vrp_val_is_max (vr->max))
4795 fprintf (file, "+INF");
4796 else
4797 print_generic_expr (file, vr->max, 0);
4799 fprintf (file, "]");
4801 if (vr->equiv)
4803 bitmap_iterator bi;
4804 unsigned i, c = 0;
4806 fprintf (file, " EQUIVALENCES: { ");
4808 EXECUTE_IF_SET_IN_BITMAP (vr->equiv, 0, i, bi)
4810 print_generic_expr (file, ssa_name (i), 0);
4811 fprintf (file, " ");
4812 c++;
4815 fprintf (file, "} (%u elements)", c);
4818 else if (vr->type == VR_VARYING)
4819 fprintf (file, "VARYING");
4820 else
4821 fprintf (file, "INVALID RANGE");
4825 /* Dump value range VR to stderr. */
4827 DEBUG_FUNCTION void
4828 debug_value_range (value_range_t *vr)
4830 dump_value_range (stderr, vr);
4831 fprintf (stderr, "\n");
4835 /* Dump value ranges of all SSA_NAMEs to FILE. */
4837 void
4838 dump_all_value_ranges (FILE *file)
4840 size_t i;
4842 for (i = 0; i < num_vr_values; i++)
4844 if (vr_value[i])
4846 print_generic_expr (file, ssa_name (i), 0);
4847 fprintf (file, ": ");
4848 dump_value_range (file, vr_value[i]);
4849 fprintf (file, "\n");
4853 fprintf (file, "\n");
4857 /* Dump all value ranges to stderr. */
4859 DEBUG_FUNCTION void
4860 debug_all_value_ranges (void)
4862 dump_all_value_ranges (stderr);
4866 /* Given a COND_EXPR COND of the form 'V OP W', and an SSA name V,
4867 create a new SSA name N and return the assertion assignment
4868 'N = ASSERT_EXPR <V, V OP W>'. */
4870 static gimple
4871 build_assert_expr_for (tree cond, tree v)
4873 tree a;
4874 gassign *assertion;
4876 gcc_assert (TREE_CODE (v) == SSA_NAME
4877 && COMPARISON_CLASS_P (cond));
4879 a = build2 (ASSERT_EXPR, TREE_TYPE (v), v, cond);
4880 assertion = gimple_build_assign (NULL_TREE, a);
4882 /* The new ASSERT_EXPR, creates a new SSA name that replaces the
4883 operand of the ASSERT_EXPR. Create it so the new name and the old one
4884 are registered in the replacement table so that we can fix the SSA web
4885 after adding all the ASSERT_EXPRs. */
4886 create_new_def_for (v, assertion, NULL);
4888 return assertion;
4892 /* Return false if EXPR is a predicate expression involving floating
4893 point values. */
4895 static inline bool
4896 fp_predicate (gimple stmt)
4898 GIMPLE_CHECK (stmt, GIMPLE_COND);
4900 return FLOAT_TYPE_P (TREE_TYPE (gimple_cond_lhs (stmt)));
4903 /* If the range of values taken by OP can be inferred after STMT executes,
4904 return the comparison code (COMP_CODE_P) and value (VAL_P) that
4905 describes the inferred range. Return true if a range could be
4906 inferred. */
4908 static bool
4909 infer_value_range (gimple stmt, tree op, enum tree_code *comp_code_p, tree *val_p)
4911 *val_p = NULL_TREE;
4912 *comp_code_p = ERROR_MARK;
4914 /* Do not attempt to infer anything in names that flow through
4915 abnormal edges. */
4916 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op))
4917 return false;
4919 /* Similarly, don't infer anything from statements that may throw
4920 exceptions. ??? Relax this requirement? */
4921 if (stmt_could_throw_p (stmt))
4922 return false;
4924 /* If STMT is the last statement of a basic block with no normal
4925 successors, there is no point inferring anything about any of its
4926 operands. We would not be able to find a proper insertion point
4927 for the assertion, anyway. */
4928 if (stmt_ends_bb_p (stmt))
4930 edge_iterator ei;
4931 edge e;
4933 FOR_EACH_EDGE (e, ei, gimple_bb (stmt)->succs)
4934 if (!(e->flags & EDGE_ABNORMAL))
4935 break;
4936 if (e == NULL)
4937 return false;
4940 if (infer_nonnull_range (stmt, op, true, true))
4942 *val_p = build_int_cst (TREE_TYPE (op), 0);
4943 *comp_code_p = NE_EXPR;
4944 return true;
4947 return false;
4951 void dump_asserts_for (FILE *, tree);
4952 void debug_asserts_for (tree);
4953 void dump_all_asserts (FILE *);
4954 void debug_all_asserts (void);
4956 /* Dump all the registered assertions for NAME to FILE. */
4958 void
4959 dump_asserts_for (FILE *file, tree name)
4961 assert_locus_t loc;
4963 fprintf (file, "Assertions to be inserted for ");
4964 print_generic_expr (file, name, 0);
4965 fprintf (file, "\n");
4967 loc = asserts_for[SSA_NAME_VERSION (name)];
4968 while (loc)
4970 fprintf (file, "\t");
4971 print_gimple_stmt (file, gsi_stmt (loc->si), 0, 0);
4972 fprintf (file, "\n\tBB #%d", loc->bb->index);
4973 if (loc->e)
4975 fprintf (file, "\n\tEDGE %d->%d", loc->e->src->index,
4976 loc->e->dest->index);
4977 dump_edge_info (file, loc->e, dump_flags, 0);
4979 fprintf (file, "\n\tPREDICATE: ");
4980 print_generic_expr (file, name, 0);
4981 fprintf (file, " %s ", get_tree_code_name (loc->comp_code));
4982 print_generic_expr (file, loc->val, 0);
4983 fprintf (file, "\n\n");
4984 loc = loc->next;
4987 fprintf (file, "\n");
4991 /* Dump all the registered assertions for NAME to stderr. */
4993 DEBUG_FUNCTION void
4994 debug_asserts_for (tree name)
4996 dump_asserts_for (stderr, name);
5000 /* Dump all the registered assertions for all the names to FILE. */
5002 void
5003 dump_all_asserts (FILE *file)
5005 unsigned i;
5006 bitmap_iterator bi;
5008 fprintf (file, "\nASSERT_EXPRs to be inserted\n\n");
5009 EXECUTE_IF_SET_IN_BITMAP (need_assert_for, 0, i, bi)
5010 dump_asserts_for (file, ssa_name (i));
5011 fprintf (file, "\n");
5015 /* Dump all the registered assertions for all the names to stderr. */
5017 DEBUG_FUNCTION void
5018 debug_all_asserts (void)
5020 dump_all_asserts (stderr);
5024 /* If NAME doesn't have an ASSERT_EXPR registered for asserting
5025 'EXPR COMP_CODE VAL' at a location that dominates block BB or
5026 E->DEST, then register this location as a possible insertion point
5027 for ASSERT_EXPR <NAME, EXPR COMP_CODE VAL>.
5029 BB, E and SI provide the exact insertion point for the new
5030 ASSERT_EXPR. If BB is NULL, then the ASSERT_EXPR is to be inserted
5031 on edge E. Otherwise, if E is NULL, the ASSERT_EXPR is inserted on
5032 BB. If SI points to a COND_EXPR or a SWITCH_EXPR statement, then E
5033 must not be NULL. */
5035 static void
5036 register_new_assert_for (tree name, tree expr,
5037 enum tree_code comp_code,
5038 tree val,
5039 basic_block bb,
5040 edge e,
5041 gimple_stmt_iterator si)
5043 assert_locus_t n, loc, last_loc;
5044 basic_block dest_bb;
5046 gcc_checking_assert (bb == NULL || e == NULL);
5048 if (e == NULL)
5049 gcc_checking_assert (gimple_code (gsi_stmt (si)) != GIMPLE_COND
5050 && gimple_code (gsi_stmt (si)) != GIMPLE_SWITCH);
5052 /* Never build an assert comparing against an integer constant with
5053 TREE_OVERFLOW set. This confuses our undefined overflow warning
5054 machinery. */
5055 if (TREE_OVERFLOW_P (val))
5056 val = drop_tree_overflow (val);
5058 /* The new assertion A will be inserted at BB or E. We need to
5059 determine if the new location is dominated by a previously
5060 registered location for A. If we are doing an edge insertion,
5061 assume that A will be inserted at E->DEST. Note that this is not
5062 necessarily true.
5064 If E is a critical edge, it will be split. But even if E is
5065 split, the new block will dominate the same set of blocks that
5066 E->DEST dominates.
5068 The reverse, however, is not true, blocks dominated by E->DEST
5069 will not be dominated by the new block created to split E. So,
5070 if the insertion location is on a critical edge, we will not use
5071 the new location to move another assertion previously registered
5072 at a block dominated by E->DEST. */
5073 dest_bb = (bb) ? bb : e->dest;
5075 /* If NAME already has an ASSERT_EXPR registered for COMP_CODE and
5076 VAL at a block dominating DEST_BB, then we don't need to insert a new
5077 one. Similarly, if the same assertion already exists at a block
5078 dominated by DEST_BB and the new location is not on a critical
5079 edge, then update the existing location for the assertion (i.e.,
5080 move the assertion up in the dominance tree).
5082 Note, this is implemented as a simple linked list because there
5083 should not be more than a handful of assertions registered per
5084 name. If this becomes a performance problem, a table hashed by
5085 COMP_CODE and VAL could be implemented. */
5086 loc = asserts_for[SSA_NAME_VERSION (name)];
5087 last_loc = loc;
5088 while (loc)
5090 if (loc->comp_code == comp_code
5091 && (loc->val == val
5092 || operand_equal_p (loc->val, val, 0))
5093 && (loc->expr == expr
5094 || operand_equal_p (loc->expr, expr, 0)))
5096 /* If E is not a critical edge and DEST_BB
5097 dominates the existing location for the assertion, move
5098 the assertion up in the dominance tree by updating its
5099 location information. */
5100 if ((e == NULL || !EDGE_CRITICAL_P (e))
5101 && dominated_by_p (CDI_DOMINATORS, loc->bb, dest_bb))
5103 loc->bb = dest_bb;
5104 loc->e = e;
5105 loc->si = si;
5106 return;
5110 /* Update the last node of the list and move to the next one. */
5111 last_loc = loc;
5112 loc = loc->next;
5115 /* If we didn't find an assertion already registered for
5116 NAME COMP_CODE VAL, add a new one at the end of the list of
5117 assertions associated with NAME. */
5118 n = XNEW (struct assert_locus_d);
5119 n->bb = dest_bb;
5120 n->e = e;
5121 n->si = si;
5122 n->comp_code = comp_code;
5123 n->val = val;
5124 n->expr = expr;
5125 n->next = NULL;
5127 if (last_loc)
5128 last_loc->next = n;
5129 else
5130 asserts_for[SSA_NAME_VERSION (name)] = n;
5132 bitmap_set_bit (need_assert_for, SSA_NAME_VERSION (name));
5135 /* (COND_OP0 COND_CODE COND_OP1) is a predicate which uses NAME.
5136 Extract a suitable test code and value and store them into *CODE_P and
5137 *VAL_P so the predicate is normalized to NAME *CODE_P *VAL_P.
5139 If no extraction was possible, return FALSE, otherwise return TRUE.
5141 If INVERT is true, then we invert the result stored into *CODE_P. */
5143 static bool
5144 extract_code_and_val_from_cond_with_ops (tree name, enum tree_code cond_code,
5145 tree cond_op0, tree cond_op1,
5146 bool invert, enum tree_code *code_p,
5147 tree *val_p)
5149 enum tree_code comp_code;
5150 tree val;
5152 /* Otherwise, we have a comparison of the form NAME COMP VAL
5153 or VAL COMP NAME. */
5154 if (name == cond_op1)
5156 /* If the predicate is of the form VAL COMP NAME, flip
5157 COMP around because we need to register NAME as the
5158 first operand in the predicate. */
5159 comp_code = swap_tree_comparison (cond_code);
5160 val = cond_op0;
5162 else
5164 /* The comparison is of the form NAME COMP VAL, so the
5165 comparison code remains unchanged. */
5166 comp_code = cond_code;
5167 val = cond_op1;
5170 /* Invert the comparison code as necessary. */
5171 if (invert)
5172 comp_code = invert_tree_comparison (comp_code, 0);
5174 /* VRP does not handle float types. */
5175 if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (val)))
5176 return false;
5178 /* Do not register always-false predicates.
5179 FIXME: this works around a limitation in fold() when dealing with
5180 enumerations. Given 'enum { N1, N2 } x;', fold will not
5181 fold 'if (x > N2)' to 'if (0)'. */
5182 if ((comp_code == GT_EXPR || comp_code == LT_EXPR)
5183 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
5185 tree min = TYPE_MIN_VALUE (TREE_TYPE (val));
5186 tree max = TYPE_MAX_VALUE (TREE_TYPE (val));
5188 if (comp_code == GT_EXPR
5189 && (!max
5190 || compare_values (val, max) == 0))
5191 return false;
5193 if (comp_code == LT_EXPR
5194 && (!min
5195 || compare_values (val, min) == 0))
5196 return false;
5198 *code_p = comp_code;
5199 *val_p = val;
5200 return true;
5203 /* Find out smallest RES where RES > VAL && (RES & MASK) == RES, if any
5204 (otherwise return VAL). VAL and MASK must be zero-extended for
5205 precision PREC. If SGNBIT is non-zero, first xor VAL with SGNBIT
5206 (to transform signed values into unsigned) and at the end xor
5207 SGNBIT back. */
5209 static wide_int
5210 masked_increment (const wide_int &val_in, const wide_int &mask,
5211 const wide_int &sgnbit, unsigned int prec)
5213 wide_int bit = wi::one (prec), res;
5214 unsigned int i;
5216 wide_int val = val_in ^ sgnbit;
5217 for (i = 0; i < prec; i++, bit += bit)
5219 res = mask;
5220 if ((res & bit) == 0)
5221 continue;
5222 res = bit - 1;
5223 res = (val + bit).and_not (res);
5224 res &= mask;
5225 if (wi::gtu_p (res, val))
5226 return res ^ sgnbit;
5228 return val ^ sgnbit;
5231 /* Try to register an edge assertion for SSA name NAME on edge E for
5232 the condition COND contributing to the conditional jump pointed to by BSI.
5233 Invert the condition COND if INVERT is true. */
5235 static void
5236 register_edge_assert_for_2 (tree name, edge e, gimple_stmt_iterator bsi,
5237 enum tree_code cond_code,
5238 tree cond_op0, tree cond_op1, bool invert)
5240 tree val;
5241 enum tree_code comp_code;
5243 if (!extract_code_and_val_from_cond_with_ops (name, cond_code,
5244 cond_op0,
5245 cond_op1,
5246 invert, &comp_code, &val))
5247 return;
5249 /* Only register an ASSERT_EXPR if NAME was found in the sub-graph
5250 reachable from E. */
5251 if (live_on_edge (e, name)
5252 && !has_single_use (name))
5253 register_new_assert_for (name, name, comp_code, val, NULL, e, bsi);
5255 /* In the case of NAME <= CST and NAME being defined as
5256 NAME = (unsigned) NAME2 + CST2 we can assert NAME2 >= -CST2
5257 and NAME2 <= CST - CST2. We can do the same for NAME > CST.
5258 This catches range and anti-range tests. */
5259 if ((comp_code == LE_EXPR
5260 || comp_code == GT_EXPR)
5261 && TREE_CODE (val) == INTEGER_CST
5262 && TYPE_UNSIGNED (TREE_TYPE (val)))
5264 gimple def_stmt = SSA_NAME_DEF_STMT (name);
5265 tree cst2 = NULL_TREE, name2 = NULL_TREE, name3 = NULL_TREE;
5267 /* Extract CST2 from the (optional) addition. */
5268 if (is_gimple_assign (def_stmt)
5269 && gimple_assign_rhs_code (def_stmt) == PLUS_EXPR)
5271 name2 = gimple_assign_rhs1 (def_stmt);
5272 cst2 = gimple_assign_rhs2 (def_stmt);
5273 if (TREE_CODE (name2) == SSA_NAME
5274 && TREE_CODE (cst2) == INTEGER_CST)
5275 def_stmt = SSA_NAME_DEF_STMT (name2);
5278 /* Extract NAME2 from the (optional) sign-changing cast. */
5279 if (gimple_assign_cast_p (def_stmt))
5281 if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt))
5282 && ! TYPE_UNSIGNED (TREE_TYPE (gimple_assign_rhs1 (def_stmt)))
5283 && (TYPE_PRECISION (gimple_expr_type (def_stmt))
5284 == TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt)))))
5285 name3 = gimple_assign_rhs1 (def_stmt);
5288 /* If name3 is used later, create an ASSERT_EXPR for it. */
5289 if (name3 != NULL_TREE
5290 && TREE_CODE (name3) == SSA_NAME
5291 && (cst2 == NULL_TREE
5292 || TREE_CODE (cst2) == INTEGER_CST)
5293 && INTEGRAL_TYPE_P (TREE_TYPE (name3))
5294 && live_on_edge (e, name3)
5295 && !has_single_use (name3))
5297 tree tmp;
5299 /* Build an expression for the range test. */
5300 tmp = build1 (NOP_EXPR, TREE_TYPE (name), name3);
5301 if (cst2 != NULL_TREE)
5302 tmp = build2 (PLUS_EXPR, TREE_TYPE (name), tmp, cst2);
5304 if (dump_file)
5306 fprintf (dump_file, "Adding assert for ");
5307 print_generic_expr (dump_file, name3, 0);
5308 fprintf (dump_file, " from ");
5309 print_generic_expr (dump_file, tmp, 0);
5310 fprintf (dump_file, "\n");
5313 register_new_assert_for (name3, tmp, comp_code, val, NULL, e, bsi);
5316 /* If name2 is used later, create an ASSERT_EXPR for it. */
5317 if (name2 != NULL_TREE
5318 && TREE_CODE (name2) == SSA_NAME
5319 && TREE_CODE (cst2) == INTEGER_CST
5320 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
5321 && live_on_edge (e, name2)
5322 && !has_single_use (name2))
5324 tree tmp;
5326 /* Build an expression for the range test. */
5327 tmp = name2;
5328 if (TREE_TYPE (name) != TREE_TYPE (name2))
5329 tmp = build1 (NOP_EXPR, TREE_TYPE (name), tmp);
5330 if (cst2 != NULL_TREE)
5331 tmp = build2 (PLUS_EXPR, TREE_TYPE (name), tmp, cst2);
5333 if (dump_file)
5335 fprintf (dump_file, "Adding assert for ");
5336 print_generic_expr (dump_file, name2, 0);
5337 fprintf (dump_file, " from ");
5338 print_generic_expr (dump_file, tmp, 0);
5339 fprintf (dump_file, "\n");
5342 register_new_assert_for (name2, tmp, comp_code, val, NULL, e, bsi);
5346 /* In the case of post-in/decrement tests like if (i++) ... and uses
5347 of the in/decremented value on the edge the extra name we want to
5348 assert for is not on the def chain of the name compared. Instead
5349 it is in the set of use stmts.
5350 Similar cases happen for conversions that were simplified through
5351 fold_{sign_changed,widened}_comparison. */
5352 if ((comp_code == NE_EXPR
5353 || comp_code == EQ_EXPR)
5354 && TREE_CODE (val) == INTEGER_CST)
5356 imm_use_iterator ui;
5357 gimple use_stmt;
5358 FOR_EACH_IMM_USE_STMT (use_stmt, ui, name)
5360 if (!is_gimple_assign (use_stmt))
5361 continue;
5363 /* Cut off to use-stmts that are dominating the predecessor. */
5364 if (!dominated_by_p (CDI_DOMINATORS, e->src, gimple_bb (use_stmt)))
5365 continue;
5367 tree name2 = gimple_assign_lhs (use_stmt);
5368 if (TREE_CODE (name2) != SSA_NAME
5369 || !live_on_edge (e, name2))
5370 continue;
5372 enum tree_code code = gimple_assign_rhs_code (use_stmt);
5373 tree cst;
5374 if (code == PLUS_EXPR
5375 || code == MINUS_EXPR)
5377 cst = gimple_assign_rhs2 (use_stmt);
5378 if (TREE_CODE (cst) != INTEGER_CST)
5379 continue;
5380 cst = int_const_binop (code, val, cst);
5382 else if (CONVERT_EXPR_CODE_P (code))
5383 cst = fold_convert (TREE_TYPE (name2), val);
5384 else
5385 continue;
5387 if (TREE_OVERFLOW_P (cst))
5388 cst = drop_tree_overflow (cst);
5389 register_new_assert_for (name2, name2, comp_code, cst,
5390 NULL, e, bsi);
5394 if (TREE_CODE_CLASS (comp_code) == tcc_comparison
5395 && TREE_CODE (val) == INTEGER_CST)
5397 gimple def_stmt = SSA_NAME_DEF_STMT (name);
5398 tree name2 = NULL_TREE, names[2], cst2 = NULL_TREE;
5399 tree val2 = NULL_TREE;
5400 unsigned int prec = TYPE_PRECISION (TREE_TYPE (val));
5401 wide_int mask = wi::zero (prec);
5402 unsigned int nprec = prec;
5403 enum tree_code rhs_code = ERROR_MARK;
5405 if (is_gimple_assign (def_stmt))
5406 rhs_code = gimple_assign_rhs_code (def_stmt);
5408 /* Add asserts for NAME cmp CST and NAME being defined
5409 as NAME = (int) NAME2. */
5410 if (!TYPE_UNSIGNED (TREE_TYPE (val))
5411 && (comp_code == LE_EXPR || comp_code == LT_EXPR
5412 || comp_code == GT_EXPR || comp_code == GE_EXPR)
5413 && gimple_assign_cast_p (def_stmt))
5415 name2 = gimple_assign_rhs1 (def_stmt);
5416 if (CONVERT_EXPR_CODE_P (rhs_code)
5417 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
5418 && TYPE_UNSIGNED (TREE_TYPE (name2))
5419 && prec == TYPE_PRECISION (TREE_TYPE (name2))
5420 && (comp_code == LE_EXPR || comp_code == GT_EXPR
5421 || !tree_int_cst_equal (val,
5422 TYPE_MIN_VALUE (TREE_TYPE (val))))
5423 && live_on_edge (e, name2)
5424 && !has_single_use (name2))
5426 tree tmp, cst;
5427 enum tree_code new_comp_code = comp_code;
5429 cst = fold_convert (TREE_TYPE (name2),
5430 TYPE_MIN_VALUE (TREE_TYPE (val)));
5431 /* Build an expression for the range test. */
5432 tmp = build2 (PLUS_EXPR, TREE_TYPE (name2), name2, cst);
5433 cst = fold_build2 (PLUS_EXPR, TREE_TYPE (name2), cst,
5434 fold_convert (TREE_TYPE (name2), val));
5435 if (comp_code == LT_EXPR || comp_code == GE_EXPR)
5437 new_comp_code = comp_code == LT_EXPR ? LE_EXPR : GT_EXPR;
5438 cst = fold_build2 (MINUS_EXPR, TREE_TYPE (name2), cst,
5439 build_int_cst (TREE_TYPE (name2), 1));
5442 if (dump_file)
5444 fprintf (dump_file, "Adding assert for ");
5445 print_generic_expr (dump_file, name2, 0);
5446 fprintf (dump_file, " from ");
5447 print_generic_expr (dump_file, tmp, 0);
5448 fprintf (dump_file, "\n");
5451 register_new_assert_for (name2, tmp, new_comp_code, cst, NULL,
5452 e, bsi);
5456 /* Add asserts for NAME cmp CST and NAME being defined as
5457 NAME = NAME2 >> CST2.
5459 Extract CST2 from the right shift. */
5460 if (rhs_code == RSHIFT_EXPR)
5462 name2 = gimple_assign_rhs1 (def_stmt);
5463 cst2 = gimple_assign_rhs2 (def_stmt);
5464 if (TREE_CODE (name2) == SSA_NAME
5465 && tree_fits_uhwi_p (cst2)
5466 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
5467 && IN_RANGE (tree_to_uhwi (cst2), 1, prec - 1)
5468 && prec == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (val)))
5469 && live_on_edge (e, name2)
5470 && !has_single_use (name2))
5472 mask = wi::mask (tree_to_uhwi (cst2), false, prec);
5473 val2 = fold_binary (LSHIFT_EXPR, TREE_TYPE (val), val, cst2);
5476 if (val2 != NULL_TREE
5477 && TREE_CODE (val2) == INTEGER_CST
5478 && simple_cst_equal (fold_build2 (RSHIFT_EXPR,
5479 TREE_TYPE (val),
5480 val2, cst2), val))
5482 enum tree_code new_comp_code = comp_code;
5483 tree tmp, new_val;
5485 tmp = name2;
5486 if (comp_code == EQ_EXPR || comp_code == NE_EXPR)
5488 if (!TYPE_UNSIGNED (TREE_TYPE (val)))
5490 tree type = build_nonstandard_integer_type (prec, 1);
5491 tmp = build1 (NOP_EXPR, type, name2);
5492 val2 = fold_convert (type, val2);
5494 tmp = fold_build2 (MINUS_EXPR, TREE_TYPE (tmp), tmp, val2);
5495 new_val = wide_int_to_tree (TREE_TYPE (tmp), mask);
5496 new_comp_code = comp_code == EQ_EXPR ? LE_EXPR : GT_EXPR;
5498 else if (comp_code == LT_EXPR || comp_code == GE_EXPR)
5500 wide_int minval
5501 = wi::min_value (prec, TYPE_SIGN (TREE_TYPE (val)));
5502 new_val = val2;
5503 if (minval == new_val)
5504 new_val = NULL_TREE;
5506 else
5508 wide_int maxval
5509 = wi::max_value (prec, TYPE_SIGN (TREE_TYPE (val)));
5510 mask |= val2;
5511 if (mask == maxval)
5512 new_val = NULL_TREE;
5513 else
5514 new_val = wide_int_to_tree (TREE_TYPE (val2), mask);
5517 if (new_val)
5519 if (dump_file)
5521 fprintf (dump_file, "Adding assert for ");
5522 print_generic_expr (dump_file, name2, 0);
5523 fprintf (dump_file, " from ");
5524 print_generic_expr (dump_file, tmp, 0);
5525 fprintf (dump_file, "\n");
5528 register_new_assert_for (name2, tmp, new_comp_code, new_val,
5529 NULL, e, bsi);
5533 /* Add asserts for NAME cmp CST and NAME being defined as
5534 NAME = NAME2 & CST2.
5536 Extract CST2 from the and.
5538 Also handle
5539 NAME = (unsigned) NAME2;
5540 casts where NAME's type is unsigned and has smaller precision
5541 than NAME2's type as if it was NAME = NAME2 & MASK. */
5542 names[0] = NULL_TREE;
5543 names[1] = NULL_TREE;
5544 cst2 = NULL_TREE;
5545 if (rhs_code == BIT_AND_EXPR
5546 || (CONVERT_EXPR_CODE_P (rhs_code)
5547 && TREE_CODE (TREE_TYPE (val)) == INTEGER_TYPE
5548 && TYPE_UNSIGNED (TREE_TYPE (val))
5549 && TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt)))
5550 > prec))
5552 name2 = gimple_assign_rhs1 (def_stmt);
5553 if (rhs_code == BIT_AND_EXPR)
5554 cst2 = gimple_assign_rhs2 (def_stmt);
5555 else
5557 cst2 = TYPE_MAX_VALUE (TREE_TYPE (val));
5558 nprec = TYPE_PRECISION (TREE_TYPE (name2));
5560 if (TREE_CODE (name2) == SSA_NAME
5561 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
5562 && TREE_CODE (cst2) == INTEGER_CST
5563 && !integer_zerop (cst2)
5564 && (nprec > 1
5565 || TYPE_UNSIGNED (TREE_TYPE (val))))
5567 gimple def_stmt2 = SSA_NAME_DEF_STMT (name2);
5568 if (gimple_assign_cast_p (def_stmt2))
5570 names[1] = gimple_assign_rhs1 (def_stmt2);
5571 if (!CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt2))
5572 || !INTEGRAL_TYPE_P (TREE_TYPE (names[1]))
5573 || (TYPE_PRECISION (TREE_TYPE (name2))
5574 != TYPE_PRECISION (TREE_TYPE (names[1])))
5575 || !live_on_edge (e, names[1])
5576 || has_single_use (names[1]))
5577 names[1] = NULL_TREE;
5579 if (live_on_edge (e, name2)
5580 && !has_single_use (name2))
5581 names[0] = name2;
5584 if (names[0] || names[1])
5586 wide_int minv, maxv, valv, cst2v;
5587 wide_int tem, sgnbit;
5588 bool valid_p = false, valn, cst2n;
5589 enum tree_code ccode = comp_code;
5591 valv = wide_int::from (val, nprec, UNSIGNED);
5592 cst2v = wide_int::from (cst2, nprec, UNSIGNED);
5593 valn = wi::neg_p (valv, TYPE_SIGN (TREE_TYPE (val)));
5594 cst2n = wi::neg_p (cst2v, TYPE_SIGN (TREE_TYPE (val)));
5595 /* If CST2 doesn't have most significant bit set,
5596 but VAL is negative, we have comparison like
5597 if ((x & 0x123) > -4) (always true). Just give up. */
5598 if (!cst2n && valn)
5599 ccode = ERROR_MARK;
5600 if (cst2n)
5601 sgnbit = wi::set_bit_in_zero (nprec - 1, nprec);
5602 else
5603 sgnbit = wi::zero (nprec);
5604 minv = valv & cst2v;
5605 switch (ccode)
5607 case EQ_EXPR:
5608 /* Minimum unsigned value for equality is VAL & CST2
5609 (should be equal to VAL, otherwise we probably should
5610 have folded the comparison into false) and
5611 maximum unsigned value is VAL | ~CST2. */
5612 maxv = valv | ~cst2v;
5613 valid_p = true;
5614 break;
5616 case NE_EXPR:
5617 tem = valv | ~cst2v;
5618 /* If VAL is 0, handle (X & CST2) != 0 as (X & CST2) > 0U. */
5619 if (valv == 0)
5621 cst2n = false;
5622 sgnbit = wi::zero (nprec);
5623 goto gt_expr;
5625 /* If (VAL | ~CST2) is all ones, handle it as
5626 (X & CST2) < VAL. */
5627 if (tem == -1)
5629 cst2n = false;
5630 valn = false;
5631 sgnbit = wi::zero (nprec);
5632 goto lt_expr;
5634 if (!cst2n && wi::neg_p (cst2v))
5635 sgnbit = wi::set_bit_in_zero (nprec - 1, nprec);
5636 if (sgnbit != 0)
5638 if (valv == sgnbit)
5640 cst2n = true;
5641 valn = true;
5642 goto gt_expr;
5644 if (tem == wi::mask (nprec - 1, false, nprec))
5646 cst2n = true;
5647 goto lt_expr;
5649 if (!cst2n)
5650 sgnbit = wi::zero (nprec);
5652 break;
5654 case GE_EXPR:
5655 /* Minimum unsigned value for >= if (VAL & CST2) == VAL
5656 is VAL and maximum unsigned value is ~0. For signed
5657 comparison, if CST2 doesn't have most significant bit
5658 set, handle it similarly. If CST2 has MSB set,
5659 the minimum is the same, and maximum is ~0U/2. */
5660 if (minv != valv)
5662 /* If (VAL & CST2) != VAL, X & CST2 can't be equal to
5663 VAL. */
5664 minv = masked_increment (valv, cst2v, sgnbit, nprec);
5665 if (minv == valv)
5666 break;
5668 maxv = wi::mask (nprec - (cst2n ? 1 : 0), false, nprec);
5669 valid_p = true;
5670 break;
5672 case GT_EXPR:
5673 gt_expr:
5674 /* Find out smallest MINV where MINV > VAL
5675 && (MINV & CST2) == MINV, if any. If VAL is signed and
5676 CST2 has MSB set, compute it biased by 1 << (nprec - 1). */
5677 minv = masked_increment (valv, cst2v, sgnbit, nprec);
5678 if (minv == valv)
5679 break;
5680 maxv = wi::mask (nprec - (cst2n ? 1 : 0), false, nprec);
5681 valid_p = true;
5682 break;
5684 case LE_EXPR:
5685 /* Minimum unsigned value for <= is 0 and maximum
5686 unsigned value is VAL | ~CST2 if (VAL & CST2) == VAL.
5687 Otherwise, find smallest VAL2 where VAL2 > VAL
5688 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
5689 as maximum.
5690 For signed comparison, if CST2 doesn't have most
5691 significant bit set, handle it similarly. If CST2 has
5692 MSB set, the maximum is the same and minimum is INT_MIN. */
5693 if (minv == valv)
5694 maxv = valv;
5695 else
5697 maxv = masked_increment (valv, cst2v, sgnbit, nprec);
5698 if (maxv == valv)
5699 break;
5700 maxv -= 1;
5702 maxv |= ~cst2v;
5703 minv = sgnbit;
5704 valid_p = true;
5705 break;
5707 case LT_EXPR:
5708 lt_expr:
5709 /* Minimum unsigned value for < is 0 and maximum
5710 unsigned value is (VAL-1) | ~CST2 if (VAL & CST2) == VAL.
5711 Otherwise, find smallest VAL2 where VAL2 > VAL
5712 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
5713 as maximum.
5714 For signed comparison, if CST2 doesn't have most
5715 significant bit set, handle it similarly. If CST2 has
5716 MSB set, the maximum is the same and minimum is INT_MIN. */
5717 if (minv == valv)
5719 if (valv == sgnbit)
5720 break;
5721 maxv = valv;
5723 else
5725 maxv = masked_increment (valv, cst2v, sgnbit, nprec);
5726 if (maxv == valv)
5727 break;
5729 maxv -= 1;
5730 maxv |= ~cst2v;
5731 minv = sgnbit;
5732 valid_p = true;
5733 break;
5735 default:
5736 break;
5738 if (valid_p
5739 && (maxv - minv) != -1)
5741 tree tmp, new_val, type;
5742 int i;
5744 for (i = 0; i < 2; i++)
5745 if (names[i])
5747 wide_int maxv2 = maxv;
5748 tmp = names[i];
5749 type = TREE_TYPE (names[i]);
5750 if (!TYPE_UNSIGNED (type))
5752 type = build_nonstandard_integer_type (nprec, 1);
5753 tmp = build1 (NOP_EXPR, type, names[i]);
5755 if (minv != 0)
5757 tmp = build2 (PLUS_EXPR, type, tmp,
5758 wide_int_to_tree (type, -minv));
5759 maxv2 = maxv - minv;
5761 new_val = wide_int_to_tree (type, maxv2);
5763 if (dump_file)
5765 fprintf (dump_file, "Adding assert for ");
5766 print_generic_expr (dump_file, names[i], 0);
5767 fprintf (dump_file, " from ");
5768 print_generic_expr (dump_file, tmp, 0);
5769 fprintf (dump_file, "\n");
5772 register_new_assert_for (names[i], tmp, LE_EXPR,
5773 new_val, NULL, e, bsi);
5780 /* OP is an operand of a truth value expression which is known to have
5781 a particular value. Register any asserts for OP and for any
5782 operands in OP's defining statement.
5784 If CODE is EQ_EXPR, then we want to register OP is zero (false),
5785 if CODE is NE_EXPR, then we want to register OP is nonzero (true). */
5787 static void
5788 register_edge_assert_for_1 (tree op, enum tree_code code,
5789 edge e, gimple_stmt_iterator bsi)
5791 gimple op_def;
5792 tree val;
5793 enum tree_code rhs_code;
5795 /* We only care about SSA_NAMEs. */
5796 if (TREE_CODE (op) != SSA_NAME)
5797 return;
5799 /* We know that OP will have a zero or nonzero value. If OP is used
5800 more than once go ahead and register an assert for OP. */
5801 if (live_on_edge (e, op)
5802 && !has_single_use (op))
5804 val = build_int_cst (TREE_TYPE (op), 0);
5805 register_new_assert_for (op, op, code, val, NULL, e, bsi);
5808 /* Now look at how OP is set. If it's set from a comparison,
5809 a truth operation or some bit operations, then we may be able
5810 to register information about the operands of that assignment. */
5811 op_def = SSA_NAME_DEF_STMT (op);
5812 if (gimple_code (op_def) != GIMPLE_ASSIGN)
5813 return;
5815 rhs_code = gimple_assign_rhs_code (op_def);
5817 if (TREE_CODE_CLASS (rhs_code) == tcc_comparison)
5819 bool invert = (code == EQ_EXPR ? true : false);
5820 tree op0 = gimple_assign_rhs1 (op_def);
5821 tree op1 = gimple_assign_rhs2 (op_def);
5823 if (TREE_CODE (op0) == SSA_NAME)
5824 register_edge_assert_for_2 (op0, e, bsi, rhs_code, op0, op1, invert);
5825 if (TREE_CODE (op1) == SSA_NAME)
5826 register_edge_assert_for_2 (op1, e, bsi, rhs_code, op0, op1, invert);
5828 else if ((code == NE_EXPR
5829 && gimple_assign_rhs_code (op_def) == BIT_AND_EXPR)
5830 || (code == EQ_EXPR
5831 && gimple_assign_rhs_code (op_def) == BIT_IOR_EXPR))
5833 /* Recurse on each operand. */
5834 tree op0 = gimple_assign_rhs1 (op_def);
5835 tree op1 = gimple_assign_rhs2 (op_def);
5836 if (TREE_CODE (op0) == SSA_NAME
5837 && has_single_use (op0))
5838 register_edge_assert_for_1 (op0, code, e, bsi);
5839 if (TREE_CODE (op1) == SSA_NAME
5840 && has_single_use (op1))
5841 register_edge_assert_for_1 (op1, code, e, bsi);
5843 else if (gimple_assign_rhs_code (op_def) == BIT_NOT_EXPR
5844 && TYPE_PRECISION (TREE_TYPE (gimple_assign_lhs (op_def))) == 1)
5846 /* Recurse, flipping CODE. */
5847 code = invert_tree_comparison (code, false);
5848 register_edge_assert_for_1 (gimple_assign_rhs1 (op_def), code, e, bsi);
5850 else if (gimple_assign_rhs_code (op_def) == SSA_NAME)
5852 /* Recurse through the copy. */
5853 register_edge_assert_for_1 (gimple_assign_rhs1 (op_def), code, e, bsi);
5855 else if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (op_def)))
5857 /* Recurse through the type conversion, unless it is a narrowing
5858 conversion or conversion from non-integral type. */
5859 tree rhs = gimple_assign_rhs1 (op_def);
5860 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs))
5861 && (TYPE_PRECISION (TREE_TYPE (rhs))
5862 <= TYPE_PRECISION (TREE_TYPE (op))))
5863 register_edge_assert_for_1 (rhs, code, e, bsi);
5867 /* Try to register an edge assertion for SSA name NAME on edge E for
5868 the condition COND contributing to the conditional jump pointed to by
5869 SI. */
5871 static void
5872 register_edge_assert_for (tree name, edge e, gimple_stmt_iterator si,
5873 enum tree_code cond_code, tree cond_op0,
5874 tree cond_op1)
5876 tree val;
5877 enum tree_code comp_code;
5878 bool is_else_edge = (e->flags & EDGE_FALSE_VALUE) != 0;
5880 /* Do not attempt to infer anything in names that flow through
5881 abnormal edges. */
5882 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name))
5883 return;
5885 if (!extract_code_and_val_from_cond_with_ops (name, cond_code,
5886 cond_op0, cond_op1,
5887 is_else_edge,
5888 &comp_code, &val))
5889 return;
5891 /* Register ASSERT_EXPRs for name. */
5892 register_edge_assert_for_2 (name, e, si, cond_code, cond_op0,
5893 cond_op1, is_else_edge);
5896 /* If COND is effectively an equality test of an SSA_NAME against
5897 the value zero or one, then we may be able to assert values
5898 for SSA_NAMEs which flow into COND. */
5900 /* In the case of NAME == 1 or NAME != 0, for BIT_AND_EXPR defining
5901 statement of NAME we can assert both operands of the BIT_AND_EXPR
5902 have nonzero value. */
5903 if (((comp_code == EQ_EXPR && integer_onep (val))
5904 || (comp_code == NE_EXPR && integer_zerop (val))))
5906 gimple def_stmt = SSA_NAME_DEF_STMT (name);
5908 if (is_gimple_assign (def_stmt)
5909 && gimple_assign_rhs_code (def_stmt) == BIT_AND_EXPR)
5911 tree op0 = gimple_assign_rhs1 (def_stmt);
5912 tree op1 = gimple_assign_rhs2 (def_stmt);
5913 register_edge_assert_for_1 (op0, NE_EXPR, e, si);
5914 register_edge_assert_for_1 (op1, NE_EXPR, e, si);
5918 /* In the case of NAME == 0 or NAME != 1, for BIT_IOR_EXPR defining
5919 statement of NAME we can assert both operands of the BIT_IOR_EXPR
5920 have zero value. */
5921 if (((comp_code == EQ_EXPR && integer_zerop (val))
5922 || (comp_code == NE_EXPR && integer_onep (val))))
5924 gimple def_stmt = SSA_NAME_DEF_STMT (name);
5926 /* For BIT_IOR_EXPR only if NAME == 0 both operands have
5927 necessarily zero value, or if type-precision is one. */
5928 if (is_gimple_assign (def_stmt)
5929 && (gimple_assign_rhs_code (def_stmt) == BIT_IOR_EXPR
5930 && (TYPE_PRECISION (TREE_TYPE (name)) == 1
5931 || comp_code == EQ_EXPR)))
5933 tree op0 = gimple_assign_rhs1 (def_stmt);
5934 tree op1 = gimple_assign_rhs2 (def_stmt);
5935 register_edge_assert_for_1 (op0, EQ_EXPR, e, si);
5936 register_edge_assert_for_1 (op1, EQ_EXPR, e, si);
5942 /* Determine whether the outgoing edges of BB should receive an
5943 ASSERT_EXPR for each of the operands of BB's LAST statement.
5944 The last statement of BB must be a COND_EXPR.
5946 If any of the sub-graphs rooted at BB have an interesting use of
5947 the predicate operands, an assert location node is added to the
5948 list of assertions for the corresponding operands. */
5950 static void
5951 find_conditional_asserts (basic_block bb, gcond *last)
5953 gimple_stmt_iterator bsi;
5954 tree op;
5955 edge_iterator ei;
5956 edge e;
5957 ssa_op_iter iter;
5959 bsi = gsi_for_stmt (last);
5961 /* Look for uses of the operands in each of the sub-graphs
5962 rooted at BB. We need to check each of the outgoing edges
5963 separately, so that we know what kind of ASSERT_EXPR to
5964 insert. */
5965 FOR_EACH_EDGE (e, ei, bb->succs)
5967 if (e->dest == bb)
5968 continue;
5970 /* Register the necessary assertions for each operand in the
5971 conditional predicate. */
5972 FOR_EACH_SSA_TREE_OPERAND (op, last, iter, SSA_OP_USE)
5973 register_edge_assert_for (op, e, bsi,
5974 gimple_cond_code (last),
5975 gimple_cond_lhs (last),
5976 gimple_cond_rhs (last));
5980 struct case_info
5982 tree expr;
5983 basic_block bb;
5986 /* Compare two case labels sorting first by the destination bb index
5987 and then by the case value. */
5989 static int
5990 compare_case_labels (const void *p1, const void *p2)
5992 const struct case_info *ci1 = (const struct case_info *) p1;
5993 const struct case_info *ci2 = (const struct case_info *) p2;
5994 int idx1 = ci1->bb->index;
5995 int idx2 = ci2->bb->index;
5997 if (idx1 < idx2)
5998 return -1;
5999 else if (idx1 == idx2)
6001 /* Make sure the default label is first in a group. */
6002 if (!CASE_LOW (ci1->expr))
6003 return -1;
6004 else if (!CASE_LOW (ci2->expr))
6005 return 1;
6006 else
6007 return tree_int_cst_compare (CASE_LOW (ci1->expr),
6008 CASE_LOW (ci2->expr));
6010 else
6011 return 1;
6014 /* Determine whether the outgoing edges of BB should receive an
6015 ASSERT_EXPR for each of the operands of BB's LAST statement.
6016 The last statement of BB must be a SWITCH_EXPR.
6018 If any of the sub-graphs rooted at BB have an interesting use of
6019 the predicate operands, an assert location node is added to the
6020 list of assertions for the corresponding operands. */
6022 static void
6023 find_switch_asserts (basic_block bb, gswitch *last)
6025 gimple_stmt_iterator bsi;
6026 tree op;
6027 edge e;
6028 struct case_info *ci;
6029 size_t n = gimple_switch_num_labels (last);
6030 #if GCC_VERSION >= 4000
6031 unsigned int idx;
6032 #else
6033 /* Work around GCC 3.4 bug (PR 37086). */
6034 volatile unsigned int idx;
6035 #endif
6037 bsi = gsi_for_stmt (last);
6038 op = gimple_switch_index (last);
6039 if (TREE_CODE (op) != SSA_NAME)
6040 return;
6042 /* Build a vector of case labels sorted by destination label. */
6043 ci = XNEWVEC (struct case_info, n);
6044 for (idx = 0; idx < n; ++idx)
6046 ci[idx].expr = gimple_switch_label (last, idx);
6047 ci[idx].bb = label_to_block (CASE_LABEL (ci[idx].expr));
6049 qsort (ci, n, sizeof (struct case_info), compare_case_labels);
6051 for (idx = 0; idx < n; ++idx)
6053 tree min, max;
6054 tree cl = ci[idx].expr;
6055 basic_block cbb = ci[idx].bb;
6057 min = CASE_LOW (cl);
6058 max = CASE_HIGH (cl);
6060 /* If there are multiple case labels with the same destination
6061 we need to combine them to a single value range for the edge. */
6062 if (idx + 1 < n && cbb == ci[idx + 1].bb)
6064 /* Skip labels until the last of the group. */
6065 do {
6066 ++idx;
6067 } while (idx < n && cbb == ci[idx].bb);
6068 --idx;
6070 /* Pick up the maximum of the case label range. */
6071 if (CASE_HIGH (ci[idx].expr))
6072 max = CASE_HIGH (ci[idx].expr);
6073 else
6074 max = CASE_LOW (ci[idx].expr);
6077 /* Nothing to do if the range includes the default label until we
6078 can register anti-ranges. */
6079 if (min == NULL_TREE)
6080 continue;
6082 /* Find the edge to register the assert expr on. */
6083 e = find_edge (bb, cbb);
6085 /* Register the necessary assertions for the operand in the
6086 SWITCH_EXPR. */
6087 register_edge_assert_for (op, e, bsi,
6088 max ? GE_EXPR : EQ_EXPR,
6089 op, fold_convert (TREE_TYPE (op), min));
6090 if (max)
6091 register_edge_assert_for (op, e, bsi, LE_EXPR, op,
6092 fold_convert (TREE_TYPE (op), max));
6095 XDELETEVEC (ci);
6099 /* Traverse all the statements in block BB looking for statements that
6100 may generate useful assertions for the SSA names in their operand.
6101 If a statement produces a useful assertion A for name N_i, then the
6102 list of assertions already generated for N_i is scanned to
6103 determine if A is actually needed.
6105 If N_i already had the assertion A at a location dominating the
6106 current location, then nothing needs to be done. Otherwise, the
6107 new location for A is recorded instead.
6109 1- For every statement S in BB, all the variables used by S are
6110 added to bitmap FOUND_IN_SUBGRAPH.
6112 2- If statement S uses an operand N in a way that exposes a known
6113 value range for N, then if N was not already generated by an
6114 ASSERT_EXPR, create a new assert location for N. For instance,
6115 if N is a pointer and the statement dereferences it, we can
6116 assume that N is not NULL.
6118 3- COND_EXPRs are a special case of #2. We can derive range
6119 information from the predicate but need to insert different
6120 ASSERT_EXPRs for each of the sub-graphs rooted at the
6121 conditional block. If the last statement of BB is a conditional
6122 expression of the form 'X op Y', then
6124 a) Remove X and Y from the set FOUND_IN_SUBGRAPH.
6126 b) If the conditional is the only entry point to the sub-graph
6127 corresponding to the THEN_CLAUSE, recurse into it. On
6128 return, if X and/or Y are marked in FOUND_IN_SUBGRAPH, then
6129 an ASSERT_EXPR is added for the corresponding variable.
6131 c) Repeat step (b) on the ELSE_CLAUSE.
6133 d) Mark X and Y in FOUND_IN_SUBGRAPH.
6135 For instance,
6137 if (a == 9)
6138 b = a;
6139 else
6140 b = c + 1;
6142 In this case, an assertion on the THEN clause is useful to
6143 determine that 'a' is always 9 on that edge. However, an assertion
6144 on the ELSE clause would be unnecessary.
6146 4- If BB does not end in a conditional expression, then we recurse
6147 into BB's dominator children.
6149 At the end of the recursive traversal, every SSA name will have a
6150 list of locations where ASSERT_EXPRs should be added. When a new
6151 location for name N is found, it is registered by calling
6152 register_new_assert_for. That function keeps track of all the
6153 registered assertions to prevent adding unnecessary assertions.
6154 For instance, if a pointer P_4 is dereferenced more than once in a
6155 dominator tree, only the location dominating all the dereference of
6156 P_4 will receive an ASSERT_EXPR. */
6158 static void
6159 find_assert_locations_1 (basic_block bb, sbitmap live)
6161 gimple last;
6163 last = last_stmt (bb);
6165 /* If BB's last statement is a conditional statement involving integer
6166 operands, determine if we need to add ASSERT_EXPRs. */
6167 if (last
6168 && gimple_code (last) == GIMPLE_COND
6169 && !fp_predicate (last)
6170 && !ZERO_SSA_OPERANDS (last, SSA_OP_USE))
6171 find_conditional_asserts (bb, as_a <gcond *> (last));
6173 /* If BB's last statement is a switch statement involving integer
6174 operands, determine if we need to add ASSERT_EXPRs. */
6175 if (last
6176 && gimple_code (last) == GIMPLE_SWITCH
6177 && !ZERO_SSA_OPERANDS (last, SSA_OP_USE))
6178 find_switch_asserts (bb, as_a <gswitch *> (last));
6180 /* Traverse all the statements in BB marking used names and looking
6181 for statements that may infer assertions for their used operands. */
6182 for (gimple_stmt_iterator si = gsi_last_bb (bb); !gsi_end_p (si);
6183 gsi_prev (&si))
6185 gimple stmt;
6186 tree op;
6187 ssa_op_iter i;
6189 stmt = gsi_stmt (si);
6191 if (is_gimple_debug (stmt))
6192 continue;
6194 /* See if we can derive an assertion for any of STMT's operands. */
6195 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_USE)
6197 tree value;
6198 enum tree_code comp_code;
6200 /* If op is not live beyond this stmt, do not bother to insert
6201 asserts for it. */
6202 if (!bitmap_bit_p (live, SSA_NAME_VERSION (op)))
6203 continue;
6205 /* If OP is used in such a way that we can infer a value
6206 range for it, and we don't find a previous assertion for
6207 it, create a new assertion location node for OP. */
6208 if (infer_value_range (stmt, op, &comp_code, &value))
6210 /* If we are able to infer a nonzero value range for OP,
6211 then walk backwards through the use-def chain to see if OP
6212 was set via a typecast.
6214 If so, then we can also infer a nonzero value range
6215 for the operand of the NOP_EXPR. */
6216 if (comp_code == NE_EXPR && integer_zerop (value))
6218 tree t = op;
6219 gimple def_stmt = SSA_NAME_DEF_STMT (t);
6221 while (is_gimple_assign (def_stmt)
6222 && CONVERT_EXPR_CODE_P
6223 (gimple_assign_rhs_code (def_stmt))
6224 && TREE_CODE
6225 (gimple_assign_rhs1 (def_stmt)) == SSA_NAME
6226 && POINTER_TYPE_P
6227 (TREE_TYPE (gimple_assign_rhs1 (def_stmt))))
6229 t = gimple_assign_rhs1 (def_stmt);
6230 def_stmt = SSA_NAME_DEF_STMT (t);
6232 /* Note we want to register the assert for the
6233 operand of the NOP_EXPR after SI, not after the
6234 conversion. */
6235 if (! has_single_use (t))
6236 register_new_assert_for (t, t, comp_code, value,
6237 bb, NULL, si);
6241 register_new_assert_for (op, op, comp_code, value, bb, NULL, si);
6245 /* Update live. */
6246 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_USE)
6247 bitmap_set_bit (live, SSA_NAME_VERSION (op));
6248 FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_DEF)
6249 bitmap_clear_bit (live, SSA_NAME_VERSION (op));
6252 /* Traverse all PHI nodes in BB, updating live. */
6253 for (gphi_iterator si = gsi_start_phis (bb); !gsi_end_p (si);
6254 gsi_next (&si))
6256 use_operand_p arg_p;
6257 ssa_op_iter i;
6258 gphi *phi = si.phi ();
6259 tree res = gimple_phi_result (phi);
6261 if (virtual_operand_p (res))
6262 continue;
6264 FOR_EACH_PHI_ARG (arg_p, phi, i, SSA_OP_USE)
6266 tree arg = USE_FROM_PTR (arg_p);
6267 if (TREE_CODE (arg) == SSA_NAME)
6268 bitmap_set_bit (live, SSA_NAME_VERSION (arg));
6271 bitmap_clear_bit (live, SSA_NAME_VERSION (res));
6275 /* Do an RPO walk over the function computing SSA name liveness
6276 on-the-fly and deciding on assert expressions to insert. */
6278 static void
6279 find_assert_locations (void)
6281 int *rpo = XNEWVEC (int, last_basic_block_for_fn (cfun));
6282 int *bb_rpo = XNEWVEC (int, last_basic_block_for_fn (cfun));
6283 int *last_rpo = XCNEWVEC (int, last_basic_block_for_fn (cfun));
6284 int rpo_cnt, i;
6286 live = XCNEWVEC (sbitmap, last_basic_block_for_fn (cfun));
6287 rpo_cnt = pre_and_rev_post_order_compute (NULL, rpo, false);
6288 for (i = 0; i < rpo_cnt; ++i)
6289 bb_rpo[rpo[i]] = i;
6291 /* Pre-seed loop latch liveness from loop header PHI nodes. Due to
6292 the order we compute liveness and insert asserts we otherwise
6293 fail to insert asserts into the loop latch. */
6294 loop_p loop;
6295 FOR_EACH_LOOP (loop, 0)
6297 i = loop->latch->index;
6298 unsigned int j = single_succ_edge (loop->latch)->dest_idx;
6299 for (gphi_iterator gsi = gsi_start_phis (loop->header);
6300 !gsi_end_p (gsi); gsi_next (&gsi))
6302 gphi *phi = gsi.phi ();
6303 if (virtual_operand_p (gimple_phi_result (phi)))
6304 continue;
6305 tree arg = gimple_phi_arg_def (phi, j);
6306 if (TREE_CODE (arg) == SSA_NAME)
6308 if (live[i] == NULL)
6310 live[i] = sbitmap_alloc (num_ssa_names);
6311 bitmap_clear (live[i]);
6313 bitmap_set_bit (live[i], SSA_NAME_VERSION (arg));
6318 for (i = rpo_cnt - 1; i >= 0; --i)
6320 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, rpo[i]);
6321 edge e;
6322 edge_iterator ei;
6324 if (!live[rpo[i]])
6326 live[rpo[i]] = sbitmap_alloc (num_ssa_names);
6327 bitmap_clear (live[rpo[i]]);
6330 /* Process BB and update the live information with uses in
6331 this block. */
6332 find_assert_locations_1 (bb, live[rpo[i]]);
6334 /* Merge liveness into the predecessor blocks and free it. */
6335 if (!bitmap_empty_p (live[rpo[i]]))
6337 int pred_rpo = i;
6338 FOR_EACH_EDGE (e, ei, bb->preds)
6340 int pred = e->src->index;
6341 if ((e->flags & EDGE_DFS_BACK) || pred == ENTRY_BLOCK)
6342 continue;
6344 if (!live[pred])
6346 live[pred] = sbitmap_alloc (num_ssa_names);
6347 bitmap_clear (live[pred]);
6349 bitmap_ior (live[pred], live[pred], live[rpo[i]]);
6351 if (bb_rpo[pred] < pred_rpo)
6352 pred_rpo = bb_rpo[pred];
6355 /* Record the RPO number of the last visited block that needs
6356 live information from this block. */
6357 last_rpo[rpo[i]] = pred_rpo;
6359 else
6361 sbitmap_free (live[rpo[i]]);
6362 live[rpo[i]] = NULL;
6365 /* We can free all successors live bitmaps if all their
6366 predecessors have been visited already. */
6367 FOR_EACH_EDGE (e, ei, bb->succs)
6368 if (last_rpo[e->dest->index] == i
6369 && live[e->dest->index])
6371 sbitmap_free (live[e->dest->index]);
6372 live[e->dest->index] = NULL;
6376 XDELETEVEC (rpo);
6377 XDELETEVEC (bb_rpo);
6378 XDELETEVEC (last_rpo);
6379 for (i = 0; i < last_basic_block_for_fn (cfun); ++i)
6380 if (live[i])
6381 sbitmap_free (live[i]);
6382 XDELETEVEC (live);
6385 /* Create an ASSERT_EXPR for NAME and insert it in the location
6386 indicated by LOC. Return true if we made any edge insertions. */
6388 static bool
6389 process_assert_insertions_for (tree name, assert_locus_t loc)
6391 /* Build the comparison expression NAME_i COMP_CODE VAL. */
6392 gimple stmt;
6393 tree cond;
6394 gimple assert_stmt;
6395 edge_iterator ei;
6396 edge e;
6398 /* If we have X <=> X do not insert an assert expr for that. */
6399 if (loc->expr == loc->val)
6400 return false;
6402 cond = build2 (loc->comp_code, boolean_type_node, loc->expr, loc->val);
6403 assert_stmt = build_assert_expr_for (cond, name);
6404 if (loc->e)
6406 /* We have been asked to insert the assertion on an edge. This
6407 is used only by COND_EXPR and SWITCH_EXPR assertions. */
6408 gcc_checking_assert (gimple_code (gsi_stmt (loc->si)) == GIMPLE_COND
6409 || (gimple_code (gsi_stmt (loc->si))
6410 == GIMPLE_SWITCH));
6412 gsi_insert_on_edge (loc->e, assert_stmt);
6413 return true;
6416 /* Otherwise, we can insert right after LOC->SI iff the
6417 statement must not be the last statement in the block. */
6418 stmt = gsi_stmt (loc->si);
6419 if (!stmt_ends_bb_p (stmt))
6421 gsi_insert_after (&loc->si, assert_stmt, GSI_SAME_STMT);
6422 return false;
6425 /* If STMT must be the last statement in BB, we can only insert new
6426 assertions on the non-abnormal edge out of BB. Note that since
6427 STMT is not control flow, there may only be one non-abnormal edge
6428 out of BB. */
6429 FOR_EACH_EDGE (e, ei, loc->bb->succs)
6430 if (!(e->flags & EDGE_ABNORMAL))
6432 gsi_insert_on_edge (e, assert_stmt);
6433 return true;
6436 gcc_unreachable ();
6440 /* Process all the insertions registered for every name N_i registered
6441 in NEED_ASSERT_FOR. The list of assertions to be inserted are
6442 found in ASSERTS_FOR[i]. */
6444 static void
6445 process_assert_insertions (void)
6447 unsigned i;
6448 bitmap_iterator bi;
6449 bool update_edges_p = false;
6450 int num_asserts = 0;
6452 if (dump_file && (dump_flags & TDF_DETAILS))
6453 dump_all_asserts (dump_file);
6455 EXECUTE_IF_SET_IN_BITMAP (need_assert_for, 0, i, bi)
6457 assert_locus_t loc = asserts_for[i];
6458 gcc_assert (loc);
6460 while (loc)
6462 assert_locus_t next = loc->next;
6463 update_edges_p |= process_assert_insertions_for (ssa_name (i), loc);
6464 free (loc);
6465 loc = next;
6466 num_asserts++;
6470 if (update_edges_p)
6471 gsi_commit_edge_inserts ();
6473 statistics_counter_event (cfun, "Number of ASSERT_EXPR expressions inserted",
6474 num_asserts);
6478 /* Traverse the flowgraph looking for conditional jumps to insert range
6479 expressions. These range expressions are meant to provide information
6480 to optimizations that need to reason in terms of value ranges. They
6481 will not be expanded into RTL. For instance, given:
6483 x = ...
6484 y = ...
6485 if (x < y)
6486 y = x - 2;
6487 else
6488 x = y + 3;
6490 this pass will transform the code into:
6492 x = ...
6493 y = ...
6494 if (x < y)
6496 x = ASSERT_EXPR <x, x < y>
6497 y = x - 2
6499 else
6501 y = ASSERT_EXPR <y, x >= y>
6502 x = y + 3
6505 The idea is that once copy and constant propagation have run, other
6506 optimizations will be able to determine what ranges of values can 'x'
6507 take in different paths of the code, simply by checking the reaching
6508 definition of 'x'. */
6510 static void
6511 insert_range_assertions (void)
6513 need_assert_for = BITMAP_ALLOC (NULL);
6514 asserts_for = XCNEWVEC (assert_locus_t, num_ssa_names);
6516 calculate_dominance_info (CDI_DOMINATORS);
6518 find_assert_locations ();
6519 if (!bitmap_empty_p (need_assert_for))
6521 process_assert_insertions ();
6522 update_ssa (TODO_update_ssa_no_phi);
6525 if (dump_file && (dump_flags & TDF_DETAILS))
6527 fprintf (dump_file, "\nSSA form after inserting ASSERT_EXPRs\n");
6528 dump_function_to_file (current_function_decl, dump_file, dump_flags);
6531 free (asserts_for);
6532 BITMAP_FREE (need_assert_for);
6535 /* Checks one ARRAY_REF in REF, located at LOCUS. Ignores flexible arrays
6536 and "struct" hacks. If VRP can determine that the
6537 array subscript is a constant, check if it is outside valid
6538 range. If the array subscript is a RANGE, warn if it is
6539 non-overlapping with valid range.
6540 IGNORE_OFF_BY_ONE is true if the ARRAY_REF is inside a ADDR_EXPR. */
6542 static void
6543 check_array_ref (location_t location, tree ref, bool ignore_off_by_one)
6545 value_range_t* vr = NULL;
6546 tree low_sub, up_sub;
6547 tree low_bound, up_bound, up_bound_p1;
6548 tree base;
6550 if (TREE_NO_WARNING (ref))
6551 return;
6553 low_sub = up_sub = TREE_OPERAND (ref, 1);
6554 up_bound = array_ref_up_bound (ref);
6556 /* Can not check flexible arrays. */
6557 if (!up_bound
6558 || TREE_CODE (up_bound) != INTEGER_CST)
6559 return;
6561 /* Accesses to trailing arrays via pointers may access storage
6562 beyond the types array bounds. */
6563 base = get_base_address (ref);
6564 if ((warn_array_bounds < 2)
6565 && base && TREE_CODE (base) == MEM_REF)
6567 tree cref, next = NULL_TREE;
6569 if (TREE_CODE (TREE_OPERAND (ref, 0)) != COMPONENT_REF)
6570 return;
6572 cref = TREE_OPERAND (ref, 0);
6573 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (cref, 0))) == RECORD_TYPE)
6574 for (next = DECL_CHAIN (TREE_OPERAND (cref, 1));
6575 next && TREE_CODE (next) != FIELD_DECL;
6576 next = DECL_CHAIN (next))
6579 /* If this is the last field in a struct type or a field in a
6580 union type do not warn. */
6581 if (!next)
6582 return;
6585 low_bound = array_ref_low_bound (ref);
6586 up_bound_p1 = int_const_binop (PLUS_EXPR, up_bound,
6587 build_int_cst (TREE_TYPE (up_bound), 1));
6589 /* Empty array. */
6590 if (tree_int_cst_equal (low_bound, up_bound_p1))
6592 warning_at (location, OPT_Warray_bounds,
6593 "array subscript is above array bounds");
6594 TREE_NO_WARNING (ref) = 1;
6597 if (TREE_CODE (low_sub) == SSA_NAME)
6599 vr = get_value_range (low_sub);
6600 if (vr->type == VR_RANGE || vr->type == VR_ANTI_RANGE)
6602 low_sub = vr->type == VR_RANGE ? vr->max : vr->min;
6603 up_sub = vr->type == VR_RANGE ? vr->min : vr->max;
6607 if (vr && vr->type == VR_ANTI_RANGE)
6609 if (TREE_CODE (up_sub) == INTEGER_CST
6610 && (ignore_off_by_one
6611 ? tree_int_cst_lt (up_bound, up_sub)
6612 : tree_int_cst_le (up_bound, up_sub))
6613 && TREE_CODE (low_sub) == INTEGER_CST
6614 && tree_int_cst_le (low_sub, low_bound))
6616 warning_at (location, OPT_Warray_bounds,
6617 "array subscript is outside array bounds");
6618 TREE_NO_WARNING (ref) = 1;
6621 else if (TREE_CODE (up_sub) == INTEGER_CST
6622 && (ignore_off_by_one
6623 ? !tree_int_cst_le (up_sub, up_bound_p1)
6624 : !tree_int_cst_le (up_sub, up_bound)))
6626 if (dump_file && (dump_flags & TDF_DETAILS))
6628 fprintf (dump_file, "Array bound warning for ");
6629 dump_generic_expr (MSG_NOTE, TDF_SLIM, ref);
6630 fprintf (dump_file, "\n");
6632 warning_at (location, OPT_Warray_bounds,
6633 "array subscript is above array bounds");
6634 TREE_NO_WARNING (ref) = 1;
6636 else if (TREE_CODE (low_sub) == INTEGER_CST
6637 && tree_int_cst_lt (low_sub, low_bound))
6639 if (dump_file && (dump_flags & TDF_DETAILS))
6641 fprintf (dump_file, "Array bound warning for ");
6642 dump_generic_expr (MSG_NOTE, TDF_SLIM, ref);
6643 fprintf (dump_file, "\n");
6645 warning_at (location, OPT_Warray_bounds,
6646 "array subscript is below array bounds");
6647 TREE_NO_WARNING (ref) = 1;
6651 /* Searches if the expr T, located at LOCATION computes
6652 address of an ARRAY_REF, and call check_array_ref on it. */
6654 static void
6655 search_for_addr_array (tree t, location_t location)
6657 /* Check each ARRAY_REFs in the reference chain. */
6660 if (TREE_CODE (t) == ARRAY_REF)
6661 check_array_ref (location, t, true /*ignore_off_by_one*/);
6663 t = TREE_OPERAND (t, 0);
6665 while (handled_component_p (t));
6667 if (TREE_CODE (t) == MEM_REF
6668 && TREE_CODE (TREE_OPERAND (t, 0)) == ADDR_EXPR
6669 && !TREE_NO_WARNING (t))
6671 tree tem = TREE_OPERAND (TREE_OPERAND (t, 0), 0);
6672 tree low_bound, up_bound, el_sz;
6673 offset_int idx;
6674 if (TREE_CODE (TREE_TYPE (tem)) != ARRAY_TYPE
6675 || TREE_CODE (TREE_TYPE (TREE_TYPE (tem))) == ARRAY_TYPE
6676 || !TYPE_DOMAIN (TREE_TYPE (tem)))
6677 return;
6679 low_bound = TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (tem)));
6680 up_bound = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (tem)));
6681 el_sz = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (tem)));
6682 if (!low_bound
6683 || TREE_CODE (low_bound) != INTEGER_CST
6684 || !up_bound
6685 || TREE_CODE (up_bound) != INTEGER_CST
6686 || !el_sz
6687 || TREE_CODE (el_sz) != INTEGER_CST)
6688 return;
6690 idx = mem_ref_offset (t);
6691 idx = wi::sdiv_trunc (idx, wi::to_offset (el_sz));
6692 if (wi::lts_p (idx, 0))
6694 if (dump_file && (dump_flags & TDF_DETAILS))
6696 fprintf (dump_file, "Array bound warning for ");
6697 dump_generic_expr (MSG_NOTE, TDF_SLIM, t);
6698 fprintf (dump_file, "\n");
6700 warning_at (location, OPT_Warray_bounds,
6701 "array subscript is below array bounds");
6702 TREE_NO_WARNING (t) = 1;
6704 else if (wi::gts_p (idx, (wi::to_offset (up_bound)
6705 - wi::to_offset (low_bound) + 1)))
6707 if (dump_file && (dump_flags & TDF_DETAILS))
6709 fprintf (dump_file, "Array bound warning for ");
6710 dump_generic_expr (MSG_NOTE, TDF_SLIM, t);
6711 fprintf (dump_file, "\n");
6713 warning_at (location, OPT_Warray_bounds,
6714 "array subscript is above array bounds");
6715 TREE_NO_WARNING (t) = 1;
6720 /* walk_tree() callback that checks if *TP is
6721 an ARRAY_REF inside an ADDR_EXPR (in which an array
6722 subscript one outside the valid range is allowed). Call
6723 check_array_ref for each ARRAY_REF found. The location is
6724 passed in DATA. */
6726 static tree
6727 check_array_bounds (tree *tp, int *walk_subtree, void *data)
6729 tree t = *tp;
6730 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
6731 location_t location;
6733 if (EXPR_HAS_LOCATION (t))
6734 location = EXPR_LOCATION (t);
6735 else
6737 location_t *locp = (location_t *) wi->info;
6738 location = *locp;
6741 *walk_subtree = TRUE;
6743 if (TREE_CODE (t) == ARRAY_REF)
6744 check_array_ref (location, t, false /*ignore_off_by_one*/);
6746 else if (TREE_CODE (t) == ADDR_EXPR)
6748 search_for_addr_array (t, location);
6749 *walk_subtree = FALSE;
6752 return NULL_TREE;
6755 /* Walk over all statements of all reachable BBs and call check_array_bounds
6756 on them. */
6758 static void
6759 check_all_array_refs (void)
6761 basic_block bb;
6762 gimple_stmt_iterator si;
6764 FOR_EACH_BB_FN (bb, cfun)
6766 edge_iterator ei;
6767 edge e;
6768 bool executable = false;
6770 /* Skip blocks that were found to be unreachable. */
6771 FOR_EACH_EDGE (e, ei, bb->preds)
6772 executable |= !!(e->flags & EDGE_EXECUTABLE);
6773 if (!executable)
6774 continue;
6776 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
6778 gimple stmt = gsi_stmt (si);
6779 struct walk_stmt_info wi;
6780 if (!gimple_has_location (stmt)
6781 || is_gimple_debug (stmt))
6782 continue;
6784 memset (&wi, 0, sizeof (wi));
6785 wi.info = CONST_CAST (void *, (const void *)
6786 gimple_location_ptr (stmt));
6788 walk_gimple_op (gsi_stmt (si),
6789 check_array_bounds,
6790 &wi);
6795 /* Return true if all imm uses of VAR are either in STMT, or
6796 feed (optionally through a chain of single imm uses) GIMPLE_COND
6797 in basic block COND_BB. */
6799 static bool
6800 all_imm_uses_in_stmt_or_feed_cond (tree var, gimple stmt, basic_block cond_bb)
6802 use_operand_p use_p, use2_p;
6803 imm_use_iterator iter;
6805 FOR_EACH_IMM_USE_FAST (use_p, iter, var)
6806 if (USE_STMT (use_p) != stmt)
6808 gimple use_stmt = USE_STMT (use_p), use_stmt2;
6809 if (is_gimple_debug (use_stmt))
6810 continue;
6811 while (is_gimple_assign (use_stmt)
6812 && TREE_CODE (gimple_assign_lhs (use_stmt)) == SSA_NAME
6813 && single_imm_use (gimple_assign_lhs (use_stmt),
6814 &use2_p, &use_stmt2))
6815 use_stmt = use_stmt2;
6816 if (gimple_code (use_stmt) != GIMPLE_COND
6817 || gimple_bb (use_stmt) != cond_bb)
6818 return false;
6820 return true;
6823 /* Handle
6824 _4 = x_3 & 31;
6825 if (_4 != 0)
6826 goto <bb 6>;
6827 else
6828 goto <bb 7>;
6829 <bb 6>:
6830 __builtin_unreachable ();
6831 <bb 7>:
6832 x_5 = ASSERT_EXPR <x_3, ...>;
6833 If x_3 has no other immediate uses (checked by caller),
6834 var is the x_3 var from ASSERT_EXPR, we can clear low 5 bits
6835 from the non-zero bitmask. */
6837 static void
6838 maybe_set_nonzero_bits (basic_block bb, tree var)
6840 edge e = single_pred_edge (bb);
6841 basic_block cond_bb = e->src;
6842 gimple stmt = last_stmt (cond_bb);
6843 tree cst;
6845 if (stmt == NULL
6846 || gimple_code (stmt) != GIMPLE_COND
6847 || gimple_cond_code (stmt) != ((e->flags & EDGE_TRUE_VALUE)
6848 ? EQ_EXPR : NE_EXPR)
6849 || TREE_CODE (gimple_cond_lhs (stmt)) != SSA_NAME
6850 || !integer_zerop (gimple_cond_rhs (stmt)))
6851 return;
6853 stmt = SSA_NAME_DEF_STMT (gimple_cond_lhs (stmt));
6854 if (!is_gimple_assign (stmt)
6855 || gimple_assign_rhs_code (stmt) != BIT_AND_EXPR
6856 || TREE_CODE (gimple_assign_rhs2 (stmt)) != INTEGER_CST)
6857 return;
6858 if (gimple_assign_rhs1 (stmt) != var)
6860 gimple stmt2;
6862 if (TREE_CODE (gimple_assign_rhs1 (stmt)) != SSA_NAME)
6863 return;
6864 stmt2 = SSA_NAME_DEF_STMT (gimple_assign_rhs1 (stmt));
6865 if (!gimple_assign_cast_p (stmt2)
6866 || gimple_assign_rhs1 (stmt2) != var
6867 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (stmt2))
6868 || (TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (stmt)))
6869 != TYPE_PRECISION (TREE_TYPE (var))))
6870 return;
6872 cst = gimple_assign_rhs2 (stmt);
6873 set_nonzero_bits (var, wi::bit_and_not (get_nonzero_bits (var), cst));
6876 /* Convert range assertion expressions into the implied copies and
6877 copy propagate away the copies. Doing the trivial copy propagation
6878 here avoids the need to run the full copy propagation pass after
6879 VRP.
6881 FIXME, this will eventually lead to copy propagation removing the
6882 names that had useful range information attached to them. For
6883 instance, if we had the assertion N_i = ASSERT_EXPR <N_j, N_j > 3>,
6884 then N_i will have the range [3, +INF].
6886 However, by converting the assertion into the implied copy
6887 operation N_i = N_j, we will then copy-propagate N_j into the uses
6888 of N_i and lose the range information. We may want to hold on to
6889 ASSERT_EXPRs a little while longer as the ranges could be used in
6890 things like jump threading.
6892 The problem with keeping ASSERT_EXPRs around is that passes after
6893 VRP need to handle them appropriately.
6895 Another approach would be to make the range information a first
6896 class property of the SSA_NAME so that it can be queried from
6897 any pass. This is made somewhat more complex by the need for
6898 multiple ranges to be associated with one SSA_NAME. */
6900 static void
6901 remove_range_assertions (void)
6903 basic_block bb;
6904 gimple_stmt_iterator si;
6905 /* 1 if looking at ASSERT_EXPRs immediately at the beginning of
6906 a basic block preceeded by GIMPLE_COND branching to it and
6907 __builtin_trap, -1 if not yet checked, 0 otherwise. */
6908 int is_unreachable;
6910 /* Note that the BSI iterator bump happens at the bottom of the
6911 loop and no bump is necessary if we're removing the statement
6912 referenced by the current BSI. */
6913 FOR_EACH_BB_FN (bb, cfun)
6914 for (si = gsi_after_labels (bb), is_unreachable = -1; !gsi_end_p (si);)
6916 gimple stmt = gsi_stmt (si);
6917 gimple use_stmt;
6919 if (is_gimple_assign (stmt)
6920 && gimple_assign_rhs_code (stmt) == ASSERT_EXPR)
6922 tree lhs = gimple_assign_lhs (stmt);
6923 tree rhs = gimple_assign_rhs1 (stmt);
6924 tree var;
6925 tree cond = fold (ASSERT_EXPR_COND (rhs));
6926 use_operand_p use_p;
6927 imm_use_iterator iter;
6929 gcc_assert (cond != boolean_false_node);
6931 var = ASSERT_EXPR_VAR (rhs);
6932 gcc_assert (TREE_CODE (var) == SSA_NAME);
6934 if (!POINTER_TYPE_P (TREE_TYPE (lhs))
6935 && SSA_NAME_RANGE_INFO (lhs))
6937 if (is_unreachable == -1)
6939 is_unreachable = 0;
6940 if (single_pred_p (bb)
6941 && assert_unreachable_fallthru_edge_p
6942 (single_pred_edge (bb)))
6943 is_unreachable = 1;
6945 /* Handle
6946 if (x_7 >= 10 && x_7 < 20)
6947 __builtin_unreachable ();
6948 x_8 = ASSERT_EXPR <x_7, ...>;
6949 if the only uses of x_7 are in the ASSERT_EXPR and
6950 in the condition. In that case, we can copy the
6951 range info from x_8 computed in this pass also
6952 for x_7. */
6953 if (is_unreachable
6954 && all_imm_uses_in_stmt_or_feed_cond (var, stmt,
6955 single_pred (bb)))
6957 set_range_info (var, SSA_NAME_RANGE_TYPE (lhs),
6958 SSA_NAME_RANGE_INFO (lhs)->get_min (),
6959 SSA_NAME_RANGE_INFO (lhs)->get_max ());
6960 maybe_set_nonzero_bits (bb, var);
6964 /* Propagate the RHS into every use of the LHS. */
6965 FOR_EACH_IMM_USE_STMT (use_stmt, iter, lhs)
6966 FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
6967 SET_USE (use_p, var);
6969 /* And finally, remove the copy, it is not needed. */
6970 gsi_remove (&si, true);
6971 release_defs (stmt);
6973 else
6975 if (!is_gimple_debug (gsi_stmt (si)))
6976 is_unreachable = 0;
6977 gsi_next (&si);
6983 /* Return true if STMT is interesting for VRP. */
6985 static bool
6986 stmt_interesting_for_vrp (gimple stmt)
6988 if (gimple_code (stmt) == GIMPLE_PHI)
6990 tree res = gimple_phi_result (stmt);
6991 return (!virtual_operand_p (res)
6992 && (INTEGRAL_TYPE_P (TREE_TYPE (res))
6993 || POINTER_TYPE_P (TREE_TYPE (res))));
6995 else if (is_gimple_assign (stmt) || is_gimple_call (stmt))
6997 tree lhs = gimple_get_lhs (stmt);
6999 /* In general, assignments with virtual operands are not useful
7000 for deriving ranges, with the obvious exception of calls to
7001 builtin functions. */
7002 if (lhs && TREE_CODE (lhs) == SSA_NAME
7003 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs))
7004 || POINTER_TYPE_P (TREE_TYPE (lhs)))
7005 && (is_gimple_call (stmt)
7006 || !gimple_vuse (stmt)))
7007 return true;
7008 else if (is_gimple_call (stmt) && gimple_call_internal_p (stmt))
7009 switch (gimple_call_internal_fn (stmt))
7011 case IFN_ADD_OVERFLOW:
7012 case IFN_SUB_OVERFLOW:
7013 case IFN_MUL_OVERFLOW:
7014 /* These internal calls return _Complex integer type,
7015 but are interesting to VRP nevertheless. */
7016 if (lhs && TREE_CODE (lhs) == SSA_NAME)
7017 return true;
7018 break;
7019 default:
7020 break;
7023 else if (gimple_code (stmt) == GIMPLE_COND
7024 || gimple_code (stmt) == GIMPLE_SWITCH)
7025 return true;
7027 return false;
7031 /* Initialize local data structures for VRP. */
7033 static void
7034 vrp_initialize (void)
7036 basic_block bb;
7038 values_propagated = false;
7039 num_vr_values = num_ssa_names;
7040 vr_value = XCNEWVEC (value_range_t *, num_vr_values);
7041 vr_phi_edge_counts = XCNEWVEC (int, num_ssa_names);
7043 FOR_EACH_BB_FN (bb, cfun)
7045 for (gphi_iterator si = gsi_start_phis (bb); !gsi_end_p (si);
7046 gsi_next (&si))
7048 gphi *phi = si.phi ();
7049 if (!stmt_interesting_for_vrp (phi))
7051 tree lhs = PHI_RESULT (phi);
7052 set_value_range_to_varying (get_value_range (lhs));
7053 prop_set_simulate_again (phi, false);
7055 else
7056 prop_set_simulate_again (phi, true);
7059 for (gimple_stmt_iterator si = gsi_start_bb (bb); !gsi_end_p (si);
7060 gsi_next (&si))
7062 gimple stmt = gsi_stmt (si);
7064 /* If the statement is a control insn, then we do not
7065 want to avoid simulating the statement once. Failure
7066 to do so means that those edges will never get added. */
7067 if (stmt_ends_bb_p (stmt))
7068 prop_set_simulate_again (stmt, true);
7069 else if (!stmt_interesting_for_vrp (stmt))
7071 ssa_op_iter i;
7072 tree def;
7073 FOR_EACH_SSA_TREE_OPERAND (def, stmt, i, SSA_OP_DEF)
7074 set_value_range_to_varying (get_value_range (def));
7075 prop_set_simulate_again (stmt, false);
7077 else
7078 prop_set_simulate_again (stmt, true);
7083 /* Return the singleton value-range for NAME or NAME. */
7085 static inline tree
7086 vrp_valueize (tree name)
7088 if (TREE_CODE (name) == SSA_NAME)
7090 value_range_t *vr = get_value_range (name);
7091 if (vr->type == VR_RANGE
7092 && (vr->min == vr->max
7093 || operand_equal_p (vr->min, vr->max, 0)))
7094 return vr->min;
7096 return name;
7099 /* Return the singleton value-range for NAME if that is a constant
7100 but signal to not follow SSA edges. */
7102 static inline tree
7103 vrp_valueize_1 (tree name)
7105 if (TREE_CODE (name) == SSA_NAME)
7107 /* If the definition may be simulated again we cannot follow
7108 this SSA edge as the SSA propagator does not necessarily
7109 re-visit the use. */
7110 gimple def_stmt = SSA_NAME_DEF_STMT (name);
7111 if (!gimple_nop_p (def_stmt)
7112 && prop_simulate_again_p (def_stmt))
7113 return NULL_TREE;
7114 value_range_t *vr = get_value_range (name);
7115 if (range_int_cst_singleton_p (vr))
7116 return vr->min;
7118 return name;
7121 /* Visit assignment STMT. If it produces an interesting range, record
7122 the SSA name in *OUTPUT_P. */
7124 static enum ssa_prop_result
7125 vrp_visit_assignment_or_call (gimple stmt, tree *output_p)
7127 tree def, lhs;
7128 ssa_op_iter iter;
7129 enum gimple_code code = gimple_code (stmt);
7130 lhs = gimple_get_lhs (stmt);
7132 /* We only keep track of ranges in integral and pointer types. */
7133 if (TREE_CODE (lhs) == SSA_NAME
7134 && ((INTEGRAL_TYPE_P (TREE_TYPE (lhs))
7135 /* It is valid to have NULL MIN/MAX values on a type. See
7136 build_range_type. */
7137 && TYPE_MIN_VALUE (TREE_TYPE (lhs))
7138 && TYPE_MAX_VALUE (TREE_TYPE (lhs)))
7139 || POINTER_TYPE_P (TREE_TYPE (lhs))))
7141 value_range_t new_vr = VR_INITIALIZER;
7143 /* Try folding the statement to a constant first. */
7144 tree tem = gimple_fold_stmt_to_constant_1 (stmt, vrp_valueize,
7145 vrp_valueize_1);
7146 if (tem && is_gimple_min_invariant (tem))
7147 set_value_range_to_value (&new_vr, tem, NULL);
7148 /* Then dispatch to value-range extracting functions. */
7149 else if (code == GIMPLE_CALL)
7150 extract_range_basic (&new_vr, stmt);
7151 else
7152 extract_range_from_assignment (&new_vr, as_a <gassign *> (stmt));
7154 if (update_value_range (lhs, &new_vr))
7156 *output_p = lhs;
7158 if (dump_file && (dump_flags & TDF_DETAILS))
7160 fprintf (dump_file, "Found new range for ");
7161 print_generic_expr (dump_file, lhs, 0);
7162 fprintf (dump_file, ": ");
7163 dump_value_range (dump_file, &new_vr);
7164 fprintf (dump_file, "\n");
7167 if (new_vr.type == VR_VARYING)
7168 return SSA_PROP_VARYING;
7170 return SSA_PROP_INTERESTING;
7173 return SSA_PROP_NOT_INTERESTING;
7175 else if (is_gimple_call (stmt) && gimple_call_internal_p (stmt))
7176 switch (gimple_call_internal_fn (stmt))
7178 case IFN_ADD_OVERFLOW:
7179 case IFN_SUB_OVERFLOW:
7180 case IFN_MUL_OVERFLOW:
7181 /* These internal calls return _Complex integer type,
7182 which VRP does not track, but the immediate uses
7183 thereof might be interesting. */
7184 if (lhs && TREE_CODE (lhs) == SSA_NAME)
7186 imm_use_iterator iter;
7187 use_operand_p use_p;
7188 enum ssa_prop_result res = SSA_PROP_VARYING;
7190 set_value_range_to_varying (get_value_range (lhs));
7192 FOR_EACH_IMM_USE_FAST (use_p, iter, lhs)
7194 gimple use_stmt = USE_STMT (use_p);
7195 if (!is_gimple_assign (use_stmt))
7196 continue;
7197 enum tree_code rhs_code = gimple_assign_rhs_code (use_stmt);
7198 if (rhs_code != REALPART_EXPR && rhs_code != IMAGPART_EXPR)
7199 continue;
7200 tree rhs1 = gimple_assign_rhs1 (use_stmt);
7201 tree use_lhs = gimple_assign_lhs (use_stmt);
7202 if (TREE_CODE (rhs1) != rhs_code
7203 || TREE_OPERAND (rhs1, 0) != lhs
7204 || TREE_CODE (use_lhs) != SSA_NAME
7205 || !stmt_interesting_for_vrp (use_stmt)
7206 || (!INTEGRAL_TYPE_P (TREE_TYPE (use_lhs))
7207 || !TYPE_MIN_VALUE (TREE_TYPE (use_lhs))
7208 || !TYPE_MAX_VALUE (TREE_TYPE (use_lhs))))
7209 continue;
7211 /* If there is a change in the value range for any of the
7212 REALPART_EXPR/IMAGPART_EXPR immediate uses, return
7213 SSA_PROP_INTERESTING. If there are any REALPART_EXPR
7214 or IMAGPART_EXPR immediate uses, but none of them have
7215 a change in their value ranges, return
7216 SSA_PROP_NOT_INTERESTING. If there are no
7217 {REAL,IMAG}PART_EXPR uses at all,
7218 return SSA_PROP_VARYING. */
7219 value_range_t new_vr = VR_INITIALIZER;
7220 extract_range_basic (&new_vr, use_stmt);
7221 value_range_t *old_vr = get_value_range (use_lhs);
7222 if (old_vr->type != new_vr.type
7223 || !vrp_operand_equal_p (old_vr->min, new_vr.min)
7224 || !vrp_operand_equal_p (old_vr->max, new_vr.max)
7225 || !vrp_bitmap_equal_p (old_vr->equiv, new_vr.equiv))
7226 res = SSA_PROP_INTERESTING;
7227 else
7228 res = SSA_PROP_NOT_INTERESTING;
7229 BITMAP_FREE (new_vr.equiv);
7230 if (res == SSA_PROP_INTERESTING)
7232 *output_p = lhs;
7233 return res;
7237 return res;
7239 break;
7240 default:
7241 break;
7244 /* Every other statement produces no useful ranges. */
7245 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_DEF)
7246 set_value_range_to_varying (get_value_range (def));
7248 return SSA_PROP_VARYING;
7251 /* Helper that gets the value range of the SSA_NAME with version I
7252 or a symbolic range containing the SSA_NAME only if the value range
7253 is varying or undefined. */
7255 static inline value_range_t
7256 get_vr_for_comparison (int i)
7258 value_range_t vr = *get_value_range (ssa_name (i));
7260 /* If name N_i does not have a valid range, use N_i as its own
7261 range. This allows us to compare against names that may
7262 have N_i in their ranges. */
7263 if (vr.type == VR_VARYING || vr.type == VR_UNDEFINED)
7265 vr.type = VR_RANGE;
7266 vr.min = ssa_name (i);
7267 vr.max = ssa_name (i);
7270 return vr;
7273 /* Compare all the value ranges for names equivalent to VAR with VAL
7274 using comparison code COMP. Return the same value returned by
7275 compare_range_with_value, including the setting of
7276 *STRICT_OVERFLOW_P. */
7278 static tree
7279 compare_name_with_value (enum tree_code comp, tree var, tree val,
7280 bool *strict_overflow_p)
7282 bitmap_iterator bi;
7283 unsigned i;
7284 bitmap e;
7285 tree retval, t;
7286 int used_strict_overflow;
7287 bool sop;
7288 value_range_t equiv_vr;
7290 /* Get the set of equivalences for VAR. */
7291 e = get_value_range (var)->equiv;
7293 /* Start at -1. Set it to 0 if we do a comparison without relying
7294 on overflow, or 1 if all comparisons rely on overflow. */
7295 used_strict_overflow = -1;
7297 /* Compare vars' value range with val. */
7298 equiv_vr = get_vr_for_comparison (SSA_NAME_VERSION (var));
7299 sop = false;
7300 retval = compare_range_with_value (comp, &equiv_vr, val, &sop);
7301 if (retval)
7302 used_strict_overflow = sop ? 1 : 0;
7304 /* If the equiv set is empty we have done all work we need to do. */
7305 if (e == NULL)
7307 if (retval
7308 && used_strict_overflow > 0)
7309 *strict_overflow_p = true;
7310 return retval;
7313 EXECUTE_IF_SET_IN_BITMAP (e, 0, i, bi)
7315 equiv_vr = get_vr_for_comparison (i);
7316 sop = false;
7317 t = compare_range_with_value (comp, &equiv_vr, val, &sop);
7318 if (t)
7320 /* If we get different answers from different members
7321 of the equivalence set this check must be in a dead
7322 code region. Folding it to a trap representation
7323 would be correct here. For now just return don't-know. */
7324 if (retval != NULL
7325 && t != retval)
7327 retval = NULL_TREE;
7328 break;
7330 retval = t;
7332 if (!sop)
7333 used_strict_overflow = 0;
7334 else if (used_strict_overflow < 0)
7335 used_strict_overflow = 1;
7339 if (retval
7340 && used_strict_overflow > 0)
7341 *strict_overflow_p = true;
7343 return retval;
7347 /* Given a comparison code COMP and names N1 and N2, compare all the
7348 ranges equivalent to N1 against all the ranges equivalent to N2
7349 to determine the value of N1 COMP N2. Return the same value
7350 returned by compare_ranges. Set *STRICT_OVERFLOW_P to indicate
7351 whether we relied on an overflow infinity in the comparison. */
7354 static tree
7355 compare_names (enum tree_code comp, tree n1, tree n2,
7356 bool *strict_overflow_p)
7358 tree t, retval;
7359 bitmap e1, e2;
7360 bitmap_iterator bi1, bi2;
7361 unsigned i1, i2;
7362 int used_strict_overflow;
7363 static bitmap_obstack *s_obstack = NULL;
7364 static bitmap s_e1 = NULL, s_e2 = NULL;
7366 /* Compare the ranges of every name equivalent to N1 against the
7367 ranges of every name equivalent to N2. */
7368 e1 = get_value_range (n1)->equiv;
7369 e2 = get_value_range (n2)->equiv;
7371 /* Use the fake bitmaps if e1 or e2 are not available. */
7372 if (s_obstack == NULL)
7374 s_obstack = XNEW (bitmap_obstack);
7375 bitmap_obstack_initialize (s_obstack);
7376 s_e1 = BITMAP_ALLOC (s_obstack);
7377 s_e2 = BITMAP_ALLOC (s_obstack);
7379 if (e1 == NULL)
7380 e1 = s_e1;
7381 if (e2 == NULL)
7382 e2 = s_e2;
7384 /* Add N1 and N2 to their own set of equivalences to avoid
7385 duplicating the body of the loop just to check N1 and N2
7386 ranges. */
7387 bitmap_set_bit (e1, SSA_NAME_VERSION (n1));
7388 bitmap_set_bit (e2, SSA_NAME_VERSION (n2));
7390 /* If the equivalence sets have a common intersection, then the two
7391 names can be compared without checking their ranges. */
7392 if (bitmap_intersect_p (e1, e2))
7394 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
7395 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
7397 return (comp == EQ_EXPR || comp == GE_EXPR || comp == LE_EXPR)
7398 ? boolean_true_node
7399 : boolean_false_node;
7402 /* Start at -1. Set it to 0 if we do a comparison without relying
7403 on overflow, or 1 if all comparisons rely on overflow. */
7404 used_strict_overflow = -1;
7406 /* Otherwise, compare all the equivalent ranges. First, add N1 and
7407 N2 to their own set of equivalences to avoid duplicating the body
7408 of the loop just to check N1 and N2 ranges. */
7409 EXECUTE_IF_SET_IN_BITMAP (e1, 0, i1, bi1)
7411 value_range_t vr1 = get_vr_for_comparison (i1);
7413 t = retval = NULL_TREE;
7414 EXECUTE_IF_SET_IN_BITMAP (e2, 0, i2, bi2)
7416 bool sop = false;
7418 value_range_t vr2 = get_vr_for_comparison (i2);
7420 t = compare_ranges (comp, &vr1, &vr2, &sop);
7421 if (t)
7423 /* If we get different answers from different members
7424 of the equivalence set this check must be in a dead
7425 code region. Folding it to a trap representation
7426 would be correct here. For now just return don't-know. */
7427 if (retval != NULL
7428 && t != retval)
7430 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
7431 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
7432 return NULL_TREE;
7434 retval = t;
7436 if (!sop)
7437 used_strict_overflow = 0;
7438 else if (used_strict_overflow < 0)
7439 used_strict_overflow = 1;
7443 if (retval)
7445 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
7446 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
7447 if (used_strict_overflow > 0)
7448 *strict_overflow_p = true;
7449 return retval;
7453 /* None of the equivalent ranges are useful in computing this
7454 comparison. */
7455 bitmap_clear_bit (e1, SSA_NAME_VERSION (n1));
7456 bitmap_clear_bit (e2, SSA_NAME_VERSION (n2));
7457 return NULL_TREE;
7460 /* Helper function for vrp_evaluate_conditional_warnv. */
7462 static tree
7463 vrp_evaluate_conditional_warnv_with_ops_using_ranges (enum tree_code code,
7464 tree op0, tree op1,
7465 bool * strict_overflow_p)
7467 value_range_t *vr0, *vr1;
7469 vr0 = (TREE_CODE (op0) == SSA_NAME) ? get_value_range (op0) : NULL;
7470 vr1 = (TREE_CODE (op1) == SSA_NAME) ? get_value_range (op1) : NULL;
7472 tree res = NULL_TREE;
7473 if (vr0 && vr1)
7474 res = compare_ranges (code, vr0, vr1, strict_overflow_p);
7475 if (!res && vr0)
7476 res = compare_range_with_value (code, vr0, op1, strict_overflow_p);
7477 if (!res && vr1)
7478 res = (compare_range_with_value
7479 (swap_tree_comparison (code), vr1, op0, strict_overflow_p));
7480 return res;
7483 /* Helper function for vrp_evaluate_conditional_warnv. */
7485 static tree
7486 vrp_evaluate_conditional_warnv_with_ops (enum tree_code code, tree op0,
7487 tree op1, bool use_equiv_p,
7488 bool *strict_overflow_p, bool *only_ranges)
7490 tree ret;
7491 if (only_ranges)
7492 *only_ranges = true;
7494 /* We only deal with integral and pointer types. */
7495 if (!INTEGRAL_TYPE_P (TREE_TYPE (op0))
7496 && !POINTER_TYPE_P (TREE_TYPE (op0)))
7497 return NULL_TREE;
7499 if (use_equiv_p)
7501 if (only_ranges
7502 && (ret = vrp_evaluate_conditional_warnv_with_ops_using_ranges
7503 (code, op0, op1, strict_overflow_p)))
7504 return ret;
7505 *only_ranges = false;
7506 if (TREE_CODE (op0) == SSA_NAME && TREE_CODE (op1) == SSA_NAME)
7507 return compare_names (code, op0, op1, strict_overflow_p);
7508 else if (TREE_CODE (op0) == SSA_NAME)
7509 return compare_name_with_value (code, op0, op1, strict_overflow_p);
7510 else if (TREE_CODE (op1) == SSA_NAME)
7511 return (compare_name_with_value
7512 (swap_tree_comparison (code), op1, op0, strict_overflow_p));
7514 else
7515 return vrp_evaluate_conditional_warnv_with_ops_using_ranges (code, op0, op1,
7516 strict_overflow_p);
7517 return NULL_TREE;
7520 /* Given (CODE OP0 OP1) within STMT, try to simplify it based on value range
7521 information. Return NULL if the conditional can not be evaluated.
7522 The ranges of all the names equivalent with the operands in COND
7523 will be used when trying to compute the value. If the result is
7524 based on undefined signed overflow, issue a warning if
7525 appropriate. */
7527 static tree
7528 vrp_evaluate_conditional (enum tree_code code, tree op0, tree op1, gimple stmt)
7530 bool sop;
7531 tree ret;
7532 bool only_ranges;
7534 /* Some passes and foldings leak constants with overflow flag set
7535 into the IL. Avoid doing wrong things with these and bail out. */
7536 if ((TREE_CODE (op0) == INTEGER_CST
7537 && TREE_OVERFLOW (op0))
7538 || (TREE_CODE (op1) == INTEGER_CST
7539 && TREE_OVERFLOW (op1)))
7540 return NULL_TREE;
7542 sop = false;
7543 ret = vrp_evaluate_conditional_warnv_with_ops (code, op0, op1, true, &sop,
7544 &only_ranges);
7546 if (ret && sop)
7548 enum warn_strict_overflow_code wc;
7549 const char* warnmsg;
7551 if (is_gimple_min_invariant (ret))
7553 wc = WARN_STRICT_OVERFLOW_CONDITIONAL;
7554 warnmsg = G_("assuming signed overflow does not occur when "
7555 "simplifying conditional to constant");
7557 else
7559 wc = WARN_STRICT_OVERFLOW_COMPARISON;
7560 warnmsg = G_("assuming signed overflow does not occur when "
7561 "simplifying conditional");
7564 if (issue_strict_overflow_warning (wc))
7566 location_t location;
7568 if (!gimple_has_location (stmt))
7569 location = input_location;
7570 else
7571 location = gimple_location (stmt);
7572 warning_at (location, OPT_Wstrict_overflow, "%s", warnmsg);
7576 if (warn_type_limits
7577 && ret && only_ranges
7578 && TREE_CODE_CLASS (code) == tcc_comparison
7579 && TREE_CODE (op0) == SSA_NAME)
7581 /* If the comparison is being folded and the operand on the LHS
7582 is being compared against a constant value that is outside of
7583 the natural range of OP0's type, then the predicate will
7584 always fold regardless of the value of OP0. If -Wtype-limits
7585 was specified, emit a warning. */
7586 tree type = TREE_TYPE (op0);
7587 value_range_t *vr0 = get_value_range (op0);
7589 if (vr0->type == VR_RANGE
7590 && INTEGRAL_TYPE_P (type)
7591 && vrp_val_is_min (vr0->min)
7592 && vrp_val_is_max (vr0->max)
7593 && is_gimple_min_invariant (op1))
7595 location_t location;
7597 if (!gimple_has_location (stmt))
7598 location = input_location;
7599 else
7600 location = gimple_location (stmt);
7602 warning_at (location, OPT_Wtype_limits,
7603 integer_zerop (ret)
7604 ? G_("comparison always false "
7605 "due to limited range of data type")
7606 : G_("comparison always true "
7607 "due to limited range of data type"));
7611 return ret;
7615 /* Visit conditional statement STMT. If we can determine which edge
7616 will be taken out of STMT's basic block, record it in
7617 *TAKEN_EDGE_P and return SSA_PROP_INTERESTING. Otherwise, return
7618 SSA_PROP_VARYING. */
7620 static enum ssa_prop_result
7621 vrp_visit_cond_stmt (gcond *stmt, edge *taken_edge_p)
7623 tree val;
7624 bool sop;
7626 *taken_edge_p = NULL;
7628 if (dump_file && (dump_flags & TDF_DETAILS))
7630 tree use;
7631 ssa_op_iter i;
7633 fprintf (dump_file, "\nVisiting conditional with predicate: ");
7634 print_gimple_stmt (dump_file, stmt, 0, 0);
7635 fprintf (dump_file, "\nWith known ranges\n");
7637 FOR_EACH_SSA_TREE_OPERAND (use, stmt, i, SSA_OP_USE)
7639 fprintf (dump_file, "\t");
7640 print_generic_expr (dump_file, use, 0);
7641 fprintf (dump_file, ": ");
7642 dump_value_range (dump_file, vr_value[SSA_NAME_VERSION (use)]);
7645 fprintf (dump_file, "\n");
7648 /* Compute the value of the predicate COND by checking the known
7649 ranges of each of its operands.
7651 Note that we cannot evaluate all the equivalent ranges here
7652 because those ranges may not yet be final and with the current
7653 propagation strategy, we cannot determine when the value ranges
7654 of the names in the equivalence set have changed.
7656 For instance, given the following code fragment
7658 i_5 = PHI <8, i_13>
7660 i_14 = ASSERT_EXPR <i_5, i_5 != 0>
7661 if (i_14 == 1)
7664 Assume that on the first visit to i_14, i_5 has the temporary
7665 range [8, 8] because the second argument to the PHI function is
7666 not yet executable. We derive the range ~[0, 0] for i_14 and the
7667 equivalence set { i_5 }. So, when we visit 'if (i_14 == 1)' for
7668 the first time, since i_14 is equivalent to the range [8, 8], we
7669 determine that the predicate is always false.
7671 On the next round of propagation, i_13 is determined to be
7672 VARYING, which causes i_5 to drop down to VARYING. So, another
7673 visit to i_14 is scheduled. In this second visit, we compute the
7674 exact same range and equivalence set for i_14, namely ~[0, 0] and
7675 { i_5 }. But we did not have the previous range for i_5
7676 registered, so vrp_visit_assignment thinks that the range for
7677 i_14 has not changed. Therefore, the predicate 'if (i_14 == 1)'
7678 is not visited again, which stops propagation from visiting
7679 statements in the THEN clause of that if().
7681 To properly fix this we would need to keep the previous range
7682 value for the names in the equivalence set. This way we would've
7683 discovered that from one visit to the other i_5 changed from
7684 range [8, 8] to VR_VARYING.
7686 However, fixing this apparent limitation may not be worth the
7687 additional checking. Testing on several code bases (GCC, DLV,
7688 MICO, TRAMP3D and SPEC2000) showed that doing this results in
7689 4 more predicates folded in SPEC. */
7690 sop = false;
7692 val = vrp_evaluate_conditional_warnv_with_ops (gimple_cond_code (stmt),
7693 gimple_cond_lhs (stmt),
7694 gimple_cond_rhs (stmt),
7695 false, &sop, NULL);
7696 if (val)
7698 if (!sop)
7699 *taken_edge_p = find_taken_edge (gimple_bb (stmt), val);
7700 else
7702 if (dump_file && (dump_flags & TDF_DETAILS))
7703 fprintf (dump_file,
7704 "\nIgnoring predicate evaluation because "
7705 "it assumes that signed overflow is undefined");
7706 val = NULL_TREE;
7710 if (dump_file && (dump_flags & TDF_DETAILS))
7712 fprintf (dump_file, "\nPredicate evaluates to: ");
7713 if (val == NULL_TREE)
7714 fprintf (dump_file, "DON'T KNOW\n");
7715 else
7716 print_generic_stmt (dump_file, val, 0);
7719 return (*taken_edge_p) ? SSA_PROP_INTERESTING : SSA_PROP_VARYING;
7722 /* Searches the case label vector VEC for the index *IDX of the CASE_LABEL
7723 that includes the value VAL. The search is restricted to the range
7724 [START_IDX, n - 1] where n is the size of VEC.
7726 If there is a CASE_LABEL for VAL, its index is placed in IDX and true is
7727 returned.
7729 If there is no CASE_LABEL for VAL and there is one that is larger than VAL,
7730 it is placed in IDX and false is returned.
7732 If VAL is larger than any CASE_LABEL, n is placed on IDX and false is
7733 returned. */
7735 static bool
7736 find_case_label_index (gswitch *stmt, size_t start_idx, tree val, size_t *idx)
7738 size_t n = gimple_switch_num_labels (stmt);
7739 size_t low, high;
7741 /* Find case label for minimum of the value range or the next one.
7742 At each iteration we are searching in [low, high - 1]. */
7744 for (low = start_idx, high = n; high != low; )
7746 tree t;
7747 int cmp;
7748 /* Note that i != high, so we never ask for n. */
7749 size_t i = (high + low) / 2;
7750 t = gimple_switch_label (stmt, i);
7752 /* Cache the result of comparing CASE_LOW and val. */
7753 cmp = tree_int_cst_compare (CASE_LOW (t), val);
7755 if (cmp == 0)
7757 /* Ranges cannot be empty. */
7758 *idx = i;
7759 return true;
7761 else if (cmp > 0)
7762 high = i;
7763 else
7765 low = i + 1;
7766 if (CASE_HIGH (t) != NULL
7767 && tree_int_cst_compare (CASE_HIGH (t), val) >= 0)
7769 *idx = i;
7770 return true;
7775 *idx = high;
7776 return false;
7779 /* Searches the case label vector VEC for the range of CASE_LABELs that is used
7780 for values between MIN and MAX. The first index is placed in MIN_IDX. The
7781 last index is placed in MAX_IDX. If the range of CASE_LABELs is empty
7782 then MAX_IDX < MIN_IDX.
7783 Returns true if the default label is not needed. */
7785 static bool
7786 find_case_label_range (gswitch *stmt, tree min, tree max, size_t *min_idx,
7787 size_t *max_idx)
7789 size_t i, j;
7790 bool min_take_default = !find_case_label_index (stmt, 1, min, &i);
7791 bool max_take_default = !find_case_label_index (stmt, i, max, &j);
7793 if (i == j
7794 && min_take_default
7795 && max_take_default)
7797 /* Only the default case label reached.
7798 Return an empty range. */
7799 *min_idx = 1;
7800 *max_idx = 0;
7801 return false;
7803 else
7805 bool take_default = min_take_default || max_take_default;
7806 tree low, high;
7807 size_t k;
7809 if (max_take_default)
7810 j--;
7812 /* If the case label range is continuous, we do not need
7813 the default case label. Verify that. */
7814 high = CASE_LOW (gimple_switch_label (stmt, i));
7815 if (CASE_HIGH (gimple_switch_label (stmt, i)))
7816 high = CASE_HIGH (gimple_switch_label (stmt, i));
7817 for (k = i + 1; k <= j; ++k)
7819 low = CASE_LOW (gimple_switch_label (stmt, k));
7820 if (!integer_onep (int_const_binop (MINUS_EXPR, low, high)))
7822 take_default = true;
7823 break;
7825 high = low;
7826 if (CASE_HIGH (gimple_switch_label (stmt, k)))
7827 high = CASE_HIGH (gimple_switch_label (stmt, k));
7830 *min_idx = i;
7831 *max_idx = j;
7832 return !take_default;
7836 /* Searches the case label vector VEC for the ranges of CASE_LABELs that are
7837 used in range VR. The indices are placed in MIN_IDX1, MAX_IDX, MIN_IDX2 and
7838 MAX_IDX2. If the ranges of CASE_LABELs are empty then MAX_IDX1 < MIN_IDX1.
7839 Returns true if the default label is not needed. */
7841 static bool
7842 find_case_label_ranges (gswitch *stmt, value_range_t *vr, size_t *min_idx1,
7843 size_t *max_idx1, size_t *min_idx2,
7844 size_t *max_idx2)
7846 size_t i, j, k, l;
7847 unsigned int n = gimple_switch_num_labels (stmt);
7848 bool take_default;
7849 tree case_low, case_high;
7850 tree min = vr->min, max = vr->max;
7852 gcc_checking_assert (vr->type == VR_RANGE || vr->type == VR_ANTI_RANGE);
7854 take_default = !find_case_label_range (stmt, min, max, &i, &j);
7856 /* Set second range to emtpy. */
7857 *min_idx2 = 1;
7858 *max_idx2 = 0;
7860 if (vr->type == VR_RANGE)
7862 *min_idx1 = i;
7863 *max_idx1 = j;
7864 return !take_default;
7867 /* Set first range to all case labels. */
7868 *min_idx1 = 1;
7869 *max_idx1 = n - 1;
7871 if (i > j)
7872 return false;
7874 /* Make sure all the values of case labels [i , j] are contained in
7875 range [MIN, MAX]. */
7876 case_low = CASE_LOW (gimple_switch_label (stmt, i));
7877 case_high = CASE_HIGH (gimple_switch_label (stmt, j));
7878 if (tree_int_cst_compare (case_low, min) < 0)
7879 i += 1;
7880 if (case_high != NULL_TREE
7881 && tree_int_cst_compare (max, case_high) < 0)
7882 j -= 1;
7884 if (i > j)
7885 return false;
7887 /* If the range spans case labels [i, j], the corresponding anti-range spans
7888 the labels [1, i - 1] and [j + 1, n - 1]. */
7889 k = j + 1;
7890 l = n - 1;
7891 if (k > l)
7893 k = 1;
7894 l = 0;
7897 j = i - 1;
7898 i = 1;
7899 if (i > j)
7901 i = k;
7902 j = l;
7903 k = 1;
7904 l = 0;
7907 *min_idx1 = i;
7908 *max_idx1 = j;
7909 *min_idx2 = k;
7910 *max_idx2 = l;
7911 return false;
7914 /* Visit switch statement STMT. If we can determine which edge
7915 will be taken out of STMT's basic block, record it in
7916 *TAKEN_EDGE_P and return SSA_PROP_INTERESTING. Otherwise, return
7917 SSA_PROP_VARYING. */
7919 static enum ssa_prop_result
7920 vrp_visit_switch_stmt (gswitch *stmt, edge *taken_edge_p)
7922 tree op, val;
7923 value_range_t *vr;
7924 size_t i = 0, j = 0, k, l;
7925 bool take_default;
7927 *taken_edge_p = NULL;
7928 op = gimple_switch_index (stmt);
7929 if (TREE_CODE (op) != SSA_NAME)
7930 return SSA_PROP_VARYING;
7932 vr = get_value_range (op);
7933 if (dump_file && (dump_flags & TDF_DETAILS))
7935 fprintf (dump_file, "\nVisiting switch expression with operand ");
7936 print_generic_expr (dump_file, op, 0);
7937 fprintf (dump_file, " with known range ");
7938 dump_value_range (dump_file, vr);
7939 fprintf (dump_file, "\n");
7942 if ((vr->type != VR_RANGE
7943 && vr->type != VR_ANTI_RANGE)
7944 || symbolic_range_p (vr))
7945 return SSA_PROP_VARYING;
7947 /* Find the single edge that is taken from the switch expression. */
7948 take_default = !find_case_label_ranges (stmt, vr, &i, &j, &k, &l);
7950 /* Check if the range spans no CASE_LABEL. If so, we only reach the default
7951 label */
7952 if (j < i)
7954 gcc_assert (take_default);
7955 val = gimple_switch_default_label (stmt);
7957 else
7959 /* Check if labels with index i to j and maybe the default label
7960 are all reaching the same label. */
7962 val = gimple_switch_label (stmt, i);
7963 if (take_default
7964 && CASE_LABEL (gimple_switch_default_label (stmt))
7965 != CASE_LABEL (val))
7967 if (dump_file && (dump_flags & TDF_DETAILS))
7968 fprintf (dump_file, " not a single destination for this "
7969 "range\n");
7970 return SSA_PROP_VARYING;
7972 for (++i; i <= j; ++i)
7974 if (CASE_LABEL (gimple_switch_label (stmt, i)) != CASE_LABEL (val))
7976 if (dump_file && (dump_flags & TDF_DETAILS))
7977 fprintf (dump_file, " not a single destination for this "
7978 "range\n");
7979 return SSA_PROP_VARYING;
7982 for (; k <= l; ++k)
7984 if (CASE_LABEL (gimple_switch_label (stmt, k)) != CASE_LABEL (val))
7986 if (dump_file && (dump_flags & TDF_DETAILS))
7987 fprintf (dump_file, " not a single destination for this "
7988 "range\n");
7989 return SSA_PROP_VARYING;
7994 *taken_edge_p = find_edge (gimple_bb (stmt),
7995 label_to_block (CASE_LABEL (val)));
7997 if (dump_file && (dump_flags & TDF_DETAILS))
7999 fprintf (dump_file, " will take edge to ");
8000 print_generic_stmt (dump_file, CASE_LABEL (val), 0);
8003 return SSA_PROP_INTERESTING;
8007 /* Evaluate statement STMT. If the statement produces a useful range,
8008 return SSA_PROP_INTERESTING and record the SSA name with the
8009 interesting range into *OUTPUT_P.
8011 If STMT is a conditional branch and we can determine its truth
8012 value, the taken edge is recorded in *TAKEN_EDGE_P.
8014 If STMT produces a varying value, return SSA_PROP_VARYING. */
8016 static enum ssa_prop_result
8017 vrp_visit_stmt (gimple stmt, edge *taken_edge_p, tree *output_p)
8019 tree def;
8020 ssa_op_iter iter;
8022 if (dump_file && (dump_flags & TDF_DETAILS))
8024 fprintf (dump_file, "\nVisiting statement:\n");
8025 print_gimple_stmt (dump_file, stmt, 0, dump_flags);
8028 if (!stmt_interesting_for_vrp (stmt))
8029 gcc_assert (stmt_ends_bb_p (stmt));
8030 else if (is_gimple_assign (stmt) || is_gimple_call (stmt))
8031 return vrp_visit_assignment_or_call (stmt, output_p);
8032 else if (gimple_code (stmt) == GIMPLE_COND)
8033 return vrp_visit_cond_stmt (as_a <gcond *> (stmt), taken_edge_p);
8034 else if (gimple_code (stmt) == GIMPLE_SWITCH)
8035 return vrp_visit_switch_stmt (as_a <gswitch *> (stmt), taken_edge_p);
8037 /* All other statements produce nothing of interest for VRP, so mark
8038 their outputs varying and prevent further simulation. */
8039 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_DEF)
8040 set_value_range_to_varying (get_value_range (def));
8042 return SSA_PROP_VARYING;
8045 /* Union the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
8046 { VR1TYPE, VR0MIN, VR0MAX } and store the result
8047 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
8048 possible such range. The resulting range is not canonicalized. */
8050 static void
8051 union_ranges (enum value_range_type *vr0type,
8052 tree *vr0min, tree *vr0max,
8053 enum value_range_type vr1type,
8054 tree vr1min, tree vr1max)
8056 bool mineq = operand_equal_p (*vr0min, vr1min, 0);
8057 bool maxeq = operand_equal_p (*vr0max, vr1max, 0);
8059 /* [] is vr0, () is vr1 in the following classification comments. */
8060 if (mineq && maxeq)
8062 /* [( )] */
8063 if (*vr0type == vr1type)
8064 /* Nothing to do for equal ranges. */
8066 else if ((*vr0type == VR_RANGE
8067 && vr1type == VR_ANTI_RANGE)
8068 || (*vr0type == VR_ANTI_RANGE
8069 && vr1type == VR_RANGE))
8071 /* For anti-range with range union the result is varying. */
8072 goto give_up;
8074 else
8075 gcc_unreachable ();
8077 else if (operand_less_p (*vr0max, vr1min) == 1
8078 || operand_less_p (vr1max, *vr0min) == 1)
8080 /* [ ] ( ) or ( ) [ ]
8081 If the ranges have an empty intersection, result of the union
8082 operation is the anti-range or if both are anti-ranges
8083 it covers all. */
8084 if (*vr0type == VR_ANTI_RANGE
8085 && vr1type == VR_ANTI_RANGE)
8086 goto give_up;
8087 else if (*vr0type == VR_ANTI_RANGE
8088 && vr1type == VR_RANGE)
8090 else if (*vr0type == VR_RANGE
8091 && vr1type == VR_ANTI_RANGE)
8093 *vr0type = vr1type;
8094 *vr0min = vr1min;
8095 *vr0max = vr1max;
8097 else if (*vr0type == VR_RANGE
8098 && vr1type == VR_RANGE)
8100 /* The result is the convex hull of both ranges. */
8101 if (operand_less_p (*vr0max, vr1min) == 1)
8103 /* If the result can be an anti-range, create one. */
8104 if (TREE_CODE (*vr0max) == INTEGER_CST
8105 && TREE_CODE (vr1min) == INTEGER_CST
8106 && vrp_val_is_min (*vr0min)
8107 && vrp_val_is_max (vr1max))
8109 tree min = int_const_binop (PLUS_EXPR,
8110 *vr0max,
8111 build_int_cst (TREE_TYPE (*vr0max), 1));
8112 tree max = int_const_binop (MINUS_EXPR,
8113 vr1min,
8114 build_int_cst (TREE_TYPE (vr1min), 1));
8115 if (!operand_less_p (max, min))
8117 *vr0type = VR_ANTI_RANGE;
8118 *vr0min = min;
8119 *vr0max = max;
8121 else
8122 *vr0max = vr1max;
8124 else
8125 *vr0max = vr1max;
8127 else
8129 /* If the result can be an anti-range, create one. */
8130 if (TREE_CODE (vr1max) == INTEGER_CST
8131 && TREE_CODE (*vr0min) == INTEGER_CST
8132 && vrp_val_is_min (vr1min)
8133 && vrp_val_is_max (*vr0max))
8135 tree min = int_const_binop (PLUS_EXPR,
8136 vr1max,
8137 build_int_cst (TREE_TYPE (vr1max), 1));
8138 tree max = int_const_binop (MINUS_EXPR,
8139 *vr0min,
8140 build_int_cst (TREE_TYPE (*vr0min), 1));
8141 if (!operand_less_p (max, min))
8143 *vr0type = VR_ANTI_RANGE;
8144 *vr0min = min;
8145 *vr0max = max;
8147 else
8148 *vr0min = vr1min;
8150 else
8151 *vr0min = vr1min;
8154 else
8155 gcc_unreachable ();
8157 else if ((maxeq || operand_less_p (vr1max, *vr0max) == 1)
8158 && (mineq || operand_less_p (*vr0min, vr1min) == 1))
8160 /* [ ( ) ] or [( ) ] or [ ( )] */
8161 if (*vr0type == VR_RANGE
8162 && vr1type == VR_RANGE)
8164 else if (*vr0type == VR_ANTI_RANGE
8165 && vr1type == VR_ANTI_RANGE)
8167 *vr0type = vr1type;
8168 *vr0min = vr1min;
8169 *vr0max = vr1max;
8171 else if (*vr0type == VR_ANTI_RANGE
8172 && vr1type == VR_RANGE)
8174 /* Arbitrarily choose the right or left gap. */
8175 if (!mineq && TREE_CODE (vr1min) == INTEGER_CST)
8176 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
8177 build_int_cst (TREE_TYPE (vr1min), 1));
8178 else if (!maxeq && TREE_CODE (vr1max) == INTEGER_CST)
8179 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
8180 build_int_cst (TREE_TYPE (vr1max), 1));
8181 else
8182 goto give_up;
8184 else if (*vr0type == VR_RANGE
8185 && vr1type == VR_ANTI_RANGE)
8186 /* The result covers everything. */
8187 goto give_up;
8188 else
8189 gcc_unreachable ();
8191 else if ((maxeq || operand_less_p (*vr0max, vr1max) == 1)
8192 && (mineq || operand_less_p (vr1min, *vr0min) == 1))
8194 /* ( [ ] ) or ([ ] ) or ( [ ]) */
8195 if (*vr0type == VR_RANGE
8196 && vr1type == VR_RANGE)
8198 *vr0type = vr1type;
8199 *vr0min = vr1min;
8200 *vr0max = vr1max;
8202 else if (*vr0type == VR_ANTI_RANGE
8203 && vr1type == VR_ANTI_RANGE)
8205 else if (*vr0type == VR_RANGE
8206 && vr1type == VR_ANTI_RANGE)
8208 *vr0type = VR_ANTI_RANGE;
8209 if (!mineq && TREE_CODE (*vr0min) == INTEGER_CST)
8211 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
8212 build_int_cst (TREE_TYPE (*vr0min), 1));
8213 *vr0min = vr1min;
8215 else if (!maxeq && TREE_CODE (*vr0max) == INTEGER_CST)
8217 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
8218 build_int_cst (TREE_TYPE (*vr0max), 1));
8219 *vr0max = vr1max;
8221 else
8222 goto give_up;
8224 else if (*vr0type == VR_ANTI_RANGE
8225 && vr1type == VR_RANGE)
8226 /* The result covers everything. */
8227 goto give_up;
8228 else
8229 gcc_unreachable ();
8231 else if ((operand_less_p (vr1min, *vr0max) == 1
8232 || operand_equal_p (vr1min, *vr0max, 0))
8233 && operand_less_p (*vr0min, vr1min) == 1
8234 && operand_less_p (*vr0max, vr1max) == 1)
8236 /* [ ( ] ) or [ ]( ) */
8237 if (*vr0type == VR_RANGE
8238 && vr1type == VR_RANGE)
8239 *vr0max = vr1max;
8240 else if (*vr0type == VR_ANTI_RANGE
8241 && vr1type == VR_ANTI_RANGE)
8242 *vr0min = vr1min;
8243 else if (*vr0type == VR_ANTI_RANGE
8244 && vr1type == VR_RANGE)
8246 if (TREE_CODE (vr1min) == INTEGER_CST)
8247 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
8248 build_int_cst (TREE_TYPE (vr1min), 1));
8249 else
8250 goto give_up;
8252 else if (*vr0type == VR_RANGE
8253 && vr1type == VR_ANTI_RANGE)
8255 if (TREE_CODE (*vr0max) == INTEGER_CST)
8257 *vr0type = vr1type;
8258 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
8259 build_int_cst (TREE_TYPE (*vr0max), 1));
8260 *vr0max = vr1max;
8262 else
8263 goto give_up;
8265 else
8266 gcc_unreachable ();
8268 else if ((operand_less_p (*vr0min, vr1max) == 1
8269 || operand_equal_p (*vr0min, vr1max, 0))
8270 && operand_less_p (vr1min, *vr0min) == 1
8271 && operand_less_p (vr1max, *vr0max) == 1)
8273 /* ( [ ) ] or ( )[ ] */
8274 if (*vr0type == VR_RANGE
8275 && vr1type == VR_RANGE)
8276 *vr0min = vr1min;
8277 else if (*vr0type == VR_ANTI_RANGE
8278 && vr1type == VR_ANTI_RANGE)
8279 *vr0max = vr1max;
8280 else if (*vr0type == VR_ANTI_RANGE
8281 && vr1type == VR_RANGE)
8283 if (TREE_CODE (vr1max) == INTEGER_CST)
8284 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
8285 build_int_cst (TREE_TYPE (vr1max), 1));
8286 else
8287 goto give_up;
8289 else if (*vr0type == VR_RANGE
8290 && vr1type == VR_ANTI_RANGE)
8292 if (TREE_CODE (*vr0min) == INTEGER_CST)
8294 *vr0type = vr1type;
8295 *vr0min = vr1min;
8296 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
8297 build_int_cst (TREE_TYPE (*vr0min), 1));
8299 else
8300 goto give_up;
8302 else
8303 gcc_unreachable ();
8305 else
8306 goto give_up;
8308 return;
8310 give_up:
8311 *vr0type = VR_VARYING;
8312 *vr0min = NULL_TREE;
8313 *vr0max = NULL_TREE;
8316 /* Intersect the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
8317 { VR1TYPE, VR0MIN, VR0MAX } and store the result
8318 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
8319 possible such range. The resulting range is not canonicalized. */
8321 static void
8322 intersect_ranges (enum value_range_type *vr0type,
8323 tree *vr0min, tree *vr0max,
8324 enum value_range_type vr1type,
8325 tree vr1min, tree vr1max)
8327 bool mineq = operand_equal_p (*vr0min, vr1min, 0);
8328 bool maxeq = operand_equal_p (*vr0max, vr1max, 0);
8330 /* [] is vr0, () is vr1 in the following classification comments. */
8331 if (mineq && maxeq)
8333 /* [( )] */
8334 if (*vr0type == vr1type)
8335 /* Nothing to do for equal ranges. */
8337 else if ((*vr0type == VR_RANGE
8338 && vr1type == VR_ANTI_RANGE)
8339 || (*vr0type == VR_ANTI_RANGE
8340 && vr1type == VR_RANGE))
8342 /* For anti-range with range intersection the result is empty. */
8343 *vr0type = VR_UNDEFINED;
8344 *vr0min = NULL_TREE;
8345 *vr0max = NULL_TREE;
8347 else
8348 gcc_unreachable ();
8350 else if (operand_less_p (*vr0max, vr1min) == 1
8351 || operand_less_p (vr1max, *vr0min) == 1)
8353 /* [ ] ( ) or ( ) [ ]
8354 If the ranges have an empty intersection, the result of the
8355 intersect operation is the range for intersecting an
8356 anti-range with a range or empty when intersecting two ranges. */
8357 if (*vr0type == VR_RANGE
8358 && vr1type == VR_ANTI_RANGE)
8360 else if (*vr0type == VR_ANTI_RANGE
8361 && vr1type == VR_RANGE)
8363 *vr0type = vr1type;
8364 *vr0min = vr1min;
8365 *vr0max = vr1max;
8367 else if (*vr0type == VR_RANGE
8368 && vr1type == VR_RANGE)
8370 *vr0type = VR_UNDEFINED;
8371 *vr0min = NULL_TREE;
8372 *vr0max = NULL_TREE;
8374 else if (*vr0type == VR_ANTI_RANGE
8375 && vr1type == VR_ANTI_RANGE)
8377 /* If the anti-ranges are adjacent to each other merge them. */
8378 if (TREE_CODE (*vr0max) == INTEGER_CST
8379 && TREE_CODE (vr1min) == INTEGER_CST
8380 && operand_less_p (*vr0max, vr1min) == 1
8381 && integer_onep (int_const_binop (MINUS_EXPR,
8382 vr1min, *vr0max)))
8383 *vr0max = vr1max;
8384 else if (TREE_CODE (vr1max) == INTEGER_CST
8385 && TREE_CODE (*vr0min) == INTEGER_CST
8386 && operand_less_p (vr1max, *vr0min) == 1
8387 && integer_onep (int_const_binop (MINUS_EXPR,
8388 *vr0min, vr1max)))
8389 *vr0min = vr1min;
8390 /* Else arbitrarily take VR0. */
8393 else if ((maxeq || operand_less_p (vr1max, *vr0max) == 1)
8394 && (mineq || operand_less_p (*vr0min, vr1min) == 1))
8396 /* [ ( ) ] or [( ) ] or [ ( )] */
8397 if (*vr0type == VR_RANGE
8398 && vr1type == VR_RANGE)
8400 /* If both are ranges the result is the inner one. */
8401 *vr0type = vr1type;
8402 *vr0min = vr1min;
8403 *vr0max = vr1max;
8405 else if (*vr0type == VR_RANGE
8406 && vr1type == VR_ANTI_RANGE)
8408 /* Choose the right gap if the left one is empty. */
8409 if (mineq)
8411 if (TREE_CODE (vr1max) == INTEGER_CST)
8412 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
8413 build_int_cst (TREE_TYPE (vr1max), 1));
8414 else
8415 *vr0min = vr1max;
8417 /* Choose the left gap if the right one is empty. */
8418 else if (maxeq)
8420 if (TREE_CODE (vr1min) == INTEGER_CST)
8421 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
8422 build_int_cst (TREE_TYPE (vr1min), 1));
8423 else
8424 *vr0max = vr1min;
8426 /* Choose the anti-range if the range is effectively varying. */
8427 else if (vrp_val_is_min (*vr0min)
8428 && vrp_val_is_max (*vr0max))
8430 *vr0type = vr1type;
8431 *vr0min = vr1min;
8432 *vr0max = vr1max;
8434 /* Else choose the range. */
8436 else if (*vr0type == VR_ANTI_RANGE
8437 && vr1type == VR_ANTI_RANGE)
8438 /* If both are anti-ranges the result is the outer one. */
8440 else if (*vr0type == VR_ANTI_RANGE
8441 && vr1type == VR_RANGE)
8443 /* The intersection is empty. */
8444 *vr0type = VR_UNDEFINED;
8445 *vr0min = NULL_TREE;
8446 *vr0max = NULL_TREE;
8448 else
8449 gcc_unreachable ();
8451 else if ((maxeq || operand_less_p (*vr0max, vr1max) == 1)
8452 && (mineq || operand_less_p (vr1min, *vr0min) == 1))
8454 /* ( [ ] ) or ([ ] ) or ( [ ]) */
8455 if (*vr0type == VR_RANGE
8456 && vr1type == VR_RANGE)
8457 /* Choose the inner range. */
8459 else if (*vr0type == VR_ANTI_RANGE
8460 && vr1type == VR_RANGE)
8462 /* Choose the right gap if the left is empty. */
8463 if (mineq)
8465 *vr0type = VR_RANGE;
8466 if (TREE_CODE (*vr0max) == INTEGER_CST)
8467 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
8468 build_int_cst (TREE_TYPE (*vr0max), 1));
8469 else
8470 *vr0min = *vr0max;
8471 *vr0max = vr1max;
8473 /* Choose the left gap if the right is empty. */
8474 else if (maxeq)
8476 *vr0type = VR_RANGE;
8477 if (TREE_CODE (*vr0min) == INTEGER_CST)
8478 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
8479 build_int_cst (TREE_TYPE (*vr0min), 1));
8480 else
8481 *vr0max = *vr0min;
8482 *vr0min = vr1min;
8484 /* Choose the anti-range if the range is effectively varying. */
8485 else if (vrp_val_is_min (vr1min)
8486 && vrp_val_is_max (vr1max))
8488 /* Else choose the range. */
8489 else
8491 *vr0type = vr1type;
8492 *vr0min = vr1min;
8493 *vr0max = vr1max;
8496 else if (*vr0type == VR_ANTI_RANGE
8497 && vr1type == VR_ANTI_RANGE)
8499 /* If both are anti-ranges the result is the outer one. */
8500 *vr0type = vr1type;
8501 *vr0min = vr1min;
8502 *vr0max = vr1max;
8504 else if (vr1type == VR_ANTI_RANGE
8505 && *vr0type == VR_RANGE)
8507 /* The intersection is empty. */
8508 *vr0type = VR_UNDEFINED;
8509 *vr0min = NULL_TREE;
8510 *vr0max = NULL_TREE;
8512 else
8513 gcc_unreachable ();
8515 else if ((operand_less_p (vr1min, *vr0max) == 1
8516 || operand_equal_p (vr1min, *vr0max, 0))
8517 && operand_less_p (*vr0min, vr1min) == 1)
8519 /* [ ( ] ) or [ ]( ) */
8520 if (*vr0type == VR_ANTI_RANGE
8521 && vr1type == VR_ANTI_RANGE)
8522 *vr0max = vr1max;
8523 else if (*vr0type == VR_RANGE
8524 && vr1type == VR_RANGE)
8525 *vr0min = vr1min;
8526 else if (*vr0type == VR_RANGE
8527 && vr1type == VR_ANTI_RANGE)
8529 if (TREE_CODE (vr1min) == INTEGER_CST)
8530 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
8531 build_int_cst (TREE_TYPE (vr1min), 1));
8532 else
8533 *vr0max = vr1min;
8535 else if (*vr0type == VR_ANTI_RANGE
8536 && vr1type == VR_RANGE)
8538 *vr0type = VR_RANGE;
8539 if (TREE_CODE (*vr0max) == INTEGER_CST)
8540 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
8541 build_int_cst (TREE_TYPE (*vr0max), 1));
8542 else
8543 *vr0min = *vr0max;
8544 *vr0max = vr1max;
8546 else
8547 gcc_unreachable ();
8549 else if ((operand_less_p (*vr0min, vr1max) == 1
8550 || operand_equal_p (*vr0min, vr1max, 0))
8551 && operand_less_p (vr1min, *vr0min) == 1)
8553 /* ( [ ) ] or ( )[ ] */
8554 if (*vr0type == VR_ANTI_RANGE
8555 && vr1type == VR_ANTI_RANGE)
8556 *vr0min = vr1min;
8557 else if (*vr0type == VR_RANGE
8558 && vr1type == VR_RANGE)
8559 *vr0max = vr1max;
8560 else if (*vr0type == VR_RANGE
8561 && vr1type == VR_ANTI_RANGE)
8563 if (TREE_CODE (vr1max) == INTEGER_CST)
8564 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
8565 build_int_cst (TREE_TYPE (vr1max), 1));
8566 else
8567 *vr0min = vr1max;
8569 else if (*vr0type == VR_ANTI_RANGE
8570 && vr1type == VR_RANGE)
8572 *vr0type = VR_RANGE;
8573 if (TREE_CODE (*vr0min) == INTEGER_CST)
8574 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
8575 build_int_cst (TREE_TYPE (*vr0min), 1));
8576 else
8577 *vr0max = *vr0min;
8578 *vr0min = vr1min;
8580 else
8581 gcc_unreachable ();
8584 /* As a fallback simply use { *VRTYPE, *VR0MIN, *VR0MAX } as
8585 result for the intersection. That's always a conservative
8586 correct estimate. */
8588 return;
8592 /* Intersect the two value-ranges *VR0 and *VR1 and store the result
8593 in *VR0. This may not be the smallest possible such range. */
8595 static void
8596 vrp_intersect_ranges_1 (value_range_t *vr0, value_range_t *vr1)
8598 value_range_t saved;
8600 /* If either range is VR_VARYING the other one wins. */
8601 if (vr1->type == VR_VARYING)
8602 return;
8603 if (vr0->type == VR_VARYING)
8605 copy_value_range (vr0, vr1);
8606 return;
8609 /* When either range is VR_UNDEFINED the resulting range is
8610 VR_UNDEFINED, too. */
8611 if (vr0->type == VR_UNDEFINED)
8612 return;
8613 if (vr1->type == VR_UNDEFINED)
8615 set_value_range_to_undefined (vr0);
8616 return;
8619 /* Save the original vr0 so we can return it as conservative intersection
8620 result when our worker turns things to varying. */
8621 saved = *vr0;
8622 intersect_ranges (&vr0->type, &vr0->min, &vr0->max,
8623 vr1->type, vr1->min, vr1->max);
8624 /* Make sure to canonicalize the result though as the inversion of a
8625 VR_RANGE can still be a VR_RANGE. */
8626 set_and_canonicalize_value_range (vr0, vr0->type,
8627 vr0->min, vr0->max, vr0->equiv);
8628 /* If that failed, use the saved original VR0. */
8629 if (vr0->type == VR_VARYING)
8631 *vr0 = saved;
8632 return;
8634 /* If the result is VR_UNDEFINED there is no need to mess with
8635 the equivalencies. */
8636 if (vr0->type == VR_UNDEFINED)
8637 return;
8639 /* The resulting set of equivalences for range intersection is the union of
8640 the two sets. */
8641 if (vr0->equiv && vr1->equiv && vr0->equiv != vr1->equiv)
8642 bitmap_ior_into (vr0->equiv, vr1->equiv);
8643 else if (vr1->equiv && !vr0->equiv)
8644 bitmap_copy (vr0->equiv, vr1->equiv);
8647 static void
8648 vrp_intersect_ranges (value_range_t *vr0, value_range_t *vr1)
8650 if (dump_file && (dump_flags & TDF_DETAILS))
8652 fprintf (dump_file, "Intersecting\n ");
8653 dump_value_range (dump_file, vr0);
8654 fprintf (dump_file, "\nand\n ");
8655 dump_value_range (dump_file, vr1);
8656 fprintf (dump_file, "\n");
8658 vrp_intersect_ranges_1 (vr0, vr1);
8659 if (dump_file && (dump_flags & TDF_DETAILS))
8661 fprintf (dump_file, "to\n ");
8662 dump_value_range (dump_file, vr0);
8663 fprintf (dump_file, "\n");
8667 /* Meet operation for value ranges. Given two value ranges VR0 and
8668 VR1, store in VR0 a range that contains both VR0 and VR1. This
8669 may not be the smallest possible such range. */
8671 static void
8672 vrp_meet_1 (value_range_t *vr0, value_range_t *vr1)
8674 value_range_t saved;
8676 if (vr0->type == VR_UNDEFINED)
8678 set_value_range (vr0, vr1->type, vr1->min, vr1->max, vr1->equiv);
8679 return;
8682 if (vr1->type == VR_UNDEFINED)
8684 /* VR0 already has the resulting range. */
8685 return;
8688 if (vr0->type == VR_VARYING)
8690 /* Nothing to do. VR0 already has the resulting range. */
8691 return;
8694 if (vr1->type == VR_VARYING)
8696 set_value_range_to_varying (vr0);
8697 return;
8700 saved = *vr0;
8701 union_ranges (&vr0->type, &vr0->min, &vr0->max,
8702 vr1->type, vr1->min, vr1->max);
8703 if (vr0->type == VR_VARYING)
8705 /* Failed to find an efficient meet. Before giving up and setting
8706 the result to VARYING, see if we can at least derive a useful
8707 anti-range. FIXME, all this nonsense about distinguishing
8708 anti-ranges from ranges is necessary because of the odd
8709 semantics of range_includes_zero_p and friends. */
8710 if (((saved.type == VR_RANGE
8711 && range_includes_zero_p (saved.min, saved.max) == 0)
8712 || (saved.type == VR_ANTI_RANGE
8713 && range_includes_zero_p (saved.min, saved.max) == 1))
8714 && ((vr1->type == VR_RANGE
8715 && range_includes_zero_p (vr1->min, vr1->max) == 0)
8716 || (vr1->type == VR_ANTI_RANGE
8717 && range_includes_zero_p (vr1->min, vr1->max) == 1)))
8719 set_value_range_to_nonnull (vr0, TREE_TYPE (saved.min));
8721 /* Since this meet operation did not result from the meeting of
8722 two equivalent names, VR0 cannot have any equivalences. */
8723 if (vr0->equiv)
8724 bitmap_clear (vr0->equiv);
8725 return;
8728 set_value_range_to_varying (vr0);
8729 return;
8731 set_and_canonicalize_value_range (vr0, vr0->type, vr0->min, vr0->max,
8732 vr0->equiv);
8733 if (vr0->type == VR_VARYING)
8734 return;
8736 /* The resulting set of equivalences is always the intersection of
8737 the two sets. */
8738 if (vr0->equiv && vr1->equiv && vr0->equiv != vr1->equiv)
8739 bitmap_and_into (vr0->equiv, vr1->equiv);
8740 else if (vr0->equiv && !vr1->equiv)
8741 bitmap_clear (vr0->equiv);
8744 static void
8745 vrp_meet (value_range_t *vr0, value_range_t *vr1)
8747 if (dump_file && (dump_flags & TDF_DETAILS))
8749 fprintf (dump_file, "Meeting\n ");
8750 dump_value_range (dump_file, vr0);
8751 fprintf (dump_file, "\nand\n ");
8752 dump_value_range (dump_file, vr1);
8753 fprintf (dump_file, "\n");
8755 vrp_meet_1 (vr0, vr1);
8756 if (dump_file && (dump_flags & TDF_DETAILS))
8758 fprintf (dump_file, "to\n ");
8759 dump_value_range (dump_file, vr0);
8760 fprintf (dump_file, "\n");
8765 /* Visit all arguments for PHI node PHI that flow through executable
8766 edges. If a valid value range can be derived from all the incoming
8767 value ranges, set a new range for the LHS of PHI. */
8769 static enum ssa_prop_result
8770 vrp_visit_phi_node (gphi *phi)
8772 size_t i;
8773 tree lhs = PHI_RESULT (phi);
8774 value_range_t *lhs_vr = get_value_range (lhs);
8775 value_range_t vr_result = VR_INITIALIZER;
8776 bool first = true;
8777 int edges, old_edges;
8778 struct loop *l;
8780 if (dump_file && (dump_flags & TDF_DETAILS))
8782 fprintf (dump_file, "\nVisiting PHI node: ");
8783 print_gimple_stmt (dump_file, phi, 0, dump_flags);
8786 edges = 0;
8787 for (i = 0; i < gimple_phi_num_args (phi); i++)
8789 edge e = gimple_phi_arg_edge (phi, i);
8791 if (dump_file && (dump_flags & TDF_DETAILS))
8793 fprintf (dump_file,
8794 " Argument #%d (%d -> %d %sexecutable)\n",
8795 (int) i, e->src->index, e->dest->index,
8796 (e->flags & EDGE_EXECUTABLE) ? "" : "not ");
8799 if (e->flags & EDGE_EXECUTABLE)
8801 tree arg = PHI_ARG_DEF (phi, i);
8802 value_range_t vr_arg;
8804 ++edges;
8806 if (TREE_CODE (arg) == SSA_NAME)
8808 vr_arg = *(get_value_range (arg));
8809 /* Do not allow equivalences or symbolic ranges to leak in from
8810 backedges. That creates invalid equivalencies.
8811 See PR53465 and PR54767. */
8812 if (e->flags & EDGE_DFS_BACK)
8814 if (vr_arg.type == VR_RANGE
8815 || vr_arg.type == VR_ANTI_RANGE)
8817 vr_arg.equiv = NULL;
8818 if (symbolic_range_p (&vr_arg))
8820 vr_arg.type = VR_VARYING;
8821 vr_arg.min = NULL_TREE;
8822 vr_arg.max = NULL_TREE;
8826 else
8828 /* If the non-backedge arguments range is VR_VARYING then
8829 we can still try recording a simple equivalence. */
8830 if (vr_arg.type == VR_VARYING)
8832 vr_arg.type = VR_RANGE;
8833 vr_arg.min = arg;
8834 vr_arg.max = arg;
8835 vr_arg.equiv = NULL;
8839 else
8841 if (TREE_OVERFLOW_P (arg))
8842 arg = drop_tree_overflow (arg);
8844 vr_arg.type = VR_RANGE;
8845 vr_arg.min = arg;
8846 vr_arg.max = arg;
8847 vr_arg.equiv = NULL;
8850 if (dump_file && (dump_flags & TDF_DETAILS))
8852 fprintf (dump_file, "\t");
8853 print_generic_expr (dump_file, arg, dump_flags);
8854 fprintf (dump_file, ": ");
8855 dump_value_range (dump_file, &vr_arg);
8856 fprintf (dump_file, "\n");
8859 if (first)
8860 copy_value_range (&vr_result, &vr_arg);
8861 else
8862 vrp_meet (&vr_result, &vr_arg);
8863 first = false;
8865 if (vr_result.type == VR_VARYING)
8866 break;
8870 if (vr_result.type == VR_VARYING)
8871 goto varying;
8872 else if (vr_result.type == VR_UNDEFINED)
8873 goto update_range;
8875 old_edges = vr_phi_edge_counts[SSA_NAME_VERSION (lhs)];
8876 vr_phi_edge_counts[SSA_NAME_VERSION (lhs)] = edges;
8878 /* To prevent infinite iterations in the algorithm, derive ranges
8879 when the new value is slightly bigger or smaller than the
8880 previous one. We don't do this if we have seen a new executable
8881 edge; this helps us avoid an overflow infinity for conditionals
8882 which are not in a loop. If the old value-range was VR_UNDEFINED
8883 use the updated range and iterate one more time. */
8884 if (edges > 0
8885 && gimple_phi_num_args (phi) > 1
8886 && edges == old_edges
8887 && lhs_vr->type != VR_UNDEFINED)
8889 /* Compare old and new ranges, fall back to varying if the
8890 values are not comparable. */
8891 int cmp_min = compare_values (lhs_vr->min, vr_result.min);
8892 if (cmp_min == -2)
8893 goto varying;
8894 int cmp_max = compare_values (lhs_vr->max, vr_result.max);
8895 if (cmp_max == -2)
8896 goto varying;
8898 /* For non VR_RANGE or for pointers fall back to varying if
8899 the range changed. */
8900 if ((lhs_vr->type != VR_RANGE || vr_result.type != VR_RANGE
8901 || POINTER_TYPE_P (TREE_TYPE (lhs)))
8902 && (cmp_min != 0 || cmp_max != 0))
8903 goto varying;
8905 /* If the new minimum is larger than than the previous one
8906 retain the old value. If the new minimum value is smaller
8907 than the previous one and not -INF go all the way to -INF + 1.
8908 In the first case, to avoid infinite bouncing between different
8909 minimums, and in the other case to avoid iterating millions of
8910 times to reach -INF. Going to -INF + 1 also lets the following
8911 iteration compute whether there will be any overflow, at the
8912 expense of one additional iteration. */
8913 if (cmp_min < 0)
8914 vr_result.min = lhs_vr->min;
8915 else if (cmp_min > 0
8916 && !vrp_val_is_min (vr_result.min))
8917 vr_result.min
8918 = int_const_binop (PLUS_EXPR,
8919 vrp_val_min (TREE_TYPE (vr_result.min)),
8920 build_int_cst (TREE_TYPE (vr_result.min), 1));
8922 /* Similarly for the maximum value. */
8923 if (cmp_max > 0)
8924 vr_result.max = lhs_vr->max;
8925 else if (cmp_max < 0
8926 && !vrp_val_is_max (vr_result.max))
8927 vr_result.max
8928 = int_const_binop (MINUS_EXPR,
8929 vrp_val_max (TREE_TYPE (vr_result.min)),
8930 build_int_cst (TREE_TYPE (vr_result.min), 1));
8932 /* If we dropped either bound to +-INF then if this is a loop
8933 PHI node SCEV may known more about its value-range. */
8934 if ((cmp_min > 0 || cmp_min < 0
8935 || cmp_max < 0 || cmp_max > 0)
8936 && (l = loop_containing_stmt (phi))
8937 && l->header == gimple_bb (phi))
8938 adjust_range_with_scev (&vr_result, l, phi, lhs);
8940 /* If we will end up with a (-INF, +INF) range, set it to
8941 VARYING. Same if the previous max value was invalid for
8942 the type and we end up with vr_result.min > vr_result.max. */
8943 if ((vrp_val_is_max (vr_result.max)
8944 && vrp_val_is_min (vr_result.min))
8945 || compare_values (vr_result.min,
8946 vr_result.max) > 0)
8947 goto varying;
8950 /* If the new range is different than the previous value, keep
8951 iterating. */
8952 update_range:
8953 if (update_value_range (lhs, &vr_result))
8955 if (dump_file && (dump_flags & TDF_DETAILS))
8957 fprintf (dump_file, "Found new range for ");
8958 print_generic_expr (dump_file, lhs, 0);
8959 fprintf (dump_file, ": ");
8960 dump_value_range (dump_file, &vr_result);
8961 fprintf (dump_file, "\n");
8964 if (vr_result.type == VR_VARYING)
8965 return SSA_PROP_VARYING;
8967 return SSA_PROP_INTERESTING;
8970 /* Nothing changed, don't add outgoing edges. */
8971 return SSA_PROP_NOT_INTERESTING;
8973 /* No match found. Set the LHS to VARYING. */
8974 varying:
8975 set_value_range_to_varying (lhs_vr);
8976 return SSA_PROP_VARYING;
8979 /* Simplify boolean operations if the source is known
8980 to be already a boolean. */
8981 static bool
8982 simplify_truth_ops_using_ranges (gimple_stmt_iterator *gsi, gimple stmt)
8984 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
8985 tree lhs, op0, op1;
8986 bool need_conversion;
8988 /* We handle only !=/== case here. */
8989 gcc_assert (rhs_code == EQ_EXPR || rhs_code == NE_EXPR);
8991 op0 = gimple_assign_rhs1 (stmt);
8992 if (!op_with_boolean_value_range_p (op0))
8993 return false;
8995 op1 = gimple_assign_rhs2 (stmt);
8996 if (!op_with_boolean_value_range_p (op1))
8997 return false;
8999 /* Reduce number of cases to handle to NE_EXPR. As there is no
9000 BIT_XNOR_EXPR we cannot replace A == B with a single statement. */
9001 if (rhs_code == EQ_EXPR)
9003 if (TREE_CODE (op1) == INTEGER_CST)
9004 op1 = int_const_binop (BIT_XOR_EXPR, op1,
9005 build_int_cst (TREE_TYPE (op1), 1));
9006 else
9007 return false;
9010 lhs = gimple_assign_lhs (stmt);
9011 need_conversion
9012 = !useless_type_conversion_p (TREE_TYPE (lhs), TREE_TYPE (op0));
9014 /* Make sure to not sign-extend a 1-bit 1 when converting the result. */
9015 if (need_conversion
9016 && !TYPE_UNSIGNED (TREE_TYPE (op0))
9017 && TYPE_PRECISION (TREE_TYPE (op0)) == 1
9018 && TYPE_PRECISION (TREE_TYPE (lhs)) > 1)
9019 return false;
9021 /* For A != 0 we can substitute A itself. */
9022 if (integer_zerop (op1))
9023 gimple_assign_set_rhs_with_ops (gsi,
9024 need_conversion
9025 ? NOP_EXPR : TREE_CODE (op0), op0);
9026 /* For A != B we substitute A ^ B. Either with conversion. */
9027 else if (need_conversion)
9029 tree tem = make_ssa_name (TREE_TYPE (op0));
9030 gassign *newop
9031 = gimple_build_assign (tem, BIT_XOR_EXPR, op0, op1);
9032 gsi_insert_before (gsi, newop, GSI_SAME_STMT);
9033 gimple_assign_set_rhs_with_ops (gsi, NOP_EXPR, tem);
9035 /* Or without. */
9036 else
9037 gimple_assign_set_rhs_with_ops (gsi, BIT_XOR_EXPR, op0, op1);
9038 update_stmt (gsi_stmt (*gsi));
9040 return true;
9043 /* Simplify a division or modulo operator to a right shift or
9044 bitwise and if the first operand is unsigned or is greater
9045 than zero and the second operand is an exact power of two.
9046 For TRUNC_MOD_EXPR op0 % op1 with constant op1, optimize it
9047 into just op0 if op0's range is known to be a subset of
9048 [-op1 + 1, op1 - 1] for signed and [0, op1 - 1] for unsigned
9049 modulo. */
9051 static bool
9052 simplify_div_or_mod_using_ranges (gimple stmt)
9054 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
9055 tree val = NULL;
9056 tree op0 = gimple_assign_rhs1 (stmt);
9057 tree op1 = gimple_assign_rhs2 (stmt);
9058 value_range_t *vr = get_value_range (op0);
9060 if (rhs_code == TRUNC_MOD_EXPR
9061 && TREE_CODE (op1) == INTEGER_CST
9062 && tree_int_cst_sgn (op1) == 1
9063 && range_int_cst_p (vr)
9064 && tree_int_cst_lt (vr->max, op1))
9066 if (TYPE_UNSIGNED (TREE_TYPE (op0))
9067 || tree_int_cst_sgn (vr->min) >= 0
9068 || tree_int_cst_lt (fold_unary (NEGATE_EXPR, TREE_TYPE (op1), op1),
9069 vr->min))
9071 /* If op0 already has the range op0 % op1 has,
9072 then TRUNC_MOD_EXPR won't change anything. */
9073 gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
9074 gimple_assign_set_rhs_from_tree (&gsi, op0);
9075 update_stmt (stmt);
9076 return true;
9080 if (!integer_pow2p (op1))
9081 return false;
9083 if (TYPE_UNSIGNED (TREE_TYPE (op0)))
9085 val = integer_one_node;
9087 else
9089 bool sop = false;
9091 val = compare_range_with_value (GE_EXPR, vr, integer_zero_node, &sop);
9093 if (val
9094 && sop
9095 && integer_onep (val)
9096 && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC))
9098 location_t location;
9100 if (!gimple_has_location (stmt))
9101 location = input_location;
9102 else
9103 location = gimple_location (stmt);
9104 warning_at (location, OPT_Wstrict_overflow,
9105 "assuming signed overflow does not occur when "
9106 "simplifying %</%> or %<%%%> to %<>>%> or %<&%>");
9110 if (val && integer_onep (val))
9112 tree t;
9114 if (rhs_code == TRUNC_DIV_EXPR)
9116 t = build_int_cst (integer_type_node, tree_log2 (op1));
9117 gimple_assign_set_rhs_code (stmt, RSHIFT_EXPR);
9118 gimple_assign_set_rhs1 (stmt, op0);
9119 gimple_assign_set_rhs2 (stmt, t);
9121 else
9123 t = build_int_cst (TREE_TYPE (op1), 1);
9124 t = int_const_binop (MINUS_EXPR, op1, t);
9125 t = fold_convert (TREE_TYPE (op0), t);
9127 gimple_assign_set_rhs_code (stmt, BIT_AND_EXPR);
9128 gimple_assign_set_rhs1 (stmt, op0);
9129 gimple_assign_set_rhs2 (stmt, t);
9132 update_stmt (stmt);
9133 return true;
9136 return false;
9139 /* If the operand to an ABS_EXPR is >= 0, then eliminate the
9140 ABS_EXPR. If the operand is <= 0, then simplify the
9141 ABS_EXPR into a NEGATE_EXPR. */
9143 static bool
9144 simplify_abs_using_ranges (gimple stmt)
9146 tree val = NULL;
9147 tree op = gimple_assign_rhs1 (stmt);
9148 tree type = TREE_TYPE (op);
9149 value_range_t *vr = get_value_range (op);
9151 if (TYPE_UNSIGNED (type))
9153 val = integer_zero_node;
9155 else if (vr)
9157 bool sop = false;
9159 val = compare_range_with_value (LE_EXPR, vr, integer_zero_node, &sop);
9160 if (!val)
9162 sop = false;
9163 val = compare_range_with_value (GE_EXPR, vr, integer_zero_node,
9164 &sop);
9166 if (val)
9168 if (integer_zerop (val))
9169 val = integer_one_node;
9170 else if (integer_onep (val))
9171 val = integer_zero_node;
9175 if (val
9176 && (integer_onep (val) || integer_zerop (val)))
9178 if (sop && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC))
9180 location_t location;
9182 if (!gimple_has_location (stmt))
9183 location = input_location;
9184 else
9185 location = gimple_location (stmt);
9186 warning_at (location, OPT_Wstrict_overflow,
9187 "assuming signed overflow does not occur when "
9188 "simplifying %<abs (X)%> to %<X%> or %<-X%>");
9191 gimple_assign_set_rhs1 (stmt, op);
9192 if (integer_onep (val))
9193 gimple_assign_set_rhs_code (stmt, NEGATE_EXPR);
9194 else
9195 gimple_assign_set_rhs_code (stmt, SSA_NAME);
9196 update_stmt (stmt);
9197 return true;
9201 return false;
9204 /* Optimize away redundant BIT_AND_EXPR and BIT_IOR_EXPR.
9205 If all the bits that are being cleared by & are already
9206 known to be zero from VR, or all the bits that are being
9207 set by | are already known to be one from VR, the bit
9208 operation is redundant. */
9210 static bool
9211 simplify_bit_ops_using_ranges (gimple_stmt_iterator *gsi, gimple stmt)
9213 tree op0 = gimple_assign_rhs1 (stmt);
9214 tree op1 = gimple_assign_rhs2 (stmt);
9215 tree op = NULL_TREE;
9216 value_range_t vr0 = VR_INITIALIZER;
9217 value_range_t vr1 = VR_INITIALIZER;
9218 wide_int may_be_nonzero0, may_be_nonzero1;
9219 wide_int must_be_nonzero0, must_be_nonzero1;
9220 wide_int mask;
9222 if (TREE_CODE (op0) == SSA_NAME)
9223 vr0 = *(get_value_range (op0));
9224 else if (is_gimple_min_invariant (op0))
9225 set_value_range_to_value (&vr0, op0, NULL);
9226 else
9227 return false;
9229 if (TREE_CODE (op1) == SSA_NAME)
9230 vr1 = *(get_value_range (op1));
9231 else if (is_gimple_min_invariant (op1))
9232 set_value_range_to_value (&vr1, op1, NULL);
9233 else
9234 return false;
9236 if (!zero_nonzero_bits_from_vr (TREE_TYPE (op0), &vr0, &may_be_nonzero0,
9237 &must_be_nonzero0))
9238 return false;
9239 if (!zero_nonzero_bits_from_vr (TREE_TYPE (op1), &vr1, &may_be_nonzero1,
9240 &must_be_nonzero1))
9241 return false;
9243 switch (gimple_assign_rhs_code (stmt))
9245 case BIT_AND_EXPR:
9246 mask = may_be_nonzero0.and_not (must_be_nonzero1);
9247 if (mask == 0)
9249 op = op0;
9250 break;
9252 mask = may_be_nonzero1.and_not (must_be_nonzero0);
9253 if (mask == 0)
9255 op = op1;
9256 break;
9258 break;
9259 case BIT_IOR_EXPR:
9260 mask = may_be_nonzero0.and_not (must_be_nonzero1);
9261 if (mask == 0)
9263 op = op1;
9264 break;
9266 mask = may_be_nonzero1.and_not (must_be_nonzero0);
9267 if (mask == 0)
9269 op = op0;
9270 break;
9272 break;
9273 default:
9274 gcc_unreachable ();
9277 if (op == NULL_TREE)
9278 return false;
9280 gimple_assign_set_rhs_with_ops (gsi, TREE_CODE (op), op);
9281 update_stmt (gsi_stmt (*gsi));
9282 return true;
9285 /* We are comparing trees OP0 and OP1 using COND_CODE. OP0 has
9286 a known value range VR.
9288 If there is one and only one value which will satisfy the
9289 conditional, then return that value. Else return NULL.
9291 If signed overflow must be undefined for the value to satisfy
9292 the conditional, then set *STRICT_OVERFLOW_P to true. */
9294 static tree
9295 test_for_singularity (enum tree_code cond_code, tree op0,
9296 tree op1, value_range_t *vr,
9297 bool *strict_overflow_p)
9299 tree min = NULL;
9300 tree max = NULL;
9302 /* Extract minimum/maximum values which satisfy the
9303 the conditional as it was written. */
9304 if (cond_code == LE_EXPR || cond_code == LT_EXPR)
9306 /* This should not be negative infinity; there is no overflow
9307 here. */
9308 min = TYPE_MIN_VALUE (TREE_TYPE (op0));
9310 max = op1;
9311 if (cond_code == LT_EXPR && !is_overflow_infinity (max))
9313 tree one = build_int_cst (TREE_TYPE (op0), 1);
9314 max = fold_build2 (MINUS_EXPR, TREE_TYPE (op0), max, one);
9315 if (EXPR_P (max))
9316 TREE_NO_WARNING (max) = 1;
9319 else if (cond_code == GE_EXPR || cond_code == GT_EXPR)
9321 /* This should not be positive infinity; there is no overflow
9322 here. */
9323 max = TYPE_MAX_VALUE (TREE_TYPE (op0));
9325 min = op1;
9326 if (cond_code == GT_EXPR && !is_overflow_infinity (min))
9328 tree one = build_int_cst (TREE_TYPE (op0), 1);
9329 min = fold_build2 (PLUS_EXPR, TREE_TYPE (op0), min, one);
9330 if (EXPR_P (min))
9331 TREE_NO_WARNING (min) = 1;
9335 /* Now refine the minimum and maximum values using any
9336 value range information we have for op0. */
9337 if (min && max)
9339 if (compare_values (vr->min, min) == 1)
9340 min = vr->min;
9341 if (compare_values (vr->max, max) == -1)
9342 max = vr->max;
9344 /* If the new min/max values have converged to a single value,
9345 then there is only one value which can satisfy the condition,
9346 return that value. */
9347 if (operand_equal_p (min, max, 0) && is_gimple_min_invariant (min))
9349 if ((cond_code == LE_EXPR || cond_code == LT_EXPR)
9350 && is_overflow_infinity (vr->max))
9351 *strict_overflow_p = true;
9352 if ((cond_code == GE_EXPR || cond_code == GT_EXPR)
9353 && is_overflow_infinity (vr->min))
9354 *strict_overflow_p = true;
9356 return min;
9359 return NULL;
9362 /* Return whether the value range *VR fits in an integer type specified
9363 by PRECISION and UNSIGNED_P. */
9365 static bool
9366 range_fits_type_p (value_range_t *vr, unsigned dest_precision, signop dest_sgn)
9368 tree src_type;
9369 unsigned src_precision;
9370 widest_int tem;
9371 signop src_sgn;
9373 /* We can only handle integral and pointer types. */
9374 src_type = TREE_TYPE (vr->min);
9375 if (!INTEGRAL_TYPE_P (src_type)
9376 && !POINTER_TYPE_P (src_type))
9377 return false;
9379 /* An extension is fine unless VR is SIGNED and dest_sgn is UNSIGNED,
9380 and so is an identity transform. */
9381 src_precision = TYPE_PRECISION (TREE_TYPE (vr->min));
9382 src_sgn = TYPE_SIGN (src_type);
9383 if ((src_precision < dest_precision
9384 && !(dest_sgn == UNSIGNED && src_sgn == SIGNED))
9385 || (src_precision == dest_precision && src_sgn == dest_sgn))
9386 return true;
9388 /* Now we can only handle ranges with constant bounds. */
9389 if (vr->type != VR_RANGE
9390 || TREE_CODE (vr->min) != INTEGER_CST
9391 || TREE_CODE (vr->max) != INTEGER_CST)
9392 return false;
9394 /* For sign changes, the MSB of the wide_int has to be clear.
9395 An unsigned value with its MSB set cannot be represented by
9396 a signed wide_int, while a negative value cannot be represented
9397 by an unsigned wide_int. */
9398 if (src_sgn != dest_sgn
9399 && (wi::lts_p (vr->min, 0) || wi::lts_p (vr->max, 0)))
9400 return false;
9402 /* Then we can perform the conversion on both ends and compare
9403 the result for equality. */
9404 tem = wi::ext (wi::to_widest (vr->min), dest_precision, dest_sgn);
9405 if (tem != wi::to_widest (vr->min))
9406 return false;
9407 tem = wi::ext (wi::to_widest (vr->max), dest_precision, dest_sgn);
9408 if (tem != wi::to_widest (vr->max))
9409 return false;
9411 return true;
9414 /* Simplify a conditional using a relational operator to an equality
9415 test if the range information indicates only one value can satisfy
9416 the original conditional. */
9418 static bool
9419 simplify_cond_using_ranges (gcond *stmt)
9421 tree op0 = gimple_cond_lhs (stmt);
9422 tree op1 = gimple_cond_rhs (stmt);
9423 enum tree_code cond_code = gimple_cond_code (stmt);
9425 if (cond_code != NE_EXPR
9426 && cond_code != EQ_EXPR
9427 && TREE_CODE (op0) == SSA_NAME
9428 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
9429 && is_gimple_min_invariant (op1))
9431 value_range_t *vr = get_value_range (op0);
9433 /* If we have range information for OP0, then we might be
9434 able to simplify this conditional. */
9435 if (vr->type == VR_RANGE)
9437 enum warn_strict_overflow_code wc = WARN_STRICT_OVERFLOW_COMPARISON;
9438 bool sop = false;
9439 tree new_tree = test_for_singularity (cond_code, op0, op1, vr, &sop);
9441 if (new_tree
9442 && (!sop || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0))))
9444 if (dump_file)
9446 fprintf (dump_file, "Simplified relational ");
9447 print_gimple_stmt (dump_file, stmt, 0, 0);
9448 fprintf (dump_file, " into ");
9451 gimple_cond_set_code (stmt, EQ_EXPR);
9452 gimple_cond_set_lhs (stmt, op0);
9453 gimple_cond_set_rhs (stmt, new_tree);
9455 update_stmt (stmt);
9457 if (dump_file)
9459 print_gimple_stmt (dump_file, stmt, 0, 0);
9460 fprintf (dump_file, "\n");
9463 if (sop && issue_strict_overflow_warning (wc))
9465 location_t location = input_location;
9466 if (gimple_has_location (stmt))
9467 location = gimple_location (stmt);
9469 warning_at (location, OPT_Wstrict_overflow,
9470 "assuming signed overflow does not occur when "
9471 "simplifying conditional");
9474 return true;
9477 /* Try again after inverting the condition. We only deal
9478 with integral types here, so no need to worry about
9479 issues with inverting FP comparisons. */
9480 sop = false;
9481 new_tree = test_for_singularity
9482 (invert_tree_comparison (cond_code, false),
9483 op0, op1, vr, &sop);
9485 if (new_tree
9486 && (!sop || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0))))
9488 if (dump_file)
9490 fprintf (dump_file, "Simplified relational ");
9491 print_gimple_stmt (dump_file, stmt, 0, 0);
9492 fprintf (dump_file, " into ");
9495 gimple_cond_set_code (stmt, NE_EXPR);
9496 gimple_cond_set_lhs (stmt, op0);
9497 gimple_cond_set_rhs (stmt, new_tree);
9499 update_stmt (stmt);
9501 if (dump_file)
9503 print_gimple_stmt (dump_file, stmt, 0, 0);
9504 fprintf (dump_file, "\n");
9507 if (sop && issue_strict_overflow_warning (wc))
9509 location_t location = input_location;
9510 if (gimple_has_location (stmt))
9511 location = gimple_location (stmt);
9513 warning_at (location, OPT_Wstrict_overflow,
9514 "assuming signed overflow does not occur when "
9515 "simplifying conditional");
9518 return true;
9523 /* If we have a comparison of an SSA_NAME (OP0) against a constant,
9524 see if OP0 was set by a type conversion where the source of
9525 the conversion is another SSA_NAME with a range that fits
9526 into the range of OP0's type.
9528 If so, the conversion is redundant as the earlier SSA_NAME can be
9529 used for the comparison directly if we just massage the constant in the
9530 comparison. */
9531 if (TREE_CODE (op0) == SSA_NAME
9532 && TREE_CODE (op1) == INTEGER_CST)
9534 gimple def_stmt = SSA_NAME_DEF_STMT (op0);
9535 tree innerop;
9537 if (!is_gimple_assign (def_stmt)
9538 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt)))
9539 return false;
9541 innerop = gimple_assign_rhs1 (def_stmt);
9543 if (TREE_CODE (innerop) == SSA_NAME
9544 && !POINTER_TYPE_P (TREE_TYPE (innerop)))
9546 value_range_t *vr = get_value_range (innerop);
9548 if (range_int_cst_p (vr)
9549 && range_fits_type_p (vr,
9550 TYPE_PRECISION (TREE_TYPE (op0)),
9551 TYPE_SIGN (TREE_TYPE (op0)))
9552 && int_fits_type_p (op1, TREE_TYPE (innerop))
9553 /* The range must not have overflowed, or if it did overflow
9554 we must not be wrapping/trapping overflow and optimizing
9555 with strict overflow semantics. */
9556 && ((!is_negative_overflow_infinity (vr->min)
9557 && !is_positive_overflow_infinity (vr->max))
9558 || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (innerop))))
9560 /* If the range overflowed and the user has asked for warnings
9561 when strict overflow semantics were used to optimize code,
9562 issue an appropriate warning. */
9563 if (cond_code != EQ_EXPR && cond_code != NE_EXPR
9564 && (is_negative_overflow_infinity (vr->min)
9565 || is_positive_overflow_infinity (vr->max))
9566 && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_CONDITIONAL))
9568 location_t location;
9570 if (!gimple_has_location (stmt))
9571 location = input_location;
9572 else
9573 location = gimple_location (stmt);
9574 warning_at (location, OPT_Wstrict_overflow,
9575 "assuming signed overflow does not occur when "
9576 "simplifying conditional");
9579 tree newconst = fold_convert (TREE_TYPE (innerop), op1);
9580 gimple_cond_set_lhs (stmt, innerop);
9581 gimple_cond_set_rhs (stmt, newconst);
9582 return true;
9587 return false;
9590 /* Simplify a switch statement using the value range of the switch
9591 argument. */
9593 static bool
9594 simplify_switch_using_ranges (gswitch *stmt)
9596 tree op = gimple_switch_index (stmt);
9597 value_range_t *vr;
9598 bool take_default;
9599 edge e;
9600 edge_iterator ei;
9601 size_t i = 0, j = 0, n, n2;
9602 tree vec2;
9603 switch_update su;
9604 size_t k = 1, l = 0;
9606 if (TREE_CODE (op) == SSA_NAME)
9608 vr = get_value_range (op);
9610 /* We can only handle integer ranges. */
9611 if ((vr->type != VR_RANGE
9612 && vr->type != VR_ANTI_RANGE)
9613 || symbolic_range_p (vr))
9614 return false;
9616 /* Find case label for min/max of the value range. */
9617 take_default = !find_case_label_ranges (stmt, vr, &i, &j, &k, &l);
9619 else if (TREE_CODE (op) == INTEGER_CST)
9621 take_default = !find_case_label_index (stmt, 1, op, &i);
9622 if (take_default)
9624 i = 1;
9625 j = 0;
9627 else
9629 j = i;
9632 else
9633 return false;
9635 n = gimple_switch_num_labels (stmt);
9637 /* Bail out if this is just all edges taken. */
9638 if (i == 1
9639 && j == n - 1
9640 && take_default)
9641 return false;
9643 /* Build a new vector of taken case labels. */
9644 vec2 = make_tree_vec (j - i + 1 + l - k + 1 + (int)take_default);
9645 n2 = 0;
9647 /* Add the default edge, if necessary. */
9648 if (take_default)
9649 TREE_VEC_ELT (vec2, n2++) = gimple_switch_default_label (stmt);
9651 for (; i <= j; ++i, ++n2)
9652 TREE_VEC_ELT (vec2, n2) = gimple_switch_label (stmt, i);
9654 for (; k <= l; ++k, ++n2)
9655 TREE_VEC_ELT (vec2, n2) = gimple_switch_label (stmt, k);
9657 /* Mark needed edges. */
9658 for (i = 0; i < n2; ++i)
9660 e = find_edge (gimple_bb (stmt),
9661 label_to_block (CASE_LABEL (TREE_VEC_ELT (vec2, i))));
9662 e->aux = (void *)-1;
9665 /* Queue not needed edges for later removal. */
9666 FOR_EACH_EDGE (e, ei, gimple_bb (stmt)->succs)
9668 if (e->aux == (void *)-1)
9670 e->aux = NULL;
9671 continue;
9674 if (dump_file && (dump_flags & TDF_DETAILS))
9676 fprintf (dump_file, "removing unreachable case label\n");
9678 to_remove_edges.safe_push (e);
9679 e->flags &= ~EDGE_EXECUTABLE;
9682 /* And queue an update for the stmt. */
9683 su.stmt = stmt;
9684 su.vec = vec2;
9685 to_update_switch_stmts.safe_push (su);
9686 return false;
9689 /* Simplify an integral conversion from an SSA name in STMT. */
9691 static bool
9692 simplify_conversion_using_ranges (gimple stmt)
9694 tree innerop, middleop, finaltype;
9695 gimple def_stmt;
9696 value_range_t *innervr;
9697 signop inner_sgn, middle_sgn, final_sgn;
9698 unsigned inner_prec, middle_prec, final_prec;
9699 widest_int innermin, innermed, innermax, middlemin, middlemed, middlemax;
9701 finaltype = TREE_TYPE (gimple_assign_lhs (stmt));
9702 if (!INTEGRAL_TYPE_P (finaltype))
9703 return false;
9704 middleop = gimple_assign_rhs1 (stmt);
9705 def_stmt = SSA_NAME_DEF_STMT (middleop);
9706 if (!is_gimple_assign (def_stmt)
9707 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt)))
9708 return false;
9709 innerop = gimple_assign_rhs1 (def_stmt);
9710 if (TREE_CODE (innerop) != SSA_NAME
9711 || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (innerop))
9712 return false;
9714 /* Get the value-range of the inner operand. */
9715 innervr = get_value_range (innerop);
9716 if (innervr->type != VR_RANGE
9717 || TREE_CODE (innervr->min) != INTEGER_CST
9718 || TREE_CODE (innervr->max) != INTEGER_CST)
9719 return false;
9721 /* Simulate the conversion chain to check if the result is equal if
9722 the middle conversion is removed. */
9723 innermin = wi::to_widest (innervr->min);
9724 innermax = wi::to_widest (innervr->max);
9726 inner_prec = TYPE_PRECISION (TREE_TYPE (innerop));
9727 middle_prec = TYPE_PRECISION (TREE_TYPE (middleop));
9728 final_prec = TYPE_PRECISION (finaltype);
9730 /* If the first conversion is not injective, the second must not
9731 be widening. */
9732 if (wi::gtu_p (innermax - innermin,
9733 wi::mask <widest_int> (middle_prec, false))
9734 && middle_prec < final_prec)
9735 return false;
9736 /* We also want a medium value so that we can track the effect that
9737 narrowing conversions with sign change have. */
9738 inner_sgn = TYPE_SIGN (TREE_TYPE (innerop));
9739 if (inner_sgn == UNSIGNED)
9740 innermed = wi::shifted_mask <widest_int> (1, inner_prec - 1, false);
9741 else
9742 innermed = 0;
9743 if (wi::cmp (innermin, innermed, inner_sgn) >= 0
9744 || wi::cmp (innermed, innermax, inner_sgn) >= 0)
9745 innermed = innermin;
9747 middle_sgn = TYPE_SIGN (TREE_TYPE (middleop));
9748 middlemin = wi::ext (innermin, middle_prec, middle_sgn);
9749 middlemed = wi::ext (innermed, middle_prec, middle_sgn);
9750 middlemax = wi::ext (innermax, middle_prec, middle_sgn);
9752 /* Require that the final conversion applied to both the original
9753 and the intermediate range produces the same result. */
9754 final_sgn = TYPE_SIGN (finaltype);
9755 if (wi::ext (middlemin, final_prec, final_sgn)
9756 != wi::ext (innermin, final_prec, final_sgn)
9757 || wi::ext (middlemed, final_prec, final_sgn)
9758 != wi::ext (innermed, final_prec, final_sgn)
9759 || wi::ext (middlemax, final_prec, final_sgn)
9760 != wi::ext (innermax, final_prec, final_sgn))
9761 return false;
9763 gimple_assign_set_rhs1 (stmt, innerop);
9764 update_stmt (stmt);
9765 return true;
9768 /* Simplify a conversion from integral SSA name to float in STMT. */
9770 static bool
9771 simplify_float_conversion_using_ranges (gimple_stmt_iterator *gsi, gimple stmt)
9773 tree rhs1 = gimple_assign_rhs1 (stmt);
9774 value_range_t *vr = get_value_range (rhs1);
9775 machine_mode fltmode = TYPE_MODE (TREE_TYPE (gimple_assign_lhs (stmt)));
9776 machine_mode mode;
9777 tree tem;
9778 gassign *conv;
9780 /* We can only handle constant ranges. */
9781 if (vr->type != VR_RANGE
9782 || TREE_CODE (vr->min) != INTEGER_CST
9783 || TREE_CODE (vr->max) != INTEGER_CST)
9784 return false;
9786 /* First check if we can use a signed type in place of an unsigned. */
9787 if (TYPE_UNSIGNED (TREE_TYPE (rhs1))
9788 && (can_float_p (fltmode, TYPE_MODE (TREE_TYPE (rhs1)), 0)
9789 != CODE_FOR_nothing)
9790 && range_fits_type_p (vr, TYPE_PRECISION (TREE_TYPE (rhs1)), SIGNED))
9791 mode = TYPE_MODE (TREE_TYPE (rhs1));
9792 /* If we can do the conversion in the current input mode do nothing. */
9793 else if (can_float_p (fltmode, TYPE_MODE (TREE_TYPE (rhs1)),
9794 TYPE_UNSIGNED (TREE_TYPE (rhs1))) != CODE_FOR_nothing)
9795 return false;
9796 /* Otherwise search for a mode we can use, starting from the narrowest
9797 integer mode available. */
9798 else
9800 mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
9803 /* If we cannot do a signed conversion to float from mode
9804 or if the value-range does not fit in the signed type
9805 try with a wider mode. */
9806 if (can_float_p (fltmode, mode, 0) != CODE_FOR_nothing
9807 && range_fits_type_p (vr, GET_MODE_PRECISION (mode), SIGNED))
9808 break;
9810 mode = GET_MODE_WIDER_MODE (mode);
9811 /* But do not widen the input. Instead leave that to the
9812 optabs expansion code. */
9813 if (GET_MODE_PRECISION (mode) > TYPE_PRECISION (TREE_TYPE (rhs1)))
9814 return false;
9816 while (mode != VOIDmode);
9817 if (mode == VOIDmode)
9818 return false;
9821 /* It works, insert a truncation or sign-change before the
9822 float conversion. */
9823 tem = make_ssa_name (build_nonstandard_integer_type
9824 (GET_MODE_PRECISION (mode), 0));
9825 conv = gimple_build_assign (tem, NOP_EXPR, rhs1);
9826 gsi_insert_before (gsi, conv, GSI_SAME_STMT);
9827 gimple_assign_set_rhs1 (stmt, tem);
9828 update_stmt (stmt);
9830 return true;
9833 /* Simplify an internal fn call using ranges if possible. */
9835 static bool
9836 simplify_internal_call_using_ranges (gimple_stmt_iterator *gsi, gimple stmt)
9838 enum tree_code subcode;
9839 bool is_ubsan = false;
9840 bool ovf = false;
9841 switch (gimple_call_internal_fn (stmt))
9843 case IFN_UBSAN_CHECK_ADD:
9844 subcode = PLUS_EXPR;
9845 is_ubsan = true;
9846 break;
9847 case IFN_UBSAN_CHECK_SUB:
9848 subcode = MINUS_EXPR;
9849 is_ubsan = true;
9850 break;
9851 case IFN_UBSAN_CHECK_MUL:
9852 subcode = MULT_EXPR;
9853 is_ubsan = true;
9854 break;
9855 case IFN_ADD_OVERFLOW:
9856 subcode = PLUS_EXPR;
9857 break;
9858 case IFN_SUB_OVERFLOW:
9859 subcode = MINUS_EXPR;
9860 break;
9861 case IFN_MUL_OVERFLOW:
9862 subcode = MULT_EXPR;
9863 break;
9864 default:
9865 return false;
9868 tree op0 = gimple_call_arg (stmt, 0);
9869 tree op1 = gimple_call_arg (stmt, 1);
9870 tree type;
9871 if (is_ubsan)
9872 type = TREE_TYPE (op0);
9873 else if (gimple_call_lhs (stmt) == NULL_TREE)
9874 return false;
9875 else
9876 type = TREE_TYPE (TREE_TYPE (gimple_call_lhs (stmt)));
9877 if (!check_for_binary_op_overflow (subcode, type, op0, op1, &ovf)
9878 || (is_ubsan && ovf))
9879 return false;
9881 gimple g;
9882 location_t loc = gimple_location (stmt);
9883 if (is_ubsan)
9884 g = gimple_build_assign (gimple_call_lhs (stmt), subcode, op0, op1);
9885 else
9887 int prec = TYPE_PRECISION (type);
9888 tree utype = type;
9889 if (ovf
9890 || !useless_type_conversion_p (type, TREE_TYPE (op0))
9891 || !useless_type_conversion_p (type, TREE_TYPE (op1)))
9892 utype = build_nonstandard_integer_type (prec, 1);
9893 if (TREE_CODE (op0) == INTEGER_CST)
9894 op0 = fold_convert (utype, op0);
9895 else if (!useless_type_conversion_p (utype, TREE_TYPE (op0)))
9897 g = gimple_build_assign (make_ssa_name (utype), NOP_EXPR, op0);
9898 gimple_set_location (g, loc);
9899 gsi_insert_before (gsi, g, GSI_SAME_STMT);
9900 op0 = gimple_assign_lhs (g);
9902 if (TREE_CODE (op1) == INTEGER_CST)
9903 op1 = fold_convert (utype, op1);
9904 else if (!useless_type_conversion_p (utype, TREE_TYPE (op1)))
9906 g = gimple_build_assign (make_ssa_name (utype), NOP_EXPR, op1);
9907 gimple_set_location (g, loc);
9908 gsi_insert_before (gsi, g, GSI_SAME_STMT);
9909 op1 = gimple_assign_lhs (g);
9911 g = gimple_build_assign (make_ssa_name (utype), subcode, op0, op1);
9912 gimple_set_location (g, loc);
9913 gsi_insert_before (gsi, g, GSI_SAME_STMT);
9914 if (utype != type)
9916 g = gimple_build_assign (make_ssa_name (type), NOP_EXPR,
9917 gimple_assign_lhs (g));
9918 gimple_set_location (g, loc);
9919 gsi_insert_before (gsi, g, GSI_SAME_STMT);
9921 g = gimple_build_assign (gimple_call_lhs (stmt), COMPLEX_EXPR,
9922 gimple_assign_lhs (g),
9923 build_int_cst (type, ovf));
9925 gimple_set_location (g, loc);
9926 gsi_replace (gsi, g, false);
9927 return true;
9930 /* Simplify STMT using ranges if possible. */
9932 static bool
9933 simplify_stmt_using_ranges (gimple_stmt_iterator *gsi)
9935 gimple stmt = gsi_stmt (*gsi);
9936 if (is_gimple_assign (stmt))
9938 enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
9939 tree rhs1 = gimple_assign_rhs1 (stmt);
9941 switch (rhs_code)
9943 case EQ_EXPR:
9944 case NE_EXPR:
9945 /* Transform EQ_EXPR, NE_EXPR into BIT_XOR_EXPR or identity
9946 if the RHS is zero or one, and the LHS are known to be boolean
9947 values. */
9948 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9949 return simplify_truth_ops_using_ranges (gsi, stmt);
9950 break;
9952 /* Transform TRUNC_DIV_EXPR and TRUNC_MOD_EXPR into RSHIFT_EXPR
9953 and BIT_AND_EXPR respectively if the first operand is greater
9954 than zero and the second operand is an exact power of two.
9955 Also optimize TRUNC_MOD_EXPR away if the second operand is
9956 constant and the first operand already has the right value
9957 range. */
9958 case TRUNC_DIV_EXPR:
9959 case TRUNC_MOD_EXPR:
9960 if (TREE_CODE (rhs1) == SSA_NAME
9961 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9962 return simplify_div_or_mod_using_ranges (stmt);
9963 break;
9965 /* Transform ABS (X) into X or -X as appropriate. */
9966 case ABS_EXPR:
9967 if (TREE_CODE (rhs1) == SSA_NAME
9968 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9969 return simplify_abs_using_ranges (stmt);
9970 break;
9972 case BIT_AND_EXPR:
9973 case BIT_IOR_EXPR:
9974 /* Optimize away BIT_AND_EXPR and BIT_IOR_EXPR
9975 if all the bits being cleared are already cleared or
9976 all the bits being set are already set. */
9977 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9978 return simplify_bit_ops_using_ranges (gsi, stmt);
9979 break;
9981 CASE_CONVERT:
9982 if (TREE_CODE (rhs1) == SSA_NAME
9983 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9984 return simplify_conversion_using_ranges (stmt);
9985 break;
9987 case FLOAT_EXPR:
9988 if (TREE_CODE (rhs1) == SSA_NAME
9989 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
9990 return simplify_float_conversion_using_ranges (gsi, stmt);
9991 break;
9993 default:
9994 break;
9997 else if (gimple_code (stmt) == GIMPLE_COND)
9998 return simplify_cond_using_ranges (as_a <gcond *> (stmt));
9999 else if (gimple_code (stmt) == GIMPLE_SWITCH)
10000 return simplify_switch_using_ranges (as_a <gswitch *> (stmt));
10001 else if (is_gimple_call (stmt)
10002 && gimple_call_internal_p (stmt))
10003 return simplify_internal_call_using_ranges (gsi, stmt);
10005 return false;
10008 /* If the statement pointed by SI has a predicate whose value can be
10009 computed using the value range information computed by VRP, compute
10010 its value and return true. Otherwise, return false. */
10012 static bool
10013 fold_predicate_in (gimple_stmt_iterator *si)
10015 bool assignment_p = false;
10016 tree val;
10017 gimple stmt = gsi_stmt (*si);
10019 if (is_gimple_assign (stmt)
10020 && TREE_CODE_CLASS (gimple_assign_rhs_code (stmt)) == tcc_comparison)
10022 assignment_p = true;
10023 val = vrp_evaluate_conditional (gimple_assign_rhs_code (stmt),
10024 gimple_assign_rhs1 (stmt),
10025 gimple_assign_rhs2 (stmt),
10026 stmt);
10028 else if (gcond *cond_stmt = dyn_cast <gcond *> (stmt))
10029 val = vrp_evaluate_conditional (gimple_cond_code (cond_stmt),
10030 gimple_cond_lhs (cond_stmt),
10031 gimple_cond_rhs (cond_stmt),
10032 stmt);
10033 else
10034 return false;
10036 if (val)
10038 if (assignment_p)
10039 val = fold_convert (gimple_expr_type (stmt), val);
10041 if (dump_file)
10043 fprintf (dump_file, "Folding predicate ");
10044 print_gimple_expr (dump_file, stmt, 0, 0);
10045 fprintf (dump_file, " to ");
10046 print_generic_expr (dump_file, val, 0);
10047 fprintf (dump_file, "\n");
10050 if (is_gimple_assign (stmt))
10051 gimple_assign_set_rhs_from_tree (si, val);
10052 else
10054 gcc_assert (gimple_code (stmt) == GIMPLE_COND);
10055 gcond *cond_stmt = as_a <gcond *> (stmt);
10056 if (integer_zerop (val))
10057 gimple_cond_make_false (cond_stmt);
10058 else if (integer_onep (val))
10059 gimple_cond_make_true (cond_stmt);
10060 else
10061 gcc_unreachable ();
10064 return true;
10067 return false;
10070 /* Callback for substitute_and_fold folding the stmt at *SI. */
10072 static bool
10073 vrp_fold_stmt (gimple_stmt_iterator *si)
10075 if (fold_predicate_in (si))
10076 return true;
10078 return simplify_stmt_using_ranges (si);
10081 /* Unwindable const/copy equivalences. */
10082 const_and_copies *equiv_stack;
10084 /* A trivial wrapper so that we can present the generic jump threading
10085 code with a simple API for simplifying statements. STMT is the
10086 statement we want to simplify, WITHIN_STMT provides the location
10087 for any overflow warnings. */
10089 static tree
10090 simplify_stmt_for_jump_threading (gimple stmt, gimple within_stmt)
10092 if (gcond *cond_stmt = dyn_cast <gcond *> (stmt))
10093 return vrp_evaluate_conditional (gimple_cond_code (cond_stmt),
10094 gimple_cond_lhs (cond_stmt),
10095 gimple_cond_rhs (cond_stmt),
10096 within_stmt);
10098 if (gassign *assign_stmt = dyn_cast <gassign *> (stmt))
10100 value_range_t new_vr = VR_INITIALIZER;
10101 tree lhs = gimple_assign_lhs (assign_stmt);
10103 if (TREE_CODE (lhs) == SSA_NAME
10104 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs))
10105 || POINTER_TYPE_P (TREE_TYPE (lhs))))
10107 extract_range_from_assignment (&new_vr, assign_stmt);
10108 if (range_int_cst_singleton_p (&new_vr))
10109 return new_vr.min;
10113 return NULL_TREE;
10116 /* Blocks which have more than one predecessor and more than
10117 one successor present jump threading opportunities, i.e.,
10118 when the block is reached from a specific predecessor, we
10119 may be able to determine which of the outgoing edges will
10120 be traversed. When this optimization applies, we are able
10121 to avoid conditionals at runtime and we may expose secondary
10122 optimization opportunities.
10124 This routine is effectively a driver for the generic jump
10125 threading code. It basically just presents the generic code
10126 with edges that may be suitable for jump threading.
10128 Unlike DOM, we do not iterate VRP if jump threading was successful.
10129 While iterating may expose new opportunities for VRP, it is expected
10130 those opportunities would be very limited and the compile time cost
10131 to expose those opportunities would be significant.
10133 As jump threading opportunities are discovered, they are registered
10134 for later realization. */
10136 static void
10137 identify_jump_threads (void)
10139 basic_block bb;
10140 gcond *dummy;
10141 int i;
10142 edge e;
10144 /* Ugh. When substituting values earlier in this pass we can
10145 wipe the dominance information. So rebuild the dominator
10146 information as we need it within the jump threading code. */
10147 calculate_dominance_info (CDI_DOMINATORS);
10149 /* We do not allow VRP information to be used for jump threading
10150 across a back edge in the CFG. Otherwise it becomes too
10151 difficult to avoid eliminating loop exit tests. Of course
10152 EDGE_DFS_BACK is not accurate at this time so we have to
10153 recompute it. */
10154 mark_dfs_back_edges ();
10156 /* Do not thread across edges we are about to remove. Just marking
10157 them as EDGE_DFS_BACK will do. */
10158 FOR_EACH_VEC_ELT (to_remove_edges, i, e)
10159 e->flags |= EDGE_DFS_BACK;
10161 /* Allocate our unwinder stack to unwind any temporary equivalences
10162 that might be recorded. */
10163 equiv_stack = new const_and_copies (dump_file, dump_flags);
10165 /* To avoid lots of silly node creation, we create a single
10166 conditional and just modify it in-place when attempting to
10167 thread jumps. */
10168 dummy = gimple_build_cond (EQ_EXPR,
10169 integer_zero_node, integer_zero_node,
10170 NULL, NULL);
10172 /* Walk through all the blocks finding those which present a
10173 potential jump threading opportunity. We could set this up
10174 as a dominator walker and record data during the walk, but
10175 I doubt it's worth the effort for the classes of jump
10176 threading opportunities we are trying to identify at this
10177 point in compilation. */
10178 FOR_EACH_BB_FN (bb, cfun)
10180 gimple last;
10182 /* If the generic jump threading code does not find this block
10183 interesting, then there is nothing to do. */
10184 if (! potentially_threadable_block (bb))
10185 continue;
10187 last = last_stmt (bb);
10189 /* We're basically looking for a switch or any kind of conditional with
10190 integral or pointer type arguments. Note the type of the second
10191 argument will be the same as the first argument, so no need to
10192 check it explicitly.
10194 We also handle the case where there are no statements in the
10195 block. This come up with forwarder blocks that are not
10196 optimized away because they lead to a loop header. But we do
10197 want to thread through them as we can sometimes thread to the
10198 loop exit which is obviously profitable. */
10199 if (!last
10200 || gimple_code (last) == GIMPLE_SWITCH
10201 || (gimple_code (last) == GIMPLE_COND
10202 && TREE_CODE (gimple_cond_lhs (last)) == SSA_NAME
10203 && (INTEGRAL_TYPE_P (TREE_TYPE (gimple_cond_lhs (last)))
10204 || POINTER_TYPE_P (TREE_TYPE (gimple_cond_lhs (last))))
10205 && (TREE_CODE (gimple_cond_rhs (last)) == SSA_NAME
10206 || is_gimple_min_invariant (gimple_cond_rhs (last)))))
10208 edge_iterator ei;
10210 /* We've got a block with multiple predecessors and multiple
10211 successors which also ends in a suitable conditional or
10212 switch statement. For each predecessor, see if we can thread
10213 it to a specific successor. */
10214 FOR_EACH_EDGE (e, ei, bb->preds)
10216 /* Do not thread across back edges or abnormal edges
10217 in the CFG. */
10218 if (e->flags & (EDGE_DFS_BACK | EDGE_COMPLEX))
10219 continue;
10221 thread_across_edge (dummy, e, true, equiv_stack,
10222 simplify_stmt_for_jump_threading);
10227 /* We do not actually update the CFG or SSA graphs at this point as
10228 ASSERT_EXPRs are still in the IL and cfg cleanup code does not yet
10229 handle ASSERT_EXPRs gracefully. */
10232 /* We identified all the jump threading opportunities earlier, but could
10233 not transform the CFG at that time. This routine transforms the
10234 CFG and arranges for the dominator tree to be rebuilt if necessary.
10236 Note the SSA graph update will occur during the normal TODO
10237 processing by the pass manager. */
10238 static void
10239 finalize_jump_threads (void)
10241 thread_through_all_blocks (false);
10242 delete equiv_stack;
10246 /* Traverse all the blocks folding conditionals with known ranges. */
10248 static void
10249 vrp_finalize (void)
10251 size_t i;
10253 values_propagated = true;
10255 if (dump_file)
10257 fprintf (dump_file, "\nValue ranges after VRP:\n\n");
10258 dump_all_value_ranges (dump_file);
10259 fprintf (dump_file, "\n");
10262 substitute_and_fold (op_with_constant_singleton_value_range,
10263 vrp_fold_stmt, false);
10265 if (warn_array_bounds && first_pass_instance)
10266 check_all_array_refs ();
10268 /* We must identify jump threading opportunities before we release
10269 the datastructures built by VRP. */
10270 identify_jump_threads ();
10272 /* Set value range to non pointer SSA_NAMEs. */
10273 for (i = 0; i < num_vr_values; i++)
10274 if (vr_value[i])
10276 tree name = ssa_name (i);
10278 if (!name
10279 || POINTER_TYPE_P (TREE_TYPE (name))
10280 || (vr_value[i]->type == VR_VARYING)
10281 || (vr_value[i]->type == VR_UNDEFINED))
10282 continue;
10284 if ((TREE_CODE (vr_value[i]->min) == INTEGER_CST)
10285 && (TREE_CODE (vr_value[i]->max) == INTEGER_CST)
10286 && (vr_value[i]->type == VR_RANGE
10287 || vr_value[i]->type == VR_ANTI_RANGE))
10288 set_range_info (name, vr_value[i]->type, vr_value[i]->min,
10289 vr_value[i]->max);
10292 /* Free allocated memory. */
10293 for (i = 0; i < num_vr_values; i++)
10294 if (vr_value[i])
10296 BITMAP_FREE (vr_value[i]->equiv);
10297 free (vr_value[i]);
10300 free (vr_value);
10301 free (vr_phi_edge_counts);
10303 /* So that we can distinguish between VRP data being available
10304 and not available. */
10305 vr_value = NULL;
10306 vr_phi_edge_counts = NULL;
10310 /* Main entry point to VRP (Value Range Propagation). This pass is
10311 loosely based on J. R. C. Patterson, ``Accurate Static Branch
10312 Prediction by Value Range Propagation,'' in SIGPLAN Conference on
10313 Programming Language Design and Implementation, pp. 67-78, 1995.
10314 Also available at http://citeseer.ist.psu.edu/patterson95accurate.html
10316 This is essentially an SSA-CCP pass modified to deal with ranges
10317 instead of constants.
10319 While propagating ranges, we may find that two or more SSA name
10320 have equivalent, though distinct ranges. For instance,
10322 1 x_9 = p_3->a;
10323 2 p_4 = ASSERT_EXPR <p_3, p_3 != 0>
10324 3 if (p_4 == q_2)
10325 4 p_5 = ASSERT_EXPR <p_4, p_4 == q_2>;
10326 5 endif
10327 6 if (q_2)
10329 In the code above, pointer p_5 has range [q_2, q_2], but from the
10330 code we can also determine that p_5 cannot be NULL and, if q_2 had
10331 a non-varying range, p_5's range should also be compatible with it.
10333 These equivalences are created by two expressions: ASSERT_EXPR and
10334 copy operations. Since p_5 is an assertion on p_4, and p_4 was the
10335 result of another assertion, then we can use the fact that p_5 and
10336 p_4 are equivalent when evaluating p_5's range.
10338 Together with value ranges, we also propagate these equivalences
10339 between names so that we can take advantage of information from
10340 multiple ranges when doing final replacement. Note that this
10341 equivalency relation is transitive but not symmetric.
10343 In the example above, p_5 is equivalent to p_4, q_2 and p_3, but we
10344 cannot assert that q_2 is equivalent to p_5 because q_2 may be used
10345 in contexts where that assertion does not hold (e.g., in line 6).
10347 TODO, the main difference between this pass and Patterson's is that
10348 we do not propagate edge probabilities. We only compute whether
10349 edges can be taken or not. That is, instead of having a spectrum
10350 of jump probabilities between 0 and 1, we only deal with 0, 1 and
10351 DON'T KNOW. In the future, it may be worthwhile to propagate
10352 probabilities to aid branch prediction. */
10354 static unsigned int
10355 execute_vrp (void)
10357 int i;
10358 edge e;
10359 switch_update *su;
10361 loop_optimizer_init (LOOPS_NORMAL | LOOPS_HAVE_RECORDED_EXITS);
10362 rewrite_into_loop_closed_ssa (NULL, TODO_update_ssa);
10363 scev_initialize ();
10365 /* ??? This ends up using stale EDGE_DFS_BACK for liveness computation.
10366 Inserting assertions may split edges which will invalidate
10367 EDGE_DFS_BACK. */
10368 insert_range_assertions ();
10370 to_remove_edges.create (10);
10371 to_update_switch_stmts.create (5);
10372 threadedge_initialize_values ();
10374 /* For visiting PHI nodes we need EDGE_DFS_BACK computed. */
10375 mark_dfs_back_edges ();
10377 vrp_initialize ();
10378 ssa_propagate (vrp_visit_stmt, vrp_visit_phi_node);
10379 vrp_finalize ();
10381 free_numbers_of_iterations_estimates ();
10383 /* ASSERT_EXPRs must be removed before finalizing jump threads
10384 as finalizing jump threads calls the CFG cleanup code which
10385 does not properly handle ASSERT_EXPRs. */
10386 remove_range_assertions ();
10388 /* If we exposed any new variables, go ahead and put them into
10389 SSA form now, before we handle jump threading. This simplifies
10390 interactions between rewriting of _DECL nodes into SSA form
10391 and rewriting SSA_NAME nodes into SSA form after block
10392 duplication and CFG manipulation. */
10393 update_ssa (TODO_update_ssa);
10395 finalize_jump_threads ();
10397 /* Remove dead edges from SWITCH_EXPR optimization. This leaves the
10398 CFG in a broken state and requires a cfg_cleanup run. */
10399 FOR_EACH_VEC_ELT (to_remove_edges, i, e)
10400 remove_edge (e);
10401 /* Update SWITCH_EXPR case label vector. */
10402 FOR_EACH_VEC_ELT (to_update_switch_stmts, i, su)
10404 size_t j;
10405 size_t n = TREE_VEC_LENGTH (su->vec);
10406 tree label;
10407 gimple_switch_set_num_labels (su->stmt, n);
10408 for (j = 0; j < n; j++)
10409 gimple_switch_set_label (su->stmt, j, TREE_VEC_ELT (su->vec, j));
10410 /* As we may have replaced the default label with a regular one
10411 make sure to make it a real default label again. This ensures
10412 optimal expansion. */
10413 label = gimple_switch_label (su->stmt, 0);
10414 CASE_LOW (label) = NULL_TREE;
10415 CASE_HIGH (label) = NULL_TREE;
10418 if (to_remove_edges.length () > 0)
10420 free_dominance_info (CDI_DOMINATORS);
10421 loops_state_set (LOOPS_NEED_FIXUP);
10424 to_remove_edges.release ();
10425 to_update_switch_stmts.release ();
10426 threadedge_finalize_values ();
10428 scev_finalize ();
10429 loop_optimizer_finalize ();
10430 return 0;
10433 namespace {
10435 const pass_data pass_data_vrp =
10437 GIMPLE_PASS, /* type */
10438 "vrp", /* name */
10439 OPTGROUP_NONE, /* optinfo_flags */
10440 TV_TREE_VRP, /* tv_id */
10441 PROP_ssa, /* properties_required */
10442 0, /* properties_provided */
10443 0, /* properties_destroyed */
10444 0, /* todo_flags_start */
10445 ( TODO_cleanup_cfg | TODO_update_ssa ), /* todo_flags_finish */
10448 class pass_vrp : public gimple_opt_pass
10450 public:
10451 pass_vrp (gcc::context *ctxt)
10452 : gimple_opt_pass (pass_data_vrp, ctxt)
10455 /* opt_pass methods: */
10456 opt_pass * clone () { return new pass_vrp (m_ctxt); }
10457 virtual bool gate (function *) { return flag_tree_vrp != 0; }
10458 virtual unsigned int execute (function *) { return execute_vrp (); }
10460 }; // class pass_vrp
10462 } // anon namespace
10464 gimple_opt_pass *
10465 make_pass_vrp (gcc::context *ctxt)
10467 return new pass_vrp (ctxt);