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"
25 #include "insn-codes.h"
30 #include "tree-pass.h"
32 #include "optabs-tree.h"
33 #include "gimple-pretty-print.h"
34 #include "diagnostic-core.h"
36 #include "fold-const.h"
37 #include "stor-layout.h"
40 #include "gimple-fold.h"
42 #include "gimple-iterator.h"
43 #include "gimple-walk.h"
45 #include "tree-ssa-loop-manip.h"
46 #include "tree-ssa-loop-niter.h"
47 #include "tree-ssa-loop.h"
48 #include "tree-into-ssa.h"
52 #include "tree-scalar-evolution.h"
53 #include "tree-ssa-propagate.h"
54 #include "tree-chrec.h"
55 #include "tree-ssa-threadupdate.h"
56 #include "tree-ssa-scopedtables.h"
57 #include "tree-ssa-threadedge.h"
61 /* Range of values that can be associated with an SSA_NAME after VRP
65 /* Lattice value represented by this range. */
66 enum value_range_type type
;
68 /* Minimum and maximum values represented by this range. These
69 values should be interpreted as follows:
71 - If TYPE is VR_UNDEFINED or VR_VARYING then MIN and MAX must
74 - If TYPE == VR_RANGE then MIN holds the minimum value and
75 MAX holds the maximum value of the range [MIN, MAX].
77 - If TYPE == ANTI_RANGE the variable is known to NOT
78 take any values in the range [MIN, MAX]. */
82 /* Set of SSA names whose value ranges are equivalent to this one.
83 This set is only valid when TYPE is VR_RANGE or VR_ANTI_RANGE. */
87 #define VR_INITIALIZER { VR_UNDEFINED, NULL_TREE, NULL_TREE, NULL }
89 /* Set of SSA names found live during the RPO traversal of the function
90 for still active basic-blocks. */
93 /* Return true if the SSA name NAME is live on the edge E. */
96 live_on_edge (edge e
, tree name
)
98 return (live
[e
->dest
->index
]
99 && bitmap_bit_p (live
[e
->dest
->index
], SSA_NAME_VERSION (name
)));
102 /* Local functions. */
103 static int compare_values (tree val1
, tree val2
);
104 static int compare_values_warnv (tree val1
, tree val2
, bool *);
105 static void vrp_meet (value_range
*, value_range
*);
106 static void vrp_intersect_ranges (value_range
*, value_range
*);
107 static tree
vrp_evaluate_conditional_warnv_with_ops (enum tree_code
,
108 tree
, tree
, bool, bool *,
111 /* Location information for ASSERT_EXPRs. Each instance of this
112 structure describes an ASSERT_EXPR for an SSA name. Since a single
113 SSA name may have more than one assertion associated with it, these
114 locations are kept in a linked list attached to the corresponding
118 /* Basic block where the assertion would be inserted. */
121 /* Some assertions need to be inserted on an edge (e.g., assertions
122 generated by COND_EXPRs). In those cases, BB will be NULL. */
125 /* Pointer to the statement that generated this assertion. */
126 gimple_stmt_iterator si
;
128 /* Predicate code for the ASSERT_EXPR. Must be COMPARISON_CLASS_P. */
129 enum tree_code comp_code
;
131 /* Value being compared against. */
134 /* Expression to compare. */
137 /* Next node in the linked list. */
141 /* If bit I is present, it means that SSA name N_i has a list of
142 assertions that should be inserted in the IL. */
143 static bitmap need_assert_for
;
145 /* Array of locations lists where to insert assertions. ASSERTS_FOR[I]
146 holds a list of ASSERT_LOCUS_T nodes that describe where
147 ASSERT_EXPRs for SSA name N_I should be inserted. */
148 static assert_locus
**asserts_for
;
150 /* Value range array. After propagation, VR_VALUE[I] holds the range
151 of values that SSA name N_I may take. */
152 static unsigned num_vr_values
;
153 static value_range
**vr_value
;
154 static bool values_propagated
;
156 /* For a PHI node which sets SSA name N_I, VR_COUNTS[I] holds the
157 number of executable edges we saw the last time we visited the
159 static int *vr_phi_edge_counts
;
161 struct switch_update
{
166 static vec
<edge
> to_remove_edges
;
167 static vec
<switch_update
> to_update_switch_stmts
;
170 /* Return the maximum value for TYPE. */
173 vrp_val_max (const_tree type
)
175 if (!INTEGRAL_TYPE_P (type
))
178 return TYPE_MAX_VALUE (type
);
181 /* Return the minimum value for TYPE. */
184 vrp_val_min (const_tree type
)
186 if (!INTEGRAL_TYPE_P (type
))
189 return TYPE_MIN_VALUE (type
);
192 /* Return whether VAL is equal to the maximum value of its type. This
193 will be true for a positive overflow infinity. We can't do a
194 simple equality comparison with TYPE_MAX_VALUE because C typedefs
195 and Ada subtypes can produce types whose TYPE_MAX_VALUE is not ==
196 to the integer constant with the same value in the type. */
199 vrp_val_is_max (const_tree val
)
201 tree type_max
= vrp_val_max (TREE_TYPE (val
));
202 return (val
== type_max
203 || (type_max
!= NULL_TREE
204 && operand_equal_p (val
, type_max
, 0)));
207 /* Return whether VAL is equal to the minimum value of its type. This
208 will be true for a negative overflow infinity. */
211 vrp_val_is_min (const_tree val
)
213 tree type_min
= vrp_val_min (TREE_TYPE (val
));
214 return (val
== type_min
215 || (type_min
!= NULL_TREE
216 && operand_equal_p (val
, type_min
, 0)));
220 /* Return whether TYPE should use an overflow infinity distinct from
221 TYPE_{MIN,MAX}_VALUE. We use an overflow infinity value to
222 represent a signed overflow during VRP computations. An infinity
223 is distinct from a half-range, which will go from some number to
224 TYPE_{MIN,MAX}_VALUE. */
227 needs_overflow_infinity (const_tree type
)
229 return INTEGRAL_TYPE_P (type
) && !TYPE_OVERFLOW_WRAPS (type
);
232 /* Return whether TYPE can support our overflow infinity
233 representation: we use the TREE_OVERFLOW flag, which only exists
234 for constants. If TYPE doesn't support this, we don't optimize
235 cases which would require signed overflow--we drop them to
239 supports_overflow_infinity (const_tree type
)
241 tree min
= vrp_val_min (type
), max
= vrp_val_max (type
);
242 gcc_checking_assert (needs_overflow_infinity (type
));
243 return (min
!= NULL_TREE
244 && CONSTANT_CLASS_P (min
)
246 && CONSTANT_CLASS_P (max
));
249 /* VAL is the maximum or minimum value of a type. Return a
250 corresponding overflow infinity. */
253 make_overflow_infinity (tree val
)
255 gcc_checking_assert (val
!= NULL_TREE
&& CONSTANT_CLASS_P (val
));
256 val
= copy_node (val
);
257 TREE_OVERFLOW (val
) = 1;
261 /* Return a negative overflow infinity for TYPE. */
264 negative_overflow_infinity (tree type
)
266 gcc_checking_assert (supports_overflow_infinity (type
));
267 return make_overflow_infinity (vrp_val_min (type
));
270 /* Return a positive overflow infinity for TYPE. */
273 positive_overflow_infinity (tree type
)
275 gcc_checking_assert (supports_overflow_infinity (type
));
276 return make_overflow_infinity (vrp_val_max (type
));
279 /* Return whether VAL is a negative overflow infinity. */
282 is_negative_overflow_infinity (const_tree val
)
284 return (TREE_OVERFLOW_P (val
)
285 && needs_overflow_infinity (TREE_TYPE (val
))
286 && vrp_val_is_min (val
));
289 /* Return whether VAL is a positive overflow infinity. */
292 is_positive_overflow_infinity (const_tree val
)
294 return (TREE_OVERFLOW_P (val
)
295 && needs_overflow_infinity (TREE_TYPE (val
))
296 && vrp_val_is_max (val
));
299 /* Return whether VAL is a positive or negative overflow infinity. */
302 is_overflow_infinity (const_tree val
)
304 return (TREE_OVERFLOW_P (val
)
305 && needs_overflow_infinity (TREE_TYPE (val
))
306 && (vrp_val_is_min (val
) || vrp_val_is_max (val
)));
309 /* Return whether STMT has a constant rhs that is_overflow_infinity. */
312 stmt_overflow_infinity (gimple
*stmt
)
314 if (is_gimple_assign (stmt
)
315 && get_gimple_rhs_class (gimple_assign_rhs_code (stmt
)) ==
317 return is_overflow_infinity (gimple_assign_rhs1 (stmt
));
321 /* If VAL is now an overflow infinity, return VAL. Otherwise, return
322 the same value with TREE_OVERFLOW clear. This can be used to avoid
323 confusing a regular value with an overflow value. */
326 avoid_overflow_infinity (tree val
)
328 if (!is_overflow_infinity (val
))
331 if (vrp_val_is_max (val
))
332 return vrp_val_max (TREE_TYPE (val
));
335 gcc_checking_assert (vrp_val_is_min (val
));
336 return vrp_val_min (TREE_TYPE (val
));
341 /* Set value range VR to VR_UNDEFINED. */
344 set_value_range_to_undefined (value_range
*vr
)
346 vr
->type
= VR_UNDEFINED
;
347 vr
->min
= vr
->max
= NULL_TREE
;
349 bitmap_clear (vr
->equiv
);
353 /* Set value range VR to VR_VARYING. */
356 set_value_range_to_varying (value_range
*vr
)
358 vr
->type
= VR_VARYING
;
359 vr
->min
= vr
->max
= NULL_TREE
;
361 bitmap_clear (vr
->equiv
);
365 /* Set value range VR to {T, MIN, MAX, EQUIV}. */
368 set_value_range (value_range
*vr
, enum value_range_type t
, tree min
,
369 tree max
, bitmap equiv
)
371 /* Check the validity of the range. */
373 && (t
== VR_RANGE
|| t
== VR_ANTI_RANGE
))
377 gcc_assert (min
&& max
);
379 gcc_assert ((!TREE_OVERFLOW_P (min
) || is_overflow_infinity (min
))
380 && (!TREE_OVERFLOW_P (max
) || is_overflow_infinity (max
)));
382 if (INTEGRAL_TYPE_P (TREE_TYPE (min
)) && t
== VR_ANTI_RANGE
)
383 gcc_assert (!vrp_val_is_min (min
) || !vrp_val_is_max (max
));
385 cmp
= compare_values (min
, max
);
386 gcc_assert (cmp
== 0 || cmp
== -1 || cmp
== -2);
388 if (needs_overflow_infinity (TREE_TYPE (min
)))
389 gcc_assert (!is_overflow_infinity (min
)
390 || !is_overflow_infinity (max
));
394 && (t
== VR_UNDEFINED
|| t
== VR_VARYING
))
396 gcc_assert (min
== NULL_TREE
&& max
== NULL_TREE
);
397 gcc_assert (equiv
== NULL
|| bitmap_empty_p (equiv
));
404 /* Since updating the equivalence set involves deep copying the
405 bitmaps, only do it if absolutely necessary. */
406 if (vr
->equiv
== NULL
408 vr
->equiv
= BITMAP_ALLOC (NULL
);
410 if (equiv
!= vr
->equiv
)
412 if (equiv
&& !bitmap_empty_p (equiv
))
413 bitmap_copy (vr
->equiv
, equiv
);
415 bitmap_clear (vr
->equiv
);
420 /* Set value range VR to the canonical form of {T, MIN, MAX, EQUIV}.
421 This means adjusting T, MIN and MAX representing the case of a
422 wrapping range with MAX < MIN covering [MIN, type_max] U [type_min, MAX]
423 as anti-rage ~[MAX+1, MIN-1]. Likewise for wrapping anti-ranges.
424 In corner cases where MAX+1 or MIN-1 wraps this will fall back
426 This routine exists to ease canonicalization in the case where we
427 extract ranges from var + CST op limit. */
430 set_and_canonicalize_value_range (value_range
*vr
, enum value_range_type t
,
431 tree min
, tree max
, bitmap equiv
)
433 /* Use the canonical setters for VR_UNDEFINED and VR_VARYING. */
434 if (t
== VR_UNDEFINED
)
436 set_value_range_to_undefined (vr
);
439 else if (t
== VR_VARYING
)
441 set_value_range_to_varying (vr
);
445 /* Nothing to canonicalize for symbolic ranges. */
446 if (TREE_CODE (min
) != INTEGER_CST
447 || TREE_CODE (max
) != INTEGER_CST
)
449 set_value_range (vr
, t
, min
, max
, equiv
);
453 /* Wrong order for min and max, to swap them and the VR type we need
455 if (tree_int_cst_lt (max
, min
))
459 /* For one bit precision if max < min, then the swapped
460 range covers all values, so for VR_RANGE it is varying and
461 for VR_ANTI_RANGE empty range, so drop to varying as well. */
462 if (TYPE_PRECISION (TREE_TYPE (min
)) == 1)
464 set_value_range_to_varying (vr
);
468 one
= build_int_cst (TREE_TYPE (min
), 1);
469 tmp
= int_const_binop (PLUS_EXPR
, max
, one
);
470 max
= int_const_binop (MINUS_EXPR
, min
, one
);
473 /* There's one corner case, if we had [C+1, C] before we now have
474 that again. But this represents an empty value range, so drop
475 to varying in this case. */
476 if (tree_int_cst_lt (max
, min
))
478 set_value_range_to_varying (vr
);
482 t
= t
== VR_RANGE
? VR_ANTI_RANGE
: VR_RANGE
;
485 /* Anti-ranges that can be represented as ranges should be so. */
486 if (t
== VR_ANTI_RANGE
)
488 bool is_min
= vrp_val_is_min (min
);
489 bool is_max
= vrp_val_is_max (max
);
491 if (is_min
&& is_max
)
493 /* We cannot deal with empty ranges, drop to varying.
494 ??? This could be VR_UNDEFINED instead. */
495 set_value_range_to_varying (vr
);
498 else if (TYPE_PRECISION (TREE_TYPE (min
)) == 1
499 && (is_min
|| is_max
))
501 /* Non-empty boolean ranges can always be represented
502 as a singleton range. */
504 min
= max
= vrp_val_max (TREE_TYPE (min
));
506 min
= max
= vrp_val_min (TREE_TYPE (min
));
510 /* As a special exception preserve non-null ranges. */
511 && !(TYPE_UNSIGNED (TREE_TYPE (min
))
512 && integer_zerop (max
)))
514 tree one
= build_int_cst (TREE_TYPE (max
), 1);
515 min
= int_const_binop (PLUS_EXPR
, max
, one
);
516 max
= vrp_val_max (TREE_TYPE (max
));
521 tree one
= build_int_cst (TREE_TYPE (min
), 1);
522 max
= int_const_binop (MINUS_EXPR
, min
, one
);
523 min
= vrp_val_min (TREE_TYPE (min
));
528 /* Drop [-INF(OVF), +INF(OVF)] to varying. */
529 if (needs_overflow_infinity (TREE_TYPE (min
))
530 && is_overflow_infinity (min
)
531 && is_overflow_infinity (max
))
533 set_value_range_to_varying (vr
);
537 set_value_range (vr
, t
, min
, max
, equiv
);
540 /* Copy value range FROM into value range TO. */
543 copy_value_range (value_range
*to
, value_range
*from
)
545 set_value_range (to
, from
->type
, from
->min
, from
->max
, from
->equiv
);
548 /* Set value range VR to a single value. This function is only called
549 with values we get from statements, and exists to clear the
550 TREE_OVERFLOW flag so that we don't think we have an overflow
551 infinity when we shouldn't. */
554 set_value_range_to_value (value_range
*vr
, tree val
, bitmap equiv
)
556 gcc_assert (is_gimple_min_invariant (val
));
557 if (TREE_OVERFLOW_P (val
))
558 val
= drop_tree_overflow (val
);
559 set_value_range (vr
, VR_RANGE
, val
, val
, equiv
);
562 /* Set value range VR to a non-negative range of type TYPE.
563 OVERFLOW_INFINITY indicates whether to use an overflow infinity
564 rather than TYPE_MAX_VALUE; this should be true if we determine
565 that the range is nonnegative based on the assumption that signed
566 overflow does not occur. */
569 set_value_range_to_nonnegative (value_range
*vr
, tree type
,
570 bool overflow_infinity
)
574 if (overflow_infinity
&& !supports_overflow_infinity (type
))
576 set_value_range_to_varying (vr
);
580 zero
= build_int_cst (type
, 0);
581 set_value_range (vr
, VR_RANGE
, zero
,
583 ? positive_overflow_infinity (type
)
584 : TYPE_MAX_VALUE (type
)),
588 /* Set value range VR to a non-NULL range of type TYPE. */
591 set_value_range_to_nonnull (value_range
*vr
, tree type
)
593 tree zero
= build_int_cst (type
, 0);
594 set_value_range (vr
, VR_ANTI_RANGE
, zero
, zero
, vr
->equiv
);
598 /* Set value range VR to a NULL range of type TYPE. */
601 set_value_range_to_null (value_range
*vr
, tree type
)
603 set_value_range_to_value (vr
, build_int_cst (type
, 0), vr
->equiv
);
607 /* Set value range VR to a range of a truthvalue of type TYPE. */
610 set_value_range_to_truthvalue (value_range
*vr
, tree type
)
612 if (TYPE_PRECISION (type
) == 1)
613 set_value_range_to_varying (vr
);
615 set_value_range (vr
, VR_RANGE
,
616 build_int_cst (type
, 0), build_int_cst (type
, 1),
621 /* If abs (min) < abs (max), set VR to [-max, max], if
622 abs (min) >= abs (max), set VR to [-min, min]. */
625 abs_extent_range (value_range
*vr
, tree min
, tree max
)
629 gcc_assert (TREE_CODE (min
) == INTEGER_CST
);
630 gcc_assert (TREE_CODE (max
) == INTEGER_CST
);
631 gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (min
)));
632 gcc_assert (!TYPE_UNSIGNED (TREE_TYPE (min
)));
633 min
= fold_unary (ABS_EXPR
, TREE_TYPE (min
), min
);
634 max
= fold_unary (ABS_EXPR
, TREE_TYPE (max
), max
);
635 if (TREE_OVERFLOW (min
) || TREE_OVERFLOW (max
))
637 set_value_range_to_varying (vr
);
640 cmp
= compare_values (min
, max
);
642 min
= fold_unary (NEGATE_EXPR
, TREE_TYPE (min
), max
);
643 else if (cmp
== 0 || cmp
== 1)
646 min
= fold_unary (NEGATE_EXPR
, TREE_TYPE (min
), min
);
650 set_value_range_to_varying (vr
);
653 set_and_canonicalize_value_range (vr
, VR_RANGE
, min
, max
, NULL
);
657 /* Return value range information for VAR.
659 If we have no values ranges recorded (ie, VRP is not running), then
660 return NULL. Otherwise create an empty range if none existed for VAR. */
663 get_value_range (const_tree var
)
665 static const value_range vr_const_varying
666 = { VR_VARYING
, NULL_TREE
, NULL_TREE
, NULL
};
669 unsigned ver
= SSA_NAME_VERSION (var
);
671 /* If we have no recorded ranges, then return NULL. */
675 /* If we query the range for a new SSA name return an unmodifiable VARYING.
676 We should get here at most from the substitute-and-fold stage which
677 will never try to change values. */
678 if (ver
>= num_vr_values
)
679 return CONST_CAST (value_range
*, &vr_const_varying
);
685 /* After propagation finished do not allocate new value-ranges. */
686 if (values_propagated
)
687 return CONST_CAST (value_range
*, &vr_const_varying
);
689 /* Create a default value range. */
690 vr_value
[ver
] = vr
= XCNEW (value_range
);
692 /* Defer allocating the equivalence set. */
695 /* If VAR is a default definition of a parameter, the variable can
696 take any value in VAR's type. */
697 if (SSA_NAME_IS_DEFAULT_DEF (var
))
699 sym
= SSA_NAME_VAR (var
);
700 if (TREE_CODE (sym
) == PARM_DECL
)
702 /* Try to use the "nonnull" attribute to create ~[0, 0]
703 anti-ranges for pointers. Note that this is only valid with
704 default definitions of PARM_DECLs. */
705 if (POINTER_TYPE_P (TREE_TYPE (sym
))
706 && nonnull_arg_p (sym
))
707 set_value_range_to_nonnull (vr
, TREE_TYPE (sym
));
709 set_value_range_to_varying (vr
);
711 else if (TREE_CODE (sym
) == RESULT_DECL
712 && DECL_BY_REFERENCE (sym
))
713 set_value_range_to_nonnull (vr
, TREE_TYPE (sym
));
719 /* Return true, if VAL1 and VAL2 are equal values for VRP purposes. */
722 vrp_operand_equal_p (const_tree val1
, const_tree val2
)
726 if (!val1
|| !val2
|| !operand_equal_p (val1
, val2
, 0))
728 return is_overflow_infinity (val1
) == is_overflow_infinity (val2
);
731 /* Return true, if the bitmaps B1 and B2 are equal. */
734 vrp_bitmap_equal_p (const_bitmap b1
, const_bitmap b2
)
737 || ((!b1
|| bitmap_empty_p (b1
))
738 && (!b2
|| bitmap_empty_p (b2
)))
740 && bitmap_equal_p (b1
, b2
)));
743 /* Update the value range and equivalence set for variable VAR to
744 NEW_VR. Return true if NEW_VR is different from VAR's previous
747 NOTE: This function assumes that NEW_VR is a temporary value range
748 object created for the sole purpose of updating VAR's range. The
749 storage used by the equivalence set from NEW_VR will be freed by
750 this function. Do not call update_value_range when NEW_VR
751 is the range object associated with another SSA name. */
754 update_value_range (const_tree var
, value_range
*new_vr
)
759 /* If there is a value-range on the SSA name from earlier analysis
761 if (INTEGRAL_TYPE_P (TREE_TYPE (var
)))
764 value_range_type rtype
= get_range_info (var
, &min
, &max
);
765 if (rtype
== VR_RANGE
|| rtype
== VR_ANTI_RANGE
)
769 nr
.min
= wide_int_to_tree (TREE_TYPE (var
), min
);
770 nr
.max
= wide_int_to_tree (TREE_TYPE (var
), max
);
772 vrp_intersect_ranges (new_vr
, &nr
);
776 /* Update the value range, if necessary. */
777 old_vr
= get_value_range (var
);
778 is_new
= old_vr
->type
!= new_vr
->type
779 || !vrp_operand_equal_p (old_vr
->min
, new_vr
->min
)
780 || !vrp_operand_equal_p (old_vr
->max
, new_vr
->max
)
781 || !vrp_bitmap_equal_p (old_vr
->equiv
, new_vr
->equiv
);
785 /* Do not allow transitions up the lattice. The following
786 is slightly more awkward than just new_vr->type < old_vr->type
787 because VR_RANGE and VR_ANTI_RANGE need to be considered
788 the same. We may not have is_new when transitioning to
789 UNDEFINED. If old_vr->type is VARYING, we shouldn't be
791 if (new_vr
->type
== VR_UNDEFINED
)
793 BITMAP_FREE (new_vr
->equiv
);
794 set_value_range_to_varying (old_vr
);
795 set_value_range_to_varying (new_vr
);
799 set_value_range (old_vr
, new_vr
->type
, new_vr
->min
, new_vr
->max
,
803 BITMAP_FREE (new_vr
->equiv
);
809 /* Add VAR and VAR's equivalence set to EQUIV. This is the central
810 point where equivalence processing can be turned on/off. */
813 add_equivalence (bitmap
*equiv
, const_tree var
)
815 unsigned ver
= SSA_NAME_VERSION (var
);
816 value_range
*vr
= vr_value
[ver
];
819 *equiv
= BITMAP_ALLOC (NULL
);
820 bitmap_set_bit (*equiv
, ver
);
822 bitmap_ior_into (*equiv
, vr
->equiv
);
826 /* Return true if VR is ~[0, 0]. */
829 range_is_nonnull (value_range
*vr
)
831 return vr
->type
== VR_ANTI_RANGE
832 && integer_zerop (vr
->min
)
833 && integer_zerop (vr
->max
);
837 /* Return true if VR is [0, 0]. */
840 range_is_null (value_range
*vr
)
842 return vr
->type
== VR_RANGE
843 && integer_zerop (vr
->min
)
844 && integer_zerop (vr
->max
);
847 /* Return true if max and min of VR are INTEGER_CST. It's not necessary
851 range_int_cst_p (value_range
*vr
)
853 return (vr
->type
== VR_RANGE
854 && TREE_CODE (vr
->max
) == INTEGER_CST
855 && TREE_CODE (vr
->min
) == INTEGER_CST
);
858 /* Return true if VR is a INTEGER_CST singleton. */
861 range_int_cst_singleton_p (value_range
*vr
)
863 return (range_int_cst_p (vr
)
864 && !is_overflow_infinity (vr
->min
)
865 && !is_overflow_infinity (vr
->max
)
866 && tree_int_cst_equal (vr
->min
, vr
->max
));
869 /* Return true if value range VR involves at least one symbol. */
872 symbolic_range_p (value_range
*vr
)
874 return (!is_gimple_min_invariant (vr
->min
)
875 || !is_gimple_min_invariant (vr
->max
));
878 /* Return the single symbol (an SSA_NAME) contained in T if any, or NULL_TREE
879 otherwise. We only handle additive operations and set NEG to true if the
880 symbol is negated and INV to the invariant part, if any. */
883 get_single_symbol (tree t
, bool *neg
, tree
*inv
)
888 if (TREE_CODE (t
) == PLUS_EXPR
889 || TREE_CODE (t
) == POINTER_PLUS_EXPR
890 || TREE_CODE (t
) == MINUS_EXPR
)
892 if (is_gimple_min_invariant (TREE_OPERAND (t
, 0)))
894 neg_
= (TREE_CODE (t
) == MINUS_EXPR
);
895 inv_
= TREE_OPERAND (t
, 0);
896 t
= TREE_OPERAND (t
, 1);
898 else if (is_gimple_min_invariant (TREE_OPERAND (t
, 1)))
901 inv_
= TREE_OPERAND (t
, 1);
902 t
= TREE_OPERAND (t
, 0);
913 if (TREE_CODE (t
) == NEGATE_EXPR
)
915 t
= TREE_OPERAND (t
, 0);
919 if (TREE_CODE (t
) != SSA_NAME
)
927 /* The reverse operation: build a symbolic expression with TYPE
928 from symbol SYM, negated according to NEG, and invariant INV. */
931 build_symbolic_expr (tree type
, tree sym
, bool neg
, tree inv
)
933 const bool pointer_p
= POINTER_TYPE_P (type
);
937 t
= build1 (NEGATE_EXPR
, type
, t
);
939 if (integer_zerop (inv
))
942 return build2 (pointer_p
? POINTER_PLUS_EXPR
: PLUS_EXPR
, type
, t
, inv
);
945 /* Return true if value range VR involves exactly one symbol SYM. */
948 symbolic_range_based_on_p (value_range
*vr
, const_tree sym
)
950 bool neg
, min_has_symbol
, max_has_symbol
;
953 if (is_gimple_min_invariant (vr
->min
))
954 min_has_symbol
= false;
955 else if (get_single_symbol (vr
->min
, &neg
, &inv
) == sym
)
956 min_has_symbol
= true;
960 if (is_gimple_min_invariant (vr
->max
))
961 max_has_symbol
= false;
962 else if (get_single_symbol (vr
->max
, &neg
, &inv
) == sym
)
963 max_has_symbol
= true;
967 return (min_has_symbol
|| max_has_symbol
);
970 /* Return true if value range VR uses an overflow infinity. */
973 overflow_infinity_range_p (value_range
*vr
)
975 return (vr
->type
== VR_RANGE
976 && (is_overflow_infinity (vr
->min
)
977 || is_overflow_infinity (vr
->max
)));
980 /* Return false if we can not make a valid comparison based on VR;
981 this will be the case if it uses an overflow infinity and overflow
982 is not undefined (i.e., -fno-strict-overflow is in effect).
983 Otherwise return true, and set *STRICT_OVERFLOW_P to true if VR
984 uses an overflow infinity. */
987 usable_range_p (value_range
*vr
, bool *strict_overflow_p
)
989 gcc_assert (vr
->type
== VR_RANGE
);
990 if (is_overflow_infinity (vr
->min
))
992 *strict_overflow_p
= true;
993 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (vr
->min
)))
996 if (is_overflow_infinity (vr
->max
))
998 *strict_overflow_p
= true;
999 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (vr
->max
)))
1005 /* Return true if the result of assignment STMT is know to be non-zero.
1006 If the return value is based on the assumption that signed overflow is
1007 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1008 *STRICT_OVERFLOW_P.*/
1011 gimple_assign_nonzero_warnv_p (gimple
*stmt
, bool *strict_overflow_p
)
1013 enum tree_code code
= gimple_assign_rhs_code (stmt
);
1014 switch (get_gimple_rhs_class (code
))
1016 case GIMPLE_UNARY_RHS
:
1017 return tree_unary_nonzero_warnv_p (gimple_assign_rhs_code (stmt
),
1018 gimple_expr_type (stmt
),
1019 gimple_assign_rhs1 (stmt
),
1021 case GIMPLE_BINARY_RHS
:
1022 return tree_binary_nonzero_warnv_p (gimple_assign_rhs_code (stmt
),
1023 gimple_expr_type (stmt
),
1024 gimple_assign_rhs1 (stmt
),
1025 gimple_assign_rhs2 (stmt
),
1027 case GIMPLE_TERNARY_RHS
:
1029 case GIMPLE_SINGLE_RHS
:
1030 return tree_single_nonzero_warnv_p (gimple_assign_rhs1 (stmt
),
1032 case GIMPLE_INVALID_RHS
:
1039 /* Return true if STMT is known to compute a non-zero value.
1040 If the return value is based on the assumption that signed overflow is
1041 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1042 *STRICT_OVERFLOW_P.*/
1045 gimple_stmt_nonzero_warnv_p (gimple
*stmt
, bool *strict_overflow_p
)
1047 switch (gimple_code (stmt
))
1050 return gimple_assign_nonzero_warnv_p (stmt
, strict_overflow_p
);
1053 tree fndecl
= gimple_call_fndecl (stmt
);
1054 if (!fndecl
) return false;
1055 if (flag_delete_null_pointer_checks
&& !flag_check_new
1056 && DECL_IS_OPERATOR_NEW (fndecl
)
1057 && !TREE_NOTHROW (fndecl
))
1059 /* References are always non-NULL. */
1060 if (flag_delete_null_pointer_checks
1061 && TREE_CODE (TREE_TYPE (fndecl
)) == REFERENCE_TYPE
)
1063 if (flag_delete_null_pointer_checks
&&
1064 lookup_attribute ("returns_nonnull",
1065 TYPE_ATTRIBUTES (gimple_call_fntype (stmt
))))
1067 return gimple_alloca_call_p (stmt
);
1074 /* Like tree_expr_nonzero_warnv_p, but this function uses value ranges
1078 vrp_stmt_computes_nonzero (gimple
*stmt
, bool *strict_overflow_p
)
1080 if (gimple_stmt_nonzero_warnv_p (stmt
, strict_overflow_p
))
1083 /* If we have an expression of the form &X->a, then the expression
1084 is nonnull if X is nonnull. */
1085 if (is_gimple_assign (stmt
)
1086 && gimple_assign_rhs_code (stmt
) == ADDR_EXPR
)
1088 tree expr
= gimple_assign_rhs1 (stmt
);
1089 tree base
= get_base_address (TREE_OPERAND (expr
, 0));
1091 if (base
!= NULL_TREE
1092 && TREE_CODE (base
) == MEM_REF
1093 && TREE_CODE (TREE_OPERAND (base
, 0)) == SSA_NAME
)
1095 value_range
*vr
= get_value_range (TREE_OPERAND (base
, 0));
1096 if (range_is_nonnull (vr
))
1104 /* Returns true if EXPR is a valid value (as expected by compare_values) --
1105 a gimple invariant, or SSA_NAME +- CST. */
1108 valid_value_p (tree expr
)
1110 if (TREE_CODE (expr
) == SSA_NAME
)
1113 if (TREE_CODE (expr
) == PLUS_EXPR
1114 || TREE_CODE (expr
) == MINUS_EXPR
)
1115 return (TREE_CODE (TREE_OPERAND (expr
, 0)) == SSA_NAME
1116 && TREE_CODE (TREE_OPERAND (expr
, 1)) == INTEGER_CST
);
1118 return is_gimple_min_invariant (expr
);
1124 -2 if those are incomparable. */
1126 operand_less_p (tree val
, tree val2
)
1128 /* LT is folded faster than GE and others. Inline the common case. */
1129 if (TREE_CODE (val
) == INTEGER_CST
&& TREE_CODE (val2
) == INTEGER_CST
)
1130 return tree_int_cst_lt (val
, val2
);
1135 fold_defer_overflow_warnings ();
1137 tcmp
= fold_binary_to_constant (LT_EXPR
, boolean_type_node
, val
, val2
);
1139 fold_undefer_and_ignore_overflow_warnings ();
1142 || TREE_CODE (tcmp
) != INTEGER_CST
)
1145 if (!integer_zerop (tcmp
))
1149 /* val >= val2, not considering overflow infinity. */
1150 if (is_negative_overflow_infinity (val
))
1151 return is_negative_overflow_infinity (val2
) ? 0 : 1;
1152 else if (is_positive_overflow_infinity (val2
))
1153 return is_positive_overflow_infinity (val
) ? 0 : 1;
1158 /* Compare two values VAL1 and VAL2. Return
1160 -2 if VAL1 and VAL2 cannot be compared at compile-time,
1163 +1 if VAL1 > VAL2, and
1166 This is similar to tree_int_cst_compare but supports pointer values
1167 and values that cannot be compared at compile time.
1169 If STRICT_OVERFLOW_P is not NULL, then set *STRICT_OVERFLOW_P to
1170 true if the return value is only valid if we assume that signed
1171 overflow is undefined. */
1174 compare_values_warnv (tree val1
, tree val2
, bool *strict_overflow_p
)
1179 /* Below we rely on the fact that VAL1 and VAL2 are both pointers or
1181 gcc_assert (POINTER_TYPE_P (TREE_TYPE (val1
))
1182 == POINTER_TYPE_P (TREE_TYPE (val2
)));
1184 /* Convert the two values into the same type. This is needed because
1185 sizetype causes sign extension even for unsigned types. */
1186 val2
= fold_convert (TREE_TYPE (val1
), val2
);
1187 STRIP_USELESS_TYPE_CONVERSION (val2
);
1189 if ((TREE_CODE (val1
) == SSA_NAME
1190 || (TREE_CODE (val1
) == NEGATE_EXPR
1191 && TREE_CODE (TREE_OPERAND (val1
, 0)) == SSA_NAME
)
1192 || TREE_CODE (val1
) == PLUS_EXPR
1193 || TREE_CODE (val1
) == MINUS_EXPR
)
1194 && (TREE_CODE (val2
) == SSA_NAME
1195 || (TREE_CODE (val2
) == NEGATE_EXPR
1196 && TREE_CODE (TREE_OPERAND (val2
, 0)) == SSA_NAME
)
1197 || TREE_CODE (val2
) == PLUS_EXPR
1198 || TREE_CODE (val2
) == MINUS_EXPR
))
1200 tree n1
, c1
, n2
, c2
;
1201 enum tree_code code1
, code2
;
1203 /* If VAL1 and VAL2 are of the form '[-]NAME [+-] CST' or 'NAME',
1204 return -1 or +1 accordingly. If VAL1 and VAL2 don't use the
1205 same name, return -2. */
1206 if (TREE_CODE (val1
) == SSA_NAME
|| TREE_CODE (val1
) == NEGATE_EXPR
)
1214 code1
= TREE_CODE (val1
);
1215 n1
= TREE_OPERAND (val1
, 0);
1216 c1
= TREE_OPERAND (val1
, 1);
1217 if (tree_int_cst_sgn (c1
) == -1)
1219 if (is_negative_overflow_infinity (c1
))
1221 c1
= fold_unary_to_constant (NEGATE_EXPR
, TREE_TYPE (c1
), c1
);
1224 code1
= code1
== MINUS_EXPR
? PLUS_EXPR
: MINUS_EXPR
;
1228 if (TREE_CODE (val2
) == SSA_NAME
|| TREE_CODE (val2
) == NEGATE_EXPR
)
1236 code2
= TREE_CODE (val2
);
1237 n2
= TREE_OPERAND (val2
, 0);
1238 c2
= TREE_OPERAND (val2
, 1);
1239 if (tree_int_cst_sgn (c2
) == -1)
1241 if (is_negative_overflow_infinity (c2
))
1243 c2
= fold_unary_to_constant (NEGATE_EXPR
, TREE_TYPE (c2
), c2
);
1246 code2
= code2
== MINUS_EXPR
? PLUS_EXPR
: MINUS_EXPR
;
1250 /* Both values must use the same name. */
1251 if (TREE_CODE (n1
) == NEGATE_EXPR
&& TREE_CODE (n2
) == NEGATE_EXPR
)
1253 n1
= TREE_OPERAND (n1
, 0);
1254 n2
= TREE_OPERAND (n2
, 0);
1259 if (code1
== SSA_NAME
&& code2
== SSA_NAME
)
1263 /* If overflow is defined we cannot simplify more. */
1264 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (val1
)))
1267 if (strict_overflow_p
!= NULL
1268 && (code1
== SSA_NAME
|| !TREE_NO_WARNING (val1
))
1269 && (code2
== SSA_NAME
|| !TREE_NO_WARNING (val2
)))
1270 *strict_overflow_p
= true;
1272 if (code1
== SSA_NAME
)
1274 if (code2
== PLUS_EXPR
)
1275 /* NAME < NAME + CST */
1277 else if (code2
== MINUS_EXPR
)
1278 /* NAME > NAME - CST */
1281 else if (code1
== PLUS_EXPR
)
1283 if (code2
== SSA_NAME
)
1284 /* NAME + CST > NAME */
1286 else if (code2
== PLUS_EXPR
)
1287 /* NAME + CST1 > NAME + CST2, if CST1 > CST2 */
1288 return compare_values_warnv (c1
, c2
, strict_overflow_p
);
1289 else if (code2
== MINUS_EXPR
)
1290 /* NAME + CST1 > NAME - CST2 */
1293 else if (code1
== MINUS_EXPR
)
1295 if (code2
== SSA_NAME
)
1296 /* NAME - CST < NAME */
1298 else if (code2
== PLUS_EXPR
)
1299 /* NAME - CST1 < NAME + CST2 */
1301 else if (code2
== MINUS_EXPR
)
1302 /* NAME - CST1 > NAME - CST2, if CST1 < CST2. Notice that
1303 C1 and C2 are swapped in the call to compare_values. */
1304 return compare_values_warnv (c2
, c1
, strict_overflow_p
);
1310 /* We cannot compare non-constants. */
1311 if (!is_gimple_min_invariant (val1
) || !is_gimple_min_invariant (val2
))
1314 if (!POINTER_TYPE_P (TREE_TYPE (val1
)))
1316 /* We cannot compare overflowed values, except for overflow
1318 if (TREE_OVERFLOW (val1
) || TREE_OVERFLOW (val2
))
1320 if (strict_overflow_p
!= NULL
)
1321 *strict_overflow_p
= true;
1322 if (is_negative_overflow_infinity (val1
))
1323 return is_negative_overflow_infinity (val2
) ? 0 : -1;
1324 else if (is_negative_overflow_infinity (val2
))
1326 else if (is_positive_overflow_infinity (val1
))
1327 return is_positive_overflow_infinity (val2
) ? 0 : 1;
1328 else if (is_positive_overflow_infinity (val2
))
1333 return tree_int_cst_compare (val1
, val2
);
1339 /* First see if VAL1 and VAL2 are not the same. */
1340 if (val1
== val2
|| operand_equal_p (val1
, val2
, 0))
1343 /* If VAL1 is a lower address than VAL2, return -1. */
1344 if (operand_less_p (val1
, val2
) == 1)
1347 /* If VAL1 is a higher address than VAL2, return +1. */
1348 if (operand_less_p (val2
, val1
) == 1)
1351 /* If VAL1 is different than VAL2, return +2.
1352 For integer constants we either have already returned -1 or 1
1353 or they are equivalent. We still might succeed in proving
1354 something about non-trivial operands. */
1355 if (TREE_CODE (val1
) != INTEGER_CST
1356 || TREE_CODE (val2
) != INTEGER_CST
)
1358 t
= fold_binary_to_constant (NE_EXPR
, boolean_type_node
, val1
, val2
);
1359 if (t
&& integer_onep (t
))
1367 /* Compare values like compare_values_warnv, but treat comparisons of
1368 nonconstants which rely on undefined overflow as incomparable. */
1371 compare_values (tree val1
, tree val2
)
1377 ret
= compare_values_warnv (val1
, val2
, &sop
);
1379 && (!is_gimple_min_invariant (val1
) || !is_gimple_min_invariant (val2
)))
1385 /* Return 1 if VAL is inside value range MIN <= VAL <= MAX,
1386 0 if VAL is not inside [MIN, MAX],
1387 -2 if we cannot tell either way.
1389 Benchmark compile/20001226-1.c compilation time after changing this
1393 value_inside_range (tree val
, tree min
, tree max
)
1397 cmp1
= operand_less_p (val
, min
);
1403 cmp2
= operand_less_p (max
, val
);
1411 /* Return true if value ranges VR0 and VR1 have a non-empty
1414 Benchmark compile/20001226-1.c compilation time after changing this
1419 value_ranges_intersect_p (value_range
*vr0
, value_range
*vr1
)
1421 /* The value ranges do not intersect if the maximum of the first range is
1422 less than the minimum of the second range or vice versa.
1423 When those relations are unknown, we can't do any better. */
1424 if (operand_less_p (vr0
->max
, vr1
->min
) != 0)
1426 if (operand_less_p (vr1
->max
, vr0
->min
) != 0)
1432 /* Return 1 if [MIN, MAX] includes the value zero, 0 if it does not
1433 include the value zero, -2 if we cannot tell. */
1436 range_includes_zero_p (tree min
, tree max
)
1438 tree zero
= build_int_cst (TREE_TYPE (min
), 0);
1439 return value_inside_range (zero
, min
, max
);
1442 /* Return true if *VR is know to only contain nonnegative values. */
1445 value_range_nonnegative_p (value_range
*vr
)
1447 /* Testing for VR_ANTI_RANGE is not useful here as any anti-range
1448 which would return a useful value should be encoded as a
1450 if (vr
->type
== VR_RANGE
)
1452 int result
= compare_values (vr
->min
, integer_zero_node
);
1453 return (result
== 0 || result
== 1);
1459 /* If *VR has a value rante that is a single constant value return that,
1460 otherwise return NULL_TREE. */
1463 value_range_constant_singleton (value_range
*vr
)
1465 if (vr
->type
== VR_RANGE
1466 && operand_equal_p (vr
->min
, vr
->max
, 0)
1467 && is_gimple_min_invariant (vr
->min
))
1473 /* If OP has a value range with a single constant value return that,
1474 otherwise return NULL_TREE. This returns OP itself if OP is a
1478 op_with_constant_singleton_value_range (tree op
)
1480 if (is_gimple_min_invariant (op
))
1483 if (TREE_CODE (op
) != SSA_NAME
)
1486 return value_range_constant_singleton (get_value_range (op
));
1489 /* Return true if op is in a boolean [0, 1] value-range. */
1492 op_with_boolean_value_range_p (tree op
)
1496 if (TYPE_PRECISION (TREE_TYPE (op
)) == 1)
1499 if (integer_zerop (op
)
1500 || integer_onep (op
))
1503 if (TREE_CODE (op
) != SSA_NAME
)
1506 vr
= get_value_range (op
);
1507 return (vr
->type
== VR_RANGE
1508 && integer_zerop (vr
->min
)
1509 && integer_onep (vr
->max
));
1512 /* Extract value range information from an ASSERT_EXPR EXPR and store
1516 extract_range_from_assert (value_range
*vr_p
, tree expr
)
1518 tree var
, cond
, limit
, min
, max
, type
;
1519 value_range
*limit_vr
;
1520 enum tree_code cond_code
;
1522 var
= ASSERT_EXPR_VAR (expr
);
1523 cond
= ASSERT_EXPR_COND (expr
);
1525 gcc_assert (COMPARISON_CLASS_P (cond
));
1527 /* Find VAR in the ASSERT_EXPR conditional. */
1528 if (var
== TREE_OPERAND (cond
, 0)
1529 || TREE_CODE (TREE_OPERAND (cond
, 0)) == PLUS_EXPR
1530 || TREE_CODE (TREE_OPERAND (cond
, 0)) == NOP_EXPR
)
1532 /* If the predicate is of the form VAR COMP LIMIT, then we just
1533 take LIMIT from the RHS and use the same comparison code. */
1534 cond_code
= TREE_CODE (cond
);
1535 limit
= TREE_OPERAND (cond
, 1);
1536 cond
= TREE_OPERAND (cond
, 0);
1540 /* If the predicate is of the form LIMIT COMP VAR, then we need
1541 to flip around the comparison code to create the proper range
1543 cond_code
= swap_tree_comparison (TREE_CODE (cond
));
1544 limit
= TREE_OPERAND (cond
, 0);
1545 cond
= TREE_OPERAND (cond
, 1);
1548 limit
= avoid_overflow_infinity (limit
);
1550 type
= TREE_TYPE (var
);
1551 gcc_assert (limit
!= var
);
1553 /* For pointer arithmetic, we only keep track of pointer equality
1555 if (POINTER_TYPE_P (type
) && cond_code
!= NE_EXPR
&& cond_code
!= EQ_EXPR
)
1557 set_value_range_to_varying (vr_p
);
1561 /* If LIMIT is another SSA name and LIMIT has a range of its own,
1562 try to use LIMIT's range to avoid creating symbolic ranges
1564 limit_vr
= (TREE_CODE (limit
) == SSA_NAME
) ? get_value_range (limit
) : NULL
;
1566 /* LIMIT's range is only interesting if it has any useful information. */
1568 && (limit_vr
->type
== VR_UNDEFINED
1569 || limit_vr
->type
== VR_VARYING
1570 || symbolic_range_p (limit_vr
)))
1573 /* Initially, the new range has the same set of equivalences of
1574 VAR's range. This will be revised before returning the final
1575 value. Since assertions may be chained via mutually exclusive
1576 predicates, we will need to trim the set of equivalences before
1578 gcc_assert (vr_p
->equiv
== NULL
);
1579 add_equivalence (&vr_p
->equiv
, var
);
1581 /* Extract a new range based on the asserted comparison for VAR and
1582 LIMIT's value range. Notice that if LIMIT has an anti-range, we
1583 will only use it for equality comparisons (EQ_EXPR). For any
1584 other kind of assertion, we cannot derive a range from LIMIT's
1585 anti-range that can be used to describe the new range. For
1586 instance, ASSERT_EXPR <x_2, x_2 <= b_4>. If b_4 is ~[2, 10],
1587 then b_4 takes on the ranges [-INF, 1] and [11, +INF]. There is
1588 no single range for x_2 that could describe LE_EXPR, so we might
1589 as well build the range [b_4, +INF] for it.
1590 One special case we handle is extracting a range from a
1591 range test encoded as (unsigned)var + CST <= limit. */
1592 if (TREE_CODE (cond
) == NOP_EXPR
1593 || TREE_CODE (cond
) == PLUS_EXPR
)
1595 if (TREE_CODE (cond
) == PLUS_EXPR
)
1597 min
= fold_build1 (NEGATE_EXPR
, TREE_TYPE (TREE_OPERAND (cond
, 1)),
1598 TREE_OPERAND (cond
, 1));
1599 max
= int_const_binop (PLUS_EXPR
, limit
, min
);
1600 cond
= TREE_OPERAND (cond
, 0);
1604 min
= build_int_cst (TREE_TYPE (var
), 0);
1608 /* Make sure to not set TREE_OVERFLOW on the final type
1609 conversion. We are willingly interpreting large positive
1610 unsigned values as negative signed values here. */
1611 min
= force_fit_type (TREE_TYPE (var
), wi::to_widest (min
), 0, false);
1612 max
= force_fit_type (TREE_TYPE (var
), wi::to_widest (max
), 0, false);
1614 /* We can transform a max, min range to an anti-range or
1615 vice-versa. Use set_and_canonicalize_value_range which does
1617 if (cond_code
== LE_EXPR
)
1618 set_and_canonicalize_value_range (vr_p
, VR_RANGE
,
1619 min
, max
, vr_p
->equiv
);
1620 else if (cond_code
== GT_EXPR
)
1621 set_and_canonicalize_value_range (vr_p
, VR_ANTI_RANGE
,
1622 min
, max
, vr_p
->equiv
);
1626 else if (cond_code
== EQ_EXPR
)
1628 enum value_range_type range_type
;
1632 range_type
= limit_vr
->type
;
1633 min
= limit_vr
->min
;
1634 max
= limit_vr
->max
;
1638 range_type
= VR_RANGE
;
1643 set_value_range (vr_p
, range_type
, min
, max
, vr_p
->equiv
);
1645 /* When asserting the equality VAR == LIMIT and LIMIT is another
1646 SSA name, the new range will also inherit the equivalence set
1648 if (TREE_CODE (limit
) == SSA_NAME
)
1649 add_equivalence (&vr_p
->equiv
, limit
);
1651 else if (cond_code
== NE_EXPR
)
1653 /* As described above, when LIMIT's range is an anti-range and
1654 this assertion is an inequality (NE_EXPR), then we cannot
1655 derive anything from the anti-range. For instance, if
1656 LIMIT's range was ~[0, 0], the assertion 'VAR != LIMIT' does
1657 not imply that VAR's range is [0, 0]. So, in the case of
1658 anti-ranges, we just assert the inequality using LIMIT and
1661 If LIMIT_VR is a range, we can only use it to build a new
1662 anti-range if LIMIT_VR is a single-valued range. For
1663 instance, if LIMIT_VR is [0, 1], the predicate
1664 VAR != [0, 1] does not mean that VAR's range is ~[0, 1].
1665 Rather, it means that for value 0 VAR should be ~[0, 0]
1666 and for value 1, VAR should be ~[1, 1]. We cannot
1667 represent these ranges.
1669 The only situation in which we can build a valid
1670 anti-range is when LIMIT_VR is a single-valued range
1671 (i.e., LIMIT_VR->MIN == LIMIT_VR->MAX). In that case,
1672 build the anti-range ~[LIMIT_VR->MIN, LIMIT_VR->MAX]. */
1674 && limit_vr
->type
== VR_RANGE
1675 && compare_values (limit_vr
->min
, limit_vr
->max
) == 0)
1677 min
= limit_vr
->min
;
1678 max
= limit_vr
->max
;
1682 /* In any other case, we cannot use LIMIT's range to build a
1683 valid anti-range. */
1687 /* If MIN and MAX cover the whole range for their type, then
1688 just use the original LIMIT. */
1689 if (INTEGRAL_TYPE_P (type
)
1690 && vrp_val_is_min (min
)
1691 && vrp_val_is_max (max
))
1694 set_and_canonicalize_value_range (vr_p
, VR_ANTI_RANGE
,
1695 min
, max
, vr_p
->equiv
);
1697 else if (cond_code
== LE_EXPR
|| cond_code
== LT_EXPR
)
1699 min
= TYPE_MIN_VALUE (type
);
1701 if (limit_vr
== NULL
|| limit_vr
->type
== VR_ANTI_RANGE
)
1705 /* If LIMIT_VR is of the form [N1, N2], we need to build the
1706 range [MIN, N2] for LE_EXPR and [MIN, N2 - 1] for
1708 max
= limit_vr
->max
;
1711 /* If the maximum value forces us to be out of bounds, simply punt.
1712 It would be pointless to try and do anything more since this
1713 all should be optimized away above us. */
1714 if ((cond_code
== LT_EXPR
1715 && compare_values (max
, min
) == 0)
1716 || is_overflow_infinity (max
))
1717 set_value_range_to_varying (vr_p
);
1720 /* For LT_EXPR, we create the range [MIN, MAX - 1]. */
1721 if (cond_code
== LT_EXPR
)
1723 if (TYPE_PRECISION (TREE_TYPE (max
)) == 1
1724 && !TYPE_UNSIGNED (TREE_TYPE (max
)))
1725 max
= fold_build2 (PLUS_EXPR
, TREE_TYPE (max
), max
,
1726 build_int_cst (TREE_TYPE (max
), -1));
1728 max
= fold_build2 (MINUS_EXPR
, TREE_TYPE (max
), max
,
1729 build_int_cst (TREE_TYPE (max
), 1));
1731 TREE_NO_WARNING (max
) = 1;
1734 set_value_range (vr_p
, VR_RANGE
, min
, max
, vr_p
->equiv
);
1737 else if (cond_code
== GE_EXPR
|| cond_code
== GT_EXPR
)
1739 max
= TYPE_MAX_VALUE (type
);
1741 if (limit_vr
== NULL
|| limit_vr
->type
== VR_ANTI_RANGE
)
1745 /* If LIMIT_VR is of the form [N1, N2], we need to build the
1746 range [N1, MAX] for GE_EXPR and [N1 + 1, MAX] for
1748 min
= limit_vr
->min
;
1751 /* If the minimum value forces us to be out of bounds, simply punt.
1752 It would be pointless to try and do anything more since this
1753 all should be optimized away above us. */
1754 if ((cond_code
== GT_EXPR
1755 && compare_values (min
, max
) == 0)
1756 || is_overflow_infinity (min
))
1757 set_value_range_to_varying (vr_p
);
1760 /* For GT_EXPR, we create the range [MIN + 1, MAX]. */
1761 if (cond_code
== GT_EXPR
)
1763 if (TYPE_PRECISION (TREE_TYPE (min
)) == 1
1764 && !TYPE_UNSIGNED (TREE_TYPE (min
)))
1765 min
= fold_build2 (MINUS_EXPR
, TREE_TYPE (min
), min
,
1766 build_int_cst (TREE_TYPE (min
), -1));
1768 min
= fold_build2 (PLUS_EXPR
, TREE_TYPE (min
), min
,
1769 build_int_cst (TREE_TYPE (min
), 1));
1771 TREE_NO_WARNING (min
) = 1;
1774 set_value_range (vr_p
, VR_RANGE
, min
, max
, vr_p
->equiv
);
1780 /* Finally intersect the new range with what we already know about var. */
1781 vrp_intersect_ranges (vr_p
, get_value_range (var
));
1785 /* Extract range information from SSA name VAR and store it in VR. If
1786 VAR has an interesting range, use it. Otherwise, create the
1787 range [VAR, VAR] and return it. This is useful in situations where
1788 we may have conditionals testing values of VARYING names. For
1795 Even if y_5 is deemed VARYING, we can determine that x_3 > y_5 is
1799 extract_range_from_ssa_name (value_range
*vr
, tree var
)
1801 value_range
*var_vr
= get_value_range (var
);
1803 if (var_vr
->type
!= VR_VARYING
)
1804 copy_value_range (vr
, var_vr
);
1806 set_value_range (vr
, VR_RANGE
, var
, var
, NULL
);
1808 add_equivalence (&vr
->equiv
, var
);
1812 /* Wrapper around int_const_binop. If the operation overflows and we
1813 are not using wrapping arithmetic, then adjust the result to be
1814 -INF or +INF depending on CODE, VAL1 and VAL2. This can return
1815 NULL_TREE if we need to use an overflow infinity representation but
1816 the type does not support it. */
1819 vrp_int_const_binop (enum tree_code code
, tree val1
, tree val2
)
1823 res
= int_const_binop (code
, val1
, val2
);
1825 /* If we are using unsigned arithmetic, operate symbolically
1826 on -INF and +INF as int_const_binop only handles signed overflow. */
1827 if (TYPE_UNSIGNED (TREE_TYPE (val1
)))
1829 int checkz
= compare_values (res
, val1
);
1830 bool overflow
= false;
1832 /* Ensure that res = val1 [+*] val2 >= val1
1833 or that res = val1 - val2 <= val1. */
1834 if ((code
== PLUS_EXPR
1835 && !(checkz
== 1 || checkz
== 0))
1836 || (code
== MINUS_EXPR
1837 && !(checkz
== 0 || checkz
== -1)))
1841 /* Checking for multiplication overflow is done by dividing the
1842 output of the multiplication by the first input of the
1843 multiplication. If the result of that division operation is
1844 not equal to the second input of the multiplication, then the
1845 multiplication overflowed. */
1846 else if (code
== MULT_EXPR
&& !integer_zerop (val1
))
1848 tree tmp
= int_const_binop (TRUNC_DIV_EXPR
,
1851 int check
= compare_values (tmp
, val2
);
1859 res
= copy_node (res
);
1860 TREE_OVERFLOW (res
) = 1;
1864 else if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (val1
)))
1865 /* If the singed operation wraps then int_const_binop has done
1866 everything we want. */
1868 /* Signed division of -1/0 overflows and by the time it gets here
1869 returns NULL_TREE. */
1872 else if ((TREE_OVERFLOW (res
)
1873 && !TREE_OVERFLOW (val1
)
1874 && !TREE_OVERFLOW (val2
))
1875 || is_overflow_infinity (val1
)
1876 || is_overflow_infinity (val2
))
1878 /* If the operation overflowed but neither VAL1 nor VAL2 are
1879 overflown, return -INF or +INF depending on the operation
1880 and the combination of signs of the operands. */
1881 int sgn1
= tree_int_cst_sgn (val1
);
1882 int sgn2
= tree_int_cst_sgn (val2
);
1884 if (needs_overflow_infinity (TREE_TYPE (res
))
1885 && !supports_overflow_infinity (TREE_TYPE (res
)))
1888 /* We have to punt on adding infinities of different signs,
1889 since we can't tell what the sign of the result should be.
1890 Likewise for subtracting infinities of the same sign. */
1891 if (((code
== PLUS_EXPR
&& sgn1
!= sgn2
)
1892 || (code
== MINUS_EXPR
&& sgn1
== sgn2
))
1893 && is_overflow_infinity (val1
)
1894 && is_overflow_infinity (val2
))
1897 /* Don't try to handle division or shifting of infinities. */
1898 if ((code
== TRUNC_DIV_EXPR
1899 || code
== FLOOR_DIV_EXPR
1900 || code
== CEIL_DIV_EXPR
1901 || code
== EXACT_DIV_EXPR
1902 || code
== ROUND_DIV_EXPR
1903 || code
== RSHIFT_EXPR
)
1904 && (is_overflow_infinity (val1
)
1905 || is_overflow_infinity (val2
)))
1908 /* Notice that we only need to handle the restricted set of
1909 operations handled by extract_range_from_binary_expr.
1910 Among them, only multiplication, addition and subtraction
1911 can yield overflow without overflown operands because we
1912 are working with integral types only... except in the
1913 case VAL1 = -INF and VAL2 = -1 which overflows to +INF
1914 for division too. */
1916 /* For multiplication, the sign of the overflow is given
1917 by the comparison of the signs of the operands. */
1918 if ((code
== MULT_EXPR
&& sgn1
== sgn2
)
1919 /* For addition, the operands must be of the same sign
1920 to yield an overflow. Its sign is therefore that
1921 of one of the operands, for example the first. For
1922 infinite operands X + -INF is negative, not positive. */
1923 || (code
== PLUS_EXPR
1925 ? !is_negative_overflow_infinity (val2
)
1926 : is_positive_overflow_infinity (val2
)))
1927 /* For subtraction, non-infinite operands must be of
1928 different signs to yield an overflow. Its sign is
1929 therefore that of the first operand or the opposite of
1930 that of the second operand. A first operand of 0 counts
1931 as positive here, for the corner case 0 - (-INF), which
1932 overflows, but must yield +INF. For infinite operands 0
1933 - INF is negative, not positive. */
1934 || (code
== MINUS_EXPR
1936 ? !is_positive_overflow_infinity (val2
)
1937 : is_negative_overflow_infinity (val2
)))
1938 /* We only get in here with positive shift count, so the
1939 overflow direction is the same as the sign of val1.
1940 Actually rshift does not overflow at all, but we only
1941 handle the case of shifting overflowed -INF and +INF. */
1942 || (code
== RSHIFT_EXPR
1944 /* For division, the only case is -INF / -1 = +INF. */
1945 || code
== TRUNC_DIV_EXPR
1946 || code
== FLOOR_DIV_EXPR
1947 || code
== CEIL_DIV_EXPR
1948 || code
== EXACT_DIV_EXPR
1949 || code
== ROUND_DIV_EXPR
)
1950 return (needs_overflow_infinity (TREE_TYPE (res
))
1951 ? positive_overflow_infinity (TREE_TYPE (res
))
1952 : TYPE_MAX_VALUE (TREE_TYPE (res
)));
1954 return (needs_overflow_infinity (TREE_TYPE (res
))
1955 ? negative_overflow_infinity (TREE_TYPE (res
))
1956 : TYPE_MIN_VALUE (TREE_TYPE (res
)));
1963 /* For range VR compute two wide_int bitmasks. In *MAY_BE_NONZERO
1964 bitmask if some bit is unset, it means for all numbers in the range
1965 the bit is 0, otherwise it might be 0 or 1. In *MUST_BE_NONZERO
1966 bitmask if some bit is set, it means for all numbers in the range
1967 the bit is 1, otherwise it might be 0 or 1. */
1970 zero_nonzero_bits_from_vr (const tree expr_type
,
1972 wide_int
*may_be_nonzero
,
1973 wide_int
*must_be_nonzero
)
1975 *may_be_nonzero
= wi::minus_one (TYPE_PRECISION (expr_type
));
1976 *must_be_nonzero
= wi::zero (TYPE_PRECISION (expr_type
));
1977 if (!range_int_cst_p (vr
)
1978 || is_overflow_infinity (vr
->min
)
1979 || is_overflow_infinity (vr
->max
))
1982 if (range_int_cst_singleton_p (vr
))
1984 *may_be_nonzero
= vr
->min
;
1985 *must_be_nonzero
= *may_be_nonzero
;
1987 else if (tree_int_cst_sgn (vr
->min
) >= 0
1988 || tree_int_cst_sgn (vr
->max
) < 0)
1990 wide_int xor_mask
= wi::bit_xor (vr
->min
, vr
->max
);
1991 *may_be_nonzero
= wi::bit_or (vr
->min
, vr
->max
);
1992 *must_be_nonzero
= wi::bit_and (vr
->min
, vr
->max
);
1995 wide_int mask
= wi::mask (wi::floor_log2 (xor_mask
), false,
1996 may_be_nonzero
->get_precision ());
1997 *may_be_nonzero
= *may_be_nonzero
| mask
;
1998 *must_be_nonzero
= must_be_nonzero
->and_not (mask
);
2005 /* Create two value-ranges in *VR0 and *VR1 from the anti-range *AR
2006 so that *VR0 U *VR1 == *AR. Returns true if that is possible,
2007 false otherwise. If *AR can be represented with a single range
2008 *VR1 will be VR_UNDEFINED. */
2011 ranges_from_anti_range (value_range
*ar
,
2012 value_range
*vr0
, value_range
*vr1
)
2014 tree type
= TREE_TYPE (ar
->min
);
2016 vr0
->type
= VR_UNDEFINED
;
2017 vr1
->type
= VR_UNDEFINED
;
2019 if (ar
->type
!= VR_ANTI_RANGE
2020 || TREE_CODE (ar
->min
) != INTEGER_CST
2021 || TREE_CODE (ar
->max
) != INTEGER_CST
2022 || !vrp_val_min (type
)
2023 || !vrp_val_max (type
))
2026 if (!vrp_val_is_min (ar
->min
))
2028 vr0
->type
= VR_RANGE
;
2029 vr0
->min
= vrp_val_min (type
);
2030 vr0
->max
= wide_int_to_tree (type
, wi::sub (ar
->min
, 1));
2032 if (!vrp_val_is_max (ar
->max
))
2034 vr1
->type
= VR_RANGE
;
2035 vr1
->min
= wide_int_to_tree (type
, wi::add (ar
->max
, 1));
2036 vr1
->max
= vrp_val_max (type
);
2038 if (vr0
->type
== VR_UNDEFINED
)
2041 vr1
->type
= VR_UNDEFINED
;
2044 return vr0
->type
!= VR_UNDEFINED
;
2047 /* Helper to extract a value-range *VR for a multiplicative operation
2051 extract_range_from_multiplicative_op_1 (value_range
*vr
,
2052 enum tree_code code
,
2053 value_range
*vr0
, value_range
*vr1
)
2055 enum value_range_type type
;
2062 /* Multiplications, divisions and shifts are a bit tricky to handle,
2063 depending on the mix of signs we have in the two ranges, we
2064 need to operate on different values to get the minimum and
2065 maximum values for the new range. One approach is to figure
2066 out all the variations of range combinations and do the
2069 However, this involves several calls to compare_values and it
2070 is pretty convoluted. It's simpler to do the 4 operations
2071 (MIN0 OP MIN1, MIN0 OP MAX1, MAX0 OP MIN1 and MAX0 OP MAX0 OP
2072 MAX1) and then figure the smallest and largest values to form
2074 gcc_assert (code
== MULT_EXPR
2075 || code
== TRUNC_DIV_EXPR
2076 || code
== FLOOR_DIV_EXPR
2077 || code
== CEIL_DIV_EXPR
2078 || code
== EXACT_DIV_EXPR
2079 || code
== ROUND_DIV_EXPR
2080 || code
== RSHIFT_EXPR
2081 || code
== LSHIFT_EXPR
);
2082 gcc_assert ((vr0
->type
== VR_RANGE
2083 || (code
== MULT_EXPR
&& vr0
->type
== VR_ANTI_RANGE
))
2084 && vr0
->type
== vr1
->type
);
2088 /* Compute the 4 cross operations. */
2090 val
[0] = vrp_int_const_binop (code
, vr0
->min
, vr1
->min
);
2091 if (val
[0] == NULL_TREE
)
2094 if (vr1
->max
== vr1
->min
)
2098 val
[1] = vrp_int_const_binop (code
, vr0
->min
, vr1
->max
);
2099 if (val
[1] == NULL_TREE
)
2103 if (vr0
->max
== vr0
->min
)
2107 val
[2] = vrp_int_const_binop (code
, vr0
->max
, vr1
->min
);
2108 if (val
[2] == NULL_TREE
)
2112 if (vr0
->min
== vr0
->max
|| vr1
->min
== vr1
->max
)
2116 val
[3] = vrp_int_const_binop (code
, vr0
->max
, vr1
->max
);
2117 if (val
[3] == NULL_TREE
)
2123 set_value_range_to_varying (vr
);
2127 /* Set MIN to the minimum of VAL[i] and MAX to the maximum
2131 for (i
= 1; i
< 4; i
++)
2133 if (!is_gimple_min_invariant (min
)
2134 || (TREE_OVERFLOW (min
) && !is_overflow_infinity (min
))
2135 || !is_gimple_min_invariant (max
)
2136 || (TREE_OVERFLOW (max
) && !is_overflow_infinity (max
)))
2141 if (!is_gimple_min_invariant (val
[i
])
2142 || (TREE_OVERFLOW (val
[i
])
2143 && !is_overflow_infinity (val
[i
])))
2145 /* If we found an overflowed value, set MIN and MAX
2146 to it so that we set the resulting range to
2152 if (compare_values (val
[i
], min
) == -1)
2155 if (compare_values (val
[i
], max
) == 1)
2160 /* If either MIN or MAX overflowed, then set the resulting range to
2161 VARYING. But we do accept an overflow infinity
2163 if (min
== NULL_TREE
2164 || !is_gimple_min_invariant (min
)
2165 || (TREE_OVERFLOW (min
) && !is_overflow_infinity (min
))
2167 || !is_gimple_min_invariant (max
)
2168 || (TREE_OVERFLOW (max
) && !is_overflow_infinity (max
)))
2170 set_value_range_to_varying (vr
);
2176 2) [-INF, +-INF(OVF)]
2177 3) [+-INF(OVF), +INF]
2178 4) [+-INF(OVF), +-INF(OVF)]
2179 We learn nothing when we have INF and INF(OVF) on both sides.
2180 Note that we do accept [-INF, -INF] and [+INF, +INF] without
2182 if ((vrp_val_is_min (min
) || is_overflow_infinity (min
))
2183 && (vrp_val_is_max (max
) || is_overflow_infinity (max
)))
2185 set_value_range_to_varying (vr
);
2189 cmp
= compare_values (min
, max
);
2190 if (cmp
== -2 || cmp
== 1)
2192 /* If the new range has its limits swapped around (MIN > MAX),
2193 then the operation caused one of them to wrap around, mark
2194 the new range VARYING. */
2195 set_value_range_to_varying (vr
);
2198 set_value_range (vr
, type
, min
, max
, NULL
);
2201 /* Extract range information from a binary operation CODE based on
2202 the ranges of each of its operands *VR0 and *VR1 with resulting
2203 type EXPR_TYPE. The resulting range is stored in *VR. */
2206 extract_range_from_binary_expr_1 (value_range
*vr
,
2207 enum tree_code code
, tree expr_type
,
2208 value_range
*vr0_
, value_range
*vr1_
)
2210 value_range vr0
= *vr0_
, vr1
= *vr1_
;
2211 value_range vrtem0
= VR_INITIALIZER
, vrtem1
= VR_INITIALIZER
;
2212 enum value_range_type type
;
2213 tree min
= NULL_TREE
, max
= NULL_TREE
;
2216 if (!INTEGRAL_TYPE_P (expr_type
)
2217 && !POINTER_TYPE_P (expr_type
))
2219 set_value_range_to_varying (vr
);
2223 /* Not all binary expressions can be applied to ranges in a
2224 meaningful way. Handle only arithmetic operations. */
2225 if (code
!= PLUS_EXPR
2226 && code
!= MINUS_EXPR
2227 && code
!= POINTER_PLUS_EXPR
2228 && code
!= MULT_EXPR
2229 && code
!= TRUNC_DIV_EXPR
2230 && code
!= FLOOR_DIV_EXPR
2231 && code
!= CEIL_DIV_EXPR
2232 && code
!= EXACT_DIV_EXPR
2233 && code
!= ROUND_DIV_EXPR
2234 && code
!= TRUNC_MOD_EXPR
2235 && code
!= RSHIFT_EXPR
2236 && code
!= LSHIFT_EXPR
2239 && code
!= BIT_AND_EXPR
2240 && code
!= BIT_IOR_EXPR
2241 && code
!= BIT_XOR_EXPR
)
2243 set_value_range_to_varying (vr
);
2247 /* If both ranges are UNDEFINED, so is the result. */
2248 if (vr0
.type
== VR_UNDEFINED
&& vr1
.type
== VR_UNDEFINED
)
2250 set_value_range_to_undefined (vr
);
2253 /* If one of the ranges is UNDEFINED drop it to VARYING for the following
2254 code. At some point we may want to special-case operations that
2255 have UNDEFINED result for all or some value-ranges of the not UNDEFINED
2257 else if (vr0
.type
== VR_UNDEFINED
)
2258 set_value_range_to_varying (&vr0
);
2259 else if (vr1
.type
== VR_UNDEFINED
)
2260 set_value_range_to_varying (&vr1
);
2262 /* Now canonicalize anti-ranges to ranges when they are not symbolic
2263 and express ~[] op X as ([]' op X) U ([]'' op X). */
2264 if (vr0
.type
== VR_ANTI_RANGE
2265 && ranges_from_anti_range (&vr0
, &vrtem0
, &vrtem1
))
2267 extract_range_from_binary_expr_1 (vr
, code
, expr_type
, &vrtem0
, vr1_
);
2268 if (vrtem1
.type
!= VR_UNDEFINED
)
2270 value_range vrres
= VR_INITIALIZER
;
2271 extract_range_from_binary_expr_1 (&vrres
, code
, expr_type
,
2273 vrp_meet (vr
, &vrres
);
2277 /* Likewise for X op ~[]. */
2278 if (vr1
.type
== VR_ANTI_RANGE
2279 && ranges_from_anti_range (&vr1
, &vrtem0
, &vrtem1
))
2281 extract_range_from_binary_expr_1 (vr
, code
, expr_type
, vr0_
, &vrtem0
);
2282 if (vrtem1
.type
!= VR_UNDEFINED
)
2284 value_range vrres
= VR_INITIALIZER
;
2285 extract_range_from_binary_expr_1 (&vrres
, code
, expr_type
,
2287 vrp_meet (vr
, &vrres
);
2292 /* The type of the resulting value range defaults to VR0.TYPE. */
2295 /* Refuse to operate on VARYING ranges, ranges of different kinds
2296 and symbolic ranges. As an exception, we allow BIT_{AND,IOR}
2297 because we may be able to derive a useful range even if one of
2298 the operands is VR_VARYING or symbolic range. Similarly for
2299 divisions, MIN/MAX and PLUS/MINUS.
2301 TODO, we may be able to derive anti-ranges in some cases. */
2302 if (code
!= BIT_AND_EXPR
2303 && code
!= BIT_IOR_EXPR
2304 && code
!= TRUNC_DIV_EXPR
2305 && code
!= FLOOR_DIV_EXPR
2306 && code
!= CEIL_DIV_EXPR
2307 && code
!= EXACT_DIV_EXPR
2308 && code
!= ROUND_DIV_EXPR
2309 && code
!= TRUNC_MOD_EXPR
2312 && code
!= PLUS_EXPR
2313 && code
!= MINUS_EXPR
2314 && code
!= RSHIFT_EXPR
2315 && (vr0
.type
== VR_VARYING
2316 || vr1
.type
== VR_VARYING
2317 || vr0
.type
!= vr1
.type
2318 || symbolic_range_p (&vr0
)
2319 || symbolic_range_p (&vr1
)))
2321 set_value_range_to_varying (vr
);
2325 /* Now evaluate the expression to determine the new range. */
2326 if (POINTER_TYPE_P (expr_type
))
2328 if (code
== MIN_EXPR
|| code
== MAX_EXPR
)
2330 /* For MIN/MAX expressions with pointers, we only care about
2331 nullness, if both are non null, then the result is nonnull.
2332 If both are null, then the result is null. Otherwise they
2334 if (range_is_nonnull (&vr0
) && range_is_nonnull (&vr1
))
2335 set_value_range_to_nonnull (vr
, expr_type
);
2336 else if (range_is_null (&vr0
) && range_is_null (&vr1
))
2337 set_value_range_to_null (vr
, expr_type
);
2339 set_value_range_to_varying (vr
);
2341 else if (code
== POINTER_PLUS_EXPR
)
2343 /* For pointer types, we are really only interested in asserting
2344 whether the expression evaluates to non-NULL. */
2345 if (range_is_nonnull (&vr0
) || range_is_nonnull (&vr1
))
2346 set_value_range_to_nonnull (vr
, expr_type
);
2347 else if (range_is_null (&vr0
) && range_is_null (&vr1
))
2348 set_value_range_to_null (vr
, expr_type
);
2350 set_value_range_to_varying (vr
);
2352 else if (code
== BIT_AND_EXPR
)
2354 /* For pointer types, we are really only interested in asserting
2355 whether the expression evaluates to non-NULL. */
2356 if (range_is_nonnull (&vr0
) && range_is_nonnull (&vr1
))
2357 set_value_range_to_nonnull (vr
, expr_type
);
2358 else if (range_is_null (&vr0
) || range_is_null (&vr1
))
2359 set_value_range_to_null (vr
, expr_type
);
2361 set_value_range_to_varying (vr
);
2364 set_value_range_to_varying (vr
);
2369 /* For integer ranges, apply the operation to each end of the
2370 range and see what we end up with. */
2371 if (code
== PLUS_EXPR
|| code
== MINUS_EXPR
)
2373 const bool minus_p
= (code
== MINUS_EXPR
);
2374 tree min_op0
= vr0
.min
;
2375 tree min_op1
= minus_p
? vr1
.max
: vr1
.min
;
2376 tree max_op0
= vr0
.max
;
2377 tree max_op1
= minus_p
? vr1
.min
: vr1
.max
;
2378 tree sym_min_op0
= NULL_TREE
;
2379 tree sym_min_op1
= NULL_TREE
;
2380 tree sym_max_op0
= NULL_TREE
;
2381 tree sym_max_op1
= NULL_TREE
;
2382 bool neg_min_op0
, neg_min_op1
, neg_max_op0
, neg_max_op1
;
2384 /* If we have a PLUS or MINUS with two VR_RANGEs, either constant or
2385 single-symbolic ranges, try to compute the precise resulting range,
2386 but only if we know that this resulting range will also be constant
2387 or single-symbolic. */
2388 if (vr0
.type
== VR_RANGE
&& vr1
.type
== VR_RANGE
2389 && (TREE_CODE (min_op0
) == INTEGER_CST
2391 = get_single_symbol (min_op0
, &neg_min_op0
, &min_op0
)))
2392 && (TREE_CODE (min_op1
) == INTEGER_CST
2394 = get_single_symbol (min_op1
, &neg_min_op1
, &min_op1
)))
2395 && (!(sym_min_op0
&& sym_min_op1
)
2396 || (sym_min_op0
== sym_min_op1
2397 && neg_min_op0
== (minus_p
? neg_min_op1
: !neg_min_op1
)))
2398 && (TREE_CODE (max_op0
) == INTEGER_CST
2400 = get_single_symbol (max_op0
, &neg_max_op0
, &max_op0
)))
2401 && (TREE_CODE (max_op1
) == INTEGER_CST
2403 = get_single_symbol (max_op1
, &neg_max_op1
, &max_op1
)))
2404 && (!(sym_max_op0
&& sym_max_op1
)
2405 || (sym_max_op0
== sym_max_op1
2406 && neg_max_op0
== (minus_p
? neg_max_op1
: !neg_max_op1
))))
2408 const signop sgn
= TYPE_SIGN (expr_type
);
2409 const unsigned int prec
= TYPE_PRECISION (expr_type
);
2410 wide_int type_min
, type_max
, wmin
, wmax
;
2414 /* Get the lower and upper bounds of the type. */
2415 if (TYPE_OVERFLOW_WRAPS (expr_type
))
2417 type_min
= wi::min_value (prec
, sgn
);
2418 type_max
= wi::max_value (prec
, sgn
);
2422 type_min
= vrp_val_min (expr_type
);
2423 type_max
= vrp_val_max (expr_type
);
2426 /* Combine the lower bounds, if any. */
2427 if (min_op0
&& min_op1
)
2431 wmin
= wi::sub (min_op0
, min_op1
);
2433 /* Check for overflow. */
2434 if (wi::cmp (0, min_op1
, sgn
)
2435 != wi::cmp (wmin
, min_op0
, sgn
))
2436 min_ovf
= wi::cmp (min_op0
, min_op1
, sgn
);
2440 wmin
= wi::add (min_op0
, min_op1
);
2442 /* Check for overflow. */
2443 if (wi::cmp (min_op1
, 0, sgn
)
2444 != wi::cmp (wmin
, min_op0
, sgn
))
2445 min_ovf
= wi::cmp (min_op0
, wmin
, sgn
);
2451 wmin
= minus_p
? wi::neg (min_op1
) : min_op1
;
2453 wmin
= wi::shwi (0, prec
);
2455 /* Combine the upper bounds, if any. */
2456 if (max_op0
&& max_op1
)
2460 wmax
= wi::sub (max_op0
, max_op1
);
2462 /* Check for overflow. */
2463 if (wi::cmp (0, max_op1
, sgn
)
2464 != wi::cmp (wmax
, max_op0
, sgn
))
2465 max_ovf
= wi::cmp (max_op0
, max_op1
, sgn
);
2469 wmax
= wi::add (max_op0
, max_op1
);
2471 if (wi::cmp (max_op1
, 0, sgn
)
2472 != wi::cmp (wmax
, max_op0
, sgn
))
2473 max_ovf
= wi::cmp (max_op0
, wmax
, sgn
);
2479 wmax
= minus_p
? wi::neg (max_op1
) : max_op1
;
2481 wmax
= wi::shwi (0, prec
);
2483 /* Check for type overflow. */
2486 if (wi::cmp (wmin
, type_min
, sgn
) == -1)
2488 else if (wi::cmp (wmin
, type_max
, sgn
) == 1)
2493 if (wi::cmp (wmax
, type_min
, sgn
) == -1)
2495 else if (wi::cmp (wmax
, type_max
, sgn
) == 1)
2499 /* If we have overflow for the constant part and the resulting
2500 range will be symbolic, drop to VR_VARYING. */
2501 if ((min_ovf
&& sym_min_op0
!= sym_min_op1
)
2502 || (max_ovf
&& sym_max_op0
!= sym_max_op1
))
2504 set_value_range_to_varying (vr
);
2508 if (TYPE_OVERFLOW_WRAPS (expr_type
))
2510 /* If overflow wraps, truncate the values and adjust the
2511 range kind and bounds appropriately. */
2512 wide_int tmin
= wide_int::from (wmin
, prec
, sgn
);
2513 wide_int tmax
= wide_int::from (wmax
, prec
, sgn
);
2514 if (min_ovf
== max_ovf
)
2516 /* No overflow or both overflow or underflow. The
2517 range kind stays VR_RANGE. */
2518 min
= wide_int_to_tree (expr_type
, tmin
);
2519 max
= wide_int_to_tree (expr_type
, tmax
);
2521 else if (min_ovf
== -1 && max_ovf
== 1)
2523 /* Underflow and overflow, drop to VR_VARYING. */
2524 set_value_range_to_varying (vr
);
2529 /* Min underflow or max overflow. The range kind
2530 changes to VR_ANTI_RANGE. */
2531 bool covers
= false;
2532 wide_int tem
= tmin
;
2533 gcc_assert ((min_ovf
== -1 && max_ovf
== 0)
2534 || (max_ovf
== 1 && min_ovf
== 0));
2535 type
= VR_ANTI_RANGE
;
2537 if (wi::cmp (tmin
, tmax
, sgn
) < 0)
2540 if (wi::cmp (tmax
, tem
, sgn
) > 0)
2542 /* If the anti-range would cover nothing, drop to varying.
2543 Likewise if the anti-range bounds are outside of the
2545 if (covers
|| wi::cmp (tmin
, tmax
, sgn
) > 0)
2547 set_value_range_to_varying (vr
);
2550 min
= wide_int_to_tree (expr_type
, tmin
);
2551 max
= wide_int_to_tree (expr_type
, tmax
);
2556 /* If overflow does not wrap, saturate to the types min/max
2560 if (needs_overflow_infinity (expr_type
)
2561 && supports_overflow_infinity (expr_type
))
2562 min
= negative_overflow_infinity (expr_type
);
2564 min
= wide_int_to_tree (expr_type
, type_min
);
2566 else if (min_ovf
== 1)
2568 if (needs_overflow_infinity (expr_type
)
2569 && supports_overflow_infinity (expr_type
))
2570 min
= positive_overflow_infinity (expr_type
);
2572 min
= wide_int_to_tree (expr_type
, type_max
);
2575 min
= wide_int_to_tree (expr_type
, wmin
);
2579 if (needs_overflow_infinity (expr_type
)
2580 && supports_overflow_infinity (expr_type
))
2581 max
= negative_overflow_infinity (expr_type
);
2583 max
= wide_int_to_tree (expr_type
, type_min
);
2585 else if (max_ovf
== 1)
2587 if (needs_overflow_infinity (expr_type
)
2588 && supports_overflow_infinity (expr_type
))
2589 max
= positive_overflow_infinity (expr_type
);
2591 max
= wide_int_to_tree (expr_type
, type_max
);
2594 max
= wide_int_to_tree (expr_type
, wmax
);
2597 if (needs_overflow_infinity (expr_type
)
2598 && supports_overflow_infinity (expr_type
))
2600 if ((min_op0
&& is_negative_overflow_infinity (min_op0
))
2603 ? is_positive_overflow_infinity (min_op1
)
2604 : is_negative_overflow_infinity (min_op1
))))
2605 min
= negative_overflow_infinity (expr_type
);
2606 if ((max_op0
&& is_positive_overflow_infinity (max_op0
))
2609 ? is_negative_overflow_infinity (max_op1
)
2610 : is_positive_overflow_infinity (max_op1
))))
2611 max
= positive_overflow_infinity (expr_type
);
2614 /* If the result lower bound is constant, we're done;
2615 otherwise, build the symbolic lower bound. */
2616 if (sym_min_op0
== sym_min_op1
)
2618 else if (sym_min_op0
)
2619 min
= build_symbolic_expr (expr_type
, sym_min_op0
,
2621 else if (sym_min_op1
)
2622 min
= build_symbolic_expr (expr_type
, sym_min_op1
,
2623 neg_min_op1
^ minus_p
, min
);
2625 /* Likewise for the upper bound. */
2626 if (sym_max_op0
== sym_max_op1
)
2628 else if (sym_max_op0
)
2629 max
= build_symbolic_expr (expr_type
, sym_max_op0
,
2631 else if (sym_max_op1
)
2632 max
= build_symbolic_expr (expr_type
, sym_max_op1
,
2633 neg_max_op1
^ minus_p
, max
);
2637 /* For other cases, for example if we have a PLUS_EXPR with two
2638 VR_ANTI_RANGEs, drop to VR_VARYING. It would take more effort
2639 to compute a precise range for such a case.
2640 ??? General even mixed range kind operations can be expressed
2641 by for example transforming ~[3, 5] + [1, 2] to range-only
2642 operations and a union primitive:
2643 [-INF, 2] + [1, 2] U [5, +INF] + [1, 2]
2644 [-INF+1, 4] U [6, +INF(OVF)]
2645 though usually the union is not exactly representable with
2646 a single range or anti-range as the above is
2647 [-INF+1, +INF(OVF)] intersected with ~[5, 5]
2648 but one could use a scheme similar to equivalences for this. */
2649 set_value_range_to_varying (vr
);
2653 else if (code
== MIN_EXPR
2654 || code
== MAX_EXPR
)
2656 if (vr0
.type
== VR_RANGE
2657 && !symbolic_range_p (&vr0
))
2660 if (vr1
.type
== VR_RANGE
2661 && !symbolic_range_p (&vr1
))
2663 /* For operations that make the resulting range directly
2664 proportional to the original ranges, apply the operation to
2665 the same end of each range. */
2666 min
= vrp_int_const_binop (code
, vr0
.min
, vr1
.min
);
2667 max
= vrp_int_const_binop (code
, vr0
.max
, vr1
.max
);
2669 else if (code
== MIN_EXPR
)
2671 min
= vrp_val_min (expr_type
);
2674 else if (code
== MAX_EXPR
)
2677 max
= vrp_val_max (expr_type
);
2680 else if (vr1
.type
== VR_RANGE
2681 && !symbolic_range_p (&vr1
))
2684 if (code
== MIN_EXPR
)
2686 min
= vrp_val_min (expr_type
);
2689 else if (code
== MAX_EXPR
)
2692 max
= vrp_val_max (expr_type
);
2697 set_value_range_to_varying (vr
);
2701 else if (code
== MULT_EXPR
)
2703 /* Fancy code so that with unsigned, [-3,-1]*[-3,-1] does not
2704 drop to varying. This test requires 2*prec bits if both
2705 operands are signed and 2*prec + 2 bits if either is not. */
2707 signop sign
= TYPE_SIGN (expr_type
);
2708 unsigned int prec
= TYPE_PRECISION (expr_type
);
2710 if (range_int_cst_p (&vr0
)
2711 && range_int_cst_p (&vr1
)
2712 && TYPE_OVERFLOW_WRAPS (expr_type
))
2714 typedef FIXED_WIDE_INT (WIDE_INT_MAX_PRECISION
* 2) vrp_int
;
2715 typedef generic_wide_int
2716 <wi::extended_tree
<WIDE_INT_MAX_PRECISION
* 2> > vrp_int_cst
;
2717 vrp_int sizem1
= wi::mask
<vrp_int
> (prec
, false);
2718 vrp_int size
= sizem1
+ 1;
2720 /* Extend the values using the sign of the result to PREC2.
2721 From here on out, everthing is just signed math no matter
2722 what the input types were. */
2723 vrp_int min0
= vrp_int_cst (vr0
.min
);
2724 vrp_int max0
= vrp_int_cst (vr0
.max
);
2725 vrp_int min1
= vrp_int_cst (vr1
.min
);
2726 vrp_int max1
= vrp_int_cst (vr1
.max
);
2727 /* Canonicalize the intervals. */
2728 if (sign
== UNSIGNED
)
2730 if (wi::ltu_p (size
, min0
+ max0
))
2736 if (wi::ltu_p (size
, min1
+ max1
))
2743 vrp_int prod0
= min0
* min1
;
2744 vrp_int prod1
= min0
* max1
;
2745 vrp_int prod2
= max0
* min1
;
2746 vrp_int prod3
= max0
* max1
;
2748 /* Sort the 4 products so that min is in prod0 and max is in
2750 /* min0min1 > max0max1 */
2751 if (wi::gts_p (prod0
, prod3
))
2752 std::swap (prod0
, prod3
);
2754 /* min0max1 > max0min1 */
2755 if (wi::gts_p (prod1
, prod2
))
2756 std::swap (prod1
, prod2
);
2758 if (wi::gts_p (prod0
, prod1
))
2759 std::swap (prod0
, prod1
);
2761 if (wi::gts_p (prod2
, prod3
))
2762 std::swap (prod2
, prod3
);
2764 /* diff = max - min. */
2765 prod2
= prod3
- prod0
;
2766 if (wi::geu_p (prod2
, sizem1
))
2768 /* the range covers all values. */
2769 set_value_range_to_varying (vr
);
2773 /* The following should handle the wrapping and selecting
2774 VR_ANTI_RANGE for us. */
2775 min
= wide_int_to_tree (expr_type
, prod0
);
2776 max
= wide_int_to_tree (expr_type
, prod3
);
2777 set_and_canonicalize_value_range (vr
, VR_RANGE
, min
, max
, NULL
);
2781 /* If we have an unsigned MULT_EXPR with two VR_ANTI_RANGEs,
2782 drop to VR_VARYING. It would take more effort to compute a
2783 precise range for such a case. For example, if we have
2784 op0 == 65536 and op1 == 65536 with their ranges both being
2785 ~[0,0] on a 32-bit machine, we would have op0 * op1 == 0, so
2786 we cannot claim that the product is in ~[0,0]. Note that we
2787 are guaranteed to have vr0.type == vr1.type at this
2789 if (vr0
.type
== VR_ANTI_RANGE
2790 && !TYPE_OVERFLOW_UNDEFINED (expr_type
))
2792 set_value_range_to_varying (vr
);
2796 extract_range_from_multiplicative_op_1 (vr
, code
, &vr0
, &vr1
);
2799 else if (code
== RSHIFT_EXPR
2800 || code
== LSHIFT_EXPR
)
2802 /* If we have a RSHIFT_EXPR with any shift values outside [0..prec-1],
2803 then drop to VR_VARYING. Outside of this range we get undefined
2804 behavior from the shift operation. We cannot even trust
2805 SHIFT_COUNT_TRUNCATED at this stage, because that applies to rtl
2806 shifts, and the operation at the tree level may be widened. */
2807 if (range_int_cst_p (&vr1
)
2808 && compare_tree_int (vr1
.min
, 0) >= 0
2809 && compare_tree_int (vr1
.max
, TYPE_PRECISION (expr_type
)) == -1)
2811 if (code
== RSHIFT_EXPR
)
2813 /* Even if vr0 is VARYING or otherwise not usable, we can derive
2814 useful ranges just from the shift count. E.g.
2815 x >> 63 for signed 64-bit x is always [-1, 0]. */
2816 if (vr0
.type
!= VR_RANGE
|| symbolic_range_p (&vr0
))
2818 vr0
.type
= type
= VR_RANGE
;
2819 vr0
.min
= vrp_val_min (expr_type
);
2820 vr0
.max
= vrp_val_max (expr_type
);
2822 extract_range_from_multiplicative_op_1 (vr
, code
, &vr0
, &vr1
);
2825 /* We can map lshifts by constants to MULT_EXPR handling. */
2826 else if (code
== LSHIFT_EXPR
2827 && range_int_cst_singleton_p (&vr1
))
2829 bool saved_flag_wrapv
;
2830 value_range vr1p
= VR_INITIALIZER
;
2831 vr1p
.type
= VR_RANGE
;
2832 vr1p
.min
= (wide_int_to_tree
2834 wi::set_bit_in_zero (tree_to_shwi (vr1
.min
),
2835 TYPE_PRECISION (expr_type
))));
2836 vr1p
.max
= vr1p
.min
;
2837 /* We have to use a wrapping multiply though as signed overflow
2838 on lshifts is implementation defined in C89. */
2839 saved_flag_wrapv
= flag_wrapv
;
2841 extract_range_from_binary_expr_1 (vr
, MULT_EXPR
, expr_type
,
2843 flag_wrapv
= saved_flag_wrapv
;
2846 else if (code
== LSHIFT_EXPR
2847 && range_int_cst_p (&vr0
))
2849 int prec
= TYPE_PRECISION (expr_type
);
2850 int overflow_pos
= prec
;
2852 wide_int low_bound
, high_bound
;
2853 bool uns
= TYPE_UNSIGNED (expr_type
);
2854 bool in_bounds
= false;
2859 bound_shift
= overflow_pos
- tree_to_shwi (vr1
.max
);
2860 /* If bound_shift == HOST_BITS_PER_WIDE_INT, the llshift can
2861 overflow. However, for that to happen, vr1.max needs to be
2862 zero, which means vr1 is a singleton range of zero, which
2863 means it should be handled by the previous LSHIFT_EXPR
2865 wide_int bound
= wi::set_bit_in_zero (bound_shift
, prec
);
2866 wide_int complement
= ~(bound
- 1);
2871 high_bound
= complement
;
2872 if (wi::ltu_p (vr0
.max
, low_bound
))
2874 /* [5, 6] << [1, 2] == [10, 24]. */
2875 /* We're shifting out only zeroes, the value increases
2879 else if (wi::ltu_p (high_bound
, vr0
.min
))
2881 /* [0xffffff00, 0xffffffff] << [1, 2]
2882 == [0xfffffc00, 0xfffffffe]. */
2883 /* We're shifting out only ones, the value decreases
2890 /* [-1, 1] << [1, 2] == [-4, 4]. */
2891 low_bound
= complement
;
2893 if (wi::lts_p (vr0
.max
, high_bound
)
2894 && wi::lts_p (low_bound
, vr0
.min
))
2896 /* For non-negative numbers, we're shifting out only
2897 zeroes, the value increases monotonically.
2898 For negative numbers, we're shifting out only ones, the
2899 value decreases monotomically. */
2906 extract_range_from_multiplicative_op_1 (vr
, code
, &vr0
, &vr1
);
2911 set_value_range_to_varying (vr
);
2914 else if (code
== TRUNC_DIV_EXPR
2915 || code
== FLOOR_DIV_EXPR
2916 || code
== CEIL_DIV_EXPR
2917 || code
== EXACT_DIV_EXPR
2918 || code
== ROUND_DIV_EXPR
)
2920 if (vr0
.type
!= VR_RANGE
|| symbolic_range_p (&vr0
))
2922 /* For division, if op1 has VR_RANGE but op0 does not, something
2923 can be deduced just from that range. Say [min, max] / [4, max]
2924 gives [min / 4, max / 4] range. */
2925 if (vr1
.type
== VR_RANGE
2926 && !symbolic_range_p (&vr1
)
2927 && range_includes_zero_p (vr1
.min
, vr1
.max
) == 0)
2929 vr0
.type
= type
= VR_RANGE
;
2930 vr0
.min
= vrp_val_min (expr_type
);
2931 vr0
.max
= vrp_val_max (expr_type
);
2935 set_value_range_to_varying (vr
);
2940 /* For divisions, if flag_non_call_exceptions is true, we must
2941 not eliminate a division by zero. */
2942 if (cfun
->can_throw_non_call_exceptions
2943 && (vr1
.type
!= VR_RANGE
2944 || range_includes_zero_p (vr1
.min
, vr1
.max
) != 0))
2946 set_value_range_to_varying (vr
);
2950 /* For divisions, if op0 is VR_RANGE, we can deduce a range
2951 even if op1 is VR_VARYING, VR_ANTI_RANGE, symbolic or can
2953 if (vr0
.type
== VR_RANGE
2954 && (vr1
.type
!= VR_RANGE
2955 || range_includes_zero_p (vr1
.min
, vr1
.max
) != 0))
2957 tree zero
= build_int_cst (TREE_TYPE (vr0
.min
), 0);
2962 if (TYPE_UNSIGNED (expr_type
)
2963 || value_range_nonnegative_p (&vr1
))
2965 /* For unsigned division or when divisor is known
2966 to be non-negative, the range has to cover
2967 all numbers from 0 to max for positive max
2968 and all numbers from min to 0 for negative min. */
2969 cmp
= compare_values (vr0
.max
, zero
);
2972 /* When vr0.max < 0, vr1.min != 0 and value
2973 ranges for dividend and divisor are available. */
2974 if (vr1
.type
== VR_RANGE
2975 && !symbolic_range_p (&vr0
)
2976 && !symbolic_range_p (&vr1
)
2977 && !compare_values (vr1
.min
, zero
))
2978 max
= int_const_binop (code
, vr0
.max
, vr1
.min
);
2982 else if (cmp
== 0 || cmp
== 1)
2986 cmp
= compare_values (vr0
.min
, zero
);
2989 /* For unsigned division when value ranges for dividend
2990 and divisor are available. */
2991 if (vr1
.type
== VR_RANGE
2992 && !symbolic_range_p (&vr0
)
2993 && !symbolic_range_p (&vr1
))
2994 min
= int_const_binop (code
, vr0
.min
, vr1
.max
);
2998 else if (cmp
== 0 || cmp
== -1)
3005 /* Otherwise the range is -max .. max or min .. -min
3006 depending on which bound is bigger in absolute value,
3007 as the division can change the sign. */
3008 abs_extent_range (vr
, vr0
.min
, vr0
.max
);
3011 if (type
== VR_VARYING
)
3013 set_value_range_to_varying (vr
);
3019 extract_range_from_multiplicative_op_1 (vr
, code
, &vr0
, &vr1
);
3023 else if (code
== TRUNC_MOD_EXPR
)
3025 if (range_is_null (&vr1
))
3027 set_value_range_to_undefined (vr
);
3030 /* ABS (A % B) < ABS (B) and either
3031 0 <= A % B <= A or A <= A % B <= 0. */
3033 signop sgn
= TYPE_SIGN (expr_type
);
3034 unsigned int prec
= TYPE_PRECISION (expr_type
);
3035 wide_int wmin
, wmax
, tmp
;
3036 wide_int zero
= wi::zero (prec
);
3037 wide_int one
= wi::one (prec
);
3038 if (vr1
.type
== VR_RANGE
&& !symbolic_range_p (&vr1
))
3040 wmax
= wi::sub (vr1
.max
, one
);
3043 tmp
= wi::sub (wi::minus_one (prec
), vr1
.min
);
3044 wmax
= wi::smax (wmax
, tmp
);
3049 wmax
= wi::max_value (prec
, sgn
);
3050 /* X % INT_MIN may be INT_MAX. */
3051 if (sgn
== UNSIGNED
)
3055 if (sgn
== UNSIGNED
)
3060 if (vr0
.type
== VR_RANGE
&& TREE_CODE (vr0
.min
) == INTEGER_CST
)
3063 if (wi::gts_p (tmp
, zero
))
3065 wmin
= wi::smax (wmin
, tmp
);
3069 if (vr0
.type
== VR_RANGE
&& TREE_CODE (vr0
.max
) == INTEGER_CST
)
3072 if (sgn
== SIGNED
&& wi::neg_p (tmp
))
3074 wmax
= wi::min (wmax
, tmp
, sgn
);
3077 min
= wide_int_to_tree (expr_type
, wmin
);
3078 max
= wide_int_to_tree (expr_type
, wmax
);
3080 else if (code
== BIT_AND_EXPR
|| code
== BIT_IOR_EXPR
|| code
== BIT_XOR_EXPR
)
3082 bool int_cst_range0
, int_cst_range1
;
3083 wide_int may_be_nonzero0
, may_be_nonzero1
;
3084 wide_int must_be_nonzero0
, must_be_nonzero1
;
3086 int_cst_range0
= zero_nonzero_bits_from_vr (expr_type
, &vr0
,
3089 int_cst_range1
= zero_nonzero_bits_from_vr (expr_type
, &vr1
,
3094 if (code
== BIT_AND_EXPR
)
3096 min
= wide_int_to_tree (expr_type
,
3097 must_be_nonzero0
& must_be_nonzero1
);
3098 wide_int wmax
= may_be_nonzero0
& may_be_nonzero1
;
3099 /* If both input ranges contain only negative values we can
3100 truncate the result range maximum to the minimum of the
3101 input range maxima. */
3102 if (int_cst_range0
&& int_cst_range1
3103 && tree_int_cst_sgn (vr0
.max
) < 0
3104 && tree_int_cst_sgn (vr1
.max
) < 0)
3106 wmax
= wi::min (wmax
, vr0
.max
, TYPE_SIGN (expr_type
));
3107 wmax
= wi::min (wmax
, vr1
.max
, TYPE_SIGN (expr_type
));
3109 /* If either input range contains only non-negative values
3110 we can truncate the result range maximum to the respective
3111 maximum of the input range. */
3112 if (int_cst_range0
&& tree_int_cst_sgn (vr0
.min
) >= 0)
3113 wmax
= wi::min (wmax
, vr0
.max
, TYPE_SIGN (expr_type
));
3114 if (int_cst_range1
&& tree_int_cst_sgn (vr1
.min
) >= 0)
3115 wmax
= wi::min (wmax
, vr1
.max
, TYPE_SIGN (expr_type
));
3116 max
= wide_int_to_tree (expr_type
, wmax
);
3118 else if (code
== BIT_IOR_EXPR
)
3120 max
= wide_int_to_tree (expr_type
,
3121 may_be_nonzero0
| may_be_nonzero1
);
3122 wide_int wmin
= must_be_nonzero0
| must_be_nonzero1
;
3123 /* If the input ranges contain only positive values we can
3124 truncate the minimum of the result range to the maximum
3125 of the input range minima. */
3126 if (int_cst_range0
&& int_cst_range1
3127 && tree_int_cst_sgn (vr0
.min
) >= 0
3128 && tree_int_cst_sgn (vr1
.min
) >= 0)
3130 wmin
= wi::max (wmin
, vr0
.min
, TYPE_SIGN (expr_type
));
3131 wmin
= wi::max (wmin
, vr1
.min
, TYPE_SIGN (expr_type
));
3133 /* If either input range contains only negative values
3134 we can truncate the minimum of the result range to the
3135 respective minimum range. */
3136 if (int_cst_range0
&& tree_int_cst_sgn (vr0
.max
) < 0)
3137 wmin
= wi::max (wmin
, vr0
.min
, TYPE_SIGN (expr_type
));
3138 if (int_cst_range1
&& tree_int_cst_sgn (vr1
.max
) < 0)
3139 wmin
= wi::max (wmin
, vr1
.min
, TYPE_SIGN (expr_type
));
3140 min
= wide_int_to_tree (expr_type
, wmin
);
3142 else if (code
== BIT_XOR_EXPR
)
3144 wide_int result_zero_bits
= ((must_be_nonzero0
& must_be_nonzero1
)
3145 | ~(may_be_nonzero0
| may_be_nonzero1
));
3146 wide_int result_one_bits
3147 = (must_be_nonzero0
.and_not (may_be_nonzero1
)
3148 | must_be_nonzero1
.and_not (may_be_nonzero0
));
3149 max
= wide_int_to_tree (expr_type
, ~result_zero_bits
);
3150 min
= wide_int_to_tree (expr_type
, result_one_bits
);
3151 /* If the range has all positive or all negative values the
3152 result is better than VARYING. */
3153 if (tree_int_cst_sgn (min
) < 0
3154 || tree_int_cst_sgn (max
) >= 0)
3157 max
= min
= NULL_TREE
;
3163 /* If either MIN or MAX overflowed, then set the resulting range to
3164 VARYING. But we do accept an overflow infinity representation. */
3165 if (min
== NULL_TREE
3166 || (TREE_OVERFLOW_P (min
) && !is_overflow_infinity (min
))
3168 || (TREE_OVERFLOW_P (max
) && !is_overflow_infinity (max
)))
3170 set_value_range_to_varying (vr
);
3176 2) [-INF, +-INF(OVF)]
3177 3) [+-INF(OVF), +INF]
3178 4) [+-INF(OVF), +-INF(OVF)]
3179 We learn nothing when we have INF and INF(OVF) on both sides.
3180 Note that we do accept [-INF, -INF] and [+INF, +INF] without
3182 if ((vrp_val_is_min (min
) || is_overflow_infinity (min
))
3183 && (vrp_val_is_max (max
) || is_overflow_infinity (max
)))
3185 set_value_range_to_varying (vr
);
3189 cmp
= compare_values (min
, max
);
3190 if (cmp
== -2 || cmp
== 1)
3192 /* If the new range has its limits swapped around (MIN > MAX),
3193 then the operation caused one of them to wrap around, mark
3194 the new range VARYING. */
3195 set_value_range_to_varying (vr
);
3198 set_value_range (vr
, type
, min
, max
, NULL
);
3201 /* Extract range information from a binary expression OP0 CODE OP1 based on
3202 the ranges of each of its operands with resulting type EXPR_TYPE.
3203 The resulting range is stored in *VR. */
3206 extract_range_from_binary_expr (value_range
*vr
,
3207 enum tree_code code
,
3208 tree expr_type
, tree op0
, tree op1
)
3210 value_range vr0
= VR_INITIALIZER
;
3211 value_range vr1
= VR_INITIALIZER
;
3213 /* Get value ranges for each operand. For constant operands, create
3214 a new value range with the operand to simplify processing. */
3215 if (TREE_CODE (op0
) == SSA_NAME
)
3216 vr0
= *(get_value_range (op0
));
3217 else if (is_gimple_min_invariant (op0
))
3218 set_value_range_to_value (&vr0
, op0
, NULL
);
3220 set_value_range_to_varying (&vr0
);
3222 if (TREE_CODE (op1
) == SSA_NAME
)
3223 vr1
= *(get_value_range (op1
));
3224 else if (is_gimple_min_invariant (op1
))
3225 set_value_range_to_value (&vr1
, op1
, NULL
);
3227 set_value_range_to_varying (&vr1
);
3229 extract_range_from_binary_expr_1 (vr
, code
, expr_type
, &vr0
, &vr1
);
3231 /* Try harder for PLUS and MINUS if the range of one operand is symbolic
3232 and based on the other operand, for example if it was deduced from a
3233 symbolic comparison. When a bound of the range of the first operand
3234 is invariant, we set the corresponding bound of the new range to INF
3235 in order to avoid recursing on the range of the second operand. */
3236 if (vr
->type
== VR_VARYING
3237 && (code
== PLUS_EXPR
|| code
== MINUS_EXPR
)
3238 && TREE_CODE (op1
) == SSA_NAME
3239 && vr0
.type
== VR_RANGE
3240 && symbolic_range_based_on_p (&vr0
, op1
))
3242 const bool minus_p
= (code
== MINUS_EXPR
);
3243 value_range n_vr1
= VR_INITIALIZER
;
3245 /* Try with VR0 and [-INF, OP1]. */
3246 if (is_gimple_min_invariant (minus_p
? vr0
.max
: vr0
.min
))
3247 set_value_range (&n_vr1
, VR_RANGE
, vrp_val_min (expr_type
), op1
, NULL
);
3249 /* Try with VR0 and [OP1, +INF]. */
3250 else if (is_gimple_min_invariant (minus_p
? vr0
.min
: vr0
.max
))
3251 set_value_range (&n_vr1
, VR_RANGE
, op1
, vrp_val_max (expr_type
), NULL
);
3253 /* Try with VR0 and [OP1, OP1]. */
3255 set_value_range (&n_vr1
, VR_RANGE
, op1
, op1
, NULL
);
3257 extract_range_from_binary_expr_1 (vr
, code
, expr_type
, &vr0
, &n_vr1
);
3260 if (vr
->type
== VR_VARYING
3261 && (code
== PLUS_EXPR
|| code
== MINUS_EXPR
)
3262 && TREE_CODE (op0
) == SSA_NAME
3263 && vr1
.type
== VR_RANGE
3264 && symbolic_range_based_on_p (&vr1
, op0
))
3266 const bool minus_p
= (code
== MINUS_EXPR
);
3267 value_range n_vr0
= VR_INITIALIZER
;
3269 /* Try with [-INF, OP0] and VR1. */
3270 if (is_gimple_min_invariant (minus_p
? vr1
.max
: vr1
.min
))
3271 set_value_range (&n_vr0
, VR_RANGE
, vrp_val_min (expr_type
), op0
, NULL
);
3273 /* Try with [OP0, +INF] and VR1. */
3274 else if (is_gimple_min_invariant (minus_p
? vr1
.min
: vr1
.max
))
3275 set_value_range (&n_vr0
, VR_RANGE
, op0
, vrp_val_max (expr_type
), NULL
);
3277 /* Try with [OP0, OP0] and VR1. */
3279 set_value_range (&n_vr0
, VR_RANGE
, op0
, op0
, NULL
);
3281 extract_range_from_binary_expr_1 (vr
, code
, expr_type
, &n_vr0
, &vr1
);
3285 /* Extract range information from a unary operation CODE based on
3286 the range of its operand *VR0 with type OP0_TYPE with resulting type TYPE.
3287 The resulting range is stored in *VR. */
3290 extract_range_from_unary_expr_1 (value_range
*vr
,
3291 enum tree_code code
, tree type
,
3292 value_range
*vr0_
, tree op0_type
)
3294 value_range vr0
= *vr0_
, vrtem0
= VR_INITIALIZER
, vrtem1
= VR_INITIALIZER
;
3296 /* VRP only operates on integral and pointer types. */
3297 if (!(INTEGRAL_TYPE_P (op0_type
)
3298 || POINTER_TYPE_P (op0_type
))
3299 || !(INTEGRAL_TYPE_P (type
)
3300 || POINTER_TYPE_P (type
)))
3302 set_value_range_to_varying (vr
);
3306 /* If VR0 is UNDEFINED, so is the result. */
3307 if (vr0
.type
== VR_UNDEFINED
)
3309 set_value_range_to_undefined (vr
);
3313 /* Handle operations that we express in terms of others. */
3314 if (code
== PAREN_EXPR
|| code
== OBJ_TYPE_REF
)
3316 /* PAREN_EXPR and OBJ_TYPE_REF are simple copies. */
3317 copy_value_range (vr
, &vr0
);
3320 else if (code
== NEGATE_EXPR
)
3322 /* -X is simply 0 - X, so re-use existing code that also handles
3323 anti-ranges fine. */
3324 value_range zero
= VR_INITIALIZER
;
3325 set_value_range_to_value (&zero
, build_int_cst (type
, 0), NULL
);
3326 extract_range_from_binary_expr_1 (vr
, MINUS_EXPR
, type
, &zero
, &vr0
);
3329 else if (code
== BIT_NOT_EXPR
)
3331 /* ~X is simply -1 - X, so re-use existing code that also handles
3332 anti-ranges fine. */
3333 value_range minusone
= VR_INITIALIZER
;
3334 set_value_range_to_value (&minusone
, build_int_cst (type
, -1), NULL
);
3335 extract_range_from_binary_expr_1 (vr
, MINUS_EXPR
,
3336 type
, &minusone
, &vr0
);
3340 /* Now canonicalize anti-ranges to ranges when they are not symbolic
3341 and express op ~[] as (op []') U (op []''). */
3342 if (vr0
.type
== VR_ANTI_RANGE
3343 && ranges_from_anti_range (&vr0
, &vrtem0
, &vrtem1
))
3345 extract_range_from_unary_expr_1 (vr
, code
, type
, &vrtem0
, op0_type
);
3346 if (vrtem1
.type
!= VR_UNDEFINED
)
3348 value_range vrres
= VR_INITIALIZER
;
3349 extract_range_from_unary_expr_1 (&vrres
, code
, type
,
3351 vrp_meet (vr
, &vrres
);
3356 if (CONVERT_EXPR_CODE_P (code
))
3358 tree inner_type
= op0_type
;
3359 tree outer_type
= type
;
3361 /* If the expression evaluates to a pointer, we are only interested in
3362 determining if it evaluates to NULL [0, 0] or non-NULL (~[0, 0]). */
3363 if (POINTER_TYPE_P (type
))
3365 if (range_is_nonnull (&vr0
))
3366 set_value_range_to_nonnull (vr
, type
);
3367 else if (range_is_null (&vr0
))
3368 set_value_range_to_null (vr
, type
);
3370 set_value_range_to_varying (vr
);
3374 /* If VR0 is varying and we increase the type precision, assume
3375 a full range for the following transformation. */
3376 if (vr0
.type
== VR_VARYING
3377 && INTEGRAL_TYPE_P (inner_type
)
3378 && TYPE_PRECISION (inner_type
) < TYPE_PRECISION (outer_type
))
3380 vr0
.type
= VR_RANGE
;
3381 vr0
.min
= TYPE_MIN_VALUE (inner_type
);
3382 vr0
.max
= TYPE_MAX_VALUE (inner_type
);
3385 /* If VR0 is a constant range or anti-range and the conversion is
3386 not truncating we can convert the min and max values and
3387 canonicalize the resulting range. Otherwise we can do the
3388 conversion if the size of the range is less than what the
3389 precision of the target type can represent and the range is
3390 not an anti-range. */
3391 if ((vr0
.type
== VR_RANGE
3392 || vr0
.type
== VR_ANTI_RANGE
)
3393 && TREE_CODE (vr0
.min
) == INTEGER_CST
3394 && TREE_CODE (vr0
.max
) == INTEGER_CST
3395 && (!is_overflow_infinity (vr0
.min
)
3396 || (vr0
.type
== VR_RANGE
3397 && TYPE_PRECISION (outer_type
) > TYPE_PRECISION (inner_type
)
3398 && needs_overflow_infinity (outer_type
)
3399 && supports_overflow_infinity (outer_type
)))
3400 && (!is_overflow_infinity (vr0
.max
)
3401 || (vr0
.type
== VR_RANGE
3402 && TYPE_PRECISION (outer_type
) > TYPE_PRECISION (inner_type
)
3403 && needs_overflow_infinity (outer_type
)
3404 && supports_overflow_infinity (outer_type
)))
3405 && (TYPE_PRECISION (outer_type
) >= TYPE_PRECISION (inner_type
)
3406 || (vr0
.type
== VR_RANGE
3407 && integer_zerop (int_const_binop (RSHIFT_EXPR
,
3408 int_const_binop (MINUS_EXPR
, vr0
.max
, vr0
.min
),
3409 size_int (TYPE_PRECISION (outer_type
)))))))
3411 tree new_min
, new_max
;
3412 if (is_overflow_infinity (vr0
.min
))
3413 new_min
= negative_overflow_infinity (outer_type
);
3415 new_min
= force_fit_type (outer_type
, wi::to_widest (vr0
.min
),
3417 if (is_overflow_infinity (vr0
.max
))
3418 new_max
= positive_overflow_infinity (outer_type
);
3420 new_max
= force_fit_type (outer_type
, wi::to_widest (vr0
.max
),
3422 set_and_canonicalize_value_range (vr
, vr0
.type
,
3423 new_min
, new_max
, NULL
);
3427 set_value_range_to_varying (vr
);
3430 else if (code
== ABS_EXPR
)
3435 /* Pass through vr0 in the easy cases. */
3436 if (TYPE_UNSIGNED (type
)
3437 || value_range_nonnegative_p (&vr0
))
3439 copy_value_range (vr
, &vr0
);
3443 /* For the remaining varying or symbolic ranges we can't do anything
3445 if (vr0
.type
== VR_VARYING
3446 || symbolic_range_p (&vr0
))
3448 set_value_range_to_varying (vr
);
3452 /* -TYPE_MIN_VALUE = TYPE_MIN_VALUE with flag_wrapv so we can't get a
3454 if (!TYPE_OVERFLOW_UNDEFINED (type
)
3455 && ((vr0
.type
== VR_RANGE
3456 && vrp_val_is_min (vr0
.min
))
3457 || (vr0
.type
== VR_ANTI_RANGE
3458 && !vrp_val_is_min (vr0
.min
))))
3460 set_value_range_to_varying (vr
);
3464 /* ABS_EXPR may flip the range around, if the original range
3465 included negative values. */
3466 if (is_overflow_infinity (vr0
.min
))
3467 min
= positive_overflow_infinity (type
);
3468 else if (!vrp_val_is_min (vr0
.min
))
3469 min
= fold_unary_to_constant (code
, type
, vr0
.min
);
3470 else if (!needs_overflow_infinity (type
))
3471 min
= TYPE_MAX_VALUE (type
);
3472 else if (supports_overflow_infinity (type
))
3473 min
= positive_overflow_infinity (type
);
3476 set_value_range_to_varying (vr
);
3480 if (is_overflow_infinity (vr0
.max
))
3481 max
= positive_overflow_infinity (type
);
3482 else if (!vrp_val_is_min (vr0
.max
))
3483 max
= fold_unary_to_constant (code
, type
, vr0
.max
);
3484 else if (!needs_overflow_infinity (type
))
3485 max
= TYPE_MAX_VALUE (type
);
3486 else if (supports_overflow_infinity (type
)
3487 /* We shouldn't generate [+INF, +INF] as set_value_range
3488 doesn't like this and ICEs. */
3489 && !is_positive_overflow_infinity (min
))
3490 max
= positive_overflow_infinity (type
);
3493 set_value_range_to_varying (vr
);
3497 cmp
= compare_values (min
, max
);
3499 /* If a VR_ANTI_RANGEs contains zero, then we have
3500 ~[-INF, min(MIN, MAX)]. */
3501 if (vr0
.type
== VR_ANTI_RANGE
)
3503 if (range_includes_zero_p (vr0
.min
, vr0
.max
) == 1)
3505 /* Take the lower of the two values. */
3509 /* Create ~[-INF, min (abs(MIN), abs(MAX))]
3510 or ~[-INF + 1, min (abs(MIN), abs(MAX))] when
3511 flag_wrapv is set and the original anti-range doesn't include
3512 TYPE_MIN_VALUE, remember -TYPE_MIN_VALUE = TYPE_MIN_VALUE. */
3513 if (TYPE_OVERFLOW_WRAPS (type
))
3515 tree type_min_value
= TYPE_MIN_VALUE (type
);
3517 min
= (vr0
.min
!= type_min_value
3518 ? int_const_binop (PLUS_EXPR
, type_min_value
,
3519 build_int_cst (TREE_TYPE (type_min_value
), 1))
3524 if (overflow_infinity_range_p (&vr0
))
3525 min
= negative_overflow_infinity (type
);
3527 min
= TYPE_MIN_VALUE (type
);
3532 /* All else has failed, so create the range [0, INF], even for
3533 flag_wrapv since TYPE_MIN_VALUE is in the original
3535 vr0
.type
= VR_RANGE
;
3536 min
= build_int_cst (type
, 0);
3537 if (needs_overflow_infinity (type
))
3539 if (supports_overflow_infinity (type
))
3540 max
= positive_overflow_infinity (type
);
3543 set_value_range_to_varying (vr
);
3548 max
= TYPE_MAX_VALUE (type
);
3552 /* If the range contains zero then we know that the minimum value in the
3553 range will be zero. */
3554 else if (range_includes_zero_p (vr0
.min
, vr0
.max
) == 1)
3558 min
= build_int_cst (type
, 0);
3562 /* If the range was reversed, swap MIN and MAX. */
3564 std::swap (min
, max
);
3567 cmp
= compare_values (min
, max
);
3568 if (cmp
== -2 || cmp
== 1)
3570 /* If the new range has its limits swapped around (MIN > MAX),
3571 then the operation caused one of them to wrap around, mark
3572 the new range VARYING. */
3573 set_value_range_to_varying (vr
);
3576 set_value_range (vr
, vr0
.type
, min
, max
, NULL
);
3580 /* For unhandled operations fall back to varying. */
3581 set_value_range_to_varying (vr
);
3586 /* Extract range information from a unary expression CODE OP0 based on
3587 the range of its operand with resulting type TYPE.
3588 The resulting range is stored in *VR. */
3591 extract_range_from_unary_expr (value_range
*vr
, enum tree_code code
,
3592 tree type
, tree op0
)
3594 value_range vr0
= VR_INITIALIZER
;
3596 /* Get value ranges for the operand. For constant operands, create
3597 a new value range with the operand to simplify processing. */
3598 if (TREE_CODE (op0
) == SSA_NAME
)
3599 vr0
= *(get_value_range (op0
));
3600 else if (is_gimple_min_invariant (op0
))
3601 set_value_range_to_value (&vr0
, op0
, NULL
);
3603 set_value_range_to_varying (&vr0
);
3605 extract_range_from_unary_expr_1 (vr
, code
, type
, &vr0
, TREE_TYPE (op0
));
3609 /* Extract range information from a conditional expression STMT based on
3610 the ranges of each of its operands and the expression code. */
3613 extract_range_from_cond_expr (value_range
*vr
, gassign
*stmt
)
3616 value_range vr0
= VR_INITIALIZER
;
3617 value_range vr1
= VR_INITIALIZER
;
3619 /* Get value ranges for each operand. For constant operands, create
3620 a new value range with the operand to simplify processing. */
3621 op0
= gimple_assign_rhs2 (stmt
);
3622 if (TREE_CODE (op0
) == SSA_NAME
)
3623 vr0
= *(get_value_range (op0
));
3624 else if (is_gimple_min_invariant (op0
))
3625 set_value_range_to_value (&vr0
, op0
, NULL
);
3627 set_value_range_to_varying (&vr0
);
3629 op1
= gimple_assign_rhs3 (stmt
);
3630 if (TREE_CODE (op1
) == SSA_NAME
)
3631 vr1
= *(get_value_range (op1
));
3632 else if (is_gimple_min_invariant (op1
))
3633 set_value_range_to_value (&vr1
, op1
, NULL
);
3635 set_value_range_to_varying (&vr1
);
3637 /* The resulting value range is the union of the operand ranges */
3638 copy_value_range (vr
, &vr0
);
3639 vrp_meet (vr
, &vr1
);
3643 /* Extract range information from a comparison expression EXPR based
3644 on the range of its operand and the expression code. */
3647 extract_range_from_comparison (value_range
*vr
, enum tree_code code
,
3648 tree type
, tree op0
, tree op1
)
3653 val
= vrp_evaluate_conditional_warnv_with_ops (code
, op0
, op1
, false, &sop
,
3656 /* A disadvantage of using a special infinity as an overflow
3657 representation is that we lose the ability to record overflow
3658 when we don't have an infinity. So we have to ignore a result
3659 which relies on overflow. */
3661 if (val
&& !is_overflow_infinity (val
) && !sop
)
3663 /* Since this expression was found on the RHS of an assignment,
3664 its type may be different from _Bool. Convert VAL to EXPR's
3666 val
= fold_convert (type
, val
);
3667 if (is_gimple_min_invariant (val
))
3668 set_value_range_to_value (vr
, val
, vr
->equiv
);
3670 set_value_range (vr
, VR_RANGE
, val
, val
, vr
->equiv
);
3673 /* The result of a comparison is always true or false. */
3674 set_value_range_to_truthvalue (vr
, type
);
3677 /* Helper function for simplify_internal_call_using_ranges and
3678 extract_range_basic. Return true if OP0 SUBCODE OP1 for
3679 SUBCODE {PLUS,MINUS,MULT}_EXPR is known to never overflow or
3680 always overflow. Set *OVF to true if it is known to always
3684 check_for_binary_op_overflow (enum tree_code subcode
, tree type
,
3685 tree op0
, tree op1
, bool *ovf
)
3687 value_range vr0
= VR_INITIALIZER
;
3688 value_range vr1
= VR_INITIALIZER
;
3689 if (TREE_CODE (op0
) == SSA_NAME
)
3690 vr0
= *get_value_range (op0
);
3691 else if (TREE_CODE (op0
) == INTEGER_CST
)
3692 set_value_range_to_value (&vr0
, op0
, NULL
);
3694 set_value_range_to_varying (&vr0
);
3696 if (TREE_CODE (op1
) == SSA_NAME
)
3697 vr1
= *get_value_range (op1
);
3698 else if (TREE_CODE (op1
) == INTEGER_CST
)
3699 set_value_range_to_value (&vr1
, op1
, NULL
);
3701 set_value_range_to_varying (&vr1
);
3703 if (!range_int_cst_p (&vr0
)
3704 || TREE_OVERFLOW (vr0
.min
)
3705 || TREE_OVERFLOW (vr0
.max
))
3707 vr0
.min
= vrp_val_min (TREE_TYPE (op0
));
3708 vr0
.max
= vrp_val_max (TREE_TYPE (op0
));
3710 if (!range_int_cst_p (&vr1
)
3711 || TREE_OVERFLOW (vr1
.min
)
3712 || TREE_OVERFLOW (vr1
.max
))
3714 vr1
.min
= vrp_val_min (TREE_TYPE (op1
));
3715 vr1
.max
= vrp_val_max (TREE_TYPE (op1
));
3717 *ovf
= arith_overflowed_p (subcode
, type
, vr0
.min
,
3718 subcode
== MINUS_EXPR
? vr1
.max
: vr1
.min
);
3719 if (arith_overflowed_p (subcode
, type
, vr0
.max
,
3720 subcode
== MINUS_EXPR
? vr1
.min
: vr1
.max
) != *ovf
)
3722 if (subcode
== MULT_EXPR
)
3724 if (arith_overflowed_p (subcode
, type
, vr0
.min
, vr1
.max
) != *ovf
3725 || arith_overflowed_p (subcode
, type
, vr0
.max
, vr1
.min
) != *ovf
)
3730 /* So far we found that there is an overflow on the boundaries.
3731 That doesn't prove that there is an overflow even for all values
3732 in between the boundaries. For that compute widest_int range
3733 of the result and see if it doesn't overlap the range of
3735 widest_int wmin
, wmax
;
3738 w
[0] = wi::to_widest (vr0
.min
);
3739 w
[1] = wi::to_widest (vr0
.max
);
3740 w
[2] = wi::to_widest (vr1
.min
);
3741 w
[3] = wi::to_widest (vr1
.max
);
3742 for (i
= 0; i
< 4; i
++)
3748 wt
= wi::add (w
[i
& 1], w
[2 + (i
& 2) / 2]);
3751 wt
= wi::sub (w
[i
& 1], w
[2 + (i
& 2) / 2]);
3754 wt
= wi::mul (w
[i
& 1], w
[2 + (i
& 2) / 2]);
3766 wmin
= wi::smin (wmin
, wt
);
3767 wmax
= wi::smax (wmax
, wt
);
3770 /* The result of op0 CODE op1 is known to be in range
3772 widest_int wtmin
= wi::to_widest (vrp_val_min (type
));
3773 widest_int wtmax
= wi::to_widest (vrp_val_max (type
));
3774 /* If all values in [wmin, wmax] are smaller than
3775 [wtmin, wtmax] or all are larger than [wtmin, wtmax],
3776 the arithmetic operation will always overflow. */
3777 if (wi::lts_p (wmax
, wtmin
) || wi::gts_p (wmin
, wtmax
))
3784 /* Try to derive a nonnegative or nonzero range out of STMT relying
3785 primarily on generic routines in fold in conjunction with range data.
3786 Store the result in *VR */
3789 extract_range_basic (value_range
*vr
, gimple
*stmt
)
3792 tree type
= gimple_expr_type (stmt
);
3794 if (gimple_call_builtin_p (stmt
, BUILT_IN_NORMAL
))
3796 tree fndecl
= gimple_call_fndecl (stmt
), arg
;
3797 int mini
, maxi
, zerov
= 0, prec
;
3799 switch (DECL_FUNCTION_CODE (fndecl
))
3801 case BUILT_IN_CONSTANT_P
:
3802 /* If the call is __builtin_constant_p and the argument is a
3803 function parameter resolve it to false. This avoids bogus
3804 array bound warnings.
3805 ??? We could do this as early as inlining is finished. */
3806 arg
= gimple_call_arg (stmt
, 0);
3807 if (TREE_CODE (arg
) == SSA_NAME
3808 && SSA_NAME_IS_DEFAULT_DEF (arg
)
3809 && TREE_CODE (SSA_NAME_VAR (arg
)) == PARM_DECL
)
3811 set_value_range_to_null (vr
, type
);
3815 /* Both __builtin_ffs* and __builtin_popcount return
3817 CASE_INT_FN (BUILT_IN_FFS
):
3818 CASE_INT_FN (BUILT_IN_POPCOUNT
):
3819 arg
= gimple_call_arg (stmt
, 0);
3820 prec
= TYPE_PRECISION (TREE_TYPE (arg
));
3823 if (TREE_CODE (arg
) == SSA_NAME
)
3825 value_range
*vr0
= get_value_range (arg
);
3826 /* If arg is non-zero, then ffs or popcount
3828 if (((vr0
->type
== VR_RANGE
3829 && range_includes_zero_p (vr0
->min
, vr0
->max
) == 0)
3830 || (vr0
->type
== VR_ANTI_RANGE
3831 && range_includes_zero_p (vr0
->min
, vr0
->max
) == 1))
3832 && !is_overflow_infinity (vr0
->min
)
3833 && !is_overflow_infinity (vr0
->max
))
3835 /* If some high bits are known to be zero,
3836 we can decrease the maximum. */
3837 if (vr0
->type
== VR_RANGE
3838 && TREE_CODE (vr0
->max
) == INTEGER_CST
3839 && !operand_less_p (vr0
->min
,
3840 build_zero_cst (TREE_TYPE (vr0
->min
)))
3841 && !is_overflow_infinity (vr0
->max
))
3842 maxi
= tree_floor_log2 (vr0
->max
) + 1;
3845 /* __builtin_parity* returns [0, 1]. */
3846 CASE_INT_FN (BUILT_IN_PARITY
):
3850 /* __builtin_c[lt]z* return [0, prec-1], except for
3851 when the argument is 0, but that is undefined behavior.
3852 On many targets where the CLZ RTL or optab value is defined
3853 for 0 the value is prec, so include that in the range
3855 CASE_INT_FN (BUILT_IN_CLZ
):
3856 arg
= gimple_call_arg (stmt
, 0);
3857 prec
= TYPE_PRECISION (TREE_TYPE (arg
));
3860 if (optab_handler (clz_optab
, TYPE_MODE (TREE_TYPE (arg
)))
3862 && CLZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (TREE_TYPE (arg
)),
3864 /* Handle only the single common value. */
3866 /* Magic value to give up, unless vr0 proves
3869 if (TREE_CODE (arg
) == SSA_NAME
)
3871 value_range
*vr0
= get_value_range (arg
);
3872 /* From clz of VR_RANGE minimum we can compute
3874 if (vr0
->type
== VR_RANGE
3875 && TREE_CODE (vr0
->min
) == INTEGER_CST
3876 && !is_overflow_infinity (vr0
->min
))
3878 maxi
= prec
- 1 - tree_floor_log2 (vr0
->min
);
3882 else if (vr0
->type
== VR_ANTI_RANGE
3883 && integer_zerop (vr0
->min
)
3884 && !is_overflow_infinity (vr0
->min
))
3891 /* From clz of VR_RANGE maximum we can compute
3893 if (vr0
->type
== VR_RANGE
3894 && TREE_CODE (vr0
->max
) == INTEGER_CST
3895 && !is_overflow_infinity (vr0
->max
))
3897 mini
= prec
- 1 - tree_floor_log2 (vr0
->max
);
3905 /* __builtin_ctz* return [0, prec-1], except for
3906 when the argument is 0, but that is undefined behavior.
3907 If there is a ctz optab for this mode and
3908 CTZ_DEFINED_VALUE_AT_ZERO, include that in the range,
3909 otherwise just assume 0 won't be seen. */
3910 CASE_INT_FN (BUILT_IN_CTZ
):
3911 arg
= gimple_call_arg (stmt
, 0);
3912 prec
= TYPE_PRECISION (TREE_TYPE (arg
));
3915 if (optab_handler (ctz_optab
, TYPE_MODE (TREE_TYPE (arg
)))
3917 && CTZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (TREE_TYPE (arg
)),
3920 /* Handle only the two common values. */
3923 else if (zerov
== prec
)
3926 /* Magic value to give up, unless vr0 proves
3930 if (TREE_CODE (arg
) == SSA_NAME
)
3932 value_range
*vr0
= get_value_range (arg
);
3933 /* If arg is non-zero, then use [0, prec - 1]. */
3934 if (((vr0
->type
== VR_RANGE
3935 && integer_nonzerop (vr0
->min
))
3936 || (vr0
->type
== VR_ANTI_RANGE
3937 && integer_zerop (vr0
->min
)))
3938 && !is_overflow_infinity (vr0
->min
))
3943 /* If some high bits are known to be zero,
3944 we can decrease the result maximum. */
3945 if (vr0
->type
== VR_RANGE
3946 && TREE_CODE (vr0
->max
) == INTEGER_CST
3947 && !is_overflow_infinity (vr0
->max
))
3949 maxi
= tree_floor_log2 (vr0
->max
);
3950 /* For vr0 [0, 0] give up. */
3958 /* __builtin_clrsb* returns [0, prec-1]. */
3959 CASE_INT_FN (BUILT_IN_CLRSB
):
3960 arg
= gimple_call_arg (stmt
, 0);
3961 prec
= TYPE_PRECISION (TREE_TYPE (arg
));
3966 set_value_range (vr
, VR_RANGE
, build_int_cst (type
, mini
),
3967 build_int_cst (type
, maxi
), NULL
);
3973 else if (is_gimple_call (stmt
) && gimple_call_internal_p (stmt
))
3975 enum tree_code subcode
= ERROR_MARK
;
3976 unsigned ifn_code
= gimple_call_internal_fn (stmt
);
3980 case IFN_UBSAN_CHECK_ADD
:
3981 subcode
= PLUS_EXPR
;
3983 case IFN_UBSAN_CHECK_SUB
:
3984 subcode
= MINUS_EXPR
;
3986 case IFN_UBSAN_CHECK_MUL
:
3987 subcode
= MULT_EXPR
;
3989 case IFN_GOACC_DIM_SIZE
:
3990 case IFN_GOACC_DIM_POS
:
3991 /* Optimizing these two internal functions helps the loop
3992 optimizer eliminate outer comparisons. Size is [1,N]
3993 and pos is [0,N-1]. */
3995 bool is_pos
= ifn_code
== IFN_GOACC_DIM_POS
;
3996 int axis
= get_oacc_ifn_dim_arg (stmt
);
3997 int size
= get_oacc_fn_dim_size (current_function_decl
, axis
);
4000 /* If it's dynamic, the backend might know a hardware
4002 size
= targetm
.goacc
.dim_limit (axis
);
4004 tree type
= TREE_TYPE (gimple_call_lhs (stmt
));
4005 set_value_range (vr
, VR_RANGE
,
4006 build_int_cst (type
, is_pos
? 0 : 1),
4007 size
? build_int_cst (type
, size
- is_pos
)
4008 : vrp_val_max (type
), NULL
);
4014 if (subcode
!= ERROR_MARK
)
4016 bool saved_flag_wrapv
= flag_wrapv
;
4017 /* Pretend the arithmetics is wrapping. If there is
4018 any overflow, we'll complain, but will actually do
4019 wrapping operation. */
4021 extract_range_from_binary_expr (vr
, subcode
, type
,
4022 gimple_call_arg (stmt
, 0),
4023 gimple_call_arg (stmt
, 1));
4024 flag_wrapv
= saved_flag_wrapv
;
4026 /* If for both arguments vrp_valueize returned non-NULL,
4027 this should have been already folded and if not, it
4028 wasn't folded because of overflow. Avoid removing the
4029 UBSAN_CHECK_* calls in that case. */
4030 if (vr
->type
== VR_RANGE
4031 && (vr
->min
== vr
->max
4032 || operand_equal_p (vr
->min
, vr
->max
, 0)))
4033 set_value_range_to_varying (vr
);
4037 /* Handle extraction of the two results (result of arithmetics and
4038 a flag whether arithmetics overflowed) from {ADD,SUB,MUL}_OVERFLOW
4039 internal function. */
4040 else if (is_gimple_assign (stmt
)
4041 && (gimple_assign_rhs_code (stmt
) == REALPART_EXPR
4042 || gimple_assign_rhs_code (stmt
) == IMAGPART_EXPR
)
4043 && INTEGRAL_TYPE_P (type
))
4045 enum tree_code code
= gimple_assign_rhs_code (stmt
);
4046 tree op
= gimple_assign_rhs1 (stmt
);
4047 if (TREE_CODE (op
) == code
&& TREE_CODE (TREE_OPERAND (op
, 0)) == SSA_NAME
)
4049 gimple
*g
= SSA_NAME_DEF_STMT (TREE_OPERAND (op
, 0));
4050 if (is_gimple_call (g
) && gimple_call_internal_p (g
))
4052 enum tree_code subcode
= ERROR_MARK
;
4053 switch (gimple_call_internal_fn (g
))
4055 case IFN_ADD_OVERFLOW
:
4056 subcode
= PLUS_EXPR
;
4058 case IFN_SUB_OVERFLOW
:
4059 subcode
= MINUS_EXPR
;
4061 case IFN_MUL_OVERFLOW
:
4062 subcode
= MULT_EXPR
;
4067 if (subcode
!= ERROR_MARK
)
4069 tree op0
= gimple_call_arg (g
, 0);
4070 tree op1
= gimple_call_arg (g
, 1);
4071 if (code
== IMAGPART_EXPR
)
4074 if (check_for_binary_op_overflow (subcode
, type
,
4076 set_value_range_to_value (vr
,
4077 build_int_cst (type
, ovf
),
4080 set_value_range (vr
, VR_RANGE
, build_int_cst (type
, 0),
4081 build_int_cst (type
, 1), NULL
);
4083 else if (types_compatible_p (type
, TREE_TYPE (op0
))
4084 && types_compatible_p (type
, TREE_TYPE (op1
)))
4086 bool saved_flag_wrapv
= flag_wrapv
;
4087 /* Pretend the arithmetics is wrapping. If there is
4088 any overflow, IMAGPART_EXPR will be set. */
4090 extract_range_from_binary_expr (vr
, subcode
, type
,
4092 flag_wrapv
= saved_flag_wrapv
;
4096 value_range vr0
= VR_INITIALIZER
;
4097 value_range vr1
= VR_INITIALIZER
;
4098 bool saved_flag_wrapv
= flag_wrapv
;
4099 /* Pretend the arithmetics is wrapping. If there is
4100 any overflow, IMAGPART_EXPR will be set. */
4102 extract_range_from_unary_expr (&vr0
, NOP_EXPR
,
4104 extract_range_from_unary_expr (&vr1
, NOP_EXPR
,
4106 extract_range_from_binary_expr_1 (vr
, subcode
, type
,
4108 flag_wrapv
= saved_flag_wrapv
;
4115 if (INTEGRAL_TYPE_P (type
)
4116 && gimple_stmt_nonnegative_warnv_p (stmt
, &sop
))
4117 set_value_range_to_nonnegative (vr
, type
,
4118 sop
|| stmt_overflow_infinity (stmt
));
4119 else if (vrp_stmt_computes_nonzero (stmt
, &sop
)
4121 set_value_range_to_nonnull (vr
, type
);
4123 set_value_range_to_varying (vr
);
4127 /* Try to compute a useful range out of assignment STMT and store it
4131 extract_range_from_assignment (value_range
*vr
, gassign
*stmt
)
4133 enum tree_code code
= gimple_assign_rhs_code (stmt
);
4135 if (code
== ASSERT_EXPR
)
4136 extract_range_from_assert (vr
, gimple_assign_rhs1 (stmt
));
4137 else if (code
== SSA_NAME
)
4138 extract_range_from_ssa_name (vr
, gimple_assign_rhs1 (stmt
));
4139 else if (TREE_CODE_CLASS (code
) == tcc_binary
)
4140 extract_range_from_binary_expr (vr
, gimple_assign_rhs_code (stmt
),
4141 gimple_expr_type (stmt
),
4142 gimple_assign_rhs1 (stmt
),
4143 gimple_assign_rhs2 (stmt
));
4144 else if (TREE_CODE_CLASS (code
) == tcc_unary
)
4145 extract_range_from_unary_expr (vr
, gimple_assign_rhs_code (stmt
),
4146 gimple_expr_type (stmt
),
4147 gimple_assign_rhs1 (stmt
));
4148 else if (code
== COND_EXPR
)
4149 extract_range_from_cond_expr (vr
, stmt
);
4150 else if (TREE_CODE_CLASS (code
) == tcc_comparison
)
4151 extract_range_from_comparison (vr
, gimple_assign_rhs_code (stmt
),
4152 gimple_expr_type (stmt
),
4153 gimple_assign_rhs1 (stmt
),
4154 gimple_assign_rhs2 (stmt
));
4155 else if (get_gimple_rhs_class (code
) == GIMPLE_SINGLE_RHS
4156 && is_gimple_min_invariant (gimple_assign_rhs1 (stmt
)))
4157 set_value_range_to_value (vr
, gimple_assign_rhs1 (stmt
), NULL
);
4159 set_value_range_to_varying (vr
);
4161 if (vr
->type
== VR_VARYING
)
4162 extract_range_basic (vr
, stmt
);
4165 /* Given a range VR, a LOOP and a variable VAR, determine whether it
4166 would be profitable to adjust VR using scalar evolution information
4167 for VAR. If so, update VR with the new limits. */
4170 adjust_range_with_scev (value_range
*vr
, struct loop
*loop
,
4171 gimple
*stmt
, tree var
)
4173 tree init
, step
, chrec
, tmin
, tmax
, min
, max
, type
, tem
;
4174 enum ev_direction dir
;
4176 /* TODO. Don't adjust anti-ranges. An anti-range may provide
4177 better opportunities than a regular range, but I'm not sure. */
4178 if (vr
->type
== VR_ANTI_RANGE
)
4181 chrec
= instantiate_parameters (loop
, analyze_scalar_evolution (loop
, var
));
4183 /* Like in PR19590, scev can return a constant function. */
4184 if (is_gimple_min_invariant (chrec
))
4186 set_value_range_to_value (vr
, chrec
, vr
->equiv
);
4190 if (TREE_CODE (chrec
) != POLYNOMIAL_CHREC
)
4193 init
= initial_condition_in_loop_num (chrec
, loop
->num
);
4194 tem
= op_with_constant_singleton_value_range (init
);
4197 step
= evolution_part_in_loop_num (chrec
, loop
->num
);
4198 tem
= op_with_constant_singleton_value_range (step
);
4202 /* If STEP is symbolic, we can't know whether INIT will be the
4203 minimum or maximum value in the range. Also, unless INIT is
4204 a simple expression, compare_values and possibly other functions
4205 in tree-vrp won't be able to handle it. */
4206 if (step
== NULL_TREE
4207 || !is_gimple_min_invariant (step
)
4208 || !valid_value_p (init
))
4211 dir
= scev_direction (chrec
);
4212 if (/* Do not adjust ranges if we do not know whether the iv increases
4213 or decreases, ... */
4214 dir
== EV_DIR_UNKNOWN
4215 /* ... or if it may wrap. */
4216 || scev_probably_wraps_p (init
, step
, stmt
, get_chrec_loop (chrec
),
4220 /* We use TYPE_MIN_VALUE and TYPE_MAX_VALUE here instead of
4221 negative_overflow_infinity and positive_overflow_infinity,
4222 because we have concluded that the loop probably does not
4225 type
= TREE_TYPE (var
);
4226 if (POINTER_TYPE_P (type
) || !TYPE_MIN_VALUE (type
))
4227 tmin
= lower_bound_in_type (type
, type
);
4229 tmin
= TYPE_MIN_VALUE (type
);
4230 if (POINTER_TYPE_P (type
) || !TYPE_MAX_VALUE (type
))
4231 tmax
= upper_bound_in_type (type
, type
);
4233 tmax
= TYPE_MAX_VALUE (type
);
4235 /* Try to use estimated number of iterations for the loop to constrain the
4236 final value in the evolution. */
4237 if (TREE_CODE (step
) == INTEGER_CST
4238 && is_gimple_val (init
)
4239 && (TREE_CODE (init
) != SSA_NAME
4240 || get_value_range (init
)->type
== VR_RANGE
))
4244 /* We are only entering here for loop header PHI nodes, so using
4245 the number of latch executions is the correct thing to use. */
4246 if (max_loop_iterations (loop
, &nit
))
4248 value_range maxvr
= VR_INITIALIZER
;
4249 signop sgn
= TYPE_SIGN (TREE_TYPE (step
));
4252 widest_int wtmp
= wi::mul (wi::to_widest (step
), nit
, sgn
,
4254 /* If the multiplication overflowed we can't do a meaningful
4255 adjustment. Likewise if the result doesn't fit in the type
4256 of the induction variable. For a signed type we have to
4257 check whether the result has the expected signedness which
4258 is that of the step as number of iterations is unsigned. */
4260 && wi::fits_to_tree_p (wtmp
, TREE_TYPE (init
))
4262 || wi::gts_p (wtmp
, 0) == wi::gts_p (step
, 0)))
4264 tem
= wide_int_to_tree (TREE_TYPE (init
), wtmp
);
4265 extract_range_from_binary_expr (&maxvr
, PLUS_EXPR
,
4266 TREE_TYPE (init
), init
, tem
);
4267 /* Likewise if the addition did. */
4268 if (maxvr
.type
== VR_RANGE
)
4277 if (vr
->type
== VR_VARYING
|| vr
->type
== VR_UNDEFINED
)
4282 /* For VARYING or UNDEFINED ranges, just about anything we get
4283 from scalar evolutions should be better. */
4285 if (dir
== EV_DIR_DECREASES
)
4290 else if (vr
->type
== VR_RANGE
)
4295 if (dir
== EV_DIR_DECREASES
)
4297 /* INIT is the maximum value. If INIT is lower than VR->MAX
4298 but no smaller than VR->MIN, set VR->MAX to INIT. */
4299 if (compare_values (init
, max
) == -1)
4302 /* According to the loop information, the variable does not
4303 overflow. If we think it does, probably because of an
4304 overflow due to arithmetic on a different INF value,
4306 if (is_negative_overflow_infinity (min
)
4307 || compare_values (min
, tmin
) == -1)
4313 /* If INIT is bigger than VR->MIN, set VR->MIN to INIT. */
4314 if (compare_values (init
, min
) == 1)
4317 if (is_positive_overflow_infinity (max
)
4318 || compare_values (tmax
, max
) == -1)
4325 /* If we just created an invalid range with the minimum
4326 greater than the maximum, we fail conservatively.
4327 This should happen only in unreachable
4328 parts of code, or for invalid programs. */
4329 if (compare_values (min
, max
) == 1
4330 || (is_negative_overflow_infinity (min
)
4331 && is_positive_overflow_infinity (max
)))
4334 set_value_range (vr
, VR_RANGE
, min
, max
, vr
->equiv
);
4338 /* Given two numeric value ranges VR0, VR1 and a comparison code COMP:
4340 - Return BOOLEAN_TRUE_NODE if VR0 COMP VR1 always returns true for
4341 all the values in the ranges.
4343 - Return BOOLEAN_FALSE_NODE if the comparison always returns false.
4345 - Return NULL_TREE if it is not always possible to determine the
4346 value of the comparison.
4348 Also set *STRICT_OVERFLOW_P to indicate whether a range with an
4349 overflow infinity was used in the test. */
4353 compare_ranges (enum tree_code comp
, value_range
*vr0
, value_range
*vr1
,
4354 bool *strict_overflow_p
)
4356 /* VARYING or UNDEFINED ranges cannot be compared. */
4357 if (vr0
->type
== VR_VARYING
4358 || vr0
->type
== VR_UNDEFINED
4359 || vr1
->type
== VR_VARYING
4360 || vr1
->type
== VR_UNDEFINED
)
4363 /* Anti-ranges need to be handled separately. */
4364 if (vr0
->type
== VR_ANTI_RANGE
|| vr1
->type
== VR_ANTI_RANGE
)
4366 /* If both are anti-ranges, then we cannot compute any
4368 if (vr0
->type
== VR_ANTI_RANGE
&& vr1
->type
== VR_ANTI_RANGE
)
4371 /* These comparisons are never statically computable. */
4378 /* Equality can be computed only between a range and an
4379 anti-range. ~[VAL1, VAL2] == [VAL1, VAL2] is always false. */
4380 if (vr0
->type
== VR_RANGE
)
4382 /* To simplify processing, make VR0 the anti-range. */
4383 value_range
*tmp
= vr0
;
4388 gcc_assert (comp
== NE_EXPR
|| comp
== EQ_EXPR
);
4390 if (compare_values_warnv (vr0
->min
, vr1
->min
, strict_overflow_p
) == 0
4391 && compare_values_warnv (vr0
->max
, vr1
->max
, strict_overflow_p
) == 0)
4392 return (comp
== NE_EXPR
) ? boolean_true_node
: boolean_false_node
;
4397 if (!usable_range_p (vr0
, strict_overflow_p
)
4398 || !usable_range_p (vr1
, strict_overflow_p
))
4401 /* Simplify processing. If COMP is GT_EXPR or GE_EXPR, switch the
4402 operands around and change the comparison code. */
4403 if (comp
== GT_EXPR
|| comp
== GE_EXPR
)
4405 comp
= (comp
== GT_EXPR
) ? LT_EXPR
: LE_EXPR
;
4406 std::swap (vr0
, vr1
);
4409 if (comp
== EQ_EXPR
)
4411 /* Equality may only be computed if both ranges represent
4412 exactly one value. */
4413 if (compare_values_warnv (vr0
->min
, vr0
->max
, strict_overflow_p
) == 0
4414 && compare_values_warnv (vr1
->min
, vr1
->max
, strict_overflow_p
) == 0)
4416 int cmp_min
= compare_values_warnv (vr0
->min
, vr1
->min
,
4418 int cmp_max
= compare_values_warnv (vr0
->max
, vr1
->max
,
4420 if (cmp_min
== 0 && cmp_max
== 0)
4421 return boolean_true_node
;
4422 else if (cmp_min
!= -2 && cmp_max
!= -2)
4423 return boolean_false_node
;
4425 /* If [V0_MIN, V1_MAX] < [V1_MIN, V1_MAX] then V0 != V1. */
4426 else if (compare_values_warnv (vr0
->min
, vr1
->max
,
4427 strict_overflow_p
) == 1
4428 || compare_values_warnv (vr1
->min
, vr0
->max
,
4429 strict_overflow_p
) == 1)
4430 return boolean_false_node
;
4434 else if (comp
== NE_EXPR
)
4438 /* If VR0 is completely to the left or completely to the right
4439 of VR1, they are always different. Notice that we need to
4440 make sure that both comparisons yield similar results to
4441 avoid comparing values that cannot be compared at
4443 cmp1
= compare_values_warnv (vr0
->max
, vr1
->min
, strict_overflow_p
);
4444 cmp2
= compare_values_warnv (vr0
->min
, vr1
->max
, strict_overflow_p
);
4445 if ((cmp1
== -1 && cmp2
== -1) || (cmp1
== 1 && cmp2
== 1))
4446 return boolean_true_node
;
4448 /* If VR0 and VR1 represent a single value and are identical,
4450 else if (compare_values_warnv (vr0
->min
, vr0
->max
,
4451 strict_overflow_p
) == 0
4452 && compare_values_warnv (vr1
->min
, vr1
->max
,
4453 strict_overflow_p
) == 0
4454 && compare_values_warnv (vr0
->min
, vr1
->min
,
4455 strict_overflow_p
) == 0
4456 && compare_values_warnv (vr0
->max
, vr1
->max
,
4457 strict_overflow_p
) == 0)
4458 return boolean_false_node
;
4460 /* Otherwise, they may or may not be different. */
4464 else if (comp
== LT_EXPR
|| comp
== LE_EXPR
)
4468 /* If VR0 is to the left of VR1, return true. */
4469 tst
= compare_values_warnv (vr0
->max
, vr1
->min
, strict_overflow_p
);
4470 if ((comp
== LT_EXPR
&& tst
== -1)
4471 || (comp
== LE_EXPR
&& (tst
== -1 || tst
== 0)))
4473 if (overflow_infinity_range_p (vr0
)
4474 || overflow_infinity_range_p (vr1
))
4475 *strict_overflow_p
= true;
4476 return boolean_true_node
;
4479 /* If VR0 is to the right of VR1, return false. */
4480 tst
= compare_values_warnv (vr0
->min
, vr1
->max
, strict_overflow_p
);
4481 if ((comp
== LT_EXPR
&& (tst
== 0 || tst
== 1))
4482 || (comp
== LE_EXPR
&& tst
== 1))
4484 if (overflow_infinity_range_p (vr0
)
4485 || overflow_infinity_range_p (vr1
))
4486 *strict_overflow_p
= true;
4487 return boolean_false_node
;
4490 /* Otherwise, we don't know. */
4498 /* Given a value range VR, a value VAL and a comparison code COMP, return
4499 BOOLEAN_TRUE_NODE if VR COMP VAL always returns true for all the
4500 values in VR. Return BOOLEAN_FALSE_NODE if the comparison
4501 always returns false. Return NULL_TREE if it is not always
4502 possible to determine the value of the comparison. Also set
4503 *STRICT_OVERFLOW_P to indicate whether a range with an overflow
4504 infinity was used in the test. */
4507 compare_range_with_value (enum tree_code comp
, value_range
*vr
, tree val
,
4508 bool *strict_overflow_p
)
4510 if (vr
->type
== VR_VARYING
|| vr
->type
== VR_UNDEFINED
)
4513 /* Anti-ranges need to be handled separately. */
4514 if (vr
->type
== VR_ANTI_RANGE
)
4516 /* For anti-ranges, the only predicates that we can compute at
4517 compile time are equality and inequality. */
4524 /* ~[VAL_1, VAL_2] OP VAL is known if VAL_1 <= VAL <= VAL_2. */
4525 if (value_inside_range (val
, vr
->min
, vr
->max
) == 1)
4526 return (comp
== NE_EXPR
) ? boolean_true_node
: boolean_false_node
;
4531 if (!usable_range_p (vr
, strict_overflow_p
))
4534 if (comp
== EQ_EXPR
)
4536 /* EQ_EXPR may only be computed if VR represents exactly
4538 if (compare_values_warnv (vr
->min
, vr
->max
, strict_overflow_p
) == 0)
4540 int cmp
= compare_values_warnv (vr
->min
, val
, strict_overflow_p
);
4542 return boolean_true_node
;
4543 else if (cmp
== -1 || cmp
== 1 || cmp
== 2)
4544 return boolean_false_node
;
4546 else if (compare_values_warnv (val
, vr
->min
, strict_overflow_p
) == -1
4547 || compare_values_warnv (vr
->max
, val
, strict_overflow_p
) == -1)
4548 return boolean_false_node
;
4552 else if (comp
== NE_EXPR
)
4554 /* If VAL is not inside VR, then they are always different. */
4555 if (compare_values_warnv (vr
->max
, val
, strict_overflow_p
) == -1
4556 || compare_values_warnv (vr
->min
, val
, strict_overflow_p
) == 1)
4557 return boolean_true_node
;
4559 /* If VR represents exactly one value equal to VAL, then return
4561 if (compare_values_warnv (vr
->min
, vr
->max
, strict_overflow_p
) == 0
4562 && compare_values_warnv (vr
->min
, val
, strict_overflow_p
) == 0)
4563 return boolean_false_node
;
4565 /* Otherwise, they may or may not be different. */
4568 else if (comp
== LT_EXPR
|| comp
== LE_EXPR
)
4572 /* If VR is to the left of VAL, return true. */
4573 tst
= compare_values_warnv (vr
->max
, val
, strict_overflow_p
);
4574 if ((comp
== LT_EXPR
&& tst
== -1)
4575 || (comp
== LE_EXPR
&& (tst
== -1 || tst
== 0)))
4577 if (overflow_infinity_range_p (vr
))
4578 *strict_overflow_p
= true;
4579 return boolean_true_node
;
4582 /* If VR is to the right of VAL, return false. */
4583 tst
= compare_values_warnv (vr
->min
, val
, strict_overflow_p
);
4584 if ((comp
== LT_EXPR
&& (tst
== 0 || tst
== 1))
4585 || (comp
== LE_EXPR
&& tst
== 1))
4587 if (overflow_infinity_range_p (vr
))
4588 *strict_overflow_p
= true;
4589 return boolean_false_node
;
4592 /* Otherwise, we don't know. */
4595 else if (comp
== GT_EXPR
|| comp
== GE_EXPR
)
4599 /* If VR is to the right of VAL, return true. */
4600 tst
= compare_values_warnv (vr
->min
, val
, strict_overflow_p
);
4601 if ((comp
== GT_EXPR
&& tst
== 1)
4602 || (comp
== GE_EXPR
&& (tst
== 0 || tst
== 1)))
4604 if (overflow_infinity_range_p (vr
))
4605 *strict_overflow_p
= true;
4606 return boolean_true_node
;
4609 /* If VR is to the left of VAL, return false. */
4610 tst
= compare_values_warnv (vr
->max
, val
, strict_overflow_p
);
4611 if ((comp
== GT_EXPR
&& (tst
== -1 || tst
== 0))
4612 || (comp
== GE_EXPR
&& tst
== -1))
4614 if (overflow_infinity_range_p (vr
))
4615 *strict_overflow_p
= true;
4616 return boolean_false_node
;
4619 /* Otherwise, we don't know. */
4627 /* Debugging dumps. */
4629 void dump_value_range (FILE *, value_range
*);
4630 void debug_value_range (value_range
*);
4631 void dump_all_value_ranges (FILE *);
4632 void debug_all_value_ranges (void);
4633 void dump_vr_equiv (FILE *, bitmap
);
4634 void debug_vr_equiv (bitmap
);
4637 /* Dump value range VR to FILE. */
4640 dump_value_range (FILE *file
, value_range
*vr
)
4643 fprintf (file
, "[]");
4644 else if (vr
->type
== VR_UNDEFINED
)
4645 fprintf (file
, "UNDEFINED");
4646 else if (vr
->type
== VR_RANGE
|| vr
->type
== VR_ANTI_RANGE
)
4648 tree type
= TREE_TYPE (vr
->min
);
4650 fprintf (file
, "%s[", (vr
->type
== VR_ANTI_RANGE
) ? "~" : "");
4652 if (is_negative_overflow_infinity (vr
->min
))
4653 fprintf (file
, "-INF(OVF)");
4654 else if (INTEGRAL_TYPE_P (type
)
4655 && !TYPE_UNSIGNED (type
)
4656 && vrp_val_is_min (vr
->min
))
4657 fprintf (file
, "-INF");
4659 print_generic_expr (file
, vr
->min
, 0);
4661 fprintf (file
, ", ");
4663 if (is_positive_overflow_infinity (vr
->max
))
4664 fprintf (file
, "+INF(OVF)");
4665 else if (INTEGRAL_TYPE_P (type
)
4666 && vrp_val_is_max (vr
->max
))
4667 fprintf (file
, "+INF");
4669 print_generic_expr (file
, vr
->max
, 0);
4671 fprintf (file
, "]");
4678 fprintf (file
, " EQUIVALENCES: { ");
4680 EXECUTE_IF_SET_IN_BITMAP (vr
->equiv
, 0, i
, bi
)
4682 print_generic_expr (file
, ssa_name (i
), 0);
4683 fprintf (file
, " ");
4687 fprintf (file
, "} (%u elements)", c
);
4690 else if (vr
->type
== VR_VARYING
)
4691 fprintf (file
, "VARYING");
4693 fprintf (file
, "INVALID RANGE");
4697 /* Dump value range VR to stderr. */
4700 debug_value_range (value_range
*vr
)
4702 dump_value_range (stderr
, vr
);
4703 fprintf (stderr
, "\n");
4707 /* Dump value ranges of all SSA_NAMEs to FILE. */
4710 dump_all_value_ranges (FILE *file
)
4714 for (i
= 0; i
< num_vr_values
; i
++)
4718 print_generic_expr (file
, ssa_name (i
), 0);
4719 fprintf (file
, ": ");
4720 dump_value_range (file
, vr_value
[i
]);
4721 fprintf (file
, "\n");
4725 fprintf (file
, "\n");
4729 /* Dump all value ranges to stderr. */
4732 debug_all_value_ranges (void)
4734 dump_all_value_ranges (stderr
);
4738 /* Given a COND_EXPR COND of the form 'V OP W', and an SSA name V,
4739 create a new SSA name N and return the assertion assignment
4740 'N = ASSERT_EXPR <V, V OP W>'. */
4743 build_assert_expr_for (tree cond
, tree v
)
4748 gcc_assert (TREE_CODE (v
) == SSA_NAME
4749 && COMPARISON_CLASS_P (cond
));
4751 a
= build2 (ASSERT_EXPR
, TREE_TYPE (v
), v
, cond
);
4752 assertion
= gimple_build_assign (NULL_TREE
, a
);
4754 /* The new ASSERT_EXPR, creates a new SSA name that replaces the
4755 operand of the ASSERT_EXPR. Create it so the new name and the old one
4756 are registered in the replacement table so that we can fix the SSA web
4757 after adding all the ASSERT_EXPRs. */
4758 create_new_def_for (v
, assertion
, NULL
);
4764 /* Return false if EXPR is a predicate expression involving floating
4768 fp_predicate (gimple
*stmt
)
4770 GIMPLE_CHECK (stmt
, GIMPLE_COND
);
4772 return FLOAT_TYPE_P (TREE_TYPE (gimple_cond_lhs (stmt
)));
4775 /* If the range of values taken by OP can be inferred after STMT executes,
4776 return the comparison code (COMP_CODE_P) and value (VAL_P) that
4777 describes the inferred range. Return true if a range could be
4781 infer_value_range (gimple
*stmt
, tree op
, tree_code
*comp_code_p
, tree
*val_p
)
4784 *comp_code_p
= ERROR_MARK
;
4786 /* Do not attempt to infer anything in names that flow through
4788 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op
))
4791 /* Similarly, don't infer anything from statements that may throw
4792 exceptions. ??? Relax this requirement? */
4793 if (stmt_could_throw_p (stmt
))
4796 /* If STMT is the last statement of a basic block with no normal
4797 successors, there is no point inferring anything about any of its
4798 operands. We would not be able to find a proper insertion point
4799 for the assertion, anyway. */
4800 if (stmt_ends_bb_p (stmt
))
4805 FOR_EACH_EDGE (e
, ei
, gimple_bb (stmt
)->succs
)
4806 if (!(e
->flags
& EDGE_ABNORMAL
))
4812 if (infer_nonnull_range (stmt
, op
))
4814 *val_p
= build_int_cst (TREE_TYPE (op
), 0);
4815 *comp_code_p
= NE_EXPR
;
4823 void dump_asserts_for (FILE *, tree
);
4824 void debug_asserts_for (tree
);
4825 void dump_all_asserts (FILE *);
4826 void debug_all_asserts (void);
4828 /* Dump all the registered assertions for NAME to FILE. */
4831 dump_asserts_for (FILE *file
, tree name
)
4835 fprintf (file
, "Assertions to be inserted for ");
4836 print_generic_expr (file
, name
, 0);
4837 fprintf (file
, "\n");
4839 loc
= asserts_for
[SSA_NAME_VERSION (name
)];
4842 fprintf (file
, "\t");
4843 print_gimple_stmt (file
, gsi_stmt (loc
->si
), 0, 0);
4844 fprintf (file
, "\n\tBB #%d", loc
->bb
->index
);
4847 fprintf (file
, "\n\tEDGE %d->%d", loc
->e
->src
->index
,
4848 loc
->e
->dest
->index
);
4849 dump_edge_info (file
, loc
->e
, dump_flags
, 0);
4851 fprintf (file
, "\n\tPREDICATE: ");
4852 print_generic_expr (file
, name
, 0);
4853 fprintf (file
, " %s ", get_tree_code_name (loc
->comp_code
));
4854 print_generic_expr (file
, loc
->val
, 0);
4855 fprintf (file
, "\n\n");
4859 fprintf (file
, "\n");
4863 /* Dump all the registered assertions for NAME to stderr. */
4866 debug_asserts_for (tree name
)
4868 dump_asserts_for (stderr
, name
);
4872 /* Dump all the registered assertions for all the names to FILE. */
4875 dump_all_asserts (FILE *file
)
4880 fprintf (file
, "\nASSERT_EXPRs to be inserted\n\n");
4881 EXECUTE_IF_SET_IN_BITMAP (need_assert_for
, 0, i
, bi
)
4882 dump_asserts_for (file
, ssa_name (i
));
4883 fprintf (file
, "\n");
4887 /* Dump all the registered assertions for all the names to stderr. */
4890 debug_all_asserts (void)
4892 dump_all_asserts (stderr
);
4896 /* If NAME doesn't have an ASSERT_EXPR registered for asserting
4897 'EXPR COMP_CODE VAL' at a location that dominates block BB or
4898 E->DEST, then register this location as a possible insertion point
4899 for ASSERT_EXPR <NAME, EXPR COMP_CODE VAL>.
4901 BB, E and SI provide the exact insertion point for the new
4902 ASSERT_EXPR. If BB is NULL, then the ASSERT_EXPR is to be inserted
4903 on edge E. Otherwise, if E is NULL, the ASSERT_EXPR is inserted on
4904 BB. If SI points to a COND_EXPR or a SWITCH_EXPR statement, then E
4905 must not be NULL. */
4908 register_new_assert_for (tree name
, tree expr
,
4909 enum tree_code comp_code
,
4913 gimple_stmt_iterator si
)
4915 assert_locus
*n
, *loc
, *last_loc
;
4916 basic_block dest_bb
;
4918 gcc_checking_assert (bb
== NULL
|| e
== NULL
);
4921 gcc_checking_assert (gimple_code (gsi_stmt (si
)) != GIMPLE_COND
4922 && gimple_code (gsi_stmt (si
)) != GIMPLE_SWITCH
);
4924 /* Never build an assert comparing against an integer constant with
4925 TREE_OVERFLOW set. This confuses our undefined overflow warning
4927 if (TREE_OVERFLOW_P (val
))
4928 val
= drop_tree_overflow (val
);
4930 /* The new assertion A will be inserted at BB or E. We need to
4931 determine if the new location is dominated by a previously
4932 registered location for A. If we are doing an edge insertion,
4933 assume that A will be inserted at E->DEST. Note that this is not
4936 If E is a critical edge, it will be split. But even if E is
4937 split, the new block will dominate the same set of blocks that
4940 The reverse, however, is not true, blocks dominated by E->DEST
4941 will not be dominated by the new block created to split E. So,
4942 if the insertion location is on a critical edge, we will not use
4943 the new location to move another assertion previously registered
4944 at a block dominated by E->DEST. */
4945 dest_bb
= (bb
) ? bb
: e
->dest
;
4947 /* If NAME already has an ASSERT_EXPR registered for COMP_CODE and
4948 VAL at a block dominating DEST_BB, then we don't need to insert a new
4949 one. Similarly, if the same assertion already exists at a block
4950 dominated by DEST_BB and the new location is not on a critical
4951 edge, then update the existing location for the assertion (i.e.,
4952 move the assertion up in the dominance tree).
4954 Note, this is implemented as a simple linked list because there
4955 should not be more than a handful of assertions registered per
4956 name. If this becomes a performance problem, a table hashed by
4957 COMP_CODE and VAL could be implemented. */
4958 loc
= asserts_for
[SSA_NAME_VERSION (name
)];
4962 if (loc
->comp_code
== comp_code
4964 || operand_equal_p (loc
->val
, val
, 0))
4965 && (loc
->expr
== expr
4966 || operand_equal_p (loc
->expr
, expr
, 0)))
4968 /* If E is not a critical edge and DEST_BB
4969 dominates the existing location for the assertion, move
4970 the assertion up in the dominance tree by updating its
4971 location information. */
4972 if ((e
== NULL
|| !EDGE_CRITICAL_P (e
))
4973 && dominated_by_p (CDI_DOMINATORS
, loc
->bb
, dest_bb
))
4982 /* Update the last node of the list and move to the next one. */
4987 /* If we didn't find an assertion already registered for
4988 NAME COMP_CODE VAL, add a new one at the end of the list of
4989 assertions associated with NAME. */
4990 n
= XNEW (struct assert_locus
);
4994 n
->comp_code
= comp_code
;
5002 asserts_for
[SSA_NAME_VERSION (name
)] = n
;
5004 bitmap_set_bit (need_assert_for
, SSA_NAME_VERSION (name
));
5007 /* (COND_OP0 COND_CODE COND_OP1) is a predicate which uses NAME.
5008 Extract a suitable test code and value and store them into *CODE_P and
5009 *VAL_P so the predicate is normalized to NAME *CODE_P *VAL_P.
5011 If no extraction was possible, return FALSE, otherwise return TRUE.
5013 If INVERT is true, then we invert the result stored into *CODE_P. */
5016 extract_code_and_val_from_cond_with_ops (tree name
, enum tree_code cond_code
,
5017 tree cond_op0
, tree cond_op1
,
5018 bool invert
, enum tree_code
*code_p
,
5021 enum tree_code comp_code
;
5024 /* Otherwise, we have a comparison of the form NAME COMP VAL
5025 or VAL COMP NAME. */
5026 if (name
== cond_op1
)
5028 /* If the predicate is of the form VAL COMP NAME, flip
5029 COMP around because we need to register NAME as the
5030 first operand in the predicate. */
5031 comp_code
= swap_tree_comparison (cond_code
);
5036 /* The comparison is of the form NAME COMP VAL, so the
5037 comparison code remains unchanged. */
5038 comp_code
= cond_code
;
5042 /* Invert the comparison code as necessary. */
5044 comp_code
= invert_tree_comparison (comp_code
, 0);
5046 /* VRP does not handle float types. */
5047 if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (val
)))
5050 /* Do not register always-false predicates.
5051 FIXME: this works around a limitation in fold() when dealing with
5052 enumerations. Given 'enum { N1, N2 } x;', fold will not
5053 fold 'if (x > N2)' to 'if (0)'. */
5054 if ((comp_code
== GT_EXPR
|| comp_code
== LT_EXPR
)
5055 && INTEGRAL_TYPE_P (TREE_TYPE (val
)))
5057 tree min
= TYPE_MIN_VALUE (TREE_TYPE (val
));
5058 tree max
= TYPE_MAX_VALUE (TREE_TYPE (val
));
5060 if (comp_code
== GT_EXPR
5062 || compare_values (val
, max
) == 0))
5065 if (comp_code
== LT_EXPR
5067 || compare_values (val
, min
) == 0))
5070 *code_p
= comp_code
;
5075 /* Find out smallest RES where RES > VAL && (RES & MASK) == RES, if any
5076 (otherwise return VAL). VAL and MASK must be zero-extended for
5077 precision PREC. If SGNBIT is non-zero, first xor VAL with SGNBIT
5078 (to transform signed values into unsigned) and at the end xor
5082 masked_increment (const wide_int
&val_in
, const wide_int
&mask
,
5083 const wide_int
&sgnbit
, unsigned int prec
)
5085 wide_int bit
= wi::one (prec
), res
;
5088 wide_int val
= val_in
^ sgnbit
;
5089 for (i
= 0; i
< prec
; i
++, bit
+= bit
)
5092 if ((res
& bit
) == 0)
5095 res
= (val
+ bit
).and_not (res
);
5097 if (wi::gtu_p (res
, val
))
5098 return res
^ sgnbit
;
5100 return val
^ sgnbit
;
5103 /* Try to register an edge assertion for SSA name NAME on edge E for
5104 the condition COND contributing to the conditional jump pointed to by BSI.
5105 Invert the condition COND if INVERT is true. */
5108 register_edge_assert_for_2 (tree name
, edge e
, gimple_stmt_iterator bsi
,
5109 enum tree_code cond_code
,
5110 tree cond_op0
, tree cond_op1
, bool invert
)
5113 enum tree_code comp_code
;
5115 if (!extract_code_and_val_from_cond_with_ops (name
, cond_code
,
5118 invert
, &comp_code
, &val
))
5121 /* Only register an ASSERT_EXPR if NAME was found in the sub-graph
5122 reachable from E. */
5123 if (live_on_edge (e
, name
)
5124 && !has_single_use (name
))
5125 register_new_assert_for (name
, name
, comp_code
, val
, NULL
, e
, bsi
);
5127 /* In the case of NAME <= CST and NAME being defined as
5128 NAME = (unsigned) NAME2 + CST2 we can assert NAME2 >= -CST2
5129 and NAME2 <= CST - CST2. We can do the same for NAME > CST.
5130 This catches range and anti-range tests. */
5131 if ((comp_code
== LE_EXPR
5132 || comp_code
== GT_EXPR
)
5133 && TREE_CODE (val
) == INTEGER_CST
5134 && TYPE_UNSIGNED (TREE_TYPE (val
)))
5136 gimple
*def_stmt
= SSA_NAME_DEF_STMT (name
);
5137 tree cst2
= NULL_TREE
, name2
= NULL_TREE
, name3
= NULL_TREE
;
5139 /* Extract CST2 from the (optional) addition. */
5140 if (is_gimple_assign (def_stmt
)
5141 && gimple_assign_rhs_code (def_stmt
) == PLUS_EXPR
)
5143 name2
= gimple_assign_rhs1 (def_stmt
);
5144 cst2
= gimple_assign_rhs2 (def_stmt
);
5145 if (TREE_CODE (name2
) == SSA_NAME
5146 && TREE_CODE (cst2
) == INTEGER_CST
)
5147 def_stmt
= SSA_NAME_DEF_STMT (name2
);
5150 /* Extract NAME2 from the (optional) sign-changing cast. */
5151 if (gimple_assign_cast_p (def_stmt
))
5153 if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt
))
5154 && ! TYPE_UNSIGNED (TREE_TYPE (gimple_assign_rhs1 (def_stmt
)))
5155 && (TYPE_PRECISION (gimple_expr_type (def_stmt
))
5156 == TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt
)))))
5157 name3
= gimple_assign_rhs1 (def_stmt
);
5160 /* If name3 is used later, create an ASSERT_EXPR for it. */
5161 if (name3
!= NULL_TREE
5162 && TREE_CODE (name3
) == SSA_NAME
5163 && (cst2
== NULL_TREE
5164 || TREE_CODE (cst2
) == INTEGER_CST
)
5165 && INTEGRAL_TYPE_P (TREE_TYPE (name3
))
5166 && live_on_edge (e
, name3
)
5167 && !has_single_use (name3
))
5171 /* Build an expression for the range test. */
5172 tmp
= build1 (NOP_EXPR
, TREE_TYPE (name
), name3
);
5173 if (cst2
!= NULL_TREE
)
5174 tmp
= build2 (PLUS_EXPR
, TREE_TYPE (name
), tmp
, cst2
);
5178 fprintf (dump_file
, "Adding assert for ");
5179 print_generic_expr (dump_file
, name3
, 0);
5180 fprintf (dump_file
, " from ");
5181 print_generic_expr (dump_file
, tmp
, 0);
5182 fprintf (dump_file
, "\n");
5185 register_new_assert_for (name3
, tmp
, comp_code
, val
, NULL
, e
, bsi
);
5188 /* If name2 is used later, create an ASSERT_EXPR for it. */
5189 if (name2
!= NULL_TREE
5190 && TREE_CODE (name2
) == SSA_NAME
5191 && TREE_CODE (cst2
) == INTEGER_CST
5192 && INTEGRAL_TYPE_P (TREE_TYPE (name2
))
5193 && live_on_edge (e
, name2
)
5194 && !has_single_use (name2
))
5198 /* Build an expression for the range test. */
5200 if (TREE_TYPE (name
) != TREE_TYPE (name2
))
5201 tmp
= build1 (NOP_EXPR
, TREE_TYPE (name
), tmp
);
5202 if (cst2
!= NULL_TREE
)
5203 tmp
= build2 (PLUS_EXPR
, TREE_TYPE (name
), tmp
, cst2
);
5207 fprintf (dump_file
, "Adding assert for ");
5208 print_generic_expr (dump_file
, name2
, 0);
5209 fprintf (dump_file
, " from ");
5210 print_generic_expr (dump_file
, tmp
, 0);
5211 fprintf (dump_file
, "\n");
5214 register_new_assert_for (name2
, tmp
, comp_code
, val
, NULL
, e
, bsi
);
5218 /* In the case of post-in/decrement tests like if (i++) ... and uses
5219 of the in/decremented value on the edge the extra name we want to
5220 assert for is not on the def chain of the name compared. Instead
5221 it is in the set of use stmts.
5222 Similar cases happen for conversions that were simplified through
5223 fold_{sign_changed,widened}_comparison. */
5224 if ((comp_code
== NE_EXPR
5225 || comp_code
== EQ_EXPR
)
5226 && TREE_CODE (val
) == INTEGER_CST
)
5228 imm_use_iterator ui
;
5230 FOR_EACH_IMM_USE_STMT (use_stmt
, ui
, name
)
5232 if (!is_gimple_assign (use_stmt
))
5235 /* Cut off to use-stmts that are dominating the predecessor. */
5236 if (!dominated_by_p (CDI_DOMINATORS
, e
->src
, gimple_bb (use_stmt
)))
5239 tree name2
= gimple_assign_lhs (use_stmt
);
5240 if (TREE_CODE (name2
) != SSA_NAME
5241 || !live_on_edge (e
, name2
))
5244 enum tree_code code
= gimple_assign_rhs_code (use_stmt
);
5246 if (code
== PLUS_EXPR
5247 || code
== MINUS_EXPR
)
5249 cst
= gimple_assign_rhs2 (use_stmt
);
5250 if (TREE_CODE (cst
) != INTEGER_CST
)
5252 cst
= int_const_binop (code
, val
, cst
);
5254 else if (CONVERT_EXPR_CODE_P (code
))
5256 /* For truncating conversions we cannot record
5258 if (comp_code
== NE_EXPR
5259 && (TYPE_PRECISION (TREE_TYPE (name2
))
5260 < TYPE_PRECISION (TREE_TYPE (name
))))
5262 cst
= fold_convert (TREE_TYPE (name2
), val
);
5267 if (TREE_OVERFLOW_P (cst
))
5268 cst
= drop_tree_overflow (cst
);
5269 register_new_assert_for (name2
, name2
, comp_code
, cst
,
5274 if (TREE_CODE_CLASS (comp_code
) == tcc_comparison
5275 && TREE_CODE (val
) == INTEGER_CST
)
5277 gimple
*def_stmt
= SSA_NAME_DEF_STMT (name
);
5278 tree name2
= NULL_TREE
, names
[2], cst2
= NULL_TREE
;
5279 tree val2
= NULL_TREE
;
5280 unsigned int prec
= TYPE_PRECISION (TREE_TYPE (val
));
5281 wide_int mask
= wi::zero (prec
);
5282 unsigned int nprec
= prec
;
5283 enum tree_code rhs_code
= ERROR_MARK
;
5285 if (is_gimple_assign (def_stmt
))
5286 rhs_code
= gimple_assign_rhs_code (def_stmt
);
5288 /* Add asserts for NAME cmp CST and NAME being defined
5289 as NAME = (int) NAME2. */
5290 if (!TYPE_UNSIGNED (TREE_TYPE (val
))
5291 && (comp_code
== LE_EXPR
|| comp_code
== LT_EXPR
5292 || comp_code
== GT_EXPR
|| comp_code
== GE_EXPR
)
5293 && gimple_assign_cast_p (def_stmt
))
5295 name2
= gimple_assign_rhs1 (def_stmt
);
5296 if (CONVERT_EXPR_CODE_P (rhs_code
)
5297 && INTEGRAL_TYPE_P (TREE_TYPE (name2
))
5298 && TYPE_UNSIGNED (TREE_TYPE (name2
))
5299 && prec
== TYPE_PRECISION (TREE_TYPE (name2
))
5300 && (comp_code
== LE_EXPR
|| comp_code
== GT_EXPR
5301 || !tree_int_cst_equal (val
,
5302 TYPE_MIN_VALUE (TREE_TYPE (val
))))
5303 && live_on_edge (e
, name2
)
5304 && !has_single_use (name2
))
5307 enum tree_code new_comp_code
= comp_code
;
5309 cst
= fold_convert (TREE_TYPE (name2
),
5310 TYPE_MIN_VALUE (TREE_TYPE (val
)));
5311 /* Build an expression for the range test. */
5312 tmp
= build2 (PLUS_EXPR
, TREE_TYPE (name2
), name2
, cst
);
5313 cst
= fold_build2 (PLUS_EXPR
, TREE_TYPE (name2
), cst
,
5314 fold_convert (TREE_TYPE (name2
), val
));
5315 if (comp_code
== LT_EXPR
|| comp_code
== GE_EXPR
)
5317 new_comp_code
= comp_code
== LT_EXPR
? LE_EXPR
: GT_EXPR
;
5318 cst
= fold_build2 (MINUS_EXPR
, TREE_TYPE (name2
), cst
,
5319 build_int_cst (TREE_TYPE (name2
), 1));
5324 fprintf (dump_file
, "Adding assert for ");
5325 print_generic_expr (dump_file
, name2
, 0);
5326 fprintf (dump_file
, " from ");
5327 print_generic_expr (dump_file
, tmp
, 0);
5328 fprintf (dump_file
, "\n");
5331 register_new_assert_for (name2
, tmp
, new_comp_code
, cst
, NULL
,
5336 /* Add asserts for NAME cmp CST and NAME being defined as
5337 NAME = NAME2 >> CST2.
5339 Extract CST2 from the right shift. */
5340 if (rhs_code
== RSHIFT_EXPR
)
5342 name2
= gimple_assign_rhs1 (def_stmt
);
5343 cst2
= gimple_assign_rhs2 (def_stmt
);
5344 if (TREE_CODE (name2
) == SSA_NAME
5345 && tree_fits_uhwi_p (cst2
)
5346 && INTEGRAL_TYPE_P (TREE_TYPE (name2
))
5347 && IN_RANGE (tree_to_uhwi (cst2
), 1, prec
- 1)
5348 && prec
== GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (val
)))
5349 && live_on_edge (e
, name2
)
5350 && !has_single_use (name2
))
5352 mask
= wi::mask (tree_to_uhwi (cst2
), false, prec
);
5353 val2
= fold_binary (LSHIFT_EXPR
, TREE_TYPE (val
), val
, cst2
);
5356 if (val2
!= NULL_TREE
5357 && TREE_CODE (val2
) == INTEGER_CST
5358 && simple_cst_equal (fold_build2 (RSHIFT_EXPR
,
5362 enum tree_code new_comp_code
= comp_code
;
5366 if (comp_code
== EQ_EXPR
|| comp_code
== NE_EXPR
)
5368 if (!TYPE_UNSIGNED (TREE_TYPE (val
)))
5370 tree type
= build_nonstandard_integer_type (prec
, 1);
5371 tmp
= build1 (NOP_EXPR
, type
, name2
);
5372 val2
= fold_convert (type
, val2
);
5374 tmp
= fold_build2 (MINUS_EXPR
, TREE_TYPE (tmp
), tmp
, val2
);
5375 new_val
= wide_int_to_tree (TREE_TYPE (tmp
), mask
);
5376 new_comp_code
= comp_code
== EQ_EXPR
? LE_EXPR
: GT_EXPR
;
5378 else if (comp_code
== LT_EXPR
|| comp_code
== GE_EXPR
)
5381 = wi::min_value (prec
, TYPE_SIGN (TREE_TYPE (val
)));
5383 if (minval
== new_val
)
5384 new_val
= NULL_TREE
;
5389 = wi::max_value (prec
, TYPE_SIGN (TREE_TYPE (val
)));
5392 new_val
= NULL_TREE
;
5394 new_val
= wide_int_to_tree (TREE_TYPE (val2
), mask
);
5401 fprintf (dump_file
, "Adding assert for ");
5402 print_generic_expr (dump_file
, name2
, 0);
5403 fprintf (dump_file
, " from ");
5404 print_generic_expr (dump_file
, tmp
, 0);
5405 fprintf (dump_file
, "\n");
5408 register_new_assert_for (name2
, tmp
, new_comp_code
, new_val
,
5413 /* Add asserts for NAME cmp CST and NAME being defined as
5414 NAME = NAME2 & CST2.
5416 Extract CST2 from the and.
5419 NAME = (unsigned) NAME2;
5420 casts where NAME's type is unsigned and has smaller precision
5421 than NAME2's type as if it was NAME = NAME2 & MASK. */
5422 names
[0] = NULL_TREE
;
5423 names
[1] = NULL_TREE
;
5425 if (rhs_code
== BIT_AND_EXPR
5426 || (CONVERT_EXPR_CODE_P (rhs_code
)
5427 && TREE_CODE (TREE_TYPE (val
)) == INTEGER_TYPE
5428 && TYPE_UNSIGNED (TREE_TYPE (val
))
5429 && TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt
)))
5432 name2
= gimple_assign_rhs1 (def_stmt
);
5433 if (rhs_code
== BIT_AND_EXPR
)
5434 cst2
= gimple_assign_rhs2 (def_stmt
);
5437 cst2
= TYPE_MAX_VALUE (TREE_TYPE (val
));
5438 nprec
= TYPE_PRECISION (TREE_TYPE (name2
));
5440 if (TREE_CODE (name2
) == SSA_NAME
5441 && INTEGRAL_TYPE_P (TREE_TYPE (name2
))
5442 && TREE_CODE (cst2
) == INTEGER_CST
5443 && !integer_zerop (cst2
)
5445 || TYPE_UNSIGNED (TREE_TYPE (val
))))
5447 gimple
*def_stmt2
= SSA_NAME_DEF_STMT (name2
);
5448 if (gimple_assign_cast_p (def_stmt2
))
5450 names
[1] = gimple_assign_rhs1 (def_stmt2
);
5451 if (!CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt2
))
5452 || !INTEGRAL_TYPE_P (TREE_TYPE (names
[1]))
5453 || (TYPE_PRECISION (TREE_TYPE (name2
))
5454 != TYPE_PRECISION (TREE_TYPE (names
[1])))
5455 || !live_on_edge (e
, names
[1])
5456 || has_single_use (names
[1]))
5457 names
[1] = NULL_TREE
;
5459 if (live_on_edge (e
, name2
)
5460 && !has_single_use (name2
))
5464 if (names
[0] || names
[1])
5466 wide_int minv
, maxv
, valv
, cst2v
;
5467 wide_int tem
, sgnbit
;
5468 bool valid_p
= false, valn
, cst2n
;
5469 enum tree_code ccode
= comp_code
;
5471 valv
= wide_int::from (val
, nprec
, UNSIGNED
);
5472 cst2v
= wide_int::from (cst2
, nprec
, UNSIGNED
);
5473 valn
= wi::neg_p (valv
, TYPE_SIGN (TREE_TYPE (val
)));
5474 cst2n
= wi::neg_p (cst2v
, TYPE_SIGN (TREE_TYPE (val
)));
5475 /* If CST2 doesn't have most significant bit set,
5476 but VAL is negative, we have comparison like
5477 if ((x & 0x123) > -4) (always true). Just give up. */
5481 sgnbit
= wi::set_bit_in_zero (nprec
- 1, nprec
);
5483 sgnbit
= wi::zero (nprec
);
5484 minv
= valv
& cst2v
;
5488 /* Minimum unsigned value for equality is VAL & CST2
5489 (should be equal to VAL, otherwise we probably should
5490 have folded the comparison into false) and
5491 maximum unsigned value is VAL | ~CST2. */
5492 maxv
= valv
| ~cst2v
;
5497 tem
= valv
| ~cst2v
;
5498 /* If VAL is 0, handle (X & CST2) != 0 as (X & CST2) > 0U. */
5502 sgnbit
= wi::zero (nprec
);
5505 /* If (VAL | ~CST2) is all ones, handle it as
5506 (X & CST2) < VAL. */
5511 sgnbit
= wi::zero (nprec
);
5514 if (!cst2n
&& wi::neg_p (cst2v
))
5515 sgnbit
= wi::set_bit_in_zero (nprec
- 1, nprec
);
5524 if (tem
== wi::mask (nprec
- 1, false, nprec
))
5530 sgnbit
= wi::zero (nprec
);
5535 /* Minimum unsigned value for >= if (VAL & CST2) == VAL
5536 is VAL and maximum unsigned value is ~0. For signed
5537 comparison, if CST2 doesn't have most significant bit
5538 set, handle it similarly. If CST2 has MSB set,
5539 the minimum is the same, and maximum is ~0U/2. */
5542 /* If (VAL & CST2) != VAL, X & CST2 can't be equal to
5544 minv
= masked_increment (valv
, cst2v
, sgnbit
, nprec
);
5548 maxv
= wi::mask (nprec
- (cst2n
? 1 : 0), false, nprec
);
5554 /* Find out smallest MINV where MINV > VAL
5555 && (MINV & CST2) == MINV, if any. If VAL is signed and
5556 CST2 has MSB set, compute it biased by 1 << (nprec - 1). */
5557 minv
= masked_increment (valv
, cst2v
, sgnbit
, nprec
);
5560 maxv
= wi::mask (nprec
- (cst2n
? 1 : 0), false, nprec
);
5565 /* Minimum unsigned value for <= is 0 and maximum
5566 unsigned value is VAL | ~CST2 if (VAL & CST2) == VAL.
5567 Otherwise, find smallest VAL2 where VAL2 > VAL
5568 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
5570 For signed comparison, if CST2 doesn't have most
5571 significant bit set, handle it similarly. If CST2 has
5572 MSB set, the maximum is the same and minimum is INT_MIN. */
5577 maxv
= masked_increment (valv
, cst2v
, sgnbit
, nprec
);
5589 /* Minimum unsigned value for < is 0 and maximum
5590 unsigned value is (VAL-1) | ~CST2 if (VAL & CST2) == VAL.
5591 Otherwise, find smallest VAL2 where VAL2 > VAL
5592 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
5594 For signed comparison, if CST2 doesn't have most
5595 significant bit set, handle it similarly. If CST2 has
5596 MSB set, the maximum is the same and minimum is INT_MIN. */
5605 maxv
= masked_increment (valv
, cst2v
, sgnbit
, nprec
);
5619 && (maxv
- minv
) != -1)
5621 tree tmp
, new_val
, type
;
5624 for (i
= 0; i
< 2; i
++)
5627 wide_int maxv2
= maxv
;
5629 type
= TREE_TYPE (names
[i
]);
5630 if (!TYPE_UNSIGNED (type
))
5632 type
= build_nonstandard_integer_type (nprec
, 1);
5633 tmp
= build1 (NOP_EXPR
, type
, names
[i
]);
5637 tmp
= build2 (PLUS_EXPR
, type
, tmp
,
5638 wide_int_to_tree (type
, -minv
));
5639 maxv2
= maxv
- minv
;
5641 new_val
= wide_int_to_tree (type
, maxv2
);
5645 fprintf (dump_file
, "Adding assert for ");
5646 print_generic_expr (dump_file
, names
[i
], 0);
5647 fprintf (dump_file
, " from ");
5648 print_generic_expr (dump_file
, tmp
, 0);
5649 fprintf (dump_file
, "\n");
5652 register_new_assert_for (names
[i
], tmp
, LE_EXPR
,
5653 new_val
, NULL
, e
, bsi
);
5660 /* OP is an operand of a truth value expression which is known to have
5661 a particular value. Register any asserts for OP and for any
5662 operands in OP's defining statement.
5664 If CODE is EQ_EXPR, then we want to register OP is zero (false),
5665 if CODE is NE_EXPR, then we want to register OP is nonzero (true). */
5668 register_edge_assert_for_1 (tree op
, enum tree_code code
,
5669 edge e
, gimple_stmt_iterator bsi
)
5673 enum tree_code rhs_code
;
5675 /* We only care about SSA_NAMEs. */
5676 if (TREE_CODE (op
) != SSA_NAME
)
5679 /* We know that OP will have a zero or nonzero value. If OP is used
5680 more than once go ahead and register an assert for OP. */
5681 if (live_on_edge (e
, op
)
5682 && !has_single_use (op
))
5684 val
= build_int_cst (TREE_TYPE (op
), 0);
5685 register_new_assert_for (op
, op
, code
, val
, NULL
, e
, bsi
);
5688 /* Now look at how OP is set. If it's set from a comparison,
5689 a truth operation or some bit operations, then we may be able
5690 to register information about the operands of that assignment. */
5691 op_def
= SSA_NAME_DEF_STMT (op
);
5692 if (gimple_code (op_def
) != GIMPLE_ASSIGN
)
5695 rhs_code
= gimple_assign_rhs_code (op_def
);
5697 if (TREE_CODE_CLASS (rhs_code
) == tcc_comparison
)
5699 bool invert
= (code
== EQ_EXPR
? true : false);
5700 tree op0
= gimple_assign_rhs1 (op_def
);
5701 tree op1
= gimple_assign_rhs2 (op_def
);
5703 if (TREE_CODE (op0
) == SSA_NAME
)
5704 register_edge_assert_for_2 (op0
, e
, bsi
, rhs_code
, op0
, op1
, invert
);
5705 if (TREE_CODE (op1
) == SSA_NAME
)
5706 register_edge_assert_for_2 (op1
, e
, bsi
, rhs_code
, op0
, op1
, invert
);
5708 else if ((code
== NE_EXPR
5709 && gimple_assign_rhs_code (op_def
) == BIT_AND_EXPR
)
5711 && gimple_assign_rhs_code (op_def
) == BIT_IOR_EXPR
))
5713 /* Recurse on each operand. */
5714 tree op0
= gimple_assign_rhs1 (op_def
);
5715 tree op1
= gimple_assign_rhs2 (op_def
);
5716 if (TREE_CODE (op0
) == SSA_NAME
5717 && has_single_use (op0
))
5718 register_edge_assert_for_1 (op0
, code
, e
, bsi
);
5719 if (TREE_CODE (op1
) == SSA_NAME
5720 && has_single_use (op1
))
5721 register_edge_assert_for_1 (op1
, code
, e
, bsi
);
5723 else if (gimple_assign_rhs_code (op_def
) == BIT_NOT_EXPR
5724 && TYPE_PRECISION (TREE_TYPE (gimple_assign_lhs (op_def
))) == 1)
5726 /* Recurse, flipping CODE. */
5727 code
= invert_tree_comparison (code
, false);
5728 register_edge_assert_for_1 (gimple_assign_rhs1 (op_def
), code
, e
, bsi
);
5730 else if (gimple_assign_rhs_code (op_def
) == SSA_NAME
)
5732 /* Recurse through the copy. */
5733 register_edge_assert_for_1 (gimple_assign_rhs1 (op_def
), code
, e
, bsi
);
5735 else if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (op_def
)))
5737 /* Recurse through the type conversion, unless it is a narrowing
5738 conversion or conversion from non-integral type. */
5739 tree rhs
= gimple_assign_rhs1 (op_def
);
5740 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs
))
5741 && (TYPE_PRECISION (TREE_TYPE (rhs
))
5742 <= TYPE_PRECISION (TREE_TYPE (op
))))
5743 register_edge_assert_for_1 (rhs
, code
, e
, bsi
);
5747 /* Try to register an edge assertion for SSA name NAME on edge E for
5748 the condition COND contributing to the conditional jump pointed to by
5752 register_edge_assert_for (tree name
, edge e
, gimple_stmt_iterator si
,
5753 enum tree_code cond_code
, tree cond_op0
,
5757 enum tree_code comp_code
;
5758 bool is_else_edge
= (e
->flags
& EDGE_FALSE_VALUE
) != 0;
5760 /* Do not attempt to infer anything in names that flow through
5762 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name
))
5765 if (!extract_code_and_val_from_cond_with_ops (name
, cond_code
,
5771 /* Register ASSERT_EXPRs for name. */
5772 register_edge_assert_for_2 (name
, e
, si
, cond_code
, cond_op0
,
5773 cond_op1
, is_else_edge
);
5776 /* If COND is effectively an equality test of an SSA_NAME against
5777 the value zero or one, then we may be able to assert values
5778 for SSA_NAMEs which flow into COND. */
5780 /* In the case of NAME == 1 or NAME != 0, for BIT_AND_EXPR defining
5781 statement of NAME we can assert both operands of the BIT_AND_EXPR
5782 have nonzero value. */
5783 if (((comp_code
== EQ_EXPR
&& integer_onep (val
))
5784 || (comp_code
== NE_EXPR
&& integer_zerop (val
))))
5786 gimple
*def_stmt
= SSA_NAME_DEF_STMT (name
);
5788 if (is_gimple_assign (def_stmt
)
5789 && gimple_assign_rhs_code (def_stmt
) == BIT_AND_EXPR
)
5791 tree op0
= gimple_assign_rhs1 (def_stmt
);
5792 tree op1
= gimple_assign_rhs2 (def_stmt
);
5793 register_edge_assert_for_1 (op0
, NE_EXPR
, e
, si
);
5794 register_edge_assert_for_1 (op1
, NE_EXPR
, e
, si
);
5798 /* In the case of NAME == 0 or NAME != 1, for BIT_IOR_EXPR defining
5799 statement of NAME we can assert both operands of the BIT_IOR_EXPR
5801 if (((comp_code
== EQ_EXPR
&& integer_zerop (val
))
5802 || (comp_code
== NE_EXPR
&& integer_onep (val
))))
5804 gimple
*def_stmt
= SSA_NAME_DEF_STMT (name
);
5806 /* For BIT_IOR_EXPR only if NAME == 0 both operands have
5807 necessarily zero value, or if type-precision is one. */
5808 if (is_gimple_assign (def_stmt
)
5809 && (gimple_assign_rhs_code (def_stmt
) == BIT_IOR_EXPR
5810 && (TYPE_PRECISION (TREE_TYPE (name
)) == 1
5811 || comp_code
== EQ_EXPR
)))
5813 tree op0
= gimple_assign_rhs1 (def_stmt
);
5814 tree op1
= gimple_assign_rhs2 (def_stmt
);
5815 register_edge_assert_for_1 (op0
, EQ_EXPR
, e
, si
);
5816 register_edge_assert_for_1 (op1
, EQ_EXPR
, e
, si
);
5822 /* Determine whether the outgoing edges of BB should receive an
5823 ASSERT_EXPR for each of the operands of BB's LAST statement.
5824 The last statement of BB must be a COND_EXPR.
5826 If any of the sub-graphs rooted at BB have an interesting use of
5827 the predicate operands, an assert location node is added to the
5828 list of assertions for the corresponding operands. */
5831 find_conditional_asserts (basic_block bb
, gcond
*last
)
5833 gimple_stmt_iterator bsi
;
5839 bsi
= gsi_for_stmt (last
);
5841 /* Look for uses of the operands in each of the sub-graphs
5842 rooted at BB. We need to check each of the outgoing edges
5843 separately, so that we know what kind of ASSERT_EXPR to
5845 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
5850 /* Register the necessary assertions for each operand in the
5851 conditional predicate. */
5852 FOR_EACH_SSA_TREE_OPERAND (op
, last
, iter
, SSA_OP_USE
)
5853 register_edge_assert_for (op
, e
, bsi
,
5854 gimple_cond_code (last
),
5855 gimple_cond_lhs (last
),
5856 gimple_cond_rhs (last
));
5866 /* Compare two case labels sorting first by the destination bb index
5867 and then by the case value. */
5870 compare_case_labels (const void *p1
, const void *p2
)
5872 const struct case_info
*ci1
= (const struct case_info
*) p1
;
5873 const struct case_info
*ci2
= (const struct case_info
*) p2
;
5874 int idx1
= ci1
->bb
->index
;
5875 int idx2
= ci2
->bb
->index
;
5879 else if (idx1
== idx2
)
5881 /* Make sure the default label is first in a group. */
5882 if (!CASE_LOW (ci1
->expr
))
5884 else if (!CASE_LOW (ci2
->expr
))
5887 return tree_int_cst_compare (CASE_LOW (ci1
->expr
),
5888 CASE_LOW (ci2
->expr
));
5894 /* Determine whether the outgoing edges of BB should receive an
5895 ASSERT_EXPR for each of the operands of BB's LAST statement.
5896 The last statement of BB must be a SWITCH_EXPR.
5898 If any of the sub-graphs rooted at BB have an interesting use of
5899 the predicate operands, an assert location node is added to the
5900 list of assertions for the corresponding operands. */
5903 find_switch_asserts (basic_block bb
, gswitch
*last
)
5905 gimple_stmt_iterator bsi
;
5908 struct case_info
*ci
;
5909 size_t n
= gimple_switch_num_labels (last
);
5910 #if GCC_VERSION >= 4000
5913 /* Work around GCC 3.4 bug (PR 37086). */
5914 volatile unsigned int idx
;
5917 bsi
= gsi_for_stmt (last
);
5918 op
= gimple_switch_index (last
);
5919 if (TREE_CODE (op
) != SSA_NAME
)
5922 /* Build a vector of case labels sorted by destination label. */
5923 ci
= XNEWVEC (struct case_info
, n
);
5924 for (idx
= 0; idx
< n
; ++idx
)
5926 ci
[idx
].expr
= gimple_switch_label (last
, idx
);
5927 ci
[idx
].bb
= label_to_block (CASE_LABEL (ci
[idx
].expr
));
5929 qsort (ci
, n
, sizeof (struct case_info
), compare_case_labels
);
5931 for (idx
= 0; idx
< n
; ++idx
)
5934 tree cl
= ci
[idx
].expr
;
5935 basic_block cbb
= ci
[idx
].bb
;
5937 min
= CASE_LOW (cl
);
5938 max
= CASE_HIGH (cl
);
5940 /* If there are multiple case labels with the same destination
5941 we need to combine them to a single value range for the edge. */
5942 if (idx
+ 1 < n
&& cbb
== ci
[idx
+ 1].bb
)
5944 /* Skip labels until the last of the group. */
5947 } while (idx
< n
&& cbb
== ci
[idx
].bb
);
5950 /* Pick up the maximum of the case label range. */
5951 if (CASE_HIGH (ci
[idx
].expr
))
5952 max
= CASE_HIGH (ci
[idx
].expr
);
5954 max
= CASE_LOW (ci
[idx
].expr
);
5957 /* Nothing to do if the range includes the default label until we
5958 can register anti-ranges. */
5959 if (min
== NULL_TREE
)
5962 /* Find the edge to register the assert expr on. */
5963 e
= find_edge (bb
, cbb
);
5965 /* Register the necessary assertions for the operand in the
5967 register_edge_assert_for (op
, e
, bsi
,
5968 max
? GE_EXPR
: EQ_EXPR
,
5969 op
, fold_convert (TREE_TYPE (op
), min
));
5971 register_edge_assert_for (op
, e
, bsi
, LE_EXPR
, op
,
5972 fold_convert (TREE_TYPE (op
), max
));
5979 /* Traverse all the statements in block BB looking for statements that
5980 may generate useful assertions for the SSA names in their operand.
5981 If a statement produces a useful assertion A for name N_i, then the
5982 list of assertions already generated for N_i is scanned to
5983 determine if A is actually needed.
5985 If N_i already had the assertion A at a location dominating the
5986 current location, then nothing needs to be done. Otherwise, the
5987 new location for A is recorded instead.
5989 1- For every statement S in BB, all the variables used by S are
5990 added to bitmap FOUND_IN_SUBGRAPH.
5992 2- If statement S uses an operand N in a way that exposes a known
5993 value range for N, then if N was not already generated by an
5994 ASSERT_EXPR, create a new assert location for N. For instance,
5995 if N is a pointer and the statement dereferences it, we can
5996 assume that N is not NULL.
5998 3- COND_EXPRs are a special case of #2. We can derive range
5999 information from the predicate but need to insert different
6000 ASSERT_EXPRs for each of the sub-graphs rooted at the
6001 conditional block. If the last statement of BB is a conditional
6002 expression of the form 'X op Y', then
6004 a) Remove X and Y from the set FOUND_IN_SUBGRAPH.
6006 b) If the conditional is the only entry point to the sub-graph
6007 corresponding to the THEN_CLAUSE, recurse into it. On
6008 return, if X and/or Y are marked in FOUND_IN_SUBGRAPH, then
6009 an ASSERT_EXPR is added for the corresponding variable.
6011 c) Repeat step (b) on the ELSE_CLAUSE.
6013 d) Mark X and Y in FOUND_IN_SUBGRAPH.
6022 In this case, an assertion on the THEN clause is useful to
6023 determine that 'a' is always 9 on that edge. However, an assertion
6024 on the ELSE clause would be unnecessary.
6026 4- If BB does not end in a conditional expression, then we recurse
6027 into BB's dominator children.
6029 At the end of the recursive traversal, every SSA name will have a
6030 list of locations where ASSERT_EXPRs should be added. When a new
6031 location for name N is found, it is registered by calling
6032 register_new_assert_for. That function keeps track of all the
6033 registered assertions to prevent adding unnecessary assertions.
6034 For instance, if a pointer P_4 is dereferenced more than once in a
6035 dominator tree, only the location dominating all the dereference of
6036 P_4 will receive an ASSERT_EXPR. */
6039 find_assert_locations_1 (basic_block bb
, sbitmap live
)
6043 last
= last_stmt (bb
);
6045 /* If BB's last statement is a conditional statement involving integer
6046 operands, determine if we need to add ASSERT_EXPRs. */
6048 && gimple_code (last
) == GIMPLE_COND
6049 && !fp_predicate (last
)
6050 && !ZERO_SSA_OPERANDS (last
, SSA_OP_USE
))
6051 find_conditional_asserts (bb
, as_a
<gcond
*> (last
));
6053 /* If BB's last statement is a switch statement involving integer
6054 operands, determine if we need to add ASSERT_EXPRs. */
6056 && gimple_code (last
) == GIMPLE_SWITCH
6057 && !ZERO_SSA_OPERANDS (last
, SSA_OP_USE
))
6058 find_switch_asserts (bb
, as_a
<gswitch
*> (last
));
6060 /* Traverse all the statements in BB marking used names and looking
6061 for statements that may infer assertions for their used operands. */
6062 for (gimple_stmt_iterator si
= gsi_last_bb (bb
); !gsi_end_p (si
);
6069 stmt
= gsi_stmt (si
);
6071 if (is_gimple_debug (stmt
))
6074 /* See if we can derive an assertion for any of STMT's operands. */
6075 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, i
, SSA_OP_USE
)
6078 enum tree_code comp_code
;
6080 /* If op is not live beyond this stmt, do not bother to insert
6082 if (!bitmap_bit_p (live
, SSA_NAME_VERSION (op
)))
6085 /* If OP is used in such a way that we can infer a value
6086 range for it, and we don't find a previous assertion for
6087 it, create a new assertion location node for OP. */
6088 if (infer_value_range (stmt
, op
, &comp_code
, &value
))
6090 /* If we are able to infer a nonzero value range for OP,
6091 then walk backwards through the use-def chain to see if OP
6092 was set via a typecast.
6094 If so, then we can also infer a nonzero value range
6095 for the operand of the NOP_EXPR. */
6096 if (comp_code
== NE_EXPR
&& integer_zerop (value
))
6099 gimple
*def_stmt
= SSA_NAME_DEF_STMT (t
);
6101 while (is_gimple_assign (def_stmt
)
6102 && CONVERT_EXPR_CODE_P
6103 (gimple_assign_rhs_code (def_stmt
))
6105 (gimple_assign_rhs1 (def_stmt
)) == SSA_NAME
6107 (TREE_TYPE (gimple_assign_rhs1 (def_stmt
))))
6109 t
= gimple_assign_rhs1 (def_stmt
);
6110 def_stmt
= SSA_NAME_DEF_STMT (t
);
6112 /* Note we want to register the assert for the
6113 operand of the NOP_EXPR after SI, not after the
6115 if (! has_single_use (t
))
6116 register_new_assert_for (t
, t
, comp_code
, value
,
6121 register_new_assert_for (op
, op
, comp_code
, value
, bb
, NULL
, si
);
6126 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, i
, SSA_OP_USE
)
6127 bitmap_set_bit (live
, SSA_NAME_VERSION (op
));
6128 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, i
, SSA_OP_DEF
)
6129 bitmap_clear_bit (live
, SSA_NAME_VERSION (op
));
6132 /* Traverse all PHI nodes in BB, updating live. */
6133 for (gphi_iterator si
= gsi_start_phis (bb
); !gsi_end_p (si
);
6136 use_operand_p arg_p
;
6138 gphi
*phi
= si
.phi ();
6139 tree res
= gimple_phi_result (phi
);
6141 if (virtual_operand_p (res
))
6144 FOR_EACH_PHI_ARG (arg_p
, phi
, i
, SSA_OP_USE
)
6146 tree arg
= USE_FROM_PTR (arg_p
);
6147 if (TREE_CODE (arg
) == SSA_NAME
)
6148 bitmap_set_bit (live
, SSA_NAME_VERSION (arg
));
6151 bitmap_clear_bit (live
, SSA_NAME_VERSION (res
));
6155 /* Do an RPO walk over the function computing SSA name liveness
6156 on-the-fly and deciding on assert expressions to insert. */
6159 find_assert_locations (void)
6161 int *rpo
= XNEWVEC (int, last_basic_block_for_fn (cfun
));
6162 int *bb_rpo
= XNEWVEC (int, last_basic_block_for_fn (cfun
));
6163 int *last_rpo
= XCNEWVEC (int, last_basic_block_for_fn (cfun
));
6166 live
= XCNEWVEC (sbitmap
, last_basic_block_for_fn (cfun
));
6167 rpo_cnt
= pre_and_rev_post_order_compute (NULL
, rpo
, false);
6168 for (i
= 0; i
< rpo_cnt
; ++i
)
6171 /* Pre-seed loop latch liveness from loop header PHI nodes. Due to
6172 the order we compute liveness and insert asserts we otherwise
6173 fail to insert asserts into the loop latch. */
6175 FOR_EACH_LOOP (loop
, 0)
6177 i
= loop
->latch
->index
;
6178 unsigned int j
= single_succ_edge (loop
->latch
)->dest_idx
;
6179 for (gphi_iterator gsi
= gsi_start_phis (loop
->header
);
6180 !gsi_end_p (gsi
); gsi_next (&gsi
))
6182 gphi
*phi
= gsi
.phi ();
6183 if (virtual_operand_p (gimple_phi_result (phi
)))
6185 tree arg
= gimple_phi_arg_def (phi
, j
);
6186 if (TREE_CODE (arg
) == SSA_NAME
)
6188 if (live
[i
] == NULL
)
6190 live
[i
] = sbitmap_alloc (num_ssa_names
);
6191 bitmap_clear (live
[i
]);
6193 bitmap_set_bit (live
[i
], SSA_NAME_VERSION (arg
));
6198 for (i
= rpo_cnt
- 1; i
>= 0; --i
)
6200 basic_block bb
= BASIC_BLOCK_FOR_FN (cfun
, rpo
[i
]);
6206 live
[rpo
[i
]] = sbitmap_alloc (num_ssa_names
);
6207 bitmap_clear (live
[rpo
[i
]]);
6210 /* Process BB and update the live information with uses in
6212 find_assert_locations_1 (bb
, live
[rpo
[i
]]);
6214 /* Merge liveness into the predecessor blocks and free it. */
6215 if (!bitmap_empty_p (live
[rpo
[i
]]))
6218 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
6220 int pred
= e
->src
->index
;
6221 if ((e
->flags
& EDGE_DFS_BACK
) || pred
== ENTRY_BLOCK
)
6226 live
[pred
] = sbitmap_alloc (num_ssa_names
);
6227 bitmap_clear (live
[pred
]);
6229 bitmap_ior (live
[pred
], live
[pred
], live
[rpo
[i
]]);
6231 if (bb_rpo
[pred
] < pred_rpo
)
6232 pred_rpo
= bb_rpo
[pred
];
6235 /* Record the RPO number of the last visited block that needs
6236 live information from this block. */
6237 last_rpo
[rpo
[i
]] = pred_rpo
;
6241 sbitmap_free (live
[rpo
[i
]]);
6242 live
[rpo
[i
]] = NULL
;
6245 /* We can free all successors live bitmaps if all their
6246 predecessors have been visited already. */
6247 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
6248 if (last_rpo
[e
->dest
->index
] == i
6249 && live
[e
->dest
->index
])
6251 sbitmap_free (live
[e
->dest
->index
]);
6252 live
[e
->dest
->index
] = NULL
;
6257 XDELETEVEC (bb_rpo
);
6258 XDELETEVEC (last_rpo
);
6259 for (i
= 0; i
< last_basic_block_for_fn (cfun
); ++i
)
6261 sbitmap_free (live
[i
]);
6265 /* Create an ASSERT_EXPR for NAME and insert it in the location
6266 indicated by LOC. Return true if we made any edge insertions. */
6269 process_assert_insertions_for (tree name
, assert_locus
*loc
)
6271 /* Build the comparison expression NAME_i COMP_CODE VAL. */
6274 gimple
*assert_stmt
;
6278 /* If we have X <=> X do not insert an assert expr for that. */
6279 if (loc
->expr
== loc
->val
)
6282 cond
= build2 (loc
->comp_code
, boolean_type_node
, loc
->expr
, loc
->val
);
6283 assert_stmt
= build_assert_expr_for (cond
, name
);
6286 /* We have been asked to insert the assertion on an edge. This
6287 is used only by COND_EXPR and SWITCH_EXPR assertions. */
6288 gcc_checking_assert (gimple_code (gsi_stmt (loc
->si
)) == GIMPLE_COND
6289 || (gimple_code (gsi_stmt (loc
->si
))
6292 gsi_insert_on_edge (loc
->e
, assert_stmt
);
6296 /* Otherwise, we can insert right after LOC->SI iff the
6297 statement must not be the last statement in the block. */
6298 stmt
= gsi_stmt (loc
->si
);
6299 if (!stmt_ends_bb_p (stmt
))
6301 gsi_insert_after (&loc
->si
, assert_stmt
, GSI_SAME_STMT
);
6305 /* If STMT must be the last statement in BB, we can only insert new
6306 assertions on the non-abnormal edge out of BB. Note that since
6307 STMT is not control flow, there may only be one non-abnormal edge
6309 FOR_EACH_EDGE (e
, ei
, loc
->bb
->succs
)
6310 if (!(e
->flags
& EDGE_ABNORMAL
))
6312 gsi_insert_on_edge (e
, assert_stmt
);
6320 /* Process all the insertions registered for every name N_i registered
6321 in NEED_ASSERT_FOR. The list of assertions to be inserted are
6322 found in ASSERTS_FOR[i]. */
6325 process_assert_insertions (void)
6329 bool update_edges_p
= false;
6330 int num_asserts
= 0;
6332 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6333 dump_all_asserts (dump_file
);
6335 EXECUTE_IF_SET_IN_BITMAP (need_assert_for
, 0, i
, bi
)
6337 assert_locus
*loc
= asserts_for
[i
];
6342 assert_locus
*next
= loc
->next
;
6343 update_edges_p
|= process_assert_insertions_for (ssa_name (i
), loc
);
6351 gsi_commit_edge_inserts ();
6353 statistics_counter_event (cfun
, "Number of ASSERT_EXPR expressions inserted",
6358 /* Traverse the flowgraph looking for conditional jumps to insert range
6359 expressions. These range expressions are meant to provide information
6360 to optimizations that need to reason in terms of value ranges. They
6361 will not be expanded into RTL. For instance, given:
6370 this pass will transform the code into:
6376 x = ASSERT_EXPR <x, x < y>
6381 y = ASSERT_EXPR <y, x >= y>
6385 The idea is that once copy and constant propagation have run, other
6386 optimizations will be able to determine what ranges of values can 'x'
6387 take in different paths of the code, simply by checking the reaching
6388 definition of 'x'. */
6391 insert_range_assertions (void)
6393 need_assert_for
= BITMAP_ALLOC (NULL
);
6394 asserts_for
= XCNEWVEC (assert_locus
*, num_ssa_names
);
6396 calculate_dominance_info (CDI_DOMINATORS
);
6398 find_assert_locations ();
6399 if (!bitmap_empty_p (need_assert_for
))
6401 process_assert_insertions ();
6402 update_ssa (TODO_update_ssa_no_phi
);
6405 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6407 fprintf (dump_file
, "\nSSA form after inserting ASSERT_EXPRs\n");
6408 dump_function_to_file (current_function_decl
, dump_file
, dump_flags
);
6412 BITMAP_FREE (need_assert_for
);
6415 /* Checks one ARRAY_REF in REF, located at LOCUS. Ignores flexible arrays
6416 and "struct" hacks. If VRP can determine that the
6417 array subscript is a constant, check if it is outside valid
6418 range. If the array subscript is a RANGE, warn if it is
6419 non-overlapping with valid range.
6420 IGNORE_OFF_BY_ONE is true if the ARRAY_REF is inside a ADDR_EXPR. */
6423 check_array_ref (location_t location
, tree ref
, bool ignore_off_by_one
)
6425 value_range
*vr
= NULL
;
6426 tree low_sub
, up_sub
;
6427 tree low_bound
, up_bound
, up_bound_p1
;
6430 if (TREE_NO_WARNING (ref
))
6433 low_sub
= up_sub
= TREE_OPERAND (ref
, 1);
6434 up_bound
= array_ref_up_bound (ref
);
6436 /* Can not check flexible arrays. */
6438 || TREE_CODE (up_bound
) != INTEGER_CST
)
6441 /* Accesses to trailing arrays via pointers may access storage
6442 beyond the types array bounds. */
6443 base
= get_base_address (ref
);
6444 if ((warn_array_bounds
< 2)
6445 && base
&& TREE_CODE (base
) == MEM_REF
)
6447 tree cref
, next
= NULL_TREE
;
6449 if (TREE_CODE (TREE_OPERAND (ref
, 0)) != COMPONENT_REF
)
6452 cref
= TREE_OPERAND (ref
, 0);
6453 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (cref
, 0))) == RECORD_TYPE
)
6454 for (next
= DECL_CHAIN (TREE_OPERAND (cref
, 1));
6455 next
&& TREE_CODE (next
) != FIELD_DECL
;
6456 next
= DECL_CHAIN (next
))
6459 /* If this is the last field in a struct type or a field in a
6460 union type do not warn. */
6465 low_bound
= array_ref_low_bound (ref
);
6466 up_bound_p1
= int_const_binop (PLUS_EXPR
, up_bound
,
6467 build_int_cst (TREE_TYPE (up_bound
), 1));
6470 if (tree_int_cst_equal (low_bound
, up_bound_p1
))
6472 warning_at (location
, OPT_Warray_bounds
,
6473 "array subscript is above array bounds");
6474 TREE_NO_WARNING (ref
) = 1;
6477 if (TREE_CODE (low_sub
) == SSA_NAME
)
6479 vr
= get_value_range (low_sub
);
6480 if (vr
->type
== VR_RANGE
|| vr
->type
== VR_ANTI_RANGE
)
6482 low_sub
= vr
->type
== VR_RANGE
? vr
->max
: vr
->min
;
6483 up_sub
= vr
->type
== VR_RANGE
? vr
->min
: vr
->max
;
6487 if (vr
&& vr
->type
== VR_ANTI_RANGE
)
6489 if (TREE_CODE (up_sub
) == INTEGER_CST
6490 && (ignore_off_by_one
6491 ? tree_int_cst_lt (up_bound
, up_sub
)
6492 : tree_int_cst_le (up_bound
, up_sub
))
6493 && TREE_CODE (low_sub
) == INTEGER_CST
6494 && tree_int_cst_le (low_sub
, low_bound
))
6496 warning_at (location
, OPT_Warray_bounds
,
6497 "array subscript is outside array bounds");
6498 TREE_NO_WARNING (ref
) = 1;
6501 else if (TREE_CODE (up_sub
) == INTEGER_CST
6502 && (ignore_off_by_one
6503 ? !tree_int_cst_le (up_sub
, up_bound_p1
)
6504 : !tree_int_cst_le (up_sub
, up_bound
)))
6506 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6508 fprintf (dump_file
, "Array bound warning for ");
6509 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, ref
);
6510 fprintf (dump_file
, "\n");
6512 warning_at (location
, OPT_Warray_bounds
,
6513 "array subscript is above array bounds");
6514 TREE_NO_WARNING (ref
) = 1;
6516 else if (TREE_CODE (low_sub
) == INTEGER_CST
6517 && tree_int_cst_lt (low_sub
, low_bound
))
6519 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6521 fprintf (dump_file
, "Array bound warning for ");
6522 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, ref
);
6523 fprintf (dump_file
, "\n");
6525 warning_at (location
, OPT_Warray_bounds
,
6526 "array subscript is below array bounds");
6527 TREE_NO_WARNING (ref
) = 1;
6531 /* Searches if the expr T, located at LOCATION computes
6532 address of an ARRAY_REF, and call check_array_ref on it. */
6535 search_for_addr_array (tree t
, location_t location
)
6537 /* Check each ARRAY_REFs in the reference chain. */
6540 if (TREE_CODE (t
) == ARRAY_REF
)
6541 check_array_ref (location
, t
, true /*ignore_off_by_one*/);
6543 t
= TREE_OPERAND (t
, 0);
6545 while (handled_component_p (t
));
6547 if (TREE_CODE (t
) == MEM_REF
6548 && TREE_CODE (TREE_OPERAND (t
, 0)) == ADDR_EXPR
6549 && !TREE_NO_WARNING (t
))
6551 tree tem
= TREE_OPERAND (TREE_OPERAND (t
, 0), 0);
6552 tree low_bound
, up_bound
, el_sz
;
6554 if (TREE_CODE (TREE_TYPE (tem
)) != ARRAY_TYPE
6555 || TREE_CODE (TREE_TYPE (TREE_TYPE (tem
))) == ARRAY_TYPE
6556 || !TYPE_DOMAIN (TREE_TYPE (tem
)))
6559 low_bound
= TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (tem
)));
6560 up_bound
= TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (tem
)));
6561 el_sz
= TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (tem
)));
6563 || TREE_CODE (low_bound
) != INTEGER_CST
6565 || TREE_CODE (up_bound
) != INTEGER_CST
6567 || TREE_CODE (el_sz
) != INTEGER_CST
)
6570 idx
= mem_ref_offset (t
);
6571 idx
= wi::sdiv_trunc (idx
, wi::to_offset (el_sz
));
6572 if (wi::lts_p (idx
, 0))
6574 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6576 fprintf (dump_file
, "Array bound warning for ");
6577 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, t
);
6578 fprintf (dump_file
, "\n");
6580 warning_at (location
, OPT_Warray_bounds
,
6581 "array subscript is below array bounds");
6582 TREE_NO_WARNING (t
) = 1;
6584 else if (wi::gts_p (idx
, (wi::to_offset (up_bound
)
6585 - wi::to_offset (low_bound
) + 1)))
6587 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6589 fprintf (dump_file
, "Array bound warning for ");
6590 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, t
);
6591 fprintf (dump_file
, "\n");
6593 warning_at (location
, OPT_Warray_bounds
,
6594 "array subscript is above array bounds");
6595 TREE_NO_WARNING (t
) = 1;
6600 /* walk_tree() callback that checks if *TP is
6601 an ARRAY_REF inside an ADDR_EXPR (in which an array
6602 subscript one outside the valid range is allowed). Call
6603 check_array_ref for each ARRAY_REF found. The location is
6607 check_array_bounds (tree
*tp
, int *walk_subtree
, void *data
)
6610 struct walk_stmt_info
*wi
= (struct walk_stmt_info
*) data
;
6611 location_t location
;
6613 if (EXPR_HAS_LOCATION (t
))
6614 location
= EXPR_LOCATION (t
);
6617 location_t
*locp
= (location_t
*) wi
->info
;
6621 *walk_subtree
= TRUE
;
6623 if (TREE_CODE (t
) == ARRAY_REF
)
6624 check_array_ref (location
, t
, false /*ignore_off_by_one*/);
6626 else if (TREE_CODE (t
) == ADDR_EXPR
)
6628 search_for_addr_array (t
, location
);
6629 *walk_subtree
= FALSE
;
6635 /* Walk over all statements of all reachable BBs and call check_array_bounds
6639 check_all_array_refs (void)
6642 gimple_stmt_iterator si
;
6644 FOR_EACH_BB_FN (bb
, cfun
)
6648 bool executable
= false;
6650 /* Skip blocks that were found to be unreachable. */
6651 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
6652 executable
|= !!(e
->flags
& EDGE_EXECUTABLE
);
6656 for (si
= gsi_start_bb (bb
); !gsi_end_p (si
); gsi_next (&si
))
6658 gimple
*stmt
= gsi_stmt (si
);
6659 struct walk_stmt_info wi
;
6660 if (!gimple_has_location (stmt
)
6661 || is_gimple_debug (stmt
))
6664 memset (&wi
, 0, sizeof (wi
));
6666 location_t loc
= gimple_location (stmt
);
6669 walk_gimple_op (gsi_stmt (si
),
6676 /* Return true if all imm uses of VAR are either in STMT, or
6677 feed (optionally through a chain of single imm uses) GIMPLE_COND
6678 in basic block COND_BB. */
6681 all_imm_uses_in_stmt_or_feed_cond (tree var
, gimple
*stmt
, basic_block cond_bb
)
6683 use_operand_p use_p
, use2_p
;
6684 imm_use_iterator iter
;
6686 FOR_EACH_IMM_USE_FAST (use_p
, iter
, var
)
6687 if (USE_STMT (use_p
) != stmt
)
6689 gimple
*use_stmt
= USE_STMT (use_p
), *use_stmt2
;
6690 if (is_gimple_debug (use_stmt
))
6692 while (is_gimple_assign (use_stmt
)
6693 && TREE_CODE (gimple_assign_lhs (use_stmt
)) == SSA_NAME
6694 && single_imm_use (gimple_assign_lhs (use_stmt
),
6695 &use2_p
, &use_stmt2
))
6696 use_stmt
= use_stmt2
;
6697 if (gimple_code (use_stmt
) != GIMPLE_COND
6698 || gimple_bb (use_stmt
) != cond_bb
)
6711 __builtin_unreachable ();
6713 x_5 = ASSERT_EXPR <x_3, ...>;
6714 If x_3 has no other immediate uses (checked by caller),
6715 var is the x_3 var from ASSERT_EXPR, we can clear low 5 bits
6716 from the non-zero bitmask. */
6719 maybe_set_nonzero_bits (basic_block bb
, tree var
)
6721 edge e
= single_pred_edge (bb
);
6722 basic_block cond_bb
= e
->src
;
6723 gimple
*stmt
= last_stmt (cond_bb
);
6727 || gimple_code (stmt
) != GIMPLE_COND
6728 || gimple_cond_code (stmt
) != ((e
->flags
& EDGE_TRUE_VALUE
)
6729 ? EQ_EXPR
: NE_EXPR
)
6730 || TREE_CODE (gimple_cond_lhs (stmt
)) != SSA_NAME
6731 || !integer_zerop (gimple_cond_rhs (stmt
)))
6734 stmt
= SSA_NAME_DEF_STMT (gimple_cond_lhs (stmt
));
6735 if (!is_gimple_assign (stmt
)
6736 || gimple_assign_rhs_code (stmt
) != BIT_AND_EXPR
6737 || TREE_CODE (gimple_assign_rhs2 (stmt
)) != INTEGER_CST
)
6739 if (gimple_assign_rhs1 (stmt
) != var
)
6743 if (TREE_CODE (gimple_assign_rhs1 (stmt
)) != SSA_NAME
)
6745 stmt2
= SSA_NAME_DEF_STMT (gimple_assign_rhs1 (stmt
));
6746 if (!gimple_assign_cast_p (stmt2
)
6747 || gimple_assign_rhs1 (stmt2
) != var
6748 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (stmt2
))
6749 || (TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (stmt
)))
6750 != TYPE_PRECISION (TREE_TYPE (var
))))
6753 cst
= gimple_assign_rhs2 (stmt
);
6754 set_nonzero_bits (var
, wi::bit_and_not (get_nonzero_bits (var
), cst
));
6757 /* Convert range assertion expressions into the implied copies and
6758 copy propagate away the copies. Doing the trivial copy propagation
6759 here avoids the need to run the full copy propagation pass after
6762 FIXME, this will eventually lead to copy propagation removing the
6763 names that had useful range information attached to them. For
6764 instance, if we had the assertion N_i = ASSERT_EXPR <N_j, N_j > 3>,
6765 then N_i will have the range [3, +INF].
6767 However, by converting the assertion into the implied copy
6768 operation N_i = N_j, we will then copy-propagate N_j into the uses
6769 of N_i and lose the range information. We may want to hold on to
6770 ASSERT_EXPRs a little while longer as the ranges could be used in
6771 things like jump threading.
6773 The problem with keeping ASSERT_EXPRs around is that passes after
6774 VRP need to handle them appropriately.
6776 Another approach would be to make the range information a first
6777 class property of the SSA_NAME so that it can be queried from
6778 any pass. This is made somewhat more complex by the need for
6779 multiple ranges to be associated with one SSA_NAME. */
6782 remove_range_assertions (void)
6785 gimple_stmt_iterator si
;
6786 /* 1 if looking at ASSERT_EXPRs immediately at the beginning of
6787 a basic block preceeded by GIMPLE_COND branching to it and
6788 __builtin_trap, -1 if not yet checked, 0 otherwise. */
6791 /* Note that the BSI iterator bump happens at the bottom of the
6792 loop and no bump is necessary if we're removing the statement
6793 referenced by the current BSI. */
6794 FOR_EACH_BB_FN (bb
, cfun
)
6795 for (si
= gsi_after_labels (bb
), is_unreachable
= -1; !gsi_end_p (si
);)
6797 gimple
*stmt
= gsi_stmt (si
);
6800 if (is_gimple_assign (stmt
)
6801 && gimple_assign_rhs_code (stmt
) == ASSERT_EXPR
)
6803 tree lhs
= gimple_assign_lhs (stmt
);
6804 tree rhs
= gimple_assign_rhs1 (stmt
);
6806 use_operand_p use_p
;
6807 imm_use_iterator iter
;
6809 var
= ASSERT_EXPR_VAR (rhs
);
6810 gcc_assert (TREE_CODE (var
) == SSA_NAME
);
6812 if (!POINTER_TYPE_P (TREE_TYPE (lhs
))
6813 && SSA_NAME_RANGE_INFO (lhs
))
6815 if (is_unreachable
== -1)
6818 if (single_pred_p (bb
)
6819 && assert_unreachable_fallthru_edge_p
6820 (single_pred_edge (bb
)))
6824 if (x_7 >= 10 && x_7 < 20)
6825 __builtin_unreachable ();
6826 x_8 = ASSERT_EXPR <x_7, ...>;
6827 if the only uses of x_7 are in the ASSERT_EXPR and
6828 in the condition. In that case, we can copy the
6829 range info from x_8 computed in this pass also
6832 && all_imm_uses_in_stmt_or_feed_cond (var
, stmt
,
6835 set_range_info (var
, SSA_NAME_RANGE_TYPE (lhs
),
6836 SSA_NAME_RANGE_INFO (lhs
)->get_min (),
6837 SSA_NAME_RANGE_INFO (lhs
)->get_max ());
6838 maybe_set_nonzero_bits (bb
, var
);
6842 /* Propagate the RHS into every use of the LHS. */
6843 FOR_EACH_IMM_USE_STMT (use_stmt
, iter
, lhs
)
6844 FOR_EACH_IMM_USE_ON_STMT (use_p
, iter
)
6845 SET_USE (use_p
, var
);
6847 /* And finally, remove the copy, it is not needed. */
6848 gsi_remove (&si
, true);
6849 release_defs (stmt
);
6853 if (!is_gimple_debug (gsi_stmt (si
)))
6861 /* Return true if STMT is interesting for VRP. */
6864 stmt_interesting_for_vrp (gimple
*stmt
)
6866 if (gimple_code (stmt
) == GIMPLE_PHI
)
6868 tree res
= gimple_phi_result (stmt
);
6869 return (!virtual_operand_p (res
)
6870 && (INTEGRAL_TYPE_P (TREE_TYPE (res
))
6871 || POINTER_TYPE_P (TREE_TYPE (res
))));
6873 else if (is_gimple_assign (stmt
) || is_gimple_call (stmt
))
6875 tree lhs
= gimple_get_lhs (stmt
);
6877 /* In general, assignments with virtual operands are not useful
6878 for deriving ranges, with the obvious exception of calls to
6879 builtin functions. */
6880 if (lhs
&& TREE_CODE (lhs
) == SSA_NAME
6881 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs
))
6882 || POINTER_TYPE_P (TREE_TYPE (lhs
)))
6883 && (is_gimple_call (stmt
)
6884 || !gimple_vuse (stmt
)))
6886 else if (is_gimple_call (stmt
) && gimple_call_internal_p (stmt
))
6887 switch (gimple_call_internal_fn (stmt
))
6889 case IFN_ADD_OVERFLOW
:
6890 case IFN_SUB_OVERFLOW
:
6891 case IFN_MUL_OVERFLOW
:
6892 /* These internal calls return _Complex integer type,
6893 but are interesting to VRP nevertheless. */
6894 if (lhs
&& TREE_CODE (lhs
) == SSA_NAME
)
6901 else if (gimple_code (stmt
) == GIMPLE_COND
6902 || gimple_code (stmt
) == GIMPLE_SWITCH
)
6909 /* Initialize local data structures for VRP. */
6912 vrp_initialize (void)
6916 values_propagated
= false;
6917 num_vr_values
= num_ssa_names
;
6918 vr_value
= XCNEWVEC (value_range
*, num_vr_values
);
6919 vr_phi_edge_counts
= XCNEWVEC (int, num_ssa_names
);
6921 FOR_EACH_BB_FN (bb
, cfun
)
6923 for (gphi_iterator si
= gsi_start_phis (bb
); !gsi_end_p (si
);
6926 gphi
*phi
= si
.phi ();
6927 if (!stmt_interesting_for_vrp (phi
))
6929 tree lhs
= PHI_RESULT (phi
);
6930 set_value_range_to_varying (get_value_range (lhs
));
6931 prop_set_simulate_again (phi
, false);
6934 prop_set_simulate_again (phi
, true);
6937 for (gimple_stmt_iterator si
= gsi_start_bb (bb
); !gsi_end_p (si
);
6940 gimple
*stmt
= gsi_stmt (si
);
6942 /* If the statement is a control insn, then we do not
6943 want to avoid simulating the statement once. Failure
6944 to do so means that those edges will never get added. */
6945 if (stmt_ends_bb_p (stmt
))
6946 prop_set_simulate_again (stmt
, true);
6947 else if (!stmt_interesting_for_vrp (stmt
))
6951 FOR_EACH_SSA_TREE_OPERAND (def
, stmt
, i
, SSA_OP_DEF
)
6952 set_value_range_to_varying (get_value_range (def
));
6953 prop_set_simulate_again (stmt
, false);
6956 prop_set_simulate_again (stmt
, true);
6961 /* Return the singleton value-range for NAME or NAME. */
6964 vrp_valueize (tree name
)
6966 if (TREE_CODE (name
) == SSA_NAME
)
6968 value_range
*vr
= get_value_range (name
);
6969 if (vr
->type
== VR_RANGE
6970 && (vr
->min
== vr
->max
6971 || operand_equal_p (vr
->min
, vr
->max
, 0)))
6977 /* Return the singleton value-range for NAME if that is a constant
6978 but signal to not follow SSA edges. */
6981 vrp_valueize_1 (tree name
)
6983 if (TREE_CODE (name
) == SSA_NAME
)
6985 /* If the definition may be simulated again we cannot follow
6986 this SSA edge as the SSA propagator does not necessarily
6987 re-visit the use. */
6988 gimple
*def_stmt
= SSA_NAME_DEF_STMT (name
);
6989 if (!gimple_nop_p (def_stmt
)
6990 && prop_simulate_again_p (def_stmt
))
6992 value_range
*vr
= get_value_range (name
);
6993 if (range_int_cst_singleton_p (vr
))
6999 /* Visit assignment STMT. If it produces an interesting range, record
7000 the SSA name in *OUTPUT_P. */
7002 static enum ssa_prop_result
7003 vrp_visit_assignment_or_call (gimple
*stmt
, tree
*output_p
)
7007 enum gimple_code code
= gimple_code (stmt
);
7008 lhs
= gimple_get_lhs (stmt
);
7010 /* We only keep track of ranges in integral and pointer types. */
7011 if (TREE_CODE (lhs
) == SSA_NAME
7012 && ((INTEGRAL_TYPE_P (TREE_TYPE (lhs
))
7013 /* It is valid to have NULL MIN/MAX values on a type. See
7014 build_range_type. */
7015 && TYPE_MIN_VALUE (TREE_TYPE (lhs
))
7016 && TYPE_MAX_VALUE (TREE_TYPE (lhs
)))
7017 || POINTER_TYPE_P (TREE_TYPE (lhs
))))
7019 value_range new_vr
= VR_INITIALIZER
;
7021 /* Try folding the statement to a constant first. */
7022 tree tem
= gimple_fold_stmt_to_constant_1 (stmt
, vrp_valueize
,
7024 if (tem
&& is_gimple_min_invariant (tem
))
7025 set_value_range_to_value (&new_vr
, tem
, NULL
);
7026 /* Then dispatch to value-range extracting functions. */
7027 else if (code
== GIMPLE_CALL
)
7028 extract_range_basic (&new_vr
, stmt
);
7030 extract_range_from_assignment (&new_vr
, as_a
<gassign
*> (stmt
));
7032 if (update_value_range (lhs
, &new_vr
))
7036 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7038 fprintf (dump_file
, "Found new range for ");
7039 print_generic_expr (dump_file
, lhs
, 0);
7040 fprintf (dump_file
, ": ");
7041 dump_value_range (dump_file
, &new_vr
);
7042 fprintf (dump_file
, "\n");
7045 if (new_vr
.type
== VR_VARYING
)
7046 return SSA_PROP_VARYING
;
7048 return SSA_PROP_INTERESTING
;
7051 return SSA_PROP_NOT_INTERESTING
;
7053 else if (is_gimple_call (stmt
) && gimple_call_internal_p (stmt
))
7054 switch (gimple_call_internal_fn (stmt
))
7056 case IFN_ADD_OVERFLOW
:
7057 case IFN_SUB_OVERFLOW
:
7058 case IFN_MUL_OVERFLOW
:
7059 /* These internal calls return _Complex integer type,
7060 which VRP does not track, but the immediate uses
7061 thereof might be interesting. */
7062 if (lhs
&& TREE_CODE (lhs
) == SSA_NAME
)
7064 imm_use_iterator iter
;
7065 use_operand_p use_p
;
7066 enum ssa_prop_result res
= SSA_PROP_VARYING
;
7068 set_value_range_to_varying (get_value_range (lhs
));
7070 FOR_EACH_IMM_USE_FAST (use_p
, iter
, lhs
)
7072 gimple
*use_stmt
= USE_STMT (use_p
);
7073 if (!is_gimple_assign (use_stmt
))
7075 enum tree_code rhs_code
= gimple_assign_rhs_code (use_stmt
);
7076 if (rhs_code
!= REALPART_EXPR
&& rhs_code
!= IMAGPART_EXPR
)
7078 tree rhs1
= gimple_assign_rhs1 (use_stmt
);
7079 tree use_lhs
= gimple_assign_lhs (use_stmt
);
7080 if (TREE_CODE (rhs1
) != rhs_code
7081 || TREE_OPERAND (rhs1
, 0) != lhs
7082 || TREE_CODE (use_lhs
) != SSA_NAME
7083 || !stmt_interesting_for_vrp (use_stmt
)
7084 || (!INTEGRAL_TYPE_P (TREE_TYPE (use_lhs
))
7085 || !TYPE_MIN_VALUE (TREE_TYPE (use_lhs
))
7086 || !TYPE_MAX_VALUE (TREE_TYPE (use_lhs
))))
7089 /* If there is a change in the value range for any of the
7090 REALPART_EXPR/IMAGPART_EXPR immediate uses, return
7091 SSA_PROP_INTERESTING. If there are any REALPART_EXPR
7092 or IMAGPART_EXPR immediate uses, but none of them have
7093 a change in their value ranges, return
7094 SSA_PROP_NOT_INTERESTING. If there are no
7095 {REAL,IMAG}PART_EXPR uses at all,
7096 return SSA_PROP_VARYING. */
7097 value_range new_vr
= VR_INITIALIZER
;
7098 extract_range_basic (&new_vr
, use_stmt
);
7099 value_range
*old_vr
= get_value_range (use_lhs
);
7100 if (old_vr
->type
!= new_vr
.type
7101 || !vrp_operand_equal_p (old_vr
->min
, new_vr
.min
)
7102 || !vrp_operand_equal_p (old_vr
->max
, new_vr
.max
)
7103 || !vrp_bitmap_equal_p (old_vr
->equiv
, new_vr
.equiv
))
7104 res
= SSA_PROP_INTERESTING
;
7106 res
= SSA_PROP_NOT_INTERESTING
;
7107 BITMAP_FREE (new_vr
.equiv
);
7108 if (res
== SSA_PROP_INTERESTING
)
7122 /* Every other statement produces no useful ranges. */
7123 FOR_EACH_SSA_TREE_OPERAND (def
, stmt
, iter
, SSA_OP_DEF
)
7124 set_value_range_to_varying (get_value_range (def
));
7126 return SSA_PROP_VARYING
;
7129 /* Helper that gets the value range of the SSA_NAME with version I
7130 or a symbolic range containing the SSA_NAME only if the value range
7131 is varying or undefined. */
7133 static inline value_range
7134 get_vr_for_comparison (int i
)
7136 value_range vr
= *get_value_range (ssa_name (i
));
7138 /* If name N_i does not have a valid range, use N_i as its own
7139 range. This allows us to compare against names that may
7140 have N_i in their ranges. */
7141 if (vr
.type
== VR_VARYING
|| vr
.type
== VR_UNDEFINED
)
7144 vr
.min
= ssa_name (i
);
7145 vr
.max
= ssa_name (i
);
7151 /* Compare all the value ranges for names equivalent to VAR with VAL
7152 using comparison code COMP. Return the same value returned by
7153 compare_range_with_value, including the setting of
7154 *STRICT_OVERFLOW_P. */
7157 compare_name_with_value (enum tree_code comp
, tree var
, tree val
,
7158 bool *strict_overflow_p
)
7164 int used_strict_overflow
;
7166 value_range equiv_vr
;
7168 /* Get the set of equivalences for VAR. */
7169 e
= get_value_range (var
)->equiv
;
7171 /* Start at -1. Set it to 0 if we do a comparison without relying
7172 on overflow, or 1 if all comparisons rely on overflow. */
7173 used_strict_overflow
= -1;
7175 /* Compare vars' value range with val. */
7176 equiv_vr
= get_vr_for_comparison (SSA_NAME_VERSION (var
));
7178 retval
= compare_range_with_value (comp
, &equiv_vr
, val
, &sop
);
7180 used_strict_overflow
= sop
? 1 : 0;
7182 /* If the equiv set is empty we have done all work we need to do. */
7186 && used_strict_overflow
> 0)
7187 *strict_overflow_p
= true;
7191 EXECUTE_IF_SET_IN_BITMAP (e
, 0, i
, bi
)
7193 equiv_vr
= get_vr_for_comparison (i
);
7195 t
= compare_range_with_value (comp
, &equiv_vr
, val
, &sop
);
7198 /* If we get different answers from different members
7199 of the equivalence set this check must be in a dead
7200 code region. Folding it to a trap representation
7201 would be correct here. For now just return don't-know. */
7211 used_strict_overflow
= 0;
7212 else if (used_strict_overflow
< 0)
7213 used_strict_overflow
= 1;
7218 && used_strict_overflow
> 0)
7219 *strict_overflow_p
= true;
7225 /* Given a comparison code COMP and names N1 and N2, compare all the
7226 ranges equivalent to N1 against all the ranges equivalent to N2
7227 to determine the value of N1 COMP N2. Return the same value
7228 returned by compare_ranges. Set *STRICT_OVERFLOW_P to indicate
7229 whether we relied on an overflow infinity in the comparison. */
7233 compare_names (enum tree_code comp
, tree n1
, tree n2
,
7234 bool *strict_overflow_p
)
7238 bitmap_iterator bi1
, bi2
;
7240 int used_strict_overflow
;
7241 static bitmap_obstack
*s_obstack
= NULL
;
7242 static bitmap s_e1
= NULL
, s_e2
= NULL
;
7244 /* Compare the ranges of every name equivalent to N1 against the
7245 ranges of every name equivalent to N2. */
7246 e1
= get_value_range (n1
)->equiv
;
7247 e2
= get_value_range (n2
)->equiv
;
7249 /* Use the fake bitmaps if e1 or e2 are not available. */
7250 if (s_obstack
== NULL
)
7252 s_obstack
= XNEW (bitmap_obstack
);
7253 bitmap_obstack_initialize (s_obstack
);
7254 s_e1
= BITMAP_ALLOC (s_obstack
);
7255 s_e2
= BITMAP_ALLOC (s_obstack
);
7262 /* Add N1 and N2 to their own set of equivalences to avoid
7263 duplicating the body of the loop just to check N1 and N2
7265 bitmap_set_bit (e1
, SSA_NAME_VERSION (n1
));
7266 bitmap_set_bit (e2
, SSA_NAME_VERSION (n2
));
7268 /* If the equivalence sets have a common intersection, then the two
7269 names can be compared without checking their ranges. */
7270 if (bitmap_intersect_p (e1
, e2
))
7272 bitmap_clear_bit (e1
, SSA_NAME_VERSION (n1
));
7273 bitmap_clear_bit (e2
, SSA_NAME_VERSION (n2
));
7275 return (comp
== EQ_EXPR
|| comp
== GE_EXPR
|| comp
== LE_EXPR
)
7277 : boolean_false_node
;
7280 /* Start at -1. Set it to 0 if we do a comparison without relying
7281 on overflow, or 1 if all comparisons rely on overflow. */
7282 used_strict_overflow
= -1;
7284 /* Otherwise, compare all the equivalent ranges. First, add N1 and
7285 N2 to their own set of equivalences to avoid duplicating the body
7286 of the loop just to check N1 and N2 ranges. */
7287 EXECUTE_IF_SET_IN_BITMAP (e1
, 0, i1
, bi1
)
7289 value_range vr1
= get_vr_for_comparison (i1
);
7291 t
= retval
= NULL_TREE
;
7292 EXECUTE_IF_SET_IN_BITMAP (e2
, 0, i2
, bi2
)
7296 value_range vr2
= get_vr_for_comparison (i2
);
7298 t
= compare_ranges (comp
, &vr1
, &vr2
, &sop
);
7301 /* If we get different answers from different members
7302 of the equivalence set this check must be in a dead
7303 code region. Folding it to a trap representation
7304 would be correct here. For now just return don't-know. */
7308 bitmap_clear_bit (e1
, SSA_NAME_VERSION (n1
));
7309 bitmap_clear_bit (e2
, SSA_NAME_VERSION (n2
));
7315 used_strict_overflow
= 0;
7316 else if (used_strict_overflow
< 0)
7317 used_strict_overflow
= 1;
7323 bitmap_clear_bit (e1
, SSA_NAME_VERSION (n1
));
7324 bitmap_clear_bit (e2
, SSA_NAME_VERSION (n2
));
7325 if (used_strict_overflow
> 0)
7326 *strict_overflow_p
= true;
7331 /* None of the equivalent ranges are useful in computing this
7333 bitmap_clear_bit (e1
, SSA_NAME_VERSION (n1
));
7334 bitmap_clear_bit (e2
, SSA_NAME_VERSION (n2
));
7338 /* Helper function for vrp_evaluate_conditional_warnv & other
7342 vrp_evaluate_conditional_warnv_with_ops_using_ranges (enum tree_code code
,
7344 bool * strict_overflow_p
)
7346 value_range
*vr0
, *vr1
;
7348 vr0
= (TREE_CODE (op0
) == SSA_NAME
) ? get_value_range (op0
) : NULL
;
7349 vr1
= (TREE_CODE (op1
) == SSA_NAME
) ? get_value_range (op1
) : NULL
;
7351 tree res
= NULL_TREE
;
7353 res
= compare_ranges (code
, vr0
, vr1
, strict_overflow_p
);
7355 res
= compare_range_with_value (code
, vr0
, op1
, strict_overflow_p
);
7357 res
= (compare_range_with_value
7358 (swap_tree_comparison (code
), vr1
, op0
, strict_overflow_p
));
7362 /* Helper function for vrp_evaluate_conditional_warnv. */
7365 vrp_evaluate_conditional_warnv_with_ops (enum tree_code code
, tree op0
,
7366 tree op1
, bool use_equiv_p
,
7367 bool *strict_overflow_p
, bool *only_ranges
)
7371 *only_ranges
= true;
7373 /* We only deal with integral and pointer types. */
7374 if (!INTEGRAL_TYPE_P (TREE_TYPE (op0
))
7375 && !POINTER_TYPE_P (TREE_TYPE (op0
)))
7381 && (ret
= vrp_evaluate_conditional_warnv_with_ops_using_ranges
7382 (code
, op0
, op1
, strict_overflow_p
)))
7384 *only_ranges
= false;
7385 if (TREE_CODE (op0
) == SSA_NAME
&& TREE_CODE (op1
) == SSA_NAME
)
7386 return compare_names (code
, op0
, op1
, strict_overflow_p
);
7387 else if (TREE_CODE (op0
) == SSA_NAME
)
7388 return compare_name_with_value (code
, op0
, op1
, strict_overflow_p
);
7389 else if (TREE_CODE (op1
) == SSA_NAME
)
7390 return (compare_name_with_value
7391 (swap_tree_comparison (code
), op1
, op0
, strict_overflow_p
));
7394 return vrp_evaluate_conditional_warnv_with_ops_using_ranges (code
, op0
, op1
,
7399 /* Given (CODE OP0 OP1) within STMT, try to simplify it based on value range
7400 information. Return NULL if the conditional can not be evaluated.
7401 The ranges of all the names equivalent with the operands in COND
7402 will be used when trying to compute the value. If the result is
7403 based on undefined signed overflow, issue a warning if
7407 vrp_evaluate_conditional (tree_code code
, tree op0
, tree op1
, gimple
*stmt
)
7413 /* Some passes and foldings leak constants with overflow flag set
7414 into the IL. Avoid doing wrong things with these and bail out. */
7415 if ((TREE_CODE (op0
) == INTEGER_CST
7416 && TREE_OVERFLOW (op0
))
7417 || (TREE_CODE (op1
) == INTEGER_CST
7418 && TREE_OVERFLOW (op1
)))
7422 ret
= vrp_evaluate_conditional_warnv_with_ops (code
, op0
, op1
, true, &sop
,
7427 enum warn_strict_overflow_code wc
;
7428 const char* warnmsg
;
7430 if (is_gimple_min_invariant (ret
))
7432 wc
= WARN_STRICT_OVERFLOW_CONDITIONAL
;
7433 warnmsg
= G_("assuming signed overflow does not occur when "
7434 "simplifying conditional to constant");
7438 wc
= WARN_STRICT_OVERFLOW_COMPARISON
;
7439 warnmsg
= G_("assuming signed overflow does not occur when "
7440 "simplifying conditional");
7443 if (issue_strict_overflow_warning (wc
))
7445 location_t location
;
7447 if (!gimple_has_location (stmt
))
7448 location
= input_location
;
7450 location
= gimple_location (stmt
);
7451 warning_at (location
, OPT_Wstrict_overflow
, "%s", warnmsg
);
7455 if (warn_type_limits
7456 && ret
&& only_ranges
7457 && TREE_CODE_CLASS (code
) == tcc_comparison
7458 && TREE_CODE (op0
) == SSA_NAME
)
7460 /* If the comparison is being folded and the operand on the LHS
7461 is being compared against a constant value that is outside of
7462 the natural range of OP0's type, then the predicate will
7463 always fold regardless of the value of OP0. If -Wtype-limits
7464 was specified, emit a warning. */
7465 tree type
= TREE_TYPE (op0
);
7466 value_range
*vr0
= get_value_range (op0
);
7468 if (vr0
->type
== VR_RANGE
7469 && INTEGRAL_TYPE_P (type
)
7470 && vrp_val_is_min (vr0
->min
)
7471 && vrp_val_is_max (vr0
->max
)
7472 && is_gimple_min_invariant (op1
))
7474 location_t location
;
7476 if (!gimple_has_location (stmt
))
7477 location
= input_location
;
7479 location
= gimple_location (stmt
);
7481 warning_at (location
, OPT_Wtype_limits
,
7483 ? G_("comparison always false "
7484 "due to limited range of data type")
7485 : G_("comparison always true "
7486 "due to limited range of data type"));
7494 /* Visit conditional statement STMT. If we can determine which edge
7495 will be taken out of STMT's basic block, record it in
7496 *TAKEN_EDGE_P and return SSA_PROP_INTERESTING. Otherwise, return
7497 SSA_PROP_VARYING. */
7499 static enum ssa_prop_result
7500 vrp_visit_cond_stmt (gcond
*stmt
, edge
*taken_edge_p
)
7505 *taken_edge_p
= NULL
;
7507 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7512 fprintf (dump_file
, "\nVisiting conditional with predicate: ");
7513 print_gimple_stmt (dump_file
, stmt
, 0, 0);
7514 fprintf (dump_file
, "\nWith known ranges\n");
7516 FOR_EACH_SSA_TREE_OPERAND (use
, stmt
, i
, SSA_OP_USE
)
7518 fprintf (dump_file
, "\t");
7519 print_generic_expr (dump_file
, use
, 0);
7520 fprintf (dump_file
, ": ");
7521 dump_value_range (dump_file
, vr_value
[SSA_NAME_VERSION (use
)]);
7524 fprintf (dump_file
, "\n");
7527 /* Compute the value of the predicate COND by checking the known
7528 ranges of each of its operands.
7530 Note that we cannot evaluate all the equivalent ranges here
7531 because those ranges may not yet be final and with the current
7532 propagation strategy, we cannot determine when the value ranges
7533 of the names in the equivalence set have changed.
7535 For instance, given the following code fragment
7539 i_14 = ASSERT_EXPR <i_5, i_5 != 0>
7543 Assume that on the first visit to i_14, i_5 has the temporary
7544 range [8, 8] because the second argument to the PHI function is
7545 not yet executable. We derive the range ~[0, 0] for i_14 and the
7546 equivalence set { i_5 }. So, when we visit 'if (i_14 == 1)' for
7547 the first time, since i_14 is equivalent to the range [8, 8], we
7548 determine that the predicate is always false.
7550 On the next round of propagation, i_13 is determined to be
7551 VARYING, which causes i_5 to drop down to VARYING. So, another
7552 visit to i_14 is scheduled. In this second visit, we compute the
7553 exact same range and equivalence set for i_14, namely ~[0, 0] and
7554 { i_5 }. But we did not have the previous range for i_5
7555 registered, so vrp_visit_assignment thinks that the range for
7556 i_14 has not changed. Therefore, the predicate 'if (i_14 == 1)'
7557 is not visited again, which stops propagation from visiting
7558 statements in the THEN clause of that if().
7560 To properly fix this we would need to keep the previous range
7561 value for the names in the equivalence set. This way we would've
7562 discovered that from one visit to the other i_5 changed from
7563 range [8, 8] to VR_VARYING.
7565 However, fixing this apparent limitation may not be worth the
7566 additional checking. Testing on several code bases (GCC, DLV,
7567 MICO, TRAMP3D and SPEC2000) showed that doing this results in
7568 4 more predicates folded in SPEC. */
7571 val
= vrp_evaluate_conditional_warnv_with_ops (gimple_cond_code (stmt
),
7572 gimple_cond_lhs (stmt
),
7573 gimple_cond_rhs (stmt
),
7578 *taken_edge_p
= find_taken_edge (gimple_bb (stmt
), val
);
7581 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7583 "\nIgnoring predicate evaluation because "
7584 "it assumes that signed overflow is undefined");
7589 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7591 fprintf (dump_file
, "\nPredicate evaluates to: ");
7592 if (val
== NULL_TREE
)
7593 fprintf (dump_file
, "DON'T KNOW\n");
7595 print_generic_stmt (dump_file
, val
, 0);
7598 return (*taken_edge_p
) ? SSA_PROP_INTERESTING
: SSA_PROP_VARYING
;
7601 /* Searches the case label vector VEC for the index *IDX of the CASE_LABEL
7602 that includes the value VAL. The search is restricted to the range
7603 [START_IDX, n - 1] where n is the size of VEC.
7605 If there is a CASE_LABEL for VAL, its index is placed in IDX and true is
7608 If there is no CASE_LABEL for VAL and there is one that is larger than VAL,
7609 it is placed in IDX and false is returned.
7611 If VAL is larger than any CASE_LABEL, n is placed on IDX and false is
7615 find_case_label_index (gswitch
*stmt
, size_t start_idx
, tree val
, size_t *idx
)
7617 size_t n
= gimple_switch_num_labels (stmt
);
7620 /* Find case label for minimum of the value range or the next one.
7621 At each iteration we are searching in [low, high - 1]. */
7623 for (low
= start_idx
, high
= n
; high
!= low
; )
7627 /* Note that i != high, so we never ask for n. */
7628 size_t i
= (high
+ low
) / 2;
7629 t
= gimple_switch_label (stmt
, i
);
7631 /* Cache the result of comparing CASE_LOW and val. */
7632 cmp
= tree_int_cst_compare (CASE_LOW (t
), val
);
7636 /* Ranges cannot be empty. */
7645 if (CASE_HIGH (t
) != NULL
7646 && tree_int_cst_compare (CASE_HIGH (t
), val
) >= 0)
7658 /* Searches the case label vector VEC for the range of CASE_LABELs that is used
7659 for values between MIN and MAX. The first index is placed in MIN_IDX. The
7660 last index is placed in MAX_IDX. If the range of CASE_LABELs is empty
7661 then MAX_IDX < MIN_IDX.
7662 Returns true if the default label is not needed. */
7665 find_case_label_range (gswitch
*stmt
, tree min
, tree max
, size_t *min_idx
,
7669 bool min_take_default
= !find_case_label_index (stmt
, 1, min
, &i
);
7670 bool max_take_default
= !find_case_label_index (stmt
, i
, max
, &j
);
7674 && max_take_default
)
7676 /* Only the default case label reached.
7677 Return an empty range. */
7684 bool take_default
= min_take_default
|| max_take_default
;
7688 if (max_take_default
)
7691 /* If the case label range is continuous, we do not need
7692 the default case label. Verify that. */
7693 high
= CASE_LOW (gimple_switch_label (stmt
, i
));
7694 if (CASE_HIGH (gimple_switch_label (stmt
, i
)))
7695 high
= CASE_HIGH (gimple_switch_label (stmt
, i
));
7696 for (k
= i
+ 1; k
<= j
; ++k
)
7698 low
= CASE_LOW (gimple_switch_label (stmt
, k
));
7699 if (!integer_onep (int_const_binop (MINUS_EXPR
, low
, high
)))
7701 take_default
= true;
7705 if (CASE_HIGH (gimple_switch_label (stmt
, k
)))
7706 high
= CASE_HIGH (gimple_switch_label (stmt
, k
));
7711 return !take_default
;
7715 /* Searches the case label vector VEC for the ranges of CASE_LABELs that are
7716 used in range VR. The indices are placed in MIN_IDX1, MAX_IDX, MIN_IDX2 and
7717 MAX_IDX2. If the ranges of CASE_LABELs are empty then MAX_IDX1 < MIN_IDX1.
7718 Returns true if the default label is not needed. */
7721 find_case_label_ranges (gswitch
*stmt
, value_range
*vr
, size_t *min_idx1
,
7722 size_t *max_idx1
, size_t *min_idx2
,
7726 unsigned int n
= gimple_switch_num_labels (stmt
);
7728 tree case_low
, case_high
;
7729 tree min
= vr
->min
, max
= vr
->max
;
7731 gcc_checking_assert (vr
->type
== VR_RANGE
|| vr
->type
== VR_ANTI_RANGE
);
7733 take_default
= !find_case_label_range (stmt
, min
, max
, &i
, &j
);
7735 /* Set second range to emtpy. */
7739 if (vr
->type
== VR_RANGE
)
7743 return !take_default
;
7746 /* Set first range to all case labels. */
7753 /* Make sure all the values of case labels [i , j] are contained in
7754 range [MIN, MAX]. */
7755 case_low
= CASE_LOW (gimple_switch_label (stmt
, i
));
7756 case_high
= CASE_HIGH (gimple_switch_label (stmt
, j
));
7757 if (tree_int_cst_compare (case_low
, min
) < 0)
7759 if (case_high
!= NULL_TREE
7760 && tree_int_cst_compare (max
, case_high
) < 0)
7766 /* If the range spans case labels [i, j], the corresponding anti-range spans
7767 the labels [1, i - 1] and [j + 1, n - 1]. */
7793 /* Visit switch statement STMT. If we can determine which edge
7794 will be taken out of STMT's basic block, record it in
7795 *TAKEN_EDGE_P and return SSA_PROP_INTERESTING. Otherwise, return
7796 SSA_PROP_VARYING. */
7798 static enum ssa_prop_result
7799 vrp_visit_switch_stmt (gswitch
*stmt
, edge
*taken_edge_p
)
7803 size_t i
= 0, j
= 0, k
, l
;
7806 *taken_edge_p
= NULL
;
7807 op
= gimple_switch_index (stmt
);
7808 if (TREE_CODE (op
) != SSA_NAME
)
7809 return SSA_PROP_VARYING
;
7811 vr
= get_value_range (op
);
7812 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7814 fprintf (dump_file
, "\nVisiting switch expression with operand ");
7815 print_generic_expr (dump_file
, op
, 0);
7816 fprintf (dump_file
, " with known range ");
7817 dump_value_range (dump_file
, vr
);
7818 fprintf (dump_file
, "\n");
7821 if ((vr
->type
!= VR_RANGE
7822 && vr
->type
!= VR_ANTI_RANGE
)
7823 || symbolic_range_p (vr
))
7824 return SSA_PROP_VARYING
;
7826 /* Find the single edge that is taken from the switch expression. */
7827 take_default
= !find_case_label_ranges (stmt
, vr
, &i
, &j
, &k
, &l
);
7829 /* Check if the range spans no CASE_LABEL. If so, we only reach the default
7833 gcc_assert (take_default
);
7834 val
= gimple_switch_default_label (stmt
);
7838 /* Check if labels with index i to j and maybe the default label
7839 are all reaching the same label. */
7841 val
= gimple_switch_label (stmt
, i
);
7843 && CASE_LABEL (gimple_switch_default_label (stmt
))
7844 != CASE_LABEL (val
))
7846 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7847 fprintf (dump_file
, " not a single destination for this "
7849 return SSA_PROP_VARYING
;
7851 for (++i
; i
<= j
; ++i
)
7853 if (CASE_LABEL (gimple_switch_label (stmt
, i
)) != CASE_LABEL (val
))
7855 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7856 fprintf (dump_file
, " not a single destination for this "
7858 return SSA_PROP_VARYING
;
7863 if (CASE_LABEL (gimple_switch_label (stmt
, k
)) != CASE_LABEL (val
))
7865 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7866 fprintf (dump_file
, " not a single destination for this "
7868 return SSA_PROP_VARYING
;
7873 *taken_edge_p
= find_edge (gimple_bb (stmt
),
7874 label_to_block (CASE_LABEL (val
)));
7876 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7878 fprintf (dump_file
, " will take edge to ");
7879 print_generic_stmt (dump_file
, CASE_LABEL (val
), 0);
7882 return SSA_PROP_INTERESTING
;
7886 /* Evaluate statement STMT. If the statement produces a useful range,
7887 return SSA_PROP_INTERESTING and record the SSA name with the
7888 interesting range into *OUTPUT_P.
7890 If STMT is a conditional branch and we can determine its truth
7891 value, the taken edge is recorded in *TAKEN_EDGE_P.
7893 If STMT produces a varying value, return SSA_PROP_VARYING. */
7895 static enum ssa_prop_result
7896 vrp_visit_stmt (gimple
*stmt
, edge
*taken_edge_p
, tree
*output_p
)
7901 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7903 fprintf (dump_file
, "\nVisiting statement:\n");
7904 print_gimple_stmt (dump_file
, stmt
, 0, dump_flags
);
7907 if (!stmt_interesting_for_vrp (stmt
))
7908 gcc_assert (stmt_ends_bb_p (stmt
));
7909 else if (is_gimple_assign (stmt
) || is_gimple_call (stmt
))
7910 return vrp_visit_assignment_or_call (stmt
, output_p
);
7911 else if (gimple_code (stmt
) == GIMPLE_COND
)
7912 return vrp_visit_cond_stmt (as_a
<gcond
*> (stmt
), taken_edge_p
);
7913 else if (gimple_code (stmt
) == GIMPLE_SWITCH
)
7914 return vrp_visit_switch_stmt (as_a
<gswitch
*> (stmt
), taken_edge_p
);
7916 /* All other statements produce nothing of interest for VRP, so mark
7917 their outputs varying and prevent further simulation. */
7918 FOR_EACH_SSA_TREE_OPERAND (def
, stmt
, iter
, SSA_OP_DEF
)
7919 set_value_range_to_varying (get_value_range (def
));
7921 return SSA_PROP_VARYING
;
7924 /* Union the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
7925 { VR1TYPE, VR0MIN, VR0MAX } and store the result
7926 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
7927 possible such range. The resulting range is not canonicalized. */
7930 union_ranges (enum value_range_type
*vr0type
,
7931 tree
*vr0min
, tree
*vr0max
,
7932 enum value_range_type vr1type
,
7933 tree vr1min
, tree vr1max
)
7935 bool mineq
= operand_equal_p (*vr0min
, vr1min
, 0);
7936 bool maxeq
= operand_equal_p (*vr0max
, vr1max
, 0);
7938 /* [] is vr0, () is vr1 in the following classification comments. */
7942 if (*vr0type
== vr1type
)
7943 /* Nothing to do for equal ranges. */
7945 else if ((*vr0type
== VR_RANGE
7946 && vr1type
== VR_ANTI_RANGE
)
7947 || (*vr0type
== VR_ANTI_RANGE
7948 && vr1type
== VR_RANGE
))
7950 /* For anti-range with range union the result is varying. */
7956 else if (operand_less_p (*vr0max
, vr1min
) == 1
7957 || operand_less_p (vr1max
, *vr0min
) == 1)
7959 /* [ ] ( ) or ( ) [ ]
7960 If the ranges have an empty intersection, result of the union
7961 operation is the anti-range or if both are anti-ranges
7963 if (*vr0type
== VR_ANTI_RANGE
7964 && vr1type
== VR_ANTI_RANGE
)
7966 else if (*vr0type
== VR_ANTI_RANGE
7967 && vr1type
== VR_RANGE
)
7969 else if (*vr0type
== VR_RANGE
7970 && vr1type
== VR_ANTI_RANGE
)
7976 else if (*vr0type
== VR_RANGE
7977 && vr1type
== VR_RANGE
)
7979 /* The result is the convex hull of both ranges. */
7980 if (operand_less_p (*vr0max
, vr1min
) == 1)
7982 /* If the result can be an anti-range, create one. */
7983 if (TREE_CODE (*vr0max
) == INTEGER_CST
7984 && TREE_CODE (vr1min
) == INTEGER_CST
7985 && vrp_val_is_min (*vr0min
)
7986 && vrp_val_is_max (vr1max
))
7988 tree min
= int_const_binop (PLUS_EXPR
,
7990 build_int_cst (TREE_TYPE (*vr0max
), 1));
7991 tree max
= int_const_binop (MINUS_EXPR
,
7993 build_int_cst (TREE_TYPE (vr1min
), 1));
7994 if (!operand_less_p (max
, min
))
7996 *vr0type
= VR_ANTI_RANGE
;
8008 /* If the result can be an anti-range, create one. */
8009 if (TREE_CODE (vr1max
) == INTEGER_CST
8010 && TREE_CODE (*vr0min
) == INTEGER_CST
8011 && vrp_val_is_min (vr1min
)
8012 && vrp_val_is_max (*vr0max
))
8014 tree min
= int_const_binop (PLUS_EXPR
,
8016 build_int_cst (TREE_TYPE (vr1max
), 1));
8017 tree max
= int_const_binop (MINUS_EXPR
,
8019 build_int_cst (TREE_TYPE (*vr0min
), 1));
8020 if (!operand_less_p (max
, min
))
8022 *vr0type
= VR_ANTI_RANGE
;
8036 else if ((maxeq
|| operand_less_p (vr1max
, *vr0max
) == 1)
8037 && (mineq
|| operand_less_p (*vr0min
, vr1min
) == 1))
8039 /* [ ( ) ] or [( ) ] or [ ( )] */
8040 if (*vr0type
== VR_RANGE
8041 && vr1type
== VR_RANGE
)
8043 else if (*vr0type
== VR_ANTI_RANGE
8044 && vr1type
== VR_ANTI_RANGE
)
8050 else if (*vr0type
== VR_ANTI_RANGE
8051 && vr1type
== VR_RANGE
)
8053 /* Arbitrarily choose the right or left gap. */
8054 if (!mineq
&& TREE_CODE (vr1min
) == INTEGER_CST
)
8055 *vr0max
= int_const_binop (MINUS_EXPR
, vr1min
,
8056 build_int_cst (TREE_TYPE (vr1min
), 1));
8057 else if (!maxeq
&& TREE_CODE (vr1max
) == INTEGER_CST
)
8058 *vr0min
= int_const_binop (PLUS_EXPR
, vr1max
,
8059 build_int_cst (TREE_TYPE (vr1max
), 1));
8063 else if (*vr0type
== VR_RANGE
8064 && vr1type
== VR_ANTI_RANGE
)
8065 /* The result covers everything. */
8070 else if ((maxeq
|| operand_less_p (*vr0max
, vr1max
) == 1)
8071 && (mineq
|| operand_less_p (vr1min
, *vr0min
) == 1))
8073 /* ( [ ] ) or ([ ] ) or ( [ ]) */
8074 if (*vr0type
== VR_RANGE
8075 && vr1type
== VR_RANGE
)
8081 else if (*vr0type
== VR_ANTI_RANGE
8082 && vr1type
== VR_ANTI_RANGE
)
8084 else if (*vr0type
== VR_RANGE
8085 && vr1type
== VR_ANTI_RANGE
)
8087 *vr0type
= VR_ANTI_RANGE
;
8088 if (!mineq
&& TREE_CODE (*vr0min
) == INTEGER_CST
)
8090 *vr0max
= int_const_binop (MINUS_EXPR
, *vr0min
,
8091 build_int_cst (TREE_TYPE (*vr0min
), 1));
8094 else if (!maxeq
&& TREE_CODE (*vr0max
) == INTEGER_CST
)
8096 *vr0min
= int_const_binop (PLUS_EXPR
, *vr0max
,
8097 build_int_cst (TREE_TYPE (*vr0max
), 1));
8103 else if (*vr0type
== VR_ANTI_RANGE
8104 && vr1type
== VR_RANGE
)
8105 /* The result covers everything. */
8110 else if ((operand_less_p (vr1min
, *vr0max
) == 1
8111 || operand_equal_p (vr1min
, *vr0max
, 0))
8112 && operand_less_p (*vr0min
, vr1min
) == 1
8113 && operand_less_p (*vr0max
, vr1max
) == 1)
8115 /* [ ( ] ) or [ ]( ) */
8116 if (*vr0type
== VR_RANGE
8117 && vr1type
== VR_RANGE
)
8119 else if (*vr0type
== VR_ANTI_RANGE
8120 && vr1type
== VR_ANTI_RANGE
)
8122 else if (*vr0type
== VR_ANTI_RANGE
8123 && vr1type
== VR_RANGE
)
8125 if (TREE_CODE (vr1min
) == INTEGER_CST
)
8126 *vr0max
= int_const_binop (MINUS_EXPR
, vr1min
,
8127 build_int_cst (TREE_TYPE (vr1min
), 1));
8131 else if (*vr0type
== VR_RANGE
8132 && vr1type
== VR_ANTI_RANGE
)
8134 if (TREE_CODE (*vr0max
) == INTEGER_CST
)
8137 *vr0min
= int_const_binop (PLUS_EXPR
, *vr0max
,
8138 build_int_cst (TREE_TYPE (*vr0max
), 1));
8147 else if ((operand_less_p (*vr0min
, vr1max
) == 1
8148 || operand_equal_p (*vr0min
, vr1max
, 0))
8149 && operand_less_p (vr1min
, *vr0min
) == 1
8150 && operand_less_p (vr1max
, *vr0max
) == 1)
8152 /* ( [ ) ] or ( )[ ] */
8153 if (*vr0type
== VR_RANGE
8154 && vr1type
== VR_RANGE
)
8156 else if (*vr0type
== VR_ANTI_RANGE
8157 && vr1type
== VR_ANTI_RANGE
)
8159 else if (*vr0type
== VR_ANTI_RANGE
8160 && vr1type
== VR_RANGE
)
8162 if (TREE_CODE (vr1max
) == INTEGER_CST
)
8163 *vr0min
= int_const_binop (PLUS_EXPR
, vr1max
,
8164 build_int_cst (TREE_TYPE (vr1max
), 1));
8168 else if (*vr0type
== VR_RANGE
8169 && vr1type
== VR_ANTI_RANGE
)
8171 if (TREE_CODE (*vr0min
) == INTEGER_CST
)
8175 *vr0max
= int_const_binop (MINUS_EXPR
, *vr0min
,
8176 build_int_cst (TREE_TYPE (*vr0min
), 1));
8190 *vr0type
= VR_VARYING
;
8191 *vr0min
= NULL_TREE
;
8192 *vr0max
= NULL_TREE
;
8195 /* Intersect the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
8196 { VR1TYPE, VR0MIN, VR0MAX } and store the result
8197 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
8198 possible such range. The resulting range is not canonicalized. */
8201 intersect_ranges (enum value_range_type
*vr0type
,
8202 tree
*vr0min
, tree
*vr0max
,
8203 enum value_range_type vr1type
,
8204 tree vr1min
, tree vr1max
)
8206 bool mineq
= operand_equal_p (*vr0min
, vr1min
, 0);
8207 bool maxeq
= operand_equal_p (*vr0max
, vr1max
, 0);
8209 /* [] is vr0, () is vr1 in the following classification comments. */
8213 if (*vr0type
== vr1type
)
8214 /* Nothing to do for equal ranges. */
8216 else if ((*vr0type
== VR_RANGE
8217 && vr1type
== VR_ANTI_RANGE
)
8218 || (*vr0type
== VR_ANTI_RANGE
8219 && vr1type
== VR_RANGE
))
8221 /* For anti-range with range intersection the result is empty. */
8222 *vr0type
= VR_UNDEFINED
;
8223 *vr0min
= NULL_TREE
;
8224 *vr0max
= NULL_TREE
;
8229 else if (operand_less_p (*vr0max
, vr1min
) == 1
8230 || operand_less_p (vr1max
, *vr0min
) == 1)
8232 /* [ ] ( ) or ( ) [ ]
8233 If the ranges have an empty intersection, the result of the
8234 intersect operation is the range for intersecting an
8235 anti-range with a range or empty when intersecting two ranges. */
8236 if (*vr0type
== VR_RANGE
8237 && vr1type
== VR_ANTI_RANGE
)
8239 else if (*vr0type
== VR_ANTI_RANGE
8240 && vr1type
== VR_RANGE
)
8246 else if (*vr0type
== VR_RANGE
8247 && vr1type
== VR_RANGE
)
8249 *vr0type
= VR_UNDEFINED
;
8250 *vr0min
= NULL_TREE
;
8251 *vr0max
= NULL_TREE
;
8253 else if (*vr0type
== VR_ANTI_RANGE
8254 && vr1type
== VR_ANTI_RANGE
)
8256 /* If the anti-ranges are adjacent to each other merge them. */
8257 if (TREE_CODE (*vr0max
) == INTEGER_CST
8258 && TREE_CODE (vr1min
) == INTEGER_CST
8259 && operand_less_p (*vr0max
, vr1min
) == 1
8260 && integer_onep (int_const_binop (MINUS_EXPR
,
8263 else if (TREE_CODE (vr1max
) == INTEGER_CST
8264 && TREE_CODE (*vr0min
) == INTEGER_CST
8265 && operand_less_p (vr1max
, *vr0min
) == 1
8266 && integer_onep (int_const_binop (MINUS_EXPR
,
8269 /* Else arbitrarily take VR0. */
8272 else if ((maxeq
|| operand_less_p (vr1max
, *vr0max
) == 1)
8273 && (mineq
|| operand_less_p (*vr0min
, vr1min
) == 1))
8275 /* [ ( ) ] or [( ) ] or [ ( )] */
8276 if (*vr0type
== VR_RANGE
8277 && vr1type
== VR_RANGE
)
8279 /* If both are ranges the result is the inner one. */
8284 else if (*vr0type
== VR_RANGE
8285 && vr1type
== VR_ANTI_RANGE
)
8287 /* Choose the right gap if the left one is empty. */
8290 if (TREE_CODE (vr1max
) == INTEGER_CST
)
8291 *vr0min
= int_const_binop (PLUS_EXPR
, vr1max
,
8292 build_int_cst (TREE_TYPE (vr1max
), 1));
8296 /* Choose the left gap if the right one is empty. */
8299 if (TREE_CODE (vr1min
) == INTEGER_CST
)
8300 *vr0max
= int_const_binop (MINUS_EXPR
, vr1min
,
8301 build_int_cst (TREE_TYPE (vr1min
), 1));
8305 /* Choose the anti-range if the range is effectively varying. */
8306 else if (vrp_val_is_min (*vr0min
)
8307 && vrp_val_is_max (*vr0max
))
8313 /* Else choose the range. */
8315 else if (*vr0type
== VR_ANTI_RANGE
8316 && vr1type
== VR_ANTI_RANGE
)
8317 /* If both are anti-ranges the result is the outer one. */
8319 else if (*vr0type
== VR_ANTI_RANGE
8320 && vr1type
== VR_RANGE
)
8322 /* The intersection is empty. */
8323 *vr0type
= VR_UNDEFINED
;
8324 *vr0min
= NULL_TREE
;
8325 *vr0max
= NULL_TREE
;
8330 else if ((maxeq
|| operand_less_p (*vr0max
, vr1max
) == 1)
8331 && (mineq
|| operand_less_p (vr1min
, *vr0min
) == 1))
8333 /* ( [ ] ) or ([ ] ) or ( [ ]) */
8334 if (*vr0type
== VR_RANGE
8335 && vr1type
== VR_RANGE
)
8336 /* Choose the inner range. */
8338 else if (*vr0type
== VR_ANTI_RANGE
8339 && vr1type
== VR_RANGE
)
8341 /* Choose the right gap if the left is empty. */
8344 *vr0type
= VR_RANGE
;
8345 if (TREE_CODE (*vr0max
) == INTEGER_CST
)
8346 *vr0min
= int_const_binop (PLUS_EXPR
, *vr0max
,
8347 build_int_cst (TREE_TYPE (*vr0max
), 1));
8352 /* Choose the left gap if the right is empty. */
8355 *vr0type
= VR_RANGE
;
8356 if (TREE_CODE (*vr0min
) == INTEGER_CST
)
8357 *vr0max
= int_const_binop (MINUS_EXPR
, *vr0min
,
8358 build_int_cst (TREE_TYPE (*vr0min
), 1));
8363 /* Choose the anti-range if the range is effectively varying. */
8364 else if (vrp_val_is_min (vr1min
)
8365 && vrp_val_is_max (vr1max
))
8367 /* Else choose the range. */
8375 else if (*vr0type
== VR_ANTI_RANGE
8376 && vr1type
== VR_ANTI_RANGE
)
8378 /* If both are anti-ranges the result is the outer one. */
8383 else if (vr1type
== VR_ANTI_RANGE
8384 && *vr0type
== VR_RANGE
)
8386 /* The intersection is empty. */
8387 *vr0type
= VR_UNDEFINED
;
8388 *vr0min
= NULL_TREE
;
8389 *vr0max
= NULL_TREE
;
8394 else if ((operand_less_p (vr1min
, *vr0max
) == 1
8395 || operand_equal_p (vr1min
, *vr0max
, 0))
8396 && operand_less_p (*vr0min
, vr1min
) == 1)
8398 /* [ ( ] ) or [ ]( ) */
8399 if (*vr0type
== VR_ANTI_RANGE
8400 && vr1type
== VR_ANTI_RANGE
)
8402 else if (*vr0type
== VR_RANGE
8403 && vr1type
== VR_RANGE
)
8405 else if (*vr0type
== VR_RANGE
8406 && vr1type
== VR_ANTI_RANGE
)
8408 if (TREE_CODE (vr1min
) == INTEGER_CST
)
8409 *vr0max
= int_const_binop (MINUS_EXPR
, vr1min
,
8410 build_int_cst (TREE_TYPE (vr1min
), 1));
8414 else if (*vr0type
== VR_ANTI_RANGE
8415 && vr1type
== VR_RANGE
)
8417 *vr0type
= VR_RANGE
;
8418 if (TREE_CODE (*vr0max
) == INTEGER_CST
)
8419 *vr0min
= int_const_binop (PLUS_EXPR
, *vr0max
,
8420 build_int_cst (TREE_TYPE (*vr0max
), 1));
8428 else if ((operand_less_p (*vr0min
, vr1max
) == 1
8429 || operand_equal_p (*vr0min
, vr1max
, 0))
8430 && operand_less_p (vr1min
, *vr0min
) == 1)
8432 /* ( [ ) ] or ( )[ ] */
8433 if (*vr0type
== VR_ANTI_RANGE
8434 && vr1type
== VR_ANTI_RANGE
)
8436 else if (*vr0type
== VR_RANGE
8437 && vr1type
== VR_RANGE
)
8439 else if (*vr0type
== VR_RANGE
8440 && vr1type
== VR_ANTI_RANGE
)
8442 if (TREE_CODE (vr1max
) == INTEGER_CST
)
8443 *vr0min
= int_const_binop (PLUS_EXPR
, vr1max
,
8444 build_int_cst (TREE_TYPE (vr1max
), 1));
8448 else if (*vr0type
== VR_ANTI_RANGE
8449 && vr1type
== VR_RANGE
)
8451 *vr0type
= VR_RANGE
;
8452 if (TREE_CODE (*vr0min
) == INTEGER_CST
)
8453 *vr0max
= int_const_binop (MINUS_EXPR
, *vr0min
,
8454 build_int_cst (TREE_TYPE (*vr0min
), 1));
8463 /* As a fallback simply use { *VRTYPE, *VR0MIN, *VR0MAX } as
8464 result for the intersection. That's always a conservative
8465 correct estimate. */
8471 /* Intersect the two value-ranges *VR0 and *VR1 and store the result
8472 in *VR0. This may not be the smallest possible such range. */
8475 vrp_intersect_ranges_1 (value_range
*vr0
, value_range
*vr1
)
8479 /* If either range is VR_VARYING the other one wins. */
8480 if (vr1
->type
== VR_VARYING
)
8482 if (vr0
->type
== VR_VARYING
)
8484 copy_value_range (vr0
, vr1
);
8488 /* When either range is VR_UNDEFINED the resulting range is
8489 VR_UNDEFINED, too. */
8490 if (vr0
->type
== VR_UNDEFINED
)
8492 if (vr1
->type
== VR_UNDEFINED
)
8494 set_value_range_to_undefined (vr0
);
8498 /* Save the original vr0 so we can return it as conservative intersection
8499 result when our worker turns things to varying. */
8501 intersect_ranges (&vr0
->type
, &vr0
->min
, &vr0
->max
,
8502 vr1
->type
, vr1
->min
, vr1
->max
);
8503 /* Make sure to canonicalize the result though as the inversion of a
8504 VR_RANGE can still be a VR_RANGE. */
8505 set_and_canonicalize_value_range (vr0
, vr0
->type
,
8506 vr0
->min
, vr0
->max
, vr0
->equiv
);
8507 /* If that failed, use the saved original VR0. */
8508 if (vr0
->type
== VR_VARYING
)
8513 /* If the result is VR_UNDEFINED there is no need to mess with
8514 the equivalencies. */
8515 if (vr0
->type
== VR_UNDEFINED
)
8518 /* The resulting set of equivalences for range intersection is the union of
8520 if (vr0
->equiv
&& vr1
->equiv
&& vr0
->equiv
!= vr1
->equiv
)
8521 bitmap_ior_into (vr0
->equiv
, vr1
->equiv
);
8522 else if (vr1
->equiv
&& !vr0
->equiv
)
8523 bitmap_copy (vr0
->equiv
, vr1
->equiv
);
8527 vrp_intersect_ranges (value_range
*vr0
, value_range
*vr1
)
8529 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8531 fprintf (dump_file
, "Intersecting\n ");
8532 dump_value_range (dump_file
, vr0
);
8533 fprintf (dump_file
, "\nand\n ");
8534 dump_value_range (dump_file
, vr1
);
8535 fprintf (dump_file
, "\n");
8537 vrp_intersect_ranges_1 (vr0
, vr1
);
8538 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8540 fprintf (dump_file
, "to\n ");
8541 dump_value_range (dump_file
, vr0
);
8542 fprintf (dump_file
, "\n");
8546 /* Meet operation for value ranges. Given two value ranges VR0 and
8547 VR1, store in VR0 a range that contains both VR0 and VR1. This
8548 may not be the smallest possible such range. */
8551 vrp_meet_1 (value_range
*vr0
, value_range
*vr1
)
8555 if (vr0
->type
== VR_UNDEFINED
)
8557 set_value_range (vr0
, vr1
->type
, vr1
->min
, vr1
->max
, vr1
->equiv
);
8561 if (vr1
->type
== VR_UNDEFINED
)
8563 /* VR0 already has the resulting range. */
8567 if (vr0
->type
== VR_VARYING
)
8569 /* Nothing to do. VR0 already has the resulting range. */
8573 if (vr1
->type
== VR_VARYING
)
8575 set_value_range_to_varying (vr0
);
8580 union_ranges (&vr0
->type
, &vr0
->min
, &vr0
->max
,
8581 vr1
->type
, vr1
->min
, vr1
->max
);
8582 if (vr0
->type
== VR_VARYING
)
8584 /* Failed to find an efficient meet. Before giving up and setting
8585 the result to VARYING, see if we can at least derive a useful
8586 anti-range. FIXME, all this nonsense about distinguishing
8587 anti-ranges from ranges is necessary because of the odd
8588 semantics of range_includes_zero_p and friends. */
8589 if (((saved
.type
== VR_RANGE
8590 && range_includes_zero_p (saved
.min
, saved
.max
) == 0)
8591 || (saved
.type
== VR_ANTI_RANGE
8592 && range_includes_zero_p (saved
.min
, saved
.max
) == 1))
8593 && ((vr1
->type
== VR_RANGE
8594 && range_includes_zero_p (vr1
->min
, vr1
->max
) == 0)
8595 || (vr1
->type
== VR_ANTI_RANGE
8596 && range_includes_zero_p (vr1
->min
, vr1
->max
) == 1)))
8598 set_value_range_to_nonnull (vr0
, TREE_TYPE (saved
.min
));
8600 /* Since this meet operation did not result from the meeting of
8601 two equivalent names, VR0 cannot have any equivalences. */
8603 bitmap_clear (vr0
->equiv
);
8607 set_value_range_to_varying (vr0
);
8610 set_and_canonicalize_value_range (vr0
, vr0
->type
, vr0
->min
, vr0
->max
,
8612 if (vr0
->type
== VR_VARYING
)
8615 /* The resulting set of equivalences is always the intersection of
8617 if (vr0
->equiv
&& vr1
->equiv
&& vr0
->equiv
!= vr1
->equiv
)
8618 bitmap_and_into (vr0
->equiv
, vr1
->equiv
);
8619 else if (vr0
->equiv
&& !vr1
->equiv
)
8620 bitmap_clear (vr0
->equiv
);
8624 vrp_meet (value_range
*vr0
, value_range
*vr1
)
8626 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8628 fprintf (dump_file
, "Meeting\n ");
8629 dump_value_range (dump_file
, vr0
);
8630 fprintf (dump_file
, "\nand\n ");
8631 dump_value_range (dump_file
, vr1
);
8632 fprintf (dump_file
, "\n");
8634 vrp_meet_1 (vr0
, vr1
);
8635 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8637 fprintf (dump_file
, "to\n ");
8638 dump_value_range (dump_file
, vr0
);
8639 fprintf (dump_file
, "\n");
8644 /* Visit all arguments for PHI node PHI that flow through executable
8645 edges. If a valid value range can be derived from all the incoming
8646 value ranges, set a new range for the LHS of PHI. */
8648 static enum ssa_prop_result
8649 vrp_visit_phi_node (gphi
*phi
)
8652 tree lhs
= PHI_RESULT (phi
);
8653 value_range
*lhs_vr
= get_value_range (lhs
);
8654 value_range vr_result
= VR_INITIALIZER
;
8656 int edges
, old_edges
;
8659 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8661 fprintf (dump_file
, "\nVisiting PHI node: ");
8662 print_gimple_stmt (dump_file
, phi
, 0, dump_flags
);
8666 for (i
= 0; i
< gimple_phi_num_args (phi
); i
++)
8668 edge e
= gimple_phi_arg_edge (phi
, i
);
8670 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8673 " Argument #%d (%d -> %d %sexecutable)\n",
8674 (int) i
, e
->src
->index
, e
->dest
->index
,
8675 (e
->flags
& EDGE_EXECUTABLE
) ? "" : "not ");
8678 if (e
->flags
& EDGE_EXECUTABLE
)
8680 tree arg
= PHI_ARG_DEF (phi
, i
);
8685 if (TREE_CODE (arg
) == SSA_NAME
)
8687 vr_arg
= *(get_value_range (arg
));
8688 /* Do not allow equivalences or symbolic ranges to leak in from
8689 backedges. That creates invalid equivalencies.
8690 See PR53465 and PR54767. */
8691 if (e
->flags
& EDGE_DFS_BACK
)
8693 if (vr_arg
.type
== VR_RANGE
8694 || vr_arg
.type
== VR_ANTI_RANGE
)
8696 vr_arg
.equiv
= NULL
;
8697 if (symbolic_range_p (&vr_arg
))
8699 vr_arg
.type
= VR_VARYING
;
8700 vr_arg
.min
= NULL_TREE
;
8701 vr_arg
.max
= NULL_TREE
;
8707 /* If the non-backedge arguments range is VR_VARYING then
8708 we can still try recording a simple equivalence. */
8709 if (vr_arg
.type
== VR_VARYING
)
8711 vr_arg
.type
= VR_RANGE
;
8714 vr_arg
.equiv
= NULL
;
8720 if (TREE_OVERFLOW_P (arg
))
8721 arg
= drop_tree_overflow (arg
);
8723 vr_arg
.type
= VR_RANGE
;
8726 vr_arg
.equiv
= NULL
;
8729 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8731 fprintf (dump_file
, "\t");
8732 print_generic_expr (dump_file
, arg
, dump_flags
);
8733 fprintf (dump_file
, ": ");
8734 dump_value_range (dump_file
, &vr_arg
);
8735 fprintf (dump_file
, "\n");
8739 copy_value_range (&vr_result
, &vr_arg
);
8741 vrp_meet (&vr_result
, &vr_arg
);
8744 if (vr_result
.type
== VR_VARYING
)
8749 if (vr_result
.type
== VR_VARYING
)
8751 else if (vr_result
.type
== VR_UNDEFINED
)
8754 old_edges
= vr_phi_edge_counts
[SSA_NAME_VERSION (lhs
)];
8755 vr_phi_edge_counts
[SSA_NAME_VERSION (lhs
)] = edges
;
8757 /* To prevent infinite iterations in the algorithm, derive ranges
8758 when the new value is slightly bigger or smaller than the
8759 previous one. We don't do this if we have seen a new executable
8760 edge; this helps us avoid an overflow infinity for conditionals
8761 which are not in a loop. If the old value-range was VR_UNDEFINED
8762 use the updated range and iterate one more time. */
8764 && gimple_phi_num_args (phi
) > 1
8765 && edges
== old_edges
8766 && lhs_vr
->type
!= VR_UNDEFINED
)
8768 /* Compare old and new ranges, fall back to varying if the
8769 values are not comparable. */
8770 int cmp_min
= compare_values (lhs_vr
->min
, vr_result
.min
);
8773 int cmp_max
= compare_values (lhs_vr
->max
, vr_result
.max
);
8777 /* For non VR_RANGE or for pointers fall back to varying if
8778 the range changed. */
8779 if ((lhs_vr
->type
!= VR_RANGE
|| vr_result
.type
!= VR_RANGE
8780 || POINTER_TYPE_P (TREE_TYPE (lhs
)))
8781 && (cmp_min
!= 0 || cmp_max
!= 0))
8784 /* If the new minimum is larger than the previous one
8785 retain the old value. If the new minimum value is smaller
8786 than the previous one and not -INF go all the way to -INF + 1.
8787 In the first case, to avoid infinite bouncing between different
8788 minimums, and in the other case to avoid iterating millions of
8789 times to reach -INF. Going to -INF + 1 also lets the following
8790 iteration compute whether there will be any overflow, at the
8791 expense of one additional iteration. */
8793 vr_result
.min
= lhs_vr
->min
;
8794 else if (cmp_min
> 0
8795 && !vrp_val_is_min (vr_result
.min
))
8797 = int_const_binop (PLUS_EXPR
,
8798 vrp_val_min (TREE_TYPE (vr_result
.min
)),
8799 build_int_cst (TREE_TYPE (vr_result
.min
), 1));
8801 /* Similarly for the maximum value. */
8803 vr_result
.max
= lhs_vr
->max
;
8804 else if (cmp_max
< 0
8805 && !vrp_val_is_max (vr_result
.max
))
8807 = int_const_binop (MINUS_EXPR
,
8808 vrp_val_max (TREE_TYPE (vr_result
.min
)),
8809 build_int_cst (TREE_TYPE (vr_result
.min
), 1));
8811 /* If we dropped either bound to +-INF then if this is a loop
8812 PHI node SCEV may known more about its value-range. */
8813 if ((cmp_min
> 0 || cmp_min
< 0
8814 || cmp_max
< 0 || cmp_max
> 0)
8815 && (l
= loop_containing_stmt (phi
))
8816 && l
->header
== gimple_bb (phi
))
8817 adjust_range_with_scev (&vr_result
, l
, phi
, lhs
);
8819 /* If we will end up with a (-INF, +INF) range, set it to
8820 VARYING. Same if the previous max value was invalid for
8821 the type and we end up with vr_result.min > vr_result.max. */
8822 if ((vrp_val_is_max (vr_result
.max
)
8823 && vrp_val_is_min (vr_result
.min
))
8824 || compare_values (vr_result
.min
,
8829 /* If the new range is different than the previous value, keep
8832 if (update_value_range (lhs
, &vr_result
))
8834 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8836 fprintf (dump_file
, "Found new range for ");
8837 print_generic_expr (dump_file
, lhs
, 0);
8838 fprintf (dump_file
, ": ");
8839 dump_value_range (dump_file
, &vr_result
);
8840 fprintf (dump_file
, "\n");
8843 if (vr_result
.type
== VR_VARYING
)
8844 return SSA_PROP_VARYING
;
8846 return SSA_PROP_INTERESTING
;
8849 /* Nothing changed, don't add outgoing edges. */
8850 return SSA_PROP_NOT_INTERESTING
;
8852 /* No match found. Set the LHS to VARYING. */
8854 set_value_range_to_varying (lhs_vr
);
8855 return SSA_PROP_VARYING
;
8858 /* Simplify boolean operations if the source is known
8859 to be already a boolean. */
8861 simplify_truth_ops_using_ranges (gimple_stmt_iterator
*gsi
, gimple
*stmt
)
8863 enum tree_code rhs_code
= gimple_assign_rhs_code (stmt
);
8865 bool need_conversion
;
8867 /* We handle only !=/== case here. */
8868 gcc_assert (rhs_code
== EQ_EXPR
|| rhs_code
== NE_EXPR
);
8870 op0
= gimple_assign_rhs1 (stmt
);
8871 if (!op_with_boolean_value_range_p (op0
))
8874 op1
= gimple_assign_rhs2 (stmt
);
8875 if (!op_with_boolean_value_range_p (op1
))
8878 /* Reduce number of cases to handle to NE_EXPR. As there is no
8879 BIT_XNOR_EXPR we cannot replace A == B with a single statement. */
8880 if (rhs_code
== EQ_EXPR
)
8882 if (TREE_CODE (op1
) == INTEGER_CST
)
8883 op1
= int_const_binop (BIT_XOR_EXPR
, op1
,
8884 build_int_cst (TREE_TYPE (op1
), 1));
8889 lhs
= gimple_assign_lhs (stmt
);
8891 = !useless_type_conversion_p (TREE_TYPE (lhs
), TREE_TYPE (op0
));
8893 /* Make sure to not sign-extend a 1-bit 1 when converting the result. */
8895 && !TYPE_UNSIGNED (TREE_TYPE (op0
))
8896 && TYPE_PRECISION (TREE_TYPE (op0
)) == 1
8897 && TYPE_PRECISION (TREE_TYPE (lhs
)) > 1)
8900 /* For A != 0 we can substitute A itself. */
8901 if (integer_zerop (op1
))
8902 gimple_assign_set_rhs_with_ops (gsi
,
8904 ? NOP_EXPR
: TREE_CODE (op0
), op0
);
8905 /* For A != B we substitute A ^ B. Either with conversion. */
8906 else if (need_conversion
)
8908 tree tem
= make_ssa_name (TREE_TYPE (op0
));
8910 = gimple_build_assign (tem
, BIT_XOR_EXPR
, op0
, op1
);
8911 gsi_insert_before (gsi
, newop
, GSI_SAME_STMT
);
8912 gimple_assign_set_rhs_with_ops (gsi
, NOP_EXPR
, tem
);
8916 gimple_assign_set_rhs_with_ops (gsi
, BIT_XOR_EXPR
, op0
, op1
);
8917 update_stmt (gsi_stmt (*gsi
));
8922 /* Simplify a division or modulo operator to a right shift or
8923 bitwise and if the first operand is unsigned or is greater
8924 than zero and the second operand is an exact power of two.
8925 For TRUNC_MOD_EXPR op0 % op1 with constant op1, optimize it
8926 into just op0 if op0's range is known to be a subset of
8927 [-op1 + 1, op1 - 1] for signed and [0, op1 - 1] for unsigned
8931 simplify_div_or_mod_using_ranges (gimple
*stmt
)
8933 enum tree_code rhs_code
= gimple_assign_rhs_code (stmt
);
8935 tree op0
= gimple_assign_rhs1 (stmt
);
8936 tree op1
= gimple_assign_rhs2 (stmt
);
8937 value_range
*vr
= get_value_range (op0
);
8939 if (rhs_code
== TRUNC_MOD_EXPR
8940 && TREE_CODE (op1
) == INTEGER_CST
8941 && tree_int_cst_sgn (op1
) == 1
8942 && range_int_cst_p (vr
)
8943 && tree_int_cst_lt (vr
->max
, op1
))
8945 if (TYPE_UNSIGNED (TREE_TYPE (op0
))
8946 || tree_int_cst_sgn (vr
->min
) >= 0
8947 || tree_int_cst_lt (fold_unary (NEGATE_EXPR
, TREE_TYPE (op1
), op1
),
8950 /* If op0 already has the range op0 % op1 has,
8951 then TRUNC_MOD_EXPR won't change anything. */
8952 gimple_stmt_iterator gsi
= gsi_for_stmt (stmt
);
8953 gimple_assign_set_rhs_from_tree (&gsi
, op0
);
8959 if (!integer_pow2p (op1
))
8962 if (TYPE_UNSIGNED (TREE_TYPE (op0
)))
8964 val
= integer_one_node
;
8970 val
= compare_range_with_value (GE_EXPR
, vr
, integer_zero_node
, &sop
);
8974 && integer_onep (val
)
8975 && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC
))
8977 location_t location
;
8979 if (!gimple_has_location (stmt
))
8980 location
= input_location
;
8982 location
= gimple_location (stmt
);
8983 warning_at (location
, OPT_Wstrict_overflow
,
8984 "assuming signed overflow does not occur when "
8985 "simplifying %</%> or %<%%%> to %<>>%> or %<&%>");
8989 if (val
&& integer_onep (val
))
8993 if (rhs_code
== TRUNC_DIV_EXPR
)
8995 t
= build_int_cst (integer_type_node
, tree_log2 (op1
));
8996 gimple_assign_set_rhs_code (stmt
, RSHIFT_EXPR
);
8997 gimple_assign_set_rhs1 (stmt
, op0
);
8998 gimple_assign_set_rhs2 (stmt
, t
);
9002 t
= build_int_cst (TREE_TYPE (op1
), 1);
9003 t
= int_const_binop (MINUS_EXPR
, op1
, t
);
9004 t
= fold_convert (TREE_TYPE (op0
), t
);
9006 gimple_assign_set_rhs_code (stmt
, BIT_AND_EXPR
);
9007 gimple_assign_set_rhs1 (stmt
, op0
);
9008 gimple_assign_set_rhs2 (stmt
, t
);
9018 /* Simplify a min or max if the ranges of the two operands are
9019 disjoint. Return true if we do simplify. */
9022 simplify_min_or_max_using_ranges (gimple
*stmt
)
9024 tree op0
= gimple_assign_rhs1 (stmt
);
9025 tree op1
= gimple_assign_rhs2 (stmt
);
9029 val
= (vrp_evaluate_conditional_warnv_with_ops_using_ranges
9030 (LE_EXPR
, op0
, op1
, &sop
));
9034 val
= (vrp_evaluate_conditional_warnv_with_ops_using_ranges
9035 (LT_EXPR
, op0
, op1
, &sop
));
9040 if (sop
&& issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC
))
9042 location_t location
;
9044 if (!gimple_has_location (stmt
))
9045 location
= input_location
;
9047 location
= gimple_location (stmt
);
9048 warning_at (location
, OPT_Wstrict_overflow
,
9049 "assuming signed overflow does not occur when "
9050 "simplifying %<min/max (X,Y)%> to %<X%> or %<Y%>");
9053 /* VAL == TRUE -> OP0 < or <= op1
9054 VAL == FALSE -> OP0 > or >= op1. */
9055 tree res
= ((gimple_assign_rhs_code (stmt
) == MAX_EXPR
)
9056 == integer_zerop (val
)) ? op0
: op1
;
9057 gimple_stmt_iterator gsi
= gsi_for_stmt (stmt
);
9058 gimple_assign_set_rhs_from_tree (&gsi
, res
);
9066 /* If the operand to an ABS_EXPR is >= 0, then eliminate the
9067 ABS_EXPR. If the operand is <= 0, then simplify the
9068 ABS_EXPR into a NEGATE_EXPR. */
9071 simplify_abs_using_ranges (gimple
*stmt
)
9073 tree op
= gimple_assign_rhs1 (stmt
);
9074 value_range
*vr
= get_value_range (op
);
9081 val
= compare_range_with_value (LE_EXPR
, vr
, integer_zero_node
, &sop
);
9084 /* The range is neither <= 0 nor > 0. Now see if it is
9085 either < 0 or >= 0. */
9087 val
= compare_range_with_value (LT_EXPR
, vr
, integer_zero_node
,
9093 if (sop
&& issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC
))
9095 location_t location
;
9097 if (!gimple_has_location (stmt
))
9098 location
= input_location
;
9100 location
= gimple_location (stmt
);
9101 warning_at (location
, OPT_Wstrict_overflow
,
9102 "assuming signed overflow does not occur when "
9103 "simplifying %<abs (X)%> to %<X%> or %<-X%>");
9106 gimple_assign_set_rhs1 (stmt
, op
);
9107 if (integer_zerop (val
))
9108 gimple_assign_set_rhs_code (stmt
, SSA_NAME
);
9110 gimple_assign_set_rhs_code (stmt
, NEGATE_EXPR
);
9119 /* Optimize away redundant BIT_AND_EXPR and BIT_IOR_EXPR.
9120 If all the bits that are being cleared by & are already
9121 known to be zero from VR, or all the bits that are being
9122 set by | are already known to be one from VR, the bit
9123 operation is redundant. */
9126 simplify_bit_ops_using_ranges (gimple_stmt_iterator
*gsi
, gimple
*stmt
)
9128 tree op0
= gimple_assign_rhs1 (stmt
);
9129 tree op1
= gimple_assign_rhs2 (stmt
);
9130 tree op
= NULL_TREE
;
9131 value_range vr0
= VR_INITIALIZER
;
9132 value_range vr1
= VR_INITIALIZER
;
9133 wide_int may_be_nonzero0
, may_be_nonzero1
;
9134 wide_int must_be_nonzero0
, must_be_nonzero1
;
9137 if (TREE_CODE (op0
) == SSA_NAME
)
9138 vr0
= *(get_value_range (op0
));
9139 else if (is_gimple_min_invariant (op0
))
9140 set_value_range_to_value (&vr0
, op0
, NULL
);
9144 if (TREE_CODE (op1
) == SSA_NAME
)
9145 vr1
= *(get_value_range (op1
));
9146 else if (is_gimple_min_invariant (op1
))
9147 set_value_range_to_value (&vr1
, op1
, NULL
);
9151 if (!zero_nonzero_bits_from_vr (TREE_TYPE (op0
), &vr0
, &may_be_nonzero0
,
9154 if (!zero_nonzero_bits_from_vr (TREE_TYPE (op1
), &vr1
, &may_be_nonzero1
,
9158 switch (gimple_assign_rhs_code (stmt
))
9161 mask
= may_be_nonzero0
.and_not (must_be_nonzero1
);
9167 mask
= may_be_nonzero1
.and_not (must_be_nonzero0
);
9175 mask
= may_be_nonzero0
.and_not (must_be_nonzero1
);
9181 mask
= may_be_nonzero1
.and_not (must_be_nonzero0
);
9192 if (op
== NULL_TREE
)
9195 gimple_assign_set_rhs_with_ops (gsi
, TREE_CODE (op
), op
);
9196 update_stmt (gsi_stmt (*gsi
));
9200 /* We are comparing trees OP0 and OP1 using COND_CODE. OP0 has
9201 a known value range VR.
9203 If there is one and only one value which will satisfy the
9204 conditional, then return that value. Else return NULL.
9206 If signed overflow must be undefined for the value to satisfy
9207 the conditional, then set *STRICT_OVERFLOW_P to true. */
9210 test_for_singularity (enum tree_code cond_code
, tree op0
,
9211 tree op1
, value_range
*vr
,
9212 bool *strict_overflow_p
)
9217 /* Extract minimum/maximum values which satisfy the
9218 the conditional as it was written. */
9219 if (cond_code
== LE_EXPR
|| cond_code
== LT_EXPR
)
9221 /* This should not be negative infinity; there is no overflow
9223 min
= TYPE_MIN_VALUE (TREE_TYPE (op0
));
9226 if (cond_code
== LT_EXPR
&& !is_overflow_infinity (max
))
9228 tree one
= build_int_cst (TREE_TYPE (op0
), 1);
9229 max
= fold_build2 (MINUS_EXPR
, TREE_TYPE (op0
), max
, one
);
9231 TREE_NO_WARNING (max
) = 1;
9234 else if (cond_code
== GE_EXPR
|| cond_code
== GT_EXPR
)
9236 /* This should not be positive infinity; there is no overflow
9238 max
= TYPE_MAX_VALUE (TREE_TYPE (op0
));
9241 if (cond_code
== GT_EXPR
&& !is_overflow_infinity (min
))
9243 tree one
= build_int_cst (TREE_TYPE (op0
), 1);
9244 min
= fold_build2 (PLUS_EXPR
, TREE_TYPE (op0
), min
, one
);
9246 TREE_NO_WARNING (min
) = 1;
9250 /* Now refine the minimum and maximum values using any
9251 value range information we have for op0. */
9254 if (compare_values (vr
->min
, min
) == 1)
9256 if (compare_values (vr
->max
, max
) == -1)
9259 /* If the new min/max values have converged to a single value,
9260 then there is only one value which can satisfy the condition,
9261 return that value. */
9262 if (operand_equal_p (min
, max
, 0) && is_gimple_min_invariant (min
))
9264 if ((cond_code
== LE_EXPR
|| cond_code
== LT_EXPR
)
9265 && is_overflow_infinity (vr
->max
))
9266 *strict_overflow_p
= true;
9267 if ((cond_code
== GE_EXPR
|| cond_code
== GT_EXPR
)
9268 && is_overflow_infinity (vr
->min
))
9269 *strict_overflow_p
= true;
9277 /* Return whether the value range *VR fits in an integer type specified
9278 by PRECISION and UNSIGNED_P. */
9281 range_fits_type_p (value_range
*vr
, unsigned dest_precision
, signop dest_sgn
)
9284 unsigned src_precision
;
9288 /* We can only handle integral and pointer types. */
9289 src_type
= TREE_TYPE (vr
->min
);
9290 if (!INTEGRAL_TYPE_P (src_type
)
9291 && !POINTER_TYPE_P (src_type
))
9294 /* An extension is fine unless VR is SIGNED and dest_sgn is UNSIGNED,
9295 and so is an identity transform. */
9296 src_precision
= TYPE_PRECISION (TREE_TYPE (vr
->min
));
9297 src_sgn
= TYPE_SIGN (src_type
);
9298 if ((src_precision
< dest_precision
9299 && !(dest_sgn
== UNSIGNED
&& src_sgn
== SIGNED
))
9300 || (src_precision
== dest_precision
&& src_sgn
== dest_sgn
))
9303 /* Now we can only handle ranges with constant bounds. */
9304 if (vr
->type
!= VR_RANGE
9305 || TREE_CODE (vr
->min
) != INTEGER_CST
9306 || TREE_CODE (vr
->max
) != INTEGER_CST
)
9309 /* For sign changes, the MSB of the wide_int has to be clear.
9310 An unsigned value with its MSB set cannot be represented by
9311 a signed wide_int, while a negative value cannot be represented
9312 by an unsigned wide_int. */
9313 if (src_sgn
!= dest_sgn
9314 && (wi::lts_p (vr
->min
, 0) || wi::lts_p (vr
->max
, 0)))
9317 /* Then we can perform the conversion on both ends and compare
9318 the result for equality. */
9319 tem
= wi::ext (wi::to_widest (vr
->min
), dest_precision
, dest_sgn
);
9320 if (tem
!= wi::to_widest (vr
->min
))
9322 tem
= wi::ext (wi::to_widest (vr
->max
), dest_precision
, dest_sgn
);
9323 if (tem
!= wi::to_widest (vr
->max
))
9329 /* Simplify a conditional using a relational operator to an equality
9330 test if the range information indicates only one value can satisfy
9331 the original conditional. */
9334 simplify_cond_using_ranges (gcond
*stmt
)
9336 tree op0
= gimple_cond_lhs (stmt
);
9337 tree op1
= gimple_cond_rhs (stmt
);
9338 enum tree_code cond_code
= gimple_cond_code (stmt
);
9340 if (cond_code
!= NE_EXPR
9341 && cond_code
!= EQ_EXPR
9342 && TREE_CODE (op0
) == SSA_NAME
9343 && INTEGRAL_TYPE_P (TREE_TYPE (op0
))
9344 && is_gimple_min_invariant (op1
))
9346 value_range
*vr
= get_value_range (op0
);
9348 /* If we have range information for OP0, then we might be
9349 able to simplify this conditional. */
9350 if (vr
->type
== VR_RANGE
)
9352 enum warn_strict_overflow_code wc
= WARN_STRICT_OVERFLOW_COMPARISON
;
9354 tree new_tree
= test_for_singularity (cond_code
, op0
, op1
, vr
, &sop
);
9357 && (!sop
|| TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0
))))
9361 fprintf (dump_file
, "Simplified relational ");
9362 print_gimple_stmt (dump_file
, stmt
, 0, 0);
9363 fprintf (dump_file
, " into ");
9366 gimple_cond_set_code (stmt
, EQ_EXPR
);
9367 gimple_cond_set_lhs (stmt
, op0
);
9368 gimple_cond_set_rhs (stmt
, new_tree
);
9374 print_gimple_stmt (dump_file
, stmt
, 0, 0);
9375 fprintf (dump_file
, "\n");
9378 if (sop
&& issue_strict_overflow_warning (wc
))
9380 location_t location
= input_location
;
9381 if (gimple_has_location (stmt
))
9382 location
= gimple_location (stmt
);
9384 warning_at (location
, OPT_Wstrict_overflow
,
9385 "assuming signed overflow does not occur when "
9386 "simplifying conditional");
9392 /* Try again after inverting the condition. We only deal
9393 with integral types here, so no need to worry about
9394 issues with inverting FP comparisons. */
9396 new_tree
= test_for_singularity
9397 (invert_tree_comparison (cond_code
, false),
9398 op0
, op1
, vr
, &sop
);
9401 && (!sop
|| TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0
))))
9405 fprintf (dump_file
, "Simplified relational ");
9406 print_gimple_stmt (dump_file
, stmt
, 0, 0);
9407 fprintf (dump_file
, " into ");
9410 gimple_cond_set_code (stmt
, NE_EXPR
);
9411 gimple_cond_set_lhs (stmt
, op0
);
9412 gimple_cond_set_rhs (stmt
, new_tree
);
9418 print_gimple_stmt (dump_file
, stmt
, 0, 0);
9419 fprintf (dump_file
, "\n");
9422 if (sop
&& issue_strict_overflow_warning (wc
))
9424 location_t location
= input_location
;
9425 if (gimple_has_location (stmt
))
9426 location
= gimple_location (stmt
);
9428 warning_at (location
, OPT_Wstrict_overflow
,
9429 "assuming signed overflow does not occur when "
9430 "simplifying conditional");
9438 /* If we have a comparison of an SSA_NAME (OP0) against a constant,
9439 see if OP0 was set by a type conversion where the source of
9440 the conversion is another SSA_NAME with a range that fits
9441 into the range of OP0's type.
9443 If so, the conversion is redundant as the earlier SSA_NAME can be
9444 used for the comparison directly if we just massage the constant in the
9446 if (TREE_CODE (op0
) == SSA_NAME
9447 && TREE_CODE (op1
) == INTEGER_CST
)
9449 gimple
*def_stmt
= SSA_NAME_DEF_STMT (op0
);
9452 if (!is_gimple_assign (def_stmt
)
9453 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt
)))
9456 innerop
= gimple_assign_rhs1 (def_stmt
);
9458 if (TREE_CODE (innerop
) == SSA_NAME
9459 && !POINTER_TYPE_P (TREE_TYPE (innerop
)))
9461 value_range
*vr
= get_value_range (innerop
);
9463 if (range_int_cst_p (vr
)
9464 && range_fits_type_p (vr
,
9465 TYPE_PRECISION (TREE_TYPE (op0
)),
9466 TYPE_SIGN (TREE_TYPE (op0
)))
9467 && int_fits_type_p (op1
, TREE_TYPE (innerop
))
9468 /* The range must not have overflowed, or if it did overflow
9469 we must not be wrapping/trapping overflow and optimizing
9470 with strict overflow semantics. */
9471 && ((!is_negative_overflow_infinity (vr
->min
)
9472 && !is_positive_overflow_infinity (vr
->max
))
9473 || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (innerop
))))
9475 /* If the range overflowed and the user has asked for warnings
9476 when strict overflow semantics were used to optimize code,
9477 issue an appropriate warning. */
9478 if (cond_code
!= EQ_EXPR
&& cond_code
!= NE_EXPR
9479 && (is_negative_overflow_infinity (vr
->min
)
9480 || is_positive_overflow_infinity (vr
->max
))
9481 && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_CONDITIONAL
))
9483 location_t location
;
9485 if (!gimple_has_location (stmt
))
9486 location
= input_location
;
9488 location
= gimple_location (stmt
);
9489 warning_at (location
, OPT_Wstrict_overflow
,
9490 "assuming signed overflow does not occur when "
9491 "simplifying conditional");
9494 tree newconst
= fold_convert (TREE_TYPE (innerop
), op1
);
9495 gimple_cond_set_lhs (stmt
, innerop
);
9496 gimple_cond_set_rhs (stmt
, newconst
);
9505 /* Simplify a switch statement using the value range of the switch
9509 simplify_switch_using_ranges (gswitch
*stmt
)
9511 tree op
= gimple_switch_index (stmt
);
9516 size_t i
= 0, j
= 0, n
, n2
;
9519 size_t k
= 1, l
= 0;
9521 if (TREE_CODE (op
) == SSA_NAME
)
9523 vr
= get_value_range (op
);
9525 /* We can only handle integer ranges. */
9526 if ((vr
->type
!= VR_RANGE
9527 && vr
->type
!= VR_ANTI_RANGE
)
9528 || symbolic_range_p (vr
))
9531 /* Find case label for min/max of the value range. */
9532 take_default
= !find_case_label_ranges (stmt
, vr
, &i
, &j
, &k
, &l
);
9534 else if (TREE_CODE (op
) == INTEGER_CST
)
9536 take_default
= !find_case_label_index (stmt
, 1, op
, &i
);
9550 n
= gimple_switch_num_labels (stmt
);
9552 /* Bail out if this is just all edges taken. */
9558 /* Build a new vector of taken case labels. */
9559 vec2
= make_tree_vec (j
- i
+ 1 + l
- k
+ 1 + (int)take_default
);
9562 /* Add the default edge, if necessary. */
9564 TREE_VEC_ELT (vec2
, n2
++) = gimple_switch_default_label (stmt
);
9566 for (; i
<= j
; ++i
, ++n2
)
9567 TREE_VEC_ELT (vec2
, n2
) = gimple_switch_label (stmt
, i
);
9569 for (; k
<= l
; ++k
, ++n2
)
9570 TREE_VEC_ELT (vec2
, n2
) = gimple_switch_label (stmt
, k
);
9572 /* Mark needed edges. */
9573 for (i
= 0; i
< n2
; ++i
)
9575 e
= find_edge (gimple_bb (stmt
),
9576 label_to_block (CASE_LABEL (TREE_VEC_ELT (vec2
, i
))));
9577 e
->aux
= (void *)-1;
9580 /* Queue not needed edges for later removal. */
9581 FOR_EACH_EDGE (e
, ei
, gimple_bb (stmt
)->succs
)
9583 if (e
->aux
== (void *)-1)
9589 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
9591 fprintf (dump_file
, "removing unreachable case label\n");
9593 to_remove_edges
.safe_push (e
);
9594 e
->flags
&= ~EDGE_EXECUTABLE
;
9597 /* And queue an update for the stmt. */
9600 to_update_switch_stmts
.safe_push (su
);
9604 /* Simplify an integral conversion from an SSA name in STMT. */
9607 simplify_conversion_using_ranges (gimple
*stmt
)
9609 tree innerop
, middleop
, finaltype
;
9611 value_range
*innervr
;
9612 signop inner_sgn
, middle_sgn
, final_sgn
;
9613 unsigned inner_prec
, middle_prec
, final_prec
;
9614 widest_int innermin
, innermed
, innermax
, middlemin
, middlemed
, middlemax
;
9616 finaltype
= TREE_TYPE (gimple_assign_lhs (stmt
));
9617 if (!INTEGRAL_TYPE_P (finaltype
))
9619 middleop
= gimple_assign_rhs1 (stmt
);
9620 def_stmt
= SSA_NAME_DEF_STMT (middleop
);
9621 if (!is_gimple_assign (def_stmt
)
9622 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt
)))
9624 innerop
= gimple_assign_rhs1 (def_stmt
);
9625 if (TREE_CODE (innerop
) != SSA_NAME
9626 || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (innerop
))
9629 /* Get the value-range of the inner operand. */
9630 innervr
= get_value_range (innerop
);
9631 if (innervr
->type
!= VR_RANGE
9632 || TREE_CODE (innervr
->min
) != INTEGER_CST
9633 || TREE_CODE (innervr
->max
) != INTEGER_CST
)
9636 /* Simulate the conversion chain to check if the result is equal if
9637 the middle conversion is removed. */
9638 innermin
= wi::to_widest (innervr
->min
);
9639 innermax
= wi::to_widest (innervr
->max
);
9641 inner_prec
= TYPE_PRECISION (TREE_TYPE (innerop
));
9642 middle_prec
= TYPE_PRECISION (TREE_TYPE (middleop
));
9643 final_prec
= TYPE_PRECISION (finaltype
);
9645 /* If the first conversion is not injective, the second must not
9647 if (wi::gtu_p (innermax
- innermin
,
9648 wi::mask
<widest_int
> (middle_prec
, false))
9649 && middle_prec
< final_prec
)
9651 /* We also want a medium value so that we can track the effect that
9652 narrowing conversions with sign change have. */
9653 inner_sgn
= TYPE_SIGN (TREE_TYPE (innerop
));
9654 if (inner_sgn
== UNSIGNED
)
9655 innermed
= wi::shifted_mask
<widest_int
> (1, inner_prec
- 1, false);
9658 if (wi::cmp (innermin
, innermed
, inner_sgn
) >= 0
9659 || wi::cmp (innermed
, innermax
, inner_sgn
) >= 0)
9660 innermed
= innermin
;
9662 middle_sgn
= TYPE_SIGN (TREE_TYPE (middleop
));
9663 middlemin
= wi::ext (innermin
, middle_prec
, middle_sgn
);
9664 middlemed
= wi::ext (innermed
, middle_prec
, middle_sgn
);
9665 middlemax
= wi::ext (innermax
, middle_prec
, middle_sgn
);
9667 /* Require that the final conversion applied to both the original
9668 and the intermediate range produces the same result. */
9669 final_sgn
= TYPE_SIGN (finaltype
);
9670 if (wi::ext (middlemin
, final_prec
, final_sgn
)
9671 != wi::ext (innermin
, final_prec
, final_sgn
)
9672 || wi::ext (middlemed
, final_prec
, final_sgn
)
9673 != wi::ext (innermed
, final_prec
, final_sgn
)
9674 || wi::ext (middlemax
, final_prec
, final_sgn
)
9675 != wi::ext (innermax
, final_prec
, final_sgn
))
9678 gimple_assign_set_rhs1 (stmt
, innerop
);
9683 /* Simplify a conversion from integral SSA name to float in STMT. */
9686 simplify_float_conversion_using_ranges (gimple_stmt_iterator
*gsi
,
9689 tree rhs1
= gimple_assign_rhs1 (stmt
);
9690 value_range
*vr
= get_value_range (rhs1
);
9691 machine_mode fltmode
= TYPE_MODE (TREE_TYPE (gimple_assign_lhs (stmt
)));
9696 /* We can only handle constant ranges. */
9697 if (vr
->type
!= VR_RANGE
9698 || TREE_CODE (vr
->min
) != INTEGER_CST
9699 || TREE_CODE (vr
->max
) != INTEGER_CST
)
9702 /* First check if we can use a signed type in place of an unsigned. */
9703 if (TYPE_UNSIGNED (TREE_TYPE (rhs1
))
9704 && (can_float_p (fltmode
, TYPE_MODE (TREE_TYPE (rhs1
)), 0)
9705 != CODE_FOR_nothing
)
9706 && range_fits_type_p (vr
, TYPE_PRECISION (TREE_TYPE (rhs1
)), SIGNED
))
9707 mode
= TYPE_MODE (TREE_TYPE (rhs1
));
9708 /* If we can do the conversion in the current input mode do nothing. */
9709 else if (can_float_p (fltmode
, TYPE_MODE (TREE_TYPE (rhs1
)),
9710 TYPE_UNSIGNED (TREE_TYPE (rhs1
))) != CODE_FOR_nothing
)
9712 /* Otherwise search for a mode we can use, starting from the narrowest
9713 integer mode available. */
9716 mode
= GET_CLASS_NARROWEST_MODE (MODE_INT
);
9719 /* If we cannot do a signed conversion to float from mode
9720 or if the value-range does not fit in the signed type
9721 try with a wider mode. */
9722 if (can_float_p (fltmode
, mode
, 0) != CODE_FOR_nothing
9723 && range_fits_type_p (vr
, GET_MODE_PRECISION (mode
), SIGNED
))
9726 mode
= GET_MODE_WIDER_MODE (mode
);
9727 /* But do not widen the input. Instead leave that to the
9728 optabs expansion code. */
9729 if (GET_MODE_PRECISION (mode
) > TYPE_PRECISION (TREE_TYPE (rhs1
)))
9732 while (mode
!= VOIDmode
);
9733 if (mode
== VOIDmode
)
9737 /* It works, insert a truncation or sign-change before the
9738 float conversion. */
9739 tem
= make_ssa_name (build_nonstandard_integer_type
9740 (GET_MODE_PRECISION (mode
), 0));
9741 conv
= gimple_build_assign (tem
, NOP_EXPR
, rhs1
);
9742 gsi_insert_before (gsi
, conv
, GSI_SAME_STMT
);
9743 gimple_assign_set_rhs1 (stmt
, tem
);
9749 /* Simplify an internal fn call using ranges if possible. */
9752 simplify_internal_call_using_ranges (gimple_stmt_iterator
*gsi
, gimple
*stmt
)
9754 enum tree_code subcode
;
9755 bool is_ubsan
= false;
9757 switch (gimple_call_internal_fn (stmt
))
9759 case IFN_UBSAN_CHECK_ADD
:
9760 subcode
= PLUS_EXPR
;
9763 case IFN_UBSAN_CHECK_SUB
:
9764 subcode
= MINUS_EXPR
;
9767 case IFN_UBSAN_CHECK_MUL
:
9768 subcode
= MULT_EXPR
;
9771 case IFN_ADD_OVERFLOW
:
9772 subcode
= PLUS_EXPR
;
9774 case IFN_SUB_OVERFLOW
:
9775 subcode
= MINUS_EXPR
;
9777 case IFN_MUL_OVERFLOW
:
9778 subcode
= MULT_EXPR
;
9784 tree op0
= gimple_call_arg (stmt
, 0);
9785 tree op1
= gimple_call_arg (stmt
, 1);
9788 type
= TREE_TYPE (op0
);
9789 else if (gimple_call_lhs (stmt
) == NULL_TREE
)
9792 type
= TREE_TYPE (TREE_TYPE (gimple_call_lhs (stmt
)));
9793 if (!check_for_binary_op_overflow (subcode
, type
, op0
, op1
, &ovf
)
9794 || (is_ubsan
&& ovf
))
9798 location_t loc
= gimple_location (stmt
);
9800 g
= gimple_build_assign (gimple_call_lhs (stmt
), subcode
, op0
, op1
);
9803 int prec
= TYPE_PRECISION (type
);
9806 || !useless_type_conversion_p (type
, TREE_TYPE (op0
))
9807 || !useless_type_conversion_p (type
, TREE_TYPE (op1
)))
9808 utype
= build_nonstandard_integer_type (prec
, 1);
9809 if (TREE_CODE (op0
) == INTEGER_CST
)
9810 op0
= fold_convert (utype
, op0
);
9811 else if (!useless_type_conversion_p (utype
, TREE_TYPE (op0
)))
9813 g
= gimple_build_assign (make_ssa_name (utype
), NOP_EXPR
, op0
);
9814 gimple_set_location (g
, loc
);
9815 gsi_insert_before (gsi
, g
, GSI_SAME_STMT
);
9816 op0
= gimple_assign_lhs (g
);
9818 if (TREE_CODE (op1
) == INTEGER_CST
)
9819 op1
= fold_convert (utype
, op1
);
9820 else if (!useless_type_conversion_p (utype
, TREE_TYPE (op1
)))
9822 g
= gimple_build_assign (make_ssa_name (utype
), NOP_EXPR
, op1
);
9823 gimple_set_location (g
, loc
);
9824 gsi_insert_before (gsi
, g
, GSI_SAME_STMT
);
9825 op1
= gimple_assign_lhs (g
);
9827 g
= gimple_build_assign (make_ssa_name (utype
), subcode
, op0
, op1
);
9828 gimple_set_location (g
, loc
);
9829 gsi_insert_before (gsi
, g
, GSI_SAME_STMT
);
9832 g
= gimple_build_assign (make_ssa_name (type
), NOP_EXPR
,
9833 gimple_assign_lhs (g
));
9834 gimple_set_location (g
, loc
);
9835 gsi_insert_before (gsi
, g
, GSI_SAME_STMT
);
9837 g
= gimple_build_assign (gimple_call_lhs (stmt
), COMPLEX_EXPR
,
9838 gimple_assign_lhs (g
),
9839 build_int_cst (type
, ovf
));
9841 gimple_set_location (g
, loc
);
9842 gsi_replace (gsi
, g
, false);
9846 /* Simplify STMT using ranges if possible. */
9849 simplify_stmt_using_ranges (gimple_stmt_iterator
*gsi
)
9851 gimple
*stmt
= gsi_stmt (*gsi
);
9852 if (is_gimple_assign (stmt
))
9854 enum tree_code rhs_code
= gimple_assign_rhs_code (stmt
);
9855 tree rhs1
= gimple_assign_rhs1 (stmt
);
9861 /* Transform EQ_EXPR, NE_EXPR into BIT_XOR_EXPR or identity
9862 if the RHS is zero or one, and the LHS are known to be boolean
9864 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
9865 return simplify_truth_ops_using_ranges (gsi
, stmt
);
9868 /* Transform TRUNC_DIV_EXPR and TRUNC_MOD_EXPR into RSHIFT_EXPR
9869 and BIT_AND_EXPR respectively if the first operand is greater
9870 than zero and the second operand is an exact power of two.
9871 Also optimize TRUNC_MOD_EXPR away if the second operand is
9872 constant and the first operand already has the right value
9874 case TRUNC_DIV_EXPR
:
9875 case TRUNC_MOD_EXPR
:
9876 if (TREE_CODE (rhs1
) == SSA_NAME
9877 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
9878 return simplify_div_or_mod_using_ranges (stmt
);
9881 /* Transform ABS (X) into X or -X as appropriate. */
9883 if (TREE_CODE (rhs1
) == SSA_NAME
9884 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
9885 return simplify_abs_using_ranges (stmt
);
9890 /* Optimize away BIT_AND_EXPR and BIT_IOR_EXPR
9891 if all the bits being cleared are already cleared or
9892 all the bits being set are already set. */
9893 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
9894 return simplify_bit_ops_using_ranges (gsi
, stmt
);
9898 if (TREE_CODE (rhs1
) == SSA_NAME
9899 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
9900 return simplify_conversion_using_ranges (stmt
);
9904 if (TREE_CODE (rhs1
) == SSA_NAME
9905 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
9906 return simplify_float_conversion_using_ranges (gsi
, stmt
);
9911 return simplify_min_or_max_using_ranges (stmt
);
9918 else if (gimple_code (stmt
) == GIMPLE_COND
)
9919 return simplify_cond_using_ranges (as_a
<gcond
*> (stmt
));
9920 else if (gimple_code (stmt
) == GIMPLE_SWITCH
)
9921 return simplify_switch_using_ranges (as_a
<gswitch
*> (stmt
));
9922 else if (is_gimple_call (stmt
)
9923 && gimple_call_internal_p (stmt
))
9924 return simplify_internal_call_using_ranges (gsi
, stmt
);
9929 /* If the statement pointed by SI has a predicate whose value can be
9930 computed using the value range information computed by VRP, compute
9931 its value and return true. Otherwise, return false. */
9934 fold_predicate_in (gimple_stmt_iterator
*si
)
9936 bool assignment_p
= false;
9938 gimple
*stmt
= gsi_stmt (*si
);
9940 if (is_gimple_assign (stmt
)
9941 && TREE_CODE_CLASS (gimple_assign_rhs_code (stmt
)) == tcc_comparison
)
9943 assignment_p
= true;
9944 val
= vrp_evaluate_conditional (gimple_assign_rhs_code (stmt
),
9945 gimple_assign_rhs1 (stmt
),
9946 gimple_assign_rhs2 (stmt
),
9949 else if (gcond
*cond_stmt
= dyn_cast
<gcond
*> (stmt
))
9950 val
= vrp_evaluate_conditional (gimple_cond_code (cond_stmt
),
9951 gimple_cond_lhs (cond_stmt
),
9952 gimple_cond_rhs (cond_stmt
),
9960 val
= fold_convert (gimple_expr_type (stmt
), val
);
9964 fprintf (dump_file
, "Folding predicate ");
9965 print_gimple_expr (dump_file
, stmt
, 0, 0);
9966 fprintf (dump_file
, " to ");
9967 print_generic_expr (dump_file
, val
, 0);
9968 fprintf (dump_file
, "\n");
9971 if (is_gimple_assign (stmt
))
9972 gimple_assign_set_rhs_from_tree (si
, val
);
9975 gcc_assert (gimple_code (stmt
) == GIMPLE_COND
);
9976 gcond
*cond_stmt
= as_a
<gcond
*> (stmt
);
9977 if (integer_zerop (val
))
9978 gimple_cond_make_false (cond_stmt
);
9979 else if (integer_onep (val
))
9980 gimple_cond_make_true (cond_stmt
);
9991 /* Callback for substitute_and_fold folding the stmt at *SI. */
9994 vrp_fold_stmt (gimple_stmt_iterator
*si
)
9996 if (fold_predicate_in (si
))
9999 return simplify_stmt_using_ranges (si
);
10002 /* Unwindable const/copy equivalences. */
10003 const_and_copies
*equiv_stack
;
10005 /* A trivial wrapper so that we can present the generic jump threading
10006 code with a simple API for simplifying statements. STMT is the
10007 statement we want to simplify, WITHIN_STMT provides the location
10008 for any overflow warnings. */
10011 simplify_stmt_for_jump_threading (gimple
*stmt
, gimple
*within_stmt
,
10012 class avail_exprs_stack
*avail_exprs_stack ATTRIBUTE_UNUSED
)
10014 if (gcond
*cond_stmt
= dyn_cast
<gcond
*> (stmt
))
10015 return vrp_evaluate_conditional (gimple_cond_code (cond_stmt
),
10016 gimple_cond_lhs (cond_stmt
),
10017 gimple_cond_rhs (cond_stmt
),
10020 if (gassign
*assign_stmt
= dyn_cast
<gassign
*> (stmt
))
10022 value_range new_vr
= VR_INITIALIZER
;
10023 tree lhs
= gimple_assign_lhs (assign_stmt
);
10025 if (TREE_CODE (lhs
) == SSA_NAME
10026 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs
))
10027 || POINTER_TYPE_P (TREE_TYPE (lhs
))))
10029 extract_range_from_assignment (&new_vr
, assign_stmt
);
10030 if (range_int_cst_singleton_p (&new_vr
))
10038 /* Blocks which have more than one predecessor and more than
10039 one successor present jump threading opportunities, i.e.,
10040 when the block is reached from a specific predecessor, we
10041 may be able to determine which of the outgoing edges will
10042 be traversed. When this optimization applies, we are able
10043 to avoid conditionals at runtime and we may expose secondary
10044 optimization opportunities.
10046 This routine is effectively a driver for the generic jump
10047 threading code. It basically just presents the generic code
10048 with edges that may be suitable for jump threading.
10050 Unlike DOM, we do not iterate VRP if jump threading was successful.
10051 While iterating may expose new opportunities for VRP, it is expected
10052 those opportunities would be very limited and the compile time cost
10053 to expose those opportunities would be significant.
10055 As jump threading opportunities are discovered, they are registered
10056 for later realization. */
10059 identify_jump_threads (void)
10066 /* Ugh. When substituting values earlier in this pass we can
10067 wipe the dominance information. So rebuild the dominator
10068 information as we need it within the jump threading code. */
10069 calculate_dominance_info (CDI_DOMINATORS
);
10071 /* We do not allow VRP information to be used for jump threading
10072 across a back edge in the CFG. Otherwise it becomes too
10073 difficult to avoid eliminating loop exit tests. Of course
10074 EDGE_DFS_BACK is not accurate at this time so we have to
10076 mark_dfs_back_edges ();
10078 /* Do not thread across edges we are about to remove. Just marking
10079 them as EDGE_IGNORE will do. */
10080 FOR_EACH_VEC_ELT (to_remove_edges
, i
, e
)
10081 e
->flags
|= EDGE_IGNORE
;
10083 /* Allocate our unwinder stack to unwind any temporary equivalences
10084 that might be recorded. */
10085 equiv_stack
= new const_and_copies ();
10087 /* To avoid lots of silly node creation, we create a single
10088 conditional and just modify it in-place when attempting to
10090 dummy
= gimple_build_cond (EQ_EXPR
,
10091 integer_zero_node
, integer_zero_node
,
10094 /* Walk through all the blocks finding those which present a
10095 potential jump threading opportunity. We could set this up
10096 as a dominator walker and record data during the walk, but
10097 I doubt it's worth the effort for the classes of jump
10098 threading opportunities we are trying to identify at this
10099 point in compilation. */
10100 FOR_EACH_BB_FN (bb
, cfun
)
10104 /* If the generic jump threading code does not find this block
10105 interesting, then there is nothing to do. */
10106 if (! potentially_threadable_block (bb
))
10109 last
= last_stmt (bb
);
10111 /* We're basically looking for a switch or any kind of conditional with
10112 integral or pointer type arguments. Note the type of the second
10113 argument will be the same as the first argument, so no need to
10114 check it explicitly.
10116 We also handle the case where there are no statements in the
10117 block. This come up with forwarder blocks that are not
10118 optimized away because they lead to a loop header. But we do
10119 want to thread through them as we can sometimes thread to the
10120 loop exit which is obviously profitable. */
10122 || gimple_code (last
) == GIMPLE_SWITCH
10123 || (gimple_code (last
) == GIMPLE_COND
10124 && TREE_CODE (gimple_cond_lhs (last
)) == SSA_NAME
10125 && (INTEGRAL_TYPE_P (TREE_TYPE (gimple_cond_lhs (last
)))
10126 || POINTER_TYPE_P (TREE_TYPE (gimple_cond_lhs (last
))))
10127 && (TREE_CODE (gimple_cond_rhs (last
)) == SSA_NAME
10128 || is_gimple_min_invariant (gimple_cond_rhs (last
)))))
10132 /* We've got a block with multiple predecessors and multiple
10133 successors which also ends in a suitable conditional or
10134 switch statement. For each predecessor, see if we can thread
10135 it to a specific successor. */
10136 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
10138 /* Do not thread across edges marked to ignoreor abnormal
10139 edges in the CFG. */
10140 if (e
->flags
& (EDGE_IGNORE
| EDGE_COMPLEX
))
10143 thread_across_edge (dummy
, e
, true, equiv_stack
, NULL
,
10144 simplify_stmt_for_jump_threading
);
10149 /* Clear EDGE_IGNORE. */
10150 FOR_EACH_VEC_ELT (to_remove_edges
, i
, e
)
10151 e
->flags
&= ~EDGE_IGNORE
;
10153 /* We do not actually update the CFG or SSA graphs at this point as
10154 ASSERT_EXPRs are still in the IL and cfg cleanup code does not yet
10155 handle ASSERT_EXPRs gracefully. */
10158 /* We identified all the jump threading opportunities earlier, but could
10159 not transform the CFG at that time. This routine transforms the
10160 CFG and arranges for the dominator tree to be rebuilt if necessary.
10162 Note the SSA graph update will occur during the normal TODO
10163 processing by the pass manager. */
10165 finalize_jump_threads (void)
10167 thread_through_all_blocks (false);
10168 delete equiv_stack
;
10172 /* Traverse all the blocks folding conditionals with known ranges. */
10175 vrp_finalize (void)
10179 values_propagated
= true;
10183 fprintf (dump_file
, "\nValue ranges after VRP:\n\n");
10184 dump_all_value_ranges (dump_file
);
10185 fprintf (dump_file
, "\n");
10188 substitute_and_fold (op_with_constant_singleton_value_range
,
10189 vrp_fold_stmt
, false);
10191 if (warn_array_bounds
&& first_pass_instance
)
10192 check_all_array_refs ();
10194 /* We must identify jump threading opportunities before we release
10195 the datastructures built by VRP. */
10196 identify_jump_threads ();
10198 /* Set value range to non pointer SSA_NAMEs. */
10199 for (i
= 0; i
< num_vr_values
; i
++)
10202 tree name
= ssa_name (i
);
10205 || POINTER_TYPE_P (TREE_TYPE (name
))
10206 || (vr_value
[i
]->type
== VR_VARYING
)
10207 || (vr_value
[i
]->type
== VR_UNDEFINED
))
10210 if ((TREE_CODE (vr_value
[i
]->min
) == INTEGER_CST
)
10211 && (TREE_CODE (vr_value
[i
]->max
) == INTEGER_CST
)
10212 && (vr_value
[i
]->type
== VR_RANGE
10213 || vr_value
[i
]->type
== VR_ANTI_RANGE
))
10214 set_range_info (name
, vr_value
[i
]->type
, vr_value
[i
]->min
,
10218 /* Free allocated memory. */
10219 for (i
= 0; i
< num_vr_values
; i
++)
10222 BITMAP_FREE (vr_value
[i
]->equiv
);
10223 free (vr_value
[i
]);
10227 free (vr_phi_edge_counts
);
10229 /* So that we can distinguish between VRP data being available
10230 and not available. */
10232 vr_phi_edge_counts
= NULL
;
10236 /* Main entry point to VRP (Value Range Propagation). This pass is
10237 loosely based on J. R. C. Patterson, ``Accurate Static Branch
10238 Prediction by Value Range Propagation,'' in SIGPLAN Conference on
10239 Programming Language Design and Implementation, pp. 67-78, 1995.
10240 Also available at http://citeseer.ist.psu.edu/patterson95accurate.html
10242 This is essentially an SSA-CCP pass modified to deal with ranges
10243 instead of constants.
10245 While propagating ranges, we may find that two or more SSA name
10246 have equivalent, though distinct ranges. For instance,
10249 2 p_4 = ASSERT_EXPR <p_3, p_3 != 0>
10251 4 p_5 = ASSERT_EXPR <p_4, p_4 == q_2>;
10255 In the code above, pointer p_5 has range [q_2, q_2], but from the
10256 code we can also determine that p_5 cannot be NULL and, if q_2 had
10257 a non-varying range, p_5's range should also be compatible with it.
10259 These equivalences are created by two expressions: ASSERT_EXPR and
10260 copy operations. Since p_5 is an assertion on p_4, and p_4 was the
10261 result of another assertion, then we can use the fact that p_5 and
10262 p_4 are equivalent when evaluating p_5's range.
10264 Together with value ranges, we also propagate these equivalences
10265 between names so that we can take advantage of information from
10266 multiple ranges when doing final replacement. Note that this
10267 equivalency relation is transitive but not symmetric.
10269 In the example above, p_5 is equivalent to p_4, q_2 and p_3, but we
10270 cannot assert that q_2 is equivalent to p_5 because q_2 may be used
10271 in contexts where that assertion does not hold (e.g., in line 6).
10273 TODO, the main difference between this pass and Patterson's is that
10274 we do not propagate edge probabilities. We only compute whether
10275 edges can be taken or not. That is, instead of having a spectrum
10276 of jump probabilities between 0 and 1, we only deal with 0, 1 and
10277 DON'T KNOW. In the future, it may be worthwhile to propagate
10278 probabilities to aid branch prediction. */
10280 static unsigned int
10287 loop_optimizer_init (LOOPS_NORMAL
| LOOPS_HAVE_RECORDED_EXITS
);
10288 rewrite_into_loop_closed_ssa (NULL
, TODO_update_ssa
);
10289 scev_initialize ();
10291 /* ??? This ends up using stale EDGE_DFS_BACK for liveness computation.
10292 Inserting assertions may split edges which will invalidate
10294 insert_range_assertions ();
10296 to_remove_edges
.create (10);
10297 to_update_switch_stmts
.create (5);
10298 threadedge_initialize_values ();
10300 /* For visiting PHI nodes we need EDGE_DFS_BACK computed. */
10301 mark_dfs_back_edges ();
10304 ssa_propagate (vrp_visit_stmt
, vrp_visit_phi_node
);
10307 free_numbers_of_iterations_estimates (cfun
);
10309 /* ASSERT_EXPRs must be removed before finalizing jump threads
10310 as finalizing jump threads calls the CFG cleanup code which
10311 does not properly handle ASSERT_EXPRs. */
10312 remove_range_assertions ();
10314 /* If we exposed any new variables, go ahead and put them into
10315 SSA form now, before we handle jump threading. This simplifies
10316 interactions between rewriting of _DECL nodes into SSA form
10317 and rewriting SSA_NAME nodes into SSA form after block
10318 duplication and CFG manipulation. */
10319 update_ssa (TODO_update_ssa
);
10321 finalize_jump_threads ();
10323 /* Remove dead edges from SWITCH_EXPR optimization. This leaves the
10324 CFG in a broken state and requires a cfg_cleanup run. */
10325 FOR_EACH_VEC_ELT (to_remove_edges
, i
, e
)
10327 /* Update SWITCH_EXPR case label vector. */
10328 FOR_EACH_VEC_ELT (to_update_switch_stmts
, i
, su
)
10331 size_t n
= TREE_VEC_LENGTH (su
->vec
);
10333 gimple_switch_set_num_labels (su
->stmt
, n
);
10334 for (j
= 0; j
< n
; j
++)
10335 gimple_switch_set_label (su
->stmt
, j
, TREE_VEC_ELT (su
->vec
, j
));
10336 /* As we may have replaced the default label with a regular one
10337 make sure to make it a real default label again. This ensures
10338 optimal expansion. */
10339 label
= gimple_switch_label (su
->stmt
, 0);
10340 CASE_LOW (label
) = NULL_TREE
;
10341 CASE_HIGH (label
) = NULL_TREE
;
10344 if (to_remove_edges
.length () > 0)
10346 free_dominance_info (CDI_DOMINATORS
);
10347 loops_state_set (LOOPS_NEED_FIXUP
);
10350 to_remove_edges
.release ();
10351 to_update_switch_stmts
.release ();
10352 threadedge_finalize_values ();
10355 loop_optimizer_finalize ();
10361 const pass_data pass_data_vrp
=
10363 GIMPLE_PASS
, /* type */
10365 OPTGROUP_NONE
, /* optinfo_flags */
10366 TV_TREE_VRP
, /* tv_id */
10367 PROP_ssa
, /* properties_required */
10368 0, /* properties_provided */
10369 0, /* properties_destroyed */
10370 0, /* todo_flags_start */
10371 ( TODO_cleanup_cfg
| TODO_update_ssa
), /* todo_flags_finish */
10374 class pass_vrp
: public gimple_opt_pass
10377 pass_vrp (gcc::context
*ctxt
)
10378 : gimple_opt_pass (pass_data_vrp
, ctxt
)
10381 /* opt_pass methods: */
10382 opt_pass
* clone () { return new pass_vrp (m_ctxt
); }
10383 virtual bool gate (function
*) { return flag_tree_vrp
!= 0; }
10384 virtual unsigned int execute (function
*) { return execute_vrp (); }
10386 }; // class pass_vrp
10388 } // anon namespace
10391 make_pass_vrp (gcc::context
*ctxt
)
10393 return new pass_vrp (ctxt
);