1 /* Support routines for Value Range Propagation (VRP).
2 Copyright (C) 2005-2014 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)
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/>. */
23 #include "coretypes.h"
27 #include "stor-layout.h"
34 #include "hard-reg-set.h"
37 #include "dominance.h"
40 #include "basic-block.h"
41 #include "tree-ssa-alias.h"
42 #include "internal-fn.h"
43 #include "gimple-fold.h"
45 #include "gimple-expr.h"
48 #include "gimple-iterator.h"
49 #include "gimple-walk.h"
50 #include "gimple-ssa.h"
52 #include "tree-phinodes.h"
53 #include "ssa-iterators.h"
54 #include "stringpool.h"
55 #include "tree-ssanames.h"
56 #include "tree-ssa-loop-manip.h"
57 #include "tree-ssa-loop-niter.h"
58 #include "tree-ssa-loop.h"
59 #include "tree-into-ssa.h"
61 #include "tree-pass.h"
62 #include "tree-dump.h"
63 #include "gimple-pretty-print.h"
64 #include "diagnostic-core.h"
67 #include "tree-scalar-evolution.h"
68 #include "tree-ssa-propagate.h"
69 #include "tree-chrec.h"
70 #include "tree-ssa-threadupdate.h"
72 #include "insn-codes.h"
74 #include "tree-ssa-threadedge.h"
79 /* Range of values that can be associated with an SSA_NAME after VRP
83 /* Lattice value represented by this range. */
84 enum value_range_type type
;
86 /* Minimum and maximum values represented by this range. These
87 values should be interpreted as follows:
89 - If TYPE is VR_UNDEFINED or VR_VARYING then MIN and MAX must
92 - If TYPE == VR_RANGE then MIN holds the minimum value and
93 MAX holds the maximum value of the range [MIN, MAX].
95 - If TYPE == ANTI_RANGE the variable is known to NOT
96 take any values in the range [MIN, MAX]. */
100 /* Set of SSA names whose value ranges are equivalent to this one.
101 This set is only valid when TYPE is VR_RANGE or VR_ANTI_RANGE. */
105 typedef struct value_range_d value_range_t
;
107 #define VR_INITIALIZER { VR_UNDEFINED, NULL_TREE, NULL_TREE, NULL }
109 /* Set of SSA names found live during the RPO traversal of the function
110 for still active basic-blocks. */
111 static sbitmap
*live
;
113 /* Return true if the SSA name NAME is live on the edge E. */
116 live_on_edge (edge e
, tree name
)
118 return (live
[e
->dest
->index
]
119 && bitmap_bit_p (live
[e
->dest
->index
], SSA_NAME_VERSION (name
)));
122 /* Local functions. */
123 static int compare_values (tree val1
, tree val2
);
124 static int compare_values_warnv (tree val1
, tree val2
, bool *);
125 static void vrp_meet (value_range_t
*, value_range_t
*);
126 static void vrp_intersect_ranges (value_range_t
*, value_range_t
*);
127 static tree
vrp_evaluate_conditional_warnv_with_ops (enum tree_code
,
128 tree
, tree
, bool, bool *,
131 /* Location information for ASSERT_EXPRs. Each instance of this
132 structure describes an ASSERT_EXPR for an SSA name. Since a single
133 SSA name may have more than one assertion associated with it, these
134 locations are kept in a linked list attached to the corresponding
136 struct assert_locus_d
138 /* Basic block where the assertion would be inserted. */
141 /* Some assertions need to be inserted on an edge (e.g., assertions
142 generated by COND_EXPRs). In those cases, BB will be NULL. */
145 /* Pointer to the statement that generated this assertion. */
146 gimple_stmt_iterator si
;
148 /* Predicate code for the ASSERT_EXPR. Must be COMPARISON_CLASS_P. */
149 enum tree_code comp_code
;
151 /* Value being compared against. */
154 /* Expression to compare. */
157 /* Next node in the linked list. */
158 struct assert_locus_d
*next
;
161 typedef struct assert_locus_d
*assert_locus_t
;
163 /* If bit I is present, it means that SSA name N_i has a list of
164 assertions that should be inserted in the IL. */
165 static bitmap need_assert_for
;
167 /* Array of locations lists where to insert assertions. ASSERTS_FOR[I]
168 holds a list of ASSERT_LOCUS_T nodes that describe where
169 ASSERT_EXPRs for SSA name N_I should be inserted. */
170 static assert_locus_t
*asserts_for
;
172 /* Value range array. After propagation, VR_VALUE[I] holds the range
173 of values that SSA name N_I may take. */
174 static unsigned num_vr_values
;
175 static value_range_t
**vr_value
;
176 static bool values_propagated
;
178 /* For a PHI node which sets SSA name N_I, VR_COUNTS[I] holds the
179 number of executable edges we saw the last time we visited the
181 static int *vr_phi_edge_counts
;
188 static vec
<edge
> to_remove_edges
;
189 static vec
<switch_update
> to_update_switch_stmts
;
192 /* Return the maximum value for TYPE. */
195 vrp_val_max (const_tree type
)
197 if (!INTEGRAL_TYPE_P (type
))
200 return TYPE_MAX_VALUE (type
);
203 /* Return the minimum value for TYPE. */
206 vrp_val_min (const_tree type
)
208 if (!INTEGRAL_TYPE_P (type
))
211 return TYPE_MIN_VALUE (type
);
214 /* Return whether VAL is equal to the maximum value of its type. This
215 will be true for a positive overflow infinity. We can't do a
216 simple equality comparison with TYPE_MAX_VALUE because C typedefs
217 and Ada subtypes can produce types whose TYPE_MAX_VALUE is not ==
218 to the integer constant with the same value in the type. */
221 vrp_val_is_max (const_tree val
)
223 tree type_max
= vrp_val_max (TREE_TYPE (val
));
224 return (val
== type_max
225 || (type_max
!= NULL_TREE
226 && operand_equal_p (val
, type_max
, 0)));
229 /* Return whether VAL is equal to the minimum value of its type. This
230 will be true for a negative overflow infinity. */
233 vrp_val_is_min (const_tree val
)
235 tree type_min
= vrp_val_min (TREE_TYPE (val
));
236 return (val
== type_min
237 || (type_min
!= NULL_TREE
238 && operand_equal_p (val
, type_min
, 0)));
242 /* Return whether TYPE should use an overflow infinity distinct from
243 TYPE_{MIN,MAX}_VALUE. We use an overflow infinity value to
244 represent a signed overflow during VRP computations. An infinity
245 is distinct from a half-range, which will go from some number to
246 TYPE_{MIN,MAX}_VALUE. */
249 needs_overflow_infinity (const_tree type
)
251 return INTEGRAL_TYPE_P (type
) && !TYPE_OVERFLOW_WRAPS (type
);
254 /* Return whether TYPE can support our overflow infinity
255 representation: we use the TREE_OVERFLOW flag, which only exists
256 for constants. If TYPE doesn't support this, we don't optimize
257 cases which would require signed overflow--we drop them to
261 supports_overflow_infinity (const_tree type
)
263 tree min
= vrp_val_min (type
), max
= vrp_val_max (type
);
264 #ifdef ENABLE_CHECKING
265 gcc_assert (needs_overflow_infinity (type
));
267 return (min
!= NULL_TREE
268 && CONSTANT_CLASS_P (min
)
270 && CONSTANT_CLASS_P (max
));
273 /* VAL is the maximum or minimum value of a type. Return a
274 corresponding overflow infinity. */
277 make_overflow_infinity (tree val
)
279 gcc_checking_assert (val
!= NULL_TREE
&& CONSTANT_CLASS_P (val
));
280 val
= copy_node (val
);
281 TREE_OVERFLOW (val
) = 1;
285 /* Return a negative overflow infinity for TYPE. */
288 negative_overflow_infinity (tree type
)
290 gcc_checking_assert (supports_overflow_infinity (type
));
291 return make_overflow_infinity (vrp_val_min (type
));
294 /* Return a positive overflow infinity for TYPE. */
297 positive_overflow_infinity (tree type
)
299 gcc_checking_assert (supports_overflow_infinity (type
));
300 return make_overflow_infinity (vrp_val_max (type
));
303 /* Return whether VAL is a negative overflow infinity. */
306 is_negative_overflow_infinity (const_tree val
)
308 return (TREE_OVERFLOW_P (val
)
309 && needs_overflow_infinity (TREE_TYPE (val
))
310 && vrp_val_is_min (val
));
313 /* Return whether VAL is a positive overflow infinity. */
316 is_positive_overflow_infinity (const_tree val
)
318 return (TREE_OVERFLOW_P (val
)
319 && needs_overflow_infinity (TREE_TYPE (val
))
320 && vrp_val_is_max (val
));
323 /* Return whether VAL is a positive or negative overflow infinity. */
326 is_overflow_infinity (const_tree val
)
328 return (TREE_OVERFLOW_P (val
)
329 && needs_overflow_infinity (TREE_TYPE (val
))
330 && (vrp_val_is_min (val
) || vrp_val_is_max (val
)));
333 /* Return whether STMT has a constant rhs that is_overflow_infinity. */
336 stmt_overflow_infinity (gimple stmt
)
338 if (is_gimple_assign (stmt
)
339 && get_gimple_rhs_class (gimple_assign_rhs_code (stmt
)) ==
341 return is_overflow_infinity (gimple_assign_rhs1 (stmt
));
345 /* If VAL is now an overflow infinity, return VAL. Otherwise, return
346 the same value with TREE_OVERFLOW clear. This can be used to avoid
347 confusing a regular value with an overflow value. */
350 avoid_overflow_infinity (tree val
)
352 if (!is_overflow_infinity (val
))
355 if (vrp_val_is_max (val
))
356 return vrp_val_max (TREE_TYPE (val
));
359 gcc_checking_assert (vrp_val_is_min (val
));
360 return vrp_val_min (TREE_TYPE (val
));
365 /* Return true if ARG is marked with the nonnull attribute in the
366 current function signature. */
369 nonnull_arg_p (const_tree arg
)
371 tree t
, attrs
, fntype
;
372 unsigned HOST_WIDE_INT arg_num
;
374 gcc_assert (TREE_CODE (arg
) == PARM_DECL
&& POINTER_TYPE_P (TREE_TYPE (arg
)));
376 /* The static chain decl is always non null. */
377 if (arg
== cfun
->static_chain_decl
)
380 fntype
= TREE_TYPE (current_function_decl
);
381 for (attrs
= TYPE_ATTRIBUTES (fntype
); attrs
; attrs
= TREE_CHAIN (attrs
))
383 attrs
= lookup_attribute ("nonnull", attrs
);
385 /* If "nonnull" wasn't specified, we know nothing about the argument. */
386 if (attrs
== NULL_TREE
)
389 /* If "nonnull" applies to all the arguments, then ARG is non-null. */
390 if (TREE_VALUE (attrs
) == NULL_TREE
)
393 /* Get the position number for ARG in the function signature. */
394 for (arg_num
= 1, t
= DECL_ARGUMENTS (current_function_decl
);
396 t
= DECL_CHAIN (t
), arg_num
++)
402 gcc_assert (t
== arg
);
404 /* Now see if ARG_NUM is mentioned in the nonnull list. */
405 for (t
= TREE_VALUE (attrs
); t
; t
= TREE_CHAIN (t
))
407 if (compare_tree_int (TREE_VALUE (t
), arg_num
) == 0)
416 /* Set value range VR to VR_UNDEFINED. */
419 set_value_range_to_undefined (value_range_t
*vr
)
421 vr
->type
= VR_UNDEFINED
;
422 vr
->min
= vr
->max
= NULL_TREE
;
424 bitmap_clear (vr
->equiv
);
428 /* Set value range VR to VR_VARYING. */
431 set_value_range_to_varying (value_range_t
*vr
)
433 vr
->type
= VR_VARYING
;
434 vr
->min
= vr
->max
= NULL_TREE
;
436 bitmap_clear (vr
->equiv
);
440 /* Set value range VR to {T, MIN, MAX, EQUIV}. */
443 set_value_range (value_range_t
*vr
, enum value_range_type t
, tree min
,
444 tree max
, bitmap equiv
)
446 #if defined ENABLE_CHECKING
447 /* Check the validity of the range. */
448 if (t
== VR_RANGE
|| t
== VR_ANTI_RANGE
)
452 gcc_assert (min
&& max
);
454 gcc_assert ((!TREE_OVERFLOW_P (min
) || is_overflow_infinity (min
))
455 && (!TREE_OVERFLOW_P (max
) || is_overflow_infinity (max
)));
457 if (INTEGRAL_TYPE_P (TREE_TYPE (min
)) && t
== VR_ANTI_RANGE
)
458 gcc_assert (!vrp_val_is_min (min
) || !vrp_val_is_max (max
));
460 cmp
= compare_values (min
, max
);
461 gcc_assert (cmp
== 0 || cmp
== -1 || cmp
== -2);
463 if (needs_overflow_infinity (TREE_TYPE (min
)))
464 gcc_assert (!is_overflow_infinity (min
)
465 || !is_overflow_infinity (max
));
468 if (t
== VR_UNDEFINED
|| t
== VR_VARYING
)
469 gcc_assert (min
== NULL_TREE
&& max
== NULL_TREE
);
471 if (t
== VR_UNDEFINED
|| t
== VR_VARYING
)
472 gcc_assert (equiv
== NULL
|| bitmap_empty_p (equiv
));
479 /* Since updating the equivalence set involves deep copying the
480 bitmaps, only do it if absolutely necessary. */
481 if (vr
->equiv
== NULL
483 vr
->equiv
= BITMAP_ALLOC (NULL
);
485 if (equiv
!= vr
->equiv
)
487 if (equiv
&& !bitmap_empty_p (equiv
))
488 bitmap_copy (vr
->equiv
, equiv
);
490 bitmap_clear (vr
->equiv
);
495 /* Set value range VR to the canonical form of {T, MIN, MAX, EQUIV}.
496 This means adjusting T, MIN and MAX representing the case of a
497 wrapping range with MAX < MIN covering [MIN, type_max] U [type_min, MAX]
498 as anti-rage ~[MAX+1, MIN-1]. Likewise for wrapping anti-ranges.
499 In corner cases where MAX+1 or MIN-1 wraps this will fall back
501 This routine exists to ease canonicalization in the case where we
502 extract ranges from var + CST op limit. */
505 set_and_canonicalize_value_range (value_range_t
*vr
, enum value_range_type t
,
506 tree min
, tree max
, bitmap equiv
)
508 /* Use the canonical setters for VR_UNDEFINED and VR_VARYING. */
509 if (t
== VR_UNDEFINED
)
511 set_value_range_to_undefined (vr
);
514 else if (t
== VR_VARYING
)
516 set_value_range_to_varying (vr
);
520 /* Nothing to canonicalize for symbolic ranges. */
521 if (TREE_CODE (min
) != INTEGER_CST
522 || TREE_CODE (max
) != INTEGER_CST
)
524 set_value_range (vr
, t
, min
, max
, equiv
);
528 /* Wrong order for min and max, to swap them and the VR type we need
530 if (tree_int_cst_lt (max
, min
))
534 /* For one bit precision if max < min, then the swapped
535 range covers all values, so for VR_RANGE it is varying and
536 for VR_ANTI_RANGE empty range, so drop to varying as well. */
537 if (TYPE_PRECISION (TREE_TYPE (min
)) == 1)
539 set_value_range_to_varying (vr
);
543 one
= build_int_cst (TREE_TYPE (min
), 1);
544 tmp
= int_const_binop (PLUS_EXPR
, max
, one
);
545 max
= int_const_binop (MINUS_EXPR
, min
, one
);
548 /* There's one corner case, if we had [C+1, C] before we now have
549 that again. But this represents an empty value range, so drop
550 to varying in this case. */
551 if (tree_int_cst_lt (max
, min
))
553 set_value_range_to_varying (vr
);
557 t
= t
== VR_RANGE
? VR_ANTI_RANGE
: VR_RANGE
;
560 /* Anti-ranges that can be represented as ranges should be so. */
561 if (t
== VR_ANTI_RANGE
)
563 bool is_min
= vrp_val_is_min (min
);
564 bool is_max
= vrp_val_is_max (max
);
566 if (is_min
&& is_max
)
568 /* We cannot deal with empty ranges, drop to varying.
569 ??? This could be VR_UNDEFINED instead. */
570 set_value_range_to_varying (vr
);
573 else if (TYPE_PRECISION (TREE_TYPE (min
)) == 1
574 && (is_min
|| is_max
))
576 /* Non-empty boolean ranges can always be represented
577 as a singleton range. */
579 min
= max
= vrp_val_max (TREE_TYPE (min
));
581 min
= max
= vrp_val_min (TREE_TYPE (min
));
585 /* As a special exception preserve non-null ranges. */
586 && !(TYPE_UNSIGNED (TREE_TYPE (min
))
587 && integer_zerop (max
)))
589 tree one
= build_int_cst (TREE_TYPE (max
), 1);
590 min
= int_const_binop (PLUS_EXPR
, max
, one
);
591 max
= vrp_val_max (TREE_TYPE (max
));
596 tree one
= build_int_cst (TREE_TYPE (min
), 1);
597 max
= int_const_binop (MINUS_EXPR
, min
, one
);
598 min
= vrp_val_min (TREE_TYPE (min
));
603 /* Drop [-INF(OVF), +INF(OVF)] to varying. */
604 if (needs_overflow_infinity (TREE_TYPE (min
))
605 && is_overflow_infinity (min
)
606 && is_overflow_infinity (max
))
608 set_value_range_to_varying (vr
);
612 set_value_range (vr
, t
, min
, max
, equiv
);
615 /* Copy value range FROM into value range TO. */
618 copy_value_range (value_range_t
*to
, value_range_t
*from
)
620 set_value_range (to
, from
->type
, from
->min
, from
->max
, from
->equiv
);
623 /* Set value range VR to a single value. This function is only called
624 with values we get from statements, and exists to clear the
625 TREE_OVERFLOW flag so that we don't think we have an overflow
626 infinity when we shouldn't. */
629 set_value_range_to_value (value_range_t
*vr
, tree val
, bitmap equiv
)
631 gcc_assert (is_gimple_min_invariant (val
));
632 if (TREE_OVERFLOW_P (val
))
633 val
= drop_tree_overflow (val
);
634 set_value_range (vr
, VR_RANGE
, val
, val
, equiv
);
637 /* Set value range VR to a non-negative range of type TYPE.
638 OVERFLOW_INFINITY indicates whether to use an overflow infinity
639 rather than TYPE_MAX_VALUE; this should be true if we determine
640 that the range is nonnegative based on the assumption that signed
641 overflow does not occur. */
644 set_value_range_to_nonnegative (value_range_t
*vr
, tree type
,
645 bool overflow_infinity
)
649 if (overflow_infinity
&& !supports_overflow_infinity (type
))
651 set_value_range_to_varying (vr
);
655 zero
= build_int_cst (type
, 0);
656 set_value_range (vr
, VR_RANGE
, zero
,
658 ? positive_overflow_infinity (type
)
659 : TYPE_MAX_VALUE (type
)),
663 /* Set value range VR to a non-NULL range of type TYPE. */
666 set_value_range_to_nonnull (value_range_t
*vr
, tree type
)
668 tree zero
= build_int_cst (type
, 0);
669 set_value_range (vr
, VR_ANTI_RANGE
, zero
, zero
, vr
->equiv
);
673 /* Set value range VR to a NULL range of type TYPE. */
676 set_value_range_to_null (value_range_t
*vr
, tree type
)
678 set_value_range_to_value (vr
, build_int_cst (type
, 0), vr
->equiv
);
682 /* Set value range VR to a range of a truthvalue of type TYPE. */
685 set_value_range_to_truthvalue (value_range_t
*vr
, tree type
)
687 if (TYPE_PRECISION (type
) == 1)
688 set_value_range_to_varying (vr
);
690 set_value_range (vr
, VR_RANGE
,
691 build_int_cst (type
, 0), build_int_cst (type
, 1),
696 /* If abs (min) < abs (max), set VR to [-max, max], if
697 abs (min) >= abs (max), set VR to [-min, min]. */
700 abs_extent_range (value_range_t
*vr
, tree min
, tree max
)
704 gcc_assert (TREE_CODE (min
) == INTEGER_CST
);
705 gcc_assert (TREE_CODE (max
) == INTEGER_CST
);
706 gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (min
)));
707 gcc_assert (!TYPE_UNSIGNED (TREE_TYPE (min
)));
708 min
= fold_unary (ABS_EXPR
, TREE_TYPE (min
), min
);
709 max
= fold_unary (ABS_EXPR
, TREE_TYPE (max
), max
);
710 if (TREE_OVERFLOW (min
) || TREE_OVERFLOW (max
))
712 set_value_range_to_varying (vr
);
715 cmp
= compare_values (min
, max
);
717 min
= fold_unary (NEGATE_EXPR
, TREE_TYPE (min
), max
);
718 else if (cmp
== 0 || cmp
== 1)
721 min
= fold_unary (NEGATE_EXPR
, TREE_TYPE (min
), min
);
725 set_value_range_to_varying (vr
);
728 set_and_canonicalize_value_range (vr
, VR_RANGE
, min
, max
, NULL
);
732 /* Return value range information for VAR.
734 If we have no values ranges recorded (ie, VRP is not running), then
735 return NULL. Otherwise create an empty range if none existed for VAR. */
737 static value_range_t
*
738 get_value_range (const_tree var
)
740 static const struct value_range_d vr_const_varying
741 = { VR_VARYING
, NULL_TREE
, NULL_TREE
, NULL
};
744 unsigned ver
= SSA_NAME_VERSION (var
);
746 /* If we have no recorded ranges, then return NULL. */
750 /* If we query the range for a new SSA name return an unmodifiable VARYING.
751 We should get here at most from the substitute-and-fold stage which
752 will never try to change values. */
753 if (ver
>= num_vr_values
)
754 return CONST_CAST (value_range_t
*, &vr_const_varying
);
760 /* After propagation finished do not allocate new value-ranges. */
761 if (values_propagated
)
762 return CONST_CAST (value_range_t
*, &vr_const_varying
);
764 /* Create a default value range. */
765 vr_value
[ver
] = vr
= XCNEW (value_range_t
);
767 /* Defer allocating the equivalence set. */
770 /* If VAR is a default definition of a parameter, the variable can
771 take any value in VAR's type. */
772 if (SSA_NAME_IS_DEFAULT_DEF (var
))
774 sym
= SSA_NAME_VAR (var
);
775 if (TREE_CODE (sym
) == PARM_DECL
)
777 /* Try to use the "nonnull" attribute to create ~[0, 0]
778 anti-ranges for pointers. Note that this is only valid with
779 default definitions of PARM_DECLs. */
780 if (POINTER_TYPE_P (TREE_TYPE (sym
))
781 && nonnull_arg_p (sym
))
782 set_value_range_to_nonnull (vr
, TREE_TYPE (sym
));
784 set_value_range_to_varying (vr
);
786 else if (TREE_CODE (sym
) == RESULT_DECL
787 && DECL_BY_REFERENCE (sym
))
788 set_value_range_to_nonnull (vr
, TREE_TYPE (sym
));
794 /* Return true, if VAL1 and VAL2 are equal values for VRP purposes. */
797 vrp_operand_equal_p (const_tree val1
, const_tree val2
)
801 if (!val1
|| !val2
|| !operand_equal_p (val1
, val2
, 0))
803 return is_overflow_infinity (val1
) == is_overflow_infinity (val2
);
806 /* Return true, if the bitmaps B1 and B2 are equal. */
809 vrp_bitmap_equal_p (const_bitmap b1
, const_bitmap b2
)
812 || ((!b1
|| bitmap_empty_p (b1
))
813 && (!b2
|| bitmap_empty_p (b2
)))
815 && bitmap_equal_p (b1
, b2
)));
818 /* Update the value range and equivalence set for variable VAR to
819 NEW_VR. Return true if NEW_VR is different from VAR's previous
822 NOTE: This function assumes that NEW_VR is a temporary value range
823 object created for the sole purpose of updating VAR's range. The
824 storage used by the equivalence set from NEW_VR will be freed by
825 this function. Do not call update_value_range when NEW_VR
826 is the range object associated with another SSA name. */
829 update_value_range (const_tree var
, value_range_t
*new_vr
)
831 value_range_t
*old_vr
;
834 /* Update the value range, if necessary. */
835 old_vr
= get_value_range (var
);
836 is_new
= old_vr
->type
!= new_vr
->type
837 || !vrp_operand_equal_p (old_vr
->min
, new_vr
->min
)
838 || !vrp_operand_equal_p (old_vr
->max
, new_vr
->max
)
839 || !vrp_bitmap_equal_p (old_vr
->equiv
, new_vr
->equiv
);
843 /* Do not allow transitions up the lattice. The following
844 is slightly more awkward than just new_vr->type < old_vr->type
845 because VR_RANGE and VR_ANTI_RANGE need to be considered
846 the same. We may not have is_new when transitioning to
847 UNDEFINED or from VARYING. */
848 if (new_vr
->type
== VR_UNDEFINED
849 || old_vr
->type
== VR_VARYING
)
850 set_value_range_to_varying (old_vr
);
852 set_value_range (old_vr
, new_vr
->type
, new_vr
->min
, new_vr
->max
,
856 BITMAP_FREE (new_vr
->equiv
);
862 /* Add VAR and VAR's equivalence set to EQUIV. This is the central
863 point where equivalence processing can be turned on/off. */
866 add_equivalence (bitmap
*equiv
, const_tree var
)
868 unsigned ver
= SSA_NAME_VERSION (var
);
869 value_range_t
*vr
= vr_value
[ver
];
872 *equiv
= BITMAP_ALLOC (NULL
);
873 bitmap_set_bit (*equiv
, ver
);
875 bitmap_ior_into (*equiv
, vr
->equiv
);
879 /* Return true if VR is ~[0, 0]. */
882 range_is_nonnull (value_range_t
*vr
)
884 return vr
->type
== VR_ANTI_RANGE
885 && integer_zerop (vr
->min
)
886 && integer_zerop (vr
->max
);
890 /* Return true if VR is [0, 0]. */
893 range_is_null (value_range_t
*vr
)
895 return vr
->type
== VR_RANGE
896 && integer_zerop (vr
->min
)
897 && integer_zerop (vr
->max
);
900 /* Return true if max and min of VR are INTEGER_CST. It's not necessary
904 range_int_cst_p (value_range_t
*vr
)
906 return (vr
->type
== VR_RANGE
907 && TREE_CODE (vr
->max
) == INTEGER_CST
908 && TREE_CODE (vr
->min
) == INTEGER_CST
);
911 /* Return true if VR is a INTEGER_CST singleton. */
914 range_int_cst_singleton_p (value_range_t
*vr
)
916 return (range_int_cst_p (vr
)
917 && !is_overflow_infinity (vr
->min
)
918 && !is_overflow_infinity (vr
->max
)
919 && tree_int_cst_equal (vr
->min
, vr
->max
));
922 /* Return true if value range VR involves at least one symbol. */
925 symbolic_range_p (value_range_t
*vr
)
927 return (!is_gimple_min_invariant (vr
->min
)
928 || !is_gimple_min_invariant (vr
->max
));
931 /* Return the single symbol (an SSA_NAME) contained in T if any, or NULL_TREE
932 otherwise. We only handle additive operations and set NEG to true if the
933 symbol is negated and INV to the invariant part, if any. */
936 get_single_symbol (tree t
, bool *neg
, tree
*inv
)
941 if (TREE_CODE (t
) == PLUS_EXPR
942 || TREE_CODE (t
) == POINTER_PLUS_EXPR
943 || TREE_CODE (t
) == MINUS_EXPR
)
945 if (is_gimple_min_invariant (TREE_OPERAND (t
, 0)))
947 neg_
= (TREE_CODE (t
) == MINUS_EXPR
);
948 inv_
= TREE_OPERAND (t
, 0);
949 t
= TREE_OPERAND (t
, 1);
951 else if (is_gimple_min_invariant (TREE_OPERAND (t
, 1)))
954 inv_
= TREE_OPERAND (t
, 1);
955 t
= TREE_OPERAND (t
, 0);
966 if (TREE_CODE (t
) == NEGATE_EXPR
)
968 t
= TREE_OPERAND (t
, 0);
972 if (TREE_CODE (t
) != SSA_NAME
)
980 /* The reverse operation: build a symbolic expression with TYPE
981 from symbol SYM, negated according to NEG, and invariant INV. */
984 build_symbolic_expr (tree type
, tree sym
, bool neg
, tree inv
)
986 const bool pointer_p
= POINTER_TYPE_P (type
);
990 t
= build1 (NEGATE_EXPR
, type
, t
);
992 if (integer_zerop (inv
))
995 return build2 (pointer_p
? POINTER_PLUS_EXPR
: PLUS_EXPR
, type
, t
, inv
);
998 /* Return true if value range VR involves exactly one symbol SYM. */
1001 symbolic_range_based_on_p (value_range_t
*vr
, const_tree sym
)
1003 bool neg
, min_has_symbol
, max_has_symbol
;
1006 if (is_gimple_min_invariant (vr
->min
))
1007 min_has_symbol
= false;
1008 else if (get_single_symbol (vr
->min
, &neg
, &inv
) == sym
)
1009 min_has_symbol
= true;
1013 if (is_gimple_min_invariant (vr
->max
))
1014 max_has_symbol
= false;
1015 else if (get_single_symbol (vr
->max
, &neg
, &inv
) == sym
)
1016 max_has_symbol
= true;
1020 return (min_has_symbol
|| max_has_symbol
);
1023 /* Return true if value range VR uses an overflow infinity. */
1026 overflow_infinity_range_p (value_range_t
*vr
)
1028 return (vr
->type
== VR_RANGE
1029 && (is_overflow_infinity (vr
->min
)
1030 || is_overflow_infinity (vr
->max
)));
1033 /* Return false if we can not make a valid comparison based on VR;
1034 this will be the case if it uses an overflow infinity and overflow
1035 is not undefined (i.e., -fno-strict-overflow is in effect).
1036 Otherwise return true, and set *STRICT_OVERFLOW_P to true if VR
1037 uses an overflow infinity. */
1040 usable_range_p (value_range_t
*vr
, bool *strict_overflow_p
)
1042 gcc_assert (vr
->type
== VR_RANGE
);
1043 if (is_overflow_infinity (vr
->min
))
1045 *strict_overflow_p
= true;
1046 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (vr
->min
)))
1049 if (is_overflow_infinity (vr
->max
))
1051 *strict_overflow_p
= true;
1052 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (vr
->max
)))
1059 /* Return true if the result of assignment STMT is know to be non-negative.
1060 If the return value is based on the assumption that signed overflow is
1061 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1062 *STRICT_OVERFLOW_P.*/
1065 gimple_assign_nonnegative_warnv_p (gimple stmt
, bool *strict_overflow_p
)
1067 enum tree_code code
= gimple_assign_rhs_code (stmt
);
1068 switch (get_gimple_rhs_class (code
))
1070 case GIMPLE_UNARY_RHS
:
1071 return tree_unary_nonnegative_warnv_p (gimple_assign_rhs_code (stmt
),
1072 gimple_expr_type (stmt
),
1073 gimple_assign_rhs1 (stmt
),
1075 case GIMPLE_BINARY_RHS
:
1076 return tree_binary_nonnegative_warnv_p (gimple_assign_rhs_code (stmt
),
1077 gimple_expr_type (stmt
),
1078 gimple_assign_rhs1 (stmt
),
1079 gimple_assign_rhs2 (stmt
),
1081 case GIMPLE_TERNARY_RHS
:
1083 case GIMPLE_SINGLE_RHS
:
1084 return tree_single_nonnegative_warnv_p (gimple_assign_rhs1 (stmt
),
1086 case GIMPLE_INVALID_RHS
:
1093 /* Return true if return value of call STMT is know to be non-negative.
1094 If the return value is based on the assumption that signed overflow is
1095 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1096 *STRICT_OVERFLOW_P.*/
1099 gimple_call_nonnegative_warnv_p (gimple stmt
, bool *strict_overflow_p
)
1101 tree arg0
= gimple_call_num_args (stmt
) > 0 ?
1102 gimple_call_arg (stmt
, 0) : NULL_TREE
;
1103 tree arg1
= gimple_call_num_args (stmt
) > 1 ?
1104 gimple_call_arg (stmt
, 1) : NULL_TREE
;
1106 return tree_call_nonnegative_warnv_p (gimple_expr_type (stmt
),
1107 gimple_call_fndecl (stmt
),
1113 /* Return true if STMT is know to to compute a non-negative value.
1114 If the return value is based on the assumption that signed overflow is
1115 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1116 *STRICT_OVERFLOW_P.*/
1119 gimple_stmt_nonnegative_warnv_p (gimple stmt
, bool *strict_overflow_p
)
1121 switch (gimple_code (stmt
))
1124 return gimple_assign_nonnegative_warnv_p (stmt
, strict_overflow_p
);
1126 return gimple_call_nonnegative_warnv_p (stmt
, strict_overflow_p
);
1132 /* Return true if the result of assignment STMT is know to be non-zero.
1133 If the return value is based on the assumption that signed overflow is
1134 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1135 *STRICT_OVERFLOW_P.*/
1138 gimple_assign_nonzero_warnv_p (gimple stmt
, bool *strict_overflow_p
)
1140 enum tree_code code
= gimple_assign_rhs_code (stmt
);
1141 switch (get_gimple_rhs_class (code
))
1143 case GIMPLE_UNARY_RHS
:
1144 return tree_unary_nonzero_warnv_p (gimple_assign_rhs_code (stmt
),
1145 gimple_expr_type (stmt
),
1146 gimple_assign_rhs1 (stmt
),
1148 case GIMPLE_BINARY_RHS
:
1149 return tree_binary_nonzero_warnv_p (gimple_assign_rhs_code (stmt
),
1150 gimple_expr_type (stmt
),
1151 gimple_assign_rhs1 (stmt
),
1152 gimple_assign_rhs2 (stmt
),
1154 case GIMPLE_TERNARY_RHS
:
1156 case GIMPLE_SINGLE_RHS
:
1157 return tree_single_nonzero_warnv_p (gimple_assign_rhs1 (stmt
),
1159 case GIMPLE_INVALID_RHS
:
1166 /* Return true if STMT is known to compute a non-zero value.
1167 If the return value is based on the assumption that signed overflow is
1168 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1169 *STRICT_OVERFLOW_P.*/
1172 gimple_stmt_nonzero_warnv_p (gimple stmt
, bool *strict_overflow_p
)
1174 switch (gimple_code (stmt
))
1177 return gimple_assign_nonzero_warnv_p (stmt
, strict_overflow_p
);
1180 tree fndecl
= gimple_call_fndecl (stmt
);
1181 if (!fndecl
) return false;
1182 if (flag_delete_null_pointer_checks
&& !flag_check_new
1183 && DECL_IS_OPERATOR_NEW (fndecl
)
1184 && !TREE_NOTHROW (fndecl
))
1186 if (flag_delete_null_pointer_checks
&&
1187 lookup_attribute ("returns_nonnull",
1188 TYPE_ATTRIBUTES (gimple_call_fntype (stmt
))))
1190 return gimple_alloca_call_p (stmt
);
1197 /* Like tree_expr_nonzero_warnv_p, but this function uses value ranges
1201 vrp_stmt_computes_nonzero (gimple stmt
, bool *strict_overflow_p
)
1203 if (gimple_stmt_nonzero_warnv_p (stmt
, strict_overflow_p
))
1206 /* If we have an expression of the form &X->a, then the expression
1207 is nonnull if X is nonnull. */
1208 if (is_gimple_assign (stmt
)
1209 && gimple_assign_rhs_code (stmt
) == ADDR_EXPR
)
1211 tree expr
= gimple_assign_rhs1 (stmt
);
1212 tree base
= get_base_address (TREE_OPERAND (expr
, 0));
1214 if (base
!= NULL_TREE
1215 && TREE_CODE (base
) == MEM_REF
1216 && TREE_CODE (TREE_OPERAND (base
, 0)) == SSA_NAME
)
1218 value_range_t
*vr
= get_value_range (TREE_OPERAND (base
, 0));
1219 if (range_is_nonnull (vr
))
1227 /* Returns true if EXPR is a valid value (as expected by compare_values) --
1228 a gimple invariant, or SSA_NAME +- CST. */
1231 valid_value_p (tree expr
)
1233 if (TREE_CODE (expr
) == SSA_NAME
)
1236 if (TREE_CODE (expr
) == PLUS_EXPR
1237 || TREE_CODE (expr
) == MINUS_EXPR
)
1238 return (TREE_CODE (TREE_OPERAND (expr
, 0)) == SSA_NAME
1239 && TREE_CODE (TREE_OPERAND (expr
, 1)) == INTEGER_CST
);
1241 return is_gimple_min_invariant (expr
);
1247 -2 if those are incomparable. */
1249 operand_less_p (tree val
, tree val2
)
1251 /* LT is folded faster than GE and others. Inline the common case. */
1252 if (TREE_CODE (val
) == INTEGER_CST
&& TREE_CODE (val2
) == INTEGER_CST
)
1253 return tree_int_cst_lt (val
, val2
);
1258 fold_defer_overflow_warnings ();
1260 tcmp
= fold_binary_to_constant (LT_EXPR
, boolean_type_node
, val
, val2
);
1262 fold_undefer_and_ignore_overflow_warnings ();
1265 || TREE_CODE (tcmp
) != INTEGER_CST
)
1268 if (!integer_zerop (tcmp
))
1272 /* val >= val2, not considering overflow infinity. */
1273 if (is_negative_overflow_infinity (val
))
1274 return is_negative_overflow_infinity (val2
) ? 0 : 1;
1275 else if (is_positive_overflow_infinity (val2
))
1276 return is_positive_overflow_infinity (val
) ? 0 : 1;
1281 /* Compare two values VAL1 and VAL2. Return
1283 -2 if VAL1 and VAL2 cannot be compared at compile-time,
1286 +1 if VAL1 > VAL2, and
1289 This is similar to tree_int_cst_compare but supports pointer values
1290 and values that cannot be compared at compile time.
1292 If STRICT_OVERFLOW_P is not NULL, then set *STRICT_OVERFLOW_P to
1293 true if the return value is only valid if we assume that signed
1294 overflow is undefined. */
1297 compare_values_warnv (tree val1
, tree val2
, bool *strict_overflow_p
)
1302 /* Below we rely on the fact that VAL1 and VAL2 are both pointers or
1304 gcc_assert (POINTER_TYPE_P (TREE_TYPE (val1
))
1305 == POINTER_TYPE_P (TREE_TYPE (val2
)));
1307 /* Convert the two values into the same type. This is needed because
1308 sizetype causes sign extension even for unsigned types. */
1309 val2
= fold_convert (TREE_TYPE (val1
), val2
);
1310 STRIP_USELESS_TYPE_CONVERSION (val2
);
1312 if ((TREE_CODE (val1
) == SSA_NAME
1313 || (TREE_CODE (val1
) == NEGATE_EXPR
1314 && TREE_CODE (TREE_OPERAND (val1
, 0)) == SSA_NAME
)
1315 || TREE_CODE (val1
) == PLUS_EXPR
1316 || TREE_CODE (val1
) == MINUS_EXPR
)
1317 && (TREE_CODE (val2
) == SSA_NAME
1318 || (TREE_CODE (val2
) == NEGATE_EXPR
1319 && TREE_CODE (TREE_OPERAND (val2
, 0)) == SSA_NAME
)
1320 || TREE_CODE (val2
) == PLUS_EXPR
1321 || TREE_CODE (val2
) == MINUS_EXPR
))
1323 tree n1
, c1
, n2
, c2
;
1324 enum tree_code code1
, code2
;
1326 /* If VAL1 and VAL2 are of the form '[-]NAME [+-] CST' or 'NAME',
1327 return -1 or +1 accordingly. If VAL1 and VAL2 don't use the
1328 same name, return -2. */
1329 if (TREE_CODE (val1
) == SSA_NAME
|| TREE_CODE (val1
) == NEGATE_EXPR
)
1337 code1
= TREE_CODE (val1
);
1338 n1
= TREE_OPERAND (val1
, 0);
1339 c1
= TREE_OPERAND (val1
, 1);
1340 if (tree_int_cst_sgn (c1
) == -1)
1342 if (is_negative_overflow_infinity (c1
))
1344 c1
= fold_unary_to_constant (NEGATE_EXPR
, TREE_TYPE (c1
), c1
);
1347 code1
= code1
== MINUS_EXPR
? PLUS_EXPR
: MINUS_EXPR
;
1351 if (TREE_CODE (val2
) == SSA_NAME
|| TREE_CODE (val2
) == NEGATE_EXPR
)
1359 code2
= TREE_CODE (val2
);
1360 n2
= TREE_OPERAND (val2
, 0);
1361 c2
= TREE_OPERAND (val2
, 1);
1362 if (tree_int_cst_sgn (c2
) == -1)
1364 if (is_negative_overflow_infinity (c2
))
1366 c2
= fold_unary_to_constant (NEGATE_EXPR
, TREE_TYPE (c2
), c2
);
1369 code2
= code2
== MINUS_EXPR
? PLUS_EXPR
: MINUS_EXPR
;
1373 /* Both values must use the same name. */
1374 if (TREE_CODE (n1
) == NEGATE_EXPR
&& TREE_CODE (n2
) == NEGATE_EXPR
)
1376 n1
= TREE_OPERAND (n1
, 0);
1377 n2
= TREE_OPERAND (n2
, 0);
1382 if (code1
== SSA_NAME
&& code2
== SSA_NAME
)
1386 /* If overflow is defined we cannot simplify more. */
1387 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (val1
)))
1390 if (strict_overflow_p
!= NULL
1391 && (code1
== SSA_NAME
|| !TREE_NO_WARNING (val1
))
1392 && (code2
== SSA_NAME
|| !TREE_NO_WARNING (val2
)))
1393 *strict_overflow_p
= true;
1395 if (code1
== SSA_NAME
)
1397 if (code2
== PLUS_EXPR
)
1398 /* NAME < NAME + CST */
1400 else if (code2
== MINUS_EXPR
)
1401 /* NAME > NAME - CST */
1404 else if (code1
== PLUS_EXPR
)
1406 if (code2
== SSA_NAME
)
1407 /* NAME + CST > NAME */
1409 else if (code2
== PLUS_EXPR
)
1410 /* NAME + CST1 > NAME + CST2, if CST1 > CST2 */
1411 return compare_values_warnv (c1
, c2
, strict_overflow_p
);
1412 else if (code2
== MINUS_EXPR
)
1413 /* NAME + CST1 > NAME - CST2 */
1416 else if (code1
== MINUS_EXPR
)
1418 if (code2
== SSA_NAME
)
1419 /* NAME - CST < NAME */
1421 else if (code2
== PLUS_EXPR
)
1422 /* NAME - CST1 < NAME + CST2 */
1424 else if (code2
== MINUS_EXPR
)
1425 /* NAME - CST1 > NAME - CST2, if CST1 < CST2. Notice that
1426 C1 and C2 are swapped in the call to compare_values. */
1427 return compare_values_warnv (c2
, c1
, strict_overflow_p
);
1433 /* We cannot compare non-constants. */
1434 if (!is_gimple_min_invariant (val1
) || !is_gimple_min_invariant (val2
))
1437 if (!POINTER_TYPE_P (TREE_TYPE (val1
)))
1439 /* We cannot compare overflowed values, except for overflow
1441 if (TREE_OVERFLOW (val1
) || TREE_OVERFLOW (val2
))
1443 if (strict_overflow_p
!= NULL
)
1444 *strict_overflow_p
= true;
1445 if (is_negative_overflow_infinity (val1
))
1446 return is_negative_overflow_infinity (val2
) ? 0 : -1;
1447 else if (is_negative_overflow_infinity (val2
))
1449 else if (is_positive_overflow_infinity (val1
))
1450 return is_positive_overflow_infinity (val2
) ? 0 : 1;
1451 else if (is_positive_overflow_infinity (val2
))
1456 return tree_int_cst_compare (val1
, val2
);
1462 /* First see if VAL1 and VAL2 are not the same. */
1463 if (val1
== val2
|| operand_equal_p (val1
, val2
, 0))
1466 /* If VAL1 is a lower address than VAL2, return -1. */
1467 if (operand_less_p (val1
, val2
) == 1)
1470 /* If VAL1 is a higher address than VAL2, return +1. */
1471 if (operand_less_p (val2
, val1
) == 1)
1474 /* If VAL1 is different than VAL2, return +2.
1475 For integer constants we either have already returned -1 or 1
1476 or they are equivalent. We still might succeed in proving
1477 something about non-trivial operands. */
1478 if (TREE_CODE (val1
) != INTEGER_CST
1479 || TREE_CODE (val2
) != INTEGER_CST
)
1481 t
= fold_binary_to_constant (NE_EXPR
, boolean_type_node
, val1
, val2
);
1482 if (t
&& integer_onep (t
))
1490 /* Compare values like compare_values_warnv, but treat comparisons of
1491 nonconstants which rely on undefined overflow as incomparable. */
1494 compare_values (tree val1
, tree val2
)
1500 ret
= compare_values_warnv (val1
, val2
, &sop
);
1502 && (!is_gimple_min_invariant (val1
) || !is_gimple_min_invariant (val2
)))
1508 /* Return 1 if VAL is inside value range MIN <= VAL <= MAX,
1509 0 if VAL is not inside [MIN, MAX],
1510 -2 if we cannot tell either way.
1512 Benchmark compile/20001226-1.c compilation time after changing this
1516 value_inside_range (tree val
, tree min
, tree max
)
1520 cmp1
= operand_less_p (val
, min
);
1526 cmp2
= operand_less_p (max
, val
);
1534 /* Return true if value ranges VR0 and VR1 have a non-empty
1537 Benchmark compile/20001226-1.c compilation time after changing this
1542 value_ranges_intersect_p (value_range_t
*vr0
, value_range_t
*vr1
)
1544 /* The value ranges do not intersect if the maximum of the first range is
1545 less than the minimum of the second range or vice versa.
1546 When those relations are unknown, we can't do any better. */
1547 if (operand_less_p (vr0
->max
, vr1
->min
) != 0)
1549 if (operand_less_p (vr1
->max
, vr0
->min
) != 0)
1555 /* Return 1 if [MIN, MAX] includes the value zero, 0 if it does not
1556 include the value zero, -2 if we cannot tell. */
1559 range_includes_zero_p (tree min
, tree max
)
1561 tree zero
= build_int_cst (TREE_TYPE (min
), 0);
1562 return value_inside_range (zero
, min
, max
);
1565 /* Return true if *VR is know to only contain nonnegative values. */
1568 value_range_nonnegative_p (value_range_t
*vr
)
1570 /* Testing for VR_ANTI_RANGE is not useful here as any anti-range
1571 which would return a useful value should be encoded as a
1573 if (vr
->type
== VR_RANGE
)
1575 int result
= compare_values (vr
->min
, integer_zero_node
);
1576 return (result
== 0 || result
== 1);
1582 /* If *VR has a value rante that is a single constant value return that,
1583 otherwise return NULL_TREE. */
1586 value_range_constant_singleton (value_range_t
*vr
)
1588 if (vr
->type
== VR_RANGE
1589 && operand_equal_p (vr
->min
, vr
->max
, 0)
1590 && is_gimple_min_invariant (vr
->min
))
1596 /* If OP has a value range with a single constant value return that,
1597 otherwise return NULL_TREE. This returns OP itself if OP is a
1601 op_with_constant_singleton_value_range (tree op
)
1603 if (is_gimple_min_invariant (op
))
1606 if (TREE_CODE (op
) != SSA_NAME
)
1609 return value_range_constant_singleton (get_value_range (op
));
1612 /* Return true if op is in a boolean [0, 1] value-range. */
1615 op_with_boolean_value_range_p (tree op
)
1619 if (TYPE_PRECISION (TREE_TYPE (op
)) == 1)
1622 if (integer_zerop (op
)
1623 || integer_onep (op
))
1626 if (TREE_CODE (op
) != SSA_NAME
)
1629 vr
= get_value_range (op
);
1630 return (vr
->type
== VR_RANGE
1631 && integer_zerop (vr
->min
)
1632 && integer_onep (vr
->max
));
1635 /* Extract value range information from an ASSERT_EXPR EXPR and store
1639 extract_range_from_assert (value_range_t
*vr_p
, tree expr
)
1641 tree var
, cond
, limit
, min
, max
, type
;
1642 value_range_t
*limit_vr
;
1643 enum tree_code cond_code
;
1645 var
= ASSERT_EXPR_VAR (expr
);
1646 cond
= ASSERT_EXPR_COND (expr
);
1648 gcc_assert (COMPARISON_CLASS_P (cond
));
1650 /* Find VAR in the ASSERT_EXPR conditional. */
1651 if (var
== TREE_OPERAND (cond
, 0)
1652 || TREE_CODE (TREE_OPERAND (cond
, 0)) == PLUS_EXPR
1653 || TREE_CODE (TREE_OPERAND (cond
, 0)) == NOP_EXPR
)
1655 /* If the predicate is of the form VAR COMP LIMIT, then we just
1656 take LIMIT from the RHS and use the same comparison code. */
1657 cond_code
= TREE_CODE (cond
);
1658 limit
= TREE_OPERAND (cond
, 1);
1659 cond
= TREE_OPERAND (cond
, 0);
1663 /* If the predicate is of the form LIMIT COMP VAR, then we need
1664 to flip around the comparison code to create the proper range
1666 cond_code
= swap_tree_comparison (TREE_CODE (cond
));
1667 limit
= TREE_OPERAND (cond
, 0);
1668 cond
= TREE_OPERAND (cond
, 1);
1671 limit
= avoid_overflow_infinity (limit
);
1673 type
= TREE_TYPE (var
);
1674 gcc_assert (limit
!= var
);
1676 /* For pointer arithmetic, we only keep track of pointer equality
1678 if (POINTER_TYPE_P (type
) && cond_code
!= NE_EXPR
&& cond_code
!= EQ_EXPR
)
1680 set_value_range_to_varying (vr_p
);
1684 /* If LIMIT is another SSA name and LIMIT has a range of its own,
1685 try to use LIMIT's range to avoid creating symbolic ranges
1687 limit_vr
= (TREE_CODE (limit
) == SSA_NAME
) ? get_value_range (limit
) : NULL
;
1689 /* LIMIT's range is only interesting if it has any useful information. */
1691 && (limit_vr
->type
== VR_UNDEFINED
1692 || limit_vr
->type
== VR_VARYING
1693 || symbolic_range_p (limit_vr
)))
1696 /* Initially, the new range has the same set of equivalences of
1697 VAR's range. This will be revised before returning the final
1698 value. Since assertions may be chained via mutually exclusive
1699 predicates, we will need to trim the set of equivalences before
1701 gcc_assert (vr_p
->equiv
== NULL
);
1702 add_equivalence (&vr_p
->equiv
, var
);
1704 /* Extract a new range based on the asserted comparison for VAR and
1705 LIMIT's value range. Notice that if LIMIT has an anti-range, we
1706 will only use it for equality comparisons (EQ_EXPR). For any
1707 other kind of assertion, we cannot derive a range from LIMIT's
1708 anti-range that can be used to describe the new range. For
1709 instance, ASSERT_EXPR <x_2, x_2 <= b_4>. If b_4 is ~[2, 10],
1710 then b_4 takes on the ranges [-INF, 1] and [11, +INF]. There is
1711 no single range for x_2 that could describe LE_EXPR, so we might
1712 as well build the range [b_4, +INF] for it.
1713 One special case we handle is extracting a range from a
1714 range test encoded as (unsigned)var + CST <= limit. */
1715 if (TREE_CODE (cond
) == NOP_EXPR
1716 || TREE_CODE (cond
) == PLUS_EXPR
)
1718 if (TREE_CODE (cond
) == PLUS_EXPR
)
1720 min
= fold_build1 (NEGATE_EXPR
, TREE_TYPE (TREE_OPERAND (cond
, 1)),
1721 TREE_OPERAND (cond
, 1));
1722 max
= int_const_binop (PLUS_EXPR
, limit
, min
);
1723 cond
= TREE_OPERAND (cond
, 0);
1727 min
= build_int_cst (TREE_TYPE (var
), 0);
1731 /* Make sure to not set TREE_OVERFLOW on the final type
1732 conversion. We are willingly interpreting large positive
1733 unsigned values as negative signed values here. */
1734 min
= force_fit_type (TREE_TYPE (var
), wi::to_widest (min
), 0, false);
1735 max
= force_fit_type (TREE_TYPE (var
), wi::to_widest (max
), 0, false);
1737 /* We can transform a max, min range to an anti-range or
1738 vice-versa. Use set_and_canonicalize_value_range which does
1740 if (cond_code
== LE_EXPR
)
1741 set_and_canonicalize_value_range (vr_p
, VR_RANGE
,
1742 min
, max
, vr_p
->equiv
);
1743 else if (cond_code
== GT_EXPR
)
1744 set_and_canonicalize_value_range (vr_p
, VR_ANTI_RANGE
,
1745 min
, max
, vr_p
->equiv
);
1749 else if (cond_code
== EQ_EXPR
)
1751 enum value_range_type range_type
;
1755 range_type
= limit_vr
->type
;
1756 min
= limit_vr
->min
;
1757 max
= limit_vr
->max
;
1761 range_type
= VR_RANGE
;
1766 set_value_range (vr_p
, range_type
, min
, max
, vr_p
->equiv
);
1768 /* When asserting the equality VAR == LIMIT and LIMIT is another
1769 SSA name, the new range will also inherit the equivalence set
1771 if (TREE_CODE (limit
) == SSA_NAME
)
1772 add_equivalence (&vr_p
->equiv
, limit
);
1774 else if (cond_code
== NE_EXPR
)
1776 /* As described above, when LIMIT's range is an anti-range and
1777 this assertion is an inequality (NE_EXPR), then we cannot
1778 derive anything from the anti-range. For instance, if
1779 LIMIT's range was ~[0, 0], the assertion 'VAR != LIMIT' does
1780 not imply that VAR's range is [0, 0]. So, in the case of
1781 anti-ranges, we just assert the inequality using LIMIT and
1784 If LIMIT_VR is a range, we can only use it to build a new
1785 anti-range if LIMIT_VR is a single-valued range. For
1786 instance, if LIMIT_VR is [0, 1], the predicate
1787 VAR != [0, 1] does not mean that VAR's range is ~[0, 1].
1788 Rather, it means that for value 0 VAR should be ~[0, 0]
1789 and for value 1, VAR should be ~[1, 1]. We cannot
1790 represent these ranges.
1792 The only situation in which we can build a valid
1793 anti-range is when LIMIT_VR is a single-valued range
1794 (i.e., LIMIT_VR->MIN == LIMIT_VR->MAX). In that case,
1795 build the anti-range ~[LIMIT_VR->MIN, LIMIT_VR->MAX]. */
1797 && limit_vr
->type
== VR_RANGE
1798 && compare_values (limit_vr
->min
, limit_vr
->max
) == 0)
1800 min
= limit_vr
->min
;
1801 max
= limit_vr
->max
;
1805 /* In any other case, we cannot use LIMIT's range to build a
1806 valid anti-range. */
1810 /* If MIN and MAX cover the whole range for their type, then
1811 just use the original LIMIT. */
1812 if (INTEGRAL_TYPE_P (type
)
1813 && vrp_val_is_min (min
)
1814 && vrp_val_is_max (max
))
1817 set_and_canonicalize_value_range (vr_p
, VR_ANTI_RANGE
,
1818 min
, max
, vr_p
->equiv
);
1820 else if (cond_code
== LE_EXPR
|| cond_code
== LT_EXPR
)
1822 min
= TYPE_MIN_VALUE (type
);
1824 if (limit_vr
== NULL
|| limit_vr
->type
== VR_ANTI_RANGE
)
1828 /* If LIMIT_VR is of the form [N1, N2], we need to build the
1829 range [MIN, N2] for LE_EXPR and [MIN, N2 - 1] for
1831 max
= limit_vr
->max
;
1834 /* If the maximum value forces us to be out of bounds, simply punt.
1835 It would be pointless to try and do anything more since this
1836 all should be optimized away above us. */
1837 if ((cond_code
== LT_EXPR
1838 && compare_values (max
, min
) == 0)
1839 || is_overflow_infinity (max
))
1840 set_value_range_to_varying (vr_p
);
1843 /* For LT_EXPR, we create the range [MIN, MAX - 1]. */
1844 if (cond_code
== LT_EXPR
)
1846 if (TYPE_PRECISION (TREE_TYPE (max
)) == 1
1847 && !TYPE_UNSIGNED (TREE_TYPE (max
)))
1848 max
= fold_build2 (PLUS_EXPR
, TREE_TYPE (max
), max
,
1849 build_int_cst (TREE_TYPE (max
), -1));
1851 max
= fold_build2 (MINUS_EXPR
, TREE_TYPE (max
), max
,
1852 build_int_cst (TREE_TYPE (max
), 1));
1854 TREE_NO_WARNING (max
) = 1;
1857 set_value_range (vr_p
, VR_RANGE
, min
, max
, vr_p
->equiv
);
1860 else if (cond_code
== GE_EXPR
|| cond_code
== GT_EXPR
)
1862 max
= TYPE_MAX_VALUE (type
);
1864 if (limit_vr
== NULL
|| limit_vr
->type
== VR_ANTI_RANGE
)
1868 /* If LIMIT_VR is of the form [N1, N2], we need to build the
1869 range [N1, MAX] for GE_EXPR and [N1 + 1, MAX] for
1871 min
= limit_vr
->min
;
1874 /* If the minimum value forces us to be out of bounds, simply punt.
1875 It would be pointless to try and do anything more since this
1876 all should be optimized away above us. */
1877 if ((cond_code
== GT_EXPR
1878 && compare_values (min
, max
) == 0)
1879 || is_overflow_infinity (min
))
1880 set_value_range_to_varying (vr_p
);
1883 /* For GT_EXPR, we create the range [MIN + 1, MAX]. */
1884 if (cond_code
== GT_EXPR
)
1886 if (TYPE_PRECISION (TREE_TYPE (min
)) == 1
1887 && !TYPE_UNSIGNED (TREE_TYPE (min
)))
1888 min
= fold_build2 (MINUS_EXPR
, TREE_TYPE (min
), min
,
1889 build_int_cst (TREE_TYPE (min
), -1));
1891 min
= fold_build2 (PLUS_EXPR
, TREE_TYPE (min
), min
,
1892 build_int_cst (TREE_TYPE (min
), 1));
1894 TREE_NO_WARNING (min
) = 1;
1897 set_value_range (vr_p
, VR_RANGE
, min
, max
, vr_p
->equiv
);
1903 /* Finally intersect the new range with what we already know about var. */
1904 vrp_intersect_ranges (vr_p
, get_value_range (var
));
1908 /* Extract range information from SSA name VAR and store it in VR. If
1909 VAR has an interesting range, use it. Otherwise, create the
1910 range [VAR, VAR] and return it. This is useful in situations where
1911 we may have conditionals testing values of VARYING names. For
1918 Even if y_5 is deemed VARYING, we can determine that x_3 > y_5 is
1922 extract_range_from_ssa_name (value_range_t
*vr
, tree var
)
1924 value_range_t
*var_vr
= get_value_range (var
);
1926 if (var_vr
->type
!= VR_VARYING
)
1927 copy_value_range (vr
, var_vr
);
1929 set_value_range (vr
, VR_RANGE
, var
, var
, NULL
);
1931 add_equivalence (&vr
->equiv
, var
);
1935 /* Wrapper around int_const_binop. If the operation overflows and we
1936 are not using wrapping arithmetic, then adjust the result to be
1937 -INF or +INF depending on CODE, VAL1 and VAL2. This can return
1938 NULL_TREE if we need to use an overflow infinity representation but
1939 the type does not support it. */
1942 vrp_int_const_binop (enum tree_code code
, tree val1
, tree val2
)
1946 res
= int_const_binop (code
, val1
, val2
);
1948 /* If we are using unsigned arithmetic, operate symbolically
1949 on -INF and +INF as int_const_binop only handles signed overflow. */
1950 if (TYPE_UNSIGNED (TREE_TYPE (val1
)))
1952 int checkz
= compare_values (res
, val1
);
1953 bool overflow
= false;
1955 /* Ensure that res = val1 [+*] val2 >= val1
1956 or that res = val1 - val2 <= val1. */
1957 if ((code
== PLUS_EXPR
1958 && !(checkz
== 1 || checkz
== 0))
1959 || (code
== MINUS_EXPR
1960 && !(checkz
== 0 || checkz
== -1)))
1964 /* Checking for multiplication overflow is done by dividing the
1965 output of the multiplication by the first input of the
1966 multiplication. If the result of that division operation is
1967 not equal to the second input of the multiplication, then the
1968 multiplication overflowed. */
1969 else if (code
== MULT_EXPR
&& !integer_zerop (val1
))
1971 tree tmp
= int_const_binop (TRUNC_DIV_EXPR
,
1974 int check
= compare_values (tmp
, val2
);
1982 res
= copy_node (res
);
1983 TREE_OVERFLOW (res
) = 1;
1987 else if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (val1
)))
1988 /* If the singed operation wraps then int_const_binop has done
1989 everything we want. */
1991 /* Signed division of -1/0 overflows and by the time it gets here
1992 returns NULL_TREE. */
1995 else if ((TREE_OVERFLOW (res
)
1996 && !TREE_OVERFLOW (val1
)
1997 && !TREE_OVERFLOW (val2
))
1998 || is_overflow_infinity (val1
)
1999 || is_overflow_infinity (val2
))
2001 /* If the operation overflowed but neither VAL1 nor VAL2 are
2002 overflown, return -INF or +INF depending on the operation
2003 and the combination of signs of the operands. */
2004 int sgn1
= tree_int_cst_sgn (val1
);
2005 int sgn2
= tree_int_cst_sgn (val2
);
2007 if (needs_overflow_infinity (TREE_TYPE (res
))
2008 && !supports_overflow_infinity (TREE_TYPE (res
)))
2011 /* We have to punt on adding infinities of different signs,
2012 since we can't tell what the sign of the result should be.
2013 Likewise for subtracting infinities of the same sign. */
2014 if (((code
== PLUS_EXPR
&& sgn1
!= sgn2
)
2015 || (code
== MINUS_EXPR
&& sgn1
== sgn2
))
2016 && is_overflow_infinity (val1
)
2017 && is_overflow_infinity (val2
))
2020 /* Don't try to handle division or shifting of infinities. */
2021 if ((code
== TRUNC_DIV_EXPR
2022 || code
== FLOOR_DIV_EXPR
2023 || code
== CEIL_DIV_EXPR
2024 || code
== EXACT_DIV_EXPR
2025 || code
== ROUND_DIV_EXPR
2026 || code
== RSHIFT_EXPR
)
2027 && (is_overflow_infinity (val1
)
2028 || is_overflow_infinity (val2
)))
2031 /* Notice that we only need to handle the restricted set of
2032 operations handled by extract_range_from_binary_expr.
2033 Among them, only multiplication, addition and subtraction
2034 can yield overflow without overflown operands because we
2035 are working with integral types only... except in the
2036 case VAL1 = -INF and VAL2 = -1 which overflows to +INF
2037 for division too. */
2039 /* For multiplication, the sign of the overflow is given
2040 by the comparison of the signs of the operands. */
2041 if ((code
== MULT_EXPR
&& sgn1
== sgn2
)
2042 /* For addition, the operands must be of the same sign
2043 to yield an overflow. Its sign is therefore that
2044 of one of the operands, for example the first. For
2045 infinite operands X + -INF is negative, not positive. */
2046 || (code
== PLUS_EXPR
2048 ? !is_negative_overflow_infinity (val2
)
2049 : is_positive_overflow_infinity (val2
)))
2050 /* For subtraction, non-infinite operands must be of
2051 different signs to yield an overflow. Its sign is
2052 therefore that of the first operand or the opposite of
2053 that of the second operand. A first operand of 0 counts
2054 as positive here, for the corner case 0 - (-INF), which
2055 overflows, but must yield +INF. For infinite operands 0
2056 - INF is negative, not positive. */
2057 || (code
== MINUS_EXPR
2059 ? !is_positive_overflow_infinity (val2
)
2060 : is_negative_overflow_infinity (val2
)))
2061 /* We only get in here with positive shift count, so the
2062 overflow direction is the same as the sign of val1.
2063 Actually rshift does not overflow at all, but we only
2064 handle the case of shifting overflowed -INF and +INF. */
2065 || (code
== RSHIFT_EXPR
2067 /* For division, the only case is -INF / -1 = +INF. */
2068 || code
== TRUNC_DIV_EXPR
2069 || code
== FLOOR_DIV_EXPR
2070 || code
== CEIL_DIV_EXPR
2071 || code
== EXACT_DIV_EXPR
2072 || code
== ROUND_DIV_EXPR
)
2073 return (needs_overflow_infinity (TREE_TYPE (res
))
2074 ? positive_overflow_infinity (TREE_TYPE (res
))
2075 : TYPE_MAX_VALUE (TREE_TYPE (res
)));
2077 return (needs_overflow_infinity (TREE_TYPE (res
))
2078 ? negative_overflow_infinity (TREE_TYPE (res
))
2079 : TYPE_MIN_VALUE (TREE_TYPE (res
)));
2086 /* For range VR compute two wide_int bitmasks. In *MAY_BE_NONZERO
2087 bitmask if some bit is unset, it means for all numbers in the range
2088 the bit is 0, otherwise it might be 0 or 1. In *MUST_BE_NONZERO
2089 bitmask if some bit is set, it means for all numbers in the range
2090 the bit is 1, otherwise it might be 0 or 1. */
2093 zero_nonzero_bits_from_vr (const tree expr_type
,
2095 wide_int
*may_be_nonzero
,
2096 wide_int
*must_be_nonzero
)
2098 *may_be_nonzero
= wi::minus_one (TYPE_PRECISION (expr_type
));
2099 *must_be_nonzero
= wi::zero (TYPE_PRECISION (expr_type
));
2100 if (!range_int_cst_p (vr
)
2101 || is_overflow_infinity (vr
->min
)
2102 || is_overflow_infinity (vr
->max
))
2105 if (range_int_cst_singleton_p (vr
))
2107 *may_be_nonzero
= vr
->min
;
2108 *must_be_nonzero
= *may_be_nonzero
;
2110 else if (tree_int_cst_sgn (vr
->min
) >= 0
2111 || tree_int_cst_sgn (vr
->max
) < 0)
2113 wide_int xor_mask
= wi::bit_xor (vr
->min
, vr
->max
);
2114 *may_be_nonzero
= wi::bit_or (vr
->min
, vr
->max
);
2115 *must_be_nonzero
= wi::bit_and (vr
->min
, vr
->max
);
2118 wide_int mask
= wi::mask (wi::floor_log2 (xor_mask
), false,
2119 may_be_nonzero
->get_precision ());
2120 *may_be_nonzero
= *may_be_nonzero
| mask
;
2121 *must_be_nonzero
= must_be_nonzero
->and_not (mask
);
2128 /* Create two value-ranges in *VR0 and *VR1 from the anti-range *AR
2129 so that *VR0 U *VR1 == *AR. Returns true if that is possible,
2130 false otherwise. If *AR can be represented with a single range
2131 *VR1 will be VR_UNDEFINED. */
2134 ranges_from_anti_range (value_range_t
*ar
,
2135 value_range_t
*vr0
, value_range_t
*vr1
)
2137 tree type
= TREE_TYPE (ar
->min
);
2139 vr0
->type
= VR_UNDEFINED
;
2140 vr1
->type
= VR_UNDEFINED
;
2142 if (ar
->type
!= VR_ANTI_RANGE
2143 || TREE_CODE (ar
->min
) != INTEGER_CST
2144 || TREE_CODE (ar
->max
) != INTEGER_CST
2145 || !vrp_val_min (type
)
2146 || !vrp_val_max (type
))
2149 if (!vrp_val_is_min (ar
->min
))
2151 vr0
->type
= VR_RANGE
;
2152 vr0
->min
= vrp_val_min (type
);
2153 vr0
->max
= wide_int_to_tree (type
, wi::sub (ar
->min
, 1));
2155 if (!vrp_val_is_max (ar
->max
))
2157 vr1
->type
= VR_RANGE
;
2158 vr1
->min
= wide_int_to_tree (type
, wi::add (ar
->max
, 1));
2159 vr1
->max
= vrp_val_max (type
);
2161 if (vr0
->type
== VR_UNDEFINED
)
2164 vr1
->type
= VR_UNDEFINED
;
2167 return vr0
->type
!= VR_UNDEFINED
;
2170 /* Helper to extract a value-range *VR for a multiplicative operation
2174 extract_range_from_multiplicative_op_1 (value_range_t
*vr
,
2175 enum tree_code code
,
2176 value_range_t
*vr0
, value_range_t
*vr1
)
2178 enum value_range_type type
;
2185 /* Multiplications, divisions and shifts are a bit tricky to handle,
2186 depending on the mix of signs we have in the two ranges, we
2187 need to operate on different values to get the minimum and
2188 maximum values for the new range. One approach is to figure
2189 out all the variations of range combinations and do the
2192 However, this involves several calls to compare_values and it
2193 is pretty convoluted. It's simpler to do the 4 operations
2194 (MIN0 OP MIN1, MIN0 OP MAX1, MAX0 OP MIN1 and MAX0 OP MAX0 OP
2195 MAX1) and then figure the smallest and largest values to form
2197 gcc_assert (code
== MULT_EXPR
2198 || code
== TRUNC_DIV_EXPR
2199 || code
== FLOOR_DIV_EXPR
2200 || code
== CEIL_DIV_EXPR
2201 || code
== EXACT_DIV_EXPR
2202 || code
== ROUND_DIV_EXPR
2203 || code
== RSHIFT_EXPR
2204 || code
== LSHIFT_EXPR
);
2205 gcc_assert ((vr0
->type
== VR_RANGE
2206 || (code
== MULT_EXPR
&& vr0
->type
== VR_ANTI_RANGE
))
2207 && vr0
->type
== vr1
->type
);
2211 /* Compute the 4 cross operations. */
2213 val
[0] = vrp_int_const_binop (code
, vr0
->min
, vr1
->min
);
2214 if (val
[0] == NULL_TREE
)
2217 if (vr1
->max
== vr1
->min
)
2221 val
[1] = vrp_int_const_binop (code
, vr0
->min
, vr1
->max
);
2222 if (val
[1] == NULL_TREE
)
2226 if (vr0
->max
== vr0
->min
)
2230 val
[2] = vrp_int_const_binop (code
, vr0
->max
, vr1
->min
);
2231 if (val
[2] == NULL_TREE
)
2235 if (vr0
->min
== vr0
->max
|| vr1
->min
== vr1
->max
)
2239 val
[3] = vrp_int_const_binop (code
, vr0
->max
, vr1
->max
);
2240 if (val
[3] == NULL_TREE
)
2246 set_value_range_to_varying (vr
);
2250 /* Set MIN to the minimum of VAL[i] and MAX to the maximum
2254 for (i
= 1; i
< 4; i
++)
2256 if (!is_gimple_min_invariant (min
)
2257 || (TREE_OVERFLOW (min
) && !is_overflow_infinity (min
))
2258 || !is_gimple_min_invariant (max
)
2259 || (TREE_OVERFLOW (max
) && !is_overflow_infinity (max
)))
2264 if (!is_gimple_min_invariant (val
[i
])
2265 || (TREE_OVERFLOW (val
[i
])
2266 && !is_overflow_infinity (val
[i
])))
2268 /* If we found an overflowed value, set MIN and MAX
2269 to it so that we set the resulting range to
2275 if (compare_values (val
[i
], min
) == -1)
2278 if (compare_values (val
[i
], max
) == 1)
2283 /* If either MIN or MAX overflowed, then set the resulting range to
2284 VARYING. But we do accept an overflow infinity
2286 if (min
== NULL_TREE
2287 || !is_gimple_min_invariant (min
)
2288 || (TREE_OVERFLOW (min
) && !is_overflow_infinity (min
))
2290 || !is_gimple_min_invariant (max
)
2291 || (TREE_OVERFLOW (max
) && !is_overflow_infinity (max
)))
2293 set_value_range_to_varying (vr
);
2299 2) [-INF, +-INF(OVF)]
2300 3) [+-INF(OVF), +INF]
2301 4) [+-INF(OVF), +-INF(OVF)]
2302 We learn nothing when we have INF and INF(OVF) on both sides.
2303 Note that we do accept [-INF, -INF] and [+INF, +INF] without
2305 if ((vrp_val_is_min (min
) || is_overflow_infinity (min
))
2306 && (vrp_val_is_max (max
) || is_overflow_infinity (max
)))
2308 set_value_range_to_varying (vr
);
2312 cmp
= compare_values (min
, max
);
2313 if (cmp
== -2 || cmp
== 1)
2315 /* If the new range has its limits swapped around (MIN > MAX),
2316 then the operation caused one of them to wrap around, mark
2317 the new range VARYING. */
2318 set_value_range_to_varying (vr
);
2321 set_value_range (vr
, type
, min
, max
, NULL
);
2324 /* Extract range information from a binary operation CODE based on
2325 the ranges of each of its operands *VR0 and *VR1 with resulting
2326 type EXPR_TYPE. The resulting range is stored in *VR. */
2329 extract_range_from_binary_expr_1 (value_range_t
*vr
,
2330 enum tree_code code
, tree expr_type
,
2331 value_range_t
*vr0_
, value_range_t
*vr1_
)
2333 value_range_t vr0
= *vr0_
, vr1
= *vr1_
;
2334 value_range_t vrtem0
= VR_INITIALIZER
, vrtem1
= VR_INITIALIZER
;
2335 enum value_range_type type
;
2336 tree min
= NULL_TREE
, max
= NULL_TREE
;
2339 if (!INTEGRAL_TYPE_P (expr_type
)
2340 && !POINTER_TYPE_P (expr_type
))
2342 set_value_range_to_varying (vr
);
2346 /* Not all binary expressions can be applied to ranges in a
2347 meaningful way. Handle only arithmetic operations. */
2348 if (code
!= PLUS_EXPR
2349 && code
!= MINUS_EXPR
2350 && code
!= POINTER_PLUS_EXPR
2351 && code
!= MULT_EXPR
2352 && code
!= TRUNC_DIV_EXPR
2353 && code
!= FLOOR_DIV_EXPR
2354 && code
!= CEIL_DIV_EXPR
2355 && code
!= EXACT_DIV_EXPR
2356 && code
!= ROUND_DIV_EXPR
2357 && code
!= TRUNC_MOD_EXPR
2358 && code
!= RSHIFT_EXPR
2359 && code
!= LSHIFT_EXPR
2362 && code
!= BIT_AND_EXPR
2363 && code
!= BIT_IOR_EXPR
2364 && code
!= BIT_XOR_EXPR
)
2366 set_value_range_to_varying (vr
);
2370 /* If both ranges are UNDEFINED, so is the result. */
2371 if (vr0
.type
== VR_UNDEFINED
&& vr1
.type
== VR_UNDEFINED
)
2373 set_value_range_to_undefined (vr
);
2376 /* If one of the ranges is UNDEFINED drop it to VARYING for the following
2377 code. At some point we may want to special-case operations that
2378 have UNDEFINED result for all or some value-ranges of the not UNDEFINED
2380 else if (vr0
.type
== VR_UNDEFINED
)
2381 set_value_range_to_varying (&vr0
);
2382 else if (vr1
.type
== VR_UNDEFINED
)
2383 set_value_range_to_varying (&vr1
);
2385 /* Now canonicalize anti-ranges to ranges when they are not symbolic
2386 and express ~[] op X as ([]' op X) U ([]'' op X). */
2387 if (vr0
.type
== VR_ANTI_RANGE
2388 && ranges_from_anti_range (&vr0
, &vrtem0
, &vrtem1
))
2390 extract_range_from_binary_expr_1 (vr
, code
, expr_type
, &vrtem0
, vr1_
);
2391 if (vrtem1
.type
!= VR_UNDEFINED
)
2393 value_range_t vrres
= VR_INITIALIZER
;
2394 extract_range_from_binary_expr_1 (&vrres
, code
, expr_type
,
2396 vrp_meet (vr
, &vrres
);
2400 /* Likewise for X op ~[]. */
2401 if (vr1
.type
== VR_ANTI_RANGE
2402 && ranges_from_anti_range (&vr1
, &vrtem0
, &vrtem1
))
2404 extract_range_from_binary_expr_1 (vr
, code
, expr_type
, vr0_
, &vrtem0
);
2405 if (vrtem1
.type
!= VR_UNDEFINED
)
2407 value_range_t vrres
= VR_INITIALIZER
;
2408 extract_range_from_binary_expr_1 (&vrres
, code
, expr_type
,
2410 vrp_meet (vr
, &vrres
);
2415 /* The type of the resulting value range defaults to VR0.TYPE. */
2418 /* Refuse to operate on VARYING ranges, ranges of different kinds
2419 and symbolic ranges. As an exception, we allow BIT_{AND,IOR}
2420 because we may be able to derive a useful range even if one of
2421 the operands is VR_VARYING or symbolic range. Similarly for
2422 divisions, MIN/MAX and PLUS/MINUS.
2424 TODO, we may be able to derive anti-ranges in some cases. */
2425 if (code
!= BIT_AND_EXPR
2426 && code
!= BIT_IOR_EXPR
2427 && code
!= TRUNC_DIV_EXPR
2428 && code
!= FLOOR_DIV_EXPR
2429 && code
!= CEIL_DIV_EXPR
2430 && code
!= EXACT_DIV_EXPR
2431 && code
!= ROUND_DIV_EXPR
2432 && code
!= TRUNC_MOD_EXPR
2435 && code
!= PLUS_EXPR
2436 && code
!= MINUS_EXPR
2437 && (vr0
.type
== VR_VARYING
2438 || vr1
.type
== VR_VARYING
2439 || vr0
.type
!= vr1
.type
2440 || symbolic_range_p (&vr0
)
2441 || symbolic_range_p (&vr1
)))
2443 set_value_range_to_varying (vr
);
2447 /* Now evaluate the expression to determine the new range. */
2448 if (POINTER_TYPE_P (expr_type
))
2450 if (code
== MIN_EXPR
|| code
== MAX_EXPR
)
2452 /* For MIN/MAX expressions with pointers, we only care about
2453 nullness, if both are non null, then the result is nonnull.
2454 If both are null, then the result is null. Otherwise they
2456 if (range_is_nonnull (&vr0
) && range_is_nonnull (&vr1
))
2457 set_value_range_to_nonnull (vr
, expr_type
);
2458 else if (range_is_null (&vr0
) && range_is_null (&vr1
))
2459 set_value_range_to_null (vr
, expr_type
);
2461 set_value_range_to_varying (vr
);
2463 else if (code
== POINTER_PLUS_EXPR
)
2465 /* For pointer types, we are really only interested in asserting
2466 whether the expression evaluates to non-NULL. */
2467 if (range_is_nonnull (&vr0
) || range_is_nonnull (&vr1
))
2468 set_value_range_to_nonnull (vr
, expr_type
);
2469 else if (range_is_null (&vr0
) && range_is_null (&vr1
))
2470 set_value_range_to_null (vr
, expr_type
);
2472 set_value_range_to_varying (vr
);
2474 else if (code
== BIT_AND_EXPR
)
2476 /* For pointer types, we are really only interested in asserting
2477 whether the expression evaluates to non-NULL. */
2478 if (range_is_nonnull (&vr0
) && range_is_nonnull (&vr1
))
2479 set_value_range_to_nonnull (vr
, expr_type
);
2480 else if (range_is_null (&vr0
) || range_is_null (&vr1
))
2481 set_value_range_to_null (vr
, expr_type
);
2483 set_value_range_to_varying (vr
);
2486 set_value_range_to_varying (vr
);
2491 /* For integer ranges, apply the operation to each end of the
2492 range and see what we end up with. */
2493 if (code
== PLUS_EXPR
|| code
== MINUS_EXPR
)
2495 const bool minus_p
= (code
== MINUS_EXPR
);
2496 tree min_op0
= vr0
.min
;
2497 tree min_op1
= minus_p
? vr1
.max
: vr1
.min
;
2498 tree max_op0
= vr0
.max
;
2499 tree max_op1
= minus_p
? vr1
.min
: vr1
.max
;
2500 tree sym_min_op0
= NULL_TREE
;
2501 tree sym_min_op1
= NULL_TREE
;
2502 tree sym_max_op0
= NULL_TREE
;
2503 tree sym_max_op1
= NULL_TREE
;
2504 bool neg_min_op0
, neg_min_op1
, neg_max_op0
, neg_max_op1
;
2506 /* If we have a PLUS or MINUS with two VR_RANGEs, either constant or
2507 single-symbolic ranges, try to compute the precise resulting range,
2508 but only if we know that this resulting range will also be constant
2509 or single-symbolic. */
2510 if (vr0
.type
== VR_RANGE
&& vr1
.type
== VR_RANGE
2511 && (TREE_CODE (min_op0
) == INTEGER_CST
2513 = get_single_symbol (min_op0
, &neg_min_op0
, &min_op0
)))
2514 && (TREE_CODE (min_op1
) == INTEGER_CST
2516 = get_single_symbol (min_op1
, &neg_min_op1
, &min_op1
)))
2517 && (!(sym_min_op0
&& sym_min_op1
)
2518 || (sym_min_op0
== sym_min_op1
2519 && neg_min_op0
== (minus_p
? neg_min_op1
: !neg_min_op1
)))
2520 && (TREE_CODE (max_op0
) == INTEGER_CST
2522 = get_single_symbol (max_op0
, &neg_max_op0
, &max_op0
)))
2523 && (TREE_CODE (max_op1
) == INTEGER_CST
2525 = get_single_symbol (max_op1
, &neg_max_op1
, &max_op1
)))
2526 && (!(sym_max_op0
&& sym_max_op1
)
2527 || (sym_max_op0
== sym_max_op1
2528 && neg_max_op0
== (minus_p
? neg_max_op1
: !neg_max_op1
))))
2530 const signop sgn
= TYPE_SIGN (expr_type
);
2531 const unsigned int prec
= TYPE_PRECISION (expr_type
);
2532 wide_int type_min
, type_max
, wmin
, wmax
;
2536 /* Get the lower and upper bounds of the type. */
2537 if (TYPE_OVERFLOW_WRAPS (expr_type
))
2539 type_min
= wi::min_value (prec
, sgn
);
2540 type_max
= wi::max_value (prec
, sgn
);
2544 type_min
= vrp_val_min (expr_type
);
2545 type_max
= vrp_val_max (expr_type
);
2548 /* Combine the lower bounds, if any. */
2549 if (min_op0
&& min_op1
)
2553 wmin
= wi::sub (min_op0
, min_op1
);
2555 /* Check for overflow. */
2556 if (wi::cmp (0, min_op1
, sgn
)
2557 != wi::cmp (wmin
, min_op0
, sgn
))
2558 min_ovf
= wi::cmp (min_op0
, min_op1
, sgn
);
2562 wmin
= wi::add (min_op0
, min_op1
);
2564 /* Check for overflow. */
2565 if (wi::cmp (min_op1
, 0, sgn
)
2566 != wi::cmp (wmin
, min_op0
, sgn
))
2567 min_ovf
= wi::cmp (min_op0
, wmin
, sgn
);
2573 wmin
= minus_p
? wi::neg (min_op1
) : min_op1
;
2575 wmin
= wi::shwi (0, prec
);
2577 /* Combine the upper bounds, if any. */
2578 if (max_op0
&& max_op1
)
2582 wmax
= wi::sub (max_op0
, max_op1
);
2584 /* Check for overflow. */
2585 if (wi::cmp (0, max_op1
, sgn
)
2586 != wi::cmp (wmax
, max_op0
, sgn
))
2587 max_ovf
= wi::cmp (max_op0
, max_op1
, sgn
);
2591 wmax
= wi::add (max_op0
, max_op1
);
2593 if (wi::cmp (max_op1
, 0, sgn
)
2594 != wi::cmp (wmax
, max_op0
, sgn
))
2595 max_ovf
= wi::cmp (max_op0
, wmax
, sgn
);
2601 wmax
= minus_p
? wi::neg (max_op1
) : max_op1
;
2603 wmax
= wi::shwi (0, prec
);
2605 /* Check for type overflow. */
2608 if (wi::cmp (wmin
, type_min
, sgn
) == -1)
2610 else if (wi::cmp (wmin
, type_max
, sgn
) == 1)
2615 if (wi::cmp (wmax
, type_min
, sgn
) == -1)
2617 else if (wi::cmp (wmax
, type_max
, sgn
) == 1)
2621 /* If we have overflow for the constant part and the resulting
2622 range will be symbolic, drop to VR_VARYING. */
2623 if ((min_ovf
&& sym_min_op0
!= sym_min_op1
)
2624 || (max_ovf
&& sym_max_op0
!= sym_max_op1
))
2626 set_value_range_to_varying (vr
);
2630 if (TYPE_OVERFLOW_WRAPS (expr_type
))
2632 /* If overflow wraps, truncate the values and adjust the
2633 range kind and bounds appropriately. */
2634 wide_int tmin
= wide_int::from (wmin
, prec
, sgn
);
2635 wide_int tmax
= wide_int::from (wmax
, prec
, sgn
);
2636 if (min_ovf
== max_ovf
)
2638 /* No overflow or both overflow or underflow. The
2639 range kind stays VR_RANGE. */
2640 min
= wide_int_to_tree (expr_type
, tmin
);
2641 max
= wide_int_to_tree (expr_type
, tmax
);
2643 else if (min_ovf
== -1 && max_ovf
== 1)
2645 /* Underflow and overflow, drop to VR_VARYING. */
2646 set_value_range_to_varying (vr
);
2651 /* Min underflow or max overflow. The range kind
2652 changes to VR_ANTI_RANGE. */
2653 bool covers
= false;
2654 wide_int tem
= tmin
;
2655 gcc_assert ((min_ovf
== -1 && max_ovf
== 0)
2656 || (max_ovf
== 1 && min_ovf
== 0));
2657 type
= VR_ANTI_RANGE
;
2659 if (wi::cmp (tmin
, tmax
, sgn
) < 0)
2662 if (wi::cmp (tmax
, tem
, sgn
) > 0)
2664 /* If the anti-range would cover nothing, drop to varying.
2665 Likewise if the anti-range bounds are outside of the
2667 if (covers
|| wi::cmp (tmin
, tmax
, sgn
) > 0)
2669 set_value_range_to_varying (vr
);
2672 min
= wide_int_to_tree (expr_type
, tmin
);
2673 max
= wide_int_to_tree (expr_type
, tmax
);
2678 /* If overflow does not wrap, saturate to the types min/max
2682 if (needs_overflow_infinity (expr_type
)
2683 && supports_overflow_infinity (expr_type
))
2684 min
= negative_overflow_infinity (expr_type
);
2686 min
= wide_int_to_tree (expr_type
, type_min
);
2688 else if (min_ovf
== 1)
2690 if (needs_overflow_infinity (expr_type
)
2691 && supports_overflow_infinity (expr_type
))
2692 min
= positive_overflow_infinity (expr_type
);
2694 min
= wide_int_to_tree (expr_type
, type_max
);
2697 min
= wide_int_to_tree (expr_type
, wmin
);
2701 if (needs_overflow_infinity (expr_type
)
2702 && supports_overflow_infinity (expr_type
))
2703 max
= negative_overflow_infinity (expr_type
);
2705 max
= wide_int_to_tree (expr_type
, type_min
);
2707 else if (max_ovf
== 1)
2709 if (needs_overflow_infinity (expr_type
)
2710 && supports_overflow_infinity (expr_type
))
2711 max
= positive_overflow_infinity (expr_type
);
2713 max
= wide_int_to_tree (expr_type
, type_max
);
2716 max
= wide_int_to_tree (expr_type
, wmax
);
2719 if (needs_overflow_infinity (expr_type
)
2720 && supports_overflow_infinity (expr_type
))
2722 if ((min_op0
&& is_negative_overflow_infinity (min_op0
))
2725 ? is_positive_overflow_infinity (min_op1
)
2726 : is_negative_overflow_infinity (min_op1
))))
2727 min
= negative_overflow_infinity (expr_type
);
2728 if ((max_op0
&& is_positive_overflow_infinity (max_op0
))
2731 ? is_negative_overflow_infinity (max_op1
)
2732 : is_positive_overflow_infinity (max_op1
))))
2733 max
= positive_overflow_infinity (expr_type
);
2736 /* If the result lower bound is constant, we're done;
2737 otherwise, build the symbolic lower bound. */
2738 if (sym_min_op0
== sym_min_op1
)
2740 else if (sym_min_op0
)
2741 min
= build_symbolic_expr (expr_type
, sym_min_op0
,
2743 else if (sym_min_op1
)
2744 min
= build_symbolic_expr (expr_type
, sym_min_op1
,
2745 neg_min_op1
^ minus_p
, min
);
2747 /* Likewise for the upper bound. */
2748 if (sym_max_op0
== sym_max_op1
)
2750 else if (sym_max_op0
)
2751 max
= build_symbolic_expr (expr_type
, sym_max_op0
,
2753 else if (sym_max_op1
)
2754 max
= build_symbolic_expr (expr_type
, sym_max_op1
,
2755 neg_max_op1
^ minus_p
, max
);
2759 /* For other cases, for example if we have a PLUS_EXPR with two
2760 VR_ANTI_RANGEs, drop to VR_VARYING. It would take more effort
2761 to compute a precise range for such a case.
2762 ??? General even mixed range kind operations can be expressed
2763 by for example transforming ~[3, 5] + [1, 2] to range-only
2764 operations and a union primitive:
2765 [-INF, 2] + [1, 2] U [5, +INF] + [1, 2]
2766 [-INF+1, 4] U [6, +INF(OVF)]
2767 though usually the union is not exactly representable with
2768 a single range or anti-range as the above is
2769 [-INF+1, +INF(OVF)] intersected with ~[5, 5]
2770 but one could use a scheme similar to equivalences for this. */
2771 set_value_range_to_varying (vr
);
2775 else if (code
== MIN_EXPR
2776 || code
== MAX_EXPR
)
2778 if (vr0
.type
== VR_RANGE
2779 && !symbolic_range_p (&vr0
))
2782 if (vr1
.type
== VR_RANGE
2783 && !symbolic_range_p (&vr1
))
2785 /* For operations that make the resulting range directly
2786 proportional to the original ranges, apply the operation to
2787 the same end of each range. */
2788 min
= vrp_int_const_binop (code
, vr0
.min
, vr1
.min
);
2789 max
= vrp_int_const_binop (code
, vr0
.max
, vr1
.max
);
2791 else if (code
== MIN_EXPR
)
2793 min
= vrp_val_min (expr_type
);
2796 else if (code
== MAX_EXPR
)
2799 max
= vrp_val_max (expr_type
);
2802 else if (vr1
.type
== VR_RANGE
2803 && !symbolic_range_p (&vr1
))
2806 if (code
== MIN_EXPR
)
2808 min
= vrp_val_min (expr_type
);
2811 else if (code
== MAX_EXPR
)
2814 max
= vrp_val_max (expr_type
);
2819 set_value_range_to_varying (vr
);
2823 else if (code
== MULT_EXPR
)
2825 /* Fancy code so that with unsigned, [-3,-1]*[-3,-1] does not
2826 drop to varying. This test requires 2*prec bits if both
2827 operands are signed and 2*prec + 2 bits if either is not. */
2829 signop sign
= TYPE_SIGN (expr_type
);
2830 unsigned int prec
= TYPE_PRECISION (expr_type
);
2832 if (range_int_cst_p (&vr0
)
2833 && range_int_cst_p (&vr1
)
2834 && TYPE_OVERFLOW_WRAPS (expr_type
))
2836 typedef FIXED_WIDE_INT (WIDE_INT_MAX_PRECISION
* 2) vrp_int
;
2837 typedef generic_wide_int
2838 <wi::extended_tree
<WIDE_INT_MAX_PRECISION
* 2> > vrp_int_cst
;
2839 vrp_int sizem1
= wi::mask
<vrp_int
> (prec
, false);
2840 vrp_int size
= sizem1
+ 1;
2842 /* Extend the values using the sign of the result to PREC2.
2843 From here on out, everthing is just signed math no matter
2844 what the input types were. */
2845 vrp_int min0
= vrp_int_cst (vr0
.min
);
2846 vrp_int max0
= vrp_int_cst (vr0
.max
);
2847 vrp_int min1
= vrp_int_cst (vr1
.min
);
2848 vrp_int max1
= vrp_int_cst (vr1
.max
);
2849 /* Canonicalize the intervals. */
2850 if (sign
== UNSIGNED
)
2852 if (wi::ltu_p (size
, min0
+ max0
))
2858 if (wi::ltu_p (size
, min1
+ max1
))
2865 vrp_int prod0
= min0
* min1
;
2866 vrp_int prod1
= min0
* max1
;
2867 vrp_int prod2
= max0
* min1
;
2868 vrp_int prod3
= max0
* max1
;
2870 /* Sort the 4 products so that min is in prod0 and max is in
2872 /* min0min1 > max0max1 */
2873 if (wi::gts_p (prod0
, prod3
))
2875 vrp_int tmp
= prod3
;
2880 /* min0max1 > max0min1 */
2881 if (wi::gts_p (prod1
, prod2
))
2883 vrp_int tmp
= prod2
;
2888 if (wi::gts_p (prod0
, prod1
))
2890 vrp_int tmp
= prod1
;
2895 if (wi::gts_p (prod2
, prod3
))
2897 vrp_int tmp
= prod3
;
2902 /* diff = max - min. */
2903 prod2
= prod3
- prod0
;
2904 if (wi::geu_p (prod2
, sizem1
))
2906 /* the range covers all values. */
2907 set_value_range_to_varying (vr
);
2911 /* The following should handle the wrapping and selecting
2912 VR_ANTI_RANGE for us. */
2913 min
= wide_int_to_tree (expr_type
, prod0
);
2914 max
= wide_int_to_tree (expr_type
, prod3
);
2915 set_and_canonicalize_value_range (vr
, VR_RANGE
, min
, max
, NULL
);
2919 /* If we have an unsigned MULT_EXPR with two VR_ANTI_RANGEs,
2920 drop to VR_VARYING. It would take more effort to compute a
2921 precise range for such a case. For example, if we have
2922 op0 == 65536 and op1 == 65536 with their ranges both being
2923 ~[0,0] on a 32-bit machine, we would have op0 * op1 == 0, so
2924 we cannot claim that the product is in ~[0,0]. Note that we
2925 are guaranteed to have vr0.type == vr1.type at this
2927 if (vr0
.type
== VR_ANTI_RANGE
2928 && !TYPE_OVERFLOW_UNDEFINED (expr_type
))
2930 set_value_range_to_varying (vr
);
2934 extract_range_from_multiplicative_op_1 (vr
, code
, &vr0
, &vr1
);
2937 else if (code
== RSHIFT_EXPR
2938 || code
== LSHIFT_EXPR
)
2940 /* If we have a RSHIFT_EXPR with any shift values outside [0..prec-1],
2941 then drop to VR_VARYING. Outside of this range we get undefined
2942 behavior from the shift operation. We cannot even trust
2943 SHIFT_COUNT_TRUNCATED at this stage, because that applies to rtl
2944 shifts, and the operation at the tree level may be widened. */
2945 if (range_int_cst_p (&vr1
)
2946 && compare_tree_int (vr1
.min
, 0) >= 0
2947 && compare_tree_int (vr1
.max
, TYPE_PRECISION (expr_type
)) == -1)
2949 if (code
== RSHIFT_EXPR
)
2951 extract_range_from_multiplicative_op_1 (vr
, code
, &vr0
, &vr1
);
2954 /* We can map lshifts by constants to MULT_EXPR handling. */
2955 else if (code
== LSHIFT_EXPR
2956 && range_int_cst_singleton_p (&vr1
))
2958 bool saved_flag_wrapv
;
2959 value_range_t vr1p
= VR_INITIALIZER
;
2960 vr1p
.type
= VR_RANGE
;
2961 vr1p
.min
= (wide_int_to_tree
2963 wi::set_bit_in_zero (tree_to_shwi (vr1
.min
),
2964 TYPE_PRECISION (expr_type
))));
2965 vr1p
.max
= vr1p
.min
;
2966 /* We have to use a wrapping multiply though as signed overflow
2967 on lshifts is implementation defined in C89. */
2968 saved_flag_wrapv
= flag_wrapv
;
2970 extract_range_from_binary_expr_1 (vr
, MULT_EXPR
, expr_type
,
2972 flag_wrapv
= saved_flag_wrapv
;
2975 else if (code
== LSHIFT_EXPR
2976 && range_int_cst_p (&vr0
))
2978 int prec
= TYPE_PRECISION (expr_type
);
2979 int overflow_pos
= prec
;
2981 wide_int low_bound
, high_bound
;
2982 bool uns
= TYPE_UNSIGNED (expr_type
);
2983 bool in_bounds
= false;
2988 bound_shift
= overflow_pos
- tree_to_shwi (vr1
.max
);
2989 /* If bound_shift == HOST_BITS_PER_WIDE_INT, the llshift can
2990 overflow. However, for that to happen, vr1.max needs to be
2991 zero, which means vr1 is a singleton range of zero, which
2992 means it should be handled by the previous LSHIFT_EXPR
2994 wide_int bound
= wi::set_bit_in_zero (bound_shift
, prec
);
2995 wide_int complement
= ~(bound
- 1);
3000 high_bound
= complement
;
3001 if (wi::ltu_p (vr0
.max
, low_bound
))
3003 /* [5, 6] << [1, 2] == [10, 24]. */
3004 /* We're shifting out only zeroes, the value increases
3008 else if (wi::ltu_p (high_bound
, vr0
.min
))
3010 /* [0xffffff00, 0xffffffff] << [1, 2]
3011 == [0xfffffc00, 0xfffffffe]. */
3012 /* We're shifting out only ones, the value decreases
3019 /* [-1, 1] << [1, 2] == [-4, 4]. */
3020 low_bound
= complement
;
3022 if (wi::lts_p (vr0
.max
, high_bound
)
3023 && wi::lts_p (low_bound
, vr0
.min
))
3025 /* For non-negative numbers, we're shifting out only
3026 zeroes, the value increases monotonically.
3027 For negative numbers, we're shifting out only ones, the
3028 value decreases monotomically. */
3035 extract_range_from_multiplicative_op_1 (vr
, code
, &vr0
, &vr1
);
3040 set_value_range_to_varying (vr
);
3043 else if (code
== TRUNC_DIV_EXPR
3044 || code
== FLOOR_DIV_EXPR
3045 || code
== CEIL_DIV_EXPR
3046 || code
== EXACT_DIV_EXPR
3047 || code
== ROUND_DIV_EXPR
)
3049 if (vr0
.type
!= VR_RANGE
|| symbolic_range_p (&vr0
))
3051 /* For division, if op1 has VR_RANGE but op0 does not, something
3052 can be deduced just from that range. Say [min, max] / [4, max]
3053 gives [min / 4, max / 4] range. */
3054 if (vr1
.type
== VR_RANGE
3055 && !symbolic_range_p (&vr1
)
3056 && range_includes_zero_p (vr1
.min
, vr1
.max
) == 0)
3058 vr0
.type
= type
= VR_RANGE
;
3059 vr0
.min
= vrp_val_min (expr_type
);
3060 vr0
.max
= vrp_val_max (expr_type
);
3064 set_value_range_to_varying (vr
);
3069 /* For divisions, if flag_non_call_exceptions is true, we must
3070 not eliminate a division by zero. */
3071 if (cfun
->can_throw_non_call_exceptions
3072 && (vr1
.type
!= VR_RANGE
3073 || range_includes_zero_p (vr1
.min
, vr1
.max
) != 0))
3075 set_value_range_to_varying (vr
);
3079 /* For divisions, if op0 is VR_RANGE, we can deduce a range
3080 even if op1 is VR_VARYING, VR_ANTI_RANGE, symbolic or can
3082 if (vr0
.type
== VR_RANGE
3083 && (vr1
.type
!= VR_RANGE
3084 || range_includes_zero_p (vr1
.min
, vr1
.max
) != 0))
3086 tree zero
= build_int_cst (TREE_TYPE (vr0
.min
), 0);
3091 if (TYPE_UNSIGNED (expr_type
)
3092 || value_range_nonnegative_p (&vr1
))
3094 /* For unsigned division or when divisor is known
3095 to be non-negative, the range has to cover
3096 all numbers from 0 to max for positive max
3097 and all numbers from min to 0 for negative min. */
3098 cmp
= compare_values (vr0
.max
, zero
);
3101 else if (cmp
== 0 || cmp
== 1)
3105 cmp
= compare_values (vr0
.min
, zero
);
3108 else if (cmp
== 0 || cmp
== -1)
3115 /* Otherwise the range is -max .. max or min .. -min
3116 depending on which bound is bigger in absolute value,
3117 as the division can change the sign. */
3118 abs_extent_range (vr
, vr0
.min
, vr0
.max
);
3121 if (type
== VR_VARYING
)
3123 set_value_range_to_varying (vr
);
3129 extract_range_from_multiplicative_op_1 (vr
, code
, &vr0
, &vr1
);
3133 else if (code
== TRUNC_MOD_EXPR
)
3135 if (vr1
.type
!= VR_RANGE
3136 || range_includes_zero_p (vr1
.min
, vr1
.max
) != 0
3137 || vrp_val_is_min (vr1
.min
))
3139 set_value_range_to_varying (vr
);
3143 /* Compute MAX <|vr1.min|, |vr1.max|> - 1. */
3144 max
= fold_unary_to_constant (ABS_EXPR
, expr_type
, vr1
.min
);
3145 if (tree_int_cst_lt (max
, vr1
.max
))
3147 max
= int_const_binop (MINUS_EXPR
, max
, build_int_cst (TREE_TYPE (max
), 1));
3148 /* If the dividend is non-negative the modulus will be
3149 non-negative as well. */
3150 if (TYPE_UNSIGNED (expr_type
)
3151 || value_range_nonnegative_p (&vr0
))
3152 min
= build_int_cst (TREE_TYPE (max
), 0);
3154 min
= fold_unary_to_constant (NEGATE_EXPR
, expr_type
, max
);
3156 else if (code
== BIT_AND_EXPR
|| code
== BIT_IOR_EXPR
|| code
== BIT_XOR_EXPR
)
3158 bool int_cst_range0
, int_cst_range1
;
3159 wide_int may_be_nonzero0
, may_be_nonzero1
;
3160 wide_int must_be_nonzero0
, must_be_nonzero1
;
3162 int_cst_range0
= zero_nonzero_bits_from_vr (expr_type
, &vr0
,
3165 int_cst_range1
= zero_nonzero_bits_from_vr (expr_type
, &vr1
,
3170 if (code
== BIT_AND_EXPR
)
3172 min
= wide_int_to_tree (expr_type
,
3173 must_be_nonzero0
& must_be_nonzero1
);
3174 wide_int wmax
= may_be_nonzero0
& may_be_nonzero1
;
3175 /* If both input ranges contain only negative values we can
3176 truncate the result range maximum to the minimum of the
3177 input range maxima. */
3178 if (int_cst_range0
&& int_cst_range1
3179 && tree_int_cst_sgn (vr0
.max
) < 0
3180 && tree_int_cst_sgn (vr1
.max
) < 0)
3182 wmax
= wi::min (wmax
, vr0
.max
, TYPE_SIGN (expr_type
));
3183 wmax
= wi::min (wmax
, vr1
.max
, TYPE_SIGN (expr_type
));
3185 /* If either input range contains only non-negative values
3186 we can truncate the result range maximum to the respective
3187 maximum of the input range. */
3188 if (int_cst_range0
&& tree_int_cst_sgn (vr0
.min
) >= 0)
3189 wmax
= wi::min (wmax
, vr0
.max
, TYPE_SIGN (expr_type
));
3190 if (int_cst_range1
&& tree_int_cst_sgn (vr1
.min
) >= 0)
3191 wmax
= wi::min (wmax
, vr1
.max
, TYPE_SIGN (expr_type
));
3192 max
= wide_int_to_tree (expr_type
, wmax
);
3194 else if (code
== BIT_IOR_EXPR
)
3196 max
= wide_int_to_tree (expr_type
,
3197 may_be_nonzero0
| may_be_nonzero1
);
3198 wide_int wmin
= must_be_nonzero0
| must_be_nonzero1
;
3199 /* If the input ranges contain only positive values we can
3200 truncate the minimum of the result range to the maximum
3201 of the input range minima. */
3202 if (int_cst_range0
&& int_cst_range1
3203 && tree_int_cst_sgn (vr0
.min
) >= 0
3204 && tree_int_cst_sgn (vr1
.min
) >= 0)
3206 wmin
= wi::max (wmin
, vr0
.min
, TYPE_SIGN (expr_type
));
3207 wmin
= wi::max (wmin
, vr1
.min
, TYPE_SIGN (expr_type
));
3209 /* If either input range contains only negative values
3210 we can truncate the minimum of the result range to the
3211 respective minimum range. */
3212 if (int_cst_range0
&& tree_int_cst_sgn (vr0
.max
) < 0)
3213 wmin
= wi::max (wmin
, vr0
.min
, TYPE_SIGN (expr_type
));
3214 if (int_cst_range1
&& tree_int_cst_sgn (vr1
.max
) < 0)
3215 wmin
= wi::max (wmin
, vr1
.min
, TYPE_SIGN (expr_type
));
3216 min
= wide_int_to_tree (expr_type
, wmin
);
3218 else if (code
== BIT_XOR_EXPR
)
3220 wide_int result_zero_bits
= ((must_be_nonzero0
& must_be_nonzero1
)
3221 | ~(may_be_nonzero0
| may_be_nonzero1
));
3222 wide_int result_one_bits
3223 = (must_be_nonzero0
.and_not (may_be_nonzero1
)
3224 | must_be_nonzero1
.and_not (may_be_nonzero0
));
3225 max
= wide_int_to_tree (expr_type
, ~result_zero_bits
);
3226 min
= wide_int_to_tree (expr_type
, result_one_bits
);
3227 /* If the range has all positive or all negative values the
3228 result is better than VARYING. */
3229 if (tree_int_cst_sgn (min
) < 0
3230 || tree_int_cst_sgn (max
) >= 0)
3233 max
= min
= NULL_TREE
;
3239 /* If either MIN or MAX overflowed, then set the resulting range to
3240 VARYING. But we do accept an overflow infinity representation. */
3241 if (min
== NULL_TREE
3242 || (TREE_OVERFLOW_P (min
) && !is_overflow_infinity (min
))
3244 || (TREE_OVERFLOW_P (max
) && !is_overflow_infinity (max
)))
3246 set_value_range_to_varying (vr
);
3252 2) [-INF, +-INF(OVF)]
3253 3) [+-INF(OVF), +INF]
3254 4) [+-INF(OVF), +-INF(OVF)]
3255 We learn nothing when we have INF and INF(OVF) on both sides.
3256 Note that we do accept [-INF, -INF] and [+INF, +INF] without
3258 if ((vrp_val_is_min (min
) || is_overflow_infinity (min
))
3259 && (vrp_val_is_max (max
) || is_overflow_infinity (max
)))
3261 set_value_range_to_varying (vr
);
3265 cmp
= compare_values (min
, max
);
3266 if (cmp
== -2 || cmp
== 1)
3268 /* If the new range has its limits swapped around (MIN > MAX),
3269 then the operation caused one of them to wrap around, mark
3270 the new range VARYING. */
3271 set_value_range_to_varying (vr
);
3274 set_value_range (vr
, type
, min
, max
, NULL
);
3277 /* Extract range information from a binary expression OP0 CODE OP1 based on
3278 the ranges of each of its operands with resulting type EXPR_TYPE.
3279 The resulting range is stored in *VR. */
3282 extract_range_from_binary_expr (value_range_t
*vr
,
3283 enum tree_code code
,
3284 tree expr_type
, tree op0
, tree op1
)
3286 value_range_t vr0
= VR_INITIALIZER
;
3287 value_range_t vr1
= VR_INITIALIZER
;
3289 /* Get value ranges for each operand. For constant operands, create
3290 a new value range with the operand to simplify processing. */
3291 if (TREE_CODE (op0
) == SSA_NAME
)
3292 vr0
= *(get_value_range (op0
));
3293 else if (is_gimple_min_invariant (op0
))
3294 set_value_range_to_value (&vr0
, op0
, NULL
);
3296 set_value_range_to_varying (&vr0
);
3298 if (TREE_CODE (op1
) == SSA_NAME
)
3299 vr1
= *(get_value_range (op1
));
3300 else if (is_gimple_min_invariant (op1
))
3301 set_value_range_to_value (&vr1
, op1
, NULL
);
3303 set_value_range_to_varying (&vr1
);
3305 extract_range_from_binary_expr_1 (vr
, code
, expr_type
, &vr0
, &vr1
);
3307 /* Try harder for PLUS and MINUS if the range of one operand is symbolic
3308 and based on the other operand, for example if it was deduced from a
3309 symbolic comparison. When a bound of the range of the first operand
3310 is invariant, we set the corresponding bound of the new range to INF
3311 in order to avoid recursing on the range of the second operand. */
3312 if (vr
->type
== VR_VARYING
3313 && (code
== PLUS_EXPR
|| code
== MINUS_EXPR
)
3314 && TREE_CODE (op1
) == SSA_NAME
3315 && vr0
.type
== VR_RANGE
3316 && symbolic_range_based_on_p (&vr0
, op1
))
3318 const bool minus_p
= (code
== MINUS_EXPR
);
3319 value_range_t n_vr1
= VR_INITIALIZER
;
3321 /* Try with VR0 and [-INF, OP1]. */
3322 if (is_gimple_min_invariant (minus_p
? vr0
.max
: vr0
.min
))
3323 set_value_range (&n_vr1
, VR_RANGE
, vrp_val_min (expr_type
), op1
, NULL
);
3325 /* Try with VR0 and [OP1, +INF]. */
3326 else if (is_gimple_min_invariant (minus_p
? vr0
.min
: vr0
.max
))
3327 set_value_range (&n_vr1
, VR_RANGE
, op1
, vrp_val_max (expr_type
), NULL
);
3329 /* Try with VR0 and [OP1, OP1]. */
3331 set_value_range (&n_vr1
, VR_RANGE
, op1
, op1
, NULL
);
3333 extract_range_from_binary_expr_1 (vr
, code
, expr_type
, &vr0
, &n_vr1
);
3336 if (vr
->type
== VR_VARYING
3337 && (code
== PLUS_EXPR
|| code
== MINUS_EXPR
)
3338 && TREE_CODE (op0
) == SSA_NAME
3339 && vr1
.type
== VR_RANGE
3340 && symbolic_range_based_on_p (&vr1
, op0
))
3342 const bool minus_p
= (code
== MINUS_EXPR
);
3343 value_range_t n_vr0
= VR_INITIALIZER
;
3345 /* Try with [-INF, OP0] and VR1. */
3346 if (is_gimple_min_invariant (minus_p
? vr1
.max
: vr1
.min
))
3347 set_value_range (&n_vr0
, VR_RANGE
, vrp_val_min (expr_type
), op0
, NULL
);
3349 /* Try with [OP0, +INF] and VR1. */
3350 else if (is_gimple_min_invariant (minus_p
? vr1
.min
: vr1
.max
))
3351 set_value_range (&n_vr0
, VR_RANGE
, op0
, vrp_val_max (expr_type
), NULL
);
3353 /* Try with [OP0, OP0] and VR1. */
3355 set_value_range (&n_vr0
, VR_RANGE
, op0
, op0
, NULL
);
3357 extract_range_from_binary_expr_1 (vr
, code
, expr_type
, &n_vr0
, &vr1
);
3361 /* Extract range information from a unary operation CODE based on
3362 the range of its operand *VR0 with type OP0_TYPE with resulting type TYPE.
3363 The The resulting range is stored in *VR. */
3366 extract_range_from_unary_expr_1 (value_range_t
*vr
,
3367 enum tree_code code
, tree type
,
3368 value_range_t
*vr0_
, tree op0_type
)
3370 value_range_t vr0
= *vr0_
, vrtem0
= VR_INITIALIZER
, vrtem1
= VR_INITIALIZER
;
3372 /* VRP only operates on integral and pointer types. */
3373 if (!(INTEGRAL_TYPE_P (op0_type
)
3374 || POINTER_TYPE_P (op0_type
))
3375 || !(INTEGRAL_TYPE_P (type
)
3376 || POINTER_TYPE_P (type
)))
3378 set_value_range_to_varying (vr
);
3382 /* If VR0 is UNDEFINED, so is the result. */
3383 if (vr0
.type
== VR_UNDEFINED
)
3385 set_value_range_to_undefined (vr
);
3389 /* Handle operations that we express in terms of others. */
3390 if (code
== PAREN_EXPR
|| code
== OBJ_TYPE_REF
)
3392 /* PAREN_EXPR and OBJ_TYPE_REF are simple copies. */
3393 copy_value_range (vr
, &vr0
);
3396 else if (code
== NEGATE_EXPR
)
3398 /* -X is simply 0 - X, so re-use existing code that also handles
3399 anti-ranges fine. */
3400 value_range_t zero
= VR_INITIALIZER
;
3401 set_value_range_to_value (&zero
, build_int_cst (type
, 0), NULL
);
3402 extract_range_from_binary_expr_1 (vr
, MINUS_EXPR
, type
, &zero
, &vr0
);
3405 else if (code
== BIT_NOT_EXPR
)
3407 /* ~X is simply -1 - X, so re-use existing code that also handles
3408 anti-ranges fine. */
3409 value_range_t minusone
= VR_INITIALIZER
;
3410 set_value_range_to_value (&minusone
, build_int_cst (type
, -1), NULL
);
3411 extract_range_from_binary_expr_1 (vr
, MINUS_EXPR
,
3412 type
, &minusone
, &vr0
);
3416 /* Now canonicalize anti-ranges to ranges when they are not symbolic
3417 and express op ~[] as (op []') U (op []''). */
3418 if (vr0
.type
== VR_ANTI_RANGE
3419 && ranges_from_anti_range (&vr0
, &vrtem0
, &vrtem1
))
3421 extract_range_from_unary_expr_1 (vr
, code
, type
, &vrtem0
, op0_type
);
3422 if (vrtem1
.type
!= VR_UNDEFINED
)
3424 value_range_t vrres
= VR_INITIALIZER
;
3425 extract_range_from_unary_expr_1 (&vrres
, code
, type
,
3427 vrp_meet (vr
, &vrres
);
3432 if (CONVERT_EXPR_CODE_P (code
))
3434 tree inner_type
= op0_type
;
3435 tree outer_type
= type
;
3437 /* If the expression evaluates to a pointer, we are only interested in
3438 determining if it evaluates to NULL [0, 0] or non-NULL (~[0, 0]). */
3439 if (POINTER_TYPE_P (type
))
3441 if (range_is_nonnull (&vr0
))
3442 set_value_range_to_nonnull (vr
, type
);
3443 else if (range_is_null (&vr0
))
3444 set_value_range_to_null (vr
, type
);
3446 set_value_range_to_varying (vr
);
3450 /* If VR0 is varying and we increase the type precision, assume
3451 a full range for the following transformation. */
3452 if (vr0
.type
== VR_VARYING
3453 && INTEGRAL_TYPE_P (inner_type
)
3454 && TYPE_PRECISION (inner_type
) < TYPE_PRECISION (outer_type
))
3456 vr0
.type
= VR_RANGE
;
3457 vr0
.min
= TYPE_MIN_VALUE (inner_type
);
3458 vr0
.max
= TYPE_MAX_VALUE (inner_type
);
3461 /* If VR0 is a constant range or anti-range and the conversion is
3462 not truncating we can convert the min and max values and
3463 canonicalize the resulting range. Otherwise we can do the
3464 conversion if the size of the range is less than what the
3465 precision of the target type can represent and the range is
3466 not an anti-range. */
3467 if ((vr0
.type
== VR_RANGE
3468 || vr0
.type
== VR_ANTI_RANGE
)
3469 && TREE_CODE (vr0
.min
) == INTEGER_CST
3470 && TREE_CODE (vr0
.max
) == INTEGER_CST
3471 && (!is_overflow_infinity (vr0
.min
)
3472 || (vr0
.type
== VR_RANGE
3473 && TYPE_PRECISION (outer_type
) > TYPE_PRECISION (inner_type
)
3474 && needs_overflow_infinity (outer_type
)
3475 && supports_overflow_infinity (outer_type
)))
3476 && (!is_overflow_infinity (vr0
.max
)
3477 || (vr0
.type
== VR_RANGE
3478 && TYPE_PRECISION (outer_type
) > TYPE_PRECISION (inner_type
)
3479 && needs_overflow_infinity (outer_type
)
3480 && supports_overflow_infinity (outer_type
)))
3481 && (TYPE_PRECISION (outer_type
) >= TYPE_PRECISION (inner_type
)
3482 || (vr0
.type
== VR_RANGE
3483 && integer_zerop (int_const_binop (RSHIFT_EXPR
,
3484 int_const_binop (MINUS_EXPR
, vr0
.max
, vr0
.min
),
3485 size_int (TYPE_PRECISION (outer_type
)))))))
3487 tree new_min
, new_max
;
3488 if (is_overflow_infinity (vr0
.min
))
3489 new_min
= negative_overflow_infinity (outer_type
);
3491 new_min
= force_fit_type (outer_type
, wi::to_widest (vr0
.min
),
3493 if (is_overflow_infinity (vr0
.max
))
3494 new_max
= positive_overflow_infinity (outer_type
);
3496 new_max
= force_fit_type (outer_type
, wi::to_widest (vr0
.max
),
3498 set_and_canonicalize_value_range (vr
, vr0
.type
,
3499 new_min
, new_max
, NULL
);
3503 set_value_range_to_varying (vr
);
3506 else if (code
== ABS_EXPR
)
3511 /* Pass through vr0 in the easy cases. */
3512 if (TYPE_UNSIGNED (type
)
3513 || value_range_nonnegative_p (&vr0
))
3515 copy_value_range (vr
, &vr0
);
3519 /* For the remaining varying or symbolic ranges we can't do anything
3521 if (vr0
.type
== VR_VARYING
3522 || symbolic_range_p (&vr0
))
3524 set_value_range_to_varying (vr
);
3528 /* -TYPE_MIN_VALUE = TYPE_MIN_VALUE with flag_wrapv so we can't get a
3530 if (!TYPE_OVERFLOW_UNDEFINED (type
)
3531 && ((vr0
.type
== VR_RANGE
3532 && vrp_val_is_min (vr0
.min
))
3533 || (vr0
.type
== VR_ANTI_RANGE
3534 && !vrp_val_is_min (vr0
.min
))))
3536 set_value_range_to_varying (vr
);
3540 /* ABS_EXPR may flip the range around, if the original range
3541 included negative values. */
3542 if (is_overflow_infinity (vr0
.min
))
3543 min
= positive_overflow_infinity (type
);
3544 else if (!vrp_val_is_min (vr0
.min
))
3545 min
= fold_unary_to_constant (code
, type
, vr0
.min
);
3546 else if (!needs_overflow_infinity (type
))
3547 min
= TYPE_MAX_VALUE (type
);
3548 else if (supports_overflow_infinity (type
))
3549 min
= positive_overflow_infinity (type
);
3552 set_value_range_to_varying (vr
);
3556 if (is_overflow_infinity (vr0
.max
))
3557 max
= positive_overflow_infinity (type
);
3558 else if (!vrp_val_is_min (vr0
.max
))
3559 max
= fold_unary_to_constant (code
, type
, vr0
.max
);
3560 else if (!needs_overflow_infinity (type
))
3561 max
= TYPE_MAX_VALUE (type
);
3562 else if (supports_overflow_infinity (type
)
3563 /* We shouldn't generate [+INF, +INF] as set_value_range
3564 doesn't like this and ICEs. */
3565 && !is_positive_overflow_infinity (min
))
3566 max
= positive_overflow_infinity (type
);
3569 set_value_range_to_varying (vr
);
3573 cmp
= compare_values (min
, max
);
3575 /* If a VR_ANTI_RANGEs contains zero, then we have
3576 ~[-INF, min(MIN, MAX)]. */
3577 if (vr0
.type
== VR_ANTI_RANGE
)
3579 if (range_includes_zero_p (vr0
.min
, vr0
.max
) == 1)
3581 /* Take the lower of the two values. */
3585 /* Create ~[-INF, min (abs(MIN), abs(MAX))]
3586 or ~[-INF + 1, min (abs(MIN), abs(MAX))] when
3587 flag_wrapv is set and the original anti-range doesn't include
3588 TYPE_MIN_VALUE, remember -TYPE_MIN_VALUE = TYPE_MIN_VALUE. */
3589 if (TYPE_OVERFLOW_WRAPS (type
))
3591 tree type_min_value
= TYPE_MIN_VALUE (type
);
3593 min
= (vr0
.min
!= type_min_value
3594 ? int_const_binop (PLUS_EXPR
, type_min_value
,
3595 build_int_cst (TREE_TYPE (type_min_value
), 1))
3600 if (overflow_infinity_range_p (&vr0
))
3601 min
= negative_overflow_infinity (type
);
3603 min
= TYPE_MIN_VALUE (type
);
3608 /* All else has failed, so create the range [0, INF], even for
3609 flag_wrapv since TYPE_MIN_VALUE is in the original
3611 vr0
.type
= VR_RANGE
;
3612 min
= build_int_cst (type
, 0);
3613 if (needs_overflow_infinity (type
))
3615 if (supports_overflow_infinity (type
))
3616 max
= positive_overflow_infinity (type
);
3619 set_value_range_to_varying (vr
);
3624 max
= TYPE_MAX_VALUE (type
);
3628 /* If the range contains zero then we know that the minimum value in the
3629 range will be zero. */
3630 else if (range_includes_zero_p (vr0
.min
, vr0
.max
) == 1)
3634 min
= build_int_cst (type
, 0);
3638 /* If the range was reversed, swap MIN and MAX. */
3647 cmp
= compare_values (min
, max
);
3648 if (cmp
== -2 || cmp
== 1)
3650 /* If the new range has its limits swapped around (MIN > MAX),
3651 then the operation caused one of them to wrap around, mark
3652 the new range VARYING. */
3653 set_value_range_to_varying (vr
);
3656 set_value_range (vr
, vr0
.type
, min
, max
, NULL
);
3660 /* For unhandled operations fall back to varying. */
3661 set_value_range_to_varying (vr
);
3666 /* Extract range information from a unary expression CODE OP0 based on
3667 the range of its operand with resulting type TYPE.
3668 The resulting range is stored in *VR. */
3671 extract_range_from_unary_expr (value_range_t
*vr
, enum tree_code code
,
3672 tree type
, tree op0
)
3674 value_range_t vr0
= VR_INITIALIZER
;
3676 /* Get value ranges for the operand. For constant operands, create
3677 a new value range with the operand to simplify processing. */
3678 if (TREE_CODE (op0
) == SSA_NAME
)
3679 vr0
= *(get_value_range (op0
));
3680 else if (is_gimple_min_invariant (op0
))
3681 set_value_range_to_value (&vr0
, op0
, NULL
);
3683 set_value_range_to_varying (&vr0
);
3685 extract_range_from_unary_expr_1 (vr
, code
, type
, &vr0
, TREE_TYPE (op0
));
3689 /* Extract range information from a conditional expression STMT based on
3690 the ranges of each of its operands and the expression code. */
3693 extract_range_from_cond_expr (value_range_t
*vr
, gimple stmt
)
3696 value_range_t vr0
= VR_INITIALIZER
;
3697 value_range_t vr1
= VR_INITIALIZER
;
3699 /* Get value ranges for each operand. For constant operands, create
3700 a new value range with the operand to simplify processing. */
3701 op0
= gimple_assign_rhs2 (stmt
);
3702 if (TREE_CODE (op0
) == SSA_NAME
)
3703 vr0
= *(get_value_range (op0
));
3704 else if (is_gimple_min_invariant (op0
))
3705 set_value_range_to_value (&vr0
, op0
, NULL
);
3707 set_value_range_to_varying (&vr0
);
3709 op1
= gimple_assign_rhs3 (stmt
);
3710 if (TREE_CODE (op1
) == SSA_NAME
)
3711 vr1
= *(get_value_range (op1
));
3712 else if (is_gimple_min_invariant (op1
))
3713 set_value_range_to_value (&vr1
, op1
, NULL
);
3715 set_value_range_to_varying (&vr1
);
3717 /* The resulting value range is the union of the operand ranges */
3718 copy_value_range (vr
, &vr0
);
3719 vrp_meet (vr
, &vr1
);
3723 /* Extract range information from a comparison expression EXPR based
3724 on the range of its operand and the expression code. */
3727 extract_range_from_comparison (value_range_t
*vr
, enum tree_code code
,
3728 tree type
, tree op0
, tree op1
)
3733 val
= vrp_evaluate_conditional_warnv_with_ops (code
, op0
, op1
, false, &sop
,
3736 /* A disadvantage of using a special infinity as an overflow
3737 representation is that we lose the ability to record overflow
3738 when we don't have an infinity. So we have to ignore a result
3739 which relies on overflow. */
3741 if (val
&& !is_overflow_infinity (val
) && !sop
)
3743 /* Since this expression was found on the RHS of an assignment,
3744 its type may be different from _Bool. Convert VAL to EXPR's
3746 val
= fold_convert (type
, val
);
3747 if (is_gimple_min_invariant (val
))
3748 set_value_range_to_value (vr
, val
, vr
->equiv
);
3750 set_value_range (vr
, VR_RANGE
, val
, val
, vr
->equiv
);
3753 /* The result of a comparison is always true or false. */
3754 set_value_range_to_truthvalue (vr
, type
);
3757 /* Try to derive a nonnegative or nonzero range out of STMT relying
3758 primarily on generic routines in fold in conjunction with range data.
3759 Store the result in *VR */
3762 extract_range_basic (value_range_t
*vr
, gimple stmt
)
3765 tree type
= gimple_expr_type (stmt
);
3767 if (gimple_call_builtin_p (stmt
, BUILT_IN_NORMAL
))
3769 tree fndecl
= gimple_call_fndecl (stmt
), arg
;
3770 int mini
, maxi
, zerov
= 0, prec
;
3772 switch (DECL_FUNCTION_CODE (fndecl
))
3774 case BUILT_IN_CONSTANT_P
:
3775 /* If the call is __builtin_constant_p and the argument is a
3776 function parameter resolve it to false. This avoids bogus
3777 array bound warnings.
3778 ??? We could do this as early as inlining is finished. */
3779 arg
= gimple_call_arg (stmt
, 0);
3780 if (TREE_CODE (arg
) == SSA_NAME
3781 && SSA_NAME_IS_DEFAULT_DEF (arg
)
3782 && TREE_CODE (SSA_NAME_VAR (arg
)) == PARM_DECL
)
3784 set_value_range_to_null (vr
, type
);
3788 /* Both __builtin_ffs* and __builtin_popcount return
3790 CASE_INT_FN (BUILT_IN_FFS
):
3791 CASE_INT_FN (BUILT_IN_POPCOUNT
):
3792 arg
= gimple_call_arg (stmt
, 0);
3793 prec
= TYPE_PRECISION (TREE_TYPE (arg
));
3796 if (TREE_CODE (arg
) == SSA_NAME
)
3798 value_range_t
*vr0
= get_value_range (arg
);
3799 /* If arg is non-zero, then ffs or popcount
3801 if (((vr0
->type
== VR_RANGE
3802 && range_includes_zero_p (vr0
->min
, vr0
->max
) == 0)
3803 || (vr0
->type
== VR_ANTI_RANGE
3804 && range_includes_zero_p (vr0
->min
, vr0
->max
) == 1))
3805 && !is_overflow_infinity (vr0
->min
)
3806 && !is_overflow_infinity (vr0
->max
))
3808 /* If some high bits are known to be zero,
3809 we can decrease the maximum. */
3810 if (vr0
->type
== VR_RANGE
3811 && TREE_CODE (vr0
->max
) == INTEGER_CST
3812 && !operand_less_p (vr0
->min
,
3813 build_zero_cst (TREE_TYPE (vr0
->min
)))
3814 && !is_overflow_infinity (vr0
->max
))
3815 maxi
= tree_floor_log2 (vr0
->max
) + 1;
3818 /* __builtin_parity* returns [0, 1]. */
3819 CASE_INT_FN (BUILT_IN_PARITY
):
3823 /* __builtin_c[lt]z* return [0, prec-1], except for
3824 when the argument is 0, but that is undefined behavior.
3825 On many targets where the CLZ RTL or optab value is defined
3826 for 0 the value is prec, so include that in the range
3828 CASE_INT_FN (BUILT_IN_CLZ
):
3829 arg
= gimple_call_arg (stmt
, 0);
3830 prec
= TYPE_PRECISION (TREE_TYPE (arg
));
3833 if (optab_handler (clz_optab
, TYPE_MODE (TREE_TYPE (arg
)))
3835 && CLZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (TREE_TYPE (arg
)),
3837 /* Handle only the single common value. */
3839 /* Magic value to give up, unless vr0 proves
3842 if (TREE_CODE (arg
) == SSA_NAME
)
3844 value_range_t
*vr0
= get_value_range (arg
);
3845 /* From clz of VR_RANGE minimum we can compute
3847 if (vr0
->type
== VR_RANGE
3848 && TREE_CODE (vr0
->min
) == INTEGER_CST
3849 && !is_overflow_infinity (vr0
->min
))
3851 maxi
= prec
- 1 - tree_floor_log2 (vr0
->min
);
3855 else if (vr0
->type
== VR_ANTI_RANGE
3856 && integer_zerop (vr0
->min
)
3857 && !is_overflow_infinity (vr0
->min
))
3864 /* From clz of VR_RANGE maximum we can compute
3866 if (vr0
->type
== VR_RANGE
3867 && TREE_CODE (vr0
->max
) == INTEGER_CST
3868 && !is_overflow_infinity (vr0
->max
))
3870 mini
= prec
- 1 - tree_floor_log2 (vr0
->max
);
3878 /* __builtin_ctz* return [0, prec-1], except for
3879 when the argument is 0, but that is undefined behavior.
3880 If there is a ctz optab for this mode and
3881 CTZ_DEFINED_VALUE_AT_ZERO, include that in the range,
3882 otherwise just assume 0 won't be seen. */
3883 CASE_INT_FN (BUILT_IN_CTZ
):
3884 arg
= gimple_call_arg (stmt
, 0);
3885 prec
= TYPE_PRECISION (TREE_TYPE (arg
));
3888 if (optab_handler (ctz_optab
, TYPE_MODE (TREE_TYPE (arg
)))
3890 && CTZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (TREE_TYPE (arg
)),
3893 /* Handle only the two common values. */
3896 else if (zerov
== prec
)
3899 /* Magic value to give up, unless vr0 proves
3903 if (TREE_CODE (arg
) == SSA_NAME
)
3905 value_range_t
*vr0
= get_value_range (arg
);
3906 /* If arg is non-zero, then use [0, prec - 1]. */
3907 if (((vr0
->type
== VR_RANGE
3908 && integer_nonzerop (vr0
->min
))
3909 || (vr0
->type
== VR_ANTI_RANGE
3910 && integer_zerop (vr0
->min
)))
3911 && !is_overflow_infinity (vr0
->min
))
3916 /* If some high bits are known to be zero,
3917 we can decrease the result maximum. */
3918 if (vr0
->type
== VR_RANGE
3919 && TREE_CODE (vr0
->max
) == INTEGER_CST
3920 && !is_overflow_infinity (vr0
->max
))
3922 maxi
= tree_floor_log2 (vr0
->max
);
3923 /* For vr0 [0, 0] give up. */
3931 /* __builtin_clrsb* returns [0, prec-1]. */
3932 CASE_INT_FN (BUILT_IN_CLRSB
):
3933 arg
= gimple_call_arg (stmt
, 0);
3934 prec
= TYPE_PRECISION (TREE_TYPE (arg
));
3939 set_value_range (vr
, VR_RANGE
, build_int_cst (type
, mini
),
3940 build_int_cst (type
, maxi
), NULL
);
3946 else if (is_gimple_call (stmt
)
3947 && gimple_call_internal_p (stmt
))
3949 enum tree_code subcode
= ERROR_MARK
;
3950 switch (gimple_call_internal_fn (stmt
))
3952 case IFN_UBSAN_CHECK_ADD
:
3953 subcode
= PLUS_EXPR
;
3955 case IFN_UBSAN_CHECK_SUB
:
3956 subcode
= MINUS_EXPR
;
3958 case IFN_UBSAN_CHECK_MUL
:
3959 subcode
= MULT_EXPR
;
3964 if (subcode
!= ERROR_MARK
)
3966 bool saved_flag_wrapv
= flag_wrapv
;
3967 /* Pretend the arithmetics is wrapping. If there is
3968 any overflow, we'll complain, but will actually do
3969 wrapping operation. */
3971 extract_range_from_binary_expr (vr
, subcode
, type
,
3972 gimple_call_arg (stmt
, 0),
3973 gimple_call_arg (stmt
, 1));
3974 flag_wrapv
= saved_flag_wrapv
;
3976 /* If for both arguments vrp_valueize returned non-NULL,
3977 this should have been already folded and if not, it
3978 wasn't folded because of overflow. Avoid removing the
3979 UBSAN_CHECK_* calls in that case. */
3980 if (vr
->type
== VR_RANGE
3981 && (vr
->min
== vr
->max
3982 || operand_equal_p (vr
->min
, vr
->max
, 0)))
3983 set_value_range_to_varying (vr
);
3987 if (INTEGRAL_TYPE_P (type
)
3988 && gimple_stmt_nonnegative_warnv_p (stmt
, &sop
))
3989 set_value_range_to_nonnegative (vr
, type
,
3990 sop
|| stmt_overflow_infinity (stmt
));
3991 else if (vrp_stmt_computes_nonzero (stmt
, &sop
)
3993 set_value_range_to_nonnull (vr
, type
);
3995 set_value_range_to_varying (vr
);
3999 /* Try to compute a useful range out of assignment STMT and store it
4003 extract_range_from_assignment (value_range_t
*vr
, gimple stmt
)
4005 enum tree_code code
= gimple_assign_rhs_code (stmt
);
4007 if (code
== ASSERT_EXPR
)
4008 extract_range_from_assert (vr
, gimple_assign_rhs1 (stmt
));
4009 else if (code
== SSA_NAME
)
4010 extract_range_from_ssa_name (vr
, gimple_assign_rhs1 (stmt
));
4011 else if (TREE_CODE_CLASS (code
) == tcc_binary
)
4012 extract_range_from_binary_expr (vr
, gimple_assign_rhs_code (stmt
),
4013 gimple_expr_type (stmt
),
4014 gimple_assign_rhs1 (stmt
),
4015 gimple_assign_rhs2 (stmt
));
4016 else if (TREE_CODE_CLASS (code
) == tcc_unary
)
4017 extract_range_from_unary_expr (vr
, gimple_assign_rhs_code (stmt
),
4018 gimple_expr_type (stmt
),
4019 gimple_assign_rhs1 (stmt
));
4020 else if (code
== COND_EXPR
)
4021 extract_range_from_cond_expr (vr
, stmt
);
4022 else if (TREE_CODE_CLASS (code
) == tcc_comparison
)
4023 extract_range_from_comparison (vr
, gimple_assign_rhs_code (stmt
),
4024 gimple_expr_type (stmt
),
4025 gimple_assign_rhs1 (stmt
),
4026 gimple_assign_rhs2 (stmt
));
4027 else if (get_gimple_rhs_class (code
) == GIMPLE_SINGLE_RHS
4028 && is_gimple_min_invariant (gimple_assign_rhs1 (stmt
)))
4029 set_value_range_to_value (vr
, gimple_assign_rhs1 (stmt
), NULL
);
4031 set_value_range_to_varying (vr
);
4033 if (vr
->type
== VR_VARYING
)
4034 extract_range_basic (vr
, stmt
);
4037 /* Given a range VR, a LOOP and a variable VAR, determine whether it
4038 would be profitable to adjust VR using scalar evolution information
4039 for VAR. If so, update VR with the new limits. */
4042 adjust_range_with_scev (value_range_t
*vr
, struct loop
*loop
,
4043 gimple stmt
, tree var
)
4045 tree init
, step
, chrec
, tmin
, tmax
, min
, max
, type
, tem
;
4046 enum ev_direction dir
;
4048 /* TODO. Don't adjust anti-ranges. An anti-range may provide
4049 better opportunities than a regular range, but I'm not sure. */
4050 if (vr
->type
== VR_ANTI_RANGE
)
4053 chrec
= instantiate_parameters (loop
, analyze_scalar_evolution (loop
, var
));
4055 /* Like in PR19590, scev can return a constant function. */
4056 if (is_gimple_min_invariant (chrec
))
4058 set_value_range_to_value (vr
, chrec
, vr
->equiv
);
4062 if (TREE_CODE (chrec
) != POLYNOMIAL_CHREC
)
4065 init
= initial_condition_in_loop_num (chrec
, loop
->num
);
4066 tem
= op_with_constant_singleton_value_range (init
);
4069 step
= evolution_part_in_loop_num (chrec
, loop
->num
);
4070 tem
= op_with_constant_singleton_value_range (step
);
4074 /* If STEP is symbolic, we can't know whether INIT will be the
4075 minimum or maximum value in the range. Also, unless INIT is
4076 a simple expression, compare_values and possibly other functions
4077 in tree-vrp won't be able to handle it. */
4078 if (step
== NULL_TREE
4079 || !is_gimple_min_invariant (step
)
4080 || !valid_value_p (init
))
4083 dir
= scev_direction (chrec
);
4084 if (/* Do not adjust ranges if we do not know whether the iv increases
4085 or decreases, ... */
4086 dir
== EV_DIR_UNKNOWN
4087 /* ... or if it may wrap. */
4088 || scev_probably_wraps_p (init
, step
, stmt
, get_chrec_loop (chrec
),
4092 /* We use TYPE_MIN_VALUE and TYPE_MAX_VALUE here instead of
4093 negative_overflow_infinity and positive_overflow_infinity,
4094 because we have concluded that the loop probably does not
4097 type
= TREE_TYPE (var
);
4098 if (POINTER_TYPE_P (type
) || !TYPE_MIN_VALUE (type
))
4099 tmin
= lower_bound_in_type (type
, type
);
4101 tmin
= TYPE_MIN_VALUE (type
);
4102 if (POINTER_TYPE_P (type
) || !TYPE_MAX_VALUE (type
))
4103 tmax
= upper_bound_in_type (type
, type
);
4105 tmax
= TYPE_MAX_VALUE (type
);
4107 /* Try to use estimated number of iterations for the loop to constrain the
4108 final value in the evolution. */
4109 if (TREE_CODE (step
) == INTEGER_CST
4110 && is_gimple_val (init
)
4111 && (TREE_CODE (init
) != SSA_NAME
4112 || get_value_range (init
)->type
== VR_RANGE
))
4116 /* We are only entering here for loop header PHI nodes, so using
4117 the number of latch executions is the correct thing to use. */
4118 if (max_loop_iterations (loop
, &nit
))
4120 value_range_t maxvr
= VR_INITIALIZER
;
4121 signop sgn
= TYPE_SIGN (TREE_TYPE (step
));
4124 widest_int wtmp
= wi::mul (wi::to_widest (step
), nit
, sgn
,
4126 /* If the multiplication overflowed we can't do a meaningful
4127 adjustment. Likewise if the result doesn't fit in the type
4128 of the induction variable. For a signed type we have to
4129 check whether the result has the expected signedness which
4130 is that of the step as number of iterations is unsigned. */
4132 && wi::fits_to_tree_p (wtmp
, TREE_TYPE (init
))
4134 || wi::gts_p (wtmp
, 0) == wi::gts_p (step
, 0)))
4136 tem
= wide_int_to_tree (TREE_TYPE (init
), wtmp
);
4137 extract_range_from_binary_expr (&maxvr
, PLUS_EXPR
,
4138 TREE_TYPE (init
), init
, tem
);
4139 /* Likewise if the addition did. */
4140 if (maxvr
.type
== VR_RANGE
)
4149 if (vr
->type
== VR_VARYING
|| vr
->type
== VR_UNDEFINED
)
4154 /* For VARYING or UNDEFINED ranges, just about anything we get
4155 from scalar evolutions should be better. */
4157 if (dir
== EV_DIR_DECREASES
)
4162 else if (vr
->type
== VR_RANGE
)
4167 if (dir
== EV_DIR_DECREASES
)
4169 /* INIT is the maximum value. If INIT is lower than VR->MAX
4170 but no smaller than VR->MIN, set VR->MAX to INIT. */
4171 if (compare_values (init
, max
) == -1)
4174 /* According to the loop information, the variable does not
4175 overflow. If we think it does, probably because of an
4176 overflow due to arithmetic on a different INF value,
4178 if (is_negative_overflow_infinity (min
)
4179 || compare_values (min
, tmin
) == -1)
4185 /* If INIT is bigger than VR->MIN, set VR->MIN to INIT. */
4186 if (compare_values (init
, min
) == 1)
4189 if (is_positive_overflow_infinity (max
)
4190 || compare_values (tmax
, max
) == -1)
4197 /* If we just created an invalid range with the minimum
4198 greater than the maximum, we fail conservatively.
4199 This should happen only in unreachable
4200 parts of code, or for invalid programs. */
4201 if (compare_values (min
, max
) == 1
4202 || (is_negative_overflow_infinity (min
)
4203 && is_positive_overflow_infinity (max
)))
4206 set_value_range (vr
, VR_RANGE
, min
, max
, vr
->equiv
);
4210 /* Given two numeric value ranges VR0, VR1 and a comparison code COMP:
4212 - Return BOOLEAN_TRUE_NODE if VR0 COMP VR1 always returns true for
4213 all the values in the ranges.
4215 - Return BOOLEAN_FALSE_NODE if the comparison always returns false.
4217 - Return NULL_TREE if it is not always possible to determine the
4218 value of the comparison.
4220 Also set *STRICT_OVERFLOW_P to indicate whether a range with an
4221 overflow infinity was used in the test. */
4225 compare_ranges (enum tree_code comp
, value_range_t
*vr0
, value_range_t
*vr1
,
4226 bool *strict_overflow_p
)
4228 /* VARYING or UNDEFINED ranges cannot be compared. */
4229 if (vr0
->type
== VR_VARYING
4230 || vr0
->type
== VR_UNDEFINED
4231 || vr1
->type
== VR_VARYING
4232 || vr1
->type
== VR_UNDEFINED
)
4235 /* Anti-ranges need to be handled separately. */
4236 if (vr0
->type
== VR_ANTI_RANGE
|| vr1
->type
== VR_ANTI_RANGE
)
4238 /* If both are anti-ranges, then we cannot compute any
4240 if (vr0
->type
== VR_ANTI_RANGE
&& vr1
->type
== VR_ANTI_RANGE
)
4243 /* These comparisons are never statically computable. */
4250 /* Equality can be computed only between a range and an
4251 anti-range. ~[VAL1, VAL2] == [VAL1, VAL2] is always false. */
4252 if (vr0
->type
== VR_RANGE
)
4254 /* To simplify processing, make VR0 the anti-range. */
4255 value_range_t
*tmp
= vr0
;
4260 gcc_assert (comp
== NE_EXPR
|| comp
== EQ_EXPR
);
4262 if (compare_values_warnv (vr0
->min
, vr1
->min
, strict_overflow_p
) == 0
4263 && compare_values_warnv (vr0
->max
, vr1
->max
, strict_overflow_p
) == 0)
4264 return (comp
== NE_EXPR
) ? boolean_true_node
: boolean_false_node
;
4269 if (!usable_range_p (vr0
, strict_overflow_p
)
4270 || !usable_range_p (vr1
, strict_overflow_p
))
4273 /* Simplify processing. If COMP is GT_EXPR or GE_EXPR, switch the
4274 operands around and change the comparison code. */
4275 if (comp
== GT_EXPR
|| comp
== GE_EXPR
)
4278 comp
= (comp
== GT_EXPR
) ? LT_EXPR
: LE_EXPR
;
4284 if (comp
== EQ_EXPR
)
4286 /* Equality may only be computed if both ranges represent
4287 exactly one value. */
4288 if (compare_values_warnv (vr0
->min
, vr0
->max
, strict_overflow_p
) == 0
4289 && compare_values_warnv (vr1
->min
, vr1
->max
, strict_overflow_p
) == 0)
4291 int cmp_min
= compare_values_warnv (vr0
->min
, vr1
->min
,
4293 int cmp_max
= compare_values_warnv (vr0
->max
, vr1
->max
,
4295 if (cmp_min
== 0 && cmp_max
== 0)
4296 return boolean_true_node
;
4297 else if (cmp_min
!= -2 && cmp_max
!= -2)
4298 return boolean_false_node
;
4300 /* If [V0_MIN, V1_MAX] < [V1_MIN, V1_MAX] then V0 != V1. */
4301 else if (compare_values_warnv (vr0
->min
, vr1
->max
,
4302 strict_overflow_p
) == 1
4303 || compare_values_warnv (vr1
->min
, vr0
->max
,
4304 strict_overflow_p
) == 1)
4305 return boolean_false_node
;
4309 else if (comp
== NE_EXPR
)
4313 /* If VR0 is completely to the left or completely to the right
4314 of VR1, they are always different. Notice that we need to
4315 make sure that both comparisons yield similar results to
4316 avoid comparing values that cannot be compared at
4318 cmp1
= compare_values_warnv (vr0
->max
, vr1
->min
, strict_overflow_p
);
4319 cmp2
= compare_values_warnv (vr0
->min
, vr1
->max
, strict_overflow_p
);
4320 if ((cmp1
== -1 && cmp2
== -1) || (cmp1
== 1 && cmp2
== 1))
4321 return boolean_true_node
;
4323 /* If VR0 and VR1 represent a single value and are identical,
4325 else if (compare_values_warnv (vr0
->min
, vr0
->max
,
4326 strict_overflow_p
) == 0
4327 && compare_values_warnv (vr1
->min
, vr1
->max
,
4328 strict_overflow_p
) == 0
4329 && compare_values_warnv (vr0
->min
, vr1
->min
,
4330 strict_overflow_p
) == 0
4331 && compare_values_warnv (vr0
->max
, vr1
->max
,
4332 strict_overflow_p
) == 0)
4333 return boolean_false_node
;
4335 /* Otherwise, they may or may not be different. */
4339 else if (comp
== LT_EXPR
|| comp
== LE_EXPR
)
4343 /* If VR0 is to the left of VR1, return true. */
4344 tst
= compare_values_warnv (vr0
->max
, vr1
->min
, strict_overflow_p
);
4345 if ((comp
== LT_EXPR
&& tst
== -1)
4346 || (comp
== LE_EXPR
&& (tst
== -1 || tst
== 0)))
4348 if (overflow_infinity_range_p (vr0
)
4349 || overflow_infinity_range_p (vr1
))
4350 *strict_overflow_p
= true;
4351 return boolean_true_node
;
4354 /* If VR0 is to the right of VR1, return false. */
4355 tst
= compare_values_warnv (vr0
->min
, vr1
->max
, strict_overflow_p
);
4356 if ((comp
== LT_EXPR
&& (tst
== 0 || tst
== 1))
4357 || (comp
== LE_EXPR
&& tst
== 1))
4359 if (overflow_infinity_range_p (vr0
)
4360 || overflow_infinity_range_p (vr1
))
4361 *strict_overflow_p
= true;
4362 return boolean_false_node
;
4365 /* Otherwise, we don't know. */
4373 /* Given a value range VR, a value VAL and a comparison code COMP, return
4374 BOOLEAN_TRUE_NODE if VR COMP VAL always returns true for all the
4375 values in VR. Return BOOLEAN_FALSE_NODE if the comparison
4376 always returns false. Return NULL_TREE if it is not always
4377 possible to determine the value of the comparison. Also set
4378 *STRICT_OVERFLOW_P to indicate whether a range with an overflow
4379 infinity was used in the test. */
4382 compare_range_with_value (enum tree_code comp
, value_range_t
*vr
, tree val
,
4383 bool *strict_overflow_p
)
4385 if (vr
->type
== VR_VARYING
|| vr
->type
== VR_UNDEFINED
)
4388 /* Anti-ranges need to be handled separately. */
4389 if (vr
->type
== VR_ANTI_RANGE
)
4391 /* For anti-ranges, the only predicates that we can compute at
4392 compile time are equality and inequality. */
4399 /* ~[VAL_1, VAL_2] OP VAL is known if VAL_1 <= VAL <= VAL_2. */
4400 if (value_inside_range (val
, vr
->min
, vr
->max
) == 1)
4401 return (comp
== NE_EXPR
) ? boolean_true_node
: boolean_false_node
;
4406 if (!usable_range_p (vr
, strict_overflow_p
))
4409 if (comp
== EQ_EXPR
)
4411 /* EQ_EXPR may only be computed if VR represents exactly
4413 if (compare_values_warnv (vr
->min
, vr
->max
, strict_overflow_p
) == 0)
4415 int cmp
= compare_values_warnv (vr
->min
, val
, strict_overflow_p
);
4417 return boolean_true_node
;
4418 else if (cmp
== -1 || cmp
== 1 || cmp
== 2)
4419 return boolean_false_node
;
4421 else if (compare_values_warnv (val
, vr
->min
, strict_overflow_p
) == -1
4422 || compare_values_warnv (vr
->max
, val
, strict_overflow_p
) == -1)
4423 return boolean_false_node
;
4427 else if (comp
== NE_EXPR
)
4429 /* If VAL is not inside VR, then they are always different. */
4430 if (compare_values_warnv (vr
->max
, val
, strict_overflow_p
) == -1
4431 || compare_values_warnv (vr
->min
, val
, strict_overflow_p
) == 1)
4432 return boolean_true_node
;
4434 /* If VR represents exactly one value equal to VAL, then return
4436 if (compare_values_warnv (vr
->min
, vr
->max
, strict_overflow_p
) == 0
4437 && compare_values_warnv (vr
->min
, val
, strict_overflow_p
) == 0)
4438 return boolean_false_node
;
4440 /* Otherwise, they may or may not be different. */
4443 else if (comp
== LT_EXPR
|| comp
== LE_EXPR
)
4447 /* If VR is to the left of VAL, return true. */
4448 tst
= compare_values_warnv (vr
->max
, val
, strict_overflow_p
);
4449 if ((comp
== LT_EXPR
&& tst
== -1)
4450 || (comp
== LE_EXPR
&& (tst
== -1 || tst
== 0)))
4452 if (overflow_infinity_range_p (vr
))
4453 *strict_overflow_p
= true;
4454 return boolean_true_node
;
4457 /* If VR is to the right of VAL, return false. */
4458 tst
= compare_values_warnv (vr
->min
, val
, strict_overflow_p
);
4459 if ((comp
== LT_EXPR
&& (tst
== 0 || tst
== 1))
4460 || (comp
== LE_EXPR
&& tst
== 1))
4462 if (overflow_infinity_range_p (vr
))
4463 *strict_overflow_p
= true;
4464 return boolean_false_node
;
4467 /* Otherwise, we don't know. */
4470 else if (comp
== GT_EXPR
|| comp
== GE_EXPR
)
4474 /* If VR is to the right of VAL, return true. */
4475 tst
= compare_values_warnv (vr
->min
, val
, strict_overflow_p
);
4476 if ((comp
== GT_EXPR
&& tst
== 1)
4477 || (comp
== GE_EXPR
&& (tst
== 0 || tst
== 1)))
4479 if (overflow_infinity_range_p (vr
))
4480 *strict_overflow_p
= true;
4481 return boolean_true_node
;
4484 /* If VR is to the left of VAL, return false. */
4485 tst
= compare_values_warnv (vr
->max
, val
, strict_overflow_p
);
4486 if ((comp
== GT_EXPR
&& (tst
== -1 || tst
== 0))
4487 || (comp
== GE_EXPR
&& tst
== -1))
4489 if (overflow_infinity_range_p (vr
))
4490 *strict_overflow_p
= true;
4491 return boolean_false_node
;
4494 /* Otherwise, we don't know. */
4502 /* Debugging dumps. */
4504 void dump_value_range (FILE *, value_range_t
*);
4505 void debug_value_range (value_range_t
*);
4506 void dump_all_value_ranges (FILE *);
4507 void debug_all_value_ranges (void);
4508 void dump_vr_equiv (FILE *, bitmap
);
4509 void debug_vr_equiv (bitmap
);
4512 /* Dump value range VR to FILE. */
4515 dump_value_range (FILE *file
, value_range_t
*vr
)
4518 fprintf (file
, "[]");
4519 else if (vr
->type
== VR_UNDEFINED
)
4520 fprintf (file
, "UNDEFINED");
4521 else if (vr
->type
== VR_RANGE
|| vr
->type
== VR_ANTI_RANGE
)
4523 tree type
= TREE_TYPE (vr
->min
);
4525 fprintf (file
, "%s[", (vr
->type
== VR_ANTI_RANGE
) ? "~" : "");
4527 if (is_negative_overflow_infinity (vr
->min
))
4528 fprintf (file
, "-INF(OVF)");
4529 else if (INTEGRAL_TYPE_P (type
)
4530 && !TYPE_UNSIGNED (type
)
4531 && vrp_val_is_min (vr
->min
))
4532 fprintf (file
, "-INF");
4534 print_generic_expr (file
, vr
->min
, 0);
4536 fprintf (file
, ", ");
4538 if (is_positive_overflow_infinity (vr
->max
))
4539 fprintf (file
, "+INF(OVF)");
4540 else if (INTEGRAL_TYPE_P (type
)
4541 && vrp_val_is_max (vr
->max
))
4542 fprintf (file
, "+INF");
4544 print_generic_expr (file
, vr
->max
, 0);
4546 fprintf (file
, "]");
4553 fprintf (file
, " EQUIVALENCES: { ");
4555 EXECUTE_IF_SET_IN_BITMAP (vr
->equiv
, 0, i
, bi
)
4557 print_generic_expr (file
, ssa_name (i
), 0);
4558 fprintf (file
, " ");
4562 fprintf (file
, "} (%u elements)", c
);
4565 else if (vr
->type
== VR_VARYING
)
4566 fprintf (file
, "VARYING");
4568 fprintf (file
, "INVALID RANGE");
4572 /* Dump value range VR to stderr. */
4575 debug_value_range (value_range_t
*vr
)
4577 dump_value_range (stderr
, vr
);
4578 fprintf (stderr
, "\n");
4582 /* Dump value ranges of all SSA_NAMEs to FILE. */
4585 dump_all_value_ranges (FILE *file
)
4589 for (i
= 0; i
< num_vr_values
; i
++)
4593 print_generic_expr (file
, ssa_name (i
), 0);
4594 fprintf (file
, ": ");
4595 dump_value_range (file
, vr_value
[i
]);
4596 fprintf (file
, "\n");
4600 fprintf (file
, "\n");
4604 /* Dump all value ranges to stderr. */
4607 debug_all_value_ranges (void)
4609 dump_all_value_ranges (stderr
);
4613 /* Given a COND_EXPR COND of the form 'V OP W', and an SSA name V,
4614 create a new SSA name N and return the assertion assignment
4615 'N = ASSERT_EXPR <V, V OP W>'. */
4618 build_assert_expr_for (tree cond
, tree v
)
4623 gcc_assert (TREE_CODE (v
) == SSA_NAME
4624 && COMPARISON_CLASS_P (cond
));
4626 a
= build2 (ASSERT_EXPR
, TREE_TYPE (v
), v
, cond
);
4627 assertion
= gimple_build_assign (NULL_TREE
, a
);
4629 /* The new ASSERT_EXPR, creates a new SSA name that replaces the
4630 operand of the ASSERT_EXPR. Create it so the new name and the old one
4631 are registered in the replacement table so that we can fix the SSA web
4632 after adding all the ASSERT_EXPRs. */
4633 create_new_def_for (v
, assertion
, NULL
);
4639 /* Return false if EXPR is a predicate expression involving floating
4643 fp_predicate (gimple stmt
)
4645 GIMPLE_CHECK (stmt
, GIMPLE_COND
);
4647 return FLOAT_TYPE_P (TREE_TYPE (gimple_cond_lhs (stmt
)));
4650 /* If the range of values taken by OP can be inferred after STMT executes,
4651 return the comparison code (COMP_CODE_P) and value (VAL_P) that
4652 describes the inferred range. Return true if a range could be
4656 infer_value_range (gimple stmt
, tree op
, enum tree_code
*comp_code_p
, tree
*val_p
)
4659 *comp_code_p
= ERROR_MARK
;
4661 /* Do not attempt to infer anything in names that flow through
4663 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op
))
4666 /* Similarly, don't infer anything from statements that may throw
4667 exceptions. ??? Relax this requirement? */
4668 if (stmt_could_throw_p (stmt
))
4671 /* If STMT is the last statement of a basic block with no normal
4672 successors, there is no point inferring anything about any of its
4673 operands. We would not be able to find a proper insertion point
4674 for the assertion, anyway. */
4675 if (stmt_ends_bb_p (stmt
))
4680 FOR_EACH_EDGE (e
, ei
, gimple_bb (stmt
)->succs
)
4681 if (!(e
->flags
& EDGE_ABNORMAL
))
4687 if (infer_nonnull_range (stmt
, op
, true, true))
4689 *val_p
= build_int_cst (TREE_TYPE (op
), 0);
4690 *comp_code_p
= NE_EXPR
;
4698 void dump_asserts_for (FILE *, tree
);
4699 void debug_asserts_for (tree
);
4700 void dump_all_asserts (FILE *);
4701 void debug_all_asserts (void);
4703 /* Dump all the registered assertions for NAME to FILE. */
4706 dump_asserts_for (FILE *file
, tree name
)
4710 fprintf (file
, "Assertions to be inserted for ");
4711 print_generic_expr (file
, name
, 0);
4712 fprintf (file
, "\n");
4714 loc
= asserts_for
[SSA_NAME_VERSION (name
)];
4717 fprintf (file
, "\t");
4718 print_gimple_stmt (file
, gsi_stmt (loc
->si
), 0, 0);
4719 fprintf (file
, "\n\tBB #%d", loc
->bb
->index
);
4722 fprintf (file
, "\n\tEDGE %d->%d", loc
->e
->src
->index
,
4723 loc
->e
->dest
->index
);
4724 dump_edge_info (file
, loc
->e
, dump_flags
, 0);
4726 fprintf (file
, "\n\tPREDICATE: ");
4727 print_generic_expr (file
, name
, 0);
4728 fprintf (file
, " %s ", get_tree_code_name (loc
->comp_code
));
4729 print_generic_expr (file
, loc
->val
, 0);
4730 fprintf (file
, "\n\n");
4734 fprintf (file
, "\n");
4738 /* Dump all the registered assertions for NAME to stderr. */
4741 debug_asserts_for (tree name
)
4743 dump_asserts_for (stderr
, name
);
4747 /* Dump all the registered assertions for all the names to FILE. */
4750 dump_all_asserts (FILE *file
)
4755 fprintf (file
, "\nASSERT_EXPRs to be inserted\n\n");
4756 EXECUTE_IF_SET_IN_BITMAP (need_assert_for
, 0, i
, bi
)
4757 dump_asserts_for (file
, ssa_name (i
));
4758 fprintf (file
, "\n");
4762 /* Dump all the registered assertions for all the names to stderr. */
4765 debug_all_asserts (void)
4767 dump_all_asserts (stderr
);
4771 /* If NAME doesn't have an ASSERT_EXPR registered for asserting
4772 'EXPR COMP_CODE VAL' at a location that dominates block BB or
4773 E->DEST, then register this location as a possible insertion point
4774 for ASSERT_EXPR <NAME, EXPR COMP_CODE VAL>.
4776 BB, E and SI provide the exact insertion point for the new
4777 ASSERT_EXPR. If BB is NULL, then the ASSERT_EXPR is to be inserted
4778 on edge E. Otherwise, if E is NULL, the ASSERT_EXPR is inserted on
4779 BB. If SI points to a COND_EXPR or a SWITCH_EXPR statement, then E
4780 must not be NULL. */
4783 register_new_assert_for (tree name
, tree expr
,
4784 enum tree_code comp_code
,
4788 gimple_stmt_iterator si
)
4790 assert_locus_t n
, loc
, last_loc
;
4791 basic_block dest_bb
;
4793 gcc_checking_assert (bb
== NULL
|| e
== NULL
);
4796 gcc_checking_assert (gimple_code (gsi_stmt (si
)) != GIMPLE_COND
4797 && gimple_code (gsi_stmt (si
)) != GIMPLE_SWITCH
);
4799 /* Never build an assert comparing against an integer constant with
4800 TREE_OVERFLOW set. This confuses our undefined overflow warning
4802 if (TREE_OVERFLOW_P (val
))
4803 val
= drop_tree_overflow (val
);
4805 /* The new assertion A will be inserted at BB or E. We need to
4806 determine if the new location is dominated by a previously
4807 registered location for A. If we are doing an edge insertion,
4808 assume that A will be inserted at E->DEST. Note that this is not
4811 If E is a critical edge, it will be split. But even if E is
4812 split, the new block will dominate the same set of blocks that
4815 The reverse, however, is not true, blocks dominated by E->DEST
4816 will not be dominated by the new block created to split E. So,
4817 if the insertion location is on a critical edge, we will not use
4818 the new location to move another assertion previously registered
4819 at a block dominated by E->DEST. */
4820 dest_bb
= (bb
) ? bb
: e
->dest
;
4822 /* If NAME already has an ASSERT_EXPR registered for COMP_CODE and
4823 VAL at a block dominating DEST_BB, then we don't need to insert a new
4824 one. Similarly, if the same assertion already exists at a block
4825 dominated by DEST_BB and the new location is not on a critical
4826 edge, then update the existing location for the assertion (i.e.,
4827 move the assertion up in the dominance tree).
4829 Note, this is implemented as a simple linked list because there
4830 should not be more than a handful of assertions registered per
4831 name. If this becomes a performance problem, a table hashed by
4832 COMP_CODE and VAL could be implemented. */
4833 loc
= asserts_for
[SSA_NAME_VERSION (name
)];
4837 if (loc
->comp_code
== comp_code
4839 || operand_equal_p (loc
->val
, val
, 0))
4840 && (loc
->expr
== expr
4841 || operand_equal_p (loc
->expr
, expr
, 0)))
4843 /* If E is not a critical edge and DEST_BB
4844 dominates the existing location for the assertion, move
4845 the assertion up in the dominance tree by updating its
4846 location information. */
4847 if ((e
== NULL
|| !EDGE_CRITICAL_P (e
))
4848 && dominated_by_p (CDI_DOMINATORS
, loc
->bb
, dest_bb
))
4857 /* Update the last node of the list and move to the next one. */
4862 /* If we didn't find an assertion already registered for
4863 NAME COMP_CODE VAL, add a new one at the end of the list of
4864 assertions associated with NAME. */
4865 n
= XNEW (struct assert_locus_d
);
4869 n
->comp_code
= comp_code
;
4877 asserts_for
[SSA_NAME_VERSION (name
)] = n
;
4879 bitmap_set_bit (need_assert_for
, SSA_NAME_VERSION (name
));
4882 /* (COND_OP0 COND_CODE COND_OP1) is a predicate which uses NAME.
4883 Extract a suitable test code and value and store them into *CODE_P and
4884 *VAL_P so the predicate is normalized to NAME *CODE_P *VAL_P.
4886 If no extraction was possible, return FALSE, otherwise return TRUE.
4888 If INVERT is true, then we invert the result stored into *CODE_P. */
4891 extract_code_and_val_from_cond_with_ops (tree name
, enum tree_code cond_code
,
4892 tree cond_op0
, tree cond_op1
,
4893 bool invert
, enum tree_code
*code_p
,
4896 enum tree_code comp_code
;
4899 /* Otherwise, we have a comparison of the form NAME COMP VAL
4900 or VAL COMP NAME. */
4901 if (name
== cond_op1
)
4903 /* If the predicate is of the form VAL COMP NAME, flip
4904 COMP around because we need to register NAME as the
4905 first operand in the predicate. */
4906 comp_code
= swap_tree_comparison (cond_code
);
4911 /* The comparison is of the form NAME COMP VAL, so the
4912 comparison code remains unchanged. */
4913 comp_code
= cond_code
;
4917 /* Invert the comparison code as necessary. */
4919 comp_code
= invert_tree_comparison (comp_code
, 0);
4921 /* VRP does not handle float types. */
4922 if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (val
)))
4925 /* Do not register always-false predicates.
4926 FIXME: this works around a limitation in fold() when dealing with
4927 enumerations. Given 'enum { N1, N2 } x;', fold will not
4928 fold 'if (x > N2)' to 'if (0)'. */
4929 if ((comp_code
== GT_EXPR
|| comp_code
== LT_EXPR
)
4930 && INTEGRAL_TYPE_P (TREE_TYPE (val
)))
4932 tree min
= TYPE_MIN_VALUE (TREE_TYPE (val
));
4933 tree max
= TYPE_MAX_VALUE (TREE_TYPE (val
));
4935 if (comp_code
== GT_EXPR
4937 || compare_values (val
, max
) == 0))
4940 if (comp_code
== LT_EXPR
4942 || compare_values (val
, min
) == 0))
4945 *code_p
= comp_code
;
4950 /* Find out smallest RES where RES > VAL && (RES & MASK) == RES, if any
4951 (otherwise return VAL). VAL and MASK must be zero-extended for
4952 precision PREC. If SGNBIT is non-zero, first xor VAL with SGNBIT
4953 (to transform signed values into unsigned) and at the end xor
4957 masked_increment (const wide_int
&val_in
, const wide_int
&mask
,
4958 const wide_int
&sgnbit
, unsigned int prec
)
4960 wide_int bit
= wi::one (prec
), res
;
4963 wide_int val
= val_in
^ sgnbit
;
4964 for (i
= 0; i
< prec
; i
++, bit
+= bit
)
4967 if ((res
& bit
) == 0)
4970 res
= (val
+ bit
).and_not (res
);
4972 if (wi::gtu_p (res
, val
))
4973 return res
^ sgnbit
;
4975 return val
^ sgnbit
;
4978 /* Try to register an edge assertion for SSA name NAME on edge E for
4979 the condition COND contributing to the conditional jump pointed to by BSI.
4980 Invert the condition COND if INVERT is true. */
4983 register_edge_assert_for_2 (tree name
, edge e
, gimple_stmt_iterator bsi
,
4984 enum tree_code cond_code
,
4985 tree cond_op0
, tree cond_op1
, bool invert
)
4988 enum tree_code comp_code
;
4990 if (!extract_code_and_val_from_cond_with_ops (name
, cond_code
,
4993 invert
, &comp_code
, &val
))
4996 /* Only register an ASSERT_EXPR if NAME was found in the sub-graph
4997 reachable from E. */
4998 if (live_on_edge (e
, name
)
4999 && !has_single_use (name
))
5000 register_new_assert_for (name
, name
, comp_code
, val
, NULL
, e
, bsi
);
5002 /* In the case of NAME <= CST and NAME being defined as
5003 NAME = (unsigned) NAME2 + CST2 we can assert NAME2 >= -CST2
5004 and NAME2 <= CST - CST2. We can do the same for NAME > CST.
5005 This catches range and anti-range tests. */
5006 if ((comp_code
== LE_EXPR
5007 || comp_code
== GT_EXPR
)
5008 && TREE_CODE (val
) == INTEGER_CST
5009 && TYPE_UNSIGNED (TREE_TYPE (val
)))
5011 gimple def_stmt
= SSA_NAME_DEF_STMT (name
);
5012 tree cst2
= NULL_TREE
, name2
= NULL_TREE
, name3
= NULL_TREE
;
5014 /* Extract CST2 from the (optional) addition. */
5015 if (is_gimple_assign (def_stmt
)
5016 && gimple_assign_rhs_code (def_stmt
) == PLUS_EXPR
)
5018 name2
= gimple_assign_rhs1 (def_stmt
);
5019 cst2
= gimple_assign_rhs2 (def_stmt
);
5020 if (TREE_CODE (name2
) == SSA_NAME
5021 && TREE_CODE (cst2
) == INTEGER_CST
)
5022 def_stmt
= SSA_NAME_DEF_STMT (name2
);
5025 /* Extract NAME2 from the (optional) sign-changing cast. */
5026 if (gimple_assign_cast_p (def_stmt
))
5028 if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt
))
5029 && ! TYPE_UNSIGNED (TREE_TYPE (gimple_assign_rhs1 (def_stmt
)))
5030 && (TYPE_PRECISION (gimple_expr_type (def_stmt
))
5031 == TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt
)))))
5032 name3
= gimple_assign_rhs1 (def_stmt
);
5035 /* If name3 is used later, create an ASSERT_EXPR for it. */
5036 if (name3
!= NULL_TREE
5037 && TREE_CODE (name3
) == SSA_NAME
5038 && (cst2
== NULL_TREE
5039 || TREE_CODE (cst2
) == INTEGER_CST
)
5040 && INTEGRAL_TYPE_P (TREE_TYPE (name3
))
5041 && live_on_edge (e
, name3
)
5042 && !has_single_use (name3
))
5046 /* Build an expression for the range test. */
5047 tmp
= build1 (NOP_EXPR
, TREE_TYPE (name
), name3
);
5048 if (cst2
!= NULL_TREE
)
5049 tmp
= build2 (PLUS_EXPR
, TREE_TYPE (name
), tmp
, cst2
);
5053 fprintf (dump_file
, "Adding assert for ");
5054 print_generic_expr (dump_file
, name3
, 0);
5055 fprintf (dump_file
, " from ");
5056 print_generic_expr (dump_file
, tmp
, 0);
5057 fprintf (dump_file
, "\n");
5060 register_new_assert_for (name3
, tmp
, comp_code
, val
, NULL
, e
, bsi
);
5063 /* If name2 is used later, create an ASSERT_EXPR for it. */
5064 if (name2
!= NULL_TREE
5065 && TREE_CODE (name2
) == SSA_NAME
5066 && TREE_CODE (cst2
) == INTEGER_CST
5067 && INTEGRAL_TYPE_P (TREE_TYPE (name2
))
5068 && live_on_edge (e
, name2
)
5069 && !has_single_use (name2
))
5073 /* Build an expression for the range test. */
5075 if (TREE_TYPE (name
) != TREE_TYPE (name2
))
5076 tmp
= build1 (NOP_EXPR
, TREE_TYPE (name
), tmp
);
5077 if (cst2
!= NULL_TREE
)
5078 tmp
= build2 (PLUS_EXPR
, TREE_TYPE (name
), tmp
, cst2
);
5082 fprintf (dump_file
, "Adding assert for ");
5083 print_generic_expr (dump_file
, name2
, 0);
5084 fprintf (dump_file
, " from ");
5085 print_generic_expr (dump_file
, tmp
, 0);
5086 fprintf (dump_file
, "\n");
5089 register_new_assert_for (name2
, tmp
, comp_code
, val
, NULL
, e
, bsi
);
5093 /* In the case of post-in/decrement tests like if (i++) ... and uses
5094 of the in/decremented value on the edge the extra name we want to
5095 assert for is not on the def chain of the name compared. Instead
5096 it is in the set of use stmts. */
5097 if ((comp_code
== NE_EXPR
5098 || comp_code
== EQ_EXPR
)
5099 && TREE_CODE (val
) == INTEGER_CST
)
5101 imm_use_iterator ui
;
5103 FOR_EACH_IMM_USE_STMT (use_stmt
, ui
, name
)
5105 /* Cut off to use-stmts that are in the predecessor. */
5106 if (gimple_bb (use_stmt
) != e
->src
)
5109 if (!is_gimple_assign (use_stmt
))
5112 enum tree_code code
= gimple_assign_rhs_code (use_stmt
);
5113 if (code
!= PLUS_EXPR
5114 && code
!= MINUS_EXPR
)
5117 tree cst
= gimple_assign_rhs2 (use_stmt
);
5118 if (TREE_CODE (cst
) != INTEGER_CST
)
5121 tree name2
= gimple_assign_lhs (use_stmt
);
5122 if (live_on_edge (e
, name2
))
5124 cst
= int_const_binop (code
, val
, cst
);
5125 register_new_assert_for (name2
, name2
, comp_code
, cst
,
5131 if (TREE_CODE_CLASS (comp_code
) == tcc_comparison
5132 && TREE_CODE (val
) == INTEGER_CST
)
5134 gimple def_stmt
= SSA_NAME_DEF_STMT (name
);
5135 tree name2
= NULL_TREE
, names
[2], cst2
= NULL_TREE
;
5136 tree val2
= NULL_TREE
;
5137 unsigned int prec
= TYPE_PRECISION (TREE_TYPE (val
));
5138 wide_int mask
= wi::zero (prec
);
5139 unsigned int nprec
= prec
;
5140 enum tree_code rhs_code
= ERROR_MARK
;
5142 if (is_gimple_assign (def_stmt
))
5143 rhs_code
= gimple_assign_rhs_code (def_stmt
);
5145 /* Add asserts for NAME cmp CST and NAME being defined
5146 as NAME = (int) NAME2. */
5147 if (!TYPE_UNSIGNED (TREE_TYPE (val
))
5148 && (comp_code
== LE_EXPR
|| comp_code
== LT_EXPR
5149 || comp_code
== GT_EXPR
|| comp_code
== GE_EXPR
)
5150 && gimple_assign_cast_p (def_stmt
))
5152 name2
= gimple_assign_rhs1 (def_stmt
);
5153 if (CONVERT_EXPR_CODE_P (rhs_code
)
5154 && INTEGRAL_TYPE_P (TREE_TYPE (name2
))
5155 && TYPE_UNSIGNED (TREE_TYPE (name2
))
5156 && prec
== TYPE_PRECISION (TREE_TYPE (name2
))
5157 && (comp_code
== LE_EXPR
|| comp_code
== GT_EXPR
5158 || !tree_int_cst_equal (val
,
5159 TYPE_MIN_VALUE (TREE_TYPE (val
))))
5160 && live_on_edge (e
, name2
)
5161 && !has_single_use (name2
))
5164 enum tree_code new_comp_code
= comp_code
;
5166 cst
= fold_convert (TREE_TYPE (name2
),
5167 TYPE_MIN_VALUE (TREE_TYPE (val
)));
5168 /* Build an expression for the range test. */
5169 tmp
= build2 (PLUS_EXPR
, TREE_TYPE (name2
), name2
, cst
);
5170 cst
= fold_build2 (PLUS_EXPR
, TREE_TYPE (name2
), cst
,
5171 fold_convert (TREE_TYPE (name2
), val
));
5172 if (comp_code
== LT_EXPR
|| comp_code
== GE_EXPR
)
5174 new_comp_code
= comp_code
== LT_EXPR
? LE_EXPR
: GT_EXPR
;
5175 cst
= fold_build2 (MINUS_EXPR
, TREE_TYPE (name2
), cst
,
5176 build_int_cst (TREE_TYPE (name2
), 1));
5181 fprintf (dump_file
, "Adding assert for ");
5182 print_generic_expr (dump_file
, name2
, 0);
5183 fprintf (dump_file
, " from ");
5184 print_generic_expr (dump_file
, tmp
, 0);
5185 fprintf (dump_file
, "\n");
5188 register_new_assert_for (name2
, tmp
, new_comp_code
, cst
, NULL
,
5193 /* Add asserts for NAME cmp CST and NAME being defined as
5194 NAME = NAME2 >> CST2.
5196 Extract CST2 from the right shift. */
5197 if (rhs_code
== RSHIFT_EXPR
)
5199 name2
= gimple_assign_rhs1 (def_stmt
);
5200 cst2
= gimple_assign_rhs2 (def_stmt
);
5201 if (TREE_CODE (name2
) == SSA_NAME
5202 && tree_fits_uhwi_p (cst2
)
5203 && INTEGRAL_TYPE_P (TREE_TYPE (name2
))
5204 && IN_RANGE (tree_to_uhwi (cst2
), 1, prec
- 1)
5205 && prec
== GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (val
)))
5206 && live_on_edge (e
, name2
)
5207 && !has_single_use (name2
))
5209 mask
= wi::mask (tree_to_uhwi (cst2
), false, prec
);
5210 val2
= fold_binary (LSHIFT_EXPR
, TREE_TYPE (val
), val
, cst2
);
5213 if (val2
!= NULL_TREE
5214 && TREE_CODE (val2
) == INTEGER_CST
5215 && simple_cst_equal (fold_build2 (RSHIFT_EXPR
,
5219 enum tree_code new_comp_code
= comp_code
;
5223 if (comp_code
== EQ_EXPR
|| comp_code
== NE_EXPR
)
5225 if (!TYPE_UNSIGNED (TREE_TYPE (val
)))
5227 tree type
= build_nonstandard_integer_type (prec
, 1);
5228 tmp
= build1 (NOP_EXPR
, type
, name2
);
5229 val2
= fold_convert (type
, val2
);
5231 tmp
= fold_build2 (MINUS_EXPR
, TREE_TYPE (tmp
), tmp
, val2
);
5232 new_val
= wide_int_to_tree (TREE_TYPE (tmp
), mask
);
5233 new_comp_code
= comp_code
== EQ_EXPR
? LE_EXPR
: GT_EXPR
;
5235 else if (comp_code
== LT_EXPR
|| comp_code
== GE_EXPR
)
5238 = wi::min_value (prec
, TYPE_SIGN (TREE_TYPE (val
)));
5240 if (minval
== new_val
)
5241 new_val
= NULL_TREE
;
5246 = wi::max_value (prec
, TYPE_SIGN (TREE_TYPE (val
)));
5249 new_val
= NULL_TREE
;
5251 new_val
= wide_int_to_tree (TREE_TYPE (val2
), mask
);
5258 fprintf (dump_file
, "Adding assert for ");
5259 print_generic_expr (dump_file
, name2
, 0);
5260 fprintf (dump_file
, " from ");
5261 print_generic_expr (dump_file
, tmp
, 0);
5262 fprintf (dump_file
, "\n");
5265 register_new_assert_for (name2
, tmp
, new_comp_code
, new_val
,
5270 /* Add asserts for NAME cmp CST and NAME being defined as
5271 NAME = NAME2 & CST2.
5273 Extract CST2 from the and.
5276 NAME = (unsigned) NAME2;
5277 casts where NAME's type is unsigned and has smaller precision
5278 than NAME2's type as if it was NAME = NAME2 & MASK. */
5279 names
[0] = NULL_TREE
;
5280 names
[1] = NULL_TREE
;
5282 if (rhs_code
== BIT_AND_EXPR
5283 || (CONVERT_EXPR_CODE_P (rhs_code
)
5284 && TREE_CODE (TREE_TYPE (val
)) == INTEGER_TYPE
5285 && TYPE_UNSIGNED (TREE_TYPE (val
))
5286 && TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt
)))
5289 name2
= gimple_assign_rhs1 (def_stmt
);
5290 if (rhs_code
== BIT_AND_EXPR
)
5291 cst2
= gimple_assign_rhs2 (def_stmt
);
5294 cst2
= TYPE_MAX_VALUE (TREE_TYPE (val
));
5295 nprec
= TYPE_PRECISION (TREE_TYPE (name2
));
5297 if (TREE_CODE (name2
) == SSA_NAME
5298 && INTEGRAL_TYPE_P (TREE_TYPE (name2
))
5299 && TREE_CODE (cst2
) == INTEGER_CST
5300 && !integer_zerop (cst2
)
5302 || TYPE_UNSIGNED (TREE_TYPE (val
))))
5304 gimple def_stmt2
= SSA_NAME_DEF_STMT (name2
);
5305 if (gimple_assign_cast_p (def_stmt2
))
5307 names
[1] = gimple_assign_rhs1 (def_stmt2
);
5308 if (!CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt2
))
5309 || !INTEGRAL_TYPE_P (TREE_TYPE (names
[1]))
5310 || (TYPE_PRECISION (TREE_TYPE (name2
))
5311 != TYPE_PRECISION (TREE_TYPE (names
[1])))
5312 || !live_on_edge (e
, names
[1])
5313 || has_single_use (names
[1]))
5314 names
[1] = NULL_TREE
;
5316 if (live_on_edge (e
, name2
)
5317 && !has_single_use (name2
))
5321 if (names
[0] || names
[1])
5323 wide_int minv
, maxv
, valv
, cst2v
;
5324 wide_int tem
, sgnbit
;
5325 bool valid_p
= false, valn
, cst2n
;
5326 enum tree_code ccode
= comp_code
;
5328 valv
= wide_int::from (val
, nprec
, UNSIGNED
);
5329 cst2v
= wide_int::from (cst2
, nprec
, UNSIGNED
);
5330 valn
= wi::neg_p (valv
, TYPE_SIGN (TREE_TYPE (val
)));
5331 cst2n
= wi::neg_p (cst2v
, TYPE_SIGN (TREE_TYPE (val
)));
5332 /* If CST2 doesn't have most significant bit set,
5333 but VAL is negative, we have comparison like
5334 if ((x & 0x123) > -4) (always true). Just give up. */
5338 sgnbit
= wi::set_bit_in_zero (nprec
- 1, nprec
);
5340 sgnbit
= wi::zero (nprec
);
5341 minv
= valv
& cst2v
;
5345 /* Minimum unsigned value for equality is VAL & CST2
5346 (should be equal to VAL, otherwise we probably should
5347 have folded the comparison into false) and
5348 maximum unsigned value is VAL | ~CST2. */
5349 maxv
= valv
| ~cst2v
;
5354 tem
= valv
| ~cst2v
;
5355 /* If VAL is 0, handle (X & CST2) != 0 as (X & CST2) > 0U. */
5359 sgnbit
= wi::zero (nprec
);
5362 /* If (VAL | ~CST2) is all ones, handle it as
5363 (X & CST2) < VAL. */
5368 sgnbit
= wi::zero (nprec
);
5371 if (!cst2n
&& wi::neg_p (cst2v
))
5372 sgnbit
= wi::set_bit_in_zero (nprec
- 1, nprec
);
5381 if (tem
== wi::mask (nprec
- 1, false, nprec
))
5387 sgnbit
= wi::zero (nprec
);
5392 /* Minimum unsigned value for >= if (VAL & CST2) == VAL
5393 is VAL and maximum unsigned value is ~0. For signed
5394 comparison, if CST2 doesn't have most significant bit
5395 set, handle it similarly. If CST2 has MSB set,
5396 the minimum is the same, and maximum is ~0U/2. */
5399 /* If (VAL & CST2) != VAL, X & CST2 can't be equal to
5401 minv
= masked_increment (valv
, cst2v
, sgnbit
, nprec
);
5405 maxv
= wi::mask (nprec
- (cst2n
? 1 : 0), false, nprec
);
5411 /* Find out smallest MINV where MINV > VAL
5412 && (MINV & CST2) == MINV, if any. If VAL is signed and
5413 CST2 has MSB set, compute it biased by 1 << (nprec - 1). */
5414 minv
= masked_increment (valv
, cst2v
, sgnbit
, nprec
);
5417 maxv
= wi::mask (nprec
- (cst2n
? 1 : 0), false, nprec
);
5422 /* Minimum unsigned value for <= is 0 and maximum
5423 unsigned value is VAL | ~CST2 if (VAL & CST2) == VAL.
5424 Otherwise, find smallest VAL2 where VAL2 > VAL
5425 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
5427 For signed comparison, if CST2 doesn't have most
5428 significant bit set, handle it similarly. If CST2 has
5429 MSB set, the maximum is the same and minimum is INT_MIN. */
5434 maxv
= masked_increment (valv
, cst2v
, sgnbit
, nprec
);
5446 /* Minimum unsigned value for < is 0 and maximum
5447 unsigned value is (VAL-1) | ~CST2 if (VAL & CST2) == VAL.
5448 Otherwise, find smallest VAL2 where VAL2 > VAL
5449 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
5451 For signed comparison, if CST2 doesn't have most
5452 significant bit set, handle it similarly. If CST2 has
5453 MSB set, the maximum is the same and minimum is INT_MIN. */
5462 maxv
= masked_increment (valv
, cst2v
, sgnbit
, nprec
);
5476 && (maxv
- minv
) != -1)
5478 tree tmp
, new_val
, type
;
5481 for (i
= 0; i
< 2; i
++)
5484 wide_int maxv2
= maxv
;
5486 type
= TREE_TYPE (names
[i
]);
5487 if (!TYPE_UNSIGNED (type
))
5489 type
= build_nonstandard_integer_type (nprec
, 1);
5490 tmp
= build1 (NOP_EXPR
, type
, names
[i
]);
5494 tmp
= build2 (PLUS_EXPR
, type
, tmp
,
5495 wide_int_to_tree (type
, -minv
));
5496 maxv2
= maxv
- minv
;
5498 new_val
= wide_int_to_tree (type
, maxv2
);
5502 fprintf (dump_file
, "Adding assert for ");
5503 print_generic_expr (dump_file
, names
[i
], 0);
5504 fprintf (dump_file
, " from ");
5505 print_generic_expr (dump_file
, tmp
, 0);
5506 fprintf (dump_file
, "\n");
5509 register_new_assert_for (names
[i
], tmp
, LE_EXPR
,
5510 new_val
, NULL
, e
, bsi
);
5517 /* OP is an operand of a truth value expression which is known to have
5518 a particular value. Register any asserts for OP and for any
5519 operands in OP's defining statement.
5521 If CODE is EQ_EXPR, then we want to register OP is zero (false),
5522 if CODE is NE_EXPR, then we want to register OP is nonzero (true). */
5525 register_edge_assert_for_1 (tree op
, enum tree_code code
,
5526 edge e
, gimple_stmt_iterator bsi
)
5530 enum tree_code rhs_code
;
5532 /* We only care about SSA_NAMEs. */
5533 if (TREE_CODE (op
) != SSA_NAME
)
5536 /* We know that OP will have a zero or nonzero value. If OP is used
5537 more than once go ahead and register an assert for OP. */
5538 if (live_on_edge (e
, op
)
5539 && !has_single_use (op
))
5541 val
= build_int_cst (TREE_TYPE (op
), 0);
5542 register_new_assert_for (op
, op
, code
, val
, NULL
, e
, bsi
);
5545 /* Now look at how OP is set. If it's set from a comparison,
5546 a truth operation or some bit operations, then we may be able
5547 to register information about the operands of that assignment. */
5548 op_def
= SSA_NAME_DEF_STMT (op
);
5549 if (gimple_code (op_def
) != GIMPLE_ASSIGN
)
5552 rhs_code
= gimple_assign_rhs_code (op_def
);
5554 if (TREE_CODE_CLASS (rhs_code
) == tcc_comparison
)
5556 bool invert
= (code
== EQ_EXPR
? true : false);
5557 tree op0
= gimple_assign_rhs1 (op_def
);
5558 tree op1
= gimple_assign_rhs2 (op_def
);
5560 if (TREE_CODE (op0
) == SSA_NAME
)
5561 register_edge_assert_for_2 (op0
, e
, bsi
, rhs_code
, op0
, op1
, invert
);
5562 if (TREE_CODE (op1
) == SSA_NAME
)
5563 register_edge_assert_for_2 (op1
, e
, bsi
, rhs_code
, op0
, op1
, invert
);
5565 else if ((code
== NE_EXPR
5566 && gimple_assign_rhs_code (op_def
) == BIT_AND_EXPR
)
5568 && gimple_assign_rhs_code (op_def
) == BIT_IOR_EXPR
))
5570 /* Recurse on each operand. */
5571 tree op0
= gimple_assign_rhs1 (op_def
);
5572 tree op1
= gimple_assign_rhs2 (op_def
);
5573 if (TREE_CODE (op0
) == SSA_NAME
5574 && has_single_use (op0
))
5575 register_edge_assert_for_1 (op0
, code
, e
, bsi
);
5576 if (TREE_CODE (op1
) == SSA_NAME
5577 && has_single_use (op1
))
5578 register_edge_assert_for_1 (op1
, code
, e
, bsi
);
5580 else if (gimple_assign_rhs_code (op_def
) == BIT_NOT_EXPR
5581 && TYPE_PRECISION (TREE_TYPE (gimple_assign_lhs (op_def
))) == 1)
5583 /* Recurse, flipping CODE. */
5584 code
= invert_tree_comparison (code
, false);
5585 register_edge_assert_for_1 (gimple_assign_rhs1 (op_def
), code
, e
, bsi
);
5587 else if (gimple_assign_rhs_code (op_def
) == SSA_NAME
)
5589 /* Recurse through the copy. */
5590 register_edge_assert_for_1 (gimple_assign_rhs1 (op_def
), code
, e
, bsi
);
5592 else if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (op_def
)))
5594 /* Recurse through the type conversion, unless it is a narrowing
5595 conversion or conversion from non-integral type. */
5596 tree rhs
= gimple_assign_rhs1 (op_def
);
5597 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs
))
5598 && (TYPE_PRECISION (TREE_TYPE (rhs
))
5599 <= TYPE_PRECISION (TREE_TYPE (op
))))
5600 register_edge_assert_for_1 (rhs
, code
, e
, bsi
);
5604 /* Try to register an edge assertion for SSA name NAME on edge E for
5605 the condition COND contributing to the conditional jump pointed to by
5609 register_edge_assert_for (tree name
, edge e
, gimple_stmt_iterator si
,
5610 enum tree_code cond_code
, tree cond_op0
,
5614 enum tree_code comp_code
;
5615 bool is_else_edge
= (e
->flags
& EDGE_FALSE_VALUE
) != 0;
5617 /* Do not attempt to infer anything in names that flow through
5619 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name
))
5622 if (!extract_code_and_val_from_cond_with_ops (name
, cond_code
,
5628 /* Register ASSERT_EXPRs for name. */
5629 register_edge_assert_for_2 (name
, e
, si
, cond_code
, cond_op0
,
5630 cond_op1
, is_else_edge
);
5633 /* If COND is effectively an equality test of an SSA_NAME against
5634 the value zero or one, then we may be able to assert values
5635 for SSA_NAMEs which flow into COND. */
5637 /* In the case of NAME == 1 or NAME != 0, for BIT_AND_EXPR defining
5638 statement of NAME we can assert both operands of the BIT_AND_EXPR
5639 have nonzero value. */
5640 if (((comp_code
== EQ_EXPR
&& integer_onep (val
))
5641 || (comp_code
== NE_EXPR
&& integer_zerop (val
))))
5643 gimple def_stmt
= SSA_NAME_DEF_STMT (name
);
5645 if (is_gimple_assign (def_stmt
)
5646 && gimple_assign_rhs_code (def_stmt
) == BIT_AND_EXPR
)
5648 tree op0
= gimple_assign_rhs1 (def_stmt
);
5649 tree op1
= gimple_assign_rhs2 (def_stmt
);
5650 register_edge_assert_for_1 (op0
, NE_EXPR
, e
, si
);
5651 register_edge_assert_for_1 (op1
, NE_EXPR
, e
, si
);
5655 /* In the case of NAME == 0 or NAME != 1, for BIT_IOR_EXPR defining
5656 statement of NAME we can assert both operands of the BIT_IOR_EXPR
5658 if (((comp_code
== EQ_EXPR
&& integer_zerop (val
))
5659 || (comp_code
== NE_EXPR
&& integer_onep (val
))))
5661 gimple def_stmt
= SSA_NAME_DEF_STMT (name
);
5663 /* For BIT_IOR_EXPR only if NAME == 0 both operands have
5664 necessarily zero value, or if type-precision is one. */
5665 if (is_gimple_assign (def_stmt
)
5666 && (gimple_assign_rhs_code (def_stmt
) == BIT_IOR_EXPR
5667 && (TYPE_PRECISION (TREE_TYPE (name
)) == 1
5668 || comp_code
== EQ_EXPR
)))
5670 tree op0
= gimple_assign_rhs1 (def_stmt
);
5671 tree op1
= gimple_assign_rhs2 (def_stmt
);
5672 register_edge_assert_for_1 (op0
, EQ_EXPR
, e
, si
);
5673 register_edge_assert_for_1 (op1
, EQ_EXPR
, e
, si
);
5679 /* Determine whether the outgoing edges of BB should receive an
5680 ASSERT_EXPR for each of the operands of BB's LAST statement.
5681 The last statement of BB must be a COND_EXPR.
5683 If any of the sub-graphs rooted at BB have an interesting use of
5684 the predicate operands, an assert location node is added to the
5685 list of assertions for the corresponding operands. */
5688 find_conditional_asserts (basic_block bb
, gimple last
)
5690 gimple_stmt_iterator bsi
;
5696 bsi
= gsi_for_stmt (last
);
5698 /* Look for uses of the operands in each of the sub-graphs
5699 rooted at BB. We need to check each of the outgoing edges
5700 separately, so that we know what kind of ASSERT_EXPR to
5702 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
5707 /* Register the necessary assertions for each operand in the
5708 conditional predicate. */
5709 FOR_EACH_SSA_TREE_OPERAND (op
, last
, iter
, SSA_OP_USE
)
5710 register_edge_assert_for (op
, e
, bsi
,
5711 gimple_cond_code (last
),
5712 gimple_cond_lhs (last
),
5713 gimple_cond_rhs (last
));
5723 /* Compare two case labels sorting first by the destination bb index
5724 and then by the case value. */
5727 compare_case_labels (const void *p1
, const void *p2
)
5729 const struct case_info
*ci1
= (const struct case_info
*) p1
;
5730 const struct case_info
*ci2
= (const struct case_info
*) p2
;
5731 int idx1
= ci1
->bb
->index
;
5732 int idx2
= ci2
->bb
->index
;
5736 else if (idx1
== idx2
)
5738 /* Make sure the default label is first in a group. */
5739 if (!CASE_LOW (ci1
->expr
))
5741 else if (!CASE_LOW (ci2
->expr
))
5744 return tree_int_cst_compare (CASE_LOW (ci1
->expr
),
5745 CASE_LOW (ci2
->expr
));
5751 /* Determine whether the outgoing edges of BB should receive an
5752 ASSERT_EXPR for each of the operands of BB's LAST statement.
5753 The last statement of BB must be a SWITCH_EXPR.
5755 If any of the sub-graphs rooted at BB have an interesting use of
5756 the predicate operands, an assert location node is added to the
5757 list of assertions for the corresponding operands. */
5760 find_switch_asserts (basic_block bb
, gimple last
)
5762 gimple_stmt_iterator bsi
;
5765 struct case_info
*ci
;
5766 size_t n
= gimple_switch_num_labels (last
);
5767 #if GCC_VERSION >= 4000
5770 /* Work around GCC 3.4 bug (PR 37086). */
5771 volatile unsigned int idx
;
5774 bsi
= gsi_for_stmt (last
);
5775 op
= gimple_switch_index (last
);
5776 if (TREE_CODE (op
) != SSA_NAME
)
5779 /* Build a vector of case labels sorted by destination label. */
5780 ci
= XNEWVEC (struct case_info
, n
);
5781 for (idx
= 0; idx
< n
; ++idx
)
5783 ci
[idx
].expr
= gimple_switch_label (last
, idx
);
5784 ci
[idx
].bb
= label_to_block (CASE_LABEL (ci
[idx
].expr
));
5786 qsort (ci
, n
, sizeof (struct case_info
), compare_case_labels
);
5788 for (idx
= 0; idx
< n
; ++idx
)
5791 tree cl
= ci
[idx
].expr
;
5792 basic_block cbb
= ci
[idx
].bb
;
5794 min
= CASE_LOW (cl
);
5795 max
= CASE_HIGH (cl
);
5797 /* If there are multiple case labels with the same destination
5798 we need to combine them to a single value range for the edge. */
5799 if (idx
+ 1 < n
&& cbb
== ci
[idx
+ 1].bb
)
5801 /* Skip labels until the last of the group. */
5804 } while (idx
< n
&& cbb
== ci
[idx
].bb
);
5807 /* Pick up the maximum of the case label range. */
5808 if (CASE_HIGH (ci
[idx
].expr
))
5809 max
= CASE_HIGH (ci
[idx
].expr
);
5811 max
= CASE_LOW (ci
[idx
].expr
);
5814 /* Nothing to do if the range includes the default label until we
5815 can register anti-ranges. */
5816 if (min
== NULL_TREE
)
5819 /* Find the edge to register the assert expr on. */
5820 e
= find_edge (bb
, cbb
);
5822 /* Register the necessary assertions for the operand in the
5824 register_edge_assert_for (op
, e
, bsi
,
5825 max
? GE_EXPR
: EQ_EXPR
,
5826 op
, fold_convert (TREE_TYPE (op
), min
));
5828 register_edge_assert_for (op
, e
, bsi
, LE_EXPR
, op
,
5829 fold_convert (TREE_TYPE (op
), max
));
5836 /* Traverse all the statements in block BB looking for statements that
5837 may generate useful assertions for the SSA names in their operand.
5838 If a statement produces a useful assertion A for name N_i, then the
5839 list of assertions already generated for N_i is scanned to
5840 determine if A is actually needed.
5842 If N_i already had the assertion A at a location dominating the
5843 current location, then nothing needs to be done. Otherwise, the
5844 new location for A is recorded instead.
5846 1- For every statement S in BB, all the variables used by S are
5847 added to bitmap FOUND_IN_SUBGRAPH.
5849 2- If statement S uses an operand N in a way that exposes a known
5850 value range for N, then if N was not already generated by an
5851 ASSERT_EXPR, create a new assert location for N. For instance,
5852 if N is a pointer and the statement dereferences it, we can
5853 assume that N is not NULL.
5855 3- COND_EXPRs are a special case of #2. We can derive range
5856 information from the predicate but need to insert different
5857 ASSERT_EXPRs for each of the sub-graphs rooted at the
5858 conditional block. If the last statement of BB is a conditional
5859 expression of the form 'X op Y', then
5861 a) Remove X and Y from the set FOUND_IN_SUBGRAPH.
5863 b) If the conditional is the only entry point to the sub-graph
5864 corresponding to the THEN_CLAUSE, recurse into it. On
5865 return, if X and/or Y are marked in FOUND_IN_SUBGRAPH, then
5866 an ASSERT_EXPR is added for the corresponding variable.
5868 c) Repeat step (b) on the ELSE_CLAUSE.
5870 d) Mark X and Y in FOUND_IN_SUBGRAPH.
5879 In this case, an assertion on the THEN clause is useful to
5880 determine that 'a' is always 9 on that edge. However, an assertion
5881 on the ELSE clause would be unnecessary.
5883 4- If BB does not end in a conditional expression, then we recurse
5884 into BB's dominator children.
5886 At the end of the recursive traversal, every SSA name will have a
5887 list of locations where ASSERT_EXPRs should be added. When a new
5888 location for name N is found, it is registered by calling
5889 register_new_assert_for. That function keeps track of all the
5890 registered assertions to prevent adding unnecessary assertions.
5891 For instance, if a pointer P_4 is dereferenced more than once in a
5892 dominator tree, only the location dominating all the dereference of
5893 P_4 will receive an ASSERT_EXPR. */
5896 find_assert_locations_1 (basic_block bb
, sbitmap live
)
5898 gimple_stmt_iterator si
;
5901 last
= last_stmt (bb
);
5903 /* If BB's last statement is a conditional statement involving integer
5904 operands, determine if we need to add ASSERT_EXPRs. */
5906 && gimple_code (last
) == GIMPLE_COND
5907 && !fp_predicate (last
)
5908 && !ZERO_SSA_OPERANDS (last
, SSA_OP_USE
))
5909 find_conditional_asserts (bb
, last
);
5911 /* If BB's last statement is a switch statement involving integer
5912 operands, determine if we need to add ASSERT_EXPRs. */
5914 && gimple_code (last
) == GIMPLE_SWITCH
5915 && !ZERO_SSA_OPERANDS (last
, SSA_OP_USE
))
5916 find_switch_asserts (bb
, last
);
5918 /* Traverse all the statements in BB marking used names and looking
5919 for statements that may infer assertions for their used operands. */
5920 for (si
= gsi_last_bb (bb
); !gsi_end_p (si
); gsi_prev (&si
))
5926 stmt
= gsi_stmt (si
);
5928 if (is_gimple_debug (stmt
))
5931 /* See if we can derive an assertion for any of STMT's operands. */
5932 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, i
, SSA_OP_USE
)
5935 enum tree_code comp_code
;
5937 /* If op is not live beyond this stmt, do not bother to insert
5939 if (!bitmap_bit_p (live
, SSA_NAME_VERSION (op
)))
5942 /* If OP is used in such a way that we can infer a value
5943 range for it, and we don't find a previous assertion for
5944 it, create a new assertion location node for OP. */
5945 if (infer_value_range (stmt
, op
, &comp_code
, &value
))
5947 /* If we are able to infer a nonzero value range for OP,
5948 then walk backwards through the use-def chain to see if OP
5949 was set via a typecast.
5951 If so, then we can also infer a nonzero value range
5952 for the operand of the NOP_EXPR. */
5953 if (comp_code
== NE_EXPR
&& integer_zerop (value
))
5956 gimple def_stmt
= SSA_NAME_DEF_STMT (t
);
5958 while (is_gimple_assign (def_stmt
)
5959 && CONVERT_EXPR_CODE_P
5960 (gimple_assign_rhs_code (def_stmt
))
5962 (gimple_assign_rhs1 (def_stmt
)) == SSA_NAME
5964 (TREE_TYPE (gimple_assign_rhs1 (def_stmt
))))
5966 t
= gimple_assign_rhs1 (def_stmt
);
5967 def_stmt
= SSA_NAME_DEF_STMT (t
);
5969 /* Note we want to register the assert for the
5970 operand of the NOP_EXPR after SI, not after the
5972 if (! has_single_use (t
))
5973 register_new_assert_for (t
, t
, comp_code
, value
,
5978 register_new_assert_for (op
, op
, comp_code
, value
, bb
, NULL
, si
);
5983 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, i
, SSA_OP_USE
)
5984 bitmap_set_bit (live
, SSA_NAME_VERSION (op
));
5985 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, i
, SSA_OP_DEF
)
5986 bitmap_clear_bit (live
, SSA_NAME_VERSION (op
));
5989 /* Traverse all PHI nodes in BB, updating live. */
5990 for (si
= gsi_start_phis (bb
); !gsi_end_p (si
); gsi_next (&si
))
5992 use_operand_p arg_p
;
5994 gimple phi
= gsi_stmt (si
);
5995 tree res
= gimple_phi_result (phi
);
5997 if (virtual_operand_p (res
))
6000 FOR_EACH_PHI_ARG (arg_p
, phi
, i
, SSA_OP_USE
)
6002 tree arg
= USE_FROM_PTR (arg_p
);
6003 if (TREE_CODE (arg
) == SSA_NAME
)
6004 bitmap_set_bit (live
, SSA_NAME_VERSION (arg
));
6007 bitmap_clear_bit (live
, SSA_NAME_VERSION (res
));
6011 /* Do an RPO walk over the function computing SSA name liveness
6012 on-the-fly and deciding on assert expressions to insert. */
6015 find_assert_locations (void)
6017 int *rpo
= XNEWVEC (int, last_basic_block_for_fn (cfun
));
6018 int *bb_rpo
= XNEWVEC (int, last_basic_block_for_fn (cfun
));
6019 int *last_rpo
= XCNEWVEC (int, last_basic_block_for_fn (cfun
));
6022 live
= XCNEWVEC (sbitmap
, last_basic_block_for_fn (cfun
));
6023 rpo_cnt
= pre_and_rev_post_order_compute (NULL
, rpo
, false);
6024 for (i
= 0; i
< rpo_cnt
; ++i
)
6027 /* Pre-seed loop latch liveness from loop header PHI nodes. Due to
6028 the order we compute liveness and insert asserts we otherwise
6029 fail to insert asserts into the loop latch. */
6031 FOR_EACH_LOOP (loop
, 0)
6033 i
= loop
->latch
->index
;
6034 unsigned int j
= single_succ_edge (loop
->latch
)->dest_idx
;
6035 for (gimple_stmt_iterator gsi
= gsi_start_phis (loop
->header
);
6036 !gsi_end_p (gsi
); gsi_next (&gsi
))
6038 gimple phi
= gsi_stmt (gsi
);
6039 if (virtual_operand_p (gimple_phi_result (phi
)))
6041 tree arg
= gimple_phi_arg_def (phi
, j
);
6042 if (TREE_CODE (arg
) == SSA_NAME
)
6044 if (live
[i
] == NULL
)
6046 live
[i
] = sbitmap_alloc (num_ssa_names
);
6047 bitmap_clear (live
[i
]);
6049 bitmap_set_bit (live
[i
], SSA_NAME_VERSION (arg
));
6054 for (i
= rpo_cnt
- 1; i
>= 0; --i
)
6056 basic_block bb
= BASIC_BLOCK_FOR_FN (cfun
, rpo
[i
]);
6062 live
[rpo
[i
]] = sbitmap_alloc (num_ssa_names
);
6063 bitmap_clear (live
[rpo
[i
]]);
6066 /* Process BB and update the live information with uses in
6068 find_assert_locations_1 (bb
, live
[rpo
[i
]]);
6070 /* Merge liveness into the predecessor blocks and free it. */
6071 if (!bitmap_empty_p (live
[rpo
[i
]]))
6074 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
6076 int pred
= e
->src
->index
;
6077 if ((e
->flags
& EDGE_DFS_BACK
) || pred
== ENTRY_BLOCK
)
6082 live
[pred
] = sbitmap_alloc (num_ssa_names
);
6083 bitmap_clear (live
[pred
]);
6085 bitmap_ior (live
[pred
], live
[pred
], live
[rpo
[i
]]);
6087 if (bb_rpo
[pred
] < pred_rpo
)
6088 pred_rpo
= bb_rpo
[pred
];
6091 /* Record the RPO number of the last visited block that needs
6092 live information from this block. */
6093 last_rpo
[rpo
[i
]] = pred_rpo
;
6097 sbitmap_free (live
[rpo
[i
]]);
6098 live
[rpo
[i
]] = NULL
;
6101 /* We can free all successors live bitmaps if all their
6102 predecessors have been visited already. */
6103 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
6104 if (last_rpo
[e
->dest
->index
] == i
6105 && live
[e
->dest
->index
])
6107 sbitmap_free (live
[e
->dest
->index
]);
6108 live
[e
->dest
->index
] = NULL
;
6113 XDELETEVEC (bb_rpo
);
6114 XDELETEVEC (last_rpo
);
6115 for (i
= 0; i
< last_basic_block_for_fn (cfun
); ++i
)
6117 sbitmap_free (live
[i
]);
6121 /* Create an ASSERT_EXPR for NAME and insert it in the location
6122 indicated by LOC. Return true if we made any edge insertions. */
6125 process_assert_insertions_for (tree name
, assert_locus_t loc
)
6127 /* Build the comparison expression NAME_i COMP_CODE VAL. */
6134 /* If we have X <=> X do not insert an assert expr for that. */
6135 if (loc
->expr
== loc
->val
)
6138 cond
= build2 (loc
->comp_code
, boolean_type_node
, loc
->expr
, loc
->val
);
6139 assert_stmt
= build_assert_expr_for (cond
, name
);
6142 /* We have been asked to insert the assertion on an edge. This
6143 is used only by COND_EXPR and SWITCH_EXPR assertions. */
6144 gcc_checking_assert (gimple_code (gsi_stmt (loc
->si
)) == GIMPLE_COND
6145 || (gimple_code (gsi_stmt (loc
->si
))
6148 gsi_insert_on_edge (loc
->e
, assert_stmt
);
6152 /* Otherwise, we can insert right after LOC->SI iff the
6153 statement must not be the last statement in the block. */
6154 stmt
= gsi_stmt (loc
->si
);
6155 if (!stmt_ends_bb_p (stmt
))
6157 gsi_insert_after (&loc
->si
, assert_stmt
, GSI_SAME_STMT
);
6161 /* If STMT must be the last statement in BB, we can only insert new
6162 assertions on the non-abnormal edge out of BB. Note that since
6163 STMT is not control flow, there may only be one non-abnormal edge
6165 FOR_EACH_EDGE (e
, ei
, loc
->bb
->succs
)
6166 if (!(e
->flags
& EDGE_ABNORMAL
))
6168 gsi_insert_on_edge (e
, assert_stmt
);
6176 /* Process all the insertions registered for every name N_i registered
6177 in NEED_ASSERT_FOR. The list of assertions to be inserted are
6178 found in ASSERTS_FOR[i]. */
6181 process_assert_insertions (void)
6185 bool update_edges_p
= false;
6186 int num_asserts
= 0;
6188 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6189 dump_all_asserts (dump_file
);
6191 EXECUTE_IF_SET_IN_BITMAP (need_assert_for
, 0, i
, bi
)
6193 assert_locus_t loc
= asserts_for
[i
];
6198 assert_locus_t next
= loc
->next
;
6199 update_edges_p
|= process_assert_insertions_for (ssa_name (i
), loc
);
6207 gsi_commit_edge_inserts ();
6209 statistics_counter_event (cfun
, "Number of ASSERT_EXPR expressions inserted",
6214 /* Traverse the flowgraph looking for conditional jumps to insert range
6215 expressions. These range expressions are meant to provide information
6216 to optimizations that need to reason in terms of value ranges. They
6217 will not be expanded into RTL. For instance, given:
6226 this pass will transform the code into:
6232 x = ASSERT_EXPR <x, x < y>
6237 y = ASSERT_EXPR <y, x >= y>
6241 The idea is that once copy and constant propagation have run, other
6242 optimizations will be able to determine what ranges of values can 'x'
6243 take in different paths of the code, simply by checking the reaching
6244 definition of 'x'. */
6247 insert_range_assertions (void)
6249 need_assert_for
= BITMAP_ALLOC (NULL
);
6250 asserts_for
= XCNEWVEC (assert_locus_t
, num_ssa_names
);
6252 calculate_dominance_info (CDI_DOMINATORS
);
6254 find_assert_locations ();
6255 if (!bitmap_empty_p (need_assert_for
))
6257 process_assert_insertions ();
6258 update_ssa (TODO_update_ssa_no_phi
);
6261 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6263 fprintf (dump_file
, "\nSSA form after inserting ASSERT_EXPRs\n");
6264 dump_function_to_file (current_function_decl
, dump_file
, dump_flags
);
6268 BITMAP_FREE (need_assert_for
);
6271 /* Checks one ARRAY_REF in REF, located at LOCUS. Ignores flexible arrays
6272 and "struct" hacks. If VRP can determine that the
6273 array subscript is a constant, check if it is outside valid
6274 range. If the array subscript is a RANGE, warn if it is
6275 non-overlapping with valid range.
6276 IGNORE_OFF_BY_ONE is true if the ARRAY_REF is inside a ADDR_EXPR. */
6279 check_array_ref (location_t location
, tree ref
, bool ignore_off_by_one
)
6281 value_range_t
* vr
= NULL
;
6282 tree low_sub
, up_sub
;
6283 tree low_bound
, up_bound
, up_bound_p1
;
6286 if (TREE_NO_WARNING (ref
))
6289 low_sub
= up_sub
= TREE_OPERAND (ref
, 1);
6290 up_bound
= array_ref_up_bound (ref
);
6292 /* Can not check flexible arrays. */
6294 || TREE_CODE (up_bound
) != INTEGER_CST
)
6297 /* Accesses to trailing arrays via pointers may access storage
6298 beyond the types array bounds. */
6299 base
= get_base_address (ref
);
6300 if (base
&& TREE_CODE (base
) == MEM_REF
)
6302 tree cref
, next
= NULL_TREE
;
6304 if (TREE_CODE (TREE_OPERAND (ref
, 0)) != COMPONENT_REF
)
6307 cref
= TREE_OPERAND (ref
, 0);
6308 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (cref
, 0))) == RECORD_TYPE
)
6309 for (next
= DECL_CHAIN (TREE_OPERAND (cref
, 1));
6310 next
&& TREE_CODE (next
) != FIELD_DECL
;
6311 next
= DECL_CHAIN (next
))
6314 /* If this is the last field in a struct type or a field in a
6315 union type do not warn. */
6320 low_bound
= array_ref_low_bound (ref
);
6321 up_bound_p1
= int_const_binop (PLUS_EXPR
, up_bound
,
6322 build_int_cst (TREE_TYPE (up_bound
), 1));
6324 if (TREE_CODE (low_sub
) == SSA_NAME
)
6326 vr
= get_value_range (low_sub
);
6327 if (vr
->type
== VR_RANGE
|| vr
->type
== VR_ANTI_RANGE
)
6329 low_sub
= vr
->type
== VR_RANGE
? vr
->max
: vr
->min
;
6330 up_sub
= vr
->type
== VR_RANGE
? vr
->min
: vr
->max
;
6334 if (vr
&& vr
->type
== VR_ANTI_RANGE
)
6336 if (TREE_CODE (up_sub
) == INTEGER_CST
6337 && tree_int_cst_lt (up_bound
, up_sub
)
6338 && TREE_CODE (low_sub
) == INTEGER_CST
6339 && tree_int_cst_lt (low_sub
, low_bound
))
6341 warning_at (location
, OPT_Warray_bounds
,
6342 "array subscript is outside array bounds");
6343 TREE_NO_WARNING (ref
) = 1;
6346 else if (TREE_CODE (up_sub
) == INTEGER_CST
6347 && (ignore_off_by_one
6348 ? (tree_int_cst_lt (up_bound
, up_sub
)
6349 && !tree_int_cst_equal (up_bound_p1
, up_sub
))
6350 : (tree_int_cst_lt (up_bound
, up_sub
)
6351 || tree_int_cst_equal (up_bound_p1
, up_sub
))))
6353 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6355 fprintf (dump_file
, "Array bound warning for ");
6356 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, ref
);
6357 fprintf (dump_file
, "\n");
6359 warning_at (location
, OPT_Warray_bounds
,
6360 "array subscript is above array bounds");
6361 TREE_NO_WARNING (ref
) = 1;
6363 else if (TREE_CODE (low_sub
) == INTEGER_CST
6364 && tree_int_cst_lt (low_sub
, low_bound
))
6366 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6368 fprintf (dump_file
, "Array bound warning for ");
6369 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, ref
);
6370 fprintf (dump_file
, "\n");
6372 warning_at (location
, OPT_Warray_bounds
,
6373 "array subscript is below array bounds");
6374 TREE_NO_WARNING (ref
) = 1;
6378 /* Searches if the expr T, located at LOCATION computes
6379 address of an ARRAY_REF, and call check_array_ref on it. */
6382 search_for_addr_array (tree t
, location_t location
)
6384 while (TREE_CODE (t
) == SSA_NAME
)
6386 gimple g
= SSA_NAME_DEF_STMT (t
);
6388 if (gimple_code (g
) != GIMPLE_ASSIGN
)
6391 if (get_gimple_rhs_class (gimple_assign_rhs_code (g
))
6392 != GIMPLE_SINGLE_RHS
)
6395 t
= gimple_assign_rhs1 (g
);
6399 /* We are only interested in addresses of ARRAY_REF's. */
6400 if (TREE_CODE (t
) != ADDR_EXPR
)
6403 /* Check each ARRAY_REFs in the reference chain. */
6406 if (TREE_CODE (t
) == ARRAY_REF
)
6407 check_array_ref (location
, t
, true /*ignore_off_by_one*/);
6409 t
= TREE_OPERAND (t
, 0);
6411 while (handled_component_p (t
));
6413 if (TREE_CODE (t
) == MEM_REF
6414 && TREE_CODE (TREE_OPERAND (t
, 0)) == ADDR_EXPR
6415 && !TREE_NO_WARNING (t
))
6417 tree tem
= TREE_OPERAND (TREE_OPERAND (t
, 0), 0);
6418 tree low_bound
, up_bound
, el_sz
;
6420 if (TREE_CODE (TREE_TYPE (tem
)) != ARRAY_TYPE
6421 || TREE_CODE (TREE_TYPE (TREE_TYPE (tem
))) == ARRAY_TYPE
6422 || !TYPE_DOMAIN (TREE_TYPE (tem
)))
6425 low_bound
= TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (tem
)));
6426 up_bound
= TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (tem
)));
6427 el_sz
= TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (tem
)));
6429 || TREE_CODE (low_bound
) != INTEGER_CST
6431 || TREE_CODE (up_bound
) != INTEGER_CST
6433 || TREE_CODE (el_sz
) != INTEGER_CST
)
6436 idx
= mem_ref_offset (t
);
6437 idx
= wi::sdiv_trunc (idx
, wi::to_offset (el_sz
));
6438 if (wi::lts_p (idx
, 0))
6440 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6442 fprintf (dump_file
, "Array bound warning for ");
6443 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, t
);
6444 fprintf (dump_file
, "\n");
6446 warning_at (location
, OPT_Warray_bounds
,
6447 "array subscript is below array bounds");
6448 TREE_NO_WARNING (t
) = 1;
6450 else if (wi::gts_p (idx
, (wi::to_offset (up_bound
)
6451 - wi::to_offset (low_bound
) + 1)))
6453 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6455 fprintf (dump_file
, "Array bound warning for ");
6456 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, t
);
6457 fprintf (dump_file
, "\n");
6459 warning_at (location
, OPT_Warray_bounds
,
6460 "array subscript is above array bounds");
6461 TREE_NO_WARNING (t
) = 1;
6466 /* walk_tree() callback that checks if *TP is
6467 an ARRAY_REF inside an ADDR_EXPR (in which an array
6468 subscript one outside the valid range is allowed). Call
6469 check_array_ref for each ARRAY_REF found. The location is
6473 check_array_bounds (tree
*tp
, int *walk_subtree
, void *data
)
6476 struct walk_stmt_info
*wi
= (struct walk_stmt_info
*) data
;
6477 location_t location
;
6479 if (EXPR_HAS_LOCATION (t
))
6480 location
= EXPR_LOCATION (t
);
6483 location_t
*locp
= (location_t
*) wi
->info
;
6487 *walk_subtree
= TRUE
;
6489 if (TREE_CODE (t
) == ARRAY_REF
)
6490 check_array_ref (location
, t
, false /*ignore_off_by_one*/);
6492 if (TREE_CODE (t
) == MEM_REF
6493 || (TREE_CODE (t
) == RETURN_EXPR
&& TREE_OPERAND (t
, 0)))
6494 search_for_addr_array (TREE_OPERAND (t
, 0), location
);
6496 if (TREE_CODE (t
) == ADDR_EXPR
)
6497 *walk_subtree
= FALSE
;
6502 /* Walk over all statements of all reachable BBs and call check_array_bounds
6506 check_all_array_refs (void)
6509 gimple_stmt_iterator si
;
6511 FOR_EACH_BB_FN (bb
, cfun
)
6515 bool executable
= false;
6517 /* Skip blocks that were found to be unreachable. */
6518 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
6519 executable
|= !!(e
->flags
& EDGE_EXECUTABLE
);
6523 for (si
= gsi_start_bb (bb
); !gsi_end_p (si
); gsi_next (&si
))
6525 gimple stmt
= gsi_stmt (si
);
6526 struct walk_stmt_info wi
;
6527 if (!gimple_has_location (stmt
))
6530 if (is_gimple_call (stmt
))
6533 size_t n
= gimple_call_num_args (stmt
);
6534 for (i
= 0; i
< n
; i
++)
6536 tree arg
= gimple_call_arg (stmt
, i
);
6537 search_for_addr_array (arg
, gimple_location (stmt
));
6542 memset (&wi
, 0, sizeof (wi
));
6543 wi
.info
= CONST_CAST (void *, (const void *)
6544 gimple_location_ptr (stmt
));
6546 walk_gimple_op (gsi_stmt (si
),
6554 /* Return true if all imm uses of VAR are either in STMT, or
6555 feed (optionally through a chain of single imm uses) GIMPLE_COND
6556 in basic block COND_BB. */
6559 all_imm_uses_in_stmt_or_feed_cond (tree var
, gimple stmt
, basic_block cond_bb
)
6561 use_operand_p use_p
, use2_p
;
6562 imm_use_iterator iter
;
6564 FOR_EACH_IMM_USE_FAST (use_p
, iter
, var
)
6565 if (USE_STMT (use_p
) != stmt
)
6567 gimple use_stmt
= USE_STMT (use_p
), use_stmt2
;
6568 if (is_gimple_debug (use_stmt
))
6570 while (is_gimple_assign (use_stmt
)
6571 && TREE_CODE (gimple_assign_lhs (use_stmt
)) == SSA_NAME
6572 && single_imm_use (gimple_assign_lhs (use_stmt
),
6573 &use2_p
, &use_stmt2
))
6574 use_stmt
= use_stmt2
;
6575 if (gimple_code (use_stmt
) != GIMPLE_COND
6576 || gimple_bb (use_stmt
) != cond_bb
)
6589 __builtin_unreachable ();
6591 x_5 = ASSERT_EXPR <x_3, ...>;
6592 If x_3 has no other immediate uses (checked by caller),
6593 var is the x_3 var from ASSERT_EXPR, we can clear low 5 bits
6594 from the non-zero bitmask. */
6597 maybe_set_nonzero_bits (basic_block bb
, tree var
)
6599 edge e
= single_pred_edge (bb
);
6600 basic_block cond_bb
= e
->src
;
6601 gimple stmt
= last_stmt (cond_bb
);
6605 || gimple_code (stmt
) != GIMPLE_COND
6606 || gimple_cond_code (stmt
) != ((e
->flags
& EDGE_TRUE_VALUE
)
6607 ? EQ_EXPR
: NE_EXPR
)
6608 || TREE_CODE (gimple_cond_lhs (stmt
)) != SSA_NAME
6609 || !integer_zerop (gimple_cond_rhs (stmt
)))
6612 stmt
= SSA_NAME_DEF_STMT (gimple_cond_lhs (stmt
));
6613 if (!is_gimple_assign (stmt
)
6614 || gimple_assign_rhs_code (stmt
) != BIT_AND_EXPR
6615 || TREE_CODE (gimple_assign_rhs2 (stmt
)) != INTEGER_CST
)
6617 if (gimple_assign_rhs1 (stmt
) != var
)
6621 if (TREE_CODE (gimple_assign_rhs1 (stmt
)) != SSA_NAME
)
6623 stmt2
= SSA_NAME_DEF_STMT (gimple_assign_rhs1 (stmt
));
6624 if (!gimple_assign_cast_p (stmt2
)
6625 || gimple_assign_rhs1 (stmt2
) != var
6626 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (stmt2
))
6627 || (TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (stmt
)))
6628 != TYPE_PRECISION (TREE_TYPE (var
))))
6631 cst
= gimple_assign_rhs2 (stmt
);
6632 set_nonzero_bits (var
, wi::bit_and_not (get_nonzero_bits (var
), cst
));
6635 /* Convert range assertion expressions into the implied copies and
6636 copy propagate away the copies. Doing the trivial copy propagation
6637 here avoids the need to run the full copy propagation pass after
6640 FIXME, this will eventually lead to copy propagation removing the
6641 names that had useful range information attached to them. For
6642 instance, if we had the assertion N_i = ASSERT_EXPR <N_j, N_j > 3>,
6643 then N_i will have the range [3, +INF].
6645 However, by converting the assertion into the implied copy
6646 operation N_i = N_j, we will then copy-propagate N_j into the uses
6647 of N_i and lose the range information. We may want to hold on to
6648 ASSERT_EXPRs a little while longer as the ranges could be used in
6649 things like jump threading.
6651 The problem with keeping ASSERT_EXPRs around is that passes after
6652 VRP need to handle them appropriately.
6654 Another approach would be to make the range information a first
6655 class property of the SSA_NAME so that it can be queried from
6656 any pass. This is made somewhat more complex by the need for
6657 multiple ranges to be associated with one SSA_NAME. */
6660 remove_range_assertions (void)
6663 gimple_stmt_iterator si
;
6664 /* 1 if looking at ASSERT_EXPRs immediately at the beginning of
6665 a basic block preceeded by GIMPLE_COND branching to it and
6666 __builtin_trap, -1 if not yet checked, 0 otherwise. */
6669 /* Note that the BSI iterator bump happens at the bottom of the
6670 loop and no bump is necessary if we're removing the statement
6671 referenced by the current BSI. */
6672 FOR_EACH_BB_FN (bb
, cfun
)
6673 for (si
= gsi_after_labels (bb
), is_unreachable
= -1; !gsi_end_p (si
);)
6675 gimple stmt
= gsi_stmt (si
);
6678 if (is_gimple_assign (stmt
)
6679 && gimple_assign_rhs_code (stmt
) == ASSERT_EXPR
)
6681 tree lhs
= gimple_assign_lhs (stmt
);
6682 tree rhs
= gimple_assign_rhs1 (stmt
);
6684 tree cond
= fold (ASSERT_EXPR_COND (rhs
));
6685 use_operand_p use_p
;
6686 imm_use_iterator iter
;
6688 gcc_assert (cond
!= boolean_false_node
);
6690 var
= ASSERT_EXPR_VAR (rhs
);
6691 gcc_assert (TREE_CODE (var
) == SSA_NAME
);
6693 if (!POINTER_TYPE_P (TREE_TYPE (lhs
))
6694 && SSA_NAME_RANGE_INFO (lhs
))
6696 if (is_unreachable
== -1)
6699 if (single_pred_p (bb
)
6700 && assert_unreachable_fallthru_edge_p
6701 (single_pred_edge (bb
)))
6705 if (x_7 >= 10 && x_7 < 20)
6706 __builtin_unreachable ();
6707 x_8 = ASSERT_EXPR <x_7, ...>;
6708 if the only uses of x_7 are in the ASSERT_EXPR and
6709 in the condition. In that case, we can copy the
6710 range info from x_8 computed in this pass also
6713 && all_imm_uses_in_stmt_or_feed_cond (var
, stmt
,
6716 set_range_info (var
, SSA_NAME_RANGE_TYPE (lhs
),
6717 SSA_NAME_RANGE_INFO (lhs
)->get_min (),
6718 SSA_NAME_RANGE_INFO (lhs
)->get_max ());
6719 maybe_set_nonzero_bits (bb
, var
);
6723 /* Propagate the RHS into every use of the LHS. */
6724 FOR_EACH_IMM_USE_STMT (use_stmt
, iter
, lhs
)
6725 FOR_EACH_IMM_USE_ON_STMT (use_p
, iter
)
6726 SET_USE (use_p
, var
);
6728 /* And finally, remove the copy, it is not needed. */
6729 gsi_remove (&si
, true);
6730 release_defs (stmt
);
6734 if (!is_gimple_debug (gsi_stmt (si
)))
6742 /* Return true if STMT is interesting for VRP. */
6745 stmt_interesting_for_vrp (gimple stmt
)
6747 if (gimple_code (stmt
) == GIMPLE_PHI
)
6749 tree res
= gimple_phi_result (stmt
);
6750 return (!virtual_operand_p (res
)
6751 && (INTEGRAL_TYPE_P (TREE_TYPE (res
))
6752 || POINTER_TYPE_P (TREE_TYPE (res
))));
6754 else if (is_gimple_assign (stmt
) || is_gimple_call (stmt
))
6756 tree lhs
= gimple_get_lhs (stmt
);
6758 /* In general, assignments with virtual operands are not useful
6759 for deriving ranges, with the obvious exception of calls to
6760 builtin functions. */
6761 if (lhs
&& TREE_CODE (lhs
) == SSA_NAME
6762 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs
))
6763 || POINTER_TYPE_P (TREE_TYPE (lhs
)))
6764 && (is_gimple_call (stmt
)
6765 || !gimple_vuse (stmt
)))
6768 else if (gimple_code (stmt
) == GIMPLE_COND
6769 || gimple_code (stmt
) == GIMPLE_SWITCH
)
6776 /* Initialize local data structures for VRP. */
6779 vrp_initialize (void)
6783 values_propagated
= false;
6784 num_vr_values
= num_ssa_names
;
6785 vr_value
= XCNEWVEC (value_range_t
*, num_vr_values
);
6786 vr_phi_edge_counts
= XCNEWVEC (int, num_ssa_names
);
6788 FOR_EACH_BB_FN (bb
, cfun
)
6790 gimple_stmt_iterator si
;
6792 for (si
= gsi_start_phis (bb
); !gsi_end_p (si
); gsi_next (&si
))
6794 gimple phi
= gsi_stmt (si
);
6795 if (!stmt_interesting_for_vrp (phi
))
6797 tree lhs
= PHI_RESULT (phi
);
6798 set_value_range_to_varying (get_value_range (lhs
));
6799 prop_set_simulate_again (phi
, false);
6802 prop_set_simulate_again (phi
, true);
6805 for (si
= gsi_start_bb (bb
); !gsi_end_p (si
); gsi_next (&si
))
6807 gimple stmt
= gsi_stmt (si
);
6809 /* If the statement is a control insn, then we do not
6810 want to avoid simulating the statement once. Failure
6811 to do so means that those edges will never get added. */
6812 if (stmt_ends_bb_p (stmt
))
6813 prop_set_simulate_again (stmt
, true);
6814 else if (!stmt_interesting_for_vrp (stmt
))
6818 FOR_EACH_SSA_TREE_OPERAND (def
, stmt
, i
, SSA_OP_DEF
)
6819 set_value_range_to_varying (get_value_range (def
));
6820 prop_set_simulate_again (stmt
, false);
6823 prop_set_simulate_again (stmt
, true);
6828 /* Return the singleton value-range for NAME or NAME. */
6831 vrp_valueize (tree name
)
6833 if (TREE_CODE (name
) == SSA_NAME
)
6835 value_range_t
*vr
= get_value_range (name
);
6836 if (vr
->type
== VR_RANGE
6837 && (vr
->min
== vr
->max
6838 || operand_equal_p (vr
->min
, vr
->max
, 0)))
6844 /* Visit assignment STMT. If it produces an interesting range, record
6845 the SSA name in *OUTPUT_P. */
6847 static enum ssa_prop_result
6848 vrp_visit_assignment_or_call (gimple stmt
, tree
*output_p
)
6852 enum gimple_code code
= gimple_code (stmt
);
6853 lhs
= gimple_get_lhs (stmt
);
6855 /* We only keep track of ranges in integral and pointer types. */
6856 if (TREE_CODE (lhs
) == SSA_NAME
6857 && ((INTEGRAL_TYPE_P (TREE_TYPE (lhs
))
6858 /* It is valid to have NULL MIN/MAX values on a type. See
6859 build_range_type. */
6860 && TYPE_MIN_VALUE (TREE_TYPE (lhs
))
6861 && TYPE_MAX_VALUE (TREE_TYPE (lhs
)))
6862 || POINTER_TYPE_P (TREE_TYPE (lhs
))))
6864 value_range_t new_vr
= VR_INITIALIZER
;
6866 /* Try folding the statement to a constant first. */
6867 tree tem
= gimple_fold_stmt_to_constant (stmt
, vrp_valueize
);
6869 set_value_range_to_value (&new_vr
, tem
, NULL
);
6870 /* Then dispatch to value-range extracting functions. */
6871 else if (code
== GIMPLE_CALL
)
6872 extract_range_basic (&new_vr
, stmt
);
6874 extract_range_from_assignment (&new_vr
, stmt
);
6876 if (update_value_range (lhs
, &new_vr
))
6880 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6882 fprintf (dump_file
, "Found new range for ");
6883 print_generic_expr (dump_file
, lhs
, 0);
6884 fprintf (dump_file
, ": ");
6885 dump_value_range (dump_file
, &new_vr
);
6886 fprintf (dump_file
, "\n");
6889 if (new_vr
.type
== VR_VARYING
)
6890 return SSA_PROP_VARYING
;
6892 return SSA_PROP_INTERESTING
;
6895 return SSA_PROP_NOT_INTERESTING
;
6898 /* Every other statement produces no useful ranges. */
6899 FOR_EACH_SSA_TREE_OPERAND (def
, stmt
, iter
, SSA_OP_DEF
)
6900 set_value_range_to_varying (get_value_range (def
));
6902 return SSA_PROP_VARYING
;
6905 /* Helper that gets the value range of the SSA_NAME with version I
6906 or a symbolic range containing the SSA_NAME only if the value range
6907 is varying or undefined. */
6909 static inline value_range_t
6910 get_vr_for_comparison (int i
)
6912 value_range_t vr
= *get_value_range (ssa_name (i
));
6914 /* If name N_i does not have a valid range, use N_i as its own
6915 range. This allows us to compare against names that may
6916 have N_i in their ranges. */
6917 if (vr
.type
== VR_VARYING
|| vr
.type
== VR_UNDEFINED
)
6920 vr
.min
= ssa_name (i
);
6921 vr
.max
= ssa_name (i
);
6927 /* Compare all the value ranges for names equivalent to VAR with VAL
6928 using comparison code COMP. Return the same value returned by
6929 compare_range_with_value, including the setting of
6930 *STRICT_OVERFLOW_P. */
6933 compare_name_with_value (enum tree_code comp
, tree var
, tree val
,
6934 bool *strict_overflow_p
)
6940 int used_strict_overflow
;
6942 value_range_t equiv_vr
;
6944 /* Get the set of equivalences for VAR. */
6945 e
= get_value_range (var
)->equiv
;
6947 /* Start at -1. Set it to 0 if we do a comparison without relying
6948 on overflow, or 1 if all comparisons rely on overflow. */
6949 used_strict_overflow
= -1;
6951 /* Compare vars' value range with val. */
6952 equiv_vr
= get_vr_for_comparison (SSA_NAME_VERSION (var
));
6954 retval
= compare_range_with_value (comp
, &equiv_vr
, val
, &sop
);
6956 used_strict_overflow
= sop
? 1 : 0;
6958 /* If the equiv set is empty we have done all work we need to do. */
6962 && used_strict_overflow
> 0)
6963 *strict_overflow_p
= true;
6967 EXECUTE_IF_SET_IN_BITMAP (e
, 0, i
, bi
)
6969 equiv_vr
= get_vr_for_comparison (i
);
6971 t
= compare_range_with_value (comp
, &equiv_vr
, val
, &sop
);
6974 /* If we get different answers from different members
6975 of the equivalence set this check must be in a dead
6976 code region. Folding it to a trap representation
6977 would be correct here. For now just return don't-know. */
6987 used_strict_overflow
= 0;
6988 else if (used_strict_overflow
< 0)
6989 used_strict_overflow
= 1;
6994 && used_strict_overflow
> 0)
6995 *strict_overflow_p
= true;
7001 /* Given a comparison code COMP and names N1 and N2, compare all the
7002 ranges equivalent to N1 against all the ranges equivalent to N2
7003 to determine the value of N1 COMP N2. Return the same value
7004 returned by compare_ranges. Set *STRICT_OVERFLOW_P to indicate
7005 whether we relied on an overflow infinity in the comparison. */
7009 compare_names (enum tree_code comp
, tree n1
, tree n2
,
7010 bool *strict_overflow_p
)
7014 bitmap_iterator bi1
, bi2
;
7016 int used_strict_overflow
;
7017 static bitmap_obstack
*s_obstack
= NULL
;
7018 static bitmap s_e1
= NULL
, s_e2
= NULL
;
7020 /* Compare the ranges of every name equivalent to N1 against the
7021 ranges of every name equivalent to N2. */
7022 e1
= get_value_range (n1
)->equiv
;
7023 e2
= get_value_range (n2
)->equiv
;
7025 /* Use the fake bitmaps if e1 or e2 are not available. */
7026 if (s_obstack
== NULL
)
7028 s_obstack
= XNEW (bitmap_obstack
);
7029 bitmap_obstack_initialize (s_obstack
);
7030 s_e1
= BITMAP_ALLOC (s_obstack
);
7031 s_e2
= BITMAP_ALLOC (s_obstack
);
7038 /* Add N1 and N2 to their own set of equivalences to avoid
7039 duplicating the body of the loop just to check N1 and N2
7041 bitmap_set_bit (e1
, SSA_NAME_VERSION (n1
));
7042 bitmap_set_bit (e2
, SSA_NAME_VERSION (n2
));
7044 /* If the equivalence sets have a common intersection, then the two
7045 names can be compared without checking their ranges. */
7046 if (bitmap_intersect_p (e1
, e2
))
7048 bitmap_clear_bit (e1
, SSA_NAME_VERSION (n1
));
7049 bitmap_clear_bit (e2
, SSA_NAME_VERSION (n2
));
7051 return (comp
== EQ_EXPR
|| comp
== GE_EXPR
|| comp
== LE_EXPR
)
7053 : boolean_false_node
;
7056 /* Start at -1. Set it to 0 if we do a comparison without relying
7057 on overflow, or 1 if all comparisons rely on overflow. */
7058 used_strict_overflow
= -1;
7060 /* Otherwise, compare all the equivalent ranges. First, add N1 and
7061 N2 to their own set of equivalences to avoid duplicating the body
7062 of the loop just to check N1 and N2 ranges. */
7063 EXECUTE_IF_SET_IN_BITMAP (e1
, 0, i1
, bi1
)
7065 value_range_t vr1
= get_vr_for_comparison (i1
);
7067 t
= retval
= NULL_TREE
;
7068 EXECUTE_IF_SET_IN_BITMAP (e2
, 0, i2
, bi2
)
7072 value_range_t vr2
= get_vr_for_comparison (i2
);
7074 t
= compare_ranges (comp
, &vr1
, &vr2
, &sop
);
7077 /* If we get different answers from different members
7078 of the equivalence set this check must be in a dead
7079 code region. Folding it to a trap representation
7080 would be correct here. For now just return don't-know. */
7084 bitmap_clear_bit (e1
, SSA_NAME_VERSION (n1
));
7085 bitmap_clear_bit (e2
, SSA_NAME_VERSION (n2
));
7091 used_strict_overflow
= 0;
7092 else if (used_strict_overflow
< 0)
7093 used_strict_overflow
= 1;
7099 bitmap_clear_bit (e1
, SSA_NAME_VERSION (n1
));
7100 bitmap_clear_bit (e2
, SSA_NAME_VERSION (n2
));
7101 if (used_strict_overflow
> 0)
7102 *strict_overflow_p
= true;
7107 /* None of the equivalent ranges are useful in computing this
7109 bitmap_clear_bit (e1
, SSA_NAME_VERSION (n1
));
7110 bitmap_clear_bit (e2
, SSA_NAME_VERSION (n2
));
7114 /* Helper function for vrp_evaluate_conditional_warnv. */
7117 vrp_evaluate_conditional_warnv_with_ops_using_ranges (enum tree_code code
,
7119 bool * strict_overflow_p
)
7121 value_range_t
*vr0
, *vr1
;
7123 vr0
= (TREE_CODE (op0
) == SSA_NAME
) ? get_value_range (op0
) : NULL
;
7124 vr1
= (TREE_CODE (op1
) == SSA_NAME
) ? get_value_range (op1
) : NULL
;
7126 tree res
= NULL_TREE
;
7128 res
= compare_ranges (code
, vr0
, vr1
, strict_overflow_p
);
7130 res
= compare_range_with_value (code
, vr0
, op1
, strict_overflow_p
);
7132 res
= (compare_range_with_value
7133 (swap_tree_comparison (code
), vr1
, op0
, strict_overflow_p
));
7137 /* Helper function for vrp_evaluate_conditional_warnv. */
7140 vrp_evaluate_conditional_warnv_with_ops (enum tree_code code
, tree op0
,
7141 tree op1
, bool use_equiv_p
,
7142 bool *strict_overflow_p
, bool *only_ranges
)
7146 *only_ranges
= true;
7148 /* We only deal with integral and pointer types. */
7149 if (!INTEGRAL_TYPE_P (TREE_TYPE (op0
))
7150 && !POINTER_TYPE_P (TREE_TYPE (op0
)))
7156 && (ret
= vrp_evaluate_conditional_warnv_with_ops_using_ranges
7157 (code
, op0
, op1
, strict_overflow_p
)))
7159 *only_ranges
= false;
7160 if (TREE_CODE (op0
) == SSA_NAME
&& TREE_CODE (op1
) == SSA_NAME
)
7161 return compare_names (code
, op0
, op1
, strict_overflow_p
);
7162 else if (TREE_CODE (op0
) == SSA_NAME
)
7163 return compare_name_with_value (code
, op0
, op1
, strict_overflow_p
);
7164 else if (TREE_CODE (op1
) == SSA_NAME
)
7165 return (compare_name_with_value
7166 (swap_tree_comparison (code
), op1
, op0
, strict_overflow_p
));
7169 return vrp_evaluate_conditional_warnv_with_ops_using_ranges (code
, op0
, op1
,
7174 /* Given (CODE OP0 OP1) within STMT, try to simplify it based on value range
7175 information. Return NULL if the conditional can not be evaluated.
7176 The ranges of all the names equivalent with the operands in COND
7177 will be used when trying to compute the value. If the result is
7178 based on undefined signed overflow, issue a warning if
7182 vrp_evaluate_conditional (enum tree_code code
, tree op0
, tree op1
, gimple stmt
)
7188 /* Some passes and foldings leak constants with overflow flag set
7189 into the IL. Avoid doing wrong things with these and bail out. */
7190 if ((TREE_CODE (op0
) == INTEGER_CST
7191 && TREE_OVERFLOW (op0
))
7192 || (TREE_CODE (op1
) == INTEGER_CST
7193 && TREE_OVERFLOW (op1
)))
7197 ret
= vrp_evaluate_conditional_warnv_with_ops (code
, op0
, op1
, true, &sop
,
7202 enum warn_strict_overflow_code wc
;
7203 const char* warnmsg
;
7205 if (is_gimple_min_invariant (ret
))
7207 wc
= WARN_STRICT_OVERFLOW_CONDITIONAL
;
7208 warnmsg
= G_("assuming signed overflow does not occur when "
7209 "simplifying conditional to constant");
7213 wc
= WARN_STRICT_OVERFLOW_COMPARISON
;
7214 warnmsg
= G_("assuming signed overflow does not occur when "
7215 "simplifying conditional");
7218 if (issue_strict_overflow_warning (wc
))
7220 location_t location
;
7222 if (!gimple_has_location (stmt
))
7223 location
= input_location
;
7225 location
= gimple_location (stmt
);
7226 warning_at (location
, OPT_Wstrict_overflow
, "%s", warnmsg
);
7230 if (warn_type_limits
7231 && ret
&& only_ranges
7232 && TREE_CODE_CLASS (code
) == tcc_comparison
7233 && TREE_CODE (op0
) == SSA_NAME
)
7235 /* If the comparison is being folded and the operand on the LHS
7236 is being compared against a constant value that is outside of
7237 the natural range of OP0's type, then the predicate will
7238 always fold regardless of the value of OP0. If -Wtype-limits
7239 was specified, emit a warning. */
7240 tree type
= TREE_TYPE (op0
);
7241 value_range_t
*vr0
= get_value_range (op0
);
7243 if (vr0
->type
!= VR_VARYING
7244 && INTEGRAL_TYPE_P (type
)
7245 && vrp_val_is_min (vr0
->min
)
7246 && vrp_val_is_max (vr0
->max
)
7247 && is_gimple_min_invariant (op1
))
7249 location_t location
;
7251 if (!gimple_has_location (stmt
))
7252 location
= input_location
;
7254 location
= gimple_location (stmt
);
7256 warning_at (location
, OPT_Wtype_limits
,
7258 ? G_("comparison always false "
7259 "due to limited range of data type")
7260 : G_("comparison always true "
7261 "due to limited range of data type"));
7269 /* Visit conditional statement STMT. If we can determine which edge
7270 will be taken out of STMT's basic block, record it in
7271 *TAKEN_EDGE_P and return SSA_PROP_INTERESTING. Otherwise, return
7272 SSA_PROP_VARYING. */
7274 static enum ssa_prop_result
7275 vrp_visit_cond_stmt (gimple stmt
, edge
*taken_edge_p
)
7280 *taken_edge_p
= NULL
;
7282 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7287 fprintf (dump_file
, "\nVisiting conditional with predicate: ");
7288 print_gimple_stmt (dump_file
, stmt
, 0, 0);
7289 fprintf (dump_file
, "\nWith known ranges\n");
7291 FOR_EACH_SSA_TREE_OPERAND (use
, stmt
, i
, SSA_OP_USE
)
7293 fprintf (dump_file
, "\t");
7294 print_generic_expr (dump_file
, use
, 0);
7295 fprintf (dump_file
, ": ");
7296 dump_value_range (dump_file
, vr_value
[SSA_NAME_VERSION (use
)]);
7299 fprintf (dump_file
, "\n");
7302 /* Compute the value of the predicate COND by checking the known
7303 ranges of each of its operands.
7305 Note that we cannot evaluate all the equivalent ranges here
7306 because those ranges may not yet be final and with the current
7307 propagation strategy, we cannot determine when the value ranges
7308 of the names in the equivalence set have changed.
7310 For instance, given the following code fragment
7314 i_14 = ASSERT_EXPR <i_5, i_5 != 0>
7318 Assume that on the first visit to i_14, i_5 has the temporary
7319 range [8, 8] because the second argument to the PHI function is
7320 not yet executable. We derive the range ~[0, 0] for i_14 and the
7321 equivalence set { i_5 }. So, when we visit 'if (i_14 == 1)' for
7322 the first time, since i_14 is equivalent to the range [8, 8], we
7323 determine that the predicate is always false.
7325 On the next round of propagation, i_13 is determined to be
7326 VARYING, which causes i_5 to drop down to VARYING. So, another
7327 visit to i_14 is scheduled. In this second visit, we compute the
7328 exact same range and equivalence set for i_14, namely ~[0, 0] and
7329 { i_5 }. But we did not have the previous range for i_5
7330 registered, so vrp_visit_assignment thinks that the range for
7331 i_14 has not changed. Therefore, the predicate 'if (i_14 == 1)'
7332 is not visited again, which stops propagation from visiting
7333 statements in the THEN clause of that if().
7335 To properly fix this we would need to keep the previous range
7336 value for the names in the equivalence set. This way we would've
7337 discovered that from one visit to the other i_5 changed from
7338 range [8, 8] to VR_VARYING.
7340 However, fixing this apparent limitation may not be worth the
7341 additional checking. Testing on several code bases (GCC, DLV,
7342 MICO, TRAMP3D and SPEC2000) showed that doing this results in
7343 4 more predicates folded in SPEC. */
7346 val
= vrp_evaluate_conditional_warnv_with_ops (gimple_cond_code (stmt
),
7347 gimple_cond_lhs (stmt
),
7348 gimple_cond_rhs (stmt
),
7353 *taken_edge_p
= find_taken_edge (gimple_bb (stmt
), val
);
7356 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7358 "\nIgnoring predicate evaluation because "
7359 "it assumes that signed overflow is undefined");
7364 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7366 fprintf (dump_file
, "\nPredicate evaluates to: ");
7367 if (val
== NULL_TREE
)
7368 fprintf (dump_file
, "DON'T KNOW\n");
7370 print_generic_stmt (dump_file
, val
, 0);
7373 return (*taken_edge_p
) ? SSA_PROP_INTERESTING
: SSA_PROP_VARYING
;
7376 /* Searches the case label vector VEC for the index *IDX of the CASE_LABEL
7377 that includes the value VAL. The search is restricted to the range
7378 [START_IDX, n - 1] where n is the size of VEC.
7380 If there is a CASE_LABEL for VAL, its index is placed in IDX and true is
7383 If there is no CASE_LABEL for VAL and there is one that is larger than VAL,
7384 it is placed in IDX and false is returned.
7386 If VAL is larger than any CASE_LABEL, n is placed on IDX and false is
7390 find_case_label_index (gimple stmt
, size_t start_idx
, tree val
, size_t *idx
)
7392 size_t n
= gimple_switch_num_labels (stmt
);
7395 /* Find case label for minimum of the value range or the next one.
7396 At each iteration we are searching in [low, high - 1]. */
7398 for (low
= start_idx
, high
= n
; high
!= low
; )
7402 /* Note that i != high, so we never ask for n. */
7403 size_t i
= (high
+ low
) / 2;
7404 t
= gimple_switch_label (stmt
, i
);
7406 /* Cache the result of comparing CASE_LOW and val. */
7407 cmp
= tree_int_cst_compare (CASE_LOW (t
), val
);
7411 /* Ranges cannot be empty. */
7420 if (CASE_HIGH (t
) != NULL
7421 && tree_int_cst_compare (CASE_HIGH (t
), val
) >= 0)
7433 /* Searches the case label vector VEC for the range of CASE_LABELs that is used
7434 for values between MIN and MAX. The first index is placed in MIN_IDX. The
7435 last index is placed in MAX_IDX. If the range of CASE_LABELs is empty
7436 then MAX_IDX < MIN_IDX.
7437 Returns true if the default label is not needed. */
7440 find_case_label_range (gimple stmt
, tree min
, tree max
, size_t *min_idx
,
7444 bool min_take_default
= !find_case_label_index (stmt
, 1, min
, &i
);
7445 bool max_take_default
= !find_case_label_index (stmt
, i
, max
, &j
);
7449 && max_take_default
)
7451 /* Only the default case label reached.
7452 Return an empty range. */
7459 bool take_default
= min_take_default
|| max_take_default
;
7463 if (max_take_default
)
7466 /* If the case label range is continuous, we do not need
7467 the default case label. Verify that. */
7468 high
= CASE_LOW (gimple_switch_label (stmt
, i
));
7469 if (CASE_HIGH (gimple_switch_label (stmt
, i
)))
7470 high
= CASE_HIGH (gimple_switch_label (stmt
, i
));
7471 for (k
= i
+ 1; k
<= j
; ++k
)
7473 low
= CASE_LOW (gimple_switch_label (stmt
, k
));
7474 if (!integer_onep (int_const_binop (MINUS_EXPR
, low
, high
)))
7476 take_default
= true;
7480 if (CASE_HIGH (gimple_switch_label (stmt
, k
)))
7481 high
= CASE_HIGH (gimple_switch_label (stmt
, k
));
7486 return !take_default
;
7490 /* Searches the case label vector VEC for the ranges of CASE_LABELs that are
7491 used in range VR. The indices are placed in MIN_IDX1, MAX_IDX, MIN_IDX2 and
7492 MAX_IDX2. If the ranges of CASE_LABELs are empty then MAX_IDX1 < MIN_IDX1.
7493 Returns true if the default label is not needed. */
7496 find_case_label_ranges (gimple stmt
, value_range_t
*vr
, size_t *min_idx1
,
7497 size_t *max_idx1
, size_t *min_idx2
,
7501 unsigned int n
= gimple_switch_num_labels (stmt
);
7503 tree case_low
, case_high
;
7504 tree min
= vr
->min
, max
= vr
->max
;
7506 gcc_checking_assert (vr
->type
== VR_RANGE
|| vr
->type
== VR_ANTI_RANGE
);
7508 take_default
= !find_case_label_range (stmt
, min
, max
, &i
, &j
);
7510 /* Set second range to emtpy. */
7514 if (vr
->type
== VR_RANGE
)
7518 return !take_default
;
7521 /* Set first range to all case labels. */
7528 /* Make sure all the values of case labels [i , j] are contained in
7529 range [MIN, MAX]. */
7530 case_low
= CASE_LOW (gimple_switch_label (stmt
, i
));
7531 case_high
= CASE_HIGH (gimple_switch_label (stmt
, j
));
7532 if (tree_int_cst_compare (case_low
, min
) < 0)
7534 if (case_high
!= NULL_TREE
7535 && tree_int_cst_compare (max
, case_high
) < 0)
7541 /* If the range spans case labels [i, j], the corresponding anti-range spans
7542 the labels [1, i - 1] and [j + 1, n - 1]. */
7568 /* Visit switch statement STMT. If we can determine which edge
7569 will be taken out of STMT's basic block, record it in
7570 *TAKEN_EDGE_P and return SSA_PROP_INTERESTING. Otherwise, return
7571 SSA_PROP_VARYING. */
7573 static enum ssa_prop_result
7574 vrp_visit_switch_stmt (gimple stmt
, edge
*taken_edge_p
)
7578 size_t i
= 0, j
= 0, k
, l
;
7581 *taken_edge_p
= NULL
;
7582 op
= gimple_switch_index (stmt
);
7583 if (TREE_CODE (op
) != SSA_NAME
)
7584 return SSA_PROP_VARYING
;
7586 vr
= get_value_range (op
);
7587 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7589 fprintf (dump_file
, "\nVisiting switch expression with operand ");
7590 print_generic_expr (dump_file
, op
, 0);
7591 fprintf (dump_file
, " with known range ");
7592 dump_value_range (dump_file
, vr
);
7593 fprintf (dump_file
, "\n");
7596 if ((vr
->type
!= VR_RANGE
7597 && vr
->type
!= VR_ANTI_RANGE
)
7598 || symbolic_range_p (vr
))
7599 return SSA_PROP_VARYING
;
7601 /* Find the single edge that is taken from the switch expression. */
7602 take_default
= !find_case_label_ranges (stmt
, vr
, &i
, &j
, &k
, &l
);
7604 /* Check if the range spans no CASE_LABEL. If so, we only reach the default
7608 gcc_assert (take_default
);
7609 val
= gimple_switch_default_label (stmt
);
7613 /* Check if labels with index i to j and maybe the default label
7614 are all reaching the same label. */
7616 val
= gimple_switch_label (stmt
, i
);
7618 && CASE_LABEL (gimple_switch_default_label (stmt
))
7619 != CASE_LABEL (val
))
7621 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7622 fprintf (dump_file
, " not a single destination for this "
7624 return SSA_PROP_VARYING
;
7626 for (++i
; i
<= j
; ++i
)
7628 if (CASE_LABEL (gimple_switch_label (stmt
, i
)) != CASE_LABEL (val
))
7630 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7631 fprintf (dump_file
, " not a single destination for this "
7633 return SSA_PROP_VARYING
;
7638 if (CASE_LABEL (gimple_switch_label (stmt
, k
)) != CASE_LABEL (val
))
7640 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7641 fprintf (dump_file
, " not a single destination for this "
7643 return SSA_PROP_VARYING
;
7648 *taken_edge_p
= find_edge (gimple_bb (stmt
),
7649 label_to_block (CASE_LABEL (val
)));
7651 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7653 fprintf (dump_file
, " will take edge to ");
7654 print_generic_stmt (dump_file
, CASE_LABEL (val
), 0);
7657 return SSA_PROP_INTERESTING
;
7661 /* Evaluate statement STMT. If the statement produces a useful range,
7662 return SSA_PROP_INTERESTING and record the SSA name with the
7663 interesting range into *OUTPUT_P.
7665 If STMT is a conditional branch and we can determine its truth
7666 value, the taken edge is recorded in *TAKEN_EDGE_P.
7668 If STMT produces a varying value, return SSA_PROP_VARYING. */
7670 static enum ssa_prop_result
7671 vrp_visit_stmt (gimple stmt
, edge
*taken_edge_p
, tree
*output_p
)
7676 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7678 fprintf (dump_file
, "\nVisiting statement:\n");
7679 print_gimple_stmt (dump_file
, stmt
, 0, dump_flags
);
7682 if (!stmt_interesting_for_vrp (stmt
))
7683 gcc_assert (stmt_ends_bb_p (stmt
));
7684 else if (is_gimple_assign (stmt
) || is_gimple_call (stmt
))
7685 return vrp_visit_assignment_or_call (stmt
, output_p
);
7686 else if (gimple_code (stmt
) == GIMPLE_COND
)
7687 return vrp_visit_cond_stmt (stmt
, taken_edge_p
);
7688 else if (gimple_code (stmt
) == GIMPLE_SWITCH
)
7689 return vrp_visit_switch_stmt (stmt
, taken_edge_p
);
7691 /* All other statements produce nothing of interest for VRP, so mark
7692 their outputs varying and prevent further simulation. */
7693 FOR_EACH_SSA_TREE_OPERAND (def
, stmt
, iter
, SSA_OP_DEF
)
7694 set_value_range_to_varying (get_value_range (def
));
7696 return SSA_PROP_VARYING
;
7699 /* Union the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
7700 { VR1TYPE, VR0MIN, VR0MAX } and store the result
7701 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
7702 possible such range. The resulting range is not canonicalized. */
7705 union_ranges (enum value_range_type
*vr0type
,
7706 tree
*vr0min
, tree
*vr0max
,
7707 enum value_range_type vr1type
,
7708 tree vr1min
, tree vr1max
)
7710 bool mineq
= operand_equal_p (*vr0min
, vr1min
, 0);
7711 bool maxeq
= operand_equal_p (*vr0max
, vr1max
, 0);
7713 /* [] is vr0, () is vr1 in the following classification comments. */
7717 if (*vr0type
== vr1type
)
7718 /* Nothing to do for equal ranges. */
7720 else if ((*vr0type
== VR_RANGE
7721 && vr1type
== VR_ANTI_RANGE
)
7722 || (*vr0type
== VR_ANTI_RANGE
7723 && vr1type
== VR_RANGE
))
7725 /* For anti-range with range union the result is varying. */
7731 else if (operand_less_p (*vr0max
, vr1min
) == 1
7732 || operand_less_p (vr1max
, *vr0min
) == 1)
7734 /* [ ] ( ) or ( ) [ ]
7735 If the ranges have an empty intersection, result of the union
7736 operation is the anti-range or if both are anti-ranges
7738 if (*vr0type
== VR_ANTI_RANGE
7739 && vr1type
== VR_ANTI_RANGE
)
7741 else if (*vr0type
== VR_ANTI_RANGE
7742 && vr1type
== VR_RANGE
)
7744 else if (*vr0type
== VR_RANGE
7745 && vr1type
== VR_ANTI_RANGE
)
7751 else if (*vr0type
== VR_RANGE
7752 && vr1type
== VR_RANGE
)
7754 /* The result is the convex hull of both ranges. */
7755 if (operand_less_p (*vr0max
, vr1min
) == 1)
7757 /* If the result can be an anti-range, create one. */
7758 if (TREE_CODE (*vr0max
) == INTEGER_CST
7759 && TREE_CODE (vr1min
) == INTEGER_CST
7760 && vrp_val_is_min (*vr0min
)
7761 && vrp_val_is_max (vr1max
))
7763 tree min
= int_const_binop (PLUS_EXPR
,
7765 build_int_cst (TREE_TYPE (*vr0max
), 1));
7766 tree max
= int_const_binop (MINUS_EXPR
,
7768 build_int_cst (TREE_TYPE (vr1min
), 1));
7769 if (!operand_less_p (max
, min
))
7771 *vr0type
= VR_ANTI_RANGE
;
7783 /* If the result can be an anti-range, create one. */
7784 if (TREE_CODE (vr1max
) == INTEGER_CST
7785 && TREE_CODE (*vr0min
) == INTEGER_CST
7786 && vrp_val_is_min (vr1min
)
7787 && vrp_val_is_max (*vr0max
))
7789 tree min
= int_const_binop (PLUS_EXPR
,
7791 build_int_cst (TREE_TYPE (vr1max
), 1));
7792 tree max
= int_const_binop (MINUS_EXPR
,
7794 build_int_cst (TREE_TYPE (*vr0min
), 1));
7795 if (!operand_less_p (max
, min
))
7797 *vr0type
= VR_ANTI_RANGE
;
7811 else if ((maxeq
|| operand_less_p (vr1max
, *vr0max
) == 1)
7812 && (mineq
|| operand_less_p (*vr0min
, vr1min
) == 1))
7814 /* [ ( ) ] or [( ) ] or [ ( )] */
7815 if (*vr0type
== VR_RANGE
7816 && vr1type
== VR_RANGE
)
7818 else if (*vr0type
== VR_ANTI_RANGE
7819 && vr1type
== VR_ANTI_RANGE
)
7825 else if (*vr0type
== VR_ANTI_RANGE
7826 && vr1type
== VR_RANGE
)
7828 /* Arbitrarily choose the right or left gap. */
7829 if (!mineq
&& TREE_CODE (vr1min
) == INTEGER_CST
)
7830 *vr0max
= int_const_binop (MINUS_EXPR
, vr1min
,
7831 build_int_cst (TREE_TYPE (vr1min
), 1));
7832 else if (!maxeq
&& TREE_CODE (vr1max
) == INTEGER_CST
)
7833 *vr0min
= int_const_binop (PLUS_EXPR
, vr1max
,
7834 build_int_cst (TREE_TYPE (vr1max
), 1));
7838 else if (*vr0type
== VR_RANGE
7839 && vr1type
== VR_ANTI_RANGE
)
7840 /* The result covers everything. */
7845 else if ((maxeq
|| operand_less_p (*vr0max
, vr1max
) == 1)
7846 && (mineq
|| operand_less_p (vr1min
, *vr0min
) == 1))
7848 /* ( [ ] ) or ([ ] ) or ( [ ]) */
7849 if (*vr0type
== VR_RANGE
7850 && vr1type
== VR_RANGE
)
7856 else if (*vr0type
== VR_ANTI_RANGE
7857 && vr1type
== VR_ANTI_RANGE
)
7859 else if (*vr0type
== VR_RANGE
7860 && vr1type
== VR_ANTI_RANGE
)
7862 *vr0type
= VR_ANTI_RANGE
;
7863 if (!mineq
&& TREE_CODE (*vr0min
) == INTEGER_CST
)
7865 *vr0max
= int_const_binop (MINUS_EXPR
, *vr0min
,
7866 build_int_cst (TREE_TYPE (*vr0min
), 1));
7869 else if (!maxeq
&& TREE_CODE (*vr0max
) == INTEGER_CST
)
7871 *vr0min
= int_const_binop (PLUS_EXPR
, *vr0max
,
7872 build_int_cst (TREE_TYPE (*vr0max
), 1));
7878 else if (*vr0type
== VR_ANTI_RANGE
7879 && vr1type
== VR_RANGE
)
7880 /* The result covers everything. */
7885 else if ((operand_less_p (vr1min
, *vr0max
) == 1
7886 || operand_equal_p (vr1min
, *vr0max
, 0))
7887 && operand_less_p (*vr0min
, vr1min
) == 1
7888 && operand_less_p (*vr0max
, vr1max
) == 1)
7890 /* [ ( ] ) or [ ]( ) */
7891 if (*vr0type
== VR_RANGE
7892 && vr1type
== VR_RANGE
)
7894 else if (*vr0type
== VR_ANTI_RANGE
7895 && vr1type
== VR_ANTI_RANGE
)
7897 else if (*vr0type
== VR_ANTI_RANGE
7898 && vr1type
== VR_RANGE
)
7900 if (TREE_CODE (vr1min
) == INTEGER_CST
)
7901 *vr0max
= int_const_binop (MINUS_EXPR
, vr1min
,
7902 build_int_cst (TREE_TYPE (vr1min
), 1));
7906 else if (*vr0type
== VR_RANGE
7907 && vr1type
== VR_ANTI_RANGE
)
7909 if (TREE_CODE (*vr0max
) == INTEGER_CST
)
7912 *vr0min
= int_const_binop (PLUS_EXPR
, *vr0max
,
7913 build_int_cst (TREE_TYPE (*vr0max
), 1));
7922 else if ((operand_less_p (*vr0min
, vr1max
) == 1
7923 || operand_equal_p (*vr0min
, vr1max
, 0))
7924 && operand_less_p (vr1min
, *vr0min
) == 1
7925 && operand_less_p (vr1max
, *vr0max
) == 1)
7927 /* ( [ ) ] or ( )[ ] */
7928 if (*vr0type
== VR_RANGE
7929 && vr1type
== VR_RANGE
)
7931 else if (*vr0type
== VR_ANTI_RANGE
7932 && vr1type
== VR_ANTI_RANGE
)
7934 else if (*vr0type
== VR_ANTI_RANGE
7935 && vr1type
== VR_RANGE
)
7937 if (TREE_CODE (vr1max
) == INTEGER_CST
)
7938 *vr0min
= int_const_binop (PLUS_EXPR
, vr1max
,
7939 build_int_cst (TREE_TYPE (vr1max
), 1));
7943 else if (*vr0type
== VR_RANGE
7944 && vr1type
== VR_ANTI_RANGE
)
7946 if (TREE_CODE (*vr0min
) == INTEGER_CST
)
7950 *vr0max
= int_const_binop (MINUS_EXPR
, *vr0min
,
7951 build_int_cst (TREE_TYPE (*vr0min
), 1));
7965 *vr0type
= VR_VARYING
;
7966 *vr0min
= NULL_TREE
;
7967 *vr0max
= NULL_TREE
;
7970 /* Intersect the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
7971 { VR1TYPE, VR0MIN, VR0MAX } and store the result
7972 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
7973 possible such range. The resulting range is not canonicalized. */
7976 intersect_ranges (enum value_range_type
*vr0type
,
7977 tree
*vr0min
, tree
*vr0max
,
7978 enum value_range_type vr1type
,
7979 tree vr1min
, tree vr1max
)
7981 bool mineq
= operand_equal_p (*vr0min
, vr1min
, 0);
7982 bool maxeq
= operand_equal_p (*vr0max
, vr1max
, 0);
7984 /* [] is vr0, () is vr1 in the following classification comments. */
7988 if (*vr0type
== vr1type
)
7989 /* Nothing to do for equal ranges. */
7991 else if ((*vr0type
== VR_RANGE
7992 && vr1type
== VR_ANTI_RANGE
)
7993 || (*vr0type
== VR_ANTI_RANGE
7994 && vr1type
== VR_RANGE
))
7996 /* For anti-range with range intersection the result is empty. */
7997 *vr0type
= VR_UNDEFINED
;
7998 *vr0min
= NULL_TREE
;
7999 *vr0max
= NULL_TREE
;
8004 else if (operand_less_p (*vr0max
, vr1min
) == 1
8005 || operand_less_p (vr1max
, *vr0min
) == 1)
8007 /* [ ] ( ) or ( ) [ ]
8008 If the ranges have an empty intersection, the result of the
8009 intersect operation is the range for intersecting an
8010 anti-range with a range or empty when intersecting two ranges. */
8011 if (*vr0type
== VR_RANGE
8012 && vr1type
== VR_ANTI_RANGE
)
8014 else if (*vr0type
== VR_ANTI_RANGE
8015 && vr1type
== VR_RANGE
)
8021 else if (*vr0type
== VR_RANGE
8022 && vr1type
== VR_RANGE
)
8024 *vr0type
= VR_UNDEFINED
;
8025 *vr0min
= NULL_TREE
;
8026 *vr0max
= NULL_TREE
;
8028 else if (*vr0type
== VR_ANTI_RANGE
8029 && vr1type
== VR_ANTI_RANGE
)
8031 /* If the anti-ranges are adjacent to each other merge them. */
8032 if (TREE_CODE (*vr0max
) == INTEGER_CST
8033 && TREE_CODE (vr1min
) == INTEGER_CST
8034 && operand_less_p (*vr0max
, vr1min
) == 1
8035 && integer_onep (int_const_binop (MINUS_EXPR
,
8038 else if (TREE_CODE (vr1max
) == INTEGER_CST
8039 && TREE_CODE (*vr0min
) == INTEGER_CST
8040 && operand_less_p (vr1max
, *vr0min
) == 1
8041 && integer_onep (int_const_binop (MINUS_EXPR
,
8044 /* Else arbitrarily take VR0. */
8047 else if ((maxeq
|| operand_less_p (vr1max
, *vr0max
) == 1)
8048 && (mineq
|| operand_less_p (*vr0min
, vr1min
) == 1))
8050 /* [ ( ) ] or [( ) ] or [ ( )] */
8051 if (*vr0type
== VR_RANGE
8052 && vr1type
== VR_RANGE
)
8054 /* If both are ranges the result is the inner one. */
8059 else if (*vr0type
== VR_RANGE
8060 && vr1type
== VR_ANTI_RANGE
)
8062 /* Choose the right gap if the left one is empty. */
8065 if (TREE_CODE (vr1max
) == INTEGER_CST
)
8066 *vr0min
= int_const_binop (PLUS_EXPR
, vr1max
,
8067 build_int_cst (TREE_TYPE (vr1max
), 1));
8071 /* Choose the left gap if the right one is empty. */
8074 if (TREE_CODE (vr1min
) == INTEGER_CST
)
8075 *vr0max
= int_const_binop (MINUS_EXPR
, vr1min
,
8076 build_int_cst (TREE_TYPE (vr1min
), 1));
8080 /* Choose the anti-range if the range is effectively varying. */
8081 else if (vrp_val_is_min (*vr0min
)
8082 && vrp_val_is_max (*vr0max
))
8088 /* Else choose the range. */
8090 else if (*vr0type
== VR_ANTI_RANGE
8091 && vr1type
== VR_ANTI_RANGE
)
8092 /* If both are anti-ranges the result is the outer one. */
8094 else if (*vr0type
== VR_ANTI_RANGE
8095 && vr1type
== VR_RANGE
)
8097 /* The intersection is empty. */
8098 *vr0type
= VR_UNDEFINED
;
8099 *vr0min
= NULL_TREE
;
8100 *vr0max
= NULL_TREE
;
8105 else if ((maxeq
|| operand_less_p (*vr0max
, vr1max
) == 1)
8106 && (mineq
|| operand_less_p (vr1min
, *vr0min
) == 1))
8108 /* ( [ ] ) or ([ ] ) or ( [ ]) */
8109 if (*vr0type
== VR_RANGE
8110 && vr1type
== VR_RANGE
)
8111 /* Choose the inner range. */
8113 else if (*vr0type
== VR_ANTI_RANGE
8114 && vr1type
== VR_RANGE
)
8116 /* Choose the right gap if the left is empty. */
8119 *vr0type
= VR_RANGE
;
8120 if (TREE_CODE (*vr0max
) == INTEGER_CST
)
8121 *vr0min
= int_const_binop (PLUS_EXPR
, *vr0max
,
8122 build_int_cst (TREE_TYPE (*vr0max
), 1));
8127 /* Choose the left gap if the right is empty. */
8130 *vr0type
= VR_RANGE
;
8131 if (TREE_CODE (*vr0min
) == INTEGER_CST
)
8132 *vr0max
= int_const_binop (MINUS_EXPR
, *vr0min
,
8133 build_int_cst (TREE_TYPE (*vr0min
), 1));
8138 /* Choose the anti-range if the range is effectively varying. */
8139 else if (vrp_val_is_min (vr1min
)
8140 && vrp_val_is_max (vr1max
))
8142 /* Else choose the range. */
8150 else if (*vr0type
== VR_ANTI_RANGE
8151 && vr1type
== VR_ANTI_RANGE
)
8153 /* If both are anti-ranges the result is the outer one. */
8158 else if (vr1type
== VR_ANTI_RANGE
8159 && *vr0type
== VR_RANGE
)
8161 /* The intersection is empty. */
8162 *vr0type
= VR_UNDEFINED
;
8163 *vr0min
= NULL_TREE
;
8164 *vr0max
= NULL_TREE
;
8169 else if ((operand_less_p (vr1min
, *vr0max
) == 1
8170 || operand_equal_p (vr1min
, *vr0max
, 0))
8171 && operand_less_p (*vr0min
, vr1min
) == 1)
8173 /* [ ( ] ) or [ ]( ) */
8174 if (*vr0type
== VR_ANTI_RANGE
8175 && vr1type
== VR_ANTI_RANGE
)
8177 else if (*vr0type
== VR_RANGE
8178 && vr1type
== VR_RANGE
)
8180 else if (*vr0type
== VR_RANGE
8181 && vr1type
== VR_ANTI_RANGE
)
8183 if (TREE_CODE (vr1min
) == INTEGER_CST
)
8184 *vr0max
= int_const_binop (MINUS_EXPR
, vr1min
,
8185 build_int_cst (TREE_TYPE (vr1min
), 1));
8189 else if (*vr0type
== VR_ANTI_RANGE
8190 && vr1type
== VR_RANGE
)
8192 *vr0type
= VR_RANGE
;
8193 if (TREE_CODE (*vr0max
) == INTEGER_CST
)
8194 *vr0min
= int_const_binop (PLUS_EXPR
, *vr0max
,
8195 build_int_cst (TREE_TYPE (*vr0max
), 1));
8203 else if ((operand_less_p (*vr0min
, vr1max
) == 1
8204 || operand_equal_p (*vr0min
, vr1max
, 0))
8205 && operand_less_p (vr1min
, *vr0min
) == 1)
8207 /* ( [ ) ] or ( )[ ] */
8208 if (*vr0type
== VR_ANTI_RANGE
8209 && vr1type
== VR_ANTI_RANGE
)
8211 else if (*vr0type
== VR_RANGE
8212 && vr1type
== VR_RANGE
)
8214 else if (*vr0type
== VR_RANGE
8215 && vr1type
== VR_ANTI_RANGE
)
8217 if (TREE_CODE (vr1max
) == INTEGER_CST
)
8218 *vr0min
= int_const_binop (PLUS_EXPR
, vr1max
,
8219 build_int_cst (TREE_TYPE (vr1max
), 1));
8223 else if (*vr0type
== VR_ANTI_RANGE
8224 && vr1type
== VR_RANGE
)
8226 *vr0type
= VR_RANGE
;
8227 if (TREE_CODE (*vr0min
) == INTEGER_CST
)
8228 *vr0max
= int_const_binop (MINUS_EXPR
, *vr0min
,
8229 build_int_cst (TREE_TYPE (*vr0min
), 1));
8238 /* As a fallback simply use { *VRTYPE, *VR0MIN, *VR0MAX } as
8239 result for the intersection. That's always a conservative
8240 correct estimate. */
8246 /* Intersect the two value-ranges *VR0 and *VR1 and store the result
8247 in *VR0. This may not be the smallest possible such range. */
8250 vrp_intersect_ranges_1 (value_range_t
*vr0
, value_range_t
*vr1
)
8252 value_range_t saved
;
8254 /* If either range is VR_VARYING the other one wins. */
8255 if (vr1
->type
== VR_VARYING
)
8257 if (vr0
->type
== VR_VARYING
)
8259 copy_value_range (vr0
, vr1
);
8263 /* When either range is VR_UNDEFINED the resulting range is
8264 VR_UNDEFINED, too. */
8265 if (vr0
->type
== VR_UNDEFINED
)
8267 if (vr1
->type
== VR_UNDEFINED
)
8269 set_value_range_to_undefined (vr0
);
8273 /* Save the original vr0 so we can return it as conservative intersection
8274 result when our worker turns things to varying. */
8276 intersect_ranges (&vr0
->type
, &vr0
->min
, &vr0
->max
,
8277 vr1
->type
, vr1
->min
, vr1
->max
);
8278 /* Make sure to canonicalize the result though as the inversion of a
8279 VR_RANGE can still be a VR_RANGE. */
8280 set_and_canonicalize_value_range (vr0
, vr0
->type
,
8281 vr0
->min
, vr0
->max
, vr0
->equiv
);
8282 /* If that failed, use the saved original VR0. */
8283 if (vr0
->type
== VR_VARYING
)
8288 /* If the result is VR_UNDEFINED there is no need to mess with
8289 the equivalencies. */
8290 if (vr0
->type
== VR_UNDEFINED
)
8293 /* The resulting set of equivalences for range intersection is the union of
8295 if (vr0
->equiv
&& vr1
->equiv
&& vr0
->equiv
!= vr1
->equiv
)
8296 bitmap_ior_into (vr0
->equiv
, vr1
->equiv
);
8297 else if (vr1
->equiv
&& !vr0
->equiv
)
8298 bitmap_copy (vr0
->equiv
, vr1
->equiv
);
8302 vrp_intersect_ranges (value_range_t
*vr0
, value_range_t
*vr1
)
8304 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8306 fprintf (dump_file
, "Intersecting\n ");
8307 dump_value_range (dump_file
, vr0
);
8308 fprintf (dump_file
, "\nand\n ");
8309 dump_value_range (dump_file
, vr1
);
8310 fprintf (dump_file
, "\n");
8312 vrp_intersect_ranges_1 (vr0
, vr1
);
8313 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8315 fprintf (dump_file
, "to\n ");
8316 dump_value_range (dump_file
, vr0
);
8317 fprintf (dump_file
, "\n");
8321 /* Meet operation for value ranges. Given two value ranges VR0 and
8322 VR1, store in VR0 a range that contains both VR0 and VR1. This
8323 may not be the smallest possible such range. */
8326 vrp_meet_1 (value_range_t
*vr0
, value_range_t
*vr1
)
8328 value_range_t saved
;
8330 if (vr0
->type
== VR_UNDEFINED
)
8332 set_value_range (vr0
, vr1
->type
, vr1
->min
, vr1
->max
, vr1
->equiv
);
8336 if (vr1
->type
== VR_UNDEFINED
)
8338 /* VR0 already has the resulting range. */
8342 if (vr0
->type
== VR_VARYING
)
8344 /* Nothing to do. VR0 already has the resulting range. */
8348 if (vr1
->type
== VR_VARYING
)
8350 set_value_range_to_varying (vr0
);
8355 union_ranges (&vr0
->type
, &vr0
->min
, &vr0
->max
,
8356 vr1
->type
, vr1
->min
, vr1
->max
);
8357 if (vr0
->type
== VR_VARYING
)
8359 /* Failed to find an efficient meet. Before giving up and setting
8360 the result to VARYING, see if we can at least derive a useful
8361 anti-range. FIXME, all this nonsense about distinguishing
8362 anti-ranges from ranges is necessary because of the odd
8363 semantics of range_includes_zero_p and friends. */
8364 if (((saved
.type
== VR_RANGE
8365 && range_includes_zero_p (saved
.min
, saved
.max
) == 0)
8366 || (saved
.type
== VR_ANTI_RANGE
8367 && range_includes_zero_p (saved
.min
, saved
.max
) == 1))
8368 && ((vr1
->type
== VR_RANGE
8369 && range_includes_zero_p (vr1
->min
, vr1
->max
) == 0)
8370 || (vr1
->type
== VR_ANTI_RANGE
8371 && range_includes_zero_p (vr1
->min
, vr1
->max
) == 1)))
8373 set_value_range_to_nonnull (vr0
, TREE_TYPE (saved
.min
));
8375 /* Since this meet operation did not result from the meeting of
8376 two equivalent names, VR0 cannot have any equivalences. */
8378 bitmap_clear (vr0
->equiv
);
8382 set_value_range_to_varying (vr0
);
8385 set_and_canonicalize_value_range (vr0
, vr0
->type
, vr0
->min
, vr0
->max
,
8387 if (vr0
->type
== VR_VARYING
)
8390 /* The resulting set of equivalences is always the intersection of
8392 if (vr0
->equiv
&& vr1
->equiv
&& vr0
->equiv
!= vr1
->equiv
)
8393 bitmap_and_into (vr0
->equiv
, vr1
->equiv
);
8394 else if (vr0
->equiv
&& !vr1
->equiv
)
8395 bitmap_clear (vr0
->equiv
);
8399 vrp_meet (value_range_t
*vr0
, value_range_t
*vr1
)
8401 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8403 fprintf (dump_file
, "Meeting\n ");
8404 dump_value_range (dump_file
, vr0
);
8405 fprintf (dump_file
, "\nand\n ");
8406 dump_value_range (dump_file
, vr1
);
8407 fprintf (dump_file
, "\n");
8409 vrp_meet_1 (vr0
, vr1
);
8410 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8412 fprintf (dump_file
, "to\n ");
8413 dump_value_range (dump_file
, vr0
);
8414 fprintf (dump_file
, "\n");
8419 /* Visit all arguments for PHI node PHI that flow through executable
8420 edges. If a valid value range can be derived from all the incoming
8421 value ranges, set a new range for the LHS of PHI. */
8423 static enum ssa_prop_result
8424 vrp_visit_phi_node (gimple phi
)
8427 tree lhs
= PHI_RESULT (phi
);
8428 value_range_t
*lhs_vr
= get_value_range (lhs
);
8429 value_range_t vr_result
= VR_INITIALIZER
;
8431 int edges
, old_edges
;
8434 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8436 fprintf (dump_file
, "\nVisiting PHI node: ");
8437 print_gimple_stmt (dump_file
, phi
, 0, dump_flags
);
8441 for (i
= 0; i
< gimple_phi_num_args (phi
); i
++)
8443 edge e
= gimple_phi_arg_edge (phi
, i
);
8445 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8448 " Argument #%d (%d -> %d %sexecutable)\n",
8449 (int) i
, e
->src
->index
, e
->dest
->index
,
8450 (e
->flags
& EDGE_EXECUTABLE
) ? "" : "not ");
8453 if (e
->flags
& EDGE_EXECUTABLE
)
8455 tree arg
= PHI_ARG_DEF (phi
, i
);
8456 value_range_t vr_arg
;
8460 if (TREE_CODE (arg
) == SSA_NAME
)
8462 vr_arg
= *(get_value_range (arg
));
8463 /* Do not allow equivalences or symbolic ranges to leak in from
8464 backedges. That creates invalid equivalencies.
8465 See PR53465 and PR54767. */
8466 if (e
->flags
& EDGE_DFS_BACK
)
8468 if (vr_arg
.type
== VR_RANGE
8469 || vr_arg
.type
== VR_ANTI_RANGE
)
8471 vr_arg
.equiv
= NULL
;
8472 if (symbolic_range_p (&vr_arg
))
8474 vr_arg
.type
= VR_VARYING
;
8475 vr_arg
.min
= NULL_TREE
;
8476 vr_arg
.max
= NULL_TREE
;
8482 /* If the non-backedge arguments range is VR_VARYING then
8483 we can still try recording a simple equivalence. */
8484 if (vr_arg
.type
== VR_VARYING
)
8486 vr_arg
.type
= VR_RANGE
;
8489 vr_arg
.equiv
= NULL
;
8495 if (TREE_OVERFLOW_P (arg
))
8496 arg
= drop_tree_overflow (arg
);
8498 vr_arg
.type
= VR_RANGE
;
8501 vr_arg
.equiv
= NULL
;
8504 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8506 fprintf (dump_file
, "\t");
8507 print_generic_expr (dump_file
, arg
, dump_flags
);
8508 fprintf (dump_file
, ": ");
8509 dump_value_range (dump_file
, &vr_arg
);
8510 fprintf (dump_file
, "\n");
8514 copy_value_range (&vr_result
, &vr_arg
);
8516 vrp_meet (&vr_result
, &vr_arg
);
8519 if (vr_result
.type
== VR_VARYING
)
8524 if (vr_result
.type
== VR_VARYING
)
8526 else if (vr_result
.type
== VR_UNDEFINED
)
8529 old_edges
= vr_phi_edge_counts
[SSA_NAME_VERSION (lhs
)];
8530 vr_phi_edge_counts
[SSA_NAME_VERSION (lhs
)] = edges
;
8532 /* To prevent infinite iterations in the algorithm, derive ranges
8533 when the new value is slightly bigger or smaller than the
8534 previous one. We don't do this if we have seen a new executable
8535 edge; this helps us avoid an overflow infinity for conditionals
8536 which are not in a loop. If the old value-range was VR_UNDEFINED
8537 use the updated range and iterate one more time. */
8539 && gimple_phi_num_args (phi
) > 1
8540 && edges
== old_edges
8541 && lhs_vr
->type
!= VR_UNDEFINED
)
8543 /* Compare old and new ranges, fall back to varying if the
8544 values are not comparable. */
8545 int cmp_min
= compare_values (lhs_vr
->min
, vr_result
.min
);
8548 int cmp_max
= compare_values (lhs_vr
->max
, vr_result
.max
);
8552 /* For non VR_RANGE or for pointers fall back to varying if
8553 the range changed. */
8554 if ((lhs_vr
->type
!= VR_RANGE
|| vr_result
.type
!= VR_RANGE
8555 || POINTER_TYPE_P (TREE_TYPE (lhs
)))
8556 && (cmp_min
!= 0 || cmp_max
!= 0))
8559 /* If the new minimum is larger than than the previous one
8560 retain the old value. If the new minimum value is smaller
8561 than the previous one and not -INF go all the way to -INF + 1.
8562 In the first case, to avoid infinite bouncing between different
8563 minimums, and in the other case to avoid iterating millions of
8564 times to reach -INF. Going to -INF + 1 also lets the following
8565 iteration compute whether there will be any overflow, at the
8566 expense of one additional iteration. */
8568 vr_result
.min
= lhs_vr
->min
;
8569 else if (cmp_min
> 0
8570 && !vrp_val_is_min (vr_result
.min
))
8572 = int_const_binop (PLUS_EXPR
,
8573 vrp_val_min (TREE_TYPE (vr_result
.min
)),
8574 build_int_cst (TREE_TYPE (vr_result
.min
), 1));
8576 /* Similarly for the maximum value. */
8578 vr_result
.max
= lhs_vr
->max
;
8579 else if (cmp_max
< 0
8580 && !vrp_val_is_max (vr_result
.max
))
8582 = int_const_binop (MINUS_EXPR
,
8583 vrp_val_max (TREE_TYPE (vr_result
.min
)),
8584 build_int_cst (TREE_TYPE (vr_result
.min
), 1));
8586 /* If we dropped either bound to +-INF then if this is a loop
8587 PHI node SCEV may known more about its value-range. */
8588 if ((cmp_min
> 0 || cmp_min
< 0
8589 || cmp_max
< 0 || cmp_max
> 0)
8590 && (l
= loop_containing_stmt (phi
))
8591 && l
->header
== gimple_bb (phi
))
8592 adjust_range_with_scev (&vr_result
, l
, phi
, lhs
);
8594 /* If we will end up with a (-INF, +INF) range, set it to
8595 VARYING. Same if the previous max value was invalid for
8596 the type and we end up with vr_result.min > vr_result.max. */
8597 if ((vrp_val_is_max (vr_result
.max
)
8598 && vrp_val_is_min (vr_result
.min
))
8599 || compare_values (vr_result
.min
,
8604 /* If the new range is different than the previous value, keep
8607 if (update_value_range (lhs
, &vr_result
))
8609 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8611 fprintf (dump_file
, "Found new range for ");
8612 print_generic_expr (dump_file
, lhs
, 0);
8613 fprintf (dump_file
, ": ");
8614 dump_value_range (dump_file
, &vr_result
);
8615 fprintf (dump_file
, "\n");
8618 return SSA_PROP_INTERESTING
;
8621 /* Nothing changed, don't add outgoing edges. */
8622 return SSA_PROP_NOT_INTERESTING
;
8624 /* No match found. Set the LHS to VARYING. */
8626 set_value_range_to_varying (lhs_vr
);
8627 return SSA_PROP_VARYING
;
8630 /* Simplify boolean operations if the source is known
8631 to be already a boolean. */
8633 simplify_truth_ops_using_ranges (gimple_stmt_iterator
*gsi
, gimple stmt
)
8635 enum tree_code rhs_code
= gimple_assign_rhs_code (stmt
);
8637 bool need_conversion
;
8639 /* We handle only !=/== case here. */
8640 gcc_assert (rhs_code
== EQ_EXPR
|| rhs_code
== NE_EXPR
);
8642 op0
= gimple_assign_rhs1 (stmt
);
8643 if (!op_with_boolean_value_range_p (op0
))
8646 op1
= gimple_assign_rhs2 (stmt
);
8647 if (!op_with_boolean_value_range_p (op1
))
8650 /* Reduce number of cases to handle to NE_EXPR. As there is no
8651 BIT_XNOR_EXPR we cannot replace A == B with a single statement. */
8652 if (rhs_code
== EQ_EXPR
)
8654 if (TREE_CODE (op1
) == INTEGER_CST
)
8655 op1
= int_const_binop (BIT_XOR_EXPR
, op1
,
8656 build_int_cst (TREE_TYPE (op1
), 1));
8661 lhs
= gimple_assign_lhs (stmt
);
8663 = !useless_type_conversion_p (TREE_TYPE (lhs
), TREE_TYPE (op0
));
8665 /* Make sure to not sign-extend a 1-bit 1 when converting the result. */
8667 && !TYPE_UNSIGNED (TREE_TYPE (op0
))
8668 && TYPE_PRECISION (TREE_TYPE (op0
)) == 1
8669 && TYPE_PRECISION (TREE_TYPE (lhs
)) > 1)
8672 /* For A != 0 we can substitute A itself. */
8673 if (integer_zerop (op1
))
8674 gimple_assign_set_rhs_with_ops (gsi
,
8676 ? NOP_EXPR
: TREE_CODE (op0
),
8678 /* For A != B we substitute A ^ B. Either with conversion. */
8679 else if (need_conversion
)
8681 tree tem
= make_ssa_name (TREE_TYPE (op0
), NULL
);
8682 gimple newop
= gimple_build_assign_with_ops (BIT_XOR_EXPR
, tem
, op0
, op1
);
8683 gsi_insert_before (gsi
, newop
, GSI_SAME_STMT
);
8684 gimple_assign_set_rhs_with_ops (gsi
, NOP_EXPR
, tem
, NULL_TREE
);
8688 gimple_assign_set_rhs_with_ops (gsi
, BIT_XOR_EXPR
, op0
, op1
);
8689 update_stmt (gsi_stmt (*gsi
));
8694 /* Simplify a division or modulo operator to a right shift or
8695 bitwise and if the first operand is unsigned or is greater
8696 than zero and the second operand is an exact power of two. */
8699 simplify_div_or_mod_using_ranges (gimple stmt
)
8701 enum tree_code rhs_code
= gimple_assign_rhs_code (stmt
);
8703 tree op0
= gimple_assign_rhs1 (stmt
);
8704 tree op1
= gimple_assign_rhs2 (stmt
);
8705 value_range_t
*vr
= get_value_range (gimple_assign_rhs1 (stmt
));
8707 if (TYPE_UNSIGNED (TREE_TYPE (op0
)))
8709 val
= integer_one_node
;
8715 val
= compare_range_with_value (GE_EXPR
, vr
, integer_zero_node
, &sop
);
8719 && integer_onep (val
)
8720 && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC
))
8722 location_t location
;
8724 if (!gimple_has_location (stmt
))
8725 location
= input_location
;
8727 location
= gimple_location (stmt
);
8728 warning_at (location
, OPT_Wstrict_overflow
,
8729 "assuming signed overflow does not occur when "
8730 "simplifying %</%> or %<%%%> to %<>>%> or %<&%>");
8734 if (val
&& integer_onep (val
))
8738 if (rhs_code
== TRUNC_DIV_EXPR
)
8740 t
= build_int_cst (integer_type_node
, tree_log2 (op1
));
8741 gimple_assign_set_rhs_code (stmt
, RSHIFT_EXPR
);
8742 gimple_assign_set_rhs1 (stmt
, op0
);
8743 gimple_assign_set_rhs2 (stmt
, t
);
8747 t
= build_int_cst (TREE_TYPE (op1
), 1);
8748 t
= int_const_binop (MINUS_EXPR
, op1
, t
);
8749 t
= fold_convert (TREE_TYPE (op0
), t
);
8751 gimple_assign_set_rhs_code (stmt
, BIT_AND_EXPR
);
8752 gimple_assign_set_rhs1 (stmt
, op0
);
8753 gimple_assign_set_rhs2 (stmt
, t
);
8763 /* If the operand to an ABS_EXPR is >= 0, then eliminate the
8764 ABS_EXPR. If the operand is <= 0, then simplify the
8765 ABS_EXPR into a NEGATE_EXPR. */
8768 simplify_abs_using_ranges (gimple stmt
)
8771 tree op
= gimple_assign_rhs1 (stmt
);
8772 tree type
= TREE_TYPE (op
);
8773 value_range_t
*vr
= get_value_range (op
);
8775 if (TYPE_UNSIGNED (type
))
8777 val
= integer_zero_node
;
8783 val
= compare_range_with_value (LE_EXPR
, vr
, integer_zero_node
, &sop
);
8787 val
= compare_range_with_value (GE_EXPR
, vr
, integer_zero_node
,
8792 if (integer_zerop (val
))
8793 val
= integer_one_node
;
8794 else if (integer_onep (val
))
8795 val
= integer_zero_node
;
8800 && (integer_onep (val
) || integer_zerop (val
)))
8802 if (sop
&& issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC
))
8804 location_t location
;
8806 if (!gimple_has_location (stmt
))
8807 location
= input_location
;
8809 location
= gimple_location (stmt
);
8810 warning_at (location
, OPT_Wstrict_overflow
,
8811 "assuming signed overflow does not occur when "
8812 "simplifying %<abs (X)%> to %<X%> or %<-X%>");
8815 gimple_assign_set_rhs1 (stmt
, op
);
8816 if (integer_onep (val
))
8817 gimple_assign_set_rhs_code (stmt
, NEGATE_EXPR
);
8819 gimple_assign_set_rhs_code (stmt
, SSA_NAME
);
8828 /* Optimize away redundant BIT_AND_EXPR and BIT_IOR_EXPR.
8829 If all the bits that are being cleared by & are already
8830 known to be zero from VR, or all the bits that are being
8831 set by | are already known to be one from VR, the bit
8832 operation is redundant. */
8835 simplify_bit_ops_using_ranges (gimple_stmt_iterator
*gsi
, gimple stmt
)
8837 tree op0
= gimple_assign_rhs1 (stmt
);
8838 tree op1
= gimple_assign_rhs2 (stmt
);
8839 tree op
= NULL_TREE
;
8840 value_range_t vr0
= VR_INITIALIZER
;
8841 value_range_t vr1
= VR_INITIALIZER
;
8842 wide_int may_be_nonzero0
, may_be_nonzero1
;
8843 wide_int must_be_nonzero0
, must_be_nonzero1
;
8846 if (TREE_CODE (op0
) == SSA_NAME
)
8847 vr0
= *(get_value_range (op0
));
8848 else if (is_gimple_min_invariant (op0
))
8849 set_value_range_to_value (&vr0
, op0
, NULL
);
8853 if (TREE_CODE (op1
) == SSA_NAME
)
8854 vr1
= *(get_value_range (op1
));
8855 else if (is_gimple_min_invariant (op1
))
8856 set_value_range_to_value (&vr1
, op1
, NULL
);
8860 if (!zero_nonzero_bits_from_vr (TREE_TYPE (op0
), &vr0
, &may_be_nonzero0
,
8863 if (!zero_nonzero_bits_from_vr (TREE_TYPE (op1
), &vr1
, &may_be_nonzero1
,
8867 switch (gimple_assign_rhs_code (stmt
))
8870 mask
= may_be_nonzero0
.and_not (must_be_nonzero1
);
8876 mask
= may_be_nonzero1
.and_not (must_be_nonzero0
);
8884 mask
= may_be_nonzero0
.and_not (must_be_nonzero1
);
8890 mask
= may_be_nonzero1
.and_not (must_be_nonzero0
);
8901 if (op
== NULL_TREE
)
8904 gimple_assign_set_rhs_with_ops (gsi
, TREE_CODE (op
), op
, NULL
);
8905 update_stmt (gsi_stmt (*gsi
));
8909 /* We are comparing trees OP0 and OP1 using COND_CODE. OP0 has
8910 a known value range VR.
8912 If there is one and only one value which will satisfy the
8913 conditional, then return that value. Else return NULL. */
8916 test_for_singularity (enum tree_code cond_code
, tree op0
,
8917 tree op1
, value_range_t
*vr
)
8922 /* Extract minimum/maximum values which satisfy the
8923 the conditional as it was written. */
8924 if (cond_code
== LE_EXPR
|| cond_code
== LT_EXPR
)
8926 /* This should not be negative infinity; there is no overflow
8928 min
= TYPE_MIN_VALUE (TREE_TYPE (op0
));
8931 if (cond_code
== LT_EXPR
&& !is_overflow_infinity (max
))
8933 tree one
= build_int_cst (TREE_TYPE (op0
), 1);
8934 max
= fold_build2 (MINUS_EXPR
, TREE_TYPE (op0
), max
, one
);
8936 TREE_NO_WARNING (max
) = 1;
8939 else if (cond_code
== GE_EXPR
|| cond_code
== GT_EXPR
)
8941 /* This should not be positive infinity; there is no overflow
8943 max
= TYPE_MAX_VALUE (TREE_TYPE (op0
));
8946 if (cond_code
== GT_EXPR
&& !is_overflow_infinity (min
))
8948 tree one
= build_int_cst (TREE_TYPE (op0
), 1);
8949 min
= fold_build2 (PLUS_EXPR
, TREE_TYPE (op0
), min
, one
);
8951 TREE_NO_WARNING (min
) = 1;
8955 /* Now refine the minimum and maximum values using any
8956 value range information we have for op0. */
8959 if (compare_values (vr
->min
, min
) == 1)
8961 if (compare_values (vr
->max
, max
) == -1)
8964 /* If the new min/max values have converged to a single value,
8965 then there is only one value which can satisfy the condition,
8966 return that value. */
8967 if (operand_equal_p (min
, max
, 0) && is_gimple_min_invariant (min
))
8973 /* Return whether the value range *VR fits in an integer type specified
8974 by PRECISION and UNSIGNED_P. */
8977 range_fits_type_p (value_range_t
*vr
, unsigned dest_precision
, signop dest_sgn
)
8980 unsigned src_precision
;
8984 /* We can only handle integral and pointer types. */
8985 src_type
= TREE_TYPE (vr
->min
);
8986 if (!INTEGRAL_TYPE_P (src_type
)
8987 && !POINTER_TYPE_P (src_type
))
8990 /* An extension is fine unless VR is SIGNED and dest_sgn is UNSIGNED,
8991 and so is an identity transform. */
8992 src_precision
= TYPE_PRECISION (TREE_TYPE (vr
->min
));
8993 src_sgn
= TYPE_SIGN (src_type
);
8994 if ((src_precision
< dest_precision
8995 && !(dest_sgn
== UNSIGNED
&& src_sgn
== SIGNED
))
8996 || (src_precision
== dest_precision
&& src_sgn
== dest_sgn
))
8999 /* Now we can only handle ranges with constant bounds. */
9000 if (vr
->type
!= VR_RANGE
9001 || TREE_CODE (vr
->min
) != INTEGER_CST
9002 || TREE_CODE (vr
->max
) != INTEGER_CST
)
9005 /* For sign changes, the MSB of the wide_int has to be clear.
9006 An unsigned value with its MSB set cannot be represented by
9007 a signed wide_int, while a negative value cannot be represented
9008 by an unsigned wide_int. */
9009 if (src_sgn
!= dest_sgn
9010 && (wi::lts_p (vr
->min
, 0) || wi::lts_p (vr
->max
, 0)))
9013 /* Then we can perform the conversion on both ends and compare
9014 the result for equality. */
9015 tem
= wi::ext (wi::to_widest (vr
->min
), dest_precision
, dest_sgn
);
9016 if (tem
!= wi::to_widest (vr
->min
))
9018 tem
= wi::ext (wi::to_widest (vr
->max
), dest_precision
, dest_sgn
);
9019 if (tem
!= wi::to_widest (vr
->max
))
9025 /* Simplify a conditional using a relational operator to an equality
9026 test if the range information indicates only one value can satisfy
9027 the original conditional. */
9030 simplify_cond_using_ranges (gimple stmt
)
9032 tree op0
= gimple_cond_lhs (stmt
);
9033 tree op1
= gimple_cond_rhs (stmt
);
9034 enum tree_code cond_code
= gimple_cond_code (stmt
);
9036 if (cond_code
!= NE_EXPR
9037 && cond_code
!= EQ_EXPR
9038 && TREE_CODE (op0
) == SSA_NAME
9039 && INTEGRAL_TYPE_P (TREE_TYPE (op0
))
9040 && is_gimple_min_invariant (op1
))
9042 value_range_t
*vr
= get_value_range (op0
);
9044 /* If we have range information for OP0, then we might be
9045 able to simplify this conditional. */
9046 if (vr
->type
== VR_RANGE
)
9048 tree new_tree
= test_for_singularity (cond_code
, op0
, op1
, vr
);
9054 fprintf (dump_file
, "Simplified relational ");
9055 print_gimple_stmt (dump_file
, stmt
, 0, 0);
9056 fprintf (dump_file
, " into ");
9059 gimple_cond_set_code (stmt
, EQ_EXPR
);
9060 gimple_cond_set_lhs (stmt
, op0
);
9061 gimple_cond_set_rhs (stmt
, new_tree
);
9067 print_gimple_stmt (dump_file
, stmt
, 0, 0);
9068 fprintf (dump_file
, "\n");
9074 /* Try again after inverting the condition. We only deal
9075 with integral types here, so no need to worry about
9076 issues with inverting FP comparisons. */
9077 cond_code
= invert_tree_comparison (cond_code
, false);
9078 new_tree
= test_for_singularity (cond_code
, op0
, op1
, vr
);
9084 fprintf (dump_file
, "Simplified relational ");
9085 print_gimple_stmt (dump_file
, stmt
, 0, 0);
9086 fprintf (dump_file
, " into ");
9089 gimple_cond_set_code (stmt
, NE_EXPR
);
9090 gimple_cond_set_lhs (stmt
, op0
);
9091 gimple_cond_set_rhs (stmt
, new_tree
);
9097 print_gimple_stmt (dump_file
, stmt
, 0, 0);
9098 fprintf (dump_file
, "\n");
9106 /* If we have a comparison of an SSA_NAME (OP0) against a constant,
9107 see if OP0 was set by a type conversion where the source of
9108 the conversion is another SSA_NAME with a range that fits
9109 into the range of OP0's type.
9111 If so, the conversion is redundant as the earlier SSA_NAME can be
9112 used for the comparison directly if we just massage the constant in the
9114 if (TREE_CODE (op0
) == SSA_NAME
9115 && TREE_CODE (op1
) == INTEGER_CST
)
9117 gimple def_stmt
= SSA_NAME_DEF_STMT (op0
);
9120 if (!is_gimple_assign (def_stmt
)
9121 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt
)))
9124 innerop
= gimple_assign_rhs1 (def_stmt
);
9126 if (TREE_CODE (innerop
) == SSA_NAME
9127 && !POINTER_TYPE_P (TREE_TYPE (innerop
)))
9129 value_range_t
*vr
= get_value_range (innerop
);
9131 if (range_int_cst_p (vr
)
9132 && range_fits_type_p (vr
,
9133 TYPE_PRECISION (TREE_TYPE (op0
)),
9134 TYPE_SIGN (TREE_TYPE (op0
)))
9135 && int_fits_type_p (op1
, TREE_TYPE (innerop
))
9136 /* The range must not have overflowed, or if it did overflow
9137 we must not be wrapping/trapping overflow and optimizing
9138 with strict overflow semantics. */
9139 && ((!is_negative_overflow_infinity (vr
->min
)
9140 && !is_positive_overflow_infinity (vr
->max
))
9141 || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (innerop
))))
9143 /* If the range overflowed and the user has asked for warnings
9144 when strict overflow semantics were used to optimize code,
9145 issue an appropriate warning. */
9146 if (cond_code
!= EQ_EXPR
&& cond_code
!= NE_EXPR
9147 && (is_negative_overflow_infinity (vr
->min
)
9148 || is_positive_overflow_infinity (vr
->max
))
9149 && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_CONDITIONAL
))
9151 location_t location
;
9153 if (!gimple_has_location (stmt
))
9154 location
= input_location
;
9156 location
= gimple_location (stmt
);
9157 warning_at (location
, OPT_Wstrict_overflow
,
9158 "assuming signed overflow does not occur when "
9159 "simplifying conditional");
9162 tree newconst
= fold_convert (TREE_TYPE (innerop
), op1
);
9163 gimple_cond_set_lhs (stmt
, innerop
);
9164 gimple_cond_set_rhs (stmt
, newconst
);
9173 /* Simplify a switch statement using the value range of the switch
9177 simplify_switch_using_ranges (gimple stmt
)
9179 tree op
= gimple_switch_index (stmt
);
9184 size_t i
= 0, j
= 0, n
, n2
;
9187 size_t k
= 1, l
= 0;
9189 if (TREE_CODE (op
) == SSA_NAME
)
9191 vr
= get_value_range (op
);
9193 /* We can only handle integer ranges. */
9194 if ((vr
->type
!= VR_RANGE
9195 && vr
->type
!= VR_ANTI_RANGE
)
9196 || symbolic_range_p (vr
))
9199 /* Find case label for min/max of the value range. */
9200 take_default
= !find_case_label_ranges (stmt
, vr
, &i
, &j
, &k
, &l
);
9202 else if (TREE_CODE (op
) == INTEGER_CST
)
9204 take_default
= !find_case_label_index (stmt
, 1, op
, &i
);
9218 n
= gimple_switch_num_labels (stmt
);
9220 /* Bail out if this is just all edges taken. */
9226 /* Build a new vector of taken case labels. */
9227 vec2
= make_tree_vec (j
- i
+ 1 + l
- k
+ 1 + (int)take_default
);
9230 /* Add the default edge, if necessary. */
9232 TREE_VEC_ELT (vec2
, n2
++) = gimple_switch_default_label (stmt
);
9234 for (; i
<= j
; ++i
, ++n2
)
9235 TREE_VEC_ELT (vec2
, n2
) = gimple_switch_label (stmt
, i
);
9237 for (; k
<= l
; ++k
, ++n2
)
9238 TREE_VEC_ELT (vec2
, n2
) = gimple_switch_label (stmt
, k
);
9240 /* Mark needed edges. */
9241 for (i
= 0; i
< n2
; ++i
)
9243 e
= find_edge (gimple_bb (stmt
),
9244 label_to_block (CASE_LABEL (TREE_VEC_ELT (vec2
, i
))));
9245 e
->aux
= (void *)-1;
9248 /* Queue not needed edges for later removal. */
9249 FOR_EACH_EDGE (e
, ei
, gimple_bb (stmt
)->succs
)
9251 if (e
->aux
== (void *)-1)
9257 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
9259 fprintf (dump_file
, "removing unreachable case label\n");
9261 to_remove_edges
.safe_push (e
);
9262 e
->flags
&= ~EDGE_EXECUTABLE
;
9265 /* And queue an update for the stmt. */
9268 to_update_switch_stmts
.safe_push (su
);
9272 /* Simplify an integral conversion from an SSA name in STMT. */
9275 simplify_conversion_using_ranges (gimple stmt
)
9277 tree innerop
, middleop
, finaltype
;
9279 value_range_t
*innervr
;
9280 signop inner_sgn
, middle_sgn
, final_sgn
;
9281 unsigned inner_prec
, middle_prec
, final_prec
;
9282 widest_int innermin
, innermed
, innermax
, middlemin
, middlemed
, middlemax
;
9284 finaltype
= TREE_TYPE (gimple_assign_lhs (stmt
));
9285 if (!INTEGRAL_TYPE_P (finaltype
))
9287 middleop
= gimple_assign_rhs1 (stmt
);
9288 def_stmt
= SSA_NAME_DEF_STMT (middleop
);
9289 if (!is_gimple_assign (def_stmt
)
9290 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt
)))
9292 innerop
= gimple_assign_rhs1 (def_stmt
);
9293 if (TREE_CODE (innerop
) != SSA_NAME
9294 || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (innerop
))
9297 /* Get the value-range of the inner operand. */
9298 innervr
= get_value_range (innerop
);
9299 if (innervr
->type
!= VR_RANGE
9300 || TREE_CODE (innervr
->min
) != INTEGER_CST
9301 || TREE_CODE (innervr
->max
) != INTEGER_CST
)
9304 /* Simulate the conversion chain to check if the result is equal if
9305 the middle conversion is removed. */
9306 innermin
= wi::to_widest (innervr
->min
);
9307 innermax
= wi::to_widest (innervr
->max
);
9309 inner_prec
= TYPE_PRECISION (TREE_TYPE (innerop
));
9310 middle_prec
= TYPE_PRECISION (TREE_TYPE (middleop
));
9311 final_prec
= TYPE_PRECISION (finaltype
);
9313 /* If the first conversion is not injective, the second must not
9315 if (wi::gtu_p (innermax
- innermin
,
9316 wi::mask
<widest_int
> (middle_prec
, false))
9317 && middle_prec
< final_prec
)
9319 /* We also want a medium value so that we can track the effect that
9320 narrowing conversions with sign change have. */
9321 inner_sgn
= TYPE_SIGN (TREE_TYPE (innerop
));
9322 if (inner_sgn
== UNSIGNED
)
9323 innermed
= wi::shifted_mask
<widest_int
> (1, inner_prec
- 1, false);
9326 if (wi::cmp (innermin
, innermed
, inner_sgn
) >= 0
9327 || wi::cmp (innermed
, innermax
, inner_sgn
) >= 0)
9328 innermed
= innermin
;
9330 middle_sgn
= TYPE_SIGN (TREE_TYPE (middleop
));
9331 middlemin
= wi::ext (innermin
, middle_prec
, middle_sgn
);
9332 middlemed
= wi::ext (innermed
, middle_prec
, middle_sgn
);
9333 middlemax
= wi::ext (innermax
, middle_prec
, middle_sgn
);
9335 /* Require that the final conversion applied to both the original
9336 and the intermediate range produces the same result. */
9337 final_sgn
= TYPE_SIGN (finaltype
);
9338 if (wi::ext (middlemin
, final_prec
, final_sgn
)
9339 != wi::ext (innermin
, final_prec
, final_sgn
)
9340 || wi::ext (middlemed
, final_prec
, final_sgn
)
9341 != wi::ext (innermed
, final_prec
, final_sgn
)
9342 || wi::ext (middlemax
, final_prec
, final_sgn
)
9343 != wi::ext (innermax
, final_prec
, final_sgn
))
9346 gimple_assign_set_rhs1 (stmt
, innerop
);
9351 /* Simplify a conversion from integral SSA name to float in STMT. */
9354 simplify_float_conversion_using_ranges (gimple_stmt_iterator
*gsi
, gimple stmt
)
9356 tree rhs1
= gimple_assign_rhs1 (stmt
);
9357 value_range_t
*vr
= get_value_range (rhs1
);
9358 machine_mode fltmode
= TYPE_MODE (TREE_TYPE (gimple_assign_lhs (stmt
)));
9363 /* We can only handle constant ranges. */
9364 if (vr
->type
!= VR_RANGE
9365 || TREE_CODE (vr
->min
) != INTEGER_CST
9366 || TREE_CODE (vr
->max
) != INTEGER_CST
)
9369 /* First check if we can use a signed type in place of an unsigned. */
9370 if (TYPE_UNSIGNED (TREE_TYPE (rhs1
))
9371 && (can_float_p (fltmode
, TYPE_MODE (TREE_TYPE (rhs1
)), 0)
9372 != CODE_FOR_nothing
)
9373 && range_fits_type_p (vr
, TYPE_PRECISION (TREE_TYPE (rhs1
)), SIGNED
))
9374 mode
= TYPE_MODE (TREE_TYPE (rhs1
));
9375 /* If we can do the conversion in the current input mode do nothing. */
9376 else if (can_float_p (fltmode
, TYPE_MODE (TREE_TYPE (rhs1
)),
9377 TYPE_UNSIGNED (TREE_TYPE (rhs1
))) != CODE_FOR_nothing
)
9379 /* Otherwise search for a mode we can use, starting from the narrowest
9380 integer mode available. */
9383 mode
= GET_CLASS_NARROWEST_MODE (MODE_INT
);
9386 /* If we cannot do a signed conversion to float from mode
9387 or if the value-range does not fit in the signed type
9388 try with a wider mode. */
9389 if (can_float_p (fltmode
, mode
, 0) != CODE_FOR_nothing
9390 && range_fits_type_p (vr
, GET_MODE_PRECISION (mode
), SIGNED
))
9393 mode
= GET_MODE_WIDER_MODE (mode
);
9394 /* But do not widen the input. Instead leave that to the
9395 optabs expansion code. */
9396 if (GET_MODE_PRECISION (mode
) > TYPE_PRECISION (TREE_TYPE (rhs1
)))
9399 while (mode
!= VOIDmode
);
9400 if (mode
== VOIDmode
)
9404 /* It works, insert a truncation or sign-change before the
9405 float conversion. */
9406 tem
= make_ssa_name (build_nonstandard_integer_type
9407 (GET_MODE_PRECISION (mode
), 0), NULL
);
9408 conv
= gimple_build_assign_with_ops (NOP_EXPR
, tem
, rhs1
, NULL_TREE
);
9409 gsi_insert_before (gsi
, conv
, GSI_SAME_STMT
);
9410 gimple_assign_set_rhs1 (stmt
, tem
);
9416 /* Simplify an internal fn call using ranges if possible. */
9419 simplify_internal_call_using_ranges (gimple_stmt_iterator
*gsi
, gimple stmt
)
9421 enum tree_code subcode
;
9422 switch (gimple_call_internal_fn (stmt
))
9424 case IFN_UBSAN_CHECK_ADD
:
9425 subcode
= PLUS_EXPR
;
9427 case IFN_UBSAN_CHECK_SUB
:
9428 subcode
= MINUS_EXPR
;
9430 case IFN_UBSAN_CHECK_MUL
:
9431 subcode
= MULT_EXPR
;
9437 value_range_t vr0
= VR_INITIALIZER
;
9438 value_range_t vr1
= VR_INITIALIZER
;
9439 tree op0
= gimple_call_arg (stmt
, 0);
9440 tree op1
= gimple_call_arg (stmt
, 1);
9442 if (TREE_CODE (op0
) == SSA_NAME
)
9443 vr0
= *get_value_range (op0
);
9444 else if (TREE_CODE (op0
) == INTEGER_CST
)
9445 set_value_range_to_value (&vr0
, op0
, NULL
);
9447 set_value_range_to_varying (&vr0
);
9449 if (TREE_CODE (op1
) == SSA_NAME
)
9450 vr1
= *get_value_range (op1
);
9451 else if (TREE_CODE (op1
) == INTEGER_CST
)
9452 set_value_range_to_value (&vr1
, op1
, NULL
);
9454 set_value_range_to_varying (&vr1
);
9456 if (!range_int_cst_p (&vr0
))
9458 /* If one range is VR_ANTI_RANGE, VR_VARYING etc.,
9459 optimize at least x = y + 0; x = y - 0; x = y * 0;
9460 and x = y * 1; which never overflow. */
9461 if (!range_int_cst_p (&vr1
))
9463 if (tree_int_cst_sgn (vr1
.min
) == -1)
9465 if (compare_tree_int (vr1
.max
, subcode
== MULT_EXPR
) == 1)
9468 else if (!range_int_cst_p (&vr1
))
9470 /* If one range is VR_ANTI_RANGE, VR_VARYING etc.,
9471 optimize at least x = 0 + y; x = 0 * y; and x = 1 * y;
9472 which never overflow. */
9473 if (subcode
== MINUS_EXPR
)
9475 if (!range_int_cst_p (&vr0
))
9477 if (tree_int_cst_sgn (vr0
.min
) == -1)
9479 if (compare_tree_int (vr0
.max
, subcode
== MULT_EXPR
) == 1)
9484 tree r1
= int_const_binop (subcode
, vr0
.min
,
9485 subcode
== MINUS_EXPR
? vr1
.max
: vr1
.min
);
9486 tree r2
= int_const_binop (subcode
, vr0
.max
,
9487 subcode
== MINUS_EXPR
? vr1
.min
: vr1
.max
);
9488 if (r1
== NULL_TREE
|| TREE_OVERFLOW (r1
)
9489 || r2
== NULL_TREE
|| TREE_OVERFLOW (r2
))
9491 if (subcode
== MULT_EXPR
)
9493 tree r3
= int_const_binop (subcode
, vr0
.min
, vr1
.max
);
9494 tree r4
= int_const_binop (subcode
, vr0
.max
, vr1
.min
);
9495 if (r3
== NULL_TREE
|| TREE_OVERFLOW (r3
)
9496 || r4
== NULL_TREE
|| TREE_OVERFLOW (r4
))
9501 gimple g
= gimple_build_assign_with_ops (subcode
, gimple_call_lhs (stmt
),
9503 gsi_replace (gsi
, g
, false);
9507 /* Simplify STMT using ranges if possible. */
9510 simplify_stmt_using_ranges (gimple_stmt_iterator
*gsi
)
9512 gimple stmt
= gsi_stmt (*gsi
);
9513 if (is_gimple_assign (stmt
))
9515 enum tree_code rhs_code
= gimple_assign_rhs_code (stmt
);
9516 tree rhs1
= gimple_assign_rhs1 (stmt
);
9522 /* Transform EQ_EXPR, NE_EXPR into BIT_XOR_EXPR or identity
9523 if the RHS is zero or one, and the LHS are known to be boolean
9525 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
9526 return simplify_truth_ops_using_ranges (gsi
, stmt
);
9529 /* Transform TRUNC_DIV_EXPR and TRUNC_MOD_EXPR into RSHIFT_EXPR
9530 and BIT_AND_EXPR respectively if the first operand is greater
9531 than zero and the second operand is an exact power of two. */
9532 case TRUNC_DIV_EXPR
:
9533 case TRUNC_MOD_EXPR
:
9534 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1
))
9535 && integer_pow2p (gimple_assign_rhs2 (stmt
)))
9536 return simplify_div_or_mod_using_ranges (stmt
);
9539 /* Transform ABS (X) into X or -X as appropriate. */
9541 if (TREE_CODE (rhs1
) == SSA_NAME
9542 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
9543 return simplify_abs_using_ranges (stmt
);
9548 /* Optimize away BIT_AND_EXPR and BIT_IOR_EXPR
9549 if all the bits being cleared are already cleared or
9550 all the bits being set are already set. */
9551 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
9552 return simplify_bit_ops_using_ranges (gsi
, stmt
);
9556 if (TREE_CODE (rhs1
) == SSA_NAME
9557 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
9558 return simplify_conversion_using_ranges (stmt
);
9562 if (TREE_CODE (rhs1
) == SSA_NAME
9563 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
9564 return simplify_float_conversion_using_ranges (gsi
, stmt
);
9571 else if (gimple_code (stmt
) == GIMPLE_COND
)
9572 return simplify_cond_using_ranges (stmt
);
9573 else if (gimple_code (stmt
) == GIMPLE_SWITCH
)
9574 return simplify_switch_using_ranges (stmt
);
9575 else if (is_gimple_call (stmt
)
9576 && gimple_call_internal_p (stmt
))
9577 return simplify_internal_call_using_ranges (gsi
, stmt
);
9582 /* If the statement pointed by SI has a predicate whose value can be
9583 computed using the value range information computed by VRP, compute
9584 its value and return true. Otherwise, return false. */
9587 fold_predicate_in (gimple_stmt_iterator
*si
)
9589 bool assignment_p
= false;
9591 gimple stmt
= gsi_stmt (*si
);
9593 if (is_gimple_assign (stmt
)
9594 && TREE_CODE_CLASS (gimple_assign_rhs_code (stmt
)) == tcc_comparison
)
9596 assignment_p
= true;
9597 val
= vrp_evaluate_conditional (gimple_assign_rhs_code (stmt
),
9598 gimple_assign_rhs1 (stmt
),
9599 gimple_assign_rhs2 (stmt
),
9602 else if (gimple_code (stmt
) == GIMPLE_COND
)
9603 val
= vrp_evaluate_conditional (gimple_cond_code (stmt
),
9604 gimple_cond_lhs (stmt
),
9605 gimple_cond_rhs (stmt
),
9613 val
= fold_convert (gimple_expr_type (stmt
), val
);
9617 fprintf (dump_file
, "Folding predicate ");
9618 print_gimple_expr (dump_file
, stmt
, 0, 0);
9619 fprintf (dump_file
, " to ");
9620 print_generic_expr (dump_file
, val
, 0);
9621 fprintf (dump_file
, "\n");
9624 if (is_gimple_assign (stmt
))
9625 gimple_assign_set_rhs_from_tree (si
, val
);
9628 gcc_assert (gimple_code (stmt
) == GIMPLE_COND
);
9629 if (integer_zerop (val
))
9630 gimple_cond_make_false (stmt
);
9631 else if (integer_onep (val
))
9632 gimple_cond_make_true (stmt
);
9643 /* Callback for substitute_and_fold folding the stmt at *SI. */
9646 vrp_fold_stmt (gimple_stmt_iterator
*si
)
9648 if (fold_predicate_in (si
))
9651 return simplify_stmt_using_ranges (si
);
9654 /* Stack of dest,src equivalency pairs that need to be restored after
9655 each attempt to thread a block's incoming edge to an outgoing edge.
9657 A NULL entry is used to mark the end of pairs which need to be
9659 static vec
<tree
> equiv_stack
;
9661 /* A trivial wrapper so that we can present the generic jump threading
9662 code with a simple API for simplifying statements. STMT is the
9663 statement we want to simplify, WITHIN_STMT provides the location
9664 for any overflow warnings. */
9667 simplify_stmt_for_jump_threading (gimple stmt
, gimple within_stmt
)
9669 if (gimple_code (stmt
) == GIMPLE_COND
)
9670 return vrp_evaluate_conditional (gimple_cond_code (stmt
),
9671 gimple_cond_lhs (stmt
),
9672 gimple_cond_rhs (stmt
), within_stmt
);
9674 if (gimple_code (stmt
) == GIMPLE_ASSIGN
)
9676 value_range_t new_vr
= VR_INITIALIZER
;
9677 tree lhs
= gimple_assign_lhs (stmt
);
9679 if (TREE_CODE (lhs
) == SSA_NAME
9680 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs
))
9681 || POINTER_TYPE_P (TREE_TYPE (lhs
))))
9683 extract_range_from_assignment (&new_vr
, stmt
);
9684 if (range_int_cst_singleton_p (&new_vr
))
9692 /* Blocks which have more than one predecessor and more than
9693 one successor present jump threading opportunities, i.e.,
9694 when the block is reached from a specific predecessor, we
9695 may be able to determine which of the outgoing edges will
9696 be traversed. When this optimization applies, we are able
9697 to avoid conditionals at runtime and we may expose secondary
9698 optimization opportunities.
9700 This routine is effectively a driver for the generic jump
9701 threading code. It basically just presents the generic code
9702 with edges that may be suitable for jump threading.
9704 Unlike DOM, we do not iterate VRP if jump threading was successful.
9705 While iterating may expose new opportunities for VRP, it is expected
9706 those opportunities would be very limited and the compile time cost
9707 to expose those opportunities would be significant.
9709 As jump threading opportunities are discovered, they are registered
9710 for later realization. */
9713 identify_jump_threads (void)
9720 /* Ugh. When substituting values earlier in this pass we can
9721 wipe the dominance information. So rebuild the dominator
9722 information as we need it within the jump threading code. */
9723 calculate_dominance_info (CDI_DOMINATORS
);
9725 /* We do not allow VRP information to be used for jump threading
9726 across a back edge in the CFG. Otherwise it becomes too
9727 difficult to avoid eliminating loop exit tests. Of course
9728 EDGE_DFS_BACK is not accurate at this time so we have to
9730 mark_dfs_back_edges ();
9732 /* Do not thread across edges we are about to remove. Just marking
9733 them as EDGE_DFS_BACK will do. */
9734 FOR_EACH_VEC_ELT (to_remove_edges
, i
, e
)
9735 e
->flags
|= EDGE_DFS_BACK
;
9737 /* Allocate our unwinder stack to unwind any temporary equivalences
9738 that might be recorded. */
9739 equiv_stack
.create (20);
9741 /* To avoid lots of silly node creation, we create a single
9742 conditional and just modify it in-place when attempting to
9744 dummy
= gimple_build_cond (EQ_EXPR
,
9745 integer_zero_node
, integer_zero_node
,
9748 /* Walk through all the blocks finding those which present a
9749 potential jump threading opportunity. We could set this up
9750 as a dominator walker and record data during the walk, but
9751 I doubt it's worth the effort for the classes of jump
9752 threading opportunities we are trying to identify at this
9753 point in compilation. */
9754 FOR_EACH_BB_FN (bb
, cfun
)
9758 /* If the generic jump threading code does not find this block
9759 interesting, then there is nothing to do. */
9760 if (! potentially_threadable_block (bb
))
9763 /* We only care about blocks ending in a COND_EXPR. While there
9764 may be some value in handling SWITCH_EXPR here, I doubt it's
9765 terribly important. */
9766 last
= gsi_stmt (gsi_last_bb (bb
));
9768 /* We're basically looking for a switch or any kind of conditional with
9769 integral or pointer type arguments. Note the type of the second
9770 argument will be the same as the first argument, so no need to
9771 check it explicitly. */
9772 if (gimple_code (last
) == GIMPLE_SWITCH
9773 || (gimple_code (last
) == GIMPLE_COND
9774 && TREE_CODE (gimple_cond_lhs (last
)) == SSA_NAME
9775 && (INTEGRAL_TYPE_P (TREE_TYPE (gimple_cond_lhs (last
)))
9776 || POINTER_TYPE_P (TREE_TYPE (gimple_cond_lhs (last
))))
9777 && (TREE_CODE (gimple_cond_rhs (last
)) == SSA_NAME
9778 || is_gimple_min_invariant (gimple_cond_rhs (last
)))))
9782 /* We've got a block with multiple predecessors and multiple
9783 successors which also ends in a suitable conditional or
9784 switch statement. For each predecessor, see if we can thread
9785 it to a specific successor. */
9786 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
9788 /* Do not thread across back edges or abnormal edges
9790 if (e
->flags
& (EDGE_DFS_BACK
| EDGE_COMPLEX
))
9793 thread_across_edge (dummy
, e
, true, &equiv_stack
,
9794 simplify_stmt_for_jump_threading
);
9799 /* We do not actually update the CFG or SSA graphs at this point as
9800 ASSERT_EXPRs are still in the IL and cfg cleanup code does not yet
9801 handle ASSERT_EXPRs gracefully. */
9804 /* We identified all the jump threading opportunities earlier, but could
9805 not transform the CFG at that time. This routine transforms the
9806 CFG and arranges for the dominator tree to be rebuilt if necessary.
9808 Note the SSA graph update will occur during the normal TODO
9809 processing by the pass manager. */
9811 finalize_jump_threads (void)
9813 thread_through_all_blocks (false);
9814 equiv_stack
.release ();
9818 /* Traverse all the blocks folding conditionals with known ranges. */
9825 values_propagated
= true;
9829 fprintf (dump_file
, "\nValue ranges after VRP:\n\n");
9830 dump_all_value_ranges (dump_file
);
9831 fprintf (dump_file
, "\n");
9834 substitute_and_fold (op_with_constant_singleton_value_range
,
9835 vrp_fold_stmt
, false);
9837 if (warn_array_bounds
)
9838 check_all_array_refs ();
9840 /* We must identify jump threading opportunities before we release
9841 the datastructures built by VRP. */
9842 identify_jump_threads ();
9844 /* Set value range to non pointer SSA_NAMEs. */
9845 for (i
= 0; i
< num_vr_values
; i
++)
9848 tree name
= ssa_name (i
);
9851 || POINTER_TYPE_P (TREE_TYPE (name
))
9852 || (vr_value
[i
]->type
== VR_VARYING
)
9853 || (vr_value
[i
]->type
== VR_UNDEFINED
))
9856 if ((TREE_CODE (vr_value
[i
]->min
) == INTEGER_CST
)
9857 && (TREE_CODE (vr_value
[i
]->max
) == INTEGER_CST
)
9858 && (vr_value
[i
]->type
== VR_RANGE
9859 || vr_value
[i
]->type
== VR_ANTI_RANGE
))
9860 set_range_info (name
, vr_value
[i
]->type
, vr_value
[i
]->min
,
9864 /* Free allocated memory. */
9865 for (i
= 0; i
< num_vr_values
; i
++)
9868 BITMAP_FREE (vr_value
[i
]->equiv
);
9873 free (vr_phi_edge_counts
);
9875 /* So that we can distinguish between VRP data being available
9876 and not available. */
9878 vr_phi_edge_counts
= NULL
;
9882 /* Main entry point to VRP (Value Range Propagation). This pass is
9883 loosely based on J. R. C. Patterson, ``Accurate Static Branch
9884 Prediction by Value Range Propagation,'' in SIGPLAN Conference on
9885 Programming Language Design and Implementation, pp. 67-78, 1995.
9886 Also available at http://citeseer.ist.psu.edu/patterson95accurate.html
9888 This is essentially an SSA-CCP pass modified to deal with ranges
9889 instead of constants.
9891 While propagating ranges, we may find that two or more SSA name
9892 have equivalent, though distinct ranges. For instance,
9895 2 p_4 = ASSERT_EXPR <p_3, p_3 != 0>
9897 4 p_5 = ASSERT_EXPR <p_4, p_4 == q_2>;
9901 In the code above, pointer p_5 has range [q_2, q_2], but from the
9902 code we can also determine that p_5 cannot be NULL and, if q_2 had
9903 a non-varying range, p_5's range should also be compatible with it.
9905 These equivalences are created by two expressions: ASSERT_EXPR and
9906 copy operations. Since p_5 is an assertion on p_4, and p_4 was the
9907 result of another assertion, then we can use the fact that p_5 and
9908 p_4 are equivalent when evaluating p_5's range.
9910 Together with value ranges, we also propagate these equivalences
9911 between names so that we can take advantage of information from
9912 multiple ranges when doing final replacement. Note that this
9913 equivalency relation is transitive but not symmetric.
9915 In the example above, p_5 is equivalent to p_4, q_2 and p_3, but we
9916 cannot assert that q_2 is equivalent to p_5 because q_2 may be used
9917 in contexts where that assertion does not hold (e.g., in line 6).
9919 TODO, the main difference between this pass and Patterson's is that
9920 we do not propagate edge probabilities. We only compute whether
9921 edges can be taken or not. That is, instead of having a spectrum
9922 of jump probabilities between 0 and 1, we only deal with 0, 1 and
9923 DON'T KNOW. In the future, it may be worthwhile to propagate
9924 probabilities to aid branch prediction. */
9933 loop_optimizer_init (LOOPS_NORMAL
| LOOPS_HAVE_RECORDED_EXITS
);
9934 rewrite_into_loop_closed_ssa (NULL
, TODO_update_ssa
);
9937 /* ??? This ends up using stale EDGE_DFS_BACK for liveness computation.
9938 Inserting assertions may split edges which will invalidate
9940 insert_range_assertions ();
9942 to_remove_edges
.create (10);
9943 to_update_switch_stmts
.create (5);
9944 threadedge_initialize_values ();
9946 /* For visiting PHI nodes we need EDGE_DFS_BACK computed. */
9947 mark_dfs_back_edges ();
9950 ssa_propagate (vrp_visit_stmt
, vrp_visit_phi_node
);
9953 free_numbers_of_iterations_estimates ();
9955 /* ASSERT_EXPRs must be removed before finalizing jump threads
9956 as finalizing jump threads calls the CFG cleanup code which
9957 does not properly handle ASSERT_EXPRs. */
9958 remove_range_assertions ();
9960 /* If we exposed any new variables, go ahead and put them into
9961 SSA form now, before we handle jump threading. This simplifies
9962 interactions between rewriting of _DECL nodes into SSA form
9963 and rewriting SSA_NAME nodes into SSA form after block
9964 duplication and CFG manipulation. */
9965 update_ssa (TODO_update_ssa
);
9967 finalize_jump_threads ();
9969 /* Remove dead edges from SWITCH_EXPR optimization. This leaves the
9970 CFG in a broken state and requires a cfg_cleanup run. */
9971 FOR_EACH_VEC_ELT (to_remove_edges
, i
, e
)
9973 /* Update SWITCH_EXPR case label vector. */
9974 FOR_EACH_VEC_ELT (to_update_switch_stmts
, i
, su
)
9977 size_t n
= TREE_VEC_LENGTH (su
->vec
);
9979 gimple_switch_set_num_labels (su
->stmt
, n
);
9980 for (j
= 0; j
< n
; j
++)
9981 gimple_switch_set_label (su
->stmt
, j
, TREE_VEC_ELT (su
->vec
, j
));
9982 /* As we may have replaced the default label with a regular one
9983 make sure to make it a real default label again. This ensures
9984 optimal expansion. */
9985 label
= gimple_switch_label (su
->stmt
, 0);
9986 CASE_LOW (label
) = NULL_TREE
;
9987 CASE_HIGH (label
) = NULL_TREE
;
9990 if (to_remove_edges
.length () > 0)
9992 free_dominance_info (CDI_DOMINATORS
);
9993 loops_state_set (LOOPS_NEED_FIXUP
);
9996 to_remove_edges
.release ();
9997 to_update_switch_stmts
.release ();
9998 threadedge_finalize_values ();
10001 loop_optimizer_finalize ();
10007 const pass_data pass_data_vrp
=
10009 GIMPLE_PASS
, /* type */
10011 OPTGROUP_NONE
, /* optinfo_flags */
10012 TV_TREE_VRP
, /* tv_id */
10013 PROP_ssa
, /* properties_required */
10014 0, /* properties_provided */
10015 0, /* properties_destroyed */
10016 0, /* todo_flags_start */
10017 ( TODO_cleanup_cfg
| TODO_update_ssa
), /* todo_flags_finish */
10020 class pass_vrp
: public gimple_opt_pass
10023 pass_vrp (gcc::context
*ctxt
)
10024 : gimple_opt_pass (pass_data_vrp
, ctxt
)
10027 /* opt_pass methods: */
10028 opt_pass
* clone () { return new pass_vrp (m_ctxt
); }
10029 virtual bool gate (function
*) { return flag_tree_vrp
!= 0; }
10030 virtual unsigned int execute (function
*) { return execute_vrp (); }
10032 }; // class pass_vrp
10034 } // anon namespace
10037 make_pass_vrp (gcc::context
*ctxt
)
10039 return new pass_vrp (ctxt
);