1 /* Support routines for Value Range Propagation (VRP).
2 Copyright (C) 2005-2016 Free Software Foundation, Inc.
3 Contributed by Diego Novillo <dnovillo@redhat.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
23 #include "coretypes.h"
25 #include "insn-codes.h"
30 #include "tree-pass.h"
32 #include "optabs-tree.h"
33 #include "gimple-pretty-print.h"
34 #include "diagnostic-core.h"
36 #include "fold-const.h"
37 #include "stor-layout.h"
40 #include "gimple-fold.h"
42 #include "gimple-iterator.h"
43 #include "gimple-walk.h"
45 #include "tree-ssa-loop-manip.h"
46 #include "tree-ssa-loop-niter.h"
47 #include "tree-ssa-loop.h"
48 #include "tree-into-ssa.h"
52 #include "tree-scalar-evolution.h"
53 #include "tree-ssa-propagate.h"
54 #include "tree-chrec.h"
55 #include "tree-ssa-threadupdate.h"
56 #include "tree-ssa-scopedtables.h"
57 #include "tree-ssa-threadedge.h"
60 #include "case-cfn-macros.h"
62 #include "alloc-pool.h"
64 #include "tree-cfgcleanup.h"
66 #define VR_INITIALIZER { VR_UNDEFINED, NULL_TREE, NULL_TREE, NULL }
68 /* Allocation pools for tree-vrp allocations. */
69 static object_allocator
<value_range
> vrp_value_range_pool ("Tree VRP value ranges");
70 static bitmap_obstack vrp_equiv_obstack
;
72 /* Set of SSA names found live during the RPO traversal of the function
73 for still active basic-blocks. */
76 /* Return true if the SSA name NAME is live on the edge E. */
79 live_on_edge (edge e
, tree name
)
81 return (live
[e
->dest
->index
]
82 && bitmap_bit_p (live
[e
->dest
->index
], SSA_NAME_VERSION (name
)));
85 /* Local functions. */
86 static int compare_values (tree val1
, tree val2
);
87 static int compare_values_warnv (tree val1
, tree val2
, bool *);
88 static tree
vrp_evaluate_conditional_warnv_with_ops (enum tree_code
,
89 tree
, tree
, bool, bool *,
92 /* Location information for ASSERT_EXPRs. Each instance of this
93 structure describes an ASSERT_EXPR for an SSA name. Since a single
94 SSA name may have more than one assertion associated with it, these
95 locations are kept in a linked list attached to the corresponding
99 /* Basic block where the assertion would be inserted. */
102 /* Some assertions need to be inserted on an edge (e.g., assertions
103 generated by COND_EXPRs). In those cases, BB will be NULL. */
106 /* Pointer to the statement that generated this assertion. */
107 gimple_stmt_iterator si
;
109 /* Predicate code for the ASSERT_EXPR. Must be COMPARISON_CLASS_P. */
110 enum tree_code comp_code
;
112 /* Value being compared against. */
115 /* Expression to compare. */
118 /* Next node in the linked list. */
122 /* If bit I is present, it means that SSA name N_i has a list of
123 assertions that should be inserted in the IL. */
124 static bitmap need_assert_for
;
126 /* Array of locations lists where to insert assertions. ASSERTS_FOR[I]
127 holds a list of ASSERT_LOCUS_T nodes that describe where
128 ASSERT_EXPRs for SSA name N_I should be inserted. */
129 static assert_locus
**asserts_for
;
131 /* Value range array. After propagation, VR_VALUE[I] holds the range
132 of values that SSA name N_I may take. */
133 static unsigned num_vr_values
;
134 static value_range
**vr_value
;
135 static bool values_propagated
;
137 /* For a PHI node which sets SSA name N_I, VR_COUNTS[I] holds the
138 number of executable edges we saw the last time we visited the
140 static int *vr_phi_edge_counts
;
142 struct switch_update
{
147 static vec
<edge
> to_remove_edges
;
148 static vec
<switch_update
> to_update_switch_stmts
;
151 /* Return the maximum value for TYPE. */
154 vrp_val_max (const_tree type
)
156 if (!INTEGRAL_TYPE_P (type
))
159 return TYPE_MAX_VALUE (type
);
162 /* Return the minimum value for TYPE. */
165 vrp_val_min (const_tree type
)
167 if (!INTEGRAL_TYPE_P (type
))
170 return TYPE_MIN_VALUE (type
);
173 /* Return whether VAL is equal to the maximum value of its type. This
174 will be true for a positive overflow infinity. We can't do a
175 simple equality comparison with TYPE_MAX_VALUE because C typedefs
176 and Ada subtypes can produce types whose TYPE_MAX_VALUE is not ==
177 to the integer constant with the same value in the type. */
180 vrp_val_is_max (const_tree val
)
182 tree type_max
= vrp_val_max (TREE_TYPE (val
));
183 return (val
== type_max
184 || (type_max
!= NULL_TREE
185 && operand_equal_p (val
, type_max
, 0)));
188 /* Return whether VAL is equal to the minimum value of its type. This
189 will be true for a negative overflow infinity. */
192 vrp_val_is_min (const_tree val
)
194 tree type_min
= vrp_val_min (TREE_TYPE (val
));
195 return (val
== type_min
196 || (type_min
!= NULL_TREE
197 && operand_equal_p (val
, type_min
, 0)));
201 /* Return whether TYPE should use an overflow infinity distinct from
202 TYPE_{MIN,MAX}_VALUE. We use an overflow infinity value to
203 represent a signed overflow during VRP computations. An infinity
204 is distinct from a half-range, which will go from some number to
205 TYPE_{MIN,MAX}_VALUE. */
208 needs_overflow_infinity (const_tree type
)
210 return INTEGRAL_TYPE_P (type
) && !TYPE_OVERFLOW_WRAPS (type
);
213 /* Return whether TYPE can support our overflow infinity
214 representation: we use the TREE_OVERFLOW flag, which only exists
215 for constants. If TYPE doesn't support this, we don't optimize
216 cases which would require signed overflow--we drop them to
220 supports_overflow_infinity (const_tree type
)
222 tree min
= vrp_val_min (type
), max
= vrp_val_max (type
);
223 gcc_checking_assert (needs_overflow_infinity (type
));
224 return (min
!= NULL_TREE
225 && CONSTANT_CLASS_P (min
)
227 && CONSTANT_CLASS_P (max
));
230 /* VAL is the maximum or minimum value of a type. Return a
231 corresponding overflow infinity. */
234 make_overflow_infinity (tree val
)
236 gcc_checking_assert (val
!= NULL_TREE
&& CONSTANT_CLASS_P (val
));
237 val
= copy_node (val
);
238 TREE_OVERFLOW (val
) = 1;
242 /* Return a negative overflow infinity for TYPE. */
245 negative_overflow_infinity (tree type
)
247 gcc_checking_assert (supports_overflow_infinity (type
));
248 return make_overflow_infinity (vrp_val_min (type
));
251 /* Return a positive overflow infinity for TYPE. */
254 positive_overflow_infinity (tree type
)
256 gcc_checking_assert (supports_overflow_infinity (type
));
257 return make_overflow_infinity (vrp_val_max (type
));
260 /* Return whether VAL is a negative overflow infinity. */
263 is_negative_overflow_infinity (const_tree val
)
265 return (TREE_OVERFLOW_P (val
)
266 && needs_overflow_infinity (TREE_TYPE (val
))
267 && vrp_val_is_min (val
));
270 /* Return whether VAL is a positive overflow infinity. */
273 is_positive_overflow_infinity (const_tree val
)
275 return (TREE_OVERFLOW_P (val
)
276 && needs_overflow_infinity (TREE_TYPE (val
))
277 && vrp_val_is_max (val
));
280 /* Return whether VAL is a positive or negative overflow infinity. */
283 is_overflow_infinity (const_tree val
)
285 return (TREE_OVERFLOW_P (val
)
286 && needs_overflow_infinity (TREE_TYPE (val
))
287 && (vrp_val_is_min (val
) || vrp_val_is_max (val
)));
290 /* Return whether STMT has a constant rhs that is_overflow_infinity. */
293 stmt_overflow_infinity (gimple
*stmt
)
295 if (is_gimple_assign (stmt
)
296 && get_gimple_rhs_class (gimple_assign_rhs_code (stmt
)) ==
298 return is_overflow_infinity (gimple_assign_rhs1 (stmt
));
302 /* If VAL is now an overflow infinity, return VAL. Otherwise, return
303 the same value with TREE_OVERFLOW clear. This can be used to avoid
304 confusing a regular value with an overflow value. */
307 avoid_overflow_infinity (tree val
)
309 if (!is_overflow_infinity (val
))
312 if (vrp_val_is_max (val
))
313 return vrp_val_max (TREE_TYPE (val
));
316 gcc_checking_assert (vrp_val_is_min (val
));
317 return vrp_val_min (TREE_TYPE (val
));
322 /* Set value range VR to VR_UNDEFINED. */
325 set_value_range_to_undefined (value_range
*vr
)
327 vr
->type
= VR_UNDEFINED
;
328 vr
->min
= vr
->max
= NULL_TREE
;
330 bitmap_clear (vr
->equiv
);
334 /* Set value range VR to VR_VARYING. */
337 set_value_range_to_varying (value_range
*vr
)
339 vr
->type
= VR_VARYING
;
340 vr
->min
= vr
->max
= NULL_TREE
;
342 bitmap_clear (vr
->equiv
);
346 /* Set value range VR to {T, MIN, MAX, EQUIV}. */
349 set_value_range (value_range
*vr
, enum value_range_type t
, tree min
,
350 tree max
, bitmap equiv
)
352 /* Check the validity of the range. */
354 && (t
== VR_RANGE
|| t
== VR_ANTI_RANGE
))
358 gcc_assert (min
&& max
);
360 gcc_assert ((!TREE_OVERFLOW_P (min
) || is_overflow_infinity (min
))
361 && (!TREE_OVERFLOW_P (max
) || is_overflow_infinity (max
)));
363 if (INTEGRAL_TYPE_P (TREE_TYPE (min
)) && t
== VR_ANTI_RANGE
)
364 gcc_assert (!vrp_val_is_min (min
) || !vrp_val_is_max (max
));
366 cmp
= compare_values (min
, max
);
367 gcc_assert (cmp
== 0 || cmp
== -1 || cmp
== -2);
369 if (needs_overflow_infinity (TREE_TYPE (min
)))
370 gcc_assert (!is_overflow_infinity (min
)
371 || !is_overflow_infinity (max
));
375 && (t
== VR_UNDEFINED
|| t
== VR_VARYING
))
377 gcc_assert (min
== NULL_TREE
&& max
== NULL_TREE
);
378 gcc_assert (equiv
== NULL
|| bitmap_empty_p (equiv
));
385 /* Since updating the equivalence set involves deep copying the
386 bitmaps, only do it if absolutely necessary. */
387 if (vr
->equiv
== NULL
389 vr
->equiv
= BITMAP_ALLOC (&vrp_equiv_obstack
);
391 if (equiv
!= vr
->equiv
)
393 if (equiv
&& !bitmap_empty_p (equiv
))
394 bitmap_copy (vr
->equiv
, equiv
);
396 bitmap_clear (vr
->equiv
);
401 /* Set value range VR to the canonical form of {T, MIN, MAX, EQUIV}.
402 This means adjusting T, MIN and MAX representing the case of a
403 wrapping range with MAX < MIN covering [MIN, type_max] U [type_min, MAX]
404 as anti-rage ~[MAX+1, MIN-1]. Likewise for wrapping anti-ranges.
405 In corner cases where MAX+1 or MIN-1 wraps this will fall back
407 This routine exists to ease canonicalization in the case where we
408 extract ranges from var + CST op limit. */
411 set_and_canonicalize_value_range (value_range
*vr
, enum value_range_type t
,
412 tree min
, tree max
, bitmap equiv
)
414 /* Use the canonical setters for VR_UNDEFINED and VR_VARYING. */
415 if (t
== VR_UNDEFINED
)
417 set_value_range_to_undefined (vr
);
420 else if (t
== VR_VARYING
)
422 set_value_range_to_varying (vr
);
426 /* Nothing to canonicalize for symbolic ranges. */
427 if (TREE_CODE (min
) != INTEGER_CST
428 || TREE_CODE (max
) != INTEGER_CST
)
430 set_value_range (vr
, t
, min
, max
, equiv
);
434 /* Wrong order for min and max, to swap them and the VR type we need
436 if (tree_int_cst_lt (max
, min
))
440 /* For one bit precision if max < min, then the swapped
441 range covers all values, so for VR_RANGE it is varying and
442 for VR_ANTI_RANGE empty range, so drop to varying as well. */
443 if (TYPE_PRECISION (TREE_TYPE (min
)) == 1)
445 set_value_range_to_varying (vr
);
449 one
= build_int_cst (TREE_TYPE (min
), 1);
450 tmp
= int_const_binop (PLUS_EXPR
, max
, one
);
451 max
= int_const_binop (MINUS_EXPR
, min
, one
);
454 /* There's one corner case, if we had [C+1, C] before we now have
455 that again. But this represents an empty value range, so drop
456 to varying in this case. */
457 if (tree_int_cst_lt (max
, min
))
459 set_value_range_to_varying (vr
);
463 t
= t
== VR_RANGE
? VR_ANTI_RANGE
: VR_RANGE
;
466 /* Anti-ranges that can be represented as ranges should be so. */
467 if (t
== VR_ANTI_RANGE
)
469 bool is_min
= vrp_val_is_min (min
);
470 bool is_max
= vrp_val_is_max (max
);
472 if (is_min
&& is_max
)
474 /* We cannot deal with empty ranges, drop to varying.
475 ??? This could be VR_UNDEFINED instead. */
476 set_value_range_to_varying (vr
);
479 else if (TYPE_PRECISION (TREE_TYPE (min
)) == 1
480 && (is_min
|| is_max
))
482 /* Non-empty boolean ranges can always be represented
483 as a singleton range. */
485 min
= max
= vrp_val_max (TREE_TYPE (min
));
487 min
= max
= vrp_val_min (TREE_TYPE (min
));
491 /* As a special exception preserve non-null ranges. */
492 && !(TYPE_UNSIGNED (TREE_TYPE (min
))
493 && integer_zerop (max
)))
495 tree one
= build_int_cst (TREE_TYPE (max
), 1);
496 min
= int_const_binop (PLUS_EXPR
, max
, one
);
497 max
= vrp_val_max (TREE_TYPE (max
));
502 tree one
= build_int_cst (TREE_TYPE (min
), 1);
503 max
= int_const_binop (MINUS_EXPR
, min
, one
);
504 min
= vrp_val_min (TREE_TYPE (min
));
509 /* Drop [-INF(OVF), +INF(OVF)] to varying. */
510 if (needs_overflow_infinity (TREE_TYPE (min
))
511 && is_overflow_infinity (min
)
512 && is_overflow_infinity (max
))
514 set_value_range_to_varying (vr
);
518 set_value_range (vr
, t
, min
, max
, equiv
);
521 /* Copy value range FROM into value range TO. */
524 copy_value_range (value_range
*to
, value_range
*from
)
526 set_value_range (to
, from
->type
, from
->min
, from
->max
, from
->equiv
);
529 /* Set value range VR to a single value. This function is only called
530 with values we get from statements, and exists to clear the
531 TREE_OVERFLOW flag so that we don't think we have an overflow
532 infinity when we shouldn't. */
535 set_value_range_to_value (value_range
*vr
, tree val
, bitmap equiv
)
537 gcc_assert (is_gimple_min_invariant (val
));
538 if (TREE_OVERFLOW_P (val
))
539 val
= drop_tree_overflow (val
);
540 set_value_range (vr
, VR_RANGE
, val
, val
, equiv
);
543 /* Set value range VR to a non-negative range of type TYPE.
544 OVERFLOW_INFINITY indicates whether to use an overflow infinity
545 rather than TYPE_MAX_VALUE; this should be true if we determine
546 that the range is nonnegative based on the assumption that signed
547 overflow does not occur. */
550 set_value_range_to_nonnegative (value_range
*vr
, tree type
,
551 bool overflow_infinity
)
555 if (overflow_infinity
&& !supports_overflow_infinity (type
))
557 set_value_range_to_varying (vr
);
561 zero
= build_int_cst (type
, 0);
562 set_value_range (vr
, VR_RANGE
, zero
,
564 ? positive_overflow_infinity (type
)
565 : TYPE_MAX_VALUE (type
)),
569 /* Set value range VR to a non-NULL range of type TYPE. */
572 set_value_range_to_nonnull (value_range
*vr
, tree type
)
574 tree zero
= build_int_cst (type
, 0);
575 set_value_range (vr
, VR_ANTI_RANGE
, zero
, zero
, vr
->equiv
);
579 /* Set value range VR to a NULL range of type TYPE. */
582 set_value_range_to_null (value_range
*vr
, tree type
)
584 set_value_range_to_value (vr
, build_int_cst (type
, 0), vr
->equiv
);
588 /* Set value range VR to a range of a truthvalue of type TYPE. */
591 set_value_range_to_truthvalue (value_range
*vr
, tree type
)
593 if (TYPE_PRECISION (type
) == 1)
594 set_value_range_to_varying (vr
);
596 set_value_range (vr
, VR_RANGE
,
597 build_int_cst (type
, 0), build_int_cst (type
, 1),
602 /* If abs (min) < abs (max), set VR to [-max, max], if
603 abs (min) >= abs (max), set VR to [-min, min]. */
606 abs_extent_range (value_range
*vr
, tree min
, tree max
)
610 gcc_assert (TREE_CODE (min
) == INTEGER_CST
);
611 gcc_assert (TREE_CODE (max
) == INTEGER_CST
);
612 gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (min
)));
613 gcc_assert (!TYPE_UNSIGNED (TREE_TYPE (min
)));
614 min
= fold_unary (ABS_EXPR
, TREE_TYPE (min
), min
);
615 max
= fold_unary (ABS_EXPR
, TREE_TYPE (max
), max
);
616 if (TREE_OVERFLOW (min
) || TREE_OVERFLOW (max
))
618 set_value_range_to_varying (vr
);
621 cmp
= compare_values (min
, max
);
623 min
= fold_unary (NEGATE_EXPR
, TREE_TYPE (min
), max
);
624 else if (cmp
== 0 || cmp
== 1)
627 min
= fold_unary (NEGATE_EXPR
, TREE_TYPE (min
), min
);
631 set_value_range_to_varying (vr
);
634 set_and_canonicalize_value_range (vr
, VR_RANGE
, min
, max
, NULL
);
638 /* Return value range information for VAR.
640 If we have no values ranges recorded (ie, VRP is not running), then
641 return NULL. Otherwise create an empty range if none existed for VAR. */
644 get_value_range (const_tree var
)
646 static const value_range vr_const_varying
647 = { VR_VARYING
, NULL_TREE
, NULL_TREE
, NULL
};
650 unsigned ver
= SSA_NAME_VERSION (var
);
652 /* If we have no recorded ranges, then return NULL. */
656 /* If we query the range for a new SSA name return an unmodifiable VARYING.
657 We should get here at most from the substitute-and-fold stage which
658 will never try to change values. */
659 if (ver
>= num_vr_values
)
660 return CONST_CAST (value_range
*, &vr_const_varying
);
666 /* After propagation finished do not allocate new value-ranges. */
667 if (values_propagated
)
668 return CONST_CAST (value_range
*, &vr_const_varying
);
670 /* Create a default value range. */
671 vr_value
[ver
] = vr
= vrp_value_range_pool
.allocate ();
672 memset (vr
, 0, sizeof (*vr
));
674 /* Defer allocating the equivalence set. */
677 /* If VAR is a default definition of a parameter, the variable can
678 take any value in VAR's type. */
679 if (SSA_NAME_IS_DEFAULT_DEF (var
))
681 sym
= SSA_NAME_VAR (var
);
682 if (TREE_CODE (sym
) == PARM_DECL
)
684 /* Try to use the "nonnull" attribute to create ~[0, 0]
685 anti-ranges for pointers. Note that this is only valid with
686 default definitions of PARM_DECLs. */
687 if (POINTER_TYPE_P (TREE_TYPE (sym
))
688 && (nonnull_arg_p (sym
)
689 || get_ptr_nonnull (var
)))
690 set_value_range_to_nonnull (vr
, TREE_TYPE (sym
));
691 else if (INTEGRAL_TYPE_P (TREE_TYPE (sym
)))
694 value_range_type rtype
= get_range_info (var
, &min
, &max
);
695 if (rtype
== VR_RANGE
|| rtype
== VR_ANTI_RANGE
)
696 set_value_range (vr
, rtype
,
697 wide_int_to_tree (TREE_TYPE (var
), min
),
698 wide_int_to_tree (TREE_TYPE (var
), max
),
701 set_value_range_to_varying (vr
);
704 set_value_range_to_varying (vr
);
706 else if (TREE_CODE (sym
) == RESULT_DECL
707 && DECL_BY_REFERENCE (sym
))
708 set_value_range_to_nonnull (vr
, TREE_TYPE (sym
));
714 /* Set value-ranges of all SSA names defined by STMT to varying. */
717 set_defs_to_varying (gimple
*stmt
)
721 FOR_EACH_SSA_TREE_OPERAND (def
, stmt
, i
, SSA_OP_DEF
)
723 value_range
*vr
= get_value_range (def
);
724 /* Avoid writing to vr_const_varying get_value_range may return. */
725 if (vr
->type
!= VR_VARYING
)
726 set_value_range_to_varying (vr
);
731 /* Return true, if VAL1 and VAL2 are equal values for VRP purposes. */
734 vrp_operand_equal_p (const_tree val1
, const_tree val2
)
738 if (!val1
|| !val2
|| !operand_equal_p (val1
, val2
, 0))
740 return is_overflow_infinity (val1
) == is_overflow_infinity (val2
);
743 /* Return true, if the bitmaps B1 and B2 are equal. */
746 vrp_bitmap_equal_p (const_bitmap b1
, const_bitmap b2
)
749 || ((!b1
|| bitmap_empty_p (b1
))
750 && (!b2
|| bitmap_empty_p (b2
)))
752 && bitmap_equal_p (b1
, b2
)));
755 /* Update the value range and equivalence set for variable VAR to
756 NEW_VR. Return true if NEW_VR is different from VAR's previous
759 NOTE: This function assumes that NEW_VR is a temporary value range
760 object created for the sole purpose of updating VAR's range. The
761 storage used by the equivalence set from NEW_VR will be freed by
762 this function. Do not call update_value_range when NEW_VR
763 is the range object associated with another SSA name. */
766 update_value_range (const_tree var
, value_range
*new_vr
)
771 /* If there is a value-range on the SSA name from earlier analysis
773 if (INTEGRAL_TYPE_P (TREE_TYPE (var
)))
776 value_range_type rtype
= get_range_info (var
, &min
, &max
);
777 if (rtype
== VR_RANGE
|| rtype
== VR_ANTI_RANGE
)
780 /* Range info on SSA names doesn't carry overflow information
781 so make sure to preserve the overflow bit on the lattice. */
782 if (rtype
== VR_RANGE
783 && needs_overflow_infinity (TREE_TYPE (var
))
784 && (new_vr
->type
== VR_VARYING
785 || (new_vr
->type
== VR_RANGE
786 && is_negative_overflow_infinity (new_vr
->min
)))
787 && wi::eq_p (vrp_val_min (TREE_TYPE (var
)), min
))
788 nr_min
= negative_overflow_infinity (TREE_TYPE (var
));
790 nr_min
= wide_int_to_tree (TREE_TYPE (var
), min
);
791 if (rtype
== VR_RANGE
792 && needs_overflow_infinity (TREE_TYPE (var
))
793 && (new_vr
->type
== VR_VARYING
794 || (new_vr
->type
== VR_RANGE
795 && is_positive_overflow_infinity (new_vr
->max
)))
796 && wi::eq_p (vrp_val_max (TREE_TYPE (var
)), max
))
797 nr_max
= positive_overflow_infinity (TREE_TYPE (var
));
799 nr_max
= wide_int_to_tree (TREE_TYPE (var
), max
);
800 value_range nr
= VR_INITIALIZER
;
801 set_and_canonicalize_value_range (&nr
, rtype
, nr_min
, nr_max
, NULL
);
802 vrp_intersect_ranges (new_vr
, &nr
);
806 /* Update the value range, if necessary. */
807 old_vr
= get_value_range (var
);
808 is_new
= old_vr
->type
!= new_vr
->type
809 || !vrp_operand_equal_p (old_vr
->min
, new_vr
->min
)
810 || !vrp_operand_equal_p (old_vr
->max
, new_vr
->max
)
811 || !vrp_bitmap_equal_p (old_vr
->equiv
, new_vr
->equiv
);
815 /* Do not allow transitions up the lattice. The following
816 is slightly more awkward than just new_vr->type < old_vr->type
817 because VR_RANGE and VR_ANTI_RANGE need to be considered
818 the same. We may not have is_new when transitioning to
819 UNDEFINED. If old_vr->type is VARYING, we shouldn't be
821 if (new_vr
->type
== VR_UNDEFINED
)
823 BITMAP_FREE (new_vr
->equiv
);
824 set_value_range_to_varying (old_vr
);
825 set_value_range_to_varying (new_vr
);
829 set_value_range (old_vr
, new_vr
->type
, new_vr
->min
, new_vr
->max
,
833 BITMAP_FREE (new_vr
->equiv
);
839 /* Add VAR and VAR's equivalence set to EQUIV. This is the central
840 point where equivalence processing can be turned on/off. */
843 add_equivalence (bitmap
*equiv
, const_tree var
)
845 unsigned ver
= SSA_NAME_VERSION (var
);
846 value_range
*vr
= get_value_range (var
);
849 *equiv
= BITMAP_ALLOC (&vrp_equiv_obstack
);
850 bitmap_set_bit (*equiv
, ver
);
852 bitmap_ior_into (*equiv
, vr
->equiv
);
856 /* Return true if VR is ~[0, 0]. */
859 range_is_nonnull (value_range
*vr
)
861 return vr
->type
== VR_ANTI_RANGE
862 && integer_zerop (vr
->min
)
863 && integer_zerop (vr
->max
);
867 /* Return true if VR is [0, 0]. */
870 range_is_null (value_range
*vr
)
872 return vr
->type
== VR_RANGE
873 && integer_zerop (vr
->min
)
874 && integer_zerop (vr
->max
);
877 /* Return true if max and min of VR are INTEGER_CST. It's not necessary
881 range_int_cst_p (value_range
*vr
)
883 return (vr
->type
== VR_RANGE
884 && TREE_CODE (vr
->max
) == INTEGER_CST
885 && TREE_CODE (vr
->min
) == INTEGER_CST
);
888 /* Return true if VR is a INTEGER_CST singleton. */
891 range_int_cst_singleton_p (value_range
*vr
)
893 return (range_int_cst_p (vr
)
894 && !is_overflow_infinity (vr
->min
)
895 && !is_overflow_infinity (vr
->max
)
896 && tree_int_cst_equal (vr
->min
, vr
->max
));
899 /* Return true if value range VR involves at least one symbol. */
902 symbolic_range_p (value_range
*vr
)
904 return (!is_gimple_min_invariant (vr
->min
)
905 || !is_gimple_min_invariant (vr
->max
));
908 /* Return the single symbol (an SSA_NAME) contained in T if any, or NULL_TREE
909 otherwise. We only handle additive operations and set NEG to true if the
910 symbol is negated and INV to the invariant part, if any. */
913 get_single_symbol (tree t
, bool *neg
, tree
*inv
)
921 if (TREE_CODE (t
) == PLUS_EXPR
922 || TREE_CODE (t
) == POINTER_PLUS_EXPR
923 || TREE_CODE (t
) == MINUS_EXPR
)
925 if (is_gimple_min_invariant (TREE_OPERAND (t
, 0)))
927 neg_
= (TREE_CODE (t
) == MINUS_EXPR
);
928 inv_
= TREE_OPERAND (t
, 0);
929 t
= TREE_OPERAND (t
, 1);
931 else if (is_gimple_min_invariant (TREE_OPERAND (t
, 1)))
934 inv_
= TREE_OPERAND (t
, 1);
935 t
= TREE_OPERAND (t
, 0);
946 if (TREE_CODE (t
) == NEGATE_EXPR
)
948 t
= TREE_OPERAND (t
, 0);
952 if (TREE_CODE (t
) != SSA_NAME
)
960 /* The reverse operation: build a symbolic expression with TYPE
961 from symbol SYM, negated according to NEG, and invariant INV. */
964 build_symbolic_expr (tree type
, tree sym
, bool neg
, tree inv
)
966 const bool pointer_p
= POINTER_TYPE_P (type
);
970 t
= build1 (NEGATE_EXPR
, type
, t
);
972 if (integer_zerop (inv
))
975 return build2 (pointer_p
? POINTER_PLUS_EXPR
: PLUS_EXPR
, type
, t
, inv
);
978 /* Return true if value range VR involves exactly one symbol SYM. */
981 symbolic_range_based_on_p (value_range
*vr
, const_tree sym
)
983 bool neg
, min_has_symbol
, max_has_symbol
;
986 if (is_gimple_min_invariant (vr
->min
))
987 min_has_symbol
= false;
988 else if (get_single_symbol (vr
->min
, &neg
, &inv
) == sym
)
989 min_has_symbol
= true;
993 if (is_gimple_min_invariant (vr
->max
))
994 max_has_symbol
= false;
995 else if (get_single_symbol (vr
->max
, &neg
, &inv
) == sym
)
996 max_has_symbol
= true;
1000 return (min_has_symbol
|| max_has_symbol
);
1003 /* Return true if value range VR uses an overflow infinity. */
1006 overflow_infinity_range_p (value_range
*vr
)
1008 return (vr
->type
== VR_RANGE
1009 && (is_overflow_infinity (vr
->min
)
1010 || is_overflow_infinity (vr
->max
)));
1013 /* Return false if we can not make a valid comparison based on VR;
1014 this will be the case if it uses an overflow infinity and overflow
1015 is not undefined (i.e., -fno-strict-overflow is in effect).
1016 Otherwise return true, and set *STRICT_OVERFLOW_P to true if VR
1017 uses an overflow infinity. */
1020 usable_range_p (value_range
*vr
, bool *strict_overflow_p
)
1022 gcc_assert (vr
->type
== VR_RANGE
);
1023 if (is_overflow_infinity (vr
->min
))
1025 *strict_overflow_p
= true;
1026 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (vr
->min
)))
1029 if (is_overflow_infinity (vr
->max
))
1031 *strict_overflow_p
= true;
1032 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (vr
->max
)))
1038 /* Return true if the result of assignment STMT is know to be non-zero.
1039 If the return value is based on the assumption that signed overflow is
1040 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1041 *STRICT_OVERFLOW_P.*/
1044 gimple_assign_nonzero_warnv_p (gimple
*stmt
, bool *strict_overflow_p
)
1046 enum tree_code code
= gimple_assign_rhs_code (stmt
);
1047 switch (get_gimple_rhs_class (code
))
1049 case GIMPLE_UNARY_RHS
:
1050 return tree_unary_nonzero_warnv_p (gimple_assign_rhs_code (stmt
),
1051 gimple_expr_type (stmt
),
1052 gimple_assign_rhs1 (stmt
),
1054 case GIMPLE_BINARY_RHS
:
1055 return tree_binary_nonzero_warnv_p (gimple_assign_rhs_code (stmt
),
1056 gimple_expr_type (stmt
),
1057 gimple_assign_rhs1 (stmt
),
1058 gimple_assign_rhs2 (stmt
),
1060 case GIMPLE_TERNARY_RHS
:
1062 case GIMPLE_SINGLE_RHS
:
1063 return tree_single_nonzero_warnv_p (gimple_assign_rhs1 (stmt
),
1065 case GIMPLE_INVALID_RHS
:
1072 /* Return true if STMT is known to compute a non-zero value.
1073 If the return value is based on the assumption that signed overflow is
1074 undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't change
1075 *STRICT_OVERFLOW_P.*/
1078 gimple_stmt_nonzero_warnv_p (gimple
*stmt
, bool *strict_overflow_p
)
1080 switch (gimple_code (stmt
))
1083 return gimple_assign_nonzero_warnv_p (stmt
, strict_overflow_p
);
1086 tree fndecl
= gimple_call_fndecl (stmt
);
1087 if (!fndecl
) return false;
1088 if (flag_delete_null_pointer_checks
&& !flag_check_new
1089 && DECL_IS_OPERATOR_NEW (fndecl
)
1090 && !TREE_NOTHROW (fndecl
))
1092 /* References are always non-NULL. */
1093 if (flag_delete_null_pointer_checks
1094 && TREE_CODE (TREE_TYPE (fndecl
)) == REFERENCE_TYPE
)
1096 if (flag_delete_null_pointer_checks
&&
1097 lookup_attribute ("returns_nonnull",
1098 TYPE_ATTRIBUTES (gimple_call_fntype (stmt
))))
1100 return gimple_alloca_call_p (stmt
);
1107 /* Like tree_expr_nonzero_warnv_p, but this function uses value ranges
1111 vrp_stmt_computes_nonzero (gimple
*stmt
, bool *strict_overflow_p
)
1113 if (gimple_stmt_nonzero_warnv_p (stmt
, strict_overflow_p
))
1116 /* If we have an expression of the form &X->a, then the expression
1117 is nonnull if X is nonnull. */
1118 if (is_gimple_assign (stmt
)
1119 && gimple_assign_rhs_code (stmt
) == ADDR_EXPR
)
1121 tree expr
= gimple_assign_rhs1 (stmt
);
1122 tree base
= get_base_address (TREE_OPERAND (expr
, 0));
1124 if (base
!= NULL_TREE
1125 && TREE_CODE (base
) == MEM_REF
1126 && TREE_CODE (TREE_OPERAND (base
, 0)) == SSA_NAME
)
1128 value_range
*vr
= get_value_range (TREE_OPERAND (base
, 0));
1129 if (range_is_nonnull (vr
))
1137 /* Returns true if EXPR is a valid value (as expected by compare_values) --
1138 a gimple invariant, or SSA_NAME +- CST. */
1141 valid_value_p (tree expr
)
1143 if (TREE_CODE (expr
) == SSA_NAME
)
1146 if (TREE_CODE (expr
) == PLUS_EXPR
1147 || TREE_CODE (expr
) == MINUS_EXPR
)
1148 return (TREE_CODE (TREE_OPERAND (expr
, 0)) == SSA_NAME
1149 && TREE_CODE (TREE_OPERAND (expr
, 1)) == INTEGER_CST
);
1151 return is_gimple_min_invariant (expr
);
1157 -2 if those are incomparable. */
1159 operand_less_p (tree val
, tree val2
)
1161 /* LT is folded faster than GE and others. Inline the common case. */
1162 if (TREE_CODE (val
) == INTEGER_CST
&& TREE_CODE (val2
) == INTEGER_CST
)
1164 if (! is_positive_overflow_infinity (val2
))
1165 return tree_int_cst_lt (val
, val2
);
1171 fold_defer_overflow_warnings ();
1173 tcmp
= fold_binary_to_constant (LT_EXPR
, boolean_type_node
, val
, val2
);
1175 fold_undefer_and_ignore_overflow_warnings ();
1178 || TREE_CODE (tcmp
) != INTEGER_CST
)
1181 if (!integer_zerop (tcmp
))
1185 /* val >= val2, not considering overflow infinity. */
1186 if (is_negative_overflow_infinity (val
))
1187 return is_negative_overflow_infinity (val2
) ? 0 : 1;
1188 else if (is_positive_overflow_infinity (val2
))
1189 return is_positive_overflow_infinity (val
) ? 0 : 1;
1194 /* Compare two values VAL1 and VAL2. Return
1196 -2 if VAL1 and VAL2 cannot be compared at compile-time,
1199 +1 if VAL1 > VAL2, and
1202 This is similar to tree_int_cst_compare but supports pointer values
1203 and values that cannot be compared at compile time.
1205 If STRICT_OVERFLOW_P is not NULL, then set *STRICT_OVERFLOW_P to
1206 true if the return value is only valid if we assume that signed
1207 overflow is undefined. */
1210 compare_values_warnv (tree val1
, tree val2
, bool *strict_overflow_p
)
1215 /* Below we rely on the fact that VAL1 and VAL2 are both pointers or
1217 gcc_assert (POINTER_TYPE_P (TREE_TYPE (val1
))
1218 == POINTER_TYPE_P (TREE_TYPE (val2
)));
1220 /* Convert the two values into the same type. This is needed because
1221 sizetype causes sign extension even for unsigned types. */
1222 val2
= fold_convert (TREE_TYPE (val1
), val2
);
1223 STRIP_USELESS_TYPE_CONVERSION (val2
);
1225 const bool overflow_undefined
1226 = INTEGRAL_TYPE_P (TREE_TYPE (val1
))
1227 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (val1
));
1230 tree sym1
= get_single_symbol (val1
, &neg1
, &inv1
);
1231 tree sym2
= get_single_symbol (val2
, &neg2
, &inv2
);
1233 /* If VAL1 and VAL2 are of the form '[-]NAME [+ CST]', return -1 or +1
1234 accordingly. If VAL1 and VAL2 don't use the same name, return -2. */
1237 /* Both values must use the same name with the same sign. */
1238 if (sym1
!= sym2
|| neg1
!= neg2
)
1241 /* [-]NAME + CST == [-]NAME + CST. */
1245 /* If overflow is defined we cannot simplify more. */
1246 if (!overflow_undefined
)
1249 if (strict_overflow_p
!= NULL
1250 && (!inv1
|| !TREE_NO_WARNING (val1
))
1251 && (!inv2
|| !TREE_NO_WARNING (val2
)))
1252 *strict_overflow_p
= true;
1255 inv1
= build_int_cst (TREE_TYPE (val1
), 0);
1257 inv2
= build_int_cst (TREE_TYPE (val2
), 0);
1259 return compare_values_warnv (inv1
, inv2
, strict_overflow_p
);
1262 const bool cst1
= is_gimple_min_invariant (val1
);
1263 const bool cst2
= is_gimple_min_invariant (val2
);
1265 /* If one is of the form '[-]NAME + CST' and the other is constant, then
1266 it might be possible to say something depending on the constants. */
1267 if ((sym1
&& inv1
&& cst2
) || (sym2
&& inv2
&& cst1
))
1269 if (!overflow_undefined
)
1272 if (strict_overflow_p
!= NULL
1273 && (!sym1
|| !TREE_NO_WARNING (val1
))
1274 && (!sym2
|| !TREE_NO_WARNING (val2
)))
1275 *strict_overflow_p
= true;
1277 const signop sgn
= TYPE_SIGN (TREE_TYPE (val1
));
1278 tree cst
= cst1
? val1
: val2
;
1279 tree inv
= cst1
? inv2
: inv1
;
1281 /* Compute the difference between the constants. If it overflows or
1282 underflows, this means that we can trivially compare the NAME with
1283 it and, consequently, the two values with each other. */
1284 wide_int diff
= wi::sub (cst
, inv
);
1285 if (wi::cmp (0, inv
, sgn
) != wi::cmp (diff
, cst
, sgn
))
1287 const int res
= wi::cmp (cst
, inv
, sgn
);
1288 return cst1
? res
: -res
;
1294 /* We cannot say anything more for non-constants. */
1298 if (!POINTER_TYPE_P (TREE_TYPE (val1
)))
1300 /* We cannot compare overflowed values, except for overflow
1302 if (TREE_OVERFLOW (val1
) || TREE_OVERFLOW (val2
))
1304 if (strict_overflow_p
!= NULL
)
1305 *strict_overflow_p
= true;
1306 if (is_negative_overflow_infinity (val1
))
1307 return is_negative_overflow_infinity (val2
) ? 0 : -1;
1308 else if (is_negative_overflow_infinity (val2
))
1310 else if (is_positive_overflow_infinity (val1
))
1311 return is_positive_overflow_infinity (val2
) ? 0 : 1;
1312 else if (is_positive_overflow_infinity (val2
))
1317 return tree_int_cst_compare (val1
, val2
);
1323 /* First see if VAL1 and VAL2 are not the same. */
1324 if (val1
== val2
|| operand_equal_p (val1
, val2
, 0))
1327 /* If VAL1 is a lower address than VAL2, return -1. */
1328 if (operand_less_p (val1
, val2
) == 1)
1331 /* If VAL1 is a higher address than VAL2, return +1. */
1332 if (operand_less_p (val2
, val1
) == 1)
1335 /* If VAL1 is different than VAL2, return +2.
1336 For integer constants we either have already returned -1 or 1
1337 or they are equivalent. We still might succeed in proving
1338 something about non-trivial operands. */
1339 if (TREE_CODE (val1
) != INTEGER_CST
1340 || TREE_CODE (val2
) != INTEGER_CST
)
1342 t
= fold_binary_to_constant (NE_EXPR
, boolean_type_node
, val1
, val2
);
1343 if (t
&& integer_onep (t
))
1351 /* Compare values like compare_values_warnv, but treat comparisons of
1352 nonconstants which rely on undefined overflow as incomparable. */
1355 compare_values (tree val1
, tree val2
)
1361 ret
= compare_values_warnv (val1
, val2
, &sop
);
1363 && (!is_gimple_min_invariant (val1
) || !is_gimple_min_invariant (val2
)))
1369 /* Return 1 if VAL is inside value range MIN <= VAL <= MAX,
1370 0 if VAL is not inside [MIN, MAX],
1371 -2 if we cannot tell either way.
1373 Benchmark compile/20001226-1.c compilation time after changing this
1377 value_inside_range (tree val
, tree min
, tree max
)
1381 cmp1
= operand_less_p (val
, min
);
1387 cmp2
= operand_less_p (max
, val
);
1395 /* Return true if value ranges VR0 and VR1 have a non-empty
1398 Benchmark compile/20001226-1.c compilation time after changing this
1403 value_ranges_intersect_p (value_range
*vr0
, value_range
*vr1
)
1405 /* The value ranges do not intersect if the maximum of the first range is
1406 less than the minimum of the second range or vice versa.
1407 When those relations are unknown, we can't do any better. */
1408 if (operand_less_p (vr0
->max
, vr1
->min
) != 0)
1410 if (operand_less_p (vr1
->max
, vr0
->min
) != 0)
1416 /* Return 1 if [MIN, MAX] includes the value zero, 0 if it does not
1417 include the value zero, -2 if we cannot tell. */
1420 range_includes_zero_p (tree min
, tree max
)
1422 tree zero
= build_int_cst (TREE_TYPE (min
), 0);
1423 return value_inside_range (zero
, min
, max
);
1426 /* Return true if *VR is know to only contain nonnegative values. */
1429 value_range_nonnegative_p (value_range
*vr
)
1431 /* Testing for VR_ANTI_RANGE is not useful here as any anti-range
1432 which would return a useful value should be encoded as a
1434 if (vr
->type
== VR_RANGE
)
1436 int result
= compare_values (vr
->min
, integer_zero_node
);
1437 return (result
== 0 || result
== 1);
1443 /* If *VR has a value rante that is a single constant value return that,
1444 otherwise return NULL_TREE. */
1447 value_range_constant_singleton (value_range
*vr
)
1449 if (vr
->type
== VR_RANGE
1450 && vrp_operand_equal_p (vr
->min
, vr
->max
)
1451 && is_gimple_min_invariant (vr
->min
))
1457 /* If OP has a value range with a single constant value return that,
1458 otherwise return NULL_TREE. This returns OP itself if OP is a
1462 op_with_constant_singleton_value_range (tree op
)
1464 if (is_gimple_min_invariant (op
))
1467 if (TREE_CODE (op
) != SSA_NAME
)
1470 return value_range_constant_singleton (get_value_range (op
));
1473 /* Return true if op is in a boolean [0, 1] value-range. */
1476 op_with_boolean_value_range_p (tree op
)
1480 if (TYPE_PRECISION (TREE_TYPE (op
)) == 1)
1483 if (integer_zerop (op
)
1484 || integer_onep (op
))
1487 if (TREE_CODE (op
) != SSA_NAME
)
1490 vr
= get_value_range (op
);
1491 return (vr
->type
== VR_RANGE
1492 && integer_zerop (vr
->min
)
1493 && integer_onep (vr
->max
));
1496 /* Extract value range information for VAR when (OP COND_CODE LIMIT) is
1497 true and store it in *VR_P. */
1500 extract_range_for_var_from_comparison_expr (tree var
, enum tree_code cond_code
,
1501 tree op
, tree limit
,
1504 tree min
, max
, type
;
1505 value_range
*limit_vr
;
1506 limit
= avoid_overflow_infinity (limit
);
1507 type
= TREE_TYPE (var
);
1508 gcc_assert (limit
!= var
);
1510 /* For pointer arithmetic, we only keep track of pointer equality
1512 if (POINTER_TYPE_P (type
) && cond_code
!= NE_EXPR
&& cond_code
!= EQ_EXPR
)
1514 set_value_range_to_varying (vr_p
);
1518 /* If LIMIT is another SSA name and LIMIT has a range of its own,
1519 try to use LIMIT's range to avoid creating symbolic ranges
1521 limit_vr
= (TREE_CODE (limit
) == SSA_NAME
) ? get_value_range (limit
) : NULL
;
1523 /* LIMIT's range is only interesting if it has any useful information. */
1525 || limit_vr
->type
== VR_UNDEFINED
1526 || limit_vr
->type
== VR_VARYING
1527 || (symbolic_range_p (limit_vr
)
1528 && ! (limit_vr
->type
== VR_RANGE
1529 && (limit_vr
->min
== limit_vr
->max
1530 || operand_equal_p (limit_vr
->min
, limit_vr
->max
, 0)))))
1533 /* Initially, the new range has the same set of equivalences of
1534 VAR's range. This will be revised before returning the final
1535 value. Since assertions may be chained via mutually exclusive
1536 predicates, we will need to trim the set of equivalences before
1538 gcc_assert (vr_p
->equiv
== NULL
);
1539 add_equivalence (&vr_p
->equiv
, var
);
1541 /* Extract a new range based on the asserted comparison for VAR and
1542 LIMIT's value range. Notice that if LIMIT has an anti-range, we
1543 will only use it for equality comparisons (EQ_EXPR). For any
1544 other kind of assertion, we cannot derive a range from LIMIT's
1545 anti-range that can be used to describe the new range. For
1546 instance, ASSERT_EXPR <x_2, x_2 <= b_4>. If b_4 is ~[2, 10],
1547 then b_4 takes on the ranges [-INF, 1] and [11, +INF]. There is
1548 no single range for x_2 that could describe LE_EXPR, so we might
1549 as well build the range [b_4, +INF] for it.
1550 One special case we handle is extracting a range from a
1551 range test encoded as (unsigned)var + CST <= limit. */
1552 if (TREE_CODE (op
) == NOP_EXPR
1553 || TREE_CODE (op
) == PLUS_EXPR
)
1555 if (TREE_CODE (op
) == PLUS_EXPR
)
1557 min
= fold_build1 (NEGATE_EXPR
, TREE_TYPE (TREE_OPERAND (op
, 1)),
1558 TREE_OPERAND (op
, 1));
1559 max
= int_const_binop (PLUS_EXPR
, limit
, min
);
1560 op
= TREE_OPERAND (op
, 0);
1564 min
= build_int_cst (TREE_TYPE (var
), 0);
1568 /* Make sure to not set TREE_OVERFLOW on the final type
1569 conversion. We are willingly interpreting large positive
1570 unsigned values as negative signed values here. */
1571 min
= force_fit_type (TREE_TYPE (var
), wi::to_widest (min
), 0, false);
1572 max
= force_fit_type (TREE_TYPE (var
), wi::to_widest (max
), 0, false);
1574 /* We can transform a max, min range to an anti-range or
1575 vice-versa. Use set_and_canonicalize_value_range which does
1577 if (cond_code
== LE_EXPR
)
1578 set_and_canonicalize_value_range (vr_p
, VR_RANGE
,
1579 min
, max
, vr_p
->equiv
);
1580 else if (cond_code
== GT_EXPR
)
1581 set_and_canonicalize_value_range (vr_p
, VR_ANTI_RANGE
,
1582 min
, max
, vr_p
->equiv
);
1586 else if (cond_code
== EQ_EXPR
)
1588 enum value_range_type range_type
;
1592 range_type
= limit_vr
->type
;
1593 min
= limit_vr
->min
;
1594 max
= limit_vr
->max
;
1598 range_type
= VR_RANGE
;
1603 set_value_range (vr_p
, range_type
, min
, max
, vr_p
->equiv
);
1605 /* When asserting the equality VAR == LIMIT and LIMIT is another
1606 SSA name, the new range will also inherit the equivalence set
1608 if (TREE_CODE (limit
) == SSA_NAME
)
1609 add_equivalence (&vr_p
->equiv
, limit
);
1611 else if (cond_code
== NE_EXPR
)
1613 /* As described above, when LIMIT's range is an anti-range and
1614 this assertion is an inequality (NE_EXPR), then we cannot
1615 derive anything from the anti-range. For instance, if
1616 LIMIT's range was ~[0, 0], the assertion 'VAR != LIMIT' does
1617 not imply that VAR's range is [0, 0]. So, in the case of
1618 anti-ranges, we just assert the inequality using LIMIT and
1621 If LIMIT_VR is a range, we can only use it to build a new
1622 anti-range if LIMIT_VR is a single-valued range. For
1623 instance, if LIMIT_VR is [0, 1], the predicate
1624 VAR != [0, 1] does not mean that VAR's range is ~[0, 1].
1625 Rather, it means that for value 0 VAR should be ~[0, 0]
1626 and for value 1, VAR should be ~[1, 1]. We cannot
1627 represent these ranges.
1629 The only situation in which we can build a valid
1630 anti-range is when LIMIT_VR is a single-valued range
1631 (i.e., LIMIT_VR->MIN == LIMIT_VR->MAX). In that case,
1632 build the anti-range ~[LIMIT_VR->MIN, LIMIT_VR->MAX]. */
1634 && limit_vr
->type
== VR_RANGE
1635 && compare_values (limit_vr
->min
, limit_vr
->max
) == 0)
1637 min
= limit_vr
->min
;
1638 max
= limit_vr
->max
;
1642 /* In any other case, we cannot use LIMIT's range to build a
1643 valid anti-range. */
1647 /* If MIN and MAX cover the whole range for their type, then
1648 just use the original LIMIT. */
1649 if (INTEGRAL_TYPE_P (type
)
1650 && vrp_val_is_min (min
)
1651 && vrp_val_is_max (max
))
1654 set_and_canonicalize_value_range (vr_p
, VR_ANTI_RANGE
,
1655 min
, max
, vr_p
->equiv
);
1657 else if (cond_code
== LE_EXPR
|| cond_code
== LT_EXPR
)
1659 min
= TYPE_MIN_VALUE (type
);
1661 if (limit_vr
== NULL
|| limit_vr
->type
== VR_ANTI_RANGE
)
1665 /* If LIMIT_VR is of the form [N1, N2], we need to build the
1666 range [MIN, N2] for LE_EXPR and [MIN, N2 - 1] for
1668 max
= limit_vr
->max
;
1671 /* If the maximum value forces us to be out of bounds, simply punt.
1672 It would be pointless to try and do anything more since this
1673 all should be optimized away above us. */
1674 if ((cond_code
== LT_EXPR
1675 && compare_values (max
, min
) == 0)
1676 || is_overflow_infinity (max
))
1677 set_value_range_to_varying (vr_p
);
1680 /* For LT_EXPR, we create the range [MIN, MAX - 1]. */
1681 if (cond_code
== LT_EXPR
)
1683 if (TYPE_PRECISION (TREE_TYPE (max
)) == 1
1684 && !TYPE_UNSIGNED (TREE_TYPE (max
)))
1685 max
= fold_build2 (PLUS_EXPR
, TREE_TYPE (max
), max
,
1686 build_int_cst (TREE_TYPE (max
), -1));
1688 max
= fold_build2 (MINUS_EXPR
, TREE_TYPE (max
), max
,
1689 build_int_cst (TREE_TYPE (max
), 1));
1691 TREE_NO_WARNING (max
) = 1;
1694 set_value_range (vr_p
, VR_RANGE
, min
, max
, vr_p
->equiv
);
1697 else if (cond_code
== GE_EXPR
|| cond_code
== GT_EXPR
)
1699 max
= TYPE_MAX_VALUE (type
);
1701 if (limit_vr
== NULL
|| limit_vr
->type
== VR_ANTI_RANGE
)
1705 /* If LIMIT_VR is of the form [N1, N2], we need to build the
1706 range [N1, MAX] for GE_EXPR and [N1 + 1, MAX] for
1708 min
= limit_vr
->min
;
1711 /* If the minimum value forces us to be out of bounds, simply punt.
1712 It would be pointless to try and do anything more since this
1713 all should be optimized away above us. */
1714 if ((cond_code
== GT_EXPR
1715 && compare_values (min
, max
) == 0)
1716 || is_overflow_infinity (min
))
1717 set_value_range_to_varying (vr_p
);
1720 /* For GT_EXPR, we create the range [MIN + 1, MAX]. */
1721 if (cond_code
== GT_EXPR
)
1723 if (TYPE_PRECISION (TREE_TYPE (min
)) == 1
1724 && !TYPE_UNSIGNED (TREE_TYPE (min
)))
1725 min
= fold_build2 (MINUS_EXPR
, TREE_TYPE (min
), min
,
1726 build_int_cst (TREE_TYPE (min
), -1));
1728 min
= fold_build2 (PLUS_EXPR
, TREE_TYPE (min
), min
,
1729 build_int_cst (TREE_TYPE (min
), 1));
1731 TREE_NO_WARNING (min
) = 1;
1734 set_value_range (vr_p
, VR_RANGE
, min
, max
, vr_p
->equiv
);
1740 /* Finally intersect the new range with what we already know about var. */
1741 vrp_intersect_ranges (vr_p
, get_value_range (var
));
1744 /* Extract value range information from an ASSERT_EXPR EXPR and store
1748 extract_range_from_assert (value_range
*vr_p
, tree expr
)
1750 tree var
= ASSERT_EXPR_VAR (expr
);
1751 tree cond
= ASSERT_EXPR_COND (expr
);
1753 enum tree_code cond_code
;
1754 gcc_assert (COMPARISON_CLASS_P (cond
));
1756 /* Find VAR in the ASSERT_EXPR conditional. */
1757 if (var
== TREE_OPERAND (cond
, 0)
1758 || TREE_CODE (TREE_OPERAND (cond
, 0)) == PLUS_EXPR
1759 || TREE_CODE (TREE_OPERAND (cond
, 0)) == NOP_EXPR
)
1761 /* If the predicate is of the form VAR COMP LIMIT, then we just
1762 take LIMIT from the RHS and use the same comparison code. */
1763 cond_code
= TREE_CODE (cond
);
1764 limit
= TREE_OPERAND (cond
, 1);
1765 op
= TREE_OPERAND (cond
, 0);
1769 /* If the predicate is of the form LIMIT COMP VAR, then we need
1770 to flip around the comparison code to create the proper range
1772 cond_code
= swap_tree_comparison (TREE_CODE (cond
));
1773 limit
= TREE_OPERAND (cond
, 0);
1774 op
= TREE_OPERAND (cond
, 1);
1776 extract_range_for_var_from_comparison_expr (var
, cond_code
, op
,
1780 /* Extract range information from SSA name VAR and store it in VR. If
1781 VAR has an interesting range, use it. Otherwise, create the
1782 range [VAR, VAR] and return it. This is useful in situations where
1783 we may have conditionals testing values of VARYING names. For
1790 Even if y_5 is deemed VARYING, we can determine that x_3 > y_5 is
1794 extract_range_from_ssa_name (value_range
*vr
, tree var
)
1796 value_range
*var_vr
= get_value_range (var
);
1798 if (var_vr
->type
!= VR_VARYING
)
1799 copy_value_range (vr
, var_vr
);
1801 set_value_range (vr
, VR_RANGE
, var
, var
, NULL
);
1803 add_equivalence (&vr
->equiv
, var
);
1807 /* Wrapper around int_const_binop. If the operation overflows and we
1808 are not using wrapping arithmetic, then adjust the result to be
1809 -INF or +INF depending on CODE, VAL1 and VAL2. This can return
1810 NULL_TREE if we need to use an overflow infinity representation but
1811 the type does not support it. */
1814 vrp_int_const_binop (enum tree_code code
, tree val1
, tree val2
)
1818 res
= int_const_binop (code
, val1
, val2
);
1820 /* If we are using unsigned arithmetic, operate symbolically
1821 on -INF and +INF as int_const_binop only handles signed overflow. */
1822 if (TYPE_UNSIGNED (TREE_TYPE (val1
)))
1824 int checkz
= compare_values (res
, val1
);
1825 bool overflow
= false;
1827 /* Ensure that res = val1 [+*] val2 >= val1
1828 or that res = val1 - val2 <= val1. */
1829 if ((code
== PLUS_EXPR
1830 && !(checkz
== 1 || checkz
== 0))
1831 || (code
== MINUS_EXPR
1832 && !(checkz
== 0 || checkz
== -1)))
1836 /* Checking for multiplication overflow is done by dividing the
1837 output of the multiplication by the first input of the
1838 multiplication. If the result of that division operation is
1839 not equal to the second input of the multiplication, then the
1840 multiplication overflowed. */
1841 else if (code
== MULT_EXPR
&& !integer_zerop (val1
))
1843 tree tmp
= int_const_binop (TRUNC_DIV_EXPR
,
1846 int check
= compare_values (tmp
, val2
);
1854 res
= copy_node (res
);
1855 TREE_OVERFLOW (res
) = 1;
1859 else if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (val1
)))
1860 /* If the singed operation wraps then int_const_binop has done
1861 everything we want. */
1863 /* Signed division of -1/0 overflows and by the time it gets here
1864 returns NULL_TREE. */
1867 else if ((TREE_OVERFLOW (res
)
1868 && !TREE_OVERFLOW (val1
)
1869 && !TREE_OVERFLOW (val2
))
1870 || is_overflow_infinity (val1
)
1871 || is_overflow_infinity (val2
))
1873 /* If the operation overflowed but neither VAL1 nor VAL2 are
1874 overflown, return -INF or +INF depending on the operation
1875 and the combination of signs of the operands. */
1876 int sgn1
= tree_int_cst_sgn (val1
);
1877 int sgn2
= tree_int_cst_sgn (val2
);
1879 if (needs_overflow_infinity (TREE_TYPE (res
))
1880 && !supports_overflow_infinity (TREE_TYPE (res
)))
1883 /* We have to punt on adding infinities of different signs,
1884 since we can't tell what the sign of the result should be.
1885 Likewise for subtracting infinities of the same sign. */
1886 if (((code
== PLUS_EXPR
&& sgn1
!= sgn2
)
1887 || (code
== MINUS_EXPR
&& sgn1
== sgn2
))
1888 && is_overflow_infinity (val1
)
1889 && is_overflow_infinity (val2
))
1892 /* Don't try to handle division or shifting of infinities. */
1893 if ((code
== TRUNC_DIV_EXPR
1894 || code
== FLOOR_DIV_EXPR
1895 || code
== CEIL_DIV_EXPR
1896 || code
== EXACT_DIV_EXPR
1897 || code
== ROUND_DIV_EXPR
1898 || code
== RSHIFT_EXPR
)
1899 && (is_overflow_infinity (val1
)
1900 || is_overflow_infinity (val2
)))
1903 /* Notice that we only need to handle the restricted set of
1904 operations handled by extract_range_from_binary_expr.
1905 Among them, only multiplication, addition and subtraction
1906 can yield overflow without overflown operands because we
1907 are working with integral types only... except in the
1908 case VAL1 = -INF and VAL2 = -1 which overflows to +INF
1909 for division too. */
1911 /* For multiplication, the sign of the overflow is given
1912 by the comparison of the signs of the operands. */
1913 if ((code
== MULT_EXPR
&& sgn1
== sgn2
)
1914 /* For addition, the operands must be of the same sign
1915 to yield an overflow. Its sign is therefore that
1916 of one of the operands, for example the first. For
1917 infinite operands X + -INF is negative, not positive. */
1918 || (code
== PLUS_EXPR
1920 ? !is_negative_overflow_infinity (val2
)
1921 : is_positive_overflow_infinity (val2
)))
1922 /* For subtraction, non-infinite operands must be of
1923 different signs to yield an overflow. Its sign is
1924 therefore that of the first operand or the opposite of
1925 that of the second operand. A first operand of 0 counts
1926 as positive here, for the corner case 0 - (-INF), which
1927 overflows, but must yield +INF. For infinite operands 0
1928 - INF is negative, not positive. */
1929 || (code
== MINUS_EXPR
1931 ? !is_positive_overflow_infinity (val2
)
1932 : is_negative_overflow_infinity (val2
)))
1933 /* We only get in here with positive shift count, so the
1934 overflow direction is the same as the sign of val1.
1935 Actually rshift does not overflow at all, but we only
1936 handle the case of shifting overflowed -INF and +INF. */
1937 || (code
== RSHIFT_EXPR
1939 /* For division, the only case is -INF / -1 = +INF. */
1940 || code
== TRUNC_DIV_EXPR
1941 || code
== FLOOR_DIV_EXPR
1942 || code
== CEIL_DIV_EXPR
1943 || code
== EXACT_DIV_EXPR
1944 || code
== ROUND_DIV_EXPR
)
1945 return (needs_overflow_infinity (TREE_TYPE (res
))
1946 ? positive_overflow_infinity (TREE_TYPE (res
))
1947 : TYPE_MAX_VALUE (TREE_TYPE (res
)));
1949 return (needs_overflow_infinity (TREE_TYPE (res
))
1950 ? negative_overflow_infinity (TREE_TYPE (res
))
1951 : TYPE_MIN_VALUE (TREE_TYPE (res
)));
1958 /* For range VR compute two wide_int bitmasks. In *MAY_BE_NONZERO
1959 bitmask if some bit is unset, it means for all numbers in the range
1960 the bit is 0, otherwise it might be 0 or 1. In *MUST_BE_NONZERO
1961 bitmask if some bit is set, it means for all numbers in the range
1962 the bit is 1, otherwise it might be 0 or 1. */
1965 zero_nonzero_bits_from_vr (const tree expr_type
,
1967 wide_int
*may_be_nonzero
,
1968 wide_int
*must_be_nonzero
)
1970 *may_be_nonzero
= wi::minus_one (TYPE_PRECISION (expr_type
));
1971 *must_be_nonzero
= wi::zero (TYPE_PRECISION (expr_type
));
1972 if (!range_int_cst_p (vr
)
1973 || is_overflow_infinity (vr
->min
)
1974 || is_overflow_infinity (vr
->max
))
1977 if (range_int_cst_singleton_p (vr
))
1979 *may_be_nonzero
= vr
->min
;
1980 *must_be_nonzero
= *may_be_nonzero
;
1982 else if (tree_int_cst_sgn (vr
->min
) >= 0
1983 || tree_int_cst_sgn (vr
->max
) < 0)
1985 wide_int xor_mask
= wi::bit_xor (vr
->min
, vr
->max
);
1986 *may_be_nonzero
= wi::bit_or (vr
->min
, vr
->max
);
1987 *must_be_nonzero
= wi::bit_and (vr
->min
, vr
->max
);
1990 wide_int mask
= wi::mask (wi::floor_log2 (xor_mask
), false,
1991 may_be_nonzero
->get_precision ());
1992 *may_be_nonzero
= *may_be_nonzero
| mask
;
1993 *must_be_nonzero
= must_be_nonzero
->and_not (mask
);
2000 /* Create two value-ranges in *VR0 and *VR1 from the anti-range *AR
2001 so that *VR0 U *VR1 == *AR. Returns true if that is possible,
2002 false otherwise. If *AR can be represented with a single range
2003 *VR1 will be VR_UNDEFINED. */
2006 ranges_from_anti_range (value_range
*ar
,
2007 value_range
*vr0
, value_range
*vr1
)
2009 tree type
= TREE_TYPE (ar
->min
);
2011 vr0
->type
= VR_UNDEFINED
;
2012 vr1
->type
= VR_UNDEFINED
;
2014 if (ar
->type
!= VR_ANTI_RANGE
2015 || TREE_CODE (ar
->min
) != INTEGER_CST
2016 || TREE_CODE (ar
->max
) != INTEGER_CST
2017 || !vrp_val_min (type
)
2018 || !vrp_val_max (type
))
2021 if (!vrp_val_is_min (ar
->min
))
2023 vr0
->type
= VR_RANGE
;
2024 vr0
->min
= vrp_val_min (type
);
2025 vr0
->max
= wide_int_to_tree (type
, wi::sub (ar
->min
, 1));
2027 if (!vrp_val_is_max (ar
->max
))
2029 vr1
->type
= VR_RANGE
;
2030 vr1
->min
= wide_int_to_tree (type
, wi::add (ar
->max
, 1));
2031 vr1
->max
= vrp_val_max (type
);
2033 if (vr0
->type
== VR_UNDEFINED
)
2036 vr1
->type
= VR_UNDEFINED
;
2039 return vr0
->type
!= VR_UNDEFINED
;
2042 /* Helper to extract a value-range *VR for a multiplicative operation
2046 extract_range_from_multiplicative_op_1 (value_range
*vr
,
2047 enum tree_code code
,
2048 value_range
*vr0
, value_range
*vr1
)
2050 enum value_range_type type
;
2057 /* Multiplications, divisions and shifts are a bit tricky to handle,
2058 depending on the mix of signs we have in the two ranges, we
2059 need to operate on different values to get the minimum and
2060 maximum values for the new range. One approach is to figure
2061 out all the variations of range combinations and do the
2064 However, this involves several calls to compare_values and it
2065 is pretty convoluted. It's simpler to do the 4 operations
2066 (MIN0 OP MIN1, MIN0 OP MAX1, MAX0 OP MIN1 and MAX0 OP MAX0 OP
2067 MAX1) and then figure the smallest and largest values to form
2069 gcc_assert (code
== MULT_EXPR
2070 || code
== TRUNC_DIV_EXPR
2071 || code
== FLOOR_DIV_EXPR
2072 || code
== CEIL_DIV_EXPR
2073 || code
== EXACT_DIV_EXPR
2074 || code
== ROUND_DIV_EXPR
2075 || code
== RSHIFT_EXPR
2076 || code
== LSHIFT_EXPR
);
2077 gcc_assert ((vr0
->type
== VR_RANGE
2078 || (code
== MULT_EXPR
&& vr0
->type
== VR_ANTI_RANGE
))
2079 && vr0
->type
== vr1
->type
);
2083 /* Compute the 4 cross operations. */
2085 val
[0] = vrp_int_const_binop (code
, vr0
->min
, vr1
->min
);
2086 if (val
[0] == NULL_TREE
)
2089 if (vr1
->max
== vr1
->min
)
2093 val
[1] = vrp_int_const_binop (code
, vr0
->min
, vr1
->max
);
2094 if (val
[1] == NULL_TREE
)
2098 if (vr0
->max
== vr0
->min
)
2102 val
[2] = vrp_int_const_binop (code
, vr0
->max
, vr1
->min
);
2103 if (val
[2] == NULL_TREE
)
2107 if (vr0
->min
== vr0
->max
|| vr1
->min
== vr1
->max
)
2111 val
[3] = vrp_int_const_binop (code
, vr0
->max
, vr1
->max
);
2112 if (val
[3] == NULL_TREE
)
2118 set_value_range_to_varying (vr
);
2122 /* Set MIN to the minimum of VAL[i] and MAX to the maximum
2126 for (i
= 1; i
< 4; i
++)
2128 if (!is_gimple_min_invariant (min
)
2129 || (TREE_OVERFLOW (min
) && !is_overflow_infinity (min
))
2130 || !is_gimple_min_invariant (max
)
2131 || (TREE_OVERFLOW (max
) && !is_overflow_infinity (max
)))
2136 if (!is_gimple_min_invariant (val
[i
])
2137 || (TREE_OVERFLOW (val
[i
])
2138 && !is_overflow_infinity (val
[i
])))
2140 /* If we found an overflowed value, set MIN and MAX
2141 to it so that we set the resulting range to
2147 if (compare_values (val
[i
], min
) == -1)
2150 if (compare_values (val
[i
], max
) == 1)
2155 /* If either MIN or MAX overflowed, then set the resulting range to
2156 VARYING. But we do accept an overflow infinity
2158 if (min
== NULL_TREE
2159 || !is_gimple_min_invariant (min
)
2160 || (TREE_OVERFLOW (min
) && !is_overflow_infinity (min
))
2162 || !is_gimple_min_invariant (max
)
2163 || (TREE_OVERFLOW (max
) && !is_overflow_infinity (max
)))
2165 set_value_range_to_varying (vr
);
2171 2) [-INF, +-INF(OVF)]
2172 3) [+-INF(OVF), +INF]
2173 4) [+-INF(OVF), +-INF(OVF)]
2174 We learn nothing when we have INF and INF(OVF) on both sides.
2175 Note that we do accept [-INF, -INF] and [+INF, +INF] without
2177 if ((vrp_val_is_min (min
) || is_overflow_infinity (min
))
2178 && (vrp_val_is_max (max
) || is_overflow_infinity (max
)))
2180 set_value_range_to_varying (vr
);
2184 cmp
= compare_values (min
, max
);
2185 if (cmp
== -2 || cmp
== 1)
2187 /* If the new range has its limits swapped around (MIN > MAX),
2188 then the operation caused one of them to wrap around, mark
2189 the new range VARYING. */
2190 set_value_range_to_varying (vr
);
2193 set_value_range (vr
, type
, min
, max
, NULL
);
2196 /* Extract range information from a binary operation CODE based on
2197 the ranges of each of its operands *VR0 and *VR1 with resulting
2198 type EXPR_TYPE. The resulting range is stored in *VR. */
2201 extract_range_from_binary_expr_1 (value_range
*vr
,
2202 enum tree_code code
, tree expr_type
,
2203 value_range
*vr0_
, value_range
*vr1_
)
2205 value_range vr0
= *vr0_
, vr1
= *vr1_
;
2206 value_range vrtem0
= VR_INITIALIZER
, vrtem1
= VR_INITIALIZER
;
2207 enum value_range_type type
;
2208 tree min
= NULL_TREE
, max
= NULL_TREE
;
2211 if (!INTEGRAL_TYPE_P (expr_type
)
2212 && !POINTER_TYPE_P (expr_type
))
2214 set_value_range_to_varying (vr
);
2218 /* Not all binary expressions can be applied to ranges in a
2219 meaningful way. Handle only arithmetic operations. */
2220 if (code
!= PLUS_EXPR
2221 && code
!= MINUS_EXPR
2222 && code
!= POINTER_PLUS_EXPR
2223 && code
!= MULT_EXPR
2224 && code
!= TRUNC_DIV_EXPR
2225 && code
!= FLOOR_DIV_EXPR
2226 && code
!= CEIL_DIV_EXPR
2227 && code
!= EXACT_DIV_EXPR
2228 && code
!= ROUND_DIV_EXPR
2229 && code
!= TRUNC_MOD_EXPR
2230 && code
!= RSHIFT_EXPR
2231 && code
!= LSHIFT_EXPR
2234 && code
!= BIT_AND_EXPR
2235 && code
!= BIT_IOR_EXPR
2236 && code
!= BIT_XOR_EXPR
)
2238 set_value_range_to_varying (vr
);
2242 /* If both ranges are UNDEFINED, so is the result. */
2243 if (vr0
.type
== VR_UNDEFINED
&& vr1
.type
== VR_UNDEFINED
)
2245 set_value_range_to_undefined (vr
);
2248 /* If one of the ranges is UNDEFINED drop it to VARYING for the following
2249 code. At some point we may want to special-case operations that
2250 have UNDEFINED result for all or some value-ranges of the not UNDEFINED
2252 else if (vr0
.type
== VR_UNDEFINED
)
2253 set_value_range_to_varying (&vr0
);
2254 else if (vr1
.type
== VR_UNDEFINED
)
2255 set_value_range_to_varying (&vr1
);
2257 /* Now canonicalize anti-ranges to ranges when they are not symbolic
2258 and express ~[] op X as ([]' op X) U ([]'' op X). */
2259 if (vr0
.type
== VR_ANTI_RANGE
2260 && ranges_from_anti_range (&vr0
, &vrtem0
, &vrtem1
))
2262 extract_range_from_binary_expr_1 (vr
, code
, expr_type
, &vrtem0
, vr1_
);
2263 if (vrtem1
.type
!= VR_UNDEFINED
)
2265 value_range vrres
= VR_INITIALIZER
;
2266 extract_range_from_binary_expr_1 (&vrres
, code
, expr_type
,
2268 vrp_meet (vr
, &vrres
);
2272 /* Likewise for X op ~[]. */
2273 if (vr1
.type
== VR_ANTI_RANGE
2274 && ranges_from_anti_range (&vr1
, &vrtem0
, &vrtem1
))
2276 extract_range_from_binary_expr_1 (vr
, code
, expr_type
, vr0_
, &vrtem0
);
2277 if (vrtem1
.type
!= VR_UNDEFINED
)
2279 value_range vrres
= VR_INITIALIZER
;
2280 extract_range_from_binary_expr_1 (&vrres
, code
, expr_type
,
2282 vrp_meet (vr
, &vrres
);
2287 /* The type of the resulting value range defaults to VR0.TYPE. */
2290 /* Refuse to operate on VARYING ranges, ranges of different kinds
2291 and symbolic ranges. As an exception, we allow BIT_{AND,IOR}
2292 because we may be able to derive a useful range even if one of
2293 the operands is VR_VARYING or symbolic range. Similarly for
2294 divisions, MIN/MAX and PLUS/MINUS.
2296 TODO, we may be able to derive anti-ranges in some cases. */
2297 if (code
!= BIT_AND_EXPR
2298 && code
!= BIT_IOR_EXPR
2299 && code
!= TRUNC_DIV_EXPR
2300 && code
!= FLOOR_DIV_EXPR
2301 && code
!= CEIL_DIV_EXPR
2302 && code
!= EXACT_DIV_EXPR
2303 && code
!= ROUND_DIV_EXPR
2304 && code
!= TRUNC_MOD_EXPR
2307 && code
!= PLUS_EXPR
2308 && code
!= MINUS_EXPR
2309 && code
!= RSHIFT_EXPR
2310 && (vr0
.type
== VR_VARYING
2311 || vr1
.type
== VR_VARYING
2312 || vr0
.type
!= vr1
.type
2313 || symbolic_range_p (&vr0
)
2314 || symbolic_range_p (&vr1
)))
2316 set_value_range_to_varying (vr
);
2320 /* Now evaluate the expression to determine the new range. */
2321 if (POINTER_TYPE_P (expr_type
))
2323 if (code
== MIN_EXPR
|| code
== MAX_EXPR
)
2325 /* For MIN/MAX expressions with pointers, we only care about
2326 nullness, if both are non null, then the result is nonnull.
2327 If both are null, then the result is null. Otherwise they
2329 if (range_is_nonnull (&vr0
) && range_is_nonnull (&vr1
))
2330 set_value_range_to_nonnull (vr
, expr_type
);
2331 else if (range_is_null (&vr0
) && range_is_null (&vr1
))
2332 set_value_range_to_null (vr
, expr_type
);
2334 set_value_range_to_varying (vr
);
2336 else if (code
== POINTER_PLUS_EXPR
)
2338 /* For pointer types, we are really only interested in asserting
2339 whether the expression evaluates to non-NULL. */
2340 if (range_is_nonnull (&vr0
) || range_is_nonnull (&vr1
))
2341 set_value_range_to_nonnull (vr
, expr_type
);
2342 else if (range_is_null (&vr0
) && range_is_null (&vr1
))
2343 set_value_range_to_null (vr
, expr_type
);
2345 set_value_range_to_varying (vr
);
2347 else if (code
== BIT_AND_EXPR
)
2349 /* For pointer types, we are really only interested in asserting
2350 whether the expression evaluates to non-NULL. */
2351 if (range_is_nonnull (&vr0
) && range_is_nonnull (&vr1
))
2352 set_value_range_to_nonnull (vr
, expr_type
);
2353 else if (range_is_null (&vr0
) || range_is_null (&vr1
))
2354 set_value_range_to_null (vr
, expr_type
);
2356 set_value_range_to_varying (vr
);
2359 set_value_range_to_varying (vr
);
2364 /* For integer ranges, apply the operation to each end of the
2365 range and see what we end up with. */
2366 if (code
== PLUS_EXPR
|| code
== MINUS_EXPR
)
2368 const bool minus_p
= (code
== MINUS_EXPR
);
2369 tree min_op0
= vr0
.min
;
2370 tree min_op1
= minus_p
? vr1
.max
: vr1
.min
;
2371 tree max_op0
= vr0
.max
;
2372 tree max_op1
= minus_p
? vr1
.min
: vr1
.max
;
2373 tree sym_min_op0
= NULL_TREE
;
2374 tree sym_min_op1
= NULL_TREE
;
2375 tree sym_max_op0
= NULL_TREE
;
2376 tree sym_max_op1
= NULL_TREE
;
2377 bool neg_min_op0
, neg_min_op1
, neg_max_op0
, neg_max_op1
;
2379 /* If we have a PLUS or MINUS with two VR_RANGEs, either constant or
2380 single-symbolic ranges, try to compute the precise resulting range,
2381 but only if we know that this resulting range will also be constant
2382 or single-symbolic. */
2383 if (vr0
.type
== VR_RANGE
&& vr1
.type
== VR_RANGE
2384 && (TREE_CODE (min_op0
) == INTEGER_CST
2386 = get_single_symbol (min_op0
, &neg_min_op0
, &min_op0
)))
2387 && (TREE_CODE (min_op1
) == INTEGER_CST
2389 = get_single_symbol (min_op1
, &neg_min_op1
, &min_op1
)))
2390 && (!(sym_min_op0
&& sym_min_op1
)
2391 || (sym_min_op0
== sym_min_op1
2392 && neg_min_op0
== (minus_p
? neg_min_op1
: !neg_min_op1
)))
2393 && (TREE_CODE (max_op0
) == INTEGER_CST
2395 = get_single_symbol (max_op0
, &neg_max_op0
, &max_op0
)))
2396 && (TREE_CODE (max_op1
) == INTEGER_CST
2398 = get_single_symbol (max_op1
, &neg_max_op1
, &max_op1
)))
2399 && (!(sym_max_op0
&& sym_max_op1
)
2400 || (sym_max_op0
== sym_max_op1
2401 && neg_max_op0
== (minus_p
? neg_max_op1
: !neg_max_op1
))))
2403 const signop sgn
= TYPE_SIGN (expr_type
);
2404 const unsigned int prec
= TYPE_PRECISION (expr_type
);
2405 wide_int type_min
, type_max
, wmin
, wmax
;
2409 /* Get the lower and upper bounds of the type. */
2410 if (TYPE_OVERFLOW_WRAPS (expr_type
))
2412 type_min
= wi::min_value (prec
, sgn
);
2413 type_max
= wi::max_value (prec
, sgn
);
2417 type_min
= vrp_val_min (expr_type
);
2418 type_max
= vrp_val_max (expr_type
);
2421 /* Combine the lower bounds, if any. */
2422 if (min_op0
&& min_op1
)
2426 wmin
= wi::sub (min_op0
, min_op1
);
2428 /* Check for overflow. */
2429 if (wi::cmp (0, min_op1
, sgn
)
2430 != wi::cmp (wmin
, min_op0
, sgn
))
2431 min_ovf
= wi::cmp (min_op0
, min_op1
, sgn
);
2435 wmin
= wi::add (min_op0
, min_op1
);
2437 /* Check for overflow. */
2438 if (wi::cmp (min_op1
, 0, sgn
)
2439 != wi::cmp (wmin
, min_op0
, sgn
))
2440 min_ovf
= wi::cmp (min_op0
, wmin
, sgn
);
2446 wmin
= minus_p
? wi::neg (min_op1
) : min_op1
;
2448 wmin
= wi::shwi (0, prec
);
2450 /* Combine the upper bounds, if any. */
2451 if (max_op0
&& max_op1
)
2455 wmax
= wi::sub (max_op0
, max_op1
);
2457 /* Check for overflow. */
2458 if (wi::cmp (0, max_op1
, sgn
)
2459 != wi::cmp (wmax
, max_op0
, sgn
))
2460 max_ovf
= wi::cmp (max_op0
, max_op1
, sgn
);
2464 wmax
= wi::add (max_op0
, max_op1
);
2466 if (wi::cmp (max_op1
, 0, sgn
)
2467 != wi::cmp (wmax
, max_op0
, sgn
))
2468 max_ovf
= wi::cmp (max_op0
, wmax
, sgn
);
2474 wmax
= minus_p
? wi::neg (max_op1
) : max_op1
;
2476 wmax
= wi::shwi (0, prec
);
2478 /* Check for type overflow. */
2481 if (wi::cmp (wmin
, type_min
, sgn
) == -1)
2483 else if (wi::cmp (wmin
, type_max
, sgn
) == 1)
2488 if (wi::cmp (wmax
, type_min
, sgn
) == -1)
2490 else if (wi::cmp (wmax
, type_max
, sgn
) == 1)
2494 /* If we have overflow for the constant part and the resulting
2495 range will be symbolic, drop to VR_VARYING. */
2496 if ((min_ovf
&& sym_min_op0
!= sym_min_op1
)
2497 || (max_ovf
&& sym_max_op0
!= sym_max_op1
))
2499 set_value_range_to_varying (vr
);
2503 if (TYPE_OVERFLOW_WRAPS (expr_type
))
2505 /* If overflow wraps, truncate the values and adjust the
2506 range kind and bounds appropriately. */
2507 wide_int tmin
= wide_int::from (wmin
, prec
, sgn
);
2508 wide_int tmax
= wide_int::from (wmax
, prec
, sgn
);
2509 if (min_ovf
== max_ovf
)
2511 /* No overflow or both overflow or underflow. The
2512 range kind stays VR_RANGE. */
2513 min
= wide_int_to_tree (expr_type
, tmin
);
2514 max
= wide_int_to_tree (expr_type
, tmax
);
2516 else if ((min_ovf
== -1 && max_ovf
== 0)
2517 || (max_ovf
== 1 && min_ovf
== 0))
2519 /* Min underflow or max overflow. The range kind
2520 changes to VR_ANTI_RANGE. */
2521 bool covers
= false;
2522 wide_int tem
= tmin
;
2523 type
= VR_ANTI_RANGE
;
2525 if (wi::cmp (tmin
, tmax
, sgn
) < 0)
2528 if (wi::cmp (tmax
, tem
, sgn
) > 0)
2530 /* If the anti-range would cover nothing, drop to varying.
2531 Likewise if the anti-range bounds are outside of the
2533 if (covers
|| wi::cmp (tmin
, tmax
, sgn
) > 0)
2535 set_value_range_to_varying (vr
);
2538 min
= wide_int_to_tree (expr_type
, tmin
);
2539 max
= wide_int_to_tree (expr_type
, tmax
);
2543 /* Other underflow and/or overflow, drop to VR_VARYING. */
2544 set_value_range_to_varying (vr
);
2550 /* If overflow does not wrap, saturate to the types min/max
2554 if (needs_overflow_infinity (expr_type
)
2555 && supports_overflow_infinity (expr_type
))
2556 min
= negative_overflow_infinity (expr_type
);
2558 min
= wide_int_to_tree (expr_type
, type_min
);
2560 else if (min_ovf
== 1)
2562 if (needs_overflow_infinity (expr_type
)
2563 && supports_overflow_infinity (expr_type
))
2564 min
= positive_overflow_infinity (expr_type
);
2566 min
= wide_int_to_tree (expr_type
, type_max
);
2569 min
= wide_int_to_tree (expr_type
, wmin
);
2573 if (needs_overflow_infinity (expr_type
)
2574 && supports_overflow_infinity (expr_type
))
2575 max
= negative_overflow_infinity (expr_type
);
2577 max
= wide_int_to_tree (expr_type
, type_min
);
2579 else if (max_ovf
== 1)
2581 if (needs_overflow_infinity (expr_type
)
2582 && supports_overflow_infinity (expr_type
))
2583 max
= positive_overflow_infinity (expr_type
);
2585 max
= wide_int_to_tree (expr_type
, type_max
);
2588 max
= wide_int_to_tree (expr_type
, wmax
);
2591 if (needs_overflow_infinity (expr_type
)
2592 && supports_overflow_infinity (expr_type
))
2594 if ((min_op0
&& is_negative_overflow_infinity (min_op0
))
2597 ? is_positive_overflow_infinity (min_op1
)
2598 : is_negative_overflow_infinity (min_op1
))))
2599 min
= negative_overflow_infinity (expr_type
);
2600 if ((max_op0
&& is_positive_overflow_infinity (max_op0
))
2603 ? is_negative_overflow_infinity (max_op1
)
2604 : is_positive_overflow_infinity (max_op1
))))
2605 max
= positive_overflow_infinity (expr_type
);
2608 /* If the result lower bound is constant, we're done;
2609 otherwise, build the symbolic lower bound. */
2610 if (sym_min_op0
== sym_min_op1
)
2612 else if (sym_min_op0
)
2613 min
= build_symbolic_expr (expr_type
, sym_min_op0
,
2615 else if (sym_min_op1
)
2616 min
= build_symbolic_expr (expr_type
, sym_min_op1
,
2617 neg_min_op1
^ minus_p
, min
);
2619 /* Likewise for the upper bound. */
2620 if (sym_max_op0
== sym_max_op1
)
2622 else if (sym_max_op0
)
2623 max
= build_symbolic_expr (expr_type
, sym_max_op0
,
2625 else if (sym_max_op1
)
2626 max
= build_symbolic_expr (expr_type
, sym_max_op1
,
2627 neg_max_op1
^ minus_p
, max
);
2631 /* For other cases, for example if we have a PLUS_EXPR with two
2632 VR_ANTI_RANGEs, drop to VR_VARYING. It would take more effort
2633 to compute a precise range for such a case.
2634 ??? General even mixed range kind operations can be expressed
2635 by for example transforming ~[3, 5] + [1, 2] to range-only
2636 operations and a union primitive:
2637 [-INF, 2] + [1, 2] U [5, +INF] + [1, 2]
2638 [-INF+1, 4] U [6, +INF(OVF)]
2639 though usually the union is not exactly representable with
2640 a single range or anti-range as the above is
2641 [-INF+1, +INF(OVF)] intersected with ~[5, 5]
2642 but one could use a scheme similar to equivalences for this. */
2643 set_value_range_to_varying (vr
);
2647 else if (code
== MIN_EXPR
2648 || code
== MAX_EXPR
)
2650 if (vr0
.type
== VR_RANGE
2651 && !symbolic_range_p (&vr0
))
2654 if (vr1
.type
== VR_RANGE
2655 && !symbolic_range_p (&vr1
))
2657 /* For operations that make the resulting range directly
2658 proportional to the original ranges, apply the operation to
2659 the same end of each range. */
2660 min
= vrp_int_const_binop (code
, vr0
.min
, vr1
.min
);
2661 max
= vrp_int_const_binop (code
, vr0
.max
, vr1
.max
);
2663 else if (code
== MIN_EXPR
)
2665 min
= vrp_val_min (expr_type
);
2668 else if (code
== MAX_EXPR
)
2671 max
= vrp_val_max (expr_type
);
2674 else if (vr1
.type
== VR_RANGE
2675 && !symbolic_range_p (&vr1
))
2678 if (code
== MIN_EXPR
)
2680 min
= vrp_val_min (expr_type
);
2683 else if (code
== MAX_EXPR
)
2686 max
= vrp_val_max (expr_type
);
2691 set_value_range_to_varying (vr
);
2695 else if (code
== MULT_EXPR
)
2697 /* Fancy code so that with unsigned, [-3,-1]*[-3,-1] does not
2698 drop to varying. This test requires 2*prec bits if both
2699 operands are signed and 2*prec + 2 bits if either is not. */
2701 signop sign
= TYPE_SIGN (expr_type
);
2702 unsigned int prec
= TYPE_PRECISION (expr_type
);
2704 if (range_int_cst_p (&vr0
)
2705 && range_int_cst_p (&vr1
)
2706 && TYPE_OVERFLOW_WRAPS (expr_type
))
2708 typedef FIXED_WIDE_INT (WIDE_INT_MAX_PRECISION
* 2) vrp_int
;
2709 typedef generic_wide_int
2710 <wi::extended_tree
<WIDE_INT_MAX_PRECISION
* 2> > vrp_int_cst
;
2711 vrp_int sizem1
= wi::mask
<vrp_int
> (prec
, false);
2712 vrp_int size
= sizem1
+ 1;
2714 /* Extend the values using the sign of the result to PREC2.
2715 From here on out, everthing is just signed math no matter
2716 what the input types were. */
2717 vrp_int min0
= vrp_int_cst (vr0
.min
);
2718 vrp_int max0
= vrp_int_cst (vr0
.max
);
2719 vrp_int min1
= vrp_int_cst (vr1
.min
);
2720 vrp_int max1
= vrp_int_cst (vr1
.max
);
2721 /* Canonicalize the intervals. */
2722 if (sign
== UNSIGNED
)
2724 if (wi::ltu_p (size
, min0
+ max0
))
2730 if (wi::ltu_p (size
, min1
+ max1
))
2737 vrp_int prod0
= min0
* min1
;
2738 vrp_int prod1
= min0
* max1
;
2739 vrp_int prod2
= max0
* min1
;
2740 vrp_int prod3
= max0
* max1
;
2742 /* Sort the 4 products so that min is in prod0 and max is in
2744 /* min0min1 > max0max1 */
2746 std::swap (prod0
, prod3
);
2748 /* min0max1 > max0min1 */
2750 std::swap (prod1
, prod2
);
2753 std::swap (prod0
, prod1
);
2756 std::swap (prod2
, prod3
);
2758 /* diff = max - min. */
2759 prod2
= prod3
- prod0
;
2760 if (wi::geu_p (prod2
, sizem1
))
2762 /* the range covers all values. */
2763 set_value_range_to_varying (vr
);
2767 /* The following should handle the wrapping and selecting
2768 VR_ANTI_RANGE for us. */
2769 min
= wide_int_to_tree (expr_type
, prod0
);
2770 max
= wide_int_to_tree (expr_type
, prod3
);
2771 set_and_canonicalize_value_range (vr
, VR_RANGE
, min
, max
, NULL
);
2775 /* If we have an unsigned MULT_EXPR with two VR_ANTI_RANGEs,
2776 drop to VR_VARYING. It would take more effort to compute a
2777 precise range for such a case. For example, if we have
2778 op0 == 65536 and op1 == 65536 with their ranges both being
2779 ~[0,0] on a 32-bit machine, we would have op0 * op1 == 0, so
2780 we cannot claim that the product is in ~[0,0]. Note that we
2781 are guaranteed to have vr0.type == vr1.type at this
2783 if (vr0
.type
== VR_ANTI_RANGE
2784 && !TYPE_OVERFLOW_UNDEFINED (expr_type
))
2786 set_value_range_to_varying (vr
);
2790 extract_range_from_multiplicative_op_1 (vr
, code
, &vr0
, &vr1
);
2793 else if (code
== RSHIFT_EXPR
2794 || code
== LSHIFT_EXPR
)
2796 /* If we have a RSHIFT_EXPR with any shift values outside [0..prec-1],
2797 then drop to VR_VARYING. Outside of this range we get undefined
2798 behavior from the shift operation. We cannot even trust
2799 SHIFT_COUNT_TRUNCATED at this stage, because that applies to rtl
2800 shifts, and the operation at the tree level may be widened. */
2801 if (range_int_cst_p (&vr1
)
2802 && compare_tree_int (vr1
.min
, 0) >= 0
2803 && compare_tree_int (vr1
.max
, TYPE_PRECISION (expr_type
)) == -1)
2805 if (code
== RSHIFT_EXPR
)
2807 /* Even if vr0 is VARYING or otherwise not usable, we can derive
2808 useful ranges just from the shift count. E.g.
2809 x >> 63 for signed 64-bit x is always [-1, 0]. */
2810 if (vr0
.type
!= VR_RANGE
|| symbolic_range_p (&vr0
))
2812 vr0
.type
= type
= VR_RANGE
;
2813 vr0
.min
= vrp_val_min (expr_type
);
2814 vr0
.max
= vrp_val_max (expr_type
);
2816 extract_range_from_multiplicative_op_1 (vr
, code
, &vr0
, &vr1
);
2819 /* We can map lshifts by constants to MULT_EXPR handling. */
2820 else if (code
== LSHIFT_EXPR
2821 && range_int_cst_singleton_p (&vr1
))
2823 bool saved_flag_wrapv
;
2824 value_range vr1p
= VR_INITIALIZER
;
2825 vr1p
.type
= VR_RANGE
;
2826 vr1p
.min
= (wide_int_to_tree
2828 wi::set_bit_in_zero (tree_to_shwi (vr1
.min
),
2829 TYPE_PRECISION (expr_type
))));
2830 vr1p
.max
= vr1p
.min
;
2831 /* We have to use a wrapping multiply though as signed overflow
2832 on lshifts is implementation defined in C89. */
2833 saved_flag_wrapv
= flag_wrapv
;
2835 extract_range_from_binary_expr_1 (vr
, MULT_EXPR
, expr_type
,
2837 flag_wrapv
= saved_flag_wrapv
;
2840 else if (code
== LSHIFT_EXPR
2841 && range_int_cst_p (&vr0
))
2843 int prec
= TYPE_PRECISION (expr_type
);
2844 int overflow_pos
= prec
;
2846 wide_int low_bound
, high_bound
;
2847 bool uns
= TYPE_UNSIGNED (expr_type
);
2848 bool in_bounds
= false;
2853 bound_shift
= overflow_pos
- tree_to_shwi (vr1
.max
);
2854 /* If bound_shift == HOST_BITS_PER_WIDE_INT, the llshift can
2855 overflow. However, for that to happen, vr1.max needs to be
2856 zero, which means vr1 is a singleton range of zero, which
2857 means it should be handled by the previous LSHIFT_EXPR
2859 wide_int bound
= wi::set_bit_in_zero (bound_shift
, prec
);
2860 wide_int complement
= ~(bound
- 1);
2865 high_bound
= complement
;
2866 if (wi::ltu_p (vr0
.max
, low_bound
))
2868 /* [5, 6] << [1, 2] == [10, 24]. */
2869 /* We're shifting out only zeroes, the value increases
2873 else if (wi::ltu_p (high_bound
, vr0
.min
))
2875 /* [0xffffff00, 0xffffffff] << [1, 2]
2876 == [0xfffffc00, 0xfffffffe]. */
2877 /* We're shifting out only ones, the value decreases
2884 /* [-1, 1] << [1, 2] == [-4, 4]. */
2885 low_bound
= complement
;
2887 if (wi::lts_p (vr0
.max
, high_bound
)
2888 && wi::lts_p (low_bound
, vr0
.min
))
2890 /* For non-negative numbers, we're shifting out only
2891 zeroes, the value increases monotonically.
2892 For negative numbers, we're shifting out only ones, the
2893 value decreases monotomically. */
2900 extract_range_from_multiplicative_op_1 (vr
, code
, &vr0
, &vr1
);
2905 set_value_range_to_varying (vr
);
2908 else if (code
== TRUNC_DIV_EXPR
2909 || code
== FLOOR_DIV_EXPR
2910 || code
== CEIL_DIV_EXPR
2911 || code
== EXACT_DIV_EXPR
2912 || code
== ROUND_DIV_EXPR
)
2914 if (vr0
.type
!= VR_RANGE
|| symbolic_range_p (&vr0
))
2916 /* For division, if op1 has VR_RANGE but op0 does not, something
2917 can be deduced just from that range. Say [min, max] / [4, max]
2918 gives [min / 4, max / 4] range. */
2919 if (vr1
.type
== VR_RANGE
2920 && !symbolic_range_p (&vr1
)
2921 && range_includes_zero_p (vr1
.min
, vr1
.max
) == 0)
2923 vr0
.type
= type
= VR_RANGE
;
2924 vr0
.min
= vrp_val_min (expr_type
);
2925 vr0
.max
= vrp_val_max (expr_type
);
2929 set_value_range_to_varying (vr
);
2934 /* For divisions, if flag_non_call_exceptions is true, we must
2935 not eliminate a division by zero. */
2936 if (cfun
->can_throw_non_call_exceptions
2937 && (vr1
.type
!= VR_RANGE
2938 || range_includes_zero_p (vr1
.min
, vr1
.max
) != 0))
2940 set_value_range_to_varying (vr
);
2944 /* For divisions, if op0 is VR_RANGE, we can deduce a range
2945 even if op1 is VR_VARYING, VR_ANTI_RANGE, symbolic or can
2947 if (vr0
.type
== VR_RANGE
2948 && (vr1
.type
!= VR_RANGE
2949 || range_includes_zero_p (vr1
.min
, vr1
.max
) != 0))
2951 tree zero
= build_int_cst (TREE_TYPE (vr0
.min
), 0);
2956 if (TYPE_UNSIGNED (expr_type
)
2957 || value_range_nonnegative_p (&vr1
))
2959 /* For unsigned division or when divisor is known
2960 to be non-negative, the range has to cover
2961 all numbers from 0 to max for positive max
2962 and all numbers from min to 0 for negative min. */
2963 cmp
= compare_values (vr0
.max
, zero
);
2966 /* When vr0.max < 0, vr1.min != 0 and value
2967 ranges for dividend and divisor are available. */
2968 if (vr1
.type
== VR_RANGE
2969 && !symbolic_range_p (&vr0
)
2970 && !symbolic_range_p (&vr1
)
2971 && compare_values (vr1
.min
, zero
) != 0)
2972 max
= int_const_binop (code
, vr0
.max
, vr1
.min
);
2976 else if (cmp
== 0 || cmp
== 1)
2980 cmp
= compare_values (vr0
.min
, zero
);
2983 /* For unsigned division when value ranges for dividend
2984 and divisor are available. */
2985 if (vr1
.type
== VR_RANGE
2986 && !symbolic_range_p (&vr0
)
2987 && !symbolic_range_p (&vr1
)
2988 && compare_values (vr1
.max
, zero
) != 0)
2989 min
= int_const_binop (code
, vr0
.min
, vr1
.max
);
2993 else if (cmp
== 0 || cmp
== -1)
3000 /* Otherwise the range is -max .. max or min .. -min
3001 depending on which bound is bigger in absolute value,
3002 as the division can change the sign. */
3003 abs_extent_range (vr
, vr0
.min
, vr0
.max
);
3006 if (type
== VR_VARYING
)
3008 set_value_range_to_varying (vr
);
3012 else if (!symbolic_range_p (&vr0
) && !symbolic_range_p (&vr1
))
3014 extract_range_from_multiplicative_op_1 (vr
, code
, &vr0
, &vr1
);
3018 else if (code
== TRUNC_MOD_EXPR
)
3020 if (range_is_null (&vr1
))
3022 set_value_range_to_undefined (vr
);
3025 /* ABS (A % B) < ABS (B) and either
3026 0 <= A % B <= A or A <= A % B <= 0. */
3028 signop sgn
= TYPE_SIGN (expr_type
);
3029 unsigned int prec
= TYPE_PRECISION (expr_type
);
3030 wide_int wmin
, wmax
, tmp
;
3031 wide_int zero
= wi::zero (prec
);
3032 wide_int one
= wi::one (prec
);
3033 if (vr1
.type
== VR_RANGE
&& !symbolic_range_p (&vr1
))
3035 wmax
= wi::sub (vr1
.max
, one
);
3038 tmp
= wi::sub (wi::minus_one (prec
), vr1
.min
);
3039 wmax
= wi::smax (wmax
, tmp
);
3044 wmax
= wi::max_value (prec
, sgn
);
3045 /* X % INT_MIN may be INT_MAX. */
3046 if (sgn
== UNSIGNED
)
3050 if (sgn
== UNSIGNED
)
3055 if (vr0
.type
== VR_RANGE
&& TREE_CODE (vr0
.min
) == INTEGER_CST
)
3058 if (wi::gts_p (tmp
, zero
))
3060 wmin
= wi::smax (wmin
, tmp
);
3064 if (vr0
.type
== VR_RANGE
&& TREE_CODE (vr0
.max
) == INTEGER_CST
)
3067 if (sgn
== SIGNED
&& wi::neg_p (tmp
))
3069 wmax
= wi::min (wmax
, tmp
, sgn
);
3072 min
= wide_int_to_tree (expr_type
, wmin
);
3073 max
= wide_int_to_tree (expr_type
, wmax
);
3075 else if (code
== BIT_AND_EXPR
|| code
== BIT_IOR_EXPR
|| code
== BIT_XOR_EXPR
)
3077 bool int_cst_range0
, int_cst_range1
;
3078 wide_int may_be_nonzero0
, may_be_nonzero1
;
3079 wide_int must_be_nonzero0
, must_be_nonzero1
;
3081 int_cst_range0
= zero_nonzero_bits_from_vr (expr_type
, &vr0
,
3084 int_cst_range1
= zero_nonzero_bits_from_vr (expr_type
, &vr1
,
3089 if (code
== BIT_AND_EXPR
)
3091 min
= wide_int_to_tree (expr_type
,
3092 must_be_nonzero0
& must_be_nonzero1
);
3093 wide_int wmax
= may_be_nonzero0
& may_be_nonzero1
;
3094 /* If both input ranges contain only negative values we can
3095 truncate the result range maximum to the minimum of the
3096 input range maxima. */
3097 if (int_cst_range0
&& int_cst_range1
3098 && tree_int_cst_sgn (vr0
.max
) < 0
3099 && tree_int_cst_sgn (vr1
.max
) < 0)
3101 wmax
= wi::min (wmax
, vr0
.max
, TYPE_SIGN (expr_type
));
3102 wmax
= wi::min (wmax
, vr1
.max
, TYPE_SIGN (expr_type
));
3104 /* If either input range contains only non-negative values
3105 we can truncate the result range maximum to the respective
3106 maximum of the input range. */
3107 if (int_cst_range0
&& tree_int_cst_sgn (vr0
.min
) >= 0)
3108 wmax
= wi::min (wmax
, vr0
.max
, TYPE_SIGN (expr_type
));
3109 if (int_cst_range1
&& tree_int_cst_sgn (vr1
.min
) >= 0)
3110 wmax
= wi::min (wmax
, vr1
.max
, TYPE_SIGN (expr_type
));
3111 max
= wide_int_to_tree (expr_type
, wmax
);
3112 cmp
= compare_values (min
, max
);
3113 /* PR68217: In case of signed & sign-bit-CST should
3114 result in [-INF, 0] instead of [-INF, INF]. */
3115 if (cmp
== -2 || cmp
== 1)
3118 = wi::set_bit_in_zero (TYPE_PRECISION (expr_type
) - 1,
3119 TYPE_PRECISION (expr_type
));
3120 if (!TYPE_UNSIGNED (expr_type
)
3121 && ((value_range_constant_singleton (&vr0
)
3122 && !wi::cmps (vr0
.min
, sign_bit
))
3123 || (value_range_constant_singleton (&vr1
)
3124 && !wi::cmps (vr1
.min
, sign_bit
))))
3126 min
= TYPE_MIN_VALUE (expr_type
);
3127 max
= build_int_cst (expr_type
, 0);
3131 else if (code
== BIT_IOR_EXPR
)
3133 max
= wide_int_to_tree (expr_type
,
3134 may_be_nonzero0
| may_be_nonzero1
);
3135 wide_int wmin
= must_be_nonzero0
| must_be_nonzero1
;
3136 /* If the input ranges contain only positive values we can
3137 truncate the minimum of the result range to the maximum
3138 of the input range minima. */
3139 if (int_cst_range0
&& int_cst_range1
3140 && tree_int_cst_sgn (vr0
.min
) >= 0
3141 && tree_int_cst_sgn (vr1
.min
) >= 0)
3143 wmin
= wi::max (wmin
, vr0
.min
, TYPE_SIGN (expr_type
));
3144 wmin
= wi::max (wmin
, vr1
.min
, TYPE_SIGN (expr_type
));
3146 /* If either input range contains only negative values
3147 we can truncate the minimum of the result range to the
3148 respective minimum range. */
3149 if (int_cst_range0
&& tree_int_cst_sgn (vr0
.max
) < 0)
3150 wmin
= wi::max (wmin
, vr0
.min
, TYPE_SIGN (expr_type
));
3151 if (int_cst_range1
&& tree_int_cst_sgn (vr1
.max
) < 0)
3152 wmin
= wi::max (wmin
, vr1
.min
, TYPE_SIGN (expr_type
));
3153 min
= wide_int_to_tree (expr_type
, wmin
);
3155 else if (code
== BIT_XOR_EXPR
)
3157 wide_int result_zero_bits
= ((must_be_nonzero0
& must_be_nonzero1
)
3158 | ~(may_be_nonzero0
| may_be_nonzero1
));
3159 wide_int result_one_bits
3160 = (must_be_nonzero0
.and_not (may_be_nonzero1
)
3161 | must_be_nonzero1
.and_not (may_be_nonzero0
));
3162 max
= wide_int_to_tree (expr_type
, ~result_zero_bits
);
3163 min
= wide_int_to_tree (expr_type
, result_one_bits
);
3164 /* If the range has all positive or all negative values the
3165 result is better than VARYING. */
3166 if (tree_int_cst_sgn (min
) < 0
3167 || tree_int_cst_sgn (max
) >= 0)
3170 max
= min
= NULL_TREE
;
3176 /* If either MIN or MAX overflowed, then set the resulting range to
3177 VARYING. But we do accept an overflow infinity representation. */
3178 if (min
== NULL_TREE
3179 || (TREE_OVERFLOW_P (min
) && !is_overflow_infinity (min
))
3181 || (TREE_OVERFLOW_P (max
) && !is_overflow_infinity (max
)))
3183 set_value_range_to_varying (vr
);
3189 2) [-INF, +-INF(OVF)]
3190 3) [+-INF(OVF), +INF]
3191 4) [+-INF(OVF), +-INF(OVF)]
3192 We learn nothing when we have INF and INF(OVF) on both sides.
3193 Note that we do accept [-INF, -INF] and [+INF, +INF] without
3195 if ((vrp_val_is_min (min
) || is_overflow_infinity (min
))
3196 && (vrp_val_is_max (max
) || is_overflow_infinity (max
)))
3198 set_value_range_to_varying (vr
);
3202 cmp
= compare_values (min
, max
);
3203 if (cmp
== -2 || cmp
== 1)
3205 /* If the new range has its limits swapped around (MIN > MAX),
3206 then the operation caused one of them to wrap around, mark
3207 the new range VARYING. */
3208 set_value_range_to_varying (vr
);
3211 set_value_range (vr
, type
, min
, max
, NULL
);
3214 /* Extract range information from a binary expression OP0 CODE OP1 based on
3215 the ranges of each of its operands with resulting type EXPR_TYPE.
3216 The resulting range is stored in *VR. */
3219 extract_range_from_binary_expr (value_range
*vr
,
3220 enum tree_code code
,
3221 tree expr_type
, tree op0
, tree op1
)
3223 value_range vr0
= VR_INITIALIZER
;
3224 value_range vr1
= VR_INITIALIZER
;
3226 /* Get value ranges for each operand. For constant operands, create
3227 a new value range with the operand to simplify processing. */
3228 if (TREE_CODE (op0
) == SSA_NAME
)
3229 vr0
= *(get_value_range (op0
));
3230 else if (is_gimple_min_invariant (op0
))
3231 set_value_range_to_value (&vr0
, op0
, NULL
);
3233 set_value_range_to_varying (&vr0
);
3235 if (TREE_CODE (op1
) == SSA_NAME
)
3236 vr1
= *(get_value_range (op1
));
3237 else if (is_gimple_min_invariant (op1
))
3238 set_value_range_to_value (&vr1
, op1
, NULL
);
3240 set_value_range_to_varying (&vr1
);
3242 extract_range_from_binary_expr_1 (vr
, code
, expr_type
, &vr0
, &vr1
);
3244 /* Try harder for PLUS and MINUS if the range of one operand is symbolic
3245 and based on the other operand, for example if it was deduced from a
3246 symbolic comparison. When a bound of the range of the first operand
3247 is invariant, we set the corresponding bound of the new range to INF
3248 in order to avoid recursing on the range of the second operand. */
3249 if (vr
->type
== VR_VARYING
3250 && (code
== PLUS_EXPR
|| code
== MINUS_EXPR
)
3251 && TREE_CODE (op1
) == SSA_NAME
3252 && vr0
.type
== VR_RANGE
3253 && symbolic_range_based_on_p (&vr0
, op1
))
3255 const bool minus_p
= (code
== MINUS_EXPR
);
3256 value_range n_vr1
= VR_INITIALIZER
;
3258 /* Try with VR0 and [-INF, OP1]. */
3259 if (is_gimple_min_invariant (minus_p
? vr0
.max
: vr0
.min
))
3260 set_value_range (&n_vr1
, VR_RANGE
, vrp_val_min (expr_type
), op1
, NULL
);
3262 /* Try with VR0 and [OP1, +INF]. */
3263 else if (is_gimple_min_invariant (minus_p
? vr0
.min
: vr0
.max
))
3264 set_value_range (&n_vr1
, VR_RANGE
, op1
, vrp_val_max (expr_type
), NULL
);
3266 /* Try with VR0 and [OP1, OP1]. */
3268 set_value_range (&n_vr1
, VR_RANGE
, op1
, op1
, NULL
);
3270 extract_range_from_binary_expr_1 (vr
, code
, expr_type
, &vr0
, &n_vr1
);
3273 if (vr
->type
== VR_VARYING
3274 && (code
== PLUS_EXPR
|| code
== MINUS_EXPR
)
3275 && TREE_CODE (op0
) == SSA_NAME
3276 && vr1
.type
== VR_RANGE
3277 && symbolic_range_based_on_p (&vr1
, op0
))
3279 const bool minus_p
= (code
== MINUS_EXPR
);
3280 value_range n_vr0
= VR_INITIALIZER
;
3282 /* Try with [-INF, OP0] and VR1. */
3283 if (is_gimple_min_invariant (minus_p
? vr1
.max
: vr1
.min
))
3284 set_value_range (&n_vr0
, VR_RANGE
, vrp_val_min (expr_type
), op0
, NULL
);
3286 /* Try with [OP0, +INF] and VR1. */
3287 else if (is_gimple_min_invariant (minus_p
? vr1
.min
: vr1
.max
))
3288 set_value_range (&n_vr0
, VR_RANGE
, op0
, vrp_val_max (expr_type
), NULL
);
3290 /* Try with [OP0, OP0] and VR1. */
3292 set_value_range (&n_vr0
, VR_RANGE
, op0
, op0
, NULL
);
3294 extract_range_from_binary_expr_1 (vr
, code
, expr_type
, &n_vr0
, &vr1
);
3298 /* Extract range information from a unary operation CODE based on
3299 the range of its operand *VR0 with type OP0_TYPE with resulting type TYPE.
3300 The resulting range is stored in *VR. */
3303 extract_range_from_unary_expr (value_range
*vr
,
3304 enum tree_code code
, tree type
,
3305 value_range
*vr0_
, tree op0_type
)
3307 value_range vr0
= *vr0_
, vrtem0
= VR_INITIALIZER
, vrtem1
= VR_INITIALIZER
;
3309 /* VRP only operates on integral and pointer types. */
3310 if (!(INTEGRAL_TYPE_P (op0_type
)
3311 || POINTER_TYPE_P (op0_type
))
3312 || !(INTEGRAL_TYPE_P (type
)
3313 || POINTER_TYPE_P (type
)))
3315 set_value_range_to_varying (vr
);
3319 /* If VR0 is UNDEFINED, so is the result. */
3320 if (vr0
.type
== VR_UNDEFINED
)
3322 set_value_range_to_undefined (vr
);
3326 /* Handle operations that we express in terms of others. */
3327 if (code
== PAREN_EXPR
|| code
== OBJ_TYPE_REF
)
3329 /* PAREN_EXPR and OBJ_TYPE_REF are simple copies. */
3330 copy_value_range (vr
, &vr0
);
3333 else if (code
== NEGATE_EXPR
)
3335 /* -X is simply 0 - X, so re-use existing code that also handles
3336 anti-ranges fine. */
3337 value_range zero
= VR_INITIALIZER
;
3338 set_value_range_to_value (&zero
, build_int_cst (type
, 0), NULL
);
3339 extract_range_from_binary_expr_1 (vr
, MINUS_EXPR
, type
, &zero
, &vr0
);
3342 else if (code
== BIT_NOT_EXPR
)
3344 /* ~X is simply -1 - X, so re-use existing code that also handles
3345 anti-ranges fine. */
3346 value_range minusone
= VR_INITIALIZER
;
3347 set_value_range_to_value (&minusone
, build_int_cst (type
, -1), NULL
);
3348 extract_range_from_binary_expr_1 (vr
, MINUS_EXPR
,
3349 type
, &minusone
, &vr0
);
3353 /* Now canonicalize anti-ranges to ranges when they are not symbolic
3354 and express op ~[] as (op []') U (op []''). */
3355 if (vr0
.type
== VR_ANTI_RANGE
3356 && ranges_from_anti_range (&vr0
, &vrtem0
, &vrtem1
))
3358 extract_range_from_unary_expr (vr
, code
, type
, &vrtem0
, op0_type
);
3359 if (vrtem1
.type
!= VR_UNDEFINED
)
3361 value_range vrres
= VR_INITIALIZER
;
3362 extract_range_from_unary_expr (&vrres
, code
, type
,
3364 vrp_meet (vr
, &vrres
);
3369 if (CONVERT_EXPR_CODE_P (code
))
3371 tree inner_type
= op0_type
;
3372 tree outer_type
= type
;
3374 /* If the expression evaluates to a pointer, we are only interested in
3375 determining if it evaluates to NULL [0, 0] or non-NULL (~[0, 0]). */
3376 if (POINTER_TYPE_P (type
))
3378 if (range_is_nonnull (&vr0
))
3379 set_value_range_to_nonnull (vr
, type
);
3380 else if (range_is_null (&vr0
))
3381 set_value_range_to_null (vr
, type
);
3383 set_value_range_to_varying (vr
);
3387 /* If VR0 is varying and we increase the type precision, assume
3388 a full range for the following transformation. */
3389 if (vr0
.type
== VR_VARYING
3390 && INTEGRAL_TYPE_P (inner_type
)
3391 && TYPE_PRECISION (inner_type
) < TYPE_PRECISION (outer_type
))
3393 vr0
.type
= VR_RANGE
;
3394 vr0
.min
= TYPE_MIN_VALUE (inner_type
);
3395 vr0
.max
= TYPE_MAX_VALUE (inner_type
);
3398 /* If VR0 is a constant range or anti-range and the conversion is
3399 not truncating we can convert the min and max values and
3400 canonicalize the resulting range. Otherwise we can do the
3401 conversion if the size of the range is less than what the
3402 precision of the target type can represent and the range is
3403 not an anti-range. */
3404 if ((vr0
.type
== VR_RANGE
3405 || vr0
.type
== VR_ANTI_RANGE
)
3406 && TREE_CODE (vr0
.min
) == INTEGER_CST
3407 && TREE_CODE (vr0
.max
) == INTEGER_CST
3408 && (!is_overflow_infinity (vr0
.min
)
3409 || (vr0
.type
== VR_RANGE
3410 && TYPE_PRECISION (outer_type
) > TYPE_PRECISION (inner_type
)
3411 && needs_overflow_infinity (outer_type
)
3412 && supports_overflow_infinity (outer_type
)))
3413 && (!is_overflow_infinity (vr0
.max
)
3414 || (vr0
.type
== VR_RANGE
3415 && TYPE_PRECISION (outer_type
) > TYPE_PRECISION (inner_type
)
3416 && needs_overflow_infinity (outer_type
)
3417 && supports_overflow_infinity (outer_type
)))
3418 && (TYPE_PRECISION (outer_type
) >= TYPE_PRECISION (inner_type
)
3419 || (vr0
.type
== VR_RANGE
3420 && integer_zerop (int_const_binop (RSHIFT_EXPR
,
3421 int_const_binop (MINUS_EXPR
, vr0
.max
, vr0
.min
),
3422 size_int (TYPE_PRECISION (outer_type
)))))))
3424 tree new_min
, new_max
;
3425 if (is_overflow_infinity (vr0
.min
))
3426 new_min
= negative_overflow_infinity (outer_type
);
3428 new_min
= force_fit_type (outer_type
, wi::to_widest (vr0
.min
),
3430 if (is_overflow_infinity (vr0
.max
))
3431 new_max
= positive_overflow_infinity (outer_type
);
3433 new_max
= force_fit_type (outer_type
, wi::to_widest (vr0
.max
),
3435 set_and_canonicalize_value_range (vr
, vr0
.type
,
3436 new_min
, new_max
, NULL
);
3440 set_value_range_to_varying (vr
);
3443 else if (code
== ABS_EXPR
)
3448 /* Pass through vr0 in the easy cases. */
3449 if (TYPE_UNSIGNED (type
)
3450 || value_range_nonnegative_p (&vr0
))
3452 copy_value_range (vr
, &vr0
);
3456 /* For the remaining varying or symbolic ranges we can't do anything
3458 if (vr0
.type
== VR_VARYING
3459 || symbolic_range_p (&vr0
))
3461 set_value_range_to_varying (vr
);
3465 /* -TYPE_MIN_VALUE = TYPE_MIN_VALUE with flag_wrapv so we can't get a
3467 if (!TYPE_OVERFLOW_UNDEFINED (type
)
3468 && ((vr0
.type
== VR_RANGE
3469 && vrp_val_is_min (vr0
.min
))
3470 || (vr0
.type
== VR_ANTI_RANGE
3471 && !vrp_val_is_min (vr0
.min
))))
3473 set_value_range_to_varying (vr
);
3477 /* ABS_EXPR may flip the range around, if the original range
3478 included negative values. */
3479 if (is_overflow_infinity (vr0
.min
))
3480 min
= positive_overflow_infinity (type
);
3481 else if (!vrp_val_is_min (vr0
.min
))
3482 min
= fold_unary_to_constant (code
, type
, vr0
.min
);
3483 else if (!needs_overflow_infinity (type
))
3484 min
= TYPE_MAX_VALUE (type
);
3485 else if (supports_overflow_infinity (type
))
3486 min
= positive_overflow_infinity (type
);
3489 set_value_range_to_varying (vr
);
3493 if (is_overflow_infinity (vr0
.max
))
3494 max
= positive_overflow_infinity (type
);
3495 else if (!vrp_val_is_min (vr0
.max
))
3496 max
= fold_unary_to_constant (code
, type
, vr0
.max
);
3497 else if (!needs_overflow_infinity (type
))
3498 max
= TYPE_MAX_VALUE (type
);
3499 else if (supports_overflow_infinity (type
)
3500 /* We shouldn't generate [+INF, +INF] as set_value_range
3501 doesn't like this and ICEs. */
3502 && !is_positive_overflow_infinity (min
))
3503 max
= positive_overflow_infinity (type
);
3506 set_value_range_to_varying (vr
);
3510 cmp
= compare_values (min
, max
);
3512 /* If a VR_ANTI_RANGEs contains zero, then we have
3513 ~[-INF, min(MIN, MAX)]. */
3514 if (vr0
.type
== VR_ANTI_RANGE
)
3516 if (range_includes_zero_p (vr0
.min
, vr0
.max
) == 1)
3518 /* Take the lower of the two values. */
3522 /* Create ~[-INF, min (abs(MIN), abs(MAX))]
3523 or ~[-INF + 1, min (abs(MIN), abs(MAX))] when
3524 flag_wrapv is set and the original anti-range doesn't include
3525 TYPE_MIN_VALUE, remember -TYPE_MIN_VALUE = TYPE_MIN_VALUE. */
3526 if (TYPE_OVERFLOW_WRAPS (type
))
3528 tree type_min_value
= TYPE_MIN_VALUE (type
);
3530 min
= (vr0
.min
!= type_min_value
3531 ? int_const_binop (PLUS_EXPR
, type_min_value
,
3532 build_int_cst (TREE_TYPE (type_min_value
), 1))
3537 if (overflow_infinity_range_p (&vr0
))
3538 min
= negative_overflow_infinity (type
);
3540 min
= TYPE_MIN_VALUE (type
);
3545 /* All else has failed, so create the range [0, INF], even for
3546 flag_wrapv since TYPE_MIN_VALUE is in the original
3548 vr0
.type
= VR_RANGE
;
3549 min
= build_int_cst (type
, 0);
3550 if (needs_overflow_infinity (type
))
3552 if (supports_overflow_infinity (type
))
3553 max
= positive_overflow_infinity (type
);
3556 set_value_range_to_varying (vr
);
3561 max
= TYPE_MAX_VALUE (type
);
3565 /* If the range contains zero then we know that the minimum value in the
3566 range will be zero. */
3567 else if (range_includes_zero_p (vr0
.min
, vr0
.max
) == 1)
3571 min
= build_int_cst (type
, 0);
3575 /* If the range was reversed, swap MIN and MAX. */
3577 std::swap (min
, max
);
3580 cmp
= compare_values (min
, max
);
3581 if (cmp
== -2 || cmp
== 1)
3583 /* If the new range has its limits swapped around (MIN > MAX),
3584 then the operation caused one of them to wrap around, mark
3585 the new range VARYING. */
3586 set_value_range_to_varying (vr
);
3589 set_value_range (vr
, vr0
.type
, min
, max
, NULL
);
3593 /* For unhandled operations fall back to varying. */
3594 set_value_range_to_varying (vr
);
3599 /* Extract range information from a unary expression CODE OP0 based on
3600 the range of its operand with resulting type TYPE.
3601 The resulting range is stored in *VR. */
3604 extract_range_from_unary_expr (value_range
*vr
, enum tree_code code
,
3605 tree type
, tree op0
)
3607 value_range vr0
= VR_INITIALIZER
;
3609 /* Get value ranges for the operand. For constant operands, create
3610 a new value range with the operand to simplify processing. */
3611 if (TREE_CODE (op0
) == SSA_NAME
)
3612 vr0
= *(get_value_range (op0
));
3613 else if (is_gimple_min_invariant (op0
))
3614 set_value_range_to_value (&vr0
, op0
, NULL
);
3616 set_value_range_to_varying (&vr0
);
3618 extract_range_from_unary_expr (vr
, code
, type
, &vr0
, TREE_TYPE (op0
));
3622 /* Extract range information from a conditional expression STMT based on
3623 the ranges of each of its operands and the expression code. */
3626 extract_range_from_cond_expr (value_range
*vr
, gassign
*stmt
)
3629 value_range vr0
= VR_INITIALIZER
;
3630 value_range vr1
= VR_INITIALIZER
;
3632 /* Get value ranges for each operand. For constant operands, create
3633 a new value range with the operand to simplify processing. */
3634 op0
= gimple_assign_rhs2 (stmt
);
3635 if (TREE_CODE (op0
) == SSA_NAME
)
3636 vr0
= *(get_value_range (op0
));
3637 else if (is_gimple_min_invariant (op0
))
3638 set_value_range_to_value (&vr0
, op0
, NULL
);
3640 set_value_range_to_varying (&vr0
);
3642 op1
= gimple_assign_rhs3 (stmt
);
3643 if (TREE_CODE (op1
) == SSA_NAME
)
3644 vr1
= *(get_value_range (op1
));
3645 else if (is_gimple_min_invariant (op1
))
3646 set_value_range_to_value (&vr1
, op1
, NULL
);
3648 set_value_range_to_varying (&vr1
);
3650 /* The resulting value range is the union of the operand ranges */
3651 copy_value_range (vr
, &vr0
);
3652 vrp_meet (vr
, &vr1
);
3656 /* Extract range information from a comparison expression EXPR based
3657 on the range of its operand and the expression code. */
3660 extract_range_from_comparison (value_range
*vr
, enum tree_code code
,
3661 tree type
, tree op0
, tree op1
)
3666 val
= vrp_evaluate_conditional_warnv_with_ops (code
, op0
, op1
, false, &sop
,
3669 /* A disadvantage of using a special infinity as an overflow
3670 representation is that we lose the ability to record overflow
3671 when we don't have an infinity. So we have to ignore a result
3672 which relies on overflow. */
3674 if (val
&& !is_overflow_infinity (val
) && !sop
)
3676 /* Since this expression was found on the RHS of an assignment,
3677 its type may be different from _Bool. Convert VAL to EXPR's
3679 val
= fold_convert (type
, val
);
3680 if (is_gimple_min_invariant (val
))
3681 set_value_range_to_value (vr
, val
, vr
->equiv
);
3683 set_value_range (vr
, VR_RANGE
, val
, val
, vr
->equiv
);
3686 /* The result of a comparison is always true or false. */
3687 set_value_range_to_truthvalue (vr
, type
);
3690 /* Helper function for simplify_internal_call_using_ranges and
3691 extract_range_basic. Return true if OP0 SUBCODE OP1 for
3692 SUBCODE {PLUS,MINUS,MULT}_EXPR is known to never overflow or
3693 always overflow. Set *OVF to true if it is known to always
3697 check_for_binary_op_overflow (enum tree_code subcode
, tree type
,
3698 tree op0
, tree op1
, bool *ovf
)
3700 value_range vr0
= VR_INITIALIZER
;
3701 value_range vr1
= VR_INITIALIZER
;
3702 if (TREE_CODE (op0
) == SSA_NAME
)
3703 vr0
= *get_value_range (op0
);
3704 else if (TREE_CODE (op0
) == INTEGER_CST
)
3705 set_value_range_to_value (&vr0
, op0
, NULL
);
3707 set_value_range_to_varying (&vr0
);
3709 if (TREE_CODE (op1
) == SSA_NAME
)
3710 vr1
= *get_value_range (op1
);
3711 else if (TREE_CODE (op1
) == INTEGER_CST
)
3712 set_value_range_to_value (&vr1
, op1
, NULL
);
3714 set_value_range_to_varying (&vr1
);
3716 if (!range_int_cst_p (&vr0
)
3717 || TREE_OVERFLOW (vr0
.min
)
3718 || TREE_OVERFLOW (vr0
.max
))
3720 vr0
.min
= vrp_val_min (TREE_TYPE (op0
));
3721 vr0
.max
= vrp_val_max (TREE_TYPE (op0
));
3723 if (!range_int_cst_p (&vr1
)
3724 || TREE_OVERFLOW (vr1
.min
)
3725 || TREE_OVERFLOW (vr1
.max
))
3727 vr1
.min
= vrp_val_min (TREE_TYPE (op1
));
3728 vr1
.max
= vrp_val_max (TREE_TYPE (op1
));
3730 *ovf
= arith_overflowed_p (subcode
, type
, vr0
.min
,
3731 subcode
== MINUS_EXPR
? vr1
.max
: vr1
.min
);
3732 if (arith_overflowed_p (subcode
, type
, vr0
.max
,
3733 subcode
== MINUS_EXPR
? vr1
.min
: vr1
.max
) != *ovf
)
3735 if (subcode
== MULT_EXPR
)
3737 if (arith_overflowed_p (subcode
, type
, vr0
.min
, vr1
.max
) != *ovf
3738 || arith_overflowed_p (subcode
, type
, vr0
.max
, vr1
.min
) != *ovf
)
3743 /* So far we found that there is an overflow on the boundaries.
3744 That doesn't prove that there is an overflow even for all values
3745 in between the boundaries. For that compute widest_int range
3746 of the result and see if it doesn't overlap the range of
3748 widest_int wmin
, wmax
;
3751 w
[0] = wi::to_widest (vr0
.min
);
3752 w
[1] = wi::to_widest (vr0
.max
);
3753 w
[2] = wi::to_widest (vr1
.min
);
3754 w
[3] = wi::to_widest (vr1
.max
);
3755 for (i
= 0; i
< 4; i
++)
3761 wt
= wi::add (w
[i
& 1], w
[2 + (i
& 2) / 2]);
3764 wt
= wi::sub (w
[i
& 1], w
[2 + (i
& 2) / 2]);
3767 wt
= wi::mul (w
[i
& 1], w
[2 + (i
& 2) / 2]);
3779 wmin
= wi::smin (wmin
, wt
);
3780 wmax
= wi::smax (wmax
, wt
);
3783 /* The result of op0 CODE op1 is known to be in range
3785 widest_int wtmin
= wi::to_widest (vrp_val_min (type
));
3786 widest_int wtmax
= wi::to_widest (vrp_val_max (type
));
3787 /* If all values in [wmin, wmax] are smaller than
3788 [wtmin, wtmax] or all are larger than [wtmin, wtmax],
3789 the arithmetic operation will always overflow. */
3790 if (wmax
< wtmin
|| wmin
> wtmax
)
3797 /* Try to derive a nonnegative or nonzero range out of STMT relying
3798 primarily on generic routines in fold in conjunction with range data.
3799 Store the result in *VR */
3802 extract_range_basic (value_range
*vr
, gimple
*stmt
)
3805 tree type
= gimple_expr_type (stmt
);
3807 if (is_gimple_call (stmt
))
3810 int mini
, maxi
, zerov
= 0, prec
;
3811 enum tree_code subcode
= ERROR_MARK
;
3812 combined_fn cfn
= gimple_call_combined_fn (stmt
);
3816 case CFN_BUILT_IN_CONSTANT_P
:
3817 /* If the call is __builtin_constant_p and the argument is a
3818 function parameter resolve it to false. This avoids bogus
3819 array bound warnings.
3820 ??? We could do this as early as inlining is finished. */
3821 arg
= gimple_call_arg (stmt
, 0);
3822 if (TREE_CODE (arg
) == SSA_NAME
3823 && SSA_NAME_IS_DEFAULT_DEF (arg
)
3824 && TREE_CODE (SSA_NAME_VAR (arg
)) == PARM_DECL
3825 && cfun
->after_inlining
)
3827 set_value_range_to_null (vr
, type
);
3831 /* Both __builtin_ffs* and __builtin_popcount return
3835 arg
= gimple_call_arg (stmt
, 0);
3836 prec
= TYPE_PRECISION (TREE_TYPE (arg
));
3839 if (TREE_CODE (arg
) == SSA_NAME
)
3841 value_range
*vr0
= get_value_range (arg
);
3842 /* If arg is non-zero, then ffs or popcount
3844 if (((vr0
->type
== VR_RANGE
3845 && range_includes_zero_p (vr0
->min
, vr0
->max
) == 0)
3846 || (vr0
->type
== VR_ANTI_RANGE
3847 && range_includes_zero_p (vr0
->min
, vr0
->max
) == 1))
3848 && !is_overflow_infinity (vr0
->min
)
3849 && !is_overflow_infinity (vr0
->max
))
3851 /* If some high bits are known to be zero,
3852 we can decrease the maximum. */
3853 if (vr0
->type
== VR_RANGE
3854 && TREE_CODE (vr0
->max
) == INTEGER_CST
3855 && !operand_less_p (vr0
->min
,
3856 build_zero_cst (TREE_TYPE (vr0
->min
)))
3857 && !is_overflow_infinity (vr0
->max
))
3858 maxi
= tree_floor_log2 (vr0
->max
) + 1;
3861 /* __builtin_parity* returns [0, 1]. */
3866 /* __builtin_c[lt]z* return [0, prec-1], except for
3867 when the argument is 0, but that is undefined behavior.
3868 On many targets where the CLZ RTL or optab value is defined
3869 for 0 the value is prec, so include that in the range
3872 arg
= gimple_call_arg (stmt
, 0);
3873 prec
= TYPE_PRECISION (TREE_TYPE (arg
));
3876 if (optab_handler (clz_optab
, TYPE_MODE (TREE_TYPE (arg
)))
3878 && CLZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (TREE_TYPE (arg
)),
3880 /* Handle only the single common value. */
3882 /* Magic value to give up, unless vr0 proves
3885 if (TREE_CODE (arg
) == SSA_NAME
)
3887 value_range
*vr0
= get_value_range (arg
);
3888 /* From clz of VR_RANGE minimum we can compute
3890 if (vr0
->type
== VR_RANGE
3891 && TREE_CODE (vr0
->min
) == INTEGER_CST
3892 && !is_overflow_infinity (vr0
->min
))
3894 maxi
= prec
- 1 - tree_floor_log2 (vr0
->min
);
3898 else if (vr0
->type
== VR_ANTI_RANGE
3899 && integer_zerop (vr0
->min
)
3900 && !is_overflow_infinity (vr0
->min
))
3907 /* From clz of VR_RANGE maximum we can compute
3909 if (vr0
->type
== VR_RANGE
3910 && TREE_CODE (vr0
->max
) == INTEGER_CST
3911 && !is_overflow_infinity (vr0
->max
))
3913 mini
= prec
- 1 - tree_floor_log2 (vr0
->max
);
3921 /* __builtin_ctz* return [0, prec-1], except for
3922 when the argument is 0, but that is undefined behavior.
3923 If there is a ctz optab for this mode and
3924 CTZ_DEFINED_VALUE_AT_ZERO, include that in the range,
3925 otherwise just assume 0 won't be seen. */
3927 arg
= gimple_call_arg (stmt
, 0);
3928 prec
= TYPE_PRECISION (TREE_TYPE (arg
));
3931 if (optab_handler (ctz_optab
, TYPE_MODE (TREE_TYPE (arg
)))
3933 && CTZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (TREE_TYPE (arg
)),
3936 /* Handle only the two common values. */
3939 else if (zerov
== prec
)
3942 /* Magic value to give up, unless vr0 proves
3946 if (TREE_CODE (arg
) == SSA_NAME
)
3948 value_range
*vr0
= get_value_range (arg
);
3949 /* If arg is non-zero, then use [0, prec - 1]. */
3950 if (((vr0
->type
== VR_RANGE
3951 && integer_nonzerop (vr0
->min
))
3952 || (vr0
->type
== VR_ANTI_RANGE
3953 && integer_zerop (vr0
->min
)))
3954 && !is_overflow_infinity (vr0
->min
))
3959 /* If some high bits are known to be zero,
3960 we can decrease the result maximum. */
3961 if (vr0
->type
== VR_RANGE
3962 && TREE_CODE (vr0
->max
) == INTEGER_CST
3963 && !is_overflow_infinity (vr0
->max
))
3965 maxi
= tree_floor_log2 (vr0
->max
);
3966 /* For vr0 [0, 0] give up. */
3974 /* __builtin_clrsb* returns [0, prec-1]. */
3976 arg
= gimple_call_arg (stmt
, 0);
3977 prec
= TYPE_PRECISION (TREE_TYPE (arg
));
3982 set_value_range (vr
, VR_RANGE
, build_int_cst (type
, mini
),
3983 build_int_cst (type
, maxi
), NULL
);
3985 case CFN_UBSAN_CHECK_ADD
:
3986 subcode
= PLUS_EXPR
;
3988 case CFN_UBSAN_CHECK_SUB
:
3989 subcode
= MINUS_EXPR
;
3991 case CFN_UBSAN_CHECK_MUL
:
3992 subcode
= MULT_EXPR
;
3994 case CFN_GOACC_DIM_SIZE
:
3995 case CFN_GOACC_DIM_POS
:
3996 /* Optimizing these two internal functions helps the loop
3997 optimizer eliminate outer comparisons. Size is [1,N]
3998 and pos is [0,N-1]. */
4000 bool is_pos
= cfn
== CFN_GOACC_DIM_POS
;
4001 int axis
= get_oacc_ifn_dim_arg (stmt
);
4002 int size
= get_oacc_fn_dim_size (current_function_decl
, axis
);
4005 /* If it's dynamic, the backend might know a hardware
4007 size
= targetm
.goacc
.dim_limit (axis
);
4009 tree type
= TREE_TYPE (gimple_call_lhs (stmt
));
4010 set_value_range (vr
, VR_RANGE
,
4011 build_int_cst (type
, is_pos
? 0 : 1),
4012 size
? build_int_cst (type
, size
- is_pos
)
4013 : vrp_val_max (type
), NULL
);
4019 if (subcode
!= ERROR_MARK
)
4021 bool saved_flag_wrapv
= flag_wrapv
;
4022 /* Pretend the arithmetics is wrapping. If there is
4023 any overflow, we'll complain, but will actually do
4024 wrapping operation. */
4026 extract_range_from_binary_expr (vr
, subcode
, type
,
4027 gimple_call_arg (stmt
, 0),
4028 gimple_call_arg (stmt
, 1));
4029 flag_wrapv
= saved_flag_wrapv
;
4031 /* If for both arguments vrp_valueize returned non-NULL,
4032 this should have been already folded and if not, it
4033 wasn't folded because of overflow. Avoid removing the
4034 UBSAN_CHECK_* calls in that case. */
4035 if (vr
->type
== VR_RANGE
4036 && (vr
->min
== vr
->max
4037 || operand_equal_p (vr
->min
, vr
->max
, 0)))
4038 set_value_range_to_varying (vr
);
4042 /* Handle extraction of the two results (result of arithmetics and
4043 a flag whether arithmetics overflowed) from {ADD,SUB,MUL}_OVERFLOW
4044 internal function. */
4045 else if (is_gimple_assign (stmt
)
4046 && (gimple_assign_rhs_code (stmt
) == REALPART_EXPR
4047 || gimple_assign_rhs_code (stmt
) == IMAGPART_EXPR
)
4048 && INTEGRAL_TYPE_P (type
))
4050 enum tree_code code
= gimple_assign_rhs_code (stmt
);
4051 tree op
= gimple_assign_rhs1 (stmt
);
4052 if (TREE_CODE (op
) == code
&& TREE_CODE (TREE_OPERAND (op
, 0)) == SSA_NAME
)
4054 gimple
*g
= SSA_NAME_DEF_STMT (TREE_OPERAND (op
, 0));
4055 if (is_gimple_call (g
) && gimple_call_internal_p (g
))
4057 enum tree_code subcode
= ERROR_MARK
;
4058 switch (gimple_call_internal_fn (g
))
4060 case IFN_ADD_OVERFLOW
:
4061 subcode
= PLUS_EXPR
;
4063 case IFN_SUB_OVERFLOW
:
4064 subcode
= MINUS_EXPR
;
4066 case IFN_MUL_OVERFLOW
:
4067 subcode
= MULT_EXPR
;
4072 if (subcode
!= ERROR_MARK
)
4074 tree op0
= gimple_call_arg (g
, 0);
4075 tree op1
= gimple_call_arg (g
, 1);
4076 if (code
== IMAGPART_EXPR
)
4079 if (check_for_binary_op_overflow (subcode
, type
,
4081 set_value_range_to_value (vr
,
4082 build_int_cst (type
, ovf
),
4084 else if (TYPE_PRECISION (type
) == 1
4085 && !TYPE_UNSIGNED (type
))
4086 set_value_range_to_varying (vr
);
4088 set_value_range (vr
, VR_RANGE
, build_int_cst (type
, 0),
4089 build_int_cst (type
, 1), NULL
);
4091 else if (types_compatible_p (type
, TREE_TYPE (op0
))
4092 && types_compatible_p (type
, TREE_TYPE (op1
)))
4094 bool saved_flag_wrapv
= flag_wrapv
;
4095 /* Pretend the arithmetics is wrapping. If there is
4096 any overflow, IMAGPART_EXPR will be set. */
4098 extract_range_from_binary_expr (vr
, subcode
, type
,
4100 flag_wrapv
= saved_flag_wrapv
;
4104 value_range vr0
= VR_INITIALIZER
;
4105 value_range vr1
= VR_INITIALIZER
;
4106 bool saved_flag_wrapv
= flag_wrapv
;
4107 /* Pretend the arithmetics is wrapping. If there is
4108 any overflow, IMAGPART_EXPR will be set. */
4110 extract_range_from_unary_expr (&vr0
, NOP_EXPR
,
4112 extract_range_from_unary_expr (&vr1
, NOP_EXPR
,
4114 extract_range_from_binary_expr_1 (vr
, subcode
, type
,
4116 flag_wrapv
= saved_flag_wrapv
;
4123 if (INTEGRAL_TYPE_P (type
)
4124 && gimple_stmt_nonnegative_warnv_p (stmt
, &sop
))
4125 set_value_range_to_nonnegative (vr
, type
,
4126 sop
|| stmt_overflow_infinity (stmt
));
4127 else if (vrp_stmt_computes_nonzero (stmt
, &sop
)
4129 set_value_range_to_nonnull (vr
, type
);
4131 set_value_range_to_varying (vr
);
4135 /* Try to compute a useful range out of assignment STMT and store it
4139 extract_range_from_assignment (value_range
*vr
, gassign
*stmt
)
4141 enum tree_code code
= gimple_assign_rhs_code (stmt
);
4143 if (code
== ASSERT_EXPR
)
4144 extract_range_from_assert (vr
, gimple_assign_rhs1 (stmt
));
4145 else if (code
== SSA_NAME
)
4146 extract_range_from_ssa_name (vr
, gimple_assign_rhs1 (stmt
));
4147 else if (TREE_CODE_CLASS (code
) == tcc_binary
)
4148 extract_range_from_binary_expr (vr
, gimple_assign_rhs_code (stmt
),
4149 gimple_expr_type (stmt
),
4150 gimple_assign_rhs1 (stmt
),
4151 gimple_assign_rhs2 (stmt
));
4152 else if (TREE_CODE_CLASS (code
) == tcc_unary
)
4153 extract_range_from_unary_expr (vr
, gimple_assign_rhs_code (stmt
),
4154 gimple_expr_type (stmt
),
4155 gimple_assign_rhs1 (stmt
));
4156 else if (code
== COND_EXPR
)
4157 extract_range_from_cond_expr (vr
, stmt
);
4158 else if (TREE_CODE_CLASS (code
) == tcc_comparison
)
4159 extract_range_from_comparison (vr
, gimple_assign_rhs_code (stmt
),
4160 gimple_expr_type (stmt
),
4161 gimple_assign_rhs1 (stmt
),
4162 gimple_assign_rhs2 (stmt
));
4163 else if (get_gimple_rhs_class (code
) == GIMPLE_SINGLE_RHS
4164 && is_gimple_min_invariant (gimple_assign_rhs1 (stmt
)))
4165 set_value_range_to_value (vr
, gimple_assign_rhs1 (stmt
), NULL
);
4167 set_value_range_to_varying (vr
);
4169 if (vr
->type
== VR_VARYING
)
4170 extract_range_basic (vr
, stmt
);
4173 /* Given a range VR, a LOOP and a variable VAR, determine whether it
4174 would be profitable to adjust VR using scalar evolution information
4175 for VAR. If so, update VR with the new limits. */
4178 adjust_range_with_scev (value_range
*vr
, struct loop
*loop
,
4179 gimple
*stmt
, tree var
)
4181 tree init
, step
, chrec
, tmin
, tmax
, min
, max
, type
, tem
;
4182 enum ev_direction dir
;
4184 /* TODO. Don't adjust anti-ranges. An anti-range may provide
4185 better opportunities than a regular range, but I'm not sure. */
4186 if (vr
->type
== VR_ANTI_RANGE
)
4189 chrec
= instantiate_parameters (loop
, analyze_scalar_evolution (loop
, var
));
4191 /* Like in PR19590, scev can return a constant function. */
4192 if (is_gimple_min_invariant (chrec
))
4194 set_value_range_to_value (vr
, chrec
, vr
->equiv
);
4198 if (TREE_CODE (chrec
) != POLYNOMIAL_CHREC
)
4201 init
= initial_condition_in_loop_num (chrec
, loop
->num
);
4202 tem
= op_with_constant_singleton_value_range (init
);
4205 step
= evolution_part_in_loop_num (chrec
, loop
->num
);
4206 tem
= op_with_constant_singleton_value_range (step
);
4210 /* If STEP is symbolic, we can't know whether INIT will be the
4211 minimum or maximum value in the range. Also, unless INIT is
4212 a simple expression, compare_values and possibly other functions
4213 in tree-vrp won't be able to handle it. */
4214 if (step
== NULL_TREE
4215 || !is_gimple_min_invariant (step
)
4216 || !valid_value_p (init
))
4219 dir
= scev_direction (chrec
);
4220 if (/* Do not adjust ranges if we do not know whether the iv increases
4221 or decreases, ... */
4222 dir
== EV_DIR_UNKNOWN
4223 /* ... or if it may wrap. */
4224 || scev_probably_wraps_p (NULL_TREE
, init
, step
, stmt
,
4225 get_chrec_loop (chrec
), true))
4228 /* We use TYPE_MIN_VALUE and TYPE_MAX_VALUE here instead of
4229 negative_overflow_infinity and positive_overflow_infinity,
4230 because we have concluded that the loop probably does not
4233 type
= TREE_TYPE (var
);
4234 if (POINTER_TYPE_P (type
) || !TYPE_MIN_VALUE (type
))
4235 tmin
= lower_bound_in_type (type
, type
);
4237 tmin
= TYPE_MIN_VALUE (type
);
4238 if (POINTER_TYPE_P (type
) || !TYPE_MAX_VALUE (type
))
4239 tmax
= upper_bound_in_type (type
, type
);
4241 tmax
= TYPE_MAX_VALUE (type
);
4243 /* Try to use estimated number of iterations for the loop to constrain the
4244 final value in the evolution. */
4245 if (TREE_CODE (step
) == INTEGER_CST
4246 && is_gimple_val (init
)
4247 && (TREE_CODE (init
) != SSA_NAME
4248 || get_value_range (init
)->type
== VR_RANGE
))
4252 /* We are only entering here for loop header PHI nodes, so using
4253 the number of latch executions is the correct thing to use. */
4254 if (max_loop_iterations (loop
, &nit
))
4256 value_range maxvr
= VR_INITIALIZER
;
4257 signop sgn
= TYPE_SIGN (TREE_TYPE (step
));
4260 widest_int wtmp
= wi::mul (wi::to_widest (step
), nit
, sgn
,
4262 /* If the multiplication overflowed we can't do a meaningful
4263 adjustment. Likewise if the result doesn't fit in the type
4264 of the induction variable. For a signed type we have to
4265 check whether the result has the expected signedness which
4266 is that of the step as number of iterations is unsigned. */
4268 && wi::fits_to_tree_p (wtmp
, TREE_TYPE (init
))
4270 || wi::gts_p (wtmp
, 0) == wi::gts_p (step
, 0)))
4272 tem
= wide_int_to_tree (TREE_TYPE (init
), wtmp
);
4273 extract_range_from_binary_expr (&maxvr
, PLUS_EXPR
,
4274 TREE_TYPE (init
), init
, tem
);
4275 /* Likewise if the addition did. */
4276 if (maxvr
.type
== VR_RANGE
)
4278 value_range initvr
= VR_INITIALIZER
;
4280 if (TREE_CODE (init
) == SSA_NAME
)
4281 initvr
= *(get_value_range (init
));
4282 else if (is_gimple_min_invariant (init
))
4283 set_value_range_to_value (&initvr
, init
, NULL
);
4287 /* Check if init + nit * step overflows. Though we checked
4288 scev {init, step}_loop doesn't wrap, it is not enough
4289 because the loop may exit immediately. Overflow could
4290 happen in the plus expression in this case. */
4291 if ((dir
== EV_DIR_DECREASES
4292 && (is_negative_overflow_infinity (maxvr
.min
)
4293 || compare_values (maxvr
.min
, initvr
.min
) != -1))
4294 || (dir
== EV_DIR_GROWS
4295 && (is_positive_overflow_infinity (maxvr
.max
)
4296 || compare_values (maxvr
.max
, initvr
.max
) != 1)))
4306 if (vr
->type
== VR_VARYING
|| vr
->type
== VR_UNDEFINED
)
4311 /* For VARYING or UNDEFINED ranges, just about anything we get
4312 from scalar evolutions should be better. */
4314 if (dir
== EV_DIR_DECREASES
)
4319 else if (vr
->type
== VR_RANGE
)
4324 if (dir
== EV_DIR_DECREASES
)
4326 /* INIT is the maximum value. If INIT is lower than VR->MAX
4327 but no smaller than VR->MIN, set VR->MAX to INIT. */
4328 if (compare_values (init
, max
) == -1)
4331 /* According to the loop information, the variable does not
4332 overflow. If we think it does, probably because of an
4333 overflow due to arithmetic on a different INF value,
4335 if (is_negative_overflow_infinity (min
)
4336 || compare_values (min
, tmin
) == -1)
4342 /* If INIT is bigger than VR->MIN, set VR->MIN to INIT. */
4343 if (compare_values (init
, min
) == 1)
4346 if (is_positive_overflow_infinity (max
)
4347 || compare_values (tmax
, max
) == -1)
4354 /* If we just created an invalid range with the minimum
4355 greater than the maximum, we fail conservatively.
4356 This should happen only in unreachable
4357 parts of code, or for invalid programs. */
4358 if (compare_values (min
, max
) == 1
4359 || (is_negative_overflow_infinity (min
)
4360 && is_positive_overflow_infinity (max
)))
4363 /* Even for valid range info, sometimes overflow flag will leak in.
4364 As GIMPLE IL should have no constants with TREE_OVERFLOW set, we
4365 drop them except for +-overflow_infinity which still need special
4366 handling in vrp pass. */
4367 if (TREE_OVERFLOW_P (min
)
4368 && ! is_negative_overflow_infinity (min
))
4369 min
= drop_tree_overflow (min
);
4370 if (TREE_OVERFLOW_P (max
)
4371 && ! is_positive_overflow_infinity (max
))
4372 max
= drop_tree_overflow (max
);
4374 set_value_range (vr
, VR_RANGE
, min
, max
, vr
->equiv
);
4378 /* Given two numeric value ranges VR0, VR1 and a comparison code COMP:
4380 - Return BOOLEAN_TRUE_NODE if VR0 COMP VR1 always returns true for
4381 all the values in the ranges.
4383 - Return BOOLEAN_FALSE_NODE if the comparison always returns false.
4385 - Return NULL_TREE if it is not always possible to determine the
4386 value of the comparison.
4388 Also set *STRICT_OVERFLOW_P to indicate whether a range with an
4389 overflow infinity was used in the test. */
4393 compare_ranges (enum tree_code comp
, value_range
*vr0
, value_range
*vr1
,
4394 bool *strict_overflow_p
)
4396 /* VARYING or UNDEFINED ranges cannot be compared. */
4397 if (vr0
->type
== VR_VARYING
4398 || vr0
->type
== VR_UNDEFINED
4399 || vr1
->type
== VR_VARYING
4400 || vr1
->type
== VR_UNDEFINED
)
4403 /* Anti-ranges need to be handled separately. */
4404 if (vr0
->type
== VR_ANTI_RANGE
|| vr1
->type
== VR_ANTI_RANGE
)
4406 /* If both are anti-ranges, then we cannot compute any
4408 if (vr0
->type
== VR_ANTI_RANGE
&& vr1
->type
== VR_ANTI_RANGE
)
4411 /* These comparisons are never statically computable. */
4418 /* Equality can be computed only between a range and an
4419 anti-range. ~[VAL1, VAL2] == [VAL1, VAL2] is always false. */
4420 if (vr0
->type
== VR_RANGE
)
4422 /* To simplify processing, make VR0 the anti-range. */
4423 value_range
*tmp
= vr0
;
4428 gcc_assert (comp
== NE_EXPR
|| comp
== EQ_EXPR
);
4430 if (compare_values_warnv (vr0
->min
, vr1
->min
, strict_overflow_p
) == 0
4431 && compare_values_warnv (vr0
->max
, vr1
->max
, strict_overflow_p
) == 0)
4432 return (comp
== NE_EXPR
) ? boolean_true_node
: boolean_false_node
;
4437 if (!usable_range_p (vr0
, strict_overflow_p
)
4438 || !usable_range_p (vr1
, strict_overflow_p
))
4441 /* Simplify processing. If COMP is GT_EXPR or GE_EXPR, switch the
4442 operands around and change the comparison code. */
4443 if (comp
== GT_EXPR
|| comp
== GE_EXPR
)
4445 comp
= (comp
== GT_EXPR
) ? LT_EXPR
: LE_EXPR
;
4446 std::swap (vr0
, vr1
);
4449 if (comp
== EQ_EXPR
)
4451 /* Equality may only be computed if both ranges represent
4452 exactly one value. */
4453 if (compare_values_warnv (vr0
->min
, vr0
->max
, strict_overflow_p
) == 0
4454 && compare_values_warnv (vr1
->min
, vr1
->max
, strict_overflow_p
) == 0)
4456 int cmp_min
= compare_values_warnv (vr0
->min
, vr1
->min
,
4458 int cmp_max
= compare_values_warnv (vr0
->max
, vr1
->max
,
4460 if (cmp_min
== 0 && cmp_max
== 0)
4461 return boolean_true_node
;
4462 else if (cmp_min
!= -2 && cmp_max
!= -2)
4463 return boolean_false_node
;
4465 /* If [V0_MIN, V1_MAX] < [V1_MIN, V1_MAX] then V0 != V1. */
4466 else if (compare_values_warnv (vr0
->min
, vr1
->max
,
4467 strict_overflow_p
) == 1
4468 || compare_values_warnv (vr1
->min
, vr0
->max
,
4469 strict_overflow_p
) == 1)
4470 return boolean_false_node
;
4474 else if (comp
== NE_EXPR
)
4478 /* If VR0 is completely to the left or completely to the right
4479 of VR1, they are always different. Notice that we need to
4480 make sure that both comparisons yield similar results to
4481 avoid comparing values that cannot be compared at
4483 cmp1
= compare_values_warnv (vr0
->max
, vr1
->min
, strict_overflow_p
);
4484 cmp2
= compare_values_warnv (vr0
->min
, vr1
->max
, strict_overflow_p
);
4485 if ((cmp1
== -1 && cmp2
== -1) || (cmp1
== 1 && cmp2
== 1))
4486 return boolean_true_node
;
4488 /* If VR0 and VR1 represent a single value and are identical,
4490 else if (compare_values_warnv (vr0
->min
, vr0
->max
,
4491 strict_overflow_p
) == 0
4492 && compare_values_warnv (vr1
->min
, vr1
->max
,
4493 strict_overflow_p
) == 0
4494 && compare_values_warnv (vr0
->min
, vr1
->min
,
4495 strict_overflow_p
) == 0
4496 && compare_values_warnv (vr0
->max
, vr1
->max
,
4497 strict_overflow_p
) == 0)
4498 return boolean_false_node
;
4500 /* Otherwise, they may or may not be different. */
4504 else if (comp
== LT_EXPR
|| comp
== LE_EXPR
)
4508 /* If VR0 is to the left of VR1, return true. */
4509 tst
= compare_values_warnv (vr0
->max
, vr1
->min
, strict_overflow_p
);
4510 if ((comp
== LT_EXPR
&& tst
== -1)
4511 || (comp
== LE_EXPR
&& (tst
== -1 || tst
== 0)))
4513 if (overflow_infinity_range_p (vr0
)
4514 || overflow_infinity_range_p (vr1
))
4515 *strict_overflow_p
= true;
4516 return boolean_true_node
;
4519 /* If VR0 is to the right of VR1, return false. */
4520 tst
= compare_values_warnv (vr0
->min
, vr1
->max
, strict_overflow_p
);
4521 if ((comp
== LT_EXPR
&& (tst
== 0 || tst
== 1))
4522 || (comp
== LE_EXPR
&& tst
== 1))
4524 if (overflow_infinity_range_p (vr0
)
4525 || overflow_infinity_range_p (vr1
))
4526 *strict_overflow_p
= true;
4527 return boolean_false_node
;
4530 /* Otherwise, we don't know. */
4538 /* Given a value range VR, a value VAL and a comparison code COMP, return
4539 BOOLEAN_TRUE_NODE if VR COMP VAL always returns true for all the
4540 values in VR. Return BOOLEAN_FALSE_NODE if the comparison
4541 always returns false. Return NULL_TREE if it is not always
4542 possible to determine the value of the comparison. Also set
4543 *STRICT_OVERFLOW_P to indicate whether a range with an overflow
4544 infinity was used in the test. */
4547 compare_range_with_value (enum tree_code comp
, value_range
*vr
, tree val
,
4548 bool *strict_overflow_p
)
4550 if (vr
->type
== VR_VARYING
|| vr
->type
== VR_UNDEFINED
)
4553 /* Anti-ranges need to be handled separately. */
4554 if (vr
->type
== VR_ANTI_RANGE
)
4556 /* For anti-ranges, the only predicates that we can compute at
4557 compile time are equality and inequality. */
4564 /* ~[VAL_1, VAL_2] OP VAL is known if VAL_1 <= VAL <= VAL_2. */
4565 if (value_inside_range (val
, vr
->min
, vr
->max
) == 1)
4566 return (comp
== NE_EXPR
) ? boolean_true_node
: boolean_false_node
;
4571 if (!usable_range_p (vr
, strict_overflow_p
))
4574 if (comp
== EQ_EXPR
)
4576 /* EQ_EXPR may only be computed if VR represents exactly
4578 if (compare_values_warnv (vr
->min
, vr
->max
, strict_overflow_p
) == 0)
4580 int cmp
= compare_values_warnv (vr
->min
, val
, strict_overflow_p
);
4582 return boolean_true_node
;
4583 else if (cmp
== -1 || cmp
== 1 || cmp
== 2)
4584 return boolean_false_node
;
4586 else if (compare_values_warnv (val
, vr
->min
, strict_overflow_p
) == -1
4587 || compare_values_warnv (vr
->max
, val
, strict_overflow_p
) == -1)
4588 return boolean_false_node
;
4592 else if (comp
== NE_EXPR
)
4594 /* If VAL is not inside VR, then they are always different. */
4595 if (compare_values_warnv (vr
->max
, val
, strict_overflow_p
) == -1
4596 || compare_values_warnv (vr
->min
, val
, strict_overflow_p
) == 1)
4597 return boolean_true_node
;
4599 /* If VR represents exactly one value equal to VAL, then return
4601 if (compare_values_warnv (vr
->min
, vr
->max
, strict_overflow_p
) == 0
4602 && compare_values_warnv (vr
->min
, val
, strict_overflow_p
) == 0)
4603 return boolean_false_node
;
4605 /* Otherwise, they may or may not be different. */
4608 else if (comp
== LT_EXPR
|| comp
== LE_EXPR
)
4612 /* If VR is to the left of VAL, return true. */
4613 tst
= compare_values_warnv (vr
->max
, val
, strict_overflow_p
);
4614 if ((comp
== LT_EXPR
&& tst
== -1)
4615 || (comp
== LE_EXPR
&& (tst
== -1 || tst
== 0)))
4617 if (overflow_infinity_range_p (vr
))
4618 *strict_overflow_p
= true;
4619 return boolean_true_node
;
4622 /* If VR is to the right of VAL, return false. */
4623 tst
= compare_values_warnv (vr
->min
, val
, strict_overflow_p
);
4624 if ((comp
== LT_EXPR
&& (tst
== 0 || tst
== 1))
4625 || (comp
== LE_EXPR
&& tst
== 1))
4627 if (overflow_infinity_range_p (vr
))
4628 *strict_overflow_p
= true;
4629 return boolean_false_node
;
4632 /* Otherwise, we don't know. */
4635 else if (comp
== GT_EXPR
|| comp
== GE_EXPR
)
4639 /* If VR is to the right of VAL, return true. */
4640 tst
= compare_values_warnv (vr
->min
, val
, strict_overflow_p
);
4641 if ((comp
== GT_EXPR
&& tst
== 1)
4642 || (comp
== GE_EXPR
&& (tst
== 0 || tst
== 1)))
4644 if (overflow_infinity_range_p (vr
))
4645 *strict_overflow_p
= true;
4646 return boolean_true_node
;
4649 /* If VR is to the left of VAL, return false. */
4650 tst
= compare_values_warnv (vr
->max
, val
, strict_overflow_p
);
4651 if ((comp
== GT_EXPR
&& (tst
== -1 || tst
== 0))
4652 || (comp
== GE_EXPR
&& tst
== -1))
4654 if (overflow_infinity_range_p (vr
))
4655 *strict_overflow_p
= true;
4656 return boolean_false_node
;
4659 /* Otherwise, we don't know. */
4667 /* Debugging dumps. */
4669 void dump_value_range (FILE *, const value_range
*);
4670 void debug_value_range (value_range
*);
4671 void dump_all_value_ranges (FILE *);
4672 void debug_all_value_ranges (void);
4673 void dump_vr_equiv (FILE *, bitmap
);
4674 void debug_vr_equiv (bitmap
);
4677 /* Dump value range VR to FILE. */
4680 dump_value_range (FILE *file
, const value_range
*vr
)
4683 fprintf (file
, "[]");
4684 else if (vr
->type
== VR_UNDEFINED
)
4685 fprintf (file
, "UNDEFINED");
4686 else if (vr
->type
== VR_RANGE
|| vr
->type
== VR_ANTI_RANGE
)
4688 tree type
= TREE_TYPE (vr
->min
);
4690 fprintf (file
, "%s[", (vr
->type
== VR_ANTI_RANGE
) ? "~" : "");
4692 if (is_negative_overflow_infinity (vr
->min
))
4693 fprintf (file
, "-INF(OVF)");
4694 else if (INTEGRAL_TYPE_P (type
)
4695 && !TYPE_UNSIGNED (type
)
4696 && vrp_val_is_min (vr
->min
))
4697 fprintf (file
, "-INF");
4699 print_generic_expr (file
, vr
->min
, 0);
4701 fprintf (file
, ", ");
4703 if (is_positive_overflow_infinity (vr
->max
))
4704 fprintf (file
, "+INF(OVF)");
4705 else if (INTEGRAL_TYPE_P (type
)
4706 && vrp_val_is_max (vr
->max
))
4707 fprintf (file
, "+INF");
4709 print_generic_expr (file
, vr
->max
, 0);
4711 fprintf (file
, "]");
4718 fprintf (file
, " EQUIVALENCES: { ");
4720 EXECUTE_IF_SET_IN_BITMAP (vr
->equiv
, 0, i
, bi
)
4722 print_generic_expr (file
, ssa_name (i
), 0);
4723 fprintf (file
, " ");
4727 fprintf (file
, "} (%u elements)", c
);
4730 else if (vr
->type
== VR_VARYING
)
4731 fprintf (file
, "VARYING");
4733 fprintf (file
, "INVALID RANGE");
4737 /* Dump value range VR to stderr. */
4740 debug_value_range (value_range
*vr
)
4742 dump_value_range (stderr
, vr
);
4743 fprintf (stderr
, "\n");
4747 /* Dump value ranges of all SSA_NAMEs to FILE. */
4750 dump_all_value_ranges (FILE *file
)
4754 for (i
= 0; i
< num_vr_values
; i
++)
4758 print_generic_expr (file
, ssa_name (i
), 0);
4759 fprintf (file
, ": ");
4760 dump_value_range (file
, vr_value
[i
]);
4761 fprintf (file
, "\n");
4765 fprintf (file
, "\n");
4769 /* Dump all value ranges to stderr. */
4772 debug_all_value_ranges (void)
4774 dump_all_value_ranges (stderr
);
4778 /* Given a COND_EXPR COND of the form 'V OP W', and an SSA name V,
4779 create a new SSA name N and return the assertion assignment
4780 'N = ASSERT_EXPR <V, V OP W>'. */
4783 build_assert_expr_for (tree cond
, tree v
)
4788 gcc_assert (TREE_CODE (v
) == SSA_NAME
4789 && COMPARISON_CLASS_P (cond
));
4791 a
= build2 (ASSERT_EXPR
, TREE_TYPE (v
), v
, cond
);
4792 assertion
= gimple_build_assign (NULL_TREE
, a
);
4794 /* The new ASSERT_EXPR, creates a new SSA name that replaces the
4795 operand of the ASSERT_EXPR. Create it so the new name and the old one
4796 are registered in the replacement table so that we can fix the SSA web
4797 after adding all the ASSERT_EXPRs. */
4798 create_new_def_for (v
, assertion
, NULL
);
4804 /* Return false if EXPR is a predicate expression involving floating
4808 fp_predicate (gimple
*stmt
)
4810 GIMPLE_CHECK (stmt
, GIMPLE_COND
);
4812 return FLOAT_TYPE_P (TREE_TYPE (gimple_cond_lhs (stmt
)));
4815 /* If the range of values taken by OP can be inferred after STMT executes,
4816 return the comparison code (COMP_CODE_P) and value (VAL_P) that
4817 describes the inferred range. Return true if a range could be
4821 infer_value_range (gimple
*stmt
, tree op
, tree_code
*comp_code_p
, tree
*val_p
)
4824 *comp_code_p
= ERROR_MARK
;
4826 /* Do not attempt to infer anything in names that flow through
4828 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op
))
4831 /* If STMT is the last statement of a basic block with no normal
4832 successors, there is no point inferring anything about any of its
4833 operands. We would not be able to find a proper insertion point
4834 for the assertion, anyway. */
4835 if (stmt_ends_bb_p (stmt
))
4840 FOR_EACH_EDGE (e
, ei
, gimple_bb (stmt
)->succs
)
4841 if (!(e
->flags
& (EDGE_ABNORMAL
|EDGE_EH
)))
4847 if (infer_nonnull_range (stmt
, op
))
4849 *val_p
= build_int_cst (TREE_TYPE (op
), 0);
4850 *comp_code_p
= NE_EXPR
;
4858 void dump_asserts_for (FILE *, tree
);
4859 void debug_asserts_for (tree
);
4860 void dump_all_asserts (FILE *);
4861 void debug_all_asserts (void);
4863 /* Dump all the registered assertions for NAME to FILE. */
4866 dump_asserts_for (FILE *file
, tree name
)
4870 fprintf (file
, "Assertions to be inserted for ");
4871 print_generic_expr (file
, name
, 0);
4872 fprintf (file
, "\n");
4874 loc
= asserts_for
[SSA_NAME_VERSION (name
)];
4877 fprintf (file
, "\t");
4878 print_gimple_stmt (file
, gsi_stmt (loc
->si
), 0, 0);
4879 fprintf (file
, "\n\tBB #%d", loc
->bb
->index
);
4882 fprintf (file
, "\n\tEDGE %d->%d", loc
->e
->src
->index
,
4883 loc
->e
->dest
->index
);
4884 dump_edge_info (file
, loc
->e
, dump_flags
, 0);
4886 fprintf (file
, "\n\tPREDICATE: ");
4887 print_generic_expr (file
, loc
->expr
, 0);
4888 fprintf (file
, " %s ", get_tree_code_name (loc
->comp_code
));
4889 print_generic_expr (file
, loc
->val
, 0);
4890 fprintf (file
, "\n\n");
4894 fprintf (file
, "\n");
4898 /* Dump all the registered assertions for NAME to stderr. */
4901 debug_asserts_for (tree name
)
4903 dump_asserts_for (stderr
, name
);
4907 /* Dump all the registered assertions for all the names to FILE. */
4910 dump_all_asserts (FILE *file
)
4915 fprintf (file
, "\nASSERT_EXPRs to be inserted\n\n");
4916 EXECUTE_IF_SET_IN_BITMAP (need_assert_for
, 0, i
, bi
)
4917 dump_asserts_for (file
, ssa_name (i
));
4918 fprintf (file
, "\n");
4922 /* Dump all the registered assertions for all the names to stderr. */
4925 debug_all_asserts (void)
4927 dump_all_asserts (stderr
);
4931 /* If NAME doesn't have an ASSERT_EXPR registered for asserting
4932 'EXPR COMP_CODE VAL' at a location that dominates block BB or
4933 E->DEST, then register this location as a possible insertion point
4934 for ASSERT_EXPR <NAME, EXPR COMP_CODE VAL>.
4936 BB, E and SI provide the exact insertion point for the new
4937 ASSERT_EXPR. If BB is NULL, then the ASSERT_EXPR is to be inserted
4938 on edge E. Otherwise, if E is NULL, the ASSERT_EXPR is inserted on
4939 BB. If SI points to a COND_EXPR or a SWITCH_EXPR statement, then E
4940 must not be NULL. */
4943 register_new_assert_for (tree name
, tree expr
,
4944 enum tree_code comp_code
,
4948 gimple_stmt_iterator si
)
4950 assert_locus
*n
, *loc
, *last_loc
;
4951 basic_block dest_bb
;
4953 gcc_checking_assert (bb
== NULL
|| e
== NULL
);
4956 gcc_checking_assert (gimple_code (gsi_stmt (si
)) != GIMPLE_COND
4957 && gimple_code (gsi_stmt (si
)) != GIMPLE_SWITCH
);
4959 /* Never build an assert comparing against an integer constant with
4960 TREE_OVERFLOW set. This confuses our undefined overflow warning
4962 if (TREE_OVERFLOW_P (val
))
4963 val
= drop_tree_overflow (val
);
4965 /* The new assertion A will be inserted at BB or E. We need to
4966 determine if the new location is dominated by a previously
4967 registered location for A. If we are doing an edge insertion,
4968 assume that A will be inserted at E->DEST. Note that this is not
4971 If E is a critical edge, it will be split. But even if E is
4972 split, the new block will dominate the same set of blocks that
4975 The reverse, however, is not true, blocks dominated by E->DEST
4976 will not be dominated by the new block created to split E. So,
4977 if the insertion location is on a critical edge, we will not use
4978 the new location to move another assertion previously registered
4979 at a block dominated by E->DEST. */
4980 dest_bb
= (bb
) ? bb
: e
->dest
;
4982 /* If NAME already has an ASSERT_EXPR registered for COMP_CODE and
4983 VAL at a block dominating DEST_BB, then we don't need to insert a new
4984 one. Similarly, if the same assertion already exists at a block
4985 dominated by DEST_BB and the new location is not on a critical
4986 edge, then update the existing location for the assertion (i.e.,
4987 move the assertion up in the dominance tree).
4989 Note, this is implemented as a simple linked list because there
4990 should not be more than a handful of assertions registered per
4991 name. If this becomes a performance problem, a table hashed by
4992 COMP_CODE and VAL could be implemented. */
4993 loc
= asserts_for
[SSA_NAME_VERSION (name
)];
4997 if (loc
->comp_code
== comp_code
4999 || operand_equal_p (loc
->val
, val
, 0))
5000 && (loc
->expr
== expr
5001 || operand_equal_p (loc
->expr
, expr
, 0)))
5003 /* If E is not a critical edge and DEST_BB
5004 dominates the existing location for the assertion, move
5005 the assertion up in the dominance tree by updating its
5006 location information. */
5007 if ((e
== NULL
|| !EDGE_CRITICAL_P (e
))
5008 && dominated_by_p (CDI_DOMINATORS
, loc
->bb
, dest_bb
))
5017 /* Update the last node of the list and move to the next one. */
5022 /* If we didn't find an assertion already registered for
5023 NAME COMP_CODE VAL, add a new one at the end of the list of
5024 assertions associated with NAME. */
5025 n
= XNEW (struct assert_locus
);
5029 n
->comp_code
= comp_code
;
5037 asserts_for
[SSA_NAME_VERSION (name
)] = n
;
5039 bitmap_set_bit (need_assert_for
, SSA_NAME_VERSION (name
));
5042 /* (COND_OP0 COND_CODE COND_OP1) is a predicate which uses NAME.
5043 Extract a suitable test code and value and store them into *CODE_P and
5044 *VAL_P so the predicate is normalized to NAME *CODE_P *VAL_P.
5046 If no extraction was possible, return FALSE, otherwise return TRUE.
5048 If INVERT is true, then we invert the result stored into *CODE_P. */
5051 extract_code_and_val_from_cond_with_ops (tree name
, enum tree_code cond_code
,
5052 tree cond_op0
, tree cond_op1
,
5053 bool invert
, enum tree_code
*code_p
,
5056 enum tree_code comp_code
;
5059 /* Otherwise, we have a comparison of the form NAME COMP VAL
5060 or VAL COMP NAME. */
5061 if (name
== cond_op1
)
5063 /* If the predicate is of the form VAL COMP NAME, flip
5064 COMP around because we need to register NAME as the
5065 first operand in the predicate. */
5066 comp_code
= swap_tree_comparison (cond_code
);
5069 else if (name
== cond_op0
)
5071 /* The comparison is of the form NAME COMP VAL, so the
5072 comparison code remains unchanged. */
5073 comp_code
= cond_code
;
5079 /* Invert the comparison code as necessary. */
5081 comp_code
= invert_tree_comparison (comp_code
, 0);
5083 /* VRP only handles integral and pointer types. */
5084 if (! INTEGRAL_TYPE_P (TREE_TYPE (val
))
5085 && ! POINTER_TYPE_P (TREE_TYPE (val
)))
5088 /* Do not register always-false predicates.
5089 FIXME: this works around a limitation in fold() when dealing with
5090 enumerations. Given 'enum { N1, N2 } x;', fold will not
5091 fold 'if (x > N2)' to 'if (0)'. */
5092 if ((comp_code
== GT_EXPR
|| comp_code
== LT_EXPR
)
5093 && INTEGRAL_TYPE_P (TREE_TYPE (val
)))
5095 tree min
= TYPE_MIN_VALUE (TREE_TYPE (val
));
5096 tree max
= TYPE_MAX_VALUE (TREE_TYPE (val
));
5098 if (comp_code
== GT_EXPR
5100 || compare_values (val
, max
) == 0))
5103 if (comp_code
== LT_EXPR
5105 || compare_values (val
, min
) == 0))
5108 *code_p
= comp_code
;
5113 /* Find out smallest RES where RES > VAL && (RES & MASK) == RES, if any
5114 (otherwise return VAL). VAL and MASK must be zero-extended for
5115 precision PREC. If SGNBIT is non-zero, first xor VAL with SGNBIT
5116 (to transform signed values into unsigned) and at the end xor
5120 masked_increment (const wide_int
&val_in
, const wide_int
&mask
,
5121 const wide_int
&sgnbit
, unsigned int prec
)
5123 wide_int bit
= wi::one (prec
), res
;
5126 wide_int val
= val_in
^ sgnbit
;
5127 for (i
= 0; i
< prec
; i
++, bit
+= bit
)
5130 if ((res
& bit
) == 0)
5133 res
= (val
+ bit
).and_not (res
);
5135 if (wi::gtu_p (res
, val
))
5136 return res
^ sgnbit
;
5138 return val
^ sgnbit
;
5141 /* Try to register an edge assertion for SSA name NAME on edge E for
5142 the condition COND contributing to the conditional jump pointed to by BSI.
5143 Invert the condition COND if INVERT is true. */
5146 register_edge_assert_for_2 (tree name
, edge e
, gimple_stmt_iterator bsi
,
5147 enum tree_code cond_code
,
5148 tree cond_op0
, tree cond_op1
, bool invert
)
5151 enum tree_code comp_code
;
5153 if (!extract_code_and_val_from_cond_with_ops (name
, cond_code
,
5156 invert
, &comp_code
, &val
))
5159 /* Only register an ASSERT_EXPR if NAME was found in the sub-graph
5160 reachable from E. */
5161 if (live_on_edge (e
, name
))
5162 register_new_assert_for (name
, name
, comp_code
, val
, NULL
, e
, bsi
);
5164 /* In the case of NAME <= CST and NAME being defined as
5165 NAME = (unsigned) NAME2 + CST2 we can assert NAME2 >= -CST2
5166 and NAME2 <= CST - CST2. We can do the same for NAME > CST.
5167 This catches range and anti-range tests. */
5168 if ((comp_code
== LE_EXPR
5169 || comp_code
== GT_EXPR
)
5170 && TREE_CODE (val
) == INTEGER_CST
5171 && TYPE_UNSIGNED (TREE_TYPE (val
)))
5173 gimple
*def_stmt
= SSA_NAME_DEF_STMT (name
);
5174 tree cst2
= NULL_TREE
, name2
= NULL_TREE
, name3
= NULL_TREE
;
5176 /* Extract CST2 from the (optional) addition. */
5177 if (is_gimple_assign (def_stmt
)
5178 && gimple_assign_rhs_code (def_stmt
) == PLUS_EXPR
)
5180 name2
= gimple_assign_rhs1 (def_stmt
);
5181 cst2
= gimple_assign_rhs2 (def_stmt
);
5182 if (TREE_CODE (name2
) == SSA_NAME
5183 && TREE_CODE (cst2
) == INTEGER_CST
)
5184 def_stmt
= SSA_NAME_DEF_STMT (name2
);
5187 /* Extract NAME2 from the (optional) sign-changing cast. */
5188 if (gimple_assign_cast_p (def_stmt
))
5190 if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt
))
5191 && ! TYPE_UNSIGNED (TREE_TYPE (gimple_assign_rhs1 (def_stmt
)))
5192 && (TYPE_PRECISION (gimple_expr_type (def_stmt
))
5193 == TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt
)))))
5194 name3
= gimple_assign_rhs1 (def_stmt
);
5197 /* If name3 is used later, create an ASSERT_EXPR for it. */
5198 if (name3
!= NULL_TREE
5199 && TREE_CODE (name3
) == SSA_NAME
5200 && (cst2
== NULL_TREE
5201 || TREE_CODE (cst2
) == INTEGER_CST
)
5202 && INTEGRAL_TYPE_P (TREE_TYPE (name3
))
5203 && live_on_edge (e
, name3
))
5207 /* Build an expression for the range test. */
5208 tmp
= build1 (NOP_EXPR
, TREE_TYPE (name
), name3
);
5209 if (cst2
!= NULL_TREE
)
5210 tmp
= build2 (PLUS_EXPR
, TREE_TYPE (name
), tmp
, cst2
);
5214 fprintf (dump_file
, "Adding assert for ");
5215 print_generic_expr (dump_file
, name3
, 0);
5216 fprintf (dump_file
, " from ");
5217 print_generic_expr (dump_file
, tmp
, 0);
5218 fprintf (dump_file
, "\n");
5221 register_new_assert_for (name3
, tmp
, comp_code
, val
, NULL
, e
, bsi
);
5224 /* If name2 is used later, create an ASSERT_EXPR for it. */
5225 if (name2
!= NULL_TREE
5226 && TREE_CODE (name2
) == SSA_NAME
5227 && TREE_CODE (cst2
) == INTEGER_CST
5228 && INTEGRAL_TYPE_P (TREE_TYPE (name2
))
5229 && live_on_edge (e
, name2
))
5233 /* Build an expression for the range test. */
5235 if (TREE_TYPE (name
) != TREE_TYPE (name2
))
5236 tmp
= build1 (NOP_EXPR
, TREE_TYPE (name
), tmp
);
5237 if (cst2
!= NULL_TREE
)
5238 tmp
= build2 (PLUS_EXPR
, TREE_TYPE (name
), tmp
, cst2
);
5242 fprintf (dump_file
, "Adding assert for ");
5243 print_generic_expr (dump_file
, name2
, 0);
5244 fprintf (dump_file
, " from ");
5245 print_generic_expr (dump_file
, tmp
, 0);
5246 fprintf (dump_file
, "\n");
5249 register_new_assert_for (name2
, tmp
, comp_code
, val
, NULL
, e
, bsi
);
5253 /* In the case of post-in/decrement tests like if (i++) ... and uses
5254 of the in/decremented value on the edge the extra name we want to
5255 assert for is not on the def chain of the name compared. Instead
5256 it is in the set of use stmts.
5257 Similar cases happen for conversions that were simplified through
5258 fold_{sign_changed,widened}_comparison. */
5259 if ((comp_code
== NE_EXPR
5260 || comp_code
== EQ_EXPR
)
5261 && TREE_CODE (val
) == INTEGER_CST
)
5263 imm_use_iterator ui
;
5265 FOR_EACH_IMM_USE_STMT (use_stmt
, ui
, name
)
5267 if (!is_gimple_assign (use_stmt
))
5270 /* Cut off to use-stmts that are dominating the predecessor. */
5271 if (!dominated_by_p (CDI_DOMINATORS
, e
->src
, gimple_bb (use_stmt
)))
5274 tree name2
= gimple_assign_lhs (use_stmt
);
5275 if (TREE_CODE (name2
) != SSA_NAME
5276 || !live_on_edge (e
, name2
))
5279 enum tree_code code
= gimple_assign_rhs_code (use_stmt
);
5281 if (code
== PLUS_EXPR
5282 || code
== MINUS_EXPR
)
5284 cst
= gimple_assign_rhs2 (use_stmt
);
5285 if (TREE_CODE (cst
) != INTEGER_CST
)
5287 cst
= int_const_binop (code
, val
, cst
);
5289 else if (CONVERT_EXPR_CODE_P (code
))
5291 /* For truncating conversions we cannot record
5293 if (comp_code
== NE_EXPR
5294 && (TYPE_PRECISION (TREE_TYPE (name2
))
5295 < TYPE_PRECISION (TREE_TYPE (name
))))
5297 cst
= fold_convert (TREE_TYPE (name2
), val
);
5302 if (TREE_OVERFLOW_P (cst
))
5303 cst
= drop_tree_overflow (cst
);
5304 register_new_assert_for (name2
, name2
, comp_code
, cst
,
5309 if (TREE_CODE_CLASS (comp_code
) == tcc_comparison
5310 && TREE_CODE (val
) == INTEGER_CST
)
5312 gimple
*def_stmt
= SSA_NAME_DEF_STMT (name
);
5313 tree name2
= NULL_TREE
, names
[2], cst2
= NULL_TREE
;
5314 tree val2
= NULL_TREE
;
5315 unsigned int prec
= TYPE_PRECISION (TREE_TYPE (val
));
5316 wide_int mask
= wi::zero (prec
);
5317 unsigned int nprec
= prec
;
5318 enum tree_code rhs_code
= ERROR_MARK
;
5320 if (is_gimple_assign (def_stmt
))
5321 rhs_code
= gimple_assign_rhs_code (def_stmt
);
5323 /* In the case of NAME != CST1 where NAME = A +- CST2 we can
5324 assert that A != CST1 -+ CST2. */
5325 if ((comp_code
== EQ_EXPR
|| comp_code
== NE_EXPR
)
5326 && (rhs_code
== PLUS_EXPR
|| rhs_code
== MINUS_EXPR
))
5328 tree op0
= gimple_assign_rhs1 (def_stmt
);
5329 tree op1
= gimple_assign_rhs2 (def_stmt
);
5330 if (TREE_CODE (op0
) == SSA_NAME
5331 && TREE_CODE (op1
) == INTEGER_CST
5332 && live_on_edge (e
, op0
))
5334 enum tree_code reverse_op
= (rhs_code
== PLUS_EXPR
5335 ? MINUS_EXPR
: PLUS_EXPR
);
5336 op1
= int_const_binop (reverse_op
, val
, op1
);
5337 if (TREE_OVERFLOW (op1
))
5338 op1
= drop_tree_overflow (op1
);
5339 register_new_assert_for (op0
, op0
, comp_code
, op1
, NULL
, e
, bsi
);
5343 /* Add asserts for NAME cmp CST and NAME being defined
5344 as NAME = (int) NAME2. */
5345 if (!TYPE_UNSIGNED (TREE_TYPE (val
))
5346 && (comp_code
== LE_EXPR
|| comp_code
== LT_EXPR
5347 || comp_code
== GT_EXPR
|| comp_code
== GE_EXPR
)
5348 && gimple_assign_cast_p (def_stmt
))
5350 name2
= gimple_assign_rhs1 (def_stmt
);
5351 if (CONVERT_EXPR_CODE_P (rhs_code
)
5352 && INTEGRAL_TYPE_P (TREE_TYPE (name2
))
5353 && TYPE_UNSIGNED (TREE_TYPE (name2
))
5354 && prec
== TYPE_PRECISION (TREE_TYPE (name2
))
5355 && (comp_code
== LE_EXPR
|| comp_code
== GT_EXPR
5356 || !tree_int_cst_equal (val
,
5357 TYPE_MIN_VALUE (TREE_TYPE (val
))))
5358 && live_on_edge (e
, name2
))
5361 enum tree_code new_comp_code
= comp_code
;
5363 cst
= fold_convert (TREE_TYPE (name2
),
5364 TYPE_MIN_VALUE (TREE_TYPE (val
)));
5365 /* Build an expression for the range test. */
5366 tmp
= build2 (PLUS_EXPR
, TREE_TYPE (name2
), name2
, cst
);
5367 cst
= fold_build2 (PLUS_EXPR
, TREE_TYPE (name2
), cst
,
5368 fold_convert (TREE_TYPE (name2
), val
));
5369 if (comp_code
== LT_EXPR
|| comp_code
== GE_EXPR
)
5371 new_comp_code
= comp_code
== LT_EXPR
? LE_EXPR
: GT_EXPR
;
5372 cst
= fold_build2 (MINUS_EXPR
, TREE_TYPE (name2
), cst
,
5373 build_int_cst (TREE_TYPE (name2
), 1));
5378 fprintf (dump_file
, "Adding assert for ");
5379 print_generic_expr (dump_file
, name2
, 0);
5380 fprintf (dump_file
, " from ");
5381 print_generic_expr (dump_file
, tmp
, 0);
5382 fprintf (dump_file
, "\n");
5385 register_new_assert_for (name2
, tmp
, new_comp_code
, cst
, NULL
,
5390 /* Add asserts for NAME cmp CST and NAME being defined as
5391 NAME = NAME2 >> CST2.
5393 Extract CST2 from the right shift. */
5394 if (rhs_code
== RSHIFT_EXPR
)
5396 name2
= gimple_assign_rhs1 (def_stmt
);
5397 cst2
= gimple_assign_rhs2 (def_stmt
);
5398 if (TREE_CODE (name2
) == SSA_NAME
5399 && tree_fits_uhwi_p (cst2
)
5400 && INTEGRAL_TYPE_P (TREE_TYPE (name2
))
5401 && IN_RANGE (tree_to_uhwi (cst2
), 1, prec
- 1)
5402 && prec
== GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (val
)))
5403 && live_on_edge (e
, name2
))
5405 mask
= wi::mask (tree_to_uhwi (cst2
), false, prec
);
5406 val2
= fold_binary (LSHIFT_EXPR
, TREE_TYPE (val
), val
, cst2
);
5409 if (val2
!= NULL_TREE
5410 && TREE_CODE (val2
) == INTEGER_CST
5411 && simple_cst_equal (fold_build2 (RSHIFT_EXPR
,
5415 enum tree_code new_comp_code
= comp_code
;
5419 if (comp_code
== EQ_EXPR
|| comp_code
== NE_EXPR
)
5421 if (!TYPE_UNSIGNED (TREE_TYPE (val
)))
5423 tree type
= build_nonstandard_integer_type (prec
, 1);
5424 tmp
= build1 (NOP_EXPR
, type
, name2
);
5425 val2
= fold_convert (type
, val2
);
5427 tmp
= fold_build2 (MINUS_EXPR
, TREE_TYPE (tmp
), tmp
, val2
);
5428 new_val
= wide_int_to_tree (TREE_TYPE (tmp
), mask
);
5429 new_comp_code
= comp_code
== EQ_EXPR
? LE_EXPR
: GT_EXPR
;
5431 else if (comp_code
== LT_EXPR
|| comp_code
== GE_EXPR
)
5434 = wi::min_value (prec
, TYPE_SIGN (TREE_TYPE (val
)));
5436 if (minval
== new_val
)
5437 new_val
= NULL_TREE
;
5442 = wi::max_value (prec
, TYPE_SIGN (TREE_TYPE (val
)));
5445 new_val
= NULL_TREE
;
5447 new_val
= wide_int_to_tree (TREE_TYPE (val2
), mask
);
5454 fprintf (dump_file
, "Adding assert for ");
5455 print_generic_expr (dump_file
, name2
, 0);
5456 fprintf (dump_file
, " from ");
5457 print_generic_expr (dump_file
, tmp
, 0);
5458 fprintf (dump_file
, "\n");
5461 register_new_assert_for (name2
, tmp
, new_comp_code
, new_val
,
5466 /* Add asserts for NAME cmp CST and NAME being defined as
5467 NAME = NAME2 & CST2.
5469 Extract CST2 from the and.
5472 NAME = (unsigned) NAME2;
5473 casts where NAME's type is unsigned and has smaller precision
5474 than NAME2's type as if it was NAME = NAME2 & MASK. */
5475 names
[0] = NULL_TREE
;
5476 names
[1] = NULL_TREE
;
5478 if (rhs_code
== BIT_AND_EXPR
5479 || (CONVERT_EXPR_CODE_P (rhs_code
)
5480 && INTEGRAL_TYPE_P (TREE_TYPE (val
))
5481 && TYPE_UNSIGNED (TREE_TYPE (val
))
5482 && TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt
)))
5485 name2
= gimple_assign_rhs1 (def_stmt
);
5486 if (rhs_code
== BIT_AND_EXPR
)
5487 cst2
= gimple_assign_rhs2 (def_stmt
);
5490 cst2
= TYPE_MAX_VALUE (TREE_TYPE (val
));
5491 nprec
= TYPE_PRECISION (TREE_TYPE (name2
));
5493 if (TREE_CODE (name2
) == SSA_NAME
5494 && INTEGRAL_TYPE_P (TREE_TYPE (name2
))
5495 && TREE_CODE (cst2
) == INTEGER_CST
5496 && !integer_zerop (cst2
)
5498 || TYPE_UNSIGNED (TREE_TYPE (val
))))
5500 gimple
*def_stmt2
= SSA_NAME_DEF_STMT (name2
);
5501 if (gimple_assign_cast_p (def_stmt2
))
5503 names
[1] = gimple_assign_rhs1 (def_stmt2
);
5504 if (!CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt2
))
5505 || !INTEGRAL_TYPE_P (TREE_TYPE (names
[1]))
5506 || (TYPE_PRECISION (TREE_TYPE (name2
))
5507 != TYPE_PRECISION (TREE_TYPE (names
[1])))
5508 || !live_on_edge (e
, names
[1]))
5509 names
[1] = NULL_TREE
;
5511 if (live_on_edge (e
, name2
))
5515 if (names
[0] || names
[1])
5517 wide_int minv
, maxv
, valv
, cst2v
;
5518 wide_int tem
, sgnbit
;
5519 bool valid_p
= false, valn
, cst2n
;
5520 enum tree_code ccode
= comp_code
;
5522 valv
= wide_int::from (val
, nprec
, UNSIGNED
);
5523 cst2v
= wide_int::from (cst2
, nprec
, UNSIGNED
);
5524 valn
= wi::neg_p (valv
, TYPE_SIGN (TREE_TYPE (val
)));
5525 cst2n
= wi::neg_p (cst2v
, TYPE_SIGN (TREE_TYPE (val
)));
5526 /* If CST2 doesn't have most significant bit set,
5527 but VAL is negative, we have comparison like
5528 if ((x & 0x123) > -4) (always true). Just give up. */
5532 sgnbit
= wi::set_bit_in_zero (nprec
- 1, nprec
);
5534 sgnbit
= wi::zero (nprec
);
5535 minv
= valv
& cst2v
;
5539 /* Minimum unsigned value for equality is VAL & CST2
5540 (should be equal to VAL, otherwise we probably should
5541 have folded the comparison into false) and
5542 maximum unsigned value is VAL | ~CST2. */
5543 maxv
= valv
| ~cst2v
;
5548 tem
= valv
| ~cst2v
;
5549 /* If VAL is 0, handle (X & CST2) != 0 as (X & CST2) > 0U. */
5553 sgnbit
= wi::zero (nprec
);
5556 /* If (VAL | ~CST2) is all ones, handle it as
5557 (X & CST2) < VAL. */
5562 sgnbit
= wi::zero (nprec
);
5565 if (!cst2n
&& wi::neg_p (cst2v
))
5566 sgnbit
= wi::set_bit_in_zero (nprec
- 1, nprec
);
5575 if (tem
== wi::mask (nprec
- 1, false, nprec
))
5581 sgnbit
= wi::zero (nprec
);
5586 /* Minimum unsigned value for >= if (VAL & CST2) == VAL
5587 is VAL and maximum unsigned value is ~0. For signed
5588 comparison, if CST2 doesn't have most significant bit
5589 set, handle it similarly. If CST2 has MSB set,
5590 the minimum is the same, and maximum is ~0U/2. */
5593 /* If (VAL & CST2) != VAL, X & CST2 can't be equal to
5595 minv
= masked_increment (valv
, cst2v
, sgnbit
, nprec
);
5599 maxv
= wi::mask (nprec
- (cst2n
? 1 : 0), false, nprec
);
5605 /* Find out smallest MINV where MINV > VAL
5606 && (MINV & CST2) == MINV, if any. If VAL is signed and
5607 CST2 has MSB set, compute it biased by 1 << (nprec - 1). */
5608 minv
= masked_increment (valv
, cst2v
, sgnbit
, nprec
);
5611 maxv
= wi::mask (nprec
- (cst2n
? 1 : 0), false, nprec
);
5616 /* Minimum unsigned value for <= is 0 and maximum
5617 unsigned value is VAL | ~CST2 if (VAL & CST2) == VAL.
5618 Otherwise, find smallest VAL2 where VAL2 > VAL
5619 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
5621 For signed comparison, if CST2 doesn't have most
5622 significant bit set, handle it similarly. If CST2 has
5623 MSB set, the maximum is the same and minimum is INT_MIN. */
5628 maxv
= masked_increment (valv
, cst2v
, sgnbit
, nprec
);
5640 /* Minimum unsigned value for < is 0 and maximum
5641 unsigned value is (VAL-1) | ~CST2 if (VAL & CST2) == VAL.
5642 Otherwise, find smallest VAL2 where VAL2 > VAL
5643 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
5645 For signed comparison, if CST2 doesn't have most
5646 significant bit set, handle it similarly. If CST2 has
5647 MSB set, the maximum is the same and minimum is INT_MIN. */
5656 maxv
= masked_increment (valv
, cst2v
, sgnbit
, nprec
);
5670 && (maxv
- minv
) != -1)
5672 tree tmp
, new_val
, type
;
5675 for (i
= 0; i
< 2; i
++)
5678 wide_int maxv2
= maxv
;
5680 type
= TREE_TYPE (names
[i
]);
5681 if (!TYPE_UNSIGNED (type
))
5683 type
= build_nonstandard_integer_type (nprec
, 1);
5684 tmp
= build1 (NOP_EXPR
, type
, names
[i
]);
5688 tmp
= build2 (PLUS_EXPR
, type
, tmp
,
5689 wide_int_to_tree (type
, -minv
));
5690 maxv2
= maxv
- minv
;
5692 new_val
= wide_int_to_tree (type
, maxv2
);
5696 fprintf (dump_file
, "Adding assert for ");
5697 print_generic_expr (dump_file
, names
[i
], 0);
5698 fprintf (dump_file
, " from ");
5699 print_generic_expr (dump_file
, tmp
, 0);
5700 fprintf (dump_file
, "\n");
5703 register_new_assert_for (names
[i
], tmp
, LE_EXPR
,
5704 new_val
, NULL
, e
, bsi
);
5711 /* OP is an operand of a truth value expression which is known to have
5712 a particular value. Register any asserts for OP and for any
5713 operands in OP's defining statement.
5715 If CODE is EQ_EXPR, then we want to register OP is zero (false),
5716 if CODE is NE_EXPR, then we want to register OP is nonzero (true). */
5719 register_edge_assert_for_1 (tree op
, enum tree_code code
,
5720 edge e
, gimple_stmt_iterator bsi
)
5724 enum tree_code rhs_code
;
5726 /* We only care about SSA_NAMEs. */
5727 if (TREE_CODE (op
) != SSA_NAME
)
5730 /* We know that OP will have a zero or nonzero value. If OP is used
5731 more than once go ahead and register an assert for OP. */
5732 if (live_on_edge (e
, op
))
5734 val
= build_int_cst (TREE_TYPE (op
), 0);
5735 register_new_assert_for (op
, op
, code
, val
, NULL
, e
, bsi
);
5738 /* Now look at how OP is set. If it's set from a comparison,
5739 a truth operation or some bit operations, then we may be able
5740 to register information about the operands of that assignment. */
5741 op_def
= SSA_NAME_DEF_STMT (op
);
5742 if (gimple_code (op_def
) != GIMPLE_ASSIGN
)
5745 rhs_code
= gimple_assign_rhs_code (op_def
);
5747 if (TREE_CODE_CLASS (rhs_code
) == tcc_comparison
)
5749 bool invert
= (code
== EQ_EXPR
? true : false);
5750 tree op0
= gimple_assign_rhs1 (op_def
);
5751 tree op1
= gimple_assign_rhs2 (op_def
);
5753 if (TREE_CODE (op0
) == SSA_NAME
)
5754 register_edge_assert_for_2 (op0
, e
, bsi
, rhs_code
, op0
, op1
, invert
);
5755 if (TREE_CODE (op1
) == SSA_NAME
)
5756 register_edge_assert_for_2 (op1
, e
, bsi
, rhs_code
, op0
, op1
, invert
);
5758 else if ((code
== NE_EXPR
5759 && gimple_assign_rhs_code (op_def
) == BIT_AND_EXPR
)
5761 && gimple_assign_rhs_code (op_def
) == BIT_IOR_EXPR
))
5763 /* Recurse on each operand. */
5764 tree op0
= gimple_assign_rhs1 (op_def
);
5765 tree op1
= gimple_assign_rhs2 (op_def
);
5766 if (TREE_CODE (op0
) == SSA_NAME
5767 && has_single_use (op0
))
5768 register_edge_assert_for_1 (op0
, code
, e
, bsi
);
5769 if (TREE_CODE (op1
) == SSA_NAME
5770 && has_single_use (op1
))
5771 register_edge_assert_for_1 (op1
, code
, e
, bsi
);
5773 else if (gimple_assign_rhs_code (op_def
) == BIT_NOT_EXPR
5774 && TYPE_PRECISION (TREE_TYPE (gimple_assign_lhs (op_def
))) == 1)
5776 /* Recurse, flipping CODE. */
5777 code
= invert_tree_comparison (code
, false);
5778 register_edge_assert_for_1 (gimple_assign_rhs1 (op_def
), code
, e
, bsi
);
5780 else if (gimple_assign_rhs_code (op_def
) == SSA_NAME
)
5782 /* Recurse through the copy. */
5783 register_edge_assert_for_1 (gimple_assign_rhs1 (op_def
), code
, e
, bsi
);
5785 else if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (op_def
)))
5787 /* Recurse through the type conversion, unless it is a narrowing
5788 conversion or conversion from non-integral type. */
5789 tree rhs
= gimple_assign_rhs1 (op_def
);
5790 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs
))
5791 && (TYPE_PRECISION (TREE_TYPE (rhs
))
5792 <= TYPE_PRECISION (TREE_TYPE (op
))))
5793 register_edge_assert_for_1 (rhs
, code
, e
, bsi
);
5797 /* Try to register an edge assertion for SSA name NAME on edge E for
5798 the condition COND contributing to the conditional jump pointed to by
5802 register_edge_assert_for (tree name
, edge e
, gimple_stmt_iterator si
,
5803 enum tree_code cond_code
, tree cond_op0
,
5807 enum tree_code comp_code
;
5808 bool is_else_edge
= (e
->flags
& EDGE_FALSE_VALUE
) != 0;
5810 /* Do not attempt to infer anything in names that flow through
5812 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name
))
5815 if (!extract_code_and_val_from_cond_with_ops (name
, cond_code
,
5821 /* Register ASSERT_EXPRs for name. */
5822 register_edge_assert_for_2 (name
, e
, si
, cond_code
, cond_op0
,
5823 cond_op1
, is_else_edge
);
5826 /* If COND is effectively an equality test of an SSA_NAME against
5827 the value zero or one, then we may be able to assert values
5828 for SSA_NAMEs which flow into COND. */
5830 /* In the case of NAME == 1 or NAME != 0, for BIT_AND_EXPR defining
5831 statement of NAME we can assert both operands of the BIT_AND_EXPR
5832 have nonzero value. */
5833 if (((comp_code
== EQ_EXPR
&& integer_onep (val
))
5834 || (comp_code
== NE_EXPR
&& integer_zerop (val
))))
5836 gimple
*def_stmt
= SSA_NAME_DEF_STMT (name
);
5838 if (is_gimple_assign (def_stmt
)
5839 && gimple_assign_rhs_code (def_stmt
) == BIT_AND_EXPR
)
5841 tree op0
= gimple_assign_rhs1 (def_stmt
);
5842 tree op1
= gimple_assign_rhs2 (def_stmt
);
5843 register_edge_assert_for_1 (op0
, NE_EXPR
, e
, si
);
5844 register_edge_assert_for_1 (op1
, NE_EXPR
, e
, si
);
5848 /* In the case of NAME == 0 or NAME != 1, for BIT_IOR_EXPR defining
5849 statement of NAME we can assert both operands of the BIT_IOR_EXPR
5851 if (((comp_code
== EQ_EXPR
&& integer_zerop (val
))
5852 || (comp_code
== NE_EXPR
&& integer_onep (val
))))
5854 gimple
*def_stmt
= SSA_NAME_DEF_STMT (name
);
5856 /* For BIT_IOR_EXPR only if NAME == 0 both operands have
5857 necessarily zero value, or if type-precision is one. */
5858 if (is_gimple_assign (def_stmt
)
5859 && (gimple_assign_rhs_code (def_stmt
) == BIT_IOR_EXPR
5860 && (TYPE_PRECISION (TREE_TYPE (name
)) == 1
5861 || comp_code
== EQ_EXPR
)))
5863 tree op0
= gimple_assign_rhs1 (def_stmt
);
5864 tree op1
= gimple_assign_rhs2 (def_stmt
);
5865 register_edge_assert_for_1 (op0
, EQ_EXPR
, e
, si
);
5866 register_edge_assert_for_1 (op1
, EQ_EXPR
, e
, si
);
5872 /* Determine whether the outgoing edges of BB should receive an
5873 ASSERT_EXPR for each of the operands of BB's LAST statement.
5874 The last statement of BB must be a COND_EXPR.
5876 If any of the sub-graphs rooted at BB have an interesting use of
5877 the predicate operands, an assert location node is added to the
5878 list of assertions for the corresponding operands. */
5881 find_conditional_asserts (basic_block bb
, gcond
*last
)
5883 gimple_stmt_iterator bsi
;
5889 bsi
= gsi_for_stmt (last
);
5891 /* Look for uses of the operands in each of the sub-graphs
5892 rooted at BB. We need to check each of the outgoing edges
5893 separately, so that we know what kind of ASSERT_EXPR to
5895 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
5900 /* Register the necessary assertions for each operand in the
5901 conditional predicate. */
5902 FOR_EACH_SSA_TREE_OPERAND (op
, last
, iter
, SSA_OP_USE
)
5903 register_edge_assert_for (op
, e
, bsi
,
5904 gimple_cond_code (last
),
5905 gimple_cond_lhs (last
),
5906 gimple_cond_rhs (last
));
5916 /* Compare two case labels sorting first by the destination bb index
5917 and then by the case value. */
5920 compare_case_labels (const void *p1
, const void *p2
)
5922 const struct case_info
*ci1
= (const struct case_info
*) p1
;
5923 const struct case_info
*ci2
= (const struct case_info
*) p2
;
5924 int idx1
= ci1
->bb
->index
;
5925 int idx2
= ci2
->bb
->index
;
5929 else if (idx1
== idx2
)
5931 /* Make sure the default label is first in a group. */
5932 if (!CASE_LOW (ci1
->expr
))
5934 else if (!CASE_LOW (ci2
->expr
))
5937 return tree_int_cst_compare (CASE_LOW (ci1
->expr
),
5938 CASE_LOW (ci2
->expr
));
5944 /* Determine whether the outgoing edges of BB should receive an
5945 ASSERT_EXPR for each of the operands of BB's LAST statement.
5946 The last statement of BB must be a SWITCH_EXPR.
5948 If any of the sub-graphs rooted at BB have an interesting use of
5949 the predicate operands, an assert location node is added to the
5950 list of assertions for the corresponding operands. */
5953 find_switch_asserts (basic_block bb
, gswitch
*last
)
5955 gimple_stmt_iterator bsi
;
5958 struct case_info
*ci
;
5959 size_t n
= gimple_switch_num_labels (last
);
5960 #if GCC_VERSION >= 4000
5963 /* Work around GCC 3.4 bug (PR 37086). */
5964 volatile unsigned int idx
;
5967 bsi
= gsi_for_stmt (last
);
5968 op
= gimple_switch_index (last
);
5969 if (TREE_CODE (op
) != SSA_NAME
)
5972 /* Build a vector of case labels sorted by destination label. */
5973 ci
= XNEWVEC (struct case_info
, n
);
5974 for (idx
= 0; idx
< n
; ++idx
)
5976 ci
[idx
].expr
= gimple_switch_label (last
, idx
);
5977 ci
[idx
].bb
= label_to_block (CASE_LABEL (ci
[idx
].expr
));
5979 edge default_edge
= find_edge (bb
, ci
[0].bb
);
5980 qsort (ci
, n
, sizeof (struct case_info
), compare_case_labels
);
5982 for (idx
= 0; idx
< n
; ++idx
)
5985 tree cl
= ci
[idx
].expr
;
5986 basic_block cbb
= ci
[idx
].bb
;
5988 min
= CASE_LOW (cl
);
5989 max
= CASE_HIGH (cl
);
5991 /* If there are multiple case labels with the same destination
5992 we need to combine them to a single value range for the edge. */
5993 if (idx
+ 1 < n
&& cbb
== ci
[idx
+ 1].bb
)
5995 /* Skip labels until the last of the group. */
5998 } while (idx
< n
&& cbb
== ci
[idx
].bb
);
6001 /* Pick up the maximum of the case label range. */
6002 if (CASE_HIGH (ci
[idx
].expr
))
6003 max
= CASE_HIGH (ci
[idx
].expr
);
6005 max
= CASE_LOW (ci
[idx
].expr
);
6008 /* Can't extract a useful assertion out of a range that includes the
6010 if (min
== NULL_TREE
)
6013 /* Find the edge to register the assert expr on. */
6014 e
= find_edge (bb
, cbb
);
6016 /* Register the necessary assertions for the operand in the
6018 register_edge_assert_for (op
, e
, bsi
,
6019 max
? GE_EXPR
: EQ_EXPR
,
6020 op
, fold_convert (TREE_TYPE (op
), min
));
6022 register_edge_assert_for (op
, e
, bsi
, LE_EXPR
, op
,
6023 fold_convert (TREE_TYPE (op
), max
));
6028 if (!live_on_edge (default_edge
, op
))
6031 /* Now register along the default label assertions that correspond to the
6032 anti-range of each label. */
6033 int insertion_limit
= PARAM_VALUE (PARAM_MAX_VRP_SWITCH_ASSERTIONS
);
6034 for (idx
= 1; idx
< n
; idx
++)
6037 tree cl
= gimple_switch_label (last
, idx
);
6039 min
= CASE_LOW (cl
);
6040 max
= CASE_HIGH (cl
);
6042 /* Combine contiguous case ranges to reduce the number of assertions
6044 for (idx
= idx
+ 1; idx
< n
; idx
++)
6046 tree next_min
, next_max
;
6047 tree next_cl
= gimple_switch_label (last
, idx
);
6049 next_min
= CASE_LOW (next_cl
);
6050 next_max
= CASE_HIGH (next_cl
);
6052 wide_int difference
= wi::sub (next_min
, max
? max
: min
);
6053 if (wi::eq_p (difference
, 1))
6054 max
= next_max
? next_max
: next_min
;
6060 if (max
== NULL_TREE
)
6062 /* Register the assertion OP != MIN. */
6063 min
= fold_convert (TREE_TYPE (op
), min
);
6064 register_edge_assert_for (op
, default_edge
, bsi
, NE_EXPR
, op
, min
);
6068 /* Register the assertion (unsigned)OP - MIN > (MAX - MIN),
6069 which will give OP the anti-range ~[MIN,MAX]. */
6070 tree uop
= fold_convert (unsigned_type_for (TREE_TYPE (op
)), op
);
6071 min
= fold_convert (TREE_TYPE (uop
), min
);
6072 max
= fold_convert (TREE_TYPE (uop
), max
);
6074 tree lhs
= fold_build2 (MINUS_EXPR
, TREE_TYPE (uop
), uop
, min
);
6075 tree rhs
= int_const_binop (MINUS_EXPR
, max
, min
);
6076 register_new_assert_for (op
, lhs
, GT_EXPR
, rhs
,
6077 NULL
, default_edge
, bsi
);
6080 if (--insertion_limit
== 0)
6086 /* Traverse all the statements in block BB looking for statements that
6087 may generate useful assertions for the SSA names in their operand.
6088 If a statement produces a useful assertion A for name N_i, then the
6089 list of assertions already generated for N_i is scanned to
6090 determine if A is actually needed.
6092 If N_i already had the assertion A at a location dominating the
6093 current location, then nothing needs to be done. Otherwise, the
6094 new location for A is recorded instead.
6096 1- For every statement S in BB, all the variables used by S are
6097 added to bitmap FOUND_IN_SUBGRAPH.
6099 2- If statement S uses an operand N in a way that exposes a known
6100 value range for N, then if N was not already generated by an
6101 ASSERT_EXPR, create a new assert location for N. For instance,
6102 if N is a pointer and the statement dereferences it, we can
6103 assume that N is not NULL.
6105 3- COND_EXPRs are a special case of #2. We can derive range
6106 information from the predicate but need to insert different
6107 ASSERT_EXPRs for each of the sub-graphs rooted at the
6108 conditional block. If the last statement of BB is a conditional
6109 expression of the form 'X op Y', then
6111 a) Remove X and Y from the set FOUND_IN_SUBGRAPH.
6113 b) If the conditional is the only entry point to the sub-graph
6114 corresponding to the THEN_CLAUSE, recurse into it. On
6115 return, if X and/or Y are marked in FOUND_IN_SUBGRAPH, then
6116 an ASSERT_EXPR is added for the corresponding variable.
6118 c) Repeat step (b) on the ELSE_CLAUSE.
6120 d) Mark X and Y in FOUND_IN_SUBGRAPH.
6129 In this case, an assertion on the THEN clause is useful to
6130 determine that 'a' is always 9 on that edge. However, an assertion
6131 on the ELSE clause would be unnecessary.
6133 4- If BB does not end in a conditional expression, then we recurse
6134 into BB's dominator children.
6136 At the end of the recursive traversal, every SSA name will have a
6137 list of locations where ASSERT_EXPRs should be added. When a new
6138 location for name N is found, it is registered by calling
6139 register_new_assert_for. That function keeps track of all the
6140 registered assertions to prevent adding unnecessary assertions.
6141 For instance, if a pointer P_4 is dereferenced more than once in a
6142 dominator tree, only the location dominating all the dereference of
6143 P_4 will receive an ASSERT_EXPR. */
6146 find_assert_locations_1 (basic_block bb
, sbitmap live
)
6150 last
= last_stmt (bb
);
6152 /* If BB's last statement is a conditional statement involving integer
6153 operands, determine if we need to add ASSERT_EXPRs. */
6155 && gimple_code (last
) == GIMPLE_COND
6156 && !fp_predicate (last
)
6157 && !ZERO_SSA_OPERANDS (last
, SSA_OP_USE
))
6158 find_conditional_asserts (bb
, as_a
<gcond
*> (last
));
6160 /* If BB's last statement is a switch statement involving integer
6161 operands, determine if we need to add ASSERT_EXPRs. */
6163 && gimple_code (last
) == GIMPLE_SWITCH
6164 && !ZERO_SSA_OPERANDS (last
, SSA_OP_USE
))
6165 find_switch_asserts (bb
, as_a
<gswitch
*> (last
));
6167 /* Traverse all the statements in BB marking used names and looking
6168 for statements that may infer assertions for their used operands. */
6169 for (gimple_stmt_iterator si
= gsi_last_bb (bb
); !gsi_end_p (si
);
6176 stmt
= gsi_stmt (si
);
6178 if (is_gimple_debug (stmt
))
6181 /* See if we can derive an assertion for any of STMT's operands. */
6182 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, i
, SSA_OP_USE
)
6185 enum tree_code comp_code
;
6187 /* If op is not live beyond this stmt, do not bother to insert
6189 if (!bitmap_bit_p (live
, SSA_NAME_VERSION (op
)))
6192 /* If OP is used in such a way that we can infer a value
6193 range for it, and we don't find a previous assertion for
6194 it, create a new assertion location node for OP. */
6195 if (infer_value_range (stmt
, op
, &comp_code
, &value
))
6197 /* If we are able to infer a nonzero value range for OP,
6198 then walk backwards through the use-def chain to see if OP
6199 was set via a typecast.
6201 If so, then we can also infer a nonzero value range
6202 for the operand of the NOP_EXPR. */
6203 if (comp_code
== NE_EXPR
&& integer_zerop (value
))
6206 gimple
*def_stmt
= SSA_NAME_DEF_STMT (t
);
6208 while (is_gimple_assign (def_stmt
)
6209 && CONVERT_EXPR_CODE_P
6210 (gimple_assign_rhs_code (def_stmt
))
6212 (gimple_assign_rhs1 (def_stmt
)) == SSA_NAME
6214 (TREE_TYPE (gimple_assign_rhs1 (def_stmt
))))
6216 t
= gimple_assign_rhs1 (def_stmt
);
6217 def_stmt
= SSA_NAME_DEF_STMT (t
);
6219 /* Note we want to register the assert for the
6220 operand of the NOP_EXPR after SI, not after the
6222 if (bitmap_bit_p (live
, SSA_NAME_VERSION (t
)))
6223 register_new_assert_for (t
, t
, comp_code
, value
,
6228 register_new_assert_for (op
, op
, comp_code
, value
, bb
, NULL
, si
);
6233 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, i
, SSA_OP_USE
)
6234 bitmap_set_bit (live
, SSA_NAME_VERSION (op
));
6235 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, i
, SSA_OP_DEF
)
6236 bitmap_clear_bit (live
, SSA_NAME_VERSION (op
));
6239 /* Traverse all PHI nodes in BB, updating live. */
6240 for (gphi_iterator si
= gsi_start_phis (bb
); !gsi_end_p (si
);
6243 use_operand_p arg_p
;
6245 gphi
*phi
= si
.phi ();
6246 tree res
= gimple_phi_result (phi
);
6248 if (virtual_operand_p (res
))
6251 FOR_EACH_PHI_ARG (arg_p
, phi
, i
, SSA_OP_USE
)
6253 tree arg
= USE_FROM_PTR (arg_p
);
6254 if (TREE_CODE (arg
) == SSA_NAME
)
6255 bitmap_set_bit (live
, SSA_NAME_VERSION (arg
));
6258 bitmap_clear_bit (live
, SSA_NAME_VERSION (res
));
6262 /* Do an RPO walk over the function computing SSA name liveness
6263 on-the-fly and deciding on assert expressions to insert. */
6266 find_assert_locations (void)
6268 int *rpo
= XNEWVEC (int, last_basic_block_for_fn (cfun
));
6269 int *bb_rpo
= XNEWVEC (int, last_basic_block_for_fn (cfun
));
6270 int *last_rpo
= XCNEWVEC (int, last_basic_block_for_fn (cfun
));
6273 live
= XCNEWVEC (sbitmap
, last_basic_block_for_fn (cfun
));
6274 rpo_cnt
= pre_and_rev_post_order_compute (NULL
, rpo
, false);
6275 for (i
= 0; i
< rpo_cnt
; ++i
)
6278 /* Pre-seed loop latch liveness from loop header PHI nodes. Due to
6279 the order we compute liveness and insert asserts we otherwise
6280 fail to insert asserts into the loop latch. */
6282 FOR_EACH_LOOP (loop
, 0)
6284 i
= loop
->latch
->index
;
6285 unsigned int j
= single_succ_edge (loop
->latch
)->dest_idx
;
6286 for (gphi_iterator gsi
= gsi_start_phis (loop
->header
);
6287 !gsi_end_p (gsi
); gsi_next (&gsi
))
6289 gphi
*phi
= gsi
.phi ();
6290 if (virtual_operand_p (gimple_phi_result (phi
)))
6292 tree arg
= gimple_phi_arg_def (phi
, j
);
6293 if (TREE_CODE (arg
) == SSA_NAME
)
6295 if (live
[i
] == NULL
)
6297 live
[i
] = sbitmap_alloc (num_ssa_names
);
6298 bitmap_clear (live
[i
]);
6300 bitmap_set_bit (live
[i
], SSA_NAME_VERSION (arg
));
6305 for (i
= rpo_cnt
- 1; i
>= 0; --i
)
6307 basic_block bb
= BASIC_BLOCK_FOR_FN (cfun
, rpo
[i
]);
6313 live
[rpo
[i
]] = sbitmap_alloc (num_ssa_names
);
6314 bitmap_clear (live
[rpo
[i
]]);
6317 /* Process BB and update the live information with uses in
6319 find_assert_locations_1 (bb
, live
[rpo
[i
]]);
6321 /* Merge liveness into the predecessor blocks and free it. */
6322 if (!bitmap_empty_p (live
[rpo
[i
]]))
6325 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
6327 int pred
= e
->src
->index
;
6328 if ((e
->flags
& EDGE_DFS_BACK
) || pred
== ENTRY_BLOCK
)
6333 live
[pred
] = sbitmap_alloc (num_ssa_names
);
6334 bitmap_clear (live
[pred
]);
6336 bitmap_ior (live
[pred
], live
[pred
], live
[rpo
[i
]]);
6338 if (bb_rpo
[pred
] < pred_rpo
)
6339 pred_rpo
= bb_rpo
[pred
];
6342 /* Record the RPO number of the last visited block that needs
6343 live information from this block. */
6344 last_rpo
[rpo
[i
]] = pred_rpo
;
6348 sbitmap_free (live
[rpo
[i
]]);
6349 live
[rpo
[i
]] = NULL
;
6352 /* We can free all successors live bitmaps if all their
6353 predecessors have been visited already. */
6354 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
6355 if (last_rpo
[e
->dest
->index
] == i
6356 && live
[e
->dest
->index
])
6358 sbitmap_free (live
[e
->dest
->index
]);
6359 live
[e
->dest
->index
] = NULL
;
6364 XDELETEVEC (bb_rpo
);
6365 XDELETEVEC (last_rpo
);
6366 for (i
= 0; i
< last_basic_block_for_fn (cfun
); ++i
)
6368 sbitmap_free (live
[i
]);
6372 /* Create an ASSERT_EXPR for NAME and insert it in the location
6373 indicated by LOC. Return true if we made any edge insertions. */
6376 process_assert_insertions_for (tree name
, assert_locus
*loc
)
6378 /* Build the comparison expression NAME_i COMP_CODE VAL. */
6381 gimple
*assert_stmt
;
6385 /* If we have X <=> X do not insert an assert expr for that. */
6386 if (loc
->expr
== loc
->val
)
6389 cond
= build2 (loc
->comp_code
, boolean_type_node
, loc
->expr
, loc
->val
);
6390 assert_stmt
= build_assert_expr_for (cond
, name
);
6393 /* We have been asked to insert the assertion on an edge. This
6394 is used only by COND_EXPR and SWITCH_EXPR assertions. */
6395 gcc_checking_assert (gimple_code (gsi_stmt (loc
->si
)) == GIMPLE_COND
6396 || (gimple_code (gsi_stmt (loc
->si
))
6399 gsi_insert_on_edge (loc
->e
, assert_stmt
);
6403 /* Otherwise, we can insert right after LOC->SI iff the
6404 statement must not be the last statement in the block. */
6405 stmt
= gsi_stmt (loc
->si
);
6406 if (!stmt_ends_bb_p (stmt
))
6408 gsi_insert_after (&loc
->si
, assert_stmt
, GSI_SAME_STMT
);
6412 /* If STMT must be the last statement in BB, we can only insert new
6413 assertions on the non-abnormal edge out of BB. Note that since
6414 STMT is not control flow, there may only be one non-abnormal/eh edge
6416 FOR_EACH_EDGE (e
, ei
, loc
->bb
->succs
)
6417 if (!(e
->flags
& (EDGE_ABNORMAL
|EDGE_EH
)))
6419 gsi_insert_on_edge (e
, assert_stmt
);
6427 /* Process all the insertions registered for every name N_i registered
6428 in NEED_ASSERT_FOR. The list of assertions to be inserted are
6429 found in ASSERTS_FOR[i]. */
6432 process_assert_insertions (void)
6436 bool update_edges_p
= false;
6437 int num_asserts
= 0;
6439 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6440 dump_all_asserts (dump_file
);
6442 EXECUTE_IF_SET_IN_BITMAP (need_assert_for
, 0, i
, bi
)
6444 assert_locus
*loc
= asserts_for
[i
];
6449 assert_locus
*next
= loc
->next
;
6450 update_edges_p
|= process_assert_insertions_for (ssa_name (i
), loc
);
6458 gsi_commit_edge_inserts ();
6460 statistics_counter_event (cfun
, "Number of ASSERT_EXPR expressions inserted",
6465 /* Traverse the flowgraph looking for conditional jumps to insert range
6466 expressions. These range expressions are meant to provide information
6467 to optimizations that need to reason in terms of value ranges. They
6468 will not be expanded into RTL. For instance, given:
6477 this pass will transform the code into:
6483 x = ASSERT_EXPR <x, x < y>
6488 y = ASSERT_EXPR <y, x >= y>
6492 The idea is that once copy and constant propagation have run, other
6493 optimizations will be able to determine what ranges of values can 'x'
6494 take in different paths of the code, simply by checking the reaching
6495 definition of 'x'. */
6498 insert_range_assertions (void)
6500 need_assert_for
= BITMAP_ALLOC (NULL
);
6501 asserts_for
= XCNEWVEC (assert_locus
*, num_ssa_names
);
6503 calculate_dominance_info (CDI_DOMINATORS
);
6505 find_assert_locations ();
6506 if (!bitmap_empty_p (need_assert_for
))
6508 process_assert_insertions ();
6509 update_ssa (TODO_update_ssa_no_phi
);
6512 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6514 fprintf (dump_file
, "\nSSA form after inserting ASSERT_EXPRs\n");
6515 dump_function_to_file (current_function_decl
, dump_file
, dump_flags
);
6519 BITMAP_FREE (need_assert_for
);
6522 /* Checks one ARRAY_REF in REF, located at LOCUS. Ignores flexible arrays
6523 and "struct" hacks. If VRP can determine that the
6524 array subscript is a constant, check if it is outside valid
6525 range. If the array subscript is a RANGE, warn if it is
6526 non-overlapping with valid range.
6527 IGNORE_OFF_BY_ONE is true if the ARRAY_REF is inside a ADDR_EXPR. */
6530 check_array_ref (location_t location
, tree ref
, bool ignore_off_by_one
)
6532 value_range
*vr
= NULL
;
6533 tree low_sub
, up_sub
;
6534 tree low_bound
, up_bound
, up_bound_p1
;
6536 if (TREE_NO_WARNING (ref
))
6539 low_sub
= up_sub
= TREE_OPERAND (ref
, 1);
6540 up_bound
= array_ref_up_bound (ref
);
6542 /* Can not check flexible arrays. */
6544 || TREE_CODE (up_bound
) != INTEGER_CST
)
6547 /* Accesses to trailing arrays via pointers may access storage
6548 beyond the types array bounds. */
6549 if (warn_array_bounds
< 2
6550 && array_at_struct_end_p (ref
))
6553 low_bound
= array_ref_low_bound (ref
);
6554 up_bound_p1
= int_const_binop (PLUS_EXPR
, up_bound
,
6555 build_int_cst (TREE_TYPE (up_bound
), 1));
6558 if (tree_int_cst_equal (low_bound
, up_bound_p1
))
6560 warning_at (location
, OPT_Warray_bounds
,
6561 "array subscript is above array bounds");
6562 TREE_NO_WARNING (ref
) = 1;
6565 if (TREE_CODE (low_sub
) == SSA_NAME
)
6567 vr
= get_value_range (low_sub
);
6568 if (vr
->type
== VR_RANGE
|| vr
->type
== VR_ANTI_RANGE
)
6570 low_sub
= vr
->type
== VR_RANGE
? vr
->max
: vr
->min
;
6571 up_sub
= vr
->type
== VR_RANGE
? vr
->min
: vr
->max
;
6575 if (vr
&& vr
->type
== VR_ANTI_RANGE
)
6577 if (TREE_CODE (up_sub
) == INTEGER_CST
6578 && (ignore_off_by_one
6579 ? tree_int_cst_lt (up_bound
, up_sub
)
6580 : tree_int_cst_le (up_bound
, up_sub
))
6581 && TREE_CODE (low_sub
) == INTEGER_CST
6582 && tree_int_cst_le (low_sub
, low_bound
))
6584 warning_at (location
, OPT_Warray_bounds
,
6585 "array subscript is outside array bounds");
6586 TREE_NO_WARNING (ref
) = 1;
6589 else if (TREE_CODE (up_sub
) == INTEGER_CST
6590 && (ignore_off_by_one
6591 ? !tree_int_cst_le (up_sub
, up_bound_p1
)
6592 : !tree_int_cst_le (up_sub
, up_bound
)))
6594 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6596 fprintf (dump_file
, "Array bound warning for ");
6597 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, ref
);
6598 fprintf (dump_file
, "\n");
6600 warning_at (location
, OPT_Warray_bounds
,
6601 "array subscript is above array bounds");
6602 TREE_NO_WARNING (ref
) = 1;
6604 else if (TREE_CODE (low_sub
) == INTEGER_CST
6605 && tree_int_cst_lt (low_sub
, low_bound
))
6607 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6609 fprintf (dump_file
, "Array bound warning for ");
6610 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, ref
);
6611 fprintf (dump_file
, "\n");
6613 warning_at (location
, OPT_Warray_bounds
,
6614 "array subscript is below array bounds");
6615 TREE_NO_WARNING (ref
) = 1;
6619 /* Searches if the expr T, located at LOCATION computes
6620 address of an ARRAY_REF, and call check_array_ref on it. */
6623 search_for_addr_array (tree t
, location_t location
)
6625 /* Check each ARRAY_REFs in the reference chain. */
6628 if (TREE_CODE (t
) == ARRAY_REF
)
6629 check_array_ref (location
, t
, true /*ignore_off_by_one*/);
6631 t
= TREE_OPERAND (t
, 0);
6633 while (handled_component_p (t
));
6635 if (TREE_CODE (t
) == MEM_REF
6636 && TREE_CODE (TREE_OPERAND (t
, 0)) == ADDR_EXPR
6637 && !TREE_NO_WARNING (t
))
6639 tree tem
= TREE_OPERAND (TREE_OPERAND (t
, 0), 0);
6640 tree low_bound
, up_bound
, el_sz
;
6642 if (TREE_CODE (TREE_TYPE (tem
)) != ARRAY_TYPE
6643 || TREE_CODE (TREE_TYPE (TREE_TYPE (tem
))) == ARRAY_TYPE
6644 || !TYPE_DOMAIN (TREE_TYPE (tem
)))
6647 low_bound
= TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (tem
)));
6648 up_bound
= TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (tem
)));
6649 el_sz
= TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (tem
)));
6651 || TREE_CODE (low_bound
) != INTEGER_CST
6653 || TREE_CODE (up_bound
) != INTEGER_CST
6655 || TREE_CODE (el_sz
) != INTEGER_CST
)
6658 idx
= mem_ref_offset (t
);
6659 idx
= wi::sdiv_trunc (idx
, wi::to_offset (el_sz
));
6662 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6664 fprintf (dump_file
, "Array bound warning for ");
6665 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, t
);
6666 fprintf (dump_file
, "\n");
6668 warning_at (location
, OPT_Warray_bounds
,
6669 "array subscript is below array bounds");
6670 TREE_NO_WARNING (t
) = 1;
6672 else if (idx
> (wi::to_offset (up_bound
)
6673 - wi::to_offset (low_bound
) + 1))
6675 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6677 fprintf (dump_file
, "Array bound warning for ");
6678 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, t
);
6679 fprintf (dump_file
, "\n");
6681 warning_at (location
, OPT_Warray_bounds
,
6682 "array subscript is above array bounds");
6683 TREE_NO_WARNING (t
) = 1;
6688 /* walk_tree() callback that checks if *TP is
6689 an ARRAY_REF inside an ADDR_EXPR (in which an array
6690 subscript one outside the valid range is allowed). Call
6691 check_array_ref for each ARRAY_REF found. The location is
6695 check_array_bounds (tree
*tp
, int *walk_subtree
, void *data
)
6698 struct walk_stmt_info
*wi
= (struct walk_stmt_info
*) data
;
6699 location_t location
;
6701 if (EXPR_HAS_LOCATION (t
))
6702 location
= EXPR_LOCATION (t
);
6705 location_t
*locp
= (location_t
*) wi
->info
;
6709 *walk_subtree
= TRUE
;
6711 if (TREE_CODE (t
) == ARRAY_REF
)
6712 check_array_ref (location
, t
, false /*ignore_off_by_one*/);
6714 else if (TREE_CODE (t
) == ADDR_EXPR
)
6716 search_for_addr_array (t
, location
);
6717 *walk_subtree
= FALSE
;
6723 /* Walk over all statements of all reachable BBs and call check_array_bounds
6727 check_all_array_refs (void)
6730 gimple_stmt_iterator si
;
6732 FOR_EACH_BB_FN (bb
, cfun
)
6736 bool executable
= false;
6738 /* Skip blocks that were found to be unreachable. */
6739 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
6740 executable
|= !!(e
->flags
& EDGE_EXECUTABLE
);
6744 for (si
= gsi_start_bb (bb
); !gsi_end_p (si
); gsi_next (&si
))
6746 gimple
*stmt
= gsi_stmt (si
);
6747 struct walk_stmt_info wi
;
6748 if (!gimple_has_location (stmt
)
6749 || is_gimple_debug (stmt
))
6752 memset (&wi
, 0, sizeof (wi
));
6754 location_t loc
= gimple_location (stmt
);
6757 walk_gimple_op (gsi_stmt (si
),
6764 /* Return true if all imm uses of VAR are either in STMT, or
6765 feed (optionally through a chain of single imm uses) GIMPLE_COND
6766 in basic block COND_BB. */
6769 all_imm_uses_in_stmt_or_feed_cond (tree var
, gimple
*stmt
, basic_block cond_bb
)
6771 use_operand_p use_p
, use2_p
;
6772 imm_use_iterator iter
;
6774 FOR_EACH_IMM_USE_FAST (use_p
, iter
, var
)
6775 if (USE_STMT (use_p
) != stmt
)
6777 gimple
*use_stmt
= USE_STMT (use_p
), *use_stmt2
;
6778 if (is_gimple_debug (use_stmt
))
6780 while (is_gimple_assign (use_stmt
)
6781 && TREE_CODE (gimple_assign_lhs (use_stmt
)) == SSA_NAME
6782 && single_imm_use (gimple_assign_lhs (use_stmt
),
6783 &use2_p
, &use_stmt2
))
6784 use_stmt
= use_stmt2
;
6785 if (gimple_code (use_stmt
) != GIMPLE_COND
6786 || gimple_bb (use_stmt
) != cond_bb
)
6799 __builtin_unreachable ();
6801 x_5 = ASSERT_EXPR <x_3, ...>;
6802 If x_3 has no other immediate uses (checked by caller),
6803 var is the x_3 var from ASSERT_EXPR, we can clear low 5 bits
6804 from the non-zero bitmask. */
6807 maybe_set_nonzero_bits (basic_block bb
, tree var
)
6809 edge e
= single_pred_edge (bb
);
6810 basic_block cond_bb
= e
->src
;
6811 gimple
*stmt
= last_stmt (cond_bb
);
6815 || gimple_code (stmt
) != GIMPLE_COND
6816 || gimple_cond_code (stmt
) != ((e
->flags
& EDGE_TRUE_VALUE
)
6817 ? EQ_EXPR
: NE_EXPR
)
6818 || TREE_CODE (gimple_cond_lhs (stmt
)) != SSA_NAME
6819 || !integer_zerop (gimple_cond_rhs (stmt
)))
6822 stmt
= SSA_NAME_DEF_STMT (gimple_cond_lhs (stmt
));
6823 if (!is_gimple_assign (stmt
)
6824 || gimple_assign_rhs_code (stmt
) != BIT_AND_EXPR
6825 || TREE_CODE (gimple_assign_rhs2 (stmt
)) != INTEGER_CST
)
6827 if (gimple_assign_rhs1 (stmt
) != var
)
6831 if (TREE_CODE (gimple_assign_rhs1 (stmt
)) != SSA_NAME
)
6833 stmt2
= SSA_NAME_DEF_STMT (gimple_assign_rhs1 (stmt
));
6834 if (!gimple_assign_cast_p (stmt2
)
6835 || gimple_assign_rhs1 (stmt2
) != var
6836 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (stmt2
))
6837 || (TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (stmt
)))
6838 != TYPE_PRECISION (TREE_TYPE (var
))))
6841 cst
= gimple_assign_rhs2 (stmt
);
6842 set_nonzero_bits (var
, wi::bit_and_not (get_nonzero_bits (var
), cst
));
6845 /* Convert range assertion expressions into the implied copies and
6846 copy propagate away the copies. Doing the trivial copy propagation
6847 here avoids the need to run the full copy propagation pass after
6850 FIXME, this will eventually lead to copy propagation removing the
6851 names that had useful range information attached to them. For
6852 instance, if we had the assertion N_i = ASSERT_EXPR <N_j, N_j > 3>,
6853 then N_i will have the range [3, +INF].
6855 However, by converting the assertion into the implied copy
6856 operation N_i = N_j, we will then copy-propagate N_j into the uses
6857 of N_i and lose the range information. We may want to hold on to
6858 ASSERT_EXPRs a little while longer as the ranges could be used in
6859 things like jump threading.
6861 The problem with keeping ASSERT_EXPRs around is that passes after
6862 VRP need to handle them appropriately.
6864 Another approach would be to make the range information a first
6865 class property of the SSA_NAME so that it can be queried from
6866 any pass. This is made somewhat more complex by the need for
6867 multiple ranges to be associated with one SSA_NAME. */
6870 remove_range_assertions (void)
6873 gimple_stmt_iterator si
;
6874 /* 1 if looking at ASSERT_EXPRs immediately at the beginning of
6875 a basic block preceeded by GIMPLE_COND branching to it and
6876 __builtin_trap, -1 if not yet checked, 0 otherwise. */
6879 /* Note that the BSI iterator bump happens at the bottom of the
6880 loop and no bump is necessary if we're removing the statement
6881 referenced by the current BSI. */
6882 FOR_EACH_BB_FN (bb
, cfun
)
6883 for (si
= gsi_after_labels (bb
), is_unreachable
= -1; !gsi_end_p (si
);)
6885 gimple
*stmt
= gsi_stmt (si
);
6887 if (is_gimple_assign (stmt
)
6888 && gimple_assign_rhs_code (stmt
) == ASSERT_EXPR
)
6890 tree lhs
= gimple_assign_lhs (stmt
);
6891 tree rhs
= gimple_assign_rhs1 (stmt
);
6894 var
= ASSERT_EXPR_VAR (rhs
);
6896 if (TREE_CODE (var
) == SSA_NAME
6897 && !POINTER_TYPE_P (TREE_TYPE (lhs
))
6898 && SSA_NAME_RANGE_INFO (lhs
))
6900 if (is_unreachable
== -1)
6903 if (single_pred_p (bb
)
6904 && assert_unreachable_fallthru_edge_p
6905 (single_pred_edge (bb
)))
6909 if (x_7 >= 10 && x_7 < 20)
6910 __builtin_unreachable ();
6911 x_8 = ASSERT_EXPR <x_7, ...>;
6912 if the only uses of x_7 are in the ASSERT_EXPR and
6913 in the condition. In that case, we can copy the
6914 range info from x_8 computed in this pass also
6917 && all_imm_uses_in_stmt_or_feed_cond (var
, stmt
,
6920 set_range_info (var
, SSA_NAME_RANGE_TYPE (lhs
),
6921 SSA_NAME_RANGE_INFO (lhs
)->get_min (),
6922 SSA_NAME_RANGE_INFO (lhs
)->get_max ());
6923 maybe_set_nonzero_bits (bb
, var
);
6927 /* Propagate the RHS into every use of the LHS. */
6928 replace_uses_by (lhs
, var
);
6930 /* And finally, remove the copy, it is not needed. */
6931 gsi_remove (&si
, true);
6932 release_defs (stmt
);
6936 if (!is_gimple_debug (gsi_stmt (si
)))
6944 /* Return true if STMT is interesting for VRP. */
6947 stmt_interesting_for_vrp (gimple
*stmt
)
6949 if (gimple_code (stmt
) == GIMPLE_PHI
)
6951 tree res
= gimple_phi_result (stmt
);
6952 return (!virtual_operand_p (res
)
6953 && (INTEGRAL_TYPE_P (TREE_TYPE (res
))
6954 || POINTER_TYPE_P (TREE_TYPE (res
))));
6956 else if (is_gimple_assign (stmt
) || is_gimple_call (stmt
))
6958 tree lhs
= gimple_get_lhs (stmt
);
6960 /* In general, assignments with virtual operands are not useful
6961 for deriving ranges, with the obvious exception of calls to
6962 builtin functions. */
6963 if (lhs
&& TREE_CODE (lhs
) == SSA_NAME
6964 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs
))
6965 || POINTER_TYPE_P (TREE_TYPE (lhs
)))
6966 && (is_gimple_call (stmt
)
6967 || !gimple_vuse (stmt
)))
6969 else if (is_gimple_call (stmt
) && gimple_call_internal_p (stmt
))
6970 switch (gimple_call_internal_fn (stmt
))
6972 case IFN_ADD_OVERFLOW
:
6973 case IFN_SUB_OVERFLOW
:
6974 case IFN_MUL_OVERFLOW
:
6975 /* These internal calls return _Complex integer type,
6976 but are interesting to VRP nevertheless. */
6977 if (lhs
&& TREE_CODE (lhs
) == SSA_NAME
)
6984 else if (gimple_code (stmt
) == GIMPLE_COND
6985 || gimple_code (stmt
) == GIMPLE_SWITCH
)
6991 /* Initialize VRP lattice. */
6994 vrp_initialize_lattice ()
6996 values_propagated
= false;
6997 num_vr_values
= num_ssa_names
;
6998 vr_value
= XCNEWVEC (value_range
*, num_vr_values
);
6999 vr_phi_edge_counts
= XCNEWVEC (int, num_ssa_names
);
7000 bitmap_obstack_initialize (&vrp_equiv_obstack
);
7003 /* Initialization required by ssa_propagate engine. */
7010 FOR_EACH_BB_FN (bb
, cfun
)
7012 for (gphi_iterator si
= gsi_start_phis (bb
); !gsi_end_p (si
);
7015 gphi
*phi
= si
.phi ();
7016 if (!stmt_interesting_for_vrp (phi
))
7018 tree lhs
= PHI_RESULT (phi
);
7019 set_value_range_to_varying (get_value_range (lhs
));
7020 prop_set_simulate_again (phi
, false);
7023 prop_set_simulate_again (phi
, true);
7026 for (gimple_stmt_iterator si
= gsi_start_bb (bb
); !gsi_end_p (si
);
7029 gimple
*stmt
= gsi_stmt (si
);
7031 /* If the statement is a control insn, then we do not
7032 want to avoid simulating the statement once. Failure
7033 to do so means that those edges will never get added. */
7034 if (stmt_ends_bb_p (stmt
))
7035 prop_set_simulate_again (stmt
, true);
7036 else if (!stmt_interesting_for_vrp (stmt
))
7038 set_defs_to_varying (stmt
);
7039 prop_set_simulate_again (stmt
, false);
7042 prop_set_simulate_again (stmt
, true);
7047 /* Return the singleton value-range for NAME or NAME. */
7050 vrp_valueize (tree name
)
7052 if (TREE_CODE (name
) == SSA_NAME
)
7054 value_range
*vr
= get_value_range (name
);
7055 if (vr
->type
== VR_RANGE
7056 && (TREE_CODE (vr
->min
) == SSA_NAME
7057 || is_gimple_min_invariant (vr
->min
))
7058 && vrp_operand_equal_p (vr
->min
, vr
->max
))
7064 /* Return the singleton value-range for NAME if that is a constant
7065 but signal to not follow SSA edges. */
7068 vrp_valueize_1 (tree name
)
7070 if (TREE_CODE (name
) == SSA_NAME
)
7072 /* If the definition may be simulated again we cannot follow
7073 this SSA edge as the SSA propagator does not necessarily
7074 re-visit the use. */
7075 gimple
*def_stmt
= SSA_NAME_DEF_STMT (name
);
7076 if (!gimple_nop_p (def_stmt
)
7077 && prop_simulate_again_p (def_stmt
))
7079 value_range
*vr
= get_value_range (name
);
7080 if (range_int_cst_singleton_p (vr
))
7086 /* Visit assignment STMT. If it produces an interesting range, record
7087 the range in VR and set LHS to OUTPUT_P. */
7090 vrp_visit_assignment_or_call (gimple
*stmt
, tree
*output_p
, value_range
*vr
)
7093 enum gimple_code code
= gimple_code (stmt
);
7094 lhs
= gimple_get_lhs (stmt
);
7095 *output_p
= NULL_TREE
;
7097 /* We only keep track of ranges in integral and pointer types. */
7098 if (TREE_CODE (lhs
) == SSA_NAME
7099 && ((INTEGRAL_TYPE_P (TREE_TYPE (lhs
))
7100 /* It is valid to have NULL MIN/MAX values on a type. See
7101 build_range_type. */
7102 && TYPE_MIN_VALUE (TREE_TYPE (lhs
))
7103 && TYPE_MAX_VALUE (TREE_TYPE (lhs
)))
7104 || POINTER_TYPE_P (TREE_TYPE (lhs
))))
7106 /* Try folding the statement to a constant first. */
7107 tree tem
= gimple_fold_stmt_to_constant_1 (stmt
, vrp_valueize
,
7109 if (tem
&& is_gimple_min_invariant (tem
))
7110 set_value_range_to_value (vr
, tem
, NULL
);
7111 /* Then dispatch to value-range extracting functions. */
7112 else if (code
== GIMPLE_CALL
)
7113 extract_range_basic (vr
, stmt
);
7115 extract_range_from_assignment (vr
, as_a
<gassign
*> (stmt
));
7120 /* Helper that gets the value range of the SSA_NAME with version I
7121 or a symbolic range containing the SSA_NAME only if the value range
7122 is varying or undefined. */
7124 static inline value_range
7125 get_vr_for_comparison (int i
)
7127 value_range vr
= *get_value_range (ssa_name (i
));
7129 /* If name N_i does not have a valid range, use N_i as its own
7130 range. This allows us to compare against names that may
7131 have N_i in their ranges. */
7132 if (vr
.type
== VR_VARYING
|| vr
.type
== VR_UNDEFINED
)
7135 vr
.min
= ssa_name (i
);
7136 vr
.max
= ssa_name (i
);
7142 /* Compare all the value ranges for names equivalent to VAR with VAL
7143 using comparison code COMP. Return the same value returned by
7144 compare_range_with_value, including the setting of
7145 *STRICT_OVERFLOW_P. */
7148 compare_name_with_value (enum tree_code comp
, tree var
, tree val
,
7149 bool *strict_overflow_p
, bool use_equiv_p
)
7155 int used_strict_overflow
;
7157 value_range equiv_vr
;
7159 /* Get the set of equivalences for VAR. */
7160 e
= get_value_range (var
)->equiv
;
7162 /* Start at -1. Set it to 0 if we do a comparison without relying
7163 on overflow, or 1 if all comparisons rely on overflow. */
7164 used_strict_overflow
= -1;
7166 /* Compare vars' value range with val. */
7167 equiv_vr
= get_vr_for_comparison (SSA_NAME_VERSION (var
));
7169 retval
= compare_range_with_value (comp
, &equiv_vr
, val
, &sop
);
7171 used_strict_overflow
= sop
? 1 : 0;
7173 /* If the equiv set is empty we have done all work we need to do. */
7177 && used_strict_overflow
> 0)
7178 *strict_overflow_p
= true;
7182 EXECUTE_IF_SET_IN_BITMAP (e
, 0, i
, bi
)
7184 tree name
= ssa_name (i
);
7189 && ! SSA_NAME_IS_DEFAULT_DEF (name
)
7190 && prop_simulate_again_p (SSA_NAME_DEF_STMT (name
)))
7193 equiv_vr
= get_vr_for_comparison (i
);
7195 t
= compare_range_with_value (comp
, &equiv_vr
, val
, &sop
);
7198 /* If we get different answers from different members
7199 of the equivalence set this check must be in a dead
7200 code region. Folding it to a trap representation
7201 would be correct here. For now just return don't-know. */
7211 used_strict_overflow
= 0;
7212 else if (used_strict_overflow
< 0)
7213 used_strict_overflow
= 1;
7218 && used_strict_overflow
> 0)
7219 *strict_overflow_p
= true;
7225 /* Given a comparison code COMP and names N1 and N2, compare all the
7226 ranges equivalent to N1 against all the ranges equivalent to N2
7227 to determine the value of N1 COMP N2. Return the same value
7228 returned by compare_ranges. Set *STRICT_OVERFLOW_P to indicate
7229 whether we relied on an overflow infinity in the comparison. */
7233 compare_names (enum tree_code comp
, tree n1
, tree n2
,
7234 bool *strict_overflow_p
)
7238 bitmap_iterator bi1
, bi2
;
7240 int used_strict_overflow
;
7241 static bitmap_obstack
*s_obstack
= NULL
;
7242 static bitmap s_e1
= NULL
, s_e2
= NULL
;
7244 /* Compare the ranges of every name equivalent to N1 against the
7245 ranges of every name equivalent to N2. */
7246 e1
= get_value_range (n1
)->equiv
;
7247 e2
= get_value_range (n2
)->equiv
;
7249 /* Use the fake bitmaps if e1 or e2 are not available. */
7250 if (s_obstack
== NULL
)
7252 s_obstack
= XNEW (bitmap_obstack
);
7253 bitmap_obstack_initialize (s_obstack
);
7254 s_e1
= BITMAP_ALLOC (s_obstack
);
7255 s_e2
= BITMAP_ALLOC (s_obstack
);
7262 /* Add N1 and N2 to their own set of equivalences to avoid
7263 duplicating the body of the loop just to check N1 and N2
7265 bitmap_set_bit (e1
, SSA_NAME_VERSION (n1
));
7266 bitmap_set_bit (e2
, SSA_NAME_VERSION (n2
));
7268 /* If the equivalence sets have a common intersection, then the two
7269 names can be compared without checking their ranges. */
7270 if (bitmap_intersect_p (e1
, e2
))
7272 bitmap_clear_bit (e1
, SSA_NAME_VERSION (n1
));
7273 bitmap_clear_bit (e2
, SSA_NAME_VERSION (n2
));
7275 return (comp
== EQ_EXPR
|| comp
== GE_EXPR
|| comp
== LE_EXPR
)
7277 : boolean_false_node
;
7280 /* Start at -1. Set it to 0 if we do a comparison without relying
7281 on overflow, or 1 if all comparisons rely on overflow. */
7282 used_strict_overflow
= -1;
7284 /* Otherwise, compare all the equivalent ranges. First, add N1 and
7285 N2 to their own set of equivalences to avoid duplicating the body
7286 of the loop just to check N1 and N2 ranges. */
7287 EXECUTE_IF_SET_IN_BITMAP (e1
, 0, i1
, bi1
)
7289 if (! ssa_name (i1
))
7292 value_range vr1
= get_vr_for_comparison (i1
);
7294 t
= retval
= NULL_TREE
;
7295 EXECUTE_IF_SET_IN_BITMAP (e2
, 0, i2
, bi2
)
7297 if (! ssa_name (i2
))
7302 value_range vr2
= get_vr_for_comparison (i2
);
7304 t
= compare_ranges (comp
, &vr1
, &vr2
, &sop
);
7307 /* If we get different answers from different members
7308 of the equivalence set this check must be in a dead
7309 code region. Folding it to a trap representation
7310 would be correct here. For now just return don't-know. */
7314 bitmap_clear_bit (e1
, SSA_NAME_VERSION (n1
));
7315 bitmap_clear_bit (e2
, SSA_NAME_VERSION (n2
));
7321 used_strict_overflow
= 0;
7322 else if (used_strict_overflow
< 0)
7323 used_strict_overflow
= 1;
7329 bitmap_clear_bit (e1
, SSA_NAME_VERSION (n1
));
7330 bitmap_clear_bit (e2
, SSA_NAME_VERSION (n2
));
7331 if (used_strict_overflow
> 0)
7332 *strict_overflow_p
= true;
7337 /* None of the equivalent ranges are useful in computing this
7339 bitmap_clear_bit (e1
, SSA_NAME_VERSION (n1
));
7340 bitmap_clear_bit (e2
, SSA_NAME_VERSION (n2
));
7344 /* Helper function for vrp_evaluate_conditional_warnv & other
7348 vrp_evaluate_conditional_warnv_with_ops_using_ranges (enum tree_code code
,
7350 bool * strict_overflow_p
)
7352 value_range
*vr0
, *vr1
;
7354 vr0
= (TREE_CODE (op0
) == SSA_NAME
) ? get_value_range (op0
) : NULL
;
7355 vr1
= (TREE_CODE (op1
) == SSA_NAME
) ? get_value_range (op1
) : NULL
;
7357 tree res
= NULL_TREE
;
7359 res
= compare_ranges (code
, vr0
, vr1
, strict_overflow_p
);
7361 res
= compare_range_with_value (code
, vr0
, op1
, strict_overflow_p
);
7363 res
= (compare_range_with_value
7364 (swap_tree_comparison (code
), vr1
, op0
, strict_overflow_p
));
7368 /* Helper function for vrp_evaluate_conditional_warnv. */
7371 vrp_evaluate_conditional_warnv_with_ops (enum tree_code code
, tree op0
,
7372 tree op1
, bool use_equiv_p
,
7373 bool *strict_overflow_p
, bool *only_ranges
)
7377 *only_ranges
= true;
7379 /* We only deal with integral and pointer types. */
7380 if (!INTEGRAL_TYPE_P (TREE_TYPE (op0
))
7381 && !POINTER_TYPE_P (TREE_TYPE (op0
)))
7384 if ((ret
= vrp_evaluate_conditional_warnv_with_ops_using_ranges
7385 (code
, op0
, op1
, strict_overflow_p
)))
7388 *only_ranges
= false;
7389 /* Do not use compare_names during propagation, it's quadratic. */
7390 if (TREE_CODE (op0
) == SSA_NAME
&& TREE_CODE (op1
) == SSA_NAME
7392 return compare_names (code
, op0
, op1
, strict_overflow_p
);
7393 else if (TREE_CODE (op0
) == SSA_NAME
)
7394 return compare_name_with_value (code
, op0
, op1
,
7395 strict_overflow_p
, use_equiv_p
);
7396 else if (TREE_CODE (op1
) == SSA_NAME
)
7397 return compare_name_with_value (swap_tree_comparison (code
), op1
, op0
,
7398 strict_overflow_p
, use_equiv_p
);
7402 /* Given (CODE OP0 OP1) within STMT, try to simplify it based on value range
7403 information. Return NULL if the conditional can not be evaluated.
7404 The ranges of all the names equivalent with the operands in COND
7405 will be used when trying to compute the value. If the result is
7406 based on undefined signed overflow, issue a warning if
7410 vrp_evaluate_conditional (tree_code code
, tree op0
, tree op1
, gimple
*stmt
)
7416 /* Some passes and foldings leak constants with overflow flag set
7417 into the IL. Avoid doing wrong things with these and bail out. */
7418 if ((TREE_CODE (op0
) == INTEGER_CST
7419 && TREE_OVERFLOW (op0
))
7420 || (TREE_CODE (op1
) == INTEGER_CST
7421 && TREE_OVERFLOW (op1
)))
7425 ret
= vrp_evaluate_conditional_warnv_with_ops (code
, op0
, op1
, true, &sop
,
7430 enum warn_strict_overflow_code wc
;
7431 const char* warnmsg
;
7433 if (is_gimple_min_invariant (ret
))
7435 wc
= WARN_STRICT_OVERFLOW_CONDITIONAL
;
7436 warnmsg
= G_("assuming signed overflow does not occur when "
7437 "simplifying conditional to constant");
7441 wc
= WARN_STRICT_OVERFLOW_COMPARISON
;
7442 warnmsg
= G_("assuming signed overflow does not occur when "
7443 "simplifying conditional");
7446 if (issue_strict_overflow_warning (wc
))
7448 location_t location
;
7450 if (!gimple_has_location (stmt
))
7451 location
= input_location
;
7453 location
= gimple_location (stmt
);
7454 warning_at (location
, OPT_Wstrict_overflow
, "%s", warnmsg
);
7458 if (warn_type_limits
7459 && ret
&& only_ranges
7460 && TREE_CODE_CLASS (code
) == tcc_comparison
7461 && TREE_CODE (op0
) == SSA_NAME
)
7463 /* If the comparison is being folded and the operand on the LHS
7464 is being compared against a constant value that is outside of
7465 the natural range of OP0's type, then the predicate will
7466 always fold regardless of the value of OP0. If -Wtype-limits
7467 was specified, emit a warning. */
7468 tree type
= TREE_TYPE (op0
);
7469 value_range
*vr0
= get_value_range (op0
);
7471 if (vr0
->type
== VR_RANGE
7472 && INTEGRAL_TYPE_P (type
)
7473 && vrp_val_is_min (vr0
->min
)
7474 && vrp_val_is_max (vr0
->max
)
7475 && is_gimple_min_invariant (op1
))
7477 location_t location
;
7479 if (!gimple_has_location (stmt
))
7480 location
= input_location
;
7482 location
= gimple_location (stmt
);
7484 warning_at (location
, OPT_Wtype_limits
,
7486 ? G_("comparison always false "
7487 "due to limited range of data type")
7488 : G_("comparison always true "
7489 "due to limited range of data type"));
7497 /* Visit conditional statement STMT. If we can determine which edge
7498 will be taken out of STMT's basic block, record it in
7499 *TAKEN_EDGE_P. Otherwise, set *TAKEN_EDGE_P to NULL. */
7502 vrp_visit_cond_stmt (gcond
*stmt
, edge
*taken_edge_p
)
7507 *taken_edge_p
= NULL
;
7509 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7514 fprintf (dump_file
, "\nVisiting conditional with predicate: ");
7515 print_gimple_stmt (dump_file
, stmt
, 0, 0);
7516 fprintf (dump_file
, "\nWith known ranges\n");
7518 FOR_EACH_SSA_TREE_OPERAND (use
, stmt
, i
, SSA_OP_USE
)
7520 fprintf (dump_file
, "\t");
7521 print_generic_expr (dump_file
, use
, 0);
7522 fprintf (dump_file
, ": ");
7523 dump_value_range (dump_file
, vr_value
[SSA_NAME_VERSION (use
)]);
7526 fprintf (dump_file
, "\n");
7529 /* Compute the value of the predicate COND by checking the known
7530 ranges of each of its operands.
7532 Note that we cannot evaluate all the equivalent ranges here
7533 because those ranges may not yet be final and with the current
7534 propagation strategy, we cannot determine when the value ranges
7535 of the names in the equivalence set have changed.
7537 For instance, given the following code fragment
7541 i_14 = ASSERT_EXPR <i_5, i_5 != 0>
7545 Assume that on the first visit to i_14, i_5 has the temporary
7546 range [8, 8] because the second argument to the PHI function is
7547 not yet executable. We derive the range ~[0, 0] for i_14 and the
7548 equivalence set { i_5 }. So, when we visit 'if (i_14 == 1)' for
7549 the first time, since i_14 is equivalent to the range [8, 8], we
7550 determine that the predicate is always false.
7552 On the next round of propagation, i_13 is determined to be
7553 VARYING, which causes i_5 to drop down to VARYING. So, another
7554 visit to i_14 is scheduled. In this second visit, we compute the
7555 exact same range and equivalence set for i_14, namely ~[0, 0] and
7556 { i_5 }. But we did not have the previous range for i_5
7557 registered, so vrp_visit_assignment thinks that the range for
7558 i_14 has not changed. Therefore, the predicate 'if (i_14 == 1)'
7559 is not visited again, which stops propagation from visiting
7560 statements in the THEN clause of that if().
7562 To properly fix this we would need to keep the previous range
7563 value for the names in the equivalence set. This way we would've
7564 discovered that from one visit to the other i_5 changed from
7565 range [8, 8] to VR_VARYING.
7567 However, fixing this apparent limitation may not be worth the
7568 additional checking. Testing on several code bases (GCC, DLV,
7569 MICO, TRAMP3D and SPEC2000) showed that doing this results in
7570 4 more predicates folded in SPEC. */
7573 val
= vrp_evaluate_conditional_warnv_with_ops (gimple_cond_code (stmt
),
7574 gimple_cond_lhs (stmt
),
7575 gimple_cond_rhs (stmt
),
7580 *taken_edge_p
= find_taken_edge (gimple_bb (stmt
), val
);
7583 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7585 "\nIgnoring predicate evaluation because "
7586 "it assumes that signed overflow is undefined");
7591 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7593 fprintf (dump_file
, "\nPredicate evaluates to: ");
7594 if (val
== NULL_TREE
)
7595 fprintf (dump_file
, "DON'T KNOW\n");
7597 print_generic_stmt (dump_file
, val
, 0);
7601 /* Searches the case label vector VEC for the index *IDX of the CASE_LABEL
7602 that includes the value VAL. The search is restricted to the range
7603 [START_IDX, n - 1] where n is the size of VEC.
7605 If there is a CASE_LABEL for VAL, its index is placed in IDX and true is
7608 If there is no CASE_LABEL for VAL and there is one that is larger than VAL,
7609 it is placed in IDX and false is returned.
7611 If VAL is larger than any CASE_LABEL, n is placed on IDX and false is
7615 find_case_label_index (gswitch
*stmt
, size_t start_idx
, tree val
, size_t *idx
)
7617 size_t n
= gimple_switch_num_labels (stmt
);
7620 /* Find case label for minimum of the value range or the next one.
7621 At each iteration we are searching in [low, high - 1]. */
7623 for (low
= start_idx
, high
= n
; high
!= low
; )
7627 /* Note that i != high, so we never ask for n. */
7628 size_t i
= (high
+ low
) / 2;
7629 t
= gimple_switch_label (stmt
, i
);
7631 /* Cache the result of comparing CASE_LOW and val. */
7632 cmp
= tree_int_cst_compare (CASE_LOW (t
), val
);
7636 /* Ranges cannot be empty. */
7645 if (CASE_HIGH (t
) != NULL
7646 && tree_int_cst_compare (CASE_HIGH (t
), val
) >= 0)
7658 /* Searches the case label vector VEC for the range of CASE_LABELs that is used
7659 for values between MIN and MAX. The first index is placed in MIN_IDX. The
7660 last index is placed in MAX_IDX. If the range of CASE_LABELs is empty
7661 then MAX_IDX < MIN_IDX.
7662 Returns true if the default label is not needed. */
7665 find_case_label_range (gswitch
*stmt
, tree min
, tree max
, size_t *min_idx
,
7669 bool min_take_default
= !find_case_label_index (stmt
, 1, min
, &i
);
7670 bool max_take_default
= !find_case_label_index (stmt
, i
, max
, &j
);
7674 && max_take_default
)
7676 /* Only the default case label reached.
7677 Return an empty range. */
7684 bool take_default
= min_take_default
|| max_take_default
;
7688 if (max_take_default
)
7691 /* If the case label range is continuous, we do not need
7692 the default case label. Verify that. */
7693 high
= CASE_LOW (gimple_switch_label (stmt
, i
));
7694 if (CASE_HIGH (gimple_switch_label (stmt
, i
)))
7695 high
= CASE_HIGH (gimple_switch_label (stmt
, i
));
7696 for (k
= i
+ 1; k
<= j
; ++k
)
7698 low
= CASE_LOW (gimple_switch_label (stmt
, k
));
7699 if (!integer_onep (int_const_binop (MINUS_EXPR
, low
, high
)))
7701 take_default
= true;
7705 if (CASE_HIGH (gimple_switch_label (stmt
, k
)))
7706 high
= CASE_HIGH (gimple_switch_label (stmt
, k
));
7711 return !take_default
;
7715 /* Searches the case label vector VEC for the ranges of CASE_LABELs that are
7716 used in range VR. The indices are placed in MIN_IDX1, MAX_IDX, MIN_IDX2 and
7717 MAX_IDX2. If the ranges of CASE_LABELs are empty then MAX_IDX1 < MIN_IDX1.
7718 Returns true if the default label is not needed. */
7721 find_case_label_ranges (gswitch
*stmt
, value_range
*vr
, size_t *min_idx1
,
7722 size_t *max_idx1
, size_t *min_idx2
,
7726 unsigned int n
= gimple_switch_num_labels (stmt
);
7728 tree case_low
, case_high
;
7729 tree min
= vr
->min
, max
= vr
->max
;
7731 gcc_checking_assert (vr
->type
== VR_RANGE
|| vr
->type
== VR_ANTI_RANGE
);
7733 take_default
= !find_case_label_range (stmt
, min
, max
, &i
, &j
);
7735 /* Set second range to emtpy. */
7739 if (vr
->type
== VR_RANGE
)
7743 return !take_default
;
7746 /* Set first range to all case labels. */
7753 /* Make sure all the values of case labels [i , j] are contained in
7754 range [MIN, MAX]. */
7755 case_low
= CASE_LOW (gimple_switch_label (stmt
, i
));
7756 case_high
= CASE_HIGH (gimple_switch_label (stmt
, j
));
7757 if (tree_int_cst_compare (case_low
, min
) < 0)
7759 if (case_high
!= NULL_TREE
7760 && tree_int_cst_compare (max
, case_high
) < 0)
7766 /* If the range spans case labels [i, j], the corresponding anti-range spans
7767 the labels [1, i - 1] and [j + 1, n - 1]. */
7793 /* Visit switch statement STMT. If we can determine which edge
7794 will be taken out of STMT's basic block, record it in
7795 *TAKEN_EDGE_P. Otherwise, *TAKEN_EDGE_P set to NULL. */
7798 vrp_visit_switch_stmt (gswitch
*stmt
, edge
*taken_edge_p
)
7802 size_t i
= 0, j
= 0, k
, l
;
7805 *taken_edge_p
= NULL
;
7806 op
= gimple_switch_index (stmt
);
7807 if (TREE_CODE (op
) != SSA_NAME
)
7810 vr
= get_value_range (op
);
7811 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7813 fprintf (dump_file
, "\nVisiting switch expression with operand ");
7814 print_generic_expr (dump_file
, op
, 0);
7815 fprintf (dump_file
, " with known range ");
7816 dump_value_range (dump_file
, vr
);
7817 fprintf (dump_file
, "\n");
7820 if ((vr
->type
!= VR_RANGE
7821 && vr
->type
!= VR_ANTI_RANGE
)
7822 || symbolic_range_p (vr
))
7825 /* Find the single edge that is taken from the switch expression. */
7826 take_default
= !find_case_label_ranges (stmt
, vr
, &i
, &j
, &k
, &l
);
7828 /* Check if the range spans no CASE_LABEL. If so, we only reach the default
7832 gcc_assert (take_default
);
7833 val
= gimple_switch_default_label (stmt
);
7837 /* Check if labels with index i to j and maybe the default label
7838 are all reaching the same label. */
7840 val
= gimple_switch_label (stmt
, i
);
7842 && CASE_LABEL (gimple_switch_default_label (stmt
))
7843 != CASE_LABEL (val
))
7845 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7846 fprintf (dump_file
, " not a single destination for this "
7850 for (++i
; i
<= j
; ++i
)
7852 if (CASE_LABEL (gimple_switch_label (stmt
, i
)) != CASE_LABEL (val
))
7854 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7855 fprintf (dump_file
, " not a single destination for this "
7862 if (CASE_LABEL (gimple_switch_label (stmt
, k
)) != CASE_LABEL (val
))
7864 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7865 fprintf (dump_file
, " not a single destination for this "
7872 *taken_edge_p
= find_edge (gimple_bb (stmt
),
7873 label_to_block (CASE_LABEL (val
)));
7875 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7877 fprintf (dump_file
, " will take edge to ");
7878 print_generic_stmt (dump_file
, CASE_LABEL (val
), 0);
7883 /* Evaluate statement STMT. If the statement produces a useful range,
7884 set VR and corepsponding OUTPUT_P.
7886 If STMT is a conditional branch and we can determine its truth
7887 value, the taken edge is recorded in *TAKEN_EDGE_P. */
7890 extract_range_from_stmt (gimple
*stmt
, edge
*taken_edge_p
,
7891 tree
*output_p
, value_range
*vr
)
7894 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7896 fprintf (dump_file
, "\nVisiting statement:\n");
7897 print_gimple_stmt (dump_file
, stmt
, 0, dump_flags
);
7900 if (!stmt_interesting_for_vrp (stmt
))
7901 gcc_assert (stmt_ends_bb_p (stmt
));
7902 else if (is_gimple_assign (stmt
) || is_gimple_call (stmt
))
7903 vrp_visit_assignment_or_call (stmt
, output_p
, vr
);
7904 else if (gimple_code (stmt
) == GIMPLE_COND
)
7905 vrp_visit_cond_stmt (as_a
<gcond
*> (stmt
), taken_edge_p
);
7906 else if (gimple_code (stmt
) == GIMPLE_SWITCH
)
7907 vrp_visit_switch_stmt (as_a
<gswitch
*> (stmt
), taken_edge_p
);
7910 /* Evaluate statement STMT. If the statement produces a useful range,
7911 return SSA_PROP_INTERESTING and record the SSA name with the
7912 interesting range into *OUTPUT_P.
7914 If STMT is a conditional branch and we can determine its truth
7915 value, the taken edge is recorded in *TAKEN_EDGE_P.
7917 If STMT produces a varying value, return SSA_PROP_VARYING. */
7919 static enum ssa_prop_result
7920 vrp_visit_stmt (gimple
*stmt
, edge
*taken_edge_p
, tree
*output_p
)
7922 value_range vr
= VR_INITIALIZER
;
7923 tree lhs
= gimple_get_lhs (stmt
);
7924 extract_range_from_stmt (stmt
, taken_edge_p
, output_p
, &vr
);
7928 if (update_value_range (*output_p
, &vr
))
7930 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7932 fprintf (dump_file
, "Found new range for ");
7933 print_generic_expr (dump_file
, *output_p
, 0);
7934 fprintf (dump_file
, ": ");
7935 dump_value_range (dump_file
, &vr
);
7936 fprintf (dump_file
, "\n");
7939 if (vr
.type
== VR_VARYING
)
7940 return SSA_PROP_VARYING
;
7942 return SSA_PROP_INTERESTING
;
7944 return SSA_PROP_NOT_INTERESTING
;
7947 if (is_gimple_call (stmt
) && gimple_call_internal_p (stmt
))
7948 switch (gimple_call_internal_fn (stmt
))
7950 case IFN_ADD_OVERFLOW
:
7951 case IFN_SUB_OVERFLOW
:
7952 case IFN_MUL_OVERFLOW
:
7953 /* These internal calls return _Complex integer type,
7954 which VRP does not track, but the immediate uses
7955 thereof might be interesting. */
7956 if (lhs
&& TREE_CODE (lhs
) == SSA_NAME
)
7958 imm_use_iterator iter
;
7959 use_operand_p use_p
;
7960 enum ssa_prop_result res
= SSA_PROP_VARYING
;
7962 set_value_range_to_varying (get_value_range (lhs
));
7964 FOR_EACH_IMM_USE_FAST (use_p
, iter
, lhs
)
7966 gimple
*use_stmt
= USE_STMT (use_p
);
7967 if (!is_gimple_assign (use_stmt
))
7969 enum tree_code rhs_code
= gimple_assign_rhs_code (use_stmt
);
7970 if (rhs_code
!= REALPART_EXPR
&& rhs_code
!= IMAGPART_EXPR
)
7972 tree rhs1
= gimple_assign_rhs1 (use_stmt
);
7973 tree use_lhs
= gimple_assign_lhs (use_stmt
);
7974 if (TREE_CODE (rhs1
) != rhs_code
7975 || TREE_OPERAND (rhs1
, 0) != lhs
7976 || TREE_CODE (use_lhs
) != SSA_NAME
7977 || !stmt_interesting_for_vrp (use_stmt
)
7978 || (!INTEGRAL_TYPE_P (TREE_TYPE (use_lhs
))
7979 || !TYPE_MIN_VALUE (TREE_TYPE (use_lhs
))
7980 || !TYPE_MAX_VALUE (TREE_TYPE (use_lhs
))))
7983 /* If there is a change in the value range for any of the
7984 REALPART_EXPR/IMAGPART_EXPR immediate uses, return
7985 SSA_PROP_INTERESTING. If there are any REALPART_EXPR
7986 or IMAGPART_EXPR immediate uses, but none of them have
7987 a change in their value ranges, return
7988 SSA_PROP_NOT_INTERESTING. If there are no
7989 {REAL,IMAG}PART_EXPR uses at all,
7990 return SSA_PROP_VARYING. */
7991 value_range new_vr
= VR_INITIALIZER
;
7992 extract_range_basic (&new_vr
, use_stmt
);
7993 value_range
*old_vr
= get_value_range (use_lhs
);
7994 if (old_vr
->type
!= new_vr
.type
7995 || !vrp_operand_equal_p (old_vr
->min
, new_vr
.min
)
7996 || !vrp_operand_equal_p (old_vr
->max
, new_vr
.max
)
7997 || !vrp_bitmap_equal_p (old_vr
->equiv
, new_vr
.equiv
))
7998 res
= SSA_PROP_INTERESTING
;
8000 res
= SSA_PROP_NOT_INTERESTING
;
8001 BITMAP_FREE (new_vr
.equiv
);
8002 if (res
== SSA_PROP_INTERESTING
)
8016 /* All other statements produce nothing of interest for VRP, so mark
8017 their outputs varying and prevent further simulation. */
8018 set_defs_to_varying (stmt
);
8020 return (*taken_edge_p
) ? SSA_PROP_INTERESTING
: SSA_PROP_VARYING
;
8023 /* Union the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
8024 { VR1TYPE, VR0MIN, VR0MAX } and store the result
8025 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
8026 possible such range. The resulting range is not canonicalized. */
8029 union_ranges (enum value_range_type
*vr0type
,
8030 tree
*vr0min
, tree
*vr0max
,
8031 enum value_range_type vr1type
,
8032 tree vr1min
, tree vr1max
)
8034 bool mineq
= vrp_operand_equal_p (*vr0min
, vr1min
);
8035 bool maxeq
= vrp_operand_equal_p (*vr0max
, vr1max
);
8037 /* [] is vr0, () is vr1 in the following classification comments. */
8041 if (*vr0type
== vr1type
)
8042 /* Nothing to do for equal ranges. */
8044 else if ((*vr0type
== VR_RANGE
8045 && vr1type
== VR_ANTI_RANGE
)
8046 || (*vr0type
== VR_ANTI_RANGE
8047 && vr1type
== VR_RANGE
))
8049 /* For anti-range with range union the result is varying. */
8055 else if (operand_less_p (*vr0max
, vr1min
) == 1
8056 || operand_less_p (vr1max
, *vr0min
) == 1)
8058 /* [ ] ( ) or ( ) [ ]
8059 If the ranges have an empty intersection, result of the union
8060 operation is the anti-range or if both are anti-ranges
8062 if (*vr0type
== VR_ANTI_RANGE
8063 && vr1type
== VR_ANTI_RANGE
)
8065 else if (*vr0type
== VR_ANTI_RANGE
8066 && vr1type
== VR_RANGE
)
8068 else if (*vr0type
== VR_RANGE
8069 && vr1type
== VR_ANTI_RANGE
)
8075 else if (*vr0type
== VR_RANGE
8076 && vr1type
== VR_RANGE
)
8078 /* The result is the convex hull of both ranges. */
8079 if (operand_less_p (*vr0max
, vr1min
) == 1)
8081 /* If the result can be an anti-range, create one. */
8082 if (TREE_CODE (*vr0max
) == INTEGER_CST
8083 && TREE_CODE (vr1min
) == INTEGER_CST
8084 && vrp_val_is_min (*vr0min
)
8085 && vrp_val_is_max (vr1max
))
8087 tree min
= int_const_binop (PLUS_EXPR
,
8089 build_int_cst (TREE_TYPE (*vr0max
), 1));
8090 tree max
= int_const_binop (MINUS_EXPR
,
8092 build_int_cst (TREE_TYPE (vr1min
), 1));
8093 if (!operand_less_p (max
, min
))
8095 *vr0type
= VR_ANTI_RANGE
;
8107 /* If the result can be an anti-range, create one. */
8108 if (TREE_CODE (vr1max
) == INTEGER_CST
8109 && TREE_CODE (*vr0min
) == INTEGER_CST
8110 && vrp_val_is_min (vr1min
)
8111 && vrp_val_is_max (*vr0max
))
8113 tree min
= int_const_binop (PLUS_EXPR
,
8115 build_int_cst (TREE_TYPE (vr1max
), 1));
8116 tree max
= int_const_binop (MINUS_EXPR
,
8118 build_int_cst (TREE_TYPE (*vr0min
), 1));
8119 if (!operand_less_p (max
, min
))
8121 *vr0type
= VR_ANTI_RANGE
;
8135 else if ((maxeq
|| operand_less_p (vr1max
, *vr0max
) == 1)
8136 && (mineq
|| operand_less_p (*vr0min
, vr1min
) == 1))
8138 /* [ ( ) ] or [( ) ] or [ ( )] */
8139 if (*vr0type
== VR_RANGE
8140 && vr1type
== VR_RANGE
)
8142 else if (*vr0type
== VR_ANTI_RANGE
8143 && vr1type
== VR_ANTI_RANGE
)
8149 else if (*vr0type
== VR_ANTI_RANGE
8150 && vr1type
== VR_RANGE
)
8152 /* Arbitrarily choose the right or left gap. */
8153 if (!mineq
&& TREE_CODE (vr1min
) == INTEGER_CST
)
8154 *vr0max
= int_const_binop (MINUS_EXPR
, vr1min
,
8155 build_int_cst (TREE_TYPE (vr1min
), 1));
8156 else if (!maxeq
&& TREE_CODE (vr1max
) == INTEGER_CST
)
8157 *vr0min
= int_const_binop (PLUS_EXPR
, vr1max
,
8158 build_int_cst (TREE_TYPE (vr1max
), 1));
8162 else if (*vr0type
== VR_RANGE
8163 && vr1type
== VR_ANTI_RANGE
)
8164 /* The result covers everything. */
8169 else if ((maxeq
|| operand_less_p (*vr0max
, vr1max
) == 1)
8170 && (mineq
|| operand_less_p (vr1min
, *vr0min
) == 1))
8172 /* ( [ ] ) or ([ ] ) or ( [ ]) */
8173 if (*vr0type
== VR_RANGE
8174 && vr1type
== VR_RANGE
)
8180 else if (*vr0type
== VR_ANTI_RANGE
8181 && vr1type
== VR_ANTI_RANGE
)
8183 else if (*vr0type
== VR_RANGE
8184 && vr1type
== VR_ANTI_RANGE
)
8186 *vr0type
= VR_ANTI_RANGE
;
8187 if (!mineq
&& TREE_CODE (*vr0min
) == INTEGER_CST
)
8189 *vr0max
= int_const_binop (MINUS_EXPR
, *vr0min
,
8190 build_int_cst (TREE_TYPE (*vr0min
), 1));
8193 else if (!maxeq
&& TREE_CODE (*vr0max
) == INTEGER_CST
)
8195 *vr0min
= int_const_binop (PLUS_EXPR
, *vr0max
,
8196 build_int_cst (TREE_TYPE (*vr0max
), 1));
8202 else if (*vr0type
== VR_ANTI_RANGE
8203 && vr1type
== VR_RANGE
)
8204 /* The result covers everything. */
8209 else if ((operand_less_p (vr1min
, *vr0max
) == 1
8210 || operand_equal_p (vr1min
, *vr0max
, 0))
8211 && operand_less_p (*vr0min
, vr1min
) == 1
8212 && operand_less_p (*vr0max
, vr1max
) == 1)
8214 /* [ ( ] ) or [ ]( ) */
8215 if (*vr0type
== VR_RANGE
8216 && vr1type
== VR_RANGE
)
8218 else if (*vr0type
== VR_ANTI_RANGE
8219 && vr1type
== VR_ANTI_RANGE
)
8221 else if (*vr0type
== VR_ANTI_RANGE
8222 && vr1type
== VR_RANGE
)
8224 if (TREE_CODE (vr1min
) == INTEGER_CST
)
8225 *vr0max
= int_const_binop (MINUS_EXPR
, vr1min
,
8226 build_int_cst (TREE_TYPE (vr1min
), 1));
8230 else if (*vr0type
== VR_RANGE
8231 && vr1type
== VR_ANTI_RANGE
)
8233 if (TREE_CODE (*vr0max
) == INTEGER_CST
)
8236 *vr0min
= int_const_binop (PLUS_EXPR
, *vr0max
,
8237 build_int_cst (TREE_TYPE (*vr0max
), 1));
8246 else if ((operand_less_p (*vr0min
, vr1max
) == 1
8247 || operand_equal_p (*vr0min
, vr1max
, 0))
8248 && operand_less_p (vr1min
, *vr0min
) == 1
8249 && operand_less_p (vr1max
, *vr0max
) == 1)
8251 /* ( [ ) ] or ( )[ ] */
8252 if (*vr0type
== VR_RANGE
8253 && vr1type
== VR_RANGE
)
8255 else if (*vr0type
== VR_ANTI_RANGE
8256 && vr1type
== VR_ANTI_RANGE
)
8258 else if (*vr0type
== VR_ANTI_RANGE
8259 && vr1type
== VR_RANGE
)
8261 if (TREE_CODE (vr1max
) == INTEGER_CST
)
8262 *vr0min
= int_const_binop (PLUS_EXPR
, vr1max
,
8263 build_int_cst (TREE_TYPE (vr1max
), 1));
8267 else if (*vr0type
== VR_RANGE
8268 && vr1type
== VR_ANTI_RANGE
)
8270 if (TREE_CODE (*vr0min
) == INTEGER_CST
)
8274 *vr0max
= int_const_binop (MINUS_EXPR
, *vr0min
,
8275 build_int_cst (TREE_TYPE (*vr0min
), 1));
8289 *vr0type
= VR_VARYING
;
8290 *vr0min
= NULL_TREE
;
8291 *vr0max
= NULL_TREE
;
8294 /* Intersect the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
8295 { VR1TYPE, VR0MIN, VR0MAX } and store the result
8296 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
8297 possible such range. The resulting range is not canonicalized. */
8300 intersect_ranges (enum value_range_type
*vr0type
,
8301 tree
*vr0min
, tree
*vr0max
,
8302 enum value_range_type vr1type
,
8303 tree vr1min
, tree vr1max
)
8305 bool mineq
= vrp_operand_equal_p (*vr0min
, vr1min
);
8306 bool maxeq
= vrp_operand_equal_p (*vr0max
, vr1max
);
8308 /* [] is vr0, () is vr1 in the following classification comments. */
8312 if (*vr0type
== vr1type
)
8313 /* Nothing to do for equal ranges. */
8315 else if ((*vr0type
== VR_RANGE
8316 && vr1type
== VR_ANTI_RANGE
)
8317 || (*vr0type
== VR_ANTI_RANGE
8318 && vr1type
== VR_RANGE
))
8320 /* For anti-range with range intersection the result is empty. */
8321 *vr0type
= VR_UNDEFINED
;
8322 *vr0min
= NULL_TREE
;
8323 *vr0max
= NULL_TREE
;
8328 else if (operand_less_p (*vr0max
, vr1min
) == 1
8329 || operand_less_p (vr1max
, *vr0min
) == 1)
8331 /* [ ] ( ) or ( ) [ ]
8332 If the ranges have an empty intersection, the result of the
8333 intersect operation is the range for intersecting an
8334 anti-range with a range or empty when intersecting two ranges. */
8335 if (*vr0type
== VR_RANGE
8336 && vr1type
== VR_ANTI_RANGE
)
8338 else if (*vr0type
== VR_ANTI_RANGE
8339 && vr1type
== VR_RANGE
)
8345 else if (*vr0type
== VR_RANGE
8346 && vr1type
== VR_RANGE
)
8348 *vr0type
= VR_UNDEFINED
;
8349 *vr0min
= NULL_TREE
;
8350 *vr0max
= NULL_TREE
;
8352 else if (*vr0type
== VR_ANTI_RANGE
8353 && vr1type
== VR_ANTI_RANGE
)
8355 /* If the anti-ranges are adjacent to each other merge them. */
8356 if (TREE_CODE (*vr0max
) == INTEGER_CST
8357 && TREE_CODE (vr1min
) == INTEGER_CST
8358 && operand_less_p (*vr0max
, vr1min
) == 1
8359 && integer_onep (int_const_binop (MINUS_EXPR
,
8362 else if (TREE_CODE (vr1max
) == INTEGER_CST
8363 && TREE_CODE (*vr0min
) == INTEGER_CST
8364 && operand_less_p (vr1max
, *vr0min
) == 1
8365 && integer_onep (int_const_binop (MINUS_EXPR
,
8368 /* Else arbitrarily take VR0. */
8371 else if ((maxeq
|| operand_less_p (vr1max
, *vr0max
) == 1)
8372 && (mineq
|| operand_less_p (*vr0min
, vr1min
) == 1))
8374 /* [ ( ) ] or [( ) ] or [ ( )] */
8375 if (*vr0type
== VR_RANGE
8376 && vr1type
== VR_RANGE
)
8378 /* If both are ranges the result is the inner one. */
8383 else if (*vr0type
== VR_RANGE
8384 && vr1type
== VR_ANTI_RANGE
)
8386 /* Choose the right gap if the left one is empty. */
8389 if (TREE_CODE (vr1max
) == INTEGER_CST
)
8390 *vr0min
= int_const_binop (PLUS_EXPR
, vr1max
,
8391 build_int_cst (TREE_TYPE (vr1max
), 1));
8395 /* Choose the left gap if the right one is empty. */
8398 if (TREE_CODE (vr1min
) == INTEGER_CST
)
8399 *vr0max
= int_const_binop (MINUS_EXPR
, vr1min
,
8400 build_int_cst (TREE_TYPE (vr1min
), 1));
8404 /* Choose the anti-range if the range is effectively varying. */
8405 else if (vrp_val_is_min (*vr0min
)
8406 && vrp_val_is_max (*vr0max
))
8412 /* Else choose the range. */
8414 else if (*vr0type
== VR_ANTI_RANGE
8415 && vr1type
== VR_ANTI_RANGE
)
8416 /* If both are anti-ranges the result is the outer one. */
8418 else if (*vr0type
== VR_ANTI_RANGE
8419 && vr1type
== VR_RANGE
)
8421 /* The intersection is empty. */
8422 *vr0type
= VR_UNDEFINED
;
8423 *vr0min
= NULL_TREE
;
8424 *vr0max
= NULL_TREE
;
8429 else if ((maxeq
|| operand_less_p (*vr0max
, vr1max
) == 1)
8430 && (mineq
|| operand_less_p (vr1min
, *vr0min
) == 1))
8432 /* ( [ ] ) or ([ ] ) or ( [ ]) */
8433 if (*vr0type
== VR_RANGE
8434 && vr1type
== VR_RANGE
)
8435 /* Choose the inner range. */
8437 else if (*vr0type
== VR_ANTI_RANGE
8438 && vr1type
== VR_RANGE
)
8440 /* Choose the right gap if the left is empty. */
8443 *vr0type
= VR_RANGE
;
8444 if (TREE_CODE (*vr0max
) == INTEGER_CST
)
8445 *vr0min
= int_const_binop (PLUS_EXPR
, *vr0max
,
8446 build_int_cst (TREE_TYPE (*vr0max
), 1));
8451 /* Choose the left gap if the right is empty. */
8454 *vr0type
= VR_RANGE
;
8455 if (TREE_CODE (*vr0min
) == INTEGER_CST
)
8456 *vr0max
= int_const_binop (MINUS_EXPR
, *vr0min
,
8457 build_int_cst (TREE_TYPE (*vr0min
), 1));
8462 /* Choose the anti-range if the range is effectively varying. */
8463 else if (vrp_val_is_min (vr1min
)
8464 && vrp_val_is_max (vr1max
))
8466 /* Else choose the range. */
8474 else if (*vr0type
== VR_ANTI_RANGE
8475 && vr1type
== VR_ANTI_RANGE
)
8477 /* If both are anti-ranges the result is the outer one. */
8482 else if (vr1type
== VR_ANTI_RANGE
8483 && *vr0type
== VR_RANGE
)
8485 /* The intersection is empty. */
8486 *vr0type
= VR_UNDEFINED
;
8487 *vr0min
= NULL_TREE
;
8488 *vr0max
= NULL_TREE
;
8493 else if ((operand_less_p (vr1min
, *vr0max
) == 1
8494 || operand_equal_p (vr1min
, *vr0max
, 0))
8495 && operand_less_p (*vr0min
, vr1min
) == 1)
8497 /* [ ( ] ) or [ ]( ) */
8498 if (*vr0type
== VR_ANTI_RANGE
8499 && vr1type
== VR_ANTI_RANGE
)
8501 else if (*vr0type
== VR_RANGE
8502 && vr1type
== VR_RANGE
)
8504 else if (*vr0type
== VR_RANGE
8505 && vr1type
== VR_ANTI_RANGE
)
8507 if (TREE_CODE (vr1min
) == INTEGER_CST
)
8508 *vr0max
= int_const_binop (MINUS_EXPR
, vr1min
,
8509 build_int_cst (TREE_TYPE (vr1min
), 1));
8513 else if (*vr0type
== VR_ANTI_RANGE
8514 && vr1type
== VR_RANGE
)
8516 *vr0type
= VR_RANGE
;
8517 if (TREE_CODE (*vr0max
) == INTEGER_CST
)
8518 *vr0min
= int_const_binop (PLUS_EXPR
, *vr0max
,
8519 build_int_cst (TREE_TYPE (*vr0max
), 1));
8527 else if ((operand_less_p (*vr0min
, vr1max
) == 1
8528 || operand_equal_p (*vr0min
, vr1max
, 0))
8529 && operand_less_p (vr1min
, *vr0min
) == 1)
8531 /* ( [ ) ] or ( )[ ] */
8532 if (*vr0type
== VR_ANTI_RANGE
8533 && vr1type
== VR_ANTI_RANGE
)
8535 else if (*vr0type
== VR_RANGE
8536 && vr1type
== VR_RANGE
)
8538 else if (*vr0type
== VR_RANGE
8539 && vr1type
== VR_ANTI_RANGE
)
8541 if (TREE_CODE (vr1max
) == INTEGER_CST
)
8542 *vr0min
= int_const_binop (PLUS_EXPR
, vr1max
,
8543 build_int_cst (TREE_TYPE (vr1max
), 1));
8547 else if (*vr0type
== VR_ANTI_RANGE
8548 && vr1type
== VR_RANGE
)
8550 *vr0type
= VR_RANGE
;
8551 if (TREE_CODE (*vr0min
) == INTEGER_CST
)
8552 *vr0max
= int_const_binop (MINUS_EXPR
, *vr0min
,
8553 build_int_cst (TREE_TYPE (*vr0min
), 1));
8562 /* As a fallback simply use { *VRTYPE, *VR0MIN, *VR0MAX } as
8563 result for the intersection. That's always a conservative
8564 correct estimate unless VR1 is a constant singleton range
8565 in which case we choose that. */
8566 if (vr1type
== VR_RANGE
8567 && is_gimple_min_invariant (vr1min
)
8568 && vrp_operand_equal_p (vr1min
, vr1max
))
8579 /* Intersect the two value-ranges *VR0 and *VR1 and store the result
8580 in *VR0. This may not be the smallest possible such range. */
8583 vrp_intersect_ranges_1 (value_range
*vr0
, value_range
*vr1
)
8587 /* If either range is VR_VARYING the other one wins. */
8588 if (vr1
->type
== VR_VARYING
)
8590 if (vr0
->type
== VR_VARYING
)
8592 copy_value_range (vr0
, vr1
);
8596 /* When either range is VR_UNDEFINED the resulting range is
8597 VR_UNDEFINED, too. */
8598 if (vr0
->type
== VR_UNDEFINED
)
8600 if (vr1
->type
== VR_UNDEFINED
)
8602 set_value_range_to_undefined (vr0
);
8606 /* Save the original vr0 so we can return it as conservative intersection
8607 result when our worker turns things to varying. */
8609 intersect_ranges (&vr0
->type
, &vr0
->min
, &vr0
->max
,
8610 vr1
->type
, vr1
->min
, vr1
->max
);
8611 /* Make sure to canonicalize the result though as the inversion of a
8612 VR_RANGE can still be a VR_RANGE. */
8613 set_and_canonicalize_value_range (vr0
, vr0
->type
,
8614 vr0
->min
, vr0
->max
, vr0
->equiv
);
8615 /* If that failed, use the saved original VR0. */
8616 if (vr0
->type
== VR_VARYING
)
8621 /* If the result is VR_UNDEFINED there is no need to mess with
8622 the equivalencies. */
8623 if (vr0
->type
== VR_UNDEFINED
)
8626 /* The resulting set of equivalences for range intersection is the union of
8628 if (vr0
->equiv
&& vr1
->equiv
&& vr0
->equiv
!= vr1
->equiv
)
8629 bitmap_ior_into (vr0
->equiv
, vr1
->equiv
);
8630 else if (vr1
->equiv
&& !vr0
->equiv
)
8632 vr0
->equiv
= BITMAP_ALLOC (&vrp_equiv_obstack
);
8633 bitmap_copy (vr0
->equiv
, vr1
->equiv
);
8638 vrp_intersect_ranges (value_range
*vr0
, value_range
*vr1
)
8640 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8642 fprintf (dump_file
, "Intersecting\n ");
8643 dump_value_range (dump_file
, vr0
);
8644 fprintf (dump_file
, "\nand\n ");
8645 dump_value_range (dump_file
, vr1
);
8646 fprintf (dump_file
, "\n");
8648 vrp_intersect_ranges_1 (vr0
, vr1
);
8649 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8651 fprintf (dump_file
, "to\n ");
8652 dump_value_range (dump_file
, vr0
);
8653 fprintf (dump_file
, "\n");
8657 /* Meet operation for value ranges. Given two value ranges VR0 and
8658 VR1, store in VR0 a range that contains both VR0 and VR1. This
8659 may not be the smallest possible such range. */
8662 vrp_meet_1 (value_range
*vr0
, const value_range
*vr1
)
8666 if (vr0
->type
== VR_UNDEFINED
)
8668 set_value_range (vr0
, vr1
->type
, vr1
->min
, vr1
->max
, vr1
->equiv
);
8672 if (vr1
->type
== VR_UNDEFINED
)
8674 /* VR0 already has the resulting range. */
8678 if (vr0
->type
== VR_VARYING
)
8680 /* Nothing to do. VR0 already has the resulting range. */
8684 if (vr1
->type
== VR_VARYING
)
8686 set_value_range_to_varying (vr0
);
8691 union_ranges (&vr0
->type
, &vr0
->min
, &vr0
->max
,
8692 vr1
->type
, vr1
->min
, vr1
->max
);
8693 if (vr0
->type
== VR_VARYING
)
8695 /* Failed to find an efficient meet. Before giving up and setting
8696 the result to VARYING, see if we can at least derive a useful
8697 anti-range. FIXME, all this nonsense about distinguishing
8698 anti-ranges from ranges is necessary because of the odd
8699 semantics of range_includes_zero_p and friends. */
8700 if (((saved
.type
== VR_RANGE
8701 && range_includes_zero_p (saved
.min
, saved
.max
) == 0)
8702 || (saved
.type
== VR_ANTI_RANGE
8703 && range_includes_zero_p (saved
.min
, saved
.max
) == 1))
8704 && ((vr1
->type
== VR_RANGE
8705 && range_includes_zero_p (vr1
->min
, vr1
->max
) == 0)
8706 || (vr1
->type
== VR_ANTI_RANGE
8707 && range_includes_zero_p (vr1
->min
, vr1
->max
) == 1)))
8709 set_value_range_to_nonnull (vr0
, TREE_TYPE (saved
.min
));
8711 /* Since this meet operation did not result from the meeting of
8712 two equivalent names, VR0 cannot have any equivalences. */
8714 bitmap_clear (vr0
->equiv
);
8718 set_value_range_to_varying (vr0
);
8721 set_and_canonicalize_value_range (vr0
, vr0
->type
, vr0
->min
, vr0
->max
,
8723 if (vr0
->type
== VR_VARYING
)
8726 /* The resulting set of equivalences is always the intersection of
8728 if (vr0
->equiv
&& vr1
->equiv
&& vr0
->equiv
!= vr1
->equiv
)
8729 bitmap_and_into (vr0
->equiv
, vr1
->equiv
);
8730 else if (vr0
->equiv
&& !vr1
->equiv
)
8731 bitmap_clear (vr0
->equiv
);
8735 vrp_meet (value_range
*vr0
, const value_range
*vr1
)
8737 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8739 fprintf (dump_file
, "Meeting\n ");
8740 dump_value_range (dump_file
, vr0
);
8741 fprintf (dump_file
, "\nand\n ");
8742 dump_value_range (dump_file
, vr1
);
8743 fprintf (dump_file
, "\n");
8745 vrp_meet_1 (vr0
, vr1
);
8746 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8748 fprintf (dump_file
, "to\n ");
8749 dump_value_range (dump_file
, vr0
);
8750 fprintf (dump_file
, "\n");
8755 /* Visit all arguments for PHI node PHI that flow through executable
8756 edges. If a valid value range can be derived from all the incoming
8757 value ranges, set a new range in VR_RESULT. */
8760 extract_range_from_phi_node (gphi
*phi
, value_range
*vr_result
)
8763 tree lhs
= PHI_RESULT (phi
);
8764 value_range
*lhs_vr
= get_value_range (lhs
);
8766 int edges
, old_edges
;
8769 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8771 fprintf (dump_file
, "\nVisiting PHI node: ");
8772 print_gimple_stmt (dump_file
, phi
, 0, dump_flags
);
8775 bool may_simulate_backedge_again
= false;
8777 for (i
= 0; i
< gimple_phi_num_args (phi
); i
++)
8779 edge e
= gimple_phi_arg_edge (phi
, i
);
8781 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8784 " Argument #%d (%d -> %d %sexecutable)\n",
8785 (int) i
, e
->src
->index
, e
->dest
->index
,
8786 (e
->flags
& EDGE_EXECUTABLE
) ? "" : "not ");
8789 if (e
->flags
& EDGE_EXECUTABLE
)
8791 tree arg
= PHI_ARG_DEF (phi
, i
);
8796 if (TREE_CODE (arg
) == SSA_NAME
)
8798 /* See if we are eventually going to change one of the args. */
8799 gimple
*def_stmt
= SSA_NAME_DEF_STMT (arg
);
8800 if (! gimple_nop_p (def_stmt
)
8801 && prop_simulate_again_p (def_stmt
)
8802 && e
->flags
& EDGE_DFS_BACK
)
8803 may_simulate_backedge_again
= true;
8805 vr_arg
= *(get_value_range (arg
));
8806 /* Do not allow equivalences or symbolic ranges to leak in from
8807 backedges. That creates invalid equivalencies.
8808 See PR53465 and PR54767. */
8809 if (e
->flags
& EDGE_DFS_BACK
)
8811 if (vr_arg
.type
== VR_RANGE
8812 || vr_arg
.type
== VR_ANTI_RANGE
)
8814 vr_arg
.equiv
= NULL
;
8815 if (symbolic_range_p (&vr_arg
))
8817 vr_arg
.type
= VR_VARYING
;
8818 vr_arg
.min
= NULL_TREE
;
8819 vr_arg
.max
= NULL_TREE
;
8825 /* If the non-backedge arguments range is VR_VARYING then
8826 we can still try recording a simple equivalence. */
8827 if (vr_arg
.type
== VR_VARYING
)
8829 vr_arg
.type
= VR_RANGE
;
8832 vr_arg
.equiv
= NULL
;
8838 if (TREE_OVERFLOW_P (arg
))
8839 arg
= drop_tree_overflow (arg
);
8841 vr_arg
.type
= VR_RANGE
;
8844 vr_arg
.equiv
= NULL
;
8847 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8849 fprintf (dump_file
, "\t");
8850 print_generic_expr (dump_file
, arg
, dump_flags
);
8851 fprintf (dump_file
, ": ");
8852 dump_value_range (dump_file
, &vr_arg
);
8853 fprintf (dump_file
, "\n");
8857 copy_value_range (vr_result
, &vr_arg
);
8859 vrp_meet (vr_result
, &vr_arg
);
8862 if (vr_result
->type
== VR_VARYING
)
8867 if (vr_result
->type
== VR_VARYING
)
8869 else if (vr_result
->type
== VR_UNDEFINED
)
8872 old_edges
= vr_phi_edge_counts
[SSA_NAME_VERSION (lhs
)];
8873 vr_phi_edge_counts
[SSA_NAME_VERSION (lhs
)] = edges
;
8875 /* To prevent infinite iterations in the algorithm, derive ranges
8876 when the new value is slightly bigger or smaller than the
8877 previous one. We don't do this if we have seen a new executable
8878 edge; this helps us avoid an overflow infinity for conditionals
8879 which are not in a loop. If the old value-range was VR_UNDEFINED
8880 use the updated range and iterate one more time. If we will not
8881 simulate this PHI again via the backedge allow us to iterate. */
8883 && gimple_phi_num_args (phi
) > 1
8884 && edges
== old_edges
8885 && lhs_vr
->type
!= VR_UNDEFINED
8886 && may_simulate_backedge_again
)
8888 /* Compare old and new ranges, fall back to varying if the
8889 values are not comparable. */
8890 int cmp_min
= compare_values (lhs_vr
->min
, vr_result
->min
);
8893 int cmp_max
= compare_values (lhs_vr
->max
, vr_result
->max
);
8897 /* For non VR_RANGE or for pointers fall back to varying if
8898 the range changed. */
8899 if ((lhs_vr
->type
!= VR_RANGE
|| vr_result
->type
!= VR_RANGE
8900 || POINTER_TYPE_P (TREE_TYPE (lhs
)))
8901 && (cmp_min
!= 0 || cmp_max
!= 0))
8904 /* If the new minimum is larger than the previous one
8905 retain the old value. If the new minimum value is smaller
8906 than the previous one and not -INF go all the way to -INF + 1.
8907 In the first case, to avoid infinite bouncing between different
8908 minimums, and in the other case to avoid iterating millions of
8909 times to reach -INF. Going to -INF + 1 also lets the following
8910 iteration compute whether there will be any overflow, at the
8911 expense of one additional iteration. */
8913 vr_result
->min
= lhs_vr
->min
;
8914 else if (cmp_min
> 0
8915 && !vrp_val_is_min (vr_result
->min
))
8917 = int_const_binop (PLUS_EXPR
,
8918 vrp_val_min (TREE_TYPE (vr_result
->min
)),
8919 build_int_cst (TREE_TYPE (vr_result
->min
), 1));
8921 /* Similarly for the maximum value. */
8923 vr_result
->max
= lhs_vr
->max
;
8924 else if (cmp_max
< 0
8925 && !vrp_val_is_max (vr_result
->max
))
8927 = int_const_binop (MINUS_EXPR
,
8928 vrp_val_max (TREE_TYPE (vr_result
->min
)),
8929 build_int_cst (TREE_TYPE (vr_result
->min
), 1));
8931 /* If we dropped either bound to +-INF then if this is a loop
8932 PHI node SCEV may known more about its value-range. */
8933 if (cmp_min
> 0 || cmp_min
< 0
8934 || cmp_max
< 0 || cmp_max
> 0)
8937 goto infinite_check
;
8943 set_value_range_to_varying (vr_result
);
8946 /* If this is a loop PHI node SCEV may known more about its value-range.
8947 scev_check can be reached from two paths, one is a fall through from above
8948 "varying" label, the other is direct goto from code block which tries to
8949 avoid infinite simulation. */
8950 if ((l
= loop_containing_stmt (phi
))
8951 && l
->header
== gimple_bb (phi
))
8952 adjust_range_with_scev (vr_result
, l
, phi
, lhs
);
8955 /* If we will end up with a (-INF, +INF) range, set it to
8956 VARYING. Same if the previous max value was invalid for
8957 the type and we end up with vr_result.min > vr_result.max. */
8958 if ((vr_result
->type
== VR_RANGE
|| vr_result
->type
== VR_ANTI_RANGE
)
8959 && !((vrp_val_is_max (vr_result
->max
) && vrp_val_is_min (vr_result
->min
))
8960 || compare_values (vr_result
->min
, vr_result
->max
) > 0))
8963 set_value_range_to_varying (vr_result
);
8965 /* If the new range is different than the previous value, keep
8971 /* Visit all arguments for PHI node PHI that flow through executable
8972 edges. If a valid value range can be derived from all the incoming
8973 value ranges, set a new range for the LHS of PHI. */
8975 static enum ssa_prop_result
8976 vrp_visit_phi_node (gphi
*phi
)
8978 tree lhs
= PHI_RESULT (phi
);
8979 value_range vr_result
= VR_INITIALIZER
;
8980 extract_range_from_phi_node (phi
, &vr_result
);
8981 if (update_value_range (lhs
, &vr_result
))
8983 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8985 fprintf (dump_file
, "Found new range for ");
8986 print_generic_expr (dump_file
, lhs
, 0);
8987 fprintf (dump_file
, ": ");
8988 dump_value_range (dump_file
, &vr_result
);
8989 fprintf (dump_file
, "\n");
8992 if (vr_result
.type
== VR_VARYING
)
8993 return SSA_PROP_VARYING
;
8995 return SSA_PROP_INTERESTING
;
8998 /* Nothing changed, don't add outgoing edges. */
8999 return SSA_PROP_NOT_INTERESTING
;
9002 /* Simplify boolean operations if the source is known
9003 to be already a boolean. */
9005 simplify_truth_ops_using_ranges (gimple_stmt_iterator
*gsi
, gimple
*stmt
)
9007 enum tree_code rhs_code
= gimple_assign_rhs_code (stmt
);
9009 bool need_conversion
;
9011 /* We handle only !=/== case here. */
9012 gcc_assert (rhs_code
== EQ_EXPR
|| rhs_code
== NE_EXPR
);
9014 op0
= gimple_assign_rhs1 (stmt
);
9015 if (!op_with_boolean_value_range_p (op0
))
9018 op1
= gimple_assign_rhs2 (stmt
);
9019 if (!op_with_boolean_value_range_p (op1
))
9022 /* Reduce number of cases to handle to NE_EXPR. As there is no
9023 BIT_XNOR_EXPR we cannot replace A == B with a single statement. */
9024 if (rhs_code
== EQ_EXPR
)
9026 if (TREE_CODE (op1
) == INTEGER_CST
)
9027 op1
= int_const_binop (BIT_XOR_EXPR
, op1
,
9028 build_int_cst (TREE_TYPE (op1
), 1));
9033 lhs
= gimple_assign_lhs (stmt
);
9035 = !useless_type_conversion_p (TREE_TYPE (lhs
), TREE_TYPE (op0
));
9037 /* Make sure to not sign-extend a 1-bit 1 when converting the result. */
9039 && !TYPE_UNSIGNED (TREE_TYPE (op0
))
9040 && TYPE_PRECISION (TREE_TYPE (op0
)) == 1
9041 && TYPE_PRECISION (TREE_TYPE (lhs
)) > 1)
9044 /* For A != 0 we can substitute A itself. */
9045 if (integer_zerop (op1
))
9046 gimple_assign_set_rhs_with_ops (gsi
,
9048 ? NOP_EXPR
: TREE_CODE (op0
), op0
);
9049 /* For A != B we substitute A ^ B. Either with conversion. */
9050 else if (need_conversion
)
9052 tree tem
= make_ssa_name (TREE_TYPE (op0
));
9054 = gimple_build_assign (tem
, BIT_XOR_EXPR
, op0
, op1
);
9055 gsi_insert_before (gsi
, newop
, GSI_SAME_STMT
);
9056 if (INTEGRAL_TYPE_P (TREE_TYPE (tem
))
9057 && TYPE_PRECISION (TREE_TYPE (tem
)) > 1)
9058 set_range_info (tem
, VR_RANGE
,
9059 wi::zero (TYPE_PRECISION (TREE_TYPE (tem
))),
9060 wi::one (TYPE_PRECISION (TREE_TYPE (tem
))));
9061 gimple_assign_set_rhs_with_ops (gsi
, NOP_EXPR
, tem
);
9065 gimple_assign_set_rhs_with_ops (gsi
, BIT_XOR_EXPR
, op0
, op1
);
9066 update_stmt (gsi_stmt (*gsi
));
9067 fold_stmt (gsi
, follow_single_use_edges
);
9072 /* Simplify a division or modulo operator to a right shift or
9073 bitwise and if the first operand is unsigned or is greater
9074 than zero and the second operand is an exact power of two.
9075 For TRUNC_MOD_EXPR op0 % op1 with constant op1, optimize it
9076 into just op0 if op0's range is known to be a subset of
9077 [-op1 + 1, op1 - 1] for signed and [0, op1 - 1] for unsigned
9081 simplify_div_or_mod_using_ranges (gimple_stmt_iterator
*gsi
, gimple
*stmt
)
9083 enum tree_code rhs_code
= gimple_assign_rhs_code (stmt
);
9085 tree op0
= gimple_assign_rhs1 (stmt
);
9086 tree op1
= gimple_assign_rhs2 (stmt
);
9087 value_range
*vr
= get_value_range (op0
);
9089 if (rhs_code
== TRUNC_MOD_EXPR
9090 && TREE_CODE (op1
) == INTEGER_CST
9091 && tree_int_cst_sgn (op1
) == 1
9092 && range_int_cst_p (vr
)
9093 && tree_int_cst_lt (vr
->max
, op1
))
9095 if (TYPE_UNSIGNED (TREE_TYPE (op0
))
9096 || tree_int_cst_sgn (vr
->min
) >= 0
9097 || tree_int_cst_lt (fold_unary (NEGATE_EXPR
, TREE_TYPE (op1
), op1
),
9100 /* If op0 already has the range op0 % op1 has,
9101 then TRUNC_MOD_EXPR won't change anything. */
9102 gimple_assign_set_rhs_from_tree (gsi
, op0
);
9107 if (!integer_pow2p (op1
))
9109 /* X % -Y can be only optimized into X % Y either if
9110 X is not INT_MIN, or Y is not -1. Fold it now, as after
9111 remove_range_assertions the range info might be not available
9113 if (rhs_code
== TRUNC_MOD_EXPR
9114 && fold_stmt (gsi
, follow_single_use_edges
))
9119 if (TYPE_UNSIGNED (TREE_TYPE (op0
)))
9120 val
= integer_one_node
;
9125 val
= compare_range_with_value (GE_EXPR
, vr
, integer_zero_node
, &sop
);
9129 && integer_onep (val
)
9130 && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC
))
9132 location_t location
;
9134 if (!gimple_has_location (stmt
))
9135 location
= input_location
;
9137 location
= gimple_location (stmt
);
9138 warning_at (location
, OPT_Wstrict_overflow
,
9139 "assuming signed overflow does not occur when "
9140 "simplifying %</%> or %<%%%> to %<>>%> or %<&%>");
9144 if (val
&& integer_onep (val
))
9148 if (rhs_code
== TRUNC_DIV_EXPR
)
9150 t
= build_int_cst (integer_type_node
, tree_log2 (op1
));
9151 gimple_assign_set_rhs_code (stmt
, RSHIFT_EXPR
);
9152 gimple_assign_set_rhs1 (stmt
, op0
);
9153 gimple_assign_set_rhs2 (stmt
, t
);
9157 t
= build_int_cst (TREE_TYPE (op1
), 1);
9158 t
= int_const_binop (MINUS_EXPR
, op1
, t
);
9159 t
= fold_convert (TREE_TYPE (op0
), t
);
9161 gimple_assign_set_rhs_code (stmt
, BIT_AND_EXPR
);
9162 gimple_assign_set_rhs1 (stmt
, op0
);
9163 gimple_assign_set_rhs2 (stmt
, t
);
9167 fold_stmt (gsi
, follow_single_use_edges
);
9174 /* Simplify a min or max if the ranges of the two operands are
9175 disjoint. Return true if we do simplify. */
9178 simplify_min_or_max_using_ranges (gimple_stmt_iterator
*gsi
, gimple
*stmt
)
9180 tree op0
= gimple_assign_rhs1 (stmt
);
9181 tree op1
= gimple_assign_rhs2 (stmt
);
9185 val
= (vrp_evaluate_conditional_warnv_with_ops_using_ranges
9186 (LE_EXPR
, op0
, op1
, &sop
));
9190 val
= (vrp_evaluate_conditional_warnv_with_ops_using_ranges
9191 (LT_EXPR
, op0
, op1
, &sop
));
9196 if (sop
&& issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC
))
9198 location_t location
;
9200 if (!gimple_has_location (stmt
))
9201 location
= input_location
;
9203 location
= gimple_location (stmt
);
9204 warning_at (location
, OPT_Wstrict_overflow
,
9205 "assuming signed overflow does not occur when "
9206 "simplifying %<min/max (X,Y)%> to %<X%> or %<Y%>");
9209 /* VAL == TRUE -> OP0 < or <= op1
9210 VAL == FALSE -> OP0 > or >= op1. */
9211 tree res
= ((gimple_assign_rhs_code (stmt
) == MAX_EXPR
)
9212 == integer_zerop (val
)) ? op0
: op1
;
9213 gimple_assign_set_rhs_from_tree (gsi
, res
);
9220 /* If the operand to an ABS_EXPR is >= 0, then eliminate the
9221 ABS_EXPR. If the operand is <= 0, then simplify the
9222 ABS_EXPR into a NEGATE_EXPR. */
9225 simplify_abs_using_ranges (gimple_stmt_iterator
*gsi
, gimple
*stmt
)
9227 tree op
= gimple_assign_rhs1 (stmt
);
9228 value_range
*vr
= get_value_range (op
);
9235 val
= compare_range_with_value (LE_EXPR
, vr
, integer_zero_node
, &sop
);
9238 /* The range is neither <= 0 nor > 0. Now see if it is
9239 either < 0 or >= 0. */
9241 val
= compare_range_with_value (LT_EXPR
, vr
, integer_zero_node
,
9247 if (sop
&& issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC
))
9249 location_t location
;
9251 if (!gimple_has_location (stmt
))
9252 location
= input_location
;
9254 location
= gimple_location (stmt
);
9255 warning_at (location
, OPT_Wstrict_overflow
,
9256 "assuming signed overflow does not occur when "
9257 "simplifying %<abs (X)%> to %<X%> or %<-X%>");
9260 gimple_assign_set_rhs1 (stmt
, op
);
9261 if (integer_zerop (val
))
9262 gimple_assign_set_rhs_code (stmt
, SSA_NAME
);
9264 gimple_assign_set_rhs_code (stmt
, NEGATE_EXPR
);
9266 fold_stmt (gsi
, follow_single_use_edges
);
9274 /* Optimize away redundant BIT_AND_EXPR and BIT_IOR_EXPR.
9275 If all the bits that are being cleared by & are already
9276 known to be zero from VR, or all the bits that are being
9277 set by | are already known to be one from VR, the bit
9278 operation is redundant. */
9281 simplify_bit_ops_using_ranges (gimple_stmt_iterator
*gsi
, gimple
*stmt
)
9283 tree op0
= gimple_assign_rhs1 (stmt
);
9284 tree op1
= gimple_assign_rhs2 (stmt
);
9285 tree op
= NULL_TREE
;
9286 value_range vr0
= VR_INITIALIZER
;
9287 value_range vr1
= VR_INITIALIZER
;
9288 wide_int may_be_nonzero0
, may_be_nonzero1
;
9289 wide_int must_be_nonzero0
, must_be_nonzero1
;
9292 if (TREE_CODE (op0
) == SSA_NAME
)
9293 vr0
= *(get_value_range (op0
));
9294 else if (is_gimple_min_invariant (op0
))
9295 set_value_range_to_value (&vr0
, op0
, NULL
);
9299 if (TREE_CODE (op1
) == SSA_NAME
)
9300 vr1
= *(get_value_range (op1
));
9301 else if (is_gimple_min_invariant (op1
))
9302 set_value_range_to_value (&vr1
, op1
, NULL
);
9306 if (!zero_nonzero_bits_from_vr (TREE_TYPE (op0
), &vr0
, &may_be_nonzero0
,
9309 if (!zero_nonzero_bits_from_vr (TREE_TYPE (op1
), &vr1
, &may_be_nonzero1
,
9313 switch (gimple_assign_rhs_code (stmt
))
9316 mask
= may_be_nonzero0
.and_not (must_be_nonzero1
);
9322 mask
= may_be_nonzero1
.and_not (must_be_nonzero0
);
9330 mask
= may_be_nonzero0
.and_not (must_be_nonzero1
);
9336 mask
= may_be_nonzero1
.and_not (must_be_nonzero0
);
9347 if (op
== NULL_TREE
)
9350 gimple_assign_set_rhs_with_ops (gsi
, TREE_CODE (op
), op
);
9351 update_stmt (gsi_stmt (*gsi
));
9355 /* We are comparing trees OP0 and OP1 using COND_CODE. OP0 has
9356 a known value range VR.
9358 If there is one and only one value which will satisfy the
9359 conditional, then return that value. Else return NULL.
9361 If signed overflow must be undefined for the value to satisfy
9362 the conditional, then set *STRICT_OVERFLOW_P to true. */
9365 test_for_singularity (enum tree_code cond_code
, tree op0
,
9366 tree op1
, value_range
*vr
,
9367 bool *strict_overflow_p
)
9372 /* Extract minimum/maximum values which satisfy the conditional as it was
9374 if (cond_code
== LE_EXPR
|| cond_code
== LT_EXPR
)
9376 /* This should not be negative infinity; there is no overflow
9378 min
= TYPE_MIN_VALUE (TREE_TYPE (op0
));
9381 if (cond_code
== LT_EXPR
&& !is_overflow_infinity (max
))
9383 tree one
= build_int_cst (TREE_TYPE (op0
), 1);
9384 max
= fold_build2 (MINUS_EXPR
, TREE_TYPE (op0
), max
, one
);
9386 TREE_NO_WARNING (max
) = 1;
9389 else if (cond_code
== GE_EXPR
|| cond_code
== GT_EXPR
)
9391 /* This should not be positive infinity; there is no overflow
9393 max
= TYPE_MAX_VALUE (TREE_TYPE (op0
));
9396 if (cond_code
== GT_EXPR
&& !is_overflow_infinity (min
))
9398 tree one
= build_int_cst (TREE_TYPE (op0
), 1);
9399 min
= fold_build2 (PLUS_EXPR
, TREE_TYPE (op0
), min
, one
);
9401 TREE_NO_WARNING (min
) = 1;
9405 /* Now refine the minimum and maximum values using any
9406 value range information we have for op0. */
9409 if (compare_values (vr
->min
, min
) == 1)
9411 if (compare_values (vr
->max
, max
) == -1)
9414 /* If the new min/max values have converged to a single value,
9415 then there is only one value which can satisfy the condition,
9416 return that value. */
9417 if (operand_equal_p (min
, max
, 0) && is_gimple_min_invariant (min
))
9419 if ((cond_code
== LE_EXPR
|| cond_code
== LT_EXPR
)
9420 && is_overflow_infinity (vr
->max
))
9421 *strict_overflow_p
= true;
9422 if ((cond_code
== GE_EXPR
|| cond_code
== GT_EXPR
)
9423 && is_overflow_infinity (vr
->min
))
9424 *strict_overflow_p
= true;
9432 /* Return whether the value range *VR fits in an integer type specified
9433 by PRECISION and UNSIGNED_P. */
9436 range_fits_type_p (value_range
*vr
, unsigned dest_precision
, signop dest_sgn
)
9439 unsigned src_precision
;
9443 /* We can only handle integral and pointer types. */
9444 src_type
= TREE_TYPE (vr
->min
);
9445 if (!INTEGRAL_TYPE_P (src_type
)
9446 && !POINTER_TYPE_P (src_type
))
9449 /* An extension is fine unless VR is SIGNED and dest_sgn is UNSIGNED,
9450 and so is an identity transform. */
9451 src_precision
= TYPE_PRECISION (TREE_TYPE (vr
->min
));
9452 src_sgn
= TYPE_SIGN (src_type
);
9453 if ((src_precision
< dest_precision
9454 && !(dest_sgn
== UNSIGNED
&& src_sgn
== SIGNED
))
9455 || (src_precision
== dest_precision
&& src_sgn
== dest_sgn
))
9458 /* Now we can only handle ranges with constant bounds. */
9459 if (vr
->type
!= VR_RANGE
9460 || TREE_CODE (vr
->min
) != INTEGER_CST
9461 || TREE_CODE (vr
->max
) != INTEGER_CST
)
9464 /* For sign changes, the MSB of the wide_int has to be clear.
9465 An unsigned value with its MSB set cannot be represented by
9466 a signed wide_int, while a negative value cannot be represented
9467 by an unsigned wide_int. */
9468 if (src_sgn
!= dest_sgn
9469 && (wi::lts_p (vr
->min
, 0) || wi::lts_p (vr
->max
, 0)))
9472 /* Then we can perform the conversion on both ends and compare
9473 the result for equality. */
9474 tem
= wi::ext (wi::to_widest (vr
->min
), dest_precision
, dest_sgn
);
9475 if (tem
!= wi::to_widest (vr
->min
))
9477 tem
= wi::ext (wi::to_widest (vr
->max
), dest_precision
, dest_sgn
);
9478 if (tem
!= wi::to_widest (vr
->max
))
9484 /* Simplify a conditional using a relational operator to an equality
9485 test if the range information indicates only one value can satisfy
9486 the original conditional. */
9489 simplify_cond_using_ranges (gcond
*stmt
)
9491 tree op0
= gimple_cond_lhs (stmt
);
9492 tree op1
= gimple_cond_rhs (stmt
);
9493 enum tree_code cond_code
= gimple_cond_code (stmt
);
9495 if (cond_code
!= NE_EXPR
9496 && cond_code
!= EQ_EXPR
9497 && TREE_CODE (op0
) == SSA_NAME
9498 && INTEGRAL_TYPE_P (TREE_TYPE (op0
))
9499 && is_gimple_min_invariant (op1
))
9501 value_range
*vr
= get_value_range (op0
);
9503 /* If we have range information for OP0, then we might be
9504 able to simplify this conditional. */
9505 if (vr
->type
== VR_RANGE
)
9507 enum warn_strict_overflow_code wc
= WARN_STRICT_OVERFLOW_COMPARISON
;
9509 tree new_tree
= test_for_singularity (cond_code
, op0
, op1
, vr
, &sop
);
9512 && (!sop
|| TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0
))))
9516 fprintf (dump_file
, "Simplified relational ");
9517 print_gimple_stmt (dump_file
, stmt
, 0, 0);
9518 fprintf (dump_file
, " into ");
9521 gimple_cond_set_code (stmt
, EQ_EXPR
);
9522 gimple_cond_set_lhs (stmt
, op0
);
9523 gimple_cond_set_rhs (stmt
, new_tree
);
9529 print_gimple_stmt (dump_file
, stmt
, 0, 0);
9530 fprintf (dump_file
, "\n");
9533 if (sop
&& issue_strict_overflow_warning (wc
))
9535 location_t location
= input_location
;
9536 if (gimple_has_location (stmt
))
9537 location
= gimple_location (stmt
);
9539 warning_at (location
, OPT_Wstrict_overflow
,
9540 "assuming signed overflow does not occur when "
9541 "simplifying conditional");
9547 /* Try again after inverting the condition. We only deal
9548 with integral types here, so no need to worry about
9549 issues with inverting FP comparisons. */
9551 new_tree
= test_for_singularity
9552 (invert_tree_comparison (cond_code
, false),
9553 op0
, op1
, vr
, &sop
);
9556 && (!sop
|| TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0
))))
9560 fprintf (dump_file
, "Simplified relational ");
9561 print_gimple_stmt (dump_file
, stmt
, 0, 0);
9562 fprintf (dump_file
, " into ");
9565 gimple_cond_set_code (stmt
, NE_EXPR
);
9566 gimple_cond_set_lhs (stmt
, op0
);
9567 gimple_cond_set_rhs (stmt
, new_tree
);
9573 print_gimple_stmt (dump_file
, stmt
, 0, 0);
9574 fprintf (dump_file
, "\n");
9577 if (sop
&& issue_strict_overflow_warning (wc
))
9579 location_t location
= input_location
;
9580 if (gimple_has_location (stmt
))
9581 location
= gimple_location (stmt
);
9583 warning_at (location
, OPT_Wstrict_overflow
,
9584 "assuming signed overflow does not occur when "
9585 "simplifying conditional");
9593 /* If we have a comparison of an SSA_NAME (OP0) against a constant,
9594 see if OP0 was set by a type conversion where the source of
9595 the conversion is another SSA_NAME with a range that fits
9596 into the range of OP0's type.
9598 If so, the conversion is redundant as the earlier SSA_NAME can be
9599 used for the comparison directly if we just massage the constant in the
9601 if (TREE_CODE (op0
) == SSA_NAME
9602 && TREE_CODE (op1
) == INTEGER_CST
)
9604 gimple
*def_stmt
= SSA_NAME_DEF_STMT (op0
);
9607 if (!is_gimple_assign (def_stmt
)
9608 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt
)))
9611 innerop
= gimple_assign_rhs1 (def_stmt
);
9613 if (TREE_CODE (innerop
) == SSA_NAME
9614 && !POINTER_TYPE_P (TREE_TYPE (innerop
))
9615 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (innerop
)
9616 && desired_pro_or_demotion_p (TREE_TYPE (innerop
), TREE_TYPE (op0
)))
9618 value_range
*vr
= get_value_range (innerop
);
9620 if (range_int_cst_p (vr
)
9621 && range_fits_type_p (vr
,
9622 TYPE_PRECISION (TREE_TYPE (op0
)),
9623 TYPE_SIGN (TREE_TYPE (op0
)))
9624 && int_fits_type_p (op1
, TREE_TYPE (innerop
))
9625 /* The range must not have overflowed, or if it did overflow
9626 we must not be wrapping/trapping overflow and optimizing
9627 with strict overflow semantics. */
9628 && ((!is_negative_overflow_infinity (vr
->min
)
9629 && !is_positive_overflow_infinity (vr
->max
))
9630 || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (innerop
))))
9632 /* If the range overflowed and the user has asked for warnings
9633 when strict overflow semantics were used to optimize code,
9634 issue an appropriate warning. */
9635 if (cond_code
!= EQ_EXPR
&& cond_code
!= NE_EXPR
9636 && (is_negative_overflow_infinity (vr
->min
)
9637 || is_positive_overflow_infinity (vr
->max
))
9638 && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_CONDITIONAL
))
9640 location_t location
;
9642 if (!gimple_has_location (stmt
))
9643 location
= input_location
;
9645 location
= gimple_location (stmt
);
9646 warning_at (location
, OPT_Wstrict_overflow
,
9647 "assuming signed overflow does not occur when "
9648 "simplifying conditional");
9651 tree newconst
= fold_convert (TREE_TYPE (innerop
), op1
);
9652 gimple_cond_set_lhs (stmt
, innerop
);
9653 gimple_cond_set_rhs (stmt
, newconst
);
9662 /* Simplify a switch statement using the value range of the switch
9666 simplify_switch_using_ranges (gswitch
*stmt
)
9668 tree op
= gimple_switch_index (stmt
);
9669 value_range
*vr
= NULL
;
9673 size_t i
= 0, j
= 0, n
, n2
;
9676 size_t k
= 1, l
= 0;
9678 if (TREE_CODE (op
) == SSA_NAME
)
9680 vr
= get_value_range (op
);
9682 /* We can only handle integer ranges. */
9683 if ((vr
->type
!= VR_RANGE
9684 && vr
->type
!= VR_ANTI_RANGE
)
9685 || symbolic_range_p (vr
))
9688 /* Find case label for min/max of the value range. */
9689 take_default
= !find_case_label_ranges (stmt
, vr
, &i
, &j
, &k
, &l
);
9691 else if (TREE_CODE (op
) == INTEGER_CST
)
9693 take_default
= !find_case_label_index (stmt
, 1, op
, &i
);
9707 n
= gimple_switch_num_labels (stmt
);
9709 /* We can truncate the case label ranges that partially overlap with OP's
9711 size_t min_idx
= 1, max_idx
= 0;
9713 find_case_label_range (stmt
, vr
->min
, vr
->max
, &min_idx
, &max_idx
);
9714 if (min_idx
<= max_idx
)
9716 tree min_label
= gimple_switch_label (stmt
, min_idx
);
9717 tree max_label
= gimple_switch_label (stmt
, max_idx
);
9719 /* Avoid changing the type of the case labels when truncating. */
9720 tree case_label_type
= TREE_TYPE (CASE_LOW (min_label
));
9721 tree vr_min
= fold_convert (case_label_type
, vr
->min
);
9722 tree vr_max
= fold_convert (case_label_type
, vr
->max
);
9724 if (vr
->type
== VR_RANGE
)
9726 /* If OP's value range is [2,8] and the low label range is
9727 0 ... 3, truncate the label's range to 2 .. 3. */
9728 if (tree_int_cst_compare (CASE_LOW (min_label
), vr_min
) < 0
9729 && CASE_HIGH (min_label
) != NULL_TREE
9730 && tree_int_cst_compare (CASE_HIGH (min_label
), vr_min
) >= 0)
9731 CASE_LOW (min_label
) = vr_min
;
9733 /* If OP's value range is [2,8] and the high label range is
9734 7 ... 10, truncate the label's range to 7 .. 8. */
9735 if (tree_int_cst_compare (CASE_LOW (max_label
), vr_max
) <= 0
9736 && CASE_HIGH (max_label
) != NULL_TREE
9737 && tree_int_cst_compare (CASE_HIGH (max_label
), vr_max
) > 0)
9738 CASE_HIGH (max_label
) = vr_max
;
9740 else if (vr
->type
== VR_ANTI_RANGE
)
9742 tree one_cst
= build_one_cst (case_label_type
);
9744 if (min_label
== max_label
)
9746 /* If OP's value range is ~[7,8] and the label's range is
9747 7 ... 10, truncate the label's range to 9 ... 10. */
9748 if (tree_int_cst_compare (CASE_LOW (min_label
), vr_min
) == 0
9749 && CASE_HIGH (min_label
) != NULL_TREE
9750 && tree_int_cst_compare (CASE_HIGH (min_label
), vr_max
) > 0)
9751 CASE_LOW (min_label
)
9752 = int_const_binop (PLUS_EXPR
, vr_max
, one_cst
);
9754 /* If OP's value range is ~[7,8] and the label's range is
9755 5 ... 8, truncate the label's range to 5 ... 6. */
9756 if (tree_int_cst_compare (CASE_LOW (min_label
), vr_min
) < 0
9757 && CASE_HIGH (min_label
) != NULL_TREE
9758 && tree_int_cst_compare (CASE_HIGH (min_label
), vr_max
) == 0)
9759 CASE_HIGH (min_label
)
9760 = int_const_binop (MINUS_EXPR
, vr_min
, one_cst
);
9764 /* If OP's value range is ~[2,8] and the low label range is
9765 0 ... 3, truncate the label's range to 0 ... 1. */
9766 if (tree_int_cst_compare (CASE_LOW (min_label
), vr_min
) < 0
9767 && CASE_HIGH (min_label
) != NULL_TREE
9768 && tree_int_cst_compare (CASE_HIGH (min_label
), vr_min
) >= 0)
9769 CASE_HIGH (min_label
)
9770 = int_const_binop (MINUS_EXPR
, vr_min
, one_cst
);
9772 /* If OP's value range is ~[2,8] and the high label range is
9773 7 ... 10, truncate the label's range to 9 ... 10. */
9774 if (tree_int_cst_compare (CASE_LOW (max_label
), vr_max
) <= 0
9775 && CASE_HIGH (max_label
) != NULL_TREE
9776 && tree_int_cst_compare (CASE_HIGH (max_label
), vr_max
) > 0)
9777 CASE_LOW (max_label
)
9778 = int_const_binop (PLUS_EXPR
, vr_max
, one_cst
);
9782 /* Canonicalize singleton case ranges. */
9783 if (tree_int_cst_equal (CASE_LOW (min_label
), CASE_HIGH (min_label
)))
9784 CASE_HIGH (min_label
) = NULL_TREE
;
9785 if (tree_int_cst_equal (CASE_LOW (max_label
), CASE_HIGH (max_label
)))
9786 CASE_HIGH (max_label
) = NULL_TREE
;
9789 /* We can also eliminate case labels that lie completely outside OP's value
9792 /* Bail out if this is just all edges taken. */
9798 /* Build a new vector of taken case labels. */
9799 vec2
= make_tree_vec (j
- i
+ 1 + l
- k
+ 1 + (int)take_default
);
9802 /* Add the default edge, if necessary. */
9804 TREE_VEC_ELT (vec2
, n2
++) = gimple_switch_default_label (stmt
);
9806 for (; i
<= j
; ++i
, ++n2
)
9807 TREE_VEC_ELT (vec2
, n2
) = gimple_switch_label (stmt
, i
);
9809 for (; k
<= l
; ++k
, ++n2
)
9810 TREE_VEC_ELT (vec2
, n2
) = gimple_switch_label (stmt
, k
);
9812 /* Mark needed edges. */
9813 for (i
= 0; i
< n2
; ++i
)
9815 e
= find_edge (gimple_bb (stmt
),
9816 label_to_block (CASE_LABEL (TREE_VEC_ELT (vec2
, i
))));
9817 e
->aux
= (void *)-1;
9820 /* Queue not needed edges for later removal. */
9821 FOR_EACH_EDGE (e
, ei
, gimple_bb (stmt
)->succs
)
9823 if (e
->aux
== (void *)-1)
9829 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
9831 fprintf (dump_file
, "removing unreachable case label\n");
9833 to_remove_edges
.safe_push (e
);
9834 e
->flags
&= ~EDGE_EXECUTABLE
;
9837 /* And queue an update for the stmt. */
9840 to_update_switch_stmts
.safe_push (su
);
9844 /* Simplify an integral conversion from an SSA name in STMT. */
9847 simplify_conversion_using_ranges (gimple_stmt_iterator
*gsi
, gimple
*stmt
)
9849 tree innerop
, middleop
, finaltype
;
9851 signop inner_sgn
, middle_sgn
, final_sgn
;
9852 unsigned inner_prec
, middle_prec
, final_prec
;
9853 widest_int innermin
, innermed
, innermax
, middlemin
, middlemed
, middlemax
;
9855 finaltype
= TREE_TYPE (gimple_assign_lhs (stmt
));
9856 if (!INTEGRAL_TYPE_P (finaltype
))
9858 middleop
= gimple_assign_rhs1 (stmt
);
9859 def_stmt
= SSA_NAME_DEF_STMT (middleop
);
9860 if (!is_gimple_assign (def_stmt
)
9861 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt
)))
9863 innerop
= gimple_assign_rhs1 (def_stmt
);
9864 if (TREE_CODE (innerop
) != SSA_NAME
9865 || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (innerop
))
9868 /* Get the value-range of the inner operand. Use get_range_info in
9869 case innerop was created during substitute-and-fold. */
9870 wide_int imin
, imax
;
9871 if (!INTEGRAL_TYPE_P (TREE_TYPE (innerop
))
9872 || get_range_info (innerop
, &imin
, &imax
) != VR_RANGE
)
9874 innermin
= widest_int::from (imin
, TYPE_SIGN (TREE_TYPE (innerop
)));
9875 innermax
= widest_int::from (imax
, TYPE_SIGN (TREE_TYPE (innerop
)));
9877 /* Simulate the conversion chain to check if the result is equal if
9878 the middle conversion is removed. */
9879 inner_prec
= TYPE_PRECISION (TREE_TYPE (innerop
));
9880 middle_prec
= TYPE_PRECISION (TREE_TYPE (middleop
));
9881 final_prec
= TYPE_PRECISION (finaltype
);
9883 /* If the first conversion is not injective, the second must not
9885 if (wi::gtu_p (innermax
- innermin
,
9886 wi::mask
<widest_int
> (middle_prec
, false))
9887 && middle_prec
< final_prec
)
9889 /* We also want a medium value so that we can track the effect that
9890 narrowing conversions with sign change have. */
9891 inner_sgn
= TYPE_SIGN (TREE_TYPE (innerop
));
9892 if (inner_sgn
== UNSIGNED
)
9893 innermed
= wi::shifted_mask
<widest_int
> (1, inner_prec
- 1, false);
9896 if (wi::cmp (innermin
, innermed
, inner_sgn
) >= 0
9897 || wi::cmp (innermed
, innermax
, inner_sgn
) >= 0)
9898 innermed
= innermin
;
9900 middle_sgn
= TYPE_SIGN (TREE_TYPE (middleop
));
9901 middlemin
= wi::ext (innermin
, middle_prec
, middle_sgn
);
9902 middlemed
= wi::ext (innermed
, middle_prec
, middle_sgn
);
9903 middlemax
= wi::ext (innermax
, middle_prec
, middle_sgn
);
9905 /* Require that the final conversion applied to both the original
9906 and the intermediate range produces the same result. */
9907 final_sgn
= TYPE_SIGN (finaltype
);
9908 if (wi::ext (middlemin
, final_prec
, final_sgn
)
9909 != wi::ext (innermin
, final_prec
, final_sgn
)
9910 || wi::ext (middlemed
, final_prec
, final_sgn
)
9911 != wi::ext (innermed
, final_prec
, final_sgn
)
9912 || wi::ext (middlemax
, final_prec
, final_sgn
)
9913 != wi::ext (innermax
, final_prec
, final_sgn
))
9916 gimple_assign_set_rhs1 (stmt
, innerop
);
9917 fold_stmt (gsi
, follow_single_use_edges
);
9921 /* Simplify a conversion from integral SSA name to float in STMT. */
9924 simplify_float_conversion_using_ranges (gimple_stmt_iterator
*gsi
,
9927 tree rhs1
= gimple_assign_rhs1 (stmt
);
9928 value_range
*vr
= get_value_range (rhs1
);
9929 machine_mode fltmode
= TYPE_MODE (TREE_TYPE (gimple_assign_lhs (stmt
)));
9934 /* We can only handle constant ranges. */
9935 if (vr
->type
!= VR_RANGE
9936 || TREE_CODE (vr
->min
) != INTEGER_CST
9937 || TREE_CODE (vr
->max
) != INTEGER_CST
)
9940 /* First check if we can use a signed type in place of an unsigned. */
9941 if (TYPE_UNSIGNED (TREE_TYPE (rhs1
))
9942 && (can_float_p (fltmode
, TYPE_MODE (TREE_TYPE (rhs1
)), 0)
9943 != CODE_FOR_nothing
)
9944 && range_fits_type_p (vr
, TYPE_PRECISION (TREE_TYPE (rhs1
)), SIGNED
))
9945 mode
= TYPE_MODE (TREE_TYPE (rhs1
));
9946 /* If we can do the conversion in the current input mode do nothing. */
9947 else if (can_float_p (fltmode
, TYPE_MODE (TREE_TYPE (rhs1
)),
9948 TYPE_UNSIGNED (TREE_TYPE (rhs1
))) != CODE_FOR_nothing
)
9950 /* Otherwise search for a mode we can use, starting from the narrowest
9951 integer mode available. */
9954 mode
= GET_CLASS_NARROWEST_MODE (MODE_INT
);
9957 /* If we cannot do a signed conversion to float from mode
9958 or if the value-range does not fit in the signed type
9959 try with a wider mode. */
9960 if (can_float_p (fltmode
, mode
, 0) != CODE_FOR_nothing
9961 && range_fits_type_p (vr
, GET_MODE_PRECISION (mode
), SIGNED
))
9964 mode
= GET_MODE_WIDER_MODE (mode
);
9965 /* But do not widen the input. Instead leave that to the
9966 optabs expansion code. */
9967 if (GET_MODE_PRECISION (mode
) > TYPE_PRECISION (TREE_TYPE (rhs1
)))
9970 while (mode
!= VOIDmode
);
9971 if (mode
== VOIDmode
)
9975 /* It works, insert a truncation or sign-change before the
9976 float conversion. */
9977 tem
= make_ssa_name (build_nonstandard_integer_type
9978 (GET_MODE_PRECISION (mode
), 0));
9979 conv
= gimple_build_assign (tem
, NOP_EXPR
, rhs1
);
9980 gsi_insert_before (gsi
, conv
, GSI_SAME_STMT
);
9981 gimple_assign_set_rhs1 (stmt
, tem
);
9982 fold_stmt (gsi
, follow_single_use_edges
);
9987 /* Simplify an internal fn call using ranges if possible. */
9990 simplify_internal_call_using_ranges (gimple_stmt_iterator
*gsi
, gimple
*stmt
)
9992 enum tree_code subcode
;
9993 bool is_ubsan
= false;
9995 switch (gimple_call_internal_fn (stmt
))
9997 case IFN_UBSAN_CHECK_ADD
:
9998 subcode
= PLUS_EXPR
;
10001 case IFN_UBSAN_CHECK_SUB
:
10002 subcode
= MINUS_EXPR
;
10005 case IFN_UBSAN_CHECK_MUL
:
10006 subcode
= MULT_EXPR
;
10009 case IFN_ADD_OVERFLOW
:
10010 subcode
= PLUS_EXPR
;
10012 case IFN_SUB_OVERFLOW
:
10013 subcode
= MINUS_EXPR
;
10015 case IFN_MUL_OVERFLOW
:
10016 subcode
= MULT_EXPR
;
10022 tree op0
= gimple_call_arg (stmt
, 0);
10023 tree op1
= gimple_call_arg (stmt
, 1);
10026 type
= TREE_TYPE (op0
);
10027 else if (gimple_call_lhs (stmt
) == NULL_TREE
)
10030 type
= TREE_TYPE (TREE_TYPE (gimple_call_lhs (stmt
)));
10031 if (!check_for_binary_op_overflow (subcode
, type
, op0
, op1
, &ovf
)
10032 || (is_ubsan
&& ovf
))
10036 location_t loc
= gimple_location (stmt
);
10038 g
= gimple_build_assign (gimple_call_lhs (stmt
), subcode
, op0
, op1
);
10041 int prec
= TYPE_PRECISION (type
);
10044 || !useless_type_conversion_p (type
, TREE_TYPE (op0
))
10045 || !useless_type_conversion_p (type
, TREE_TYPE (op1
)))
10046 utype
= build_nonstandard_integer_type (prec
, 1);
10047 if (TREE_CODE (op0
) == INTEGER_CST
)
10048 op0
= fold_convert (utype
, op0
);
10049 else if (!useless_type_conversion_p (utype
, TREE_TYPE (op0
)))
10051 g
= gimple_build_assign (make_ssa_name (utype
), NOP_EXPR
, op0
);
10052 gimple_set_location (g
, loc
);
10053 gsi_insert_before (gsi
, g
, GSI_SAME_STMT
);
10054 op0
= gimple_assign_lhs (g
);
10056 if (TREE_CODE (op1
) == INTEGER_CST
)
10057 op1
= fold_convert (utype
, op1
);
10058 else if (!useless_type_conversion_p (utype
, TREE_TYPE (op1
)))
10060 g
= gimple_build_assign (make_ssa_name (utype
), NOP_EXPR
, op1
);
10061 gimple_set_location (g
, loc
);
10062 gsi_insert_before (gsi
, g
, GSI_SAME_STMT
);
10063 op1
= gimple_assign_lhs (g
);
10065 g
= gimple_build_assign (make_ssa_name (utype
), subcode
, op0
, op1
);
10066 gimple_set_location (g
, loc
);
10067 gsi_insert_before (gsi
, g
, GSI_SAME_STMT
);
10070 g
= gimple_build_assign (make_ssa_name (type
), NOP_EXPR
,
10071 gimple_assign_lhs (g
));
10072 gimple_set_location (g
, loc
);
10073 gsi_insert_before (gsi
, g
, GSI_SAME_STMT
);
10075 g
= gimple_build_assign (gimple_call_lhs (stmt
), COMPLEX_EXPR
,
10076 gimple_assign_lhs (g
),
10077 build_int_cst (type
, ovf
));
10079 gimple_set_location (g
, loc
);
10080 gsi_replace (gsi
, g
, false);
10084 /* Return true if VAR is a two-valued variable. Set a and b with the
10085 two-values when it is true. Return false otherwise. */
10088 two_valued_val_range_p (tree var
, tree
*a
, tree
*b
)
10090 value_range
*vr
= get_value_range (var
);
10091 if ((vr
->type
!= VR_RANGE
10092 && vr
->type
!= VR_ANTI_RANGE
)
10093 || TREE_CODE (vr
->min
) != INTEGER_CST
10094 || TREE_CODE (vr
->max
) != INTEGER_CST
)
10097 if (vr
->type
== VR_RANGE
10098 && wi::sub (vr
->max
, vr
->min
) == 1)
10105 /* ~[TYPE_MIN + 1, TYPE_MAX - 1] */
10106 if (vr
->type
== VR_ANTI_RANGE
10107 && wi::sub (vr
->min
, vrp_val_min (TREE_TYPE (var
))) == 1
10108 && wi::sub (vrp_val_max (TREE_TYPE (var
)), vr
->max
) == 1)
10110 *a
= vrp_val_min (TREE_TYPE (var
));
10111 *b
= vrp_val_max (TREE_TYPE (var
));
10118 /* Simplify STMT using ranges if possible. */
10121 simplify_stmt_using_ranges (gimple_stmt_iterator
*gsi
)
10123 gimple
*stmt
= gsi_stmt (*gsi
);
10124 if (is_gimple_assign (stmt
))
10126 enum tree_code rhs_code
= gimple_assign_rhs_code (stmt
);
10127 tree rhs1
= gimple_assign_rhs1 (stmt
);
10128 tree rhs2
= gimple_assign_rhs2 (stmt
);
10129 tree lhs
= gimple_assign_lhs (stmt
);
10130 tree val1
= NULL_TREE
, val2
= NULL_TREE
;
10131 use_operand_p use_p
;
10135 LHS = CST BINOP VAR
10136 Where VAR is two-valued and LHS is used in GIMPLE_COND only
10138 LHS = VAR == VAL1 ? (CST BINOP VAL1) : (CST BINOP VAL2)
10141 LHS = VAR BINOP CST
10142 Where VAR is two-valued and LHS is used in GIMPLE_COND only
10144 LHS = VAR == VAL1 ? (VAL1 BINOP CST) : (VAL2 BINOP CST) */
10146 if (TREE_CODE_CLASS (rhs_code
) == tcc_binary
10147 && INTEGRAL_TYPE_P (TREE_TYPE (lhs
))
10148 && ((TREE_CODE (rhs1
) == INTEGER_CST
10149 && TREE_CODE (rhs2
) == SSA_NAME
)
10150 || (TREE_CODE (rhs2
) == INTEGER_CST
10151 && TREE_CODE (rhs1
) == SSA_NAME
))
10152 && single_imm_use (lhs
, &use_p
, &use_stmt
)
10153 && gimple_code (use_stmt
) == GIMPLE_COND
)
10156 tree new_rhs1
= NULL_TREE
;
10157 tree new_rhs2
= NULL_TREE
;
10158 tree cmp_var
= NULL_TREE
;
10160 if (TREE_CODE (rhs2
) == SSA_NAME
10161 && two_valued_val_range_p (rhs2
, &val1
, &val2
))
10163 /* Optimize RHS1 OP [VAL1, VAL2]. */
10164 new_rhs1
= int_const_binop (rhs_code
, rhs1
, val1
);
10165 new_rhs2
= int_const_binop (rhs_code
, rhs1
, val2
);
10168 else if (TREE_CODE (rhs1
) == SSA_NAME
10169 && two_valued_val_range_p (rhs1
, &val1
, &val2
))
10171 /* Optimize [VAL1, VAL2] OP RHS2. */
10172 new_rhs1
= int_const_binop (rhs_code
, val1
, rhs2
);
10173 new_rhs2
= int_const_binop (rhs_code
, val2
, rhs2
);
10177 /* If we could not find two-vals or the optimzation is invalid as
10178 in divide by zero, new_rhs1 / new_rhs will be NULL_TREE. */
10179 if (new_rhs1
&& new_rhs2
)
10181 tree cond
= build2 (EQ_EXPR
, TREE_TYPE (cmp_var
), cmp_var
, val1
);
10182 gimple_assign_set_rhs_with_ops (gsi
,
10186 update_stmt (gsi_stmt (*gsi
));
10187 fold_stmt (gsi
, follow_single_use_edges
);
10196 /* Transform EQ_EXPR, NE_EXPR into BIT_XOR_EXPR or identity
10197 if the RHS is zero or one, and the LHS are known to be boolean
10199 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
10200 return simplify_truth_ops_using_ranges (gsi
, stmt
);
10203 /* Transform TRUNC_DIV_EXPR and TRUNC_MOD_EXPR into RSHIFT_EXPR
10204 and BIT_AND_EXPR respectively if the first operand is greater
10205 than zero and the second operand is an exact power of two.
10206 Also optimize TRUNC_MOD_EXPR away if the second operand is
10207 constant and the first operand already has the right value
10209 case TRUNC_DIV_EXPR
:
10210 case TRUNC_MOD_EXPR
:
10211 if (TREE_CODE (rhs1
) == SSA_NAME
10212 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
10213 return simplify_div_or_mod_using_ranges (gsi
, stmt
);
10216 /* Transform ABS (X) into X or -X as appropriate. */
10218 if (TREE_CODE (rhs1
) == SSA_NAME
10219 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
10220 return simplify_abs_using_ranges (gsi
, stmt
);
10225 /* Optimize away BIT_AND_EXPR and BIT_IOR_EXPR
10226 if all the bits being cleared are already cleared or
10227 all the bits being set are already set. */
10228 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
10229 return simplify_bit_ops_using_ranges (gsi
, stmt
);
10233 if (TREE_CODE (rhs1
) == SSA_NAME
10234 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
10235 return simplify_conversion_using_ranges (gsi
, stmt
);
10239 if (TREE_CODE (rhs1
) == SSA_NAME
10240 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
10241 return simplify_float_conversion_using_ranges (gsi
, stmt
);
10246 return simplify_min_or_max_using_ranges (gsi
, stmt
);
10252 else if (gimple_code (stmt
) == GIMPLE_COND
)
10253 return simplify_cond_using_ranges (as_a
<gcond
*> (stmt
));
10254 else if (gimple_code (stmt
) == GIMPLE_SWITCH
)
10255 return simplify_switch_using_ranges (as_a
<gswitch
*> (stmt
));
10256 else if (is_gimple_call (stmt
)
10257 && gimple_call_internal_p (stmt
))
10258 return simplify_internal_call_using_ranges (gsi
, stmt
);
10263 /* If the statement pointed by SI has a predicate whose value can be
10264 computed using the value range information computed by VRP, compute
10265 its value and return true. Otherwise, return false. */
10268 fold_predicate_in (gimple_stmt_iterator
*si
)
10270 bool assignment_p
= false;
10272 gimple
*stmt
= gsi_stmt (*si
);
10274 if (is_gimple_assign (stmt
)
10275 && TREE_CODE_CLASS (gimple_assign_rhs_code (stmt
)) == tcc_comparison
)
10277 assignment_p
= true;
10278 val
= vrp_evaluate_conditional (gimple_assign_rhs_code (stmt
),
10279 gimple_assign_rhs1 (stmt
),
10280 gimple_assign_rhs2 (stmt
),
10283 else if (gcond
*cond_stmt
= dyn_cast
<gcond
*> (stmt
))
10284 val
= vrp_evaluate_conditional (gimple_cond_code (cond_stmt
),
10285 gimple_cond_lhs (cond_stmt
),
10286 gimple_cond_rhs (cond_stmt
),
10294 val
= fold_convert (gimple_expr_type (stmt
), val
);
10298 fprintf (dump_file
, "Folding predicate ");
10299 print_gimple_expr (dump_file
, stmt
, 0, 0);
10300 fprintf (dump_file
, " to ");
10301 print_generic_expr (dump_file
, val
, 0);
10302 fprintf (dump_file
, "\n");
10305 if (is_gimple_assign (stmt
))
10306 gimple_assign_set_rhs_from_tree (si
, val
);
10309 gcc_assert (gimple_code (stmt
) == GIMPLE_COND
);
10310 gcond
*cond_stmt
= as_a
<gcond
*> (stmt
);
10311 if (integer_zerop (val
))
10312 gimple_cond_make_false (cond_stmt
);
10313 else if (integer_onep (val
))
10314 gimple_cond_make_true (cond_stmt
);
10316 gcc_unreachable ();
10325 /* Callback for substitute_and_fold folding the stmt at *SI. */
10328 vrp_fold_stmt (gimple_stmt_iterator
*si
)
10330 if (fold_predicate_in (si
))
10333 return simplify_stmt_using_ranges (si
);
10336 /* Unwindable const/copy equivalences. */
10337 const_and_copies
*equiv_stack
;
10339 /* A trivial wrapper so that we can present the generic jump threading
10340 code with a simple API for simplifying statements. STMT is the
10341 statement we want to simplify, WITHIN_STMT provides the location
10342 for any overflow warnings. */
10345 simplify_stmt_for_jump_threading (gimple
*stmt
, gimple
*within_stmt
,
10346 class avail_exprs_stack
*avail_exprs_stack ATTRIBUTE_UNUSED
)
10348 if (gcond
*cond_stmt
= dyn_cast
<gcond
*> (stmt
))
10349 return vrp_evaluate_conditional (gimple_cond_code (cond_stmt
),
10350 gimple_cond_lhs (cond_stmt
),
10351 gimple_cond_rhs (cond_stmt
),
10354 /* We simplify a switch statement by trying to determine which case label
10355 will be taken. If we are successful then we return the corresponding
10356 CASE_LABEL_EXPR. */
10357 if (gswitch
*switch_stmt
= dyn_cast
<gswitch
*> (stmt
))
10359 tree op
= gimple_switch_index (switch_stmt
);
10360 if (TREE_CODE (op
) != SSA_NAME
)
10363 value_range
*vr
= get_value_range (op
);
10364 if ((vr
->type
!= VR_RANGE
&& vr
->type
!= VR_ANTI_RANGE
)
10365 || symbolic_range_p (vr
))
10368 if (vr
->type
== VR_RANGE
)
10371 /* Get the range of labels that contain a part of the operand's
10373 find_case_label_range (switch_stmt
, vr
->min
, vr
->max
, &i
, &j
);
10375 /* Is there only one such label? */
10378 tree label
= gimple_switch_label (switch_stmt
, i
);
10380 /* The i'th label will be taken only if the value range of the
10381 operand is entirely within the bounds of this label. */
10382 if (CASE_HIGH (label
) != NULL_TREE
10383 ? (tree_int_cst_compare (CASE_LOW (label
), vr
->min
) <= 0
10384 && tree_int_cst_compare (CASE_HIGH (label
), vr
->max
) >= 0)
10385 : (tree_int_cst_equal (CASE_LOW (label
), vr
->min
)
10386 && tree_int_cst_equal (vr
->min
, vr
->max
)))
10390 /* If there are no such labels then the default label will be
10393 return gimple_switch_label (switch_stmt
, 0);
10396 if (vr
->type
== VR_ANTI_RANGE
)
10398 unsigned n
= gimple_switch_num_labels (switch_stmt
);
10399 tree min_label
= gimple_switch_label (switch_stmt
, 1);
10400 tree max_label
= gimple_switch_label (switch_stmt
, n
- 1);
10402 /* The default label will be taken only if the anti-range of the
10403 operand is entirely outside the bounds of all the (non-default)
10405 if (tree_int_cst_compare (vr
->min
, CASE_LOW (min_label
)) <= 0
10406 && (CASE_HIGH (max_label
) != NULL_TREE
10407 ? tree_int_cst_compare (vr
->max
, CASE_HIGH (max_label
)) >= 0
10408 : tree_int_cst_compare (vr
->max
, CASE_LOW (max_label
)) >= 0))
10409 return gimple_switch_label (switch_stmt
, 0);
10415 if (gassign
*assign_stmt
= dyn_cast
<gassign
*> (stmt
))
10417 value_range new_vr
= VR_INITIALIZER
;
10418 tree lhs
= gimple_assign_lhs (assign_stmt
);
10420 if (TREE_CODE (lhs
) == SSA_NAME
10421 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs
))
10422 || POINTER_TYPE_P (TREE_TYPE (lhs
))))
10424 extract_range_from_assignment (&new_vr
, assign_stmt
);
10425 if (range_int_cst_singleton_p (&new_vr
))
10433 /* Blocks which have more than one predecessor and more than
10434 one successor present jump threading opportunities, i.e.,
10435 when the block is reached from a specific predecessor, we
10436 may be able to determine which of the outgoing edges will
10437 be traversed. When this optimization applies, we are able
10438 to avoid conditionals at runtime and we may expose secondary
10439 optimization opportunities.
10441 This routine is effectively a driver for the generic jump
10442 threading code. It basically just presents the generic code
10443 with edges that may be suitable for jump threading.
10445 Unlike DOM, we do not iterate VRP if jump threading was successful.
10446 While iterating may expose new opportunities for VRP, it is expected
10447 those opportunities would be very limited and the compile time cost
10448 to expose those opportunities would be significant.
10450 As jump threading opportunities are discovered, they are registered
10451 for later realization. */
10454 identify_jump_threads (void)
10461 /* Ugh. When substituting values earlier in this pass we can
10462 wipe the dominance information. So rebuild the dominator
10463 information as we need it within the jump threading code. */
10464 calculate_dominance_info (CDI_DOMINATORS
);
10466 /* We do not allow VRP information to be used for jump threading
10467 across a back edge in the CFG. Otherwise it becomes too
10468 difficult to avoid eliminating loop exit tests. Of course
10469 EDGE_DFS_BACK is not accurate at this time so we have to
10471 mark_dfs_back_edges ();
10473 /* Do not thread across edges we are about to remove. Just marking
10474 them as EDGE_IGNORE will do. */
10475 FOR_EACH_VEC_ELT (to_remove_edges
, i
, e
)
10476 e
->flags
|= EDGE_IGNORE
;
10478 /* Allocate our unwinder stack to unwind any temporary equivalences
10479 that might be recorded. */
10480 equiv_stack
= new const_and_copies ();
10482 /* To avoid lots of silly node creation, we create a single
10483 conditional and just modify it in-place when attempting to
10485 dummy
= gimple_build_cond (EQ_EXPR
,
10486 integer_zero_node
, integer_zero_node
,
10489 /* Walk through all the blocks finding those which present a
10490 potential jump threading opportunity. We could set this up
10491 as a dominator walker and record data during the walk, but
10492 I doubt it's worth the effort for the classes of jump
10493 threading opportunities we are trying to identify at this
10494 point in compilation. */
10495 FOR_EACH_BB_FN (bb
, cfun
)
10499 /* If the generic jump threading code does not find this block
10500 interesting, then there is nothing to do. */
10501 if (! potentially_threadable_block (bb
))
10504 last
= last_stmt (bb
);
10506 /* We're basically looking for a switch or any kind of conditional with
10507 integral or pointer type arguments. Note the type of the second
10508 argument will be the same as the first argument, so no need to
10509 check it explicitly.
10511 We also handle the case where there are no statements in the
10512 block. This come up with forwarder blocks that are not
10513 optimized away because they lead to a loop header. But we do
10514 want to thread through them as we can sometimes thread to the
10515 loop exit which is obviously profitable. */
10517 || gimple_code (last
) == GIMPLE_SWITCH
10518 || (gimple_code (last
) == GIMPLE_COND
10519 && TREE_CODE (gimple_cond_lhs (last
)) == SSA_NAME
10520 && (INTEGRAL_TYPE_P (TREE_TYPE (gimple_cond_lhs (last
)))
10521 || POINTER_TYPE_P (TREE_TYPE (gimple_cond_lhs (last
))))
10522 && (TREE_CODE (gimple_cond_rhs (last
)) == SSA_NAME
10523 || is_gimple_min_invariant (gimple_cond_rhs (last
)))))
10527 /* We've got a block with multiple predecessors and multiple
10528 successors which also ends in a suitable conditional or
10529 switch statement. For each predecessor, see if we can thread
10530 it to a specific successor. */
10531 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
10533 /* Do not thread across edges marked to ignoreor abnormal
10534 edges in the CFG. */
10535 if (e
->flags
& (EDGE_IGNORE
| EDGE_COMPLEX
))
10538 thread_across_edge (dummy
, e
, true, equiv_stack
, NULL
,
10539 simplify_stmt_for_jump_threading
);
10544 /* Clear EDGE_IGNORE. */
10545 FOR_EACH_VEC_ELT (to_remove_edges
, i
, e
)
10546 e
->flags
&= ~EDGE_IGNORE
;
10548 /* We do not actually update the CFG or SSA graphs at this point as
10549 ASSERT_EXPRs are still in the IL and cfg cleanup code does not yet
10550 handle ASSERT_EXPRs gracefully. */
10553 /* We identified all the jump threading opportunities earlier, but could
10554 not transform the CFG at that time. This routine transforms the
10555 CFG and arranges for the dominator tree to be rebuilt if necessary.
10557 Note the SSA graph update will occur during the normal TODO
10558 processing by the pass manager. */
10560 finalize_jump_threads (void)
10562 thread_through_all_blocks (false);
10563 delete equiv_stack
;
10566 /* Free VRP lattice. */
10569 vrp_free_lattice ()
10571 /* Free allocated memory. */
10573 free (vr_phi_edge_counts
);
10574 bitmap_obstack_release (&vrp_equiv_obstack
);
10575 vrp_value_range_pool
.release ();
10577 /* So that we can distinguish between VRP data being available
10578 and not available. */
10580 vr_phi_edge_counts
= NULL
;
10583 /* Traverse all the blocks folding conditionals with known ranges. */
10586 vrp_finalize (bool warn_array_bounds_p
)
10590 values_propagated
= true;
10594 fprintf (dump_file
, "\nValue ranges after VRP:\n\n");
10595 dump_all_value_ranges (dump_file
);
10596 fprintf (dump_file
, "\n");
10599 /* Set value range to non pointer SSA_NAMEs. */
10600 for (i
= 0; i
< num_vr_values
; i
++)
10603 tree name
= ssa_name (i
);
10606 || (vr_value
[i
]->type
== VR_VARYING
)
10607 || (vr_value
[i
]->type
== VR_UNDEFINED
)
10608 || (TREE_CODE (vr_value
[i
]->min
) != INTEGER_CST
)
10609 || (TREE_CODE (vr_value
[i
]->max
) != INTEGER_CST
))
10612 if (POINTER_TYPE_P (TREE_TYPE (name
))
10613 && ((vr_value
[i
]->type
== VR_RANGE
10614 && range_includes_zero_p (vr_value
[i
]->min
,
10615 vr_value
[i
]->max
) == 0)
10616 || (vr_value
[i
]->type
== VR_ANTI_RANGE
10617 && range_includes_zero_p (vr_value
[i
]->min
,
10618 vr_value
[i
]->max
) == 1)))
10619 set_ptr_nonnull (name
);
10620 else if (!POINTER_TYPE_P (TREE_TYPE (name
)))
10621 set_range_info (name
, vr_value
[i
]->type
, vr_value
[i
]->min
,
10625 substitute_and_fold (op_with_constant_singleton_value_range
, vrp_fold_stmt
);
10627 if (warn_array_bounds
&& warn_array_bounds_p
)
10628 check_all_array_refs ();
10630 /* We must identify jump threading opportunities before we release
10631 the datastructures built by VRP. */
10632 identify_jump_threads ();
10635 /* evrp_dom_walker visits the basic blocks in the dominance order and set
10636 the Value Ranges (VR) for SSA_NAMEs in the scope. Use this VR to
10637 discover more VRs. */
10639 class evrp_dom_walker
: public dom_walker
10643 : dom_walker (CDI_DOMINATORS
), stack (10)
10645 stmts_to_fixup
.create (0);
10646 need_eh_cleanup
= BITMAP_ALLOC (NULL
);
10648 ~evrp_dom_walker ()
10650 stmts_to_fixup
.release ();
10651 BITMAP_FREE (need_eh_cleanup
);
10653 virtual edge
before_dom_children (basic_block
);
10654 virtual void after_dom_children (basic_block
);
10655 void push_value_range (const_tree var
, value_range
*vr
);
10656 value_range
*pop_value_range (const_tree var
);
10657 value_range
*try_find_new_range (tree op
, tree_code code
, tree limit
);
10659 /* Cond_stack holds the old VR. */
10660 auto_vec
<std::pair
<const_tree
, value_range
*> > stack
;
10661 bitmap need_eh_cleanup
;
10662 vec
<gimple
*> stmts_to_fixup
;
10666 /* Find new range for OP such that (OP CODE LIMIT) is true. */
10669 evrp_dom_walker::try_find_new_range (tree op
, tree_code code
, tree limit
)
10671 value_range vr
= VR_INITIALIZER
;
10672 value_range
*old_vr
= get_value_range (op
);
10674 /* Discover VR when condition is true. */
10675 extract_range_for_var_from_comparison_expr (op
, code
, op
,
10677 if (old_vr
->type
== VR_RANGE
|| old_vr
->type
== VR_ANTI_RANGE
)
10678 vrp_intersect_ranges (&vr
, old_vr
);
10679 /* If we found any usable VR, set the VR to ssa_name and create a
10680 PUSH old value in the stack with the old VR. */
10681 if (vr
.type
== VR_RANGE
|| vr
.type
== VR_ANTI_RANGE
)
10683 value_range
*new_vr
= vrp_value_range_pool
.allocate ();
10690 /* See if there is any new scope is entered with new VR and set that VR to
10691 ssa_name before visiting the statements in the scope. */
10694 evrp_dom_walker::before_dom_children (basic_block bb
)
10696 tree op0
= NULL_TREE
;
10698 push_value_range (NULL_TREE
, NULL
);
10699 if (single_pred_p (bb
))
10701 edge e
= single_pred_edge (bb
);
10702 gimple
*stmt
= last_stmt (e
->src
);
10704 && gimple_code (stmt
) == GIMPLE_COND
10705 && (op0
= gimple_cond_lhs (stmt
))
10706 && TREE_CODE (op0
) == SSA_NAME
10707 && (INTEGRAL_TYPE_P (TREE_TYPE (gimple_cond_lhs (stmt
)))
10708 || POINTER_TYPE_P (TREE_TYPE (gimple_cond_lhs (stmt
)))))
10710 /* Entering a new scope. Try to see if we can find a VR
10712 tree op1
= gimple_cond_rhs (stmt
);
10713 tree_code code
= gimple_cond_code (stmt
);
10715 if (TREE_OVERFLOW_P (op1
))
10716 op1
= drop_tree_overflow (op1
);
10718 /* If condition is false, invert the cond. */
10719 if (e
->flags
& EDGE_FALSE_VALUE
)
10720 code
= invert_tree_comparison (gimple_cond_code (stmt
),
10722 /* Add VR when (OP0 CODE OP1) condition is true. */
10723 value_range
*op0_range
= try_find_new_range (op0
, code
, op1
);
10725 /* Register ranges for y in x < y where
10726 y might have ranges that are useful. */
10728 tree_code new_code
;
10729 if (TREE_CODE (op1
) == SSA_NAME
10730 && extract_code_and_val_from_cond_with_ops (op1
, code
,
10733 &new_code
, &limit
))
10735 /* Add VR when (OP1 NEW_CODE LIMIT) condition is true. */
10736 value_range
*op1_range
= try_find_new_range (op1
, new_code
, limit
);
10738 push_value_range (op1
, op1_range
);
10742 push_value_range (op0
, op0_range
);
10746 /* Visit PHI stmts and discover any new VRs possible. */
10747 gimple_stmt_iterator gsi
;
10750 bool has_unvisited_preds
= false;
10752 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
10753 if (e
->flags
& EDGE_EXECUTABLE
10754 && !(e
->src
->flags
& BB_VISITED
))
10756 has_unvisited_preds
= true;
10760 for (gphi_iterator gpi
= gsi_start_phis (bb
);
10761 !gsi_end_p (gpi
); gsi_next (&gpi
))
10763 gphi
*phi
= gpi
.phi ();
10764 tree lhs
= PHI_RESULT (phi
);
10765 value_range vr_result
= VR_INITIALIZER
;
10766 if (!has_unvisited_preds
10767 && stmt_interesting_for_vrp (phi
))
10768 extract_range_from_phi_node (phi
, &vr_result
);
10770 set_value_range_to_varying (&vr_result
);
10771 update_value_range (lhs
, &vr_result
);
10774 edge taken_edge
= NULL
;
10776 /* Visit all other stmts and discover any new VRs possible. */
10777 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
10779 gimple
*stmt
= gsi_stmt (gsi
);
10780 tree output
= NULL_TREE
;
10781 gimple
*old_stmt
= stmt
;
10782 bool was_noreturn
= (is_gimple_call (stmt
)
10783 && gimple_call_noreturn_p (stmt
));
10785 if (gcond
*cond
= dyn_cast
<gcond
*> (stmt
))
10787 vrp_visit_cond_stmt (cond
, &taken_edge
);
10790 if (taken_edge
->flags
& EDGE_TRUE_VALUE
)
10791 gimple_cond_make_true (cond
);
10792 else if (taken_edge
->flags
& EDGE_FALSE_VALUE
)
10793 gimple_cond_make_false (cond
);
10795 gcc_unreachable ();
10798 else if (stmt_interesting_for_vrp (stmt
))
10801 value_range vr
= VR_INITIALIZER
;
10802 extract_range_from_stmt (stmt
, &taken_edge
, &output
, &vr
);
10804 && (vr
.type
== VR_RANGE
|| vr
.type
== VR_ANTI_RANGE
))
10806 update_value_range (output
, &vr
);
10807 vr
= *get_value_range (output
);
10810 /* Set the SSA with the value range. */
10811 if (INTEGRAL_TYPE_P (TREE_TYPE (output
)))
10813 if ((vr
.type
== VR_RANGE
10814 || vr
.type
== VR_ANTI_RANGE
)
10815 && (TREE_CODE (vr
.min
) == INTEGER_CST
)
10816 && (TREE_CODE (vr
.max
) == INTEGER_CST
))
10817 set_range_info (output
, vr
.type
, vr
.min
, vr
.max
);
10819 else if (POINTER_TYPE_P (TREE_TYPE (output
))
10820 && ((vr
.type
== VR_RANGE
10821 && range_includes_zero_p (vr
.min
,
10823 || (vr
.type
== VR_ANTI_RANGE
10824 && range_includes_zero_p (vr
.min
,
10826 set_ptr_nonnull (output
);
10829 set_defs_to_varying (stmt
);
10832 set_defs_to_varying (stmt
);
10834 /* Try folding stmts with the VR discovered. */
10836 = replace_uses_in (stmt
, op_with_constant_singleton_value_range
);
10837 if (fold_stmt (&gsi
, follow_single_use_edges
)
10839 update_stmt (gsi_stmt (gsi
));
10843 /* If we cleaned up EH information from the statement,
10844 remove EH edges. */
10845 if (maybe_clean_or_replace_eh_stmt (old_stmt
, stmt
))
10846 bitmap_set_bit (need_eh_cleanup
, bb
->index
);
10848 /* If we turned a not noreturn call into a noreturn one
10849 schedule it for fixup. */
10851 && is_gimple_call (stmt
)
10852 && gimple_call_noreturn_p (stmt
))
10853 stmts_to_fixup
.safe_push (stmt
);
10855 if (gimple_assign_single_p (stmt
))
10857 tree rhs
= gimple_assign_rhs1 (stmt
);
10858 if (TREE_CODE (rhs
) == ADDR_EXPR
)
10859 recompute_tree_invariant_for_addr_expr (rhs
);
10863 bb
->flags
|= BB_VISITED
;
10868 /* Restore/pop VRs valid only for BB when we leave BB. */
10871 evrp_dom_walker::after_dom_children (basic_block bb ATTRIBUTE_UNUSED
)
10873 gcc_checking_assert (!stack
.is_empty ());
10874 while (stack
.last ().first
!= NULL_TREE
)
10875 pop_value_range (stack
.last ().first
);
10876 pop_value_range (stack
.last ().first
);
10879 /* Push the Value Range of VAR to the stack and update it with new VR. */
10882 evrp_dom_walker::push_value_range (const_tree var
, value_range
*vr
)
10886 unsigned ver
= SSA_NAME_VERSION (var
);
10887 gcc_checking_assert (vr_value
);
10888 stack
.safe_push (std::make_pair (var
, vr_value
[ver
]));
10890 if (ver
< num_vr_values
)
10891 vr_value
[ver
] = vr
;
10894 stack
.safe_push (std::make_pair (var
, vr
));
10897 /* Pop the Value Range from the vrp_stack and update VAR with it. */
10900 evrp_dom_walker::pop_value_range (const_tree var
)
10902 value_range
*vr
= stack
.last ().second
;
10905 unsigned ver
= SSA_NAME_VERSION (var
);
10906 gcc_checking_assert (var
== stack
.last ().first
);
10907 gcc_checking_assert (vr_value
);
10909 if (ver
< num_vr_values
)
10910 vr_value
[ver
] = vr
;
10917 /* Main entry point for the early vrp pass which is a simplified non-iterative
10918 version of vrp where basic blocks are visited in dominance order. Value
10919 ranges discovered in early vrp will also be used by ipa-vrp. */
10921 static unsigned int
10922 execute_early_vrp ()
10928 loop_optimizer_init (LOOPS_NORMAL
| LOOPS_HAVE_RECORDED_EXITS
);
10929 rewrite_into_loop_closed_ssa (NULL
, TODO_update_ssa
);
10930 scev_initialize ();
10931 calculate_dominance_info (CDI_DOMINATORS
);
10932 FOR_EACH_BB_FN (bb
, cfun
)
10934 bb
->flags
&= ~BB_VISITED
;
10935 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
10936 e
->flags
|= EDGE_EXECUTABLE
;
10938 vrp_initialize_lattice ();
10940 /* Walk stmts in dominance order and propagate VRP. */
10941 evrp_dom_walker walker
;
10942 walker
.walk (ENTRY_BLOCK_PTR_FOR_FN (cfun
));
10944 if (!bitmap_empty_p (walker
.need_eh_cleanup
))
10945 gimple_purge_all_dead_eh_edges (walker
.need_eh_cleanup
);
10947 /* Fixup stmts that became noreturn calls. This may require splitting
10948 blocks and thus isn't possible during the dominator walk. Do this
10949 in reverse order so we don't inadvertedly remove a stmt we want to
10950 fixup by visiting a dominating now noreturn call first. */
10951 while (!walker
.stmts_to_fixup
.is_empty ())
10953 gimple
*stmt
= walker
.stmts_to_fixup
.pop ();
10954 fixup_noreturn_call (stmt
);
10959 fprintf (dump_file
, "\nValue ranges after Early VRP:\n\n");
10960 dump_all_value_ranges (dump_file
);
10961 fprintf (dump_file
, "\n");
10963 vrp_free_lattice ();
10965 loop_optimizer_finalize ();
10970 /* Main entry point to VRP (Value Range Propagation). This pass is
10971 loosely based on J. R. C. Patterson, ``Accurate Static Branch
10972 Prediction by Value Range Propagation,'' in SIGPLAN Conference on
10973 Programming Language Design and Implementation, pp. 67-78, 1995.
10974 Also available at http://citeseer.ist.psu.edu/patterson95accurate.html
10976 This is essentially an SSA-CCP pass modified to deal with ranges
10977 instead of constants.
10979 While propagating ranges, we may find that two or more SSA name
10980 have equivalent, though distinct ranges. For instance,
10983 2 p_4 = ASSERT_EXPR <p_3, p_3 != 0>
10985 4 p_5 = ASSERT_EXPR <p_4, p_4 == q_2>;
10989 In the code above, pointer p_5 has range [q_2, q_2], but from the
10990 code we can also determine that p_5 cannot be NULL and, if q_2 had
10991 a non-varying range, p_5's range should also be compatible with it.
10993 These equivalences are created by two expressions: ASSERT_EXPR and
10994 copy operations. Since p_5 is an assertion on p_4, and p_4 was the
10995 result of another assertion, then we can use the fact that p_5 and
10996 p_4 are equivalent when evaluating p_5's range.
10998 Together with value ranges, we also propagate these equivalences
10999 between names so that we can take advantage of information from
11000 multiple ranges when doing final replacement. Note that this
11001 equivalency relation is transitive but not symmetric.
11003 In the example above, p_5 is equivalent to p_4, q_2 and p_3, but we
11004 cannot assert that q_2 is equivalent to p_5 because q_2 may be used
11005 in contexts where that assertion does not hold (e.g., in line 6).
11007 TODO, the main difference between this pass and Patterson's is that
11008 we do not propagate edge probabilities. We only compute whether
11009 edges can be taken or not. That is, instead of having a spectrum
11010 of jump probabilities between 0 and 1, we only deal with 0, 1 and
11011 DON'T KNOW. In the future, it may be worthwhile to propagate
11012 probabilities to aid branch prediction. */
11014 static unsigned int
11015 execute_vrp (bool warn_array_bounds_p
)
11021 loop_optimizer_init (LOOPS_NORMAL
| LOOPS_HAVE_RECORDED_EXITS
);
11022 rewrite_into_loop_closed_ssa (NULL
, TODO_update_ssa
);
11023 scev_initialize ();
11025 /* ??? This ends up using stale EDGE_DFS_BACK for liveness computation.
11026 Inserting assertions may split edges which will invalidate
11028 insert_range_assertions ();
11030 to_remove_edges
.create (10);
11031 to_update_switch_stmts
.create (5);
11032 threadedge_initialize_values ();
11034 /* For visiting PHI nodes we need EDGE_DFS_BACK computed. */
11035 mark_dfs_back_edges ();
11037 vrp_initialize_lattice ();
11039 ssa_propagate (vrp_visit_stmt
, vrp_visit_phi_node
);
11040 vrp_finalize (warn_array_bounds_p
);
11041 vrp_free_lattice ();
11043 free_numbers_of_iterations_estimates (cfun
);
11045 /* ASSERT_EXPRs must be removed before finalizing jump threads
11046 as finalizing jump threads calls the CFG cleanup code which
11047 does not properly handle ASSERT_EXPRs. */
11048 remove_range_assertions ();
11050 /* If we exposed any new variables, go ahead and put them into
11051 SSA form now, before we handle jump threading. This simplifies
11052 interactions between rewriting of _DECL nodes into SSA form
11053 and rewriting SSA_NAME nodes into SSA form after block
11054 duplication and CFG manipulation. */
11055 update_ssa (TODO_update_ssa
);
11057 finalize_jump_threads ();
11059 /* Remove dead edges from SWITCH_EXPR optimization. This leaves the
11060 CFG in a broken state and requires a cfg_cleanup run. */
11061 FOR_EACH_VEC_ELT (to_remove_edges
, i
, e
)
11063 /* Update SWITCH_EXPR case label vector. */
11064 FOR_EACH_VEC_ELT (to_update_switch_stmts
, i
, su
)
11067 size_t n
= TREE_VEC_LENGTH (su
->vec
);
11069 gimple_switch_set_num_labels (su
->stmt
, n
);
11070 for (j
= 0; j
< n
; j
++)
11071 gimple_switch_set_label (su
->stmt
, j
, TREE_VEC_ELT (su
->vec
, j
));
11072 /* As we may have replaced the default label with a regular one
11073 make sure to make it a real default label again. This ensures
11074 optimal expansion. */
11075 label
= gimple_switch_label (su
->stmt
, 0);
11076 CASE_LOW (label
) = NULL_TREE
;
11077 CASE_HIGH (label
) = NULL_TREE
;
11080 if (to_remove_edges
.length () > 0)
11082 free_dominance_info (CDI_DOMINATORS
);
11083 loops_state_set (LOOPS_NEED_FIXUP
);
11086 to_remove_edges
.release ();
11087 to_update_switch_stmts
.release ();
11088 threadedge_finalize_values ();
11091 loop_optimizer_finalize ();
11097 const pass_data pass_data_vrp
=
11099 GIMPLE_PASS
, /* type */
11101 OPTGROUP_NONE
, /* optinfo_flags */
11102 TV_TREE_VRP
, /* tv_id */
11103 PROP_ssa
, /* properties_required */
11104 0, /* properties_provided */
11105 0, /* properties_destroyed */
11106 0, /* todo_flags_start */
11107 ( TODO_cleanup_cfg
| TODO_update_ssa
), /* todo_flags_finish */
11110 class pass_vrp
: public gimple_opt_pass
11113 pass_vrp (gcc::context
*ctxt
)
11114 : gimple_opt_pass (pass_data_vrp
, ctxt
), warn_array_bounds_p (false)
11117 /* opt_pass methods: */
11118 opt_pass
* clone () { return new pass_vrp (m_ctxt
); }
11119 void set_pass_param (unsigned int n
, bool param
)
11121 gcc_assert (n
== 0);
11122 warn_array_bounds_p
= param
;
11124 virtual bool gate (function
*) { return flag_tree_vrp
!= 0; }
11125 virtual unsigned int execute (function
*)
11126 { return execute_vrp (warn_array_bounds_p
); }
11129 bool warn_array_bounds_p
;
11130 }; // class pass_vrp
11132 } // anon namespace
11135 make_pass_vrp (gcc::context
*ctxt
)
11137 return new pass_vrp (ctxt
);
11142 const pass_data pass_data_early_vrp
=
11144 GIMPLE_PASS
, /* type */
11146 OPTGROUP_NONE
, /* optinfo_flags */
11147 TV_TREE_EARLY_VRP
, /* tv_id */
11148 PROP_ssa
, /* properties_required */
11149 0, /* properties_provided */
11150 0, /* properties_destroyed */
11151 0, /* todo_flags_start */
11152 ( TODO_cleanup_cfg
| TODO_update_ssa
| TODO_verify_all
),
11155 class pass_early_vrp
: public gimple_opt_pass
11158 pass_early_vrp (gcc::context
*ctxt
)
11159 : gimple_opt_pass (pass_data_early_vrp
, ctxt
)
11162 /* opt_pass methods: */
11163 opt_pass
* clone () { return new pass_early_vrp (m_ctxt
); }
11164 virtual bool gate (function
*)
11166 return flag_tree_vrp
!= 0;
11168 virtual unsigned int execute (function
*)
11169 { return execute_early_vrp (); }
11171 }; // class pass_vrp
11172 } // anon namespace
11175 make_pass_early_vrp (gcc::context
*ctxt
)
11177 return new pass_early_vrp (ctxt
);