1 /* Support routines for Value Range Propagation (VRP).
2 Copyright (C) 2005-2017 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"
58 #include "omp-general.h"
60 #include "case-cfn-macros.h"
62 #include "alloc-pool.h"
64 #include "tree-cfgcleanup.h"
65 #include "stringpool.h"
68 #define VR_INITIALIZER { VR_UNDEFINED, NULL_TREE, NULL_TREE, NULL }
70 /* Allocation pools for tree-vrp allocations. */
71 static object_allocator
<value_range
> vrp_value_range_pool ("Tree VRP value ranges");
72 static bitmap_obstack vrp_equiv_obstack
;
74 /* Set of SSA names found live during the RPO traversal of the function
75 for still active basic-blocks. */
78 /* Return true if the SSA name NAME is live on the edge E. */
81 live_on_edge (edge e
, tree name
)
83 return (live
[e
->dest
->index
]
84 && bitmap_bit_p (live
[e
->dest
->index
], SSA_NAME_VERSION (name
)));
87 /* Local functions. */
88 static int compare_values (tree val1
, tree val2
);
89 static int compare_values_warnv (tree val1
, tree val2
, bool *);
90 static tree
vrp_evaluate_conditional_warnv_with_ops (enum tree_code
,
91 tree
, tree
, bool, bool *,
96 /* Predicate code for the ASSERT_EXPR. Must be COMPARISON_CLASS_P. */
97 enum tree_code comp_code
;
99 /* Name to register the assert for. */
102 /* Value being compared against. */
105 /* Expression to compare. */
109 /* Location information for ASSERT_EXPRs. Each instance of this
110 structure describes an ASSERT_EXPR for an SSA name. Since a single
111 SSA name may have more than one assertion associated with it, these
112 locations are kept in a linked list attached to the corresponding
116 /* Basic block where the assertion would be inserted. */
119 /* Some assertions need to be inserted on an edge (e.g., assertions
120 generated by COND_EXPRs). In those cases, BB will be NULL. */
123 /* Pointer to the statement that generated this assertion. */
124 gimple_stmt_iterator si
;
126 /* Predicate code for the ASSERT_EXPR. Must be COMPARISON_CLASS_P. */
127 enum tree_code comp_code
;
129 /* Value being compared against. */
132 /* Expression to compare. */
135 /* Next node in the linked list. */
139 /* If bit I is present, it means that SSA name N_i has a list of
140 assertions that should be inserted in the IL. */
141 static bitmap need_assert_for
;
143 /* Array of locations lists where to insert assertions. ASSERTS_FOR[I]
144 holds a list of ASSERT_LOCUS_T nodes that describe where
145 ASSERT_EXPRs for SSA name N_I should be inserted. */
146 static assert_locus
**asserts_for
;
148 /* Value range array. After propagation, VR_VALUE[I] holds the range
149 of values that SSA name N_I may take. */
150 static unsigned num_vr_values
;
151 static value_range
**vr_value
;
152 static bool values_propagated
;
154 /* For a PHI node which sets SSA name N_I, VR_COUNTS[I] holds the
155 number of executable edges we saw the last time we visited the
157 static int *vr_phi_edge_counts
;
159 struct switch_update
{
164 static vec
<edge
> to_remove_edges
;
165 static vec
<switch_update
> to_update_switch_stmts
;
168 /* Return the maximum value for TYPE. */
171 vrp_val_max (const_tree type
)
173 if (!INTEGRAL_TYPE_P (type
))
176 return TYPE_MAX_VALUE (type
);
179 /* Return the minimum value for TYPE. */
182 vrp_val_min (const_tree type
)
184 if (!INTEGRAL_TYPE_P (type
))
187 return TYPE_MIN_VALUE (type
);
190 /* Return whether VAL is equal to the maximum value of its type.
191 We can't do a simple equality comparison with TYPE_MAX_VALUE because
192 C typedefs and Ada subtypes can produce types whose TYPE_MAX_VALUE
193 is not == to the integer constant with the same value in the type. */
196 vrp_val_is_max (const_tree val
)
198 tree type_max
= vrp_val_max (TREE_TYPE (val
));
199 return (val
== type_max
200 || (type_max
!= NULL_TREE
201 && operand_equal_p (val
, type_max
, 0)));
204 /* Return whether VAL is equal to the minimum value of its type. */
207 vrp_val_is_min (const_tree val
)
209 tree type_min
= vrp_val_min (TREE_TYPE (val
));
210 return (val
== type_min
211 || (type_min
!= NULL_TREE
212 && operand_equal_p (val
, type_min
, 0)));
216 /* Set value range VR to VR_UNDEFINED. */
219 set_value_range_to_undefined (value_range
*vr
)
221 vr
->type
= VR_UNDEFINED
;
222 vr
->min
= vr
->max
= NULL_TREE
;
224 bitmap_clear (vr
->equiv
);
228 /* Set value range VR to VR_VARYING. */
231 set_value_range_to_varying (value_range
*vr
)
233 vr
->type
= VR_VARYING
;
234 vr
->min
= vr
->max
= NULL_TREE
;
236 bitmap_clear (vr
->equiv
);
240 /* Set value range VR to {T, MIN, MAX, EQUIV}. */
243 set_value_range (value_range
*vr
, enum value_range_type t
, tree min
,
244 tree max
, bitmap equiv
)
246 /* Check the validity of the range. */
248 && (t
== VR_RANGE
|| t
== VR_ANTI_RANGE
))
252 gcc_assert (min
&& max
);
254 gcc_assert (!TREE_OVERFLOW_P (min
) && !TREE_OVERFLOW_P (max
));
256 if (INTEGRAL_TYPE_P (TREE_TYPE (min
)) && t
== VR_ANTI_RANGE
)
257 gcc_assert (!vrp_val_is_min (min
) || !vrp_val_is_max (max
));
259 cmp
= compare_values (min
, max
);
260 gcc_assert (cmp
== 0 || cmp
== -1 || cmp
== -2);
264 && (t
== VR_UNDEFINED
|| t
== VR_VARYING
))
266 gcc_assert (min
== NULL_TREE
&& max
== NULL_TREE
);
267 gcc_assert (equiv
== NULL
|| bitmap_empty_p (equiv
));
274 /* Since updating the equivalence set involves deep copying the
275 bitmaps, only do it if absolutely necessary. */
276 if (vr
->equiv
== NULL
278 vr
->equiv
= BITMAP_ALLOC (&vrp_equiv_obstack
);
280 if (equiv
!= vr
->equiv
)
282 if (equiv
&& !bitmap_empty_p (equiv
))
283 bitmap_copy (vr
->equiv
, equiv
);
285 bitmap_clear (vr
->equiv
);
290 /* Set value range VR to the canonical form of {T, MIN, MAX, EQUIV}.
291 This means adjusting T, MIN and MAX representing the case of a
292 wrapping range with MAX < MIN covering [MIN, type_max] U [type_min, MAX]
293 as anti-rage ~[MAX+1, MIN-1]. Likewise for wrapping anti-ranges.
294 In corner cases where MAX+1 or MIN-1 wraps this will fall back
296 This routine exists to ease canonicalization in the case where we
297 extract ranges from var + CST op limit. */
300 set_and_canonicalize_value_range (value_range
*vr
, enum value_range_type t
,
301 tree min
, tree max
, bitmap equiv
)
303 /* Use the canonical setters for VR_UNDEFINED and VR_VARYING. */
304 if (t
== VR_UNDEFINED
)
306 set_value_range_to_undefined (vr
);
309 else if (t
== VR_VARYING
)
311 set_value_range_to_varying (vr
);
315 /* Nothing to canonicalize for symbolic ranges. */
316 if (TREE_CODE (min
) != INTEGER_CST
317 || TREE_CODE (max
) != INTEGER_CST
)
319 set_value_range (vr
, t
, min
, max
, equiv
);
323 /* Wrong order for min and max, to swap them and the VR type we need
325 if (tree_int_cst_lt (max
, min
))
329 /* For one bit precision if max < min, then the swapped
330 range covers all values, so for VR_RANGE it is varying and
331 for VR_ANTI_RANGE empty range, so drop to varying as well. */
332 if (TYPE_PRECISION (TREE_TYPE (min
)) == 1)
334 set_value_range_to_varying (vr
);
338 one
= build_int_cst (TREE_TYPE (min
), 1);
339 tmp
= int_const_binop (PLUS_EXPR
, max
, one
);
340 max
= int_const_binop (MINUS_EXPR
, min
, one
);
343 /* There's one corner case, if we had [C+1, C] before we now have
344 that again. But this represents an empty value range, so drop
345 to varying in this case. */
346 if (tree_int_cst_lt (max
, min
))
348 set_value_range_to_varying (vr
);
352 t
= t
== VR_RANGE
? VR_ANTI_RANGE
: VR_RANGE
;
355 /* Anti-ranges that can be represented as ranges should be so. */
356 if (t
== VR_ANTI_RANGE
)
358 bool is_min
= vrp_val_is_min (min
);
359 bool is_max
= vrp_val_is_max (max
);
361 if (is_min
&& is_max
)
363 /* We cannot deal with empty ranges, drop to varying.
364 ??? This could be VR_UNDEFINED instead. */
365 set_value_range_to_varying (vr
);
368 else if (TYPE_PRECISION (TREE_TYPE (min
)) == 1
369 && (is_min
|| is_max
))
371 /* Non-empty boolean ranges can always be represented
372 as a singleton range. */
374 min
= max
= vrp_val_max (TREE_TYPE (min
));
376 min
= max
= vrp_val_min (TREE_TYPE (min
));
380 /* As a special exception preserve non-null ranges. */
381 && !(TYPE_UNSIGNED (TREE_TYPE (min
))
382 && integer_zerop (max
)))
384 tree one
= build_int_cst (TREE_TYPE (max
), 1);
385 min
= int_const_binop (PLUS_EXPR
, max
, one
);
386 max
= vrp_val_max (TREE_TYPE (max
));
391 tree one
= build_int_cst (TREE_TYPE (min
), 1);
392 max
= int_const_binop (MINUS_EXPR
, min
, one
);
393 min
= vrp_val_min (TREE_TYPE (min
));
398 /* Do not drop [-INF(OVF), +INF(OVF)] to varying. (OVF) has to be sticky
399 to make sure VRP iteration terminates, otherwise we can get into
402 set_value_range (vr
, t
, min
, max
, equiv
);
405 /* Copy value range FROM into value range TO. */
408 copy_value_range (value_range
*to
, value_range
*from
)
410 set_value_range (to
, from
->type
, from
->min
, from
->max
, from
->equiv
);
413 /* Set value range VR to a single value. This function is only called
414 with values we get from statements, and exists to clear the
415 TREE_OVERFLOW flag. */
418 set_value_range_to_value (value_range
*vr
, tree val
, bitmap equiv
)
420 gcc_assert (is_gimple_min_invariant (val
));
421 if (TREE_OVERFLOW_P (val
))
422 val
= drop_tree_overflow (val
);
423 set_value_range (vr
, VR_RANGE
, val
, val
, equiv
);
426 /* Set value range VR to a non-negative range of type TYPE. */
429 set_value_range_to_nonnegative (value_range
*vr
, tree type
)
431 tree zero
= build_int_cst (type
, 0);
432 set_value_range (vr
, VR_RANGE
, zero
, vrp_val_max (type
), vr
->equiv
);
435 /* Set value range VR to a non-NULL range of type TYPE. */
438 set_value_range_to_nonnull (value_range
*vr
, tree type
)
440 tree zero
= build_int_cst (type
, 0);
441 set_value_range (vr
, VR_ANTI_RANGE
, zero
, zero
, vr
->equiv
);
445 /* Set value range VR to a NULL range of type TYPE. */
448 set_value_range_to_null (value_range
*vr
, tree type
)
450 set_value_range_to_value (vr
, build_int_cst (type
, 0), vr
->equiv
);
454 /* Set value range VR to a range of a truthvalue of type TYPE. */
457 set_value_range_to_truthvalue (value_range
*vr
, tree type
)
459 if (TYPE_PRECISION (type
) == 1)
460 set_value_range_to_varying (vr
);
462 set_value_range (vr
, VR_RANGE
,
463 build_int_cst (type
, 0), build_int_cst (type
, 1),
468 /* If abs (min) < abs (max), set VR to [-max, max], if
469 abs (min) >= abs (max), set VR to [-min, min]. */
472 abs_extent_range (value_range
*vr
, tree min
, tree max
)
476 gcc_assert (TREE_CODE (min
) == INTEGER_CST
);
477 gcc_assert (TREE_CODE (max
) == INTEGER_CST
);
478 gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (min
)));
479 gcc_assert (!TYPE_UNSIGNED (TREE_TYPE (min
)));
480 min
= fold_unary (ABS_EXPR
, TREE_TYPE (min
), min
);
481 max
= fold_unary (ABS_EXPR
, TREE_TYPE (max
), max
);
482 if (TREE_OVERFLOW (min
) || TREE_OVERFLOW (max
))
484 set_value_range_to_varying (vr
);
487 cmp
= compare_values (min
, max
);
489 min
= fold_unary (NEGATE_EXPR
, TREE_TYPE (min
), max
);
490 else if (cmp
== 0 || cmp
== 1)
493 min
= fold_unary (NEGATE_EXPR
, TREE_TYPE (min
), min
);
497 set_value_range_to_varying (vr
);
500 set_and_canonicalize_value_range (vr
, VR_RANGE
, min
, max
, NULL
);
504 /* Return value range information for VAR.
506 If we have no values ranges recorded (ie, VRP is not running), then
507 return NULL. Otherwise create an empty range if none existed for VAR. */
510 get_value_range (const_tree var
)
512 static const value_range vr_const_varying
513 = { VR_VARYING
, NULL_TREE
, NULL_TREE
, NULL
};
516 unsigned ver
= SSA_NAME_VERSION (var
);
518 /* If we have no recorded ranges, then return NULL. */
522 /* If we query the range for a new SSA name return an unmodifiable VARYING.
523 We should get here at most from the substitute-and-fold stage which
524 will never try to change values. */
525 if (ver
>= num_vr_values
)
526 return CONST_CAST (value_range
*, &vr_const_varying
);
532 /* After propagation finished do not allocate new value-ranges. */
533 if (values_propagated
)
534 return CONST_CAST (value_range
*, &vr_const_varying
);
536 /* Create a default value range. */
537 vr_value
[ver
] = vr
= vrp_value_range_pool
.allocate ();
538 memset (vr
, 0, sizeof (*vr
));
540 /* Defer allocating the equivalence set. */
543 /* If VAR is a default definition of a parameter, the variable can
544 take any value in VAR's type. */
545 if (SSA_NAME_IS_DEFAULT_DEF (var
))
547 sym
= SSA_NAME_VAR (var
);
548 if (TREE_CODE (sym
) == PARM_DECL
)
550 /* Try to use the "nonnull" attribute to create ~[0, 0]
551 anti-ranges for pointers. Note that this is only valid with
552 default definitions of PARM_DECLs. */
553 if (POINTER_TYPE_P (TREE_TYPE (sym
))
554 && (nonnull_arg_p (sym
)
555 || get_ptr_nonnull (var
)))
556 set_value_range_to_nonnull (vr
, TREE_TYPE (sym
));
557 else if (INTEGRAL_TYPE_P (TREE_TYPE (sym
)))
560 value_range_type rtype
= get_range_info (var
, &min
, &max
);
561 if (rtype
== VR_RANGE
|| rtype
== VR_ANTI_RANGE
)
562 set_value_range (vr
, rtype
,
563 wide_int_to_tree (TREE_TYPE (var
), min
),
564 wide_int_to_tree (TREE_TYPE (var
), max
),
567 set_value_range_to_varying (vr
);
570 set_value_range_to_varying (vr
);
572 else if (TREE_CODE (sym
) == RESULT_DECL
573 && DECL_BY_REFERENCE (sym
))
574 set_value_range_to_nonnull (vr
, TREE_TYPE (sym
));
580 /* Set value-ranges of all SSA names defined by STMT to varying. */
583 set_defs_to_varying (gimple
*stmt
)
587 FOR_EACH_SSA_TREE_OPERAND (def
, stmt
, i
, SSA_OP_DEF
)
589 value_range
*vr
= get_value_range (def
);
590 /* Avoid writing to vr_const_varying get_value_range may return. */
591 if (vr
->type
!= VR_VARYING
)
592 set_value_range_to_varying (vr
);
597 /* Return true, if VAL1 and VAL2 are equal values for VRP purposes. */
600 vrp_operand_equal_p (const_tree val1
, const_tree val2
)
604 if (!val1
|| !val2
|| !operand_equal_p (val1
, val2
, 0))
609 /* Return true, if the bitmaps B1 and B2 are equal. */
612 vrp_bitmap_equal_p (const_bitmap b1
, const_bitmap b2
)
615 || ((!b1
|| bitmap_empty_p (b1
))
616 && (!b2
|| bitmap_empty_p (b2
)))
618 && bitmap_equal_p (b1
, b2
)));
621 /* Update the value range and equivalence set for variable VAR to
622 NEW_VR. Return true if NEW_VR is different from VAR's previous
625 NOTE: This function assumes that NEW_VR is a temporary value range
626 object created for the sole purpose of updating VAR's range. The
627 storage used by the equivalence set from NEW_VR will be freed by
628 this function. Do not call update_value_range when NEW_VR
629 is the range object associated with another SSA name. */
632 update_value_range (const_tree var
, value_range
*new_vr
)
637 /* If there is a value-range on the SSA name from earlier analysis
639 if (INTEGRAL_TYPE_P (TREE_TYPE (var
)))
642 value_range_type rtype
= get_range_info (var
, &min
, &max
);
643 if (rtype
== VR_RANGE
|| rtype
== VR_ANTI_RANGE
)
646 nr_min
= wide_int_to_tree (TREE_TYPE (var
), min
);
647 nr_max
= wide_int_to_tree (TREE_TYPE (var
), max
);
648 value_range nr
= VR_INITIALIZER
;
649 set_and_canonicalize_value_range (&nr
, rtype
, nr_min
, nr_max
, NULL
);
650 vrp_intersect_ranges (new_vr
, &nr
);
654 /* Update the value range, if necessary. */
655 old_vr
= get_value_range (var
);
656 is_new
= old_vr
->type
!= new_vr
->type
657 || !vrp_operand_equal_p (old_vr
->min
, new_vr
->min
)
658 || !vrp_operand_equal_p (old_vr
->max
, new_vr
->max
)
659 || !vrp_bitmap_equal_p (old_vr
->equiv
, new_vr
->equiv
);
663 /* Do not allow transitions up the lattice. The following
664 is slightly more awkward than just new_vr->type < old_vr->type
665 because VR_RANGE and VR_ANTI_RANGE need to be considered
666 the same. We may not have is_new when transitioning to
667 UNDEFINED. If old_vr->type is VARYING, we shouldn't be
669 if (new_vr
->type
== VR_UNDEFINED
)
671 BITMAP_FREE (new_vr
->equiv
);
672 set_value_range_to_varying (old_vr
);
673 set_value_range_to_varying (new_vr
);
677 set_value_range (old_vr
, new_vr
->type
, new_vr
->min
, new_vr
->max
,
681 BITMAP_FREE (new_vr
->equiv
);
687 /* Add VAR and VAR's equivalence set to EQUIV. This is the central
688 point where equivalence processing can be turned on/off. */
691 add_equivalence (bitmap
*equiv
, const_tree var
)
693 unsigned ver
= SSA_NAME_VERSION (var
);
694 value_range
*vr
= get_value_range (var
);
697 *equiv
= BITMAP_ALLOC (&vrp_equiv_obstack
);
698 bitmap_set_bit (*equiv
, ver
);
700 bitmap_ior_into (*equiv
, vr
->equiv
);
704 /* Return true if VR is ~[0, 0]. */
707 range_is_nonnull (value_range
*vr
)
709 return vr
->type
== VR_ANTI_RANGE
710 && integer_zerop (vr
->min
)
711 && integer_zerop (vr
->max
);
715 /* Return true if VR is [0, 0]. */
718 range_is_null (value_range
*vr
)
720 return vr
->type
== VR_RANGE
721 && integer_zerop (vr
->min
)
722 && integer_zerop (vr
->max
);
725 /* Return true if max and min of VR are INTEGER_CST. It's not necessary
729 range_int_cst_p (value_range
*vr
)
731 return (vr
->type
== VR_RANGE
732 && TREE_CODE (vr
->max
) == INTEGER_CST
733 && TREE_CODE (vr
->min
) == INTEGER_CST
);
736 /* Return true if VR is a INTEGER_CST singleton. */
739 range_int_cst_singleton_p (value_range
*vr
)
741 return (range_int_cst_p (vr
)
742 && tree_int_cst_equal (vr
->min
, vr
->max
));
745 /* Return true if value range VR involves at least one symbol. */
748 symbolic_range_p (value_range
*vr
)
750 return (!is_gimple_min_invariant (vr
->min
)
751 || !is_gimple_min_invariant (vr
->max
));
754 /* Return the single symbol (an SSA_NAME) contained in T if any, or NULL_TREE
755 otherwise. We only handle additive operations and set NEG to true if the
756 symbol is negated and INV to the invariant part, if any. */
759 get_single_symbol (tree t
, bool *neg
, tree
*inv
)
767 if (TREE_CODE (t
) == PLUS_EXPR
768 || TREE_CODE (t
) == POINTER_PLUS_EXPR
769 || TREE_CODE (t
) == MINUS_EXPR
)
771 if (is_gimple_min_invariant (TREE_OPERAND (t
, 0)))
773 neg_
= (TREE_CODE (t
) == MINUS_EXPR
);
774 inv_
= TREE_OPERAND (t
, 0);
775 t
= TREE_OPERAND (t
, 1);
777 else if (is_gimple_min_invariant (TREE_OPERAND (t
, 1)))
780 inv_
= TREE_OPERAND (t
, 1);
781 t
= TREE_OPERAND (t
, 0);
792 if (TREE_CODE (t
) == NEGATE_EXPR
)
794 t
= TREE_OPERAND (t
, 0);
798 if (TREE_CODE (t
) != SSA_NAME
)
801 if (inv_
&& TREE_OVERFLOW_P (inv_
))
802 inv_
= drop_tree_overflow (inv_
);
809 /* The reverse operation: build a symbolic expression with TYPE
810 from symbol SYM, negated according to NEG, and invariant INV. */
813 build_symbolic_expr (tree type
, tree sym
, bool neg
, tree inv
)
815 const bool pointer_p
= POINTER_TYPE_P (type
);
819 t
= build1 (NEGATE_EXPR
, type
, t
);
821 if (integer_zerop (inv
))
824 return build2 (pointer_p
? POINTER_PLUS_EXPR
: PLUS_EXPR
, type
, t
, inv
);
827 /* Return true if value range VR involves exactly one symbol SYM. */
830 symbolic_range_based_on_p (value_range
*vr
, const_tree sym
)
832 bool neg
, min_has_symbol
, max_has_symbol
;
835 if (is_gimple_min_invariant (vr
->min
))
836 min_has_symbol
= false;
837 else if (get_single_symbol (vr
->min
, &neg
, &inv
) == sym
)
838 min_has_symbol
= true;
842 if (is_gimple_min_invariant (vr
->max
))
843 max_has_symbol
= false;
844 else if (get_single_symbol (vr
->max
, &neg
, &inv
) == sym
)
845 max_has_symbol
= true;
849 return (min_has_symbol
|| max_has_symbol
);
852 /* Return true if the result of assignment STMT is know to be non-zero. */
855 gimple_assign_nonzero_p (gimple
*stmt
)
857 enum tree_code code
= gimple_assign_rhs_code (stmt
);
858 bool strict_overflow_p
;
859 switch (get_gimple_rhs_class (code
))
861 case GIMPLE_UNARY_RHS
:
862 return tree_unary_nonzero_warnv_p (gimple_assign_rhs_code (stmt
),
863 gimple_expr_type (stmt
),
864 gimple_assign_rhs1 (stmt
),
866 case GIMPLE_BINARY_RHS
:
867 return tree_binary_nonzero_warnv_p (gimple_assign_rhs_code (stmt
),
868 gimple_expr_type (stmt
),
869 gimple_assign_rhs1 (stmt
),
870 gimple_assign_rhs2 (stmt
),
872 case GIMPLE_TERNARY_RHS
:
874 case GIMPLE_SINGLE_RHS
:
875 return tree_single_nonzero_warnv_p (gimple_assign_rhs1 (stmt
),
877 case GIMPLE_INVALID_RHS
:
884 /* Return true if STMT is known to compute a non-zero value. */
887 gimple_stmt_nonzero_p (gimple
*stmt
)
889 switch (gimple_code (stmt
))
892 return gimple_assign_nonzero_p (stmt
);
895 tree fndecl
= gimple_call_fndecl (stmt
);
896 if (!fndecl
) return false;
897 if (flag_delete_null_pointer_checks
&& !flag_check_new
898 && DECL_IS_OPERATOR_NEW (fndecl
)
899 && !TREE_NOTHROW (fndecl
))
901 /* References are always non-NULL. */
902 if (flag_delete_null_pointer_checks
903 && TREE_CODE (TREE_TYPE (fndecl
)) == REFERENCE_TYPE
)
905 if (flag_delete_null_pointer_checks
&&
906 lookup_attribute ("returns_nonnull",
907 TYPE_ATTRIBUTES (gimple_call_fntype (stmt
))))
910 gcall
*call_stmt
= as_a
<gcall
*> (stmt
);
911 unsigned rf
= gimple_call_return_flags (call_stmt
);
912 if (rf
& ERF_RETURNS_ARG
)
914 unsigned argnum
= rf
& ERF_RETURN_ARG_MASK
;
915 if (argnum
< gimple_call_num_args (call_stmt
))
917 tree arg
= gimple_call_arg (call_stmt
, argnum
);
919 && infer_nonnull_range_by_attribute (stmt
, arg
))
923 return gimple_alloca_call_p (stmt
);
930 /* Like tree_expr_nonzero_p, but this function uses value ranges
934 vrp_stmt_computes_nonzero (gimple
*stmt
)
936 if (gimple_stmt_nonzero_p (stmt
))
939 /* If we have an expression of the form &X->a, then the expression
940 is nonnull if X is nonnull. */
941 if (is_gimple_assign (stmt
)
942 && gimple_assign_rhs_code (stmt
) == ADDR_EXPR
)
944 tree expr
= gimple_assign_rhs1 (stmt
);
945 tree base
= get_base_address (TREE_OPERAND (expr
, 0));
947 if (base
!= NULL_TREE
948 && TREE_CODE (base
) == MEM_REF
949 && TREE_CODE (TREE_OPERAND (base
, 0)) == SSA_NAME
)
951 value_range
*vr
= get_value_range (TREE_OPERAND (base
, 0));
952 if (range_is_nonnull (vr
))
960 /* Returns true if EXPR is a valid value (as expected by compare_values) --
961 a gimple invariant, or SSA_NAME +- CST. */
964 valid_value_p (tree expr
)
966 if (TREE_CODE (expr
) == SSA_NAME
)
969 if (TREE_CODE (expr
) == PLUS_EXPR
970 || TREE_CODE (expr
) == MINUS_EXPR
)
971 return (TREE_CODE (TREE_OPERAND (expr
, 0)) == SSA_NAME
972 && TREE_CODE (TREE_OPERAND (expr
, 1)) == INTEGER_CST
);
974 return is_gimple_min_invariant (expr
);
980 -2 if those are incomparable. */
982 operand_less_p (tree val
, tree val2
)
984 /* LT is folded faster than GE and others. Inline the common case. */
985 if (TREE_CODE (val
) == INTEGER_CST
&& TREE_CODE (val2
) == INTEGER_CST
)
986 return tree_int_cst_lt (val
, val2
);
991 fold_defer_overflow_warnings ();
993 tcmp
= fold_binary_to_constant (LT_EXPR
, boolean_type_node
, val
, val2
);
995 fold_undefer_and_ignore_overflow_warnings ();
998 || TREE_CODE (tcmp
) != INTEGER_CST
)
1001 if (!integer_zerop (tcmp
))
1008 /* Compare two values VAL1 and VAL2. Return
1010 -2 if VAL1 and VAL2 cannot be compared at compile-time,
1013 +1 if VAL1 > VAL2, and
1016 This is similar to tree_int_cst_compare but supports pointer values
1017 and values that cannot be compared at compile time.
1019 If STRICT_OVERFLOW_P is not NULL, then set *STRICT_OVERFLOW_P to
1020 true if the return value is only valid if we assume that signed
1021 overflow is undefined. */
1024 compare_values_warnv (tree val1
, tree val2
, bool *strict_overflow_p
)
1029 /* Below we rely on the fact that VAL1 and VAL2 are both pointers or
1031 gcc_assert (POINTER_TYPE_P (TREE_TYPE (val1
))
1032 == POINTER_TYPE_P (TREE_TYPE (val2
)));
1034 /* Convert the two values into the same type. This is needed because
1035 sizetype causes sign extension even for unsigned types. */
1036 val2
= fold_convert (TREE_TYPE (val1
), val2
);
1037 STRIP_USELESS_TYPE_CONVERSION (val2
);
1039 const bool overflow_undefined
1040 = INTEGRAL_TYPE_P (TREE_TYPE (val1
))
1041 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (val1
));
1044 tree sym1
= get_single_symbol (val1
, &neg1
, &inv1
);
1045 tree sym2
= get_single_symbol (val2
, &neg2
, &inv2
);
1047 /* If VAL1 and VAL2 are of the form '[-]NAME [+ CST]', return -1 or +1
1048 accordingly. If VAL1 and VAL2 don't use the same name, return -2. */
1051 /* Both values must use the same name with the same sign. */
1052 if (sym1
!= sym2
|| neg1
!= neg2
)
1055 /* [-]NAME + CST == [-]NAME + CST. */
1059 /* If overflow is defined we cannot simplify more. */
1060 if (!overflow_undefined
)
1063 if (strict_overflow_p
!= NULL
1064 /* Symbolic range building sets TREE_NO_WARNING to declare
1065 that overflow doesn't happen. */
1066 && (!inv1
|| !TREE_NO_WARNING (val1
))
1067 && (!inv2
|| !TREE_NO_WARNING (val2
)))
1068 *strict_overflow_p
= true;
1071 inv1
= build_int_cst (TREE_TYPE (val1
), 0);
1073 inv2
= build_int_cst (TREE_TYPE (val2
), 0);
1075 return wi::cmp (inv1
, inv2
, TYPE_SIGN (TREE_TYPE (val1
)));
1078 const bool cst1
= is_gimple_min_invariant (val1
);
1079 const bool cst2
= is_gimple_min_invariant (val2
);
1081 /* If one is of the form '[-]NAME + CST' and the other is constant, then
1082 it might be possible to say something depending on the constants. */
1083 if ((sym1
&& inv1
&& cst2
) || (sym2
&& inv2
&& cst1
))
1085 if (!overflow_undefined
)
1088 if (strict_overflow_p
!= NULL
1089 /* Symbolic range building sets TREE_NO_WARNING to declare
1090 that overflow doesn't happen. */
1091 && (!sym1
|| !TREE_NO_WARNING (val1
))
1092 && (!sym2
|| !TREE_NO_WARNING (val2
)))
1093 *strict_overflow_p
= true;
1095 const signop sgn
= TYPE_SIGN (TREE_TYPE (val1
));
1096 tree cst
= cst1
? val1
: val2
;
1097 tree inv
= cst1
? inv2
: inv1
;
1099 /* Compute the difference between the constants. If it overflows or
1100 underflows, this means that we can trivially compare the NAME with
1101 it and, consequently, the two values with each other. */
1102 wide_int diff
= wi::sub (cst
, inv
);
1103 if (wi::cmp (0, inv
, sgn
) != wi::cmp (diff
, cst
, sgn
))
1105 const int res
= wi::cmp (cst
, inv
, sgn
);
1106 return cst1
? res
: -res
;
1112 /* We cannot say anything more for non-constants. */
1116 if (!POINTER_TYPE_P (TREE_TYPE (val1
)))
1118 /* We cannot compare overflowed values. */
1119 if (TREE_OVERFLOW (val1
) || TREE_OVERFLOW (val2
))
1122 return tree_int_cst_compare (val1
, val2
);
1128 /* First see if VAL1 and VAL2 are not the same. */
1129 if (val1
== val2
|| operand_equal_p (val1
, val2
, 0))
1132 /* If VAL1 is a lower address than VAL2, return -1. */
1133 if (operand_less_p (val1
, val2
) == 1)
1136 /* If VAL1 is a higher address than VAL2, return +1. */
1137 if (operand_less_p (val2
, val1
) == 1)
1140 /* If VAL1 is different than VAL2, return +2.
1141 For integer constants we either have already returned -1 or 1
1142 or they are equivalent. We still might succeed in proving
1143 something about non-trivial operands. */
1144 if (TREE_CODE (val1
) != INTEGER_CST
1145 || TREE_CODE (val2
) != INTEGER_CST
)
1147 t
= fold_binary_to_constant (NE_EXPR
, boolean_type_node
, val1
, val2
);
1148 if (t
&& integer_onep (t
))
1156 /* Compare values like compare_values_warnv. */
1159 compare_values (tree val1
, tree val2
)
1162 return compare_values_warnv (val1
, val2
, &sop
);
1166 /* Return 1 if VAL is inside value range MIN <= VAL <= MAX,
1167 0 if VAL is not inside [MIN, MAX],
1168 -2 if we cannot tell either way.
1170 Benchmark compile/20001226-1.c compilation time after changing this
1174 value_inside_range (tree val
, tree min
, tree max
)
1178 cmp1
= operand_less_p (val
, min
);
1184 cmp2
= operand_less_p (max
, val
);
1192 /* Return true if value ranges VR0 and VR1 have a non-empty
1195 Benchmark compile/20001226-1.c compilation time after changing this
1200 value_ranges_intersect_p (value_range
*vr0
, value_range
*vr1
)
1202 /* The value ranges do not intersect if the maximum of the first range is
1203 less than the minimum of the second range or vice versa.
1204 When those relations are unknown, we can't do any better. */
1205 if (operand_less_p (vr0
->max
, vr1
->min
) != 0)
1207 if (operand_less_p (vr1
->max
, vr0
->min
) != 0)
1213 /* Return 1 if [MIN, MAX] includes the value zero, 0 if it does not
1214 include the value zero, -2 if we cannot tell. */
1217 range_includes_zero_p (tree min
, tree max
)
1219 tree zero
= build_int_cst (TREE_TYPE (min
), 0);
1220 return value_inside_range (zero
, min
, max
);
1223 /* Return true if *VR is know to only contain nonnegative values. */
1226 value_range_nonnegative_p (value_range
*vr
)
1228 /* Testing for VR_ANTI_RANGE is not useful here as any anti-range
1229 which would return a useful value should be encoded as a
1231 if (vr
->type
== VR_RANGE
)
1233 int result
= compare_values (vr
->min
, integer_zero_node
);
1234 return (result
== 0 || result
== 1);
1240 /* If *VR has a value rante that is a single constant value return that,
1241 otherwise return NULL_TREE. */
1244 value_range_constant_singleton (value_range
*vr
)
1246 if (vr
->type
== VR_RANGE
1247 && vrp_operand_equal_p (vr
->min
, vr
->max
)
1248 && is_gimple_min_invariant (vr
->min
))
1254 /* If OP has a value range with a single constant value return that,
1255 otherwise return NULL_TREE. This returns OP itself if OP is a
1259 op_with_constant_singleton_value_range (tree op
)
1261 if (is_gimple_min_invariant (op
))
1264 if (TREE_CODE (op
) != SSA_NAME
)
1267 return value_range_constant_singleton (get_value_range (op
));
1270 /* Return true if op is in a boolean [0, 1] value-range. */
1273 op_with_boolean_value_range_p (tree op
)
1277 if (TYPE_PRECISION (TREE_TYPE (op
)) == 1)
1280 if (integer_zerop (op
)
1281 || integer_onep (op
))
1284 if (TREE_CODE (op
) != SSA_NAME
)
1287 vr
= get_value_range (op
);
1288 return (vr
->type
== VR_RANGE
1289 && integer_zerop (vr
->min
)
1290 && integer_onep (vr
->max
));
1293 /* Extract value range information for VAR when (OP COND_CODE LIMIT) is
1294 true and store it in *VR_P. */
1297 extract_range_for_var_from_comparison_expr (tree var
, enum tree_code cond_code
,
1298 tree op
, tree limit
,
1301 tree min
, max
, type
;
1302 value_range
*limit_vr
;
1303 type
= TREE_TYPE (var
);
1304 gcc_assert (limit
!= var
);
1306 /* For pointer arithmetic, we only keep track of pointer equality
1308 if (POINTER_TYPE_P (type
) && cond_code
!= NE_EXPR
&& cond_code
!= EQ_EXPR
)
1310 set_value_range_to_varying (vr_p
);
1314 /* If LIMIT is another SSA name and LIMIT has a range of its own,
1315 try to use LIMIT's range to avoid creating symbolic ranges
1317 limit_vr
= (TREE_CODE (limit
) == SSA_NAME
) ? get_value_range (limit
) : NULL
;
1319 /* LIMIT's range is only interesting if it has any useful information. */
1321 || limit_vr
->type
== VR_UNDEFINED
1322 || limit_vr
->type
== VR_VARYING
1323 || (symbolic_range_p (limit_vr
)
1324 && ! (limit_vr
->type
== VR_RANGE
1325 && (limit_vr
->min
== limit_vr
->max
1326 || operand_equal_p (limit_vr
->min
, limit_vr
->max
, 0)))))
1329 /* Initially, the new range has the same set of equivalences of
1330 VAR's range. This will be revised before returning the final
1331 value. Since assertions may be chained via mutually exclusive
1332 predicates, we will need to trim the set of equivalences before
1334 gcc_assert (vr_p
->equiv
== NULL
);
1335 add_equivalence (&vr_p
->equiv
, var
);
1337 /* Extract a new range based on the asserted comparison for VAR and
1338 LIMIT's value range. Notice that if LIMIT has an anti-range, we
1339 will only use it for equality comparisons (EQ_EXPR). For any
1340 other kind of assertion, we cannot derive a range from LIMIT's
1341 anti-range that can be used to describe the new range. For
1342 instance, ASSERT_EXPR <x_2, x_2 <= b_4>. If b_4 is ~[2, 10],
1343 then b_4 takes on the ranges [-INF, 1] and [11, +INF]. There is
1344 no single range for x_2 that could describe LE_EXPR, so we might
1345 as well build the range [b_4, +INF] for it.
1346 One special case we handle is extracting a range from a
1347 range test encoded as (unsigned)var + CST <= limit. */
1348 if (TREE_CODE (op
) == NOP_EXPR
1349 || TREE_CODE (op
) == PLUS_EXPR
)
1351 if (TREE_CODE (op
) == PLUS_EXPR
)
1353 min
= fold_build1 (NEGATE_EXPR
, TREE_TYPE (TREE_OPERAND (op
, 1)),
1354 TREE_OPERAND (op
, 1));
1355 max
= int_const_binop (PLUS_EXPR
, limit
, min
);
1356 op
= TREE_OPERAND (op
, 0);
1360 min
= build_int_cst (TREE_TYPE (var
), 0);
1364 /* Make sure to not set TREE_OVERFLOW on the final type
1365 conversion. We are willingly interpreting large positive
1366 unsigned values as negative signed values here. */
1367 min
= force_fit_type (TREE_TYPE (var
), wi::to_widest (min
), 0, false);
1368 max
= force_fit_type (TREE_TYPE (var
), wi::to_widest (max
), 0, false);
1370 /* We can transform a max, min range to an anti-range or
1371 vice-versa. Use set_and_canonicalize_value_range which does
1373 if (cond_code
== LE_EXPR
)
1374 set_and_canonicalize_value_range (vr_p
, VR_RANGE
,
1375 min
, max
, vr_p
->equiv
);
1376 else if (cond_code
== GT_EXPR
)
1377 set_and_canonicalize_value_range (vr_p
, VR_ANTI_RANGE
,
1378 min
, max
, vr_p
->equiv
);
1382 else if (cond_code
== EQ_EXPR
)
1384 enum value_range_type range_type
;
1388 range_type
= limit_vr
->type
;
1389 min
= limit_vr
->min
;
1390 max
= limit_vr
->max
;
1394 range_type
= VR_RANGE
;
1399 set_value_range (vr_p
, range_type
, min
, max
, vr_p
->equiv
);
1401 /* When asserting the equality VAR == LIMIT and LIMIT is another
1402 SSA name, the new range will also inherit the equivalence set
1404 if (TREE_CODE (limit
) == SSA_NAME
)
1405 add_equivalence (&vr_p
->equiv
, limit
);
1407 else if (cond_code
== NE_EXPR
)
1409 /* As described above, when LIMIT's range is an anti-range and
1410 this assertion is an inequality (NE_EXPR), then we cannot
1411 derive anything from the anti-range. For instance, if
1412 LIMIT's range was ~[0, 0], the assertion 'VAR != LIMIT' does
1413 not imply that VAR's range is [0, 0]. So, in the case of
1414 anti-ranges, we just assert the inequality using LIMIT and
1417 If LIMIT_VR is a range, we can only use it to build a new
1418 anti-range if LIMIT_VR is a single-valued range. For
1419 instance, if LIMIT_VR is [0, 1], the predicate
1420 VAR != [0, 1] does not mean that VAR's range is ~[0, 1].
1421 Rather, it means that for value 0 VAR should be ~[0, 0]
1422 and for value 1, VAR should be ~[1, 1]. We cannot
1423 represent these ranges.
1425 The only situation in which we can build a valid
1426 anti-range is when LIMIT_VR is a single-valued range
1427 (i.e., LIMIT_VR->MIN == LIMIT_VR->MAX). In that case,
1428 build the anti-range ~[LIMIT_VR->MIN, LIMIT_VR->MAX]. */
1430 && limit_vr
->type
== VR_RANGE
1431 && compare_values (limit_vr
->min
, limit_vr
->max
) == 0)
1433 min
= limit_vr
->min
;
1434 max
= limit_vr
->max
;
1438 /* In any other case, we cannot use LIMIT's range to build a
1439 valid anti-range. */
1443 /* If MIN and MAX cover the whole range for their type, then
1444 just use the original LIMIT. */
1445 if (INTEGRAL_TYPE_P (type
)
1446 && vrp_val_is_min (min
)
1447 && vrp_val_is_max (max
))
1450 set_and_canonicalize_value_range (vr_p
, VR_ANTI_RANGE
,
1451 min
, max
, vr_p
->equiv
);
1453 else if (cond_code
== LE_EXPR
|| cond_code
== LT_EXPR
)
1455 min
= TYPE_MIN_VALUE (type
);
1457 if (limit_vr
== NULL
|| limit_vr
->type
== VR_ANTI_RANGE
)
1461 /* If LIMIT_VR is of the form [N1, N2], we need to build the
1462 range [MIN, N2] for LE_EXPR and [MIN, N2 - 1] for
1464 max
= limit_vr
->max
;
1467 /* If the maximum value forces us to be out of bounds, simply punt.
1468 It would be pointless to try and do anything more since this
1469 all should be optimized away above us. */
1470 if (cond_code
== LT_EXPR
1471 && compare_values (max
, min
) == 0)
1472 set_value_range_to_varying (vr_p
);
1475 /* For LT_EXPR, we create the range [MIN, MAX - 1]. */
1476 if (cond_code
== LT_EXPR
)
1478 if (TYPE_PRECISION (TREE_TYPE (max
)) == 1
1479 && !TYPE_UNSIGNED (TREE_TYPE (max
)))
1480 max
= fold_build2 (PLUS_EXPR
, TREE_TYPE (max
), max
,
1481 build_int_cst (TREE_TYPE (max
), -1));
1483 max
= fold_build2 (MINUS_EXPR
, TREE_TYPE (max
), max
,
1484 build_int_cst (TREE_TYPE (max
), 1));
1485 /* Signal to compare_values_warnv this expr doesn't overflow. */
1487 TREE_NO_WARNING (max
) = 1;
1490 set_value_range (vr_p
, VR_RANGE
, min
, max
, vr_p
->equiv
);
1493 else if (cond_code
== GE_EXPR
|| cond_code
== GT_EXPR
)
1495 max
= TYPE_MAX_VALUE (type
);
1497 if (limit_vr
== NULL
|| limit_vr
->type
== VR_ANTI_RANGE
)
1501 /* If LIMIT_VR is of the form [N1, N2], we need to build the
1502 range [N1, MAX] for GE_EXPR and [N1 + 1, MAX] for
1504 min
= limit_vr
->min
;
1507 /* If the minimum value forces us to be out of bounds, simply punt.
1508 It would be pointless to try and do anything more since this
1509 all should be optimized away above us. */
1510 if (cond_code
== GT_EXPR
1511 && compare_values (min
, max
) == 0)
1512 set_value_range_to_varying (vr_p
);
1515 /* For GT_EXPR, we create the range [MIN + 1, MAX]. */
1516 if (cond_code
== GT_EXPR
)
1518 if (TYPE_PRECISION (TREE_TYPE (min
)) == 1
1519 && !TYPE_UNSIGNED (TREE_TYPE (min
)))
1520 min
= fold_build2 (MINUS_EXPR
, TREE_TYPE (min
), min
,
1521 build_int_cst (TREE_TYPE (min
), -1));
1523 min
= fold_build2 (PLUS_EXPR
, TREE_TYPE (min
), min
,
1524 build_int_cst (TREE_TYPE (min
), 1));
1525 /* Signal to compare_values_warnv this expr doesn't overflow. */
1527 TREE_NO_WARNING (min
) = 1;
1530 set_value_range (vr_p
, VR_RANGE
, min
, max
, vr_p
->equiv
);
1536 /* Finally intersect the new range with what we already know about var. */
1537 vrp_intersect_ranges (vr_p
, get_value_range (var
));
1540 /* Extract value range information from an ASSERT_EXPR EXPR and store
1544 extract_range_from_assert (value_range
*vr_p
, tree expr
)
1546 tree var
= ASSERT_EXPR_VAR (expr
);
1547 tree cond
= ASSERT_EXPR_COND (expr
);
1549 enum tree_code cond_code
;
1550 gcc_assert (COMPARISON_CLASS_P (cond
));
1552 /* Find VAR in the ASSERT_EXPR conditional. */
1553 if (var
== TREE_OPERAND (cond
, 0)
1554 || TREE_CODE (TREE_OPERAND (cond
, 0)) == PLUS_EXPR
1555 || TREE_CODE (TREE_OPERAND (cond
, 0)) == NOP_EXPR
)
1557 /* If the predicate is of the form VAR COMP LIMIT, then we just
1558 take LIMIT from the RHS and use the same comparison code. */
1559 cond_code
= TREE_CODE (cond
);
1560 limit
= TREE_OPERAND (cond
, 1);
1561 op
= TREE_OPERAND (cond
, 0);
1565 /* If the predicate is of the form LIMIT COMP VAR, then we need
1566 to flip around the comparison code to create the proper range
1568 cond_code
= swap_tree_comparison (TREE_CODE (cond
));
1569 limit
= TREE_OPERAND (cond
, 0);
1570 op
= TREE_OPERAND (cond
, 1);
1572 extract_range_for_var_from_comparison_expr (var
, cond_code
, op
,
1576 /* Extract range information from SSA name VAR and store it in VR. If
1577 VAR has an interesting range, use it. Otherwise, create the
1578 range [VAR, VAR] and return it. This is useful in situations where
1579 we may have conditionals testing values of VARYING names. For
1586 Even if y_5 is deemed VARYING, we can determine that x_3 > y_5 is
1590 extract_range_from_ssa_name (value_range
*vr
, tree var
)
1592 value_range
*var_vr
= get_value_range (var
);
1594 if (var_vr
->type
!= VR_VARYING
)
1595 copy_value_range (vr
, var_vr
);
1597 set_value_range (vr
, VR_RANGE
, var
, var
, NULL
);
1599 add_equivalence (&vr
->equiv
, var
);
1603 /* Wrapper around int_const_binop. If the operation overflows and
1604 overflow is undefined, then adjust the result to be
1605 -INF or +INF depending on CODE, VAL1 and VAL2. Sets *OVERFLOW_P
1606 to whether the operation overflowed. For division by zero
1607 the result is indeterminate but *OVERFLOW_P is set. */
1610 vrp_int_const_binop (enum tree_code code
, tree val1
, tree val2
,
1613 bool overflow
= false;
1614 signop sign
= TYPE_SIGN (TREE_TYPE (val1
));
1617 *overflow_p
= false;
1624 wide_int wval2
= wi::to_wide (val2
, TYPE_PRECISION (TREE_TYPE (val1
)));
1625 if (wi::neg_p (wval2
))
1628 if (code
== RSHIFT_EXPR
)
1634 if (code
== RSHIFT_EXPR
)
1635 /* It's unclear from the C standard whether shifts can overflow.
1636 The following code ignores overflow; perhaps a C standard
1637 interpretation ruling is needed. */
1638 res
= wi::rshift (val1
, wval2
, sign
);
1640 res
= wi::lshift (val1
, wval2
);
1645 res
= wi::mul (val1
, val2
, sign
, &overflow
);
1648 case TRUNC_DIV_EXPR
:
1649 case EXACT_DIV_EXPR
:
1656 res
= wi::div_trunc (val1
, val2
, sign
, &overflow
);
1659 case FLOOR_DIV_EXPR
:
1665 res
= wi::div_floor (val1
, val2
, sign
, &overflow
);
1674 res
= wi::div_ceil (val1
, val2
, sign
, &overflow
);
1677 case ROUND_DIV_EXPR
:
1683 res
= wi::div_round (val1
, val2
, sign
, &overflow
);
1691 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (val1
)))
1693 /* If the operation overflowed return -INF or +INF depending
1694 on the operation and the combination of signs of the operands. */
1695 int sgn1
= tree_int_cst_sgn (val1
);
1696 int sgn2
= tree_int_cst_sgn (val2
);
1698 /* Notice that we only need to handle the restricted set of
1699 operations handled by extract_range_from_binary_expr.
1700 Among them, only multiplication, addition and subtraction
1701 can yield overflow without overflown operands because we
1702 are working with integral types only... except in the
1703 case VAL1 = -INF and VAL2 = -1 which overflows to +INF
1704 for division too. */
1706 /* For multiplication, the sign of the overflow is given
1707 by the comparison of the signs of the operands. */
1708 if ((code
== MULT_EXPR
&& sgn1
== sgn2
)
1709 /* For addition, the operands must be of the same sign
1710 to yield an overflow. Its sign is therefore that
1711 of one of the operands, for example the first. */
1712 || (code
== PLUS_EXPR
&& sgn1
>= 0)
1713 /* For subtraction, operands must be of
1714 different signs to yield an overflow. Its sign is
1715 therefore that of the first operand or the opposite of
1716 that of the second operand. A first operand of 0 counts
1717 as positive here, for the corner case 0 - (-INF), which
1718 overflows, but must yield +INF. */
1719 || (code
== MINUS_EXPR
&& sgn1
>= 0)
1720 /* For division, the only case is -INF / -1 = +INF. */
1721 || code
== TRUNC_DIV_EXPR
1722 || code
== FLOOR_DIV_EXPR
1723 || code
== CEIL_DIV_EXPR
1724 || code
== EXACT_DIV_EXPR
1725 || code
== ROUND_DIV_EXPR
)
1726 return wi::max_value (TYPE_PRECISION (TREE_TYPE (val1
)),
1727 TYPE_SIGN (TREE_TYPE (val1
)));
1729 return wi::min_value (TYPE_PRECISION (TREE_TYPE (val1
)),
1730 TYPE_SIGN (TREE_TYPE (val1
)));
1733 *overflow_p
= overflow
;
1739 /* For range VR compute two wide_int bitmasks. In *MAY_BE_NONZERO
1740 bitmask if some bit is unset, it means for all numbers in the range
1741 the bit is 0, otherwise it might be 0 or 1. In *MUST_BE_NONZERO
1742 bitmask if some bit is set, it means for all numbers in the range
1743 the bit is 1, otherwise it might be 0 or 1. */
1746 zero_nonzero_bits_from_vr (const tree expr_type
,
1748 wide_int
*may_be_nonzero
,
1749 wide_int
*must_be_nonzero
)
1751 *may_be_nonzero
= wi::minus_one (TYPE_PRECISION (expr_type
));
1752 *must_be_nonzero
= wi::zero (TYPE_PRECISION (expr_type
));
1753 if (!range_int_cst_p (vr
))
1756 if (range_int_cst_singleton_p (vr
))
1758 *may_be_nonzero
= vr
->min
;
1759 *must_be_nonzero
= *may_be_nonzero
;
1761 else if (tree_int_cst_sgn (vr
->min
) >= 0
1762 || tree_int_cst_sgn (vr
->max
) < 0)
1764 wide_int xor_mask
= wi::bit_xor (vr
->min
, vr
->max
);
1765 *may_be_nonzero
= wi::bit_or (vr
->min
, vr
->max
);
1766 *must_be_nonzero
= wi::bit_and (vr
->min
, vr
->max
);
1769 wide_int mask
= wi::mask (wi::floor_log2 (xor_mask
), false,
1770 may_be_nonzero
->get_precision ());
1771 *may_be_nonzero
= *may_be_nonzero
| mask
;
1772 *must_be_nonzero
= must_be_nonzero
->and_not (mask
);
1779 /* Create two value-ranges in *VR0 and *VR1 from the anti-range *AR
1780 so that *VR0 U *VR1 == *AR. Returns true if that is possible,
1781 false otherwise. If *AR can be represented with a single range
1782 *VR1 will be VR_UNDEFINED. */
1785 ranges_from_anti_range (value_range
*ar
,
1786 value_range
*vr0
, value_range
*vr1
)
1788 tree type
= TREE_TYPE (ar
->min
);
1790 vr0
->type
= VR_UNDEFINED
;
1791 vr1
->type
= VR_UNDEFINED
;
1793 if (ar
->type
!= VR_ANTI_RANGE
1794 || TREE_CODE (ar
->min
) != INTEGER_CST
1795 || TREE_CODE (ar
->max
) != INTEGER_CST
1796 || !vrp_val_min (type
)
1797 || !vrp_val_max (type
))
1800 if (!vrp_val_is_min (ar
->min
))
1802 vr0
->type
= VR_RANGE
;
1803 vr0
->min
= vrp_val_min (type
);
1804 vr0
->max
= wide_int_to_tree (type
, wi::sub (ar
->min
, 1));
1806 if (!vrp_val_is_max (ar
->max
))
1808 vr1
->type
= VR_RANGE
;
1809 vr1
->min
= wide_int_to_tree (type
, wi::add (ar
->max
, 1));
1810 vr1
->max
= vrp_val_max (type
);
1812 if (vr0
->type
== VR_UNDEFINED
)
1815 vr1
->type
= VR_UNDEFINED
;
1818 return vr0
->type
!= VR_UNDEFINED
;
1821 /* Helper to extract a value-range *VR for a multiplicative operation
1825 extract_range_from_multiplicative_op_1 (value_range
*vr
,
1826 enum tree_code code
,
1827 value_range
*vr0
, value_range
*vr1
)
1829 enum value_range_type rtype
;
1830 wide_int val
, min
, max
;
1834 /* Multiplications, divisions and shifts are a bit tricky to handle,
1835 depending on the mix of signs we have in the two ranges, we
1836 need to operate on different values to get the minimum and
1837 maximum values for the new range. One approach is to figure
1838 out all the variations of range combinations and do the
1841 However, this involves several calls to compare_values and it
1842 is pretty convoluted. It's simpler to do the 4 operations
1843 (MIN0 OP MIN1, MIN0 OP MAX1, MAX0 OP MIN1 and MAX0 OP MAX0 OP
1844 MAX1) and then figure the smallest and largest values to form
1846 gcc_assert (code
== MULT_EXPR
1847 || code
== TRUNC_DIV_EXPR
1848 || code
== FLOOR_DIV_EXPR
1849 || code
== CEIL_DIV_EXPR
1850 || code
== EXACT_DIV_EXPR
1851 || code
== ROUND_DIV_EXPR
1852 || code
== RSHIFT_EXPR
1853 || code
== LSHIFT_EXPR
);
1854 gcc_assert ((vr0
->type
== VR_RANGE
1855 || (code
== MULT_EXPR
&& vr0
->type
== VR_ANTI_RANGE
))
1856 && vr0
->type
== vr1
->type
);
1859 type
= TREE_TYPE (vr0
->min
);
1860 signop sgn
= TYPE_SIGN (type
);
1862 /* Compute the 4 cross operations and their minimum and maximum value. */
1864 val
= vrp_int_const_binop (code
, vr0
->min
, vr1
->min
, &sop
);
1868 if (vr1
->max
== vr1
->min
)
1872 val
= vrp_int_const_binop (code
, vr0
->min
, vr1
->max
, &sop
);
1875 if (wi::lt_p (val
, min
, sgn
))
1877 else if (wi::gt_p (val
, max
, sgn
))
1882 if (vr0
->max
== vr0
->min
)
1886 val
= vrp_int_const_binop (code
, vr0
->max
, vr1
->min
, &sop
);
1889 if (wi::lt_p (val
, min
, sgn
))
1891 else if (wi::gt_p (val
, max
, sgn
))
1896 if (vr0
->min
== vr0
->max
|| vr1
->min
== vr1
->max
)
1900 val
= vrp_int_const_binop (code
, vr0
->max
, vr1
->max
, &sop
);
1903 if (wi::lt_p (val
, min
, sgn
))
1905 else if (wi::gt_p (val
, max
, sgn
))
1910 /* If either operation overflowed, drop to VARYING. */
1913 set_value_range_to_varying (vr
);
1917 /* If the new range has its limits swapped around (MIN > MAX),
1918 then the operation caused one of them to wrap around, mark
1919 the new range VARYING. */
1920 if (wi::gt_p (min
, max
, sgn
))
1922 set_value_range_to_varying (vr
);
1926 /* We punt for [-INF, +INF].
1927 We learn nothing when we have INF on both sides.
1928 Note that we do accept [-INF, -INF] and [+INF, +INF]. */
1929 if (wi::eq_p (min
, wi::min_value (TYPE_PRECISION (type
), sgn
))
1930 && wi::eq_p (max
, wi::max_value (TYPE_PRECISION (type
), sgn
)))
1932 set_value_range_to_varying (vr
);
1936 set_value_range (vr
, rtype
,
1937 wide_int_to_tree (type
, min
),
1938 wide_int_to_tree (type
, max
), NULL
);
1941 /* Extract range information from a binary operation CODE based on
1942 the ranges of each of its operands *VR0 and *VR1 with resulting
1943 type EXPR_TYPE. The resulting range is stored in *VR. */
1946 extract_range_from_binary_expr_1 (value_range
*vr
,
1947 enum tree_code code
, tree expr_type
,
1948 value_range
*vr0_
, value_range
*vr1_
)
1950 value_range vr0
= *vr0_
, vr1
= *vr1_
;
1951 value_range vrtem0
= VR_INITIALIZER
, vrtem1
= VR_INITIALIZER
;
1952 enum value_range_type type
;
1953 tree min
= NULL_TREE
, max
= NULL_TREE
;
1956 if (!INTEGRAL_TYPE_P (expr_type
)
1957 && !POINTER_TYPE_P (expr_type
))
1959 set_value_range_to_varying (vr
);
1963 /* Not all binary expressions can be applied to ranges in a
1964 meaningful way. Handle only arithmetic operations. */
1965 if (code
!= PLUS_EXPR
1966 && code
!= MINUS_EXPR
1967 && code
!= POINTER_PLUS_EXPR
1968 && code
!= MULT_EXPR
1969 && code
!= TRUNC_DIV_EXPR
1970 && code
!= FLOOR_DIV_EXPR
1971 && code
!= CEIL_DIV_EXPR
1972 && code
!= EXACT_DIV_EXPR
1973 && code
!= ROUND_DIV_EXPR
1974 && code
!= TRUNC_MOD_EXPR
1975 && code
!= RSHIFT_EXPR
1976 && code
!= LSHIFT_EXPR
1979 && code
!= BIT_AND_EXPR
1980 && code
!= BIT_IOR_EXPR
1981 && code
!= BIT_XOR_EXPR
)
1983 set_value_range_to_varying (vr
);
1987 /* If both ranges are UNDEFINED, so is the result. */
1988 if (vr0
.type
== VR_UNDEFINED
&& vr1
.type
== VR_UNDEFINED
)
1990 set_value_range_to_undefined (vr
);
1993 /* If one of the ranges is UNDEFINED drop it to VARYING for the following
1994 code. At some point we may want to special-case operations that
1995 have UNDEFINED result for all or some value-ranges of the not UNDEFINED
1997 else if (vr0
.type
== VR_UNDEFINED
)
1998 set_value_range_to_varying (&vr0
);
1999 else if (vr1
.type
== VR_UNDEFINED
)
2000 set_value_range_to_varying (&vr1
);
2002 /* We get imprecise results from ranges_from_anti_range when
2003 code is EXACT_DIV_EXPR. We could mask out bits in the resulting
2004 range, but then we also need to hack up vrp_meet. It's just
2005 easier to special case when vr0 is ~[0,0] for EXACT_DIV_EXPR. */
2006 if (code
== EXACT_DIV_EXPR
2007 && vr0
.type
== VR_ANTI_RANGE
2008 && vr0
.min
== vr0
.max
2009 && integer_zerop (vr0
.min
))
2011 set_value_range_to_nonnull (vr
, expr_type
);
2015 /* Now canonicalize anti-ranges to ranges when they are not symbolic
2016 and express ~[] op X as ([]' op X) U ([]'' op X). */
2017 if (vr0
.type
== VR_ANTI_RANGE
2018 && ranges_from_anti_range (&vr0
, &vrtem0
, &vrtem1
))
2020 extract_range_from_binary_expr_1 (vr
, code
, expr_type
, &vrtem0
, vr1_
);
2021 if (vrtem1
.type
!= VR_UNDEFINED
)
2023 value_range vrres
= VR_INITIALIZER
;
2024 extract_range_from_binary_expr_1 (&vrres
, code
, expr_type
,
2026 vrp_meet (vr
, &vrres
);
2030 /* Likewise for X op ~[]. */
2031 if (vr1
.type
== VR_ANTI_RANGE
2032 && ranges_from_anti_range (&vr1
, &vrtem0
, &vrtem1
))
2034 extract_range_from_binary_expr_1 (vr
, code
, expr_type
, vr0_
, &vrtem0
);
2035 if (vrtem1
.type
!= VR_UNDEFINED
)
2037 value_range vrres
= VR_INITIALIZER
;
2038 extract_range_from_binary_expr_1 (&vrres
, code
, expr_type
,
2040 vrp_meet (vr
, &vrres
);
2045 /* The type of the resulting value range defaults to VR0.TYPE. */
2048 /* Refuse to operate on VARYING ranges, ranges of different kinds
2049 and symbolic ranges. As an exception, we allow BIT_{AND,IOR}
2050 because we may be able to derive a useful range even if one of
2051 the operands is VR_VARYING or symbolic range. Similarly for
2052 divisions, MIN/MAX and PLUS/MINUS.
2054 TODO, we may be able to derive anti-ranges in some cases. */
2055 if (code
!= BIT_AND_EXPR
2056 && code
!= BIT_IOR_EXPR
2057 && code
!= TRUNC_DIV_EXPR
2058 && code
!= FLOOR_DIV_EXPR
2059 && code
!= CEIL_DIV_EXPR
2060 && code
!= EXACT_DIV_EXPR
2061 && code
!= ROUND_DIV_EXPR
2062 && code
!= TRUNC_MOD_EXPR
2065 && code
!= PLUS_EXPR
2066 && code
!= MINUS_EXPR
2067 && code
!= RSHIFT_EXPR
2068 && (vr0
.type
== VR_VARYING
2069 || vr1
.type
== VR_VARYING
2070 || vr0
.type
!= vr1
.type
2071 || symbolic_range_p (&vr0
)
2072 || symbolic_range_p (&vr1
)))
2074 set_value_range_to_varying (vr
);
2078 /* Now evaluate the expression to determine the new range. */
2079 if (POINTER_TYPE_P (expr_type
))
2081 if (code
== MIN_EXPR
|| code
== MAX_EXPR
)
2083 /* For MIN/MAX expressions with pointers, we only care about
2084 nullness, if both are non null, then the result is nonnull.
2085 If both are null, then the result is null. Otherwise they
2087 if (range_is_nonnull (&vr0
) && range_is_nonnull (&vr1
))
2088 set_value_range_to_nonnull (vr
, expr_type
);
2089 else if (range_is_null (&vr0
) && range_is_null (&vr1
))
2090 set_value_range_to_null (vr
, expr_type
);
2092 set_value_range_to_varying (vr
);
2094 else if (code
== POINTER_PLUS_EXPR
)
2096 /* For pointer types, we are really only interested in asserting
2097 whether the expression evaluates to non-NULL. */
2098 if (range_is_nonnull (&vr0
) || range_is_nonnull (&vr1
))
2099 set_value_range_to_nonnull (vr
, expr_type
);
2100 else if (range_is_null (&vr0
) && range_is_null (&vr1
))
2101 set_value_range_to_null (vr
, expr_type
);
2103 set_value_range_to_varying (vr
);
2105 else if (code
== BIT_AND_EXPR
)
2107 /* For pointer types, we are really only interested in asserting
2108 whether the expression evaluates to non-NULL. */
2109 if (range_is_nonnull (&vr0
) && range_is_nonnull (&vr1
))
2110 set_value_range_to_nonnull (vr
, expr_type
);
2111 else if (range_is_null (&vr0
) || range_is_null (&vr1
))
2112 set_value_range_to_null (vr
, expr_type
);
2114 set_value_range_to_varying (vr
);
2117 set_value_range_to_varying (vr
);
2122 /* For integer ranges, apply the operation to each end of the
2123 range and see what we end up with. */
2124 if (code
== PLUS_EXPR
|| code
== MINUS_EXPR
)
2126 const bool minus_p
= (code
== MINUS_EXPR
);
2127 tree min_op0
= vr0
.min
;
2128 tree min_op1
= minus_p
? vr1
.max
: vr1
.min
;
2129 tree max_op0
= vr0
.max
;
2130 tree max_op1
= minus_p
? vr1
.min
: vr1
.max
;
2131 tree sym_min_op0
= NULL_TREE
;
2132 tree sym_min_op1
= NULL_TREE
;
2133 tree sym_max_op0
= NULL_TREE
;
2134 tree sym_max_op1
= NULL_TREE
;
2135 bool neg_min_op0
, neg_min_op1
, neg_max_op0
, neg_max_op1
;
2137 /* If we have a PLUS or MINUS with two VR_RANGEs, either constant or
2138 single-symbolic ranges, try to compute the precise resulting range,
2139 but only if we know that this resulting range will also be constant
2140 or single-symbolic. */
2141 if (vr0
.type
== VR_RANGE
&& vr1
.type
== VR_RANGE
2142 && (TREE_CODE (min_op0
) == INTEGER_CST
2144 = get_single_symbol (min_op0
, &neg_min_op0
, &min_op0
)))
2145 && (TREE_CODE (min_op1
) == INTEGER_CST
2147 = get_single_symbol (min_op1
, &neg_min_op1
, &min_op1
)))
2148 && (!(sym_min_op0
&& sym_min_op1
)
2149 || (sym_min_op0
== sym_min_op1
2150 && neg_min_op0
== (minus_p
? neg_min_op1
: !neg_min_op1
)))
2151 && (TREE_CODE (max_op0
) == INTEGER_CST
2153 = get_single_symbol (max_op0
, &neg_max_op0
, &max_op0
)))
2154 && (TREE_CODE (max_op1
) == INTEGER_CST
2156 = get_single_symbol (max_op1
, &neg_max_op1
, &max_op1
)))
2157 && (!(sym_max_op0
&& sym_max_op1
)
2158 || (sym_max_op0
== sym_max_op1
2159 && neg_max_op0
== (minus_p
? neg_max_op1
: !neg_max_op1
))))
2161 const signop sgn
= TYPE_SIGN (expr_type
);
2162 const unsigned int prec
= TYPE_PRECISION (expr_type
);
2163 wide_int type_min
, type_max
, wmin
, wmax
;
2167 /* Get the lower and upper bounds of the type. */
2168 if (TYPE_OVERFLOW_WRAPS (expr_type
))
2170 type_min
= wi::min_value (prec
, sgn
);
2171 type_max
= wi::max_value (prec
, sgn
);
2175 type_min
= vrp_val_min (expr_type
);
2176 type_max
= vrp_val_max (expr_type
);
2179 /* Combine the lower bounds, if any. */
2180 if (min_op0
&& min_op1
)
2184 wmin
= wi::sub (min_op0
, min_op1
);
2186 /* Check for overflow. */
2187 if (wi::cmp (0, min_op1
, sgn
)
2188 != wi::cmp (wmin
, min_op0
, sgn
))
2189 min_ovf
= wi::cmp (min_op0
, min_op1
, sgn
);
2193 wmin
= wi::add (min_op0
, min_op1
);
2195 /* Check for overflow. */
2196 if (wi::cmp (min_op1
, 0, sgn
)
2197 != wi::cmp (wmin
, min_op0
, sgn
))
2198 min_ovf
= wi::cmp (min_op0
, wmin
, sgn
);
2207 wmin
= wi::neg (min_op1
);
2209 /* Check for overflow. */
2210 if (sgn
== SIGNED
&& wi::neg_p (min_op1
) && wi::neg_p (wmin
))
2212 else if (sgn
== UNSIGNED
&& wi::ne_p (min_op1
, 0))
2219 wmin
= wi::shwi (0, prec
);
2221 /* Combine the upper bounds, if any. */
2222 if (max_op0
&& max_op1
)
2226 wmax
= wi::sub (max_op0
, max_op1
);
2228 /* Check for overflow. */
2229 if (wi::cmp (0, max_op1
, sgn
)
2230 != wi::cmp (wmax
, max_op0
, sgn
))
2231 max_ovf
= wi::cmp (max_op0
, max_op1
, sgn
);
2235 wmax
= wi::add (max_op0
, max_op1
);
2237 if (wi::cmp (max_op1
, 0, sgn
)
2238 != wi::cmp (wmax
, max_op0
, sgn
))
2239 max_ovf
= wi::cmp (max_op0
, wmax
, sgn
);
2248 wmax
= wi::neg (max_op1
);
2250 /* Check for overflow. */
2251 if (sgn
== SIGNED
&& wi::neg_p (max_op1
) && wi::neg_p (wmax
))
2253 else if (sgn
== UNSIGNED
&& wi::ne_p (max_op1
, 0))
2260 wmax
= wi::shwi (0, prec
);
2262 /* Check for type overflow. */
2265 if (wi::cmp (wmin
, type_min
, sgn
) == -1)
2267 else if (wi::cmp (wmin
, type_max
, sgn
) == 1)
2272 if (wi::cmp (wmax
, type_min
, sgn
) == -1)
2274 else if (wi::cmp (wmax
, type_max
, sgn
) == 1)
2278 /* If we have overflow for the constant part and the resulting
2279 range will be symbolic, drop to VR_VARYING. */
2280 if ((min_ovf
&& sym_min_op0
!= sym_min_op1
)
2281 || (max_ovf
&& sym_max_op0
!= sym_max_op1
))
2283 set_value_range_to_varying (vr
);
2287 if (TYPE_OVERFLOW_WRAPS (expr_type
))
2289 /* If overflow wraps, truncate the values and adjust the
2290 range kind and bounds appropriately. */
2291 wide_int tmin
= wide_int::from (wmin
, prec
, sgn
);
2292 wide_int tmax
= wide_int::from (wmax
, prec
, sgn
);
2293 if (min_ovf
== max_ovf
)
2295 /* No overflow or both overflow or underflow. The
2296 range kind stays VR_RANGE. */
2297 min
= wide_int_to_tree (expr_type
, tmin
);
2298 max
= wide_int_to_tree (expr_type
, tmax
);
2300 else if ((min_ovf
== -1 && max_ovf
== 0)
2301 || (max_ovf
== 1 && min_ovf
== 0))
2303 /* Min underflow or max overflow. The range kind
2304 changes to VR_ANTI_RANGE. */
2305 bool covers
= false;
2306 wide_int tem
= tmin
;
2307 type
= VR_ANTI_RANGE
;
2309 if (wi::cmp (tmin
, tmax
, sgn
) < 0)
2312 if (wi::cmp (tmax
, tem
, sgn
) > 0)
2314 /* If the anti-range would cover nothing, drop to varying.
2315 Likewise if the anti-range bounds are outside of the
2317 if (covers
|| wi::cmp (tmin
, tmax
, sgn
) > 0)
2319 set_value_range_to_varying (vr
);
2322 min
= wide_int_to_tree (expr_type
, tmin
);
2323 max
= wide_int_to_tree (expr_type
, tmax
);
2327 /* Other underflow and/or overflow, drop to VR_VARYING. */
2328 set_value_range_to_varying (vr
);
2334 /* If overflow does not wrap, saturate to the types min/max
2337 min
= wide_int_to_tree (expr_type
, type_min
);
2338 else if (min_ovf
== 1)
2339 min
= wide_int_to_tree (expr_type
, type_max
);
2341 min
= wide_int_to_tree (expr_type
, wmin
);
2344 max
= wide_int_to_tree (expr_type
, type_min
);
2345 else if (max_ovf
== 1)
2346 max
= wide_int_to_tree (expr_type
, type_max
);
2348 max
= wide_int_to_tree (expr_type
, wmax
);
2351 /* If the result lower bound is constant, we're done;
2352 otherwise, build the symbolic lower bound. */
2353 if (sym_min_op0
== sym_min_op1
)
2355 else if (sym_min_op0
)
2356 min
= build_symbolic_expr (expr_type
, sym_min_op0
,
2358 else if (sym_min_op1
)
2360 /* We may not negate if that might introduce
2361 undefined overflow. */
2364 || TYPE_OVERFLOW_WRAPS (expr_type
))
2365 min
= build_symbolic_expr (expr_type
, sym_min_op1
,
2366 neg_min_op1
^ minus_p
, min
);
2371 /* Likewise for the upper bound. */
2372 if (sym_max_op0
== sym_max_op1
)
2374 else if (sym_max_op0
)
2375 max
= build_symbolic_expr (expr_type
, sym_max_op0
,
2377 else if (sym_max_op1
)
2379 /* We may not negate if that might introduce
2380 undefined overflow. */
2383 || TYPE_OVERFLOW_WRAPS (expr_type
))
2384 max
= build_symbolic_expr (expr_type
, sym_max_op1
,
2385 neg_max_op1
^ minus_p
, max
);
2392 /* For other cases, for example if we have a PLUS_EXPR with two
2393 VR_ANTI_RANGEs, drop to VR_VARYING. It would take more effort
2394 to compute a precise range for such a case.
2395 ??? General even mixed range kind operations can be expressed
2396 by for example transforming ~[3, 5] + [1, 2] to range-only
2397 operations and a union primitive:
2398 [-INF, 2] + [1, 2] U [5, +INF] + [1, 2]
2399 [-INF+1, 4] U [6, +INF(OVF)]
2400 though usually the union is not exactly representable with
2401 a single range or anti-range as the above is
2402 [-INF+1, +INF(OVF)] intersected with ~[5, 5]
2403 but one could use a scheme similar to equivalences for this. */
2404 set_value_range_to_varying (vr
);
2408 else if (code
== MIN_EXPR
2409 || code
== MAX_EXPR
)
2411 if (vr0
.type
== VR_RANGE
2412 && !symbolic_range_p (&vr0
))
2415 if (vr1
.type
== VR_RANGE
2416 && !symbolic_range_p (&vr1
))
2418 /* For operations that make the resulting range directly
2419 proportional to the original ranges, apply the operation to
2420 the same end of each range. */
2421 min
= int_const_binop (code
, vr0
.min
, vr1
.min
);
2422 max
= int_const_binop (code
, vr0
.max
, vr1
.max
);
2424 else if (code
== MIN_EXPR
)
2426 min
= vrp_val_min (expr_type
);
2429 else if (code
== MAX_EXPR
)
2432 max
= vrp_val_max (expr_type
);
2435 else if (vr1
.type
== VR_RANGE
2436 && !symbolic_range_p (&vr1
))
2439 if (code
== MIN_EXPR
)
2441 min
= vrp_val_min (expr_type
);
2444 else if (code
== MAX_EXPR
)
2447 max
= vrp_val_max (expr_type
);
2452 set_value_range_to_varying (vr
);
2456 else if (code
== MULT_EXPR
)
2458 /* Fancy code so that with unsigned, [-3,-1]*[-3,-1] does not
2459 drop to varying. This test requires 2*prec bits if both
2460 operands are signed and 2*prec + 2 bits if either is not. */
2462 signop sign
= TYPE_SIGN (expr_type
);
2463 unsigned int prec
= TYPE_PRECISION (expr_type
);
2465 if (range_int_cst_p (&vr0
)
2466 && range_int_cst_p (&vr1
)
2467 && TYPE_OVERFLOW_WRAPS (expr_type
))
2469 typedef FIXED_WIDE_INT (WIDE_INT_MAX_PRECISION
* 2) vrp_int
;
2470 typedef generic_wide_int
2471 <wi::extended_tree
<WIDE_INT_MAX_PRECISION
* 2> > vrp_int_cst
;
2472 vrp_int sizem1
= wi::mask
<vrp_int
> (prec
, false);
2473 vrp_int size
= sizem1
+ 1;
2475 /* Extend the values using the sign of the result to PREC2.
2476 From here on out, everthing is just signed math no matter
2477 what the input types were. */
2478 vrp_int min0
= vrp_int_cst (vr0
.min
);
2479 vrp_int max0
= vrp_int_cst (vr0
.max
);
2480 vrp_int min1
= vrp_int_cst (vr1
.min
);
2481 vrp_int max1
= vrp_int_cst (vr1
.max
);
2482 /* Canonicalize the intervals. */
2483 if (sign
== UNSIGNED
)
2485 if (wi::ltu_p (size
, min0
+ max0
))
2491 if (wi::ltu_p (size
, min1
+ max1
))
2498 vrp_int prod0
= min0
* min1
;
2499 vrp_int prod1
= min0
* max1
;
2500 vrp_int prod2
= max0
* min1
;
2501 vrp_int prod3
= max0
* max1
;
2503 /* Sort the 4 products so that min is in prod0 and max is in
2505 /* min0min1 > max0max1 */
2507 std::swap (prod0
, prod3
);
2509 /* min0max1 > max0min1 */
2511 std::swap (prod1
, prod2
);
2514 std::swap (prod0
, prod1
);
2517 std::swap (prod2
, prod3
);
2519 /* diff = max - min. */
2520 prod2
= prod3
- prod0
;
2521 if (wi::geu_p (prod2
, sizem1
))
2523 /* the range covers all values. */
2524 set_value_range_to_varying (vr
);
2528 /* The following should handle the wrapping and selecting
2529 VR_ANTI_RANGE for us. */
2530 min
= wide_int_to_tree (expr_type
, prod0
);
2531 max
= wide_int_to_tree (expr_type
, prod3
);
2532 set_and_canonicalize_value_range (vr
, VR_RANGE
, min
, max
, NULL
);
2536 /* If we have an unsigned MULT_EXPR with two VR_ANTI_RANGEs,
2537 drop to VR_VARYING. It would take more effort to compute a
2538 precise range for such a case. For example, if we have
2539 op0 == 65536 and op1 == 65536 with their ranges both being
2540 ~[0,0] on a 32-bit machine, we would have op0 * op1 == 0, so
2541 we cannot claim that the product is in ~[0,0]. Note that we
2542 are guaranteed to have vr0.type == vr1.type at this
2544 if (vr0
.type
== VR_ANTI_RANGE
2545 && !TYPE_OVERFLOW_UNDEFINED (expr_type
))
2547 set_value_range_to_varying (vr
);
2551 extract_range_from_multiplicative_op_1 (vr
, code
, &vr0
, &vr1
);
2554 else if (code
== RSHIFT_EXPR
2555 || code
== LSHIFT_EXPR
)
2557 /* If we have a RSHIFT_EXPR with any shift values outside [0..prec-1],
2558 then drop to VR_VARYING. Outside of this range we get undefined
2559 behavior from the shift operation. We cannot even trust
2560 SHIFT_COUNT_TRUNCATED at this stage, because that applies to rtl
2561 shifts, and the operation at the tree level may be widened. */
2562 if (range_int_cst_p (&vr1
)
2563 && compare_tree_int (vr1
.min
, 0) >= 0
2564 && compare_tree_int (vr1
.max
, TYPE_PRECISION (expr_type
)) == -1)
2566 if (code
== RSHIFT_EXPR
)
2568 /* Even if vr0 is VARYING or otherwise not usable, we can derive
2569 useful ranges just from the shift count. E.g.
2570 x >> 63 for signed 64-bit x is always [-1, 0]. */
2571 if (vr0
.type
!= VR_RANGE
|| symbolic_range_p (&vr0
))
2573 vr0
.type
= type
= VR_RANGE
;
2574 vr0
.min
= vrp_val_min (expr_type
);
2575 vr0
.max
= vrp_val_max (expr_type
);
2577 extract_range_from_multiplicative_op_1 (vr
, code
, &vr0
, &vr1
);
2580 /* We can map lshifts by constants to MULT_EXPR handling. */
2581 else if (code
== LSHIFT_EXPR
2582 && range_int_cst_singleton_p (&vr1
))
2584 bool saved_flag_wrapv
;
2585 value_range vr1p
= VR_INITIALIZER
;
2586 vr1p
.type
= VR_RANGE
;
2587 vr1p
.min
= (wide_int_to_tree
2589 wi::set_bit_in_zero (tree_to_shwi (vr1
.min
),
2590 TYPE_PRECISION (expr_type
))));
2591 vr1p
.max
= vr1p
.min
;
2592 /* We have to use a wrapping multiply though as signed overflow
2593 on lshifts is implementation defined in C89. */
2594 saved_flag_wrapv
= flag_wrapv
;
2596 extract_range_from_binary_expr_1 (vr
, MULT_EXPR
, expr_type
,
2598 flag_wrapv
= saved_flag_wrapv
;
2601 else if (code
== LSHIFT_EXPR
2602 && range_int_cst_p (&vr0
))
2604 int prec
= TYPE_PRECISION (expr_type
);
2605 int overflow_pos
= prec
;
2607 wide_int low_bound
, high_bound
;
2608 bool uns
= TYPE_UNSIGNED (expr_type
);
2609 bool in_bounds
= false;
2614 bound_shift
= overflow_pos
- tree_to_shwi (vr1
.max
);
2615 /* If bound_shift == HOST_BITS_PER_WIDE_INT, the llshift can
2616 overflow. However, for that to happen, vr1.max needs to be
2617 zero, which means vr1 is a singleton range of zero, which
2618 means it should be handled by the previous LSHIFT_EXPR
2620 wide_int bound
= wi::set_bit_in_zero (bound_shift
, prec
);
2621 wide_int complement
= ~(bound
- 1);
2626 high_bound
= complement
;
2627 if (wi::ltu_p (vr0
.max
, low_bound
))
2629 /* [5, 6] << [1, 2] == [10, 24]. */
2630 /* We're shifting out only zeroes, the value increases
2634 else if (wi::ltu_p (high_bound
, vr0
.min
))
2636 /* [0xffffff00, 0xffffffff] << [1, 2]
2637 == [0xfffffc00, 0xfffffffe]. */
2638 /* We're shifting out only ones, the value decreases
2645 /* [-1, 1] << [1, 2] == [-4, 4]. */
2646 low_bound
= complement
;
2648 if (wi::lts_p (vr0
.max
, high_bound
)
2649 && wi::lts_p (low_bound
, vr0
.min
))
2651 /* For non-negative numbers, we're shifting out only
2652 zeroes, the value increases monotonically.
2653 For negative numbers, we're shifting out only ones, the
2654 value decreases monotomically. */
2661 extract_range_from_multiplicative_op_1 (vr
, code
, &vr0
, &vr1
);
2666 set_value_range_to_varying (vr
);
2669 else if (code
== TRUNC_DIV_EXPR
2670 || code
== FLOOR_DIV_EXPR
2671 || code
== CEIL_DIV_EXPR
2672 || code
== EXACT_DIV_EXPR
2673 || code
== ROUND_DIV_EXPR
)
2675 if (vr0
.type
!= VR_RANGE
|| symbolic_range_p (&vr0
))
2677 /* For division, if op1 has VR_RANGE but op0 does not, something
2678 can be deduced just from that range. Say [min, max] / [4, max]
2679 gives [min / 4, max / 4] range. */
2680 if (vr1
.type
== VR_RANGE
2681 && !symbolic_range_p (&vr1
)
2682 && range_includes_zero_p (vr1
.min
, vr1
.max
) == 0)
2684 vr0
.type
= type
= VR_RANGE
;
2685 vr0
.min
= vrp_val_min (expr_type
);
2686 vr0
.max
= vrp_val_max (expr_type
);
2690 set_value_range_to_varying (vr
);
2695 /* For divisions, if flag_non_call_exceptions is true, we must
2696 not eliminate a division by zero. */
2697 if (cfun
->can_throw_non_call_exceptions
2698 && (vr1
.type
!= VR_RANGE
2699 || range_includes_zero_p (vr1
.min
, vr1
.max
) != 0))
2701 set_value_range_to_varying (vr
);
2705 /* For divisions, if op0 is VR_RANGE, we can deduce a range
2706 even if op1 is VR_VARYING, VR_ANTI_RANGE, symbolic or can
2708 if (vr0
.type
== VR_RANGE
2709 && (vr1
.type
!= VR_RANGE
2710 || range_includes_zero_p (vr1
.min
, vr1
.max
) != 0))
2712 tree zero
= build_int_cst (TREE_TYPE (vr0
.min
), 0);
2717 if (TYPE_UNSIGNED (expr_type
)
2718 || value_range_nonnegative_p (&vr1
))
2720 /* For unsigned division or when divisor is known
2721 to be non-negative, the range has to cover
2722 all numbers from 0 to max for positive max
2723 and all numbers from min to 0 for negative min. */
2724 cmp
= compare_values (vr0
.max
, zero
);
2727 /* When vr0.max < 0, vr1.min != 0 and value
2728 ranges for dividend and divisor are available. */
2729 if (vr1
.type
== VR_RANGE
2730 && !symbolic_range_p (&vr0
)
2731 && !symbolic_range_p (&vr1
)
2732 && compare_values (vr1
.min
, zero
) != 0)
2733 max
= int_const_binop (code
, vr0
.max
, vr1
.min
);
2737 else if (cmp
== 0 || cmp
== 1)
2741 cmp
= compare_values (vr0
.min
, zero
);
2744 /* For unsigned division when value ranges for dividend
2745 and divisor are available. */
2746 if (vr1
.type
== VR_RANGE
2747 && !symbolic_range_p (&vr0
)
2748 && !symbolic_range_p (&vr1
)
2749 && compare_values (vr1
.max
, zero
) != 0)
2750 min
= int_const_binop (code
, vr0
.min
, vr1
.max
);
2754 else if (cmp
== 0 || cmp
== -1)
2761 /* Otherwise the range is -max .. max or min .. -min
2762 depending on which bound is bigger in absolute value,
2763 as the division can change the sign. */
2764 abs_extent_range (vr
, vr0
.min
, vr0
.max
);
2767 if (type
== VR_VARYING
)
2769 set_value_range_to_varying (vr
);
2773 else if (!symbolic_range_p (&vr0
) && !symbolic_range_p (&vr1
))
2775 extract_range_from_multiplicative_op_1 (vr
, code
, &vr0
, &vr1
);
2779 else if (code
== TRUNC_MOD_EXPR
)
2781 if (range_is_null (&vr1
))
2783 set_value_range_to_undefined (vr
);
2786 /* ABS (A % B) < ABS (B) and either
2787 0 <= A % B <= A or A <= A % B <= 0. */
2789 signop sgn
= TYPE_SIGN (expr_type
);
2790 unsigned int prec
= TYPE_PRECISION (expr_type
);
2791 wide_int wmin
, wmax
, tmp
;
2792 wide_int zero
= wi::zero (prec
);
2793 wide_int one
= wi::one (prec
);
2794 if (vr1
.type
== VR_RANGE
&& !symbolic_range_p (&vr1
))
2796 wmax
= wi::sub (vr1
.max
, one
);
2799 tmp
= wi::sub (wi::minus_one (prec
), vr1
.min
);
2800 wmax
= wi::smax (wmax
, tmp
);
2805 wmax
= wi::max_value (prec
, sgn
);
2806 /* X % INT_MIN may be INT_MAX. */
2807 if (sgn
== UNSIGNED
)
2811 if (sgn
== UNSIGNED
)
2816 if (vr0
.type
== VR_RANGE
&& TREE_CODE (vr0
.min
) == INTEGER_CST
)
2819 if (wi::gts_p (tmp
, zero
))
2821 wmin
= wi::smax (wmin
, tmp
);
2825 if (vr0
.type
== VR_RANGE
&& TREE_CODE (vr0
.max
) == INTEGER_CST
)
2828 if (sgn
== SIGNED
&& wi::neg_p (tmp
))
2830 wmax
= wi::min (wmax
, tmp
, sgn
);
2833 min
= wide_int_to_tree (expr_type
, wmin
);
2834 max
= wide_int_to_tree (expr_type
, wmax
);
2836 else if (code
== BIT_AND_EXPR
|| code
== BIT_IOR_EXPR
|| code
== BIT_XOR_EXPR
)
2838 bool int_cst_range0
, int_cst_range1
;
2839 wide_int may_be_nonzero0
, may_be_nonzero1
;
2840 wide_int must_be_nonzero0
, must_be_nonzero1
;
2842 int_cst_range0
= zero_nonzero_bits_from_vr (expr_type
, &vr0
,
2845 int_cst_range1
= zero_nonzero_bits_from_vr (expr_type
, &vr1
,
2849 if (code
== BIT_AND_EXPR
|| code
== BIT_IOR_EXPR
)
2851 value_range
*vr0p
= NULL
, *vr1p
= NULL
;
2852 if (range_int_cst_singleton_p (&vr1
))
2857 else if (range_int_cst_singleton_p (&vr0
))
2862 /* For op & or | attempt to optimize:
2863 [x, y] op z into [x op z, y op z]
2864 if z is a constant which (for op | its bitwise not) has n
2865 consecutive least significant bits cleared followed by m 1
2866 consecutive bits set immediately above it and either
2867 m + n == precision, or (x >> (m + n)) == (y >> (m + n)).
2868 The least significant n bits of all the values in the range are
2869 cleared or set, the m bits above it are preserved and any bits
2870 above these are required to be the same for all values in the
2872 if (vr0p
&& range_int_cst_p (vr0p
))
2874 wide_int w
= vr1p
->min
;
2876 if (code
== BIT_IOR_EXPR
)
2878 if (wi::eq_p (w
, 0))
2879 n
= TYPE_PRECISION (expr_type
);
2883 w
= ~(w
| wi::mask (n
, false, w
.get_precision ()));
2884 if (wi::eq_p (w
, 0))
2885 m
= TYPE_PRECISION (expr_type
) - n
;
2887 m
= wi::ctz (w
) - n
;
2889 wide_int mask
= wi::mask (m
+ n
, true, w
.get_precision ());
2890 if (wi::eq_p (mask
& vr0p
->min
, mask
& vr0p
->max
))
2892 min
= int_const_binop (code
, vr0p
->min
, vr1p
->min
);
2893 max
= int_const_binop (code
, vr0p
->max
, vr1p
->min
);
2900 /* Optimized above already. */;
2901 else if (code
== BIT_AND_EXPR
)
2903 min
= wide_int_to_tree (expr_type
,
2904 must_be_nonzero0
& must_be_nonzero1
);
2905 wide_int wmax
= may_be_nonzero0
& may_be_nonzero1
;
2906 /* If both input ranges contain only negative values we can
2907 truncate the result range maximum to the minimum of the
2908 input range maxima. */
2909 if (int_cst_range0
&& int_cst_range1
2910 && tree_int_cst_sgn (vr0
.max
) < 0
2911 && tree_int_cst_sgn (vr1
.max
) < 0)
2913 wmax
= wi::min (wmax
, vr0
.max
, TYPE_SIGN (expr_type
));
2914 wmax
= wi::min (wmax
, vr1
.max
, TYPE_SIGN (expr_type
));
2916 /* If either input range contains only non-negative values
2917 we can truncate the result range maximum to the respective
2918 maximum of the input range. */
2919 if (int_cst_range0
&& tree_int_cst_sgn (vr0
.min
) >= 0)
2920 wmax
= wi::min (wmax
, vr0
.max
, TYPE_SIGN (expr_type
));
2921 if (int_cst_range1
&& tree_int_cst_sgn (vr1
.min
) >= 0)
2922 wmax
= wi::min (wmax
, vr1
.max
, TYPE_SIGN (expr_type
));
2923 max
= wide_int_to_tree (expr_type
, wmax
);
2924 cmp
= compare_values (min
, max
);
2925 /* PR68217: In case of signed & sign-bit-CST should
2926 result in [-INF, 0] instead of [-INF, INF]. */
2927 if (cmp
== -2 || cmp
== 1)
2930 = wi::set_bit_in_zero (TYPE_PRECISION (expr_type
) - 1,
2931 TYPE_PRECISION (expr_type
));
2932 if (!TYPE_UNSIGNED (expr_type
)
2933 && ((value_range_constant_singleton (&vr0
)
2934 && !wi::cmps (vr0
.min
, sign_bit
))
2935 || (value_range_constant_singleton (&vr1
)
2936 && !wi::cmps (vr1
.min
, sign_bit
))))
2938 min
= TYPE_MIN_VALUE (expr_type
);
2939 max
= build_int_cst (expr_type
, 0);
2943 else if (code
== BIT_IOR_EXPR
)
2945 max
= wide_int_to_tree (expr_type
,
2946 may_be_nonzero0
| may_be_nonzero1
);
2947 wide_int wmin
= must_be_nonzero0
| must_be_nonzero1
;
2948 /* If the input ranges contain only positive values we can
2949 truncate the minimum of the result range to the maximum
2950 of the input range minima. */
2951 if (int_cst_range0
&& int_cst_range1
2952 && tree_int_cst_sgn (vr0
.min
) >= 0
2953 && tree_int_cst_sgn (vr1
.min
) >= 0)
2955 wmin
= wi::max (wmin
, vr0
.min
, TYPE_SIGN (expr_type
));
2956 wmin
= wi::max (wmin
, vr1
.min
, TYPE_SIGN (expr_type
));
2958 /* If either input range contains only negative values
2959 we can truncate the minimum of the result range to the
2960 respective minimum range. */
2961 if (int_cst_range0
&& tree_int_cst_sgn (vr0
.max
) < 0)
2962 wmin
= wi::max (wmin
, vr0
.min
, TYPE_SIGN (expr_type
));
2963 if (int_cst_range1
&& tree_int_cst_sgn (vr1
.max
) < 0)
2964 wmin
= wi::max (wmin
, vr1
.min
, TYPE_SIGN (expr_type
));
2965 min
= wide_int_to_tree (expr_type
, wmin
);
2967 else if (code
== BIT_XOR_EXPR
)
2969 wide_int result_zero_bits
= ((must_be_nonzero0
& must_be_nonzero1
)
2970 | ~(may_be_nonzero0
| may_be_nonzero1
));
2971 wide_int result_one_bits
2972 = (must_be_nonzero0
.and_not (may_be_nonzero1
)
2973 | must_be_nonzero1
.and_not (may_be_nonzero0
));
2974 max
= wide_int_to_tree (expr_type
, ~result_zero_bits
);
2975 min
= wide_int_to_tree (expr_type
, result_one_bits
);
2976 /* If the range has all positive or all negative values the
2977 result is better than VARYING. */
2978 if (tree_int_cst_sgn (min
) < 0
2979 || tree_int_cst_sgn (max
) >= 0)
2982 max
= min
= NULL_TREE
;
2988 /* If either MIN or MAX overflowed, then set the resulting range to
2990 if (min
== NULL_TREE
2991 || TREE_OVERFLOW_P (min
)
2993 || TREE_OVERFLOW_P (max
))
2995 set_value_range_to_varying (vr
);
2999 /* We punt for [-INF, +INF].
3000 We learn nothing when we have INF on both sides.
3001 Note that we do accept [-INF, -INF] and [+INF, +INF]. */
3002 if (vrp_val_is_min (min
) && vrp_val_is_max (max
))
3004 set_value_range_to_varying (vr
);
3008 cmp
= compare_values (min
, max
);
3009 if (cmp
== -2 || cmp
== 1)
3011 /* If the new range has its limits swapped around (MIN > MAX),
3012 then the operation caused one of them to wrap around, mark
3013 the new range VARYING. */
3014 set_value_range_to_varying (vr
);
3017 set_value_range (vr
, type
, min
, max
, NULL
);
3020 /* Extract range information from a binary expression OP0 CODE OP1 based on
3021 the ranges of each of its operands with resulting type EXPR_TYPE.
3022 The resulting range is stored in *VR. */
3025 extract_range_from_binary_expr (value_range
*vr
,
3026 enum tree_code code
,
3027 tree expr_type
, tree op0
, tree op1
)
3029 value_range vr0
= VR_INITIALIZER
;
3030 value_range vr1
= VR_INITIALIZER
;
3032 /* Get value ranges for each operand. For constant operands, create
3033 a new value range with the operand to simplify processing. */
3034 if (TREE_CODE (op0
) == SSA_NAME
)
3035 vr0
= *(get_value_range (op0
));
3036 else if (is_gimple_min_invariant (op0
))
3037 set_value_range_to_value (&vr0
, op0
, NULL
);
3039 set_value_range_to_varying (&vr0
);
3041 if (TREE_CODE (op1
) == SSA_NAME
)
3042 vr1
= *(get_value_range (op1
));
3043 else if (is_gimple_min_invariant (op1
))
3044 set_value_range_to_value (&vr1
, op1
, NULL
);
3046 set_value_range_to_varying (&vr1
);
3048 extract_range_from_binary_expr_1 (vr
, code
, expr_type
, &vr0
, &vr1
);
3050 /* Try harder for PLUS and MINUS if the range of one operand is symbolic
3051 and based on the other operand, for example if it was deduced from a
3052 symbolic comparison. When a bound of the range of the first operand
3053 is invariant, we set the corresponding bound of the new range to INF
3054 in order to avoid recursing on the range of the second operand. */
3055 if (vr
->type
== VR_VARYING
3056 && (code
== PLUS_EXPR
|| code
== MINUS_EXPR
)
3057 && TREE_CODE (op1
) == SSA_NAME
3058 && vr0
.type
== VR_RANGE
3059 && symbolic_range_based_on_p (&vr0
, op1
))
3061 const bool minus_p
= (code
== MINUS_EXPR
);
3062 value_range n_vr1
= VR_INITIALIZER
;
3064 /* Try with VR0 and [-INF, OP1]. */
3065 if (is_gimple_min_invariant (minus_p
? vr0
.max
: vr0
.min
))
3066 set_value_range (&n_vr1
, VR_RANGE
, vrp_val_min (expr_type
), op1
, NULL
);
3068 /* Try with VR0 and [OP1, +INF]. */
3069 else if (is_gimple_min_invariant (minus_p
? vr0
.min
: vr0
.max
))
3070 set_value_range (&n_vr1
, VR_RANGE
, op1
, vrp_val_max (expr_type
), NULL
);
3072 /* Try with VR0 and [OP1, OP1]. */
3074 set_value_range (&n_vr1
, VR_RANGE
, op1
, op1
, NULL
);
3076 extract_range_from_binary_expr_1 (vr
, code
, expr_type
, &vr0
, &n_vr1
);
3079 if (vr
->type
== VR_VARYING
3080 && (code
== PLUS_EXPR
|| code
== MINUS_EXPR
)
3081 && TREE_CODE (op0
) == SSA_NAME
3082 && vr1
.type
== VR_RANGE
3083 && symbolic_range_based_on_p (&vr1
, op0
))
3085 const bool minus_p
= (code
== MINUS_EXPR
);
3086 value_range n_vr0
= VR_INITIALIZER
;
3088 /* Try with [-INF, OP0] and VR1. */
3089 if (is_gimple_min_invariant (minus_p
? vr1
.max
: vr1
.min
))
3090 set_value_range (&n_vr0
, VR_RANGE
, vrp_val_min (expr_type
), op0
, NULL
);
3092 /* Try with [OP0, +INF] and VR1. */
3093 else if (is_gimple_min_invariant (minus_p
? vr1
.min
: vr1
.max
))
3094 set_value_range (&n_vr0
, VR_RANGE
, op0
, vrp_val_max (expr_type
), NULL
);
3096 /* Try with [OP0, OP0] and VR1. */
3098 set_value_range (&n_vr0
, VR_RANGE
, op0
, op0
, NULL
);
3100 extract_range_from_binary_expr_1 (vr
, code
, expr_type
, &n_vr0
, &vr1
);
3103 /* If we didn't derive a range for MINUS_EXPR, and
3104 op1's range is ~[op0,op0] or vice-versa, then we
3105 can derive a non-null range. This happens often for
3106 pointer subtraction. */
3107 if (vr
->type
== VR_VARYING
3108 && code
== MINUS_EXPR
3109 && TREE_CODE (op0
) == SSA_NAME
3110 && ((vr0
.type
== VR_ANTI_RANGE
3112 && vr0
.min
== vr0
.max
)
3113 || (vr1
.type
== VR_ANTI_RANGE
3115 && vr1
.min
== vr1
.max
)))
3116 set_value_range_to_nonnull (vr
, TREE_TYPE (op0
));
3119 /* Extract range information from a unary operation CODE based on
3120 the range of its operand *VR0 with type OP0_TYPE with resulting type TYPE.
3121 The resulting range is stored in *VR. */
3124 extract_range_from_unary_expr (value_range
*vr
,
3125 enum tree_code code
, tree type
,
3126 value_range
*vr0_
, tree op0_type
)
3128 value_range vr0
= *vr0_
, vrtem0
= VR_INITIALIZER
, vrtem1
= VR_INITIALIZER
;
3130 /* VRP only operates on integral and pointer types. */
3131 if (!(INTEGRAL_TYPE_P (op0_type
)
3132 || POINTER_TYPE_P (op0_type
))
3133 || !(INTEGRAL_TYPE_P (type
)
3134 || POINTER_TYPE_P (type
)))
3136 set_value_range_to_varying (vr
);
3140 /* If VR0 is UNDEFINED, so is the result. */
3141 if (vr0
.type
== VR_UNDEFINED
)
3143 set_value_range_to_undefined (vr
);
3147 /* Handle operations that we express in terms of others. */
3148 if (code
== PAREN_EXPR
|| code
== OBJ_TYPE_REF
)
3150 /* PAREN_EXPR and OBJ_TYPE_REF are simple copies. */
3151 copy_value_range (vr
, &vr0
);
3154 else if (code
== NEGATE_EXPR
)
3156 /* -X is simply 0 - X, so re-use existing code that also handles
3157 anti-ranges fine. */
3158 value_range zero
= VR_INITIALIZER
;
3159 set_value_range_to_value (&zero
, build_int_cst (type
, 0), NULL
);
3160 extract_range_from_binary_expr_1 (vr
, MINUS_EXPR
, type
, &zero
, &vr0
);
3163 else if (code
== BIT_NOT_EXPR
)
3165 /* ~X is simply -1 - X, so re-use existing code that also handles
3166 anti-ranges fine. */
3167 value_range minusone
= VR_INITIALIZER
;
3168 set_value_range_to_value (&minusone
, build_int_cst (type
, -1), NULL
);
3169 extract_range_from_binary_expr_1 (vr
, MINUS_EXPR
,
3170 type
, &minusone
, &vr0
);
3174 /* Now canonicalize anti-ranges to ranges when they are not symbolic
3175 and express op ~[] as (op []') U (op []''). */
3176 if (vr0
.type
== VR_ANTI_RANGE
3177 && ranges_from_anti_range (&vr0
, &vrtem0
, &vrtem1
))
3179 extract_range_from_unary_expr (vr
, code
, type
, &vrtem0
, op0_type
);
3180 if (vrtem1
.type
!= VR_UNDEFINED
)
3182 value_range vrres
= VR_INITIALIZER
;
3183 extract_range_from_unary_expr (&vrres
, code
, type
,
3185 vrp_meet (vr
, &vrres
);
3190 if (CONVERT_EXPR_CODE_P (code
))
3192 tree inner_type
= op0_type
;
3193 tree outer_type
= type
;
3195 /* If the expression evaluates to a pointer, we are only interested in
3196 determining if it evaluates to NULL [0, 0] or non-NULL (~[0, 0]). */
3197 if (POINTER_TYPE_P (type
))
3199 if (range_is_nonnull (&vr0
))
3200 set_value_range_to_nonnull (vr
, type
);
3201 else if (range_is_null (&vr0
))
3202 set_value_range_to_null (vr
, type
);
3204 set_value_range_to_varying (vr
);
3208 /* If VR0 is varying and we increase the type precision, assume
3209 a full range for the following transformation. */
3210 if (vr0
.type
== VR_VARYING
3211 && INTEGRAL_TYPE_P (inner_type
)
3212 && TYPE_PRECISION (inner_type
) < TYPE_PRECISION (outer_type
))
3214 vr0
.type
= VR_RANGE
;
3215 vr0
.min
= TYPE_MIN_VALUE (inner_type
);
3216 vr0
.max
= TYPE_MAX_VALUE (inner_type
);
3219 /* If VR0 is a constant range or anti-range and the conversion is
3220 not truncating we can convert the min and max values and
3221 canonicalize the resulting range. Otherwise we can do the
3222 conversion if the size of the range is less than what the
3223 precision of the target type can represent and the range is
3224 not an anti-range. */
3225 if ((vr0
.type
== VR_RANGE
3226 || vr0
.type
== VR_ANTI_RANGE
)
3227 && TREE_CODE (vr0
.min
) == INTEGER_CST
3228 && TREE_CODE (vr0
.max
) == INTEGER_CST
3229 && (TYPE_PRECISION (outer_type
) >= TYPE_PRECISION (inner_type
)
3230 || (vr0
.type
== VR_RANGE
3231 && integer_zerop (int_const_binop (RSHIFT_EXPR
,
3232 int_const_binop (MINUS_EXPR
, vr0
.max
, vr0
.min
),
3233 size_int (TYPE_PRECISION (outer_type
)))))))
3235 tree new_min
, new_max
;
3236 new_min
= force_fit_type (outer_type
, wi::to_widest (vr0
.min
),
3238 new_max
= force_fit_type (outer_type
, wi::to_widest (vr0
.max
),
3240 set_and_canonicalize_value_range (vr
, vr0
.type
,
3241 new_min
, new_max
, NULL
);
3245 set_value_range_to_varying (vr
);
3248 else if (code
== ABS_EXPR
)
3253 /* Pass through vr0 in the easy cases. */
3254 if (TYPE_UNSIGNED (type
)
3255 || value_range_nonnegative_p (&vr0
))
3257 copy_value_range (vr
, &vr0
);
3261 /* For the remaining varying or symbolic ranges we can't do anything
3263 if (vr0
.type
== VR_VARYING
3264 || symbolic_range_p (&vr0
))
3266 set_value_range_to_varying (vr
);
3270 /* -TYPE_MIN_VALUE = TYPE_MIN_VALUE with flag_wrapv so we can't get a
3272 if (!TYPE_OVERFLOW_UNDEFINED (type
)
3273 && ((vr0
.type
== VR_RANGE
3274 && vrp_val_is_min (vr0
.min
))
3275 || (vr0
.type
== VR_ANTI_RANGE
3276 && !vrp_val_is_min (vr0
.min
))))
3278 set_value_range_to_varying (vr
);
3282 /* ABS_EXPR may flip the range around, if the original range
3283 included negative values. */
3284 if (!vrp_val_is_min (vr0
.min
))
3285 min
= fold_unary_to_constant (code
, type
, vr0
.min
);
3287 min
= TYPE_MAX_VALUE (type
);
3289 if (!vrp_val_is_min (vr0
.max
))
3290 max
= fold_unary_to_constant (code
, type
, vr0
.max
);
3292 max
= TYPE_MAX_VALUE (type
);
3294 cmp
= compare_values (min
, max
);
3296 /* If a VR_ANTI_RANGEs contains zero, then we have
3297 ~[-INF, min(MIN, MAX)]. */
3298 if (vr0
.type
== VR_ANTI_RANGE
)
3300 if (range_includes_zero_p (vr0
.min
, vr0
.max
) == 1)
3302 /* Take the lower of the two values. */
3306 /* Create ~[-INF, min (abs(MIN), abs(MAX))]
3307 or ~[-INF + 1, min (abs(MIN), abs(MAX))] when
3308 flag_wrapv is set and the original anti-range doesn't include
3309 TYPE_MIN_VALUE, remember -TYPE_MIN_VALUE = TYPE_MIN_VALUE. */
3310 if (TYPE_OVERFLOW_WRAPS (type
))
3312 tree type_min_value
= TYPE_MIN_VALUE (type
);
3314 min
= (vr0
.min
!= type_min_value
3315 ? int_const_binop (PLUS_EXPR
, type_min_value
,
3316 build_int_cst (TREE_TYPE (type_min_value
), 1))
3320 min
= TYPE_MIN_VALUE (type
);
3324 /* All else has failed, so create the range [0, INF], even for
3325 flag_wrapv since TYPE_MIN_VALUE is in the original
3327 vr0
.type
= VR_RANGE
;
3328 min
= build_int_cst (type
, 0);
3329 max
= TYPE_MAX_VALUE (type
);
3333 /* If the range contains zero then we know that the minimum value in the
3334 range will be zero. */
3335 else if (range_includes_zero_p (vr0
.min
, vr0
.max
) == 1)
3339 min
= build_int_cst (type
, 0);
3343 /* If the range was reversed, swap MIN and MAX. */
3345 std::swap (min
, max
);
3348 cmp
= compare_values (min
, max
);
3349 if (cmp
== -2 || cmp
== 1)
3351 /* If the new range has its limits swapped around (MIN > MAX),
3352 then the operation caused one of them to wrap around, mark
3353 the new range VARYING. */
3354 set_value_range_to_varying (vr
);
3357 set_value_range (vr
, vr0
.type
, min
, max
, NULL
);
3361 /* For unhandled operations fall back to varying. */
3362 set_value_range_to_varying (vr
);
3367 /* Extract range information from a unary expression CODE OP0 based on
3368 the range of its operand with resulting type TYPE.
3369 The resulting range is stored in *VR. */
3372 extract_range_from_unary_expr (value_range
*vr
, enum tree_code code
,
3373 tree type
, tree op0
)
3375 value_range vr0
= VR_INITIALIZER
;
3377 /* Get value ranges for the operand. For constant operands, create
3378 a new value range with the operand to simplify processing. */
3379 if (TREE_CODE (op0
) == SSA_NAME
)
3380 vr0
= *(get_value_range (op0
));
3381 else if (is_gimple_min_invariant (op0
))
3382 set_value_range_to_value (&vr0
, op0
, NULL
);
3384 set_value_range_to_varying (&vr0
);
3386 extract_range_from_unary_expr (vr
, code
, type
, &vr0
, TREE_TYPE (op0
));
3390 /* Extract range information from a conditional expression STMT based on
3391 the ranges of each of its operands and the expression code. */
3394 extract_range_from_cond_expr (value_range
*vr
, gassign
*stmt
)
3397 value_range vr0
= VR_INITIALIZER
;
3398 value_range vr1
= VR_INITIALIZER
;
3400 /* Get value ranges for each operand. For constant operands, create
3401 a new value range with the operand to simplify processing. */
3402 op0
= gimple_assign_rhs2 (stmt
);
3403 if (TREE_CODE (op0
) == SSA_NAME
)
3404 vr0
= *(get_value_range (op0
));
3405 else if (is_gimple_min_invariant (op0
))
3406 set_value_range_to_value (&vr0
, op0
, NULL
);
3408 set_value_range_to_varying (&vr0
);
3410 op1
= gimple_assign_rhs3 (stmt
);
3411 if (TREE_CODE (op1
) == SSA_NAME
)
3412 vr1
= *(get_value_range (op1
));
3413 else if (is_gimple_min_invariant (op1
))
3414 set_value_range_to_value (&vr1
, op1
, NULL
);
3416 set_value_range_to_varying (&vr1
);
3418 /* The resulting value range is the union of the operand ranges */
3419 copy_value_range (vr
, &vr0
);
3420 vrp_meet (vr
, &vr1
);
3424 /* Extract range information from a comparison expression EXPR based
3425 on the range of its operand and the expression code. */
3428 extract_range_from_comparison (value_range
*vr
, enum tree_code code
,
3429 tree type
, tree op0
, tree op1
)
3434 val
= vrp_evaluate_conditional_warnv_with_ops (code
, op0
, op1
, false, &sop
,
3438 /* Since this expression was found on the RHS of an assignment,
3439 its type may be different from _Bool. Convert VAL to EXPR's
3441 val
= fold_convert (type
, val
);
3442 if (is_gimple_min_invariant (val
))
3443 set_value_range_to_value (vr
, val
, vr
->equiv
);
3445 set_value_range (vr
, VR_RANGE
, val
, val
, vr
->equiv
);
3448 /* The result of a comparison is always true or false. */
3449 set_value_range_to_truthvalue (vr
, type
);
3452 /* Helper function for simplify_internal_call_using_ranges and
3453 extract_range_basic. Return true if OP0 SUBCODE OP1 for
3454 SUBCODE {PLUS,MINUS,MULT}_EXPR is known to never overflow or
3455 always overflow. Set *OVF to true if it is known to always
3459 check_for_binary_op_overflow (enum tree_code subcode
, tree type
,
3460 tree op0
, tree op1
, bool *ovf
)
3462 value_range vr0
= VR_INITIALIZER
;
3463 value_range vr1
= VR_INITIALIZER
;
3464 if (TREE_CODE (op0
) == SSA_NAME
)
3465 vr0
= *get_value_range (op0
);
3466 else if (TREE_CODE (op0
) == INTEGER_CST
)
3467 set_value_range_to_value (&vr0
, op0
, NULL
);
3469 set_value_range_to_varying (&vr0
);
3471 if (TREE_CODE (op1
) == SSA_NAME
)
3472 vr1
= *get_value_range (op1
);
3473 else if (TREE_CODE (op1
) == INTEGER_CST
)
3474 set_value_range_to_value (&vr1
, op1
, NULL
);
3476 set_value_range_to_varying (&vr1
);
3478 if (!range_int_cst_p (&vr0
)
3479 || TREE_OVERFLOW (vr0
.min
)
3480 || TREE_OVERFLOW (vr0
.max
))
3482 vr0
.min
= vrp_val_min (TREE_TYPE (op0
));
3483 vr0
.max
= vrp_val_max (TREE_TYPE (op0
));
3485 if (!range_int_cst_p (&vr1
)
3486 || TREE_OVERFLOW (vr1
.min
)
3487 || TREE_OVERFLOW (vr1
.max
))
3489 vr1
.min
= vrp_val_min (TREE_TYPE (op1
));
3490 vr1
.max
= vrp_val_max (TREE_TYPE (op1
));
3492 *ovf
= arith_overflowed_p (subcode
, type
, vr0
.min
,
3493 subcode
== MINUS_EXPR
? vr1
.max
: vr1
.min
);
3494 if (arith_overflowed_p (subcode
, type
, vr0
.max
,
3495 subcode
== MINUS_EXPR
? vr1
.min
: vr1
.max
) != *ovf
)
3497 if (subcode
== MULT_EXPR
)
3499 if (arith_overflowed_p (subcode
, type
, vr0
.min
, vr1
.max
) != *ovf
3500 || arith_overflowed_p (subcode
, type
, vr0
.max
, vr1
.min
) != *ovf
)
3505 /* So far we found that there is an overflow on the boundaries.
3506 That doesn't prove that there is an overflow even for all values
3507 in between the boundaries. For that compute widest_int range
3508 of the result and see if it doesn't overlap the range of
3510 widest_int wmin
, wmax
;
3513 w
[0] = wi::to_widest (vr0
.min
);
3514 w
[1] = wi::to_widest (vr0
.max
);
3515 w
[2] = wi::to_widest (vr1
.min
);
3516 w
[3] = wi::to_widest (vr1
.max
);
3517 for (i
= 0; i
< 4; i
++)
3523 wt
= wi::add (w
[i
& 1], w
[2 + (i
& 2) / 2]);
3526 wt
= wi::sub (w
[i
& 1], w
[2 + (i
& 2) / 2]);
3529 wt
= wi::mul (w
[i
& 1], w
[2 + (i
& 2) / 2]);
3541 wmin
= wi::smin (wmin
, wt
);
3542 wmax
= wi::smax (wmax
, wt
);
3545 /* The result of op0 CODE op1 is known to be in range
3547 widest_int wtmin
= wi::to_widest (vrp_val_min (type
));
3548 widest_int wtmax
= wi::to_widest (vrp_val_max (type
));
3549 /* If all values in [wmin, wmax] are smaller than
3550 [wtmin, wtmax] or all are larger than [wtmin, wtmax],
3551 the arithmetic operation will always overflow. */
3552 if (wmax
< wtmin
|| wmin
> wtmax
)
3559 /* Try to derive a nonnegative or nonzero range out of STMT relying
3560 primarily on generic routines in fold in conjunction with range data.
3561 Store the result in *VR */
3564 extract_range_basic (value_range
*vr
, gimple
*stmt
)
3567 tree type
= gimple_expr_type (stmt
);
3569 if (is_gimple_call (stmt
))
3572 int mini
, maxi
, zerov
= 0, prec
;
3573 enum tree_code subcode
= ERROR_MARK
;
3574 combined_fn cfn
= gimple_call_combined_fn (stmt
);
3578 case CFN_BUILT_IN_CONSTANT_P
:
3579 /* If the call is __builtin_constant_p and the argument is a
3580 function parameter resolve it to false. This avoids bogus
3581 array bound warnings.
3582 ??? We could do this as early as inlining is finished. */
3583 arg
= gimple_call_arg (stmt
, 0);
3584 if (TREE_CODE (arg
) == SSA_NAME
3585 && SSA_NAME_IS_DEFAULT_DEF (arg
)
3586 && TREE_CODE (SSA_NAME_VAR (arg
)) == PARM_DECL
3587 && cfun
->after_inlining
)
3589 set_value_range_to_null (vr
, type
);
3593 /* Both __builtin_ffs* and __builtin_popcount return
3597 arg
= gimple_call_arg (stmt
, 0);
3598 prec
= TYPE_PRECISION (TREE_TYPE (arg
));
3601 if (TREE_CODE (arg
) == SSA_NAME
)
3603 value_range
*vr0
= get_value_range (arg
);
3604 /* If arg is non-zero, then ffs or popcount
3606 if ((vr0
->type
== VR_RANGE
3607 && range_includes_zero_p (vr0
->min
, vr0
->max
) == 0)
3608 || (vr0
->type
== VR_ANTI_RANGE
3609 && range_includes_zero_p (vr0
->min
, vr0
->max
) == 1))
3611 /* If some high bits are known to be zero,
3612 we can decrease the maximum. */
3613 if (vr0
->type
== VR_RANGE
3614 && TREE_CODE (vr0
->max
) == INTEGER_CST
3615 && !operand_less_p (vr0
->min
,
3616 build_zero_cst (TREE_TYPE (vr0
->min
))))
3617 maxi
= tree_floor_log2 (vr0
->max
) + 1;
3620 /* __builtin_parity* returns [0, 1]. */
3625 /* __builtin_c[lt]z* return [0, prec-1], except for
3626 when the argument is 0, but that is undefined behavior.
3627 On many targets where the CLZ RTL or optab value is defined
3628 for 0 the value is prec, so include that in the range
3631 arg
= gimple_call_arg (stmt
, 0);
3632 prec
= TYPE_PRECISION (TREE_TYPE (arg
));
3635 if (optab_handler (clz_optab
, TYPE_MODE (TREE_TYPE (arg
)))
3637 && CLZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (TREE_TYPE (arg
)),
3639 /* Handle only the single common value. */
3641 /* Magic value to give up, unless vr0 proves
3644 if (TREE_CODE (arg
) == SSA_NAME
)
3646 value_range
*vr0
= get_value_range (arg
);
3647 /* From clz of VR_RANGE minimum we can compute
3649 if (vr0
->type
== VR_RANGE
3650 && TREE_CODE (vr0
->min
) == INTEGER_CST
)
3652 maxi
= prec
- 1 - tree_floor_log2 (vr0
->min
);
3656 else if (vr0
->type
== VR_ANTI_RANGE
3657 && integer_zerop (vr0
->min
))
3664 /* From clz of VR_RANGE maximum we can compute
3666 if (vr0
->type
== VR_RANGE
3667 && TREE_CODE (vr0
->max
) == INTEGER_CST
)
3669 mini
= prec
- 1 - tree_floor_log2 (vr0
->max
);
3677 /* __builtin_ctz* return [0, prec-1], except for
3678 when the argument is 0, but that is undefined behavior.
3679 If there is a ctz optab for this mode and
3680 CTZ_DEFINED_VALUE_AT_ZERO, include that in the range,
3681 otherwise just assume 0 won't be seen. */
3683 arg
= gimple_call_arg (stmt
, 0);
3684 prec
= TYPE_PRECISION (TREE_TYPE (arg
));
3687 if (optab_handler (ctz_optab
, TYPE_MODE (TREE_TYPE (arg
)))
3689 && CTZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (TREE_TYPE (arg
)),
3692 /* Handle only the two common values. */
3695 else if (zerov
== prec
)
3698 /* Magic value to give up, unless vr0 proves
3702 if (TREE_CODE (arg
) == SSA_NAME
)
3704 value_range
*vr0
= get_value_range (arg
);
3705 /* If arg is non-zero, then use [0, prec - 1]. */
3706 if ((vr0
->type
== VR_RANGE
3707 && integer_nonzerop (vr0
->min
))
3708 || (vr0
->type
== VR_ANTI_RANGE
3709 && integer_zerop (vr0
->min
)))
3714 /* If some high bits are known to be zero,
3715 we can decrease the result maximum. */
3716 if (vr0
->type
== VR_RANGE
3717 && TREE_CODE (vr0
->max
) == INTEGER_CST
)
3719 maxi
= tree_floor_log2 (vr0
->max
);
3720 /* For vr0 [0, 0] give up. */
3728 /* __builtin_clrsb* returns [0, prec-1]. */
3730 arg
= gimple_call_arg (stmt
, 0);
3731 prec
= TYPE_PRECISION (TREE_TYPE (arg
));
3736 set_value_range (vr
, VR_RANGE
, build_int_cst (type
, mini
),
3737 build_int_cst (type
, maxi
), NULL
);
3739 case CFN_UBSAN_CHECK_ADD
:
3740 subcode
= PLUS_EXPR
;
3742 case CFN_UBSAN_CHECK_SUB
:
3743 subcode
= MINUS_EXPR
;
3745 case CFN_UBSAN_CHECK_MUL
:
3746 subcode
= MULT_EXPR
;
3748 case CFN_GOACC_DIM_SIZE
:
3749 case CFN_GOACC_DIM_POS
:
3750 /* Optimizing these two internal functions helps the loop
3751 optimizer eliminate outer comparisons. Size is [1,N]
3752 and pos is [0,N-1]. */
3754 bool is_pos
= cfn
== CFN_GOACC_DIM_POS
;
3755 int axis
= oacc_get_ifn_dim_arg (stmt
);
3756 int size
= oacc_get_fn_dim_size (current_function_decl
, axis
);
3759 /* If it's dynamic, the backend might know a hardware
3761 size
= targetm
.goacc
.dim_limit (axis
);
3763 tree type
= TREE_TYPE (gimple_call_lhs (stmt
));
3764 set_value_range (vr
, VR_RANGE
,
3765 build_int_cst (type
, is_pos
? 0 : 1),
3766 size
? build_int_cst (type
, size
- is_pos
)
3767 : vrp_val_max (type
), NULL
);
3770 case CFN_BUILT_IN_STRLEN
:
3771 if (tree lhs
= gimple_call_lhs (stmt
))
3772 if (ptrdiff_type_node
3773 && (TYPE_PRECISION (ptrdiff_type_node
)
3774 == TYPE_PRECISION (TREE_TYPE (lhs
))))
3776 tree type
= TREE_TYPE (lhs
);
3777 tree max
= vrp_val_max (ptrdiff_type_node
);
3778 wide_int wmax
= wi::to_wide (max
, TYPE_PRECISION (TREE_TYPE (max
)));
3779 tree range_min
= build_zero_cst (type
);
3780 tree range_max
= wide_int_to_tree (type
, wmax
- 1);
3781 set_value_range (vr
, VR_RANGE
, range_min
, range_max
, NULL
);
3788 if (subcode
!= ERROR_MARK
)
3790 bool saved_flag_wrapv
= flag_wrapv
;
3791 /* Pretend the arithmetics is wrapping. If there is
3792 any overflow, we'll complain, but will actually do
3793 wrapping operation. */
3795 extract_range_from_binary_expr (vr
, subcode
, type
,
3796 gimple_call_arg (stmt
, 0),
3797 gimple_call_arg (stmt
, 1));
3798 flag_wrapv
= saved_flag_wrapv
;
3800 /* If for both arguments vrp_valueize returned non-NULL,
3801 this should have been already folded and if not, it
3802 wasn't folded because of overflow. Avoid removing the
3803 UBSAN_CHECK_* calls in that case. */
3804 if (vr
->type
== VR_RANGE
3805 && (vr
->min
== vr
->max
3806 || operand_equal_p (vr
->min
, vr
->max
, 0)))
3807 set_value_range_to_varying (vr
);
3811 /* Handle extraction of the two results (result of arithmetics and
3812 a flag whether arithmetics overflowed) from {ADD,SUB,MUL}_OVERFLOW
3813 internal function. Similarly from ATOMIC_COMPARE_EXCHANGE. */
3814 else if (is_gimple_assign (stmt
)
3815 && (gimple_assign_rhs_code (stmt
) == REALPART_EXPR
3816 || gimple_assign_rhs_code (stmt
) == IMAGPART_EXPR
)
3817 && INTEGRAL_TYPE_P (type
))
3819 enum tree_code code
= gimple_assign_rhs_code (stmt
);
3820 tree op
= gimple_assign_rhs1 (stmt
);
3821 if (TREE_CODE (op
) == code
&& TREE_CODE (TREE_OPERAND (op
, 0)) == SSA_NAME
)
3823 gimple
*g
= SSA_NAME_DEF_STMT (TREE_OPERAND (op
, 0));
3824 if (is_gimple_call (g
) && gimple_call_internal_p (g
))
3826 enum tree_code subcode
= ERROR_MARK
;
3827 switch (gimple_call_internal_fn (g
))
3829 case IFN_ADD_OVERFLOW
:
3830 subcode
= PLUS_EXPR
;
3832 case IFN_SUB_OVERFLOW
:
3833 subcode
= MINUS_EXPR
;
3835 case IFN_MUL_OVERFLOW
:
3836 subcode
= MULT_EXPR
;
3838 case IFN_ATOMIC_COMPARE_EXCHANGE
:
3839 if (code
== IMAGPART_EXPR
)
3841 /* This is the boolean return value whether compare and
3842 exchange changed anything or not. */
3843 set_value_range (vr
, VR_RANGE
, build_int_cst (type
, 0),
3844 build_int_cst (type
, 1), NULL
);
3851 if (subcode
!= ERROR_MARK
)
3853 tree op0
= gimple_call_arg (g
, 0);
3854 tree op1
= gimple_call_arg (g
, 1);
3855 if (code
== IMAGPART_EXPR
)
3858 if (check_for_binary_op_overflow (subcode
, type
,
3860 set_value_range_to_value (vr
,
3861 build_int_cst (type
, ovf
),
3863 else if (TYPE_PRECISION (type
) == 1
3864 && !TYPE_UNSIGNED (type
))
3865 set_value_range_to_varying (vr
);
3867 set_value_range (vr
, VR_RANGE
, build_int_cst (type
, 0),
3868 build_int_cst (type
, 1), NULL
);
3870 else if (types_compatible_p (type
, TREE_TYPE (op0
))
3871 && types_compatible_p (type
, TREE_TYPE (op1
)))
3873 bool saved_flag_wrapv
= flag_wrapv
;
3874 /* Pretend the arithmetics is wrapping. If there is
3875 any overflow, IMAGPART_EXPR will be set. */
3877 extract_range_from_binary_expr (vr
, subcode
, type
,
3879 flag_wrapv
= saved_flag_wrapv
;
3883 value_range vr0
= VR_INITIALIZER
;
3884 value_range vr1
= VR_INITIALIZER
;
3885 bool saved_flag_wrapv
= flag_wrapv
;
3886 /* Pretend the arithmetics is wrapping. If there is
3887 any overflow, IMAGPART_EXPR will be set. */
3889 extract_range_from_unary_expr (&vr0
, NOP_EXPR
,
3891 extract_range_from_unary_expr (&vr1
, NOP_EXPR
,
3893 extract_range_from_binary_expr_1 (vr
, subcode
, type
,
3895 flag_wrapv
= saved_flag_wrapv
;
3902 if (INTEGRAL_TYPE_P (type
)
3903 && gimple_stmt_nonnegative_warnv_p (stmt
, &sop
))
3904 set_value_range_to_nonnegative (vr
, type
);
3905 else if (vrp_stmt_computes_nonzero (stmt
))
3906 set_value_range_to_nonnull (vr
, type
);
3908 set_value_range_to_varying (vr
);
3912 /* Try to compute a useful range out of assignment STMT and store it
3916 extract_range_from_assignment (value_range
*vr
, gassign
*stmt
)
3918 enum tree_code code
= gimple_assign_rhs_code (stmt
);
3920 if (code
== ASSERT_EXPR
)
3921 extract_range_from_assert (vr
, gimple_assign_rhs1 (stmt
));
3922 else if (code
== SSA_NAME
)
3923 extract_range_from_ssa_name (vr
, gimple_assign_rhs1 (stmt
));
3924 else if (TREE_CODE_CLASS (code
) == tcc_binary
)
3925 extract_range_from_binary_expr (vr
, gimple_assign_rhs_code (stmt
),
3926 gimple_expr_type (stmt
),
3927 gimple_assign_rhs1 (stmt
),
3928 gimple_assign_rhs2 (stmt
));
3929 else if (TREE_CODE_CLASS (code
) == tcc_unary
)
3930 extract_range_from_unary_expr (vr
, gimple_assign_rhs_code (stmt
),
3931 gimple_expr_type (stmt
),
3932 gimple_assign_rhs1 (stmt
));
3933 else if (code
== COND_EXPR
)
3934 extract_range_from_cond_expr (vr
, stmt
);
3935 else if (TREE_CODE_CLASS (code
) == tcc_comparison
)
3936 extract_range_from_comparison (vr
, gimple_assign_rhs_code (stmt
),
3937 gimple_expr_type (stmt
),
3938 gimple_assign_rhs1 (stmt
),
3939 gimple_assign_rhs2 (stmt
));
3940 else if (get_gimple_rhs_class (code
) == GIMPLE_SINGLE_RHS
3941 && is_gimple_min_invariant (gimple_assign_rhs1 (stmt
)))
3942 set_value_range_to_value (vr
, gimple_assign_rhs1 (stmt
), NULL
);
3944 set_value_range_to_varying (vr
);
3946 if (vr
->type
== VR_VARYING
)
3947 extract_range_basic (vr
, stmt
);
3950 /* Given a range VR, a LOOP and a variable VAR, determine whether it
3951 would be profitable to adjust VR using scalar evolution information
3952 for VAR. If so, update VR with the new limits. */
3955 adjust_range_with_scev (value_range
*vr
, struct loop
*loop
,
3956 gimple
*stmt
, tree var
)
3958 tree init
, step
, chrec
, tmin
, tmax
, min
, max
, type
, tem
;
3959 enum ev_direction dir
;
3961 /* TODO. Don't adjust anti-ranges. An anti-range may provide
3962 better opportunities than a regular range, but I'm not sure. */
3963 if (vr
->type
== VR_ANTI_RANGE
)
3966 chrec
= instantiate_parameters (loop
, analyze_scalar_evolution (loop
, var
));
3968 /* Like in PR19590, scev can return a constant function. */
3969 if (is_gimple_min_invariant (chrec
))
3971 set_value_range_to_value (vr
, chrec
, vr
->equiv
);
3975 if (TREE_CODE (chrec
) != POLYNOMIAL_CHREC
)
3978 init
= initial_condition_in_loop_num (chrec
, loop
->num
);
3979 tem
= op_with_constant_singleton_value_range (init
);
3982 step
= evolution_part_in_loop_num (chrec
, loop
->num
);
3983 tem
= op_with_constant_singleton_value_range (step
);
3987 /* If STEP is symbolic, we can't know whether INIT will be the
3988 minimum or maximum value in the range. Also, unless INIT is
3989 a simple expression, compare_values and possibly other functions
3990 in tree-vrp won't be able to handle it. */
3991 if (step
== NULL_TREE
3992 || !is_gimple_min_invariant (step
)
3993 || !valid_value_p (init
))
3996 dir
= scev_direction (chrec
);
3997 if (/* Do not adjust ranges if we do not know whether the iv increases
3998 or decreases, ... */
3999 dir
== EV_DIR_UNKNOWN
4000 /* ... or if it may wrap. */
4001 || scev_probably_wraps_p (NULL_TREE
, init
, step
, stmt
,
4002 get_chrec_loop (chrec
), true))
4005 type
= TREE_TYPE (var
);
4006 if (POINTER_TYPE_P (type
) || !TYPE_MIN_VALUE (type
))
4007 tmin
= lower_bound_in_type (type
, type
);
4009 tmin
= TYPE_MIN_VALUE (type
);
4010 if (POINTER_TYPE_P (type
) || !TYPE_MAX_VALUE (type
))
4011 tmax
= upper_bound_in_type (type
, type
);
4013 tmax
= TYPE_MAX_VALUE (type
);
4015 /* Try to use estimated number of iterations for the loop to constrain the
4016 final value in the evolution. */
4017 if (TREE_CODE (step
) == INTEGER_CST
4018 && is_gimple_val (init
)
4019 && (TREE_CODE (init
) != SSA_NAME
4020 || get_value_range (init
)->type
== VR_RANGE
))
4024 /* We are only entering here for loop header PHI nodes, so using
4025 the number of latch executions is the correct thing to use. */
4026 if (max_loop_iterations (loop
, &nit
))
4028 value_range maxvr
= VR_INITIALIZER
;
4029 signop sgn
= TYPE_SIGN (TREE_TYPE (step
));
4032 widest_int wtmp
= wi::mul (wi::to_widest (step
), nit
, sgn
,
4034 /* If the multiplication overflowed we can't do a meaningful
4035 adjustment. Likewise if the result doesn't fit in the type
4036 of the induction variable. For a signed type we have to
4037 check whether the result has the expected signedness which
4038 is that of the step as number of iterations is unsigned. */
4040 && wi::fits_to_tree_p (wtmp
, TREE_TYPE (init
))
4042 || wi::gts_p (wtmp
, 0) == wi::gts_p (step
, 0)))
4044 tem
= wide_int_to_tree (TREE_TYPE (init
), wtmp
);
4045 extract_range_from_binary_expr (&maxvr
, PLUS_EXPR
,
4046 TREE_TYPE (init
), init
, tem
);
4047 /* Likewise if the addition did. */
4048 if (maxvr
.type
== VR_RANGE
)
4050 value_range initvr
= VR_INITIALIZER
;
4052 if (TREE_CODE (init
) == SSA_NAME
)
4053 initvr
= *(get_value_range (init
));
4054 else if (is_gimple_min_invariant (init
))
4055 set_value_range_to_value (&initvr
, init
, NULL
);
4059 /* Check if init + nit * step overflows. Though we checked
4060 scev {init, step}_loop doesn't wrap, it is not enough
4061 because the loop may exit immediately. Overflow could
4062 happen in the plus expression in this case. */
4063 if ((dir
== EV_DIR_DECREASES
4064 && compare_values (maxvr
.min
, initvr
.min
) != -1)
4065 || (dir
== EV_DIR_GROWS
4066 && compare_values (maxvr
.max
, initvr
.max
) != 1))
4076 if (vr
->type
== VR_VARYING
|| vr
->type
== VR_UNDEFINED
)
4081 /* For VARYING or UNDEFINED ranges, just about anything we get
4082 from scalar evolutions should be better. */
4084 if (dir
== EV_DIR_DECREASES
)
4089 else if (vr
->type
== VR_RANGE
)
4094 if (dir
== EV_DIR_DECREASES
)
4096 /* INIT is the maximum value. If INIT is lower than VR->MAX
4097 but no smaller than VR->MIN, set VR->MAX to INIT. */
4098 if (compare_values (init
, max
) == -1)
4101 /* According to the loop information, the variable does not
4103 if (compare_values (min
, tmin
) == -1)
4109 /* If INIT is bigger than VR->MIN, set VR->MIN to INIT. */
4110 if (compare_values (init
, min
) == 1)
4113 if (compare_values (tmax
, max
) == -1)
4120 /* If we just created an invalid range with the minimum
4121 greater than the maximum, we fail conservatively.
4122 This should happen only in unreachable
4123 parts of code, or for invalid programs. */
4124 if (compare_values (min
, max
) == 1)
4127 /* Even for valid range info, sometimes overflow flag will leak in.
4128 As GIMPLE IL should have no constants with TREE_OVERFLOW set, we
4130 if (TREE_OVERFLOW_P (min
))
4131 min
= drop_tree_overflow (min
);
4132 if (TREE_OVERFLOW_P (max
))
4133 max
= drop_tree_overflow (max
);
4135 set_value_range (vr
, VR_RANGE
, min
, max
, vr
->equiv
);
4139 /* Given two numeric value ranges VR0, VR1 and a comparison code COMP:
4141 - Return BOOLEAN_TRUE_NODE if VR0 COMP VR1 always returns true for
4142 all the values in the ranges.
4144 - Return BOOLEAN_FALSE_NODE if the comparison always returns false.
4146 - Return NULL_TREE if it is not always possible to determine the
4147 value of the comparison.
4149 Also set *STRICT_OVERFLOW_P to indicate whether comparision evaluation
4150 assumed signed overflow is undefined. */
4154 compare_ranges (enum tree_code comp
, value_range
*vr0
, value_range
*vr1
,
4155 bool *strict_overflow_p
)
4157 /* VARYING or UNDEFINED ranges cannot be compared. */
4158 if (vr0
->type
== VR_VARYING
4159 || vr0
->type
== VR_UNDEFINED
4160 || vr1
->type
== VR_VARYING
4161 || vr1
->type
== VR_UNDEFINED
)
4164 /* Anti-ranges need to be handled separately. */
4165 if (vr0
->type
== VR_ANTI_RANGE
|| vr1
->type
== VR_ANTI_RANGE
)
4167 /* If both are anti-ranges, then we cannot compute any
4169 if (vr0
->type
== VR_ANTI_RANGE
&& vr1
->type
== VR_ANTI_RANGE
)
4172 /* These comparisons are never statically computable. */
4179 /* Equality can be computed only between a range and an
4180 anti-range. ~[VAL1, VAL2] == [VAL1, VAL2] is always false. */
4181 if (vr0
->type
== VR_RANGE
)
4183 /* To simplify processing, make VR0 the anti-range. */
4184 value_range
*tmp
= vr0
;
4189 gcc_assert (comp
== NE_EXPR
|| comp
== EQ_EXPR
);
4191 if (compare_values_warnv (vr0
->min
, vr1
->min
, strict_overflow_p
) == 0
4192 && compare_values_warnv (vr0
->max
, vr1
->max
, strict_overflow_p
) == 0)
4193 return (comp
== NE_EXPR
) ? boolean_true_node
: boolean_false_node
;
4198 /* Simplify processing. If COMP is GT_EXPR or GE_EXPR, switch the
4199 operands around and change the comparison code. */
4200 if (comp
== GT_EXPR
|| comp
== GE_EXPR
)
4202 comp
= (comp
== GT_EXPR
) ? LT_EXPR
: LE_EXPR
;
4203 std::swap (vr0
, vr1
);
4206 if (comp
== EQ_EXPR
)
4208 /* Equality may only be computed if both ranges represent
4209 exactly one value. */
4210 if (compare_values_warnv (vr0
->min
, vr0
->max
, strict_overflow_p
) == 0
4211 && compare_values_warnv (vr1
->min
, vr1
->max
, strict_overflow_p
) == 0)
4213 int cmp_min
= compare_values_warnv (vr0
->min
, vr1
->min
,
4215 int cmp_max
= compare_values_warnv (vr0
->max
, vr1
->max
,
4217 if (cmp_min
== 0 && cmp_max
== 0)
4218 return boolean_true_node
;
4219 else if (cmp_min
!= -2 && cmp_max
!= -2)
4220 return boolean_false_node
;
4222 /* If [V0_MIN, V1_MAX] < [V1_MIN, V1_MAX] then V0 != V1. */
4223 else if (compare_values_warnv (vr0
->min
, vr1
->max
,
4224 strict_overflow_p
) == 1
4225 || compare_values_warnv (vr1
->min
, vr0
->max
,
4226 strict_overflow_p
) == 1)
4227 return boolean_false_node
;
4231 else if (comp
== NE_EXPR
)
4235 /* If VR0 is completely to the left or completely to the right
4236 of VR1, they are always different. Notice that we need to
4237 make sure that both comparisons yield similar results to
4238 avoid comparing values that cannot be compared at
4240 cmp1
= compare_values_warnv (vr0
->max
, vr1
->min
, strict_overflow_p
);
4241 cmp2
= compare_values_warnv (vr0
->min
, vr1
->max
, strict_overflow_p
);
4242 if ((cmp1
== -1 && cmp2
== -1) || (cmp1
== 1 && cmp2
== 1))
4243 return boolean_true_node
;
4245 /* If VR0 and VR1 represent a single value and are identical,
4247 else if (compare_values_warnv (vr0
->min
, vr0
->max
,
4248 strict_overflow_p
) == 0
4249 && compare_values_warnv (vr1
->min
, vr1
->max
,
4250 strict_overflow_p
) == 0
4251 && compare_values_warnv (vr0
->min
, vr1
->min
,
4252 strict_overflow_p
) == 0
4253 && compare_values_warnv (vr0
->max
, vr1
->max
,
4254 strict_overflow_p
) == 0)
4255 return boolean_false_node
;
4257 /* Otherwise, they may or may not be different. */
4261 else if (comp
== LT_EXPR
|| comp
== LE_EXPR
)
4265 /* If VR0 is to the left of VR1, return true. */
4266 tst
= compare_values_warnv (vr0
->max
, vr1
->min
, strict_overflow_p
);
4267 if ((comp
== LT_EXPR
&& tst
== -1)
4268 || (comp
== LE_EXPR
&& (tst
== -1 || tst
== 0)))
4269 return boolean_true_node
;
4271 /* If VR0 is to the right of VR1, return false. */
4272 tst
= compare_values_warnv (vr0
->min
, vr1
->max
, strict_overflow_p
);
4273 if ((comp
== LT_EXPR
&& (tst
== 0 || tst
== 1))
4274 || (comp
== LE_EXPR
&& tst
== 1))
4275 return boolean_false_node
;
4277 /* Otherwise, we don't know. */
4285 /* Given a value range VR, a value VAL and a comparison code COMP, return
4286 BOOLEAN_TRUE_NODE if VR COMP VAL always returns true for all the
4287 values in VR. Return BOOLEAN_FALSE_NODE if the comparison
4288 always returns false. Return NULL_TREE if it is not always
4289 possible to determine the value of the comparison. Also set
4290 *STRICT_OVERFLOW_P to indicate whether comparision evaluation
4291 assumed signed overflow is undefined. */
4294 compare_range_with_value (enum tree_code comp
, value_range
*vr
, tree val
,
4295 bool *strict_overflow_p
)
4297 if (vr
->type
== VR_VARYING
|| vr
->type
== VR_UNDEFINED
)
4300 /* Anti-ranges need to be handled separately. */
4301 if (vr
->type
== VR_ANTI_RANGE
)
4303 /* For anti-ranges, the only predicates that we can compute at
4304 compile time are equality and inequality. */
4311 /* ~[VAL_1, VAL_2] OP VAL is known if VAL_1 <= VAL <= VAL_2. */
4312 if (value_inside_range (val
, vr
->min
, vr
->max
) == 1)
4313 return (comp
== NE_EXPR
) ? boolean_true_node
: boolean_false_node
;
4318 if (comp
== EQ_EXPR
)
4320 /* EQ_EXPR may only be computed if VR represents exactly
4322 if (compare_values_warnv (vr
->min
, vr
->max
, strict_overflow_p
) == 0)
4324 int cmp
= compare_values_warnv (vr
->min
, val
, strict_overflow_p
);
4326 return boolean_true_node
;
4327 else if (cmp
== -1 || cmp
== 1 || cmp
== 2)
4328 return boolean_false_node
;
4330 else if (compare_values_warnv (val
, vr
->min
, strict_overflow_p
) == -1
4331 || compare_values_warnv (vr
->max
, val
, strict_overflow_p
) == -1)
4332 return boolean_false_node
;
4336 else if (comp
== NE_EXPR
)
4338 /* If VAL is not inside VR, then they are always different. */
4339 if (compare_values_warnv (vr
->max
, val
, strict_overflow_p
) == -1
4340 || compare_values_warnv (vr
->min
, val
, strict_overflow_p
) == 1)
4341 return boolean_true_node
;
4343 /* If VR represents exactly one value equal to VAL, then return
4345 if (compare_values_warnv (vr
->min
, vr
->max
, strict_overflow_p
) == 0
4346 && compare_values_warnv (vr
->min
, val
, strict_overflow_p
) == 0)
4347 return boolean_false_node
;
4349 /* Otherwise, they may or may not be different. */
4352 else if (comp
== LT_EXPR
|| comp
== LE_EXPR
)
4356 /* If VR is to the left of VAL, return true. */
4357 tst
= compare_values_warnv (vr
->max
, val
, strict_overflow_p
);
4358 if ((comp
== LT_EXPR
&& tst
== -1)
4359 || (comp
== LE_EXPR
&& (tst
== -1 || tst
== 0)))
4360 return boolean_true_node
;
4362 /* If VR is to the right of VAL, return false. */
4363 tst
= compare_values_warnv (vr
->min
, val
, strict_overflow_p
);
4364 if ((comp
== LT_EXPR
&& (tst
== 0 || tst
== 1))
4365 || (comp
== LE_EXPR
&& tst
== 1))
4366 return boolean_false_node
;
4368 /* Otherwise, we don't know. */
4371 else if (comp
== GT_EXPR
|| comp
== GE_EXPR
)
4375 /* If VR is to the right of VAL, return true. */
4376 tst
= compare_values_warnv (vr
->min
, val
, strict_overflow_p
);
4377 if ((comp
== GT_EXPR
&& tst
== 1)
4378 || (comp
== GE_EXPR
&& (tst
== 0 || tst
== 1)))
4379 return boolean_true_node
;
4381 /* If VR is to the left of VAL, return false. */
4382 tst
= compare_values_warnv (vr
->max
, val
, strict_overflow_p
);
4383 if ((comp
== GT_EXPR
&& (tst
== -1 || tst
== 0))
4384 || (comp
== GE_EXPR
&& tst
== -1))
4385 return boolean_false_node
;
4387 /* Otherwise, we don't know. */
4395 /* Debugging dumps. */
4397 void dump_value_range (FILE *, const value_range
*);
4398 void debug_value_range (value_range
*);
4399 void dump_all_value_ranges (FILE *);
4400 void debug_all_value_ranges (void);
4401 void dump_vr_equiv (FILE *, bitmap
);
4402 void debug_vr_equiv (bitmap
);
4405 /* Dump value range VR to FILE. */
4408 dump_value_range (FILE *file
, const value_range
*vr
)
4411 fprintf (file
, "[]");
4412 else if (vr
->type
== VR_UNDEFINED
)
4413 fprintf (file
, "UNDEFINED");
4414 else if (vr
->type
== VR_RANGE
|| vr
->type
== VR_ANTI_RANGE
)
4416 tree type
= TREE_TYPE (vr
->min
);
4418 fprintf (file
, "%s[", (vr
->type
== VR_ANTI_RANGE
) ? "~" : "");
4420 if (INTEGRAL_TYPE_P (type
)
4421 && !TYPE_UNSIGNED (type
)
4422 && vrp_val_is_min (vr
->min
))
4423 fprintf (file
, "-INF");
4425 print_generic_expr (file
, vr
->min
);
4427 fprintf (file
, ", ");
4429 if (INTEGRAL_TYPE_P (type
)
4430 && vrp_val_is_max (vr
->max
))
4431 fprintf (file
, "+INF");
4433 print_generic_expr (file
, vr
->max
);
4435 fprintf (file
, "]");
4442 fprintf (file
, " EQUIVALENCES: { ");
4444 EXECUTE_IF_SET_IN_BITMAP (vr
->equiv
, 0, i
, bi
)
4446 print_generic_expr (file
, ssa_name (i
));
4447 fprintf (file
, " ");
4451 fprintf (file
, "} (%u elements)", c
);
4454 else if (vr
->type
== VR_VARYING
)
4455 fprintf (file
, "VARYING");
4457 fprintf (file
, "INVALID RANGE");
4461 /* Dump value range VR to stderr. */
4464 debug_value_range (value_range
*vr
)
4466 dump_value_range (stderr
, vr
);
4467 fprintf (stderr
, "\n");
4471 /* Dump value ranges of all SSA_NAMEs to FILE. */
4474 dump_all_value_ranges (FILE *file
)
4478 for (i
= 0; i
< num_vr_values
; i
++)
4482 print_generic_expr (file
, ssa_name (i
));
4483 fprintf (file
, ": ");
4484 dump_value_range (file
, vr_value
[i
]);
4485 fprintf (file
, "\n");
4489 fprintf (file
, "\n");
4493 /* Dump all value ranges to stderr. */
4496 debug_all_value_ranges (void)
4498 dump_all_value_ranges (stderr
);
4502 /* Given a COND_EXPR COND of the form 'V OP W', and an SSA name V,
4503 create a new SSA name N and return the assertion assignment
4504 'N = ASSERT_EXPR <V, V OP W>'. */
4507 build_assert_expr_for (tree cond
, tree v
)
4512 gcc_assert (TREE_CODE (v
) == SSA_NAME
4513 && COMPARISON_CLASS_P (cond
));
4515 a
= build2 (ASSERT_EXPR
, TREE_TYPE (v
), v
, cond
);
4516 assertion
= gimple_build_assign (NULL_TREE
, a
);
4518 /* The new ASSERT_EXPR, creates a new SSA name that replaces the
4519 operand of the ASSERT_EXPR. Create it so the new name and the old one
4520 are registered in the replacement table so that we can fix the SSA web
4521 after adding all the ASSERT_EXPRs. */
4522 create_new_def_for (v
, assertion
, NULL
);
4528 /* Return false if EXPR is a predicate expression involving floating
4532 fp_predicate (gimple
*stmt
)
4534 GIMPLE_CHECK (stmt
, GIMPLE_COND
);
4536 return FLOAT_TYPE_P (TREE_TYPE (gimple_cond_lhs (stmt
)));
4539 /* If the range of values taken by OP can be inferred after STMT executes,
4540 return the comparison code (COMP_CODE_P) and value (VAL_P) that
4541 describes the inferred range. Return true if a range could be
4545 infer_value_range (gimple
*stmt
, tree op
, tree_code
*comp_code_p
, tree
*val_p
)
4548 *comp_code_p
= ERROR_MARK
;
4550 /* Do not attempt to infer anything in names that flow through
4552 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op
))
4555 /* If STMT is the last statement of a basic block with no normal
4556 successors, there is no point inferring anything about any of its
4557 operands. We would not be able to find a proper insertion point
4558 for the assertion, anyway. */
4559 if (stmt_ends_bb_p (stmt
))
4564 FOR_EACH_EDGE (e
, ei
, gimple_bb (stmt
)->succs
)
4565 if (!(e
->flags
& (EDGE_ABNORMAL
|EDGE_EH
)))
4571 if (infer_nonnull_range (stmt
, op
))
4573 *val_p
= build_int_cst (TREE_TYPE (op
), 0);
4574 *comp_code_p
= NE_EXPR
;
4582 void dump_asserts_for (FILE *, tree
);
4583 void debug_asserts_for (tree
);
4584 void dump_all_asserts (FILE *);
4585 void debug_all_asserts (void);
4587 /* Dump all the registered assertions for NAME to FILE. */
4590 dump_asserts_for (FILE *file
, tree name
)
4594 fprintf (file
, "Assertions to be inserted for ");
4595 print_generic_expr (file
, name
);
4596 fprintf (file
, "\n");
4598 loc
= asserts_for
[SSA_NAME_VERSION (name
)];
4601 fprintf (file
, "\t");
4602 print_gimple_stmt (file
, gsi_stmt (loc
->si
), 0);
4603 fprintf (file
, "\n\tBB #%d", loc
->bb
->index
);
4606 fprintf (file
, "\n\tEDGE %d->%d", loc
->e
->src
->index
,
4607 loc
->e
->dest
->index
);
4608 dump_edge_info (file
, loc
->e
, dump_flags
, 0);
4610 fprintf (file
, "\n\tPREDICATE: ");
4611 print_generic_expr (file
, loc
->expr
);
4612 fprintf (file
, " %s ", get_tree_code_name (loc
->comp_code
));
4613 print_generic_expr (file
, loc
->val
);
4614 fprintf (file
, "\n\n");
4618 fprintf (file
, "\n");
4622 /* Dump all the registered assertions for NAME to stderr. */
4625 debug_asserts_for (tree name
)
4627 dump_asserts_for (stderr
, name
);
4631 /* Dump all the registered assertions for all the names to FILE. */
4634 dump_all_asserts (FILE *file
)
4639 fprintf (file
, "\nASSERT_EXPRs to be inserted\n\n");
4640 EXECUTE_IF_SET_IN_BITMAP (need_assert_for
, 0, i
, bi
)
4641 dump_asserts_for (file
, ssa_name (i
));
4642 fprintf (file
, "\n");
4646 /* Dump all the registered assertions for all the names to stderr. */
4649 debug_all_asserts (void)
4651 dump_all_asserts (stderr
);
4654 /* Push the assert info for NAME, EXPR, COMP_CODE and VAL to ASSERTS. */
4657 add_assert_info (vec
<assert_info
> &asserts
,
4658 tree name
, tree expr
, enum tree_code comp_code
, tree val
)
4661 info
.comp_code
= comp_code
;
4665 asserts
.safe_push (info
);
4668 /* If NAME doesn't have an ASSERT_EXPR registered for asserting
4669 'EXPR COMP_CODE VAL' at a location that dominates block BB or
4670 E->DEST, then register this location as a possible insertion point
4671 for ASSERT_EXPR <NAME, EXPR COMP_CODE VAL>.
4673 BB, E and SI provide the exact insertion point for the new
4674 ASSERT_EXPR. If BB is NULL, then the ASSERT_EXPR is to be inserted
4675 on edge E. Otherwise, if E is NULL, the ASSERT_EXPR is inserted on
4676 BB. If SI points to a COND_EXPR or a SWITCH_EXPR statement, then E
4677 must not be NULL. */
4680 register_new_assert_for (tree name
, tree expr
,
4681 enum tree_code comp_code
,
4685 gimple_stmt_iterator si
)
4687 assert_locus
*n
, *loc
, *last_loc
;
4688 basic_block dest_bb
;
4690 gcc_checking_assert (bb
== NULL
|| e
== NULL
);
4693 gcc_checking_assert (gimple_code (gsi_stmt (si
)) != GIMPLE_COND
4694 && gimple_code (gsi_stmt (si
)) != GIMPLE_SWITCH
);
4696 /* Never build an assert comparing against an integer constant with
4697 TREE_OVERFLOW set. This confuses our undefined overflow warning
4699 if (TREE_OVERFLOW_P (val
))
4700 val
= drop_tree_overflow (val
);
4702 /* The new assertion A will be inserted at BB or E. We need to
4703 determine if the new location is dominated by a previously
4704 registered location for A. If we are doing an edge insertion,
4705 assume that A will be inserted at E->DEST. Note that this is not
4708 If E is a critical edge, it will be split. But even if E is
4709 split, the new block will dominate the same set of blocks that
4712 The reverse, however, is not true, blocks dominated by E->DEST
4713 will not be dominated by the new block created to split E. So,
4714 if the insertion location is on a critical edge, we will not use
4715 the new location to move another assertion previously registered
4716 at a block dominated by E->DEST. */
4717 dest_bb
= (bb
) ? bb
: e
->dest
;
4719 /* If NAME already has an ASSERT_EXPR registered for COMP_CODE and
4720 VAL at a block dominating DEST_BB, then we don't need to insert a new
4721 one. Similarly, if the same assertion already exists at a block
4722 dominated by DEST_BB and the new location is not on a critical
4723 edge, then update the existing location for the assertion (i.e.,
4724 move the assertion up in the dominance tree).
4726 Note, this is implemented as a simple linked list because there
4727 should not be more than a handful of assertions registered per
4728 name. If this becomes a performance problem, a table hashed by
4729 COMP_CODE and VAL could be implemented. */
4730 loc
= asserts_for
[SSA_NAME_VERSION (name
)];
4734 if (loc
->comp_code
== comp_code
4736 || operand_equal_p (loc
->val
, val
, 0))
4737 && (loc
->expr
== expr
4738 || operand_equal_p (loc
->expr
, expr
, 0)))
4740 /* If E is not a critical edge and DEST_BB
4741 dominates the existing location for the assertion, move
4742 the assertion up in the dominance tree by updating its
4743 location information. */
4744 if ((e
== NULL
|| !EDGE_CRITICAL_P (e
))
4745 && dominated_by_p (CDI_DOMINATORS
, loc
->bb
, dest_bb
))
4754 /* Update the last node of the list and move to the next one. */
4759 /* If we didn't find an assertion already registered for
4760 NAME COMP_CODE VAL, add a new one at the end of the list of
4761 assertions associated with NAME. */
4762 n
= XNEW (struct assert_locus
);
4766 n
->comp_code
= comp_code
;
4774 asserts_for
[SSA_NAME_VERSION (name
)] = n
;
4776 bitmap_set_bit (need_assert_for
, SSA_NAME_VERSION (name
));
4779 /* (COND_OP0 COND_CODE COND_OP1) is a predicate which uses NAME.
4780 Extract a suitable test code and value and store them into *CODE_P and
4781 *VAL_P so the predicate is normalized to NAME *CODE_P *VAL_P.
4783 If no extraction was possible, return FALSE, otherwise return TRUE.
4785 If INVERT is true, then we invert the result stored into *CODE_P. */
4788 extract_code_and_val_from_cond_with_ops (tree name
, enum tree_code cond_code
,
4789 tree cond_op0
, tree cond_op1
,
4790 bool invert
, enum tree_code
*code_p
,
4793 enum tree_code comp_code
;
4796 /* Otherwise, we have a comparison of the form NAME COMP VAL
4797 or VAL COMP NAME. */
4798 if (name
== cond_op1
)
4800 /* If the predicate is of the form VAL COMP NAME, flip
4801 COMP around because we need to register NAME as the
4802 first operand in the predicate. */
4803 comp_code
= swap_tree_comparison (cond_code
);
4806 else if (name
== cond_op0
)
4808 /* The comparison is of the form NAME COMP VAL, so the
4809 comparison code remains unchanged. */
4810 comp_code
= cond_code
;
4816 /* Invert the comparison code as necessary. */
4818 comp_code
= invert_tree_comparison (comp_code
, 0);
4820 /* VRP only handles integral and pointer types. */
4821 if (! INTEGRAL_TYPE_P (TREE_TYPE (val
))
4822 && ! POINTER_TYPE_P (TREE_TYPE (val
)))
4825 /* Do not register always-false predicates.
4826 FIXME: this works around a limitation in fold() when dealing with
4827 enumerations. Given 'enum { N1, N2 } x;', fold will not
4828 fold 'if (x > N2)' to 'if (0)'. */
4829 if ((comp_code
== GT_EXPR
|| comp_code
== LT_EXPR
)
4830 && INTEGRAL_TYPE_P (TREE_TYPE (val
)))
4832 tree min
= TYPE_MIN_VALUE (TREE_TYPE (val
));
4833 tree max
= TYPE_MAX_VALUE (TREE_TYPE (val
));
4835 if (comp_code
== GT_EXPR
4837 || compare_values (val
, max
) == 0))
4840 if (comp_code
== LT_EXPR
4842 || compare_values (val
, min
) == 0))
4845 *code_p
= comp_code
;
4850 /* Find out smallest RES where RES > VAL && (RES & MASK) == RES, if any
4851 (otherwise return VAL). VAL and MASK must be zero-extended for
4852 precision PREC. If SGNBIT is non-zero, first xor VAL with SGNBIT
4853 (to transform signed values into unsigned) and at the end xor
4857 masked_increment (const wide_int
&val_in
, const wide_int
&mask
,
4858 const wide_int
&sgnbit
, unsigned int prec
)
4860 wide_int bit
= wi::one (prec
), res
;
4863 wide_int val
= val_in
^ sgnbit
;
4864 for (i
= 0; i
< prec
; i
++, bit
+= bit
)
4867 if ((res
& bit
) == 0)
4870 res
= (val
+ bit
).and_not (res
);
4872 if (wi::gtu_p (res
, val
))
4873 return res
^ sgnbit
;
4875 return val
^ sgnbit
;
4878 /* Helper for overflow_comparison_p
4880 OP0 CODE OP1 is a comparison. Examine the comparison and potentially
4881 OP1's defining statement to see if it ultimately has the form
4882 OP0 CODE (OP0 PLUS INTEGER_CST)
4884 If so, return TRUE indicating this is an overflow test and store into
4885 *NEW_CST an updated constant that can be used in a narrowed range test.
4887 REVERSED indicates if the comparison was originally:
4891 This affects how we build the updated constant. */
4894 overflow_comparison_p_1 (enum tree_code code
, tree op0
, tree op1
,
4895 bool follow_assert_exprs
, bool reversed
, tree
*new_cst
)
4897 /* See if this is a relational operation between two SSA_NAMES with
4898 unsigned, overflow wrapping values. If so, check it more deeply. */
4899 if ((code
== LT_EXPR
|| code
== LE_EXPR
4900 || code
== GE_EXPR
|| code
== GT_EXPR
)
4901 && TREE_CODE (op0
) == SSA_NAME
4902 && TREE_CODE (op1
) == SSA_NAME
4903 && INTEGRAL_TYPE_P (TREE_TYPE (op0
))
4904 && TYPE_UNSIGNED (TREE_TYPE (op0
))
4905 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (op0
)))
4907 gimple
*op1_def
= SSA_NAME_DEF_STMT (op1
);
4909 /* If requested, follow any ASSERT_EXPRs backwards for OP1. */
4910 if (follow_assert_exprs
)
4912 while (gimple_assign_single_p (op1_def
)
4913 && TREE_CODE (gimple_assign_rhs1 (op1_def
)) == ASSERT_EXPR
)
4915 op1
= TREE_OPERAND (gimple_assign_rhs1 (op1_def
), 0);
4916 if (TREE_CODE (op1
) != SSA_NAME
)
4918 op1_def
= SSA_NAME_DEF_STMT (op1
);
4922 /* Now look at the defining statement of OP1 to see if it adds
4923 or subtracts a nonzero constant from another operand. */
4925 && is_gimple_assign (op1_def
)
4926 && gimple_assign_rhs_code (op1_def
) == PLUS_EXPR
4927 && TREE_CODE (gimple_assign_rhs2 (op1_def
)) == INTEGER_CST
4928 && !integer_zerop (gimple_assign_rhs2 (op1_def
)))
4930 tree target
= gimple_assign_rhs1 (op1_def
);
4932 /* If requested, follow ASSERT_EXPRs backwards for op0 looking
4933 for one where TARGET appears on the RHS. */
4934 if (follow_assert_exprs
)
4936 /* Now see if that "other operand" is op0, following the chain
4937 of ASSERT_EXPRs if necessary. */
4938 gimple
*op0_def
= SSA_NAME_DEF_STMT (op0
);
4939 while (op0
!= target
4940 && gimple_assign_single_p (op0_def
)
4941 && TREE_CODE (gimple_assign_rhs1 (op0_def
)) == ASSERT_EXPR
)
4943 op0
= TREE_OPERAND (gimple_assign_rhs1 (op0_def
), 0);
4944 if (TREE_CODE (op0
) != SSA_NAME
)
4946 op0_def
= SSA_NAME_DEF_STMT (op0
);
4950 /* If we did not find our target SSA_NAME, then this is not
4951 an overflow test. */
4955 tree type
= TREE_TYPE (op0
);
4956 wide_int max
= wi::max_value (TYPE_PRECISION (type
), UNSIGNED
);
4957 tree inc
= gimple_assign_rhs2 (op1_def
);
4959 *new_cst
= wide_int_to_tree (type
, max
+ inc
);
4961 *new_cst
= wide_int_to_tree (type
, max
- inc
);
4968 /* OP0 CODE OP1 is a comparison. Examine the comparison and potentially
4969 OP1's defining statement to see if it ultimately has the form
4970 OP0 CODE (OP0 PLUS INTEGER_CST)
4972 If so, return TRUE indicating this is an overflow test and store into
4973 *NEW_CST an updated constant that can be used in a narrowed range test.
4975 These statements are left as-is in the IL to facilitate discovery of
4976 {ADD,SUB}_OVERFLOW sequences later in the optimizer pipeline. But
4977 the alternate range representation is often useful within VRP. */
4980 overflow_comparison_p (tree_code code
, tree name
, tree val
,
4981 bool use_equiv_p
, tree
*new_cst
)
4983 if (overflow_comparison_p_1 (code
, name
, val
, use_equiv_p
, false, new_cst
))
4985 return overflow_comparison_p_1 (swap_tree_comparison (code
), val
, name
,
4986 use_equiv_p
, true, new_cst
);
4990 /* Try to register an edge assertion for SSA name NAME on edge E for
4991 the condition COND contributing to the conditional jump pointed to by BSI.
4992 Invert the condition COND if INVERT is true. */
4995 register_edge_assert_for_2 (tree name
, edge e
,
4996 enum tree_code cond_code
,
4997 tree cond_op0
, tree cond_op1
, bool invert
,
4998 vec
<assert_info
> &asserts
)
5001 enum tree_code comp_code
;
5003 if (!extract_code_and_val_from_cond_with_ops (name
, cond_code
,
5006 invert
, &comp_code
, &val
))
5009 /* Queue the assert. */
5011 if (overflow_comparison_p (comp_code
, name
, val
, false, &x
))
5013 enum tree_code new_code
= ((comp_code
== GT_EXPR
|| comp_code
== GE_EXPR
)
5014 ? GT_EXPR
: LE_EXPR
);
5015 add_assert_info (asserts
, name
, name
, new_code
, x
);
5017 add_assert_info (asserts
, name
, name
, comp_code
, val
);
5019 /* In the case of NAME <= CST and NAME being defined as
5020 NAME = (unsigned) NAME2 + CST2 we can assert NAME2 >= -CST2
5021 and NAME2 <= CST - CST2. We can do the same for NAME > CST.
5022 This catches range and anti-range tests. */
5023 if ((comp_code
== LE_EXPR
5024 || comp_code
== GT_EXPR
)
5025 && TREE_CODE (val
) == INTEGER_CST
5026 && TYPE_UNSIGNED (TREE_TYPE (val
)))
5028 gimple
*def_stmt
= SSA_NAME_DEF_STMT (name
);
5029 tree cst2
= NULL_TREE
, name2
= NULL_TREE
, name3
= NULL_TREE
;
5031 /* Extract CST2 from the (optional) addition. */
5032 if (is_gimple_assign (def_stmt
)
5033 && gimple_assign_rhs_code (def_stmt
) == PLUS_EXPR
)
5035 name2
= gimple_assign_rhs1 (def_stmt
);
5036 cst2
= gimple_assign_rhs2 (def_stmt
);
5037 if (TREE_CODE (name2
) == SSA_NAME
5038 && TREE_CODE (cst2
) == INTEGER_CST
)
5039 def_stmt
= SSA_NAME_DEF_STMT (name2
);
5042 /* Extract NAME2 from the (optional) sign-changing cast. */
5043 if (gimple_assign_cast_p (def_stmt
))
5045 if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt
))
5046 && ! TYPE_UNSIGNED (TREE_TYPE (gimple_assign_rhs1 (def_stmt
)))
5047 && (TYPE_PRECISION (gimple_expr_type (def_stmt
))
5048 == TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt
)))))
5049 name3
= gimple_assign_rhs1 (def_stmt
);
5052 /* If name3 is used later, create an ASSERT_EXPR for it. */
5053 if (name3
!= NULL_TREE
5054 && TREE_CODE (name3
) == SSA_NAME
5055 && (cst2
== NULL_TREE
5056 || TREE_CODE (cst2
) == INTEGER_CST
)
5057 && INTEGRAL_TYPE_P (TREE_TYPE (name3
)))
5061 /* Build an expression for the range test. */
5062 tmp
= build1 (NOP_EXPR
, TREE_TYPE (name
), name3
);
5063 if (cst2
!= NULL_TREE
)
5064 tmp
= build2 (PLUS_EXPR
, TREE_TYPE (name
), tmp
, cst2
);
5068 fprintf (dump_file
, "Adding assert for ");
5069 print_generic_expr (dump_file
, name3
);
5070 fprintf (dump_file
, " from ");
5071 print_generic_expr (dump_file
, tmp
);
5072 fprintf (dump_file
, "\n");
5075 add_assert_info (asserts
, name3
, tmp
, comp_code
, val
);
5078 /* If name2 is used later, create an ASSERT_EXPR for it. */
5079 if (name2
!= NULL_TREE
5080 && TREE_CODE (name2
) == SSA_NAME
5081 && TREE_CODE (cst2
) == INTEGER_CST
5082 && INTEGRAL_TYPE_P (TREE_TYPE (name2
)))
5086 /* Build an expression for the range test. */
5088 if (TREE_TYPE (name
) != TREE_TYPE (name2
))
5089 tmp
= build1 (NOP_EXPR
, TREE_TYPE (name
), tmp
);
5090 if (cst2
!= NULL_TREE
)
5091 tmp
= build2 (PLUS_EXPR
, TREE_TYPE (name
), tmp
, cst2
);
5095 fprintf (dump_file
, "Adding assert for ");
5096 print_generic_expr (dump_file
, name2
);
5097 fprintf (dump_file
, " from ");
5098 print_generic_expr (dump_file
, tmp
);
5099 fprintf (dump_file
, "\n");
5102 add_assert_info (asserts
, name2
, tmp
, comp_code
, val
);
5106 /* In the case of post-in/decrement tests like if (i++) ... and uses
5107 of the in/decremented value on the edge the extra name we want to
5108 assert for is not on the def chain of the name compared. Instead
5109 it is in the set of use stmts.
5110 Similar cases happen for conversions that were simplified through
5111 fold_{sign_changed,widened}_comparison. */
5112 if ((comp_code
== NE_EXPR
5113 || comp_code
== EQ_EXPR
)
5114 && TREE_CODE (val
) == INTEGER_CST
)
5116 imm_use_iterator ui
;
5118 FOR_EACH_IMM_USE_STMT (use_stmt
, ui
, name
)
5120 if (!is_gimple_assign (use_stmt
))
5123 /* Cut off to use-stmts that are dominating the predecessor. */
5124 if (!dominated_by_p (CDI_DOMINATORS
, e
->src
, gimple_bb (use_stmt
)))
5127 tree name2
= gimple_assign_lhs (use_stmt
);
5128 if (TREE_CODE (name2
) != SSA_NAME
)
5131 enum tree_code code
= gimple_assign_rhs_code (use_stmt
);
5133 if (code
== PLUS_EXPR
5134 || code
== MINUS_EXPR
)
5136 cst
= gimple_assign_rhs2 (use_stmt
);
5137 if (TREE_CODE (cst
) != INTEGER_CST
)
5139 cst
= int_const_binop (code
, val
, cst
);
5141 else if (CONVERT_EXPR_CODE_P (code
))
5143 /* For truncating conversions we cannot record
5145 if (comp_code
== NE_EXPR
5146 && (TYPE_PRECISION (TREE_TYPE (name2
))
5147 < TYPE_PRECISION (TREE_TYPE (name
))))
5149 cst
= fold_convert (TREE_TYPE (name2
), val
);
5154 if (TREE_OVERFLOW_P (cst
))
5155 cst
= drop_tree_overflow (cst
);
5156 add_assert_info (asserts
, name2
, name2
, comp_code
, cst
);
5160 if (TREE_CODE_CLASS (comp_code
) == tcc_comparison
5161 && TREE_CODE (val
) == INTEGER_CST
)
5163 gimple
*def_stmt
= SSA_NAME_DEF_STMT (name
);
5164 tree name2
= NULL_TREE
, names
[2], cst2
= NULL_TREE
;
5165 tree val2
= NULL_TREE
;
5166 unsigned int prec
= TYPE_PRECISION (TREE_TYPE (val
));
5167 wide_int mask
= wi::zero (prec
);
5168 unsigned int nprec
= prec
;
5169 enum tree_code rhs_code
= ERROR_MARK
;
5171 if (is_gimple_assign (def_stmt
))
5172 rhs_code
= gimple_assign_rhs_code (def_stmt
);
5174 /* In the case of NAME != CST1 where NAME = A +- CST2 we can
5175 assert that A != CST1 -+ CST2. */
5176 if ((comp_code
== EQ_EXPR
|| comp_code
== NE_EXPR
)
5177 && (rhs_code
== PLUS_EXPR
|| rhs_code
== MINUS_EXPR
))
5179 tree op0
= gimple_assign_rhs1 (def_stmt
);
5180 tree op1
= gimple_assign_rhs2 (def_stmt
);
5181 if (TREE_CODE (op0
) == SSA_NAME
5182 && TREE_CODE (op1
) == INTEGER_CST
)
5184 enum tree_code reverse_op
= (rhs_code
== PLUS_EXPR
5185 ? MINUS_EXPR
: PLUS_EXPR
);
5186 op1
= int_const_binop (reverse_op
, val
, op1
);
5187 if (TREE_OVERFLOW (op1
))
5188 op1
= drop_tree_overflow (op1
);
5189 add_assert_info (asserts
, op0
, op0
, comp_code
, op1
);
5193 /* Add asserts for NAME cmp CST and NAME being defined
5194 as NAME = (int) NAME2. */
5195 if (!TYPE_UNSIGNED (TREE_TYPE (val
))
5196 && (comp_code
== LE_EXPR
|| comp_code
== LT_EXPR
5197 || comp_code
== GT_EXPR
|| comp_code
== GE_EXPR
)
5198 && gimple_assign_cast_p (def_stmt
))
5200 name2
= gimple_assign_rhs1 (def_stmt
);
5201 if (CONVERT_EXPR_CODE_P (rhs_code
)
5202 && INTEGRAL_TYPE_P (TREE_TYPE (name2
))
5203 && TYPE_UNSIGNED (TREE_TYPE (name2
))
5204 && prec
== TYPE_PRECISION (TREE_TYPE (name2
))
5205 && (comp_code
== LE_EXPR
|| comp_code
== GT_EXPR
5206 || !tree_int_cst_equal (val
,
5207 TYPE_MIN_VALUE (TREE_TYPE (val
)))))
5210 enum tree_code new_comp_code
= comp_code
;
5212 cst
= fold_convert (TREE_TYPE (name2
),
5213 TYPE_MIN_VALUE (TREE_TYPE (val
)));
5214 /* Build an expression for the range test. */
5215 tmp
= build2 (PLUS_EXPR
, TREE_TYPE (name2
), name2
, cst
);
5216 cst
= fold_build2 (PLUS_EXPR
, TREE_TYPE (name2
), cst
,
5217 fold_convert (TREE_TYPE (name2
), val
));
5218 if (comp_code
== LT_EXPR
|| comp_code
== GE_EXPR
)
5220 new_comp_code
= comp_code
== LT_EXPR
? LE_EXPR
: GT_EXPR
;
5221 cst
= fold_build2 (MINUS_EXPR
, TREE_TYPE (name2
), cst
,
5222 build_int_cst (TREE_TYPE (name2
), 1));
5227 fprintf (dump_file
, "Adding assert for ");
5228 print_generic_expr (dump_file
, name2
);
5229 fprintf (dump_file
, " from ");
5230 print_generic_expr (dump_file
, tmp
);
5231 fprintf (dump_file
, "\n");
5234 add_assert_info (asserts
, name2
, tmp
, new_comp_code
, cst
);
5238 /* Add asserts for NAME cmp CST and NAME being defined as
5239 NAME = NAME2 >> CST2.
5241 Extract CST2 from the right shift. */
5242 if (rhs_code
== RSHIFT_EXPR
)
5244 name2
= gimple_assign_rhs1 (def_stmt
);
5245 cst2
= gimple_assign_rhs2 (def_stmt
);
5246 if (TREE_CODE (name2
) == SSA_NAME
5247 && tree_fits_uhwi_p (cst2
)
5248 && INTEGRAL_TYPE_P (TREE_TYPE (name2
))
5249 && IN_RANGE (tree_to_uhwi (cst2
), 1, prec
- 1)
5250 && type_has_mode_precision_p (TREE_TYPE (val
)))
5252 mask
= wi::mask (tree_to_uhwi (cst2
), false, prec
);
5253 val2
= fold_binary (LSHIFT_EXPR
, TREE_TYPE (val
), val
, cst2
);
5256 if (val2
!= NULL_TREE
5257 && TREE_CODE (val2
) == INTEGER_CST
5258 && simple_cst_equal (fold_build2 (RSHIFT_EXPR
,
5262 enum tree_code new_comp_code
= comp_code
;
5266 if (comp_code
== EQ_EXPR
|| comp_code
== NE_EXPR
)
5268 if (!TYPE_UNSIGNED (TREE_TYPE (val
)))
5270 tree type
= build_nonstandard_integer_type (prec
, 1);
5271 tmp
= build1 (NOP_EXPR
, type
, name2
);
5272 val2
= fold_convert (type
, val2
);
5274 tmp
= fold_build2 (MINUS_EXPR
, TREE_TYPE (tmp
), tmp
, val2
);
5275 new_val
= wide_int_to_tree (TREE_TYPE (tmp
), mask
);
5276 new_comp_code
= comp_code
== EQ_EXPR
? LE_EXPR
: GT_EXPR
;
5278 else if (comp_code
== LT_EXPR
|| comp_code
== GE_EXPR
)
5281 = wi::min_value (prec
, TYPE_SIGN (TREE_TYPE (val
)));
5283 if (minval
== new_val
)
5284 new_val
= NULL_TREE
;
5289 = wi::max_value (prec
, TYPE_SIGN (TREE_TYPE (val
)));
5292 new_val
= NULL_TREE
;
5294 new_val
= wide_int_to_tree (TREE_TYPE (val2
), mask
);
5301 fprintf (dump_file
, "Adding assert for ");
5302 print_generic_expr (dump_file
, name2
);
5303 fprintf (dump_file
, " from ");
5304 print_generic_expr (dump_file
, tmp
);
5305 fprintf (dump_file
, "\n");
5308 add_assert_info (asserts
, name2
, tmp
, new_comp_code
, new_val
);
5312 /* Add asserts for NAME cmp CST and NAME being defined as
5313 NAME = NAME2 & CST2.
5315 Extract CST2 from the and.
5318 NAME = (unsigned) NAME2;
5319 casts where NAME's type is unsigned and has smaller precision
5320 than NAME2's type as if it was NAME = NAME2 & MASK. */
5321 names
[0] = NULL_TREE
;
5322 names
[1] = NULL_TREE
;
5324 if (rhs_code
== BIT_AND_EXPR
5325 || (CONVERT_EXPR_CODE_P (rhs_code
)
5326 && INTEGRAL_TYPE_P (TREE_TYPE (val
))
5327 && TYPE_UNSIGNED (TREE_TYPE (val
))
5328 && TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt
)))
5331 name2
= gimple_assign_rhs1 (def_stmt
);
5332 if (rhs_code
== BIT_AND_EXPR
)
5333 cst2
= gimple_assign_rhs2 (def_stmt
);
5336 cst2
= TYPE_MAX_VALUE (TREE_TYPE (val
));
5337 nprec
= TYPE_PRECISION (TREE_TYPE (name2
));
5339 if (TREE_CODE (name2
) == SSA_NAME
5340 && INTEGRAL_TYPE_P (TREE_TYPE (name2
))
5341 && TREE_CODE (cst2
) == INTEGER_CST
5342 && !integer_zerop (cst2
)
5344 || TYPE_UNSIGNED (TREE_TYPE (val
))))
5346 gimple
*def_stmt2
= SSA_NAME_DEF_STMT (name2
);
5347 if (gimple_assign_cast_p (def_stmt2
))
5349 names
[1] = gimple_assign_rhs1 (def_stmt2
);
5350 if (!CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt2
))
5351 || !INTEGRAL_TYPE_P (TREE_TYPE (names
[1]))
5352 || (TYPE_PRECISION (TREE_TYPE (name2
))
5353 != TYPE_PRECISION (TREE_TYPE (names
[1]))))
5354 names
[1] = NULL_TREE
;
5359 if (names
[0] || names
[1])
5361 wide_int minv
, maxv
, valv
, cst2v
;
5362 wide_int tem
, sgnbit
;
5363 bool valid_p
= false, valn
, cst2n
;
5364 enum tree_code ccode
= comp_code
;
5366 valv
= wide_int::from (val
, nprec
, UNSIGNED
);
5367 cst2v
= wide_int::from (cst2
, nprec
, UNSIGNED
);
5368 valn
= wi::neg_p (valv
, TYPE_SIGN (TREE_TYPE (val
)));
5369 cst2n
= wi::neg_p (cst2v
, TYPE_SIGN (TREE_TYPE (val
)));
5370 /* If CST2 doesn't have most significant bit set,
5371 but VAL is negative, we have comparison like
5372 if ((x & 0x123) > -4) (always true). Just give up. */
5376 sgnbit
= wi::set_bit_in_zero (nprec
- 1, nprec
);
5378 sgnbit
= wi::zero (nprec
);
5379 minv
= valv
& cst2v
;
5383 /* Minimum unsigned value for equality is VAL & CST2
5384 (should be equal to VAL, otherwise we probably should
5385 have folded the comparison into false) and
5386 maximum unsigned value is VAL | ~CST2. */
5387 maxv
= valv
| ~cst2v
;
5392 tem
= valv
| ~cst2v
;
5393 /* If VAL is 0, handle (X & CST2) != 0 as (X & CST2) > 0U. */
5397 sgnbit
= wi::zero (nprec
);
5400 /* If (VAL | ~CST2) is all ones, handle it as
5401 (X & CST2) < VAL. */
5406 sgnbit
= wi::zero (nprec
);
5409 if (!cst2n
&& wi::neg_p (cst2v
))
5410 sgnbit
= wi::set_bit_in_zero (nprec
- 1, nprec
);
5419 if (tem
== wi::mask (nprec
- 1, false, nprec
))
5425 sgnbit
= wi::zero (nprec
);
5430 /* Minimum unsigned value for >= if (VAL & CST2) == VAL
5431 is VAL and maximum unsigned value is ~0. For signed
5432 comparison, if CST2 doesn't have most significant bit
5433 set, handle it similarly. If CST2 has MSB set,
5434 the minimum is the same, and maximum is ~0U/2. */
5437 /* If (VAL & CST2) != VAL, X & CST2 can't be equal to
5439 minv
= masked_increment (valv
, cst2v
, sgnbit
, nprec
);
5443 maxv
= wi::mask (nprec
- (cst2n
? 1 : 0), false, nprec
);
5449 /* Find out smallest MINV where MINV > VAL
5450 && (MINV & CST2) == MINV, if any. If VAL is signed and
5451 CST2 has MSB set, compute it biased by 1 << (nprec - 1). */
5452 minv
= masked_increment (valv
, cst2v
, sgnbit
, nprec
);
5455 maxv
= wi::mask (nprec
- (cst2n
? 1 : 0), false, nprec
);
5460 /* Minimum unsigned value for <= is 0 and maximum
5461 unsigned value is VAL | ~CST2 if (VAL & CST2) == VAL.
5462 Otherwise, find smallest VAL2 where VAL2 > VAL
5463 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
5465 For signed comparison, if CST2 doesn't have most
5466 significant bit set, handle it similarly. If CST2 has
5467 MSB set, the maximum is the same and minimum is INT_MIN. */
5472 maxv
= masked_increment (valv
, cst2v
, sgnbit
, nprec
);
5484 /* Minimum unsigned value for < is 0 and maximum
5485 unsigned value is (VAL-1) | ~CST2 if (VAL & CST2) == VAL.
5486 Otherwise, find smallest VAL2 where VAL2 > VAL
5487 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
5489 For signed comparison, if CST2 doesn't have most
5490 significant bit set, handle it similarly. If CST2 has
5491 MSB set, the maximum is the same and minimum is INT_MIN. */
5500 maxv
= masked_increment (valv
, cst2v
, sgnbit
, nprec
);
5514 && (maxv
- minv
) != -1)
5516 tree tmp
, new_val
, type
;
5519 for (i
= 0; i
< 2; i
++)
5522 wide_int maxv2
= maxv
;
5524 type
= TREE_TYPE (names
[i
]);
5525 if (!TYPE_UNSIGNED (type
))
5527 type
= build_nonstandard_integer_type (nprec
, 1);
5528 tmp
= build1 (NOP_EXPR
, type
, names
[i
]);
5532 tmp
= build2 (PLUS_EXPR
, type
, tmp
,
5533 wide_int_to_tree (type
, -minv
));
5534 maxv2
= maxv
- minv
;
5536 new_val
= wide_int_to_tree (type
, maxv2
);
5540 fprintf (dump_file
, "Adding assert for ");
5541 print_generic_expr (dump_file
, names
[i
]);
5542 fprintf (dump_file
, " from ");
5543 print_generic_expr (dump_file
, tmp
);
5544 fprintf (dump_file
, "\n");
5547 add_assert_info (asserts
, names
[i
], tmp
, LE_EXPR
, new_val
);
5554 /* OP is an operand of a truth value expression which is known to have
5555 a particular value. Register any asserts for OP and for any
5556 operands in OP's defining statement.
5558 If CODE is EQ_EXPR, then we want to register OP is zero (false),
5559 if CODE is NE_EXPR, then we want to register OP is nonzero (true). */
5562 register_edge_assert_for_1 (tree op
, enum tree_code code
,
5563 edge e
, vec
<assert_info
> &asserts
)
5567 enum tree_code rhs_code
;
5569 /* We only care about SSA_NAMEs. */
5570 if (TREE_CODE (op
) != SSA_NAME
)
5573 /* We know that OP will have a zero or nonzero value. */
5574 val
= build_int_cst (TREE_TYPE (op
), 0);
5575 add_assert_info (asserts
, op
, op
, code
, val
);
5577 /* Now look at how OP is set. If it's set from a comparison,
5578 a truth operation or some bit operations, then we may be able
5579 to register information about the operands of that assignment. */
5580 op_def
= SSA_NAME_DEF_STMT (op
);
5581 if (gimple_code (op_def
) != GIMPLE_ASSIGN
)
5584 rhs_code
= gimple_assign_rhs_code (op_def
);
5586 if (TREE_CODE_CLASS (rhs_code
) == tcc_comparison
)
5588 bool invert
= (code
== EQ_EXPR
? true : false);
5589 tree op0
= gimple_assign_rhs1 (op_def
);
5590 tree op1
= gimple_assign_rhs2 (op_def
);
5592 if (TREE_CODE (op0
) == SSA_NAME
)
5593 register_edge_assert_for_2 (op0
, e
, rhs_code
, op0
, op1
, invert
, asserts
);
5594 if (TREE_CODE (op1
) == SSA_NAME
)
5595 register_edge_assert_for_2 (op1
, e
, rhs_code
, op0
, op1
, invert
, asserts
);
5597 else if ((code
== NE_EXPR
5598 && gimple_assign_rhs_code (op_def
) == BIT_AND_EXPR
)
5600 && gimple_assign_rhs_code (op_def
) == BIT_IOR_EXPR
))
5602 /* Recurse on each operand. */
5603 tree op0
= gimple_assign_rhs1 (op_def
);
5604 tree op1
= gimple_assign_rhs2 (op_def
);
5605 if (TREE_CODE (op0
) == SSA_NAME
5606 && has_single_use (op0
))
5607 register_edge_assert_for_1 (op0
, code
, e
, asserts
);
5608 if (TREE_CODE (op1
) == SSA_NAME
5609 && has_single_use (op1
))
5610 register_edge_assert_for_1 (op1
, code
, e
, asserts
);
5612 else if (gimple_assign_rhs_code (op_def
) == BIT_NOT_EXPR
5613 && TYPE_PRECISION (TREE_TYPE (gimple_assign_lhs (op_def
))) == 1)
5615 /* Recurse, flipping CODE. */
5616 code
= invert_tree_comparison (code
, false);
5617 register_edge_assert_for_1 (gimple_assign_rhs1 (op_def
), code
, e
, asserts
);
5619 else if (gimple_assign_rhs_code (op_def
) == SSA_NAME
)
5621 /* Recurse through the copy. */
5622 register_edge_assert_for_1 (gimple_assign_rhs1 (op_def
), code
, e
, asserts
);
5624 else if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (op_def
)))
5626 /* Recurse through the type conversion, unless it is a narrowing
5627 conversion or conversion from non-integral type. */
5628 tree rhs
= gimple_assign_rhs1 (op_def
);
5629 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs
))
5630 && (TYPE_PRECISION (TREE_TYPE (rhs
))
5631 <= TYPE_PRECISION (TREE_TYPE (op
))))
5632 register_edge_assert_for_1 (rhs
, code
, e
, asserts
);
5636 /* Check if comparison
5637 NAME COND_OP INTEGER_CST
5639 (X & 11...100..0) COND_OP XX...X00...0
5640 Such comparison can yield assertions like
5643 in case of COND_OP being NE_EXPR or
5646 in case of EQ_EXPR. */
5649 is_masked_range_test (tree name
, tree valt
, enum tree_code cond_code
,
5650 tree
*new_name
, tree
*low
, enum tree_code
*low_code
,
5651 tree
*high
, enum tree_code
*high_code
)
5653 gimple
*def_stmt
= SSA_NAME_DEF_STMT (name
);
5655 if (!is_gimple_assign (def_stmt
)
5656 || gimple_assign_rhs_code (def_stmt
) != BIT_AND_EXPR
)
5659 tree t
= gimple_assign_rhs1 (def_stmt
);
5660 tree maskt
= gimple_assign_rhs2 (def_stmt
);
5661 if (TREE_CODE (t
) != SSA_NAME
|| TREE_CODE (maskt
) != INTEGER_CST
)
5664 wide_int mask
= maskt
;
5665 wide_int inv_mask
= ~mask
;
5666 wide_int val
= valt
; // Assume VALT is INTEGER_CST
5668 if ((inv_mask
& (inv_mask
+ 1)) != 0
5669 || (val
& mask
) != val
)
5672 bool is_range
= cond_code
== EQ_EXPR
;
5674 tree type
= TREE_TYPE (t
);
5675 wide_int min
= wi::min_value (type
),
5676 max
= wi::max_value (type
);
5680 *low_code
= val
== min
? ERROR_MARK
: GE_EXPR
;
5681 *high_code
= val
== max
? ERROR_MARK
: LE_EXPR
;
5685 /* We can still generate assertion if one of alternatives
5686 is known to always be false. */
5689 *low_code
= (enum tree_code
) 0;
5690 *high_code
= GT_EXPR
;
5692 else if ((val
| inv_mask
) == max
)
5694 *low_code
= LT_EXPR
;
5695 *high_code
= (enum tree_code
) 0;
5702 *low
= wide_int_to_tree (type
, val
);
5703 *high
= wide_int_to_tree (type
, val
| inv_mask
);
5705 if (wi::neg_p (val
, TYPE_SIGN (type
)))
5706 std::swap (*low
, *high
);
5711 /* Try to register an edge assertion for SSA name NAME on edge E for
5712 the condition COND contributing to the conditional jump pointed to by
5716 register_edge_assert_for (tree name
, edge e
,
5717 enum tree_code cond_code
, tree cond_op0
,
5718 tree cond_op1
, vec
<assert_info
> &asserts
)
5721 enum tree_code comp_code
;
5722 bool is_else_edge
= (e
->flags
& EDGE_FALSE_VALUE
) != 0;
5724 /* Do not attempt to infer anything in names that flow through
5726 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name
))
5729 if (!extract_code_and_val_from_cond_with_ops (name
, cond_code
,
5735 /* Register ASSERT_EXPRs for name. */
5736 register_edge_assert_for_2 (name
, e
, cond_code
, cond_op0
,
5737 cond_op1
, is_else_edge
, asserts
);
5740 /* If COND is effectively an equality test of an SSA_NAME against
5741 the value zero or one, then we may be able to assert values
5742 for SSA_NAMEs which flow into COND. */
5744 /* In the case of NAME == 1 or NAME != 0, for BIT_AND_EXPR defining
5745 statement of NAME we can assert both operands of the BIT_AND_EXPR
5746 have nonzero value. */
5747 if (((comp_code
== EQ_EXPR
&& integer_onep (val
))
5748 || (comp_code
== NE_EXPR
&& integer_zerop (val
))))
5750 gimple
*def_stmt
= SSA_NAME_DEF_STMT (name
);
5752 if (is_gimple_assign (def_stmt
)
5753 && gimple_assign_rhs_code (def_stmt
) == BIT_AND_EXPR
)
5755 tree op0
= gimple_assign_rhs1 (def_stmt
);
5756 tree op1
= gimple_assign_rhs2 (def_stmt
);
5757 register_edge_assert_for_1 (op0
, NE_EXPR
, e
, asserts
);
5758 register_edge_assert_for_1 (op1
, NE_EXPR
, e
, asserts
);
5762 /* In the case of NAME == 0 or NAME != 1, for BIT_IOR_EXPR defining
5763 statement of NAME we can assert both operands of the BIT_IOR_EXPR
5765 if (((comp_code
== EQ_EXPR
&& integer_zerop (val
))
5766 || (comp_code
== NE_EXPR
&& integer_onep (val
))))
5768 gimple
*def_stmt
= SSA_NAME_DEF_STMT (name
);
5770 /* For BIT_IOR_EXPR only if NAME == 0 both operands have
5771 necessarily zero value, or if type-precision is one. */
5772 if (is_gimple_assign (def_stmt
)
5773 && (gimple_assign_rhs_code (def_stmt
) == BIT_IOR_EXPR
5774 && (TYPE_PRECISION (TREE_TYPE (name
)) == 1
5775 || comp_code
== EQ_EXPR
)))
5777 tree op0
= gimple_assign_rhs1 (def_stmt
);
5778 tree op1
= gimple_assign_rhs2 (def_stmt
);
5779 register_edge_assert_for_1 (op0
, EQ_EXPR
, e
, asserts
);
5780 register_edge_assert_for_1 (op1
, EQ_EXPR
, e
, asserts
);
5784 /* Sometimes we can infer ranges from (NAME & MASK) == VALUE. */
5785 if ((comp_code
== EQ_EXPR
|| comp_code
== NE_EXPR
)
5786 && TREE_CODE (val
) == INTEGER_CST
)
5788 enum tree_code low_code
, high_code
;
5790 if (is_masked_range_test (name
, val
, comp_code
, &name
, &low
,
5791 &low_code
, &high
, &high_code
))
5793 if (low_code
!= ERROR_MARK
)
5794 register_edge_assert_for_2 (name
, e
, low_code
, name
,
5795 low
, /*invert*/false, asserts
);
5796 if (high_code
!= ERROR_MARK
)
5797 register_edge_assert_for_2 (name
, e
, high_code
, name
,
5798 high
, /*invert*/false, asserts
);
5803 /* Finish found ASSERTS for E and register them at GSI. */
5806 finish_register_edge_assert_for (edge e
, gimple_stmt_iterator gsi
,
5807 vec
<assert_info
> &asserts
)
5809 for (unsigned i
= 0; i
< asserts
.length (); ++i
)
5810 /* Only register an ASSERT_EXPR if NAME was found in the sub-graph
5811 reachable from E. */
5812 if (live_on_edge (e
, asserts
[i
].name
))
5813 register_new_assert_for (asserts
[i
].name
, asserts
[i
].expr
,
5814 asserts
[i
].comp_code
, asserts
[i
].val
,
5820 /* Determine whether the outgoing edges of BB should receive an
5821 ASSERT_EXPR for each of the operands of BB's LAST statement.
5822 The last statement of BB must be a COND_EXPR.
5824 If any of the sub-graphs rooted at BB have an interesting use of
5825 the predicate operands, an assert location node is added to the
5826 list of assertions for the corresponding operands. */
5829 find_conditional_asserts (basic_block bb
, gcond
*last
)
5831 gimple_stmt_iterator bsi
;
5837 bsi
= gsi_for_stmt (last
);
5839 /* Look for uses of the operands in each of the sub-graphs
5840 rooted at BB. We need to check each of the outgoing edges
5841 separately, so that we know what kind of ASSERT_EXPR to
5843 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
5848 /* Register the necessary assertions for each operand in the
5849 conditional predicate. */
5850 auto_vec
<assert_info
, 8> asserts
;
5851 FOR_EACH_SSA_TREE_OPERAND (op
, last
, iter
, SSA_OP_USE
)
5852 register_edge_assert_for (op
, e
,
5853 gimple_cond_code (last
),
5854 gimple_cond_lhs (last
),
5855 gimple_cond_rhs (last
), asserts
);
5856 finish_register_edge_assert_for (e
, bsi
, asserts
);
5866 /* Compare two case labels sorting first by the destination bb index
5867 and then by the case value. */
5870 compare_case_labels (const void *p1
, const void *p2
)
5872 const struct case_info
*ci1
= (const struct case_info
*) p1
;
5873 const struct case_info
*ci2
= (const struct case_info
*) p2
;
5874 int idx1
= ci1
->bb
->index
;
5875 int idx2
= ci2
->bb
->index
;
5879 else if (idx1
== idx2
)
5881 /* Make sure the default label is first in a group. */
5882 if (!CASE_LOW (ci1
->expr
))
5884 else if (!CASE_LOW (ci2
->expr
))
5887 return tree_int_cst_compare (CASE_LOW (ci1
->expr
),
5888 CASE_LOW (ci2
->expr
));
5894 /* Determine whether the outgoing edges of BB should receive an
5895 ASSERT_EXPR for each of the operands of BB's LAST statement.
5896 The last statement of BB must be a SWITCH_EXPR.
5898 If any of the sub-graphs rooted at BB have an interesting use of
5899 the predicate operands, an assert location node is added to the
5900 list of assertions for the corresponding operands. */
5903 find_switch_asserts (basic_block bb
, gswitch
*last
)
5905 gimple_stmt_iterator bsi
;
5908 struct case_info
*ci
;
5909 size_t n
= gimple_switch_num_labels (last
);
5910 #if GCC_VERSION >= 4000
5913 /* Work around GCC 3.4 bug (PR 37086). */
5914 volatile unsigned int idx
;
5917 bsi
= gsi_for_stmt (last
);
5918 op
= gimple_switch_index (last
);
5919 if (TREE_CODE (op
) != SSA_NAME
)
5922 /* Build a vector of case labels sorted by destination label. */
5923 ci
= XNEWVEC (struct case_info
, n
);
5924 for (idx
= 0; idx
< n
; ++idx
)
5926 ci
[idx
].expr
= gimple_switch_label (last
, idx
);
5927 ci
[idx
].bb
= label_to_block (CASE_LABEL (ci
[idx
].expr
));
5929 edge default_edge
= find_edge (bb
, ci
[0].bb
);
5930 qsort (ci
, n
, sizeof (struct case_info
), compare_case_labels
);
5932 for (idx
= 0; idx
< n
; ++idx
)
5935 tree cl
= ci
[idx
].expr
;
5936 basic_block cbb
= ci
[idx
].bb
;
5938 min
= CASE_LOW (cl
);
5939 max
= CASE_HIGH (cl
);
5941 /* If there are multiple case labels with the same destination
5942 we need to combine them to a single value range for the edge. */
5943 if (idx
+ 1 < n
&& cbb
== ci
[idx
+ 1].bb
)
5945 /* Skip labels until the last of the group. */
5948 } while (idx
< n
&& cbb
== ci
[idx
].bb
);
5951 /* Pick up the maximum of the case label range. */
5952 if (CASE_HIGH (ci
[idx
].expr
))
5953 max
= CASE_HIGH (ci
[idx
].expr
);
5955 max
= CASE_LOW (ci
[idx
].expr
);
5958 /* Can't extract a useful assertion out of a range that includes the
5960 if (min
== NULL_TREE
)
5963 /* Find the edge to register the assert expr on. */
5964 e
= find_edge (bb
, cbb
);
5966 /* Register the necessary assertions for the operand in the
5968 auto_vec
<assert_info
, 8> asserts
;
5969 register_edge_assert_for (op
, e
,
5970 max
? GE_EXPR
: EQ_EXPR
,
5971 op
, fold_convert (TREE_TYPE (op
), min
),
5974 register_edge_assert_for (op
, e
, LE_EXPR
, op
,
5975 fold_convert (TREE_TYPE (op
), max
),
5977 finish_register_edge_assert_for (e
, bsi
, asserts
);
5982 if (!live_on_edge (default_edge
, op
))
5985 /* Now register along the default label assertions that correspond to the
5986 anti-range of each label. */
5987 int insertion_limit
= PARAM_VALUE (PARAM_MAX_VRP_SWITCH_ASSERTIONS
);
5988 if (insertion_limit
== 0)
5991 /* We can't do this if the default case shares a label with another case. */
5992 tree default_cl
= gimple_switch_default_label (last
);
5993 for (idx
= 1; idx
< n
; idx
++)
5996 tree cl
= gimple_switch_label (last
, idx
);
5997 if (CASE_LABEL (cl
) == CASE_LABEL (default_cl
))
6000 min
= CASE_LOW (cl
);
6001 max
= CASE_HIGH (cl
);
6003 /* Combine contiguous case ranges to reduce the number of assertions
6005 for (idx
= idx
+ 1; idx
< n
; idx
++)
6007 tree next_min
, next_max
;
6008 tree next_cl
= gimple_switch_label (last
, idx
);
6009 if (CASE_LABEL (next_cl
) == CASE_LABEL (default_cl
))
6012 next_min
= CASE_LOW (next_cl
);
6013 next_max
= CASE_HIGH (next_cl
);
6015 wide_int difference
= wi::sub (next_min
, max
? max
: min
);
6016 if (wi::eq_p (difference
, 1))
6017 max
= next_max
? next_max
: next_min
;
6023 if (max
== NULL_TREE
)
6025 /* Register the assertion OP != MIN. */
6026 auto_vec
<assert_info
, 8> asserts
;
6027 min
= fold_convert (TREE_TYPE (op
), min
);
6028 register_edge_assert_for (op
, default_edge
, NE_EXPR
, op
, min
,
6030 finish_register_edge_assert_for (default_edge
, bsi
, asserts
);
6034 /* Register the assertion (unsigned)OP - MIN > (MAX - MIN),
6035 which will give OP the anti-range ~[MIN,MAX]. */
6036 tree uop
= fold_convert (unsigned_type_for (TREE_TYPE (op
)), op
);
6037 min
= fold_convert (TREE_TYPE (uop
), min
);
6038 max
= fold_convert (TREE_TYPE (uop
), max
);
6040 tree lhs
= fold_build2 (MINUS_EXPR
, TREE_TYPE (uop
), uop
, min
);
6041 tree rhs
= int_const_binop (MINUS_EXPR
, max
, min
);
6042 register_new_assert_for (op
, lhs
, GT_EXPR
, rhs
,
6043 NULL
, default_edge
, bsi
);
6046 if (--insertion_limit
== 0)
6052 /* Traverse all the statements in block BB looking for statements that
6053 may generate useful assertions for the SSA names in their operand.
6054 If a statement produces a useful assertion A for name N_i, then the
6055 list of assertions already generated for N_i is scanned to
6056 determine if A is actually needed.
6058 If N_i already had the assertion A at a location dominating the
6059 current location, then nothing needs to be done. Otherwise, the
6060 new location for A is recorded instead.
6062 1- For every statement S in BB, all the variables used by S are
6063 added to bitmap FOUND_IN_SUBGRAPH.
6065 2- If statement S uses an operand N in a way that exposes a known
6066 value range for N, then if N was not already generated by an
6067 ASSERT_EXPR, create a new assert location for N. For instance,
6068 if N is a pointer and the statement dereferences it, we can
6069 assume that N is not NULL.
6071 3- COND_EXPRs are a special case of #2. We can derive range
6072 information from the predicate but need to insert different
6073 ASSERT_EXPRs for each of the sub-graphs rooted at the
6074 conditional block. If the last statement of BB is a conditional
6075 expression of the form 'X op Y', then
6077 a) Remove X and Y from the set FOUND_IN_SUBGRAPH.
6079 b) If the conditional is the only entry point to the sub-graph
6080 corresponding to the THEN_CLAUSE, recurse into it. On
6081 return, if X and/or Y are marked in FOUND_IN_SUBGRAPH, then
6082 an ASSERT_EXPR is added for the corresponding variable.
6084 c) Repeat step (b) on the ELSE_CLAUSE.
6086 d) Mark X and Y in FOUND_IN_SUBGRAPH.
6095 In this case, an assertion on the THEN clause is useful to
6096 determine that 'a' is always 9 on that edge. However, an assertion
6097 on the ELSE clause would be unnecessary.
6099 4- If BB does not end in a conditional expression, then we recurse
6100 into BB's dominator children.
6102 At the end of the recursive traversal, every SSA name will have a
6103 list of locations where ASSERT_EXPRs should be added. When a new
6104 location for name N is found, it is registered by calling
6105 register_new_assert_for. That function keeps track of all the
6106 registered assertions to prevent adding unnecessary assertions.
6107 For instance, if a pointer P_4 is dereferenced more than once in a
6108 dominator tree, only the location dominating all the dereference of
6109 P_4 will receive an ASSERT_EXPR. */
6112 find_assert_locations_1 (basic_block bb
, sbitmap live
)
6116 last
= last_stmt (bb
);
6118 /* If BB's last statement is a conditional statement involving integer
6119 operands, determine if we need to add ASSERT_EXPRs. */
6121 && gimple_code (last
) == GIMPLE_COND
6122 && !fp_predicate (last
)
6123 && !ZERO_SSA_OPERANDS (last
, SSA_OP_USE
))
6124 find_conditional_asserts (bb
, as_a
<gcond
*> (last
));
6126 /* If BB's last statement is a switch statement involving integer
6127 operands, determine if we need to add ASSERT_EXPRs. */
6129 && gimple_code (last
) == GIMPLE_SWITCH
6130 && !ZERO_SSA_OPERANDS (last
, SSA_OP_USE
))
6131 find_switch_asserts (bb
, as_a
<gswitch
*> (last
));
6133 /* Traverse all the statements in BB marking used names and looking
6134 for statements that may infer assertions for their used operands. */
6135 for (gimple_stmt_iterator si
= gsi_last_bb (bb
); !gsi_end_p (si
);
6142 stmt
= gsi_stmt (si
);
6144 if (is_gimple_debug (stmt
))
6147 /* See if we can derive an assertion for any of STMT's operands. */
6148 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, i
, SSA_OP_USE
)
6151 enum tree_code comp_code
;
6153 /* If op is not live beyond this stmt, do not bother to insert
6155 if (!bitmap_bit_p (live
, SSA_NAME_VERSION (op
)))
6158 /* If OP is used in such a way that we can infer a value
6159 range for it, and we don't find a previous assertion for
6160 it, create a new assertion location node for OP. */
6161 if (infer_value_range (stmt
, op
, &comp_code
, &value
))
6163 /* If we are able to infer a nonzero value range for OP,
6164 then walk backwards through the use-def chain to see if OP
6165 was set via a typecast.
6167 If so, then we can also infer a nonzero value range
6168 for the operand of the NOP_EXPR. */
6169 if (comp_code
== NE_EXPR
&& integer_zerop (value
))
6172 gimple
*def_stmt
= SSA_NAME_DEF_STMT (t
);
6174 while (is_gimple_assign (def_stmt
)
6175 && CONVERT_EXPR_CODE_P
6176 (gimple_assign_rhs_code (def_stmt
))
6178 (gimple_assign_rhs1 (def_stmt
)) == SSA_NAME
6180 (TREE_TYPE (gimple_assign_rhs1 (def_stmt
))))
6182 t
= gimple_assign_rhs1 (def_stmt
);
6183 def_stmt
= SSA_NAME_DEF_STMT (t
);
6185 /* Note we want to register the assert for the
6186 operand of the NOP_EXPR after SI, not after the
6188 if (bitmap_bit_p (live
, SSA_NAME_VERSION (t
)))
6189 register_new_assert_for (t
, t
, comp_code
, value
,
6194 register_new_assert_for (op
, op
, comp_code
, value
, bb
, NULL
, si
);
6199 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, i
, SSA_OP_USE
)
6200 bitmap_set_bit (live
, SSA_NAME_VERSION (op
));
6201 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, i
, SSA_OP_DEF
)
6202 bitmap_clear_bit (live
, SSA_NAME_VERSION (op
));
6205 /* Traverse all PHI nodes in BB, updating live. */
6206 for (gphi_iterator si
= gsi_start_phis (bb
); !gsi_end_p (si
);
6209 use_operand_p arg_p
;
6211 gphi
*phi
= si
.phi ();
6212 tree res
= gimple_phi_result (phi
);
6214 if (virtual_operand_p (res
))
6217 FOR_EACH_PHI_ARG (arg_p
, phi
, i
, SSA_OP_USE
)
6219 tree arg
= USE_FROM_PTR (arg_p
);
6220 if (TREE_CODE (arg
) == SSA_NAME
)
6221 bitmap_set_bit (live
, SSA_NAME_VERSION (arg
));
6224 bitmap_clear_bit (live
, SSA_NAME_VERSION (res
));
6228 /* Do an RPO walk over the function computing SSA name liveness
6229 on-the-fly and deciding on assert expressions to insert. */
6232 find_assert_locations (void)
6234 int *rpo
= XNEWVEC (int, last_basic_block_for_fn (cfun
));
6235 int *bb_rpo
= XNEWVEC (int, last_basic_block_for_fn (cfun
));
6236 int *last_rpo
= XCNEWVEC (int, last_basic_block_for_fn (cfun
));
6239 live
= XCNEWVEC (sbitmap
, last_basic_block_for_fn (cfun
));
6240 rpo_cnt
= pre_and_rev_post_order_compute (NULL
, rpo
, false);
6241 for (i
= 0; i
< rpo_cnt
; ++i
)
6244 /* Pre-seed loop latch liveness from loop header PHI nodes. Due to
6245 the order we compute liveness and insert asserts we otherwise
6246 fail to insert asserts into the loop latch. */
6248 FOR_EACH_LOOP (loop
, 0)
6250 i
= loop
->latch
->index
;
6251 unsigned int j
= single_succ_edge (loop
->latch
)->dest_idx
;
6252 for (gphi_iterator gsi
= gsi_start_phis (loop
->header
);
6253 !gsi_end_p (gsi
); gsi_next (&gsi
))
6255 gphi
*phi
= gsi
.phi ();
6256 if (virtual_operand_p (gimple_phi_result (phi
)))
6258 tree arg
= gimple_phi_arg_def (phi
, j
);
6259 if (TREE_CODE (arg
) == SSA_NAME
)
6261 if (live
[i
] == NULL
)
6263 live
[i
] = sbitmap_alloc (num_ssa_names
);
6264 bitmap_clear (live
[i
]);
6266 bitmap_set_bit (live
[i
], SSA_NAME_VERSION (arg
));
6271 for (i
= rpo_cnt
- 1; i
>= 0; --i
)
6273 basic_block bb
= BASIC_BLOCK_FOR_FN (cfun
, rpo
[i
]);
6279 live
[rpo
[i
]] = sbitmap_alloc (num_ssa_names
);
6280 bitmap_clear (live
[rpo
[i
]]);
6283 /* Process BB and update the live information with uses in
6285 find_assert_locations_1 (bb
, live
[rpo
[i
]]);
6287 /* Merge liveness into the predecessor blocks and free it. */
6288 if (!bitmap_empty_p (live
[rpo
[i
]]))
6291 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
6293 int pred
= e
->src
->index
;
6294 if ((e
->flags
& EDGE_DFS_BACK
) || pred
== ENTRY_BLOCK
)
6299 live
[pred
] = sbitmap_alloc (num_ssa_names
);
6300 bitmap_clear (live
[pred
]);
6302 bitmap_ior (live
[pred
], live
[pred
], live
[rpo
[i
]]);
6304 if (bb_rpo
[pred
] < pred_rpo
)
6305 pred_rpo
= bb_rpo
[pred
];
6308 /* Record the RPO number of the last visited block that needs
6309 live information from this block. */
6310 last_rpo
[rpo
[i
]] = pred_rpo
;
6314 sbitmap_free (live
[rpo
[i
]]);
6315 live
[rpo
[i
]] = NULL
;
6318 /* We can free all successors live bitmaps if all their
6319 predecessors have been visited already. */
6320 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
6321 if (last_rpo
[e
->dest
->index
] == i
6322 && live
[e
->dest
->index
])
6324 sbitmap_free (live
[e
->dest
->index
]);
6325 live
[e
->dest
->index
] = NULL
;
6330 XDELETEVEC (bb_rpo
);
6331 XDELETEVEC (last_rpo
);
6332 for (i
= 0; i
< last_basic_block_for_fn (cfun
); ++i
)
6334 sbitmap_free (live
[i
]);
6338 /* Create an ASSERT_EXPR for NAME and insert it in the location
6339 indicated by LOC. Return true if we made any edge insertions. */
6342 process_assert_insertions_for (tree name
, assert_locus
*loc
)
6344 /* Build the comparison expression NAME_i COMP_CODE VAL. */
6347 gimple
*assert_stmt
;
6351 /* If we have X <=> X do not insert an assert expr for that. */
6352 if (loc
->expr
== loc
->val
)
6355 cond
= build2 (loc
->comp_code
, boolean_type_node
, loc
->expr
, loc
->val
);
6356 assert_stmt
= build_assert_expr_for (cond
, name
);
6359 /* We have been asked to insert the assertion on an edge. This
6360 is used only by COND_EXPR and SWITCH_EXPR assertions. */
6361 gcc_checking_assert (gimple_code (gsi_stmt (loc
->si
)) == GIMPLE_COND
6362 || (gimple_code (gsi_stmt (loc
->si
))
6365 gsi_insert_on_edge (loc
->e
, assert_stmt
);
6369 /* If the stmt iterator points at the end then this is an insertion
6370 at the beginning of a block. */
6371 if (gsi_end_p (loc
->si
))
6373 gimple_stmt_iterator si
= gsi_after_labels (loc
->bb
);
6374 gsi_insert_before (&si
, assert_stmt
, GSI_SAME_STMT
);
6378 /* Otherwise, we can insert right after LOC->SI iff the
6379 statement must not be the last statement in the block. */
6380 stmt
= gsi_stmt (loc
->si
);
6381 if (!stmt_ends_bb_p (stmt
))
6383 gsi_insert_after (&loc
->si
, assert_stmt
, GSI_SAME_STMT
);
6387 /* If STMT must be the last statement in BB, we can only insert new
6388 assertions on the non-abnormal edge out of BB. Note that since
6389 STMT is not control flow, there may only be one non-abnormal/eh edge
6391 FOR_EACH_EDGE (e
, ei
, loc
->bb
->succs
)
6392 if (!(e
->flags
& (EDGE_ABNORMAL
|EDGE_EH
)))
6394 gsi_insert_on_edge (e
, assert_stmt
);
6401 /* Qsort helper for sorting assert locations. If stable is true, don't
6402 use iterative_hash_expr because it can be unstable for -fcompare-debug,
6403 on the other side some pointers might be NULL. */
6405 template <bool stable
>
6407 compare_assert_loc (const void *pa
, const void *pb
)
6409 assert_locus
* const a
= *(assert_locus
* const *)pa
;
6410 assert_locus
* const b
= *(assert_locus
* const *)pb
;
6412 /* If stable, some asserts might be optimized away already, sort
6422 if (a
->e
== NULL
&& b
->e
!= NULL
)
6424 else if (a
->e
!= NULL
&& b
->e
== NULL
)
6427 /* After the above checks, we know that (a->e == NULL) == (b->e == NULL),
6428 no need to test both a->e and b->e. */
6430 /* Sort after destination index. */
6433 else if (a
->e
->dest
->index
> b
->e
->dest
->index
)
6435 else if (a
->e
->dest
->index
< b
->e
->dest
->index
)
6438 /* Sort after comp_code. */
6439 if (a
->comp_code
> b
->comp_code
)
6441 else if (a
->comp_code
< b
->comp_code
)
6446 /* E.g. if a->val is ADDR_EXPR of a VAR_DECL, iterative_hash_expr
6447 uses DECL_UID of the VAR_DECL, so sorting might differ between
6448 -g and -g0. When doing the removal of redundant assert exprs
6449 and commonization to successors, this does not matter, but for
6450 the final sort needs to be stable. */
6458 ha
= iterative_hash_expr (a
->expr
, iterative_hash_expr (a
->val
, 0));
6459 hb
= iterative_hash_expr (b
->expr
, iterative_hash_expr (b
->val
, 0));
6462 /* Break the tie using hashing and source/bb index. */
6464 return (a
->e
!= NULL
6465 ? a
->e
->src
->index
- b
->e
->src
->index
6466 : a
->bb
->index
- b
->bb
->index
);
6467 return ha
> hb
? 1 : -1;
6470 /* Process all the insertions registered for every name N_i registered
6471 in NEED_ASSERT_FOR. The list of assertions to be inserted are
6472 found in ASSERTS_FOR[i]. */
6475 process_assert_insertions (void)
6479 bool update_edges_p
= false;
6480 int num_asserts
= 0;
6482 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6483 dump_all_asserts (dump_file
);
6485 EXECUTE_IF_SET_IN_BITMAP (need_assert_for
, 0, i
, bi
)
6487 assert_locus
*loc
= asserts_for
[i
];
6490 auto_vec
<assert_locus
*, 16> asserts
;
6491 for (; loc
; loc
= loc
->next
)
6492 asserts
.safe_push (loc
);
6493 asserts
.qsort (compare_assert_loc
<false>);
6495 /* Push down common asserts to successors and remove redundant ones. */
6497 assert_locus
*common
= NULL
;
6498 unsigned commonj
= 0;
6499 for (unsigned j
= 0; j
< asserts
.length (); ++j
)
6505 || loc
->e
->dest
!= common
->e
->dest
6506 || loc
->comp_code
!= common
->comp_code
6507 || ! operand_equal_p (loc
->val
, common
->val
, 0)
6508 || ! operand_equal_p (loc
->expr
, common
->expr
, 0))
6514 else if (loc
->e
== asserts
[j
-1]->e
)
6516 /* Remove duplicate asserts. */
6517 if (commonj
== j
- 1)
6522 free (asserts
[j
-1]);
6523 asserts
[j
-1] = NULL
;
6528 if (EDGE_COUNT (common
->e
->dest
->preds
) == ecnt
)
6530 /* We have the same assertion on all incoming edges of a BB.
6531 Insert it at the beginning of that block. */
6532 loc
->bb
= loc
->e
->dest
;
6534 loc
->si
= gsi_none ();
6536 /* Clear asserts commoned. */
6537 for (; commonj
!= j
; ++commonj
)
6538 if (asserts
[commonj
])
6540 free (asserts
[commonj
]);
6541 asserts
[commonj
] = NULL
;
6547 /* The asserts vector sorting above might be unstable for
6548 -fcompare-debug, sort again to ensure a stable sort. */
6549 asserts
.qsort (compare_assert_loc
<true>);
6550 for (unsigned j
= 0; j
< asserts
.length (); ++j
)
6555 update_edges_p
|= process_assert_insertions_for (ssa_name (i
), loc
);
6562 gsi_commit_edge_inserts ();
6564 statistics_counter_event (cfun
, "Number of ASSERT_EXPR expressions inserted",
6569 /* Traverse the flowgraph looking for conditional jumps to insert range
6570 expressions. These range expressions are meant to provide information
6571 to optimizations that need to reason in terms of value ranges. They
6572 will not be expanded into RTL. For instance, given:
6581 this pass will transform the code into:
6587 x = ASSERT_EXPR <x, x < y>
6592 y = ASSERT_EXPR <y, x >= y>
6596 The idea is that once copy and constant propagation have run, other
6597 optimizations will be able to determine what ranges of values can 'x'
6598 take in different paths of the code, simply by checking the reaching
6599 definition of 'x'. */
6602 insert_range_assertions (void)
6604 need_assert_for
= BITMAP_ALLOC (NULL
);
6605 asserts_for
= XCNEWVEC (assert_locus
*, num_ssa_names
);
6607 calculate_dominance_info (CDI_DOMINATORS
);
6609 find_assert_locations ();
6610 if (!bitmap_empty_p (need_assert_for
))
6612 process_assert_insertions ();
6613 update_ssa (TODO_update_ssa_no_phi
);
6616 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6618 fprintf (dump_file
, "\nSSA form after inserting ASSERT_EXPRs\n");
6619 dump_function_to_file (current_function_decl
, dump_file
, dump_flags
);
6623 BITMAP_FREE (need_assert_for
);
6626 /* Checks one ARRAY_REF in REF, located at LOCUS. Ignores flexible arrays
6627 and "struct" hacks. If VRP can determine that the
6628 array subscript is a constant, check if it is outside valid
6629 range. If the array subscript is a RANGE, warn if it is
6630 non-overlapping with valid range.
6631 IGNORE_OFF_BY_ONE is true if the ARRAY_REF is inside a ADDR_EXPR. */
6634 check_array_ref (location_t location
, tree ref
, bool ignore_off_by_one
)
6636 value_range
*vr
= NULL
;
6637 tree low_sub
, up_sub
;
6638 tree low_bound
, up_bound
, up_bound_p1
;
6640 if (TREE_NO_WARNING (ref
))
6643 low_sub
= up_sub
= TREE_OPERAND (ref
, 1);
6644 up_bound
= array_ref_up_bound (ref
);
6646 /* Can not check flexible arrays. */
6648 || TREE_CODE (up_bound
) != INTEGER_CST
)
6651 /* Accesses to trailing arrays via pointers may access storage
6652 beyond the types array bounds. */
6653 if (warn_array_bounds
< 2
6654 && array_at_struct_end_p (ref
))
6657 low_bound
= array_ref_low_bound (ref
);
6658 up_bound_p1
= int_const_binop (PLUS_EXPR
, up_bound
,
6659 build_int_cst (TREE_TYPE (up_bound
), 1));
6662 if (tree_int_cst_equal (low_bound
, up_bound_p1
))
6664 warning_at (location
, OPT_Warray_bounds
,
6665 "array subscript is above array bounds");
6666 TREE_NO_WARNING (ref
) = 1;
6669 if (TREE_CODE (low_sub
) == SSA_NAME
)
6671 vr
= get_value_range (low_sub
);
6672 if (vr
->type
== VR_RANGE
|| vr
->type
== VR_ANTI_RANGE
)
6674 low_sub
= vr
->type
== VR_RANGE
? vr
->max
: vr
->min
;
6675 up_sub
= vr
->type
== VR_RANGE
? vr
->min
: vr
->max
;
6679 if (vr
&& vr
->type
== VR_ANTI_RANGE
)
6681 if (TREE_CODE (up_sub
) == INTEGER_CST
6682 && (ignore_off_by_one
6683 ? tree_int_cst_lt (up_bound
, up_sub
)
6684 : tree_int_cst_le (up_bound
, up_sub
))
6685 && TREE_CODE (low_sub
) == INTEGER_CST
6686 && tree_int_cst_le (low_sub
, low_bound
))
6688 warning_at (location
, OPT_Warray_bounds
,
6689 "array subscript is outside array bounds");
6690 TREE_NO_WARNING (ref
) = 1;
6693 else if (TREE_CODE (up_sub
) == INTEGER_CST
6694 && (ignore_off_by_one
6695 ? !tree_int_cst_le (up_sub
, up_bound_p1
)
6696 : !tree_int_cst_le (up_sub
, up_bound
)))
6698 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6700 fprintf (dump_file
, "Array bound warning for ");
6701 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, ref
);
6702 fprintf (dump_file
, "\n");
6704 warning_at (location
, OPT_Warray_bounds
,
6705 "array subscript is above array bounds");
6706 TREE_NO_WARNING (ref
) = 1;
6708 else if (TREE_CODE (low_sub
) == INTEGER_CST
6709 && tree_int_cst_lt (low_sub
, low_bound
))
6711 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6713 fprintf (dump_file
, "Array bound warning for ");
6714 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, ref
);
6715 fprintf (dump_file
, "\n");
6717 warning_at (location
, OPT_Warray_bounds
,
6718 "array subscript is below array bounds");
6719 TREE_NO_WARNING (ref
) = 1;
6723 /* Searches if the expr T, located at LOCATION computes
6724 address of an ARRAY_REF, and call check_array_ref on it. */
6727 search_for_addr_array (tree t
, location_t location
)
6729 /* Check each ARRAY_REFs in the reference chain. */
6732 if (TREE_CODE (t
) == ARRAY_REF
)
6733 check_array_ref (location
, t
, true /*ignore_off_by_one*/);
6735 t
= TREE_OPERAND (t
, 0);
6737 while (handled_component_p (t
));
6739 if (TREE_CODE (t
) == MEM_REF
6740 && TREE_CODE (TREE_OPERAND (t
, 0)) == ADDR_EXPR
6741 && !TREE_NO_WARNING (t
))
6743 tree tem
= TREE_OPERAND (TREE_OPERAND (t
, 0), 0);
6744 tree low_bound
, up_bound
, el_sz
;
6746 if (TREE_CODE (TREE_TYPE (tem
)) != ARRAY_TYPE
6747 || TREE_CODE (TREE_TYPE (TREE_TYPE (tem
))) == ARRAY_TYPE
6748 || !TYPE_DOMAIN (TREE_TYPE (tem
)))
6751 low_bound
= TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (tem
)));
6752 up_bound
= TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (tem
)));
6753 el_sz
= TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (tem
)));
6755 || TREE_CODE (low_bound
) != INTEGER_CST
6757 || TREE_CODE (up_bound
) != INTEGER_CST
6759 || TREE_CODE (el_sz
) != INTEGER_CST
)
6762 idx
= mem_ref_offset (t
);
6763 idx
= wi::sdiv_trunc (idx
, wi::to_offset (el_sz
));
6766 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6768 fprintf (dump_file
, "Array bound warning for ");
6769 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, t
);
6770 fprintf (dump_file
, "\n");
6772 warning_at (location
, OPT_Warray_bounds
,
6773 "array subscript is below array bounds");
6774 TREE_NO_WARNING (t
) = 1;
6776 else if (idx
> (wi::to_offset (up_bound
)
6777 - wi::to_offset (low_bound
) + 1))
6779 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6781 fprintf (dump_file
, "Array bound warning for ");
6782 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, t
);
6783 fprintf (dump_file
, "\n");
6785 warning_at (location
, OPT_Warray_bounds
,
6786 "array subscript is above array bounds");
6787 TREE_NO_WARNING (t
) = 1;
6792 /* walk_tree() callback that checks if *TP is
6793 an ARRAY_REF inside an ADDR_EXPR (in which an array
6794 subscript one outside the valid range is allowed). Call
6795 check_array_ref for each ARRAY_REF found. The location is
6799 check_array_bounds (tree
*tp
, int *walk_subtree
, void *data
)
6802 struct walk_stmt_info
*wi
= (struct walk_stmt_info
*) data
;
6803 location_t location
;
6805 if (EXPR_HAS_LOCATION (t
))
6806 location
= EXPR_LOCATION (t
);
6809 location_t
*locp
= (location_t
*) wi
->info
;
6813 *walk_subtree
= TRUE
;
6815 if (TREE_CODE (t
) == ARRAY_REF
)
6816 check_array_ref (location
, t
, false /*ignore_off_by_one*/);
6818 else if (TREE_CODE (t
) == ADDR_EXPR
)
6820 search_for_addr_array (t
, location
);
6821 *walk_subtree
= FALSE
;
6827 /* Walk over all statements of all reachable BBs and call check_array_bounds
6831 check_all_array_refs (void)
6834 gimple_stmt_iterator si
;
6836 FOR_EACH_BB_FN (bb
, cfun
)
6840 bool executable
= false;
6842 /* Skip blocks that were found to be unreachable. */
6843 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
6844 executable
|= !!(e
->flags
& EDGE_EXECUTABLE
);
6848 for (si
= gsi_start_bb (bb
); !gsi_end_p (si
); gsi_next (&si
))
6850 gimple
*stmt
= gsi_stmt (si
);
6851 struct walk_stmt_info wi
;
6852 if (!gimple_has_location (stmt
)
6853 || is_gimple_debug (stmt
))
6856 memset (&wi
, 0, sizeof (wi
));
6858 location_t loc
= gimple_location (stmt
);
6861 walk_gimple_op (gsi_stmt (si
),
6868 /* Return true if all imm uses of VAR are either in STMT, or
6869 feed (optionally through a chain of single imm uses) GIMPLE_COND
6870 in basic block COND_BB. */
6873 all_imm_uses_in_stmt_or_feed_cond (tree var
, gimple
*stmt
, basic_block cond_bb
)
6875 use_operand_p use_p
, use2_p
;
6876 imm_use_iterator iter
;
6878 FOR_EACH_IMM_USE_FAST (use_p
, iter
, var
)
6879 if (USE_STMT (use_p
) != stmt
)
6881 gimple
*use_stmt
= USE_STMT (use_p
), *use_stmt2
;
6882 if (is_gimple_debug (use_stmt
))
6884 while (is_gimple_assign (use_stmt
)
6885 && TREE_CODE (gimple_assign_lhs (use_stmt
)) == SSA_NAME
6886 && single_imm_use (gimple_assign_lhs (use_stmt
),
6887 &use2_p
, &use_stmt2
))
6888 use_stmt
= use_stmt2
;
6889 if (gimple_code (use_stmt
) != GIMPLE_COND
6890 || gimple_bb (use_stmt
) != cond_bb
)
6903 __builtin_unreachable ();
6905 x_5 = ASSERT_EXPR <x_3, ...>;
6906 If x_3 has no other immediate uses (checked by caller),
6907 var is the x_3 var from ASSERT_EXPR, we can clear low 5 bits
6908 from the non-zero bitmask. */
6911 maybe_set_nonzero_bits (basic_block bb
, tree var
)
6913 edge e
= single_pred_edge (bb
);
6914 basic_block cond_bb
= e
->src
;
6915 gimple
*stmt
= last_stmt (cond_bb
);
6919 || gimple_code (stmt
) != GIMPLE_COND
6920 || gimple_cond_code (stmt
) != ((e
->flags
& EDGE_TRUE_VALUE
)
6921 ? EQ_EXPR
: NE_EXPR
)
6922 || TREE_CODE (gimple_cond_lhs (stmt
)) != SSA_NAME
6923 || !integer_zerop (gimple_cond_rhs (stmt
)))
6926 stmt
= SSA_NAME_DEF_STMT (gimple_cond_lhs (stmt
));
6927 if (!is_gimple_assign (stmt
)
6928 || gimple_assign_rhs_code (stmt
) != BIT_AND_EXPR
6929 || TREE_CODE (gimple_assign_rhs2 (stmt
)) != INTEGER_CST
)
6931 if (gimple_assign_rhs1 (stmt
) != var
)
6935 if (TREE_CODE (gimple_assign_rhs1 (stmt
)) != SSA_NAME
)
6937 stmt2
= SSA_NAME_DEF_STMT (gimple_assign_rhs1 (stmt
));
6938 if (!gimple_assign_cast_p (stmt2
)
6939 || gimple_assign_rhs1 (stmt2
) != var
6940 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (stmt2
))
6941 || (TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (stmt
)))
6942 != TYPE_PRECISION (TREE_TYPE (var
))))
6945 cst
= gimple_assign_rhs2 (stmt
);
6946 set_nonzero_bits (var
, wi::bit_and_not (get_nonzero_bits (var
), cst
));
6949 /* Convert range assertion expressions into the implied copies and
6950 copy propagate away the copies. Doing the trivial copy propagation
6951 here avoids the need to run the full copy propagation pass after
6954 FIXME, this will eventually lead to copy propagation removing the
6955 names that had useful range information attached to them. For
6956 instance, if we had the assertion N_i = ASSERT_EXPR <N_j, N_j > 3>,
6957 then N_i will have the range [3, +INF].
6959 However, by converting the assertion into the implied copy
6960 operation N_i = N_j, we will then copy-propagate N_j into the uses
6961 of N_i and lose the range information. We may want to hold on to
6962 ASSERT_EXPRs a little while longer as the ranges could be used in
6963 things like jump threading.
6965 The problem with keeping ASSERT_EXPRs around is that passes after
6966 VRP need to handle them appropriately.
6968 Another approach would be to make the range information a first
6969 class property of the SSA_NAME so that it can be queried from
6970 any pass. This is made somewhat more complex by the need for
6971 multiple ranges to be associated with one SSA_NAME. */
6974 remove_range_assertions (void)
6977 gimple_stmt_iterator si
;
6978 /* 1 if looking at ASSERT_EXPRs immediately at the beginning of
6979 a basic block preceeded by GIMPLE_COND branching to it and
6980 __builtin_trap, -1 if not yet checked, 0 otherwise. */
6983 /* Note that the BSI iterator bump happens at the bottom of the
6984 loop and no bump is necessary if we're removing the statement
6985 referenced by the current BSI. */
6986 FOR_EACH_BB_FN (bb
, cfun
)
6987 for (si
= gsi_after_labels (bb
), is_unreachable
= -1; !gsi_end_p (si
);)
6989 gimple
*stmt
= gsi_stmt (si
);
6991 if (is_gimple_assign (stmt
)
6992 && gimple_assign_rhs_code (stmt
) == ASSERT_EXPR
)
6994 tree lhs
= gimple_assign_lhs (stmt
);
6995 tree rhs
= gimple_assign_rhs1 (stmt
);
6998 var
= ASSERT_EXPR_VAR (rhs
);
7000 if (TREE_CODE (var
) == SSA_NAME
7001 && !POINTER_TYPE_P (TREE_TYPE (lhs
))
7002 && SSA_NAME_RANGE_INFO (lhs
))
7004 if (is_unreachable
== -1)
7007 if (single_pred_p (bb
)
7008 && assert_unreachable_fallthru_edge_p
7009 (single_pred_edge (bb
)))
7013 if (x_7 >= 10 && x_7 < 20)
7014 __builtin_unreachable ();
7015 x_8 = ASSERT_EXPR <x_7, ...>;
7016 if the only uses of x_7 are in the ASSERT_EXPR and
7017 in the condition. In that case, we can copy the
7018 range info from x_8 computed in this pass also
7021 && all_imm_uses_in_stmt_or_feed_cond (var
, stmt
,
7024 set_range_info (var
, SSA_NAME_RANGE_TYPE (lhs
),
7025 SSA_NAME_RANGE_INFO (lhs
)->get_min (),
7026 SSA_NAME_RANGE_INFO (lhs
)->get_max ());
7027 maybe_set_nonzero_bits (bb
, var
);
7031 /* Propagate the RHS into every use of the LHS. For SSA names
7032 also propagate abnormals as it merely restores the original
7033 IL in this case (an replace_uses_by would assert). */
7034 if (TREE_CODE (var
) == SSA_NAME
)
7036 imm_use_iterator iter
;
7037 use_operand_p use_p
;
7039 FOR_EACH_IMM_USE_STMT (use_stmt
, iter
, lhs
)
7040 FOR_EACH_IMM_USE_ON_STMT (use_p
, iter
)
7041 SET_USE (use_p
, var
);
7044 replace_uses_by (lhs
, var
);
7046 /* And finally, remove the copy, it is not needed. */
7047 gsi_remove (&si
, true);
7048 release_defs (stmt
);
7052 if (!is_gimple_debug (gsi_stmt (si
)))
7060 /* Return true if STMT is interesting for VRP. */
7063 stmt_interesting_for_vrp (gimple
*stmt
)
7065 if (gimple_code (stmt
) == GIMPLE_PHI
)
7067 tree res
= gimple_phi_result (stmt
);
7068 return (!virtual_operand_p (res
)
7069 && (INTEGRAL_TYPE_P (TREE_TYPE (res
))
7070 || POINTER_TYPE_P (TREE_TYPE (res
))));
7072 else if (is_gimple_assign (stmt
) || is_gimple_call (stmt
))
7074 tree lhs
= gimple_get_lhs (stmt
);
7076 /* In general, assignments with virtual operands are not useful
7077 for deriving ranges, with the obvious exception of calls to
7078 builtin functions. */
7079 if (lhs
&& TREE_CODE (lhs
) == SSA_NAME
7080 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs
))
7081 || POINTER_TYPE_P (TREE_TYPE (lhs
)))
7082 && (is_gimple_call (stmt
)
7083 || !gimple_vuse (stmt
)))
7085 else if (is_gimple_call (stmt
) && gimple_call_internal_p (stmt
))
7086 switch (gimple_call_internal_fn (stmt
))
7088 case IFN_ADD_OVERFLOW
:
7089 case IFN_SUB_OVERFLOW
:
7090 case IFN_MUL_OVERFLOW
:
7091 case IFN_ATOMIC_COMPARE_EXCHANGE
:
7092 /* These internal calls return _Complex integer type,
7093 but are interesting to VRP nevertheless. */
7094 if (lhs
&& TREE_CODE (lhs
) == SSA_NAME
)
7101 else if (gimple_code (stmt
) == GIMPLE_COND
7102 || gimple_code (stmt
) == GIMPLE_SWITCH
)
7108 /* Initialize VRP lattice. */
7111 vrp_initialize_lattice ()
7113 values_propagated
= false;
7114 num_vr_values
= num_ssa_names
;
7115 vr_value
= XCNEWVEC (value_range
*, num_vr_values
);
7116 vr_phi_edge_counts
= XCNEWVEC (int, num_ssa_names
);
7117 bitmap_obstack_initialize (&vrp_equiv_obstack
);
7120 /* Initialization required by ssa_propagate engine. */
7127 FOR_EACH_BB_FN (bb
, cfun
)
7129 for (gphi_iterator si
= gsi_start_phis (bb
); !gsi_end_p (si
);
7132 gphi
*phi
= si
.phi ();
7133 if (!stmt_interesting_for_vrp (phi
))
7135 tree lhs
= PHI_RESULT (phi
);
7136 set_value_range_to_varying (get_value_range (lhs
));
7137 prop_set_simulate_again (phi
, false);
7140 prop_set_simulate_again (phi
, true);
7143 for (gimple_stmt_iterator si
= gsi_start_bb (bb
); !gsi_end_p (si
);
7146 gimple
*stmt
= gsi_stmt (si
);
7148 /* If the statement is a control insn, then we do not
7149 want to avoid simulating the statement once. Failure
7150 to do so means that those edges will never get added. */
7151 if (stmt_ends_bb_p (stmt
))
7152 prop_set_simulate_again (stmt
, true);
7153 else if (!stmt_interesting_for_vrp (stmt
))
7155 set_defs_to_varying (stmt
);
7156 prop_set_simulate_again (stmt
, false);
7159 prop_set_simulate_again (stmt
, true);
7164 /* Return the singleton value-range for NAME or NAME. */
7167 vrp_valueize (tree name
)
7169 if (TREE_CODE (name
) == SSA_NAME
)
7171 value_range
*vr
= get_value_range (name
);
7172 if (vr
->type
== VR_RANGE
7173 && (TREE_CODE (vr
->min
) == SSA_NAME
7174 || is_gimple_min_invariant (vr
->min
))
7175 && vrp_operand_equal_p (vr
->min
, vr
->max
))
7181 /* Return the singleton value-range for NAME if that is a constant
7182 but signal to not follow SSA edges. */
7185 vrp_valueize_1 (tree name
)
7187 if (TREE_CODE (name
) == SSA_NAME
)
7189 /* If the definition may be simulated again we cannot follow
7190 this SSA edge as the SSA propagator does not necessarily
7191 re-visit the use. */
7192 gimple
*def_stmt
= SSA_NAME_DEF_STMT (name
);
7193 if (!gimple_nop_p (def_stmt
)
7194 && prop_simulate_again_p (def_stmt
))
7196 value_range
*vr
= get_value_range (name
);
7197 if (range_int_cst_singleton_p (vr
))
7203 /* Visit assignment STMT. If it produces an interesting range, record
7204 the range in VR and set LHS to OUTPUT_P. */
7207 vrp_visit_assignment_or_call (gimple
*stmt
, tree
*output_p
, value_range
*vr
)
7210 enum gimple_code code
= gimple_code (stmt
);
7211 lhs
= gimple_get_lhs (stmt
);
7212 *output_p
= NULL_TREE
;
7214 /* We only keep track of ranges in integral and pointer types. */
7215 if (TREE_CODE (lhs
) == SSA_NAME
7216 && ((INTEGRAL_TYPE_P (TREE_TYPE (lhs
))
7217 /* It is valid to have NULL MIN/MAX values on a type. See
7218 build_range_type. */
7219 && TYPE_MIN_VALUE (TREE_TYPE (lhs
))
7220 && TYPE_MAX_VALUE (TREE_TYPE (lhs
)))
7221 || POINTER_TYPE_P (TREE_TYPE (lhs
))))
7225 /* Try folding the statement to a constant first. */
7226 tree tem
= gimple_fold_stmt_to_constant_1 (stmt
, vrp_valueize
,
7230 if (TREE_CODE (tem
) == SSA_NAME
7231 && (SSA_NAME_IS_DEFAULT_DEF (tem
)
7232 || ! prop_simulate_again_p (SSA_NAME_DEF_STMT (tem
))))
7234 extract_range_from_ssa_name (vr
, tem
);
7237 else if (is_gimple_min_invariant (tem
))
7239 set_value_range_to_value (vr
, tem
, NULL
);
7243 /* Then dispatch to value-range extracting functions. */
7244 if (code
== GIMPLE_CALL
)
7245 extract_range_basic (vr
, stmt
);
7247 extract_range_from_assignment (vr
, as_a
<gassign
*> (stmt
));
7251 /* Helper that gets the value range of the SSA_NAME with version I
7252 or a symbolic range containing the SSA_NAME only if the value range
7253 is varying or undefined. */
7255 static inline value_range
7256 get_vr_for_comparison (int i
)
7258 value_range vr
= *get_value_range (ssa_name (i
));
7260 /* If name N_i does not have a valid range, use N_i as its own
7261 range. This allows us to compare against names that may
7262 have N_i in their ranges. */
7263 if (vr
.type
== VR_VARYING
|| vr
.type
== VR_UNDEFINED
)
7266 vr
.min
= ssa_name (i
);
7267 vr
.max
= ssa_name (i
);
7273 /* Compare all the value ranges for names equivalent to VAR with VAL
7274 using comparison code COMP. Return the same value returned by
7275 compare_range_with_value, including the setting of
7276 *STRICT_OVERFLOW_P. */
7279 compare_name_with_value (enum tree_code comp
, tree var
, tree val
,
7280 bool *strict_overflow_p
, bool use_equiv_p
)
7286 int used_strict_overflow
;
7288 value_range equiv_vr
;
7290 /* Get the set of equivalences for VAR. */
7291 e
= get_value_range (var
)->equiv
;
7293 /* Start at -1. Set it to 0 if we do a comparison without relying
7294 on overflow, or 1 if all comparisons rely on overflow. */
7295 used_strict_overflow
= -1;
7297 /* Compare vars' value range with val. */
7298 equiv_vr
= get_vr_for_comparison (SSA_NAME_VERSION (var
));
7300 retval
= compare_range_with_value (comp
, &equiv_vr
, val
, &sop
);
7302 used_strict_overflow
= sop
? 1 : 0;
7304 /* If the equiv set is empty we have done all work we need to do. */
7308 && used_strict_overflow
> 0)
7309 *strict_overflow_p
= true;
7313 EXECUTE_IF_SET_IN_BITMAP (e
, 0, i
, bi
)
7315 tree name
= ssa_name (i
);
7320 && ! SSA_NAME_IS_DEFAULT_DEF (name
)
7321 && prop_simulate_again_p (SSA_NAME_DEF_STMT (name
)))
7324 equiv_vr
= get_vr_for_comparison (i
);
7326 t
= compare_range_with_value (comp
, &equiv_vr
, val
, &sop
);
7329 /* If we get different answers from different members
7330 of the equivalence set this check must be in a dead
7331 code region. Folding it to a trap representation
7332 would be correct here. For now just return don't-know. */
7342 used_strict_overflow
= 0;
7343 else if (used_strict_overflow
< 0)
7344 used_strict_overflow
= 1;
7349 && used_strict_overflow
> 0)
7350 *strict_overflow_p
= true;
7356 /* Given a comparison code COMP and names N1 and N2, compare all the
7357 ranges equivalent to N1 against all the ranges equivalent to N2
7358 to determine the value of N1 COMP N2. Return the same value
7359 returned by compare_ranges. Set *STRICT_OVERFLOW_P to indicate
7360 whether we relied on undefined signed overflow in the comparison. */
7364 compare_names (enum tree_code comp
, tree n1
, tree n2
,
7365 bool *strict_overflow_p
)
7369 bitmap_iterator bi1
, bi2
;
7371 int used_strict_overflow
;
7372 static bitmap_obstack
*s_obstack
= NULL
;
7373 static bitmap s_e1
= NULL
, s_e2
= NULL
;
7375 /* Compare the ranges of every name equivalent to N1 against the
7376 ranges of every name equivalent to N2. */
7377 e1
= get_value_range (n1
)->equiv
;
7378 e2
= get_value_range (n2
)->equiv
;
7380 /* Use the fake bitmaps if e1 or e2 are not available. */
7381 if (s_obstack
== NULL
)
7383 s_obstack
= XNEW (bitmap_obstack
);
7384 bitmap_obstack_initialize (s_obstack
);
7385 s_e1
= BITMAP_ALLOC (s_obstack
);
7386 s_e2
= BITMAP_ALLOC (s_obstack
);
7393 /* Add N1 and N2 to their own set of equivalences to avoid
7394 duplicating the body of the loop just to check N1 and N2
7396 bitmap_set_bit (e1
, SSA_NAME_VERSION (n1
));
7397 bitmap_set_bit (e2
, SSA_NAME_VERSION (n2
));
7399 /* If the equivalence sets have a common intersection, then the two
7400 names can be compared without checking their ranges. */
7401 if (bitmap_intersect_p (e1
, e2
))
7403 bitmap_clear_bit (e1
, SSA_NAME_VERSION (n1
));
7404 bitmap_clear_bit (e2
, SSA_NAME_VERSION (n2
));
7406 return (comp
== EQ_EXPR
|| comp
== GE_EXPR
|| comp
== LE_EXPR
)
7408 : boolean_false_node
;
7411 /* Start at -1. Set it to 0 if we do a comparison without relying
7412 on overflow, or 1 if all comparisons rely on overflow. */
7413 used_strict_overflow
= -1;
7415 /* Otherwise, compare all the equivalent ranges. First, add N1 and
7416 N2 to their own set of equivalences to avoid duplicating the body
7417 of the loop just to check N1 and N2 ranges. */
7418 EXECUTE_IF_SET_IN_BITMAP (e1
, 0, i1
, bi1
)
7420 if (! ssa_name (i1
))
7423 value_range vr1
= get_vr_for_comparison (i1
);
7425 t
= retval
= NULL_TREE
;
7426 EXECUTE_IF_SET_IN_BITMAP (e2
, 0, i2
, bi2
)
7428 if (! ssa_name (i2
))
7433 value_range vr2
= get_vr_for_comparison (i2
);
7435 t
= compare_ranges (comp
, &vr1
, &vr2
, &sop
);
7438 /* If we get different answers from different members
7439 of the equivalence set this check must be in a dead
7440 code region. Folding it to a trap representation
7441 would be correct here. For now just return don't-know. */
7445 bitmap_clear_bit (e1
, SSA_NAME_VERSION (n1
));
7446 bitmap_clear_bit (e2
, SSA_NAME_VERSION (n2
));
7452 used_strict_overflow
= 0;
7453 else if (used_strict_overflow
< 0)
7454 used_strict_overflow
= 1;
7460 bitmap_clear_bit (e1
, SSA_NAME_VERSION (n1
));
7461 bitmap_clear_bit (e2
, SSA_NAME_VERSION (n2
));
7462 if (used_strict_overflow
> 0)
7463 *strict_overflow_p
= true;
7468 /* None of the equivalent ranges are useful in computing this
7470 bitmap_clear_bit (e1
, SSA_NAME_VERSION (n1
));
7471 bitmap_clear_bit (e2
, SSA_NAME_VERSION (n2
));
7475 /* Helper function for vrp_evaluate_conditional_warnv & other
7479 vrp_evaluate_conditional_warnv_with_ops_using_ranges (enum tree_code code
,
7481 bool * strict_overflow_p
)
7483 value_range
*vr0
, *vr1
;
7485 vr0
= (TREE_CODE (op0
) == SSA_NAME
) ? get_value_range (op0
) : NULL
;
7486 vr1
= (TREE_CODE (op1
) == SSA_NAME
) ? get_value_range (op1
) : NULL
;
7488 tree res
= NULL_TREE
;
7490 res
= compare_ranges (code
, vr0
, vr1
, strict_overflow_p
);
7492 res
= compare_range_with_value (code
, vr0
, op1
, strict_overflow_p
);
7494 res
= (compare_range_with_value
7495 (swap_tree_comparison (code
), vr1
, op0
, strict_overflow_p
));
7499 /* Helper function for vrp_evaluate_conditional_warnv. */
7502 vrp_evaluate_conditional_warnv_with_ops (enum tree_code code
, tree op0
,
7503 tree op1
, bool use_equiv_p
,
7504 bool *strict_overflow_p
, bool *only_ranges
)
7508 *only_ranges
= true;
7510 /* We only deal with integral and pointer types. */
7511 if (!INTEGRAL_TYPE_P (TREE_TYPE (op0
))
7512 && !POINTER_TYPE_P (TREE_TYPE (op0
)))
7515 /* If OP0 CODE OP1 is an overflow comparison, if it can be expressed
7516 as a simple equality test, then prefer that over its current form
7519 An overflow test which collapses to an equality test can always be
7520 expressed as a comparison of one argument against zero. Overflow
7521 occurs when the chosen argument is zero and does not occur if the
7522 chosen argument is not zero. */
7524 if (overflow_comparison_p (code
, op0
, op1
, use_equiv_p
, &x
))
7526 wide_int max
= wi::max_value (TYPE_PRECISION (TREE_TYPE (op0
)), UNSIGNED
);
7527 /* B = A - 1; if (A < B) -> B = A - 1; if (A == 0)
7528 B = A - 1; if (A > B) -> B = A - 1; if (A != 0)
7529 B = A + 1; if (B < A) -> B = A + 1; if (B == 0)
7530 B = A + 1; if (B > A) -> B = A + 1; if (B != 0) */
7531 if (integer_zerop (x
))
7534 code
= (code
== LT_EXPR
|| code
== LE_EXPR
) ? EQ_EXPR
: NE_EXPR
;
7536 /* B = A + 1; if (A > B) -> B = A + 1; if (B == 0)
7537 B = A + 1; if (A < B) -> B = A + 1; if (B != 0)
7538 B = A - 1; if (B > A) -> B = A - 1; if (A == 0)
7539 B = A - 1; if (B < A) -> B = A - 1; if (A != 0) */
7540 else if (wi::eq_p (x
, max
- 1))
7543 op1
= wide_int_to_tree (TREE_TYPE (op0
), 0);
7544 code
= (code
== GT_EXPR
|| code
== GE_EXPR
) ? EQ_EXPR
: NE_EXPR
;
7548 if ((ret
= vrp_evaluate_conditional_warnv_with_ops_using_ranges
7549 (code
, op0
, op1
, strict_overflow_p
)))
7552 *only_ranges
= false;
7553 /* Do not use compare_names during propagation, it's quadratic. */
7554 if (TREE_CODE (op0
) == SSA_NAME
&& TREE_CODE (op1
) == SSA_NAME
7556 return compare_names (code
, op0
, op1
, strict_overflow_p
);
7557 else if (TREE_CODE (op0
) == SSA_NAME
)
7558 return compare_name_with_value (code
, op0
, op1
,
7559 strict_overflow_p
, use_equiv_p
);
7560 else if (TREE_CODE (op1
) == SSA_NAME
)
7561 return compare_name_with_value (swap_tree_comparison (code
), op1
, op0
,
7562 strict_overflow_p
, use_equiv_p
);
7566 /* Given (CODE OP0 OP1) within STMT, try to simplify it based on value range
7567 information. Return NULL if the conditional can not be evaluated.
7568 The ranges of all the names equivalent with the operands in COND
7569 will be used when trying to compute the value. If the result is
7570 based on undefined signed overflow, issue a warning if
7574 vrp_evaluate_conditional (tree_code code
, tree op0
, tree op1
, gimple
*stmt
)
7580 /* Some passes and foldings leak constants with overflow flag set
7581 into the IL. Avoid doing wrong things with these and bail out. */
7582 if ((TREE_CODE (op0
) == INTEGER_CST
7583 && TREE_OVERFLOW (op0
))
7584 || (TREE_CODE (op1
) == INTEGER_CST
7585 && TREE_OVERFLOW (op1
)))
7589 ret
= vrp_evaluate_conditional_warnv_with_ops (code
, op0
, op1
, true, &sop
,
7594 enum warn_strict_overflow_code wc
;
7595 const char* warnmsg
;
7597 if (is_gimple_min_invariant (ret
))
7599 wc
= WARN_STRICT_OVERFLOW_CONDITIONAL
;
7600 warnmsg
= G_("assuming signed overflow does not occur when "
7601 "simplifying conditional to constant");
7605 wc
= WARN_STRICT_OVERFLOW_COMPARISON
;
7606 warnmsg
= G_("assuming signed overflow does not occur when "
7607 "simplifying conditional");
7610 if (issue_strict_overflow_warning (wc
))
7612 location_t location
;
7614 if (!gimple_has_location (stmt
))
7615 location
= input_location
;
7617 location
= gimple_location (stmt
);
7618 warning_at (location
, OPT_Wstrict_overflow
, "%s", warnmsg
);
7622 if (warn_type_limits
7623 && ret
&& only_ranges
7624 && TREE_CODE_CLASS (code
) == tcc_comparison
7625 && TREE_CODE (op0
) == SSA_NAME
)
7627 /* If the comparison is being folded and the operand on the LHS
7628 is being compared against a constant value that is outside of
7629 the natural range of OP0's type, then the predicate will
7630 always fold regardless of the value of OP0. If -Wtype-limits
7631 was specified, emit a warning. */
7632 tree type
= TREE_TYPE (op0
);
7633 value_range
*vr0
= get_value_range (op0
);
7635 if (vr0
->type
== VR_RANGE
7636 && INTEGRAL_TYPE_P (type
)
7637 && vrp_val_is_min (vr0
->min
)
7638 && vrp_val_is_max (vr0
->max
)
7639 && is_gimple_min_invariant (op1
))
7641 location_t location
;
7643 if (!gimple_has_location (stmt
))
7644 location
= input_location
;
7646 location
= gimple_location (stmt
);
7648 warning_at (location
, OPT_Wtype_limits
,
7650 ? G_("comparison always false "
7651 "due to limited range of data type")
7652 : G_("comparison always true "
7653 "due to limited range of data type"));
7661 /* Visit conditional statement STMT. If we can determine which edge
7662 will be taken out of STMT's basic block, record it in
7663 *TAKEN_EDGE_P. Otherwise, set *TAKEN_EDGE_P to NULL. */
7666 vrp_visit_cond_stmt (gcond
*stmt
, edge
*taken_edge_p
)
7670 *taken_edge_p
= NULL
;
7672 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7677 fprintf (dump_file
, "\nVisiting conditional with predicate: ");
7678 print_gimple_stmt (dump_file
, stmt
, 0);
7679 fprintf (dump_file
, "\nWith known ranges\n");
7681 FOR_EACH_SSA_TREE_OPERAND (use
, stmt
, i
, SSA_OP_USE
)
7683 fprintf (dump_file
, "\t");
7684 print_generic_expr (dump_file
, use
);
7685 fprintf (dump_file
, ": ");
7686 dump_value_range (dump_file
, vr_value
[SSA_NAME_VERSION (use
)]);
7689 fprintf (dump_file
, "\n");
7692 /* Compute the value of the predicate COND by checking the known
7693 ranges of each of its operands.
7695 Note that we cannot evaluate all the equivalent ranges here
7696 because those ranges may not yet be final and with the current
7697 propagation strategy, we cannot determine when the value ranges
7698 of the names in the equivalence set have changed.
7700 For instance, given the following code fragment
7704 i_14 = ASSERT_EXPR <i_5, i_5 != 0>
7708 Assume that on the first visit to i_14, i_5 has the temporary
7709 range [8, 8] because the second argument to the PHI function is
7710 not yet executable. We derive the range ~[0, 0] for i_14 and the
7711 equivalence set { i_5 }. So, when we visit 'if (i_14 == 1)' for
7712 the first time, since i_14 is equivalent to the range [8, 8], we
7713 determine that the predicate is always false.
7715 On the next round of propagation, i_13 is determined to be
7716 VARYING, which causes i_5 to drop down to VARYING. So, another
7717 visit to i_14 is scheduled. In this second visit, we compute the
7718 exact same range and equivalence set for i_14, namely ~[0, 0] and
7719 { i_5 }. But we did not have the previous range for i_5
7720 registered, so vrp_visit_assignment thinks that the range for
7721 i_14 has not changed. Therefore, the predicate 'if (i_14 == 1)'
7722 is not visited again, which stops propagation from visiting
7723 statements in the THEN clause of that if().
7725 To properly fix this we would need to keep the previous range
7726 value for the names in the equivalence set. This way we would've
7727 discovered that from one visit to the other i_5 changed from
7728 range [8, 8] to VR_VARYING.
7730 However, fixing this apparent limitation may not be worth the
7731 additional checking. Testing on several code bases (GCC, DLV,
7732 MICO, TRAMP3D and SPEC2000) showed that doing this results in
7733 4 more predicates folded in SPEC. */
7736 val
= vrp_evaluate_conditional_warnv_with_ops (gimple_cond_code (stmt
),
7737 gimple_cond_lhs (stmt
),
7738 gimple_cond_rhs (stmt
),
7741 *taken_edge_p
= find_taken_edge (gimple_bb (stmt
), val
);
7743 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7745 fprintf (dump_file
, "\nPredicate evaluates to: ");
7746 if (val
== NULL_TREE
)
7747 fprintf (dump_file
, "DON'T KNOW\n");
7749 print_generic_stmt (dump_file
, val
);
7753 /* Searches the case label vector VEC for the index *IDX of the CASE_LABEL
7754 that includes the value VAL. The search is restricted to the range
7755 [START_IDX, n - 1] where n is the size of VEC.
7757 If there is a CASE_LABEL for VAL, its index is placed in IDX and true is
7760 If there is no CASE_LABEL for VAL and there is one that is larger than VAL,
7761 it is placed in IDX and false is returned.
7763 If VAL is larger than any CASE_LABEL, n is placed on IDX and false is
7767 find_case_label_index (gswitch
*stmt
, size_t start_idx
, tree val
, size_t *idx
)
7769 size_t n
= gimple_switch_num_labels (stmt
);
7772 /* Find case label for minimum of the value range or the next one.
7773 At each iteration we are searching in [low, high - 1]. */
7775 for (low
= start_idx
, high
= n
; high
!= low
; )
7779 /* Note that i != high, so we never ask for n. */
7780 size_t i
= (high
+ low
) / 2;
7781 t
= gimple_switch_label (stmt
, i
);
7783 /* Cache the result of comparing CASE_LOW and val. */
7784 cmp
= tree_int_cst_compare (CASE_LOW (t
), val
);
7788 /* Ranges cannot be empty. */
7797 if (CASE_HIGH (t
) != NULL
7798 && tree_int_cst_compare (CASE_HIGH (t
), val
) >= 0)
7810 /* Searches the case label vector VEC for the range of CASE_LABELs that is used
7811 for values between MIN and MAX. The first index is placed in MIN_IDX. The
7812 last index is placed in MAX_IDX. If the range of CASE_LABELs is empty
7813 then MAX_IDX < MIN_IDX.
7814 Returns true if the default label is not needed. */
7817 find_case_label_range (gswitch
*stmt
, tree min
, tree max
, size_t *min_idx
,
7821 bool min_take_default
= !find_case_label_index (stmt
, 1, min
, &i
);
7822 bool max_take_default
= !find_case_label_index (stmt
, i
, max
, &j
);
7826 && max_take_default
)
7828 /* Only the default case label reached.
7829 Return an empty range. */
7836 bool take_default
= min_take_default
|| max_take_default
;
7840 if (max_take_default
)
7843 /* If the case label range is continuous, we do not need
7844 the default case label. Verify that. */
7845 high
= CASE_LOW (gimple_switch_label (stmt
, i
));
7846 if (CASE_HIGH (gimple_switch_label (stmt
, i
)))
7847 high
= CASE_HIGH (gimple_switch_label (stmt
, i
));
7848 for (k
= i
+ 1; k
<= j
; ++k
)
7850 low
= CASE_LOW (gimple_switch_label (stmt
, k
));
7851 if (!integer_onep (int_const_binop (MINUS_EXPR
, low
, high
)))
7853 take_default
= true;
7857 if (CASE_HIGH (gimple_switch_label (stmt
, k
)))
7858 high
= CASE_HIGH (gimple_switch_label (stmt
, k
));
7863 return !take_default
;
7867 /* Searches the case label vector VEC for the ranges of CASE_LABELs that are
7868 used in range VR. The indices are placed in MIN_IDX1, MAX_IDX, MIN_IDX2 and
7869 MAX_IDX2. If the ranges of CASE_LABELs are empty then MAX_IDX1 < MIN_IDX1.
7870 Returns true if the default label is not needed. */
7873 find_case_label_ranges (gswitch
*stmt
, value_range
*vr
, size_t *min_idx1
,
7874 size_t *max_idx1
, size_t *min_idx2
,
7878 unsigned int n
= gimple_switch_num_labels (stmt
);
7880 tree case_low
, case_high
;
7881 tree min
= vr
->min
, max
= vr
->max
;
7883 gcc_checking_assert (vr
->type
== VR_RANGE
|| vr
->type
== VR_ANTI_RANGE
);
7885 take_default
= !find_case_label_range (stmt
, min
, max
, &i
, &j
);
7887 /* Set second range to emtpy. */
7891 if (vr
->type
== VR_RANGE
)
7895 return !take_default
;
7898 /* Set first range to all case labels. */
7905 /* Make sure all the values of case labels [i , j] are contained in
7906 range [MIN, MAX]. */
7907 case_low
= CASE_LOW (gimple_switch_label (stmt
, i
));
7908 case_high
= CASE_HIGH (gimple_switch_label (stmt
, j
));
7909 if (tree_int_cst_compare (case_low
, min
) < 0)
7911 if (case_high
!= NULL_TREE
7912 && tree_int_cst_compare (max
, case_high
) < 0)
7918 /* If the range spans case labels [i, j], the corresponding anti-range spans
7919 the labels [1, i - 1] and [j + 1, n - 1]. */
7945 /* Visit switch statement STMT. If we can determine which edge
7946 will be taken out of STMT's basic block, record it in
7947 *TAKEN_EDGE_P. Otherwise, *TAKEN_EDGE_P set to NULL. */
7950 vrp_visit_switch_stmt (gswitch
*stmt
, edge
*taken_edge_p
)
7954 size_t i
= 0, j
= 0, k
, l
;
7957 *taken_edge_p
= NULL
;
7958 op
= gimple_switch_index (stmt
);
7959 if (TREE_CODE (op
) != SSA_NAME
)
7962 vr
= get_value_range (op
);
7963 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7965 fprintf (dump_file
, "\nVisiting switch expression with operand ");
7966 print_generic_expr (dump_file
, op
);
7967 fprintf (dump_file
, " with known range ");
7968 dump_value_range (dump_file
, vr
);
7969 fprintf (dump_file
, "\n");
7972 if ((vr
->type
!= VR_RANGE
7973 && vr
->type
!= VR_ANTI_RANGE
)
7974 || symbolic_range_p (vr
))
7977 /* Find the single edge that is taken from the switch expression. */
7978 take_default
= !find_case_label_ranges (stmt
, vr
, &i
, &j
, &k
, &l
);
7980 /* Check if the range spans no CASE_LABEL. If so, we only reach the default
7984 gcc_assert (take_default
);
7985 val
= gimple_switch_default_label (stmt
);
7989 /* Check if labels with index i to j and maybe the default label
7990 are all reaching the same label. */
7992 val
= gimple_switch_label (stmt
, i
);
7994 && CASE_LABEL (gimple_switch_default_label (stmt
))
7995 != CASE_LABEL (val
))
7997 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7998 fprintf (dump_file
, " not a single destination for this "
8002 for (++i
; i
<= j
; ++i
)
8004 if (CASE_LABEL (gimple_switch_label (stmt
, i
)) != CASE_LABEL (val
))
8006 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8007 fprintf (dump_file
, " not a single destination for this "
8014 if (CASE_LABEL (gimple_switch_label (stmt
, k
)) != CASE_LABEL (val
))
8016 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8017 fprintf (dump_file
, " not a single destination for this "
8024 *taken_edge_p
= find_edge (gimple_bb (stmt
),
8025 label_to_block (CASE_LABEL (val
)));
8027 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8029 fprintf (dump_file
, " will take edge to ");
8030 print_generic_stmt (dump_file
, CASE_LABEL (val
));
8035 /* Evaluate statement STMT. If the statement produces a useful range,
8036 set VR and corepsponding OUTPUT_P.
8038 If STMT is a conditional branch and we can determine its truth
8039 value, the taken edge is recorded in *TAKEN_EDGE_P. */
8042 extract_range_from_stmt (gimple
*stmt
, edge
*taken_edge_p
,
8043 tree
*output_p
, value_range
*vr
)
8046 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8048 fprintf (dump_file
, "\nVisiting statement:\n");
8049 print_gimple_stmt (dump_file
, stmt
, 0, dump_flags
);
8052 if (!stmt_interesting_for_vrp (stmt
))
8053 gcc_assert (stmt_ends_bb_p (stmt
));
8054 else if (is_gimple_assign (stmt
) || is_gimple_call (stmt
))
8055 vrp_visit_assignment_or_call (stmt
, output_p
, vr
);
8056 else if (gimple_code (stmt
) == GIMPLE_COND
)
8057 vrp_visit_cond_stmt (as_a
<gcond
*> (stmt
), taken_edge_p
);
8058 else if (gimple_code (stmt
) == GIMPLE_SWITCH
)
8059 vrp_visit_switch_stmt (as_a
<gswitch
*> (stmt
), taken_edge_p
);
8062 /* Evaluate statement STMT. If the statement produces a useful range,
8063 return SSA_PROP_INTERESTING and record the SSA name with the
8064 interesting range into *OUTPUT_P.
8066 If STMT is a conditional branch and we can determine its truth
8067 value, the taken edge is recorded in *TAKEN_EDGE_P.
8069 If STMT produces a varying value, return SSA_PROP_VARYING. */
8071 static enum ssa_prop_result
8072 vrp_visit_stmt (gimple
*stmt
, edge
*taken_edge_p
, tree
*output_p
)
8074 value_range vr
= VR_INITIALIZER
;
8075 tree lhs
= gimple_get_lhs (stmt
);
8076 extract_range_from_stmt (stmt
, taken_edge_p
, output_p
, &vr
);
8080 if (update_value_range (*output_p
, &vr
))
8082 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8084 fprintf (dump_file
, "Found new range for ");
8085 print_generic_expr (dump_file
, *output_p
);
8086 fprintf (dump_file
, ": ");
8087 dump_value_range (dump_file
, &vr
);
8088 fprintf (dump_file
, "\n");
8091 if (vr
.type
== VR_VARYING
)
8092 return SSA_PROP_VARYING
;
8094 return SSA_PROP_INTERESTING
;
8096 return SSA_PROP_NOT_INTERESTING
;
8099 if (is_gimple_call (stmt
) && gimple_call_internal_p (stmt
))
8100 switch (gimple_call_internal_fn (stmt
))
8102 case IFN_ADD_OVERFLOW
:
8103 case IFN_SUB_OVERFLOW
:
8104 case IFN_MUL_OVERFLOW
:
8105 case IFN_ATOMIC_COMPARE_EXCHANGE
:
8106 /* These internal calls return _Complex integer type,
8107 which VRP does not track, but the immediate uses
8108 thereof might be interesting. */
8109 if (lhs
&& TREE_CODE (lhs
) == SSA_NAME
)
8111 imm_use_iterator iter
;
8112 use_operand_p use_p
;
8113 enum ssa_prop_result res
= SSA_PROP_VARYING
;
8115 set_value_range_to_varying (get_value_range (lhs
));
8117 FOR_EACH_IMM_USE_FAST (use_p
, iter
, lhs
)
8119 gimple
*use_stmt
= USE_STMT (use_p
);
8120 if (!is_gimple_assign (use_stmt
))
8122 enum tree_code rhs_code
= gimple_assign_rhs_code (use_stmt
);
8123 if (rhs_code
!= REALPART_EXPR
&& rhs_code
!= IMAGPART_EXPR
)
8125 tree rhs1
= gimple_assign_rhs1 (use_stmt
);
8126 tree use_lhs
= gimple_assign_lhs (use_stmt
);
8127 if (TREE_CODE (rhs1
) != rhs_code
8128 || TREE_OPERAND (rhs1
, 0) != lhs
8129 || TREE_CODE (use_lhs
) != SSA_NAME
8130 || !stmt_interesting_for_vrp (use_stmt
)
8131 || (!INTEGRAL_TYPE_P (TREE_TYPE (use_lhs
))
8132 || !TYPE_MIN_VALUE (TREE_TYPE (use_lhs
))
8133 || !TYPE_MAX_VALUE (TREE_TYPE (use_lhs
))))
8136 /* If there is a change in the value range for any of the
8137 REALPART_EXPR/IMAGPART_EXPR immediate uses, return
8138 SSA_PROP_INTERESTING. If there are any REALPART_EXPR
8139 or IMAGPART_EXPR immediate uses, but none of them have
8140 a change in their value ranges, return
8141 SSA_PROP_NOT_INTERESTING. If there are no
8142 {REAL,IMAG}PART_EXPR uses at all,
8143 return SSA_PROP_VARYING. */
8144 value_range new_vr
= VR_INITIALIZER
;
8145 extract_range_basic (&new_vr
, use_stmt
);
8146 value_range
*old_vr
= get_value_range (use_lhs
);
8147 if (old_vr
->type
!= new_vr
.type
8148 || !vrp_operand_equal_p (old_vr
->min
, new_vr
.min
)
8149 || !vrp_operand_equal_p (old_vr
->max
, new_vr
.max
)
8150 || !vrp_bitmap_equal_p (old_vr
->equiv
, new_vr
.equiv
))
8151 res
= SSA_PROP_INTERESTING
;
8153 res
= SSA_PROP_NOT_INTERESTING
;
8154 BITMAP_FREE (new_vr
.equiv
);
8155 if (res
== SSA_PROP_INTERESTING
)
8169 /* All other statements produce nothing of interest for VRP, so mark
8170 their outputs varying and prevent further simulation. */
8171 set_defs_to_varying (stmt
);
8173 return (*taken_edge_p
) ? SSA_PROP_INTERESTING
: SSA_PROP_VARYING
;
8176 /* Union the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
8177 { VR1TYPE, VR0MIN, VR0MAX } and store the result
8178 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
8179 possible such range. The resulting range is not canonicalized. */
8182 union_ranges (enum value_range_type
*vr0type
,
8183 tree
*vr0min
, tree
*vr0max
,
8184 enum value_range_type vr1type
,
8185 tree vr1min
, tree vr1max
)
8187 bool mineq
= vrp_operand_equal_p (*vr0min
, vr1min
);
8188 bool maxeq
= vrp_operand_equal_p (*vr0max
, vr1max
);
8190 /* [] is vr0, () is vr1 in the following classification comments. */
8194 if (*vr0type
== vr1type
)
8195 /* Nothing to do for equal ranges. */
8197 else if ((*vr0type
== VR_RANGE
8198 && vr1type
== VR_ANTI_RANGE
)
8199 || (*vr0type
== VR_ANTI_RANGE
8200 && vr1type
== VR_RANGE
))
8202 /* For anti-range with range union the result is varying. */
8208 else if (operand_less_p (*vr0max
, vr1min
) == 1
8209 || operand_less_p (vr1max
, *vr0min
) == 1)
8211 /* [ ] ( ) or ( ) [ ]
8212 If the ranges have an empty intersection, result of the union
8213 operation is the anti-range or if both are anti-ranges
8215 if (*vr0type
== VR_ANTI_RANGE
8216 && vr1type
== VR_ANTI_RANGE
)
8218 else if (*vr0type
== VR_ANTI_RANGE
8219 && vr1type
== VR_RANGE
)
8221 else if (*vr0type
== VR_RANGE
8222 && vr1type
== VR_ANTI_RANGE
)
8228 else if (*vr0type
== VR_RANGE
8229 && vr1type
== VR_RANGE
)
8231 /* The result is the convex hull of both ranges. */
8232 if (operand_less_p (*vr0max
, vr1min
) == 1)
8234 /* If the result can be an anti-range, create one. */
8235 if (TREE_CODE (*vr0max
) == INTEGER_CST
8236 && TREE_CODE (vr1min
) == INTEGER_CST
8237 && vrp_val_is_min (*vr0min
)
8238 && vrp_val_is_max (vr1max
))
8240 tree min
= int_const_binop (PLUS_EXPR
,
8242 build_int_cst (TREE_TYPE (*vr0max
), 1));
8243 tree max
= int_const_binop (MINUS_EXPR
,
8245 build_int_cst (TREE_TYPE (vr1min
), 1));
8246 if (!operand_less_p (max
, min
))
8248 *vr0type
= VR_ANTI_RANGE
;
8260 /* If the result can be an anti-range, create one. */
8261 if (TREE_CODE (vr1max
) == INTEGER_CST
8262 && TREE_CODE (*vr0min
) == INTEGER_CST
8263 && vrp_val_is_min (vr1min
)
8264 && vrp_val_is_max (*vr0max
))
8266 tree min
= int_const_binop (PLUS_EXPR
,
8268 build_int_cst (TREE_TYPE (vr1max
), 1));
8269 tree max
= int_const_binop (MINUS_EXPR
,
8271 build_int_cst (TREE_TYPE (*vr0min
), 1));
8272 if (!operand_less_p (max
, min
))
8274 *vr0type
= VR_ANTI_RANGE
;
8288 else if ((maxeq
|| operand_less_p (vr1max
, *vr0max
) == 1)
8289 && (mineq
|| operand_less_p (*vr0min
, vr1min
) == 1))
8291 /* [ ( ) ] or [( ) ] or [ ( )] */
8292 if (*vr0type
== VR_RANGE
8293 && vr1type
== VR_RANGE
)
8295 else if (*vr0type
== VR_ANTI_RANGE
8296 && vr1type
== VR_ANTI_RANGE
)
8302 else if (*vr0type
== VR_ANTI_RANGE
8303 && vr1type
== VR_RANGE
)
8305 /* Arbitrarily choose the right or left gap. */
8306 if (!mineq
&& TREE_CODE (vr1min
) == INTEGER_CST
)
8307 *vr0max
= int_const_binop (MINUS_EXPR
, vr1min
,
8308 build_int_cst (TREE_TYPE (vr1min
), 1));
8309 else if (!maxeq
&& TREE_CODE (vr1max
) == INTEGER_CST
)
8310 *vr0min
= int_const_binop (PLUS_EXPR
, vr1max
,
8311 build_int_cst (TREE_TYPE (vr1max
), 1));
8315 else if (*vr0type
== VR_RANGE
8316 && vr1type
== VR_ANTI_RANGE
)
8317 /* The result covers everything. */
8322 else if ((maxeq
|| operand_less_p (*vr0max
, vr1max
) == 1)
8323 && (mineq
|| operand_less_p (vr1min
, *vr0min
) == 1))
8325 /* ( [ ] ) or ([ ] ) or ( [ ]) */
8326 if (*vr0type
== VR_RANGE
8327 && vr1type
== VR_RANGE
)
8333 else if (*vr0type
== VR_ANTI_RANGE
8334 && vr1type
== VR_ANTI_RANGE
)
8336 else if (*vr0type
== VR_RANGE
8337 && vr1type
== VR_ANTI_RANGE
)
8339 *vr0type
= VR_ANTI_RANGE
;
8340 if (!mineq
&& TREE_CODE (*vr0min
) == INTEGER_CST
)
8342 *vr0max
= int_const_binop (MINUS_EXPR
, *vr0min
,
8343 build_int_cst (TREE_TYPE (*vr0min
), 1));
8346 else if (!maxeq
&& TREE_CODE (*vr0max
) == INTEGER_CST
)
8348 *vr0min
= int_const_binop (PLUS_EXPR
, *vr0max
,
8349 build_int_cst (TREE_TYPE (*vr0max
), 1));
8355 else if (*vr0type
== VR_ANTI_RANGE
8356 && vr1type
== VR_RANGE
)
8357 /* The result covers everything. */
8362 else if ((operand_less_p (vr1min
, *vr0max
) == 1
8363 || operand_equal_p (vr1min
, *vr0max
, 0))
8364 && operand_less_p (*vr0min
, vr1min
) == 1
8365 && operand_less_p (*vr0max
, vr1max
) == 1)
8367 /* [ ( ] ) or [ ]( ) */
8368 if (*vr0type
== VR_RANGE
8369 && vr1type
== VR_RANGE
)
8371 else if (*vr0type
== VR_ANTI_RANGE
8372 && vr1type
== VR_ANTI_RANGE
)
8374 else if (*vr0type
== VR_ANTI_RANGE
8375 && vr1type
== VR_RANGE
)
8377 if (TREE_CODE (vr1min
) == INTEGER_CST
)
8378 *vr0max
= int_const_binop (MINUS_EXPR
, vr1min
,
8379 build_int_cst (TREE_TYPE (vr1min
), 1));
8383 else if (*vr0type
== VR_RANGE
8384 && vr1type
== VR_ANTI_RANGE
)
8386 if (TREE_CODE (*vr0max
) == INTEGER_CST
)
8389 *vr0min
= int_const_binop (PLUS_EXPR
, *vr0max
,
8390 build_int_cst (TREE_TYPE (*vr0max
), 1));
8399 else if ((operand_less_p (*vr0min
, vr1max
) == 1
8400 || operand_equal_p (*vr0min
, vr1max
, 0))
8401 && operand_less_p (vr1min
, *vr0min
) == 1
8402 && operand_less_p (vr1max
, *vr0max
) == 1)
8404 /* ( [ ) ] or ( )[ ] */
8405 if (*vr0type
== VR_RANGE
8406 && vr1type
== VR_RANGE
)
8408 else if (*vr0type
== VR_ANTI_RANGE
8409 && vr1type
== VR_ANTI_RANGE
)
8411 else if (*vr0type
== VR_ANTI_RANGE
8412 && vr1type
== VR_RANGE
)
8414 if (TREE_CODE (vr1max
) == INTEGER_CST
)
8415 *vr0min
= int_const_binop (PLUS_EXPR
, vr1max
,
8416 build_int_cst (TREE_TYPE (vr1max
), 1));
8420 else if (*vr0type
== VR_RANGE
8421 && vr1type
== VR_ANTI_RANGE
)
8423 if (TREE_CODE (*vr0min
) == INTEGER_CST
)
8427 *vr0max
= int_const_binop (MINUS_EXPR
, *vr0min
,
8428 build_int_cst (TREE_TYPE (*vr0min
), 1));
8442 *vr0type
= VR_VARYING
;
8443 *vr0min
= NULL_TREE
;
8444 *vr0max
= NULL_TREE
;
8447 /* Intersect the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
8448 { VR1TYPE, VR0MIN, VR0MAX } and store the result
8449 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
8450 possible such range. The resulting range is not canonicalized. */
8453 intersect_ranges (enum value_range_type
*vr0type
,
8454 tree
*vr0min
, tree
*vr0max
,
8455 enum value_range_type vr1type
,
8456 tree vr1min
, tree vr1max
)
8458 bool mineq
= vrp_operand_equal_p (*vr0min
, vr1min
);
8459 bool maxeq
= vrp_operand_equal_p (*vr0max
, vr1max
);
8461 /* [] is vr0, () is vr1 in the following classification comments. */
8465 if (*vr0type
== vr1type
)
8466 /* Nothing to do for equal ranges. */
8468 else if ((*vr0type
== VR_RANGE
8469 && vr1type
== VR_ANTI_RANGE
)
8470 || (*vr0type
== VR_ANTI_RANGE
8471 && vr1type
== VR_RANGE
))
8473 /* For anti-range with range intersection the result is empty. */
8474 *vr0type
= VR_UNDEFINED
;
8475 *vr0min
= NULL_TREE
;
8476 *vr0max
= NULL_TREE
;
8481 else if (operand_less_p (*vr0max
, vr1min
) == 1
8482 || operand_less_p (vr1max
, *vr0min
) == 1)
8484 /* [ ] ( ) or ( ) [ ]
8485 If the ranges have an empty intersection, the result of the
8486 intersect operation is the range for intersecting an
8487 anti-range with a range or empty when intersecting two ranges. */
8488 if (*vr0type
== VR_RANGE
8489 && vr1type
== VR_ANTI_RANGE
)
8491 else if (*vr0type
== VR_ANTI_RANGE
8492 && vr1type
== VR_RANGE
)
8498 else if (*vr0type
== VR_RANGE
8499 && vr1type
== VR_RANGE
)
8501 *vr0type
= VR_UNDEFINED
;
8502 *vr0min
= NULL_TREE
;
8503 *vr0max
= NULL_TREE
;
8505 else if (*vr0type
== VR_ANTI_RANGE
8506 && vr1type
== VR_ANTI_RANGE
)
8508 /* If the anti-ranges are adjacent to each other merge them. */
8509 if (TREE_CODE (*vr0max
) == INTEGER_CST
8510 && TREE_CODE (vr1min
) == INTEGER_CST
8511 && operand_less_p (*vr0max
, vr1min
) == 1
8512 && integer_onep (int_const_binop (MINUS_EXPR
,
8515 else if (TREE_CODE (vr1max
) == INTEGER_CST
8516 && TREE_CODE (*vr0min
) == INTEGER_CST
8517 && operand_less_p (vr1max
, *vr0min
) == 1
8518 && integer_onep (int_const_binop (MINUS_EXPR
,
8521 /* Else arbitrarily take VR0. */
8524 else if ((maxeq
|| operand_less_p (vr1max
, *vr0max
) == 1)
8525 && (mineq
|| operand_less_p (*vr0min
, vr1min
) == 1))
8527 /* [ ( ) ] or [( ) ] or [ ( )] */
8528 if (*vr0type
== VR_RANGE
8529 && vr1type
== VR_RANGE
)
8531 /* If both are ranges the result is the inner one. */
8536 else if (*vr0type
== VR_RANGE
8537 && vr1type
== VR_ANTI_RANGE
)
8539 /* Choose the right gap if the left one is empty. */
8542 if (TREE_CODE (vr1max
) != INTEGER_CST
)
8544 else if (TYPE_PRECISION (TREE_TYPE (vr1max
)) == 1
8545 && !TYPE_UNSIGNED (TREE_TYPE (vr1max
)))
8547 = int_const_binop (MINUS_EXPR
, vr1max
,
8548 build_int_cst (TREE_TYPE (vr1max
), -1));
8551 = int_const_binop (PLUS_EXPR
, vr1max
,
8552 build_int_cst (TREE_TYPE (vr1max
), 1));
8554 /* Choose the left gap if the right one is empty. */
8557 if (TREE_CODE (vr1min
) != INTEGER_CST
)
8559 else if (TYPE_PRECISION (TREE_TYPE (vr1min
)) == 1
8560 && !TYPE_UNSIGNED (TREE_TYPE (vr1min
)))
8562 = int_const_binop (PLUS_EXPR
, vr1min
,
8563 build_int_cst (TREE_TYPE (vr1min
), -1));
8566 = int_const_binop (MINUS_EXPR
, vr1min
,
8567 build_int_cst (TREE_TYPE (vr1min
), 1));
8569 /* Choose the anti-range if the range is effectively varying. */
8570 else if (vrp_val_is_min (*vr0min
)
8571 && vrp_val_is_max (*vr0max
))
8577 /* Else choose the range. */
8579 else if (*vr0type
== VR_ANTI_RANGE
8580 && vr1type
== VR_ANTI_RANGE
)
8581 /* If both are anti-ranges the result is the outer one. */
8583 else if (*vr0type
== VR_ANTI_RANGE
8584 && vr1type
== VR_RANGE
)
8586 /* The intersection is empty. */
8587 *vr0type
= VR_UNDEFINED
;
8588 *vr0min
= NULL_TREE
;
8589 *vr0max
= NULL_TREE
;
8594 else if ((maxeq
|| operand_less_p (*vr0max
, vr1max
) == 1)
8595 && (mineq
|| operand_less_p (vr1min
, *vr0min
) == 1))
8597 /* ( [ ] ) or ([ ] ) or ( [ ]) */
8598 if (*vr0type
== VR_RANGE
8599 && vr1type
== VR_RANGE
)
8600 /* Choose the inner range. */
8602 else if (*vr0type
== VR_ANTI_RANGE
8603 && vr1type
== VR_RANGE
)
8605 /* Choose the right gap if the left is empty. */
8608 *vr0type
= VR_RANGE
;
8609 if (TREE_CODE (*vr0max
) != INTEGER_CST
)
8611 else if (TYPE_PRECISION (TREE_TYPE (*vr0max
)) == 1
8612 && !TYPE_UNSIGNED (TREE_TYPE (*vr0max
)))
8614 = int_const_binop (MINUS_EXPR
, *vr0max
,
8615 build_int_cst (TREE_TYPE (*vr0max
), -1));
8618 = int_const_binop (PLUS_EXPR
, *vr0max
,
8619 build_int_cst (TREE_TYPE (*vr0max
), 1));
8622 /* Choose the left gap if the right is empty. */
8625 *vr0type
= VR_RANGE
;
8626 if (TREE_CODE (*vr0min
) != INTEGER_CST
)
8628 else if (TYPE_PRECISION (TREE_TYPE (*vr0min
)) == 1
8629 && !TYPE_UNSIGNED (TREE_TYPE (*vr0min
)))
8631 = int_const_binop (PLUS_EXPR
, *vr0min
,
8632 build_int_cst (TREE_TYPE (*vr0min
), -1));
8635 = int_const_binop (MINUS_EXPR
, *vr0min
,
8636 build_int_cst (TREE_TYPE (*vr0min
), 1));
8639 /* Choose the anti-range if the range is effectively varying. */
8640 else if (vrp_val_is_min (vr1min
)
8641 && vrp_val_is_max (vr1max
))
8643 /* Choose the anti-range if it is ~[0,0], that range is special
8644 enough to special case when vr1's range is relatively wide. */
8645 else if (*vr0min
== *vr0max
8646 && integer_zerop (*vr0min
)
8647 && (TYPE_PRECISION (TREE_TYPE (*vr0min
))
8648 == TYPE_PRECISION (ptr_type_node
))
8649 && TREE_CODE (vr1max
) == INTEGER_CST
8650 && TREE_CODE (vr1min
) == INTEGER_CST
8651 && (wi::clz (wi::sub (vr1max
, vr1min
))
8652 < TYPE_PRECISION (TREE_TYPE (*vr0min
)) / 2))
8654 /* Else choose the range. */
8662 else if (*vr0type
== VR_ANTI_RANGE
8663 && vr1type
== VR_ANTI_RANGE
)
8665 /* If both are anti-ranges the result is the outer one. */
8670 else if (vr1type
== VR_ANTI_RANGE
8671 && *vr0type
== VR_RANGE
)
8673 /* The intersection is empty. */
8674 *vr0type
= VR_UNDEFINED
;
8675 *vr0min
= NULL_TREE
;
8676 *vr0max
= NULL_TREE
;
8681 else if ((operand_less_p (vr1min
, *vr0max
) == 1
8682 || operand_equal_p (vr1min
, *vr0max
, 0))
8683 && operand_less_p (*vr0min
, vr1min
) == 1)
8685 /* [ ( ] ) or [ ]( ) */
8686 if (*vr0type
== VR_ANTI_RANGE
8687 && vr1type
== VR_ANTI_RANGE
)
8689 else if (*vr0type
== VR_RANGE
8690 && vr1type
== VR_RANGE
)
8692 else if (*vr0type
== VR_RANGE
8693 && vr1type
== VR_ANTI_RANGE
)
8695 if (TREE_CODE (vr1min
) == INTEGER_CST
)
8696 *vr0max
= int_const_binop (MINUS_EXPR
, vr1min
,
8697 build_int_cst (TREE_TYPE (vr1min
), 1));
8701 else if (*vr0type
== VR_ANTI_RANGE
8702 && vr1type
== VR_RANGE
)
8704 *vr0type
= VR_RANGE
;
8705 if (TREE_CODE (*vr0max
) == INTEGER_CST
)
8706 *vr0min
= int_const_binop (PLUS_EXPR
, *vr0max
,
8707 build_int_cst (TREE_TYPE (*vr0max
), 1));
8715 else if ((operand_less_p (*vr0min
, vr1max
) == 1
8716 || operand_equal_p (*vr0min
, vr1max
, 0))
8717 && operand_less_p (vr1min
, *vr0min
) == 1)
8719 /* ( [ ) ] or ( )[ ] */
8720 if (*vr0type
== VR_ANTI_RANGE
8721 && vr1type
== VR_ANTI_RANGE
)
8723 else if (*vr0type
== VR_RANGE
8724 && vr1type
== VR_RANGE
)
8726 else if (*vr0type
== VR_RANGE
8727 && vr1type
== VR_ANTI_RANGE
)
8729 if (TREE_CODE (vr1max
) == INTEGER_CST
)
8730 *vr0min
= int_const_binop (PLUS_EXPR
, vr1max
,
8731 build_int_cst (TREE_TYPE (vr1max
), 1));
8735 else if (*vr0type
== VR_ANTI_RANGE
8736 && vr1type
== VR_RANGE
)
8738 *vr0type
= VR_RANGE
;
8739 if (TREE_CODE (*vr0min
) == INTEGER_CST
)
8740 *vr0max
= int_const_binop (MINUS_EXPR
, *vr0min
,
8741 build_int_cst (TREE_TYPE (*vr0min
), 1));
8750 /* As a fallback simply use { *VRTYPE, *VR0MIN, *VR0MAX } as
8751 result for the intersection. That's always a conservative
8752 correct estimate unless VR1 is a constant singleton range
8753 in which case we choose that. */
8754 if (vr1type
== VR_RANGE
8755 && is_gimple_min_invariant (vr1min
)
8756 && vrp_operand_equal_p (vr1min
, vr1max
))
8767 /* Intersect the two value-ranges *VR0 and *VR1 and store the result
8768 in *VR0. This may not be the smallest possible such range. */
8771 vrp_intersect_ranges_1 (value_range
*vr0
, value_range
*vr1
)
8775 /* If either range is VR_VARYING the other one wins. */
8776 if (vr1
->type
== VR_VARYING
)
8778 if (vr0
->type
== VR_VARYING
)
8780 copy_value_range (vr0
, vr1
);
8784 /* When either range is VR_UNDEFINED the resulting range is
8785 VR_UNDEFINED, too. */
8786 if (vr0
->type
== VR_UNDEFINED
)
8788 if (vr1
->type
== VR_UNDEFINED
)
8790 set_value_range_to_undefined (vr0
);
8794 /* Save the original vr0 so we can return it as conservative intersection
8795 result when our worker turns things to varying. */
8797 intersect_ranges (&vr0
->type
, &vr0
->min
, &vr0
->max
,
8798 vr1
->type
, vr1
->min
, vr1
->max
);
8799 /* Make sure to canonicalize the result though as the inversion of a
8800 VR_RANGE can still be a VR_RANGE. */
8801 set_and_canonicalize_value_range (vr0
, vr0
->type
,
8802 vr0
->min
, vr0
->max
, vr0
->equiv
);
8803 /* If that failed, use the saved original VR0. */
8804 if (vr0
->type
== VR_VARYING
)
8809 /* If the result is VR_UNDEFINED there is no need to mess with
8810 the equivalencies. */
8811 if (vr0
->type
== VR_UNDEFINED
)
8814 /* The resulting set of equivalences for range intersection is the union of
8816 if (vr0
->equiv
&& vr1
->equiv
&& vr0
->equiv
!= vr1
->equiv
)
8817 bitmap_ior_into (vr0
->equiv
, vr1
->equiv
);
8818 else if (vr1
->equiv
&& !vr0
->equiv
)
8820 vr0
->equiv
= BITMAP_ALLOC (&vrp_equiv_obstack
);
8821 bitmap_copy (vr0
->equiv
, vr1
->equiv
);
8826 vrp_intersect_ranges (value_range
*vr0
, value_range
*vr1
)
8828 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8830 fprintf (dump_file
, "Intersecting\n ");
8831 dump_value_range (dump_file
, vr0
);
8832 fprintf (dump_file
, "\nand\n ");
8833 dump_value_range (dump_file
, vr1
);
8834 fprintf (dump_file
, "\n");
8836 vrp_intersect_ranges_1 (vr0
, vr1
);
8837 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8839 fprintf (dump_file
, "to\n ");
8840 dump_value_range (dump_file
, vr0
);
8841 fprintf (dump_file
, "\n");
8845 /* Meet operation for value ranges. Given two value ranges VR0 and
8846 VR1, store in VR0 a range that contains both VR0 and VR1. This
8847 may not be the smallest possible such range. */
8850 vrp_meet_1 (value_range
*vr0
, const value_range
*vr1
)
8854 if (vr0
->type
== VR_UNDEFINED
)
8856 set_value_range (vr0
, vr1
->type
, vr1
->min
, vr1
->max
, vr1
->equiv
);
8860 if (vr1
->type
== VR_UNDEFINED
)
8862 /* VR0 already has the resulting range. */
8866 if (vr0
->type
== VR_VARYING
)
8868 /* Nothing to do. VR0 already has the resulting range. */
8872 if (vr1
->type
== VR_VARYING
)
8874 set_value_range_to_varying (vr0
);
8879 union_ranges (&vr0
->type
, &vr0
->min
, &vr0
->max
,
8880 vr1
->type
, vr1
->min
, vr1
->max
);
8881 if (vr0
->type
== VR_VARYING
)
8883 /* Failed to find an efficient meet. Before giving up and setting
8884 the result to VARYING, see if we can at least derive a useful
8885 anti-range. FIXME, all this nonsense about distinguishing
8886 anti-ranges from ranges is necessary because of the odd
8887 semantics of range_includes_zero_p and friends. */
8888 if (((saved
.type
== VR_RANGE
8889 && range_includes_zero_p (saved
.min
, saved
.max
) == 0)
8890 || (saved
.type
== VR_ANTI_RANGE
8891 && range_includes_zero_p (saved
.min
, saved
.max
) == 1))
8892 && ((vr1
->type
== VR_RANGE
8893 && range_includes_zero_p (vr1
->min
, vr1
->max
) == 0)
8894 || (vr1
->type
== VR_ANTI_RANGE
8895 && range_includes_zero_p (vr1
->min
, vr1
->max
) == 1)))
8897 set_value_range_to_nonnull (vr0
, TREE_TYPE (saved
.min
));
8899 /* Since this meet operation did not result from the meeting of
8900 two equivalent names, VR0 cannot have any equivalences. */
8902 bitmap_clear (vr0
->equiv
);
8906 set_value_range_to_varying (vr0
);
8909 set_and_canonicalize_value_range (vr0
, vr0
->type
, vr0
->min
, vr0
->max
,
8911 if (vr0
->type
== VR_VARYING
)
8914 /* The resulting set of equivalences is always the intersection of
8916 if (vr0
->equiv
&& vr1
->equiv
&& vr0
->equiv
!= vr1
->equiv
)
8917 bitmap_and_into (vr0
->equiv
, vr1
->equiv
);
8918 else if (vr0
->equiv
&& !vr1
->equiv
)
8919 bitmap_clear (vr0
->equiv
);
8923 vrp_meet (value_range
*vr0
, const value_range
*vr1
)
8925 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8927 fprintf (dump_file
, "Meeting\n ");
8928 dump_value_range (dump_file
, vr0
);
8929 fprintf (dump_file
, "\nand\n ");
8930 dump_value_range (dump_file
, vr1
);
8931 fprintf (dump_file
, "\n");
8933 vrp_meet_1 (vr0
, vr1
);
8934 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8936 fprintf (dump_file
, "to\n ");
8937 dump_value_range (dump_file
, vr0
);
8938 fprintf (dump_file
, "\n");
8943 /* Visit all arguments for PHI node PHI that flow through executable
8944 edges. If a valid value range can be derived from all the incoming
8945 value ranges, set a new range in VR_RESULT. */
8948 extract_range_from_phi_node (gphi
*phi
, value_range
*vr_result
)
8951 tree lhs
= PHI_RESULT (phi
);
8952 value_range
*lhs_vr
= get_value_range (lhs
);
8954 int edges
, old_edges
;
8957 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8959 fprintf (dump_file
, "\nVisiting PHI node: ");
8960 print_gimple_stmt (dump_file
, phi
, 0, dump_flags
);
8963 bool may_simulate_backedge_again
= false;
8965 for (i
= 0; i
< gimple_phi_num_args (phi
); i
++)
8967 edge e
= gimple_phi_arg_edge (phi
, i
);
8969 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8972 " Argument #%d (%d -> %d %sexecutable)\n",
8973 (int) i
, e
->src
->index
, e
->dest
->index
,
8974 (e
->flags
& EDGE_EXECUTABLE
) ? "" : "not ");
8977 if (e
->flags
& EDGE_EXECUTABLE
)
8979 tree arg
= PHI_ARG_DEF (phi
, i
);
8984 if (TREE_CODE (arg
) == SSA_NAME
)
8986 /* See if we are eventually going to change one of the args. */
8987 gimple
*def_stmt
= SSA_NAME_DEF_STMT (arg
);
8988 if (! gimple_nop_p (def_stmt
)
8989 && prop_simulate_again_p (def_stmt
)
8990 && e
->flags
& EDGE_DFS_BACK
)
8991 may_simulate_backedge_again
= true;
8993 vr_arg
= *(get_value_range (arg
));
8994 /* Do not allow equivalences or symbolic ranges to leak in from
8995 backedges. That creates invalid equivalencies.
8996 See PR53465 and PR54767. */
8997 if (e
->flags
& EDGE_DFS_BACK
)
8999 if (vr_arg
.type
== VR_RANGE
9000 || vr_arg
.type
== VR_ANTI_RANGE
)
9002 vr_arg
.equiv
= NULL
;
9003 if (symbolic_range_p (&vr_arg
))
9005 vr_arg
.type
= VR_VARYING
;
9006 vr_arg
.min
= NULL_TREE
;
9007 vr_arg
.max
= NULL_TREE
;
9013 /* If the non-backedge arguments range is VR_VARYING then
9014 we can still try recording a simple equivalence. */
9015 if (vr_arg
.type
== VR_VARYING
)
9017 vr_arg
.type
= VR_RANGE
;
9020 vr_arg
.equiv
= NULL
;
9026 if (TREE_OVERFLOW_P (arg
))
9027 arg
= drop_tree_overflow (arg
);
9029 vr_arg
.type
= VR_RANGE
;
9032 vr_arg
.equiv
= NULL
;
9035 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
9037 fprintf (dump_file
, "\t");
9038 print_generic_expr (dump_file
, arg
, dump_flags
);
9039 fprintf (dump_file
, ": ");
9040 dump_value_range (dump_file
, &vr_arg
);
9041 fprintf (dump_file
, "\n");
9045 copy_value_range (vr_result
, &vr_arg
);
9047 vrp_meet (vr_result
, &vr_arg
);
9050 if (vr_result
->type
== VR_VARYING
)
9055 if (vr_result
->type
== VR_VARYING
)
9057 else if (vr_result
->type
== VR_UNDEFINED
)
9060 old_edges
= vr_phi_edge_counts
[SSA_NAME_VERSION (lhs
)];
9061 vr_phi_edge_counts
[SSA_NAME_VERSION (lhs
)] = edges
;
9063 /* To prevent infinite iterations in the algorithm, derive ranges
9064 when the new value is slightly bigger or smaller than the
9065 previous one. We don't do this if we have seen a new executable
9066 edge; this helps us avoid an infinity for conditionals
9067 which are not in a loop. If the old value-range was VR_UNDEFINED
9068 use the updated range and iterate one more time. If we will not
9069 simulate this PHI again via the backedge allow us to iterate. */
9071 && gimple_phi_num_args (phi
) > 1
9072 && edges
== old_edges
9073 && lhs_vr
->type
!= VR_UNDEFINED
9074 && may_simulate_backedge_again
)
9076 /* Compare old and new ranges, fall back to varying if the
9077 values are not comparable. */
9078 int cmp_min
= compare_values (lhs_vr
->min
, vr_result
->min
);
9081 int cmp_max
= compare_values (lhs_vr
->max
, vr_result
->max
);
9085 /* For non VR_RANGE or for pointers fall back to varying if
9086 the range changed. */
9087 if ((lhs_vr
->type
!= VR_RANGE
|| vr_result
->type
!= VR_RANGE
9088 || POINTER_TYPE_P (TREE_TYPE (lhs
)))
9089 && (cmp_min
!= 0 || cmp_max
!= 0))
9092 /* If the new minimum is larger than the previous one
9093 retain the old value. If the new minimum value is smaller
9094 than the previous one and not -INF go all the way to -INF + 1.
9095 In the first case, to avoid infinite bouncing between different
9096 minimums, and in the other case to avoid iterating millions of
9097 times to reach -INF. Going to -INF + 1 also lets the following
9098 iteration compute whether there will be any overflow, at the
9099 expense of one additional iteration. */
9101 vr_result
->min
= lhs_vr
->min
;
9102 else if (cmp_min
> 0
9103 && !vrp_val_is_min (vr_result
->min
))
9105 = int_const_binop (PLUS_EXPR
,
9106 vrp_val_min (TREE_TYPE (vr_result
->min
)),
9107 build_int_cst (TREE_TYPE (vr_result
->min
), 1));
9109 /* Similarly for the maximum value. */
9111 vr_result
->max
= lhs_vr
->max
;
9112 else if (cmp_max
< 0
9113 && !vrp_val_is_max (vr_result
->max
))
9115 = int_const_binop (MINUS_EXPR
,
9116 vrp_val_max (TREE_TYPE (vr_result
->min
)),
9117 build_int_cst (TREE_TYPE (vr_result
->min
), 1));
9119 /* If we dropped either bound to +-INF then if this is a loop
9120 PHI node SCEV may known more about its value-range. */
9121 if (cmp_min
> 0 || cmp_min
< 0
9122 || cmp_max
< 0 || cmp_max
> 0)
9125 goto infinite_check
;
9131 set_value_range_to_varying (vr_result
);
9134 /* If this is a loop PHI node SCEV may known more about its value-range.
9135 scev_check can be reached from two paths, one is a fall through from above
9136 "varying" label, the other is direct goto from code block which tries to
9137 avoid infinite simulation. */
9138 if ((l
= loop_containing_stmt (phi
))
9139 && l
->header
== gimple_bb (phi
))
9140 adjust_range_with_scev (vr_result
, l
, phi
, lhs
);
9143 /* If we will end up with a (-INF, +INF) range, set it to
9144 VARYING. Same if the previous max value was invalid for
9145 the type and we end up with vr_result.min > vr_result.max. */
9146 if ((vr_result
->type
== VR_RANGE
|| vr_result
->type
== VR_ANTI_RANGE
)
9147 && !((vrp_val_is_max (vr_result
->max
) && vrp_val_is_min (vr_result
->min
))
9148 || compare_values (vr_result
->min
, vr_result
->max
) > 0))
9151 set_value_range_to_varying (vr_result
);
9153 /* If the new range is different than the previous value, keep
9159 /* Visit all arguments for PHI node PHI that flow through executable
9160 edges. If a valid value range can be derived from all the incoming
9161 value ranges, set a new range for the LHS of PHI. */
9163 static enum ssa_prop_result
9164 vrp_visit_phi_node (gphi
*phi
)
9166 tree lhs
= PHI_RESULT (phi
);
9167 value_range vr_result
= VR_INITIALIZER
;
9168 extract_range_from_phi_node (phi
, &vr_result
);
9169 if (update_value_range (lhs
, &vr_result
))
9171 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
9173 fprintf (dump_file
, "Found new range for ");
9174 print_generic_expr (dump_file
, lhs
);
9175 fprintf (dump_file
, ": ");
9176 dump_value_range (dump_file
, &vr_result
);
9177 fprintf (dump_file
, "\n");
9180 if (vr_result
.type
== VR_VARYING
)
9181 return SSA_PROP_VARYING
;
9183 return SSA_PROP_INTERESTING
;
9186 /* Nothing changed, don't add outgoing edges. */
9187 return SSA_PROP_NOT_INTERESTING
;
9190 /* Simplify boolean operations if the source is known
9191 to be already a boolean. */
9193 simplify_truth_ops_using_ranges (gimple_stmt_iterator
*gsi
, gimple
*stmt
)
9195 enum tree_code rhs_code
= gimple_assign_rhs_code (stmt
);
9197 bool need_conversion
;
9199 /* We handle only !=/== case here. */
9200 gcc_assert (rhs_code
== EQ_EXPR
|| rhs_code
== NE_EXPR
);
9202 op0
= gimple_assign_rhs1 (stmt
);
9203 if (!op_with_boolean_value_range_p (op0
))
9206 op1
= gimple_assign_rhs2 (stmt
);
9207 if (!op_with_boolean_value_range_p (op1
))
9210 /* Reduce number of cases to handle to NE_EXPR. As there is no
9211 BIT_XNOR_EXPR we cannot replace A == B with a single statement. */
9212 if (rhs_code
== EQ_EXPR
)
9214 if (TREE_CODE (op1
) == INTEGER_CST
)
9215 op1
= int_const_binop (BIT_XOR_EXPR
, op1
,
9216 build_int_cst (TREE_TYPE (op1
), 1));
9221 lhs
= gimple_assign_lhs (stmt
);
9223 = !useless_type_conversion_p (TREE_TYPE (lhs
), TREE_TYPE (op0
));
9225 /* Make sure to not sign-extend a 1-bit 1 when converting the result. */
9227 && !TYPE_UNSIGNED (TREE_TYPE (op0
))
9228 && TYPE_PRECISION (TREE_TYPE (op0
)) == 1
9229 && TYPE_PRECISION (TREE_TYPE (lhs
)) > 1)
9232 /* For A != 0 we can substitute A itself. */
9233 if (integer_zerop (op1
))
9234 gimple_assign_set_rhs_with_ops (gsi
,
9236 ? NOP_EXPR
: TREE_CODE (op0
), op0
);
9237 /* For A != B we substitute A ^ B. Either with conversion. */
9238 else if (need_conversion
)
9240 tree tem
= make_ssa_name (TREE_TYPE (op0
));
9242 = gimple_build_assign (tem
, BIT_XOR_EXPR
, op0
, op1
);
9243 gsi_insert_before (gsi
, newop
, GSI_SAME_STMT
);
9244 if (INTEGRAL_TYPE_P (TREE_TYPE (tem
))
9245 && TYPE_PRECISION (TREE_TYPE (tem
)) > 1)
9246 set_range_info (tem
, VR_RANGE
,
9247 wi::zero (TYPE_PRECISION (TREE_TYPE (tem
))),
9248 wi::one (TYPE_PRECISION (TREE_TYPE (tem
))));
9249 gimple_assign_set_rhs_with_ops (gsi
, NOP_EXPR
, tem
);
9253 gimple_assign_set_rhs_with_ops (gsi
, BIT_XOR_EXPR
, op0
, op1
);
9254 update_stmt (gsi_stmt (*gsi
));
9255 fold_stmt (gsi
, follow_single_use_edges
);
9260 /* Simplify a division or modulo operator to a right shift or bitwise and
9261 if the first operand is unsigned or is greater than zero and the second
9262 operand is an exact power of two. For TRUNC_MOD_EXPR op0 % op1 with
9263 constant op1 (op1min = op1) or with op1 in [op1min, op1max] range,
9264 optimize it into just op0 if op0's range is known to be a subset of
9265 [-op1min + 1, op1min - 1] for signed and [0, op1min - 1] for unsigned
9269 simplify_div_or_mod_using_ranges (gimple_stmt_iterator
*gsi
, gimple
*stmt
)
9271 enum tree_code rhs_code
= gimple_assign_rhs_code (stmt
);
9273 tree op0
= gimple_assign_rhs1 (stmt
);
9274 tree op1
= gimple_assign_rhs2 (stmt
);
9275 tree op0min
= NULL_TREE
, op0max
= NULL_TREE
;
9277 value_range
*vr
= NULL
;
9279 if (TREE_CODE (op0
) == INTEGER_CST
)
9286 vr
= get_value_range (op0
);
9287 if (range_int_cst_p (vr
))
9294 if (rhs_code
== TRUNC_MOD_EXPR
9295 && TREE_CODE (op1
) == SSA_NAME
)
9297 value_range
*vr1
= get_value_range (op1
);
9298 if (range_int_cst_p (vr1
))
9301 if (rhs_code
== TRUNC_MOD_EXPR
9302 && TREE_CODE (op1min
) == INTEGER_CST
9303 && tree_int_cst_sgn (op1min
) == 1
9305 && tree_int_cst_lt (op0max
, op1min
))
9307 if (TYPE_UNSIGNED (TREE_TYPE (op0
))
9308 || tree_int_cst_sgn (op0min
) >= 0
9309 || tree_int_cst_lt (fold_unary (NEGATE_EXPR
, TREE_TYPE (op1min
), op1min
),
9312 /* If op0 already has the range op0 % op1 has,
9313 then TRUNC_MOD_EXPR won't change anything. */
9314 gimple_assign_set_rhs_from_tree (gsi
, op0
);
9319 if (TREE_CODE (op0
) != SSA_NAME
)
9322 if (!integer_pow2p (op1
))
9324 /* X % -Y can be only optimized into X % Y either if
9325 X is not INT_MIN, or Y is not -1. Fold it now, as after
9326 remove_range_assertions the range info might be not available
9328 if (rhs_code
== TRUNC_MOD_EXPR
9329 && fold_stmt (gsi
, follow_single_use_edges
))
9334 if (TYPE_UNSIGNED (TREE_TYPE (op0
)))
9335 val
= integer_one_node
;
9340 val
= compare_range_with_value (GE_EXPR
, vr
, integer_zero_node
, &sop
);
9344 && integer_onep (val
)
9345 && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC
))
9347 location_t location
;
9349 if (!gimple_has_location (stmt
))
9350 location
= input_location
;
9352 location
= gimple_location (stmt
);
9353 warning_at (location
, OPT_Wstrict_overflow
,
9354 "assuming signed overflow does not occur when "
9355 "simplifying %</%> or %<%%%> to %<>>%> or %<&%>");
9359 if (val
&& integer_onep (val
))
9363 if (rhs_code
== TRUNC_DIV_EXPR
)
9365 t
= build_int_cst (integer_type_node
, tree_log2 (op1
));
9366 gimple_assign_set_rhs_code (stmt
, RSHIFT_EXPR
);
9367 gimple_assign_set_rhs1 (stmt
, op0
);
9368 gimple_assign_set_rhs2 (stmt
, t
);
9372 t
= build_int_cst (TREE_TYPE (op1
), 1);
9373 t
= int_const_binop (MINUS_EXPR
, op1
, t
);
9374 t
= fold_convert (TREE_TYPE (op0
), t
);
9376 gimple_assign_set_rhs_code (stmt
, BIT_AND_EXPR
);
9377 gimple_assign_set_rhs1 (stmt
, op0
);
9378 gimple_assign_set_rhs2 (stmt
, t
);
9382 fold_stmt (gsi
, follow_single_use_edges
);
9389 /* Simplify a min or max if the ranges of the two operands are
9390 disjoint. Return true if we do simplify. */
9393 simplify_min_or_max_using_ranges (gimple_stmt_iterator
*gsi
, gimple
*stmt
)
9395 tree op0
= gimple_assign_rhs1 (stmt
);
9396 tree op1
= gimple_assign_rhs2 (stmt
);
9400 val
= (vrp_evaluate_conditional_warnv_with_ops_using_ranges
9401 (LE_EXPR
, op0
, op1
, &sop
));
9405 val
= (vrp_evaluate_conditional_warnv_with_ops_using_ranges
9406 (LT_EXPR
, op0
, op1
, &sop
));
9411 if (sop
&& issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC
))
9413 location_t location
;
9415 if (!gimple_has_location (stmt
))
9416 location
= input_location
;
9418 location
= gimple_location (stmt
);
9419 warning_at (location
, OPT_Wstrict_overflow
,
9420 "assuming signed overflow does not occur when "
9421 "simplifying %<min/max (X,Y)%> to %<X%> or %<Y%>");
9424 /* VAL == TRUE -> OP0 < or <= op1
9425 VAL == FALSE -> OP0 > or >= op1. */
9426 tree res
= ((gimple_assign_rhs_code (stmt
) == MAX_EXPR
)
9427 == integer_zerop (val
)) ? op0
: op1
;
9428 gimple_assign_set_rhs_from_tree (gsi
, res
);
9435 /* If the operand to an ABS_EXPR is >= 0, then eliminate the
9436 ABS_EXPR. If the operand is <= 0, then simplify the
9437 ABS_EXPR into a NEGATE_EXPR. */
9440 simplify_abs_using_ranges (gimple_stmt_iterator
*gsi
, gimple
*stmt
)
9442 tree op
= gimple_assign_rhs1 (stmt
);
9443 value_range
*vr
= get_value_range (op
);
9450 val
= compare_range_with_value (LE_EXPR
, vr
, integer_zero_node
, &sop
);
9453 /* The range is neither <= 0 nor > 0. Now see if it is
9454 either < 0 or >= 0. */
9456 val
= compare_range_with_value (LT_EXPR
, vr
, integer_zero_node
,
9462 if (sop
&& issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC
))
9464 location_t location
;
9466 if (!gimple_has_location (stmt
))
9467 location
= input_location
;
9469 location
= gimple_location (stmt
);
9470 warning_at (location
, OPT_Wstrict_overflow
,
9471 "assuming signed overflow does not occur when "
9472 "simplifying %<abs (X)%> to %<X%> or %<-X%>");
9475 gimple_assign_set_rhs1 (stmt
, op
);
9476 if (integer_zerop (val
))
9477 gimple_assign_set_rhs_code (stmt
, SSA_NAME
);
9479 gimple_assign_set_rhs_code (stmt
, NEGATE_EXPR
);
9481 fold_stmt (gsi
, follow_single_use_edges
);
9489 /* Optimize away redundant BIT_AND_EXPR and BIT_IOR_EXPR.
9490 If all the bits that are being cleared by & are already
9491 known to be zero from VR, or all the bits that are being
9492 set by | are already known to be one from VR, the bit
9493 operation is redundant. */
9496 simplify_bit_ops_using_ranges (gimple_stmt_iterator
*gsi
, gimple
*stmt
)
9498 tree op0
= gimple_assign_rhs1 (stmt
);
9499 tree op1
= gimple_assign_rhs2 (stmt
);
9500 tree op
= NULL_TREE
;
9501 value_range vr0
= VR_INITIALIZER
;
9502 value_range vr1
= VR_INITIALIZER
;
9503 wide_int may_be_nonzero0
, may_be_nonzero1
;
9504 wide_int must_be_nonzero0
, must_be_nonzero1
;
9507 if (TREE_CODE (op0
) == SSA_NAME
)
9508 vr0
= *(get_value_range (op0
));
9509 else if (is_gimple_min_invariant (op0
))
9510 set_value_range_to_value (&vr0
, op0
, NULL
);
9514 if (TREE_CODE (op1
) == SSA_NAME
)
9515 vr1
= *(get_value_range (op1
));
9516 else if (is_gimple_min_invariant (op1
))
9517 set_value_range_to_value (&vr1
, op1
, NULL
);
9521 if (!zero_nonzero_bits_from_vr (TREE_TYPE (op0
), &vr0
, &may_be_nonzero0
,
9524 if (!zero_nonzero_bits_from_vr (TREE_TYPE (op1
), &vr1
, &may_be_nonzero1
,
9528 switch (gimple_assign_rhs_code (stmt
))
9531 mask
= may_be_nonzero0
.and_not (must_be_nonzero1
);
9537 mask
= may_be_nonzero1
.and_not (must_be_nonzero0
);
9545 mask
= may_be_nonzero0
.and_not (must_be_nonzero1
);
9551 mask
= may_be_nonzero1
.and_not (must_be_nonzero0
);
9562 if (op
== NULL_TREE
)
9565 gimple_assign_set_rhs_with_ops (gsi
, TREE_CODE (op
), op
);
9566 update_stmt (gsi_stmt (*gsi
));
9570 /* We are comparing trees OP0 and OP1 using COND_CODE. OP0 has
9571 a known value range VR.
9573 If there is one and only one value which will satisfy the
9574 conditional, then return that value. Else return NULL.
9576 If signed overflow must be undefined for the value to satisfy
9577 the conditional, then set *STRICT_OVERFLOW_P to true. */
9580 test_for_singularity (enum tree_code cond_code
, tree op0
,
9581 tree op1
, value_range
*vr
)
9586 /* Extract minimum/maximum values which satisfy the conditional as it was
9588 if (cond_code
== LE_EXPR
|| cond_code
== LT_EXPR
)
9590 min
= TYPE_MIN_VALUE (TREE_TYPE (op0
));
9593 if (cond_code
== LT_EXPR
)
9595 tree one
= build_int_cst (TREE_TYPE (op0
), 1);
9596 max
= fold_build2 (MINUS_EXPR
, TREE_TYPE (op0
), max
, one
);
9597 /* Signal to compare_values_warnv this expr doesn't overflow. */
9599 TREE_NO_WARNING (max
) = 1;
9602 else if (cond_code
== GE_EXPR
|| cond_code
== GT_EXPR
)
9604 max
= TYPE_MAX_VALUE (TREE_TYPE (op0
));
9607 if (cond_code
== GT_EXPR
)
9609 tree one
= build_int_cst (TREE_TYPE (op0
), 1);
9610 min
= fold_build2 (PLUS_EXPR
, TREE_TYPE (op0
), min
, one
);
9611 /* Signal to compare_values_warnv this expr doesn't overflow. */
9613 TREE_NO_WARNING (min
) = 1;
9617 /* Now refine the minimum and maximum values using any
9618 value range information we have for op0. */
9621 if (compare_values (vr
->min
, min
) == 1)
9623 if (compare_values (vr
->max
, max
) == -1)
9626 /* If the new min/max values have converged to a single value,
9627 then there is only one value which can satisfy the condition,
9628 return that value. */
9629 if (operand_equal_p (min
, max
, 0) && is_gimple_min_invariant (min
))
9635 /* Return whether the value range *VR fits in an integer type specified
9636 by PRECISION and UNSIGNED_P. */
9639 range_fits_type_p (value_range
*vr
, unsigned dest_precision
, signop dest_sgn
)
9642 unsigned src_precision
;
9646 /* We can only handle integral and pointer types. */
9647 src_type
= TREE_TYPE (vr
->min
);
9648 if (!INTEGRAL_TYPE_P (src_type
)
9649 && !POINTER_TYPE_P (src_type
))
9652 /* An extension is fine unless VR is SIGNED and dest_sgn is UNSIGNED,
9653 and so is an identity transform. */
9654 src_precision
= TYPE_PRECISION (TREE_TYPE (vr
->min
));
9655 src_sgn
= TYPE_SIGN (src_type
);
9656 if ((src_precision
< dest_precision
9657 && !(dest_sgn
== UNSIGNED
&& src_sgn
== SIGNED
))
9658 || (src_precision
== dest_precision
&& src_sgn
== dest_sgn
))
9661 /* Now we can only handle ranges with constant bounds. */
9662 if (vr
->type
!= VR_RANGE
9663 || TREE_CODE (vr
->min
) != INTEGER_CST
9664 || TREE_CODE (vr
->max
) != INTEGER_CST
)
9667 /* For sign changes, the MSB of the wide_int has to be clear.
9668 An unsigned value with its MSB set cannot be represented by
9669 a signed wide_int, while a negative value cannot be represented
9670 by an unsigned wide_int. */
9671 if (src_sgn
!= dest_sgn
9672 && (wi::lts_p (vr
->min
, 0) || wi::lts_p (vr
->max
, 0)))
9675 /* Then we can perform the conversion on both ends and compare
9676 the result for equality. */
9677 tem
= wi::ext (wi::to_widest (vr
->min
), dest_precision
, dest_sgn
);
9678 if (tem
!= wi::to_widest (vr
->min
))
9680 tem
= wi::ext (wi::to_widest (vr
->max
), dest_precision
, dest_sgn
);
9681 if (tem
!= wi::to_widest (vr
->max
))
9687 /* Simplify a conditional using a relational operator to an equality
9688 test if the range information indicates only one value can satisfy
9689 the original conditional. */
9692 simplify_cond_using_ranges_1 (gcond
*stmt
)
9694 tree op0
= gimple_cond_lhs (stmt
);
9695 tree op1
= gimple_cond_rhs (stmt
);
9696 enum tree_code cond_code
= gimple_cond_code (stmt
);
9698 if (cond_code
!= NE_EXPR
9699 && cond_code
!= EQ_EXPR
9700 && TREE_CODE (op0
) == SSA_NAME
9701 && INTEGRAL_TYPE_P (TREE_TYPE (op0
))
9702 && is_gimple_min_invariant (op1
))
9704 value_range
*vr
= get_value_range (op0
);
9706 /* If we have range information for OP0, then we might be
9707 able to simplify this conditional. */
9708 if (vr
->type
== VR_RANGE
)
9710 tree new_tree
= test_for_singularity (cond_code
, op0
, op1
, vr
);
9715 fprintf (dump_file
, "Simplified relational ");
9716 print_gimple_stmt (dump_file
, stmt
, 0);
9717 fprintf (dump_file
, " into ");
9720 gimple_cond_set_code (stmt
, EQ_EXPR
);
9721 gimple_cond_set_lhs (stmt
, op0
);
9722 gimple_cond_set_rhs (stmt
, new_tree
);
9728 print_gimple_stmt (dump_file
, stmt
, 0);
9729 fprintf (dump_file
, "\n");
9735 /* Try again after inverting the condition. We only deal
9736 with integral types here, so no need to worry about
9737 issues with inverting FP comparisons. */
9738 new_tree
= test_for_singularity
9739 (invert_tree_comparison (cond_code
, false),
9745 fprintf (dump_file
, "Simplified relational ");
9746 print_gimple_stmt (dump_file
, stmt
, 0);
9747 fprintf (dump_file
, " into ");
9750 gimple_cond_set_code (stmt
, NE_EXPR
);
9751 gimple_cond_set_lhs (stmt
, op0
);
9752 gimple_cond_set_rhs (stmt
, new_tree
);
9758 print_gimple_stmt (dump_file
, stmt
, 0);
9759 fprintf (dump_file
, "\n");
9769 /* STMT is a conditional at the end of a basic block.
9771 If the conditional is of the form SSA_NAME op constant and the SSA_NAME
9772 was set via a type conversion, try to replace the SSA_NAME with the RHS
9773 of the type conversion. Doing so makes the conversion dead which helps
9774 subsequent passes. */
9777 simplify_cond_using_ranges_2 (gcond
*stmt
)
9779 tree op0
= gimple_cond_lhs (stmt
);
9780 tree op1
= gimple_cond_rhs (stmt
);
9782 /* If we have a comparison of an SSA_NAME (OP0) against a constant,
9783 see if OP0 was set by a type conversion where the source of
9784 the conversion is another SSA_NAME with a range that fits
9785 into the range of OP0's type.
9787 If so, the conversion is redundant as the earlier SSA_NAME can be
9788 used for the comparison directly if we just massage the constant in the
9790 if (TREE_CODE (op0
) == SSA_NAME
9791 && TREE_CODE (op1
) == INTEGER_CST
)
9793 gimple
*def_stmt
= SSA_NAME_DEF_STMT (op0
);
9796 if (!is_gimple_assign (def_stmt
)
9797 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt
)))
9800 innerop
= gimple_assign_rhs1 (def_stmt
);
9802 if (TREE_CODE (innerop
) == SSA_NAME
9803 && !POINTER_TYPE_P (TREE_TYPE (innerop
))
9804 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (innerop
)
9805 && desired_pro_or_demotion_p (TREE_TYPE (innerop
), TREE_TYPE (op0
)))
9807 value_range
*vr
= get_value_range (innerop
);
9809 if (range_int_cst_p (vr
)
9810 && range_fits_type_p (vr
,
9811 TYPE_PRECISION (TREE_TYPE (op0
)),
9812 TYPE_SIGN (TREE_TYPE (op0
)))
9813 && int_fits_type_p (op1
, TREE_TYPE (innerop
)))
9815 tree newconst
= fold_convert (TREE_TYPE (innerop
), op1
);
9816 gimple_cond_set_lhs (stmt
, innerop
);
9817 gimple_cond_set_rhs (stmt
, newconst
);
9819 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
9821 fprintf (dump_file
, "Folded into: ");
9822 print_gimple_stmt (dump_file
, stmt
, 0, TDF_SLIM
);
9823 fprintf (dump_file
, "\n");
9830 /* Simplify a switch statement using the value range of the switch
9834 simplify_switch_using_ranges (gswitch
*stmt
)
9836 tree op
= gimple_switch_index (stmt
);
9837 value_range
*vr
= NULL
;
9841 size_t i
= 0, j
= 0, n
, n2
;
9844 size_t k
= 1, l
= 0;
9846 if (TREE_CODE (op
) == SSA_NAME
)
9848 vr
= get_value_range (op
);
9850 /* We can only handle integer ranges. */
9851 if ((vr
->type
!= VR_RANGE
9852 && vr
->type
!= VR_ANTI_RANGE
)
9853 || symbolic_range_p (vr
))
9856 /* Find case label for min/max of the value range. */
9857 take_default
= !find_case_label_ranges (stmt
, vr
, &i
, &j
, &k
, &l
);
9859 else if (TREE_CODE (op
) == INTEGER_CST
)
9861 take_default
= !find_case_label_index (stmt
, 1, op
, &i
);
9875 n
= gimple_switch_num_labels (stmt
);
9877 /* We can truncate the case label ranges that partially overlap with OP's
9879 size_t min_idx
= 1, max_idx
= 0;
9881 find_case_label_range (stmt
, vr
->min
, vr
->max
, &min_idx
, &max_idx
);
9882 if (min_idx
<= max_idx
)
9884 tree min_label
= gimple_switch_label (stmt
, min_idx
);
9885 tree max_label
= gimple_switch_label (stmt
, max_idx
);
9887 /* Avoid changing the type of the case labels when truncating. */
9888 tree case_label_type
= TREE_TYPE (CASE_LOW (min_label
));
9889 tree vr_min
= fold_convert (case_label_type
, vr
->min
);
9890 tree vr_max
= fold_convert (case_label_type
, vr
->max
);
9892 if (vr
->type
== VR_RANGE
)
9894 /* If OP's value range is [2,8] and the low label range is
9895 0 ... 3, truncate the label's range to 2 .. 3. */
9896 if (tree_int_cst_compare (CASE_LOW (min_label
), vr_min
) < 0
9897 && CASE_HIGH (min_label
) != NULL_TREE
9898 && tree_int_cst_compare (CASE_HIGH (min_label
), vr_min
) >= 0)
9899 CASE_LOW (min_label
) = vr_min
;
9901 /* If OP's value range is [2,8] and the high label range is
9902 7 ... 10, truncate the label's range to 7 .. 8. */
9903 if (tree_int_cst_compare (CASE_LOW (max_label
), vr_max
) <= 0
9904 && CASE_HIGH (max_label
) != NULL_TREE
9905 && tree_int_cst_compare (CASE_HIGH (max_label
), vr_max
) > 0)
9906 CASE_HIGH (max_label
) = vr_max
;
9908 else if (vr
->type
== VR_ANTI_RANGE
)
9910 tree one_cst
= build_one_cst (case_label_type
);
9912 if (min_label
== max_label
)
9914 /* If OP's value range is ~[7,8] and the label's range is
9915 7 ... 10, truncate the label's range to 9 ... 10. */
9916 if (tree_int_cst_compare (CASE_LOW (min_label
), vr_min
) == 0
9917 && CASE_HIGH (min_label
) != NULL_TREE
9918 && tree_int_cst_compare (CASE_HIGH (min_label
), vr_max
) > 0)
9919 CASE_LOW (min_label
)
9920 = int_const_binop (PLUS_EXPR
, vr_max
, one_cst
);
9922 /* If OP's value range is ~[7,8] and the label's range is
9923 5 ... 8, truncate the label's range to 5 ... 6. */
9924 if (tree_int_cst_compare (CASE_LOW (min_label
), vr_min
) < 0
9925 && CASE_HIGH (min_label
) != NULL_TREE
9926 && tree_int_cst_compare (CASE_HIGH (min_label
), vr_max
) == 0)
9927 CASE_HIGH (min_label
)
9928 = int_const_binop (MINUS_EXPR
, vr_min
, one_cst
);
9932 /* If OP's value range is ~[2,8] and the low label range is
9933 0 ... 3, truncate the label's range to 0 ... 1. */
9934 if (tree_int_cst_compare (CASE_LOW (min_label
), vr_min
) < 0
9935 && CASE_HIGH (min_label
) != NULL_TREE
9936 && tree_int_cst_compare (CASE_HIGH (min_label
), vr_min
) >= 0)
9937 CASE_HIGH (min_label
)
9938 = int_const_binop (MINUS_EXPR
, vr_min
, one_cst
);
9940 /* If OP's value range is ~[2,8] and the high label range is
9941 7 ... 10, truncate the label's range to 9 ... 10. */
9942 if (tree_int_cst_compare (CASE_LOW (max_label
), vr_max
) <= 0
9943 && CASE_HIGH (max_label
) != NULL_TREE
9944 && tree_int_cst_compare (CASE_HIGH (max_label
), vr_max
) > 0)
9945 CASE_LOW (max_label
)
9946 = int_const_binop (PLUS_EXPR
, vr_max
, one_cst
);
9950 /* Canonicalize singleton case ranges. */
9951 if (tree_int_cst_equal (CASE_LOW (min_label
), CASE_HIGH (min_label
)))
9952 CASE_HIGH (min_label
) = NULL_TREE
;
9953 if (tree_int_cst_equal (CASE_LOW (max_label
), CASE_HIGH (max_label
)))
9954 CASE_HIGH (max_label
) = NULL_TREE
;
9957 /* We can also eliminate case labels that lie completely outside OP's value
9960 /* Bail out if this is just all edges taken. */
9966 /* Build a new vector of taken case labels. */
9967 vec2
= make_tree_vec (j
- i
+ 1 + l
- k
+ 1 + (int)take_default
);
9970 /* Add the default edge, if necessary. */
9972 TREE_VEC_ELT (vec2
, n2
++) = gimple_switch_default_label (stmt
);
9974 for (; i
<= j
; ++i
, ++n2
)
9975 TREE_VEC_ELT (vec2
, n2
) = gimple_switch_label (stmt
, i
);
9977 for (; k
<= l
; ++k
, ++n2
)
9978 TREE_VEC_ELT (vec2
, n2
) = gimple_switch_label (stmt
, k
);
9980 /* Mark needed edges. */
9981 for (i
= 0; i
< n2
; ++i
)
9983 e
= find_edge (gimple_bb (stmt
),
9984 label_to_block (CASE_LABEL (TREE_VEC_ELT (vec2
, i
))));
9985 e
->aux
= (void *)-1;
9988 /* Queue not needed edges for later removal. */
9989 FOR_EACH_EDGE (e
, ei
, gimple_bb (stmt
)->succs
)
9991 if (e
->aux
== (void *)-1)
9997 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
9999 fprintf (dump_file
, "removing unreachable case label\n");
10001 to_remove_edges
.safe_push (e
);
10002 e
->flags
&= ~EDGE_EXECUTABLE
;
10005 /* And queue an update for the stmt. */
10008 to_update_switch_stmts
.safe_push (su
);
10012 /* Simplify an integral conversion from an SSA name in STMT. */
10015 simplify_conversion_using_ranges (gimple_stmt_iterator
*gsi
, gimple
*stmt
)
10017 tree innerop
, middleop
, finaltype
;
10019 signop inner_sgn
, middle_sgn
, final_sgn
;
10020 unsigned inner_prec
, middle_prec
, final_prec
;
10021 widest_int innermin
, innermed
, innermax
, middlemin
, middlemed
, middlemax
;
10023 finaltype
= TREE_TYPE (gimple_assign_lhs (stmt
));
10024 if (!INTEGRAL_TYPE_P (finaltype
))
10026 middleop
= gimple_assign_rhs1 (stmt
);
10027 def_stmt
= SSA_NAME_DEF_STMT (middleop
);
10028 if (!is_gimple_assign (def_stmt
)
10029 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt
)))
10031 innerop
= gimple_assign_rhs1 (def_stmt
);
10032 if (TREE_CODE (innerop
) != SSA_NAME
10033 || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (innerop
))
10036 /* Get the value-range of the inner operand. Use get_range_info in
10037 case innerop was created during substitute-and-fold. */
10038 wide_int imin
, imax
;
10039 if (!INTEGRAL_TYPE_P (TREE_TYPE (innerop
))
10040 || get_range_info (innerop
, &imin
, &imax
) != VR_RANGE
)
10042 innermin
= widest_int::from (imin
, TYPE_SIGN (TREE_TYPE (innerop
)));
10043 innermax
= widest_int::from (imax
, TYPE_SIGN (TREE_TYPE (innerop
)));
10045 /* Simulate the conversion chain to check if the result is equal if
10046 the middle conversion is removed. */
10047 inner_prec
= TYPE_PRECISION (TREE_TYPE (innerop
));
10048 middle_prec
= TYPE_PRECISION (TREE_TYPE (middleop
));
10049 final_prec
= TYPE_PRECISION (finaltype
);
10051 /* If the first conversion is not injective, the second must not
10053 if (wi::gtu_p (innermax
- innermin
,
10054 wi::mask
<widest_int
> (middle_prec
, false))
10055 && middle_prec
< final_prec
)
10057 /* We also want a medium value so that we can track the effect that
10058 narrowing conversions with sign change have. */
10059 inner_sgn
= TYPE_SIGN (TREE_TYPE (innerop
));
10060 if (inner_sgn
== UNSIGNED
)
10061 innermed
= wi::shifted_mask
<widest_int
> (1, inner_prec
- 1, false);
10064 if (wi::cmp (innermin
, innermed
, inner_sgn
) >= 0
10065 || wi::cmp (innermed
, innermax
, inner_sgn
) >= 0)
10066 innermed
= innermin
;
10068 middle_sgn
= TYPE_SIGN (TREE_TYPE (middleop
));
10069 middlemin
= wi::ext (innermin
, middle_prec
, middle_sgn
);
10070 middlemed
= wi::ext (innermed
, middle_prec
, middle_sgn
);
10071 middlemax
= wi::ext (innermax
, middle_prec
, middle_sgn
);
10073 /* Require that the final conversion applied to both the original
10074 and the intermediate range produces the same result. */
10075 final_sgn
= TYPE_SIGN (finaltype
);
10076 if (wi::ext (middlemin
, final_prec
, final_sgn
)
10077 != wi::ext (innermin
, final_prec
, final_sgn
)
10078 || wi::ext (middlemed
, final_prec
, final_sgn
)
10079 != wi::ext (innermed
, final_prec
, final_sgn
)
10080 || wi::ext (middlemax
, final_prec
, final_sgn
)
10081 != wi::ext (innermax
, final_prec
, final_sgn
))
10084 gimple_assign_set_rhs1 (stmt
, innerop
);
10085 fold_stmt (gsi
, follow_single_use_edges
);
10089 /* Simplify a conversion from integral SSA name to float in STMT. */
10092 simplify_float_conversion_using_ranges (gimple_stmt_iterator
*gsi
,
10095 tree rhs1
= gimple_assign_rhs1 (stmt
);
10096 value_range
*vr
= get_value_range (rhs1
);
10097 machine_mode fltmode
= TYPE_MODE (TREE_TYPE (gimple_assign_lhs (stmt
)));
10102 /* We can only handle constant ranges. */
10103 if (vr
->type
!= VR_RANGE
10104 || TREE_CODE (vr
->min
) != INTEGER_CST
10105 || TREE_CODE (vr
->max
) != INTEGER_CST
)
10108 /* First check if we can use a signed type in place of an unsigned. */
10109 if (TYPE_UNSIGNED (TREE_TYPE (rhs1
))
10110 && (can_float_p (fltmode
, TYPE_MODE (TREE_TYPE (rhs1
)), 0)
10111 != CODE_FOR_nothing
)
10112 && range_fits_type_p (vr
, TYPE_PRECISION (TREE_TYPE (rhs1
)), SIGNED
))
10113 mode
= TYPE_MODE (TREE_TYPE (rhs1
));
10114 /* If we can do the conversion in the current input mode do nothing. */
10115 else if (can_float_p (fltmode
, TYPE_MODE (TREE_TYPE (rhs1
)),
10116 TYPE_UNSIGNED (TREE_TYPE (rhs1
))) != CODE_FOR_nothing
)
10118 /* Otherwise search for a mode we can use, starting from the narrowest
10119 integer mode available. */
10122 mode
= GET_CLASS_NARROWEST_MODE (MODE_INT
);
10125 /* If we cannot do a signed conversion to float from mode
10126 or if the value-range does not fit in the signed type
10127 try with a wider mode. */
10128 if (can_float_p (fltmode
, mode
, 0) != CODE_FOR_nothing
10129 && range_fits_type_p (vr
, GET_MODE_PRECISION (mode
), SIGNED
))
10132 /* But do not widen the input. Instead leave that to the
10133 optabs expansion code. */
10134 if (!GET_MODE_WIDER_MODE (mode
).exists (&mode
)
10135 || GET_MODE_PRECISION (mode
) > TYPE_PRECISION (TREE_TYPE (rhs1
)))
10140 /* It works, insert a truncation or sign-change before the
10141 float conversion. */
10142 tem
= make_ssa_name (build_nonstandard_integer_type
10143 (GET_MODE_PRECISION (mode
), 0));
10144 conv
= gimple_build_assign (tem
, NOP_EXPR
, rhs1
);
10145 gsi_insert_before (gsi
, conv
, GSI_SAME_STMT
);
10146 gimple_assign_set_rhs1 (stmt
, tem
);
10147 fold_stmt (gsi
, follow_single_use_edges
);
10152 /* Simplify an internal fn call using ranges if possible. */
10155 simplify_internal_call_using_ranges (gimple_stmt_iterator
*gsi
, gimple
*stmt
)
10157 enum tree_code subcode
;
10158 bool is_ubsan
= false;
10160 switch (gimple_call_internal_fn (stmt
))
10162 case IFN_UBSAN_CHECK_ADD
:
10163 subcode
= PLUS_EXPR
;
10166 case IFN_UBSAN_CHECK_SUB
:
10167 subcode
= MINUS_EXPR
;
10170 case IFN_UBSAN_CHECK_MUL
:
10171 subcode
= MULT_EXPR
;
10174 case IFN_ADD_OVERFLOW
:
10175 subcode
= PLUS_EXPR
;
10177 case IFN_SUB_OVERFLOW
:
10178 subcode
= MINUS_EXPR
;
10180 case IFN_MUL_OVERFLOW
:
10181 subcode
= MULT_EXPR
;
10187 tree op0
= gimple_call_arg (stmt
, 0);
10188 tree op1
= gimple_call_arg (stmt
, 1);
10192 type
= TREE_TYPE (op0
);
10193 if (VECTOR_TYPE_P (type
))
10196 else if (gimple_call_lhs (stmt
) == NULL_TREE
)
10199 type
= TREE_TYPE (TREE_TYPE (gimple_call_lhs (stmt
)));
10200 if (!check_for_binary_op_overflow (subcode
, type
, op0
, op1
, &ovf
)
10201 || (is_ubsan
&& ovf
))
10205 location_t loc
= gimple_location (stmt
);
10207 g
= gimple_build_assign (gimple_call_lhs (stmt
), subcode
, op0
, op1
);
10210 int prec
= TYPE_PRECISION (type
);
10213 || !useless_type_conversion_p (type
, TREE_TYPE (op0
))
10214 || !useless_type_conversion_p (type
, TREE_TYPE (op1
)))
10215 utype
= build_nonstandard_integer_type (prec
, 1);
10216 if (TREE_CODE (op0
) == INTEGER_CST
)
10217 op0
= fold_convert (utype
, op0
);
10218 else if (!useless_type_conversion_p (utype
, TREE_TYPE (op0
)))
10220 g
= gimple_build_assign (make_ssa_name (utype
), NOP_EXPR
, op0
);
10221 gimple_set_location (g
, loc
);
10222 gsi_insert_before (gsi
, g
, GSI_SAME_STMT
);
10223 op0
= gimple_assign_lhs (g
);
10225 if (TREE_CODE (op1
) == INTEGER_CST
)
10226 op1
= fold_convert (utype
, op1
);
10227 else if (!useless_type_conversion_p (utype
, TREE_TYPE (op1
)))
10229 g
= gimple_build_assign (make_ssa_name (utype
), NOP_EXPR
, op1
);
10230 gimple_set_location (g
, loc
);
10231 gsi_insert_before (gsi
, g
, GSI_SAME_STMT
);
10232 op1
= gimple_assign_lhs (g
);
10234 g
= gimple_build_assign (make_ssa_name (utype
), subcode
, op0
, op1
);
10235 gimple_set_location (g
, loc
);
10236 gsi_insert_before (gsi
, g
, GSI_SAME_STMT
);
10239 g
= gimple_build_assign (make_ssa_name (type
), NOP_EXPR
,
10240 gimple_assign_lhs (g
));
10241 gimple_set_location (g
, loc
);
10242 gsi_insert_before (gsi
, g
, GSI_SAME_STMT
);
10244 g
= gimple_build_assign (gimple_call_lhs (stmt
), COMPLEX_EXPR
,
10245 gimple_assign_lhs (g
),
10246 build_int_cst (type
, ovf
));
10248 gimple_set_location (g
, loc
);
10249 gsi_replace (gsi
, g
, false);
10253 /* Return true if VAR is a two-valued variable. Set a and b with the
10254 two-values when it is true. Return false otherwise. */
10257 two_valued_val_range_p (tree var
, tree
*a
, tree
*b
)
10259 value_range
*vr
= get_value_range (var
);
10260 if ((vr
->type
!= VR_RANGE
10261 && vr
->type
!= VR_ANTI_RANGE
)
10262 || TREE_CODE (vr
->min
) != INTEGER_CST
10263 || TREE_CODE (vr
->max
) != INTEGER_CST
)
10266 if (vr
->type
== VR_RANGE
10267 && wi::sub (vr
->max
, vr
->min
) == 1)
10274 /* ~[TYPE_MIN + 1, TYPE_MAX - 1] */
10275 if (vr
->type
== VR_ANTI_RANGE
10276 && wi::sub (vr
->min
, vrp_val_min (TREE_TYPE (var
))) == 1
10277 && wi::sub (vrp_val_max (TREE_TYPE (var
)), vr
->max
) == 1)
10279 *a
= vrp_val_min (TREE_TYPE (var
));
10280 *b
= vrp_val_max (TREE_TYPE (var
));
10287 /* Simplify STMT using ranges if possible. */
10290 simplify_stmt_using_ranges (gimple_stmt_iterator
*gsi
)
10292 gimple
*stmt
= gsi_stmt (*gsi
);
10293 if (is_gimple_assign (stmt
))
10295 enum tree_code rhs_code
= gimple_assign_rhs_code (stmt
);
10296 tree rhs1
= gimple_assign_rhs1 (stmt
);
10297 tree rhs2
= gimple_assign_rhs2 (stmt
);
10298 tree lhs
= gimple_assign_lhs (stmt
);
10299 tree val1
= NULL_TREE
, val2
= NULL_TREE
;
10300 use_operand_p use_p
;
10304 LHS = CST BINOP VAR
10305 Where VAR is two-valued and LHS is used in GIMPLE_COND only
10307 LHS = VAR == VAL1 ? (CST BINOP VAL1) : (CST BINOP VAL2)
10310 LHS = VAR BINOP CST
10311 Where VAR is two-valued and LHS is used in GIMPLE_COND only
10313 LHS = VAR == VAL1 ? (VAL1 BINOP CST) : (VAL2 BINOP CST) */
10315 if (TREE_CODE_CLASS (rhs_code
) == tcc_binary
10316 && INTEGRAL_TYPE_P (TREE_TYPE (lhs
))
10317 && ((TREE_CODE (rhs1
) == INTEGER_CST
10318 && TREE_CODE (rhs2
) == SSA_NAME
)
10319 || (TREE_CODE (rhs2
) == INTEGER_CST
10320 && TREE_CODE (rhs1
) == SSA_NAME
))
10321 && single_imm_use (lhs
, &use_p
, &use_stmt
)
10322 && gimple_code (use_stmt
) == GIMPLE_COND
)
10325 tree new_rhs1
= NULL_TREE
;
10326 tree new_rhs2
= NULL_TREE
;
10327 tree cmp_var
= NULL_TREE
;
10329 if (TREE_CODE (rhs2
) == SSA_NAME
10330 && two_valued_val_range_p (rhs2
, &val1
, &val2
))
10332 /* Optimize RHS1 OP [VAL1, VAL2]. */
10333 new_rhs1
= int_const_binop (rhs_code
, rhs1
, val1
);
10334 new_rhs2
= int_const_binop (rhs_code
, rhs1
, val2
);
10337 else if (TREE_CODE (rhs1
) == SSA_NAME
10338 && two_valued_val_range_p (rhs1
, &val1
, &val2
))
10340 /* Optimize [VAL1, VAL2] OP RHS2. */
10341 new_rhs1
= int_const_binop (rhs_code
, val1
, rhs2
);
10342 new_rhs2
= int_const_binop (rhs_code
, val2
, rhs2
);
10346 /* If we could not find two-vals or the optimzation is invalid as
10347 in divide by zero, new_rhs1 / new_rhs will be NULL_TREE. */
10348 if (new_rhs1
&& new_rhs2
)
10350 tree cond
= build2 (EQ_EXPR
, boolean_type_node
, cmp_var
, val1
);
10351 gimple_assign_set_rhs_with_ops (gsi
,
10355 update_stmt (gsi_stmt (*gsi
));
10356 fold_stmt (gsi
, follow_single_use_edges
);
10365 /* Transform EQ_EXPR, NE_EXPR into BIT_XOR_EXPR or identity
10366 if the RHS is zero or one, and the LHS are known to be boolean
10368 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
10369 return simplify_truth_ops_using_ranges (gsi
, stmt
);
10372 /* Transform TRUNC_DIV_EXPR and TRUNC_MOD_EXPR into RSHIFT_EXPR
10373 and BIT_AND_EXPR respectively if the first operand is greater
10374 than zero and the second operand is an exact power of two.
10375 Also optimize TRUNC_MOD_EXPR away if the second operand is
10376 constant and the first operand already has the right value
10378 case TRUNC_DIV_EXPR
:
10379 case TRUNC_MOD_EXPR
:
10380 if ((TREE_CODE (rhs1
) == SSA_NAME
10381 || TREE_CODE (rhs1
) == INTEGER_CST
)
10382 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
10383 return simplify_div_or_mod_using_ranges (gsi
, stmt
);
10386 /* Transform ABS (X) into X or -X as appropriate. */
10388 if (TREE_CODE (rhs1
) == SSA_NAME
10389 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
10390 return simplify_abs_using_ranges (gsi
, stmt
);
10395 /* Optimize away BIT_AND_EXPR and BIT_IOR_EXPR
10396 if all the bits being cleared are already cleared or
10397 all the bits being set are already set. */
10398 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
10399 return simplify_bit_ops_using_ranges (gsi
, stmt
);
10403 if (TREE_CODE (rhs1
) == SSA_NAME
10404 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
10405 return simplify_conversion_using_ranges (gsi
, stmt
);
10409 if (TREE_CODE (rhs1
) == SSA_NAME
10410 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
10411 return simplify_float_conversion_using_ranges (gsi
, stmt
);
10416 return simplify_min_or_max_using_ranges (gsi
, stmt
);
10422 else if (gimple_code (stmt
) == GIMPLE_COND
)
10423 return simplify_cond_using_ranges_1 (as_a
<gcond
*> (stmt
));
10424 else if (gimple_code (stmt
) == GIMPLE_SWITCH
)
10425 return simplify_switch_using_ranges (as_a
<gswitch
*> (stmt
));
10426 else if (is_gimple_call (stmt
)
10427 && gimple_call_internal_p (stmt
))
10428 return simplify_internal_call_using_ranges (gsi
, stmt
);
10433 /* If the statement pointed by SI has a predicate whose value can be
10434 computed using the value range information computed by VRP, compute
10435 its value and return true. Otherwise, return false. */
10438 fold_predicate_in (gimple_stmt_iterator
*si
)
10440 bool assignment_p
= false;
10442 gimple
*stmt
= gsi_stmt (*si
);
10444 if (is_gimple_assign (stmt
)
10445 && TREE_CODE_CLASS (gimple_assign_rhs_code (stmt
)) == tcc_comparison
)
10447 assignment_p
= true;
10448 val
= vrp_evaluate_conditional (gimple_assign_rhs_code (stmt
),
10449 gimple_assign_rhs1 (stmt
),
10450 gimple_assign_rhs2 (stmt
),
10453 else if (gcond
*cond_stmt
= dyn_cast
<gcond
*> (stmt
))
10454 val
= vrp_evaluate_conditional (gimple_cond_code (cond_stmt
),
10455 gimple_cond_lhs (cond_stmt
),
10456 gimple_cond_rhs (cond_stmt
),
10464 val
= fold_convert (gimple_expr_type (stmt
), val
);
10468 fprintf (dump_file
, "Folding predicate ");
10469 print_gimple_expr (dump_file
, stmt
, 0);
10470 fprintf (dump_file
, " to ");
10471 print_generic_expr (dump_file
, val
);
10472 fprintf (dump_file
, "\n");
10475 if (is_gimple_assign (stmt
))
10476 gimple_assign_set_rhs_from_tree (si
, val
);
10479 gcc_assert (gimple_code (stmt
) == GIMPLE_COND
);
10480 gcond
*cond_stmt
= as_a
<gcond
*> (stmt
);
10481 if (integer_zerop (val
))
10482 gimple_cond_make_false (cond_stmt
);
10483 else if (integer_onep (val
))
10484 gimple_cond_make_true (cond_stmt
);
10486 gcc_unreachable ();
10495 /* Callback for substitute_and_fold folding the stmt at *SI. */
10498 vrp_fold_stmt (gimple_stmt_iterator
*si
)
10500 if (fold_predicate_in (si
))
10503 return simplify_stmt_using_ranges (si
);
10506 /* Return the LHS of any ASSERT_EXPR where OP appears as the first
10507 argument to the ASSERT_EXPR and in which the ASSERT_EXPR dominates
10508 BB. If no such ASSERT_EXPR is found, return OP. */
10511 lhs_of_dominating_assert (tree op
, basic_block bb
, gimple
*stmt
)
10513 imm_use_iterator imm_iter
;
10515 use_operand_p use_p
;
10517 if (TREE_CODE (op
) == SSA_NAME
)
10519 FOR_EACH_IMM_USE_FAST (use_p
, imm_iter
, op
)
10521 use_stmt
= USE_STMT (use_p
);
10522 if (use_stmt
!= stmt
10523 && gimple_assign_single_p (use_stmt
)
10524 && TREE_CODE (gimple_assign_rhs1 (use_stmt
)) == ASSERT_EXPR
10525 && TREE_OPERAND (gimple_assign_rhs1 (use_stmt
), 0) == op
10526 && dominated_by_p (CDI_DOMINATORS
, bb
, gimple_bb (use_stmt
)))
10527 return gimple_assign_lhs (use_stmt
);
10533 /* A trivial wrapper so that we can present the generic jump threading
10534 code with a simple API for simplifying statements. STMT is the
10535 statement we want to simplify, WITHIN_STMT provides the location
10536 for any overflow warnings. */
10539 simplify_stmt_for_jump_threading (gimple
*stmt
, gimple
*within_stmt
,
10540 class avail_exprs_stack
*avail_exprs_stack ATTRIBUTE_UNUSED
,
10543 /* First see if the conditional is in the hash table. */
10544 tree cached_lhs
= avail_exprs_stack
->lookup_avail_expr (stmt
, false, true);
10545 if (cached_lhs
&& is_gimple_min_invariant (cached_lhs
))
10548 if (gcond
*cond_stmt
= dyn_cast
<gcond
*> (stmt
))
10550 tree op0
= gimple_cond_lhs (cond_stmt
);
10551 op0
= lhs_of_dominating_assert (op0
, bb
, stmt
);
10553 tree op1
= gimple_cond_rhs (cond_stmt
);
10554 op1
= lhs_of_dominating_assert (op1
, bb
, stmt
);
10556 return vrp_evaluate_conditional (gimple_cond_code (cond_stmt
),
10557 op0
, op1
, within_stmt
);
10560 /* We simplify a switch statement by trying to determine which case label
10561 will be taken. If we are successful then we return the corresponding
10562 CASE_LABEL_EXPR. */
10563 if (gswitch
*switch_stmt
= dyn_cast
<gswitch
*> (stmt
))
10565 tree op
= gimple_switch_index (switch_stmt
);
10566 if (TREE_CODE (op
) != SSA_NAME
)
10569 op
= lhs_of_dominating_assert (op
, bb
, stmt
);
10571 value_range
*vr
= get_value_range (op
);
10572 if ((vr
->type
!= VR_RANGE
&& vr
->type
!= VR_ANTI_RANGE
)
10573 || symbolic_range_p (vr
))
10576 if (vr
->type
== VR_RANGE
)
10579 /* Get the range of labels that contain a part of the operand's
10581 find_case_label_range (switch_stmt
, vr
->min
, vr
->max
, &i
, &j
);
10583 /* Is there only one such label? */
10586 tree label
= gimple_switch_label (switch_stmt
, i
);
10588 /* The i'th label will be taken only if the value range of the
10589 operand is entirely within the bounds of this label. */
10590 if (CASE_HIGH (label
) != NULL_TREE
10591 ? (tree_int_cst_compare (CASE_LOW (label
), vr
->min
) <= 0
10592 && tree_int_cst_compare (CASE_HIGH (label
), vr
->max
) >= 0)
10593 : (tree_int_cst_equal (CASE_LOW (label
), vr
->min
)
10594 && tree_int_cst_equal (vr
->min
, vr
->max
)))
10598 /* If there are no such labels then the default label will be
10601 return gimple_switch_label (switch_stmt
, 0);
10604 if (vr
->type
== VR_ANTI_RANGE
)
10606 unsigned n
= gimple_switch_num_labels (switch_stmt
);
10607 tree min_label
= gimple_switch_label (switch_stmt
, 1);
10608 tree max_label
= gimple_switch_label (switch_stmt
, n
- 1);
10610 /* The default label will be taken only if the anti-range of the
10611 operand is entirely outside the bounds of all the (non-default)
10613 if (tree_int_cst_compare (vr
->min
, CASE_LOW (min_label
)) <= 0
10614 && (CASE_HIGH (max_label
) != NULL_TREE
10615 ? tree_int_cst_compare (vr
->max
, CASE_HIGH (max_label
)) >= 0
10616 : tree_int_cst_compare (vr
->max
, CASE_LOW (max_label
)) >= 0))
10617 return gimple_switch_label (switch_stmt
, 0);
10623 if (gassign
*assign_stmt
= dyn_cast
<gassign
*> (stmt
))
10625 value_range new_vr
= VR_INITIALIZER
;
10626 tree lhs
= gimple_assign_lhs (assign_stmt
);
10628 if (TREE_CODE (lhs
) == SSA_NAME
10629 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs
))
10630 || POINTER_TYPE_P (TREE_TYPE (lhs
))))
10632 extract_range_from_assignment (&new_vr
, assign_stmt
);
10633 if (range_int_cst_singleton_p (&new_vr
))
10641 class vrp_dom_walker
: public dom_walker
10644 vrp_dom_walker (cdi_direction direction
,
10645 class const_and_copies
*const_and_copies
,
10646 class avail_exprs_stack
*avail_exprs_stack
)
10647 : dom_walker (direction
, true),
10648 m_const_and_copies (const_and_copies
),
10649 m_avail_exprs_stack (avail_exprs_stack
),
10650 m_dummy_cond (NULL
) {}
10652 virtual edge
before_dom_children (basic_block
);
10653 virtual void after_dom_children (basic_block
);
10656 class const_and_copies
*m_const_and_copies
;
10657 class avail_exprs_stack
*m_avail_exprs_stack
;
10659 gcond
*m_dummy_cond
;
10662 /* Called before processing dominator children of BB. We want to look
10663 at ASSERT_EXPRs and record information from them in the appropriate
10666 We could look at other statements here. It's not seen as likely
10667 to significantly increase the jump threads we discover. */
10670 vrp_dom_walker::before_dom_children (basic_block bb
)
10672 gimple_stmt_iterator gsi
;
10674 m_avail_exprs_stack
->push_marker ();
10675 m_const_and_copies
->push_marker ();
10676 for (gsi
= gsi_start_nondebug_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
10678 gimple
*stmt
= gsi_stmt (gsi
);
10679 if (gimple_assign_single_p (stmt
)
10680 && TREE_CODE (gimple_assign_rhs1 (stmt
)) == ASSERT_EXPR
)
10682 tree rhs1
= gimple_assign_rhs1 (stmt
);
10683 tree cond
= TREE_OPERAND (rhs1
, 1);
10684 tree inverted
= invert_truthvalue (cond
);
10685 vec
<cond_equivalence
> p
;
10687 record_conditions (&p
, cond
, inverted
);
10688 for (unsigned int i
= 0; i
< p
.length (); i
++)
10689 m_avail_exprs_stack
->record_cond (&p
[i
]);
10691 tree lhs
= gimple_assign_lhs (stmt
);
10692 m_const_and_copies
->record_const_or_copy (lhs
,
10693 TREE_OPERAND (rhs1
, 0));
10702 /* Called after processing dominator children of BB. This is where we
10703 actually call into the threader. */
10705 vrp_dom_walker::after_dom_children (basic_block bb
)
10708 m_dummy_cond
= gimple_build_cond (NE_EXPR
,
10709 integer_zero_node
, integer_zero_node
,
10712 thread_outgoing_edges (bb
, m_dummy_cond
, m_const_and_copies
,
10713 m_avail_exprs_stack
,
10714 simplify_stmt_for_jump_threading
);
10716 m_avail_exprs_stack
->pop_to_marker ();
10717 m_const_and_copies
->pop_to_marker ();
10720 /* Blocks which have more than one predecessor and more than
10721 one successor present jump threading opportunities, i.e.,
10722 when the block is reached from a specific predecessor, we
10723 may be able to determine which of the outgoing edges will
10724 be traversed. When this optimization applies, we are able
10725 to avoid conditionals at runtime and we may expose secondary
10726 optimization opportunities.
10728 This routine is effectively a driver for the generic jump
10729 threading code. It basically just presents the generic code
10730 with edges that may be suitable for jump threading.
10732 Unlike DOM, we do not iterate VRP if jump threading was successful.
10733 While iterating may expose new opportunities for VRP, it is expected
10734 those opportunities would be very limited and the compile time cost
10735 to expose those opportunities would be significant.
10737 As jump threading opportunities are discovered, they are registered
10738 for later realization. */
10741 identify_jump_threads (void)
10746 /* Ugh. When substituting values earlier in this pass we can
10747 wipe the dominance information. So rebuild the dominator
10748 information as we need it within the jump threading code. */
10749 calculate_dominance_info (CDI_DOMINATORS
);
10751 /* We do not allow VRP information to be used for jump threading
10752 across a back edge in the CFG. Otherwise it becomes too
10753 difficult to avoid eliminating loop exit tests. Of course
10754 EDGE_DFS_BACK is not accurate at this time so we have to
10756 mark_dfs_back_edges ();
10758 /* Do not thread across edges we are about to remove. Just marking
10759 them as EDGE_IGNORE will do. */
10760 FOR_EACH_VEC_ELT (to_remove_edges
, i
, e
)
10761 e
->flags
|= EDGE_IGNORE
;
10763 /* Allocate our unwinder stack to unwind any temporary equivalences
10764 that might be recorded. */
10765 const_and_copies
*equiv_stack
= new const_and_copies ();
10767 hash_table
<expr_elt_hasher
> *avail_exprs
10768 = new hash_table
<expr_elt_hasher
> (1024);
10769 avail_exprs_stack
*avail_exprs_stack
10770 = new class avail_exprs_stack (avail_exprs
);
10772 vrp_dom_walker
walker (CDI_DOMINATORS
, equiv_stack
, avail_exprs_stack
);
10773 walker
.walk (cfun
->cfg
->x_entry_block_ptr
);
10775 /* Clear EDGE_IGNORE. */
10776 FOR_EACH_VEC_ELT (to_remove_edges
, i
, e
)
10777 e
->flags
&= ~EDGE_IGNORE
;
10779 /* We do not actually update the CFG or SSA graphs at this point as
10780 ASSERT_EXPRs are still in the IL and cfg cleanup code does not yet
10781 handle ASSERT_EXPRs gracefully. */
10782 delete equiv_stack
;
10783 delete avail_exprs
;
10784 delete avail_exprs_stack
;
10787 /* Free VRP lattice. */
10790 vrp_free_lattice ()
10792 /* Free allocated memory. */
10794 free (vr_phi_edge_counts
);
10795 bitmap_obstack_release (&vrp_equiv_obstack
);
10796 vrp_value_range_pool
.release ();
10798 /* So that we can distinguish between VRP data being available
10799 and not available. */
10801 vr_phi_edge_counts
= NULL
;
10804 /* Traverse all the blocks folding conditionals with known ranges. */
10807 vrp_finalize (bool warn_array_bounds_p
)
10811 values_propagated
= true;
10815 fprintf (dump_file
, "\nValue ranges after VRP:\n\n");
10816 dump_all_value_ranges (dump_file
);
10817 fprintf (dump_file
, "\n");
10820 /* Set value range to non pointer SSA_NAMEs. */
10821 for (i
= 0; i
< num_vr_values
; i
++)
10824 tree name
= ssa_name (i
);
10827 || (vr_value
[i
]->type
== VR_VARYING
)
10828 || (vr_value
[i
]->type
== VR_UNDEFINED
)
10829 || (TREE_CODE (vr_value
[i
]->min
) != INTEGER_CST
)
10830 || (TREE_CODE (vr_value
[i
]->max
) != INTEGER_CST
))
10833 if (POINTER_TYPE_P (TREE_TYPE (name
))
10834 && ((vr_value
[i
]->type
== VR_RANGE
10835 && range_includes_zero_p (vr_value
[i
]->min
,
10836 vr_value
[i
]->max
) == 0)
10837 || (vr_value
[i
]->type
== VR_ANTI_RANGE
10838 && range_includes_zero_p (vr_value
[i
]->min
,
10839 vr_value
[i
]->max
) == 1)))
10840 set_ptr_nonnull (name
);
10841 else if (!POINTER_TYPE_P (TREE_TYPE (name
)))
10842 set_range_info (name
, vr_value
[i
]->type
, vr_value
[i
]->min
,
10846 substitute_and_fold (op_with_constant_singleton_value_range
, vrp_fold_stmt
);
10848 if (warn_array_bounds
&& warn_array_bounds_p
)
10849 check_all_array_refs ();
10852 /* evrp_dom_walker visits the basic blocks in the dominance order and set
10853 the Value Ranges (VR) for SSA_NAMEs in the scope. Use this VR to
10854 discover more VRs. */
10856 class evrp_dom_walker
: public dom_walker
10860 : dom_walker (CDI_DOMINATORS
), stack (10)
10862 need_eh_cleanup
= BITMAP_ALLOC (NULL
);
10864 ~evrp_dom_walker ()
10866 BITMAP_FREE (need_eh_cleanup
);
10868 virtual edge
before_dom_children (basic_block
);
10869 virtual void after_dom_children (basic_block
);
10870 void push_value_range (tree var
, value_range
*vr
);
10871 value_range
*pop_value_range (tree var
);
10872 value_range
*try_find_new_range (tree
, tree op
, tree_code code
, tree limit
);
10874 /* Cond_stack holds the old VR. */
10875 auto_vec
<std::pair
<tree
, value_range
*> > stack
;
10876 bitmap need_eh_cleanup
;
10877 auto_vec
<gimple
*> stmts_to_fixup
;
10878 auto_vec
<gimple
*> stmts_to_remove
;
10881 /* Find new range for NAME such that (OP CODE LIMIT) is true. */
10884 evrp_dom_walker::try_find_new_range (tree name
,
10885 tree op
, tree_code code
, tree limit
)
10887 value_range vr
= VR_INITIALIZER
;
10888 value_range
*old_vr
= get_value_range (name
);
10890 /* Discover VR when condition is true. */
10891 extract_range_for_var_from_comparison_expr (name
, code
, op
,
10893 /* If we found any usable VR, set the VR to ssa_name and create a
10894 PUSH old value in the stack with the old VR. */
10895 if (vr
.type
== VR_RANGE
|| vr
.type
== VR_ANTI_RANGE
)
10897 if (old_vr
->type
== vr
.type
10898 && vrp_operand_equal_p (old_vr
->min
, vr
.min
)
10899 && vrp_operand_equal_p (old_vr
->max
, vr
.max
))
10901 value_range
*new_vr
= vrp_value_range_pool
.allocate ();
10908 /* See if there is any new scope is entered with new VR and set that VR to
10909 ssa_name before visiting the statements in the scope. */
10912 evrp_dom_walker::before_dom_children (basic_block bb
)
10914 tree op0
= NULL_TREE
;
10918 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
10919 fprintf (dump_file
, "Visiting BB%d\n", bb
->index
);
10921 stack
.safe_push (std::make_pair (NULL_TREE
, (value_range
*)NULL
));
10923 edge pred_e
= NULL
;
10924 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
10926 /* Ignore simple backedges from this to allow recording conditions
10927 in loop headers. */
10928 if (dominated_by_p (CDI_DOMINATORS
, e
->src
, e
->dest
))
10940 gimple
*stmt
= last_stmt (pred_e
->src
);
10942 && gimple_code (stmt
) == GIMPLE_COND
10943 && (op0
= gimple_cond_lhs (stmt
))
10944 && TREE_CODE (op0
) == SSA_NAME
10945 && (INTEGRAL_TYPE_P (TREE_TYPE (gimple_cond_lhs (stmt
)))
10946 || POINTER_TYPE_P (TREE_TYPE (gimple_cond_lhs (stmt
)))))
10948 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
10950 fprintf (dump_file
, "Visiting controlling predicate ");
10951 print_gimple_stmt (dump_file
, stmt
, 0);
10953 /* Entering a new scope. Try to see if we can find a VR
10955 tree op1
= gimple_cond_rhs (stmt
);
10956 if (TREE_OVERFLOW_P (op1
))
10957 op1
= drop_tree_overflow (op1
);
10958 tree_code code
= gimple_cond_code (stmt
);
10960 auto_vec
<assert_info
, 8> asserts
;
10961 register_edge_assert_for (op0
, pred_e
, code
, op0
, op1
, asserts
);
10962 if (TREE_CODE (op1
) == SSA_NAME
)
10963 register_edge_assert_for (op1
, pred_e
, code
, op0
, op1
, asserts
);
10965 auto_vec
<std::pair
<tree
, value_range
*>, 8> vrs
;
10966 for (unsigned i
= 0; i
< asserts
.length (); ++i
)
10968 value_range
*vr
= try_find_new_range (asserts
[i
].name
,
10970 asserts
[i
].comp_code
,
10973 vrs
.safe_push (std::make_pair (asserts
[i
].name
, vr
));
10975 /* Push updated ranges only after finding all of them to avoid
10976 ordering issues that can lead to worse ranges. */
10977 for (unsigned i
= 0; i
< vrs
.length (); ++i
)
10978 push_value_range (vrs
[i
].first
, vrs
[i
].second
);
10982 /* Visit PHI stmts and discover any new VRs possible. */
10983 bool has_unvisited_preds
= false;
10984 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
10985 if (e
->flags
& EDGE_EXECUTABLE
10986 && !(e
->src
->flags
& BB_VISITED
))
10988 has_unvisited_preds
= true;
10992 for (gphi_iterator gpi
= gsi_start_phis (bb
);
10993 !gsi_end_p (gpi
); gsi_next (&gpi
))
10995 gphi
*phi
= gpi
.phi ();
10996 tree lhs
= PHI_RESULT (phi
);
10997 if (virtual_operand_p (lhs
))
10999 value_range vr_result
= VR_INITIALIZER
;
11000 bool interesting
= stmt_interesting_for_vrp (phi
);
11001 if (interesting
&& dump_file
&& (dump_flags
& TDF_DETAILS
))
11003 fprintf (dump_file
, "Visiting PHI node ");
11004 print_gimple_stmt (dump_file
, phi
, 0);
11006 if (!has_unvisited_preds
11008 extract_range_from_phi_node (phi
, &vr_result
);
11011 set_value_range_to_varying (&vr_result
);
11012 /* When we have an unvisited executable predecessor we can't
11013 use PHI arg ranges which may be still UNDEFINED but have
11014 to use VARYING for them. But we can still resort to
11015 SCEV for loop header PHIs. */
11018 && (l
= loop_containing_stmt (phi
))
11019 && l
->header
== gimple_bb (phi
))
11020 adjust_range_with_scev (&vr_result
, l
, phi
, lhs
);
11022 update_value_range (lhs
, &vr_result
);
11024 /* Mark PHIs whose lhs we fully propagate for removal. */
11025 tree val
= op_with_constant_singleton_value_range (lhs
);
11026 if (val
&& may_propagate_copy (lhs
, val
))
11028 stmts_to_remove
.safe_push (phi
);
11032 /* Set the SSA with the value range. */
11033 if (INTEGRAL_TYPE_P (TREE_TYPE (lhs
)))
11035 if ((vr_result
.type
== VR_RANGE
11036 || vr_result
.type
== VR_ANTI_RANGE
)
11037 && (TREE_CODE (vr_result
.min
) == INTEGER_CST
)
11038 && (TREE_CODE (vr_result
.max
) == INTEGER_CST
))
11039 set_range_info (lhs
,
11040 vr_result
.type
, vr_result
.min
, vr_result
.max
);
11042 else if (POINTER_TYPE_P (TREE_TYPE (lhs
))
11043 && ((vr_result
.type
== VR_RANGE
11044 && range_includes_zero_p (vr_result
.min
,
11045 vr_result
.max
) == 0)
11046 || (vr_result
.type
== VR_ANTI_RANGE
11047 && range_includes_zero_p (vr_result
.min
,
11048 vr_result
.max
) == 1)))
11049 set_ptr_nonnull (lhs
);
11052 edge taken_edge
= NULL
;
11054 /* Visit all other stmts and discover any new VRs possible. */
11055 for (gimple_stmt_iterator gsi
= gsi_start_bb (bb
);
11056 !gsi_end_p (gsi
); gsi_next (&gsi
))
11058 gimple
*stmt
= gsi_stmt (gsi
);
11059 tree output
= NULL_TREE
;
11060 gimple
*old_stmt
= stmt
;
11061 bool was_noreturn
= (is_gimple_call (stmt
)
11062 && gimple_call_noreturn_p (stmt
));
11064 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
11066 fprintf (dump_file
, "Visiting stmt ");
11067 print_gimple_stmt (dump_file
, stmt
, 0);
11070 if (gcond
*cond
= dyn_cast
<gcond
*> (stmt
))
11072 vrp_visit_cond_stmt (cond
, &taken_edge
);
11075 if (taken_edge
->flags
& EDGE_TRUE_VALUE
)
11076 gimple_cond_make_true (cond
);
11077 else if (taken_edge
->flags
& EDGE_FALSE_VALUE
)
11078 gimple_cond_make_false (cond
);
11080 gcc_unreachable ();
11081 update_stmt (stmt
);
11084 else if (stmt_interesting_for_vrp (stmt
))
11087 value_range vr
= VR_INITIALIZER
;
11088 extract_range_from_stmt (stmt
, &taken_edge
, &output
, &vr
);
11090 && (vr
.type
== VR_RANGE
|| vr
.type
== VR_ANTI_RANGE
))
11092 update_value_range (output
, &vr
);
11093 vr
= *get_value_range (output
);
11095 /* Mark stmts whose output we fully propagate for removal. */
11097 if ((val
= op_with_constant_singleton_value_range (output
))
11098 && may_propagate_copy (output
, val
)
11099 && !stmt_could_throw_p (stmt
)
11100 && !gimple_has_side_effects (stmt
))
11102 stmts_to_remove
.safe_push (stmt
);
11106 /* Set the SSA with the value range. */
11107 if (INTEGRAL_TYPE_P (TREE_TYPE (output
)))
11109 if ((vr
.type
== VR_RANGE
11110 || vr
.type
== VR_ANTI_RANGE
)
11111 && (TREE_CODE (vr
.min
) == INTEGER_CST
)
11112 && (TREE_CODE (vr
.max
) == INTEGER_CST
))
11113 set_range_info (output
, vr
.type
, vr
.min
, vr
.max
);
11115 else if (POINTER_TYPE_P (TREE_TYPE (output
))
11116 && ((vr
.type
== VR_RANGE
11117 && range_includes_zero_p (vr
.min
,
11119 || (vr
.type
== VR_ANTI_RANGE
11120 && range_includes_zero_p (vr
.min
,
11122 set_ptr_nonnull (output
);
11125 set_defs_to_varying (stmt
);
11128 set_defs_to_varying (stmt
);
11130 /* See if we can derive a range for any of STMT's operands. */
11133 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, i
, SSA_OP_USE
)
11136 enum tree_code comp_code
;
11138 /* If OP is used in such a way that we can infer a value
11139 range for it, and we don't find a previous assertion for
11140 it, create a new assertion location node for OP. */
11141 if (infer_value_range (stmt
, op
, &comp_code
, &value
))
11143 /* If we are able to infer a nonzero value range for OP,
11144 then walk backwards through the use-def chain to see if OP
11145 was set via a typecast.
11146 If so, then we can also infer a nonzero value range
11147 for the operand of the NOP_EXPR. */
11148 if (comp_code
== NE_EXPR
&& integer_zerop (value
))
11151 gimple
*def_stmt
= SSA_NAME_DEF_STMT (t
);
11152 while (is_gimple_assign (def_stmt
)
11153 && CONVERT_EXPR_CODE_P
11154 (gimple_assign_rhs_code (def_stmt
))
11156 (gimple_assign_rhs1 (def_stmt
)) == SSA_NAME
11158 (TREE_TYPE (gimple_assign_rhs1 (def_stmt
))))
11160 t
= gimple_assign_rhs1 (def_stmt
);
11161 def_stmt
= SSA_NAME_DEF_STMT (t
);
11163 /* Add VR when (T COMP_CODE value) condition is
11165 value_range
*op_range
11166 = try_find_new_range (t
, t
, comp_code
, value
);
11168 push_value_range (t
, op_range
);
11171 /* Add VR when (OP COMP_CODE value) condition is true. */
11172 value_range
*op_range
= try_find_new_range (op
, op
,
11175 push_value_range (op
, op_range
);
11179 /* Try folding stmts with the VR discovered. */
11181 = replace_uses_in (stmt
, op_with_constant_singleton_value_range
);
11182 if (fold_stmt (&gsi
, follow_single_use_edges
)
11185 stmt
= gsi_stmt (gsi
);
11186 update_stmt (stmt
);
11187 did_replace
= true;
11192 /* If we cleaned up EH information from the statement,
11193 remove EH edges. */
11194 if (maybe_clean_or_replace_eh_stmt (old_stmt
, stmt
))
11195 bitmap_set_bit (need_eh_cleanup
, bb
->index
);
11197 /* If we turned a not noreturn call into a noreturn one
11198 schedule it for fixup. */
11200 && is_gimple_call (stmt
)
11201 && gimple_call_noreturn_p (stmt
))
11202 stmts_to_fixup
.safe_push (stmt
);
11204 if (gimple_assign_single_p (stmt
))
11206 tree rhs
= gimple_assign_rhs1 (stmt
);
11207 if (TREE_CODE (rhs
) == ADDR_EXPR
)
11208 recompute_tree_invariant_for_addr_expr (rhs
);
11213 /* Visit BB successor PHI nodes and replace PHI args. */
11214 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
11216 for (gphi_iterator gpi
= gsi_start_phis (e
->dest
);
11217 !gsi_end_p (gpi
); gsi_next (&gpi
))
11219 gphi
*phi
= gpi
.phi ();
11220 use_operand_p use_p
= PHI_ARG_DEF_PTR_FROM_EDGE (phi
, e
);
11221 tree arg
= USE_FROM_PTR (use_p
);
11222 if (TREE_CODE (arg
) != SSA_NAME
11223 || virtual_operand_p (arg
))
11225 tree val
= op_with_constant_singleton_value_range (arg
);
11226 if (val
&& may_propagate_copy (arg
, val
))
11227 propagate_value (use_p
, val
);
11231 bb
->flags
|= BB_VISITED
;
11236 /* Restore/pop VRs valid only for BB when we leave BB. */
11239 evrp_dom_walker::after_dom_children (basic_block bb ATTRIBUTE_UNUSED
)
11241 gcc_checking_assert (!stack
.is_empty ());
11242 while (stack
.last ().first
!= NULL_TREE
)
11243 pop_value_range (stack
.last ().first
);
11247 /* Push the Value Range of VAR to the stack and update it with new VR. */
11250 evrp_dom_walker::push_value_range (tree var
, value_range
*vr
)
11252 if (SSA_NAME_VERSION (var
) >= num_vr_values
)
11254 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
11256 fprintf (dump_file
, "pushing new range for ");
11257 print_generic_expr (dump_file
, var
);
11258 fprintf (dump_file
, ": ");
11259 dump_value_range (dump_file
, vr
);
11260 fprintf (dump_file
, "\n");
11262 stack
.safe_push (std::make_pair (var
, get_value_range (var
)));
11263 vr_value
[SSA_NAME_VERSION (var
)] = vr
;
11266 /* Pop the Value Range from the vrp_stack and update VAR with it. */
11269 evrp_dom_walker::pop_value_range (tree var
)
11271 value_range
*vr
= stack
.last ().second
;
11272 gcc_checking_assert (var
== stack
.last ().first
);
11273 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
11275 fprintf (dump_file
, "popping range for ");
11276 print_generic_expr (dump_file
, var
);
11277 fprintf (dump_file
, ", restoring ");
11278 dump_value_range (dump_file
, vr
);
11279 fprintf (dump_file
, "\n");
11281 vr_value
[SSA_NAME_VERSION (var
)] = vr
;
11287 /* Main entry point for the early vrp pass which is a simplified non-iterative
11288 version of vrp where basic blocks are visited in dominance order. Value
11289 ranges discovered in early vrp will also be used by ipa-vrp. */
11291 static unsigned int
11292 execute_early_vrp ()
11298 loop_optimizer_init (LOOPS_NORMAL
| LOOPS_HAVE_RECORDED_EXITS
);
11299 rewrite_into_loop_closed_ssa (NULL
, TODO_update_ssa
);
11300 scev_initialize ();
11301 calculate_dominance_info (CDI_DOMINATORS
);
11302 FOR_EACH_BB_FN (bb
, cfun
)
11304 bb
->flags
&= ~BB_VISITED
;
11305 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
11306 e
->flags
|= EDGE_EXECUTABLE
;
11308 vrp_initialize_lattice ();
11310 /* Walk stmts in dominance order and propagate VRP. */
11311 evrp_dom_walker walker
;
11312 walker
.walk (ENTRY_BLOCK_PTR_FOR_FN (cfun
));
11316 fprintf (dump_file
, "\nValue ranges after Early VRP:\n\n");
11317 dump_all_value_ranges (dump_file
);
11318 fprintf (dump_file
, "\n");
11321 /* Remove stmts in reverse order to make debug stmt creation possible. */
11322 while (! walker
.stmts_to_remove
.is_empty ())
11324 gimple
*stmt
= walker
.stmts_to_remove
.pop ();
11325 if (dump_file
&& dump_flags
& TDF_DETAILS
)
11327 fprintf (dump_file
, "Removing dead stmt ");
11328 print_gimple_stmt (dump_file
, stmt
, 0);
11329 fprintf (dump_file
, "\n");
11331 gimple_stmt_iterator gsi
= gsi_for_stmt (stmt
);
11332 if (gimple_code (stmt
) == GIMPLE_PHI
)
11333 remove_phi_node (&gsi
, true);
11336 unlink_stmt_vdef (stmt
);
11337 gsi_remove (&gsi
, true);
11338 release_defs (stmt
);
11342 if (!bitmap_empty_p (walker
.need_eh_cleanup
))
11343 gimple_purge_all_dead_eh_edges (walker
.need_eh_cleanup
);
11345 /* Fixup stmts that became noreturn calls. This may require splitting
11346 blocks and thus isn't possible during the dominator walk. Do this
11347 in reverse order so we don't inadvertedly remove a stmt we want to
11348 fixup by visiting a dominating now noreturn call first. */
11349 while (!walker
.stmts_to_fixup
.is_empty ())
11351 gimple
*stmt
= walker
.stmts_to_fixup
.pop ();
11352 fixup_noreturn_call (stmt
);
11355 vrp_free_lattice ();
11357 loop_optimizer_finalize ();
11362 /* Main entry point to VRP (Value Range Propagation). This pass is
11363 loosely based on J. R. C. Patterson, ``Accurate Static Branch
11364 Prediction by Value Range Propagation,'' in SIGPLAN Conference on
11365 Programming Language Design and Implementation, pp. 67-78, 1995.
11366 Also available at http://citeseer.ist.psu.edu/patterson95accurate.html
11368 This is essentially an SSA-CCP pass modified to deal with ranges
11369 instead of constants.
11371 While propagating ranges, we may find that two or more SSA name
11372 have equivalent, though distinct ranges. For instance,
11375 2 p_4 = ASSERT_EXPR <p_3, p_3 != 0>
11377 4 p_5 = ASSERT_EXPR <p_4, p_4 == q_2>;
11381 In the code above, pointer p_5 has range [q_2, q_2], but from the
11382 code we can also determine that p_5 cannot be NULL and, if q_2 had
11383 a non-varying range, p_5's range should also be compatible with it.
11385 These equivalences are created by two expressions: ASSERT_EXPR and
11386 copy operations. Since p_5 is an assertion on p_4, and p_4 was the
11387 result of another assertion, then we can use the fact that p_5 and
11388 p_4 are equivalent when evaluating p_5's range.
11390 Together with value ranges, we also propagate these equivalences
11391 between names so that we can take advantage of information from
11392 multiple ranges when doing final replacement. Note that this
11393 equivalency relation is transitive but not symmetric.
11395 In the example above, p_5 is equivalent to p_4, q_2 and p_3, but we
11396 cannot assert that q_2 is equivalent to p_5 because q_2 may be used
11397 in contexts where that assertion does not hold (e.g., in line 6).
11399 TODO, the main difference between this pass and Patterson's is that
11400 we do not propagate edge probabilities. We only compute whether
11401 edges can be taken or not. That is, instead of having a spectrum
11402 of jump probabilities between 0 and 1, we only deal with 0, 1 and
11403 DON'T KNOW. In the future, it may be worthwhile to propagate
11404 probabilities to aid branch prediction. */
11406 static unsigned int
11407 execute_vrp (bool warn_array_bounds_p
)
11413 loop_optimizer_init (LOOPS_NORMAL
| LOOPS_HAVE_RECORDED_EXITS
);
11414 rewrite_into_loop_closed_ssa (NULL
, TODO_update_ssa
);
11415 scev_initialize ();
11417 /* ??? This ends up using stale EDGE_DFS_BACK for liveness computation.
11418 Inserting assertions may split edges which will invalidate
11420 insert_range_assertions ();
11422 to_remove_edges
.create (10);
11423 to_update_switch_stmts
.create (5);
11424 threadedge_initialize_values ();
11426 /* For visiting PHI nodes we need EDGE_DFS_BACK computed. */
11427 mark_dfs_back_edges ();
11429 vrp_initialize_lattice ();
11431 ssa_propagate (vrp_visit_stmt
, vrp_visit_phi_node
);
11432 vrp_finalize (warn_array_bounds_p
);
11434 /* We must identify jump threading opportunities before we release
11435 the datastructures built by VRP. */
11436 identify_jump_threads ();
11438 /* A comparison of an SSA_NAME against a constant where the SSA_NAME
11439 was set by a type conversion can often be rewritten to use the
11440 RHS of the type conversion.
11442 However, doing so inhibits jump threading through the comparison.
11443 So that transformation is not performed until after jump threading
11446 FOR_EACH_BB_FN (bb
, cfun
)
11448 gimple
*last
= last_stmt (bb
);
11449 if (last
&& gimple_code (last
) == GIMPLE_COND
)
11450 simplify_cond_using_ranges_2 (as_a
<gcond
*> (last
));
11453 vrp_free_lattice ();
11455 free_numbers_of_iterations_estimates (cfun
);
11457 /* ASSERT_EXPRs must be removed before finalizing jump threads
11458 as finalizing jump threads calls the CFG cleanup code which
11459 does not properly handle ASSERT_EXPRs. */
11460 remove_range_assertions ();
11462 /* If we exposed any new variables, go ahead and put them into
11463 SSA form now, before we handle jump threading. This simplifies
11464 interactions between rewriting of _DECL nodes into SSA form
11465 and rewriting SSA_NAME nodes into SSA form after block
11466 duplication and CFG manipulation. */
11467 update_ssa (TODO_update_ssa
);
11469 /* We identified all the jump threading opportunities earlier, but could
11470 not transform the CFG at that time. This routine transforms the
11471 CFG and arranges for the dominator tree to be rebuilt if necessary.
11473 Note the SSA graph update will occur during the normal TODO
11474 processing by the pass manager. */
11475 thread_through_all_blocks (false);
11477 /* Remove dead edges from SWITCH_EXPR optimization. This leaves the
11478 CFG in a broken state and requires a cfg_cleanup run. */
11479 FOR_EACH_VEC_ELT (to_remove_edges
, i
, e
)
11481 /* Update SWITCH_EXPR case label vector. */
11482 FOR_EACH_VEC_ELT (to_update_switch_stmts
, i
, su
)
11485 size_t n
= TREE_VEC_LENGTH (su
->vec
);
11487 gimple_switch_set_num_labels (su
->stmt
, n
);
11488 for (j
= 0; j
< n
; j
++)
11489 gimple_switch_set_label (su
->stmt
, j
, TREE_VEC_ELT (su
->vec
, j
));
11490 /* As we may have replaced the default label with a regular one
11491 make sure to make it a real default label again. This ensures
11492 optimal expansion. */
11493 label
= gimple_switch_label (su
->stmt
, 0);
11494 CASE_LOW (label
) = NULL_TREE
;
11495 CASE_HIGH (label
) = NULL_TREE
;
11498 if (to_remove_edges
.length () > 0)
11500 free_dominance_info (CDI_DOMINATORS
);
11501 loops_state_set (LOOPS_NEED_FIXUP
);
11504 to_remove_edges
.release ();
11505 to_update_switch_stmts
.release ();
11506 threadedge_finalize_values ();
11509 loop_optimizer_finalize ();
11515 const pass_data pass_data_vrp
=
11517 GIMPLE_PASS
, /* type */
11519 OPTGROUP_NONE
, /* optinfo_flags */
11520 TV_TREE_VRP
, /* tv_id */
11521 PROP_ssa
, /* properties_required */
11522 0, /* properties_provided */
11523 0, /* properties_destroyed */
11524 0, /* todo_flags_start */
11525 ( TODO_cleanup_cfg
| TODO_update_ssa
), /* todo_flags_finish */
11528 class pass_vrp
: public gimple_opt_pass
11531 pass_vrp (gcc::context
*ctxt
)
11532 : gimple_opt_pass (pass_data_vrp
, ctxt
), warn_array_bounds_p (false)
11535 /* opt_pass methods: */
11536 opt_pass
* clone () { return new pass_vrp (m_ctxt
); }
11537 void set_pass_param (unsigned int n
, bool param
)
11539 gcc_assert (n
== 0);
11540 warn_array_bounds_p
= param
;
11542 virtual bool gate (function
*) { return flag_tree_vrp
!= 0; }
11543 virtual unsigned int execute (function
*)
11544 { return execute_vrp (warn_array_bounds_p
); }
11547 bool warn_array_bounds_p
;
11548 }; // class pass_vrp
11550 } // anon namespace
11553 make_pass_vrp (gcc::context
*ctxt
)
11555 return new pass_vrp (ctxt
);
11560 const pass_data pass_data_early_vrp
=
11562 GIMPLE_PASS
, /* type */
11564 OPTGROUP_NONE
, /* optinfo_flags */
11565 TV_TREE_EARLY_VRP
, /* tv_id */
11566 PROP_ssa
, /* properties_required */
11567 0, /* properties_provided */
11568 0, /* properties_destroyed */
11569 0, /* todo_flags_start */
11570 ( TODO_cleanup_cfg
| TODO_update_ssa
| TODO_verify_all
),
11573 class pass_early_vrp
: public gimple_opt_pass
11576 pass_early_vrp (gcc::context
*ctxt
)
11577 : gimple_opt_pass (pass_data_early_vrp
, ctxt
)
11580 /* opt_pass methods: */
11581 opt_pass
* clone () { return new pass_early_vrp (m_ctxt
); }
11582 virtual bool gate (function
*)
11584 return flag_tree_vrp
!= 0;
11586 virtual unsigned int execute (function
*)
11587 { return execute_early_vrp (); }
11589 }; // class pass_vrp
11590 } // anon namespace
11593 make_pass_early_vrp (gcc::context
*ctxt
)
11595 return new pass_early_vrp (ctxt
);