1 /* Support routines for Value Range Propagation (VRP).
2 Copyright (C) 2005-2015 Free Software Foundation, Inc.
3 Contributed by Diego Novillo <dnovillo@redhat.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
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"
30 #include "fold-const.h"
31 #include "stor-layout.h"
34 #include "hard-reg-set.h"
36 #include "dominance.h"
39 #include "basic-block.h"
40 #include "tree-ssa-alias.h"
41 #include "internal-fn.h"
42 #include "gimple-fold.h"
44 #include "gimple-expr.h"
47 #include "gimple-iterator.h"
48 #include "gimple-walk.h"
49 #include "gimple-ssa.h"
51 #include "tree-phinodes.h"
52 #include "ssa-iterators.h"
53 #include "stringpool.h"
54 #include "tree-ssanames.h"
55 #include "tree-ssa-loop-manip.h"
56 #include "tree-ssa-loop-niter.h"
57 #include "tree-ssa-loop.h"
58 #include "tree-into-ssa.h"
60 #include "tree-pass.h"
61 #include "tree-dump.h"
62 #include "gimple-pretty-print.h"
63 #include "diagnostic-core.h"
66 #include "tree-scalar-evolution.h"
67 #include "tree-ssa-propagate.h"
68 #include "tree-chrec.h"
69 #include "tree-ssa-threadupdate.h"
71 #include "insn-config.h"
79 #include "insn-codes.h"
81 #include "tree-ssa-scopedtables.h"
82 #include "tree-ssa-threadedge.h"
86 /* Range of values that can be associated with an SSA_NAME after VRP
90 /* Lattice value represented by this range. */
91 enum value_range_type type
;
93 /* Minimum and maximum values represented by this range. These
94 values should be interpreted as follows:
96 - If TYPE is VR_UNDEFINED or VR_VARYING then MIN and MAX must
99 - If TYPE == VR_RANGE then MIN holds the minimum value and
100 MAX holds the maximum value of the range [MIN, MAX].
102 - If TYPE == ANTI_RANGE the variable is known to NOT
103 take any values in the range [MIN, MAX]. */
107 /* Set of SSA names whose value ranges are equivalent to this one.
108 This set is only valid when TYPE is VR_RANGE or VR_ANTI_RANGE. */
112 typedef struct value_range_d value_range_t
;
114 #define VR_INITIALIZER { VR_UNDEFINED, NULL_TREE, NULL_TREE, NULL }
116 /* Set of SSA names found live during the RPO traversal of the function
117 for still active basic-blocks. */
118 static sbitmap
*live
;
120 /* Return true if the SSA name NAME is live on the edge E. */
123 live_on_edge (edge e
, tree name
)
125 return (live
[e
->dest
->index
]
126 && bitmap_bit_p (live
[e
->dest
->index
], SSA_NAME_VERSION (name
)));
129 /* Local functions. */
130 static int compare_values (tree val1
, tree val2
);
131 static int compare_values_warnv (tree val1
, tree val2
, bool *);
132 static void vrp_meet (value_range_t
*, value_range_t
*);
133 static void vrp_intersect_ranges (value_range_t
*, value_range_t
*);
134 static tree
vrp_evaluate_conditional_warnv_with_ops (enum tree_code
,
135 tree
, tree
, bool, bool *,
138 /* Location information for ASSERT_EXPRs. Each instance of this
139 structure describes an ASSERT_EXPR for an SSA name. Since a single
140 SSA name may have more than one assertion associated with it, these
141 locations are kept in a linked list attached to the corresponding
143 struct assert_locus_d
145 /* Basic block where the assertion would be inserted. */
148 /* Some assertions need to be inserted on an edge (e.g., assertions
149 generated by COND_EXPRs). In those cases, BB will be NULL. */
152 /* Pointer to the statement that generated this assertion. */
153 gimple_stmt_iterator si
;
155 /* Predicate code for the ASSERT_EXPR. Must be COMPARISON_CLASS_P. */
156 enum tree_code comp_code
;
158 /* Value being compared against. */
161 /* Expression to compare. */
164 /* Next node in the linked list. */
165 struct assert_locus_d
*next
;
168 typedef struct assert_locus_d
*assert_locus_t
;
170 /* If bit I is present, it means that SSA name N_i has a list of
171 assertions that should be inserted in the IL. */
172 static bitmap need_assert_for
;
174 /* Array of locations lists where to insert assertions. ASSERTS_FOR[I]
175 holds a list of ASSERT_LOCUS_T nodes that describe where
176 ASSERT_EXPRs for SSA name N_I should be inserted. */
177 static assert_locus_t
*asserts_for
;
179 /* Value range array. After propagation, VR_VALUE[I] holds the range
180 of values that SSA name N_I may take. */
181 static unsigned num_vr_values
;
182 static value_range_t
**vr_value
;
183 static bool values_propagated
;
185 /* For a PHI node which sets SSA name N_I, VR_COUNTS[I] holds the
186 number of executable edges we saw the last time we visited the
188 static int *vr_phi_edge_counts
;
195 static vec
<edge
> to_remove_edges
;
196 static vec
<switch_update
> to_update_switch_stmts
;
199 /* Return the maximum value for TYPE. */
202 vrp_val_max (const_tree type
)
204 if (!INTEGRAL_TYPE_P (type
))
207 return TYPE_MAX_VALUE (type
);
210 /* Return the minimum value for TYPE. */
213 vrp_val_min (const_tree type
)
215 if (!INTEGRAL_TYPE_P (type
))
218 return TYPE_MIN_VALUE (type
);
221 /* Return whether VAL is equal to the maximum value of its type. This
222 will be true for a positive overflow infinity. We can't do a
223 simple equality comparison with TYPE_MAX_VALUE because C typedefs
224 and Ada subtypes can produce types whose TYPE_MAX_VALUE is not ==
225 to the integer constant with the same value in the type. */
228 vrp_val_is_max (const_tree val
)
230 tree type_max
= vrp_val_max (TREE_TYPE (val
));
231 return (val
== type_max
232 || (type_max
!= NULL_TREE
233 && operand_equal_p (val
, type_max
, 0)));
236 /* Return whether VAL is equal to the minimum value of its type. This
237 will be true for a negative overflow infinity. */
240 vrp_val_is_min (const_tree val
)
242 tree type_min
= vrp_val_min (TREE_TYPE (val
));
243 return (val
== type_min
244 || (type_min
!= NULL_TREE
245 && operand_equal_p (val
, type_min
, 0)));
249 /* Return whether TYPE should use an overflow infinity distinct from
250 TYPE_{MIN,MAX}_VALUE. We use an overflow infinity value to
251 represent a signed overflow during VRP computations. An infinity
252 is distinct from a half-range, which will go from some number to
253 TYPE_{MIN,MAX}_VALUE. */
256 needs_overflow_infinity (const_tree type
)
258 return INTEGRAL_TYPE_P (type
) && !TYPE_OVERFLOW_WRAPS (type
);
261 /* Return whether TYPE can support our overflow infinity
262 representation: we use the TREE_OVERFLOW flag, which only exists
263 for constants. If TYPE doesn't support this, we don't optimize
264 cases which would require signed overflow--we drop them to
268 supports_overflow_infinity (const_tree type
)
270 tree min
= vrp_val_min (type
), max
= vrp_val_max (type
);
271 #ifdef ENABLE_CHECKING
272 gcc_assert (needs_overflow_infinity (type
));
274 return (min
!= NULL_TREE
275 && CONSTANT_CLASS_P (min
)
277 && CONSTANT_CLASS_P (max
));
280 /* VAL is the maximum or minimum value of a type. Return a
281 corresponding overflow infinity. */
284 make_overflow_infinity (tree val
)
286 gcc_checking_assert (val
!= NULL_TREE
&& CONSTANT_CLASS_P (val
));
287 val
= copy_node (val
);
288 TREE_OVERFLOW (val
) = 1;
292 /* Return a negative overflow infinity for TYPE. */
295 negative_overflow_infinity (tree type
)
297 gcc_checking_assert (supports_overflow_infinity (type
));
298 return make_overflow_infinity (vrp_val_min (type
));
301 /* Return a positive overflow infinity for TYPE. */
304 positive_overflow_infinity (tree type
)
306 gcc_checking_assert (supports_overflow_infinity (type
));
307 return make_overflow_infinity (vrp_val_max (type
));
310 /* Return whether VAL is a negative overflow infinity. */
313 is_negative_overflow_infinity (const_tree val
)
315 return (TREE_OVERFLOW_P (val
)
316 && needs_overflow_infinity (TREE_TYPE (val
))
317 && vrp_val_is_min (val
));
320 /* Return whether VAL is a positive overflow infinity. */
323 is_positive_overflow_infinity (const_tree val
)
325 return (TREE_OVERFLOW_P (val
)
326 && needs_overflow_infinity (TREE_TYPE (val
))
327 && vrp_val_is_max (val
));
330 /* Return whether VAL is a positive or negative overflow infinity. */
333 is_overflow_infinity (const_tree val
)
335 return (TREE_OVERFLOW_P (val
)
336 && needs_overflow_infinity (TREE_TYPE (val
))
337 && (vrp_val_is_min (val
) || vrp_val_is_max (val
)));
340 /* Return whether STMT has a constant rhs that is_overflow_infinity. */
343 stmt_overflow_infinity (gimple stmt
)
345 if (is_gimple_assign (stmt
)
346 && get_gimple_rhs_class (gimple_assign_rhs_code (stmt
)) ==
348 return is_overflow_infinity (gimple_assign_rhs1 (stmt
));
352 /* If VAL is now an overflow infinity, return VAL. Otherwise, return
353 the same value with TREE_OVERFLOW clear. This can be used to avoid
354 confusing a regular value with an overflow value. */
357 avoid_overflow_infinity (tree val
)
359 if (!is_overflow_infinity (val
))
362 if (vrp_val_is_max (val
))
363 return vrp_val_max (TREE_TYPE (val
));
366 gcc_checking_assert (vrp_val_is_min (val
));
367 return vrp_val_min (TREE_TYPE (val
));
372 /* Return true if ARG is marked with the nonnull attribute in the
373 current function signature. */
376 nonnull_arg_p (const_tree arg
)
378 tree t
, attrs
, fntype
;
379 unsigned HOST_WIDE_INT arg_num
;
381 gcc_assert (TREE_CODE (arg
) == PARM_DECL
&& POINTER_TYPE_P (TREE_TYPE (arg
)));
383 /* The static chain decl is always non null. */
384 if (arg
== cfun
->static_chain_decl
)
387 /* THIS argument of method is always non-NULL. */
388 if (TREE_CODE (TREE_TYPE (current_function_decl
)) == METHOD_TYPE
389 && arg
== DECL_ARGUMENTS (current_function_decl
)
390 && flag_delete_null_pointer_checks
)
393 /* Values passed by reference are always non-NULL. */
394 if (TREE_CODE (TREE_TYPE (arg
)) == REFERENCE_TYPE
395 && flag_delete_null_pointer_checks
)
398 fntype
= TREE_TYPE (current_function_decl
);
399 for (attrs
= TYPE_ATTRIBUTES (fntype
); attrs
; attrs
= TREE_CHAIN (attrs
))
401 attrs
= lookup_attribute ("nonnull", attrs
);
403 /* If "nonnull" wasn't specified, we know nothing about the argument. */
404 if (attrs
== NULL_TREE
)
407 /* If "nonnull" applies to all the arguments, then ARG is non-null. */
408 if (TREE_VALUE (attrs
) == NULL_TREE
)
411 /* Get the position number for ARG in the function signature. */
412 for (arg_num
= 1, t
= DECL_ARGUMENTS (current_function_decl
);
414 t
= DECL_CHAIN (t
), arg_num
++)
420 gcc_assert (t
== arg
);
422 /* Now see if ARG_NUM is mentioned in the nonnull list. */
423 for (t
= TREE_VALUE (attrs
); t
; t
= TREE_CHAIN (t
))
425 if (compare_tree_int (TREE_VALUE (t
), arg_num
) == 0)
434 /* Set value range VR to VR_UNDEFINED. */
437 set_value_range_to_undefined (value_range_t
*vr
)
439 vr
->type
= VR_UNDEFINED
;
440 vr
->min
= vr
->max
= NULL_TREE
;
442 bitmap_clear (vr
->equiv
);
446 /* Set value range VR to VR_VARYING. */
449 set_value_range_to_varying (value_range_t
*vr
)
451 vr
->type
= VR_VARYING
;
452 vr
->min
= vr
->max
= NULL_TREE
;
454 bitmap_clear (vr
->equiv
);
458 /* Set value range VR to {T, MIN, MAX, EQUIV}. */
461 set_value_range (value_range_t
*vr
, enum value_range_type t
, tree min
,
462 tree max
, bitmap equiv
)
464 #if defined ENABLE_CHECKING
465 /* Check the validity of the range. */
466 if (t
== VR_RANGE
|| t
== VR_ANTI_RANGE
)
470 gcc_assert (min
&& max
);
472 gcc_assert ((!TREE_OVERFLOW_P (min
) || is_overflow_infinity (min
))
473 && (!TREE_OVERFLOW_P (max
) || is_overflow_infinity (max
)));
475 if (INTEGRAL_TYPE_P (TREE_TYPE (min
)) && t
== VR_ANTI_RANGE
)
476 gcc_assert (!vrp_val_is_min (min
) || !vrp_val_is_max (max
));
478 cmp
= compare_values (min
, max
);
479 gcc_assert (cmp
== 0 || cmp
== -1 || cmp
== -2);
481 if (needs_overflow_infinity (TREE_TYPE (min
)))
482 gcc_assert (!is_overflow_infinity (min
)
483 || !is_overflow_infinity (max
));
486 if (t
== VR_UNDEFINED
|| t
== VR_VARYING
)
487 gcc_assert (min
== NULL_TREE
&& max
== NULL_TREE
);
489 if (t
== VR_UNDEFINED
|| t
== VR_VARYING
)
490 gcc_assert (equiv
== NULL
|| bitmap_empty_p (equiv
));
497 /* Since updating the equivalence set involves deep copying the
498 bitmaps, only do it if absolutely necessary. */
499 if (vr
->equiv
== NULL
501 vr
->equiv
= BITMAP_ALLOC (NULL
);
503 if (equiv
!= vr
->equiv
)
505 if (equiv
&& !bitmap_empty_p (equiv
))
506 bitmap_copy (vr
->equiv
, equiv
);
508 bitmap_clear (vr
->equiv
);
513 /* Set value range VR to the canonical form of {T, MIN, MAX, EQUIV}.
514 This means adjusting T, MIN and MAX representing the case of a
515 wrapping range with MAX < MIN covering [MIN, type_max] U [type_min, MAX]
516 as anti-rage ~[MAX+1, MIN-1]. Likewise for wrapping anti-ranges.
517 In corner cases where MAX+1 or MIN-1 wraps this will fall back
519 This routine exists to ease canonicalization in the case where we
520 extract ranges from var + CST op limit. */
523 set_and_canonicalize_value_range (value_range_t
*vr
, enum value_range_type t
,
524 tree min
, tree max
, bitmap equiv
)
526 /* Use the canonical setters for VR_UNDEFINED and VR_VARYING. */
527 if (t
== VR_UNDEFINED
)
529 set_value_range_to_undefined (vr
);
532 else if (t
== VR_VARYING
)
534 set_value_range_to_varying (vr
);
538 /* Nothing to canonicalize for symbolic ranges. */
539 if (TREE_CODE (min
) != INTEGER_CST
540 || TREE_CODE (max
) != INTEGER_CST
)
542 set_value_range (vr
, t
, min
, max
, equiv
);
546 /* Wrong order for min and max, to swap them and the VR type we need
548 if (tree_int_cst_lt (max
, min
))
552 /* For one bit precision if max < min, then the swapped
553 range covers all values, so for VR_RANGE it is varying and
554 for VR_ANTI_RANGE empty range, so drop to varying as well. */
555 if (TYPE_PRECISION (TREE_TYPE (min
)) == 1)
557 set_value_range_to_varying (vr
);
561 one
= build_int_cst (TREE_TYPE (min
), 1);
562 tmp
= int_const_binop (PLUS_EXPR
, max
, one
);
563 max
= int_const_binop (MINUS_EXPR
, min
, one
);
566 /* There's one corner case, if we had [C+1, C] before we now have
567 that again. But this represents an empty value range, so drop
568 to varying in this case. */
569 if (tree_int_cst_lt (max
, min
))
571 set_value_range_to_varying (vr
);
575 t
= t
== VR_RANGE
? VR_ANTI_RANGE
: VR_RANGE
;
578 /* Anti-ranges that can be represented as ranges should be so. */
579 if (t
== VR_ANTI_RANGE
)
581 bool is_min
= vrp_val_is_min (min
);
582 bool is_max
= vrp_val_is_max (max
);
584 if (is_min
&& is_max
)
586 /* We cannot deal with empty ranges, drop to varying.
587 ??? This could be VR_UNDEFINED instead. */
588 set_value_range_to_varying (vr
);
591 else if (TYPE_PRECISION (TREE_TYPE (min
)) == 1
592 && (is_min
|| is_max
))
594 /* Non-empty boolean ranges can always be represented
595 as a singleton range. */
597 min
= max
= vrp_val_max (TREE_TYPE (min
));
599 min
= max
= vrp_val_min (TREE_TYPE (min
));
603 /* As a special exception preserve non-null ranges. */
604 && !(TYPE_UNSIGNED (TREE_TYPE (min
))
605 && integer_zerop (max
)))
607 tree one
= build_int_cst (TREE_TYPE (max
), 1);
608 min
= int_const_binop (PLUS_EXPR
, max
, one
);
609 max
= vrp_val_max (TREE_TYPE (max
));
614 tree one
= build_int_cst (TREE_TYPE (min
), 1);
615 max
= int_const_binop (MINUS_EXPR
, min
, one
);
616 min
= vrp_val_min (TREE_TYPE (min
));
621 /* Drop [-INF(OVF), +INF(OVF)] to varying. */
622 if (needs_overflow_infinity (TREE_TYPE (min
))
623 && is_overflow_infinity (min
)
624 && is_overflow_infinity (max
))
626 set_value_range_to_varying (vr
);
630 set_value_range (vr
, t
, min
, max
, equiv
);
633 /* Copy value range FROM into value range TO. */
636 copy_value_range (value_range_t
*to
, value_range_t
*from
)
638 set_value_range (to
, from
->type
, from
->min
, from
->max
, from
->equiv
);
641 /* Set value range VR to a single value. This function is only called
642 with values we get from statements, and exists to clear the
643 TREE_OVERFLOW flag so that we don't think we have an overflow
644 infinity when we shouldn't. */
647 set_value_range_to_value (value_range_t
*vr
, tree val
, bitmap equiv
)
649 gcc_assert (is_gimple_min_invariant (val
));
650 if (TREE_OVERFLOW_P (val
))
651 val
= drop_tree_overflow (val
);
652 set_value_range (vr
, VR_RANGE
, val
, val
, equiv
);
655 /* Set value range VR to a non-negative range of type TYPE.
656 OVERFLOW_INFINITY indicates whether to use an overflow infinity
657 rather than TYPE_MAX_VALUE; this should be true if we determine
658 that the range is nonnegative based on the assumption that signed
659 overflow does not occur. */
662 set_value_range_to_nonnegative (value_range_t
*vr
, tree type
,
663 bool overflow_infinity
)
667 if (overflow_infinity
&& !supports_overflow_infinity (type
))
669 set_value_range_to_varying (vr
);
673 zero
= build_int_cst (type
, 0);
674 set_value_range (vr
, VR_RANGE
, zero
,
676 ? positive_overflow_infinity (type
)
677 : TYPE_MAX_VALUE (type
)),
681 /* Set value range VR to a non-NULL range of type TYPE. */
684 set_value_range_to_nonnull (value_range_t
*vr
, tree type
)
686 tree zero
= build_int_cst (type
, 0);
687 set_value_range (vr
, VR_ANTI_RANGE
, zero
, zero
, vr
->equiv
);
691 /* Set value range VR to a NULL range of type TYPE. */
694 set_value_range_to_null (value_range_t
*vr
, tree type
)
696 set_value_range_to_value (vr
, build_int_cst (type
, 0), vr
->equiv
);
700 /* Set value range VR to a range of a truthvalue of type TYPE. */
703 set_value_range_to_truthvalue (value_range_t
*vr
, tree type
)
705 if (TYPE_PRECISION (type
) == 1)
706 set_value_range_to_varying (vr
);
708 set_value_range (vr
, VR_RANGE
,
709 build_int_cst (type
, 0), build_int_cst (type
, 1),
714 /* If abs (min) < abs (max), set VR to [-max, max], if
715 abs (min) >= abs (max), set VR to [-min, min]. */
718 abs_extent_range (value_range_t
*vr
, tree min
, tree max
)
722 gcc_assert (TREE_CODE (min
) == INTEGER_CST
);
723 gcc_assert (TREE_CODE (max
) == INTEGER_CST
);
724 gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (min
)));
725 gcc_assert (!TYPE_UNSIGNED (TREE_TYPE (min
)));
726 min
= fold_unary (ABS_EXPR
, TREE_TYPE (min
), min
);
727 max
= fold_unary (ABS_EXPR
, TREE_TYPE (max
), max
);
728 if (TREE_OVERFLOW (min
) || TREE_OVERFLOW (max
))
730 set_value_range_to_varying (vr
);
733 cmp
= compare_values (min
, max
);
735 min
= fold_unary (NEGATE_EXPR
, TREE_TYPE (min
), max
);
736 else if (cmp
== 0 || cmp
== 1)
739 min
= fold_unary (NEGATE_EXPR
, TREE_TYPE (min
), min
);
743 set_value_range_to_varying (vr
);
746 set_and_canonicalize_value_range (vr
, VR_RANGE
, min
, max
, NULL
);
750 /* Return value range information for VAR.
752 If we have no values ranges recorded (ie, VRP is not running), then
753 return NULL. Otherwise create an empty range if none existed for VAR. */
755 static value_range_t
*
756 get_value_range (const_tree var
)
758 static const struct value_range_d vr_const_varying
759 = { VR_VARYING
, NULL_TREE
, NULL_TREE
, NULL
};
762 unsigned ver
= SSA_NAME_VERSION (var
);
764 /* If we have no recorded ranges, then return NULL. */
768 /* If we query the range for a new SSA name return an unmodifiable VARYING.
769 We should get here at most from the substitute-and-fold stage which
770 will never try to change values. */
771 if (ver
>= num_vr_values
)
772 return CONST_CAST (value_range_t
*, &vr_const_varying
);
778 /* After propagation finished do not allocate new value-ranges. */
779 if (values_propagated
)
780 return CONST_CAST (value_range_t
*, &vr_const_varying
);
782 /* Create a default value range. */
783 vr_value
[ver
] = vr
= XCNEW (value_range_t
);
785 /* Defer allocating the equivalence set. */
788 /* If VAR is a default definition of a parameter, the variable can
789 take any value in VAR's type. */
790 if (SSA_NAME_IS_DEFAULT_DEF (var
))
792 sym
= SSA_NAME_VAR (var
);
793 if (TREE_CODE (sym
) == PARM_DECL
)
795 /* Try to use the "nonnull" attribute to create ~[0, 0]
796 anti-ranges for pointers. Note that this is only valid with
797 default definitions of PARM_DECLs. */
798 if (POINTER_TYPE_P (TREE_TYPE (sym
))
799 && nonnull_arg_p (sym
))
800 set_value_range_to_nonnull (vr
, TREE_TYPE (sym
));
802 set_value_range_to_varying (vr
);
804 else if (TREE_CODE (sym
) == RESULT_DECL
805 && DECL_BY_REFERENCE (sym
))
806 set_value_range_to_nonnull (vr
, TREE_TYPE (sym
));
812 /* Return true, if VAL1 and VAL2 are equal values for VRP purposes. */
815 vrp_operand_equal_p (const_tree val1
, const_tree val2
)
819 if (!val1
|| !val2
|| !operand_equal_p (val1
, val2
, 0))
821 return is_overflow_infinity (val1
) == is_overflow_infinity (val2
);
824 /* Return true, if the bitmaps B1 and B2 are equal. */
827 vrp_bitmap_equal_p (const_bitmap b1
, const_bitmap b2
)
830 || ((!b1
|| bitmap_empty_p (b1
))
831 && (!b2
|| bitmap_empty_p (b2
)))
833 && bitmap_equal_p (b1
, b2
)));
836 /* Update the value range and equivalence set for variable VAR to
837 NEW_VR. Return true if NEW_VR is different from VAR's previous
840 NOTE: This function assumes that NEW_VR is a temporary value range
841 object created for the sole purpose of updating VAR's range. The
842 storage used by the equivalence set from NEW_VR will be freed by
843 this function. Do not call update_value_range when NEW_VR
844 is the range object associated with another SSA name. */
847 update_value_range (const_tree var
, value_range_t
*new_vr
)
849 value_range_t
*old_vr
;
852 /* If there is a value-range on the SSA name from earlier analysis
854 if (INTEGRAL_TYPE_P (TREE_TYPE (var
)))
857 value_range_type rtype
= get_range_info (var
, &min
, &max
);
858 if (rtype
== VR_RANGE
|| rtype
== VR_ANTI_RANGE
)
862 nr
.min
= wide_int_to_tree (TREE_TYPE (var
), min
);
863 nr
.max
= wide_int_to_tree (TREE_TYPE (var
), max
);
865 vrp_intersect_ranges (new_vr
, &nr
);
869 /* Update the value range, if necessary. */
870 old_vr
= get_value_range (var
);
871 is_new
= old_vr
->type
!= new_vr
->type
872 || !vrp_operand_equal_p (old_vr
->min
, new_vr
->min
)
873 || !vrp_operand_equal_p (old_vr
->max
, new_vr
->max
)
874 || !vrp_bitmap_equal_p (old_vr
->equiv
, new_vr
->equiv
);
878 /* Do not allow transitions up the lattice. The following
879 is slightly more awkward than just new_vr->type < old_vr->type
880 because VR_RANGE and VR_ANTI_RANGE need to be considered
881 the same. We may not have is_new when transitioning to
882 UNDEFINED. If old_vr->type is VARYING, we shouldn't be
884 if (new_vr
->type
== VR_UNDEFINED
)
886 BITMAP_FREE (new_vr
->equiv
);
887 set_value_range_to_varying (old_vr
);
888 set_value_range_to_varying (new_vr
);
892 set_value_range (old_vr
, new_vr
->type
, new_vr
->min
, new_vr
->max
,
896 BITMAP_FREE (new_vr
->equiv
);
902 /* Add VAR and VAR's equivalence set to EQUIV. This is the central
903 point where equivalence processing can be turned on/off. */
906 add_equivalence (bitmap
*equiv
, const_tree var
)
908 unsigned ver
= SSA_NAME_VERSION (var
);
909 value_range_t
*vr
= vr_value
[ver
];
912 *equiv
= BITMAP_ALLOC (NULL
);
913 bitmap_set_bit (*equiv
, ver
);
915 bitmap_ior_into (*equiv
, vr
->equiv
);
919 /* Return true if VR is ~[0, 0]. */
922 range_is_nonnull (value_range_t
*vr
)
924 return vr
->type
== VR_ANTI_RANGE
925 && integer_zerop (vr
->min
)
926 && integer_zerop (vr
->max
);
930 /* Return true if VR is [0, 0]. */
933 range_is_null (value_range_t
*vr
)
935 return vr
->type
== VR_RANGE
936 && integer_zerop (vr
->min
)
937 && integer_zerop (vr
->max
);
940 /* Return true if max and min of VR are INTEGER_CST. It's not necessary
944 range_int_cst_p (value_range_t
*vr
)
946 return (vr
->type
== VR_RANGE
947 && TREE_CODE (vr
->max
) == INTEGER_CST
948 && TREE_CODE (vr
->min
) == INTEGER_CST
);
951 /* Return true if VR is a INTEGER_CST singleton. */
954 range_int_cst_singleton_p (value_range_t
*vr
)
956 return (range_int_cst_p (vr
)
957 && !is_overflow_infinity (vr
->min
)
958 && !is_overflow_infinity (vr
->max
)
959 && tree_int_cst_equal (vr
->min
, vr
->max
));
962 /* Return true if value range VR involves at least one symbol. */
965 symbolic_range_p (value_range_t
*vr
)
967 return (!is_gimple_min_invariant (vr
->min
)
968 || !is_gimple_min_invariant (vr
->max
));
971 /* Return the single symbol (an SSA_NAME) contained in T if any, or NULL_TREE
972 otherwise. We only handle additive operations and set NEG to true if the
973 symbol is negated and INV to the invariant part, if any. */
976 get_single_symbol (tree t
, bool *neg
, tree
*inv
)
981 if (TREE_CODE (t
) == PLUS_EXPR
982 || TREE_CODE (t
) == POINTER_PLUS_EXPR
983 || TREE_CODE (t
) == MINUS_EXPR
)
985 if (is_gimple_min_invariant (TREE_OPERAND (t
, 0)))
987 neg_
= (TREE_CODE (t
) == MINUS_EXPR
);
988 inv_
= TREE_OPERAND (t
, 0);
989 t
= TREE_OPERAND (t
, 1);
991 else if (is_gimple_min_invariant (TREE_OPERAND (t
, 1)))
994 inv_
= TREE_OPERAND (t
, 1);
995 t
= TREE_OPERAND (t
, 0);
1006 if (TREE_CODE (t
) == NEGATE_EXPR
)
1008 t
= TREE_OPERAND (t
, 0);
1012 if (TREE_CODE (t
) != SSA_NAME
)
1020 /* The reverse operation: build a symbolic expression with TYPE
1021 from symbol SYM, negated according to NEG, and invariant INV. */
1024 build_symbolic_expr (tree type
, tree sym
, bool neg
, tree inv
)
1026 const bool pointer_p
= POINTER_TYPE_P (type
);
1030 t
= build1 (NEGATE_EXPR
, type
, t
);
1032 if (integer_zerop (inv
))
1035 return build2 (pointer_p
? POINTER_PLUS_EXPR
: PLUS_EXPR
, type
, t
, inv
);
1038 /* Return true if value range VR involves exactly one symbol SYM. */
1041 symbolic_range_based_on_p (value_range_t
*vr
, const_tree sym
)
1043 bool neg
, min_has_symbol
, max_has_symbol
;
1046 if (is_gimple_min_invariant (vr
->min
))
1047 min_has_symbol
= false;
1048 else if (get_single_symbol (vr
->min
, &neg
, &inv
) == sym
)
1049 min_has_symbol
= true;
1053 if (is_gimple_min_invariant (vr
->max
))
1054 max_has_symbol
= false;
1055 else if (get_single_symbol (vr
->max
, &neg
, &inv
) == sym
)
1056 max_has_symbol
= true;
1060 return (min_has_symbol
|| max_has_symbol
);
1063 /* Return true if value range VR uses an overflow infinity. */
1066 overflow_infinity_range_p (value_range_t
*vr
)
1068 return (vr
->type
== VR_RANGE
1069 && (is_overflow_infinity (vr
->min
)
1070 || is_overflow_infinity (vr
->max
)));
1073 /* Return false if we can not make a valid comparison based on VR;
1074 this will be the case if it uses an overflow infinity and overflow
1075 is not undefined (i.e., -fno-strict-overflow is in effect).
1076 Otherwise return true, and set *STRICT_OVERFLOW_P to true if VR
1077 uses an overflow infinity. */
1080 usable_range_p (value_range_t
*vr
, bool *strict_overflow_p
)
1082 gcc_assert (vr
->type
== VR_RANGE
);
1083 if (is_overflow_infinity (vr
->min
))
1085 *strict_overflow_p
= true;
1086 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (vr
->min
)))
1089 if (is_overflow_infinity (vr
->max
))
1091 *strict_overflow_p
= true;
1092 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (vr
->max
)))
1099 /* Return true if the result of assignment STMT is know to be non-negative.
1100 If the return value is based on the assumption that signed overflow is
1101 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1102 *STRICT_OVERFLOW_P.*/
1105 gimple_assign_nonnegative_warnv_p (gimple stmt
, bool *strict_overflow_p
)
1107 enum tree_code code
= gimple_assign_rhs_code (stmt
);
1108 switch (get_gimple_rhs_class (code
))
1110 case GIMPLE_UNARY_RHS
:
1111 return tree_unary_nonnegative_warnv_p (gimple_assign_rhs_code (stmt
),
1112 gimple_expr_type (stmt
),
1113 gimple_assign_rhs1 (stmt
),
1115 case GIMPLE_BINARY_RHS
:
1116 return tree_binary_nonnegative_warnv_p (gimple_assign_rhs_code (stmt
),
1117 gimple_expr_type (stmt
),
1118 gimple_assign_rhs1 (stmt
),
1119 gimple_assign_rhs2 (stmt
),
1121 case GIMPLE_TERNARY_RHS
:
1123 case GIMPLE_SINGLE_RHS
:
1124 return tree_single_nonnegative_warnv_p (gimple_assign_rhs1 (stmt
),
1126 case GIMPLE_INVALID_RHS
:
1133 /* Return true if return value of call STMT is know to be non-negative.
1134 If the return value is based on the assumption that signed overflow is
1135 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1136 *STRICT_OVERFLOW_P.*/
1139 gimple_call_nonnegative_warnv_p (gimple stmt
, bool *strict_overflow_p
)
1141 tree arg0
= gimple_call_num_args (stmt
) > 0 ?
1142 gimple_call_arg (stmt
, 0) : NULL_TREE
;
1143 tree arg1
= gimple_call_num_args (stmt
) > 1 ?
1144 gimple_call_arg (stmt
, 1) : NULL_TREE
;
1146 return tree_call_nonnegative_warnv_p (gimple_expr_type (stmt
),
1147 gimple_call_fndecl (stmt
),
1153 /* Return true if STMT is know to to compute a non-negative value.
1154 If the return value is based on the assumption that signed overflow is
1155 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1156 *STRICT_OVERFLOW_P.*/
1159 gimple_stmt_nonnegative_warnv_p (gimple stmt
, bool *strict_overflow_p
)
1161 switch (gimple_code (stmt
))
1164 return gimple_assign_nonnegative_warnv_p (stmt
, strict_overflow_p
);
1166 return gimple_call_nonnegative_warnv_p (stmt
, strict_overflow_p
);
1172 /* Return true if the result of assignment STMT is know to be non-zero.
1173 If the return value is based on the assumption that signed overflow is
1174 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1175 *STRICT_OVERFLOW_P.*/
1178 gimple_assign_nonzero_warnv_p (gimple stmt
, bool *strict_overflow_p
)
1180 enum tree_code code
= gimple_assign_rhs_code (stmt
);
1181 switch (get_gimple_rhs_class (code
))
1183 case GIMPLE_UNARY_RHS
:
1184 return tree_unary_nonzero_warnv_p (gimple_assign_rhs_code (stmt
),
1185 gimple_expr_type (stmt
),
1186 gimple_assign_rhs1 (stmt
),
1188 case GIMPLE_BINARY_RHS
:
1189 return tree_binary_nonzero_warnv_p (gimple_assign_rhs_code (stmt
),
1190 gimple_expr_type (stmt
),
1191 gimple_assign_rhs1 (stmt
),
1192 gimple_assign_rhs2 (stmt
),
1194 case GIMPLE_TERNARY_RHS
:
1196 case GIMPLE_SINGLE_RHS
:
1197 return tree_single_nonzero_warnv_p (gimple_assign_rhs1 (stmt
),
1199 case GIMPLE_INVALID_RHS
:
1206 /* Return true if STMT is known to compute a non-zero value.
1207 If the return value is based on the assumption that signed overflow is
1208 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1209 *STRICT_OVERFLOW_P.*/
1212 gimple_stmt_nonzero_warnv_p (gimple stmt
, bool *strict_overflow_p
)
1214 switch (gimple_code (stmt
))
1217 return gimple_assign_nonzero_warnv_p (stmt
, strict_overflow_p
);
1220 tree fndecl
= gimple_call_fndecl (stmt
);
1221 if (!fndecl
) return false;
1222 if (flag_delete_null_pointer_checks
&& !flag_check_new
1223 && DECL_IS_OPERATOR_NEW (fndecl
)
1224 && !TREE_NOTHROW (fndecl
))
1226 /* References are always non-NULL. */
1227 if (flag_delete_null_pointer_checks
1228 && TREE_CODE (TREE_TYPE (fndecl
)) == REFERENCE_TYPE
)
1230 if (flag_delete_null_pointer_checks
&&
1231 lookup_attribute ("returns_nonnull",
1232 TYPE_ATTRIBUTES (gimple_call_fntype (stmt
))))
1234 return gimple_alloca_call_p (stmt
);
1241 /* Like tree_expr_nonzero_warnv_p, but this function uses value ranges
1245 vrp_stmt_computes_nonzero (gimple stmt
, bool *strict_overflow_p
)
1247 if (gimple_stmt_nonzero_warnv_p (stmt
, strict_overflow_p
))
1250 /* If we have an expression of the form &X->a, then the expression
1251 is nonnull if X is nonnull. */
1252 if (is_gimple_assign (stmt
)
1253 && gimple_assign_rhs_code (stmt
) == ADDR_EXPR
)
1255 tree expr
= gimple_assign_rhs1 (stmt
);
1256 tree base
= get_base_address (TREE_OPERAND (expr
, 0));
1258 if (base
!= NULL_TREE
1259 && TREE_CODE (base
) == MEM_REF
1260 && TREE_CODE (TREE_OPERAND (base
, 0)) == SSA_NAME
)
1262 value_range_t
*vr
= get_value_range (TREE_OPERAND (base
, 0));
1263 if (range_is_nonnull (vr
))
1271 /* Returns true if EXPR is a valid value (as expected by compare_values) --
1272 a gimple invariant, or SSA_NAME +- CST. */
1275 valid_value_p (tree expr
)
1277 if (TREE_CODE (expr
) == SSA_NAME
)
1280 if (TREE_CODE (expr
) == PLUS_EXPR
1281 || TREE_CODE (expr
) == MINUS_EXPR
)
1282 return (TREE_CODE (TREE_OPERAND (expr
, 0)) == SSA_NAME
1283 && TREE_CODE (TREE_OPERAND (expr
, 1)) == INTEGER_CST
);
1285 return is_gimple_min_invariant (expr
);
1291 -2 if those are incomparable. */
1293 operand_less_p (tree val
, tree val2
)
1295 /* LT is folded faster than GE and others. Inline the common case. */
1296 if (TREE_CODE (val
) == INTEGER_CST
&& TREE_CODE (val2
) == INTEGER_CST
)
1297 return tree_int_cst_lt (val
, val2
);
1302 fold_defer_overflow_warnings ();
1304 tcmp
= fold_binary_to_constant (LT_EXPR
, boolean_type_node
, val
, val2
);
1306 fold_undefer_and_ignore_overflow_warnings ();
1309 || TREE_CODE (tcmp
) != INTEGER_CST
)
1312 if (!integer_zerop (tcmp
))
1316 /* val >= val2, not considering overflow infinity. */
1317 if (is_negative_overflow_infinity (val
))
1318 return is_negative_overflow_infinity (val2
) ? 0 : 1;
1319 else if (is_positive_overflow_infinity (val2
))
1320 return is_positive_overflow_infinity (val
) ? 0 : 1;
1325 /* Compare two values VAL1 and VAL2. Return
1327 -2 if VAL1 and VAL2 cannot be compared at compile-time,
1330 +1 if VAL1 > VAL2, and
1333 This is similar to tree_int_cst_compare but supports pointer values
1334 and values that cannot be compared at compile time.
1336 If STRICT_OVERFLOW_P is not NULL, then set *STRICT_OVERFLOW_P to
1337 true if the return value is only valid if we assume that signed
1338 overflow is undefined. */
1341 compare_values_warnv (tree val1
, tree val2
, bool *strict_overflow_p
)
1346 /* Below we rely on the fact that VAL1 and VAL2 are both pointers or
1348 gcc_assert (POINTER_TYPE_P (TREE_TYPE (val1
))
1349 == POINTER_TYPE_P (TREE_TYPE (val2
)));
1351 /* Convert the two values into the same type. This is needed because
1352 sizetype causes sign extension even for unsigned types. */
1353 val2
= fold_convert (TREE_TYPE (val1
), val2
);
1354 STRIP_USELESS_TYPE_CONVERSION (val2
);
1356 if ((TREE_CODE (val1
) == SSA_NAME
1357 || (TREE_CODE (val1
) == NEGATE_EXPR
1358 && TREE_CODE (TREE_OPERAND (val1
, 0)) == SSA_NAME
)
1359 || TREE_CODE (val1
) == PLUS_EXPR
1360 || TREE_CODE (val1
) == MINUS_EXPR
)
1361 && (TREE_CODE (val2
) == SSA_NAME
1362 || (TREE_CODE (val2
) == NEGATE_EXPR
1363 && TREE_CODE (TREE_OPERAND (val2
, 0)) == SSA_NAME
)
1364 || TREE_CODE (val2
) == PLUS_EXPR
1365 || TREE_CODE (val2
) == MINUS_EXPR
))
1367 tree n1
, c1
, n2
, c2
;
1368 enum tree_code code1
, code2
;
1370 /* If VAL1 and VAL2 are of the form '[-]NAME [+-] CST' or 'NAME',
1371 return -1 or +1 accordingly. If VAL1 and VAL2 don't use the
1372 same name, return -2. */
1373 if (TREE_CODE (val1
) == SSA_NAME
|| TREE_CODE (val1
) == NEGATE_EXPR
)
1381 code1
= TREE_CODE (val1
);
1382 n1
= TREE_OPERAND (val1
, 0);
1383 c1
= TREE_OPERAND (val1
, 1);
1384 if (tree_int_cst_sgn (c1
) == -1)
1386 if (is_negative_overflow_infinity (c1
))
1388 c1
= fold_unary_to_constant (NEGATE_EXPR
, TREE_TYPE (c1
), c1
);
1391 code1
= code1
== MINUS_EXPR
? PLUS_EXPR
: MINUS_EXPR
;
1395 if (TREE_CODE (val2
) == SSA_NAME
|| TREE_CODE (val2
) == NEGATE_EXPR
)
1403 code2
= TREE_CODE (val2
);
1404 n2
= TREE_OPERAND (val2
, 0);
1405 c2
= TREE_OPERAND (val2
, 1);
1406 if (tree_int_cst_sgn (c2
) == -1)
1408 if (is_negative_overflow_infinity (c2
))
1410 c2
= fold_unary_to_constant (NEGATE_EXPR
, TREE_TYPE (c2
), c2
);
1413 code2
= code2
== MINUS_EXPR
? PLUS_EXPR
: MINUS_EXPR
;
1417 /* Both values must use the same name. */
1418 if (TREE_CODE (n1
) == NEGATE_EXPR
&& TREE_CODE (n2
) == NEGATE_EXPR
)
1420 n1
= TREE_OPERAND (n1
, 0);
1421 n2
= TREE_OPERAND (n2
, 0);
1426 if (code1
== SSA_NAME
&& code2
== SSA_NAME
)
1430 /* If overflow is defined we cannot simplify more. */
1431 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (val1
)))
1434 if (strict_overflow_p
!= NULL
1435 && (code1
== SSA_NAME
|| !TREE_NO_WARNING (val1
))
1436 && (code2
== SSA_NAME
|| !TREE_NO_WARNING (val2
)))
1437 *strict_overflow_p
= true;
1439 if (code1
== SSA_NAME
)
1441 if (code2
== PLUS_EXPR
)
1442 /* NAME < NAME + CST */
1444 else if (code2
== MINUS_EXPR
)
1445 /* NAME > NAME - CST */
1448 else if (code1
== PLUS_EXPR
)
1450 if (code2
== SSA_NAME
)
1451 /* NAME + CST > NAME */
1453 else if (code2
== PLUS_EXPR
)
1454 /* NAME + CST1 > NAME + CST2, if CST1 > CST2 */
1455 return compare_values_warnv (c1
, c2
, strict_overflow_p
);
1456 else if (code2
== MINUS_EXPR
)
1457 /* NAME + CST1 > NAME - CST2 */
1460 else if (code1
== MINUS_EXPR
)
1462 if (code2
== SSA_NAME
)
1463 /* NAME - CST < NAME */
1465 else if (code2
== PLUS_EXPR
)
1466 /* NAME - CST1 < NAME + CST2 */
1468 else if (code2
== MINUS_EXPR
)
1469 /* NAME - CST1 > NAME - CST2, if CST1 < CST2. Notice that
1470 C1 and C2 are swapped in the call to compare_values. */
1471 return compare_values_warnv (c2
, c1
, strict_overflow_p
);
1477 /* We cannot compare non-constants. */
1478 if (!is_gimple_min_invariant (val1
) || !is_gimple_min_invariant (val2
))
1481 if (!POINTER_TYPE_P (TREE_TYPE (val1
)))
1483 /* We cannot compare overflowed values, except for overflow
1485 if (TREE_OVERFLOW (val1
) || TREE_OVERFLOW (val2
))
1487 if (strict_overflow_p
!= NULL
)
1488 *strict_overflow_p
= true;
1489 if (is_negative_overflow_infinity (val1
))
1490 return is_negative_overflow_infinity (val2
) ? 0 : -1;
1491 else if (is_negative_overflow_infinity (val2
))
1493 else if (is_positive_overflow_infinity (val1
))
1494 return is_positive_overflow_infinity (val2
) ? 0 : 1;
1495 else if (is_positive_overflow_infinity (val2
))
1500 return tree_int_cst_compare (val1
, val2
);
1506 /* First see if VAL1 and VAL2 are not the same. */
1507 if (val1
== val2
|| operand_equal_p (val1
, val2
, 0))
1510 /* If VAL1 is a lower address than VAL2, return -1. */
1511 if (operand_less_p (val1
, val2
) == 1)
1514 /* If VAL1 is a higher address than VAL2, return +1. */
1515 if (operand_less_p (val2
, val1
) == 1)
1518 /* If VAL1 is different than VAL2, return +2.
1519 For integer constants we either have already returned -1 or 1
1520 or they are equivalent. We still might succeed in proving
1521 something about non-trivial operands. */
1522 if (TREE_CODE (val1
) != INTEGER_CST
1523 || TREE_CODE (val2
) != INTEGER_CST
)
1525 t
= fold_binary_to_constant (NE_EXPR
, boolean_type_node
, val1
, val2
);
1526 if (t
&& integer_onep (t
))
1534 /* Compare values like compare_values_warnv, but treat comparisons of
1535 nonconstants which rely on undefined overflow as incomparable. */
1538 compare_values (tree val1
, tree val2
)
1544 ret
= compare_values_warnv (val1
, val2
, &sop
);
1546 && (!is_gimple_min_invariant (val1
) || !is_gimple_min_invariant (val2
)))
1552 /* Return 1 if VAL is inside value range MIN <= VAL <= MAX,
1553 0 if VAL is not inside [MIN, MAX],
1554 -2 if we cannot tell either way.
1556 Benchmark compile/20001226-1.c compilation time after changing this
1560 value_inside_range (tree val
, tree min
, tree max
)
1564 cmp1
= operand_less_p (val
, min
);
1570 cmp2
= operand_less_p (max
, val
);
1578 /* Return true if value ranges VR0 and VR1 have a non-empty
1581 Benchmark compile/20001226-1.c compilation time after changing this
1586 value_ranges_intersect_p (value_range_t
*vr0
, value_range_t
*vr1
)
1588 /* The value ranges do not intersect if the maximum of the first range is
1589 less than the minimum of the second range or vice versa.
1590 When those relations are unknown, we can't do any better. */
1591 if (operand_less_p (vr0
->max
, vr1
->min
) != 0)
1593 if (operand_less_p (vr1
->max
, vr0
->min
) != 0)
1599 /* Return 1 if [MIN, MAX] includes the value zero, 0 if it does not
1600 include the value zero, -2 if we cannot tell. */
1603 range_includes_zero_p (tree min
, tree max
)
1605 tree zero
= build_int_cst (TREE_TYPE (min
), 0);
1606 return value_inside_range (zero
, min
, max
);
1609 /* Return true if *VR is know to only contain nonnegative values. */
1612 value_range_nonnegative_p (value_range_t
*vr
)
1614 /* Testing for VR_ANTI_RANGE is not useful here as any anti-range
1615 which would return a useful value should be encoded as a
1617 if (vr
->type
== VR_RANGE
)
1619 int result
= compare_values (vr
->min
, integer_zero_node
);
1620 return (result
== 0 || result
== 1);
1626 /* If *VR has a value rante that is a single constant value return that,
1627 otherwise return NULL_TREE. */
1630 value_range_constant_singleton (value_range_t
*vr
)
1632 if (vr
->type
== VR_RANGE
1633 && operand_equal_p (vr
->min
, vr
->max
, 0)
1634 && is_gimple_min_invariant (vr
->min
))
1640 /* If OP has a value range with a single constant value return that,
1641 otherwise return NULL_TREE. This returns OP itself if OP is a
1645 op_with_constant_singleton_value_range (tree op
)
1647 if (is_gimple_min_invariant (op
))
1650 if (TREE_CODE (op
) != SSA_NAME
)
1653 return value_range_constant_singleton (get_value_range (op
));
1656 /* Return true if op is in a boolean [0, 1] value-range. */
1659 op_with_boolean_value_range_p (tree op
)
1663 if (TYPE_PRECISION (TREE_TYPE (op
)) == 1)
1666 if (integer_zerop (op
)
1667 || integer_onep (op
))
1670 if (TREE_CODE (op
) != SSA_NAME
)
1673 vr
= get_value_range (op
);
1674 return (vr
->type
== VR_RANGE
1675 && integer_zerop (vr
->min
)
1676 && integer_onep (vr
->max
));
1679 /* Extract value range information from an ASSERT_EXPR EXPR and store
1683 extract_range_from_assert (value_range_t
*vr_p
, tree expr
)
1685 tree var
, cond
, limit
, min
, max
, type
;
1686 value_range_t
*limit_vr
;
1687 enum tree_code cond_code
;
1689 var
= ASSERT_EXPR_VAR (expr
);
1690 cond
= ASSERT_EXPR_COND (expr
);
1692 gcc_assert (COMPARISON_CLASS_P (cond
));
1694 /* Find VAR in the ASSERT_EXPR conditional. */
1695 if (var
== TREE_OPERAND (cond
, 0)
1696 || TREE_CODE (TREE_OPERAND (cond
, 0)) == PLUS_EXPR
1697 || TREE_CODE (TREE_OPERAND (cond
, 0)) == NOP_EXPR
)
1699 /* If the predicate is of the form VAR COMP LIMIT, then we just
1700 take LIMIT from the RHS and use the same comparison code. */
1701 cond_code
= TREE_CODE (cond
);
1702 limit
= TREE_OPERAND (cond
, 1);
1703 cond
= TREE_OPERAND (cond
, 0);
1707 /* If the predicate is of the form LIMIT COMP VAR, then we need
1708 to flip around the comparison code to create the proper range
1710 cond_code
= swap_tree_comparison (TREE_CODE (cond
));
1711 limit
= TREE_OPERAND (cond
, 0);
1712 cond
= TREE_OPERAND (cond
, 1);
1715 limit
= avoid_overflow_infinity (limit
);
1717 type
= TREE_TYPE (var
);
1718 gcc_assert (limit
!= var
);
1720 /* For pointer arithmetic, we only keep track of pointer equality
1722 if (POINTER_TYPE_P (type
) && cond_code
!= NE_EXPR
&& cond_code
!= EQ_EXPR
)
1724 set_value_range_to_varying (vr_p
);
1728 /* If LIMIT is another SSA name and LIMIT has a range of its own,
1729 try to use LIMIT's range to avoid creating symbolic ranges
1731 limit_vr
= (TREE_CODE (limit
) == SSA_NAME
) ? get_value_range (limit
) : NULL
;
1733 /* LIMIT's range is only interesting if it has any useful information. */
1735 && (limit_vr
->type
== VR_UNDEFINED
1736 || limit_vr
->type
== VR_VARYING
1737 || symbolic_range_p (limit_vr
)))
1740 /* Initially, the new range has the same set of equivalences of
1741 VAR's range. This will be revised before returning the final
1742 value. Since assertions may be chained via mutually exclusive
1743 predicates, we will need to trim the set of equivalences before
1745 gcc_assert (vr_p
->equiv
== NULL
);
1746 add_equivalence (&vr_p
->equiv
, var
);
1748 /* Extract a new range based on the asserted comparison for VAR and
1749 LIMIT's value range. Notice that if LIMIT has an anti-range, we
1750 will only use it for equality comparisons (EQ_EXPR). For any
1751 other kind of assertion, we cannot derive a range from LIMIT's
1752 anti-range that can be used to describe the new range. For
1753 instance, ASSERT_EXPR <x_2, x_2 <= b_4>. If b_4 is ~[2, 10],
1754 then b_4 takes on the ranges [-INF, 1] and [11, +INF]. There is
1755 no single range for x_2 that could describe LE_EXPR, so we might
1756 as well build the range [b_4, +INF] for it.
1757 One special case we handle is extracting a range from a
1758 range test encoded as (unsigned)var + CST <= limit. */
1759 if (TREE_CODE (cond
) == NOP_EXPR
1760 || TREE_CODE (cond
) == PLUS_EXPR
)
1762 if (TREE_CODE (cond
) == PLUS_EXPR
)
1764 min
= fold_build1 (NEGATE_EXPR
, TREE_TYPE (TREE_OPERAND (cond
, 1)),
1765 TREE_OPERAND (cond
, 1));
1766 max
= int_const_binop (PLUS_EXPR
, limit
, min
);
1767 cond
= TREE_OPERAND (cond
, 0);
1771 min
= build_int_cst (TREE_TYPE (var
), 0);
1775 /* Make sure to not set TREE_OVERFLOW on the final type
1776 conversion. We are willingly interpreting large positive
1777 unsigned values as negative signed values here. */
1778 min
= force_fit_type (TREE_TYPE (var
), wi::to_widest (min
), 0, false);
1779 max
= force_fit_type (TREE_TYPE (var
), wi::to_widest (max
), 0, false);
1781 /* We can transform a max, min range to an anti-range or
1782 vice-versa. Use set_and_canonicalize_value_range which does
1784 if (cond_code
== LE_EXPR
)
1785 set_and_canonicalize_value_range (vr_p
, VR_RANGE
,
1786 min
, max
, vr_p
->equiv
);
1787 else if (cond_code
== GT_EXPR
)
1788 set_and_canonicalize_value_range (vr_p
, VR_ANTI_RANGE
,
1789 min
, max
, vr_p
->equiv
);
1793 else if (cond_code
== EQ_EXPR
)
1795 enum value_range_type range_type
;
1799 range_type
= limit_vr
->type
;
1800 min
= limit_vr
->min
;
1801 max
= limit_vr
->max
;
1805 range_type
= VR_RANGE
;
1810 set_value_range (vr_p
, range_type
, min
, max
, vr_p
->equiv
);
1812 /* When asserting the equality VAR == LIMIT and LIMIT is another
1813 SSA name, the new range will also inherit the equivalence set
1815 if (TREE_CODE (limit
) == SSA_NAME
)
1816 add_equivalence (&vr_p
->equiv
, limit
);
1818 else if (cond_code
== NE_EXPR
)
1820 /* As described above, when LIMIT's range is an anti-range and
1821 this assertion is an inequality (NE_EXPR), then we cannot
1822 derive anything from the anti-range. For instance, if
1823 LIMIT's range was ~[0, 0], the assertion 'VAR != LIMIT' does
1824 not imply that VAR's range is [0, 0]. So, in the case of
1825 anti-ranges, we just assert the inequality using LIMIT and
1828 If LIMIT_VR is a range, we can only use it to build a new
1829 anti-range if LIMIT_VR is a single-valued range. For
1830 instance, if LIMIT_VR is [0, 1], the predicate
1831 VAR != [0, 1] does not mean that VAR's range is ~[0, 1].
1832 Rather, it means that for value 0 VAR should be ~[0, 0]
1833 and for value 1, VAR should be ~[1, 1]. We cannot
1834 represent these ranges.
1836 The only situation in which we can build a valid
1837 anti-range is when LIMIT_VR is a single-valued range
1838 (i.e., LIMIT_VR->MIN == LIMIT_VR->MAX). In that case,
1839 build the anti-range ~[LIMIT_VR->MIN, LIMIT_VR->MAX]. */
1841 && limit_vr
->type
== VR_RANGE
1842 && compare_values (limit_vr
->min
, limit_vr
->max
) == 0)
1844 min
= limit_vr
->min
;
1845 max
= limit_vr
->max
;
1849 /* In any other case, we cannot use LIMIT's range to build a
1850 valid anti-range. */
1854 /* If MIN and MAX cover the whole range for their type, then
1855 just use the original LIMIT. */
1856 if (INTEGRAL_TYPE_P (type
)
1857 && vrp_val_is_min (min
)
1858 && vrp_val_is_max (max
))
1861 set_and_canonicalize_value_range (vr_p
, VR_ANTI_RANGE
,
1862 min
, max
, vr_p
->equiv
);
1864 else if (cond_code
== LE_EXPR
|| cond_code
== LT_EXPR
)
1866 min
= TYPE_MIN_VALUE (type
);
1868 if (limit_vr
== NULL
|| limit_vr
->type
== VR_ANTI_RANGE
)
1872 /* If LIMIT_VR is of the form [N1, N2], we need to build the
1873 range [MIN, N2] for LE_EXPR and [MIN, N2 - 1] for
1875 max
= limit_vr
->max
;
1878 /* If the maximum value forces us to be out of bounds, simply punt.
1879 It would be pointless to try and do anything more since this
1880 all should be optimized away above us. */
1881 if ((cond_code
== LT_EXPR
1882 && compare_values (max
, min
) == 0)
1883 || is_overflow_infinity (max
))
1884 set_value_range_to_varying (vr_p
);
1887 /* For LT_EXPR, we create the range [MIN, MAX - 1]. */
1888 if (cond_code
== LT_EXPR
)
1890 if (TYPE_PRECISION (TREE_TYPE (max
)) == 1
1891 && !TYPE_UNSIGNED (TREE_TYPE (max
)))
1892 max
= fold_build2 (PLUS_EXPR
, TREE_TYPE (max
), max
,
1893 build_int_cst (TREE_TYPE (max
), -1));
1895 max
= fold_build2 (MINUS_EXPR
, TREE_TYPE (max
), max
,
1896 build_int_cst (TREE_TYPE (max
), 1));
1898 TREE_NO_WARNING (max
) = 1;
1901 set_value_range (vr_p
, VR_RANGE
, min
, max
, vr_p
->equiv
);
1904 else if (cond_code
== GE_EXPR
|| cond_code
== GT_EXPR
)
1906 max
= TYPE_MAX_VALUE (type
);
1908 if (limit_vr
== NULL
|| limit_vr
->type
== VR_ANTI_RANGE
)
1912 /* If LIMIT_VR is of the form [N1, N2], we need to build the
1913 range [N1, MAX] for GE_EXPR and [N1 + 1, MAX] for
1915 min
= limit_vr
->min
;
1918 /* If the minimum value forces us to be out of bounds, simply punt.
1919 It would be pointless to try and do anything more since this
1920 all should be optimized away above us. */
1921 if ((cond_code
== GT_EXPR
1922 && compare_values (min
, max
) == 0)
1923 || is_overflow_infinity (min
))
1924 set_value_range_to_varying (vr_p
);
1927 /* For GT_EXPR, we create the range [MIN + 1, MAX]. */
1928 if (cond_code
== GT_EXPR
)
1930 if (TYPE_PRECISION (TREE_TYPE (min
)) == 1
1931 && !TYPE_UNSIGNED (TREE_TYPE (min
)))
1932 min
= fold_build2 (MINUS_EXPR
, TREE_TYPE (min
), min
,
1933 build_int_cst (TREE_TYPE (min
), -1));
1935 min
= fold_build2 (PLUS_EXPR
, TREE_TYPE (min
), min
,
1936 build_int_cst (TREE_TYPE (min
), 1));
1938 TREE_NO_WARNING (min
) = 1;
1941 set_value_range (vr_p
, VR_RANGE
, min
, max
, vr_p
->equiv
);
1947 /* Finally intersect the new range with what we already know about var. */
1948 vrp_intersect_ranges (vr_p
, get_value_range (var
));
1952 /* Extract range information from SSA name VAR and store it in VR. If
1953 VAR has an interesting range, use it. Otherwise, create the
1954 range [VAR, VAR] and return it. This is useful in situations where
1955 we may have conditionals testing values of VARYING names. For
1962 Even if y_5 is deemed VARYING, we can determine that x_3 > y_5 is
1966 extract_range_from_ssa_name (value_range_t
*vr
, tree var
)
1968 value_range_t
*var_vr
= get_value_range (var
);
1970 if (var_vr
->type
!= VR_VARYING
)
1971 copy_value_range (vr
, var_vr
);
1973 set_value_range (vr
, VR_RANGE
, var
, var
, NULL
);
1975 add_equivalence (&vr
->equiv
, var
);
1979 /* Wrapper around int_const_binop. If the operation overflows and we
1980 are not using wrapping arithmetic, then adjust the result to be
1981 -INF or +INF depending on CODE, VAL1 and VAL2. This can return
1982 NULL_TREE if we need to use an overflow infinity representation but
1983 the type does not support it. */
1986 vrp_int_const_binop (enum tree_code code
, tree val1
, tree val2
)
1990 res
= int_const_binop (code
, val1
, val2
);
1992 /* If we are using unsigned arithmetic, operate symbolically
1993 on -INF and +INF as int_const_binop only handles signed overflow. */
1994 if (TYPE_UNSIGNED (TREE_TYPE (val1
)))
1996 int checkz
= compare_values (res
, val1
);
1997 bool overflow
= false;
1999 /* Ensure that res = val1 [+*] val2 >= val1
2000 or that res = val1 - val2 <= val1. */
2001 if ((code
== PLUS_EXPR
2002 && !(checkz
== 1 || checkz
== 0))
2003 || (code
== MINUS_EXPR
2004 && !(checkz
== 0 || checkz
== -1)))
2008 /* Checking for multiplication overflow is done by dividing the
2009 output of the multiplication by the first input of the
2010 multiplication. If the result of that division operation is
2011 not equal to the second input of the multiplication, then the
2012 multiplication overflowed. */
2013 else if (code
== MULT_EXPR
&& !integer_zerop (val1
))
2015 tree tmp
= int_const_binop (TRUNC_DIV_EXPR
,
2018 int check
= compare_values (tmp
, val2
);
2026 res
= copy_node (res
);
2027 TREE_OVERFLOW (res
) = 1;
2031 else if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (val1
)))
2032 /* If the singed operation wraps then int_const_binop has done
2033 everything we want. */
2035 /* Signed division of -1/0 overflows and by the time it gets here
2036 returns NULL_TREE. */
2039 else if ((TREE_OVERFLOW (res
)
2040 && !TREE_OVERFLOW (val1
)
2041 && !TREE_OVERFLOW (val2
))
2042 || is_overflow_infinity (val1
)
2043 || is_overflow_infinity (val2
))
2045 /* If the operation overflowed but neither VAL1 nor VAL2 are
2046 overflown, return -INF or +INF depending on the operation
2047 and the combination of signs of the operands. */
2048 int sgn1
= tree_int_cst_sgn (val1
);
2049 int sgn2
= tree_int_cst_sgn (val2
);
2051 if (needs_overflow_infinity (TREE_TYPE (res
))
2052 && !supports_overflow_infinity (TREE_TYPE (res
)))
2055 /* We have to punt on adding infinities of different signs,
2056 since we can't tell what the sign of the result should be.
2057 Likewise for subtracting infinities of the same sign. */
2058 if (((code
== PLUS_EXPR
&& sgn1
!= sgn2
)
2059 || (code
== MINUS_EXPR
&& sgn1
== sgn2
))
2060 && is_overflow_infinity (val1
)
2061 && is_overflow_infinity (val2
))
2064 /* Don't try to handle division or shifting of infinities. */
2065 if ((code
== TRUNC_DIV_EXPR
2066 || code
== FLOOR_DIV_EXPR
2067 || code
== CEIL_DIV_EXPR
2068 || code
== EXACT_DIV_EXPR
2069 || code
== ROUND_DIV_EXPR
2070 || code
== RSHIFT_EXPR
)
2071 && (is_overflow_infinity (val1
)
2072 || is_overflow_infinity (val2
)))
2075 /* Notice that we only need to handle the restricted set of
2076 operations handled by extract_range_from_binary_expr.
2077 Among them, only multiplication, addition and subtraction
2078 can yield overflow without overflown operands because we
2079 are working with integral types only... except in the
2080 case VAL1 = -INF and VAL2 = -1 which overflows to +INF
2081 for division too. */
2083 /* For multiplication, the sign of the overflow is given
2084 by the comparison of the signs of the operands. */
2085 if ((code
== MULT_EXPR
&& sgn1
== sgn2
)
2086 /* For addition, the operands must be of the same sign
2087 to yield an overflow. Its sign is therefore that
2088 of one of the operands, for example the first. For
2089 infinite operands X + -INF is negative, not positive. */
2090 || (code
== PLUS_EXPR
2092 ? !is_negative_overflow_infinity (val2
)
2093 : is_positive_overflow_infinity (val2
)))
2094 /* For subtraction, non-infinite operands must be of
2095 different signs to yield an overflow. Its sign is
2096 therefore that of the first operand or the opposite of
2097 that of the second operand. A first operand of 0 counts
2098 as positive here, for the corner case 0 - (-INF), which
2099 overflows, but must yield +INF. For infinite operands 0
2100 - INF is negative, not positive. */
2101 || (code
== MINUS_EXPR
2103 ? !is_positive_overflow_infinity (val2
)
2104 : is_negative_overflow_infinity (val2
)))
2105 /* We only get in here with positive shift count, so the
2106 overflow direction is the same as the sign of val1.
2107 Actually rshift does not overflow at all, but we only
2108 handle the case of shifting overflowed -INF and +INF. */
2109 || (code
== RSHIFT_EXPR
2111 /* For division, the only case is -INF / -1 = +INF. */
2112 || code
== TRUNC_DIV_EXPR
2113 || code
== FLOOR_DIV_EXPR
2114 || code
== CEIL_DIV_EXPR
2115 || code
== EXACT_DIV_EXPR
2116 || code
== ROUND_DIV_EXPR
)
2117 return (needs_overflow_infinity (TREE_TYPE (res
))
2118 ? positive_overflow_infinity (TREE_TYPE (res
))
2119 : TYPE_MAX_VALUE (TREE_TYPE (res
)));
2121 return (needs_overflow_infinity (TREE_TYPE (res
))
2122 ? negative_overflow_infinity (TREE_TYPE (res
))
2123 : TYPE_MIN_VALUE (TREE_TYPE (res
)));
2130 /* For range VR compute two wide_int bitmasks. In *MAY_BE_NONZERO
2131 bitmask if some bit is unset, it means for all numbers in the range
2132 the bit is 0, otherwise it might be 0 or 1. In *MUST_BE_NONZERO
2133 bitmask if some bit is set, it means for all numbers in the range
2134 the bit is 1, otherwise it might be 0 or 1. */
2137 zero_nonzero_bits_from_vr (const tree expr_type
,
2139 wide_int
*may_be_nonzero
,
2140 wide_int
*must_be_nonzero
)
2142 *may_be_nonzero
= wi::minus_one (TYPE_PRECISION (expr_type
));
2143 *must_be_nonzero
= wi::zero (TYPE_PRECISION (expr_type
));
2144 if (!range_int_cst_p (vr
)
2145 || is_overflow_infinity (vr
->min
)
2146 || is_overflow_infinity (vr
->max
))
2149 if (range_int_cst_singleton_p (vr
))
2151 *may_be_nonzero
= vr
->min
;
2152 *must_be_nonzero
= *may_be_nonzero
;
2154 else if (tree_int_cst_sgn (vr
->min
) >= 0
2155 || tree_int_cst_sgn (vr
->max
) < 0)
2157 wide_int xor_mask
= wi::bit_xor (vr
->min
, vr
->max
);
2158 *may_be_nonzero
= wi::bit_or (vr
->min
, vr
->max
);
2159 *must_be_nonzero
= wi::bit_and (vr
->min
, vr
->max
);
2162 wide_int mask
= wi::mask (wi::floor_log2 (xor_mask
), false,
2163 may_be_nonzero
->get_precision ());
2164 *may_be_nonzero
= *may_be_nonzero
| mask
;
2165 *must_be_nonzero
= must_be_nonzero
->and_not (mask
);
2172 /* Create two value-ranges in *VR0 and *VR1 from the anti-range *AR
2173 so that *VR0 U *VR1 == *AR. Returns true if that is possible,
2174 false otherwise. If *AR can be represented with a single range
2175 *VR1 will be VR_UNDEFINED. */
2178 ranges_from_anti_range (value_range_t
*ar
,
2179 value_range_t
*vr0
, value_range_t
*vr1
)
2181 tree type
= TREE_TYPE (ar
->min
);
2183 vr0
->type
= VR_UNDEFINED
;
2184 vr1
->type
= VR_UNDEFINED
;
2186 if (ar
->type
!= VR_ANTI_RANGE
2187 || TREE_CODE (ar
->min
) != INTEGER_CST
2188 || TREE_CODE (ar
->max
) != INTEGER_CST
2189 || !vrp_val_min (type
)
2190 || !vrp_val_max (type
))
2193 if (!vrp_val_is_min (ar
->min
))
2195 vr0
->type
= VR_RANGE
;
2196 vr0
->min
= vrp_val_min (type
);
2197 vr0
->max
= wide_int_to_tree (type
, wi::sub (ar
->min
, 1));
2199 if (!vrp_val_is_max (ar
->max
))
2201 vr1
->type
= VR_RANGE
;
2202 vr1
->min
= wide_int_to_tree (type
, wi::add (ar
->max
, 1));
2203 vr1
->max
= vrp_val_max (type
);
2205 if (vr0
->type
== VR_UNDEFINED
)
2208 vr1
->type
= VR_UNDEFINED
;
2211 return vr0
->type
!= VR_UNDEFINED
;
2214 /* Helper to extract a value-range *VR for a multiplicative operation
2218 extract_range_from_multiplicative_op_1 (value_range_t
*vr
,
2219 enum tree_code code
,
2220 value_range_t
*vr0
, value_range_t
*vr1
)
2222 enum value_range_type type
;
2229 /* Multiplications, divisions and shifts are a bit tricky to handle,
2230 depending on the mix of signs we have in the two ranges, we
2231 need to operate on different values to get the minimum and
2232 maximum values for the new range. One approach is to figure
2233 out all the variations of range combinations and do the
2236 However, this involves several calls to compare_values and it
2237 is pretty convoluted. It's simpler to do the 4 operations
2238 (MIN0 OP MIN1, MIN0 OP MAX1, MAX0 OP MIN1 and MAX0 OP MAX0 OP
2239 MAX1) and then figure the smallest and largest values to form
2241 gcc_assert (code
== MULT_EXPR
2242 || code
== TRUNC_DIV_EXPR
2243 || code
== FLOOR_DIV_EXPR
2244 || code
== CEIL_DIV_EXPR
2245 || code
== EXACT_DIV_EXPR
2246 || code
== ROUND_DIV_EXPR
2247 || code
== RSHIFT_EXPR
2248 || code
== LSHIFT_EXPR
);
2249 gcc_assert ((vr0
->type
== VR_RANGE
2250 || (code
== MULT_EXPR
&& vr0
->type
== VR_ANTI_RANGE
))
2251 && vr0
->type
== vr1
->type
);
2255 /* Compute the 4 cross operations. */
2257 val
[0] = vrp_int_const_binop (code
, vr0
->min
, vr1
->min
);
2258 if (val
[0] == NULL_TREE
)
2261 if (vr1
->max
== vr1
->min
)
2265 val
[1] = vrp_int_const_binop (code
, vr0
->min
, vr1
->max
);
2266 if (val
[1] == NULL_TREE
)
2270 if (vr0
->max
== vr0
->min
)
2274 val
[2] = vrp_int_const_binop (code
, vr0
->max
, vr1
->min
);
2275 if (val
[2] == NULL_TREE
)
2279 if (vr0
->min
== vr0
->max
|| vr1
->min
== vr1
->max
)
2283 val
[3] = vrp_int_const_binop (code
, vr0
->max
, vr1
->max
);
2284 if (val
[3] == NULL_TREE
)
2290 set_value_range_to_varying (vr
);
2294 /* Set MIN to the minimum of VAL[i] and MAX to the maximum
2298 for (i
= 1; i
< 4; i
++)
2300 if (!is_gimple_min_invariant (min
)
2301 || (TREE_OVERFLOW (min
) && !is_overflow_infinity (min
))
2302 || !is_gimple_min_invariant (max
)
2303 || (TREE_OVERFLOW (max
) && !is_overflow_infinity (max
)))
2308 if (!is_gimple_min_invariant (val
[i
])
2309 || (TREE_OVERFLOW (val
[i
])
2310 && !is_overflow_infinity (val
[i
])))
2312 /* If we found an overflowed value, set MIN and MAX
2313 to it so that we set the resulting range to
2319 if (compare_values (val
[i
], min
) == -1)
2322 if (compare_values (val
[i
], max
) == 1)
2327 /* If either MIN or MAX overflowed, then set the resulting range to
2328 VARYING. But we do accept an overflow infinity
2330 if (min
== NULL_TREE
2331 || !is_gimple_min_invariant (min
)
2332 || (TREE_OVERFLOW (min
) && !is_overflow_infinity (min
))
2334 || !is_gimple_min_invariant (max
)
2335 || (TREE_OVERFLOW (max
) && !is_overflow_infinity (max
)))
2337 set_value_range_to_varying (vr
);
2343 2) [-INF, +-INF(OVF)]
2344 3) [+-INF(OVF), +INF]
2345 4) [+-INF(OVF), +-INF(OVF)]
2346 We learn nothing when we have INF and INF(OVF) on both sides.
2347 Note that we do accept [-INF, -INF] and [+INF, +INF] without
2349 if ((vrp_val_is_min (min
) || is_overflow_infinity (min
))
2350 && (vrp_val_is_max (max
) || is_overflow_infinity (max
)))
2352 set_value_range_to_varying (vr
);
2356 cmp
= compare_values (min
, max
);
2357 if (cmp
== -2 || cmp
== 1)
2359 /* If the new range has its limits swapped around (MIN > MAX),
2360 then the operation caused one of them to wrap around, mark
2361 the new range VARYING. */
2362 set_value_range_to_varying (vr
);
2365 set_value_range (vr
, type
, min
, max
, NULL
);
2368 /* Extract range information from a binary operation CODE based on
2369 the ranges of each of its operands *VR0 and *VR1 with resulting
2370 type EXPR_TYPE. The resulting range is stored in *VR. */
2373 extract_range_from_binary_expr_1 (value_range_t
*vr
,
2374 enum tree_code code
, tree expr_type
,
2375 value_range_t
*vr0_
, value_range_t
*vr1_
)
2377 value_range_t vr0
= *vr0_
, vr1
= *vr1_
;
2378 value_range_t vrtem0
= VR_INITIALIZER
, vrtem1
= VR_INITIALIZER
;
2379 enum value_range_type type
;
2380 tree min
= NULL_TREE
, max
= NULL_TREE
;
2383 if (!INTEGRAL_TYPE_P (expr_type
)
2384 && !POINTER_TYPE_P (expr_type
))
2386 set_value_range_to_varying (vr
);
2390 /* Not all binary expressions can be applied to ranges in a
2391 meaningful way. Handle only arithmetic operations. */
2392 if (code
!= PLUS_EXPR
2393 && code
!= MINUS_EXPR
2394 && code
!= POINTER_PLUS_EXPR
2395 && code
!= MULT_EXPR
2396 && code
!= TRUNC_DIV_EXPR
2397 && code
!= FLOOR_DIV_EXPR
2398 && code
!= CEIL_DIV_EXPR
2399 && code
!= EXACT_DIV_EXPR
2400 && code
!= ROUND_DIV_EXPR
2401 && code
!= TRUNC_MOD_EXPR
2402 && code
!= RSHIFT_EXPR
2403 && code
!= LSHIFT_EXPR
2406 && code
!= BIT_AND_EXPR
2407 && code
!= BIT_IOR_EXPR
2408 && code
!= BIT_XOR_EXPR
)
2410 set_value_range_to_varying (vr
);
2414 /* If both ranges are UNDEFINED, so is the result. */
2415 if (vr0
.type
== VR_UNDEFINED
&& vr1
.type
== VR_UNDEFINED
)
2417 set_value_range_to_undefined (vr
);
2420 /* If one of the ranges is UNDEFINED drop it to VARYING for the following
2421 code. At some point we may want to special-case operations that
2422 have UNDEFINED result for all or some value-ranges of the not UNDEFINED
2424 else if (vr0
.type
== VR_UNDEFINED
)
2425 set_value_range_to_varying (&vr0
);
2426 else if (vr1
.type
== VR_UNDEFINED
)
2427 set_value_range_to_varying (&vr1
);
2429 /* Now canonicalize anti-ranges to ranges when they are not symbolic
2430 and express ~[] op X as ([]' op X) U ([]'' op X). */
2431 if (vr0
.type
== VR_ANTI_RANGE
2432 && ranges_from_anti_range (&vr0
, &vrtem0
, &vrtem1
))
2434 extract_range_from_binary_expr_1 (vr
, code
, expr_type
, &vrtem0
, vr1_
);
2435 if (vrtem1
.type
!= VR_UNDEFINED
)
2437 value_range_t vrres
= VR_INITIALIZER
;
2438 extract_range_from_binary_expr_1 (&vrres
, code
, expr_type
,
2440 vrp_meet (vr
, &vrres
);
2444 /* Likewise for X op ~[]. */
2445 if (vr1
.type
== VR_ANTI_RANGE
2446 && ranges_from_anti_range (&vr1
, &vrtem0
, &vrtem1
))
2448 extract_range_from_binary_expr_1 (vr
, code
, expr_type
, vr0_
, &vrtem0
);
2449 if (vrtem1
.type
!= VR_UNDEFINED
)
2451 value_range_t vrres
= VR_INITIALIZER
;
2452 extract_range_from_binary_expr_1 (&vrres
, code
, expr_type
,
2454 vrp_meet (vr
, &vrres
);
2459 /* The type of the resulting value range defaults to VR0.TYPE. */
2462 /* Refuse to operate on VARYING ranges, ranges of different kinds
2463 and symbolic ranges. As an exception, we allow BIT_{AND,IOR}
2464 because we may be able to derive a useful range even if one of
2465 the operands is VR_VARYING or symbolic range. Similarly for
2466 divisions, MIN/MAX and PLUS/MINUS.
2468 TODO, we may be able to derive anti-ranges in some cases. */
2469 if (code
!= BIT_AND_EXPR
2470 && code
!= BIT_IOR_EXPR
2471 && code
!= TRUNC_DIV_EXPR
2472 && code
!= FLOOR_DIV_EXPR
2473 && code
!= CEIL_DIV_EXPR
2474 && code
!= EXACT_DIV_EXPR
2475 && code
!= ROUND_DIV_EXPR
2476 && code
!= TRUNC_MOD_EXPR
2479 && code
!= PLUS_EXPR
2480 && code
!= MINUS_EXPR
2481 && code
!= RSHIFT_EXPR
2482 && (vr0
.type
== VR_VARYING
2483 || vr1
.type
== VR_VARYING
2484 || vr0
.type
!= vr1
.type
2485 || symbolic_range_p (&vr0
)
2486 || symbolic_range_p (&vr1
)))
2488 set_value_range_to_varying (vr
);
2492 /* Now evaluate the expression to determine the new range. */
2493 if (POINTER_TYPE_P (expr_type
))
2495 if (code
== MIN_EXPR
|| code
== MAX_EXPR
)
2497 /* For MIN/MAX expressions with pointers, we only care about
2498 nullness, if both are non null, then the result is nonnull.
2499 If both are null, then the result is null. Otherwise they
2501 if (range_is_nonnull (&vr0
) && range_is_nonnull (&vr1
))
2502 set_value_range_to_nonnull (vr
, expr_type
);
2503 else if (range_is_null (&vr0
) && range_is_null (&vr1
))
2504 set_value_range_to_null (vr
, expr_type
);
2506 set_value_range_to_varying (vr
);
2508 else if (code
== POINTER_PLUS_EXPR
)
2510 /* For pointer types, we are really only interested in asserting
2511 whether the expression evaluates to non-NULL. */
2512 if (range_is_nonnull (&vr0
) || range_is_nonnull (&vr1
))
2513 set_value_range_to_nonnull (vr
, expr_type
);
2514 else if (range_is_null (&vr0
) && range_is_null (&vr1
))
2515 set_value_range_to_null (vr
, expr_type
);
2517 set_value_range_to_varying (vr
);
2519 else if (code
== BIT_AND_EXPR
)
2521 /* For pointer types, we are really only interested in asserting
2522 whether the expression evaluates to non-NULL. */
2523 if (range_is_nonnull (&vr0
) && range_is_nonnull (&vr1
))
2524 set_value_range_to_nonnull (vr
, expr_type
);
2525 else if (range_is_null (&vr0
) || range_is_null (&vr1
))
2526 set_value_range_to_null (vr
, expr_type
);
2528 set_value_range_to_varying (vr
);
2531 set_value_range_to_varying (vr
);
2536 /* For integer ranges, apply the operation to each end of the
2537 range and see what we end up with. */
2538 if (code
== PLUS_EXPR
|| code
== MINUS_EXPR
)
2540 const bool minus_p
= (code
== MINUS_EXPR
);
2541 tree min_op0
= vr0
.min
;
2542 tree min_op1
= minus_p
? vr1
.max
: vr1
.min
;
2543 tree max_op0
= vr0
.max
;
2544 tree max_op1
= minus_p
? vr1
.min
: vr1
.max
;
2545 tree sym_min_op0
= NULL_TREE
;
2546 tree sym_min_op1
= NULL_TREE
;
2547 tree sym_max_op0
= NULL_TREE
;
2548 tree sym_max_op1
= NULL_TREE
;
2549 bool neg_min_op0
, neg_min_op1
, neg_max_op0
, neg_max_op1
;
2551 /* If we have a PLUS or MINUS with two VR_RANGEs, either constant or
2552 single-symbolic ranges, try to compute the precise resulting range,
2553 but only if we know that this resulting range will also be constant
2554 or single-symbolic. */
2555 if (vr0
.type
== VR_RANGE
&& vr1
.type
== VR_RANGE
2556 && (TREE_CODE (min_op0
) == INTEGER_CST
2558 = get_single_symbol (min_op0
, &neg_min_op0
, &min_op0
)))
2559 && (TREE_CODE (min_op1
) == INTEGER_CST
2561 = get_single_symbol (min_op1
, &neg_min_op1
, &min_op1
)))
2562 && (!(sym_min_op0
&& sym_min_op1
)
2563 || (sym_min_op0
== sym_min_op1
2564 && neg_min_op0
== (minus_p
? neg_min_op1
: !neg_min_op1
)))
2565 && (TREE_CODE (max_op0
) == INTEGER_CST
2567 = get_single_symbol (max_op0
, &neg_max_op0
, &max_op0
)))
2568 && (TREE_CODE (max_op1
) == INTEGER_CST
2570 = get_single_symbol (max_op1
, &neg_max_op1
, &max_op1
)))
2571 && (!(sym_max_op0
&& sym_max_op1
)
2572 || (sym_max_op0
== sym_max_op1
2573 && neg_max_op0
== (minus_p
? neg_max_op1
: !neg_max_op1
))))
2575 const signop sgn
= TYPE_SIGN (expr_type
);
2576 const unsigned int prec
= TYPE_PRECISION (expr_type
);
2577 wide_int type_min
, type_max
, wmin
, wmax
;
2581 /* Get the lower and upper bounds of the type. */
2582 if (TYPE_OVERFLOW_WRAPS (expr_type
))
2584 type_min
= wi::min_value (prec
, sgn
);
2585 type_max
= wi::max_value (prec
, sgn
);
2589 type_min
= vrp_val_min (expr_type
);
2590 type_max
= vrp_val_max (expr_type
);
2593 /* Combine the lower bounds, if any. */
2594 if (min_op0
&& min_op1
)
2598 wmin
= wi::sub (min_op0
, min_op1
);
2600 /* Check for overflow. */
2601 if (wi::cmp (0, min_op1
, sgn
)
2602 != wi::cmp (wmin
, min_op0
, sgn
))
2603 min_ovf
= wi::cmp (min_op0
, min_op1
, sgn
);
2607 wmin
= wi::add (min_op0
, min_op1
);
2609 /* Check for overflow. */
2610 if (wi::cmp (min_op1
, 0, sgn
)
2611 != wi::cmp (wmin
, min_op0
, sgn
))
2612 min_ovf
= wi::cmp (min_op0
, wmin
, sgn
);
2618 wmin
= minus_p
? wi::neg (min_op1
) : min_op1
;
2620 wmin
= wi::shwi (0, prec
);
2622 /* Combine the upper bounds, if any. */
2623 if (max_op0
&& max_op1
)
2627 wmax
= wi::sub (max_op0
, max_op1
);
2629 /* Check for overflow. */
2630 if (wi::cmp (0, max_op1
, sgn
)
2631 != wi::cmp (wmax
, max_op0
, sgn
))
2632 max_ovf
= wi::cmp (max_op0
, max_op1
, sgn
);
2636 wmax
= wi::add (max_op0
, max_op1
);
2638 if (wi::cmp (max_op1
, 0, sgn
)
2639 != wi::cmp (wmax
, max_op0
, sgn
))
2640 max_ovf
= wi::cmp (max_op0
, wmax
, sgn
);
2646 wmax
= minus_p
? wi::neg (max_op1
) : max_op1
;
2648 wmax
= wi::shwi (0, prec
);
2650 /* Check for type overflow. */
2653 if (wi::cmp (wmin
, type_min
, sgn
) == -1)
2655 else if (wi::cmp (wmin
, type_max
, sgn
) == 1)
2660 if (wi::cmp (wmax
, type_min
, sgn
) == -1)
2662 else if (wi::cmp (wmax
, type_max
, sgn
) == 1)
2666 /* If we have overflow for the constant part and the resulting
2667 range will be symbolic, drop to VR_VARYING. */
2668 if ((min_ovf
&& sym_min_op0
!= sym_min_op1
)
2669 || (max_ovf
&& sym_max_op0
!= sym_max_op1
))
2671 set_value_range_to_varying (vr
);
2675 if (TYPE_OVERFLOW_WRAPS (expr_type
))
2677 /* If overflow wraps, truncate the values and adjust the
2678 range kind and bounds appropriately. */
2679 wide_int tmin
= wide_int::from (wmin
, prec
, sgn
);
2680 wide_int tmax
= wide_int::from (wmax
, prec
, sgn
);
2681 if (min_ovf
== max_ovf
)
2683 /* No overflow or both overflow or underflow. The
2684 range kind stays VR_RANGE. */
2685 min
= wide_int_to_tree (expr_type
, tmin
);
2686 max
= wide_int_to_tree (expr_type
, tmax
);
2688 else if (min_ovf
== -1 && max_ovf
== 1)
2690 /* Underflow and overflow, drop to VR_VARYING. */
2691 set_value_range_to_varying (vr
);
2696 /* Min underflow or max overflow. The range kind
2697 changes to VR_ANTI_RANGE. */
2698 bool covers
= false;
2699 wide_int tem
= tmin
;
2700 gcc_assert ((min_ovf
== -1 && max_ovf
== 0)
2701 || (max_ovf
== 1 && min_ovf
== 0));
2702 type
= VR_ANTI_RANGE
;
2704 if (wi::cmp (tmin
, tmax
, sgn
) < 0)
2707 if (wi::cmp (tmax
, tem
, sgn
) > 0)
2709 /* If the anti-range would cover nothing, drop to varying.
2710 Likewise if the anti-range bounds are outside of the
2712 if (covers
|| wi::cmp (tmin
, tmax
, sgn
) > 0)
2714 set_value_range_to_varying (vr
);
2717 min
= wide_int_to_tree (expr_type
, tmin
);
2718 max
= wide_int_to_tree (expr_type
, tmax
);
2723 /* If overflow does not wrap, saturate to the types min/max
2727 if (needs_overflow_infinity (expr_type
)
2728 && supports_overflow_infinity (expr_type
))
2729 min
= negative_overflow_infinity (expr_type
);
2731 min
= wide_int_to_tree (expr_type
, type_min
);
2733 else if (min_ovf
== 1)
2735 if (needs_overflow_infinity (expr_type
)
2736 && supports_overflow_infinity (expr_type
))
2737 min
= positive_overflow_infinity (expr_type
);
2739 min
= wide_int_to_tree (expr_type
, type_max
);
2742 min
= wide_int_to_tree (expr_type
, wmin
);
2746 if (needs_overflow_infinity (expr_type
)
2747 && supports_overflow_infinity (expr_type
))
2748 max
= negative_overflow_infinity (expr_type
);
2750 max
= wide_int_to_tree (expr_type
, type_min
);
2752 else if (max_ovf
== 1)
2754 if (needs_overflow_infinity (expr_type
)
2755 && supports_overflow_infinity (expr_type
))
2756 max
= positive_overflow_infinity (expr_type
);
2758 max
= wide_int_to_tree (expr_type
, type_max
);
2761 max
= wide_int_to_tree (expr_type
, wmax
);
2764 if (needs_overflow_infinity (expr_type
)
2765 && supports_overflow_infinity (expr_type
))
2767 if ((min_op0
&& is_negative_overflow_infinity (min_op0
))
2770 ? is_positive_overflow_infinity (min_op1
)
2771 : is_negative_overflow_infinity (min_op1
))))
2772 min
= negative_overflow_infinity (expr_type
);
2773 if ((max_op0
&& is_positive_overflow_infinity (max_op0
))
2776 ? is_negative_overflow_infinity (max_op1
)
2777 : is_positive_overflow_infinity (max_op1
))))
2778 max
= positive_overflow_infinity (expr_type
);
2781 /* If the result lower bound is constant, we're done;
2782 otherwise, build the symbolic lower bound. */
2783 if (sym_min_op0
== sym_min_op1
)
2785 else if (sym_min_op0
)
2786 min
= build_symbolic_expr (expr_type
, sym_min_op0
,
2788 else if (sym_min_op1
)
2789 min
= build_symbolic_expr (expr_type
, sym_min_op1
,
2790 neg_min_op1
^ minus_p
, min
);
2792 /* Likewise for the upper bound. */
2793 if (sym_max_op0
== sym_max_op1
)
2795 else if (sym_max_op0
)
2796 max
= build_symbolic_expr (expr_type
, sym_max_op0
,
2798 else if (sym_max_op1
)
2799 max
= build_symbolic_expr (expr_type
, sym_max_op1
,
2800 neg_max_op1
^ minus_p
, max
);
2804 /* For other cases, for example if we have a PLUS_EXPR with two
2805 VR_ANTI_RANGEs, drop to VR_VARYING. It would take more effort
2806 to compute a precise range for such a case.
2807 ??? General even mixed range kind operations can be expressed
2808 by for example transforming ~[3, 5] + [1, 2] to range-only
2809 operations and a union primitive:
2810 [-INF, 2] + [1, 2] U [5, +INF] + [1, 2]
2811 [-INF+1, 4] U [6, +INF(OVF)]
2812 though usually the union is not exactly representable with
2813 a single range or anti-range as the above is
2814 [-INF+1, +INF(OVF)] intersected with ~[5, 5]
2815 but one could use a scheme similar to equivalences for this. */
2816 set_value_range_to_varying (vr
);
2820 else if (code
== MIN_EXPR
2821 || code
== MAX_EXPR
)
2823 if (vr0
.type
== VR_RANGE
2824 && !symbolic_range_p (&vr0
))
2827 if (vr1
.type
== VR_RANGE
2828 && !symbolic_range_p (&vr1
))
2830 /* For operations that make the resulting range directly
2831 proportional to the original ranges, apply the operation to
2832 the same end of each range. */
2833 min
= vrp_int_const_binop (code
, vr0
.min
, vr1
.min
);
2834 max
= vrp_int_const_binop (code
, vr0
.max
, vr1
.max
);
2836 else if (code
== MIN_EXPR
)
2838 min
= vrp_val_min (expr_type
);
2841 else if (code
== MAX_EXPR
)
2844 max
= vrp_val_max (expr_type
);
2847 else if (vr1
.type
== VR_RANGE
2848 && !symbolic_range_p (&vr1
))
2851 if (code
== MIN_EXPR
)
2853 min
= vrp_val_min (expr_type
);
2856 else if (code
== MAX_EXPR
)
2859 max
= vrp_val_max (expr_type
);
2864 set_value_range_to_varying (vr
);
2868 else if (code
== MULT_EXPR
)
2870 /* Fancy code so that with unsigned, [-3,-1]*[-3,-1] does not
2871 drop to varying. This test requires 2*prec bits if both
2872 operands are signed and 2*prec + 2 bits if either is not. */
2874 signop sign
= TYPE_SIGN (expr_type
);
2875 unsigned int prec
= TYPE_PRECISION (expr_type
);
2877 if (range_int_cst_p (&vr0
)
2878 && range_int_cst_p (&vr1
)
2879 && TYPE_OVERFLOW_WRAPS (expr_type
))
2881 typedef FIXED_WIDE_INT (WIDE_INT_MAX_PRECISION
* 2) vrp_int
;
2882 typedef generic_wide_int
2883 <wi::extended_tree
<WIDE_INT_MAX_PRECISION
* 2> > vrp_int_cst
;
2884 vrp_int sizem1
= wi::mask
<vrp_int
> (prec
, false);
2885 vrp_int size
= sizem1
+ 1;
2887 /* Extend the values using the sign of the result to PREC2.
2888 From here on out, everthing is just signed math no matter
2889 what the input types were. */
2890 vrp_int min0
= vrp_int_cst (vr0
.min
);
2891 vrp_int max0
= vrp_int_cst (vr0
.max
);
2892 vrp_int min1
= vrp_int_cst (vr1
.min
);
2893 vrp_int max1
= vrp_int_cst (vr1
.max
);
2894 /* Canonicalize the intervals. */
2895 if (sign
== UNSIGNED
)
2897 if (wi::ltu_p (size
, min0
+ max0
))
2903 if (wi::ltu_p (size
, min1
+ max1
))
2910 vrp_int prod0
= min0
* min1
;
2911 vrp_int prod1
= min0
* max1
;
2912 vrp_int prod2
= max0
* min1
;
2913 vrp_int prod3
= max0
* max1
;
2915 /* Sort the 4 products so that min is in prod0 and max is in
2917 /* min0min1 > max0max1 */
2918 if (wi::gts_p (prod0
, prod3
))
2920 vrp_int tmp
= prod3
;
2925 /* min0max1 > max0min1 */
2926 if (wi::gts_p (prod1
, prod2
))
2928 vrp_int tmp
= prod2
;
2933 if (wi::gts_p (prod0
, prod1
))
2935 vrp_int tmp
= prod1
;
2940 if (wi::gts_p (prod2
, prod3
))
2942 vrp_int tmp
= prod3
;
2947 /* diff = max - min. */
2948 prod2
= prod3
- prod0
;
2949 if (wi::geu_p (prod2
, sizem1
))
2951 /* the range covers all values. */
2952 set_value_range_to_varying (vr
);
2956 /* The following should handle the wrapping and selecting
2957 VR_ANTI_RANGE for us. */
2958 min
= wide_int_to_tree (expr_type
, prod0
);
2959 max
= wide_int_to_tree (expr_type
, prod3
);
2960 set_and_canonicalize_value_range (vr
, VR_RANGE
, min
, max
, NULL
);
2964 /* If we have an unsigned MULT_EXPR with two VR_ANTI_RANGEs,
2965 drop to VR_VARYING. It would take more effort to compute a
2966 precise range for such a case. For example, if we have
2967 op0 == 65536 and op1 == 65536 with their ranges both being
2968 ~[0,0] on a 32-bit machine, we would have op0 * op1 == 0, so
2969 we cannot claim that the product is in ~[0,0]. Note that we
2970 are guaranteed to have vr0.type == vr1.type at this
2972 if (vr0
.type
== VR_ANTI_RANGE
2973 && !TYPE_OVERFLOW_UNDEFINED (expr_type
))
2975 set_value_range_to_varying (vr
);
2979 extract_range_from_multiplicative_op_1 (vr
, code
, &vr0
, &vr1
);
2982 else if (code
== RSHIFT_EXPR
2983 || code
== LSHIFT_EXPR
)
2985 /* If we have a RSHIFT_EXPR with any shift values outside [0..prec-1],
2986 then drop to VR_VARYING. Outside of this range we get undefined
2987 behavior from the shift operation. We cannot even trust
2988 SHIFT_COUNT_TRUNCATED at this stage, because that applies to rtl
2989 shifts, and the operation at the tree level may be widened. */
2990 if (range_int_cst_p (&vr1
)
2991 && compare_tree_int (vr1
.min
, 0) >= 0
2992 && compare_tree_int (vr1
.max
, TYPE_PRECISION (expr_type
)) == -1)
2994 if (code
== RSHIFT_EXPR
)
2996 /* Even if vr0 is VARYING or otherwise not usable, we can derive
2997 useful ranges just from the shift count. E.g.
2998 x >> 63 for signed 64-bit x is always [-1, 0]. */
2999 if (vr0
.type
!= VR_RANGE
|| symbolic_range_p (&vr0
))
3001 vr0
.type
= type
= VR_RANGE
;
3002 vr0
.min
= vrp_val_min (expr_type
);
3003 vr0
.max
= vrp_val_max (expr_type
);
3005 extract_range_from_multiplicative_op_1 (vr
, code
, &vr0
, &vr1
);
3008 /* We can map lshifts by constants to MULT_EXPR handling. */
3009 else if (code
== LSHIFT_EXPR
3010 && range_int_cst_singleton_p (&vr1
))
3012 bool saved_flag_wrapv
;
3013 value_range_t vr1p
= VR_INITIALIZER
;
3014 vr1p
.type
= VR_RANGE
;
3015 vr1p
.min
= (wide_int_to_tree
3017 wi::set_bit_in_zero (tree_to_shwi (vr1
.min
),
3018 TYPE_PRECISION (expr_type
))));
3019 vr1p
.max
= vr1p
.min
;
3020 /* We have to use a wrapping multiply though as signed overflow
3021 on lshifts is implementation defined in C89. */
3022 saved_flag_wrapv
= flag_wrapv
;
3024 extract_range_from_binary_expr_1 (vr
, MULT_EXPR
, expr_type
,
3026 flag_wrapv
= saved_flag_wrapv
;
3029 else if (code
== LSHIFT_EXPR
3030 && range_int_cst_p (&vr0
))
3032 int prec
= TYPE_PRECISION (expr_type
);
3033 int overflow_pos
= prec
;
3035 wide_int low_bound
, high_bound
;
3036 bool uns
= TYPE_UNSIGNED (expr_type
);
3037 bool in_bounds
= false;
3042 bound_shift
= overflow_pos
- tree_to_shwi (vr1
.max
);
3043 /* If bound_shift == HOST_BITS_PER_WIDE_INT, the llshift can
3044 overflow. However, for that to happen, vr1.max needs to be
3045 zero, which means vr1 is a singleton range of zero, which
3046 means it should be handled by the previous LSHIFT_EXPR
3048 wide_int bound
= wi::set_bit_in_zero (bound_shift
, prec
);
3049 wide_int complement
= ~(bound
- 1);
3054 high_bound
= complement
;
3055 if (wi::ltu_p (vr0
.max
, low_bound
))
3057 /* [5, 6] << [1, 2] == [10, 24]. */
3058 /* We're shifting out only zeroes, the value increases
3062 else if (wi::ltu_p (high_bound
, vr0
.min
))
3064 /* [0xffffff00, 0xffffffff] << [1, 2]
3065 == [0xfffffc00, 0xfffffffe]. */
3066 /* We're shifting out only ones, the value decreases
3073 /* [-1, 1] << [1, 2] == [-4, 4]. */
3074 low_bound
= complement
;
3076 if (wi::lts_p (vr0
.max
, high_bound
)
3077 && wi::lts_p (low_bound
, vr0
.min
))
3079 /* For non-negative numbers, we're shifting out only
3080 zeroes, the value increases monotonically.
3081 For negative numbers, we're shifting out only ones, the
3082 value decreases monotomically. */
3089 extract_range_from_multiplicative_op_1 (vr
, code
, &vr0
, &vr1
);
3094 set_value_range_to_varying (vr
);
3097 else if (code
== TRUNC_DIV_EXPR
3098 || code
== FLOOR_DIV_EXPR
3099 || code
== CEIL_DIV_EXPR
3100 || code
== EXACT_DIV_EXPR
3101 || code
== ROUND_DIV_EXPR
)
3103 if (vr0
.type
!= VR_RANGE
|| symbolic_range_p (&vr0
))
3105 /* For division, if op1 has VR_RANGE but op0 does not, something
3106 can be deduced just from that range. Say [min, max] / [4, max]
3107 gives [min / 4, max / 4] range. */
3108 if (vr1
.type
== VR_RANGE
3109 && !symbolic_range_p (&vr1
)
3110 && range_includes_zero_p (vr1
.min
, vr1
.max
) == 0)
3112 vr0
.type
= type
= VR_RANGE
;
3113 vr0
.min
= vrp_val_min (expr_type
);
3114 vr0
.max
= vrp_val_max (expr_type
);
3118 set_value_range_to_varying (vr
);
3123 /* For divisions, if flag_non_call_exceptions is true, we must
3124 not eliminate a division by zero. */
3125 if (cfun
->can_throw_non_call_exceptions
3126 && (vr1
.type
!= VR_RANGE
3127 || range_includes_zero_p (vr1
.min
, vr1
.max
) != 0))
3129 set_value_range_to_varying (vr
);
3133 /* For divisions, if op0 is VR_RANGE, we can deduce a range
3134 even if op1 is VR_VARYING, VR_ANTI_RANGE, symbolic or can
3136 if (vr0
.type
== VR_RANGE
3137 && (vr1
.type
!= VR_RANGE
3138 || range_includes_zero_p (vr1
.min
, vr1
.max
) != 0))
3140 tree zero
= build_int_cst (TREE_TYPE (vr0
.min
), 0);
3145 if (TYPE_UNSIGNED (expr_type
)
3146 || value_range_nonnegative_p (&vr1
))
3148 /* For unsigned division or when divisor is known
3149 to be non-negative, the range has to cover
3150 all numbers from 0 to max for positive max
3151 and all numbers from min to 0 for negative min. */
3152 cmp
= compare_values (vr0
.max
, zero
);
3155 else if (cmp
== 0 || cmp
== 1)
3159 cmp
= compare_values (vr0
.min
, zero
);
3162 else if (cmp
== 0 || cmp
== -1)
3169 /* Otherwise the range is -max .. max or min .. -min
3170 depending on which bound is bigger in absolute value,
3171 as the division can change the sign. */
3172 abs_extent_range (vr
, vr0
.min
, vr0
.max
);
3175 if (type
== VR_VARYING
)
3177 set_value_range_to_varying (vr
);
3183 extract_range_from_multiplicative_op_1 (vr
, code
, &vr0
, &vr1
);
3187 else if (code
== TRUNC_MOD_EXPR
)
3189 if (range_is_null (&vr1
))
3191 set_value_range_to_undefined (vr
);
3194 /* ABS (A % B) < ABS (B) and either
3195 0 <= A % B <= A or A <= A % B <= 0. */
3197 signop sgn
= TYPE_SIGN (expr_type
);
3198 unsigned int prec
= TYPE_PRECISION (expr_type
);
3199 wide_int wmin
, wmax
, tmp
;
3200 wide_int zero
= wi::zero (prec
);
3201 wide_int one
= wi::one (prec
);
3202 if (vr1
.type
== VR_RANGE
&& !symbolic_range_p (&vr1
))
3204 wmax
= wi::sub (vr1
.max
, one
);
3207 tmp
= wi::sub (wi::minus_one (prec
), vr1
.min
);
3208 wmax
= wi::smax (wmax
, tmp
);
3213 wmax
= wi::max_value (prec
, sgn
);
3214 /* X % INT_MIN may be INT_MAX. */
3215 if (sgn
== UNSIGNED
)
3219 if (sgn
== UNSIGNED
)
3224 if (vr0
.type
== VR_RANGE
&& TREE_CODE (vr0
.min
) == INTEGER_CST
)
3227 if (wi::gts_p (tmp
, zero
))
3229 wmin
= wi::smax (wmin
, tmp
);
3233 if (vr0
.type
== VR_RANGE
&& TREE_CODE (vr0
.max
) == INTEGER_CST
)
3236 if (sgn
== SIGNED
&& wi::neg_p (tmp
))
3238 wmax
= wi::min (wmax
, tmp
, sgn
);
3241 min
= wide_int_to_tree (expr_type
, wmin
);
3242 max
= wide_int_to_tree (expr_type
, wmax
);
3244 else if (code
== BIT_AND_EXPR
|| code
== BIT_IOR_EXPR
|| code
== BIT_XOR_EXPR
)
3246 bool int_cst_range0
, int_cst_range1
;
3247 wide_int may_be_nonzero0
, may_be_nonzero1
;
3248 wide_int must_be_nonzero0
, must_be_nonzero1
;
3250 int_cst_range0
= zero_nonzero_bits_from_vr (expr_type
, &vr0
,
3253 int_cst_range1
= zero_nonzero_bits_from_vr (expr_type
, &vr1
,
3258 if (code
== BIT_AND_EXPR
)
3260 min
= wide_int_to_tree (expr_type
,
3261 must_be_nonzero0
& must_be_nonzero1
);
3262 wide_int wmax
= may_be_nonzero0
& may_be_nonzero1
;
3263 /* If both input ranges contain only negative values we can
3264 truncate the result range maximum to the minimum of the
3265 input range maxima. */
3266 if (int_cst_range0
&& int_cst_range1
3267 && tree_int_cst_sgn (vr0
.max
) < 0
3268 && tree_int_cst_sgn (vr1
.max
) < 0)
3270 wmax
= wi::min (wmax
, vr0
.max
, TYPE_SIGN (expr_type
));
3271 wmax
= wi::min (wmax
, vr1
.max
, TYPE_SIGN (expr_type
));
3273 /* If either input range contains only non-negative values
3274 we can truncate the result range maximum to the respective
3275 maximum of the input range. */
3276 if (int_cst_range0
&& tree_int_cst_sgn (vr0
.min
) >= 0)
3277 wmax
= wi::min (wmax
, vr0
.max
, TYPE_SIGN (expr_type
));
3278 if (int_cst_range1
&& tree_int_cst_sgn (vr1
.min
) >= 0)
3279 wmax
= wi::min (wmax
, vr1
.max
, TYPE_SIGN (expr_type
));
3280 max
= wide_int_to_tree (expr_type
, wmax
);
3282 else if (code
== BIT_IOR_EXPR
)
3284 max
= wide_int_to_tree (expr_type
,
3285 may_be_nonzero0
| may_be_nonzero1
);
3286 wide_int wmin
= must_be_nonzero0
| must_be_nonzero1
;
3287 /* If the input ranges contain only positive values we can
3288 truncate the minimum of the result range to the maximum
3289 of the input range minima. */
3290 if (int_cst_range0
&& int_cst_range1
3291 && tree_int_cst_sgn (vr0
.min
) >= 0
3292 && tree_int_cst_sgn (vr1
.min
) >= 0)
3294 wmin
= wi::max (wmin
, vr0
.min
, TYPE_SIGN (expr_type
));
3295 wmin
= wi::max (wmin
, vr1
.min
, TYPE_SIGN (expr_type
));
3297 /* If either input range contains only negative values
3298 we can truncate the minimum of the result range to the
3299 respective minimum range. */
3300 if (int_cst_range0
&& tree_int_cst_sgn (vr0
.max
) < 0)
3301 wmin
= wi::max (wmin
, vr0
.min
, TYPE_SIGN (expr_type
));
3302 if (int_cst_range1
&& tree_int_cst_sgn (vr1
.max
) < 0)
3303 wmin
= wi::max (wmin
, vr1
.min
, TYPE_SIGN (expr_type
));
3304 min
= wide_int_to_tree (expr_type
, wmin
);
3306 else if (code
== BIT_XOR_EXPR
)
3308 wide_int result_zero_bits
= ((must_be_nonzero0
& must_be_nonzero1
)
3309 | ~(may_be_nonzero0
| may_be_nonzero1
));
3310 wide_int result_one_bits
3311 = (must_be_nonzero0
.and_not (may_be_nonzero1
)
3312 | must_be_nonzero1
.and_not (may_be_nonzero0
));
3313 max
= wide_int_to_tree (expr_type
, ~result_zero_bits
);
3314 min
= wide_int_to_tree (expr_type
, result_one_bits
);
3315 /* If the range has all positive or all negative values the
3316 result is better than VARYING. */
3317 if (tree_int_cst_sgn (min
) < 0
3318 || tree_int_cst_sgn (max
) >= 0)
3321 max
= min
= NULL_TREE
;
3327 /* If either MIN or MAX overflowed, then set the resulting range to
3328 VARYING. But we do accept an overflow infinity representation. */
3329 if (min
== NULL_TREE
3330 || (TREE_OVERFLOW_P (min
) && !is_overflow_infinity (min
))
3332 || (TREE_OVERFLOW_P (max
) && !is_overflow_infinity (max
)))
3334 set_value_range_to_varying (vr
);
3340 2) [-INF, +-INF(OVF)]
3341 3) [+-INF(OVF), +INF]
3342 4) [+-INF(OVF), +-INF(OVF)]
3343 We learn nothing when we have INF and INF(OVF) on both sides.
3344 Note that we do accept [-INF, -INF] and [+INF, +INF] without
3346 if ((vrp_val_is_min (min
) || is_overflow_infinity (min
))
3347 && (vrp_val_is_max (max
) || is_overflow_infinity (max
)))
3349 set_value_range_to_varying (vr
);
3353 cmp
= compare_values (min
, max
);
3354 if (cmp
== -2 || cmp
== 1)
3356 /* If the new range has its limits swapped around (MIN > MAX),
3357 then the operation caused one of them to wrap around, mark
3358 the new range VARYING. */
3359 set_value_range_to_varying (vr
);
3362 set_value_range (vr
, type
, min
, max
, NULL
);
3365 /* Extract range information from a binary expression OP0 CODE OP1 based on
3366 the ranges of each of its operands with resulting type EXPR_TYPE.
3367 The resulting range is stored in *VR. */
3370 extract_range_from_binary_expr (value_range_t
*vr
,
3371 enum tree_code code
,
3372 tree expr_type
, tree op0
, tree op1
)
3374 value_range_t vr0
= VR_INITIALIZER
;
3375 value_range_t vr1
= VR_INITIALIZER
;
3377 /* Get value ranges for each operand. For constant operands, create
3378 a new value range with the operand to simplify processing. */
3379 if (TREE_CODE (op0
) == SSA_NAME
)
3380 vr0
= *(get_value_range (op0
));
3381 else if (is_gimple_min_invariant (op0
))
3382 set_value_range_to_value (&vr0
, op0
, NULL
);
3384 set_value_range_to_varying (&vr0
);
3386 if (TREE_CODE (op1
) == SSA_NAME
)
3387 vr1
= *(get_value_range (op1
));
3388 else if (is_gimple_min_invariant (op1
))
3389 set_value_range_to_value (&vr1
, op1
, NULL
);
3391 set_value_range_to_varying (&vr1
);
3393 extract_range_from_binary_expr_1 (vr
, code
, expr_type
, &vr0
, &vr1
);
3395 /* Try harder for PLUS and MINUS if the range of one operand is symbolic
3396 and based on the other operand, for example if it was deduced from a
3397 symbolic comparison. When a bound of the range of the first operand
3398 is invariant, we set the corresponding bound of the new range to INF
3399 in order to avoid recursing on the range of the second operand. */
3400 if (vr
->type
== VR_VARYING
3401 && (code
== PLUS_EXPR
|| code
== MINUS_EXPR
)
3402 && TREE_CODE (op1
) == SSA_NAME
3403 && vr0
.type
== VR_RANGE
3404 && symbolic_range_based_on_p (&vr0
, op1
))
3406 const bool minus_p
= (code
== MINUS_EXPR
);
3407 value_range_t n_vr1
= VR_INITIALIZER
;
3409 /* Try with VR0 and [-INF, OP1]. */
3410 if (is_gimple_min_invariant (minus_p
? vr0
.max
: vr0
.min
))
3411 set_value_range (&n_vr1
, VR_RANGE
, vrp_val_min (expr_type
), op1
, NULL
);
3413 /* Try with VR0 and [OP1, +INF]. */
3414 else if (is_gimple_min_invariant (minus_p
? vr0
.min
: vr0
.max
))
3415 set_value_range (&n_vr1
, VR_RANGE
, op1
, vrp_val_max (expr_type
), NULL
);
3417 /* Try with VR0 and [OP1, OP1]. */
3419 set_value_range (&n_vr1
, VR_RANGE
, op1
, op1
, NULL
);
3421 extract_range_from_binary_expr_1 (vr
, code
, expr_type
, &vr0
, &n_vr1
);
3424 if (vr
->type
== VR_VARYING
3425 && (code
== PLUS_EXPR
|| code
== MINUS_EXPR
)
3426 && TREE_CODE (op0
) == SSA_NAME
3427 && vr1
.type
== VR_RANGE
3428 && symbolic_range_based_on_p (&vr1
, op0
))
3430 const bool minus_p
= (code
== MINUS_EXPR
);
3431 value_range_t n_vr0
= VR_INITIALIZER
;
3433 /* Try with [-INF, OP0] and VR1. */
3434 if (is_gimple_min_invariant (minus_p
? vr1
.max
: vr1
.min
))
3435 set_value_range (&n_vr0
, VR_RANGE
, vrp_val_min (expr_type
), op0
, NULL
);
3437 /* Try with [OP0, +INF] and VR1. */
3438 else if (is_gimple_min_invariant (minus_p
? vr1
.min
: vr1
.max
))
3439 set_value_range (&n_vr0
, VR_RANGE
, op0
, vrp_val_max (expr_type
), NULL
);
3441 /* Try with [OP0, OP0] and VR1. */
3443 set_value_range (&n_vr0
, VR_RANGE
, op0
, op0
, NULL
);
3445 extract_range_from_binary_expr_1 (vr
, code
, expr_type
, &n_vr0
, &vr1
);
3449 /* Extract range information from a unary operation CODE based on
3450 the range of its operand *VR0 with type OP0_TYPE with resulting type TYPE.
3451 The The resulting range is stored in *VR. */
3454 extract_range_from_unary_expr_1 (value_range_t
*vr
,
3455 enum tree_code code
, tree type
,
3456 value_range_t
*vr0_
, tree op0_type
)
3458 value_range_t vr0
= *vr0_
, vrtem0
= VR_INITIALIZER
, vrtem1
= VR_INITIALIZER
;
3460 /* VRP only operates on integral and pointer types. */
3461 if (!(INTEGRAL_TYPE_P (op0_type
)
3462 || POINTER_TYPE_P (op0_type
))
3463 || !(INTEGRAL_TYPE_P (type
)
3464 || POINTER_TYPE_P (type
)))
3466 set_value_range_to_varying (vr
);
3470 /* If VR0 is UNDEFINED, so is the result. */
3471 if (vr0
.type
== VR_UNDEFINED
)
3473 set_value_range_to_undefined (vr
);
3477 /* Handle operations that we express in terms of others. */
3478 if (code
== PAREN_EXPR
|| code
== OBJ_TYPE_REF
)
3480 /* PAREN_EXPR and OBJ_TYPE_REF are simple copies. */
3481 copy_value_range (vr
, &vr0
);
3484 else if (code
== NEGATE_EXPR
)
3486 /* -X is simply 0 - X, so re-use existing code that also handles
3487 anti-ranges fine. */
3488 value_range_t zero
= VR_INITIALIZER
;
3489 set_value_range_to_value (&zero
, build_int_cst (type
, 0), NULL
);
3490 extract_range_from_binary_expr_1 (vr
, MINUS_EXPR
, type
, &zero
, &vr0
);
3493 else if (code
== BIT_NOT_EXPR
)
3495 /* ~X is simply -1 - X, so re-use existing code that also handles
3496 anti-ranges fine. */
3497 value_range_t minusone
= VR_INITIALIZER
;
3498 set_value_range_to_value (&minusone
, build_int_cst (type
, -1), NULL
);
3499 extract_range_from_binary_expr_1 (vr
, MINUS_EXPR
,
3500 type
, &minusone
, &vr0
);
3504 /* Now canonicalize anti-ranges to ranges when they are not symbolic
3505 and express op ~[] as (op []') U (op []''). */
3506 if (vr0
.type
== VR_ANTI_RANGE
3507 && ranges_from_anti_range (&vr0
, &vrtem0
, &vrtem1
))
3509 extract_range_from_unary_expr_1 (vr
, code
, type
, &vrtem0
, op0_type
);
3510 if (vrtem1
.type
!= VR_UNDEFINED
)
3512 value_range_t vrres
= VR_INITIALIZER
;
3513 extract_range_from_unary_expr_1 (&vrres
, code
, type
,
3515 vrp_meet (vr
, &vrres
);
3520 if (CONVERT_EXPR_CODE_P (code
))
3522 tree inner_type
= op0_type
;
3523 tree outer_type
= type
;
3525 /* If the expression evaluates to a pointer, we are only interested in
3526 determining if it evaluates to NULL [0, 0] or non-NULL (~[0, 0]). */
3527 if (POINTER_TYPE_P (type
))
3529 if (range_is_nonnull (&vr0
))
3530 set_value_range_to_nonnull (vr
, type
);
3531 else if (range_is_null (&vr0
))
3532 set_value_range_to_null (vr
, type
);
3534 set_value_range_to_varying (vr
);
3538 /* If VR0 is varying and we increase the type precision, assume
3539 a full range for the following transformation. */
3540 if (vr0
.type
== VR_VARYING
3541 && INTEGRAL_TYPE_P (inner_type
)
3542 && TYPE_PRECISION (inner_type
) < TYPE_PRECISION (outer_type
))
3544 vr0
.type
= VR_RANGE
;
3545 vr0
.min
= TYPE_MIN_VALUE (inner_type
);
3546 vr0
.max
= TYPE_MAX_VALUE (inner_type
);
3549 /* If VR0 is a constant range or anti-range and the conversion is
3550 not truncating we can convert the min and max values and
3551 canonicalize the resulting range. Otherwise we can do the
3552 conversion if the size of the range is less than what the
3553 precision of the target type can represent and the range is
3554 not an anti-range. */
3555 if ((vr0
.type
== VR_RANGE
3556 || vr0
.type
== VR_ANTI_RANGE
)
3557 && TREE_CODE (vr0
.min
) == INTEGER_CST
3558 && TREE_CODE (vr0
.max
) == INTEGER_CST
3559 && (!is_overflow_infinity (vr0
.min
)
3560 || (vr0
.type
== VR_RANGE
3561 && TYPE_PRECISION (outer_type
) > TYPE_PRECISION (inner_type
)
3562 && needs_overflow_infinity (outer_type
)
3563 && supports_overflow_infinity (outer_type
)))
3564 && (!is_overflow_infinity (vr0
.max
)
3565 || (vr0
.type
== VR_RANGE
3566 && TYPE_PRECISION (outer_type
) > TYPE_PRECISION (inner_type
)
3567 && needs_overflow_infinity (outer_type
)
3568 && supports_overflow_infinity (outer_type
)))
3569 && (TYPE_PRECISION (outer_type
) >= TYPE_PRECISION (inner_type
)
3570 || (vr0
.type
== VR_RANGE
3571 && integer_zerop (int_const_binop (RSHIFT_EXPR
,
3572 int_const_binop (MINUS_EXPR
, vr0
.max
, vr0
.min
),
3573 size_int (TYPE_PRECISION (outer_type
)))))))
3575 tree new_min
, new_max
;
3576 if (is_overflow_infinity (vr0
.min
))
3577 new_min
= negative_overflow_infinity (outer_type
);
3579 new_min
= force_fit_type (outer_type
, wi::to_widest (vr0
.min
),
3581 if (is_overflow_infinity (vr0
.max
))
3582 new_max
= positive_overflow_infinity (outer_type
);
3584 new_max
= force_fit_type (outer_type
, wi::to_widest (vr0
.max
),
3586 set_and_canonicalize_value_range (vr
, vr0
.type
,
3587 new_min
, new_max
, NULL
);
3591 set_value_range_to_varying (vr
);
3594 else if (code
== ABS_EXPR
)
3599 /* Pass through vr0 in the easy cases. */
3600 if (TYPE_UNSIGNED (type
)
3601 || value_range_nonnegative_p (&vr0
))
3603 copy_value_range (vr
, &vr0
);
3607 /* For the remaining varying or symbolic ranges we can't do anything
3609 if (vr0
.type
== VR_VARYING
3610 || symbolic_range_p (&vr0
))
3612 set_value_range_to_varying (vr
);
3616 /* -TYPE_MIN_VALUE = TYPE_MIN_VALUE with flag_wrapv so we can't get a
3618 if (!TYPE_OVERFLOW_UNDEFINED (type
)
3619 && ((vr0
.type
== VR_RANGE
3620 && vrp_val_is_min (vr0
.min
))
3621 || (vr0
.type
== VR_ANTI_RANGE
3622 && !vrp_val_is_min (vr0
.min
))))
3624 set_value_range_to_varying (vr
);
3628 /* ABS_EXPR may flip the range around, if the original range
3629 included negative values. */
3630 if (is_overflow_infinity (vr0
.min
))
3631 min
= positive_overflow_infinity (type
);
3632 else if (!vrp_val_is_min (vr0
.min
))
3633 min
= fold_unary_to_constant (code
, type
, vr0
.min
);
3634 else if (!needs_overflow_infinity (type
))
3635 min
= TYPE_MAX_VALUE (type
);
3636 else if (supports_overflow_infinity (type
))
3637 min
= positive_overflow_infinity (type
);
3640 set_value_range_to_varying (vr
);
3644 if (is_overflow_infinity (vr0
.max
))
3645 max
= positive_overflow_infinity (type
);
3646 else if (!vrp_val_is_min (vr0
.max
))
3647 max
= fold_unary_to_constant (code
, type
, vr0
.max
);
3648 else if (!needs_overflow_infinity (type
))
3649 max
= TYPE_MAX_VALUE (type
);
3650 else if (supports_overflow_infinity (type
)
3651 /* We shouldn't generate [+INF, +INF] as set_value_range
3652 doesn't like this and ICEs. */
3653 && !is_positive_overflow_infinity (min
))
3654 max
= positive_overflow_infinity (type
);
3657 set_value_range_to_varying (vr
);
3661 cmp
= compare_values (min
, max
);
3663 /* If a VR_ANTI_RANGEs contains zero, then we have
3664 ~[-INF, min(MIN, MAX)]. */
3665 if (vr0
.type
== VR_ANTI_RANGE
)
3667 if (range_includes_zero_p (vr0
.min
, vr0
.max
) == 1)
3669 /* Take the lower of the two values. */
3673 /* Create ~[-INF, min (abs(MIN), abs(MAX))]
3674 or ~[-INF + 1, min (abs(MIN), abs(MAX))] when
3675 flag_wrapv is set and the original anti-range doesn't include
3676 TYPE_MIN_VALUE, remember -TYPE_MIN_VALUE = TYPE_MIN_VALUE. */
3677 if (TYPE_OVERFLOW_WRAPS (type
))
3679 tree type_min_value
= TYPE_MIN_VALUE (type
);
3681 min
= (vr0
.min
!= type_min_value
3682 ? int_const_binop (PLUS_EXPR
, type_min_value
,
3683 build_int_cst (TREE_TYPE (type_min_value
), 1))
3688 if (overflow_infinity_range_p (&vr0
))
3689 min
= negative_overflow_infinity (type
);
3691 min
= TYPE_MIN_VALUE (type
);
3696 /* All else has failed, so create the range [0, INF], even for
3697 flag_wrapv since TYPE_MIN_VALUE is in the original
3699 vr0
.type
= VR_RANGE
;
3700 min
= build_int_cst (type
, 0);
3701 if (needs_overflow_infinity (type
))
3703 if (supports_overflow_infinity (type
))
3704 max
= positive_overflow_infinity (type
);
3707 set_value_range_to_varying (vr
);
3712 max
= TYPE_MAX_VALUE (type
);
3716 /* If the range contains zero then we know that the minimum value in the
3717 range will be zero. */
3718 else if (range_includes_zero_p (vr0
.min
, vr0
.max
) == 1)
3722 min
= build_int_cst (type
, 0);
3726 /* If the range was reversed, swap MIN and MAX. */
3735 cmp
= compare_values (min
, max
);
3736 if (cmp
== -2 || cmp
== 1)
3738 /* If the new range has its limits swapped around (MIN > MAX),
3739 then the operation caused one of them to wrap around, mark
3740 the new range VARYING. */
3741 set_value_range_to_varying (vr
);
3744 set_value_range (vr
, vr0
.type
, min
, max
, NULL
);
3748 /* For unhandled operations fall back to varying. */
3749 set_value_range_to_varying (vr
);
3754 /* Extract range information from a unary expression CODE OP0 based on
3755 the range of its operand with resulting type TYPE.
3756 The resulting range is stored in *VR. */
3759 extract_range_from_unary_expr (value_range_t
*vr
, enum tree_code code
,
3760 tree type
, tree op0
)
3762 value_range_t vr0
= VR_INITIALIZER
;
3764 /* Get value ranges for the operand. For constant operands, create
3765 a new value range with the operand to simplify processing. */
3766 if (TREE_CODE (op0
) == SSA_NAME
)
3767 vr0
= *(get_value_range (op0
));
3768 else if (is_gimple_min_invariant (op0
))
3769 set_value_range_to_value (&vr0
, op0
, NULL
);
3771 set_value_range_to_varying (&vr0
);
3773 extract_range_from_unary_expr_1 (vr
, code
, type
, &vr0
, TREE_TYPE (op0
));
3777 /* Extract range information from a conditional expression STMT based on
3778 the ranges of each of its operands and the expression code. */
3781 extract_range_from_cond_expr (value_range_t
*vr
, gassign
*stmt
)
3784 value_range_t vr0
= VR_INITIALIZER
;
3785 value_range_t vr1
= VR_INITIALIZER
;
3787 /* Get value ranges for each operand. For constant operands, create
3788 a new value range with the operand to simplify processing. */
3789 op0
= gimple_assign_rhs2 (stmt
);
3790 if (TREE_CODE (op0
) == SSA_NAME
)
3791 vr0
= *(get_value_range (op0
));
3792 else if (is_gimple_min_invariant (op0
))
3793 set_value_range_to_value (&vr0
, op0
, NULL
);
3795 set_value_range_to_varying (&vr0
);
3797 op1
= gimple_assign_rhs3 (stmt
);
3798 if (TREE_CODE (op1
) == SSA_NAME
)
3799 vr1
= *(get_value_range (op1
));
3800 else if (is_gimple_min_invariant (op1
))
3801 set_value_range_to_value (&vr1
, op1
, NULL
);
3803 set_value_range_to_varying (&vr1
);
3805 /* The resulting value range is the union of the operand ranges */
3806 copy_value_range (vr
, &vr0
);
3807 vrp_meet (vr
, &vr1
);
3811 /* Extract range information from a comparison expression EXPR based
3812 on the range of its operand and the expression code. */
3815 extract_range_from_comparison (value_range_t
*vr
, enum tree_code code
,
3816 tree type
, tree op0
, tree op1
)
3821 val
= vrp_evaluate_conditional_warnv_with_ops (code
, op0
, op1
, false, &sop
,
3824 /* A disadvantage of using a special infinity as an overflow
3825 representation is that we lose the ability to record overflow
3826 when we don't have an infinity. So we have to ignore a result
3827 which relies on overflow. */
3829 if (val
&& !is_overflow_infinity (val
) && !sop
)
3831 /* Since this expression was found on the RHS of an assignment,
3832 its type may be different from _Bool. Convert VAL to EXPR's
3834 val
= fold_convert (type
, val
);
3835 if (is_gimple_min_invariant (val
))
3836 set_value_range_to_value (vr
, val
, vr
->equiv
);
3838 set_value_range (vr
, VR_RANGE
, val
, val
, vr
->equiv
);
3841 /* The result of a comparison is always true or false. */
3842 set_value_range_to_truthvalue (vr
, type
);
3845 /* Helper function for simplify_internal_call_using_ranges and
3846 extract_range_basic. Return true if OP0 SUBCODE OP1 for
3847 SUBCODE {PLUS,MINUS,MULT}_EXPR is known to never overflow or
3848 always overflow. Set *OVF to true if it is known to always
3852 check_for_binary_op_overflow (enum tree_code subcode
, tree type
,
3853 tree op0
, tree op1
, bool *ovf
)
3855 value_range_t vr0
= VR_INITIALIZER
;
3856 value_range_t vr1
= VR_INITIALIZER
;
3857 if (TREE_CODE (op0
) == SSA_NAME
)
3858 vr0
= *get_value_range (op0
);
3859 else if (TREE_CODE (op0
) == INTEGER_CST
)
3860 set_value_range_to_value (&vr0
, op0
, NULL
);
3862 set_value_range_to_varying (&vr0
);
3864 if (TREE_CODE (op1
) == SSA_NAME
)
3865 vr1
= *get_value_range (op1
);
3866 else if (TREE_CODE (op1
) == INTEGER_CST
)
3867 set_value_range_to_value (&vr1
, op1
, NULL
);
3869 set_value_range_to_varying (&vr1
);
3871 if (!range_int_cst_p (&vr0
)
3872 || TREE_OVERFLOW (vr0
.min
)
3873 || TREE_OVERFLOW (vr0
.max
))
3875 vr0
.min
= vrp_val_min (TREE_TYPE (op0
));
3876 vr0
.max
= vrp_val_max (TREE_TYPE (op0
));
3878 if (!range_int_cst_p (&vr1
)
3879 || TREE_OVERFLOW (vr1
.min
)
3880 || TREE_OVERFLOW (vr1
.max
))
3882 vr1
.min
= vrp_val_min (TREE_TYPE (op1
));
3883 vr1
.max
= vrp_val_max (TREE_TYPE (op1
));
3885 *ovf
= arith_overflowed_p (subcode
, type
, vr0
.min
,
3886 subcode
== MINUS_EXPR
? vr1
.max
: vr1
.min
);
3887 if (arith_overflowed_p (subcode
, type
, vr0
.max
,
3888 subcode
== MINUS_EXPR
? vr1
.min
: vr1
.max
) != *ovf
)
3890 if (subcode
== MULT_EXPR
)
3892 if (arith_overflowed_p (subcode
, type
, vr0
.min
, vr1
.max
) != *ovf
3893 || arith_overflowed_p (subcode
, type
, vr0
.max
, vr1
.min
) != *ovf
)
3898 /* So far we found that there is an overflow on the boundaries.
3899 That doesn't prove that there is an overflow even for all values
3900 in between the boundaries. For that compute widest_int range
3901 of the result and see if it doesn't overlap the range of
3903 widest_int wmin
, wmax
;
3906 w
[0] = wi::to_widest (vr0
.min
);
3907 w
[1] = wi::to_widest (vr0
.max
);
3908 w
[2] = wi::to_widest (vr1
.min
);
3909 w
[3] = wi::to_widest (vr1
.max
);
3910 for (i
= 0; i
< 4; i
++)
3916 wt
= wi::add (w
[i
& 1], w
[2 + (i
& 2) / 2]);
3919 wt
= wi::sub (w
[i
& 1], w
[2 + (i
& 2) / 2]);
3922 wt
= wi::mul (w
[i
& 1], w
[2 + (i
& 2) / 2]);
3934 wmin
= wi::smin (wmin
, wt
);
3935 wmax
= wi::smax (wmax
, wt
);
3938 /* The result of op0 CODE op1 is known to be in range
3940 widest_int wtmin
= wi::to_widest (vrp_val_min (type
));
3941 widest_int wtmax
= wi::to_widest (vrp_val_max (type
));
3942 /* If all values in [wmin, wmax] are smaller than
3943 [wtmin, wtmax] or all are larger than [wtmin, wtmax],
3944 the arithmetic operation will always overflow. */
3945 if (wi::lts_p (wmax
, wtmin
) || wi::gts_p (wmin
, wtmax
))
3952 /* Try to derive a nonnegative or nonzero range out of STMT relying
3953 primarily on generic routines in fold in conjunction with range data.
3954 Store the result in *VR */
3957 extract_range_basic (value_range_t
*vr
, gimple stmt
)
3960 tree type
= gimple_expr_type (stmt
);
3962 if (gimple_call_builtin_p (stmt
, BUILT_IN_NORMAL
))
3964 tree fndecl
= gimple_call_fndecl (stmt
), arg
;
3965 int mini
, maxi
, zerov
= 0, prec
;
3967 switch (DECL_FUNCTION_CODE (fndecl
))
3969 case BUILT_IN_CONSTANT_P
:
3970 /* If the call is __builtin_constant_p and the argument is a
3971 function parameter resolve it to false. This avoids bogus
3972 array bound warnings.
3973 ??? We could do this as early as inlining is finished. */
3974 arg
= gimple_call_arg (stmt
, 0);
3975 if (TREE_CODE (arg
) == SSA_NAME
3976 && SSA_NAME_IS_DEFAULT_DEF (arg
)
3977 && TREE_CODE (SSA_NAME_VAR (arg
)) == PARM_DECL
)
3979 set_value_range_to_null (vr
, type
);
3983 /* Both __builtin_ffs* and __builtin_popcount return
3985 CASE_INT_FN (BUILT_IN_FFS
):
3986 CASE_INT_FN (BUILT_IN_POPCOUNT
):
3987 arg
= gimple_call_arg (stmt
, 0);
3988 prec
= TYPE_PRECISION (TREE_TYPE (arg
));
3991 if (TREE_CODE (arg
) == SSA_NAME
)
3993 value_range_t
*vr0
= get_value_range (arg
);
3994 /* If arg is non-zero, then ffs or popcount
3996 if (((vr0
->type
== VR_RANGE
3997 && range_includes_zero_p (vr0
->min
, vr0
->max
) == 0)
3998 || (vr0
->type
== VR_ANTI_RANGE
3999 && range_includes_zero_p (vr0
->min
, vr0
->max
) == 1))
4000 && !is_overflow_infinity (vr0
->min
)
4001 && !is_overflow_infinity (vr0
->max
))
4003 /* If some high bits are known to be zero,
4004 we can decrease the maximum. */
4005 if (vr0
->type
== VR_RANGE
4006 && TREE_CODE (vr0
->max
) == INTEGER_CST
4007 && !operand_less_p (vr0
->min
,
4008 build_zero_cst (TREE_TYPE (vr0
->min
)))
4009 && !is_overflow_infinity (vr0
->max
))
4010 maxi
= tree_floor_log2 (vr0
->max
) + 1;
4013 /* __builtin_parity* returns [0, 1]. */
4014 CASE_INT_FN (BUILT_IN_PARITY
):
4018 /* __builtin_c[lt]z* return [0, prec-1], except for
4019 when the argument is 0, but that is undefined behavior.
4020 On many targets where the CLZ RTL or optab value is defined
4021 for 0 the value is prec, so include that in the range
4023 CASE_INT_FN (BUILT_IN_CLZ
):
4024 arg
= gimple_call_arg (stmt
, 0);
4025 prec
= TYPE_PRECISION (TREE_TYPE (arg
));
4028 if (optab_handler (clz_optab
, TYPE_MODE (TREE_TYPE (arg
)))
4030 && CLZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (TREE_TYPE (arg
)),
4032 /* Handle only the single common value. */
4034 /* Magic value to give up, unless vr0 proves
4037 if (TREE_CODE (arg
) == SSA_NAME
)
4039 value_range_t
*vr0
= get_value_range (arg
);
4040 /* From clz of VR_RANGE minimum we can compute
4042 if (vr0
->type
== VR_RANGE
4043 && TREE_CODE (vr0
->min
) == INTEGER_CST
4044 && !is_overflow_infinity (vr0
->min
))
4046 maxi
= prec
- 1 - tree_floor_log2 (vr0
->min
);
4050 else if (vr0
->type
== VR_ANTI_RANGE
4051 && integer_zerop (vr0
->min
)
4052 && !is_overflow_infinity (vr0
->min
))
4059 /* From clz of VR_RANGE maximum we can compute
4061 if (vr0
->type
== VR_RANGE
4062 && TREE_CODE (vr0
->max
) == INTEGER_CST
4063 && !is_overflow_infinity (vr0
->max
))
4065 mini
= prec
- 1 - tree_floor_log2 (vr0
->max
);
4073 /* __builtin_ctz* return [0, prec-1], except for
4074 when the argument is 0, but that is undefined behavior.
4075 If there is a ctz optab for this mode and
4076 CTZ_DEFINED_VALUE_AT_ZERO, include that in the range,
4077 otherwise just assume 0 won't be seen. */
4078 CASE_INT_FN (BUILT_IN_CTZ
):
4079 arg
= gimple_call_arg (stmt
, 0);
4080 prec
= TYPE_PRECISION (TREE_TYPE (arg
));
4083 if (optab_handler (ctz_optab
, TYPE_MODE (TREE_TYPE (arg
)))
4085 && CTZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (TREE_TYPE (arg
)),
4088 /* Handle only the two common values. */
4091 else if (zerov
== prec
)
4094 /* Magic value to give up, unless vr0 proves
4098 if (TREE_CODE (arg
) == SSA_NAME
)
4100 value_range_t
*vr0
= get_value_range (arg
);
4101 /* If arg is non-zero, then use [0, prec - 1]. */
4102 if (((vr0
->type
== VR_RANGE
4103 && integer_nonzerop (vr0
->min
))
4104 || (vr0
->type
== VR_ANTI_RANGE
4105 && integer_zerop (vr0
->min
)))
4106 && !is_overflow_infinity (vr0
->min
))
4111 /* If some high bits are known to be zero,
4112 we can decrease the result maximum. */
4113 if (vr0
->type
== VR_RANGE
4114 && TREE_CODE (vr0
->max
) == INTEGER_CST
4115 && !is_overflow_infinity (vr0
->max
))
4117 maxi
= tree_floor_log2 (vr0
->max
);
4118 /* For vr0 [0, 0] give up. */
4126 /* __builtin_clrsb* returns [0, prec-1]. */
4127 CASE_INT_FN (BUILT_IN_CLRSB
):
4128 arg
= gimple_call_arg (stmt
, 0);
4129 prec
= TYPE_PRECISION (TREE_TYPE (arg
));
4134 set_value_range (vr
, VR_RANGE
, build_int_cst (type
, mini
),
4135 build_int_cst (type
, maxi
), NULL
);
4141 else if (is_gimple_call (stmt
) && gimple_call_internal_p (stmt
))
4143 enum tree_code subcode
= ERROR_MARK
;
4144 switch (gimple_call_internal_fn (stmt
))
4146 case IFN_UBSAN_CHECK_ADD
:
4147 subcode
= PLUS_EXPR
;
4149 case IFN_UBSAN_CHECK_SUB
:
4150 subcode
= MINUS_EXPR
;
4152 case IFN_UBSAN_CHECK_MUL
:
4153 subcode
= MULT_EXPR
;
4158 if (subcode
!= ERROR_MARK
)
4160 bool saved_flag_wrapv
= flag_wrapv
;
4161 /* Pretend the arithmetics is wrapping. If there is
4162 any overflow, we'll complain, but will actually do
4163 wrapping operation. */
4165 extract_range_from_binary_expr (vr
, subcode
, type
,
4166 gimple_call_arg (stmt
, 0),
4167 gimple_call_arg (stmt
, 1));
4168 flag_wrapv
= saved_flag_wrapv
;
4170 /* If for both arguments vrp_valueize returned non-NULL,
4171 this should have been already folded and if not, it
4172 wasn't folded because of overflow. Avoid removing the
4173 UBSAN_CHECK_* calls in that case. */
4174 if (vr
->type
== VR_RANGE
4175 && (vr
->min
== vr
->max
4176 || operand_equal_p (vr
->min
, vr
->max
, 0)))
4177 set_value_range_to_varying (vr
);
4181 /* Handle extraction of the two results (result of arithmetics and
4182 a flag whether arithmetics overflowed) from {ADD,SUB,MUL}_OVERFLOW
4183 internal function. */
4184 else if (is_gimple_assign (stmt
)
4185 && (gimple_assign_rhs_code (stmt
) == REALPART_EXPR
4186 || gimple_assign_rhs_code (stmt
) == IMAGPART_EXPR
)
4187 && INTEGRAL_TYPE_P (type
))
4189 enum tree_code code
= gimple_assign_rhs_code (stmt
);
4190 tree op
= gimple_assign_rhs1 (stmt
);
4191 if (TREE_CODE (op
) == code
&& TREE_CODE (TREE_OPERAND (op
, 0)) == SSA_NAME
)
4193 gimple g
= SSA_NAME_DEF_STMT (TREE_OPERAND (op
, 0));
4194 if (is_gimple_call (g
) && gimple_call_internal_p (g
))
4196 enum tree_code subcode
= ERROR_MARK
;
4197 switch (gimple_call_internal_fn (g
))
4199 case IFN_ADD_OVERFLOW
:
4200 subcode
= PLUS_EXPR
;
4202 case IFN_SUB_OVERFLOW
:
4203 subcode
= MINUS_EXPR
;
4205 case IFN_MUL_OVERFLOW
:
4206 subcode
= MULT_EXPR
;
4211 if (subcode
!= ERROR_MARK
)
4213 tree op0
= gimple_call_arg (g
, 0);
4214 tree op1
= gimple_call_arg (g
, 1);
4215 if (code
== IMAGPART_EXPR
)
4218 if (check_for_binary_op_overflow (subcode
, type
,
4220 set_value_range_to_value (vr
,
4221 build_int_cst (type
, ovf
),
4224 set_value_range (vr
, VR_RANGE
, build_int_cst (type
, 0),
4225 build_int_cst (type
, 1), NULL
);
4227 else if (types_compatible_p (type
, TREE_TYPE (op0
))
4228 && types_compatible_p (type
, TREE_TYPE (op1
)))
4230 bool saved_flag_wrapv
= flag_wrapv
;
4231 /* Pretend the arithmetics is wrapping. If there is
4232 any overflow, IMAGPART_EXPR will be set. */
4234 extract_range_from_binary_expr (vr
, subcode
, type
,
4236 flag_wrapv
= saved_flag_wrapv
;
4240 value_range_t vr0
= VR_INITIALIZER
;
4241 value_range_t vr1
= VR_INITIALIZER
;
4242 bool saved_flag_wrapv
= flag_wrapv
;
4243 /* Pretend the arithmetics is wrapping. If there is
4244 any overflow, IMAGPART_EXPR will be set. */
4246 extract_range_from_unary_expr (&vr0
, NOP_EXPR
,
4248 extract_range_from_unary_expr (&vr1
, NOP_EXPR
,
4250 extract_range_from_binary_expr_1 (vr
, subcode
, type
,
4252 flag_wrapv
= saved_flag_wrapv
;
4259 if (INTEGRAL_TYPE_P (type
)
4260 && gimple_stmt_nonnegative_warnv_p (stmt
, &sop
))
4261 set_value_range_to_nonnegative (vr
, type
,
4262 sop
|| stmt_overflow_infinity (stmt
));
4263 else if (vrp_stmt_computes_nonzero (stmt
, &sop
)
4265 set_value_range_to_nonnull (vr
, type
);
4267 set_value_range_to_varying (vr
);
4271 /* Try to compute a useful range out of assignment STMT and store it
4275 extract_range_from_assignment (value_range_t
*vr
, gassign
*stmt
)
4277 enum tree_code code
= gimple_assign_rhs_code (stmt
);
4279 if (code
== ASSERT_EXPR
)
4280 extract_range_from_assert (vr
, gimple_assign_rhs1 (stmt
));
4281 else if (code
== SSA_NAME
)
4282 extract_range_from_ssa_name (vr
, gimple_assign_rhs1 (stmt
));
4283 else if (TREE_CODE_CLASS (code
) == tcc_binary
)
4284 extract_range_from_binary_expr (vr
, gimple_assign_rhs_code (stmt
),
4285 gimple_expr_type (stmt
),
4286 gimple_assign_rhs1 (stmt
),
4287 gimple_assign_rhs2 (stmt
));
4288 else if (TREE_CODE_CLASS (code
) == tcc_unary
)
4289 extract_range_from_unary_expr (vr
, gimple_assign_rhs_code (stmt
),
4290 gimple_expr_type (stmt
),
4291 gimple_assign_rhs1 (stmt
));
4292 else if (code
== COND_EXPR
)
4293 extract_range_from_cond_expr (vr
, stmt
);
4294 else if (TREE_CODE_CLASS (code
) == tcc_comparison
)
4295 extract_range_from_comparison (vr
, gimple_assign_rhs_code (stmt
),
4296 gimple_expr_type (stmt
),
4297 gimple_assign_rhs1 (stmt
),
4298 gimple_assign_rhs2 (stmt
));
4299 else if (get_gimple_rhs_class (code
) == GIMPLE_SINGLE_RHS
4300 && is_gimple_min_invariant (gimple_assign_rhs1 (stmt
)))
4301 set_value_range_to_value (vr
, gimple_assign_rhs1 (stmt
), NULL
);
4303 set_value_range_to_varying (vr
);
4305 if (vr
->type
== VR_VARYING
)
4306 extract_range_basic (vr
, stmt
);
4309 /* Given a range VR, a LOOP and a variable VAR, determine whether it
4310 would be profitable to adjust VR using scalar evolution information
4311 for VAR. If so, update VR with the new limits. */
4314 adjust_range_with_scev (value_range_t
*vr
, struct loop
*loop
,
4315 gimple stmt
, tree var
)
4317 tree init
, step
, chrec
, tmin
, tmax
, min
, max
, type
, tem
;
4318 enum ev_direction dir
;
4320 /* TODO. Don't adjust anti-ranges. An anti-range may provide
4321 better opportunities than a regular range, but I'm not sure. */
4322 if (vr
->type
== VR_ANTI_RANGE
)
4325 chrec
= instantiate_parameters (loop
, analyze_scalar_evolution (loop
, var
));
4327 /* Like in PR19590, scev can return a constant function. */
4328 if (is_gimple_min_invariant (chrec
))
4330 set_value_range_to_value (vr
, chrec
, vr
->equiv
);
4334 if (TREE_CODE (chrec
) != POLYNOMIAL_CHREC
)
4337 init
= initial_condition_in_loop_num (chrec
, loop
->num
);
4338 tem
= op_with_constant_singleton_value_range (init
);
4341 step
= evolution_part_in_loop_num (chrec
, loop
->num
);
4342 tem
= op_with_constant_singleton_value_range (step
);
4346 /* If STEP is symbolic, we can't know whether INIT will be the
4347 minimum or maximum value in the range. Also, unless INIT is
4348 a simple expression, compare_values and possibly other functions
4349 in tree-vrp won't be able to handle it. */
4350 if (step
== NULL_TREE
4351 || !is_gimple_min_invariant (step
)
4352 || !valid_value_p (init
))
4355 dir
= scev_direction (chrec
);
4356 if (/* Do not adjust ranges if we do not know whether the iv increases
4357 or decreases, ... */
4358 dir
== EV_DIR_UNKNOWN
4359 /* ... or if it may wrap. */
4360 || scev_probably_wraps_p (init
, step
, stmt
, get_chrec_loop (chrec
),
4364 /* We use TYPE_MIN_VALUE and TYPE_MAX_VALUE here instead of
4365 negative_overflow_infinity and positive_overflow_infinity,
4366 because we have concluded that the loop probably does not
4369 type
= TREE_TYPE (var
);
4370 if (POINTER_TYPE_P (type
) || !TYPE_MIN_VALUE (type
))
4371 tmin
= lower_bound_in_type (type
, type
);
4373 tmin
= TYPE_MIN_VALUE (type
);
4374 if (POINTER_TYPE_P (type
) || !TYPE_MAX_VALUE (type
))
4375 tmax
= upper_bound_in_type (type
, type
);
4377 tmax
= TYPE_MAX_VALUE (type
);
4379 /* Try to use estimated number of iterations for the loop to constrain the
4380 final value in the evolution. */
4381 if (TREE_CODE (step
) == INTEGER_CST
4382 && is_gimple_val (init
)
4383 && (TREE_CODE (init
) != SSA_NAME
4384 || get_value_range (init
)->type
== VR_RANGE
))
4388 /* We are only entering here for loop header PHI nodes, so using
4389 the number of latch executions is the correct thing to use. */
4390 if (max_loop_iterations (loop
, &nit
))
4392 value_range_t maxvr
= VR_INITIALIZER
;
4393 signop sgn
= TYPE_SIGN (TREE_TYPE (step
));
4396 widest_int wtmp
= wi::mul (wi::to_widest (step
), nit
, sgn
,
4398 /* If the multiplication overflowed we can't do a meaningful
4399 adjustment. Likewise if the result doesn't fit in the type
4400 of the induction variable. For a signed type we have to
4401 check whether the result has the expected signedness which
4402 is that of the step as number of iterations is unsigned. */
4404 && wi::fits_to_tree_p (wtmp
, TREE_TYPE (init
))
4406 || wi::gts_p (wtmp
, 0) == wi::gts_p (step
, 0)))
4408 tem
= wide_int_to_tree (TREE_TYPE (init
), wtmp
);
4409 extract_range_from_binary_expr (&maxvr
, PLUS_EXPR
,
4410 TREE_TYPE (init
), init
, tem
);
4411 /* Likewise if the addition did. */
4412 if (maxvr
.type
== VR_RANGE
)
4421 if (vr
->type
== VR_VARYING
|| vr
->type
== VR_UNDEFINED
)
4426 /* For VARYING or UNDEFINED ranges, just about anything we get
4427 from scalar evolutions should be better. */
4429 if (dir
== EV_DIR_DECREASES
)
4434 else if (vr
->type
== VR_RANGE
)
4439 if (dir
== EV_DIR_DECREASES
)
4441 /* INIT is the maximum value. If INIT is lower than VR->MAX
4442 but no smaller than VR->MIN, set VR->MAX to INIT. */
4443 if (compare_values (init
, max
) == -1)
4446 /* According to the loop information, the variable does not
4447 overflow. If we think it does, probably because of an
4448 overflow due to arithmetic on a different INF value,
4450 if (is_negative_overflow_infinity (min
)
4451 || compare_values (min
, tmin
) == -1)
4457 /* If INIT is bigger than VR->MIN, set VR->MIN to INIT. */
4458 if (compare_values (init
, min
) == 1)
4461 if (is_positive_overflow_infinity (max
)
4462 || compare_values (tmax
, max
) == -1)
4469 /* If we just created an invalid range with the minimum
4470 greater than the maximum, we fail conservatively.
4471 This should happen only in unreachable
4472 parts of code, or for invalid programs. */
4473 if (compare_values (min
, max
) == 1
4474 || (is_negative_overflow_infinity (min
)
4475 && is_positive_overflow_infinity (max
)))
4478 set_value_range (vr
, VR_RANGE
, min
, max
, vr
->equiv
);
4482 /* Given two numeric value ranges VR0, VR1 and a comparison code COMP:
4484 - Return BOOLEAN_TRUE_NODE if VR0 COMP VR1 always returns true for
4485 all the values in the ranges.
4487 - Return BOOLEAN_FALSE_NODE if the comparison always returns false.
4489 - Return NULL_TREE if it is not always possible to determine the
4490 value of the comparison.
4492 Also set *STRICT_OVERFLOW_P to indicate whether a range with an
4493 overflow infinity was used in the test. */
4497 compare_ranges (enum tree_code comp
, value_range_t
*vr0
, value_range_t
*vr1
,
4498 bool *strict_overflow_p
)
4500 /* VARYING or UNDEFINED ranges cannot be compared. */
4501 if (vr0
->type
== VR_VARYING
4502 || vr0
->type
== VR_UNDEFINED
4503 || vr1
->type
== VR_VARYING
4504 || vr1
->type
== VR_UNDEFINED
)
4507 /* Anti-ranges need to be handled separately. */
4508 if (vr0
->type
== VR_ANTI_RANGE
|| vr1
->type
== VR_ANTI_RANGE
)
4510 /* If both are anti-ranges, then we cannot compute any
4512 if (vr0
->type
== VR_ANTI_RANGE
&& vr1
->type
== VR_ANTI_RANGE
)
4515 /* These comparisons are never statically computable. */
4522 /* Equality can be computed only between a range and an
4523 anti-range. ~[VAL1, VAL2] == [VAL1, VAL2] is always false. */
4524 if (vr0
->type
== VR_RANGE
)
4526 /* To simplify processing, make VR0 the anti-range. */
4527 value_range_t
*tmp
= vr0
;
4532 gcc_assert (comp
== NE_EXPR
|| comp
== EQ_EXPR
);
4534 if (compare_values_warnv (vr0
->min
, vr1
->min
, strict_overflow_p
) == 0
4535 && compare_values_warnv (vr0
->max
, vr1
->max
, strict_overflow_p
) == 0)
4536 return (comp
== NE_EXPR
) ? boolean_true_node
: boolean_false_node
;
4541 if (!usable_range_p (vr0
, strict_overflow_p
)
4542 || !usable_range_p (vr1
, strict_overflow_p
))
4545 /* Simplify processing. If COMP is GT_EXPR or GE_EXPR, switch the
4546 operands around and change the comparison code. */
4547 if (comp
== GT_EXPR
|| comp
== GE_EXPR
)
4549 comp
= (comp
== GT_EXPR
) ? LT_EXPR
: LE_EXPR
;
4550 std::swap (vr0
, vr1
);
4553 if (comp
== EQ_EXPR
)
4555 /* Equality may only be computed if both ranges represent
4556 exactly one value. */
4557 if (compare_values_warnv (vr0
->min
, vr0
->max
, strict_overflow_p
) == 0
4558 && compare_values_warnv (vr1
->min
, vr1
->max
, strict_overflow_p
) == 0)
4560 int cmp_min
= compare_values_warnv (vr0
->min
, vr1
->min
,
4562 int cmp_max
= compare_values_warnv (vr0
->max
, vr1
->max
,
4564 if (cmp_min
== 0 && cmp_max
== 0)
4565 return boolean_true_node
;
4566 else if (cmp_min
!= -2 && cmp_max
!= -2)
4567 return boolean_false_node
;
4569 /* If [V0_MIN, V1_MAX] < [V1_MIN, V1_MAX] then V0 != V1. */
4570 else if (compare_values_warnv (vr0
->min
, vr1
->max
,
4571 strict_overflow_p
) == 1
4572 || compare_values_warnv (vr1
->min
, vr0
->max
,
4573 strict_overflow_p
) == 1)
4574 return boolean_false_node
;
4578 else if (comp
== NE_EXPR
)
4582 /* If VR0 is completely to the left or completely to the right
4583 of VR1, they are always different. Notice that we need to
4584 make sure that both comparisons yield similar results to
4585 avoid comparing values that cannot be compared at
4587 cmp1
= compare_values_warnv (vr0
->max
, vr1
->min
, strict_overflow_p
);
4588 cmp2
= compare_values_warnv (vr0
->min
, vr1
->max
, strict_overflow_p
);
4589 if ((cmp1
== -1 && cmp2
== -1) || (cmp1
== 1 && cmp2
== 1))
4590 return boolean_true_node
;
4592 /* If VR0 and VR1 represent a single value and are identical,
4594 else if (compare_values_warnv (vr0
->min
, vr0
->max
,
4595 strict_overflow_p
) == 0
4596 && compare_values_warnv (vr1
->min
, vr1
->max
,
4597 strict_overflow_p
) == 0
4598 && compare_values_warnv (vr0
->min
, vr1
->min
,
4599 strict_overflow_p
) == 0
4600 && compare_values_warnv (vr0
->max
, vr1
->max
,
4601 strict_overflow_p
) == 0)
4602 return boolean_false_node
;
4604 /* Otherwise, they may or may not be different. */
4608 else if (comp
== LT_EXPR
|| comp
== LE_EXPR
)
4612 /* If VR0 is to the left of VR1, return true. */
4613 tst
= compare_values_warnv (vr0
->max
, vr1
->min
, strict_overflow_p
);
4614 if ((comp
== LT_EXPR
&& tst
== -1)
4615 || (comp
== LE_EXPR
&& (tst
== -1 || tst
== 0)))
4617 if (overflow_infinity_range_p (vr0
)
4618 || overflow_infinity_range_p (vr1
))
4619 *strict_overflow_p
= true;
4620 return boolean_true_node
;
4623 /* If VR0 is to the right of VR1, return false. */
4624 tst
= compare_values_warnv (vr0
->min
, vr1
->max
, strict_overflow_p
);
4625 if ((comp
== LT_EXPR
&& (tst
== 0 || tst
== 1))
4626 || (comp
== LE_EXPR
&& tst
== 1))
4628 if (overflow_infinity_range_p (vr0
)
4629 || overflow_infinity_range_p (vr1
))
4630 *strict_overflow_p
= true;
4631 return boolean_false_node
;
4634 /* Otherwise, we don't know. */
4642 /* Given a value range VR, a value VAL and a comparison code COMP, return
4643 BOOLEAN_TRUE_NODE if VR COMP VAL always returns true for all the
4644 values in VR. Return BOOLEAN_FALSE_NODE if the comparison
4645 always returns false. Return NULL_TREE if it is not always
4646 possible to determine the value of the comparison. Also set
4647 *STRICT_OVERFLOW_P to indicate whether a range with an overflow
4648 infinity was used in the test. */
4651 compare_range_with_value (enum tree_code comp
, value_range_t
*vr
, tree val
,
4652 bool *strict_overflow_p
)
4654 if (vr
->type
== VR_VARYING
|| vr
->type
== VR_UNDEFINED
)
4657 /* Anti-ranges need to be handled separately. */
4658 if (vr
->type
== VR_ANTI_RANGE
)
4660 /* For anti-ranges, the only predicates that we can compute at
4661 compile time are equality and inequality. */
4668 /* ~[VAL_1, VAL_2] OP VAL is known if VAL_1 <= VAL <= VAL_2. */
4669 if (value_inside_range (val
, vr
->min
, vr
->max
) == 1)
4670 return (comp
== NE_EXPR
) ? boolean_true_node
: boolean_false_node
;
4675 if (!usable_range_p (vr
, strict_overflow_p
))
4678 if (comp
== EQ_EXPR
)
4680 /* EQ_EXPR may only be computed if VR represents exactly
4682 if (compare_values_warnv (vr
->min
, vr
->max
, strict_overflow_p
) == 0)
4684 int cmp
= compare_values_warnv (vr
->min
, val
, strict_overflow_p
);
4686 return boolean_true_node
;
4687 else if (cmp
== -1 || cmp
== 1 || cmp
== 2)
4688 return boolean_false_node
;
4690 else if (compare_values_warnv (val
, vr
->min
, strict_overflow_p
) == -1
4691 || compare_values_warnv (vr
->max
, val
, strict_overflow_p
) == -1)
4692 return boolean_false_node
;
4696 else if (comp
== NE_EXPR
)
4698 /* If VAL is not inside VR, then they are always different. */
4699 if (compare_values_warnv (vr
->max
, val
, strict_overflow_p
) == -1
4700 || compare_values_warnv (vr
->min
, val
, strict_overflow_p
) == 1)
4701 return boolean_true_node
;
4703 /* If VR represents exactly one value equal to VAL, then return
4705 if (compare_values_warnv (vr
->min
, vr
->max
, strict_overflow_p
) == 0
4706 && compare_values_warnv (vr
->min
, val
, strict_overflow_p
) == 0)
4707 return boolean_false_node
;
4709 /* Otherwise, they may or may not be different. */
4712 else if (comp
== LT_EXPR
|| comp
== LE_EXPR
)
4716 /* If VR is to the left of VAL, return true. */
4717 tst
= compare_values_warnv (vr
->max
, val
, strict_overflow_p
);
4718 if ((comp
== LT_EXPR
&& tst
== -1)
4719 || (comp
== LE_EXPR
&& (tst
== -1 || tst
== 0)))
4721 if (overflow_infinity_range_p (vr
))
4722 *strict_overflow_p
= true;
4723 return boolean_true_node
;
4726 /* If VR is to the right of VAL, return false. */
4727 tst
= compare_values_warnv (vr
->min
, val
, strict_overflow_p
);
4728 if ((comp
== LT_EXPR
&& (tst
== 0 || tst
== 1))
4729 || (comp
== LE_EXPR
&& tst
== 1))
4731 if (overflow_infinity_range_p (vr
))
4732 *strict_overflow_p
= true;
4733 return boolean_false_node
;
4736 /* Otherwise, we don't know. */
4739 else if (comp
== GT_EXPR
|| comp
== GE_EXPR
)
4743 /* If VR is to the right of VAL, return true. */
4744 tst
= compare_values_warnv (vr
->min
, val
, strict_overflow_p
);
4745 if ((comp
== GT_EXPR
&& tst
== 1)
4746 || (comp
== GE_EXPR
&& (tst
== 0 || tst
== 1)))
4748 if (overflow_infinity_range_p (vr
))
4749 *strict_overflow_p
= true;
4750 return boolean_true_node
;
4753 /* If VR is to the left of VAL, return false. */
4754 tst
= compare_values_warnv (vr
->max
, val
, strict_overflow_p
);
4755 if ((comp
== GT_EXPR
&& (tst
== -1 || tst
== 0))
4756 || (comp
== GE_EXPR
&& tst
== -1))
4758 if (overflow_infinity_range_p (vr
))
4759 *strict_overflow_p
= true;
4760 return boolean_false_node
;
4763 /* Otherwise, we don't know. */
4771 /* Debugging dumps. */
4773 void dump_value_range (FILE *, value_range_t
*);
4774 void debug_value_range (value_range_t
*);
4775 void dump_all_value_ranges (FILE *);
4776 void debug_all_value_ranges (void);
4777 void dump_vr_equiv (FILE *, bitmap
);
4778 void debug_vr_equiv (bitmap
);
4781 /* Dump value range VR to FILE. */
4784 dump_value_range (FILE *file
, value_range_t
*vr
)
4787 fprintf (file
, "[]");
4788 else if (vr
->type
== VR_UNDEFINED
)
4789 fprintf (file
, "UNDEFINED");
4790 else if (vr
->type
== VR_RANGE
|| vr
->type
== VR_ANTI_RANGE
)
4792 tree type
= TREE_TYPE (vr
->min
);
4794 fprintf (file
, "%s[", (vr
->type
== VR_ANTI_RANGE
) ? "~" : "");
4796 if (is_negative_overflow_infinity (vr
->min
))
4797 fprintf (file
, "-INF(OVF)");
4798 else if (INTEGRAL_TYPE_P (type
)
4799 && !TYPE_UNSIGNED (type
)
4800 && vrp_val_is_min (vr
->min
))
4801 fprintf (file
, "-INF");
4803 print_generic_expr (file
, vr
->min
, 0);
4805 fprintf (file
, ", ");
4807 if (is_positive_overflow_infinity (vr
->max
))
4808 fprintf (file
, "+INF(OVF)");
4809 else if (INTEGRAL_TYPE_P (type
)
4810 && vrp_val_is_max (vr
->max
))
4811 fprintf (file
, "+INF");
4813 print_generic_expr (file
, vr
->max
, 0);
4815 fprintf (file
, "]");
4822 fprintf (file
, " EQUIVALENCES: { ");
4824 EXECUTE_IF_SET_IN_BITMAP (vr
->equiv
, 0, i
, bi
)
4826 print_generic_expr (file
, ssa_name (i
), 0);
4827 fprintf (file
, " ");
4831 fprintf (file
, "} (%u elements)", c
);
4834 else if (vr
->type
== VR_VARYING
)
4835 fprintf (file
, "VARYING");
4837 fprintf (file
, "INVALID RANGE");
4841 /* Dump value range VR to stderr. */
4844 debug_value_range (value_range_t
*vr
)
4846 dump_value_range (stderr
, vr
);
4847 fprintf (stderr
, "\n");
4851 /* Dump value ranges of all SSA_NAMEs to FILE. */
4854 dump_all_value_ranges (FILE *file
)
4858 for (i
= 0; i
< num_vr_values
; i
++)
4862 print_generic_expr (file
, ssa_name (i
), 0);
4863 fprintf (file
, ": ");
4864 dump_value_range (file
, vr_value
[i
]);
4865 fprintf (file
, "\n");
4869 fprintf (file
, "\n");
4873 /* Dump all value ranges to stderr. */
4876 debug_all_value_ranges (void)
4878 dump_all_value_ranges (stderr
);
4882 /* Given a COND_EXPR COND of the form 'V OP W', and an SSA name V,
4883 create a new SSA name N and return the assertion assignment
4884 'N = ASSERT_EXPR <V, V OP W>'. */
4887 build_assert_expr_for (tree cond
, tree v
)
4892 gcc_assert (TREE_CODE (v
) == SSA_NAME
4893 && COMPARISON_CLASS_P (cond
));
4895 a
= build2 (ASSERT_EXPR
, TREE_TYPE (v
), v
, cond
);
4896 assertion
= gimple_build_assign (NULL_TREE
, a
);
4898 /* The new ASSERT_EXPR, creates a new SSA name that replaces the
4899 operand of the ASSERT_EXPR. Create it so the new name and the old one
4900 are registered in the replacement table so that we can fix the SSA web
4901 after adding all the ASSERT_EXPRs. */
4902 create_new_def_for (v
, assertion
, NULL
);
4908 /* Return false if EXPR is a predicate expression involving floating
4912 fp_predicate (gimple stmt
)
4914 GIMPLE_CHECK (stmt
, GIMPLE_COND
);
4916 return FLOAT_TYPE_P (TREE_TYPE (gimple_cond_lhs (stmt
)));
4919 /* If the range of values taken by OP can be inferred after STMT executes,
4920 return the comparison code (COMP_CODE_P) and value (VAL_P) that
4921 describes the inferred range. Return true if a range could be
4925 infer_value_range (gimple stmt
, tree op
, enum tree_code
*comp_code_p
, tree
*val_p
)
4928 *comp_code_p
= ERROR_MARK
;
4930 /* Do not attempt to infer anything in names that flow through
4932 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op
))
4935 /* Similarly, don't infer anything from statements that may throw
4936 exceptions. ??? Relax this requirement? */
4937 if (stmt_could_throw_p (stmt
))
4940 /* If STMT is the last statement of a basic block with no normal
4941 successors, there is no point inferring anything about any of its
4942 operands. We would not be able to find a proper insertion point
4943 for the assertion, anyway. */
4944 if (stmt_ends_bb_p (stmt
))
4949 FOR_EACH_EDGE (e
, ei
, gimple_bb (stmt
)->succs
)
4950 if (!(e
->flags
& EDGE_ABNORMAL
))
4956 if (infer_nonnull_range (stmt
, op
, true, true))
4958 *val_p
= build_int_cst (TREE_TYPE (op
), 0);
4959 *comp_code_p
= NE_EXPR
;
4967 void dump_asserts_for (FILE *, tree
);
4968 void debug_asserts_for (tree
);
4969 void dump_all_asserts (FILE *);
4970 void debug_all_asserts (void);
4972 /* Dump all the registered assertions for NAME to FILE. */
4975 dump_asserts_for (FILE *file
, tree name
)
4979 fprintf (file
, "Assertions to be inserted for ");
4980 print_generic_expr (file
, name
, 0);
4981 fprintf (file
, "\n");
4983 loc
= asserts_for
[SSA_NAME_VERSION (name
)];
4986 fprintf (file
, "\t");
4987 print_gimple_stmt (file
, gsi_stmt (loc
->si
), 0, 0);
4988 fprintf (file
, "\n\tBB #%d", loc
->bb
->index
);
4991 fprintf (file
, "\n\tEDGE %d->%d", loc
->e
->src
->index
,
4992 loc
->e
->dest
->index
);
4993 dump_edge_info (file
, loc
->e
, dump_flags
, 0);
4995 fprintf (file
, "\n\tPREDICATE: ");
4996 print_generic_expr (file
, name
, 0);
4997 fprintf (file
, " %s ", get_tree_code_name (loc
->comp_code
));
4998 print_generic_expr (file
, loc
->val
, 0);
4999 fprintf (file
, "\n\n");
5003 fprintf (file
, "\n");
5007 /* Dump all the registered assertions for NAME to stderr. */
5010 debug_asserts_for (tree name
)
5012 dump_asserts_for (stderr
, name
);
5016 /* Dump all the registered assertions for all the names to FILE. */
5019 dump_all_asserts (FILE *file
)
5024 fprintf (file
, "\nASSERT_EXPRs to be inserted\n\n");
5025 EXECUTE_IF_SET_IN_BITMAP (need_assert_for
, 0, i
, bi
)
5026 dump_asserts_for (file
, ssa_name (i
));
5027 fprintf (file
, "\n");
5031 /* Dump all the registered assertions for all the names to stderr. */
5034 debug_all_asserts (void)
5036 dump_all_asserts (stderr
);
5040 /* If NAME doesn't have an ASSERT_EXPR registered for asserting
5041 'EXPR COMP_CODE VAL' at a location that dominates block BB or
5042 E->DEST, then register this location as a possible insertion point
5043 for ASSERT_EXPR <NAME, EXPR COMP_CODE VAL>.
5045 BB, E and SI provide the exact insertion point for the new
5046 ASSERT_EXPR. If BB is NULL, then the ASSERT_EXPR is to be inserted
5047 on edge E. Otherwise, if E is NULL, the ASSERT_EXPR is inserted on
5048 BB. If SI points to a COND_EXPR or a SWITCH_EXPR statement, then E
5049 must not be NULL. */
5052 register_new_assert_for (tree name
, tree expr
,
5053 enum tree_code comp_code
,
5057 gimple_stmt_iterator si
)
5059 assert_locus_t n
, loc
, last_loc
;
5060 basic_block dest_bb
;
5062 gcc_checking_assert (bb
== NULL
|| e
== NULL
);
5065 gcc_checking_assert (gimple_code (gsi_stmt (si
)) != GIMPLE_COND
5066 && gimple_code (gsi_stmt (si
)) != GIMPLE_SWITCH
);
5068 /* Never build an assert comparing against an integer constant with
5069 TREE_OVERFLOW set. This confuses our undefined overflow warning
5071 if (TREE_OVERFLOW_P (val
))
5072 val
= drop_tree_overflow (val
);
5074 /* The new assertion A will be inserted at BB or E. We need to
5075 determine if the new location is dominated by a previously
5076 registered location for A. If we are doing an edge insertion,
5077 assume that A will be inserted at E->DEST. Note that this is not
5080 If E is a critical edge, it will be split. But even if E is
5081 split, the new block will dominate the same set of blocks that
5084 The reverse, however, is not true, blocks dominated by E->DEST
5085 will not be dominated by the new block created to split E. So,
5086 if the insertion location is on a critical edge, we will not use
5087 the new location to move another assertion previously registered
5088 at a block dominated by E->DEST. */
5089 dest_bb
= (bb
) ? bb
: e
->dest
;
5091 /* If NAME already has an ASSERT_EXPR registered for COMP_CODE and
5092 VAL at a block dominating DEST_BB, then we don't need to insert a new
5093 one. Similarly, if the same assertion already exists at a block
5094 dominated by DEST_BB and the new location is not on a critical
5095 edge, then update the existing location for the assertion (i.e.,
5096 move the assertion up in the dominance tree).
5098 Note, this is implemented as a simple linked list because there
5099 should not be more than a handful of assertions registered per
5100 name. If this becomes a performance problem, a table hashed by
5101 COMP_CODE and VAL could be implemented. */
5102 loc
= asserts_for
[SSA_NAME_VERSION (name
)];
5106 if (loc
->comp_code
== comp_code
5108 || operand_equal_p (loc
->val
, val
, 0))
5109 && (loc
->expr
== expr
5110 || operand_equal_p (loc
->expr
, expr
, 0)))
5112 /* If E is not a critical edge and DEST_BB
5113 dominates the existing location for the assertion, move
5114 the assertion up in the dominance tree by updating its
5115 location information. */
5116 if ((e
== NULL
|| !EDGE_CRITICAL_P (e
))
5117 && dominated_by_p (CDI_DOMINATORS
, loc
->bb
, dest_bb
))
5126 /* Update the last node of the list and move to the next one. */
5131 /* If we didn't find an assertion already registered for
5132 NAME COMP_CODE VAL, add a new one at the end of the list of
5133 assertions associated with NAME. */
5134 n
= XNEW (struct assert_locus_d
);
5138 n
->comp_code
= comp_code
;
5146 asserts_for
[SSA_NAME_VERSION (name
)] = n
;
5148 bitmap_set_bit (need_assert_for
, SSA_NAME_VERSION (name
));
5151 /* (COND_OP0 COND_CODE COND_OP1) is a predicate which uses NAME.
5152 Extract a suitable test code and value and store them into *CODE_P and
5153 *VAL_P so the predicate is normalized to NAME *CODE_P *VAL_P.
5155 If no extraction was possible, return FALSE, otherwise return TRUE.
5157 If INVERT is true, then we invert the result stored into *CODE_P. */
5160 extract_code_and_val_from_cond_with_ops (tree name
, enum tree_code cond_code
,
5161 tree cond_op0
, tree cond_op1
,
5162 bool invert
, enum tree_code
*code_p
,
5165 enum tree_code comp_code
;
5168 /* Otherwise, we have a comparison of the form NAME COMP VAL
5169 or VAL COMP NAME. */
5170 if (name
== cond_op1
)
5172 /* If the predicate is of the form VAL COMP NAME, flip
5173 COMP around because we need to register NAME as the
5174 first operand in the predicate. */
5175 comp_code
= swap_tree_comparison (cond_code
);
5180 /* The comparison is of the form NAME COMP VAL, so the
5181 comparison code remains unchanged. */
5182 comp_code
= cond_code
;
5186 /* Invert the comparison code as necessary. */
5188 comp_code
= invert_tree_comparison (comp_code
, 0);
5190 /* VRP does not handle float types. */
5191 if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (val
)))
5194 /* Do not register always-false predicates.
5195 FIXME: this works around a limitation in fold() when dealing with
5196 enumerations. Given 'enum { N1, N2 } x;', fold will not
5197 fold 'if (x > N2)' to 'if (0)'. */
5198 if ((comp_code
== GT_EXPR
|| comp_code
== LT_EXPR
)
5199 && INTEGRAL_TYPE_P (TREE_TYPE (val
)))
5201 tree min
= TYPE_MIN_VALUE (TREE_TYPE (val
));
5202 tree max
= TYPE_MAX_VALUE (TREE_TYPE (val
));
5204 if (comp_code
== GT_EXPR
5206 || compare_values (val
, max
) == 0))
5209 if (comp_code
== LT_EXPR
5211 || compare_values (val
, min
) == 0))
5214 *code_p
= comp_code
;
5219 /* Find out smallest RES where RES > VAL && (RES & MASK) == RES, if any
5220 (otherwise return VAL). VAL and MASK must be zero-extended for
5221 precision PREC. If SGNBIT is non-zero, first xor VAL with SGNBIT
5222 (to transform signed values into unsigned) and at the end xor
5226 masked_increment (const wide_int
&val_in
, const wide_int
&mask
,
5227 const wide_int
&sgnbit
, unsigned int prec
)
5229 wide_int bit
= wi::one (prec
), res
;
5232 wide_int val
= val_in
^ sgnbit
;
5233 for (i
= 0; i
< prec
; i
++, bit
+= bit
)
5236 if ((res
& bit
) == 0)
5239 res
= (val
+ bit
).and_not (res
);
5241 if (wi::gtu_p (res
, val
))
5242 return res
^ sgnbit
;
5244 return val
^ sgnbit
;
5247 /* Try to register an edge assertion for SSA name NAME on edge E for
5248 the condition COND contributing to the conditional jump pointed to by BSI.
5249 Invert the condition COND if INVERT is true. */
5252 register_edge_assert_for_2 (tree name
, edge e
, gimple_stmt_iterator bsi
,
5253 enum tree_code cond_code
,
5254 tree cond_op0
, tree cond_op1
, bool invert
)
5257 enum tree_code comp_code
;
5259 if (!extract_code_and_val_from_cond_with_ops (name
, cond_code
,
5262 invert
, &comp_code
, &val
))
5265 /* Only register an ASSERT_EXPR if NAME was found in the sub-graph
5266 reachable from E. */
5267 if (live_on_edge (e
, name
)
5268 && !has_single_use (name
))
5269 register_new_assert_for (name
, name
, comp_code
, val
, NULL
, e
, bsi
);
5271 /* In the case of NAME <= CST and NAME being defined as
5272 NAME = (unsigned) NAME2 + CST2 we can assert NAME2 >= -CST2
5273 and NAME2 <= CST - CST2. We can do the same for NAME > CST.
5274 This catches range and anti-range tests. */
5275 if ((comp_code
== LE_EXPR
5276 || comp_code
== GT_EXPR
)
5277 && TREE_CODE (val
) == INTEGER_CST
5278 && TYPE_UNSIGNED (TREE_TYPE (val
)))
5280 gimple def_stmt
= SSA_NAME_DEF_STMT (name
);
5281 tree cst2
= NULL_TREE
, name2
= NULL_TREE
, name3
= NULL_TREE
;
5283 /* Extract CST2 from the (optional) addition. */
5284 if (is_gimple_assign (def_stmt
)
5285 && gimple_assign_rhs_code (def_stmt
) == PLUS_EXPR
)
5287 name2
= gimple_assign_rhs1 (def_stmt
);
5288 cst2
= gimple_assign_rhs2 (def_stmt
);
5289 if (TREE_CODE (name2
) == SSA_NAME
5290 && TREE_CODE (cst2
) == INTEGER_CST
)
5291 def_stmt
= SSA_NAME_DEF_STMT (name2
);
5294 /* Extract NAME2 from the (optional) sign-changing cast. */
5295 if (gimple_assign_cast_p (def_stmt
))
5297 if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt
))
5298 && ! TYPE_UNSIGNED (TREE_TYPE (gimple_assign_rhs1 (def_stmt
)))
5299 && (TYPE_PRECISION (gimple_expr_type (def_stmt
))
5300 == TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt
)))))
5301 name3
= gimple_assign_rhs1 (def_stmt
);
5304 /* If name3 is used later, create an ASSERT_EXPR for it. */
5305 if (name3
!= NULL_TREE
5306 && TREE_CODE (name3
) == SSA_NAME
5307 && (cst2
== NULL_TREE
5308 || TREE_CODE (cst2
) == INTEGER_CST
)
5309 && INTEGRAL_TYPE_P (TREE_TYPE (name3
))
5310 && live_on_edge (e
, name3
)
5311 && !has_single_use (name3
))
5315 /* Build an expression for the range test. */
5316 tmp
= build1 (NOP_EXPR
, TREE_TYPE (name
), name3
);
5317 if (cst2
!= NULL_TREE
)
5318 tmp
= build2 (PLUS_EXPR
, TREE_TYPE (name
), tmp
, cst2
);
5322 fprintf (dump_file
, "Adding assert for ");
5323 print_generic_expr (dump_file
, name3
, 0);
5324 fprintf (dump_file
, " from ");
5325 print_generic_expr (dump_file
, tmp
, 0);
5326 fprintf (dump_file
, "\n");
5329 register_new_assert_for (name3
, tmp
, comp_code
, val
, NULL
, e
, bsi
);
5332 /* If name2 is used later, create an ASSERT_EXPR for it. */
5333 if (name2
!= NULL_TREE
5334 && TREE_CODE (name2
) == SSA_NAME
5335 && TREE_CODE (cst2
) == INTEGER_CST
5336 && INTEGRAL_TYPE_P (TREE_TYPE (name2
))
5337 && live_on_edge (e
, name2
)
5338 && !has_single_use (name2
))
5342 /* Build an expression for the range test. */
5344 if (TREE_TYPE (name
) != TREE_TYPE (name2
))
5345 tmp
= build1 (NOP_EXPR
, TREE_TYPE (name
), tmp
);
5346 if (cst2
!= NULL_TREE
)
5347 tmp
= build2 (PLUS_EXPR
, TREE_TYPE (name
), tmp
, cst2
);
5351 fprintf (dump_file
, "Adding assert for ");
5352 print_generic_expr (dump_file
, name2
, 0);
5353 fprintf (dump_file
, " from ");
5354 print_generic_expr (dump_file
, tmp
, 0);
5355 fprintf (dump_file
, "\n");
5358 register_new_assert_for (name2
, tmp
, comp_code
, val
, NULL
, e
, bsi
);
5362 /* In the case of post-in/decrement tests like if (i++) ... and uses
5363 of the in/decremented value on the edge the extra name we want to
5364 assert for is not on the def chain of the name compared. Instead
5365 it is in the set of use stmts. */
5366 if ((comp_code
== NE_EXPR
5367 || comp_code
== EQ_EXPR
)
5368 && TREE_CODE (val
) == INTEGER_CST
)
5370 imm_use_iterator ui
;
5372 FOR_EACH_IMM_USE_STMT (use_stmt
, ui
, name
)
5374 /* Cut off to use-stmts that are in the predecessor. */
5375 if (gimple_bb (use_stmt
) != e
->src
)
5378 if (!is_gimple_assign (use_stmt
))
5381 enum tree_code code
= gimple_assign_rhs_code (use_stmt
);
5382 if (code
!= PLUS_EXPR
5383 && code
!= MINUS_EXPR
)
5386 tree cst
= gimple_assign_rhs2 (use_stmt
);
5387 if (TREE_CODE (cst
) != INTEGER_CST
)
5390 tree name2
= gimple_assign_lhs (use_stmt
);
5391 if (live_on_edge (e
, name2
))
5393 cst
= int_const_binop (code
, val
, cst
);
5394 register_new_assert_for (name2
, name2
, comp_code
, cst
,
5400 if (TREE_CODE_CLASS (comp_code
) == tcc_comparison
5401 && TREE_CODE (val
) == INTEGER_CST
)
5403 gimple def_stmt
= SSA_NAME_DEF_STMT (name
);
5404 tree name2
= NULL_TREE
, names
[2], cst2
= NULL_TREE
;
5405 tree val2
= NULL_TREE
;
5406 unsigned int prec
= TYPE_PRECISION (TREE_TYPE (val
));
5407 wide_int mask
= wi::zero (prec
);
5408 unsigned int nprec
= prec
;
5409 enum tree_code rhs_code
= ERROR_MARK
;
5411 if (is_gimple_assign (def_stmt
))
5412 rhs_code
= gimple_assign_rhs_code (def_stmt
);
5414 /* Add asserts for NAME cmp CST and NAME being defined
5415 as NAME = (int) NAME2. */
5416 if (!TYPE_UNSIGNED (TREE_TYPE (val
))
5417 && (comp_code
== LE_EXPR
|| comp_code
== LT_EXPR
5418 || comp_code
== GT_EXPR
|| comp_code
== GE_EXPR
)
5419 && gimple_assign_cast_p (def_stmt
))
5421 name2
= gimple_assign_rhs1 (def_stmt
);
5422 if (CONVERT_EXPR_CODE_P (rhs_code
)
5423 && INTEGRAL_TYPE_P (TREE_TYPE (name2
))
5424 && TYPE_UNSIGNED (TREE_TYPE (name2
))
5425 && prec
== TYPE_PRECISION (TREE_TYPE (name2
))
5426 && (comp_code
== LE_EXPR
|| comp_code
== GT_EXPR
5427 || !tree_int_cst_equal (val
,
5428 TYPE_MIN_VALUE (TREE_TYPE (val
))))
5429 && live_on_edge (e
, name2
)
5430 && !has_single_use (name2
))
5433 enum tree_code new_comp_code
= comp_code
;
5435 cst
= fold_convert (TREE_TYPE (name2
),
5436 TYPE_MIN_VALUE (TREE_TYPE (val
)));
5437 /* Build an expression for the range test. */
5438 tmp
= build2 (PLUS_EXPR
, TREE_TYPE (name2
), name2
, cst
);
5439 cst
= fold_build2 (PLUS_EXPR
, TREE_TYPE (name2
), cst
,
5440 fold_convert (TREE_TYPE (name2
), val
));
5441 if (comp_code
== LT_EXPR
|| comp_code
== GE_EXPR
)
5443 new_comp_code
= comp_code
== LT_EXPR
? LE_EXPR
: GT_EXPR
;
5444 cst
= fold_build2 (MINUS_EXPR
, TREE_TYPE (name2
), cst
,
5445 build_int_cst (TREE_TYPE (name2
), 1));
5450 fprintf (dump_file
, "Adding assert for ");
5451 print_generic_expr (dump_file
, name2
, 0);
5452 fprintf (dump_file
, " from ");
5453 print_generic_expr (dump_file
, tmp
, 0);
5454 fprintf (dump_file
, "\n");
5457 register_new_assert_for (name2
, tmp
, new_comp_code
, cst
, NULL
,
5462 /* Add asserts for NAME cmp CST and NAME being defined as
5463 NAME = NAME2 >> CST2.
5465 Extract CST2 from the right shift. */
5466 if (rhs_code
== RSHIFT_EXPR
)
5468 name2
= gimple_assign_rhs1 (def_stmt
);
5469 cst2
= gimple_assign_rhs2 (def_stmt
);
5470 if (TREE_CODE (name2
) == SSA_NAME
5471 && tree_fits_uhwi_p (cst2
)
5472 && INTEGRAL_TYPE_P (TREE_TYPE (name2
))
5473 && IN_RANGE (tree_to_uhwi (cst2
), 1, prec
- 1)
5474 && prec
== GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (val
)))
5475 && live_on_edge (e
, name2
)
5476 && !has_single_use (name2
))
5478 mask
= wi::mask (tree_to_uhwi (cst2
), false, prec
);
5479 val2
= fold_binary (LSHIFT_EXPR
, TREE_TYPE (val
), val
, cst2
);
5482 if (val2
!= NULL_TREE
5483 && TREE_CODE (val2
) == INTEGER_CST
5484 && simple_cst_equal (fold_build2 (RSHIFT_EXPR
,
5488 enum tree_code new_comp_code
= comp_code
;
5492 if (comp_code
== EQ_EXPR
|| comp_code
== NE_EXPR
)
5494 if (!TYPE_UNSIGNED (TREE_TYPE (val
)))
5496 tree type
= build_nonstandard_integer_type (prec
, 1);
5497 tmp
= build1 (NOP_EXPR
, type
, name2
);
5498 val2
= fold_convert (type
, val2
);
5500 tmp
= fold_build2 (MINUS_EXPR
, TREE_TYPE (tmp
), tmp
, val2
);
5501 new_val
= wide_int_to_tree (TREE_TYPE (tmp
), mask
);
5502 new_comp_code
= comp_code
== EQ_EXPR
? LE_EXPR
: GT_EXPR
;
5504 else if (comp_code
== LT_EXPR
|| comp_code
== GE_EXPR
)
5507 = wi::min_value (prec
, TYPE_SIGN (TREE_TYPE (val
)));
5509 if (minval
== new_val
)
5510 new_val
= NULL_TREE
;
5515 = wi::max_value (prec
, TYPE_SIGN (TREE_TYPE (val
)));
5518 new_val
= NULL_TREE
;
5520 new_val
= wide_int_to_tree (TREE_TYPE (val2
), mask
);
5527 fprintf (dump_file
, "Adding assert for ");
5528 print_generic_expr (dump_file
, name2
, 0);
5529 fprintf (dump_file
, " from ");
5530 print_generic_expr (dump_file
, tmp
, 0);
5531 fprintf (dump_file
, "\n");
5534 register_new_assert_for (name2
, tmp
, new_comp_code
, new_val
,
5539 /* Add asserts for NAME cmp CST and NAME being defined as
5540 NAME = NAME2 & CST2.
5542 Extract CST2 from the and.
5545 NAME = (unsigned) NAME2;
5546 casts where NAME's type is unsigned and has smaller precision
5547 than NAME2's type as if it was NAME = NAME2 & MASK. */
5548 names
[0] = NULL_TREE
;
5549 names
[1] = NULL_TREE
;
5551 if (rhs_code
== BIT_AND_EXPR
5552 || (CONVERT_EXPR_CODE_P (rhs_code
)
5553 && TREE_CODE (TREE_TYPE (val
)) == INTEGER_TYPE
5554 && TYPE_UNSIGNED (TREE_TYPE (val
))
5555 && TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt
)))
5558 name2
= gimple_assign_rhs1 (def_stmt
);
5559 if (rhs_code
== BIT_AND_EXPR
)
5560 cst2
= gimple_assign_rhs2 (def_stmt
);
5563 cst2
= TYPE_MAX_VALUE (TREE_TYPE (val
));
5564 nprec
= TYPE_PRECISION (TREE_TYPE (name2
));
5566 if (TREE_CODE (name2
) == SSA_NAME
5567 && INTEGRAL_TYPE_P (TREE_TYPE (name2
))
5568 && TREE_CODE (cst2
) == INTEGER_CST
5569 && !integer_zerop (cst2
)
5571 || TYPE_UNSIGNED (TREE_TYPE (val
))))
5573 gimple def_stmt2
= SSA_NAME_DEF_STMT (name2
);
5574 if (gimple_assign_cast_p (def_stmt2
))
5576 names
[1] = gimple_assign_rhs1 (def_stmt2
);
5577 if (!CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt2
))
5578 || !INTEGRAL_TYPE_P (TREE_TYPE (names
[1]))
5579 || (TYPE_PRECISION (TREE_TYPE (name2
))
5580 != TYPE_PRECISION (TREE_TYPE (names
[1])))
5581 || !live_on_edge (e
, names
[1])
5582 || has_single_use (names
[1]))
5583 names
[1] = NULL_TREE
;
5585 if (live_on_edge (e
, name2
)
5586 && !has_single_use (name2
))
5590 if (names
[0] || names
[1])
5592 wide_int minv
, maxv
, valv
, cst2v
;
5593 wide_int tem
, sgnbit
;
5594 bool valid_p
= false, valn
, cst2n
;
5595 enum tree_code ccode
= comp_code
;
5597 valv
= wide_int::from (val
, nprec
, UNSIGNED
);
5598 cst2v
= wide_int::from (cst2
, nprec
, UNSIGNED
);
5599 valn
= wi::neg_p (valv
, TYPE_SIGN (TREE_TYPE (val
)));
5600 cst2n
= wi::neg_p (cst2v
, TYPE_SIGN (TREE_TYPE (val
)));
5601 /* If CST2 doesn't have most significant bit set,
5602 but VAL is negative, we have comparison like
5603 if ((x & 0x123) > -4) (always true). Just give up. */
5607 sgnbit
= wi::set_bit_in_zero (nprec
- 1, nprec
);
5609 sgnbit
= wi::zero (nprec
);
5610 minv
= valv
& cst2v
;
5614 /* Minimum unsigned value for equality is VAL & CST2
5615 (should be equal to VAL, otherwise we probably should
5616 have folded the comparison into false) and
5617 maximum unsigned value is VAL | ~CST2. */
5618 maxv
= valv
| ~cst2v
;
5623 tem
= valv
| ~cst2v
;
5624 /* If VAL is 0, handle (X & CST2) != 0 as (X & CST2) > 0U. */
5628 sgnbit
= wi::zero (nprec
);
5631 /* If (VAL | ~CST2) is all ones, handle it as
5632 (X & CST2) < VAL. */
5637 sgnbit
= wi::zero (nprec
);
5640 if (!cst2n
&& wi::neg_p (cst2v
))
5641 sgnbit
= wi::set_bit_in_zero (nprec
- 1, nprec
);
5650 if (tem
== wi::mask (nprec
- 1, false, nprec
))
5656 sgnbit
= wi::zero (nprec
);
5661 /* Minimum unsigned value for >= if (VAL & CST2) == VAL
5662 is VAL and maximum unsigned value is ~0. For signed
5663 comparison, if CST2 doesn't have most significant bit
5664 set, handle it similarly. If CST2 has MSB set,
5665 the minimum is the same, and maximum is ~0U/2. */
5668 /* If (VAL & CST2) != VAL, X & CST2 can't be equal to
5670 minv
= masked_increment (valv
, cst2v
, sgnbit
, nprec
);
5674 maxv
= wi::mask (nprec
- (cst2n
? 1 : 0), false, nprec
);
5680 /* Find out smallest MINV where MINV > VAL
5681 && (MINV & CST2) == MINV, if any. If VAL is signed and
5682 CST2 has MSB set, compute it biased by 1 << (nprec - 1). */
5683 minv
= masked_increment (valv
, cst2v
, sgnbit
, nprec
);
5686 maxv
= wi::mask (nprec
- (cst2n
? 1 : 0), false, nprec
);
5691 /* Minimum unsigned value for <= is 0 and maximum
5692 unsigned value is VAL | ~CST2 if (VAL & CST2) == VAL.
5693 Otherwise, find smallest VAL2 where VAL2 > VAL
5694 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
5696 For signed comparison, if CST2 doesn't have most
5697 significant bit set, handle it similarly. If CST2 has
5698 MSB set, the maximum is the same and minimum is INT_MIN. */
5703 maxv
= masked_increment (valv
, cst2v
, sgnbit
, nprec
);
5715 /* Minimum unsigned value for < is 0 and maximum
5716 unsigned value is (VAL-1) | ~CST2 if (VAL & CST2) == VAL.
5717 Otherwise, find smallest VAL2 where VAL2 > VAL
5718 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
5720 For signed comparison, if CST2 doesn't have most
5721 significant bit set, handle it similarly. If CST2 has
5722 MSB set, the maximum is the same and minimum is INT_MIN. */
5731 maxv
= masked_increment (valv
, cst2v
, sgnbit
, nprec
);
5745 && (maxv
- minv
) != -1)
5747 tree tmp
, new_val
, type
;
5750 for (i
= 0; i
< 2; i
++)
5753 wide_int maxv2
= maxv
;
5755 type
= TREE_TYPE (names
[i
]);
5756 if (!TYPE_UNSIGNED (type
))
5758 type
= build_nonstandard_integer_type (nprec
, 1);
5759 tmp
= build1 (NOP_EXPR
, type
, names
[i
]);
5763 tmp
= build2 (PLUS_EXPR
, type
, tmp
,
5764 wide_int_to_tree (type
, -minv
));
5765 maxv2
= maxv
- minv
;
5767 new_val
= wide_int_to_tree (type
, maxv2
);
5771 fprintf (dump_file
, "Adding assert for ");
5772 print_generic_expr (dump_file
, names
[i
], 0);
5773 fprintf (dump_file
, " from ");
5774 print_generic_expr (dump_file
, tmp
, 0);
5775 fprintf (dump_file
, "\n");
5778 register_new_assert_for (names
[i
], tmp
, LE_EXPR
,
5779 new_val
, NULL
, e
, bsi
);
5786 /* OP is an operand of a truth value expression which is known to have
5787 a particular value. Register any asserts for OP and for any
5788 operands in OP's defining statement.
5790 If CODE is EQ_EXPR, then we want to register OP is zero (false),
5791 if CODE is NE_EXPR, then we want to register OP is nonzero (true). */
5794 register_edge_assert_for_1 (tree op
, enum tree_code code
,
5795 edge e
, gimple_stmt_iterator bsi
)
5799 enum tree_code rhs_code
;
5801 /* We only care about SSA_NAMEs. */
5802 if (TREE_CODE (op
) != SSA_NAME
)
5805 /* We know that OP will have a zero or nonzero value. If OP is used
5806 more than once go ahead and register an assert for OP. */
5807 if (live_on_edge (e
, op
)
5808 && !has_single_use (op
))
5810 val
= build_int_cst (TREE_TYPE (op
), 0);
5811 register_new_assert_for (op
, op
, code
, val
, NULL
, e
, bsi
);
5814 /* Now look at how OP is set. If it's set from a comparison,
5815 a truth operation or some bit operations, then we may be able
5816 to register information about the operands of that assignment. */
5817 op_def
= SSA_NAME_DEF_STMT (op
);
5818 if (gimple_code (op_def
) != GIMPLE_ASSIGN
)
5821 rhs_code
= gimple_assign_rhs_code (op_def
);
5823 if (TREE_CODE_CLASS (rhs_code
) == tcc_comparison
)
5825 bool invert
= (code
== EQ_EXPR
? true : false);
5826 tree op0
= gimple_assign_rhs1 (op_def
);
5827 tree op1
= gimple_assign_rhs2 (op_def
);
5829 if (TREE_CODE (op0
) == SSA_NAME
)
5830 register_edge_assert_for_2 (op0
, e
, bsi
, rhs_code
, op0
, op1
, invert
);
5831 if (TREE_CODE (op1
) == SSA_NAME
)
5832 register_edge_assert_for_2 (op1
, e
, bsi
, rhs_code
, op0
, op1
, invert
);
5834 else if ((code
== NE_EXPR
5835 && gimple_assign_rhs_code (op_def
) == BIT_AND_EXPR
)
5837 && gimple_assign_rhs_code (op_def
) == BIT_IOR_EXPR
))
5839 /* Recurse on each operand. */
5840 tree op0
= gimple_assign_rhs1 (op_def
);
5841 tree op1
= gimple_assign_rhs2 (op_def
);
5842 if (TREE_CODE (op0
) == SSA_NAME
5843 && has_single_use (op0
))
5844 register_edge_assert_for_1 (op0
, code
, e
, bsi
);
5845 if (TREE_CODE (op1
) == SSA_NAME
5846 && has_single_use (op1
))
5847 register_edge_assert_for_1 (op1
, code
, e
, bsi
);
5849 else if (gimple_assign_rhs_code (op_def
) == BIT_NOT_EXPR
5850 && TYPE_PRECISION (TREE_TYPE (gimple_assign_lhs (op_def
))) == 1)
5852 /* Recurse, flipping CODE. */
5853 code
= invert_tree_comparison (code
, false);
5854 register_edge_assert_for_1 (gimple_assign_rhs1 (op_def
), code
, e
, bsi
);
5856 else if (gimple_assign_rhs_code (op_def
) == SSA_NAME
)
5858 /* Recurse through the copy. */
5859 register_edge_assert_for_1 (gimple_assign_rhs1 (op_def
), code
, e
, bsi
);
5861 else if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (op_def
)))
5863 /* Recurse through the type conversion, unless it is a narrowing
5864 conversion or conversion from non-integral type. */
5865 tree rhs
= gimple_assign_rhs1 (op_def
);
5866 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs
))
5867 && (TYPE_PRECISION (TREE_TYPE (rhs
))
5868 <= TYPE_PRECISION (TREE_TYPE (op
))))
5869 register_edge_assert_for_1 (rhs
, code
, e
, bsi
);
5873 /* Try to register an edge assertion for SSA name NAME on edge E for
5874 the condition COND contributing to the conditional jump pointed to by
5878 register_edge_assert_for (tree name
, edge e
, gimple_stmt_iterator si
,
5879 enum tree_code cond_code
, tree cond_op0
,
5883 enum tree_code comp_code
;
5884 bool is_else_edge
= (e
->flags
& EDGE_FALSE_VALUE
) != 0;
5886 /* Do not attempt to infer anything in names that flow through
5888 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name
))
5891 if (!extract_code_and_val_from_cond_with_ops (name
, cond_code
,
5897 /* Register ASSERT_EXPRs for name. */
5898 register_edge_assert_for_2 (name
, e
, si
, cond_code
, cond_op0
,
5899 cond_op1
, is_else_edge
);
5902 /* If COND is effectively an equality test of an SSA_NAME against
5903 the value zero or one, then we may be able to assert values
5904 for SSA_NAMEs which flow into COND. */
5906 /* In the case of NAME == 1 or NAME != 0, for BIT_AND_EXPR defining
5907 statement of NAME we can assert both operands of the BIT_AND_EXPR
5908 have nonzero value. */
5909 if (((comp_code
== EQ_EXPR
&& integer_onep (val
))
5910 || (comp_code
== NE_EXPR
&& integer_zerop (val
))))
5912 gimple def_stmt
= SSA_NAME_DEF_STMT (name
);
5914 if (is_gimple_assign (def_stmt
)
5915 && gimple_assign_rhs_code (def_stmt
) == BIT_AND_EXPR
)
5917 tree op0
= gimple_assign_rhs1 (def_stmt
);
5918 tree op1
= gimple_assign_rhs2 (def_stmt
);
5919 register_edge_assert_for_1 (op0
, NE_EXPR
, e
, si
);
5920 register_edge_assert_for_1 (op1
, NE_EXPR
, e
, si
);
5924 /* In the case of NAME == 0 or NAME != 1, for BIT_IOR_EXPR defining
5925 statement of NAME we can assert both operands of the BIT_IOR_EXPR
5927 if (((comp_code
== EQ_EXPR
&& integer_zerop (val
))
5928 || (comp_code
== NE_EXPR
&& integer_onep (val
))))
5930 gimple def_stmt
= SSA_NAME_DEF_STMT (name
);
5932 /* For BIT_IOR_EXPR only if NAME == 0 both operands have
5933 necessarily zero value, or if type-precision is one. */
5934 if (is_gimple_assign (def_stmt
)
5935 && (gimple_assign_rhs_code (def_stmt
) == BIT_IOR_EXPR
5936 && (TYPE_PRECISION (TREE_TYPE (name
)) == 1
5937 || comp_code
== EQ_EXPR
)))
5939 tree op0
= gimple_assign_rhs1 (def_stmt
);
5940 tree op1
= gimple_assign_rhs2 (def_stmt
);
5941 register_edge_assert_for_1 (op0
, EQ_EXPR
, e
, si
);
5942 register_edge_assert_for_1 (op1
, EQ_EXPR
, e
, si
);
5948 /* Determine whether the outgoing edges of BB should receive an
5949 ASSERT_EXPR for each of the operands of BB's LAST statement.
5950 The last statement of BB must be a COND_EXPR.
5952 If any of the sub-graphs rooted at BB have an interesting use of
5953 the predicate operands, an assert location node is added to the
5954 list of assertions for the corresponding operands. */
5957 find_conditional_asserts (basic_block bb
, gcond
*last
)
5959 gimple_stmt_iterator bsi
;
5965 bsi
= gsi_for_stmt (last
);
5967 /* Look for uses of the operands in each of the sub-graphs
5968 rooted at BB. We need to check each of the outgoing edges
5969 separately, so that we know what kind of ASSERT_EXPR to
5971 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
5976 /* Register the necessary assertions for each operand in the
5977 conditional predicate. */
5978 FOR_EACH_SSA_TREE_OPERAND (op
, last
, iter
, SSA_OP_USE
)
5979 register_edge_assert_for (op
, e
, bsi
,
5980 gimple_cond_code (last
),
5981 gimple_cond_lhs (last
),
5982 gimple_cond_rhs (last
));
5992 /* Compare two case labels sorting first by the destination bb index
5993 and then by the case value. */
5996 compare_case_labels (const void *p1
, const void *p2
)
5998 const struct case_info
*ci1
= (const struct case_info
*) p1
;
5999 const struct case_info
*ci2
= (const struct case_info
*) p2
;
6000 int idx1
= ci1
->bb
->index
;
6001 int idx2
= ci2
->bb
->index
;
6005 else if (idx1
== idx2
)
6007 /* Make sure the default label is first in a group. */
6008 if (!CASE_LOW (ci1
->expr
))
6010 else if (!CASE_LOW (ci2
->expr
))
6013 return tree_int_cst_compare (CASE_LOW (ci1
->expr
),
6014 CASE_LOW (ci2
->expr
));
6020 /* Determine whether the outgoing edges of BB should receive an
6021 ASSERT_EXPR for each of the operands of BB's LAST statement.
6022 The last statement of BB must be a SWITCH_EXPR.
6024 If any of the sub-graphs rooted at BB have an interesting use of
6025 the predicate operands, an assert location node is added to the
6026 list of assertions for the corresponding operands. */
6029 find_switch_asserts (basic_block bb
, gswitch
*last
)
6031 gimple_stmt_iterator bsi
;
6034 struct case_info
*ci
;
6035 size_t n
= gimple_switch_num_labels (last
);
6036 #if GCC_VERSION >= 4000
6039 /* Work around GCC 3.4 bug (PR 37086). */
6040 volatile unsigned int idx
;
6043 bsi
= gsi_for_stmt (last
);
6044 op
= gimple_switch_index (last
);
6045 if (TREE_CODE (op
) != SSA_NAME
)
6048 /* Build a vector of case labels sorted by destination label. */
6049 ci
= XNEWVEC (struct case_info
, n
);
6050 for (idx
= 0; idx
< n
; ++idx
)
6052 ci
[idx
].expr
= gimple_switch_label (last
, idx
);
6053 ci
[idx
].bb
= label_to_block (CASE_LABEL (ci
[idx
].expr
));
6055 qsort (ci
, n
, sizeof (struct case_info
), compare_case_labels
);
6057 for (idx
= 0; idx
< n
; ++idx
)
6060 tree cl
= ci
[idx
].expr
;
6061 basic_block cbb
= ci
[idx
].bb
;
6063 min
= CASE_LOW (cl
);
6064 max
= CASE_HIGH (cl
);
6066 /* If there are multiple case labels with the same destination
6067 we need to combine them to a single value range for the edge. */
6068 if (idx
+ 1 < n
&& cbb
== ci
[idx
+ 1].bb
)
6070 /* Skip labels until the last of the group. */
6073 } while (idx
< n
&& cbb
== ci
[idx
].bb
);
6076 /* Pick up the maximum of the case label range. */
6077 if (CASE_HIGH (ci
[idx
].expr
))
6078 max
= CASE_HIGH (ci
[idx
].expr
);
6080 max
= CASE_LOW (ci
[idx
].expr
);
6083 /* Nothing to do if the range includes the default label until we
6084 can register anti-ranges. */
6085 if (min
== NULL_TREE
)
6088 /* Find the edge to register the assert expr on. */
6089 e
= find_edge (bb
, cbb
);
6091 /* Register the necessary assertions for the operand in the
6093 register_edge_assert_for (op
, e
, bsi
,
6094 max
? GE_EXPR
: EQ_EXPR
,
6095 op
, fold_convert (TREE_TYPE (op
), min
));
6097 register_edge_assert_for (op
, e
, bsi
, LE_EXPR
, op
,
6098 fold_convert (TREE_TYPE (op
), max
));
6105 /* Traverse all the statements in block BB looking for statements that
6106 may generate useful assertions for the SSA names in their operand.
6107 If a statement produces a useful assertion A for name N_i, then the
6108 list of assertions already generated for N_i is scanned to
6109 determine if A is actually needed.
6111 If N_i already had the assertion A at a location dominating the
6112 current location, then nothing needs to be done. Otherwise, the
6113 new location for A is recorded instead.
6115 1- For every statement S in BB, all the variables used by S are
6116 added to bitmap FOUND_IN_SUBGRAPH.
6118 2- If statement S uses an operand N in a way that exposes a known
6119 value range for N, then if N was not already generated by an
6120 ASSERT_EXPR, create a new assert location for N. For instance,
6121 if N is a pointer and the statement dereferences it, we can
6122 assume that N is not NULL.
6124 3- COND_EXPRs are a special case of #2. We can derive range
6125 information from the predicate but need to insert different
6126 ASSERT_EXPRs for each of the sub-graphs rooted at the
6127 conditional block. If the last statement of BB is a conditional
6128 expression of the form 'X op Y', then
6130 a) Remove X and Y from the set FOUND_IN_SUBGRAPH.
6132 b) If the conditional is the only entry point to the sub-graph
6133 corresponding to the THEN_CLAUSE, recurse into it. On
6134 return, if X and/or Y are marked in FOUND_IN_SUBGRAPH, then
6135 an ASSERT_EXPR is added for the corresponding variable.
6137 c) Repeat step (b) on the ELSE_CLAUSE.
6139 d) Mark X and Y in FOUND_IN_SUBGRAPH.
6148 In this case, an assertion on the THEN clause is useful to
6149 determine that 'a' is always 9 on that edge. However, an assertion
6150 on the ELSE clause would be unnecessary.
6152 4- If BB does not end in a conditional expression, then we recurse
6153 into BB's dominator children.
6155 At the end of the recursive traversal, every SSA name will have a
6156 list of locations where ASSERT_EXPRs should be added. When a new
6157 location for name N is found, it is registered by calling
6158 register_new_assert_for. That function keeps track of all the
6159 registered assertions to prevent adding unnecessary assertions.
6160 For instance, if a pointer P_4 is dereferenced more than once in a
6161 dominator tree, only the location dominating all the dereference of
6162 P_4 will receive an ASSERT_EXPR. */
6165 find_assert_locations_1 (basic_block bb
, sbitmap live
)
6169 last
= last_stmt (bb
);
6171 /* If BB's last statement is a conditional statement involving integer
6172 operands, determine if we need to add ASSERT_EXPRs. */
6174 && gimple_code (last
) == GIMPLE_COND
6175 && !fp_predicate (last
)
6176 && !ZERO_SSA_OPERANDS (last
, SSA_OP_USE
))
6177 find_conditional_asserts (bb
, as_a
<gcond
*> (last
));
6179 /* If BB's last statement is a switch statement involving integer
6180 operands, determine if we need to add ASSERT_EXPRs. */
6182 && gimple_code (last
) == GIMPLE_SWITCH
6183 && !ZERO_SSA_OPERANDS (last
, SSA_OP_USE
))
6184 find_switch_asserts (bb
, as_a
<gswitch
*> (last
));
6186 /* Traverse all the statements in BB marking used names and looking
6187 for statements that may infer assertions for their used operands. */
6188 for (gimple_stmt_iterator si
= gsi_last_bb (bb
); !gsi_end_p (si
);
6195 stmt
= gsi_stmt (si
);
6197 if (is_gimple_debug (stmt
))
6200 /* See if we can derive an assertion for any of STMT's operands. */
6201 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, i
, SSA_OP_USE
)
6204 enum tree_code comp_code
;
6206 /* If op is not live beyond this stmt, do not bother to insert
6208 if (!bitmap_bit_p (live
, SSA_NAME_VERSION (op
)))
6211 /* If OP is used in such a way that we can infer a value
6212 range for it, and we don't find a previous assertion for
6213 it, create a new assertion location node for OP. */
6214 if (infer_value_range (stmt
, op
, &comp_code
, &value
))
6216 /* If we are able to infer a nonzero value range for OP,
6217 then walk backwards through the use-def chain to see if OP
6218 was set via a typecast.
6220 If so, then we can also infer a nonzero value range
6221 for the operand of the NOP_EXPR. */
6222 if (comp_code
== NE_EXPR
&& integer_zerop (value
))
6225 gimple def_stmt
= SSA_NAME_DEF_STMT (t
);
6227 while (is_gimple_assign (def_stmt
)
6228 && CONVERT_EXPR_CODE_P
6229 (gimple_assign_rhs_code (def_stmt
))
6231 (gimple_assign_rhs1 (def_stmt
)) == SSA_NAME
6233 (TREE_TYPE (gimple_assign_rhs1 (def_stmt
))))
6235 t
= gimple_assign_rhs1 (def_stmt
);
6236 def_stmt
= SSA_NAME_DEF_STMT (t
);
6238 /* Note we want to register the assert for the
6239 operand of the NOP_EXPR after SI, not after the
6241 if (! has_single_use (t
))
6242 register_new_assert_for (t
, t
, comp_code
, value
,
6247 register_new_assert_for (op
, op
, comp_code
, value
, bb
, NULL
, si
);
6252 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, i
, SSA_OP_USE
)
6253 bitmap_set_bit (live
, SSA_NAME_VERSION (op
));
6254 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, i
, SSA_OP_DEF
)
6255 bitmap_clear_bit (live
, SSA_NAME_VERSION (op
));
6258 /* Traverse all PHI nodes in BB, updating live. */
6259 for (gphi_iterator si
= gsi_start_phis (bb
); !gsi_end_p (si
);
6262 use_operand_p arg_p
;
6264 gphi
*phi
= si
.phi ();
6265 tree res
= gimple_phi_result (phi
);
6267 if (virtual_operand_p (res
))
6270 FOR_EACH_PHI_ARG (arg_p
, phi
, i
, SSA_OP_USE
)
6272 tree arg
= USE_FROM_PTR (arg_p
);
6273 if (TREE_CODE (arg
) == SSA_NAME
)
6274 bitmap_set_bit (live
, SSA_NAME_VERSION (arg
));
6277 bitmap_clear_bit (live
, SSA_NAME_VERSION (res
));
6281 /* Do an RPO walk over the function computing SSA name liveness
6282 on-the-fly and deciding on assert expressions to insert. */
6285 find_assert_locations (void)
6287 int *rpo
= XNEWVEC (int, last_basic_block_for_fn (cfun
));
6288 int *bb_rpo
= XNEWVEC (int, last_basic_block_for_fn (cfun
));
6289 int *last_rpo
= XCNEWVEC (int, last_basic_block_for_fn (cfun
));
6292 live
= XCNEWVEC (sbitmap
, last_basic_block_for_fn (cfun
));
6293 rpo_cnt
= pre_and_rev_post_order_compute (NULL
, rpo
, false);
6294 for (i
= 0; i
< rpo_cnt
; ++i
)
6297 /* Pre-seed loop latch liveness from loop header PHI nodes. Due to
6298 the order we compute liveness and insert asserts we otherwise
6299 fail to insert asserts into the loop latch. */
6301 FOR_EACH_LOOP (loop
, 0)
6303 i
= loop
->latch
->index
;
6304 unsigned int j
= single_succ_edge (loop
->latch
)->dest_idx
;
6305 for (gphi_iterator gsi
= gsi_start_phis (loop
->header
);
6306 !gsi_end_p (gsi
); gsi_next (&gsi
))
6308 gphi
*phi
= gsi
.phi ();
6309 if (virtual_operand_p (gimple_phi_result (phi
)))
6311 tree arg
= gimple_phi_arg_def (phi
, j
);
6312 if (TREE_CODE (arg
) == SSA_NAME
)
6314 if (live
[i
] == NULL
)
6316 live
[i
] = sbitmap_alloc (num_ssa_names
);
6317 bitmap_clear (live
[i
]);
6319 bitmap_set_bit (live
[i
], SSA_NAME_VERSION (arg
));
6324 for (i
= rpo_cnt
- 1; i
>= 0; --i
)
6326 basic_block bb
= BASIC_BLOCK_FOR_FN (cfun
, rpo
[i
]);
6332 live
[rpo
[i
]] = sbitmap_alloc (num_ssa_names
);
6333 bitmap_clear (live
[rpo
[i
]]);
6336 /* Process BB and update the live information with uses in
6338 find_assert_locations_1 (bb
, live
[rpo
[i
]]);
6340 /* Merge liveness into the predecessor blocks and free it. */
6341 if (!bitmap_empty_p (live
[rpo
[i
]]))
6344 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
6346 int pred
= e
->src
->index
;
6347 if ((e
->flags
& EDGE_DFS_BACK
) || pred
== ENTRY_BLOCK
)
6352 live
[pred
] = sbitmap_alloc (num_ssa_names
);
6353 bitmap_clear (live
[pred
]);
6355 bitmap_ior (live
[pred
], live
[pred
], live
[rpo
[i
]]);
6357 if (bb_rpo
[pred
] < pred_rpo
)
6358 pred_rpo
= bb_rpo
[pred
];
6361 /* Record the RPO number of the last visited block that needs
6362 live information from this block. */
6363 last_rpo
[rpo
[i
]] = pred_rpo
;
6367 sbitmap_free (live
[rpo
[i
]]);
6368 live
[rpo
[i
]] = NULL
;
6371 /* We can free all successors live bitmaps if all their
6372 predecessors have been visited already. */
6373 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
6374 if (last_rpo
[e
->dest
->index
] == i
6375 && live
[e
->dest
->index
])
6377 sbitmap_free (live
[e
->dest
->index
]);
6378 live
[e
->dest
->index
] = NULL
;
6383 XDELETEVEC (bb_rpo
);
6384 XDELETEVEC (last_rpo
);
6385 for (i
= 0; i
< last_basic_block_for_fn (cfun
); ++i
)
6387 sbitmap_free (live
[i
]);
6391 /* Create an ASSERT_EXPR for NAME and insert it in the location
6392 indicated by LOC. Return true if we made any edge insertions. */
6395 process_assert_insertions_for (tree name
, assert_locus_t loc
)
6397 /* Build the comparison expression NAME_i COMP_CODE VAL. */
6404 /* If we have X <=> X do not insert an assert expr for that. */
6405 if (loc
->expr
== loc
->val
)
6408 cond
= build2 (loc
->comp_code
, boolean_type_node
, loc
->expr
, loc
->val
);
6409 assert_stmt
= build_assert_expr_for (cond
, name
);
6412 /* We have been asked to insert the assertion on an edge. This
6413 is used only by COND_EXPR and SWITCH_EXPR assertions. */
6414 gcc_checking_assert (gimple_code (gsi_stmt (loc
->si
)) == GIMPLE_COND
6415 || (gimple_code (gsi_stmt (loc
->si
))
6418 gsi_insert_on_edge (loc
->e
, assert_stmt
);
6422 /* Otherwise, we can insert right after LOC->SI iff the
6423 statement must not be the last statement in the block. */
6424 stmt
= gsi_stmt (loc
->si
);
6425 if (!stmt_ends_bb_p (stmt
))
6427 gsi_insert_after (&loc
->si
, assert_stmt
, GSI_SAME_STMT
);
6431 /* If STMT must be the last statement in BB, we can only insert new
6432 assertions on the non-abnormal edge out of BB. Note that since
6433 STMT is not control flow, there may only be one non-abnormal edge
6435 FOR_EACH_EDGE (e
, ei
, loc
->bb
->succs
)
6436 if (!(e
->flags
& EDGE_ABNORMAL
))
6438 gsi_insert_on_edge (e
, assert_stmt
);
6446 /* Process all the insertions registered for every name N_i registered
6447 in NEED_ASSERT_FOR. The list of assertions to be inserted are
6448 found in ASSERTS_FOR[i]. */
6451 process_assert_insertions (void)
6455 bool update_edges_p
= false;
6456 int num_asserts
= 0;
6458 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6459 dump_all_asserts (dump_file
);
6461 EXECUTE_IF_SET_IN_BITMAP (need_assert_for
, 0, i
, bi
)
6463 assert_locus_t loc
= asserts_for
[i
];
6468 assert_locus_t next
= loc
->next
;
6469 update_edges_p
|= process_assert_insertions_for (ssa_name (i
), loc
);
6477 gsi_commit_edge_inserts ();
6479 statistics_counter_event (cfun
, "Number of ASSERT_EXPR expressions inserted",
6484 /* Traverse the flowgraph looking for conditional jumps to insert range
6485 expressions. These range expressions are meant to provide information
6486 to optimizations that need to reason in terms of value ranges. They
6487 will not be expanded into RTL. For instance, given:
6496 this pass will transform the code into:
6502 x = ASSERT_EXPR <x, x < y>
6507 y = ASSERT_EXPR <y, x >= y>
6511 The idea is that once copy and constant propagation have run, other
6512 optimizations will be able to determine what ranges of values can 'x'
6513 take in different paths of the code, simply by checking the reaching
6514 definition of 'x'. */
6517 insert_range_assertions (void)
6519 need_assert_for
= BITMAP_ALLOC (NULL
);
6520 asserts_for
= XCNEWVEC (assert_locus_t
, num_ssa_names
);
6522 calculate_dominance_info (CDI_DOMINATORS
);
6524 find_assert_locations ();
6525 if (!bitmap_empty_p (need_assert_for
))
6527 process_assert_insertions ();
6528 update_ssa (TODO_update_ssa_no_phi
);
6531 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6533 fprintf (dump_file
, "\nSSA form after inserting ASSERT_EXPRs\n");
6534 dump_function_to_file (current_function_decl
, dump_file
, dump_flags
);
6538 BITMAP_FREE (need_assert_for
);
6541 /* Checks one ARRAY_REF in REF, located at LOCUS. Ignores flexible arrays
6542 and "struct" hacks. If VRP can determine that the
6543 array subscript is a constant, check if it is outside valid
6544 range. If the array subscript is a RANGE, warn if it is
6545 non-overlapping with valid range.
6546 IGNORE_OFF_BY_ONE is true if the ARRAY_REF is inside a ADDR_EXPR. */
6549 check_array_ref (location_t location
, tree ref
, bool ignore_off_by_one
)
6551 value_range_t
* vr
= NULL
;
6552 tree low_sub
, up_sub
;
6553 tree low_bound
, up_bound
, up_bound_p1
;
6556 if (TREE_NO_WARNING (ref
))
6559 low_sub
= up_sub
= TREE_OPERAND (ref
, 1);
6560 up_bound
= array_ref_up_bound (ref
);
6562 /* Can not check flexible arrays. */
6564 || TREE_CODE (up_bound
) != INTEGER_CST
)
6567 /* Accesses to trailing arrays via pointers may access storage
6568 beyond the types array bounds. */
6569 base
= get_base_address (ref
);
6570 if ((warn_array_bounds
< 2)
6571 && base
&& TREE_CODE (base
) == MEM_REF
)
6573 tree cref
, next
= NULL_TREE
;
6575 if (TREE_CODE (TREE_OPERAND (ref
, 0)) != COMPONENT_REF
)
6578 cref
= TREE_OPERAND (ref
, 0);
6579 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (cref
, 0))) == RECORD_TYPE
)
6580 for (next
= DECL_CHAIN (TREE_OPERAND (cref
, 1));
6581 next
&& TREE_CODE (next
) != FIELD_DECL
;
6582 next
= DECL_CHAIN (next
))
6585 /* If this is the last field in a struct type or a field in a
6586 union type do not warn. */
6591 low_bound
= array_ref_low_bound (ref
);
6592 up_bound_p1
= int_const_binop (PLUS_EXPR
, up_bound
,
6593 build_int_cst (TREE_TYPE (up_bound
), 1));
6596 if (tree_int_cst_equal (low_bound
, up_bound_p1
))
6598 warning_at (location
, OPT_Warray_bounds
,
6599 "array subscript is above array bounds");
6600 TREE_NO_WARNING (ref
) = 1;
6603 if (TREE_CODE (low_sub
) == SSA_NAME
)
6605 vr
= get_value_range (low_sub
);
6606 if (vr
->type
== VR_RANGE
|| vr
->type
== VR_ANTI_RANGE
)
6608 low_sub
= vr
->type
== VR_RANGE
? vr
->max
: vr
->min
;
6609 up_sub
= vr
->type
== VR_RANGE
? vr
->min
: vr
->max
;
6613 if (vr
&& vr
->type
== VR_ANTI_RANGE
)
6615 if (TREE_CODE (up_sub
) == INTEGER_CST
6616 && (ignore_off_by_one
6617 ? tree_int_cst_lt (up_bound
, up_sub
)
6618 : tree_int_cst_le (up_bound
, up_sub
))
6619 && TREE_CODE (low_sub
) == INTEGER_CST
6620 && tree_int_cst_le (low_sub
, low_bound
))
6622 warning_at (location
, OPT_Warray_bounds
,
6623 "array subscript is outside array bounds");
6624 TREE_NO_WARNING (ref
) = 1;
6627 else if (TREE_CODE (up_sub
) == INTEGER_CST
6628 && (ignore_off_by_one
6629 ? !tree_int_cst_le (up_sub
, up_bound_p1
)
6630 : !tree_int_cst_le (up_sub
, up_bound
)))
6632 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6634 fprintf (dump_file
, "Array bound warning for ");
6635 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, ref
);
6636 fprintf (dump_file
, "\n");
6638 warning_at (location
, OPT_Warray_bounds
,
6639 "array subscript is above array bounds");
6640 TREE_NO_WARNING (ref
) = 1;
6642 else if (TREE_CODE (low_sub
) == INTEGER_CST
6643 && tree_int_cst_lt (low_sub
, low_bound
))
6645 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6647 fprintf (dump_file
, "Array bound warning for ");
6648 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, ref
);
6649 fprintf (dump_file
, "\n");
6651 warning_at (location
, OPT_Warray_bounds
,
6652 "array subscript is below array bounds");
6653 TREE_NO_WARNING (ref
) = 1;
6657 /* Searches if the expr T, located at LOCATION computes
6658 address of an ARRAY_REF, and call check_array_ref on it. */
6661 search_for_addr_array (tree t
, location_t location
)
6663 /* Check each ARRAY_REFs in the reference chain. */
6666 if (TREE_CODE (t
) == ARRAY_REF
)
6667 check_array_ref (location
, t
, true /*ignore_off_by_one*/);
6669 t
= TREE_OPERAND (t
, 0);
6671 while (handled_component_p (t
));
6673 if (TREE_CODE (t
) == MEM_REF
6674 && TREE_CODE (TREE_OPERAND (t
, 0)) == ADDR_EXPR
6675 && !TREE_NO_WARNING (t
))
6677 tree tem
= TREE_OPERAND (TREE_OPERAND (t
, 0), 0);
6678 tree low_bound
, up_bound
, el_sz
;
6680 if (TREE_CODE (TREE_TYPE (tem
)) != ARRAY_TYPE
6681 || TREE_CODE (TREE_TYPE (TREE_TYPE (tem
))) == ARRAY_TYPE
6682 || !TYPE_DOMAIN (TREE_TYPE (tem
)))
6685 low_bound
= TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (tem
)));
6686 up_bound
= TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (tem
)));
6687 el_sz
= TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (tem
)));
6689 || TREE_CODE (low_bound
) != INTEGER_CST
6691 || TREE_CODE (up_bound
) != INTEGER_CST
6693 || TREE_CODE (el_sz
) != INTEGER_CST
)
6696 idx
= mem_ref_offset (t
);
6697 idx
= wi::sdiv_trunc (idx
, wi::to_offset (el_sz
));
6698 if (wi::lts_p (idx
, 0))
6700 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6702 fprintf (dump_file
, "Array bound warning for ");
6703 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, t
);
6704 fprintf (dump_file
, "\n");
6706 warning_at (location
, OPT_Warray_bounds
,
6707 "array subscript is below array bounds");
6708 TREE_NO_WARNING (t
) = 1;
6710 else if (wi::gts_p (idx
, (wi::to_offset (up_bound
)
6711 - wi::to_offset (low_bound
) + 1)))
6713 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6715 fprintf (dump_file
, "Array bound warning for ");
6716 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, t
);
6717 fprintf (dump_file
, "\n");
6719 warning_at (location
, OPT_Warray_bounds
,
6720 "array subscript is above array bounds");
6721 TREE_NO_WARNING (t
) = 1;
6726 /* walk_tree() callback that checks if *TP is
6727 an ARRAY_REF inside an ADDR_EXPR (in which an array
6728 subscript one outside the valid range is allowed). Call
6729 check_array_ref for each ARRAY_REF found. The location is
6733 check_array_bounds (tree
*tp
, int *walk_subtree
, void *data
)
6736 struct walk_stmt_info
*wi
= (struct walk_stmt_info
*) data
;
6737 location_t location
;
6739 if (EXPR_HAS_LOCATION (t
))
6740 location
= EXPR_LOCATION (t
);
6743 location_t
*locp
= (location_t
*) wi
->info
;
6747 *walk_subtree
= TRUE
;
6749 if (TREE_CODE (t
) == ARRAY_REF
)
6750 check_array_ref (location
, t
, false /*ignore_off_by_one*/);
6752 else if (TREE_CODE (t
) == ADDR_EXPR
)
6754 search_for_addr_array (t
, location
);
6755 *walk_subtree
= FALSE
;
6761 /* Walk over all statements of all reachable BBs and call check_array_bounds
6765 check_all_array_refs (void)
6768 gimple_stmt_iterator si
;
6770 FOR_EACH_BB_FN (bb
, cfun
)
6774 bool executable
= false;
6776 /* Skip blocks that were found to be unreachable. */
6777 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
6778 executable
|= !!(e
->flags
& EDGE_EXECUTABLE
);
6782 for (si
= gsi_start_bb (bb
); !gsi_end_p (si
); gsi_next (&si
))
6784 gimple stmt
= gsi_stmt (si
);
6785 struct walk_stmt_info wi
;
6786 if (!gimple_has_location (stmt
)
6787 || is_gimple_debug (stmt
))
6790 memset (&wi
, 0, sizeof (wi
));
6791 wi
.info
= CONST_CAST (void *, (const void *)
6792 gimple_location_ptr (stmt
));
6794 walk_gimple_op (gsi_stmt (si
),
6801 /* Return true if all imm uses of VAR are either in STMT, or
6802 feed (optionally through a chain of single imm uses) GIMPLE_COND
6803 in basic block COND_BB. */
6806 all_imm_uses_in_stmt_or_feed_cond (tree var
, gimple stmt
, basic_block cond_bb
)
6808 use_operand_p use_p
, use2_p
;
6809 imm_use_iterator iter
;
6811 FOR_EACH_IMM_USE_FAST (use_p
, iter
, var
)
6812 if (USE_STMT (use_p
) != stmt
)
6814 gimple use_stmt
= USE_STMT (use_p
), use_stmt2
;
6815 if (is_gimple_debug (use_stmt
))
6817 while (is_gimple_assign (use_stmt
)
6818 && TREE_CODE (gimple_assign_lhs (use_stmt
)) == SSA_NAME
6819 && single_imm_use (gimple_assign_lhs (use_stmt
),
6820 &use2_p
, &use_stmt2
))
6821 use_stmt
= use_stmt2
;
6822 if (gimple_code (use_stmt
) != GIMPLE_COND
6823 || gimple_bb (use_stmt
) != cond_bb
)
6836 __builtin_unreachable ();
6838 x_5 = ASSERT_EXPR <x_3, ...>;
6839 If x_3 has no other immediate uses (checked by caller),
6840 var is the x_3 var from ASSERT_EXPR, we can clear low 5 bits
6841 from the non-zero bitmask. */
6844 maybe_set_nonzero_bits (basic_block bb
, tree var
)
6846 edge e
= single_pred_edge (bb
);
6847 basic_block cond_bb
= e
->src
;
6848 gimple stmt
= last_stmt (cond_bb
);
6852 || gimple_code (stmt
) != GIMPLE_COND
6853 || gimple_cond_code (stmt
) != ((e
->flags
& EDGE_TRUE_VALUE
)
6854 ? EQ_EXPR
: NE_EXPR
)
6855 || TREE_CODE (gimple_cond_lhs (stmt
)) != SSA_NAME
6856 || !integer_zerop (gimple_cond_rhs (stmt
)))
6859 stmt
= SSA_NAME_DEF_STMT (gimple_cond_lhs (stmt
));
6860 if (!is_gimple_assign (stmt
)
6861 || gimple_assign_rhs_code (stmt
) != BIT_AND_EXPR
6862 || TREE_CODE (gimple_assign_rhs2 (stmt
)) != INTEGER_CST
)
6864 if (gimple_assign_rhs1 (stmt
) != var
)
6868 if (TREE_CODE (gimple_assign_rhs1 (stmt
)) != SSA_NAME
)
6870 stmt2
= SSA_NAME_DEF_STMT (gimple_assign_rhs1 (stmt
));
6871 if (!gimple_assign_cast_p (stmt2
)
6872 || gimple_assign_rhs1 (stmt2
) != var
6873 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (stmt2
))
6874 || (TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (stmt
)))
6875 != TYPE_PRECISION (TREE_TYPE (var
))))
6878 cst
= gimple_assign_rhs2 (stmt
);
6879 set_nonzero_bits (var
, wi::bit_and_not (get_nonzero_bits (var
), cst
));
6882 /* Convert range assertion expressions into the implied copies and
6883 copy propagate away the copies. Doing the trivial copy propagation
6884 here avoids the need to run the full copy propagation pass after
6887 FIXME, this will eventually lead to copy propagation removing the
6888 names that had useful range information attached to them. For
6889 instance, if we had the assertion N_i = ASSERT_EXPR <N_j, N_j > 3>,
6890 then N_i will have the range [3, +INF].
6892 However, by converting the assertion into the implied copy
6893 operation N_i = N_j, we will then copy-propagate N_j into the uses
6894 of N_i and lose the range information. We may want to hold on to
6895 ASSERT_EXPRs a little while longer as the ranges could be used in
6896 things like jump threading.
6898 The problem with keeping ASSERT_EXPRs around is that passes after
6899 VRP need to handle them appropriately.
6901 Another approach would be to make the range information a first
6902 class property of the SSA_NAME so that it can be queried from
6903 any pass. This is made somewhat more complex by the need for
6904 multiple ranges to be associated with one SSA_NAME. */
6907 remove_range_assertions (void)
6910 gimple_stmt_iterator si
;
6911 /* 1 if looking at ASSERT_EXPRs immediately at the beginning of
6912 a basic block preceeded by GIMPLE_COND branching to it and
6913 __builtin_trap, -1 if not yet checked, 0 otherwise. */
6916 /* Note that the BSI iterator bump happens at the bottom of the
6917 loop and no bump is necessary if we're removing the statement
6918 referenced by the current BSI. */
6919 FOR_EACH_BB_FN (bb
, cfun
)
6920 for (si
= gsi_after_labels (bb
), is_unreachable
= -1; !gsi_end_p (si
);)
6922 gimple stmt
= gsi_stmt (si
);
6925 if (is_gimple_assign (stmt
)
6926 && gimple_assign_rhs_code (stmt
) == ASSERT_EXPR
)
6928 tree lhs
= gimple_assign_lhs (stmt
);
6929 tree rhs
= gimple_assign_rhs1 (stmt
);
6931 tree cond
= fold (ASSERT_EXPR_COND (rhs
));
6932 use_operand_p use_p
;
6933 imm_use_iterator iter
;
6935 gcc_assert (cond
!= boolean_false_node
);
6937 var
= ASSERT_EXPR_VAR (rhs
);
6938 gcc_assert (TREE_CODE (var
) == SSA_NAME
);
6940 if (!POINTER_TYPE_P (TREE_TYPE (lhs
))
6941 && SSA_NAME_RANGE_INFO (lhs
))
6943 if (is_unreachable
== -1)
6946 if (single_pred_p (bb
)
6947 && assert_unreachable_fallthru_edge_p
6948 (single_pred_edge (bb
)))
6952 if (x_7 >= 10 && x_7 < 20)
6953 __builtin_unreachable ();
6954 x_8 = ASSERT_EXPR <x_7, ...>;
6955 if the only uses of x_7 are in the ASSERT_EXPR and
6956 in the condition. In that case, we can copy the
6957 range info from x_8 computed in this pass also
6960 && all_imm_uses_in_stmt_or_feed_cond (var
, stmt
,
6963 set_range_info (var
, SSA_NAME_RANGE_TYPE (lhs
),
6964 SSA_NAME_RANGE_INFO (lhs
)->get_min (),
6965 SSA_NAME_RANGE_INFO (lhs
)->get_max ());
6966 maybe_set_nonzero_bits (bb
, var
);
6970 /* Propagate the RHS into every use of the LHS. */
6971 FOR_EACH_IMM_USE_STMT (use_stmt
, iter
, lhs
)
6972 FOR_EACH_IMM_USE_ON_STMT (use_p
, iter
)
6973 SET_USE (use_p
, var
);
6975 /* And finally, remove the copy, it is not needed. */
6976 gsi_remove (&si
, true);
6977 release_defs (stmt
);
6981 if (!is_gimple_debug (gsi_stmt (si
)))
6989 /* Return true if STMT is interesting for VRP. */
6992 stmt_interesting_for_vrp (gimple stmt
)
6994 if (gimple_code (stmt
) == GIMPLE_PHI
)
6996 tree res
= gimple_phi_result (stmt
);
6997 return (!virtual_operand_p (res
)
6998 && (INTEGRAL_TYPE_P (TREE_TYPE (res
))
6999 || POINTER_TYPE_P (TREE_TYPE (res
))));
7001 else if (is_gimple_assign (stmt
) || is_gimple_call (stmt
))
7003 tree lhs
= gimple_get_lhs (stmt
);
7005 /* In general, assignments with virtual operands are not useful
7006 for deriving ranges, with the obvious exception of calls to
7007 builtin functions. */
7008 if (lhs
&& TREE_CODE (lhs
) == SSA_NAME
7009 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs
))
7010 || POINTER_TYPE_P (TREE_TYPE (lhs
)))
7011 && (is_gimple_call (stmt
)
7012 || !gimple_vuse (stmt
)))
7014 else if (is_gimple_call (stmt
) && gimple_call_internal_p (stmt
))
7015 switch (gimple_call_internal_fn (stmt
))
7017 case IFN_ADD_OVERFLOW
:
7018 case IFN_SUB_OVERFLOW
:
7019 case IFN_MUL_OVERFLOW
:
7020 /* These internal calls return _Complex integer type,
7021 but are interesting to VRP nevertheless. */
7022 if (lhs
&& TREE_CODE (lhs
) == SSA_NAME
)
7029 else if (gimple_code (stmt
) == GIMPLE_COND
7030 || gimple_code (stmt
) == GIMPLE_SWITCH
)
7037 /* Initialize local data structures for VRP. */
7040 vrp_initialize (void)
7044 values_propagated
= false;
7045 num_vr_values
= num_ssa_names
;
7046 vr_value
= XCNEWVEC (value_range_t
*, num_vr_values
);
7047 vr_phi_edge_counts
= XCNEWVEC (int, num_ssa_names
);
7049 FOR_EACH_BB_FN (bb
, cfun
)
7051 for (gphi_iterator si
= gsi_start_phis (bb
); !gsi_end_p (si
);
7054 gphi
*phi
= si
.phi ();
7055 if (!stmt_interesting_for_vrp (phi
))
7057 tree lhs
= PHI_RESULT (phi
);
7058 set_value_range_to_varying (get_value_range (lhs
));
7059 prop_set_simulate_again (phi
, false);
7062 prop_set_simulate_again (phi
, true);
7065 for (gimple_stmt_iterator si
= gsi_start_bb (bb
); !gsi_end_p (si
);
7068 gimple stmt
= gsi_stmt (si
);
7070 /* If the statement is a control insn, then we do not
7071 want to avoid simulating the statement once. Failure
7072 to do so means that those edges will never get added. */
7073 if (stmt_ends_bb_p (stmt
))
7074 prop_set_simulate_again (stmt
, true);
7075 else if (!stmt_interesting_for_vrp (stmt
))
7079 FOR_EACH_SSA_TREE_OPERAND (def
, stmt
, i
, SSA_OP_DEF
)
7080 set_value_range_to_varying (get_value_range (def
));
7081 prop_set_simulate_again (stmt
, false);
7084 prop_set_simulate_again (stmt
, true);
7089 /* Return the singleton value-range for NAME or NAME. */
7092 vrp_valueize (tree name
)
7094 if (TREE_CODE (name
) == SSA_NAME
)
7096 value_range_t
*vr
= get_value_range (name
);
7097 if (vr
->type
== VR_RANGE
7098 && (vr
->min
== vr
->max
7099 || operand_equal_p (vr
->min
, vr
->max
, 0)))
7105 /* Return the singleton value-range for NAME if that is a constant
7106 but signal to not follow SSA edges. */
7109 vrp_valueize_1 (tree name
)
7111 if (TREE_CODE (name
) == SSA_NAME
)
7113 /* If the definition may be simulated again we cannot follow
7114 this SSA edge as the SSA propagator does not necessarily
7115 re-visit the use. */
7116 gimple def_stmt
= SSA_NAME_DEF_STMT (name
);
7117 if (!gimple_nop_p (def_stmt
)
7118 && prop_simulate_again_p (def_stmt
))
7120 value_range_t
*vr
= get_value_range (name
);
7121 if (range_int_cst_singleton_p (vr
))
7127 /* Visit assignment STMT. If it produces an interesting range, record
7128 the SSA name in *OUTPUT_P. */
7130 static enum ssa_prop_result
7131 vrp_visit_assignment_or_call (gimple stmt
, tree
*output_p
)
7135 enum gimple_code code
= gimple_code (stmt
);
7136 lhs
= gimple_get_lhs (stmt
);
7138 /* We only keep track of ranges in integral and pointer types. */
7139 if (TREE_CODE (lhs
) == SSA_NAME
7140 && ((INTEGRAL_TYPE_P (TREE_TYPE (lhs
))
7141 /* It is valid to have NULL MIN/MAX values on a type. See
7142 build_range_type. */
7143 && TYPE_MIN_VALUE (TREE_TYPE (lhs
))
7144 && TYPE_MAX_VALUE (TREE_TYPE (lhs
)))
7145 || POINTER_TYPE_P (TREE_TYPE (lhs
))))
7147 value_range_t new_vr
= VR_INITIALIZER
;
7149 /* Try folding the statement to a constant first. */
7150 tree tem
= gimple_fold_stmt_to_constant_1 (stmt
, vrp_valueize
,
7152 if (tem
&& is_gimple_min_invariant (tem
))
7153 set_value_range_to_value (&new_vr
, tem
, NULL
);
7154 /* Then dispatch to value-range extracting functions. */
7155 else if (code
== GIMPLE_CALL
)
7156 extract_range_basic (&new_vr
, stmt
);
7158 extract_range_from_assignment (&new_vr
, as_a
<gassign
*> (stmt
));
7160 if (update_value_range (lhs
, &new_vr
))
7164 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7166 fprintf (dump_file
, "Found new range for ");
7167 print_generic_expr (dump_file
, lhs
, 0);
7168 fprintf (dump_file
, ": ");
7169 dump_value_range (dump_file
, &new_vr
);
7170 fprintf (dump_file
, "\n");
7173 if (new_vr
.type
== VR_VARYING
)
7174 return SSA_PROP_VARYING
;
7176 return SSA_PROP_INTERESTING
;
7179 return SSA_PROP_NOT_INTERESTING
;
7181 else if (is_gimple_call (stmt
) && gimple_call_internal_p (stmt
))
7182 switch (gimple_call_internal_fn (stmt
))
7184 case IFN_ADD_OVERFLOW
:
7185 case IFN_SUB_OVERFLOW
:
7186 case IFN_MUL_OVERFLOW
:
7187 /* These internal calls return _Complex integer type,
7188 which VRP does not track, but the immediate uses
7189 thereof might be interesting. */
7190 if (lhs
&& TREE_CODE (lhs
) == SSA_NAME
)
7192 imm_use_iterator iter
;
7193 use_operand_p use_p
;
7194 enum ssa_prop_result res
= SSA_PROP_VARYING
;
7196 set_value_range_to_varying (get_value_range (lhs
));
7198 FOR_EACH_IMM_USE_FAST (use_p
, iter
, lhs
)
7200 gimple use_stmt
= USE_STMT (use_p
);
7201 if (!is_gimple_assign (use_stmt
))
7203 enum tree_code rhs_code
= gimple_assign_rhs_code (use_stmt
);
7204 if (rhs_code
!= REALPART_EXPR
&& rhs_code
!= IMAGPART_EXPR
)
7206 tree rhs1
= gimple_assign_rhs1 (use_stmt
);
7207 tree use_lhs
= gimple_assign_lhs (use_stmt
);
7208 if (TREE_CODE (rhs1
) != rhs_code
7209 || TREE_OPERAND (rhs1
, 0) != lhs
7210 || TREE_CODE (use_lhs
) != SSA_NAME
7211 || !stmt_interesting_for_vrp (use_stmt
)
7212 || (!INTEGRAL_TYPE_P (TREE_TYPE (use_lhs
))
7213 || !TYPE_MIN_VALUE (TREE_TYPE (use_lhs
))
7214 || !TYPE_MAX_VALUE (TREE_TYPE (use_lhs
))))
7217 /* If there is a change in the value range for any of the
7218 REALPART_EXPR/IMAGPART_EXPR immediate uses, return
7219 SSA_PROP_INTERESTING. If there are any REALPART_EXPR
7220 or IMAGPART_EXPR immediate uses, but none of them have
7221 a change in their value ranges, return
7222 SSA_PROP_NOT_INTERESTING. If there are no
7223 {REAL,IMAG}PART_EXPR uses at all,
7224 return SSA_PROP_VARYING. */
7225 value_range_t new_vr
= VR_INITIALIZER
;
7226 extract_range_basic (&new_vr
, use_stmt
);
7227 value_range_t
*old_vr
= get_value_range (use_lhs
);
7228 if (old_vr
->type
!= new_vr
.type
7229 || !vrp_operand_equal_p (old_vr
->min
, new_vr
.min
)
7230 || !vrp_operand_equal_p (old_vr
->max
, new_vr
.max
)
7231 || !vrp_bitmap_equal_p (old_vr
->equiv
, new_vr
.equiv
))
7232 res
= SSA_PROP_INTERESTING
;
7234 res
= SSA_PROP_NOT_INTERESTING
;
7235 BITMAP_FREE (new_vr
.equiv
);
7236 if (res
== SSA_PROP_INTERESTING
)
7250 /* Every other statement produces no useful ranges. */
7251 FOR_EACH_SSA_TREE_OPERAND (def
, stmt
, iter
, SSA_OP_DEF
)
7252 set_value_range_to_varying (get_value_range (def
));
7254 return SSA_PROP_VARYING
;
7257 /* Helper that gets the value range of the SSA_NAME with version I
7258 or a symbolic range containing the SSA_NAME only if the value range
7259 is varying or undefined. */
7261 static inline value_range_t
7262 get_vr_for_comparison (int i
)
7264 value_range_t vr
= *get_value_range (ssa_name (i
));
7266 /* If name N_i does not have a valid range, use N_i as its own
7267 range. This allows us to compare against names that may
7268 have N_i in their ranges. */
7269 if (vr
.type
== VR_VARYING
|| vr
.type
== VR_UNDEFINED
)
7272 vr
.min
= ssa_name (i
);
7273 vr
.max
= ssa_name (i
);
7279 /* Compare all the value ranges for names equivalent to VAR with VAL
7280 using comparison code COMP. Return the same value returned by
7281 compare_range_with_value, including the setting of
7282 *STRICT_OVERFLOW_P. */
7285 compare_name_with_value (enum tree_code comp
, tree var
, tree val
,
7286 bool *strict_overflow_p
)
7292 int used_strict_overflow
;
7294 value_range_t equiv_vr
;
7296 /* Get the set of equivalences for VAR. */
7297 e
= get_value_range (var
)->equiv
;
7299 /* Start at -1. Set it to 0 if we do a comparison without relying
7300 on overflow, or 1 if all comparisons rely on overflow. */
7301 used_strict_overflow
= -1;
7303 /* Compare vars' value range with val. */
7304 equiv_vr
= get_vr_for_comparison (SSA_NAME_VERSION (var
));
7306 retval
= compare_range_with_value (comp
, &equiv_vr
, val
, &sop
);
7308 used_strict_overflow
= sop
? 1 : 0;
7310 /* If the equiv set is empty we have done all work we need to do. */
7314 && used_strict_overflow
> 0)
7315 *strict_overflow_p
= true;
7319 EXECUTE_IF_SET_IN_BITMAP (e
, 0, i
, bi
)
7321 equiv_vr
= get_vr_for_comparison (i
);
7323 t
= compare_range_with_value (comp
, &equiv_vr
, val
, &sop
);
7326 /* If we get different answers from different members
7327 of the equivalence set this check must be in a dead
7328 code region. Folding it to a trap representation
7329 would be correct here. For now just return don't-know. */
7339 used_strict_overflow
= 0;
7340 else if (used_strict_overflow
< 0)
7341 used_strict_overflow
= 1;
7346 && used_strict_overflow
> 0)
7347 *strict_overflow_p
= true;
7353 /* Given a comparison code COMP and names N1 and N2, compare all the
7354 ranges equivalent to N1 against all the ranges equivalent to N2
7355 to determine the value of N1 COMP N2. Return the same value
7356 returned by compare_ranges. Set *STRICT_OVERFLOW_P to indicate
7357 whether we relied on an overflow infinity in the comparison. */
7361 compare_names (enum tree_code comp
, tree n1
, tree n2
,
7362 bool *strict_overflow_p
)
7366 bitmap_iterator bi1
, bi2
;
7368 int used_strict_overflow
;
7369 static bitmap_obstack
*s_obstack
= NULL
;
7370 static bitmap s_e1
= NULL
, s_e2
= NULL
;
7372 /* Compare the ranges of every name equivalent to N1 against the
7373 ranges of every name equivalent to N2. */
7374 e1
= get_value_range (n1
)->equiv
;
7375 e2
= get_value_range (n2
)->equiv
;
7377 /* Use the fake bitmaps if e1 or e2 are not available. */
7378 if (s_obstack
== NULL
)
7380 s_obstack
= XNEW (bitmap_obstack
);
7381 bitmap_obstack_initialize (s_obstack
);
7382 s_e1
= BITMAP_ALLOC (s_obstack
);
7383 s_e2
= BITMAP_ALLOC (s_obstack
);
7390 /* Add N1 and N2 to their own set of equivalences to avoid
7391 duplicating the body of the loop just to check N1 and N2
7393 bitmap_set_bit (e1
, SSA_NAME_VERSION (n1
));
7394 bitmap_set_bit (e2
, SSA_NAME_VERSION (n2
));
7396 /* If the equivalence sets have a common intersection, then the two
7397 names can be compared without checking their ranges. */
7398 if (bitmap_intersect_p (e1
, e2
))
7400 bitmap_clear_bit (e1
, SSA_NAME_VERSION (n1
));
7401 bitmap_clear_bit (e2
, SSA_NAME_VERSION (n2
));
7403 return (comp
== EQ_EXPR
|| comp
== GE_EXPR
|| comp
== LE_EXPR
)
7405 : boolean_false_node
;
7408 /* Start at -1. Set it to 0 if we do a comparison without relying
7409 on overflow, or 1 if all comparisons rely on overflow. */
7410 used_strict_overflow
= -1;
7412 /* Otherwise, compare all the equivalent ranges. First, add N1 and
7413 N2 to their own set of equivalences to avoid duplicating the body
7414 of the loop just to check N1 and N2 ranges. */
7415 EXECUTE_IF_SET_IN_BITMAP (e1
, 0, i1
, bi1
)
7417 value_range_t vr1
= get_vr_for_comparison (i1
);
7419 t
= retval
= NULL_TREE
;
7420 EXECUTE_IF_SET_IN_BITMAP (e2
, 0, i2
, bi2
)
7424 value_range_t vr2
= get_vr_for_comparison (i2
);
7426 t
= compare_ranges (comp
, &vr1
, &vr2
, &sop
);
7429 /* If we get different answers from different members
7430 of the equivalence set this check must be in a dead
7431 code region. Folding it to a trap representation
7432 would be correct here. For now just return don't-know. */
7436 bitmap_clear_bit (e1
, SSA_NAME_VERSION (n1
));
7437 bitmap_clear_bit (e2
, SSA_NAME_VERSION (n2
));
7443 used_strict_overflow
= 0;
7444 else if (used_strict_overflow
< 0)
7445 used_strict_overflow
= 1;
7451 bitmap_clear_bit (e1
, SSA_NAME_VERSION (n1
));
7452 bitmap_clear_bit (e2
, SSA_NAME_VERSION (n2
));
7453 if (used_strict_overflow
> 0)
7454 *strict_overflow_p
= true;
7459 /* None of the equivalent ranges are useful in computing this
7461 bitmap_clear_bit (e1
, SSA_NAME_VERSION (n1
));
7462 bitmap_clear_bit (e2
, SSA_NAME_VERSION (n2
));
7466 /* Helper function for vrp_evaluate_conditional_warnv. */
7469 vrp_evaluate_conditional_warnv_with_ops_using_ranges (enum tree_code code
,
7471 bool * strict_overflow_p
)
7473 value_range_t
*vr0
, *vr1
;
7475 vr0
= (TREE_CODE (op0
) == SSA_NAME
) ? get_value_range (op0
) : NULL
;
7476 vr1
= (TREE_CODE (op1
) == SSA_NAME
) ? get_value_range (op1
) : NULL
;
7478 tree res
= NULL_TREE
;
7480 res
= compare_ranges (code
, vr0
, vr1
, strict_overflow_p
);
7482 res
= compare_range_with_value (code
, vr0
, op1
, strict_overflow_p
);
7484 res
= (compare_range_with_value
7485 (swap_tree_comparison (code
), vr1
, op0
, strict_overflow_p
));
7489 /* Helper function for vrp_evaluate_conditional_warnv. */
7492 vrp_evaluate_conditional_warnv_with_ops (enum tree_code code
, tree op0
,
7493 tree op1
, bool use_equiv_p
,
7494 bool *strict_overflow_p
, bool *only_ranges
)
7498 *only_ranges
= true;
7500 /* We only deal with integral and pointer types. */
7501 if (!INTEGRAL_TYPE_P (TREE_TYPE (op0
))
7502 && !POINTER_TYPE_P (TREE_TYPE (op0
)))
7508 && (ret
= vrp_evaluate_conditional_warnv_with_ops_using_ranges
7509 (code
, op0
, op1
, strict_overflow_p
)))
7511 *only_ranges
= false;
7512 if (TREE_CODE (op0
) == SSA_NAME
&& TREE_CODE (op1
) == SSA_NAME
)
7513 return compare_names (code
, op0
, op1
, strict_overflow_p
);
7514 else if (TREE_CODE (op0
) == SSA_NAME
)
7515 return compare_name_with_value (code
, op0
, op1
, strict_overflow_p
);
7516 else if (TREE_CODE (op1
) == SSA_NAME
)
7517 return (compare_name_with_value
7518 (swap_tree_comparison (code
), op1
, op0
, strict_overflow_p
));
7521 return vrp_evaluate_conditional_warnv_with_ops_using_ranges (code
, op0
, op1
,
7526 /* Given (CODE OP0 OP1) within STMT, try to simplify it based on value range
7527 information. Return NULL if the conditional can not be evaluated.
7528 The ranges of all the names equivalent with the operands in COND
7529 will be used when trying to compute the value. If the result is
7530 based on undefined signed overflow, issue a warning if
7534 vrp_evaluate_conditional (enum tree_code code
, tree op0
, tree op1
, gimple stmt
)
7540 /* Some passes and foldings leak constants with overflow flag set
7541 into the IL. Avoid doing wrong things with these and bail out. */
7542 if ((TREE_CODE (op0
) == INTEGER_CST
7543 && TREE_OVERFLOW (op0
))
7544 || (TREE_CODE (op1
) == INTEGER_CST
7545 && TREE_OVERFLOW (op1
)))
7549 ret
= vrp_evaluate_conditional_warnv_with_ops (code
, op0
, op1
, true, &sop
,
7554 enum warn_strict_overflow_code wc
;
7555 const char* warnmsg
;
7557 if (is_gimple_min_invariant (ret
))
7559 wc
= WARN_STRICT_OVERFLOW_CONDITIONAL
;
7560 warnmsg
= G_("assuming signed overflow does not occur when "
7561 "simplifying conditional to constant");
7565 wc
= WARN_STRICT_OVERFLOW_COMPARISON
;
7566 warnmsg
= G_("assuming signed overflow does not occur when "
7567 "simplifying conditional");
7570 if (issue_strict_overflow_warning (wc
))
7572 location_t location
;
7574 if (!gimple_has_location (stmt
))
7575 location
= input_location
;
7577 location
= gimple_location (stmt
);
7578 warning_at (location
, OPT_Wstrict_overflow
, "%s", warnmsg
);
7582 if (warn_type_limits
7583 && ret
&& only_ranges
7584 && TREE_CODE_CLASS (code
) == tcc_comparison
7585 && TREE_CODE (op0
) == SSA_NAME
)
7587 /* If the comparison is being folded and the operand on the LHS
7588 is being compared against a constant value that is outside of
7589 the natural range of OP0's type, then the predicate will
7590 always fold regardless of the value of OP0. If -Wtype-limits
7591 was specified, emit a warning. */
7592 tree type
= TREE_TYPE (op0
);
7593 value_range_t
*vr0
= get_value_range (op0
);
7595 if (vr0
->type
== VR_RANGE
7596 && INTEGRAL_TYPE_P (type
)
7597 && vrp_val_is_min (vr0
->min
)
7598 && vrp_val_is_max (vr0
->max
)
7599 && is_gimple_min_invariant (op1
))
7601 location_t location
;
7603 if (!gimple_has_location (stmt
))
7604 location
= input_location
;
7606 location
= gimple_location (stmt
);
7608 warning_at (location
, OPT_Wtype_limits
,
7610 ? G_("comparison always false "
7611 "due to limited range of data type")
7612 : G_("comparison always true "
7613 "due to limited range of data type"));
7621 /* Visit conditional statement STMT. If we can determine which edge
7622 will be taken out of STMT's basic block, record it in
7623 *TAKEN_EDGE_P and return SSA_PROP_INTERESTING. Otherwise, return
7624 SSA_PROP_VARYING. */
7626 static enum ssa_prop_result
7627 vrp_visit_cond_stmt (gcond
*stmt
, edge
*taken_edge_p
)
7632 *taken_edge_p
= NULL
;
7634 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7639 fprintf (dump_file
, "\nVisiting conditional with predicate: ");
7640 print_gimple_stmt (dump_file
, stmt
, 0, 0);
7641 fprintf (dump_file
, "\nWith known ranges\n");
7643 FOR_EACH_SSA_TREE_OPERAND (use
, stmt
, i
, SSA_OP_USE
)
7645 fprintf (dump_file
, "\t");
7646 print_generic_expr (dump_file
, use
, 0);
7647 fprintf (dump_file
, ": ");
7648 dump_value_range (dump_file
, vr_value
[SSA_NAME_VERSION (use
)]);
7651 fprintf (dump_file
, "\n");
7654 /* Compute the value of the predicate COND by checking the known
7655 ranges of each of its operands.
7657 Note that we cannot evaluate all the equivalent ranges here
7658 because those ranges may not yet be final and with the current
7659 propagation strategy, we cannot determine when the value ranges
7660 of the names in the equivalence set have changed.
7662 For instance, given the following code fragment
7666 i_14 = ASSERT_EXPR <i_5, i_5 != 0>
7670 Assume that on the first visit to i_14, i_5 has the temporary
7671 range [8, 8] because the second argument to the PHI function is
7672 not yet executable. We derive the range ~[0, 0] for i_14 and the
7673 equivalence set { i_5 }. So, when we visit 'if (i_14 == 1)' for
7674 the first time, since i_14 is equivalent to the range [8, 8], we
7675 determine that the predicate is always false.
7677 On the next round of propagation, i_13 is determined to be
7678 VARYING, which causes i_5 to drop down to VARYING. So, another
7679 visit to i_14 is scheduled. In this second visit, we compute the
7680 exact same range and equivalence set for i_14, namely ~[0, 0] and
7681 { i_5 }. But we did not have the previous range for i_5
7682 registered, so vrp_visit_assignment thinks that the range for
7683 i_14 has not changed. Therefore, the predicate 'if (i_14 == 1)'
7684 is not visited again, which stops propagation from visiting
7685 statements in the THEN clause of that if().
7687 To properly fix this we would need to keep the previous range
7688 value for the names in the equivalence set. This way we would've
7689 discovered that from one visit to the other i_5 changed from
7690 range [8, 8] to VR_VARYING.
7692 However, fixing this apparent limitation may not be worth the
7693 additional checking. Testing on several code bases (GCC, DLV,
7694 MICO, TRAMP3D and SPEC2000) showed that doing this results in
7695 4 more predicates folded in SPEC. */
7698 val
= vrp_evaluate_conditional_warnv_with_ops (gimple_cond_code (stmt
),
7699 gimple_cond_lhs (stmt
),
7700 gimple_cond_rhs (stmt
),
7705 *taken_edge_p
= find_taken_edge (gimple_bb (stmt
), val
);
7708 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7710 "\nIgnoring predicate evaluation because "
7711 "it assumes that signed overflow is undefined");
7716 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7718 fprintf (dump_file
, "\nPredicate evaluates to: ");
7719 if (val
== NULL_TREE
)
7720 fprintf (dump_file
, "DON'T KNOW\n");
7722 print_generic_stmt (dump_file
, val
, 0);
7725 return (*taken_edge_p
) ? SSA_PROP_INTERESTING
: SSA_PROP_VARYING
;
7728 /* Searches the case label vector VEC for the index *IDX of the CASE_LABEL
7729 that includes the value VAL. The search is restricted to the range
7730 [START_IDX, n - 1] where n is the size of VEC.
7732 If there is a CASE_LABEL for VAL, its index is placed in IDX and true is
7735 If there is no CASE_LABEL for VAL and there is one that is larger than VAL,
7736 it is placed in IDX and false is returned.
7738 If VAL is larger than any CASE_LABEL, n is placed on IDX and false is
7742 find_case_label_index (gswitch
*stmt
, size_t start_idx
, tree val
, size_t *idx
)
7744 size_t n
= gimple_switch_num_labels (stmt
);
7747 /* Find case label for minimum of the value range or the next one.
7748 At each iteration we are searching in [low, high - 1]. */
7750 for (low
= start_idx
, high
= n
; high
!= low
; )
7754 /* Note that i != high, so we never ask for n. */
7755 size_t i
= (high
+ low
) / 2;
7756 t
= gimple_switch_label (stmt
, i
);
7758 /* Cache the result of comparing CASE_LOW and val. */
7759 cmp
= tree_int_cst_compare (CASE_LOW (t
), val
);
7763 /* Ranges cannot be empty. */
7772 if (CASE_HIGH (t
) != NULL
7773 && tree_int_cst_compare (CASE_HIGH (t
), val
) >= 0)
7785 /* Searches the case label vector VEC for the range of CASE_LABELs that is used
7786 for values between MIN and MAX. The first index is placed in MIN_IDX. The
7787 last index is placed in MAX_IDX. If the range of CASE_LABELs is empty
7788 then MAX_IDX < MIN_IDX.
7789 Returns true if the default label is not needed. */
7792 find_case_label_range (gswitch
*stmt
, tree min
, tree max
, size_t *min_idx
,
7796 bool min_take_default
= !find_case_label_index (stmt
, 1, min
, &i
);
7797 bool max_take_default
= !find_case_label_index (stmt
, i
, max
, &j
);
7801 && max_take_default
)
7803 /* Only the default case label reached.
7804 Return an empty range. */
7811 bool take_default
= min_take_default
|| max_take_default
;
7815 if (max_take_default
)
7818 /* If the case label range is continuous, we do not need
7819 the default case label. Verify that. */
7820 high
= CASE_LOW (gimple_switch_label (stmt
, i
));
7821 if (CASE_HIGH (gimple_switch_label (stmt
, i
)))
7822 high
= CASE_HIGH (gimple_switch_label (stmt
, i
));
7823 for (k
= i
+ 1; k
<= j
; ++k
)
7825 low
= CASE_LOW (gimple_switch_label (stmt
, k
));
7826 if (!integer_onep (int_const_binop (MINUS_EXPR
, low
, high
)))
7828 take_default
= true;
7832 if (CASE_HIGH (gimple_switch_label (stmt
, k
)))
7833 high
= CASE_HIGH (gimple_switch_label (stmt
, k
));
7838 return !take_default
;
7842 /* Searches the case label vector VEC for the ranges of CASE_LABELs that are
7843 used in range VR. The indices are placed in MIN_IDX1, MAX_IDX, MIN_IDX2 and
7844 MAX_IDX2. If the ranges of CASE_LABELs are empty then MAX_IDX1 < MIN_IDX1.
7845 Returns true if the default label is not needed. */
7848 find_case_label_ranges (gswitch
*stmt
, value_range_t
*vr
, size_t *min_idx1
,
7849 size_t *max_idx1
, size_t *min_idx2
,
7853 unsigned int n
= gimple_switch_num_labels (stmt
);
7855 tree case_low
, case_high
;
7856 tree min
= vr
->min
, max
= vr
->max
;
7858 gcc_checking_assert (vr
->type
== VR_RANGE
|| vr
->type
== VR_ANTI_RANGE
);
7860 take_default
= !find_case_label_range (stmt
, min
, max
, &i
, &j
);
7862 /* Set second range to emtpy. */
7866 if (vr
->type
== VR_RANGE
)
7870 return !take_default
;
7873 /* Set first range to all case labels. */
7880 /* Make sure all the values of case labels [i , j] are contained in
7881 range [MIN, MAX]. */
7882 case_low
= CASE_LOW (gimple_switch_label (stmt
, i
));
7883 case_high
= CASE_HIGH (gimple_switch_label (stmt
, j
));
7884 if (tree_int_cst_compare (case_low
, min
) < 0)
7886 if (case_high
!= NULL_TREE
7887 && tree_int_cst_compare (max
, case_high
) < 0)
7893 /* If the range spans case labels [i, j], the corresponding anti-range spans
7894 the labels [1, i - 1] and [j + 1, n - 1]. */
7920 /* Visit switch statement STMT. If we can determine which edge
7921 will be taken out of STMT's basic block, record it in
7922 *TAKEN_EDGE_P and return SSA_PROP_INTERESTING. Otherwise, return
7923 SSA_PROP_VARYING. */
7925 static enum ssa_prop_result
7926 vrp_visit_switch_stmt (gswitch
*stmt
, edge
*taken_edge_p
)
7930 size_t i
= 0, j
= 0, k
, l
;
7933 *taken_edge_p
= NULL
;
7934 op
= gimple_switch_index (stmt
);
7935 if (TREE_CODE (op
) != SSA_NAME
)
7936 return SSA_PROP_VARYING
;
7938 vr
= get_value_range (op
);
7939 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7941 fprintf (dump_file
, "\nVisiting switch expression with operand ");
7942 print_generic_expr (dump_file
, op
, 0);
7943 fprintf (dump_file
, " with known range ");
7944 dump_value_range (dump_file
, vr
);
7945 fprintf (dump_file
, "\n");
7948 if ((vr
->type
!= VR_RANGE
7949 && vr
->type
!= VR_ANTI_RANGE
)
7950 || symbolic_range_p (vr
))
7951 return SSA_PROP_VARYING
;
7953 /* Find the single edge that is taken from the switch expression. */
7954 take_default
= !find_case_label_ranges (stmt
, vr
, &i
, &j
, &k
, &l
);
7956 /* Check if the range spans no CASE_LABEL. If so, we only reach the default
7960 gcc_assert (take_default
);
7961 val
= gimple_switch_default_label (stmt
);
7965 /* Check if labels with index i to j and maybe the default label
7966 are all reaching the same label. */
7968 val
= gimple_switch_label (stmt
, i
);
7970 && CASE_LABEL (gimple_switch_default_label (stmt
))
7971 != CASE_LABEL (val
))
7973 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7974 fprintf (dump_file
, " not a single destination for this "
7976 return SSA_PROP_VARYING
;
7978 for (++i
; i
<= j
; ++i
)
7980 if (CASE_LABEL (gimple_switch_label (stmt
, i
)) != CASE_LABEL (val
))
7982 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7983 fprintf (dump_file
, " not a single destination for this "
7985 return SSA_PROP_VARYING
;
7990 if (CASE_LABEL (gimple_switch_label (stmt
, k
)) != CASE_LABEL (val
))
7992 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7993 fprintf (dump_file
, " not a single destination for this "
7995 return SSA_PROP_VARYING
;
8000 *taken_edge_p
= find_edge (gimple_bb (stmt
),
8001 label_to_block (CASE_LABEL (val
)));
8003 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8005 fprintf (dump_file
, " will take edge to ");
8006 print_generic_stmt (dump_file
, CASE_LABEL (val
), 0);
8009 return SSA_PROP_INTERESTING
;
8013 /* Evaluate statement STMT. If the statement produces a useful range,
8014 return SSA_PROP_INTERESTING and record the SSA name with the
8015 interesting range into *OUTPUT_P.
8017 If STMT is a conditional branch and we can determine its truth
8018 value, the taken edge is recorded in *TAKEN_EDGE_P.
8020 If STMT produces a varying value, return SSA_PROP_VARYING. */
8022 static enum ssa_prop_result
8023 vrp_visit_stmt (gimple stmt
, edge
*taken_edge_p
, tree
*output_p
)
8028 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8030 fprintf (dump_file
, "\nVisiting statement:\n");
8031 print_gimple_stmt (dump_file
, stmt
, 0, dump_flags
);
8034 if (!stmt_interesting_for_vrp (stmt
))
8035 gcc_assert (stmt_ends_bb_p (stmt
));
8036 else if (is_gimple_assign (stmt
) || is_gimple_call (stmt
))
8037 return vrp_visit_assignment_or_call (stmt
, output_p
);
8038 else if (gimple_code (stmt
) == GIMPLE_COND
)
8039 return vrp_visit_cond_stmt (as_a
<gcond
*> (stmt
), taken_edge_p
);
8040 else if (gimple_code (stmt
) == GIMPLE_SWITCH
)
8041 return vrp_visit_switch_stmt (as_a
<gswitch
*> (stmt
), taken_edge_p
);
8043 /* All other statements produce nothing of interest for VRP, so mark
8044 their outputs varying and prevent further simulation. */
8045 FOR_EACH_SSA_TREE_OPERAND (def
, stmt
, iter
, SSA_OP_DEF
)
8046 set_value_range_to_varying (get_value_range (def
));
8048 return SSA_PROP_VARYING
;
8051 /* Union the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
8052 { VR1TYPE, VR0MIN, VR0MAX } and store the result
8053 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
8054 possible such range. The resulting range is not canonicalized. */
8057 union_ranges (enum value_range_type
*vr0type
,
8058 tree
*vr0min
, tree
*vr0max
,
8059 enum value_range_type vr1type
,
8060 tree vr1min
, tree vr1max
)
8062 bool mineq
= operand_equal_p (*vr0min
, vr1min
, 0);
8063 bool maxeq
= operand_equal_p (*vr0max
, vr1max
, 0);
8065 /* [] is vr0, () is vr1 in the following classification comments. */
8069 if (*vr0type
== vr1type
)
8070 /* Nothing to do for equal ranges. */
8072 else if ((*vr0type
== VR_RANGE
8073 && vr1type
== VR_ANTI_RANGE
)
8074 || (*vr0type
== VR_ANTI_RANGE
8075 && vr1type
== VR_RANGE
))
8077 /* For anti-range with range union the result is varying. */
8083 else if (operand_less_p (*vr0max
, vr1min
) == 1
8084 || operand_less_p (vr1max
, *vr0min
) == 1)
8086 /* [ ] ( ) or ( ) [ ]
8087 If the ranges have an empty intersection, result of the union
8088 operation is the anti-range or if both are anti-ranges
8090 if (*vr0type
== VR_ANTI_RANGE
8091 && vr1type
== VR_ANTI_RANGE
)
8093 else if (*vr0type
== VR_ANTI_RANGE
8094 && vr1type
== VR_RANGE
)
8096 else if (*vr0type
== VR_RANGE
8097 && vr1type
== VR_ANTI_RANGE
)
8103 else if (*vr0type
== VR_RANGE
8104 && vr1type
== VR_RANGE
)
8106 /* The result is the convex hull of both ranges. */
8107 if (operand_less_p (*vr0max
, vr1min
) == 1)
8109 /* If the result can be an anti-range, create one. */
8110 if (TREE_CODE (*vr0max
) == INTEGER_CST
8111 && TREE_CODE (vr1min
) == INTEGER_CST
8112 && vrp_val_is_min (*vr0min
)
8113 && vrp_val_is_max (vr1max
))
8115 tree min
= int_const_binop (PLUS_EXPR
,
8117 build_int_cst (TREE_TYPE (*vr0max
), 1));
8118 tree max
= int_const_binop (MINUS_EXPR
,
8120 build_int_cst (TREE_TYPE (vr1min
), 1));
8121 if (!operand_less_p (max
, min
))
8123 *vr0type
= VR_ANTI_RANGE
;
8135 /* If the result can be an anti-range, create one. */
8136 if (TREE_CODE (vr1max
) == INTEGER_CST
8137 && TREE_CODE (*vr0min
) == INTEGER_CST
8138 && vrp_val_is_min (vr1min
)
8139 && vrp_val_is_max (*vr0max
))
8141 tree min
= int_const_binop (PLUS_EXPR
,
8143 build_int_cst (TREE_TYPE (vr1max
), 1));
8144 tree max
= int_const_binop (MINUS_EXPR
,
8146 build_int_cst (TREE_TYPE (*vr0min
), 1));
8147 if (!operand_less_p (max
, min
))
8149 *vr0type
= VR_ANTI_RANGE
;
8163 else if ((maxeq
|| operand_less_p (vr1max
, *vr0max
) == 1)
8164 && (mineq
|| operand_less_p (*vr0min
, vr1min
) == 1))
8166 /* [ ( ) ] or [( ) ] or [ ( )] */
8167 if (*vr0type
== VR_RANGE
8168 && vr1type
== VR_RANGE
)
8170 else if (*vr0type
== VR_ANTI_RANGE
8171 && vr1type
== VR_ANTI_RANGE
)
8177 else if (*vr0type
== VR_ANTI_RANGE
8178 && vr1type
== VR_RANGE
)
8180 /* Arbitrarily choose the right or left gap. */
8181 if (!mineq
&& TREE_CODE (vr1min
) == INTEGER_CST
)
8182 *vr0max
= int_const_binop (MINUS_EXPR
, vr1min
,
8183 build_int_cst (TREE_TYPE (vr1min
), 1));
8184 else if (!maxeq
&& TREE_CODE (vr1max
) == INTEGER_CST
)
8185 *vr0min
= int_const_binop (PLUS_EXPR
, vr1max
,
8186 build_int_cst (TREE_TYPE (vr1max
), 1));
8190 else if (*vr0type
== VR_RANGE
8191 && vr1type
== VR_ANTI_RANGE
)
8192 /* The result covers everything. */
8197 else if ((maxeq
|| operand_less_p (*vr0max
, vr1max
) == 1)
8198 && (mineq
|| operand_less_p (vr1min
, *vr0min
) == 1))
8200 /* ( [ ] ) or ([ ] ) or ( [ ]) */
8201 if (*vr0type
== VR_RANGE
8202 && vr1type
== VR_RANGE
)
8208 else if (*vr0type
== VR_ANTI_RANGE
8209 && vr1type
== VR_ANTI_RANGE
)
8211 else if (*vr0type
== VR_RANGE
8212 && vr1type
== VR_ANTI_RANGE
)
8214 *vr0type
= VR_ANTI_RANGE
;
8215 if (!mineq
&& TREE_CODE (*vr0min
) == INTEGER_CST
)
8217 *vr0max
= int_const_binop (MINUS_EXPR
, *vr0min
,
8218 build_int_cst (TREE_TYPE (*vr0min
), 1));
8221 else if (!maxeq
&& TREE_CODE (*vr0max
) == INTEGER_CST
)
8223 *vr0min
= int_const_binop (PLUS_EXPR
, *vr0max
,
8224 build_int_cst (TREE_TYPE (*vr0max
), 1));
8230 else if (*vr0type
== VR_ANTI_RANGE
8231 && vr1type
== VR_RANGE
)
8232 /* The result covers everything. */
8237 else if ((operand_less_p (vr1min
, *vr0max
) == 1
8238 || operand_equal_p (vr1min
, *vr0max
, 0))
8239 && operand_less_p (*vr0min
, vr1min
) == 1
8240 && operand_less_p (*vr0max
, vr1max
) == 1)
8242 /* [ ( ] ) or [ ]( ) */
8243 if (*vr0type
== VR_RANGE
8244 && vr1type
== VR_RANGE
)
8246 else if (*vr0type
== VR_ANTI_RANGE
8247 && vr1type
== VR_ANTI_RANGE
)
8249 else if (*vr0type
== VR_ANTI_RANGE
8250 && vr1type
== VR_RANGE
)
8252 if (TREE_CODE (vr1min
) == INTEGER_CST
)
8253 *vr0max
= int_const_binop (MINUS_EXPR
, vr1min
,
8254 build_int_cst (TREE_TYPE (vr1min
), 1));
8258 else if (*vr0type
== VR_RANGE
8259 && vr1type
== VR_ANTI_RANGE
)
8261 if (TREE_CODE (*vr0max
) == INTEGER_CST
)
8264 *vr0min
= int_const_binop (PLUS_EXPR
, *vr0max
,
8265 build_int_cst (TREE_TYPE (*vr0max
), 1));
8274 else if ((operand_less_p (*vr0min
, vr1max
) == 1
8275 || operand_equal_p (*vr0min
, vr1max
, 0))
8276 && operand_less_p (vr1min
, *vr0min
) == 1
8277 && operand_less_p (vr1max
, *vr0max
) == 1)
8279 /* ( [ ) ] or ( )[ ] */
8280 if (*vr0type
== VR_RANGE
8281 && vr1type
== VR_RANGE
)
8283 else if (*vr0type
== VR_ANTI_RANGE
8284 && vr1type
== VR_ANTI_RANGE
)
8286 else if (*vr0type
== VR_ANTI_RANGE
8287 && vr1type
== VR_RANGE
)
8289 if (TREE_CODE (vr1max
) == INTEGER_CST
)
8290 *vr0min
= int_const_binop (PLUS_EXPR
, vr1max
,
8291 build_int_cst (TREE_TYPE (vr1max
), 1));
8295 else if (*vr0type
== VR_RANGE
8296 && vr1type
== VR_ANTI_RANGE
)
8298 if (TREE_CODE (*vr0min
) == INTEGER_CST
)
8302 *vr0max
= int_const_binop (MINUS_EXPR
, *vr0min
,
8303 build_int_cst (TREE_TYPE (*vr0min
), 1));
8317 *vr0type
= VR_VARYING
;
8318 *vr0min
= NULL_TREE
;
8319 *vr0max
= NULL_TREE
;
8322 /* Intersect the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
8323 { VR1TYPE, VR0MIN, VR0MAX } and store the result
8324 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
8325 possible such range. The resulting range is not canonicalized. */
8328 intersect_ranges (enum value_range_type
*vr0type
,
8329 tree
*vr0min
, tree
*vr0max
,
8330 enum value_range_type vr1type
,
8331 tree vr1min
, tree vr1max
)
8333 bool mineq
= operand_equal_p (*vr0min
, vr1min
, 0);
8334 bool maxeq
= operand_equal_p (*vr0max
, vr1max
, 0);
8336 /* [] is vr0, () is vr1 in the following classification comments. */
8340 if (*vr0type
== vr1type
)
8341 /* Nothing to do for equal ranges. */
8343 else if ((*vr0type
== VR_RANGE
8344 && vr1type
== VR_ANTI_RANGE
)
8345 || (*vr0type
== VR_ANTI_RANGE
8346 && vr1type
== VR_RANGE
))
8348 /* For anti-range with range intersection the result is empty. */
8349 *vr0type
= VR_UNDEFINED
;
8350 *vr0min
= NULL_TREE
;
8351 *vr0max
= NULL_TREE
;
8356 else if (operand_less_p (*vr0max
, vr1min
) == 1
8357 || operand_less_p (vr1max
, *vr0min
) == 1)
8359 /* [ ] ( ) or ( ) [ ]
8360 If the ranges have an empty intersection, the result of the
8361 intersect operation is the range for intersecting an
8362 anti-range with a range or empty when intersecting two ranges. */
8363 if (*vr0type
== VR_RANGE
8364 && vr1type
== VR_ANTI_RANGE
)
8366 else if (*vr0type
== VR_ANTI_RANGE
8367 && vr1type
== VR_RANGE
)
8373 else if (*vr0type
== VR_RANGE
8374 && vr1type
== VR_RANGE
)
8376 *vr0type
= VR_UNDEFINED
;
8377 *vr0min
= NULL_TREE
;
8378 *vr0max
= NULL_TREE
;
8380 else if (*vr0type
== VR_ANTI_RANGE
8381 && vr1type
== VR_ANTI_RANGE
)
8383 /* If the anti-ranges are adjacent to each other merge them. */
8384 if (TREE_CODE (*vr0max
) == INTEGER_CST
8385 && TREE_CODE (vr1min
) == INTEGER_CST
8386 && operand_less_p (*vr0max
, vr1min
) == 1
8387 && integer_onep (int_const_binop (MINUS_EXPR
,
8390 else if (TREE_CODE (vr1max
) == INTEGER_CST
8391 && TREE_CODE (*vr0min
) == INTEGER_CST
8392 && operand_less_p (vr1max
, *vr0min
) == 1
8393 && integer_onep (int_const_binop (MINUS_EXPR
,
8396 /* Else arbitrarily take VR0. */
8399 else if ((maxeq
|| operand_less_p (vr1max
, *vr0max
) == 1)
8400 && (mineq
|| operand_less_p (*vr0min
, vr1min
) == 1))
8402 /* [ ( ) ] or [( ) ] or [ ( )] */
8403 if (*vr0type
== VR_RANGE
8404 && vr1type
== VR_RANGE
)
8406 /* If both are ranges the result is the inner one. */
8411 else if (*vr0type
== VR_RANGE
8412 && vr1type
== VR_ANTI_RANGE
)
8414 /* Choose the right gap if the left one is empty. */
8417 if (TREE_CODE (vr1max
) == INTEGER_CST
)
8418 *vr0min
= int_const_binop (PLUS_EXPR
, vr1max
,
8419 build_int_cst (TREE_TYPE (vr1max
), 1));
8423 /* Choose the left gap if the right one is empty. */
8426 if (TREE_CODE (vr1min
) == INTEGER_CST
)
8427 *vr0max
= int_const_binop (MINUS_EXPR
, vr1min
,
8428 build_int_cst (TREE_TYPE (vr1min
), 1));
8432 /* Choose the anti-range if the range is effectively varying. */
8433 else if (vrp_val_is_min (*vr0min
)
8434 && vrp_val_is_max (*vr0max
))
8440 /* Else choose the range. */
8442 else if (*vr0type
== VR_ANTI_RANGE
8443 && vr1type
== VR_ANTI_RANGE
)
8444 /* If both are anti-ranges the result is the outer one. */
8446 else if (*vr0type
== VR_ANTI_RANGE
8447 && vr1type
== VR_RANGE
)
8449 /* The intersection is empty. */
8450 *vr0type
= VR_UNDEFINED
;
8451 *vr0min
= NULL_TREE
;
8452 *vr0max
= NULL_TREE
;
8457 else if ((maxeq
|| operand_less_p (*vr0max
, vr1max
) == 1)
8458 && (mineq
|| operand_less_p (vr1min
, *vr0min
) == 1))
8460 /* ( [ ] ) or ([ ] ) or ( [ ]) */
8461 if (*vr0type
== VR_RANGE
8462 && vr1type
== VR_RANGE
)
8463 /* Choose the inner range. */
8465 else if (*vr0type
== VR_ANTI_RANGE
8466 && vr1type
== VR_RANGE
)
8468 /* Choose the right gap if the left is empty. */
8471 *vr0type
= VR_RANGE
;
8472 if (TREE_CODE (*vr0max
) == INTEGER_CST
)
8473 *vr0min
= int_const_binop (PLUS_EXPR
, *vr0max
,
8474 build_int_cst (TREE_TYPE (*vr0max
), 1));
8479 /* Choose the left gap if the right is empty. */
8482 *vr0type
= VR_RANGE
;
8483 if (TREE_CODE (*vr0min
) == INTEGER_CST
)
8484 *vr0max
= int_const_binop (MINUS_EXPR
, *vr0min
,
8485 build_int_cst (TREE_TYPE (*vr0min
), 1));
8490 /* Choose the anti-range if the range is effectively varying. */
8491 else if (vrp_val_is_min (vr1min
)
8492 && vrp_val_is_max (vr1max
))
8494 /* Else choose the range. */
8502 else if (*vr0type
== VR_ANTI_RANGE
8503 && vr1type
== VR_ANTI_RANGE
)
8505 /* If both are anti-ranges the result is the outer one. */
8510 else if (vr1type
== VR_ANTI_RANGE
8511 && *vr0type
== VR_RANGE
)
8513 /* The intersection is empty. */
8514 *vr0type
= VR_UNDEFINED
;
8515 *vr0min
= NULL_TREE
;
8516 *vr0max
= NULL_TREE
;
8521 else if ((operand_less_p (vr1min
, *vr0max
) == 1
8522 || operand_equal_p (vr1min
, *vr0max
, 0))
8523 && operand_less_p (*vr0min
, vr1min
) == 1)
8525 /* [ ( ] ) or [ ]( ) */
8526 if (*vr0type
== VR_ANTI_RANGE
8527 && vr1type
== VR_ANTI_RANGE
)
8529 else if (*vr0type
== VR_RANGE
8530 && vr1type
== VR_RANGE
)
8532 else if (*vr0type
== VR_RANGE
8533 && vr1type
== VR_ANTI_RANGE
)
8535 if (TREE_CODE (vr1min
) == INTEGER_CST
)
8536 *vr0max
= int_const_binop (MINUS_EXPR
, vr1min
,
8537 build_int_cst (TREE_TYPE (vr1min
), 1));
8541 else if (*vr0type
== VR_ANTI_RANGE
8542 && vr1type
== VR_RANGE
)
8544 *vr0type
= VR_RANGE
;
8545 if (TREE_CODE (*vr0max
) == INTEGER_CST
)
8546 *vr0min
= int_const_binop (PLUS_EXPR
, *vr0max
,
8547 build_int_cst (TREE_TYPE (*vr0max
), 1));
8555 else if ((operand_less_p (*vr0min
, vr1max
) == 1
8556 || operand_equal_p (*vr0min
, vr1max
, 0))
8557 && operand_less_p (vr1min
, *vr0min
) == 1)
8559 /* ( [ ) ] or ( )[ ] */
8560 if (*vr0type
== VR_ANTI_RANGE
8561 && vr1type
== VR_ANTI_RANGE
)
8563 else if (*vr0type
== VR_RANGE
8564 && vr1type
== VR_RANGE
)
8566 else if (*vr0type
== VR_RANGE
8567 && vr1type
== VR_ANTI_RANGE
)
8569 if (TREE_CODE (vr1max
) == INTEGER_CST
)
8570 *vr0min
= int_const_binop (PLUS_EXPR
, vr1max
,
8571 build_int_cst (TREE_TYPE (vr1max
), 1));
8575 else if (*vr0type
== VR_ANTI_RANGE
8576 && vr1type
== VR_RANGE
)
8578 *vr0type
= VR_RANGE
;
8579 if (TREE_CODE (*vr0min
) == INTEGER_CST
)
8580 *vr0max
= int_const_binop (MINUS_EXPR
, *vr0min
,
8581 build_int_cst (TREE_TYPE (*vr0min
), 1));
8590 /* As a fallback simply use { *VRTYPE, *VR0MIN, *VR0MAX } as
8591 result for the intersection. That's always a conservative
8592 correct estimate. */
8598 /* Intersect the two value-ranges *VR0 and *VR1 and store the result
8599 in *VR0. This may not be the smallest possible such range. */
8602 vrp_intersect_ranges_1 (value_range_t
*vr0
, value_range_t
*vr1
)
8604 value_range_t saved
;
8606 /* If either range is VR_VARYING the other one wins. */
8607 if (vr1
->type
== VR_VARYING
)
8609 if (vr0
->type
== VR_VARYING
)
8611 copy_value_range (vr0
, vr1
);
8615 /* When either range is VR_UNDEFINED the resulting range is
8616 VR_UNDEFINED, too. */
8617 if (vr0
->type
== VR_UNDEFINED
)
8619 if (vr1
->type
== VR_UNDEFINED
)
8621 set_value_range_to_undefined (vr0
);
8625 /* Save the original vr0 so we can return it as conservative intersection
8626 result when our worker turns things to varying. */
8628 intersect_ranges (&vr0
->type
, &vr0
->min
, &vr0
->max
,
8629 vr1
->type
, vr1
->min
, vr1
->max
);
8630 /* Make sure to canonicalize the result though as the inversion of a
8631 VR_RANGE can still be a VR_RANGE. */
8632 set_and_canonicalize_value_range (vr0
, vr0
->type
,
8633 vr0
->min
, vr0
->max
, vr0
->equiv
);
8634 /* If that failed, use the saved original VR0. */
8635 if (vr0
->type
== VR_VARYING
)
8640 /* If the result is VR_UNDEFINED there is no need to mess with
8641 the equivalencies. */
8642 if (vr0
->type
== VR_UNDEFINED
)
8645 /* The resulting set of equivalences for range intersection is the union of
8647 if (vr0
->equiv
&& vr1
->equiv
&& vr0
->equiv
!= vr1
->equiv
)
8648 bitmap_ior_into (vr0
->equiv
, vr1
->equiv
);
8649 else if (vr1
->equiv
&& !vr0
->equiv
)
8650 bitmap_copy (vr0
->equiv
, vr1
->equiv
);
8654 vrp_intersect_ranges (value_range_t
*vr0
, value_range_t
*vr1
)
8656 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8658 fprintf (dump_file
, "Intersecting\n ");
8659 dump_value_range (dump_file
, vr0
);
8660 fprintf (dump_file
, "\nand\n ");
8661 dump_value_range (dump_file
, vr1
);
8662 fprintf (dump_file
, "\n");
8664 vrp_intersect_ranges_1 (vr0
, vr1
);
8665 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8667 fprintf (dump_file
, "to\n ");
8668 dump_value_range (dump_file
, vr0
);
8669 fprintf (dump_file
, "\n");
8673 /* Meet operation for value ranges. Given two value ranges VR0 and
8674 VR1, store in VR0 a range that contains both VR0 and VR1. This
8675 may not be the smallest possible such range. */
8678 vrp_meet_1 (value_range_t
*vr0
, value_range_t
*vr1
)
8680 value_range_t saved
;
8682 if (vr0
->type
== VR_UNDEFINED
)
8684 set_value_range (vr0
, vr1
->type
, vr1
->min
, vr1
->max
, vr1
->equiv
);
8688 if (vr1
->type
== VR_UNDEFINED
)
8690 /* VR0 already has the resulting range. */
8694 if (vr0
->type
== VR_VARYING
)
8696 /* Nothing to do. VR0 already has the resulting range. */
8700 if (vr1
->type
== VR_VARYING
)
8702 set_value_range_to_varying (vr0
);
8707 union_ranges (&vr0
->type
, &vr0
->min
, &vr0
->max
,
8708 vr1
->type
, vr1
->min
, vr1
->max
);
8709 if (vr0
->type
== VR_VARYING
)
8711 /* Failed to find an efficient meet. Before giving up and setting
8712 the result to VARYING, see if we can at least derive a useful
8713 anti-range. FIXME, all this nonsense about distinguishing
8714 anti-ranges from ranges is necessary because of the odd
8715 semantics of range_includes_zero_p and friends. */
8716 if (((saved
.type
== VR_RANGE
8717 && range_includes_zero_p (saved
.min
, saved
.max
) == 0)
8718 || (saved
.type
== VR_ANTI_RANGE
8719 && range_includes_zero_p (saved
.min
, saved
.max
) == 1))
8720 && ((vr1
->type
== VR_RANGE
8721 && range_includes_zero_p (vr1
->min
, vr1
->max
) == 0)
8722 || (vr1
->type
== VR_ANTI_RANGE
8723 && range_includes_zero_p (vr1
->min
, vr1
->max
) == 1)))
8725 set_value_range_to_nonnull (vr0
, TREE_TYPE (saved
.min
));
8727 /* Since this meet operation did not result from the meeting of
8728 two equivalent names, VR0 cannot have any equivalences. */
8730 bitmap_clear (vr0
->equiv
);
8734 set_value_range_to_varying (vr0
);
8737 set_and_canonicalize_value_range (vr0
, vr0
->type
, vr0
->min
, vr0
->max
,
8739 if (vr0
->type
== VR_VARYING
)
8742 /* The resulting set of equivalences is always the intersection of
8744 if (vr0
->equiv
&& vr1
->equiv
&& vr0
->equiv
!= vr1
->equiv
)
8745 bitmap_and_into (vr0
->equiv
, vr1
->equiv
);
8746 else if (vr0
->equiv
&& !vr1
->equiv
)
8747 bitmap_clear (vr0
->equiv
);
8751 vrp_meet (value_range_t
*vr0
, value_range_t
*vr1
)
8753 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8755 fprintf (dump_file
, "Meeting\n ");
8756 dump_value_range (dump_file
, vr0
);
8757 fprintf (dump_file
, "\nand\n ");
8758 dump_value_range (dump_file
, vr1
);
8759 fprintf (dump_file
, "\n");
8761 vrp_meet_1 (vr0
, vr1
);
8762 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8764 fprintf (dump_file
, "to\n ");
8765 dump_value_range (dump_file
, vr0
);
8766 fprintf (dump_file
, "\n");
8771 /* Visit all arguments for PHI node PHI that flow through executable
8772 edges. If a valid value range can be derived from all the incoming
8773 value ranges, set a new range for the LHS of PHI. */
8775 static enum ssa_prop_result
8776 vrp_visit_phi_node (gphi
*phi
)
8779 tree lhs
= PHI_RESULT (phi
);
8780 value_range_t
*lhs_vr
= get_value_range (lhs
);
8781 value_range_t vr_result
= VR_INITIALIZER
;
8783 int edges
, old_edges
;
8786 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8788 fprintf (dump_file
, "\nVisiting PHI node: ");
8789 print_gimple_stmt (dump_file
, phi
, 0, dump_flags
);
8793 for (i
= 0; i
< gimple_phi_num_args (phi
); i
++)
8795 edge e
= gimple_phi_arg_edge (phi
, i
);
8797 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8800 " Argument #%d (%d -> %d %sexecutable)\n",
8801 (int) i
, e
->src
->index
, e
->dest
->index
,
8802 (e
->flags
& EDGE_EXECUTABLE
) ? "" : "not ");
8805 if (e
->flags
& EDGE_EXECUTABLE
)
8807 tree arg
= PHI_ARG_DEF (phi
, i
);
8808 value_range_t vr_arg
;
8812 if (TREE_CODE (arg
) == SSA_NAME
)
8814 vr_arg
= *(get_value_range (arg
));
8815 /* Do not allow equivalences or symbolic ranges to leak in from
8816 backedges. That creates invalid equivalencies.
8817 See PR53465 and PR54767. */
8818 if (e
->flags
& EDGE_DFS_BACK
)
8820 if (vr_arg
.type
== VR_RANGE
8821 || vr_arg
.type
== VR_ANTI_RANGE
)
8823 vr_arg
.equiv
= NULL
;
8824 if (symbolic_range_p (&vr_arg
))
8826 vr_arg
.type
= VR_VARYING
;
8827 vr_arg
.min
= NULL_TREE
;
8828 vr_arg
.max
= NULL_TREE
;
8834 /* If the non-backedge arguments range is VR_VARYING then
8835 we can still try recording a simple equivalence. */
8836 if (vr_arg
.type
== VR_VARYING
)
8838 vr_arg
.type
= VR_RANGE
;
8841 vr_arg
.equiv
= NULL
;
8847 if (TREE_OVERFLOW_P (arg
))
8848 arg
= drop_tree_overflow (arg
);
8850 vr_arg
.type
= VR_RANGE
;
8853 vr_arg
.equiv
= NULL
;
8856 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8858 fprintf (dump_file
, "\t");
8859 print_generic_expr (dump_file
, arg
, dump_flags
);
8860 fprintf (dump_file
, ": ");
8861 dump_value_range (dump_file
, &vr_arg
);
8862 fprintf (dump_file
, "\n");
8866 copy_value_range (&vr_result
, &vr_arg
);
8868 vrp_meet (&vr_result
, &vr_arg
);
8871 if (vr_result
.type
== VR_VARYING
)
8876 if (vr_result
.type
== VR_VARYING
)
8878 else if (vr_result
.type
== VR_UNDEFINED
)
8881 old_edges
= vr_phi_edge_counts
[SSA_NAME_VERSION (lhs
)];
8882 vr_phi_edge_counts
[SSA_NAME_VERSION (lhs
)] = edges
;
8884 /* To prevent infinite iterations in the algorithm, derive ranges
8885 when the new value is slightly bigger or smaller than the
8886 previous one. We don't do this if we have seen a new executable
8887 edge; this helps us avoid an overflow infinity for conditionals
8888 which are not in a loop. If the old value-range was VR_UNDEFINED
8889 use the updated range and iterate one more time. */
8891 && gimple_phi_num_args (phi
) > 1
8892 && edges
== old_edges
8893 && lhs_vr
->type
!= VR_UNDEFINED
)
8895 /* Compare old and new ranges, fall back to varying if the
8896 values are not comparable. */
8897 int cmp_min
= compare_values (lhs_vr
->min
, vr_result
.min
);
8900 int cmp_max
= compare_values (lhs_vr
->max
, vr_result
.max
);
8904 /* For non VR_RANGE or for pointers fall back to varying if
8905 the range changed. */
8906 if ((lhs_vr
->type
!= VR_RANGE
|| vr_result
.type
!= VR_RANGE
8907 || POINTER_TYPE_P (TREE_TYPE (lhs
)))
8908 && (cmp_min
!= 0 || cmp_max
!= 0))
8911 /* If the new minimum is larger than than the previous one
8912 retain the old value. If the new minimum value is smaller
8913 than the previous one and not -INF go all the way to -INF + 1.
8914 In the first case, to avoid infinite bouncing between different
8915 minimums, and in the other case to avoid iterating millions of
8916 times to reach -INF. Going to -INF + 1 also lets the following
8917 iteration compute whether there will be any overflow, at the
8918 expense of one additional iteration. */
8920 vr_result
.min
= lhs_vr
->min
;
8921 else if (cmp_min
> 0
8922 && !vrp_val_is_min (vr_result
.min
))
8924 = int_const_binop (PLUS_EXPR
,
8925 vrp_val_min (TREE_TYPE (vr_result
.min
)),
8926 build_int_cst (TREE_TYPE (vr_result
.min
), 1));
8928 /* Similarly for the maximum value. */
8930 vr_result
.max
= lhs_vr
->max
;
8931 else if (cmp_max
< 0
8932 && !vrp_val_is_max (vr_result
.max
))
8934 = int_const_binop (MINUS_EXPR
,
8935 vrp_val_max (TREE_TYPE (vr_result
.min
)),
8936 build_int_cst (TREE_TYPE (vr_result
.min
), 1));
8938 /* If we dropped either bound to +-INF then if this is a loop
8939 PHI node SCEV may known more about its value-range. */
8940 if ((cmp_min
> 0 || cmp_min
< 0
8941 || cmp_max
< 0 || cmp_max
> 0)
8942 && (l
= loop_containing_stmt (phi
))
8943 && l
->header
== gimple_bb (phi
))
8944 adjust_range_with_scev (&vr_result
, l
, phi
, lhs
);
8946 /* If we will end up with a (-INF, +INF) range, set it to
8947 VARYING. Same if the previous max value was invalid for
8948 the type and we end up with vr_result.min > vr_result.max. */
8949 if ((vrp_val_is_max (vr_result
.max
)
8950 && vrp_val_is_min (vr_result
.min
))
8951 || compare_values (vr_result
.min
,
8956 /* If the new range is different than the previous value, keep
8959 if (update_value_range (lhs
, &vr_result
))
8961 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8963 fprintf (dump_file
, "Found new range for ");
8964 print_generic_expr (dump_file
, lhs
, 0);
8965 fprintf (dump_file
, ": ");
8966 dump_value_range (dump_file
, &vr_result
);
8967 fprintf (dump_file
, "\n");
8970 if (vr_result
.type
== VR_VARYING
)
8971 return SSA_PROP_VARYING
;
8973 return SSA_PROP_INTERESTING
;
8976 /* Nothing changed, don't add outgoing edges. */
8977 return SSA_PROP_NOT_INTERESTING
;
8979 /* No match found. Set the LHS to VARYING. */
8981 set_value_range_to_varying (lhs_vr
);
8982 return SSA_PROP_VARYING
;
8985 /* Simplify boolean operations if the source is known
8986 to be already a boolean. */
8988 simplify_truth_ops_using_ranges (gimple_stmt_iterator
*gsi
, gimple stmt
)
8990 enum tree_code rhs_code
= gimple_assign_rhs_code (stmt
);
8992 bool need_conversion
;
8994 /* We handle only !=/== case here. */
8995 gcc_assert (rhs_code
== EQ_EXPR
|| rhs_code
== NE_EXPR
);
8997 op0
= gimple_assign_rhs1 (stmt
);
8998 if (!op_with_boolean_value_range_p (op0
))
9001 op1
= gimple_assign_rhs2 (stmt
);
9002 if (!op_with_boolean_value_range_p (op1
))
9005 /* Reduce number of cases to handle to NE_EXPR. As there is no
9006 BIT_XNOR_EXPR we cannot replace A == B with a single statement. */
9007 if (rhs_code
== EQ_EXPR
)
9009 if (TREE_CODE (op1
) == INTEGER_CST
)
9010 op1
= int_const_binop (BIT_XOR_EXPR
, op1
,
9011 build_int_cst (TREE_TYPE (op1
), 1));
9016 lhs
= gimple_assign_lhs (stmt
);
9018 = !useless_type_conversion_p (TREE_TYPE (lhs
), TREE_TYPE (op0
));
9020 /* Make sure to not sign-extend a 1-bit 1 when converting the result. */
9022 && !TYPE_UNSIGNED (TREE_TYPE (op0
))
9023 && TYPE_PRECISION (TREE_TYPE (op0
)) == 1
9024 && TYPE_PRECISION (TREE_TYPE (lhs
)) > 1)
9027 /* For A != 0 we can substitute A itself. */
9028 if (integer_zerop (op1
))
9029 gimple_assign_set_rhs_with_ops (gsi
,
9031 ? NOP_EXPR
: TREE_CODE (op0
), op0
);
9032 /* For A != B we substitute A ^ B. Either with conversion. */
9033 else if (need_conversion
)
9035 tree tem
= make_ssa_name (TREE_TYPE (op0
));
9037 = gimple_build_assign (tem
, BIT_XOR_EXPR
, op0
, op1
);
9038 gsi_insert_before (gsi
, newop
, GSI_SAME_STMT
);
9039 gimple_assign_set_rhs_with_ops (gsi
, NOP_EXPR
, tem
);
9043 gimple_assign_set_rhs_with_ops (gsi
, BIT_XOR_EXPR
, op0
, op1
);
9044 update_stmt (gsi_stmt (*gsi
));
9049 /* Simplify a division or modulo operator to a right shift or
9050 bitwise and if the first operand is unsigned or is greater
9051 than zero and the second operand is an exact power of two.
9052 For TRUNC_MOD_EXPR op0 % op1 with constant op1, optimize it
9053 into just op0 if op0's range is known to be a subset of
9054 [-op1 + 1, op1 - 1] for signed and [0, op1 - 1] for unsigned
9058 simplify_div_or_mod_using_ranges (gimple stmt
)
9060 enum tree_code rhs_code
= gimple_assign_rhs_code (stmt
);
9062 tree op0
= gimple_assign_rhs1 (stmt
);
9063 tree op1
= gimple_assign_rhs2 (stmt
);
9064 value_range_t
*vr
= get_value_range (op0
);
9066 if (rhs_code
== TRUNC_MOD_EXPR
9067 && TREE_CODE (op1
) == INTEGER_CST
9068 && tree_int_cst_sgn (op1
) == 1
9069 && range_int_cst_p (vr
)
9070 && tree_int_cst_lt (vr
->max
, op1
))
9072 if (TYPE_UNSIGNED (TREE_TYPE (op0
))
9073 || tree_int_cst_sgn (vr
->min
) >= 0
9074 || tree_int_cst_lt (fold_unary (NEGATE_EXPR
, TREE_TYPE (op1
), op1
),
9077 /* If op0 already has the range op0 % op1 has,
9078 then TRUNC_MOD_EXPR won't change anything. */
9079 gimple_stmt_iterator gsi
= gsi_for_stmt (stmt
);
9080 gimple_assign_set_rhs_from_tree (&gsi
, op0
);
9086 if (!integer_pow2p (op1
))
9089 if (TYPE_UNSIGNED (TREE_TYPE (op0
)))
9091 val
= integer_one_node
;
9097 val
= compare_range_with_value (GE_EXPR
, vr
, integer_zero_node
, &sop
);
9101 && integer_onep (val
)
9102 && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC
))
9104 location_t location
;
9106 if (!gimple_has_location (stmt
))
9107 location
= input_location
;
9109 location
= gimple_location (stmt
);
9110 warning_at (location
, OPT_Wstrict_overflow
,
9111 "assuming signed overflow does not occur when "
9112 "simplifying %</%> or %<%%%> to %<>>%> or %<&%>");
9116 if (val
&& integer_onep (val
))
9120 if (rhs_code
== TRUNC_DIV_EXPR
)
9122 t
= build_int_cst (integer_type_node
, tree_log2 (op1
));
9123 gimple_assign_set_rhs_code (stmt
, RSHIFT_EXPR
);
9124 gimple_assign_set_rhs1 (stmt
, op0
);
9125 gimple_assign_set_rhs2 (stmt
, t
);
9129 t
= build_int_cst (TREE_TYPE (op1
), 1);
9130 t
= int_const_binop (MINUS_EXPR
, op1
, t
);
9131 t
= fold_convert (TREE_TYPE (op0
), t
);
9133 gimple_assign_set_rhs_code (stmt
, BIT_AND_EXPR
);
9134 gimple_assign_set_rhs1 (stmt
, op0
);
9135 gimple_assign_set_rhs2 (stmt
, t
);
9145 /* If the operand to an ABS_EXPR is >= 0, then eliminate the
9146 ABS_EXPR. If the operand is <= 0, then simplify the
9147 ABS_EXPR into a NEGATE_EXPR. */
9150 simplify_abs_using_ranges (gimple stmt
)
9153 tree op
= gimple_assign_rhs1 (stmt
);
9154 tree type
= TREE_TYPE (op
);
9155 value_range_t
*vr
= get_value_range (op
);
9157 if (TYPE_UNSIGNED (type
))
9159 val
= integer_zero_node
;
9165 val
= compare_range_with_value (LE_EXPR
, vr
, integer_zero_node
, &sop
);
9169 val
= compare_range_with_value (GE_EXPR
, vr
, integer_zero_node
,
9174 if (integer_zerop (val
))
9175 val
= integer_one_node
;
9176 else if (integer_onep (val
))
9177 val
= integer_zero_node
;
9182 && (integer_onep (val
) || integer_zerop (val
)))
9184 if (sop
&& issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC
))
9186 location_t location
;
9188 if (!gimple_has_location (stmt
))
9189 location
= input_location
;
9191 location
= gimple_location (stmt
);
9192 warning_at (location
, OPT_Wstrict_overflow
,
9193 "assuming signed overflow does not occur when "
9194 "simplifying %<abs (X)%> to %<X%> or %<-X%>");
9197 gimple_assign_set_rhs1 (stmt
, op
);
9198 if (integer_onep (val
))
9199 gimple_assign_set_rhs_code (stmt
, NEGATE_EXPR
);
9201 gimple_assign_set_rhs_code (stmt
, SSA_NAME
);
9210 /* Optimize away redundant BIT_AND_EXPR and BIT_IOR_EXPR.
9211 If all the bits that are being cleared by & are already
9212 known to be zero from VR, or all the bits that are being
9213 set by | are already known to be one from VR, the bit
9214 operation is redundant. */
9217 simplify_bit_ops_using_ranges (gimple_stmt_iterator
*gsi
, gimple stmt
)
9219 tree op0
= gimple_assign_rhs1 (stmt
);
9220 tree op1
= gimple_assign_rhs2 (stmt
);
9221 tree op
= NULL_TREE
;
9222 value_range_t vr0
= VR_INITIALIZER
;
9223 value_range_t vr1
= VR_INITIALIZER
;
9224 wide_int may_be_nonzero0
, may_be_nonzero1
;
9225 wide_int must_be_nonzero0
, must_be_nonzero1
;
9228 if (TREE_CODE (op0
) == SSA_NAME
)
9229 vr0
= *(get_value_range (op0
));
9230 else if (is_gimple_min_invariant (op0
))
9231 set_value_range_to_value (&vr0
, op0
, NULL
);
9235 if (TREE_CODE (op1
) == SSA_NAME
)
9236 vr1
= *(get_value_range (op1
));
9237 else if (is_gimple_min_invariant (op1
))
9238 set_value_range_to_value (&vr1
, op1
, NULL
);
9242 if (!zero_nonzero_bits_from_vr (TREE_TYPE (op0
), &vr0
, &may_be_nonzero0
,
9245 if (!zero_nonzero_bits_from_vr (TREE_TYPE (op1
), &vr1
, &may_be_nonzero1
,
9249 switch (gimple_assign_rhs_code (stmt
))
9252 mask
= may_be_nonzero0
.and_not (must_be_nonzero1
);
9258 mask
= may_be_nonzero1
.and_not (must_be_nonzero0
);
9266 mask
= may_be_nonzero0
.and_not (must_be_nonzero1
);
9272 mask
= may_be_nonzero1
.and_not (must_be_nonzero0
);
9283 if (op
== NULL_TREE
)
9286 gimple_assign_set_rhs_with_ops (gsi
, TREE_CODE (op
), op
);
9287 update_stmt (gsi_stmt (*gsi
));
9291 /* We are comparing trees OP0 and OP1 using COND_CODE. OP0 has
9292 a known value range VR.
9294 If there is one and only one value which will satisfy the
9295 conditional, then return that value. Else return NULL.
9297 If signed overflow must be undefined for the value to satisfy
9298 the conditional, then set *STRICT_OVERFLOW_P to true. */
9301 test_for_singularity (enum tree_code cond_code
, tree op0
,
9302 tree op1
, value_range_t
*vr
,
9303 bool *strict_overflow_p
)
9308 /* Extract minimum/maximum values which satisfy the
9309 the conditional as it was written. */
9310 if (cond_code
== LE_EXPR
|| cond_code
== LT_EXPR
)
9312 /* This should not be negative infinity; there is no overflow
9314 min
= TYPE_MIN_VALUE (TREE_TYPE (op0
));
9317 if (cond_code
== LT_EXPR
&& !is_overflow_infinity (max
))
9319 tree one
= build_int_cst (TREE_TYPE (op0
), 1);
9320 max
= fold_build2 (MINUS_EXPR
, TREE_TYPE (op0
), max
, one
);
9322 TREE_NO_WARNING (max
) = 1;
9325 else if (cond_code
== GE_EXPR
|| cond_code
== GT_EXPR
)
9327 /* This should not be positive infinity; there is no overflow
9329 max
= TYPE_MAX_VALUE (TREE_TYPE (op0
));
9332 if (cond_code
== GT_EXPR
&& !is_overflow_infinity (min
))
9334 tree one
= build_int_cst (TREE_TYPE (op0
), 1);
9335 min
= fold_build2 (PLUS_EXPR
, TREE_TYPE (op0
), min
, one
);
9337 TREE_NO_WARNING (min
) = 1;
9341 /* Now refine the minimum and maximum values using any
9342 value range information we have for op0. */
9345 if (compare_values (vr
->min
, min
) == 1)
9347 if (compare_values (vr
->max
, max
) == -1)
9350 /* If the new min/max values have converged to a single value,
9351 then there is only one value which can satisfy the condition,
9352 return that value. */
9353 if (operand_equal_p (min
, max
, 0) && is_gimple_min_invariant (min
))
9355 if ((cond_code
== LE_EXPR
|| cond_code
== LT_EXPR
)
9356 && is_overflow_infinity (vr
->max
))
9357 *strict_overflow_p
= true;
9358 if ((cond_code
== GE_EXPR
|| cond_code
== GT_EXPR
)
9359 && is_overflow_infinity (vr
->min
))
9360 *strict_overflow_p
= true;
9368 /* Return whether the value range *VR fits in an integer type specified
9369 by PRECISION and UNSIGNED_P. */
9372 range_fits_type_p (value_range_t
*vr
, unsigned dest_precision
, signop dest_sgn
)
9375 unsigned src_precision
;
9379 /* We can only handle integral and pointer types. */
9380 src_type
= TREE_TYPE (vr
->min
);
9381 if (!INTEGRAL_TYPE_P (src_type
)
9382 && !POINTER_TYPE_P (src_type
))
9385 /* An extension is fine unless VR is SIGNED and dest_sgn is UNSIGNED,
9386 and so is an identity transform. */
9387 src_precision
= TYPE_PRECISION (TREE_TYPE (vr
->min
));
9388 src_sgn
= TYPE_SIGN (src_type
);
9389 if ((src_precision
< dest_precision
9390 && !(dest_sgn
== UNSIGNED
&& src_sgn
== SIGNED
))
9391 || (src_precision
== dest_precision
&& src_sgn
== dest_sgn
))
9394 /* Now we can only handle ranges with constant bounds. */
9395 if (vr
->type
!= VR_RANGE
9396 || TREE_CODE (vr
->min
) != INTEGER_CST
9397 || TREE_CODE (vr
->max
) != INTEGER_CST
)
9400 /* For sign changes, the MSB of the wide_int has to be clear.
9401 An unsigned value with its MSB set cannot be represented by
9402 a signed wide_int, while a negative value cannot be represented
9403 by an unsigned wide_int. */
9404 if (src_sgn
!= dest_sgn
9405 && (wi::lts_p (vr
->min
, 0) || wi::lts_p (vr
->max
, 0)))
9408 /* Then we can perform the conversion on both ends and compare
9409 the result for equality. */
9410 tem
= wi::ext (wi::to_widest (vr
->min
), dest_precision
, dest_sgn
);
9411 if (tem
!= wi::to_widest (vr
->min
))
9413 tem
= wi::ext (wi::to_widest (vr
->max
), dest_precision
, dest_sgn
);
9414 if (tem
!= wi::to_widest (vr
->max
))
9420 /* Simplify a conditional using a relational operator to an equality
9421 test if the range information indicates only one value can satisfy
9422 the original conditional. */
9425 simplify_cond_using_ranges (gcond
*stmt
)
9427 tree op0
= gimple_cond_lhs (stmt
);
9428 tree op1
= gimple_cond_rhs (stmt
);
9429 enum tree_code cond_code
= gimple_cond_code (stmt
);
9431 if (cond_code
!= NE_EXPR
9432 && cond_code
!= EQ_EXPR
9433 && TREE_CODE (op0
) == SSA_NAME
9434 && INTEGRAL_TYPE_P (TREE_TYPE (op0
))
9435 && is_gimple_min_invariant (op1
))
9437 value_range_t
*vr
= get_value_range (op0
);
9439 /* If we have range information for OP0, then we might be
9440 able to simplify this conditional. */
9441 if (vr
->type
== VR_RANGE
)
9443 enum warn_strict_overflow_code wc
= WARN_STRICT_OVERFLOW_COMPARISON
;
9445 tree new_tree
= test_for_singularity (cond_code
, op0
, op1
, vr
, &sop
);
9448 && (!sop
|| TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0
))))
9452 fprintf (dump_file
, "Simplified relational ");
9453 print_gimple_stmt (dump_file
, stmt
, 0, 0);
9454 fprintf (dump_file
, " into ");
9457 gimple_cond_set_code (stmt
, EQ_EXPR
);
9458 gimple_cond_set_lhs (stmt
, op0
);
9459 gimple_cond_set_rhs (stmt
, new_tree
);
9465 print_gimple_stmt (dump_file
, stmt
, 0, 0);
9466 fprintf (dump_file
, "\n");
9469 if (sop
&& issue_strict_overflow_warning (wc
))
9471 location_t location
= input_location
;
9472 if (gimple_has_location (stmt
))
9473 location
= gimple_location (stmt
);
9475 warning_at (location
, OPT_Wstrict_overflow
,
9476 "assuming signed overflow does not occur when "
9477 "simplifying conditional");
9483 /* Try again after inverting the condition. We only deal
9484 with integral types here, so no need to worry about
9485 issues with inverting FP comparisons. */
9487 new_tree
= test_for_singularity
9488 (invert_tree_comparison (cond_code
, false),
9489 op0
, op1
, vr
, &sop
);
9492 && (!sop
|| TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0
))))
9496 fprintf (dump_file
, "Simplified relational ");
9497 print_gimple_stmt (dump_file
, stmt
, 0, 0);
9498 fprintf (dump_file
, " into ");
9501 gimple_cond_set_code (stmt
, NE_EXPR
);
9502 gimple_cond_set_lhs (stmt
, op0
);
9503 gimple_cond_set_rhs (stmt
, new_tree
);
9509 print_gimple_stmt (dump_file
, stmt
, 0, 0);
9510 fprintf (dump_file
, "\n");
9513 if (sop
&& issue_strict_overflow_warning (wc
))
9515 location_t location
= input_location
;
9516 if (gimple_has_location (stmt
))
9517 location
= gimple_location (stmt
);
9519 warning_at (location
, OPT_Wstrict_overflow
,
9520 "assuming signed overflow does not occur when "
9521 "simplifying conditional");
9529 /* If we have a comparison of an SSA_NAME (OP0) against a constant,
9530 see if OP0 was set by a type conversion where the source of
9531 the conversion is another SSA_NAME with a range that fits
9532 into the range of OP0's type.
9534 If so, the conversion is redundant as the earlier SSA_NAME can be
9535 used for the comparison directly if we just massage the constant in the
9537 if (TREE_CODE (op0
) == SSA_NAME
9538 && TREE_CODE (op1
) == INTEGER_CST
)
9540 gimple def_stmt
= SSA_NAME_DEF_STMT (op0
);
9543 if (!is_gimple_assign (def_stmt
)
9544 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt
)))
9547 innerop
= gimple_assign_rhs1 (def_stmt
);
9549 if (TREE_CODE (innerop
) == SSA_NAME
9550 && !POINTER_TYPE_P (TREE_TYPE (innerop
)))
9552 value_range_t
*vr
= get_value_range (innerop
);
9554 if (range_int_cst_p (vr
)
9555 && range_fits_type_p (vr
,
9556 TYPE_PRECISION (TREE_TYPE (op0
)),
9557 TYPE_SIGN (TREE_TYPE (op0
)))
9558 && int_fits_type_p (op1
, TREE_TYPE (innerop
))
9559 /* The range must not have overflowed, or if it did overflow
9560 we must not be wrapping/trapping overflow and optimizing
9561 with strict overflow semantics. */
9562 && ((!is_negative_overflow_infinity (vr
->min
)
9563 && !is_positive_overflow_infinity (vr
->max
))
9564 || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (innerop
))))
9566 /* If the range overflowed and the user has asked for warnings
9567 when strict overflow semantics were used to optimize code,
9568 issue an appropriate warning. */
9569 if (cond_code
!= EQ_EXPR
&& cond_code
!= NE_EXPR
9570 && (is_negative_overflow_infinity (vr
->min
)
9571 || is_positive_overflow_infinity (vr
->max
))
9572 && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_CONDITIONAL
))
9574 location_t location
;
9576 if (!gimple_has_location (stmt
))
9577 location
= input_location
;
9579 location
= gimple_location (stmt
);
9580 warning_at (location
, OPT_Wstrict_overflow
,
9581 "assuming signed overflow does not occur when "
9582 "simplifying conditional");
9585 tree newconst
= fold_convert (TREE_TYPE (innerop
), op1
);
9586 gimple_cond_set_lhs (stmt
, innerop
);
9587 gimple_cond_set_rhs (stmt
, newconst
);
9596 /* Simplify a switch statement using the value range of the switch
9600 simplify_switch_using_ranges (gswitch
*stmt
)
9602 tree op
= gimple_switch_index (stmt
);
9607 size_t i
= 0, j
= 0, n
, n2
;
9610 size_t k
= 1, l
= 0;
9612 if (TREE_CODE (op
) == SSA_NAME
)
9614 vr
= get_value_range (op
);
9616 /* We can only handle integer ranges. */
9617 if ((vr
->type
!= VR_RANGE
9618 && vr
->type
!= VR_ANTI_RANGE
)
9619 || symbolic_range_p (vr
))
9622 /* Find case label for min/max of the value range. */
9623 take_default
= !find_case_label_ranges (stmt
, vr
, &i
, &j
, &k
, &l
);
9625 else if (TREE_CODE (op
) == INTEGER_CST
)
9627 take_default
= !find_case_label_index (stmt
, 1, op
, &i
);
9641 n
= gimple_switch_num_labels (stmt
);
9643 /* Bail out if this is just all edges taken. */
9649 /* Build a new vector of taken case labels. */
9650 vec2
= make_tree_vec (j
- i
+ 1 + l
- k
+ 1 + (int)take_default
);
9653 /* Add the default edge, if necessary. */
9655 TREE_VEC_ELT (vec2
, n2
++) = gimple_switch_default_label (stmt
);
9657 for (; i
<= j
; ++i
, ++n2
)
9658 TREE_VEC_ELT (vec2
, n2
) = gimple_switch_label (stmt
, i
);
9660 for (; k
<= l
; ++k
, ++n2
)
9661 TREE_VEC_ELT (vec2
, n2
) = gimple_switch_label (stmt
, k
);
9663 /* Mark needed edges. */
9664 for (i
= 0; i
< n2
; ++i
)
9666 e
= find_edge (gimple_bb (stmt
),
9667 label_to_block (CASE_LABEL (TREE_VEC_ELT (vec2
, i
))));
9668 e
->aux
= (void *)-1;
9671 /* Queue not needed edges for later removal. */
9672 FOR_EACH_EDGE (e
, ei
, gimple_bb (stmt
)->succs
)
9674 if (e
->aux
== (void *)-1)
9680 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
9682 fprintf (dump_file
, "removing unreachable case label\n");
9684 to_remove_edges
.safe_push (e
);
9685 e
->flags
&= ~EDGE_EXECUTABLE
;
9688 /* And queue an update for the stmt. */
9691 to_update_switch_stmts
.safe_push (su
);
9695 /* Simplify an integral conversion from an SSA name in STMT. */
9698 simplify_conversion_using_ranges (gimple stmt
)
9700 tree innerop
, middleop
, finaltype
;
9702 value_range_t
*innervr
;
9703 signop inner_sgn
, middle_sgn
, final_sgn
;
9704 unsigned inner_prec
, middle_prec
, final_prec
;
9705 widest_int innermin
, innermed
, innermax
, middlemin
, middlemed
, middlemax
;
9707 finaltype
= TREE_TYPE (gimple_assign_lhs (stmt
));
9708 if (!INTEGRAL_TYPE_P (finaltype
))
9710 middleop
= gimple_assign_rhs1 (stmt
);
9711 def_stmt
= SSA_NAME_DEF_STMT (middleop
);
9712 if (!is_gimple_assign (def_stmt
)
9713 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt
)))
9715 innerop
= gimple_assign_rhs1 (def_stmt
);
9716 if (TREE_CODE (innerop
) != SSA_NAME
9717 || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (innerop
))
9720 /* Get the value-range of the inner operand. */
9721 innervr
= get_value_range (innerop
);
9722 if (innervr
->type
!= VR_RANGE
9723 || TREE_CODE (innervr
->min
) != INTEGER_CST
9724 || TREE_CODE (innervr
->max
) != INTEGER_CST
)
9727 /* Simulate the conversion chain to check if the result is equal if
9728 the middle conversion is removed. */
9729 innermin
= wi::to_widest (innervr
->min
);
9730 innermax
= wi::to_widest (innervr
->max
);
9732 inner_prec
= TYPE_PRECISION (TREE_TYPE (innerop
));
9733 middle_prec
= TYPE_PRECISION (TREE_TYPE (middleop
));
9734 final_prec
= TYPE_PRECISION (finaltype
);
9736 /* If the first conversion is not injective, the second must not
9738 if (wi::gtu_p (innermax
- innermin
,
9739 wi::mask
<widest_int
> (middle_prec
, false))
9740 && middle_prec
< final_prec
)
9742 /* We also want a medium value so that we can track the effect that
9743 narrowing conversions with sign change have. */
9744 inner_sgn
= TYPE_SIGN (TREE_TYPE (innerop
));
9745 if (inner_sgn
== UNSIGNED
)
9746 innermed
= wi::shifted_mask
<widest_int
> (1, inner_prec
- 1, false);
9749 if (wi::cmp (innermin
, innermed
, inner_sgn
) >= 0
9750 || wi::cmp (innermed
, innermax
, inner_sgn
) >= 0)
9751 innermed
= innermin
;
9753 middle_sgn
= TYPE_SIGN (TREE_TYPE (middleop
));
9754 middlemin
= wi::ext (innermin
, middle_prec
, middle_sgn
);
9755 middlemed
= wi::ext (innermed
, middle_prec
, middle_sgn
);
9756 middlemax
= wi::ext (innermax
, middle_prec
, middle_sgn
);
9758 /* Require that the final conversion applied to both the original
9759 and the intermediate range produces the same result. */
9760 final_sgn
= TYPE_SIGN (finaltype
);
9761 if (wi::ext (middlemin
, final_prec
, final_sgn
)
9762 != wi::ext (innermin
, final_prec
, final_sgn
)
9763 || wi::ext (middlemed
, final_prec
, final_sgn
)
9764 != wi::ext (innermed
, final_prec
, final_sgn
)
9765 || wi::ext (middlemax
, final_prec
, final_sgn
)
9766 != wi::ext (innermax
, final_prec
, final_sgn
))
9769 gimple_assign_set_rhs1 (stmt
, innerop
);
9774 /* Simplify a conversion from integral SSA name to float in STMT. */
9777 simplify_float_conversion_using_ranges (gimple_stmt_iterator
*gsi
, gimple stmt
)
9779 tree rhs1
= gimple_assign_rhs1 (stmt
);
9780 value_range_t
*vr
= get_value_range (rhs1
);
9781 machine_mode fltmode
= TYPE_MODE (TREE_TYPE (gimple_assign_lhs (stmt
)));
9786 /* We can only handle constant ranges. */
9787 if (vr
->type
!= VR_RANGE
9788 || TREE_CODE (vr
->min
) != INTEGER_CST
9789 || TREE_CODE (vr
->max
) != INTEGER_CST
)
9792 /* First check if we can use a signed type in place of an unsigned. */
9793 if (TYPE_UNSIGNED (TREE_TYPE (rhs1
))
9794 && (can_float_p (fltmode
, TYPE_MODE (TREE_TYPE (rhs1
)), 0)
9795 != CODE_FOR_nothing
)
9796 && range_fits_type_p (vr
, TYPE_PRECISION (TREE_TYPE (rhs1
)), SIGNED
))
9797 mode
= TYPE_MODE (TREE_TYPE (rhs1
));
9798 /* If we can do the conversion in the current input mode do nothing. */
9799 else if (can_float_p (fltmode
, TYPE_MODE (TREE_TYPE (rhs1
)),
9800 TYPE_UNSIGNED (TREE_TYPE (rhs1
))) != CODE_FOR_nothing
)
9802 /* Otherwise search for a mode we can use, starting from the narrowest
9803 integer mode available. */
9806 mode
= GET_CLASS_NARROWEST_MODE (MODE_INT
);
9809 /* If we cannot do a signed conversion to float from mode
9810 or if the value-range does not fit in the signed type
9811 try with a wider mode. */
9812 if (can_float_p (fltmode
, mode
, 0) != CODE_FOR_nothing
9813 && range_fits_type_p (vr
, GET_MODE_PRECISION (mode
), SIGNED
))
9816 mode
= GET_MODE_WIDER_MODE (mode
);
9817 /* But do not widen the input. Instead leave that to the
9818 optabs expansion code. */
9819 if (GET_MODE_PRECISION (mode
) > TYPE_PRECISION (TREE_TYPE (rhs1
)))
9822 while (mode
!= VOIDmode
);
9823 if (mode
== VOIDmode
)
9827 /* It works, insert a truncation or sign-change before the
9828 float conversion. */
9829 tem
= make_ssa_name (build_nonstandard_integer_type
9830 (GET_MODE_PRECISION (mode
), 0));
9831 conv
= gimple_build_assign (tem
, NOP_EXPR
, rhs1
);
9832 gsi_insert_before (gsi
, conv
, GSI_SAME_STMT
);
9833 gimple_assign_set_rhs1 (stmt
, tem
);
9839 /* Simplify an internal fn call using ranges if possible. */
9842 simplify_internal_call_using_ranges (gimple_stmt_iterator
*gsi
, gimple stmt
)
9844 enum tree_code subcode
;
9845 bool is_ubsan
= false;
9847 switch (gimple_call_internal_fn (stmt
))
9849 case IFN_UBSAN_CHECK_ADD
:
9850 subcode
= PLUS_EXPR
;
9853 case IFN_UBSAN_CHECK_SUB
:
9854 subcode
= MINUS_EXPR
;
9857 case IFN_UBSAN_CHECK_MUL
:
9858 subcode
= MULT_EXPR
;
9861 case IFN_ADD_OVERFLOW
:
9862 subcode
= PLUS_EXPR
;
9864 case IFN_SUB_OVERFLOW
:
9865 subcode
= MINUS_EXPR
;
9867 case IFN_MUL_OVERFLOW
:
9868 subcode
= MULT_EXPR
;
9874 tree op0
= gimple_call_arg (stmt
, 0);
9875 tree op1
= gimple_call_arg (stmt
, 1);
9878 type
= TREE_TYPE (op0
);
9879 else if (gimple_call_lhs (stmt
) == NULL_TREE
)
9882 type
= TREE_TYPE (TREE_TYPE (gimple_call_lhs (stmt
)));
9883 if (!check_for_binary_op_overflow (subcode
, type
, op0
, op1
, &ovf
)
9884 || (is_ubsan
&& ovf
))
9888 location_t loc
= gimple_location (stmt
);
9890 g
= gimple_build_assign (gimple_call_lhs (stmt
), subcode
, op0
, op1
);
9893 int prec
= TYPE_PRECISION (type
);
9896 || !useless_type_conversion_p (type
, TREE_TYPE (op0
))
9897 || !useless_type_conversion_p (type
, TREE_TYPE (op1
)))
9898 utype
= build_nonstandard_integer_type (prec
, 1);
9899 if (TREE_CODE (op0
) == INTEGER_CST
)
9900 op0
= fold_convert (utype
, op0
);
9901 else if (!useless_type_conversion_p (utype
, TREE_TYPE (op0
)))
9903 g
= gimple_build_assign (make_ssa_name (utype
), NOP_EXPR
, op0
);
9904 gimple_set_location (g
, loc
);
9905 gsi_insert_before (gsi
, g
, GSI_SAME_STMT
);
9906 op0
= gimple_assign_lhs (g
);
9908 if (TREE_CODE (op1
) == INTEGER_CST
)
9909 op1
= fold_convert (utype
, op1
);
9910 else if (!useless_type_conversion_p (utype
, TREE_TYPE (op1
)))
9912 g
= gimple_build_assign (make_ssa_name (utype
), NOP_EXPR
, op1
);
9913 gimple_set_location (g
, loc
);
9914 gsi_insert_before (gsi
, g
, GSI_SAME_STMT
);
9915 op1
= gimple_assign_lhs (g
);
9917 g
= gimple_build_assign (make_ssa_name (utype
), subcode
, op0
, op1
);
9918 gimple_set_location (g
, loc
);
9919 gsi_insert_before (gsi
, g
, GSI_SAME_STMT
);
9922 g
= gimple_build_assign (make_ssa_name (type
), NOP_EXPR
,
9923 gimple_assign_lhs (g
));
9924 gimple_set_location (g
, loc
);
9925 gsi_insert_before (gsi
, g
, GSI_SAME_STMT
);
9927 g
= gimple_build_assign (gimple_call_lhs (stmt
), COMPLEX_EXPR
,
9928 gimple_assign_lhs (g
),
9929 build_int_cst (type
, ovf
));
9931 gimple_set_location (g
, loc
);
9932 gsi_replace (gsi
, g
, false);
9936 /* Simplify STMT using ranges if possible. */
9939 simplify_stmt_using_ranges (gimple_stmt_iterator
*gsi
)
9941 gimple stmt
= gsi_stmt (*gsi
);
9942 if (is_gimple_assign (stmt
))
9944 enum tree_code rhs_code
= gimple_assign_rhs_code (stmt
);
9945 tree rhs1
= gimple_assign_rhs1 (stmt
);
9951 /* Transform EQ_EXPR, NE_EXPR into BIT_XOR_EXPR or identity
9952 if the RHS is zero or one, and the LHS are known to be boolean
9954 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
9955 return simplify_truth_ops_using_ranges (gsi
, stmt
);
9958 /* Transform TRUNC_DIV_EXPR and TRUNC_MOD_EXPR into RSHIFT_EXPR
9959 and BIT_AND_EXPR respectively if the first operand is greater
9960 than zero and the second operand is an exact power of two.
9961 Also optimize TRUNC_MOD_EXPR away if the second operand is
9962 constant and the first operand already has the right value
9964 case TRUNC_DIV_EXPR
:
9965 case TRUNC_MOD_EXPR
:
9966 if (TREE_CODE (rhs1
) == SSA_NAME
9967 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
9968 return simplify_div_or_mod_using_ranges (stmt
);
9971 /* Transform ABS (X) into X or -X as appropriate. */
9973 if (TREE_CODE (rhs1
) == SSA_NAME
9974 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
9975 return simplify_abs_using_ranges (stmt
);
9980 /* Optimize away BIT_AND_EXPR and BIT_IOR_EXPR
9981 if all the bits being cleared are already cleared or
9982 all the bits being set are already set. */
9983 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
9984 return simplify_bit_ops_using_ranges (gsi
, stmt
);
9988 if (TREE_CODE (rhs1
) == SSA_NAME
9989 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
9990 return simplify_conversion_using_ranges (stmt
);
9994 if (TREE_CODE (rhs1
) == SSA_NAME
9995 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
9996 return simplify_float_conversion_using_ranges (gsi
, stmt
);
10003 else if (gimple_code (stmt
) == GIMPLE_COND
)
10004 return simplify_cond_using_ranges (as_a
<gcond
*> (stmt
));
10005 else if (gimple_code (stmt
) == GIMPLE_SWITCH
)
10006 return simplify_switch_using_ranges (as_a
<gswitch
*> (stmt
));
10007 else if (is_gimple_call (stmt
)
10008 && gimple_call_internal_p (stmt
))
10009 return simplify_internal_call_using_ranges (gsi
, stmt
);
10014 /* If the statement pointed by SI has a predicate whose value can be
10015 computed using the value range information computed by VRP, compute
10016 its value and return true. Otherwise, return false. */
10019 fold_predicate_in (gimple_stmt_iterator
*si
)
10021 bool assignment_p
= false;
10023 gimple stmt
= gsi_stmt (*si
);
10025 if (is_gimple_assign (stmt
)
10026 && TREE_CODE_CLASS (gimple_assign_rhs_code (stmt
)) == tcc_comparison
)
10028 assignment_p
= true;
10029 val
= vrp_evaluate_conditional (gimple_assign_rhs_code (stmt
),
10030 gimple_assign_rhs1 (stmt
),
10031 gimple_assign_rhs2 (stmt
),
10034 else if (gcond
*cond_stmt
= dyn_cast
<gcond
*> (stmt
))
10035 val
= vrp_evaluate_conditional (gimple_cond_code (cond_stmt
),
10036 gimple_cond_lhs (cond_stmt
),
10037 gimple_cond_rhs (cond_stmt
),
10045 val
= fold_convert (gimple_expr_type (stmt
), val
);
10049 fprintf (dump_file
, "Folding predicate ");
10050 print_gimple_expr (dump_file
, stmt
, 0, 0);
10051 fprintf (dump_file
, " to ");
10052 print_generic_expr (dump_file
, val
, 0);
10053 fprintf (dump_file
, "\n");
10056 if (is_gimple_assign (stmt
))
10057 gimple_assign_set_rhs_from_tree (si
, val
);
10060 gcc_assert (gimple_code (stmt
) == GIMPLE_COND
);
10061 gcond
*cond_stmt
= as_a
<gcond
*> (stmt
);
10062 if (integer_zerop (val
))
10063 gimple_cond_make_false (cond_stmt
);
10064 else if (integer_onep (val
))
10065 gimple_cond_make_true (cond_stmt
);
10067 gcc_unreachable ();
10076 /* Callback for substitute_and_fold folding the stmt at *SI. */
10079 vrp_fold_stmt (gimple_stmt_iterator
*si
)
10081 if (fold_predicate_in (si
))
10084 return simplify_stmt_using_ranges (si
);
10087 /* Unwindable const/copy equivalences. */
10088 const_and_copies
*equiv_stack
;
10090 /* A trivial wrapper so that we can present the generic jump threading
10091 code with a simple API for simplifying statements. STMT is the
10092 statement we want to simplify, WITHIN_STMT provides the location
10093 for any overflow warnings. */
10096 simplify_stmt_for_jump_threading (gimple stmt
, gimple within_stmt
)
10098 if (gcond
*cond_stmt
= dyn_cast
<gcond
*> (stmt
))
10099 return vrp_evaluate_conditional (gimple_cond_code (cond_stmt
),
10100 gimple_cond_lhs (cond_stmt
),
10101 gimple_cond_rhs (cond_stmt
),
10104 if (gassign
*assign_stmt
= dyn_cast
<gassign
*> (stmt
))
10106 value_range_t new_vr
= VR_INITIALIZER
;
10107 tree lhs
= gimple_assign_lhs (assign_stmt
);
10109 if (TREE_CODE (lhs
) == SSA_NAME
10110 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs
))
10111 || POINTER_TYPE_P (TREE_TYPE (lhs
))))
10113 extract_range_from_assignment (&new_vr
, assign_stmt
);
10114 if (range_int_cst_singleton_p (&new_vr
))
10122 /* Blocks which have more than one predecessor and more than
10123 one successor present jump threading opportunities, i.e.,
10124 when the block is reached from a specific predecessor, we
10125 may be able to determine which of the outgoing edges will
10126 be traversed. When this optimization applies, we are able
10127 to avoid conditionals at runtime and we may expose secondary
10128 optimization opportunities.
10130 This routine is effectively a driver for the generic jump
10131 threading code. It basically just presents the generic code
10132 with edges that may be suitable for jump threading.
10134 Unlike DOM, we do not iterate VRP if jump threading was successful.
10135 While iterating may expose new opportunities for VRP, it is expected
10136 those opportunities would be very limited and the compile time cost
10137 to expose those opportunities would be significant.
10139 As jump threading opportunities are discovered, they are registered
10140 for later realization. */
10143 identify_jump_threads (void)
10150 /* Ugh. When substituting values earlier in this pass we can
10151 wipe the dominance information. So rebuild the dominator
10152 information as we need it within the jump threading code. */
10153 calculate_dominance_info (CDI_DOMINATORS
);
10155 /* We do not allow VRP information to be used for jump threading
10156 across a back edge in the CFG. Otherwise it becomes too
10157 difficult to avoid eliminating loop exit tests. Of course
10158 EDGE_DFS_BACK is not accurate at this time so we have to
10160 mark_dfs_back_edges ();
10162 /* Do not thread across edges we are about to remove. Just marking
10163 them as EDGE_DFS_BACK will do. */
10164 FOR_EACH_VEC_ELT (to_remove_edges
, i
, e
)
10165 e
->flags
|= EDGE_DFS_BACK
;
10167 /* Allocate our unwinder stack to unwind any temporary equivalences
10168 that might be recorded. */
10169 equiv_stack
= new const_and_copies (dump_file
, dump_flags
);
10171 /* To avoid lots of silly node creation, we create a single
10172 conditional and just modify it in-place when attempting to
10174 dummy
= gimple_build_cond (EQ_EXPR
,
10175 integer_zero_node
, integer_zero_node
,
10178 /* Walk through all the blocks finding those which present a
10179 potential jump threading opportunity. We could set this up
10180 as a dominator walker and record data during the walk, but
10181 I doubt it's worth the effort for the classes of jump
10182 threading opportunities we are trying to identify at this
10183 point in compilation. */
10184 FOR_EACH_BB_FN (bb
, cfun
)
10188 /* If the generic jump threading code does not find this block
10189 interesting, then there is nothing to do. */
10190 if (! potentially_threadable_block (bb
))
10193 last
= last_stmt (bb
);
10195 /* We're basically looking for a switch or any kind of conditional with
10196 integral or pointer type arguments. Note the type of the second
10197 argument will be the same as the first argument, so no need to
10198 check it explicitly.
10200 We also handle the case where there are no statements in the
10201 block. This come up with forwarder blocks that are not
10202 optimized away because they lead to a loop header. But we do
10203 want to thread through them as we can sometimes thread to the
10204 loop exit which is obviously profitable. */
10206 || gimple_code (last
) == GIMPLE_SWITCH
10207 || (gimple_code (last
) == GIMPLE_COND
10208 && TREE_CODE (gimple_cond_lhs (last
)) == SSA_NAME
10209 && (INTEGRAL_TYPE_P (TREE_TYPE (gimple_cond_lhs (last
)))
10210 || POINTER_TYPE_P (TREE_TYPE (gimple_cond_lhs (last
))))
10211 && (TREE_CODE (gimple_cond_rhs (last
)) == SSA_NAME
10212 || is_gimple_min_invariant (gimple_cond_rhs (last
)))))
10216 /* We've got a block with multiple predecessors and multiple
10217 successors which also ends in a suitable conditional or
10218 switch statement. For each predecessor, see if we can thread
10219 it to a specific successor. */
10220 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
10222 /* Do not thread across back edges or abnormal edges
10224 if (e
->flags
& (EDGE_DFS_BACK
| EDGE_COMPLEX
))
10227 thread_across_edge (dummy
, e
, true, equiv_stack
,
10228 simplify_stmt_for_jump_threading
);
10233 /* We do not actually update the CFG or SSA graphs at this point as
10234 ASSERT_EXPRs are still in the IL and cfg cleanup code does not yet
10235 handle ASSERT_EXPRs gracefully. */
10238 /* We identified all the jump threading opportunities earlier, but could
10239 not transform the CFG at that time. This routine transforms the
10240 CFG and arranges for the dominator tree to be rebuilt if necessary.
10242 Note the SSA graph update will occur during the normal TODO
10243 processing by the pass manager. */
10245 finalize_jump_threads (void)
10247 thread_through_all_blocks (false);
10248 delete equiv_stack
;
10252 /* Traverse all the blocks folding conditionals with known ranges. */
10255 vrp_finalize (void)
10259 values_propagated
= true;
10263 fprintf (dump_file
, "\nValue ranges after VRP:\n\n");
10264 dump_all_value_ranges (dump_file
);
10265 fprintf (dump_file
, "\n");
10268 substitute_and_fold (op_with_constant_singleton_value_range
,
10269 vrp_fold_stmt
, false);
10271 if (warn_array_bounds
&& first_pass_instance
)
10272 check_all_array_refs ();
10274 /* We must identify jump threading opportunities before we release
10275 the datastructures built by VRP. */
10276 identify_jump_threads ();
10278 /* Set value range to non pointer SSA_NAMEs. */
10279 for (i
= 0; i
< num_vr_values
; i
++)
10282 tree name
= ssa_name (i
);
10285 || POINTER_TYPE_P (TREE_TYPE (name
))
10286 || (vr_value
[i
]->type
== VR_VARYING
)
10287 || (vr_value
[i
]->type
== VR_UNDEFINED
))
10290 if ((TREE_CODE (vr_value
[i
]->min
) == INTEGER_CST
)
10291 && (TREE_CODE (vr_value
[i
]->max
) == INTEGER_CST
)
10292 && (vr_value
[i
]->type
== VR_RANGE
10293 || vr_value
[i
]->type
== VR_ANTI_RANGE
))
10294 set_range_info (name
, vr_value
[i
]->type
, vr_value
[i
]->min
,
10298 /* Free allocated memory. */
10299 for (i
= 0; i
< num_vr_values
; i
++)
10302 BITMAP_FREE (vr_value
[i
]->equiv
);
10303 free (vr_value
[i
]);
10307 free (vr_phi_edge_counts
);
10309 /* So that we can distinguish between VRP data being available
10310 and not available. */
10312 vr_phi_edge_counts
= NULL
;
10316 /* Main entry point to VRP (Value Range Propagation). This pass is
10317 loosely based on J. R. C. Patterson, ``Accurate Static Branch
10318 Prediction by Value Range Propagation,'' in SIGPLAN Conference on
10319 Programming Language Design and Implementation, pp. 67-78, 1995.
10320 Also available at http://citeseer.ist.psu.edu/patterson95accurate.html
10322 This is essentially an SSA-CCP pass modified to deal with ranges
10323 instead of constants.
10325 While propagating ranges, we may find that two or more SSA name
10326 have equivalent, though distinct ranges. For instance,
10329 2 p_4 = ASSERT_EXPR <p_3, p_3 != 0>
10331 4 p_5 = ASSERT_EXPR <p_4, p_4 == q_2>;
10335 In the code above, pointer p_5 has range [q_2, q_2], but from the
10336 code we can also determine that p_5 cannot be NULL and, if q_2 had
10337 a non-varying range, p_5's range should also be compatible with it.
10339 These equivalences are created by two expressions: ASSERT_EXPR and
10340 copy operations. Since p_5 is an assertion on p_4, and p_4 was the
10341 result of another assertion, then we can use the fact that p_5 and
10342 p_4 are equivalent when evaluating p_5's range.
10344 Together with value ranges, we also propagate these equivalences
10345 between names so that we can take advantage of information from
10346 multiple ranges when doing final replacement. Note that this
10347 equivalency relation is transitive but not symmetric.
10349 In the example above, p_5 is equivalent to p_4, q_2 and p_3, but we
10350 cannot assert that q_2 is equivalent to p_5 because q_2 may be used
10351 in contexts where that assertion does not hold (e.g., in line 6).
10353 TODO, the main difference between this pass and Patterson's is that
10354 we do not propagate edge probabilities. We only compute whether
10355 edges can be taken or not. That is, instead of having a spectrum
10356 of jump probabilities between 0 and 1, we only deal with 0, 1 and
10357 DON'T KNOW. In the future, it may be worthwhile to propagate
10358 probabilities to aid branch prediction. */
10360 static unsigned int
10367 loop_optimizer_init (LOOPS_NORMAL
| LOOPS_HAVE_RECORDED_EXITS
);
10368 rewrite_into_loop_closed_ssa (NULL
, TODO_update_ssa
);
10369 scev_initialize ();
10371 /* ??? This ends up using stale EDGE_DFS_BACK for liveness computation.
10372 Inserting assertions may split edges which will invalidate
10374 insert_range_assertions ();
10376 to_remove_edges
.create (10);
10377 to_update_switch_stmts
.create (5);
10378 threadedge_initialize_values ();
10380 /* For visiting PHI nodes we need EDGE_DFS_BACK computed. */
10381 mark_dfs_back_edges ();
10384 ssa_propagate (vrp_visit_stmt
, vrp_visit_phi_node
);
10387 free_numbers_of_iterations_estimates ();
10389 /* ASSERT_EXPRs must be removed before finalizing jump threads
10390 as finalizing jump threads calls the CFG cleanup code which
10391 does not properly handle ASSERT_EXPRs. */
10392 remove_range_assertions ();
10394 /* If we exposed any new variables, go ahead and put them into
10395 SSA form now, before we handle jump threading. This simplifies
10396 interactions between rewriting of _DECL nodes into SSA form
10397 and rewriting SSA_NAME nodes into SSA form after block
10398 duplication and CFG manipulation. */
10399 update_ssa (TODO_update_ssa
);
10401 finalize_jump_threads ();
10403 /* Remove dead edges from SWITCH_EXPR optimization. This leaves the
10404 CFG in a broken state and requires a cfg_cleanup run. */
10405 FOR_EACH_VEC_ELT (to_remove_edges
, i
, e
)
10407 /* Update SWITCH_EXPR case label vector. */
10408 FOR_EACH_VEC_ELT (to_update_switch_stmts
, i
, su
)
10411 size_t n
= TREE_VEC_LENGTH (su
->vec
);
10413 gimple_switch_set_num_labels (su
->stmt
, n
);
10414 for (j
= 0; j
< n
; j
++)
10415 gimple_switch_set_label (su
->stmt
, j
, TREE_VEC_ELT (su
->vec
, j
));
10416 /* As we may have replaced the default label with a regular one
10417 make sure to make it a real default label again. This ensures
10418 optimal expansion. */
10419 label
= gimple_switch_label (su
->stmt
, 0);
10420 CASE_LOW (label
) = NULL_TREE
;
10421 CASE_HIGH (label
) = NULL_TREE
;
10424 if (to_remove_edges
.length () > 0)
10426 free_dominance_info (CDI_DOMINATORS
);
10427 loops_state_set (LOOPS_NEED_FIXUP
);
10430 to_remove_edges
.release ();
10431 to_update_switch_stmts
.release ();
10432 threadedge_finalize_values ();
10435 loop_optimizer_finalize ();
10441 const pass_data pass_data_vrp
=
10443 GIMPLE_PASS
, /* type */
10445 OPTGROUP_NONE
, /* optinfo_flags */
10446 TV_TREE_VRP
, /* tv_id */
10447 PROP_ssa
, /* properties_required */
10448 0, /* properties_provided */
10449 0, /* properties_destroyed */
10450 0, /* todo_flags_start */
10451 ( TODO_cleanup_cfg
| TODO_update_ssa
), /* todo_flags_finish */
10454 class pass_vrp
: public gimple_opt_pass
10457 pass_vrp (gcc::context
*ctxt
)
10458 : gimple_opt_pass (pass_data_vrp
, ctxt
)
10461 /* opt_pass methods: */
10462 opt_pass
* clone () { return new pass_vrp (m_ctxt
); }
10463 virtual bool gate (function
*) { return flag_tree_vrp
!= 0; }
10464 virtual unsigned int execute (function
*) { return execute_vrp (); }
10466 }; // class pass_vrp
10468 } // anon namespace
10471 make_pass_vrp (gcc::context
*ctxt
)
10473 return new pass_vrp (ctxt
);