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.
277 All equivalence bitmaps are allocated from the same obstack. So
278 we can use the obstack associated with EQUIV to allocate vr->equiv. */
279 if (vr
->equiv
== NULL
281 vr
->equiv
= BITMAP_ALLOC (equiv
->obstack
);
283 if (equiv
!= vr
->equiv
)
285 if (equiv
&& !bitmap_empty_p (equiv
))
286 bitmap_copy (vr
->equiv
, equiv
);
288 bitmap_clear (vr
->equiv
);
293 /* Set value range VR to the canonical form of {T, MIN, MAX, EQUIV}.
294 This means adjusting T, MIN and MAX representing the case of a
295 wrapping range with MAX < MIN covering [MIN, type_max] U [type_min, MAX]
296 as anti-rage ~[MAX+1, MIN-1]. Likewise for wrapping anti-ranges.
297 In corner cases where MAX+1 or MIN-1 wraps this will fall back
299 This routine exists to ease canonicalization in the case where we
300 extract ranges from var + CST op limit. */
303 set_and_canonicalize_value_range (value_range
*vr
, enum value_range_type t
,
304 tree min
, tree max
, bitmap equiv
)
306 /* Use the canonical setters for VR_UNDEFINED and VR_VARYING. */
307 if (t
== VR_UNDEFINED
)
309 set_value_range_to_undefined (vr
);
312 else if (t
== VR_VARYING
)
314 set_value_range_to_varying (vr
);
318 /* Nothing to canonicalize for symbolic ranges. */
319 if (TREE_CODE (min
) != INTEGER_CST
320 || TREE_CODE (max
) != INTEGER_CST
)
322 set_value_range (vr
, t
, min
, max
, equiv
);
326 /* Wrong order for min and max, to swap them and the VR type we need
328 if (tree_int_cst_lt (max
, min
))
332 /* For one bit precision if max < min, then the swapped
333 range covers all values, so for VR_RANGE it is varying and
334 for VR_ANTI_RANGE empty range, so drop to varying as well. */
335 if (TYPE_PRECISION (TREE_TYPE (min
)) == 1)
337 set_value_range_to_varying (vr
);
341 one
= build_int_cst (TREE_TYPE (min
), 1);
342 tmp
= int_const_binop (PLUS_EXPR
, max
, one
);
343 max
= int_const_binop (MINUS_EXPR
, min
, one
);
346 /* There's one corner case, if we had [C+1, C] before we now have
347 that again. But this represents an empty value range, so drop
348 to varying in this case. */
349 if (tree_int_cst_lt (max
, min
))
351 set_value_range_to_varying (vr
);
355 t
= t
== VR_RANGE
? VR_ANTI_RANGE
: VR_RANGE
;
358 /* Anti-ranges that can be represented as ranges should be so. */
359 if (t
== VR_ANTI_RANGE
)
361 bool is_min
= vrp_val_is_min (min
);
362 bool is_max
= vrp_val_is_max (max
);
364 if (is_min
&& is_max
)
366 /* We cannot deal with empty ranges, drop to varying.
367 ??? This could be VR_UNDEFINED instead. */
368 set_value_range_to_varying (vr
);
371 else if (TYPE_PRECISION (TREE_TYPE (min
)) == 1
372 && (is_min
|| is_max
))
374 /* Non-empty boolean ranges can always be represented
375 as a singleton range. */
377 min
= max
= vrp_val_max (TREE_TYPE (min
));
379 min
= max
= vrp_val_min (TREE_TYPE (min
));
383 /* As a special exception preserve non-null ranges. */
384 && !(TYPE_UNSIGNED (TREE_TYPE (min
))
385 && integer_zerop (max
)))
387 tree one
= build_int_cst (TREE_TYPE (max
), 1);
388 min
= int_const_binop (PLUS_EXPR
, max
, one
);
389 max
= vrp_val_max (TREE_TYPE (max
));
394 tree one
= build_int_cst (TREE_TYPE (min
), 1);
395 max
= int_const_binop (MINUS_EXPR
, min
, one
);
396 min
= vrp_val_min (TREE_TYPE (min
));
401 /* Do not drop [-INF(OVF), +INF(OVF)] to varying. (OVF) has to be sticky
402 to make sure VRP iteration terminates, otherwise we can get into
405 set_value_range (vr
, t
, min
, max
, equiv
);
408 /* Copy value range FROM into value range TO. */
411 copy_value_range (value_range
*to
, value_range
*from
)
413 set_value_range (to
, from
->type
, from
->min
, from
->max
, from
->equiv
);
416 /* Set value range VR to a single value. This function is only called
417 with values we get from statements, and exists to clear the
418 TREE_OVERFLOW flag. */
421 set_value_range_to_value (value_range
*vr
, tree val
, bitmap equiv
)
423 gcc_assert (is_gimple_min_invariant (val
));
424 if (TREE_OVERFLOW_P (val
))
425 val
= drop_tree_overflow (val
);
426 set_value_range (vr
, VR_RANGE
, val
, val
, equiv
);
429 /* Set value range VR to a non-negative range of type TYPE. */
432 set_value_range_to_nonnegative (value_range
*vr
, tree type
)
434 tree zero
= build_int_cst (type
, 0);
435 set_value_range (vr
, VR_RANGE
, zero
, vrp_val_max (type
), vr
->equiv
);
438 /* Set value range VR to a non-NULL range of type TYPE. */
441 set_value_range_to_nonnull (value_range
*vr
, tree type
)
443 tree zero
= build_int_cst (type
, 0);
444 set_value_range (vr
, VR_ANTI_RANGE
, zero
, zero
, vr
->equiv
);
448 /* Set value range VR to a NULL range of type TYPE. */
451 set_value_range_to_null (value_range
*vr
, tree type
)
453 set_value_range_to_value (vr
, build_int_cst (type
, 0), vr
->equiv
);
457 /* Set value range VR to a range of a truthvalue of type TYPE. */
460 set_value_range_to_truthvalue (value_range
*vr
, tree type
)
462 if (TYPE_PRECISION (type
) == 1)
463 set_value_range_to_varying (vr
);
465 set_value_range (vr
, VR_RANGE
,
466 build_int_cst (type
, 0), build_int_cst (type
, 1),
471 /* If abs (min) < abs (max), set VR to [-max, max], if
472 abs (min) >= abs (max), set VR to [-min, min]. */
475 abs_extent_range (value_range
*vr
, tree min
, tree max
)
479 gcc_assert (TREE_CODE (min
) == INTEGER_CST
);
480 gcc_assert (TREE_CODE (max
) == INTEGER_CST
);
481 gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (min
)));
482 gcc_assert (!TYPE_UNSIGNED (TREE_TYPE (min
)));
483 min
= fold_unary (ABS_EXPR
, TREE_TYPE (min
), min
);
484 max
= fold_unary (ABS_EXPR
, TREE_TYPE (max
), max
);
485 if (TREE_OVERFLOW (min
) || TREE_OVERFLOW (max
))
487 set_value_range_to_varying (vr
);
490 cmp
= compare_values (min
, max
);
492 min
= fold_unary (NEGATE_EXPR
, TREE_TYPE (min
), max
);
493 else if (cmp
== 0 || cmp
== 1)
496 min
= fold_unary (NEGATE_EXPR
, TREE_TYPE (min
), min
);
500 set_value_range_to_varying (vr
);
503 set_and_canonicalize_value_range (vr
, VR_RANGE
, min
, max
, NULL
);
507 /* Return value range information for VAR.
509 If we have no values ranges recorded (ie, VRP is not running), then
510 return NULL. Otherwise create an empty range if none existed for VAR. */
513 get_value_range (const_tree var
)
515 static const value_range vr_const_varying
516 = { VR_VARYING
, NULL_TREE
, NULL_TREE
, NULL
};
519 unsigned ver
= SSA_NAME_VERSION (var
);
521 /* If we have no recorded ranges, then return NULL. */
525 /* If we query the range for a new SSA name return an unmodifiable VARYING.
526 We should get here at most from the substitute-and-fold stage which
527 will never try to change values. */
528 if (ver
>= num_vr_values
)
529 return CONST_CAST (value_range
*, &vr_const_varying
);
535 /* After propagation finished do not allocate new value-ranges. */
536 if (values_propagated
)
537 return CONST_CAST (value_range
*, &vr_const_varying
);
539 /* Create a default value range. */
540 vr_value
[ver
] = vr
= vrp_value_range_pool
.allocate ();
541 memset (vr
, 0, sizeof (*vr
));
543 /* Defer allocating the equivalence set. */
546 /* If VAR is a default definition of a parameter, the variable can
547 take any value in VAR's type. */
548 if (SSA_NAME_IS_DEFAULT_DEF (var
))
550 sym
= SSA_NAME_VAR (var
);
551 if (TREE_CODE (sym
) == PARM_DECL
)
553 /* Try to use the "nonnull" attribute to create ~[0, 0]
554 anti-ranges for pointers. Note that this is only valid with
555 default definitions of PARM_DECLs. */
556 if (POINTER_TYPE_P (TREE_TYPE (sym
))
557 && (nonnull_arg_p (sym
)
558 || get_ptr_nonnull (var
)))
559 set_value_range_to_nonnull (vr
, TREE_TYPE (sym
));
560 else if (INTEGRAL_TYPE_P (TREE_TYPE (sym
)))
563 value_range_type rtype
= get_range_info (var
, &min
, &max
);
564 if (rtype
== VR_RANGE
|| rtype
== VR_ANTI_RANGE
)
565 set_value_range (vr
, rtype
,
566 wide_int_to_tree (TREE_TYPE (var
), min
),
567 wide_int_to_tree (TREE_TYPE (var
), max
),
570 set_value_range_to_varying (vr
);
573 set_value_range_to_varying (vr
);
575 else if (TREE_CODE (sym
) == RESULT_DECL
576 && DECL_BY_REFERENCE (sym
))
577 set_value_range_to_nonnull (vr
, TREE_TYPE (sym
));
583 /* Set value-ranges of all SSA names defined by STMT to varying. */
586 set_defs_to_varying (gimple
*stmt
)
590 FOR_EACH_SSA_TREE_OPERAND (def
, stmt
, i
, SSA_OP_DEF
)
592 value_range
*vr
= get_value_range (def
);
593 /* Avoid writing to vr_const_varying get_value_range may return. */
594 if (vr
->type
!= VR_VARYING
)
595 set_value_range_to_varying (vr
);
600 /* Return true, if VAL1 and VAL2 are equal values for VRP purposes. */
603 vrp_operand_equal_p (const_tree val1
, const_tree val2
)
607 if (!val1
|| !val2
|| !operand_equal_p (val1
, val2
, 0))
612 /* Return true, if the bitmaps B1 and B2 are equal. */
615 vrp_bitmap_equal_p (const_bitmap b1
, const_bitmap b2
)
618 || ((!b1
|| bitmap_empty_p (b1
))
619 && (!b2
|| bitmap_empty_p (b2
)))
621 && bitmap_equal_p (b1
, b2
)));
624 /* Update the value range and equivalence set for variable VAR to
625 NEW_VR. Return true if NEW_VR is different from VAR's previous
628 NOTE: This function assumes that NEW_VR is a temporary value range
629 object created for the sole purpose of updating VAR's range. The
630 storage used by the equivalence set from NEW_VR will be freed by
631 this function. Do not call update_value_range when NEW_VR
632 is the range object associated with another SSA name. */
635 update_value_range (const_tree var
, value_range
*new_vr
)
640 /* If there is a value-range on the SSA name from earlier analysis
642 if (INTEGRAL_TYPE_P (TREE_TYPE (var
)))
645 value_range_type rtype
= get_range_info (var
, &min
, &max
);
646 if (rtype
== VR_RANGE
|| rtype
== VR_ANTI_RANGE
)
649 nr_min
= wide_int_to_tree (TREE_TYPE (var
), min
);
650 nr_max
= wide_int_to_tree (TREE_TYPE (var
), max
);
651 value_range nr
= VR_INITIALIZER
;
652 set_and_canonicalize_value_range (&nr
, rtype
, nr_min
, nr_max
, NULL
);
653 vrp_intersect_ranges (new_vr
, &nr
);
657 /* Update the value range, if necessary. */
658 old_vr
= get_value_range (var
);
659 is_new
= old_vr
->type
!= new_vr
->type
660 || !vrp_operand_equal_p (old_vr
->min
, new_vr
->min
)
661 || !vrp_operand_equal_p (old_vr
->max
, new_vr
->max
)
662 || !vrp_bitmap_equal_p (old_vr
->equiv
, new_vr
->equiv
);
666 /* Do not allow transitions up the lattice. The following
667 is slightly more awkward than just new_vr->type < old_vr->type
668 because VR_RANGE and VR_ANTI_RANGE need to be considered
669 the same. We may not have is_new when transitioning to
670 UNDEFINED. If old_vr->type is VARYING, we shouldn't be
672 if (new_vr
->type
== VR_UNDEFINED
)
674 BITMAP_FREE (new_vr
->equiv
);
675 set_value_range_to_varying (old_vr
);
676 set_value_range_to_varying (new_vr
);
680 set_value_range (old_vr
, new_vr
->type
, new_vr
->min
, new_vr
->max
,
684 BITMAP_FREE (new_vr
->equiv
);
690 /* Add VAR and VAR's equivalence set to EQUIV. This is the central
691 point where equivalence processing can be turned on/off. */
694 add_equivalence (bitmap
*equiv
, const_tree var
)
696 unsigned ver
= SSA_NAME_VERSION (var
);
697 value_range
*vr
= get_value_range (var
);
700 *equiv
= BITMAP_ALLOC (&vrp_equiv_obstack
);
701 bitmap_set_bit (*equiv
, ver
);
703 bitmap_ior_into (*equiv
, vr
->equiv
);
707 /* Return true if VR is ~[0, 0]. */
710 range_is_nonnull (value_range
*vr
)
712 return vr
->type
== VR_ANTI_RANGE
713 && integer_zerop (vr
->min
)
714 && integer_zerop (vr
->max
);
718 /* Return true if VR is [0, 0]. */
721 range_is_null (value_range
*vr
)
723 return vr
->type
== VR_RANGE
724 && integer_zerop (vr
->min
)
725 && integer_zerop (vr
->max
);
728 /* Return true if max and min of VR are INTEGER_CST. It's not necessary
732 range_int_cst_p (value_range
*vr
)
734 return (vr
->type
== VR_RANGE
735 && TREE_CODE (vr
->max
) == INTEGER_CST
736 && TREE_CODE (vr
->min
) == INTEGER_CST
);
739 /* Return true if VR is a INTEGER_CST singleton. */
742 range_int_cst_singleton_p (value_range
*vr
)
744 return (range_int_cst_p (vr
)
745 && tree_int_cst_equal (vr
->min
, vr
->max
));
748 /* Return true if value range VR involves at least one symbol. */
751 symbolic_range_p (value_range
*vr
)
753 return (!is_gimple_min_invariant (vr
->min
)
754 || !is_gimple_min_invariant (vr
->max
));
757 /* Return the single symbol (an SSA_NAME) contained in T if any, or NULL_TREE
758 otherwise. We only handle additive operations and set NEG to true if the
759 symbol is negated and INV to the invariant part, if any. */
762 get_single_symbol (tree t
, bool *neg
, tree
*inv
)
770 if (TREE_CODE (t
) == PLUS_EXPR
771 || TREE_CODE (t
) == POINTER_PLUS_EXPR
772 || TREE_CODE (t
) == MINUS_EXPR
)
774 if (is_gimple_min_invariant (TREE_OPERAND (t
, 0)))
776 neg_
= (TREE_CODE (t
) == MINUS_EXPR
);
777 inv_
= TREE_OPERAND (t
, 0);
778 t
= TREE_OPERAND (t
, 1);
780 else if (is_gimple_min_invariant (TREE_OPERAND (t
, 1)))
783 inv_
= TREE_OPERAND (t
, 1);
784 t
= TREE_OPERAND (t
, 0);
795 if (TREE_CODE (t
) == NEGATE_EXPR
)
797 t
= TREE_OPERAND (t
, 0);
801 if (TREE_CODE (t
) != SSA_NAME
)
804 if (inv_
&& TREE_OVERFLOW_P (inv_
))
805 inv_
= drop_tree_overflow (inv_
);
812 /* The reverse operation: build a symbolic expression with TYPE
813 from symbol SYM, negated according to NEG, and invariant INV. */
816 build_symbolic_expr (tree type
, tree sym
, bool neg
, tree inv
)
818 const bool pointer_p
= POINTER_TYPE_P (type
);
822 t
= build1 (NEGATE_EXPR
, type
, t
);
824 if (integer_zerop (inv
))
827 return build2 (pointer_p
? POINTER_PLUS_EXPR
: PLUS_EXPR
, type
, t
, inv
);
830 /* Return true if value range VR involves exactly one symbol SYM. */
833 symbolic_range_based_on_p (value_range
*vr
, const_tree sym
)
835 bool neg
, min_has_symbol
, max_has_symbol
;
838 if (is_gimple_min_invariant (vr
->min
))
839 min_has_symbol
= false;
840 else if (get_single_symbol (vr
->min
, &neg
, &inv
) == sym
)
841 min_has_symbol
= true;
845 if (is_gimple_min_invariant (vr
->max
))
846 max_has_symbol
= false;
847 else if (get_single_symbol (vr
->max
, &neg
, &inv
) == sym
)
848 max_has_symbol
= true;
852 return (min_has_symbol
|| max_has_symbol
);
855 /* Return true if the result of assignment STMT is know to be non-zero. */
858 gimple_assign_nonzero_p (gimple
*stmt
)
860 enum tree_code code
= gimple_assign_rhs_code (stmt
);
861 bool strict_overflow_p
;
862 switch (get_gimple_rhs_class (code
))
864 case GIMPLE_UNARY_RHS
:
865 return tree_unary_nonzero_warnv_p (gimple_assign_rhs_code (stmt
),
866 gimple_expr_type (stmt
),
867 gimple_assign_rhs1 (stmt
),
869 case GIMPLE_BINARY_RHS
:
870 return tree_binary_nonzero_warnv_p (gimple_assign_rhs_code (stmt
),
871 gimple_expr_type (stmt
),
872 gimple_assign_rhs1 (stmt
),
873 gimple_assign_rhs2 (stmt
),
875 case GIMPLE_TERNARY_RHS
:
877 case GIMPLE_SINGLE_RHS
:
878 return tree_single_nonzero_warnv_p (gimple_assign_rhs1 (stmt
),
880 case GIMPLE_INVALID_RHS
:
887 /* Return true if STMT is known to compute a non-zero value. */
890 gimple_stmt_nonzero_p (gimple
*stmt
)
892 switch (gimple_code (stmt
))
895 return gimple_assign_nonzero_p (stmt
);
898 tree fndecl
= gimple_call_fndecl (stmt
);
899 if (!fndecl
) return false;
900 if (flag_delete_null_pointer_checks
&& !flag_check_new
901 && DECL_IS_OPERATOR_NEW (fndecl
)
902 && !TREE_NOTHROW (fndecl
))
904 /* References are always non-NULL. */
905 if (flag_delete_null_pointer_checks
906 && TREE_CODE (TREE_TYPE (fndecl
)) == REFERENCE_TYPE
)
908 if (flag_delete_null_pointer_checks
&&
909 lookup_attribute ("returns_nonnull",
910 TYPE_ATTRIBUTES (gimple_call_fntype (stmt
))))
913 gcall
*call_stmt
= as_a
<gcall
*> (stmt
);
914 unsigned rf
= gimple_call_return_flags (call_stmt
);
915 if (rf
& ERF_RETURNS_ARG
)
917 unsigned argnum
= rf
& ERF_RETURN_ARG_MASK
;
918 if (argnum
< gimple_call_num_args (call_stmt
))
920 tree arg
= gimple_call_arg (call_stmt
, argnum
);
922 && infer_nonnull_range_by_attribute (stmt
, arg
))
926 return gimple_alloca_call_p (stmt
);
933 /* Like tree_expr_nonzero_p, but this function uses value ranges
937 vrp_stmt_computes_nonzero (gimple
*stmt
)
939 if (gimple_stmt_nonzero_p (stmt
))
942 /* If we have an expression of the form &X->a, then the expression
943 is nonnull if X is nonnull. */
944 if (is_gimple_assign (stmt
)
945 && gimple_assign_rhs_code (stmt
) == ADDR_EXPR
)
947 tree expr
= gimple_assign_rhs1 (stmt
);
948 tree base
= get_base_address (TREE_OPERAND (expr
, 0));
950 if (base
!= NULL_TREE
951 && TREE_CODE (base
) == MEM_REF
952 && TREE_CODE (TREE_OPERAND (base
, 0)) == SSA_NAME
)
954 value_range
*vr
= get_value_range (TREE_OPERAND (base
, 0));
955 if (range_is_nonnull (vr
))
963 /* Returns true if EXPR is a valid value (as expected by compare_values) --
964 a gimple invariant, or SSA_NAME +- CST. */
967 valid_value_p (tree expr
)
969 if (TREE_CODE (expr
) == SSA_NAME
)
972 if (TREE_CODE (expr
) == PLUS_EXPR
973 || TREE_CODE (expr
) == MINUS_EXPR
)
974 return (TREE_CODE (TREE_OPERAND (expr
, 0)) == SSA_NAME
975 && TREE_CODE (TREE_OPERAND (expr
, 1)) == INTEGER_CST
);
977 return is_gimple_min_invariant (expr
);
983 -2 if those are incomparable. */
985 operand_less_p (tree val
, tree val2
)
987 /* LT is folded faster than GE and others. Inline the common case. */
988 if (TREE_CODE (val
) == INTEGER_CST
&& TREE_CODE (val2
) == INTEGER_CST
)
989 return tree_int_cst_lt (val
, val2
);
994 fold_defer_overflow_warnings ();
996 tcmp
= fold_binary_to_constant (LT_EXPR
, boolean_type_node
, val
, val2
);
998 fold_undefer_and_ignore_overflow_warnings ();
1001 || TREE_CODE (tcmp
) != INTEGER_CST
)
1004 if (!integer_zerop (tcmp
))
1011 /* Compare two values VAL1 and VAL2. Return
1013 -2 if VAL1 and VAL2 cannot be compared at compile-time,
1016 +1 if VAL1 > VAL2, and
1019 This is similar to tree_int_cst_compare but supports pointer values
1020 and values that cannot be compared at compile time.
1022 If STRICT_OVERFLOW_P is not NULL, then set *STRICT_OVERFLOW_P to
1023 true if the return value is only valid if we assume that signed
1024 overflow is undefined. */
1027 compare_values_warnv (tree val1
, tree val2
, bool *strict_overflow_p
)
1032 /* Below we rely on the fact that VAL1 and VAL2 are both pointers or
1034 gcc_assert (POINTER_TYPE_P (TREE_TYPE (val1
))
1035 == POINTER_TYPE_P (TREE_TYPE (val2
)));
1037 /* Convert the two values into the same type. This is needed because
1038 sizetype causes sign extension even for unsigned types. */
1039 val2
= fold_convert (TREE_TYPE (val1
), val2
);
1040 STRIP_USELESS_TYPE_CONVERSION (val2
);
1042 const bool overflow_undefined
1043 = INTEGRAL_TYPE_P (TREE_TYPE (val1
))
1044 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (val1
));
1047 tree sym1
= get_single_symbol (val1
, &neg1
, &inv1
);
1048 tree sym2
= get_single_symbol (val2
, &neg2
, &inv2
);
1050 /* If VAL1 and VAL2 are of the form '[-]NAME [+ CST]', return -1 or +1
1051 accordingly. If VAL1 and VAL2 don't use the same name, return -2. */
1054 /* Both values must use the same name with the same sign. */
1055 if (sym1
!= sym2
|| neg1
!= neg2
)
1058 /* [-]NAME + CST == [-]NAME + CST. */
1062 /* If overflow is defined we cannot simplify more. */
1063 if (!overflow_undefined
)
1066 if (strict_overflow_p
!= NULL
1067 /* Symbolic range building sets TREE_NO_WARNING to declare
1068 that overflow doesn't happen. */
1069 && (!inv1
|| !TREE_NO_WARNING (val1
))
1070 && (!inv2
|| !TREE_NO_WARNING (val2
)))
1071 *strict_overflow_p
= true;
1074 inv1
= build_int_cst (TREE_TYPE (val1
), 0);
1076 inv2
= build_int_cst (TREE_TYPE (val2
), 0);
1078 return wi::cmp (wi::to_wide (inv1
), wi::to_wide (inv2
),
1079 TYPE_SIGN (TREE_TYPE (val1
)));
1082 const bool cst1
= is_gimple_min_invariant (val1
);
1083 const bool cst2
= is_gimple_min_invariant (val2
);
1085 /* If one is of the form '[-]NAME + CST' and the other is constant, then
1086 it might be possible to say something depending on the constants. */
1087 if ((sym1
&& inv1
&& cst2
) || (sym2
&& inv2
&& cst1
))
1089 if (!overflow_undefined
)
1092 if (strict_overflow_p
!= NULL
1093 /* Symbolic range building sets TREE_NO_WARNING to declare
1094 that overflow doesn't happen. */
1095 && (!sym1
|| !TREE_NO_WARNING (val1
))
1096 && (!sym2
|| !TREE_NO_WARNING (val2
)))
1097 *strict_overflow_p
= true;
1099 const signop sgn
= TYPE_SIGN (TREE_TYPE (val1
));
1100 tree cst
= cst1
? val1
: val2
;
1101 tree inv
= cst1
? inv2
: inv1
;
1103 /* Compute the difference between the constants. If it overflows or
1104 underflows, this means that we can trivially compare the NAME with
1105 it and, consequently, the two values with each other. */
1106 wide_int diff
= wi::to_wide (cst
) - wi::to_wide (inv
);
1107 if (wi::cmp (0, wi::to_wide (inv
), sgn
)
1108 != wi::cmp (diff
, wi::to_wide (cst
), sgn
))
1110 const int res
= wi::cmp (wi::to_wide (cst
), wi::to_wide (inv
), sgn
);
1111 return cst1
? res
: -res
;
1117 /* We cannot say anything more for non-constants. */
1121 if (!POINTER_TYPE_P (TREE_TYPE (val1
)))
1123 /* We cannot compare overflowed values. */
1124 if (TREE_OVERFLOW (val1
) || TREE_OVERFLOW (val2
))
1127 return tree_int_cst_compare (val1
, val2
);
1133 /* First see if VAL1 and VAL2 are not the same. */
1134 if (val1
== val2
|| operand_equal_p (val1
, val2
, 0))
1137 /* If VAL1 is a lower address than VAL2, return -1. */
1138 if (operand_less_p (val1
, val2
) == 1)
1141 /* If VAL1 is a higher address than VAL2, return +1. */
1142 if (operand_less_p (val2
, val1
) == 1)
1145 /* If VAL1 is different than VAL2, return +2.
1146 For integer constants we either have already returned -1 or 1
1147 or they are equivalent. We still might succeed in proving
1148 something about non-trivial operands. */
1149 if (TREE_CODE (val1
) != INTEGER_CST
1150 || TREE_CODE (val2
) != INTEGER_CST
)
1152 t
= fold_binary_to_constant (NE_EXPR
, boolean_type_node
, val1
, val2
);
1153 if (t
&& integer_onep (t
))
1161 /* Compare values like compare_values_warnv. */
1164 compare_values (tree val1
, tree val2
)
1167 return compare_values_warnv (val1
, val2
, &sop
);
1171 /* Return 1 if VAL is inside value range MIN <= VAL <= MAX,
1172 0 if VAL is not inside [MIN, MAX],
1173 -2 if we cannot tell either way.
1175 Benchmark compile/20001226-1.c compilation time after changing this
1179 value_inside_range (tree val
, tree min
, tree max
)
1183 cmp1
= operand_less_p (val
, min
);
1189 cmp2
= operand_less_p (max
, val
);
1197 /* Return true if value ranges VR0 and VR1 have a non-empty
1200 Benchmark compile/20001226-1.c compilation time after changing this
1205 value_ranges_intersect_p (value_range
*vr0
, value_range
*vr1
)
1207 /* The value ranges do not intersect if the maximum of the first range is
1208 less than the minimum of the second range or vice versa.
1209 When those relations are unknown, we can't do any better. */
1210 if (operand_less_p (vr0
->max
, vr1
->min
) != 0)
1212 if (operand_less_p (vr1
->max
, vr0
->min
) != 0)
1218 /* Return 1 if [MIN, MAX] includes the value zero, 0 if it does not
1219 include the value zero, -2 if we cannot tell. */
1222 range_includes_zero_p (tree min
, tree max
)
1224 tree zero
= build_int_cst (TREE_TYPE (min
), 0);
1225 return value_inside_range (zero
, min
, max
);
1228 /* Return true if *VR is know to only contain nonnegative values. */
1231 value_range_nonnegative_p (value_range
*vr
)
1233 /* Testing for VR_ANTI_RANGE is not useful here as any anti-range
1234 which would return a useful value should be encoded as a
1236 if (vr
->type
== VR_RANGE
)
1238 int result
= compare_values (vr
->min
, integer_zero_node
);
1239 return (result
== 0 || result
== 1);
1245 /* If *VR has a value rante that is a single constant value return that,
1246 otherwise return NULL_TREE. */
1249 value_range_constant_singleton (value_range
*vr
)
1251 if (vr
->type
== VR_RANGE
1252 && vrp_operand_equal_p (vr
->min
, vr
->max
)
1253 && is_gimple_min_invariant (vr
->min
))
1259 /* If OP has a value range with a single constant value return that,
1260 otherwise return NULL_TREE. This returns OP itself if OP is a
1264 op_with_constant_singleton_value_range (tree op
)
1266 if (is_gimple_min_invariant (op
))
1269 if (TREE_CODE (op
) != SSA_NAME
)
1272 return value_range_constant_singleton (get_value_range (op
));
1275 /* Return true if op is in a boolean [0, 1] value-range. */
1278 op_with_boolean_value_range_p (tree op
)
1282 if (TYPE_PRECISION (TREE_TYPE (op
)) == 1)
1285 if (integer_zerop (op
)
1286 || integer_onep (op
))
1289 if (TREE_CODE (op
) != SSA_NAME
)
1292 vr
= get_value_range (op
);
1293 return (vr
->type
== VR_RANGE
1294 && integer_zerop (vr
->min
)
1295 && integer_onep (vr
->max
));
1298 /* Extract value range information for VAR when (OP COND_CODE LIMIT) is
1299 true and store it in *VR_P. */
1302 extract_range_for_var_from_comparison_expr (tree var
, enum tree_code cond_code
,
1303 tree op
, tree limit
,
1306 tree min
, max
, type
;
1307 value_range
*limit_vr
;
1308 type
= TREE_TYPE (var
);
1309 gcc_assert (limit
!= var
);
1311 /* For pointer arithmetic, we only keep track of pointer equality
1313 if (POINTER_TYPE_P (type
) && cond_code
!= NE_EXPR
&& cond_code
!= EQ_EXPR
)
1315 set_value_range_to_varying (vr_p
);
1319 /* If LIMIT is another SSA name and LIMIT has a range of its own,
1320 try to use LIMIT's range to avoid creating symbolic ranges
1322 limit_vr
= (TREE_CODE (limit
) == SSA_NAME
) ? get_value_range (limit
) : NULL
;
1324 /* LIMIT's range is only interesting if it has any useful information. */
1326 || limit_vr
->type
== VR_UNDEFINED
1327 || limit_vr
->type
== VR_VARYING
1328 || (symbolic_range_p (limit_vr
)
1329 && ! (limit_vr
->type
== VR_RANGE
1330 && (limit_vr
->min
== limit_vr
->max
1331 || operand_equal_p (limit_vr
->min
, limit_vr
->max
, 0)))))
1334 /* Initially, the new range has the same set of equivalences of
1335 VAR's range. This will be revised before returning the final
1336 value. Since assertions may be chained via mutually exclusive
1337 predicates, we will need to trim the set of equivalences before
1339 gcc_assert (vr_p
->equiv
== NULL
);
1340 add_equivalence (&vr_p
->equiv
, var
);
1342 /* Extract a new range based on the asserted comparison for VAR and
1343 LIMIT's value range. Notice that if LIMIT has an anti-range, we
1344 will only use it for equality comparisons (EQ_EXPR). For any
1345 other kind of assertion, we cannot derive a range from LIMIT's
1346 anti-range that can be used to describe the new range. For
1347 instance, ASSERT_EXPR <x_2, x_2 <= b_4>. If b_4 is ~[2, 10],
1348 then b_4 takes on the ranges [-INF, 1] and [11, +INF]. There is
1349 no single range for x_2 that could describe LE_EXPR, so we might
1350 as well build the range [b_4, +INF] for it.
1351 One special case we handle is extracting a range from a
1352 range test encoded as (unsigned)var + CST <= limit. */
1353 if (TREE_CODE (op
) == NOP_EXPR
1354 || TREE_CODE (op
) == PLUS_EXPR
)
1356 if (TREE_CODE (op
) == PLUS_EXPR
)
1358 min
= fold_build1 (NEGATE_EXPR
, TREE_TYPE (TREE_OPERAND (op
, 1)),
1359 TREE_OPERAND (op
, 1));
1360 max
= int_const_binop (PLUS_EXPR
, limit
, min
);
1361 op
= TREE_OPERAND (op
, 0);
1365 min
= build_int_cst (TREE_TYPE (var
), 0);
1369 /* Make sure to not set TREE_OVERFLOW on the final type
1370 conversion. We are willingly interpreting large positive
1371 unsigned values as negative signed values here. */
1372 min
= force_fit_type (TREE_TYPE (var
), wi::to_widest (min
), 0, false);
1373 max
= force_fit_type (TREE_TYPE (var
), wi::to_widest (max
), 0, false);
1375 /* We can transform a max, min range to an anti-range or
1376 vice-versa. Use set_and_canonicalize_value_range which does
1378 if (cond_code
== LE_EXPR
)
1379 set_and_canonicalize_value_range (vr_p
, VR_RANGE
,
1380 min
, max
, vr_p
->equiv
);
1381 else if (cond_code
== GT_EXPR
)
1382 set_and_canonicalize_value_range (vr_p
, VR_ANTI_RANGE
,
1383 min
, max
, vr_p
->equiv
);
1387 else if (cond_code
== EQ_EXPR
)
1389 enum value_range_type range_type
;
1393 range_type
= limit_vr
->type
;
1394 min
= limit_vr
->min
;
1395 max
= limit_vr
->max
;
1399 range_type
= VR_RANGE
;
1404 set_value_range (vr_p
, range_type
, min
, max
, vr_p
->equiv
);
1406 /* When asserting the equality VAR == LIMIT and LIMIT is another
1407 SSA name, the new range will also inherit the equivalence set
1409 if (TREE_CODE (limit
) == SSA_NAME
)
1410 add_equivalence (&vr_p
->equiv
, limit
);
1412 else if (cond_code
== NE_EXPR
)
1414 /* As described above, when LIMIT's range is an anti-range and
1415 this assertion is an inequality (NE_EXPR), then we cannot
1416 derive anything from the anti-range. For instance, if
1417 LIMIT's range was ~[0, 0], the assertion 'VAR != LIMIT' does
1418 not imply that VAR's range is [0, 0]. So, in the case of
1419 anti-ranges, we just assert the inequality using LIMIT and
1422 If LIMIT_VR is a range, we can only use it to build a new
1423 anti-range if LIMIT_VR is a single-valued range. For
1424 instance, if LIMIT_VR is [0, 1], the predicate
1425 VAR != [0, 1] does not mean that VAR's range is ~[0, 1].
1426 Rather, it means that for value 0 VAR should be ~[0, 0]
1427 and for value 1, VAR should be ~[1, 1]. We cannot
1428 represent these ranges.
1430 The only situation in which we can build a valid
1431 anti-range is when LIMIT_VR is a single-valued range
1432 (i.e., LIMIT_VR->MIN == LIMIT_VR->MAX). In that case,
1433 build the anti-range ~[LIMIT_VR->MIN, LIMIT_VR->MAX]. */
1435 && limit_vr
->type
== VR_RANGE
1436 && compare_values (limit_vr
->min
, limit_vr
->max
) == 0)
1438 min
= limit_vr
->min
;
1439 max
= limit_vr
->max
;
1443 /* In any other case, we cannot use LIMIT's range to build a
1444 valid anti-range. */
1448 /* If MIN and MAX cover the whole range for their type, then
1449 just use the original LIMIT. */
1450 if (INTEGRAL_TYPE_P (type
)
1451 && vrp_val_is_min (min
)
1452 && vrp_val_is_max (max
))
1455 set_and_canonicalize_value_range (vr_p
, VR_ANTI_RANGE
,
1456 min
, max
, vr_p
->equiv
);
1458 else if (cond_code
== LE_EXPR
|| cond_code
== LT_EXPR
)
1460 min
= TYPE_MIN_VALUE (type
);
1462 if (limit_vr
== NULL
|| limit_vr
->type
== VR_ANTI_RANGE
)
1466 /* If LIMIT_VR is of the form [N1, N2], we need to build the
1467 range [MIN, N2] for LE_EXPR and [MIN, N2 - 1] for
1469 max
= limit_vr
->max
;
1472 /* If the maximum value forces us to be out of bounds, simply punt.
1473 It would be pointless to try and do anything more since this
1474 all should be optimized away above us. */
1475 if (cond_code
== LT_EXPR
1476 && compare_values (max
, min
) == 0)
1477 set_value_range_to_varying (vr_p
);
1480 /* For LT_EXPR, we create the range [MIN, MAX - 1]. */
1481 if (cond_code
== LT_EXPR
)
1483 if (TYPE_PRECISION (TREE_TYPE (max
)) == 1
1484 && !TYPE_UNSIGNED (TREE_TYPE (max
)))
1485 max
= fold_build2 (PLUS_EXPR
, TREE_TYPE (max
), max
,
1486 build_int_cst (TREE_TYPE (max
), -1));
1488 max
= fold_build2 (MINUS_EXPR
, TREE_TYPE (max
), max
,
1489 build_int_cst (TREE_TYPE (max
), 1));
1490 /* Signal to compare_values_warnv this expr doesn't overflow. */
1492 TREE_NO_WARNING (max
) = 1;
1495 set_value_range (vr_p
, VR_RANGE
, min
, max
, vr_p
->equiv
);
1498 else if (cond_code
== GE_EXPR
|| cond_code
== GT_EXPR
)
1500 max
= TYPE_MAX_VALUE (type
);
1502 if (limit_vr
== NULL
|| limit_vr
->type
== VR_ANTI_RANGE
)
1506 /* If LIMIT_VR is of the form [N1, N2], we need to build the
1507 range [N1, MAX] for GE_EXPR and [N1 + 1, MAX] for
1509 min
= limit_vr
->min
;
1512 /* If the minimum value forces us to be out of bounds, simply punt.
1513 It would be pointless to try and do anything more since this
1514 all should be optimized away above us. */
1515 if (cond_code
== GT_EXPR
1516 && compare_values (min
, max
) == 0)
1517 set_value_range_to_varying (vr_p
);
1520 /* For GT_EXPR, we create the range [MIN + 1, MAX]. */
1521 if (cond_code
== GT_EXPR
)
1523 if (TYPE_PRECISION (TREE_TYPE (min
)) == 1
1524 && !TYPE_UNSIGNED (TREE_TYPE (min
)))
1525 min
= fold_build2 (MINUS_EXPR
, TREE_TYPE (min
), min
,
1526 build_int_cst (TREE_TYPE (min
), -1));
1528 min
= fold_build2 (PLUS_EXPR
, TREE_TYPE (min
), min
,
1529 build_int_cst (TREE_TYPE (min
), 1));
1530 /* Signal to compare_values_warnv this expr doesn't overflow. */
1532 TREE_NO_WARNING (min
) = 1;
1535 set_value_range (vr_p
, VR_RANGE
, min
, max
, vr_p
->equiv
);
1541 /* Finally intersect the new range with what we already know about var. */
1542 vrp_intersect_ranges (vr_p
, get_value_range (var
));
1545 /* Extract value range information from an ASSERT_EXPR EXPR and store
1549 extract_range_from_assert (value_range
*vr_p
, tree expr
)
1551 tree var
= ASSERT_EXPR_VAR (expr
);
1552 tree cond
= ASSERT_EXPR_COND (expr
);
1554 enum tree_code cond_code
;
1555 gcc_assert (COMPARISON_CLASS_P (cond
));
1557 /* Find VAR in the ASSERT_EXPR conditional. */
1558 if (var
== TREE_OPERAND (cond
, 0)
1559 || TREE_CODE (TREE_OPERAND (cond
, 0)) == PLUS_EXPR
1560 || TREE_CODE (TREE_OPERAND (cond
, 0)) == NOP_EXPR
)
1562 /* If the predicate is of the form VAR COMP LIMIT, then we just
1563 take LIMIT from the RHS and use the same comparison code. */
1564 cond_code
= TREE_CODE (cond
);
1565 limit
= TREE_OPERAND (cond
, 1);
1566 op
= TREE_OPERAND (cond
, 0);
1570 /* If the predicate is of the form LIMIT COMP VAR, then we need
1571 to flip around the comparison code to create the proper range
1573 cond_code
= swap_tree_comparison (TREE_CODE (cond
));
1574 limit
= TREE_OPERAND (cond
, 0);
1575 op
= TREE_OPERAND (cond
, 1);
1577 extract_range_for_var_from_comparison_expr (var
, cond_code
, op
,
1581 /* Extract range information from SSA name VAR and store it in VR. If
1582 VAR has an interesting range, use it. Otherwise, create the
1583 range [VAR, VAR] and return it. This is useful in situations where
1584 we may have conditionals testing values of VARYING names. For
1591 Even if y_5 is deemed VARYING, we can determine that x_3 > y_5 is
1595 extract_range_from_ssa_name (value_range
*vr
, tree var
)
1597 value_range
*var_vr
= get_value_range (var
);
1599 if (var_vr
->type
!= VR_VARYING
)
1600 copy_value_range (vr
, var_vr
);
1602 set_value_range (vr
, VR_RANGE
, var
, var
, NULL
);
1604 add_equivalence (&vr
->equiv
, var
);
1608 /* Wrapper around int_const_binop. Return true if we can compute the
1609 result; i.e. if the operation doesn't overflow or if the overflow is
1610 undefined. In the latter case (if the operation overflows and
1611 overflow is undefined), then adjust the result to be -INF or +INF
1612 depending on CODE, VAL1 and VAL2. Return the value in *RES.
1614 Return false for division by zero, for which the result is
1618 vrp_int_const_binop (enum tree_code code
, tree val1
, tree val2
, wide_int
*res
)
1620 bool overflow
= false;
1621 signop sign
= TYPE_SIGN (TREE_TYPE (val1
));
1628 wide_int wval2
= wi::to_wide (val2
, TYPE_PRECISION (TREE_TYPE (val1
)));
1629 if (wi::neg_p (wval2
))
1632 if (code
== RSHIFT_EXPR
)
1638 if (code
== RSHIFT_EXPR
)
1639 /* It's unclear from the C standard whether shifts can overflow.
1640 The following code ignores overflow; perhaps a C standard
1641 interpretation ruling is needed. */
1642 *res
= wi::rshift (wi::to_wide (val1
), wval2
, sign
);
1644 *res
= wi::lshift (wi::to_wide (val1
), wval2
);
1649 *res
= wi::mul (wi::to_wide (val1
),
1650 wi::to_wide (val2
), sign
, &overflow
);
1653 case TRUNC_DIV_EXPR
:
1654 case EXACT_DIV_EXPR
:
1658 *res
= wi::div_trunc (wi::to_wide (val1
),
1659 wi::to_wide (val2
), sign
, &overflow
);
1662 case FLOOR_DIV_EXPR
:
1665 *res
= wi::div_floor (wi::to_wide (val1
),
1666 wi::to_wide (val2
), sign
, &overflow
);
1672 *res
= wi::div_ceil (wi::to_wide (val1
),
1673 wi::to_wide (val2
), sign
, &overflow
);
1676 case ROUND_DIV_EXPR
:
1679 *res
= wi::div_round (wi::to_wide (val1
),
1680 wi::to_wide (val2
), sign
, &overflow
);
1688 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (val1
)))
1690 /* If the operation overflowed return -INF or +INF depending
1691 on the operation and the combination of signs of the operands. */
1692 int sgn1
= tree_int_cst_sgn (val1
);
1693 int sgn2
= tree_int_cst_sgn (val2
);
1695 /* Notice that we only need to handle the restricted set of
1696 operations handled by extract_range_from_binary_expr.
1697 Among them, only multiplication, addition and subtraction
1698 can yield overflow without overflown operands because we
1699 are working with integral types only... except in the
1700 case VAL1 = -INF and VAL2 = -1 which overflows to +INF
1701 for division too. */
1703 /* For multiplication, the sign of the overflow is given
1704 by the comparison of the signs of the operands. */
1705 if ((code
== MULT_EXPR
&& sgn1
== sgn2
)
1706 /* For addition, the operands must be of the same sign
1707 to yield an overflow. Its sign is therefore that
1708 of one of the operands, for example the first. */
1709 || (code
== PLUS_EXPR
&& sgn1
>= 0)
1710 /* For subtraction, operands must be of
1711 different signs to yield an overflow. Its sign is
1712 therefore that of the first operand or the opposite of
1713 that of the second operand. A first operand of 0 counts
1714 as positive here, for the corner case 0 - (-INF), which
1715 overflows, but must yield +INF. */
1716 || (code
== MINUS_EXPR
&& sgn1
>= 0)
1717 /* For division, the only case is -INF / -1 = +INF. */
1718 || code
== TRUNC_DIV_EXPR
1719 || code
== FLOOR_DIV_EXPR
1720 || code
== CEIL_DIV_EXPR
1721 || code
== EXACT_DIV_EXPR
1722 || code
== ROUND_DIV_EXPR
)
1723 *res
= wi::max_value (TYPE_PRECISION (TREE_TYPE (val1
)),
1724 TYPE_SIGN (TREE_TYPE (val1
)));
1726 *res
= wi::min_value (TYPE_PRECISION (TREE_TYPE (val1
)),
1727 TYPE_SIGN (TREE_TYPE (val1
)));
1735 /* For range VR compute two wide_int bitmasks. In *MAY_BE_NONZERO
1736 bitmask if some bit is unset, it means for all numbers in the range
1737 the bit is 0, otherwise it might be 0 or 1. In *MUST_BE_NONZERO
1738 bitmask if some bit is set, it means for all numbers in the range
1739 the bit is 1, otherwise it might be 0 or 1. */
1742 zero_nonzero_bits_from_vr (const tree expr_type
,
1744 wide_int
*may_be_nonzero
,
1745 wide_int
*must_be_nonzero
)
1747 *may_be_nonzero
= wi::minus_one (TYPE_PRECISION (expr_type
));
1748 *must_be_nonzero
= wi::zero (TYPE_PRECISION (expr_type
));
1749 if (!range_int_cst_p (vr
))
1752 if (range_int_cst_singleton_p (vr
))
1754 *may_be_nonzero
= wi::to_wide (vr
->min
);
1755 *must_be_nonzero
= *may_be_nonzero
;
1757 else if (tree_int_cst_sgn (vr
->min
) >= 0
1758 || tree_int_cst_sgn (vr
->max
) < 0)
1760 wide_int xor_mask
= wi::to_wide (vr
->min
) ^ wi::to_wide (vr
->max
);
1761 *may_be_nonzero
= wi::to_wide (vr
->min
) | wi::to_wide (vr
->max
);
1762 *must_be_nonzero
= wi::to_wide (vr
->min
) & wi::to_wide (vr
->max
);
1765 wide_int mask
= wi::mask (wi::floor_log2 (xor_mask
), false,
1766 may_be_nonzero
->get_precision ());
1767 *may_be_nonzero
= *may_be_nonzero
| mask
;
1768 *must_be_nonzero
= wi::bit_and_not (*must_be_nonzero
, mask
);
1775 /* Create two value-ranges in *VR0 and *VR1 from the anti-range *AR
1776 so that *VR0 U *VR1 == *AR. Returns true if that is possible,
1777 false otherwise. If *AR can be represented with a single range
1778 *VR1 will be VR_UNDEFINED. */
1781 ranges_from_anti_range (value_range
*ar
,
1782 value_range
*vr0
, value_range
*vr1
)
1784 tree type
= TREE_TYPE (ar
->min
);
1786 vr0
->type
= VR_UNDEFINED
;
1787 vr1
->type
= VR_UNDEFINED
;
1789 if (ar
->type
!= VR_ANTI_RANGE
1790 || TREE_CODE (ar
->min
) != INTEGER_CST
1791 || TREE_CODE (ar
->max
) != INTEGER_CST
1792 || !vrp_val_min (type
)
1793 || !vrp_val_max (type
))
1796 if (!vrp_val_is_min (ar
->min
))
1798 vr0
->type
= VR_RANGE
;
1799 vr0
->min
= vrp_val_min (type
);
1800 vr0
->max
= wide_int_to_tree (type
, wi::to_wide (ar
->min
) - 1);
1802 if (!vrp_val_is_max (ar
->max
))
1804 vr1
->type
= VR_RANGE
;
1805 vr1
->min
= wide_int_to_tree (type
, wi::to_wide (ar
->max
) + 1);
1806 vr1
->max
= vrp_val_max (type
);
1808 if (vr0
->type
== VR_UNDEFINED
)
1811 vr1
->type
= VR_UNDEFINED
;
1814 return vr0
->type
!= VR_UNDEFINED
;
1817 /* Helper to extract a value-range *VR for a multiplicative operation
1821 extract_range_from_multiplicative_op_1 (value_range
*vr
,
1822 enum tree_code code
,
1823 value_range
*vr0
, value_range
*vr1
)
1825 enum value_range_type rtype
;
1826 wide_int val
, min
, max
;
1829 /* Multiplications, divisions and shifts are a bit tricky to handle,
1830 depending on the mix of signs we have in the two ranges, we
1831 need to operate on different values to get the minimum and
1832 maximum values for the new range. One approach is to figure
1833 out all the variations of range combinations and do the
1836 However, this involves several calls to compare_values and it
1837 is pretty convoluted. It's simpler to do the 4 operations
1838 (MIN0 OP MIN1, MIN0 OP MAX1, MAX0 OP MIN1 and MAX0 OP MAX0 OP
1839 MAX1) and then figure the smallest and largest values to form
1841 gcc_assert (code
== MULT_EXPR
1842 || code
== TRUNC_DIV_EXPR
1843 || code
== FLOOR_DIV_EXPR
1844 || code
== CEIL_DIV_EXPR
1845 || code
== EXACT_DIV_EXPR
1846 || code
== ROUND_DIV_EXPR
1847 || code
== RSHIFT_EXPR
1848 || code
== LSHIFT_EXPR
);
1849 gcc_assert (vr0
->type
== VR_RANGE
1850 && vr0
->type
== vr1
->type
);
1853 type
= TREE_TYPE (vr0
->min
);
1854 signop sgn
= TYPE_SIGN (type
);
1856 /* Compute the 4 cross operations and their minimum and maximum value. */
1857 if (!vrp_int_const_binop (code
, vr0
->min
, vr1
->min
, &val
))
1859 set_value_range_to_varying (vr
);
1864 if (vr1
->max
!= vr1
->min
)
1866 if (!vrp_int_const_binop (code
, vr0
->min
, vr1
->max
, &val
))
1868 set_value_range_to_varying (vr
);
1871 if (wi::lt_p (val
, min
, sgn
))
1873 else if (wi::gt_p (val
, max
, sgn
))
1877 if (vr0
->max
!= vr0
->min
)
1879 if (!vrp_int_const_binop (code
, vr0
->max
, vr1
->min
, &val
))
1881 set_value_range_to_varying (vr
);
1884 if (wi::lt_p (val
, min
, sgn
))
1886 else if (wi::gt_p (val
, max
, sgn
))
1890 if (vr0
->min
!= vr0
->max
&& vr1
->min
!= vr1
->max
)
1892 if (!vrp_int_const_binop (code
, vr0
->max
, vr1
->max
, &val
))
1894 set_value_range_to_varying (vr
);
1897 if (wi::lt_p (val
, min
, sgn
))
1899 else if (wi::gt_p (val
, max
, sgn
))
1903 /* If the new range has its limits swapped around (MIN > MAX),
1904 then the operation caused one of them to wrap around, mark
1905 the new range VARYING. */
1906 if (wi::gt_p (min
, max
, sgn
))
1908 set_value_range_to_varying (vr
);
1912 /* We punt for [-INF, +INF].
1913 We learn nothing when we have INF on both sides.
1914 Note that we do accept [-INF, -INF] and [+INF, +INF]. */
1915 if (wi::eq_p (min
, wi::min_value (TYPE_PRECISION (type
), sgn
))
1916 && wi::eq_p (max
, wi::max_value (TYPE_PRECISION (type
), sgn
)))
1918 set_value_range_to_varying (vr
);
1922 set_value_range (vr
, rtype
,
1923 wide_int_to_tree (type
, min
),
1924 wide_int_to_tree (type
, max
), NULL
);
1927 /* Extract range information from a binary operation CODE based on
1928 the ranges of each of its operands *VR0 and *VR1 with resulting
1929 type EXPR_TYPE. The resulting range is stored in *VR. */
1932 extract_range_from_binary_expr_1 (value_range
*vr
,
1933 enum tree_code code
, tree expr_type
,
1934 value_range
*vr0_
, value_range
*vr1_
)
1936 value_range vr0
= *vr0_
, vr1
= *vr1_
;
1937 value_range vrtem0
= VR_INITIALIZER
, vrtem1
= VR_INITIALIZER
;
1938 enum value_range_type type
;
1939 tree min
= NULL_TREE
, max
= NULL_TREE
;
1942 if (!INTEGRAL_TYPE_P (expr_type
)
1943 && !POINTER_TYPE_P (expr_type
))
1945 set_value_range_to_varying (vr
);
1949 /* Not all binary expressions can be applied to ranges in a
1950 meaningful way. Handle only arithmetic operations. */
1951 if (code
!= PLUS_EXPR
1952 && code
!= MINUS_EXPR
1953 && code
!= POINTER_PLUS_EXPR
1954 && code
!= MULT_EXPR
1955 && code
!= TRUNC_DIV_EXPR
1956 && code
!= FLOOR_DIV_EXPR
1957 && code
!= CEIL_DIV_EXPR
1958 && code
!= EXACT_DIV_EXPR
1959 && code
!= ROUND_DIV_EXPR
1960 && code
!= TRUNC_MOD_EXPR
1961 && code
!= RSHIFT_EXPR
1962 && code
!= LSHIFT_EXPR
1965 && code
!= BIT_AND_EXPR
1966 && code
!= BIT_IOR_EXPR
1967 && code
!= BIT_XOR_EXPR
)
1969 set_value_range_to_varying (vr
);
1973 /* If both ranges are UNDEFINED, so is the result. */
1974 if (vr0
.type
== VR_UNDEFINED
&& vr1
.type
== VR_UNDEFINED
)
1976 set_value_range_to_undefined (vr
);
1979 /* If one of the ranges is UNDEFINED drop it to VARYING for the following
1980 code. At some point we may want to special-case operations that
1981 have UNDEFINED result for all or some value-ranges of the not UNDEFINED
1983 else if (vr0
.type
== VR_UNDEFINED
)
1984 set_value_range_to_varying (&vr0
);
1985 else if (vr1
.type
== VR_UNDEFINED
)
1986 set_value_range_to_varying (&vr1
);
1988 /* We get imprecise results from ranges_from_anti_range when
1989 code is EXACT_DIV_EXPR. We could mask out bits in the resulting
1990 range, but then we also need to hack up vrp_meet. It's just
1991 easier to special case when vr0 is ~[0,0] for EXACT_DIV_EXPR. */
1992 if (code
== EXACT_DIV_EXPR
1993 && vr0
.type
== VR_ANTI_RANGE
1994 && vr0
.min
== vr0
.max
1995 && integer_zerop (vr0
.min
))
1997 set_value_range_to_nonnull (vr
, expr_type
);
2001 /* Now canonicalize anti-ranges to ranges when they are not symbolic
2002 and express ~[] op X as ([]' op X) U ([]'' op X). */
2003 if (vr0
.type
== VR_ANTI_RANGE
2004 && ranges_from_anti_range (&vr0
, &vrtem0
, &vrtem1
))
2006 extract_range_from_binary_expr_1 (vr
, code
, expr_type
, &vrtem0
, vr1_
);
2007 if (vrtem1
.type
!= VR_UNDEFINED
)
2009 value_range vrres
= VR_INITIALIZER
;
2010 extract_range_from_binary_expr_1 (&vrres
, code
, expr_type
,
2012 vrp_meet (vr
, &vrres
);
2016 /* Likewise for X op ~[]. */
2017 if (vr1
.type
== VR_ANTI_RANGE
2018 && ranges_from_anti_range (&vr1
, &vrtem0
, &vrtem1
))
2020 extract_range_from_binary_expr_1 (vr
, code
, expr_type
, vr0_
, &vrtem0
);
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
);
2031 /* The type of the resulting value range defaults to VR0.TYPE. */
2034 /* Refuse to operate on VARYING ranges, ranges of different kinds
2035 and symbolic ranges. As an exception, we allow BIT_{AND,IOR}
2036 because we may be able to derive a useful range even if one of
2037 the operands is VR_VARYING or symbolic range. Similarly for
2038 divisions, MIN/MAX and PLUS/MINUS.
2040 TODO, we may be able to derive anti-ranges in some cases. */
2041 if (code
!= BIT_AND_EXPR
2042 && code
!= BIT_IOR_EXPR
2043 && code
!= TRUNC_DIV_EXPR
2044 && code
!= FLOOR_DIV_EXPR
2045 && code
!= CEIL_DIV_EXPR
2046 && code
!= EXACT_DIV_EXPR
2047 && code
!= ROUND_DIV_EXPR
2048 && code
!= TRUNC_MOD_EXPR
2051 && code
!= PLUS_EXPR
2052 && code
!= MINUS_EXPR
2053 && code
!= RSHIFT_EXPR
2054 && (vr0
.type
== VR_VARYING
2055 || vr1
.type
== VR_VARYING
2056 || vr0
.type
!= vr1
.type
2057 || symbolic_range_p (&vr0
)
2058 || symbolic_range_p (&vr1
)))
2060 set_value_range_to_varying (vr
);
2064 /* Now evaluate the expression to determine the new range. */
2065 if (POINTER_TYPE_P (expr_type
))
2067 if (code
== MIN_EXPR
|| code
== MAX_EXPR
)
2069 /* For MIN/MAX expressions with pointers, we only care about
2070 nullness, if both are non null, then the result is nonnull.
2071 If both are null, then the result is null. Otherwise they
2073 if (range_is_nonnull (&vr0
) && range_is_nonnull (&vr1
))
2074 set_value_range_to_nonnull (vr
, expr_type
);
2075 else if (range_is_null (&vr0
) && range_is_null (&vr1
))
2076 set_value_range_to_null (vr
, expr_type
);
2078 set_value_range_to_varying (vr
);
2080 else if (code
== POINTER_PLUS_EXPR
)
2082 /* For pointer types, we are really only interested in asserting
2083 whether the expression evaluates to non-NULL. */
2084 if (range_is_nonnull (&vr0
) || range_is_nonnull (&vr1
))
2085 set_value_range_to_nonnull (vr
, expr_type
);
2086 else if (range_is_null (&vr0
) && range_is_null (&vr1
))
2087 set_value_range_to_null (vr
, expr_type
);
2089 set_value_range_to_varying (vr
);
2091 else if (code
== BIT_AND_EXPR
)
2093 /* For pointer types, we are really only interested in asserting
2094 whether the expression evaluates to non-NULL. */
2095 if (range_is_nonnull (&vr0
) && range_is_nonnull (&vr1
))
2096 set_value_range_to_nonnull (vr
, expr_type
);
2097 else if (range_is_null (&vr0
) || range_is_null (&vr1
))
2098 set_value_range_to_null (vr
, expr_type
);
2100 set_value_range_to_varying (vr
);
2103 set_value_range_to_varying (vr
);
2108 /* For integer ranges, apply the operation to each end of the
2109 range and see what we end up with. */
2110 if (code
== PLUS_EXPR
|| code
== MINUS_EXPR
)
2112 const bool minus_p
= (code
== MINUS_EXPR
);
2113 tree min_op0
= vr0
.min
;
2114 tree min_op1
= minus_p
? vr1
.max
: vr1
.min
;
2115 tree max_op0
= vr0
.max
;
2116 tree max_op1
= minus_p
? vr1
.min
: vr1
.max
;
2117 tree sym_min_op0
= NULL_TREE
;
2118 tree sym_min_op1
= NULL_TREE
;
2119 tree sym_max_op0
= NULL_TREE
;
2120 tree sym_max_op1
= NULL_TREE
;
2121 bool neg_min_op0
, neg_min_op1
, neg_max_op0
, neg_max_op1
;
2123 /* If we have a PLUS or MINUS with two VR_RANGEs, either constant or
2124 single-symbolic ranges, try to compute the precise resulting range,
2125 but only if we know that this resulting range will also be constant
2126 or single-symbolic. */
2127 if (vr0
.type
== VR_RANGE
&& vr1
.type
== VR_RANGE
2128 && (TREE_CODE (min_op0
) == INTEGER_CST
2130 = get_single_symbol (min_op0
, &neg_min_op0
, &min_op0
)))
2131 && (TREE_CODE (min_op1
) == INTEGER_CST
2133 = get_single_symbol (min_op1
, &neg_min_op1
, &min_op1
)))
2134 && (!(sym_min_op0
&& sym_min_op1
)
2135 || (sym_min_op0
== sym_min_op1
2136 && neg_min_op0
== (minus_p
? neg_min_op1
: !neg_min_op1
)))
2137 && (TREE_CODE (max_op0
) == INTEGER_CST
2139 = get_single_symbol (max_op0
, &neg_max_op0
, &max_op0
)))
2140 && (TREE_CODE (max_op1
) == INTEGER_CST
2142 = get_single_symbol (max_op1
, &neg_max_op1
, &max_op1
)))
2143 && (!(sym_max_op0
&& sym_max_op1
)
2144 || (sym_max_op0
== sym_max_op1
2145 && neg_max_op0
== (minus_p
? neg_max_op1
: !neg_max_op1
))))
2147 const signop sgn
= TYPE_SIGN (expr_type
);
2148 const unsigned int prec
= TYPE_PRECISION (expr_type
);
2149 wide_int type_min
, type_max
, wmin
, wmax
;
2153 /* Get the lower and upper bounds of the type. */
2154 if (TYPE_OVERFLOW_WRAPS (expr_type
))
2156 type_min
= wi::min_value (prec
, sgn
);
2157 type_max
= wi::max_value (prec
, sgn
);
2161 type_min
= wi::to_wide (vrp_val_min (expr_type
));
2162 type_max
= wi::to_wide (vrp_val_max (expr_type
));
2165 /* Combine the lower bounds, if any. */
2166 if (min_op0
&& min_op1
)
2170 wmin
= wi::to_wide (min_op0
) - wi::to_wide (min_op1
);
2172 /* Check for overflow. */
2173 if (wi::cmp (0, wi::to_wide (min_op1
), sgn
)
2174 != wi::cmp (wmin
, wi::to_wide (min_op0
), sgn
))
2175 min_ovf
= wi::cmp (wi::to_wide (min_op0
),
2176 wi::to_wide (min_op1
), sgn
);
2180 wmin
= wi::to_wide (min_op0
) + wi::to_wide (min_op1
);
2182 /* Check for overflow. */
2183 if (wi::cmp (wi::to_wide (min_op1
), 0, sgn
)
2184 != wi::cmp (wmin
, wi::to_wide (min_op0
), sgn
))
2185 min_ovf
= wi::cmp (wi::to_wide (min_op0
), wmin
, sgn
);
2189 wmin
= wi::to_wide (min_op0
);
2194 wmin
= -wi::to_wide (min_op1
);
2196 /* Check for overflow. */
2198 && wi::neg_p (wi::to_wide (min_op1
))
2199 && wi::neg_p (wmin
))
2201 else if (sgn
== UNSIGNED
&& wi::to_wide (min_op1
) != 0)
2205 wmin
= wi::to_wide (min_op1
);
2208 wmin
= wi::shwi (0, prec
);
2210 /* Combine the upper bounds, if any. */
2211 if (max_op0
&& max_op1
)
2215 wmax
= wi::to_wide (max_op0
) - wi::to_wide (max_op1
);
2217 /* Check for overflow. */
2218 if (wi::cmp (0, wi::to_wide (max_op1
), sgn
)
2219 != wi::cmp (wmax
, wi::to_wide (max_op0
), sgn
))
2220 max_ovf
= wi::cmp (wi::to_wide (max_op0
),
2221 wi::to_wide (max_op1
), sgn
);
2225 wmax
= wi::to_wide (max_op0
) + wi::to_wide (max_op1
);
2227 if (wi::cmp (wi::to_wide (max_op1
), 0, sgn
)
2228 != wi::cmp (wmax
, wi::to_wide (max_op0
), sgn
))
2229 max_ovf
= wi::cmp (wi::to_wide (max_op0
), wmax
, sgn
);
2233 wmax
= wi::to_wide (max_op0
);
2238 wmax
= -wi::to_wide (max_op1
);
2240 /* Check for overflow. */
2242 && wi::neg_p (wi::to_wide (max_op1
))
2243 && wi::neg_p (wmax
))
2245 else if (sgn
== UNSIGNED
&& wi::to_wide (max_op1
) != 0)
2249 wmax
= wi::to_wide (max_op1
);
2252 wmax
= wi::shwi (0, prec
);
2254 /* Check for type overflow. */
2257 if (wi::cmp (wmin
, type_min
, sgn
) == -1)
2259 else if (wi::cmp (wmin
, type_max
, sgn
) == 1)
2264 if (wi::cmp (wmax
, type_min
, sgn
) == -1)
2266 else if (wi::cmp (wmax
, type_max
, sgn
) == 1)
2270 /* If we have overflow for the constant part and the resulting
2271 range will be symbolic, drop to VR_VARYING. */
2272 if ((min_ovf
&& sym_min_op0
!= sym_min_op1
)
2273 || (max_ovf
&& sym_max_op0
!= sym_max_op1
))
2275 set_value_range_to_varying (vr
);
2279 if (TYPE_OVERFLOW_WRAPS (expr_type
))
2281 /* If overflow wraps, truncate the values and adjust the
2282 range kind and bounds appropriately. */
2283 wide_int tmin
= wide_int::from (wmin
, prec
, sgn
);
2284 wide_int tmax
= wide_int::from (wmax
, prec
, sgn
);
2285 if (min_ovf
== max_ovf
)
2287 /* No overflow or both overflow or underflow. The
2288 range kind stays VR_RANGE. */
2289 min
= wide_int_to_tree (expr_type
, tmin
);
2290 max
= wide_int_to_tree (expr_type
, tmax
);
2292 else if ((min_ovf
== -1 && max_ovf
== 0)
2293 || (max_ovf
== 1 && min_ovf
== 0))
2295 /* Min underflow or max overflow. The range kind
2296 changes to VR_ANTI_RANGE. */
2297 bool covers
= false;
2298 wide_int tem
= tmin
;
2299 type
= VR_ANTI_RANGE
;
2301 if (wi::cmp (tmin
, tmax
, sgn
) < 0)
2304 if (wi::cmp (tmax
, tem
, sgn
) > 0)
2306 /* If the anti-range would cover nothing, drop to varying.
2307 Likewise if the anti-range bounds are outside of the
2309 if (covers
|| wi::cmp (tmin
, tmax
, sgn
) > 0)
2311 set_value_range_to_varying (vr
);
2314 min
= wide_int_to_tree (expr_type
, tmin
);
2315 max
= wide_int_to_tree (expr_type
, tmax
);
2319 /* Other underflow and/or overflow, drop to VR_VARYING. */
2320 set_value_range_to_varying (vr
);
2326 /* If overflow does not wrap, saturate to the types min/max
2329 min
= wide_int_to_tree (expr_type
, type_min
);
2330 else if (min_ovf
== 1)
2331 min
= wide_int_to_tree (expr_type
, type_max
);
2333 min
= wide_int_to_tree (expr_type
, wmin
);
2336 max
= wide_int_to_tree (expr_type
, type_min
);
2337 else if (max_ovf
== 1)
2338 max
= wide_int_to_tree (expr_type
, type_max
);
2340 max
= wide_int_to_tree (expr_type
, wmax
);
2343 /* If the result lower bound is constant, we're done;
2344 otherwise, build the symbolic lower bound. */
2345 if (sym_min_op0
== sym_min_op1
)
2347 else if (sym_min_op0
)
2348 min
= build_symbolic_expr (expr_type
, sym_min_op0
,
2350 else if (sym_min_op1
)
2352 /* We may not negate if that might introduce
2353 undefined overflow. */
2356 || TYPE_OVERFLOW_WRAPS (expr_type
))
2357 min
= build_symbolic_expr (expr_type
, sym_min_op1
,
2358 neg_min_op1
^ minus_p
, min
);
2363 /* Likewise for the upper bound. */
2364 if (sym_max_op0
== sym_max_op1
)
2366 else if (sym_max_op0
)
2367 max
= build_symbolic_expr (expr_type
, sym_max_op0
,
2369 else if (sym_max_op1
)
2371 /* We may not negate if that might introduce
2372 undefined overflow. */
2375 || TYPE_OVERFLOW_WRAPS (expr_type
))
2376 max
= build_symbolic_expr (expr_type
, sym_max_op1
,
2377 neg_max_op1
^ minus_p
, max
);
2384 /* For other cases, for example if we have a PLUS_EXPR with two
2385 VR_ANTI_RANGEs, drop to VR_VARYING. It would take more effort
2386 to compute a precise range for such a case.
2387 ??? General even mixed range kind operations can be expressed
2388 by for example transforming ~[3, 5] + [1, 2] to range-only
2389 operations and a union primitive:
2390 [-INF, 2] + [1, 2] U [5, +INF] + [1, 2]
2391 [-INF+1, 4] U [6, +INF(OVF)]
2392 though usually the union is not exactly representable with
2393 a single range or anti-range as the above is
2394 [-INF+1, +INF(OVF)] intersected with ~[5, 5]
2395 but one could use a scheme similar to equivalences for this. */
2396 set_value_range_to_varying (vr
);
2400 else if (code
== MIN_EXPR
2401 || code
== MAX_EXPR
)
2403 if (vr0
.type
== VR_RANGE
2404 && !symbolic_range_p (&vr0
))
2407 if (vr1
.type
== VR_RANGE
2408 && !symbolic_range_p (&vr1
))
2410 /* For operations that make the resulting range directly
2411 proportional to the original ranges, apply the operation to
2412 the same end of each range. */
2413 min
= int_const_binop (code
, vr0
.min
, vr1
.min
);
2414 max
= int_const_binop (code
, vr0
.max
, vr1
.max
);
2416 else if (code
== MIN_EXPR
)
2418 min
= vrp_val_min (expr_type
);
2421 else if (code
== MAX_EXPR
)
2424 max
= vrp_val_max (expr_type
);
2427 else if (vr1
.type
== VR_RANGE
2428 && !symbolic_range_p (&vr1
))
2431 if (code
== MIN_EXPR
)
2433 min
= vrp_val_min (expr_type
);
2436 else if (code
== MAX_EXPR
)
2439 max
= vrp_val_max (expr_type
);
2444 set_value_range_to_varying (vr
);
2448 else if (code
== MULT_EXPR
)
2450 /* Fancy code so that with unsigned, [-3,-1]*[-3,-1] does not
2451 drop to varying. This test requires 2*prec bits if both
2452 operands are signed and 2*prec + 2 bits if either is not. */
2454 signop sign
= TYPE_SIGN (expr_type
);
2455 unsigned int prec
= TYPE_PRECISION (expr_type
);
2457 if (!range_int_cst_p (&vr0
)
2458 || !range_int_cst_p (&vr1
))
2460 set_value_range_to_varying (vr
);
2464 if (TYPE_OVERFLOW_WRAPS (expr_type
))
2466 typedef FIXED_WIDE_INT (WIDE_INT_MAX_PRECISION
* 2) vrp_int
;
2467 typedef generic_wide_int
2468 <wi::extended_tree
<WIDE_INT_MAX_PRECISION
* 2> > vrp_int_cst
;
2469 vrp_int sizem1
= wi::mask
<vrp_int
> (prec
, false);
2470 vrp_int size
= sizem1
+ 1;
2472 /* Extend the values using the sign of the result to PREC2.
2473 From here on out, everthing is just signed math no matter
2474 what the input types were. */
2475 vrp_int min0
= vrp_int_cst (vr0
.min
);
2476 vrp_int max0
= vrp_int_cst (vr0
.max
);
2477 vrp_int min1
= vrp_int_cst (vr1
.min
);
2478 vrp_int max1
= vrp_int_cst (vr1
.max
);
2479 /* Canonicalize the intervals. */
2480 if (sign
== UNSIGNED
)
2482 if (wi::ltu_p (size
, min0
+ max0
))
2488 if (wi::ltu_p (size
, min1
+ max1
))
2495 vrp_int prod0
= min0
* min1
;
2496 vrp_int prod1
= min0
* max1
;
2497 vrp_int prod2
= max0
* min1
;
2498 vrp_int prod3
= max0
* max1
;
2500 /* Sort the 4 products so that min is in prod0 and max is in
2502 /* min0min1 > max0max1 */
2504 std::swap (prod0
, prod3
);
2506 /* min0max1 > max0min1 */
2508 std::swap (prod1
, prod2
);
2511 std::swap (prod0
, prod1
);
2514 std::swap (prod2
, prod3
);
2516 /* diff = max - min. */
2517 prod2
= prod3
- prod0
;
2518 if (wi::geu_p (prod2
, sizem1
))
2520 /* the range covers all values. */
2521 set_value_range_to_varying (vr
);
2525 /* The following should handle the wrapping and selecting
2526 VR_ANTI_RANGE for us. */
2527 min
= wide_int_to_tree (expr_type
, prod0
);
2528 max
= wide_int_to_tree (expr_type
, prod3
);
2529 set_and_canonicalize_value_range (vr
, VR_RANGE
, min
, max
, NULL
);
2533 /* If we have an unsigned MULT_EXPR with two VR_ANTI_RANGEs,
2534 drop to VR_VARYING. It would take more effort to compute a
2535 precise range for such a case. For example, if we have
2536 op0 == 65536 and op1 == 65536 with their ranges both being
2537 ~[0,0] on a 32-bit machine, we would have op0 * op1 == 0, so
2538 we cannot claim that the product is in ~[0,0]. Note that we
2539 are guaranteed to have vr0.type == vr1.type at this
2541 if (vr0
.type
== VR_ANTI_RANGE
2542 && !TYPE_OVERFLOW_UNDEFINED (expr_type
))
2544 set_value_range_to_varying (vr
);
2548 extract_range_from_multiplicative_op_1 (vr
, code
, &vr0
, &vr1
);
2551 else if (code
== RSHIFT_EXPR
2552 || code
== LSHIFT_EXPR
)
2554 /* If we have a RSHIFT_EXPR with any shift values outside [0..prec-1],
2555 then drop to VR_VARYING. Outside of this range we get undefined
2556 behavior from the shift operation. We cannot even trust
2557 SHIFT_COUNT_TRUNCATED at this stage, because that applies to rtl
2558 shifts, and the operation at the tree level may be widened. */
2559 if (range_int_cst_p (&vr1
)
2560 && compare_tree_int (vr1
.min
, 0) >= 0
2561 && compare_tree_int (vr1
.max
, TYPE_PRECISION (expr_type
)) == -1)
2563 if (code
== RSHIFT_EXPR
)
2565 /* Even if vr0 is VARYING or otherwise not usable, we can derive
2566 useful ranges just from the shift count. E.g.
2567 x >> 63 for signed 64-bit x is always [-1, 0]. */
2568 if (vr0
.type
!= VR_RANGE
|| symbolic_range_p (&vr0
))
2570 vr0
.type
= type
= VR_RANGE
;
2571 vr0
.min
= vrp_val_min (expr_type
);
2572 vr0
.max
= vrp_val_max (expr_type
);
2574 extract_range_from_multiplicative_op_1 (vr
, code
, &vr0
, &vr1
);
2577 /* We can map lshifts by constants to MULT_EXPR handling. */
2578 else if (code
== LSHIFT_EXPR
2579 && range_int_cst_singleton_p (&vr1
))
2581 bool saved_flag_wrapv
;
2582 value_range vr1p
= VR_INITIALIZER
;
2583 vr1p
.type
= VR_RANGE
;
2584 vr1p
.min
= (wide_int_to_tree
2586 wi::set_bit_in_zero (tree_to_shwi (vr1
.min
),
2587 TYPE_PRECISION (expr_type
))));
2588 vr1p
.max
= vr1p
.min
;
2589 /* We have to use a wrapping multiply though as signed overflow
2590 on lshifts is implementation defined in C89. */
2591 saved_flag_wrapv
= flag_wrapv
;
2593 extract_range_from_binary_expr_1 (vr
, MULT_EXPR
, expr_type
,
2595 flag_wrapv
= saved_flag_wrapv
;
2598 else if (code
== LSHIFT_EXPR
2599 && range_int_cst_p (&vr0
))
2601 int prec
= TYPE_PRECISION (expr_type
);
2602 int overflow_pos
= prec
;
2604 wide_int low_bound
, high_bound
;
2605 bool uns
= TYPE_UNSIGNED (expr_type
);
2606 bool in_bounds
= false;
2611 bound_shift
= overflow_pos
- tree_to_shwi (vr1
.max
);
2612 /* If bound_shift == HOST_BITS_PER_WIDE_INT, the llshift can
2613 overflow. However, for that to happen, vr1.max needs to be
2614 zero, which means vr1 is a singleton range of zero, which
2615 means it should be handled by the previous LSHIFT_EXPR
2617 wide_int bound
= wi::set_bit_in_zero (bound_shift
, prec
);
2618 wide_int complement
= ~(bound
- 1);
2623 high_bound
= complement
;
2624 if (wi::ltu_p (wi::to_wide (vr0
.max
), low_bound
))
2626 /* [5, 6] << [1, 2] == [10, 24]. */
2627 /* We're shifting out only zeroes, the value increases
2631 else if (wi::ltu_p (high_bound
, wi::to_wide (vr0
.min
)))
2633 /* [0xffffff00, 0xffffffff] << [1, 2]
2634 == [0xfffffc00, 0xfffffffe]. */
2635 /* We're shifting out only ones, the value decreases
2642 /* [-1, 1] << [1, 2] == [-4, 4]. */
2643 low_bound
= complement
;
2645 if (wi::lts_p (wi::to_wide (vr0
.max
), high_bound
)
2646 && wi::lts_p (low_bound
, wi::to_wide (vr0
.min
)))
2648 /* For non-negative numbers, we're shifting out only
2649 zeroes, the value increases monotonically.
2650 For negative numbers, we're shifting out only ones, the
2651 value decreases monotomically. */
2658 extract_range_from_multiplicative_op_1 (vr
, code
, &vr0
, &vr1
);
2663 set_value_range_to_varying (vr
);
2666 else if (code
== TRUNC_DIV_EXPR
2667 || code
== FLOOR_DIV_EXPR
2668 || code
== CEIL_DIV_EXPR
2669 || code
== EXACT_DIV_EXPR
2670 || code
== ROUND_DIV_EXPR
)
2672 if (vr0
.type
!= VR_RANGE
|| symbolic_range_p (&vr0
))
2674 /* For division, if op1 has VR_RANGE but op0 does not, something
2675 can be deduced just from that range. Say [min, max] / [4, max]
2676 gives [min / 4, max / 4] range. */
2677 if (vr1
.type
== VR_RANGE
2678 && !symbolic_range_p (&vr1
)
2679 && range_includes_zero_p (vr1
.min
, vr1
.max
) == 0)
2681 vr0
.type
= type
= VR_RANGE
;
2682 vr0
.min
= vrp_val_min (expr_type
);
2683 vr0
.max
= vrp_val_max (expr_type
);
2687 set_value_range_to_varying (vr
);
2692 /* For divisions, if flag_non_call_exceptions is true, we must
2693 not eliminate a division by zero. */
2694 if (cfun
->can_throw_non_call_exceptions
2695 && (vr1
.type
!= VR_RANGE
2696 || range_includes_zero_p (vr1
.min
, vr1
.max
) != 0))
2698 set_value_range_to_varying (vr
);
2702 /* For divisions, if op0 is VR_RANGE, we can deduce a range
2703 even if op1 is VR_VARYING, VR_ANTI_RANGE, symbolic or can
2705 if (vr0
.type
== VR_RANGE
2706 && (vr1
.type
!= VR_RANGE
2707 || range_includes_zero_p (vr1
.min
, vr1
.max
) != 0))
2709 tree zero
= build_int_cst (TREE_TYPE (vr0
.min
), 0);
2714 if (TYPE_UNSIGNED (expr_type
)
2715 || value_range_nonnegative_p (&vr1
))
2717 /* For unsigned division or when divisor is known
2718 to be non-negative, the range has to cover
2719 all numbers from 0 to max for positive max
2720 and all numbers from min to 0 for negative min. */
2721 cmp
= compare_values (vr0
.max
, zero
);
2724 /* When vr0.max < 0, vr1.min != 0 and value
2725 ranges for dividend and divisor are available. */
2726 if (vr1
.type
== VR_RANGE
2727 && !symbolic_range_p (&vr0
)
2728 && !symbolic_range_p (&vr1
)
2729 && compare_values (vr1
.min
, zero
) != 0)
2730 max
= int_const_binop (code
, vr0
.max
, vr1
.min
);
2734 else if (cmp
== 0 || cmp
== 1)
2738 cmp
= compare_values (vr0
.min
, zero
);
2741 /* For unsigned division when value ranges for dividend
2742 and divisor are available. */
2743 if (vr1
.type
== VR_RANGE
2744 && !symbolic_range_p (&vr0
)
2745 && !symbolic_range_p (&vr1
)
2746 && compare_values (vr1
.max
, zero
) != 0)
2747 min
= int_const_binop (code
, vr0
.min
, vr1
.max
);
2751 else if (cmp
== 0 || cmp
== -1)
2758 /* Otherwise the range is -max .. max or min .. -min
2759 depending on which bound is bigger in absolute value,
2760 as the division can change the sign. */
2761 abs_extent_range (vr
, vr0
.min
, vr0
.max
);
2764 if (type
== VR_VARYING
)
2766 set_value_range_to_varying (vr
);
2770 else if (!symbolic_range_p (&vr0
) && !symbolic_range_p (&vr1
))
2772 extract_range_from_multiplicative_op_1 (vr
, code
, &vr0
, &vr1
);
2776 else if (code
== TRUNC_MOD_EXPR
)
2778 if (range_is_null (&vr1
))
2780 set_value_range_to_undefined (vr
);
2783 /* ABS (A % B) < ABS (B) and either
2784 0 <= A % B <= A or A <= A % B <= 0. */
2786 signop sgn
= TYPE_SIGN (expr_type
);
2787 unsigned int prec
= TYPE_PRECISION (expr_type
);
2788 wide_int wmin
, wmax
, tmp
;
2789 if (vr1
.type
== VR_RANGE
&& !symbolic_range_p (&vr1
))
2791 wmax
= wi::to_wide (vr1
.max
) - 1;
2794 tmp
= -1 - wi::to_wide (vr1
.min
);
2795 wmax
= wi::smax (wmax
, tmp
);
2800 wmax
= wi::max_value (prec
, sgn
);
2801 /* X % INT_MIN may be INT_MAX. */
2802 if (sgn
== UNSIGNED
)
2806 if (sgn
== UNSIGNED
)
2807 wmin
= wi::zero (prec
);
2811 if (vr0
.type
== VR_RANGE
&& TREE_CODE (vr0
.min
) == INTEGER_CST
)
2813 tmp
= wi::to_wide (vr0
.min
);
2814 if (wi::gts_p (tmp
, 0))
2815 tmp
= wi::zero (prec
);
2816 wmin
= wi::smax (wmin
, tmp
);
2820 if (vr0
.type
== VR_RANGE
&& TREE_CODE (vr0
.max
) == INTEGER_CST
)
2822 tmp
= wi::to_wide (vr0
.max
);
2823 if (sgn
== SIGNED
&& wi::neg_p (tmp
))
2824 tmp
= wi::zero (prec
);
2825 wmax
= wi::min (wmax
, tmp
, sgn
);
2828 min
= wide_int_to_tree (expr_type
, wmin
);
2829 max
= wide_int_to_tree (expr_type
, wmax
);
2831 else if (code
== BIT_AND_EXPR
|| code
== BIT_IOR_EXPR
|| code
== BIT_XOR_EXPR
)
2833 bool int_cst_range0
, int_cst_range1
;
2834 wide_int may_be_nonzero0
, may_be_nonzero1
;
2835 wide_int must_be_nonzero0
, must_be_nonzero1
;
2837 int_cst_range0
= zero_nonzero_bits_from_vr (expr_type
, &vr0
,
2840 int_cst_range1
= zero_nonzero_bits_from_vr (expr_type
, &vr1
,
2844 if (code
== BIT_AND_EXPR
|| code
== BIT_IOR_EXPR
)
2846 value_range
*vr0p
= NULL
, *vr1p
= NULL
;
2847 if (range_int_cst_singleton_p (&vr1
))
2852 else if (range_int_cst_singleton_p (&vr0
))
2857 /* For op & or | attempt to optimize:
2858 [x, y] op z into [x op z, y op z]
2859 if z is a constant which (for op | its bitwise not) has n
2860 consecutive least significant bits cleared followed by m 1
2861 consecutive bits set immediately above it and either
2862 m + n == precision, or (x >> (m + n)) == (y >> (m + n)).
2863 The least significant n bits of all the values in the range are
2864 cleared or set, the m bits above it are preserved and any bits
2865 above these are required to be the same for all values in the
2867 if (vr0p
&& range_int_cst_p (vr0p
))
2869 wide_int w
= wi::to_wide (vr1p
->min
);
2871 if (code
== BIT_IOR_EXPR
)
2873 if (wi::eq_p (w
, 0))
2874 n
= TYPE_PRECISION (expr_type
);
2878 w
= ~(w
| wi::mask (n
, false, w
.get_precision ()));
2879 if (wi::eq_p (w
, 0))
2880 m
= TYPE_PRECISION (expr_type
) - n
;
2882 m
= wi::ctz (w
) - n
;
2884 wide_int mask
= wi::mask (m
+ n
, true, w
.get_precision ());
2885 if ((mask
& wi::to_wide (vr0p
->min
))
2886 == (mask
& wi::to_wide (vr0p
->max
)))
2888 min
= int_const_binop (code
, vr0p
->min
, vr1p
->min
);
2889 max
= int_const_binop (code
, vr0p
->max
, vr1p
->min
);
2896 /* Optimized above already. */;
2897 else if (code
== BIT_AND_EXPR
)
2899 min
= wide_int_to_tree (expr_type
,
2900 must_be_nonzero0
& must_be_nonzero1
);
2901 wide_int wmax
= may_be_nonzero0
& may_be_nonzero1
;
2902 /* If both input ranges contain only negative values we can
2903 truncate the result range maximum to the minimum of the
2904 input range maxima. */
2905 if (int_cst_range0
&& int_cst_range1
2906 && tree_int_cst_sgn (vr0
.max
) < 0
2907 && tree_int_cst_sgn (vr1
.max
) < 0)
2909 wmax
= wi::min (wmax
, wi::to_wide (vr0
.max
),
2910 TYPE_SIGN (expr_type
));
2911 wmax
= wi::min (wmax
, wi::to_wide (vr1
.max
),
2912 TYPE_SIGN (expr_type
));
2914 /* If either input range contains only non-negative values
2915 we can truncate the result range maximum to the respective
2916 maximum of the input range. */
2917 if (int_cst_range0
&& tree_int_cst_sgn (vr0
.min
) >= 0)
2918 wmax
= wi::min (wmax
, wi::to_wide (vr0
.max
),
2919 TYPE_SIGN (expr_type
));
2920 if (int_cst_range1
&& tree_int_cst_sgn (vr1
.min
) >= 0)
2921 wmax
= wi::min (wmax
, wi::to_wide (vr1
.max
),
2922 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
)
2934 && value_range_constant_singleton (&vr0
)
2935 && !wi::cmps (wi::to_wide (vr0
.min
), sign_bit
))
2937 && value_range_constant_singleton (&vr1
)
2938 && !wi::cmps (wi::to_wide (vr1
.min
), sign_bit
))))
2940 min
= TYPE_MIN_VALUE (expr_type
);
2941 max
= build_int_cst (expr_type
, 0);
2945 else if (code
== BIT_IOR_EXPR
)
2947 max
= wide_int_to_tree (expr_type
,
2948 may_be_nonzero0
| may_be_nonzero1
);
2949 wide_int wmin
= must_be_nonzero0
| must_be_nonzero1
;
2950 /* If the input ranges contain only positive values we can
2951 truncate the minimum of the result range to the maximum
2952 of the input range minima. */
2953 if (int_cst_range0
&& int_cst_range1
2954 && tree_int_cst_sgn (vr0
.min
) >= 0
2955 && tree_int_cst_sgn (vr1
.min
) >= 0)
2957 wmin
= wi::max (wmin
, wi::to_wide (vr0
.min
),
2958 TYPE_SIGN (expr_type
));
2959 wmin
= wi::max (wmin
, wi::to_wide (vr1
.min
),
2960 TYPE_SIGN (expr_type
));
2962 /* If either input range contains only negative values
2963 we can truncate the minimum of the result range to the
2964 respective minimum range. */
2965 if (int_cst_range0
&& tree_int_cst_sgn (vr0
.max
) < 0)
2966 wmin
= wi::max (wmin
, wi::to_wide (vr0
.min
),
2967 TYPE_SIGN (expr_type
));
2968 if (int_cst_range1
&& tree_int_cst_sgn (vr1
.max
) < 0)
2969 wmin
= wi::max (wmin
, wi::to_wide (vr1
.min
),
2970 TYPE_SIGN (expr_type
));
2971 min
= wide_int_to_tree (expr_type
, wmin
);
2973 else if (code
== BIT_XOR_EXPR
)
2975 wide_int result_zero_bits
= ((must_be_nonzero0
& must_be_nonzero1
)
2976 | ~(may_be_nonzero0
| may_be_nonzero1
));
2977 wide_int result_one_bits
2978 = (wi::bit_and_not (must_be_nonzero0
, may_be_nonzero1
)
2979 | wi::bit_and_not (must_be_nonzero1
, may_be_nonzero0
));
2980 max
= wide_int_to_tree (expr_type
, ~result_zero_bits
);
2981 min
= wide_int_to_tree (expr_type
, result_one_bits
);
2982 /* If the range has all positive or all negative values the
2983 result is better than VARYING. */
2984 if (tree_int_cst_sgn (min
) < 0
2985 || tree_int_cst_sgn (max
) >= 0)
2988 max
= min
= NULL_TREE
;
2994 /* If either MIN or MAX overflowed, then set the resulting range to
2996 if (min
== NULL_TREE
2997 || TREE_OVERFLOW_P (min
)
2999 || TREE_OVERFLOW_P (max
))
3001 set_value_range_to_varying (vr
);
3005 /* We punt for [-INF, +INF].
3006 We learn nothing when we have INF on both sides.
3007 Note that we do accept [-INF, -INF] and [+INF, +INF]. */
3008 if (vrp_val_is_min (min
) && vrp_val_is_max (max
))
3010 set_value_range_to_varying (vr
);
3014 cmp
= compare_values (min
, max
);
3015 if (cmp
== -2 || cmp
== 1)
3017 /* If the new range has its limits swapped around (MIN > MAX),
3018 then the operation caused one of them to wrap around, mark
3019 the new range VARYING. */
3020 set_value_range_to_varying (vr
);
3023 set_value_range (vr
, type
, min
, max
, NULL
);
3026 /* Extract range information from a binary expression OP0 CODE OP1 based on
3027 the ranges of each of its operands with resulting type EXPR_TYPE.
3028 The resulting range is stored in *VR. */
3031 extract_range_from_binary_expr (value_range
*vr
,
3032 enum tree_code code
,
3033 tree expr_type
, tree op0
, tree op1
)
3035 value_range vr0
= VR_INITIALIZER
;
3036 value_range vr1
= VR_INITIALIZER
;
3038 /* Get value ranges for each operand. For constant operands, create
3039 a new value range with the operand to simplify processing. */
3040 if (TREE_CODE (op0
) == SSA_NAME
)
3041 vr0
= *(get_value_range (op0
));
3042 else if (is_gimple_min_invariant (op0
))
3043 set_value_range_to_value (&vr0
, op0
, NULL
);
3045 set_value_range_to_varying (&vr0
);
3047 if (TREE_CODE (op1
) == SSA_NAME
)
3048 vr1
= *(get_value_range (op1
));
3049 else if (is_gimple_min_invariant (op1
))
3050 set_value_range_to_value (&vr1
, op1
, NULL
);
3052 set_value_range_to_varying (&vr1
);
3054 extract_range_from_binary_expr_1 (vr
, code
, expr_type
, &vr0
, &vr1
);
3056 /* Try harder for PLUS and MINUS if the range of one operand is symbolic
3057 and based on the other operand, for example if it was deduced from a
3058 symbolic comparison. When a bound of the range of the first operand
3059 is invariant, we set the corresponding bound of the new range to INF
3060 in order to avoid recursing on the range of the second operand. */
3061 if (vr
->type
== VR_VARYING
3062 && (code
== PLUS_EXPR
|| code
== MINUS_EXPR
)
3063 && TREE_CODE (op1
) == SSA_NAME
3064 && vr0
.type
== VR_RANGE
3065 && symbolic_range_based_on_p (&vr0
, op1
))
3067 const bool minus_p
= (code
== MINUS_EXPR
);
3068 value_range n_vr1
= VR_INITIALIZER
;
3070 /* Try with VR0 and [-INF, OP1]. */
3071 if (is_gimple_min_invariant (minus_p
? vr0
.max
: vr0
.min
))
3072 set_value_range (&n_vr1
, VR_RANGE
, vrp_val_min (expr_type
), op1
, NULL
);
3074 /* Try with VR0 and [OP1, +INF]. */
3075 else if (is_gimple_min_invariant (minus_p
? vr0
.min
: vr0
.max
))
3076 set_value_range (&n_vr1
, VR_RANGE
, op1
, vrp_val_max (expr_type
), NULL
);
3078 /* Try with VR0 and [OP1, OP1]. */
3080 set_value_range (&n_vr1
, VR_RANGE
, op1
, op1
, NULL
);
3082 extract_range_from_binary_expr_1 (vr
, code
, expr_type
, &vr0
, &n_vr1
);
3085 if (vr
->type
== VR_VARYING
3086 && (code
== PLUS_EXPR
|| code
== MINUS_EXPR
)
3087 && TREE_CODE (op0
) == SSA_NAME
3088 && vr1
.type
== VR_RANGE
3089 && symbolic_range_based_on_p (&vr1
, op0
))
3091 const bool minus_p
= (code
== MINUS_EXPR
);
3092 value_range n_vr0
= VR_INITIALIZER
;
3094 /* Try with [-INF, OP0] and VR1. */
3095 if (is_gimple_min_invariant (minus_p
? vr1
.max
: vr1
.min
))
3096 set_value_range (&n_vr0
, VR_RANGE
, vrp_val_min (expr_type
), op0
, NULL
);
3098 /* Try with [OP0, +INF] and VR1. */
3099 else if (is_gimple_min_invariant (minus_p
? vr1
.min
: vr1
.max
))
3100 set_value_range (&n_vr0
, VR_RANGE
, op0
, vrp_val_max (expr_type
), NULL
);
3102 /* Try with [OP0, OP0] and VR1. */
3104 set_value_range (&n_vr0
, VR_RANGE
, op0
, op0
, NULL
);
3106 extract_range_from_binary_expr_1 (vr
, code
, expr_type
, &n_vr0
, &vr1
);
3109 /* If we didn't derive a range for MINUS_EXPR, and
3110 op1's range is ~[op0,op0] or vice-versa, then we
3111 can derive a non-null range. This happens often for
3112 pointer subtraction. */
3113 if (vr
->type
== VR_VARYING
3114 && code
== MINUS_EXPR
3115 && TREE_CODE (op0
) == SSA_NAME
3116 && ((vr0
.type
== VR_ANTI_RANGE
3118 && vr0
.min
== vr0
.max
)
3119 || (vr1
.type
== VR_ANTI_RANGE
3121 && vr1
.min
== vr1
.max
)))
3122 set_value_range_to_nonnull (vr
, TREE_TYPE (op0
));
3125 /* Extract range information from a unary operation CODE based on
3126 the range of its operand *VR0 with type OP0_TYPE with resulting type TYPE.
3127 The resulting range is stored in *VR. */
3130 extract_range_from_unary_expr (value_range
*vr
,
3131 enum tree_code code
, tree type
,
3132 value_range
*vr0_
, tree op0_type
)
3134 value_range vr0
= *vr0_
, vrtem0
= VR_INITIALIZER
, vrtem1
= VR_INITIALIZER
;
3136 /* VRP only operates on integral and pointer types. */
3137 if (!(INTEGRAL_TYPE_P (op0_type
)
3138 || POINTER_TYPE_P (op0_type
))
3139 || !(INTEGRAL_TYPE_P (type
)
3140 || POINTER_TYPE_P (type
)))
3142 set_value_range_to_varying (vr
);
3146 /* If VR0 is UNDEFINED, so is the result. */
3147 if (vr0
.type
== VR_UNDEFINED
)
3149 set_value_range_to_undefined (vr
);
3153 /* Handle operations that we express in terms of others. */
3154 if (code
== PAREN_EXPR
|| code
== OBJ_TYPE_REF
)
3156 /* PAREN_EXPR and OBJ_TYPE_REF are simple copies. */
3157 copy_value_range (vr
, &vr0
);
3160 else if (code
== NEGATE_EXPR
)
3162 /* -X is simply 0 - X, so re-use existing code that also handles
3163 anti-ranges fine. */
3164 value_range zero
= VR_INITIALIZER
;
3165 set_value_range_to_value (&zero
, build_int_cst (type
, 0), NULL
);
3166 extract_range_from_binary_expr_1 (vr
, MINUS_EXPR
, type
, &zero
, &vr0
);
3169 else if (code
== BIT_NOT_EXPR
)
3171 /* ~X is simply -1 - X, so re-use existing code that also handles
3172 anti-ranges fine. */
3173 value_range minusone
= VR_INITIALIZER
;
3174 set_value_range_to_value (&minusone
, build_int_cst (type
, -1), NULL
);
3175 extract_range_from_binary_expr_1 (vr
, MINUS_EXPR
,
3176 type
, &minusone
, &vr0
);
3180 /* Now canonicalize anti-ranges to ranges when they are not symbolic
3181 and express op ~[] as (op []') U (op []''). */
3182 if (vr0
.type
== VR_ANTI_RANGE
3183 && ranges_from_anti_range (&vr0
, &vrtem0
, &vrtem1
))
3185 extract_range_from_unary_expr (vr
, code
, type
, &vrtem0
, op0_type
);
3186 if (vrtem1
.type
!= VR_UNDEFINED
)
3188 value_range vrres
= VR_INITIALIZER
;
3189 extract_range_from_unary_expr (&vrres
, code
, type
,
3191 vrp_meet (vr
, &vrres
);
3196 if (CONVERT_EXPR_CODE_P (code
))
3198 tree inner_type
= op0_type
;
3199 tree outer_type
= type
;
3201 /* If the expression evaluates to a pointer, we are only interested in
3202 determining if it evaluates to NULL [0, 0] or non-NULL (~[0, 0]). */
3203 if (POINTER_TYPE_P (type
))
3205 if (range_is_nonnull (&vr0
))
3206 set_value_range_to_nonnull (vr
, type
);
3207 else if (range_is_null (&vr0
))
3208 set_value_range_to_null (vr
, type
);
3210 set_value_range_to_varying (vr
);
3214 /* If VR0 is varying and we increase the type precision, assume
3215 a full range for the following transformation. */
3216 if (vr0
.type
== VR_VARYING
3217 && INTEGRAL_TYPE_P (inner_type
)
3218 && TYPE_PRECISION (inner_type
) < TYPE_PRECISION (outer_type
))
3220 vr0
.type
= VR_RANGE
;
3221 vr0
.min
= TYPE_MIN_VALUE (inner_type
);
3222 vr0
.max
= TYPE_MAX_VALUE (inner_type
);
3225 /* If VR0 is a constant range or anti-range and the conversion is
3226 not truncating we can convert the min and max values and
3227 canonicalize the resulting range. Otherwise we can do the
3228 conversion if the size of the range is less than what the
3229 precision of the target type can represent and the range is
3230 not an anti-range. */
3231 if ((vr0
.type
== VR_RANGE
3232 || vr0
.type
== VR_ANTI_RANGE
)
3233 && TREE_CODE (vr0
.min
) == INTEGER_CST
3234 && TREE_CODE (vr0
.max
) == INTEGER_CST
3235 && (TYPE_PRECISION (outer_type
) >= TYPE_PRECISION (inner_type
)
3236 || (vr0
.type
== VR_RANGE
3237 && integer_zerop (int_const_binop (RSHIFT_EXPR
,
3238 int_const_binop (MINUS_EXPR
, vr0
.max
, vr0
.min
),
3239 size_int (TYPE_PRECISION (outer_type
)))))))
3241 tree new_min
, new_max
;
3242 new_min
= force_fit_type (outer_type
, wi::to_widest (vr0
.min
),
3244 new_max
= force_fit_type (outer_type
, wi::to_widest (vr0
.max
),
3246 set_and_canonicalize_value_range (vr
, vr0
.type
,
3247 new_min
, new_max
, NULL
);
3251 set_value_range_to_varying (vr
);
3254 else if (code
== ABS_EXPR
)
3259 /* Pass through vr0 in the easy cases. */
3260 if (TYPE_UNSIGNED (type
)
3261 || value_range_nonnegative_p (&vr0
))
3263 copy_value_range (vr
, &vr0
);
3267 /* For the remaining varying or symbolic ranges we can't do anything
3269 if (vr0
.type
== VR_VARYING
3270 || symbolic_range_p (&vr0
))
3272 set_value_range_to_varying (vr
);
3276 /* -TYPE_MIN_VALUE = TYPE_MIN_VALUE with flag_wrapv so we can't get a
3278 if (!TYPE_OVERFLOW_UNDEFINED (type
)
3279 && ((vr0
.type
== VR_RANGE
3280 && vrp_val_is_min (vr0
.min
))
3281 || (vr0
.type
== VR_ANTI_RANGE
3282 && !vrp_val_is_min (vr0
.min
))))
3284 set_value_range_to_varying (vr
);
3288 /* ABS_EXPR may flip the range around, if the original range
3289 included negative values. */
3290 if (!vrp_val_is_min (vr0
.min
))
3291 min
= fold_unary_to_constant (code
, type
, vr0
.min
);
3293 min
= TYPE_MAX_VALUE (type
);
3295 if (!vrp_val_is_min (vr0
.max
))
3296 max
= fold_unary_to_constant (code
, type
, vr0
.max
);
3298 max
= TYPE_MAX_VALUE (type
);
3300 cmp
= compare_values (min
, max
);
3302 /* If a VR_ANTI_RANGEs contains zero, then we have
3303 ~[-INF, min(MIN, MAX)]. */
3304 if (vr0
.type
== VR_ANTI_RANGE
)
3306 if (range_includes_zero_p (vr0
.min
, vr0
.max
) == 1)
3308 /* Take the lower of the two values. */
3312 /* Create ~[-INF, min (abs(MIN), abs(MAX))]
3313 or ~[-INF + 1, min (abs(MIN), abs(MAX))] when
3314 flag_wrapv is set and the original anti-range doesn't include
3315 TYPE_MIN_VALUE, remember -TYPE_MIN_VALUE = TYPE_MIN_VALUE. */
3316 if (TYPE_OVERFLOW_WRAPS (type
))
3318 tree type_min_value
= TYPE_MIN_VALUE (type
);
3320 min
= (vr0
.min
!= type_min_value
3321 ? int_const_binop (PLUS_EXPR
, type_min_value
,
3322 build_int_cst (TREE_TYPE (type_min_value
), 1))
3326 min
= TYPE_MIN_VALUE (type
);
3330 /* All else has failed, so create the range [0, INF], even for
3331 flag_wrapv since TYPE_MIN_VALUE is in the original
3333 vr0
.type
= VR_RANGE
;
3334 min
= build_int_cst (type
, 0);
3335 max
= TYPE_MAX_VALUE (type
);
3339 /* If the range contains zero then we know that the minimum value in the
3340 range will be zero. */
3341 else if (range_includes_zero_p (vr0
.min
, vr0
.max
) == 1)
3345 min
= build_int_cst (type
, 0);
3349 /* If the range was reversed, swap MIN and MAX. */
3351 std::swap (min
, max
);
3354 cmp
= compare_values (min
, max
);
3355 if (cmp
== -2 || cmp
== 1)
3357 /* If the new range has its limits swapped around (MIN > MAX),
3358 then the operation caused one of them to wrap around, mark
3359 the new range VARYING. */
3360 set_value_range_to_varying (vr
);
3363 set_value_range (vr
, vr0
.type
, min
, max
, NULL
);
3367 /* For unhandled operations fall back to varying. */
3368 set_value_range_to_varying (vr
);
3373 /* Extract range information from a unary expression CODE OP0 based on
3374 the range of its operand with resulting type TYPE.
3375 The resulting range is stored in *VR. */
3378 extract_range_from_unary_expr (value_range
*vr
, enum tree_code code
,
3379 tree type
, tree op0
)
3381 value_range vr0
= VR_INITIALIZER
;
3383 /* Get value ranges for the operand. For constant operands, create
3384 a new value range with the operand to simplify processing. */
3385 if (TREE_CODE (op0
) == SSA_NAME
)
3386 vr0
= *(get_value_range (op0
));
3387 else if (is_gimple_min_invariant (op0
))
3388 set_value_range_to_value (&vr0
, op0
, NULL
);
3390 set_value_range_to_varying (&vr0
);
3392 extract_range_from_unary_expr (vr
, code
, type
, &vr0
, TREE_TYPE (op0
));
3396 /* Extract range information from a conditional expression STMT based on
3397 the ranges of each of its operands and the expression code. */
3400 extract_range_from_cond_expr (value_range
*vr
, gassign
*stmt
)
3403 value_range vr0
= VR_INITIALIZER
;
3404 value_range vr1
= VR_INITIALIZER
;
3406 /* Get value ranges for each operand. For constant operands, create
3407 a new value range with the operand to simplify processing. */
3408 op0
= gimple_assign_rhs2 (stmt
);
3409 if (TREE_CODE (op0
) == SSA_NAME
)
3410 vr0
= *(get_value_range (op0
));
3411 else if (is_gimple_min_invariant (op0
))
3412 set_value_range_to_value (&vr0
, op0
, NULL
);
3414 set_value_range_to_varying (&vr0
);
3416 op1
= gimple_assign_rhs3 (stmt
);
3417 if (TREE_CODE (op1
) == SSA_NAME
)
3418 vr1
= *(get_value_range (op1
));
3419 else if (is_gimple_min_invariant (op1
))
3420 set_value_range_to_value (&vr1
, op1
, NULL
);
3422 set_value_range_to_varying (&vr1
);
3424 /* The resulting value range is the union of the operand ranges */
3425 copy_value_range (vr
, &vr0
);
3426 vrp_meet (vr
, &vr1
);
3430 /* Extract range information from a comparison expression EXPR based
3431 on the range of its operand and the expression code. */
3434 extract_range_from_comparison (value_range
*vr
, enum tree_code code
,
3435 tree type
, tree op0
, tree op1
)
3440 val
= vrp_evaluate_conditional_warnv_with_ops (code
, op0
, op1
, false, &sop
,
3444 /* Since this expression was found on the RHS of an assignment,
3445 its type may be different from _Bool. Convert VAL to EXPR's
3447 val
= fold_convert (type
, val
);
3448 if (is_gimple_min_invariant (val
))
3449 set_value_range_to_value (vr
, val
, vr
->equiv
);
3451 set_value_range (vr
, VR_RANGE
, val
, val
, vr
->equiv
);
3454 /* The result of a comparison is always true or false. */
3455 set_value_range_to_truthvalue (vr
, type
);
3458 /* Helper function for simplify_internal_call_using_ranges and
3459 extract_range_basic. Return true if OP0 SUBCODE OP1 for
3460 SUBCODE {PLUS,MINUS,MULT}_EXPR is known to never overflow or
3461 always overflow. Set *OVF to true if it is known to always
3465 check_for_binary_op_overflow (enum tree_code subcode
, tree type
,
3466 tree op0
, tree op1
, bool *ovf
)
3468 value_range vr0
= VR_INITIALIZER
;
3469 value_range vr1
= VR_INITIALIZER
;
3470 if (TREE_CODE (op0
) == SSA_NAME
)
3471 vr0
= *get_value_range (op0
);
3472 else if (TREE_CODE (op0
) == INTEGER_CST
)
3473 set_value_range_to_value (&vr0
, op0
, NULL
);
3475 set_value_range_to_varying (&vr0
);
3477 if (TREE_CODE (op1
) == SSA_NAME
)
3478 vr1
= *get_value_range (op1
);
3479 else if (TREE_CODE (op1
) == INTEGER_CST
)
3480 set_value_range_to_value (&vr1
, op1
, NULL
);
3482 set_value_range_to_varying (&vr1
);
3484 if (!range_int_cst_p (&vr0
)
3485 || TREE_OVERFLOW (vr0
.min
)
3486 || TREE_OVERFLOW (vr0
.max
))
3488 vr0
.min
= vrp_val_min (TREE_TYPE (op0
));
3489 vr0
.max
= vrp_val_max (TREE_TYPE (op0
));
3491 if (!range_int_cst_p (&vr1
)
3492 || TREE_OVERFLOW (vr1
.min
)
3493 || TREE_OVERFLOW (vr1
.max
))
3495 vr1
.min
= vrp_val_min (TREE_TYPE (op1
));
3496 vr1
.max
= vrp_val_max (TREE_TYPE (op1
));
3498 *ovf
= arith_overflowed_p (subcode
, type
, vr0
.min
,
3499 subcode
== MINUS_EXPR
? vr1
.max
: vr1
.min
);
3500 if (arith_overflowed_p (subcode
, type
, vr0
.max
,
3501 subcode
== MINUS_EXPR
? vr1
.min
: vr1
.max
) != *ovf
)
3503 if (subcode
== MULT_EXPR
)
3505 if (arith_overflowed_p (subcode
, type
, vr0
.min
, vr1
.max
) != *ovf
3506 || arith_overflowed_p (subcode
, type
, vr0
.max
, vr1
.min
) != *ovf
)
3511 /* So far we found that there is an overflow on the boundaries.
3512 That doesn't prove that there is an overflow even for all values
3513 in between the boundaries. For that compute widest_int range
3514 of the result and see if it doesn't overlap the range of
3516 widest_int wmin
, wmax
;
3519 w
[0] = wi::to_widest (vr0
.min
);
3520 w
[1] = wi::to_widest (vr0
.max
);
3521 w
[2] = wi::to_widest (vr1
.min
);
3522 w
[3] = wi::to_widest (vr1
.max
);
3523 for (i
= 0; i
< 4; i
++)
3529 wt
= wi::add (w
[i
& 1], w
[2 + (i
& 2) / 2]);
3532 wt
= wi::sub (w
[i
& 1], w
[2 + (i
& 2) / 2]);
3535 wt
= wi::mul (w
[i
& 1], w
[2 + (i
& 2) / 2]);
3547 wmin
= wi::smin (wmin
, wt
);
3548 wmax
= wi::smax (wmax
, wt
);
3551 /* The result of op0 CODE op1 is known to be in range
3553 widest_int wtmin
= wi::to_widest (vrp_val_min (type
));
3554 widest_int wtmax
= wi::to_widest (vrp_val_max (type
));
3555 /* If all values in [wmin, wmax] are smaller than
3556 [wtmin, wtmax] or all are larger than [wtmin, wtmax],
3557 the arithmetic operation will always overflow. */
3558 if (wmax
< wtmin
|| wmin
> wtmax
)
3565 /* Try to derive a nonnegative or nonzero range out of STMT relying
3566 primarily on generic routines in fold in conjunction with range data.
3567 Store the result in *VR */
3570 extract_range_basic (value_range
*vr
, gimple
*stmt
)
3573 tree type
= gimple_expr_type (stmt
);
3575 if (is_gimple_call (stmt
))
3578 int mini
, maxi
, zerov
= 0, prec
;
3579 enum tree_code subcode
= ERROR_MARK
;
3580 combined_fn cfn
= gimple_call_combined_fn (stmt
);
3581 scalar_int_mode mode
;
3585 case CFN_BUILT_IN_CONSTANT_P
:
3586 /* If the call is __builtin_constant_p and the argument is a
3587 function parameter resolve it to false. This avoids bogus
3588 array bound warnings.
3589 ??? We could do this as early as inlining is finished. */
3590 arg
= gimple_call_arg (stmt
, 0);
3591 if (TREE_CODE (arg
) == SSA_NAME
3592 && SSA_NAME_IS_DEFAULT_DEF (arg
)
3593 && TREE_CODE (SSA_NAME_VAR (arg
)) == PARM_DECL
3594 && cfun
->after_inlining
)
3596 set_value_range_to_null (vr
, type
);
3600 /* Both __builtin_ffs* and __builtin_popcount return
3604 arg
= gimple_call_arg (stmt
, 0);
3605 prec
= TYPE_PRECISION (TREE_TYPE (arg
));
3608 if (TREE_CODE (arg
) == SSA_NAME
)
3610 value_range
*vr0
= get_value_range (arg
);
3611 /* If arg is non-zero, then ffs or popcount
3613 if ((vr0
->type
== VR_RANGE
3614 && range_includes_zero_p (vr0
->min
, vr0
->max
) == 0)
3615 || (vr0
->type
== VR_ANTI_RANGE
3616 && range_includes_zero_p (vr0
->min
, vr0
->max
) == 1))
3618 /* If some high bits are known to be zero,
3619 we can decrease the maximum. */
3620 if (vr0
->type
== VR_RANGE
3621 && TREE_CODE (vr0
->max
) == INTEGER_CST
3622 && !operand_less_p (vr0
->min
,
3623 build_zero_cst (TREE_TYPE (vr0
->min
))))
3624 maxi
= tree_floor_log2 (vr0
->max
) + 1;
3627 /* __builtin_parity* returns [0, 1]. */
3632 /* __builtin_c[lt]z* return [0, prec-1], except for
3633 when the argument is 0, but that is undefined behavior.
3634 On many targets where the CLZ RTL or optab value is defined
3635 for 0 the value is prec, so include that in the range
3638 arg
= gimple_call_arg (stmt
, 0);
3639 prec
= TYPE_PRECISION (TREE_TYPE (arg
));
3642 mode
= SCALAR_INT_TYPE_MODE (TREE_TYPE (arg
));
3643 if (optab_handler (clz_optab
, mode
) != CODE_FOR_nothing
3644 && CLZ_DEFINED_VALUE_AT_ZERO (mode
, zerov
)
3645 /* Handle only the single common value. */
3647 /* Magic value to give up, unless vr0 proves
3650 if (TREE_CODE (arg
) == SSA_NAME
)
3652 value_range
*vr0
= get_value_range (arg
);
3653 /* From clz of VR_RANGE minimum we can compute
3655 if (vr0
->type
== VR_RANGE
3656 && TREE_CODE (vr0
->min
) == INTEGER_CST
)
3658 maxi
= prec
- 1 - tree_floor_log2 (vr0
->min
);
3662 else if (vr0
->type
== VR_ANTI_RANGE
3663 && integer_zerop (vr0
->min
))
3670 /* From clz of VR_RANGE maximum we can compute
3672 if (vr0
->type
== VR_RANGE
3673 && TREE_CODE (vr0
->max
) == INTEGER_CST
)
3675 mini
= prec
- 1 - tree_floor_log2 (vr0
->max
);
3683 /* __builtin_ctz* return [0, prec-1], except for
3684 when the argument is 0, but that is undefined behavior.
3685 If there is a ctz optab for this mode and
3686 CTZ_DEFINED_VALUE_AT_ZERO, include that in the range,
3687 otherwise just assume 0 won't be seen. */
3689 arg
= gimple_call_arg (stmt
, 0);
3690 prec
= TYPE_PRECISION (TREE_TYPE (arg
));
3693 mode
= SCALAR_INT_TYPE_MODE (TREE_TYPE (arg
));
3694 if (optab_handler (ctz_optab
, mode
) != CODE_FOR_nothing
3695 && CTZ_DEFINED_VALUE_AT_ZERO (mode
, zerov
))
3697 /* Handle only the two common values. */
3700 else if (zerov
== prec
)
3703 /* Magic value to give up, unless vr0 proves
3707 if (TREE_CODE (arg
) == SSA_NAME
)
3709 value_range
*vr0
= get_value_range (arg
);
3710 /* If arg is non-zero, then use [0, prec - 1]. */
3711 if ((vr0
->type
== VR_RANGE
3712 && integer_nonzerop (vr0
->min
))
3713 || (vr0
->type
== VR_ANTI_RANGE
3714 && integer_zerop (vr0
->min
)))
3719 /* If some high bits are known to be zero,
3720 we can decrease the result maximum. */
3721 if (vr0
->type
== VR_RANGE
3722 && TREE_CODE (vr0
->max
) == INTEGER_CST
)
3724 maxi
= tree_floor_log2 (vr0
->max
);
3725 /* For vr0 [0, 0] give up. */
3733 /* __builtin_clrsb* returns [0, prec-1]. */
3735 arg
= gimple_call_arg (stmt
, 0);
3736 prec
= TYPE_PRECISION (TREE_TYPE (arg
));
3741 set_value_range (vr
, VR_RANGE
, build_int_cst (type
, mini
),
3742 build_int_cst (type
, maxi
), NULL
);
3744 case CFN_UBSAN_CHECK_ADD
:
3745 subcode
= PLUS_EXPR
;
3747 case CFN_UBSAN_CHECK_SUB
:
3748 subcode
= MINUS_EXPR
;
3750 case CFN_UBSAN_CHECK_MUL
:
3751 subcode
= MULT_EXPR
;
3753 case CFN_GOACC_DIM_SIZE
:
3754 case CFN_GOACC_DIM_POS
:
3755 /* Optimizing these two internal functions helps the loop
3756 optimizer eliminate outer comparisons. Size is [1,N]
3757 and pos is [0,N-1]. */
3759 bool is_pos
= cfn
== CFN_GOACC_DIM_POS
;
3760 int axis
= oacc_get_ifn_dim_arg (stmt
);
3761 int size
= oacc_get_fn_dim_size (current_function_decl
, axis
);
3764 /* If it's dynamic, the backend might know a hardware
3766 size
= targetm
.goacc
.dim_limit (axis
);
3768 tree type
= TREE_TYPE (gimple_call_lhs (stmt
));
3769 set_value_range (vr
, VR_RANGE
,
3770 build_int_cst (type
, is_pos
? 0 : 1),
3771 size
? build_int_cst (type
, size
- is_pos
)
3772 : vrp_val_max (type
), NULL
);
3775 case CFN_BUILT_IN_STRLEN
:
3776 if (tree lhs
= gimple_call_lhs (stmt
))
3777 if (ptrdiff_type_node
3778 && (TYPE_PRECISION (ptrdiff_type_node
)
3779 == TYPE_PRECISION (TREE_TYPE (lhs
))))
3781 tree type
= TREE_TYPE (lhs
);
3782 tree max
= vrp_val_max (ptrdiff_type_node
);
3783 wide_int wmax
= wi::to_wide (max
, TYPE_PRECISION (TREE_TYPE (max
)));
3784 tree range_min
= build_zero_cst (type
);
3785 tree range_max
= wide_int_to_tree (type
, wmax
- 1);
3786 set_value_range (vr
, VR_RANGE
, range_min
, range_max
, NULL
);
3793 if (subcode
!= ERROR_MARK
)
3795 bool saved_flag_wrapv
= flag_wrapv
;
3796 /* Pretend the arithmetics is wrapping. If there is
3797 any overflow, we'll complain, but will actually do
3798 wrapping operation. */
3800 extract_range_from_binary_expr (vr
, subcode
, type
,
3801 gimple_call_arg (stmt
, 0),
3802 gimple_call_arg (stmt
, 1));
3803 flag_wrapv
= saved_flag_wrapv
;
3805 /* If for both arguments vrp_valueize returned non-NULL,
3806 this should have been already folded and if not, it
3807 wasn't folded because of overflow. Avoid removing the
3808 UBSAN_CHECK_* calls in that case. */
3809 if (vr
->type
== VR_RANGE
3810 && (vr
->min
== vr
->max
3811 || operand_equal_p (vr
->min
, vr
->max
, 0)))
3812 set_value_range_to_varying (vr
);
3816 /* Handle extraction of the two results (result of arithmetics and
3817 a flag whether arithmetics overflowed) from {ADD,SUB,MUL}_OVERFLOW
3818 internal function. Similarly from ATOMIC_COMPARE_EXCHANGE. */
3819 else if (is_gimple_assign (stmt
)
3820 && (gimple_assign_rhs_code (stmt
) == REALPART_EXPR
3821 || gimple_assign_rhs_code (stmt
) == IMAGPART_EXPR
)
3822 && INTEGRAL_TYPE_P (type
))
3824 enum tree_code code
= gimple_assign_rhs_code (stmt
);
3825 tree op
= gimple_assign_rhs1 (stmt
);
3826 if (TREE_CODE (op
) == code
&& TREE_CODE (TREE_OPERAND (op
, 0)) == SSA_NAME
)
3828 gimple
*g
= SSA_NAME_DEF_STMT (TREE_OPERAND (op
, 0));
3829 if (is_gimple_call (g
) && gimple_call_internal_p (g
))
3831 enum tree_code subcode
= ERROR_MARK
;
3832 switch (gimple_call_internal_fn (g
))
3834 case IFN_ADD_OVERFLOW
:
3835 subcode
= PLUS_EXPR
;
3837 case IFN_SUB_OVERFLOW
:
3838 subcode
= MINUS_EXPR
;
3840 case IFN_MUL_OVERFLOW
:
3841 subcode
= MULT_EXPR
;
3843 case IFN_ATOMIC_COMPARE_EXCHANGE
:
3844 if (code
== IMAGPART_EXPR
)
3846 /* This is the boolean return value whether compare and
3847 exchange changed anything or not. */
3848 set_value_range (vr
, VR_RANGE
, build_int_cst (type
, 0),
3849 build_int_cst (type
, 1), NULL
);
3856 if (subcode
!= ERROR_MARK
)
3858 tree op0
= gimple_call_arg (g
, 0);
3859 tree op1
= gimple_call_arg (g
, 1);
3860 if (code
== IMAGPART_EXPR
)
3863 if (check_for_binary_op_overflow (subcode
, type
,
3865 set_value_range_to_value (vr
,
3866 build_int_cst (type
, ovf
),
3868 else if (TYPE_PRECISION (type
) == 1
3869 && !TYPE_UNSIGNED (type
))
3870 set_value_range_to_varying (vr
);
3872 set_value_range (vr
, VR_RANGE
, build_int_cst (type
, 0),
3873 build_int_cst (type
, 1), NULL
);
3875 else if (types_compatible_p (type
, TREE_TYPE (op0
))
3876 && types_compatible_p (type
, TREE_TYPE (op1
)))
3878 bool saved_flag_wrapv
= flag_wrapv
;
3879 /* Pretend the arithmetics is wrapping. If there is
3880 any overflow, IMAGPART_EXPR will be set. */
3882 extract_range_from_binary_expr (vr
, subcode
, type
,
3884 flag_wrapv
= saved_flag_wrapv
;
3888 value_range vr0
= VR_INITIALIZER
;
3889 value_range vr1
= VR_INITIALIZER
;
3890 bool saved_flag_wrapv
= flag_wrapv
;
3891 /* Pretend the arithmetics is wrapping. If there is
3892 any overflow, IMAGPART_EXPR will be set. */
3894 extract_range_from_unary_expr (&vr0
, NOP_EXPR
,
3896 extract_range_from_unary_expr (&vr1
, NOP_EXPR
,
3898 extract_range_from_binary_expr_1 (vr
, subcode
, type
,
3900 flag_wrapv
= saved_flag_wrapv
;
3907 if (INTEGRAL_TYPE_P (type
)
3908 && gimple_stmt_nonnegative_warnv_p (stmt
, &sop
))
3909 set_value_range_to_nonnegative (vr
, type
);
3910 else if (vrp_stmt_computes_nonzero (stmt
))
3911 set_value_range_to_nonnull (vr
, type
);
3913 set_value_range_to_varying (vr
);
3917 /* Try to compute a useful range out of assignment STMT and store it
3921 extract_range_from_assignment (value_range
*vr
, gassign
*stmt
)
3923 enum tree_code code
= gimple_assign_rhs_code (stmt
);
3925 if (code
== ASSERT_EXPR
)
3926 extract_range_from_assert (vr
, gimple_assign_rhs1 (stmt
));
3927 else if (code
== SSA_NAME
)
3928 extract_range_from_ssa_name (vr
, gimple_assign_rhs1 (stmt
));
3929 else if (TREE_CODE_CLASS (code
) == tcc_binary
)
3930 extract_range_from_binary_expr (vr
, gimple_assign_rhs_code (stmt
),
3931 gimple_expr_type (stmt
),
3932 gimple_assign_rhs1 (stmt
),
3933 gimple_assign_rhs2 (stmt
));
3934 else if (TREE_CODE_CLASS (code
) == tcc_unary
)
3935 extract_range_from_unary_expr (vr
, gimple_assign_rhs_code (stmt
),
3936 gimple_expr_type (stmt
),
3937 gimple_assign_rhs1 (stmt
));
3938 else if (code
== COND_EXPR
)
3939 extract_range_from_cond_expr (vr
, stmt
);
3940 else if (TREE_CODE_CLASS (code
) == tcc_comparison
)
3941 extract_range_from_comparison (vr
, gimple_assign_rhs_code (stmt
),
3942 gimple_expr_type (stmt
),
3943 gimple_assign_rhs1 (stmt
),
3944 gimple_assign_rhs2 (stmt
));
3945 else if (get_gimple_rhs_class (code
) == GIMPLE_SINGLE_RHS
3946 && is_gimple_min_invariant (gimple_assign_rhs1 (stmt
)))
3947 set_value_range_to_value (vr
, gimple_assign_rhs1 (stmt
), NULL
);
3949 set_value_range_to_varying (vr
);
3951 if (vr
->type
== VR_VARYING
)
3952 extract_range_basic (vr
, stmt
);
3955 /* Given a range VR, a LOOP and a variable VAR, determine whether it
3956 would be profitable to adjust VR using scalar evolution information
3957 for VAR. If so, update VR with the new limits. */
3960 adjust_range_with_scev (value_range
*vr
, struct loop
*loop
,
3961 gimple
*stmt
, tree var
)
3963 tree init
, step
, chrec
, tmin
, tmax
, min
, max
, type
, tem
;
3964 enum ev_direction dir
;
3966 /* TODO. Don't adjust anti-ranges. An anti-range may provide
3967 better opportunities than a regular range, but I'm not sure. */
3968 if (vr
->type
== VR_ANTI_RANGE
)
3971 chrec
= instantiate_parameters (loop
, analyze_scalar_evolution (loop
, var
));
3973 /* Like in PR19590, scev can return a constant function. */
3974 if (is_gimple_min_invariant (chrec
))
3976 set_value_range_to_value (vr
, chrec
, vr
->equiv
);
3980 if (TREE_CODE (chrec
) != POLYNOMIAL_CHREC
)
3983 init
= initial_condition_in_loop_num (chrec
, loop
->num
);
3984 tem
= op_with_constant_singleton_value_range (init
);
3987 step
= evolution_part_in_loop_num (chrec
, loop
->num
);
3988 tem
= op_with_constant_singleton_value_range (step
);
3992 /* If STEP is symbolic, we can't know whether INIT will be the
3993 minimum or maximum value in the range. Also, unless INIT is
3994 a simple expression, compare_values and possibly other functions
3995 in tree-vrp won't be able to handle it. */
3996 if (step
== NULL_TREE
3997 || !is_gimple_min_invariant (step
)
3998 || !valid_value_p (init
))
4001 dir
= scev_direction (chrec
);
4002 if (/* Do not adjust ranges if we do not know whether the iv increases
4003 or decreases, ... */
4004 dir
== EV_DIR_UNKNOWN
4005 /* ... or if it may wrap. */
4006 || scev_probably_wraps_p (NULL_TREE
, init
, step
, stmt
,
4007 get_chrec_loop (chrec
), true))
4010 type
= TREE_TYPE (var
);
4011 if (POINTER_TYPE_P (type
) || !TYPE_MIN_VALUE (type
))
4012 tmin
= lower_bound_in_type (type
, type
);
4014 tmin
= TYPE_MIN_VALUE (type
);
4015 if (POINTER_TYPE_P (type
) || !TYPE_MAX_VALUE (type
))
4016 tmax
= upper_bound_in_type (type
, type
);
4018 tmax
= TYPE_MAX_VALUE (type
);
4020 /* Try to use estimated number of iterations for the loop to constrain the
4021 final value in the evolution. */
4022 if (TREE_CODE (step
) == INTEGER_CST
4023 && is_gimple_val (init
)
4024 && (TREE_CODE (init
) != SSA_NAME
4025 || get_value_range (init
)->type
== VR_RANGE
))
4029 /* We are only entering here for loop header PHI nodes, so using
4030 the number of latch executions is the correct thing to use. */
4031 if (max_loop_iterations (loop
, &nit
))
4033 value_range maxvr
= VR_INITIALIZER
;
4034 signop sgn
= TYPE_SIGN (TREE_TYPE (step
));
4037 widest_int wtmp
= wi::mul (wi::to_widest (step
), nit
, sgn
,
4039 /* If the multiplication overflowed we can't do a meaningful
4040 adjustment. Likewise if the result doesn't fit in the type
4041 of the induction variable. For a signed type we have to
4042 check whether the result has the expected signedness which
4043 is that of the step as number of iterations is unsigned. */
4045 && wi::fits_to_tree_p (wtmp
, TREE_TYPE (init
))
4047 || wi::gts_p (wtmp
, 0) == wi::gts_p (wi::to_wide (step
), 0)))
4049 tem
= wide_int_to_tree (TREE_TYPE (init
), wtmp
);
4050 extract_range_from_binary_expr (&maxvr
, PLUS_EXPR
,
4051 TREE_TYPE (init
), init
, tem
);
4052 /* Likewise if the addition did. */
4053 if (maxvr
.type
== VR_RANGE
)
4055 value_range initvr
= VR_INITIALIZER
;
4057 if (TREE_CODE (init
) == SSA_NAME
)
4058 initvr
= *(get_value_range (init
));
4059 else if (is_gimple_min_invariant (init
))
4060 set_value_range_to_value (&initvr
, init
, NULL
);
4064 /* Check if init + nit * step overflows. Though we checked
4065 scev {init, step}_loop doesn't wrap, it is not enough
4066 because the loop may exit immediately. Overflow could
4067 happen in the plus expression in this case. */
4068 if ((dir
== EV_DIR_DECREASES
4069 && compare_values (maxvr
.min
, initvr
.min
) != -1)
4070 || (dir
== EV_DIR_GROWS
4071 && compare_values (maxvr
.max
, initvr
.max
) != 1))
4081 if (vr
->type
== VR_VARYING
|| vr
->type
== VR_UNDEFINED
)
4086 /* For VARYING or UNDEFINED ranges, just about anything we get
4087 from scalar evolutions should be better. */
4089 if (dir
== EV_DIR_DECREASES
)
4094 else if (vr
->type
== VR_RANGE
)
4099 if (dir
== EV_DIR_DECREASES
)
4101 /* INIT is the maximum value. If INIT is lower than VR->MAX
4102 but no smaller than VR->MIN, set VR->MAX to INIT. */
4103 if (compare_values (init
, max
) == -1)
4106 /* According to the loop information, the variable does not
4108 if (compare_values (min
, tmin
) == -1)
4114 /* If INIT is bigger than VR->MIN, set VR->MIN to INIT. */
4115 if (compare_values (init
, min
) == 1)
4118 if (compare_values (tmax
, max
) == -1)
4125 /* If we just created an invalid range with the minimum
4126 greater than the maximum, we fail conservatively.
4127 This should happen only in unreachable
4128 parts of code, or for invalid programs. */
4129 if (compare_values (min
, max
) == 1)
4132 /* Even for valid range info, sometimes overflow flag will leak in.
4133 As GIMPLE IL should have no constants with TREE_OVERFLOW set, we
4135 if (TREE_OVERFLOW_P (min
))
4136 min
= drop_tree_overflow (min
);
4137 if (TREE_OVERFLOW_P (max
))
4138 max
= drop_tree_overflow (max
);
4140 set_value_range (vr
, VR_RANGE
, min
, max
, vr
->equiv
);
4144 /* Given two numeric value ranges VR0, VR1 and a comparison code COMP:
4146 - Return BOOLEAN_TRUE_NODE if VR0 COMP VR1 always returns true for
4147 all the values in the ranges.
4149 - Return BOOLEAN_FALSE_NODE if the comparison always returns false.
4151 - Return NULL_TREE if it is not always possible to determine the
4152 value of the comparison.
4154 Also set *STRICT_OVERFLOW_P to indicate whether comparision evaluation
4155 assumed signed overflow is undefined. */
4159 compare_ranges (enum tree_code comp
, value_range
*vr0
, value_range
*vr1
,
4160 bool *strict_overflow_p
)
4162 /* VARYING or UNDEFINED ranges cannot be compared. */
4163 if (vr0
->type
== VR_VARYING
4164 || vr0
->type
== VR_UNDEFINED
4165 || vr1
->type
== VR_VARYING
4166 || vr1
->type
== VR_UNDEFINED
)
4169 /* Anti-ranges need to be handled separately. */
4170 if (vr0
->type
== VR_ANTI_RANGE
|| vr1
->type
== VR_ANTI_RANGE
)
4172 /* If both are anti-ranges, then we cannot compute any
4174 if (vr0
->type
== VR_ANTI_RANGE
&& vr1
->type
== VR_ANTI_RANGE
)
4177 /* These comparisons are never statically computable. */
4184 /* Equality can be computed only between a range and an
4185 anti-range. ~[VAL1, VAL2] == [VAL1, VAL2] is always false. */
4186 if (vr0
->type
== VR_RANGE
)
4188 /* To simplify processing, make VR0 the anti-range. */
4189 value_range
*tmp
= vr0
;
4194 gcc_assert (comp
== NE_EXPR
|| comp
== EQ_EXPR
);
4196 if (compare_values_warnv (vr0
->min
, vr1
->min
, strict_overflow_p
) == 0
4197 && compare_values_warnv (vr0
->max
, vr1
->max
, strict_overflow_p
) == 0)
4198 return (comp
== NE_EXPR
) ? boolean_true_node
: boolean_false_node
;
4203 /* Simplify processing. If COMP is GT_EXPR or GE_EXPR, switch the
4204 operands around and change the comparison code. */
4205 if (comp
== GT_EXPR
|| comp
== GE_EXPR
)
4207 comp
= (comp
== GT_EXPR
) ? LT_EXPR
: LE_EXPR
;
4208 std::swap (vr0
, vr1
);
4211 if (comp
== EQ_EXPR
)
4213 /* Equality may only be computed if both ranges represent
4214 exactly one value. */
4215 if (compare_values_warnv (vr0
->min
, vr0
->max
, strict_overflow_p
) == 0
4216 && compare_values_warnv (vr1
->min
, vr1
->max
, strict_overflow_p
) == 0)
4218 int cmp_min
= compare_values_warnv (vr0
->min
, vr1
->min
,
4220 int cmp_max
= compare_values_warnv (vr0
->max
, vr1
->max
,
4222 if (cmp_min
== 0 && cmp_max
== 0)
4223 return boolean_true_node
;
4224 else if (cmp_min
!= -2 && cmp_max
!= -2)
4225 return boolean_false_node
;
4227 /* If [V0_MIN, V1_MAX] < [V1_MIN, V1_MAX] then V0 != V1. */
4228 else if (compare_values_warnv (vr0
->min
, vr1
->max
,
4229 strict_overflow_p
) == 1
4230 || compare_values_warnv (vr1
->min
, vr0
->max
,
4231 strict_overflow_p
) == 1)
4232 return boolean_false_node
;
4236 else if (comp
== NE_EXPR
)
4240 /* If VR0 is completely to the left or completely to the right
4241 of VR1, they are always different. Notice that we need to
4242 make sure that both comparisons yield similar results to
4243 avoid comparing values that cannot be compared at
4245 cmp1
= compare_values_warnv (vr0
->max
, vr1
->min
, strict_overflow_p
);
4246 cmp2
= compare_values_warnv (vr0
->min
, vr1
->max
, strict_overflow_p
);
4247 if ((cmp1
== -1 && cmp2
== -1) || (cmp1
== 1 && cmp2
== 1))
4248 return boolean_true_node
;
4250 /* If VR0 and VR1 represent a single value and are identical,
4252 else if (compare_values_warnv (vr0
->min
, vr0
->max
,
4253 strict_overflow_p
) == 0
4254 && compare_values_warnv (vr1
->min
, vr1
->max
,
4255 strict_overflow_p
) == 0
4256 && compare_values_warnv (vr0
->min
, vr1
->min
,
4257 strict_overflow_p
) == 0
4258 && compare_values_warnv (vr0
->max
, vr1
->max
,
4259 strict_overflow_p
) == 0)
4260 return boolean_false_node
;
4262 /* Otherwise, they may or may not be different. */
4266 else if (comp
== LT_EXPR
|| comp
== LE_EXPR
)
4270 /* If VR0 is to the left of VR1, return true. */
4271 tst
= compare_values_warnv (vr0
->max
, vr1
->min
, strict_overflow_p
);
4272 if ((comp
== LT_EXPR
&& tst
== -1)
4273 || (comp
== LE_EXPR
&& (tst
== -1 || tst
== 0)))
4274 return boolean_true_node
;
4276 /* If VR0 is to the right of VR1, return false. */
4277 tst
= compare_values_warnv (vr0
->min
, vr1
->max
, strict_overflow_p
);
4278 if ((comp
== LT_EXPR
&& (tst
== 0 || tst
== 1))
4279 || (comp
== LE_EXPR
&& tst
== 1))
4280 return boolean_false_node
;
4282 /* Otherwise, we don't know. */
4290 /* Given a value range VR, a value VAL and a comparison code COMP, return
4291 BOOLEAN_TRUE_NODE if VR COMP VAL always returns true for all the
4292 values in VR. Return BOOLEAN_FALSE_NODE if the comparison
4293 always returns false. Return NULL_TREE if it is not always
4294 possible to determine the value of the comparison. Also set
4295 *STRICT_OVERFLOW_P to indicate whether comparision evaluation
4296 assumed signed overflow is undefined. */
4299 compare_range_with_value (enum tree_code comp
, value_range
*vr
, tree val
,
4300 bool *strict_overflow_p
)
4302 if (vr
->type
== VR_VARYING
|| vr
->type
== VR_UNDEFINED
)
4305 /* Anti-ranges need to be handled separately. */
4306 if (vr
->type
== VR_ANTI_RANGE
)
4308 /* For anti-ranges, the only predicates that we can compute at
4309 compile time are equality and inequality. */
4316 /* ~[VAL_1, VAL_2] OP VAL is known if VAL_1 <= VAL <= VAL_2. */
4317 if (value_inside_range (val
, vr
->min
, vr
->max
) == 1)
4318 return (comp
== NE_EXPR
) ? boolean_true_node
: boolean_false_node
;
4323 if (comp
== EQ_EXPR
)
4325 /* EQ_EXPR may only be computed if VR represents exactly
4327 if (compare_values_warnv (vr
->min
, vr
->max
, strict_overflow_p
) == 0)
4329 int cmp
= compare_values_warnv (vr
->min
, val
, strict_overflow_p
);
4331 return boolean_true_node
;
4332 else if (cmp
== -1 || cmp
== 1 || cmp
== 2)
4333 return boolean_false_node
;
4335 else if (compare_values_warnv (val
, vr
->min
, strict_overflow_p
) == -1
4336 || compare_values_warnv (vr
->max
, val
, strict_overflow_p
) == -1)
4337 return boolean_false_node
;
4341 else if (comp
== NE_EXPR
)
4343 /* If VAL is not inside VR, then they are always different. */
4344 if (compare_values_warnv (vr
->max
, val
, strict_overflow_p
) == -1
4345 || compare_values_warnv (vr
->min
, val
, strict_overflow_p
) == 1)
4346 return boolean_true_node
;
4348 /* If VR represents exactly one value equal to VAL, then return
4350 if (compare_values_warnv (vr
->min
, vr
->max
, strict_overflow_p
) == 0
4351 && compare_values_warnv (vr
->min
, val
, strict_overflow_p
) == 0)
4352 return boolean_false_node
;
4354 /* Otherwise, they may or may not be different. */
4357 else if (comp
== LT_EXPR
|| comp
== LE_EXPR
)
4361 /* If VR is to the left of VAL, return true. */
4362 tst
= compare_values_warnv (vr
->max
, val
, strict_overflow_p
);
4363 if ((comp
== LT_EXPR
&& tst
== -1)
4364 || (comp
== LE_EXPR
&& (tst
== -1 || tst
== 0)))
4365 return boolean_true_node
;
4367 /* If VR is to the right of VAL, return false. */
4368 tst
= compare_values_warnv (vr
->min
, val
, strict_overflow_p
);
4369 if ((comp
== LT_EXPR
&& (tst
== 0 || tst
== 1))
4370 || (comp
== LE_EXPR
&& tst
== 1))
4371 return boolean_false_node
;
4373 /* Otherwise, we don't know. */
4376 else if (comp
== GT_EXPR
|| comp
== GE_EXPR
)
4380 /* If VR is to the right of VAL, return true. */
4381 tst
= compare_values_warnv (vr
->min
, val
, strict_overflow_p
);
4382 if ((comp
== GT_EXPR
&& tst
== 1)
4383 || (comp
== GE_EXPR
&& (tst
== 0 || tst
== 1)))
4384 return boolean_true_node
;
4386 /* If VR is to the left of VAL, return false. */
4387 tst
= compare_values_warnv (vr
->max
, val
, strict_overflow_p
);
4388 if ((comp
== GT_EXPR
&& (tst
== -1 || tst
== 0))
4389 || (comp
== GE_EXPR
&& tst
== -1))
4390 return boolean_false_node
;
4392 /* Otherwise, we don't know. */
4400 /* Debugging dumps. */
4402 void dump_value_range (FILE *, const value_range
*);
4403 void debug_value_range (value_range
*);
4404 void dump_all_value_ranges (FILE *);
4405 void debug_all_value_ranges (void);
4406 void dump_vr_equiv (FILE *, bitmap
);
4407 void debug_vr_equiv (bitmap
);
4410 /* Dump value range VR to FILE. */
4413 dump_value_range (FILE *file
, const value_range
*vr
)
4416 fprintf (file
, "[]");
4417 else if (vr
->type
== VR_UNDEFINED
)
4418 fprintf (file
, "UNDEFINED");
4419 else if (vr
->type
== VR_RANGE
|| vr
->type
== VR_ANTI_RANGE
)
4421 tree type
= TREE_TYPE (vr
->min
);
4423 fprintf (file
, "%s[", (vr
->type
== VR_ANTI_RANGE
) ? "~" : "");
4425 if (INTEGRAL_TYPE_P (type
)
4426 && !TYPE_UNSIGNED (type
)
4427 && vrp_val_is_min (vr
->min
))
4428 fprintf (file
, "-INF");
4430 print_generic_expr (file
, vr
->min
);
4432 fprintf (file
, ", ");
4434 if (INTEGRAL_TYPE_P (type
)
4435 && vrp_val_is_max (vr
->max
))
4436 fprintf (file
, "+INF");
4438 print_generic_expr (file
, vr
->max
);
4440 fprintf (file
, "]");
4447 fprintf (file
, " EQUIVALENCES: { ");
4449 EXECUTE_IF_SET_IN_BITMAP (vr
->equiv
, 0, i
, bi
)
4451 print_generic_expr (file
, ssa_name (i
));
4452 fprintf (file
, " ");
4456 fprintf (file
, "} (%u elements)", c
);
4459 else if (vr
->type
== VR_VARYING
)
4460 fprintf (file
, "VARYING");
4462 fprintf (file
, "INVALID RANGE");
4466 /* Dump value range VR to stderr. */
4469 debug_value_range (value_range
*vr
)
4471 dump_value_range (stderr
, vr
);
4472 fprintf (stderr
, "\n");
4476 /* Dump value ranges of all SSA_NAMEs to FILE. */
4479 dump_all_value_ranges (FILE *file
)
4483 for (i
= 0; i
< num_vr_values
; i
++)
4487 print_generic_expr (file
, ssa_name (i
));
4488 fprintf (file
, ": ");
4489 dump_value_range (file
, vr_value
[i
]);
4490 fprintf (file
, "\n");
4494 fprintf (file
, "\n");
4498 /* Dump all value ranges to stderr. */
4501 debug_all_value_ranges (void)
4503 dump_all_value_ranges (stderr
);
4507 /* Given a COND_EXPR COND of the form 'V OP W', and an SSA name V,
4508 create a new SSA name N and return the assertion assignment
4509 'N = ASSERT_EXPR <V, V OP W>'. */
4512 build_assert_expr_for (tree cond
, tree v
)
4517 gcc_assert (TREE_CODE (v
) == SSA_NAME
4518 && COMPARISON_CLASS_P (cond
));
4520 a
= build2 (ASSERT_EXPR
, TREE_TYPE (v
), v
, cond
);
4521 assertion
= gimple_build_assign (NULL_TREE
, a
);
4523 /* The new ASSERT_EXPR, creates a new SSA name that replaces the
4524 operand of the ASSERT_EXPR. Create it so the new name and the old one
4525 are registered in the replacement table so that we can fix the SSA web
4526 after adding all the ASSERT_EXPRs. */
4527 tree new_def
= create_new_def_for (v
, assertion
, NULL
);
4528 /* Make sure we preserve abnormalness throughout an ASSERT_EXPR chain
4529 given we have to be able to fully propagate those out to re-create
4530 valid SSA when removing the asserts. */
4531 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (v
))
4532 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (new_def
) = 1;
4538 /* Return false if EXPR is a predicate expression involving floating
4542 fp_predicate (gimple
*stmt
)
4544 GIMPLE_CHECK (stmt
, GIMPLE_COND
);
4546 return FLOAT_TYPE_P (TREE_TYPE (gimple_cond_lhs (stmt
)));
4549 /* If the range of values taken by OP can be inferred after STMT executes,
4550 return the comparison code (COMP_CODE_P) and value (VAL_P) that
4551 describes the inferred range. Return true if a range could be
4555 infer_value_range (gimple
*stmt
, tree op
, tree_code
*comp_code_p
, tree
*val_p
)
4558 *comp_code_p
= ERROR_MARK
;
4560 /* Do not attempt to infer anything in names that flow through
4562 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op
))
4565 /* If STMT is the last statement of a basic block with no normal
4566 successors, there is no point inferring anything about any of its
4567 operands. We would not be able to find a proper insertion point
4568 for the assertion, anyway. */
4569 if (stmt_ends_bb_p (stmt
))
4574 FOR_EACH_EDGE (e
, ei
, gimple_bb (stmt
)->succs
)
4575 if (!(e
->flags
& (EDGE_ABNORMAL
|EDGE_EH
)))
4581 if (infer_nonnull_range (stmt
, op
))
4583 *val_p
= build_int_cst (TREE_TYPE (op
), 0);
4584 *comp_code_p
= NE_EXPR
;
4592 void dump_asserts_for (FILE *, tree
);
4593 void debug_asserts_for (tree
);
4594 void dump_all_asserts (FILE *);
4595 void debug_all_asserts (void);
4597 /* Dump all the registered assertions for NAME to FILE. */
4600 dump_asserts_for (FILE *file
, tree name
)
4604 fprintf (file
, "Assertions to be inserted for ");
4605 print_generic_expr (file
, name
);
4606 fprintf (file
, "\n");
4608 loc
= asserts_for
[SSA_NAME_VERSION (name
)];
4611 fprintf (file
, "\t");
4612 print_gimple_stmt (file
, gsi_stmt (loc
->si
), 0);
4613 fprintf (file
, "\n\tBB #%d", loc
->bb
->index
);
4616 fprintf (file
, "\n\tEDGE %d->%d", loc
->e
->src
->index
,
4617 loc
->e
->dest
->index
);
4618 dump_edge_info (file
, loc
->e
, dump_flags
, 0);
4620 fprintf (file
, "\n\tPREDICATE: ");
4621 print_generic_expr (file
, loc
->expr
);
4622 fprintf (file
, " %s ", get_tree_code_name (loc
->comp_code
));
4623 print_generic_expr (file
, loc
->val
);
4624 fprintf (file
, "\n\n");
4628 fprintf (file
, "\n");
4632 /* Dump all the registered assertions for NAME to stderr. */
4635 debug_asserts_for (tree name
)
4637 dump_asserts_for (stderr
, name
);
4641 /* Dump all the registered assertions for all the names to FILE. */
4644 dump_all_asserts (FILE *file
)
4649 fprintf (file
, "\nASSERT_EXPRs to be inserted\n\n");
4650 EXECUTE_IF_SET_IN_BITMAP (need_assert_for
, 0, i
, bi
)
4651 dump_asserts_for (file
, ssa_name (i
));
4652 fprintf (file
, "\n");
4656 /* Dump all the registered assertions for all the names to stderr. */
4659 debug_all_asserts (void)
4661 dump_all_asserts (stderr
);
4664 /* Push the assert info for NAME, EXPR, COMP_CODE and VAL to ASSERTS. */
4667 add_assert_info (vec
<assert_info
> &asserts
,
4668 tree name
, tree expr
, enum tree_code comp_code
, tree val
)
4671 info
.comp_code
= comp_code
;
4675 asserts
.safe_push (info
);
4678 /* If NAME doesn't have an ASSERT_EXPR registered for asserting
4679 'EXPR COMP_CODE VAL' at a location that dominates block BB or
4680 E->DEST, then register this location as a possible insertion point
4681 for ASSERT_EXPR <NAME, EXPR COMP_CODE VAL>.
4683 BB, E and SI provide the exact insertion point for the new
4684 ASSERT_EXPR. If BB is NULL, then the ASSERT_EXPR is to be inserted
4685 on edge E. Otherwise, if E is NULL, the ASSERT_EXPR is inserted on
4686 BB. If SI points to a COND_EXPR or a SWITCH_EXPR statement, then E
4687 must not be NULL. */
4690 register_new_assert_for (tree name
, tree expr
,
4691 enum tree_code comp_code
,
4695 gimple_stmt_iterator si
)
4697 assert_locus
*n
, *loc
, *last_loc
;
4698 basic_block dest_bb
;
4700 gcc_checking_assert (bb
== NULL
|| e
== NULL
);
4703 gcc_checking_assert (gimple_code (gsi_stmt (si
)) != GIMPLE_COND
4704 && gimple_code (gsi_stmt (si
)) != GIMPLE_SWITCH
);
4706 /* Never build an assert comparing against an integer constant with
4707 TREE_OVERFLOW set. This confuses our undefined overflow warning
4709 if (TREE_OVERFLOW_P (val
))
4710 val
= drop_tree_overflow (val
);
4712 /* The new assertion A will be inserted at BB or E. We need to
4713 determine if the new location is dominated by a previously
4714 registered location for A. If we are doing an edge insertion,
4715 assume that A will be inserted at E->DEST. Note that this is not
4718 If E is a critical edge, it will be split. But even if E is
4719 split, the new block will dominate the same set of blocks that
4722 The reverse, however, is not true, blocks dominated by E->DEST
4723 will not be dominated by the new block created to split E. So,
4724 if the insertion location is on a critical edge, we will not use
4725 the new location to move another assertion previously registered
4726 at a block dominated by E->DEST. */
4727 dest_bb
= (bb
) ? bb
: e
->dest
;
4729 /* If NAME already has an ASSERT_EXPR registered for COMP_CODE and
4730 VAL at a block dominating DEST_BB, then we don't need to insert a new
4731 one. Similarly, if the same assertion already exists at a block
4732 dominated by DEST_BB and the new location is not on a critical
4733 edge, then update the existing location for the assertion (i.e.,
4734 move the assertion up in the dominance tree).
4736 Note, this is implemented as a simple linked list because there
4737 should not be more than a handful of assertions registered per
4738 name. If this becomes a performance problem, a table hashed by
4739 COMP_CODE and VAL could be implemented. */
4740 loc
= asserts_for
[SSA_NAME_VERSION (name
)];
4744 if (loc
->comp_code
== comp_code
4746 || operand_equal_p (loc
->val
, val
, 0))
4747 && (loc
->expr
== expr
4748 || operand_equal_p (loc
->expr
, expr
, 0)))
4750 /* If E is not a critical edge and DEST_BB
4751 dominates the existing location for the assertion, move
4752 the assertion up in the dominance tree by updating its
4753 location information. */
4754 if ((e
== NULL
|| !EDGE_CRITICAL_P (e
))
4755 && dominated_by_p (CDI_DOMINATORS
, loc
->bb
, dest_bb
))
4764 /* Update the last node of the list and move to the next one. */
4769 /* If we didn't find an assertion already registered for
4770 NAME COMP_CODE VAL, add a new one at the end of the list of
4771 assertions associated with NAME. */
4772 n
= XNEW (struct assert_locus
);
4776 n
->comp_code
= comp_code
;
4784 asserts_for
[SSA_NAME_VERSION (name
)] = n
;
4786 bitmap_set_bit (need_assert_for
, SSA_NAME_VERSION (name
));
4789 /* (COND_OP0 COND_CODE COND_OP1) is a predicate which uses NAME.
4790 Extract a suitable test code and value and store them into *CODE_P and
4791 *VAL_P so the predicate is normalized to NAME *CODE_P *VAL_P.
4793 If no extraction was possible, return FALSE, otherwise return TRUE.
4795 If INVERT is true, then we invert the result stored into *CODE_P. */
4798 extract_code_and_val_from_cond_with_ops (tree name
, enum tree_code cond_code
,
4799 tree cond_op0
, tree cond_op1
,
4800 bool invert
, enum tree_code
*code_p
,
4803 enum tree_code comp_code
;
4806 /* Otherwise, we have a comparison of the form NAME COMP VAL
4807 or VAL COMP NAME. */
4808 if (name
== cond_op1
)
4810 /* If the predicate is of the form VAL COMP NAME, flip
4811 COMP around because we need to register NAME as the
4812 first operand in the predicate. */
4813 comp_code
= swap_tree_comparison (cond_code
);
4816 else if (name
== cond_op0
)
4818 /* The comparison is of the form NAME COMP VAL, so the
4819 comparison code remains unchanged. */
4820 comp_code
= cond_code
;
4826 /* Invert the comparison code as necessary. */
4828 comp_code
= invert_tree_comparison (comp_code
, 0);
4830 /* VRP only handles integral and pointer types. */
4831 if (! INTEGRAL_TYPE_P (TREE_TYPE (val
))
4832 && ! POINTER_TYPE_P (TREE_TYPE (val
)))
4835 /* Do not register always-false predicates.
4836 FIXME: this works around a limitation in fold() when dealing with
4837 enumerations. Given 'enum { N1, N2 } x;', fold will not
4838 fold 'if (x > N2)' to 'if (0)'. */
4839 if ((comp_code
== GT_EXPR
|| comp_code
== LT_EXPR
)
4840 && INTEGRAL_TYPE_P (TREE_TYPE (val
)))
4842 tree min
= TYPE_MIN_VALUE (TREE_TYPE (val
));
4843 tree max
= TYPE_MAX_VALUE (TREE_TYPE (val
));
4845 if (comp_code
== GT_EXPR
4847 || compare_values (val
, max
) == 0))
4850 if (comp_code
== LT_EXPR
4852 || compare_values (val
, min
) == 0))
4855 *code_p
= comp_code
;
4860 /* Find out smallest RES where RES > VAL && (RES & MASK) == RES, if any
4861 (otherwise return VAL). VAL and MASK must be zero-extended for
4862 precision PREC. If SGNBIT is non-zero, first xor VAL with SGNBIT
4863 (to transform signed values into unsigned) and at the end xor
4867 masked_increment (const wide_int
&val_in
, const wide_int
&mask
,
4868 const wide_int
&sgnbit
, unsigned int prec
)
4870 wide_int bit
= wi::one (prec
), res
;
4873 wide_int val
= val_in
^ sgnbit
;
4874 for (i
= 0; i
< prec
; i
++, bit
+= bit
)
4877 if ((res
& bit
) == 0)
4880 res
= wi::bit_and_not (val
+ bit
, res
);
4882 if (wi::gtu_p (res
, val
))
4883 return res
^ sgnbit
;
4885 return val
^ sgnbit
;
4888 /* Helper for overflow_comparison_p
4890 OP0 CODE OP1 is a comparison. Examine the comparison and potentially
4891 OP1's defining statement to see if it ultimately has the form
4892 OP0 CODE (OP0 PLUS INTEGER_CST)
4894 If so, return TRUE indicating this is an overflow test and store into
4895 *NEW_CST an updated constant that can be used in a narrowed range test.
4897 REVERSED indicates if the comparison was originally:
4901 This affects how we build the updated constant. */
4904 overflow_comparison_p_1 (enum tree_code code
, tree op0
, tree op1
,
4905 bool follow_assert_exprs
, bool reversed
, tree
*new_cst
)
4907 /* See if this is a relational operation between two SSA_NAMES with
4908 unsigned, overflow wrapping values. If so, check it more deeply. */
4909 if ((code
== LT_EXPR
|| code
== LE_EXPR
4910 || code
== GE_EXPR
|| code
== GT_EXPR
)
4911 && TREE_CODE (op0
) == SSA_NAME
4912 && TREE_CODE (op1
) == SSA_NAME
4913 && INTEGRAL_TYPE_P (TREE_TYPE (op0
))
4914 && TYPE_UNSIGNED (TREE_TYPE (op0
))
4915 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (op0
)))
4917 gimple
*op1_def
= SSA_NAME_DEF_STMT (op1
);
4919 /* If requested, follow any ASSERT_EXPRs backwards for OP1. */
4920 if (follow_assert_exprs
)
4922 while (gimple_assign_single_p (op1_def
)
4923 && TREE_CODE (gimple_assign_rhs1 (op1_def
)) == ASSERT_EXPR
)
4925 op1
= TREE_OPERAND (gimple_assign_rhs1 (op1_def
), 0);
4926 if (TREE_CODE (op1
) != SSA_NAME
)
4928 op1_def
= SSA_NAME_DEF_STMT (op1
);
4932 /* Now look at the defining statement of OP1 to see if it adds
4933 or subtracts a nonzero constant from another operand. */
4935 && is_gimple_assign (op1_def
)
4936 && gimple_assign_rhs_code (op1_def
) == PLUS_EXPR
4937 && TREE_CODE (gimple_assign_rhs2 (op1_def
)) == INTEGER_CST
4938 && !integer_zerop (gimple_assign_rhs2 (op1_def
)))
4940 tree target
= gimple_assign_rhs1 (op1_def
);
4942 /* If requested, follow ASSERT_EXPRs backwards for op0 looking
4943 for one where TARGET appears on the RHS. */
4944 if (follow_assert_exprs
)
4946 /* Now see if that "other operand" is op0, following the chain
4947 of ASSERT_EXPRs if necessary. */
4948 gimple
*op0_def
= SSA_NAME_DEF_STMT (op0
);
4949 while (op0
!= target
4950 && gimple_assign_single_p (op0_def
)
4951 && TREE_CODE (gimple_assign_rhs1 (op0_def
)) == ASSERT_EXPR
)
4953 op0
= TREE_OPERAND (gimple_assign_rhs1 (op0_def
), 0);
4954 if (TREE_CODE (op0
) != SSA_NAME
)
4956 op0_def
= SSA_NAME_DEF_STMT (op0
);
4960 /* If we did not find our target SSA_NAME, then this is not
4961 an overflow test. */
4965 tree type
= TREE_TYPE (op0
);
4966 wide_int max
= wi::max_value (TYPE_PRECISION (type
), UNSIGNED
);
4967 tree inc
= gimple_assign_rhs2 (op1_def
);
4969 *new_cst
= wide_int_to_tree (type
, max
+ wi::to_wide (inc
));
4971 *new_cst
= wide_int_to_tree (type
, max
- wi::to_wide (inc
));
4978 /* OP0 CODE OP1 is a comparison. Examine the comparison and potentially
4979 OP1's defining statement to see if it ultimately has the form
4980 OP0 CODE (OP0 PLUS INTEGER_CST)
4982 If so, return TRUE indicating this is an overflow test and store into
4983 *NEW_CST an updated constant that can be used in a narrowed range test.
4985 These statements are left as-is in the IL to facilitate discovery of
4986 {ADD,SUB}_OVERFLOW sequences later in the optimizer pipeline. But
4987 the alternate range representation is often useful within VRP. */
4990 overflow_comparison_p (tree_code code
, tree name
, tree val
,
4991 bool use_equiv_p
, tree
*new_cst
)
4993 if (overflow_comparison_p_1 (code
, name
, val
, use_equiv_p
, false, new_cst
))
4995 return overflow_comparison_p_1 (swap_tree_comparison (code
), val
, name
,
4996 use_equiv_p
, true, new_cst
);
5000 /* Try to register an edge assertion for SSA name NAME on edge E for
5001 the condition COND contributing to the conditional jump pointed to by BSI.
5002 Invert the condition COND if INVERT is true. */
5005 register_edge_assert_for_2 (tree name
, edge e
,
5006 enum tree_code cond_code
,
5007 tree cond_op0
, tree cond_op1
, bool invert
,
5008 vec
<assert_info
> &asserts
)
5011 enum tree_code comp_code
;
5013 if (!extract_code_and_val_from_cond_with_ops (name
, cond_code
,
5016 invert
, &comp_code
, &val
))
5019 /* Queue the assert. */
5021 if (overflow_comparison_p (comp_code
, name
, val
, false, &x
))
5023 enum tree_code new_code
= ((comp_code
== GT_EXPR
|| comp_code
== GE_EXPR
)
5024 ? GT_EXPR
: LE_EXPR
);
5025 add_assert_info (asserts
, name
, name
, new_code
, x
);
5027 add_assert_info (asserts
, name
, name
, comp_code
, val
);
5029 /* In the case of NAME <= CST and NAME being defined as
5030 NAME = (unsigned) NAME2 + CST2 we can assert NAME2 >= -CST2
5031 and NAME2 <= CST - CST2. We can do the same for NAME > CST.
5032 This catches range and anti-range tests. */
5033 if ((comp_code
== LE_EXPR
5034 || comp_code
== GT_EXPR
)
5035 && TREE_CODE (val
) == INTEGER_CST
5036 && TYPE_UNSIGNED (TREE_TYPE (val
)))
5038 gimple
*def_stmt
= SSA_NAME_DEF_STMT (name
);
5039 tree cst2
= NULL_TREE
, name2
= NULL_TREE
, name3
= NULL_TREE
;
5041 /* Extract CST2 from the (optional) addition. */
5042 if (is_gimple_assign (def_stmt
)
5043 && gimple_assign_rhs_code (def_stmt
) == PLUS_EXPR
)
5045 name2
= gimple_assign_rhs1 (def_stmt
);
5046 cst2
= gimple_assign_rhs2 (def_stmt
);
5047 if (TREE_CODE (name2
) == SSA_NAME
5048 && TREE_CODE (cst2
) == INTEGER_CST
)
5049 def_stmt
= SSA_NAME_DEF_STMT (name2
);
5052 /* Extract NAME2 from the (optional) sign-changing cast. */
5053 if (gimple_assign_cast_p (def_stmt
))
5055 if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt
))
5056 && ! TYPE_UNSIGNED (TREE_TYPE (gimple_assign_rhs1 (def_stmt
)))
5057 && (TYPE_PRECISION (gimple_expr_type (def_stmt
))
5058 == TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt
)))))
5059 name3
= gimple_assign_rhs1 (def_stmt
);
5062 /* If name3 is used later, create an ASSERT_EXPR for it. */
5063 if (name3
!= NULL_TREE
5064 && TREE_CODE (name3
) == SSA_NAME
5065 && (cst2
== NULL_TREE
5066 || TREE_CODE (cst2
) == INTEGER_CST
)
5067 && INTEGRAL_TYPE_P (TREE_TYPE (name3
)))
5071 /* Build an expression for the range test. */
5072 tmp
= build1 (NOP_EXPR
, TREE_TYPE (name
), name3
);
5073 if (cst2
!= NULL_TREE
)
5074 tmp
= build2 (PLUS_EXPR
, TREE_TYPE (name
), tmp
, cst2
);
5078 fprintf (dump_file
, "Adding assert for ");
5079 print_generic_expr (dump_file
, name3
);
5080 fprintf (dump_file
, " from ");
5081 print_generic_expr (dump_file
, tmp
);
5082 fprintf (dump_file
, "\n");
5085 add_assert_info (asserts
, name3
, tmp
, comp_code
, val
);
5088 /* If name2 is used later, create an ASSERT_EXPR for it. */
5089 if (name2
!= NULL_TREE
5090 && TREE_CODE (name2
) == SSA_NAME
5091 && TREE_CODE (cst2
) == INTEGER_CST
5092 && INTEGRAL_TYPE_P (TREE_TYPE (name2
)))
5096 /* Build an expression for the range test. */
5098 if (TREE_TYPE (name
) != TREE_TYPE (name2
))
5099 tmp
= build1 (NOP_EXPR
, TREE_TYPE (name
), tmp
);
5100 if (cst2
!= NULL_TREE
)
5101 tmp
= build2 (PLUS_EXPR
, TREE_TYPE (name
), tmp
, cst2
);
5105 fprintf (dump_file
, "Adding assert for ");
5106 print_generic_expr (dump_file
, name2
);
5107 fprintf (dump_file
, " from ");
5108 print_generic_expr (dump_file
, tmp
);
5109 fprintf (dump_file
, "\n");
5112 add_assert_info (asserts
, name2
, tmp
, comp_code
, val
);
5116 /* In the case of post-in/decrement tests like if (i++) ... and uses
5117 of the in/decremented value on the edge the extra name we want to
5118 assert for is not on the def chain of the name compared. Instead
5119 it is in the set of use stmts.
5120 Similar cases happen for conversions that were simplified through
5121 fold_{sign_changed,widened}_comparison. */
5122 if ((comp_code
== NE_EXPR
5123 || comp_code
== EQ_EXPR
)
5124 && TREE_CODE (val
) == INTEGER_CST
)
5126 imm_use_iterator ui
;
5128 FOR_EACH_IMM_USE_STMT (use_stmt
, ui
, name
)
5130 if (!is_gimple_assign (use_stmt
))
5133 /* Cut off to use-stmts that are dominating the predecessor. */
5134 if (!dominated_by_p (CDI_DOMINATORS
, e
->src
, gimple_bb (use_stmt
)))
5137 tree name2
= gimple_assign_lhs (use_stmt
);
5138 if (TREE_CODE (name2
) != SSA_NAME
)
5141 enum tree_code code
= gimple_assign_rhs_code (use_stmt
);
5143 if (code
== PLUS_EXPR
5144 || code
== MINUS_EXPR
)
5146 cst
= gimple_assign_rhs2 (use_stmt
);
5147 if (TREE_CODE (cst
) != INTEGER_CST
)
5149 cst
= int_const_binop (code
, val
, cst
);
5151 else if (CONVERT_EXPR_CODE_P (code
))
5153 /* For truncating conversions we cannot record
5155 if (comp_code
== NE_EXPR
5156 && (TYPE_PRECISION (TREE_TYPE (name2
))
5157 < TYPE_PRECISION (TREE_TYPE (name
))))
5159 cst
= fold_convert (TREE_TYPE (name2
), val
);
5164 if (TREE_OVERFLOW_P (cst
))
5165 cst
= drop_tree_overflow (cst
);
5166 add_assert_info (asserts
, name2
, name2
, comp_code
, cst
);
5170 if (TREE_CODE_CLASS (comp_code
) == tcc_comparison
5171 && TREE_CODE (val
) == INTEGER_CST
)
5173 gimple
*def_stmt
= SSA_NAME_DEF_STMT (name
);
5174 tree name2
= NULL_TREE
, names
[2], cst2
= NULL_TREE
;
5175 tree val2
= NULL_TREE
;
5176 unsigned int prec
= TYPE_PRECISION (TREE_TYPE (val
));
5177 wide_int mask
= wi::zero (prec
);
5178 unsigned int nprec
= prec
;
5179 enum tree_code rhs_code
= ERROR_MARK
;
5181 if (is_gimple_assign (def_stmt
))
5182 rhs_code
= gimple_assign_rhs_code (def_stmt
);
5184 /* In the case of NAME != CST1 where NAME = A +- CST2 we can
5185 assert that A != CST1 -+ CST2. */
5186 if ((comp_code
== EQ_EXPR
|| comp_code
== NE_EXPR
)
5187 && (rhs_code
== PLUS_EXPR
|| rhs_code
== MINUS_EXPR
))
5189 tree op0
= gimple_assign_rhs1 (def_stmt
);
5190 tree op1
= gimple_assign_rhs2 (def_stmt
);
5191 if (TREE_CODE (op0
) == SSA_NAME
5192 && TREE_CODE (op1
) == INTEGER_CST
)
5194 enum tree_code reverse_op
= (rhs_code
== PLUS_EXPR
5195 ? MINUS_EXPR
: PLUS_EXPR
);
5196 op1
= int_const_binop (reverse_op
, val
, op1
);
5197 if (TREE_OVERFLOW (op1
))
5198 op1
= drop_tree_overflow (op1
);
5199 add_assert_info (asserts
, op0
, op0
, comp_code
, op1
);
5203 /* Add asserts for NAME cmp CST and NAME being defined
5204 as NAME = (int) NAME2. */
5205 if (!TYPE_UNSIGNED (TREE_TYPE (val
))
5206 && (comp_code
== LE_EXPR
|| comp_code
== LT_EXPR
5207 || comp_code
== GT_EXPR
|| comp_code
== GE_EXPR
)
5208 && gimple_assign_cast_p (def_stmt
))
5210 name2
= gimple_assign_rhs1 (def_stmt
);
5211 if (CONVERT_EXPR_CODE_P (rhs_code
)
5212 && INTEGRAL_TYPE_P (TREE_TYPE (name2
))
5213 && TYPE_UNSIGNED (TREE_TYPE (name2
))
5214 && prec
== TYPE_PRECISION (TREE_TYPE (name2
))
5215 && (comp_code
== LE_EXPR
|| comp_code
== GT_EXPR
5216 || !tree_int_cst_equal (val
,
5217 TYPE_MIN_VALUE (TREE_TYPE (val
)))))
5220 enum tree_code new_comp_code
= comp_code
;
5222 cst
= fold_convert (TREE_TYPE (name2
),
5223 TYPE_MIN_VALUE (TREE_TYPE (val
)));
5224 /* Build an expression for the range test. */
5225 tmp
= build2 (PLUS_EXPR
, TREE_TYPE (name2
), name2
, cst
);
5226 cst
= fold_build2 (PLUS_EXPR
, TREE_TYPE (name2
), cst
,
5227 fold_convert (TREE_TYPE (name2
), val
));
5228 if (comp_code
== LT_EXPR
|| comp_code
== GE_EXPR
)
5230 new_comp_code
= comp_code
== LT_EXPR
? LE_EXPR
: GT_EXPR
;
5231 cst
= fold_build2 (MINUS_EXPR
, TREE_TYPE (name2
), cst
,
5232 build_int_cst (TREE_TYPE (name2
), 1));
5237 fprintf (dump_file
, "Adding assert for ");
5238 print_generic_expr (dump_file
, name2
);
5239 fprintf (dump_file
, " from ");
5240 print_generic_expr (dump_file
, tmp
);
5241 fprintf (dump_file
, "\n");
5244 add_assert_info (asserts
, name2
, tmp
, new_comp_code
, cst
);
5248 /* Add asserts for NAME cmp CST and NAME being defined as
5249 NAME = NAME2 >> CST2.
5251 Extract CST2 from the right shift. */
5252 if (rhs_code
== RSHIFT_EXPR
)
5254 name2
= gimple_assign_rhs1 (def_stmt
);
5255 cst2
= gimple_assign_rhs2 (def_stmt
);
5256 if (TREE_CODE (name2
) == SSA_NAME
5257 && tree_fits_uhwi_p (cst2
)
5258 && INTEGRAL_TYPE_P (TREE_TYPE (name2
))
5259 && IN_RANGE (tree_to_uhwi (cst2
), 1, prec
- 1)
5260 && type_has_mode_precision_p (TREE_TYPE (val
)))
5262 mask
= wi::mask (tree_to_uhwi (cst2
), false, prec
);
5263 val2
= fold_binary (LSHIFT_EXPR
, TREE_TYPE (val
), val
, cst2
);
5266 if (val2
!= NULL_TREE
5267 && TREE_CODE (val2
) == INTEGER_CST
5268 && simple_cst_equal (fold_build2 (RSHIFT_EXPR
,
5272 enum tree_code new_comp_code
= comp_code
;
5276 if (comp_code
== EQ_EXPR
|| comp_code
== NE_EXPR
)
5278 if (!TYPE_UNSIGNED (TREE_TYPE (val
)))
5280 tree type
= build_nonstandard_integer_type (prec
, 1);
5281 tmp
= build1 (NOP_EXPR
, type
, name2
);
5282 val2
= fold_convert (type
, val2
);
5284 tmp
= fold_build2 (MINUS_EXPR
, TREE_TYPE (tmp
), tmp
, val2
);
5285 new_val
= wide_int_to_tree (TREE_TYPE (tmp
), mask
);
5286 new_comp_code
= comp_code
== EQ_EXPR
? LE_EXPR
: GT_EXPR
;
5288 else if (comp_code
== LT_EXPR
|| comp_code
== GE_EXPR
)
5291 = wi::min_value (prec
, TYPE_SIGN (TREE_TYPE (val
)));
5293 if (minval
== wi::to_wide (new_val
))
5294 new_val
= NULL_TREE
;
5299 = wi::max_value (prec
, TYPE_SIGN (TREE_TYPE (val
)));
5300 mask
|= wi::to_wide (val2
);
5301 if (wi::eq_p (mask
, maxval
))
5302 new_val
= NULL_TREE
;
5304 new_val
= wide_int_to_tree (TREE_TYPE (val2
), mask
);
5311 fprintf (dump_file
, "Adding assert for ");
5312 print_generic_expr (dump_file
, name2
);
5313 fprintf (dump_file
, " from ");
5314 print_generic_expr (dump_file
, tmp
);
5315 fprintf (dump_file
, "\n");
5318 add_assert_info (asserts
, name2
, tmp
, new_comp_code
, new_val
);
5322 /* Add asserts for NAME cmp CST and NAME being defined as
5323 NAME = NAME2 & CST2.
5325 Extract CST2 from the and.
5328 NAME = (unsigned) NAME2;
5329 casts where NAME's type is unsigned and has smaller precision
5330 than NAME2's type as if it was NAME = NAME2 & MASK. */
5331 names
[0] = NULL_TREE
;
5332 names
[1] = NULL_TREE
;
5334 if (rhs_code
== BIT_AND_EXPR
5335 || (CONVERT_EXPR_CODE_P (rhs_code
)
5336 && INTEGRAL_TYPE_P (TREE_TYPE (val
))
5337 && TYPE_UNSIGNED (TREE_TYPE (val
))
5338 && TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt
)))
5341 name2
= gimple_assign_rhs1 (def_stmt
);
5342 if (rhs_code
== BIT_AND_EXPR
)
5343 cst2
= gimple_assign_rhs2 (def_stmt
);
5346 cst2
= TYPE_MAX_VALUE (TREE_TYPE (val
));
5347 nprec
= TYPE_PRECISION (TREE_TYPE (name2
));
5349 if (TREE_CODE (name2
) == SSA_NAME
5350 && INTEGRAL_TYPE_P (TREE_TYPE (name2
))
5351 && TREE_CODE (cst2
) == INTEGER_CST
5352 && !integer_zerop (cst2
)
5354 || TYPE_UNSIGNED (TREE_TYPE (val
))))
5356 gimple
*def_stmt2
= SSA_NAME_DEF_STMT (name2
);
5357 if (gimple_assign_cast_p (def_stmt2
))
5359 names
[1] = gimple_assign_rhs1 (def_stmt2
);
5360 if (!CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt2
))
5361 || !INTEGRAL_TYPE_P (TREE_TYPE (names
[1]))
5362 || (TYPE_PRECISION (TREE_TYPE (name2
))
5363 != TYPE_PRECISION (TREE_TYPE (names
[1]))))
5364 names
[1] = NULL_TREE
;
5369 if (names
[0] || names
[1])
5371 wide_int minv
, maxv
, valv
, cst2v
;
5372 wide_int tem
, sgnbit
;
5373 bool valid_p
= false, valn
, cst2n
;
5374 enum tree_code ccode
= comp_code
;
5376 valv
= wide_int::from (wi::to_wide (val
), nprec
, UNSIGNED
);
5377 cst2v
= wide_int::from (wi::to_wide (cst2
), nprec
, UNSIGNED
);
5378 valn
= wi::neg_p (valv
, TYPE_SIGN (TREE_TYPE (val
)));
5379 cst2n
= wi::neg_p (cst2v
, TYPE_SIGN (TREE_TYPE (val
)));
5380 /* If CST2 doesn't have most significant bit set,
5381 but VAL is negative, we have comparison like
5382 if ((x & 0x123) > -4) (always true). Just give up. */
5386 sgnbit
= wi::set_bit_in_zero (nprec
- 1, nprec
);
5388 sgnbit
= wi::zero (nprec
);
5389 minv
= valv
& cst2v
;
5393 /* Minimum unsigned value for equality is VAL & CST2
5394 (should be equal to VAL, otherwise we probably should
5395 have folded the comparison into false) and
5396 maximum unsigned value is VAL | ~CST2. */
5397 maxv
= valv
| ~cst2v
;
5402 tem
= valv
| ~cst2v
;
5403 /* If VAL is 0, handle (X & CST2) != 0 as (X & CST2) > 0U. */
5407 sgnbit
= wi::zero (nprec
);
5410 /* If (VAL | ~CST2) is all ones, handle it as
5411 (X & CST2) < VAL. */
5416 sgnbit
= wi::zero (nprec
);
5419 if (!cst2n
&& wi::neg_p (cst2v
))
5420 sgnbit
= wi::set_bit_in_zero (nprec
- 1, nprec
);
5429 if (tem
== wi::mask (nprec
- 1, false, nprec
))
5435 sgnbit
= wi::zero (nprec
);
5440 /* Minimum unsigned value for >= if (VAL & CST2) == VAL
5441 is VAL and maximum unsigned value is ~0. For signed
5442 comparison, if CST2 doesn't have most significant bit
5443 set, handle it similarly. If CST2 has MSB set,
5444 the minimum is the same, and maximum is ~0U/2. */
5447 /* If (VAL & CST2) != VAL, X & CST2 can't be equal to
5449 minv
= masked_increment (valv
, cst2v
, sgnbit
, nprec
);
5453 maxv
= wi::mask (nprec
- (cst2n
? 1 : 0), false, nprec
);
5459 /* Find out smallest MINV where MINV > VAL
5460 && (MINV & CST2) == MINV, if any. If VAL is signed and
5461 CST2 has MSB set, compute it biased by 1 << (nprec - 1). */
5462 minv
= masked_increment (valv
, cst2v
, sgnbit
, nprec
);
5465 maxv
= wi::mask (nprec
- (cst2n
? 1 : 0), false, nprec
);
5470 /* Minimum unsigned value for <= is 0 and maximum
5471 unsigned value is VAL | ~CST2 if (VAL & CST2) == VAL.
5472 Otherwise, find smallest VAL2 where VAL2 > VAL
5473 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
5475 For signed comparison, if CST2 doesn't have most
5476 significant bit set, handle it similarly. If CST2 has
5477 MSB set, the maximum is the same and minimum is INT_MIN. */
5482 maxv
= masked_increment (valv
, cst2v
, sgnbit
, nprec
);
5494 /* Minimum unsigned value for < is 0 and maximum
5495 unsigned value is (VAL-1) | ~CST2 if (VAL & CST2) == VAL.
5496 Otherwise, find smallest VAL2 where VAL2 > VAL
5497 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
5499 For signed comparison, if CST2 doesn't have most
5500 significant bit set, handle it similarly. If CST2 has
5501 MSB set, the maximum is the same and minimum is INT_MIN. */
5510 maxv
= masked_increment (valv
, cst2v
, sgnbit
, nprec
);
5524 && (maxv
- minv
) != -1)
5526 tree tmp
, new_val
, type
;
5529 for (i
= 0; i
< 2; i
++)
5532 wide_int maxv2
= maxv
;
5534 type
= TREE_TYPE (names
[i
]);
5535 if (!TYPE_UNSIGNED (type
))
5537 type
= build_nonstandard_integer_type (nprec
, 1);
5538 tmp
= build1 (NOP_EXPR
, type
, names
[i
]);
5542 tmp
= build2 (PLUS_EXPR
, type
, tmp
,
5543 wide_int_to_tree (type
, -minv
));
5544 maxv2
= maxv
- minv
;
5546 new_val
= wide_int_to_tree (type
, maxv2
);
5550 fprintf (dump_file
, "Adding assert for ");
5551 print_generic_expr (dump_file
, names
[i
]);
5552 fprintf (dump_file
, " from ");
5553 print_generic_expr (dump_file
, tmp
);
5554 fprintf (dump_file
, "\n");
5557 add_assert_info (asserts
, names
[i
], tmp
, LE_EXPR
, new_val
);
5564 /* OP is an operand of a truth value expression which is known to have
5565 a particular value. Register any asserts for OP and for any
5566 operands in OP's defining statement.
5568 If CODE is EQ_EXPR, then we want to register OP is zero (false),
5569 if CODE is NE_EXPR, then we want to register OP is nonzero (true). */
5572 register_edge_assert_for_1 (tree op
, enum tree_code code
,
5573 edge e
, vec
<assert_info
> &asserts
)
5577 enum tree_code rhs_code
;
5579 /* We only care about SSA_NAMEs. */
5580 if (TREE_CODE (op
) != SSA_NAME
)
5583 /* We know that OP will have a zero or nonzero value. */
5584 val
= build_int_cst (TREE_TYPE (op
), 0);
5585 add_assert_info (asserts
, op
, op
, code
, val
);
5587 /* Now look at how OP is set. If it's set from a comparison,
5588 a truth operation or some bit operations, then we may be able
5589 to register information about the operands of that assignment. */
5590 op_def
= SSA_NAME_DEF_STMT (op
);
5591 if (gimple_code (op_def
) != GIMPLE_ASSIGN
)
5594 rhs_code
= gimple_assign_rhs_code (op_def
);
5596 if (TREE_CODE_CLASS (rhs_code
) == tcc_comparison
)
5598 bool invert
= (code
== EQ_EXPR
? true : false);
5599 tree op0
= gimple_assign_rhs1 (op_def
);
5600 tree op1
= gimple_assign_rhs2 (op_def
);
5602 if (TREE_CODE (op0
) == SSA_NAME
)
5603 register_edge_assert_for_2 (op0
, e
, rhs_code
, op0
, op1
, invert
, asserts
);
5604 if (TREE_CODE (op1
) == SSA_NAME
)
5605 register_edge_assert_for_2 (op1
, e
, rhs_code
, op0
, op1
, invert
, asserts
);
5607 else if ((code
== NE_EXPR
5608 && gimple_assign_rhs_code (op_def
) == BIT_AND_EXPR
)
5610 && gimple_assign_rhs_code (op_def
) == BIT_IOR_EXPR
))
5612 /* Recurse on each operand. */
5613 tree op0
= gimple_assign_rhs1 (op_def
);
5614 tree op1
= gimple_assign_rhs2 (op_def
);
5615 if (TREE_CODE (op0
) == SSA_NAME
5616 && has_single_use (op0
))
5617 register_edge_assert_for_1 (op0
, code
, e
, asserts
);
5618 if (TREE_CODE (op1
) == SSA_NAME
5619 && has_single_use (op1
))
5620 register_edge_assert_for_1 (op1
, code
, e
, asserts
);
5622 else if (gimple_assign_rhs_code (op_def
) == BIT_NOT_EXPR
5623 && TYPE_PRECISION (TREE_TYPE (gimple_assign_lhs (op_def
))) == 1)
5625 /* Recurse, flipping CODE. */
5626 code
= invert_tree_comparison (code
, false);
5627 register_edge_assert_for_1 (gimple_assign_rhs1 (op_def
), code
, e
, asserts
);
5629 else if (gimple_assign_rhs_code (op_def
) == SSA_NAME
)
5631 /* Recurse through the copy. */
5632 register_edge_assert_for_1 (gimple_assign_rhs1 (op_def
), code
, e
, asserts
);
5634 else if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (op_def
)))
5636 /* Recurse through the type conversion, unless it is a narrowing
5637 conversion or conversion from non-integral type. */
5638 tree rhs
= gimple_assign_rhs1 (op_def
);
5639 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs
))
5640 && (TYPE_PRECISION (TREE_TYPE (rhs
))
5641 <= TYPE_PRECISION (TREE_TYPE (op
))))
5642 register_edge_assert_for_1 (rhs
, code
, e
, asserts
);
5646 /* Check if comparison
5647 NAME COND_OP INTEGER_CST
5649 (X & 11...100..0) COND_OP XX...X00...0
5650 Such comparison can yield assertions like
5653 in case of COND_OP being NE_EXPR or
5656 in case of EQ_EXPR. */
5659 is_masked_range_test (tree name
, tree valt
, enum tree_code cond_code
,
5660 tree
*new_name
, tree
*low
, enum tree_code
*low_code
,
5661 tree
*high
, enum tree_code
*high_code
)
5663 gimple
*def_stmt
= SSA_NAME_DEF_STMT (name
);
5665 if (!is_gimple_assign (def_stmt
)
5666 || gimple_assign_rhs_code (def_stmt
) != BIT_AND_EXPR
)
5669 tree t
= gimple_assign_rhs1 (def_stmt
);
5670 tree maskt
= gimple_assign_rhs2 (def_stmt
);
5671 if (TREE_CODE (t
) != SSA_NAME
|| TREE_CODE (maskt
) != INTEGER_CST
)
5674 wi::tree_to_wide_ref mask
= wi::to_wide (maskt
);
5675 wide_int inv_mask
= ~mask
;
5676 /* Assume VALT is INTEGER_CST. */
5677 wi::tree_to_wide_ref val
= wi::to_wide (valt
);
5679 if ((inv_mask
& (inv_mask
+ 1)) != 0
5680 || (val
& mask
) != val
)
5683 bool is_range
= cond_code
== EQ_EXPR
;
5685 tree type
= TREE_TYPE (t
);
5686 wide_int min
= wi::min_value (type
),
5687 max
= wi::max_value (type
);
5691 *low_code
= val
== min
? ERROR_MARK
: GE_EXPR
;
5692 *high_code
= val
== max
? ERROR_MARK
: LE_EXPR
;
5696 /* We can still generate assertion if one of alternatives
5697 is known to always be false. */
5700 *low_code
= (enum tree_code
) 0;
5701 *high_code
= GT_EXPR
;
5703 else if ((val
| inv_mask
) == max
)
5705 *low_code
= LT_EXPR
;
5706 *high_code
= (enum tree_code
) 0;
5713 *low
= wide_int_to_tree (type
, val
);
5714 *high
= wide_int_to_tree (type
, val
| inv_mask
);
5716 if (wi::neg_p (val
, TYPE_SIGN (type
)))
5717 std::swap (*low
, *high
);
5722 /* Try to register an edge assertion for SSA name NAME on edge E for
5723 the condition COND contributing to the conditional jump pointed to by
5727 register_edge_assert_for (tree name
, edge e
,
5728 enum tree_code cond_code
, tree cond_op0
,
5729 tree cond_op1
, vec
<assert_info
> &asserts
)
5732 enum tree_code comp_code
;
5733 bool is_else_edge
= (e
->flags
& EDGE_FALSE_VALUE
) != 0;
5735 /* Do not attempt to infer anything in names that flow through
5737 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name
))
5740 if (!extract_code_and_val_from_cond_with_ops (name
, cond_code
,
5746 /* Register ASSERT_EXPRs for name. */
5747 register_edge_assert_for_2 (name
, e
, cond_code
, cond_op0
,
5748 cond_op1
, is_else_edge
, asserts
);
5751 /* If COND is effectively an equality test of an SSA_NAME against
5752 the value zero or one, then we may be able to assert values
5753 for SSA_NAMEs which flow into COND. */
5755 /* In the case of NAME == 1 or NAME != 0, for BIT_AND_EXPR defining
5756 statement of NAME we can assert both operands of the BIT_AND_EXPR
5757 have nonzero value. */
5758 if (((comp_code
== EQ_EXPR
&& integer_onep (val
))
5759 || (comp_code
== NE_EXPR
&& integer_zerop (val
))))
5761 gimple
*def_stmt
= SSA_NAME_DEF_STMT (name
);
5763 if (is_gimple_assign (def_stmt
)
5764 && gimple_assign_rhs_code (def_stmt
) == BIT_AND_EXPR
)
5766 tree op0
= gimple_assign_rhs1 (def_stmt
);
5767 tree op1
= gimple_assign_rhs2 (def_stmt
);
5768 register_edge_assert_for_1 (op0
, NE_EXPR
, e
, asserts
);
5769 register_edge_assert_for_1 (op1
, NE_EXPR
, e
, asserts
);
5773 /* In the case of NAME == 0 or NAME != 1, for BIT_IOR_EXPR defining
5774 statement of NAME we can assert both operands of the BIT_IOR_EXPR
5776 if (((comp_code
== EQ_EXPR
&& integer_zerop (val
))
5777 || (comp_code
== NE_EXPR
&& integer_onep (val
))))
5779 gimple
*def_stmt
= SSA_NAME_DEF_STMT (name
);
5781 /* For BIT_IOR_EXPR only if NAME == 0 both operands have
5782 necessarily zero value, or if type-precision is one. */
5783 if (is_gimple_assign (def_stmt
)
5784 && (gimple_assign_rhs_code (def_stmt
) == BIT_IOR_EXPR
5785 && (TYPE_PRECISION (TREE_TYPE (name
)) == 1
5786 || comp_code
== EQ_EXPR
)))
5788 tree op0
= gimple_assign_rhs1 (def_stmt
);
5789 tree op1
= gimple_assign_rhs2 (def_stmt
);
5790 register_edge_assert_for_1 (op0
, EQ_EXPR
, e
, asserts
);
5791 register_edge_assert_for_1 (op1
, EQ_EXPR
, e
, asserts
);
5795 /* Sometimes we can infer ranges from (NAME & MASK) == VALUE. */
5796 if ((comp_code
== EQ_EXPR
|| comp_code
== NE_EXPR
)
5797 && TREE_CODE (val
) == INTEGER_CST
)
5799 enum tree_code low_code
, high_code
;
5801 if (is_masked_range_test (name
, val
, comp_code
, &name
, &low
,
5802 &low_code
, &high
, &high_code
))
5804 if (low_code
!= ERROR_MARK
)
5805 register_edge_assert_for_2 (name
, e
, low_code
, name
,
5806 low
, /*invert*/false, asserts
);
5807 if (high_code
!= ERROR_MARK
)
5808 register_edge_assert_for_2 (name
, e
, high_code
, name
,
5809 high
, /*invert*/false, asserts
);
5814 /* Finish found ASSERTS for E and register them at GSI. */
5817 finish_register_edge_assert_for (edge e
, gimple_stmt_iterator gsi
,
5818 vec
<assert_info
> &asserts
)
5820 for (unsigned i
= 0; i
< asserts
.length (); ++i
)
5821 /* Only register an ASSERT_EXPR if NAME was found in the sub-graph
5822 reachable from E. */
5823 if (live_on_edge (e
, asserts
[i
].name
))
5824 register_new_assert_for (asserts
[i
].name
, asserts
[i
].expr
,
5825 asserts
[i
].comp_code
, asserts
[i
].val
,
5831 /* Determine whether the outgoing edges of BB should receive an
5832 ASSERT_EXPR for each of the operands of BB's LAST statement.
5833 The last statement of BB must be a COND_EXPR.
5835 If any of the sub-graphs rooted at BB have an interesting use of
5836 the predicate operands, an assert location node is added to the
5837 list of assertions for the corresponding operands. */
5840 find_conditional_asserts (basic_block bb
, gcond
*last
)
5842 gimple_stmt_iterator bsi
;
5848 bsi
= gsi_for_stmt (last
);
5850 /* Look for uses of the operands in each of the sub-graphs
5851 rooted at BB. We need to check each of the outgoing edges
5852 separately, so that we know what kind of ASSERT_EXPR to
5854 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
5859 /* Register the necessary assertions for each operand in the
5860 conditional predicate. */
5861 auto_vec
<assert_info
, 8> asserts
;
5862 FOR_EACH_SSA_TREE_OPERAND (op
, last
, iter
, SSA_OP_USE
)
5863 register_edge_assert_for (op
, e
,
5864 gimple_cond_code (last
),
5865 gimple_cond_lhs (last
),
5866 gimple_cond_rhs (last
), asserts
);
5867 finish_register_edge_assert_for (e
, bsi
, asserts
);
5877 /* Compare two case labels sorting first by the destination bb index
5878 and then by the case value. */
5881 compare_case_labels (const void *p1
, const void *p2
)
5883 const struct case_info
*ci1
= (const struct case_info
*) p1
;
5884 const struct case_info
*ci2
= (const struct case_info
*) p2
;
5885 int idx1
= ci1
->bb
->index
;
5886 int idx2
= ci2
->bb
->index
;
5890 else if (idx1
== idx2
)
5892 /* Make sure the default label is first in a group. */
5893 if (!CASE_LOW (ci1
->expr
))
5895 else if (!CASE_LOW (ci2
->expr
))
5898 return tree_int_cst_compare (CASE_LOW (ci1
->expr
),
5899 CASE_LOW (ci2
->expr
));
5905 /* Determine whether the outgoing edges of BB should receive an
5906 ASSERT_EXPR for each of the operands of BB's LAST statement.
5907 The last statement of BB must be a SWITCH_EXPR.
5909 If any of the sub-graphs rooted at BB have an interesting use of
5910 the predicate operands, an assert location node is added to the
5911 list of assertions for the corresponding operands. */
5914 find_switch_asserts (basic_block bb
, gswitch
*last
)
5916 gimple_stmt_iterator bsi
;
5919 struct case_info
*ci
;
5920 size_t n
= gimple_switch_num_labels (last
);
5921 #if GCC_VERSION >= 4000
5924 /* Work around GCC 3.4 bug (PR 37086). */
5925 volatile unsigned int idx
;
5928 bsi
= gsi_for_stmt (last
);
5929 op
= gimple_switch_index (last
);
5930 if (TREE_CODE (op
) != SSA_NAME
)
5933 /* Build a vector of case labels sorted by destination label. */
5934 ci
= XNEWVEC (struct case_info
, n
);
5935 for (idx
= 0; idx
< n
; ++idx
)
5937 ci
[idx
].expr
= gimple_switch_label (last
, idx
);
5938 ci
[idx
].bb
= label_to_block (CASE_LABEL (ci
[idx
].expr
));
5940 edge default_edge
= find_edge (bb
, ci
[0].bb
);
5941 qsort (ci
, n
, sizeof (struct case_info
), compare_case_labels
);
5943 for (idx
= 0; idx
< n
; ++idx
)
5946 tree cl
= ci
[idx
].expr
;
5947 basic_block cbb
= ci
[idx
].bb
;
5949 min
= CASE_LOW (cl
);
5950 max
= CASE_HIGH (cl
);
5952 /* If there are multiple case labels with the same destination
5953 we need to combine them to a single value range for the edge. */
5954 if (idx
+ 1 < n
&& cbb
== ci
[idx
+ 1].bb
)
5956 /* Skip labels until the last of the group. */
5959 } while (idx
< n
&& cbb
== ci
[idx
].bb
);
5962 /* Pick up the maximum of the case label range. */
5963 if (CASE_HIGH (ci
[idx
].expr
))
5964 max
= CASE_HIGH (ci
[idx
].expr
);
5966 max
= CASE_LOW (ci
[idx
].expr
);
5969 /* Can't extract a useful assertion out of a range that includes the
5971 if (min
== NULL_TREE
)
5974 /* Find the edge to register the assert expr on. */
5975 e
= find_edge (bb
, cbb
);
5977 /* Register the necessary assertions for the operand in the
5979 auto_vec
<assert_info
, 8> asserts
;
5980 register_edge_assert_for (op
, e
,
5981 max
? GE_EXPR
: EQ_EXPR
,
5982 op
, fold_convert (TREE_TYPE (op
), min
),
5985 register_edge_assert_for (op
, e
, LE_EXPR
, op
,
5986 fold_convert (TREE_TYPE (op
), max
),
5988 finish_register_edge_assert_for (e
, bsi
, asserts
);
5993 if (!live_on_edge (default_edge
, op
))
5996 /* Now register along the default label assertions that correspond to the
5997 anti-range of each label. */
5998 int insertion_limit
= PARAM_VALUE (PARAM_MAX_VRP_SWITCH_ASSERTIONS
);
5999 if (insertion_limit
== 0)
6002 /* We can't do this if the default case shares a label with another case. */
6003 tree default_cl
= gimple_switch_default_label (last
);
6004 for (idx
= 1; idx
< n
; idx
++)
6007 tree cl
= gimple_switch_label (last
, idx
);
6008 if (CASE_LABEL (cl
) == CASE_LABEL (default_cl
))
6011 min
= CASE_LOW (cl
);
6012 max
= CASE_HIGH (cl
);
6014 /* Combine contiguous case ranges to reduce the number of assertions
6016 for (idx
= idx
+ 1; idx
< n
; idx
++)
6018 tree next_min
, next_max
;
6019 tree next_cl
= gimple_switch_label (last
, idx
);
6020 if (CASE_LABEL (next_cl
) == CASE_LABEL (default_cl
))
6023 next_min
= CASE_LOW (next_cl
);
6024 next_max
= CASE_HIGH (next_cl
);
6026 wide_int difference
= (wi::to_wide (next_min
)
6027 - wi::to_wide (max
? max
: min
));
6028 if (wi::eq_p (difference
, 1))
6029 max
= next_max
? next_max
: next_min
;
6035 if (max
== NULL_TREE
)
6037 /* Register the assertion OP != MIN. */
6038 auto_vec
<assert_info
, 8> asserts
;
6039 min
= fold_convert (TREE_TYPE (op
), min
);
6040 register_edge_assert_for (op
, default_edge
, NE_EXPR
, op
, min
,
6042 finish_register_edge_assert_for (default_edge
, bsi
, asserts
);
6046 /* Register the assertion (unsigned)OP - MIN > (MAX - MIN),
6047 which will give OP the anti-range ~[MIN,MAX]. */
6048 tree uop
= fold_convert (unsigned_type_for (TREE_TYPE (op
)), op
);
6049 min
= fold_convert (TREE_TYPE (uop
), min
);
6050 max
= fold_convert (TREE_TYPE (uop
), max
);
6052 tree lhs
= fold_build2 (MINUS_EXPR
, TREE_TYPE (uop
), uop
, min
);
6053 tree rhs
= int_const_binop (MINUS_EXPR
, max
, min
);
6054 register_new_assert_for (op
, lhs
, GT_EXPR
, rhs
,
6055 NULL
, default_edge
, bsi
);
6058 if (--insertion_limit
== 0)
6064 /* Traverse all the statements in block BB looking for statements that
6065 may generate useful assertions for the SSA names in their operand.
6066 If a statement produces a useful assertion A for name N_i, then the
6067 list of assertions already generated for N_i is scanned to
6068 determine if A is actually needed.
6070 If N_i already had the assertion A at a location dominating the
6071 current location, then nothing needs to be done. Otherwise, the
6072 new location for A is recorded instead.
6074 1- For every statement S in BB, all the variables used by S are
6075 added to bitmap FOUND_IN_SUBGRAPH.
6077 2- If statement S uses an operand N in a way that exposes a known
6078 value range for N, then if N was not already generated by an
6079 ASSERT_EXPR, create a new assert location for N. For instance,
6080 if N is a pointer and the statement dereferences it, we can
6081 assume that N is not NULL.
6083 3- COND_EXPRs are a special case of #2. We can derive range
6084 information from the predicate but need to insert different
6085 ASSERT_EXPRs for each of the sub-graphs rooted at the
6086 conditional block. If the last statement of BB is a conditional
6087 expression of the form 'X op Y', then
6089 a) Remove X and Y from the set FOUND_IN_SUBGRAPH.
6091 b) If the conditional is the only entry point to the sub-graph
6092 corresponding to the THEN_CLAUSE, recurse into it. On
6093 return, if X and/or Y are marked in FOUND_IN_SUBGRAPH, then
6094 an ASSERT_EXPR is added for the corresponding variable.
6096 c) Repeat step (b) on the ELSE_CLAUSE.
6098 d) Mark X and Y in FOUND_IN_SUBGRAPH.
6107 In this case, an assertion on the THEN clause is useful to
6108 determine that 'a' is always 9 on that edge. However, an assertion
6109 on the ELSE clause would be unnecessary.
6111 4- If BB does not end in a conditional expression, then we recurse
6112 into BB's dominator children.
6114 At the end of the recursive traversal, every SSA name will have a
6115 list of locations where ASSERT_EXPRs should be added. When a new
6116 location for name N is found, it is registered by calling
6117 register_new_assert_for. That function keeps track of all the
6118 registered assertions to prevent adding unnecessary assertions.
6119 For instance, if a pointer P_4 is dereferenced more than once in a
6120 dominator tree, only the location dominating all the dereference of
6121 P_4 will receive an ASSERT_EXPR. */
6124 find_assert_locations_1 (basic_block bb
, sbitmap live
)
6128 last
= last_stmt (bb
);
6130 /* If BB's last statement is a conditional statement involving integer
6131 operands, determine if we need to add ASSERT_EXPRs. */
6133 && gimple_code (last
) == GIMPLE_COND
6134 && !fp_predicate (last
)
6135 && !ZERO_SSA_OPERANDS (last
, SSA_OP_USE
))
6136 find_conditional_asserts (bb
, as_a
<gcond
*> (last
));
6138 /* If BB's last statement is a switch statement involving integer
6139 operands, determine if we need to add ASSERT_EXPRs. */
6141 && gimple_code (last
) == GIMPLE_SWITCH
6142 && !ZERO_SSA_OPERANDS (last
, SSA_OP_USE
))
6143 find_switch_asserts (bb
, as_a
<gswitch
*> (last
));
6145 /* Traverse all the statements in BB marking used names and looking
6146 for statements that may infer assertions for their used operands. */
6147 for (gimple_stmt_iterator si
= gsi_last_bb (bb
); !gsi_end_p (si
);
6154 stmt
= gsi_stmt (si
);
6156 if (is_gimple_debug (stmt
))
6159 /* See if we can derive an assertion for any of STMT's operands. */
6160 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, i
, SSA_OP_USE
)
6163 enum tree_code comp_code
;
6165 /* If op is not live beyond this stmt, do not bother to insert
6167 if (!bitmap_bit_p (live
, SSA_NAME_VERSION (op
)))
6170 /* If OP is used in such a way that we can infer a value
6171 range for it, and we don't find a previous assertion for
6172 it, create a new assertion location node for OP. */
6173 if (infer_value_range (stmt
, op
, &comp_code
, &value
))
6175 /* If we are able to infer a nonzero value range for OP,
6176 then walk backwards through the use-def chain to see if OP
6177 was set via a typecast.
6179 If so, then we can also infer a nonzero value range
6180 for the operand of the NOP_EXPR. */
6181 if (comp_code
== NE_EXPR
&& integer_zerop (value
))
6184 gimple
*def_stmt
= SSA_NAME_DEF_STMT (t
);
6186 while (is_gimple_assign (def_stmt
)
6187 && CONVERT_EXPR_CODE_P
6188 (gimple_assign_rhs_code (def_stmt
))
6190 (gimple_assign_rhs1 (def_stmt
)) == SSA_NAME
6192 (TREE_TYPE (gimple_assign_rhs1 (def_stmt
))))
6194 t
= gimple_assign_rhs1 (def_stmt
);
6195 def_stmt
= SSA_NAME_DEF_STMT (t
);
6197 /* Note we want to register the assert for the
6198 operand of the NOP_EXPR after SI, not after the
6200 if (bitmap_bit_p (live
, SSA_NAME_VERSION (t
)))
6201 register_new_assert_for (t
, t
, comp_code
, value
,
6206 register_new_assert_for (op
, op
, comp_code
, value
, bb
, NULL
, si
);
6211 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, i
, SSA_OP_USE
)
6212 bitmap_set_bit (live
, SSA_NAME_VERSION (op
));
6213 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, i
, SSA_OP_DEF
)
6214 bitmap_clear_bit (live
, SSA_NAME_VERSION (op
));
6217 /* Traverse all PHI nodes in BB, updating live. */
6218 for (gphi_iterator si
= gsi_start_phis (bb
); !gsi_end_p (si
);
6221 use_operand_p arg_p
;
6223 gphi
*phi
= si
.phi ();
6224 tree res
= gimple_phi_result (phi
);
6226 if (virtual_operand_p (res
))
6229 FOR_EACH_PHI_ARG (arg_p
, phi
, i
, SSA_OP_USE
)
6231 tree arg
= USE_FROM_PTR (arg_p
);
6232 if (TREE_CODE (arg
) == SSA_NAME
)
6233 bitmap_set_bit (live
, SSA_NAME_VERSION (arg
));
6236 bitmap_clear_bit (live
, SSA_NAME_VERSION (res
));
6240 /* Do an RPO walk over the function computing SSA name liveness
6241 on-the-fly and deciding on assert expressions to insert. */
6244 find_assert_locations (void)
6246 int *rpo
= XNEWVEC (int, last_basic_block_for_fn (cfun
));
6247 int *bb_rpo
= XNEWVEC (int, last_basic_block_for_fn (cfun
));
6248 int *last_rpo
= XCNEWVEC (int, last_basic_block_for_fn (cfun
));
6251 live
= XCNEWVEC (sbitmap
, last_basic_block_for_fn (cfun
));
6252 rpo_cnt
= pre_and_rev_post_order_compute (NULL
, rpo
, false);
6253 for (i
= 0; i
< rpo_cnt
; ++i
)
6256 /* Pre-seed loop latch liveness from loop header PHI nodes. Due to
6257 the order we compute liveness and insert asserts we otherwise
6258 fail to insert asserts into the loop latch. */
6260 FOR_EACH_LOOP (loop
, 0)
6262 i
= loop
->latch
->index
;
6263 unsigned int j
= single_succ_edge (loop
->latch
)->dest_idx
;
6264 for (gphi_iterator gsi
= gsi_start_phis (loop
->header
);
6265 !gsi_end_p (gsi
); gsi_next (&gsi
))
6267 gphi
*phi
= gsi
.phi ();
6268 if (virtual_operand_p (gimple_phi_result (phi
)))
6270 tree arg
= gimple_phi_arg_def (phi
, j
);
6271 if (TREE_CODE (arg
) == SSA_NAME
)
6273 if (live
[i
] == NULL
)
6275 live
[i
] = sbitmap_alloc (num_ssa_names
);
6276 bitmap_clear (live
[i
]);
6278 bitmap_set_bit (live
[i
], SSA_NAME_VERSION (arg
));
6283 for (i
= rpo_cnt
- 1; i
>= 0; --i
)
6285 basic_block bb
= BASIC_BLOCK_FOR_FN (cfun
, rpo
[i
]);
6291 live
[rpo
[i
]] = sbitmap_alloc (num_ssa_names
);
6292 bitmap_clear (live
[rpo
[i
]]);
6295 /* Process BB and update the live information with uses in
6297 find_assert_locations_1 (bb
, live
[rpo
[i
]]);
6299 /* Merge liveness into the predecessor blocks and free it. */
6300 if (!bitmap_empty_p (live
[rpo
[i
]]))
6303 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
6305 int pred
= e
->src
->index
;
6306 if ((e
->flags
& EDGE_DFS_BACK
) || pred
== ENTRY_BLOCK
)
6311 live
[pred
] = sbitmap_alloc (num_ssa_names
);
6312 bitmap_clear (live
[pred
]);
6314 bitmap_ior (live
[pred
], live
[pred
], live
[rpo
[i
]]);
6316 if (bb_rpo
[pred
] < pred_rpo
)
6317 pred_rpo
= bb_rpo
[pred
];
6320 /* Record the RPO number of the last visited block that needs
6321 live information from this block. */
6322 last_rpo
[rpo
[i
]] = pred_rpo
;
6326 sbitmap_free (live
[rpo
[i
]]);
6327 live
[rpo
[i
]] = NULL
;
6330 /* We can free all successors live bitmaps if all their
6331 predecessors have been visited already. */
6332 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
6333 if (last_rpo
[e
->dest
->index
] == i
6334 && live
[e
->dest
->index
])
6336 sbitmap_free (live
[e
->dest
->index
]);
6337 live
[e
->dest
->index
] = NULL
;
6342 XDELETEVEC (bb_rpo
);
6343 XDELETEVEC (last_rpo
);
6344 for (i
= 0; i
< last_basic_block_for_fn (cfun
); ++i
)
6346 sbitmap_free (live
[i
]);
6350 /* Create an ASSERT_EXPR for NAME and insert it in the location
6351 indicated by LOC. Return true if we made any edge insertions. */
6354 process_assert_insertions_for (tree name
, assert_locus
*loc
)
6356 /* Build the comparison expression NAME_i COMP_CODE VAL. */
6359 gimple
*assert_stmt
;
6363 /* If we have X <=> X do not insert an assert expr for that. */
6364 if (loc
->expr
== loc
->val
)
6367 cond
= build2 (loc
->comp_code
, boolean_type_node
, loc
->expr
, loc
->val
);
6368 assert_stmt
= build_assert_expr_for (cond
, name
);
6371 /* We have been asked to insert the assertion on an edge. This
6372 is used only by COND_EXPR and SWITCH_EXPR assertions. */
6373 gcc_checking_assert (gimple_code (gsi_stmt (loc
->si
)) == GIMPLE_COND
6374 || (gimple_code (gsi_stmt (loc
->si
))
6377 gsi_insert_on_edge (loc
->e
, assert_stmt
);
6381 /* If the stmt iterator points at the end then this is an insertion
6382 at the beginning of a block. */
6383 if (gsi_end_p (loc
->si
))
6385 gimple_stmt_iterator si
= gsi_after_labels (loc
->bb
);
6386 gsi_insert_before (&si
, assert_stmt
, GSI_SAME_STMT
);
6390 /* Otherwise, we can insert right after LOC->SI iff the
6391 statement must not be the last statement in the block. */
6392 stmt
= gsi_stmt (loc
->si
);
6393 if (!stmt_ends_bb_p (stmt
))
6395 gsi_insert_after (&loc
->si
, assert_stmt
, GSI_SAME_STMT
);
6399 /* If STMT must be the last statement in BB, we can only insert new
6400 assertions on the non-abnormal edge out of BB. Note that since
6401 STMT is not control flow, there may only be one non-abnormal/eh edge
6403 FOR_EACH_EDGE (e
, ei
, loc
->bb
->succs
)
6404 if (!(e
->flags
& (EDGE_ABNORMAL
|EDGE_EH
)))
6406 gsi_insert_on_edge (e
, assert_stmt
);
6413 /* Qsort helper for sorting assert locations. If stable is true, don't
6414 use iterative_hash_expr because it can be unstable for -fcompare-debug,
6415 on the other side some pointers might be NULL. */
6417 template <bool stable
>
6419 compare_assert_loc (const void *pa
, const void *pb
)
6421 assert_locus
* const a
= *(assert_locus
* const *)pa
;
6422 assert_locus
* const b
= *(assert_locus
* const *)pb
;
6424 /* If stable, some asserts might be optimized away already, sort
6434 if (a
->e
== NULL
&& b
->e
!= NULL
)
6436 else if (a
->e
!= NULL
&& b
->e
== NULL
)
6439 /* After the above checks, we know that (a->e == NULL) == (b->e == NULL),
6440 no need to test both a->e and b->e. */
6442 /* Sort after destination index. */
6445 else if (a
->e
->dest
->index
> b
->e
->dest
->index
)
6447 else if (a
->e
->dest
->index
< b
->e
->dest
->index
)
6450 /* Sort after comp_code. */
6451 if (a
->comp_code
> b
->comp_code
)
6453 else if (a
->comp_code
< b
->comp_code
)
6458 /* E.g. if a->val is ADDR_EXPR of a VAR_DECL, iterative_hash_expr
6459 uses DECL_UID of the VAR_DECL, so sorting might differ between
6460 -g and -g0. When doing the removal of redundant assert exprs
6461 and commonization to successors, this does not matter, but for
6462 the final sort needs to be stable. */
6470 ha
= iterative_hash_expr (a
->expr
, iterative_hash_expr (a
->val
, 0));
6471 hb
= iterative_hash_expr (b
->expr
, iterative_hash_expr (b
->val
, 0));
6474 /* Break the tie using hashing and source/bb index. */
6476 return (a
->e
!= NULL
6477 ? a
->e
->src
->index
- b
->e
->src
->index
6478 : a
->bb
->index
- b
->bb
->index
);
6479 return ha
> hb
? 1 : -1;
6482 /* Process all the insertions registered for every name N_i registered
6483 in NEED_ASSERT_FOR. The list of assertions to be inserted are
6484 found in ASSERTS_FOR[i]. */
6487 process_assert_insertions (void)
6491 bool update_edges_p
= false;
6492 int num_asserts
= 0;
6494 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6495 dump_all_asserts (dump_file
);
6497 EXECUTE_IF_SET_IN_BITMAP (need_assert_for
, 0, i
, bi
)
6499 assert_locus
*loc
= asserts_for
[i
];
6502 auto_vec
<assert_locus
*, 16> asserts
;
6503 for (; loc
; loc
= loc
->next
)
6504 asserts
.safe_push (loc
);
6505 asserts
.qsort (compare_assert_loc
<false>);
6507 /* Push down common asserts to successors and remove redundant ones. */
6509 assert_locus
*common
= NULL
;
6510 unsigned commonj
= 0;
6511 for (unsigned j
= 0; j
< asserts
.length (); ++j
)
6517 || loc
->e
->dest
!= common
->e
->dest
6518 || loc
->comp_code
!= common
->comp_code
6519 || ! operand_equal_p (loc
->val
, common
->val
, 0)
6520 || ! operand_equal_p (loc
->expr
, common
->expr
, 0))
6526 else if (loc
->e
== asserts
[j
-1]->e
)
6528 /* Remove duplicate asserts. */
6529 if (commonj
== j
- 1)
6534 free (asserts
[j
-1]);
6535 asserts
[j
-1] = NULL
;
6540 if (EDGE_COUNT (common
->e
->dest
->preds
) == ecnt
)
6542 /* We have the same assertion on all incoming edges of a BB.
6543 Insert it at the beginning of that block. */
6544 loc
->bb
= loc
->e
->dest
;
6546 loc
->si
= gsi_none ();
6548 /* Clear asserts commoned. */
6549 for (; commonj
!= j
; ++commonj
)
6550 if (asserts
[commonj
])
6552 free (asserts
[commonj
]);
6553 asserts
[commonj
] = NULL
;
6559 /* The asserts vector sorting above might be unstable for
6560 -fcompare-debug, sort again to ensure a stable sort. */
6561 asserts
.qsort (compare_assert_loc
<true>);
6562 for (unsigned j
= 0; j
< asserts
.length (); ++j
)
6567 update_edges_p
|= process_assert_insertions_for (ssa_name (i
), loc
);
6574 gsi_commit_edge_inserts ();
6576 statistics_counter_event (cfun
, "Number of ASSERT_EXPR expressions inserted",
6581 /* Traverse the flowgraph looking for conditional jumps to insert range
6582 expressions. These range expressions are meant to provide information
6583 to optimizations that need to reason in terms of value ranges. They
6584 will not be expanded into RTL. For instance, given:
6593 this pass will transform the code into:
6599 x = ASSERT_EXPR <x, x < y>
6604 y = ASSERT_EXPR <y, x >= y>
6608 The idea is that once copy and constant propagation have run, other
6609 optimizations will be able to determine what ranges of values can 'x'
6610 take in different paths of the code, simply by checking the reaching
6611 definition of 'x'. */
6614 insert_range_assertions (void)
6616 need_assert_for
= BITMAP_ALLOC (NULL
);
6617 asserts_for
= XCNEWVEC (assert_locus
*, num_ssa_names
);
6619 calculate_dominance_info (CDI_DOMINATORS
);
6621 find_assert_locations ();
6622 if (!bitmap_empty_p (need_assert_for
))
6624 process_assert_insertions ();
6625 update_ssa (TODO_update_ssa_no_phi
);
6628 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6630 fprintf (dump_file
, "\nSSA form after inserting ASSERT_EXPRs\n");
6631 dump_function_to_file (current_function_decl
, dump_file
, dump_flags
);
6635 BITMAP_FREE (need_assert_for
);
6638 /* Checks one ARRAY_REF in REF, located at LOCUS. Ignores flexible arrays
6639 and "struct" hacks. If VRP can determine that the
6640 array subscript is a constant, check if it is outside valid
6641 range. If the array subscript is a RANGE, warn if it is
6642 non-overlapping with valid range.
6643 IGNORE_OFF_BY_ONE is true if the ARRAY_REF is inside a ADDR_EXPR. */
6646 check_array_ref (location_t location
, tree ref
, bool ignore_off_by_one
)
6648 value_range
*vr
= NULL
;
6649 tree low_sub
, up_sub
;
6650 tree low_bound
, up_bound
, up_bound_p1
;
6652 if (TREE_NO_WARNING (ref
))
6655 low_sub
= up_sub
= TREE_OPERAND (ref
, 1);
6656 up_bound
= array_ref_up_bound (ref
);
6658 /* Can not check flexible arrays. */
6660 || TREE_CODE (up_bound
) != INTEGER_CST
)
6663 /* Accesses to trailing arrays via pointers may access storage
6664 beyond the types array bounds. */
6665 if (warn_array_bounds
< 2
6666 && array_at_struct_end_p (ref
))
6669 low_bound
= array_ref_low_bound (ref
);
6670 up_bound_p1
= int_const_binop (PLUS_EXPR
, up_bound
,
6671 build_int_cst (TREE_TYPE (up_bound
), 1));
6674 if (tree_int_cst_equal (low_bound
, up_bound_p1
))
6676 warning_at (location
, OPT_Warray_bounds
,
6677 "array subscript is above array bounds");
6678 TREE_NO_WARNING (ref
) = 1;
6681 if (TREE_CODE (low_sub
) == SSA_NAME
)
6683 vr
= get_value_range (low_sub
);
6684 if (vr
->type
== VR_RANGE
|| vr
->type
== VR_ANTI_RANGE
)
6686 low_sub
= vr
->type
== VR_RANGE
? vr
->max
: vr
->min
;
6687 up_sub
= vr
->type
== VR_RANGE
? vr
->min
: vr
->max
;
6691 if (vr
&& vr
->type
== VR_ANTI_RANGE
)
6693 if (TREE_CODE (up_sub
) == INTEGER_CST
6694 && (ignore_off_by_one
6695 ? tree_int_cst_lt (up_bound
, up_sub
)
6696 : tree_int_cst_le (up_bound
, up_sub
))
6697 && TREE_CODE (low_sub
) == INTEGER_CST
6698 && tree_int_cst_le (low_sub
, low_bound
))
6700 warning_at (location
, OPT_Warray_bounds
,
6701 "array subscript is outside array bounds");
6702 TREE_NO_WARNING (ref
) = 1;
6705 else if (TREE_CODE (up_sub
) == INTEGER_CST
6706 && (ignore_off_by_one
6707 ? !tree_int_cst_le (up_sub
, up_bound_p1
)
6708 : !tree_int_cst_le (up_sub
, up_bound
)))
6710 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6712 fprintf (dump_file
, "Array bound warning for ");
6713 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, ref
);
6714 fprintf (dump_file
, "\n");
6716 warning_at (location
, OPT_Warray_bounds
,
6717 "array subscript is above array bounds");
6718 TREE_NO_WARNING (ref
) = 1;
6720 else if (TREE_CODE (low_sub
) == INTEGER_CST
6721 && tree_int_cst_lt (low_sub
, low_bound
))
6723 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6725 fprintf (dump_file
, "Array bound warning for ");
6726 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, ref
);
6727 fprintf (dump_file
, "\n");
6729 warning_at (location
, OPT_Warray_bounds
,
6730 "array subscript is below array bounds");
6731 TREE_NO_WARNING (ref
) = 1;
6735 /* Searches if the expr T, located at LOCATION computes
6736 address of an ARRAY_REF, and call check_array_ref on it. */
6739 search_for_addr_array (tree t
, location_t location
)
6741 /* Check each ARRAY_REFs in the reference chain. */
6744 if (TREE_CODE (t
) == ARRAY_REF
)
6745 check_array_ref (location
, t
, true /*ignore_off_by_one*/);
6747 t
= TREE_OPERAND (t
, 0);
6749 while (handled_component_p (t
));
6751 if (TREE_CODE (t
) == MEM_REF
6752 && TREE_CODE (TREE_OPERAND (t
, 0)) == ADDR_EXPR
6753 && !TREE_NO_WARNING (t
))
6755 tree tem
= TREE_OPERAND (TREE_OPERAND (t
, 0), 0);
6756 tree low_bound
, up_bound
, el_sz
;
6758 if (TREE_CODE (TREE_TYPE (tem
)) != ARRAY_TYPE
6759 || TREE_CODE (TREE_TYPE (TREE_TYPE (tem
))) == ARRAY_TYPE
6760 || !TYPE_DOMAIN (TREE_TYPE (tem
)))
6763 low_bound
= TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (tem
)));
6764 up_bound
= TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (tem
)));
6765 el_sz
= TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (tem
)));
6767 || TREE_CODE (low_bound
) != INTEGER_CST
6769 || TREE_CODE (up_bound
) != INTEGER_CST
6771 || TREE_CODE (el_sz
) != INTEGER_CST
)
6774 idx
= mem_ref_offset (t
);
6775 idx
= wi::sdiv_trunc (idx
, wi::to_offset (el_sz
));
6778 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6780 fprintf (dump_file
, "Array bound warning for ");
6781 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, t
);
6782 fprintf (dump_file
, "\n");
6784 warning_at (location
, OPT_Warray_bounds
,
6785 "array subscript is below array bounds");
6786 TREE_NO_WARNING (t
) = 1;
6788 else if (idx
> (wi::to_offset (up_bound
)
6789 - wi::to_offset (low_bound
) + 1))
6791 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6793 fprintf (dump_file
, "Array bound warning for ");
6794 dump_generic_expr (MSG_NOTE
, TDF_SLIM
, t
);
6795 fprintf (dump_file
, "\n");
6797 warning_at (location
, OPT_Warray_bounds
,
6798 "array subscript is above array bounds");
6799 TREE_NO_WARNING (t
) = 1;
6804 /* walk_tree() callback that checks if *TP is
6805 an ARRAY_REF inside an ADDR_EXPR (in which an array
6806 subscript one outside the valid range is allowed). Call
6807 check_array_ref for each ARRAY_REF found. The location is
6811 check_array_bounds (tree
*tp
, int *walk_subtree
, void *data
)
6814 struct walk_stmt_info
*wi
= (struct walk_stmt_info
*) data
;
6815 location_t location
;
6817 if (EXPR_HAS_LOCATION (t
))
6818 location
= EXPR_LOCATION (t
);
6820 location
= gimple_location (wi
->stmt
);
6822 *walk_subtree
= TRUE
;
6824 if (TREE_CODE (t
) == ARRAY_REF
)
6825 check_array_ref (location
, t
, false /*ignore_off_by_one*/);
6827 else if (TREE_CODE (t
) == ADDR_EXPR
)
6829 search_for_addr_array (t
, location
);
6830 *walk_subtree
= FALSE
;
6836 /* Walk over all statements of all reachable BBs and call check_array_bounds
6840 check_all_array_refs (void)
6843 gimple_stmt_iterator si
;
6845 FOR_EACH_BB_FN (bb
, cfun
)
6849 bool executable
= false;
6851 /* Skip blocks that were found to be unreachable. */
6852 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
6853 executable
|= !!(e
->flags
& EDGE_EXECUTABLE
);
6857 for (si
= gsi_start_bb (bb
); !gsi_end_p (si
); gsi_next (&si
))
6859 gimple
*stmt
= gsi_stmt (si
);
6860 struct walk_stmt_info wi
;
6861 if (!gimple_has_location (stmt
)
6862 || is_gimple_debug (stmt
))
6865 memset (&wi
, 0, sizeof (wi
));
6867 walk_gimple_op (gsi_stmt (si
),
6874 /* Return true if all imm uses of VAR are either in STMT, or
6875 feed (optionally through a chain of single imm uses) GIMPLE_COND
6876 in basic block COND_BB. */
6879 all_imm_uses_in_stmt_or_feed_cond (tree var
, gimple
*stmt
, basic_block cond_bb
)
6881 use_operand_p use_p
, use2_p
;
6882 imm_use_iterator iter
;
6884 FOR_EACH_IMM_USE_FAST (use_p
, iter
, var
)
6885 if (USE_STMT (use_p
) != stmt
)
6887 gimple
*use_stmt
= USE_STMT (use_p
), *use_stmt2
;
6888 if (is_gimple_debug (use_stmt
))
6890 while (is_gimple_assign (use_stmt
)
6891 && TREE_CODE (gimple_assign_lhs (use_stmt
)) == SSA_NAME
6892 && single_imm_use (gimple_assign_lhs (use_stmt
),
6893 &use2_p
, &use_stmt2
))
6894 use_stmt
= use_stmt2
;
6895 if (gimple_code (use_stmt
) != GIMPLE_COND
6896 || gimple_bb (use_stmt
) != cond_bb
)
6909 __builtin_unreachable ();
6911 x_5 = ASSERT_EXPR <x_3, ...>;
6912 If x_3 has no other immediate uses (checked by caller),
6913 var is the x_3 var from ASSERT_EXPR, we can clear low 5 bits
6914 from the non-zero bitmask. */
6917 maybe_set_nonzero_bits (basic_block bb
, tree var
)
6919 edge e
= single_pred_edge (bb
);
6920 basic_block cond_bb
= e
->src
;
6921 gimple
*stmt
= last_stmt (cond_bb
);
6925 || gimple_code (stmt
) != GIMPLE_COND
6926 || gimple_cond_code (stmt
) != ((e
->flags
& EDGE_TRUE_VALUE
)
6927 ? EQ_EXPR
: NE_EXPR
)
6928 || TREE_CODE (gimple_cond_lhs (stmt
)) != SSA_NAME
6929 || !integer_zerop (gimple_cond_rhs (stmt
)))
6932 stmt
= SSA_NAME_DEF_STMT (gimple_cond_lhs (stmt
));
6933 if (!is_gimple_assign (stmt
)
6934 || gimple_assign_rhs_code (stmt
) != BIT_AND_EXPR
6935 || TREE_CODE (gimple_assign_rhs2 (stmt
)) != INTEGER_CST
)
6937 if (gimple_assign_rhs1 (stmt
) != var
)
6941 if (TREE_CODE (gimple_assign_rhs1 (stmt
)) != SSA_NAME
)
6943 stmt2
= SSA_NAME_DEF_STMT (gimple_assign_rhs1 (stmt
));
6944 if (!gimple_assign_cast_p (stmt2
)
6945 || gimple_assign_rhs1 (stmt2
) != var
6946 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (stmt2
))
6947 || (TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (stmt
)))
6948 != TYPE_PRECISION (TREE_TYPE (var
))))
6951 cst
= gimple_assign_rhs2 (stmt
);
6952 set_nonzero_bits (var
, wi::bit_and_not (get_nonzero_bits (var
),
6953 wi::to_wide (cst
)));
6956 /* Convert range assertion expressions into the implied copies and
6957 copy propagate away the copies. Doing the trivial copy propagation
6958 here avoids the need to run the full copy propagation pass after
6961 FIXME, this will eventually lead to copy propagation removing the
6962 names that had useful range information attached to them. For
6963 instance, if we had the assertion N_i = ASSERT_EXPR <N_j, N_j > 3>,
6964 then N_i will have the range [3, +INF].
6966 However, by converting the assertion into the implied copy
6967 operation N_i = N_j, we will then copy-propagate N_j into the uses
6968 of N_i and lose the range information. We may want to hold on to
6969 ASSERT_EXPRs a little while longer as the ranges could be used in
6970 things like jump threading.
6972 The problem with keeping ASSERT_EXPRs around is that passes after
6973 VRP need to handle them appropriately.
6975 Another approach would be to make the range information a first
6976 class property of the SSA_NAME so that it can be queried from
6977 any pass. This is made somewhat more complex by the need for
6978 multiple ranges to be associated with one SSA_NAME. */
6981 remove_range_assertions (void)
6984 gimple_stmt_iterator si
;
6985 /* 1 if looking at ASSERT_EXPRs immediately at the beginning of
6986 a basic block preceeded by GIMPLE_COND branching to it and
6987 __builtin_trap, -1 if not yet checked, 0 otherwise. */
6990 /* Note that the BSI iterator bump happens at the bottom of the
6991 loop and no bump is necessary if we're removing the statement
6992 referenced by the current BSI. */
6993 FOR_EACH_BB_FN (bb
, cfun
)
6994 for (si
= gsi_after_labels (bb
), is_unreachable
= -1; !gsi_end_p (si
);)
6996 gimple
*stmt
= gsi_stmt (si
);
6998 if (is_gimple_assign (stmt
)
6999 && gimple_assign_rhs_code (stmt
) == ASSERT_EXPR
)
7001 tree lhs
= gimple_assign_lhs (stmt
);
7002 tree rhs
= gimple_assign_rhs1 (stmt
);
7005 var
= ASSERT_EXPR_VAR (rhs
);
7007 if (TREE_CODE (var
) == SSA_NAME
7008 && !POINTER_TYPE_P (TREE_TYPE (lhs
))
7009 && SSA_NAME_RANGE_INFO (lhs
))
7011 if (is_unreachable
== -1)
7014 if (single_pred_p (bb
)
7015 && assert_unreachable_fallthru_edge_p
7016 (single_pred_edge (bb
)))
7020 if (x_7 >= 10 && x_7 < 20)
7021 __builtin_unreachable ();
7022 x_8 = ASSERT_EXPR <x_7, ...>;
7023 if the only uses of x_7 are in the ASSERT_EXPR and
7024 in the condition. In that case, we can copy the
7025 range info from x_8 computed in this pass also
7028 && all_imm_uses_in_stmt_or_feed_cond (var
, stmt
,
7031 set_range_info (var
, SSA_NAME_RANGE_TYPE (lhs
),
7032 SSA_NAME_RANGE_INFO (lhs
)->get_min (),
7033 SSA_NAME_RANGE_INFO (lhs
)->get_max ());
7034 maybe_set_nonzero_bits (bb
, var
);
7038 /* Propagate the RHS into every use of the LHS. For SSA names
7039 also propagate abnormals as it merely restores the original
7040 IL in this case (an replace_uses_by would assert). */
7041 if (TREE_CODE (var
) == SSA_NAME
)
7043 imm_use_iterator iter
;
7044 use_operand_p use_p
;
7046 FOR_EACH_IMM_USE_STMT (use_stmt
, iter
, lhs
)
7047 FOR_EACH_IMM_USE_ON_STMT (use_p
, iter
)
7048 SET_USE (use_p
, var
);
7051 replace_uses_by (lhs
, var
);
7053 /* And finally, remove the copy, it is not needed. */
7054 gsi_remove (&si
, true);
7055 release_defs (stmt
);
7059 if (!is_gimple_debug (gsi_stmt (si
)))
7067 /* Return true if STMT is interesting for VRP. */
7070 stmt_interesting_for_vrp (gimple
*stmt
)
7072 if (gimple_code (stmt
) == GIMPLE_PHI
)
7074 tree res
= gimple_phi_result (stmt
);
7075 return (!virtual_operand_p (res
)
7076 && (INTEGRAL_TYPE_P (TREE_TYPE (res
))
7077 || POINTER_TYPE_P (TREE_TYPE (res
))));
7079 else if (is_gimple_assign (stmt
) || is_gimple_call (stmt
))
7081 tree lhs
= gimple_get_lhs (stmt
);
7083 /* In general, assignments with virtual operands are not useful
7084 for deriving ranges, with the obvious exception of calls to
7085 builtin functions. */
7086 if (lhs
&& TREE_CODE (lhs
) == SSA_NAME
7087 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs
))
7088 || POINTER_TYPE_P (TREE_TYPE (lhs
)))
7089 && (is_gimple_call (stmt
)
7090 || !gimple_vuse (stmt
)))
7092 else if (is_gimple_call (stmt
) && gimple_call_internal_p (stmt
))
7093 switch (gimple_call_internal_fn (stmt
))
7095 case IFN_ADD_OVERFLOW
:
7096 case IFN_SUB_OVERFLOW
:
7097 case IFN_MUL_OVERFLOW
:
7098 case IFN_ATOMIC_COMPARE_EXCHANGE
:
7099 /* These internal calls return _Complex integer type,
7100 but are interesting to VRP nevertheless. */
7101 if (lhs
&& TREE_CODE (lhs
) == SSA_NAME
)
7108 else if (gimple_code (stmt
) == GIMPLE_COND
7109 || gimple_code (stmt
) == GIMPLE_SWITCH
)
7115 /* Initialize VRP lattice. */
7118 vrp_initialize_lattice ()
7120 values_propagated
= false;
7121 num_vr_values
= num_ssa_names
;
7122 vr_value
= XCNEWVEC (value_range
*, num_vr_values
);
7123 vr_phi_edge_counts
= XCNEWVEC (int, num_ssa_names
);
7124 bitmap_obstack_initialize (&vrp_equiv_obstack
);
7127 /* Initialization required by ssa_propagate engine. */
7134 FOR_EACH_BB_FN (bb
, cfun
)
7136 for (gphi_iterator si
= gsi_start_phis (bb
); !gsi_end_p (si
);
7139 gphi
*phi
= si
.phi ();
7140 if (!stmt_interesting_for_vrp (phi
))
7142 tree lhs
= PHI_RESULT (phi
);
7143 set_value_range_to_varying (get_value_range (lhs
));
7144 prop_set_simulate_again (phi
, false);
7147 prop_set_simulate_again (phi
, true);
7150 for (gimple_stmt_iterator si
= gsi_start_bb (bb
); !gsi_end_p (si
);
7153 gimple
*stmt
= gsi_stmt (si
);
7155 /* If the statement is a control insn, then we do not
7156 want to avoid simulating the statement once. Failure
7157 to do so means that those edges will never get added. */
7158 if (stmt_ends_bb_p (stmt
))
7159 prop_set_simulate_again (stmt
, true);
7160 else if (!stmt_interesting_for_vrp (stmt
))
7162 set_defs_to_varying (stmt
);
7163 prop_set_simulate_again (stmt
, false);
7166 prop_set_simulate_again (stmt
, true);
7171 /* Return the singleton value-range for NAME or NAME. */
7174 vrp_valueize (tree name
)
7176 if (TREE_CODE (name
) == SSA_NAME
)
7178 value_range
*vr
= get_value_range (name
);
7179 if (vr
->type
== VR_RANGE
7180 && (TREE_CODE (vr
->min
) == SSA_NAME
7181 || is_gimple_min_invariant (vr
->min
))
7182 && vrp_operand_equal_p (vr
->min
, vr
->max
))
7188 /* Return the singleton value-range for NAME if that is a constant
7189 but signal to not follow SSA edges. */
7192 vrp_valueize_1 (tree name
)
7194 if (TREE_CODE (name
) == SSA_NAME
)
7196 /* If the definition may be simulated again we cannot follow
7197 this SSA edge as the SSA propagator does not necessarily
7198 re-visit the use. */
7199 gimple
*def_stmt
= SSA_NAME_DEF_STMT (name
);
7200 if (!gimple_nop_p (def_stmt
)
7201 && prop_simulate_again_p (def_stmt
))
7203 value_range
*vr
= get_value_range (name
);
7204 if (range_int_cst_singleton_p (vr
))
7210 /* Visit assignment STMT. If it produces an interesting range, record
7211 the range in VR and set LHS to OUTPUT_P. */
7214 vrp_visit_assignment_or_call (gimple
*stmt
, tree
*output_p
, value_range
*vr
)
7217 enum gimple_code code
= gimple_code (stmt
);
7218 lhs
= gimple_get_lhs (stmt
);
7219 *output_p
= NULL_TREE
;
7221 /* We only keep track of ranges in integral and pointer types. */
7222 if (TREE_CODE (lhs
) == SSA_NAME
7223 && ((INTEGRAL_TYPE_P (TREE_TYPE (lhs
))
7224 /* It is valid to have NULL MIN/MAX values on a type. See
7225 build_range_type. */
7226 && TYPE_MIN_VALUE (TREE_TYPE (lhs
))
7227 && TYPE_MAX_VALUE (TREE_TYPE (lhs
)))
7228 || POINTER_TYPE_P (TREE_TYPE (lhs
))))
7232 /* Try folding the statement to a constant first. */
7233 tree tem
= gimple_fold_stmt_to_constant_1 (stmt
, vrp_valueize
,
7237 if (TREE_CODE (tem
) == SSA_NAME
7238 && (SSA_NAME_IS_DEFAULT_DEF (tem
)
7239 || ! prop_simulate_again_p (SSA_NAME_DEF_STMT (tem
))))
7241 extract_range_from_ssa_name (vr
, tem
);
7244 else if (is_gimple_min_invariant (tem
))
7246 set_value_range_to_value (vr
, tem
, NULL
);
7250 /* Then dispatch to value-range extracting functions. */
7251 if (code
== GIMPLE_CALL
)
7252 extract_range_basic (vr
, stmt
);
7254 extract_range_from_assignment (vr
, as_a
<gassign
*> (stmt
));
7258 /* Helper that gets the value range of the SSA_NAME with version I
7259 or a symbolic range containing the SSA_NAME only if the value range
7260 is varying or undefined. */
7262 static inline value_range
7263 get_vr_for_comparison (int i
)
7265 value_range vr
= *get_value_range (ssa_name (i
));
7267 /* If name N_i does not have a valid range, use N_i as its own
7268 range. This allows us to compare against names that may
7269 have N_i in their ranges. */
7270 if (vr
.type
== VR_VARYING
|| vr
.type
== VR_UNDEFINED
)
7273 vr
.min
= ssa_name (i
);
7274 vr
.max
= ssa_name (i
);
7280 /* Compare all the value ranges for names equivalent to VAR with VAL
7281 using comparison code COMP. Return the same value returned by
7282 compare_range_with_value, including the setting of
7283 *STRICT_OVERFLOW_P. */
7286 compare_name_with_value (enum tree_code comp
, tree var
, tree val
,
7287 bool *strict_overflow_p
, bool use_equiv_p
)
7293 int used_strict_overflow
;
7295 value_range equiv_vr
;
7297 /* Get the set of equivalences for VAR. */
7298 e
= get_value_range (var
)->equiv
;
7300 /* Start at -1. Set it to 0 if we do a comparison without relying
7301 on overflow, or 1 if all comparisons rely on overflow. */
7302 used_strict_overflow
= -1;
7304 /* Compare vars' value range with val. */
7305 equiv_vr
= get_vr_for_comparison (SSA_NAME_VERSION (var
));
7307 retval
= compare_range_with_value (comp
, &equiv_vr
, val
, &sop
);
7309 used_strict_overflow
= sop
? 1 : 0;
7311 /* If the equiv set is empty we have done all work we need to do. */
7315 && used_strict_overflow
> 0)
7316 *strict_overflow_p
= true;
7320 EXECUTE_IF_SET_IN_BITMAP (e
, 0, i
, bi
)
7322 tree name
= ssa_name (i
);
7327 && ! SSA_NAME_IS_DEFAULT_DEF (name
)
7328 && prop_simulate_again_p (SSA_NAME_DEF_STMT (name
)))
7331 equiv_vr
= get_vr_for_comparison (i
);
7333 t
= compare_range_with_value (comp
, &equiv_vr
, val
, &sop
);
7336 /* If we get different answers from different members
7337 of the equivalence set this check must be in a dead
7338 code region. Folding it to a trap representation
7339 would be correct here. For now just return don't-know. */
7349 used_strict_overflow
= 0;
7350 else if (used_strict_overflow
< 0)
7351 used_strict_overflow
= 1;
7356 && used_strict_overflow
> 0)
7357 *strict_overflow_p
= true;
7363 /* Given a comparison code COMP and names N1 and N2, compare all the
7364 ranges equivalent to N1 against all the ranges equivalent to N2
7365 to determine the value of N1 COMP N2. Return the same value
7366 returned by compare_ranges. Set *STRICT_OVERFLOW_P to indicate
7367 whether we relied on undefined signed overflow in the comparison. */
7371 compare_names (enum tree_code comp
, tree n1
, tree n2
,
7372 bool *strict_overflow_p
)
7376 bitmap_iterator bi1
, bi2
;
7378 int used_strict_overflow
;
7379 static bitmap_obstack
*s_obstack
= NULL
;
7380 static bitmap s_e1
= NULL
, s_e2
= NULL
;
7382 /* Compare the ranges of every name equivalent to N1 against the
7383 ranges of every name equivalent to N2. */
7384 e1
= get_value_range (n1
)->equiv
;
7385 e2
= get_value_range (n2
)->equiv
;
7387 /* Use the fake bitmaps if e1 or e2 are not available. */
7388 if (s_obstack
== NULL
)
7390 s_obstack
= XNEW (bitmap_obstack
);
7391 bitmap_obstack_initialize (s_obstack
);
7392 s_e1
= BITMAP_ALLOC (s_obstack
);
7393 s_e2
= BITMAP_ALLOC (s_obstack
);
7400 /* Add N1 and N2 to their own set of equivalences to avoid
7401 duplicating the body of the loop just to check N1 and N2
7403 bitmap_set_bit (e1
, SSA_NAME_VERSION (n1
));
7404 bitmap_set_bit (e2
, SSA_NAME_VERSION (n2
));
7406 /* If the equivalence sets have a common intersection, then the two
7407 names can be compared without checking their ranges. */
7408 if (bitmap_intersect_p (e1
, e2
))
7410 bitmap_clear_bit (e1
, SSA_NAME_VERSION (n1
));
7411 bitmap_clear_bit (e2
, SSA_NAME_VERSION (n2
));
7413 return (comp
== EQ_EXPR
|| comp
== GE_EXPR
|| comp
== LE_EXPR
)
7415 : boolean_false_node
;
7418 /* Start at -1. Set it to 0 if we do a comparison without relying
7419 on overflow, or 1 if all comparisons rely on overflow. */
7420 used_strict_overflow
= -1;
7422 /* Otherwise, compare all the equivalent ranges. First, add N1 and
7423 N2 to their own set of equivalences to avoid duplicating the body
7424 of the loop just to check N1 and N2 ranges. */
7425 EXECUTE_IF_SET_IN_BITMAP (e1
, 0, i1
, bi1
)
7427 if (! ssa_name (i1
))
7430 value_range vr1
= get_vr_for_comparison (i1
);
7432 t
= retval
= NULL_TREE
;
7433 EXECUTE_IF_SET_IN_BITMAP (e2
, 0, i2
, bi2
)
7435 if (! ssa_name (i2
))
7440 value_range vr2
= get_vr_for_comparison (i2
);
7442 t
= compare_ranges (comp
, &vr1
, &vr2
, &sop
);
7445 /* If we get different answers from different members
7446 of the equivalence set this check must be in a dead
7447 code region. Folding it to a trap representation
7448 would be correct here. For now just return don't-know. */
7452 bitmap_clear_bit (e1
, SSA_NAME_VERSION (n1
));
7453 bitmap_clear_bit (e2
, SSA_NAME_VERSION (n2
));
7459 used_strict_overflow
= 0;
7460 else if (used_strict_overflow
< 0)
7461 used_strict_overflow
= 1;
7467 bitmap_clear_bit (e1
, SSA_NAME_VERSION (n1
));
7468 bitmap_clear_bit (e2
, SSA_NAME_VERSION (n2
));
7469 if (used_strict_overflow
> 0)
7470 *strict_overflow_p
= true;
7475 /* None of the equivalent ranges are useful in computing this
7477 bitmap_clear_bit (e1
, SSA_NAME_VERSION (n1
));
7478 bitmap_clear_bit (e2
, SSA_NAME_VERSION (n2
));
7482 /* Helper function for vrp_evaluate_conditional_warnv & other
7486 vrp_evaluate_conditional_warnv_with_ops_using_ranges (enum tree_code code
,
7488 bool * strict_overflow_p
)
7490 value_range
*vr0
, *vr1
;
7492 vr0
= (TREE_CODE (op0
) == SSA_NAME
) ? get_value_range (op0
) : NULL
;
7493 vr1
= (TREE_CODE (op1
) == SSA_NAME
) ? get_value_range (op1
) : NULL
;
7495 tree res
= NULL_TREE
;
7497 res
= compare_ranges (code
, vr0
, vr1
, strict_overflow_p
);
7499 res
= compare_range_with_value (code
, vr0
, op1
, strict_overflow_p
);
7501 res
= (compare_range_with_value
7502 (swap_tree_comparison (code
), vr1
, op0
, strict_overflow_p
));
7506 /* Helper function for vrp_evaluate_conditional_warnv. */
7509 vrp_evaluate_conditional_warnv_with_ops (enum tree_code code
, tree op0
,
7510 tree op1
, bool use_equiv_p
,
7511 bool *strict_overflow_p
, bool *only_ranges
)
7515 *only_ranges
= true;
7517 /* We only deal with integral and pointer types. */
7518 if (!INTEGRAL_TYPE_P (TREE_TYPE (op0
))
7519 && !POINTER_TYPE_P (TREE_TYPE (op0
)))
7522 /* If OP0 CODE OP1 is an overflow comparison, if it can be expressed
7523 as a simple equality test, then prefer that over its current form
7526 An overflow test which collapses to an equality test can always be
7527 expressed as a comparison of one argument against zero. Overflow
7528 occurs when the chosen argument is zero and does not occur if the
7529 chosen argument is not zero. */
7531 if (overflow_comparison_p (code
, op0
, op1
, use_equiv_p
, &x
))
7533 wide_int max
= wi::max_value (TYPE_PRECISION (TREE_TYPE (op0
)), UNSIGNED
);
7534 /* B = A - 1; if (A < B) -> B = A - 1; if (A == 0)
7535 B = A - 1; if (A > B) -> B = A - 1; if (A != 0)
7536 B = A + 1; if (B < A) -> B = A + 1; if (B == 0)
7537 B = A + 1; if (B > A) -> B = A + 1; if (B != 0) */
7538 if (integer_zerop (x
))
7541 code
= (code
== LT_EXPR
|| code
== LE_EXPR
) ? EQ_EXPR
: NE_EXPR
;
7543 /* B = A + 1; if (A > B) -> B = A + 1; if (B == 0)
7544 B = A + 1; if (A < B) -> B = A + 1; if (B != 0)
7545 B = A - 1; if (B > A) -> B = A - 1; if (A == 0)
7546 B = A - 1; if (B < A) -> B = A - 1; if (A != 0) */
7547 else if (wi::to_wide (x
) == max
- 1)
7550 op1
= wide_int_to_tree (TREE_TYPE (op0
), 0);
7551 code
= (code
== GT_EXPR
|| code
== GE_EXPR
) ? EQ_EXPR
: NE_EXPR
;
7555 if ((ret
= vrp_evaluate_conditional_warnv_with_ops_using_ranges
7556 (code
, op0
, op1
, strict_overflow_p
)))
7559 *only_ranges
= false;
7560 /* Do not use compare_names during propagation, it's quadratic. */
7561 if (TREE_CODE (op0
) == SSA_NAME
&& TREE_CODE (op1
) == SSA_NAME
7563 return compare_names (code
, op0
, op1
, strict_overflow_p
);
7564 else if (TREE_CODE (op0
) == SSA_NAME
)
7565 return compare_name_with_value (code
, op0
, op1
,
7566 strict_overflow_p
, use_equiv_p
);
7567 else if (TREE_CODE (op1
) == SSA_NAME
)
7568 return compare_name_with_value (swap_tree_comparison (code
), op1
, op0
,
7569 strict_overflow_p
, use_equiv_p
);
7573 /* Given (CODE OP0 OP1) within STMT, try to simplify it based on value range
7574 information. Return NULL if the conditional can not be evaluated.
7575 The ranges of all the names equivalent with the operands in COND
7576 will be used when trying to compute the value. If the result is
7577 based on undefined signed overflow, issue a warning if
7581 vrp_evaluate_conditional (tree_code code
, tree op0
, tree op1
, gimple
*stmt
)
7587 /* Some passes and foldings leak constants with overflow flag set
7588 into the IL. Avoid doing wrong things with these and bail out. */
7589 if ((TREE_CODE (op0
) == INTEGER_CST
7590 && TREE_OVERFLOW (op0
))
7591 || (TREE_CODE (op1
) == INTEGER_CST
7592 && TREE_OVERFLOW (op1
)))
7596 ret
= vrp_evaluate_conditional_warnv_with_ops (code
, op0
, op1
, true, &sop
,
7601 enum warn_strict_overflow_code wc
;
7602 const char* warnmsg
;
7604 if (is_gimple_min_invariant (ret
))
7606 wc
= WARN_STRICT_OVERFLOW_CONDITIONAL
;
7607 warnmsg
= G_("assuming signed overflow does not occur when "
7608 "simplifying conditional to constant");
7612 wc
= WARN_STRICT_OVERFLOW_COMPARISON
;
7613 warnmsg
= G_("assuming signed overflow does not occur when "
7614 "simplifying conditional");
7617 if (issue_strict_overflow_warning (wc
))
7619 location_t location
;
7621 if (!gimple_has_location (stmt
))
7622 location
= input_location
;
7624 location
= gimple_location (stmt
);
7625 warning_at (location
, OPT_Wstrict_overflow
, "%s", warnmsg
);
7629 if (warn_type_limits
7630 && ret
&& only_ranges
7631 && TREE_CODE_CLASS (code
) == tcc_comparison
7632 && TREE_CODE (op0
) == SSA_NAME
)
7634 /* If the comparison is being folded and the operand on the LHS
7635 is being compared against a constant value that is outside of
7636 the natural range of OP0's type, then the predicate will
7637 always fold regardless of the value of OP0. If -Wtype-limits
7638 was specified, emit a warning. */
7639 tree type
= TREE_TYPE (op0
);
7640 value_range
*vr0
= get_value_range (op0
);
7642 if (vr0
->type
== VR_RANGE
7643 && INTEGRAL_TYPE_P (type
)
7644 && vrp_val_is_min (vr0
->min
)
7645 && vrp_val_is_max (vr0
->max
)
7646 && is_gimple_min_invariant (op1
))
7648 location_t location
;
7650 if (!gimple_has_location (stmt
))
7651 location
= input_location
;
7653 location
= gimple_location (stmt
);
7655 warning_at (location
, OPT_Wtype_limits
,
7657 ? G_("comparison always false "
7658 "due to limited range of data type")
7659 : G_("comparison always true "
7660 "due to limited range of data type"));
7668 /* Visit conditional statement STMT. If we can determine which edge
7669 will be taken out of STMT's basic block, record it in
7670 *TAKEN_EDGE_P. Otherwise, set *TAKEN_EDGE_P to NULL. */
7673 vrp_visit_cond_stmt (gcond
*stmt
, edge
*taken_edge_p
)
7677 *taken_edge_p
= NULL
;
7679 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7684 fprintf (dump_file
, "\nVisiting conditional with predicate: ");
7685 print_gimple_stmt (dump_file
, stmt
, 0);
7686 fprintf (dump_file
, "\nWith known ranges\n");
7688 FOR_EACH_SSA_TREE_OPERAND (use
, stmt
, i
, SSA_OP_USE
)
7690 fprintf (dump_file
, "\t");
7691 print_generic_expr (dump_file
, use
);
7692 fprintf (dump_file
, ": ");
7693 dump_value_range (dump_file
, vr_value
[SSA_NAME_VERSION (use
)]);
7696 fprintf (dump_file
, "\n");
7699 /* Compute the value of the predicate COND by checking the known
7700 ranges of each of its operands.
7702 Note that we cannot evaluate all the equivalent ranges here
7703 because those ranges may not yet be final and with the current
7704 propagation strategy, we cannot determine when the value ranges
7705 of the names in the equivalence set have changed.
7707 For instance, given the following code fragment
7711 i_14 = ASSERT_EXPR <i_5, i_5 != 0>
7715 Assume that on the first visit to i_14, i_5 has the temporary
7716 range [8, 8] because the second argument to the PHI function is
7717 not yet executable. We derive the range ~[0, 0] for i_14 and the
7718 equivalence set { i_5 }. So, when we visit 'if (i_14 == 1)' for
7719 the first time, since i_14 is equivalent to the range [8, 8], we
7720 determine that the predicate is always false.
7722 On the next round of propagation, i_13 is determined to be
7723 VARYING, which causes i_5 to drop down to VARYING. So, another
7724 visit to i_14 is scheduled. In this second visit, we compute the
7725 exact same range and equivalence set for i_14, namely ~[0, 0] and
7726 { i_5 }. But we did not have the previous range for i_5
7727 registered, so vrp_visit_assignment thinks that the range for
7728 i_14 has not changed. Therefore, the predicate 'if (i_14 == 1)'
7729 is not visited again, which stops propagation from visiting
7730 statements in the THEN clause of that if().
7732 To properly fix this we would need to keep the previous range
7733 value for the names in the equivalence set. This way we would've
7734 discovered that from one visit to the other i_5 changed from
7735 range [8, 8] to VR_VARYING.
7737 However, fixing this apparent limitation may not be worth the
7738 additional checking. Testing on several code bases (GCC, DLV,
7739 MICO, TRAMP3D and SPEC2000) showed that doing this results in
7740 4 more predicates folded in SPEC. */
7743 val
= vrp_evaluate_conditional_warnv_with_ops (gimple_cond_code (stmt
),
7744 gimple_cond_lhs (stmt
),
7745 gimple_cond_rhs (stmt
),
7748 *taken_edge_p
= find_taken_edge (gimple_bb (stmt
), val
);
7750 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7752 fprintf (dump_file
, "\nPredicate evaluates to: ");
7753 if (val
== NULL_TREE
)
7754 fprintf (dump_file
, "DON'T KNOW\n");
7756 print_generic_stmt (dump_file
, val
);
7760 /* Searches the case label vector VEC for the index *IDX of the CASE_LABEL
7761 that includes the value VAL. The search is restricted to the range
7762 [START_IDX, n - 1] where n is the size of VEC.
7764 If there is a CASE_LABEL for VAL, its index is placed in IDX and true is
7767 If there is no CASE_LABEL for VAL and there is one that is larger than VAL,
7768 it is placed in IDX and false is returned.
7770 If VAL is larger than any CASE_LABEL, n is placed on IDX and false is
7774 find_case_label_index (gswitch
*stmt
, size_t start_idx
, tree val
, size_t *idx
)
7776 size_t n
= gimple_switch_num_labels (stmt
);
7779 /* Find case label for minimum of the value range or the next one.
7780 At each iteration we are searching in [low, high - 1]. */
7782 for (low
= start_idx
, high
= n
; high
!= low
; )
7786 /* Note that i != high, so we never ask for n. */
7787 size_t i
= (high
+ low
) / 2;
7788 t
= gimple_switch_label (stmt
, i
);
7790 /* Cache the result of comparing CASE_LOW and val. */
7791 cmp
= tree_int_cst_compare (CASE_LOW (t
), val
);
7795 /* Ranges cannot be empty. */
7804 if (CASE_HIGH (t
) != NULL
7805 && tree_int_cst_compare (CASE_HIGH (t
), val
) >= 0)
7817 /* Searches the case label vector VEC for the range of CASE_LABELs that is used
7818 for values between MIN and MAX. The first index is placed in MIN_IDX. The
7819 last index is placed in MAX_IDX. If the range of CASE_LABELs is empty
7820 then MAX_IDX < MIN_IDX.
7821 Returns true if the default label is not needed. */
7824 find_case_label_range (gswitch
*stmt
, tree min
, tree max
, size_t *min_idx
,
7828 bool min_take_default
= !find_case_label_index (stmt
, 1, min
, &i
);
7829 bool max_take_default
= !find_case_label_index (stmt
, i
, max
, &j
);
7833 && max_take_default
)
7835 /* Only the default case label reached.
7836 Return an empty range. */
7843 bool take_default
= min_take_default
|| max_take_default
;
7847 if (max_take_default
)
7850 /* If the case label range is continuous, we do not need
7851 the default case label. Verify that. */
7852 high
= CASE_LOW (gimple_switch_label (stmt
, i
));
7853 if (CASE_HIGH (gimple_switch_label (stmt
, i
)))
7854 high
= CASE_HIGH (gimple_switch_label (stmt
, i
));
7855 for (k
= i
+ 1; k
<= j
; ++k
)
7857 low
= CASE_LOW (gimple_switch_label (stmt
, k
));
7858 if (!integer_onep (int_const_binop (MINUS_EXPR
, low
, high
)))
7860 take_default
= true;
7864 if (CASE_HIGH (gimple_switch_label (stmt
, k
)))
7865 high
= CASE_HIGH (gimple_switch_label (stmt
, k
));
7870 return !take_default
;
7874 /* Searches the case label vector VEC for the ranges of CASE_LABELs that are
7875 used in range VR. The indices are placed in MIN_IDX1, MAX_IDX, MIN_IDX2 and
7876 MAX_IDX2. If the ranges of CASE_LABELs are empty then MAX_IDX1 < MIN_IDX1.
7877 Returns true if the default label is not needed. */
7880 find_case_label_ranges (gswitch
*stmt
, value_range
*vr
, size_t *min_idx1
,
7881 size_t *max_idx1
, size_t *min_idx2
,
7885 unsigned int n
= gimple_switch_num_labels (stmt
);
7887 tree case_low
, case_high
;
7888 tree min
= vr
->min
, max
= vr
->max
;
7890 gcc_checking_assert (vr
->type
== VR_RANGE
|| vr
->type
== VR_ANTI_RANGE
);
7892 take_default
= !find_case_label_range (stmt
, min
, max
, &i
, &j
);
7894 /* Set second range to emtpy. */
7898 if (vr
->type
== VR_RANGE
)
7902 return !take_default
;
7905 /* Set first range to all case labels. */
7912 /* Make sure all the values of case labels [i , j] are contained in
7913 range [MIN, MAX]. */
7914 case_low
= CASE_LOW (gimple_switch_label (stmt
, i
));
7915 case_high
= CASE_HIGH (gimple_switch_label (stmt
, j
));
7916 if (tree_int_cst_compare (case_low
, min
) < 0)
7918 if (case_high
!= NULL_TREE
7919 && tree_int_cst_compare (max
, case_high
) < 0)
7925 /* If the range spans case labels [i, j], the corresponding anti-range spans
7926 the labels [1, i - 1] and [j + 1, n - 1]. */
7952 /* Visit switch statement STMT. If we can determine which edge
7953 will be taken out of STMT's basic block, record it in
7954 *TAKEN_EDGE_P. Otherwise, *TAKEN_EDGE_P set to NULL. */
7957 vrp_visit_switch_stmt (gswitch
*stmt
, edge
*taken_edge_p
)
7961 size_t i
= 0, j
= 0, k
, l
;
7964 *taken_edge_p
= NULL
;
7965 op
= gimple_switch_index (stmt
);
7966 if (TREE_CODE (op
) != SSA_NAME
)
7969 vr
= get_value_range (op
);
7970 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7972 fprintf (dump_file
, "\nVisiting switch expression with operand ");
7973 print_generic_expr (dump_file
, op
);
7974 fprintf (dump_file
, " with known range ");
7975 dump_value_range (dump_file
, vr
);
7976 fprintf (dump_file
, "\n");
7979 if ((vr
->type
!= VR_RANGE
7980 && vr
->type
!= VR_ANTI_RANGE
)
7981 || symbolic_range_p (vr
))
7984 /* Find the single edge that is taken from the switch expression. */
7985 take_default
= !find_case_label_ranges (stmt
, vr
, &i
, &j
, &k
, &l
);
7987 /* Check if the range spans no CASE_LABEL. If so, we only reach the default
7991 gcc_assert (take_default
);
7992 val
= gimple_switch_default_label (stmt
);
7996 /* Check if labels with index i to j and maybe the default label
7997 are all reaching the same label. */
7999 val
= gimple_switch_label (stmt
, i
);
8001 && CASE_LABEL (gimple_switch_default_label (stmt
))
8002 != CASE_LABEL (val
))
8004 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8005 fprintf (dump_file
, " not a single destination for this "
8009 for (++i
; i
<= j
; ++i
)
8011 if (CASE_LABEL (gimple_switch_label (stmt
, i
)) != CASE_LABEL (val
))
8013 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8014 fprintf (dump_file
, " not a single destination for this "
8021 if (CASE_LABEL (gimple_switch_label (stmt
, k
)) != CASE_LABEL (val
))
8023 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8024 fprintf (dump_file
, " not a single destination for this "
8031 *taken_edge_p
= find_edge (gimple_bb (stmt
),
8032 label_to_block (CASE_LABEL (val
)));
8034 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8036 fprintf (dump_file
, " will take edge to ");
8037 print_generic_stmt (dump_file
, CASE_LABEL (val
));
8042 /* Evaluate statement STMT. If the statement produces a useful range,
8043 set VR and corepsponding OUTPUT_P.
8045 If STMT is a conditional branch and we can determine its truth
8046 value, the taken edge is recorded in *TAKEN_EDGE_P. */
8049 extract_range_from_stmt (gimple
*stmt
, edge
*taken_edge_p
,
8050 tree
*output_p
, value_range
*vr
)
8053 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8055 fprintf (dump_file
, "\nVisiting statement:\n");
8056 print_gimple_stmt (dump_file
, stmt
, 0, dump_flags
);
8059 if (!stmt_interesting_for_vrp (stmt
))
8060 gcc_assert (stmt_ends_bb_p (stmt
));
8061 else if (is_gimple_assign (stmt
) || is_gimple_call (stmt
))
8062 vrp_visit_assignment_or_call (stmt
, output_p
, vr
);
8063 else if (gimple_code (stmt
) == GIMPLE_COND
)
8064 vrp_visit_cond_stmt (as_a
<gcond
*> (stmt
), taken_edge_p
);
8065 else if (gimple_code (stmt
) == GIMPLE_SWITCH
)
8066 vrp_visit_switch_stmt (as_a
<gswitch
*> (stmt
), taken_edge_p
);
8069 class vrp_prop
: public ssa_propagation_engine
8072 enum ssa_prop_result
visit_stmt (gimple
*, edge
*, tree
*) FINAL OVERRIDE
;
8073 enum ssa_prop_result
visit_phi (gphi
*) FINAL OVERRIDE
;
8076 /* Evaluate statement STMT. If the statement produces a useful range,
8077 return SSA_PROP_INTERESTING and record the SSA name with the
8078 interesting range into *OUTPUT_P.
8080 If STMT is a conditional branch and we can determine its truth
8081 value, the taken edge is recorded in *TAKEN_EDGE_P.
8083 If STMT produces a varying value, return SSA_PROP_VARYING. */
8085 enum ssa_prop_result
8086 vrp_prop::visit_stmt (gimple
*stmt
, edge
*taken_edge_p
, tree
*output_p
)
8088 value_range vr
= VR_INITIALIZER
;
8089 tree lhs
= gimple_get_lhs (stmt
);
8090 extract_range_from_stmt (stmt
, taken_edge_p
, output_p
, &vr
);
8094 if (update_value_range (*output_p
, &vr
))
8096 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8098 fprintf (dump_file
, "Found new range for ");
8099 print_generic_expr (dump_file
, *output_p
);
8100 fprintf (dump_file
, ": ");
8101 dump_value_range (dump_file
, &vr
);
8102 fprintf (dump_file
, "\n");
8105 if (vr
.type
== VR_VARYING
)
8106 return SSA_PROP_VARYING
;
8108 return SSA_PROP_INTERESTING
;
8110 return SSA_PROP_NOT_INTERESTING
;
8113 if (is_gimple_call (stmt
) && gimple_call_internal_p (stmt
))
8114 switch (gimple_call_internal_fn (stmt
))
8116 case IFN_ADD_OVERFLOW
:
8117 case IFN_SUB_OVERFLOW
:
8118 case IFN_MUL_OVERFLOW
:
8119 case IFN_ATOMIC_COMPARE_EXCHANGE
:
8120 /* These internal calls return _Complex integer type,
8121 which VRP does not track, but the immediate uses
8122 thereof might be interesting. */
8123 if (lhs
&& TREE_CODE (lhs
) == SSA_NAME
)
8125 imm_use_iterator iter
;
8126 use_operand_p use_p
;
8127 enum ssa_prop_result res
= SSA_PROP_VARYING
;
8129 set_value_range_to_varying (get_value_range (lhs
));
8131 FOR_EACH_IMM_USE_FAST (use_p
, iter
, lhs
)
8133 gimple
*use_stmt
= USE_STMT (use_p
);
8134 if (!is_gimple_assign (use_stmt
))
8136 enum tree_code rhs_code
= gimple_assign_rhs_code (use_stmt
);
8137 if (rhs_code
!= REALPART_EXPR
&& rhs_code
!= IMAGPART_EXPR
)
8139 tree rhs1
= gimple_assign_rhs1 (use_stmt
);
8140 tree use_lhs
= gimple_assign_lhs (use_stmt
);
8141 if (TREE_CODE (rhs1
) != rhs_code
8142 || TREE_OPERAND (rhs1
, 0) != lhs
8143 || TREE_CODE (use_lhs
) != SSA_NAME
8144 || !stmt_interesting_for_vrp (use_stmt
)
8145 || (!INTEGRAL_TYPE_P (TREE_TYPE (use_lhs
))
8146 || !TYPE_MIN_VALUE (TREE_TYPE (use_lhs
))
8147 || !TYPE_MAX_VALUE (TREE_TYPE (use_lhs
))))
8150 /* If there is a change in the value range for any of the
8151 REALPART_EXPR/IMAGPART_EXPR immediate uses, return
8152 SSA_PROP_INTERESTING. If there are any REALPART_EXPR
8153 or IMAGPART_EXPR immediate uses, but none of them have
8154 a change in their value ranges, return
8155 SSA_PROP_NOT_INTERESTING. If there are no
8156 {REAL,IMAG}PART_EXPR uses at all,
8157 return SSA_PROP_VARYING. */
8158 value_range new_vr
= VR_INITIALIZER
;
8159 extract_range_basic (&new_vr
, use_stmt
);
8160 value_range
*old_vr
= get_value_range (use_lhs
);
8161 if (old_vr
->type
!= new_vr
.type
8162 || !vrp_operand_equal_p (old_vr
->min
, new_vr
.min
)
8163 || !vrp_operand_equal_p (old_vr
->max
, new_vr
.max
)
8164 || !vrp_bitmap_equal_p (old_vr
->equiv
, new_vr
.equiv
))
8165 res
= SSA_PROP_INTERESTING
;
8167 res
= SSA_PROP_NOT_INTERESTING
;
8168 BITMAP_FREE (new_vr
.equiv
);
8169 if (res
== SSA_PROP_INTERESTING
)
8183 /* All other statements produce nothing of interest for VRP, so mark
8184 their outputs varying and prevent further simulation. */
8185 set_defs_to_varying (stmt
);
8187 return (*taken_edge_p
) ? SSA_PROP_INTERESTING
: SSA_PROP_VARYING
;
8190 /* Union the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
8191 { VR1TYPE, VR0MIN, VR0MAX } and store the result
8192 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
8193 possible such range. The resulting range is not canonicalized. */
8196 union_ranges (enum value_range_type
*vr0type
,
8197 tree
*vr0min
, tree
*vr0max
,
8198 enum value_range_type vr1type
,
8199 tree vr1min
, tree vr1max
)
8201 bool mineq
= vrp_operand_equal_p (*vr0min
, vr1min
);
8202 bool maxeq
= vrp_operand_equal_p (*vr0max
, vr1max
);
8204 /* [] is vr0, () is vr1 in the following classification comments. */
8208 if (*vr0type
== vr1type
)
8209 /* Nothing to do for equal ranges. */
8211 else if ((*vr0type
== VR_RANGE
8212 && vr1type
== VR_ANTI_RANGE
)
8213 || (*vr0type
== VR_ANTI_RANGE
8214 && vr1type
== VR_RANGE
))
8216 /* For anti-range with range union the result is varying. */
8222 else if (operand_less_p (*vr0max
, vr1min
) == 1
8223 || operand_less_p (vr1max
, *vr0min
) == 1)
8225 /* [ ] ( ) or ( ) [ ]
8226 If the ranges have an empty intersection, result of the union
8227 operation is the anti-range or if both are anti-ranges
8229 if (*vr0type
== VR_ANTI_RANGE
8230 && vr1type
== VR_ANTI_RANGE
)
8232 else if (*vr0type
== VR_ANTI_RANGE
8233 && vr1type
== VR_RANGE
)
8235 else if (*vr0type
== VR_RANGE
8236 && vr1type
== VR_ANTI_RANGE
)
8242 else if (*vr0type
== VR_RANGE
8243 && vr1type
== VR_RANGE
)
8245 /* The result is the convex hull of both ranges. */
8246 if (operand_less_p (*vr0max
, vr1min
) == 1)
8248 /* If the result can be an anti-range, create one. */
8249 if (TREE_CODE (*vr0max
) == INTEGER_CST
8250 && TREE_CODE (vr1min
) == INTEGER_CST
8251 && vrp_val_is_min (*vr0min
)
8252 && vrp_val_is_max (vr1max
))
8254 tree min
= int_const_binop (PLUS_EXPR
,
8256 build_int_cst (TREE_TYPE (*vr0max
), 1));
8257 tree max
= int_const_binop (MINUS_EXPR
,
8259 build_int_cst (TREE_TYPE (vr1min
), 1));
8260 if (!operand_less_p (max
, min
))
8262 *vr0type
= VR_ANTI_RANGE
;
8274 /* If the result can be an anti-range, create one. */
8275 if (TREE_CODE (vr1max
) == INTEGER_CST
8276 && TREE_CODE (*vr0min
) == INTEGER_CST
8277 && vrp_val_is_min (vr1min
)
8278 && vrp_val_is_max (*vr0max
))
8280 tree min
= int_const_binop (PLUS_EXPR
,
8282 build_int_cst (TREE_TYPE (vr1max
), 1));
8283 tree max
= int_const_binop (MINUS_EXPR
,
8285 build_int_cst (TREE_TYPE (*vr0min
), 1));
8286 if (!operand_less_p (max
, min
))
8288 *vr0type
= VR_ANTI_RANGE
;
8302 else if ((maxeq
|| operand_less_p (vr1max
, *vr0max
) == 1)
8303 && (mineq
|| operand_less_p (*vr0min
, vr1min
) == 1))
8305 /* [ ( ) ] or [( ) ] or [ ( )] */
8306 if (*vr0type
== VR_RANGE
8307 && vr1type
== VR_RANGE
)
8309 else if (*vr0type
== VR_ANTI_RANGE
8310 && vr1type
== VR_ANTI_RANGE
)
8316 else if (*vr0type
== VR_ANTI_RANGE
8317 && vr1type
== VR_RANGE
)
8319 /* Arbitrarily choose the right or left gap. */
8320 if (!mineq
&& TREE_CODE (vr1min
) == INTEGER_CST
)
8321 *vr0max
= int_const_binop (MINUS_EXPR
, vr1min
,
8322 build_int_cst (TREE_TYPE (vr1min
), 1));
8323 else if (!maxeq
&& TREE_CODE (vr1max
) == INTEGER_CST
)
8324 *vr0min
= int_const_binop (PLUS_EXPR
, vr1max
,
8325 build_int_cst (TREE_TYPE (vr1max
), 1));
8329 else if (*vr0type
== VR_RANGE
8330 && vr1type
== VR_ANTI_RANGE
)
8331 /* The result covers everything. */
8336 else if ((maxeq
|| operand_less_p (*vr0max
, vr1max
) == 1)
8337 && (mineq
|| operand_less_p (vr1min
, *vr0min
) == 1))
8339 /* ( [ ] ) or ([ ] ) or ( [ ]) */
8340 if (*vr0type
== VR_RANGE
8341 && vr1type
== VR_RANGE
)
8347 else if (*vr0type
== VR_ANTI_RANGE
8348 && vr1type
== VR_ANTI_RANGE
)
8350 else if (*vr0type
== VR_RANGE
8351 && vr1type
== VR_ANTI_RANGE
)
8353 *vr0type
= VR_ANTI_RANGE
;
8354 if (!mineq
&& TREE_CODE (*vr0min
) == INTEGER_CST
)
8356 *vr0max
= int_const_binop (MINUS_EXPR
, *vr0min
,
8357 build_int_cst (TREE_TYPE (*vr0min
), 1));
8360 else if (!maxeq
&& TREE_CODE (*vr0max
) == INTEGER_CST
)
8362 *vr0min
= int_const_binop (PLUS_EXPR
, *vr0max
,
8363 build_int_cst (TREE_TYPE (*vr0max
), 1));
8369 else if (*vr0type
== VR_ANTI_RANGE
8370 && vr1type
== VR_RANGE
)
8371 /* The result covers everything. */
8376 else if ((operand_less_p (vr1min
, *vr0max
) == 1
8377 || operand_equal_p (vr1min
, *vr0max
, 0))
8378 && operand_less_p (*vr0min
, vr1min
) == 1
8379 && operand_less_p (*vr0max
, vr1max
) == 1)
8381 /* [ ( ] ) or [ ]( ) */
8382 if (*vr0type
== VR_RANGE
8383 && vr1type
== VR_RANGE
)
8385 else if (*vr0type
== VR_ANTI_RANGE
8386 && vr1type
== VR_ANTI_RANGE
)
8388 else if (*vr0type
== VR_ANTI_RANGE
8389 && vr1type
== VR_RANGE
)
8391 if (TREE_CODE (vr1min
) == INTEGER_CST
)
8392 *vr0max
= int_const_binop (MINUS_EXPR
, vr1min
,
8393 build_int_cst (TREE_TYPE (vr1min
), 1));
8397 else if (*vr0type
== VR_RANGE
8398 && vr1type
== VR_ANTI_RANGE
)
8400 if (TREE_CODE (*vr0max
) == INTEGER_CST
)
8403 *vr0min
= int_const_binop (PLUS_EXPR
, *vr0max
,
8404 build_int_cst (TREE_TYPE (*vr0max
), 1));
8413 else if ((operand_less_p (*vr0min
, vr1max
) == 1
8414 || operand_equal_p (*vr0min
, vr1max
, 0))
8415 && operand_less_p (vr1min
, *vr0min
) == 1
8416 && operand_less_p (vr1max
, *vr0max
) == 1)
8418 /* ( [ ) ] or ( )[ ] */
8419 if (*vr0type
== VR_RANGE
8420 && vr1type
== VR_RANGE
)
8422 else if (*vr0type
== VR_ANTI_RANGE
8423 && vr1type
== VR_ANTI_RANGE
)
8425 else if (*vr0type
== VR_ANTI_RANGE
8426 && vr1type
== VR_RANGE
)
8428 if (TREE_CODE (vr1max
) == INTEGER_CST
)
8429 *vr0min
= int_const_binop (PLUS_EXPR
, vr1max
,
8430 build_int_cst (TREE_TYPE (vr1max
), 1));
8434 else if (*vr0type
== VR_RANGE
8435 && vr1type
== VR_ANTI_RANGE
)
8437 if (TREE_CODE (*vr0min
) == INTEGER_CST
)
8441 *vr0max
= int_const_binop (MINUS_EXPR
, *vr0min
,
8442 build_int_cst (TREE_TYPE (*vr0min
), 1));
8456 *vr0type
= VR_VARYING
;
8457 *vr0min
= NULL_TREE
;
8458 *vr0max
= NULL_TREE
;
8461 /* Intersect the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
8462 { VR1TYPE, VR0MIN, VR0MAX } and store the result
8463 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
8464 possible such range. The resulting range is not canonicalized. */
8467 intersect_ranges (enum value_range_type
*vr0type
,
8468 tree
*vr0min
, tree
*vr0max
,
8469 enum value_range_type vr1type
,
8470 tree vr1min
, tree vr1max
)
8472 bool mineq
= vrp_operand_equal_p (*vr0min
, vr1min
);
8473 bool maxeq
= vrp_operand_equal_p (*vr0max
, vr1max
);
8475 /* [] is vr0, () is vr1 in the following classification comments. */
8479 if (*vr0type
== vr1type
)
8480 /* Nothing to do for equal ranges. */
8482 else if ((*vr0type
== VR_RANGE
8483 && vr1type
== VR_ANTI_RANGE
)
8484 || (*vr0type
== VR_ANTI_RANGE
8485 && vr1type
== VR_RANGE
))
8487 /* For anti-range with range intersection the result is empty. */
8488 *vr0type
= VR_UNDEFINED
;
8489 *vr0min
= NULL_TREE
;
8490 *vr0max
= NULL_TREE
;
8495 else if (operand_less_p (*vr0max
, vr1min
) == 1
8496 || operand_less_p (vr1max
, *vr0min
) == 1)
8498 /* [ ] ( ) or ( ) [ ]
8499 If the ranges have an empty intersection, the result of the
8500 intersect operation is the range for intersecting an
8501 anti-range with a range or empty when intersecting two ranges. */
8502 if (*vr0type
== VR_RANGE
8503 && vr1type
== VR_ANTI_RANGE
)
8505 else if (*vr0type
== VR_ANTI_RANGE
8506 && vr1type
== VR_RANGE
)
8512 else if (*vr0type
== VR_RANGE
8513 && vr1type
== VR_RANGE
)
8515 *vr0type
= VR_UNDEFINED
;
8516 *vr0min
= NULL_TREE
;
8517 *vr0max
= NULL_TREE
;
8519 else if (*vr0type
== VR_ANTI_RANGE
8520 && vr1type
== VR_ANTI_RANGE
)
8522 /* If the anti-ranges are adjacent to each other merge them. */
8523 if (TREE_CODE (*vr0max
) == INTEGER_CST
8524 && TREE_CODE (vr1min
) == INTEGER_CST
8525 && operand_less_p (*vr0max
, vr1min
) == 1
8526 && integer_onep (int_const_binop (MINUS_EXPR
,
8529 else if (TREE_CODE (vr1max
) == INTEGER_CST
8530 && TREE_CODE (*vr0min
) == INTEGER_CST
8531 && operand_less_p (vr1max
, *vr0min
) == 1
8532 && integer_onep (int_const_binop (MINUS_EXPR
,
8535 /* Else arbitrarily take VR0. */
8538 else if ((maxeq
|| operand_less_p (vr1max
, *vr0max
) == 1)
8539 && (mineq
|| operand_less_p (*vr0min
, vr1min
) == 1))
8541 /* [ ( ) ] or [( ) ] or [ ( )] */
8542 if (*vr0type
== VR_RANGE
8543 && vr1type
== VR_RANGE
)
8545 /* If both are ranges the result is the inner one. */
8550 else if (*vr0type
== VR_RANGE
8551 && vr1type
== VR_ANTI_RANGE
)
8553 /* Choose the right gap if the left one is empty. */
8556 if (TREE_CODE (vr1max
) != INTEGER_CST
)
8558 else if (TYPE_PRECISION (TREE_TYPE (vr1max
)) == 1
8559 && !TYPE_UNSIGNED (TREE_TYPE (vr1max
)))
8561 = int_const_binop (MINUS_EXPR
, vr1max
,
8562 build_int_cst (TREE_TYPE (vr1max
), -1));
8565 = int_const_binop (PLUS_EXPR
, vr1max
,
8566 build_int_cst (TREE_TYPE (vr1max
), 1));
8568 /* Choose the left gap if the right one is empty. */
8571 if (TREE_CODE (vr1min
) != INTEGER_CST
)
8573 else if (TYPE_PRECISION (TREE_TYPE (vr1min
)) == 1
8574 && !TYPE_UNSIGNED (TREE_TYPE (vr1min
)))
8576 = int_const_binop (PLUS_EXPR
, vr1min
,
8577 build_int_cst (TREE_TYPE (vr1min
), -1));
8580 = int_const_binop (MINUS_EXPR
, vr1min
,
8581 build_int_cst (TREE_TYPE (vr1min
), 1));
8583 /* Choose the anti-range if the range is effectively varying. */
8584 else if (vrp_val_is_min (*vr0min
)
8585 && vrp_val_is_max (*vr0max
))
8591 /* Else choose the range. */
8593 else if (*vr0type
== VR_ANTI_RANGE
8594 && vr1type
== VR_ANTI_RANGE
)
8595 /* If both are anti-ranges the result is the outer one. */
8597 else if (*vr0type
== VR_ANTI_RANGE
8598 && vr1type
== VR_RANGE
)
8600 /* The intersection is empty. */
8601 *vr0type
= VR_UNDEFINED
;
8602 *vr0min
= NULL_TREE
;
8603 *vr0max
= NULL_TREE
;
8608 else if ((maxeq
|| operand_less_p (*vr0max
, vr1max
) == 1)
8609 && (mineq
|| operand_less_p (vr1min
, *vr0min
) == 1))
8611 /* ( [ ] ) or ([ ] ) or ( [ ]) */
8612 if (*vr0type
== VR_RANGE
8613 && vr1type
== VR_RANGE
)
8614 /* Choose the inner range. */
8616 else if (*vr0type
== VR_ANTI_RANGE
8617 && vr1type
== VR_RANGE
)
8619 /* Choose the right gap if the left is empty. */
8622 *vr0type
= VR_RANGE
;
8623 if (TREE_CODE (*vr0max
) != INTEGER_CST
)
8625 else if (TYPE_PRECISION (TREE_TYPE (*vr0max
)) == 1
8626 && !TYPE_UNSIGNED (TREE_TYPE (*vr0max
)))
8628 = int_const_binop (MINUS_EXPR
, *vr0max
,
8629 build_int_cst (TREE_TYPE (*vr0max
), -1));
8632 = int_const_binop (PLUS_EXPR
, *vr0max
,
8633 build_int_cst (TREE_TYPE (*vr0max
), 1));
8636 /* Choose the left gap if the right is empty. */
8639 *vr0type
= VR_RANGE
;
8640 if (TREE_CODE (*vr0min
) != INTEGER_CST
)
8642 else if (TYPE_PRECISION (TREE_TYPE (*vr0min
)) == 1
8643 && !TYPE_UNSIGNED (TREE_TYPE (*vr0min
)))
8645 = int_const_binop (PLUS_EXPR
, *vr0min
,
8646 build_int_cst (TREE_TYPE (*vr0min
), -1));
8649 = int_const_binop (MINUS_EXPR
, *vr0min
,
8650 build_int_cst (TREE_TYPE (*vr0min
), 1));
8653 /* Choose the anti-range if the range is effectively varying. */
8654 else if (vrp_val_is_min (vr1min
)
8655 && vrp_val_is_max (vr1max
))
8657 /* Choose the anti-range if it is ~[0,0], that range is special
8658 enough to special case when vr1's range is relatively wide. */
8659 else if (*vr0min
== *vr0max
8660 && integer_zerop (*vr0min
)
8661 && (TYPE_PRECISION (TREE_TYPE (*vr0min
))
8662 == TYPE_PRECISION (ptr_type_node
))
8663 && TREE_CODE (vr1max
) == INTEGER_CST
8664 && TREE_CODE (vr1min
) == INTEGER_CST
8665 && (wi::clz (wi::to_wide (vr1max
) - wi::to_wide (vr1min
))
8666 < TYPE_PRECISION (TREE_TYPE (*vr0min
)) / 2))
8668 /* Else choose the range. */
8676 else if (*vr0type
== VR_ANTI_RANGE
8677 && vr1type
== VR_ANTI_RANGE
)
8679 /* If both are anti-ranges the result is the outer one. */
8684 else if (vr1type
== VR_ANTI_RANGE
8685 && *vr0type
== VR_RANGE
)
8687 /* The intersection is empty. */
8688 *vr0type
= VR_UNDEFINED
;
8689 *vr0min
= NULL_TREE
;
8690 *vr0max
= NULL_TREE
;
8695 else if ((operand_less_p (vr1min
, *vr0max
) == 1
8696 || operand_equal_p (vr1min
, *vr0max
, 0))
8697 && operand_less_p (*vr0min
, vr1min
) == 1)
8699 /* [ ( ] ) or [ ]( ) */
8700 if (*vr0type
== VR_ANTI_RANGE
8701 && vr1type
== VR_ANTI_RANGE
)
8703 else if (*vr0type
== VR_RANGE
8704 && vr1type
== VR_RANGE
)
8706 else if (*vr0type
== VR_RANGE
8707 && vr1type
== VR_ANTI_RANGE
)
8709 if (TREE_CODE (vr1min
) == INTEGER_CST
)
8710 *vr0max
= int_const_binop (MINUS_EXPR
, vr1min
,
8711 build_int_cst (TREE_TYPE (vr1min
), 1));
8715 else if (*vr0type
== VR_ANTI_RANGE
8716 && vr1type
== VR_RANGE
)
8718 *vr0type
= VR_RANGE
;
8719 if (TREE_CODE (*vr0max
) == INTEGER_CST
)
8720 *vr0min
= int_const_binop (PLUS_EXPR
, *vr0max
,
8721 build_int_cst (TREE_TYPE (*vr0max
), 1));
8729 else if ((operand_less_p (*vr0min
, vr1max
) == 1
8730 || operand_equal_p (*vr0min
, vr1max
, 0))
8731 && operand_less_p (vr1min
, *vr0min
) == 1)
8733 /* ( [ ) ] or ( )[ ] */
8734 if (*vr0type
== VR_ANTI_RANGE
8735 && vr1type
== VR_ANTI_RANGE
)
8737 else if (*vr0type
== VR_RANGE
8738 && vr1type
== VR_RANGE
)
8740 else if (*vr0type
== VR_RANGE
8741 && vr1type
== VR_ANTI_RANGE
)
8743 if (TREE_CODE (vr1max
) == INTEGER_CST
)
8744 *vr0min
= int_const_binop (PLUS_EXPR
, vr1max
,
8745 build_int_cst (TREE_TYPE (vr1max
), 1));
8749 else if (*vr0type
== VR_ANTI_RANGE
8750 && vr1type
== VR_RANGE
)
8752 *vr0type
= VR_RANGE
;
8753 if (TREE_CODE (*vr0min
) == INTEGER_CST
)
8754 *vr0max
= int_const_binop (MINUS_EXPR
, *vr0min
,
8755 build_int_cst (TREE_TYPE (*vr0min
), 1));
8764 /* As a fallback simply use { *VRTYPE, *VR0MIN, *VR0MAX } as
8765 result for the intersection. That's always a conservative
8766 correct estimate unless VR1 is a constant singleton range
8767 in which case we choose that. */
8768 if (vr1type
== VR_RANGE
8769 && is_gimple_min_invariant (vr1min
)
8770 && vrp_operand_equal_p (vr1min
, vr1max
))
8781 /* Intersect the two value-ranges *VR0 and *VR1 and store the result
8782 in *VR0. This may not be the smallest possible such range. */
8785 vrp_intersect_ranges_1 (value_range
*vr0
, value_range
*vr1
)
8789 /* If either range is VR_VARYING the other one wins. */
8790 if (vr1
->type
== VR_VARYING
)
8792 if (vr0
->type
== VR_VARYING
)
8794 copy_value_range (vr0
, vr1
);
8798 /* When either range is VR_UNDEFINED the resulting range is
8799 VR_UNDEFINED, too. */
8800 if (vr0
->type
== VR_UNDEFINED
)
8802 if (vr1
->type
== VR_UNDEFINED
)
8804 set_value_range_to_undefined (vr0
);
8808 /* Save the original vr0 so we can return it as conservative intersection
8809 result when our worker turns things to varying. */
8811 intersect_ranges (&vr0
->type
, &vr0
->min
, &vr0
->max
,
8812 vr1
->type
, vr1
->min
, vr1
->max
);
8813 /* Make sure to canonicalize the result though as the inversion of a
8814 VR_RANGE can still be a VR_RANGE. */
8815 set_and_canonicalize_value_range (vr0
, vr0
->type
,
8816 vr0
->min
, vr0
->max
, vr0
->equiv
);
8817 /* If that failed, use the saved original VR0. */
8818 if (vr0
->type
== VR_VARYING
)
8823 /* If the result is VR_UNDEFINED there is no need to mess with
8824 the equivalencies. */
8825 if (vr0
->type
== VR_UNDEFINED
)
8828 /* The resulting set of equivalences for range intersection is the union of
8830 if (vr0
->equiv
&& vr1
->equiv
&& vr0
->equiv
!= vr1
->equiv
)
8831 bitmap_ior_into (vr0
->equiv
, vr1
->equiv
);
8832 else if (vr1
->equiv
&& !vr0
->equiv
)
8834 /* All equivalence bitmaps are allocated from the same obstack. So
8835 we can use the obstack associated with VR to allocate vr0->equiv. */
8836 vr0
->equiv
= BITMAP_ALLOC (vr1
->equiv
->obstack
);
8837 bitmap_copy (vr0
->equiv
, vr1
->equiv
);
8842 vrp_intersect_ranges (value_range
*vr0
, value_range
*vr1
)
8844 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8846 fprintf (dump_file
, "Intersecting\n ");
8847 dump_value_range (dump_file
, vr0
);
8848 fprintf (dump_file
, "\nand\n ");
8849 dump_value_range (dump_file
, vr1
);
8850 fprintf (dump_file
, "\n");
8852 vrp_intersect_ranges_1 (vr0
, vr1
);
8853 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8855 fprintf (dump_file
, "to\n ");
8856 dump_value_range (dump_file
, vr0
);
8857 fprintf (dump_file
, "\n");
8861 /* Meet operation for value ranges. Given two value ranges VR0 and
8862 VR1, store in VR0 a range that contains both VR0 and VR1. This
8863 may not be the smallest possible such range. */
8866 vrp_meet_1 (value_range
*vr0
, const value_range
*vr1
)
8870 if (vr0
->type
== VR_UNDEFINED
)
8872 set_value_range (vr0
, vr1
->type
, vr1
->min
, vr1
->max
, vr1
->equiv
);
8876 if (vr1
->type
== VR_UNDEFINED
)
8878 /* VR0 already has the resulting range. */
8882 if (vr0
->type
== VR_VARYING
)
8884 /* Nothing to do. VR0 already has the resulting range. */
8888 if (vr1
->type
== VR_VARYING
)
8890 set_value_range_to_varying (vr0
);
8895 union_ranges (&vr0
->type
, &vr0
->min
, &vr0
->max
,
8896 vr1
->type
, vr1
->min
, vr1
->max
);
8897 if (vr0
->type
== VR_VARYING
)
8899 /* Failed to find an efficient meet. Before giving up and setting
8900 the result to VARYING, see if we can at least derive a useful
8901 anti-range. FIXME, all this nonsense about distinguishing
8902 anti-ranges from ranges is necessary because of the odd
8903 semantics of range_includes_zero_p and friends. */
8904 if (((saved
.type
== VR_RANGE
8905 && range_includes_zero_p (saved
.min
, saved
.max
) == 0)
8906 || (saved
.type
== VR_ANTI_RANGE
8907 && range_includes_zero_p (saved
.min
, saved
.max
) == 1))
8908 && ((vr1
->type
== VR_RANGE
8909 && range_includes_zero_p (vr1
->min
, vr1
->max
) == 0)
8910 || (vr1
->type
== VR_ANTI_RANGE
8911 && range_includes_zero_p (vr1
->min
, vr1
->max
) == 1)))
8913 set_value_range_to_nonnull (vr0
, TREE_TYPE (saved
.min
));
8915 /* Since this meet operation did not result from the meeting of
8916 two equivalent names, VR0 cannot have any equivalences. */
8918 bitmap_clear (vr0
->equiv
);
8922 set_value_range_to_varying (vr0
);
8925 set_and_canonicalize_value_range (vr0
, vr0
->type
, vr0
->min
, vr0
->max
,
8927 if (vr0
->type
== VR_VARYING
)
8930 /* The resulting set of equivalences is always the intersection of
8932 if (vr0
->equiv
&& vr1
->equiv
&& vr0
->equiv
!= vr1
->equiv
)
8933 bitmap_and_into (vr0
->equiv
, vr1
->equiv
);
8934 else if (vr0
->equiv
&& !vr1
->equiv
)
8935 bitmap_clear (vr0
->equiv
);
8939 vrp_meet (value_range
*vr0
, const value_range
*vr1
)
8941 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8943 fprintf (dump_file
, "Meeting\n ");
8944 dump_value_range (dump_file
, vr0
);
8945 fprintf (dump_file
, "\nand\n ");
8946 dump_value_range (dump_file
, vr1
);
8947 fprintf (dump_file
, "\n");
8949 vrp_meet_1 (vr0
, vr1
);
8950 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8952 fprintf (dump_file
, "to\n ");
8953 dump_value_range (dump_file
, vr0
);
8954 fprintf (dump_file
, "\n");
8959 /* Visit all arguments for PHI node PHI that flow through executable
8960 edges. If a valid value range can be derived from all the incoming
8961 value ranges, set a new range in VR_RESULT. */
8964 extract_range_from_phi_node (gphi
*phi
, value_range
*vr_result
)
8967 tree lhs
= PHI_RESULT (phi
);
8968 value_range
*lhs_vr
= get_value_range (lhs
);
8970 int edges
, old_edges
;
8973 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8975 fprintf (dump_file
, "\nVisiting PHI node: ");
8976 print_gimple_stmt (dump_file
, phi
, 0, dump_flags
);
8979 bool may_simulate_backedge_again
= false;
8981 for (i
= 0; i
< gimple_phi_num_args (phi
); i
++)
8983 edge e
= gimple_phi_arg_edge (phi
, i
);
8985 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
8988 " Argument #%d (%d -> %d %sexecutable)\n",
8989 (int) i
, e
->src
->index
, e
->dest
->index
,
8990 (e
->flags
& EDGE_EXECUTABLE
) ? "" : "not ");
8993 if (e
->flags
& EDGE_EXECUTABLE
)
8995 tree arg
= PHI_ARG_DEF (phi
, i
);
9000 if (TREE_CODE (arg
) == SSA_NAME
)
9002 /* See if we are eventually going to change one of the args. */
9003 gimple
*def_stmt
= SSA_NAME_DEF_STMT (arg
);
9004 if (! gimple_nop_p (def_stmt
)
9005 && prop_simulate_again_p (def_stmt
)
9006 && e
->flags
& EDGE_DFS_BACK
)
9007 may_simulate_backedge_again
= true;
9009 vr_arg
= *(get_value_range (arg
));
9010 /* Do not allow equivalences or symbolic ranges to leak in from
9011 backedges. That creates invalid equivalencies.
9012 See PR53465 and PR54767. */
9013 if (e
->flags
& EDGE_DFS_BACK
)
9015 if (vr_arg
.type
== VR_RANGE
9016 || vr_arg
.type
== VR_ANTI_RANGE
)
9018 vr_arg
.equiv
= NULL
;
9019 if (symbolic_range_p (&vr_arg
))
9021 vr_arg
.type
= VR_VARYING
;
9022 vr_arg
.min
= NULL_TREE
;
9023 vr_arg
.max
= NULL_TREE
;
9029 /* If the non-backedge arguments range is VR_VARYING then
9030 we can still try recording a simple equivalence. */
9031 if (vr_arg
.type
== VR_VARYING
)
9033 vr_arg
.type
= VR_RANGE
;
9036 vr_arg
.equiv
= NULL
;
9042 if (TREE_OVERFLOW_P (arg
))
9043 arg
= drop_tree_overflow (arg
);
9045 vr_arg
.type
= VR_RANGE
;
9048 vr_arg
.equiv
= NULL
;
9051 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
9053 fprintf (dump_file
, "\t");
9054 print_generic_expr (dump_file
, arg
, dump_flags
);
9055 fprintf (dump_file
, ": ");
9056 dump_value_range (dump_file
, &vr_arg
);
9057 fprintf (dump_file
, "\n");
9061 copy_value_range (vr_result
, &vr_arg
);
9063 vrp_meet (vr_result
, &vr_arg
);
9066 if (vr_result
->type
== VR_VARYING
)
9071 if (vr_result
->type
== VR_VARYING
)
9073 else if (vr_result
->type
== VR_UNDEFINED
)
9076 old_edges
= vr_phi_edge_counts
[SSA_NAME_VERSION (lhs
)];
9077 vr_phi_edge_counts
[SSA_NAME_VERSION (lhs
)] = edges
;
9079 /* To prevent infinite iterations in the algorithm, derive ranges
9080 when the new value is slightly bigger or smaller than the
9081 previous one. We don't do this if we have seen a new executable
9082 edge; this helps us avoid an infinity for conditionals
9083 which are not in a loop. If the old value-range was VR_UNDEFINED
9084 use the updated range and iterate one more time. If we will not
9085 simulate this PHI again via the backedge allow us to iterate. */
9087 && gimple_phi_num_args (phi
) > 1
9088 && edges
== old_edges
9089 && lhs_vr
->type
!= VR_UNDEFINED
9090 && may_simulate_backedge_again
)
9092 /* Compare old and new ranges, fall back to varying if the
9093 values are not comparable. */
9094 int cmp_min
= compare_values (lhs_vr
->min
, vr_result
->min
);
9097 int cmp_max
= compare_values (lhs_vr
->max
, vr_result
->max
);
9101 /* For non VR_RANGE or for pointers fall back to varying if
9102 the range changed. */
9103 if ((lhs_vr
->type
!= VR_RANGE
|| vr_result
->type
!= VR_RANGE
9104 || POINTER_TYPE_P (TREE_TYPE (lhs
)))
9105 && (cmp_min
!= 0 || cmp_max
!= 0))
9108 /* If the new minimum is larger than the previous one
9109 retain the old value. If the new minimum value is smaller
9110 than the previous one and not -INF go all the way to -INF + 1.
9111 In the first case, to avoid infinite bouncing between different
9112 minimums, and in the other case to avoid iterating millions of
9113 times to reach -INF. Going to -INF + 1 also lets the following
9114 iteration compute whether there will be any overflow, at the
9115 expense of one additional iteration. */
9117 vr_result
->min
= lhs_vr
->min
;
9118 else if (cmp_min
> 0
9119 && !vrp_val_is_min (vr_result
->min
))
9121 = int_const_binop (PLUS_EXPR
,
9122 vrp_val_min (TREE_TYPE (vr_result
->min
)),
9123 build_int_cst (TREE_TYPE (vr_result
->min
), 1));
9125 /* Similarly for the maximum value. */
9127 vr_result
->max
= lhs_vr
->max
;
9128 else if (cmp_max
< 0
9129 && !vrp_val_is_max (vr_result
->max
))
9131 = int_const_binop (MINUS_EXPR
,
9132 vrp_val_max (TREE_TYPE (vr_result
->min
)),
9133 build_int_cst (TREE_TYPE (vr_result
->min
), 1));
9135 /* If we dropped either bound to +-INF then if this is a loop
9136 PHI node SCEV may known more about its value-range. */
9137 if (cmp_min
> 0 || cmp_min
< 0
9138 || cmp_max
< 0 || cmp_max
> 0)
9141 goto infinite_check
;
9147 set_value_range_to_varying (vr_result
);
9150 /* If this is a loop PHI node SCEV may known more about its value-range.
9151 scev_check can be reached from two paths, one is a fall through from above
9152 "varying" label, the other is direct goto from code block which tries to
9153 avoid infinite simulation. */
9154 if ((l
= loop_containing_stmt (phi
))
9155 && l
->header
== gimple_bb (phi
))
9156 adjust_range_with_scev (vr_result
, l
, phi
, lhs
);
9159 /* If we will end up with a (-INF, +INF) range, set it to
9160 VARYING. Same if the previous max value was invalid for
9161 the type and we end up with vr_result.min > vr_result.max. */
9162 if ((vr_result
->type
== VR_RANGE
|| vr_result
->type
== VR_ANTI_RANGE
)
9163 && !((vrp_val_is_max (vr_result
->max
) && vrp_val_is_min (vr_result
->min
))
9164 || compare_values (vr_result
->min
, vr_result
->max
) > 0))
9167 set_value_range_to_varying (vr_result
);
9169 /* If the new range is different than the previous value, keep
9175 /* Visit all arguments for PHI node PHI that flow through executable
9176 edges. If a valid value range can be derived from all the incoming
9177 value ranges, set a new range for the LHS of PHI. */
9179 enum ssa_prop_result
9180 vrp_prop::visit_phi (gphi
*phi
)
9182 tree lhs
= PHI_RESULT (phi
);
9183 value_range vr_result
= VR_INITIALIZER
;
9184 extract_range_from_phi_node (phi
, &vr_result
);
9185 if (update_value_range (lhs
, &vr_result
))
9187 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
9189 fprintf (dump_file
, "Found new range for ");
9190 print_generic_expr (dump_file
, lhs
);
9191 fprintf (dump_file
, ": ");
9192 dump_value_range (dump_file
, &vr_result
);
9193 fprintf (dump_file
, "\n");
9196 if (vr_result
.type
== VR_VARYING
)
9197 return SSA_PROP_VARYING
;
9199 return SSA_PROP_INTERESTING
;
9202 /* Nothing changed, don't add outgoing edges. */
9203 return SSA_PROP_NOT_INTERESTING
;
9206 /* Simplify boolean operations if the source is known
9207 to be already a boolean. */
9209 simplify_truth_ops_using_ranges (gimple_stmt_iterator
*gsi
, gimple
*stmt
)
9211 enum tree_code rhs_code
= gimple_assign_rhs_code (stmt
);
9213 bool need_conversion
;
9215 /* We handle only !=/== case here. */
9216 gcc_assert (rhs_code
== EQ_EXPR
|| rhs_code
== NE_EXPR
);
9218 op0
= gimple_assign_rhs1 (stmt
);
9219 if (!op_with_boolean_value_range_p (op0
))
9222 op1
= gimple_assign_rhs2 (stmt
);
9223 if (!op_with_boolean_value_range_p (op1
))
9226 /* Reduce number of cases to handle to NE_EXPR. As there is no
9227 BIT_XNOR_EXPR we cannot replace A == B with a single statement. */
9228 if (rhs_code
== EQ_EXPR
)
9230 if (TREE_CODE (op1
) == INTEGER_CST
)
9231 op1
= int_const_binop (BIT_XOR_EXPR
, op1
,
9232 build_int_cst (TREE_TYPE (op1
), 1));
9237 lhs
= gimple_assign_lhs (stmt
);
9239 = !useless_type_conversion_p (TREE_TYPE (lhs
), TREE_TYPE (op0
));
9241 /* Make sure to not sign-extend a 1-bit 1 when converting the result. */
9243 && !TYPE_UNSIGNED (TREE_TYPE (op0
))
9244 && TYPE_PRECISION (TREE_TYPE (op0
)) == 1
9245 && TYPE_PRECISION (TREE_TYPE (lhs
)) > 1)
9248 /* For A != 0 we can substitute A itself. */
9249 if (integer_zerop (op1
))
9250 gimple_assign_set_rhs_with_ops (gsi
,
9252 ? NOP_EXPR
: TREE_CODE (op0
), op0
);
9253 /* For A != B we substitute A ^ B. Either with conversion. */
9254 else if (need_conversion
)
9256 tree tem
= make_ssa_name (TREE_TYPE (op0
));
9258 = gimple_build_assign (tem
, BIT_XOR_EXPR
, op0
, op1
);
9259 gsi_insert_before (gsi
, newop
, GSI_SAME_STMT
);
9260 if (INTEGRAL_TYPE_P (TREE_TYPE (tem
))
9261 && TYPE_PRECISION (TREE_TYPE (tem
)) > 1)
9262 set_range_info (tem
, VR_RANGE
,
9263 wi::zero (TYPE_PRECISION (TREE_TYPE (tem
))),
9264 wi::one (TYPE_PRECISION (TREE_TYPE (tem
))));
9265 gimple_assign_set_rhs_with_ops (gsi
, NOP_EXPR
, tem
);
9269 gimple_assign_set_rhs_with_ops (gsi
, BIT_XOR_EXPR
, op0
, op1
);
9270 update_stmt (gsi_stmt (*gsi
));
9271 fold_stmt (gsi
, follow_single_use_edges
);
9276 /* Simplify a division or modulo operator to a right shift or bitwise and
9277 if the first operand is unsigned or is greater than zero and the second
9278 operand is an exact power of two. For TRUNC_MOD_EXPR op0 % op1 with
9279 constant op1 (op1min = op1) or with op1 in [op1min, op1max] range,
9280 optimize it into just op0 if op0's range is known to be a subset of
9281 [-op1min + 1, op1min - 1] for signed and [0, op1min - 1] for unsigned
9285 simplify_div_or_mod_using_ranges (gimple_stmt_iterator
*gsi
, gimple
*stmt
)
9287 enum tree_code rhs_code
= gimple_assign_rhs_code (stmt
);
9289 tree op0
= gimple_assign_rhs1 (stmt
);
9290 tree op1
= gimple_assign_rhs2 (stmt
);
9291 tree op0min
= NULL_TREE
, op0max
= NULL_TREE
;
9293 value_range
*vr
= NULL
;
9295 if (TREE_CODE (op0
) == INTEGER_CST
)
9302 vr
= get_value_range (op0
);
9303 if (range_int_cst_p (vr
))
9310 if (rhs_code
== TRUNC_MOD_EXPR
9311 && TREE_CODE (op1
) == SSA_NAME
)
9313 value_range
*vr1
= get_value_range (op1
);
9314 if (range_int_cst_p (vr1
))
9317 if (rhs_code
== TRUNC_MOD_EXPR
9318 && TREE_CODE (op1min
) == INTEGER_CST
9319 && tree_int_cst_sgn (op1min
) == 1
9321 && tree_int_cst_lt (op0max
, op1min
))
9323 if (TYPE_UNSIGNED (TREE_TYPE (op0
))
9324 || tree_int_cst_sgn (op0min
) >= 0
9325 || tree_int_cst_lt (fold_unary (NEGATE_EXPR
, TREE_TYPE (op1min
), op1min
),
9328 /* If op0 already has the range op0 % op1 has,
9329 then TRUNC_MOD_EXPR won't change anything. */
9330 gimple_assign_set_rhs_from_tree (gsi
, op0
);
9335 if (TREE_CODE (op0
) != SSA_NAME
)
9338 if (!integer_pow2p (op1
))
9340 /* X % -Y can be only optimized into X % Y either if
9341 X is not INT_MIN, or Y is not -1. Fold it now, as after
9342 remove_range_assertions the range info might be not available
9344 if (rhs_code
== TRUNC_MOD_EXPR
9345 && fold_stmt (gsi
, follow_single_use_edges
))
9350 if (TYPE_UNSIGNED (TREE_TYPE (op0
)))
9351 val
= integer_one_node
;
9356 val
= compare_range_with_value (GE_EXPR
, vr
, integer_zero_node
, &sop
);
9360 && integer_onep (val
)
9361 && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC
))
9363 location_t location
;
9365 if (!gimple_has_location (stmt
))
9366 location
= input_location
;
9368 location
= gimple_location (stmt
);
9369 warning_at (location
, OPT_Wstrict_overflow
,
9370 "assuming signed overflow does not occur when "
9371 "simplifying %</%> or %<%%%> to %<>>%> or %<&%>");
9375 if (val
&& integer_onep (val
))
9379 if (rhs_code
== TRUNC_DIV_EXPR
)
9381 t
= build_int_cst (integer_type_node
, tree_log2 (op1
));
9382 gimple_assign_set_rhs_code (stmt
, RSHIFT_EXPR
);
9383 gimple_assign_set_rhs1 (stmt
, op0
);
9384 gimple_assign_set_rhs2 (stmt
, t
);
9388 t
= build_int_cst (TREE_TYPE (op1
), 1);
9389 t
= int_const_binop (MINUS_EXPR
, op1
, t
);
9390 t
= fold_convert (TREE_TYPE (op0
), t
);
9392 gimple_assign_set_rhs_code (stmt
, BIT_AND_EXPR
);
9393 gimple_assign_set_rhs1 (stmt
, op0
);
9394 gimple_assign_set_rhs2 (stmt
, t
);
9398 fold_stmt (gsi
, follow_single_use_edges
);
9405 /* Simplify a min or max if the ranges of the two operands are
9406 disjoint. Return true if we do simplify. */
9409 simplify_min_or_max_using_ranges (gimple_stmt_iterator
*gsi
, gimple
*stmt
)
9411 tree op0
= gimple_assign_rhs1 (stmt
);
9412 tree op1
= gimple_assign_rhs2 (stmt
);
9416 val
= (vrp_evaluate_conditional_warnv_with_ops_using_ranges
9417 (LE_EXPR
, op0
, op1
, &sop
));
9421 val
= (vrp_evaluate_conditional_warnv_with_ops_using_ranges
9422 (LT_EXPR
, op0
, op1
, &sop
));
9427 if (sop
&& issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC
))
9429 location_t location
;
9431 if (!gimple_has_location (stmt
))
9432 location
= input_location
;
9434 location
= gimple_location (stmt
);
9435 warning_at (location
, OPT_Wstrict_overflow
,
9436 "assuming signed overflow does not occur when "
9437 "simplifying %<min/max (X,Y)%> to %<X%> or %<Y%>");
9440 /* VAL == TRUE -> OP0 < or <= op1
9441 VAL == FALSE -> OP0 > or >= op1. */
9442 tree res
= ((gimple_assign_rhs_code (stmt
) == MAX_EXPR
)
9443 == integer_zerop (val
)) ? op0
: op1
;
9444 gimple_assign_set_rhs_from_tree (gsi
, res
);
9451 /* If the operand to an ABS_EXPR is >= 0, then eliminate the
9452 ABS_EXPR. If the operand is <= 0, then simplify the
9453 ABS_EXPR into a NEGATE_EXPR. */
9456 simplify_abs_using_ranges (gimple_stmt_iterator
*gsi
, gimple
*stmt
)
9458 tree op
= gimple_assign_rhs1 (stmt
);
9459 value_range
*vr
= get_value_range (op
);
9466 val
= compare_range_with_value (LE_EXPR
, vr
, integer_zero_node
, &sop
);
9469 /* The range is neither <= 0 nor > 0. Now see if it is
9470 either < 0 or >= 0. */
9472 val
= compare_range_with_value (LT_EXPR
, vr
, integer_zero_node
,
9478 if (sop
&& issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC
))
9480 location_t location
;
9482 if (!gimple_has_location (stmt
))
9483 location
= input_location
;
9485 location
= gimple_location (stmt
);
9486 warning_at (location
, OPT_Wstrict_overflow
,
9487 "assuming signed overflow does not occur when "
9488 "simplifying %<abs (X)%> to %<X%> or %<-X%>");
9491 gimple_assign_set_rhs1 (stmt
, op
);
9492 if (integer_zerop (val
))
9493 gimple_assign_set_rhs_code (stmt
, SSA_NAME
);
9495 gimple_assign_set_rhs_code (stmt
, NEGATE_EXPR
);
9497 fold_stmt (gsi
, follow_single_use_edges
);
9505 /* Optimize away redundant BIT_AND_EXPR and BIT_IOR_EXPR.
9506 If all the bits that are being cleared by & are already
9507 known to be zero from VR, or all the bits that are being
9508 set by | are already known to be one from VR, the bit
9509 operation is redundant. */
9512 simplify_bit_ops_using_ranges (gimple_stmt_iterator
*gsi
, gimple
*stmt
)
9514 tree op0
= gimple_assign_rhs1 (stmt
);
9515 tree op1
= gimple_assign_rhs2 (stmt
);
9516 tree op
= NULL_TREE
;
9517 value_range vr0
= VR_INITIALIZER
;
9518 value_range vr1
= VR_INITIALIZER
;
9519 wide_int may_be_nonzero0
, may_be_nonzero1
;
9520 wide_int must_be_nonzero0
, must_be_nonzero1
;
9523 if (TREE_CODE (op0
) == SSA_NAME
)
9524 vr0
= *(get_value_range (op0
));
9525 else if (is_gimple_min_invariant (op0
))
9526 set_value_range_to_value (&vr0
, op0
, NULL
);
9530 if (TREE_CODE (op1
) == SSA_NAME
)
9531 vr1
= *(get_value_range (op1
));
9532 else if (is_gimple_min_invariant (op1
))
9533 set_value_range_to_value (&vr1
, op1
, NULL
);
9537 if (!zero_nonzero_bits_from_vr (TREE_TYPE (op0
), &vr0
, &may_be_nonzero0
,
9540 if (!zero_nonzero_bits_from_vr (TREE_TYPE (op1
), &vr1
, &may_be_nonzero1
,
9544 switch (gimple_assign_rhs_code (stmt
))
9547 mask
= wi::bit_and_not (may_be_nonzero0
, must_be_nonzero1
);
9553 mask
= wi::bit_and_not (may_be_nonzero1
, must_be_nonzero0
);
9561 mask
= wi::bit_and_not (may_be_nonzero0
, must_be_nonzero1
);
9567 mask
= wi::bit_and_not (may_be_nonzero1
, must_be_nonzero0
);
9578 if (op
== NULL_TREE
)
9581 gimple_assign_set_rhs_with_ops (gsi
, TREE_CODE (op
), op
);
9582 update_stmt (gsi_stmt (*gsi
));
9586 /* We are comparing trees OP0 and OP1 using COND_CODE. OP0 has
9587 a known value range VR.
9589 If there is one and only one value which will satisfy the
9590 conditional, then return that value. Else return NULL.
9592 If signed overflow must be undefined for the value to satisfy
9593 the conditional, then set *STRICT_OVERFLOW_P to true. */
9596 test_for_singularity (enum tree_code cond_code
, tree op0
,
9597 tree op1
, value_range
*vr
)
9602 /* Extract minimum/maximum values which satisfy the conditional as it was
9604 if (cond_code
== LE_EXPR
|| cond_code
== LT_EXPR
)
9606 min
= TYPE_MIN_VALUE (TREE_TYPE (op0
));
9609 if (cond_code
== LT_EXPR
)
9611 tree one
= build_int_cst (TREE_TYPE (op0
), 1);
9612 max
= fold_build2 (MINUS_EXPR
, TREE_TYPE (op0
), max
, one
);
9613 /* Signal to compare_values_warnv this expr doesn't overflow. */
9615 TREE_NO_WARNING (max
) = 1;
9618 else if (cond_code
== GE_EXPR
|| cond_code
== GT_EXPR
)
9620 max
= TYPE_MAX_VALUE (TREE_TYPE (op0
));
9623 if (cond_code
== GT_EXPR
)
9625 tree one
= build_int_cst (TREE_TYPE (op0
), 1);
9626 min
= fold_build2 (PLUS_EXPR
, TREE_TYPE (op0
), min
, one
);
9627 /* Signal to compare_values_warnv this expr doesn't overflow. */
9629 TREE_NO_WARNING (min
) = 1;
9633 /* Now refine the minimum and maximum values using any
9634 value range information we have for op0. */
9637 if (compare_values (vr
->min
, min
) == 1)
9639 if (compare_values (vr
->max
, max
) == -1)
9642 /* If the new min/max values have converged to a single value,
9643 then there is only one value which can satisfy the condition,
9644 return that value. */
9645 if (operand_equal_p (min
, max
, 0) && is_gimple_min_invariant (min
))
9651 /* Return whether the value range *VR fits in an integer type specified
9652 by PRECISION and UNSIGNED_P. */
9655 range_fits_type_p (value_range
*vr
, unsigned dest_precision
, signop dest_sgn
)
9658 unsigned src_precision
;
9662 /* We can only handle integral and pointer types. */
9663 src_type
= TREE_TYPE (vr
->min
);
9664 if (!INTEGRAL_TYPE_P (src_type
)
9665 && !POINTER_TYPE_P (src_type
))
9668 /* An extension is fine unless VR is SIGNED and dest_sgn is UNSIGNED,
9669 and so is an identity transform. */
9670 src_precision
= TYPE_PRECISION (TREE_TYPE (vr
->min
));
9671 src_sgn
= TYPE_SIGN (src_type
);
9672 if ((src_precision
< dest_precision
9673 && !(dest_sgn
== UNSIGNED
&& src_sgn
== SIGNED
))
9674 || (src_precision
== dest_precision
&& src_sgn
== dest_sgn
))
9677 /* Now we can only handle ranges with constant bounds. */
9678 if (vr
->type
!= VR_RANGE
9679 || TREE_CODE (vr
->min
) != INTEGER_CST
9680 || TREE_CODE (vr
->max
) != INTEGER_CST
)
9683 /* For sign changes, the MSB of the wide_int has to be clear.
9684 An unsigned value with its MSB set cannot be represented by
9685 a signed wide_int, while a negative value cannot be represented
9686 by an unsigned wide_int. */
9687 if (src_sgn
!= dest_sgn
9688 && (wi::lts_p (wi::to_wide (vr
->min
), 0)
9689 || wi::lts_p (wi::to_wide (vr
->max
), 0)))
9692 /* Then we can perform the conversion on both ends and compare
9693 the result for equality. */
9694 tem
= wi::ext (wi::to_widest (vr
->min
), dest_precision
, dest_sgn
);
9695 if (tem
!= wi::to_widest (vr
->min
))
9697 tem
= wi::ext (wi::to_widest (vr
->max
), dest_precision
, dest_sgn
);
9698 if (tem
!= wi::to_widest (vr
->max
))
9704 /* Simplify a conditional using a relational operator to an equality
9705 test if the range information indicates only one value can satisfy
9706 the original conditional. */
9709 simplify_cond_using_ranges_1 (gcond
*stmt
)
9711 tree op0
= gimple_cond_lhs (stmt
);
9712 tree op1
= gimple_cond_rhs (stmt
);
9713 enum tree_code cond_code
= gimple_cond_code (stmt
);
9715 if (cond_code
!= NE_EXPR
9716 && cond_code
!= EQ_EXPR
9717 && TREE_CODE (op0
) == SSA_NAME
9718 && INTEGRAL_TYPE_P (TREE_TYPE (op0
))
9719 && is_gimple_min_invariant (op1
))
9721 value_range
*vr
= get_value_range (op0
);
9723 /* If we have range information for OP0, then we might be
9724 able to simplify this conditional. */
9725 if (vr
->type
== VR_RANGE
)
9727 tree new_tree
= test_for_singularity (cond_code
, op0
, op1
, vr
);
9732 fprintf (dump_file
, "Simplified relational ");
9733 print_gimple_stmt (dump_file
, stmt
, 0);
9734 fprintf (dump_file
, " into ");
9737 gimple_cond_set_code (stmt
, EQ_EXPR
);
9738 gimple_cond_set_lhs (stmt
, op0
);
9739 gimple_cond_set_rhs (stmt
, new_tree
);
9745 print_gimple_stmt (dump_file
, stmt
, 0);
9746 fprintf (dump_file
, "\n");
9752 /* Try again after inverting the condition. We only deal
9753 with integral types here, so no need to worry about
9754 issues with inverting FP comparisons. */
9755 new_tree
= test_for_singularity
9756 (invert_tree_comparison (cond_code
, false),
9762 fprintf (dump_file
, "Simplified relational ");
9763 print_gimple_stmt (dump_file
, stmt
, 0);
9764 fprintf (dump_file
, " into ");
9767 gimple_cond_set_code (stmt
, NE_EXPR
);
9768 gimple_cond_set_lhs (stmt
, op0
);
9769 gimple_cond_set_rhs (stmt
, new_tree
);
9775 print_gimple_stmt (dump_file
, stmt
, 0);
9776 fprintf (dump_file
, "\n");
9786 /* STMT is a conditional at the end of a basic block.
9788 If the conditional is of the form SSA_NAME op constant and the SSA_NAME
9789 was set via a type conversion, try to replace the SSA_NAME with the RHS
9790 of the type conversion. Doing so makes the conversion dead which helps
9791 subsequent passes. */
9794 simplify_cond_using_ranges_2 (gcond
*stmt
)
9796 tree op0
= gimple_cond_lhs (stmt
);
9797 tree op1
= gimple_cond_rhs (stmt
);
9799 /* If we have a comparison of an SSA_NAME (OP0) against a constant,
9800 see if OP0 was set by a type conversion where the source of
9801 the conversion is another SSA_NAME with a range that fits
9802 into the range of OP0's type.
9804 If so, the conversion is redundant as the earlier SSA_NAME can be
9805 used for the comparison directly if we just massage the constant in the
9807 if (TREE_CODE (op0
) == SSA_NAME
9808 && TREE_CODE (op1
) == INTEGER_CST
)
9810 gimple
*def_stmt
= SSA_NAME_DEF_STMT (op0
);
9813 if (!is_gimple_assign (def_stmt
)
9814 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt
)))
9817 innerop
= gimple_assign_rhs1 (def_stmt
);
9819 if (TREE_CODE (innerop
) == SSA_NAME
9820 && !POINTER_TYPE_P (TREE_TYPE (innerop
))
9821 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (innerop
)
9822 && desired_pro_or_demotion_p (TREE_TYPE (innerop
), TREE_TYPE (op0
)))
9824 value_range
*vr
= get_value_range (innerop
);
9826 if (range_int_cst_p (vr
)
9827 && range_fits_type_p (vr
,
9828 TYPE_PRECISION (TREE_TYPE (op0
)),
9829 TYPE_SIGN (TREE_TYPE (op0
)))
9830 && int_fits_type_p (op1
, TREE_TYPE (innerop
)))
9832 tree newconst
= fold_convert (TREE_TYPE (innerop
), op1
);
9833 gimple_cond_set_lhs (stmt
, innerop
);
9834 gimple_cond_set_rhs (stmt
, newconst
);
9836 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
9838 fprintf (dump_file
, "Folded into: ");
9839 print_gimple_stmt (dump_file
, stmt
, 0, TDF_SLIM
);
9840 fprintf (dump_file
, "\n");
9847 /* Simplify a switch statement using the value range of the switch
9851 simplify_switch_using_ranges (gswitch
*stmt
)
9853 tree op
= gimple_switch_index (stmt
);
9854 value_range
*vr
= NULL
;
9858 size_t i
= 0, j
= 0, n
, n2
;
9861 size_t k
= 1, l
= 0;
9863 if (TREE_CODE (op
) == SSA_NAME
)
9865 vr
= get_value_range (op
);
9867 /* We can only handle integer ranges. */
9868 if ((vr
->type
!= VR_RANGE
9869 && vr
->type
!= VR_ANTI_RANGE
)
9870 || symbolic_range_p (vr
))
9873 /* Find case label for min/max of the value range. */
9874 take_default
= !find_case_label_ranges (stmt
, vr
, &i
, &j
, &k
, &l
);
9876 else if (TREE_CODE (op
) == INTEGER_CST
)
9878 take_default
= !find_case_label_index (stmt
, 1, op
, &i
);
9892 n
= gimple_switch_num_labels (stmt
);
9894 /* We can truncate the case label ranges that partially overlap with OP's
9896 size_t min_idx
= 1, max_idx
= 0;
9898 find_case_label_range (stmt
, vr
->min
, vr
->max
, &min_idx
, &max_idx
);
9899 if (min_idx
<= max_idx
)
9901 tree min_label
= gimple_switch_label (stmt
, min_idx
);
9902 tree max_label
= gimple_switch_label (stmt
, max_idx
);
9904 /* Avoid changing the type of the case labels when truncating. */
9905 tree case_label_type
= TREE_TYPE (CASE_LOW (min_label
));
9906 tree vr_min
= fold_convert (case_label_type
, vr
->min
);
9907 tree vr_max
= fold_convert (case_label_type
, vr
->max
);
9909 if (vr
->type
== VR_RANGE
)
9911 /* If OP's value range is [2,8] and the low label range is
9912 0 ... 3, truncate the label's range to 2 .. 3. */
9913 if (tree_int_cst_compare (CASE_LOW (min_label
), vr_min
) < 0
9914 && CASE_HIGH (min_label
) != NULL_TREE
9915 && tree_int_cst_compare (CASE_HIGH (min_label
), vr_min
) >= 0)
9916 CASE_LOW (min_label
) = vr_min
;
9918 /* If OP's value range is [2,8] and the high label range is
9919 7 ... 10, truncate the label's range to 7 .. 8. */
9920 if (tree_int_cst_compare (CASE_LOW (max_label
), vr_max
) <= 0
9921 && CASE_HIGH (max_label
) != NULL_TREE
9922 && tree_int_cst_compare (CASE_HIGH (max_label
), vr_max
) > 0)
9923 CASE_HIGH (max_label
) = vr_max
;
9925 else if (vr
->type
== VR_ANTI_RANGE
)
9927 tree one_cst
= build_one_cst (case_label_type
);
9929 if (min_label
== max_label
)
9931 /* If OP's value range is ~[7,8] and the label's range is
9932 7 ... 10, truncate the label's range to 9 ... 10. */
9933 if (tree_int_cst_compare (CASE_LOW (min_label
), vr_min
) == 0
9934 && CASE_HIGH (min_label
) != NULL_TREE
9935 && tree_int_cst_compare (CASE_HIGH (min_label
), vr_max
) > 0)
9936 CASE_LOW (min_label
)
9937 = int_const_binop (PLUS_EXPR
, vr_max
, one_cst
);
9939 /* If OP's value range is ~[7,8] and the label's range is
9940 5 ... 8, truncate the label's range to 5 ... 6. */
9941 if (tree_int_cst_compare (CASE_LOW (min_label
), vr_min
) < 0
9942 && CASE_HIGH (min_label
) != NULL_TREE
9943 && tree_int_cst_compare (CASE_HIGH (min_label
), vr_max
) == 0)
9944 CASE_HIGH (min_label
)
9945 = int_const_binop (MINUS_EXPR
, vr_min
, one_cst
);
9949 /* If OP's value range is ~[2,8] and the low label range is
9950 0 ... 3, truncate the label's range to 0 ... 1. */
9951 if (tree_int_cst_compare (CASE_LOW (min_label
), vr_min
) < 0
9952 && CASE_HIGH (min_label
) != NULL_TREE
9953 && tree_int_cst_compare (CASE_HIGH (min_label
), vr_min
) >= 0)
9954 CASE_HIGH (min_label
)
9955 = int_const_binop (MINUS_EXPR
, vr_min
, one_cst
);
9957 /* If OP's value range is ~[2,8] and the high label range is
9958 7 ... 10, truncate the label's range to 9 ... 10. */
9959 if (tree_int_cst_compare (CASE_LOW (max_label
), vr_max
) <= 0
9960 && CASE_HIGH (max_label
) != NULL_TREE
9961 && tree_int_cst_compare (CASE_HIGH (max_label
), vr_max
) > 0)
9962 CASE_LOW (max_label
)
9963 = int_const_binop (PLUS_EXPR
, vr_max
, one_cst
);
9967 /* Canonicalize singleton case ranges. */
9968 if (tree_int_cst_equal (CASE_LOW (min_label
), CASE_HIGH (min_label
)))
9969 CASE_HIGH (min_label
) = NULL_TREE
;
9970 if (tree_int_cst_equal (CASE_LOW (max_label
), CASE_HIGH (max_label
)))
9971 CASE_HIGH (max_label
) = NULL_TREE
;
9974 /* We can also eliminate case labels that lie completely outside OP's value
9977 /* Bail out if this is just all edges taken. */
9983 /* Build a new vector of taken case labels. */
9984 vec2
= make_tree_vec (j
- i
+ 1 + l
- k
+ 1 + (int)take_default
);
9987 /* Add the default edge, if necessary. */
9989 TREE_VEC_ELT (vec2
, n2
++) = gimple_switch_default_label (stmt
);
9991 for (; i
<= j
; ++i
, ++n2
)
9992 TREE_VEC_ELT (vec2
, n2
) = gimple_switch_label (stmt
, i
);
9994 for (; k
<= l
; ++k
, ++n2
)
9995 TREE_VEC_ELT (vec2
, n2
) = gimple_switch_label (stmt
, k
);
9997 /* Mark needed edges. */
9998 for (i
= 0; i
< n2
; ++i
)
10000 e
= find_edge (gimple_bb (stmt
),
10001 label_to_block (CASE_LABEL (TREE_VEC_ELT (vec2
, i
))));
10002 e
->aux
= (void *)-1;
10005 /* Queue not needed edges for later removal. */
10006 FOR_EACH_EDGE (e
, ei
, gimple_bb (stmt
)->succs
)
10008 if (e
->aux
== (void *)-1)
10014 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
10016 fprintf (dump_file
, "removing unreachable case label\n");
10018 to_remove_edges
.safe_push (e
);
10019 e
->flags
&= ~EDGE_EXECUTABLE
;
10022 /* And queue an update for the stmt. */
10025 to_update_switch_stmts
.safe_push (su
);
10029 /* Simplify an integral conversion from an SSA name in STMT. */
10032 simplify_conversion_using_ranges (gimple_stmt_iterator
*gsi
, gimple
*stmt
)
10034 tree innerop
, middleop
, finaltype
;
10036 signop inner_sgn
, middle_sgn
, final_sgn
;
10037 unsigned inner_prec
, middle_prec
, final_prec
;
10038 widest_int innermin
, innermed
, innermax
, middlemin
, middlemed
, middlemax
;
10040 finaltype
= TREE_TYPE (gimple_assign_lhs (stmt
));
10041 if (!INTEGRAL_TYPE_P (finaltype
))
10043 middleop
= gimple_assign_rhs1 (stmt
);
10044 def_stmt
= SSA_NAME_DEF_STMT (middleop
);
10045 if (!is_gimple_assign (def_stmt
)
10046 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt
)))
10048 innerop
= gimple_assign_rhs1 (def_stmt
);
10049 if (TREE_CODE (innerop
) != SSA_NAME
10050 || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (innerop
))
10053 /* Get the value-range of the inner operand. Use get_range_info in
10054 case innerop was created during substitute-and-fold. */
10055 wide_int imin
, imax
;
10056 if (!INTEGRAL_TYPE_P (TREE_TYPE (innerop
))
10057 || get_range_info (innerop
, &imin
, &imax
) != VR_RANGE
)
10059 innermin
= widest_int::from (imin
, TYPE_SIGN (TREE_TYPE (innerop
)));
10060 innermax
= widest_int::from (imax
, TYPE_SIGN (TREE_TYPE (innerop
)));
10062 /* Simulate the conversion chain to check if the result is equal if
10063 the middle conversion is removed. */
10064 inner_prec
= TYPE_PRECISION (TREE_TYPE (innerop
));
10065 middle_prec
= TYPE_PRECISION (TREE_TYPE (middleop
));
10066 final_prec
= TYPE_PRECISION (finaltype
);
10068 /* If the first conversion is not injective, the second must not
10070 if (wi::gtu_p (innermax
- innermin
,
10071 wi::mask
<widest_int
> (middle_prec
, false))
10072 && middle_prec
< final_prec
)
10074 /* We also want a medium value so that we can track the effect that
10075 narrowing conversions with sign change have. */
10076 inner_sgn
= TYPE_SIGN (TREE_TYPE (innerop
));
10077 if (inner_sgn
== UNSIGNED
)
10078 innermed
= wi::shifted_mask
<widest_int
> (1, inner_prec
- 1, false);
10081 if (wi::cmp (innermin
, innermed
, inner_sgn
) >= 0
10082 || wi::cmp (innermed
, innermax
, inner_sgn
) >= 0)
10083 innermed
= innermin
;
10085 middle_sgn
= TYPE_SIGN (TREE_TYPE (middleop
));
10086 middlemin
= wi::ext (innermin
, middle_prec
, middle_sgn
);
10087 middlemed
= wi::ext (innermed
, middle_prec
, middle_sgn
);
10088 middlemax
= wi::ext (innermax
, middle_prec
, middle_sgn
);
10090 /* Require that the final conversion applied to both the original
10091 and the intermediate range produces the same result. */
10092 final_sgn
= TYPE_SIGN (finaltype
);
10093 if (wi::ext (middlemin
, final_prec
, final_sgn
)
10094 != wi::ext (innermin
, final_prec
, final_sgn
)
10095 || wi::ext (middlemed
, final_prec
, final_sgn
)
10096 != wi::ext (innermed
, final_prec
, final_sgn
)
10097 || wi::ext (middlemax
, final_prec
, final_sgn
)
10098 != wi::ext (innermax
, final_prec
, final_sgn
))
10101 gimple_assign_set_rhs1 (stmt
, innerop
);
10102 fold_stmt (gsi
, follow_single_use_edges
);
10106 /* Simplify a conversion from integral SSA name to float in STMT. */
10109 simplify_float_conversion_using_ranges (gimple_stmt_iterator
*gsi
,
10112 tree rhs1
= gimple_assign_rhs1 (stmt
);
10113 value_range
*vr
= get_value_range (rhs1
);
10114 scalar_float_mode fltmode
10115 = SCALAR_FLOAT_TYPE_MODE (TREE_TYPE (gimple_assign_lhs (stmt
)));
10116 scalar_int_mode mode
;
10120 /* We can only handle constant ranges. */
10121 if (vr
->type
!= VR_RANGE
10122 || TREE_CODE (vr
->min
) != INTEGER_CST
10123 || TREE_CODE (vr
->max
) != INTEGER_CST
)
10126 /* First check if we can use a signed type in place of an unsigned. */
10127 scalar_int_mode rhs_mode
= SCALAR_INT_TYPE_MODE (TREE_TYPE (rhs1
));
10128 if (TYPE_UNSIGNED (TREE_TYPE (rhs1
))
10129 && can_float_p (fltmode
, rhs_mode
, 0) != CODE_FOR_nothing
10130 && range_fits_type_p (vr
, TYPE_PRECISION (TREE_TYPE (rhs1
)), SIGNED
))
10132 /* If we can do the conversion in the current input mode do nothing. */
10133 else if (can_float_p (fltmode
, rhs_mode
,
10134 TYPE_UNSIGNED (TREE_TYPE (rhs1
))) != CODE_FOR_nothing
)
10136 /* Otherwise search for a mode we can use, starting from the narrowest
10137 integer mode available. */
10140 mode
= NARROWEST_INT_MODE
;
10143 /* If we cannot do a signed conversion to float from mode
10144 or if the value-range does not fit in the signed type
10145 try with a wider mode. */
10146 if (can_float_p (fltmode
, mode
, 0) != CODE_FOR_nothing
10147 && range_fits_type_p (vr
, GET_MODE_PRECISION (mode
), SIGNED
))
10150 /* But do not widen the input. Instead leave that to the
10151 optabs expansion code. */
10152 if (!GET_MODE_WIDER_MODE (mode
).exists (&mode
)
10153 || GET_MODE_PRECISION (mode
) > TYPE_PRECISION (TREE_TYPE (rhs1
)))
10158 /* It works, insert a truncation or sign-change before the
10159 float conversion. */
10160 tem
= make_ssa_name (build_nonstandard_integer_type
10161 (GET_MODE_PRECISION (mode
), 0));
10162 conv
= gimple_build_assign (tem
, NOP_EXPR
, rhs1
);
10163 gsi_insert_before (gsi
, conv
, GSI_SAME_STMT
);
10164 gimple_assign_set_rhs1 (stmt
, tem
);
10165 fold_stmt (gsi
, follow_single_use_edges
);
10170 /* Simplify an internal fn call using ranges if possible. */
10173 simplify_internal_call_using_ranges (gimple_stmt_iterator
*gsi
, gimple
*stmt
)
10175 enum tree_code subcode
;
10176 bool is_ubsan
= false;
10178 switch (gimple_call_internal_fn (stmt
))
10180 case IFN_UBSAN_CHECK_ADD
:
10181 subcode
= PLUS_EXPR
;
10184 case IFN_UBSAN_CHECK_SUB
:
10185 subcode
= MINUS_EXPR
;
10188 case IFN_UBSAN_CHECK_MUL
:
10189 subcode
= MULT_EXPR
;
10192 case IFN_ADD_OVERFLOW
:
10193 subcode
= PLUS_EXPR
;
10195 case IFN_SUB_OVERFLOW
:
10196 subcode
= MINUS_EXPR
;
10198 case IFN_MUL_OVERFLOW
:
10199 subcode
= MULT_EXPR
;
10205 tree op0
= gimple_call_arg (stmt
, 0);
10206 tree op1
= gimple_call_arg (stmt
, 1);
10210 type
= TREE_TYPE (op0
);
10211 if (VECTOR_TYPE_P (type
))
10214 else if (gimple_call_lhs (stmt
) == NULL_TREE
)
10217 type
= TREE_TYPE (TREE_TYPE (gimple_call_lhs (stmt
)));
10218 if (!check_for_binary_op_overflow (subcode
, type
, op0
, op1
, &ovf
)
10219 || (is_ubsan
&& ovf
))
10223 location_t loc
= gimple_location (stmt
);
10225 g
= gimple_build_assign (gimple_call_lhs (stmt
), subcode
, op0
, op1
);
10228 int prec
= TYPE_PRECISION (type
);
10231 || !useless_type_conversion_p (type
, TREE_TYPE (op0
))
10232 || !useless_type_conversion_p (type
, TREE_TYPE (op1
)))
10233 utype
= build_nonstandard_integer_type (prec
, 1);
10234 if (TREE_CODE (op0
) == INTEGER_CST
)
10235 op0
= fold_convert (utype
, op0
);
10236 else if (!useless_type_conversion_p (utype
, TREE_TYPE (op0
)))
10238 g
= gimple_build_assign (make_ssa_name (utype
), NOP_EXPR
, op0
);
10239 gimple_set_location (g
, loc
);
10240 gsi_insert_before (gsi
, g
, GSI_SAME_STMT
);
10241 op0
= gimple_assign_lhs (g
);
10243 if (TREE_CODE (op1
) == INTEGER_CST
)
10244 op1
= fold_convert (utype
, op1
);
10245 else if (!useless_type_conversion_p (utype
, TREE_TYPE (op1
)))
10247 g
= gimple_build_assign (make_ssa_name (utype
), NOP_EXPR
, op1
);
10248 gimple_set_location (g
, loc
);
10249 gsi_insert_before (gsi
, g
, GSI_SAME_STMT
);
10250 op1
= gimple_assign_lhs (g
);
10252 g
= gimple_build_assign (make_ssa_name (utype
), subcode
, op0
, op1
);
10253 gimple_set_location (g
, loc
);
10254 gsi_insert_before (gsi
, g
, GSI_SAME_STMT
);
10257 g
= gimple_build_assign (make_ssa_name (type
), NOP_EXPR
,
10258 gimple_assign_lhs (g
));
10259 gimple_set_location (g
, loc
);
10260 gsi_insert_before (gsi
, g
, GSI_SAME_STMT
);
10262 g
= gimple_build_assign (gimple_call_lhs (stmt
), COMPLEX_EXPR
,
10263 gimple_assign_lhs (g
),
10264 build_int_cst (type
, ovf
));
10266 gimple_set_location (g
, loc
);
10267 gsi_replace (gsi
, g
, false);
10271 /* Return true if VAR is a two-valued variable. Set a and b with the
10272 two-values when it is true. Return false otherwise. */
10275 two_valued_val_range_p (tree var
, tree
*a
, tree
*b
)
10277 value_range
*vr
= get_value_range (var
);
10278 if ((vr
->type
!= VR_RANGE
10279 && vr
->type
!= VR_ANTI_RANGE
)
10280 || TREE_CODE (vr
->min
) != INTEGER_CST
10281 || TREE_CODE (vr
->max
) != INTEGER_CST
)
10284 if (vr
->type
== VR_RANGE
10285 && wi::to_wide (vr
->max
) - wi::to_wide (vr
->min
) == 1)
10292 /* ~[TYPE_MIN + 1, TYPE_MAX - 1] */
10293 if (vr
->type
== VR_ANTI_RANGE
10294 && (wi::to_wide (vr
->min
)
10295 - wi::to_wide (vrp_val_min (TREE_TYPE (var
)))) == 1
10296 && (wi::to_wide (vrp_val_max (TREE_TYPE (var
)))
10297 - wi::to_wide (vr
->max
)) == 1)
10299 *a
= vrp_val_min (TREE_TYPE (var
));
10300 *b
= vrp_val_max (TREE_TYPE (var
));
10307 /* Simplify STMT using ranges if possible. */
10310 simplify_stmt_using_ranges (gimple_stmt_iterator
*gsi
)
10312 gimple
*stmt
= gsi_stmt (*gsi
);
10313 if (is_gimple_assign (stmt
))
10315 enum tree_code rhs_code
= gimple_assign_rhs_code (stmt
);
10316 tree rhs1
= gimple_assign_rhs1 (stmt
);
10317 tree rhs2
= gimple_assign_rhs2 (stmt
);
10318 tree lhs
= gimple_assign_lhs (stmt
);
10319 tree val1
= NULL_TREE
, val2
= NULL_TREE
;
10320 use_operand_p use_p
;
10324 LHS = CST BINOP VAR
10325 Where VAR is two-valued and LHS is used in GIMPLE_COND only
10327 LHS = VAR == VAL1 ? (CST BINOP VAL1) : (CST BINOP VAL2)
10330 LHS = VAR BINOP CST
10331 Where VAR is two-valued and LHS is used in GIMPLE_COND only
10333 LHS = VAR == VAL1 ? (VAL1 BINOP CST) : (VAL2 BINOP CST) */
10335 if (TREE_CODE_CLASS (rhs_code
) == tcc_binary
10336 && INTEGRAL_TYPE_P (TREE_TYPE (lhs
))
10337 && ((TREE_CODE (rhs1
) == INTEGER_CST
10338 && TREE_CODE (rhs2
) == SSA_NAME
)
10339 || (TREE_CODE (rhs2
) == INTEGER_CST
10340 && TREE_CODE (rhs1
) == SSA_NAME
))
10341 && single_imm_use (lhs
, &use_p
, &use_stmt
)
10342 && gimple_code (use_stmt
) == GIMPLE_COND
)
10345 tree new_rhs1
= NULL_TREE
;
10346 tree new_rhs2
= NULL_TREE
;
10347 tree cmp_var
= NULL_TREE
;
10349 if (TREE_CODE (rhs2
) == SSA_NAME
10350 && two_valued_val_range_p (rhs2
, &val1
, &val2
))
10352 /* Optimize RHS1 OP [VAL1, VAL2]. */
10353 new_rhs1
= int_const_binop (rhs_code
, rhs1
, val1
);
10354 new_rhs2
= int_const_binop (rhs_code
, rhs1
, val2
);
10357 else if (TREE_CODE (rhs1
) == SSA_NAME
10358 && two_valued_val_range_p (rhs1
, &val1
, &val2
))
10360 /* Optimize [VAL1, VAL2] OP RHS2. */
10361 new_rhs1
= int_const_binop (rhs_code
, val1
, rhs2
);
10362 new_rhs2
= int_const_binop (rhs_code
, val2
, rhs2
);
10366 /* If we could not find two-vals or the optimzation is invalid as
10367 in divide by zero, new_rhs1 / new_rhs will be NULL_TREE. */
10368 if (new_rhs1
&& new_rhs2
)
10370 tree cond
= build2 (EQ_EXPR
, boolean_type_node
, cmp_var
, val1
);
10371 gimple_assign_set_rhs_with_ops (gsi
,
10375 update_stmt (gsi_stmt (*gsi
));
10376 fold_stmt (gsi
, follow_single_use_edges
);
10385 /* Transform EQ_EXPR, NE_EXPR into BIT_XOR_EXPR or identity
10386 if the RHS is zero or one, and the LHS are known to be boolean
10388 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
10389 return simplify_truth_ops_using_ranges (gsi
, stmt
);
10392 /* Transform TRUNC_DIV_EXPR and TRUNC_MOD_EXPR into RSHIFT_EXPR
10393 and BIT_AND_EXPR respectively if the first operand is greater
10394 than zero and the second operand is an exact power of two.
10395 Also optimize TRUNC_MOD_EXPR away if the second operand is
10396 constant and the first operand already has the right value
10398 case TRUNC_DIV_EXPR
:
10399 case TRUNC_MOD_EXPR
:
10400 if ((TREE_CODE (rhs1
) == SSA_NAME
10401 || TREE_CODE (rhs1
) == INTEGER_CST
)
10402 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
10403 return simplify_div_or_mod_using_ranges (gsi
, stmt
);
10406 /* Transform ABS (X) into X or -X as appropriate. */
10408 if (TREE_CODE (rhs1
) == SSA_NAME
10409 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
10410 return simplify_abs_using_ranges (gsi
, stmt
);
10415 /* Optimize away BIT_AND_EXPR and BIT_IOR_EXPR
10416 if all the bits being cleared are already cleared or
10417 all the bits being set are already set. */
10418 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
10419 return simplify_bit_ops_using_ranges (gsi
, stmt
);
10423 if (TREE_CODE (rhs1
) == SSA_NAME
10424 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
10425 return simplify_conversion_using_ranges (gsi
, stmt
);
10429 if (TREE_CODE (rhs1
) == SSA_NAME
10430 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
10431 return simplify_float_conversion_using_ranges (gsi
, stmt
);
10436 return simplify_min_or_max_using_ranges (gsi
, stmt
);
10442 else if (gimple_code (stmt
) == GIMPLE_COND
)
10443 return simplify_cond_using_ranges_1 (as_a
<gcond
*> (stmt
));
10444 else if (gimple_code (stmt
) == GIMPLE_SWITCH
)
10445 return simplify_switch_using_ranges (as_a
<gswitch
*> (stmt
));
10446 else if (is_gimple_call (stmt
)
10447 && gimple_call_internal_p (stmt
))
10448 return simplify_internal_call_using_ranges (gsi
, stmt
);
10453 /* If the statement pointed by SI has a predicate whose value can be
10454 computed using the value range information computed by VRP, compute
10455 its value and return true. Otherwise, return false. */
10458 fold_predicate_in (gimple_stmt_iterator
*si
)
10460 bool assignment_p
= false;
10462 gimple
*stmt
= gsi_stmt (*si
);
10464 if (is_gimple_assign (stmt
)
10465 && TREE_CODE_CLASS (gimple_assign_rhs_code (stmt
)) == tcc_comparison
)
10467 assignment_p
= true;
10468 val
= vrp_evaluate_conditional (gimple_assign_rhs_code (stmt
),
10469 gimple_assign_rhs1 (stmt
),
10470 gimple_assign_rhs2 (stmt
),
10473 else if (gcond
*cond_stmt
= dyn_cast
<gcond
*> (stmt
))
10474 val
= vrp_evaluate_conditional (gimple_cond_code (cond_stmt
),
10475 gimple_cond_lhs (cond_stmt
),
10476 gimple_cond_rhs (cond_stmt
),
10484 val
= fold_convert (gimple_expr_type (stmt
), val
);
10488 fprintf (dump_file
, "Folding predicate ");
10489 print_gimple_expr (dump_file
, stmt
, 0);
10490 fprintf (dump_file
, " to ");
10491 print_generic_expr (dump_file
, val
);
10492 fprintf (dump_file
, "\n");
10495 if (is_gimple_assign (stmt
))
10496 gimple_assign_set_rhs_from_tree (si
, val
);
10499 gcc_assert (gimple_code (stmt
) == GIMPLE_COND
);
10500 gcond
*cond_stmt
= as_a
<gcond
*> (stmt
);
10501 if (integer_zerop (val
))
10502 gimple_cond_make_false (cond_stmt
);
10503 else if (integer_onep (val
))
10504 gimple_cond_make_true (cond_stmt
);
10506 gcc_unreachable ();
10515 class vrp_folder
: public substitute_and_fold_engine
10518 tree
get_value (tree
) FINAL OVERRIDE
;
10519 bool fold_stmt (gimple_stmt_iterator
*) FINAL OVERRIDE
;
10522 /* Callback for substitute_and_fold folding the stmt at *SI. */
10525 vrp_folder::fold_stmt (gimple_stmt_iterator
*si
)
10527 if (fold_predicate_in (si
))
10530 return simplify_stmt_using_ranges (si
);
10533 /* If OP has a value range with a single constant value return that,
10534 otherwise return NULL_TREE. This returns OP itself if OP is a
10537 Implemented as a pure wrapper right now, but this will change. */
10540 vrp_folder::get_value (tree op
)
10542 return op_with_constant_singleton_value_range (op
);
10545 /* Return the LHS of any ASSERT_EXPR where OP appears as the first
10546 argument to the ASSERT_EXPR and in which the ASSERT_EXPR dominates
10547 BB. If no such ASSERT_EXPR is found, return OP. */
10550 lhs_of_dominating_assert (tree op
, basic_block bb
, gimple
*stmt
)
10552 imm_use_iterator imm_iter
;
10554 use_operand_p use_p
;
10556 if (TREE_CODE (op
) == SSA_NAME
)
10558 FOR_EACH_IMM_USE_FAST (use_p
, imm_iter
, op
)
10560 use_stmt
= USE_STMT (use_p
);
10561 if (use_stmt
!= stmt
10562 && gimple_assign_single_p (use_stmt
)
10563 && TREE_CODE (gimple_assign_rhs1 (use_stmt
)) == ASSERT_EXPR
10564 && TREE_OPERAND (gimple_assign_rhs1 (use_stmt
), 0) == op
10565 && dominated_by_p (CDI_DOMINATORS
, bb
, gimple_bb (use_stmt
)))
10566 return gimple_assign_lhs (use_stmt
);
10572 /* A trivial wrapper so that we can present the generic jump threading
10573 code with a simple API for simplifying statements. STMT is the
10574 statement we want to simplify, WITHIN_STMT provides the location
10575 for any overflow warnings. */
10578 simplify_stmt_for_jump_threading (gimple
*stmt
, gimple
*within_stmt
,
10579 class avail_exprs_stack
*avail_exprs_stack ATTRIBUTE_UNUSED
,
10582 /* First see if the conditional is in the hash table. */
10583 tree cached_lhs
= avail_exprs_stack
->lookup_avail_expr (stmt
, false, true);
10584 if (cached_lhs
&& is_gimple_min_invariant (cached_lhs
))
10587 if (gcond
*cond_stmt
= dyn_cast
<gcond
*> (stmt
))
10589 tree op0
= gimple_cond_lhs (cond_stmt
);
10590 op0
= lhs_of_dominating_assert (op0
, bb
, stmt
);
10592 tree op1
= gimple_cond_rhs (cond_stmt
);
10593 op1
= lhs_of_dominating_assert (op1
, bb
, stmt
);
10595 return vrp_evaluate_conditional (gimple_cond_code (cond_stmt
),
10596 op0
, op1
, within_stmt
);
10599 /* We simplify a switch statement by trying to determine which case label
10600 will be taken. If we are successful then we return the corresponding
10601 CASE_LABEL_EXPR. */
10602 if (gswitch
*switch_stmt
= dyn_cast
<gswitch
*> (stmt
))
10604 tree op
= gimple_switch_index (switch_stmt
);
10605 if (TREE_CODE (op
) != SSA_NAME
)
10608 op
= lhs_of_dominating_assert (op
, bb
, stmt
);
10610 value_range
*vr
= get_value_range (op
);
10611 if ((vr
->type
!= VR_RANGE
&& vr
->type
!= VR_ANTI_RANGE
)
10612 || symbolic_range_p (vr
))
10615 if (vr
->type
== VR_RANGE
)
10618 /* Get the range of labels that contain a part of the operand's
10620 find_case_label_range (switch_stmt
, vr
->min
, vr
->max
, &i
, &j
);
10622 /* Is there only one such label? */
10625 tree label
= gimple_switch_label (switch_stmt
, i
);
10627 /* The i'th label will be taken only if the value range of the
10628 operand is entirely within the bounds of this label. */
10629 if (CASE_HIGH (label
) != NULL_TREE
10630 ? (tree_int_cst_compare (CASE_LOW (label
), vr
->min
) <= 0
10631 && tree_int_cst_compare (CASE_HIGH (label
), vr
->max
) >= 0)
10632 : (tree_int_cst_equal (CASE_LOW (label
), vr
->min
)
10633 && tree_int_cst_equal (vr
->min
, vr
->max
)))
10637 /* If there are no such labels then the default label will be
10640 return gimple_switch_label (switch_stmt
, 0);
10643 if (vr
->type
== VR_ANTI_RANGE
)
10645 unsigned n
= gimple_switch_num_labels (switch_stmt
);
10646 tree min_label
= gimple_switch_label (switch_stmt
, 1);
10647 tree max_label
= gimple_switch_label (switch_stmt
, n
- 1);
10649 /* The default label will be taken only if the anti-range of the
10650 operand is entirely outside the bounds of all the (non-default)
10652 if (tree_int_cst_compare (vr
->min
, CASE_LOW (min_label
)) <= 0
10653 && (CASE_HIGH (max_label
) != NULL_TREE
10654 ? tree_int_cst_compare (vr
->max
, CASE_HIGH (max_label
)) >= 0
10655 : tree_int_cst_compare (vr
->max
, CASE_LOW (max_label
)) >= 0))
10656 return gimple_switch_label (switch_stmt
, 0);
10662 if (gassign
*assign_stmt
= dyn_cast
<gassign
*> (stmt
))
10664 value_range new_vr
= VR_INITIALIZER
;
10665 tree lhs
= gimple_assign_lhs (assign_stmt
);
10667 if (TREE_CODE (lhs
) == SSA_NAME
10668 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs
))
10669 || POINTER_TYPE_P (TREE_TYPE (lhs
))))
10671 extract_range_from_assignment (&new_vr
, assign_stmt
);
10672 if (range_int_cst_singleton_p (&new_vr
))
10680 class vrp_dom_walker
: public dom_walker
10683 vrp_dom_walker (cdi_direction direction
,
10684 class const_and_copies
*const_and_copies
,
10685 class avail_exprs_stack
*avail_exprs_stack
)
10686 : dom_walker (direction
, true),
10687 m_const_and_copies (const_and_copies
),
10688 m_avail_exprs_stack (avail_exprs_stack
),
10689 m_dummy_cond (NULL
) {}
10691 virtual edge
before_dom_children (basic_block
);
10692 virtual void after_dom_children (basic_block
);
10695 class const_and_copies
*m_const_and_copies
;
10696 class avail_exprs_stack
*m_avail_exprs_stack
;
10698 gcond
*m_dummy_cond
;
10701 /* Called before processing dominator children of BB. We want to look
10702 at ASSERT_EXPRs and record information from them in the appropriate
10705 We could look at other statements here. It's not seen as likely
10706 to significantly increase the jump threads we discover. */
10709 vrp_dom_walker::before_dom_children (basic_block bb
)
10711 gimple_stmt_iterator gsi
;
10713 m_avail_exprs_stack
->push_marker ();
10714 m_const_and_copies
->push_marker ();
10715 for (gsi
= gsi_start_nondebug_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
10717 gimple
*stmt
= gsi_stmt (gsi
);
10718 if (gimple_assign_single_p (stmt
)
10719 && TREE_CODE (gimple_assign_rhs1 (stmt
)) == ASSERT_EXPR
)
10721 tree rhs1
= gimple_assign_rhs1 (stmt
);
10722 tree cond
= TREE_OPERAND (rhs1
, 1);
10723 tree inverted
= invert_truthvalue (cond
);
10724 vec
<cond_equivalence
> p
;
10726 record_conditions (&p
, cond
, inverted
);
10727 for (unsigned int i
= 0; i
< p
.length (); i
++)
10728 m_avail_exprs_stack
->record_cond (&p
[i
]);
10730 tree lhs
= gimple_assign_lhs (stmt
);
10731 m_const_and_copies
->record_const_or_copy (lhs
,
10732 TREE_OPERAND (rhs1
, 0));
10741 /* Called after processing dominator children of BB. This is where we
10742 actually call into the threader. */
10744 vrp_dom_walker::after_dom_children (basic_block bb
)
10747 m_dummy_cond
= gimple_build_cond (NE_EXPR
,
10748 integer_zero_node
, integer_zero_node
,
10751 thread_outgoing_edges (bb
, m_dummy_cond
, m_const_and_copies
,
10752 m_avail_exprs_stack
,
10753 simplify_stmt_for_jump_threading
);
10755 m_avail_exprs_stack
->pop_to_marker ();
10756 m_const_and_copies
->pop_to_marker ();
10759 /* Blocks which have more than one predecessor and more than
10760 one successor present jump threading opportunities, i.e.,
10761 when the block is reached from a specific predecessor, we
10762 may be able to determine which of the outgoing edges will
10763 be traversed. When this optimization applies, we are able
10764 to avoid conditionals at runtime and we may expose secondary
10765 optimization opportunities.
10767 This routine is effectively a driver for the generic jump
10768 threading code. It basically just presents the generic code
10769 with edges that may be suitable for jump threading.
10771 Unlike DOM, we do not iterate VRP if jump threading was successful.
10772 While iterating may expose new opportunities for VRP, it is expected
10773 those opportunities would be very limited and the compile time cost
10774 to expose those opportunities would be significant.
10776 As jump threading opportunities are discovered, they are registered
10777 for later realization. */
10780 identify_jump_threads (void)
10785 /* Ugh. When substituting values earlier in this pass we can
10786 wipe the dominance information. So rebuild the dominator
10787 information as we need it within the jump threading code. */
10788 calculate_dominance_info (CDI_DOMINATORS
);
10790 /* We do not allow VRP information to be used for jump threading
10791 across a back edge in the CFG. Otherwise it becomes too
10792 difficult to avoid eliminating loop exit tests. Of course
10793 EDGE_DFS_BACK is not accurate at this time so we have to
10795 mark_dfs_back_edges ();
10797 /* Do not thread across edges we are about to remove. Just marking
10798 them as EDGE_IGNORE will do. */
10799 FOR_EACH_VEC_ELT (to_remove_edges
, i
, e
)
10800 e
->flags
|= EDGE_IGNORE
;
10802 /* Allocate our unwinder stack to unwind any temporary equivalences
10803 that might be recorded. */
10804 const_and_copies
*equiv_stack
= new const_and_copies ();
10806 hash_table
<expr_elt_hasher
> *avail_exprs
10807 = new hash_table
<expr_elt_hasher
> (1024);
10808 avail_exprs_stack
*avail_exprs_stack
10809 = new class avail_exprs_stack (avail_exprs
);
10811 vrp_dom_walker
walker (CDI_DOMINATORS
, equiv_stack
, avail_exprs_stack
);
10812 walker
.walk (cfun
->cfg
->x_entry_block_ptr
);
10814 /* Clear EDGE_IGNORE. */
10815 FOR_EACH_VEC_ELT (to_remove_edges
, i
, e
)
10816 e
->flags
&= ~EDGE_IGNORE
;
10818 /* We do not actually update the CFG or SSA graphs at this point as
10819 ASSERT_EXPRs are still in the IL and cfg cleanup code does not yet
10820 handle ASSERT_EXPRs gracefully. */
10821 delete equiv_stack
;
10822 delete avail_exprs
;
10823 delete avail_exprs_stack
;
10826 /* Free VRP lattice. */
10829 vrp_free_lattice ()
10831 /* Free allocated memory. */
10833 free (vr_phi_edge_counts
);
10834 bitmap_obstack_release (&vrp_equiv_obstack
);
10835 vrp_value_range_pool
.release ();
10837 /* So that we can distinguish between VRP data being available
10838 and not available. */
10840 vr_phi_edge_counts
= NULL
;
10843 /* Traverse all the blocks folding conditionals with known ranges. */
10846 vrp_finalize (bool warn_array_bounds_p
)
10850 values_propagated
= true;
10854 fprintf (dump_file
, "\nValue ranges after VRP:\n\n");
10855 dump_all_value_ranges (dump_file
);
10856 fprintf (dump_file
, "\n");
10859 /* Set value range to non pointer SSA_NAMEs. */
10860 for (i
= 0; i
< num_vr_values
; i
++)
10863 tree name
= ssa_name (i
);
10866 || (vr_value
[i
]->type
== VR_VARYING
)
10867 || (vr_value
[i
]->type
== VR_UNDEFINED
)
10868 || (TREE_CODE (vr_value
[i
]->min
) != INTEGER_CST
)
10869 || (TREE_CODE (vr_value
[i
]->max
) != INTEGER_CST
))
10872 if (POINTER_TYPE_P (TREE_TYPE (name
))
10873 && ((vr_value
[i
]->type
== VR_RANGE
10874 && range_includes_zero_p (vr_value
[i
]->min
,
10875 vr_value
[i
]->max
) == 0)
10876 || (vr_value
[i
]->type
== VR_ANTI_RANGE
10877 && range_includes_zero_p (vr_value
[i
]->min
,
10878 vr_value
[i
]->max
) == 1)))
10879 set_ptr_nonnull (name
);
10880 else if (!POINTER_TYPE_P (TREE_TYPE (name
)))
10881 set_range_info (name
, vr_value
[i
]->type
,
10882 wi::to_wide (vr_value
[i
]->min
),
10883 wi::to_wide (vr_value
[i
]->max
));
10886 class vrp_folder vrp_folder
;
10887 vrp_folder
.substitute_and_fold ();
10889 if (warn_array_bounds
&& warn_array_bounds_p
)
10890 check_all_array_refs ();
10893 /* evrp_dom_walker visits the basic blocks in the dominance order and set
10894 the Value Ranges (VR) for SSA_NAMEs in the scope. Use this VR to
10895 discover more VRs. */
10897 class evrp_dom_walker
: public dom_walker
10901 : dom_walker (CDI_DOMINATORS
), stack (10)
10903 need_eh_cleanup
= BITMAP_ALLOC (NULL
);
10905 ~evrp_dom_walker ()
10907 BITMAP_FREE (need_eh_cleanup
);
10909 virtual edge
before_dom_children (basic_block
);
10910 virtual void after_dom_children (basic_block
);
10911 void push_value_range (tree var
, value_range
*vr
);
10912 value_range
*pop_value_range (tree var
);
10913 value_range
*try_find_new_range (tree
, tree op
, tree_code code
, tree limit
);
10915 /* Cond_stack holds the old VR. */
10916 auto_vec
<std::pair
<tree
, value_range
*> > stack
;
10917 bitmap need_eh_cleanup
;
10918 auto_vec
<gimple
*> stmts_to_fixup
;
10919 auto_vec
<gimple
*> stmts_to_remove
;
10922 /* Find new range for NAME such that (OP CODE LIMIT) is true. */
10925 evrp_dom_walker::try_find_new_range (tree name
,
10926 tree op
, tree_code code
, tree limit
)
10928 value_range vr
= VR_INITIALIZER
;
10929 value_range
*old_vr
= get_value_range (name
);
10931 /* Discover VR when condition is true. */
10932 extract_range_for_var_from_comparison_expr (name
, code
, op
,
10934 /* If we found any usable VR, set the VR to ssa_name and create a
10935 PUSH old value in the stack with the old VR. */
10936 if (vr
.type
== VR_RANGE
|| vr
.type
== VR_ANTI_RANGE
)
10938 if (old_vr
->type
== vr
.type
10939 && vrp_operand_equal_p (old_vr
->min
, vr
.min
)
10940 && vrp_operand_equal_p (old_vr
->max
, vr
.max
))
10942 value_range
*new_vr
= vrp_value_range_pool
.allocate ();
10949 /* See if there is any new scope is entered with new VR and set that VR to
10950 ssa_name before visiting the statements in the scope. */
10953 evrp_dom_walker::before_dom_children (basic_block bb
)
10955 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
10956 fprintf (dump_file
, "Visiting BB%d\n", bb
->index
);
10958 stack
.safe_push (std::make_pair (NULL_TREE
, (value_range
*)NULL
));
10960 edge pred_e
= single_pred_edge_ignoring_loop_edges (bb
, false);
10963 gimple
*stmt
= last_stmt (pred_e
->src
);
10964 tree op0
= NULL_TREE
;
10967 && gimple_code (stmt
) == GIMPLE_COND
10968 && (op0
= gimple_cond_lhs (stmt
))
10969 && TREE_CODE (op0
) == SSA_NAME
10970 && (INTEGRAL_TYPE_P (TREE_TYPE (gimple_cond_lhs (stmt
)))
10971 || POINTER_TYPE_P (TREE_TYPE (gimple_cond_lhs (stmt
)))))
10973 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
10975 fprintf (dump_file
, "Visiting controlling predicate ");
10976 print_gimple_stmt (dump_file
, stmt
, 0);
10978 /* Entering a new scope. Try to see if we can find a VR
10980 tree op1
= gimple_cond_rhs (stmt
);
10981 if (TREE_OVERFLOW_P (op1
))
10982 op1
= drop_tree_overflow (op1
);
10983 tree_code code
= gimple_cond_code (stmt
);
10985 auto_vec
<assert_info
, 8> asserts
;
10986 register_edge_assert_for (op0
, pred_e
, code
, op0
, op1
, asserts
);
10987 if (TREE_CODE (op1
) == SSA_NAME
)
10988 register_edge_assert_for (op1
, pred_e
, code
, op0
, op1
, asserts
);
10990 auto_vec
<std::pair
<tree
, value_range
*>, 8> vrs
;
10991 for (unsigned i
= 0; i
< asserts
.length (); ++i
)
10993 value_range
*vr
= try_find_new_range (asserts
[i
].name
,
10995 asserts
[i
].comp_code
,
10998 vrs
.safe_push (std::make_pair (asserts
[i
].name
, vr
));
11000 /* Push updated ranges only after finding all of them to avoid
11001 ordering issues that can lead to worse ranges. */
11002 for (unsigned i
= 0; i
< vrs
.length (); ++i
)
11003 push_value_range (vrs
[i
].first
, vrs
[i
].second
);
11007 /* Visit PHI stmts and discover any new VRs possible. */
11008 bool has_unvisited_preds
= false;
11011 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
11012 if (e
->flags
& EDGE_EXECUTABLE
11013 && !(e
->src
->flags
& BB_VISITED
))
11015 has_unvisited_preds
= true;
11019 for (gphi_iterator gpi
= gsi_start_phis (bb
);
11020 !gsi_end_p (gpi
); gsi_next (&gpi
))
11022 gphi
*phi
= gpi
.phi ();
11023 tree lhs
= PHI_RESULT (phi
);
11024 if (virtual_operand_p (lhs
))
11026 value_range vr_result
= VR_INITIALIZER
;
11027 bool interesting
= stmt_interesting_for_vrp (phi
);
11028 if (interesting
&& dump_file
&& (dump_flags
& TDF_DETAILS
))
11030 fprintf (dump_file
, "Visiting PHI node ");
11031 print_gimple_stmt (dump_file
, phi
, 0);
11033 if (!has_unvisited_preds
11035 extract_range_from_phi_node (phi
, &vr_result
);
11038 set_value_range_to_varying (&vr_result
);
11039 /* When we have an unvisited executable predecessor we can't
11040 use PHI arg ranges which may be still UNDEFINED but have
11041 to use VARYING for them. But we can still resort to
11042 SCEV for loop header PHIs. */
11045 && (l
= loop_containing_stmt (phi
))
11046 && l
->header
== gimple_bb (phi
))
11047 adjust_range_with_scev (&vr_result
, l
, phi
, lhs
);
11049 update_value_range (lhs
, &vr_result
);
11051 /* Mark PHIs whose lhs we fully propagate for removal. */
11052 tree val
= op_with_constant_singleton_value_range (lhs
);
11053 if (val
&& may_propagate_copy (lhs
, val
))
11055 stmts_to_remove
.safe_push (phi
);
11059 /* Set the SSA with the value range. */
11060 if (INTEGRAL_TYPE_P (TREE_TYPE (lhs
)))
11062 if ((vr_result
.type
== VR_RANGE
11063 || vr_result
.type
== VR_ANTI_RANGE
)
11064 && (TREE_CODE (vr_result
.min
) == INTEGER_CST
)
11065 && (TREE_CODE (vr_result
.max
) == INTEGER_CST
))
11066 set_range_info (lhs
, vr_result
.type
,
11067 wi::to_wide (vr_result
.min
),
11068 wi::to_wide (vr_result
.max
));
11070 else if (POINTER_TYPE_P (TREE_TYPE (lhs
))
11071 && ((vr_result
.type
== VR_RANGE
11072 && range_includes_zero_p (vr_result
.min
,
11073 vr_result
.max
) == 0)
11074 || (vr_result
.type
== VR_ANTI_RANGE
11075 && range_includes_zero_p (vr_result
.min
,
11076 vr_result
.max
) == 1)))
11077 set_ptr_nonnull (lhs
);
11080 edge taken_edge
= NULL
;
11082 /* Visit all other stmts and discover any new VRs possible. */
11083 for (gimple_stmt_iterator gsi
= gsi_start_bb (bb
);
11084 !gsi_end_p (gsi
); gsi_next (&gsi
))
11086 gimple
*stmt
= gsi_stmt (gsi
);
11087 tree output
= NULL_TREE
;
11088 gimple
*old_stmt
= stmt
;
11089 bool was_noreturn
= (is_gimple_call (stmt
)
11090 && gimple_call_noreturn_p (stmt
));
11092 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
11094 fprintf (dump_file
, "Visiting stmt ");
11095 print_gimple_stmt (dump_file
, stmt
, 0);
11098 if (gcond
*cond
= dyn_cast
<gcond
*> (stmt
))
11100 vrp_visit_cond_stmt (cond
, &taken_edge
);
11103 if (taken_edge
->flags
& EDGE_TRUE_VALUE
)
11104 gimple_cond_make_true (cond
);
11105 else if (taken_edge
->flags
& EDGE_FALSE_VALUE
)
11106 gimple_cond_make_false (cond
);
11108 gcc_unreachable ();
11109 update_stmt (stmt
);
11112 else if (stmt_interesting_for_vrp (stmt
))
11115 value_range vr
= VR_INITIALIZER
;
11116 extract_range_from_stmt (stmt
, &taken_edge
, &output
, &vr
);
11118 && (vr
.type
== VR_RANGE
|| vr
.type
== VR_ANTI_RANGE
))
11120 update_value_range (output
, &vr
);
11121 vr
= *get_value_range (output
);
11123 /* Mark stmts whose output we fully propagate for removal. */
11125 if ((val
= op_with_constant_singleton_value_range (output
))
11126 && may_propagate_copy (output
, val
)
11127 && !stmt_could_throw_p (stmt
)
11128 && !gimple_has_side_effects (stmt
))
11130 stmts_to_remove
.safe_push (stmt
);
11134 /* Set the SSA with the value range. */
11135 if (INTEGRAL_TYPE_P (TREE_TYPE (output
)))
11137 if ((vr
.type
== VR_RANGE
11138 || vr
.type
== VR_ANTI_RANGE
)
11139 && (TREE_CODE (vr
.min
) == INTEGER_CST
)
11140 && (TREE_CODE (vr
.max
) == INTEGER_CST
))
11141 set_range_info (output
, vr
.type
,
11142 wi::to_wide (vr
.min
),
11143 wi::to_wide (vr
.max
));
11145 else if (POINTER_TYPE_P (TREE_TYPE (output
))
11146 && ((vr
.type
== VR_RANGE
11147 && range_includes_zero_p (vr
.min
,
11149 || (vr
.type
== VR_ANTI_RANGE
11150 && range_includes_zero_p (vr
.min
,
11152 set_ptr_nonnull (output
);
11155 set_defs_to_varying (stmt
);
11158 set_defs_to_varying (stmt
);
11160 /* See if we can derive a range for any of STMT's operands. */
11163 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, i
, SSA_OP_USE
)
11166 enum tree_code comp_code
;
11168 /* If OP is used in such a way that we can infer a value
11169 range for it, and we don't find a previous assertion for
11170 it, create a new assertion location node for OP. */
11171 if (infer_value_range (stmt
, op
, &comp_code
, &value
))
11173 /* If we are able to infer a nonzero value range for OP,
11174 then walk backwards through the use-def chain to see if OP
11175 was set via a typecast.
11176 If so, then we can also infer a nonzero value range
11177 for the operand of the NOP_EXPR. */
11178 if (comp_code
== NE_EXPR
&& integer_zerop (value
))
11181 gimple
*def_stmt
= SSA_NAME_DEF_STMT (t
);
11182 while (is_gimple_assign (def_stmt
)
11183 && CONVERT_EXPR_CODE_P
11184 (gimple_assign_rhs_code (def_stmt
))
11186 (gimple_assign_rhs1 (def_stmt
)) == SSA_NAME
11188 (TREE_TYPE (gimple_assign_rhs1 (def_stmt
))))
11190 t
= gimple_assign_rhs1 (def_stmt
);
11191 def_stmt
= SSA_NAME_DEF_STMT (t
);
11193 /* Add VR when (T COMP_CODE value) condition is
11195 value_range
*op_range
11196 = try_find_new_range (t
, t
, comp_code
, value
);
11198 push_value_range (t
, op_range
);
11201 /* Add VR when (OP COMP_CODE value) condition is true. */
11202 value_range
*op_range
= try_find_new_range (op
, op
,
11205 push_value_range (op
, op_range
);
11209 /* Try folding stmts with the VR discovered. */
11210 class vrp_folder vrp_folder
;
11211 bool did_replace
= vrp_folder
.replace_uses_in (stmt
);
11212 if (fold_stmt (&gsi
, follow_single_use_edges
)
11215 stmt
= gsi_stmt (gsi
);
11216 update_stmt (stmt
);
11217 did_replace
= true;
11222 /* If we cleaned up EH information from the statement,
11223 remove EH edges. */
11224 if (maybe_clean_or_replace_eh_stmt (old_stmt
, stmt
))
11225 bitmap_set_bit (need_eh_cleanup
, bb
->index
);
11227 /* If we turned a not noreturn call into a noreturn one
11228 schedule it for fixup. */
11230 && is_gimple_call (stmt
)
11231 && gimple_call_noreturn_p (stmt
))
11232 stmts_to_fixup
.safe_push (stmt
);
11234 if (gimple_assign_single_p (stmt
))
11236 tree rhs
= gimple_assign_rhs1 (stmt
);
11237 if (TREE_CODE (rhs
) == ADDR_EXPR
)
11238 recompute_tree_invariant_for_addr_expr (rhs
);
11243 /* Visit BB successor PHI nodes and replace PHI args. */
11244 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
11246 for (gphi_iterator gpi
= gsi_start_phis (e
->dest
);
11247 !gsi_end_p (gpi
); gsi_next (&gpi
))
11249 gphi
*phi
= gpi
.phi ();
11250 use_operand_p use_p
= PHI_ARG_DEF_PTR_FROM_EDGE (phi
, e
);
11251 tree arg
= USE_FROM_PTR (use_p
);
11252 if (TREE_CODE (arg
) != SSA_NAME
11253 || virtual_operand_p (arg
))
11255 tree val
= op_with_constant_singleton_value_range (arg
);
11256 if (val
&& may_propagate_copy (arg
, val
))
11257 propagate_value (use_p
, val
);
11261 bb
->flags
|= BB_VISITED
;
11266 /* Restore/pop VRs valid only for BB when we leave BB. */
11269 evrp_dom_walker::after_dom_children (basic_block bb ATTRIBUTE_UNUSED
)
11271 gcc_checking_assert (!stack
.is_empty ());
11272 while (stack
.last ().first
!= NULL_TREE
)
11273 pop_value_range (stack
.last ().first
);
11277 /* Push the Value Range of VAR to the stack and update it with new VR. */
11280 evrp_dom_walker::push_value_range (tree var
, value_range
*vr
)
11282 if (SSA_NAME_VERSION (var
) >= num_vr_values
)
11284 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
11286 fprintf (dump_file
, "pushing new range for ");
11287 print_generic_expr (dump_file
, var
);
11288 fprintf (dump_file
, ": ");
11289 dump_value_range (dump_file
, vr
);
11290 fprintf (dump_file
, "\n");
11292 stack
.safe_push (std::make_pair (var
, get_value_range (var
)));
11293 vr_value
[SSA_NAME_VERSION (var
)] = vr
;
11296 /* Pop the Value Range from the vrp_stack and update VAR with it. */
11299 evrp_dom_walker::pop_value_range (tree var
)
11301 value_range
*vr
= stack
.last ().second
;
11302 gcc_checking_assert (var
== stack
.last ().first
);
11303 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
11305 fprintf (dump_file
, "popping range for ");
11306 print_generic_expr (dump_file
, var
);
11307 fprintf (dump_file
, ", restoring ");
11308 dump_value_range (dump_file
, vr
);
11309 fprintf (dump_file
, "\n");
11311 vr_value
[SSA_NAME_VERSION (var
)] = vr
;
11317 /* Main entry point for the early vrp pass which is a simplified non-iterative
11318 version of vrp where basic blocks are visited in dominance order. Value
11319 ranges discovered in early vrp will also be used by ipa-vrp. */
11321 static unsigned int
11322 execute_early_vrp ()
11328 loop_optimizer_init (LOOPS_NORMAL
| LOOPS_HAVE_RECORDED_EXITS
);
11329 rewrite_into_loop_closed_ssa (NULL
, TODO_update_ssa
);
11330 scev_initialize ();
11331 calculate_dominance_info (CDI_DOMINATORS
);
11332 FOR_EACH_BB_FN (bb
, cfun
)
11334 bb
->flags
&= ~BB_VISITED
;
11335 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
11336 e
->flags
|= EDGE_EXECUTABLE
;
11338 vrp_initialize_lattice ();
11340 /* Walk stmts in dominance order and propagate VRP. */
11341 evrp_dom_walker walker
;
11342 walker
.walk (ENTRY_BLOCK_PTR_FOR_FN (cfun
));
11346 fprintf (dump_file
, "\nValue ranges after Early VRP:\n\n");
11347 dump_all_value_ranges (dump_file
);
11348 fprintf (dump_file
, "\n");
11351 /* Remove stmts in reverse order to make debug stmt creation possible. */
11352 while (! walker
.stmts_to_remove
.is_empty ())
11354 gimple
*stmt
= walker
.stmts_to_remove
.pop ();
11355 if (dump_file
&& dump_flags
& TDF_DETAILS
)
11357 fprintf (dump_file
, "Removing dead stmt ");
11358 print_gimple_stmt (dump_file
, stmt
, 0);
11359 fprintf (dump_file
, "\n");
11361 gimple_stmt_iterator gsi
= gsi_for_stmt (stmt
);
11362 if (gimple_code (stmt
) == GIMPLE_PHI
)
11363 remove_phi_node (&gsi
, true);
11366 unlink_stmt_vdef (stmt
);
11367 gsi_remove (&gsi
, true);
11368 release_defs (stmt
);
11372 if (!bitmap_empty_p (walker
.need_eh_cleanup
))
11373 gimple_purge_all_dead_eh_edges (walker
.need_eh_cleanup
);
11375 /* Fixup stmts that became noreturn calls. This may require splitting
11376 blocks and thus isn't possible during the dominator walk. Do this
11377 in reverse order so we don't inadvertedly remove a stmt we want to
11378 fixup by visiting a dominating now noreturn call first. */
11379 while (!walker
.stmts_to_fixup
.is_empty ())
11381 gimple
*stmt
= walker
.stmts_to_fixup
.pop ();
11382 fixup_noreturn_call (stmt
);
11385 vrp_free_lattice ();
11387 loop_optimizer_finalize ();
11392 /* Main entry point to VRP (Value Range Propagation). This pass is
11393 loosely based on J. R. C. Patterson, ``Accurate Static Branch
11394 Prediction by Value Range Propagation,'' in SIGPLAN Conference on
11395 Programming Language Design and Implementation, pp. 67-78, 1995.
11396 Also available at http://citeseer.ist.psu.edu/patterson95accurate.html
11398 This is essentially an SSA-CCP pass modified to deal with ranges
11399 instead of constants.
11401 While propagating ranges, we may find that two or more SSA name
11402 have equivalent, though distinct ranges. For instance,
11405 2 p_4 = ASSERT_EXPR <p_3, p_3 != 0>
11407 4 p_5 = ASSERT_EXPR <p_4, p_4 == q_2>;
11411 In the code above, pointer p_5 has range [q_2, q_2], but from the
11412 code we can also determine that p_5 cannot be NULL and, if q_2 had
11413 a non-varying range, p_5's range should also be compatible with it.
11415 These equivalences are created by two expressions: ASSERT_EXPR and
11416 copy operations. Since p_5 is an assertion on p_4, and p_4 was the
11417 result of another assertion, then we can use the fact that p_5 and
11418 p_4 are equivalent when evaluating p_5's range.
11420 Together with value ranges, we also propagate these equivalences
11421 between names so that we can take advantage of information from
11422 multiple ranges when doing final replacement. Note that this
11423 equivalency relation is transitive but not symmetric.
11425 In the example above, p_5 is equivalent to p_4, q_2 and p_3, but we
11426 cannot assert that q_2 is equivalent to p_5 because q_2 may be used
11427 in contexts where that assertion does not hold (e.g., in line 6).
11429 TODO, the main difference between this pass and Patterson's is that
11430 we do not propagate edge probabilities. We only compute whether
11431 edges can be taken or not. That is, instead of having a spectrum
11432 of jump probabilities between 0 and 1, we only deal with 0, 1 and
11433 DON'T KNOW. In the future, it may be worthwhile to propagate
11434 probabilities to aid branch prediction. */
11436 static unsigned int
11437 execute_vrp (bool warn_array_bounds_p
)
11443 loop_optimizer_init (LOOPS_NORMAL
| LOOPS_HAVE_RECORDED_EXITS
);
11444 rewrite_into_loop_closed_ssa (NULL
, TODO_update_ssa
);
11445 scev_initialize ();
11447 /* ??? This ends up using stale EDGE_DFS_BACK for liveness computation.
11448 Inserting assertions may split edges which will invalidate
11450 insert_range_assertions ();
11452 to_remove_edges
.create (10);
11453 to_update_switch_stmts
.create (5);
11454 threadedge_initialize_values ();
11456 /* For visiting PHI nodes we need EDGE_DFS_BACK computed. */
11457 mark_dfs_back_edges ();
11459 vrp_initialize_lattice ();
11461 class vrp_prop vrp_prop
;
11462 vrp_prop
.ssa_propagate ();
11463 vrp_finalize (warn_array_bounds_p
);
11465 /* We must identify jump threading opportunities before we release
11466 the datastructures built by VRP. */
11467 identify_jump_threads ();
11469 /* A comparison of an SSA_NAME against a constant where the SSA_NAME
11470 was set by a type conversion can often be rewritten to use the
11471 RHS of the type conversion.
11473 However, doing so inhibits jump threading through the comparison.
11474 So that transformation is not performed until after jump threading
11477 FOR_EACH_BB_FN (bb
, cfun
)
11479 gimple
*last
= last_stmt (bb
);
11480 if (last
&& gimple_code (last
) == GIMPLE_COND
)
11481 simplify_cond_using_ranges_2 (as_a
<gcond
*> (last
));
11484 vrp_free_lattice ();
11486 free_numbers_of_iterations_estimates (cfun
);
11488 /* ASSERT_EXPRs must be removed before finalizing jump threads
11489 as finalizing jump threads calls the CFG cleanup code which
11490 does not properly handle ASSERT_EXPRs. */
11491 remove_range_assertions ();
11493 /* If we exposed any new variables, go ahead and put them into
11494 SSA form now, before we handle jump threading. This simplifies
11495 interactions between rewriting of _DECL nodes into SSA form
11496 and rewriting SSA_NAME nodes into SSA form after block
11497 duplication and CFG manipulation. */
11498 update_ssa (TODO_update_ssa
);
11500 /* We identified all the jump threading opportunities earlier, but could
11501 not transform the CFG at that time. This routine transforms the
11502 CFG and arranges for the dominator tree to be rebuilt if necessary.
11504 Note the SSA graph update will occur during the normal TODO
11505 processing by the pass manager. */
11506 thread_through_all_blocks (false);
11508 /* Remove dead edges from SWITCH_EXPR optimization. This leaves the
11509 CFG in a broken state and requires a cfg_cleanup run. */
11510 FOR_EACH_VEC_ELT (to_remove_edges
, i
, e
)
11512 /* Update SWITCH_EXPR case label vector. */
11513 FOR_EACH_VEC_ELT (to_update_switch_stmts
, i
, su
)
11516 size_t n
= TREE_VEC_LENGTH (su
->vec
);
11518 gimple_switch_set_num_labels (su
->stmt
, n
);
11519 for (j
= 0; j
< n
; j
++)
11520 gimple_switch_set_label (su
->stmt
, j
, TREE_VEC_ELT (su
->vec
, j
));
11521 /* As we may have replaced the default label with a regular one
11522 make sure to make it a real default label again. This ensures
11523 optimal expansion. */
11524 label
= gimple_switch_label (su
->stmt
, 0);
11525 CASE_LOW (label
) = NULL_TREE
;
11526 CASE_HIGH (label
) = NULL_TREE
;
11529 if (to_remove_edges
.length () > 0)
11531 free_dominance_info (CDI_DOMINATORS
);
11532 loops_state_set (LOOPS_NEED_FIXUP
);
11535 to_remove_edges
.release ();
11536 to_update_switch_stmts
.release ();
11537 threadedge_finalize_values ();
11540 loop_optimizer_finalize ();
11546 const pass_data pass_data_vrp
=
11548 GIMPLE_PASS
, /* type */
11550 OPTGROUP_NONE
, /* optinfo_flags */
11551 TV_TREE_VRP
, /* tv_id */
11552 PROP_ssa
, /* properties_required */
11553 0, /* properties_provided */
11554 0, /* properties_destroyed */
11555 0, /* todo_flags_start */
11556 ( TODO_cleanup_cfg
| TODO_update_ssa
), /* todo_flags_finish */
11559 class pass_vrp
: public gimple_opt_pass
11562 pass_vrp (gcc::context
*ctxt
)
11563 : gimple_opt_pass (pass_data_vrp
, ctxt
), warn_array_bounds_p (false)
11566 /* opt_pass methods: */
11567 opt_pass
* clone () { return new pass_vrp (m_ctxt
); }
11568 void set_pass_param (unsigned int n
, bool param
)
11570 gcc_assert (n
== 0);
11571 warn_array_bounds_p
= param
;
11573 virtual bool gate (function
*) { return flag_tree_vrp
!= 0; }
11574 virtual unsigned int execute (function
*)
11575 { return execute_vrp (warn_array_bounds_p
); }
11578 bool warn_array_bounds_p
;
11579 }; // class pass_vrp
11581 } // anon namespace
11584 make_pass_vrp (gcc::context
*ctxt
)
11586 return new pass_vrp (ctxt
);
11591 const pass_data pass_data_early_vrp
=
11593 GIMPLE_PASS
, /* type */
11595 OPTGROUP_NONE
, /* optinfo_flags */
11596 TV_TREE_EARLY_VRP
, /* tv_id */
11597 PROP_ssa
, /* properties_required */
11598 0, /* properties_provided */
11599 0, /* properties_destroyed */
11600 0, /* todo_flags_start */
11601 ( TODO_cleanup_cfg
| TODO_update_ssa
| TODO_verify_all
),
11604 class pass_early_vrp
: public gimple_opt_pass
11607 pass_early_vrp (gcc::context
*ctxt
)
11608 : gimple_opt_pass (pass_data_early_vrp
, ctxt
)
11611 /* opt_pass methods: */
11612 opt_pass
* clone () { return new pass_early_vrp (m_ctxt
); }
11613 virtual bool gate (function
*)
11615 return flag_tree_vrp
!= 0;
11617 virtual unsigned int execute (function
*)
11618 { return execute_early_vrp (); }
11620 }; // class pass_vrp
11621 } // anon namespace
11624 make_pass_early_vrp (gcc::context
*ctxt
)
11626 return new pass_early_vrp (ctxt
);