1 /* Support routines for Value Range Propagation (VRP).
2 Copyright (C) 2005-2021 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"
24 #include "basic-block.h"
28 #include "dominance.h"
33 #include "tree-pass.h"
35 #include "gimple-pretty-print.h"
36 #include "fold-const.h"
38 #include "gimple-iterator.h"
40 #include "tree-ssa-loop-manip.h"
41 #include "tree-ssa-loop-niter.h"
42 #include "tree-into-ssa.h"
44 #include "tree-scalar-evolution.h"
45 #include "tree-ssa-propagate.h"
46 #include "tree-ssa-threadedge.h"
48 #include "vr-values.h"
49 #include "gimple-array-bounds.h"
50 #include "gimple-range.h"
51 #include "gimple-range-path.h"
53 /* Set of SSA names found live during the RPO traversal of the function
54 for still active basic-blocks. */
60 void set (tree
, basic_block
);
61 void clear (tree
, basic_block
);
62 void merge (basic_block dest
, basic_block src
);
63 bool live_on_block_p (tree
, basic_block
);
64 bool live_on_edge_p (tree
, edge
);
65 bool block_has_live_names_p (basic_block
);
66 void clear_block (basic_block
);
71 void init_bitmap_if_needed (basic_block
);
75 live_names::init_bitmap_if_needed (basic_block bb
)
77 unsigned i
= bb
->index
;
80 live
[i
] = sbitmap_alloc (num_ssa_names
);
81 bitmap_clear (live
[i
]);
86 live_names::block_has_live_names_p (basic_block bb
)
88 unsigned i
= bb
->index
;
89 return live
[i
] && bitmap_empty_p (live
[i
]);
93 live_names::clear_block (basic_block bb
)
95 unsigned i
= bb
->index
;
98 sbitmap_free (live
[i
]);
104 live_names::merge (basic_block dest
, basic_block src
)
106 init_bitmap_if_needed (dest
);
107 init_bitmap_if_needed (src
);
108 bitmap_ior (live
[dest
->index
], live
[dest
->index
], live
[src
->index
]);
112 live_names::set (tree name
, basic_block bb
)
114 init_bitmap_if_needed (bb
);
115 bitmap_set_bit (live
[bb
->index
], SSA_NAME_VERSION (name
));
119 live_names::clear (tree name
, basic_block bb
)
121 unsigned i
= bb
->index
;
123 bitmap_clear_bit (live
[i
], SSA_NAME_VERSION (name
));
126 live_names::live_names ()
128 num_blocks
= last_basic_block_for_fn (cfun
);
129 live
= XCNEWVEC (sbitmap
, num_blocks
);
132 live_names::~live_names ()
134 for (unsigned i
= 0; i
< num_blocks
; ++i
)
136 sbitmap_free (live
[i
]);
141 live_names::live_on_block_p (tree name
, basic_block bb
)
143 return (live
[bb
->index
]
144 && bitmap_bit_p (live
[bb
->index
], SSA_NAME_VERSION (name
)));
147 /* Return true if the SSA name NAME is live on the edge E. */
150 live_names::live_on_edge_p (tree name
, edge e
)
152 return live_on_block_p (name
, e
->dest
);
156 /* VR_TYPE describes a range with mininum value *MIN and maximum
157 value *MAX. Restrict the range to the set of values that have
158 no bits set outside NONZERO_BITS. Update *MIN and *MAX and
159 return the new range type.
161 SGN gives the sign of the values described by the range. */
163 enum value_range_kind
164 intersect_range_with_nonzero_bits (enum value_range_kind vr_type
,
165 wide_int
*min
, wide_int
*max
,
166 const wide_int
&nonzero_bits
,
169 if (vr_type
== VR_ANTI_RANGE
)
171 /* The VR_ANTI_RANGE is equivalent to the union of the ranges
172 A: [-INF, *MIN) and B: (*MAX, +INF]. First use NONZERO_BITS
173 to create an inclusive upper bound for A and an inclusive lower
175 wide_int a_max
= wi::round_down_for_mask (*min
- 1, nonzero_bits
);
176 wide_int b_min
= wi::round_up_for_mask (*max
+ 1, nonzero_bits
);
178 /* If the calculation of A_MAX wrapped, A is effectively empty
179 and A_MAX is the highest value that satisfies NONZERO_BITS.
180 Likewise if the calculation of B_MIN wrapped, B is effectively
181 empty and B_MIN is the lowest value that satisfies NONZERO_BITS. */
182 bool a_empty
= wi::ge_p (a_max
, *min
, sgn
);
183 bool b_empty
= wi::le_p (b_min
, *max
, sgn
);
185 /* If both A and B are empty, there are no valid values. */
186 if (a_empty
&& b_empty
)
189 /* If exactly one of A or B is empty, return a VR_RANGE for the
191 if (a_empty
|| b_empty
)
195 gcc_checking_assert (wi::le_p (*min
, *max
, sgn
));
199 /* Update the VR_ANTI_RANGE bounds. */
202 gcc_checking_assert (wi::le_p (*min
, *max
, sgn
));
204 /* Now check whether the excluded range includes any values that
205 satisfy NONZERO_BITS. If not, switch to a full VR_RANGE. */
206 if (wi::round_up_for_mask (*min
, nonzero_bits
) == b_min
)
208 unsigned int precision
= min
->get_precision ();
209 *min
= wi::min_value (precision
, sgn
);
210 *max
= wi::max_value (precision
, sgn
);
214 if (vr_type
== VR_RANGE
|| vr_type
== VR_VARYING
)
216 *max
= wi::round_down_for_mask (*max
, nonzero_bits
);
218 /* Check that the range contains at least one valid value. */
219 if (wi::gt_p (*min
, *max
, sgn
))
222 *min
= wi::round_up_for_mask (*min
, nonzero_bits
);
223 gcc_checking_assert (wi::le_p (*min
, *max
, sgn
));
228 /* Return true if max and min of VR are INTEGER_CST. It's not necessary
232 range_int_cst_p (const value_range
*vr
)
234 return (vr
->kind () == VR_RANGE
&& range_has_numeric_bounds_p (vr
));
237 /* Return the single symbol (an SSA_NAME) contained in T if any, or NULL_TREE
238 otherwise. We only handle additive operations and set NEG to true if the
239 symbol is negated and INV to the invariant part, if any. */
242 get_single_symbol (tree t
, bool *neg
, tree
*inv
)
250 if (TREE_CODE (t
) == PLUS_EXPR
251 || TREE_CODE (t
) == POINTER_PLUS_EXPR
252 || TREE_CODE (t
) == MINUS_EXPR
)
254 if (is_gimple_min_invariant (TREE_OPERAND (t
, 0)))
256 neg_
= (TREE_CODE (t
) == MINUS_EXPR
);
257 inv_
= TREE_OPERAND (t
, 0);
258 t
= TREE_OPERAND (t
, 1);
260 else if (is_gimple_min_invariant (TREE_OPERAND (t
, 1)))
263 inv_
= TREE_OPERAND (t
, 1);
264 t
= TREE_OPERAND (t
, 0);
275 if (TREE_CODE (t
) == NEGATE_EXPR
)
277 t
= TREE_OPERAND (t
, 0);
281 if (TREE_CODE (t
) != SSA_NAME
)
284 if (inv_
&& TREE_OVERFLOW_P (inv_
))
285 inv_
= drop_tree_overflow (inv_
);
292 /* The reverse operation: build a symbolic expression with TYPE
293 from symbol SYM, negated according to NEG, and invariant INV. */
296 build_symbolic_expr (tree type
, tree sym
, bool neg
, tree inv
)
298 const bool pointer_p
= POINTER_TYPE_P (type
);
302 t
= build1 (NEGATE_EXPR
, type
, t
);
304 if (integer_zerop (inv
))
307 return build2 (pointer_p
? POINTER_PLUS_EXPR
: PLUS_EXPR
, type
, t
, inv
);
313 -2 if those are incomparable. */
315 operand_less_p (tree val
, tree val2
)
317 /* LT is folded faster than GE and others. Inline the common case. */
318 if (TREE_CODE (val
) == INTEGER_CST
&& TREE_CODE (val2
) == INTEGER_CST
)
319 return tree_int_cst_lt (val
, val2
);
320 else if (TREE_CODE (val
) == SSA_NAME
&& TREE_CODE (val2
) == SSA_NAME
)
321 return val
== val2
? 0 : -2;
324 int cmp
= compare_values (val
, val2
);
327 else if (cmp
== 0 || cmp
== 1)
336 /* Compare two values VAL1 and VAL2. Return
338 -2 if VAL1 and VAL2 cannot be compared at compile-time,
341 +1 if VAL1 > VAL2, and
344 This is similar to tree_int_cst_compare but supports pointer values
345 and values that cannot be compared at compile time.
347 If STRICT_OVERFLOW_P is not NULL, then set *STRICT_OVERFLOW_P to
348 true if the return value is only valid if we assume that signed
349 overflow is undefined. */
352 compare_values_warnv (tree val1
, tree val2
, bool *strict_overflow_p
)
357 /* Below we rely on the fact that VAL1 and VAL2 are both pointers or
359 gcc_assert (POINTER_TYPE_P (TREE_TYPE (val1
))
360 == POINTER_TYPE_P (TREE_TYPE (val2
)));
362 /* Convert the two values into the same type. This is needed because
363 sizetype causes sign extension even for unsigned types. */
364 if (!useless_type_conversion_p (TREE_TYPE (val1
), TREE_TYPE (val2
)))
365 val2
= fold_convert (TREE_TYPE (val1
), val2
);
367 const bool overflow_undefined
368 = INTEGRAL_TYPE_P (TREE_TYPE (val1
))
369 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (val1
));
372 tree sym1
= get_single_symbol (val1
, &neg1
, &inv1
);
373 tree sym2
= get_single_symbol (val2
, &neg2
, &inv2
);
375 /* If VAL1 and VAL2 are of the form '[-]NAME [+ CST]', return -1 or +1
376 accordingly. If VAL1 and VAL2 don't use the same name, return -2. */
379 /* Both values must use the same name with the same sign. */
380 if (sym1
!= sym2
|| neg1
!= neg2
)
383 /* [-]NAME + CST == [-]NAME + CST. */
387 /* If overflow is defined we cannot simplify more. */
388 if (!overflow_undefined
)
391 if (strict_overflow_p
!= NULL
392 /* Symbolic range building sets the no-warning bit to declare
393 that overflow doesn't happen. */
394 && (!inv1
|| !warning_suppressed_p (val1
, OPT_Woverflow
))
395 && (!inv2
|| !warning_suppressed_p (val2
, OPT_Woverflow
)))
396 *strict_overflow_p
= true;
399 inv1
= build_int_cst (TREE_TYPE (val1
), 0);
401 inv2
= build_int_cst (TREE_TYPE (val2
), 0);
403 return wi::cmp (wi::to_wide (inv1
), wi::to_wide (inv2
),
404 TYPE_SIGN (TREE_TYPE (val1
)));
407 const bool cst1
= is_gimple_min_invariant (val1
);
408 const bool cst2
= is_gimple_min_invariant (val2
);
410 /* If one is of the form '[-]NAME + CST' and the other is constant, then
411 it might be possible to say something depending on the constants. */
412 if ((sym1
&& inv1
&& cst2
) || (sym2
&& inv2
&& cst1
))
414 if (!overflow_undefined
)
417 if (strict_overflow_p
!= NULL
418 /* Symbolic range building sets the no-warning bit to declare
419 that overflow doesn't happen. */
420 && (!sym1
|| !warning_suppressed_p (val1
, OPT_Woverflow
))
421 && (!sym2
|| !warning_suppressed_p (val2
, OPT_Woverflow
)))
422 *strict_overflow_p
= true;
424 const signop sgn
= TYPE_SIGN (TREE_TYPE (val1
));
425 tree cst
= cst1
? val1
: val2
;
426 tree inv
= cst1
? inv2
: inv1
;
428 /* Compute the difference between the constants. If it overflows or
429 underflows, this means that we can trivially compare the NAME with
430 it and, consequently, the two values with each other. */
431 wide_int diff
= wi::to_wide (cst
) - wi::to_wide (inv
);
432 if (wi::cmp (0, wi::to_wide (inv
), sgn
)
433 != wi::cmp (diff
, wi::to_wide (cst
), sgn
))
435 const int res
= wi::cmp (wi::to_wide (cst
), wi::to_wide (inv
), sgn
);
436 return cst1
? res
: -res
;
442 /* We cannot say anything more for non-constants. */
446 if (!POINTER_TYPE_P (TREE_TYPE (val1
)))
448 /* We cannot compare overflowed values. */
449 if (TREE_OVERFLOW (val1
) || TREE_OVERFLOW (val2
))
452 if (TREE_CODE (val1
) == INTEGER_CST
453 && TREE_CODE (val2
) == INTEGER_CST
)
454 return tree_int_cst_compare (val1
, val2
);
456 if (poly_int_tree_p (val1
) && poly_int_tree_p (val2
))
458 if (known_eq (wi::to_poly_widest (val1
),
459 wi::to_poly_widest (val2
)))
461 if (known_lt (wi::to_poly_widest (val1
),
462 wi::to_poly_widest (val2
)))
464 if (known_gt (wi::to_poly_widest (val1
),
465 wi::to_poly_widest (val2
)))
473 if (TREE_CODE (val1
) == INTEGER_CST
&& TREE_CODE (val2
) == INTEGER_CST
)
475 /* We cannot compare overflowed values. */
476 if (TREE_OVERFLOW (val1
) || TREE_OVERFLOW (val2
))
479 return tree_int_cst_compare (val1
, val2
);
482 /* First see if VAL1 and VAL2 are not the same. */
483 if (operand_equal_p (val1
, val2
, 0))
486 fold_defer_overflow_warnings ();
488 /* If VAL1 is a lower address than VAL2, return -1. */
489 tree t
= fold_binary_to_constant (LT_EXPR
, boolean_type_node
, val1
, val2
);
490 if (t
&& integer_onep (t
))
492 fold_undefer_and_ignore_overflow_warnings ();
496 /* If VAL1 is a higher address than VAL2, return +1. */
497 t
= fold_binary_to_constant (LT_EXPR
, boolean_type_node
, val2
, val1
);
498 if (t
&& integer_onep (t
))
500 fold_undefer_and_ignore_overflow_warnings ();
504 /* If VAL1 is different than VAL2, return +2. */
505 t
= fold_binary_to_constant (NE_EXPR
, boolean_type_node
, val1
, val2
);
506 fold_undefer_and_ignore_overflow_warnings ();
507 if (t
&& integer_onep (t
))
514 /* Compare values like compare_values_warnv. */
517 compare_values (tree val1
, tree val2
)
520 return compare_values_warnv (val1
, val2
, &sop
);
523 /* If BOUND will include a symbolic bound, adjust it accordingly,
524 otherwise leave it as is.
526 CODE is the original operation that combined the bounds (PLUS_EXPR
529 TYPE is the type of the original operation.
531 SYM_OPn is the symbolic for OPn if it has a symbolic.
533 NEG_OPn is TRUE if the OPn was negated. */
536 adjust_symbolic_bound (tree
&bound
, enum tree_code code
, tree type
,
537 tree sym_op0
, tree sym_op1
,
538 bool neg_op0
, bool neg_op1
)
540 bool minus_p
= (code
== MINUS_EXPR
);
541 /* If the result bound is constant, we're done; otherwise, build the
542 symbolic lower bound. */
543 if (sym_op0
== sym_op1
)
546 bound
= build_symbolic_expr (type
, sym_op0
,
550 /* We may not negate if that might introduce
551 undefined overflow. */
554 || TYPE_OVERFLOW_WRAPS (type
))
555 bound
= build_symbolic_expr (type
, sym_op1
,
556 neg_op1
^ minus_p
, bound
);
562 /* Combine OP1 and OP1, which are two parts of a bound, into one wide
563 int bound according to CODE. CODE is the operation combining the
564 bound (either a PLUS_EXPR or a MINUS_EXPR).
566 TYPE is the type of the combine operation.
568 WI is the wide int to store the result.
570 OVF is -1 if an underflow occurred, +1 if an overflow occurred or 0
571 if over/underflow occurred. */
574 combine_bound (enum tree_code code
, wide_int
&wi
, wi::overflow_type
&ovf
,
575 tree type
, tree op0
, tree op1
)
577 bool minus_p
= (code
== MINUS_EXPR
);
578 const signop sgn
= TYPE_SIGN (type
);
579 const unsigned int prec
= TYPE_PRECISION (type
);
581 /* Combine the bounds, if any. */
585 wi
= wi::sub (wi::to_wide (op0
), wi::to_wide (op1
), sgn
, &ovf
);
587 wi
= wi::add (wi::to_wide (op0
), wi::to_wide (op1
), sgn
, &ovf
);
590 wi
= wi::to_wide (op0
);
594 wi
= wi::neg (wi::to_wide (op1
), &ovf
);
596 wi
= wi::to_wide (op1
);
599 wi
= wi::shwi (0, prec
);
602 /* Given a range in [WMIN, WMAX], adjust it for possible overflow and
603 put the result in VR.
605 TYPE is the type of the range.
607 MIN_OVF and MAX_OVF indicate what type of overflow, if any,
608 occurred while originally calculating WMIN or WMAX. -1 indicates
609 underflow. +1 indicates overflow. 0 indicates neither. */
612 set_value_range_with_overflow (value_range_kind
&kind
, tree
&min
, tree
&max
,
614 const wide_int
&wmin
, const wide_int
&wmax
,
615 wi::overflow_type min_ovf
,
616 wi::overflow_type max_ovf
)
618 const signop sgn
= TYPE_SIGN (type
);
619 const unsigned int prec
= TYPE_PRECISION (type
);
621 /* For one bit precision if max < min, then the swapped
622 range covers all values. */
623 if (prec
== 1 && wi::lt_p (wmax
, wmin
, sgn
))
629 if (TYPE_OVERFLOW_WRAPS (type
))
631 /* If overflow wraps, truncate the values and adjust the
632 range kind and bounds appropriately. */
633 wide_int tmin
= wide_int::from (wmin
, prec
, sgn
);
634 wide_int tmax
= wide_int::from (wmax
, prec
, sgn
);
635 if ((min_ovf
!= wi::OVF_NONE
) == (max_ovf
!= wi::OVF_NONE
))
637 /* If the limits are swapped, we wrapped around and cover
639 if (wi::gt_p (tmin
, tmax
, sgn
))
644 /* No overflow or both overflow or underflow. The
645 range kind stays VR_RANGE. */
646 min
= wide_int_to_tree (type
, tmin
);
647 max
= wide_int_to_tree (type
, tmax
);
651 else if ((min_ovf
== wi::OVF_UNDERFLOW
&& max_ovf
== wi::OVF_NONE
)
652 || (max_ovf
== wi::OVF_OVERFLOW
&& min_ovf
== wi::OVF_NONE
))
654 /* Min underflow or max overflow. The range kind
655 changes to VR_ANTI_RANGE. */
659 if (wi::cmp (tmin
, tmax
, sgn
) < 0)
662 if (wi::cmp (tmax
, tem
, sgn
) > 0)
664 /* If the anti-range would cover nothing, drop to varying.
665 Likewise if the anti-range bounds are outside of the
667 if (covers
|| wi::cmp (tmin
, tmax
, sgn
) > 0)
672 kind
= VR_ANTI_RANGE
;
673 min
= wide_int_to_tree (type
, tmin
);
674 max
= wide_int_to_tree (type
, tmax
);
679 /* Other underflow and/or overflow, drop to VR_VARYING. */
686 /* If overflow does not wrap, saturate to the types min/max
688 wide_int type_min
= wi::min_value (prec
, sgn
);
689 wide_int type_max
= wi::max_value (prec
, sgn
);
691 if (min_ovf
== wi::OVF_UNDERFLOW
)
692 min
= wide_int_to_tree (type
, type_min
);
693 else if (min_ovf
== wi::OVF_OVERFLOW
)
694 min
= wide_int_to_tree (type
, type_max
);
696 min
= wide_int_to_tree (type
, wmin
);
698 if (max_ovf
== wi::OVF_UNDERFLOW
)
699 max
= wide_int_to_tree (type
, type_min
);
700 else if (max_ovf
== wi::OVF_OVERFLOW
)
701 max
= wide_int_to_tree (type
, type_max
);
703 max
= wide_int_to_tree (type
, wmax
);
707 /* Fold two value range's of a POINTER_PLUS_EXPR into VR. */
710 extract_range_from_pointer_plus_expr (value_range
*vr
,
713 const value_range
*vr0
,
714 const value_range
*vr1
)
716 gcc_checking_assert (POINTER_TYPE_P (expr_type
)
717 && code
== POINTER_PLUS_EXPR
);
718 /* For pointer types, we are really only interested in asserting
719 whether the expression evaluates to non-NULL.
720 With -fno-delete-null-pointer-checks we need to be more
721 conservative. As some object might reside at address 0,
722 then some offset could be added to it and the same offset
723 subtracted again and the result would be NULL.
725 static int a[12]; where &a[0] is NULL and
728 ptr will be NULL here, even when there is POINTER_PLUS_EXPR
729 where the first range doesn't include zero and the second one
730 doesn't either. As the second operand is sizetype (unsigned),
731 consider all ranges where the MSB could be set as possible
732 subtractions where the result might be NULL. */
733 if ((!range_includes_zero_p (vr0
)
734 || !range_includes_zero_p (vr1
))
735 && !TYPE_OVERFLOW_WRAPS (expr_type
)
736 && (flag_delete_null_pointer_checks
737 || (range_int_cst_p (vr1
)
738 && !tree_int_cst_sign_bit (vr1
->max ()))))
739 vr
->set_nonzero (expr_type
);
740 else if (vr0
->zero_p () && vr1
->zero_p ())
741 vr
->set_zero (expr_type
);
743 vr
->set_varying (expr_type
);
746 /* Extract range information from a PLUS/MINUS_EXPR and store the
750 extract_range_from_plus_minus_expr (value_range
*vr
,
753 const value_range
*vr0_
,
754 const value_range
*vr1_
)
756 gcc_checking_assert (code
== PLUS_EXPR
|| code
== MINUS_EXPR
);
758 value_range vr0
= *vr0_
, vr1
= *vr1_
;
759 value_range vrtem0
, vrtem1
;
761 /* Now canonicalize anti-ranges to ranges when they are not symbolic
762 and express ~[] op X as ([]' op X) U ([]'' op X). */
763 if (vr0
.kind () == VR_ANTI_RANGE
764 && ranges_from_anti_range (&vr0
, &vrtem0
, &vrtem1
))
766 extract_range_from_plus_minus_expr (vr
, code
, expr_type
, &vrtem0
, vr1_
);
767 if (!vrtem1
.undefined_p ())
770 extract_range_from_plus_minus_expr (&vrres
, code
, expr_type
,
776 /* Likewise for X op ~[]. */
777 if (vr1
.kind () == VR_ANTI_RANGE
778 && ranges_from_anti_range (&vr1
, &vrtem0
, &vrtem1
))
780 extract_range_from_plus_minus_expr (vr
, code
, expr_type
, vr0_
, &vrtem0
);
781 if (!vrtem1
.undefined_p ())
784 extract_range_from_plus_minus_expr (&vrres
, code
, expr_type
,
791 value_range_kind kind
;
792 value_range_kind vr0_kind
= vr0
.kind (), vr1_kind
= vr1
.kind ();
793 tree vr0_min
= vr0
.min (), vr0_max
= vr0
.max ();
794 tree vr1_min
= vr1
.min (), vr1_max
= vr1
.max ();
795 tree min
= NULL_TREE
, max
= NULL_TREE
;
797 /* This will normalize things such that calculating
798 [0,0] - VR_VARYING is not dropped to varying, but is
799 calculated as [MIN+1, MAX]. */
800 if (vr0
.varying_p ())
803 vr0_min
= vrp_val_min (expr_type
);
804 vr0_max
= vrp_val_max (expr_type
);
806 if (vr1
.varying_p ())
809 vr1_min
= vrp_val_min (expr_type
);
810 vr1_max
= vrp_val_max (expr_type
);
813 const bool minus_p
= (code
== MINUS_EXPR
);
814 tree min_op0
= vr0_min
;
815 tree min_op1
= minus_p
? vr1_max
: vr1_min
;
816 tree max_op0
= vr0_max
;
817 tree max_op1
= minus_p
? vr1_min
: vr1_max
;
818 tree sym_min_op0
= NULL_TREE
;
819 tree sym_min_op1
= NULL_TREE
;
820 tree sym_max_op0
= NULL_TREE
;
821 tree sym_max_op1
= NULL_TREE
;
822 bool neg_min_op0
, neg_min_op1
, neg_max_op0
, neg_max_op1
;
824 neg_min_op0
= neg_min_op1
= neg_max_op0
= neg_max_op1
= false;
826 /* If we have a PLUS or MINUS with two VR_RANGEs, either constant or
827 single-symbolic ranges, try to compute the precise resulting range,
828 but only if we know that this resulting range will also be constant
829 or single-symbolic. */
830 if (vr0_kind
== VR_RANGE
&& vr1_kind
== VR_RANGE
831 && (TREE_CODE (min_op0
) == INTEGER_CST
833 = get_single_symbol (min_op0
, &neg_min_op0
, &min_op0
)))
834 && (TREE_CODE (min_op1
) == INTEGER_CST
836 = get_single_symbol (min_op1
, &neg_min_op1
, &min_op1
)))
837 && (!(sym_min_op0
&& sym_min_op1
)
838 || (sym_min_op0
== sym_min_op1
839 && neg_min_op0
== (minus_p
? neg_min_op1
: !neg_min_op1
)))
840 && (TREE_CODE (max_op0
) == INTEGER_CST
842 = get_single_symbol (max_op0
, &neg_max_op0
, &max_op0
)))
843 && (TREE_CODE (max_op1
) == INTEGER_CST
845 = get_single_symbol (max_op1
, &neg_max_op1
, &max_op1
)))
846 && (!(sym_max_op0
&& sym_max_op1
)
847 || (sym_max_op0
== sym_max_op1
848 && neg_max_op0
== (minus_p
? neg_max_op1
: !neg_max_op1
))))
851 wi::overflow_type min_ovf
= wi::OVF_NONE
;
852 wi::overflow_type max_ovf
= wi::OVF_NONE
;
854 /* Build the bounds. */
855 combine_bound (code
, wmin
, min_ovf
, expr_type
, min_op0
, min_op1
);
856 combine_bound (code
, wmax
, max_ovf
, expr_type
, max_op0
, max_op1
);
858 /* If the resulting range will be symbolic, we need to eliminate any
859 explicit or implicit overflow introduced in the above computation
860 because compare_values could make an incorrect use of it. That's
861 why we require one of the ranges to be a singleton. */
862 if ((sym_min_op0
!= sym_min_op1
|| sym_max_op0
!= sym_max_op1
)
863 && ((bool)min_ovf
|| (bool)max_ovf
864 || (min_op0
!= max_op0
&& min_op1
!= max_op1
)))
866 vr
->set_varying (expr_type
);
870 /* Adjust the range for possible overflow. */
871 set_value_range_with_overflow (kind
, min
, max
, expr_type
,
872 wmin
, wmax
, min_ovf
, max_ovf
);
873 if (kind
== VR_VARYING
)
875 vr
->set_varying (expr_type
);
879 /* Build the symbolic bounds if needed. */
880 adjust_symbolic_bound (min
, code
, expr_type
,
881 sym_min_op0
, sym_min_op1
,
882 neg_min_op0
, neg_min_op1
);
883 adjust_symbolic_bound (max
, code
, expr_type
,
884 sym_max_op0
, sym_max_op1
,
885 neg_max_op0
, neg_max_op1
);
889 /* For other cases, for example if we have a PLUS_EXPR with two
890 VR_ANTI_RANGEs, drop to VR_VARYING. It would take more effort
891 to compute a precise range for such a case.
892 ??? General even mixed range kind operations can be expressed
893 by for example transforming ~[3, 5] + [1, 2] to range-only
894 operations and a union primitive:
895 [-INF, 2] + [1, 2] U [5, +INF] + [1, 2]
896 [-INF+1, 4] U [6, +INF(OVF)]
897 though usually the union is not exactly representable with
898 a single range or anti-range as the above is
899 [-INF+1, +INF(OVF)] intersected with ~[5, 5]
900 but one could use a scheme similar to equivalences for this. */
901 vr
->set_varying (expr_type
);
905 /* If either MIN or MAX overflowed, then set the resulting range to
908 || TREE_OVERFLOW_P (min
)
910 || TREE_OVERFLOW_P (max
))
912 vr
->set_varying (expr_type
);
916 int cmp
= compare_values (min
, max
);
917 if (cmp
== -2 || cmp
== 1)
919 /* If the new range has its limits swapped around (MIN > MAX),
920 then the operation caused one of them to wrap around, mark
921 the new range VARYING. */
922 vr
->set_varying (expr_type
);
925 vr
->set (min
, max
, kind
);
928 /* Return the range-ops handler for CODE and EXPR_TYPE. If no
929 suitable operator is found, return NULL and set VR to VARYING. */
931 static const range_operator
*
932 get_range_op_handler (value_range
*vr
,
936 const range_operator
*op
= range_op_handler (code
, expr_type
);
938 vr
->set_varying (expr_type
);
942 /* If the types passed are supported, return TRUE, otherwise set VR to
943 VARYING and return FALSE. */
946 supported_types_p (value_range
*vr
,
950 if (!value_range::supports_type_p (type0
)
951 || (type1
&& !value_range::supports_type_p (type1
)))
953 vr
->set_varying (type0
);
959 /* If any of the ranges passed are defined, return TRUE, otherwise set
960 VR to UNDEFINED and return FALSE. */
963 defined_ranges_p (value_range
*vr
,
964 const value_range
*vr0
, const value_range
*vr1
= NULL
)
966 if (vr0
->undefined_p () && (!vr1
|| vr1
->undefined_p ()))
968 vr
->set_undefined ();
975 drop_undefines_to_varying (const value_range
*vr
, tree expr_type
)
977 if (vr
->undefined_p ())
978 return value_range (expr_type
);
983 /* If any operand is symbolic, perform a binary operation on them and
984 return TRUE, otherwise return FALSE. */
987 range_fold_binary_symbolics_p (value_range
*vr
,
990 const value_range
*vr0_
,
991 const value_range
*vr1_
)
993 if (vr0_
->symbolic_p () || vr1_
->symbolic_p ())
995 value_range vr0
= drop_undefines_to_varying (vr0_
, expr_type
);
996 value_range vr1
= drop_undefines_to_varying (vr1_
, expr_type
);
997 if ((code
== PLUS_EXPR
|| code
== MINUS_EXPR
))
999 extract_range_from_plus_minus_expr (vr
, code
, expr_type
,
1003 if (POINTER_TYPE_P (expr_type
) && code
== POINTER_PLUS_EXPR
)
1005 extract_range_from_pointer_plus_expr (vr
, code
, expr_type
,
1009 const range_operator
*op
= get_range_op_handler (vr
, code
, expr_type
);
1010 vr0
.normalize_symbolics ();
1011 vr1
.normalize_symbolics ();
1012 return op
->fold_range (*vr
, expr_type
, vr0
, vr1
);
1017 /* If operand is symbolic, perform a unary operation on it and return
1018 TRUE, otherwise return FALSE. */
1021 range_fold_unary_symbolics_p (value_range
*vr
,
1024 const value_range
*vr0
)
1026 if (vr0
->symbolic_p ())
1028 if (code
== NEGATE_EXPR
)
1030 /* -X is simply 0 - X. */
1032 zero
.set_zero (vr0
->type ());
1033 range_fold_binary_expr (vr
, MINUS_EXPR
, expr_type
, &zero
, vr0
);
1036 if (code
== BIT_NOT_EXPR
)
1038 /* ~X is simply -1 - X. */
1039 value_range minusone
;
1040 minusone
.set (build_int_cst (vr0
->type (), -1));
1041 range_fold_binary_expr (vr
, MINUS_EXPR
, expr_type
, &minusone
, vr0
);
1044 const range_operator
*op
= get_range_op_handler (vr
, code
, expr_type
);
1045 value_range
vr0_cst (*vr0
);
1046 vr0_cst
.normalize_symbolics ();
1047 return op
->fold_range (*vr
, expr_type
, vr0_cst
, value_range (expr_type
));
1052 /* Perform a binary operation on a pair of ranges. */
1055 range_fold_binary_expr (value_range
*vr
,
1056 enum tree_code code
,
1058 const value_range
*vr0_
,
1059 const value_range
*vr1_
)
1061 if (!supported_types_p (vr
, expr_type
)
1062 || !defined_ranges_p (vr
, vr0_
, vr1_
))
1064 const range_operator
*op
= get_range_op_handler (vr
, code
, expr_type
);
1068 if (range_fold_binary_symbolics_p (vr
, code
, expr_type
, vr0_
, vr1_
))
1071 value_range
vr0 (*vr0_
);
1072 value_range
vr1 (*vr1_
);
1073 if (vr0
.undefined_p ())
1074 vr0
.set_varying (expr_type
);
1075 if (vr1
.undefined_p ())
1076 vr1
.set_varying (expr_type
);
1077 vr0
.normalize_addresses ();
1078 vr1
.normalize_addresses ();
1079 op
->fold_range (*vr
, expr_type
, vr0
, vr1
);
1082 /* Perform a unary operation on a range. */
1085 range_fold_unary_expr (value_range
*vr
,
1086 enum tree_code code
, tree expr_type
,
1087 const value_range
*vr0
,
1090 if (!supported_types_p (vr
, expr_type
, vr0_type
)
1091 || !defined_ranges_p (vr
, vr0
))
1093 const range_operator
*op
= get_range_op_handler (vr
, code
, expr_type
);
1097 if (range_fold_unary_symbolics_p (vr
, code
, expr_type
, vr0
))
1100 value_range
vr0_cst (*vr0
);
1101 vr0_cst
.normalize_addresses ();
1102 op
->fold_range (*vr
, expr_type
, vr0_cst
, value_range (expr_type
));
1105 /* If the range of values taken by OP can be inferred after STMT executes,
1106 return the comparison code (COMP_CODE_P) and value (VAL_P) that
1107 describes the inferred range. Return true if a range could be
1111 infer_value_range (gimple
*stmt
, tree op
, tree_code
*comp_code_p
, tree
*val_p
)
1114 *comp_code_p
= ERROR_MARK
;
1116 /* Do not attempt to infer anything in names that flow through
1118 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op
))
1121 /* If STMT is the last statement of a basic block with no normal
1122 successors, there is no point inferring anything about any of its
1123 operands. We would not be able to find a proper insertion point
1124 for the assertion, anyway. */
1125 if (stmt_ends_bb_p (stmt
))
1130 FOR_EACH_EDGE (e
, ei
, gimple_bb (stmt
)->succs
)
1131 if (!(e
->flags
& (EDGE_ABNORMAL
|EDGE_EH
)))
1137 if (infer_nonnull_range (stmt
, op
))
1139 *val_p
= build_int_cst (TREE_TYPE (op
), 0);
1140 *comp_code_p
= NE_EXPR
;
1147 /* Dump assert_info structure. */
1150 dump_assert_info (FILE *file
, const assert_info
&assert)
1152 fprintf (file
, "Assert for: ");
1153 print_generic_expr (file
, assert.name
);
1154 fprintf (file
, "\n\tPREDICATE: expr=[");
1155 print_generic_expr (file
, assert.expr
);
1156 fprintf (file
, "] %s ", get_tree_code_name (assert.comp_code
));
1157 fprintf (file
, "val=[");
1158 print_generic_expr (file
, assert.val
);
1159 fprintf (file
, "]\n\n");
1163 debug (const assert_info
&assert)
1165 dump_assert_info (stderr
, assert);
1168 /* Dump a vector of assert_info's. */
1171 dump_asserts_info (FILE *file
, const vec
<assert_info
> &asserts
)
1173 for (unsigned i
= 0; i
< asserts
.length (); ++i
)
1175 dump_assert_info (file
, asserts
[i
]);
1176 fprintf (file
, "\n");
1181 debug (const vec
<assert_info
> &asserts
)
1183 dump_asserts_info (stderr
, asserts
);
1186 /* Push the assert info for NAME, EXPR, COMP_CODE and VAL to ASSERTS. */
1189 add_assert_info (vec
<assert_info
> &asserts
,
1190 tree name
, tree expr
, enum tree_code comp_code
, tree val
)
1193 info
.comp_code
= comp_code
;
1195 if (TREE_OVERFLOW_P (val
))
1196 val
= drop_tree_overflow (val
);
1199 asserts
.safe_push (info
);
1200 if (dump_enabled_p ())
1201 dump_printf (MSG_NOTE
| MSG_PRIORITY_INTERNALS
,
1202 "Adding assert for %T from %T %s %T\n",
1203 name
, expr
, op_symbol_code (comp_code
), val
);
1206 /* (COND_OP0 COND_CODE COND_OP1) is a predicate which uses NAME.
1207 Extract a suitable test code and value and store them into *CODE_P and
1208 *VAL_P so the predicate is normalized to NAME *CODE_P *VAL_P.
1210 If no extraction was possible, return FALSE, otherwise return TRUE.
1212 If INVERT is true, then we invert the result stored into *CODE_P. */
1215 extract_code_and_val_from_cond_with_ops (tree name
, enum tree_code cond_code
,
1216 tree cond_op0
, tree cond_op1
,
1217 bool invert
, enum tree_code
*code_p
,
1220 enum tree_code comp_code
;
1223 /* Otherwise, we have a comparison of the form NAME COMP VAL
1224 or VAL COMP NAME. */
1225 if (name
== cond_op1
)
1227 /* If the predicate is of the form VAL COMP NAME, flip
1228 COMP around because we need to register NAME as the
1229 first operand in the predicate. */
1230 comp_code
= swap_tree_comparison (cond_code
);
1233 else if (name
== cond_op0
)
1235 /* The comparison is of the form NAME COMP VAL, so the
1236 comparison code remains unchanged. */
1237 comp_code
= cond_code
;
1243 /* Invert the comparison code as necessary. */
1245 comp_code
= invert_tree_comparison (comp_code
, 0);
1247 /* VRP only handles integral and pointer types. */
1248 if (! INTEGRAL_TYPE_P (TREE_TYPE (val
))
1249 && ! POINTER_TYPE_P (TREE_TYPE (val
)))
1252 /* Do not register always-false predicates.
1253 FIXME: this works around a limitation in fold() when dealing with
1254 enumerations. Given 'enum { N1, N2 } x;', fold will not
1255 fold 'if (x > N2)' to 'if (0)'. */
1256 if ((comp_code
== GT_EXPR
|| comp_code
== LT_EXPR
)
1257 && INTEGRAL_TYPE_P (TREE_TYPE (val
)))
1259 tree min
= TYPE_MIN_VALUE (TREE_TYPE (val
));
1260 tree max
= TYPE_MAX_VALUE (TREE_TYPE (val
));
1262 if (comp_code
== GT_EXPR
1264 || compare_values (val
, max
) == 0))
1267 if (comp_code
== LT_EXPR
1269 || compare_values (val
, min
) == 0))
1272 *code_p
= comp_code
;
1277 /* Find out smallest RES where RES > VAL && (RES & MASK) == RES, if any
1278 (otherwise return VAL). VAL and MASK must be zero-extended for
1279 precision PREC. If SGNBIT is non-zero, first xor VAL with SGNBIT
1280 (to transform signed values into unsigned) and at the end xor
1284 masked_increment (const wide_int
&val_in
, const wide_int
&mask
,
1285 const wide_int
&sgnbit
, unsigned int prec
)
1287 wide_int bit
= wi::one (prec
), res
;
1290 wide_int val
= val_in
^ sgnbit
;
1291 for (i
= 0; i
< prec
; i
++, bit
+= bit
)
1294 if ((res
& bit
) == 0)
1297 res
= wi::bit_and_not (val
+ bit
, res
);
1299 if (wi::gtu_p (res
, val
))
1300 return res
^ sgnbit
;
1302 return val
^ sgnbit
;
1305 /* Helper for overflow_comparison_p
1307 OP0 CODE OP1 is a comparison. Examine the comparison and potentially
1308 OP1's defining statement to see if it ultimately has the form
1309 OP0 CODE (OP0 PLUS INTEGER_CST)
1311 If so, return TRUE indicating this is an overflow test and store into
1312 *NEW_CST an updated constant that can be used in a narrowed range test.
1314 REVERSED indicates if the comparison was originally:
1318 This affects how we build the updated constant. */
1321 overflow_comparison_p_1 (enum tree_code code
, tree op0
, tree op1
,
1322 bool follow_assert_exprs
, bool reversed
, tree
*new_cst
)
1324 /* See if this is a relational operation between two SSA_NAMES with
1325 unsigned, overflow wrapping values. If so, check it more deeply. */
1326 if ((code
== LT_EXPR
|| code
== LE_EXPR
1327 || code
== GE_EXPR
|| code
== GT_EXPR
)
1328 && TREE_CODE (op0
) == SSA_NAME
1329 && TREE_CODE (op1
) == SSA_NAME
1330 && INTEGRAL_TYPE_P (TREE_TYPE (op0
))
1331 && TYPE_UNSIGNED (TREE_TYPE (op0
))
1332 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (op0
)))
1334 gimple
*op1_def
= SSA_NAME_DEF_STMT (op1
);
1336 /* If requested, follow any ASSERT_EXPRs backwards for OP1. */
1337 if (follow_assert_exprs
)
1339 while (gimple_assign_single_p (op1_def
)
1340 && TREE_CODE (gimple_assign_rhs1 (op1_def
)) == ASSERT_EXPR
)
1342 op1
= TREE_OPERAND (gimple_assign_rhs1 (op1_def
), 0);
1343 if (TREE_CODE (op1
) != SSA_NAME
)
1345 op1_def
= SSA_NAME_DEF_STMT (op1
);
1349 /* Now look at the defining statement of OP1 to see if it adds
1350 or subtracts a nonzero constant from another operand. */
1352 && is_gimple_assign (op1_def
)
1353 && gimple_assign_rhs_code (op1_def
) == PLUS_EXPR
1354 && TREE_CODE (gimple_assign_rhs2 (op1_def
)) == INTEGER_CST
1355 && !integer_zerop (gimple_assign_rhs2 (op1_def
)))
1357 tree target
= gimple_assign_rhs1 (op1_def
);
1359 /* If requested, follow ASSERT_EXPRs backwards for op0 looking
1360 for one where TARGET appears on the RHS. */
1361 if (follow_assert_exprs
)
1363 /* Now see if that "other operand" is op0, following the chain
1364 of ASSERT_EXPRs if necessary. */
1365 gimple
*op0_def
= SSA_NAME_DEF_STMT (op0
);
1366 while (op0
!= target
1367 && gimple_assign_single_p (op0_def
)
1368 && TREE_CODE (gimple_assign_rhs1 (op0_def
)) == ASSERT_EXPR
)
1370 op0
= TREE_OPERAND (gimple_assign_rhs1 (op0_def
), 0);
1371 if (TREE_CODE (op0
) != SSA_NAME
)
1373 op0_def
= SSA_NAME_DEF_STMT (op0
);
1377 /* If we did not find our target SSA_NAME, then this is not
1378 an overflow test. */
1382 tree type
= TREE_TYPE (op0
);
1383 wide_int max
= wi::max_value (TYPE_PRECISION (type
), UNSIGNED
);
1384 tree inc
= gimple_assign_rhs2 (op1_def
);
1386 *new_cst
= wide_int_to_tree (type
, max
+ wi::to_wide (inc
));
1388 *new_cst
= wide_int_to_tree (type
, max
- wi::to_wide (inc
));
1395 /* OP0 CODE OP1 is a comparison. Examine the comparison and potentially
1396 OP1's defining statement to see if it ultimately has the form
1397 OP0 CODE (OP0 PLUS INTEGER_CST)
1399 If so, return TRUE indicating this is an overflow test and store into
1400 *NEW_CST an updated constant that can be used in a narrowed range test.
1402 These statements are left as-is in the IL to facilitate discovery of
1403 {ADD,SUB}_OVERFLOW sequences later in the optimizer pipeline. But
1404 the alternate range representation is often useful within VRP. */
1407 overflow_comparison_p (tree_code code
, tree name
, tree val
,
1408 bool use_equiv_p
, tree
*new_cst
)
1410 if (overflow_comparison_p_1 (code
, name
, val
, use_equiv_p
, false, new_cst
))
1412 return overflow_comparison_p_1 (swap_tree_comparison (code
), val
, name
,
1413 use_equiv_p
, true, new_cst
);
1417 /* Try to register an edge assertion for SSA name NAME on edge E for
1418 the condition COND contributing to the conditional jump pointed to by BSI.
1419 Invert the condition COND if INVERT is true. */
1422 register_edge_assert_for_2 (tree name
, edge e
,
1423 enum tree_code cond_code
,
1424 tree cond_op0
, tree cond_op1
, bool invert
,
1425 vec
<assert_info
> &asserts
)
1428 enum tree_code comp_code
;
1430 if (!extract_code_and_val_from_cond_with_ops (name
, cond_code
,
1433 invert
, &comp_code
, &val
))
1436 /* Queue the assert. */
1438 if (overflow_comparison_p (comp_code
, name
, val
, false, &x
))
1440 enum tree_code new_code
= ((comp_code
== GT_EXPR
|| comp_code
== GE_EXPR
)
1441 ? GT_EXPR
: LE_EXPR
);
1442 add_assert_info (asserts
, name
, name
, new_code
, x
);
1444 add_assert_info (asserts
, name
, name
, comp_code
, val
);
1446 /* In the case of NAME <= CST and NAME being defined as
1447 NAME = (unsigned) NAME2 + CST2 we can assert NAME2 >= -CST2
1448 and NAME2 <= CST - CST2. We can do the same for NAME > CST.
1449 This catches range and anti-range tests. */
1450 if ((comp_code
== LE_EXPR
1451 || comp_code
== GT_EXPR
)
1452 && TREE_CODE (val
) == INTEGER_CST
1453 && TYPE_UNSIGNED (TREE_TYPE (val
)))
1455 gimple
*def_stmt
= SSA_NAME_DEF_STMT (name
);
1456 tree cst2
= NULL_TREE
, name2
= NULL_TREE
, name3
= NULL_TREE
;
1458 /* Extract CST2 from the (optional) addition. */
1459 if (is_gimple_assign (def_stmt
)
1460 && gimple_assign_rhs_code (def_stmt
) == PLUS_EXPR
)
1462 name2
= gimple_assign_rhs1 (def_stmt
);
1463 cst2
= gimple_assign_rhs2 (def_stmt
);
1464 if (TREE_CODE (name2
) == SSA_NAME
1465 && TREE_CODE (cst2
) == INTEGER_CST
)
1466 def_stmt
= SSA_NAME_DEF_STMT (name2
);
1469 /* Extract NAME2 from the (optional) sign-changing cast. */
1470 if (gassign
*ass
= dyn_cast
<gassign
*> (def_stmt
))
1472 if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (ass
))
1473 && ! TYPE_UNSIGNED (TREE_TYPE (gimple_assign_rhs1 (ass
)))
1474 && (TYPE_PRECISION (TREE_TYPE (gimple_assign_lhs (ass
)))
1475 == TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (ass
)))))
1476 name3
= gimple_assign_rhs1 (ass
);
1479 /* If name3 is used later, create an ASSERT_EXPR for it. */
1480 if (name3
!= NULL_TREE
1481 && TREE_CODE (name3
) == SSA_NAME
1482 && (cst2
== NULL_TREE
1483 || TREE_CODE (cst2
) == INTEGER_CST
)
1484 && INTEGRAL_TYPE_P (TREE_TYPE (name3
)))
1488 /* Build an expression for the range test. */
1489 tmp
= build1 (NOP_EXPR
, TREE_TYPE (name
), name3
);
1490 if (cst2
!= NULL_TREE
)
1491 tmp
= build2 (PLUS_EXPR
, TREE_TYPE (name
), tmp
, cst2
);
1492 add_assert_info (asserts
, name3
, tmp
, comp_code
, val
);
1495 /* If name2 is used later, create an ASSERT_EXPR for it. */
1496 if (name2
!= NULL_TREE
1497 && TREE_CODE (name2
) == SSA_NAME
1498 && TREE_CODE (cst2
) == INTEGER_CST
1499 && INTEGRAL_TYPE_P (TREE_TYPE (name2
)))
1503 /* Build an expression for the range test. */
1505 if (TREE_TYPE (name
) != TREE_TYPE (name2
))
1506 tmp
= build1 (NOP_EXPR
, TREE_TYPE (name
), tmp
);
1507 if (cst2
!= NULL_TREE
)
1508 tmp
= build2 (PLUS_EXPR
, TREE_TYPE (name
), tmp
, cst2
);
1509 add_assert_info (asserts
, name2
, tmp
, comp_code
, val
);
1513 /* In the case of post-in/decrement tests like if (i++) ... and uses
1514 of the in/decremented value on the edge the extra name we want to
1515 assert for is not on the def chain of the name compared. Instead
1516 it is in the set of use stmts.
1517 Similar cases happen for conversions that were simplified through
1518 fold_{sign_changed,widened}_comparison. */
1519 if ((comp_code
== NE_EXPR
1520 || comp_code
== EQ_EXPR
)
1521 && TREE_CODE (val
) == INTEGER_CST
)
1523 imm_use_iterator ui
;
1525 FOR_EACH_IMM_USE_STMT (use_stmt
, ui
, name
)
1527 if (!is_gimple_assign (use_stmt
))
1530 /* Cut off to use-stmts that are dominating the predecessor. */
1531 if (!dominated_by_p (CDI_DOMINATORS
, e
->src
, gimple_bb (use_stmt
)))
1534 tree name2
= gimple_assign_lhs (use_stmt
);
1535 if (TREE_CODE (name2
) != SSA_NAME
)
1538 enum tree_code code
= gimple_assign_rhs_code (use_stmt
);
1540 if (code
== PLUS_EXPR
1541 || code
== MINUS_EXPR
)
1543 cst
= gimple_assign_rhs2 (use_stmt
);
1544 if (TREE_CODE (cst
) != INTEGER_CST
)
1546 cst
= int_const_binop (code
, val
, cst
);
1548 else if (CONVERT_EXPR_CODE_P (code
))
1550 /* For truncating conversions we cannot record
1552 if (comp_code
== NE_EXPR
1553 && (TYPE_PRECISION (TREE_TYPE (name2
))
1554 < TYPE_PRECISION (TREE_TYPE (name
))))
1556 cst
= fold_convert (TREE_TYPE (name2
), val
);
1561 if (TREE_OVERFLOW_P (cst
))
1562 cst
= drop_tree_overflow (cst
);
1563 add_assert_info (asserts
, name2
, name2
, comp_code
, cst
);
1567 if (TREE_CODE_CLASS (comp_code
) == tcc_comparison
1568 && TREE_CODE (val
) == INTEGER_CST
)
1570 gimple
*def_stmt
= SSA_NAME_DEF_STMT (name
);
1571 tree name2
= NULL_TREE
, names
[2], cst2
= NULL_TREE
;
1572 tree val2
= NULL_TREE
;
1573 unsigned int prec
= TYPE_PRECISION (TREE_TYPE (val
));
1574 wide_int mask
= wi::zero (prec
);
1575 unsigned int nprec
= prec
;
1576 enum tree_code rhs_code
= ERROR_MARK
;
1578 if (is_gimple_assign (def_stmt
))
1579 rhs_code
= gimple_assign_rhs_code (def_stmt
);
1581 /* In the case of NAME != CST1 where NAME = A +- CST2 we can
1582 assert that A != CST1 -+ CST2. */
1583 if ((comp_code
== EQ_EXPR
|| comp_code
== NE_EXPR
)
1584 && (rhs_code
== PLUS_EXPR
|| rhs_code
== MINUS_EXPR
))
1586 tree op0
= gimple_assign_rhs1 (def_stmt
);
1587 tree op1
= gimple_assign_rhs2 (def_stmt
);
1588 if (TREE_CODE (op0
) == SSA_NAME
1589 && TREE_CODE (op1
) == INTEGER_CST
)
1591 enum tree_code reverse_op
= (rhs_code
== PLUS_EXPR
1592 ? MINUS_EXPR
: PLUS_EXPR
);
1593 op1
= int_const_binop (reverse_op
, val
, op1
);
1594 if (TREE_OVERFLOW (op1
))
1595 op1
= drop_tree_overflow (op1
);
1596 add_assert_info (asserts
, op0
, op0
, comp_code
, op1
);
1600 /* Add asserts for NAME cmp CST and NAME being defined
1601 as NAME = (int) NAME2. */
1602 if (!TYPE_UNSIGNED (TREE_TYPE (val
))
1603 && (comp_code
== LE_EXPR
|| comp_code
== LT_EXPR
1604 || comp_code
== GT_EXPR
|| comp_code
== GE_EXPR
)
1605 && gimple_assign_cast_p (def_stmt
))
1607 name2
= gimple_assign_rhs1 (def_stmt
);
1608 if (CONVERT_EXPR_CODE_P (rhs_code
)
1609 && TREE_CODE (name2
) == SSA_NAME
1610 && INTEGRAL_TYPE_P (TREE_TYPE (name2
))
1611 && TYPE_UNSIGNED (TREE_TYPE (name2
))
1612 && prec
== TYPE_PRECISION (TREE_TYPE (name2
))
1613 && (comp_code
== LE_EXPR
|| comp_code
== GT_EXPR
1614 || !tree_int_cst_equal (val
,
1615 TYPE_MIN_VALUE (TREE_TYPE (val
)))))
1618 enum tree_code new_comp_code
= comp_code
;
1620 cst
= fold_convert (TREE_TYPE (name2
),
1621 TYPE_MIN_VALUE (TREE_TYPE (val
)));
1622 /* Build an expression for the range test. */
1623 tmp
= build2 (PLUS_EXPR
, TREE_TYPE (name2
), name2
, cst
);
1624 cst
= fold_build2 (PLUS_EXPR
, TREE_TYPE (name2
), cst
,
1625 fold_convert (TREE_TYPE (name2
), val
));
1626 if (comp_code
== LT_EXPR
|| comp_code
== GE_EXPR
)
1628 new_comp_code
= comp_code
== LT_EXPR
? LE_EXPR
: GT_EXPR
;
1629 cst
= fold_build2 (MINUS_EXPR
, TREE_TYPE (name2
), cst
,
1630 build_int_cst (TREE_TYPE (name2
), 1));
1632 add_assert_info (asserts
, name2
, tmp
, new_comp_code
, cst
);
1636 /* Add asserts for NAME cmp CST and NAME being defined as
1637 NAME = NAME2 >> CST2.
1639 Extract CST2 from the right shift. */
1640 if (rhs_code
== RSHIFT_EXPR
)
1642 name2
= gimple_assign_rhs1 (def_stmt
);
1643 cst2
= gimple_assign_rhs2 (def_stmt
);
1644 if (TREE_CODE (name2
) == SSA_NAME
1645 && tree_fits_uhwi_p (cst2
)
1646 && INTEGRAL_TYPE_P (TREE_TYPE (name2
))
1647 && IN_RANGE (tree_to_uhwi (cst2
), 1, prec
- 1)
1648 && type_has_mode_precision_p (TREE_TYPE (val
)))
1650 mask
= wi::mask (tree_to_uhwi (cst2
), false, prec
);
1651 val2
= fold_binary (LSHIFT_EXPR
, TREE_TYPE (val
), val
, cst2
);
1654 if (val2
!= NULL_TREE
1655 && TREE_CODE (val2
) == INTEGER_CST
1656 && simple_cst_equal (fold_build2 (RSHIFT_EXPR
,
1660 enum tree_code new_comp_code
= comp_code
;
1664 if (comp_code
== EQ_EXPR
|| comp_code
== NE_EXPR
)
1666 if (!TYPE_UNSIGNED (TREE_TYPE (val
)))
1668 tree type
= build_nonstandard_integer_type (prec
, 1);
1669 tmp
= build1 (NOP_EXPR
, type
, name2
);
1670 val2
= fold_convert (type
, val2
);
1672 tmp
= fold_build2 (MINUS_EXPR
, TREE_TYPE (tmp
), tmp
, val2
);
1673 new_val
= wide_int_to_tree (TREE_TYPE (tmp
), mask
);
1674 new_comp_code
= comp_code
== EQ_EXPR
? LE_EXPR
: GT_EXPR
;
1676 else if (comp_code
== LT_EXPR
|| comp_code
== GE_EXPR
)
1679 = wi::min_value (prec
, TYPE_SIGN (TREE_TYPE (val
)));
1681 if (minval
== wi::to_wide (new_val
))
1682 new_val
= NULL_TREE
;
1687 = wi::max_value (prec
, TYPE_SIGN (TREE_TYPE (val
)));
1688 mask
|= wi::to_wide (val2
);
1689 if (wi::eq_p (mask
, maxval
))
1690 new_val
= NULL_TREE
;
1692 new_val
= wide_int_to_tree (TREE_TYPE (val2
), mask
);
1696 add_assert_info (asserts
, name2
, tmp
, new_comp_code
, new_val
);
1699 /* If we have a conversion that doesn't change the value of the source
1700 simply register the same assert for it. */
1701 if (CONVERT_EXPR_CODE_P (rhs_code
))
1704 tree rhs1
= gimple_assign_rhs1 (def_stmt
);
1705 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1
))
1706 && TREE_CODE (rhs1
) == SSA_NAME
1707 /* Make sure the relation preserves the upper/lower boundary of
1708 the range conservatively. */
1709 && (comp_code
== NE_EXPR
1710 || comp_code
== EQ_EXPR
1711 || (TYPE_SIGN (TREE_TYPE (name
))
1712 == TYPE_SIGN (TREE_TYPE (rhs1
)))
1713 || ((comp_code
== LE_EXPR
1714 || comp_code
== LT_EXPR
)
1715 && !TYPE_UNSIGNED (TREE_TYPE (rhs1
)))
1716 || ((comp_code
== GE_EXPR
1717 || comp_code
== GT_EXPR
)
1718 && TYPE_UNSIGNED (TREE_TYPE (rhs1
))))
1719 /* And the conversion does not alter the value we compare
1720 against and all values in rhs1 can be represented in
1721 the converted to type. */
1722 && int_fits_type_p (val
, TREE_TYPE (rhs1
))
1723 && ((TYPE_PRECISION (TREE_TYPE (name
))
1724 > TYPE_PRECISION (TREE_TYPE (rhs1
)))
1725 || ((get_range_query (cfun
)->range_of_expr (vr
, rhs1
)
1726 && vr
.kind () == VR_RANGE
)
1727 && wi::fits_to_tree_p
1728 (widest_int::from (vr
.lower_bound (),
1729 TYPE_SIGN (TREE_TYPE (rhs1
))),
1731 && wi::fits_to_tree_p
1732 (widest_int::from (vr
.upper_bound (),
1733 TYPE_SIGN (TREE_TYPE (rhs1
))),
1734 TREE_TYPE (name
)))))
1735 add_assert_info (asserts
, rhs1
, rhs1
,
1736 comp_code
, fold_convert (TREE_TYPE (rhs1
), val
));
1739 /* Add asserts for NAME cmp CST and NAME being defined as
1740 NAME = NAME2 & CST2.
1742 Extract CST2 from the and.
1745 NAME = (unsigned) NAME2;
1746 casts where NAME's type is unsigned and has smaller precision
1747 than NAME2's type as if it was NAME = NAME2 & MASK. */
1748 names
[0] = NULL_TREE
;
1749 names
[1] = NULL_TREE
;
1751 if (rhs_code
== BIT_AND_EXPR
1752 || (CONVERT_EXPR_CODE_P (rhs_code
)
1753 && INTEGRAL_TYPE_P (TREE_TYPE (val
))
1754 && TYPE_UNSIGNED (TREE_TYPE (val
))
1755 && TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt
)))
1758 name2
= gimple_assign_rhs1 (def_stmt
);
1759 if (rhs_code
== BIT_AND_EXPR
)
1760 cst2
= gimple_assign_rhs2 (def_stmt
);
1763 cst2
= TYPE_MAX_VALUE (TREE_TYPE (val
));
1764 nprec
= TYPE_PRECISION (TREE_TYPE (name2
));
1766 if (TREE_CODE (name2
) == SSA_NAME
1767 && INTEGRAL_TYPE_P (TREE_TYPE (name2
))
1768 && TREE_CODE (cst2
) == INTEGER_CST
1769 && !integer_zerop (cst2
)
1771 || TYPE_UNSIGNED (TREE_TYPE (val
))))
1773 gimple
*def_stmt2
= SSA_NAME_DEF_STMT (name2
);
1774 if (gimple_assign_cast_p (def_stmt2
))
1776 names
[1] = gimple_assign_rhs1 (def_stmt2
);
1777 if (!CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt2
))
1778 || TREE_CODE (names
[1]) != SSA_NAME
1779 || !INTEGRAL_TYPE_P (TREE_TYPE (names
[1]))
1780 || (TYPE_PRECISION (TREE_TYPE (name2
))
1781 != TYPE_PRECISION (TREE_TYPE (names
[1]))))
1782 names
[1] = NULL_TREE
;
1787 if (names
[0] || names
[1])
1789 wide_int minv
, maxv
, valv
, cst2v
;
1790 wide_int tem
, sgnbit
;
1791 bool valid_p
= false, valn
, cst2n
;
1792 enum tree_code ccode
= comp_code
;
1794 valv
= wide_int::from (wi::to_wide (val
), nprec
, UNSIGNED
);
1795 cst2v
= wide_int::from (wi::to_wide (cst2
), nprec
, UNSIGNED
);
1796 valn
= wi::neg_p (valv
, TYPE_SIGN (TREE_TYPE (val
)));
1797 cst2n
= wi::neg_p (cst2v
, TYPE_SIGN (TREE_TYPE (val
)));
1798 /* If CST2 doesn't have most significant bit set,
1799 but VAL is negative, we have comparison like
1800 if ((x & 0x123) > -4) (always true). Just give up. */
1804 sgnbit
= wi::set_bit_in_zero (nprec
- 1, nprec
);
1806 sgnbit
= wi::zero (nprec
);
1807 minv
= valv
& cst2v
;
1811 /* Minimum unsigned value for equality is VAL & CST2
1812 (should be equal to VAL, otherwise we probably should
1813 have folded the comparison into false) and
1814 maximum unsigned value is VAL | ~CST2. */
1815 maxv
= valv
| ~cst2v
;
1820 tem
= valv
| ~cst2v
;
1821 /* If VAL is 0, handle (X & CST2) != 0 as (X & CST2) > 0U. */
1825 sgnbit
= wi::zero (nprec
);
1828 /* If (VAL | ~CST2) is all ones, handle it as
1829 (X & CST2) < VAL. */
1834 sgnbit
= wi::zero (nprec
);
1837 if (!cst2n
&& wi::neg_p (cst2v
))
1838 sgnbit
= wi::set_bit_in_zero (nprec
- 1, nprec
);
1847 if (tem
== wi::mask (nprec
- 1, false, nprec
))
1853 sgnbit
= wi::zero (nprec
);
1858 /* Minimum unsigned value for >= if (VAL & CST2) == VAL
1859 is VAL and maximum unsigned value is ~0. For signed
1860 comparison, if CST2 doesn't have most significant bit
1861 set, handle it similarly. If CST2 has MSB set,
1862 the minimum is the same, and maximum is ~0U/2. */
1865 /* If (VAL & CST2) != VAL, X & CST2 can't be equal to
1867 minv
= masked_increment (valv
, cst2v
, sgnbit
, nprec
);
1871 maxv
= wi::mask (nprec
- (cst2n
? 1 : 0), false, nprec
);
1877 /* Find out smallest MINV where MINV > VAL
1878 && (MINV & CST2) == MINV, if any. If VAL is signed and
1879 CST2 has MSB set, compute it biased by 1 << (nprec - 1). */
1880 minv
= masked_increment (valv
, cst2v
, sgnbit
, nprec
);
1883 maxv
= wi::mask (nprec
- (cst2n
? 1 : 0), false, nprec
);
1888 /* Minimum unsigned value for <= is 0 and maximum
1889 unsigned value is VAL | ~CST2 if (VAL & CST2) == VAL.
1890 Otherwise, find smallest VAL2 where VAL2 > VAL
1891 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
1893 For signed comparison, if CST2 doesn't have most
1894 significant bit set, handle it similarly. If CST2 has
1895 MSB set, the maximum is the same and minimum is INT_MIN. */
1900 maxv
= masked_increment (valv
, cst2v
, sgnbit
, nprec
);
1912 /* Minimum unsigned value for < is 0 and maximum
1913 unsigned value is (VAL-1) | ~CST2 if (VAL & CST2) == VAL.
1914 Otherwise, find smallest VAL2 where VAL2 > VAL
1915 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
1917 For signed comparison, if CST2 doesn't have most
1918 significant bit set, handle it similarly. If CST2 has
1919 MSB set, the maximum is the same and minimum is INT_MIN. */
1928 maxv
= masked_increment (valv
, cst2v
, sgnbit
, nprec
);
1942 && (maxv
- minv
) != -1)
1944 tree tmp
, new_val
, type
;
1947 for (i
= 0; i
< 2; i
++)
1950 wide_int maxv2
= maxv
;
1952 type
= TREE_TYPE (names
[i
]);
1953 if (!TYPE_UNSIGNED (type
))
1955 type
= build_nonstandard_integer_type (nprec
, 1);
1956 tmp
= build1 (NOP_EXPR
, type
, names
[i
]);
1960 tmp
= build2 (PLUS_EXPR
, type
, tmp
,
1961 wide_int_to_tree (type
, -minv
));
1962 maxv2
= maxv
- minv
;
1964 new_val
= wide_int_to_tree (type
, maxv2
);
1965 add_assert_info (asserts
, names
[i
], tmp
, LE_EXPR
, new_val
);
1972 /* OP is an operand of a truth value expression which is known to have
1973 a particular value. Register any asserts for OP and for any
1974 operands in OP's defining statement.
1976 If CODE is EQ_EXPR, then we want to register OP is zero (false),
1977 if CODE is NE_EXPR, then we want to register OP is nonzero (true). */
1980 register_edge_assert_for_1 (tree op
, enum tree_code code
,
1981 edge e
, vec
<assert_info
> &asserts
)
1985 enum tree_code rhs_code
;
1987 /* We only care about SSA_NAMEs. */
1988 if (TREE_CODE (op
) != SSA_NAME
)
1991 /* We know that OP will have a zero or nonzero value. */
1992 val
= build_int_cst (TREE_TYPE (op
), 0);
1993 add_assert_info (asserts
, op
, op
, code
, val
);
1995 /* Now look at how OP is set. If it's set from a comparison,
1996 a truth operation or some bit operations, then we may be able
1997 to register information about the operands of that assignment. */
1998 op_def
= SSA_NAME_DEF_STMT (op
);
1999 if (gimple_code (op_def
) != GIMPLE_ASSIGN
)
2002 rhs_code
= gimple_assign_rhs_code (op_def
);
2004 if (TREE_CODE_CLASS (rhs_code
) == tcc_comparison
)
2006 bool invert
= (code
== EQ_EXPR
? true : false);
2007 tree op0
= gimple_assign_rhs1 (op_def
);
2008 tree op1
= gimple_assign_rhs2 (op_def
);
2010 if (TREE_CODE (op0
) == SSA_NAME
)
2011 register_edge_assert_for_2 (op0
, e
, rhs_code
, op0
, op1
, invert
, asserts
);
2012 if (TREE_CODE (op1
) == SSA_NAME
)
2013 register_edge_assert_for_2 (op1
, e
, rhs_code
, op0
, op1
, invert
, asserts
);
2015 else if ((code
== NE_EXPR
2016 && gimple_assign_rhs_code (op_def
) == BIT_AND_EXPR
)
2018 && gimple_assign_rhs_code (op_def
) == BIT_IOR_EXPR
))
2020 /* Recurse on each operand. */
2021 tree op0
= gimple_assign_rhs1 (op_def
);
2022 tree op1
= gimple_assign_rhs2 (op_def
);
2023 if (TREE_CODE (op0
) == SSA_NAME
2024 && has_single_use (op0
))
2025 register_edge_assert_for_1 (op0
, code
, e
, asserts
);
2026 if (TREE_CODE (op1
) == SSA_NAME
2027 && has_single_use (op1
))
2028 register_edge_assert_for_1 (op1
, code
, e
, asserts
);
2030 else if (gimple_assign_rhs_code (op_def
) == BIT_NOT_EXPR
2031 && TYPE_PRECISION (TREE_TYPE (gimple_assign_lhs (op_def
))) == 1)
2033 /* Recurse, flipping CODE. */
2034 code
= invert_tree_comparison (code
, false);
2035 register_edge_assert_for_1 (gimple_assign_rhs1 (op_def
), code
, e
, asserts
);
2037 else if (gimple_assign_rhs_code (op_def
) == SSA_NAME
)
2039 /* Recurse through the copy. */
2040 register_edge_assert_for_1 (gimple_assign_rhs1 (op_def
), code
, e
, asserts
);
2042 else if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (op_def
)))
2044 /* Recurse through the type conversion, unless it is a narrowing
2045 conversion or conversion from non-integral type. */
2046 tree rhs
= gimple_assign_rhs1 (op_def
);
2047 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs
))
2048 && (TYPE_PRECISION (TREE_TYPE (rhs
))
2049 <= TYPE_PRECISION (TREE_TYPE (op
))))
2050 register_edge_assert_for_1 (rhs
, code
, e
, asserts
);
2054 /* Check if comparison
2055 NAME COND_OP INTEGER_CST
2057 (X & 11...100..0) COND_OP XX...X00...0
2058 Such comparison can yield assertions like
2061 in case of COND_OP being EQ_EXPR or
2064 in case of NE_EXPR. */
2067 is_masked_range_test (tree name
, tree valt
, enum tree_code cond_code
,
2068 tree
*new_name
, tree
*low
, enum tree_code
*low_code
,
2069 tree
*high
, enum tree_code
*high_code
)
2071 gimple
*def_stmt
= SSA_NAME_DEF_STMT (name
);
2073 if (!is_gimple_assign (def_stmt
)
2074 || gimple_assign_rhs_code (def_stmt
) != BIT_AND_EXPR
)
2077 tree t
= gimple_assign_rhs1 (def_stmt
);
2078 tree maskt
= gimple_assign_rhs2 (def_stmt
);
2079 if (TREE_CODE (t
) != SSA_NAME
|| TREE_CODE (maskt
) != INTEGER_CST
)
2082 wi::tree_to_wide_ref mask
= wi::to_wide (maskt
);
2083 wide_int inv_mask
= ~mask
;
2084 /* Must have been removed by now so don't bother optimizing. */
2085 if (mask
== 0 || inv_mask
== 0)
2088 /* Assume VALT is INTEGER_CST. */
2089 wi::tree_to_wide_ref val
= wi::to_wide (valt
);
2091 if ((inv_mask
& (inv_mask
+ 1)) != 0
2092 || (val
& mask
) != val
)
2095 bool is_range
= cond_code
== EQ_EXPR
;
2097 tree type
= TREE_TYPE (t
);
2098 wide_int min
= wi::min_value (type
),
2099 max
= wi::max_value (type
);
2103 *low_code
= val
== min
? ERROR_MARK
: GE_EXPR
;
2104 *high_code
= val
== max
? ERROR_MARK
: LE_EXPR
;
2108 /* We can still generate assertion if one of alternatives
2109 is known to always be false. */
2112 *low_code
= (enum tree_code
) 0;
2113 *high_code
= GT_EXPR
;
2115 else if ((val
| inv_mask
) == max
)
2117 *low_code
= LT_EXPR
;
2118 *high_code
= (enum tree_code
) 0;
2125 *low
= wide_int_to_tree (type
, val
);
2126 *high
= wide_int_to_tree (type
, val
| inv_mask
);
2131 /* Try to register an edge assertion for SSA name NAME on edge E for
2132 the condition COND contributing to the conditional jump pointed to by
2136 register_edge_assert_for (tree name
, edge e
,
2137 enum tree_code cond_code
, tree cond_op0
,
2138 tree cond_op1
, vec
<assert_info
> &asserts
)
2141 enum tree_code comp_code
;
2142 bool is_else_edge
= (e
->flags
& EDGE_FALSE_VALUE
) != 0;
2144 /* Do not attempt to infer anything in names that flow through
2146 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name
))
2149 if (!extract_code_and_val_from_cond_with_ops (name
, cond_code
,
2155 /* Register ASSERT_EXPRs for name. */
2156 register_edge_assert_for_2 (name
, e
, cond_code
, cond_op0
,
2157 cond_op1
, is_else_edge
, asserts
);
2160 /* If COND is effectively an equality test of an SSA_NAME against
2161 the value zero or one, then we may be able to assert values
2162 for SSA_NAMEs which flow into COND. */
2164 /* In the case of NAME == 1 or NAME != 0, for BIT_AND_EXPR defining
2165 statement of NAME we can assert both operands of the BIT_AND_EXPR
2166 have nonzero value. */
2167 if ((comp_code
== EQ_EXPR
&& integer_onep (val
))
2168 || (comp_code
== NE_EXPR
&& integer_zerop (val
)))
2170 gimple
*def_stmt
= SSA_NAME_DEF_STMT (name
);
2172 if (is_gimple_assign (def_stmt
)
2173 && gimple_assign_rhs_code (def_stmt
) == BIT_AND_EXPR
)
2175 tree op0
= gimple_assign_rhs1 (def_stmt
);
2176 tree op1
= gimple_assign_rhs2 (def_stmt
);
2177 register_edge_assert_for_1 (op0
, NE_EXPR
, e
, asserts
);
2178 register_edge_assert_for_1 (op1
, NE_EXPR
, e
, asserts
);
2180 else if (is_gimple_assign (def_stmt
)
2181 && (TREE_CODE_CLASS (gimple_assign_rhs_code (def_stmt
))
2183 register_edge_assert_for_1 (name
, NE_EXPR
, e
, asserts
);
2186 /* In the case of NAME == 0 or NAME != 1, for BIT_IOR_EXPR defining
2187 statement of NAME we can assert both operands of the BIT_IOR_EXPR
2189 if ((comp_code
== EQ_EXPR
&& integer_zerop (val
))
2190 || (comp_code
== NE_EXPR
2191 && integer_onep (val
)
2192 && TYPE_PRECISION (TREE_TYPE (name
)) == 1))
2194 gimple
*def_stmt
= SSA_NAME_DEF_STMT (name
);
2196 /* For BIT_IOR_EXPR only if NAME == 0 both operands have
2197 necessarily zero value, or if type-precision is one. */
2198 if (is_gimple_assign (def_stmt
)
2199 && gimple_assign_rhs_code (def_stmt
) == BIT_IOR_EXPR
)
2201 tree op0
= gimple_assign_rhs1 (def_stmt
);
2202 tree op1
= gimple_assign_rhs2 (def_stmt
);
2203 register_edge_assert_for_1 (op0
, EQ_EXPR
, e
, asserts
);
2204 register_edge_assert_for_1 (op1
, EQ_EXPR
, e
, asserts
);
2206 else if (is_gimple_assign (def_stmt
)
2207 && (TREE_CODE_CLASS (gimple_assign_rhs_code (def_stmt
))
2209 register_edge_assert_for_1 (name
, EQ_EXPR
, e
, asserts
);
2212 /* Sometimes we can infer ranges from (NAME & MASK) == VALUE. */
2213 if ((comp_code
== EQ_EXPR
|| comp_code
== NE_EXPR
)
2214 && TREE_CODE (val
) == INTEGER_CST
)
2216 enum tree_code low_code
, high_code
;
2218 if (is_masked_range_test (name
, val
, comp_code
, &name
, &low
,
2219 &low_code
, &high
, &high_code
))
2221 if (low_code
!= ERROR_MARK
)
2222 register_edge_assert_for_2 (name
, e
, low_code
, name
,
2223 low
, /*invert*/false, asserts
);
2224 if (high_code
!= ERROR_MARK
)
2225 register_edge_assert_for_2 (name
, e
, high_code
, name
,
2226 high
, /*invert*/false, asserts
);
2238 __builtin_unreachable ();
2240 x_5 = ASSERT_EXPR <x_3, ...>;
2241 If x_3 has no other immediate uses (checked by caller),
2242 var is the x_3 var from ASSERT_EXPR, we can clear low 5 bits
2243 from the non-zero bitmask. */
2246 maybe_set_nonzero_bits (edge e
, tree var
)
2248 basic_block cond_bb
= e
->src
;
2249 gimple
*stmt
= last_stmt (cond_bb
);
2253 || gimple_code (stmt
) != GIMPLE_COND
2254 || gimple_cond_code (stmt
) != ((e
->flags
& EDGE_TRUE_VALUE
)
2255 ? EQ_EXPR
: NE_EXPR
)
2256 || TREE_CODE (gimple_cond_lhs (stmt
)) != SSA_NAME
2257 || !integer_zerop (gimple_cond_rhs (stmt
)))
2260 stmt
= SSA_NAME_DEF_STMT (gimple_cond_lhs (stmt
));
2261 if (!is_gimple_assign (stmt
)
2262 || gimple_assign_rhs_code (stmt
) != BIT_AND_EXPR
2263 || TREE_CODE (gimple_assign_rhs2 (stmt
)) != INTEGER_CST
)
2265 if (gimple_assign_rhs1 (stmt
) != var
)
2269 if (TREE_CODE (gimple_assign_rhs1 (stmt
)) != SSA_NAME
)
2271 stmt2
= SSA_NAME_DEF_STMT (gimple_assign_rhs1 (stmt
));
2272 if (!gimple_assign_cast_p (stmt2
)
2273 || gimple_assign_rhs1 (stmt2
) != var
2274 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (stmt2
))
2275 || (TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (stmt
)))
2276 != TYPE_PRECISION (TREE_TYPE (var
))))
2279 cst
= gimple_assign_rhs2 (stmt
);
2280 set_nonzero_bits (var
, wi::bit_and_not (get_nonzero_bits (var
),
2281 wi::to_wide (cst
)));
2284 /* Return true if STMT is interesting for VRP. */
2287 stmt_interesting_for_vrp (gimple
*stmt
)
2289 if (gimple_code (stmt
) == GIMPLE_PHI
)
2291 tree res
= gimple_phi_result (stmt
);
2292 return (!virtual_operand_p (res
)
2293 && (INTEGRAL_TYPE_P (TREE_TYPE (res
))
2294 || POINTER_TYPE_P (TREE_TYPE (res
))));
2296 else if (is_gimple_assign (stmt
) || is_gimple_call (stmt
))
2298 tree lhs
= gimple_get_lhs (stmt
);
2300 /* In general, assignments with virtual operands are not useful
2301 for deriving ranges, with the obvious exception of calls to
2302 builtin functions. */
2303 if (lhs
&& TREE_CODE (lhs
) == SSA_NAME
2304 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs
))
2305 || POINTER_TYPE_P (TREE_TYPE (lhs
)))
2306 && (is_gimple_call (stmt
)
2307 || !gimple_vuse (stmt
)))
2309 else if (is_gimple_call (stmt
) && gimple_call_internal_p (stmt
))
2310 switch (gimple_call_internal_fn (stmt
))
2312 case IFN_ADD_OVERFLOW
:
2313 case IFN_SUB_OVERFLOW
:
2314 case IFN_MUL_OVERFLOW
:
2315 case IFN_ATOMIC_COMPARE_EXCHANGE
:
2316 /* These internal calls return _Complex integer type,
2317 but are interesting to VRP nevertheless. */
2318 if (lhs
&& TREE_CODE (lhs
) == SSA_NAME
)
2325 else if (gimple_code (stmt
) == GIMPLE_COND
2326 || gimple_code (stmt
) == GIMPLE_SWITCH
)
2332 /* Searches the case label vector VEC for the index *IDX of the CASE_LABEL
2333 that includes the value VAL. The search is restricted to the range
2334 [START_IDX, n - 1] where n is the size of VEC.
2336 If there is a CASE_LABEL for VAL, its index is placed in IDX and true is
2339 If there is no CASE_LABEL for VAL and there is one that is larger than VAL,
2340 it is placed in IDX and false is returned.
2342 If VAL is larger than any CASE_LABEL, n is placed on IDX and false is
2346 find_case_label_index (gswitch
*stmt
, size_t start_idx
, tree val
, size_t *idx
)
2348 size_t n
= gimple_switch_num_labels (stmt
);
2351 /* Find case label for minimum of the value range or the next one.
2352 At each iteration we are searching in [low, high - 1]. */
2354 for (low
= start_idx
, high
= n
; high
!= low
; )
2358 /* Note that i != high, so we never ask for n. */
2359 size_t i
= (high
+ low
) / 2;
2360 t
= gimple_switch_label (stmt
, i
);
2362 /* Cache the result of comparing CASE_LOW and val. */
2363 cmp
= tree_int_cst_compare (CASE_LOW (t
), val
);
2367 /* Ranges cannot be empty. */
2376 if (CASE_HIGH (t
) != NULL
2377 && tree_int_cst_compare (CASE_HIGH (t
), val
) >= 0)
2389 /* Searches the case label vector VEC for the range of CASE_LABELs that is used
2390 for values between MIN and MAX. The first index is placed in MIN_IDX. The
2391 last index is placed in MAX_IDX. If the range of CASE_LABELs is empty
2392 then MAX_IDX < MIN_IDX.
2393 Returns true if the default label is not needed. */
2396 find_case_label_range (gswitch
*stmt
, tree min
, tree max
, size_t *min_idx
,
2400 bool min_take_default
= !find_case_label_index (stmt
, 1, min
, &i
);
2401 bool max_take_default
= !find_case_label_index (stmt
, i
, max
, &j
);
2405 && max_take_default
)
2407 /* Only the default case label reached.
2408 Return an empty range. */
2415 bool take_default
= min_take_default
|| max_take_default
;
2419 if (max_take_default
)
2422 /* If the case label range is continuous, we do not need
2423 the default case label. Verify that. */
2424 high
= CASE_LOW (gimple_switch_label (stmt
, i
));
2425 if (CASE_HIGH (gimple_switch_label (stmt
, i
)))
2426 high
= CASE_HIGH (gimple_switch_label (stmt
, i
));
2427 for (k
= i
+ 1; k
<= j
; ++k
)
2429 low
= CASE_LOW (gimple_switch_label (stmt
, k
));
2430 if (!integer_onep (int_const_binop (MINUS_EXPR
, low
, high
)))
2432 take_default
= true;
2436 if (CASE_HIGH (gimple_switch_label (stmt
, k
)))
2437 high
= CASE_HIGH (gimple_switch_label (stmt
, k
));
2442 return !take_default
;
2446 /* Given a SWITCH_STMT, return the case label that encompasses the
2447 known possible values for the switch operand. RANGE_OF_OP is a
2448 range for the known values of the switch operand. */
2451 find_case_label_range (gswitch
*switch_stmt
, const irange
*range_of_op
)
2453 if (range_of_op
->undefined_p ()
2454 || range_of_op
->varying_p ()
2455 || range_of_op
->symbolic_p ())
2459 tree op
= gimple_switch_index (switch_stmt
);
2460 tree type
= TREE_TYPE (op
);
2461 tree tmin
= wide_int_to_tree (type
, range_of_op
->lower_bound ());
2462 tree tmax
= wide_int_to_tree (type
, range_of_op
->upper_bound ());
2463 find_case_label_range (switch_stmt
, tmin
, tmax
, &i
, &j
);
2466 /* Look for exactly one label that encompasses the range of
2468 tree label
= gimple_switch_label (switch_stmt
, i
);
2470 = CASE_HIGH (label
) ? CASE_HIGH (label
) : CASE_LOW (label
);
2471 int_range_max
label_range (CASE_LOW (label
), case_high
);
2472 if (!types_compatible_p (label_range
.type (), range_of_op
->type ()))
2473 range_cast (label_range
, range_of_op
->type ());
2474 label_range
.intersect (range_of_op
);
2475 if (label_range
== *range_of_op
)
2480 /* If there are no labels at all, take the default. */
2481 return gimple_switch_label (switch_stmt
, 0);
2485 /* Otherwise, there are various labels that can encompass
2486 the range of operand. In which case, see if the range of
2487 the operand is entirely *outside* the bounds of all the
2488 (non-default) case labels. If so, take the default. */
2489 unsigned n
= gimple_switch_num_labels (switch_stmt
);
2490 tree min_label
= gimple_switch_label (switch_stmt
, 1);
2491 tree max_label
= gimple_switch_label (switch_stmt
, n
- 1);
2492 tree case_high
= CASE_HIGH (max_label
);
2494 case_high
= CASE_LOW (max_label
);
2495 int_range_max
label_range (CASE_LOW (min_label
), case_high
);
2496 if (!types_compatible_p (label_range
.type (), range_of_op
->type ()))
2497 range_cast (label_range
, range_of_op
->type ());
2498 label_range
.intersect (range_of_op
);
2499 if (label_range
.undefined_p ())
2500 return gimple_switch_label (switch_stmt
, 0);
2511 /* Location information for ASSERT_EXPRs. Each instance of this
2512 structure describes an ASSERT_EXPR for an SSA name. Since a single
2513 SSA name may have more than one assertion associated with it, these
2514 locations are kept in a linked list attached to the corresponding
2518 /* Basic block where the assertion would be inserted. */
2521 /* Some assertions need to be inserted on an edge (e.g., assertions
2522 generated by COND_EXPRs). In those cases, BB will be NULL. */
2525 /* Pointer to the statement that generated this assertion. */
2526 gimple_stmt_iterator si
;
2528 /* Predicate code for the ASSERT_EXPR. Must be COMPARISON_CLASS_P. */
2529 enum tree_code comp_code
;
2531 /* Value being compared against. */
2534 /* Expression to compare. */
2537 /* Next node in the linked list. */
2541 /* Class to traverse the flowgraph looking for conditional jumps to
2542 insert ASSERT_EXPR range expressions. These range expressions are
2543 meant to provide information to optimizations that need to reason
2544 in terms of value ranges. They will not be expanded into RTL. */
2549 vrp_asserts (struct function
*fn
) : fun (fn
) { }
2551 void insert_range_assertions ();
2553 /* Convert range assertion expressions into the implied copies and
2554 copy propagate away the copies. */
2555 void remove_range_assertions ();
2557 /* Dump all the registered assertions for all the names to FILE. */
2560 /* Dump all the registered assertions for NAME to FILE. */
2561 void dump (FILE *file
, tree name
);
2563 /* Dump all the registered assertions for NAME to stderr. */
2564 void debug (tree name
)
2566 dump (stderr
, name
);
2569 /* Dump all the registered assertions for all the names to stderr. */
2576 /* Set of SSA names found live during the RPO traversal of the function
2577 for still active basic-blocks. */
2580 /* Function to work on. */
2581 struct function
*fun
;
2583 /* If bit I is present, it means that SSA name N_i has a list of
2584 assertions that should be inserted in the IL. */
2585 bitmap need_assert_for
;
2587 /* Array of locations lists where to insert assertions. ASSERTS_FOR[I]
2588 holds a list of ASSERT_LOCUS_T nodes that describe where
2589 ASSERT_EXPRs for SSA name N_I should be inserted. */
2590 assert_locus
**asserts_for
;
2592 /* Finish found ASSERTS for E and register them at GSI. */
2593 void finish_register_edge_assert_for (edge e
, gimple_stmt_iterator gsi
,
2594 vec
<assert_info
> &asserts
);
2596 /* Determine whether the outgoing edges of BB should receive an
2597 ASSERT_EXPR for each of the operands of BB's LAST statement. The
2598 last statement of BB must be a SWITCH_EXPR.
2600 If any of the sub-graphs rooted at BB have an interesting use of
2601 the predicate operands, an assert location node is added to the
2602 list of assertions for the corresponding operands. */
2603 void find_switch_asserts (basic_block bb
, gswitch
*last
);
2605 /* Do an RPO walk over the function computing SSA name liveness
2606 on-the-fly and deciding on assert expressions to insert. */
2607 void find_assert_locations ();
2609 /* Traverse all the statements in block BB looking for statements that
2610 may generate useful assertions for the SSA names in their operand.
2611 See method implementation comentary for more information. */
2612 void find_assert_locations_in_bb (basic_block bb
);
2614 /* Determine whether the outgoing edges of BB should receive an
2615 ASSERT_EXPR for each of the operands of BB's LAST statement.
2616 The last statement of BB must be a COND_EXPR.
2618 If any of the sub-graphs rooted at BB have an interesting use of
2619 the predicate operands, an assert location node is added to the
2620 list of assertions for the corresponding operands. */
2621 void find_conditional_asserts (basic_block bb
, gcond
*last
);
2623 /* Process all the insertions registered for every name N_i registered
2624 in NEED_ASSERT_FOR. The list of assertions to be inserted are
2625 found in ASSERTS_FOR[i]. */
2626 void process_assert_insertions ();
2628 /* If NAME doesn't have an ASSERT_EXPR registered for asserting
2629 'EXPR COMP_CODE VAL' at a location that dominates block BB or
2630 E->DEST, then register this location as a possible insertion point
2631 for ASSERT_EXPR <NAME, EXPR COMP_CODE VAL>.
2633 BB, E and SI provide the exact insertion point for the new
2634 ASSERT_EXPR. If BB is NULL, then the ASSERT_EXPR is to be inserted
2635 on edge E. Otherwise, if E is NULL, the ASSERT_EXPR is inserted on
2636 BB. If SI points to a COND_EXPR or a SWITCH_EXPR statement, then E
2637 must not be NULL. */
2638 void register_new_assert_for (tree name
, tree expr
,
2639 enum tree_code comp_code
,
2640 tree val
, basic_block bb
,
2641 edge e
, gimple_stmt_iterator si
);
2643 /* Given a COND_EXPR COND of the form 'V OP W', and an SSA name V,
2644 create a new SSA name N and return the assertion assignment
2645 'N = ASSERT_EXPR <V, V OP W>'. */
2646 gimple
*build_assert_expr_for (tree cond
, tree v
);
2648 /* Create an ASSERT_EXPR for NAME and insert it in the location
2649 indicated by LOC. Return true if we made any edge insertions. */
2650 bool process_assert_insertions_for (tree name
, assert_locus
*loc
);
2652 /* Qsort callback for sorting assert locations. */
2653 template <bool stable
> static int compare_assert_loc (const void *,
2656 /* Return false if EXPR is a predicate expression involving floating
2658 bool fp_predicate (gimple
*stmt
)
2660 GIMPLE_CHECK (stmt
, GIMPLE_COND
);
2661 return FLOAT_TYPE_P (TREE_TYPE (gimple_cond_lhs (stmt
)));
2664 bool all_imm_uses_in_stmt_or_feed_cond (tree var
, gimple
*stmt
,
2665 basic_block cond_bb
);
2667 static int compare_case_labels (const void *, const void *);
2670 /* Given a COND_EXPR COND of the form 'V OP W', and an SSA name V,
2671 create a new SSA name N and return the assertion assignment
2672 'N = ASSERT_EXPR <V, V OP W>'. */
2675 vrp_asserts::build_assert_expr_for (tree cond
, tree v
)
2680 gcc_assert (TREE_CODE (v
) == SSA_NAME
2681 && COMPARISON_CLASS_P (cond
));
2683 a
= build2 (ASSERT_EXPR
, TREE_TYPE (v
), v
, cond
);
2684 assertion
= gimple_build_assign (NULL_TREE
, a
);
2686 /* The new ASSERT_EXPR, creates a new SSA name that replaces the
2687 operand of the ASSERT_EXPR. Create it so the new name and the old one
2688 are registered in the replacement table so that we can fix the SSA web
2689 after adding all the ASSERT_EXPRs. */
2690 tree new_def
= create_new_def_for (v
, assertion
, NULL
);
2691 /* Make sure we preserve abnormalness throughout an ASSERT_EXPR chain
2692 given we have to be able to fully propagate those out to re-create
2693 valid SSA when removing the asserts. */
2694 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (v
))
2695 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (new_def
) = 1;
2700 /* Dump all the registered assertions for NAME to FILE. */
2703 vrp_asserts::dump (FILE *file
, tree name
)
2707 fprintf (file
, "Assertions to be inserted for ");
2708 print_generic_expr (file
, name
);
2709 fprintf (file
, "\n");
2711 loc
= asserts_for
[SSA_NAME_VERSION (name
)];
2714 fprintf (file
, "\t");
2715 print_gimple_stmt (file
, gsi_stmt (loc
->si
), 0);
2716 fprintf (file
, "\n\tBB #%d", loc
->bb
->index
);
2719 fprintf (file
, "\n\tEDGE %d->%d", loc
->e
->src
->index
,
2720 loc
->e
->dest
->index
);
2721 dump_edge_info (file
, loc
->e
, dump_flags
, 0);
2723 fprintf (file
, "\n\tPREDICATE: ");
2724 print_generic_expr (file
, loc
->expr
);
2725 fprintf (file
, " %s ", get_tree_code_name (loc
->comp_code
));
2726 print_generic_expr (file
, loc
->val
);
2727 fprintf (file
, "\n\n");
2731 fprintf (file
, "\n");
2734 /* Dump all the registered assertions for all the names to FILE. */
2737 vrp_asserts::dump (FILE *file
)
2742 fprintf (file
, "\nASSERT_EXPRs to be inserted\n\n");
2743 EXECUTE_IF_SET_IN_BITMAP (need_assert_for
, 0, i
, bi
)
2744 dump (file
, ssa_name (i
));
2745 fprintf (file
, "\n");
2748 /* If NAME doesn't have an ASSERT_EXPR registered for asserting
2749 'EXPR COMP_CODE VAL' at a location that dominates block BB or
2750 E->DEST, then register this location as a possible insertion point
2751 for ASSERT_EXPR <NAME, EXPR COMP_CODE VAL>.
2753 BB, E and SI provide the exact insertion point for the new
2754 ASSERT_EXPR. If BB is NULL, then the ASSERT_EXPR is to be inserted
2755 on edge E. Otherwise, if E is NULL, the ASSERT_EXPR is inserted on
2756 BB. If SI points to a COND_EXPR or a SWITCH_EXPR statement, then E
2757 must not be NULL. */
2760 vrp_asserts::register_new_assert_for (tree name
, tree expr
,
2761 enum tree_code comp_code
,
2765 gimple_stmt_iterator si
)
2767 assert_locus
*n
, *loc
, *last_loc
;
2768 basic_block dest_bb
;
2770 gcc_checking_assert (bb
== NULL
|| e
== NULL
);
2773 gcc_checking_assert (gimple_code (gsi_stmt (si
)) != GIMPLE_COND
2774 && gimple_code (gsi_stmt (si
)) != GIMPLE_SWITCH
);
2776 /* Never build an assert comparing against an integer constant with
2777 TREE_OVERFLOW set. This confuses our undefined overflow warning
2779 if (TREE_OVERFLOW_P (val
))
2780 val
= drop_tree_overflow (val
);
2782 /* The new assertion A will be inserted at BB or E. We need to
2783 determine if the new location is dominated by a previously
2784 registered location for A. If we are doing an edge insertion,
2785 assume that A will be inserted at E->DEST. Note that this is not
2788 If E is a critical edge, it will be split. But even if E is
2789 split, the new block will dominate the same set of blocks that
2792 The reverse, however, is not true, blocks dominated by E->DEST
2793 will not be dominated by the new block created to split E. So,
2794 if the insertion location is on a critical edge, we will not use
2795 the new location to move another assertion previously registered
2796 at a block dominated by E->DEST. */
2797 dest_bb
= (bb
) ? bb
: e
->dest
;
2799 /* If NAME already has an ASSERT_EXPR registered for COMP_CODE and
2800 VAL at a block dominating DEST_BB, then we don't need to insert a new
2801 one. Similarly, if the same assertion already exists at a block
2802 dominated by DEST_BB and the new location is not on a critical
2803 edge, then update the existing location for the assertion (i.e.,
2804 move the assertion up in the dominance tree).
2806 Note, this is implemented as a simple linked list because there
2807 should not be more than a handful of assertions registered per
2808 name. If this becomes a performance problem, a table hashed by
2809 COMP_CODE and VAL could be implemented. */
2810 loc
= asserts_for
[SSA_NAME_VERSION (name
)];
2814 if (loc
->comp_code
== comp_code
2816 || operand_equal_p (loc
->val
, val
, 0))
2817 && (loc
->expr
== expr
2818 || operand_equal_p (loc
->expr
, expr
, 0)))
2820 /* If E is not a critical edge and DEST_BB
2821 dominates the existing location for the assertion, move
2822 the assertion up in the dominance tree by updating its
2823 location information. */
2824 if ((e
== NULL
|| !EDGE_CRITICAL_P (e
))
2825 && dominated_by_p (CDI_DOMINATORS
, loc
->bb
, dest_bb
))
2834 /* Update the last node of the list and move to the next one. */
2839 /* If we didn't find an assertion already registered for
2840 NAME COMP_CODE VAL, add a new one at the end of the list of
2841 assertions associated with NAME. */
2842 n
= XNEW (struct assert_locus
);
2846 n
->comp_code
= comp_code
;
2854 asserts_for
[SSA_NAME_VERSION (name
)] = n
;
2856 bitmap_set_bit (need_assert_for
, SSA_NAME_VERSION (name
));
2859 /* Finish found ASSERTS for E and register them at GSI. */
2862 vrp_asserts::finish_register_edge_assert_for (edge e
,
2863 gimple_stmt_iterator gsi
,
2864 vec
<assert_info
> &asserts
)
2866 for (unsigned i
= 0; i
< asserts
.length (); ++i
)
2867 /* Only register an ASSERT_EXPR if NAME was found in the sub-graph
2868 reachable from E. */
2869 if (live
.live_on_edge_p (asserts
[i
].name
, e
))
2870 register_new_assert_for (asserts
[i
].name
, asserts
[i
].expr
,
2871 asserts
[i
].comp_code
, asserts
[i
].val
,
2875 /* Determine whether the outgoing edges of BB should receive an
2876 ASSERT_EXPR for each of the operands of BB's LAST statement.
2877 The last statement of BB must be a COND_EXPR.
2879 If any of the sub-graphs rooted at BB have an interesting use of
2880 the predicate operands, an assert location node is added to the
2881 list of assertions for the corresponding operands. */
2884 vrp_asserts::find_conditional_asserts (basic_block bb
, gcond
*last
)
2886 gimple_stmt_iterator bsi
;
2892 bsi
= gsi_for_stmt (last
);
2894 /* Look for uses of the operands in each of the sub-graphs
2895 rooted at BB. We need to check each of the outgoing edges
2896 separately, so that we know what kind of ASSERT_EXPR to
2898 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
2903 /* Register the necessary assertions for each operand in the
2904 conditional predicate. */
2905 auto_vec
<assert_info
, 8> asserts
;
2906 FOR_EACH_SSA_TREE_OPERAND (op
, last
, iter
, SSA_OP_USE
)
2907 register_edge_assert_for (op
, e
,
2908 gimple_cond_code (last
),
2909 gimple_cond_lhs (last
),
2910 gimple_cond_rhs (last
), asserts
);
2911 finish_register_edge_assert_for (e
, bsi
, asserts
);
2915 /* Compare two case labels sorting first by the destination bb index
2916 and then by the case value. */
2919 vrp_asserts::compare_case_labels (const void *p1
, const void *p2
)
2921 const struct case_info
*ci1
= (const struct case_info
*) p1
;
2922 const struct case_info
*ci2
= (const struct case_info
*) p2
;
2923 int idx1
= ci1
->bb
->index
;
2924 int idx2
= ci2
->bb
->index
;
2928 else if (idx1
== idx2
)
2930 /* Make sure the default label is first in a group. */
2931 if (!CASE_LOW (ci1
->expr
))
2933 else if (!CASE_LOW (ci2
->expr
))
2936 return tree_int_cst_compare (CASE_LOW (ci1
->expr
),
2937 CASE_LOW (ci2
->expr
));
2943 /* Determine whether the outgoing edges of BB should receive an
2944 ASSERT_EXPR for each of the operands of BB's LAST statement.
2945 The last statement of BB must be a SWITCH_EXPR.
2947 If any of the sub-graphs rooted at BB have an interesting use of
2948 the predicate operands, an assert location node is added to the
2949 list of assertions for the corresponding operands. */
2952 vrp_asserts::find_switch_asserts (basic_block bb
, gswitch
*last
)
2954 gimple_stmt_iterator bsi
;
2957 struct case_info
*ci
;
2958 size_t n
= gimple_switch_num_labels (last
);
2959 #if GCC_VERSION >= 4000
2962 /* Work around GCC 3.4 bug (PR 37086). */
2963 volatile unsigned int idx
;
2966 bsi
= gsi_for_stmt (last
);
2967 op
= gimple_switch_index (last
);
2968 if (TREE_CODE (op
) != SSA_NAME
)
2971 /* Build a vector of case labels sorted by destination label. */
2972 ci
= XNEWVEC (struct case_info
, n
);
2973 for (idx
= 0; idx
< n
; ++idx
)
2975 ci
[idx
].expr
= gimple_switch_label (last
, idx
);
2976 ci
[idx
].bb
= label_to_block (fun
, CASE_LABEL (ci
[idx
].expr
));
2978 edge default_edge
= find_edge (bb
, ci
[0].bb
);
2979 qsort (ci
, n
, sizeof (struct case_info
), compare_case_labels
);
2981 for (idx
= 0; idx
< n
; ++idx
)
2984 tree cl
= ci
[idx
].expr
;
2985 basic_block cbb
= ci
[idx
].bb
;
2987 min
= CASE_LOW (cl
);
2988 max
= CASE_HIGH (cl
);
2990 /* If there are multiple case labels with the same destination
2991 we need to combine them to a single value range for the edge. */
2992 if (idx
+ 1 < n
&& cbb
== ci
[idx
+ 1].bb
)
2994 /* Skip labels until the last of the group. */
2997 } while (idx
< n
&& cbb
== ci
[idx
].bb
);
3000 /* Pick up the maximum of the case label range. */
3001 if (CASE_HIGH (ci
[idx
].expr
))
3002 max
= CASE_HIGH (ci
[idx
].expr
);
3004 max
= CASE_LOW (ci
[idx
].expr
);
3007 /* Can't extract a useful assertion out of a range that includes the
3009 if (min
== NULL_TREE
)
3012 /* Find the edge to register the assert expr on. */
3013 e
= find_edge (bb
, cbb
);
3015 /* Register the necessary assertions for the operand in the
3017 auto_vec
<assert_info
, 8> asserts
;
3018 register_edge_assert_for (op
, e
,
3019 max
? GE_EXPR
: EQ_EXPR
,
3020 op
, fold_convert (TREE_TYPE (op
), min
),
3023 register_edge_assert_for (op
, e
, LE_EXPR
, op
,
3024 fold_convert (TREE_TYPE (op
), max
),
3026 finish_register_edge_assert_for (e
, bsi
, asserts
);
3031 if (!live
.live_on_edge_p (op
, default_edge
))
3034 /* Now register along the default label assertions that correspond to the
3035 anti-range of each label. */
3036 int insertion_limit
= param_max_vrp_switch_assertions
;
3037 if (insertion_limit
== 0)
3040 /* We can't do this if the default case shares a label with another case. */
3041 tree default_cl
= gimple_switch_default_label (last
);
3042 for (idx
= 1; idx
< n
; idx
++)
3045 tree cl
= gimple_switch_label (last
, idx
);
3046 if (CASE_LABEL (cl
) == CASE_LABEL (default_cl
))
3049 min
= CASE_LOW (cl
);
3050 max
= CASE_HIGH (cl
);
3052 /* Combine contiguous case ranges to reduce the number of assertions
3054 for (idx
= idx
+ 1; idx
< n
; idx
++)
3056 tree next_min
, next_max
;
3057 tree next_cl
= gimple_switch_label (last
, idx
);
3058 if (CASE_LABEL (next_cl
) == CASE_LABEL (default_cl
))
3061 next_min
= CASE_LOW (next_cl
);
3062 next_max
= CASE_HIGH (next_cl
);
3064 wide_int difference
= (wi::to_wide (next_min
)
3065 - wi::to_wide (max
? max
: min
));
3066 if (wi::eq_p (difference
, 1))
3067 max
= next_max
? next_max
: next_min
;
3073 if (max
== NULL_TREE
)
3075 /* Register the assertion OP != MIN. */
3076 auto_vec
<assert_info
, 8> asserts
;
3077 min
= fold_convert (TREE_TYPE (op
), min
);
3078 register_edge_assert_for (op
, default_edge
, NE_EXPR
, op
, min
,
3080 finish_register_edge_assert_for (default_edge
, bsi
, asserts
);
3084 /* Register the assertion (unsigned)OP - MIN > (MAX - MIN),
3085 which will give OP the anti-range ~[MIN,MAX]. */
3086 tree uop
= fold_convert (unsigned_type_for (TREE_TYPE (op
)), op
);
3087 min
= fold_convert (TREE_TYPE (uop
), min
);
3088 max
= fold_convert (TREE_TYPE (uop
), max
);
3090 tree lhs
= fold_build2 (MINUS_EXPR
, TREE_TYPE (uop
), uop
, min
);
3091 tree rhs
= int_const_binop (MINUS_EXPR
, max
, min
);
3092 register_new_assert_for (op
, lhs
, GT_EXPR
, rhs
,
3093 NULL
, default_edge
, bsi
);
3096 if (--insertion_limit
== 0)
3101 /* Traverse all the statements in block BB looking for statements that
3102 may generate useful assertions for the SSA names in their operand.
3103 If a statement produces a useful assertion A for name N_i, then the
3104 list of assertions already generated for N_i is scanned to
3105 determine if A is actually needed.
3107 If N_i already had the assertion A at a location dominating the
3108 current location, then nothing needs to be done. Otherwise, the
3109 new location for A is recorded instead.
3111 1- For every statement S in BB, all the variables used by S are
3112 added to bitmap FOUND_IN_SUBGRAPH.
3114 2- If statement S uses an operand N in a way that exposes a known
3115 value range for N, then if N was not already generated by an
3116 ASSERT_EXPR, create a new assert location for N. For instance,
3117 if N is a pointer and the statement dereferences it, we can
3118 assume that N is not NULL.
3120 3- COND_EXPRs are a special case of #2. We can derive range
3121 information from the predicate but need to insert different
3122 ASSERT_EXPRs for each of the sub-graphs rooted at the
3123 conditional block. If the last statement of BB is a conditional
3124 expression of the form 'X op Y', then
3126 a) Remove X and Y from the set FOUND_IN_SUBGRAPH.
3128 b) If the conditional is the only entry point to the sub-graph
3129 corresponding to the THEN_CLAUSE, recurse into it. On
3130 return, if X and/or Y are marked in FOUND_IN_SUBGRAPH, then
3131 an ASSERT_EXPR is added for the corresponding variable.
3133 c) Repeat step (b) on the ELSE_CLAUSE.
3135 d) Mark X and Y in FOUND_IN_SUBGRAPH.
3144 In this case, an assertion on the THEN clause is useful to
3145 determine that 'a' is always 9 on that edge. However, an assertion
3146 on the ELSE clause would be unnecessary.
3148 4- If BB does not end in a conditional expression, then we recurse
3149 into BB's dominator children.
3151 At the end of the recursive traversal, every SSA name will have a
3152 list of locations where ASSERT_EXPRs should be added. When a new
3153 location for name N is found, it is registered by calling
3154 register_new_assert_for. That function keeps track of all the
3155 registered assertions to prevent adding unnecessary assertions.
3156 For instance, if a pointer P_4 is dereferenced more than once in a
3157 dominator tree, only the location dominating all the dereference of
3158 P_4 will receive an ASSERT_EXPR. */
3161 vrp_asserts::find_assert_locations_in_bb (basic_block bb
)
3165 last
= last_stmt (bb
);
3167 /* If BB's last statement is a conditional statement involving integer
3168 operands, determine if we need to add ASSERT_EXPRs. */
3170 && gimple_code (last
) == GIMPLE_COND
3171 && !fp_predicate (last
)
3172 && !ZERO_SSA_OPERANDS (last
, SSA_OP_USE
))
3173 find_conditional_asserts (bb
, as_a
<gcond
*> (last
));
3175 /* If BB's last statement is a switch statement involving integer
3176 operands, determine if we need to add ASSERT_EXPRs. */
3178 && gimple_code (last
) == GIMPLE_SWITCH
3179 && !ZERO_SSA_OPERANDS (last
, SSA_OP_USE
))
3180 find_switch_asserts (bb
, as_a
<gswitch
*> (last
));
3182 /* Traverse all the statements in BB marking used names and looking
3183 for statements that may infer assertions for their used operands. */
3184 for (gimple_stmt_iterator si
= gsi_last_bb (bb
); !gsi_end_p (si
);
3191 stmt
= gsi_stmt (si
);
3193 if (is_gimple_debug (stmt
))
3196 /* See if we can derive an assertion for any of STMT's operands. */
3197 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, i
, SSA_OP_USE
)
3200 enum tree_code comp_code
;
3202 /* If op is not live beyond this stmt, do not bother to insert
3204 if (!live
.live_on_block_p (op
, bb
))
3207 /* If OP is used in such a way that we can infer a value
3208 range for it, and we don't find a previous assertion for
3209 it, create a new assertion location node for OP. */
3210 if (infer_value_range (stmt
, op
, &comp_code
, &value
))
3212 /* If we are able to infer a nonzero value range for OP,
3213 then walk backwards through the use-def chain to see if OP
3214 was set via a typecast.
3216 If so, then we can also infer a nonzero value range
3217 for the operand of the NOP_EXPR. */
3218 if (comp_code
== NE_EXPR
&& integer_zerop (value
))
3221 gimple
*def_stmt
= SSA_NAME_DEF_STMT (t
);
3223 while (is_gimple_assign (def_stmt
)
3224 && CONVERT_EXPR_CODE_P
3225 (gimple_assign_rhs_code (def_stmt
))
3227 (gimple_assign_rhs1 (def_stmt
)) == SSA_NAME
3229 (TREE_TYPE (gimple_assign_rhs1 (def_stmt
))))
3231 t
= gimple_assign_rhs1 (def_stmt
);
3232 def_stmt
= SSA_NAME_DEF_STMT (t
);
3234 /* Note we want to register the assert for the
3235 operand of the NOP_EXPR after SI, not after the
3237 if (live
.live_on_block_p (t
, bb
))
3238 register_new_assert_for (t
, t
, comp_code
, value
,
3243 register_new_assert_for (op
, op
, comp_code
, value
, bb
, NULL
, si
);
3248 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, i
, SSA_OP_USE
)
3250 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, i
, SSA_OP_DEF
)
3251 live
.clear (op
, bb
);
3254 /* Traverse all PHI nodes in BB, updating live. */
3255 for (gphi_iterator si
= gsi_start_phis (bb
); !gsi_end_p (si
);
3258 use_operand_p arg_p
;
3260 gphi
*phi
= si
.phi ();
3261 tree res
= gimple_phi_result (phi
);
3263 if (virtual_operand_p (res
))
3266 FOR_EACH_PHI_ARG (arg_p
, phi
, i
, SSA_OP_USE
)
3268 tree arg
= USE_FROM_PTR (arg_p
);
3269 if (TREE_CODE (arg
) == SSA_NAME
)
3273 live
.clear (res
, bb
);
3277 /* Do an RPO walk over the function computing SSA name liveness
3278 on-the-fly and deciding on assert expressions to insert. */
3281 vrp_asserts::find_assert_locations (void)
3283 int *rpo
= XNEWVEC (int, last_basic_block_for_fn (fun
));
3284 int *bb_rpo
= XNEWVEC (int, last_basic_block_for_fn (fun
));
3285 int *last_rpo
= XCNEWVEC (int, last_basic_block_for_fn (fun
));
3288 rpo_cnt
= pre_and_rev_post_order_compute (NULL
, rpo
, false);
3289 for (i
= 0; i
< rpo_cnt
; ++i
)
3292 /* Pre-seed loop latch liveness from loop header PHI nodes. Due to
3293 the order we compute liveness and insert asserts we otherwise
3294 fail to insert asserts into the loop latch. */
3295 for (auto loop
: loops_list (cfun
, 0))
3297 i
= loop
->latch
->index
;
3298 unsigned int j
= single_succ_edge (loop
->latch
)->dest_idx
;
3299 for (gphi_iterator gsi
= gsi_start_phis (loop
->header
);
3300 !gsi_end_p (gsi
); gsi_next (&gsi
))
3302 gphi
*phi
= gsi
.phi ();
3303 if (virtual_operand_p (gimple_phi_result (phi
)))
3305 tree arg
= gimple_phi_arg_def (phi
, j
);
3306 if (TREE_CODE (arg
) == SSA_NAME
)
3307 live
.set (arg
, loop
->latch
);
3311 for (i
= rpo_cnt
- 1; i
>= 0; --i
)
3313 basic_block bb
= BASIC_BLOCK_FOR_FN (fun
, rpo
[i
]);
3317 /* Process BB and update the live information with uses in
3319 find_assert_locations_in_bb (bb
);
3321 /* Merge liveness into the predecessor blocks and free it. */
3322 if (!live
.block_has_live_names_p (bb
))
3325 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
3327 int pred
= e
->src
->index
;
3328 if ((e
->flags
& EDGE_DFS_BACK
) || pred
== ENTRY_BLOCK
)
3331 live
.merge (e
->src
, bb
);
3333 if (bb_rpo
[pred
] < pred_rpo
)
3334 pred_rpo
= bb_rpo
[pred
];
3337 /* Record the RPO number of the last visited block that needs
3338 live information from this block. */
3339 last_rpo
[rpo
[i
]] = pred_rpo
;
3342 live
.clear_block (bb
);
3344 /* We can free all successors live bitmaps if all their
3345 predecessors have been visited already. */
3346 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
3347 if (last_rpo
[e
->dest
->index
] == i
)
3348 live
.clear_block (e
->dest
);
3352 XDELETEVEC (bb_rpo
);
3353 XDELETEVEC (last_rpo
);
3356 /* Create an ASSERT_EXPR for NAME and insert it in the location
3357 indicated by LOC. Return true if we made any edge insertions. */
3360 vrp_asserts::process_assert_insertions_for (tree name
, assert_locus
*loc
)
3362 /* Build the comparison expression NAME_i COMP_CODE VAL. */
3365 gimple
*assert_stmt
;
3369 /* If we have X <=> X do not insert an assert expr for that. */
3370 if (loc
->expr
== loc
->val
)
3373 cond
= build2 (loc
->comp_code
, boolean_type_node
, loc
->expr
, loc
->val
);
3374 assert_stmt
= build_assert_expr_for (cond
, name
);
3377 /* We have been asked to insert the assertion on an edge. This
3378 is used only by COND_EXPR and SWITCH_EXPR assertions. */
3379 gcc_checking_assert (gimple_code (gsi_stmt (loc
->si
)) == GIMPLE_COND
3380 || (gimple_code (gsi_stmt (loc
->si
))
3383 gsi_insert_on_edge (loc
->e
, assert_stmt
);
3387 /* If the stmt iterator points at the end then this is an insertion
3388 at the beginning of a block. */
3389 if (gsi_end_p (loc
->si
))
3391 gimple_stmt_iterator si
= gsi_after_labels (loc
->bb
);
3392 gsi_insert_before (&si
, assert_stmt
, GSI_SAME_STMT
);
3396 /* Otherwise, we can insert right after LOC->SI iff the
3397 statement must not be the last statement in the block. */
3398 stmt
= gsi_stmt (loc
->si
);
3399 if (!stmt_ends_bb_p (stmt
))
3401 gsi_insert_after (&loc
->si
, assert_stmt
, GSI_SAME_STMT
);
3405 /* If STMT must be the last statement in BB, we can only insert new
3406 assertions on the non-abnormal edge out of BB. Note that since
3407 STMT is not control flow, there may only be one non-abnormal/eh edge
3409 FOR_EACH_EDGE (e
, ei
, loc
->bb
->succs
)
3410 if (!(e
->flags
& (EDGE_ABNORMAL
|EDGE_EH
)))
3412 gsi_insert_on_edge (e
, assert_stmt
);
3419 /* Qsort helper for sorting assert locations. If stable is true, don't
3420 use iterative_hash_expr because it can be unstable for -fcompare-debug,
3421 on the other side some pointers might be NULL. */
3423 template <bool stable
>
3425 vrp_asserts::compare_assert_loc (const void *pa
, const void *pb
)
3427 assert_locus
* const a
= *(assert_locus
* const *)pa
;
3428 assert_locus
* const b
= *(assert_locus
* const *)pb
;
3430 /* If stable, some asserts might be optimized away already, sort
3440 if (a
->e
== NULL
&& b
->e
!= NULL
)
3442 else if (a
->e
!= NULL
&& b
->e
== NULL
)
3445 /* After the above checks, we know that (a->e == NULL) == (b->e == NULL),
3446 no need to test both a->e and b->e. */
3448 /* Sort after destination index. */
3451 else if (a
->e
->dest
->index
> b
->e
->dest
->index
)
3453 else if (a
->e
->dest
->index
< b
->e
->dest
->index
)
3456 /* Sort after comp_code. */
3457 if (a
->comp_code
> b
->comp_code
)
3459 else if (a
->comp_code
< b
->comp_code
)
3464 /* E.g. if a->val is ADDR_EXPR of a VAR_DECL, iterative_hash_expr
3465 uses DECL_UID of the VAR_DECL, so sorting might differ between
3466 -g and -g0. When doing the removal of redundant assert exprs
3467 and commonization to successors, this does not matter, but for
3468 the final sort needs to be stable. */
3476 ha
= iterative_hash_expr (a
->expr
, iterative_hash_expr (a
->val
, 0));
3477 hb
= iterative_hash_expr (b
->expr
, iterative_hash_expr (b
->val
, 0));
3480 /* Break the tie using hashing and source/bb index. */
3482 return (a
->e
!= NULL
3483 ? a
->e
->src
->index
- b
->e
->src
->index
3484 : a
->bb
->index
- b
->bb
->index
);
3485 return ha
> hb
? 1 : -1;
3488 /* Process all the insertions registered for every name N_i registered
3489 in NEED_ASSERT_FOR. The list of assertions to be inserted are
3490 found in ASSERTS_FOR[i]. */
3493 vrp_asserts::process_assert_insertions ()
3497 bool update_edges_p
= false;
3498 int num_asserts
= 0;
3500 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3503 EXECUTE_IF_SET_IN_BITMAP (need_assert_for
, 0, i
, bi
)
3505 assert_locus
*loc
= asserts_for
[i
];
3508 auto_vec
<assert_locus
*, 16> asserts
;
3509 for (; loc
; loc
= loc
->next
)
3510 asserts
.safe_push (loc
);
3511 asserts
.qsort (compare_assert_loc
<false>);
3513 /* Push down common asserts to successors and remove redundant ones. */
3515 assert_locus
*common
= NULL
;
3516 unsigned commonj
= 0;
3517 for (unsigned j
= 0; j
< asserts
.length (); ++j
)
3523 || loc
->e
->dest
!= common
->e
->dest
3524 || loc
->comp_code
!= common
->comp_code
3525 || ! operand_equal_p (loc
->val
, common
->val
, 0)
3526 || ! operand_equal_p (loc
->expr
, common
->expr
, 0))
3532 else if (loc
->e
== asserts
[j
-1]->e
)
3534 /* Remove duplicate asserts. */
3535 if (commonj
== j
- 1)
3540 free (asserts
[j
-1]);
3541 asserts
[j
-1] = NULL
;
3546 if (EDGE_COUNT (common
->e
->dest
->preds
) == ecnt
)
3548 /* We have the same assertion on all incoming edges of a BB.
3549 Insert it at the beginning of that block. */
3550 loc
->bb
= loc
->e
->dest
;
3552 loc
->si
= gsi_none ();
3554 /* Clear asserts commoned. */
3555 for (; commonj
!= j
; ++commonj
)
3556 if (asserts
[commonj
])
3558 free (asserts
[commonj
]);
3559 asserts
[commonj
] = NULL
;
3565 /* The asserts vector sorting above might be unstable for
3566 -fcompare-debug, sort again to ensure a stable sort. */
3567 asserts
.qsort (compare_assert_loc
<true>);
3568 for (unsigned j
= 0; j
< asserts
.length (); ++j
)
3573 update_edges_p
|= process_assert_insertions_for (ssa_name (i
), loc
);
3580 gsi_commit_edge_inserts ();
3582 statistics_counter_event (fun
, "Number of ASSERT_EXPR expressions inserted",
3586 /* Traverse the flowgraph looking for conditional jumps to insert range
3587 expressions. These range expressions are meant to provide information
3588 to optimizations that need to reason in terms of value ranges. They
3589 will not be expanded into RTL. For instance, given:
3598 this pass will transform the code into:
3604 x = ASSERT_EXPR <x, x < y>
3609 y = ASSERT_EXPR <y, x >= y>
3613 The idea is that once copy and constant propagation have run, other
3614 optimizations will be able to determine what ranges of values can 'x'
3615 take in different paths of the code, simply by checking the reaching
3616 definition of 'x'. */
3619 vrp_asserts::insert_range_assertions (void)
3621 need_assert_for
= BITMAP_ALLOC (NULL
);
3622 asserts_for
= XCNEWVEC (assert_locus
*, num_ssa_names
);
3624 calculate_dominance_info (CDI_DOMINATORS
);
3626 find_assert_locations ();
3627 if (!bitmap_empty_p (need_assert_for
))
3629 process_assert_insertions ();
3630 update_ssa (TODO_update_ssa_no_phi
);
3633 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3635 fprintf (dump_file
, "\nSSA form after inserting ASSERT_EXPRs\n");
3636 dump_function_to_file (current_function_decl
, dump_file
, dump_flags
);
3640 BITMAP_FREE (need_assert_for
);
3643 /* Return true if all imm uses of VAR are either in STMT, or
3644 feed (optionally through a chain of single imm uses) GIMPLE_COND
3645 in basic block COND_BB. */
3648 vrp_asserts::all_imm_uses_in_stmt_or_feed_cond (tree var
,
3650 basic_block cond_bb
)
3652 use_operand_p use_p
, use2_p
;
3653 imm_use_iterator iter
;
3655 FOR_EACH_IMM_USE_FAST (use_p
, iter
, var
)
3656 if (USE_STMT (use_p
) != stmt
)
3658 gimple
*use_stmt
= USE_STMT (use_p
), *use_stmt2
;
3659 if (is_gimple_debug (use_stmt
))
3661 while (is_gimple_assign (use_stmt
)
3662 && TREE_CODE (gimple_assign_lhs (use_stmt
)) == SSA_NAME
3663 && single_imm_use (gimple_assign_lhs (use_stmt
),
3664 &use2_p
, &use_stmt2
))
3665 use_stmt
= use_stmt2
;
3666 if (gimple_code (use_stmt
) != GIMPLE_COND
3667 || gimple_bb (use_stmt
) != cond_bb
)
3673 /* Convert range assertion expressions into the implied copies and
3674 copy propagate away the copies. Doing the trivial copy propagation
3675 here avoids the need to run the full copy propagation pass after
3678 FIXME, this will eventually lead to copy propagation removing the
3679 names that had useful range information attached to them. For
3680 instance, if we had the assertion N_i = ASSERT_EXPR <N_j, N_j > 3>,
3681 then N_i will have the range [3, +INF].
3683 However, by converting the assertion into the implied copy
3684 operation N_i = N_j, we will then copy-propagate N_j into the uses
3685 of N_i and lose the range information. We may want to hold on to
3686 ASSERT_EXPRs a little while longer as the ranges could be used in
3687 things like jump threading.
3689 The problem with keeping ASSERT_EXPRs around is that passes after
3690 VRP need to handle them appropriately.
3692 Another approach would be to make the range information a first
3693 class property of the SSA_NAME so that it can be queried from
3694 any pass. This is made somewhat more complex by the need for
3695 multiple ranges to be associated with one SSA_NAME. */
3698 vrp_asserts::remove_range_assertions ()
3701 gimple_stmt_iterator si
;
3702 /* 1 if looking at ASSERT_EXPRs immediately at the beginning of
3703 a basic block preceeded by GIMPLE_COND branching to it and
3704 __builtin_trap, -1 if not yet checked, 0 otherwise. */
3707 /* Note that the BSI iterator bump happens at the bottom of the
3708 loop and no bump is necessary if we're removing the statement
3709 referenced by the current BSI. */
3710 FOR_EACH_BB_FN (bb
, fun
)
3711 for (si
= gsi_after_labels (bb
), is_unreachable
= -1; !gsi_end_p (si
);)
3713 gimple
*stmt
= gsi_stmt (si
);
3715 if (is_gimple_assign (stmt
)
3716 && gimple_assign_rhs_code (stmt
) == ASSERT_EXPR
)
3718 tree lhs
= gimple_assign_lhs (stmt
);
3719 tree rhs
= gimple_assign_rhs1 (stmt
);
3722 var
= ASSERT_EXPR_VAR (rhs
);
3724 if (TREE_CODE (var
) == SSA_NAME
3725 && !POINTER_TYPE_P (TREE_TYPE (lhs
))
3726 && SSA_NAME_RANGE_INFO (lhs
))
3728 if (is_unreachable
== -1)
3731 if (single_pred_p (bb
)
3732 && assert_unreachable_fallthru_edge_p
3733 (single_pred_edge (bb
)))
3737 if (x_7 >= 10 && x_7 < 20)
3738 __builtin_unreachable ();
3739 x_8 = ASSERT_EXPR <x_7, ...>;
3740 if the only uses of x_7 are in the ASSERT_EXPR and
3741 in the condition. In that case, we can copy the
3742 range info from x_8 computed in this pass also
3745 && all_imm_uses_in_stmt_or_feed_cond (var
, stmt
,
3748 set_range_info (var
, SSA_NAME_RANGE_TYPE (lhs
),
3749 SSA_NAME_RANGE_INFO (lhs
)->get_min (),
3750 SSA_NAME_RANGE_INFO (lhs
)->get_max ());
3751 maybe_set_nonzero_bits (single_pred_edge (bb
), var
);
3755 /* Propagate the RHS into every use of the LHS. For SSA names
3756 also propagate abnormals as it merely restores the original
3757 IL in this case (an replace_uses_by would assert). */
3758 if (TREE_CODE (var
) == SSA_NAME
)
3760 imm_use_iterator iter
;
3761 use_operand_p use_p
;
3763 FOR_EACH_IMM_USE_STMT (use_stmt
, iter
, lhs
)
3764 FOR_EACH_IMM_USE_ON_STMT (use_p
, iter
)
3765 SET_USE (use_p
, var
);
3768 replace_uses_by (lhs
, var
);
3770 /* And finally, remove the copy, it is not needed. */
3771 gsi_remove (&si
, true);
3772 release_defs (stmt
);
3776 if (!is_gimple_debug (gsi_stmt (si
)))
3783 class vrp_prop
: public ssa_propagation_engine
3786 vrp_prop (vr_values
*v
)
3787 : ssa_propagation_engine (),
3790 void initialize (struct function
*);
3794 enum ssa_prop_result
visit_stmt (gimple
*, edge
*, tree
*) FINAL OVERRIDE
;
3795 enum ssa_prop_result
visit_phi (gphi
*) FINAL OVERRIDE
;
3797 struct function
*fun
;
3798 vr_values
*m_vr_values
;
3801 /* Initialization required by ssa_propagate engine. */
3804 vrp_prop::initialize (struct function
*fn
)
3809 FOR_EACH_BB_FN (bb
, fun
)
3811 for (gphi_iterator si
= gsi_start_phis (bb
); !gsi_end_p (si
);
3814 gphi
*phi
= si
.phi ();
3815 if (!stmt_interesting_for_vrp (phi
))
3817 tree lhs
= PHI_RESULT (phi
);
3818 m_vr_values
->set_def_to_varying (lhs
);
3819 prop_set_simulate_again (phi
, false);
3822 prop_set_simulate_again (phi
, true);
3825 for (gimple_stmt_iterator si
= gsi_start_bb (bb
); !gsi_end_p (si
);
3828 gimple
*stmt
= gsi_stmt (si
);
3830 /* If the statement is a control insn, then we do not
3831 want to avoid simulating the statement once. Failure
3832 to do so means that those edges will never get added. */
3833 if (stmt_ends_bb_p (stmt
))
3834 prop_set_simulate_again (stmt
, true);
3835 else if (!stmt_interesting_for_vrp (stmt
))
3837 m_vr_values
->set_defs_to_varying (stmt
);
3838 prop_set_simulate_again (stmt
, false);
3841 prop_set_simulate_again (stmt
, true);
3846 /* Evaluate statement STMT. If the statement produces a useful range,
3847 return SSA_PROP_INTERESTING and record the SSA name with the
3848 interesting range into *OUTPUT_P.
3850 If STMT is a conditional branch and we can determine its truth
3851 value, the taken edge is recorded in *TAKEN_EDGE_P.
3853 If STMT produces a varying value, return SSA_PROP_VARYING. */
3855 enum ssa_prop_result
3856 vrp_prop::visit_stmt (gimple
*stmt
, edge
*taken_edge_p
, tree
*output_p
)
3858 tree lhs
= gimple_get_lhs (stmt
);
3859 value_range_equiv vr
;
3860 m_vr_values
->extract_range_from_stmt (stmt
, taken_edge_p
, output_p
, &vr
);
3864 if (m_vr_values
->update_value_range (*output_p
, &vr
))
3866 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3868 fprintf (dump_file
, "Found new range for ");
3869 print_generic_expr (dump_file
, *output_p
);
3870 fprintf (dump_file
, ": ");
3871 dump_value_range (dump_file
, &vr
);
3872 fprintf (dump_file
, "\n");
3875 if (vr
.varying_p ())
3876 return SSA_PROP_VARYING
;
3878 return SSA_PROP_INTERESTING
;
3880 return SSA_PROP_NOT_INTERESTING
;
3883 if (is_gimple_call (stmt
) && gimple_call_internal_p (stmt
))
3884 switch (gimple_call_internal_fn (stmt
))
3886 case IFN_ADD_OVERFLOW
:
3887 case IFN_SUB_OVERFLOW
:
3888 case IFN_MUL_OVERFLOW
:
3889 case IFN_ATOMIC_COMPARE_EXCHANGE
:
3890 /* These internal calls return _Complex integer type,
3891 which VRP does not track, but the immediate uses
3892 thereof might be interesting. */
3893 if (lhs
&& TREE_CODE (lhs
) == SSA_NAME
)
3895 imm_use_iterator iter
;
3896 use_operand_p use_p
;
3897 enum ssa_prop_result res
= SSA_PROP_VARYING
;
3899 m_vr_values
->set_def_to_varying (lhs
);
3901 FOR_EACH_IMM_USE_FAST (use_p
, iter
, lhs
)
3903 gimple
*use_stmt
= USE_STMT (use_p
);
3904 if (!is_gimple_assign (use_stmt
))
3906 enum tree_code rhs_code
= gimple_assign_rhs_code (use_stmt
);
3907 if (rhs_code
!= REALPART_EXPR
&& rhs_code
!= IMAGPART_EXPR
)
3909 tree rhs1
= gimple_assign_rhs1 (use_stmt
);
3910 tree use_lhs
= gimple_assign_lhs (use_stmt
);
3911 if (TREE_CODE (rhs1
) != rhs_code
3912 || TREE_OPERAND (rhs1
, 0) != lhs
3913 || TREE_CODE (use_lhs
) != SSA_NAME
3914 || !stmt_interesting_for_vrp (use_stmt
)
3915 || (!INTEGRAL_TYPE_P (TREE_TYPE (use_lhs
))
3916 || !TYPE_MIN_VALUE (TREE_TYPE (use_lhs
))
3917 || !TYPE_MAX_VALUE (TREE_TYPE (use_lhs
))))
3920 /* If there is a change in the value range for any of the
3921 REALPART_EXPR/IMAGPART_EXPR immediate uses, return
3922 SSA_PROP_INTERESTING. If there are any REALPART_EXPR
3923 or IMAGPART_EXPR immediate uses, but none of them have
3924 a change in their value ranges, return
3925 SSA_PROP_NOT_INTERESTING. If there are no
3926 {REAL,IMAG}PART_EXPR uses at all,
3927 return SSA_PROP_VARYING. */
3928 value_range_equiv new_vr
;
3929 m_vr_values
->extract_range_basic (&new_vr
, use_stmt
);
3930 const value_range_equiv
*old_vr
3931 = m_vr_values
->get_value_range (use_lhs
);
3932 if (!old_vr
->equal_p (new_vr
, /*ignore_equivs=*/false))
3933 res
= SSA_PROP_INTERESTING
;
3935 res
= SSA_PROP_NOT_INTERESTING
;
3936 new_vr
.equiv_clear ();
3937 if (res
== SSA_PROP_INTERESTING
)
3951 /* All other statements produce nothing of interest for VRP, so mark
3952 their outputs varying and prevent further simulation. */
3953 m_vr_values
->set_defs_to_varying (stmt
);
3955 return (*taken_edge_p
) ? SSA_PROP_INTERESTING
: SSA_PROP_VARYING
;
3958 /* Visit all arguments for PHI node PHI that flow through executable
3959 edges. If a valid value range can be derived from all the incoming
3960 value ranges, set a new range for the LHS of PHI. */
3962 enum ssa_prop_result
3963 vrp_prop::visit_phi (gphi
*phi
)
3965 tree lhs
= PHI_RESULT (phi
);
3966 value_range_equiv vr_result
;
3967 m_vr_values
->extract_range_from_phi_node (phi
, &vr_result
);
3968 if (m_vr_values
->update_value_range (lhs
, &vr_result
))
3970 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3972 fprintf (dump_file
, "Found new range for ");
3973 print_generic_expr (dump_file
, lhs
);
3974 fprintf (dump_file
, ": ");
3975 dump_value_range (dump_file
, &vr_result
);
3976 fprintf (dump_file
, "\n");
3979 if (vr_result
.varying_p ())
3980 return SSA_PROP_VARYING
;
3982 return SSA_PROP_INTERESTING
;
3985 /* Nothing changed, don't add outgoing edges. */
3986 return SSA_PROP_NOT_INTERESTING
;
3989 /* Traverse all the blocks folding conditionals with known ranges. */
3992 vrp_prop::finalize ()
3996 /* We have completed propagating through the lattice. */
3997 m_vr_values
->set_lattice_propagation_complete ();
4001 fprintf (dump_file
, "\nValue ranges after VRP:\n\n");
4002 m_vr_values
->dump (dump_file
);
4003 fprintf (dump_file
, "\n");
4006 /* Set value range to non pointer SSA_NAMEs. */
4007 for (i
= 0; i
< num_ssa_names
; i
++)
4009 tree name
= ssa_name (i
);
4013 const value_range_equiv
*vr
= m_vr_values
->get_value_range (name
);
4014 if (!name
|| vr
->varying_p () || !vr
->constant_p ())
4017 if (POINTER_TYPE_P (TREE_TYPE (name
))
4018 && range_includes_zero_p (vr
) == 0)
4019 set_ptr_nonnull (name
);
4020 else if (!POINTER_TYPE_P (TREE_TYPE (name
)))
4021 set_range_info (name
, *vr
);
4025 class vrp_folder
: public substitute_and_fold_engine
4028 vrp_folder (vr_values
*v
)
4029 : substitute_and_fold_engine (/* Fold all stmts. */ true),
4030 m_vr_values (v
), simplifier (v
)
4034 tree
value_of_expr (tree name
, gimple
*stmt
) OVERRIDE
4036 return m_vr_values
->value_of_expr (name
, stmt
);
4038 bool fold_stmt (gimple_stmt_iterator
*) FINAL OVERRIDE
;
4039 bool fold_predicate_in (gimple_stmt_iterator
*);
4041 vr_values
*m_vr_values
;
4042 simplify_using_ranges simplifier
;
4045 /* If the statement pointed by SI has a predicate whose value can be
4046 computed using the value range information computed by VRP, compute
4047 its value and return true. Otherwise, return false. */
4050 vrp_folder::fold_predicate_in (gimple_stmt_iterator
*si
)
4052 bool assignment_p
= false;
4054 gimple
*stmt
= gsi_stmt (*si
);
4056 if (is_gimple_assign (stmt
)
4057 && TREE_CODE_CLASS (gimple_assign_rhs_code (stmt
)) == tcc_comparison
)
4059 assignment_p
= true;
4060 val
= simplifier
.vrp_evaluate_conditional (gimple_assign_rhs_code (stmt
),
4061 gimple_assign_rhs1 (stmt
),
4062 gimple_assign_rhs2 (stmt
),
4065 else if (gcond
*cond_stmt
= dyn_cast
<gcond
*> (stmt
))
4066 val
= simplifier
.vrp_evaluate_conditional (gimple_cond_code (cond_stmt
),
4067 gimple_cond_lhs (cond_stmt
),
4068 gimple_cond_rhs (cond_stmt
),
4076 val
= fold_convert (TREE_TYPE (gimple_assign_lhs (stmt
)), val
);
4080 fprintf (dump_file
, "Folding predicate ");
4081 print_gimple_expr (dump_file
, stmt
, 0);
4082 fprintf (dump_file
, " to ");
4083 print_generic_expr (dump_file
, val
);
4084 fprintf (dump_file
, "\n");
4087 if (is_gimple_assign (stmt
))
4088 gimple_assign_set_rhs_from_tree (si
, val
);
4091 gcc_assert (gimple_code (stmt
) == GIMPLE_COND
);
4092 gcond
*cond_stmt
= as_a
<gcond
*> (stmt
);
4093 if (integer_zerop (val
))
4094 gimple_cond_make_false (cond_stmt
);
4095 else if (integer_onep (val
))
4096 gimple_cond_make_true (cond_stmt
);
4107 /* Callback for substitute_and_fold folding the stmt at *SI. */
4110 vrp_folder::fold_stmt (gimple_stmt_iterator
*si
)
4112 if (fold_predicate_in (si
))
4115 return simplifier
.simplify (si
);
4118 /* STMT is a conditional at the end of a basic block.
4120 If the conditional is of the form SSA_NAME op constant and the SSA_NAME
4121 was set via a type conversion, try to replace the SSA_NAME with the RHS
4122 of the type conversion. Doing so makes the conversion dead which helps
4123 subsequent passes. */
4126 vrp_simplify_cond_using_ranges (range_query
*query
, gcond
*stmt
)
4128 tree op0
= gimple_cond_lhs (stmt
);
4129 tree op1
= gimple_cond_rhs (stmt
);
4131 /* If we have a comparison of an SSA_NAME (OP0) against a constant,
4132 see if OP0 was set by a type conversion where the source of
4133 the conversion is another SSA_NAME with a range that fits
4134 into the range of OP0's type.
4136 If so, the conversion is redundant as the earlier SSA_NAME can be
4137 used for the comparison directly if we just massage the constant in the
4139 if (TREE_CODE (op0
) == SSA_NAME
4140 && TREE_CODE (op1
) == INTEGER_CST
)
4142 gimple
*def_stmt
= SSA_NAME_DEF_STMT (op0
);
4145 if (!is_gimple_assign (def_stmt
))
4148 switch (gimple_assign_rhs_code (def_stmt
))
4151 innerop
= gimple_assign_rhs1 (def_stmt
);
4153 case VIEW_CONVERT_EXPR
:
4154 innerop
= TREE_OPERAND (gimple_assign_rhs1 (def_stmt
), 0);
4155 if (!INTEGRAL_TYPE_P (TREE_TYPE (innerop
)))
4162 if (TREE_CODE (innerop
) == SSA_NAME
4163 && !POINTER_TYPE_P (TREE_TYPE (innerop
))
4164 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (innerop
)
4165 && desired_pro_or_demotion_p (TREE_TYPE (innerop
), TREE_TYPE (op0
)))
4167 const value_range
*vr
= query
->get_value_range (innerop
);
4169 if (range_int_cst_p (vr
)
4170 && range_fits_type_p (vr
,
4171 TYPE_PRECISION (TREE_TYPE (op0
)),
4172 TYPE_SIGN (TREE_TYPE (op0
)))
4173 && int_fits_type_p (op1
, TREE_TYPE (innerop
)))
4175 tree newconst
= fold_convert (TREE_TYPE (innerop
), op1
);
4176 gimple_cond_set_lhs (stmt
, innerop
);
4177 gimple_cond_set_rhs (stmt
, newconst
);
4179 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4181 fprintf (dump_file
, "Folded into: ");
4182 print_gimple_stmt (dump_file
, stmt
, 0, TDF_SLIM
);
4183 fprintf (dump_file
, "\n");
4190 /* A comparison of an SSA_NAME against a constant where the SSA_NAME
4191 was set by a type conversion can often be rewritten to use the RHS
4192 of the type conversion. Do this optimization for all conditionals
4195 However, doing so inhibits jump threading through the comparison.
4196 So that transformation is not performed until after jump threading
4200 simplify_casted_conds (function
*fun
, range_query
*query
)
4203 FOR_EACH_BB_FN (bb
, fun
)
4205 gimple
*last
= last_stmt (bb
);
4206 if (last
&& gimple_code (last
) == GIMPLE_COND
)
4207 vrp_simplify_cond_using_ranges (query
, as_a
<gcond
*> (last
));
4211 /* Main entry point to VRP (Value Range Propagation). This pass is
4212 loosely based on J. R. C. Patterson, ``Accurate Static Branch
4213 Prediction by Value Range Propagation,'' in SIGPLAN Conference on
4214 Programming Language Design and Implementation, pp. 67-78, 1995.
4215 Also available at http://citeseer.ist.psu.edu/patterson95accurate.html
4217 This is essentially an SSA-CCP pass modified to deal with ranges
4218 instead of constants.
4220 While propagating ranges, we may find that two or more SSA name
4221 have equivalent, though distinct ranges. For instance,
4224 2 p_4 = ASSERT_EXPR <p_3, p_3 != 0>
4226 4 p_5 = ASSERT_EXPR <p_4, p_4 == q_2>;
4230 In the code above, pointer p_5 has range [q_2, q_2], but from the
4231 code we can also determine that p_5 cannot be NULL and, if q_2 had
4232 a non-varying range, p_5's range should also be compatible with it.
4234 These equivalences are created by two expressions: ASSERT_EXPR and
4235 copy operations. Since p_5 is an assertion on p_4, and p_4 was the
4236 result of another assertion, then we can use the fact that p_5 and
4237 p_4 are equivalent when evaluating p_5's range.
4239 Together with value ranges, we also propagate these equivalences
4240 between names so that we can take advantage of information from
4241 multiple ranges when doing final replacement. Note that this
4242 equivalency relation is transitive but not symmetric.
4244 In the example above, p_5 is equivalent to p_4, q_2 and p_3, but we
4245 cannot assert that q_2 is equivalent to p_5 because q_2 may be used
4246 in contexts where that assertion does not hold (e.g., in line 6).
4248 TODO, the main difference between this pass and Patterson's is that
4249 we do not propagate edge probabilities. We only compute whether
4250 edges can be taken or not. That is, instead of having a spectrum
4251 of jump probabilities between 0 and 1, we only deal with 0, 1 and
4252 DON'T KNOW. In the future, it may be worthwhile to propagate
4253 probabilities to aid branch prediction. */
4256 execute_vrp (struct function
*fun
, bool warn_array_bounds_p
)
4258 loop_optimizer_init (LOOPS_NORMAL
| LOOPS_HAVE_RECORDED_EXITS
);
4259 rewrite_into_loop_closed_ssa (NULL
, TODO_update_ssa
);
4262 /* ??? This ends up using stale EDGE_DFS_BACK for liveness computation.
4263 Inserting assertions may split edges which will invalidate
4265 vrp_asserts
assert_engine (fun
);
4266 assert_engine
.insert_range_assertions ();
4268 /* For visiting PHI nodes we need EDGE_DFS_BACK computed. */
4269 mark_dfs_back_edges ();
4271 vr_values vrp_vr_values
;
4273 class vrp_prop
vrp_prop (&vrp_vr_values
);
4274 vrp_prop
.initialize (fun
);
4275 vrp_prop
.ssa_propagate ();
4277 /* Instantiate the folder here, so that edge cleanups happen at the
4278 end of this function. */
4279 vrp_folder
folder (&vrp_vr_values
);
4280 vrp_prop
.finalize ();
4282 /* If we're checking array refs, we want to merge information on
4283 the executability of each edge between vrp_folder and the
4284 check_array_bounds_dom_walker: each can clear the
4285 EDGE_EXECUTABLE flag on edges, in different ways.
4287 Hence, if we're going to call check_all_array_refs, set
4288 the flag on every edge now, rather than in
4289 check_array_bounds_dom_walker's ctor; vrp_folder may clear
4290 it from some edges. */
4291 if (warn_array_bounds
&& warn_array_bounds_p
)
4292 set_all_edges_as_executable (fun
);
4294 folder
.substitute_and_fold ();
4296 if (warn_array_bounds
&& warn_array_bounds_p
)
4298 array_bounds_checker
array_checker (fun
, &vrp_vr_values
);
4299 array_checker
.check ();
4302 simplify_casted_conds (fun
, &vrp_vr_values
);
4304 free_numbers_of_iterations_estimates (fun
);
4306 /* ASSERT_EXPRs must be removed before finalizing jump threads
4307 as finalizing jump threads calls the CFG cleanup code which
4308 does not properly handle ASSERT_EXPRs. */
4309 assert_engine
.remove_range_assertions ();
4312 loop_optimizer_finalize ();
4318 const pass_data pass_data_vrp
=
4320 GIMPLE_PASS
, /* type */
4322 OPTGROUP_NONE
, /* optinfo_flags */
4323 TV_TREE_VRP
, /* tv_id */
4324 PROP_ssa
, /* properties_required */
4325 0, /* properties_provided */
4326 0, /* properties_destroyed */
4327 0, /* todo_flags_start */
4328 ( TODO_cleanup_cfg
| TODO_update_ssa
), /* todo_flags_finish */
4331 class pass_vrp
: public gimple_opt_pass
4334 pass_vrp (gcc::context
*ctxt
)
4335 : gimple_opt_pass (pass_data_vrp
, ctxt
), warn_array_bounds_p (false)
4338 /* opt_pass methods: */
4339 opt_pass
* clone () { return new pass_vrp (m_ctxt
); }
4340 void set_pass_param (unsigned int n
, bool param
)
4342 gcc_assert (n
== 0);
4343 warn_array_bounds_p
= param
;
4345 virtual bool gate (function
*) { return flag_tree_vrp
!= 0; }
4346 virtual unsigned int execute (function
*fun
)
4347 { return execute_vrp (fun
, warn_array_bounds_p
); }
4350 bool warn_array_bounds_p
;
4351 }; // class pass_vrp
4356 make_pass_vrp (gcc::context
*ctxt
)
4358 return new pass_vrp (ctxt
);
4361 // This is the dom walker for the hybrid threader. The reason this is
4362 // here, as opposed to the generic threading files, is because the
4363 // other client would be DOM, and they have their own custom walker.
4365 class hybrid_threader
: public dom_walker
4369 ~hybrid_threader ();
4371 void thread_jumps (function
*fun
)
4373 walk (fun
->cfg
->x_entry_block_ptr
);
4375 bool thread_through_all_blocks ()
4377 return m_threader
->thread_through_all_blocks (false);
4381 edge
before_dom_children (basic_block
) override
;
4382 void after_dom_children (basic_block bb
) override
;
4384 hybrid_jt_simplifier
*m_simplifier
;
4385 jump_threader
*m_threader
;
4387 gimple_ranger
*m_ranger
;
4388 path_range_query
*m_query
;
4391 hybrid_threader::hybrid_threader () : dom_walker (CDI_DOMINATORS
, REACHABLE_BLOCKS
)
4393 loop_optimizer_init (LOOPS_NORMAL
| LOOPS_HAVE_RECORDED_EXITS
);
4395 calculate_dominance_info (CDI_DOMINATORS
);
4396 mark_dfs_back_edges ();
4398 m_ranger
= new gimple_ranger
;
4399 m_query
= new path_range_query (*m_ranger
, /*resolve=*/true);
4400 m_simplifier
= new hybrid_jt_simplifier (m_ranger
, m_query
);
4401 m_state
= new hybrid_jt_state
;
4402 m_threader
= new jump_threader (m_simplifier
, m_state
);
4405 hybrid_threader::~hybrid_threader ()
4407 delete m_simplifier
;
4414 loop_optimizer_finalize ();
4418 hybrid_threader::before_dom_children (basic_block bb
)
4420 gimple_stmt_iterator gsi
;
4423 for (gsi
= gsi_start_nondebug_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
4425 gimple
*stmt
= gsi_stmt (gsi
);
4426 m_ranger
->range_of_stmt (r
, stmt
);
4432 hybrid_threader::after_dom_children (basic_block bb
)
4434 m_threader
->thread_outgoing_edges (bb
);
4438 execute_vrp_threader (function
*fun
)
4440 hybrid_threader threader
;
4441 threader
.thread_jumps (fun
);
4442 if (threader
.thread_through_all_blocks ())
4443 return (TODO_cleanup_cfg
| TODO_update_ssa
);
4449 const pass_data pass_data_vrp_threader
=
4451 GIMPLE_PASS
, /* type */
4452 "vrp-thread", /* name */
4453 OPTGROUP_NONE
, /* optinfo_flags */
4454 TV_TREE_VRP_THREADER
, /* tv_id */
4455 PROP_ssa
, /* properties_required */
4456 0, /* properties_provided */
4457 0, /* properties_destroyed */
4458 0, /* todo_flags_start */
4459 0 /* todo_flags_finish */
4462 class pass_vrp_threader
: public gimple_opt_pass
4465 pass_vrp_threader (gcc::context
*ctxt
)
4466 : gimple_opt_pass (pass_data_vrp_threader
, ctxt
)
4469 /* opt_pass methods: */
4470 opt_pass
* clone () { return new pass_vrp_threader (m_ctxt
); }
4471 virtual bool gate (function
*) { return flag_tree_vrp
!= 0; }
4472 virtual unsigned int execute (function
*fun
)
4473 { return execute_vrp_threader (fun
); }
4479 make_pass_vrp_threader (gcc::context
*ctxt
)
4481 return new pass_vrp_threader (ctxt
);