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"
32 #include "fold-const.h"
33 #include "stor-layout.h"
36 #include "internal-fn.h"
37 #include "gimple-fold.h"
39 #include "gimple-iterator.h"
40 #include "gimple-walk.h"
42 #include "tree-ssa-loop-manip.h"
43 #include "tree-ssa-loop-niter.h"
44 #include "tree-ssa-loop.h"
45 #include "tree-into-ssa.h"
47 #include "tree-pass.h"
48 #include "tree-dump.h"
49 #include "gimple-pretty-print.h"
50 #include "diagnostic-core.h"
53 #include "tree-scalar-evolution.h"
54 #include "tree-ssa-propagate.h"
55 #include "tree-chrec.h"
56 #include "tree-ssa-threadupdate.h"
57 #include "insn-config.h"
65 #include "insn-codes.h"
67 #include "tree-ssa-scopedtables.h"
68 #include "tree-ssa-threadedge.h"
72 /* Range of values that can be associated with an SSA_NAME after VRP
76 /* Lattice value represented by this range. */
77 enum value_range_type type
;
79 /* Minimum and maximum values represented by this range. These
80 values should be interpreted as follows:
82 - If TYPE is VR_UNDEFINED or VR_VARYING then MIN and MAX must
85 - If TYPE == VR_RANGE then MIN holds the minimum value and
86 MAX holds the maximum value of the range [MIN, MAX].
88 - If TYPE == ANTI_RANGE the variable is known to NOT
89 take any values in the range [MIN, MAX]. */
93 /* Set of SSA names whose value ranges are equivalent to this one.
94 This set is only valid when TYPE is VR_RANGE or VR_ANTI_RANGE. */
98 typedef struct value_range_d value_range_t
;
100 #define VR_INITIALIZER { VR_UNDEFINED, NULL_TREE, NULL_TREE, NULL }
102 /* Set of SSA names found live during the RPO traversal of the function
103 for still active basic-blocks. */
104 static sbitmap
*live
;
106 /* Return true if the SSA name NAME is live on the edge E. */
109 live_on_edge (edge e
, tree name
)
111 return (live
[e
->dest
->index
]
112 && bitmap_bit_p (live
[e
->dest
->index
], SSA_NAME_VERSION (name
)));
115 /* Local functions. */
116 static int compare_values (tree val1
, tree val2
);
117 static int compare_values_warnv (tree val1
, tree val2
, bool *);
118 static void vrp_meet (value_range_t
*, value_range_t
*);
119 static void vrp_intersect_ranges (value_range_t
*, value_range_t
*);
120 static tree
vrp_evaluate_conditional_warnv_with_ops (enum tree_code
,
121 tree
, tree
, bool, bool *,
124 /* Location information for ASSERT_EXPRs. Each instance of this
125 structure describes an ASSERT_EXPR for an SSA name. Since a single
126 SSA name may have more than one assertion associated with it, these
127 locations are kept in a linked list attached to the corresponding
131 /* Basic block where the assertion would be inserted. */
134 /* Some assertions need to be inserted on an edge (e.g., assertions
135 generated by COND_EXPRs). In those cases, BB will be NULL. */
138 /* Pointer to the statement that generated this assertion. */
139 gimple_stmt_iterator si
;
141 /* Predicate code for the ASSERT_EXPR. Must be COMPARISON_CLASS_P. */
142 enum tree_code comp_code
;
144 /* Value being compared against. */
147 /* Expression to compare. */
150 /* Next node in the linked list. */
154 /* If bit I is present, it means that SSA name N_i has a list of
155 assertions that should be inserted in the IL. */
156 static bitmap need_assert_for
;
158 /* Array of locations lists where to insert assertions. ASSERTS_FOR[I]
159 holds a list of ASSERT_LOCUS_T nodes that describe where
160 ASSERT_EXPRs for SSA name N_I should be inserted. */
161 static assert_locus
**asserts_for
;
163 /* Value range array. After propagation, VR_VALUE[I] holds the range
164 of values that SSA name N_I may take. */
165 static unsigned num_vr_values
;
166 static value_range_t
**vr_value
;
167 static bool values_propagated
;
169 /* For a PHI node which sets SSA name N_I, VR_COUNTS[I] holds the
170 number of executable edges we saw the last time we visited the
172 static int *vr_phi_edge_counts
;
174 struct switch_update
{
179 static vec
<edge
> to_remove_edges
;
180 static vec
<switch_update
> to_update_switch_stmts
;
183 /* Return the maximum value for TYPE. */
186 vrp_val_max (const_tree type
)
188 if (!INTEGRAL_TYPE_P (type
))
191 return TYPE_MAX_VALUE (type
);
194 /* Return the minimum value for TYPE. */
197 vrp_val_min (const_tree type
)
199 if (!INTEGRAL_TYPE_P (type
))
202 return TYPE_MIN_VALUE (type
);
205 /* Return whether VAL is equal to the maximum value of its type. This
206 will be true for a positive overflow infinity. We can't do a
207 simple equality comparison with TYPE_MAX_VALUE because C typedefs
208 and Ada subtypes can produce types whose TYPE_MAX_VALUE is not ==
209 to the integer constant with the same value in the type. */
212 vrp_val_is_max (const_tree val
)
214 tree type_max
= vrp_val_max (TREE_TYPE (val
));
215 return (val
== type_max
216 || (type_max
!= NULL_TREE
217 && operand_equal_p (val
, type_max
, 0)));
220 /* Return whether VAL is equal to the minimum value of its type. This
221 will be true for a negative overflow infinity. */
224 vrp_val_is_min (const_tree val
)
226 tree type_min
= vrp_val_min (TREE_TYPE (val
));
227 return (val
== type_min
228 || (type_min
!= NULL_TREE
229 && operand_equal_p (val
, type_min
, 0)));
233 /* Return whether TYPE should use an overflow infinity distinct from
234 TYPE_{MIN,MAX}_VALUE. We use an overflow infinity value to
235 represent a signed overflow during VRP computations. An infinity
236 is distinct from a half-range, which will go from some number to
237 TYPE_{MIN,MAX}_VALUE. */
240 needs_overflow_infinity (const_tree type
)
242 return INTEGRAL_TYPE_P (type
) && !TYPE_OVERFLOW_WRAPS (type
);
245 /* Return whether TYPE can support our overflow infinity
246 representation: we use the TREE_OVERFLOW flag, which only exists
247 for constants. If TYPE doesn't support this, we don't optimize
248 cases which would require signed overflow--we drop them to
252 supports_overflow_infinity (const_tree type
)
254 tree min
= vrp_val_min (type
), max
= vrp_val_max (type
);
255 #ifdef ENABLE_CHECKING
256 gcc_assert (needs_overflow_infinity (type
));
258 return (min
!= NULL_TREE
259 && CONSTANT_CLASS_P (min
)
261 && CONSTANT_CLASS_P (max
));
264 /* VAL is the maximum or minimum value of a type. Return a
265 corresponding overflow infinity. */
268 make_overflow_infinity (tree val
)
270 gcc_checking_assert (val
!= NULL_TREE
&& CONSTANT_CLASS_P (val
));
271 val
= copy_node (val
);
272 TREE_OVERFLOW (val
) = 1;
276 /* Return a negative overflow infinity for TYPE. */
279 negative_overflow_infinity (tree type
)
281 gcc_checking_assert (supports_overflow_infinity (type
));
282 return make_overflow_infinity (vrp_val_min (type
));
285 /* Return a positive overflow infinity for TYPE. */
288 positive_overflow_infinity (tree type
)
290 gcc_checking_assert (supports_overflow_infinity (type
));
291 return make_overflow_infinity (vrp_val_max (type
));
294 /* Return whether VAL is a negative overflow infinity. */
297 is_negative_overflow_infinity (const_tree val
)
299 return (TREE_OVERFLOW_P (val
)
300 && needs_overflow_infinity (TREE_TYPE (val
))
301 && vrp_val_is_min (val
));
304 /* Return whether VAL is a positive overflow infinity. */
307 is_positive_overflow_infinity (const_tree val
)
309 return (TREE_OVERFLOW_P (val
)
310 && needs_overflow_infinity (TREE_TYPE (val
))
311 && vrp_val_is_max (val
));
314 /* Return whether VAL is a positive or negative overflow infinity. */
317 is_overflow_infinity (const_tree val
)
319 return (TREE_OVERFLOW_P (val
)
320 && needs_overflow_infinity (TREE_TYPE (val
))
321 && (vrp_val_is_min (val
) || vrp_val_is_max (val
)));
324 /* Return whether STMT has a constant rhs that is_overflow_infinity. */
327 stmt_overflow_infinity (gimple stmt
)
329 if (is_gimple_assign (stmt
)
330 && get_gimple_rhs_class (gimple_assign_rhs_code (stmt
)) ==
332 return is_overflow_infinity (gimple_assign_rhs1 (stmt
));
336 /* If VAL is now an overflow infinity, return VAL. Otherwise, return
337 the same value with TREE_OVERFLOW clear. This can be used to avoid
338 confusing a regular value with an overflow value. */
341 avoid_overflow_infinity (tree val
)
343 if (!is_overflow_infinity (val
))
346 if (vrp_val_is_max (val
))
347 return vrp_val_max (TREE_TYPE (val
));
350 gcc_checking_assert (vrp_val_is_min (val
));
351 return vrp_val_min (TREE_TYPE (val
));
356 /* Set value range VR to VR_UNDEFINED. */
359 set_value_range_to_undefined (value_range_t
*vr
)
361 vr
->type
= VR_UNDEFINED
;
362 vr
->min
= vr
->max
= NULL_TREE
;
364 bitmap_clear (vr
->equiv
);
368 /* Set value range VR to VR_VARYING. */
371 set_value_range_to_varying (value_range_t
*vr
)
373 vr
->type
= VR_VARYING
;
374 vr
->min
= vr
->max
= NULL_TREE
;
376 bitmap_clear (vr
->equiv
);
380 /* Set value range VR to {T, MIN, MAX, EQUIV}. */
383 set_value_range (value_range_t
*vr
, enum value_range_type t
, tree min
,
384 tree max
, bitmap equiv
)
386 #if defined ENABLE_CHECKING
387 /* Check the validity of the range. */
388 if (t
== VR_RANGE
|| t
== VR_ANTI_RANGE
)
392 gcc_assert (min
&& max
);
394 gcc_assert ((!TREE_OVERFLOW_P (min
) || is_overflow_infinity (min
))
395 && (!TREE_OVERFLOW_P (max
) || is_overflow_infinity (max
)));
397 if (INTEGRAL_TYPE_P (TREE_TYPE (min
)) && t
== VR_ANTI_RANGE
)
398 gcc_assert (!vrp_val_is_min (min
) || !vrp_val_is_max (max
));
400 cmp
= compare_values (min
, max
);
401 gcc_assert (cmp
== 0 || cmp
== -1 || cmp
== -2);
403 if (needs_overflow_infinity (TREE_TYPE (min
)))
404 gcc_assert (!is_overflow_infinity (min
)
405 || !is_overflow_infinity (max
));
408 if (t
== VR_UNDEFINED
|| t
== VR_VARYING
)
409 gcc_assert (min
== NULL_TREE
&& max
== NULL_TREE
);
411 if (t
== VR_UNDEFINED
|| t
== VR_VARYING
)
412 gcc_assert (equiv
== NULL
|| bitmap_empty_p (equiv
));
419 /* Since updating the equivalence set involves deep copying the
420 bitmaps, only do it if absolutely necessary. */
421 if (vr
->equiv
== NULL
423 vr
->equiv
= BITMAP_ALLOC (NULL
);
425 if (equiv
!= vr
->equiv
)
427 if (equiv
&& !bitmap_empty_p (equiv
))
428 bitmap_copy (vr
->equiv
, equiv
);
430 bitmap_clear (vr
->equiv
);
435 /* Set value range VR to the canonical form of {T, MIN, MAX, EQUIV}.
436 This means adjusting T, MIN and MAX representing the case of a
437 wrapping range with MAX < MIN covering [MIN, type_max] U [type_min, MAX]
438 as anti-rage ~[MAX+1, MIN-1]. Likewise for wrapping anti-ranges.
439 In corner cases where MAX+1 or MIN-1 wraps this will fall back
441 This routine exists to ease canonicalization in the case where we
442 extract ranges from var + CST op limit. */
445 set_and_canonicalize_value_range (value_range_t
*vr
, enum value_range_type t
,
446 tree min
, tree max
, bitmap equiv
)
448 /* Use the canonical setters for VR_UNDEFINED and VR_VARYING. */
449 if (t
== VR_UNDEFINED
)
451 set_value_range_to_undefined (vr
);
454 else if (t
== VR_VARYING
)
456 set_value_range_to_varying (vr
);
460 /* Nothing to canonicalize for symbolic ranges. */
461 if (TREE_CODE (min
) != INTEGER_CST
462 || TREE_CODE (max
) != INTEGER_CST
)
464 set_value_range (vr
, t
, min
, max
, equiv
);
468 /* Wrong order for min and max, to swap them and the VR type we need
470 if (tree_int_cst_lt (max
, min
))
474 /* For one bit precision if max < min, then the swapped
475 range covers all values, so for VR_RANGE it is varying and
476 for VR_ANTI_RANGE empty range, so drop to varying as well. */
477 if (TYPE_PRECISION (TREE_TYPE (min
)) == 1)
479 set_value_range_to_varying (vr
);
483 one
= build_int_cst (TREE_TYPE (min
), 1);
484 tmp
= int_const_binop (PLUS_EXPR
, max
, one
);
485 max
= int_const_binop (MINUS_EXPR
, min
, one
);
488 /* There's one corner case, if we had [C+1, C] before we now have
489 that again. But this represents an empty value range, so drop
490 to varying in this case. */
491 if (tree_int_cst_lt (max
, min
))
493 set_value_range_to_varying (vr
);
497 t
= t
== VR_RANGE
? VR_ANTI_RANGE
: VR_RANGE
;
500 /* Anti-ranges that can be represented as ranges should be so. */
501 if (t
== VR_ANTI_RANGE
)
503 bool is_min
= vrp_val_is_min (min
);
504 bool is_max
= vrp_val_is_max (max
);
506 if (is_min
&& is_max
)
508 /* We cannot deal with empty ranges, drop to varying.
509 ??? This could be VR_UNDEFINED instead. */
510 set_value_range_to_varying (vr
);
513 else if (TYPE_PRECISION (TREE_TYPE (min
)) == 1
514 && (is_min
|| is_max
))
516 /* Non-empty boolean ranges can always be represented
517 as a singleton range. */
519 min
= max
= vrp_val_max (TREE_TYPE (min
));
521 min
= max
= vrp_val_min (TREE_TYPE (min
));
525 /* As a special exception preserve non-null ranges. */
526 && !(TYPE_UNSIGNED (TREE_TYPE (min
))
527 && integer_zerop (max
)))
529 tree one
= build_int_cst (TREE_TYPE (max
), 1);
530 min
= int_const_binop (PLUS_EXPR
, max
, one
);
531 max
= vrp_val_max (TREE_TYPE (max
));
536 tree one
= build_int_cst (TREE_TYPE (min
), 1);
537 max
= int_const_binop (MINUS_EXPR
, min
, one
);
538 min
= vrp_val_min (TREE_TYPE (min
));
543 /* Drop [-INF(OVF), +INF(OVF)] to varying. */
544 if (needs_overflow_infinity (TREE_TYPE (min
))
545 && is_overflow_infinity (min
)
546 && is_overflow_infinity (max
))
548 set_value_range_to_varying (vr
);
552 set_value_range (vr
, t
, min
, max
, equiv
);
555 /* Copy value range FROM into value range TO. */
558 copy_value_range (value_range_t
*to
, value_range_t
*from
)
560 set_value_range (to
, from
->type
, from
->min
, from
->max
, from
->equiv
);
563 /* Set value range VR to a single value. This function is only called
564 with values we get from statements, and exists to clear the
565 TREE_OVERFLOW flag so that we don't think we have an overflow
566 infinity when we shouldn't. */
569 set_value_range_to_value (value_range_t
*vr
, tree val
, bitmap equiv
)
571 gcc_assert (is_gimple_min_invariant (val
));
572 if (TREE_OVERFLOW_P (val
))
573 val
= drop_tree_overflow (val
);
574 set_value_range (vr
, VR_RANGE
, val
, val
, equiv
);
577 /* Set value range VR to a non-negative range of type TYPE.
578 OVERFLOW_INFINITY indicates whether to use an overflow infinity
579 rather than TYPE_MAX_VALUE; this should be true if we determine
580 that the range is nonnegative based on the assumption that signed
581 overflow does not occur. */
584 set_value_range_to_nonnegative (value_range_t
*vr
, tree type
,
585 bool overflow_infinity
)
589 if (overflow_infinity
&& !supports_overflow_infinity (type
))
591 set_value_range_to_varying (vr
);
595 zero
= build_int_cst (type
, 0);
596 set_value_range (vr
, VR_RANGE
, zero
,
598 ? positive_overflow_infinity (type
)
599 : TYPE_MAX_VALUE (type
)),
603 /* Set value range VR to a non-NULL range of type TYPE. */
606 set_value_range_to_nonnull (value_range_t
*vr
, tree type
)
608 tree zero
= build_int_cst (type
, 0);
609 set_value_range (vr
, VR_ANTI_RANGE
, zero
, zero
, vr
->equiv
);
613 /* Set value range VR to a NULL range of type TYPE. */
616 set_value_range_to_null (value_range_t
*vr
, tree type
)
618 set_value_range_to_value (vr
, build_int_cst (type
, 0), vr
->equiv
);
622 /* Set value range VR to a range of a truthvalue of type TYPE. */
625 set_value_range_to_truthvalue (value_range_t
*vr
, tree type
)
627 if (TYPE_PRECISION (type
) == 1)
628 set_value_range_to_varying (vr
);
630 set_value_range (vr
, VR_RANGE
,
631 build_int_cst (type
, 0), build_int_cst (type
, 1),
636 /* If abs (min) < abs (max), set VR to [-max, max], if
637 abs (min) >= abs (max), set VR to [-min, min]. */
640 abs_extent_range (value_range_t
*vr
, tree min
, tree max
)
644 gcc_assert (TREE_CODE (min
) == INTEGER_CST
);
645 gcc_assert (TREE_CODE (max
) == INTEGER_CST
);
646 gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (min
)));
647 gcc_assert (!TYPE_UNSIGNED (TREE_TYPE (min
)));
648 min
= fold_unary (ABS_EXPR
, TREE_TYPE (min
), min
);
649 max
= fold_unary (ABS_EXPR
, TREE_TYPE (max
), max
);
650 if (TREE_OVERFLOW (min
) || TREE_OVERFLOW (max
))
652 set_value_range_to_varying (vr
);
655 cmp
= compare_values (min
, max
);
657 min
= fold_unary (NEGATE_EXPR
, TREE_TYPE (min
), max
);
658 else if (cmp
== 0 || cmp
== 1)
661 min
= fold_unary (NEGATE_EXPR
, TREE_TYPE (min
), min
);
665 set_value_range_to_varying (vr
);
668 set_and_canonicalize_value_range (vr
, VR_RANGE
, min
, max
, NULL
);
672 /* Return value range information for VAR.
674 If we have no values ranges recorded (ie, VRP is not running), then
675 return NULL. Otherwise create an empty range if none existed for VAR. */
677 static value_range_t
*
678 get_value_range (const_tree var
)
680 static const struct value_range_d vr_const_varying
681 = { VR_VARYING
, NULL_TREE
, NULL_TREE
, NULL
};
684 unsigned ver
= SSA_NAME_VERSION (var
);
686 /* If we have no recorded ranges, then return NULL. */
690 /* If we query the range for a new SSA name return an unmodifiable VARYING.
691 We should get here at most from the substitute-and-fold stage which
692 will never try to change values. */
693 if (ver
>= num_vr_values
)
694 return CONST_CAST (value_range_t
*, &vr_const_varying
);
700 /* After propagation finished do not allocate new value-ranges. */
701 if (values_propagated
)
702 return CONST_CAST (value_range_t
*, &vr_const_varying
);
704 /* Create a default value range. */
705 vr_value
[ver
] = vr
= XCNEW (value_range_t
);
707 /* Defer allocating the equivalence set. */
710 /* If VAR is a default definition of a parameter, the variable can
711 take any value in VAR's type. */
712 if (SSA_NAME_IS_DEFAULT_DEF (var
))
714 sym
= SSA_NAME_VAR (var
);
715 if (TREE_CODE (sym
) == PARM_DECL
)
717 /* Try to use the "nonnull" attribute to create ~[0, 0]
718 anti-ranges for pointers. Note that this is only valid with
719 default definitions of PARM_DECLs. */
720 if (POINTER_TYPE_P (TREE_TYPE (sym
))
721 && nonnull_arg_p (sym
))
722 set_value_range_to_nonnull (vr
, TREE_TYPE (sym
));
724 set_value_range_to_varying (vr
);
726 else if (TREE_CODE (sym
) == RESULT_DECL
727 && DECL_BY_REFERENCE (sym
))
728 set_value_range_to_nonnull (vr
, TREE_TYPE (sym
));
734 /* Return true, if VAL1 and VAL2 are equal values for VRP purposes. */
737 vrp_operand_equal_p (const_tree val1
, const_tree val2
)
741 if (!val1
|| !val2
|| !operand_equal_p (val1
, val2
, 0))
743 return is_overflow_infinity (val1
) == is_overflow_infinity (val2
);
746 /* Return true, if the bitmaps B1 and B2 are equal. */
749 vrp_bitmap_equal_p (const_bitmap b1
, const_bitmap b2
)
752 || ((!b1
|| bitmap_empty_p (b1
))
753 && (!b2
|| bitmap_empty_p (b2
)))
755 && bitmap_equal_p (b1
, b2
)));
758 /* Update the value range and equivalence set for variable VAR to
759 NEW_VR. Return true if NEW_VR is different from VAR's previous
762 NOTE: This function assumes that NEW_VR is a temporary value range
763 object created for the sole purpose of updating VAR's range. The
764 storage used by the equivalence set from NEW_VR will be freed by
765 this function. Do not call update_value_range when NEW_VR
766 is the range object associated with another SSA name. */
769 update_value_range (const_tree var
, value_range_t
*new_vr
)
771 value_range_t
*old_vr
;
774 /* If there is a value-range on the SSA name from earlier analysis
776 if (INTEGRAL_TYPE_P (TREE_TYPE (var
)))
779 value_range_type rtype
= get_range_info (var
, &min
, &max
);
780 if (rtype
== VR_RANGE
|| rtype
== VR_ANTI_RANGE
)
784 nr
.min
= wide_int_to_tree (TREE_TYPE (var
), min
);
785 nr
.max
= wide_int_to_tree (TREE_TYPE (var
), max
);
787 vrp_intersect_ranges (new_vr
, &nr
);
791 /* Update the value range, if necessary. */
792 old_vr
= get_value_range (var
);
793 is_new
= old_vr
->type
!= new_vr
->type
794 || !vrp_operand_equal_p (old_vr
->min
, new_vr
->min
)
795 || !vrp_operand_equal_p (old_vr
->max
, new_vr
->max
)
796 || !vrp_bitmap_equal_p (old_vr
->equiv
, new_vr
->equiv
);
800 /* Do not allow transitions up the lattice. The following
801 is slightly more awkward than just new_vr->type < old_vr->type
802 because VR_RANGE and VR_ANTI_RANGE need to be considered
803 the same. We may not have is_new when transitioning to
804 UNDEFINED. If old_vr->type is VARYING, we shouldn't be
806 if (new_vr
->type
== VR_UNDEFINED
)
808 BITMAP_FREE (new_vr
->equiv
);
809 set_value_range_to_varying (old_vr
);
810 set_value_range_to_varying (new_vr
);
814 set_value_range (old_vr
, new_vr
->type
, new_vr
->min
, new_vr
->max
,
818 BITMAP_FREE (new_vr
->equiv
);
824 /* Add VAR and VAR's equivalence set to EQUIV. This is the central
825 point where equivalence processing can be turned on/off. */
828 add_equivalence (bitmap
*equiv
, const_tree var
)
830 unsigned ver
= SSA_NAME_VERSION (var
);
831 value_range_t
*vr
= vr_value
[ver
];
834 *equiv
= BITMAP_ALLOC (NULL
);
835 bitmap_set_bit (*equiv
, ver
);
837 bitmap_ior_into (*equiv
, vr
->equiv
);
841 /* Return true if VR is ~[0, 0]. */
844 range_is_nonnull (value_range_t
*vr
)
846 return vr
->type
== VR_ANTI_RANGE
847 && integer_zerop (vr
->min
)
848 && integer_zerop (vr
->max
);
852 /* Return true if VR is [0, 0]. */
855 range_is_null (value_range_t
*vr
)
857 return vr
->type
== VR_RANGE
858 && integer_zerop (vr
->min
)
859 && integer_zerop (vr
->max
);
862 /* Return true if max and min of VR are INTEGER_CST. It's not necessary
866 range_int_cst_p (value_range_t
*vr
)
868 return (vr
->type
== VR_RANGE
869 && TREE_CODE (vr
->max
) == INTEGER_CST
870 && TREE_CODE (vr
->min
) == INTEGER_CST
);
873 /* Return true if VR is a INTEGER_CST singleton. */
876 range_int_cst_singleton_p (value_range_t
*vr
)
878 return (range_int_cst_p (vr
)
879 && !is_overflow_infinity (vr
->min
)
880 && !is_overflow_infinity (vr
->max
)
881 && tree_int_cst_equal (vr
->min
, vr
->max
));
884 /* Return true if value range VR involves at least one symbol. */
887 symbolic_range_p (value_range_t
*vr
)
889 return (!is_gimple_min_invariant (vr
->min
)
890 || !is_gimple_min_invariant (vr
->max
));
893 /* Return the single symbol (an SSA_NAME) contained in T if any, or NULL_TREE
894 otherwise. We only handle additive operations and set NEG to true if the
895 symbol is negated and INV to the invariant part, if any. */
898 get_single_symbol (tree t
, bool *neg
, tree
*inv
)
903 if (TREE_CODE (t
) == PLUS_EXPR
904 || TREE_CODE (t
) == POINTER_PLUS_EXPR
905 || TREE_CODE (t
) == MINUS_EXPR
)
907 if (is_gimple_min_invariant (TREE_OPERAND (t
, 0)))
909 neg_
= (TREE_CODE (t
) == MINUS_EXPR
);
910 inv_
= TREE_OPERAND (t
, 0);
911 t
= TREE_OPERAND (t
, 1);
913 else if (is_gimple_min_invariant (TREE_OPERAND (t
, 1)))
916 inv_
= TREE_OPERAND (t
, 1);
917 t
= TREE_OPERAND (t
, 0);
928 if (TREE_CODE (t
) == NEGATE_EXPR
)
930 t
= TREE_OPERAND (t
, 0);
934 if (TREE_CODE (t
) != SSA_NAME
)
942 /* The reverse operation: build a symbolic expression with TYPE
943 from symbol SYM, negated according to NEG, and invariant INV. */
946 build_symbolic_expr (tree type
, tree sym
, bool neg
, tree inv
)
948 const bool pointer_p
= POINTER_TYPE_P (type
);
952 t
= build1 (NEGATE_EXPR
, type
, t
);
954 if (integer_zerop (inv
))
957 return build2 (pointer_p
? POINTER_PLUS_EXPR
: PLUS_EXPR
, type
, t
, inv
);
960 /* Return true if value range VR involves exactly one symbol SYM. */
963 symbolic_range_based_on_p (value_range_t
*vr
, const_tree sym
)
965 bool neg
, min_has_symbol
, max_has_symbol
;
968 if (is_gimple_min_invariant (vr
->min
))
969 min_has_symbol
= false;
970 else if (get_single_symbol (vr
->min
, &neg
, &inv
) == sym
)
971 min_has_symbol
= true;
975 if (is_gimple_min_invariant (vr
->max
))
976 max_has_symbol
= false;
977 else if (get_single_symbol (vr
->max
, &neg
, &inv
) == sym
)
978 max_has_symbol
= true;
982 return (min_has_symbol
|| max_has_symbol
);
985 /* Return true if value range VR uses an overflow infinity. */
988 overflow_infinity_range_p (value_range_t
*vr
)
990 return (vr
->type
== VR_RANGE
991 && (is_overflow_infinity (vr
->min
)
992 || is_overflow_infinity (vr
->max
)));
995 /* Return false if we can not make a valid comparison based on VR;
996 this will be the case if it uses an overflow infinity and overflow
997 is not undefined (i.e., -fno-strict-overflow is in effect).
998 Otherwise return true, and set *STRICT_OVERFLOW_P to true if VR
999 uses an overflow infinity. */
1002 usable_range_p (value_range_t
*vr
, bool *strict_overflow_p
)
1004 gcc_assert (vr
->type
== VR_RANGE
);
1005 if (is_overflow_infinity (vr
->min
))
1007 *strict_overflow_p
= true;
1008 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (vr
->min
)))
1011 if (is_overflow_infinity (vr
->max
))
1013 *strict_overflow_p
= true;
1014 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (vr
->max
)))
1021 /* Return true if the result of assignment STMT is know to be non-negative.
1022 If the return value is based on the assumption that signed overflow is
1023 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1024 *STRICT_OVERFLOW_P.*/
1027 gimple_assign_nonnegative_warnv_p (gimple stmt
, bool *strict_overflow_p
)
1029 enum tree_code code
= gimple_assign_rhs_code (stmt
);
1030 switch (get_gimple_rhs_class (code
))
1032 case GIMPLE_UNARY_RHS
:
1033 return tree_unary_nonnegative_warnv_p (gimple_assign_rhs_code (stmt
),
1034 gimple_expr_type (stmt
),
1035 gimple_assign_rhs1 (stmt
),
1037 case GIMPLE_BINARY_RHS
:
1038 return tree_binary_nonnegative_warnv_p (gimple_assign_rhs_code (stmt
),
1039 gimple_expr_type (stmt
),
1040 gimple_assign_rhs1 (stmt
),
1041 gimple_assign_rhs2 (stmt
),
1043 case GIMPLE_TERNARY_RHS
:
1045 case GIMPLE_SINGLE_RHS
:
1046 return tree_single_nonnegative_warnv_p (gimple_assign_rhs1 (stmt
),
1048 case GIMPLE_INVALID_RHS
:
1055 /* Return true if return value of call STMT is know to be non-negative.
1056 If the return value is based on the assumption that signed overflow is
1057 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1058 *STRICT_OVERFLOW_P.*/
1061 gimple_call_nonnegative_warnv_p (gimple stmt
, bool *strict_overflow_p
)
1063 tree arg0
= gimple_call_num_args (stmt
) > 0 ?
1064 gimple_call_arg (stmt
, 0) : NULL_TREE
;
1065 tree arg1
= gimple_call_num_args (stmt
) > 1 ?
1066 gimple_call_arg (stmt
, 1) : NULL_TREE
;
1068 return tree_call_nonnegative_warnv_p (gimple_expr_type (stmt
),
1069 gimple_call_fndecl (stmt
),
1075 /* Return true if STMT is know to compute a non-negative value.
1076 If the return value is based on the assumption that signed overflow is
1077 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1078 *STRICT_OVERFLOW_P.*/
1081 gimple_stmt_nonnegative_warnv_p (gimple stmt
, bool *strict_overflow_p
)
1083 switch (gimple_code (stmt
))
1086 return gimple_assign_nonnegative_warnv_p (stmt
, strict_overflow_p
);
1088 return gimple_call_nonnegative_warnv_p (stmt
, strict_overflow_p
);
1094 /* Return true if the result of assignment STMT is know to be non-zero.
1095 If the return value is based on the assumption that signed overflow is
1096 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1097 *STRICT_OVERFLOW_P.*/
1100 gimple_assign_nonzero_warnv_p (gimple stmt
, bool *strict_overflow_p
)
1102 enum tree_code code
= gimple_assign_rhs_code (stmt
);
1103 switch (get_gimple_rhs_class (code
))
1105 case GIMPLE_UNARY_RHS
:
1106 return tree_unary_nonzero_warnv_p (gimple_assign_rhs_code (stmt
),
1107 gimple_expr_type (stmt
),
1108 gimple_assign_rhs1 (stmt
),
1110 case GIMPLE_BINARY_RHS
:
1111 return tree_binary_nonzero_warnv_p (gimple_assign_rhs_code (stmt
),
1112 gimple_expr_type (stmt
),
1113 gimple_assign_rhs1 (stmt
),
1114 gimple_assign_rhs2 (stmt
),
1116 case GIMPLE_TERNARY_RHS
:
1118 case GIMPLE_SINGLE_RHS
:
1119 return tree_single_nonzero_warnv_p (gimple_assign_rhs1 (stmt
),
1121 case GIMPLE_INVALID_RHS
:
1128 /* Return true if STMT is known to compute a non-zero value.
1129 If the return value is based on the assumption that signed overflow is
1130 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1131 *STRICT_OVERFLOW_P.*/
1134 gimple_stmt_nonzero_warnv_p (gimple stmt
, bool *strict_overflow_p
)
1136 switch (gimple_code (stmt
))
1139 return gimple_assign_nonzero_warnv_p (stmt
, strict_overflow_p
);
1142 tree fndecl
= gimple_call_fndecl (stmt
);
1143 if (!fndecl
) return false;
1144 if (flag_delete_null_pointer_checks
&& !flag_check_new
1145 && DECL_IS_OPERATOR_NEW (fndecl
)
1146 && !TREE_NOTHROW (fndecl
))
1148 /* References are always non-NULL. */
1149 if (flag_delete_null_pointer_checks
1150 && TREE_CODE (TREE_TYPE (fndecl
)) == REFERENCE_TYPE
)
1152 if (flag_delete_null_pointer_checks
&&
1153 lookup_attribute ("returns_nonnull",
1154 TYPE_ATTRIBUTES (gimple_call_fntype (stmt
))))
1156 return gimple_alloca_call_p (stmt
);
1163 /* Like tree_expr_nonzero_warnv_p, but this function uses value ranges
1167 vrp_stmt_computes_nonzero (gimple stmt
, bool *strict_overflow_p
)
1169 if (gimple_stmt_nonzero_warnv_p (stmt
, strict_overflow_p
))
1172 /* If we have an expression of the form &X->a, then the expression
1173 is nonnull if X is nonnull. */
1174 if (is_gimple_assign (stmt
)
1175 && gimple_assign_rhs_code (stmt
) == ADDR_EXPR
)
1177 tree expr
= gimple_assign_rhs1 (stmt
);
1178 tree base
= get_base_address (TREE_OPERAND (expr
, 0));
1180 if (base
!= NULL_TREE
1181 && TREE_CODE (base
) == MEM_REF
1182 && TREE_CODE (TREE_OPERAND (base
, 0)) == SSA_NAME
)
1184 value_range_t
*vr
= get_value_range (TREE_OPERAND (base
, 0));
1185 if (range_is_nonnull (vr
))
1193 /* Returns true if EXPR is a valid value (as expected by compare_values) --
1194 a gimple invariant, or SSA_NAME +- CST. */
1197 valid_value_p (tree expr
)
1199 if (TREE_CODE (expr
) == SSA_NAME
)
1202 if (TREE_CODE (expr
) == PLUS_EXPR
1203 || TREE_CODE (expr
) == MINUS_EXPR
)
1204 return (TREE_CODE (TREE_OPERAND (expr
, 0)) == SSA_NAME
1205 && TREE_CODE (TREE_OPERAND (expr
, 1)) == INTEGER_CST
);
1207 return is_gimple_min_invariant (expr
);
1213 -2 if those are incomparable. */
1215 operand_less_p (tree val
, tree val2
)
1217 /* LT is folded faster than GE and others. Inline the common case. */
1218 if (TREE_CODE (val
) == INTEGER_CST
&& TREE_CODE (val2
) == INTEGER_CST
)
1219 return tree_int_cst_lt (val
, val2
);
1224 fold_defer_overflow_warnings ();
1226 tcmp
= fold_binary_to_constant (LT_EXPR
, boolean_type_node
, val
, val2
);
1228 fold_undefer_and_ignore_overflow_warnings ();
1231 || TREE_CODE (tcmp
) != INTEGER_CST
)
1234 if (!integer_zerop (tcmp
))
1238 /* val >= val2, not considering overflow infinity. */
1239 if (is_negative_overflow_infinity (val
))
1240 return is_negative_overflow_infinity (val2
) ? 0 : 1;
1241 else if (is_positive_overflow_infinity (val2
))
1242 return is_positive_overflow_infinity (val
) ? 0 : 1;
1247 /* Compare two values VAL1 and VAL2. Return
1249 -2 if VAL1 and VAL2 cannot be compared at compile-time,
1252 +1 if VAL1 > VAL2, and
1255 This is similar to tree_int_cst_compare but supports pointer values
1256 and values that cannot be compared at compile time.
1258 If STRICT_OVERFLOW_P is not NULL, then set *STRICT_OVERFLOW_P to
1259 true if the return value is only valid if we assume that signed
1260 overflow is undefined. */
1263 compare_values_warnv (tree val1
, tree val2
, bool *strict_overflow_p
)
1268 /* Below we rely on the fact that VAL1 and VAL2 are both pointers or
1270 gcc_assert (POINTER_TYPE_P (TREE_TYPE (val1
))
1271 == POINTER_TYPE_P (TREE_TYPE (val2
)));
1273 /* Convert the two values into the same type. This is needed because
1274 sizetype causes sign extension even for unsigned types. */
1275 val2
= fold_convert (TREE_TYPE (val1
), val2
);
1276 STRIP_USELESS_TYPE_CONVERSION (val2
);
1278 if ((TREE_CODE (val1
) == SSA_NAME
1279 || (TREE_CODE (val1
) == NEGATE_EXPR
1280 && TREE_CODE (TREE_OPERAND (val1
, 0)) == SSA_NAME
)
1281 || TREE_CODE (val1
) == PLUS_EXPR
1282 || TREE_CODE (val1
) == MINUS_EXPR
)
1283 && (TREE_CODE (val2
) == SSA_NAME
1284 || (TREE_CODE (val2
) == NEGATE_EXPR
1285 && TREE_CODE (TREE_OPERAND (val2
, 0)) == SSA_NAME
)
1286 || TREE_CODE (val2
) == PLUS_EXPR
1287 || TREE_CODE (val2
) == MINUS_EXPR
))
1289 tree n1
, c1
, n2
, c2
;
1290 enum tree_code code1
, code2
;
1292 /* If VAL1 and VAL2 are of the form '[-]NAME [+-] CST' or 'NAME',
1293 return -1 or +1 accordingly. If VAL1 and VAL2 don't use the
1294 same name, return -2. */
1295 if (TREE_CODE (val1
) == SSA_NAME
|| TREE_CODE (val1
) == NEGATE_EXPR
)
1303 code1
= TREE_CODE (val1
);
1304 n1
= TREE_OPERAND (val1
, 0);
1305 c1
= TREE_OPERAND (val1
, 1);
1306 if (tree_int_cst_sgn (c1
) == -1)
1308 if (is_negative_overflow_infinity (c1
))
1310 c1
= fold_unary_to_constant (NEGATE_EXPR
, TREE_TYPE (c1
), c1
);
1313 code1
= code1
== MINUS_EXPR
? PLUS_EXPR
: MINUS_EXPR
;
1317 if (TREE_CODE (val2
) == SSA_NAME
|| TREE_CODE (val2
) == NEGATE_EXPR
)
1325 code2
= TREE_CODE (val2
);
1326 n2
= TREE_OPERAND (val2
, 0);
1327 c2
= TREE_OPERAND (val2
, 1);
1328 if (tree_int_cst_sgn (c2
) == -1)
1330 if (is_negative_overflow_infinity (c2
))
1332 c2
= fold_unary_to_constant (NEGATE_EXPR
, TREE_TYPE (c2
), c2
);
1335 code2
= code2
== MINUS_EXPR
? PLUS_EXPR
: MINUS_EXPR
;
1339 /* Both values must use the same name. */
1340 if (TREE_CODE (n1
) == NEGATE_EXPR
&& TREE_CODE (n2
) == NEGATE_EXPR
)
1342 n1
= TREE_OPERAND (n1
, 0);
1343 n2
= TREE_OPERAND (n2
, 0);
1348 if (code1
== SSA_NAME
&& code2
== SSA_NAME
)
1352 /* If overflow is defined we cannot simplify more. */
1353 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (val1
)))
1356 if (strict_overflow_p
!= NULL
1357 && (code1
== SSA_NAME
|| !TREE_NO_WARNING (val1
))
1358 && (code2
== SSA_NAME
|| !TREE_NO_WARNING (val2
)))
1359 *strict_overflow_p
= true;
1361 if (code1
== SSA_NAME
)
1363 if (code2
== PLUS_EXPR
)
1364 /* NAME < NAME + CST */
1366 else if (code2
== MINUS_EXPR
)
1367 /* NAME > NAME - CST */
1370 else if (code1
== PLUS_EXPR
)
1372 if (code2
== SSA_NAME
)
1373 /* NAME + CST > NAME */
1375 else if (code2
== PLUS_EXPR
)
1376 /* NAME + CST1 > NAME + CST2, if CST1 > CST2 */
1377 return compare_values_warnv (c1
, c2
, strict_overflow_p
);
1378 else if (code2
== MINUS_EXPR
)
1379 /* NAME + CST1 > NAME - CST2 */
1382 else if (code1
== MINUS_EXPR
)
1384 if (code2
== SSA_NAME
)
1385 /* NAME - CST < NAME */
1387 else if (code2
== PLUS_EXPR
)
1388 /* NAME - CST1 < NAME + CST2 */
1390 else if (code2
== MINUS_EXPR
)
1391 /* NAME - CST1 > NAME - CST2, if CST1 < CST2. Notice that
1392 C1 and C2 are swapped in the call to compare_values. */
1393 return compare_values_warnv (c2
, c1
, strict_overflow_p
);
1399 /* We cannot compare non-constants. */
1400 if (!is_gimple_min_invariant (val1
) || !is_gimple_min_invariant (val2
))
1403 if (!POINTER_TYPE_P (TREE_TYPE (val1
)))
1405 /* We cannot compare overflowed values, except for overflow
1407 if (TREE_OVERFLOW (val1
) || TREE_OVERFLOW (val2
))
1409 if (strict_overflow_p
!= NULL
)
1410 *strict_overflow_p
= true;
1411 if (is_negative_overflow_infinity (val1
))
1412 return is_negative_overflow_infinity (val2
) ? 0 : -1;
1413 else if (is_negative_overflow_infinity (val2
))
1415 else if (is_positive_overflow_infinity (val1
))
1416 return is_positive_overflow_infinity (val2
) ? 0 : 1;
1417 else if (is_positive_overflow_infinity (val2
))
1422 return tree_int_cst_compare (val1
, val2
);
1428 /* First see if VAL1 and VAL2 are not the same. */
1429 if (val1
== val2
|| operand_equal_p (val1
, val2
, 0))
1432 /* If VAL1 is a lower address than VAL2, return -1. */
1433 if (operand_less_p (val1
, val2
) == 1)
1436 /* If VAL1 is a higher address than VAL2, return +1. */
1437 if (operand_less_p (val2
, val1
) == 1)
1440 /* If VAL1 is different than VAL2, return +2.
1441 For integer constants we either have already returned -1 or 1
1442 or they are equivalent. We still might succeed in proving
1443 something about non-trivial operands. */
1444 if (TREE_CODE (val1
) != INTEGER_CST
1445 || TREE_CODE (val2
) != INTEGER_CST
)
1447 t
= fold_binary_to_constant (NE_EXPR
, boolean_type_node
, val1
, val2
);
1448 if (t
&& integer_onep (t
))
1456 /* Compare values like compare_values_warnv, but treat comparisons of
1457 nonconstants which rely on undefined overflow as incomparable. */
1460 compare_values (tree val1
, tree val2
)
1466 ret
= compare_values_warnv (val1
, val2
, &sop
);
1468 && (!is_gimple_min_invariant (val1
) || !is_gimple_min_invariant (val2
)))
1474 /* Return 1 if VAL is inside value range MIN <= VAL <= MAX,
1475 0 if VAL is not inside [MIN, MAX],
1476 -2 if we cannot tell either way.
1478 Benchmark compile/20001226-1.c compilation time after changing this
1482 value_inside_range (tree val
, tree min
, tree max
)
1486 cmp1
= operand_less_p (val
, min
);
1492 cmp2
= operand_less_p (max
, val
);
1500 /* Return true if value ranges VR0 and VR1 have a non-empty
1503 Benchmark compile/20001226-1.c compilation time after changing this
1508 value_ranges_intersect_p (value_range_t
*vr0
, value_range_t
*vr1
)
1510 /* The value ranges do not intersect if the maximum of the first range is
1511 less than the minimum of the second range or vice versa.
1512 When those relations are unknown, we can't do any better. */
1513 if (operand_less_p (vr0
->max
, vr1
->min
) != 0)
1515 if (operand_less_p (vr1
->max
, vr0
->min
) != 0)
1521 /* Return 1 if [MIN, MAX] includes the value zero, 0 if it does not
1522 include the value zero, -2 if we cannot tell. */
1525 range_includes_zero_p (tree min
, tree max
)
1527 tree zero
= build_int_cst (TREE_TYPE (min
), 0);
1528 return value_inside_range (zero
, min
, max
);
1531 /* Return true if *VR is know to only contain nonnegative values. */
1534 value_range_nonnegative_p (value_range_t
*vr
)
1536 /* Testing for VR_ANTI_RANGE is not useful here as any anti-range
1537 which would return a useful value should be encoded as a
1539 if (vr
->type
== VR_RANGE
)
1541 int result
= compare_values (vr
->min
, integer_zero_node
);
1542 return (result
== 0 || result
== 1);
1548 /* If *VR has a value rante that is a single constant value return that,
1549 otherwise return NULL_TREE. */
1552 value_range_constant_singleton (value_range_t
*vr
)
1554 if (vr
->type
== VR_RANGE
1555 && operand_equal_p (vr
->min
, vr
->max
, 0)
1556 && is_gimple_min_invariant (vr
->min
))
1562 /* If OP has a value range with a single constant value return that,
1563 otherwise return NULL_TREE. This returns OP itself if OP is a
1567 op_with_constant_singleton_value_range (tree op
)
1569 if (is_gimple_min_invariant (op
))
1572 if (TREE_CODE (op
) != SSA_NAME
)
1575 return value_range_constant_singleton (get_value_range (op
));
1578 /* Return true if op is in a boolean [0, 1] value-range. */
1581 op_with_boolean_value_range_p (tree op
)
1585 if (TYPE_PRECISION (TREE_TYPE (op
)) == 1)
1588 if (integer_zerop (op
)
1589 || integer_onep (op
))
1592 if (TREE_CODE (op
) != SSA_NAME
)
1595 vr
= get_value_range (op
);
1596 return (vr
->type
== VR_RANGE
1597 && integer_zerop (vr
->min
)
1598 && integer_onep (vr
->max
));
1601 /* Extract value range information from an ASSERT_EXPR EXPR and store
1605 extract_range_from_assert (value_range_t
*vr_p
, tree expr
)
1607 tree var
, cond
, limit
, min
, max
, type
;
1608 value_range_t
*limit_vr
;
1609 enum tree_code cond_code
;
1611 var
= ASSERT_EXPR_VAR (expr
);
1612 cond
= ASSERT_EXPR_COND (expr
);
1614 gcc_assert (COMPARISON_CLASS_P (cond
));
1616 /* Find VAR in the ASSERT_EXPR conditional. */
1617 if (var
== TREE_OPERAND (cond
, 0)
1618 || TREE_CODE (TREE_OPERAND (cond
, 0)) == PLUS_EXPR
1619 || TREE_CODE (TREE_OPERAND (cond
, 0)) == NOP_EXPR
)
1621 /* If the predicate is of the form VAR COMP LIMIT, then we just
1622 take LIMIT from the RHS and use the same comparison code. */
1623 cond_code
= TREE_CODE (cond
);
1624 limit
= TREE_OPERAND (cond
, 1);
1625 cond
= TREE_OPERAND (cond
, 0);
1629 /* If the predicate is of the form LIMIT COMP VAR, then we need
1630 to flip around the comparison code to create the proper range
1632 cond_code
= swap_tree_comparison (TREE_CODE (cond
));
1633 limit
= TREE_OPERAND (cond
, 0);
1634 cond
= TREE_OPERAND (cond
, 1);
1637 limit
= avoid_overflow_infinity (limit
);
1639 type
= TREE_TYPE (var
);
1640 gcc_assert (limit
!= var
);
1642 /* For pointer arithmetic, we only keep track of pointer equality
1644 if (POINTER_TYPE_P (type
) && cond_code
!= NE_EXPR
&& cond_code
!= EQ_EXPR
)
1646 set_value_range_to_varying (vr_p
);
1650 /* If LIMIT is another SSA name and LIMIT has a range of its own,
1651 try to use LIMIT's range to avoid creating symbolic ranges
1653 limit_vr
= (TREE_CODE (limit
) == SSA_NAME
) ? get_value_range (limit
) : NULL
;
1655 /* LIMIT's range is only interesting if it has any useful information. */
1657 && (limit_vr
->type
== VR_UNDEFINED
1658 || limit_vr
->type
== VR_VARYING
1659 || symbolic_range_p (limit_vr
)))
1662 /* Initially, the new range has the same set of equivalences of
1663 VAR's range. This will be revised before returning the final
1664 value. Since assertions may be chained via mutually exclusive
1665 predicates, we will need to trim the set of equivalences before
1667 gcc_assert (vr_p
->equiv
== NULL
);
1668 add_equivalence (&vr_p
->equiv
, var
);
1670 /* Extract a new range based on the asserted comparison for VAR and
1671 LIMIT's value range. Notice that if LIMIT has an anti-range, we
1672 will only use it for equality comparisons (EQ_EXPR). For any
1673 other kind of assertion, we cannot derive a range from LIMIT's
1674 anti-range that can be used to describe the new range. For
1675 instance, ASSERT_EXPR <x_2, x_2 <= b_4>. If b_4 is ~[2, 10],
1676 then b_4 takes on the ranges [-INF, 1] and [11, +INF]. There is
1677 no single range for x_2 that could describe LE_EXPR, so we might
1678 as well build the range [b_4, +INF] for it.
1679 One special case we handle is extracting a range from a
1680 range test encoded as (unsigned)var + CST <= limit. */
1681 if (TREE_CODE (cond
) == NOP_EXPR
1682 || TREE_CODE (cond
) == PLUS_EXPR
)
1684 if (TREE_CODE (cond
) == PLUS_EXPR
)
1686 min
= fold_build1 (NEGATE_EXPR
, TREE_TYPE (TREE_OPERAND (cond
, 1)),
1687 TREE_OPERAND (cond
, 1));
1688 max
= int_const_binop (PLUS_EXPR
, limit
, min
);
1689 cond
= TREE_OPERAND (cond
, 0);
1693 min
= build_int_cst (TREE_TYPE (var
), 0);
1697 /* Make sure to not set TREE_OVERFLOW on the final type
1698 conversion. We are willingly interpreting large positive
1699 unsigned values as negative signed values here. */
1700 min
= force_fit_type (TREE_TYPE (var
), wi::to_widest (min
), 0, false);
1701 max
= force_fit_type (TREE_TYPE (var
), wi::to_widest (max
), 0, false);
1703 /* We can transform a max, min range to an anti-range or
1704 vice-versa. Use set_and_canonicalize_value_range which does
1706 if (cond_code
== LE_EXPR
)
1707 set_and_canonicalize_value_range (vr_p
, VR_RANGE
,
1708 min
, max
, vr_p
->equiv
);
1709 else if (cond_code
== GT_EXPR
)
1710 set_and_canonicalize_value_range (vr_p
, VR_ANTI_RANGE
,
1711 min
, max
, vr_p
->equiv
);
1715 else if (cond_code
== EQ_EXPR
)
1717 enum value_range_type range_type
;
1721 range_type
= limit_vr
->type
;
1722 min
= limit_vr
->min
;
1723 max
= limit_vr
->max
;
1727 range_type
= VR_RANGE
;
1732 set_value_range (vr_p
, range_type
, min
, max
, vr_p
->equiv
);
1734 /* When asserting the equality VAR == LIMIT and LIMIT is another
1735 SSA name, the new range will also inherit the equivalence set
1737 if (TREE_CODE (limit
) == SSA_NAME
)
1738 add_equivalence (&vr_p
->equiv
, limit
);
1740 else if (cond_code
== NE_EXPR
)
1742 /* As described above, when LIMIT's range is an anti-range and
1743 this assertion is an inequality (NE_EXPR), then we cannot
1744 derive anything from the anti-range. For instance, if
1745 LIMIT's range was ~[0, 0], the assertion 'VAR != LIMIT' does
1746 not imply that VAR's range is [0, 0]. So, in the case of
1747 anti-ranges, we just assert the inequality using LIMIT and
1750 If LIMIT_VR is a range, we can only use it to build a new
1751 anti-range if LIMIT_VR is a single-valued range. For
1752 instance, if LIMIT_VR is [0, 1], the predicate
1753 VAR != [0, 1] does not mean that VAR's range is ~[0, 1].
1754 Rather, it means that for value 0 VAR should be ~[0, 0]
1755 and for value 1, VAR should be ~[1, 1]. We cannot
1756 represent these ranges.
1758 The only situation in which we can build a valid
1759 anti-range is when LIMIT_VR is a single-valued range
1760 (i.e., LIMIT_VR->MIN == LIMIT_VR->MAX). In that case,
1761 build the anti-range ~[LIMIT_VR->MIN, LIMIT_VR->MAX]. */
1763 && limit_vr
->type
== VR_RANGE
1764 && compare_values (limit_vr
->min
, limit_vr
->max
) == 0)
1766 min
= limit_vr
->min
;
1767 max
= limit_vr
->max
;
1771 /* In any other case, we cannot use LIMIT's range to build a
1772 valid anti-range. */
1776 /* If MIN and MAX cover the whole range for their type, then
1777 just use the original LIMIT. */
1778 if (INTEGRAL_TYPE_P (type
)
1779 && vrp_val_is_min (min
)
1780 && vrp_val_is_max (max
))
1783 set_and_canonicalize_value_range (vr_p
, VR_ANTI_RANGE
,
1784 min
, max
, vr_p
->equiv
);
1786 else if (cond_code
== LE_EXPR
|| cond_code
== LT_EXPR
)
1788 min
= TYPE_MIN_VALUE (type
);
1790 if (limit_vr
== NULL
|| limit_vr
->type
== VR_ANTI_RANGE
)
1794 /* If LIMIT_VR is of the form [N1, N2], we need to build the
1795 range [MIN, N2] for LE_EXPR and [MIN, N2 - 1] for
1797 max
= limit_vr
->max
;
1800 /* If the maximum value forces us to be out of bounds, simply punt.
1801 It would be pointless to try and do anything more since this
1802 all should be optimized away above us. */
1803 if ((cond_code
== LT_EXPR
1804 && compare_values (max
, min
) == 0)
1805 || is_overflow_infinity (max
))
1806 set_value_range_to_varying (vr_p
);
1809 /* For LT_EXPR, we create the range [MIN, MAX - 1]. */
1810 if (cond_code
== LT_EXPR
)
1812 if (TYPE_PRECISION (TREE_TYPE (max
)) == 1
1813 && !TYPE_UNSIGNED (TREE_TYPE (max
)))
1814 max
= fold_build2 (PLUS_EXPR
, TREE_TYPE (max
), max
,
1815 build_int_cst (TREE_TYPE (max
), -1));
1817 max
= fold_build2 (MINUS_EXPR
, TREE_TYPE (max
), max
,
1818 build_int_cst (TREE_TYPE (max
), 1));
1820 TREE_NO_WARNING (max
) = 1;
1823 set_value_range (vr_p
, VR_RANGE
, min
, max
, vr_p
->equiv
);
1826 else if (cond_code
== GE_EXPR
|| cond_code
== GT_EXPR
)
1828 max
= TYPE_MAX_VALUE (type
);
1830 if (limit_vr
== NULL
|| limit_vr
->type
== VR_ANTI_RANGE
)
1834 /* If LIMIT_VR is of the form [N1, N2], we need to build the
1835 range [N1, MAX] for GE_EXPR and [N1 + 1, MAX] for
1837 min
= limit_vr
->min
;
1840 /* If the minimum value forces us to be out of bounds, simply punt.
1841 It would be pointless to try and do anything more since this
1842 all should be optimized away above us. */
1843 if ((cond_code
== GT_EXPR
1844 && compare_values (min
, max
) == 0)
1845 || is_overflow_infinity (min
))
1846 set_value_range_to_varying (vr_p
);
1849 /* For GT_EXPR, we create the range [MIN + 1, MAX]. */
1850 if (cond_code
== GT_EXPR
)
1852 if (TYPE_PRECISION (TREE_TYPE (min
)) == 1
1853 && !TYPE_UNSIGNED (TREE_TYPE (min
)))
1854 min
= fold_build2 (MINUS_EXPR
, TREE_TYPE (min
), min
,
1855 build_int_cst (TREE_TYPE (min
), -1));
1857 min
= fold_build2 (PLUS_EXPR
, TREE_TYPE (min
), min
,
1858 build_int_cst (TREE_TYPE (min
), 1));
1860 TREE_NO_WARNING (min
) = 1;
1863 set_value_range (vr_p
, VR_RANGE
, min
, max
, vr_p
->equiv
);
1869 /* Finally intersect the new range with what we already know about var. */
1870 vrp_intersect_ranges (vr_p
, get_value_range (var
));
1874 /* Extract range information from SSA name VAR and store it in VR. If
1875 VAR has an interesting range, use it. Otherwise, create the
1876 range [VAR, VAR] and return it. This is useful in situations where
1877 we may have conditionals testing values of VARYING names. For
1884 Even if y_5 is deemed VARYING, we can determine that x_3 > y_5 is
1888 extract_range_from_ssa_name (value_range_t
*vr
, tree var
)
1890 value_range_t
*var_vr
= get_value_range (var
);
1892 if (var_vr
->type
!= VR_VARYING
)
1893 copy_value_range (vr
, var_vr
);
1895 set_value_range (vr
, VR_RANGE
, var
, var
, NULL
);
1897 add_equivalence (&vr
->equiv
, var
);
1901 /* Wrapper around int_const_binop. If the operation overflows and we
1902 are not using wrapping arithmetic, then adjust the result to be
1903 -INF or +INF depending on CODE, VAL1 and VAL2. This can return
1904 NULL_TREE if we need to use an overflow infinity representation but
1905 the type does not support it. */
1908 vrp_int_const_binop (enum tree_code code
, tree val1
, tree val2
)
1912 res
= int_const_binop (code
, val1
, val2
);
1914 /* If we are using unsigned arithmetic, operate symbolically
1915 on -INF and +INF as int_const_binop only handles signed overflow. */
1916 if (TYPE_UNSIGNED (TREE_TYPE (val1
)))
1918 int checkz
= compare_values (res
, val1
);
1919 bool overflow
= false;
1921 /* Ensure that res = val1 [+*] val2 >= val1
1922 or that res = val1 - val2 <= val1. */
1923 if ((code
== PLUS_EXPR
1924 && !(checkz
== 1 || checkz
== 0))
1925 || (code
== MINUS_EXPR
1926 && !(checkz
== 0 || checkz
== -1)))
1930 /* Checking for multiplication overflow is done by dividing the
1931 output of the multiplication by the first input of the
1932 multiplication. If the result of that division operation is
1933 not equal to the second input of the multiplication, then the
1934 multiplication overflowed. */
1935 else if (code
== MULT_EXPR
&& !integer_zerop (val1
))
1937 tree tmp
= int_const_binop (TRUNC_DIV_EXPR
,
1940 int check
= compare_values (tmp
, val2
);
1948 res
= copy_node (res
);
1949 TREE_OVERFLOW (res
) = 1;
1953 else if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (val1
)))
1954 /* If the singed operation wraps then int_const_binop has done
1955 everything we want. */
1957 /* Signed division of -1/0 overflows and by the time it gets here
1958 returns NULL_TREE. */
1961 else if ((TREE_OVERFLOW (res
)
1962 && !TREE_OVERFLOW (val1
)
1963 && !TREE_OVERFLOW (val2
))
1964 || is_overflow_infinity (val1
)
1965 || is_overflow_infinity (val2
))
1967 /* If the operation overflowed but neither VAL1 nor VAL2 are
1968 overflown, return -INF or +INF depending on the operation
1969 and the combination of signs of the operands. */
1970 int sgn1
= tree_int_cst_sgn (val1
);
1971 int sgn2
= tree_int_cst_sgn (val2
);
1973 if (needs_overflow_infinity (TREE_TYPE (res
))
1974 && !supports_overflow_infinity (TREE_TYPE (res
)))
1977 /* We have to punt on adding infinities of different signs,
1978 since we can't tell what the sign of the result should be.
1979 Likewise for subtracting infinities of the same sign. */
1980 if (((code
== PLUS_EXPR
&& sgn1
!= sgn2
)
1981 || (code
== MINUS_EXPR
&& sgn1
== sgn2
))
1982 && is_overflow_infinity (val1
)
1983 && is_overflow_infinity (val2
))
1986 /* Don't try to handle division or shifting of infinities. */
1987 if ((code
== TRUNC_DIV_EXPR
1988 || code
== FLOOR_DIV_EXPR
1989 || code
== CEIL_DIV_EXPR
1990 || code
== EXACT_DIV_EXPR
1991 || code
== ROUND_DIV_EXPR
1992 || code
== RSHIFT_EXPR
)
1993 && (is_overflow_infinity (val1
)
1994 || is_overflow_infinity (val2
)))
1997 /* Notice that we only need to handle the restricted set of
1998 operations handled by extract_range_from_binary_expr.
1999 Among them, only multiplication, addition and subtraction
2000 can yield overflow without overflown operands because we
2001 are working with integral types only... except in the
2002 case VAL1 = -INF and VAL2 = -1 which overflows to +INF
2003 for division too. */
2005 /* For multiplication, the sign of the overflow is given
2006 by the comparison of the signs of the operands. */
2007 if ((code
== MULT_EXPR
&& sgn1
== sgn2
)
2008 /* For addition, the operands must be of the same sign
2009 to yield an overflow. Its sign is therefore that
2010 of one of the operands, for example the first. For
2011 infinite operands X + -INF is negative, not positive. */
2012 || (code
== PLUS_EXPR
2014 ? !is_negative_overflow_infinity (val2
)
2015 : is_positive_overflow_infinity (val2
)))
2016 /* For subtraction, non-infinite operands must be of
2017 different signs to yield an overflow. Its sign is
2018 therefore that of the first operand or the opposite of
2019 that of the second operand. A first operand of 0 counts
2020 as positive here, for the corner case 0 - (-INF), which
2021 overflows, but must yield +INF. For infinite operands 0
2022 - INF is negative, not positive. */
2023 || (code
== MINUS_EXPR
2025 ? !is_positive_overflow_infinity (val2
)
2026 : is_negative_overflow_infinity (val2
)))
2027 /* We only get in here with positive shift count, so the
2028 overflow direction is the same as the sign of val1.
2029 Actually rshift does not overflow at all, but we only
2030 handle the case of shifting overflowed -INF and +INF. */
2031 || (code
== RSHIFT_EXPR
2033 /* For division, the only case is -INF / -1 = +INF. */
2034 || code
== TRUNC_DIV_EXPR
2035 || code
== FLOOR_DIV_EXPR
2036 || code
== CEIL_DIV_EXPR
2037 || code
== EXACT_DIV_EXPR
2038 || code
== ROUND_DIV_EXPR
)
2039 return (needs_overflow_infinity (TREE_TYPE (res
))
2040 ? positive_overflow_infinity (TREE_TYPE (res
))
2041 : TYPE_MAX_VALUE (TREE_TYPE (res
)));
2043 return (needs_overflow_infinity (TREE_TYPE (res
))
2044 ? negative_overflow_infinity (TREE_TYPE (res
))
2045 : TYPE_MIN_VALUE (TREE_TYPE (res
)));
2052 /* For range VR compute two wide_int bitmasks. In *MAY_BE_NONZERO
2053 bitmask if some bit is unset, it means for all numbers in the range
2054 the bit is 0, otherwise it might be 0 or 1. In *MUST_BE_NONZERO
2055 bitmask if some bit is set, it means for all numbers in the range
2056 the bit is 1, otherwise it might be 0 or 1. */
2059 zero_nonzero_bits_from_vr (const tree expr_type
,
2061 wide_int
*may_be_nonzero
,
2062 wide_int
*must_be_nonzero
)
2064 *may_be_nonzero
= wi::minus_one (TYPE_PRECISION (expr_type
));
2065 *must_be_nonzero
= wi::zero (TYPE_PRECISION (expr_type
));
2066 if (!range_int_cst_p (vr
)
2067 || is_overflow_infinity (vr
->min
)
2068 || is_overflow_infinity (vr
->max
))
2071 if (range_int_cst_singleton_p (vr
))
2073 *may_be_nonzero
= vr
->min
;
2074 *must_be_nonzero
= *may_be_nonzero
;
2076 else if (tree_int_cst_sgn (vr
->min
) >= 0
2077 || tree_int_cst_sgn (vr
->max
) < 0)
2079 wide_int xor_mask
= wi::bit_xor (vr
->min
, vr
->max
);
2080 *may_be_nonzero
= wi::bit_or (vr
->min
, vr
->max
);
2081 *must_be_nonzero
= wi::bit_and (vr
->min
, vr
->max
);
2084 wide_int mask
= wi::mask (wi::floor_log2 (xor_mask
), false,
2085 may_be_nonzero
->get_precision ());
2086 *may_be_nonzero
= *may_be_nonzero
| mask
;
2087 *must_be_nonzero
= must_be_nonzero
->and_not (mask
);
2094 /* Create two value-ranges in *VR0 and *VR1 from the anti-range *AR
2095 so that *VR0 U *VR1 == *AR. Returns true if that is possible,
2096 false otherwise. If *AR can be represented with a single range
2097 *VR1 will be VR_UNDEFINED. */
2100 ranges_from_anti_range (value_range_t
*ar
,
2101 value_range_t
*vr0
, value_range_t
*vr1
)
2103 tree type
= TREE_TYPE (ar
->min
);
2105 vr0
->type
= VR_UNDEFINED
;
2106 vr1
->type
= VR_UNDEFINED
;
2108 if (ar
->type
!= VR_ANTI_RANGE
2109 || TREE_CODE (ar
->min
) != INTEGER_CST
2110 || TREE_CODE (ar
->max
) != INTEGER_CST
2111 || !vrp_val_min (type
)
2112 || !vrp_val_max (type
))
2115 if (!vrp_val_is_min (ar
->min
))
2117 vr0
->type
= VR_RANGE
;
2118 vr0
->min
= vrp_val_min (type
);
2119 vr0
->max
= wide_int_to_tree (type
, wi::sub (ar
->min
, 1));
2121 if (!vrp_val_is_max (ar
->max
))
2123 vr1
->type
= VR_RANGE
;
2124 vr1
->min
= wide_int_to_tree (type
, wi::add (ar
->max
, 1));
2125 vr1
->max
= vrp_val_max (type
);
2127 if (vr0
->type
== VR_UNDEFINED
)
2130 vr1
->type
= VR_UNDEFINED
;
2133 return vr0
->type
!= VR_UNDEFINED
;
2136 /* Helper to extract a value-range *VR for a multiplicative operation
2140 extract_range_from_multiplicative_op_1 (value_range_t
*vr
,
2141 enum tree_code code
,
2142 value_range_t
*vr0
, value_range_t
*vr1
)
2144 enum value_range_type type
;
2151 /* Multiplications, divisions and shifts are a bit tricky to handle,
2152 depending on the mix of signs we have in the two ranges, we
2153 need to operate on different values to get the minimum and
2154 maximum values for the new range. One approach is to figure
2155 out all the variations of range combinations and do the
2158 However, this involves several calls to compare_values and it
2159 is pretty convoluted. It's simpler to do the 4 operations
2160 (MIN0 OP MIN1, MIN0 OP MAX1, MAX0 OP MIN1 and MAX0 OP MAX0 OP
2161 MAX1) and then figure the smallest and largest values to form
2163 gcc_assert (code
== MULT_EXPR
2164 || code
== TRUNC_DIV_EXPR
2165 || code
== FLOOR_DIV_EXPR
2166 || code
== CEIL_DIV_EXPR
2167 || code
== EXACT_DIV_EXPR
2168 || code
== ROUND_DIV_EXPR
2169 || code
== RSHIFT_EXPR
2170 || code
== LSHIFT_EXPR
);
2171 gcc_assert ((vr0
->type
== VR_RANGE
2172 || (code
== MULT_EXPR
&& vr0
->type
== VR_ANTI_RANGE
))
2173 && vr0
->type
== vr1
->type
);
2177 /* Compute the 4 cross operations. */
2179 val
[0] = vrp_int_const_binop (code
, vr0
->min
, vr1
->min
);
2180 if (val
[0] == NULL_TREE
)
2183 if (vr1
->max
== vr1
->min
)
2187 val
[1] = vrp_int_const_binop (code
, vr0
->min
, vr1
->max
);
2188 if (val
[1] == NULL_TREE
)
2192 if (vr0
->max
== vr0
->min
)
2196 val
[2] = vrp_int_const_binop (code
, vr0
->max
, vr1
->min
);
2197 if (val
[2] == NULL_TREE
)
2201 if (vr0
->min
== vr0
->max
|| vr1
->min
== vr1
->max
)
2205 val
[3] = vrp_int_const_binop (code
, vr0
->max
, vr1
->max
);
2206 if (val
[3] == NULL_TREE
)
2212 set_value_range_to_varying (vr
);
2216 /* Set MIN to the minimum of VAL[i] and MAX to the maximum
2220 for (i
= 1; i
< 4; i
++)
2222 if (!is_gimple_min_invariant (min
)
2223 || (TREE_OVERFLOW (min
) && !is_overflow_infinity (min
))
2224 || !is_gimple_min_invariant (max
)
2225 || (TREE_OVERFLOW (max
) && !is_overflow_infinity (max
)))
2230 if (!is_gimple_min_invariant (val
[i
])
2231 || (TREE_OVERFLOW (val
[i
])
2232 && !is_overflow_infinity (val
[i
])))
2234 /* If we found an overflowed value, set MIN and MAX
2235 to it so that we set the resulting range to
2241 if (compare_values (val
[i
], min
) == -1)
2244 if (compare_values (val
[i
], max
) == 1)
2249 /* If either MIN or MAX overflowed, then set the resulting range to
2250 VARYING. But we do accept an overflow infinity
2252 if (min
== NULL_TREE
2253 || !is_gimple_min_invariant (min
)
2254 || (TREE_OVERFLOW (min
) && !is_overflow_infinity (min
))
2256 || !is_gimple_min_invariant (max
)
2257 || (TREE_OVERFLOW (max
) && !is_overflow_infinity (max
)))
2259 set_value_range_to_varying (vr
);
2265 2) [-INF, +-INF(OVF)]
2266 3) [+-INF(OVF), +INF]
2267 4) [+-INF(OVF), +-INF(OVF)]
2268 We learn nothing when we have INF and INF(OVF) on both sides.
2269 Note that we do accept [-INF, -INF] and [+INF, +INF] without
2271 if ((vrp_val_is_min (min
) || is_overflow_infinity (min
))
2272 && (vrp_val_is_max (max
) || is_overflow_infinity (max
)))
2274 set_value_range_to_varying (vr
);
2278 cmp
= compare_values (min
, max
);
2279 if (cmp
== -2 || cmp
== 1)
2281 /* If the new range has its limits swapped around (MIN > MAX),
2282 then the operation caused one of them to wrap around, mark
2283 the new range VARYING. */
2284 set_value_range_to_varying (vr
);
2287 set_value_range (vr
, type
, min
, max
, NULL
);
2290 /* Extract range information from a binary operation CODE based on
2291 the ranges of each of its operands *VR0 and *VR1 with resulting
2292 type EXPR_TYPE. The resulting range is stored in *VR. */
2295 extract_range_from_binary_expr_1 (value_range_t
*vr
,
2296 enum tree_code code
, tree expr_type
,
2297 value_range_t
*vr0_
, value_range_t
*vr1_
)
2299 value_range_t vr0
= *vr0_
, vr1
= *vr1_
;
2300 value_range_t vrtem0
= VR_INITIALIZER
, vrtem1
= VR_INITIALIZER
;
2301 enum value_range_type type
;
2302 tree min
= NULL_TREE
, max
= NULL_TREE
;
2305 if (!INTEGRAL_TYPE_P (expr_type
)
2306 && !POINTER_TYPE_P (expr_type
))
2308 set_value_range_to_varying (vr
);
2312 /* Not all binary expressions can be applied to ranges in a
2313 meaningful way. Handle only arithmetic operations. */
2314 if (code
!= PLUS_EXPR
2315 && code
!= MINUS_EXPR
2316 && code
!= POINTER_PLUS_EXPR
2317 && code
!= MULT_EXPR
2318 && code
!= TRUNC_DIV_EXPR
2319 && code
!= FLOOR_DIV_EXPR
2320 && code
!= CEIL_DIV_EXPR
2321 && code
!= EXACT_DIV_EXPR
2322 && code
!= ROUND_DIV_EXPR
2323 && code
!= TRUNC_MOD_EXPR
2324 && code
!= RSHIFT_EXPR
2325 && code
!= LSHIFT_EXPR
2328 && code
!= BIT_AND_EXPR
2329 && code
!= BIT_IOR_EXPR
2330 && code
!= BIT_XOR_EXPR
)
2332 set_value_range_to_varying (vr
);
2336 /* If both ranges are UNDEFINED, so is the result. */
2337 if (vr0
.type
== VR_UNDEFINED
&& vr1
.type
== VR_UNDEFINED
)
2339 set_value_range_to_undefined (vr
);
2342 /* If one of the ranges is UNDEFINED drop it to VARYING for the following
2343 code. At some point we may want to special-case operations that
2344 have UNDEFINED result for all or some value-ranges of the not UNDEFINED
2346 else if (vr0
.type
== VR_UNDEFINED
)
2347 set_value_range_to_varying (&vr0
);
2348 else if (vr1
.type
== VR_UNDEFINED
)
2349 set_value_range_to_varying (&vr1
);
2351 /* Now canonicalize anti-ranges to ranges when they are not symbolic
2352 and express ~[] op X as ([]' op X) U ([]'' op X). */
2353 if (vr0
.type
== VR_ANTI_RANGE
2354 && ranges_from_anti_range (&vr0
, &vrtem0
, &vrtem1
))
2356 extract_range_from_binary_expr_1 (vr
, code
, expr_type
, &vrtem0
, vr1_
);
2357 if (vrtem1
.type
!= VR_UNDEFINED
)
2359 value_range_t vrres
= VR_INITIALIZER
;
2360 extract_range_from_binary_expr_1 (&vrres
, code
, expr_type
,
2362 vrp_meet (vr
, &vrres
);
2366 /* Likewise for X op ~[]. */
2367 if (vr1
.type
== VR_ANTI_RANGE
2368 && ranges_from_anti_range (&vr1
, &vrtem0
, &vrtem1
))
2370 extract_range_from_binary_expr_1 (vr
, code
, expr_type
, vr0_
, &vrtem0
);
2371 if (vrtem1
.type
!= VR_UNDEFINED
)
2373 value_range_t vrres
= VR_INITIALIZER
;
2374 extract_range_from_binary_expr_1 (&vrres
, code
, expr_type
,
2376 vrp_meet (vr
, &vrres
);
2381 /* The type of the resulting value range defaults to VR0.TYPE. */
2384 /* Refuse to operate on VARYING ranges, ranges of different kinds
2385 and symbolic ranges. As an exception, we allow BIT_{AND,IOR}
2386 because we may be able to derive a useful range even if one of
2387 the operands is VR_VARYING or symbolic range. Similarly for
2388 divisions, MIN/MAX and PLUS/MINUS.
2390 TODO, we may be able to derive anti-ranges in some cases. */
2391 if (code
!= BIT_AND_EXPR
2392 && code
!= BIT_IOR_EXPR
2393 && code
!= TRUNC_DIV_EXPR
2394 && code
!= FLOOR_DIV_EXPR
2395 && code
!= CEIL_DIV_EXPR
2396 && code
!= EXACT_DIV_EXPR
2397 && code
!= ROUND_DIV_EXPR
2398 && code
!= TRUNC_MOD_EXPR
2401 && code
!= PLUS_EXPR
2402 && code
!= MINUS_EXPR
2403 && code
!= RSHIFT_EXPR
2404 && (vr0
.type
== VR_VARYING
2405 || vr1
.type
== VR_VARYING
2406 || vr0
.type
!= vr1
.type
2407 || symbolic_range_p (&vr0
)
2408 || symbolic_range_p (&vr1
)))
2410 set_value_range_to_varying (vr
);
2414 /* Now evaluate the expression to determine the new range. */
2415 if (POINTER_TYPE_P (expr_type
))
2417 if (code
== MIN_EXPR
|| code
== MAX_EXPR
)
2419 /* For MIN/MAX expressions with pointers, we only care about
2420 nullness, if both are non null, then the result is nonnull.
2421 If both are null, then the result is null. Otherwise they
2423 if (range_is_nonnull (&vr0
) && range_is_nonnull (&vr1
))
2424 set_value_range_to_nonnull (vr
, expr_type
);
2425 else if (range_is_null (&vr0
) && range_is_null (&vr1
))
2426 set_value_range_to_null (vr
, expr_type
);
2428 set_value_range_to_varying (vr
);
2430 else if (code
== POINTER_PLUS_EXPR
)
2432 /* For pointer types, we are really only interested in asserting
2433 whether the expression evaluates to non-NULL. */
2434 if (range_is_nonnull (&vr0
) || range_is_nonnull (&vr1
))
2435 set_value_range_to_nonnull (vr
, expr_type
);
2436 else if (range_is_null (&vr0
) && range_is_null (&vr1
))
2437 set_value_range_to_null (vr
, expr_type
);
2439 set_value_range_to_varying (vr
);
2441 else if (code
== BIT_AND_EXPR
)
2443 /* For pointer types, we are really only interested in asserting
2444 whether the expression evaluates to non-NULL. */
2445 if (range_is_nonnull (&vr0
) && range_is_nonnull (&vr1
))
2446 set_value_range_to_nonnull (vr
, expr_type
);
2447 else if (range_is_null (&vr0
) || range_is_null (&vr1
))
2448 set_value_range_to_null (vr
, expr_type
);
2450 set_value_range_to_varying (vr
);
2453 set_value_range_to_varying (vr
);
2458 /* For integer ranges, apply the operation to each end of the
2459 range and see what we end up with. */
2460 if (code
== PLUS_EXPR
|| code
== MINUS_EXPR
)
2462 const bool minus_p
= (code
== MINUS_EXPR
);
2463 tree min_op0
= vr0
.min
;
2464 tree min_op1
= minus_p
? vr1
.max
: vr1
.min
;
2465 tree max_op0
= vr0
.max
;
2466 tree max_op1
= minus_p
? vr1
.min
: vr1
.max
;
2467 tree sym_min_op0
= NULL_TREE
;
2468 tree sym_min_op1
= NULL_TREE
;
2469 tree sym_max_op0
= NULL_TREE
;
2470 tree sym_max_op1
= NULL_TREE
;
2471 bool neg_min_op0
, neg_min_op1
, neg_max_op0
, neg_max_op1
;
2473 /* If we have a PLUS or MINUS with two VR_RANGEs, either constant or
2474 single-symbolic ranges, try to compute the precise resulting range,
2475 but only if we know that this resulting range will also be constant
2476 or single-symbolic. */
2477 if (vr0
.type
== VR_RANGE
&& vr1
.type
== VR_RANGE
2478 && (TREE_CODE (min_op0
) == INTEGER_CST
2480 = get_single_symbol (min_op0
, &neg_min_op0
, &min_op0
)))
2481 && (TREE_CODE (min_op1
) == INTEGER_CST
2483 = get_single_symbol (min_op1
, &neg_min_op1
, &min_op1
)))
2484 && (!(sym_min_op0
&& sym_min_op1
)
2485 || (sym_min_op0
== sym_min_op1
2486 && neg_min_op0
== (minus_p
? neg_min_op1
: !neg_min_op1
)))
2487 && (TREE_CODE (max_op0
) == INTEGER_CST
2489 = get_single_symbol (max_op0
, &neg_max_op0
, &max_op0
)))
2490 && (TREE_CODE (max_op1
) == INTEGER_CST
2492 = get_single_symbol (max_op1
, &neg_max_op1
, &max_op1
)))
2493 && (!(sym_max_op0
&& sym_max_op1
)
2494 || (sym_max_op0
== sym_max_op1
2495 && neg_max_op0
== (minus_p
? neg_max_op1
: !neg_max_op1
))))
2497 const signop sgn
= TYPE_SIGN (expr_type
);
2498 const unsigned int prec
= TYPE_PRECISION (expr_type
);
2499 wide_int type_min
, type_max
, wmin
, wmax
;
2503 /* Get the lower and upper bounds of the type. */
2504 if (TYPE_OVERFLOW_WRAPS (expr_type
))
2506 type_min
= wi::min_value (prec
, sgn
);
2507 type_max
= wi::max_value (prec
, sgn
);
2511 type_min
= vrp_val_min (expr_type
);
2512 type_max
= vrp_val_max (expr_type
);
2515 /* Combine the lower bounds, if any. */
2516 if (min_op0
&& min_op1
)
2520 wmin
= wi::sub (min_op0
, min_op1
);
2522 /* Check for overflow. */
2523 if (wi::cmp (0, min_op1
, sgn
)
2524 != wi::cmp (wmin
, min_op0
, sgn
))
2525 min_ovf
= wi::cmp (min_op0
, min_op1
, sgn
);
2529 wmin
= wi::add (min_op0
, min_op1
);
2531 /* Check for overflow. */
2532 if (wi::cmp (min_op1
, 0, sgn
)
2533 != wi::cmp (wmin
, min_op0
, sgn
))
2534 min_ovf
= wi::cmp (min_op0
, wmin
, sgn
);
2540 wmin
= minus_p
? wi::neg (min_op1
) : min_op1
;
2542 wmin
= wi::shwi (0, prec
);
2544 /* Combine the upper bounds, if any. */
2545 if (max_op0
&& max_op1
)
2549 wmax
= wi::sub (max_op0
, max_op1
);
2551 /* Check for overflow. */
2552 if (wi::cmp (0, max_op1
, sgn
)
2553 != wi::cmp (wmax
, max_op0
, sgn
))
2554 max_ovf
= wi::cmp (max_op0
, max_op1
, sgn
);
2558 wmax
= wi::add (max_op0
, max_op1
);
2560 if (wi::cmp (max_op1
, 0, sgn
)
2561 != wi::cmp (wmax
, max_op0
, sgn
))
2562 max_ovf
= wi::cmp (max_op0
, wmax
, sgn
);
2568 wmax
= minus_p
? wi::neg (max_op1
) : max_op1
;
2570 wmax
= wi::shwi (0, prec
);
2572 /* Check for type overflow. */
2575 if (wi::cmp (wmin
, type_min
, sgn
) == -1)
2577 else if (wi::cmp (wmin
, type_max
, sgn
) == 1)
2582 if (wi::cmp (wmax
, type_min
, sgn
) == -1)
2584 else if (wi::cmp (wmax
, type_max
, sgn
) == 1)
2588 /* If we have overflow for the constant part and the resulting
2589 range will be symbolic, drop to VR_VARYING. */
2590 if ((min_ovf
&& sym_min_op0
!= sym_min_op1
)
2591 || (max_ovf
&& sym_max_op0
!= sym_max_op1
))
2593 set_value_range_to_varying (vr
);
2597 if (TYPE_OVERFLOW_WRAPS (expr_type
))
2599 /* If overflow wraps, truncate the values and adjust the
2600 range kind and bounds appropriately. */
2601 wide_int tmin
= wide_int::from (wmin
, prec
, sgn
);
2602 wide_int tmax
= wide_int::from (wmax
, prec
, sgn
);
2603 if (min_ovf
== max_ovf
)
2605 /* No overflow or both overflow or underflow. The
2606 range kind stays VR_RANGE. */
2607 min
= wide_int_to_tree (expr_type
, tmin
);
2608 max
= wide_int_to_tree (expr_type
, tmax
);
2610 else if (min_ovf
== -1 && max_ovf
== 1)
2612 /* Underflow and overflow, drop to VR_VARYING. */
2613 set_value_range_to_varying (vr
);
2618 /* Min underflow or max overflow. The range kind
2619 changes to VR_ANTI_RANGE. */
2620 bool covers
= false;
2621 wide_int tem
= tmin
;
2622 gcc_assert ((min_ovf
== -1 && max_ovf
== 0)
2623 || (max_ovf
== 1 && min_ovf
== 0));
2624 type
= VR_ANTI_RANGE
;
2626 if (wi::cmp (tmin
, tmax
, sgn
) < 0)
2629 if (wi::cmp (tmax
, tem
, sgn
) > 0)
2631 /* If the anti-range would cover nothing, drop to varying.
2632 Likewise if the anti-range bounds are outside of the
2634 if (covers
|| wi::cmp (tmin
, tmax
, sgn
) > 0)
2636 set_value_range_to_varying (vr
);
2639 min
= wide_int_to_tree (expr_type
, tmin
);
2640 max
= wide_int_to_tree (expr_type
, tmax
);
2645 /* If overflow does not wrap, saturate to the types min/max
2649 if (needs_overflow_infinity (expr_type
)
2650 && supports_overflow_infinity (expr_type
))
2651 min
= negative_overflow_infinity (expr_type
);
2653 min
= wide_int_to_tree (expr_type
, type_min
);
2655 else if (min_ovf
== 1)
2657 if (needs_overflow_infinity (expr_type
)
2658 && supports_overflow_infinity (expr_type
))
2659 min
= positive_overflow_infinity (expr_type
);
2661 min
= wide_int_to_tree (expr_type
, type_max
);
2664 min
= wide_int_to_tree (expr_type
, wmin
);
2668 if (needs_overflow_infinity (expr_type
)
2669 && supports_overflow_infinity (expr_type
))
2670 max
= negative_overflow_infinity (expr_type
);
2672 max
= wide_int_to_tree (expr_type
, type_min
);
2674 else if (max_ovf
== 1)
2676 if (needs_overflow_infinity (expr_type
)
2677 && supports_overflow_infinity (expr_type
))
2678 max
= positive_overflow_infinity (expr_type
);
2680 max
= wide_int_to_tree (expr_type
, type_max
);
2683 max
= wide_int_to_tree (expr_type
, wmax
);
2686 if (needs_overflow_infinity (expr_type
)
2687 && supports_overflow_infinity (expr_type
))
2689 if ((min_op0
&& is_negative_overflow_infinity (min_op0
))
2692 ? is_positive_overflow_infinity (min_op1
)
2693 : is_negative_overflow_infinity (min_op1
))))
2694 min
= negative_overflow_infinity (expr_type
);
2695 if ((max_op0
&& is_positive_overflow_infinity (max_op0
))
2698 ? is_negative_overflow_infinity (max_op1
)
2699 : is_positive_overflow_infinity (max_op1
))))
2700 max
= positive_overflow_infinity (expr_type
);
2703 /* If the result lower bound is constant, we're done;
2704 otherwise, build the symbolic lower bound. */
2705 if (sym_min_op0
== sym_min_op1
)
2707 else if (sym_min_op0
)
2708 min
= build_symbolic_expr (expr_type
, sym_min_op0
,
2710 else if (sym_min_op1
)
2711 min
= build_symbolic_expr (expr_type
, sym_min_op1
,
2712 neg_min_op1
^ minus_p
, min
);
2714 /* Likewise for the upper bound. */
2715 if (sym_max_op0
== sym_max_op1
)
2717 else if (sym_max_op0
)
2718 max
= build_symbolic_expr (expr_type
, sym_max_op0
,
2720 else if (sym_max_op1
)
2721 max
= build_symbolic_expr (expr_type
, sym_max_op1
,
2722 neg_max_op1
^ minus_p
, max
);
2726 /* For other cases, for example if we have a PLUS_EXPR with two
2727 VR_ANTI_RANGEs, drop to VR_VARYING. It would take more effort
2728 to compute a precise range for such a case.
2729 ??? General even mixed range kind operations can be expressed
2730 by for example transforming ~[3, 5] + [1, 2] to range-only
2731 operations and a union primitive:
2732 [-INF, 2] + [1, 2] U [5, +INF] + [1, 2]
2733 [-INF+1, 4] U [6, +INF(OVF)]
2734 though usually the union is not exactly representable with
2735 a single range or anti-range as the above is
2736 [-INF+1, +INF(OVF)] intersected with ~[5, 5]
2737 but one could use a scheme similar to equivalences for this. */
2738 set_value_range_to_varying (vr
);
2742 else if (code
== MIN_EXPR
2743 || code
== MAX_EXPR
)
2745 if (vr0
.type
== VR_RANGE
2746 && !symbolic_range_p (&vr0
))
2749 if (vr1
.type
== VR_RANGE
2750 && !symbolic_range_p (&vr1
))
2752 /* For operations that make the resulting range directly
2753 proportional to the original ranges, apply the operation to
2754 the same end of each range. */
2755 min
= vrp_int_const_binop (code
, vr0
.min
, vr1
.min
);
2756 max
= vrp_int_const_binop (code
, vr0
.max
, vr1
.max
);
2758 else if (code
== MIN_EXPR
)
2760 min
= vrp_val_min (expr_type
);
2763 else if (code
== MAX_EXPR
)
2766 max
= vrp_val_max (expr_type
);
2769 else if (vr1
.type
== VR_RANGE
2770 && !symbolic_range_p (&vr1
))
2773 if (code
== MIN_EXPR
)
2775 min
= vrp_val_min (expr_type
);
2778 else if (code
== MAX_EXPR
)
2781 max
= vrp_val_max (expr_type
);
2786 set_value_range_to_varying (vr
);
2790 else if (code
== MULT_EXPR
)
2792 /* Fancy code so that with unsigned, [-3,-1]*[-3,-1] does not
2793 drop to varying. This test requires 2*prec bits if both
2794 operands are signed and 2*prec + 2 bits if either is not. */
2796 signop sign
= TYPE_SIGN (expr_type
);
2797 unsigned int prec
= TYPE_PRECISION (expr_type
);
2799 if (range_int_cst_p (&vr0
)
2800 && range_int_cst_p (&vr1
)
2801 && TYPE_OVERFLOW_WRAPS (expr_type
))
2803 typedef FIXED_WIDE_INT (WIDE_INT_MAX_PRECISION
* 2) vrp_int
;
2804 typedef generic_wide_int
2805 <wi::extended_tree
<WIDE_INT_MAX_PRECISION
* 2> > vrp_int_cst
;
2806 vrp_int sizem1
= wi::mask
<vrp_int
> (prec
, false);
2807 vrp_int size
= sizem1
+ 1;
2809 /* Extend the values using the sign of the result to PREC2.
2810 From here on out, everthing is just signed math no matter
2811 what the input types were. */
2812 vrp_int min0
= vrp_int_cst (vr0
.min
);
2813 vrp_int max0
= vrp_int_cst (vr0
.max
);
2814 vrp_int min1
= vrp_int_cst (vr1
.min
);
2815 vrp_int max1
= vrp_int_cst (vr1
.max
);
2816 /* Canonicalize the intervals. */
2817 if (sign
== UNSIGNED
)
2819 if (wi::ltu_p (size
, min0
+ max0
))
2825 if (wi::ltu_p (size
, min1
+ max1
))
2832 vrp_int prod0
= min0
* min1
;
2833 vrp_int prod1
= min0
* max1
;
2834 vrp_int prod2
= max0
* min1
;
2835 vrp_int prod3
= max0
* max1
;
2837 /* Sort the 4 products so that min is in prod0 and max is in
2839 /* min0min1 > max0max1 */
2840 if (wi::gts_p (prod0
, prod3
))
2841 std::swap (prod0
, prod3
);
2843 /* min0max1 > max0min1 */
2844 if (wi::gts_p (prod1
, prod2
))
2845 std::swap (prod1
, prod2
);
2847 if (wi::gts_p (prod0
, prod1
))
2848 std::swap (prod0
, prod1
);
2850 if (wi::gts_p (prod2
, prod3
))
2851 std::swap (prod2
, prod3
);
2853 /* diff = max - min. */
2854 prod2
= prod3
- prod0
;
2855 if (wi::geu_p (prod2
, sizem1
))
2857 /* the range covers all values. */
2858 set_value_range_to_varying (vr
);
2862 /* The following should handle the wrapping and selecting
2863 VR_ANTI_RANGE for us. */
2864 min
= wide_int_to_tree (expr_type
, prod0
);
2865 max
= wide_int_to_tree (expr_type
, prod3
);
2866 set_and_canonicalize_value_range (vr
, VR_RANGE
, min
, max
, NULL
);
2870 /* If we have an unsigned MULT_EXPR with two VR_ANTI_RANGEs,
2871 drop to VR_VARYING. It would take more effort to compute a
2872 precise range for such a case. For example, if we have
2873 op0 == 65536 and op1 == 65536 with their ranges both being
2874 ~[0,0] on a 32-bit machine, we would have op0 * op1 == 0, so
2875 we cannot claim that the product is in ~[0,0]. Note that we
2876 are guaranteed to have vr0.type == vr1.type at this
2878 if (vr0
.type
== VR_ANTI_RANGE
2879 && !TYPE_OVERFLOW_UNDEFINED (expr_type
))
2881 set_value_range_to_varying (vr
);
2885 extract_range_from_multiplicative_op_1 (vr
, code
, &vr0
, &vr1
);
2888 else if (code
== RSHIFT_EXPR
2889 || code
== LSHIFT_EXPR
)
2891 /* If we have a RSHIFT_EXPR with any shift values outside [0..prec-1],
2892 then drop to VR_VARYING. Outside of this range we get undefined
2893 behavior from the shift operation. We cannot even trust
2894 SHIFT_COUNT_TRUNCATED at this stage, because that applies to rtl
2895 shifts, and the operation at the tree level may be widened. */
2896 if (range_int_cst_p (&vr1
)
2897 && compare_tree_int (vr1
.min
, 0) >= 0
2898 && compare_tree_int (vr1
.max
, TYPE_PRECISION (expr_type
)) == -1)
2900 if (code
== RSHIFT_EXPR
)
2902 /* Even if vr0 is VARYING or otherwise not usable, we can derive
2903 useful ranges just from the shift count. E.g.
2904 x >> 63 for signed 64-bit x is always [-1, 0]. */
2905 if (vr0
.type
!= VR_RANGE
|| symbolic_range_p (&vr0
))
2907 vr0
.type
= type
= VR_RANGE
;
2908 vr0
.min
= vrp_val_min (expr_type
);
2909 vr0
.max
= vrp_val_max (expr_type
);
2911 extract_range_from_multiplicative_op_1 (vr
, code
, &vr0
, &vr1
);
2914 /* We can map lshifts by constants to MULT_EXPR handling. */
2915 else if (code
== LSHIFT_EXPR
2916 && range_int_cst_singleton_p (&vr1
))
2918 bool saved_flag_wrapv
;
2919 value_range_t vr1p
= VR_INITIALIZER
;
2920 vr1p
.type
= VR_RANGE
;
2921 vr1p
.min
= (wide_int_to_tree
2923 wi::set_bit_in_zero (tree_to_shwi (vr1
.min
),
2924 TYPE_PRECISION (expr_type
))));
2925 vr1p
.max
= vr1p
.min
;
2926 /* We have to use a wrapping multiply though as signed overflow
2927 on lshifts is implementation defined in C89. */
2928 saved_flag_wrapv
= flag_wrapv
;
2930 extract_range_from_binary_expr_1 (vr
, MULT_EXPR
, expr_type
,
2932 flag_wrapv
= saved_flag_wrapv
;
2935 else if (code
== LSHIFT_EXPR
2936 && range_int_cst_p (&vr0
))
2938 int prec
= TYPE_PRECISION (expr_type
);
2939 int overflow_pos
= prec
;
2941 wide_int low_bound
, high_bound
;
2942 bool uns
= TYPE_UNSIGNED (expr_type
);
2943 bool in_bounds
= false;
2948 bound_shift
= overflow_pos
- tree_to_shwi (vr1
.max
);
2949 /* If bound_shift == HOST_BITS_PER_WIDE_INT, the llshift can
2950 overflow. However, for that to happen, vr1.max needs to be
2951 zero, which means vr1 is a singleton range of zero, which
2952 means it should be handled by the previous LSHIFT_EXPR
2954 wide_int bound
= wi::set_bit_in_zero (bound_shift
, prec
);
2955 wide_int complement
= ~(bound
- 1);
2960 high_bound
= complement
;
2961 if (wi::ltu_p (vr0
.max
, low_bound
))
2963 /* [5, 6] << [1, 2] == [10, 24]. */
2964 /* We're shifting out only zeroes, the value increases
2968 else if (wi::ltu_p (high_bound
, vr0
.min
))
2970 /* [0xffffff00, 0xffffffff] << [1, 2]
2971 == [0xfffffc00, 0xfffffffe]. */
2972 /* We're shifting out only ones, the value decreases
2979 /* [-1, 1] << [1, 2] == [-4, 4]. */
2980 low_bound
= complement
;
2982 if (wi::lts_p (vr0
.max
, high_bound
)
2983 && wi::lts_p (low_bound
, vr0
.min
))
2985 /* For non-negative numbers, we're shifting out only
2986 zeroes, the value increases monotonically.
2987 For negative numbers, we're shifting out only ones, the
2988 value decreases monotomically. */
2995 extract_range_from_multiplicative_op_1 (vr
, code
, &vr0
, &vr1
);
3000 set_value_range_to_varying (vr
);
3003 else if (code
== TRUNC_DIV_EXPR
3004 || code
== FLOOR_DIV_EXPR
3005 || code
== CEIL_DIV_EXPR
3006 || code
== EXACT_DIV_EXPR
3007 || code
== ROUND_DIV_EXPR
)
3009 if (vr0
.type
!= VR_RANGE
|| symbolic_range_p (&vr0
))
3011 /* For division, if op1 has VR_RANGE but op0 does not, something
3012 can be deduced just from that range. Say [min, max] / [4, max]
3013 gives [min / 4, max / 4] range. */
3014 if (vr1
.type
== VR_RANGE
3015 && !symbolic_range_p (&vr1
)
3016 && range_includes_zero_p (vr1
.min
, vr1
.max
) == 0)
3018 vr0
.type
= type
= VR_RANGE
;
3019 vr0
.min
= vrp_val_min (expr_type
);
3020 vr0
.max
= vrp_val_max (expr_type
);
3024 set_value_range_to_varying (vr
);
3029 /* For divisions, if flag_non_call_exceptions is true, we must
3030 not eliminate a division by zero. */
3031 if (cfun
->can_throw_non_call_exceptions
3032 && (vr1
.type
!= VR_RANGE
3033 || range_includes_zero_p (vr1
.min
, vr1
.max
) != 0))
3035 set_value_range_to_varying (vr
);
3039 /* For divisions, if op0 is VR_RANGE, we can deduce a range
3040 even if op1 is VR_VARYING, VR_ANTI_RANGE, symbolic or can
3042 if (vr0
.type
== VR_RANGE
3043 && (vr1
.type
!= VR_RANGE
3044 || range_includes_zero_p (vr1
.min
, vr1
.max
) != 0))
3046 tree zero
= build_int_cst (TREE_TYPE (vr0
.min
), 0);
3051 if (TYPE_UNSIGNED (expr_type
)
3052 || value_range_nonnegative_p (&vr1
))
3054 /* For unsigned division or when divisor is known
3055 to be non-negative, the range has to cover
3056 all numbers from 0 to max for positive max
3057 and all numbers from min to 0 for negative min. */
3058 cmp
= compare_values (vr0
.max
, zero
);
3061 /* When vr0.max < 0, vr1.min != 0 and value
3062 ranges for dividend and divisor are available. */
3063 if (vr1
.type
== VR_RANGE
3064 && !symbolic_range_p (&vr0
)
3065 && !symbolic_range_p (&vr1
)
3066 && !compare_values (vr1
.min
, zero
))
3067 max
= int_const_binop (code
, vr0
.max
, vr1
.min
);
3071 else if (cmp
== 0 || cmp
== 1)
3075 cmp
= compare_values (vr0
.min
, zero
);
3078 /* For unsigned division when value ranges for dividend
3079 and divisor are available. */
3080 if (vr1
.type
== VR_RANGE
3081 && !symbolic_range_p (&vr0
)
3082 && !symbolic_range_p (&vr1
))
3083 min
= int_const_binop (code
, vr0
.min
, vr1
.max
);
3087 else if (cmp
== 0 || cmp
== -1)
3094 /* Otherwise the range is -max .. max or min .. -min
3095 depending on which bound is bigger in absolute value,
3096 as the division can change the sign. */
3097 abs_extent_range (vr
, vr0
.min
, vr0
.max
);
3100 if (type
== VR_VARYING
)
3102 set_value_range_to_varying (vr
);
3108 extract_range_from_multiplicative_op_1 (vr
, code
, &vr0
, &vr1
);
3112 else if (code
== TRUNC_MOD_EXPR
)
3114 if (range_is_null (&vr1
))
3116 set_value_range_to_undefined (vr
);
3119 /* ABS (A % B) < ABS (B) and either
3120 0 <= A % B <= A or A <= A % B <= 0. */
3122 signop sgn
= TYPE_SIGN (expr_type
);
3123 unsigned int prec
= TYPE_PRECISION (expr_type
);
3124 wide_int wmin
, wmax
, tmp
;
3125 wide_int zero
= wi::zero (prec
);
3126 wide_int one
= wi::one (prec
);
3127 if (vr1
.type
== VR_RANGE
&& !symbolic_range_p (&vr1
))
3129 wmax
= wi::sub (vr1
.max
, one
);
3132 tmp
= wi::sub (wi::minus_one (prec
), vr1
.min
);
3133 wmax
= wi::smax (wmax
, tmp
);
3138 wmax
= wi::max_value (prec
, sgn
);
3139 /* X % INT_MIN may be INT_MAX. */
3140 if (sgn
== UNSIGNED
)
3144 if (sgn
== UNSIGNED
)
3149 if (vr0
.type
== VR_RANGE
&& TREE_CODE (vr0
.min
) == INTEGER_CST
)
3152 if (wi::gts_p (tmp
, zero
))
3154 wmin
= wi::smax (wmin
, tmp
);
3158 if (vr0
.type
== VR_RANGE
&& TREE_CODE (vr0
.max
) == INTEGER_CST
)
3161 if (sgn
== SIGNED
&& wi::neg_p (tmp
))
3163 wmax
= wi::min (wmax
, tmp
, sgn
);
3166 min
= wide_int_to_tree (expr_type
, wmin
);
3167 max
= wide_int_to_tree (expr_type
, wmax
);
3169 else if (code
== BIT_AND_EXPR
|| code
== BIT_IOR_EXPR
|| code
== BIT_XOR_EXPR
)
3171 bool int_cst_range0
, int_cst_range1
;
3172 wide_int may_be_nonzero0
, may_be_nonzero1
;
3173 wide_int must_be_nonzero0
, must_be_nonzero1
;
3175 int_cst_range0
= zero_nonzero_bits_from_vr (expr_type
, &vr0
,
3178 int_cst_range1
= zero_nonzero_bits_from_vr (expr_type
, &vr1
,
3183 if (code
== BIT_AND_EXPR
)
3185 min
= wide_int_to_tree (expr_type
,
3186 must_be_nonzero0
& must_be_nonzero1
);
3187 wide_int wmax
= may_be_nonzero0
& may_be_nonzero1
;
3188 /* If both input ranges contain only negative values we can
3189 truncate the result range maximum to the minimum of the
3190 input range maxima. */
3191 if (int_cst_range0
&& int_cst_range1
3192 && tree_int_cst_sgn (vr0
.max
) < 0
3193 && tree_int_cst_sgn (vr1
.max
) < 0)
3195 wmax
= wi::min (wmax
, vr0
.max
, TYPE_SIGN (expr_type
));
3196 wmax
= wi::min (wmax
, vr1
.max
, TYPE_SIGN (expr_type
));
3198 /* If either input range contains only non-negative values
3199 we can truncate the result range maximum to the respective
3200 maximum of the input range. */
3201 if (int_cst_range0
&& tree_int_cst_sgn (vr0
.min
) >= 0)
3202 wmax
= wi::min (wmax
, vr0
.max
, TYPE_SIGN (expr_type
));
3203 if (int_cst_range1
&& tree_int_cst_sgn (vr1
.min
) >= 0)
3204 wmax
= wi::min (wmax
, vr1
.max
, TYPE_SIGN (expr_type
));
3205 max
= wide_int_to_tree (expr_type
, wmax
);
3207 else if (code
== BIT_IOR_EXPR
)
3209 max
= wide_int_to_tree (expr_type
,
3210 may_be_nonzero0
| may_be_nonzero1
);
3211 wide_int wmin
= must_be_nonzero0
| must_be_nonzero1
;
3212 /* If the input ranges contain only positive values we can
3213 truncate the minimum of the result range to the maximum
3214 of the input range minima. */
3215 if (int_cst_range0
&& int_cst_range1
3216 && tree_int_cst_sgn (vr0
.min
) >= 0
3217 && tree_int_cst_sgn (vr1
.min
) >= 0)
3219 wmin
= wi::max (wmin
, vr0
.min
, TYPE_SIGN (expr_type
));
3220 wmin
= wi::max (wmin
, vr1
.min
, TYPE_SIGN (expr_type
));
3222 /* If either input range contains only negative values
3223 we can truncate the minimum of the result range to the
3224 respective minimum range. */
3225 if (int_cst_range0
&& tree_int_cst_sgn (vr0
.max
) < 0)
3226 wmin
= wi::max (wmin
, vr0
.min
, TYPE_SIGN (expr_type
));
3227 if (int_cst_range1
&& tree_int_cst_sgn (vr1
.max
) < 0)
3228 wmin
= wi::max (wmin
, vr1
.min
, TYPE_SIGN (expr_type
));
3229 min
= wide_int_to_tree (expr_type
, wmin
);
3231 else if (code
== BIT_XOR_EXPR
)
3233 wide_int result_zero_bits
= ((must_be_nonzero0
& must_be_nonzero1
)
3234 | ~(may_be_nonzero0
| may_be_nonzero1
));
3235 wide_int result_one_bits
3236 = (must_be_nonzero0
.and_not (may_be_nonzero1
)
3237 | must_be_nonzero1
.and_not (may_be_nonzero0
));
3238 max
= wide_int_to_tree (expr_type
, ~result_zero_bits
);
3239 min
= wide_int_to_tree (expr_type
, result_one_bits
);
3240 /* If the range has all positive or all negative values the
3241 result is better than VARYING. */
3242 if (tree_int_cst_sgn (min
) < 0
3243 || tree_int_cst_sgn (max
) >= 0)
3246 max
= min
= NULL_TREE
;
3252 /* If either MIN or MAX overflowed, then set the resulting range to
3253 VARYING. But we do accept an overflow infinity representation. */
3254 if (min
== NULL_TREE
3255 || (TREE_OVERFLOW_P (min
) && !is_overflow_infinity (min
))
3257 || (TREE_OVERFLOW_P (max
) && !is_overflow_infinity (max
)))
3259 set_value_range_to_varying (vr
);
3265 2) [-INF, +-INF(OVF)]
3266 3) [+-INF(OVF), +INF]
3267 4) [+-INF(OVF), +-INF(OVF)]
3268 We learn nothing when we have INF and INF(OVF) on both sides.
3269 Note that we do accept [-INF, -INF] and [+INF, +INF] without
3271 if ((vrp_val_is_min (min
) || is_overflow_infinity (min
))
3272 && (vrp_val_is_max (max
) || is_overflow_infinity (max
)))
3274 set_value_range_to_varying (vr
);
3278 cmp
= compare_values (min
, max
);
3279 if (cmp
== -2 || cmp
== 1)
3281 /* If the new range has its limits swapped around (MIN > MAX),
3282 then the operation caused one of them to wrap around, mark
3283 the new range VARYING. */
3284 set_value_range_to_varying (vr
);
3287 set_value_range (vr
, type
, min
, max
, NULL
);
3290 /* Extract range information from a binary expression OP0 CODE OP1 based on
3291 the ranges of each of its operands with resulting type EXPR_TYPE.
3292 The resulting range is stored in *VR. */
3295 extract_range_from_binary_expr (value_range_t
*vr
,
3296 enum tree_code code
,
3297 tree expr_type
, tree op0
, tree op1
)
3299 value_range_t vr0
= VR_INITIALIZER
;
3300 value_range_t vr1
= VR_INITIALIZER
;
3302 /* Get value ranges for each operand. For constant operands, create
3303 a new value range with the operand to simplify processing. */
3304 if (TREE_CODE (op0
) == SSA_NAME
)
3305 vr0
= *(get_value_range (op0
));
3306 else if (is_gimple_min_invariant (op0
))
3307 set_value_range_to_value (&vr0
, op0
, NULL
);
3309 set_value_range_to_varying (&vr0
);
3311 if (TREE_CODE (op1
) == SSA_NAME
)
3312 vr1
= *(get_value_range (op1
));
3313 else if (is_gimple_min_invariant (op1
))
3314 set_value_range_to_value (&vr1
, op1
, NULL
);
3316 set_value_range_to_varying (&vr1
);
3318 extract_range_from_binary_expr_1 (vr
, code
, expr_type
, &vr0
, &vr1
);
3320 /* Try harder for PLUS and MINUS if the range of one operand is symbolic
3321 and based on the other operand, for example if it was deduced from a
3322 symbolic comparison. When a bound of the range of the first operand
3323 is invariant, we set the corresponding bound of the new range to INF
3324 in order to avoid recursing on the range of the second operand. */
3325 if (vr
->type
== VR_VARYING
3326 && (code
== PLUS_EXPR
|| code
== MINUS_EXPR
)
3327 && TREE_CODE (op1
) == SSA_NAME
3328 && vr0
.type
== VR_RANGE
3329 && symbolic_range_based_on_p (&vr0
, op1
))
3331 const bool minus_p
= (code
== MINUS_EXPR
);
3332 value_range_t n_vr1
= VR_INITIALIZER
;
3334 /* Try with VR0 and [-INF, OP1]. */
3335 if (is_gimple_min_invariant (minus_p
? vr0
.max
: vr0
.min
))
3336 set_value_range (&n_vr1
, VR_RANGE
, vrp_val_min (expr_type
), op1
, NULL
);
3338 /* Try with VR0 and [OP1, +INF]. */
3339 else if (is_gimple_min_invariant (minus_p
? vr0
.min
: vr0
.max
))
3340 set_value_range (&n_vr1
, VR_RANGE
, op1
, vrp_val_max (expr_type
), NULL
);
3342 /* Try with VR0 and [OP1, OP1]. */
3344 set_value_range (&n_vr1
, VR_RANGE
, op1
, op1
, NULL
);
3346 extract_range_from_binary_expr_1 (vr
, code
, expr_type
, &vr0
, &n_vr1
);
3349 if (vr
->type
== VR_VARYING
3350 && (code
== PLUS_EXPR
|| code
== MINUS_EXPR
)
3351 && TREE_CODE (op0
) == SSA_NAME
3352 && vr1
.type
== VR_RANGE
3353 && symbolic_range_based_on_p (&vr1
, op0
))
3355 const bool minus_p
= (code
== MINUS_EXPR
);
3356 value_range_t n_vr0
= VR_INITIALIZER
;
3358 /* Try with [-INF, OP0] and VR1. */
3359 if (is_gimple_min_invariant (minus_p
? vr1
.max
: vr1
.min
))
3360 set_value_range (&n_vr0
, VR_RANGE
, vrp_val_min (expr_type
), op0
, NULL
);
3362 /* Try with [OP0, +INF] and VR1. */
3363 else if (is_gimple_min_invariant (minus_p
? vr1
.min
: vr1
.max
))
3364 set_value_range (&n_vr0
, VR_RANGE
, op0
, vrp_val_max (expr_type
), NULL
);
3366 /* Try with [OP0, OP0] and VR1. */
3368 set_value_range (&n_vr0
, VR_RANGE
, op0
, op0
, NULL
);
3370 extract_range_from_binary_expr_1 (vr
, code
, expr_type
, &n_vr0
, &vr1
);
3374 /* Extract range information from a unary operation CODE based on
3375 the range of its operand *VR0 with type OP0_TYPE with resulting type TYPE.
3376 The resulting range is stored in *VR. */
3379 extract_range_from_unary_expr_1 (value_range_t
*vr
,
3380 enum tree_code code
, tree type
,
3381 value_range_t
*vr0_
, tree op0_type
)
3383 value_range_t vr0
= *vr0_
, vrtem0
= VR_INITIALIZER
, vrtem1
= VR_INITIALIZER
;
3385 /* VRP only operates on integral and pointer types. */
3386 if (!(INTEGRAL_TYPE_P (op0_type
)
3387 || POINTER_TYPE_P (op0_type
))
3388 || !(INTEGRAL_TYPE_P (type
)
3389 || POINTER_TYPE_P (type
)))
3391 set_value_range_to_varying (vr
);
3395 /* If VR0 is UNDEFINED, so is the result. */
3396 if (vr0
.type
== VR_UNDEFINED
)
3398 set_value_range_to_undefined (vr
);
3402 /* Handle operations that we express in terms of others. */
3403 if (code
== PAREN_EXPR
|| code
== OBJ_TYPE_REF
)
3405 /* PAREN_EXPR and OBJ_TYPE_REF are simple copies. */
3406 copy_value_range (vr
, &vr0
);
3409 else if (code
== NEGATE_EXPR
)
3411 /* -X is simply 0 - X, so re-use existing code that also handles
3412 anti-ranges fine. */
3413 value_range_t zero
= VR_INITIALIZER
;
3414 set_value_range_to_value (&zero
, build_int_cst (type
, 0), NULL
);
3415 extract_range_from_binary_expr_1 (vr
, MINUS_EXPR
, type
, &zero
, &vr0
);
3418 else if (code
== BIT_NOT_EXPR
)
3420 /* ~X is simply -1 - X, so re-use existing code that also handles
3421 anti-ranges fine. */
3422 value_range_t minusone
= VR_INITIALIZER
;
3423 set_value_range_to_value (&minusone
, build_int_cst (type
, -1), NULL
);
3424 extract_range_from_binary_expr_1 (vr
, MINUS_EXPR
,
3425 type
, &minusone
, &vr0
);
3429 /* Now canonicalize anti-ranges to ranges when they are not symbolic
3430 and express op ~[] as (op []') U (op []''). */
3431 if (vr0
.type
== VR_ANTI_RANGE
3432 && ranges_from_anti_range (&vr0
, &vrtem0
, &vrtem1
))
3434 extract_range_from_unary_expr_1 (vr
, code
, type
, &vrtem0
, op0_type
);
3435 if (vrtem1
.type
!= VR_UNDEFINED
)
3437 value_range_t vrres
= VR_INITIALIZER
;
3438 extract_range_from_unary_expr_1 (&vrres
, code
, type
,
3440 vrp_meet (vr
, &vrres
);
3445 if (CONVERT_EXPR_CODE_P (code
))
3447 tree inner_type
= op0_type
;
3448 tree outer_type
= type
;
3450 /* If the expression evaluates to a pointer, we are only interested in
3451 determining if it evaluates to NULL [0, 0] or non-NULL (~[0, 0]). */
3452 if (POINTER_TYPE_P (type
))
3454 if (range_is_nonnull (&vr0
))
3455 set_value_range_to_nonnull (vr
, type
);
3456 else if (range_is_null (&vr0
))
3457 set_value_range_to_null (vr
, type
);
3459 set_value_range_to_varying (vr
);
3463 /* If VR0 is varying and we increase the type precision, assume
3464 a full range for the following transformation. */
3465 if (vr0
.type
== VR_VARYING
3466 && INTEGRAL_TYPE_P (inner_type
)
3467 && TYPE_PRECISION (inner_type
) < TYPE_PRECISION (outer_type
))
3469 vr0
.type
= VR_RANGE
;
3470 vr0
.min
= TYPE_MIN_VALUE (inner_type
);
3471 vr0
.max
= TYPE_MAX_VALUE (inner_type
);
3474 /* If VR0 is a constant range or anti-range and the conversion is
3475 not truncating we can convert the min and max values and
3476 canonicalize the resulting range. Otherwise we can do the
3477 conversion if the size of the range is less than what the
3478 precision of the target type can represent and the range is
3479 not an anti-range. */
3480 if ((vr0
.type
== VR_RANGE
3481 || vr0
.type
== VR_ANTI_RANGE
)
3482 && TREE_CODE (vr0
.min
) == INTEGER_CST
3483 && TREE_CODE (vr0
.max
) == INTEGER_CST
3484 && (!is_overflow_infinity (vr0
.min
)
3485 || (vr0
.type
== VR_RANGE
3486 && TYPE_PRECISION (outer_type
) > TYPE_PRECISION (inner_type
)
3487 && needs_overflow_infinity (outer_type
)
3488 && supports_overflow_infinity (outer_type
)))
3489 && (!is_overflow_infinity (vr0
.max
)
3490 || (vr0
.type
== VR_RANGE
3491 && TYPE_PRECISION (outer_type
) > TYPE_PRECISION (inner_type
)
3492 && needs_overflow_infinity (outer_type
)
3493 && supports_overflow_infinity (outer_type
)))
3494 && (TYPE_PRECISION (outer_type
) >= TYPE_PRECISION (inner_type
)
3495 || (vr0
.type
== VR_RANGE
3496 && integer_zerop (int_const_binop (RSHIFT_EXPR
,
3497 int_const_binop (MINUS_EXPR
, vr0
.max
, vr0
.min
),
3498 size_int (TYPE_PRECISION (outer_type
)))))))
3500 tree new_min
, new_max
;
3501 if (is_overflow_infinity (vr0
.min
))
3502 new_min
= negative_overflow_infinity (outer_type
);
3504 new_min
= force_fit_type (outer_type
, wi::to_widest (vr0
.min
),
3506 if (is_overflow_infinity (vr0
.max
))
3507 new_max
= positive_overflow_infinity (outer_type
);
3509 new_max
= force_fit_type (outer_type
, wi::to_widest (vr0
.max
),
3511 set_and_canonicalize_value_range (vr
, vr0
.type
,
3512 new_min
, new_max
, NULL
);
3516 set_value_range_to_varying (vr
);
3519 else if (code
== ABS_EXPR
)
3524 /* Pass through vr0 in the easy cases. */
3525 if (TYPE_UNSIGNED (type
)
3526 || value_range_nonnegative_p (&vr0
))
3528 copy_value_range (vr
, &vr0
);
3532 /* For the remaining varying or symbolic ranges we can't do anything
3534 if (vr0
.type
== VR_VARYING
3535 || symbolic_range_p (&vr0
))
3537 set_value_range_to_varying (vr
);
3541 /* -TYPE_MIN_VALUE = TYPE_MIN_VALUE with flag_wrapv so we can't get a
3543 if (!TYPE_OVERFLOW_UNDEFINED (type
)
3544 && ((vr0
.type
== VR_RANGE
3545 && vrp_val_is_min (vr0
.min
))
3546 || (vr0
.type
== VR_ANTI_RANGE
3547 && !vrp_val_is_min (vr0
.min
))))
3549 set_value_range_to_varying (vr
);
3553 /* ABS_EXPR may flip the range around, if the original range
3554 included negative values. */
3555 if (is_overflow_infinity (vr0
.min
))
3556 min
= positive_overflow_infinity (type
);
3557 else if (!vrp_val_is_min (vr0
.min
))
3558 min
= fold_unary_to_constant (code
, type
, vr0
.min
);
3559 else if (!needs_overflow_infinity (type
))
3560 min
= TYPE_MAX_VALUE (type
);
3561 else if (supports_overflow_infinity (type
))
3562 min
= positive_overflow_infinity (type
);
3565 set_value_range_to_varying (vr
);
3569 if (is_overflow_infinity (vr0
.max
))
3570 max
= positive_overflow_infinity (type
);
3571 else if (!vrp_val_is_min (vr0
.max
))
3572 max
= fold_unary_to_constant (code
, type
, vr0
.max
);
3573 else if (!needs_overflow_infinity (type
))
3574 max
= TYPE_MAX_VALUE (type
);
3575 else if (supports_overflow_infinity (type
)
3576 /* We shouldn't generate [+INF, +INF] as set_value_range
3577 doesn't like this and ICEs. */
3578 && !is_positive_overflow_infinity (min
))
3579 max
= positive_overflow_infinity (type
);
3582 set_value_range_to_varying (vr
);
3586 cmp
= compare_values (min
, max
);
3588 /* If a VR_ANTI_RANGEs contains zero, then we have
3589 ~[-INF, min(MIN, MAX)]. */
3590 if (vr0
.type
== VR_ANTI_RANGE
)
3592 if (range_includes_zero_p (vr0
.min
, vr0
.max
) == 1)
3594 /* Take the lower of the two values. */
3598 /* Create ~[-INF, min (abs(MIN), abs(MAX))]
3599 or ~[-INF + 1, min (abs(MIN), abs(MAX))] when
3600 flag_wrapv is set and the original anti-range doesn't include
3601 TYPE_MIN_VALUE, remember -TYPE_MIN_VALUE = TYPE_MIN_VALUE. */
3602 if (TYPE_OVERFLOW_WRAPS (type
))
3604 tree type_min_value
= TYPE_MIN_VALUE (type
);
3606 min
= (vr0
.min
!= type_min_value
3607 ? int_const_binop (PLUS_EXPR
, type_min_value
,
3608 build_int_cst (TREE_TYPE (type_min_value
), 1))
3613 if (overflow_infinity_range_p (&vr0
))
3614 min
= negative_overflow_infinity (type
);
3616 min
= TYPE_MIN_VALUE (type
);
3621 /* All else has failed, so create the range [0, INF], even for
3622 flag_wrapv since TYPE_MIN_VALUE is in the original
3624 vr0
.type
= VR_RANGE
;
3625 min
= build_int_cst (type
, 0);
3626 if (needs_overflow_infinity (type
))
3628 if (supports_overflow_infinity (type
))
3629 max
= positive_overflow_infinity (type
);
3632 set_value_range_to_varying (vr
);
3637 max
= TYPE_MAX_VALUE (type
);
3641 /* If the range contains zero then we know that the minimum value in the
3642 range will be zero. */
3643 else if (range_includes_zero_p (vr0
.min
, vr0
.max
) == 1)
3647 min
= build_int_cst (type
, 0);
3651 /* If the range was reversed, swap MIN and MAX. */
3653 std::swap (min
, max
);
3656 cmp
= compare_values (min
, max
);
3657 if (cmp
== -2 || cmp
== 1)
3659 /* If the new range has its limits swapped around (MIN > MAX),
3660 then the operation caused one of them to wrap around, mark
3661 the new range VARYING. */
3662 set_value_range_to_varying (vr
);
3665 set_value_range (vr
, vr0
.type
, min
, max
, NULL
);
3669 /* For unhandled operations fall back to varying. */
3670 set_value_range_to_varying (vr
);
3675 /* Extract range information from a unary expression CODE OP0 based on
3676 the range of its operand with resulting type TYPE.
3677 The resulting range is stored in *VR. */
3680 extract_range_from_unary_expr (value_range_t
*vr
, enum tree_code code
,
3681 tree type
, tree op0
)
3683 value_range_t vr0
= VR_INITIALIZER
;
3685 /* Get value ranges for the operand. For constant operands, create
3686 a new value range with the operand to simplify processing. */
3687 if (TREE_CODE (op0
) == SSA_NAME
)
3688 vr0
= *(get_value_range (op0
));
3689 else if (is_gimple_min_invariant (op0
))
3690 set_value_range_to_value (&vr0
, op0
, NULL
);
3692 set_value_range_to_varying (&vr0
);
3694 extract_range_from_unary_expr_1 (vr
, code
, type
, &vr0
, TREE_TYPE (op0
));
3698 /* Extract range information from a conditional expression STMT based on
3699 the ranges of each of its operands and the expression code. */
3702 extract_range_from_cond_expr (value_range_t
*vr
, gassign
*stmt
)
3705 value_range_t vr0
= VR_INITIALIZER
;
3706 value_range_t vr1
= VR_INITIALIZER
;
3708 /* Get value ranges for each operand. For constant operands, create
3709 a new value range with the operand to simplify processing. */
3710 op0
= gimple_assign_rhs2 (stmt
);
3711 if (TREE_CODE (op0
) == SSA_NAME
)
3712 vr0
= *(get_value_range (op0
));
3713 else if (is_gimple_min_invariant (op0
))
3714 set_value_range_to_value (&vr0
, op0
, NULL
);
3716 set_value_range_to_varying (&vr0
);
3718 op1
= gimple_assign_rhs3 (stmt
);
3719 if (TREE_CODE (op1
) == SSA_NAME
)
3720 vr1
= *(get_value_range (op1
));
3721 else if (is_gimple_min_invariant (op1
))
3722 set_value_range_to_value (&vr1
, op1
, NULL
);
3724 set_value_range_to_varying (&vr1
);
3726 /* The resulting value range is the union of the operand ranges */
3727 copy_value_range (vr
, &vr0
);
3728 vrp_meet (vr
, &vr1
);
3732 /* Extract range information from a comparison expression EXPR based
3733 on the range of its operand and the expression code. */
3736 extract_range_from_comparison (value_range_t
*vr
, enum tree_code code
,
3737 tree type
, tree op0
, tree op1
)
3742 val
= vrp_evaluate_conditional_warnv_with_ops (code
, op0
, op1
, false, &sop
,
3745 /* A disadvantage of using a special infinity as an overflow
3746 representation is that we lose the ability to record overflow
3747 when we don't have an infinity. So we have to ignore a result
3748 which relies on overflow. */
3750 if (val
&& !is_overflow_infinity (val
) && !sop
)
3752 /* Since this expression was found on the RHS of an assignment,
3753 its type may be different from _Bool. Convert VAL to EXPR's
3755 val
= fold_convert (type
, val
);
3756 if (is_gimple_min_invariant (val
))
3757 set_value_range_to_value (vr
, val
, vr
->equiv
);
3759 set_value_range (vr
, VR_RANGE
, val
, val
, vr
->equiv
);
3762 /* The result of a comparison is always true or false. */
3763 set_value_range_to_truthvalue (vr
, type
);
3766 /* Helper function for simplify_internal_call_using_ranges and
3767 extract_range_basic. Return true if OP0 SUBCODE OP1 for
3768 SUBCODE {PLUS,MINUS,MULT}_EXPR is known to never overflow or
3769 always overflow. Set *OVF to true if it is known to always
3773 check_for_binary_op_overflow (enum tree_code subcode
, tree type
,
3774 tree op0
, tree op1
, bool *ovf
)
3776 value_range_t vr0
= VR_INITIALIZER
;
3777 value_range_t vr1
= VR_INITIALIZER
;
3778 if (TREE_CODE (op0
) == SSA_NAME
)
3779 vr0
= *get_value_range (op0
);
3780 else if (TREE_CODE (op0
) == INTEGER_CST
)
3781 set_value_range_to_value (&vr0
, op0
, NULL
);
3783 set_value_range_to_varying (&vr0
);
3785 if (TREE_CODE (op1
) == SSA_NAME
)
3786 vr1
= *get_value_range (op1
);
3787 else if (TREE_CODE (op1
) == INTEGER_CST
)
3788 set_value_range_to_value (&vr1
, op1
, NULL
);
3790 set_value_range_to_varying (&vr1
);
3792 if (!range_int_cst_p (&vr0
)
3793 || TREE_OVERFLOW (vr0
.min
)
3794 || TREE_OVERFLOW (vr0
.max
))
3796 vr0
.min
= vrp_val_min (TREE_TYPE (op0
));
3797 vr0
.max
= vrp_val_max (TREE_TYPE (op0
));
3799 if (!range_int_cst_p (&vr1
)
3800 || TREE_OVERFLOW (vr1
.min
)
3801 || TREE_OVERFLOW (vr1
.max
))
3803 vr1
.min
= vrp_val_min (TREE_TYPE (op1
));
3804 vr1
.max
= vrp_val_max (TREE_TYPE (op1
));
3806 *ovf
= arith_overflowed_p (subcode
, type
, vr0
.min
,
3807 subcode
== MINUS_EXPR
? vr1
.max
: vr1
.min
);
3808 if (arith_overflowed_p (subcode
, type
, vr0
.max
,
3809 subcode
== MINUS_EXPR
? vr1
.min
: vr1
.max
) != *ovf
)
3811 if (subcode
== MULT_EXPR
)
3813 if (arith_overflowed_p (subcode
, type
, vr0
.min
, vr1
.max
) != *ovf
3814 || arith_overflowed_p (subcode
, type
, vr0
.max
, vr1
.min
) != *ovf
)
3819 /* So far we found that there is an overflow on the boundaries.
3820 That doesn't prove that there is an overflow even for all values
3821 in between the boundaries. For that compute widest_int range
3822 of the result and see if it doesn't overlap the range of
3824 widest_int wmin
, wmax
;
3827 w
[0] = wi::to_widest (vr0
.min
);
3828 w
[1] = wi::to_widest (vr0
.max
);
3829 w
[2] = wi::to_widest (vr1
.min
);
3830 w
[3] = wi::to_widest (vr1
.max
);
3831 for (i
= 0; i
< 4; i
++)
3837 wt
= wi::add (w
[i
& 1], w
[2 + (i
& 2) / 2]);
3840 wt
= wi::sub (w
[i
& 1], w
[2 + (i
& 2) / 2]);
3843 wt
= wi::mul (w
[i
& 1], w
[2 + (i
& 2) / 2]);
3855 wmin
= wi::smin (wmin
, wt
);
3856 wmax
= wi::smax (wmax
, wt
);
3859 /* The result of op0 CODE op1 is known to be in range
3861 widest_int wtmin
= wi::to_widest (vrp_val_min (type
));
3862 widest_int wtmax
= wi::to_widest (vrp_val_max (type
));
3863 /* If all values in [wmin, wmax] are smaller than
3864 [wtmin, wtmax] or all are larger than [wtmin, wtmax],
3865 the arithmetic operation will always overflow. */
3866 if (wi::lts_p (wmax
, wtmin
) || wi::gts_p (wmin
, wtmax
))
3873 /* Try to derive a nonnegative or nonzero range out of STMT relying
3874 primarily on generic routines in fold in conjunction with range data.
3875 Store the result in *VR */
3878 extract_range_basic (value_range_t
*vr
, gimple stmt
)
3881 tree type
= gimple_expr_type (stmt
);
3883 if (gimple_call_builtin_p (stmt
, BUILT_IN_NORMAL
))
3885 tree fndecl
= gimple_call_fndecl (stmt
), arg
;
3886 int mini
, maxi
, zerov
= 0, prec
;
3888 switch (DECL_FUNCTION_CODE (fndecl
))
3890 case BUILT_IN_CONSTANT_P
:
3891 /* If the call is __builtin_constant_p and the argument is a
3892 function parameter resolve it to false. This avoids bogus
3893 array bound warnings.
3894 ??? We could do this as early as inlining is finished. */
3895 arg
= gimple_call_arg (stmt
, 0);
3896 if (TREE_CODE (arg
) == SSA_NAME
3897 && SSA_NAME_IS_DEFAULT_DEF (arg
)
3898 && TREE_CODE (SSA_NAME_VAR (arg
)) == PARM_DECL
)
3900 set_value_range_to_null (vr
, type
);
3904 /* Both __builtin_ffs* and __builtin_popcount return
3906 CASE_INT_FN (BUILT_IN_FFS
):
3907 CASE_INT_FN (BUILT_IN_POPCOUNT
):
3908 arg
= gimple_call_arg (stmt
, 0);
3909 prec
= TYPE_PRECISION (TREE_TYPE (arg
));
3912 if (TREE_CODE (arg
) == SSA_NAME
)
3914 value_range_t
*vr0
= get_value_range (arg
);
3915 /* If arg is non-zero, then ffs or popcount
3917 if (((vr0
->type
== VR_RANGE
3918 && range_includes_zero_p (vr0
->min
, vr0
->max
) == 0)
3919 || (vr0
->type
== VR_ANTI_RANGE
3920 && range_includes_zero_p (vr0
->min
, vr0
->max
) == 1))
3921 && !is_overflow_infinity (vr0
->min
)
3922 && !is_overflow_infinity (vr0
->max
))
3924 /* If some high bits are known to be zero,
3925 we can decrease the maximum. */
3926 if (vr0
->type
== VR_RANGE
3927 && TREE_CODE (vr0
->max
) == INTEGER_CST
3928 && !operand_less_p (vr0
->min
,
3929 build_zero_cst (TREE_TYPE (vr0
->min
)))
3930 && !is_overflow_infinity (vr0
->max
))
3931 maxi
= tree_floor_log2 (vr0
->max
) + 1;
3934 /* __builtin_parity* returns [0, 1]. */
3935 CASE_INT_FN (BUILT_IN_PARITY
):
3939 /* __builtin_c[lt]z* return [0, prec-1], except for
3940 when the argument is 0, but that is undefined behavior.
3941 On many targets where the CLZ RTL or optab value is defined
3942 for 0 the value is prec, so include that in the range
3944 CASE_INT_FN (BUILT_IN_CLZ
):
3945 arg
= gimple_call_arg (stmt
, 0);
3946 prec
= TYPE_PRECISION (TREE_TYPE (arg
));
3949 if (optab_handler (clz_optab
, TYPE_MODE (TREE_TYPE (arg
)))
3951 && CLZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (TREE_TYPE (arg
)),
3953 /* Handle only the single common value. */
3955 /* Magic value to give up, unless vr0 proves
3958 if (TREE_CODE (arg
) == SSA_NAME
)
3960 value_range_t
*vr0
= get_value_range (arg
);
3961 /* From clz of VR_RANGE minimum we can compute
3963 if (vr0
->type
== VR_RANGE
3964 && TREE_CODE (vr0
->min
) == INTEGER_CST
3965 && !is_overflow_infinity (vr0
->min
))
3967 maxi
= prec
- 1 - tree_floor_log2 (vr0
->min
);
3971 else if (vr0
->type
== VR_ANTI_RANGE
3972 && integer_zerop (vr0
->min
)
3973 && !is_overflow_infinity (vr0
->min
))
3980 /* From clz of VR_RANGE maximum we can compute
3982 if (vr0
->type
== VR_RANGE
3983 && TREE_CODE (vr0
->max
) == INTEGER_CST
3984 && !is_overflow_infinity (vr0
->max
))
3986 mini
= prec
- 1 - tree_floor_log2 (vr0
->max
);
3994 /* __builtin_ctz* return [0, prec-1], except for
3995 when the argument is 0, but that is undefined behavior.
3996 If there is a ctz optab for this mode and
3997 CTZ_DEFINED_VALUE_AT_ZERO, include that in the range,
3998 otherwise just assume 0 won't be seen. */
3999 CASE_INT_FN (BUILT_IN_CTZ
):
4000 arg
= gimple_call_arg (stmt
, 0);
4001 prec
= TYPE_PRECISION (TREE_TYPE (arg
));
4004 if (optab_handler (ctz_optab
, TYPE_MODE (TREE_TYPE (arg
)))
4006 && CTZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (TREE_TYPE (arg
)),
4009 /* Handle only the two common values. */
4012 else if (zerov
== prec
)
4015 /* Magic value to give up, unless vr0 proves
4019 if (TREE_CODE (arg
) == SSA_NAME
)
4021 value_range_t
*vr0
= get_value_range (arg
);
4022 /* If arg is non-zero, then use [0, prec - 1]. */
4023 if (((vr0
->type
== VR_RANGE
4024 && integer_nonzerop (vr0
->min
))
4025 || (vr0
->type
== VR_ANTI_RANGE
4026 && integer_zerop (vr0
->min
)))
4027 && !is_overflow_infinity (vr0
->min
))
4032 /* If some high bits are known to be zero,
4033 we can decrease the result maximum. */
4034 if (vr0
->type
== VR_RANGE
4035 && TREE_CODE (vr0
->max
) == INTEGER_CST
4036 && !is_overflow_infinity (vr0
->max
))
4038 maxi
= tree_floor_log2 (vr0
->max
);
4039 /* For vr0 [0, 0] give up. */
4047 /* __builtin_clrsb* returns [0, prec-1]. */
4048 CASE_INT_FN (BUILT_IN_CLRSB
):
4049 arg
= gimple_call_arg (stmt
, 0);
4050 prec
= TYPE_PRECISION (TREE_TYPE (arg
));
4055 set_value_range (vr
, VR_RANGE
, build_int_cst (type
, mini
),
4056 build_int_cst (type
, maxi
), NULL
);
4062 else if (is_gimple_call (stmt
) && gimple_call_internal_p (stmt
))
4064 enum tree_code subcode
= ERROR_MARK
;
4065 switch (gimple_call_internal_fn (stmt
))
4067 case IFN_UBSAN_CHECK_ADD
:
4068 subcode
= PLUS_EXPR
;
4070 case IFN_UBSAN_CHECK_SUB
:
4071 subcode
= MINUS_EXPR
;
4073 case IFN_UBSAN_CHECK_MUL
:
4074 subcode
= MULT_EXPR
;
4079 if (subcode
!= ERROR_MARK
)
4081 bool saved_flag_wrapv
= flag_wrapv
;
4082 /* Pretend the arithmetics is wrapping. If there is
4083 any overflow, we'll complain, but will actually do
4084 wrapping operation. */
4086 extract_range_from_binary_expr (vr
, subcode
, type
,
4087 gimple_call_arg (stmt
, 0),
4088 gimple_call_arg (stmt
, 1));
4089 flag_wrapv
= saved_flag_wrapv
;
4091 /* If for both arguments vrp_valueize returned non-NULL,
4092 this should have been already folded and if not, it
4093 wasn't folded because of overflow. Avoid removing the
4094 UBSAN_CHECK_* calls in that case. */
4095 if (vr
->type
== VR_RANGE
4096 && (vr
->min
== vr
->max
4097 || operand_equal_p (vr
->min
, vr
->max
, 0)))
4098 set_value_range_to_varying (vr
);
4102 /* Handle extraction of the two results (result of arithmetics and
4103 a flag whether arithmetics overflowed) from {ADD,SUB,MUL}_OVERFLOW
4104 internal function. */
4105 else if (is_gimple_assign (stmt
)
4106 && (gimple_assign_rhs_code (stmt
) == REALPART_EXPR
4107 || gimple_assign_rhs_code (stmt
) == IMAGPART_EXPR
)
4108 && INTEGRAL_TYPE_P (type
))
4110 enum tree_code code
= gimple_assign_rhs_code (stmt
);
4111 tree op
= gimple_assign_rhs1 (stmt
);
4112 if (TREE_CODE (op
) == code
&& TREE_CODE (TREE_OPERAND (op
, 0)) == SSA_NAME
)
4114 gimple g
= SSA_NAME_DEF_STMT (TREE_OPERAND (op
, 0));
4115 if (is_gimple_call (g
) && gimple_call_internal_p (g
))
4117 enum tree_code subcode
= ERROR_MARK
;
4118 switch (gimple_call_internal_fn (g
))
4120 case IFN_ADD_OVERFLOW
:
4121 subcode
= PLUS_EXPR
;
4123 case IFN_SUB_OVERFLOW
:
4124 subcode
= MINUS_EXPR
;
4126 case IFN_MUL_OVERFLOW
:
4127 subcode
= MULT_EXPR
;
4132 if (subcode
!= ERROR_MARK
)
4134 tree op0
= gimple_call_arg (g
, 0);
4135 tree op1
= gimple_call_arg (g
, 1);
4136 if (code
== IMAGPART_EXPR
)
4139 if (check_for_binary_op_overflow (subcode
, type
,
4141 set_value_range_to_value (vr
,
4142 build_int_cst (type
, ovf
),
4145 set_value_range (vr
, VR_RANGE
, build_int_cst (type
, 0),
4146 build_int_cst (type
, 1), NULL
);
4148 else if (types_compatible_p (type
, TREE_TYPE (op0
))
4149 && types_compatible_p (type
, TREE_TYPE (op1
)))
4151 bool saved_flag_wrapv
= flag_wrapv
;
4152 /* Pretend the arithmetics is wrapping. If there is
4153 any overflow, IMAGPART_EXPR will be set. */
4155 extract_range_from_binary_expr (vr
, subcode
, type
,
4157 flag_wrapv
= saved_flag_wrapv
;
4161 value_range_t vr0
= VR_INITIALIZER
;
4162 value_range_t vr1
= VR_INITIALIZER
;
4163 bool saved_flag_wrapv
= flag_wrapv
;
4164 /* Pretend the arithmetics is wrapping. If there is
4165 any overflow, IMAGPART_EXPR will be set. */
4167 extract_range_from_unary_expr (&vr0
, NOP_EXPR
,
4169 extract_range_from_unary_expr (&vr1
, NOP_EXPR
,
4171 extract_range_from_binary_expr_1 (vr
, subcode
, type
,
4173 flag_wrapv
= saved_flag_wrapv
;
4180 if (INTEGRAL_TYPE_P (type
)
4181 && gimple_stmt_nonnegative_warnv_p (stmt
, &sop
))
4182 set_value_range_to_nonnegative (vr
, type
,
4183 sop
|| stmt_overflow_infinity (stmt
));
4184 else if (vrp_stmt_computes_nonzero (stmt
, &sop
)
4186 set_value_range_to_nonnull (vr
, type
);
4188 set_value_range_to_varying (vr
);
4192 /* Try to compute a useful range out of assignment STMT and store it
4196 extract_range_from_assignment (value_range_t
*vr
, gassign
*stmt
)
4198 enum tree_code code
= gimple_assign_rhs_code (stmt
);
4200 if (code
== ASSERT_EXPR
)
4201 extract_range_from_assert (vr
, gimple_assign_rhs1 (stmt
));
4202 else if (code
== SSA_NAME
)
4203 extract_range_from_ssa_name (vr
, gimple_assign_rhs1 (stmt
));
4204 else if (TREE_CODE_CLASS (code
) == tcc_binary
)
4205 extract_range_from_binary_expr (vr
, gimple_assign_rhs_code (stmt
),
4206 gimple_expr_type (stmt
),
4207 gimple_assign_rhs1 (stmt
),
4208 gimple_assign_rhs2 (stmt
));
4209 else if (TREE_CODE_CLASS (code
) == tcc_unary
)
4210 extract_range_from_unary_expr (vr
, gimple_assign_rhs_code (stmt
),
4211 gimple_expr_type (stmt
),
4212 gimple_assign_rhs1 (stmt
));
4213 else if (code
== COND_EXPR
)
4214 extract_range_from_cond_expr (vr
, stmt
);
4215 else if (TREE_CODE_CLASS (code
) == tcc_comparison
)
4216 extract_range_from_comparison (vr
, gimple_assign_rhs_code (stmt
),
4217 gimple_expr_type (stmt
),
4218 gimple_assign_rhs1 (stmt
),
4219 gimple_assign_rhs2 (stmt
));
4220 else if (get_gimple_rhs_class (code
) == GIMPLE_SINGLE_RHS
4221 && is_gimple_min_invariant (gimple_assign_rhs1 (stmt
)))
4222 set_value_range_to_value (vr
, gimple_assign_rhs1 (stmt
), NULL
);
4224 set_value_range_to_varying (vr
);
4226 if (vr
->type
== VR_VARYING
)
4227 extract_range_basic (vr
, stmt
);
4230 /* Given a range VR, a LOOP and a variable VAR, determine whether it
4231 would be profitable to adjust VR using scalar evolution information
4232 for VAR. If so, update VR with the new limits. */
4235 adjust_range_with_scev (value_range_t
*vr
, struct loop
*loop
,
4236 gimple stmt
, tree var
)
4238 tree init
, step
, chrec
, tmin
, tmax
, min
, max
, type
, tem
;
4239 enum ev_direction dir
;
4241 /* TODO. Don't adjust anti-ranges. An anti-range may provide
4242 better opportunities than a regular range, but I'm not sure. */
4243 if (vr
->type
== VR_ANTI_RANGE
)
4246 chrec
= instantiate_parameters (loop
, analyze_scalar_evolution (loop
, var
));
4248 /* Like in PR19590, scev can return a constant function. */
4249 if (is_gimple_min_invariant (chrec
))
4251 set_value_range_to_value (vr
, chrec
, vr
->equiv
);
4255 if (TREE_CODE (chrec
) != POLYNOMIAL_CHREC
)
4258 init
= initial_condition_in_loop_num (chrec
, loop
->num
);
4259 tem
= op_with_constant_singleton_value_range (init
);
4262 step
= evolution_part_in_loop_num (chrec
, loop
->num
);
4263 tem
= op_with_constant_singleton_value_range (step
);
4267 /* If STEP is symbolic, we can't know whether INIT will be the
4268 minimum or maximum value in the range. Also, unless INIT is
4269 a simple expression, compare_values and possibly other functions
4270 in tree-vrp won't be able to handle it. */
4271 if (step
== NULL_TREE
4272 || !is_gimple_min_invariant (step
)
4273 || !valid_value_p (init
))
4276 dir
= scev_direction (chrec
);
4277 if (/* Do not adjust ranges if we do not know whether the iv increases
4278 or decreases, ... */
4279 dir
== EV_DIR_UNKNOWN
4280 /* ... or if it may wrap. */
4281 || scev_probably_wraps_p (init
, step
, stmt
, get_chrec_loop (chrec
),
4285 /* We use TYPE_MIN_VALUE and TYPE_MAX_VALUE here instead of
4286 negative_overflow_infinity and positive_overflow_infinity,
4287 because we have concluded that the loop probably does not
4290 type
= TREE_TYPE (var
);
4291 if (POINTER_TYPE_P (type
) || !TYPE_MIN_VALUE (type
))
4292 tmin
= lower_bound_in_type (type
, type
);
4294 tmin
= TYPE_MIN_VALUE (type
);
4295 if (POINTER_TYPE_P (type
) || !TYPE_MAX_VALUE (type
))
4296 tmax
= upper_bound_in_type (type
, type
);
4298 tmax
= TYPE_MAX_VALUE (type
);
4300 /* Try to use estimated number of iterations for the loop to constrain the
4301 final value in the evolution. */
4302 if (TREE_CODE (step
) == INTEGER_CST
4303 && is_gimple_val (init
)
4304 && (TREE_CODE (init
) != SSA_NAME
4305 || get_value_range (init
)->type
== VR_RANGE
))
4309 /* We are only entering here for loop header PHI nodes, so using
4310 the number of latch executions is the correct thing to use. */
4311 if (max_loop_iterations (loop
, &nit
))
4313 value_range_t maxvr
= VR_INITIALIZER
;
4314 signop sgn
= TYPE_SIGN (TREE_TYPE (step
));
4317 widest_int wtmp
= wi::mul (wi::to_widest (step
), nit
, sgn
,
4319 /* If the multiplication overflowed we can't do a meaningful
4320 adjustment. Likewise if the result doesn't fit in the type
4321 of the induction variable. For a signed type we have to
4322 check whether the result has the expected signedness which
4323 is that of the step as number of iterations is unsigned. */
4325 && wi::fits_to_tree_p (wtmp
, TREE_TYPE (init
))
4327 || wi::gts_p (wtmp
, 0) == wi::gts_p (step
, 0)))
4329 tem
= wide_int_to_tree (TREE_TYPE (init
), wtmp
);
4330 extract_range_from_binary_expr (&maxvr
, PLUS_EXPR
,
4331 TREE_TYPE (init
), init
, tem
);
4332 /* Likewise if the addition did. */
4333 if (maxvr
.type
== VR_RANGE
)
4342 if (vr
->type
== VR_VARYING
|| vr
->type
== VR_UNDEFINED
)
4347 /* For VARYING or UNDEFINED ranges, just about anything we get
4348 from scalar evolutions should be better. */
4350 if (dir
== EV_DIR_DECREASES
)
4355 else if (vr
->type
== VR_RANGE
)
4360 if (dir
== EV_DIR_DECREASES
)
4362 /* INIT is the maximum value. If INIT is lower than VR->MAX
4363 but no smaller than VR->MIN, set VR->MAX to INIT. */
4364 if (compare_values (init
, max
) == -1)
4367 /* According to the loop information, the variable does not
4368 overflow. If we think it does, probably because of an
4369 overflow due to arithmetic on a different INF value,
4371 if (is_negative_overflow_infinity (min
)
4372 || compare_values (min
, tmin
) == -1)
4378 /* If INIT is bigger than VR->MIN, set VR->MIN to INIT. */
4379 if (compare_values (init
, min
) == 1)
4382 if (is_positive_overflow_infinity (max
)
4383 || compare_values (tmax
, max
) == -1)
4390 /* If we just created an invalid range with the minimum
4391 greater than the maximum, we fail conservatively.
4392 This should happen only in unreachable
4393 parts of code, or for invalid programs. */
4394 if (compare_values (min
, max
) == 1
4395 || (is_negative_overflow_infinity (min
)
4396 && is_positive_overflow_infinity (max
)))
4399 set_value_range (vr
, VR_RANGE
, min
, max
, vr
->equiv
);
4403 /* Given two numeric value ranges VR0, VR1 and a comparison code COMP:
4405 - Return BOOLEAN_TRUE_NODE if VR0 COMP VR1 always returns true for
4406 all the values in the ranges.
4408 - Return BOOLEAN_FALSE_NODE if the comparison always returns false.
4410 - Return NULL_TREE if it is not always possible to determine the
4411 value of the comparison.
4413 Also set *STRICT_OVERFLOW_P to indicate whether a range with an
4414 overflow infinity was used in the test. */
4418 compare_ranges (enum tree_code comp
, value_range_t
*vr0
, value_range_t
*vr1
,
4419 bool *strict_overflow_p
)
4421 /* VARYING or UNDEFINED ranges cannot be compared. */
4422 if (vr0
->type
== VR_VARYING
4423 || vr0
->type
== VR_UNDEFINED
4424 || vr1
->type
== VR_VARYING
4425 || vr1
->type
== VR_UNDEFINED
)
4428 /* Anti-ranges need to be handled separately. */
4429 if (vr0
->type
== VR_ANTI_RANGE
|| vr1
->type
== VR_ANTI_RANGE
)
4431 /* If both are anti-ranges, then we cannot compute any
4433 if (vr0
->type
== VR_ANTI_RANGE
&& vr1
->type
== VR_ANTI_RANGE
)
4436 /* These comparisons are never statically computable. */
4443 /* Equality can be computed only between a range and an
4444 anti-range. ~[VAL1, VAL2] == [VAL1, VAL2] is always false. */
4445 if (vr0
->type
== VR_RANGE
)
4447 /* To simplify processing, make VR0 the anti-range. */
4448 value_range_t
*tmp
= vr0
;
4453 gcc_assert (comp
== NE_EXPR
|| comp
== EQ_EXPR
);
4455 if (compare_values_warnv (vr0
->min
, vr1
->min
, strict_overflow_p
) == 0
4456 && compare_values_warnv (vr0
->max
, vr1
->max
, strict_overflow_p
) == 0)
4457 return (comp
== NE_EXPR
) ? boolean_true_node
: boolean_false_node
;
4462 if (!usable_range_p (vr0
, strict_overflow_p
)
4463 || !usable_range_p (vr1
, strict_overflow_p
))
4466 /* Simplify processing. If COMP is GT_EXPR or GE_EXPR, switch the
4467 operands around and change the comparison code. */
4468 if (comp
== GT_EXPR
|| comp
== GE_EXPR
)
4470 comp
= (comp
== GT_EXPR
) ? LT_EXPR
: LE_EXPR
;
4471 std::swap (vr0
, vr1
);
4474 if (comp
== EQ_EXPR
)
4476 /* Equality may only be computed if both ranges represent
4477 exactly one value. */
4478 if (compare_values_warnv (vr0
->min
, vr0
->max
, strict_overflow_p
) == 0
4479 && compare_values_warnv (vr1
->min
, vr1
->max
, strict_overflow_p
) == 0)
4481 int cmp_min
= compare_values_warnv (vr0
->min
, vr1
->min
,
4483 int cmp_max
= compare_values_warnv (vr0
->max
, vr1
->max
,
4485 if (cmp_min
== 0 && cmp_max
== 0)
4486 return boolean_true_node
;
4487 else if (cmp_min
!= -2 && cmp_max
!= -2)
4488 return boolean_false_node
;
4490 /* If [V0_MIN, V1_MAX] < [V1_MIN, V1_MAX] then V0 != V1. */
4491 else if (compare_values_warnv (vr0
->min
, vr1
->max
,
4492 strict_overflow_p
) == 1
4493 || compare_values_warnv (vr1
->min
, vr0
->max
,
4494 strict_overflow_p
) == 1)
4495 return boolean_false_node
;
4499 else if (comp
== NE_EXPR
)
4503 /* If VR0 is completely to the left or completely to the right
4504 of VR1, they are always different. Notice that we need to
4505 make sure that both comparisons yield similar results to
4506 avoid comparing values that cannot be compared at
4508 cmp1
= compare_values_warnv (vr0
->max
, vr1
->min
, strict_overflow_p
);
4509 cmp2
= compare_values_warnv (vr0
->min
, vr1
->max
, strict_overflow_p
);
4510 if ((cmp1
== -1 && cmp2
== -1) || (cmp1
== 1 && cmp2
== 1))
4511 return boolean_true_node
;
4513 /* If VR0 and VR1 represent a single value and are identical,
4515 else if (compare_values_warnv (vr0
->min
, vr0
->max
,
4516 strict_overflow_p
) == 0
4517 && compare_values_warnv (vr1
->min
, vr1
->max
,
4518 strict_overflow_p
) == 0
4519 && compare_values_warnv (vr0
->min
, vr1
->min
,
4520 strict_overflow_p
) == 0
4521 && compare_values_warnv (vr0
->max
, vr1
->max
,
4522 strict_overflow_p
) == 0)
4523 return boolean_false_node
;
4525 /* Otherwise, they may or may not be different. */
4529 else if (comp
== LT_EXPR
|| comp
== LE_EXPR
)
4533 /* If VR0 is to the left of VR1, return true. */
4534 tst
= compare_values_warnv (vr0
->max
, vr1
->min
, strict_overflow_p
);
4535 if ((comp
== LT_EXPR
&& tst
== -1)
4536 || (comp
== LE_EXPR
&& (tst
== -1 || tst
== 0)))
4538 if (overflow_infinity_range_p (vr0
)
4539 || overflow_infinity_range_p (vr1
))
4540 *strict_overflow_p
= true;
4541 return boolean_true_node
;
4544 /* If VR0 is to the right of VR1, return false. */
4545 tst
= compare_values_warnv (vr0
->min
, vr1
->max
, strict_overflow_p
);
4546 if ((comp
== LT_EXPR
&& (tst
== 0 || tst
== 1))
4547 || (comp
== LE_EXPR
&& tst
== 1))
4549 if (overflow_infinity_range_p (vr0
)
4550 || overflow_infinity_range_p (vr1
))
4551 *strict_overflow_p
= true;
4552 return boolean_false_node
;
4555 /* Otherwise, we don't know. */
4563 /* Given a value range VR, a value VAL and a comparison code COMP, return
4564 BOOLEAN_TRUE_NODE if VR COMP VAL always returns true for all the
4565 values in VR. Return BOOLEAN_FALSE_NODE if the comparison
4566 always returns false. Return NULL_TREE if it is not always
4567 possible to determine the value of the comparison. Also set
4568 *STRICT_OVERFLOW_P to indicate whether a range with an overflow
4569 infinity was used in the test. */
4572 compare_range_with_value (enum tree_code comp
, value_range_t
*vr
, tree val
,
4573 bool *strict_overflow_p
)
4575 if (vr
->type
== VR_VARYING
|| vr
->type
== VR_UNDEFINED
)
4578 /* Anti-ranges need to be handled separately. */
4579 if (vr
->type
== VR_ANTI_RANGE
)
4581 /* For anti-ranges, the only predicates that we can compute at
4582 compile time are equality and inequality. */
4589 /* ~[VAL_1, VAL_2] OP VAL is known if VAL_1 <= VAL <= VAL_2. */
4590 if (value_inside_range (val
, vr
->min
, vr
->max
) == 1)
4591 return (comp
== NE_EXPR
) ? boolean_true_node
: boolean_false_node
;
4596 if (!usable_range_p (vr
, strict_overflow_p
))
4599 if (comp
== EQ_EXPR
)
4601 /* EQ_EXPR may only be computed if VR represents exactly
4603 if (compare_values_warnv (vr
->min
, vr
->max
, strict_overflow_p
) == 0)
4605 int cmp
= compare_values_warnv (vr
->min
, val
, strict_overflow_p
);
4607 return boolean_true_node
;
4608 else if (cmp
== -1 || cmp
== 1 || cmp
== 2)
4609 return boolean_false_node
;
4611 else if (compare_values_warnv (val
, vr
->min
, strict_overflow_p
) == -1
4612 || compare_values_warnv (vr
->max
, val
, strict_overflow_p
) == -1)
4613 return boolean_false_node
;
4617 else if (comp
== NE_EXPR
)
4619 /* If VAL is not inside VR, then they are always different. */
4620 if (compare_values_warnv (vr
->max
, val
, strict_overflow_p
) == -1
4621 || compare_values_warnv (vr
->min
, val
, strict_overflow_p
) == 1)
4622 return boolean_true_node
;
4624 /* If VR represents exactly one value equal to VAL, then return
4626 if (compare_values_warnv (vr
->min
, vr
->max
, strict_overflow_p
) == 0
4627 && compare_values_warnv (vr
->min
, val
, strict_overflow_p
) == 0)
4628 return boolean_false_node
;
4630 /* Otherwise, they may or may not be different. */
4633 else if (comp
== LT_EXPR
|| comp
== LE_EXPR
)
4637 /* If VR is to the left of VAL, return true. */
4638 tst
= compare_values_warnv (vr
->max
, val
, strict_overflow_p
);
4639 if ((comp
== LT_EXPR
&& tst
== -1)
4640 || (comp
== LE_EXPR
&& (tst
== -1 || tst
== 0)))
4642 if (overflow_infinity_range_p (vr
))
4643 *strict_overflow_p
= true;
4644 return boolean_true_node
;
4647 /* If VR is to the right of VAL, return false. */
4648 tst
= compare_values_warnv (vr
->min
, val
, strict_overflow_p
);
4649 if ((comp
== LT_EXPR
&& (tst
== 0 || tst
== 1))
4650 || (comp
== LE_EXPR
&& tst
== 1))
4652 if (overflow_infinity_range_p (vr
))
4653 *strict_overflow_p
= true;
4654 return boolean_false_node
;
4657 /* Otherwise, we don't know. */
4660 else if (comp
== GT_EXPR
|| comp
== GE_EXPR
)
4664 /* If VR is to the right of VAL, return true. */
4665 tst
= compare_values_warnv (vr
->min
, val
, strict_overflow_p
);
4666 if ((comp
== GT_EXPR
&& tst
== 1)
4667 || (comp
== GE_EXPR
&& (tst
== 0 || tst
== 1)))
4669 if (overflow_infinity_range_p (vr
))
4670 *strict_overflow_p
= true;
4671 return boolean_true_node
;
4674 /* If VR is to the left of VAL, return false. */
4675 tst
= compare_values_warnv (vr
->max
, val
, strict_overflow_p
);
4676 if ((comp
== GT_EXPR
&& (tst
== -1 || tst
== 0))
4677 || (comp
== GE_EXPR
&& tst
== -1))
4679 if (overflow_infinity_range_p (vr
))
4680 *strict_overflow_p
= true;
4681 return boolean_false_node
;
4684 /* Otherwise, we don't know. */
4692 /* Debugging dumps. */
4694 void dump_value_range (FILE *, value_range_t
*);
4695 void debug_value_range (value_range_t
*);
4696 void dump_all_value_ranges (FILE *);
4697 void debug_all_value_ranges (void);
4698 void dump_vr_equiv (FILE *, bitmap
);
4699 void debug_vr_equiv (bitmap
);
4702 /* Dump value range VR to FILE. */
4705 dump_value_range (FILE *file
, value_range_t
*vr
)
4708 fprintf (file
, "[]");
4709 else if (vr
->type
== VR_UNDEFINED
)
4710 fprintf (file
, "UNDEFINED");
4711 else if (vr
->type
== VR_RANGE
|| vr
->type
== VR_ANTI_RANGE
)
4713 tree type
= TREE_TYPE (vr
->min
);
4715 fprintf (file
, "%s[", (vr
->type
== VR_ANTI_RANGE
) ? "~" : "");
4717 if (is_negative_overflow_infinity (vr
->min
))
4718 fprintf (file
, "-INF(OVF)");
4719 else if (INTEGRAL_TYPE_P (type
)
4720 && !TYPE_UNSIGNED (type
)
4721 && vrp_val_is_min (vr
->min
))
4722 fprintf (file
, "-INF");
4724 print_generic_expr (file
, vr
->min
, 0);
4726 fprintf (file
, ", ");
4728 if (is_positive_overflow_infinity (vr
->max
))
4729 fprintf (file
, "+INF(OVF)");
4730 else if (INTEGRAL_TYPE_P (type
)
4731 && vrp_val_is_max (vr
->max
))
4732 fprintf (file
, "+INF");
4734 print_generic_expr (file
, vr
->max
, 0);
4736 fprintf (file
, "]");
4743 fprintf (file
, " EQUIVALENCES: { ");
4745 EXECUTE_IF_SET_IN_BITMAP (vr
->equiv
, 0, i
, bi
)
4747 print_generic_expr (file
, ssa_name (i
), 0);
4748 fprintf (file
, " ");
4752 fprintf (file
, "} (%u elements)", c
);
4755 else if (vr
->type
== VR_VARYING
)
4756 fprintf (file
, "VARYING");
4758 fprintf (file
, "INVALID RANGE");
4762 /* Dump value range VR to stderr. */
4765 debug_value_range (value_range_t
*vr
)
4767 dump_value_range (stderr
, vr
);
4768 fprintf (stderr
, "\n");
4772 /* Dump value ranges of all SSA_NAMEs to FILE. */
4775 dump_all_value_ranges (FILE *file
)
4779 for (i
= 0; i
< num_vr_values
; i
++)
4783 print_generic_expr (file
, ssa_name (i
), 0);
4784 fprintf (file
, ": ");
4785 dump_value_range (file
, vr_value
[i
]);
4786 fprintf (file
, "\n");
4790 fprintf (file
, "\n");
4794 /* Dump all value ranges to stderr. */
4797 debug_all_value_ranges (void)
4799 dump_all_value_ranges (stderr
);
4803 /* Given a COND_EXPR COND of the form 'V OP W', and an SSA name V,
4804 create a new SSA name N and return the assertion assignment
4805 'N = ASSERT_EXPR <V, V OP W>'. */
4808 build_assert_expr_for (tree cond
, tree v
)
4813 gcc_assert (TREE_CODE (v
) == SSA_NAME
4814 && COMPARISON_CLASS_P (cond
));
4816 a
= build2 (ASSERT_EXPR
, TREE_TYPE (v
), v
, cond
);
4817 assertion
= gimple_build_assign (NULL_TREE
, a
);
4819 /* The new ASSERT_EXPR, creates a new SSA name that replaces the
4820 operand of the ASSERT_EXPR. Create it so the new name and the old one
4821 are registered in the replacement table so that we can fix the SSA web
4822 after adding all the ASSERT_EXPRs. */
4823 create_new_def_for (v
, assertion
, NULL
);
4829 /* Return false if EXPR is a predicate expression involving floating
4833 fp_predicate (gimple stmt
)
4835 GIMPLE_CHECK (stmt
, GIMPLE_COND
);
4837 return FLOAT_TYPE_P (TREE_TYPE (gimple_cond_lhs (stmt
)));
4840 /* If the range of values taken by OP can be inferred after STMT executes,
4841 return the comparison code (COMP_CODE_P) and value (VAL_P) that
4842 describes the inferred range. Return true if a range could be
4846 infer_value_range (gimple stmt
, tree op
, enum tree_code
*comp_code_p
, tree
*val_p
)
4849 *comp_code_p
= ERROR_MARK
;
4851 /* Do not attempt to infer anything in names that flow through
4853 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op
))
4856 /* Similarly, don't infer anything from statements that may throw
4857 exceptions. ??? Relax this requirement? */
4858 if (stmt_could_throw_p (stmt
))
4861 /* If STMT is the last statement of a basic block with no normal
4862 successors, there is no point inferring anything about any of its
4863 operands. We would not be able to find a proper insertion point
4864 for the assertion, anyway. */
4865 if (stmt_ends_bb_p (stmt
))
4870 FOR_EACH_EDGE (e
, ei
, gimple_bb (stmt
)->succs
)
4871 if (!(e
->flags
& EDGE_ABNORMAL
))
4877 if (infer_nonnull_range (stmt
, op
))
4879 *val_p
= build_int_cst (TREE_TYPE (op
), 0);
4880 *comp_code_p
= NE_EXPR
;
4888 void dump_asserts_for (FILE *, tree
);
4889 void debug_asserts_for (tree
);
4890 void dump_all_asserts (FILE *);
4891 void debug_all_asserts (void);
4893 /* Dump all the registered assertions for NAME to FILE. */
4896 dump_asserts_for (FILE *file
, tree name
)
4900 fprintf (file
, "Assertions to be inserted for ");
4901 print_generic_expr (file
, name
, 0);
4902 fprintf (file
, "\n");
4904 loc
= asserts_for
[SSA_NAME_VERSION (name
)];
4907 fprintf (file
, "\t");
4908 print_gimple_stmt (file
, gsi_stmt (loc
->si
), 0, 0);
4909 fprintf (file
, "\n\tBB #%d", loc
->bb
->index
);
4912 fprintf (file
, "\n\tEDGE %d->%d", loc
->e
->src
->index
,
4913 loc
->e
->dest
->index
);
4914 dump_edge_info (file
, loc
->e
, dump_flags
, 0);
4916 fprintf (file
, "\n\tPREDICATE: ");
4917 print_generic_expr (file
, name
, 0);
4918 fprintf (file
, " %s ", get_tree_code_name (loc
->comp_code
));
4919 print_generic_expr (file
, loc
->val
, 0);
4920 fprintf (file
, "\n\n");
4924 fprintf (file
, "\n");
4928 /* Dump all the registered assertions for NAME to stderr. */
4931 debug_asserts_for (tree name
)
4933 dump_asserts_for (stderr
, name
);
4937 /* Dump all the registered assertions for all the names to FILE. */
4940 dump_all_asserts (FILE *file
)
4945 fprintf (file
, "\nASSERT_EXPRs to be inserted\n\n");
4946 EXECUTE_IF_SET_IN_BITMAP (need_assert_for
, 0, i
, bi
)
4947 dump_asserts_for (file
, ssa_name (i
));
4948 fprintf (file
, "\n");
4952 /* Dump all the registered assertions for all the names to stderr. */
4955 debug_all_asserts (void)
4957 dump_all_asserts (stderr
);
4961 /* If NAME doesn't have an ASSERT_EXPR registered for asserting
4962 'EXPR COMP_CODE VAL' at a location that dominates block BB or
4963 E->DEST, then register this location as a possible insertion point
4964 for ASSERT_EXPR <NAME, EXPR COMP_CODE VAL>.
4966 BB, E and SI provide the exact insertion point for the new
4967 ASSERT_EXPR. If BB is NULL, then the ASSERT_EXPR is to be inserted
4968 on edge E. Otherwise, if E is NULL, the ASSERT_EXPR is inserted on
4969 BB. If SI points to a COND_EXPR or a SWITCH_EXPR statement, then E
4970 must not be NULL. */
4973 register_new_assert_for (tree name
, tree expr
,
4974 enum tree_code comp_code
,
4978 gimple_stmt_iterator si
)
4980 assert_locus
*n
, *loc
, *last_loc
;
4981 basic_block dest_bb
;
4983 gcc_checking_assert (bb
== NULL
|| e
== NULL
);
4986 gcc_checking_assert (gimple_code (gsi_stmt (si
)) != GIMPLE_COND
4987 && gimple_code (gsi_stmt (si
)) != GIMPLE_SWITCH
);
4989 /* Never build an assert comparing against an integer constant with
4990 TREE_OVERFLOW set. This confuses our undefined overflow warning
4992 if (TREE_OVERFLOW_P (val
))
4993 val
= drop_tree_overflow (val
);
4995 /* The new assertion A will be inserted at BB or E. We need to
4996 determine if the new location is dominated by a previously
4997 registered location for A. If we are doing an edge insertion,
4998 assume that A will be inserted at E->DEST. Note that this is not
5001 If E is a critical edge, it will be split. But even if E is
5002 split, the new block will dominate the same set of blocks that
5005 The reverse, however, is not true, blocks dominated by E->DEST
5006 will not be dominated by the new block created to split E. So,
5007 if the insertion location is on a critical edge, we will not use
5008 the new location to move another assertion previously registered
5009 at a block dominated by E->DEST. */
5010 dest_bb
= (bb
) ? bb
: e
->dest
;
5012 /* If NAME already has an ASSERT_EXPR registered for COMP_CODE and
5013 VAL at a block dominating DEST_BB, then we don't need to insert a new
5014 one. Similarly, if the same assertion already exists at a block
5015 dominated by DEST_BB and the new location is not on a critical
5016 edge, then update the existing location for the assertion (i.e.,
5017 move the assertion up in the dominance tree).
5019 Note, this is implemented as a simple linked list because there
5020 should not be more than a handful of assertions registered per
5021 name. If this becomes a performance problem, a table hashed by
5022 COMP_CODE and VAL could be implemented. */
5023 loc
= asserts_for
[SSA_NAME_VERSION (name
)];
5027 if (loc
->comp_code
== comp_code
5029 || operand_equal_p (loc
->val
, val
, 0))
5030 && (loc
->expr
== expr
5031 || operand_equal_p (loc
->expr
, expr
, 0)))
5033 /* If E is not a critical edge and DEST_BB
5034 dominates the existing location for the assertion, move
5035 the assertion up in the dominance tree by updating its
5036 location information. */
5037 if ((e
== NULL
|| !EDGE_CRITICAL_P (e
))
5038 && dominated_by_p (CDI_DOMINATORS
, loc
->bb
, dest_bb
))
5047 /* Update the last node of the list and move to the next one. */
5052 /* If we didn't find an assertion already registered for
5053 NAME COMP_CODE VAL, add a new one at the end of the list of
5054 assertions associated with NAME. */
5055 n
= XNEW (struct assert_locus
);
5059 n
->comp_code
= comp_code
;
5067 asserts_for
[SSA_NAME_VERSION (name
)] = n
;
5069 bitmap_set_bit (need_assert_for
, SSA_NAME_VERSION (name
));
5072 /* (COND_OP0 COND_CODE COND_OP1) is a predicate which uses NAME.
5073 Extract a suitable test code and value and store them into *CODE_P and
5074 *VAL_P so the predicate is normalized to NAME *CODE_P *VAL_P.
5076 If no extraction was possible, return FALSE, otherwise return TRUE.
5078 If INVERT is true, then we invert the result stored into *CODE_P. */
5081 extract_code_and_val_from_cond_with_ops (tree name
, enum tree_code cond_code
,
5082 tree cond_op0
, tree cond_op1
,
5083 bool invert
, enum tree_code
*code_p
,
5086 enum tree_code comp_code
;
5089 /* Otherwise, we have a comparison of the form NAME COMP VAL
5090 or VAL COMP NAME. */
5091 if (name
== cond_op1
)
5093 /* If the predicate is of the form VAL COMP NAME, flip
5094 COMP around because we need to register NAME as the
5095 first operand in the predicate. */
5096 comp_code
= swap_tree_comparison (cond_code
);
5101 /* The comparison is of the form NAME COMP VAL, so the
5102 comparison code remains unchanged. */
5103 comp_code
= cond_code
;
5107 /* Invert the comparison code as necessary. */
5109 comp_code
= invert_tree_comparison (comp_code
, 0);
5111 /* VRP does not handle float types. */
5112 if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (val
)))
5115 /* Do not register always-false predicates.
5116 FIXME: this works around a limitation in fold() when dealing with
5117 enumerations. Given 'enum { N1, N2 } x;', fold will not
5118 fold 'if (x > N2)' to 'if (0)'. */
5119 if ((comp_code
== GT_EXPR
|| comp_code
== LT_EXPR
)
5120 && INTEGRAL_TYPE_P (TREE_TYPE (val
)))
5122 tree min
= TYPE_MIN_VALUE (TREE_TYPE (val
));
5123 tree max
= TYPE_MAX_VALUE (TREE_TYPE (val
));
5125 if (comp_code
== GT_EXPR
5127 || compare_values (val
, max
) == 0))
5130 if (comp_code
== LT_EXPR
5132 || compare_values (val
, min
) == 0))
5135 *code_p
= comp_code
;
5140 /* Find out smallest RES where RES > VAL && (RES & MASK) == RES, if any
5141 (otherwise return VAL). VAL and MASK must be zero-extended for
5142 precision PREC. If SGNBIT is non-zero, first xor VAL with SGNBIT
5143 (to transform signed values into unsigned) and at the end xor
5147 masked_increment (const wide_int
&val_in
, const wide_int
&mask
,
5148 const wide_int
&sgnbit
, unsigned int prec
)
5150 wide_int bit
= wi::one (prec
), res
;
5153 wide_int val
= val_in
^ sgnbit
;
5154 for (i
= 0; i
< prec
; i
++, bit
+= bit
)
5157 if ((res
& bit
) == 0)
5160 res
= (val
+ bit
).and_not (res
);
5162 if (wi::gtu_p (res
, val
))
5163 return res
^ sgnbit
;
5165 return val
^ sgnbit
;
5168 /* Try to register an edge assertion for SSA name NAME on edge E for
5169 the condition COND contributing to the conditional jump pointed to by BSI.
5170 Invert the condition COND if INVERT is true. */
5173 register_edge_assert_for_2 (tree name
, edge e
, gimple_stmt_iterator bsi
,
5174 enum tree_code cond_code
,
5175 tree cond_op0
, tree cond_op1
, bool invert
)
5178 enum tree_code comp_code
;
5180 if (!extract_code_and_val_from_cond_with_ops (name
, cond_code
,
5183 invert
, &comp_code
, &val
))
5186 /* Only register an ASSERT_EXPR if NAME was found in the sub-graph
5187 reachable from E. */
5188 if (live_on_edge (e
, name
)
5189 && !has_single_use (name
))
5190 register_new_assert_for (name
, name
, comp_code
, val
, NULL
, e
, bsi
);
5192 /* In the case of NAME <= CST and NAME being defined as
5193 NAME = (unsigned) NAME2 + CST2 we can assert NAME2 >= -CST2
5194 and NAME2 <= CST - CST2. We can do the same for NAME > CST.
5195 This catches range and anti-range tests. */
5196 if ((comp_code
== LE_EXPR
5197 || comp_code
== GT_EXPR
)
5198 && TREE_CODE (val
) == INTEGER_CST
5199 && TYPE_UNSIGNED (TREE_TYPE (val
)))
5201 gimple def_stmt
= SSA_NAME_DEF_STMT (name
);
5202 tree cst2
= NULL_TREE
, name2
= NULL_TREE
, name3
= NULL_TREE
;
5204 /* Extract CST2 from the (optional) addition. */
5205 if (is_gimple_assign (def_stmt
)
5206 && gimple_assign_rhs_code (def_stmt
) == PLUS_EXPR
)
5208 name2
= gimple_assign_rhs1 (def_stmt
);
5209 cst2
= gimple_assign_rhs2 (def_stmt
);
5210 if (TREE_CODE (name2
) == SSA_NAME
5211 && TREE_CODE (cst2
) == INTEGER_CST
)
5212 def_stmt
= SSA_NAME_DEF_STMT (name2
);
5215 /* Extract NAME2 from the (optional) sign-changing cast. */
5216 if (gimple_assign_cast_p (def_stmt
))
5218 if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt
))
5219 && ! TYPE_UNSIGNED (TREE_TYPE (gimple_assign_rhs1 (def_stmt
)))
5220 && (TYPE_PRECISION (gimple_expr_type (def_stmt
))
5221 == TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt
)))))
5222 name3
= gimple_assign_rhs1 (def_stmt
);
5225 /* If name3 is used later, create an ASSERT_EXPR for it. */
5226 if (name3
!= NULL_TREE
5227 && TREE_CODE (name3
) == SSA_NAME
5228 && (cst2
== NULL_TREE
5229 || TREE_CODE (cst2
) == INTEGER_CST
)
5230 && INTEGRAL_TYPE_P (TREE_TYPE (name3
))
5231 && live_on_edge (e
, name3
)
5232 && !has_single_use (name3
))
5236 /* Build an expression for the range test. */
5237 tmp
= build1 (NOP_EXPR
, TREE_TYPE (name
), name3
);
5238 if (cst2
!= NULL_TREE
)
5239 tmp
= build2 (PLUS_EXPR
, TREE_TYPE (name
), tmp
, cst2
);
5243 fprintf (dump_file
, "Adding assert for ");
5244 print_generic_expr (dump_file
, name3
, 0);
5245 fprintf (dump_file
, " from ");
5246 print_generic_expr (dump_file
, tmp
, 0);
5247 fprintf (dump_file
, "\n");
5250 register_new_assert_for (name3
, tmp
, comp_code
, val
, NULL
, e
, bsi
);
5253 /* If name2 is used later, create an ASSERT_EXPR for it. */
5254 if (name2
!= NULL_TREE
5255 && TREE_CODE (name2
) == SSA_NAME
5256 && TREE_CODE (cst2
) == INTEGER_CST
5257 && INTEGRAL_TYPE_P (TREE_TYPE (name2
))
5258 && live_on_edge (e
, name2
)
5259 && !has_single_use (name2
))
5263 /* Build an expression for the range test. */
5265 if (TREE_TYPE (name
) != TREE_TYPE (name2
))
5266 tmp
= build1 (NOP_EXPR
, TREE_TYPE (name
), tmp
);
5267 if (cst2
!= NULL_TREE
)
5268 tmp
= build2 (PLUS_EXPR
, TREE_TYPE (name
), tmp
, cst2
);
5272 fprintf (dump_file
, "Adding assert for ");
5273 print_generic_expr (dump_file
, name2
, 0);
5274 fprintf (dump_file
, " from ");
5275 print_generic_expr (dump_file
, tmp
, 0);
5276 fprintf (dump_file
, "\n");
5279 register_new_assert_for (name2
, tmp
, comp_code
, val
, NULL
, e
, bsi
);
5283 /* In the case of post-in/decrement tests like if (i++) ... and uses
5284 of the in/decremented value on the edge the extra name we want to
5285 assert for is not on the def chain of the name compared. Instead
5286 it is in the set of use stmts.
5287 Similar cases happen for conversions that were simplified through
5288 fold_{sign_changed,widened}_comparison. */
5289 if ((comp_code
== NE_EXPR
5290 || comp_code
== EQ_EXPR
)
5291 && TREE_CODE (val
) == INTEGER_CST
)
5293 imm_use_iterator ui
;
5295 FOR_EACH_IMM_USE_STMT (use_stmt
, ui
, name
)
5297 if (!is_gimple_assign (use_stmt
))
5300 /* Cut off to use-stmts that are dominating the predecessor. */
5301 if (!dominated_by_p (CDI_DOMINATORS
, e
->src
, gimple_bb (use_stmt
)))
5304 tree name2
= gimple_assign_lhs (use_stmt
);
5305 if (TREE_CODE (name2
) != SSA_NAME
5306 || !live_on_edge (e
, name2
))
5309 enum tree_code code
= gimple_assign_rhs_code (use_stmt
);
5311 if (code
== PLUS_EXPR
5312 || code
== MINUS_EXPR
)
5314 cst
= gimple_assign_rhs2 (use_stmt
);
5315 if (TREE_CODE (cst
) != INTEGER_CST
)
5317 cst
= int_const_binop (code
, val
, cst
);
5319 else if (CONVERT_EXPR_CODE_P (code
))
5321 /* For truncating conversions we cannot record
5323 if (comp_code
== NE_EXPR
5324 && (TYPE_PRECISION (TREE_TYPE (name2
))
5325 < TYPE_PRECISION (TREE_TYPE (name
))))
5327 cst
= fold_convert (TREE_TYPE (name2
), val
);
5332 if (TREE_OVERFLOW_P (cst
))
5333 cst
= drop_tree_overflow (cst
);
5334 register_new_assert_for (name2
, name2
, comp_code
, cst
,
5339 if (TREE_CODE_CLASS (comp_code
) == tcc_comparison
5340 && TREE_CODE (val
) == INTEGER_CST
)
5342 gimple def_stmt
= SSA_NAME_DEF_STMT (name
);
5343 tree name2
= NULL_TREE
, names
[2], cst2
= NULL_TREE
;
5344 tree val2
= NULL_TREE
;
5345 unsigned int prec
= TYPE_PRECISION (TREE_TYPE (val
));
5346 wide_int mask
= wi::zero (prec
);
5347 unsigned int nprec
= prec
;
5348 enum tree_code rhs_code
= ERROR_MARK
;
5350 if (is_gimple_assign (def_stmt
))
5351 rhs_code
= gimple_assign_rhs_code (def_stmt
);
5353 /* Add asserts for NAME cmp CST and NAME being defined
5354 as NAME = (int) NAME2. */
5355 if (!TYPE_UNSIGNED (TREE_TYPE (val
))
5356 && (comp_code
== LE_EXPR
|| comp_code
== LT_EXPR
5357 || comp_code
== GT_EXPR
|| comp_code
== GE_EXPR
)
5358 && gimple_assign_cast_p (def_stmt
))
5360 name2
= gimple_assign_rhs1 (def_stmt
);
5361 if (CONVERT_EXPR_CODE_P (rhs_code
)
5362 && INTEGRAL_TYPE_P (TREE_TYPE (name2
))
5363 && TYPE_UNSIGNED (TREE_TYPE (name2
))
5364 && prec
== TYPE_PRECISION (TREE_TYPE (name2
))
5365 && (comp_code
== LE_EXPR
|| comp_code
== GT_EXPR
5366 || !tree_int_cst_equal (val
,
5367 TYPE_MIN_VALUE (TREE_TYPE (val
))))
5368 && live_on_edge (e
, name2
)
5369 && !has_single_use (name2
))
5372 enum tree_code new_comp_code
= comp_code
;
5374 cst
= fold_convert (TREE_TYPE (name2
),
5375 TYPE_MIN_VALUE (TREE_TYPE (val
)));
5376 /* Build an expression for the range test. */
5377 tmp
= build2 (PLUS_EXPR
, TREE_TYPE (name2
), name2
, cst
);
5378 cst
= fold_build2 (PLUS_EXPR
, TREE_TYPE (name2
), cst
,
5379 fold_convert (TREE_TYPE (name2
), val
));
5380 if (comp_code
== LT_EXPR
|| comp_code
== GE_EXPR
)
5382 new_comp_code
= comp_code
== LT_EXPR
? LE_EXPR
: GT_EXPR
;
5383 cst
= fold_build2 (MINUS_EXPR
, TREE_TYPE (name2
), cst
,
5384 build_int_cst (TREE_TYPE (name2
), 1));
5389 fprintf (dump_file
, "Adding assert for ");
5390 print_generic_expr (dump_file
, name2
, 0);
5391 fprintf (dump_file
, " from ");
5392 print_generic_expr (dump_file
, tmp
, 0);
5393 fprintf (dump_file
, "\n");
5396 register_new_assert_for (name2
, tmp
, new_comp_code
, cst
, NULL
,
5401 /* Add asserts for NAME cmp CST and NAME being defined as
5402 NAME = NAME2 >> CST2.
5404 Extract CST2 from the right shift. */
5405 if (rhs_code
== RSHIFT_EXPR
)
5407 name2
= gimple_assign_rhs1 (def_stmt
);
5408 cst2
= gimple_assign_rhs2 (def_stmt
);
5409 if (TREE_CODE (name2
) == SSA_NAME
5410 && tree_fits_uhwi_p (cst2
)
5411 && INTEGRAL_TYPE_P (TREE_TYPE (name2
))
5412 && IN_RANGE (tree_to_uhwi (cst2
), 1, prec
- 1)
5413 && prec
== GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (val
)))
5414 && live_on_edge (e
, name2
)
5415 && !has_single_use (name2
))
5417 mask
= wi::mask (tree_to_uhwi (cst2
), false, prec
);
5418 val2
= fold_binary (LSHIFT_EXPR
, TREE_TYPE (val
), val
, cst2
);
5421 if (val2
!= NULL_TREE
5422 && TREE_CODE (val2
) == INTEGER_CST
5423 && simple_cst_equal (fold_build2 (RSHIFT_EXPR
,
5427 enum tree_code new_comp_code
= comp_code
;
5431 if (comp_code
== EQ_EXPR
|| comp_code
== NE_EXPR
)
5433 if (!TYPE_UNSIGNED (TREE_TYPE (val
)))
5435 tree type
= build_nonstandard_integer_type (prec
, 1);
5436 tmp
= build1 (NOP_EXPR
, type
, name2
);
5437 val2
= fold_convert (type
, val2
);
5439 tmp
= fold_build2 (MINUS_EXPR
, TREE_TYPE (tmp
), tmp
, val2
);
5440 new_val
= wide_int_to_tree (TREE_TYPE (tmp
), mask
);
5441 new_comp_code
= comp_code
== EQ_EXPR
? LE_EXPR
: GT_EXPR
;
5443 else if (comp_code
== LT_EXPR
|| comp_code
== GE_EXPR
)
5446 = wi::min_value (prec
, TYPE_SIGN (TREE_TYPE (val
)));
5448 if (minval
== new_val
)
5449 new_val
= NULL_TREE
;
5454 = wi::max_value (prec
, TYPE_SIGN (TREE_TYPE (val
)));
5457 new_val
= NULL_TREE
;
5459 new_val
= wide_int_to_tree (TREE_TYPE (val2
), mask
);
5466 fprintf (dump_file
, "Adding assert for ");
5467 print_generic_expr (dump_file
, name2
, 0);
5468 fprintf (dump_file
, " from ");
5469 print_generic_expr (dump_file
, tmp
, 0);
5470 fprintf (dump_file
, "\n");
5473 register_new_assert_for (name2
, tmp
, new_comp_code
, new_val
,
5478 /* Add asserts for NAME cmp CST and NAME being defined as
5479 NAME = NAME2 & CST2.
5481 Extract CST2 from the and.
5484 NAME = (unsigned) NAME2;
5485 casts where NAME's type is unsigned and has smaller precision
5486 than NAME2's type as if it was NAME = NAME2 & MASK. */
5487 names
[0] = NULL_TREE
;
5488 names
[1] = NULL_TREE
;
5490 if (rhs_code
== BIT_AND_EXPR
5491 || (CONVERT_EXPR_CODE_P (rhs_code
)
5492 && TREE_CODE (TREE_TYPE (val
)) == INTEGER_TYPE
5493 && TYPE_UNSIGNED (TREE_TYPE (val
))
5494 && TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt
)))
5497 name2
= gimple_assign_rhs1 (def_stmt
);
5498 if (rhs_code
== BIT_AND_EXPR
)
5499 cst2
= gimple_assign_rhs2 (def_stmt
);
5502 cst2
= TYPE_MAX_VALUE (TREE_TYPE (val
));
5503 nprec
= TYPE_PRECISION (TREE_TYPE (name2
));
5505 if (TREE_CODE (name2
) == SSA_NAME
5506 && INTEGRAL_TYPE_P (TREE_TYPE (name2
))
5507 && TREE_CODE (cst2
) == INTEGER_CST
5508 && !integer_zerop (cst2
)
5510 || TYPE_UNSIGNED (TREE_TYPE (val
))))
5512 gimple def_stmt2
= SSA_NAME_DEF_STMT (name2
);
5513 if (gimple_assign_cast_p (def_stmt2
))
5515 names
[1] = gimple_assign_rhs1 (def_stmt2
);
5516 if (!CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt2
))
5517 || !INTEGRAL_TYPE_P (TREE_TYPE (names
[1]))
5518 || (TYPE_PRECISION (TREE_TYPE (name2
))
5519 != TYPE_PRECISION (TREE_TYPE (names
[1])))
5520 || !live_on_edge (e
, names
[1])
5521 || has_single_use (names
[1]))
5522 names
[1] = NULL_TREE
;
5524 if (live_on_edge (e
, name2
)
5525 && !has_single_use (name2
))
5529 if (names
[0] || names
[1])
5531 wide_int minv
, maxv
, valv
, cst2v
;
5532 wide_int tem
, sgnbit
;
5533 bool valid_p
= false, valn
, cst2n
;
5534 enum tree_code ccode
= comp_code
;
5536 valv
= wide_int::from (val
, nprec
, UNSIGNED
);
5537 cst2v
= wide_int::from (cst2
, nprec
, UNSIGNED
);
5538 valn
= wi::neg_p (valv
, TYPE_SIGN (TREE_TYPE (val
)));
5539 cst2n
= wi::neg_p (cst2v
, TYPE_SIGN (TREE_TYPE (val
)));
5540 /* If CST2 doesn't have most significant bit set,
5541 but VAL is negative, we have comparison like
5542 if ((x & 0x123) > -4) (always true). Just give up. */
5546 sgnbit
= wi::set_bit_in_zero (nprec
- 1, nprec
);
5548 sgnbit
= wi::zero (nprec
);
5549 minv
= valv
& cst2v
;
5553 /* Minimum unsigned value for equality is VAL & CST2
5554 (should be equal to VAL, otherwise we probably should
5555 have folded the comparison into false) and
5556 maximum unsigned value is VAL | ~CST2. */
5557 maxv
= valv
| ~cst2v
;
5562 tem
= valv
| ~cst2v
;
5563 /* If VAL is 0, handle (X & CST2) != 0 as (X & CST2) > 0U. */
5567 sgnbit
= wi::zero (nprec
);
5570 /* If (VAL | ~CST2) is all ones, handle it as
5571 (X & CST2) < VAL. */
5576 sgnbit
= wi::zero (nprec
);
5579 if (!cst2n
&& wi::neg_p (cst2v
))
5580 sgnbit
= wi::set_bit_in_zero (nprec
- 1, nprec
);
5589 if (tem
== wi::mask (nprec
- 1, false, nprec
))
5595 sgnbit
= wi::zero (nprec
);
5600 /* Minimum unsigned value for >= if (VAL & CST2) == VAL
5601 is VAL and maximum unsigned value is ~0. For signed
5602 comparison, if CST2 doesn't have most significant bit
5603 set, handle it similarly. If CST2 has MSB set,
5604 the minimum is the same, and maximum is ~0U/2. */
5607 /* If (VAL & CST2) != VAL, X & CST2 can't be equal to
5609 minv
= masked_increment (valv
, cst2v
, sgnbit
, nprec
);
5613 maxv
= wi::mask (nprec
- (cst2n
? 1 : 0), false, nprec
);
5619 /* Find out smallest MINV where MINV > VAL
5620 && (MINV & CST2) == MINV, if any. If VAL is signed and
5621 CST2 has MSB set, compute it biased by 1 << (nprec - 1). */
5622 minv
= masked_increment (valv
, cst2v
, sgnbit
, nprec
);
5625 maxv
= wi::mask (nprec
- (cst2n
? 1 : 0), false, nprec
);
5630 /* Minimum unsigned value for <= is 0 and maximum
5631 unsigned value is VAL | ~CST2 if (VAL & CST2) == VAL.
5632 Otherwise, find smallest VAL2 where VAL2 > VAL
5633 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
5635 For signed comparison, if CST2 doesn't have most
5636 significant bit set, handle it similarly. If CST2 has
5637 MSB set, the maximum is the same and minimum is INT_MIN. */
5642 maxv
= masked_increment (valv
, cst2v
, sgnbit
, nprec
);
5654 /* Minimum unsigned value for < is 0 and maximum
5655 unsigned value is (VAL-1) | ~CST2 if (VAL & CST2) == VAL.
5656 Otherwise, find smallest VAL2 where VAL2 > VAL
5657 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
5659 For signed comparison, if CST2 doesn't have most
5660 significant bit set, handle it similarly. If CST2 has
5661 MSB set, the maximum is the same and minimum is INT_MIN. */
5670 maxv
= masked_increment (valv
, cst2v
, sgnbit
, nprec
);
5684 && (maxv
- minv
) != -1)
5686 tree tmp
, new_val
, type
;
5689 for (i
= 0; i
< 2; i
++)
5692 wide_int maxv2
= maxv
;
5694 type
= TREE_TYPE (names
[i
]);
5695 if (!TYPE_UNSIGNED (type
))
5697 type
= build_nonstandard_integer_type (nprec
, 1);
5698 tmp
= build1 (NOP_EXPR
, type
, names
[i
]);
5702 tmp
= build2 (PLUS_EXPR
, type
, tmp
,
5703 wide_int_to_tree (type
, -minv
));
5704 maxv2
= maxv
- minv
;
5706 new_val
= wide_int_to_tree (type
, maxv2
);
5710 fprintf (dump_file
, "Adding assert for ");
5711 print_generic_expr (dump_file
, names
[i
], 0);
5712 fprintf (dump_file
, " from ");
5713 print_generic_expr (dump_file
, tmp
, 0);
5714 fprintf (dump_file
, "\n");
5717 register_new_assert_for (names
[i
], tmp
, LE_EXPR
,
5718 new_val
, NULL
, e
, bsi
);
5725 /* OP is an operand of a truth value expression which is known to have
5726 a particular value. Register any asserts for OP and for any
5727 operands in OP's defining statement.
5729 If CODE is EQ_EXPR, then we want to register OP is zero (false),
5730 if CODE is NE_EXPR, then we want to register OP is nonzero (true). */
5733 register_edge_assert_for_1 (tree op
, enum tree_code code
,
5734 edge e
, gimple_stmt_iterator bsi
)
5738 enum tree_code rhs_code
;
5740 /* We only care about SSA_NAMEs. */
5741 if (TREE_CODE (op
) != SSA_NAME
)
5744 /* We know that OP will have a zero or nonzero value. If OP is used
5745 more than once go ahead and register an assert for OP. */
5746 if (live_on_edge (e
, op
)
5747 && !has_single_use (op
))
5749 val
= build_int_cst (TREE_TYPE (op
), 0);
5750 register_new_assert_for (op
, op
, code
, val
, NULL
, e
, bsi
);
5753 /* Now look at how OP is set. If it's set from a comparison,
5754 a truth operation or some bit operations, then we may be able
5755 to register information about the operands of that assignment. */
5756 op_def
= SSA_NAME_DEF_STMT (op
);
5757 if (gimple_code (op_def
) != GIMPLE_ASSIGN
)
5760 rhs_code
= gimple_assign_rhs_code (op_def
);
5762 if (TREE_CODE_CLASS (rhs_code
) == tcc_comparison
)
5764 bool invert
= (code
== EQ_EXPR
? true : false);
5765 tree op0
= gimple_assign_rhs1 (op_def
);
5766 tree op1
= gimple_assign_rhs2 (op_def
);
5768 if (TREE_CODE (op0
) == SSA_NAME
)
5769 register_edge_assert_for_2 (op0
, e
, bsi
, rhs_code
, op0
, op1
, invert
);
5770 if (TREE_CODE (op1
) == SSA_NAME
)
5771 register_edge_assert_for_2 (op1
, e
, bsi
, rhs_code
, op0
, op1
, invert
);
5773 else if ((code
== NE_EXPR
5774 && gimple_assign_rhs_code (op_def
) == BIT_AND_EXPR
)
5776 && gimple_assign_rhs_code (op_def
) == BIT_IOR_EXPR
))
5778 /* Recurse on each operand. */
5779 tree op0
= gimple_assign_rhs1 (op_def
);
5780 tree op1
= gimple_assign_rhs2 (op_def
);
5781 if (TREE_CODE (op0
) == SSA_NAME
5782 && has_single_use (op0
))
5783 register_edge_assert_for_1 (op0
, code
, e
, bsi
);
5784 if (TREE_CODE (op1
) == SSA_NAME
5785 && has_single_use (op1
))
5786 register_edge_assert_for_1 (op1
, code
, e
, bsi
);
5788 else if (gimple_assign_rhs_code (op_def
) == BIT_NOT_EXPR
5789 && TYPE_PRECISION (TREE_TYPE (gimple_assign_lhs (op_def
))) == 1)
5791 /* Recurse, flipping CODE. */
5792 code
= invert_tree_comparison (code
, false);
5793 register_edge_assert_for_1 (gimple_assign_rhs1 (op_def
), code
, e
, bsi
);
5795 else if (gimple_assign_rhs_code (op_def
) == SSA_NAME
)
5797 /* Recurse through the copy. */
5798 register_edge_assert_for_1 (gimple_assign_rhs1 (op_def
), code
, e
, bsi
);
5800 else if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (op_def
)))
5802 /* Recurse through the type conversion, unless it is a narrowing
5803 conversion or conversion from non-integral type. */
5804 tree rhs
= gimple_assign_rhs1 (op_def
);
5805 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs
))
5806 && (TYPE_PRECISION (TREE_TYPE (rhs
))
5807 <= TYPE_PRECISION (TREE_TYPE (op
))))
5808 register_edge_assert_for_1 (rhs
, code
, e
, bsi
);
5812 /* Try to register an edge assertion for SSA name NAME on edge E for
5813 the condition COND contributing to the conditional jump pointed to by
5817 register_edge_assert_for (tree name
, edge e
, gimple_stmt_iterator si
,
5818 enum tree_code cond_code
, tree cond_op0
,
5822 enum tree_code comp_code
;
5823 bool is_else_edge
= (e
->flags
& EDGE_FALSE_VALUE
) != 0;
5825 /* Do not attempt to infer anything in names that flow through
5827 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name
))
5830 if (!extract_code_and_val_from_cond_with_ops (name
, cond_code
,
5836 /* Register ASSERT_EXPRs for name. */
5837 register_edge_assert_for_2 (name
, e
, si
, cond_code
, cond_op0
,
5838 cond_op1
, is_else_edge
);
5841 /* If COND is effectively an equality test of an SSA_NAME against
5842 the value zero or one, then we may be able to assert values
5843 for SSA_NAMEs which flow into COND. */
5845 /* In the case of NAME == 1 or NAME != 0, for BIT_AND_EXPR defining
5846 statement of NAME we can assert both operands of the BIT_AND_EXPR
5847 have nonzero value. */
5848 if (((comp_code
== EQ_EXPR
&& integer_onep (val
))
5849 || (comp_code
== NE_EXPR
&& integer_zerop (val
))))
5851 gimple def_stmt
= SSA_NAME_DEF_STMT (name
);
5853 if (is_gimple_assign (def_stmt
)
5854 && gimple_assign_rhs_code (def_stmt
) == BIT_AND_EXPR
)
5856 tree op0
= gimple_assign_rhs1 (def_stmt
);
5857 tree op1
= gimple_assign_rhs2 (def_stmt
);
5858 register_edge_assert_for_1 (op0
, NE_EXPR
, e
, si
);
5859 register_edge_assert_for_1 (op1
, NE_EXPR
, e
, si
);
5863 /* In the case of NAME == 0 or NAME != 1, for BIT_IOR_EXPR defining
5864 statement of NAME we can assert both operands of the BIT_IOR_EXPR
5866 if (((comp_code
== EQ_EXPR
&& integer_zerop (val
))
5867 || (comp_code
== NE_EXPR
&& integer_onep (val
))))
5869 gimple def_stmt
= SSA_NAME_DEF_STMT (name
);
5871 /* For BIT_IOR_EXPR only if NAME == 0 both operands have
5872 necessarily zero value, or if type-precision is one. */
5873 if (is_gimple_assign (def_stmt
)
5874 && (gimple_assign_rhs_code (def_stmt
) == BIT_IOR_EXPR
5875 && (TYPE_PRECISION (TREE_TYPE (name
)) == 1
5876 || comp_code
== EQ_EXPR
)))
5878 tree op0
= gimple_assign_rhs1 (def_stmt
);
5879 tree op1
= gimple_assign_rhs2 (def_stmt
);
5880 register_edge_assert_for_1 (op0
, EQ_EXPR
, e
, si
);
5881 register_edge_assert_for_1 (op1
, EQ_EXPR
, e
, si
);
5887 /* Determine whether the outgoing edges of BB should receive an
5888 ASSERT_EXPR for each of the operands of BB's LAST statement.
5889 The last statement of BB must be a COND_EXPR.
5891 If any of the sub-graphs rooted at BB have an interesting use of
5892 the predicate operands, an assert location node is added to the
5893 list of assertions for the corresponding operands. */
5896 find_conditional_asserts (basic_block bb
, gcond
*last
)
5898 gimple_stmt_iterator bsi
;
5904 bsi
= gsi_for_stmt (last
);
5906 /* Look for uses of the operands in each of the sub-graphs
5907 rooted at BB. We need to check each of the outgoing edges
5908 separately, so that we know what kind of ASSERT_EXPR to
5910 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
5915 /* Register the necessary assertions for each operand in the
5916 conditional predicate. */
5917 FOR_EACH_SSA_TREE_OPERAND (op
, last
, iter
, SSA_OP_USE
)
5918 register_edge_assert_for (op
, e
, bsi
,
5919 gimple_cond_code (last
),
5920 gimple_cond_lhs (last
),
5921 gimple_cond_rhs (last
));
5931 /* Compare two case labels sorting first by the destination bb index
5932 and then by the case value. */
5935 compare_case_labels (const void *p1
, const void *p2
)
5937 const struct case_info
*ci1
= (const struct case_info
*) p1
;
5938 const struct case_info
*ci2
= (const struct case_info
*) p2
;
5939 int idx1
= ci1
->bb
->index
;
5940 int idx2
= ci2
->bb
->index
;
5944 else if (idx1
== idx2
)
5946 /* Make sure the default label is first in a group. */
5947 if (!CASE_LOW (ci1
->expr
))
5949 else if (!CASE_LOW (ci2
->expr
))
5952 return tree_int_cst_compare (CASE_LOW (ci1
->expr
),
5953 CASE_LOW (ci2
->expr
));
5959 /* Determine whether the outgoing edges of BB should receive an
5960 ASSERT_EXPR for each of the operands of BB's LAST statement.
5961 The last statement of BB must be a SWITCH_EXPR.
5963 If any of the sub-graphs rooted at BB have an interesting use of
5964 the predicate operands, an assert location node is added to the
5965 list of assertions for the corresponding operands. */
5968 find_switch_asserts (basic_block bb
, gswitch
*last
)
5970 gimple_stmt_iterator bsi
;
5973 struct case_info
*ci
;
5974 size_t n
= gimple_switch_num_labels (last
);
5975 #if GCC_VERSION >= 4000
5978 /* Work around GCC 3.4 bug (PR 37086). */
5979 volatile unsigned int idx
;
5982 bsi
= gsi_for_stmt (last
);
5983 op
= gimple_switch_index (last
);
5984 if (TREE_CODE (op
) != SSA_NAME
)
5987 /* Build a vector of case labels sorted by destination label. */
5988 ci
= XNEWVEC (struct case_info
, n
);
5989 for (idx
= 0; idx
< n
; ++idx
)
5991 ci
[idx
].expr
= gimple_switch_label (last
, idx
);
5992 ci
[idx
].bb
= label_to_block (CASE_LABEL (ci
[idx
].expr
));
5994 qsort (ci
, n
, sizeof (struct case_info
), compare_case_labels
);
5996 for (idx
= 0; idx
< n
; ++idx
)
5999 tree cl
= ci
[idx
].expr
;
6000 basic_block cbb
= ci
[idx
].bb
;
6002 min
= CASE_LOW (cl
);
6003 max
= CASE_HIGH (cl
);
6005 /* If there are multiple case labels with the same destination
6006 we need to combine them to a single value range for the edge. */
6007 if (idx
+ 1 < n
&& cbb
== ci
[idx
+ 1].bb
)
6009 /* Skip labels until the last of the group. */
6012 } while (idx
< n
&& cbb
== ci
[idx
].bb
);
6015 /* Pick up the maximum of the case label range. */
6016 if (CASE_HIGH (ci
[idx
].expr
))
6017 max
= CASE_HIGH (ci
[idx
].expr
);
6019 max
= CASE_LOW (ci
[idx
].expr
);
6022 /* Nothing to do if the range includes the default label until we
6023 can register anti-ranges. */
6024 if (min
== NULL_TREE
)
6027 /* Find the edge to register the assert expr on. */
6028 e
= find_edge (bb
, cbb
);
6030 /* Register the necessary assertions for the operand in the
6032 register_edge_assert_for (op
, e
, bsi
,
6033 max
? GE_EXPR
: EQ_EXPR
,
6034 op
, fold_convert (TREE_TYPE (op
), min
));
6036 register_edge_assert_for (op
, e
, bsi
, LE_EXPR
, op
,
6037 fold_convert (TREE_TYPE (op
), max
));
6044 /* Traverse all the statements in block BB looking for statements that
6045 may generate useful assertions for the SSA names in their operand.
6046 If a statement produces a useful assertion A for name N_i, then the
6047 list of assertions already generated for N_i is scanned to
6048 determine if A is actually needed.
6050 If N_i already had the assertion A at a location dominating the
6051 current location, then nothing needs to be done. Otherwise, the
6052 new location for A is recorded instead.
6054 1- For every statement S in BB, all the variables used by S are
6055 added to bitmap FOUND_IN_SUBGRAPH.
6057 2- If statement S uses an operand N in a way that exposes a known
6058 value range for N, then if N was not already generated by an
6059 ASSERT_EXPR, create a new assert location for N. For instance,
6060 if N is a pointer and the statement dereferences it, we can
6061 assume that N is not NULL.
6063 3- COND_EXPRs are a special case of #2. We can derive range
6064 information from the predicate but need to insert different
6065 ASSERT_EXPRs for each of the sub-graphs rooted at the
6066 conditional block. If the last statement of BB is a conditional
6067 expression of the form 'X op Y', then
6069 a) Remove X and Y from the set FOUND_IN_SUBGRAPH.
6071 b) If the conditional is the only entry point to the sub-graph
6072 corresponding to the THEN_CLAUSE, recurse into it. On
6073 return, if X and/or Y are marked in FOUND_IN_SUBGRAPH, then
6074 an ASSERT_EXPR is added for the corresponding variable.
6076 c) Repeat step (b) on the ELSE_CLAUSE.
6078 d) Mark X and Y in FOUND_IN_SUBGRAPH.
6087 In this case, an assertion on the THEN clause is useful to
6088 determine that 'a' is always 9 on that edge. However, an assertion
6089 on the ELSE clause would be unnecessary.
6091 4- If BB does not end in a conditional expression, then we recurse
6092 into BB's dominator children.
6094 At the end of the recursive traversal, every SSA name will have a
6095 list of locations where ASSERT_EXPRs should be added. When a new
6096 location for name N is found, it is registered by calling
6097 register_new_assert_for. That function keeps track of all the
6098 registered assertions to prevent adding unnecessary assertions.
6099 For instance, if a pointer P_4 is dereferenced more than once in a
6100 dominator tree, only the location dominating all the dereference of
6101 P_4 will receive an ASSERT_EXPR. */
6104 find_assert_locations_1 (basic_block bb
, sbitmap live
)
6108 last
= last_stmt (bb
);
6110 /* If BB's last statement is a conditional statement involving integer
6111 operands, determine if we need to add ASSERT_EXPRs. */
6113 && gimple_code (last
) == GIMPLE_COND
6114 && !fp_predicate (last
)
6115 && !ZERO_SSA_OPERANDS (last
, SSA_OP_USE
))
6116 find_conditional_asserts (bb
, as_a
<gcond
*> (last
));
6118 /* If BB's last statement is a switch statement involving integer
6119 operands, determine if we need to add ASSERT_EXPRs. */
6121 && gimple_code (last
) == GIMPLE_SWITCH
6122 && !ZERO_SSA_OPERANDS (last
, SSA_OP_USE
))
6123 find_switch_asserts (bb
, as_a
<gswitch
*> (last
));
6125 /* Traverse all the statements in BB marking used names and looking
6126 for statements that may infer assertions for their used operands. */
6127 for (gimple_stmt_iterator si
= gsi_last_bb (bb
); !gsi_end_p (si
);
6134 stmt
= gsi_stmt (si
);
6136 if (is_gimple_debug (stmt
))
6139 /* See if we can derive an assertion for any of STMT's operands. */
6140 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, i
, SSA_OP_USE
)
6143 enum tree_code comp_code
;
6145 /* If op is not live beyond this stmt, do not bother to insert
6147 if (!bitmap_bit_p (live
, SSA_NAME_VERSION (op
)))
6150 /* If OP is used in such a way that we can infer a value
6151 range for it, and we don't find a previous assertion for
6152 it, create a new assertion location node for OP. */
6153 if (infer_value_range (stmt
, op
, &comp_code
, &value
))
6155 /* If we are able to infer a nonzero value range for OP,
6156 then walk backwards through the use-def chain to see if OP
6157 was set via a typecast.
6159 If so, then we can also infer a nonzero value range
6160 for the operand of the NOP_EXPR. */
6161 if (comp_code
== NE_EXPR
&& integer_zerop (value
))
6164 gimple def_stmt
= SSA_NAME_DEF_STMT (t
);
6166 while (is_gimple_assign (def_stmt
)
6167 && CONVERT_EXPR_CODE_P
6168 (gimple_assign_rhs_code (def_stmt
))
6170 (gimple_assign_rhs1 (def_stmt
)) == SSA_NAME
6172 (TREE_TYPE (gimple_assign_rhs1 (def_stmt
))))
6174 t
= gimple_assign_rhs1 (def_stmt
);
6175 def_stmt
= SSA_NAME_DEF_STMT (t
);
6177 /* Note we want to register the assert for the
6178 operand of the NOP_EXPR after SI, not after the
6180 if (! has_single_use (t
))
6181 register_new_assert_for (t
, t
, comp_code
, value
,
6186 register_new_assert_for (op
, op
, comp_code
, value
, bb
, NULL
, si
);
6191 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, i
, SSA_OP_USE
)
6192 bitmap_set_bit (live
, SSA_NAME_VERSION (op
));
6193 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, i
, SSA_OP_DEF
)
6194 bitmap_clear_bit (live
, SSA_NAME_VERSION (op
));
6197 /* Traverse all PHI nodes in BB, updating live. */
6198 for (gphi_iterator si
= gsi_start_phis (bb
); !gsi_end_p (si
);
6201 use_operand_p arg_p
;
6203 gphi
*phi
= si
.phi ();
6204 tree res
= gimple_phi_result (phi
);
6206 if (virtual_operand_p (res
))
6209 FOR_EACH_PHI_ARG (arg_p
, phi
, i
, SSA_OP_USE
)
6211 tree arg
= USE_FROM_PTR (arg_p
);
6212 if (TREE_CODE (arg
) == SSA_NAME
)
6213 bitmap_set_bit (live
, SSA_NAME_VERSION (arg
));
6216 bitmap_clear_bit (live
, SSA_NAME_VERSION (res
));
6220 /* Do an RPO walk over the function computing SSA name liveness
6221 on-the-fly and deciding on assert expressions to insert. */
6224 find_assert_locations (void)
6226 int *rpo
= XNEWVEC (int, last_basic_block_for_fn (cfun
));
6227 int *bb_rpo
= XNEWVEC (int, last_basic_block_for_fn (cfun
));
6228 int *last_rpo
= XCNEWVEC (int, last_basic_block_for_fn (cfun
));
6231 live
= XCNEWVEC (sbitmap
, last_basic_block_for_fn (cfun
));
6232 rpo_cnt
= pre_and_rev_post_order_compute (NULL
, rpo
, false);
6233 for (i
= 0; i
< rpo_cnt
; ++i
)
6236 /* Pre-seed loop latch liveness from loop header PHI nodes. Due to
6237 the order we compute liveness and insert asserts we otherwise
6238 fail to insert asserts into the loop latch. */
6240 FOR_EACH_LOOP (loop
, 0)
6242 i
= loop
->latch
->index
;
6243 unsigned int j
= single_succ_edge (loop
->latch
)->dest_idx
;
6244 for (gphi_iterator gsi
= gsi_start_phis (loop
->header
);
6245 !gsi_end_p (gsi
); gsi_next (&gsi
))
6247 gphi
*phi
= gsi
.phi ();
6248 if (virtual_operand_p (gimple_phi_result (phi
)))
6250 tree arg
= gimple_phi_arg_def (phi
, j
);
6251 if (TREE_CODE (arg
) == SSA_NAME
)
6253 if (live
[i
] == NULL
)
6255 live
[i
] = sbitmap_alloc (num_ssa_names
);
6256 bitmap_clear (live
[i
]);
6258 bitmap_set_bit (live
[i
], SSA_NAME_VERSION (arg
));
6263 for (i
= rpo_cnt
- 1; i
>= 0; --i
)
6265 basic_block bb
= BASIC_BLOCK_FOR_FN (cfun
, rpo
[i
]);
6271 live
[rpo
[i
]] = sbitmap_alloc (num_ssa_names
);
6272 bitmap_clear (live
[rpo
[i
]]);
6275 /* Process BB and update the live information with uses in
6277 find_assert_locations_1 (bb
, live
[rpo
[i
]]);
6279 /* Merge liveness into the predecessor blocks and free it. */
6280 if (!bitmap_empty_p (live
[rpo
[i
]]))
6283 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
6285 int pred
= e
->src
->index
;
6286 if ((e
->flags
& EDGE_DFS_BACK
) || pred
== ENTRY_BLOCK
)
6291 live
[pred
] = sbitmap_alloc (num_ssa_names
);
6292 bitmap_clear (live
[pred
]);
6294 bitmap_ior (live
[pred
], live
[pred
], live
[rpo
[i
]]);
6296 if (bb_rpo
[pred
] < pred_rpo
)
6297 pred_rpo
= bb_rpo
[pred
];
6300 /* Record the RPO number of the last visited block that needs
6301 live information from this block. */
6302 last_rpo
[rpo
[i
]] = pred_rpo
;
6306 sbitmap_free (live
[rpo
[i
]]);
6307 live
[rpo
[i
]] = NULL
;
6310 /* We can free all successors live bitmaps if all their
6311 predecessors have been visited already. */
6312 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
6313 if (last_rpo
[e
->dest
->index
] == i
6314 && live
[e
->dest
->index
])
6316 sbitmap_free (live
[e
->dest
->index
]);
6317 live
[e
->dest
->index
] = NULL
;
6322 XDELETEVEC (bb_rpo
);
6323 XDELETEVEC (last_rpo
);
6324 for (i
= 0; i
< last_basic_block_for_fn (cfun
); ++i
)
6326 sbitmap_free (live
[i
]);
6330 /* Create an ASSERT_EXPR for NAME and insert it in the location
6331 indicated by LOC. Return true if we made any edge insertions. */
6334 process_assert_insertions_for (tree name
, assert_locus
*loc
)
6336 /* Build the comparison expression NAME_i COMP_CODE VAL. */
6343 /* If we have X <=> X do not insert an assert expr for that. */
6344 if (loc
->expr
== loc
->val
)
6347 cond
= build2 (loc
->comp_code
, boolean_type_node
, loc
->expr
, loc
->val
);
6348 assert_stmt
= build_assert_expr_for (cond
, name
);
6351 /* We have been asked to insert the assertion on an edge. This
6352 is used only by COND_EXPR and SWITCH_EXPR assertions. */
6353 gcc_checking_assert (gimple_code (gsi_stmt (loc
->si
)) == GIMPLE_COND
6354 || (gimple_code (gsi_stmt (loc
->si
))
6357 gsi_insert_on_edge (loc
->e
, assert_stmt
);
6361 /* Otherwise, we can insert right after LOC->SI iff the
6362 statement must not be the last statement in the block. */
6363 stmt
= gsi_stmt (loc
->si
);
6364 if (!stmt_ends_bb_p (stmt
))
6366 gsi_insert_after (&loc
->si
, assert_stmt
, GSI_SAME_STMT
);
6370 /* If STMT must be the last statement in BB, we can only insert new
6371 assertions on the non-abnormal edge out of BB. Note that since
6372 STMT is not control flow, there may only be one non-abnormal edge
6374 FOR_EACH_EDGE (e
, ei
, loc
->bb
->succs
)
6375 if (!(e
->flags
& EDGE_ABNORMAL
))
6377 gsi_insert_on_edge (e
, assert_stmt
);
6385 /* Process all the insertions registered for every name N_i registered
6386 in NEED_ASSERT_FOR. The list of assertions to be inserted are
6387 found in ASSERTS_FOR[i]. */
6390 process_assert_insertions (void)
6394 bool update_edges_p
= false;
6395 int num_asserts
= 0;
6397 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6398 dump_all_asserts (dump_file
);
6400 EXECUTE_IF_SET_IN_BITMAP (need_assert_for
, 0, i
, bi
)
6402 assert_locus
*loc
= asserts_for
[i
];
6407 assert_locus
*next
= loc
->next
;
6408 update_edges_p
|= process_assert_insertions_for (ssa_name (i
), loc
);
6416 gsi_commit_edge_inserts ();
6418 statistics_counter_event (cfun
, "Number of ASSERT_EXPR expressions inserted",
6423 /* Traverse the flowgraph looking for conditional jumps to insert range
6424 expressions. These range expressions are meant to provide information
6425 to optimizations that need to reason in terms of value ranges. They
6426 will not be expanded into RTL. For instance, given:
6435 this pass will transform the code into:
6441 x = ASSERT_EXPR <x, x < y>
6446 y = ASSERT_EXPR <y, x >= y>
6450 The idea is that once copy and constant propagation have run, other
6451 optimizations will be able to determine what ranges of values can 'x'
6452 take in different paths of the code, simply by checking the reaching
6453 definition of 'x'. */
6456 insert_range_assertions (void)
6458 need_assert_for
= BITMAP_ALLOC (NULL
);
6459 asserts_for
= XCNEWVEC (assert_locus
*, num_ssa_names
);
6461 calculate_dominance_info (CDI_DOMINATORS
);
6463 find_assert_locations ();
6464 if (!bitmap_empty_p (need_assert_for
))
6466 process_assert_insertions ();
6467 update_ssa (TODO_update_ssa_no_phi
);
6470 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6472 fprintf (dump_file
, "\nSSA form after inserting ASSERT_EXPRs\n");
6473 dump_function_to_file (current_function_decl
, dump_file
, dump_flags
);
6477 BITMAP_FREE (need_assert_for
);
6480 /* Checks one ARRAY_REF in REF, located at LOCUS. Ignores flexible arrays
6481 and "struct" hacks. If VRP can determine that the
6482 array subscript is a constant, check if it is outside valid
6483 range. If the array subscript is a RANGE, warn if it is
6484 non-overlapping with valid range.
6485 IGNORE_OFF_BY_ONE is true if the ARRAY_REF is inside a ADDR_EXPR. */
6488 check_array_ref (location_t location
, tree ref
, bool ignore_off_by_one
)
6490 value_range_t
* vr
= NULL
;
6491 tree low_sub
, up_sub
;
6492 tree low_bound
, up_bound
, up_bound_p1
;
6495 if (TREE_NO_WARNING (ref
))
6498 low_sub
= up_sub
= TREE_OPERAND (ref
, 1);
6499 up_bound
= array_ref_up_bound (ref
);
6501 /* Can not check flexible arrays. */
6503 || TREE_CODE (up_bound
) != INTEGER_CST
)
6506 /* Accesses to trailing arrays via pointers may access storage
6507 beyond the types array bounds. */
6508 base
= get_base_address (ref
);
6509 if ((warn_array_bounds
< 2)
6510 && base
&& TREE_CODE (base
) == MEM_REF
)
6512 tree cref
, next
= NULL_TREE
;
6514 if (TREE_CODE (TREE_OPERAND (ref
, 0)) != COMPONENT_REF
)
6517 cref
= TREE_OPERAND (ref
, 0);
6518 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (cref
, 0))) == RECORD_TYPE
)
6519 for (next
= DECL_CHAIN (TREE_OPERAND (cref
, 1));
6520 next
&& TREE_CODE (next
) != FIELD_DECL
;
6521 next
= DECL_CHAIN (next
))
6524 /* If this is the last field in a struct type or a field in a
6525 union type do not warn. */
6530 low_bound
= array_ref_low_bound (ref
);
6531 up_bound_p1
= int_const_binop (PLUS_EXPR
, up_bound
,
6532 build_int_cst (TREE_TYPE (up_bound
), 1));
6535 if (tree_int_cst_equal (low_bound
, up_bound_p1
))
6537 warning_at (location
, OPT_Warray_bounds
,
6538 "array subscript is above array bounds");
6539 TREE_NO_WARNING (ref
) = 1;
6542 if (TREE_CODE (low_sub
) == SSA_NAME
)
6544 vr
= get_value_range (low_sub
);
6545 if (vr
->type
== VR_RANGE
|| vr
->type
== VR_ANTI_RANGE
)
6547 low_sub
= vr
->type
== VR_RANGE
? vr
->max
: vr
->min
;
6548 up_sub
= vr
->type
== VR_RANGE
? vr
->min
: vr
->max
;
6552 if (vr
&& vr
->type
== VR_ANTI_RANGE
)
6554 if (TREE_CODE (up_sub
) == INTEGER_CST
6555 && (ignore_off_by_one
6556 ? tree_int_cst_lt (up_bound
, up_sub
)
6557 : tree_int_cst_le (up_bound
, up_sub
))
6558 && TREE_CODE (low_sub
) == INTEGER_CST
6559 && tree_int_cst_le (low_sub
, low_bound
))
6561 warning_at (location
, OPT_Warray_bounds
,
6562 "array subscript is outside array bounds");
6563 TREE_NO_WARNING (ref
) = 1;
6566 else if (TREE_CODE (up_sub
) == INTEGER_CST
6567 && (ignore_off_by_one
6568 ? !tree_int_cst_le (up_sub
, up_bound_p1
)
6569 : !tree_int_cst_le (up_sub
, up_bound
)))
6571 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6573 fprintf (dump_file
, "Array bound warning for ");
6574 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, ref
);
6575 fprintf (dump_file
, "\n");
6577 warning_at (location
, OPT_Warray_bounds
,
6578 "array subscript is above array bounds");
6579 TREE_NO_WARNING (ref
) = 1;
6581 else if (TREE_CODE (low_sub
) == INTEGER_CST
6582 && tree_int_cst_lt (low_sub
, low_bound
))
6584 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6586 fprintf (dump_file
, "Array bound warning for ");
6587 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, ref
);
6588 fprintf (dump_file
, "\n");
6590 warning_at (location
, OPT_Warray_bounds
,
6591 "array subscript is below array bounds");
6592 TREE_NO_WARNING (ref
) = 1;
6596 /* Searches if the expr T, located at LOCATION computes
6597 address of an ARRAY_REF, and call check_array_ref on it. */
6600 search_for_addr_array (tree t
, location_t location
)
6602 /* Check each ARRAY_REFs in the reference chain. */
6605 if (TREE_CODE (t
) == ARRAY_REF
)
6606 check_array_ref (location
, t
, true /*ignore_off_by_one*/);
6608 t
= TREE_OPERAND (t
, 0);
6610 while (handled_component_p (t
));
6612 if (TREE_CODE (t
) == MEM_REF
6613 && TREE_CODE (TREE_OPERAND (t
, 0)) == ADDR_EXPR
6614 && !TREE_NO_WARNING (t
))
6616 tree tem
= TREE_OPERAND (TREE_OPERAND (t
, 0), 0);
6617 tree low_bound
, up_bound
, el_sz
;
6619 if (TREE_CODE (TREE_TYPE (tem
)) != ARRAY_TYPE
6620 || TREE_CODE (TREE_TYPE (TREE_TYPE (tem
))) == ARRAY_TYPE
6621 || !TYPE_DOMAIN (TREE_TYPE (tem
)))
6624 low_bound
= TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (tem
)));
6625 up_bound
= TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (tem
)));
6626 el_sz
= TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (tem
)));
6628 || TREE_CODE (low_bound
) != INTEGER_CST
6630 || TREE_CODE (up_bound
) != INTEGER_CST
6632 || TREE_CODE (el_sz
) != INTEGER_CST
)
6635 idx
= mem_ref_offset (t
);
6636 idx
= wi::sdiv_trunc (idx
, wi::to_offset (el_sz
));
6637 if (wi::lts_p (idx
, 0))
6639 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6641 fprintf (dump_file
, "Array bound warning for ");
6642 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, t
);
6643 fprintf (dump_file
, "\n");
6645 warning_at (location
, OPT_Warray_bounds
,
6646 "array subscript is below array bounds");
6647 TREE_NO_WARNING (t
) = 1;
6649 else if (wi::gts_p (idx
, (wi::to_offset (up_bound
)
6650 - wi::to_offset (low_bound
) + 1)))
6652 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6654 fprintf (dump_file
, "Array bound warning for ");
6655 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, t
);
6656 fprintf (dump_file
, "\n");
6658 warning_at (location
, OPT_Warray_bounds
,
6659 "array subscript is above array bounds");
6660 TREE_NO_WARNING (t
) = 1;
6665 /* walk_tree() callback that checks if *TP is
6666 an ARRAY_REF inside an ADDR_EXPR (in which an array
6667 subscript one outside the valid range is allowed). Call
6668 check_array_ref for each ARRAY_REF found. The location is
6672 check_array_bounds (tree
*tp
, int *walk_subtree
, void *data
)
6675 struct walk_stmt_info
*wi
= (struct walk_stmt_info
*) data
;
6676 location_t location
;
6678 if (EXPR_HAS_LOCATION (t
))
6679 location
= EXPR_LOCATION (t
);
6682 location_t
*locp
= (location_t
*) wi
->info
;
6686 *walk_subtree
= TRUE
;
6688 if (TREE_CODE (t
) == ARRAY_REF
)
6689 check_array_ref (location
, t
, false /*ignore_off_by_one*/);
6691 else if (TREE_CODE (t
) == ADDR_EXPR
)
6693 search_for_addr_array (t
, location
);
6694 *walk_subtree
= FALSE
;
6700 /* Walk over all statements of all reachable BBs and call check_array_bounds
6704 check_all_array_refs (void)
6707 gimple_stmt_iterator si
;
6709 FOR_EACH_BB_FN (bb
, cfun
)
6713 bool executable
= false;
6715 /* Skip blocks that were found to be unreachable. */
6716 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
6717 executable
|= !!(e
->flags
& EDGE_EXECUTABLE
);
6721 for (si
= gsi_start_bb (bb
); !gsi_end_p (si
); gsi_next (&si
))
6723 gimple stmt
= gsi_stmt (si
);
6724 struct walk_stmt_info wi
;
6725 if (!gimple_has_location (stmt
)
6726 || is_gimple_debug (stmt
))
6729 memset (&wi
, 0, sizeof (wi
));
6730 wi
.info
= CONST_CAST (void *, (const void *)
6731 gimple_location_ptr (stmt
));
6733 walk_gimple_op (gsi_stmt (si
),
6740 /* Return true if all imm uses of VAR are either in STMT, or
6741 feed (optionally through a chain of single imm uses) GIMPLE_COND
6742 in basic block COND_BB. */
6745 all_imm_uses_in_stmt_or_feed_cond (tree var
, gimple stmt
, basic_block cond_bb
)
6747 use_operand_p use_p
, use2_p
;
6748 imm_use_iterator iter
;
6750 FOR_EACH_IMM_USE_FAST (use_p
, iter
, var
)
6751 if (USE_STMT (use_p
) != stmt
)
6753 gimple use_stmt
= USE_STMT (use_p
), use_stmt2
;
6754 if (is_gimple_debug (use_stmt
))
6756 while (is_gimple_assign (use_stmt
)
6757 && TREE_CODE (gimple_assign_lhs (use_stmt
)) == SSA_NAME
6758 && single_imm_use (gimple_assign_lhs (use_stmt
),
6759 &use2_p
, &use_stmt2
))
6760 use_stmt
= use_stmt2
;
6761 if (gimple_code (use_stmt
) != GIMPLE_COND
6762 || gimple_bb (use_stmt
) != cond_bb
)
6775 __builtin_unreachable ();
6777 x_5 = ASSERT_EXPR <x_3, ...>;
6778 If x_3 has no other immediate uses (checked by caller),
6779 var is the x_3 var from ASSERT_EXPR, we can clear low 5 bits
6780 from the non-zero bitmask. */
6783 maybe_set_nonzero_bits (basic_block bb
, tree var
)
6785 edge e
= single_pred_edge (bb
);
6786 basic_block cond_bb
= e
->src
;
6787 gimple stmt
= last_stmt (cond_bb
);
6791 || gimple_code (stmt
) != GIMPLE_COND
6792 || gimple_cond_code (stmt
) != ((e
->flags
& EDGE_TRUE_VALUE
)
6793 ? EQ_EXPR
: NE_EXPR
)
6794 || TREE_CODE (gimple_cond_lhs (stmt
)) != SSA_NAME
6795 || !integer_zerop (gimple_cond_rhs (stmt
)))
6798 stmt
= SSA_NAME_DEF_STMT (gimple_cond_lhs (stmt
));
6799 if (!is_gimple_assign (stmt
)
6800 || gimple_assign_rhs_code (stmt
) != BIT_AND_EXPR
6801 || TREE_CODE (gimple_assign_rhs2 (stmt
)) != INTEGER_CST
)
6803 if (gimple_assign_rhs1 (stmt
) != var
)
6807 if (TREE_CODE (gimple_assign_rhs1 (stmt
)) != SSA_NAME
)
6809 stmt2
= SSA_NAME_DEF_STMT (gimple_assign_rhs1 (stmt
));
6810 if (!gimple_assign_cast_p (stmt2
)
6811 || gimple_assign_rhs1 (stmt2
) != var
6812 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (stmt2
))
6813 || (TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (stmt
)))
6814 != TYPE_PRECISION (TREE_TYPE (var
))))
6817 cst
= gimple_assign_rhs2 (stmt
);
6818 set_nonzero_bits (var
, wi::bit_and_not (get_nonzero_bits (var
), cst
));
6821 /* Convert range assertion expressions into the implied copies and
6822 copy propagate away the copies. Doing the trivial copy propagation
6823 here avoids the need to run the full copy propagation pass after
6826 FIXME, this will eventually lead to copy propagation removing the
6827 names that had useful range information attached to them. For
6828 instance, if we had the assertion N_i = ASSERT_EXPR <N_j, N_j > 3>,
6829 then N_i will have the range [3, +INF].
6831 However, by converting the assertion into the implied copy
6832 operation N_i = N_j, we will then copy-propagate N_j into the uses
6833 of N_i and lose the range information. We may want to hold on to
6834 ASSERT_EXPRs a little while longer as the ranges could be used in
6835 things like jump threading.
6837 The problem with keeping ASSERT_EXPRs around is that passes after
6838 VRP need to handle them appropriately.
6840 Another approach would be to make the range information a first
6841 class property of the SSA_NAME so that it can be queried from
6842 any pass. This is made somewhat more complex by the need for
6843 multiple ranges to be associated with one SSA_NAME. */
6846 remove_range_assertions (void)
6849 gimple_stmt_iterator si
;
6850 /* 1 if looking at ASSERT_EXPRs immediately at the beginning of
6851 a basic block preceeded by GIMPLE_COND branching to it and
6852 __builtin_trap, -1 if not yet checked, 0 otherwise. */
6855 /* Note that the BSI iterator bump happens at the bottom of the
6856 loop and no bump is necessary if we're removing the statement
6857 referenced by the current BSI. */
6858 FOR_EACH_BB_FN (bb
, cfun
)
6859 for (si
= gsi_after_labels (bb
), is_unreachable
= -1; !gsi_end_p (si
);)
6861 gimple stmt
= gsi_stmt (si
);
6864 if (is_gimple_assign (stmt
)
6865 && gimple_assign_rhs_code (stmt
) == ASSERT_EXPR
)
6867 tree lhs
= gimple_assign_lhs (stmt
);
6868 tree rhs
= gimple_assign_rhs1 (stmt
);
6870 tree cond
= fold (ASSERT_EXPR_COND (rhs
));
6871 use_operand_p use_p
;
6872 imm_use_iterator iter
;
6874 gcc_assert (cond
!= boolean_false_node
);
6876 var
= ASSERT_EXPR_VAR (rhs
);
6877 gcc_assert (TREE_CODE (var
) == SSA_NAME
);
6879 if (!POINTER_TYPE_P (TREE_TYPE (lhs
))
6880 && SSA_NAME_RANGE_INFO (lhs
))
6882 if (is_unreachable
== -1)
6885 if (single_pred_p (bb
)
6886 && assert_unreachable_fallthru_edge_p
6887 (single_pred_edge (bb
)))
6891 if (x_7 >= 10 && x_7 < 20)
6892 __builtin_unreachable ();
6893 x_8 = ASSERT_EXPR <x_7, ...>;
6894 if the only uses of x_7 are in the ASSERT_EXPR and
6895 in the condition. In that case, we can copy the
6896 range info from x_8 computed in this pass also
6899 && all_imm_uses_in_stmt_or_feed_cond (var
, stmt
,
6902 set_range_info (var
, SSA_NAME_RANGE_TYPE (lhs
),
6903 SSA_NAME_RANGE_INFO (lhs
)->get_min (),
6904 SSA_NAME_RANGE_INFO (lhs
)->get_max ());
6905 maybe_set_nonzero_bits (bb
, var
);
6909 /* Propagate the RHS into every use of the LHS. */
6910 FOR_EACH_IMM_USE_STMT (use_stmt
, iter
, lhs
)
6911 FOR_EACH_IMM_USE_ON_STMT (use_p
, iter
)
6912 SET_USE (use_p
, var
);
6914 /* And finally, remove the copy, it is not needed. */
6915 gsi_remove (&si
, true);
6916 release_defs (stmt
);
6920 if (!is_gimple_debug (gsi_stmt (si
)))
6928 /* Return true if STMT is interesting for VRP. */
6931 stmt_interesting_for_vrp (gimple stmt
)
6933 if (gimple_code (stmt
) == GIMPLE_PHI
)
6935 tree res
= gimple_phi_result (stmt
);
6936 return (!virtual_operand_p (res
)
6937 && (INTEGRAL_TYPE_P (TREE_TYPE (res
))
6938 || POINTER_TYPE_P (TREE_TYPE (res
))));
6940 else if (is_gimple_assign (stmt
) || is_gimple_call (stmt
))
6942 tree lhs
= gimple_get_lhs (stmt
);
6944 /* In general, assignments with virtual operands are not useful
6945 for deriving ranges, with the obvious exception of calls to
6946 builtin functions. */
6947 if (lhs
&& TREE_CODE (lhs
) == SSA_NAME
6948 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs
))
6949 || POINTER_TYPE_P (TREE_TYPE (lhs
)))
6950 && (is_gimple_call (stmt
)
6951 || !gimple_vuse (stmt
)))
6953 else if (is_gimple_call (stmt
) && gimple_call_internal_p (stmt
))
6954 switch (gimple_call_internal_fn (stmt
))
6956 case IFN_ADD_OVERFLOW
:
6957 case IFN_SUB_OVERFLOW
:
6958 case IFN_MUL_OVERFLOW
:
6959 /* These internal calls return _Complex integer type,
6960 but are interesting to VRP nevertheless. */
6961 if (lhs
&& TREE_CODE (lhs
) == SSA_NAME
)
6968 else if (gimple_code (stmt
) == GIMPLE_COND
6969 || gimple_code (stmt
) == GIMPLE_SWITCH
)
6976 /* Initialize local data structures for VRP. */
6979 vrp_initialize (void)
6983 values_propagated
= false;
6984 num_vr_values
= num_ssa_names
;
6985 vr_value
= XCNEWVEC (value_range_t
*, num_vr_values
);
6986 vr_phi_edge_counts
= XCNEWVEC (int, num_ssa_names
);
6988 FOR_EACH_BB_FN (bb
, cfun
)
6990 for (gphi_iterator si
= gsi_start_phis (bb
); !gsi_end_p (si
);
6993 gphi
*phi
= si
.phi ();
6994 if (!stmt_interesting_for_vrp (phi
))
6996 tree lhs
= PHI_RESULT (phi
);
6997 set_value_range_to_varying (get_value_range (lhs
));
6998 prop_set_simulate_again (phi
, false);
7001 prop_set_simulate_again (phi
, true);
7004 for (gimple_stmt_iterator si
= gsi_start_bb (bb
); !gsi_end_p (si
);
7007 gimple stmt
= gsi_stmt (si
);
7009 /* If the statement is a control insn, then we do not
7010 want to avoid simulating the statement once. Failure
7011 to do so means that those edges will never get added. */
7012 if (stmt_ends_bb_p (stmt
))
7013 prop_set_simulate_again (stmt
, true);
7014 else if (!stmt_interesting_for_vrp (stmt
))
7018 FOR_EACH_SSA_TREE_OPERAND (def
, stmt
, i
, SSA_OP_DEF
)
7019 set_value_range_to_varying (get_value_range (def
));
7020 prop_set_simulate_again (stmt
, false);
7023 prop_set_simulate_again (stmt
, true);
7028 /* Return the singleton value-range for NAME or NAME. */
7031 vrp_valueize (tree name
)
7033 if (TREE_CODE (name
) == SSA_NAME
)
7035 value_range_t
*vr
= get_value_range (name
);
7036 if (vr
->type
== VR_RANGE
7037 && (vr
->min
== vr
->max
7038 || operand_equal_p (vr
->min
, vr
->max
, 0)))
7044 /* Return the singleton value-range for NAME if that is a constant
7045 but signal to not follow SSA edges. */
7048 vrp_valueize_1 (tree name
)
7050 if (TREE_CODE (name
) == SSA_NAME
)
7052 /* If the definition may be simulated again we cannot follow
7053 this SSA edge as the SSA propagator does not necessarily
7054 re-visit the use. */
7055 gimple def_stmt
= SSA_NAME_DEF_STMT (name
);
7056 if (!gimple_nop_p (def_stmt
)
7057 && prop_simulate_again_p (def_stmt
))
7059 value_range_t
*vr
= get_value_range (name
);
7060 if (range_int_cst_singleton_p (vr
))
7066 /* Visit assignment STMT. If it produces an interesting range, record
7067 the SSA name in *OUTPUT_P. */
7069 static enum ssa_prop_result
7070 vrp_visit_assignment_or_call (gimple stmt
, tree
*output_p
)
7074 enum gimple_code code
= gimple_code (stmt
);
7075 lhs
= gimple_get_lhs (stmt
);
7077 /* We only keep track of ranges in integral and pointer types. */
7078 if (TREE_CODE (lhs
) == SSA_NAME
7079 && ((INTEGRAL_TYPE_P (TREE_TYPE (lhs
))
7080 /* It is valid to have NULL MIN/MAX values on a type. See
7081 build_range_type. */
7082 && TYPE_MIN_VALUE (TREE_TYPE (lhs
))
7083 && TYPE_MAX_VALUE (TREE_TYPE (lhs
)))
7084 || POINTER_TYPE_P (TREE_TYPE (lhs
))))
7086 value_range_t new_vr
= VR_INITIALIZER
;
7088 /* Try folding the statement to a constant first. */
7089 tree tem
= gimple_fold_stmt_to_constant_1 (stmt
, vrp_valueize
,
7091 if (tem
&& is_gimple_min_invariant (tem
))
7092 set_value_range_to_value (&new_vr
, tem
, NULL
);
7093 /* Then dispatch to value-range extracting functions. */
7094 else if (code
== GIMPLE_CALL
)
7095 extract_range_basic (&new_vr
, stmt
);
7097 extract_range_from_assignment (&new_vr
, as_a
<gassign
*> (stmt
));
7099 if (update_value_range (lhs
, &new_vr
))
7103 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7105 fprintf (dump_file
, "Found new range for ");
7106 print_generic_expr (dump_file
, lhs
, 0);
7107 fprintf (dump_file
, ": ");
7108 dump_value_range (dump_file
, &new_vr
);
7109 fprintf (dump_file
, "\n");
7112 if (new_vr
.type
== VR_VARYING
)
7113 return SSA_PROP_VARYING
;
7115 return SSA_PROP_INTERESTING
;
7118 return SSA_PROP_NOT_INTERESTING
;
7120 else if (is_gimple_call (stmt
) && gimple_call_internal_p (stmt
))
7121 switch (gimple_call_internal_fn (stmt
))
7123 case IFN_ADD_OVERFLOW
:
7124 case IFN_SUB_OVERFLOW
:
7125 case IFN_MUL_OVERFLOW
:
7126 /* These internal calls return _Complex integer type,
7127 which VRP does not track, but the immediate uses
7128 thereof might be interesting. */
7129 if (lhs
&& TREE_CODE (lhs
) == SSA_NAME
)
7131 imm_use_iterator iter
;
7132 use_operand_p use_p
;
7133 enum ssa_prop_result res
= SSA_PROP_VARYING
;
7135 set_value_range_to_varying (get_value_range (lhs
));
7137 FOR_EACH_IMM_USE_FAST (use_p
, iter
, lhs
)
7139 gimple use_stmt
= USE_STMT (use_p
);
7140 if (!is_gimple_assign (use_stmt
))
7142 enum tree_code rhs_code
= gimple_assign_rhs_code (use_stmt
);
7143 if (rhs_code
!= REALPART_EXPR
&& rhs_code
!= IMAGPART_EXPR
)
7145 tree rhs1
= gimple_assign_rhs1 (use_stmt
);
7146 tree use_lhs
= gimple_assign_lhs (use_stmt
);
7147 if (TREE_CODE (rhs1
) != rhs_code
7148 || TREE_OPERAND (rhs1
, 0) != lhs
7149 || TREE_CODE (use_lhs
) != SSA_NAME
7150 || !stmt_interesting_for_vrp (use_stmt
)
7151 || (!INTEGRAL_TYPE_P (TREE_TYPE (use_lhs
))
7152 || !TYPE_MIN_VALUE (TREE_TYPE (use_lhs
))
7153 || !TYPE_MAX_VALUE (TREE_TYPE (use_lhs
))))
7156 /* If there is a change in the value range for any of the
7157 REALPART_EXPR/IMAGPART_EXPR immediate uses, return
7158 SSA_PROP_INTERESTING. If there are any REALPART_EXPR
7159 or IMAGPART_EXPR immediate uses, but none of them have
7160 a change in their value ranges, return
7161 SSA_PROP_NOT_INTERESTING. If there are no
7162 {REAL,IMAG}PART_EXPR uses at all,
7163 return SSA_PROP_VARYING. */
7164 value_range_t new_vr
= VR_INITIALIZER
;
7165 extract_range_basic (&new_vr
, use_stmt
);
7166 value_range_t
*old_vr
= get_value_range (use_lhs
);
7167 if (old_vr
->type
!= new_vr
.type
7168 || !vrp_operand_equal_p (old_vr
->min
, new_vr
.min
)
7169 || !vrp_operand_equal_p (old_vr
->max
, new_vr
.max
)
7170 || !vrp_bitmap_equal_p (old_vr
->equiv
, new_vr
.equiv
))
7171 res
= SSA_PROP_INTERESTING
;
7173 res
= SSA_PROP_NOT_INTERESTING
;
7174 BITMAP_FREE (new_vr
.equiv
);
7175 if (res
== SSA_PROP_INTERESTING
)
7189 /* Every other statement produces no useful ranges. */
7190 FOR_EACH_SSA_TREE_OPERAND (def
, stmt
, iter
, SSA_OP_DEF
)
7191 set_value_range_to_varying (get_value_range (def
));
7193 return SSA_PROP_VARYING
;
7196 /* Helper that gets the value range of the SSA_NAME with version I
7197 or a symbolic range containing the SSA_NAME only if the value range
7198 is varying or undefined. */
7200 static inline value_range_t
7201 get_vr_for_comparison (int i
)
7203 value_range_t vr
= *get_value_range (ssa_name (i
));
7205 /* If name N_i does not have a valid range, use N_i as its own
7206 range. This allows us to compare against names that may
7207 have N_i in their ranges. */
7208 if (vr
.type
== VR_VARYING
|| vr
.type
== VR_UNDEFINED
)
7211 vr
.min
= ssa_name (i
);
7212 vr
.max
= ssa_name (i
);
7218 /* Compare all the value ranges for names equivalent to VAR with VAL
7219 using comparison code COMP. Return the same value returned by
7220 compare_range_with_value, including the setting of
7221 *STRICT_OVERFLOW_P. */
7224 compare_name_with_value (enum tree_code comp
, tree var
, tree val
,
7225 bool *strict_overflow_p
)
7231 int used_strict_overflow
;
7233 value_range_t equiv_vr
;
7235 /* Get the set of equivalences for VAR. */
7236 e
= get_value_range (var
)->equiv
;
7238 /* Start at -1. Set it to 0 if we do a comparison without relying
7239 on overflow, or 1 if all comparisons rely on overflow. */
7240 used_strict_overflow
= -1;
7242 /* Compare vars' value range with val. */
7243 equiv_vr
= get_vr_for_comparison (SSA_NAME_VERSION (var
));
7245 retval
= compare_range_with_value (comp
, &equiv_vr
, val
, &sop
);
7247 used_strict_overflow
= sop
? 1 : 0;
7249 /* If the equiv set is empty we have done all work we need to do. */
7253 && used_strict_overflow
> 0)
7254 *strict_overflow_p
= true;
7258 EXECUTE_IF_SET_IN_BITMAP (e
, 0, i
, bi
)
7260 equiv_vr
= get_vr_for_comparison (i
);
7262 t
= compare_range_with_value (comp
, &equiv_vr
, val
, &sop
);
7265 /* If we get different answers from different members
7266 of the equivalence set this check must be in a dead
7267 code region. Folding it to a trap representation
7268 would be correct here. For now just return don't-know. */
7278 used_strict_overflow
= 0;
7279 else if (used_strict_overflow
< 0)
7280 used_strict_overflow
= 1;
7285 && used_strict_overflow
> 0)
7286 *strict_overflow_p
= true;
7292 /* Given a comparison code COMP and names N1 and N2, compare all the
7293 ranges equivalent to N1 against all the ranges equivalent to N2
7294 to determine the value of N1 COMP N2. Return the same value
7295 returned by compare_ranges. Set *STRICT_OVERFLOW_P to indicate
7296 whether we relied on an overflow infinity in the comparison. */
7300 compare_names (enum tree_code comp
, tree n1
, tree n2
,
7301 bool *strict_overflow_p
)
7305 bitmap_iterator bi1
, bi2
;
7307 int used_strict_overflow
;
7308 static bitmap_obstack
*s_obstack
= NULL
;
7309 static bitmap s_e1
= NULL
, s_e2
= NULL
;
7311 /* Compare the ranges of every name equivalent to N1 against the
7312 ranges of every name equivalent to N2. */
7313 e1
= get_value_range (n1
)->equiv
;
7314 e2
= get_value_range (n2
)->equiv
;
7316 /* Use the fake bitmaps if e1 or e2 are not available. */
7317 if (s_obstack
== NULL
)
7319 s_obstack
= XNEW (bitmap_obstack
);
7320 bitmap_obstack_initialize (s_obstack
);
7321 s_e1
= BITMAP_ALLOC (s_obstack
);
7322 s_e2
= BITMAP_ALLOC (s_obstack
);
7329 /* Add N1 and N2 to their own set of equivalences to avoid
7330 duplicating the body of the loop just to check N1 and N2
7332 bitmap_set_bit (e1
, SSA_NAME_VERSION (n1
));
7333 bitmap_set_bit (e2
, SSA_NAME_VERSION (n2
));
7335 /* If the equivalence sets have a common intersection, then the two
7336 names can be compared without checking their ranges. */
7337 if (bitmap_intersect_p (e1
, e2
))
7339 bitmap_clear_bit (e1
, SSA_NAME_VERSION (n1
));
7340 bitmap_clear_bit (e2
, SSA_NAME_VERSION (n2
));
7342 return (comp
== EQ_EXPR
|| comp
== GE_EXPR
|| comp
== LE_EXPR
)
7344 : boolean_false_node
;
7347 /* Start at -1. Set it to 0 if we do a comparison without relying
7348 on overflow, or 1 if all comparisons rely on overflow. */
7349 used_strict_overflow
= -1;
7351 /* Otherwise, compare all the equivalent ranges. First, add N1 and
7352 N2 to their own set of equivalences to avoid duplicating the body
7353 of the loop just to check N1 and N2 ranges. */
7354 EXECUTE_IF_SET_IN_BITMAP (e1
, 0, i1
, bi1
)
7356 value_range_t vr1
= get_vr_for_comparison (i1
);
7358 t
= retval
= NULL_TREE
;
7359 EXECUTE_IF_SET_IN_BITMAP (e2
, 0, i2
, bi2
)
7363 value_range_t vr2
= get_vr_for_comparison (i2
);
7365 t
= compare_ranges (comp
, &vr1
, &vr2
, &sop
);
7368 /* If we get different answers from different members
7369 of the equivalence set this check must be in a dead
7370 code region. Folding it to a trap representation
7371 would be correct here. For now just return don't-know. */
7375 bitmap_clear_bit (e1
, SSA_NAME_VERSION (n1
));
7376 bitmap_clear_bit (e2
, SSA_NAME_VERSION (n2
));
7382 used_strict_overflow
= 0;
7383 else if (used_strict_overflow
< 0)
7384 used_strict_overflow
= 1;
7390 bitmap_clear_bit (e1
, SSA_NAME_VERSION (n1
));
7391 bitmap_clear_bit (e2
, SSA_NAME_VERSION (n2
));
7392 if (used_strict_overflow
> 0)
7393 *strict_overflow_p
= true;
7398 /* None of the equivalent ranges are useful in computing this
7400 bitmap_clear_bit (e1
, SSA_NAME_VERSION (n1
));
7401 bitmap_clear_bit (e2
, SSA_NAME_VERSION (n2
));
7405 /* Helper function for vrp_evaluate_conditional_warnv & other
7409 vrp_evaluate_conditional_warnv_with_ops_using_ranges (enum tree_code code
,
7411 bool * strict_overflow_p
)
7413 value_range_t
*vr0
, *vr1
;
7415 vr0
= (TREE_CODE (op0
) == SSA_NAME
) ? get_value_range (op0
) : NULL
;
7416 vr1
= (TREE_CODE (op1
) == SSA_NAME
) ? get_value_range (op1
) : NULL
;
7418 tree res
= NULL_TREE
;
7420 res
= compare_ranges (code
, vr0
, vr1
, strict_overflow_p
);
7422 res
= compare_range_with_value (code
, vr0
, op1
, strict_overflow_p
);
7424 res
= (compare_range_with_value
7425 (swap_tree_comparison (code
), vr1
, op0
, strict_overflow_p
));
7429 /* Helper function for vrp_evaluate_conditional_warnv. */
7432 vrp_evaluate_conditional_warnv_with_ops (enum tree_code code
, tree op0
,
7433 tree op1
, bool use_equiv_p
,
7434 bool *strict_overflow_p
, bool *only_ranges
)
7438 *only_ranges
= true;
7440 /* We only deal with integral and pointer types. */
7441 if (!INTEGRAL_TYPE_P (TREE_TYPE (op0
))
7442 && !POINTER_TYPE_P (TREE_TYPE (op0
)))
7448 && (ret
= vrp_evaluate_conditional_warnv_with_ops_using_ranges
7449 (code
, op0
, op1
, strict_overflow_p
)))
7451 *only_ranges
= false;
7452 if (TREE_CODE (op0
) == SSA_NAME
&& TREE_CODE (op1
) == SSA_NAME
)
7453 return compare_names (code
, op0
, op1
, strict_overflow_p
);
7454 else if (TREE_CODE (op0
) == SSA_NAME
)
7455 return compare_name_with_value (code
, op0
, op1
, strict_overflow_p
);
7456 else if (TREE_CODE (op1
) == SSA_NAME
)
7457 return (compare_name_with_value
7458 (swap_tree_comparison (code
), op1
, op0
, strict_overflow_p
));
7461 return vrp_evaluate_conditional_warnv_with_ops_using_ranges (code
, op0
, op1
,
7466 /* Given (CODE OP0 OP1) within STMT, try to simplify it based on value range
7467 information. Return NULL if the conditional can not be evaluated.
7468 The ranges of all the names equivalent with the operands in COND
7469 will be used when trying to compute the value. If the result is
7470 based on undefined signed overflow, issue a warning if
7474 vrp_evaluate_conditional (enum tree_code code
, tree op0
, tree op1
, gimple stmt
)
7480 /* Some passes and foldings leak constants with overflow flag set
7481 into the IL. Avoid doing wrong things with these and bail out. */
7482 if ((TREE_CODE (op0
) == INTEGER_CST
7483 && TREE_OVERFLOW (op0
))
7484 || (TREE_CODE (op1
) == INTEGER_CST
7485 && TREE_OVERFLOW (op1
)))
7489 ret
= vrp_evaluate_conditional_warnv_with_ops (code
, op0
, op1
, true, &sop
,
7494 enum warn_strict_overflow_code wc
;
7495 const char* warnmsg
;
7497 if (is_gimple_min_invariant (ret
))
7499 wc
= WARN_STRICT_OVERFLOW_CONDITIONAL
;
7500 warnmsg
= G_("assuming signed overflow does not occur when "
7501 "simplifying conditional to constant");
7505 wc
= WARN_STRICT_OVERFLOW_COMPARISON
;
7506 warnmsg
= G_("assuming signed overflow does not occur when "
7507 "simplifying conditional");
7510 if (issue_strict_overflow_warning (wc
))
7512 location_t location
;
7514 if (!gimple_has_location (stmt
))
7515 location
= input_location
;
7517 location
= gimple_location (stmt
);
7518 warning_at (location
, OPT_Wstrict_overflow
, "%s", warnmsg
);
7522 if (warn_type_limits
7523 && ret
&& only_ranges
7524 && TREE_CODE_CLASS (code
) == tcc_comparison
7525 && TREE_CODE (op0
) == SSA_NAME
)
7527 /* If the comparison is being folded and the operand on the LHS
7528 is being compared against a constant value that is outside of
7529 the natural range of OP0's type, then the predicate will
7530 always fold regardless of the value of OP0. If -Wtype-limits
7531 was specified, emit a warning. */
7532 tree type
= TREE_TYPE (op0
);
7533 value_range_t
*vr0
= get_value_range (op0
);
7535 if (vr0
->type
== VR_RANGE
7536 && INTEGRAL_TYPE_P (type
)
7537 && vrp_val_is_min (vr0
->min
)
7538 && vrp_val_is_max (vr0
->max
)
7539 && is_gimple_min_invariant (op1
))
7541 location_t location
;
7543 if (!gimple_has_location (stmt
))
7544 location
= input_location
;
7546 location
= gimple_location (stmt
);
7548 warning_at (location
, OPT_Wtype_limits
,
7550 ? G_("comparison always false "
7551 "due to limited range of data type")
7552 : G_("comparison always true "
7553 "due to limited range of data type"));
7561 /* Visit conditional statement STMT. If we can determine which edge
7562 will be taken out of STMT's basic block, record it in
7563 *TAKEN_EDGE_P and return SSA_PROP_INTERESTING. Otherwise, return
7564 SSA_PROP_VARYING. */
7566 static enum ssa_prop_result
7567 vrp_visit_cond_stmt (gcond
*stmt
, edge
*taken_edge_p
)
7572 *taken_edge_p
= NULL
;
7574 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7579 fprintf (dump_file
, "\nVisiting conditional with predicate: ");
7580 print_gimple_stmt (dump_file
, stmt
, 0, 0);
7581 fprintf (dump_file
, "\nWith known ranges\n");
7583 FOR_EACH_SSA_TREE_OPERAND (use
, stmt
, i
, SSA_OP_USE
)
7585 fprintf (dump_file
, "\t");
7586 print_generic_expr (dump_file
, use
, 0);
7587 fprintf (dump_file
, ": ");
7588 dump_value_range (dump_file
, vr_value
[SSA_NAME_VERSION (use
)]);
7591 fprintf (dump_file
, "\n");
7594 /* Compute the value of the predicate COND by checking the known
7595 ranges of each of its operands.
7597 Note that we cannot evaluate all the equivalent ranges here
7598 because those ranges may not yet be final and with the current
7599 propagation strategy, we cannot determine when the value ranges
7600 of the names in the equivalence set have changed.
7602 For instance, given the following code fragment
7606 i_14 = ASSERT_EXPR <i_5, i_5 != 0>
7610 Assume that on the first visit to i_14, i_5 has the temporary
7611 range [8, 8] because the second argument to the PHI function is
7612 not yet executable. We derive the range ~[0, 0] for i_14 and the
7613 equivalence set { i_5 }. So, when we visit 'if (i_14 == 1)' for
7614 the first time, since i_14 is equivalent to the range [8, 8], we
7615 determine that the predicate is always false.
7617 On the next round of propagation, i_13 is determined to be
7618 VARYING, which causes i_5 to drop down to VARYING. So, another
7619 visit to i_14 is scheduled. In this second visit, we compute the
7620 exact same range and equivalence set for i_14, namely ~[0, 0] and
7621 { i_5 }. But we did not have the previous range for i_5
7622 registered, so vrp_visit_assignment thinks that the range for
7623 i_14 has not changed. Therefore, the predicate 'if (i_14 == 1)'
7624 is not visited again, which stops propagation from visiting
7625 statements in the THEN clause of that if().
7627 To properly fix this we would need to keep the previous range
7628 value for the names in the equivalence set. This way we would've
7629 discovered that from one visit to the other i_5 changed from
7630 range [8, 8] to VR_VARYING.
7632 However, fixing this apparent limitation may not be worth the
7633 additional checking. Testing on several code bases (GCC, DLV,
7634 MICO, TRAMP3D and SPEC2000) showed that doing this results in
7635 4 more predicates folded in SPEC. */
7638 val
= vrp_evaluate_conditional_warnv_with_ops (gimple_cond_code (stmt
),
7639 gimple_cond_lhs (stmt
),
7640 gimple_cond_rhs (stmt
),
7645 *taken_edge_p
= find_taken_edge (gimple_bb (stmt
), val
);
7648 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7650 "\nIgnoring predicate evaluation because "
7651 "it assumes that signed overflow is undefined");
7656 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7658 fprintf (dump_file
, "\nPredicate evaluates to: ");
7659 if (val
== NULL_TREE
)
7660 fprintf (dump_file
, "DON'T KNOW\n");
7662 print_generic_stmt (dump_file
, val
, 0);
7665 return (*taken_edge_p
) ? SSA_PROP_INTERESTING
: SSA_PROP_VARYING
;
7668 /* Searches the case label vector VEC for the index *IDX of the CASE_LABEL
7669 that includes the value VAL. The search is restricted to the range
7670 [START_IDX, n - 1] where n is the size of VEC.
7672 If there is a CASE_LABEL for VAL, its index is placed in IDX and true is
7675 If there is no CASE_LABEL for VAL and there is one that is larger than VAL,
7676 it is placed in IDX and false is returned.
7678 If VAL is larger than any CASE_LABEL, n is placed on IDX and false is
7682 find_case_label_index (gswitch
*stmt
, size_t start_idx
, tree val
, size_t *idx
)
7684 size_t n
= gimple_switch_num_labels (stmt
);
7687 /* Find case label for minimum of the value range or the next one.
7688 At each iteration we are searching in [low, high - 1]. */
7690 for (low
= start_idx
, high
= n
; high
!= low
; )
7694 /* Note that i != high, so we never ask for n. */
7695 size_t i
= (high
+ low
) / 2;
7696 t
= gimple_switch_label (stmt
, i
);
7698 /* Cache the result of comparing CASE_LOW and val. */
7699 cmp
= tree_int_cst_compare (CASE_LOW (t
), val
);
7703 /* Ranges cannot be empty. */
7712 if (CASE_HIGH (t
) != NULL
7713 && tree_int_cst_compare (CASE_HIGH (t
), val
) >= 0)
7725 /* Searches the case label vector VEC for the range of CASE_LABELs that is used
7726 for values between MIN and MAX. The first index is placed in MIN_IDX. The
7727 last index is placed in MAX_IDX. If the range of CASE_LABELs is empty
7728 then MAX_IDX < MIN_IDX.
7729 Returns true if the default label is not needed. */
7732 find_case_label_range (gswitch
*stmt
, tree min
, tree max
, size_t *min_idx
,
7736 bool min_take_default
= !find_case_label_index (stmt
, 1, min
, &i
);
7737 bool max_take_default
= !find_case_label_index (stmt
, i
, max
, &j
);
7741 && max_take_default
)
7743 /* Only the default case label reached.
7744 Return an empty range. */
7751 bool take_default
= min_take_default
|| max_take_default
;
7755 if (max_take_default
)
7758 /* If the case label range is continuous, we do not need
7759 the default case label. Verify that. */
7760 high
= CASE_LOW (gimple_switch_label (stmt
, i
));
7761 if (CASE_HIGH (gimple_switch_label (stmt
, i
)))
7762 high
= CASE_HIGH (gimple_switch_label (stmt
, i
));
7763 for (k
= i
+ 1; k
<= j
; ++k
)
7765 low
= CASE_LOW (gimple_switch_label (stmt
, k
));
7766 if (!integer_onep (int_const_binop (MINUS_EXPR
, low
, high
)))
7768 take_default
= true;
7772 if (CASE_HIGH (gimple_switch_label (stmt
, k
)))
7773 high
= CASE_HIGH (gimple_switch_label (stmt
, k
));
7778 return !take_default
;
7782 /* Searches the case label vector VEC for the ranges of CASE_LABELs that are
7783 used in range VR. The indices are placed in MIN_IDX1, MAX_IDX, MIN_IDX2 and
7784 MAX_IDX2. If the ranges of CASE_LABELs are empty then MAX_IDX1 < MIN_IDX1.
7785 Returns true if the default label is not needed. */
7788 find_case_label_ranges (gswitch
*stmt
, value_range_t
*vr
, size_t *min_idx1
,
7789 size_t *max_idx1
, size_t *min_idx2
,
7793 unsigned int n
= gimple_switch_num_labels (stmt
);
7795 tree case_low
, case_high
;
7796 tree min
= vr
->min
, max
= vr
->max
;
7798 gcc_checking_assert (vr
->type
== VR_RANGE
|| vr
->type
== VR_ANTI_RANGE
);
7800 take_default
= !find_case_label_range (stmt
, min
, max
, &i
, &j
);
7802 /* Set second range to emtpy. */
7806 if (vr
->type
== VR_RANGE
)
7810 return !take_default
;
7813 /* Set first range to all case labels. */
7820 /* Make sure all the values of case labels [i , j] are contained in
7821 range [MIN, MAX]. */
7822 case_low
= CASE_LOW (gimple_switch_label (stmt
, i
));
7823 case_high
= CASE_HIGH (gimple_switch_label (stmt
, j
));
7824 if (tree_int_cst_compare (case_low
, min
) < 0)
7826 if (case_high
!= NULL_TREE
7827 && tree_int_cst_compare (max
, case_high
) < 0)
7833 /* If the range spans case labels [i, j], the corresponding anti-range spans
7834 the labels [1, i - 1] and [j + 1, n - 1]. */
7860 /* Visit switch statement STMT. If we can determine which edge
7861 will be taken out of STMT's basic block, record it in
7862 *TAKEN_EDGE_P and return SSA_PROP_INTERESTING. Otherwise, return
7863 SSA_PROP_VARYING. */
7865 static enum ssa_prop_result
7866 vrp_visit_switch_stmt (gswitch
*stmt
, edge
*taken_edge_p
)
7870 size_t i
= 0, j
= 0, k
, l
;
7873 *taken_edge_p
= NULL
;
7874 op
= gimple_switch_index (stmt
);
7875 if (TREE_CODE (op
) != SSA_NAME
)
7876 return SSA_PROP_VARYING
;
7878 vr
= get_value_range (op
);
7879 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7881 fprintf (dump_file
, "\nVisiting switch expression with operand ");
7882 print_generic_expr (dump_file
, op
, 0);
7883 fprintf (dump_file
, " with known range ");
7884 dump_value_range (dump_file
, vr
);
7885 fprintf (dump_file
, "\n");
7888 if ((vr
->type
!= VR_RANGE
7889 && vr
->type
!= VR_ANTI_RANGE
)
7890 || symbolic_range_p (vr
))
7891 return SSA_PROP_VARYING
;
7893 /* Find the single edge that is taken from the switch expression. */
7894 take_default
= !find_case_label_ranges (stmt
, vr
, &i
, &j
, &k
, &l
);
7896 /* Check if the range spans no CASE_LABEL. If so, we only reach the default
7900 gcc_assert (take_default
);
7901 val
= gimple_switch_default_label (stmt
);
7905 /* Check if labels with index i to j and maybe the default label
7906 are all reaching the same label. */
7908 val
= gimple_switch_label (stmt
, i
);
7910 && CASE_LABEL (gimple_switch_default_label (stmt
))
7911 != CASE_LABEL (val
))
7913 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7914 fprintf (dump_file
, " not a single destination for this "
7916 return SSA_PROP_VARYING
;
7918 for (++i
; i
<= j
; ++i
)
7920 if (CASE_LABEL (gimple_switch_label (stmt
, i
)) != CASE_LABEL (val
))
7922 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7923 fprintf (dump_file
, " not a single destination for this "
7925 return SSA_PROP_VARYING
;
7930 if (CASE_LABEL (gimple_switch_label (stmt
, k
)) != CASE_LABEL (val
))
7932 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7933 fprintf (dump_file
, " not a single destination for this "
7935 return SSA_PROP_VARYING
;
7940 *taken_edge_p
= find_edge (gimple_bb (stmt
),
7941 label_to_block (CASE_LABEL (val
)));
7943 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7945 fprintf (dump_file
, " will take edge to ");
7946 print_generic_stmt (dump_file
, CASE_LABEL (val
), 0);
7949 return SSA_PROP_INTERESTING
;
7953 /* Evaluate statement STMT. If the statement produces a useful range,
7954 return SSA_PROP_INTERESTING and record the SSA name with the
7955 interesting range into *OUTPUT_P.
7957 If STMT is a conditional branch and we can determine its truth
7958 value, the taken edge is recorded in *TAKEN_EDGE_P.
7960 If STMT produces a varying value, return SSA_PROP_VARYING. */
7962 static enum ssa_prop_result
7963 vrp_visit_stmt (gimple stmt
, edge
*taken_edge_p
, tree
*output_p
)
7968 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7970 fprintf (dump_file
, "\nVisiting statement:\n");
7971 print_gimple_stmt (dump_file
, stmt
, 0, dump_flags
);
7974 if (!stmt_interesting_for_vrp (stmt
))
7975 gcc_assert (stmt_ends_bb_p (stmt
));
7976 else if (is_gimple_assign (stmt
) || is_gimple_call (stmt
))
7977 return vrp_visit_assignment_or_call (stmt
, output_p
);
7978 else if (gimple_code (stmt
) == GIMPLE_COND
)
7979 return vrp_visit_cond_stmt (as_a
<gcond
*> (stmt
), taken_edge_p
);
7980 else if (gimple_code (stmt
) == GIMPLE_SWITCH
)
7981 return vrp_visit_switch_stmt (as_a
<gswitch
*> (stmt
), taken_edge_p
);
7983 /* All other statements produce nothing of interest for VRP, so mark
7984 their outputs varying and prevent further simulation. */
7985 FOR_EACH_SSA_TREE_OPERAND (def
, stmt
, iter
, SSA_OP_DEF
)
7986 set_value_range_to_varying (get_value_range (def
));
7988 return SSA_PROP_VARYING
;
7991 /* Union the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
7992 { VR1TYPE, VR0MIN, VR0MAX } and store the result
7993 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
7994 possible such range. The resulting range is not canonicalized. */
7997 union_ranges (enum value_range_type
*vr0type
,
7998 tree
*vr0min
, tree
*vr0max
,
7999 enum value_range_type vr1type
,
8000 tree vr1min
, tree vr1max
)
8002 bool mineq
= operand_equal_p (*vr0min
, vr1min
, 0);
8003 bool maxeq
= operand_equal_p (*vr0max
, vr1max
, 0);
8005 /* [] is vr0, () is vr1 in the following classification comments. */
8009 if (*vr0type
== vr1type
)
8010 /* Nothing to do for equal ranges. */
8012 else if ((*vr0type
== VR_RANGE
8013 && vr1type
== VR_ANTI_RANGE
)
8014 || (*vr0type
== VR_ANTI_RANGE
8015 && vr1type
== VR_RANGE
))
8017 /* For anti-range with range union the result is varying. */
8023 else if (operand_less_p (*vr0max
, vr1min
) == 1
8024 || operand_less_p (vr1max
, *vr0min
) == 1)
8026 /* [ ] ( ) or ( ) [ ]
8027 If the ranges have an empty intersection, result of the union
8028 operation is the anti-range or if both are anti-ranges
8030 if (*vr0type
== VR_ANTI_RANGE
8031 && vr1type
== VR_ANTI_RANGE
)
8033 else if (*vr0type
== VR_ANTI_RANGE
8034 && vr1type
== VR_RANGE
)
8036 else if (*vr0type
== VR_RANGE
8037 && vr1type
== VR_ANTI_RANGE
)
8043 else if (*vr0type
== VR_RANGE
8044 && vr1type
== VR_RANGE
)
8046 /* The result is the convex hull of both ranges. */
8047 if (operand_less_p (*vr0max
, vr1min
) == 1)
8049 /* If the result can be an anti-range, create one. */
8050 if (TREE_CODE (*vr0max
) == INTEGER_CST
8051 && TREE_CODE (vr1min
) == INTEGER_CST
8052 && vrp_val_is_min (*vr0min
)
8053 && vrp_val_is_max (vr1max
))
8055 tree min
= int_const_binop (PLUS_EXPR
,
8057 build_int_cst (TREE_TYPE (*vr0max
), 1));
8058 tree max
= int_const_binop (MINUS_EXPR
,
8060 build_int_cst (TREE_TYPE (vr1min
), 1));
8061 if (!operand_less_p (max
, min
))
8063 *vr0type
= VR_ANTI_RANGE
;
8075 /* If the result can be an anti-range, create one. */
8076 if (TREE_CODE (vr1max
) == INTEGER_CST
8077 && TREE_CODE (*vr0min
) == INTEGER_CST
8078 && vrp_val_is_min (vr1min
)
8079 && vrp_val_is_max (*vr0max
))
8081 tree min
= int_const_binop (PLUS_EXPR
,
8083 build_int_cst (TREE_TYPE (vr1max
), 1));
8084 tree max
= int_const_binop (MINUS_EXPR
,
8086 build_int_cst (TREE_TYPE (*vr0min
), 1));
8087 if (!operand_less_p (max
, min
))
8089 *vr0type
= VR_ANTI_RANGE
;
8103 else if ((maxeq
|| operand_less_p (vr1max
, *vr0max
) == 1)
8104 && (mineq
|| operand_less_p (*vr0min
, vr1min
) == 1))
8106 /* [ ( ) ] or [( ) ] or [ ( )] */
8107 if (*vr0type
== VR_RANGE
8108 && vr1type
== VR_RANGE
)
8110 else if (*vr0type
== VR_ANTI_RANGE
8111 && vr1type
== VR_ANTI_RANGE
)
8117 else if (*vr0type
== VR_ANTI_RANGE
8118 && vr1type
== VR_RANGE
)
8120 /* Arbitrarily choose the right or left gap. */
8121 if (!mineq
&& TREE_CODE (vr1min
) == INTEGER_CST
)
8122 *vr0max
= int_const_binop (MINUS_EXPR
, vr1min
,
8123 build_int_cst (TREE_TYPE (vr1min
), 1));
8124 else if (!maxeq
&& TREE_CODE (vr1max
) == INTEGER_CST
)
8125 *vr0min
= int_const_binop (PLUS_EXPR
, vr1max
,
8126 build_int_cst (TREE_TYPE (vr1max
), 1));
8130 else if (*vr0type
== VR_RANGE
8131 && vr1type
== VR_ANTI_RANGE
)
8132 /* The result covers everything. */
8137 else if ((maxeq
|| operand_less_p (*vr0max
, vr1max
) == 1)
8138 && (mineq
|| operand_less_p (vr1min
, *vr0min
) == 1))
8140 /* ( [ ] ) or ([ ] ) or ( [ ]) */
8141 if (*vr0type
== VR_RANGE
8142 && vr1type
== VR_RANGE
)
8148 else if (*vr0type
== VR_ANTI_RANGE
8149 && vr1type
== VR_ANTI_RANGE
)
8151 else if (*vr0type
== VR_RANGE
8152 && vr1type
== VR_ANTI_RANGE
)
8154 *vr0type
= VR_ANTI_RANGE
;
8155 if (!mineq
&& TREE_CODE (*vr0min
) == INTEGER_CST
)
8157 *vr0max
= int_const_binop (MINUS_EXPR
, *vr0min
,
8158 build_int_cst (TREE_TYPE (*vr0min
), 1));
8161 else if (!maxeq
&& TREE_CODE (*vr0max
) == INTEGER_CST
)
8163 *vr0min
= int_const_binop (PLUS_EXPR
, *vr0max
,
8164 build_int_cst (TREE_TYPE (*vr0max
), 1));
8170 else if (*vr0type
== VR_ANTI_RANGE
8171 && vr1type
== VR_RANGE
)
8172 /* The result covers everything. */
8177 else if ((operand_less_p (vr1min
, *vr0max
) == 1
8178 || operand_equal_p (vr1min
, *vr0max
, 0))
8179 && operand_less_p (*vr0min
, vr1min
) == 1
8180 && operand_less_p (*vr0max
, vr1max
) == 1)
8182 /* [ ( ] ) or [ ]( ) */
8183 if (*vr0type
== VR_RANGE
8184 && vr1type
== VR_RANGE
)
8186 else if (*vr0type
== VR_ANTI_RANGE
8187 && vr1type
== VR_ANTI_RANGE
)
8189 else if (*vr0type
== VR_ANTI_RANGE
8190 && vr1type
== VR_RANGE
)
8192 if (TREE_CODE (vr1min
) == INTEGER_CST
)
8193 *vr0max
= int_const_binop (MINUS_EXPR
, vr1min
,
8194 build_int_cst (TREE_TYPE (vr1min
), 1));
8198 else if (*vr0type
== VR_RANGE
8199 && vr1type
== VR_ANTI_RANGE
)
8201 if (TREE_CODE (*vr0max
) == INTEGER_CST
)
8204 *vr0min
= int_const_binop (PLUS_EXPR
, *vr0max
,
8205 build_int_cst (TREE_TYPE (*vr0max
), 1));
8214 else if ((operand_less_p (*vr0min
, vr1max
) == 1
8215 || operand_equal_p (*vr0min
, vr1max
, 0))
8216 && operand_less_p (vr1min
, *vr0min
) == 1
8217 && operand_less_p (vr1max
, *vr0max
) == 1)
8219 /* ( [ ) ] or ( )[ ] */
8220 if (*vr0type
== VR_RANGE
8221 && vr1type
== VR_RANGE
)
8223 else if (*vr0type
== VR_ANTI_RANGE
8224 && vr1type
== VR_ANTI_RANGE
)
8226 else if (*vr0type
== VR_ANTI_RANGE
8227 && vr1type
== VR_RANGE
)
8229 if (TREE_CODE (vr1max
) == INTEGER_CST
)
8230 *vr0min
= int_const_binop (PLUS_EXPR
, vr1max
,
8231 build_int_cst (TREE_TYPE (vr1max
), 1));
8235 else if (*vr0type
== VR_RANGE
8236 && vr1type
== VR_ANTI_RANGE
)
8238 if (TREE_CODE (*vr0min
) == INTEGER_CST
)
8242 *vr0max
= int_const_binop (MINUS_EXPR
, *vr0min
,
8243 build_int_cst (TREE_TYPE (*vr0min
), 1));
8257 *vr0type
= VR_VARYING
;
8258 *vr0min
= NULL_TREE
;
8259 *vr0max
= NULL_TREE
;
8262 /* Intersect the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
8263 { VR1TYPE, VR0MIN, VR0MAX } and store the result
8264 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
8265 possible such range. The resulting range is not canonicalized. */
8268 intersect_ranges (enum value_range_type
*vr0type
,
8269 tree
*vr0min
, tree
*vr0max
,
8270 enum value_range_type vr1type
,
8271 tree vr1min
, tree vr1max
)
8273 bool mineq
= operand_equal_p (*vr0min
, vr1min
, 0);
8274 bool maxeq
= operand_equal_p (*vr0max
, vr1max
, 0);
8276 /* [] is vr0, () is vr1 in the following classification comments. */
8280 if (*vr0type
== vr1type
)
8281 /* Nothing to do for equal ranges. */
8283 else if ((*vr0type
== VR_RANGE
8284 && vr1type
== VR_ANTI_RANGE
)
8285 || (*vr0type
== VR_ANTI_RANGE
8286 && vr1type
== VR_RANGE
))
8288 /* For anti-range with range intersection the result is empty. */
8289 *vr0type
= VR_UNDEFINED
;
8290 *vr0min
= NULL_TREE
;
8291 *vr0max
= NULL_TREE
;
8296 else if (operand_less_p (*vr0max
, vr1min
) == 1
8297 || operand_less_p (vr1max
, *vr0min
) == 1)
8299 /* [ ] ( ) or ( ) [ ]
8300 If the ranges have an empty intersection, the result of the
8301 intersect operation is the range for intersecting an
8302 anti-range with a range or empty when intersecting two ranges. */
8303 if (*vr0type
== VR_RANGE
8304 && vr1type
== VR_ANTI_RANGE
)
8306 else if (*vr0type
== VR_ANTI_RANGE
8307 && vr1type
== VR_RANGE
)
8313 else if (*vr0type
== VR_RANGE
8314 && vr1type
== VR_RANGE
)
8316 *vr0type
= VR_UNDEFINED
;
8317 *vr0min
= NULL_TREE
;
8318 *vr0max
= NULL_TREE
;
8320 else if (*vr0type
== VR_ANTI_RANGE
8321 && vr1type
== VR_ANTI_RANGE
)
8323 /* If the anti-ranges are adjacent to each other merge them. */
8324 if (TREE_CODE (*vr0max
) == INTEGER_CST
8325 && TREE_CODE (vr1min
) == INTEGER_CST
8326 && operand_less_p (*vr0max
, vr1min
) == 1
8327 && integer_onep (int_const_binop (MINUS_EXPR
,
8330 else if (TREE_CODE (vr1max
) == INTEGER_CST
8331 && TREE_CODE (*vr0min
) == INTEGER_CST
8332 && operand_less_p (vr1max
, *vr0min
) == 1
8333 && integer_onep (int_const_binop (MINUS_EXPR
,
8336 /* Else arbitrarily take VR0. */
8339 else if ((maxeq
|| operand_less_p (vr1max
, *vr0max
) == 1)
8340 && (mineq
|| operand_less_p (*vr0min
, vr1min
) == 1))
8342 /* [ ( ) ] or [( ) ] or [ ( )] */
8343 if (*vr0type
== VR_RANGE
8344 && vr1type
== VR_RANGE
)
8346 /* If both are ranges the result is the inner one. */
8351 else if (*vr0type
== VR_RANGE
8352 && vr1type
== VR_ANTI_RANGE
)
8354 /* Choose the right gap if the left one is empty. */
8357 if (TREE_CODE (vr1max
) == INTEGER_CST
)
8358 *vr0min
= int_const_binop (PLUS_EXPR
, vr1max
,
8359 build_int_cst (TREE_TYPE (vr1max
), 1));
8363 /* Choose the left gap if the right one is empty. */
8366 if (TREE_CODE (vr1min
) == INTEGER_CST
)
8367 *vr0max
= int_const_binop (MINUS_EXPR
, vr1min
,
8368 build_int_cst (TREE_TYPE (vr1min
), 1));
8372 /* Choose the anti-range if the range is effectively varying. */
8373 else if (vrp_val_is_min (*vr0min
)
8374 && vrp_val_is_max (*vr0max
))
8380 /* Else choose the range. */
8382 else if (*vr0type
== VR_ANTI_RANGE
8383 && vr1type
== VR_ANTI_RANGE
)
8384 /* If both are anti-ranges the result is the outer one. */
8386 else if (*vr0type
== VR_ANTI_RANGE
8387 && vr1type
== VR_RANGE
)
8389 /* The intersection is empty. */
8390 *vr0type
= VR_UNDEFINED
;
8391 *vr0min
= NULL_TREE
;
8392 *vr0max
= NULL_TREE
;
8397 else if ((maxeq
|| operand_less_p (*vr0max
, vr1max
) == 1)
8398 && (mineq
|| operand_less_p (vr1min
, *vr0min
) == 1))
8400 /* ( [ ] ) or ([ ] ) or ( [ ]) */
8401 if (*vr0type
== VR_RANGE
8402 && vr1type
== VR_RANGE
)
8403 /* Choose the inner range. */
8405 else if (*vr0type
== VR_ANTI_RANGE
8406 && vr1type
== VR_RANGE
)
8408 /* Choose the right gap if the left is empty. */
8411 *vr0type
= VR_RANGE
;
8412 if (TREE_CODE (*vr0max
) == INTEGER_CST
)
8413 *vr0min
= int_const_binop (PLUS_EXPR
, *vr0max
,
8414 build_int_cst (TREE_TYPE (*vr0max
), 1));
8419 /* Choose the left gap if the right is empty. */
8422 *vr0type
= VR_RANGE
;
8423 if (TREE_CODE (*vr0min
) == INTEGER_CST
)
8424 *vr0max
= int_const_binop (MINUS_EXPR
, *vr0min
,
8425 build_int_cst (TREE_TYPE (*vr0min
), 1));
8430 /* Choose the anti-range if the range is effectively varying. */
8431 else if (vrp_val_is_min (vr1min
)
8432 && vrp_val_is_max (vr1max
))
8434 /* Else choose the range. */
8442 else if (*vr0type
== VR_ANTI_RANGE
8443 && vr1type
== VR_ANTI_RANGE
)
8445 /* If both are anti-ranges the result is the outer one. */
8450 else if (vr1type
== VR_ANTI_RANGE
8451 && *vr0type
== VR_RANGE
)
8453 /* The intersection is empty. */
8454 *vr0type
= VR_UNDEFINED
;
8455 *vr0min
= NULL_TREE
;
8456 *vr0max
= NULL_TREE
;
8461 else if ((operand_less_p (vr1min
, *vr0max
) == 1
8462 || operand_equal_p (vr1min
, *vr0max
, 0))
8463 && operand_less_p (*vr0min
, vr1min
) == 1)
8465 /* [ ( ] ) or [ ]( ) */
8466 if (*vr0type
== VR_ANTI_RANGE
8467 && vr1type
== VR_ANTI_RANGE
)
8469 else if (*vr0type
== VR_RANGE
8470 && vr1type
== VR_RANGE
)
8472 else if (*vr0type
== VR_RANGE
8473 && vr1type
== VR_ANTI_RANGE
)
8475 if (TREE_CODE (vr1min
) == INTEGER_CST
)
8476 *vr0max
= int_const_binop (MINUS_EXPR
, vr1min
,
8477 build_int_cst (TREE_TYPE (vr1min
), 1));
8481 else if (*vr0type
== VR_ANTI_RANGE
8482 && vr1type
== VR_RANGE
)
8484 *vr0type
= VR_RANGE
;
8485 if (TREE_CODE (*vr0max
) == INTEGER_CST
)
8486 *vr0min
= int_const_binop (PLUS_EXPR
, *vr0max
,
8487 build_int_cst (TREE_TYPE (*vr0max
), 1));
8495 else if ((operand_less_p (*vr0min
, vr1max
) == 1
8496 || operand_equal_p (*vr0min
, vr1max
, 0))
8497 && operand_less_p (vr1min
, *vr0min
) == 1)
8499 /* ( [ ) ] or ( )[ ] */
8500 if (*vr0type
== VR_ANTI_RANGE
8501 && vr1type
== VR_ANTI_RANGE
)
8503 else if (*vr0type
== VR_RANGE
8504 && vr1type
== VR_RANGE
)
8506 else if (*vr0type
== VR_RANGE
8507 && vr1type
== VR_ANTI_RANGE
)
8509 if (TREE_CODE (vr1max
) == INTEGER_CST
)
8510 *vr0min
= int_const_binop (PLUS_EXPR
, vr1max
,
8511 build_int_cst (TREE_TYPE (vr1max
), 1));
8515 else if (*vr0type
== VR_ANTI_RANGE
8516 && vr1type
== VR_RANGE
)
8518 *vr0type
= VR_RANGE
;
8519 if (TREE_CODE (*vr0min
) == INTEGER_CST
)
8520 *vr0max
= int_const_binop (MINUS_EXPR
, *vr0min
,
8521 build_int_cst (TREE_TYPE (*vr0min
), 1));
8530 /* As a fallback simply use { *VRTYPE, *VR0MIN, *VR0MAX } as
8531 result for the intersection. That's always a conservative
8532 correct estimate. */
8538 /* Intersect the two value-ranges *VR0 and *VR1 and store the result
8539 in *VR0. This may not be the smallest possible such range. */
8542 vrp_intersect_ranges_1 (value_range_t
*vr0
, value_range_t
*vr1
)
8544 value_range_t saved
;
8546 /* If either range is VR_VARYING the other one wins. */
8547 if (vr1
->type
== VR_VARYING
)
8549 if (vr0
->type
== VR_VARYING
)
8551 copy_value_range (vr0
, vr1
);
8555 /* When either range is VR_UNDEFINED the resulting range is
8556 VR_UNDEFINED, too. */
8557 if (vr0
->type
== VR_UNDEFINED
)
8559 if (vr1
->type
== VR_UNDEFINED
)
8561 set_value_range_to_undefined (vr0
);
8565 /* Save the original vr0 so we can return it as conservative intersection
8566 result when our worker turns things to varying. */
8568 intersect_ranges (&vr0
->type
, &vr0
->min
, &vr0
->max
,
8569 vr1
->type
, vr1
->min
, vr1
->max
);
8570 /* Make sure to canonicalize the result though as the inversion of a
8571 VR_RANGE can still be a VR_RANGE. */
8572 set_and_canonicalize_value_range (vr0
, vr0
->type
,
8573 vr0
->min
, vr0
->max
, vr0
->equiv
);
8574 /* If that failed, use the saved original VR0. */
8575 if (vr0
->type
== VR_VARYING
)
8580 /* If the result is VR_UNDEFINED there is no need to mess with
8581 the equivalencies. */
8582 if (vr0
->type
== VR_UNDEFINED
)
8585 /* The resulting set of equivalences for range intersection is the union of
8587 if (vr0
->equiv
&& vr1
->equiv
&& vr0
->equiv
!= vr1
->equiv
)
8588 bitmap_ior_into (vr0
->equiv
, vr1
->equiv
);
8589 else if (vr1
->equiv
&& !vr0
->equiv
)
8590 bitmap_copy (vr0
->equiv
, vr1
->equiv
);
8594 vrp_intersect_ranges (value_range_t
*vr0
, value_range_t
*vr1
)
8596 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8598 fprintf (dump_file
, "Intersecting\n ");
8599 dump_value_range (dump_file
, vr0
);
8600 fprintf (dump_file
, "\nand\n ");
8601 dump_value_range (dump_file
, vr1
);
8602 fprintf (dump_file
, "\n");
8604 vrp_intersect_ranges_1 (vr0
, vr1
);
8605 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8607 fprintf (dump_file
, "to\n ");
8608 dump_value_range (dump_file
, vr0
);
8609 fprintf (dump_file
, "\n");
8613 /* Meet operation for value ranges. Given two value ranges VR0 and
8614 VR1, store in VR0 a range that contains both VR0 and VR1. This
8615 may not be the smallest possible such range. */
8618 vrp_meet_1 (value_range_t
*vr0
, value_range_t
*vr1
)
8620 value_range_t saved
;
8622 if (vr0
->type
== VR_UNDEFINED
)
8624 set_value_range (vr0
, vr1
->type
, vr1
->min
, vr1
->max
, vr1
->equiv
);
8628 if (vr1
->type
== VR_UNDEFINED
)
8630 /* VR0 already has the resulting range. */
8634 if (vr0
->type
== VR_VARYING
)
8636 /* Nothing to do. VR0 already has the resulting range. */
8640 if (vr1
->type
== VR_VARYING
)
8642 set_value_range_to_varying (vr0
);
8647 union_ranges (&vr0
->type
, &vr0
->min
, &vr0
->max
,
8648 vr1
->type
, vr1
->min
, vr1
->max
);
8649 if (vr0
->type
== VR_VARYING
)
8651 /* Failed to find an efficient meet. Before giving up and setting
8652 the result to VARYING, see if we can at least derive a useful
8653 anti-range. FIXME, all this nonsense about distinguishing
8654 anti-ranges from ranges is necessary because of the odd
8655 semantics of range_includes_zero_p and friends. */
8656 if (((saved
.type
== VR_RANGE
8657 && range_includes_zero_p (saved
.min
, saved
.max
) == 0)
8658 || (saved
.type
== VR_ANTI_RANGE
8659 && range_includes_zero_p (saved
.min
, saved
.max
) == 1))
8660 && ((vr1
->type
== VR_RANGE
8661 && range_includes_zero_p (vr1
->min
, vr1
->max
) == 0)
8662 || (vr1
->type
== VR_ANTI_RANGE
8663 && range_includes_zero_p (vr1
->min
, vr1
->max
) == 1)))
8665 set_value_range_to_nonnull (vr0
, TREE_TYPE (saved
.min
));
8667 /* Since this meet operation did not result from the meeting of
8668 two equivalent names, VR0 cannot have any equivalences. */
8670 bitmap_clear (vr0
->equiv
);
8674 set_value_range_to_varying (vr0
);
8677 set_and_canonicalize_value_range (vr0
, vr0
->type
, vr0
->min
, vr0
->max
,
8679 if (vr0
->type
== VR_VARYING
)
8682 /* The resulting set of equivalences is always the intersection of
8684 if (vr0
->equiv
&& vr1
->equiv
&& vr0
->equiv
!= vr1
->equiv
)
8685 bitmap_and_into (vr0
->equiv
, vr1
->equiv
);
8686 else if (vr0
->equiv
&& !vr1
->equiv
)
8687 bitmap_clear (vr0
->equiv
);
8691 vrp_meet (value_range_t
*vr0
, value_range_t
*vr1
)
8693 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8695 fprintf (dump_file
, "Meeting\n ");
8696 dump_value_range (dump_file
, vr0
);
8697 fprintf (dump_file
, "\nand\n ");
8698 dump_value_range (dump_file
, vr1
);
8699 fprintf (dump_file
, "\n");
8701 vrp_meet_1 (vr0
, vr1
);
8702 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8704 fprintf (dump_file
, "to\n ");
8705 dump_value_range (dump_file
, vr0
);
8706 fprintf (dump_file
, "\n");
8711 /* Visit all arguments for PHI node PHI that flow through executable
8712 edges. If a valid value range can be derived from all the incoming
8713 value ranges, set a new range for the LHS of PHI. */
8715 static enum ssa_prop_result
8716 vrp_visit_phi_node (gphi
*phi
)
8719 tree lhs
= PHI_RESULT (phi
);
8720 value_range_t
*lhs_vr
= get_value_range (lhs
);
8721 value_range_t vr_result
= VR_INITIALIZER
;
8723 int edges
, old_edges
;
8726 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8728 fprintf (dump_file
, "\nVisiting PHI node: ");
8729 print_gimple_stmt (dump_file
, phi
, 0, dump_flags
);
8733 for (i
= 0; i
< gimple_phi_num_args (phi
); i
++)
8735 edge e
= gimple_phi_arg_edge (phi
, i
);
8737 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8740 " Argument #%d (%d -> %d %sexecutable)\n",
8741 (int) i
, e
->src
->index
, e
->dest
->index
,
8742 (e
->flags
& EDGE_EXECUTABLE
) ? "" : "not ");
8745 if (e
->flags
& EDGE_EXECUTABLE
)
8747 tree arg
= PHI_ARG_DEF (phi
, i
);
8748 value_range_t vr_arg
;
8752 if (TREE_CODE (arg
) == SSA_NAME
)
8754 vr_arg
= *(get_value_range (arg
));
8755 /* Do not allow equivalences or symbolic ranges to leak in from
8756 backedges. That creates invalid equivalencies.
8757 See PR53465 and PR54767. */
8758 if (e
->flags
& EDGE_DFS_BACK
)
8760 if (vr_arg
.type
== VR_RANGE
8761 || vr_arg
.type
== VR_ANTI_RANGE
)
8763 vr_arg
.equiv
= NULL
;
8764 if (symbolic_range_p (&vr_arg
))
8766 vr_arg
.type
= VR_VARYING
;
8767 vr_arg
.min
= NULL_TREE
;
8768 vr_arg
.max
= NULL_TREE
;
8774 /* If the non-backedge arguments range is VR_VARYING then
8775 we can still try recording a simple equivalence. */
8776 if (vr_arg
.type
== VR_VARYING
)
8778 vr_arg
.type
= VR_RANGE
;
8781 vr_arg
.equiv
= NULL
;
8787 if (TREE_OVERFLOW_P (arg
))
8788 arg
= drop_tree_overflow (arg
);
8790 vr_arg
.type
= VR_RANGE
;
8793 vr_arg
.equiv
= NULL
;
8796 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8798 fprintf (dump_file
, "\t");
8799 print_generic_expr (dump_file
, arg
, dump_flags
);
8800 fprintf (dump_file
, ": ");
8801 dump_value_range (dump_file
, &vr_arg
);
8802 fprintf (dump_file
, "\n");
8806 copy_value_range (&vr_result
, &vr_arg
);
8808 vrp_meet (&vr_result
, &vr_arg
);
8811 if (vr_result
.type
== VR_VARYING
)
8816 if (vr_result
.type
== VR_VARYING
)
8818 else if (vr_result
.type
== VR_UNDEFINED
)
8821 old_edges
= vr_phi_edge_counts
[SSA_NAME_VERSION (lhs
)];
8822 vr_phi_edge_counts
[SSA_NAME_VERSION (lhs
)] = edges
;
8824 /* To prevent infinite iterations in the algorithm, derive ranges
8825 when the new value is slightly bigger or smaller than the
8826 previous one. We don't do this if we have seen a new executable
8827 edge; this helps us avoid an overflow infinity for conditionals
8828 which are not in a loop. If the old value-range was VR_UNDEFINED
8829 use the updated range and iterate one more time. */
8831 && gimple_phi_num_args (phi
) > 1
8832 && edges
== old_edges
8833 && lhs_vr
->type
!= VR_UNDEFINED
)
8835 /* Compare old and new ranges, fall back to varying if the
8836 values are not comparable. */
8837 int cmp_min
= compare_values (lhs_vr
->min
, vr_result
.min
);
8840 int cmp_max
= compare_values (lhs_vr
->max
, vr_result
.max
);
8844 /* For non VR_RANGE or for pointers fall back to varying if
8845 the range changed. */
8846 if ((lhs_vr
->type
!= VR_RANGE
|| vr_result
.type
!= VR_RANGE
8847 || POINTER_TYPE_P (TREE_TYPE (lhs
)))
8848 && (cmp_min
!= 0 || cmp_max
!= 0))
8851 /* If the new minimum is larger than the previous one
8852 retain the old value. If the new minimum value is smaller
8853 than the previous one and not -INF go all the way to -INF + 1.
8854 In the first case, to avoid infinite bouncing between different
8855 minimums, and in the other case to avoid iterating millions of
8856 times to reach -INF. Going to -INF + 1 also lets the following
8857 iteration compute whether there will be any overflow, at the
8858 expense of one additional iteration. */
8860 vr_result
.min
= lhs_vr
->min
;
8861 else if (cmp_min
> 0
8862 && !vrp_val_is_min (vr_result
.min
))
8864 = int_const_binop (PLUS_EXPR
,
8865 vrp_val_min (TREE_TYPE (vr_result
.min
)),
8866 build_int_cst (TREE_TYPE (vr_result
.min
), 1));
8868 /* Similarly for the maximum value. */
8870 vr_result
.max
= lhs_vr
->max
;
8871 else if (cmp_max
< 0
8872 && !vrp_val_is_max (vr_result
.max
))
8874 = int_const_binop (MINUS_EXPR
,
8875 vrp_val_max (TREE_TYPE (vr_result
.min
)),
8876 build_int_cst (TREE_TYPE (vr_result
.min
), 1));
8878 /* If we dropped either bound to +-INF then if this is a loop
8879 PHI node SCEV may known more about its value-range. */
8880 if ((cmp_min
> 0 || cmp_min
< 0
8881 || cmp_max
< 0 || cmp_max
> 0)
8882 && (l
= loop_containing_stmt (phi
))
8883 && l
->header
== gimple_bb (phi
))
8884 adjust_range_with_scev (&vr_result
, l
, phi
, lhs
);
8886 /* If we will end up with a (-INF, +INF) range, set it to
8887 VARYING. Same if the previous max value was invalid for
8888 the type and we end up with vr_result.min > vr_result.max. */
8889 if ((vrp_val_is_max (vr_result
.max
)
8890 && vrp_val_is_min (vr_result
.min
))
8891 || compare_values (vr_result
.min
,
8896 /* If the new range is different than the previous value, keep
8899 if (update_value_range (lhs
, &vr_result
))
8901 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8903 fprintf (dump_file
, "Found new range for ");
8904 print_generic_expr (dump_file
, lhs
, 0);
8905 fprintf (dump_file
, ": ");
8906 dump_value_range (dump_file
, &vr_result
);
8907 fprintf (dump_file
, "\n");
8910 if (vr_result
.type
== VR_VARYING
)
8911 return SSA_PROP_VARYING
;
8913 return SSA_PROP_INTERESTING
;
8916 /* Nothing changed, don't add outgoing edges. */
8917 return SSA_PROP_NOT_INTERESTING
;
8919 /* No match found. Set the LHS to VARYING. */
8921 set_value_range_to_varying (lhs_vr
);
8922 return SSA_PROP_VARYING
;
8925 /* Simplify boolean operations if the source is known
8926 to be already a boolean. */
8928 simplify_truth_ops_using_ranges (gimple_stmt_iterator
*gsi
, gimple stmt
)
8930 enum tree_code rhs_code
= gimple_assign_rhs_code (stmt
);
8932 bool need_conversion
;
8934 /* We handle only !=/== case here. */
8935 gcc_assert (rhs_code
== EQ_EXPR
|| rhs_code
== NE_EXPR
);
8937 op0
= gimple_assign_rhs1 (stmt
);
8938 if (!op_with_boolean_value_range_p (op0
))
8941 op1
= gimple_assign_rhs2 (stmt
);
8942 if (!op_with_boolean_value_range_p (op1
))
8945 /* Reduce number of cases to handle to NE_EXPR. As there is no
8946 BIT_XNOR_EXPR we cannot replace A == B with a single statement. */
8947 if (rhs_code
== EQ_EXPR
)
8949 if (TREE_CODE (op1
) == INTEGER_CST
)
8950 op1
= int_const_binop (BIT_XOR_EXPR
, op1
,
8951 build_int_cst (TREE_TYPE (op1
), 1));
8956 lhs
= gimple_assign_lhs (stmt
);
8958 = !useless_type_conversion_p (TREE_TYPE (lhs
), TREE_TYPE (op0
));
8960 /* Make sure to not sign-extend a 1-bit 1 when converting the result. */
8962 && !TYPE_UNSIGNED (TREE_TYPE (op0
))
8963 && TYPE_PRECISION (TREE_TYPE (op0
)) == 1
8964 && TYPE_PRECISION (TREE_TYPE (lhs
)) > 1)
8967 /* For A != 0 we can substitute A itself. */
8968 if (integer_zerop (op1
))
8969 gimple_assign_set_rhs_with_ops (gsi
,
8971 ? NOP_EXPR
: TREE_CODE (op0
), op0
);
8972 /* For A != B we substitute A ^ B. Either with conversion. */
8973 else if (need_conversion
)
8975 tree tem
= make_ssa_name (TREE_TYPE (op0
));
8977 = gimple_build_assign (tem
, BIT_XOR_EXPR
, op0
, op1
);
8978 gsi_insert_before (gsi
, newop
, GSI_SAME_STMT
);
8979 gimple_assign_set_rhs_with_ops (gsi
, NOP_EXPR
, tem
);
8983 gimple_assign_set_rhs_with_ops (gsi
, BIT_XOR_EXPR
, op0
, op1
);
8984 update_stmt (gsi_stmt (*gsi
));
8989 /* Simplify a division or modulo operator to a right shift or
8990 bitwise and if the first operand is unsigned or is greater
8991 than zero and the second operand is an exact power of two.
8992 For TRUNC_MOD_EXPR op0 % op1 with constant op1, optimize it
8993 into just op0 if op0's range is known to be a subset of
8994 [-op1 + 1, op1 - 1] for signed and [0, op1 - 1] for unsigned
8998 simplify_div_or_mod_using_ranges (gimple stmt
)
9000 enum tree_code rhs_code
= gimple_assign_rhs_code (stmt
);
9002 tree op0
= gimple_assign_rhs1 (stmt
);
9003 tree op1
= gimple_assign_rhs2 (stmt
);
9004 value_range_t
*vr
= get_value_range (op0
);
9006 if (rhs_code
== TRUNC_MOD_EXPR
9007 && TREE_CODE (op1
) == INTEGER_CST
9008 && tree_int_cst_sgn (op1
) == 1
9009 && range_int_cst_p (vr
)
9010 && tree_int_cst_lt (vr
->max
, op1
))
9012 if (TYPE_UNSIGNED (TREE_TYPE (op0
))
9013 || tree_int_cst_sgn (vr
->min
) >= 0
9014 || tree_int_cst_lt (fold_unary (NEGATE_EXPR
, TREE_TYPE (op1
), op1
),
9017 /* If op0 already has the range op0 % op1 has,
9018 then TRUNC_MOD_EXPR won't change anything. */
9019 gimple_stmt_iterator gsi
= gsi_for_stmt (stmt
);
9020 gimple_assign_set_rhs_from_tree (&gsi
, op0
);
9026 if (!integer_pow2p (op1
))
9029 if (TYPE_UNSIGNED (TREE_TYPE (op0
)))
9031 val
= integer_one_node
;
9037 val
= compare_range_with_value (GE_EXPR
, vr
, integer_zero_node
, &sop
);
9041 && integer_onep (val
)
9042 && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC
))
9044 location_t location
;
9046 if (!gimple_has_location (stmt
))
9047 location
= input_location
;
9049 location
= gimple_location (stmt
);
9050 warning_at (location
, OPT_Wstrict_overflow
,
9051 "assuming signed overflow does not occur when "
9052 "simplifying %</%> or %<%%%> to %<>>%> or %<&%>");
9056 if (val
&& integer_onep (val
))
9060 if (rhs_code
== TRUNC_DIV_EXPR
)
9062 t
= build_int_cst (integer_type_node
, tree_log2 (op1
));
9063 gimple_assign_set_rhs_code (stmt
, RSHIFT_EXPR
);
9064 gimple_assign_set_rhs1 (stmt
, op0
);
9065 gimple_assign_set_rhs2 (stmt
, t
);
9069 t
= build_int_cst (TREE_TYPE (op1
), 1);
9070 t
= int_const_binop (MINUS_EXPR
, op1
, t
);
9071 t
= fold_convert (TREE_TYPE (op0
), t
);
9073 gimple_assign_set_rhs_code (stmt
, BIT_AND_EXPR
);
9074 gimple_assign_set_rhs1 (stmt
, op0
);
9075 gimple_assign_set_rhs2 (stmt
, t
);
9085 /* Simplify a min or max if the ranges of the two operands are
9086 disjoint. Return true if we do simplify. */
9089 simplify_min_or_max_using_ranges (gimple stmt
)
9091 tree op0
= gimple_assign_rhs1 (stmt
);
9092 tree op1
= gimple_assign_rhs2 (stmt
);
9096 val
= (vrp_evaluate_conditional_warnv_with_ops_using_ranges
9097 (LE_EXPR
, op0
, op1
, &sop
));
9101 val
= (vrp_evaluate_conditional_warnv_with_ops_using_ranges
9102 (LT_EXPR
, op0
, op1
, &sop
));
9107 if (sop
&& issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC
))
9109 location_t location
;
9111 if (!gimple_has_location (stmt
))
9112 location
= input_location
;
9114 location
= gimple_location (stmt
);
9115 warning_at (location
, OPT_Wstrict_overflow
,
9116 "assuming signed overflow does not occur when "
9117 "simplifying %<min/max (X,Y)%> to %<X%> or %<Y%>");
9120 /* VAL == TRUE -> OP0 < or <= op1
9121 VAL == FALSE -> OP0 > or >= op1. */
9122 tree res
= ((gimple_assign_rhs_code (stmt
) == MAX_EXPR
)
9123 == integer_zerop (val
)) ? op0
: op1
;
9124 gimple_stmt_iterator gsi
= gsi_for_stmt (stmt
);
9125 gimple_assign_set_rhs_from_tree (&gsi
, res
);
9133 /* If the operand to an ABS_EXPR is >= 0, then eliminate the
9134 ABS_EXPR. If the operand is <= 0, then simplify the
9135 ABS_EXPR into a NEGATE_EXPR. */
9138 simplify_abs_using_ranges (gimple stmt
)
9140 tree op
= gimple_assign_rhs1 (stmt
);
9141 value_range_t
*vr
= get_value_range (op
);
9148 val
= compare_range_with_value (LE_EXPR
, vr
, integer_zero_node
, &sop
);
9151 /* The range is neither <= 0 nor > 0. Now see if it is
9152 either < 0 or >= 0. */
9154 val
= compare_range_with_value (LT_EXPR
, vr
, integer_zero_node
,
9160 if (sop
&& issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC
))
9162 location_t location
;
9164 if (!gimple_has_location (stmt
))
9165 location
= input_location
;
9167 location
= gimple_location (stmt
);
9168 warning_at (location
, OPT_Wstrict_overflow
,
9169 "assuming signed overflow does not occur when "
9170 "simplifying %<abs (X)%> to %<X%> or %<-X%>");
9173 gimple_assign_set_rhs1 (stmt
, op
);
9174 if (integer_zerop (val
))
9175 gimple_assign_set_rhs_code (stmt
, SSA_NAME
);
9177 gimple_assign_set_rhs_code (stmt
, NEGATE_EXPR
);
9186 /* Optimize away redundant BIT_AND_EXPR and BIT_IOR_EXPR.
9187 If all the bits that are being cleared by & are already
9188 known to be zero from VR, or all the bits that are being
9189 set by | are already known to be one from VR, the bit
9190 operation is redundant. */
9193 simplify_bit_ops_using_ranges (gimple_stmt_iterator
*gsi
, gimple stmt
)
9195 tree op0
= gimple_assign_rhs1 (stmt
);
9196 tree op1
= gimple_assign_rhs2 (stmt
);
9197 tree op
= NULL_TREE
;
9198 value_range_t vr0
= VR_INITIALIZER
;
9199 value_range_t vr1
= VR_INITIALIZER
;
9200 wide_int may_be_nonzero0
, may_be_nonzero1
;
9201 wide_int must_be_nonzero0
, must_be_nonzero1
;
9204 if (TREE_CODE (op0
) == SSA_NAME
)
9205 vr0
= *(get_value_range (op0
));
9206 else if (is_gimple_min_invariant (op0
))
9207 set_value_range_to_value (&vr0
, op0
, NULL
);
9211 if (TREE_CODE (op1
) == SSA_NAME
)
9212 vr1
= *(get_value_range (op1
));
9213 else if (is_gimple_min_invariant (op1
))
9214 set_value_range_to_value (&vr1
, op1
, NULL
);
9218 if (!zero_nonzero_bits_from_vr (TREE_TYPE (op0
), &vr0
, &may_be_nonzero0
,
9221 if (!zero_nonzero_bits_from_vr (TREE_TYPE (op1
), &vr1
, &may_be_nonzero1
,
9225 switch (gimple_assign_rhs_code (stmt
))
9228 mask
= may_be_nonzero0
.and_not (must_be_nonzero1
);
9234 mask
= may_be_nonzero1
.and_not (must_be_nonzero0
);
9242 mask
= may_be_nonzero0
.and_not (must_be_nonzero1
);
9248 mask
= may_be_nonzero1
.and_not (must_be_nonzero0
);
9259 if (op
== NULL_TREE
)
9262 gimple_assign_set_rhs_with_ops (gsi
, TREE_CODE (op
), op
);
9263 update_stmt (gsi_stmt (*gsi
));
9267 /* We are comparing trees OP0 and OP1 using COND_CODE. OP0 has
9268 a known value range VR.
9270 If there is one and only one value which will satisfy the
9271 conditional, then return that value. Else return NULL.
9273 If signed overflow must be undefined for the value to satisfy
9274 the conditional, then set *STRICT_OVERFLOW_P to true. */
9277 test_for_singularity (enum tree_code cond_code
, tree op0
,
9278 tree op1
, value_range_t
*vr
,
9279 bool *strict_overflow_p
)
9284 /* Extract minimum/maximum values which satisfy the
9285 the conditional as it was written. */
9286 if (cond_code
== LE_EXPR
|| cond_code
== LT_EXPR
)
9288 /* This should not be negative infinity; there is no overflow
9290 min
= TYPE_MIN_VALUE (TREE_TYPE (op0
));
9293 if (cond_code
== LT_EXPR
&& !is_overflow_infinity (max
))
9295 tree one
= build_int_cst (TREE_TYPE (op0
), 1);
9296 max
= fold_build2 (MINUS_EXPR
, TREE_TYPE (op0
), max
, one
);
9298 TREE_NO_WARNING (max
) = 1;
9301 else if (cond_code
== GE_EXPR
|| cond_code
== GT_EXPR
)
9303 /* This should not be positive infinity; there is no overflow
9305 max
= TYPE_MAX_VALUE (TREE_TYPE (op0
));
9308 if (cond_code
== GT_EXPR
&& !is_overflow_infinity (min
))
9310 tree one
= build_int_cst (TREE_TYPE (op0
), 1);
9311 min
= fold_build2 (PLUS_EXPR
, TREE_TYPE (op0
), min
, one
);
9313 TREE_NO_WARNING (min
) = 1;
9317 /* Now refine the minimum and maximum values using any
9318 value range information we have for op0. */
9321 if (compare_values (vr
->min
, min
) == 1)
9323 if (compare_values (vr
->max
, max
) == -1)
9326 /* If the new min/max values have converged to a single value,
9327 then there is only one value which can satisfy the condition,
9328 return that value. */
9329 if (operand_equal_p (min
, max
, 0) && is_gimple_min_invariant (min
))
9331 if ((cond_code
== LE_EXPR
|| cond_code
== LT_EXPR
)
9332 && is_overflow_infinity (vr
->max
))
9333 *strict_overflow_p
= true;
9334 if ((cond_code
== GE_EXPR
|| cond_code
== GT_EXPR
)
9335 && is_overflow_infinity (vr
->min
))
9336 *strict_overflow_p
= true;
9344 /* Return whether the value range *VR fits in an integer type specified
9345 by PRECISION and UNSIGNED_P. */
9348 range_fits_type_p (value_range_t
*vr
, unsigned dest_precision
, signop dest_sgn
)
9351 unsigned src_precision
;
9355 /* We can only handle integral and pointer types. */
9356 src_type
= TREE_TYPE (vr
->min
);
9357 if (!INTEGRAL_TYPE_P (src_type
)
9358 && !POINTER_TYPE_P (src_type
))
9361 /* An extension is fine unless VR is SIGNED and dest_sgn is UNSIGNED,
9362 and so is an identity transform. */
9363 src_precision
= TYPE_PRECISION (TREE_TYPE (vr
->min
));
9364 src_sgn
= TYPE_SIGN (src_type
);
9365 if ((src_precision
< dest_precision
9366 && !(dest_sgn
== UNSIGNED
&& src_sgn
== SIGNED
))
9367 || (src_precision
== dest_precision
&& src_sgn
== dest_sgn
))
9370 /* Now we can only handle ranges with constant bounds. */
9371 if (vr
->type
!= VR_RANGE
9372 || TREE_CODE (vr
->min
) != INTEGER_CST
9373 || TREE_CODE (vr
->max
) != INTEGER_CST
)
9376 /* For sign changes, the MSB of the wide_int has to be clear.
9377 An unsigned value with its MSB set cannot be represented by
9378 a signed wide_int, while a negative value cannot be represented
9379 by an unsigned wide_int. */
9380 if (src_sgn
!= dest_sgn
9381 && (wi::lts_p (vr
->min
, 0) || wi::lts_p (vr
->max
, 0)))
9384 /* Then we can perform the conversion on both ends and compare
9385 the result for equality. */
9386 tem
= wi::ext (wi::to_widest (vr
->min
), dest_precision
, dest_sgn
);
9387 if (tem
!= wi::to_widest (vr
->min
))
9389 tem
= wi::ext (wi::to_widest (vr
->max
), dest_precision
, dest_sgn
);
9390 if (tem
!= wi::to_widest (vr
->max
))
9396 /* Simplify a conditional using a relational operator to an equality
9397 test if the range information indicates only one value can satisfy
9398 the original conditional. */
9401 simplify_cond_using_ranges (gcond
*stmt
)
9403 tree op0
= gimple_cond_lhs (stmt
);
9404 tree op1
= gimple_cond_rhs (stmt
);
9405 enum tree_code cond_code
= gimple_cond_code (stmt
);
9407 if (cond_code
!= NE_EXPR
9408 && cond_code
!= EQ_EXPR
9409 && TREE_CODE (op0
) == SSA_NAME
9410 && INTEGRAL_TYPE_P (TREE_TYPE (op0
))
9411 && is_gimple_min_invariant (op1
))
9413 value_range_t
*vr
= get_value_range (op0
);
9415 /* If we have range information for OP0, then we might be
9416 able to simplify this conditional. */
9417 if (vr
->type
== VR_RANGE
)
9419 enum warn_strict_overflow_code wc
= WARN_STRICT_OVERFLOW_COMPARISON
;
9421 tree new_tree
= test_for_singularity (cond_code
, op0
, op1
, vr
, &sop
);
9424 && (!sop
|| TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0
))))
9428 fprintf (dump_file
, "Simplified relational ");
9429 print_gimple_stmt (dump_file
, stmt
, 0, 0);
9430 fprintf (dump_file
, " into ");
9433 gimple_cond_set_code (stmt
, EQ_EXPR
);
9434 gimple_cond_set_lhs (stmt
, op0
);
9435 gimple_cond_set_rhs (stmt
, new_tree
);
9441 print_gimple_stmt (dump_file
, stmt
, 0, 0);
9442 fprintf (dump_file
, "\n");
9445 if (sop
&& issue_strict_overflow_warning (wc
))
9447 location_t location
= input_location
;
9448 if (gimple_has_location (stmt
))
9449 location
= gimple_location (stmt
);
9451 warning_at (location
, OPT_Wstrict_overflow
,
9452 "assuming signed overflow does not occur when "
9453 "simplifying conditional");
9459 /* Try again after inverting the condition. We only deal
9460 with integral types here, so no need to worry about
9461 issues with inverting FP comparisons. */
9463 new_tree
= test_for_singularity
9464 (invert_tree_comparison (cond_code
, false),
9465 op0
, op1
, vr
, &sop
);
9468 && (!sop
|| TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0
))))
9472 fprintf (dump_file
, "Simplified relational ");
9473 print_gimple_stmt (dump_file
, stmt
, 0, 0);
9474 fprintf (dump_file
, " into ");
9477 gimple_cond_set_code (stmt
, NE_EXPR
);
9478 gimple_cond_set_lhs (stmt
, op0
);
9479 gimple_cond_set_rhs (stmt
, new_tree
);
9485 print_gimple_stmt (dump_file
, stmt
, 0, 0);
9486 fprintf (dump_file
, "\n");
9489 if (sop
&& issue_strict_overflow_warning (wc
))
9491 location_t location
= input_location
;
9492 if (gimple_has_location (stmt
))
9493 location
= gimple_location (stmt
);
9495 warning_at (location
, OPT_Wstrict_overflow
,
9496 "assuming signed overflow does not occur when "
9497 "simplifying conditional");
9505 /* If we have a comparison of an SSA_NAME (OP0) against a constant,
9506 see if OP0 was set by a type conversion where the source of
9507 the conversion is another SSA_NAME with a range that fits
9508 into the range of OP0's type.
9510 If so, the conversion is redundant as the earlier SSA_NAME can be
9511 used for the comparison directly if we just massage the constant in the
9513 if (TREE_CODE (op0
) == SSA_NAME
9514 && TREE_CODE (op1
) == INTEGER_CST
)
9516 gimple def_stmt
= SSA_NAME_DEF_STMT (op0
);
9519 if (!is_gimple_assign (def_stmt
)
9520 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt
)))
9523 innerop
= gimple_assign_rhs1 (def_stmt
);
9525 if (TREE_CODE (innerop
) == SSA_NAME
9526 && !POINTER_TYPE_P (TREE_TYPE (innerop
)))
9528 value_range_t
*vr
= get_value_range (innerop
);
9530 if (range_int_cst_p (vr
)
9531 && range_fits_type_p (vr
,
9532 TYPE_PRECISION (TREE_TYPE (op0
)),
9533 TYPE_SIGN (TREE_TYPE (op0
)))
9534 && int_fits_type_p (op1
, TREE_TYPE (innerop
))
9535 /* The range must not have overflowed, or if it did overflow
9536 we must not be wrapping/trapping overflow and optimizing
9537 with strict overflow semantics. */
9538 && ((!is_negative_overflow_infinity (vr
->min
)
9539 && !is_positive_overflow_infinity (vr
->max
))
9540 || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (innerop
))))
9542 /* If the range overflowed and the user has asked for warnings
9543 when strict overflow semantics were used to optimize code,
9544 issue an appropriate warning. */
9545 if (cond_code
!= EQ_EXPR
&& cond_code
!= NE_EXPR
9546 && (is_negative_overflow_infinity (vr
->min
)
9547 || is_positive_overflow_infinity (vr
->max
))
9548 && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_CONDITIONAL
))
9550 location_t location
;
9552 if (!gimple_has_location (stmt
))
9553 location
= input_location
;
9555 location
= gimple_location (stmt
);
9556 warning_at (location
, OPT_Wstrict_overflow
,
9557 "assuming signed overflow does not occur when "
9558 "simplifying conditional");
9561 tree newconst
= fold_convert (TREE_TYPE (innerop
), op1
);
9562 gimple_cond_set_lhs (stmt
, innerop
);
9563 gimple_cond_set_rhs (stmt
, newconst
);
9572 /* Simplify a switch statement using the value range of the switch
9576 simplify_switch_using_ranges (gswitch
*stmt
)
9578 tree op
= gimple_switch_index (stmt
);
9583 size_t i
= 0, j
= 0, n
, n2
;
9586 size_t k
= 1, l
= 0;
9588 if (TREE_CODE (op
) == SSA_NAME
)
9590 vr
= get_value_range (op
);
9592 /* We can only handle integer ranges. */
9593 if ((vr
->type
!= VR_RANGE
9594 && vr
->type
!= VR_ANTI_RANGE
)
9595 || symbolic_range_p (vr
))
9598 /* Find case label for min/max of the value range. */
9599 take_default
= !find_case_label_ranges (stmt
, vr
, &i
, &j
, &k
, &l
);
9601 else if (TREE_CODE (op
) == INTEGER_CST
)
9603 take_default
= !find_case_label_index (stmt
, 1, op
, &i
);
9617 n
= gimple_switch_num_labels (stmt
);
9619 /* Bail out if this is just all edges taken. */
9625 /* Build a new vector of taken case labels. */
9626 vec2
= make_tree_vec (j
- i
+ 1 + l
- k
+ 1 + (int)take_default
);
9629 /* Add the default edge, if necessary. */
9631 TREE_VEC_ELT (vec2
, n2
++) = gimple_switch_default_label (stmt
);
9633 for (; i
<= j
; ++i
, ++n2
)
9634 TREE_VEC_ELT (vec2
, n2
) = gimple_switch_label (stmt
, i
);
9636 for (; k
<= l
; ++k
, ++n2
)
9637 TREE_VEC_ELT (vec2
, n2
) = gimple_switch_label (stmt
, k
);
9639 /* Mark needed edges. */
9640 for (i
= 0; i
< n2
; ++i
)
9642 e
= find_edge (gimple_bb (stmt
),
9643 label_to_block (CASE_LABEL (TREE_VEC_ELT (vec2
, i
))));
9644 e
->aux
= (void *)-1;
9647 /* Queue not needed edges for later removal. */
9648 FOR_EACH_EDGE (e
, ei
, gimple_bb (stmt
)->succs
)
9650 if (e
->aux
== (void *)-1)
9656 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
9658 fprintf (dump_file
, "removing unreachable case label\n");
9660 to_remove_edges
.safe_push (e
);
9661 e
->flags
&= ~EDGE_EXECUTABLE
;
9664 /* And queue an update for the stmt. */
9667 to_update_switch_stmts
.safe_push (su
);
9671 /* Simplify an integral conversion from an SSA name in STMT. */
9674 simplify_conversion_using_ranges (gimple stmt
)
9676 tree innerop
, middleop
, finaltype
;
9678 value_range_t
*innervr
;
9679 signop inner_sgn
, middle_sgn
, final_sgn
;
9680 unsigned inner_prec
, middle_prec
, final_prec
;
9681 widest_int innermin
, innermed
, innermax
, middlemin
, middlemed
, middlemax
;
9683 finaltype
= TREE_TYPE (gimple_assign_lhs (stmt
));
9684 if (!INTEGRAL_TYPE_P (finaltype
))
9686 middleop
= gimple_assign_rhs1 (stmt
);
9687 def_stmt
= SSA_NAME_DEF_STMT (middleop
);
9688 if (!is_gimple_assign (def_stmt
)
9689 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt
)))
9691 innerop
= gimple_assign_rhs1 (def_stmt
);
9692 if (TREE_CODE (innerop
) != SSA_NAME
9693 || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (innerop
))
9696 /* Get the value-range of the inner operand. */
9697 innervr
= get_value_range (innerop
);
9698 if (innervr
->type
!= VR_RANGE
9699 || TREE_CODE (innervr
->min
) != INTEGER_CST
9700 || TREE_CODE (innervr
->max
) != INTEGER_CST
)
9703 /* Simulate the conversion chain to check if the result is equal if
9704 the middle conversion is removed. */
9705 innermin
= wi::to_widest (innervr
->min
);
9706 innermax
= wi::to_widest (innervr
->max
);
9708 inner_prec
= TYPE_PRECISION (TREE_TYPE (innerop
));
9709 middle_prec
= TYPE_PRECISION (TREE_TYPE (middleop
));
9710 final_prec
= TYPE_PRECISION (finaltype
);
9712 /* If the first conversion is not injective, the second must not
9714 if (wi::gtu_p (innermax
- innermin
,
9715 wi::mask
<widest_int
> (middle_prec
, false))
9716 && middle_prec
< final_prec
)
9718 /* We also want a medium value so that we can track the effect that
9719 narrowing conversions with sign change have. */
9720 inner_sgn
= TYPE_SIGN (TREE_TYPE (innerop
));
9721 if (inner_sgn
== UNSIGNED
)
9722 innermed
= wi::shifted_mask
<widest_int
> (1, inner_prec
- 1, false);
9725 if (wi::cmp (innermin
, innermed
, inner_sgn
) >= 0
9726 || wi::cmp (innermed
, innermax
, inner_sgn
) >= 0)
9727 innermed
= innermin
;
9729 middle_sgn
= TYPE_SIGN (TREE_TYPE (middleop
));
9730 middlemin
= wi::ext (innermin
, middle_prec
, middle_sgn
);
9731 middlemed
= wi::ext (innermed
, middle_prec
, middle_sgn
);
9732 middlemax
= wi::ext (innermax
, middle_prec
, middle_sgn
);
9734 /* Require that the final conversion applied to both the original
9735 and the intermediate range produces the same result. */
9736 final_sgn
= TYPE_SIGN (finaltype
);
9737 if (wi::ext (middlemin
, final_prec
, final_sgn
)
9738 != wi::ext (innermin
, final_prec
, final_sgn
)
9739 || wi::ext (middlemed
, final_prec
, final_sgn
)
9740 != wi::ext (innermed
, final_prec
, final_sgn
)
9741 || wi::ext (middlemax
, final_prec
, final_sgn
)
9742 != wi::ext (innermax
, final_prec
, final_sgn
))
9745 gimple_assign_set_rhs1 (stmt
, innerop
);
9750 /* Simplify a conversion from integral SSA name to float in STMT. */
9753 simplify_float_conversion_using_ranges (gimple_stmt_iterator
*gsi
, gimple stmt
)
9755 tree rhs1
= gimple_assign_rhs1 (stmt
);
9756 value_range_t
*vr
= get_value_range (rhs1
);
9757 machine_mode fltmode
= TYPE_MODE (TREE_TYPE (gimple_assign_lhs (stmt
)));
9762 /* We can only handle constant ranges. */
9763 if (vr
->type
!= VR_RANGE
9764 || TREE_CODE (vr
->min
) != INTEGER_CST
9765 || TREE_CODE (vr
->max
) != INTEGER_CST
)
9768 /* First check if we can use a signed type in place of an unsigned. */
9769 if (TYPE_UNSIGNED (TREE_TYPE (rhs1
))
9770 && (can_float_p (fltmode
, TYPE_MODE (TREE_TYPE (rhs1
)), 0)
9771 != CODE_FOR_nothing
)
9772 && range_fits_type_p (vr
, TYPE_PRECISION (TREE_TYPE (rhs1
)), SIGNED
))
9773 mode
= TYPE_MODE (TREE_TYPE (rhs1
));
9774 /* If we can do the conversion in the current input mode do nothing. */
9775 else if (can_float_p (fltmode
, TYPE_MODE (TREE_TYPE (rhs1
)),
9776 TYPE_UNSIGNED (TREE_TYPE (rhs1
))) != CODE_FOR_nothing
)
9778 /* Otherwise search for a mode we can use, starting from the narrowest
9779 integer mode available. */
9782 mode
= GET_CLASS_NARROWEST_MODE (MODE_INT
);
9785 /* If we cannot do a signed conversion to float from mode
9786 or if the value-range does not fit in the signed type
9787 try with a wider mode. */
9788 if (can_float_p (fltmode
, mode
, 0) != CODE_FOR_nothing
9789 && range_fits_type_p (vr
, GET_MODE_PRECISION (mode
), SIGNED
))
9792 mode
= GET_MODE_WIDER_MODE (mode
);
9793 /* But do not widen the input. Instead leave that to the
9794 optabs expansion code. */
9795 if (GET_MODE_PRECISION (mode
) > TYPE_PRECISION (TREE_TYPE (rhs1
)))
9798 while (mode
!= VOIDmode
);
9799 if (mode
== VOIDmode
)
9803 /* It works, insert a truncation or sign-change before the
9804 float conversion. */
9805 tem
= make_ssa_name (build_nonstandard_integer_type
9806 (GET_MODE_PRECISION (mode
), 0));
9807 conv
= gimple_build_assign (tem
, NOP_EXPR
, rhs1
);
9808 gsi_insert_before (gsi
, conv
, GSI_SAME_STMT
);
9809 gimple_assign_set_rhs1 (stmt
, tem
);
9815 /* Simplify an internal fn call using ranges if possible. */
9818 simplify_internal_call_using_ranges (gimple_stmt_iterator
*gsi
, gimple stmt
)
9820 enum tree_code subcode
;
9821 bool is_ubsan
= false;
9823 switch (gimple_call_internal_fn (stmt
))
9825 case IFN_UBSAN_CHECK_ADD
:
9826 subcode
= PLUS_EXPR
;
9829 case IFN_UBSAN_CHECK_SUB
:
9830 subcode
= MINUS_EXPR
;
9833 case IFN_UBSAN_CHECK_MUL
:
9834 subcode
= MULT_EXPR
;
9837 case IFN_ADD_OVERFLOW
:
9838 subcode
= PLUS_EXPR
;
9840 case IFN_SUB_OVERFLOW
:
9841 subcode
= MINUS_EXPR
;
9843 case IFN_MUL_OVERFLOW
:
9844 subcode
= MULT_EXPR
;
9850 tree op0
= gimple_call_arg (stmt
, 0);
9851 tree op1
= gimple_call_arg (stmt
, 1);
9854 type
= TREE_TYPE (op0
);
9855 else if (gimple_call_lhs (stmt
) == NULL_TREE
)
9858 type
= TREE_TYPE (TREE_TYPE (gimple_call_lhs (stmt
)));
9859 if (!check_for_binary_op_overflow (subcode
, type
, op0
, op1
, &ovf
)
9860 || (is_ubsan
&& ovf
))
9864 location_t loc
= gimple_location (stmt
);
9866 g
= gimple_build_assign (gimple_call_lhs (stmt
), subcode
, op0
, op1
);
9869 int prec
= TYPE_PRECISION (type
);
9872 || !useless_type_conversion_p (type
, TREE_TYPE (op0
))
9873 || !useless_type_conversion_p (type
, TREE_TYPE (op1
)))
9874 utype
= build_nonstandard_integer_type (prec
, 1);
9875 if (TREE_CODE (op0
) == INTEGER_CST
)
9876 op0
= fold_convert (utype
, op0
);
9877 else if (!useless_type_conversion_p (utype
, TREE_TYPE (op0
)))
9879 g
= gimple_build_assign (make_ssa_name (utype
), NOP_EXPR
, op0
);
9880 gimple_set_location (g
, loc
);
9881 gsi_insert_before (gsi
, g
, GSI_SAME_STMT
);
9882 op0
= gimple_assign_lhs (g
);
9884 if (TREE_CODE (op1
) == INTEGER_CST
)
9885 op1
= fold_convert (utype
, op1
);
9886 else if (!useless_type_conversion_p (utype
, TREE_TYPE (op1
)))
9888 g
= gimple_build_assign (make_ssa_name (utype
), NOP_EXPR
, op1
);
9889 gimple_set_location (g
, loc
);
9890 gsi_insert_before (gsi
, g
, GSI_SAME_STMT
);
9891 op1
= gimple_assign_lhs (g
);
9893 g
= gimple_build_assign (make_ssa_name (utype
), subcode
, op0
, op1
);
9894 gimple_set_location (g
, loc
);
9895 gsi_insert_before (gsi
, g
, GSI_SAME_STMT
);
9898 g
= gimple_build_assign (make_ssa_name (type
), NOP_EXPR
,
9899 gimple_assign_lhs (g
));
9900 gimple_set_location (g
, loc
);
9901 gsi_insert_before (gsi
, g
, GSI_SAME_STMT
);
9903 g
= gimple_build_assign (gimple_call_lhs (stmt
), COMPLEX_EXPR
,
9904 gimple_assign_lhs (g
),
9905 build_int_cst (type
, ovf
));
9907 gimple_set_location (g
, loc
);
9908 gsi_replace (gsi
, g
, false);
9912 /* Simplify STMT using ranges if possible. */
9915 simplify_stmt_using_ranges (gimple_stmt_iterator
*gsi
)
9917 gimple stmt
= gsi_stmt (*gsi
);
9918 if (is_gimple_assign (stmt
))
9920 enum tree_code rhs_code
= gimple_assign_rhs_code (stmt
);
9921 tree rhs1
= gimple_assign_rhs1 (stmt
);
9927 /* Transform EQ_EXPR, NE_EXPR into BIT_XOR_EXPR or identity
9928 if the RHS is zero or one, and the LHS are known to be boolean
9930 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
9931 return simplify_truth_ops_using_ranges (gsi
, stmt
);
9934 /* Transform TRUNC_DIV_EXPR and TRUNC_MOD_EXPR into RSHIFT_EXPR
9935 and BIT_AND_EXPR respectively if the first operand is greater
9936 than zero and the second operand is an exact power of two.
9937 Also optimize TRUNC_MOD_EXPR away if the second operand is
9938 constant and the first operand already has the right value
9940 case TRUNC_DIV_EXPR
:
9941 case TRUNC_MOD_EXPR
:
9942 if (TREE_CODE (rhs1
) == SSA_NAME
9943 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
9944 return simplify_div_or_mod_using_ranges (stmt
);
9947 /* Transform ABS (X) into X or -X as appropriate. */
9949 if (TREE_CODE (rhs1
) == SSA_NAME
9950 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
9951 return simplify_abs_using_ranges (stmt
);
9956 /* Optimize away BIT_AND_EXPR and BIT_IOR_EXPR
9957 if all the bits being cleared are already cleared or
9958 all the bits being set are already set. */
9959 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
9960 return simplify_bit_ops_using_ranges (gsi
, stmt
);
9964 if (TREE_CODE (rhs1
) == SSA_NAME
9965 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
9966 return simplify_conversion_using_ranges (stmt
);
9970 if (TREE_CODE (rhs1
) == SSA_NAME
9971 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
9972 return simplify_float_conversion_using_ranges (gsi
, stmt
);
9977 return simplify_min_or_max_using_ranges (stmt
);
9984 else if (gimple_code (stmt
) == GIMPLE_COND
)
9985 return simplify_cond_using_ranges (as_a
<gcond
*> (stmt
));
9986 else if (gimple_code (stmt
) == GIMPLE_SWITCH
)
9987 return simplify_switch_using_ranges (as_a
<gswitch
*> (stmt
));
9988 else if (is_gimple_call (stmt
)
9989 && gimple_call_internal_p (stmt
))
9990 return simplify_internal_call_using_ranges (gsi
, stmt
);
9995 /* If the statement pointed by SI has a predicate whose value can be
9996 computed using the value range information computed by VRP, compute
9997 its value and return true. Otherwise, return false. */
10000 fold_predicate_in (gimple_stmt_iterator
*si
)
10002 bool assignment_p
= false;
10004 gimple stmt
= gsi_stmt (*si
);
10006 if (is_gimple_assign (stmt
)
10007 && TREE_CODE_CLASS (gimple_assign_rhs_code (stmt
)) == tcc_comparison
)
10009 assignment_p
= true;
10010 val
= vrp_evaluate_conditional (gimple_assign_rhs_code (stmt
),
10011 gimple_assign_rhs1 (stmt
),
10012 gimple_assign_rhs2 (stmt
),
10015 else if (gcond
*cond_stmt
= dyn_cast
<gcond
*> (stmt
))
10016 val
= vrp_evaluate_conditional (gimple_cond_code (cond_stmt
),
10017 gimple_cond_lhs (cond_stmt
),
10018 gimple_cond_rhs (cond_stmt
),
10026 val
= fold_convert (gimple_expr_type (stmt
), val
);
10030 fprintf (dump_file
, "Folding predicate ");
10031 print_gimple_expr (dump_file
, stmt
, 0, 0);
10032 fprintf (dump_file
, " to ");
10033 print_generic_expr (dump_file
, val
, 0);
10034 fprintf (dump_file
, "\n");
10037 if (is_gimple_assign (stmt
))
10038 gimple_assign_set_rhs_from_tree (si
, val
);
10041 gcc_assert (gimple_code (stmt
) == GIMPLE_COND
);
10042 gcond
*cond_stmt
= as_a
<gcond
*> (stmt
);
10043 if (integer_zerop (val
))
10044 gimple_cond_make_false (cond_stmt
);
10045 else if (integer_onep (val
))
10046 gimple_cond_make_true (cond_stmt
);
10048 gcc_unreachable ();
10057 /* Callback for substitute_and_fold folding the stmt at *SI. */
10060 vrp_fold_stmt (gimple_stmt_iterator
*si
)
10062 if (fold_predicate_in (si
))
10065 return simplify_stmt_using_ranges (si
);
10068 /* Unwindable const/copy equivalences. */
10069 const_and_copies
*equiv_stack
;
10071 /* A trivial wrapper so that we can present the generic jump threading
10072 code with a simple API for simplifying statements. STMT is the
10073 statement we want to simplify, WITHIN_STMT provides the location
10074 for any overflow warnings. */
10077 simplify_stmt_for_jump_threading (gimple stmt
, gimple within_stmt
)
10079 if (gcond
*cond_stmt
= dyn_cast
<gcond
*> (stmt
))
10080 return vrp_evaluate_conditional (gimple_cond_code (cond_stmt
),
10081 gimple_cond_lhs (cond_stmt
),
10082 gimple_cond_rhs (cond_stmt
),
10085 if (gassign
*assign_stmt
= dyn_cast
<gassign
*> (stmt
))
10087 value_range_t new_vr
= VR_INITIALIZER
;
10088 tree lhs
= gimple_assign_lhs (assign_stmt
);
10090 if (TREE_CODE (lhs
) == SSA_NAME
10091 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs
))
10092 || POINTER_TYPE_P (TREE_TYPE (lhs
))))
10094 extract_range_from_assignment (&new_vr
, assign_stmt
);
10095 if (range_int_cst_singleton_p (&new_vr
))
10103 /* Blocks which have more than one predecessor and more than
10104 one successor present jump threading opportunities, i.e.,
10105 when the block is reached from a specific predecessor, we
10106 may be able to determine which of the outgoing edges will
10107 be traversed. When this optimization applies, we are able
10108 to avoid conditionals at runtime and we may expose secondary
10109 optimization opportunities.
10111 This routine is effectively a driver for the generic jump
10112 threading code. It basically just presents the generic code
10113 with edges that may be suitable for jump threading.
10115 Unlike DOM, we do not iterate VRP if jump threading was successful.
10116 While iterating may expose new opportunities for VRP, it is expected
10117 those opportunities would be very limited and the compile time cost
10118 to expose those opportunities would be significant.
10120 As jump threading opportunities are discovered, they are registered
10121 for later realization. */
10124 identify_jump_threads (void)
10131 /* Ugh. When substituting values earlier in this pass we can
10132 wipe the dominance information. So rebuild the dominator
10133 information as we need it within the jump threading code. */
10134 calculate_dominance_info (CDI_DOMINATORS
);
10136 /* We do not allow VRP information to be used for jump threading
10137 across a back edge in the CFG. Otherwise it becomes too
10138 difficult to avoid eliminating loop exit tests. Of course
10139 EDGE_DFS_BACK is not accurate at this time so we have to
10141 mark_dfs_back_edges ();
10143 /* Do not thread across edges we are about to remove. Just marking
10144 them as EDGE_DFS_BACK will do. */
10145 FOR_EACH_VEC_ELT (to_remove_edges
, i
, e
)
10146 e
->flags
|= EDGE_DFS_BACK
;
10148 /* Allocate our unwinder stack to unwind any temporary equivalences
10149 that might be recorded. */
10150 equiv_stack
= new const_and_copies ();
10152 /* To avoid lots of silly node creation, we create a single
10153 conditional and just modify it in-place when attempting to
10155 dummy
= gimple_build_cond (EQ_EXPR
,
10156 integer_zero_node
, integer_zero_node
,
10159 /* Walk through all the blocks finding those which present a
10160 potential jump threading opportunity. We could set this up
10161 as a dominator walker and record data during the walk, but
10162 I doubt it's worth the effort for the classes of jump
10163 threading opportunities we are trying to identify at this
10164 point in compilation. */
10165 FOR_EACH_BB_FN (bb
, cfun
)
10169 /* If the generic jump threading code does not find this block
10170 interesting, then there is nothing to do. */
10171 if (! potentially_threadable_block (bb
))
10174 last
= last_stmt (bb
);
10176 /* We're basically looking for a switch or any kind of conditional with
10177 integral or pointer type arguments. Note the type of the second
10178 argument will be the same as the first argument, so no need to
10179 check it explicitly.
10181 We also handle the case where there are no statements in the
10182 block. This come up with forwarder blocks that are not
10183 optimized away because they lead to a loop header. But we do
10184 want to thread through them as we can sometimes thread to the
10185 loop exit which is obviously profitable. */
10187 || gimple_code (last
) == GIMPLE_SWITCH
10188 || (gimple_code (last
) == GIMPLE_COND
10189 && TREE_CODE (gimple_cond_lhs (last
)) == SSA_NAME
10190 && (INTEGRAL_TYPE_P (TREE_TYPE (gimple_cond_lhs (last
)))
10191 || POINTER_TYPE_P (TREE_TYPE (gimple_cond_lhs (last
))))
10192 && (TREE_CODE (gimple_cond_rhs (last
)) == SSA_NAME
10193 || is_gimple_min_invariant (gimple_cond_rhs (last
)))))
10197 /* We've got a block with multiple predecessors and multiple
10198 successors which also ends in a suitable conditional or
10199 switch statement. For each predecessor, see if we can thread
10200 it to a specific successor. */
10201 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
10203 /* Do not thread across back edges or abnormal edges
10205 if (e
->flags
& (EDGE_DFS_BACK
| EDGE_COMPLEX
))
10208 thread_across_edge (dummy
, e
, true, equiv_stack
,
10209 simplify_stmt_for_jump_threading
);
10214 /* We do not actually update the CFG or SSA graphs at this point as
10215 ASSERT_EXPRs are still in the IL and cfg cleanup code does not yet
10216 handle ASSERT_EXPRs gracefully. */
10219 /* We identified all the jump threading opportunities earlier, but could
10220 not transform the CFG at that time. This routine transforms the
10221 CFG and arranges for the dominator tree to be rebuilt if necessary.
10223 Note the SSA graph update will occur during the normal TODO
10224 processing by the pass manager. */
10226 finalize_jump_threads (void)
10228 thread_through_all_blocks (false);
10229 delete equiv_stack
;
10233 /* Traverse all the blocks folding conditionals with known ranges. */
10236 vrp_finalize (void)
10240 values_propagated
= true;
10244 fprintf (dump_file
, "\nValue ranges after VRP:\n\n");
10245 dump_all_value_ranges (dump_file
);
10246 fprintf (dump_file
, "\n");
10249 substitute_and_fold (op_with_constant_singleton_value_range
,
10250 vrp_fold_stmt
, false);
10252 if (warn_array_bounds
&& first_pass_instance
)
10253 check_all_array_refs ();
10255 /* We must identify jump threading opportunities before we release
10256 the datastructures built by VRP. */
10257 identify_jump_threads ();
10259 /* Set value range to non pointer SSA_NAMEs. */
10260 for (i
= 0; i
< num_vr_values
; i
++)
10263 tree name
= ssa_name (i
);
10266 || POINTER_TYPE_P (TREE_TYPE (name
))
10267 || (vr_value
[i
]->type
== VR_VARYING
)
10268 || (vr_value
[i
]->type
== VR_UNDEFINED
))
10271 if ((TREE_CODE (vr_value
[i
]->min
) == INTEGER_CST
)
10272 && (TREE_CODE (vr_value
[i
]->max
) == INTEGER_CST
)
10273 && (vr_value
[i
]->type
== VR_RANGE
10274 || vr_value
[i
]->type
== VR_ANTI_RANGE
))
10275 set_range_info (name
, vr_value
[i
]->type
, vr_value
[i
]->min
,
10279 /* Free allocated memory. */
10280 for (i
= 0; i
< num_vr_values
; i
++)
10283 BITMAP_FREE (vr_value
[i
]->equiv
);
10284 free (vr_value
[i
]);
10288 free (vr_phi_edge_counts
);
10290 /* So that we can distinguish between VRP data being available
10291 and not available. */
10293 vr_phi_edge_counts
= NULL
;
10297 /* Main entry point to VRP (Value Range Propagation). This pass is
10298 loosely based on J. R. C. Patterson, ``Accurate Static Branch
10299 Prediction by Value Range Propagation,'' in SIGPLAN Conference on
10300 Programming Language Design and Implementation, pp. 67-78, 1995.
10301 Also available at http://citeseer.ist.psu.edu/patterson95accurate.html
10303 This is essentially an SSA-CCP pass modified to deal with ranges
10304 instead of constants.
10306 While propagating ranges, we may find that two or more SSA name
10307 have equivalent, though distinct ranges. For instance,
10310 2 p_4 = ASSERT_EXPR <p_3, p_3 != 0>
10312 4 p_5 = ASSERT_EXPR <p_4, p_4 == q_2>;
10316 In the code above, pointer p_5 has range [q_2, q_2], but from the
10317 code we can also determine that p_5 cannot be NULL and, if q_2 had
10318 a non-varying range, p_5's range should also be compatible with it.
10320 These equivalences are created by two expressions: ASSERT_EXPR and
10321 copy operations. Since p_5 is an assertion on p_4, and p_4 was the
10322 result of another assertion, then we can use the fact that p_5 and
10323 p_4 are equivalent when evaluating p_5's range.
10325 Together with value ranges, we also propagate these equivalences
10326 between names so that we can take advantage of information from
10327 multiple ranges when doing final replacement. Note that this
10328 equivalency relation is transitive but not symmetric.
10330 In the example above, p_5 is equivalent to p_4, q_2 and p_3, but we
10331 cannot assert that q_2 is equivalent to p_5 because q_2 may be used
10332 in contexts where that assertion does not hold (e.g., in line 6).
10334 TODO, the main difference between this pass and Patterson's is that
10335 we do not propagate edge probabilities. We only compute whether
10336 edges can be taken or not. That is, instead of having a spectrum
10337 of jump probabilities between 0 and 1, we only deal with 0, 1 and
10338 DON'T KNOW. In the future, it may be worthwhile to propagate
10339 probabilities to aid branch prediction. */
10341 static unsigned int
10348 loop_optimizer_init (LOOPS_NORMAL
| LOOPS_HAVE_RECORDED_EXITS
);
10349 rewrite_into_loop_closed_ssa (NULL
, TODO_update_ssa
);
10350 scev_initialize ();
10352 /* ??? This ends up using stale EDGE_DFS_BACK for liveness computation.
10353 Inserting assertions may split edges which will invalidate
10355 insert_range_assertions ();
10357 to_remove_edges
.create (10);
10358 to_update_switch_stmts
.create (5);
10359 threadedge_initialize_values ();
10361 /* For visiting PHI nodes we need EDGE_DFS_BACK computed. */
10362 mark_dfs_back_edges ();
10365 ssa_propagate (vrp_visit_stmt
, vrp_visit_phi_node
);
10368 free_numbers_of_iterations_estimates ();
10370 /* ASSERT_EXPRs must be removed before finalizing jump threads
10371 as finalizing jump threads calls the CFG cleanup code which
10372 does not properly handle ASSERT_EXPRs. */
10373 remove_range_assertions ();
10375 /* If we exposed any new variables, go ahead and put them into
10376 SSA form now, before we handle jump threading. This simplifies
10377 interactions between rewriting of _DECL nodes into SSA form
10378 and rewriting SSA_NAME nodes into SSA form after block
10379 duplication and CFG manipulation. */
10380 update_ssa (TODO_update_ssa
);
10382 finalize_jump_threads ();
10384 /* Remove dead edges from SWITCH_EXPR optimization. This leaves the
10385 CFG in a broken state and requires a cfg_cleanup run. */
10386 FOR_EACH_VEC_ELT (to_remove_edges
, i
, e
)
10388 /* Update SWITCH_EXPR case label vector. */
10389 FOR_EACH_VEC_ELT (to_update_switch_stmts
, i
, su
)
10392 size_t n
= TREE_VEC_LENGTH (su
->vec
);
10394 gimple_switch_set_num_labels (su
->stmt
, n
);
10395 for (j
= 0; j
< n
; j
++)
10396 gimple_switch_set_label (su
->stmt
, j
, TREE_VEC_ELT (su
->vec
, j
));
10397 /* As we may have replaced the default label with a regular one
10398 make sure to make it a real default label again. This ensures
10399 optimal expansion. */
10400 label
= gimple_switch_label (su
->stmt
, 0);
10401 CASE_LOW (label
) = NULL_TREE
;
10402 CASE_HIGH (label
) = NULL_TREE
;
10405 if (to_remove_edges
.length () > 0)
10407 free_dominance_info (CDI_DOMINATORS
);
10408 loops_state_set (LOOPS_NEED_FIXUP
);
10411 to_remove_edges
.release ();
10412 to_update_switch_stmts
.release ();
10413 threadedge_finalize_values ();
10416 loop_optimizer_finalize ();
10422 const pass_data pass_data_vrp
=
10424 GIMPLE_PASS
, /* type */
10426 OPTGROUP_NONE
, /* optinfo_flags */
10427 TV_TREE_VRP
, /* tv_id */
10428 PROP_ssa
, /* properties_required */
10429 0, /* properties_provided */
10430 0, /* properties_destroyed */
10431 0, /* todo_flags_start */
10432 ( TODO_cleanup_cfg
| TODO_update_ssa
), /* todo_flags_finish */
10435 class pass_vrp
: public gimple_opt_pass
10438 pass_vrp (gcc::context
*ctxt
)
10439 : gimple_opt_pass (pass_data_vrp
, ctxt
)
10442 /* opt_pass methods: */
10443 opt_pass
* clone () { return new pass_vrp (m_ctxt
); }
10444 virtual bool gate (function
*) { return flag_tree_vrp
!= 0; }
10445 virtual unsigned int execute (function
*) { return execute_vrp (); }
10447 }; // class pass_vrp
10449 } // anon namespace
10452 make_pass_vrp (gcc::context
*ctxt
)
10454 return new pass_vrp (ctxt
);