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"
73 #include "tree-ssa-threadedge.h"
78 /* Range of values that can be associated with an SSA_NAME after VRP
82 /* Lattice value represented by this range. */
83 enum value_range_type type
;
85 /* Minimum and maximum values represented by this range. These
86 values should be interpreted as follows:
88 - If TYPE is VR_UNDEFINED or VR_VARYING then MIN and MAX must
91 - If TYPE == VR_RANGE then MIN holds the minimum value and
92 MAX holds the maximum value of the range [MIN, MAX].
94 - If TYPE == ANTI_RANGE the variable is known to NOT
95 take any values in the range [MIN, MAX]. */
99 /* Set of SSA names whose value ranges are equivalent to this one.
100 This set is only valid when TYPE is VR_RANGE or VR_ANTI_RANGE. */
104 typedef struct value_range_d value_range_t
;
106 #define VR_INITIALIZER { VR_UNDEFINED, NULL_TREE, NULL_TREE, NULL }
108 /* Set of SSA names found live during the RPO traversal of the function
109 for still active basic-blocks. */
110 static sbitmap
*live
;
112 /* Return true if the SSA name NAME is live on the edge E. */
115 live_on_edge (edge e
, tree name
)
117 return (live
[e
->dest
->index
]
118 && bitmap_bit_p (live
[e
->dest
->index
], SSA_NAME_VERSION (name
)));
121 /* Local functions. */
122 static int compare_values (tree val1
, tree val2
);
123 static int compare_values_warnv (tree val1
, tree val2
, bool *);
124 static void vrp_meet (value_range_t
*, value_range_t
*);
125 static void vrp_intersect_ranges (value_range_t
*, value_range_t
*);
126 static tree
vrp_evaluate_conditional_warnv_with_ops (enum tree_code
,
127 tree
, tree
, bool, bool *,
130 /* Location information for ASSERT_EXPRs. Each instance of this
131 structure describes an ASSERT_EXPR for an SSA name. Since a single
132 SSA name may have more than one assertion associated with it, these
133 locations are kept in a linked list attached to the corresponding
135 struct assert_locus_d
137 /* Basic block where the assertion would be inserted. */
140 /* Some assertions need to be inserted on an edge (e.g., assertions
141 generated by COND_EXPRs). In those cases, BB will be NULL. */
144 /* Pointer to the statement that generated this assertion. */
145 gimple_stmt_iterator si
;
147 /* Predicate code for the ASSERT_EXPR. Must be COMPARISON_CLASS_P. */
148 enum tree_code comp_code
;
150 /* Value being compared against. */
153 /* Expression to compare. */
156 /* Next node in the linked list. */
157 struct assert_locus_d
*next
;
160 typedef struct assert_locus_d
*assert_locus_t
;
162 /* If bit I is present, it means that SSA name N_i has a list of
163 assertions that should be inserted in the IL. */
164 static bitmap need_assert_for
;
166 /* Array of locations lists where to insert assertions. ASSERTS_FOR[I]
167 holds a list of ASSERT_LOCUS_T nodes that describe where
168 ASSERT_EXPRs for SSA name N_I should be inserted. */
169 static assert_locus_t
*asserts_for
;
171 /* Value range array. After propagation, VR_VALUE[I] holds the range
172 of values that SSA name N_I may take. */
173 static unsigned num_vr_values
;
174 static value_range_t
**vr_value
;
175 static bool values_propagated
;
177 /* For a PHI node which sets SSA name N_I, VR_COUNTS[I] holds the
178 number of executable edges we saw the last time we visited the
180 static int *vr_phi_edge_counts
;
187 static vec
<edge
> to_remove_edges
;
188 static vec
<switch_update
> to_update_switch_stmts
;
191 /* Return the maximum value for TYPE. */
194 vrp_val_max (const_tree type
)
196 if (!INTEGRAL_TYPE_P (type
))
199 return TYPE_MAX_VALUE (type
);
202 /* Return the minimum value for TYPE. */
205 vrp_val_min (const_tree type
)
207 if (!INTEGRAL_TYPE_P (type
))
210 return TYPE_MIN_VALUE (type
);
213 /* Return whether VAL is equal to the maximum value of its type. This
214 will be true for a positive overflow infinity. We can't do a
215 simple equality comparison with TYPE_MAX_VALUE because C typedefs
216 and Ada subtypes can produce types whose TYPE_MAX_VALUE is not ==
217 to the integer constant with the same value in the type. */
220 vrp_val_is_max (const_tree val
)
222 tree type_max
= vrp_val_max (TREE_TYPE (val
));
223 return (val
== type_max
224 || (type_max
!= NULL_TREE
225 && operand_equal_p (val
, type_max
, 0)));
228 /* Return whether VAL is equal to the minimum value of its type. This
229 will be true for a negative overflow infinity. */
232 vrp_val_is_min (const_tree val
)
234 tree type_min
= vrp_val_min (TREE_TYPE (val
));
235 return (val
== type_min
236 || (type_min
!= NULL_TREE
237 && operand_equal_p (val
, type_min
, 0)));
241 /* Return whether TYPE should use an overflow infinity distinct from
242 TYPE_{MIN,MAX}_VALUE. We use an overflow infinity value to
243 represent a signed overflow during VRP computations. An infinity
244 is distinct from a half-range, which will go from some number to
245 TYPE_{MIN,MAX}_VALUE. */
248 needs_overflow_infinity (const_tree type
)
250 return INTEGRAL_TYPE_P (type
) && !TYPE_OVERFLOW_WRAPS (type
);
253 /* Return whether TYPE can support our overflow infinity
254 representation: we use the TREE_OVERFLOW flag, which only exists
255 for constants. If TYPE doesn't support this, we don't optimize
256 cases which would require signed overflow--we drop them to
260 supports_overflow_infinity (const_tree type
)
262 tree min
= vrp_val_min (type
), max
= vrp_val_max (type
);
263 #ifdef ENABLE_CHECKING
264 gcc_assert (needs_overflow_infinity (type
));
266 return (min
!= NULL_TREE
267 && CONSTANT_CLASS_P (min
)
269 && CONSTANT_CLASS_P (max
));
272 /* VAL is the maximum or minimum value of a type. Return a
273 corresponding overflow infinity. */
276 make_overflow_infinity (tree val
)
278 gcc_checking_assert (val
!= NULL_TREE
&& CONSTANT_CLASS_P (val
));
279 val
= copy_node (val
);
280 TREE_OVERFLOW (val
) = 1;
284 /* Return a negative overflow infinity for TYPE. */
287 negative_overflow_infinity (tree type
)
289 gcc_checking_assert (supports_overflow_infinity (type
));
290 return make_overflow_infinity (vrp_val_min (type
));
293 /* Return a positive overflow infinity for TYPE. */
296 positive_overflow_infinity (tree type
)
298 gcc_checking_assert (supports_overflow_infinity (type
));
299 return make_overflow_infinity (vrp_val_max (type
));
302 /* Return whether VAL is a negative overflow infinity. */
305 is_negative_overflow_infinity (const_tree val
)
307 return (TREE_OVERFLOW_P (val
)
308 && needs_overflow_infinity (TREE_TYPE (val
))
309 && vrp_val_is_min (val
));
312 /* Return whether VAL is a positive overflow infinity. */
315 is_positive_overflow_infinity (const_tree val
)
317 return (TREE_OVERFLOW_P (val
)
318 && needs_overflow_infinity (TREE_TYPE (val
))
319 && vrp_val_is_max (val
));
322 /* Return whether VAL is a positive or negative overflow infinity. */
325 is_overflow_infinity (const_tree val
)
327 return (TREE_OVERFLOW_P (val
)
328 && needs_overflow_infinity (TREE_TYPE (val
))
329 && (vrp_val_is_min (val
) || vrp_val_is_max (val
)));
332 /* Return whether STMT has a constant rhs that is_overflow_infinity. */
335 stmt_overflow_infinity (gimple stmt
)
337 if (is_gimple_assign (stmt
)
338 && get_gimple_rhs_class (gimple_assign_rhs_code (stmt
)) ==
340 return is_overflow_infinity (gimple_assign_rhs1 (stmt
));
344 /* If VAL is now an overflow infinity, return VAL. Otherwise, return
345 the same value with TREE_OVERFLOW clear. This can be used to avoid
346 confusing a regular value with an overflow value. */
349 avoid_overflow_infinity (tree val
)
351 if (!is_overflow_infinity (val
))
354 if (vrp_val_is_max (val
))
355 return vrp_val_max (TREE_TYPE (val
));
358 gcc_checking_assert (vrp_val_is_min (val
));
359 return vrp_val_min (TREE_TYPE (val
));
364 /* Return true if ARG is marked with the nonnull attribute in the
365 current function signature. */
368 nonnull_arg_p (const_tree arg
)
370 tree t
, attrs
, fntype
;
371 unsigned HOST_WIDE_INT arg_num
;
373 gcc_assert (TREE_CODE (arg
) == PARM_DECL
&& POINTER_TYPE_P (TREE_TYPE (arg
)));
375 /* The static chain decl is always non null. */
376 if (arg
== cfun
->static_chain_decl
)
379 fntype
= TREE_TYPE (current_function_decl
);
380 for (attrs
= TYPE_ATTRIBUTES (fntype
); attrs
; attrs
= TREE_CHAIN (attrs
))
382 attrs
= lookup_attribute ("nonnull", attrs
);
384 /* If "nonnull" wasn't specified, we know nothing about the argument. */
385 if (attrs
== NULL_TREE
)
388 /* If "nonnull" applies to all the arguments, then ARG is non-null. */
389 if (TREE_VALUE (attrs
) == NULL_TREE
)
392 /* Get the position number for ARG in the function signature. */
393 for (arg_num
= 1, t
= DECL_ARGUMENTS (current_function_decl
);
395 t
= DECL_CHAIN (t
), arg_num
++)
401 gcc_assert (t
== arg
);
403 /* Now see if ARG_NUM is mentioned in the nonnull list. */
404 for (t
= TREE_VALUE (attrs
); t
; t
= TREE_CHAIN (t
))
406 if (compare_tree_int (TREE_VALUE (t
), arg_num
) == 0)
415 /* Set value range VR to VR_UNDEFINED. */
418 set_value_range_to_undefined (value_range_t
*vr
)
420 vr
->type
= VR_UNDEFINED
;
421 vr
->min
= vr
->max
= NULL_TREE
;
423 bitmap_clear (vr
->equiv
);
427 /* Set value range VR to VR_VARYING. */
430 set_value_range_to_varying (value_range_t
*vr
)
432 vr
->type
= VR_VARYING
;
433 vr
->min
= vr
->max
= NULL_TREE
;
435 bitmap_clear (vr
->equiv
);
439 /* Set value range VR to {T, MIN, MAX, EQUIV}. */
442 set_value_range (value_range_t
*vr
, enum value_range_type t
, tree min
,
443 tree max
, bitmap equiv
)
445 #if defined ENABLE_CHECKING
446 /* Check the validity of the range. */
447 if (t
== VR_RANGE
|| t
== VR_ANTI_RANGE
)
451 gcc_assert (min
&& max
);
453 gcc_assert ((!TREE_OVERFLOW_P (min
) || is_overflow_infinity (min
))
454 && (!TREE_OVERFLOW_P (max
) || is_overflow_infinity (max
)));
456 if (INTEGRAL_TYPE_P (TREE_TYPE (min
)) && t
== VR_ANTI_RANGE
)
457 gcc_assert (!vrp_val_is_min (min
) || !vrp_val_is_max (max
));
459 cmp
= compare_values (min
, max
);
460 gcc_assert (cmp
== 0 || cmp
== -1 || cmp
== -2);
462 if (needs_overflow_infinity (TREE_TYPE (min
)))
463 gcc_assert (!is_overflow_infinity (min
)
464 || !is_overflow_infinity (max
));
467 if (t
== VR_UNDEFINED
|| t
== VR_VARYING
)
468 gcc_assert (min
== NULL_TREE
&& max
== NULL_TREE
);
470 if (t
== VR_UNDEFINED
|| t
== VR_VARYING
)
471 gcc_assert (equiv
== NULL
|| bitmap_empty_p (equiv
));
478 /* Since updating the equivalence set involves deep copying the
479 bitmaps, only do it if absolutely necessary. */
480 if (vr
->equiv
== NULL
482 vr
->equiv
= BITMAP_ALLOC (NULL
);
484 if (equiv
!= vr
->equiv
)
486 if (equiv
&& !bitmap_empty_p (equiv
))
487 bitmap_copy (vr
->equiv
, equiv
);
489 bitmap_clear (vr
->equiv
);
494 /* Set value range VR to the canonical form of {T, MIN, MAX, EQUIV}.
495 This means adjusting T, MIN and MAX representing the case of a
496 wrapping range with MAX < MIN covering [MIN, type_max] U [type_min, MAX]
497 as anti-rage ~[MAX+1, MIN-1]. Likewise for wrapping anti-ranges.
498 In corner cases where MAX+1 or MIN-1 wraps this will fall back
500 This routine exists to ease canonicalization in the case where we
501 extract ranges from var + CST op limit. */
504 set_and_canonicalize_value_range (value_range_t
*vr
, enum value_range_type t
,
505 tree min
, tree max
, bitmap equiv
)
507 /* Use the canonical setters for VR_UNDEFINED and VR_VARYING. */
508 if (t
== VR_UNDEFINED
)
510 set_value_range_to_undefined (vr
);
513 else if (t
== VR_VARYING
)
515 set_value_range_to_varying (vr
);
519 /* Nothing to canonicalize for symbolic ranges. */
520 if (TREE_CODE (min
) != INTEGER_CST
521 || TREE_CODE (max
) != INTEGER_CST
)
523 set_value_range (vr
, t
, min
, max
, equiv
);
527 /* Wrong order for min and max, to swap them and the VR type we need
529 if (tree_int_cst_lt (max
, min
))
533 /* For one bit precision if max < min, then the swapped
534 range covers all values, so for VR_RANGE it is varying and
535 for VR_ANTI_RANGE empty range, so drop to varying as well. */
536 if (TYPE_PRECISION (TREE_TYPE (min
)) == 1)
538 set_value_range_to_varying (vr
);
542 one
= build_int_cst (TREE_TYPE (min
), 1);
543 tmp
= int_const_binop (PLUS_EXPR
, max
, one
);
544 max
= int_const_binop (MINUS_EXPR
, min
, one
);
547 /* There's one corner case, if we had [C+1, C] before we now have
548 that again. But this represents an empty value range, so drop
549 to varying in this case. */
550 if (tree_int_cst_lt (max
, min
))
552 set_value_range_to_varying (vr
);
556 t
= t
== VR_RANGE
? VR_ANTI_RANGE
: VR_RANGE
;
559 /* Anti-ranges that can be represented as ranges should be so. */
560 if (t
== VR_ANTI_RANGE
)
562 bool is_min
= vrp_val_is_min (min
);
563 bool is_max
= vrp_val_is_max (max
);
565 if (is_min
&& is_max
)
567 /* We cannot deal with empty ranges, drop to varying.
568 ??? This could be VR_UNDEFINED instead. */
569 set_value_range_to_varying (vr
);
572 else if (TYPE_PRECISION (TREE_TYPE (min
)) == 1
573 && (is_min
|| is_max
))
575 /* Non-empty boolean ranges can always be represented
576 as a singleton range. */
578 min
= max
= vrp_val_max (TREE_TYPE (min
));
580 min
= max
= vrp_val_min (TREE_TYPE (min
));
584 /* As a special exception preserve non-null ranges. */
585 && !(TYPE_UNSIGNED (TREE_TYPE (min
))
586 && integer_zerop (max
)))
588 tree one
= build_int_cst (TREE_TYPE (max
), 1);
589 min
= int_const_binop (PLUS_EXPR
, max
, one
);
590 max
= vrp_val_max (TREE_TYPE (max
));
595 tree one
= build_int_cst (TREE_TYPE (min
), 1);
596 max
= int_const_binop (MINUS_EXPR
, min
, one
);
597 min
= vrp_val_min (TREE_TYPE (min
));
602 /* Drop [-INF(OVF), +INF(OVF)] to varying. */
603 if (needs_overflow_infinity (TREE_TYPE (min
))
604 && is_overflow_infinity (min
)
605 && is_overflow_infinity (max
))
607 set_value_range_to_varying (vr
);
611 set_value_range (vr
, t
, min
, max
, equiv
);
614 /* Copy value range FROM into value range TO. */
617 copy_value_range (value_range_t
*to
, value_range_t
*from
)
619 set_value_range (to
, from
->type
, from
->min
, from
->max
, from
->equiv
);
622 /* Set value range VR to a single value. This function is only called
623 with values we get from statements, and exists to clear the
624 TREE_OVERFLOW flag so that we don't think we have an overflow
625 infinity when we shouldn't. */
628 set_value_range_to_value (value_range_t
*vr
, tree val
, bitmap equiv
)
630 gcc_assert (is_gimple_min_invariant (val
));
631 if (TREE_OVERFLOW_P (val
))
632 val
= drop_tree_overflow (val
);
633 set_value_range (vr
, VR_RANGE
, val
, val
, equiv
);
636 /* Set value range VR to a non-negative range of type TYPE.
637 OVERFLOW_INFINITY indicates whether to use an overflow infinity
638 rather than TYPE_MAX_VALUE; this should be true if we determine
639 that the range is nonnegative based on the assumption that signed
640 overflow does not occur. */
643 set_value_range_to_nonnegative (value_range_t
*vr
, tree type
,
644 bool overflow_infinity
)
648 if (overflow_infinity
&& !supports_overflow_infinity (type
))
650 set_value_range_to_varying (vr
);
654 zero
= build_int_cst (type
, 0);
655 set_value_range (vr
, VR_RANGE
, zero
,
657 ? positive_overflow_infinity (type
)
658 : TYPE_MAX_VALUE (type
)),
662 /* Set value range VR to a non-NULL range of type TYPE. */
665 set_value_range_to_nonnull (value_range_t
*vr
, tree type
)
667 tree zero
= build_int_cst (type
, 0);
668 set_value_range (vr
, VR_ANTI_RANGE
, zero
, zero
, vr
->equiv
);
672 /* Set value range VR to a NULL range of type TYPE. */
675 set_value_range_to_null (value_range_t
*vr
, tree type
)
677 set_value_range_to_value (vr
, build_int_cst (type
, 0), vr
->equiv
);
681 /* Set value range VR to a range of a truthvalue of type TYPE. */
684 set_value_range_to_truthvalue (value_range_t
*vr
, tree type
)
686 if (TYPE_PRECISION (type
) == 1)
687 set_value_range_to_varying (vr
);
689 set_value_range (vr
, VR_RANGE
,
690 build_int_cst (type
, 0), build_int_cst (type
, 1),
695 /* If abs (min) < abs (max), set VR to [-max, max], if
696 abs (min) >= abs (max), set VR to [-min, min]. */
699 abs_extent_range (value_range_t
*vr
, tree min
, tree max
)
703 gcc_assert (TREE_CODE (min
) == INTEGER_CST
);
704 gcc_assert (TREE_CODE (max
) == INTEGER_CST
);
705 gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (min
)));
706 gcc_assert (!TYPE_UNSIGNED (TREE_TYPE (min
)));
707 min
= fold_unary (ABS_EXPR
, TREE_TYPE (min
), min
);
708 max
= fold_unary (ABS_EXPR
, TREE_TYPE (max
), max
);
709 if (TREE_OVERFLOW (min
) || TREE_OVERFLOW (max
))
711 set_value_range_to_varying (vr
);
714 cmp
= compare_values (min
, max
);
716 min
= fold_unary (NEGATE_EXPR
, TREE_TYPE (min
), max
);
717 else if (cmp
== 0 || cmp
== 1)
720 min
= fold_unary (NEGATE_EXPR
, TREE_TYPE (min
), min
);
724 set_value_range_to_varying (vr
);
727 set_and_canonicalize_value_range (vr
, VR_RANGE
, min
, max
, NULL
);
731 /* Return value range information for VAR.
733 If we have no values ranges recorded (ie, VRP is not running), then
734 return NULL. Otherwise create an empty range if none existed for VAR. */
736 static value_range_t
*
737 get_value_range (const_tree var
)
739 static const struct value_range_d vr_const_varying
740 = { VR_VARYING
, NULL_TREE
, NULL_TREE
, NULL
};
743 unsigned ver
= SSA_NAME_VERSION (var
);
745 /* If we have no recorded ranges, then return NULL. */
749 /* If we query the range for a new SSA name return an unmodifiable VARYING.
750 We should get here at most from the substitute-and-fold stage which
751 will never try to change values. */
752 if (ver
>= num_vr_values
)
753 return CONST_CAST (value_range_t
*, &vr_const_varying
);
759 /* After propagation finished do not allocate new value-ranges. */
760 if (values_propagated
)
761 return CONST_CAST (value_range_t
*, &vr_const_varying
);
763 /* Create a default value range. */
764 vr_value
[ver
] = vr
= XCNEW (value_range_t
);
766 /* Defer allocating the equivalence set. */
769 /* If VAR is a default definition of a parameter, the variable can
770 take any value in VAR's type. */
771 if (SSA_NAME_IS_DEFAULT_DEF (var
))
773 sym
= SSA_NAME_VAR (var
);
774 if (TREE_CODE (sym
) == PARM_DECL
)
776 /* Try to use the "nonnull" attribute to create ~[0, 0]
777 anti-ranges for pointers. Note that this is only valid with
778 default definitions of PARM_DECLs. */
779 if (POINTER_TYPE_P (TREE_TYPE (sym
))
780 && nonnull_arg_p (sym
))
781 set_value_range_to_nonnull (vr
, TREE_TYPE (sym
));
783 set_value_range_to_varying (vr
);
785 else if (TREE_CODE (sym
) == RESULT_DECL
786 && DECL_BY_REFERENCE (sym
))
787 set_value_range_to_nonnull (vr
, TREE_TYPE (sym
));
793 /* Return true, if VAL1 and VAL2 are equal values for VRP purposes. */
796 vrp_operand_equal_p (const_tree val1
, const_tree val2
)
800 if (!val1
|| !val2
|| !operand_equal_p (val1
, val2
, 0))
802 return is_overflow_infinity (val1
) == is_overflow_infinity (val2
);
805 /* Return true, if the bitmaps B1 and B2 are equal. */
808 vrp_bitmap_equal_p (const_bitmap b1
, const_bitmap b2
)
811 || ((!b1
|| bitmap_empty_p (b1
))
812 && (!b2
|| bitmap_empty_p (b2
)))
814 && bitmap_equal_p (b1
, b2
)));
817 /* Update the value range and equivalence set for variable VAR to
818 NEW_VR. Return true if NEW_VR is different from VAR's previous
821 NOTE: This function assumes that NEW_VR is a temporary value range
822 object created for the sole purpose of updating VAR's range. The
823 storage used by the equivalence set from NEW_VR will be freed by
824 this function. Do not call update_value_range when NEW_VR
825 is the range object associated with another SSA name. */
828 update_value_range (const_tree var
, value_range_t
*new_vr
)
830 value_range_t
*old_vr
;
833 /* Update the value range, if necessary. */
834 old_vr
= get_value_range (var
);
835 is_new
= old_vr
->type
!= new_vr
->type
836 || !vrp_operand_equal_p (old_vr
->min
, new_vr
->min
)
837 || !vrp_operand_equal_p (old_vr
->max
, new_vr
->max
)
838 || !vrp_bitmap_equal_p (old_vr
->equiv
, new_vr
->equiv
);
842 /* Do not allow transitions up the lattice. The following
843 is slightly more awkward than just new_vr->type < old_vr->type
844 because VR_RANGE and VR_ANTI_RANGE need to be considered
845 the same. We may not have is_new when transitioning to
846 UNDEFINED or from VARYING. */
847 if (new_vr
->type
== VR_UNDEFINED
848 || old_vr
->type
== VR_VARYING
)
849 set_value_range_to_varying (old_vr
);
851 set_value_range (old_vr
, new_vr
->type
, new_vr
->min
, new_vr
->max
,
855 BITMAP_FREE (new_vr
->equiv
);
861 /* Add VAR and VAR's equivalence set to EQUIV. This is the central
862 point where equivalence processing can be turned on/off. */
865 add_equivalence (bitmap
*equiv
, const_tree var
)
867 unsigned ver
= SSA_NAME_VERSION (var
);
868 value_range_t
*vr
= vr_value
[ver
];
871 *equiv
= BITMAP_ALLOC (NULL
);
872 bitmap_set_bit (*equiv
, ver
);
874 bitmap_ior_into (*equiv
, vr
->equiv
);
878 /* Return true if VR is ~[0, 0]. */
881 range_is_nonnull (value_range_t
*vr
)
883 return vr
->type
== VR_ANTI_RANGE
884 && integer_zerop (vr
->min
)
885 && integer_zerop (vr
->max
);
889 /* Return true if VR is [0, 0]. */
892 range_is_null (value_range_t
*vr
)
894 return vr
->type
== VR_RANGE
895 && integer_zerop (vr
->min
)
896 && integer_zerop (vr
->max
);
899 /* Return true if max and min of VR are INTEGER_CST. It's not necessary
903 range_int_cst_p (value_range_t
*vr
)
905 return (vr
->type
== VR_RANGE
906 && TREE_CODE (vr
->max
) == INTEGER_CST
907 && TREE_CODE (vr
->min
) == INTEGER_CST
);
910 /* Return true if VR is a INTEGER_CST singleton. */
913 range_int_cst_singleton_p (value_range_t
*vr
)
915 return (range_int_cst_p (vr
)
916 && !is_overflow_infinity (vr
->min
)
917 && !is_overflow_infinity (vr
->max
)
918 && tree_int_cst_equal (vr
->min
, vr
->max
));
921 /* Return true if value range VR involves at least one symbol. */
924 symbolic_range_p (value_range_t
*vr
)
926 return (!is_gimple_min_invariant (vr
->min
)
927 || !is_gimple_min_invariant (vr
->max
));
930 /* Return the single symbol (an SSA_NAME) contained in T if any, or NULL_TREE
931 otherwise. We only handle additive operations and set NEG to true if the
932 symbol is negated and INV to the invariant part, if any. */
935 get_single_symbol (tree t
, bool *neg
, tree
*inv
)
940 if (TREE_CODE (t
) == PLUS_EXPR
941 || TREE_CODE (t
) == POINTER_PLUS_EXPR
942 || TREE_CODE (t
) == MINUS_EXPR
)
944 if (is_gimple_min_invariant (TREE_OPERAND (t
, 0)))
946 neg_
= (TREE_CODE (t
) == MINUS_EXPR
);
947 inv_
= TREE_OPERAND (t
, 0);
948 t
= TREE_OPERAND (t
, 1);
950 else if (is_gimple_min_invariant (TREE_OPERAND (t
, 1)))
953 inv_
= TREE_OPERAND (t
, 1);
954 t
= TREE_OPERAND (t
, 0);
965 if (TREE_CODE (t
) == NEGATE_EXPR
)
967 t
= TREE_OPERAND (t
, 0);
971 if (TREE_CODE (t
) != SSA_NAME
)
979 /* The reverse operation: build a symbolic expression with TYPE
980 from symbol SYM, negated according to NEG, and invariant INV. */
983 build_symbolic_expr (tree type
, tree sym
, bool neg
, tree inv
)
985 const bool pointer_p
= POINTER_TYPE_P (type
);
989 t
= build1 (NEGATE_EXPR
, type
, t
);
991 if (integer_zerop (inv
))
994 return build2 (pointer_p
? POINTER_PLUS_EXPR
: PLUS_EXPR
, type
, t
, inv
);
997 /* Return true if value range VR involves exactly one symbol SYM. */
1000 symbolic_range_based_on_p (value_range_t
*vr
, const_tree sym
)
1002 bool neg
, min_has_symbol
, max_has_symbol
;
1005 if (is_gimple_min_invariant (vr
->min
))
1006 min_has_symbol
= false;
1007 else if (get_single_symbol (vr
->min
, &neg
, &inv
) == sym
)
1008 min_has_symbol
= true;
1012 if (is_gimple_min_invariant (vr
->max
))
1013 max_has_symbol
= false;
1014 else if (get_single_symbol (vr
->max
, &neg
, &inv
) == sym
)
1015 max_has_symbol
= true;
1019 return (min_has_symbol
|| max_has_symbol
);
1022 /* Return true if value range VR uses an overflow infinity. */
1025 overflow_infinity_range_p (value_range_t
*vr
)
1027 return (vr
->type
== VR_RANGE
1028 && (is_overflow_infinity (vr
->min
)
1029 || is_overflow_infinity (vr
->max
)));
1032 /* Return false if we can not make a valid comparison based on VR;
1033 this will be the case if it uses an overflow infinity and overflow
1034 is not undefined (i.e., -fno-strict-overflow is in effect).
1035 Otherwise return true, and set *STRICT_OVERFLOW_P to true if VR
1036 uses an overflow infinity. */
1039 usable_range_p (value_range_t
*vr
, bool *strict_overflow_p
)
1041 gcc_assert (vr
->type
== VR_RANGE
);
1042 if (is_overflow_infinity (vr
->min
))
1044 *strict_overflow_p
= true;
1045 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (vr
->min
)))
1048 if (is_overflow_infinity (vr
->max
))
1050 *strict_overflow_p
= true;
1051 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (vr
->max
)))
1058 /* Return true if the result of assignment STMT is know to be non-negative.
1059 If the return value is based on the assumption that signed overflow is
1060 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1061 *STRICT_OVERFLOW_P.*/
1064 gimple_assign_nonnegative_warnv_p (gimple stmt
, bool *strict_overflow_p
)
1066 enum tree_code code
= gimple_assign_rhs_code (stmt
);
1067 switch (get_gimple_rhs_class (code
))
1069 case GIMPLE_UNARY_RHS
:
1070 return tree_unary_nonnegative_warnv_p (gimple_assign_rhs_code (stmt
),
1071 gimple_expr_type (stmt
),
1072 gimple_assign_rhs1 (stmt
),
1074 case GIMPLE_BINARY_RHS
:
1075 return tree_binary_nonnegative_warnv_p (gimple_assign_rhs_code (stmt
),
1076 gimple_expr_type (stmt
),
1077 gimple_assign_rhs1 (stmt
),
1078 gimple_assign_rhs2 (stmt
),
1080 case GIMPLE_TERNARY_RHS
:
1082 case GIMPLE_SINGLE_RHS
:
1083 return tree_single_nonnegative_warnv_p (gimple_assign_rhs1 (stmt
),
1085 case GIMPLE_INVALID_RHS
:
1092 /* Return true if return value of call STMT is know to be non-negative.
1093 If the return value is based on the assumption that signed overflow is
1094 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1095 *STRICT_OVERFLOW_P.*/
1098 gimple_call_nonnegative_warnv_p (gimple stmt
, bool *strict_overflow_p
)
1100 tree arg0
= gimple_call_num_args (stmt
) > 0 ?
1101 gimple_call_arg (stmt
, 0) : NULL_TREE
;
1102 tree arg1
= gimple_call_num_args (stmt
) > 1 ?
1103 gimple_call_arg (stmt
, 1) : NULL_TREE
;
1105 return tree_call_nonnegative_warnv_p (gimple_expr_type (stmt
),
1106 gimple_call_fndecl (stmt
),
1112 /* Return true if STMT is know to to compute a non-negative value.
1113 If the return value is based on the assumption that signed overflow is
1114 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1115 *STRICT_OVERFLOW_P.*/
1118 gimple_stmt_nonnegative_warnv_p (gimple stmt
, bool *strict_overflow_p
)
1120 switch (gimple_code (stmt
))
1123 return gimple_assign_nonnegative_warnv_p (stmt
, strict_overflow_p
);
1125 return gimple_call_nonnegative_warnv_p (stmt
, strict_overflow_p
);
1131 /* Return true if the result of assignment STMT is know to be non-zero.
1132 If the return value is based on the assumption that signed overflow is
1133 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1134 *STRICT_OVERFLOW_P.*/
1137 gimple_assign_nonzero_warnv_p (gimple stmt
, bool *strict_overflow_p
)
1139 enum tree_code code
= gimple_assign_rhs_code (stmt
);
1140 switch (get_gimple_rhs_class (code
))
1142 case GIMPLE_UNARY_RHS
:
1143 return tree_unary_nonzero_warnv_p (gimple_assign_rhs_code (stmt
),
1144 gimple_expr_type (stmt
),
1145 gimple_assign_rhs1 (stmt
),
1147 case GIMPLE_BINARY_RHS
:
1148 return tree_binary_nonzero_warnv_p (gimple_assign_rhs_code (stmt
),
1149 gimple_expr_type (stmt
),
1150 gimple_assign_rhs1 (stmt
),
1151 gimple_assign_rhs2 (stmt
),
1153 case GIMPLE_TERNARY_RHS
:
1155 case GIMPLE_SINGLE_RHS
:
1156 return tree_single_nonzero_warnv_p (gimple_assign_rhs1 (stmt
),
1158 case GIMPLE_INVALID_RHS
:
1165 /* Return true if STMT is known to compute a non-zero value.
1166 If the return value is based on the assumption that signed overflow is
1167 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1168 *STRICT_OVERFLOW_P.*/
1171 gimple_stmt_nonzero_warnv_p (gimple stmt
, bool *strict_overflow_p
)
1173 switch (gimple_code (stmt
))
1176 return gimple_assign_nonzero_warnv_p (stmt
, strict_overflow_p
);
1179 tree fndecl
= gimple_call_fndecl (stmt
);
1180 if (!fndecl
) return false;
1181 if (flag_delete_null_pointer_checks
&& !flag_check_new
1182 && DECL_IS_OPERATOR_NEW (fndecl
)
1183 && !TREE_NOTHROW (fndecl
))
1185 if (flag_delete_null_pointer_checks
&&
1186 lookup_attribute ("returns_nonnull",
1187 TYPE_ATTRIBUTES (gimple_call_fntype (stmt
))))
1189 return gimple_alloca_call_p (stmt
);
1196 /* Like tree_expr_nonzero_warnv_p, but this function uses value ranges
1200 vrp_stmt_computes_nonzero (gimple stmt
, bool *strict_overflow_p
)
1202 if (gimple_stmt_nonzero_warnv_p (stmt
, strict_overflow_p
))
1205 /* If we have an expression of the form &X->a, then the expression
1206 is nonnull if X is nonnull. */
1207 if (is_gimple_assign (stmt
)
1208 && gimple_assign_rhs_code (stmt
) == ADDR_EXPR
)
1210 tree expr
= gimple_assign_rhs1 (stmt
);
1211 tree base
= get_base_address (TREE_OPERAND (expr
, 0));
1213 if (base
!= NULL_TREE
1214 && TREE_CODE (base
) == MEM_REF
1215 && TREE_CODE (TREE_OPERAND (base
, 0)) == SSA_NAME
)
1217 value_range_t
*vr
= get_value_range (TREE_OPERAND (base
, 0));
1218 if (range_is_nonnull (vr
))
1226 /* Returns true if EXPR is a valid value (as expected by compare_values) --
1227 a gimple invariant, or SSA_NAME +- CST. */
1230 valid_value_p (tree expr
)
1232 if (TREE_CODE (expr
) == SSA_NAME
)
1235 if (TREE_CODE (expr
) == PLUS_EXPR
1236 || TREE_CODE (expr
) == MINUS_EXPR
)
1237 return (TREE_CODE (TREE_OPERAND (expr
, 0)) == SSA_NAME
1238 && TREE_CODE (TREE_OPERAND (expr
, 1)) == INTEGER_CST
);
1240 return is_gimple_min_invariant (expr
);
1246 -2 if those are incomparable. */
1248 operand_less_p (tree val
, tree val2
)
1250 /* LT is folded faster than GE and others. Inline the common case. */
1251 if (TREE_CODE (val
) == INTEGER_CST
&& TREE_CODE (val2
) == INTEGER_CST
)
1252 return tree_int_cst_lt (val
, val2
);
1257 fold_defer_overflow_warnings ();
1259 tcmp
= fold_binary_to_constant (LT_EXPR
, boolean_type_node
, val
, val2
);
1261 fold_undefer_and_ignore_overflow_warnings ();
1264 || TREE_CODE (tcmp
) != INTEGER_CST
)
1267 if (!integer_zerop (tcmp
))
1271 /* val >= val2, not considering overflow infinity. */
1272 if (is_negative_overflow_infinity (val
))
1273 return is_negative_overflow_infinity (val2
) ? 0 : 1;
1274 else if (is_positive_overflow_infinity (val2
))
1275 return is_positive_overflow_infinity (val
) ? 0 : 1;
1280 /* Compare two values VAL1 and VAL2. Return
1282 -2 if VAL1 and VAL2 cannot be compared at compile-time,
1285 +1 if VAL1 > VAL2, and
1288 This is similar to tree_int_cst_compare but supports pointer values
1289 and values that cannot be compared at compile time.
1291 If STRICT_OVERFLOW_P is not NULL, then set *STRICT_OVERFLOW_P to
1292 true if the return value is only valid if we assume that signed
1293 overflow is undefined. */
1296 compare_values_warnv (tree val1
, tree val2
, bool *strict_overflow_p
)
1301 /* Below we rely on the fact that VAL1 and VAL2 are both pointers or
1303 gcc_assert (POINTER_TYPE_P (TREE_TYPE (val1
))
1304 == POINTER_TYPE_P (TREE_TYPE (val2
)));
1306 /* Convert the two values into the same type. This is needed because
1307 sizetype causes sign extension even for unsigned types. */
1308 val2
= fold_convert (TREE_TYPE (val1
), val2
);
1309 STRIP_USELESS_TYPE_CONVERSION (val2
);
1311 if ((TREE_CODE (val1
) == SSA_NAME
1312 || (TREE_CODE (val1
) == NEGATE_EXPR
1313 && TREE_CODE (TREE_OPERAND (val1
, 0)) == SSA_NAME
)
1314 || TREE_CODE (val1
) == PLUS_EXPR
1315 || TREE_CODE (val1
) == MINUS_EXPR
)
1316 && (TREE_CODE (val2
) == SSA_NAME
1317 || (TREE_CODE (val2
) == NEGATE_EXPR
1318 && TREE_CODE (TREE_OPERAND (val2
, 0)) == SSA_NAME
)
1319 || TREE_CODE (val2
) == PLUS_EXPR
1320 || TREE_CODE (val2
) == MINUS_EXPR
))
1322 tree n1
, c1
, n2
, c2
;
1323 enum tree_code code1
, code2
;
1325 /* If VAL1 and VAL2 are of the form '[-]NAME [+-] CST' or 'NAME',
1326 return -1 or +1 accordingly. If VAL1 and VAL2 don't use the
1327 same name, return -2. */
1328 if (TREE_CODE (val1
) == SSA_NAME
|| TREE_CODE (val1
) == NEGATE_EXPR
)
1336 code1
= TREE_CODE (val1
);
1337 n1
= TREE_OPERAND (val1
, 0);
1338 c1
= TREE_OPERAND (val1
, 1);
1339 if (tree_int_cst_sgn (c1
) == -1)
1341 if (is_negative_overflow_infinity (c1
))
1343 c1
= fold_unary_to_constant (NEGATE_EXPR
, TREE_TYPE (c1
), c1
);
1346 code1
= code1
== MINUS_EXPR
? PLUS_EXPR
: MINUS_EXPR
;
1350 if (TREE_CODE (val2
) == SSA_NAME
|| TREE_CODE (val2
) == NEGATE_EXPR
)
1358 code2
= TREE_CODE (val2
);
1359 n2
= TREE_OPERAND (val2
, 0);
1360 c2
= TREE_OPERAND (val2
, 1);
1361 if (tree_int_cst_sgn (c2
) == -1)
1363 if (is_negative_overflow_infinity (c2
))
1365 c2
= fold_unary_to_constant (NEGATE_EXPR
, TREE_TYPE (c2
), c2
);
1368 code2
= code2
== MINUS_EXPR
? PLUS_EXPR
: MINUS_EXPR
;
1372 /* Both values must use the same name. */
1373 if (TREE_CODE (n1
) == NEGATE_EXPR
&& TREE_CODE (n2
) == NEGATE_EXPR
)
1375 n1
= TREE_OPERAND (n1
, 0);
1376 n2
= TREE_OPERAND (n2
, 0);
1381 if (code1
== SSA_NAME
&& code2
== SSA_NAME
)
1385 /* If overflow is defined we cannot simplify more. */
1386 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (val1
)))
1389 if (strict_overflow_p
!= NULL
1390 && (code1
== SSA_NAME
|| !TREE_NO_WARNING (val1
))
1391 && (code2
== SSA_NAME
|| !TREE_NO_WARNING (val2
)))
1392 *strict_overflow_p
= true;
1394 if (code1
== SSA_NAME
)
1396 if (code2
== PLUS_EXPR
)
1397 /* NAME < NAME + CST */
1399 else if (code2
== MINUS_EXPR
)
1400 /* NAME > NAME - CST */
1403 else if (code1
== PLUS_EXPR
)
1405 if (code2
== SSA_NAME
)
1406 /* NAME + CST > NAME */
1408 else if (code2
== PLUS_EXPR
)
1409 /* NAME + CST1 > NAME + CST2, if CST1 > CST2 */
1410 return compare_values_warnv (c1
, c2
, strict_overflow_p
);
1411 else if (code2
== MINUS_EXPR
)
1412 /* NAME + CST1 > NAME - CST2 */
1415 else if (code1
== MINUS_EXPR
)
1417 if (code2
== SSA_NAME
)
1418 /* NAME - CST < NAME */
1420 else if (code2
== PLUS_EXPR
)
1421 /* NAME - CST1 < NAME + CST2 */
1423 else if (code2
== MINUS_EXPR
)
1424 /* NAME - CST1 > NAME - CST2, if CST1 < CST2. Notice that
1425 C1 and C2 are swapped in the call to compare_values. */
1426 return compare_values_warnv (c2
, c1
, strict_overflow_p
);
1432 /* We cannot compare non-constants. */
1433 if (!is_gimple_min_invariant (val1
) || !is_gimple_min_invariant (val2
))
1436 if (!POINTER_TYPE_P (TREE_TYPE (val1
)))
1438 /* We cannot compare overflowed values, except for overflow
1440 if (TREE_OVERFLOW (val1
) || TREE_OVERFLOW (val2
))
1442 if (strict_overflow_p
!= NULL
)
1443 *strict_overflow_p
= true;
1444 if (is_negative_overflow_infinity (val1
))
1445 return is_negative_overflow_infinity (val2
) ? 0 : -1;
1446 else if (is_negative_overflow_infinity (val2
))
1448 else if (is_positive_overflow_infinity (val1
))
1449 return is_positive_overflow_infinity (val2
) ? 0 : 1;
1450 else if (is_positive_overflow_infinity (val2
))
1455 return tree_int_cst_compare (val1
, val2
);
1461 /* First see if VAL1 and VAL2 are not the same. */
1462 if (val1
== val2
|| operand_equal_p (val1
, val2
, 0))
1465 /* If VAL1 is a lower address than VAL2, return -1. */
1466 if (operand_less_p (val1
, val2
) == 1)
1469 /* If VAL1 is a higher address than VAL2, return +1. */
1470 if (operand_less_p (val2
, val1
) == 1)
1473 /* If VAL1 is different than VAL2, return +2.
1474 For integer constants we either have already returned -1 or 1
1475 or they are equivalent. We still might succeed in proving
1476 something about non-trivial operands. */
1477 if (TREE_CODE (val1
) != INTEGER_CST
1478 || TREE_CODE (val2
) != INTEGER_CST
)
1480 t
= fold_binary_to_constant (NE_EXPR
, boolean_type_node
, val1
, val2
);
1481 if (t
&& integer_onep (t
))
1489 /* Compare values like compare_values_warnv, but treat comparisons of
1490 nonconstants which rely on undefined overflow as incomparable. */
1493 compare_values (tree val1
, tree val2
)
1499 ret
= compare_values_warnv (val1
, val2
, &sop
);
1501 && (!is_gimple_min_invariant (val1
) || !is_gimple_min_invariant (val2
)))
1507 /* Return 1 if VAL is inside value range MIN <= VAL <= MAX,
1508 0 if VAL is not inside [MIN, MAX],
1509 -2 if we cannot tell either way.
1511 Benchmark compile/20001226-1.c compilation time after changing this
1515 value_inside_range (tree val
, tree min
, tree max
)
1519 cmp1
= operand_less_p (val
, min
);
1525 cmp2
= operand_less_p (max
, val
);
1533 /* Return true if value ranges VR0 and VR1 have a non-empty
1536 Benchmark compile/20001226-1.c compilation time after changing this
1541 value_ranges_intersect_p (value_range_t
*vr0
, value_range_t
*vr1
)
1543 /* The value ranges do not intersect if the maximum of the first range is
1544 less than the minimum of the second range or vice versa.
1545 When those relations are unknown, we can't do any better. */
1546 if (operand_less_p (vr0
->max
, vr1
->min
) != 0)
1548 if (operand_less_p (vr1
->max
, vr0
->min
) != 0)
1554 /* Return 1 if [MIN, MAX] includes the value zero, 0 if it does not
1555 include the value zero, -2 if we cannot tell. */
1558 range_includes_zero_p (tree min
, tree max
)
1560 tree zero
= build_int_cst (TREE_TYPE (min
), 0);
1561 return value_inside_range (zero
, min
, max
);
1564 /* Return true if *VR is know to only contain nonnegative values. */
1567 value_range_nonnegative_p (value_range_t
*vr
)
1569 /* Testing for VR_ANTI_RANGE is not useful here as any anti-range
1570 which would return a useful value should be encoded as a
1572 if (vr
->type
== VR_RANGE
)
1574 int result
= compare_values (vr
->min
, integer_zero_node
);
1575 return (result
== 0 || result
== 1);
1581 /* If *VR has a value rante that is a single constant value return that,
1582 otherwise return NULL_TREE. */
1585 value_range_constant_singleton (value_range_t
*vr
)
1587 if (vr
->type
== VR_RANGE
1588 && operand_equal_p (vr
->min
, vr
->max
, 0)
1589 && is_gimple_min_invariant (vr
->min
))
1595 /* If OP has a value range with a single constant value return that,
1596 otherwise return NULL_TREE. This returns OP itself if OP is a
1600 op_with_constant_singleton_value_range (tree op
)
1602 if (is_gimple_min_invariant (op
))
1605 if (TREE_CODE (op
) != SSA_NAME
)
1608 return value_range_constant_singleton (get_value_range (op
));
1611 /* Return true if op is in a boolean [0, 1] value-range. */
1614 op_with_boolean_value_range_p (tree op
)
1618 if (TYPE_PRECISION (TREE_TYPE (op
)) == 1)
1621 if (integer_zerop (op
)
1622 || integer_onep (op
))
1625 if (TREE_CODE (op
) != SSA_NAME
)
1628 vr
= get_value_range (op
);
1629 return (vr
->type
== VR_RANGE
1630 && integer_zerop (vr
->min
)
1631 && integer_onep (vr
->max
));
1634 /* Extract value range information from an ASSERT_EXPR EXPR and store
1638 extract_range_from_assert (value_range_t
*vr_p
, tree expr
)
1640 tree var
, cond
, limit
, min
, max
, type
;
1641 value_range_t
*limit_vr
;
1642 enum tree_code cond_code
;
1644 var
= ASSERT_EXPR_VAR (expr
);
1645 cond
= ASSERT_EXPR_COND (expr
);
1647 gcc_assert (COMPARISON_CLASS_P (cond
));
1649 /* Find VAR in the ASSERT_EXPR conditional. */
1650 if (var
== TREE_OPERAND (cond
, 0)
1651 || TREE_CODE (TREE_OPERAND (cond
, 0)) == PLUS_EXPR
1652 || TREE_CODE (TREE_OPERAND (cond
, 0)) == NOP_EXPR
)
1654 /* If the predicate is of the form VAR COMP LIMIT, then we just
1655 take LIMIT from the RHS and use the same comparison code. */
1656 cond_code
= TREE_CODE (cond
);
1657 limit
= TREE_OPERAND (cond
, 1);
1658 cond
= TREE_OPERAND (cond
, 0);
1662 /* If the predicate is of the form LIMIT COMP VAR, then we need
1663 to flip around the comparison code to create the proper range
1665 cond_code
= swap_tree_comparison (TREE_CODE (cond
));
1666 limit
= TREE_OPERAND (cond
, 0);
1667 cond
= TREE_OPERAND (cond
, 1);
1670 limit
= avoid_overflow_infinity (limit
);
1672 type
= TREE_TYPE (var
);
1673 gcc_assert (limit
!= var
);
1675 /* For pointer arithmetic, we only keep track of pointer equality
1677 if (POINTER_TYPE_P (type
) && cond_code
!= NE_EXPR
&& cond_code
!= EQ_EXPR
)
1679 set_value_range_to_varying (vr_p
);
1683 /* If LIMIT is another SSA name and LIMIT has a range of its own,
1684 try to use LIMIT's range to avoid creating symbolic ranges
1686 limit_vr
= (TREE_CODE (limit
) == SSA_NAME
) ? get_value_range (limit
) : NULL
;
1688 /* LIMIT's range is only interesting if it has any useful information. */
1690 && (limit_vr
->type
== VR_UNDEFINED
1691 || limit_vr
->type
== VR_VARYING
1692 || symbolic_range_p (limit_vr
)))
1695 /* Initially, the new range has the same set of equivalences of
1696 VAR's range. This will be revised before returning the final
1697 value. Since assertions may be chained via mutually exclusive
1698 predicates, we will need to trim the set of equivalences before
1700 gcc_assert (vr_p
->equiv
== NULL
);
1701 add_equivalence (&vr_p
->equiv
, var
);
1703 /* Extract a new range based on the asserted comparison for VAR and
1704 LIMIT's value range. Notice that if LIMIT has an anti-range, we
1705 will only use it for equality comparisons (EQ_EXPR). For any
1706 other kind of assertion, we cannot derive a range from LIMIT's
1707 anti-range that can be used to describe the new range. For
1708 instance, ASSERT_EXPR <x_2, x_2 <= b_4>. If b_4 is ~[2, 10],
1709 then b_4 takes on the ranges [-INF, 1] and [11, +INF]. There is
1710 no single range for x_2 that could describe LE_EXPR, so we might
1711 as well build the range [b_4, +INF] for it.
1712 One special case we handle is extracting a range from a
1713 range test encoded as (unsigned)var + CST <= limit. */
1714 if (TREE_CODE (cond
) == NOP_EXPR
1715 || TREE_CODE (cond
) == PLUS_EXPR
)
1717 if (TREE_CODE (cond
) == PLUS_EXPR
)
1719 min
= fold_build1 (NEGATE_EXPR
, TREE_TYPE (TREE_OPERAND (cond
, 1)),
1720 TREE_OPERAND (cond
, 1));
1721 max
= int_const_binop (PLUS_EXPR
, limit
, min
);
1722 cond
= TREE_OPERAND (cond
, 0);
1726 min
= build_int_cst (TREE_TYPE (var
), 0);
1730 /* Make sure to not set TREE_OVERFLOW on the final type
1731 conversion. We are willingly interpreting large positive
1732 unsigned values as negative signed values here. */
1733 min
= force_fit_type (TREE_TYPE (var
), wi::to_widest (min
), 0, false);
1734 max
= force_fit_type (TREE_TYPE (var
), wi::to_widest (max
), 0, false);
1736 /* We can transform a max, min range to an anti-range or
1737 vice-versa. Use set_and_canonicalize_value_range which does
1739 if (cond_code
== LE_EXPR
)
1740 set_and_canonicalize_value_range (vr_p
, VR_RANGE
,
1741 min
, max
, vr_p
->equiv
);
1742 else if (cond_code
== GT_EXPR
)
1743 set_and_canonicalize_value_range (vr_p
, VR_ANTI_RANGE
,
1744 min
, max
, vr_p
->equiv
);
1748 else if (cond_code
== EQ_EXPR
)
1750 enum value_range_type range_type
;
1754 range_type
= limit_vr
->type
;
1755 min
= limit_vr
->min
;
1756 max
= limit_vr
->max
;
1760 range_type
= VR_RANGE
;
1765 set_value_range (vr_p
, range_type
, min
, max
, vr_p
->equiv
);
1767 /* When asserting the equality VAR == LIMIT and LIMIT is another
1768 SSA name, the new range will also inherit the equivalence set
1770 if (TREE_CODE (limit
) == SSA_NAME
)
1771 add_equivalence (&vr_p
->equiv
, limit
);
1773 else if (cond_code
== NE_EXPR
)
1775 /* As described above, when LIMIT's range is an anti-range and
1776 this assertion is an inequality (NE_EXPR), then we cannot
1777 derive anything from the anti-range. For instance, if
1778 LIMIT's range was ~[0, 0], the assertion 'VAR != LIMIT' does
1779 not imply that VAR's range is [0, 0]. So, in the case of
1780 anti-ranges, we just assert the inequality using LIMIT and
1783 If LIMIT_VR is a range, we can only use it to build a new
1784 anti-range if LIMIT_VR is a single-valued range. For
1785 instance, if LIMIT_VR is [0, 1], the predicate
1786 VAR != [0, 1] does not mean that VAR's range is ~[0, 1].
1787 Rather, it means that for value 0 VAR should be ~[0, 0]
1788 and for value 1, VAR should be ~[1, 1]. We cannot
1789 represent these ranges.
1791 The only situation in which we can build a valid
1792 anti-range is when LIMIT_VR is a single-valued range
1793 (i.e., LIMIT_VR->MIN == LIMIT_VR->MAX). In that case,
1794 build the anti-range ~[LIMIT_VR->MIN, LIMIT_VR->MAX]. */
1796 && limit_vr
->type
== VR_RANGE
1797 && compare_values (limit_vr
->min
, limit_vr
->max
) == 0)
1799 min
= limit_vr
->min
;
1800 max
= limit_vr
->max
;
1804 /* In any other case, we cannot use LIMIT's range to build a
1805 valid anti-range. */
1809 /* If MIN and MAX cover the whole range for their type, then
1810 just use the original LIMIT. */
1811 if (INTEGRAL_TYPE_P (type
)
1812 && vrp_val_is_min (min
)
1813 && vrp_val_is_max (max
))
1816 set_and_canonicalize_value_range (vr_p
, VR_ANTI_RANGE
,
1817 min
, max
, vr_p
->equiv
);
1819 else if (cond_code
== LE_EXPR
|| cond_code
== LT_EXPR
)
1821 min
= TYPE_MIN_VALUE (type
);
1823 if (limit_vr
== NULL
|| limit_vr
->type
== VR_ANTI_RANGE
)
1827 /* If LIMIT_VR is of the form [N1, N2], we need to build the
1828 range [MIN, N2] for LE_EXPR and [MIN, N2 - 1] for
1830 max
= limit_vr
->max
;
1833 /* If the maximum value forces us to be out of bounds, simply punt.
1834 It would be pointless to try and do anything more since this
1835 all should be optimized away above us. */
1836 if ((cond_code
== LT_EXPR
1837 && compare_values (max
, min
) == 0)
1838 || is_overflow_infinity (max
))
1839 set_value_range_to_varying (vr_p
);
1842 /* For LT_EXPR, we create the range [MIN, MAX - 1]. */
1843 if (cond_code
== LT_EXPR
)
1845 if (TYPE_PRECISION (TREE_TYPE (max
)) == 1
1846 && !TYPE_UNSIGNED (TREE_TYPE (max
)))
1847 max
= fold_build2 (PLUS_EXPR
, TREE_TYPE (max
), max
,
1848 build_int_cst (TREE_TYPE (max
), -1));
1850 max
= fold_build2 (MINUS_EXPR
, TREE_TYPE (max
), max
,
1851 build_int_cst (TREE_TYPE (max
), 1));
1853 TREE_NO_WARNING (max
) = 1;
1856 set_value_range (vr_p
, VR_RANGE
, min
, max
, vr_p
->equiv
);
1859 else if (cond_code
== GE_EXPR
|| cond_code
== GT_EXPR
)
1861 max
= TYPE_MAX_VALUE (type
);
1863 if (limit_vr
== NULL
|| limit_vr
->type
== VR_ANTI_RANGE
)
1867 /* If LIMIT_VR is of the form [N1, N2], we need to build the
1868 range [N1, MAX] for GE_EXPR and [N1 + 1, MAX] for
1870 min
= limit_vr
->min
;
1873 /* If the minimum value forces us to be out of bounds, simply punt.
1874 It would be pointless to try and do anything more since this
1875 all should be optimized away above us. */
1876 if ((cond_code
== GT_EXPR
1877 && compare_values (min
, max
) == 0)
1878 || is_overflow_infinity (min
))
1879 set_value_range_to_varying (vr_p
);
1882 /* For GT_EXPR, we create the range [MIN + 1, MAX]. */
1883 if (cond_code
== GT_EXPR
)
1885 if (TYPE_PRECISION (TREE_TYPE (min
)) == 1
1886 && !TYPE_UNSIGNED (TREE_TYPE (min
)))
1887 min
= fold_build2 (MINUS_EXPR
, TREE_TYPE (min
), min
,
1888 build_int_cst (TREE_TYPE (min
), -1));
1890 min
= fold_build2 (PLUS_EXPR
, TREE_TYPE (min
), min
,
1891 build_int_cst (TREE_TYPE (min
), 1));
1893 TREE_NO_WARNING (min
) = 1;
1896 set_value_range (vr_p
, VR_RANGE
, min
, max
, vr_p
->equiv
);
1902 /* Finally intersect the new range with what we already know about var. */
1903 vrp_intersect_ranges (vr_p
, get_value_range (var
));
1907 /* Extract range information from SSA name VAR and store it in VR. If
1908 VAR has an interesting range, use it. Otherwise, create the
1909 range [VAR, VAR] and return it. This is useful in situations where
1910 we may have conditionals testing values of VARYING names. For
1917 Even if y_5 is deemed VARYING, we can determine that x_3 > y_5 is
1921 extract_range_from_ssa_name (value_range_t
*vr
, tree var
)
1923 value_range_t
*var_vr
= get_value_range (var
);
1925 if (var_vr
->type
!= VR_VARYING
)
1926 copy_value_range (vr
, var_vr
);
1928 set_value_range (vr
, VR_RANGE
, var
, var
, NULL
);
1930 add_equivalence (&vr
->equiv
, var
);
1934 /* Wrapper around int_const_binop. If the operation overflows and we
1935 are not using wrapping arithmetic, then adjust the result to be
1936 -INF or +INF depending on CODE, VAL1 and VAL2. This can return
1937 NULL_TREE if we need to use an overflow infinity representation but
1938 the type does not support it. */
1941 vrp_int_const_binop (enum tree_code code
, tree val1
, tree val2
)
1945 res
= int_const_binop (code
, val1
, val2
);
1947 /* If we are using unsigned arithmetic, operate symbolically
1948 on -INF and +INF as int_const_binop only handles signed overflow. */
1949 if (TYPE_UNSIGNED (TREE_TYPE (val1
)))
1951 int checkz
= compare_values (res
, val1
);
1952 bool overflow
= false;
1954 /* Ensure that res = val1 [+*] val2 >= val1
1955 or that res = val1 - val2 <= val1. */
1956 if ((code
== PLUS_EXPR
1957 && !(checkz
== 1 || checkz
== 0))
1958 || (code
== MINUS_EXPR
1959 && !(checkz
== 0 || checkz
== -1)))
1963 /* Checking for multiplication overflow is done by dividing the
1964 output of the multiplication by the first input of the
1965 multiplication. If the result of that division operation is
1966 not equal to the second input of the multiplication, then the
1967 multiplication overflowed. */
1968 else if (code
== MULT_EXPR
&& !integer_zerop (val1
))
1970 tree tmp
= int_const_binop (TRUNC_DIV_EXPR
,
1973 int check
= compare_values (tmp
, val2
);
1981 res
= copy_node (res
);
1982 TREE_OVERFLOW (res
) = 1;
1986 else if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (val1
)))
1987 /* If the singed operation wraps then int_const_binop has done
1988 everything we want. */
1990 /* Signed division of -1/0 overflows and by the time it gets here
1991 returns NULL_TREE. */
1994 else if ((TREE_OVERFLOW (res
)
1995 && !TREE_OVERFLOW (val1
)
1996 && !TREE_OVERFLOW (val2
))
1997 || is_overflow_infinity (val1
)
1998 || is_overflow_infinity (val2
))
2000 /* If the operation overflowed but neither VAL1 nor VAL2 are
2001 overflown, return -INF or +INF depending on the operation
2002 and the combination of signs of the operands. */
2003 int sgn1
= tree_int_cst_sgn (val1
);
2004 int sgn2
= tree_int_cst_sgn (val2
);
2006 if (needs_overflow_infinity (TREE_TYPE (res
))
2007 && !supports_overflow_infinity (TREE_TYPE (res
)))
2010 /* We have to punt on adding infinities of different signs,
2011 since we can't tell what the sign of the result should be.
2012 Likewise for subtracting infinities of the same sign. */
2013 if (((code
== PLUS_EXPR
&& sgn1
!= sgn2
)
2014 || (code
== MINUS_EXPR
&& sgn1
== sgn2
))
2015 && is_overflow_infinity (val1
)
2016 && is_overflow_infinity (val2
))
2019 /* Don't try to handle division or shifting of infinities. */
2020 if ((code
== TRUNC_DIV_EXPR
2021 || code
== FLOOR_DIV_EXPR
2022 || code
== CEIL_DIV_EXPR
2023 || code
== EXACT_DIV_EXPR
2024 || code
== ROUND_DIV_EXPR
2025 || code
== RSHIFT_EXPR
)
2026 && (is_overflow_infinity (val1
)
2027 || is_overflow_infinity (val2
)))
2030 /* Notice that we only need to handle the restricted set of
2031 operations handled by extract_range_from_binary_expr.
2032 Among them, only multiplication, addition and subtraction
2033 can yield overflow without overflown operands because we
2034 are working with integral types only... except in the
2035 case VAL1 = -INF and VAL2 = -1 which overflows to +INF
2036 for division too. */
2038 /* For multiplication, the sign of the overflow is given
2039 by the comparison of the signs of the operands. */
2040 if ((code
== MULT_EXPR
&& sgn1
== sgn2
)
2041 /* For addition, the operands must be of the same sign
2042 to yield an overflow. Its sign is therefore that
2043 of one of the operands, for example the first. For
2044 infinite operands X + -INF is negative, not positive. */
2045 || (code
== PLUS_EXPR
2047 ? !is_negative_overflow_infinity (val2
)
2048 : is_positive_overflow_infinity (val2
)))
2049 /* For subtraction, non-infinite operands must be of
2050 different signs to yield an overflow. Its sign is
2051 therefore that of the first operand or the opposite of
2052 that of the second operand. A first operand of 0 counts
2053 as positive here, for the corner case 0 - (-INF), which
2054 overflows, but must yield +INF. For infinite operands 0
2055 - INF is negative, not positive. */
2056 || (code
== MINUS_EXPR
2058 ? !is_positive_overflow_infinity (val2
)
2059 : is_negative_overflow_infinity (val2
)))
2060 /* We only get in here with positive shift count, so the
2061 overflow direction is the same as the sign of val1.
2062 Actually rshift does not overflow at all, but we only
2063 handle the case of shifting overflowed -INF and +INF. */
2064 || (code
== RSHIFT_EXPR
2066 /* For division, the only case is -INF / -1 = +INF. */
2067 || code
== TRUNC_DIV_EXPR
2068 || code
== FLOOR_DIV_EXPR
2069 || code
== CEIL_DIV_EXPR
2070 || code
== EXACT_DIV_EXPR
2071 || code
== ROUND_DIV_EXPR
)
2072 return (needs_overflow_infinity (TREE_TYPE (res
))
2073 ? positive_overflow_infinity (TREE_TYPE (res
))
2074 : TYPE_MAX_VALUE (TREE_TYPE (res
)));
2076 return (needs_overflow_infinity (TREE_TYPE (res
))
2077 ? negative_overflow_infinity (TREE_TYPE (res
))
2078 : TYPE_MIN_VALUE (TREE_TYPE (res
)));
2085 /* For range VR compute two wide_int bitmasks. In *MAY_BE_NONZERO
2086 bitmask if some bit is unset, it means for all numbers in the range
2087 the bit is 0, otherwise it might be 0 or 1. In *MUST_BE_NONZERO
2088 bitmask if some bit is set, it means for all numbers in the range
2089 the bit is 1, otherwise it might be 0 or 1. */
2092 zero_nonzero_bits_from_vr (const tree expr_type
,
2094 wide_int
*may_be_nonzero
,
2095 wide_int
*must_be_nonzero
)
2097 *may_be_nonzero
= wi::minus_one (TYPE_PRECISION (expr_type
));
2098 *must_be_nonzero
= wi::zero (TYPE_PRECISION (expr_type
));
2099 if (!range_int_cst_p (vr
)
2100 || is_overflow_infinity (vr
->min
)
2101 || is_overflow_infinity (vr
->max
))
2104 if (range_int_cst_singleton_p (vr
))
2106 *may_be_nonzero
= vr
->min
;
2107 *must_be_nonzero
= *may_be_nonzero
;
2109 else if (tree_int_cst_sgn (vr
->min
) >= 0
2110 || tree_int_cst_sgn (vr
->max
) < 0)
2112 wide_int xor_mask
= wi::bit_xor (vr
->min
, vr
->max
);
2113 *may_be_nonzero
= wi::bit_or (vr
->min
, vr
->max
);
2114 *must_be_nonzero
= wi::bit_and (vr
->min
, vr
->max
);
2117 wide_int mask
= wi::mask (wi::floor_log2 (xor_mask
), false,
2118 may_be_nonzero
->get_precision ());
2119 *may_be_nonzero
= *may_be_nonzero
| mask
;
2120 *must_be_nonzero
= must_be_nonzero
->and_not (mask
);
2127 /* Create two value-ranges in *VR0 and *VR1 from the anti-range *AR
2128 so that *VR0 U *VR1 == *AR. Returns true if that is possible,
2129 false otherwise. If *AR can be represented with a single range
2130 *VR1 will be VR_UNDEFINED. */
2133 ranges_from_anti_range (value_range_t
*ar
,
2134 value_range_t
*vr0
, value_range_t
*vr1
)
2136 tree type
= TREE_TYPE (ar
->min
);
2138 vr0
->type
= VR_UNDEFINED
;
2139 vr1
->type
= VR_UNDEFINED
;
2141 if (ar
->type
!= VR_ANTI_RANGE
2142 || TREE_CODE (ar
->min
) != INTEGER_CST
2143 || TREE_CODE (ar
->max
) != INTEGER_CST
2144 || !vrp_val_min (type
)
2145 || !vrp_val_max (type
))
2148 if (!vrp_val_is_min (ar
->min
))
2150 vr0
->type
= VR_RANGE
;
2151 vr0
->min
= vrp_val_min (type
);
2152 vr0
->max
= wide_int_to_tree (type
, wi::sub (ar
->min
, 1));
2154 if (!vrp_val_is_max (ar
->max
))
2156 vr1
->type
= VR_RANGE
;
2157 vr1
->min
= wide_int_to_tree (type
, wi::add (ar
->max
, 1));
2158 vr1
->max
= vrp_val_max (type
);
2160 if (vr0
->type
== VR_UNDEFINED
)
2163 vr1
->type
= VR_UNDEFINED
;
2166 return vr0
->type
!= VR_UNDEFINED
;
2169 /* Helper to extract a value-range *VR for a multiplicative operation
2173 extract_range_from_multiplicative_op_1 (value_range_t
*vr
,
2174 enum tree_code code
,
2175 value_range_t
*vr0
, value_range_t
*vr1
)
2177 enum value_range_type type
;
2184 /* Multiplications, divisions and shifts are a bit tricky to handle,
2185 depending on the mix of signs we have in the two ranges, we
2186 need to operate on different values to get the minimum and
2187 maximum values for the new range. One approach is to figure
2188 out all the variations of range combinations and do the
2191 However, this involves several calls to compare_values and it
2192 is pretty convoluted. It's simpler to do the 4 operations
2193 (MIN0 OP MIN1, MIN0 OP MAX1, MAX0 OP MIN1 and MAX0 OP MAX0 OP
2194 MAX1) and then figure the smallest and largest values to form
2196 gcc_assert (code
== MULT_EXPR
2197 || code
== TRUNC_DIV_EXPR
2198 || code
== FLOOR_DIV_EXPR
2199 || code
== CEIL_DIV_EXPR
2200 || code
== EXACT_DIV_EXPR
2201 || code
== ROUND_DIV_EXPR
2202 || code
== RSHIFT_EXPR
2203 || code
== LSHIFT_EXPR
);
2204 gcc_assert ((vr0
->type
== VR_RANGE
2205 || (code
== MULT_EXPR
&& vr0
->type
== VR_ANTI_RANGE
))
2206 && vr0
->type
== vr1
->type
);
2210 /* Compute the 4 cross operations. */
2212 val
[0] = vrp_int_const_binop (code
, vr0
->min
, vr1
->min
);
2213 if (val
[0] == NULL_TREE
)
2216 if (vr1
->max
== vr1
->min
)
2220 val
[1] = vrp_int_const_binop (code
, vr0
->min
, vr1
->max
);
2221 if (val
[1] == NULL_TREE
)
2225 if (vr0
->max
== vr0
->min
)
2229 val
[2] = vrp_int_const_binop (code
, vr0
->max
, vr1
->min
);
2230 if (val
[2] == NULL_TREE
)
2234 if (vr0
->min
== vr0
->max
|| vr1
->min
== vr1
->max
)
2238 val
[3] = vrp_int_const_binop (code
, vr0
->max
, vr1
->max
);
2239 if (val
[3] == NULL_TREE
)
2245 set_value_range_to_varying (vr
);
2249 /* Set MIN to the minimum of VAL[i] and MAX to the maximum
2253 for (i
= 1; i
< 4; i
++)
2255 if (!is_gimple_min_invariant (min
)
2256 || (TREE_OVERFLOW (min
) && !is_overflow_infinity (min
))
2257 || !is_gimple_min_invariant (max
)
2258 || (TREE_OVERFLOW (max
) && !is_overflow_infinity (max
)))
2263 if (!is_gimple_min_invariant (val
[i
])
2264 || (TREE_OVERFLOW (val
[i
])
2265 && !is_overflow_infinity (val
[i
])))
2267 /* If we found an overflowed value, set MIN and MAX
2268 to it so that we set the resulting range to
2274 if (compare_values (val
[i
], min
) == -1)
2277 if (compare_values (val
[i
], max
) == 1)
2282 /* If either MIN or MAX overflowed, then set the resulting range to
2283 VARYING. But we do accept an overflow infinity
2285 if (min
== NULL_TREE
2286 || !is_gimple_min_invariant (min
)
2287 || (TREE_OVERFLOW (min
) && !is_overflow_infinity (min
))
2289 || !is_gimple_min_invariant (max
)
2290 || (TREE_OVERFLOW (max
) && !is_overflow_infinity (max
)))
2292 set_value_range_to_varying (vr
);
2298 2) [-INF, +-INF(OVF)]
2299 3) [+-INF(OVF), +INF]
2300 4) [+-INF(OVF), +-INF(OVF)]
2301 We learn nothing when we have INF and INF(OVF) on both sides.
2302 Note that we do accept [-INF, -INF] and [+INF, +INF] without
2304 if ((vrp_val_is_min (min
) || is_overflow_infinity (min
))
2305 && (vrp_val_is_max (max
) || is_overflow_infinity (max
)))
2307 set_value_range_to_varying (vr
);
2311 cmp
= compare_values (min
, max
);
2312 if (cmp
== -2 || cmp
== 1)
2314 /* If the new range has its limits swapped around (MIN > MAX),
2315 then the operation caused one of them to wrap around, mark
2316 the new range VARYING. */
2317 set_value_range_to_varying (vr
);
2320 set_value_range (vr
, type
, min
, max
, NULL
);
2323 /* Extract range information from a binary operation CODE based on
2324 the ranges of each of its operands *VR0 and *VR1 with resulting
2325 type EXPR_TYPE. The resulting range is stored in *VR. */
2328 extract_range_from_binary_expr_1 (value_range_t
*vr
,
2329 enum tree_code code
, tree expr_type
,
2330 value_range_t
*vr0_
, value_range_t
*vr1_
)
2332 value_range_t vr0
= *vr0_
, vr1
= *vr1_
;
2333 value_range_t vrtem0
= VR_INITIALIZER
, vrtem1
= VR_INITIALIZER
;
2334 enum value_range_type type
;
2335 tree min
= NULL_TREE
, max
= NULL_TREE
;
2338 if (!INTEGRAL_TYPE_P (expr_type
)
2339 && !POINTER_TYPE_P (expr_type
))
2341 set_value_range_to_varying (vr
);
2345 /* Not all binary expressions can be applied to ranges in a
2346 meaningful way. Handle only arithmetic operations. */
2347 if (code
!= PLUS_EXPR
2348 && code
!= MINUS_EXPR
2349 && code
!= POINTER_PLUS_EXPR
2350 && code
!= MULT_EXPR
2351 && code
!= TRUNC_DIV_EXPR
2352 && code
!= FLOOR_DIV_EXPR
2353 && code
!= CEIL_DIV_EXPR
2354 && code
!= EXACT_DIV_EXPR
2355 && code
!= ROUND_DIV_EXPR
2356 && code
!= TRUNC_MOD_EXPR
2357 && code
!= RSHIFT_EXPR
2358 && code
!= LSHIFT_EXPR
2361 && code
!= BIT_AND_EXPR
2362 && code
!= BIT_IOR_EXPR
2363 && code
!= BIT_XOR_EXPR
)
2365 set_value_range_to_varying (vr
);
2369 /* If both ranges are UNDEFINED, so is the result. */
2370 if (vr0
.type
== VR_UNDEFINED
&& vr1
.type
== VR_UNDEFINED
)
2372 set_value_range_to_undefined (vr
);
2375 /* If one of the ranges is UNDEFINED drop it to VARYING for the following
2376 code. At some point we may want to special-case operations that
2377 have UNDEFINED result for all or some value-ranges of the not UNDEFINED
2379 else if (vr0
.type
== VR_UNDEFINED
)
2380 set_value_range_to_varying (&vr0
);
2381 else if (vr1
.type
== VR_UNDEFINED
)
2382 set_value_range_to_varying (&vr1
);
2384 /* Now canonicalize anti-ranges to ranges when they are not symbolic
2385 and express ~[] op X as ([]' op X) U ([]'' op X). */
2386 if (vr0
.type
== VR_ANTI_RANGE
2387 && ranges_from_anti_range (&vr0
, &vrtem0
, &vrtem1
))
2389 extract_range_from_binary_expr_1 (vr
, code
, expr_type
, &vrtem0
, vr1_
);
2390 if (vrtem1
.type
!= VR_UNDEFINED
)
2392 value_range_t vrres
= VR_INITIALIZER
;
2393 extract_range_from_binary_expr_1 (&vrres
, code
, expr_type
,
2395 vrp_meet (vr
, &vrres
);
2399 /* Likewise for X op ~[]. */
2400 if (vr1
.type
== VR_ANTI_RANGE
2401 && ranges_from_anti_range (&vr1
, &vrtem0
, &vrtem1
))
2403 extract_range_from_binary_expr_1 (vr
, code
, expr_type
, vr0_
, &vrtem0
);
2404 if (vrtem1
.type
!= VR_UNDEFINED
)
2406 value_range_t vrres
= VR_INITIALIZER
;
2407 extract_range_from_binary_expr_1 (&vrres
, code
, expr_type
,
2409 vrp_meet (vr
, &vrres
);
2414 /* The type of the resulting value range defaults to VR0.TYPE. */
2417 /* Refuse to operate on VARYING ranges, ranges of different kinds
2418 and symbolic ranges. As an exception, we allow BIT_{AND,IOR}
2419 because we may be able to derive a useful range even if one of
2420 the operands is VR_VARYING or symbolic range. Similarly for
2421 divisions, MIN/MAX and PLUS/MINUS.
2423 TODO, we may be able to derive anti-ranges in some cases. */
2424 if (code
!= BIT_AND_EXPR
2425 && code
!= BIT_IOR_EXPR
2426 && code
!= TRUNC_DIV_EXPR
2427 && code
!= FLOOR_DIV_EXPR
2428 && code
!= CEIL_DIV_EXPR
2429 && code
!= EXACT_DIV_EXPR
2430 && code
!= ROUND_DIV_EXPR
2431 && code
!= TRUNC_MOD_EXPR
2434 && code
!= PLUS_EXPR
2435 && code
!= MINUS_EXPR
2436 && (vr0
.type
== VR_VARYING
2437 || vr1
.type
== VR_VARYING
2438 || vr0
.type
!= vr1
.type
2439 || symbolic_range_p (&vr0
)
2440 || symbolic_range_p (&vr1
)))
2442 set_value_range_to_varying (vr
);
2446 /* Now evaluate the expression to determine the new range. */
2447 if (POINTER_TYPE_P (expr_type
))
2449 if (code
== MIN_EXPR
|| code
== MAX_EXPR
)
2451 /* For MIN/MAX expressions with pointers, we only care about
2452 nullness, if both are non null, then the result is nonnull.
2453 If both are null, then the result is null. Otherwise they
2455 if (range_is_nonnull (&vr0
) && range_is_nonnull (&vr1
))
2456 set_value_range_to_nonnull (vr
, expr_type
);
2457 else if (range_is_null (&vr0
) && range_is_null (&vr1
))
2458 set_value_range_to_null (vr
, expr_type
);
2460 set_value_range_to_varying (vr
);
2462 else if (code
== POINTER_PLUS_EXPR
)
2464 /* For pointer types, we are really only interested in asserting
2465 whether the expression evaluates to non-NULL. */
2466 if (range_is_nonnull (&vr0
) || range_is_nonnull (&vr1
))
2467 set_value_range_to_nonnull (vr
, expr_type
);
2468 else if (range_is_null (&vr0
) && range_is_null (&vr1
))
2469 set_value_range_to_null (vr
, expr_type
);
2471 set_value_range_to_varying (vr
);
2473 else if (code
== BIT_AND_EXPR
)
2475 /* For pointer types, we are really only interested in asserting
2476 whether the expression evaluates to non-NULL. */
2477 if (range_is_nonnull (&vr0
) && range_is_nonnull (&vr1
))
2478 set_value_range_to_nonnull (vr
, expr_type
);
2479 else if (range_is_null (&vr0
) || range_is_null (&vr1
))
2480 set_value_range_to_null (vr
, expr_type
);
2482 set_value_range_to_varying (vr
);
2485 set_value_range_to_varying (vr
);
2490 /* For integer ranges, apply the operation to each end of the
2491 range and see what we end up with. */
2492 if (code
== PLUS_EXPR
|| code
== MINUS_EXPR
)
2494 const bool minus_p
= (code
== MINUS_EXPR
);
2495 tree min_op0
= vr0
.min
;
2496 tree min_op1
= minus_p
? vr1
.max
: vr1
.min
;
2497 tree max_op0
= vr0
.max
;
2498 tree max_op1
= minus_p
? vr1
.min
: vr1
.max
;
2499 tree sym_min_op0
= NULL_TREE
;
2500 tree sym_min_op1
= NULL_TREE
;
2501 tree sym_max_op0
= NULL_TREE
;
2502 tree sym_max_op1
= NULL_TREE
;
2503 bool neg_min_op0
, neg_min_op1
, neg_max_op0
, neg_max_op1
;
2505 /* If we have a PLUS or MINUS with two VR_RANGEs, either constant or
2506 single-symbolic ranges, try to compute the precise resulting range,
2507 but only if we know that this resulting range will also be constant
2508 or single-symbolic. */
2509 if (vr0
.type
== VR_RANGE
&& vr1
.type
== VR_RANGE
2510 && (TREE_CODE (min_op0
) == INTEGER_CST
2512 = get_single_symbol (min_op0
, &neg_min_op0
, &min_op0
)))
2513 && (TREE_CODE (min_op1
) == INTEGER_CST
2515 = get_single_symbol (min_op1
, &neg_min_op1
, &min_op1
)))
2516 && (!(sym_min_op0
&& sym_min_op1
)
2517 || (sym_min_op0
== sym_min_op1
2518 && neg_min_op0
== (minus_p
? neg_min_op1
: !neg_min_op1
)))
2519 && (TREE_CODE (max_op0
) == INTEGER_CST
2521 = get_single_symbol (max_op0
, &neg_max_op0
, &max_op0
)))
2522 && (TREE_CODE (max_op1
) == INTEGER_CST
2524 = get_single_symbol (max_op1
, &neg_max_op1
, &max_op1
)))
2525 && (!(sym_max_op0
&& sym_max_op1
)
2526 || (sym_max_op0
== sym_max_op1
2527 && neg_max_op0
== (minus_p
? neg_max_op1
: !neg_max_op1
))))
2529 const signop sgn
= TYPE_SIGN (expr_type
);
2530 const unsigned int prec
= TYPE_PRECISION (expr_type
);
2531 wide_int type_min
, type_max
, wmin
, wmax
;
2535 /* Get the lower and upper bounds of the type. */
2536 if (TYPE_OVERFLOW_WRAPS (expr_type
))
2538 type_min
= wi::min_value (prec
, sgn
);
2539 type_max
= wi::max_value (prec
, sgn
);
2543 type_min
= vrp_val_min (expr_type
);
2544 type_max
= vrp_val_max (expr_type
);
2547 /* Combine the lower bounds, if any. */
2548 if (min_op0
&& min_op1
)
2552 wmin
= wi::sub (min_op0
, min_op1
);
2554 /* Check for overflow. */
2555 if (wi::cmp (0, min_op1
, sgn
)
2556 != wi::cmp (wmin
, min_op0
, sgn
))
2557 min_ovf
= wi::cmp (min_op0
, min_op1
, sgn
);
2561 wmin
= wi::add (min_op0
, min_op1
);
2563 /* Check for overflow. */
2564 if (wi::cmp (min_op1
, 0, sgn
)
2565 != wi::cmp (wmin
, min_op0
, sgn
))
2566 min_ovf
= wi::cmp (min_op0
, wmin
, sgn
);
2572 wmin
= minus_p
? wi::neg (min_op1
) : min_op1
;
2574 wmin
= wi::shwi (0, prec
);
2576 /* Combine the upper bounds, if any. */
2577 if (max_op0
&& max_op1
)
2581 wmax
= wi::sub (max_op0
, max_op1
);
2583 /* Check for overflow. */
2584 if (wi::cmp (0, max_op1
, sgn
)
2585 != wi::cmp (wmax
, max_op0
, sgn
))
2586 max_ovf
= wi::cmp (max_op0
, max_op1
, sgn
);
2590 wmax
= wi::add (max_op0
, max_op1
);
2592 if (wi::cmp (max_op1
, 0, sgn
)
2593 != wi::cmp (wmax
, max_op0
, sgn
))
2594 max_ovf
= wi::cmp (max_op0
, wmax
, sgn
);
2600 wmax
= minus_p
? wi::neg (max_op1
) : max_op1
;
2602 wmax
= wi::shwi (0, prec
);
2604 /* Check for type overflow. */
2607 if (wi::cmp (wmin
, type_min
, sgn
) == -1)
2609 else if (wi::cmp (wmin
, type_max
, sgn
) == 1)
2614 if (wi::cmp (wmax
, type_min
, sgn
) == -1)
2616 else if (wi::cmp (wmax
, type_max
, sgn
) == 1)
2620 /* If we have overflow for the constant part and the resulting
2621 range will be symbolic, drop to VR_VARYING. */
2622 if ((min_ovf
&& sym_min_op0
!= sym_min_op1
)
2623 || (max_ovf
&& sym_max_op0
!= sym_max_op1
))
2625 set_value_range_to_varying (vr
);
2629 if (TYPE_OVERFLOW_WRAPS (expr_type
))
2631 /* If overflow wraps, truncate the values and adjust the
2632 range kind and bounds appropriately. */
2633 wide_int tmin
= wide_int::from (wmin
, prec
, sgn
);
2634 wide_int tmax
= wide_int::from (wmax
, prec
, sgn
);
2635 if (min_ovf
== max_ovf
)
2637 /* No overflow or both overflow or underflow. The
2638 range kind stays VR_RANGE. */
2639 min
= wide_int_to_tree (expr_type
, tmin
);
2640 max
= wide_int_to_tree (expr_type
, tmax
);
2642 else if (min_ovf
== -1 && max_ovf
== 1)
2644 /* Underflow and overflow, drop to VR_VARYING. */
2645 set_value_range_to_varying (vr
);
2650 /* Min underflow or max overflow. The range kind
2651 changes to VR_ANTI_RANGE. */
2652 bool covers
= false;
2653 wide_int tem
= tmin
;
2654 gcc_assert ((min_ovf
== -1 && max_ovf
== 0)
2655 || (max_ovf
== 1 && min_ovf
== 0));
2656 type
= VR_ANTI_RANGE
;
2658 if (wi::cmp (tmin
, tmax
, sgn
) < 0)
2661 if (wi::cmp (tmax
, tem
, sgn
) > 0)
2663 /* If the anti-range would cover nothing, drop to varying.
2664 Likewise if the anti-range bounds are outside of the
2666 if (covers
|| wi::cmp (tmin
, tmax
, sgn
) > 0)
2668 set_value_range_to_varying (vr
);
2671 min
= wide_int_to_tree (expr_type
, tmin
);
2672 max
= wide_int_to_tree (expr_type
, tmax
);
2677 /* If overflow does not wrap, saturate to the types min/max
2681 if (needs_overflow_infinity (expr_type
)
2682 && supports_overflow_infinity (expr_type
))
2683 min
= negative_overflow_infinity (expr_type
);
2685 min
= wide_int_to_tree (expr_type
, type_min
);
2687 else if (min_ovf
== 1)
2689 if (needs_overflow_infinity (expr_type
)
2690 && supports_overflow_infinity (expr_type
))
2691 min
= positive_overflow_infinity (expr_type
);
2693 min
= wide_int_to_tree (expr_type
, type_max
);
2696 min
= wide_int_to_tree (expr_type
, wmin
);
2700 if (needs_overflow_infinity (expr_type
)
2701 && supports_overflow_infinity (expr_type
))
2702 max
= negative_overflow_infinity (expr_type
);
2704 max
= wide_int_to_tree (expr_type
, type_min
);
2706 else if (max_ovf
== 1)
2708 if (needs_overflow_infinity (expr_type
)
2709 && supports_overflow_infinity (expr_type
))
2710 max
= positive_overflow_infinity (expr_type
);
2712 max
= wide_int_to_tree (expr_type
, type_max
);
2715 max
= wide_int_to_tree (expr_type
, wmax
);
2718 if (needs_overflow_infinity (expr_type
)
2719 && supports_overflow_infinity (expr_type
))
2721 if ((min_op0
&& is_negative_overflow_infinity (min_op0
))
2724 ? is_positive_overflow_infinity (min_op1
)
2725 : is_negative_overflow_infinity (min_op1
))))
2726 min
= negative_overflow_infinity (expr_type
);
2727 if ((max_op0
&& is_positive_overflow_infinity (max_op0
))
2730 ? is_negative_overflow_infinity (max_op1
)
2731 : is_positive_overflow_infinity (max_op1
))))
2732 max
= positive_overflow_infinity (expr_type
);
2735 /* If the result lower bound is constant, we're done;
2736 otherwise, build the symbolic lower bound. */
2737 if (sym_min_op0
== sym_min_op1
)
2739 else if (sym_min_op0
)
2740 min
= build_symbolic_expr (expr_type
, sym_min_op0
,
2742 else if (sym_min_op1
)
2743 min
= build_symbolic_expr (expr_type
, sym_min_op1
,
2744 neg_min_op1
^ minus_p
, min
);
2746 /* Likewise for the upper bound. */
2747 if (sym_max_op0
== sym_max_op1
)
2749 else if (sym_max_op0
)
2750 max
= build_symbolic_expr (expr_type
, sym_max_op0
,
2752 else if (sym_max_op1
)
2753 max
= build_symbolic_expr (expr_type
, sym_max_op1
,
2754 neg_max_op1
^ minus_p
, max
);
2758 /* For other cases, for example if we have a PLUS_EXPR with two
2759 VR_ANTI_RANGEs, drop to VR_VARYING. It would take more effort
2760 to compute a precise range for such a case.
2761 ??? General even mixed range kind operations can be expressed
2762 by for example transforming ~[3, 5] + [1, 2] to range-only
2763 operations and a union primitive:
2764 [-INF, 2] + [1, 2] U [5, +INF] + [1, 2]
2765 [-INF+1, 4] U [6, +INF(OVF)]
2766 though usually the union is not exactly representable with
2767 a single range or anti-range as the above is
2768 [-INF+1, +INF(OVF)] intersected with ~[5, 5]
2769 but one could use a scheme similar to equivalences for this. */
2770 set_value_range_to_varying (vr
);
2774 else if (code
== MIN_EXPR
2775 || code
== MAX_EXPR
)
2777 if (vr0
.type
== VR_RANGE
2778 && !symbolic_range_p (&vr0
))
2781 if (vr1
.type
== VR_RANGE
2782 && !symbolic_range_p (&vr1
))
2784 /* For operations that make the resulting range directly
2785 proportional to the original ranges, apply the operation to
2786 the same end of each range. */
2787 min
= vrp_int_const_binop (code
, vr0
.min
, vr1
.min
);
2788 max
= vrp_int_const_binop (code
, vr0
.max
, vr1
.max
);
2790 else if (code
== MIN_EXPR
)
2792 min
= vrp_val_min (expr_type
);
2795 else if (code
== MAX_EXPR
)
2798 max
= vrp_val_max (expr_type
);
2801 else if (vr1
.type
== VR_RANGE
2802 && !symbolic_range_p (&vr1
))
2805 if (code
== MIN_EXPR
)
2807 min
= vrp_val_min (expr_type
);
2810 else if (code
== MAX_EXPR
)
2813 max
= vrp_val_max (expr_type
);
2818 set_value_range_to_varying (vr
);
2822 else if (code
== MULT_EXPR
)
2824 /* Fancy code so that with unsigned, [-3,-1]*[-3,-1] does not
2825 drop to varying. This test requires 2*prec bits if both
2826 operands are signed and 2*prec + 2 bits if either is not. */
2828 signop sign
= TYPE_SIGN (expr_type
);
2829 unsigned int prec
= TYPE_PRECISION (expr_type
);
2831 if (range_int_cst_p (&vr0
)
2832 && range_int_cst_p (&vr1
)
2833 && TYPE_OVERFLOW_WRAPS (expr_type
))
2835 typedef FIXED_WIDE_INT (WIDE_INT_MAX_PRECISION
* 2) vrp_int
;
2836 typedef generic_wide_int
2837 <wi::extended_tree
<WIDE_INT_MAX_PRECISION
* 2> > vrp_int_cst
;
2838 vrp_int sizem1
= wi::mask
<vrp_int
> (prec
, false);
2839 vrp_int size
= sizem1
+ 1;
2841 /* Extend the values using the sign of the result to PREC2.
2842 From here on out, everthing is just signed math no matter
2843 what the input types were. */
2844 vrp_int min0
= vrp_int_cst (vr0
.min
);
2845 vrp_int max0
= vrp_int_cst (vr0
.max
);
2846 vrp_int min1
= vrp_int_cst (vr1
.min
);
2847 vrp_int max1
= vrp_int_cst (vr1
.max
);
2848 /* Canonicalize the intervals. */
2849 if (sign
== UNSIGNED
)
2851 if (wi::ltu_p (size
, min0
+ max0
))
2857 if (wi::ltu_p (size
, min1
+ max1
))
2864 vrp_int prod0
= min0
* min1
;
2865 vrp_int prod1
= min0
* max1
;
2866 vrp_int prod2
= max0
* min1
;
2867 vrp_int prod3
= max0
* max1
;
2869 /* Sort the 4 products so that min is in prod0 and max is in
2871 /* min0min1 > max0max1 */
2872 if (wi::gts_p (prod0
, prod3
))
2874 vrp_int tmp
= prod3
;
2879 /* min0max1 > max0min1 */
2880 if (wi::gts_p (prod1
, prod2
))
2882 vrp_int tmp
= prod2
;
2887 if (wi::gts_p (prod0
, prod1
))
2889 vrp_int tmp
= prod1
;
2894 if (wi::gts_p (prod2
, prod3
))
2896 vrp_int tmp
= prod3
;
2901 /* diff = max - min. */
2902 prod2
= prod3
- prod0
;
2903 if (wi::geu_p (prod2
, sizem1
))
2905 /* the range covers all values. */
2906 set_value_range_to_varying (vr
);
2910 /* The following should handle the wrapping and selecting
2911 VR_ANTI_RANGE for us. */
2912 min
= wide_int_to_tree (expr_type
, prod0
);
2913 max
= wide_int_to_tree (expr_type
, prod3
);
2914 set_and_canonicalize_value_range (vr
, VR_RANGE
, min
, max
, NULL
);
2918 /* If we have an unsigned MULT_EXPR with two VR_ANTI_RANGEs,
2919 drop to VR_VARYING. It would take more effort to compute a
2920 precise range for such a case. For example, if we have
2921 op0 == 65536 and op1 == 65536 with their ranges both being
2922 ~[0,0] on a 32-bit machine, we would have op0 * op1 == 0, so
2923 we cannot claim that the product is in ~[0,0]. Note that we
2924 are guaranteed to have vr0.type == vr1.type at this
2926 if (vr0
.type
== VR_ANTI_RANGE
2927 && !TYPE_OVERFLOW_UNDEFINED (expr_type
))
2929 set_value_range_to_varying (vr
);
2933 extract_range_from_multiplicative_op_1 (vr
, code
, &vr0
, &vr1
);
2936 else if (code
== RSHIFT_EXPR
2937 || code
== LSHIFT_EXPR
)
2939 /* If we have a RSHIFT_EXPR with any shift values outside [0..prec-1],
2940 then drop to VR_VARYING. Outside of this range we get undefined
2941 behavior from the shift operation. We cannot even trust
2942 SHIFT_COUNT_TRUNCATED at this stage, because that applies to rtl
2943 shifts, and the operation at the tree level may be widened. */
2944 if (range_int_cst_p (&vr1
)
2945 && compare_tree_int (vr1
.min
, 0) >= 0
2946 && compare_tree_int (vr1
.max
, TYPE_PRECISION (expr_type
)) == -1)
2948 if (code
== RSHIFT_EXPR
)
2950 extract_range_from_multiplicative_op_1 (vr
, code
, &vr0
, &vr1
);
2953 /* We can map lshifts by constants to MULT_EXPR handling. */
2954 else if (code
== LSHIFT_EXPR
2955 && range_int_cst_singleton_p (&vr1
))
2957 bool saved_flag_wrapv
;
2958 value_range_t vr1p
= VR_INITIALIZER
;
2959 vr1p
.type
= VR_RANGE
;
2960 vr1p
.min
= (wide_int_to_tree
2962 wi::set_bit_in_zero (tree_to_shwi (vr1
.min
),
2963 TYPE_PRECISION (expr_type
))));
2964 vr1p
.max
= vr1p
.min
;
2965 /* We have to use a wrapping multiply though as signed overflow
2966 on lshifts is implementation defined in C89. */
2967 saved_flag_wrapv
= flag_wrapv
;
2969 extract_range_from_binary_expr_1 (vr
, MULT_EXPR
, expr_type
,
2971 flag_wrapv
= saved_flag_wrapv
;
2974 else if (code
== LSHIFT_EXPR
2975 && range_int_cst_p (&vr0
))
2977 int prec
= TYPE_PRECISION (expr_type
);
2978 int overflow_pos
= prec
;
2980 wide_int low_bound
, high_bound
;
2981 bool uns
= TYPE_UNSIGNED (expr_type
);
2982 bool in_bounds
= false;
2987 bound_shift
= overflow_pos
- tree_to_shwi (vr1
.max
);
2988 /* If bound_shift == HOST_BITS_PER_WIDE_INT, the llshift can
2989 overflow. However, for that to happen, vr1.max needs to be
2990 zero, which means vr1 is a singleton range of zero, which
2991 means it should be handled by the previous LSHIFT_EXPR
2993 wide_int bound
= wi::set_bit_in_zero (bound_shift
, prec
);
2994 wide_int complement
= ~(bound
- 1);
2999 high_bound
= complement
;
3000 if (wi::ltu_p (vr0
.max
, low_bound
))
3002 /* [5, 6] << [1, 2] == [10, 24]. */
3003 /* We're shifting out only zeroes, the value increases
3007 else if (wi::ltu_p (high_bound
, vr0
.min
))
3009 /* [0xffffff00, 0xffffffff] << [1, 2]
3010 == [0xfffffc00, 0xfffffffe]. */
3011 /* We're shifting out only ones, the value decreases
3018 /* [-1, 1] << [1, 2] == [-4, 4]. */
3019 low_bound
= complement
;
3021 if (wi::lts_p (vr0
.max
, high_bound
)
3022 && wi::lts_p (low_bound
, vr0
.min
))
3024 /* For non-negative numbers, we're shifting out only
3025 zeroes, the value increases monotonically.
3026 For negative numbers, we're shifting out only ones, the
3027 value decreases monotomically. */
3034 extract_range_from_multiplicative_op_1 (vr
, code
, &vr0
, &vr1
);
3039 set_value_range_to_varying (vr
);
3042 else if (code
== TRUNC_DIV_EXPR
3043 || code
== FLOOR_DIV_EXPR
3044 || code
== CEIL_DIV_EXPR
3045 || code
== EXACT_DIV_EXPR
3046 || code
== ROUND_DIV_EXPR
)
3048 if (vr0
.type
!= VR_RANGE
|| symbolic_range_p (&vr0
))
3050 /* For division, if op1 has VR_RANGE but op0 does not, something
3051 can be deduced just from that range. Say [min, max] / [4, max]
3052 gives [min / 4, max / 4] range. */
3053 if (vr1
.type
== VR_RANGE
3054 && !symbolic_range_p (&vr1
)
3055 && range_includes_zero_p (vr1
.min
, vr1
.max
) == 0)
3057 vr0
.type
= type
= VR_RANGE
;
3058 vr0
.min
= vrp_val_min (expr_type
);
3059 vr0
.max
= vrp_val_max (expr_type
);
3063 set_value_range_to_varying (vr
);
3068 /* For divisions, if flag_non_call_exceptions is true, we must
3069 not eliminate a division by zero. */
3070 if (cfun
->can_throw_non_call_exceptions
3071 && (vr1
.type
!= VR_RANGE
3072 || range_includes_zero_p (vr1
.min
, vr1
.max
) != 0))
3074 set_value_range_to_varying (vr
);
3078 /* For divisions, if op0 is VR_RANGE, we can deduce a range
3079 even if op1 is VR_VARYING, VR_ANTI_RANGE, symbolic or can
3081 if (vr0
.type
== VR_RANGE
3082 && (vr1
.type
!= VR_RANGE
3083 || range_includes_zero_p (vr1
.min
, vr1
.max
) != 0))
3085 tree zero
= build_int_cst (TREE_TYPE (vr0
.min
), 0);
3090 if (TYPE_UNSIGNED (expr_type
)
3091 || value_range_nonnegative_p (&vr1
))
3093 /* For unsigned division or when divisor is known
3094 to be non-negative, the range has to cover
3095 all numbers from 0 to max for positive max
3096 and all numbers from min to 0 for negative min. */
3097 cmp
= compare_values (vr0
.max
, zero
);
3100 else if (cmp
== 0 || cmp
== 1)
3104 cmp
= compare_values (vr0
.min
, zero
);
3107 else if (cmp
== 0 || cmp
== -1)
3114 /* Otherwise the range is -max .. max or min .. -min
3115 depending on which bound is bigger in absolute value,
3116 as the division can change the sign. */
3117 abs_extent_range (vr
, vr0
.min
, vr0
.max
);
3120 if (type
== VR_VARYING
)
3122 set_value_range_to_varying (vr
);
3128 extract_range_from_multiplicative_op_1 (vr
, code
, &vr0
, &vr1
);
3132 else if (code
== TRUNC_MOD_EXPR
)
3134 if (vr1
.type
!= VR_RANGE
3135 || range_includes_zero_p (vr1
.min
, vr1
.max
) != 0
3136 || vrp_val_is_min (vr1
.min
))
3138 set_value_range_to_varying (vr
);
3142 /* Compute MAX <|vr1.min|, |vr1.max|> - 1. */
3143 max
= fold_unary_to_constant (ABS_EXPR
, expr_type
, vr1
.min
);
3144 if (tree_int_cst_lt (max
, vr1
.max
))
3146 max
= int_const_binop (MINUS_EXPR
, max
, build_int_cst (TREE_TYPE (max
), 1));
3147 /* If the dividend is non-negative the modulus will be
3148 non-negative as well. */
3149 if (TYPE_UNSIGNED (expr_type
)
3150 || value_range_nonnegative_p (&vr0
))
3151 min
= build_int_cst (TREE_TYPE (max
), 0);
3153 min
= fold_unary_to_constant (NEGATE_EXPR
, expr_type
, max
);
3155 else if (code
== BIT_AND_EXPR
|| code
== BIT_IOR_EXPR
|| code
== BIT_XOR_EXPR
)
3157 bool int_cst_range0
, int_cst_range1
;
3158 wide_int may_be_nonzero0
, may_be_nonzero1
;
3159 wide_int must_be_nonzero0
, must_be_nonzero1
;
3161 int_cst_range0
= zero_nonzero_bits_from_vr (expr_type
, &vr0
,
3164 int_cst_range1
= zero_nonzero_bits_from_vr (expr_type
, &vr1
,
3169 if (code
== BIT_AND_EXPR
)
3171 min
= wide_int_to_tree (expr_type
,
3172 must_be_nonzero0
& must_be_nonzero1
);
3173 wide_int wmax
= may_be_nonzero0
& may_be_nonzero1
;
3174 /* If both input ranges contain only negative values we can
3175 truncate the result range maximum to the minimum of the
3176 input range maxima. */
3177 if (int_cst_range0
&& int_cst_range1
3178 && tree_int_cst_sgn (vr0
.max
) < 0
3179 && tree_int_cst_sgn (vr1
.max
) < 0)
3181 wmax
= wi::min (wmax
, vr0
.max
, TYPE_SIGN (expr_type
));
3182 wmax
= wi::min (wmax
, vr1
.max
, TYPE_SIGN (expr_type
));
3184 /* If either input range contains only non-negative values
3185 we can truncate the result range maximum to the respective
3186 maximum of the input range. */
3187 if (int_cst_range0
&& tree_int_cst_sgn (vr0
.min
) >= 0)
3188 wmax
= wi::min (wmax
, vr0
.max
, TYPE_SIGN (expr_type
));
3189 if (int_cst_range1
&& tree_int_cst_sgn (vr1
.min
) >= 0)
3190 wmax
= wi::min (wmax
, vr1
.max
, TYPE_SIGN (expr_type
));
3191 max
= wide_int_to_tree (expr_type
, wmax
);
3193 else if (code
== BIT_IOR_EXPR
)
3195 max
= wide_int_to_tree (expr_type
,
3196 may_be_nonzero0
| may_be_nonzero1
);
3197 wide_int wmin
= must_be_nonzero0
| must_be_nonzero1
;
3198 /* If the input ranges contain only positive values we can
3199 truncate the minimum of the result range to the maximum
3200 of the input range minima. */
3201 if (int_cst_range0
&& int_cst_range1
3202 && tree_int_cst_sgn (vr0
.min
) >= 0
3203 && tree_int_cst_sgn (vr1
.min
) >= 0)
3205 wmin
= wi::max (wmin
, vr0
.min
, TYPE_SIGN (expr_type
));
3206 wmin
= wi::max (wmin
, vr1
.min
, TYPE_SIGN (expr_type
));
3208 /* If either input range contains only negative values
3209 we can truncate the minimum of the result range to the
3210 respective minimum range. */
3211 if (int_cst_range0
&& tree_int_cst_sgn (vr0
.max
) < 0)
3212 wmin
= wi::max (wmin
, vr0
.min
, TYPE_SIGN (expr_type
));
3213 if (int_cst_range1
&& tree_int_cst_sgn (vr1
.max
) < 0)
3214 wmin
= wi::max (wmin
, vr1
.min
, TYPE_SIGN (expr_type
));
3215 min
= wide_int_to_tree (expr_type
, wmin
);
3217 else if (code
== BIT_XOR_EXPR
)
3219 wide_int result_zero_bits
= ((must_be_nonzero0
& must_be_nonzero1
)
3220 | ~(may_be_nonzero0
| may_be_nonzero1
));
3221 wide_int result_one_bits
3222 = (must_be_nonzero0
.and_not (may_be_nonzero1
)
3223 | must_be_nonzero1
.and_not (may_be_nonzero0
));
3224 max
= wide_int_to_tree (expr_type
, ~result_zero_bits
);
3225 min
= wide_int_to_tree (expr_type
, result_one_bits
);
3226 /* If the range has all positive or all negative values the
3227 result is better than VARYING. */
3228 if (tree_int_cst_sgn (min
) < 0
3229 || tree_int_cst_sgn (max
) >= 0)
3232 max
= min
= NULL_TREE
;
3238 /* If either MIN or MAX overflowed, then set the resulting range to
3239 VARYING. But we do accept an overflow infinity representation. */
3240 if (min
== NULL_TREE
3241 || (TREE_OVERFLOW_P (min
) && !is_overflow_infinity (min
))
3243 || (TREE_OVERFLOW_P (max
) && !is_overflow_infinity (max
)))
3245 set_value_range_to_varying (vr
);
3251 2) [-INF, +-INF(OVF)]
3252 3) [+-INF(OVF), +INF]
3253 4) [+-INF(OVF), +-INF(OVF)]
3254 We learn nothing when we have INF and INF(OVF) on both sides.
3255 Note that we do accept [-INF, -INF] and [+INF, +INF] without
3257 if ((vrp_val_is_min (min
) || is_overflow_infinity (min
))
3258 && (vrp_val_is_max (max
) || is_overflow_infinity (max
)))
3260 set_value_range_to_varying (vr
);
3264 cmp
= compare_values (min
, max
);
3265 if (cmp
== -2 || cmp
== 1)
3267 /* If the new range has its limits swapped around (MIN > MAX),
3268 then the operation caused one of them to wrap around, mark
3269 the new range VARYING. */
3270 set_value_range_to_varying (vr
);
3273 set_value_range (vr
, type
, min
, max
, NULL
);
3276 /* Extract range information from a binary expression OP0 CODE OP1 based on
3277 the ranges of each of its operands with resulting type EXPR_TYPE.
3278 The resulting range is stored in *VR. */
3281 extract_range_from_binary_expr (value_range_t
*vr
,
3282 enum tree_code code
,
3283 tree expr_type
, tree op0
, tree op1
)
3285 value_range_t vr0
= VR_INITIALIZER
;
3286 value_range_t vr1
= VR_INITIALIZER
;
3288 /* Get value ranges for each operand. For constant operands, create
3289 a new value range with the operand to simplify processing. */
3290 if (TREE_CODE (op0
) == SSA_NAME
)
3291 vr0
= *(get_value_range (op0
));
3292 else if (is_gimple_min_invariant (op0
))
3293 set_value_range_to_value (&vr0
, op0
, NULL
);
3295 set_value_range_to_varying (&vr0
);
3297 if (TREE_CODE (op1
) == SSA_NAME
)
3298 vr1
= *(get_value_range (op1
));
3299 else if (is_gimple_min_invariant (op1
))
3300 set_value_range_to_value (&vr1
, op1
, NULL
);
3302 set_value_range_to_varying (&vr1
);
3304 extract_range_from_binary_expr_1 (vr
, code
, expr_type
, &vr0
, &vr1
);
3306 /* Try harder for PLUS and MINUS if the range of one operand is symbolic
3307 and based on the other operand, for example if it was deduced from a
3308 symbolic comparison. When a bound of the range of the first operand
3309 is invariant, we set the corresponding bound of the new range to INF
3310 in order to avoid recursing on the range of the second operand. */
3311 if (vr
->type
== VR_VARYING
3312 && (code
== PLUS_EXPR
|| code
== MINUS_EXPR
)
3313 && TREE_CODE (op1
) == SSA_NAME
3314 && vr0
.type
== VR_RANGE
3315 && symbolic_range_based_on_p (&vr0
, op1
))
3317 const bool minus_p
= (code
== MINUS_EXPR
);
3318 value_range_t n_vr1
= VR_INITIALIZER
;
3320 /* Try with VR0 and [-INF, OP1]. */
3321 if (is_gimple_min_invariant (minus_p
? vr0
.max
: vr0
.min
))
3322 set_value_range (&n_vr1
, VR_RANGE
, vrp_val_min (expr_type
), op1
, NULL
);
3324 /* Try with VR0 and [OP1, +INF]. */
3325 else if (is_gimple_min_invariant (minus_p
? vr0
.min
: vr0
.max
))
3326 set_value_range (&n_vr1
, VR_RANGE
, op1
, vrp_val_max (expr_type
), NULL
);
3328 /* Try with VR0 and [OP1, OP1]. */
3330 set_value_range (&n_vr1
, VR_RANGE
, op1
, op1
, NULL
);
3332 extract_range_from_binary_expr_1 (vr
, code
, expr_type
, &vr0
, &n_vr1
);
3335 if (vr
->type
== VR_VARYING
3336 && (code
== PLUS_EXPR
|| code
== MINUS_EXPR
)
3337 && TREE_CODE (op0
) == SSA_NAME
3338 && vr1
.type
== VR_RANGE
3339 && symbolic_range_based_on_p (&vr1
, op0
))
3341 const bool minus_p
= (code
== MINUS_EXPR
);
3342 value_range_t n_vr0
= VR_INITIALIZER
;
3344 /* Try with [-INF, OP0] and VR1. */
3345 if (is_gimple_min_invariant (minus_p
? vr1
.max
: vr1
.min
))
3346 set_value_range (&n_vr0
, VR_RANGE
, vrp_val_min (expr_type
), op0
, NULL
);
3348 /* Try with [OP0, +INF] and VR1. */
3349 else if (is_gimple_min_invariant (minus_p
? vr1
.min
: vr1
.max
))
3350 set_value_range (&n_vr0
, VR_RANGE
, op0
, vrp_val_max (expr_type
), NULL
);
3352 /* Try with [OP0, OP0] and VR1. */
3354 set_value_range (&n_vr0
, VR_RANGE
, op0
, op0
, NULL
);
3356 extract_range_from_binary_expr_1 (vr
, code
, expr_type
, &n_vr0
, &vr1
);
3360 /* Extract range information from a unary operation CODE based on
3361 the range of its operand *VR0 with type OP0_TYPE with resulting type TYPE.
3362 The The resulting range is stored in *VR. */
3365 extract_range_from_unary_expr_1 (value_range_t
*vr
,
3366 enum tree_code code
, tree type
,
3367 value_range_t
*vr0_
, tree op0_type
)
3369 value_range_t vr0
= *vr0_
, vrtem0
= VR_INITIALIZER
, vrtem1
= VR_INITIALIZER
;
3371 /* VRP only operates on integral and pointer types. */
3372 if (!(INTEGRAL_TYPE_P (op0_type
)
3373 || POINTER_TYPE_P (op0_type
))
3374 || !(INTEGRAL_TYPE_P (type
)
3375 || POINTER_TYPE_P (type
)))
3377 set_value_range_to_varying (vr
);
3381 /* If VR0 is UNDEFINED, so is the result. */
3382 if (vr0
.type
== VR_UNDEFINED
)
3384 set_value_range_to_undefined (vr
);
3388 /* Handle operations that we express in terms of others. */
3389 if (code
== PAREN_EXPR
|| code
== OBJ_TYPE_REF
)
3391 /* PAREN_EXPR and OBJ_TYPE_REF are simple copies. */
3392 copy_value_range (vr
, &vr0
);
3395 else if (code
== NEGATE_EXPR
)
3397 /* -X is simply 0 - X, so re-use existing code that also handles
3398 anti-ranges fine. */
3399 value_range_t zero
= VR_INITIALIZER
;
3400 set_value_range_to_value (&zero
, build_int_cst (type
, 0), NULL
);
3401 extract_range_from_binary_expr_1 (vr
, MINUS_EXPR
, type
, &zero
, &vr0
);
3404 else if (code
== BIT_NOT_EXPR
)
3406 /* ~X is simply -1 - X, so re-use existing code that also handles
3407 anti-ranges fine. */
3408 value_range_t minusone
= VR_INITIALIZER
;
3409 set_value_range_to_value (&minusone
, build_int_cst (type
, -1), NULL
);
3410 extract_range_from_binary_expr_1 (vr
, MINUS_EXPR
,
3411 type
, &minusone
, &vr0
);
3415 /* Now canonicalize anti-ranges to ranges when they are not symbolic
3416 and express op ~[] as (op []') U (op []''). */
3417 if (vr0
.type
== VR_ANTI_RANGE
3418 && ranges_from_anti_range (&vr0
, &vrtem0
, &vrtem1
))
3420 extract_range_from_unary_expr_1 (vr
, code
, type
, &vrtem0
, op0_type
);
3421 if (vrtem1
.type
!= VR_UNDEFINED
)
3423 value_range_t vrres
= VR_INITIALIZER
;
3424 extract_range_from_unary_expr_1 (&vrres
, code
, type
,
3426 vrp_meet (vr
, &vrres
);
3431 if (CONVERT_EXPR_CODE_P (code
))
3433 tree inner_type
= op0_type
;
3434 tree outer_type
= type
;
3436 /* If the expression evaluates to a pointer, we are only interested in
3437 determining if it evaluates to NULL [0, 0] or non-NULL (~[0, 0]). */
3438 if (POINTER_TYPE_P (type
))
3440 if (range_is_nonnull (&vr0
))
3441 set_value_range_to_nonnull (vr
, type
);
3442 else if (range_is_null (&vr0
))
3443 set_value_range_to_null (vr
, type
);
3445 set_value_range_to_varying (vr
);
3449 /* If VR0 is varying and we increase the type precision, assume
3450 a full range for the following transformation. */
3451 if (vr0
.type
== VR_VARYING
3452 && INTEGRAL_TYPE_P (inner_type
)
3453 && TYPE_PRECISION (inner_type
) < TYPE_PRECISION (outer_type
))
3455 vr0
.type
= VR_RANGE
;
3456 vr0
.min
= TYPE_MIN_VALUE (inner_type
);
3457 vr0
.max
= TYPE_MAX_VALUE (inner_type
);
3460 /* If VR0 is a constant range or anti-range and the conversion is
3461 not truncating we can convert the min and max values and
3462 canonicalize the resulting range. Otherwise we can do the
3463 conversion if the size of the range is less than what the
3464 precision of the target type can represent and the range is
3465 not an anti-range. */
3466 if ((vr0
.type
== VR_RANGE
3467 || vr0
.type
== VR_ANTI_RANGE
)
3468 && TREE_CODE (vr0
.min
) == INTEGER_CST
3469 && TREE_CODE (vr0
.max
) == INTEGER_CST
3470 && (!is_overflow_infinity (vr0
.min
)
3471 || (vr0
.type
== VR_RANGE
3472 && TYPE_PRECISION (outer_type
) > TYPE_PRECISION (inner_type
)
3473 && needs_overflow_infinity (outer_type
)
3474 && supports_overflow_infinity (outer_type
)))
3475 && (!is_overflow_infinity (vr0
.max
)
3476 || (vr0
.type
== VR_RANGE
3477 && TYPE_PRECISION (outer_type
) > TYPE_PRECISION (inner_type
)
3478 && needs_overflow_infinity (outer_type
)
3479 && supports_overflow_infinity (outer_type
)))
3480 && (TYPE_PRECISION (outer_type
) >= TYPE_PRECISION (inner_type
)
3481 || (vr0
.type
== VR_RANGE
3482 && integer_zerop (int_const_binop (RSHIFT_EXPR
,
3483 int_const_binop (MINUS_EXPR
, vr0
.max
, vr0
.min
),
3484 size_int (TYPE_PRECISION (outer_type
)))))))
3486 tree new_min
, new_max
;
3487 if (is_overflow_infinity (vr0
.min
))
3488 new_min
= negative_overflow_infinity (outer_type
);
3490 new_min
= force_fit_type (outer_type
, wi::to_widest (vr0
.min
),
3492 if (is_overflow_infinity (vr0
.max
))
3493 new_max
= positive_overflow_infinity (outer_type
);
3495 new_max
= force_fit_type (outer_type
, wi::to_widest (vr0
.max
),
3497 set_and_canonicalize_value_range (vr
, vr0
.type
,
3498 new_min
, new_max
, NULL
);
3502 set_value_range_to_varying (vr
);
3505 else if (code
== ABS_EXPR
)
3510 /* Pass through vr0 in the easy cases. */
3511 if (TYPE_UNSIGNED (type
)
3512 || value_range_nonnegative_p (&vr0
))
3514 copy_value_range (vr
, &vr0
);
3518 /* For the remaining varying or symbolic ranges we can't do anything
3520 if (vr0
.type
== VR_VARYING
3521 || symbolic_range_p (&vr0
))
3523 set_value_range_to_varying (vr
);
3527 /* -TYPE_MIN_VALUE = TYPE_MIN_VALUE with flag_wrapv so we can't get a
3529 if (!TYPE_OVERFLOW_UNDEFINED (type
)
3530 && ((vr0
.type
== VR_RANGE
3531 && vrp_val_is_min (vr0
.min
))
3532 || (vr0
.type
== VR_ANTI_RANGE
3533 && !vrp_val_is_min (vr0
.min
))))
3535 set_value_range_to_varying (vr
);
3539 /* ABS_EXPR may flip the range around, if the original range
3540 included negative values. */
3541 if (is_overflow_infinity (vr0
.min
))
3542 min
= positive_overflow_infinity (type
);
3543 else if (!vrp_val_is_min (vr0
.min
))
3544 min
= fold_unary_to_constant (code
, type
, vr0
.min
);
3545 else if (!needs_overflow_infinity (type
))
3546 min
= TYPE_MAX_VALUE (type
);
3547 else if (supports_overflow_infinity (type
))
3548 min
= positive_overflow_infinity (type
);
3551 set_value_range_to_varying (vr
);
3555 if (is_overflow_infinity (vr0
.max
))
3556 max
= positive_overflow_infinity (type
);
3557 else if (!vrp_val_is_min (vr0
.max
))
3558 max
= fold_unary_to_constant (code
, type
, vr0
.max
);
3559 else if (!needs_overflow_infinity (type
))
3560 max
= TYPE_MAX_VALUE (type
);
3561 else if (supports_overflow_infinity (type
)
3562 /* We shouldn't generate [+INF, +INF] as set_value_range
3563 doesn't like this and ICEs. */
3564 && !is_positive_overflow_infinity (min
))
3565 max
= positive_overflow_infinity (type
);
3568 set_value_range_to_varying (vr
);
3572 cmp
= compare_values (min
, max
);
3574 /* If a VR_ANTI_RANGEs contains zero, then we have
3575 ~[-INF, min(MIN, MAX)]. */
3576 if (vr0
.type
== VR_ANTI_RANGE
)
3578 if (range_includes_zero_p (vr0
.min
, vr0
.max
) == 1)
3580 /* Take the lower of the two values. */
3584 /* Create ~[-INF, min (abs(MIN), abs(MAX))]
3585 or ~[-INF + 1, min (abs(MIN), abs(MAX))] when
3586 flag_wrapv is set and the original anti-range doesn't include
3587 TYPE_MIN_VALUE, remember -TYPE_MIN_VALUE = TYPE_MIN_VALUE. */
3588 if (TYPE_OVERFLOW_WRAPS (type
))
3590 tree type_min_value
= TYPE_MIN_VALUE (type
);
3592 min
= (vr0
.min
!= type_min_value
3593 ? int_const_binop (PLUS_EXPR
, type_min_value
,
3594 build_int_cst (TREE_TYPE (type_min_value
), 1))
3599 if (overflow_infinity_range_p (&vr0
))
3600 min
= negative_overflow_infinity (type
);
3602 min
= TYPE_MIN_VALUE (type
);
3607 /* All else has failed, so create the range [0, INF], even for
3608 flag_wrapv since TYPE_MIN_VALUE is in the original
3610 vr0
.type
= VR_RANGE
;
3611 min
= build_int_cst (type
, 0);
3612 if (needs_overflow_infinity (type
))
3614 if (supports_overflow_infinity (type
))
3615 max
= positive_overflow_infinity (type
);
3618 set_value_range_to_varying (vr
);
3623 max
= TYPE_MAX_VALUE (type
);
3627 /* If the range contains zero then we know that the minimum value in the
3628 range will be zero. */
3629 else if (range_includes_zero_p (vr0
.min
, vr0
.max
) == 1)
3633 min
= build_int_cst (type
, 0);
3637 /* If the range was reversed, swap MIN and MAX. */
3646 cmp
= compare_values (min
, max
);
3647 if (cmp
== -2 || cmp
== 1)
3649 /* If the new range has its limits swapped around (MIN > MAX),
3650 then the operation caused one of them to wrap around, mark
3651 the new range VARYING. */
3652 set_value_range_to_varying (vr
);
3655 set_value_range (vr
, vr0
.type
, min
, max
, NULL
);
3659 /* For unhandled operations fall back to varying. */
3660 set_value_range_to_varying (vr
);
3665 /* Extract range information from a unary expression CODE OP0 based on
3666 the range of its operand with resulting type TYPE.
3667 The resulting range is stored in *VR. */
3670 extract_range_from_unary_expr (value_range_t
*vr
, enum tree_code code
,
3671 tree type
, tree op0
)
3673 value_range_t vr0
= VR_INITIALIZER
;
3675 /* Get value ranges for the operand. For constant operands, create
3676 a new value range with the operand to simplify processing. */
3677 if (TREE_CODE (op0
) == SSA_NAME
)
3678 vr0
= *(get_value_range (op0
));
3679 else if (is_gimple_min_invariant (op0
))
3680 set_value_range_to_value (&vr0
, op0
, NULL
);
3682 set_value_range_to_varying (&vr0
);
3684 extract_range_from_unary_expr_1 (vr
, code
, type
, &vr0
, TREE_TYPE (op0
));
3688 /* Extract range information from a conditional expression STMT based on
3689 the ranges of each of its operands and the expression code. */
3692 extract_range_from_cond_expr (value_range_t
*vr
, gassign
*stmt
)
3695 value_range_t vr0
= VR_INITIALIZER
;
3696 value_range_t vr1
= VR_INITIALIZER
;
3698 /* Get value ranges for each operand. For constant operands, create
3699 a new value range with the operand to simplify processing. */
3700 op0
= gimple_assign_rhs2 (stmt
);
3701 if (TREE_CODE (op0
) == SSA_NAME
)
3702 vr0
= *(get_value_range (op0
));
3703 else if (is_gimple_min_invariant (op0
))
3704 set_value_range_to_value (&vr0
, op0
, NULL
);
3706 set_value_range_to_varying (&vr0
);
3708 op1
= gimple_assign_rhs3 (stmt
);
3709 if (TREE_CODE (op1
) == SSA_NAME
)
3710 vr1
= *(get_value_range (op1
));
3711 else if (is_gimple_min_invariant (op1
))
3712 set_value_range_to_value (&vr1
, op1
, NULL
);
3714 set_value_range_to_varying (&vr1
);
3716 /* The resulting value range is the union of the operand ranges */
3717 copy_value_range (vr
, &vr0
);
3718 vrp_meet (vr
, &vr1
);
3722 /* Extract range information from a comparison expression EXPR based
3723 on the range of its operand and the expression code. */
3726 extract_range_from_comparison (value_range_t
*vr
, enum tree_code code
,
3727 tree type
, tree op0
, tree op1
)
3732 val
= vrp_evaluate_conditional_warnv_with_ops (code
, op0
, op1
, false, &sop
,
3735 /* A disadvantage of using a special infinity as an overflow
3736 representation is that we lose the ability to record overflow
3737 when we don't have an infinity. So we have to ignore a result
3738 which relies on overflow. */
3740 if (val
&& !is_overflow_infinity (val
) && !sop
)
3742 /* Since this expression was found on the RHS of an assignment,
3743 its type may be different from _Bool. Convert VAL to EXPR's
3745 val
= fold_convert (type
, val
);
3746 if (is_gimple_min_invariant (val
))
3747 set_value_range_to_value (vr
, val
, vr
->equiv
);
3749 set_value_range (vr
, VR_RANGE
, val
, val
, vr
->equiv
);
3752 /* The result of a comparison is always true or false. */
3753 set_value_range_to_truthvalue (vr
, type
);
3756 /* Try to derive a nonnegative or nonzero range out of STMT relying
3757 primarily on generic routines in fold in conjunction with range data.
3758 Store the result in *VR */
3761 extract_range_basic (value_range_t
*vr
, gimple stmt
)
3764 tree type
= gimple_expr_type (stmt
);
3766 if (gimple_call_builtin_p (stmt
, BUILT_IN_NORMAL
))
3768 tree fndecl
= gimple_call_fndecl (stmt
), arg
;
3769 int mini
, maxi
, zerov
= 0, prec
;
3771 switch (DECL_FUNCTION_CODE (fndecl
))
3773 case BUILT_IN_CONSTANT_P
:
3774 /* If the call is __builtin_constant_p and the argument is a
3775 function parameter resolve it to false. This avoids bogus
3776 array bound warnings.
3777 ??? We could do this as early as inlining is finished. */
3778 arg
= gimple_call_arg (stmt
, 0);
3779 if (TREE_CODE (arg
) == SSA_NAME
3780 && SSA_NAME_IS_DEFAULT_DEF (arg
)
3781 && TREE_CODE (SSA_NAME_VAR (arg
)) == PARM_DECL
)
3783 set_value_range_to_null (vr
, type
);
3787 /* Both __builtin_ffs* and __builtin_popcount return
3789 CASE_INT_FN (BUILT_IN_FFS
):
3790 CASE_INT_FN (BUILT_IN_POPCOUNT
):
3791 arg
= gimple_call_arg (stmt
, 0);
3792 prec
= TYPE_PRECISION (TREE_TYPE (arg
));
3795 if (TREE_CODE (arg
) == SSA_NAME
)
3797 value_range_t
*vr0
= get_value_range (arg
);
3798 /* If arg is non-zero, then ffs or popcount
3800 if (((vr0
->type
== VR_RANGE
3801 && range_includes_zero_p (vr0
->min
, vr0
->max
) == 0)
3802 || (vr0
->type
== VR_ANTI_RANGE
3803 && range_includes_zero_p (vr0
->min
, vr0
->max
) == 1))
3804 && !is_overflow_infinity (vr0
->min
)
3805 && !is_overflow_infinity (vr0
->max
))
3807 /* If some high bits are known to be zero,
3808 we can decrease the maximum. */
3809 if (vr0
->type
== VR_RANGE
3810 && TREE_CODE (vr0
->max
) == INTEGER_CST
3811 && !operand_less_p (vr0
->min
,
3812 build_zero_cst (TREE_TYPE (vr0
->min
)))
3813 && !is_overflow_infinity (vr0
->max
))
3814 maxi
= tree_floor_log2 (vr0
->max
) + 1;
3817 /* __builtin_parity* returns [0, 1]. */
3818 CASE_INT_FN (BUILT_IN_PARITY
):
3822 /* __builtin_c[lt]z* return [0, prec-1], except for
3823 when the argument is 0, but that is undefined behavior.
3824 On many targets where the CLZ RTL or optab value is defined
3825 for 0 the value is prec, so include that in the range
3827 CASE_INT_FN (BUILT_IN_CLZ
):
3828 arg
= gimple_call_arg (stmt
, 0);
3829 prec
= TYPE_PRECISION (TREE_TYPE (arg
));
3832 if (optab_handler (clz_optab
, TYPE_MODE (TREE_TYPE (arg
)))
3834 && CLZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (TREE_TYPE (arg
)),
3836 /* Handle only the single common value. */
3838 /* Magic value to give up, unless vr0 proves
3841 if (TREE_CODE (arg
) == SSA_NAME
)
3843 value_range_t
*vr0
= get_value_range (arg
);
3844 /* From clz of VR_RANGE minimum we can compute
3846 if (vr0
->type
== VR_RANGE
3847 && TREE_CODE (vr0
->min
) == INTEGER_CST
3848 && !is_overflow_infinity (vr0
->min
))
3850 maxi
= prec
- 1 - tree_floor_log2 (vr0
->min
);
3854 else if (vr0
->type
== VR_ANTI_RANGE
3855 && integer_zerop (vr0
->min
)
3856 && !is_overflow_infinity (vr0
->min
))
3863 /* From clz of VR_RANGE maximum we can compute
3865 if (vr0
->type
== VR_RANGE
3866 && TREE_CODE (vr0
->max
) == INTEGER_CST
3867 && !is_overflow_infinity (vr0
->max
))
3869 mini
= prec
- 1 - tree_floor_log2 (vr0
->max
);
3877 /* __builtin_ctz* return [0, prec-1], except for
3878 when the argument is 0, but that is undefined behavior.
3879 If there is a ctz optab for this mode and
3880 CTZ_DEFINED_VALUE_AT_ZERO, include that in the range,
3881 otherwise just assume 0 won't be seen. */
3882 CASE_INT_FN (BUILT_IN_CTZ
):
3883 arg
= gimple_call_arg (stmt
, 0);
3884 prec
= TYPE_PRECISION (TREE_TYPE (arg
));
3887 if (optab_handler (ctz_optab
, TYPE_MODE (TREE_TYPE (arg
)))
3889 && CTZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (TREE_TYPE (arg
)),
3892 /* Handle only the two common values. */
3895 else if (zerov
== prec
)
3898 /* Magic value to give up, unless vr0 proves
3902 if (TREE_CODE (arg
) == SSA_NAME
)
3904 value_range_t
*vr0
= get_value_range (arg
);
3905 /* If arg is non-zero, then use [0, prec - 1]. */
3906 if (((vr0
->type
== VR_RANGE
3907 && integer_nonzerop (vr0
->min
))
3908 || (vr0
->type
== VR_ANTI_RANGE
3909 && integer_zerop (vr0
->min
)))
3910 && !is_overflow_infinity (vr0
->min
))
3915 /* If some high bits are known to be zero,
3916 we can decrease the result maximum. */
3917 if (vr0
->type
== VR_RANGE
3918 && TREE_CODE (vr0
->max
) == INTEGER_CST
3919 && !is_overflow_infinity (vr0
->max
))
3921 maxi
= tree_floor_log2 (vr0
->max
);
3922 /* For vr0 [0, 0] give up. */
3930 /* __builtin_clrsb* returns [0, prec-1]. */
3931 CASE_INT_FN (BUILT_IN_CLRSB
):
3932 arg
= gimple_call_arg (stmt
, 0);
3933 prec
= TYPE_PRECISION (TREE_TYPE (arg
));
3938 set_value_range (vr
, VR_RANGE
, build_int_cst (type
, mini
),
3939 build_int_cst (type
, maxi
), NULL
);
3945 else if (is_gimple_call (stmt
)
3946 && gimple_call_internal_p (stmt
))
3948 enum tree_code subcode
= ERROR_MARK
;
3949 switch (gimple_call_internal_fn (stmt
))
3951 case IFN_UBSAN_CHECK_ADD
:
3952 subcode
= PLUS_EXPR
;
3954 case IFN_UBSAN_CHECK_SUB
:
3955 subcode
= MINUS_EXPR
;
3957 case IFN_UBSAN_CHECK_MUL
:
3958 subcode
= MULT_EXPR
;
3963 if (subcode
!= ERROR_MARK
)
3965 bool saved_flag_wrapv
= flag_wrapv
;
3966 /* Pretend the arithmetics is wrapping. If there is
3967 any overflow, we'll complain, but will actually do
3968 wrapping operation. */
3970 extract_range_from_binary_expr (vr
, subcode
, type
,
3971 gimple_call_arg (stmt
, 0),
3972 gimple_call_arg (stmt
, 1));
3973 flag_wrapv
= saved_flag_wrapv
;
3975 /* If for both arguments vrp_valueize returned non-NULL,
3976 this should have been already folded and if not, it
3977 wasn't folded because of overflow. Avoid removing the
3978 UBSAN_CHECK_* calls in that case. */
3979 if (vr
->type
== VR_RANGE
3980 && (vr
->min
== vr
->max
3981 || operand_equal_p (vr
->min
, vr
->max
, 0)))
3982 set_value_range_to_varying (vr
);
3986 if (INTEGRAL_TYPE_P (type
)
3987 && gimple_stmt_nonnegative_warnv_p (stmt
, &sop
))
3988 set_value_range_to_nonnegative (vr
, type
,
3989 sop
|| stmt_overflow_infinity (stmt
));
3990 else if (vrp_stmt_computes_nonzero (stmt
, &sop
)
3992 set_value_range_to_nonnull (vr
, type
);
3994 set_value_range_to_varying (vr
);
3998 /* Try to compute a useful range out of assignment STMT and store it
4002 extract_range_from_assignment (value_range_t
*vr
, gassign
*stmt
)
4004 enum tree_code code
= gimple_assign_rhs_code (stmt
);
4006 if (code
== ASSERT_EXPR
)
4007 extract_range_from_assert (vr
, gimple_assign_rhs1 (stmt
));
4008 else if (code
== SSA_NAME
)
4009 extract_range_from_ssa_name (vr
, gimple_assign_rhs1 (stmt
));
4010 else if (TREE_CODE_CLASS (code
) == tcc_binary
)
4011 extract_range_from_binary_expr (vr
, gimple_assign_rhs_code (stmt
),
4012 gimple_expr_type (stmt
),
4013 gimple_assign_rhs1 (stmt
),
4014 gimple_assign_rhs2 (stmt
));
4015 else if (TREE_CODE_CLASS (code
) == tcc_unary
)
4016 extract_range_from_unary_expr (vr
, gimple_assign_rhs_code (stmt
),
4017 gimple_expr_type (stmt
),
4018 gimple_assign_rhs1 (stmt
));
4019 else if (code
== COND_EXPR
)
4020 extract_range_from_cond_expr (vr
, stmt
);
4021 else if (TREE_CODE_CLASS (code
) == tcc_comparison
)
4022 extract_range_from_comparison (vr
, gimple_assign_rhs_code (stmt
),
4023 gimple_expr_type (stmt
),
4024 gimple_assign_rhs1 (stmt
),
4025 gimple_assign_rhs2 (stmt
));
4026 else if (get_gimple_rhs_class (code
) == GIMPLE_SINGLE_RHS
4027 && is_gimple_min_invariant (gimple_assign_rhs1 (stmt
)))
4028 set_value_range_to_value (vr
, gimple_assign_rhs1 (stmt
), NULL
);
4030 set_value_range_to_varying (vr
);
4032 if (vr
->type
== VR_VARYING
)
4033 extract_range_basic (vr
, stmt
);
4036 /* Given a range VR, a LOOP and a variable VAR, determine whether it
4037 would be profitable to adjust VR using scalar evolution information
4038 for VAR. If so, update VR with the new limits. */
4041 adjust_range_with_scev (value_range_t
*vr
, struct loop
*loop
,
4042 gimple stmt
, tree var
)
4044 tree init
, step
, chrec
, tmin
, tmax
, min
, max
, type
, tem
;
4045 enum ev_direction dir
;
4047 /* TODO. Don't adjust anti-ranges. An anti-range may provide
4048 better opportunities than a regular range, but I'm not sure. */
4049 if (vr
->type
== VR_ANTI_RANGE
)
4052 chrec
= instantiate_parameters (loop
, analyze_scalar_evolution (loop
, var
));
4054 /* Like in PR19590, scev can return a constant function. */
4055 if (is_gimple_min_invariant (chrec
))
4057 set_value_range_to_value (vr
, chrec
, vr
->equiv
);
4061 if (TREE_CODE (chrec
) != POLYNOMIAL_CHREC
)
4064 init
= initial_condition_in_loop_num (chrec
, loop
->num
);
4065 tem
= op_with_constant_singleton_value_range (init
);
4068 step
= evolution_part_in_loop_num (chrec
, loop
->num
);
4069 tem
= op_with_constant_singleton_value_range (step
);
4073 /* If STEP is symbolic, we can't know whether INIT will be the
4074 minimum or maximum value in the range. Also, unless INIT is
4075 a simple expression, compare_values and possibly other functions
4076 in tree-vrp won't be able to handle it. */
4077 if (step
== NULL_TREE
4078 || !is_gimple_min_invariant (step
)
4079 || !valid_value_p (init
))
4082 dir
= scev_direction (chrec
);
4083 if (/* Do not adjust ranges if we do not know whether the iv increases
4084 or decreases, ... */
4085 dir
== EV_DIR_UNKNOWN
4086 /* ... or if it may wrap. */
4087 || scev_probably_wraps_p (init
, step
, stmt
, get_chrec_loop (chrec
),
4091 /* We use TYPE_MIN_VALUE and TYPE_MAX_VALUE here instead of
4092 negative_overflow_infinity and positive_overflow_infinity,
4093 because we have concluded that the loop probably does not
4096 type
= TREE_TYPE (var
);
4097 if (POINTER_TYPE_P (type
) || !TYPE_MIN_VALUE (type
))
4098 tmin
= lower_bound_in_type (type
, type
);
4100 tmin
= TYPE_MIN_VALUE (type
);
4101 if (POINTER_TYPE_P (type
) || !TYPE_MAX_VALUE (type
))
4102 tmax
= upper_bound_in_type (type
, type
);
4104 tmax
= TYPE_MAX_VALUE (type
);
4106 /* Try to use estimated number of iterations for the loop to constrain the
4107 final value in the evolution. */
4108 if (TREE_CODE (step
) == INTEGER_CST
4109 && is_gimple_val (init
)
4110 && (TREE_CODE (init
) != SSA_NAME
4111 || get_value_range (init
)->type
== VR_RANGE
))
4115 /* We are only entering here for loop header PHI nodes, so using
4116 the number of latch executions is the correct thing to use. */
4117 if (max_loop_iterations (loop
, &nit
))
4119 value_range_t maxvr
= VR_INITIALIZER
;
4120 signop sgn
= TYPE_SIGN (TREE_TYPE (step
));
4123 widest_int wtmp
= wi::mul (wi::to_widest (step
), nit
, sgn
,
4125 /* If the multiplication overflowed we can't do a meaningful
4126 adjustment. Likewise if the result doesn't fit in the type
4127 of the induction variable. For a signed type we have to
4128 check whether the result has the expected signedness which
4129 is that of the step as number of iterations is unsigned. */
4131 && wi::fits_to_tree_p (wtmp
, TREE_TYPE (init
))
4133 || wi::gts_p (wtmp
, 0) == wi::gts_p (step
, 0)))
4135 tem
= wide_int_to_tree (TREE_TYPE (init
), wtmp
);
4136 extract_range_from_binary_expr (&maxvr
, PLUS_EXPR
,
4137 TREE_TYPE (init
), init
, tem
);
4138 /* Likewise if the addition did. */
4139 if (maxvr
.type
== VR_RANGE
)
4148 if (vr
->type
== VR_VARYING
|| vr
->type
== VR_UNDEFINED
)
4153 /* For VARYING or UNDEFINED ranges, just about anything we get
4154 from scalar evolutions should be better. */
4156 if (dir
== EV_DIR_DECREASES
)
4161 else if (vr
->type
== VR_RANGE
)
4166 if (dir
== EV_DIR_DECREASES
)
4168 /* INIT is the maximum value. If INIT is lower than VR->MAX
4169 but no smaller than VR->MIN, set VR->MAX to INIT. */
4170 if (compare_values (init
, max
) == -1)
4173 /* According to the loop information, the variable does not
4174 overflow. If we think it does, probably because of an
4175 overflow due to arithmetic on a different INF value,
4177 if (is_negative_overflow_infinity (min
)
4178 || compare_values (min
, tmin
) == -1)
4184 /* If INIT is bigger than VR->MIN, set VR->MIN to INIT. */
4185 if (compare_values (init
, min
) == 1)
4188 if (is_positive_overflow_infinity (max
)
4189 || compare_values (tmax
, max
) == -1)
4196 /* If we just created an invalid range with the minimum
4197 greater than the maximum, we fail conservatively.
4198 This should happen only in unreachable
4199 parts of code, or for invalid programs. */
4200 if (compare_values (min
, max
) == 1
4201 || (is_negative_overflow_infinity (min
)
4202 && is_positive_overflow_infinity (max
)))
4205 set_value_range (vr
, VR_RANGE
, min
, max
, vr
->equiv
);
4209 /* Given two numeric value ranges VR0, VR1 and a comparison code COMP:
4211 - Return BOOLEAN_TRUE_NODE if VR0 COMP VR1 always returns true for
4212 all the values in the ranges.
4214 - Return BOOLEAN_FALSE_NODE if the comparison always returns false.
4216 - Return NULL_TREE if it is not always possible to determine the
4217 value of the comparison.
4219 Also set *STRICT_OVERFLOW_P to indicate whether a range with an
4220 overflow infinity was used in the test. */
4224 compare_ranges (enum tree_code comp
, value_range_t
*vr0
, value_range_t
*vr1
,
4225 bool *strict_overflow_p
)
4227 /* VARYING or UNDEFINED ranges cannot be compared. */
4228 if (vr0
->type
== VR_VARYING
4229 || vr0
->type
== VR_UNDEFINED
4230 || vr1
->type
== VR_VARYING
4231 || vr1
->type
== VR_UNDEFINED
)
4234 /* Anti-ranges need to be handled separately. */
4235 if (vr0
->type
== VR_ANTI_RANGE
|| vr1
->type
== VR_ANTI_RANGE
)
4237 /* If both are anti-ranges, then we cannot compute any
4239 if (vr0
->type
== VR_ANTI_RANGE
&& vr1
->type
== VR_ANTI_RANGE
)
4242 /* These comparisons are never statically computable. */
4249 /* Equality can be computed only between a range and an
4250 anti-range. ~[VAL1, VAL2] == [VAL1, VAL2] is always false. */
4251 if (vr0
->type
== VR_RANGE
)
4253 /* To simplify processing, make VR0 the anti-range. */
4254 value_range_t
*tmp
= vr0
;
4259 gcc_assert (comp
== NE_EXPR
|| comp
== EQ_EXPR
);
4261 if (compare_values_warnv (vr0
->min
, vr1
->min
, strict_overflow_p
) == 0
4262 && compare_values_warnv (vr0
->max
, vr1
->max
, strict_overflow_p
) == 0)
4263 return (comp
== NE_EXPR
) ? boolean_true_node
: boolean_false_node
;
4268 if (!usable_range_p (vr0
, strict_overflow_p
)
4269 || !usable_range_p (vr1
, strict_overflow_p
))
4272 /* Simplify processing. If COMP is GT_EXPR or GE_EXPR, switch the
4273 operands around and change the comparison code. */
4274 if (comp
== GT_EXPR
|| comp
== GE_EXPR
)
4277 comp
= (comp
== GT_EXPR
) ? LT_EXPR
: LE_EXPR
;
4283 if (comp
== EQ_EXPR
)
4285 /* Equality may only be computed if both ranges represent
4286 exactly one value. */
4287 if (compare_values_warnv (vr0
->min
, vr0
->max
, strict_overflow_p
) == 0
4288 && compare_values_warnv (vr1
->min
, vr1
->max
, strict_overflow_p
) == 0)
4290 int cmp_min
= compare_values_warnv (vr0
->min
, vr1
->min
,
4292 int cmp_max
= compare_values_warnv (vr0
->max
, vr1
->max
,
4294 if (cmp_min
== 0 && cmp_max
== 0)
4295 return boolean_true_node
;
4296 else if (cmp_min
!= -2 && cmp_max
!= -2)
4297 return boolean_false_node
;
4299 /* If [V0_MIN, V1_MAX] < [V1_MIN, V1_MAX] then V0 != V1. */
4300 else if (compare_values_warnv (vr0
->min
, vr1
->max
,
4301 strict_overflow_p
) == 1
4302 || compare_values_warnv (vr1
->min
, vr0
->max
,
4303 strict_overflow_p
) == 1)
4304 return boolean_false_node
;
4308 else if (comp
== NE_EXPR
)
4312 /* If VR0 is completely to the left or completely to the right
4313 of VR1, they are always different. Notice that we need to
4314 make sure that both comparisons yield similar results to
4315 avoid comparing values that cannot be compared at
4317 cmp1
= compare_values_warnv (vr0
->max
, vr1
->min
, strict_overflow_p
);
4318 cmp2
= compare_values_warnv (vr0
->min
, vr1
->max
, strict_overflow_p
);
4319 if ((cmp1
== -1 && cmp2
== -1) || (cmp1
== 1 && cmp2
== 1))
4320 return boolean_true_node
;
4322 /* If VR0 and VR1 represent a single value and are identical,
4324 else if (compare_values_warnv (vr0
->min
, vr0
->max
,
4325 strict_overflow_p
) == 0
4326 && compare_values_warnv (vr1
->min
, vr1
->max
,
4327 strict_overflow_p
) == 0
4328 && compare_values_warnv (vr0
->min
, vr1
->min
,
4329 strict_overflow_p
) == 0
4330 && compare_values_warnv (vr0
->max
, vr1
->max
,
4331 strict_overflow_p
) == 0)
4332 return boolean_false_node
;
4334 /* Otherwise, they may or may not be different. */
4338 else if (comp
== LT_EXPR
|| comp
== LE_EXPR
)
4342 /* If VR0 is to the left of VR1, return true. */
4343 tst
= compare_values_warnv (vr0
->max
, vr1
->min
, strict_overflow_p
);
4344 if ((comp
== LT_EXPR
&& tst
== -1)
4345 || (comp
== LE_EXPR
&& (tst
== -1 || tst
== 0)))
4347 if (overflow_infinity_range_p (vr0
)
4348 || overflow_infinity_range_p (vr1
))
4349 *strict_overflow_p
= true;
4350 return boolean_true_node
;
4353 /* If VR0 is to the right of VR1, return false. */
4354 tst
= compare_values_warnv (vr0
->min
, vr1
->max
, strict_overflow_p
);
4355 if ((comp
== LT_EXPR
&& (tst
== 0 || tst
== 1))
4356 || (comp
== LE_EXPR
&& tst
== 1))
4358 if (overflow_infinity_range_p (vr0
)
4359 || overflow_infinity_range_p (vr1
))
4360 *strict_overflow_p
= true;
4361 return boolean_false_node
;
4364 /* Otherwise, we don't know. */
4372 /* Given a value range VR, a value VAL and a comparison code COMP, return
4373 BOOLEAN_TRUE_NODE if VR COMP VAL always returns true for all the
4374 values in VR. Return BOOLEAN_FALSE_NODE if the comparison
4375 always returns false. Return NULL_TREE if it is not always
4376 possible to determine the value of the comparison. Also set
4377 *STRICT_OVERFLOW_P to indicate whether a range with an overflow
4378 infinity was used in the test. */
4381 compare_range_with_value (enum tree_code comp
, value_range_t
*vr
, tree val
,
4382 bool *strict_overflow_p
)
4384 if (vr
->type
== VR_VARYING
|| vr
->type
== VR_UNDEFINED
)
4387 /* Anti-ranges need to be handled separately. */
4388 if (vr
->type
== VR_ANTI_RANGE
)
4390 /* For anti-ranges, the only predicates that we can compute at
4391 compile time are equality and inequality. */
4398 /* ~[VAL_1, VAL_2] OP VAL is known if VAL_1 <= VAL <= VAL_2. */
4399 if (value_inside_range (val
, vr
->min
, vr
->max
) == 1)
4400 return (comp
== NE_EXPR
) ? boolean_true_node
: boolean_false_node
;
4405 if (!usable_range_p (vr
, strict_overflow_p
))
4408 if (comp
== EQ_EXPR
)
4410 /* EQ_EXPR may only be computed if VR represents exactly
4412 if (compare_values_warnv (vr
->min
, vr
->max
, strict_overflow_p
) == 0)
4414 int cmp
= compare_values_warnv (vr
->min
, val
, strict_overflow_p
);
4416 return boolean_true_node
;
4417 else if (cmp
== -1 || cmp
== 1 || cmp
== 2)
4418 return boolean_false_node
;
4420 else if (compare_values_warnv (val
, vr
->min
, strict_overflow_p
) == -1
4421 || compare_values_warnv (vr
->max
, val
, strict_overflow_p
) == -1)
4422 return boolean_false_node
;
4426 else if (comp
== NE_EXPR
)
4428 /* If VAL is not inside VR, then they are always different. */
4429 if (compare_values_warnv (vr
->max
, val
, strict_overflow_p
) == -1
4430 || compare_values_warnv (vr
->min
, val
, strict_overflow_p
) == 1)
4431 return boolean_true_node
;
4433 /* If VR represents exactly one value equal to VAL, then return
4435 if (compare_values_warnv (vr
->min
, vr
->max
, strict_overflow_p
) == 0
4436 && compare_values_warnv (vr
->min
, val
, strict_overflow_p
) == 0)
4437 return boolean_false_node
;
4439 /* Otherwise, they may or may not be different. */
4442 else if (comp
== LT_EXPR
|| comp
== LE_EXPR
)
4446 /* If VR is to the left of VAL, return true. */
4447 tst
= compare_values_warnv (vr
->max
, val
, strict_overflow_p
);
4448 if ((comp
== LT_EXPR
&& tst
== -1)
4449 || (comp
== LE_EXPR
&& (tst
== -1 || tst
== 0)))
4451 if (overflow_infinity_range_p (vr
))
4452 *strict_overflow_p
= true;
4453 return boolean_true_node
;
4456 /* If VR is to the right of VAL, return false. */
4457 tst
= compare_values_warnv (vr
->min
, val
, strict_overflow_p
);
4458 if ((comp
== LT_EXPR
&& (tst
== 0 || tst
== 1))
4459 || (comp
== LE_EXPR
&& tst
== 1))
4461 if (overflow_infinity_range_p (vr
))
4462 *strict_overflow_p
= true;
4463 return boolean_false_node
;
4466 /* Otherwise, we don't know. */
4469 else if (comp
== GT_EXPR
|| comp
== GE_EXPR
)
4473 /* If VR is to the right of VAL, return true. */
4474 tst
= compare_values_warnv (vr
->min
, val
, strict_overflow_p
);
4475 if ((comp
== GT_EXPR
&& tst
== 1)
4476 || (comp
== GE_EXPR
&& (tst
== 0 || tst
== 1)))
4478 if (overflow_infinity_range_p (vr
))
4479 *strict_overflow_p
= true;
4480 return boolean_true_node
;
4483 /* If VR is to the left of VAL, return false. */
4484 tst
= compare_values_warnv (vr
->max
, val
, strict_overflow_p
);
4485 if ((comp
== GT_EXPR
&& (tst
== -1 || tst
== 0))
4486 || (comp
== GE_EXPR
&& tst
== -1))
4488 if (overflow_infinity_range_p (vr
))
4489 *strict_overflow_p
= true;
4490 return boolean_false_node
;
4493 /* Otherwise, we don't know. */
4501 /* Debugging dumps. */
4503 void dump_value_range (FILE *, value_range_t
*);
4504 void debug_value_range (value_range_t
*);
4505 void dump_all_value_ranges (FILE *);
4506 void debug_all_value_ranges (void);
4507 void dump_vr_equiv (FILE *, bitmap
);
4508 void debug_vr_equiv (bitmap
);
4511 /* Dump value range VR to FILE. */
4514 dump_value_range (FILE *file
, value_range_t
*vr
)
4517 fprintf (file
, "[]");
4518 else if (vr
->type
== VR_UNDEFINED
)
4519 fprintf (file
, "UNDEFINED");
4520 else if (vr
->type
== VR_RANGE
|| vr
->type
== VR_ANTI_RANGE
)
4522 tree type
= TREE_TYPE (vr
->min
);
4524 fprintf (file
, "%s[", (vr
->type
== VR_ANTI_RANGE
) ? "~" : "");
4526 if (is_negative_overflow_infinity (vr
->min
))
4527 fprintf (file
, "-INF(OVF)");
4528 else if (INTEGRAL_TYPE_P (type
)
4529 && !TYPE_UNSIGNED (type
)
4530 && vrp_val_is_min (vr
->min
))
4531 fprintf (file
, "-INF");
4533 print_generic_expr (file
, vr
->min
, 0);
4535 fprintf (file
, ", ");
4537 if (is_positive_overflow_infinity (vr
->max
))
4538 fprintf (file
, "+INF(OVF)");
4539 else if (INTEGRAL_TYPE_P (type
)
4540 && vrp_val_is_max (vr
->max
))
4541 fprintf (file
, "+INF");
4543 print_generic_expr (file
, vr
->max
, 0);
4545 fprintf (file
, "]");
4552 fprintf (file
, " EQUIVALENCES: { ");
4554 EXECUTE_IF_SET_IN_BITMAP (vr
->equiv
, 0, i
, bi
)
4556 print_generic_expr (file
, ssa_name (i
), 0);
4557 fprintf (file
, " ");
4561 fprintf (file
, "} (%u elements)", c
);
4564 else if (vr
->type
== VR_VARYING
)
4565 fprintf (file
, "VARYING");
4567 fprintf (file
, "INVALID RANGE");
4571 /* Dump value range VR to stderr. */
4574 debug_value_range (value_range_t
*vr
)
4576 dump_value_range (stderr
, vr
);
4577 fprintf (stderr
, "\n");
4581 /* Dump value ranges of all SSA_NAMEs to FILE. */
4584 dump_all_value_ranges (FILE *file
)
4588 for (i
= 0; i
< num_vr_values
; i
++)
4592 print_generic_expr (file
, ssa_name (i
), 0);
4593 fprintf (file
, ": ");
4594 dump_value_range (file
, vr_value
[i
]);
4595 fprintf (file
, "\n");
4599 fprintf (file
, "\n");
4603 /* Dump all value ranges to stderr. */
4606 debug_all_value_ranges (void)
4608 dump_all_value_ranges (stderr
);
4612 /* Given a COND_EXPR COND of the form 'V OP W', and an SSA name V,
4613 create a new SSA name N and return the assertion assignment
4614 'N = ASSERT_EXPR <V, V OP W>'. */
4617 build_assert_expr_for (tree cond
, tree v
)
4622 gcc_assert (TREE_CODE (v
) == SSA_NAME
4623 && COMPARISON_CLASS_P (cond
));
4625 a
= build2 (ASSERT_EXPR
, TREE_TYPE (v
), v
, cond
);
4626 assertion
= gimple_build_assign (NULL_TREE
, a
);
4628 /* The new ASSERT_EXPR, creates a new SSA name that replaces the
4629 operand of the ASSERT_EXPR. Create it so the new name and the old one
4630 are registered in the replacement table so that we can fix the SSA web
4631 after adding all the ASSERT_EXPRs. */
4632 create_new_def_for (v
, assertion
, NULL
);
4638 /* Return false if EXPR is a predicate expression involving floating
4642 fp_predicate (gimple stmt
)
4644 GIMPLE_CHECK (stmt
, GIMPLE_COND
);
4646 return FLOAT_TYPE_P (TREE_TYPE (gimple_cond_lhs (stmt
)));
4649 /* If the range of values taken by OP can be inferred after STMT executes,
4650 return the comparison code (COMP_CODE_P) and value (VAL_P) that
4651 describes the inferred range. Return true if a range could be
4655 infer_value_range (gimple stmt
, tree op
, enum tree_code
*comp_code_p
, tree
*val_p
)
4658 *comp_code_p
= ERROR_MARK
;
4660 /* Do not attempt to infer anything in names that flow through
4662 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op
))
4665 /* Similarly, don't infer anything from statements that may throw
4666 exceptions. ??? Relax this requirement? */
4667 if (stmt_could_throw_p (stmt
))
4670 /* If STMT is the last statement of a basic block with no normal
4671 successors, there is no point inferring anything about any of its
4672 operands. We would not be able to find a proper insertion point
4673 for the assertion, anyway. */
4674 if (stmt_ends_bb_p (stmt
))
4679 FOR_EACH_EDGE (e
, ei
, gimple_bb (stmt
)->succs
)
4680 if (!(e
->flags
& EDGE_ABNORMAL
))
4686 if (infer_nonnull_range (stmt
, op
, true, true))
4688 *val_p
= build_int_cst (TREE_TYPE (op
), 0);
4689 *comp_code_p
= NE_EXPR
;
4697 void dump_asserts_for (FILE *, tree
);
4698 void debug_asserts_for (tree
);
4699 void dump_all_asserts (FILE *);
4700 void debug_all_asserts (void);
4702 /* Dump all the registered assertions for NAME to FILE. */
4705 dump_asserts_for (FILE *file
, tree name
)
4709 fprintf (file
, "Assertions to be inserted for ");
4710 print_generic_expr (file
, name
, 0);
4711 fprintf (file
, "\n");
4713 loc
= asserts_for
[SSA_NAME_VERSION (name
)];
4716 fprintf (file
, "\t");
4717 print_gimple_stmt (file
, gsi_stmt (loc
->si
), 0, 0);
4718 fprintf (file
, "\n\tBB #%d", loc
->bb
->index
);
4721 fprintf (file
, "\n\tEDGE %d->%d", loc
->e
->src
->index
,
4722 loc
->e
->dest
->index
);
4723 dump_edge_info (file
, loc
->e
, dump_flags
, 0);
4725 fprintf (file
, "\n\tPREDICATE: ");
4726 print_generic_expr (file
, name
, 0);
4727 fprintf (file
, " %s ", get_tree_code_name (loc
->comp_code
));
4728 print_generic_expr (file
, loc
->val
, 0);
4729 fprintf (file
, "\n\n");
4733 fprintf (file
, "\n");
4737 /* Dump all the registered assertions for NAME to stderr. */
4740 debug_asserts_for (tree name
)
4742 dump_asserts_for (stderr
, name
);
4746 /* Dump all the registered assertions for all the names to FILE. */
4749 dump_all_asserts (FILE *file
)
4754 fprintf (file
, "\nASSERT_EXPRs to be inserted\n\n");
4755 EXECUTE_IF_SET_IN_BITMAP (need_assert_for
, 0, i
, bi
)
4756 dump_asserts_for (file
, ssa_name (i
));
4757 fprintf (file
, "\n");
4761 /* Dump all the registered assertions for all the names to stderr. */
4764 debug_all_asserts (void)
4766 dump_all_asserts (stderr
);
4770 /* If NAME doesn't have an ASSERT_EXPR registered for asserting
4771 'EXPR COMP_CODE VAL' at a location that dominates block BB or
4772 E->DEST, then register this location as a possible insertion point
4773 for ASSERT_EXPR <NAME, EXPR COMP_CODE VAL>.
4775 BB, E and SI provide the exact insertion point for the new
4776 ASSERT_EXPR. If BB is NULL, then the ASSERT_EXPR is to be inserted
4777 on edge E. Otherwise, if E is NULL, the ASSERT_EXPR is inserted on
4778 BB. If SI points to a COND_EXPR or a SWITCH_EXPR statement, then E
4779 must not be NULL. */
4782 register_new_assert_for (tree name
, tree expr
,
4783 enum tree_code comp_code
,
4787 gimple_stmt_iterator si
)
4789 assert_locus_t n
, loc
, last_loc
;
4790 basic_block dest_bb
;
4792 gcc_checking_assert (bb
== NULL
|| e
== NULL
);
4795 gcc_checking_assert (gimple_code (gsi_stmt (si
)) != GIMPLE_COND
4796 && gimple_code (gsi_stmt (si
)) != GIMPLE_SWITCH
);
4798 /* Never build an assert comparing against an integer constant with
4799 TREE_OVERFLOW set. This confuses our undefined overflow warning
4801 if (TREE_OVERFLOW_P (val
))
4802 val
= drop_tree_overflow (val
);
4804 /* The new assertion A will be inserted at BB or E. We need to
4805 determine if the new location is dominated by a previously
4806 registered location for A. If we are doing an edge insertion,
4807 assume that A will be inserted at E->DEST. Note that this is not
4810 If E is a critical edge, it will be split. But even if E is
4811 split, the new block will dominate the same set of blocks that
4814 The reverse, however, is not true, blocks dominated by E->DEST
4815 will not be dominated by the new block created to split E. So,
4816 if the insertion location is on a critical edge, we will not use
4817 the new location to move another assertion previously registered
4818 at a block dominated by E->DEST. */
4819 dest_bb
= (bb
) ? bb
: e
->dest
;
4821 /* If NAME already has an ASSERT_EXPR registered for COMP_CODE and
4822 VAL at a block dominating DEST_BB, then we don't need to insert a new
4823 one. Similarly, if the same assertion already exists at a block
4824 dominated by DEST_BB and the new location is not on a critical
4825 edge, then update the existing location for the assertion (i.e.,
4826 move the assertion up in the dominance tree).
4828 Note, this is implemented as a simple linked list because there
4829 should not be more than a handful of assertions registered per
4830 name. If this becomes a performance problem, a table hashed by
4831 COMP_CODE and VAL could be implemented. */
4832 loc
= asserts_for
[SSA_NAME_VERSION (name
)];
4836 if (loc
->comp_code
== comp_code
4838 || operand_equal_p (loc
->val
, val
, 0))
4839 && (loc
->expr
== expr
4840 || operand_equal_p (loc
->expr
, expr
, 0)))
4842 /* If E is not a critical edge and DEST_BB
4843 dominates the existing location for the assertion, move
4844 the assertion up in the dominance tree by updating its
4845 location information. */
4846 if ((e
== NULL
|| !EDGE_CRITICAL_P (e
))
4847 && dominated_by_p (CDI_DOMINATORS
, loc
->bb
, dest_bb
))
4856 /* Update the last node of the list and move to the next one. */
4861 /* If we didn't find an assertion already registered for
4862 NAME COMP_CODE VAL, add a new one at the end of the list of
4863 assertions associated with NAME. */
4864 n
= XNEW (struct assert_locus_d
);
4868 n
->comp_code
= comp_code
;
4876 asserts_for
[SSA_NAME_VERSION (name
)] = n
;
4878 bitmap_set_bit (need_assert_for
, SSA_NAME_VERSION (name
));
4881 /* (COND_OP0 COND_CODE COND_OP1) is a predicate which uses NAME.
4882 Extract a suitable test code and value and store them into *CODE_P and
4883 *VAL_P so the predicate is normalized to NAME *CODE_P *VAL_P.
4885 If no extraction was possible, return FALSE, otherwise return TRUE.
4887 If INVERT is true, then we invert the result stored into *CODE_P. */
4890 extract_code_and_val_from_cond_with_ops (tree name
, enum tree_code cond_code
,
4891 tree cond_op0
, tree cond_op1
,
4892 bool invert
, enum tree_code
*code_p
,
4895 enum tree_code comp_code
;
4898 /* Otherwise, we have a comparison of the form NAME COMP VAL
4899 or VAL COMP NAME. */
4900 if (name
== cond_op1
)
4902 /* If the predicate is of the form VAL COMP NAME, flip
4903 COMP around because we need to register NAME as the
4904 first operand in the predicate. */
4905 comp_code
= swap_tree_comparison (cond_code
);
4910 /* The comparison is of the form NAME COMP VAL, so the
4911 comparison code remains unchanged. */
4912 comp_code
= cond_code
;
4916 /* Invert the comparison code as necessary. */
4918 comp_code
= invert_tree_comparison (comp_code
, 0);
4920 /* VRP does not handle float types. */
4921 if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (val
)))
4924 /* Do not register always-false predicates.
4925 FIXME: this works around a limitation in fold() when dealing with
4926 enumerations. Given 'enum { N1, N2 } x;', fold will not
4927 fold 'if (x > N2)' to 'if (0)'. */
4928 if ((comp_code
== GT_EXPR
|| comp_code
== LT_EXPR
)
4929 && INTEGRAL_TYPE_P (TREE_TYPE (val
)))
4931 tree min
= TYPE_MIN_VALUE (TREE_TYPE (val
));
4932 tree max
= TYPE_MAX_VALUE (TREE_TYPE (val
));
4934 if (comp_code
== GT_EXPR
4936 || compare_values (val
, max
) == 0))
4939 if (comp_code
== LT_EXPR
4941 || compare_values (val
, min
) == 0))
4944 *code_p
= comp_code
;
4949 /* Find out smallest RES where RES > VAL && (RES & MASK) == RES, if any
4950 (otherwise return VAL). VAL and MASK must be zero-extended for
4951 precision PREC. If SGNBIT is non-zero, first xor VAL with SGNBIT
4952 (to transform signed values into unsigned) and at the end xor
4956 masked_increment (const wide_int
&val_in
, const wide_int
&mask
,
4957 const wide_int
&sgnbit
, unsigned int prec
)
4959 wide_int bit
= wi::one (prec
), res
;
4962 wide_int val
= val_in
^ sgnbit
;
4963 for (i
= 0; i
< prec
; i
++, bit
+= bit
)
4966 if ((res
& bit
) == 0)
4969 res
= (val
+ bit
).and_not (res
);
4971 if (wi::gtu_p (res
, val
))
4972 return res
^ sgnbit
;
4974 return val
^ sgnbit
;
4977 /* Try to register an edge assertion for SSA name NAME on edge E for
4978 the condition COND contributing to the conditional jump pointed to by BSI.
4979 Invert the condition COND if INVERT is true.
4980 Return true if an assertion for NAME could be registered. */
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
;
4989 bool retval
= false;
4991 if (!extract_code_and_val_from_cond_with_ops (name
, cond_code
,
4994 invert
, &comp_code
, &val
))
4997 /* Only register an ASSERT_EXPR if NAME was found in the sub-graph
4998 reachable from E. */
4999 if (live_on_edge (e
, name
)
5000 && !has_single_use (name
))
5002 register_new_assert_for (name
, name
, comp_code
, val
, NULL
, e
, bsi
);
5006 /* In the case of NAME <= CST and NAME being defined as
5007 NAME = (unsigned) NAME2 + CST2 we can assert NAME2 >= -CST2
5008 and NAME2 <= CST - CST2. We can do the same for NAME > CST.
5009 This catches range and anti-range tests. */
5010 if ((comp_code
== LE_EXPR
5011 || comp_code
== GT_EXPR
)
5012 && TREE_CODE (val
) == INTEGER_CST
5013 && TYPE_UNSIGNED (TREE_TYPE (val
)))
5015 gimple def_stmt
= SSA_NAME_DEF_STMT (name
);
5016 tree cst2
= NULL_TREE
, name2
= NULL_TREE
, name3
= NULL_TREE
;
5018 /* Extract CST2 from the (optional) addition. */
5019 if (is_gimple_assign (def_stmt
)
5020 && gimple_assign_rhs_code (def_stmt
) == PLUS_EXPR
)
5022 name2
= gimple_assign_rhs1 (def_stmt
);
5023 cst2
= gimple_assign_rhs2 (def_stmt
);
5024 if (TREE_CODE (name2
) == SSA_NAME
5025 && TREE_CODE (cst2
) == INTEGER_CST
)
5026 def_stmt
= SSA_NAME_DEF_STMT (name2
);
5029 /* Extract NAME2 from the (optional) sign-changing cast. */
5030 if (gimple_assign_cast_p (def_stmt
))
5032 if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt
))
5033 && ! TYPE_UNSIGNED (TREE_TYPE (gimple_assign_rhs1 (def_stmt
)))
5034 && (TYPE_PRECISION (gimple_expr_type (def_stmt
))
5035 == TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt
)))))
5036 name3
= gimple_assign_rhs1 (def_stmt
);
5039 /* If name3 is used later, create an ASSERT_EXPR for it. */
5040 if (name3
!= NULL_TREE
5041 && TREE_CODE (name3
) == SSA_NAME
5042 && (cst2
== NULL_TREE
5043 || TREE_CODE (cst2
) == INTEGER_CST
)
5044 && INTEGRAL_TYPE_P (TREE_TYPE (name3
))
5045 && live_on_edge (e
, name3
)
5046 && !has_single_use (name3
))
5050 /* Build an expression for the range test. */
5051 tmp
= build1 (NOP_EXPR
, TREE_TYPE (name
), name3
);
5052 if (cst2
!= NULL_TREE
)
5053 tmp
= build2 (PLUS_EXPR
, TREE_TYPE (name
), tmp
, cst2
);
5057 fprintf (dump_file
, "Adding assert for ");
5058 print_generic_expr (dump_file
, name3
, 0);
5059 fprintf (dump_file
, " from ");
5060 print_generic_expr (dump_file
, tmp
, 0);
5061 fprintf (dump_file
, "\n");
5064 register_new_assert_for (name3
, tmp
, comp_code
, val
, NULL
, e
, bsi
);
5069 /* If name2 is used later, create an ASSERT_EXPR for it. */
5070 if (name2
!= NULL_TREE
5071 && TREE_CODE (name2
) == SSA_NAME
5072 && TREE_CODE (cst2
) == INTEGER_CST
5073 && INTEGRAL_TYPE_P (TREE_TYPE (name2
))
5074 && live_on_edge (e
, name2
)
5075 && !has_single_use (name2
))
5079 /* Build an expression for the range test. */
5081 if (TREE_TYPE (name
) != TREE_TYPE (name2
))
5082 tmp
= build1 (NOP_EXPR
, TREE_TYPE (name
), tmp
);
5083 if (cst2
!= NULL_TREE
)
5084 tmp
= build2 (PLUS_EXPR
, TREE_TYPE (name
), tmp
, cst2
);
5088 fprintf (dump_file
, "Adding assert for ");
5089 print_generic_expr (dump_file
, name2
, 0);
5090 fprintf (dump_file
, " from ");
5091 print_generic_expr (dump_file
, tmp
, 0);
5092 fprintf (dump_file
, "\n");
5095 register_new_assert_for (name2
, tmp
, comp_code
, val
, NULL
, e
, bsi
);
5101 /* In the case of post-in/decrement tests like if (i++) ... and uses
5102 of the in/decremented value on the edge the extra name we want to
5103 assert for is not on the def chain of the name compared. Instead
5104 it is in the set of use stmts. */
5105 if ((comp_code
== NE_EXPR
5106 || comp_code
== EQ_EXPR
)
5107 && TREE_CODE (val
) == INTEGER_CST
)
5109 imm_use_iterator ui
;
5111 FOR_EACH_IMM_USE_STMT (use_stmt
, ui
, name
)
5113 /* Cut off to use-stmts that are in the predecessor. */
5114 if (gimple_bb (use_stmt
) != e
->src
)
5117 if (!is_gimple_assign (use_stmt
))
5120 enum tree_code code
= gimple_assign_rhs_code (use_stmt
);
5121 if (code
!= PLUS_EXPR
5122 && code
!= MINUS_EXPR
)
5125 tree cst
= gimple_assign_rhs2 (use_stmt
);
5126 if (TREE_CODE (cst
) != INTEGER_CST
)
5129 tree name2
= gimple_assign_lhs (use_stmt
);
5130 if (live_on_edge (e
, name2
))
5132 cst
= int_const_binop (code
, val
, cst
);
5133 register_new_assert_for (name2
, name2
, comp_code
, cst
,
5140 if (TREE_CODE_CLASS (comp_code
) == tcc_comparison
5141 && TREE_CODE (val
) == INTEGER_CST
)
5143 gimple def_stmt
= SSA_NAME_DEF_STMT (name
);
5144 tree name2
= NULL_TREE
, names
[2], cst2
= NULL_TREE
;
5145 tree val2
= NULL_TREE
;
5146 unsigned int prec
= TYPE_PRECISION (TREE_TYPE (val
));
5147 wide_int mask
= wi::zero (prec
);
5148 unsigned int nprec
= prec
;
5149 enum tree_code rhs_code
= ERROR_MARK
;
5151 if (is_gimple_assign (def_stmt
))
5152 rhs_code
= gimple_assign_rhs_code (def_stmt
);
5154 /* Add asserts for NAME cmp CST and NAME being defined
5155 as NAME = (int) NAME2. */
5156 if (!TYPE_UNSIGNED (TREE_TYPE (val
))
5157 && (comp_code
== LE_EXPR
|| comp_code
== LT_EXPR
5158 || comp_code
== GT_EXPR
|| comp_code
== GE_EXPR
)
5159 && gimple_assign_cast_p (def_stmt
))
5161 name2
= gimple_assign_rhs1 (def_stmt
);
5162 if (CONVERT_EXPR_CODE_P (rhs_code
)
5163 && INTEGRAL_TYPE_P (TREE_TYPE (name2
))
5164 && TYPE_UNSIGNED (TREE_TYPE (name2
))
5165 && prec
== TYPE_PRECISION (TREE_TYPE (name2
))
5166 && (comp_code
== LE_EXPR
|| comp_code
== GT_EXPR
5167 || !tree_int_cst_equal (val
,
5168 TYPE_MIN_VALUE (TREE_TYPE (val
))))
5169 && live_on_edge (e
, name2
)
5170 && !has_single_use (name2
))
5173 enum tree_code new_comp_code
= comp_code
;
5175 cst
= fold_convert (TREE_TYPE (name2
),
5176 TYPE_MIN_VALUE (TREE_TYPE (val
)));
5177 /* Build an expression for the range test. */
5178 tmp
= build2 (PLUS_EXPR
, TREE_TYPE (name2
), name2
, cst
);
5179 cst
= fold_build2 (PLUS_EXPR
, TREE_TYPE (name2
), cst
,
5180 fold_convert (TREE_TYPE (name2
), val
));
5181 if (comp_code
== LT_EXPR
|| comp_code
== GE_EXPR
)
5183 new_comp_code
= comp_code
== LT_EXPR
? LE_EXPR
: GT_EXPR
;
5184 cst
= fold_build2 (MINUS_EXPR
, TREE_TYPE (name2
), cst
,
5185 build_int_cst (TREE_TYPE (name2
), 1));
5190 fprintf (dump_file
, "Adding assert for ");
5191 print_generic_expr (dump_file
, name2
, 0);
5192 fprintf (dump_file
, " from ");
5193 print_generic_expr (dump_file
, tmp
, 0);
5194 fprintf (dump_file
, "\n");
5197 register_new_assert_for (name2
, tmp
, new_comp_code
, cst
, NULL
,
5204 /* Add asserts for NAME cmp CST and NAME being defined as
5205 NAME = NAME2 >> CST2.
5207 Extract CST2 from the right shift. */
5208 if (rhs_code
== RSHIFT_EXPR
)
5210 name2
= gimple_assign_rhs1 (def_stmt
);
5211 cst2
= gimple_assign_rhs2 (def_stmt
);
5212 if (TREE_CODE (name2
) == SSA_NAME
5213 && tree_fits_uhwi_p (cst2
)
5214 && INTEGRAL_TYPE_P (TREE_TYPE (name2
))
5215 && IN_RANGE (tree_to_uhwi (cst2
), 1, prec
- 1)
5216 && prec
== GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (val
)))
5217 && live_on_edge (e
, name2
)
5218 && !has_single_use (name2
))
5220 mask
= wi::mask (tree_to_uhwi (cst2
), false, prec
);
5221 val2
= fold_binary (LSHIFT_EXPR
, TREE_TYPE (val
), val
, cst2
);
5224 if (val2
!= NULL_TREE
5225 && TREE_CODE (val2
) == INTEGER_CST
5226 && simple_cst_equal (fold_build2 (RSHIFT_EXPR
,
5230 enum tree_code new_comp_code
= comp_code
;
5234 if (comp_code
== EQ_EXPR
|| comp_code
== NE_EXPR
)
5236 if (!TYPE_UNSIGNED (TREE_TYPE (val
)))
5238 tree type
= build_nonstandard_integer_type (prec
, 1);
5239 tmp
= build1 (NOP_EXPR
, type
, name2
);
5240 val2
= fold_convert (type
, val2
);
5242 tmp
= fold_build2 (MINUS_EXPR
, TREE_TYPE (tmp
), tmp
, val2
);
5243 new_val
= wide_int_to_tree (TREE_TYPE (tmp
), mask
);
5244 new_comp_code
= comp_code
== EQ_EXPR
? LE_EXPR
: GT_EXPR
;
5246 else if (comp_code
== LT_EXPR
|| comp_code
== GE_EXPR
)
5249 = wi::min_value (prec
, TYPE_SIGN (TREE_TYPE (val
)));
5251 if (minval
== new_val
)
5252 new_val
= NULL_TREE
;
5257 = wi::max_value (prec
, TYPE_SIGN (TREE_TYPE (val
)));
5260 new_val
= NULL_TREE
;
5262 new_val
= wide_int_to_tree (TREE_TYPE (val2
), mask
);
5269 fprintf (dump_file
, "Adding assert for ");
5270 print_generic_expr (dump_file
, name2
, 0);
5271 fprintf (dump_file
, " from ");
5272 print_generic_expr (dump_file
, tmp
, 0);
5273 fprintf (dump_file
, "\n");
5276 register_new_assert_for (name2
, tmp
, new_comp_code
, new_val
,
5282 /* Add asserts for NAME cmp CST and NAME being defined as
5283 NAME = NAME2 & CST2.
5285 Extract CST2 from the and.
5288 NAME = (unsigned) NAME2;
5289 casts where NAME's type is unsigned and has smaller precision
5290 than NAME2's type as if it was NAME = NAME2 & MASK. */
5291 names
[0] = NULL_TREE
;
5292 names
[1] = NULL_TREE
;
5294 if (rhs_code
== BIT_AND_EXPR
5295 || (CONVERT_EXPR_CODE_P (rhs_code
)
5296 && TREE_CODE (TREE_TYPE (val
)) == INTEGER_TYPE
5297 && TYPE_UNSIGNED (TREE_TYPE (val
))
5298 && TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt
)))
5302 name2
= gimple_assign_rhs1 (def_stmt
);
5303 if (rhs_code
== BIT_AND_EXPR
)
5304 cst2
= gimple_assign_rhs2 (def_stmt
);
5307 cst2
= TYPE_MAX_VALUE (TREE_TYPE (val
));
5308 nprec
= TYPE_PRECISION (TREE_TYPE (name2
));
5310 if (TREE_CODE (name2
) == SSA_NAME
5311 && INTEGRAL_TYPE_P (TREE_TYPE (name2
))
5312 && TREE_CODE (cst2
) == INTEGER_CST
5313 && !integer_zerop (cst2
)
5315 || TYPE_UNSIGNED (TREE_TYPE (val
))))
5317 gimple def_stmt2
= SSA_NAME_DEF_STMT (name2
);
5318 if (gimple_assign_cast_p (def_stmt2
))
5320 names
[1] = gimple_assign_rhs1 (def_stmt2
);
5321 if (!CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt2
))
5322 || !INTEGRAL_TYPE_P (TREE_TYPE (names
[1]))
5323 || (TYPE_PRECISION (TREE_TYPE (name2
))
5324 != TYPE_PRECISION (TREE_TYPE (names
[1])))
5325 || !live_on_edge (e
, names
[1])
5326 || has_single_use (names
[1]))
5327 names
[1] = NULL_TREE
;
5329 if (live_on_edge (e
, name2
)
5330 && !has_single_use (name2
))
5334 if (names
[0] || names
[1])
5336 wide_int minv
, maxv
, valv
, cst2v
;
5337 wide_int tem
, sgnbit
;
5338 bool valid_p
= false, valn
, cst2n
;
5339 enum tree_code ccode
= comp_code
;
5341 valv
= wide_int::from (val
, nprec
, UNSIGNED
);
5342 cst2v
= wide_int::from (cst2
, nprec
, UNSIGNED
);
5343 valn
= wi::neg_p (valv
, TYPE_SIGN (TREE_TYPE (val
)));
5344 cst2n
= wi::neg_p (cst2v
, TYPE_SIGN (TREE_TYPE (val
)));
5345 /* If CST2 doesn't have most significant bit set,
5346 but VAL is negative, we have comparison like
5347 if ((x & 0x123) > -4) (always true). Just give up. */
5351 sgnbit
= wi::set_bit_in_zero (nprec
- 1, nprec
);
5353 sgnbit
= wi::zero (nprec
);
5354 minv
= valv
& cst2v
;
5358 /* Minimum unsigned value for equality is VAL & CST2
5359 (should be equal to VAL, otherwise we probably should
5360 have folded the comparison into false) and
5361 maximum unsigned value is VAL | ~CST2. */
5362 maxv
= valv
| ~cst2v
;
5367 tem
= valv
| ~cst2v
;
5368 /* If VAL is 0, handle (X & CST2) != 0 as (X & CST2) > 0U. */
5372 sgnbit
= wi::zero (nprec
);
5375 /* If (VAL | ~CST2) is all ones, handle it as
5376 (X & CST2) < VAL. */
5381 sgnbit
= wi::zero (nprec
);
5384 if (!cst2n
&& wi::neg_p (cst2v
))
5385 sgnbit
= wi::set_bit_in_zero (nprec
- 1, nprec
);
5394 if (tem
== wi::mask (nprec
- 1, false, nprec
))
5400 sgnbit
= wi::zero (nprec
);
5405 /* Minimum unsigned value for >= if (VAL & CST2) == VAL
5406 is VAL and maximum unsigned value is ~0. For signed
5407 comparison, if CST2 doesn't have most significant bit
5408 set, handle it similarly. If CST2 has MSB set,
5409 the minimum is the same, and maximum is ~0U/2. */
5412 /* If (VAL & CST2) != VAL, X & CST2 can't be equal to
5414 minv
= masked_increment (valv
, cst2v
, sgnbit
, nprec
);
5418 maxv
= wi::mask (nprec
- (cst2n
? 1 : 0), false, nprec
);
5424 /* Find out smallest MINV where MINV > VAL
5425 && (MINV & CST2) == MINV, if any. If VAL is signed and
5426 CST2 has MSB set, compute it biased by 1 << (nprec - 1). */
5427 minv
= masked_increment (valv
, cst2v
, sgnbit
, nprec
);
5430 maxv
= wi::mask (nprec
- (cst2n
? 1 : 0), false, nprec
);
5435 /* Minimum unsigned value for <= is 0 and maximum
5436 unsigned value is VAL | ~CST2 if (VAL & CST2) == VAL.
5437 Otherwise, find smallest VAL2 where VAL2 > VAL
5438 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
5440 For signed comparison, if CST2 doesn't have most
5441 significant bit set, handle it similarly. If CST2 has
5442 MSB set, the maximum is the same and minimum is INT_MIN. */
5447 maxv
= masked_increment (valv
, cst2v
, sgnbit
, nprec
);
5459 /* Minimum unsigned value for < is 0 and maximum
5460 unsigned value is (VAL-1) | ~CST2 if (VAL & CST2) == VAL.
5461 Otherwise, find smallest VAL2 where VAL2 > VAL
5462 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
5464 For signed comparison, if CST2 doesn't have most
5465 significant bit set, handle it similarly. If CST2 has
5466 MSB set, the maximum is the same and minimum is INT_MIN. */
5475 maxv
= masked_increment (valv
, cst2v
, sgnbit
, nprec
);
5489 && (maxv
- minv
) != -1)
5491 tree tmp
, new_val
, type
;
5494 for (i
= 0; i
< 2; i
++)
5497 wide_int maxv2
= maxv
;
5499 type
= TREE_TYPE (names
[i
]);
5500 if (!TYPE_UNSIGNED (type
))
5502 type
= build_nonstandard_integer_type (nprec
, 1);
5503 tmp
= build1 (NOP_EXPR
, type
, names
[i
]);
5507 tmp
= build2 (PLUS_EXPR
, type
, tmp
,
5508 wide_int_to_tree (type
, -minv
));
5509 maxv2
= maxv
- minv
;
5511 new_val
= wide_int_to_tree (type
, maxv2
);
5515 fprintf (dump_file
, "Adding assert for ");
5516 print_generic_expr (dump_file
, names
[i
], 0);
5517 fprintf (dump_file
, " from ");
5518 print_generic_expr (dump_file
, tmp
, 0);
5519 fprintf (dump_file
, "\n");
5522 register_new_assert_for (names
[i
], tmp
, LE_EXPR
,
5523 new_val
, NULL
, e
, bsi
);
5533 /* OP is an operand of a truth value expression which is known to have
5534 a particular value. Register any asserts for OP and for any
5535 operands in OP's defining statement.
5537 If CODE is EQ_EXPR, then we want to register OP is zero (false),
5538 if CODE is NE_EXPR, then we want to register OP is nonzero (true). */
5541 register_edge_assert_for_1 (tree op
, enum tree_code code
,
5542 edge e
, gimple_stmt_iterator bsi
)
5544 bool retval
= false;
5547 enum tree_code rhs_code
;
5549 /* We only care about SSA_NAMEs. */
5550 if (TREE_CODE (op
) != SSA_NAME
)
5553 /* We know that OP will have a zero or nonzero value. If OP is used
5554 more than once go ahead and register an assert for OP. */
5555 if (live_on_edge (e
, op
)
5556 && !has_single_use (op
))
5558 val
= build_int_cst (TREE_TYPE (op
), 0);
5559 register_new_assert_for (op
, op
, code
, val
, NULL
, e
, bsi
);
5563 /* Now look at how OP is set. If it's set from a comparison,
5564 a truth operation or some bit operations, then we may be able
5565 to register information about the operands of that assignment. */
5566 op_def
= SSA_NAME_DEF_STMT (op
);
5567 if (gimple_code (op_def
) != GIMPLE_ASSIGN
)
5570 rhs_code
= gimple_assign_rhs_code (op_def
);
5572 if (TREE_CODE_CLASS (rhs_code
) == tcc_comparison
)
5574 bool invert
= (code
== EQ_EXPR
? true : false);
5575 tree op0
= gimple_assign_rhs1 (op_def
);
5576 tree op1
= gimple_assign_rhs2 (op_def
);
5578 if (TREE_CODE (op0
) == SSA_NAME
)
5579 retval
|= register_edge_assert_for_2 (op0
, e
, bsi
, rhs_code
, op0
, op1
,
5581 if (TREE_CODE (op1
) == SSA_NAME
)
5582 retval
|= register_edge_assert_for_2 (op1
, e
, bsi
, rhs_code
, op0
, op1
,
5585 else if ((code
== NE_EXPR
5586 && gimple_assign_rhs_code (op_def
) == BIT_AND_EXPR
)
5588 && gimple_assign_rhs_code (op_def
) == BIT_IOR_EXPR
))
5590 /* Recurse on each operand. */
5591 tree op0
= gimple_assign_rhs1 (op_def
);
5592 tree op1
= gimple_assign_rhs2 (op_def
);
5593 if (TREE_CODE (op0
) == SSA_NAME
5594 && has_single_use (op0
))
5595 retval
|= register_edge_assert_for_1 (op0
, code
, e
, bsi
);
5596 if (TREE_CODE (op1
) == SSA_NAME
5597 && has_single_use (op1
))
5598 retval
|= register_edge_assert_for_1 (op1
, code
, e
, bsi
);
5600 else if (gimple_assign_rhs_code (op_def
) == BIT_NOT_EXPR
5601 && TYPE_PRECISION (TREE_TYPE (gimple_assign_lhs (op_def
))) == 1)
5603 /* Recurse, flipping CODE. */
5604 code
= invert_tree_comparison (code
, false);
5605 retval
|= register_edge_assert_for_1 (gimple_assign_rhs1 (op_def
),
5608 else if (gimple_assign_rhs_code (op_def
) == SSA_NAME
)
5610 /* Recurse through the copy. */
5611 retval
|= register_edge_assert_for_1 (gimple_assign_rhs1 (op_def
),
5614 else if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (op_def
)))
5616 /* Recurse through the type conversion, unless it is a narrowing
5617 conversion or conversion from non-integral type. */
5618 tree rhs
= gimple_assign_rhs1 (op_def
);
5619 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs
))
5620 && (TYPE_PRECISION (TREE_TYPE (rhs
))
5621 <= TYPE_PRECISION (TREE_TYPE (op
))))
5622 retval
|= register_edge_assert_for_1 (rhs
, code
, e
, bsi
);
5628 /* Try to register an edge assertion for SSA name NAME on edge E for
5629 the condition COND contributing to the conditional jump pointed to by SI.
5630 Return true if an assertion for NAME could be registered. */
5633 register_edge_assert_for (tree name
, edge e
, gimple_stmt_iterator si
,
5634 enum tree_code cond_code
, tree cond_op0
,
5638 enum tree_code comp_code
;
5639 bool retval
= false;
5640 bool is_else_edge
= (e
->flags
& EDGE_FALSE_VALUE
) != 0;
5642 /* Do not attempt to infer anything in names that flow through
5644 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name
))
5647 if (!extract_code_and_val_from_cond_with_ops (name
, cond_code
,
5653 /* Register ASSERT_EXPRs for name. */
5654 retval
|= register_edge_assert_for_2 (name
, e
, si
, cond_code
, cond_op0
,
5655 cond_op1
, is_else_edge
);
5658 /* If COND is effectively an equality test of an SSA_NAME against
5659 the value zero or one, then we may be able to assert values
5660 for SSA_NAMEs which flow into COND. */
5662 /* In the case of NAME == 1 or NAME != 0, for BIT_AND_EXPR defining
5663 statement of NAME we can assert both operands of the BIT_AND_EXPR
5664 have nonzero value. */
5665 if (((comp_code
== EQ_EXPR
&& integer_onep (val
))
5666 || (comp_code
== NE_EXPR
&& integer_zerop (val
))))
5668 gimple def_stmt
= SSA_NAME_DEF_STMT (name
);
5670 if (is_gimple_assign (def_stmt
)
5671 && gimple_assign_rhs_code (def_stmt
) == BIT_AND_EXPR
)
5673 tree op0
= gimple_assign_rhs1 (def_stmt
);
5674 tree op1
= gimple_assign_rhs2 (def_stmt
);
5675 retval
|= register_edge_assert_for_1 (op0
, NE_EXPR
, e
, si
);
5676 retval
|= register_edge_assert_for_1 (op1
, NE_EXPR
, e
, si
);
5680 /* In the case of NAME == 0 or NAME != 1, for BIT_IOR_EXPR defining
5681 statement of NAME we can assert both operands of the BIT_IOR_EXPR
5683 if (((comp_code
== EQ_EXPR
&& integer_zerop (val
))
5684 || (comp_code
== NE_EXPR
&& integer_onep (val
))))
5686 gimple def_stmt
= SSA_NAME_DEF_STMT (name
);
5688 /* For BIT_IOR_EXPR only if NAME == 0 both operands have
5689 necessarily zero value, or if type-precision is one. */
5690 if (is_gimple_assign (def_stmt
)
5691 && (gimple_assign_rhs_code (def_stmt
) == BIT_IOR_EXPR
5692 && (TYPE_PRECISION (TREE_TYPE (name
)) == 1
5693 || comp_code
== EQ_EXPR
)))
5695 tree op0
= gimple_assign_rhs1 (def_stmt
);
5696 tree op1
= gimple_assign_rhs2 (def_stmt
);
5697 retval
|= register_edge_assert_for_1 (op0
, EQ_EXPR
, e
, si
);
5698 retval
|= register_edge_assert_for_1 (op1
, EQ_EXPR
, e
, si
);
5706 /* Determine whether the outgoing edges of BB should receive an
5707 ASSERT_EXPR for each of the operands of BB's LAST statement.
5708 The last statement of BB must be a COND_EXPR.
5710 If any of the sub-graphs rooted at BB have an interesting use of
5711 the predicate operands, an assert location node is added to the
5712 list of assertions for the corresponding operands. */
5715 find_conditional_asserts (basic_block bb
, gcond
*last
)
5718 gimple_stmt_iterator bsi
;
5724 need_assert
= false;
5725 bsi
= gsi_for_stmt (last
);
5727 /* Look for uses of the operands in each of the sub-graphs
5728 rooted at BB. We need to check each of the outgoing edges
5729 separately, so that we know what kind of ASSERT_EXPR to
5731 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
5736 /* Register the necessary assertions for each operand in the
5737 conditional predicate. */
5738 FOR_EACH_SSA_TREE_OPERAND (op
, last
, iter
, SSA_OP_USE
)
5740 need_assert
|= register_edge_assert_for (op
, e
, bsi
,
5741 gimple_cond_code (last
),
5742 gimple_cond_lhs (last
),
5743 gimple_cond_rhs (last
));
5756 /* Compare two case labels sorting first by the destination bb index
5757 and then by the case value. */
5760 compare_case_labels (const void *p1
, const void *p2
)
5762 const struct case_info
*ci1
= (const struct case_info
*) p1
;
5763 const struct case_info
*ci2
= (const struct case_info
*) p2
;
5764 int idx1
= ci1
->bb
->index
;
5765 int idx2
= ci2
->bb
->index
;
5769 else if (idx1
== idx2
)
5771 /* Make sure the default label is first in a group. */
5772 if (!CASE_LOW (ci1
->expr
))
5774 else if (!CASE_LOW (ci2
->expr
))
5777 return tree_int_cst_compare (CASE_LOW (ci1
->expr
),
5778 CASE_LOW (ci2
->expr
));
5784 /* Determine whether the outgoing edges of BB should receive an
5785 ASSERT_EXPR for each of the operands of BB's LAST statement.
5786 The last statement of BB must be a SWITCH_EXPR.
5788 If any of the sub-graphs rooted at BB have an interesting use of
5789 the predicate operands, an assert location node is added to the
5790 list of assertions for the corresponding operands. */
5793 find_switch_asserts (basic_block bb
, gswitch
*last
)
5796 gimple_stmt_iterator bsi
;
5799 struct case_info
*ci
;
5800 size_t n
= gimple_switch_num_labels (last
);
5801 #if GCC_VERSION >= 4000
5804 /* Work around GCC 3.4 bug (PR 37086). */
5805 volatile unsigned int idx
;
5808 need_assert
= false;
5809 bsi
= gsi_for_stmt (last
);
5810 op
= gimple_switch_index (last
);
5811 if (TREE_CODE (op
) != SSA_NAME
)
5814 /* Build a vector of case labels sorted by destination label. */
5815 ci
= XNEWVEC (struct case_info
, n
);
5816 for (idx
= 0; idx
< n
; ++idx
)
5818 ci
[idx
].expr
= gimple_switch_label (last
, idx
);
5819 ci
[idx
].bb
= label_to_block (CASE_LABEL (ci
[idx
].expr
));
5821 qsort (ci
, n
, sizeof (struct case_info
), compare_case_labels
);
5823 for (idx
= 0; idx
< n
; ++idx
)
5826 tree cl
= ci
[idx
].expr
;
5827 basic_block cbb
= ci
[idx
].bb
;
5829 min
= CASE_LOW (cl
);
5830 max
= CASE_HIGH (cl
);
5832 /* If there are multiple case labels with the same destination
5833 we need to combine them to a single value range for the edge. */
5834 if (idx
+ 1 < n
&& cbb
== ci
[idx
+ 1].bb
)
5836 /* Skip labels until the last of the group. */
5839 } while (idx
< n
&& cbb
== ci
[idx
].bb
);
5842 /* Pick up the maximum of the case label range. */
5843 if (CASE_HIGH (ci
[idx
].expr
))
5844 max
= CASE_HIGH (ci
[idx
].expr
);
5846 max
= CASE_LOW (ci
[idx
].expr
);
5849 /* Nothing to do if the range includes the default label until we
5850 can register anti-ranges. */
5851 if (min
== NULL_TREE
)
5854 /* Find the edge to register the assert expr on. */
5855 e
= find_edge (bb
, cbb
);
5857 /* Register the necessary assertions for the operand in the
5859 need_assert
|= register_edge_assert_for (op
, e
, bsi
,
5860 max
? GE_EXPR
: EQ_EXPR
,
5862 fold_convert (TREE_TYPE (op
),
5866 need_assert
|= register_edge_assert_for (op
, e
, bsi
, LE_EXPR
,
5868 fold_convert (TREE_TYPE (op
),
5878 /* Traverse all the statements in block BB looking for statements that
5879 may generate useful assertions for the SSA names in their operand.
5880 If a statement produces a useful assertion A for name N_i, then the
5881 list of assertions already generated for N_i is scanned to
5882 determine if A is actually needed.
5884 If N_i already had the assertion A at a location dominating the
5885 current location, then nothing needs to be done. Otherwise, the
5886 new location for A is recorded instead.
5888 1- For every statement S in BB, all the variables used by S are
5889 added to bitmap FOUND_IN_SUBGRAPH.
5891 2- If statement S uses an operand N in a way that exposes a known
5892 value range for N, then if N was not already generated by an
5893 ASSERT_EXPR, create a new assert location for N. For instance,
5894 if N is a pointer and the statement dereferences it, we can
5895 assume that N is not NULL.
5897 3- COND_EXPRs are a special case of #2. We can derive range
5898 information from the predicate but need to insert different
5899 ASSERT_EXPRs for each of the sub-graphs rooted at the
5900 conditional block. If the last statement of BB is a conditional
5901 expression of the form 'X op Y', then
5903 a) Remove X and Y from the set FOUND_IN_SUBGRAPH.
5905 b) If the conditional is the only entry point to the sub-graph
5906 corresponding to the THEN_CLAUSE, recurse into it. On
5907 return, if X and/or Y are marked in FOUND_IN_SUBGRAPH, then
5908 an ASSERT_EXPR is added for the corresponding variable.
5910 c) Repeat step (b) on the ELSE_CLAUSE.
5912 d) Mark X and Y in FOUND_IN_SUBGRAPH.
5921 In this case, an assertion on the THEN clause is useful to
5922 determine that 'a' is always 9 on that edge. However, an assertion
5923 on the ELSE clause would be unnecessary.
5925 4- If BB does not end in a conditional expression, then we recurse
5926 into BB's dominator children.
5928 At the end of the recursive traversal, every SSA name will have a
5929 list of locations where ASSERT_EXPRs should be added. When a new
5930 location for name N is found, it is registered by calling
5931 register_new_assert_for. That function keeps track of all the
5932 registered assertions to prevent adding unnecessary assertions.
5933 For instance, if a pointer P_4 is dereferenced more than once in a
5934 dominator tree, only the location dominating all the dereference of
5935 P_4 will receive an ASSERT_EXPR.
5937 If this function returns true, then it means that there are names
5938 for which we need to generate ASSERT_EXPRs. Those assertions are
5939 inserted by process_assert_insertions. */
5942 find_assert_locations_1 (basic_block bb
, sbitmap live
)
5947 need_assert
= false;
5948 last
= last_stmt (bb
);
5950 /* If BB's last statement is a conditional statement involving integer
5951 operands, determine if we need to add ASSERT_EXPRs. */
5953 && gimple_code (last
) == GIMPLE_COND
5954 && !fp_predicate (last
)
5955 && !ZERO_SSA_OPERANDS (last
, SSA_OP_USE
))
5956 need_assert
|= find_conditional_asserts (bb
, as_a
<gcond
*> (last
));
5958 /* If BB's last statement is a switch statement involving integer
5959 operands, determine if we need to add ASSERT_EXPRs. */
5961 && gimple_code (last
) == GIMPLE_SWITCH
5962 && !ZERO_SSA_OPERANDS (last
, SSA_OP_USE
))
5963 need_assert
|= find_switch_asserts (bb
, as_a
<gswitch
*> (last
));
5965 /* Traverse all the statements in BB marking used names and looking
5966 for statements that may infer assertions for their used operands. */
5967 for (gimple_stmt_iterator si
= gsi_last_bb (bb
); !gsi_end_p (si
);
5974 stmt
= gsi_stmt (si
);
5976 if (is_gimple_debug (stmt
))
5979 /* See if we can derive an assertion for any of STMT's operands. */
5980 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, i
, SSA_OP_USE
)
5983 enum tree_code comp_code
;
5985 /* If op is not live beyond this stmt, do not bother to insert
5987 if (!bitmap_bit_p (live
, SSA_NAME_VERSION (op
)))
5990 /* If OP is used in such a way that we can infer a value
5991 range for it, and we don't find a previous assertion for
5992 it, create a new assertion location node for OP. */
5993 if (infer_value_range (stmt
, op
, &comp_code
, &value
))
5995 /* If we are able to infer a nonzero value range for OP,
5996 then walk backwards through the use-def chain to see if OP
5997 was set via a typecast.
5999 If so, then we can also infer a nonzero value range
6000 for the operand of the NOP_EXPR. */
6001 if (comp_code
== NE_EXPR
&& integer_zerop (value
))
6004 gimple def_stmt
= SSA_NAME_DEF_STMT (t
);
6006 while (is_gimple_assign (def_stmt
)
6007 && gimple_assign_rhs_code (def_stmt
) == NOP_EXPR
6009 (gimple_assign_rhs1 (def_stmt
)) == SSA_NAME
6011 (TREE_TYPE (gimple_assign_rhs1 (def_stmt
))))
6013 t
= gimple_assign_rhs1 (def_stmt
);
6014 def_stmt
= SSA_NAME_DEF_STMT (t
);
6016 /* Note we want to register the assert for the
6017 operand of the NOP_EXPR after SI, not after the
6019 if (! has_single_use (t
))
6021 register_new_assert_for (t
, t
, comp_code
, value
,
6028 register_new_assert_for (op
, op
, comp_code
, value
, bb
, NULL
, si
);
6034 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, i
, SSA_OP_USE
)
6035 bitmap_set_bit (live
, SSA_NAME_VERSION (op
));
6036 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, i
, SSA_OP_DEF
)
6037 bitmap_clear_bit (live
, SSA_NAME_VERSION (op
));
6040 /* Traverse all PHI nodes in BB, updating live. */
6041 for (gphi_iterator si
= gsi_start_phis (bb
); !gsi_end_p (si
);
6044 use_operand_p arg_p
;
6046 gphi
*phi
= si
.phi ();
6047 tree res
= gimple_phi_result (phi
);
6049 if (virtual_operand_p (res
))
6052 FOR_EACH_PHI_ARG (arg_p
, phi
, i
, SSA_OP_USE
)
6054 tree arg
= USE_FROM_PTR (arg_p
);
6055 if (TREE_CODE (arg
) == SSA_NAME
)
6056 bitmap_set_bit (live
, SSA_NAME_VERSION (arg
));
6059 bitmap_clear_bit (live
, SSA_NAME_VERSION (res
));
6065 /* Do an RPO walk over the function computing SSA name liveness
6066 on-the-fly and deciding on assert expressions to insert.
6067 Returns true if there are assert expressions to be inserted. */
6070 find_assert_locations (void)
6072 int *rpo
= XNEWVEC (int, last_basic_block_for_fn (cfun
));
6073 int *bb_rpo
= XNEWVEC (int, last_basic_block_for_fn (cfun
));
6074 int *last_rpo
= XCNEWVEC (int, last_basic_block_for_fn (cfun
));
6078 live
= XCNEWVEC (sbitmap
, last_basic_block_for_fn (cfun
));
6079 rpo_cnt
= pre_and_rev_post_order_compute (NULL
, rpo
, false);
6080 for (i
= 0; i
< rpo_cnt
; ++i
)
6083 /* Pre-seed loop latch liveness from loop header PHI nodes. Due to
6084 the order we compute liveness and insert asserts we otherwise
6085 fail to insert asserts into the loop latch. */
6087 FOR_EACH_LOOP (loop
, 0)
6089 i
= loop
->latch
->index
;
6090 unsigned int j
= single_succ_edge (loop
->latch
)->dest_idx
;
6091 for (gphi_iterator gsi
= gsi_start_phis (loop
->header
);
6092 !gsi_end_p (gsi
); gsi_next (&gsi
))
6094 gphi
*phi
= gsi
.phi ();
6095 if (virtual_operand_p (gimple_phi_result (phi
)))
6097 tree arg
= gimple_phi_arg_def (phi
, j
);
6098 if (TREE_CODE (arg
) == SSA_NAME
)
6100 if (live
[i
] == NULL
)
6102 live
[i
] = sbitmap_alloc (num_ssa_names
);
6103 bitmap_clear (live
[i
]);
6105 bitmap_set_bit (live
[i
], SSA_NAME_VERSION (arg
));
6110 need_asserts
= false;
6111 for (i
= rpo_cnt
- 1; i
>= 0; --i
)
6113 basic_block bb
= BASIC_BLOCK_FOR_FN (cfun
, rpo
[i
]);
6119 live
[rpo
[i
]] = sbitmap_alloc (num_ssa_names
);
6120 bitmap_clear (live
[rpo
[i
]]);
6123 /* Process BB and update the live information with uses in
6125 need_asserts
|= find_assert_locations_1 (bb
, live
[rpo
[i
]]);
6127 /* Merge liveness into the predecessor blocks and free it. */
6128 if (!bitmap_empty_p (live
[rpo
[i
]]))
6131 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
6133 int pred
= e
->src
->index
;
6134 if ((e
->flags
& EDGE_DFS_BACK
) || pred
== ENTRY_BLOCK
)
6139 live
[pred
] = sbitmap_alloc (num_ssa_names
);
6140 bitmap_clear (live
[pred
]);
6142 bitmap_ior (live
[pred
], live
[pred
], live
[rpo
[i
]]);
6144 if (bb_rpo
[pred
] < pred_rpo
)
6145 pred_rpo
= bb_rpo
[pred
];
6148 /* Record the RPO number of the last visited block that needs
6149 live information from this block. */
6150 last_rpo
[rpo
[i
]] = pred_rpo
;
6154 sbitmap_free (live
[rpo
[i
]]);
6155 live
[rpo
[i
]] = NULL
;
6158 /* We can free all successors live bitmaps if all their
6159 predecessors have been visited already. */
6160 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
6161 if (last_rpo
[e
->dest
->index
] == i
6162 && live
[e
->dest
->index
])
6164 sbitmap_free (live
[e
->dest
->index
]);
6165 live
[e
->dest
->index
] = NULL
;
6170 XDELETEVEC (bb_rpo
);
6171 XDELETEVEC (last_rpo
);
6172 for (i
= 0; i
< last_basic_block_for_fn (cfun
); ++i
)
6174 sbitmap_free (live
[i
]);
6177 return need_asserts
;
6180 /* Create an ASSERT_EXPR for NAME and insert it in the location
6181 indicated by LOC. Return true if we made any edge insertions. */
6184 process_assert_insertions_for (tree name
, assert_locus_t loc
)
6186 /* Build the comparison expression NAME_i COMP_CODE VAL. */
6193 /* If we have X <=> X do not insert an assert expr for that. */
6194 if (loc
->expr
== loc
->val
)
6197 cond
= build2 (loc
->comp_code
, boolean_type_node
, loc
->expr
, loc
->val
);
6198 assert_stmt
= build_assert_expr_for (cond
, name
);
6201 /* We have been asked to insert the assertion on an edge. This
6202 is used only by COND_EXPR and SWITCH_EXPR assertions. */
6203 gcc_checking_assert (gimple_code (gsi_stmt (loc
->si
)) == GIMPLE_COND
6204 || (gimple_code (gsi_stmt (loc
->si
))
6207 gsi_insert_on_edge (loc
->e
, assert_stmt
);
6211 /* Otherwise, we can insert right after LOC->SI iff the
6212 statement must not be the last statement in the block. */
6213 stmt
= gsi_stmt (loc
->si
);
6214 if (!stmt_ends_bb_p (stmt
))
6216 gsi_insert_after (&loc
->si
, assert_stmt
, GSI_SAME_STMT
);
6220 /* If STMT must be the last statement in BB, we can only insert new
6221 assertions on the non-abnormal edge out of BB. Note that since
6222 STMT is not control flow, there may only be one non-abnormal edge
6224 FOR_EACH_EDGE (e
, ei
, loc
->bb
->succs
)
6225 if (!(e
->flags
& EDGE_ABNORMAL
))
6227 gsi_insert_on_edge (e
, assert_stmt
);
6235 /* Process all the insertions registered for every name N_i registered
6236 in NEED_ASSERT_FOR. The list of assertions to be inserted are
6237 found in ASSERTS_FOR[i]. */
6240 process_assert_insertions (void)
6244 bool update_edges_p
= false;
6245 int num_asserts
= 0;
6247 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6248 dump_all_asserts (dump_file
);
6250 EXECUTE_IF_SET_IN_BITMAP (need_assert_for
, 0, i
, bi
)
6252 assert_locus_t loc
= asserts_for
[i
];
6257 assert_locus_t next
= loc
->next
;
6258 update_edges_p
|= process_assert_insertions_for (ssa_name (i
), loc
);
6266 gsi_commit_edge_inserts ();
6268 statistics_counter_event (cfun
, "Number of ASSERT_EXPR expressions inserted",
6273 /* Traverse the flowgraph looking for conditional jumps to insert range
6274 expressions. These range expressions are meant to provide information
6275 to optimizations that need to reason in terms of value ranges. They
6276 will not be expanded into RTL. For instance, given:
6285 this pass will transform the code into:
6291 x = ASSERT_EXPR <x, x < y>
6296 y = ASSERT_EXPR <y, x >= y>
6300 The idea is that once copy and constant propagation have run, other
6301 optimizations will be able to determine what ranges of values can 'x'
6302 take in different paths of the code, simply by checking the reaching
6303 definition of 'x'. */
6306 insert_range_assertions (void)
6308 need_assert_for
= BITMAP_ALLOC (NULL
);
6309 asserts_for
= XCNEWVEC (assert_locus_t
, num_ssa_names
);
6311 calculate_dominance_info (CDI_DOMINATORS
);
6313 if (find_assert_locations ())
6315 process_assert_insertions ();
6316 update_ssa (TODO_update_ssa_no_phi
);
6319 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6321 fprintf (dump_file
, "\nSSA form after inserting ASSERT_EXPRs\n");
6322 dump_function_to_file (current_function_decl
, dump_file
, dump_flags
);
6326 BITMAP_FREE (need_assert_for
);
6329 /* Checks one ARRAY_REF in REF, located at LOCUS. Ignores flexible arrays
6330 and "struct" hacks. If VRP can determine that the
6331 array subscript is a constant, check if it is outside valid
6332 range. If the array subscript is a RANGE, warn if it is
6333 non-overlapping with valid range.
6334 IGNORE_OFF_BY_ONE is true if the ARRAY_REF is inside a ADDR_EXPR. */
6337 check_array_ref (location_t location
, tree ref
, bool ignore_off_by_one
)
6339 value_range_t
* vr
= NULL
;
6340 tree low_sub
, up_sub
;
6341 tree low_bound
, up_bound
, up_bound_p1
;
6344 if (TREE_NO_WARNING (ref
))
6347 low_sub
= up_sub
= TREE_OPERAND (ref
, 1);
6348 up_bound
= array_ref_up_bound (ref
);
6350 /* Can not check flexible arrays. */
6352 || TREE_CODE (up_bound
) != INTEGER_CST
)
6355 /* Accesses to trailing arrays via pointers may access storage
6356 beyond the types array bounds. */
6357 base
= get_base_address (ref
);
6358 if (base
&& TREE_CODE (base
) == MEM_REF
)
6360 tree cref
, next
= NULL_TREE
;
6362 if (TREE_CODE (TREE_OPERAND (ref
, 0)) != COMPONENT_REF
)
6365 cref
= TREE_OPERAND (ref
, 0);
6366 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (cref
, 0))) == RECORD_TYPE
)
6367 for (next
= DECL_CHAIN (TREE_OPERAND (cref
, 1));
6368 next
&& TREE_CODE (next
) != FIELD_DECL
;
6369 next
= DECL_CHAIN (next
))
6372 /* If this is the last field in a struct type or a field in a
6373 union type do not warn. */
6378 low_bound
= array_ref_low_bound (ref
);
6379 up_bound_p1
= int_const_binop (PLUS_EXPR
, up_bound
,
6380 build_int_cst (TREE_TYPE (up_bound
), 1));
6382 if (TREE_CODE (low_sub
) == SSA_NAME
)
6384 vr
= get_value_range (low_sub
);
6385 if (vr
->type
== VR_RANGE
|| vr
->type
== VR_ANTI_RANGE
)
6387 low_sub
= vr
->type
== VR_RANGE
? vr
->max
: vr
->min
;
6388 up_sub
= vr
->type
== VR_RANGE
? vr
->min
: vr
->max
;
6392 if (vr
&& vr
->type
== VR_ANTI_RANGE
)
6394 if (TREE_CODE (up_sub
) == INTEGER_CST
6395 && tree_int_cst_lt (up_bound
, up_sub
)
6396 && TREE_CODE (low_sub
) == INTEGER_CST
6397 && tree_int_cst_lt (low_sub
, low_bound
))
6399 warning_at (location
, OPT_Warray_bounds
,
6400 "array subscript is outside array bounds");
6401 TREE_NO_WARNING (ref
) = 1;
6404 else if (TREE_CODE (up_sub
) == INTEGER_CST
6405 && (ignore_off_by_one
6406 ? (tree_int_cst_lt (up_bound
, up_sub
)
6407 && !tree_int_cst_equal (up_bound_p1
, up_sub
))
6408 : (tree_int_cst_lt (up_bound
, up_sub
)
6409 || tree_int_cst_equal (up_bound_p1
, up_sub
))))
6411 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6413 fprintf (dump_file
, "Array bound warning for ");
6414 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, ref
);
6415 fprintf (dump_file
, "\n");
6417 warning_at (location
, OPT_Warray_bounds
,
6418 "array subscript is above array bounds");
6419 TREE_NO_WARNING (ref
) = 1;
6421 else if (TREE_CODE (low_sub
) == INTEGER_CST
6422 && tree_int_cst_lt (low_sub
, low_bound
))
6424 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6426 fprintf (dump_file
, "Array bound warning for ");
6427 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, ref
);
6428 fprintf (dump_file
, "\n");
6430 warning_at (location
, OPT_Warray_bounds
,
6431 "array subscript is below array bounds");
6432 TREE_NO_WARNING (ref
) = 1;
6436 /* Searches if the expr T, located at LOCATION computes
6437 address of an ARRAY_REF, and call check_array_ref on it. */
6440 search_for_addr_array (tree t
, location_t location
)
6442 while (TREE_CODE (t
) == SSA_NAME
)
6444 gimple g
= SSA_NAME_DEF_STMT (t
);
6446 if (gimple_code (g
) != GIMPLE_ASSIGN
)
6449 if (get_gimple_rhs_class (gimple_assign_rhs_code (g
))
6450 != GIMPLE_SINGLE_RHS
)
6453 t
= gimple_assign_rhs1 (g
);
6457 /* We are only interested in addresses of ARRAY_REF's. */
6458 if (TREE_CODE (t
) != ADDR_EXPR
)
6461 /* Check each ARRAY_REFs in the reference chain. */
6464 if (TREE_CODE (t
) == ARRAY_REF
)
6465 check_array_ref (location
, t
, true /*ignore_off_by_one*/);
6467 t
= TREE_OPERAND (t
, 0);
6469 while (handled_component_p (t
));
6471 if (TREE_CODE (t
) == MEM_REF
6472 && TREE_CODE (TREE_OPERAND (t
, 0)) == ADDR_EXPR
6473 && !TREE_NO_WARNING (t
))
6475 tree tem
= TREE_OPERAND (TREE_OPERAND (t
, 0), 0);
6476 tree low_bound
, up_bound
, el_sz
;
6478 if (TREE_CODE (TREE_TYPE (tem
)) != ARRAY_TYPE
6479 || TREE_CODE (TREE_TYPE (TREE_TYPE (tem
))) == ARRAY_TYPE
6480 || !TYPE_DOMAIN (TREE_TYPE (tem
)))
6483 low_bound
= TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (tem
)));
6484 up_bound
= TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (tem
)));
6485 el_sz
= TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (tem
)));
6487 || TREE_CODE (low_bound
) != INTEGER_CST
6489 || TREE_CODE (up_bound
) != INTEGER_CST
6491 || TREE_CODE (el_sz
) != INTEGER_CST
)
6494 idx
= mem_ref_offset (t
);
6495 idx
= wi::sdiv_trunc (idx
, wi::to_offset (el_sz
));
6496 if (wi::lts_p (idx
, 0))
6498 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6500 fprintf (dump_file
, "Array bound warning for ");
6501 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, t
);
6502 fprintf (dump_file
, "\n");
6504 warning_at (location
, OPT_Warray_bounds
,
6505 "array subscript is below array bounds");
6506 TREE_NO_WARNING (t
) = 1;
6508 else if (wi::gts_p (idx
, (wi::to_offset (up_bound
)
6509 - wi::to_offset (low_bound
) + 1)))
6511 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6513 fprintf (dump_file
, "Array bound warning for ");
6514 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, t
);
6515 fprintf (dump_file
, "\n");
6517 warning_at (location
, OPT_Warray_bounds
,
6518 "array subscript is above array bounds");
6519 TREE_NO_WARNING (t
) = 1;
6524 /* walk_tree() callback that checks if *TP is
6525 an ARRAY_REF inside an ADDR_EXPR (in which an array
6526 subscript one outside the valid range is allowed). Call
6527 check_array_ref for each ARRAY_REF found. The location is
6531 check_array_bounds (tree
*tp
, int *walk_subtree
, void *data
)
6534 struct walk_stmt_info
*wi
= (struct walk_stmt_info
*) data
;
6535 location_t location
;
6537 if (EXPR_HAS_LOCATION (t
))
6538 location
= EXPR_LOCATION (t
);
6541 location_t
*locp
= (location_t
*) wi
->info
;
6545 *walk_subtree
= TRUE
;
6547 if (TREE_CODE (t
) == ARRAY_REF
)
6548 check_array_ref (location
, t
, false /*ignore_off_by_one*/);
6550 if (TREE_CODE (t
) == MEM_REF
6551 || (TREE_CODE (t
) == RETURN_EXPR
&& TREE_OPERAND (t
, 0)))
6552 search_for_addr_array (TREE_OPERAND (t
, 0), location
);
6554 if (TREE_CODE (t
) == ADDR_EXPR
)
6555 *walk_subtree
= FALSE
;
6560 /* Walk over all statements of all reachable BBs and call check_array_bounds
6564 check_all_array_refs (void)
6567 gimple_stmt_iterator si
;
6569 FOR_EACH_BB_FN (bb
, cfun
)
6573 bool executable
= false;
6575 /* Skip blocks that were found to be unreachable. */
6576 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
6577 executable
|= !!(e
->flags
& EDGE_EXECUTABLE
);
6581 for (si
= gsi_start_bb (bb
); !gsi_end_p (si
); gsi_next (&si
))
6583 gimple stmt
= gsi_stmt (si
);
6584 struct walk_stmt_info wi
;
6585 if (!gimple_has_location (stmt
))
6588 if (is_gimple_call (stmt
))
6591 size_t n
= gimple_call_num_args (stmt
);
6592 for (i
= 0; i
< n
; i
++)
6594 tree arg
= gimple_call_arg (stmt
, i
);
6595 search_for_addr_array (arg
, gimple_location (stmt
));
6600 memset (&wi
, 0, sizeof (wi
));
6601 wi
.info
= CONST_CAST (void *, (const void *)
6602 gimple_location_ptr (stmt
));
6604 walk_gimple_op (gsi_stmt (si
),
6612 /* Return true if all imm uses of VAR are either in STMT, or
6613 feed (optionally through a chain of single imm uses) GIMPLE_COND
6614 in basic block COND_BB. */
6617 all_imm_uses_in_stmt_or_feed_cond (tree var
, gimple stmt
, basic_block cond_bb
)
6619 use_operand_p use_p
, use2_p
;
6620 imm_use_iterator iter
;
6622 FOR_EACH_IMM_USE_FAST (use_p
, iter
, var
)
6623 if (USE_STMT (use_p
) != stmt
)
6625 gimple use_stmt
= USE_STMT (use_p
), use_stmt2
;
6626 if (is_gimple_debug (use_stmt
))
6628 while (is_gimple_assign (use_stmt
)
6629 && TREE_CODE (gimple_assign_lhs (use_stmt
)) == SSA_NAME
6630 && single_imm_use (gimple_assign_lhs (use_stmt
),
6631 &use2_p
, &use_stmt2
))
6632 use_stmt
= use_stmt2
;
6633 if (gimple_code (use_stmt
) != GIMPLE_COND
6634 || gimple_bb (use_stmt
) != cond_bb
)
6647 __builtin_unreachable ();
6649 x_5 = ASSERT_EXPR <x_3, ...>;
6650 If x_3 has no other immediate uses (checked by caller),
6651 var is the x_3 var from ASSERT_EXPR, we can clear low 5 bits
6652 from the non-zero bitmask. */
6655 maybe_set_nonzero_bits (basic_block bb
, tree var
)
6657 edge e
= single_pred_edge (bb
);
6658 basic_block cond_bb
= e
->src
;
6659 gimple stmt
= last_stmt (cond_bb
);
6663 || gimple_code (stmt
) != GIMPLE_COND
6664 || gimple_cond_code (stmt
) != ((e
->flags
& EDGE_TRUE_VALUE
)
6665 ? EQ_EXPR
: NE_EXPR
)
6666 || TREE_CODE (gimple_cond_lhs (stmt
)) != SSA_NAME
6667 || !integer_zerop (gimple_cond_rhs (stmt
)))
6670 stmt
= SSA_NAME_DEF_STMT (gimple_cond_lhs (stmt
));
6671 if (!is_gimple_assign (stmt
)
6672 || gimple_assign_rhs_code (stmt
) != BIT_AND_EXPR
6673 || TREE_CODE (gimple_assign_rhs2 (stmt
)) != INTEGER_CST
)
6675 if (gimple_assign_rhs1 (stmt
) != var
)
6679 if (TREE_CODE (gimple_assign_rhs1 (stmt
)) != SSA_NAME
)
6681 stmt2
= SSA_NAME_DEF_STMT (gimple_assign_rhs1 (stmt
));
6682 if (!gimple_assign_cast_p (stmt2
)
6683 || gimple_assign_rhs1 (stmt2
) != var
6684 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (stmt2
))
6685 || (TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (stmt
)))
6686 != TYPE_PRECISION (TREE_TYPE (var
))))
6689 cst
= gimple_assign_rhs2 (stmt
);
6690 set_nonzero_bits (var
, wi::bit_and_not (get_nonzero_bits (var
), cst
));
6693 /* Convert range assertion expressions into the implied copies and
6694 copy propagate away the copies. Doing the trivial copy propagation
6695 here avoids the need to run the full copy propagation pass after
6698 FIXME, this will eventually lead to copy propagation removing the
6699 names that had useful range information attached to them. For
6700 instance, if we had the assertion N_i = ASSERT_EXPR <N_j, N_j > 3>,
6701 then N_i will have the range [3, +INF].
6703 However, by converting the assertion into the implied copy
6704 operation N_i = N_j, we will then copy-propagate N_j into the uses
6705 of N_i and lose the range information. We may want to hold on to
6706 ASSERT_EXPRs a little while longer as the ranges could be used in
6707 things like jump threading.
6709 The problem with keeping ASSERT_EXPRs around is that passes after
6710 VRP need to handle them appropriately.
6712 Another approach would be to make the range information a first
6713 class property of the SSA_NAME so that it can be queried from
6714 any pass. This is made somewhat more complex by the need for
6715 multiple ranges to be associated with one SSA_NAME. */
6718 remove_range_assertions (void)
6721 gimple_stmt_iterator si
;
6722 /* 1 if looking at ASSERT_EXPRs immediately at the beginning of
6723 a basic block preceeded by GIMPLE_COND branching to it and
6724 __builtin_trap, -1 if not yet checked, 0 otherwise. */
6727 /* Note that the BSI iterator bump happens at the bottom of the
6728 loop and no bump is necessary if we're removing the statement
6729 referenced by the current BSI. */
6730 FOR_EACH_BB_FN (bb
, cfun
)
6731 for (si
= gsi_after_labels (bb
), is_unreachable
= -1; !gsi_end_p (si
);)
6733 gimple stmt
= gsi_stmt (si
);
6736 if (is_gimple_assign (stmt
)
6737 && gimple_assign_rhs_code (stmt
) == ASSERT_EXPR
)
6739 tree lhs
= gimple_assign_lhs (stmt
);
6740 tree rhs
= gimple_assign_rhs1 (stmt
);
6742 tree cond
= fold (ASSERT_EXPR_COND (rhs
));
6743 use_operand_p use_p
;
6744 imm_use_iterator iter
;
6746 gcc_assert (cond
!= boolean_false_node
);
6748 var
= ASSERT_EXPR_VAR (rhs
);
6749 gcc_assert (TREE_CODE (var
) == SSA_NAME
);
6751 if (!POINTER_TYPE_P (TREE_TYPE (lhs
))
6752 && SSA_NAME_RANGE_INFO (lhs
))
6754 if (is_unreachable
== -1)
6757 if (single_pred_p (bb
)
6758 && assert_unreachable_fallthru_edge_p
6759 (single_pred_edge (bb
)))
6763 if (x_7 >= 10 && x_7 < 20)
6764 __builtin_unreachable ();
6765 x_8 = ASSERT_EXPR <x_7, ...>;
6766 if the only uses of x_7 are in the ASSERT_EXPR and
6767 in the condition. In that case, we can copy the
6768 range info from x_8 computed in this pass also
6771 && all_imm_uses_in_stmt_or_feed_cond (var
, stmt
,
6774 set_range_info (var
, SSA_NAME_RANGE_TYPE (lhs
),
6775 SSA_NAME_RANGE_INFO (lhs
)->get_min (),
6776 SSA_NAME_RANGE_INFO (lhs
)->get_max ());
6777 maybe_set_nonzero_bits (bb
, var
);
6781 /* Propagate the RHS into every use of the LHS. */
6782 FOR_EACH_IMM_USE_STMT (use_stmt
, iter
, lhs
)
6783 FOR_EACH_IMM_USE_ON_STMT (use_p
, iter
)
6784 SET_USE (use_p
, var
);
6786 /* And finally, remove the copy, it is not needed. */
6787 gsi_remove (&si
, true);
6788 release_defs (stmt
);
6792 if (!is_gimple_debug (gsi_stmt (si
)))
6800 /* Return true if STMT is interesting for VRP. */
6803 stmt_interesting_for_vrp (gimple stmt
)
6805 if (gimple_code (stmt
) == GIMPLE_PHI
)
6807 tree res
= gimple_phi_result (stmt
);
6808 return (!virtual_operand_p (res
)
6809 && (INTEGRAL_TYPE_P (TREE_TYPE (res
))
6810 || POINTER_TYPE_P (TREE_TYPE (res
))));
6812 else if (is_gimple_assign (stmt
) || is_gimple_call (stmt
))
6814 tree lhs
= gimple_get_lhs (stmt
);
6816 /* In general, assignments with virtual operands are not useful
6817 for deriving ranges, with the obvious exception of calls to
6818 builtin functions. */
6819 if (lhs
&& TREE_CODE (lhs
) == SSA_NAME
6820 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs
))
6821 || POINTER_TYPE_P (TREE_TYPE (lhs
)))
6822 && (is_gimple_call (stmt
)
6823 || !gimple_vuse (stmt
)))
6826 else if (gimple_code (stmt
) == GIMPLE_COND
6827 || gimple_code (stmt
) == GIMPLE_SWITCH
)
6834 /* Initialize local data structures for VRP. */
6837 vrp_initialize (void)
6841 values_propagated
= false;
6842 num_vr_values
= num_ssa_names
;
6843 vr_value
= XCNEWVEC (value_range_t
*, num_vr_values
);
6844 vr_phi_edge_counts
= XCNEWVEC (int, num_ssa_names
);
6846 FOR_EACH_BB_FN (bb
, cfun
)
6848 for (gphi_iterator si
= gsi_start_phis (bb
); !gsi_end_p (si
);
6851 gphi
*phi
= si
.phi ();
6852 if (!stmt_interesting_for_vrp (phi
))
6854 tree lhs
= PHI_RESULT (phi
);
6855 set_value_range_to_varying (get_value_range (lhs
));
6856 prop_set_simulate_again (phi
, false);
6859 prop_set_simulate_again (phi
, true);
6862 for (gimple_stmt_iterator si
= gsi_start_bb (bb
); !gsi_end_p (si
);
6865 gimple stmt
= gsi_stmt (si
);
6867 /* If the statement is a control insn, then we do not
6868 want to avoid simulating the statement once. Failure
6869 to do so means that those edges will never get added. */
6870 if (stmt_ends_bb_p (stmt
))
6871 prop_set_simulate_again (stmt
, true);
6872 else if (!stmt_interesting_for_vrp (stmt
))
6876 FOR_EACH_SSA_TREE_OPERAND (def
, stmt
, i
, SSA_OP_DEF
)
6877 set_value_range_to_varying (get_value_range (def
));
6878 prop_set_simulate_again (stmt
, false);
6881 prop_set_simulate_again (stmt
, true);
6886 /* Return the singleton value-range for NAME or NAME. */
6889 vrp_valueize (tree name
)
6891 if (TREE_CODE (name
) == SSA_NAME
)
6893 value_range_t
*vr
= get_value_range (name
);
6894 if (vr
->type
== VR_RANGE
6895 && (vr
->min
== vr
->max
6896 || operand_equal_p (vr
->min
, vr
->max
, 0)))
6902 /* Visit assignment STMT. If it produces an interesting range, record
6903 the SSA name in *OUTPUT_P. */
6905 static enum ssa_prop_result
6906 vrp_visit_assignment_or_call (gimple stmt
, tree
*output_p
)
6910 enum gimple_code code
= gimple_code (stmt
);
6911 lhs
= gimple_get_lhs (stmt
);
6913 /* We only keep track of ranges in integral and pointer types. */
6914 if (TREE_CODE (lhs
) == SSA_NAME
6915 && ((INTEGRAL_TYPE_P (TREE_TYPE (lhs
))
6916 /* It is valid to have NULL MIN/MAX values on a type. See
6917 build_range_type. */
6918 && TYPE_MIN_VALUE (TREE_TYPE (lhs
))
6919 && TYPE_MAX_VALUE (TREE_TYPE (lhs
)))
6920 || POINTER_TYPE_P (TREE_TYPE (lhs
))))
6922 value_range_t new_vr
= VR_INITIALIZER
;
6924 /* Try folding the statement to a constant first. */
6925 tree tem
= gimple_fold_stmt_to_constant (stmt
, vrp_valueize
);
6927 set_value_range_to_value (&new_vr
, tem
, NULL
);
6928 /* Then dispatch to value-range extracting functions. */
6929 else if (code
== GIMPLE_CALL
)
6930 extract_range_basic (&new_vr
, stmt
);
6932 extract_range_from_assignment (&new_vr
, as_a
<gassign
*> (stmt
));
6934 if (update_value_range (lhs
, &new_vr
))
6938 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6940 fprintf (dump_file
, "Found new range for ");
6941 print_generic_expr (dump_file
, lhs
, 0);
6942 fprintf (dump_file
, ": ");
6943 dump_value_range (dump_file
, &new_vr
);
6944 fprintf (dump_file
, "\n");
6947 if (new_vr
.type
== VR_VARYING
)
6948 return SSA_PROP_VARYING
;
6950 return SSA_PROP_INTERESTING
;
6953 return SSA_PROP_NOT_INTERESTING
;
6956 /* Every other statement produces no useful ranges. */
6957 FOR_EACH_SSA_TREE_OPERAND (def
, stmt
, iter
, SSA_OP_DEF
)
6958 set_value_range_to_varying (get_value_range (def
));
6960 return SSA_PROP_VARYING
;
6963 /* Helper that gets the value range of the SSA_NAME with version I
6964 or a symbolic range containing the SSA_NAME only if the value range
6965 is varying or undefined. */
6967 static inline value_range_t
6968 get_vr_for_comparison (int i
)
6970 value_range_t vr
= *get_value_range (ssa_name (i
));
6972 /* If name N_i does not have a valid range, use N_i as its own
6973 range. This allows us to compare against names that may
6974 have N_i in their ranges. */
6975 if (vr
.type
== VR_VARYING
|| vr
.type
== VR_UNDEFINED
)
6978 vr
.min
= ssa_name (i
);
6979 vr
.max
= ssa_name (i
);
6985 /* Compare all the value ranges for names equivalent to VAR with VAL
6986 using comparison code COMP. Return the same value returned by
6987 compare_range_with_value, including the setting of
6988 *STRICT_OVERFLOW_P. */
6991 compare_name_with_value (enum tree_code comp
, tree var
, tree val
,
6992 bool *strict_overflow_p
)
6998 int used_strict_overflow
;
7000 value_range_t equiv_vr
;
7002 /* Get the set of equivalences for VAR. */
7003 e
= get_value_range (var
)->equiv
;
7005 /* Start at -1. Set it to 0 if we do a comparison without relying
7006 on overflow, or 1 if all comparisons rely on overflow. */
7007 used_strict_overflow
= -1;
7009 /* Compare vars' value range with val. */
7010 equiv_vr
= get_vr_for_comparison (SSA_NAME_VERSION (var
));
7012 retval
= compare_range_with_value (comp
, &equiv_vr
, val
, &sop
);
7014 used_strict_overflow
= sop
? 1 : 0;
7016 /* If the equiv set is empty we have done all work we need to do. */
7020 && used_strict_overflow
> 0)
7021 *strict_overflow_p
= true;
7025 EXECUTE_IF_SET_IN_BITMAP (e
, 0, i
, bi
)
7027 equiv_vr
= get_vr_for_comparison (i
);
7029 t
= compare_range_with_value (comp
, &equiv_vr
, val
, &sop
);
7032 /* If we get different answers from different members
7033 of the equivalence set this check must be in a dead
7034 code region. Folding it to a trap representation
7035 would be correct here. For now just return don't-know. */
7045 used_strict_overflow
= 0;
7046 else if (used_strict_overflow
< 0)
7047 used_strict_overflow
= 1;
7052 && used_strict_overflow
> 0)
7053 *strict_overflow_p
= true;
7059 /* Given a comparison code COMP and names N1 and N2, compare all the
7060 ranges equivalent to N1 against all the ranges equivalent to N2
7061 to determine the value of N1 COMP N2. Return the same value
7062 returned by compare_ranges. Set *STRICT_OVERFLOW_P to indicate
7063 whether we relied on an overflow infinity in the comparison. */
7067 compare_names (enum tree_code comp
, tree n1
, tree n2
,
7068 bool *strict_overflow_p
)
7072 bitmap_iterator bi1
, bi2
;
7074 int used_strict_overflow
;
7075 static bitmap_obstack
*s_obstack
= NULL
;
7076 static bitmap s_e1
= NULL
, s_e2
= NULL
;
7078 /* Compare the ranges of every name equivalent to N1 against the
7079 ranges of every name equivalent to N2. */
7080 e1
= get_value_range (n1
)->equiv
;
7081 e2
= get_value_range (n2
)->equiv
;
7083 /* Use the fake bitmaps if e1 or e2 are not available. */
7084 if (s_obstack
== NULL
)
7086 s_obstack
= XNEW (bitmap_obstack
);
7087 bitmap_obstack_initialize (s_obstack
);
7088 s_e1
= BITMAP_ALLOC (s_obstack
);
7089 s_e2
= BITMAP_ALLOC (s_obstack
);
7096 /* Add N1 and N2 to their own set of equivalences to avoid
7097 duplicating the body of the loop just to check N1 and N2
7099 bitmap_set_bit (e1
, SSA_NAME_VERSION (n1
));
7100 bitmap_set_bit (e2
, SSA_NAME_VERSION (n2
));
7102 /* If the equivalence sets have a common intersection, then the two
7103 names can be compared without checking their ranges. */
7104 if (bitmap_intersect_p (e1
, e2
))
7106 bitmap_clear_bit (e1
, SSA_NAME_VERSION (n1
));
7107 bitmap_clear_bit (e2
, SSA_NAME_VERSION (n2
));
7109 return (comp
== EQ_EXPR
|| comp
== GE_EXPR
|| comp
== LE_EXPR
)
7111 : boolean_false_node
;
7114 /* Start at -1. Set it to 0 if we do a comparison without relying
7115 on overflow, or 1 if all comparisons rely on overflow. */
7116 used_strict_overflow
= -1;
7118 /* Otherwise, compare all the equivalent ranges. First, add N1 and
7119 N2 to their own set of equivalences to avoid duplicating the body
7120 of the loop just to check N1 and N2 ranges. */
7121 EXECUTE_IF_SET_IN_BITMAP (e1
, 0, i1
, bi1
)
7123 value_range_t vr1
= get_vr_for_comparison (i1
);
7125 t
= retval
= NULL_TREE
;
7126 EXECUTE_IF_SET_IN_BITMAP (e2
, 0, i2
, bi2
)
7130 value_range_t vr2
= get_vr_for_comparison (i2
);
7132 t
= compare_ranges (comp
, &vr1
, &vr2
, &sop
);
7135 /* If we get different answers from different members
7136 of the equivalence set this check must be in a dead
7137 code region. Folding it to a trap representation
7138 would be correct here. For now just return don't-know. */
7142 bitmap_clear_bit (e1
, SSA_NAME_VERSION (n1
));
7143 bitmap_clear_bit (e2
, SSA_NAME_VERSION (n2
));
7149 used_strict_overflow
= 0;
7150 else if (used_strict_overflow
< 0)
7151 used_strict_overflow
= 1;
7157 bitmap_clear_bit (e1
, SSA_NAME_VERSION (n1
));
7158 bitmap_clear_bit (e2
, SSA_NAME_VERSION (n2
));
7159 if (used_strict_overflow
> 0)
7160 *strict_overflow_p
= true;
7165 /* None of the equivalent ranges are useful in computing this
7167 bitmap_clear_bit (e1
, SSA_NAME_VERSION (n1
));
7168 bitmap_clear_bit (e2
, SSA_NAME_VERSION (n2
));
7172 /* Helper function for vrp_evaluate_conditional_warnv. */
7175 vrp_evaluate_conditional_warnv_with_ops_using_ranges (enum tree_code code
,
7177 bool * strict_overflow_p
)
7179 value_range_t
*vr0
, *vr1
;
7181 vr0
= (TREE_CODE (op0
) == SSA_NAME
) ? get_value_range (op0
) : NULL
;
7182 vr1
= (TREE_CODE (op1
) == SSA_NAME
) ? get_value_range (op1
) : NULL
;
7184 tree res
= NULL_TREE
;
7186 res
= compare_ranges (code
, vr0
, vr1
, strict_overflow_p
);
7188 res
= compare_range_with_value (code
, vr0
, op1
, strict_overflow_p
);
7190 res
= (compare_range_with_value
7191 (swap_tree_comparison (code
), vr1
, op0
, strict_overflow_p
));
7195 /* Helper function for vrp_evaluate_conditional_warnv. */
7198 vrp_evaluate_conditional_warnv_with_ops (enum tree_code code
, tree op0
,
7199 tree op1
, bool use_equiv_p
,
7200 bool *strict_overflow_p
, bool *only_ranges
)
7204 *only_ranges
= true;
7206 /* We only deal with integral and pointer types. */
7207 if (!INTEGRAL_TYPE_P (TREE_TYPE (op0
))
7208 && !POINTER_TYPE_P (TREE_TYPE (op0
)))
7214 && (ret
= vrp_evaluate_conditional_warnv_with_ops_using_ranges
7215 (code
, op0
, op1
, strict_overflow_p
)))
7217 *only_ranges
= false;
7218 if (TREE_CODE (op0
) == SSA_NAME
&& TREE_CODE (op1
) == SSA_NAME
)
7219 return compare_names (code
, op0
, op1
, strict_overflow_p
);
7220 else if (TREE_CODE (op0
) == SSA_NAME
)
7221 return compare_name_with_value (code
, op0
, op1
, strict_overflow_p
);
7222 else if (TREE_CODE (op1
) == SSA_NAME
)
7223 return (compare_name_with_value
7224 (swap_tree_comparison (code
), op1
, op0
, strict_overflow_p
));
7227 return vrp_evaluate_conditional_warnv_with_ops_using_ranges (code
, op0
, op1
,
7232 /* Given (CODE OP0 OP1) within STMT, try to simplify it based on value range
7233 information. Return NULL if the conditional can not be evaluated.
7234 The ranges of all the names equivalent with the operands in COND
7235 will be used when trying to compute the value. If the result is
7236 based on undefined signed overflow, issue a warning if
7240 vrp_evaluate_conditional (enum tree_code code
, tree op0
, tree op1
, gimple stmt
)
7246 /* Some passes and foldings leak constants with overflow flag set
7247 into the IL. Avoid doing wrong things with these and bail out. */
7248 if ((TREE_CODE (op0
) == INTEGER_CST
7249 && TREE_OVERFLOW (op0
))
7250 || (TREE_CODE (op1
) == INTEGER_CST
7251 && TREE_OVERFLOW (op1
)))
7255 ret
= vrp_evaluate_conditional_warnv_with_ops (code
, op0
, op1
, true, &sop
,
7260 enum warn_strict_overflow_code wc
;
7261 const char* warnmsg
;
7263 if (is_gimple_min_invariant (ret
))
7265 wc
= WARN_STRICT_OVERFLOW_CONDITIONAL
;
7266 warnmsg
= G_("assuming signed overflow does not occur when "
7267 "simplifying conditional to constant");
7271 wc
= WARN_STRICT_OVERFLOW_COMPARISON
;
7272 warnmsg
= G_("assuming signed overflow does not occur when "
7273 "simplifying conditional");
7276 if (issue_strict_overflow_warning (wc
))
7278 location_t location
;
7280 if (!gimple_has_location (stmt
))
7281 location
= input_location
;
7283 location
= gimple_location (stmt
);
7284 warning_at (location
, OPT_Wstrict_overflow
, "%s", warnmsg
);
7288 if (warn_type_limits
7289 && ret
&& only_ranges
7290 && TREE_CODE_CLASS (code
) == tcc_comparison
7291 && TREE_CODE (op0
) == SSA_NAME
)
7293 /* If the comparison is being folded and the operand on the LHS
7294 is being compared against a constant value that is outside of
7295 the natural range of OP0's type, then the predicate will
7296 always fold regardless of the value of OP0. If -Wtype-limits
7297 was specified, emit a warning. */
7298 tree type
= TREE_TYPE (op0
);
7299 value_range_t
*vr0
= get_value_range (op0
);
7301 if (vr0
->type
!= VR_VARYING
7302 && INTEGRAL_TYPE_P (type
)
7303 && vrp_val_is_min (vr0
->min
)
7304 && vrp_val_is_max (vr0
->max
)
7305 && is_gimple_min_invariant (op1
))
7307 location_t location
;
7309 if (!gimple_has_location (stmt
))
7310 location
= input_location
;
7312 location
= gimple_location (stmt
);
7314 warning_at (location
, OPT_Wtype_limits
,
7316 ? G_("comparison always false "
7317 "due to limited range of data type")
7318 : G_("comparison always true "
7319 "due to limited range of data type"));
7327 /* Visit conditional statement STMT. If we can determine which edge
7328 will be taken out of STMT's basic block, record it in
7329 *TAKEN_EDGE_P and return SSA_PROP_INTERESTING. Otherwise, return
7330 SSA_PROP_VARYING. */
7332 static enum ssa_prop_result
7333 vrp_visit_cond_stmt (gcond
*stmt
, edge
*taken_edge_p
)
7338 *taken_edge_p
= NULL
;
7340 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7345 fprintf (dump_file
, "\nVisiting conditional with predicate: ");
7346 print_gimple_stmt (dump_file
, stmt
, 0, 0);
7347 fprintf (dump_file
, "\nWith known ranges\n");
7349 FOR_EACH_SSA_TREE_OPERAND (use
, stmt
, i
, SSA_OP_USE
)
7351 fprintf (dump_file
, "\t");
7352 print_generic_expr (dump_file
, use
, 0);
7353 fprintf (dump_file
, ": ");
7354 dump_value_range (dump_file
, vr_value
[SSA_NAME_VERSION (use
)]);
7357 fprintf (dump_file
, "\n");
7360 /* Compute the value of the predicate COND by checking the known
7361 ranges of each of its operands.
7363 Note that we cannot evaluate all the equivalent ranges here
7364 because those ranges may not yet be final and with the current
7365 propagation strategy, we cannot determine when the value ranges
7366 of the names in the equivalence set have changed.
7368 For instance, given the following code fragment
7372 i_14 = ASSERT_EXPR <i_5, i_5 != 0>
7376 Assume that on the first visit to i_14, i_5 has the temporary
7377 range [8, 8] because the second argument to the PHI function is
7378 not yet executable. We derive the range ~[0, 0] for i_14 and the
7379 equivalence set { i_5 }. So, when we visit 'if (i_14 == 1)' for
7380 the first time, since i_14 is equivalent to the range [8, 8], we
7381 determine that the predicate is always false.
7383 On the next round of propagation, i_13 is determined to be
7384 VARYING, which causes i_5 to drop down to VARYING. So, another
7385 visit to i_14 is scheduled. In this second visit, we compute the
7386 exact same range and equivalence set for i_14, namely ~[0, 0] and
7387 { i_5 }. But we did not have the previous range for i_5
7388 registered, so vrp_visit_assignment thinks that the range for
7389 i_14 has not changed. Therefore, the predicate 'if (i_14 == 1)'
7390 is not visited again, which stops propagation from visiting
7391 statements in the THEN clause of that if().
7393 To properly fix this we would need to keep the previous range
7394 value for the names in the equivalence set. This way we would've
7395 discovered that from one visit to the other i_5 changed from
7396 range [8, 8] to VR_VARYING.
7398 However, fixing this apparent limitation may not be worth the
7399 additional checking. Testing on several code bases (GCC, DLV,
7400 MICO, TRAMP3D and SPEC2000) showed that doing this results in
7401 4 more predicates folded in SPEC. */
7404 val
= vrp_evaluate_conditional_warnv_with_ops (gimple_cond_code (stmt
),
7405 gimple_cond_lhs (stmt
),
7406 gimple_cond_rhs (stmt
),
7411 *taken_edge_p
= find_taken_edge (gimple_bb (stmt
), val
);
7414 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7416 "\nIgnoring predicate evaluation because "
7417 "it assumes that signed overflow is undefined");
7422 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7424 fprintf (dump_file
, "\nPredicate evaluates to: ");
7425 if (val
== NULL_TREE
)
7426 fprintf (dump_file
, "DON'T KNOW\n");
7428 print_generic_stmt (dump_file
, val
, 0);
7431 return (*taken_edge_p
) ? SSA_PROP_INTERESTING
: SSA_PROP_VARYING
;
7434 /* Searches the case label vector VEC for the index *IDX of the CASE_LABEL
7435 that includes the value VAL. The search is restricted to the range
7436 [START_IDX, n - 1] where n is the size of VEC.
7438 If there is a CASE_LABEL for VAL, its index is placed in IDX and true is
7441 If there is no CASE_LABEL for VAL and there is one that is larger than VAL,
7442 it is placed in IDX and false is returned.
7444 If VAL is larger than any CASE_LABEL, n is placed on IDX and false is
7448 find_case_label_index (gswitch
*stmt
, size_t start_idx
, tree val
, size_t *idx
)
7450 size_t n
= gimple_switch_num_labels (stmt
);
7453 /* Find case label for minimum of the value range or the next one.
7454 At each iteration we are searching in [low, high - 1]. */
7456 for (low
= start_idx
, high
= n
; high
!= low
; )
7460 /* Note that i != high, so we never ask for n. */
7461 size_t i
= (high
+ low
) / 2;
7462 t
= gimple_switch_label (stmt
, i
);
7464 /* Cache the result of comparing CASE_LOW and val. */
7465 cmp
= tree_int_cst_compare (CASE_LOW (t
), val
);
7469 /* Ranges cannot be empty. */
7478 if (CASE_HIGH (t
) != NULL
7479 && tree_int_cst_compare (CASE_HIGH (t
), val
) >= 0)
7491 /* Searches the case label vector VEC for the range of CASE_LABELs that is used
7492 for values between MIN and MAX. The first index is placed in MIN_IDX. The
7493 last index is placed in MAX_IDX. If the range of CASE_LABELs is empty
7494 then MAX_IDX < MIN_IDX.
7495 Returns true if the default label is not needed. */
7498 find_case_label_range (gswitch
*stmt
, tree min
, tree max
, size_t *min_idx
,
7502 bool min_take_default
= !find_case_label_index (stmt
, 1, min
, &i
);
7503 bool max_take_default
= !find_case_label_index (stmt
, i
, max
, &j
);
7507 && max_take_default
)
7509 /* Only the default case label reached.
7510 Return an empty range. */
7517 bool take_default
= min_take_default
|| max_take_default
;
7521 if (max_take_default
)
7524 /* If the case label range is continuous, we do not need
7525 the default case label. Verify that. */
7526 high
= CASE_LOW (gimple_switch_label (stmt
, i
));
7527 if (CASE_HIGH (gimple_switch_label (stmt
, i
)))
7528 high
= CASE_HIGH (gimple_switch_label (stmt
, i
));
7529 for (k
= i
+ 1; k
<= j
; ++k
)
7531 low
= CASE_LOW (gimple_switch_label (stmt
, k
));
7532 if (!integer_onep (int_const_binop (MINUS_EXPR
, low
, high
)))
7534 take_default
= true;
7538 if (CASE_HIGH (gimple_switch_label (stmt
, k
)))
7539 high
= CASE_HIGH (gimple_switch_label (stmt
, k
));
7544 return !take_default
;
7548 /* Searches the case label vector VEC for the ranges of CASE_LABELs that are
7549 used in range VR. The indices are placed in MIN_IDX1, MAX_IDX, MIN_IDX2 and
7550 MAX_IDX2. If the ranges of CASE_LABELs are empty then MAX_IDX1 < MIN_IDX1.
7551 Returns true if the default label is not needed. */
7554 find_case_label_ranges (gswitch
*stmt
, value_range_t
*vr
, size_t *min_idx1
,
7555 size_t *max_idx1
, size_t *min_idx2
,
7559 unsigned int n
= gimple_switch_num_labels (stmt
);
7561 tree case_low
, case_high
;
7562 tree min
= vr
->min
, max
= vr
->max
;
7564 gcc_checking_assert (vr
->type
== VR_RANGE
|| vr
->type
== VR_ANTI_RANGE
);
7566 take_default
= !find_case_label_range (stmt
, min
, max
, &i
, &j
);
7568 /* Set second range to emtpy. */
7572 if (vr
->type
== VR_RANGE
)
7576 return !take_default
;
7579 /* Set first range to all case labels. */
7586 /* Make sure all the values of case labels [i , j] are contained in
7587 range [MIN, MAX]. */
7588 case_low
= CASE_LOW (gimple_switch_label (stmt
, i
));
7589 case_high
= CASE_HIGH (gimple_switch_label (stmt
, j
));
7590 if (tree_int_cst_compare (case_low
, min
) < 0)
7592 if (case_high
!= NULL_TREE
7593 && tree_int_cst_compare (max
, case_high
) < 0)
7599 /* If the range spans case labels [i, j], the corresponding anti-range spans
7600 the labels [1, i - 1] and [j + 1, n - 1]. */
7626 /* Visit switch statement STMT. If we can determine which edge
7627 will be taken out of STMT's basic block, record it in
7628 *TAKEN_EDGE_P and return SSA_PROP_INTERESTING. Otherwise, return
7629 SSA_PROP_VARYING. */
7631 static enum ssa_prop_result
7632 vrp_visit_switch_stmt (gswitch
*stmt
, edge
*taken_edge_p
)
7636 size_t i
= 0, j
= 0, k
, l
;
7639 *taken_edge_p
= NULL
;
7640 op
= gimple_switch_index (stmt
);
7641 if (TREE_CODE (op
) != SSA_NAME
)
7642 return SSA_PROP_VARYING
;
7644 vr
= get_value_range (op
);
7645 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7647 fprintf (dump_file
, "\nVisiting switch expression with operand ");
7648 print_generic_expr (dump_file
, op
, 0);
7649 fprintf (dump_file
, " with known range ");
7650 dump_value_range (dump_file
, vr
);
7651 fprintf (dump_file
, "\n");
7654 if ((vr
->type
!= VR_RANGE
7655 && vr
->type
!= VR_ANTI_RANGE
)
7656 || symbolic_range_p (vr
))
7657 return SSA_PROP_VARYING
;
7659 /* Find the single edge that is taken from the switch expression. */
7660 take_default
= !find_case_label_ranges (stmt
, vr
, &i
, &j
, &k
, &l
);
7662 /* Check if the range spans no CASE_LABEL. If so, we only reach the default
7666 gcc_assert (take_default
);
7667 val
= gimple_switch_default_label (stmt
);
7671 /* Check if labels with index i to j and maybe the default label
7672 are all reaching the same label. */
7674 val
= gimple_switch_label (stmt
, i
);
7676 && CASE_LABEL (gimple_switch_default_label (stmt
))
7677 != CASE_LABEL (val
))
7679 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7680 fprintf (dump_file
, " not a single destination for this "
7682 return SSA_PROP_VARYING
;
7684 for (++i
; i
<= j
; ++i
)
7686 if (CASE_LABEL (gimple_switch_label (stmt
, i
)) != CASE_LABEL (val
))
7688 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7689 fprintf (dump_file
, " not a single destination for this "
7691 return SSA_PROP_VARYING
;
7696 if (CASE_LABEL (gimple_switch_label (stmt
, k
)) != CASE_LABEL (val
))
7698 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7699 fprintf (dump_file
, " not a single destination for this "
7701 return SSA_PROP_VARYING
;
7706 *taken_edge_p
= find_edge (gimple_bb (stmt
),
7707 label_to_block (CASE_LABEL (val
)));
7709 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7711 fprintf (dump_file
, " will take edge to ");
7712 print_generic_stmt (dump_file
, CASE_LABEL (val
), 0);
7715 return SSA_PROP_INTERESTING
;
7719 /* Evaluate statement STMT. If the statement produces a useful range,
7720 return SSA_PROP_INTERESTING and record the SSA name with the
7721 interesting range into *OUTPUT_P.
7723 If STMT is a conditional branch and we can determine its truth
7724 value, the taken edge is recorded in *TAKEN_EDGE_P.
7726 If STMT produces a varying value, return SSA_PROP_VARYING. */
7728 static enum ssa_prop_result
7729 vrp_visit_stmt (gimple stmt
, edge
*taken_edge_p
, tree
*output_p
)
7734 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7736 fprintf (dump_file
, "\nVisiting statement:\n");
7737 print_gimple_stmt (dump_file
, stmt
, 0, dump_flags
);
7740 if (!stmt_interesting_for_vrp (stmt
))
7741 gcc_assert (stmt_ends_bb_p (stmt
));
7742 else if (is_gimple_assign (stmt
) || is_gimple_call (stmt
))
7743 return vrp_visit_assignment_or_call (stmt
, output_p
);
7744 else if (gimple_code (stmt
) == GIMPLE_COND
)
7745 return vrp_visit_cond_stmt (as_a
<gcond
*> (stmt
), taken_edge_p
);
7746 else if (gimple_code (stmt
) == GIMPLE_SWITCH
)
7747 return vrp_visit_switch_stmt (as_a
<gswitch
*> (stmt
), taken_edge_p
);
7749 /* All other statements produce nothing of interest for VRP, so mark
7750 their outputs varying and prevent further simulation. */
7751 FOR_EACH_SSA_TREE_OPERAND (def
, stmt
, iter
, SSA_OP_DEF
)
7752 set_value_range_to_varying (get_value_range (def
));
7754 return SSA_PROP_VARYING
;
7757 /* Union the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
7758 { VR1TYPE, VR0MIN, VR0MAX } and store the result
7759 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
7760 possible such range. The resulting range is not canonicalized. */
7763 union_ranges (enum value_range_type
*vr0type
,
7764 tree
*vr0min
, tree
*vr0max
,
7765 enum value_range_type vr1type
,
7766 tree vr1min
, tree vr1max
)
7768 bool mineq
= operand_equal_p (*vr0min
, vr1min
, 0);
7769 bool maxeq
= operand_equal_p (*vr0max
, vr1max
, 0);
7771 /* [] is vr0, () is vr1 in the following classification comments. */
7775 if (*vr0type
== vr1type
)
7776 /* Nothing to do for equal ranges. */
7778 else if ((*vr0type
== VR_RANGE
7779 && vr1type
== VR_ANTI_RANGE
)
7780 || (*vr0type
== VR_ANTI_RANGE
7781 && vr1type
== VR_RANGE
))
7783 /* For anti-range with range union the result is varying. */
7789 else if (operand_less_p (*vr0max
, vr1min
) == 1
7790 || operand_less_p (vr1max
, *vr0min
) == 1)
7792 /* [ ] ( ) or ( ) [ ]
7793 If the ranges have an empty intersection, result of the union
7794 operation is the anti-range or if both are anti-ranges
7796 if (*vr0type
== VR_ANTI_RANGE
7797 && vr1type
== VR_ANTI_RANGE
)
7799 else if (*vr0type
== VR_ANTI_RANGE
7800 && vr1type
== VR_RANGE
)
7802 else if (*vr0type
== VR_RANGE
7803 && vr1type
== VR_ANTI_RANGE
)
7809 else if (*vr0type
== VR_RANGE
7810 && vr1type
== VR_RANGE
)
7812 /* The result is the convex hull of both ranges. */
7813 if (operand_less_p (*vr0max
, vr1min
) == 1)
7815 /* If the result can be an anti-range, create one. */
7816 if (TREE_CODE (*vr0max
) == INTEGER_CST
7817 && TREE_CODE (vr1min
) == INTEGER_CST
7818 && vrp_val_is_min (*vr0min
)
7819 && vrp_val_is_max (vr1max
))
7821 tree min
= int_const_binop (PLUS_EXPR
,
7823 build_int_cst (TREE_TYPE (*vr0max
), 1));
7824 tree max
= int_const_binop (MINUS_EXPR
,
7826 build_int_cst (TREE_TYPE (vr1min
), 1));
7827 if (!operand_less_p (max
, min
))
7829 *vr0type
= VR_ANTI_RANGE
;
7841 /* If the result can be an anti-range, create one. */
7842 if (TREE_CODE (vr1max
) == INTEGER_CST
7843 && TREE_CODE (*vr0min
) == INTEGER_CST
7844 && vrp_val_is_min (vr1min
)
7845 && vrp_val_is_max (*vr0max
))
7847 tree min
= int_const_binop (PLUS_EXPR
,
7849 build_int_cst (TREE_TYPE (vr1max
), 1));
7850 tree max
= int_const_binop (MINUS_EXPR
,
7852 build_int_cst (TREE_TYPE (*vr0min
), 1));
7853 if (!operand_less_p (max
, min
))
7855 *vr0type
= VR_ANTI_RANGE
;
7869 else if ((maxeq
|| operand_less_p (vr1max
, *vr0max
) == 1)
7870 && (mineq
|| operand_less_p (*vr0min
, vr1min
) == 1))
7872 /* [ ( ) ] or [( ) ] or [ ( )] */
7873 if (*vr0type
== VR_RANGE
7874 && vr1type
== VR_RANGE
)
7876 else if (*vr0type
== VR_ANTI_RANGE
7877 && vr1type
== VR_ANTI_RANGE
)
7883 else if (*vr0type
== VR_ANTI_RANGE
7884 && vr1type
== VR_RANGE
)
7886 /* Arbitrarily choose the right or left gap. */
7887 if (!mineq
&& TREE_CODE (vr1min
) == INTEGER_CST
)
7888 *vr0max
= int_const_binop (MINUS_EXPR
, vr1min
,
7889 build_int_cst (TREE_TYPE (vr1min
), 1));
7890 else if (!maxeq
&& TREE_CODE (vr1max
) == INTEGER_CST
)
7891 *vr0min
= int_const_binop (PLUS_EXPR
, vr1max
,
7892 build_int_cst (TREE_TYPE (vr1max
), 1));
7896 else if (*vr0type
== VR_RANGE
7897 && vr1type
== VR_ANTI_RANGE
)
7898 /* The result covers everything. */
7903 else if ((maxeq
|| operand_less_p (*vr0max
, vr1max
) == 1)
7904 && (mineq
|| operand_less_p (vr1min
, *vr0min
) == 1))
7906 /* ( [ ] ) or ([ ] ) or ( [ ]) */
7907 if (*vr0type
== VR_RANGE
7908 && vr1type
== VR_RANGE
)
7914 else if (*vr0type
== VR_ANTI_RANGE
7915 && vr1type
== VR_ANTI_RANGE
)
7917 else if (*vr0type
== VR_RANGE
7918 && vr1type
== VR_ANTI_RANGE
)
7920 *vr0type
= VR_ANTI_RANGE
;
7921 if (!mineq
&& TREE_CODE (*vr0min
) == INTEGER_CST
)
7923 *vr0max
= int_const_binop (MINUS_EXPR
, *vr0min
,
7924 build_int_cst (TREE_TYPE (*vr0min
), 1));
7927 else if (!maxeq
&& TREE_CODE (*vr0max
) == INTEGER_CST
)
7929 *vr0min
= int_const_binop (PLUS_EXPR
, *vr0max
,
7930 build_int_cst (TREE_TYPE (*vr0max
), 1));
7936 else if (*vr0type
== VR_ANTI_RANGE
7937 && vr1type
== VR_RANGE
)
7938 /* The result covers everything. */
7943 else if ((operand_less_p (vr1min
, *vr0max
) == 1
7944 || operand_equal_p (vr1min
, *vr0max
, 0))
7945 && operand_less_p (*vr0min
, vr1min
) == 1
7946 && operand_less_p (*vr0max
, vr1max
) == 1)
7948 /* [ ( ] ) or [ ]( ) */
7949 if (*vr0type
== VR_RANGE
7950 && vr1type
== VR_RANGE
)
7952 else if (*vr0type
== VR_ANTI_RANGE
7953 && vr1type
== VR_ANTI_RANGE
)
7955 else if (*vr0type
== VR_ANTI_RANGE
7956 && vr1type
== VR_RANGE
)
7958 if (TREE_CODE (vr1min
) == INTEGER_CST
)
7959 *vr0max
= int_const_binop (MINUS_EXPR
, vr1min
,
7960 build_int_cst (TREE_TYPE (vr1min
), 1));
7964 else if (*vr0type
== VR_RANGE
7965 && vr1type
== VR_ANTI_RANGE
)
7967 if (TREE_CODE (*vr0max
) == INTEGER_CST
)
7970 *vr0min
= int_const_binop (PLUS_EXPR
, *vr0max
,
7971 build_int_cst (TREE_TYPE (*vr0max
), 1));
7980 else if ((operand_less_p (*vr0min
, vr1max
) == 1
7981 || operand_equal_p (*vr0min
, vr1max
, 0))
7982 && operand_less_p (vr1min
, *vr0min
) == 1
7983 && operand_less_p (vr1max
, *vr0max
) == 1)
7985 /* ( [ ) ] or ( )[ ] */
7986 if (*vr0type
== VR_RANGE
7987 && vr1type
== VR_RANGE
)
7989 else if (*vr0type
== VR_ANTI_RANGE
7990 && vr1type
== VR_ANTI_RANGE
)
7992 else if (*vr0type
== VR_ANTI_RANGE
7993 && vr1type
== VR_RANGE
)
7995 if (TREE_CODE (vr1max
) == INTEGER_CST
)
7996 *vr0min
= int_const_binop (PLUS_EXPR
, vr1max
,
7997 build_int_cst (TREE_TYPE (vr1max
), 1));
8001 else if (*vr0type
== VR_RANGE
8002 && vr1type
== VR_ANTI_RANGE
)
8004 if (TREE_CODE (*vr0min
) == INTEGER_CST
)
8008 *vr0max
= int_const_binop (MINUS_EXPR
, *vr0min
,
8009 build_int_cst (TREE_TYPE (*vr0min
), 1));
8023 *vr0type
= VR_VARYING
;
8024 *vr0min
= NULL_TREE
;
8025 *vr0max
= NULL_TREE
;
8028 /* Intersect the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
8029 { VR1TYPE, VR0MIN, VR0MAX } and store the result
8030 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
8031 possible such range. The resulting range is not canonicalized. */
8034 intersect_ranges (enum value_range_type
*vr0type
,
8035 tree
*vr0min
, tree
*vr0max
,
8036 enum value_range_type vr1type
,
8037 tree vr1min
, tree vr1max
)
8039 bool mineq
= operand_equal_p (*vr0min
, vr1min
, 0);
8040 bool maxeq
= operand_equal_p (*vr0max
, vr1max
, 0);
8042 /* [] is vr0, () is vr1 in the following classification comments. */
8046 if (*vr0type
== vr1type
)
8047 /* Nothing to do for equal ranges. */
8049 else if ((*vr0type
== VR_RANGE
8050 && vr1type
== VR_ANTI_RANGE
)
8051 || (*vr0type
== VR_ANTI_RANGE
8052 && vr1type
== VR_RANGE
))
8054 /* For anti-range with range intersection the result is empty. */
8055 *vr0type
= VR_UNDEFINED
;
8056 *vr0min
= NULL_TREE
;
8057 *vr0max
= NULL_TREE
;
8062 else if (operand_less_p (*vr0max
, vr1min
) == 1
8063 || operand_less_p (vr1max
, *vr0min
) == 1)
8065 /* [ ] ( ) or ( ) [ ]
8066 If the ranges have an empty intersection, the result of the
8067 intersect operation is the range for intersecting an
8068 anti-range with a range or empty when intersecting two ranges. */
8069 if (*vr0type
== VR_RANGE
8070 && vr1type
== VR_ANTI_RANGE
)
8072 else if (*vr0type
== VR_ANTI_RANGE
8073 && vr1type
== VR_RANGE
)
8079 else if (*vr0type
== VR_RANGE
8080 && vr1type
== VR_RANGE
)
8082 *vr0type
= VR_UNDEFINED
;
8083 *vr0min
= NULL_TREE
;
8084 *vr0max
= NULL_TREE
;
8086 else if (*vr0type
== VR_ANTI_RANGE
8087 && vr1type
== VR_ANTI_RANGE
)
8089 /* If the anti-ranges are adjacent to each other merge them. */
8090 if (TREE_CODE (*vr0max
) == INTEGER_CST
8091 && TREE_CODE (vr1min
) == INTEGER_CST
8092 && operand_less_p (*vr0max
, vr1min
) == 1
8093 && integer_onep (int_const_binop (MINUS_EXPR
,
8096 else if (TREE_CODE (vr1max
) == INTEGER_CST
8097 && TREE_CODE (*vr0min
) == INTEGER_CST
8098 && operand_less_p (vr1max
, *vr0min
) == 1
8099 && integer_onep (int_const_binop (MINUS_EXPR
,
8102 /* Else arbitrarily take VR0. */
8105 else if ((maxeq
|| operand_less_p (vr1max
, *vr0max
) == 1)
8106 && (mineq
|| operand_less_p (*vr0min
, vr1min
) == 1))
8108 /* [ ( ) ] or [( ) ] or [ ( )] */
8109 if (*vr0type
== VR_RANGE
8110 && vr1type
== VR_RANGE
)
8112 /* If both are ranges the result is the inner one. */
8117 else if (*vr0type
== VR_RANGE
8118 && vr1type
== VR_ANTI_RANGE
)
8120 /* Choose the right gap if the left one is empty. */
8123 if (TREE_CODE (vr1max
) == INTEGER_CST
)
8124 *vr0min
= int_const_binop (PLUS_EXPR
, vr1max
,
8125 build_int_cst (TREE_TYPE (vr1max
), 1));
8129 /* Choose the left gap if the right one is empty. */
8132 if (TREE_CODE (vr1min
) == INTEGER_CST
)
8133 *vr0max
= int_const_binop (MINUS_EXPR
, vr1min
,
8134 build_int_cst (TREE_TYPE (vr1min
), 1));
8138 /* Choose the anti-range if the range is effectively varying. */
8139 else if (vrp_val_is_min (*vr0min
)
8140 && vrp_val_is_max (*vr0max
))
8146 /* Else choose the range. */
8148 else if (*vr0type
== VR_ANTI_RANGE
8149 && vr1type
== VR_ANTI_RANGE
)
8150 /* If both are anti-ranges the result is the outer one. */
8152 else if (*vr0type
== VR_ANTI_RANGE
8153 && vr1type
== VR_RANGE
)
8155 /* The intersection is empty. */
8156 *vr0type
= VR_UNDEFINED
;
8157 *vr0min
= NULL_TREE
;
8158 *vr0max
= NULL_TREE
;
8163 else if ((maxeq
|| operand_less_p (*vr0max
, vr1max
) == 1)
8164 && (mineq
|| operand_less_p (vr1min
, *vr0min
) == 1))
8166 /* ( [ ] ) or ([ ] ) or ( [ ]) */
8167 if (*vr0type
== VR_RANGE
8168 && vr1type
== VR_RANGE
)
8169 /* Choose the inner range. */
8171 else if (*vr0type
== VR_ANTI_RANGE
8172 && vr1type
== VR_RANGE
)
8174 /* Choose the right gap if the left is empty. */
8177 *vr0type
= VR_RANGE
;
8178 if (TREE_CODE (*vr0max
) == INTEGER_CST
)
8179 *vr0min
= int_const_binop (PLUS_EXPR
, *vr0max
,
8180 build_int_cst (TREE_TYPE (*vr0max
), 1));
8185 /* Choose the left gap if the right is empty. */
8188 *vr0type
= VR_RANGE
;
8189 if (TREE_CODE (*vr0min
) == INTEGER_CST
)
8190 *vr0max
= int_const_binop (MINUS_EXPR
, *vr0min
,
8191 build_int_cst (TREE_TYPE (*vr0min
), 1));
8196 /* Choose the anti-range if the range is effectively varying. */
8197 else if (vrp_val_is_min (vr1min
)
8198 && vrp_val_is_max (vr1max
))
8200 /* Else choose the range. */
8208 else if (*vr0type
== VR_ANTI_RANGE
8209 && vr1type
== VR_ANTI_RANGE
)
8211 /* If both are anti-ranges the result is the outer one. */
8216 else if (vr1type
== VR_ANTI_RANGE
8217 && *vr0type
== VR_RANGE
)
8219 /* The intersection is empty. */
8220 *vr0type
= VR_UNDEFINED
;
8221 *vr0min
= NULL_TREE
;
8222 *vr0max
= NULL_TREE
;
8227 else if ((operand_less_p (vr1min
, *vr0max
) == 1
8228 || operand_equal_p (vr1min
, *vr0max
, 0))
8229 && operand_less_p (*vr0min
, vr1min
) == 1)
8231 /* [ ( ] ) or [ ]( ) */
8232 if (*vr0type
== VR_ANTI_RANGE
8233 && vr1type
== VR_ANTI_RANGE
)
8235 else if (*vr0type
== VR_RANGE
8236 && vr1type
== VR_RANGE
)
8238 else if (*vr0type
== VR_RANGE
8239 && vr1type
== VR_ANTI_RANGE
)
8241 if (TREE_CODE (vr1min
) == INTEGER_CST
)
8242 *vr0max
= int_const_binop (MINUS_EXPR
, vr1min
,
8243 build_int_cst (TREE_TYPE (vr1min
), 1));
8247 else if (*vr0type
== VR_ANTI_RANGE
8248 && vr1type
== VR_RANGE
)
8250 *vr0type
= VR_RANGE
;
8251 if (TREE_CODE (*vr0max
) == INTEGER_CST
)
8252 *vr0min
= int_const_binop (PLUS_EXPR
, *vr0max
,
8253 build_int_cst (TREE_TYPE (*vr0max
), 1));
8261 else if ((operand_less_p (*vr0min
, vr1max
) == 1
8262 || operand_equal_p (*vr0min
, vr1max
, 0))
8263 && operand_less_p (vr1min
, *vr0min
) == 1)
8265 /* ( [ ) ] or ( )[ ] */
8266 if (*vr0type
== VR_ANTI_RANGE
8267 && vr1type
== VR_ANTI_RANGE
)
8269 else if (*vr0type
== VR_RANGE
8270 && vr1type
== VR_RANGE
)
8272 else if (*vr0type
== VR_RANGE
8273 && vr1type
== VR_ANTI_RANGE
)
8275 if (TREE_CODE (vr1max
) == INTEGER_CST
)
8276 *vr0min
= int_const_binop (PLUS_EXPR
, vr1max
,
8277 build_int_cst (TREE_TYPE (vr1max
), 1));
8281 else if (*vr0type
== VR_ANTI_RANGE
8282 && vr1type
== VR_RANGE
)
8284 *vr0type
= VR_RANGE
;
8285 if (TREE_CODE (*vr0min
) == INTEGER_CST
)
8286 *vr0max
= int_const_binop (MINUS_EXPR
, *vr0min
,
8287 build_int_cst (TREE_TYPE (*vr0min
), 1));
8296 /* As a fallback simply use { *VRTYPE, *VR0MIN, *VR0MAX } as
8297 result for the intersection. That's always a conservative
8298 correct estimate. */
8304 /* Intersect the two value-ranges *VR0 and *VR1 and store the result
8305 in *VR0. This may not be the smallest possible such range. */
8308 vrp_intersect_ranges_1 (value_range_t
*vr0
, value_range_t
*vr1
)
8310 value_range_t saved
;
8312 /* If either range is VR_VARYING the other one wins. */
8313 if (vr1
->type
== VR_VARYING
)
8315 if (vr0
->type
== VR_VARYING
)
8317 copy_value_range (vr0
, vr1
);
8321 /* When either range is VR_UNDEFINED the resulting range is
8322 VR_UNDEFINED, too. */
8323 if (vr0
->type
== VR_UNDEFINED
)
8325 if (vr1
->type
== VR_UNDEFINED
)
8327 set_value_range_to_undefined (vr0
);
8331 /* Save the original vr0 so we can return it as conservative intersection
8332 result when our worker turns things to varying. */
8334 intersect_ranges (&vr0
->type
, &vr0
->min
, &vr0
->max
,
8335 vr1
->type
, vr1
->min
, vr1
->max
);
8336 /* Make sure to canonicalize the result though as the inversion of a
8337 VR_RANGE can still be a VR_RANGE. */
8338 set_and_canonicalize_value_range (vr0
, vr0
->type
,
8339 vr0
->min
, vr0
->max
, vr0
->equiv
);
8340 /* If that failed, use the saved original VR0. */
8341 if (vr0
->type
== VR_VARYING
)
8346 /* If the result is VR_UNDEFINED there is no need to mess with
8347 the equivalencies. */
8348 if (vr0
->type
== VR_UNDEFINED
)
8351 /* The resulting set of equivalences for range intersection is the union of
8353 if (vr0
->equiv
&& vr1
->equiv
&& vr0
->equiv
!= vr1
->equiv
)
8354 bitmap_ior_into (vr0
->equiv
, vr1
->equiv
);
8355 else if (vr1
->equiv
&& !vr0
->equiv
)
8356 bitmap_copy (vr0
->equiv
, vr1
->equiv
);
8360 vrp_intersect_ranges (value_range_t
*vr0
, value_range_t
*vr1
)
8362 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8364 fprintf (dump_file
, "Intersecting\n ");
8365 dump_value_range (dump_file
, vr0
);
8366 fprintf (dump_file
, "\nand\n ");
8367 dump_value_range (dump_file
, vr1
);
8368 fprintf (dump_file
, "\n");
8370 vrp_intersect_ranges_1 (vr0
, vr1
);
8371 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8373 fprintf (dump_file
, "to\n ");
8374 dump_value_range (dump_file
, vr0
);
8375 fprintf (dump_file
, "\n");
8379 /* Meet operation for value ranges. Given two value ranges VR0 and
8380 VR1, store in VR0 a range that contains both VR0 and VR1. This
8381 may not be the smallest possible such range. */
8384 vrp_meet_1 (value_range_t
*vr0
, value_range_t
*vr1
)
8386 value_range_t saved
;
8388 if (vr0
->type
== VR_UNDEFINED
)
8390 set_value_range (vr0
, vr1
->type
, vr1
->min
, vr1
->max
, vr1
->equiv
);
8394 if (vr1
->type
== VR_UNDEFINED
)
8396 /* VR0 already has the resulting range. */
8400 if (vr0
->type
== VR_VARYING
)
8402 /* Nothing to do. VR0 already has the resulting range. */
8406 if (vr1
->type
== VR_VARYING
)
8408 set_value_range_to_varying (vr0
);
8413 union_ranges (&vr0
->type
, &vr0
->min
, &vr0
->max
,
8414 vr1
->type
, vr1
->min
, vr1
->max
);
8415 if (vr0
->type
== VR_VARYING
)
8417 /* Failed to find an efficient meet. Before giving up and setting
8418 the result to VARYING, see if we can at least derive a useful
8419 anti-range. FIXME, all this nonsense about distinguishing
8420 anti-ranges from ranges is necessary because of the odd
8421 semantics of range_includes_zero_p and friends. */
8422 if (((saved
.type
== VR_RANGE
8423 && range_includes_zero_p (saved
.min
, saved
.max
) == 0)
8424 || (saved
.type
== VR_ANTI_RANGE
8425 && range_includes_zero_p (saved
.min
, saved
.max
) == 1))
8426 && ((vr1
->type
== VR_RANGE
8427 && range_includes_zero_p (vr1
->min
, vr1
->max
) == 0)
8428 || (vr1
->type
== VR_ANTI_RANGE
8429 && range_includes_zero_p (vr1
->min
, vr1
->max
) == 1)))
8431 set_value_range_to_nonnull (vr0
, TREE_TYPE (saved
.min
));
8433 /* Since this meet operation did not result from the meeting of
8434 two equivalent names, VR0 cannot have any equivalences. */
8436 bitmap_clear (vr0
->equiv
);
8440 set_value_range_to_varying (vr0
);
8443 set_and_canonicalize_value_range (vr0
, vr0
->type
, vr0
->min
, vr0
->max
,
8445 if (vr0
->type
== VR_VARYING
)
8448 /* The resulting set of equivalences is always the intersection of
8450 if (vr0
->equiv
&& vr1
->equiv
&& vr0
->equiv
!= vr1
->equiv
)
8451 bitmap_and_into (vr0
->equiv
, vr1
->equiv
);
8452 else if (vr0
->equiv
&& !vr1
->equiv
)
8453 bitmap_clear (vr0
->equiv
);
8457 vrp_meet (value_range_t
*vr0
, value_range_t
*vr1
)
8459 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8461 fprintf (dump_file
, "Meeting\n ");
8462 dump_value_range (dump_file
, vr0
);
8463 fprintf (dump_file
, "\nand\n ");
8464 dump_value_range (dump_file
, vr1
);
8465 fprintf (dump_file
, "\n");
8467 vrp_meet_1 (vr0
, vr1
);
8468 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8470 fprintf (dump_file
, "to\n ");
8471 dump_value_range (dump_file
, vr0
);
8472 fprintf (dump_file
, "\n");
8477 /* Visit all arguments for PHI node PHI that flow through executable
8478 edges. If a valid value range can be derived from all the incoming
8479 value ranges, set a new range for the LHS of PHI. */
8481 static enum ssa_prop_result
8482 vrp_visit_phi_node (gphi
*phi
)
8485 tree lhs
= PHI_RESULT (phi
);
8486 value_range_t
*lhs_vr
= get_value_range (lhs
);
8487 value_range_t vr_result
= VR_INITIALIZER
;
8489 int edges
, old_edges
;
8492 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8494 fprintf (dump_file
, "\nVisiting PHI node: ");
8495 print_gimple_stmt (dump_file
, phi
, 0, dump_flags
);
8499 for (i
= 0; i
< gimple_phi_num_args (phi
); i
++)
8501 edge e
= gimple_phi_arg_edge (phi
, i
);
8503 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8506 " Argument #%d (%d -> %d %sexecutable)\n",
8507 (int) i
, e
->src
->index
, e
->dest
->index
,
8508 (e
->flags
& EDGE_EXECUTABLE
) ? "" : "not ");
8511 if (e
->flags
& EDGE_EXECUTABLE
)
8513 tree arg
= PHI_ARG_DEF (phi
, i
);
8514 value_range_t vr_arg
;
8518 if (TREE_CODE (arg
) == SSA_NAME
)
8520 vr_arg
= *(get_value_range (arg
));
8521 /* Do not allow equivalences or symbolic ranges to leak in from
8522 backedges. That creates invalid equivalencies.
8523 See PR53465 and PR54767. */
8524 if (e
->flags
& EDGE_DFS_BACK
)
8526 if (vr_arg
.type
== VR_RANGE
8527 || vr_arg
.type
== VR_ANTI_RANGE
)
8529 vr_arg
.equiv
= NULL
;
8530 if (symbolic_range_p (&vr_arg
))
8532 vr_arg
.type
= VR_VARYING
;
8533 vr_arg
.min
= NULL_TREE
;
8534 vr_arg
.max
= NULL_TREE
;
8540 /* If the non-backedge arguments range is VR_VARYING then
8541 we can still try recording a simple equivalence. */
8542 if (vr_arg
.type
== VR_VARYING
)
8544 vr_arg
.type
= VR_RANGE
;
8547 vr_arg
.equiv
= NULL
;
8553 if (TREE_OVERFLOW_P (arg
))
8554 arg
= drop_tree_overflow (arg
);
8556 vr_arg
.type
= VR_RANGE
;
8559 vr_arg
.equiv
= NULL
;
8562 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8564 fprintf (dump_file
, "\t");
8565 print_generic_expr (dump_file
, arg
, dump_flags
);
8566 fprintf (dump_file
, ": ");
8567 dump_value_range (dump_file
, &vr_arg
);
8568 fprintf (dump_file
, "\n");
8572 copy_value_range (&vr_result
, &vr_arg
);
8574 vrp_meet (&vr_result
, &vr_arg
);
8577 if (vr_result
.type
== VR_VARYING
)
8582 if (vr_result
.type
== VR_VARYING
)
8584 else if (vr_result
.type
== VR_UNDEFINED
)
8587 old_edges
= vr_phi_edge_counts
[SSA_NAME_VERSION (lhs
)];
8588 vr_phi_edge_counts
[SSA_NAME_VERSION (lhs
)] = edges
;
8590 /* To prevent infinite iterations in the algorithm, derive ranges
8591 when the new value is slightly bigger or smaller than the
8592 previous one. We don't do this if we have seen a new executable
8593 edge; this helps us avoid an overflow infinity for conditionals
8594 which are not in a loop. If the old value-range was VR_UNDEFINED
8595 use the updated range and iterate one more time. */
8597 && gimple_phi_num_args (phi
) > 1
8598 && edges
== old_edges
8599 && lhs_vr
->type
!= VR_UNDEFINED
)
8601 /* Compare old and new ranges, fall back to varying if the
8602 values are not comparable. */
8603 int cmp_min
= compare_values (lhs_vr
->min
, vr_result
.min
);
8606 int cmp_max
= compare_values (lhs_vr
->max
, vr_result
.max
);
8610 /* For non VR_RANGE or for pointers fall back to varying if
8611 the range changed. */
8612 if ((lhs_vr
->type
!= VR_RANGE
|| vr_result
.type
!= VR_RANGE
8613 || POINTER_TYPE_P (TREE_TYPE (lhs
)))
8614 && (cmp_min
!= 0 || cmp_max
!= 0))
8617 /* If the new minimum is larger than than the previous one
8618 retain the old value. If the new minimum value is smaller
8619 than the previous one and not -INF go all the way to -INF + 1.
8620 In the first case, to avoid infinite bouncing between different
8621 minimums, and in the other case to avoid iterating millions of
8622 times to reach -INF. Going to -INF + 1 also lets the following
8623 iteration compute whether there will be any overflow, at the
8624 expense of one additional iteration. */
8626 vr_result
.min
= lhs_vr
->min
;
8627 else if (cmp_min
> 0
8628 && !vrp_val_is_min (vr_result
.min
))
8630 = int_const_binop (PLUS_EXPR
,
8631 vrp_val_min (TREE_TYPE (vr_result
.min
)),
8632 build_int_cst (TREE_TYPE (vr_result
.min
), 1));
8634 /* Similarly for the maximum value. */
8636 vr_result
.max
= lhs_vr
->max
;
8637 else if (cmp_max
< 0
8638 && !vrp_val_is_max (vr_result
.max
))
8640 = int_const_binop (MINUS_EXPR
,
8641 vrp_val_max (TREE_TYPE (vr_result
.min
)),
8642 build_int_cst (TREE_TYPE (vr_result
.min
), 1));
8644 /* If we dropped either bound to +-INF then if this is a loop
8645 PHI node SCEV may known more about its value-range. */
8646 if ((cmp_min
> 0 || cmp_min
< 0
8647 || cmp_max
< 0 || cmp_max
> 0)
8648 && (l
= loop_containing_stmt (phi
))
8649 && l
->header
== gimple_bb (phi
))
8650 adjust_range_with_scev (&vr_result
, l
, phi
, lhs
);
8652 /* If we will end up with a (-INF, +INF) range, set it to
8653 VARYING. Same if the previous max value was invalid for
8654 the type and we end up with vr_result.min > vr_result.max. */
8655 if ((vrp_val_is_max (vr_result
.max
)
8656 && vrp_val_is_min (vr_result
.min
))
8657 || compare_values (vr_result
.min
,
8662 /* If the new range is different than the previous value, keep
8665 if (update_value_range (lhs
, &vr_result
))
8667 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8669 fprintf (dump_file
, "Found new range for ");
8670 print_generic_expr (dump_file
, lhs
, 0);
8671 fprintf (dump_file
, ": ");
8672 dump_value_range (dump_file
, &vr_result
);
8673 fprintf (dump_file
, "\n");
8676 return SSA_PROP_INTERESTING
;
8679 /* Nothing changed, don't add outgoing edges. */
8680 return SSA_PROP_NOT_INTERESTING
;
8682 /* No match found. Set the LHS to VARYING. */
8684 set_value_range_to_varying (lhs_vr
);
8685 return SSA_PROP_VARYING
;
8688 /* Simplify boolean operations if the source is known
8689 to be already a boolean. */
8691 simplify_truth_ops_using_ranges (gimple_stmt_iterator
*gsi
, gimple stmt
)
8693 enum tree_code rhs_code
= gimple_assign_rhs_code (stmt
);
8695 bool need_conversion
;
8697 /* We handle only !=/== case here. */
8698 gcc_assert (rhs_code
== EQ_EXPR
|| rhs_code
== NE_EXPR
);
8700 op0
= gimple_assign_rhs1 (stmt
);
8701 if (!op_with_boolean_value_range_p (op0
))
8704 op1
= gimple_assign_rhs2 (stmt
);
8705 if (!op_with_boolean_value_range_p (op1
))
8708 /* Reduce number of cases to handle to NE_EXPR. As there is no
8709 BIT_XNOR_EXPR we cannot replace A == B with a single statement. */
8710 if (rhs_code
== EQ_EXPR
)
8712 if (TREE_CODE (op1
) == INTEGER_CST
)
8713 op1
= int_const_binop (BIT_XOR_EXPR
, op1
,
8714 build_int_cst (TREE_TYPE (op1
), 1));
8719 lhs
= gimple_assign_lhs (stmt
);
8721 = !useless_type_conversion_p (TREE_TYPE (lhs
), TREE_TYPE (op0
));
8723 /* Make sure to not sign-extend a 1-bit 1 when converting the result. */
8725 && !TYPE_UNSIGNED (TREE_TYPE (op0
))
8726 && TYPE_PRECISION (TREE_TYPE (op0
)) == 1
8727 && TYPE_PRECISION (TREE_TYPE (lhs
)) > 1)
8730 /* For A != 0 we can substitute A itself. */
8731 if (integer_zerop (op1
))
8732 gimple_assign_set_rhs_with_ops (gsi
,
8734 ? NOP_EXPR
: TREE_CODE (op0
),
8736 /* For A != B we substitute A ^ B. Either with conversion. */
8737 else if (need_conversion
)
8739 tree tem
= make_ssa_name (TREE_TYPE (op0
), NULL
);
8741 gimple_build_assign_with_ops (BIT_XOR_EXPR
, tem
, op0
, op1
);
8742 gsi_insert_before (gsi
, newop
, GSI_SAME_STMT
);
8743 gimple_assign_set_rhs_with_ops (gsi
, NOP_EXPR
, tem
, NULL_TREE
);
8747 gimple_assign_set_rhs_with_ops (gsi
, BIT_XOR_EXPR
, op0
, op1
);
8748 update_stmt (gsi_stmt (*gsi
));
8753 /* Simplify a division or modulo operator to a right shift or
8754 bitwise and if the first operand is unsigned or is greater
8755 than zero and the second operand is an exact power of two. */
8758 simplify_div_or_mod_using_ranges (gimple stmt
)
8760 enum tree_code rhs_code
= gimple_assign_rhs_code (stmt
);
8762 tree op0
= gimple_assign_rhs1 (stmt
);
8763 tree op1
= gimple_assign_rhs2 (stmt
);
8764 value_range_t
*vr
= get_value_range (gimple_assign_rhs1 (stmt
));
8766 if (TYPE_UNSIGNED (TREE_TYPE (op0
)))
8768 val
= integer_one_node
;
8774 val
= compare_range_with_value (GE_EXPR
, vr
, integer_zero_node
, &sop
);
8778 && integer_onep (val
)
8779 && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC
))
8781 location_t location
;
8783 if (!gimple_has_location (stmt
))
8784 location
= input_location
;
8786 location
= gimple_location (stmt
);
8787 warning_at (location
, OPT_Wstrict_overflow
,
8788 "assuming signed overflow does not occur when "
8789 "simplifying %</%> or %<%%%> to %<>>%> or %<&%>");
8793 if (val
&& integer_onep (val
))
8797 if (rhs_code
== TRUNC_DIV_EXPR
)
8799 t
= build_int_cst (integer_type_node
, tree_log2 (op1
));
8800 gimple_assign_set_rhs_code (stmt
, RSHIFT_EXPR
);
8801 gimple_assign_set_rhs1 (stmt
, op0
);
8802 gimple_assign_set_rhs2 (stmt
, t
);
8806 t
= build_int_cst (TREE_TYPE (op1
), 1);
8807 t
= int_const_binop (MINUS_EXPR
, op1
, t
);
8808 t
= fold_convert (TREE_TYPE (op0
), t
);
8810 gimple_assign_set_rhs_code (stmt
, BIT_AND_EXPR
);
8811 gimple_assign_set_rhs1 (stmt
, op0
);
8812 gimple_assign_set_rhs2 (stmt
, t
);
8822 /* If the operand to an ABS_EXPR is >= 0, then eliminate the
8823 ABS_EXPR. If the operand is <= 0, then simplify the
8824 ABS_EXPR into a NEGATE_EXPR. */
8827 simplify_abs_using_ranges (gimple stmt
)
8830 tree op
= gimple_assign_rhs1 (stmt
);
8831 tree type
= TREE_TYPE (op
);
8832 value_range_t
*vr
= get_value_range (op
);
8834 if (TYPE_UNSIGNED (type
))
8836 val
= integer_zero_node
;
8842 val
= compare_range_with_value (LE_EXPR
, vr
, integer_zero_node
, &sop
);
8846 val
= compare_range_with_value (GE_EXPR
, vr
, integer_zero_node
,
8851 if (integer_zerop (val
))
8852 val
= integer_one_node
;
8853 else if (integer_onep (val
))
8854 val
= integer_zero_node
;
8859 && (integer_onep (val
) || integer_zerop (val
)))
8861 if (sop
&& issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC
))
8863 location_t location
;
8865 if (!gimple_has_location (stmt
))
8866 location
= input_location
;
8868 location
= gimple_location (stmt
);
8869 warning_at (location
, OPT_Wstrict_overflow
,
8870 "assuming signed overflow does not occur when "
8871 "simplifying %<abs (X)%> to %<X%> or %<-X%>");
8874 gimple_assign_set_rhs1 (stmt
, op
);
8875 if (integer_onep (val
))
8876 gimple_assign_set_rhs_code (stmt
, NEGATE_EXPR
);
8878 gimple_assign_set_rhs_code (stmt
, SSA_NAME
);
8887 /* Optimize away redundant BIT_AND_EXPR and BIT_IOR_EXPR.
8888 If all the bits that are being cleared by & are already
8889 known to be zero from VR, or all the bits that are being
8890 set by | are already known to be one from VR, the bit
8891 operation is redundant. */
8894 simplify_bit_ops_using_ranges (gimple_stmt_iterator
*gsi
, gimple stmt
)
8896 tree op0
= gimple_assign_rhs1 (stmt
);
8897 tree op1
= gimple_assign_rhs2 (stmt
);
8898 tree op
= NULL_TREE
;
8899 value_range_t vr0
= VR_INITIALIZER
;
8900 value_range_t vr1
= VR_INITIALIZER
;
8901 wide_int may_be_nonzero0
, may_be_nonzero1
;
8902 wide_int must_be_nonzero0
, must_be_nonzero1
;
8905 if (TREE_CODE (op0
) == SSA_NAME
)
8906 vr0
= *(get_value_range (op0
));
8907 else if (is_gimple_min_invariant (op0
))
8908 set_value_range_to_value (&vr0
, op0
, NULL
);
8912 if (TREE_CODE (op1
) == SSA_NAME
)
8913 vr1
= *(get_value_range (op1
));
8914 else if (is_gimple_min_invariant (op1
))
8915 set_value_range_to_value (&vr1
, op1
, NULL
);
8919 if (!zero_nonzero_bits_from_vr (TREE_TYPE (op0
), &vr0
, &may_be_nonzero0
,
8922 if (!zero_nonzero_bits_from_vr (TREE_TYPE (op1
), &vr1
, &may_be_nonzero1
,
8926 switch (gimple_assign_rhs_code (stmt
))
8929 mask
= may_be_nonzero0
.and_not (must_be_nonzero1
);
8935 mask
= may_be_nonzero1
.and_not (must_be_nonzero0
);
8943 mask
= may_be_nonzero0
.and_not (must_be_nonzero1
);
8949 mask
= may_be_nonzero1
.and_not (must_be_nonzero0
);
8960 if (op
== NULL_TREE
)
8963 gimple_assign_set_rhs_with_ops (gsi
, TREE_CODE (op
), op
, NULL
);
8964 update_stmt (gsi_stmt (*gsi
));
8968 /* We are comparing trees OP0 and OP1 using COND_CODE. OP0 has
8969 a known value range VR.
8971 If there is one and only one value which will satisfy the
8972 conditional, then return that value. Else return NULL. */
8975 test_for_singularity (enum tree_code cond_code
, tree op0
,
8976 tree op1
, value_range_t
*vr
)
8981 /* Extract minimum/maximum values which satisfy the
8982 the conditional as it was written. */
8983 if (cond_code
== LE_EXPR
|| cond_code
== LT_EXPR
)
8985 /* This should not be negative infinity; there is no overflow
8987 min
= TYPE_MIN_VALUE (TREE_TYPE (op0
));
8990 if (cond_code
== LT_EXPR
&& !is_overflow_infinity (max
))
8992 tree one
= build_int_cst (TREE_TYPE (op0
), 1);
8993 max
= fold_build2 (MINUS_EXPR
, TREE_TYPE (op0
), max
, one
);
8995 TREE_NO_WARNING (max
) = 1;
8998 else if (cond_code
== GE_EXPR
|| cond_code
== GT_EXPR
)
9000 /* This should not be positive infinity; there is no overflow
9002 max
= TYPE_MAX_VALUE (TREE_TYPE (op0
));
9005 if (cond_code
== GT_EXPR
&& !is_overflow_infinity (min
))
9007 tree one
= build_int_cst (TREE_TYPE (op0
), 1);
9008 min
= fold_build2 (PLUS_EXPR
, TREE_TYPE (op0
), min
, one
);
9010 TREE_NO_WARNING (min
) = 1;
9014 /* Now refine the minimum and maximum values using any
9015 value range information we have for op0. */
9018 if (compare_values (vr
->min
, min
) == 1)
9020 if (compare_values (vr
->max
, max
) == -1)
9023 /* If the new min/max values have converged to a single value,
9024 then there is only one value which can satisfy the condition,
9025 return that value. */
9026 if (operand_equal_p (min
, max
, 0) && is_gimple_min_invariant (min
))
9032 /* Return whether the value range *VR fits in an integer type specified
9033 by PRECISION and UNSIGNED_P. */
9036 range_fits_type_p (value_range_t
*vr
, unsigned dest_precision
, signop dest_sgn
)
9039 unsigned src_precision
;
9043 /* We can only handle integral and pointer types. */
9044 src_type
= TREE_TYPE (vr
->min
);
9045 if (!INTEGRAL_TYPE_P (src_type
)
9046 && !POINTER_TYPE_P (src_type
))
9049 /* An extension is fine unless VR is SIGNED and dest_sgn is UNSIGNED,
9050 and so is an identity transform. */
9051 src_precision
= TYPE_PRECISION (TREE_TYPE (vr
->min
));
9052 src_sgn
= TYPE_SIGN (src_type
);
9053 if ((src_precision
< dest_precision
9054 && !(dest_sgn
== UNSIGNED
&& src_sgn
== SIGNED
))
9055 || (src_precision
== dest_precision
&& src_sgn
== dest_sgn
))
9058 /* Now we can only handle ranges with constant bounds. */
9059 if (vr
->type
!= VR_RANGE
9060 || TREE_CODE (vr
->min
) != INTEGER_CST
9061 || TREE_CODE (vr
->max
) != INTEGER_CST
)
9064 /* For sign changes, the MSB of the wide_int has to be clear.
9065 An unsigned value with its MSB set cannot be represented by
9066 a signed wide_int, while a negative value cannot be represented
9067 by an unsigned wide_int. */
9068 if (src_sgn
!= dest_sgn
9069 && (wi::lts_p (vr
->min
, 0) || wi::lts_p (vr
->max
, 0)))
9072 /* Then we can perform the conversion on both ends and compare
9073 the result for equality. */
9074 tem
= wi::ext (wi::to_widest (vr
->min
), dest_precision
, dest_sgn
);
9075 if (tem
!= wi::to_widest (vr
->min
))
9077 tem
= wi::ext (wi::to_widest (vr
->max
), dest_precision
, dest_sgn
);
9078 if (tem
!= wi::to_widest (vr
->max
))
9084 /* Simplify a conditional using a relational operator to an equality
9085 test if the range information indicates only one value can satisfy
9086 the original conditional. */
9089 simplify_cond_using_ranges (gcond
*stmt
)
9091 tree op0
= gimple_cond_lhs (stmt
);
9092 tree op1
= gimple_cond_rhs (stmt
);
9093 enum tree_code cond_code
= gimple_cond_code (stmt
);
9095 if (cond_code
!= NE_EXPR
9096 && cond_code
!= EQ_EXPR
9097 && TREE_CODE (op0
) == SSA_NAME
9098 && INTEGRAL_TYPE_P (TREE_TYPE (op0
))
9099 && is_gimple_min_invariant (op1
))
9101 value_range_t
*vr
= get_value_range (op0
);
9103 /* If we have range information for OP0, then we might be
9104 able to simplify this conditional. */
9105 if (vr
->type
== VR_RANGE
)
9107 tree new_tree
= test_for_singularity (cond_code
, op0
, op1
, vr
);
9113 fprintf (dump_file
, "Simplified relational ");
9114 print_gimple_stmt (dump_file
, stmt
, 0, 0);
9115 fprintf (dump_file
, " into ");
9118 gimple_cond_set_code (stmt
, EQ_EXPR
);
9119 gimple_cond_set_lhs (stmt
, op0
);
9120 gimple_cond_set_rhs (stmt
, new_tree
);
9126 print_gimple_stmt (dump_file
, stmt
, 0, 0);
9127 fprintf (dump_file
, "\n");
9133 /* Try again after inverting the condition. We only deal
9134 with integral types here, so no need to worry about
9135 issues with inverting FP comparisons. */
9136 cond_code
= invert_tree_comparison (cond_code
, false);
9137 new_tree
= test_for_singularity (cond_code
, op0
, op1
, vr
);
9143 fprintf (dump_file
, "Simplified relational ");
9144 print_gimple_stmt (dump_file
, stmt
, 0, 0);
9145 fprintf (dump_file
, " into ");
9148 gimple_cond_set_code (stmt
, NE_EXPR
);
9149 gimple_cond_set_lhs (stmt
, op0
);
9150 gimple_cond_set_rhs (stmt
, new_tree
);
9156 print_gimple_stmt (dump_file
, stmt
, 0, 0);
9157 fprintf (dump_file
, "\n");
9165 /* If we have a comparison of an SSA_NAME (OP0) against a constant,
9166 see if OP0 was set by a type conversion where the source of
9167 the conversion is another SSA_NAME with a range that fits
9168 into the range of OP0's type.
9170 If so, the conversion is redundant as the earlier SSA_NAME can be
9171 used for the comparison directly if we just massage the constant in the
9173 if (TREE_CODE (op0
) == SSA_NAME
9174 && TREE_CODE (op1
) == INTEGER_CST
)
9176 gimple def_stmt
= SSA_NAME_DEF_STMT (op0
);
9179 if (!is_gimple_assign (def_stmt
)
9180 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt
)))
9183 innerop
= gimple_assign_rhs1 (def_stmt
);
9185 if (TREE_CODE (innerop
) == SSA_NAME
9186 && !POINTER_TYPE_P (TREE_TYPE (innerop
)))
9188 value_range_t
*vr
= get_value_range (innerop
);
9190 if (range_int_cst_p (vr
)
9191 && range_fits_type_p (vr
,
9192 TYPE_PRECISION (TREE_TYPE (op0
)),
9193 TYPE_SIGN (TREE_TYPE (op0
)))
9194 && int_fits_type_p (op1
, TREE_TYPE (innerop
))
9195 /* The range must not have overflowed, or if it did overflow
9196 we must not be wrapping/trapping overflow and optimizing
9197 with strict overflow semantics. */
9198 && ((!is_negative_overflow_infinity (vr
->min
)
9199 && !is_positive_overflow_infinity (vr
->max
))
9200 || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (innerop
))))
9202 /* If the range overflowed and the user has asked for warnings
9203 when strict overflow semantics were used to optimize code,
9204 issue an appropriate warning. */
9205 if (cond_code
!= EQ_EXPR
&& cond_code
!= NE_EXPR
9206 && (is_negative_overflow_infinity (vr
->min
)
9207 || is_positive_overflow_infinity (vr
->max
))
9208 && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_CONDITIONAL
))
9210 location_t location
;
9212 if (!gimple_has_location (stmt
))
9213 location
= input_location
;
9215 location
= gimple_location (stmt
);
9216 warning_at (location
, OPT_Wstrict_overflow
,
9217 "assuming signed overflow does not occur when "
9218 "simplifying conditional");
9221 tree newconst
= fold_convert (TREE_TYPE (innerop
), op1
);
9222 gimple_cond_set_lhs (stmt
, innerop
);
9223 gimple_cond_set_rhs (stmt
, newconst
);
9232 /* Simplify a switch statement using the value range of the switch
9236 simplify_switch_using_ranges (gswitch
*stmt
)
9238 tree op
= gimple_switch_index (stmt
);
9243 size_t i
= 0, j
= 0, n
, n2
;
9246 size_t k
= 1, l
= 0;
9248 if (TREE_CODE (op
) == SSA_NAME
)
9250 vr
= get_value_range (op
);
9252 /* We can only handle integer ranges. */
9253 if ((vr
->type
!= VR_RANGE
9254 && vr
->type
!= VR_ANTI_RANGE
)
9255 || symbolic_range_p (vr
))
9258 /* Find case label for min/max of the value range. */
9259 take_default
= !find_case_label_ranges (stmt
, vr
, &i
, &j
, &k
, &l
);
9261 else if (TREE_CODE (op
) == INTEGER_CST
)
9263 take_default
= !find_case_label_index (stmt
, 1, op
, &i
);
9277 n
= gimple_switch_num_labels (stmt
);
9279 /* Bail out if this is just all edges taken. */
9285 /* Build a new vector of taken case labels. */
9286 vec2
= make_tree_vec (j
- i
+ 1 + l
- k
+ 1 + (int)take_default
);
9289 /* Add the default edge, if necessary. */
9291 TREE_VEC_ELT (vec2
, n2
++) = gimple_switch_default_label (stmt
);
9293 for (; i
<= j
; ++i
, ++n2
)
9294 TREE_VEC_ELT (vec2
, n2
) = gimple_switch_label (stmt
, i
);
9296 for (; k
<= l
; ++k
, ++n2
)
9297 TREE_VEC_ELT (vec2
, n2
) = gimple_switch_label (stmt
, k
);
9299 /* Mark needed edges. */
9300 for (i
= 0; i
< n2
; ++i
)
9302 e
= find_edge (gimple_bb (stmt
),
9303 label_to_block (CASE_LABEL (TREE_VEC_ELT (vec2
, i
))));
9304 e
->aux
= (void *)-1;
9307 /* Queue not needed edges for later removal. */
9308 FOR_EACH_EDGE (e
, ei
, gimple_bb (stmt
)->succs
)
9310 if (e
->aux
== (void *)-1)
9316 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
9318 fprintf (dump_file
, "removing unreachable case label\n");
9320 to_remove_edges
.safe_push (e
);
9321 e
->flags
&= ~EDGE_EXECUTABLE
;
9324 /* And queue an update for the stmt. */
9327 to_update_switch_stmts
.safe_push (su
);
9331 /* Simplify an integral conversion from an SSA name in STMT. */
9334 simplify_conversion_using_ranges (gimple stmt
)
9336 tree innerop
, middleop
, finaltype
;
9338 value_range_t
*innervr
;
9339 signop inner_sgn
, middle_sgn
, final_sgn
;
9340 unsigned inner_prec
, middle_prec
, final_prec
;
9341 widest_int innermin
, innermed
, innermax
, middlemin
, middlemed
, middlemax
;
9343 finaltype
= TREE_TYPE (gimple_assign_lhs (stmt
));
9344 if (!INTEGRAL_TYPE_P (finaltype
))
9346 middleop
= gimple_assign_rhs1 (stmt
);
9347 def_stmt
= SSA_NAME_DEF_STMT (middleop
);
9348 if (!is_gimple_assign (def_stmt
)
9349 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt
)))
9351 innerop
= gimple_assign_rhs1 (def_stmt
);
9352 if (TREE_CODE (innerop
) != SSA_NAME
9353 || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (innerop
))
9356 /* Get the value-range of the inner operand. */
9357 innervr
= get_value_range (innerop
);
9358 if (innervr
->type
!= VR_RANGE
9359 || TREE_CODE (innervr
->min
) != INTEGER_CST
9360 || TREE_CODE (innervr
->max
) != INTEGER_CST
)
9363 /* Simulate the conversion chain to check if the result is equal if
9364 the middle conversion is removed. */
9365 innermin
= wi::to_widest (innervr
->min
);
9366 innermax
= wi::to_widest (innervr
->max
);
9368 inner_prec
= TYPE_PRECISION (TREE_TYPE (innerop
));
9369 middle_prec
= TYPE_PRECISION (TREE_TYPE (middleop
));
9370 final_prec
= TYPE_PRECISION (finaltype
);
9372 /* If the first conversion is not injective, the second must not
9374 if (wi::gtu_p (innermax
- innermin
,
9375 wi::mask
<widest_int
> (middle_prec
, false))
9376 && middle_prec
< final_prec
)
9378 /* We also want a medium value so that we can track the effect that
9379 narrowing conversions with sign change have. */
9380 inner_sgn
= TYPE_SIGN (TREE_TYPE (innerop
));
9381 if (inner_sgn
== UNSIGNED
)
9382 innermed
= wi::shifted_mask
<widest_int
> (1, inner_prec
- 1, false);
9385 if (wi::cmp (innermin
, innermed
, inner_sgn
) >= 0
9386 || wi::cmp (innermed
, innermax
, inner_sgn
) >= 0)
9387 innermed
= innermin
;
9389 middle_sgn
= TYPE_SIGN (TREE_TYPE (middleop
));
9390 middlemin
= wi::ext (innermin
, middle_prec
, middle_sgn
);
9391 middlemed
= wi::ext (innermed
, middle_prec
, middle_sgn
);
9392 middlemax
= wi::ext (innermax
, middle_prec
, middle_sgn
);
9394 /* Require that the final conversion applied to both the original
9395 and the intermediate range produces the same result. */
9396 final_sgn
= TYPE_SIGN (finaltype
);
9397 if (wi::ext (middlemin
, final_prec
, final_sgn
)
9398 != wi::ext (innermin
, final_prec
, final_sgn
)
9399 || wi::ext (middlemed
, final_prec
, final_sgn
)
9400 != wi::ext (innermed
, final_prec
, final_sgn
)
9401 || wi::ext (middlemax
, final_prec
, final_sgn
)
9402 != wi::ext (innermax
, final_prec
, final_sgn
))
9405 gimple_assign_set_rhs1 (stmt
, innerop
);
9410 /* Simplify a conversion from integral SSA name to float in STMT. */
9413 simplify_float_conversion_using_ranges (gimple_stmt_iterator
*gsi
, gimple stmt
)
9415 tree rhs1
= gimple_assign_rhs1 (stmt
);
9416 value_range_t
*vr
= get_value_range (rhs1
);
9417 enum machine_mode fltmode
= TYPE_MODE (TREE_TYPE (gimple_assign_lhs (stmt
)));
9418 enum machine_mode mode
;
9422 /* We can only handle constant ranges. */
9423 if (vr
->type
!= VR_RANGE
9424 || TREE_CODE (vr
->min
) != INTEGER_CST
9425 || TREE_CODE (vr
->max
) != INTEGER_CST
)
9428 /* First check if we can use a signed type in place of an unsigned. */
9429 if (TYPE_UNSIGNED (TREE_TYPE (rhs1
))
9430 && (can_float_p (fltmode
, TYPE_MODE (TREE_TYPE (rhs1
)), 0)
9431 != CODE_FOR_nothing
)
9432 && range_fits_type_p (vr
, TYPE_PRECISION (TREE_TYPE (rhs1
)), SIGNED
))
9433 mode
= TYPE_MODE (TREE_TYPE (rhs1
));
9434 /* If we can do the conversion in the current input mode do nothing. */
9435 else if (can_float_p (fltmode
, TYPE_MODE (TREE_TYPE (rhs1
)),
9436 TYPE_UNSIGNED (TREE_TYPE (rhs1
))) != CODE_FOR_nothing
)
9438 /* Otherwise search for a mode we can use, starting from the narrowest
9439 integer mode available. */
9442 mode
= GET_CLASS_NARROWEST_MODE (MODE_INT
);
9445 /* If we cannot do a signed conversion to float from mode
9446 or if the value-range does not fit in the signed type
9447 try with a wider mode. */
9448 if (can_float_p (fltmode
, mode
, 0) != CODE_FOR_nothing
9449 && range_fits_type_p (vr
, GET_MODE_PRECISION (mode
), SIGNED
))
9452 mode
= GET_MODE_WIDER_MODE (mode
);
9453 /* But do not widen the input. Instead leave that to the
9454 optabs expansion code. */
9455 if (GET_MODE_PRECISION (mode
) > TYPE_PRECISION (TREE_TYPE (rhs1
)))
9458 while (mode
!= VOIDmode
);
9459 if (mode
== VOIDmode
)
9463 /* It works, insert a truncation or sign-change before the
9464 float conversion. */
9465 tem
= make_ssa_name (build_nonstandard_integer_type
9466 (GET_MODE_PRECISION (mode
), 0), NULL
);
9467 conv
= gimple_build_assign_with_ops (NOP_EXPR
, tem
, rhs1
, NULL_TREE
);
9468 gsi_insert_before (gsi
, conv
, GSI_SAME_STMT
);
9469 gimple_assign_set_rhs1 (stmt
, tem
);
9475 /* Simplify an internal fn call using ranges if possible. */
9478 simplify_internal_call_using_ranges (gimple_stmt_iterator
*gsi
, gimple stmt
)
9480 enum tree_code subcode
;
9481 switch (gimple_call_internal_fn (stmt
))
9483 case IFN_UBSAN_CHECK_ADD
:
9484 subcode
= PLUS_EXPR
;
9486 case IFN_UBSAN_CHECK_SUB
:
9487 subcode
= MINUS_EXPR
;
9489 case IFN_UBSAN_CHECK_MUL
:
9490 subcode
= MULT_EXPR
;
9496 value_range_t vr0
= VR_INITIALIZER
;
9497 value_range_t vr1
= VR_INITIALIZER
;
9498 tree op0
= gimple_call_arg (stmt
, 0);
9499 tree op1
= gimple_call_arg (stmt
, 1);
9501 if (TREE_CODE (op0
) == SSA_NAME
)
9502 vr0
= *get_value_range (op0
);
9503 else if (TREE_CODE (op0
) == INTEGER_CST
)
9504 set_value_range_to_value (&vr0
, op0
, NULL
);
9506 set_value_range_to_varying (&vr0
);
9508 if (TREE_CODE (op1
) == SSA_NAME
)
9509 vr1
= *get_value_range (op1
);
9510 else if (TREE_CODE (op1
) == INTEGER_CST
)
9511 set_value_range_to_value (&vr1
, op1
, NULL
);
9513 set_value_range_to_varying (&vr1
);
9515 if (!range_int_cst_p (&vr0
))
9517 /* If one range is VR_ANTI_RANGE, VR_VARYING etc.,
9518 optimize at least x = y + 0; x = y - 0; x = y * 0;
9519 and x = y * 1; which never overflow. */
9520 if (!range_int_cst_p (&vr1
))
9522 if (tree_int_cst_sgn (vr1
.min
) == -1)
9524 if (compare_tree_int (vr1
.max
, subcode
== MULT_EXPR
) == 1)
9527 else if (!range_int_cst_p (&vr1
))
9529 /* If one range is VR_ANTI_RANGE, VR_VARYING etc.,
9530 optimize at least x = 0 + y; x = 0 * y; and x = 1 * y;
9531 which never overflow. */
9532 if (subcode
== MINUS_EXPR
)
9534 if (!range_int_cst_p (&vr0
))
9536 if (tree_int_cst_sgn (vr0
.min
) == -1)
9538 if (compare_tree_int (vr0
.max
, subcode
== MULT_EXPR
) == 1)
9543 tree r1
= int_const_binop (subcode
, vr0
.min
, vr1
.min
);
9544 tree r2
= int_const_binop (subcode
, vr0
.max
, vr1
.max
);
9545 if (r1
== NULL_TREE
|| TREE_OVERFLOW (r1
)
9546 || r2
== NULL_TREE
|| TREE_OVERFLOW (r2
))
9548 if (subcode
== MULT_EXPR
)
9550 tree r3
= int_const_binop (subcode
, vr0
.min
, vr1
.max
);
9551 tree r4
= int_const_binop (subcode
, vr0
.max
, vr1
.min
);
9552 if (r3
== NULL_TREE
|| TREE_OVERFLOW (r3
)
9553 || r4
== NULL_TREE
|| TREE_OVERFLOW (r4
))
9558 gimple g
= gimple_build_assign_with_ops (subcode
, gimple_call_lhs (stmt
),
9560 gsi_replace (gsi
, g
, false);
9564 /* Simplify STMT using ranges if possible. */
9567 simplify_stmt_using_ranges (gimple_stmt_iterator
*gsi
)
9569 gimple stmt
= gsi_stmt (*gsi
);
9570 if (is_gimple_assign (stmt
))
9572 enum tree_code rhs_code
= gimple_assign_rhs_code (stmt
);
9573 tree rhs1
= gimple_assign_rhs1 (stmt
);
9579 /* Transform EQ_EXPR, NE_EXPR into BIT_XOR_EXPR or identity
9580 if the RHS is zero or one, and the LHS are known to be boolean
9582 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
9583 return simplify_truth_ops_using_ranges (gsi
, stmt
);
9586 /* Transform TRUNC_DIV_EXPR and TRUNC_MOD_EXPR into RSHIFT_EXPR
9587 and BIT_AND_EXPR respectively if the first operand is greater
9588 than zero and the second operand is an exact power of two. */
9589 case TRUNC_DIV_EXPR
:
9590 case TRUNC_MOD_EXPR
:
9591 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1
))
9592 && integer_pow2p (gimple_assign_rhs2 (stmt
)))
9593 return simplify_div_or_mod_using_ranges (stmt
);
9596 /* Transform ABS (X) into X or -X as appropriate. */
9598 if (TREE_CODE (rhs1
) == SSA_NAME
9599 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
9600 return simplify_abs_using_ranges (stmt
);
9605 /* Optimize away BIT_AND_EXPR and BIT_IOR_EXPR
9606 if all the bits being cleared are already cleared or
9607 all the bits being set are already set. */
9608 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
9609 return simplify_bit_ops_using_ranges (gsi
, stmt
);
9613 if (TREE_CODE (rhs1
) == SSA_NAME
9614 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
9615 return simplify_conversion_using_ranges (stmt
);
9619 if (TREE_CODE (rhs1
) == SSA_NAME
9620 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
9621 return simplify_float_conversion_using_ranges (gsi
, stmt
);
9628 else if (gimple_code (stmt
) == GIMPLE_COND
)
9629 return simplify_cond_using_ranges (as_a
<gcond
*> (stmt
));
9630 else if (gimple_code (stmt
) == GIMPLE_SWITCH
)
9631 return simplify_switch_using_ranges (as_a
<gswitch
*> (stmt
));
9632 else if (is_gimple_call (stmt
)
9633 && gimple_call_internal_p (stmt
))
9634 return simplify_internal_call_using_ranges (gsi
, stmt
);
9639 /* If the statement pointed by SI has a predicate whose value can be
9640 computed using the value range information computed by VRP, compute
9641 its value and return true. Otherwise, return false. */
9644 fold_predicate_in (gimple_stmt_iterator
*si
)
9646 bool assignment_p
= false;
9648 gimple stmt
= gsi_stmt (*si
);
9650 if (is_gimple_assign (stmt
)
9651 && TREE_CODE_CLASS (gimple_assign_rhs_code (stmt
)) == tcc_comparison
)
9653 assignment_p
= true;
9654 val
= vrp_evaluate_conditional (gimple_assign_rhs_code (stmt
),
9655 gimple_assign_rhs1 (stmt
),
9656 gimple_assign_rhs2 (stmt
),
9659 else if (gcond
*cond_stmt
= dyn_cast
<gcond
*> (stmt
))
9660 val
= vrp_evaluate_conditional (gimple_cond_code (cond_stmt
),
9661 gimple_cond_lhs (cond_stmt
),
9662 gimple_cond_rhs (cond_stmt
),
9670 val
= fold_convert (gimple_expr_type (stmt
), val
);
9674 fprintf (dump_file
, "Folding predicate ");
9675 print_gimple_expr (dump_file
, stmt
, 0, 0);
9676 fprintf (dump_file
, " to ");
9677 print_generic_expr (dump_file
, val
, 0);
9678 fprintf (dump_file
, "\n");
9681 if (is_gimple_assign (stmt
))
9682 gimple_assign_set_rhs_from_tree (si
, val
);
9685 gcc_assert (gimple_code (stmt
) == GIMPLE_COND
);
9686 gcond
*cond_stmt
= as_a
<gcond
*> (stmt
);
9687 if (integer_zerop (val
))
9688 gimple_cond_make_false (cond_stmt
);
9689 else if (integer_onep (val
))
9690 gimple_cond_make_true (cond_stmt
);
9701 /* Callback for substitute_and_fold folding the stmt at *SI. */
9704 vrp_fold_stmt (gimple_stmt_iterator
*si
)
9706 if (fold_predicate_in (si
))
9709 return simplify_stmt_using_ranges (si
);
9712 /* Stack of dest,src equivalency pairs that need to be restored after
9713 each attempt to thread a block's incoming edge to an outgoing edge.
9715 A NULL entry is used to mark the end of pairs which need to be
9717 static vec
<tree
> equiv_stack
;
9719 /* A trivial wrapper so that we can present the generic jump threading
9720 code with a simple API for simplifying statements. STMT is the
9721 statement we want to simplify, WITHIN_STMT provides the location
9722 for any overflow warnings. */
9725 simplify_stmt_for_jump_threading (gimple stmt
, gimple within_stmt
)
9727 if (gcond
*cond_stmt
= dyn_cast
<gcond
*> (stmt
))
9728 return vrp_evaluate_conditional (gimple_cond_code (cond_stmt
),
9729 gimple_cond_lhs (cond_stmt
),
9730 gimple_cond_rhs (cond_stmt
),
9733 if (gassign
*assign_stmt
= dyn_cast
<gassign
*> (stmt
))
9735 value_range_t new_vr
= VR_INITIALIZER
;
9736 tree lhs
= gimple_assign_lhs (assign_stmt
);
9738 if (TREE_CODE (lhs
) == SSA_NAME
9739 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs
))
9740 || POINTER_TYPE_P (TREE_TYPE (lhs
))))
9742 extract_range_from_assignment (&new_vr
, assign_stmt
);
9743 if (range_int_cst_singleton_p (&new_vr
))
9751 /* Blocks which have more than one predecessor and more than
9752 one successor present jump threading opportunities, i.e.,
9753 when the block is reached from a specific predecessor, we
9754 may be able to determine which of the outgoing edges will
9755 be traversed. When this optimization applies, we are able
9756 to avoid conditionals at runtime and we may expose secondary
9757 optimization opportunities.
9759 This routine is effectively a driver for the generic jump
9760 threading code. It basically just presents the generic code
9761 with edges that may be suitable for jump threading.
9763 Unlike DOM, we do not iterate VRP if jump threading was successful.
9764 While iterating may expose new opportunities for VRP, it is expected
9765 those opportunities would be very limited and the compile time cost
9766 to expose those opportunities would be significant.
9768 As jump threading opportunities are discovered, they are registered
9769 for later realization. */
9772 identify_jump_threads (void)
9779 /* Ugh. When substituting values earlier in this pass we can
9780 wipe the dominance information. So rebuild the dominator
9781 information as we need it within the jump threading code. */
9782 calculate_dominance_info (CDI_DOMINATORS
);
9784 /* We do not allow VRP information to be used for jump threading
9785 across a back edge in the CFG. Otherwise it becomes too
9786 difficult to avoid eliminating loop exit tests. Of course
9787 EDGE_DFS_BACK is not accurate at this time so we have to
9789 mark_dfs_back_edges ();
9791 /* Do not thread across edges we are about to remove. Just marking
9792 them as EDGE_DFS_BACK will do. */
9793 FOR_EACH_VEC_ELT (to_remove_edges
, i
, e
)
9794 e
->flags
|= EDGE_DFS_BACK
;
9796 /* Allocate our unwinder stack to unwind any temporary equivalences
9797 that might be recorded. */
9798 equiv_stack
.create (20);
9800 /* To avoid lots of silly node creation, we create a single
9801 conditional and just modify it in-place when attempting to
9803 dummy
= gimple_build_cond (EQ_EXPR
,
9804 integer_zero_node
, integer_zero_node
,
9807 /* Walk through all the blocks finding those which present a
9808 potential jump threading opportunity. We could set this up
9809 as a dominator walker and record data during the walk, but
9810 I doubt it's worth the effort for the classes of jump
9811 threading opportunities we are trying to identify at this
9812 point in compilation. */
9813 FOR_EACH_BB_FN (bb
, cfun
)
9817 /* If the generic jump threading code does not find this block
9818 interesting, then there is nothing to do. */
9819 if (! potentially_threadable_block (bb
))
9822 /* We only care about blocks ending in a COND_EXPR. While there
9823 may be some value in handling SWITCH_EXPR here, I doubt it's
9824 terribly important. */
9825 last
= gsi_stmt (gsi_last_bb (bb
));
9827 /* We're basically looking for a switch or any kind of conditional with
9828 integral or pointer type arguments. Note the type of the second
9829 argument will be the same as the first argument, so no need to
9830 check it explicitly. */
9831 if (gimple_code (last
) == GIMPLE_SWITCH
9832 || (gimple_code (last
) == GIMPLE_COND
9833 && TREE_CODE (gimple_cond_lhs (last
)) == SSA_NAME
9834 && (INTEGRAL_TYPE_P (TREE_TYPE (gimple_cond_lhs (last
)))
9835 || POINTER_TYPE_P (TREE_TYPE (gimple_cond_lhs (last
))))
9836 && (TREE_CODE (gimple_cond_rhs (last
)) == SSA_NAME
9837 || is_gimple_min_invariant (gimple_cond_rhs (last
)))))
9841 /* We've got a block with multiple predecessors and multiple
9842 successors which also ends in a suitable conditional or
9843 switch statement. For each predecessor, see if we can thread
9844 it to a specific successor. */
9845 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
9847 /* Do not thread across back edges or abnormal edges
9849 if (e
->flags
& (EDGE_DFS_BACK
| EDGE_COMPLEX
))
9852 thread_across_edge (dummy
, e
, true, &equiv_stack
,
9853 simplify_stmt_for_jump_threading
);
9858 /* We do not actually update the CFG or SSA graphs at this point as
9859 ASSERT_EXPRs are still in the IL and cfg cleanup code does not yet
9860 handle ASSERT_EXPRs gracefully. */
9863 /* We identified all the jump threading opportunities earlier, but could
9864 not transform the CFG at that time. This routine transforms the
9865 CFG and arranges for the dominator tree to be rebuilt if necessary.
9867 Note the SSA graph update will occur during the normal TODO
9868 processing by the pass manager. */
9870 finalize_jump_threads (void)
9872 thread_through_all_blocks (false);
9873 equiv_stack
.release ();
9877 /* Traverse all the blocks folding conditionals with known ranges. */
9884 values_propagated
= true;
9888 fprintf (dump_file
, "\nValue ranges after VRP:\n\n");
9889 dump_all_value_ranges (dump_file
);
9890 fprintf (dump_file
, "\n");
9893 substitute_and_fold (op_with_constant_singleton_value_range
,
9894 vrp_fold_stmt
, false);
9896 if (warn_array_bounds
)
9897 check_all_array_refs ();
9899 /* We must identify jump threading opportunities before we release
9900 the datastructures built by VRP. */
9901 identify_jump_threads ();
9903 /* Set value range to non pointer SSA_NAMEs. */
9904 for (i
= 0; i
< num_vr_values
; i
++)
9907 tree name
= ssa_name (i
);
9910 || POINTER_TYPE_P (TREE_TYPE (name
))
9911 || (vr_value
[i
]->type
== VR_VARYING
)
9912 || (vr_value
[i
]->type
== VR_UNDEFINED
))
9915 if ((TREE_CODE (vr_value
[i
]->min
) == INTEGER_CST
)
9916 && (TREE_CODE (vr_value
[i
]->max
) == INTEGER_CST
)
9917 && (vr_value
[i
]->type
== VR_RANGE
9918 || vr_value
[i
]->type
== VR_ANTI_RANGE
))
9919 set_range_info (name
, vr_value
[i
]->type
, vr_value
[i
]->min
,
9923 /* Free allocated memory. */
9924 for (i
= 0; i
< num_vr_values
; i
++)
9927 BITMAP_FREE (vr_value
[i
]->equiv
);
9932 free (vr_phi_edge_counts
);
9934 /* So that we can distinguish between VRP data being available
9935 and not available. */
9937 vr_phi_edge_counts
= NULL
;
9941 /* Main entry point to VRP (Value Range Propagation). This pass is
9942 loosely based on J. R. C. Patterson, ``Accurate Static Branch
9943 Prediction by Value Range Propagation,'' in SIGPLAN Conference on
9944 Programming Language Design and Implementation, pp. 67-78, 1995.
9945 Also available at http://citeseer.ist.psu.edu/patterson95accurate.html
9947 This is essentially an SSA-CCP pass modified to deal with ranges
9948 instead of constants.
9950 While propagating ranges, we may find that two or more SSA name
9951 have equivalent, though distinct ranges. For instance,
9954 2 p_4 = ASSERT_EXPR <p_3, p_3 != 0>
9956 4 p_5 = ASSERT_EXPR <p_4, p_4 == q_2>;
9960 In the code above, pointer p_5 has range [q_2, q_2], but from the
9961 code we can also determine that p_5 cannot be NULL and, if q_2 had
9962 a non-varying range, p_5's range should also be compatible with it.
9964 These equivalences are created by two expressions: ASSERT_EXPR and
9965 copy operations. Since p_5 is an assertion on p_4, and p_4 was the
9966 result of another assertion, then we can use the fact that p_5 and
9967 p_4 are equivalent when evaluating p_5's range.
9969 Together with value ranges, we also propagate these equivalences
9970 between names so that we can take advantage of information from
9971 multiple ranges when doing final replacement. Note that this
9972 equivalency relation is transitive but not symmetric.
9974 In the example above, p_5 is equivalent to p_4, q_2 and p_3, but we
9975 cannot assert that q_2 is equivalent to p_5 because q_2 may be used
9976 in contexts where that assertion does not hold (e.g., in line 6).
9978 TODO, the main difference between this pass and Patterson's is that
9979 we do not propagate edge probabilities. We only compute whether
9980 edges can be taken or not. That is, instead of having a spectrum
9981 of jump probabilities between 0 and 1, we only deal with 0, 1 and
9982 DON'T KNOW. In the future, it may be worthwhile to propagate
9983 probabilities to aid branch prediction. */
9992 loop_optimizer_init (LOOPS_NORMAL
| LOOPS_HAVE_RECORDED_EXITS
);
9993 rewrite_into_loop_closed_ssa (NULL
, TODO_update_ssa
);
9996 /* ??? This ends up using stale EDGE_DFS_BACK for liveness computation.
9997 Inserting assertions may split edges which will invalidate
9999 insert_range_assertions ();
10001 to_remove_edges
.create (10);
10002 to_update_switch_stmts
.create (5);
10003 threadedge_initialize_values ();
10005 /* For visiting PHI nodes we need EDGE_DFS_BACK computed. */
10006 mark_dfs_back_edges ();
10009 ssa_propagate (vrp_visit_stmt
, vrp_visit_phi_node
);
10012 free_numbers_of_iterations_estimates ();
10014 /* ASSERT_EXPRs must be removed before finalizing jump threads
10015 as finalizing jump threads calls the CFG cleanup code which
10016 does not properly handle ASSERT_EXPRs. */
10017 remove_range_assertions ();
10019 /* If we exposed any new variables, go ahead and put them into
10020 SSA form now, before we handle jump threading. This simplifies
10021 interactions between rewriting of _DECL nodes into SSA form
10022 and rewriting SSA_NAME nodes into SSA form after block
10023 duplication and CFG manipulation. */
10024 update_ssa (TODO_update_ssa
);
10026 finalize_jump_threads ();
10028 /* Remove dead edges from SWITCH_EXPR optimization. This leaves the
10029 CFG in a broken state and requires a cfg_cleanup run. */
10030 FOR_EACH_VEC_ELT (to_remove_edges
, i
, e
)
10032 /* Update SWITCH_EXPR case label vector. */
10033 FOR_EACH_VEC_ELT (to_update_switch_stmts
, i
, su
)
10036 size_t n
= TREE_VEC_LENGTH (su
->vec
);
10038 gimple_switch_set_num_labels (su
->stmt
, n
);
10039 for (j
= 0; j
< n
; j
++)
10040 gimple_switch_set_label (su
->stmt
, j
, TREE_VEC_ELT (su
->vec
, j
));
10041 /* As we may have replaced the default label with a regular one
10042 make sure to make it a real default label again. This ensures
10043 optimal expansion. */
10044 label
= gimple_switch_label (su
->stmt
, 0);
10045 CASE_LOW (label
) = NULL_TREE
;
10046 CASE_HIGH (label
) = NULL_TREE
;
10049 if (to_remove_edges
.length () > 0)
10051 free_dominance_info (CDI_DOMINATORS
);
10052 loops_state_set (LOOPS_NEED_FIXUP
);
10055 to_remove_edges
.release ();
10056 to_update_switch_stmts
.release ();
10057 threadedge_finalize_values ();
10060 loop_optimizer_finalize ();
10066 const pass_data pass_data_vrp
=
10068 GIMPLE_PASS
, /* type */
10070 OPTGROUP_NONE
, /* optinfo_flags */
10071 TV_TREE_VRP
, /* tv_id */
10072 PROP_ssa
, /* properties_required */
10073 0, /* properties_provided */
10074 0, /* properties_destroyed */
10075 0, /* todo_flags_start */
10076 ( TODO_cleanup_cfg
| TODO_update_ssa
), /* todo_flags_finish */
10079 class pass_vrp
: public gimple_opt_pass
10082 pass_vrp (gcc::context
*ctxt
)
10083 : gimple_opt_pass (pass_data_vrp
, ctxt
)
10086 /* opt_pass methods: */
10087 opt_pass
* clone () { return new pass_vrp (m_ctxt
); }
10088 virtual bool gate (function
*) { return flag_tree_vrp
!= 0; }
10089 virtual unsigned int execute (function
*) { return execute_vrp (); }
10091 }; // class pass_vrp
10093 } // anon namespace
10096 make_pass_vrp (gcc::context
*ctxt
)
10098 return new pass_vrp (ctxt
);