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"
25 #include "insn-codes.h"
30 #include "tree-pass.h"
32 #include "optabs-tree.h"
33 #include "gimple-pretty-print.h"
35 #include "fold-const.h"
36 #include "stor-layout.h"
39 #include "gimple-fold.h"
41 #include "gimple-iterator.h"
42 #include "gimple-walk.h"
44 #include "tree-ssa-loop-manip.h"
45 #include "tree-ssa-loop-niter.h"
46 #include "tree-ssa-loop.h"
47 #include "tree-into-ssa.h"
50 #include "tree-scalar-evolution.h"
51 #include "tree-ssa-propagate.h"
52 #include "tree-chrec.h"
53 #include "tree-ssa-threadupdate.h"
54 #include "tree-ssa-scopedtables.h"
55 #include "tree-ssa-threadedge.h"
56 #include "omp-general.h"
58 #include "case-cfn-macros.h"
59 #include "alloc-pool.h"
61 #include "tree-cfgcleanup.h"
62 #include "stringpool.h"
64 #include "vr-values.h"
67 #include "value-range-equiv.h"
68 #include "gimple-array-bounds.h"
70 /* Set of SSA names found live during the RPO traversal of the function
71 for still active basic-blocks. */
77 void set (tree
, basic_block
);
78 void clear (tree
, basic_block
);
79 void merge (basic_block dest
, basic_block src
);
80 bool live_on_block_p (tree
, basic_block
);
81 bool live_on_edge_p (tree
, edge
);
82 bool block_has_live_names_p (basic_block
);
83 void clear_block (basic_block
);
88 void init_bitmap_if_needed (basic_block
);
92 live_names::init_bitmap_if_needed (basic_block bb
)
94 unsigned i
= bb
->index
;
97 live
[i
] = sbitmap_alloc (num_ssa_names
);
98 bitmap_clear (live
[i
]);
103 live_names::block_has_live_names_p (basic_block bb
)
105 unsigned i
= bb
->index
;
106 return live
[i
] && bitmap_empty_p (live
[i
]);
110 live_names::clear_block (basic_block bb
)
112 unsigned i
= bb
->index
;
115 sbitmap_free (live
[i
]);
121 live_names::merge (basic_block dest
, basic_block src
)
123 init_bitmap_if_needed (dest
);
124 init_bitmap_if_needed (src
);
125 bitmap_ior (live
[dest
->index
], live
[dest
->index
], live
[src
->index
]);
129 live_names::set (tree name
, basic_block bb
)
131 init_bitmap_if_needed (bb
);
132 bitmap_set_bit (live
[bb
->index
], SSA_NAME_VERSION (name
));
136 live_names::clear (tree name
, basic_block bb
)
138 unsigned i
= bb
->index
;
140 bitmap_clear_bit (live
[i
], SSA_NAME_VERSION (name
));
143 live_names::live_names ()
145 num_blocks
= last_basic_block_for_fn (cfun
);
146 live
= XCNEWVEC (sbitmap
, num_blocks
);
149 live_names::~live_names ()
151 for (unsigned i
= 0; i
< num_blocks
; ++i
)
153 sbitmap_free (live
[i
]);
158 live_names::live_on_block_p (tree name
, basic_block bb
)
160 return (live
[bb
->index
]
161 && bitmap_bit_p (live
[bb
->index
], SSA_NAME_VERSION (name
)));
164 /* Return true if the SSA name NAME is live on the edge E. */
167 live_names::live_on_edge_p (tree name
, edge e
)
169 return live_on_block_p (name
, e
->dest
);
173 /* VR_TYPE describes a range with mininum value *MIN and maximum
174 value *MAX. Restrict the range to the set of values that have
175 no bits set outside NONZERO_BITS. Update *MIN and *MAX and
176 return the new range type.
178 SGN gives the sign of the values described by the range. */
180 enum value_range_kind
181 intersect_range_with_nonzero_bits (enum value_range_kind vr_type
,
182 wide_int
*min
, wide_int
*max
,
183 const wide_int
&nonzero_bits
,
186 if (vr_type
== VR_ANTI_RANGE
)
188 /* The VR_ANTI_RANGE is equivalent to the union of the ranges
189 A: [-INF, *MIN) and B: (*MAX, +INF]. First use NONZERO_BITS
190 to create an inclusive upper bound for A and an inclusive lower
192 wide_int a_max
= wi::round_down_for_mask (*min
- 1, nonzero_bits
);
193 wide_int b_min
= wi::round_up_for_mask (*max
+ 1, nonzero_bits
);
195 /* If the calculation of A_MAX wrapped, A is effectively empty
196 and A_MAX is the highest value that satisfies NONZERO_BITS.
197 Likewise if the calculation of B_MIN wrapped, B is effectively
198 empty and B_MIN is the lowest value that satisfies NONZERO_BITS. */
199 bool a_empty
= wi::ge_p (a_max
, *min
, sgn
);
200 bool b_empty
= wi::le_p (b_min
, *max
, sgn
);
202 /* If both A and B are empty, there are no valid values. */
203 if (a_empty
&& b_empty
)
206 /* If exactly one of A or B is empty, return a VR_RANGE for the
208 if (a_empty
|| b_empty
)
212 gcc_checking_assert (wi::le_p (*min
, *max
, sgn
));
216 /* Update the VR_ANTI_RANGE bounds. */
219 gcc_checking_assert (wi::le_p (*min
, *max
, sgn
));
221 /* Now check whether the excluded range includes any values that
222 satisfy NONZERO_BITS. If not, switch to a full VR_RANGE. */
223 if (wi::round_up_for_mask (*min
, nonzero_bits
) == b_min
)
225 unsigned int precision
= min
->get_precision ();
226 *min
= wi::min_value (precision
, sgn
);
227 *max
= wi::max_value (precision
, sgn
);
231 if (vr_type
== VR_RANGE
|| vr_type
== VR_VARYING
)
233 *max
= wi::round_down_for_mask (*max
, nonzero_bits
);
235 /* Check that the range contains at least one valid value. */
236 if (wi::gt_p (*min
, *max
, sgn
))
239 *min
= wi::round_up_for_mask (*min
, nonzero_bits
);
240 gcc_checking_assert (wi::le_p (*min
, *max
, sgn
));
245 /* Return true if max and min of VR are INTEGER_CST. It's not necessary
249 range_int_cst_p (const value_range
*vr
)
251 return (vr
->kind () == VR_RANGE
&& range_has_numeric_bounds_p (vr
));
254 /* Return the single symbol (an SSA_NAME) contained in T if any, or NULL_TREE
255 otherwise. We only handle additive operations and set NEG to true if the
256 symbol is negated and INV to the invariant part, if any. */
259 get_single_symbol (tree t
, bool *neg
, tree
*inv
)
267 if (TREE_CODE (t
) == PLUS_EXPR
268 || TREE_CODE (t
) == POINTER_PLUS_EXPR
269 || TREE_CODE (t
) == MINUS_EXPR
)
271 if (is_gimple_min_invariant (TREE_OPERAND (t
, 0)))
273 neg_
= (TREE_CODE (t
) == MINUS_EXPR
);
274 inv_
= TREE_OPERAND (t
, 0);
275 t
= TREE_OPERAND (t
, 1);
277 else if (is_gimple_min_invariant (TREE_OPERAND (t
, 1)))
280 inv_
= TREE_OPERAND (t
, 1);
281 t
= TREE_OPERAND (t
, 0);
292 if (TREE_CODE (t
) == NEGATE_EXPR
)
294 t
= TREE_OPERAND (t
, 0);
298 if (TREE_CODE (t
) != SSA_NAME
)
301 if (inv_
&& TREE_OVERFLOW_P (inv_
))
302 inv_
= drop_tree_overflow (inv_
);
309 /* The reverse operation: build a symbolic expression with TYPE
310 from symbol SYM, negated according to NEG, and invariant INV. */
313 build_symbolic_expr (tree type
, tree sym
, bool neg
, tree inv
)
315 const bool pointer_p
= POINTER_TYPE_P (type
);
319 t
= build1 (NEGATE_EXPR
, type
, t
);
321 if (integer_zerop (inv
))
324 return build2 (pointer_p
? POINTER_PLUS_EXPR
: PLUS_EXPR
, type
, t
, inv
);
330 -2 if those are incomparable. */
332 operand_less_p (tree val
, tree val2
)
334 /* LT is folded faster than GE and others. Inline the common case. */
335 if (TREE_CODE (val
) == INTEGER_CST
&& TREE_CODE (val2
) == INTEGER_CST
)
336 return tree_int_cst_lt (val
, val2
);
337 else if (TREE_CODE (val
) == SSA_NAME
&& TREE_CODE (val2
) == SSA_NAME
)
338 return val
== val2
? 0 : -2;
341 int cmp
= compare_values (val
, val2
);
344 else if (cmp
== 0 || cmp
== 1)
353 /* Compare two values VAL1 and VAL2. Return
355 -2 if VAL1 and VAL2 cannot be compared at compile-time,
358 +1 if VAL1 > VAL2, and
361 This is similar to tree_int_cst_compare but supports pointer values
362 and values that cannot be compared at compile time.
364 If STRICT_OVERFLOW_P is not NULL, then set *STRICT_OVERFLOW_P to
365 true if the return value is only valid if we assume that signed
366 overflow is undefined. */
369 compare_values_warnv (tree val1
, tree val2
, bool *strict_overflow_p
)
374 /* Below we rely on the fact that VAL1 and VAL2 are both pointers or
376 gcc_assert (POINTER_TYPE_P (TREE_TYPE (val1
))
377 == POINTER_TYPE_P (TREE_TYPE (val2
)));
379 /* Convert the two values into the same type. This is needed because
380 sizetype causes sign extension even for unsigned types. */
381 if (!useless_type_conversion_p (TREE_TYPE (val1
), TREE_TYPE (val2
)))
382 val2
= fold_convert (TREE_TYPE (val1
), val2
);
384 const bool overflow_undefined
385 = INTEGRAL_TYPE_P (TREE_TYPE (val1
))
386 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (val1
));
389 tree sym1
= get_single_symbol (val1
, &neg1
, &inv1
);
390 tree sym2
= get_single_symbol (val2
, &neg2
, &inv2
);
392 /* If VAL1 and VAL2 are of the form '[-]NAME [+ CST]', return -1 or +1
393 accordingly. If VAL1 and VAL2 don't use the same name, return -2. */
396 /* Both values must use the same name with the same sign. */
397 if (sym1
!= sym2
|| neg1
!= neg2
)
400 /* [-]NAME + CST == [-]NAME + CST. */
404 /* If overflow is defined we cannot simplify more. */
405 if (!overflow_undefined
)
408 if (strict_overflow_p
!= NULL
409 /* Symbolic range building sets the no-warning bit to declare
410 that overflow doesn't happen. */
411 && (!inv1
|| !warning_suppressed_p (val1
, OPT_Woverflow
))
412 && (!inv2
|| !warning_suppressed_p (val2
, OPT_Woverflow
)))
413 *strict_overflow_p
= true;
416 inv1
= build_int_cst (TREE_TYPE (val1
), 0);
418 inv2
= build_int_cst (TREE_TYPE (val2
), 0);
420 return wi::cmp (wi::to_wide (inv1
), wi::to_wide (inv2
),
421 TYPE_SIGN (TREE_TYPE (val1
)));
424 const bool cst1
= is_gimple_min_invariant (val1
);
425 const bool cst2
= is_gimple_min_invariant (val2
);
427 /* If one is of the form '[-]NAME + CST' and the other is constant, then
428 it might be possible to say something depending on the constants. */
429 if ((sym1
&& inv1
&& cst2
) || (sym2
&& inv2
&& cst1
))
431 if (!overflow_undefined
)
434 if (strict_overflow_p
!= NULL
435 /* Symbolic range building sets the no-warning bit to declare
436 that overflow doesn't happen. */
437 && (!sym1
|| !warning_suppressed_p (val1
, OPT_Woverflow
))
438 && (!sym2
|| !warning_suppressed_p (val2
, OPT_Woverflow
)))
439 *strict_overflow_p
= true;
441 const signop sgn
= TYPE_SIGN (TREE_TYPE (val1
));
442 tree cst
= cst1
? val1
: val2
;
443 tree inv
= cst1
? inv2
: inv1
;
445 /* Compute the difference between the constants. If it overflows or
446 underflows, this means that we can trivially compare the NAME with
447 it and, consequently, the two values with each other. */
448 wide_int diff
= wi::to_wide (cst
) - wi::to_wide (inv
);
449 if (wi::cmp (0, wi::to_wide (inv
), sgn
)
450 != wi::cmp (diff
, wi::to_wide (cst
), sgn
))
452 const int res
= wi::cmp (wi::to_wide (cst
), wi::to_wide (inv
), sgn
);
453 return cst1
? res
: -res
;
459 /* We cannot say anything more for non-constants. */
463 if (!POINTER_TYPE_P (TREE_TYPE (val1
)))
465 /* We cannot compare overflowed values. */
466 if (TREE_OVERFLOW (val1
) || TREE_OVERFLOW (val2
))
469 if (TREE_CODE (val1
) == INTEGER_CST
470 && TREE_CODE (val2
) == INTEGER_CST
)
471 return tree_int_cst_compare (val1
, val2
);
473 if (poly_int_tree_p (val1
) && poly_int_tree_p (val2
))
475 if (known_eq (wi::to_poly_widest (val1
),
476 wi::to_poly_widest (val2
)))
478 if (known_lt (wi::to_poly_widest (val1
),
479 wi::to_poly_widest (val2
)))
481 if (known_gt (wi::to_poly_widest (val1
),
482 wi::to_poly_widest (val2
)))
490 if (TREE_CODE (val1
) == INTEGER_CST
&& TREE_CODE (val2
) == INTEGER_CST
)
492 /* We cannot compare overflowed values. */
493 if (TREE_OVERFLOW (val1
) || TREE_OVERFLOW (val2
))
496 return tree_int_cst_compare (val1
, val2
);
499 /* First see if VAL1 and VAL2 are not the same. */
500 if (operand_equal_p (val1
, val2
, 0))
503 fold_defer_overflow_warnings ();
505 /* If VAL1 is a lower address than VAL2, return -1. */
506 tree t
= fold_binary_to_constant (LT_EXPR
, boolean_type_node
, val1
, val2
);
507 if (t
&& integer_onep (t
))
509 fold_undefer_and_ignore_overflow_warnings ();
513 /* If VAL1 is a higher address than VAL2, return +1. */
514 t
= fold_binary_to_constant (LT_EXPR
, boolean_type_node
, val2
, val1
);
515 if (t
&& integer_onep (t
))
517 fold_undefer_and_ignore_overflow_warnings ();
521 /* If VAL1 is different than VAL2, return +2. */
522 t
= fold_binary_to_constant (NE_EXPR
, boolean_type_node
, val1
, val2
);
523 fold_undefer_and_ignore_overflow_warnings ();
524 if (t
&& integer_onep (t
))
531 /* Compare values like compare_values_warnv. */
534 compare_values (tree val1
, tree val2
)
537 return compare_values_warnv (val1
, val2
, &sop
);
540 /* If BOUND will include a symbolic bound, adjust it accordingly,
541 otherwise leave it as is.
543 CODE is the original operation that combined the bounds (PLUS_EXPR
546 TYPE is the type of the original operation.
548 SYM_OPn is the symbolic for OPn if it has a symbolic.
550 NEG_OPn is TRUE if the OPn was negated. */
553 adjust_symbolic_bound (tree
&bound
, enum tree_code code
, tree type
,
554 tree sym_op0
, tree sym_op1
,
555 bool neg_op0
, bool neg_op1
)
557 bool minus_p
= (code
== MINUS_EXPR
);
558 /* If the result bound is constant, we're done; otherwise, build the
559 symbolic lower bound. */
560 if (sym_op0
== sym_op1
)
563 bound
= build_symbolic_expr (type
, sym_op0
,
567 /* We may not negate if that might introduce
568 undefined overflow. */
571 || TYPE_OVERFLOW_WRAPS (type
))
572 bound
= build_symbolic_expr (type
, sym_op1
,
573 neg_op1
^ minus_p
, bound
);
579 /* Combine OP1 and OP1, which are two parts of a bound, into one wide
580 int bound according to CODE. CODE is the operation combining the
581 bound (either a PLUS_EXPR or a MINUS_EXPR).
583 TYPE is the type of the combine operation.
585 WI is the wide int to store the result.
587 OVF is -1 if an underflow occurred, +1 if an overflow occurred or 0
588 if over/underflow occurred. */
591 combine_bound (enum tree_code code
, wide_int
&wi
, wi::overflow_type
&ovf
,
592 tree type
, tree op0
, tree op1
)
594 bool minus_p
= (code
== MINUS_EXPR
);
595 const signop sgn
= TYPE_SIGN (type
);
596 const unsigned int prec
= TYPE_PRECISION (type
);
598 /* Combine the bounds, if any. */
602 wi
= wi::sub (wi::to_wide (op0
), wi::to_wide (op1
), sgn
, &ovf
);
604 wi
= wi::add (wi::to_wide (op0
), wi::to_wide (op1
), sgn
, &ovf
);
607 wi
= wi::to_wide (op0
);
611 wi
= wi::neg (wi::to_wide (op1
), &ovf
);
613 wi
= wi::to_wide (op1
);
616 wi
= wi::shwi (0, prec
);
619 /* Given a range in [WMIN, WMAX], adjust it for possible overflow and
620 put the result in VR.
622 TYPE is the type of the range.
624 MIN_OVF and MAX_OVF indicate what type of overflow, if any,
625 occurred while originally calculating WMIN or WMAX. -1 indicates
626 underflow. +1 indicates overflow. 0 indicates neither. */
629 set_value_range_with_overflow (value_range_kind
&kind
, tree
&min
, tree
&max
,
631 const wide_int
&wmin
, const wide_int
&wmax
,
632 wi::overflow_type min_ovf
,
633 wi::overflow_type max_ovf
)
635 const signop sgn
= TYPE_SIGN (type
);
636 const unsigned int prec
= TYPE_PRECISION (type
);
638 /* For one bit precision if max < min, then the swapped
639 range covers all values. */
640 if (prec
== 1 && wi::lt_p (wmax
, wmin
, sgn
))
646 if (TYPE_OVERFLOW_WRAPS (type
))
648 /* If overflow wraps, truncate the values and adjust the
649 range kind and bounds appropriately. */
650 wide_int tmin
= wide_int::from (wmin
, prec
, sgn
);
651 wide_int tmax
= wide_int::from (wmax
, prec
, sgn
);
652 if ((min_ovf
!= wi::OVF_NONE
) == (max_ovf
!= wi::OVF_NONE
))
654 /* If the limits are swapped, we wrapped around and cover
656 if (wi::gt_p (tmin
, tmax
, sgn
))
661 /* No overflow or both overflow or underflow. The
662 range kind stays VR_RANGE. */
663 min
= wide_int_to_tree (type
, tmin
);
664 max
= wide_int_to_tree (type
, tmax
);
668 else if ((min_ovf
== wi::OVF_UNDERFLOW
&& max_ovf
== wi::OVF_NONE
)
669 || (max_ovf
== wi::OVF_OVERFLOW
&& min_ovf
== wi::OVF_NONE
))
671 /* Min underflow or max overflow. The range kind
672 changes to VR_ANTI_RANGE. */
676 if (wi::cmp (tmin
, tmax
, sgn
) < 0)
679 if (wi::cmp (tmax
, tem
, sgn
) > 0)
681 /* If the anti-range would cover nothing, drop to varying.
682 Likewise if the anti-range bounds are outside of the
684 if (covers
|| wi::cmp (tmin
, tmax
, sgn
) > 0)
689 kind
= VR_ANTI_RANGE
;
690 min
= wide_int_to_tree (type
, tmin
);
691 max
= wide_int_to_tree (type
, tmax
);
696 /* Other underflow and/or overflow, drop to VR_VARYING. */
703 /* If overflow does not wrap, saturate to the types min/max
705 wide_int type_min
= wi::min_value (prec
, sgn
);
706 wide_int type_max
= wi::max_value (prec
, sgn
);
708 if (min_ovf
== wi::OVF_UNDERFLOW
)
709 min
= wide_int_to_tree (type
, type_min
);
710 else if (min_ovf
== wi::OVF_OVERFLOW
)
711 min
= wide_int_to_tree (type
, type_max
);
713 min
= wide_int_to_tree (type
, wmin
);
715 if (max_ovf
== wi::OVF_UNDERFLOW
)
716 max
= wide_int_to_tree (type
, type_min
);
717 else if (max_ovf
== wi::OVF_OVERFLOW
)
718 max
= wide_int_to_tree (type
, type_max
);
720 max
= wide_int_to_tree (type
, wmax
);
724 /* Fold two value range's of a POINTER_PLUS_EXPR into VR. */
727 extract_range_from_pointer_plus_expr (value_range
*vr
,
730 const value_range
*vr0
,
731 const value_range
*vr1
)
733 gcc_checking_assert (POINTER_TYPE_P (expr_type
)
734 && code
== POINTER_PLUS_EXPR
);
735 /* For pointer types, we are really only interested in asserting
736 whether the expression evaluates to non-NULL.
737 With -fno-delete-null-pointer-checks we need to be more
738 conservative. As some object might reside at address 0,
739 then some offset could be added to it and the same offset
740 subtracted again and the result would be NULL.
742 static int a[12]; where &a[0] is NULL and
745 ptr will be NULL here, even when there is POINTER_PLUS_EXPR
746 where the first range doesn't include zero and the second one
747 doesn't either. As the second operand is sizetype (unsigned),
748 consider all ranges where the MSB could be set as possible
749 subtractions where the result might be NULL. */
750 if ((!range_includes_zero_p (vr0
)
751 || !range_includes_zero_p (vr1
))
752 && !TYPE_OVERFLOW_WRAPS (expr_type
)
753 && (flag_delete_null_pointer_checks
754 || (range_int_cst_p (vr1
)
755 && !tree_int_cst_sign_bit (vr1
->max ()))))
756 vr
->set_nonzero (expr_type
);
757 else if (vr0
->zero_p () && vr1
->zero_p ())
758 vr
->set_zero (expr_type
);
760 vr
->set_varying (expr_type
);
763 /* Extract range information from a PLUS/MINUS_EXPR and store the
767 extract_range_from_plus_minus_expr (value_range
*vr
,
770 const value_range
*vr0_
,
771 const value_range
*vr1_
)
773 gcc_checking_assert (code
== PLUS_EXPR
|| code
== MINUS_EXPR
);
775 value_range vr0
= *vr0_
, vr1
= *vr1_
;
776 value_range vrtem0
, vrtem1
;
778 /* Now canonicalize anti-ranges to ranges when they are not symbolic
779 and express ~[] op X as ([]' op X) U ([]'' op X). */
780 if (vr0
.kind () == VR_ANTI_RANGE
781 && ranges_from_anti_range (&vr0
, &vrtem0
, &vrtem1
))
783 extract_range_from_plus_minus_expr (vr
, code
, expr_type
, &vrtem0
, vr1_
);
784 if (!vrtem1
.undefined_p ())
787 extract_range_from_plus_minus_expr (&vrres
, code
, expr_type
,
793 /* Likewise for X op ~[]. */
794 if (vr1
.kind () == VR_ANTI_RANGE
795 && ranges_from_anti_range (&vr1
, &vrtem0
, &vrtem1
))
797 extract_range_from_plus_minus_expr (vr
, code
, expr_type
, vr0_
, &vrtem0
);
798 if (!vrtem1
.undefined_p ())
801 extract_range_from_plus_minus_expr (&vrres
, code
, expr_type
,
808 value_range_kind kind
;
809 value_range_kind vr0_kind
= vr0
.kind (), vr1_kind
= vr1
.kind ();
810 tree vr0_min
= vr0
.min (), vr0_max
= vr0
.max ();
811 tree vr1_min
= vr1
.min (), vr1_max
= vr1
.max ();
812 tree min
= NULL_TREE
, max
= NULL_TREE
;
814 /* This will normalize things such that calculating
815 [0,0] - VR_VARYING is not dropped to varying, but is
816 calculated as [MIN+1, MAX]. */
817 if (vr0
.varying_p ())
820 vr0_min
= vrp_val_min (expr_type
);
821 vr0_max
= vrp_val_max (expr_type
);
823 if (vr1
.varying_p ())
826 vr1_min
= vrp_val_min (expr_type
);
827 vr1_max
= vrp_val_max (expr_type
);
830 const bool minus_p
= (code
== MINUS_EXPR
);
831 tree min_op0
= vr0_min
;
832 tree min_op1
= minus_p
? vr1_max
: vr1_min
;
833 tree max_op0
= vr0_max
;
834 tree max_op1
= minus_p
? vr1_min
: vr1_max
;
835 tree sym_min_op0
= NULL_TREE
;
836 tree sym_min_op1
= NULL_TREE
;
837 tree sym_max_op0
= NULL_TREE
;
838 tree sym_max_op1
= NULL_TREE
;
839 bool neg_min_op0
, neg_min_op1
, neg_max_op0
, neg_max_op1
;
841 neg_min_op0
= neg_min_op1
= neg_max_op0
= neg_max_op1
= false;
843 /* If we have a PLUS or MINUS with two VR_RANGEs, either constant or
844 single-symbolic ranges, try to compute the precise resulting range,
845 but only if we know that this resulting range will also be constant
846 or single-symbolic. */
847 if (vr0_kind
== VR_RANGE
&& vr1_kind
== VR_RANGE
848 && (TREE_CODE (min_op0
) == INTEGER_CST
850 = get_single_symbol (min_op0
, &neg_min_op0
, &min_op0
)))
851 && (TREE_CODE (min_op1
) == INTEGER_CST
853 = get_single_symbol (min_op1
, &neg_min_op1
, &min_op1
)))
854 && (!(sym_min_op0
&& sym_min_op1
)
855 || (sym_min_op0
== sym_min_op1
856 && neg_min_op0
== (minus_p
? neg_min_op1
: !neg_min_op1
)))
857 && (TREE_CODE (max_op0
) == INTEGER_CST
859 = get_single_symbol (max_op0
, &neg_max_op0
, &max_op0
)))
860 && (TREE_CODE (max_op1
) == INTEGER_CST
862 = get_single_symbol (max_op1
, &neg_max_op1
, &max_op1
)))
863 && (!(sym_max_op0
&& sym_max_op1
)
864 || (sym_max_op0
== sym_max_op1
865 && neg_max_op0
== (minus_p
? neg_max_op1
: !neg_max_op1
))))
868 wi::overflow_type min_ovf
= wi::OVF_NONE
;
869 wi::overflow_type max_ovf
= wi::OVF_NONE
;
871 /* Build the bounds. */
872 combine_bound (code
, wmin
, min_ovf
, expr_type
, min_op0
, min_op1
);
873 combine_bound (code
, wmax
, max_ovf
, expr_type
, max_op0
, max_op1
);
875 /* If the resulting range will be symbolic, we need to eliminate any
876 explicit or implicit overflow introduced in the above computation
877 because compare_values could make an incorrect use of it. That's
878 why we require one of the ranges to be a singleton. */
879 if ((sym_min_op0
!= sym_min_op1
|| sym_max_op0
!= sym_max_op1
)
880 && ((bool)min_ovf
|| (bool)max_ovf
881 || (min_op0
!= max_op0
&& min_op1
!= max_op1
)))
883 vr
->set_varying (expr_type
);
887 /* Adjust the range for possible overflow. */
888 set_value_range_with_overflow (kind
, min
, max
, expr_type
,
889 wmin
, wmax
, min_ovf
, max_ovf
);
890 if (kind
== VR_VARYING
)
892 vr
->set_varying (expr_type
);
896 /* Build the symbolic bounds if needed. */
897 adjust_symbolic_bound (min
, code
, expr_type
,
898 sym_min_op0
, sym_min_op1
,
899 neg_min_op0
, neg_min_op1
);
900 adjust_symbolic_bound (max
, code
, expr_type
,
901 sym_max_op0
, sym_max_op1
,
902 neg_max_op0
, neg_max_op1
);
906 /* For other cases, for example if we have a PLUS_EXPR with two
907 VR_ANTI_RANGEs, drop to VR_VARYING. It would take more effort
908 to compute a precise range for such a case.
909 ??? General even mixed range kind operations can be expressed
910 by for example transforming ~[3, 5] + [1, 2] to range-only
911 operations and a union primitive:
912 [-INF, 2] + [1, 2] U [5, +INF] + [1, 2]
913 [-INF+1, 4] U [6, +INF(OVF)]
914 though usually the union is not exactly representable with
915 a single range or anti-range as the above is
916 [-INF+1, +INF(OVF)] intersected with ~[5, 5]
917 but one could use a scheme similar to equivalences for this. */
918 vr
->set_varying (expr_type
);
922 /* If either MIN or MAX overflowed, then set the resulting range to
925 || TREE_OVERFLOW_P (min
)
927 || TREE_OVERFLOW_P (max
))
929 vr
->set_varying (expr_type
);
933 int cmp
= compare_values (min
, max
);
934 if (cmp
== -2 || cmp
== 1)
936 /* If the new range has its limits swapped around (MIN > MAX),
937 then the operation caused one of them to wrap around, mark
938 the new range VARYING. */
939 vr
->set_varying (expr_type
);
942 vr
->set (min
, max
, kind
);
945 /* Return the range-ops handler for CODE and EXPR_TYPE. If no
946 suitable operator is found, return NULL and set VR to VARYING. */
948 static const range_operator
*
949 get_range_op_handler (value_range
*vr
,
953 const range_operator
*op
= range_op_handler (code
, expr_type
);
955 vr
->set_varying (expr_type
);
959 /* If the types passed are supported, return TRUE, otherwise set VR to
960 VARYING and return FALSE. */
963 supported_types_p (value_range
*vr
,
967 if (!value_range::supports_type_p (type0
)
968 || (type1
&& !value_range::supports_type_p (type1
)))
970 vr
->set_varying (type0
);
976 /* If any of the ranges passed are defined, return TRUE, otherwise set
977 VR to UNDEFINED and return FALSE. */
980 defined_ranges_p (value_range
*vr
,
981 const value_range
*vr0
, const value_range
*vr1
= NULL
)
983 if (vr0
->undefined_p () && (!vr1
|| vr1
->undefined_p ()))
985 vr
->set_undefined ();
992 drop_undefines_to_varying (const value_range
*vr
, tree expr_type
)
994 if (vr
->undefined_p ())
995 return value_range (expr_type
);
1000 /* If any operand is symbolic, perform a binary operation on them and
1001 return TRUE, otherwise return FALSE. */
1004 range_fold_binary_symbolics_p (value_range
*vr
,
1007 const value_range
*vr0_
,
1008 const value_range
*vr1_
)
1010 if (vr0_
->symbolic_p () || vr1_
->symbolic_p ())
1012 value_range vr0
= drop_undefines_to_varying (vr0_
, expr_type
);
1013 value_range vr1
= drop_undefines_to_varying (vr1_
, expr_type
);
1014 if ((code
== PLUS_EXPR
|| code
== MINUS_EXPR
))
1016 extract_range_from_plus_minus_expr (vr
, code
, expr_type
,
1020 if (POINTER_TYPE_P (expr_type
) && code
== POINTER_PLUS_EXPR
)
1022 extract_range_from_pointer_plus_expr (vr
, code
, expr_type
,
1026 const range_operator
*op
= get_range_op_handler (vr
, code
, expr_type
);
1027 vr0
.normalize_symbolics ();
1028 vr1
.normalize_symbolics ();
1029 return op
->fold_range (*vr
, expr_type
, vr0
, vr1
);
1034 /* If operand is symbolic, perform a unary operation on it and return
1035 TRUE, otherwise return FALSE. */
1038 range_fold_unary_symbolics_p (value_range
*vr
,
1041 const value_range
*vr0
)
1043 if (vr0
->symbolic_p ())
1045 if (code
== NEGATE_EXPR
)
1047 /* -X is simply 0 - X. */
1049 zero
.set_zero (vr0
->type ());
1050 range_fold_binary_expr (vr
, MINUS_EXPR
, expr_type
, &zero
, vr0
);
1053 if (code
== BIT_NOT_EXPR
)
1055 /* ~X is simply -1 - X. */
1056 value_range minusone
;
1057 minusone
.set (build_int_cst (vr0
->type (), -1));
1058 range_fold_binary_expr (vr
, MINUS_EXPR
, expr_type
, &minusone
, vr0
);
1061 const range_operator
*op
= get_range_op_handler (vr
, code
, expr_type
);
1062 value_range
vr0_cst (*vr0
);
1063 vr0_cst
.normalize_symbolics ();
1064 return op
->fold_range (*vr
, expr_type
, vr0_cst
, value_range (expr_type
));
1069 /* Perform a binary operation on a pair of ranges. */
1072 range_fold_binary_expr (value_range
*vr
,
1073 enum tree_code code
,
1075 const value_range
*vr0_
,
1076 const value_range
*vr1_
)
1078 if (!supported_types_p (vr
, expr_type
)
1079 || !defined_ranges_p (vr
, vr0_
, vr1_
))
1081 const range_operator
*op
= get_range_op_handler (vr
, code
, expr_type
);
1085 if (range_fold_binary_symbolics_p (vr
, code
, expr_type
, vr0_
, vr1_
))
1088 value_range
vr0 (*vr0_
);
1089 value_range
vr1 (*vr1_
);
1090 if (vr0
.undefined_p ())
1091 vr0
.set_varying (expr_type
);
1092 if (vr1
.undefined_p ())
1093 vr1
.set_varying (expr_type
);
1094 vr0
.normalize_addresses ();
1095 vr1
.normalize_addresses ();
1096 op
->fold_range (*vr
, expr_type
, vr0
, vr1
);
1099 /* Perform a unary operation on a range. */
1102 range_fold_unary_expr (value_range
*vr
,
1103 enum tree_code code
, tree expr_type
,
1104 const value_range
*vr0
,
1107 if (!supported_types_p (vr
, expr_type
, vr0_type
)
1108 || !defined_ranges_p (vr
, vr0
))
1110 const range_operator
*op
= get_range_op_handler (vr
, code
, expr_type
);
1114 if (range_fold_unary_symbolics_p (vr
, code
, expr_type
, vr0
))
1117 value_range
vr0_cst (*vr0
);
1118 vr0_cst
.normalize_addresses ();
1119 op
->fold_range (*vr
, expr_type
, vr0_cst
, value_range (expr_type
));
1122 /* If the range of values taken by OP can be inferred after STMT executes,
1123 return the comparison code (COMP_CODE_P) and value (VAL_P) that
1124 describes the inferred range. Return true if a range could be
1128 infer_value_range (gimple
*stmt
, tree op
, tree_code
*comp_code_p
, tree
*val_p
)
1131 *comp_code_p
= ERROR_MARK
;
1133 /* Do not attempt to infer anything in names that flow through
1135 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op
))
1138 /* If STMT is the last statement of a basic block with no normal
1139 successors, there is no point inferring anything about any of its
1140 operands. We would not be able to find a proper insertion point
1141 for the assertion, anyway. */
1142 if (stmt_ends_bb_p (stmt
))
1147 FOR_EACH_EDGE (e
, ei
, gimple_bb (stmt
)->succs
)
1148 if (!(e
->flags
& (EDGE_ABNORMAL
|EDGE_EH
)))
1154 if (infer_nonnull_range (stmt
, op
))
1156 *val_p
= build_int_cst (TREE_TYPE (op
), 0);
1157 *comp_code_p
= NE_EXPR
;
1164 /* Dump assert_info structure. */
1167 dump_assert_info (FILE *file
, const assert_info
&assert)
1169 fprintf (file
, "Assert for: ");
1170 print_generic_expr (file
, assert.name
);
1171 fprintf (file
, "\n\tPREDICATE: expr=[");
1172 print_generic_expr (file
, assert.expr
);
1173 fprintf (file
, "] %s ", get_tree_code_name (assert.comp_code
));
1174 fprintf (file
, "val=[");
1175 print_generic_expr (file
, assert.val
);
1176 fprintf (file
, "]\n\n");
1180 debug (const assert_info
&assert)
1182 dump_assert_info (stderr
, assert);
1185 /* Dump a vector of assert_info's. */
1188 dump_asserts_info (FILE *file
, const vec
<assert_info
> &asserts
)
1190 for (unsigned i
= 0; i
< asserts
.length (); ++i
)
1192 dump_assert_info (file
, asserts
[i
]);
1193 fprintf (file
, "\n");
1198 debug (const vec
<assert_info
> &asserts
)
1200 dump_asserts_info (stderr
, asserts
);
1203 /* Push the assert info for NAME, EXPR, COMP_CODE and VAL to ASSERTS. */
1206 add_assert_info (vec
<assert_info
> &asserts
,
1207 tree name
, tree expr
, enum tree_code comp_code
, tree val
)
1210 info
.comp_code
= comp_code
;
1212 if (TREE_OVERFLOW_P (val
))
1213 val
= drop_tree_overflow (val
);
1216 asserts
.safe_push (info
);
1217 if (dump_enabled_p ())
1218 dump_printf (MSG_NOTE
| MSG_PRIORITY_INTERNALS
,
1219 "Adding assert for %T from %T %s %T\n",
1220 name
, expr
, op_symbol_code (comp_code
), val
);
1223 /* (COND_OP0 COND_CODE COND_OP1) is a predicate which uses NAME.
1224 Extract a suitable test code and value and store them into *CODE_P and
1225 *VAL_P so the predicate is normalized to NAME *CODE_P *VAL_P.
1227 If no extraction was possible, return FALSE, otherwise return TRUE.
1229 If INVERT is true, then we invert the result stored into *CODE_P. */
1232 extract_code_and_val_from_cond_with_ops (tree name
, enum tree_code cond_code
,
1233 tree cond_op0
, tree cond_op1
,
1234 bool invert
, enum tree_code
*code_p
,
1237 enum tree_code comp_code
;
1240 /* Otherwise, we have a comparison of the form NAME COMP VAL
1241 or VAL COMP NAME. */
1242 if (name
== cond_op1
)
1244 /* If the predicate is of the form VAL COMP NAME, flip
1245 COMP around because we need to register NAME as the
1246 first operand in the predicate. */
1247 comp_code
= swap_tree_comparison (cond_code
);
1250 else if (name
== cond_op0
)
1252 /* The comparison is of the form NAME COMP VAL, so the
1253 comparison code remains unchanged. */
1254 comp_code
= cond_code
;
1260 /* Invert the comparison code as necessary. */
1262 comp_code
= invert_tree_comparison (comp_code
, 0);
1264 /* VRP only handles integral and pointer types. */
1265 if (! INTEGRAL_TYPE_P (TREE_TYPE (val
))
1266 && ! POINTER_TYPE_P (TREE_TYPE (val
)))
1269 /* Do not register always-false predicates.
1270 FIXME: this works around a limitation in fold() when dealing with
1271 enumerations. Given 'enum { N1, N2 } x;', fold will not
1272 fold 'if (x > N2)' to 'if (0)'. */
1273 if ((comp_code
== GT_EXPR
|| comp_code
== LT_EXPR
)
1274 && INTEGRAL_TYPE_P (TREE_TYPE (val
)))
1276 tree min
= TYPE_MIN_VALUE (TREE_TYPE (val
));
1277 tree max
= TYPE_MAX_VALUE (TREE_TYPE (val
));
1279 if (comp_code
== GT_EXPR
1281 || compare_values (val
, max
) == 0))
1284 if (comp_code
== LT_EXPR
1286 || compare_values (val
, min
) == 0))
1289 *code_p
= comp_code
;
1294 /* Find out smallest RES where RES > VAL && (RES & MASK) == RES, if any
1295 (otherwise return VAL). VAL and MASK must be zero-extended for
1296 precision PREC. If SGNBIT is non-zero, first xor VAL with SGNBIT
1297 (to transform signed values into unsigned) and at the end xor
1301 masked_increment (const wide_int
&val_in
, const wide_int
&mask
,
1302 const wide_int
&sgnbit
, unsigned int prec
)
1304 wide_int bit
= wi::one (prec
), res
;
1307 wide_int val
= val_in
^ sgnbit
;
1308 for (i
= 0; i
< prec
; i
++, bit
+= bit
)
1311 if ((res
& bit
) == 0)
1314 res
= wi::bit_and_not (val
+ bit
, res
);
1316 if (wi::gtu_p (res
, val
))
1317 return res
^ sgnbit
;
1319 return val
^ sgnbit
;
1322 /* Helper for overflow_comparison_p
1324 OP0 CODE OP1 is a comparison. Examine the comparison and potentially
1325 OP1's defining statement to see if it ultimately has the form
1326 OP0 CODE (OP0 PLUS INTEGER_CST)
1328 If so, return TRUE indicating this is an overflow test and store into
1329 *NEW_CST an updated constant that can be used in a narrowed range test.
1331 REVERSED indicates if the comparison was originally:
1335 This affects how we build the updated constant. */
1338 overflow_comparison_p_1 (enum tree_code code
, tree op0
, tree op1
,
1339 bool follow_assert_exprs
, bool reversed
, tree
*new_cst
)
1341 /* See if this is a relational operation between two SSA_NAMES with
1342 unsigned, overflow wrapping values. If so, check it more deeply. */
1343 if ((code
== LT_EXPR
|| code
== LE_EXPR
1344 || code
== GE_EXPR
|| code
== GT_EXPR
)
1345 && TREE_CODE (op0
) == SSA_NAME
1346 && TREE_CODE (op1
) == SSA_NAME
1347 && INTEGRAL_TYPE_P (TREE_TYPE (op0
))
1348 && TYPE_UNSIGNED (TREE_TYPE (op0
))
1349 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (op0
)))
1351 gimple
*op1_def
= SSA_NAME_DEF_STMT (op1
);
1353 /* If requested, follow any ASSERT_EXPRs backwards for OP1. */
1354 if (follow_assert_exprs
)
1356 while (gimple_assign_single_p (op1_def
)
1357 && TREE_CODE (gimple_assign_rhs1 (op1_def
)) == ASSERT_EXPR
)
1359 op1
= TREE_OPERAND (gimple_assign_rhs1 (op1_def
), 0);
1360 if (TREE_CODE (op1
) != SSA_NAME
)
1362 op1_def
= SSA_NAME_DEF_STMT (op1
);
1366 /* Now look at the defining statement of OP1 to see if it adds
1367 or subtracts a nonzero constant from another operand. */
1369 && is_gimple_assign (op1_def
)
1370 && gimple_assign_rhs_code (op1_def
) == PLUS_EXPR
1371 && TREE_CODE (gimple_assign_rhs2 (op1_def
)) == INTEGER_CST
1372 && !integer_zerop (gimple_assign_rhs2 (op1_def
)))
1374 tree target
= gimple_assign_rhs1 (op1_def
);
1376 /* If requested, follow ASSERT_EXPRs backwards for op0 looking
1377 for one where TARGET appears on the RHS. */
1378 if (follow_assert_exprs
)
1380 /* Now see if that "other operand" is op0, following the chain
1381 of ASSERT_EXPRs if necessary. */
1382 gimple
*op0_def
= SSA_NAME_DEF_STMT (op0
);
1383 while (op0
!= target
1384 && gimple_assign_single_p (op0_def
)
1385 && TREE_CODE (gimple_assign_rhs1 (op0_def
)) == ASSERT_EXPR
)
1387 op0
= TREE_OPERAND (gimple_assign_rhs1 (op0_def
), 0);
1388 if (TREE_CODE (op0
) != SSA_NAME
)
1390 op0_def
= SSA_NAME_DEF_STMT (op0
);
1394 /* If we did not find our target SSA_NAME, then this is not
1395 an overflow test. */
1399 tree type
= TREE_TYPE (op0
);
1400 wide_int max
= wi::max_value (TYPE_PRECISION (type
), UNSIGNED
);
1401 tree inc
= gimple_assign_rhs2 (op1_def
);
1403 *new_cst
= wide_int_to_tree (type
, max
+ wi::to_wide (inc
));
1405 *new_cst
= wide_int_to_tree (type
, max
- wi::to_wide (inc
));
1412 /* OP0 CODE OP1 is a comparison. Examine the comparison and potentially
1413 OP1's defining statement to see if it ultimately has the form
1414 OP0 CODE (OP0 PLUS INTEGER_CST)
1416 If so, return TRUE indicating this is an overflow test and store into
1417 *NEW_CST an updated constant that can be used in a narrowed range test.
1419 These statements are left as-is in the IL to facilitate discovery of
1420 {ADD,SUB}_OVERFLOW sequences later in the optimizer pipeline. But
1421 the alternate range representation is often useful within VRP. */
1424 overflow_comparison_p (tree_code code
, tree name
, tree val
,
1425 bool use_equiv_p
, tree
*new_cst
)
1427 if (overflow_comparison_p_1 (code
, name
, val
, use_equiv_p
, false, new_cst
))
1429 return overflow_comparison_p_1 (swap_tree_comparison (code
), val
, name
,
1430 use_equiv_p
, true, new_cst
);
1434 /* Try to register an edge assertion for SSA name NAME on edge E for
1435 the condition COND contributing to the conditional jump pointed to by BSI.
1436 Invert the condition COND if INVERT is true. */
1439 register_edge_assert_for_2 (tree name
, edge e
,
1440 enum tree_code cond_code
,
1441 tree cond_op0
, tree cond_op1
, bool invert
,
1442 vec
<assert_info
> &asserts
)
1445 enum tree_code comp_code
;
1447 if (!extract_code_and_val_from_cond_with_ops (name
, cond_code
,
1450 invert
, &comp_code
, &val
))
1453 /* Queue the assert. */
1455 if (overflow_comparison_p (comp_code
, name
, val
, false, &x
))
1457 enum tree_code new_code
= ((comp_code
== GT_EXPR
|| comp_code
== GE_EXPR
)
1458 ? GT_EXPR
: LE_EXPR
);
1459 add_assert_info (asserts
, name
, name
, new_code
, x
);
1461 add_assert_info (asserts
, name
, name
, comp_code
, val
);
1463 /* In the case of NAME <= CST and NAME being defined as
1464 NAME = (unsigned) NAME2 + CST2 we can assert NAME2 >= -CST2
1465 and NAME2 <= CST - CST2. We can do the same for NAME > CST.
1466 This catches range and anti-range tests. */
1467 if ((comp_code
== LE_EXPR
1468 || comp_code
== GT_EXPR
)
1469 && TREE_CODE (val
) == INTEGER_CST
1470 && TYPE_UNSIGNED (TREE_TYPE (val
)))
1472 gimple
*def_stmt
= SSA_NAME_DEF_STMT (name
);
1473 tree cst2
= NULL_TREE
, name2
= NULL_TREE
, name3
= NULL_TREE
;
1475 /* Extract CST2 from the (optional) addition. */
1476 if (is_gimple_assign (def_stmt
)
1477 && gimple_assign_rhs_code (def_stmt
) == PLUS_EXPR
)
1479 name2
= gimple_assign_rhs1 (def_stmt
);
1480 cst2
= gimple_assign_rhs2 (def_stmt
);
1481 if (TREE_CODE (name2
) == SSA_NAME
1482 && TREE_CODE (cst2
) == INTEGER_CST
)
1483 def_stmt
= SSA_NAME_DEF_STMT (name2
);
1486 /* Extract NAME2 from the (optional) sign-changing cast. */
1487 if (gassign
*ass
= dyn_cast
<gassign
*> (def_stmt
))
1489 if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (ass
))
1490 && ! TYPE_UNSIGNED (TREE_TYPE (gimple_assign_rhs1 (ass
)))
1491 && (TYPE_PRECISION (TREE_TYPE (gimple_assign_lhs (ass
)))
1492 == TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (ass
)))))
1493 name3
= gimple_assign_rhs1 (ass
);
1496 /* If name3 is used later, create an ASSERT_EXPR for it. */
1497 if (name3
!= NULL_TREE
1498 && TREE_CODE (name3
) == SSA_NAME
1499 && (cst2
== NULL_TREE
1500 || TREE_CODE (cst2
) == INTEGER_CST
)
1501 && INTEGRAL_TYPE_P (TREE_TYPE (name3
)))
1505 /* Build an expression for the range test. */
1506 tmp
= build1 (NOP_EXPR
, TREE_TYPE (name
), name3
);
1507 if (cst2
!= NULL_TREE
)
1508 tmp
= build2 (PLUS_EXPR
, TREE_TYPE (name
), tmp
, cst2
);
1509 add_assert_info (asserts
, name3
, tmp
, comp_code
, val
);
1512 /* If name2 is used later, create an ASSERT_EXPR for it. */
1513 if (name2
!= NULL_TREE
1514 && TREE_CODE (name2
) == SSA_NAME
1515 && TREE_CODE (cst2
) == INTEGER_CST
1516 && INTEGRAL_TYPE_P (TREE_TYPE (name2
)))
1520 /* Build an expression for the range test. */
1522 if (TREE_TYPE (name
) != TREE_TYPE (name2
))
1523 tmp
= build1 (NOP_EXPR
, TREE_TYPE (name
), tmp
);
1524 if (cst2
!= NULL_TREE
)
1525 tmp
= build2 (PLUS_EXPR
, TREE_TYPE (name
), tmp
, cst2
);
1526 add_assert_info (asserts
, name2
, tmp
, comp_code
, val
);
1530 /* In the case of post-in/decrement tests like if (i++) ... and uses
1531 of the in/decremented value on the edge the extra name we want to
1532 assert for is not on the def chain of the name compared. Instead
1533 it is in the set of use stmts.
1534 Similar cases happen for conversions that were simplified through
1535 fold_{sign_changed,widened}_comparison. */
1536 if ((comp_code
== NE_EXPR
1537 || comp_code
== EQ_EXPR
)
1538 && TREE_CODE (val
) == INTEGER_CST
)
1540 imm_use_iterator ui
;
1542 FOR_EACH_IMM_USE_STMT (use_stmt
, ui
, name
)
1544 if (!is_gimple_assign (use_stmt
))
1547 /* Cut off to use-stmts that are dominating the predecessor. */
1548 if (!dominated_by_p (CDI_DOMINATORS
, e
->src
, gimple_bb (use_stmt
)))
1551 tree name2
= gimple_assign_lhs (use_stmt
);
1552 if (TREE_CODE (name2
) != SSA_NAME
)
1555 enum tree_code code
= gimple_assign_rhs_code (use_stmt
);
1557 if (code
== PLUS_EXPR
1558 || code
== MINUS_EXPR
)
1560 cst
= gimple_assign_rhs2 (use_stmt
);
1561 if (TREE_CODE (cst
) != INTEGER_CST
)
1563 cst
= int_const_binop (code
, val
, cst
);
1565 else if (CONVERT_EXPR_CODE_P (code
))
1567 /* For truncating conversions we cannot record
1569 if (comp_code
== NE_EXPR
1570 && (TYPE_PRECISION (TREE_TYPE (name2
))
1571 < TYPE_PRECISION (TREE_TYPE (name
))))
1573 cst
= fold_convert (TREE_TYPE (name2
), val
);
1578 if (TREE_OVERFLOW_P (cst
))
1579 cst
= drop_tree_overflow (cst
);
1580 add_assert_info (asserts
, name2
, name2
, comp_code
, cst
);
1584 if (TREE_CODE_CLASS (comp_code
) == tcc_comparison
1585 && TREE_CODE (val
) == INTEGER_CST
)
1587 gimple
*def_stmt
= SSA_NAME_DEF_STMT (name
);
1588 tree name2
= NULL_TREE
, names
[2], cst2
= NULL_TREE
;
1589 tree val2
= NULL_TREE
;
1590 unsigned int prec
= TYPE_PRECISION (TREE_TYPE (val
));
1591 wide_int mask
= wi::zero (prec
);
1592 unsigned int nprec
= prec
;
1593 enum tree_code rhs_code
= ERROR_MARK
;
1595 if (is_gimple_assign (def_stmt
))
1596 rhs_code
= gimple_assign_rhs_code (def_stmt
);
1598 /* In the case of NAME != CST1 where NAME = A +- CST2 we can
1599 assert that A != CST1 -+ CST2. */
1600 if ((comp_code
== EQ_EXPR
|| comp_code
== NE_EXPR
)
1601 && (rhs_code
== PLUS_EXPR
|| rhs_code
== MINUS_EXPR
))
1603 tree op0
= gimple_assign_rhs1 (def_stmt
);
1604 tree op1
= gimple_assign_rhs2 (def_stmt
);
1605 if (TREE_CODE (op0
) == SSA_NAME
1606 && TREE_CODE (op1
) == INTEGER_CST
)
1608 enum tree_code reverse_op
= (rhs_code
== PLUS_EXPR
1609 ? MINUS_EXPR
: PLUS_EXPR
);
1610 op1
= int_const_binop (reverse_op
, val
, op1
);
1611 if (TREE_OVERFLOW (op1
))
1612 op1
= drop_tree_overflow (op1
);
1613 add_assert_info (asserts
, op0
, op0
, comp_code
, op1
);
1617 /* Add asserts for NAME cmp CST and NAME being defined
1618 as NAME = (int) NAME2. */
1619 if (!TYPE_UNSIGNED (TREE_TYPE (val
))
1620 && (comp_code
== LE_EXPR
|| comp_code
== LT_EXPR
1621 || comp_code
== GT_EXPR
|| comp_code
== GE_EXPR
)
1622 && gimple_assign_cast_p (def_stmt
))
1624 name2
= gimple_assign_rhs1 (def_stmt
);
1625 if (CONVERT_EXPR_CODE_P (rhs_code
)
1626 && TREE_CODE (name2
) == SSA_NAME
1627 && INTEGRAL_TYPE_P (TREE_TYPE (name2
))
1628 && TYPE_UNSIGNED (TREE_TYPE (name2
))
1629 && prec
== TYPE_PRECISION (TREE_TYPE (name2
))
1630 && (comp_code
== LE_EXPR
|| comp_code
== GT_EXPR
1631 || !tree_int_cst_equal (val
,
1632 TYPE_MIN_VALUE (TREE_TYPE (val
)))))
1635 enum tree_code new_comp_code
= comp_code
;
1637 cst
= fold_convert (TREE_TYPE (name2
),
1638 TYPE_MIN_VALUE (TREE_TYPE (val
)));
1639 /* Build an expression for the range test. */
1640 tmp
= build2 (PLUS_EXPR
, TREE_TYPE (name2
), name2
, cst
);
1641 cst
= fold_build2 (PLUS_EXPR
, TREE_TYPE (name2
), cst
,
1642 fold_convert (TREE_TYPE (name2
), val
));
1643 if (comp_code
== LT_EXPR
|| comp_code
== GE_EXPR
)
1645 new_comp_code
= comp_code
== LT_EXPR
? LE_EXPR
: GT_EXPR
;
1646 cst
= fold_build2 (MINUS_EXPR
, TREE_TYPE (name2
), cst
,
1647 build_int_cst (TREE_TYPE (name2
), 1));
1649 add_assert_info (asserts
, name2
, tmp
, new_comp_code
, cst
);
1653 /* Add asserts for NAME cmp CST and NAME being defined as
1654 NAME = NAME2 >> CST2.
1656 Extract CST2 from the right shift. */
1657 if (rhs_code
== RSHIFT_EXPR
)
1659 name2
= gimple_assign_rhs1 (def_stmt
);
1660 cst2
= gimple_assign_rhs2 (def_stmt
);
1661 if (TREE_CODE (name2
) == SSA_NAME
1662 && tree_fits_uhwi_p (cst2
)
1663 && INTEGRAL_TYPE_P (TREE_TYPE (name2
))
1664 && IN_RANGE (tree_to_uhwi (cst2
), 1, prec
- 1)
1665 && type_has_mode_precision_p (TREE_TYPE (val
)))
1667 mask
= wi::mask (tree_to_uhwi (cst2
), false, prec
);
1668 val2
= fold_binary (LSHIFT_EXPR
, TREE_TYPE (val
), val
, cst2
);
1671 if (val2
!= NULL_TREE
1672 && TREE_CODE (val2
) == INTEGER_CST
1673 && simple_cst_equal (fold_build2 (RSHIFT_EXPR
,
1677 enum tree_code new_comp_code
= comp_code
;
1681 if (comp_code
== EQ_EXPR
|| comp_code
== NE_EXPR
)
1683 if (!TYPE_UNSIGNED (TREE_TYPE (val
)))
1685 tree type
= build_nonstandard_integer_type (prec
, 1);
1686 tmp
= build1 (NOP_EXPR
, type
, name2
);
1687 val2
= fold_convert (type
, val2
);
1689 tmp
= fold_build2 (MINUS_EXPR
, TREE_TYPE (tmp
), tmp
, val2
);
1690 new_val
= wide_int_to_tree (TREE_TYPE (tmp
), mask
);
1691 new_comp_code
= comp_code
== EQ_EXPR
? LE_EXPR
: GT_EXPR
;
1693 else if (comp_code
== LT_EXPR
|| comp_code
== GE_EXPR
)
1696 = wi::min_value (prec
, TYPE_SIGN (TREE_TYPE (val
)));
1698 if (minval
== wi::to_wide (new_val
))
1699 new_val
= NULL_TREE
;
1704 = wi::max_value (prec
, TYPE_SIGN (TREE_TYPE (val
)));
1705 mask
|= wi::to_wide (val2
);
1706 if (wi::eq_p (mask
, maxval
))
1707 new_val
= NULL_TREE
;
1709 new_val
= wide_int_to_tree (TREE_TYPE (val2
), mask
);
1713 add_assert_info (asserts
, name2
, tmp
, new_comp_code
, new_val
);
1716 /* If we have a conversion that doesn't change the value of the source
1717 simply register the same assert for it. */
1718 if (CONVERT_EXPR_CODE_P (rhs_code
))
1721 tree rhs1
= gimple_assign_rhs1 (def_stmt
);
1722 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1
))
1723 && TREE_CODE (rhs1
) == SSA_NAME
1724 /* Make sure the relation preserves the upper/lower boundary of
1725 the range conservatively. */
1726 && (comp_code
== NE_EXPR
1727 || comp_code
== EQ_EXPR
1728 || (TYPE_SIGN (TREE_TYPE (name
))
1729 == TYPE_SIGN (TREE_TYPE (rhs1
)))
1730 || ((comp_code
== LE_EXPR
1731 || comp_code
== LT_EXPR
)
1732 && !TYPE_UNSIGNED (TREE_TYPE (rhs1
)))
1733 || ((comp_code
== GE_EXPR
1734 || comp_code
== GT_EXPR
)
1735 && TYPE_UNSIGNED (TREE_TYPE (rhs1
))))
1736 /* And the conversion does not alter the value we compare
1737 against and all values in rhs1 can be represented in
1738 the converted to type. */
1739 && int_fits_type_p (val
, TREE_TYPE (rhs1
))
1740 && ((TYPE_PRECISION (TREE_TYPE (name
))
1741 > TYPE_PRECISION (TREE_TYPE (rhs1
)))
1742 || ((get_range_query (cfun
)->range_of_expr (vr
, rhs1
)
1743 && vr
.kind () == VR_RANGE
)
1744 && wi::fits_to_tree_p
1745 (widest_int::from (vr
.lower_bound (),
1746 TYPE_SIGN (TREE_TYPE (rhs1
))),
1748 && wi::fits_to_tree_p
1749 (widest_int::from (vr
.upper_bound (),
1750 TYPE_SIGN (TREE_TYPE (rhs1
))),
1751 TREE_TYPE (name
)))))
1752 add_assert_info (asserts
, rhs1
, rhs1
,
1753 comp_code
, fold_convert (TREE_TYPE (rhs1
), val
));
1756 /* Add asserts for NAME cmp CST and NAME being defined as
1757 NAME = NAME2 & CST2.
1759 Extract CST2 from the and.
1762 NAME = (unsigned) NAME2;
1763 casts where NAME's type is unsigned and has smaller precision
1764 than NAME2's type as if it was NAME = NAME2 & MASK. */
1765 names
[0] = NULL_TREE
;
1766 names
[1] = NULL_TREE
;
1768 if (rhs_code
== BIT_AND_EXPR
1769 || (CONVERT_EXPR_CODE_P (rhs_code
)
1770 && INTEGRAL_TYPE_P (TREE_TYPE (val
))
1771 && TYPE_UNSIGNED (TREE_TYPE (val
))
1772 && TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt
)))
1775 name2
= gimple_assign_rhs1 (def_stmt
);
1776 if (rhs_code
== BIT_AND_EXPR
)
1777 cst2
= gimple_assign_rhs2 (def_stmt
);
1780 cst2
= TYPE_MAX_VALUE (TREE_TYPE (val
));
1781 nprec
= TYPE_PRECISION (TREE_TYPE (name2
));
1783 if (TREE_CODE (name2
) == SSA_NAME
1784 && INTEGRAL_TYPE_P (TREE_TYPE (name2
))
1785 && TREE_CODE (cst2
) == INTEGER_CST
1786 && !integer_zerop (cst2
)
1788 || TYPE_UNSIGNED (TREE_TYPE (val
))))
1790 gimple
*def_stmt2
= SSA_NAME_DEF_STMT (name2
);
1791 if (gimple_assign_cast_p (def_stmt2
))
1793 names
[1] = gimple_assign_rhs1 (def_stmt2
);
1794 if (!CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt2
))
1795 || TREE_CODE (names
[1]) != SSA_NAME
1796 || !INTEGRAL_TYPE_P (TREE_TYPE (names
[1]))
1797 || (TYPE_PRECISION (TREE_TYPE (name2
))
1798 != TYPE_PRECISION (TREE_TYPE (names
[1]))))
1799 names
[1] = NULL_TREE
;
1804 if (names
[0] || names
[1])
1806 wide_int minv
, maxv
, valv
, cst2v
;
1807 wide_int tem
, sgnbit
;
1808 bool valid_p
= false, valn
, cst2n
;
1809 enum tree_code ccode
= comp_code
;
1811 valv
= wide_int::from (wi::to_wide (val
), nprec
, UNSIGNED
);
1812 cst2v
= wide_int::from (wi::to_wide (cst2
), nprec
, UNSIGNED
);
1813 valn
= wi::neg_p (valv
, TYPE_SIGN (TREE_TYPE (val
)));
1814 cst2n
= wi::neg_p (cst2v
, TYPE_SIGN (TREE_TYPE (val
)));
1815 /* If CST2 doesn't have most significant bit set,
1816 but VAL is negative, we have comparison like
1817 if ((x & 0x123) > -4) (always true). Just give up. */
1821 sgnbit
= wi::set_bit_in_zero (nprec
- 1, nprec
);
1823 sgnbit
= wi::zero (nprec
);
1824 minv
= valv
& cst2v
;
1828 /* Minimum unsigned value for equality is VAL & CST2
1829 (should be equal to VAL, otherwise we probably should
1830 have folded the comparison into false) and
1831 maximum unsigned value is VAL | ~CST2. */
1832 maxv
= valv
| ~cst2v
;
1837 tem
= valv
| ~cst2v
;
1838 /* If VAL is 0, handle (X & CST2) != 0 as (X & CST2) > 0U. */
1842 sgnbit
= wi::zero (nprec
);
1845 /* If (VAL | ~CST2) is all ones, handle it as
1846 (X & CST2) < VAL. */
1851 sgnbit
= wi::zero (nprec
);
1854 if (!cst2n
&& wi::neg_p (cst2v
))
1855 sgnbit
= wi::set_bit_in_zero (nprec
- 1, nprec
);
1864 if (tem
== wi::mask (nprec
- 1, false, nprec
))
1870 sgnbit
= wi::zero (nprec
);
1875 /* Minimum unsigned value for >= if (VAL & CST2) == VAL
1876 is VAL and maximum unsigned value is ~0. For signed
1877 comparison, if CST2 doesn't have most significant bit
1878 set, handle it similarly. If CST2 has MSB set,
1879 the minimum is the same, and maximum is ~0U/2. */
1882 /* If (VAL & CST2) != VAL, X & CST2 can't be equal to
1884 minv
= masked_increment (valv
, cst2v
, sgnbit
, nprec
);
1888 maxv
= wi::mask (nprec
- (cst2n
? 1 : 0), false, nprec
);
1894 /* Find out smallest MINV where MINV > VAL
1895 && (MINV & CST2) == MINV, if any. If VAL is signed and
1896 CST2 has MSB set, compute it biased by 1 << (nprec - 1). */
1897 minv
= masked_increment (valv
, cst2v
, sgnbit
, nprec
);
1900 maxv
= wi::mask (nprec
- (cst2n
? 1 : 0), false, nprec
);
1905 /* Minimum unsigned value for <= is 0 and maximum
1906 unsigned value is VAL | ~CST2 if (VAL & CST2) == VAL.
1907 Otherwise, find smallest VAL2 where VAL2 > VAL
1908 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
1910 For signed comparison, if CST2 doesn't have most
1911 significant bit set, handle it similarly. If CST2 has
1912 MSB set, the maximum is the same and minimum is INT_MIN. */
1917 maxv
= masked_increment (valv
, cst2v
, sgnbit
, nprec
);
1929 /* Minimum unsigned value for < is 0 and maximum
1930 unsigned value is (VAL-1) | ~CST2 if (VAL & CST2) == VAL.
1931 Otherwise, find smallest VAL2 where VAL2 > VAL
1932 && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
1934 For signed comparison, if CST2 doesn't have most
1935 significant bit set, handle it similarly. If CST2 has
1936 MSB set, the maximum is the same and minimum is INT_MIN. */
1945 maxv
= masked_increment (valv
, cst2v
, sgnbit
, nprec
);
1959 && (maxv
- minv
) != -1)
1961 tree tmp
, new_val
, type
;
1964 for (i
= 0; i
< 2; i
++)
1967 wide_int maxv2
= maxv
;
1969 type
= TREE_TYPE (names
[i
]);
1970 if (!TYPE_UNSIGNED (type
))
1972 type
= build_nonstandard_integer_type (nprec
, 1);
1973 tmp
= build1 (NOP_EXPR
, type
, names
[i
]);
1977 tmp
= build2 (PLUS_EXPR
, type
, tmp
,
1978 wide_int_to_tree (type
, -minv
));
1979 maxv2
= maxv
- minv
;
1981 new_val
= wide_int_to_tree (type
, maxv2
);
1982 add_assert_info (asserts
, names
[i
], tmp
, LE_EXPR
, new_val
);
1989 /* OP is an operand of a truth value expression which is known to have
1990 a particular value. Register any asserts for OP and for any
1991 operands in OP's defining statement.
1993 If CODE is EQ_EXPR, then we want to register OP is zero (false),
1994 if CODE is NE_EXPR, then we want to register OP is nonzero (true). */
1997 register_edge_assert_for_1 (tree op
, enum tree_code code
,
1998 edge e
, vec
<assert_info
> &asserts
)
2002 enum tree_code rhs_code
;
2004 /* We only care about SSA_NAMEs. */
2005 if (TREE_CODE (op
) != SSA_NAME
)
2008 /* We know that OP will have a zero or nonzero value. */
2009 val
= build_int_cst (TREE_TYPE (op
), 0);
2010 add_assert_info (asserts
, op
, op
, code
, val
);
2012 /* Now look at how OP is set. If it's set from a comparison,
2013 a truth operation or some bit operations, then we may be able
2014 to register information about the operands of that assignment. */
2015 op_def
= SSA_NAME_DEF_STMT (op
);
2016 if (gimple_code (op_def
) != GIMPLE_ASSIGN
)
2019 rhs_code
= gimple_assign_rhs_code (op_def
);
2021 if (TREE_CODE_CLASS (rhs_code
) == tcc_comparison
)
2023 bool invert
= (code
== EQ_EXPR
? true : false);
2024 tree op0
= gimple_assign_rhs1 (op_def
);
2025 tree op1
= gimple_assign_rhs2 (op_def
);
2027 if (TREE_CODE (op0
) == SSA_NAME
)
2028 register_edge_assert_for_2 (op0
, e
, rhs_code
, op0
, op1
, invert
, asserts
);
2029 if (TREE_CODE (op1
) == SSA_NAME
)
2030 register_edge_assert_for_2 (op1
, e
, rhs_code
, op0
, op1
, invert
, asserts
);
2032 else if ((code
== NE_EXPR
2033 && gimple_assign_rhs_code (op_def
) == BIT_AND_EXPR
)
2035 && gimple_assign_rhs_code (op_def
) == BIT_IOR_EXPR
))
2037 /* Recurse on each operand. */
2038 tree op0
= gimple_assign_rhs1 (op_def
);
2039 tree op1
= gimple_assign_rhs2 (op_def
);
2040 if (TREE_CODE (op0
) == SSA_NAME
2041 && has_single_use (op0
))
2042 register_edge_assert_for_1 (op0
, code
, e
, asserts
);
2043 if (TREE_CODE (op1
) == SSA_NAME
2044 && has_single_use (op1
))
2045 register_edge_assert_for_1 (op1
, code
, e
, asserts
);
2047 else if (gimple_assign_rhs_code (op_def
) == BIT_NOT_EXPR
2048 && TYPE_PRECISION (TREE_TYPE (gimple_assign_lhs (op_def
))) == 1)
2050 /* Recurse, flipping CODE. */
2051 code
= invert_tree_comparison (code
, false);
2052 register_edge_assert_for_1 (gimple_assign_rhs1 (op_def
), code
, e
, asserts
);
2054 else if (gimple_assign_rhs_code (op_def
) == SSA_NAME
)
2056 /* Recurse through the copy. */
2057 register_edge_assert_for_1 (gimple_assign_rhs1 (op_def
), code
, e
, asserts
);
2059 else if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (op_def
)))
2061 /* Recurse through the type conversion, unless it is a narrowing
2062 conversion or conversion from non-integral type. */
2063 tree rhs
= gimple_assign_rhs1 (op_def
);
2064 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs
))
2065 && (TYPE_PRECISION (TREE_TYPE (rhs
))
2066 <= TYPE_PRECISION (TREE_TYPE (op
))))
2067 register_edge_assert_for_1 (rhs
, code
, e
, asserts
);
2071 /* Check if comparison
2072 NAME COND_OP INTEGER_CST
2074 (X & 11...100..0) COND_OP XX...X00...0
2075 Such comparison can yield assertions like
2078 in case of COND_OP being EQ_EXPR or
2081 in case of NE_EXPR. */
2084 is_masked_range_test (tree name
, tree valt
, enum tree_code cond_code
,
2085 tree
*new_name
, tree
*low
, enum tree_code
*low_code
,
2086 tree
*high
, enum tree_code
*high_code
)
2088 gimple
*def_stmt
= SSA_NAME_DEF_STMT (name
);
2090 if (!is_gimple_assign (def_stmt
)
2091 || gimple_assign_rhs_code (def_stmt
) != BIT_AND_EXPR
)
2094 tree t
= gimple_assign_rhs1 (def_stmt
);
2095 tree maskt
= gimple_assign_rhs2 (def_stmt
);
2096 if (TREE_CODE (t
) != SSA_NAME
|| TREE_CODE (maskt
) != INTEGER_CST
)
2099 wi::tree_to_wide_ref mask
= wi::to_wide (maskt
);
2100 wide_int inv_mask
= ~mask
;
2101 /* Must have been removed by now so don't bother optimizing. */
2102 if (mask
== 0 || inv_mask
== 0)
2105 /* Assume VALT is INTEGER_CST. */
2106 wi::tree_to_wide_ref val
= wi::to_wide (valt
);
2108 if ((inv_mask
& (inv_mask
+ 1)) != 0
2109 || (val
& mask
) != val
)
2112 bool is_range
= cond_code
== EQ_EXPR
;
2114 tree type
= TREE_TYPE (t
);
2115 wide_int min
= wi::min_value (type
),
2116 max
= wi::max_value (type
);
2120 *low_code
= val
== min
? ERROR_MARK
: GE_EXPR
;
2121 *high_code
= val
== max
? ERROR_MARK
: LE_EXPR
;
2125 /* We can still generate assertion if one of alternatives
2126 is known to always be false. */
2129 *low_code
= (enum tree_code
) 0;
2130 *high_code
= GT_EXPR
;
2132 else if ((val
| inv_mask
) == max
)
2134 *low_code
= LT_EXPR
;
2135 *high_code
= (enum tree_code
) 0;
2142 *low
= wide_int_to_tree (type
, val
);
2143 *high
= wide_int_to_tree (type
, val
| inv_mask
);
2148 /* Try to register an edge assertion for SSA name NAME on edge E for
2149 the condition COND contributing to the conditional jump pointed to by
2153 register_edge_assert_for (tree name
, edge e
,
2154 enum tree_code cond_code
, tree cond_op0
,
2155 tree cond_op1
, vec
<assert_info
> &asserts
)
2158 enum tree_code comp_code
;
2159 bool is_else_edge
= (e
->flags
& EDGE_FALSE_VALUE
) != 0;
2161 /* Do not attempt to infer anything in names that flow through
2163 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name
))
2166 if (!extract_code_and_val_from_cond_with_ops (name
, cond_code
,
2172 /* Register ASSERT_EXPRs for name. */
2173 register_edge_assert_for_2 (name
, e
, cond_code
, cond_op0
,
2174 cond_op1
, is_else_edge
, asserts
);
2177 /* If COND is effectively an equality test of an SSA_NAME against
2178 the value zero or one, then we may be able to assert values
2179 for SSA_NAMEs which flow into COND. */
2181 /* In the case of NAME == 1 or NAME != 0, for BIT_AND_EXPR defining
2182 statement of NAME we can assert both operands of the BIT_AND_EXPR
2183 have nonzero value. */
2184 if ((comp_code
== EQ_EXPR
&& integer_onep (val
))
2185 || (comp_code
== NE_EXPR
&& integer_zerop (val
)))
2187 gimple
*def_stmt
= SSA_NAME_DEF_STMT (name
);
2189 if (is_gimple_assign (def_stmt
)
2190 && gimple_assign_rhs_code (def_stmt
) == BIT_AND_EXPR
)
2192 tree op0
= gimple_assign_rhs1 (def_stmt
);
2193 tree op1
= gimple_assign_rhs2 (def_stmt
);
2194 register_edge_assert_for_1 (op0
, NE_EXPR
, e
, asserts
);
2195 register_edge_assert_for_1 (op1
, NE_EXPR
, e
, asserts
);
2197 else if (is_gimple_assign (def_stmt
)
2198 && (TREE_CODE_CLASS (gimple_assign_rhs_code (def_stmt
))
2200 register_edge_assert_for_1 (name
, NE_EXPR
, e
, asserts
);
2203 /* In the case of NAME == 0 or NAME != 1, for BIT_IOR_EXPR defining
2204 statement of NAME we can assert both operands of the BIT_IOR_EXPR
2206 if ((comp_code
== EQ_EXPR
&& integer_zerop (val
))
2207 || (comp_code
== NE_EXPR
2208 && integer_onep (val
)
2209 && TYPE_PRECISION (TREE_TYPE (name
)) == 1))
2211 gimple
*def_stmt
= SSA_NAME_DEF_STMT (name
);
2213 /* For BIT_IOR_EXPR only if NAME == 0 both operands have
2214 necessarily zero value, or if type-precision is one. */
2215 if (is_gimple_assign (def_stmt
)
2216 && gimple_assign_rhs_code (def_stmt
) == BIT_IOR_EXPR
)
2218 tree op0
= gimple_assign_rhs1 (def_stmt
);
2219 tree op1
= gimple_assign_rhs2 (def_stmt
);
2220 register_edge_assert_for_1 (op0
, EQ_EXPR
, e
, asserts
);
2221 register_edge_assert_for_1 (op1
, EQ_EXPR
, e
, asserts
);
2223 else if (is_gimple_assign (def_stmt
)
2224 && (TREE_CODE_CLASS (gimple_assign_rhs_code (def_stmt
))
2226 register_edge_assert_for_1 (name
, EQ_EXPR
, e
, asserts
);
2229 /* Sometimes we can infer ranges from (NAME & MASK) == VALUE. */
2230 if ((comp_code
== EQ_EXPR
|| comp_code
== NE_EXPR
)
2231 && TREE_CODE (val
) == INTEGER_CST
)
2233 enum tree_code low_code
, high_code
;
2235 if (is_masked_range_test (name
, val
, comp_code
, &name
, &low
,
2236 &low_code
, &high
, &high_code
))
2238 if (low_code
!= ERROR_MARK
)
2239 register_edge_assert_for_2 (name
, e
, low_code
, name
,
2240 low
, /*invert*/false, asserts
);
2241 if (high_code
!= ERROR_MARK
)
2242 register_edge_assert_for_2 (name
, e
, high_code
, name
,
2243 high
, /*invert*/false, asserts
);
2255 __builtin_unreachable ();
2257 x_5 = ASSERT_EXPR <x_3, ...>;
2258 If x_3 has no other immediate uses (checked by caller),
2259 var is the x_3 var from ASSERT_EXPR, we can clear low 5 bits
2260 from the non-zero bitmask. */
2263 maybe_set_nonzero_bits (edge e
, tree var
)
2265 basic_block cond_bb
= e
->src
;
2266 gimple
*stmt
= last_stmt (cond_bb
);
2270 || gimple_code (stmt
) != GIMPLE_COND
2271 || gimple_cond_code (stmt
) != ((e
->flags
& EDGE_TRUE_VALUE
)
2272 ? EQ_EXPR
: NE_EXPR
)
2273 || TREE_CODE (gimple_cond_lhs (stmt
)) != SSA_NAME
2274 || !integer_zerop (gimple_cond_rhs (stmt
)))
2277 stmt
= SSA_NAME_DEF_STMT (gimple_cond_lhs (stmt
));
2278 if (!is_gimple_assign (stmt
)
2279 || gimple_assign_rhs_code (stmt
) != BIT_AND_EXPR
2280 || TREE_CODE (gimple_assign_rhs2 (stmt
)) != INTEGER_CST
)
2282 if (gimple_assign_rhs1 (stmt
) != var
)
2286 if (TREE_CODE (gimple_assign_rhs1 (stmt
)) != SSA_NAME
)
2288 stmt2
= SSA_NAME_DEF_STMT (gimple_assign_rhs1 (stmt
));
2289 if (!gimple_assign_cast_p (stmt2
)
2290 || gimple_assign_rhs1 (stmt2
) != var
2291 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (stmt2
))
2292 || (TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (stmt
)))
2293 != TYPE_PRECISION (TREE_TYPE (var
))))
2296 cst
= gimple_assign_rhs2 (stmt
);
2297 set_nonzero_bits (var
, wi::bit_and_not (get_nonzero_bits (var
),
2298 wi::to_wide (cst
)));
2301 /* Return true if STMT is interesting for VRP. */
2304 stmt_interesting_for_vrp (gimple
*stmt
)
2306 if (gimple_code (stmt
) == GIMPLE_PHI
)
2308 tree res
= gimple_phi_result (stmt
);
2309 return (!virtual_operand_p (res
)
2310 && (INTEGRAL_TYPE_P (TREE_TYPE (res
))
2311 || POINTER_TYPE_P (TREE_TYPE (res
))));
2313 else if (is_gimple_assign (stmt
) || is_gimple_call (stmt
))
2315 tree lhs
= gimple_get_lhs (stmt
);
2317 /* In general, assignments with virtual operands are not useful
2318 for deriving ranges, with the obvious exception of calls to
2319 builtin functions. */
2320 if (lhs
&& TREE_CODE (lhs
) == SSA_NAME
2321 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs
))
2322 || POINTER_TYPE_P (TREE_TYPE (lhs
)))
2323 && (is_gimple_call (stmt
)
2324 || !gimple_vuse (stmt
)))
2326 else if (is_gimple_call (stmt
) && gimple_call_internal_p (stmt
))
2327 switch (gimple_call_internal_fn (stmt
))
2329 case IFN_ADD_OVERFLOW
:
2330 case IFN_SUB_OVERFLOW
:
2331 case IFN_MUL_OVERFLOW
:
2332 case IFN_ATOMIC_COMPARE_EXCHANGE
:
2333 /* These internal calls return _Complex integer type,
2334 but are interesting to VRP nevertheless. */
2335 if (lhs
&& TREE_CODE (lhs
) == SSA_NAME
)
2342 else if (gimple_code (stmt
) == GIMPLE_COND
2343 || gimple_code (stmt
) == GIMPLE_SWITCH
)
2350 /* Return the LHS of any ASSERT_EXPR where OP appears as the first
2351 argument to the ASSERT_EXPR and in which the ASSERT_EXPR dominates
2352 BB. If no such ASSERT_EXPR is found, return OP. */
2355 lhs_of_dominating_assert (tree op
, basic_block bb
, gimple
*stmt
)
2357 imm_use_iterator imm_iter
;
2359 use_operand_p use_p
;
2361 if (TREE_CODE (op
) == SSA_NAME
)
2363 FOR_EACH_IMM_USE_FAST (use_p
, imm_iter
, op
)
2365 use_stmt
= USE_STMT (use_p
);
2366 if (use_stmt
!= stmt
2367 && gimple_assign_single_p (use_stmt
)
2368 && TREE_CODE (gimple_assign_rhs1 (use_stmt
)) == ASSERT_EXPR
2369 && TREE_OPERAND (gimple_assign_rhs1 (use_stmt
), 0) == op
2370 && dominated_by_p (CDI_DOMINATORS
, bb
, gimple_bb (use_stmt
)))
2371 return gimple_assign_lhs (use_stmt
);
2377 /* Searches the case label vector VEC for the index *IDX of the CASE_LABEL
2378 that includes the value VAL. The search is restricted to the range
2379 [START_IDX, n - 1] where n is the size of VEC.
2381 If there is a CASE_LABEL for VAL, its index is placed in IDX and true is
2384 If there is no CASE_LABEL for VAL and there is one that is larger than VAL,
2385 it is placed in IDX and false is returned.
2387 If VAL is larger than any CASE_LABEL, n is placed on IDX and false is
2391 find_case_label_index (gswitch
*stmt
, size_t start_idx
, tree val
, size_t *idx
)
2393 size_t n
= gimple_switch_num_labels (stmt
);
2396 /* Find case label for minimum of the value range or the next one.
2397 At each iteration we are searching in [low, high - 1]. */
2399 for (low
= start_idx
, high
= n
; high
!= low
; )
2403 /* Note that i != high, so we never ask for n. */
2404 size_t i
= (high
+ low
) / 2;
2405 t
= gimple_switch_label (stmt
, i
);
2407 /* Cache the result of comparing CASE_LOW and val. */
2408 cmp
= tree_int_cst_compare (CASE_LOW (t
), val
);
2412 /* Ranges cannot be empty. */
2421 if (CASE_HIGH (t
) != NULL
2422 && tree_int_cst_compare (CASE_HIGH (t
), val
) >= 0)
2434 /* Searches the case label vector VEC for the range of CASE_LABELs that is used
2435 for values between MIN and MAX. The first index is placed in MIN_IDX. The
2436 last index is placed in MAX_IDX. If the range of CASE_LABELs is empty
2437 then MAX_IDX < MIN_IDX.
2438 Returns true if the default label is not needed. */
2441 find_case_label_range (gswitch
*stmt
, tree min
, tree max
, size_t *min_idx
,
2445 bool min_take_default
= !find_case_label_index (stmt
, 1, min
, &i
);
2446 bool max_take_default
= !find_case_label_index (stmt
, i
, max
, &j
);
2450 && max_take_default
)
2452 /* Only the default case label reached.
2453 Return an empty range. */
2460 bool take_default
= min_take_default
|| max_take_default
;
2464 if (max_take_default
)
2467 /* If the case label range is continuous, we do not need
2468 the default case label. Verify that. */
2469 high
= CASE_LOW (gimple_switch_label (stmt
, i
));
2470 if (CASE_HIGH (gimple_switch_label (stmt
, i
)))
2471 high
= CASE_HIGH (gimple_switch_label (stmt
, i
));
2472 for (k
= i
+ 1; k
<= j
; ++k
)
2474 low
= CASE_LOW (gimple_switch_label (stmt
, k
));
2475 if (!integer_onep (int_const_binop (MINUS_EXPR
, low
, high
)))
2477 take_default
= true;
2481 if (CASE_HIGH (gimple_switch_label (stmt
, k
)))
2482 high
= CASE_HIGH (gimple_switch_label (stmt
, k
));
2487 return !take_default
;
2491 /* Given a SWITCH_STMT, return the case label that encompasses the
2492 known possible values for the switch operand. RANGE_OF_OP is a
2493 range for the known values of the switch operand. */
2496 find_case_label_range (gswitch
*switch_stmt
, const irange
*range_of_op
)
2498 if (range_of_op
->undefined_p ()
2499 || range_of_op
->varying_p ()
2500 || range_of_op
->symbolic_p ())
2504 tree op
= gimple_switch_index (switch_stmt
);
2505 tree type
= TREE_TYPE (op
);
2506 tree tmin
= wide_int_to_tree (type
, range_of_op
->lower_bound ());
2507 tree tmax
= wide_int_to_tree (type
, range_of_op
->upper_bound ());
2508 find_case_label_range (switch_stmt
, tmin
, tmax
, &i
, &j
);
2511 /* Look for exactly one label that encompasses the range of
2513 tree label
= gimple_switch_label (switch_stmt
, i
);
2515 = CASE_HIGH (label
) ? CASE_HIGH (label
) : CASE_LOW (label
);
2516 int_range_max
label_range (CASE_LOW (label
), case_high
);
2517 if (!types_compatible_p (label_range
.type (), range_of_op
->type ()))
2518 range_cast (label_range
, range_of_op
->type ());
2519 label_range
.intersect (range_of_op
);
2520 if (label_range
== *range_of_op
)
2525 /* If there are no labels at all, take the default. */
2526 return gimple_switch_label (switch_stmt
, 0);
2530 /* Otherwise, there are various labels that can encompass
2531 the range of operand. In which case, see if the range of
2532 the operand is entirely *outside* the bounds of all the
2533 (non-default) case labels. If so, take the default. */
2534 unsigned n
= gimple_switch_num_labels (switch_stmt
);
2535 tree min_label
= gimple_switch_label (switch_stmt
, 1);
2536 tree max_label
= gimple_switch_label (switch_stmt
, n
- 1);
2537 tree case_high
= CASE_HIGH (max_label
);
2539 case_high
= CASE_LOW (max_label
);
2540 int_range_max
label_range (CASE_LOW (min_label
), case_high
);
2541 if (!types_compatible_p (label_range
.type (), range_of_op
->type ()))
2542 range_cast (label_range
, range_of_op
->type ());
2543 label_range
.intersect (range_of_op
);
2544 if (label_range
.undefined_p ())
2545 return gimple_switch_label (switch_stmt
, 0);
2556 /* Location information for ASSERT_EXPRs. Each instance of this
2557 structure describes an ASSERT_EXPR for an SSA name. Since a single
2558 SSA name may have more than one assertion associated with it, these
2559 locations are kept in a linked list attached to the corresponding
2563 /* Basic block where the assertion would be inserted. */
2566 /* Some assertions need to be inserted on an edge (e.g., assertions
2567 generated by COND_EXPRs). In those cases, BB will be NULL. */
2570 /* Pointer to the statement that generated this assertion. */
2571 gimple_stmt_iterator si
;
2573 /* Predicate code for the ASSERT_EXPR. Must be COMPARISON_CLASS_P. */
2574 enum tree_code comp_code
;
2576 /* Value being compared against. */
2579 /* Expression to compare. */
2582 /* Next node in the linked list. */
2586 /* Class to traverse the flowgraph looking for conditional jumps to
2587 insert ASSERT_EXPR range expressions. These range expressions are
2588 meant to provide information to optimizations that need to reason
2589 in terms of value ranges. They will not be expanded into RTL. */
2594 vrp_asserts (struct function
*fn
) : fun (fn
) { }
2596 void insert_range_assertions ();
2598 /* Convert range assertion expressions into the implied copies and
2599 copy propagate away the copies. */
2600 void remove_range_assertions ();
2602 /* Dump all the registered assertions for all the names to FILE. */
2605 /* Dump all the registered assertions for NAME to FILE. */
2606 void dump (FILE *file
, tree name
);
2608 /* Dump all the registered assertions for NAME to stderr. */
2609 void debug (tree name
)
2611 dump (stderr
, name
);
2614 /* Dump all the registered assertions for all the names to stderr. */
2621 /* Set of SSA names found live during the RPO traversal of the function
2622 for still active basic-blocks. */
2625 /* Function to work on. */
2626 struct function
*fun
;
2628 /* If bit I is present, it means that SSA name N_i has a list of
2629 assertions that should be inserted in the IL. */
2630 bitmap need_assert_for
;
2632 /* Array of locations lists where to insert assertions. ASSERTS_FOR[I]
2633 holds a list of ASSERT_LOCUS_T nodes that describe where
2634 ASSERT_EXPRs for SSA name N_I should be inserted. */
2635 assert_locus
**asserts_for
;
2637 /* Finish found ASSERTS for E and register them at GSI. */
2638 void finish_register_edge_assert_for (edge e
, gimple_stmt_iterator gsi
,
2639 vec
<assert_info
> &asserts
);
2641 /* Determine whether the outgoing edges of BB should receive an
2642 ASSERT_EXPR for each of the operands of BB's LAST statement. The
2643 last statement of BB must be a SWITCH_EXPR.
2645 If any of the sub-graphs rooted at BB have an interesting use of
2646 the predicate operands, an assert location node is added to the
2647 list of assertions for the corresponding operands. */
2648 void find_switch_asserts (basic_block bb
, gswitch
*last
);
2650 /* Do an RPO walk over the function computing SSA name liveness
2651 on-the-fly and deciding on assert expressions to insert. */
2652 void find_assert_locations ();
2654 /* Traverse all the statements in block BB looking for statements that
2655 may generate useful assertions for the SSA names in their operand.
2656 See method implementation comentary for more information. */
2657 void find_assert_locations_in_bb (basic_block bb
);
2659 /* Determine whether the outgoing edges of BB should receive an
2660 ASSERT_EXPR for each of the operands of BB's LAST statement.
2661 The last statement of BB must be a COND_EXPR.
2663 If any of the sub-graphs rooted at BB have an interesting use of
2664 the predicate operands, an assert location node is added to the
2665 list of assertions for the corresponding operands. */
2666 void find_conditional_asserts (basic_block bb
, gcond
*last
);
2668 /* Process all the insertions registered for every name N_i registered
2669 in NEED_ASSERT_FOR. The list of assertions to be inserted are
2670 found in ASSERTS_FOR[i]. */
2671 void process_assert_insertions ();
2673 /* If NAME doesn't have an ASSERT_EXPR registered for asserting
2674 'EXPR COMP_CODE VAL' at a location that dominates block BB or
2675 E->DEST, then register this location as a possible insertion point
2676 for ASSERT_EXPR <NAME, EXPR COMP_CODE VAL>.
2678 BB, E and SI provide the exact insertion point for the new
2679 ASSERT_EXPR. If BB is NULL, then the ASSERT_EXPR is to be inserted
2680 on edge E. Otherwise, if E is NULL, the ASSERT_EXPR is inserted on
2681 BB. If SI points to a COND_EXPR or a SWITCH_EXPR statement, then E
2682 must not be NULL. */
2683 void register_new_assert_for (tree name
, tree expr
,
2684 enum tree_code comp_code
,
2685 tree val
, basic_block bb
,
2686 edge e
, gimple_stmt_iterator si
);
2688 /* Given a COND_EXPR COND of the form 'V OP W', and an SSA name V,
2689 create a new SSA name N and return the assertion assignment
2690 'N = ASSERT_EXPR <V, V OP W>'. */
2691 gimple
*build_assert_expr_for (tree cond
, tree v
);
2693 /* Create an ASSERT_EXPR for NAME and insert it in the location
2694 indicated by LOC. Return true if we made any edge insertions. */
2695 bool process_assert_insertions_for (tree name
, assert_locus
*loc
);
2697 /* Qsort callback for sorting assert locations. */
2698 template <bool stable
> static int compare_assert_loc (const void *,
2701 /* Return false if EXPR is a predicate expression involving floating
2703 bool fp_predicate (gimple
*stmt
)
2705 GIMPLE_CHECK (stmt
, GIMPLE_COND
);
2706 return FLOAT_TYPE_P (TREE_TYPE (gimple_cond_lhs (stmt
)));
2709 bool all_imm_uses_in_stmt_or_feed_cond (tree var
, gimple
*stmt
,
2710 basic_block cond_bb
);
2712 static int compare_case_labels (const void *, const void *);
2715 /* Given a COND_EXPR COND of the form 'V OP W', and an SSA name V,
2716 create a new SSA name N and return the assertion assignment
2717 'N = ASSERT_EXPR <V, V OP W>'. */
2720 vrp_asserts::build_assert_expr_for (tree cond
, tree v
)
2725 gcc_assert (TREE_CODE (v
) == SSA_NAME
2726 && COMPARISON_CLASS_P (cond
));
2728 a
= build2 (ASSERT_EXPR
, TREE_TYPE (v
), v
, cond
);
2729 assertion
= gimple_build_assign (NULL_TREE
, a
);
2731 /* The new ASSERT_EXPR, creates a new SSA name that replaces the
2732 operand of the ASSERT_EXPR. Create it so the new name and the old one
2733 are registered in the replacement table so that we can fix the SSA web
2734 after adding all the ASSERT_EXPRs. */
2735 tree new_def
= create_new_def_for (v
, assertion
, NULL
);
2736 /* Make sure we preserve abnormalness throughout an ASSERT_EXPR chain
2737 given we have to be able to fully propagate those out to re-create
2738 valid SSA when removing the asserts. */
2739 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (v
))
2740 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (new_def
) = 1;
2745 /* Dump all the registered assertions for NAME to FILE. */
2748 vrp_asserts::dump (FILE *file
, tree name
)
2752 fprintf (file
, "Assertions to be inserted for ");
2753 print_generic_expr (file
, name
);
2754 fprintf (file
, "\n");
2756 loc
= asserts_for
[SSA_NAME_VERSION (name
)];
2759 fprintf (file
, "\t");
2760 print_gimple_stmt (file
, gsi_stmt (loc
->si
), 0);
2761 fprintf (file
, "\n\tBB #%d", loc
->bb
->index
);
2764 fprintf (file
, "\n\tEDGE %d->%d", loc
->e
->src
->index
,
2765 loc
->e
->dest
->index
);
2766 dump_edge_info (file
, loc
->e
, dump_flags
, 0);
2768 fprintf (file
, "\n\tPREDICATE: ");
2769 print_generic_expr (file
, loc
->expr
);
2770 fprintf (file
, " %s ", get_tree_code_name (loc
->comp_code
));
2771 print_generic_expr (file
, loc
->val
);
2772 fprintf (file
, "\n\n");
2776 fprintf (file
, "\n");
2779 /* Dump all the registered assertions for all the names to FILE. */
2782 vrp_asserts::dump (FILE *file
)
2787 fprintf (file
, "\nASSERT_EXPRs to be inserted\n\n");
2788 EXECUTE_IF_SET_IN_BITMAP (need_assert_for
, 0, i
, bi
)
2789 dump (file
, ssa_name (i
));
2790 fprintf (file
, "\n");
2793 /* If NAME doesn't have an ASSERT_EXPR registered for asserting
2794 'EXPR COMP_CODE VAL' at a location that dominates block BB or
2795 E->DEST, then register this location as a possible insertion point
2796 for ASSERT_EXPR <NAME, EXPR COMP_CODE VAL>.
2798 BB, E and SI provide the exact insertion point for the new
2799 ASSERT_EXPR. If BB is NULL, then the ASSERT_EXPR is to be inserted
2800 on edge E. Otherwise, if E is NULL, the ASSERT_EXPR is inserted on
2801 BB. If SI points to a COND_EXPR or a SWITCH_EXPR statement, then E
2802 must not be NULL. */
2805 vrp_asserts::register_new_assert_for (tree name
, tree expr
,
2806 enum tree_code comp_code
,
2810 gimple_stmt_iterator si
)
2812 assert_locus
*n
, *loc
, *last_loc
;
2813 basic_block dest_bb
;
2815 gcc_checking_assert (bb
== NULL
|| e
== NULL
);
2818 gcc_checking_assert (gimple_code (gsi_stmt (si
)) != GIMPLE_COND
2819 && gimple_code (gsi_stmt (si
)) != GIMPLE_SWITCH
);
2821 /* Never build an assert comparing against an integer constant with
2822 TREE_OVERFLOW set. This confuses our undefined overflow warning
2824 if (TREE_OVERFLOW_P (val
))
2825 val
= drop_tree_overflow (val
);
2827 /* The new assertion A will be inserted at BB or E. We need to
2828 determine if the new location is dominated by a previously
2829 registered location for A. If we are doing an edge insertion,
2830 assume that A will be inserted at E->DEST. Note that this is not
2833 If E is a critical edge, it will be split. But even if E is
2834 split, the new block will dominate the same set of blocks that
2837 The reverse, however, is not true, blocks dominated by E->DEST
2838 will not be dominated by the new block created to split E. So,
2839 if the insertion location is on a critical edge, we will not use
2840 the new location to move another assertion previously registered
2841 at a block dominated by E->DEST. */
2842 dest_bb
= (bb
) ? bb
: e
->dest
;
2844 /* If NAME already has an ASSERT_EXPR registered for COMP_CODE and
2845 VAL at a block dominating DEST_BB, then we don't need to insert a new
2846 one. Similarly, if the same assertion already exists at a block
2847 dominated by DEST_BB and the new location is not on a critical
2848 edge, then update the existing location for the assertion (i.e.,
2849 move the assertion up in the dominance tree).
2851 Note, this is implemented as a simple linked list because there
2852 should not be more than a handful of assertions registered per
2853 name. If this becomes a performance problem, a table hashed by
2854 COMP_CODE and VAL could be implemented. */
2855 loc
= asserts_for
[SSA_NAME_VERSION (name
)];
2859 if (loc
->comp_code
== comp_code
2861 || operand_equal_p (loc
->val
, val
, 0))
2862 && (loc
->expr
== expr
2863 || operand_equal_p (loc
->expr
, expr
, 0)))
2865 /* If E is not a critical edge and DEST_BB
2866 dominates the existing location for the assertion, move
2867 the assertion up in the dominance tree by updating its
2868 location information. */
2869 if ((e
== NULL
|| !EDGE_CRITICAL_P (e
))
2870 && dominated_by_p (CDI_DOMINATORS
, loc
->bb
, dest_bb
))
2879 /* Update the last node of the list and move to the next one. */
2884 /* If we didn't find an assertion already registered for
2885 NAME COMP_CODE VAL, add a new one at the end of the list of
2886 assertions associated with NAME. */
2887 n
= XNEW (struct assert_locus
);
2891 n
->comp_code
= comp_code
;
2899 asserts_for
[SSA_NAME_VERSION (name
)] = n
;
2901 bitmap_set_bit (need_assert_for
, SSA_NAME_VERSION (name
));
2904 /* Finish found ASSERTS for E and register them at GSI. */
2907 vrp_asserts::finish_register_edge_assert_for (edge e
,
2908 gimple_stmt_iterator gsi
,
2909 vec
<assert_info
> &asserts
)
2911 for (unsigned i
= 0; i
< asserts
.length (); ++i
)
2912 /* Only register an ASSERT_EXPR if NAME was found in the sub-graph
2913 reachable from E. */
2914 if (live
.live_on_edge_p (asserts
[i
].name
, e
))
2915 register_new_assert_for (asserts
[i
].name
, asserts
[i
].expr
,
2916 asserts
[i
].comp_code
, asserts
[i
].val
,
2920 /* Determine whether the outgoing edges of BB should receive an
2921 ASSERT_EXPR for each of the operands of BB's LAST statement.
2922 The last statement of BB must be a COND_EXPR.
2924 If any of the sub-graphs rooted at BB have an interesting use of
2925 the predicate operands, an assert location node is added to the
2926 list of assertions for the corresponding operands. */
2929 vrp_asserts::find_conditional_asserts (basic_block bb
, gcond
*last
)
2931 gimple_stmt_iterator bsi
;
2937 bsi
= gsi_for_stmt (last
);
2939 /* Look for uses of the operands in each of the sub-graphs
2940 rooted at BB. We need to check each of the outgoing edges
2941 separately, so that we know what kind of ASSERT_EXPR to
2943 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
2948 /* Register the necessary assertions for each operand in the
2949 conditional predicate. */
2950 auto_vec
<assert_info
, 8> asserts
;
2951 FOR_EACH_SSA_TREE_OPERAND (op
, last
, iter
, SSA_OP_USE
)
2952 register_edge_assert_for (op
, e
,
2953 gimple_cond_code (last
),
2954 gimple_cond_lhs (last
),
2955 gimple_cond_rhs (last
), asserts
);
2956 finish_register_edge_assert_for (e
, bsi
, asserts
);
2960 /* Compare two case labels sorting first by the destination bb index
2961 and then by the case value. */
2964 vrp_asserts::compare_case_labels (const void *p1
, const void *p2
)
2966 const struct case_info
*ci1
= (const struct case_info
*) p1
;
2967 const struct case_info
*ci2
= (const struct case_info
*) p2
;
2968 int idx1
= ci1
->bb
->index
;
2969 int idx2
= ci2
->bb
->index
;
2973 else if (idx1
== idx2
)
2975 /* Make sure the default label is first in a group. */
2976 if (!CASE_LOW (ci1
->expr
))
2978 else if (!CASE_LOW (ci2
->expr
))
2981 return tree_int_cst_compare (CASE_LOW (ci1
->expr
),
2982 CASE_LOW (ci2
->expr
));
2988 /* Determine whether the outgoing edges of BB should receive an
2989 ASSERT_EXPR for each of the operands of BB's LAST statement.
2990 The last statement of BB must be a SWITCH_EXPR.
2992 If any of the sub-graphs rooted at BB have an interesting use of
2993 the predicate operands, an assert location node is added to the
2994 list of assertions for the corresponding operands. */
2997 vrp_asserts::find_switch_asserts (basic_block bb
, gswitch
*last
)
2999 gimple_stmt_iterator bsi
;
3002 struct case_info
*ci
;
3003 size_t n
= gimple_switch_num_labels (last
);
3004 #if GCC_VERSION >= 4000
3007 /* Work around GCC 3.4 bug (PR 37086). */
3008 volatile unsigned int idx
;
3011 bsi
= gsi_for_stmt (last
);
3012 op
= gimple_switch_index (last
);
3013 if (TREE_CODE (op
) != SSA_NAME
)
3016 /* Build a vector of case labels sorted by destination label. */
3017 ci
= XNEWVEC (struct case_info
, n
);
3018 for (idx
= 0; idx
< n
; ++idx
)
3020 ci
[idx
].expr
= gimple_switch_label (last
, idx
);
3021 ci
[idx
].bb
= label_to_block (fun
, CASE_LABEL (ci
[idx
].expr
));
3023 edge default_edge
= find_edge (bb
, ci
[0].bb
);
3024 qsort (ci
, n
, sizeof (struct case_info
), compare_case_labels
);
3026 for (idx
= 0; idx
< n
; ++idx
)
3029 tree cl
= ci
[idx
].expr
;
3030 basic_block cbb
= ci
[idx
].bb
;
3032 min
= CASE_LOW (cl
);
3033 max
= CASE_HIGH (cl
);
3035 /* If there are multiple case labels with the same destination
3036 we need to combine them to a single value range for the edge. */
3037 if (idx
+ 1 < n
&& cbb
== ci
[idx
+ 1].bb
)
3039 /* Skip labels until the last of the group. */
3042 } while (idx
< n
&& cbb
== ci
[idx
].bb
);
3045 /* Pick up the maximum of the case label range. */
3046 if (CASE_HIGH (ci
[idx
].expr
))
3047 max
= CASE_HIGH (ci
[idx
].expr
);
3049 max
= CASE_LOW (ci
[idx
].expr
);
3052 /* Can't extract a useful assertion out of a range that includes the
3054 if (min
== NULL_TREE
)
3057 /* Find the edge to register the assert expr on. */
3058 e
= find_edge (bb
, cbb
);
3060 /* Register the necessary assertions for the operand in the
3062 auto_vec
<assert_info
, 8> asserts
;
3063 register_edge_assert_for (op
, e
,
3064 max
? GE_EXPR
: EQ_EXPR
,
3065 op
, fold_convert (TREE_TYPE (op
), min
),
3068 register_edge_assert_for (op
, e
, LE_EXPR
, op
,
3069 fold_convert (TREE_TYPE (op
), max
),
3071 finish_register_edge_assert_for (e
, bsi
, asserts
);
3076 if (!live
.live_on_edge_p (op
, default_edge
))
3079 /* Now register along the default label assertions that correspond to the
3080 anti-range of each label. */
3081 int insertion_limit
= param_max_vrp_switch_assertions
;
3082 if (insertion_limit
== 0)
3085 /* We can't do this if the default case shares a label with another case. */
3086 tree default_cl
= gimple_switch_default_label (last
);
3087 for (idx
= 1; idx
< n
; idx
++)
3090 tree cl
= gimple_switch_label (last
, idx
);
3091 if (CASE_LABEL (cl
) == CASE_LABEL (default_cl
))
3094 min
= CASE_LOW (cl
);
3095 max
= CASE_HIGH (cl
);
3097 /* Combine contiguous case ranges to reduce the number of assertions
3099 for (idx
= idx
+ 1; idx
< n
; idx
++)
3101 tree next_min
, next_max
;
3102 tree next_cl
= gimple_switch_label (last
, idx
);
3103 if (CASE_LABEL (next_cl
) == CASE_LABEL (default_cl
))
3106 next_min
= CASE_LOW (next_cl
);
3107 next_max
= CASE_HIGH (next_cl
);
3109 wide_int difference
= (wi::to_wide (next_min
)
3110 - wi::to_wide (max
? max
: min
));
3111 if (wi::eq_p (difference
, 1))
3112 max
= next_max
? next_max
: next_min
;
3118 if (max
== NULL_TREE
)
3120 /* Register the assertion OP != MIN. */
3121 auto_vec
<assert_info
, 8> asserts
;
3122 min
= fold_convert (TREE_TYPE (op
), min
);
3123 register_edge_assert_for (op
, default_edge
, NE_EXPR
, op
, min
,
3125 finish_register_edge_assert_for (default_edge
, bsi
, asserts
);
3129 /* Register the assertion (unsigned)OP - MIN > (MAX - MIN),
3130 which will give OP the anti-range ~[MIN,MAX]. */
3131 tree uop
= fold_convert (unsigned_type_for (TREE_TYPE (op
)), op
);
3132 min
= fold_convert (TREE_TYPE (uop
), min
);
3133 max
= fold_convert (TREE_TYPE (uop
), max
);
3135 tree lhs
= fold_build2 (MINUS_EXPR
, TREE_TYPE (uop
), uop
, min
);
3136 tree rhs
= int_const_binop (MINUS_EXPR
, max
, min
);
3137 register_new_assert_for (op
, lhs
, GT_EXPR
, rhs
,
3138 NULL
, default_edge
, bsi
);
3141 if (--insertion_limit
== 0)
3146 /* Traverse all the statements in block BB looking for statements that
3147 may generate useful assertions for the SSA names in their operand.
3148 If a statement produces a useful assertion A for name N_i, then the
3149 list of assertions already generated for N_i is scanned to
3150 determine if A is actually needed.
3152 If N_i already had the assertion A at a location dominating the
3153 current location, then nothing needs to be done. Otherwise, the
3154 new location for A is recorded instead.
3156 1- For every statement S in BB, all the variables used by S are
3157 added to bitmap FOUND_IN_SUBGRAPH.
3159 2- If statement S uses an operand N in a way that exposes a known
3160 value range for N, then if N was not already generated by an
3161 ASSERT_EXPR, create a new assert location for N. For instance,
3162 if N is a pointer and the statement dereferences it, we can
3163 assume that N is not NULL.
3165 3- COND_EXPRs are a special case of #2. We can derive range
3166 information from the predicate but need to insert different
3167 ASSERT_EXPRs for each of the sub-graphs rooted at the
3168 conditional block. If the last statement of BB is a conditional
3169 expression of the form 'X op Y', then
3171 a) Remove X and Y from the set FOUND_IN_SUBGRAPH.
3173 b) If the conditional is the only entry point to the sub-graph
3174 corresponding to the THEN_CLAUSE, recurse into it. On
3175 return, if X and/or Y are marked in FOUND_IN_SUBGRAPH, then
3176 an ASSERT_EXPR is added for the corresponding variable.
3178 c) Repeat step (b) on the ELSE_CLAUSE.
3180 d) Mark X and Y in FOUND_IN_SUBGRAPH.
3189 In this case, an assertion on the THEN clause is useful to
3190 determine that 'a' is always 9 on that edge. However, an assertion
3191 on the ELSE clause would be unnecessary.
3193 4- If BB does not end in a conditional expression, then we recurse
3194 into BB's dominator children.
3196 At the end of the recursive traversal, every SSA name will have a
3197 list of locations where ASSERT_EXPRs should be added. When a new
3198 location for name N is found, it is registered by calling
3199 register_new_assert_for. That function keeps track of all the
3200 registered assertions to prevent adding unnecessary assertions.
3201 For instance, if a pointer P_4 is dereferenced more than once in a
3202 dominator tree, only the location dominating all the dereference of
3203 P_4 will receive an ASSERT_EXPR. */
3206 vrp_asserts::find_assert_locations_in_bb (basic_block bb
)
3210 last
= last_stmt (bb
);
3212 /* If BB's last statement is a conditional statement involving integer
3213 operands, determine if we need to add ASSERT_EXPRs. */
3215 && gimple_code (last
) == GIMPLE_COND
3216 && !fp_predicate (last
)
3217 && !ZERO_SSA_OPERANDS (last
, SSA_OP_USE
))
3218 find_conditional_asserts (bb
, as_a
<gcond
*> (last
));
3220 /* If BB's last statement is a switch statement involving integer
3221 operands, determine if we need to add ASSERT_EXPRs. */
3223 && gimple_code (last
) == GIMPLE_SWITCH
3224 && !ZERO_SSA_OPERANDS (last
, SSA_OP_USE
))
3225 find_switch_asserts (bb
, as_a
<gswitch
*> (last
));
3227 /* Traverse all the statements in BB marking used names and looking
3228 for statements that may infer assertions for their used operands. */
3229 for (gimple_stmt_iterator si
= gsi_last_bb (bb
); !gsi_end_p (si
);
3236 stmt
= gsi_stmt (si
);
3238 if (is_gimple_debug (stmt
))
3241 /* See if we can derive an assertion for any of STMT's operands. */
3242 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, i
, SSA_OP_USE
)
3245 enum tree_code comp_code
;
3247 /* If op is not live beyond this stmt, do not bother to insert
3249 if (!live
.live_on_block_p (op
, bb
))
3252 /* If OP is used in such a way that we can infer a value
3253 range for it, and we don't find a previous assertion for
3254 it, create a new assertion location node for OP. */
3255 if (infer_value_range (stmt
, op
, &comp_code
, &value
))
3257 /* If we are able to infer a nonzero value range for OP,
3258 then walk backwards through the use-def chain to see if OP
3259 was set via a typecast.
3261 If so, then we can also infer a nonzero value range
3262 for the operand of the NOP_EXPR. */
3263 if (comp_code
== NE_EXPR
&& integer_zerop (value
))
3266 gimple
*def_stmt
= SSA_NAME_DEF_STMT (t
);
3268 while (is_gimple_assign (def_stmt
)
3269 && CONVERT_EXPR_CODE_P
3270 (gimple_assign_rhs_code (def_stmt
))
3272 (gimple_assign_rhs1 (def_stmt
)) == SSA_NAME
3274 (TREE_TYPE (gimple_assign_rhs1 (def_stmt
))))
3276 t
= gimple_assign_rhs1 (def_stmt
);
3277 def_stmt
= SSA_NAME_DEF_STMT (t
);
3279 /* Note we want to register the assert for the
3280 operand of the NOP_EXPR after SI, not after the
3282 if (live
.live_on_block_p (t
, bb
))
3283 register_new_assert_for (t
, t
, comp_code
, value
,
3288 register_new_assert_for (op
, op
, comp_code
, value
, bb
, NULL
, si
);
3293 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, i
, SSA_OP_USE
)
3295 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, i
, SSA_OP_DEF
)
3296 live
.clear (op
, bb
);
3299 /* Traverse all PHI nodes in BB, updating live. */
3300 for (gphi_iterator si
= gsi_start_phis (bb
); !gsi_end_p (si
);
3303 use_operand_p arg_p
;
3305 gphi
*phi
= si
.phi ();
3306 tree res
= gimple_phi_result (phi
);
3308 if (virtual_operand_p (res
))
3311 FOR_EACH_PHI_ARG (arg_p
, phi
, i
, SSA_OP_USE
)
3313 tree arg
= USE_FROM_PTR (arg_p
);
3314 if (TREE_CODE (arg
) == SSA_NAME
)
3318 live
.clear (res
, bb
);
3322 /* Do an RPO walk over the function computing SSA name liveness
3323 on-the-fly and deciding on assert expressions to insert. */
3326 vrp_asserts::find_assert_locations (void)
3328 int *rpo
= XNEWVEC (int, last_basic_block_for_fn (fun
));
3329 int *bb_rpo
= XNEWVEC (int, last_basic_block_for_fn (fun
));
3330 int *last_rpo
= XCNEWVEC (int, last_basic_block_for_fn (fun
));
3333 rpo_cnt
= pre_and_rev_post_order_compute (NULL
, rpo
, false);
3334 for (i
= 0; i
< rpo_cnt
; ++i
)
3337 /* Pre-seed loop latch liveness from loop header PHI nodes. Due to
3338 the order we compute liveness and insert asserts we otherwise
3339 fail to insert asserts into the loop latch. */
3340 for (auto loop
: loops_list (cfun
, 0))
3342 i
= loop
->latch
->index
;
3343 unsigned int j
= single_succ_edge (loop
->latch
)->dest_idx
;
3344 for (gphi_iterator gsi
= gsi_start_phis (loop
->header
);
3345 !gsi_end_p (gsi
); gsi_next (&gsi
))
3347 gphi
*phi
= gsi
.phi ();
3348 if (virtual_operand_p (gimple_phi_result (phi
)))
3350 tree arg
= gimple_phi_arg_def (phi
, j
);
3351 if (TREE_CODE (arg
) == SSA_NAME
)
3352 live
.set (arg
, loop
->latch
);
3356 for (i
= rpo_cnt
- 1; i
>= 0; --i
)
3358 basic_block bb
= BASIC_BLOCK_FOR_FN (fun
, rpo
[i
]);
3362 /* Process BB and update the live information with uses in
3364 find_assert_locations_in_bb (bb
);
3366 /* Merge liveness into the predecessor blocks and free it. */
3367 if (!live
.block_has_live_names_p (bb
))
3370 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
3372 int pred
= e
->src
->index
;
3373 if ((e
->flags
& EDGE_DFS_BACK
) || pred
== ENTRY_BLOCK
)
3376 live
.merge (e
->src
, bb
);
3378 if (bb_rpo
[pred
] < pred_rpo
)
3379 pred_rpo
= bb_rpo
[pred
];
3382 /* Record the RPO number of the last visited block that needs
3383 live information from this block. */
3384 last_rpo
[rpo
[i
]] = pred_rpo
;
3387 live
.clear_block (bb
);
3389 /* We can free all successors live bitmaps if all their
3390 predecessors have been visited already. */
3391 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
3392 if (last_rpo
[e
->dest
->index
] == i
)
3393 live
.clear_block (e
->dest
);
3397 XDELETEVEC (bb_rpo
);
3398 XDELETEVEC (last_rpo
);
3401 /* Create an ASSERT_EXPR for NAME and insert it in the location
3402 indicated by LOC. Return true if we made any edge insertions. */
3405 vrp_asserts::process_assert_insertions_for (tree name
, assert_locus
*loc
)
3407 /* Build the comparison expression NAME_i COMP_CODE VAL. */
3410 gimple
*assert_stmt
;
3414 /* If we have X <=> X do not insert an assert expr for that. */
3415 if (loc
->expr
== loc
->val
)
3418 cond
= build2 (loc
->comp_code
, boolean_type_node
, loc
->expr
, loc
->val
);
3419 assert_stmt
= build_assert_expr_for (cond
, name
);
3422 /* We have been asked to insert the assertion on an edge. This
3423 is used only by COND_EXPR and SWITCH_EXPR assertions. */
3424 gcc_checking_assert (gimple_code (gsi_stmt (loc
->si
)) == GIMPLE_COND
3425 || (gimple_code (gsi_stmt (loc
->si
))
3428 gsi_insert_on_edge (loc
->e
, assert_stmt
);
3432 /* If the stmt iterator points at the end then this is an insertion
3433 at the beginning of a block. */
3434 if (gsi_end_p (loc
->si
))
3436 gimple_stmt_iterator si
= gsi_after_labels (loc
->bb
);
3437 gsi_insert_before (&si
, assert_stmt
, GSI_SAME_STMT
);
3441 /* Otherwise, we can insert right after LOC->SI iff the
3442 statement must not be the last statement in the block. */
3443 stmt
= gsi_stmt (loc
->si
);
3444 if (!stmt_ends_bb_p (stmt
))
3446 gsi_insert_after (&loc
->si
, assert_stmt
, GSI_SAME_STMT
);
3450 /* If STMT must be the last statement in BB, we can only insert new
3451 assertions on the non-abnormal edge out of BB. Note that since
3452 STMT is not control flow, there may only be one non-abnormal/eh edge
3454 FOR_EACH_EDGE (e
, ei
, loc
->bb
->succs
)
3455 if (!(e
->flags
& (EDGE_ABNORMAL
|EDGE_EH
)))
3457 gsi_insert_on_edge (e
, assert_stmt
);
3464 /* Qsort helper for sorting assert locations. If stable is true, don't
3465 use iterative_hash_expr because it can be unstable for -fcompare-debug,
3466 on the other side some pointers might be NULL. */
3468 template <bool stable
>
3470 vrp_asserts::compare_assert_loc (const void *pa
, const void *pb
)
3472 assert_locus
* const a
= *(assert_locus
* const *)pa
;
3473 assert_locus
* const b
= *(assert_locus
* const *)pb
;
3475 /* If stable, some asserts might be optimized away already, sort
3485 if (a
->e
== NULL
&& b
->e
!= NULL
)
3487 else if (a
->e
!= NULL
&& b
->e
== NULL
)
3490 /* After the above checks, we know that (a->e == NULL) == (b->e == NULL),
3491 no need to test both a->e and b->e. */
3493 /* Sort after destination index. */
3496 else if (a
->e
->dest
->index
> b
->e
->dest
->index
)
3498 else if (a
->e
->dest
->index
< b
->e
->dest
->index
)
3501 /* Sort after comp_code. */
3502 if (a
->comp_code
> b
->comp_code
)
3504 else if (a
->comp_code
< b
->comp_code
)
3509 /* E.g. if a->val is ADDR_EXPR of a VAR_DECL, iterative_hash_expr
3510 uses DECL_UID of the VAR_DECL, so sorting might differ between
3511 -g and -g0. When doing the removal of redundant assert exprs
3512 and commonization to successors, this does not matter, but for
3513 the final sort needs to be stable. */
3521 ha
= iterative_hash_expr (a
->expr
, iterative_hash_expr (a
->val
, 0));
3522 hb
= iterative_hash_expr (b
->expr
, iterative_hash_expr (b
->val
, 0));
3525 /* Break the tie using hashing and source/bb index. */
3527 return (a
->e
!= NULL
3528 ? a
->e
->src
->index
- b
->e
->src
->index
3529 : a
->bb
->index
- b
->bb
->index
);
3530 return ha
> hb
? 1 : -1;
3533 /* Process all the insertions registered for every name N_i registered
3534 in NEED_ASSERT_FOR. The list of assertions to be inserted are
3535 found in ASSERTS_FOR[i]. */
3538 vrp_asserts::process_assert_insertions ()
3542 bool update_edges_p
= false;
3543 int num_asserts
= 0;
3545 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3548 EXECUTE_IF_SET_IN_BITMAP (need_assert_for
, 0, i
, bi
)
3550 assert_locus
*loc
= asserts_for
[i
];
3553 auto_vec
<assert_locus
*, 16> asserts
;
3554 for (; loc
; loc
= loc
->next
)
3555 asserts
.safe_push (loc
);
3556 asserts
.qsort (compare_assert_loc
<false>);
3558 /* Push down common asserts to successors and remove redundant ones. */
3560 assert_locus
*common
= NULL
;
3561 unsigned commonj
= 0;
3562 for (unsigned j
= 0; j
< asserts
.length (); ++j
)
3568 || loc
->e
->dest
!= common
->e
->dest
3569 || loc
->comp_code
!= common
->comp_code
3570 || ! operand_equal_p (loc
->val
, common
->val
, 0)
3571 || ! operand_equal_p (loc
->expr
, common
->expr
, 0))
3577 else if (loc
->e
== asserts
[j
-1]->e
)
3579 /* Remove duplicate asserts. */
3580 if (commonj
== j
- 1)
3585 free (asserts
[j
-1]);
3586 asserts
[j
-1] = NULL
;
3591 if (EDGE_COUNT (common
->e
->dest
->preds
) == ecnt
)
3593 /* We have the same assertion on all incoming edges of a BB.
3594 Insert it at the beginning of that block. */
3595 loc
->bb
= loc
->e
->dest
;
3597 loc
->si
= gsi_none ();
3599 /* Clear asserts commoned. */
3600 for (; commonj
!= j
; ++commonj
)
3601 if (asserts
[commonj
])
3603 free (asserts
[commonj
]);
3604 asserts
[commonj
] = NULL
;
3610 /* The asserts vector sorting above might be unstable for
3611 -fcompare-debug, sort again to ensure a stable sort. */
3612 asserts
.qsort (compare_assert_loc
<true>);
3613 for (unsigned j
= 0; j
< asserts
.length (); ++j
)
3618 update_edges_p
|= process_assert_insertions_for (ssa_name (i
), loc
);
3625 gsi_commit_edge_inserts ();
3627 statistics_counter_event (fun
, "Number of ASSERT_EXPR expressions inserted",
3631 /* Traverse the flowgraph looking for conditional jumps to insert range
3632 expressions. These range expressions are meant to provide information
3633 to optimizations that need to reason in terms of value ranges. They
3634 will not be expanded into RTL. For instance, given:
3643 this pass will transform the code into:
3649 x = ASSERT_EXPR <x, x < y>
3654 y = ASSERT_EXPR <y, x >= y>
3658 The idea is that once copy and constant propagation have run, other
3659 optimizations will be able to determine what ranges of values can 'x'
3660 take in different paths of the code, simply by checking the reaching
3661 definition of 'x'. */
3664 vrp_asserts::insert_range_assertions (void)
3666 need_assert_for
= BITMAP_ALLOC (NULL
);
3667 asserts_for
= XCNEWVEC (assert_locus
*, num_ssa_names
);
3669 calculate_dominance_info (CDI_DOMINATORS
);
3671 find_assert_locations ();
3672 if (!bitmap_empty_p (need_assert_for
))
3674 process_assert_insertions ();
3675 update_ssa (TODO_update_ssa_no_phi
);
3678 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3680 fprintf (dump_file
, "\nSSA form after inserting ASSERT_EXPRs\n");
3681 dump_function_to_file (current_function_decl
, dump_file
, dump_flags
);
3685 BITMAP_FREE (need_assert_for
);
3688 /* Return true if all imm uses of VAR are either in STMT, or
3689 feed (optionally through a chain of single imm uses) GIMPLE_COND
3690 in basic block COND_BB. */
3693 vrp_asserts::all_imm_uses_in_stmt_or_feed_cond (tree var
,
3695 basic_block cond_bb
)
3697 use_operand_p use_p
, use2_p
;
3698 imm_use_iterator iter
;
3700 FOR_EACH_IMM_USE_FAST (use_p
, iter
, var
)
3701 if (USE_STMT (use_p
) != stmt
)
3703 gimple
*use_stmt
= USE_STMT (use_p
), *use_stmt2
;
3704 if (is_gimple_debug (use_stmt
))
3706 while (is_gimple_assign (use_stmt
)
3707 && TREE_CODE (gimple_assign_lhs (use_stmt
)) == SSA_NAME
3708 && single_imm_use (gimple_assign_lhs (use_stmt
),
3709 &use2_p
, &use_stmt2
))
3710 use_stmt
= use_stmt2
;
3711 if (gimple_code (use_stmt
) != GIMPLE_COND
3712 || gimple_bb (use_stmt
) != cond_bb
)
3718 /* Convert range assertion expressions into the implied copies and
3719 copy propagate away the copies. Doing the trivial copy propagation
3720 here avoids the need to run the full copy propagation pass after
3723 FIXME, this will eventually lead to copy propagation removing the
3724 names that had useful range information attached to them. For
3725 instance, if we had the assertion N_i = ASSERT_EXPR <N_j, N_j > 3>,
3726 then N_i will have the range [3, +INF].
3728 However, by converting the assertion into the implied copy
3729 operation N_i = N_j, we will then copy-propagate N_j into the uses
3730 of N_i and lose the range information. We may want to hold on to
3731 ASSERT_EXPRs a little while longer as the ranges could be used in
3732 things like jump threading.
3734 The problem with keeping ASSERT_EXPRs around is that passes after
3735 VRP need to handle them appropriately.
3737 Another approach would be to make the range information a first
3738 class property of the SSA_NAME so that it can be queried from
3739 any pass. This is made somewhat more complex by the need for
3740 multiple ranges to be associated with one SSA_NAME. */
3743 vrp_asserts::remove_range_assertions ()
3746 gimple_stmt_iterator si
;
3747 /* 1 if looking at ASSERT_EXPRs immediately at the beginning of
3748 a basic block preceeded by GIMPLE_COND branching to it and
3749 __builtin_trap, -1 if not yet checked, 0 otherwise. */
3752 /* Note that the BSI iterator bump happens at the bottom of the
3753 loop and no bump is necessary if we're removing the statement
3754 referenced by the current BSI. */
3755 FOR_EACH_BB_FN (bb
, fun
)
3756 for (si
= gsi_after_labels (bb
), is_unreachable
= -1; !gsi_end_p (si
);)
3758 gimple
*stmt
= gsi_stmt (si
);
3760 if (is_gimple_assign (stmt
)
3761 && gimple_assign_rhs_code (stmt
) == ASSERT_EXPR
)
3763 tree lhs
= gimple_assign_lhs (stmt
);
3764 tree rhs
= gimple_assign_rhs1 (stmt
);
3767 var
= ASSERT_EXPR_VAR (rhs
);
3769 if (TREE_CODE (var
) == SSA_NAME
3770 && !POINTER_TYPE_P (TREE_TYPE (lhs
))
3771 && SSA_NAME_RANGE_INFO (lhs
))
3773 if (is_unreachable
== -1)
3776 if (single_pred_p (bb
)
3777 && assert_unreachable_fallthru_edge_p
3778 (single_pred_edge (bb
)))
3782 if (x_7 >= 10 && x_7 < 20)
3783 __builtin_unreachable ();
3784 x_8 = ASSERT_EXPR <x_7, ...>;
3785 if the only uses of x_7 are in the ASSERT_EXPR and
3786 in the condition. In that case, we can copy the
3787 range info from x_8 computed in this pass also
3790 && all_imm_uses_in_stmt_or_feed_cond (var
, stmt
,
3793 set_range_info (var
, SSA_NAME_RANGE_TYPE (lhs
),
3794 SSA_NAME_RANGE_INFO (lhs
)->get_min (),
3795 SSA_NAME_RANGE_INFO (lhs
)->get_max ());
3796 maybe_set_nonzero_bits (single_pred_edge (bb
), var
);
3800 /* Propagate the RHS into every use of the LHS. For SSA names
3801 also propagate abnormals as it merely restores the original
3802 IL in this case (an replace_uses_by would assert). */
3803 if (TREE_CODE (var
) == SSA_NAME
)
3805 imm_use_iterator iter
;
3806 use_operand_p use_p
;
3808 FOR_EACH_IMM_USE_STMT (use_stmt
, iter
, lhs
)
3809 FOR_EACH_IMM_USE_ON_STMT (use_p
, iter
)
3810 SET_USE (use_p
, var
);
3813 replace_uses_by (lhs
, var
);
3815 /* And finally, remove the copy, it is not needed. */
3816 gsi_remove (&si
, true);
3817 release_defs (stmt
);
3821 if (!is_gimple_debug (gsi_stmt (si
)))
3828 class vrp_prop
: public ssa_propagation_engine
3831 vrp_prop (vr_values
*v
)
3832 : ssa_propagation_engine (),
3835 void initialize (struct function
*);
3839 enum ssa_prop_result
visit_stmt (gimple
*, edge
*, tree
*) FINAL OVERRIDE
;
3840 enum ssa_prop_result
visit_phi (gphi
*) FINAL OVERRIDE
;
3842 struct function
*fun
;
3843 vr_values
*m_vr_values
;
3846 /* Initialization required by ssa_propagate engine. */
3849 vrp_prop::initialize (struct function
*fn
)
3854 FOR_EACH_BB_FN (bb
, fun
)
3856 for (gphi_iterator si
= gsi_start_phis (bb
); !gsi_end_p (si
);
3859 gphi
*phi
= si
.phi ();
3860 if (!stmt_interesting_for_vrp (phi
))
3862 tree lhs
= PHI_RESULT (phi
);
3863 m_vr_values
->set_def_to_varying (lhs
);
3864 prop_set_simulate_again (phi
, false);
3867 prop_set_simulate_again (phi
, true);
3870 for (gimple_stmt_iterator si
= gsi_start_bb (bb
); !gsi_end_p (si
);
3873 gimple
*stmt
= gsi_stmt (si
);
3875 /* If the statement is a control insn, then we do not
3876 want to avoid simulating the statement once. Failure
3877 to do so means that those edges will never get added. */
3878 if (stmt_ends_bb_p (stmt
))
3879 prop_set_simulate_again (stmt
, true);
3880 else if (!stmt_interesting_for_vrp (stmt
))
3882 m_vr_values
->set_defs_to_varying (stmt
);
3883 prop_set_simulate_again (stmt
, false);
3886 prop_set_simulate_again (stmt
, true);
3891 /* Evaluate statement STMT. If the statement produces a useful range,
3892 return SSA_PROP_INTERESTING and record the SSA name with the
3893 interesting range into *OUTPUT_P.
3895 If STMT is a conditional branch and we can determine its truth
3896 value, the taken edge is recorded in *TAKEN_EDGE_P.
3898 If STMT produces a varying value, return SSA_PROP_VARYING. */
3900 enum ssa_prop_result
3901 vrp_prop::visit_stmt (gimple
*stmt
, edge
*taken_edge_p
, tree
*output_p
)
3903 tree lhs
= gimple_get_lhs (stmt
);
3904 value_range_equiv vr
;
3905 m_vr_values
->extract_range_from_stmt (stmt
, taken_edge_p
, output_p
, &vr
);
3909 if (m_vr_values
->update_value_range (*output_p
, &vr
))
3911 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3913 fprintf (dump_file
, "Found new range for ");
3914 print_generic_expr (dump_file
, *output_p
);
3915 fprintf (dump_file
, ": ");
3916 dump_value_range (dump_file
, &vr
);
3917 fprintf (dump_file
, "\n");
3920 if (vr
.varying_p ())
3921 return SSA_PROP_VARYING
;
3923 return SSA_PROP_INTERESTING
;
3925 return SSA_PROP_NOT_INTERESTING
;
3928 if (is_gimple_call (stmt
) && gimple_call_internal_p (stmt
))
3929 switch (gimple_call_internal_fn (stmt
))
3931 case IFN_ADD_OVERFLOW
:
3932 case IFN_SUB_OVERFLOW
:
3933 case IFN_MUL_OVERFLOW
:
3934 case IFN_ATOMIC_COMPARE_EXCHANGE
:
3935 /* These internal calls return _Complex integer type,
3936 which VRP does not track, but the immediate uses
3937 thereof might be interesting. */
3938 if (lhs
&& TREE_CODE (lhs
) == SSA_NAME
)
3940 imm_use_iterator iter
;
3941 use_operand_p use_p
;
3942 enum ssa_prop_result res
= SSA_PROP_VARYING
;
3944 m_vr_values
->set_def_to_varying (lhs
);
3946 FOR_EACH_IMM_USE_FAST (use_p
, iter
, lhs
)
3948 gimple
*use_stmt
= USE_STMT (use_p
);
3949 if (!is_gimple_assign (use_stmt
))
3951 enum tree_code rhs_code
= gimple_assign_rhs_code (use_stmt
);
3952 if (rhs_code
!= REALPART_EXPR
&& rhs_code
!= IMAGPART_EXPR
)
3954 tree rhs1
= gimple_assign_rhs1 (use_stmt
);
3955 tree use_lhs
= gimple_assign_lhs (use_stmt
);
3956 if (TREE_CODE (rhs1
) != rhs_code
3957 || TREE_OPERAND (rhs1
, 0) != lhs
3958 || TREE_CODE (use_lhs
) != SSA_NAME
3959 || !stmt_interesting_for_vrp (use_stmt
)
3960 || (!INTEGRAL_TYPE_P (TREE_TYPE (use_lhs
))
3961 || !TYPE_MIN_VALUE (TREE_TYPE (use_lhs
))
3962 || !TYPE_MAX_VALUE (TREE_TYPE (use_lhs
))))
3965 /* If there is a change in the value range for any of the
3966 REALPART_EXPR/IMAGPART_EXPR immediate uses, return
3967 SSA_PROP_INTERESTING. If there are any REALPART_EXPR
3968 or IMAGPART_EXPR immediate uses, but none of them have
3969 a change in their value ranges, return
3970 SSA_PROP_NOT_INTERESTING. If there are no
3971 {REAL,IMAG}PART_EXPR uses at all,
3972 return SSA_PROP_VARYING. */
3973 value_range_equiv new_vr
;
3974 m_vr_values
->extract_range_basic (&new_vr
, use_stmt
);
3975 const value_range_equiv
*old_vr
3976 = m_vr_values
->get_value_range (use_lhs
);
3977 if (!old_vr
->equal_p (new_vr
, /*ignore_equivs=*/false))
3978 res
= SSA_PROP_INTERESTING
;
3980 res
= SSA_PROP_NOT_INTERESTING
;
3981 new_vr
.equiv_clear ();
3982 if (res
== SSA_PROP_INTERESTING
)
3996 /* All other statements produce nothing of interest for VRP, so mark
3997 their outputs varying and prevent further simulation. */
3998 m_vr_values
->set_defs_to_varying (stmt
);
4000 return (*taken_edge_p
) ? SSA_PROP_INTERESTING
: SSA_PROP_VARYING
;
4003 /* Visit all arguments for PHI node PHI that flow through executable
4004 edges. If a valid value range can be derived from all the incoming
4005 value ranges, set a new range for the LHS of PHI. */
4007 enum ssa_prop_result
4008 vrp_prop::visit_phi (gphi
*phi
)
4010 tree lhs
= PHI_RESULT (phi
);
4011 value_range_equiv vr_result
;
4012 m_vr_values
->extract_range_from_phi_node (phi
, &vr_result
);
4013 if (m_vr_values
->update_value_range (lhs
, &vr_result
))
4015 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4017 fprintf (dump_file
, "Found new range for ");
4018 print_generic_expr (dump_file
, lhs
);
4019 fprintf (dump_file
, ": ");
4020 dump_value_range (dump_file
, &vr_result
);
4021 fprintf (dump_file
, "\n");
4024 if (vr_result
.varying_p ())
4025 return SSA_PROP_VARYING
;
4027 return SSA_PROP_INTERESTING
;
4030 /* Nothing changed, don't add outgoing edges. */
4031 return SSA_PROP_NOT_INTERESTING
;
4034 /* Traverse all the blocks folding conditionals with known ranges. */
4037 vrp_prop::finalize ()
4041 /* We have completed propagating through the lattice. */
4042 m_vr_values
->set_lattice_propagation_complete ();
4046 fprintf (dump_file
, "\nValue ranges after VRP:\n\n");
4047 m_vr_values
->dump (dump_file
);
4048 fprintf (dump_file
, "\n");
4051 /* Set value range to non pointer SSA_NAMEs. */
4052 for (i
= 0; i
< num_ssa_names
; i
++)
4054 tree name
= ssa_name (i
);
4058 const value_range_equiv
*vr
= m_vr_values
->get_value_range (name
);
4059 if (!name
|| vr
->varying_p () || !vr
->constant_p ())
4062 if (POINTER_TYPE_P (TREE_TYPE (name
))
4063 && range_includes_zero_p (vr
) == 0)
4064 set_ptr_nonnull (name
);
4065 else if (!POINTER_TYPE_P (TREE_TYPE (name
)))
4066 set_range_info (name
, *vr
);
4070 class vrp_folder
: public substitute_and_fold_engine
4073 vrp_folder (vr_values
*v
)
4074 : substitute_and_fold_engine (/* Fold all stmts. */ true),
4075 m_vr_values (v
), simplifier (v
)
4079 tree
value_of_expr (tree name
, gimple
*stmt
) OVERRIDE
4081 return m_vr_values
->value_of_expr (name
, stmt
);
4083 bool fold_stmt (gimple_stmt_iterator
*) FINAL OVERRIDE
;
4084 bool fold_predicate_in (gimple_stmt_iterator
*);
4086 vr_values
*m_vr_values
;
4087 simplify_using_ranges simplifier
;
4090 /* If the statement pointed by SI has a predicate whose value can be
4091 computed using the value range information computed by VRP, compute
4092 its value and return true. Otherwise, return false. */
4095 vrp_folder::fold_predicate_in (gimple_stmt_iterator
*si
)
4097 bool assignment_p
= false;
4099 gimple
*stmt
= gsi_stmt (*si
);
4101 if (is_gimple_assign (stmt
)
4102 && TREE_CODE_CLASS (gimple_assign_rhs_code (stmt
)) == tcc_comparison
)
4104 assignment_p
= true;
4105 val
= simplifier
.vrp_evaluate_conditional (gimple_assign_rhs_code (stmt
),
4106 gimple_assign_rhs1 (stmt
),
4107 gimple_assign_rhs2 (stmt
),
4110 else if (gcond
*cond_stmt
= dyn_cast
<gcond
*> (stmt
))
4111 val
= simplifier
.vrp_evaluate_conditional (gimple_cond_code (cond_stmt
),
4112 gimple_cond_lhs (cond_stmt
),
4113 gimple_cond_rhs (cond_stmt
),
4121 val
= fold_convert (TREE_TYPE (gimple_assign_lhs (stmt
)), val
);
4125 fprintf (dump_file
, "Folding predicate ");
4126 print_gimple_expr (dump_file
, stmt
, 0);
4127 fprintf (dump_file
, " to ");
4128 print_generic_expr (dump_file
, val
);
4129 fprintf (dump_file
, "\n");
4132 if (is_gimple_assign (stmt
))
4133 gimple_assign_set_rhs_from_tree (si
, val
);
4136 gcc_assert (gimple_code (stmt
) == GIMPLE_COND
);
4137 gcond
*cond_stmt
= as_a
<gcond
*> (stmt
);
4138 if (integer_zerop (val
))
4139 gimple_cond_make_false (cond_stmt
);
4140 else if (integer_onep (val
))
4141 gimple_cond_make_true (cond_stmt
);
4152 /* Callback for substitute_and_fold folding the stmt at *SI. */
4155 vrp_folder::fold_stmt (gimple_stmt_iterator
*si
)
4157 if (fold_predicate_in (si
))
4160 return simplifier
.simplify (si
);
4163 class vrp_jump_threader_simplifier
: public jump_threader_simplifier
4166 vrp_jump_threader_simplifier (vr_values
*v
, avail_exprs_stack
*avails
)
4167 : jump_threader_simplifier (v
), m_avail_exprs_stack (avails
) { }
4170 tree
simplify (gimple
*, gimple
*, basic_block
, jt_state
*) OVERRIDE
;
4171 avail_exprs_stack
*m_avail_exprs_stack
;
4175 vrp_jump_threader_simplifier::simplify (gimple
*stmt
,
4176 gimple
*within_stmt
,
4180 /* First see if the conditional is in the hash table. */
4181 tree cached_lhs
= m_avail_exprs_stack
->lookup_avail_expr (stmt
, false, true);
4182 if (cached_lhs
&& is_gimple_min_invariant (cached_lhs
))
4185 if (gcond
*cond_stmt
= dyn_cast
<gcond
*> (stmt
))
4187 tree op0
= gimple_cond_lhs (cond_stmt
);
4188 op0
= lhs_of_dominating_assert (op0
, bb
, stmt
);
4190 tree op1
= gimple_cond_rhs (cond_stmt
);
4191 op1
= lhs_of_dominating_assert (op1
, bb
, stmt
);
4193 simplify_using_ranges
simplifier (m_vr_values
);
4194 return simplifier
.vrp_evaluate_conditional (gimple_cond_code (cond_stmt
),
4195 op0
, op1
, within_stmt
);
4198 if (gswitch
*switch_stmt
= dyn_cast
<gswitch
*> (stmt
))
4200 tree op
= gimple_switch_index (switch_stmt
);
4201 if (TREE_CODE (op
) != SSA_NAME
)
4204 op
= lhs_of_dominating_assert (op
, bb
, stmt
);
4206 const value_range_equiv
*vr
= m_vr_values
->get_value_range (op
);
4207 return find_case_label_range (switch_stmt
, vr
);
4210 return jump_threader_simplifier::simplify (stmt
, within_stmt
, bb
, state
);
4213 /* Blocks which have more than one predecessor and more than
4214 one successor present jump threading opportunities, i.e.,
4215 when the block is reached from a specific predecessor, we
4216 may be able to determine which of the outgoing edges will
4217 be traversed. When this optimization applies, we are able
4218 to avoid conditionals at runtime and we may expose secondary
4219 optimization opportunities.
4221 This class is effectively a driver for the generic jump
4222 threading code. It basically just presents the generic code
4223 with edges that may be suitable for jump threading.
4225 Unlike DOM, we do not iterate VRP if jump threading was successful.
4226 While iterating may expose new opportunities for VRP, it is expected
4227 those opportunities would be very limited and the compile time cost
4228 to expose those opportunities would be significant.
4230 As jump threading opportunities are discovered, they are registered
4231 for later realization. */
4233 class vrp_jump_threader
: public dom_walker
4236 vrp_jump_threader (function
*, vr_values
*);
4237 ~vrp_jump_threader ();
4239 void thread_jumps ()
4241 walk (m_fun
->cfg
->x_entry_block_ptr
);
4244 void thread_through_all_blocks ()
4246 // FIXME: Put this in the destructor?
4247 m_threader
->thread_through_all_blocks (false);
4251 virtual edge
before_dom_children (basic_block
);
4252 virtual void after_dom_children (basic_block
);
4255 vr_values
*m_vr_values
;
4256 const_and_copies
*m_const_and_copies
;
4257 avail_exprs_stack
*m_avail_exprs_stack
;
4258 hash_table
<expr_elt_hasher
> *m_avail_exprs
;
4259 vrp_jump_threader_simplifier
*m_simplifier
;
4260 jump_threader
*m_threader
;
4264 vrp_jump_threader::vrp_jump_threader (struct function
*fun
, vr_values
*v
)
4265 : dom_walker (CDI_DOMINATORS
, REACHABLE_BLOCKS
)
4267 /* Ugh. When substituting values earlier in this pass we can wipe
4268 the dominance information. So rebuild the dominator information
4269 as we need it within the jump threading code. */
4270 calculate_dominance_info (CDI_DOMINATORS
);
4272 /* We do not allow VRP information to be used for jump threading
4273 across a back edge in the CFG. Otherwise it becomes too
4274 difficult to avoid eliminating loop exit tests. Of course
4275 EDGE_DFS_BACK is not accurate at this time so we have to
4277 mark_dfs_back_edges ();
4279 /* Allocate our unwinder stack to unwind any temporary equivalences
4280 that might be recorded. */
4281 m_const_and_copies
= new const_and_copies ();
4285 m_avail_exprs
= new hash_table
<expr_elt_hasher
> (1024);
4286 m_avail_exprs_stack
= new avail_exprs_stack (m_avail_exprs
);
4287 m_state
= new jt_state (m_const_and_copies
, m_avail_exprs_stack
, NULL
);
4289 m_simplifier
= new vrp_jump_threader_simplifier (m_vr_values
,
4290 m_avail_exprs_stack
);
4291 m_threader
= new jump_threader (m_simplifier
, m_state
);
4294 vrp_jump_threader::~vrp_jump_threader ()
4296 /* We do not actually update the CFG or SSA graphs at this point as
4297 ASSERT_EXPRs are still in the IL and cfg cleanup code does not
4298 yet handle ASSERT_EXPRs gracefully. */
4299 delete m_const_and_copies
;
4300 delete m_avail_exprs
;
4301 delete m_avail_exprs_stack
;
4302 delete m_simplifier
;
4307 /* Called before processing dominator children of BB. We want to look
4308 at ASSERT_EXPRs and record information from them in the appropriate
4311 We could look at other statements here. It's not seen as likely
4312 to significantly increase the jump threads we discover. */
4315 vrp_jump_threader::before_dom_children (basic_block bb
)
4317 gimple_stmt_iterator gsi
;
4319 m_avail_exprs_stack
->push_marker ();
4320 m_const_and_copies
->push_marker ();
4321 for (gsi
= gsi_start_nondebug_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
4323 gimple
*stmt
= gsi_stmt (gsi
);
4324 if (gimple_assign_single_p (stmt
)
4325 && TREE_CODE (gimple_assign_rhs1 (stmt
)) == ASSERT_EXPR
)
4327 tree rhs1
= gimple_assign_rhs1 (stmt
);
4328 tree cond
= TREE_OPERAND (rhs1
, 1);
4329 tree inverted
= invert_truthvalue (cond
);
4330 vec
<cond_equivalence
> p
;
4332 record_conditions (&p
, cond
, inverted
);
4333 for (unsigned int i
= 0; i
< p
.length (); i
++)
4334 m_avail_exprs_stack
->record_cond (&p
[i
]);
4336 tree lhs
= gimple_assign_lhs (stmt
);
4337 m_const_and_copies
->record_const_or_copy (lhs
,
4338 TREE_OPERAND (rhs1
, 0));
4347 /* Called after processing dominator children of BB. This is where we
4348 actually call into the threader. */
4350 vrp_jump_threader::after_dom_children (basic_block bb
)
4352 m_threader
->thread_outgoing_edges (bb
);
4353 m_avail_exprs_stack
->pop_to_marker ();
4354 m_const_and_copies
->pop_to_marker ();
4357 /* STMT is a conditional at the end of a basic block.
4359 If the conditional is of the form SSA_NAME op constant and the SSA_NAME
4360 was set via a type conversion, try to replace the SSA_NAME with the RHS
4361 of the type conversion. Doing so makes the conversion dead which helps
4362 subsequent passes. */
4365 vrp_simplify_cond_using_ranges (range_query
*query
, gcond
*stmt
)
4367 tree op0
= gimple_cond_lhs (stmt
);
4368 tree op1
= gimple_cond_rhs (stmt
);
4370 /* If we have a comparison of an SSA_NAME (OP0) against a constant,
4371 see if OP0 was set by a type conversion where the source of
4372 the conversion is another SSA_NAME with a range that fits
4373 into the range of OP0's type.
4375 If so, the conversion is redundant as the earlier SSA_NAME can be
4376 used for the comparison directly if we just massage the constant in the
4378 if (TREE_CODE (op0
) == SSA_NAME
4379 && TREE_CODE (op1
) == INTEGER_CST
)
4381 gimple
*def_stmt
= SSA_NAME_DEF_STMT (op0
);
4384 if (!is_gimple_assign (def_stmt
))
4387 switch (gimple_assign_rhs_code (def_stmt
))
4390 innerop
= gimple_assign_rhs1 (def_stmt
);
4392 case VIEW_CONVERT_EXPR
:
4393 innerop
= TREE_OPERAND (gimple_assign_rhs1 (def_stmt
), 0);
4394 if (!INTEGRAL_TYPE_P (TREE_TYPE (innerop
)))
4401 if (TREE_CODE (innerop
) == SSA_NAME
4402 && !POINTER_TYPE_P (TREE_TYPE (innerop
))
4403 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (innerop
)
4404 && desired_pro_or_demotion_p (TREE_TYPE (innerop
), TREE_TYPE (op0
)))
4406 const value_range
*vr
= query
->get_value_range (innerop
);
4408 if (range_int_cst_p (vr
)
4409 && range_fits_type_p (vr
,
4410 TYPE_PRECISION (TREE_TYPE (op0
)),
4411 TYPE_SIGN (TREE_TYPE (op0
)))
4412 && int_fits_type_p (op1
, TREE_TYPE (innerop
)))
4414 tree newconst
= fold_convert (TREE_TYPE (innerop
), op1
);
4415 gimple_cond_set_lhs (stmt
, innerop
);
4416 gimple_cond_set_rhs (stmt
, newconst
);
4418 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4420 fprintf (dump_file
, "Folded into: ");
4421 print_gimple_stmt (dump_file
, stmt
, 0, TDF_SLIM
);
4422 fprintf (dump_file
, "\n");
4429 /* A comparison of an SSA_NAME against a constant where the SSA_NAME
4430 was set by a type conversion can often be rewritten to use the RHS
4431 of the type conversion. Do this optimization for all conditionals
4434 However, doing so inhibits jump threading through the comparison.
4435 So that transformation is not performed until after jump threading
4439 simplify_casted_conds (function
*fun
, range_query
*query
)
4442 FOR_EACH_BB_FN (bb
, fun
)
4444 gimple
*last
= last_stmt (bb
);
4445 if (last
&& gimple_code (last
) == GIMPLE_COND
)
4446 vrp_simplify_cond_using_ranges (query
, as_a
<gcond
*> (last
));
4450 /* Main entry point to VRP (Value Range Propagation). This pass is
4451 loosely based on J. R. C. Patterson, ``Accurate Static Branch
4452 Prediction by Value Range Propagation,'' in SIGPLAN Conference on
4453 Programming Language Design and Implementation, pp. 67-78, 1995.
4454 Also available at http://citeseer.ist.psu.edu/patterson95accurate.html
4456 This is essentially an SSA-CCP pass modified to deal with ranges
4457 instead of constants.
4459 While propagating ranges, we may find that two or more SSA name
4460 have equivalent, though distinct ranges. For instance,
4463 2 p_4 = ASSERT_EXPR <p_3, p_3 != 0>
4465 4 p_5 = ASSERT_EXPR <p_4, p_4 == q_2>;
4469 In the code above, pointer p_5 has range [q_2, q_2], but from the
4470 code we can also determine that p_5 cannot be NULL and, if q_2 had
4471 a non-varying range, p_5's range should also be compatible with it.
4473 These equivalences are created by two expressions: ASSERT_EXPR and
4474 copy operations. Since p_5 is an assertion on p_4, and p_4 was the
4475 result of another assertion, then we can use the fact that p_5 and
4476 p_4 are equivalent when evaluating p_5's range.
4478 Together with value ranges, we also propagate these equivalences
4479 between names so that we can take advantage of information from
4480 multiple ranges when doing final replacement. Note that this
4481 equivalency relation is transitive but not symmetric.
4483 In the example above, p_5 is equivalent to p_4, q_2 and p_3, but we
4484 cannot assert that q_2 is equivalent to p_5 because q_2 may be used
4485 in contexts where that assertion does not hold (e.g., in line 6).
4487 TODO, the main difference between this pass and Patterson's is that
4488 we do not propagate edge probabilities. We only compute whether
4489 edges can be taken or not. That is, instead of having a spectrum
4490 of jump probabilities between 0 and 1, we only deal with 0, 1 and
4491 DON'T KNOW. In the future, it may be worthwhile to propagate
4492 probabilities to aid branch prediction. */
4495 execute_vrp (struct function
*fun
, bool warn_array_bounds_p
)
4497 loop_optimizer_init (LOOPS_NORMAL
| LOOPS_HAVE_RECORDED_EXITS
);
4498 rewrite_into_loop_closed_ssa (NULL
, TODO_update_ssa
);
4501 /* ??? This ends up using stale EDGE_DFS_BACK for liveness computation.
4502 Inserting assertions may split edges which will invalidate
4504 vrp_asserts
assert_engine (fun
);
4505 assert_engine
.insert_range_assertions ();
4507 /* For visiting PHI nodes we need EDGE_DFS_BACK computed. */
4508 mark_dfs_back_edges ();
4510 vr_values vrp_vr_values
;
4512 class vrp_prop
vrp_prop (&vrp_vr_values
);
4513 vrp_prop
.initialize (fun
);
4514 vrp_prop
.ssa_propagate ();
4516 /* Instantiate the folder here, so that edge cleanups happen at the
4517 end of this function. */
4518 vrp_folder
folder (&vrp_vr_values
);
4519 vrp_prop
.finalize ();
4521 /* If we're checking array refs, we want to merge information on
4522 the executability of each edge between vrp_folder and the
4523 check_array_bounds_dom_walker: each can clear the
4524 EDGE_EXECUTABLE flag on edges, in different ways.
4526 Hence, if we're going to call check_all_array_refs, set
4527 the flag on every edge now, rather than in
4528 check_array_bounds_dom_walker's ctor; vrp_folder may clear
4529 it from some edges. */
4530 if (warn_array_bounds
&& warn_array_bounds_p
)
4531 set_all_edges_as_executable (fun
);
4533 folder
.substitute_and_fold ();
4535 if (warn_array_bounds
&& warn_array_bounds_p
)
4537 array_bounds_checker
array_checker (fun
, &vrp_vr_values
);
4538 array_checker
.check ();
4541 /* We must identify jump threading opportunities before we release
4542 the datastructures built by VRP. */
4543 vrp_jump_threader
threader (fun
, &vrp_vr_values
);
4544 threader
.thread_jumps ();
4546 simplify_casted_conds (fun
, &vrp_vr_values
);
4548 free_numbers_of_iterations_estimates (fun
);
4550 /* ASSERT_EXPRs must be removed before finalizing jump threads
4551 as finalizing jump threads calls the CFG cleanup code which
4552 does not properly handle ASSERT_EXPRs. */
4553 assert_engine
.remove_range_assertions ();
4555 /* If we exposed any new variables, go ahead and put them into
4556 SSA form now, before we handle jump threading. This simplifies
4557 interactions between rewriting of _DECL nodes into SSA form
4558 and rewriting SSA_NAME nodes into SSA form after block
4559 duplication and CFG manipulation. */
4560 update_ssa (TODO_update_ssa
);
4562 /* We identified all the jump threading opportunities earlier, but could
4563 not transform the CFG at that time. This routine transforms the
4564 CFG and arranges for the dominator tree to be rebuilt if necessary.
4566 Note the SSA graph update will occur during the normal TODO
4567 processing by the pass manager. */
4568 threader
.thread_through_all_blocks ();
4571 loop_optimizer_finalize ();
4577 const pass_data pass_data_vrp
=
4579 GIMPLE_PASS
, /* type */
4581 OPTGROUP_NONE
, /* optinfo_flags */
4582 TV_TREE_VRP
, /* tv_id */
4583 PROP_ssa
, /* properties_required */
4584 0, /* properties_provided */
4585 0, /* properties_destroyed */
4586 0, /* todo_flags_start */
4587 ( TODO_cleanup_cfg
| TODO_update_ssa
), /* todo_flags_finish */
4590 class pass_vrp
: public gimple_opt_pass
4593 pass_vrp (gcc::context
*ctxt
)
4594 : gimple_opt_pass (pass_data_vrp
, ctxt
), warn_array_bounds_p (false)
4597 /* opt_pass methods: */
4598 opt_pass
* clone () { return new pass_vrp (m_ctxt
); }
4599 void set_pass_param (unsigned int n
, bool param
)
4601 gcc_assert (n
== 0);
4602 warn_array_bounds_p
= param
;
4604 virtual bool gate (function
*) { return flag_tree_vrp
!= 0; }
4605 virtual unsigned int execute (function
*fun
)
4606 { return execute_vrp (fun
, warn_array_bounds_p
); }
4609 bool warn_array_bounds_p
;
4610 }; // class pass_vrp
4615 make_pass_vrp (gcc::context
*ctxt
)
4617 return new pass_vrp (ctxt
);