1 /* Support routines for Value Range Propagation (VRP).
2 Copyright (C) 2005-2019 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
11 GCC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
22 #include "coretypes.h"
24 #include "insn-codes.h"
28 #include "optabs-tree.h"
29 #include "gimple-pretty-print.h"
30 #include "diagnostic-core.h"
32 #include "fold-const.h"
35 #include "gimple-fold.h"
36 #include "gimple-iterator.h"
38 #include "tree-ssa-loop-niter.h"
39 #include "tree-ssa-loop.h"
42 #include "tree-scalar-evolution.h"
43 #include "tree-ssa-propagate.h"
44 #include "tree-chrec.h"
45 #include "omp-general.h"
46 #include "case-cfn-macros.h"
47 #include "alloc-pool.h"
50 #include "vr-values.h"
54 /* Set value range VR to a non-negative range of type TYPE. */
57 set_value_range_to_nonnegative (value_range
*vr
, tree type
)
59 tree zero
= build_int_cst (type
, 0);
60 vr
->update (VR_RANGE
, zero
, vrp_val_max (type
));
63 /* Set value range VR to a range of a truthvalue of type TYPE. */
66 set_value_range_to_truthvalue (value_range
*vr
, tree type
)
68 if (TYPE_PRECISION (type
) == 1)
69 vr
->set_varying (type
);
71 vr
->update (VR_RANGE
, build_int_cst (type
, 0), build_int_cst (type
, 1));
74 /* Return the lattice entry for VAR or NULL if it doesn't exist or cannot
78 vr_values::get_lattice_entry (const_tree var
)
82 unsigned ver
= SSA_NAME_VERSION (var
);
84 /* If we query the entry for a new SSA name avoid reallocating the lattice
85 since we should get here at most from the substitute-and-fold stage which
86 will never try to change values. */
87 if (ver
>= num_vr_values
)
94 /* Create a default value range. */
95 vr_value
[ver
] = vr
= vrp_value_range_pool
.allocate ();
97 /* After propagation finished return varying. */
98 if (values_propagated
)
100 vr
->set_varying (TREE_TYPE (var
));
104 vr
->set_undefined ();
106 /* If VAR is a default definition of a parameter, the variable can
107 take any value in VAR's type. */
108 if (SSA_NAME_IS_DEFAULT_DEF (var
))
110 sym
= SSA_NAME_VAR (var
);
111 if (TREE_CODE (sym
) == PARM_DECL
)
113 /* Try to use the "nonnull" attribute to create ~[0, 0]
114 anti-ranges for pointers. Note that this is only valid with
115 default definitions of PARM_DECLs. */
116 if (POINTER_TYPE_P (TREE_TYPE (sym
))
117 && (nonnull_arg_p (sym
)
118 || get_ptr_nonnull (var
)))
120 vr
->set_nonzero (TREE_TYPE (sym
));
123 else if (INTEGRAL_TYPE_P (TREE_TYPE (sym
)))
125 get_range_info (var
, *vr
);
126 if (vr
->undefined_p ())
127 vr
->set_varying (TREE_TYPE (sym
));
130 vr
->set_varying (TREE_TYPE (sym
));
132 else if (TREE_CODE (sym
) == RESULT_DECL
133 && DECL_BY_REFERENCE (sym
))
135 vr
->set_nonzero (TREE_TYPE (sym
));
143 /* Return value range information for VAR.
145 If we have no values ranges recorded (ie, VRP is not running), then
146 return NULL. Otherwise create an empty range if none existed for VAR. */
149 vr_values::get_value_range (const_tree var
)
151 /* If we have no recorded ranges, then return NULL. */
155 value_range
*vr
= get_lattice_entry (var
);
157 /* Reallocate the lattice if needed. */
160 unsigned int old_sz
= num_vr_values
;
161 num_vr_values
= num_ssa_names
+ num_ssa_names
/ 10;
162 vr_value
= XRESIZEVEC (value_range
*, vr_value
, num_vr_values
);
163 for ( ; old_sz
< num_vr_values
; old_sz
++)
164 vr_value
[old_sz
] = NULL
;
166 /* Now that the lattice has been resized, we should never fail. */
167 vr
= get_lattice_entry (var
);
174 /* Set the lattice entry for DEF to VARYING. */
177 vr_values::set_def_to_varying (const_tree def
)
179 value_range
*vr
= get_lattice_entry (def
);
181 vr
->set_varying (TREE_TYPE (def
));
184 /* Set value-ranges of all SSA names defined by STMT to varying. */
187 vr_values::set_defs_to_varying (gimple
*stmt
)
191 FOR_EACH_SSA_TREE_OPERAND (def
, stmt
, i
, SSA_OP_DEF
)
192 set_def_to_varying (def
);
195 /* Update the value range and equivalence set for variable VAR to
196 NEW_VR. Return true if NEW_VR is different from VAR's previous
199 NOTE: This function assumes that NEW_VR is a temporary value range
200 object created for the sole purpose of updating VAR's range. The
201 storage used by the equivalence set from NEW_VR will be freed by
202 this function. Do not call update_value_range when NEW_VR
203 is the range object associated with another SSA name. */
206 vr_values::update_value_range (const_tree var
, value_range
*new_vr
)
211 /* If there is a value-range on the SSA name from earlier analysis
213 if (INTEGRAL_TYPE_P (TREE_TYPE (var
)))
216 value_range_kind rtype
= get_range_info (var
, nr
);
217 if (rtype
== VR_RANGE
|| rtype
== VR_ANTI_RANGE
)
218 new_vr
->intersect (&nr
);
221 /* Update the value range, if necessary. If we cannot allocate a lattice
222 entry for VAR keep it at VARYING. This happens when DOM feeds us stmts
223 with SSA names allocated after setting up the lattice. */
224 old_vr
= get_lattice_entry (var
);
227 is_new
= !old_vr
->equal_p (*new_vr
, /*ignore_equivs=*/false);
231 /* Do not allow transitions up the lattice. The following
232 is slightly more awkward than just new_vr->type < old_vr->type
233 because VR_RANGE and VR_ANTI_RANGE need to be considered
234 the same. We may not have is_new when transitioning to
235 UNDEFINED. If old_vr->type is VARYING, we shouldn't be
236 called, if we are anyway, keep it VARYING. */
237 if (old_vr
->varying_p ())
239 new_vr
->set_varying (TREE_TYPE (var
));
242 else if (new_vr
->undefined_p ())
244 old_vr
->set_varying (TREE_TYPE (var
));
245 new_vr
->set_varying (TREE_TYPE (var
));
249 old_vr
->set (new_vr
->kind (),
250 new_vr
->min (), new_vr
->max (), new_vr
->equiv ());
253 new_vr
->equiv_clear ();
258 /* Return true if value range VR involves exactly one symbol SYM. */
261 symbolic_range_based_on_p (value_range_base
*vr
, const_tree sym
)
263 bool neg
, min_has_symbol
, max_has_symbol
;
266 if (is_gimple_min_invariant (vr
->min ()))
267 min_has_symbol
= false;
268 else if (get_single_symbol (vr
->min (), &neg
, &inv
) == sym
)
269 min_has_symbol
= true;
273 if (is_gimple_min_invariant (vr
->max ()))
274 max_has_symbol
= false;
275 else if (get_single_symbol (vr
->max (), &neg
, &inv
) == sym
)
276 max_has_symbol
= true;
280 return (min_has_symbol
|| max_has_symbol
);
283 /* Return true if the result of assignment STMT is know to be non-zero. */
286 gimple_assign_nonzero_p (gimple
*stmt
)
288 enum tree_code code
= gimple_assign_rhs_code (stmt
);
289 bool strict_overflow_p
;
290 switch (get_gimple_rhs_class (code
))
292 case GIMPLE_UNARY_RHS
:
293 return tree_unary_nonzero_warnv_p (gimple_assign_rhs_code (stmt
),
294 gimple_expr_type (stmt
),
295 gimple_assign_rhs1 (stmt
),
297 case GIMPLE_BINARY_RHS
:
298 return tree_binary_nonzero_warnv_p (gimple_assign_rhs_code (stmt
),
299 gimple_expr_type (stmt
),
300 gimple_assign_rhs1 (stmt
),
301 gimple_assign_rhs2 (stmt
),
303 case GIMPLE_TERNARY_RHS
:
305 case GIMPLE_SINGLE_RHS
:
306 return tree_single_nonzero_warnv_p (gimple_assign_rhs1 (stmt
),
308 case GIMPLE_INVALID_RHS
:
315 /* Return true if STMT is known to compute a non-zero value. */
318 gimple_stmt_nonzero_p (gimple
*stmt
)
320 switch (gimple_code (stmt
))
323 return gimple_assign_nonzero_p (stmt
);
326 gcall
*call_stmt
= as_a
<gcall
*> (stmt
);
327 return (gimple_call_nonnull_result_p (call_stmt
)
328 || gimple_call_nonnull_arg (call_stmt
));
334 /* Like tree_expr_nonzero_p, but this function uses value ranges
338 vr_values::vrp_stmt_computes_nonzero (gimple
*stmt
)
340 if (gimple_stmt_nonzero_p (stmt
))
343 /* If we have an expression of the form &X->a, then the expression
344 is nonnull if X is nonnull. */
345 if (is_gimple_assign (stmt
)
346 && gimple_assign_rhs_code (stmt
) == ADDR_EXPR
)
348 tree expr
= gimple_assign_rhs1 (stmt
);
349 poly_int64 bitsize
, bitpos
;
352 int unsignedp
, reversep
, volatilep
;
353 tree base
= get_inner_reference (TREE_OPERAND (expr
, 0), &bitsize
,
354 &bitpos
, &offset
, &mode
, &unsignedp
,
355 &reversep
, &volatilep
);
357 if (base
!= NULL_TREE
358 && TREE_CODE (base
) == MEM_REF
359 && TREE_CODE (TREE_OPERAND (base
, 0)) == SSA_NAME
)
361 poly_offset_int off
= 0;
362 bool off_cst
= false;
363 if (offset
== NULL_TREE
|| TREE_CODE (offset
) == INTEGER_CST
)
365 off
= mem_ref_offset (base
);
367 off
+= poly_offset_int::from (wi::to_poly_wide (offset
),
369 off
<<= LOG2_BITS_PER_UNIT
;
373 /* If &X->a is equal to X and X is ~[0, 0], the result is too.
374 For -fdelete-null-pointer-checks -fno-wrapv-pointer we don't
375 allow going from non-NULL pointer to NULL. */
376 if ((off_cst
&& known_eq (off
, 0))
377 || (flag_delete_null_pointer_checks
378 && !TYPE_OVERFLOW_WRAPS (TREE_TYPE (expr
))))
380 const value_range
*vr
= get_value_range (TREE_OPERAND (base
, 0));
381 if (!range_includes_zero_p (vr
))
384 /* If MEM_REF has a "positive" offset, consider it non-NULL
385 always, for -fdelete-null-pointer-checks also "negative"
386 ones. Punt for unknown offsets (e.g. variable ones). */
387 if (!TYPE_OVERFLOW_WRAPS (TREE_TYPE (expr
))
390 && (flag_delete_null_pointer_checks
|| known_gt (off
, 0)))
398 /* Returns true if EXPR is a valid value (as expected by compare_values) --
399 a gimple invariant, or SSA_NAME +- CST. */
402 valid_value_p (tree expr
)
404 if (TREE_CODE (expr
) == SSA_NAME
)
407 if (TREE_CODE (expr
) == PLUS_EXPR
408 || TREE_CODE (expr
) == MINUS_EXPR
)
409 return (TREE_CODE (TREE_OPERAND (expr
, 0)) == SSA_NAME
410 && TREE_CODE (TREE_OPERAND (expr
, 1)) == INTEGER_CST
);
412 return is_gimple_min_invariant (expr
);
415 /* If OP has a value range with a single constant value return that,
416 otherwise return NULL_TREE. This returns OP itself if OP is a
420 vr_values::op_with_constant_singleton_value_range (tree op
)
422 if (is_gimple_min_invariant (op
))
425 if (TREE_CODE (op
) != SSA_NAME
)
429 if (get_value_range (op
)->singleton_p (&t
))
434 /* Return true if op is in a boolean [0, 1] value-range. */
437 vr_values::op_with_boolean_value_range_p (tree op
)
439 const value_range
*vr
;
441 if (TYPE_PRECISION (TREE_TYPE (op
)) == 1)
444 if (integer_zerop (op
)
445 || integer_onep (op
))
448 if (TREE_CODE (op
) != SSA_NAME
)
451 vr
= get_value_range (op
);
452 return (vr
->kind () == VR_RANGE
453 && integer_zerop (vr
->min ())
454 && integer_onep (vr
->max ()));
457 /* Extract value range information for VAR when (OP COND_CODE LIMIT) is
458 true and store it in *VR_P. */
461 vr_values::extract_range_for_var_from_comparison_expr (tree var
,
462 enum tree_code cond_code
,
467 const value_range
*limit_vr
;
468 type
= TREE_TYPE (var
);
470 /* For pointer arithmetic, we only keep track of pointer equality
471 and inequality. If we arrive here with unfolded conditions like
472 _1 > _1 do not derive anything. */
473 if ((POINTER_TYPE_P (type
) && cond_code
!= NE_EXPR
&& cond_code
!= EQ_EXPR
)
476 vr_p
->set_varying (type
);
480 /* If LIMIT is another SSA name and LIMIT has a range of its own,
481 try to use LIMIT's range to avoid creating symbolic ranges
483 limit_vr
= (TREE_CODE (limit
) == SSA_NAME
) ? get_value_range (limit
) : NULL
;
485 /* LIMIT's range is only interesting if it has any useful information. */
487 || limit_vr
->undefined_p ()
488 || limit_vr
->varying_p ()
489 || (limit_vr
->symbolic_p ()
490 && ! (limit_vr
->kind () == VR_RANGE
491 && (limit_vr
->min () == limit_vr
->max ()
492 || operand_equal_p (limit_vr
->min (),
493 limit_vr
->max (), 0)))))
496 /* Initially, the new range has the same set of equivalences of
497 VAR's range. This will be revised before returning the final
498 value. Since assertions may be chained via mutually exclusive
499 predicates, we will need to trim the set of equivalences before
501 gcc_assert (vr_p
->equiv () == NULL
);
502 vr_p
->equiv_add (var
, get_value_range (var
), &vrp_equiv_obstack
);
504 /* Extract a new range based on the asserted comparison for VAR and
505 LIMIT's value range. Notice that if LIMIT has an anti-range, we
506 will only use it for equality comparisons (EQ_EXPR). For any
507 other kind of assertion, we cannot derive a range from LIMIT's
508 anti-range that can be used to describe the new range. For
509 instance, ASSERT_EXPR <x_2, x_2 <= b_4>. If b_4 is ~[2, 10],
510 then b_4 takes on the ranges [-INF, 1] and [11, +INF]. There is
511 no single range for x_2 that could describe LE_EXPR, so we might
512 as well build the range [b_4, +INF] for it.
513 One special case we handle is extracting a range from a
514 range test encoded as (unsigned)var + CST <= limit. */
515 if (TREE_CODE (op
) == NOP_EXPR
516 || TREE_CODE (op
) == PLUS_EXPR
)
518 if (TREE_CODE (op
) == PLUS_EXPR
)
520 min
= fold_build1 (NEGATE_EXPR
, TREE_TYPE (TREE_OPERAND (op
, 1)),
521 TREE_OPERAND (op
, 1));
522 max
= int_const_binop (PLUS_EXPR
, limit
, min
);
523 op
= TREE_OPERAND (op
, 0);
527 min
= build_int_cst (TREE_TYPE (var
), 0);
531 /* Make sure to not set TREE_OVERFLOW on the final type
532 conversion. We are willingly interpreting large positive
533 unsigned values as negative signed values here. */
534 min
= force_fit_type (TREE_TYPE (var
), wi::to_widest (min
), 0, false);
535 max
= force_fit_type (TREE_TYPE (var
), wi::to_widest (max
), 0, false);
537 /* We can transform a max, min range to an anti-range or
538 vice-versa. Use set_and_canonicalize which does this for
540 if (cond_code
== LE_EXPR
)
541 vr_p
->set (VR_RANGE
, min
, max
, vr_p
->equiv ());
542 else if (cond_code
== GT_EXPR
)
543 vr_p
->set (VR_ANTI_RANGE
, min
, max
, vr_p
->equiv ());
547 else if (cond_code
== EQ_EXPR
)
549 enum value_range_kind range_type
;
553 range_type
= limit_vr
->kind ();
554 min
= limit_vr
->min ();
555 max
= limit_vr
->max ();
559 range_type
= VR_RANGE
;
564 vr_p
->update (range_type
, min
, max
);
566 /* When asserting the equality VAR == LIMIT and LIMIT is another
567 SSA name, the new range will also inherit the equivalence set
569 if (TREE_CODE (limit
) == SSA_NAME
)
570 vr_p
->equiv_add (limit
, get_value_range (limit
), &vrp_equiv_obstack
);
572 else if (cond_code
== NE_EXPR
)
574 /* As described above, when LIMIT's range is an anti-range and
575 this assertion is an inequality (NE_EXPR), then we cannot
576 derive anything from the anti-range. For instance, if
577 LIMIT's range was ~[0, 0], the assertion 'VAR != LIMIT' does
578 not imply that VAR's range is [0, 0]. So, in the case of
579 anti-ranges, we just assert the inequality using LIMIT and
582 If LIMIT_VR is a range, we can only use it to build a new
583 anti-range if LIMIT_VR is a single-valued range. For
584 instance, if LIMIT_VR is [0, 1], the predicate
585 VAR != [0, 1] does not mean that VAR's range is ~[0, 1].
586 Rather, it means that for value 0 VAR should be ~[0, 0]
587 and for value 1, VAR should be ~[1, 1]. We cannot
588 represent these ranges.
590 The only situation in which we can build a valid
591 anti-range is when LIMIT_VR is a single-valued range
592 (i.e., LIMIT_VR->MIN == LIMIT_VR->MAX). In that case,
593 build the anti-range ~[LIMIT_VR->MIN, LIMIT_VR->MAX]. */
595 && limit_vr
->kind () == VR_RANGE
596 && compare_values (limit_vr
->min (), limit_vr
->max ()) == 0)
598 min
= limit_vr
->min ();
599 max
= limit_vr
->max ();
603 /* In any other case, we cannot use LIMIT's range to build a
608 /* If MIN and MAX cover the whole range for their type, then
609 just use the original LIMIT. */
610 if (INTEGRAL_TYPE_P (type
)
611 && vrp_val_is_min (min
)
612 && vrp_val_is_max (max
))
615 vr_p
->set (VR_ANTI_RANGE
, min
, max
, vr_p
->equiv ());
617 else if (cond_code
== LE_EXPR
|| cond_code
== LT_EXPR
)
619 min
= TYPE_MIN_VALUE (type
);
621 if (limit_vr
== NULL
|| limit_vr
->kind () == VR_ANTI_RANGE
)
625 /* If LIMIT_VR is of the form [N1, N2], we need to build the
626 range [MIN, N2] for LE_EXPR and [MIN, N2 - 1] for
628 max
= limit_vr
->max ();
631 /* If the maximum value forces us to be out of bounds, simply punt.
632 It would be pointless to try and do anything more since this
633 all should be optimized away above us. */
634 if (cond_code
== LT_EXPR
635 && compare_values (max
, min
) == 0)
636 vr_p
->set_varying (TREE_TYPE (min
));
639 /* For LT_EXPR, we create the range [MIN, MAX - 1]. */
640 if (cond_code
== LT_EXPR
)
642 if (TYPE_PRECISION (TREE_TYPE (max
)) == 1
643 && !TYPE_UNSIGNED (TREE_TYPE (max
)))
644 max
= fold_build2 (PLUS_EXPR
, TREE_TYPE (max
), max
,
645 build_int_cst (TREE_TYPE (max
), -1));
647 max
= fold_build2 (MINUS_EXPR
, TREE_TYPE (max
), max
,
648 build_int_cst (TREE_TYPE (max
), 1));
649 /* Signal to compare_values_warnv this expr doesn't overflow. */
651 TREE_NO_WARNING (max
) = 1;
654 vr_p
->update (VR_RANGE
, min
, max
);
657 else if (cond_code
== GE_EXPR
|| cond_code
== GT_EXPR
)
659 max
= TYPE_MAX_VALUE (type
);
661 if (limit_vr
== NULL
|| limit_vr
->kind () == VR_ANTI_RANGE
)
665 /* If LIMIT_VR is of the form [N1, N2], we need to build the
666 range [N1, MAX] for GE_EXPR and [N1 + 1, MAX] for
668 min
= limit_vr
->min ();
671 /* If the minimum value forces us to be out of bounds, simply punt.
672 It would be pointless to try and do anything more since this
673 all should be optimized away above us. */
674 if (cond_code
== GT_EXPR
675 && compare_values (min
, max
) == 0)
676 vr_p
->set_varying (TREE_TYPE (min
));
679 /* For GT_EXPR, we create the range [MIN + 1, MAX]. */
680 if (cond_code
== GT_EXPR
)
682 if (TYPE_PRECISION (TREE_TYPE (min
)) == 1
683 && !TYPE_UNSIGNED (TREE_TYPE (min
)))
684 min
= fold_build2 (MINUS_EXPR
, TREE_TYPE (min
), min
,
685 build_int_cst (TREE_TYPE (min
), -1));
687 min
= fold_build2 (PLUS_EXPR
, TREE_TYPE (min
), min
,
688 build_int_cst (TREE_TYPE (min
), 1));
689 /* Signal to compare_values_warnv this expr doesn't overflow. */
691 TREE_NO_WARNING (min
) = 1;
694 vr_p
->update (VR_RANGE
, min
, max
);
700 /* Finally intersect the new range with what we already know about var. */
701 vr_p
->intersect (get_value_range (var
));
704 /* Extract value range information from an ASSERT_EXPR EXPR and store
708 vr_values::extract_range_from_assert (value_range
*vr_p
, tree expr
)
710 tree var
= ASSERT_EXPR_VAR (expr
);
711 tree cond
= ASSERT_EXPR_COND (expr
);
713 enum tree_code cond_code
;
714 gcc_assert (COMPARISON_CLASS_P (cond
));
716 /* Find VAR in the ASSERT_EXPR conditional. */
717 if (var
== TREE_OPERAND (cond
, 0)
718 || TREE_CODE (TREE_OPERAND (cond
, 0)) == PLUS_EXPR
719 || TREE_CODE (TREE_OPERAND (cond
, 0)) == NOP_EXPR
)
721 /* If the predicate is of the form VAR COMP LIMIT, then we just
722 take LIMIT from the RHS and use the same comparison code. */
723 cond_code
= TREE_CODE (cond
);
724 limit
= TREE_OPERAND (cond
, 1);
725 op
= TREE_OPERAND (cond
, 0);
729 /* If the predicate is of the form LIMIT COMP VAR, then we need
730 to flip around the comparison code to create the proper range
732 cond_code
= swap_tree_comparison (TREE_CODE (cond
));
733 limit
= TREE_OPERAND (cond
, 0);
734 op
= TREE_OPERAND (cond
, 1);
736 extract_range_for_var_from_comparison_expr (var
, cond_code
, op
,
740 /* Extract range information from SSA name VAR and store it in VR. If
741 VAR has an interesting range, use it. Otherwise, create the
742 range [VAR, VAR] and return it. This is useful in situations where
743 we may have conditionals testing values of VARYING names. For
750 Even if y_5 is deemed VARYING, we can determine that x_3 > y_5 is
754 vr_values::extract_range_from_ssa_name (value_range
*vr
, tree var
)
756 const value_range
*var_vr
= get_value_range (var
);
758 if (!var_vr
->varying_p ())
759 vr
->deep_copy (var_vr
);
763 if (!vr
->undefined_p ())
764 vr
->equiv_add (var
, get_value_range (var
), &vrp_equiv_obstack
);
767 /* Extract range information from a binary expression OP0 CODE OP1 based on
768 the ranges of each of its operands with resulting type EXPR_TYPE.
769 The resulting range is stored in *VR. */
772 vr_values::extract_range_from_binary_expr (value_range
*vr
,
774 tree expr_type
, tree op0
, tree op1
)
776 /* Get value ranges for each operand. For constant operands, create
777 a new value range with the operand to simplify processing. */
778 value_range_base vr0
, vr1
;
779 if (TREE_CODE (op0
) == SSA_NAME
)
780 vr0
= *(get_value_range (op0
));
781 else if (is_gimple_min_invariant (op0
))
784 vr0
.set_varying (TREE_TYPE (op0
));
786 if (TREE_CODE (op1
) == SSA_NAME
)
787 vr1
= *(get_value_range (op1
));
788 else if (is_gimple_min_invariant (op1
))
791 vr1
.set_varying (TREE_TYPE (op1
));
793 /* If one argument is varying, we can sometimes still deduce a
794 range for the output: any + [3, +INF] is in [MIN+3, +INF]. */
795 if (INTEGRAL_TYPE_P (TREE_TYPE (op0
))
796 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0
)))
798 if (vr0
.varying_p () && !vr1
.varying_p ())
799 vr0
= value_range (VR_RANGE
,
800 vrp_val_min (expr_type
),
801 vrp_val_max (expr_type
));
802 else if (vr1
.varying_p () && !vr0
.varying_p ())
803 vr1
= value_range (VR_RANGE
,
804 vrp_val_min (expr_type
),
805 vrp_val_max (expr_type
));
808 range_fold_binary_expr (vr
, code
, expr_type
, &vr0
, &vr1
);
810 /* Set value_range for n in following sequence:
811 def = __builtin_memchr (arg, 0, sz)
813 Here the range for n can be set to [0, PTRDIFF_MAX - 1]. */
816 && code
== POINTER_DIFF_EXPR
817 && TREE_CODE (op0
) == SSA_NAME
818 && TREE_CODE (op1
) == SSA_NAME
)
820 tree op0_ptype
= TREE_TYPE (TREE_TYPE (op0
));
821 tree op1_ptype
= TREE_TYPE (TREE_TYPE (op1
));
822 gcall
*call_stmt
= NULL
;
824 if (TYPE_MODE (op0_ptype
) == TYPE_MODE (char_type_node
)
825 && TYPE_PRECISION (op0_ptype
) == TYPE_PRECISION (char_type_node
)
826 && TYPE_MODE (op1_ptype
) == TYPE_MODE (char_type_node
)
827 && TYPE_PRECISION (op1_ptype
) == TYPE_PRECISION (char_type_node
)
828 && (call_stmt
= dyn_cast
<gcall
*>(SSA_NAME_DEF_STMT (op0
)))
829 && gimple_call_builtin_p (call_stmt
, BUILT_IN_MEMCHR
)
830 && operand_equal_p (op0
, gimple_call_lhs (call_stmt
), 0)
831 && operand_equal_p (op1
, gimple_call_arg (call_stmt
, 0), 0)
832 && integer_zerop (gimple_call_arg (call_stmt
, 1)))
834 tree max
= vrp_val_max (ptrdiff_type_node
);
835 wide_int wmax
= wi::to_wide (max
, TYPE_PRECISION (TREE_TYPE (max
)));
836 tree range_min
= build_zero_cst (expr_type
);
837 tree range_max
= wide_int_to_tree (expr_type
, wmax
- 1);
838 vr
->set (VR_RANGE
, range_min
, range_max
);
843 /* Try harder for PLUS and MINUS if the range of one operand is symbolic
844 and based on the other operand, for example if it was deduced from a
845 symbolic comparison. When a bound of the range of the first operand
846 is invariant, we set the corresponding bound of the new range to INF
847 in order to avoid recursing on the range of the second operand. */
849 && (code
== PLUS_EXPR
|| code
== MINUS_EXPR
)
850 && TREE_CODE (op1
) == SSA_NAME
851 && vr0
.kind () == VR_RANGE
852 && symbolic_range_based_on_p (&vr0
, op1
))
854 const bool minus_p
= (code
== MINUS_EXPR
);
857 /* Try with VR0 and [-INF, OP1]. */
858 if (is_gimple_min_invariant (minus_p
? vr0
.max () : vr0
.min ()))
859 n_vr1
.set (VR_RANGE
, vrp_val_min (expr_type
), op1
);
861 /* Try with VR0 and [OP1, +INF]. */
862 else if (is_gimple_min_invariant (minus_p
? vr0
.min () : vr0
.max ()))
863 n_vr1
.set (VR_RANGE
, op1
, vrp_val_max (expr_type
));
865 /* Try with VR0 and [OP1, OP1]. */
867 n_vr1
.set (VR_RANGE
, op1
, op1
);
869 range_fold_binary_expr (vr
, code
, expr_type
, &vr0
, &n_vr1
);
873 && (code
== PLUS_EXPR
|| code
== MINUS_EXPR
)
874 && TREE_CODE (op0
) == SSA_NAME
875 && vr1
.kind () == VR_RANGE
876 && symbolic_range_based_on_p (&vr1
, op0
))
878 const bool minus_p
= (code
== MINUS_EXPR
);
881 /* Try with [-INF, OP0] and VR1. */
882 if (is_gimple_min_invariant (minus_p
? vr1
.max () : vr1
.min ()))
883 n_vr0
.set (VR_RANGE
, vrp_val_min (expr_type
), op0
);
885 /* Try with [OP0, +INF] and VR1. */
886 else if (is_gimple_min_invariant (minus_p
? vr1
.min (): vr1
.max ()))
887 n_vr0
.set (VR_RANGE
, op0
, vrp_val_max (expr_type
));
889 /* Try with [OP0, OP0] and VR1. */
893 range_fold_binary_expr (vr
, code
, expr_type
, &n_vr0
, &vr1
);
896 /* If we didn't derive a range for MINUS_EXPR, and
897 op1's range is ~[op0,op0] or vice-versa, then we
898 can derive a non-null range. This happens often for
899 pointer subtraction. */
901 && (code
== MINUS_EXPR
|| code
== POINTER_DIFF_EXPR
)
902 && TREE_CODE (op0
) == SSA_NAME
903 && ((vr0
.kind () == VR_ANTI_RANGE
905 && vr0
.min () == vr0
.max ())
906 || (vr1
.kind () == VR_ANTI_RANGE
908 && vr1
.min () == vr1
.max ())))
910 vr
->set_nonzero (expr_type
);
915 /* Extract range information from a unary expression CODE OP0 based on
916 the range of its operand with resulting type TYPE.
917 The resulting range is stored in *VR. */
920 vr_values::extract_range_from_unary_expr (value_range
*vr
, enum tree_code code
,
923 value_range_base vr0
;
925 /* Get value ranges for the operand. For constant operands, create
926 a new value range with the operand to simplify processing. */
927 if (TREE_CODE (op0
) == SSA_NAME
)
928 vr0
= *(get_value_range (op0
));
929 else if (is_gimple_min_invariant (op0
))
932 vr0
.set_varying (type
);
934 range_fold_unary_expr (vr
, code
, type
, &vr0
, TREE_TYPE (op0
));
938 /* Extract range information from a conditional expression STMT based on
939 the ranges of each of its operands and the expression code. */
942 vr_values::extract_range_from_cond_expr (value_range
*vr
, gassign
*stmt
)
944 /* Get value ranges for each operand. For constant operands, create
945 a new value range with the operand to simplify processing. */
946 tree op0
= gimple_assign_rhs2 (stmt
);
948 const value_range
*vr0
= &tem0
;
949 if (TREE_CODE (op0
) == SSA_NAME
)
950 vr0
= get_value_range (op0
);
951 else if (is_gimple_min_invariant (op0
))
954 tem0
.set_varying (TREE_TYPE (op0
));
956 tree op1
= gimple_assign_rhs3 (stmt
);
958 const value_range
*vr1
= &tem1
;
959 if (TREE_CODE (op1
) == SSA_NAME
)
960 vr1
= get_value_range (op1
);
961 else if (is_gimple_min_invariant (op1
))
964 tem1
.set_varying (TREE_TYPE (op1
));
966 /* The resulting value range is the union of the operand ranges */
972 /* Extract range information from a comparison expression EXPR based
973 on the range of its operand and the expression code. */
976 vr_values::extract_range_from_comparison (value_range
*vr
, enum tree_code code
,
977 tree type
, tree op0
, tree op1
)
982 val
= vrp_evaluate_conditional_warnv_with_ops (code
, op0
, op1
, false, &sop
,
986 /* Since this expression was found on the RHS of an assignment,
987 its type may be different from _Bool. Convert VAL to EXPR's
989 val
= fold_convert (type
, val
);
990 if (is_gimple_min_invariant (val
))
993 vr
->update (VR_RANGE
, val
, val
);
996 /* The result of a comparison is always true or false. */
997 set_value_range_to_truthvalue (vr
, type
);
1000 /* Helper function for simplify_internal_call_using_ranges and
1001 extract_range_basic. Return true if OP0 SUBCODE OP1 for
1002 SUBCODE {PLUS,MINUS,MULT}_EXPR is known to never overflow or
1003 always overflow. Set *OVF to true if it is known to always
1007 vr_values::check_for_binary_op_overflow (enum tree_code subcode
, tree type
,
1008 tree op0
, tree op1
, bool *ovf
)
1010 value_range_base vr0
, vr1
;
1011 if (TREE_CODE (op0
) == SSA_NAME
)
1012 vr0
= *get_value_range (op0
);
1013 else if (TREE_CODE (op0
) == INTEGER_CST
)
1016 vr0
.set_varying (TREE_TYPE (op0
));
1018 if (TREE_CODE (op1
) == SSA_NAME
)
1019 vr1
= *get_value_range (op1
);
1020 else if (TREE_CODE (op1
) == INTEGER_CST
)
1023 vr1
.set_varying (TREE_TYPE (op1
));
1025 tree vr0min
= vr0
.min (), vr0max
= vr0
.max ();
1026 tree vr1min
= vr1
.min (), vr1max
= vr1
.max ();
1027 if (!range_int_cst_p (&vr0
)
1028 || TREE_OVERFLOW (vr0min
)
1029 || TREE_OVERFLOW (vr0max
))
1031 vr0min
= vrp_val_min (TREE_TYPE (op0
));
1032 vr0max
= vrp_val_max (TREE_TYPE (op0
));
1034 if (!range_int_cst_p (&vr1
)
1035 || TREE_OVERFLOW (vr1min
)
1036 || TREE_OVERFLOW (vr1max
))
1038 vr1min
= vrp_val_min (TREE_TYPE (op1
));
1039 vr1max
= vrp_val_max (TREE_TYPE (op1
));
1041 *ovf
= arith_overflowed_p (subcode
, type
, vr0min
,
1042 subcode
== MINUS_EXPR
? vr1max
: vr1min
);
1043 if (arith_overflowed_p (subcode
, type
, vr0max
,
1044 subcode
== MINUS_EXPR
? vr1min
: vr1max
) != *ovf
)
1046 if (subcode
== MULT_EXPR
)
1048 if (arith_overflowed_p (subcode
, type
, vr0min
, vr1max
) != *ovf
1049 || arith_overflowed_p (subcode
, type
, vr0max
, vr1min
) != *ovf
)
1054 /* So far we found that there is an overflow on the boundaries.
1055 That doesn't prove that there is an overflow even for all values
1056 in between the boundaries. For that compute widest_int range
1057 of the result and see if it doesn't overlap the range of
1059 widest_int wmin
, wmax
;
1062 w
[0] = wi::to_widest (vr0min
);
1063 w
[1] = wi::to_widest (vr0max
);
1064 w
[2] = wi::to_widest (vr1min
);
1065 w
[3] = wi::to_widest (vr1max
);
1066 for (i
= 0; i
< 4; i
++)
1072 wt
= wi::add (w
[i
& 1], w
[2 + (i
& 2) / 2]);
1075 wt
= wi::sub (w
[i
& 1], w
[2 + (i
& 2) / 2]);
1078 wt
= wi::mul (w
[i
& 1], w
[2 + (i
& 2) / 2]);
1090 wmin
= wi::smin (wmin
, wt
);
1091 wmax
= wi::smax (wmax
, wt
);
1094 /* The result of op0 CODE op1 is known to be in range
1096 widest_int wtmin
= wi::to_widest (vrp_val_min (type
));
1097 widest_int wtmax
= wi::to_widest (vrp_val_max (type
));
1098 /* If all values in [wmin, wmax] are smaller than
1099 [wtmin, wtmax] or all are larger than [wtmin, wtmax],
1100 the arithmetic operation will always overflow. */
1101 if (wmax
< wtmin
|| wmin
> wtmax
)
1108 /* Try to derive a nonnegative or nonzero range out of STMT relying
1109 primarily on generic routines in fold in conjunction with range data.
1110 Store the result in *VR */
1113 vr_values::extract_range_basic (value_range
*vr
, gimple
*stmt
)
1116 tree type
= gimple_expr_type (stmt
);
1118 if (is_gimple_call (stmt
))
1121 int mini
, maxi
, zerov
= 0, prec
;
1122 enum tree_code subcode
= ERROR_MARK
;
1123 combined_fn cfn
= gimple_call_combined_fn (stmt
);
1124 scalar_int_mode mode
;
1128 case CFN_BUILT_IN_CONSTANT_P
:
1129 /* Resolve calls to __builtin_constant_p after inlining. */
1130 if (cfun
->after_inlining
)
1132 vr
->set_zero (type
);
1137 /* Both __builtin_ffs* and __builtin_popcount return
1141 arg
= gimple_call_arg (stmt
, 0);
1142 prec
= TYPE_PRECISION (TREE_TYPE (arg
));
1145 if (TREE_CODE (arg
) == SSA_NAME
)
1147 const value_range
*vr0
= get_value_range (arg
);
1148 /* If arg is non-zero, then ffs or popcount are non-zero. */
1149 if (range_includes_zero_p (vr0
) == 0)
1151 /* If some high bits are known to be zero,
1152 we can decrease the maximum. */
1153 if (vr0
->kind () == VR_RANGE
1154 && TREE_CODE (vr0
->max ()) == INTEGER_CST
1155 && !operand_less_p (vr0
->min (),
1156 build_zero_cst (TREE_TYPE (vr0
->min ()))))
1157 maxi
= tree_floor_log2 (vr0
->max ()) + 1;
1160 /* __builtin_parity* returns [0, 1]. */
1165 /* __builtin_c[lt]z* return [0, prec-1], except for
1166 when the argument is 0, but that is undefined behavior.
1167 On many targets where the CLZ RTL or optab value is defined
1168 for 0 the value is prec, so include that in the range
1171 arg
= gimple_call_arg (stmt
, 0);
1172 prec
= TYPE_PRECISION (TREE_TYPE (arg
));
1175 mode
= SCALAR_INT_TYPE_MODE (TREE_TYPE (arg
));
1176 if (optab_handler (clz_optab
, mode
) != CODE_FOR_nothing
1177 && CLZ_DEFINED_VALUE_AT_ZERO (mode
, zerov
)
1178 /* Handle only the single common value. */
1180 /* Magic value to give up, unless vr0 proves
1183 if (TREE_CODE (arg
) == SSA_NAME
)
1185 const value_range
*vr0
= get_value_range (arg
);
1186 /* From clz of VR_RANGE minimum we can compute
1188 if (vr0
->kind () == VR_RANGE
1189 && TREE_CODE (vr0
->min ()) == INTEGER_CST
)
1191 maxi
= prec
- 1 - tree_floor_log2 (vr0
->min ());
1195 else if (vr0
->kind () == VR_ANTI_RANGE
1196 && integer_zerop (vr0
->min ()))
1203 /* From clz of VR_RANGE maximum we can compute
1205 if (vr0
->kind () == VR_RANGE
1206 && TREE_CODE (vr0
->max ()) == INTEGER_CST
)
1208 mini
= prec
- 1 - tree_floor_log2 (vr0
->max ());
1216 /* __builtin_ctz* return [0, prec-1], except for
1217 when the argument is 0, but that is undefined behavior.
1218 If there is a ctz optab for this mode and
1219 CTZ_DEFINED_VALUE_AT_ZERO, include that in the range,
1220 otherwise just assume 0 won't be seen. */
1222 arg
= gimple_call_arg (stmt
, 0);
1223 prec
= TYPE_PRECISION (TREE_TYPE (arg
));
1226 mode
= SCALAR_INT_TYPE_MODE (TREE_TYPE (arg
));
1227 if (optab_handler (ctz_optab
, mode
) != CODE_FOR_nothing
1228 && CTZ_DEFINED_VALUE_AT_ZERO (mode
, zerov
))
1230 /* Handle only the two common values. */
1233 else if (zerov
== prec
)
1236 /* Magic value to give up, unless vr0 proves
1240 if (TREE_CODE (arg
) == SSA_NAME
)
1242 const value_range
*vr0
= get_value_range (arg
);
1243 /* If arg is non-zero, then use [0, prec - 1]. */
1244 if ((vr0
->kind () == VR_RANGE
1245 && integer_nonzerop (vr0
->min ()))
1246 || (vr0
->kind () == VR_ANTI_RANGE
1247 && integer_zerop (vr0
->min ())))
1252 /* If some high bits are known to be zero,
1253 we can decrease the result maximum. */
1254 if (vr0
->kind () == VR_RANGE
1255 && TREE_CODE (vr0
->max ()) == INTEGER_CST
)
1257 maxi
= tree_floor_log2 (vr0
->max ());
1258 /* For vr0 [0, 0] give up. */
1266 /* __builtin_clrsb* returns [0, prec-1]. */
1268 arg
= gimple_call_arg (stmt
, 0);
1269 prec
= TYPE_PRECISION (TREE_TYPE (arg
));
1274 vr
->set (VR_RANGE
, build_int_cst (type
, mini
),
1275 build_int_cst (type
, maxi
));
1277 case CFN_UBSAN_CHECK_ADD
:
1278 subcode
= PLUS_EXPR
;
1280 case CFN_UBSAN_CHECK_SUB
:
1281 subcode
= MINUS_EXPR
;
1283 case CFN_UBSAN_CHECK_MUL
:
1284 subcode
= MULT_EXPR
;
1286 case CFN_GOACC_DIM_SIZE
:
1287 case CFN_GOACC_DIM_POS
:
1288 /* Optimizing these two internal functions helps the loop
1289 optimizer eliminate outer comparisons. Size is [1,N]
1290 and pos is [0,N-1]. */
1292 bool is_pos
= cfn
== CFN_GOACC_DIM_POS
;
1293 int axis
= oacc_get_ifn_dim_arg (stmt
);
1294 int size
= oacc_get_fn_dim_size (current_function_decl
, axis
);
1297 /* If it's dynamic, the backend might know a hardware
1299 size
= targetm
.goacc
.dim_limit (axis
);
1301 tree type
= TREE_TYPE (gimple_call_lhs (stmt
));
1302 vr
->set(VR_RANGE
, build_int_cst (type
, is_pos
? 0 : 1),
1304 ? build_int_cst (type
, size
- is_pos
) : vrp_val_max (type
));
1307 case CFN_BUILT_IN_STRLEN
:
1308 if (tree lhs
= gimple_call_lhs (stmt
))
1309 if (ptrdiff_type_node
1310 && (TYPE_PRECISION (ptrdiff_type_node
)
1311 == TYPE_PRECISION (TREE_TYPE (lhs
))))
1313 tree type
= TREE_TYPE (lhs
);
1314 tree max
= vrp_val_max (ptrdiff_type_node
);
1315 wide_int wmax
= wi::to_wide (max
, TYPE_PRECISION (TREE_TYPE (max
)));
1316 tree range_min
= build_zero_cst (type
);
1317 /* To account for the terminating NUL, the maximum length
1318 is one less than the maximum array size, which in turn
1319 is one less than PTRDIFF_MAX (or SIZE_MAX where it's
1320 smaller than the former type).
1321 FIXME: Use max_object_size() - 1 here. */
1322 tree range_max
= wide_int_to_tree (type
, wmax
- 2);
1323 vr
->set (VR_RANGE
, range_min
, range_max
);
1330 if (subcode
!= ERROR_MARK
)
1332 bool saved_flag_wrapv
= flag_wrapv
;
1333 /* Pretend the arithmetics is wrapping. If there is
1334 any overflow, we'll complain, but will actually do
1335 wrapping operation. */
1337 extract_range_from_binary_expr (vr
, subcode
, type
,
1338 gimple_call_arg (stmt
, 0),
1339 gimple_call_arg (stmt
, 1));
1340 flag_wrapv
= saved_flag_wrapv
;
1342 /* If for both arguments vrp_valueize returned non-NULL,
1343 this should have been already folded and if not, it
1344 wasn't folded because of overflow. Avoid removing the
1345 UBSAN_CHECK_* calls in that case. */
1346 if (vr
->kind () == VR_RANGE
1347 && (vr
->min () == vr
->max ()
1348 || operand_equal_p (vr
->min (), vr
->max (), 0)))
1349 vr
->set_varying (vr
->type ());
1353 /* Handle extraction of the two results (result of arithmetics and
1354 a flag whether arithmetics overflowed) from {ADD,SUB,MUL}_OVERFLOW
1355 internal function. Similarly from ATOMIC_COMPARE_EXCHANGE. */
1356 else if (is_gimple_assign (stmt
)
1357 && (gimple_assign_rhs_code (stmt
) == REALPART_EXPR
1358 || gimple_assign_rhs_code (stmt
) == IMAGPART_EXPR
)
1359 && INTEGRAL_TYPE_P (type
))
1361 enum tree_code code
= gimple_assign_rhs_code (stmt
);
1362 tree op
= gimple_assign_rhs1 (stmt
);
1363 if (TREE_CODE (op
) == code
&& TREE_CODE (TREE_OPERAND (op
, 0)) == SSA_NAME
)
1365 gimple
*g
= SSA_NAME_DEF_STMT (TREE_OPERAND (op
, 0));
1366 if (is_gimple_call (g
) && gimple_call_internal_p (g
))
1368 enum tree_code subcode
= ERROR_MARK
;
1369 switch (gimple_call_internal_fn (g
))
1371 case IFN_ADD_OVERFLOW
:
1372 subcode
= PLUS_EXPR
;
1374 case IFN_SUB_OVERFLOW
:
1375 subcode
= MINUS_EXPR
;
1377 case IFN_MUL_OVERFLOW
:
1378 subcode
= MULT_EXPR
;
1380 case IFN_ATOMIC_COMPARE_EXCHANGE
:
1381 if (code
== IMAGPART_EXPR
)
1383 /* This is the boolean return value whether compare and
1384 exchange changed anything or not. */
1385 vr
->set (VR_RANGE
, build_int_cst (type
, 0),
1386 build_int_cst (type
, 1));
1393 if (subcode
!= ERROR_MARK
)
1395 tree op0
= gimple_call_arg (g
, 0);
1396 tree op1
= gimple_call_arg (g
, 1);
1397 if (code
== IMAGPART_EXPR
)
1400 if (check_for_binary_op_overflow (subcode
, type
,
1402 vr
->set (build_int_cst (type
, ovf
));
1403 else if (TYPE_PRECISION (type
) == 1
1404 && !TYPE_UNSIGNED (type
))
1405 vr
->set_varying (type
);
1407 vr
->set (VR_RANGE
, build_int_cst (type
, 0),
1408 build_int_cst (type
, 1));
1410 else if (types_compatible_p (type
, TREE_TYPE (op0
))
1411 && types_compatible_p (type
, TREE_TYPE (op1
)))
1413 bool saved_flag_wrapv
= flag_wrapv
;
1414 /* Pretend the arithmetics is wrapping. If there is
1415 any overflow, IMAGPART_EXPR will be set. */
1417 extract_range_from_binary_expr (vr
, subcode
, type
,
1419 flag_wrapv
= saved_flag_wrapv
;
1423 value_range vr0
, vr1
;
1424 bool saved_flag_wrapv
= flag_wrapv
;
1425 /* Pretend the arithmetics is wrapping. If there is
1426 any overflow, IMAGPART_EXPR will be set. */
1428 extract_range_from_unary_expr (&vr0
, NOP_EXPR
,
1430 extract_range_from_unary_expr (&vr1
, NOP_EXPR
,
1432 range_fold_binary_expr (vr
, subcode
, type
, &vr0
, &vr1
);
1433 flag_wrapv
= saved_flag_wrapv
;
1440 if (INTEGRAL_TYPE_P (type
)
1441 && gimple_stmt_nonnegative_warnv_p (stmt
, &sop
))
1442 set_value_range_to_nonnegative (vr
, type
);
1443 else if (vrp_stmt_computes_nonzero (stmt
))
1445 vr
->set_nonzero (type
);
1449 vr
->set_varying (type
);
1453 /* Try to compute a useful range out of assignment STMT and store it
1457 vr_values::extract_range_from_assignment (value_range
*vr
, gassign
*stmt
)
1459 enum tree_code code
= gimple_assign_rhs_code (stmt
);
1461 if (code
== ASSERT_EXPR
)
1462 extract_range_from_assert (vr
, gimple_assign_rhs1 (stmt
));
1463 else if (code
== SSA_NAME
)
1464 extract_range_from_ssa_name (vr
, gimple_assign_rhs1 (stmt
));
1465 else if (TREE_CODE_CLASS (code
) == tcc_binary
)
1466 extract_range_from_binary_expr (vr
, gimple_assign_rhs_code (stmt
),
1467 gimple_expr_type (stmt
),
1468 gimple_assign_rhs1 (stmt
),
1469 gimple_assign_rhs2 (stmt
));
1470 else if (TREE_CODE_CLASS (code
) == tcc_unary
)
1471 extract_range_from_unary_expr (vr
, gimple_assign_rhs_code (stmt
),
1472 gimple_expr_type (stmt
),
1473 gimple_assign_rhs1 (stmt
));
1474 else if (code
== COND_EXPR
)
1475 extract_range_from_cond_expr (vr
, stmt
);
1476 else if (TREE_CODE_CLASS (code
) == tcc_comparison
)
1477 extract_range_from_comparison (vr
, gimple_assign_rhs_code (stmt
),
1478 gimple_expr_type (stmt
),
1479 gimple_assign_rhs1 (stmt
),
1480 gimple_assign_rhs2 (stmt
));
1481 else if (get_gimple_rhs_class (code
) == GIMPLE_SINGLE_RHS
1482 && is_gimple_min_invariant (gimple_assign_rhs1 (stmt
)))
1483 vr
->set (gimple_assign_rhs1 (stmt
));
1485 vr
->set_varying (TREE_TYPE (gimple_assign_lhs (stmt
)));
1487 if (vr
->varying_p ())
1488 extract_range_basic (vr
, stmt
);
1491 /* Given two numeric value ranges VR0, VR1 and a comparison code COMP:
1493 - Return BOOLEAN_TRUE_NODE if VR0 COMP VR1 always returns true for
1494 all the values in the ranges.
1496 - Return BOOLEAN_FALSE_NODE if the comparison always returns false.
1498 - Return NULL_TREE if it is not always possible to determine the
1499 value of the comparison.
1501 Also set *STRICT_OVERFLOW_P to indicate whether comparision evaluation
1502 assumed signed overflow is undefined. */
1506 compare_ranges (enum tree_code comp
, const value_range
*vr0
,
1507 const value_range
*vr1
, bool *strict_overflow_p
)
1509 /* VARYING or UNDEFINED ranges cannot be compared. */
1510 if (vr0
->varying_p ()
1511 || vr0
->undefined_p ()
1512 || vr1
->varying_p ()
1513 || vr1
->undefined_p ())
1516 /* Anti-ranges need to be handled separately. */
1517 if (vr0
->kind () == VR_ANTI_RANGE
|| vr1
->kind () == VR_ANTI_RANGE
)
1519 /* If both are anti-ranges, then we cannot compute any
1521 if (vr0
->kind () == VR_ANTI_RANGE
&& vr1
->kind () == VR_ANTI_RANGE
)
1524 /* These comparisons are never statically computable. */
1531 /* Equality can be computed only between a range and an
1532 anti-range. ~[VAL1, VAL2] == [VAL1, VAL2] is always false. */
1533 if (vr0
->kind () == VR_RANGE
)
1534 /* To simplify processing, make VR0 the anti-range. */
1535 std::swap (vr0
, vr1
);
1537 gcc_assert (comp
== NE_EXPR
|| comp
== EQ_EXPR
);
1539 if (compare_values_warnv (vr0
->min (), vr1
->min (), strict_overflow_p
) == 0
1540 && compare_values_warnv (vr0
->max (), vr1
->max (), strict_overflow_p
) == 0)
1541 return (comp
== NE_EXPR
) ? boolean_true_node
: boolean_false_node
;
1546 /* Simplify processing. If COMP is GT_EXPR or GE_EXPR, switch the
1547 operands around and change the comparison code. */
1548 if (comp
== GT_EXPR
|| comp
== GE_EXPR
)
1550 comp
= (comp
== GT_EXPR
) ? LT_EXPR
: LE_EXPR
;
1551 std::swap (vr0
, vr1
);
1554 if (comp
== EQ_EXPR
)
1556 /* Equality may only be computed if both ranges represent
1557 exactly one value. */
1558 if (compare_values_warnv (vr0
->min (), vr0
->max (), strict_overflow_p
) == 0
1559 && compare_values_warnv (vr1
->min (), vr1
->max (), strict_overflow_p
) == 0)
1561 int cmp_min
= compare_values_warnv (vr0
->min (), vr1
->min (),
1563 int cmp_max
= compare_values_warnv (vr0
->max (), vr1
->max (),
1565 if (cmp_min
== 0 && cmp_max
== 0)
1566 return boolean_true_node
;
1567 else if (cmp_min
!= -2 && cmp_max
!= -2)
1568 return boolean_false_node
;
1570 /* If [V0_MIN, V1_MAX] < [V1_MIN, V1_MAX] then V0 != V1. */
1571 else if (compare_values_warnv (vr0
->min (), vr1
->max (),
1572 strict_overflow_p
) == 1
1573 || compare_values_warnv (vr1
->min (), vr0
->max (),
1574 strict_overflow_p
) == 1)
1575 return boolean_false_node
;
1579 else if (comp
== NE_EXPR
)
1583 /* If VR0 is completely to the left or completely to the right
1584 of VR1, they are always different. Notice that we need to
1585 make sure that both comparisons yield similar results to
1586 avoid comparing values that cannot be compared at
1588 cmp1
= compare_values_warnv (vr0
->max (), vr1
->min (), strict_overflow_p
);
1589 cmp2
= compare_values_warnv (vr0
->min (), vr1
->max (), strict_overflow_p
);
1590 if ((cmp1
== -1 && cmp2
== -1) || (cmp1
== 1 && cmp2
== 1))
1591 return boolean_true_node
;
1593 /* If VR0 and VR1 represent a single value and are identical,
1595 else if (compare_values_warnv (vr0
->min (), vr0
->max (),
1596 strict_overflow_p
) == 0
1597 && compare_values_warnv (vr1
->min (), vr1
->max (),
1598 strict_overflow_p
) == 0
1599 && compare_values_warnv (vr0
->min (), vr1
->min (),
1600 strict_overflow_p
) == 0
1601 && compare_values_warnv (vr0
->max (), vr1
->max (),
1602 strict_overflow_p
) == 0)
1603 return boolean_false_node
;
1605 /* Otherwise, they may or may not be different. */
1609 else if (comp
== LT_EXPR
|| comp
== LE_EXPR
)
1613 /* If VR0 is to the left of VR1, return true. */
1614 tst
= compare_values_warnv (vr0
->max (), vr1
->min (), strict_overflow_p
);
1615 if ((comp
== LT_EXPR
&& tst
== -1)
1616 || (comp
== LE_EXPR
&& (tst
== -1 || tst
== 0)))
1617 return boolean_true_node
;
1619 /* If VR0 is to the right of VR1, return false. */
1620 tst
= compare_values_warnv (vr0
->min (), vr1
->max (), strict_overflow_p
);
1621 if ((comp
== LT_EXPR
&& (tst
== 0 || tst
== 1))
1622 || (comp
== LE_EXPR
&& tst
== 1))
1623 return boolean_false_node
;
1625 /* Otherwise, we don't know. */
1632 /* Given a value range VR, a value VAL and a comparison code COMP, return
1633 BOOLEAN_TRUE_NODE if VR COMP VAL always returns true for all the
1634 values in VR. Return BOOLEAN_FALSE_NODE if the comparison
1635 always returns false. Return NULL_TREE if it is not always
1636 possible to determine the value of the comparison. Also set
1637 *STRICT_OVERFLOW_P to indicate whether comparision evaluation
1638 assumed signed overflow is undefined. */
1641 compare_range_with_value (enum tree_code comp
, const value_range
*vr
, tree val
,
1642 bool *strict_overflow_p
)
1644 if (vr
->varying_p () || vr
->undefined_p ())
1647 /* Anti-ranges need to be handled separately. */
1648 if (vr
->kind () == VR_ANTI_RANGE
)
1650 /* For anti-ranges, the only predicates that we can compute at
1651 compile time are equality and inequality. */
1658 /* ~[VAL_1, VAL_2] OP VAL is known if VAL_1 <= VAL <= VAL_2. */
1659 if (!vr
->may_contain_p (val
))
1660 return (comp
== NE_EXPR
) ? boolean_true_node
: boolean_false_node
;
1665 if (comp
== EQ_EXPR
)
1667 /* EQ_EXPR may only be computed if VR represents exactly
1669 if (compare_values_warnv (vr
->min (), vr
->max (), strict_overflow_p
) == 0)
1671 int cmp
= compare_values_warnv (vr
->min (), val
, strict_overflow_p
);
1673 return boolean_true_node
;
1674 else if (cmp
== -1 || cmp
== 1 || cmp
== 2)
1675 return boolean_false_node
;
1677 else if (compare_values_warnv (val
, vr
->min (), strict_overflow_p
) == -1
1678 || compare_values_warnv (vr
->max (), val
, strict_overflow_p
) == -1)
1679 return boolean_false_node
;
1683 else if (comp
== NE_EXPR
)
1685 /* If VAL is not inside VR, then they are always different. */
1686 if (compare_values_warnv (vr
->max (), val
, strict_overflow_p
) == -1
1687 || compare_values_warnv (vr
->min (), val
, strict_overflow_p
) == 1)
1688 return boolean_true_node
;
1690 /* If VR represents exactly one value equal to VAL, then return
1692 if (compare_values_warnv (vr
->min (), vr
->max (), strict_overflow_p
) == 0
1693 && compare_values_warnv (vr
->min (), val
, strict_overflow_p
) == 0)
1694 return boolean_false_node
;
1696 /* Otherwise, they may or may not be different. */
1699 else if (comp
== LT_EXPR
|| comp
== LE_EXPR
)
1703 /* If VR is to the left of VAL, return true. */
1704 tst
= compare_values_warnv (vr
->max (), val
, strict_overflow_p
);
1705 if ((comp
== LT_EXPR
&& tst
== -1)
1706 || (comp
== LE_EXPR
&& (tst
== -1 || tst
== 0)))
1707 return boolean_true_node
;
1709 /* If VR is to the right of VAL, return false. */
1710 tst
= compare_values_warnv (vr
->min (), val
, strict_overflow_p
);
1711 if ((comp
== LT_EXPR
&& (tst
== 0 || tst
== 1))
1712 || (comp
== LE_EXPR
&& tst
== 1))
1713 return boolean_false_node
;
1715 /* Otherwise, we don't know. */
1718 else if (comp
== GT_EXPR
|| comp
== GE_EXPR
)
1722 /* If VR is to the right of VAL, return true. */
1723 tst
= compare_values_warnv (vr
->min (), val
, strict_overflow_p
);
1724 if ((comp
== GT_EXPR
&& tst
== 1)
1725 || (comp
== GE_EXPR
&& (tst
== 0 || tst
== 1)))
1726 return boolean_true_node
;
1728 /* If VR is to the left of VAL, return false. */
1729 tst
= compare_values_warnv (vr
->max (), val
, strict_overflow_p
);
1730 if ((comp
== GT_EXPR
&& (tst
== -1 || tst
== 0))
1731 || (comp
== GE_EXPR
&& tst
== -1))
1732 return boolean_false_node
;
1734 /* Otherwise, we don't know. */
1740 /* Given a range VR, a LOOP and a variable VAR, determine whether it
1741 would be profitable to adjust VR using scalar evolution information
1742 for VAR. If so, update VR with the new limits. */
1745 vr_values::adjust_range_with_scev (value_range
*vr
, class loop
*loop
,
1746 gimple
*stmt
, tree var
)
1748 tree init
, step
, chrec
, tmin
, tmax
, min
, max
, type
, tem
;
1749 enum ev_direction dir
;
1751 /* TODO. Don't adjust anti-ranges. An anti-range may provide
1752 better opportunities than a regular range, but I'm not sure. */
1753 if (vr
->kind () == VR_ANTI_RANGE
)
1756 chrec
= instantiate_parameters (loop
, analyze_scalar_evolution (loop
, var
));
1758 /* Like in PR19590, scev can return a constant function. */
1759 if (is_gimple_min_invariant (chrec
))
1765 if (TREE_CODE (chrec
) != POLYNOMIAL_CHREC
)
1768 init
= initial_condition_in_loop_num (chrec
, loop
->num
);
1769 tem
= op_with_constant_singleton_value_range (init
);
1772 step
= evolution_part_in_loop_num (chrec
, loop
->num
);
1773 tem
= op_with_constant_singleton_value_range (step
);
1777 /* If STEP is symbolic, we can't know whether INIT will be the
1778 minimum or maximum value in the range. Also, unless INIT is
1779 a simple expression, compare_values and possibly other functions
1780 in tree-vrp won't be able to handle it. */
1781 if (step
== NULL_TREE
1782 || !is_gimple_min_invariant (step
)
1783 || !valid_value_p (init
))
1786 dir
= scev_direction (chrec
);
1787 if (/* Do not adjust ranges if we do not know whether the iv increases
1788 or decreases, ... */
1789 dir
== EV_DIR_UNKNOWN
1790 /* ... or if it may wrap. */
1791 || scev_probably_wraps_p (NULL_TREE
, init
, step
, stmt
,
1792 get_chrec_loop (chrec
), true))
1795 type
= TREE_TYPE (var
);
1796 if (POINTER_TYPE_P (type
) || !TYPE_MIN_VALUE (type
))
1797 tmin
= lower_bound_in_type (type
, type
);
1799 tmin
= TYPE_MIN_VALUE (type
);
1800 if (POINTER_TYPE_P (type
) || !TYPE_MAX_VALUE (type
))
1801 tmax
= upper_bound_in_type (type
, type
);
1803 tmax
= TYPE_MAX_VALUE (type
);
1805 /* Try to use estimated number of iterations for the loop to constrain the
1806 final value in the evolution. */
1807 if (TREE_CODE (step
) == INTEGER_CST
1808 && is_gimple_val (init
)
1809 && (TREE_CODE (init
) != SSA_NAME
1810 || get_value_range (init
)->kind () == VR_RANGE
))
1814 /* We are only entering here for loop header PHI nodes, so using
1815 the number of latch executions is the correct thing to use. */
1816 if (max_loop_iterations (loop
, &nit
))
1819 signop sgn
= TYPE_SIGN (TREE_TYPE (step
));
1820 wi::overflow_type overflow
;
1822 widest_int wtmp
= wi::mul (wi::to_widest (step
), nit
, sgn
,
1824 /* If the multiplication overflowed we can't do a meaningful
1825 adjustment. Likewise if the result doesn't fit in the type
1826 of the induction variable. For a signed type we have to
1827 check whether the result has the expected signedness which
1828 is that of the step as number of iterations is unsigned. */
1830 && wi::fits_to_tree_p (wtmp
, TREE_TYPE (init
))
1832 || wi::gts_p (wtmp
, 0) == wi::gts_p (wi::to_wide (step
), 0)))
1834 tem
= wide_int_to_tree (TREE_TYPE (init
), wtmp
);
1835 extract_range_from_binary_expr (&maxvr
, PLUS_EXPR
,
1836 TREE_TYPE (init
), init
, tem
);
1837 /* Likewise if the addition did. */
1838 if (maxvr
.kind () == VR_RANGE
)
1840 value_range_base initvr
;
1842 if (TREE_CODE (init
) == SSA_NAME
)
1843 initvr
= *(get_value_range (init
));
1844 else if (is_gimple_min_invariant (init
))
1849 /* Check if init + nit * step overflows. Though we checked
1850 scev {init, step}_loop doesn't wrap, it is not enough
1851 because the loop may exit immediately. Overflow could
1852 happen in the plus expression in this case. */
1853 if ((dir
== EV_DIR_DECREASES
1854 && compare_values (maxvr
.min (), initvr
.min ()) != -1)
1855 || (dir
== EV_DIR_GROWS
1856 && compare_values (maxvr
.max (), initvr
.max ()) != 1))
1859 tmin
= maxvr
.min ();
1860 tmax
= maxvr
.max ();
1866 if (vr
->varying_p () || vr
->undefined_p ())
1871 /* For VARYING or UNDEFINED ranges, just about anything we get
1872 from scalar evolutions should be better. */
1874 if (dir
== EV_DIR_DECREASES
)
1879 else if (vr
->kind () == VR_RANGE
)
1884 if (dir
== EV_DIR_DECREASES
)
1886 /* INIT is the maximum value. If INIT is lower than VR->MAX ()
1887 but no smaller than VR->MIN (), set VR->MAX () to INIT. */
1888 if (compare_values (init
, max
) == -1)
1891 /* According to the loop information, the variable does not
1893 if (compare_values (min
, tmin
) == -1)
1899 /* If INIT is bigger than VR->MIN (), set VR->MIN () to INIT. */
1900 if (compare_values (init
, min
) == 1)
1903 if (compare_values (tmax
, max
) == -1)
1910 /* If we just created an invalid range with the minimum
1911 greater than the maximum, we fail conservatively.
1912 This should happen only in unreachable
1913 parts of code, or for invalid programs. */
1914 if (compare_values (min
, max
) == 1)
1917 /* Even for valid range info, sometimes overflow flag will leak in.
1918 As GIMPLE IL should have no constants with TREE_OVERFLOW set, we
1920 if (TREE_OVERFLOW_P (min
))
1921 min
= drop_tree_overflow (min
);
1922 if (TREE_OVERFLOW_P (max
))
1923 max
= drop_tree_overflow (max
);
1925 vr
->update (VR_RANGE
, min
, max
);
1928 /* Dump value ranges of all SSA_NAMEs to FILE. */
1931 vr_values::dump_all_value_ranges (FILE *file
)
1935 for (i
= 0; i
< num_vr_values
; i
++)
1939 print_generic_expr (file
, ssa_name (i
));
1940 fprintf (file
, ": ");
1941 dump_value_range (file
, vr_value
[i
]);
1942 fprintf (file
, "\n");
1946 fprintf (file
, "\n");
1949 /* Initialize VRP lattice. */
1951 vr_values::vr_values () : vrp_value_range_pool ("Tree VRP value ranges")
1953 values_propagated
= false;
1954 num_vr_values
= num_ssa_names
* 2;
1955 vr_value
= XCNEWVEC (value_range
*, num_vr_values
);
1956 vr_phi_edge_counts
= XCNEWVEC (int, num_ssa_names
);
1957 bitmap_obstack_initialize (&vrp_equiv_obstack
);
1958 to_remove_edges
= vNULL
;
1959 to_update_switch_stmts
= vNULL
;
1962 /* Free VRP lattice. */
1964 vr_values::~vr_values ()
1966 /* Free allocated memory. */
1968 free (vr_phi_edge_counts
);
1969 bitmap_obstack_release (&vrp_equiv_obstack
);
1970 vrp_value_range_pool
.release ();
1972 /* So that we can distinguish between VRP data being available
1973 and not available. */
1975 vr_phi_edge_counts
= NULL
;
1977 /* If there are entries left in TO_REMOVE_EDGES or TO_UPDATE_SWITCH_STMTS
1978 then an EVRP client did not clean up properly. Catch it now rather
1979 than seeing something more obscure later. */
1980 gcc_assert (to_remove_edges
.is_empty ()
1981 && to_update_switch_stmts
.is_empty ());
1986 static class vr_values
*x_vr_values
;
1988 /* Return the singleton value-range for NAME or NAME. */
1991 vrp_valueize (tree name
)
1993 if (TREE_CODE (name
) == SSA_NAME
)
1995 const value_range
*vr
= x_vr_values
->get_value_range (name
);
1996 if (vr
->kind () == VR_RANGE
1997 && (TREE_CODE (vr
->min ()) == SSA_NAME
1998 || is_gimple_min_invariant (vr
->min ()))
1999 && vrp_operand_equal_p (vr
->min (), vr
->max ()))
2005 /* Return the singleton value-range for NAME if that is a constant
2006 but signal to not follow SSA edges. */
2009 vrp_valueize_1 (tree name
)
2011 if (TREE_CODE (name
) == SSA_NAME
)
2013 /* If the definition may be simulated again we cannot follow
2014 this SSA edge as the SSA propagator does not necessarily
2015 re-visit the use. */
2016 gimple
*def_stmt
= SSA_NAME_DEF_STMT (name
);
2017 if (!gimple_nop_p (def_stmt
)
2018 && prop_simulate_again_p (def_stmt
))
2020 const value_range
*vr
= x_vr_values
->get_value_range (name
);
2022 if (vr
->singleton_p (&singleton
))
2028 /* Given STMT, an assignment or call, return its LHS if the type
2029 of the LHS is suitable for VRP analysis, else return NULL_TREE. */
2032 get_output_for_vrp (gimple
*stmt
)
2034 if (!is_gimple_assign (stmt
) && !is_gimple_call (stmt
))
2037 /* We only keep track of ranges in integral and pointer types. */
2038 tree lhs
= gimple_get_lhs (stmt
);
2039 if (TREE_CODE (lhs
) == SSA_NAME
2040 && ((INTEGRAL_TYPE_P (TREE_TYPE (lhs
))
2041 /* It is valid to have NULL MIN/MAX values on a type. See
2042 build_range_type. */
2043 && TYPE_MIN_VALUE (TREE_TYPE (lhs
))
2044 && TYPE_MAX_VALUE (TREE_TYPE (lhs
)))
2045 || POINTER_TYPE_P (TREE_TYPE (lhs
))))
2051 /* Visit assignment STMT. If it produces an interesting range, record
2052 the range in VR and set LHS to OUTPUT_P. */
2055 vr_values::vrp_visit_assignment_or_call (gimple
*stmt
, tree
*output_p
,
2058 tree lhs
= get_output_for_vrp (stmt
);
2061 /* We only keep track of ranges in integral and pointer types. */
2064 enum gimple_code code
= gimple_code (stmt
);
2066 /* Try folding the statement to a constant first. */
2068 tree tem
= gimple_fold_stmt_to_constant_1 (stmt
, vrp_valueize
,
2073 if (TREE_CODE (tem
) == SSA_NAME
2074 && (SSA_NAME_IS_DEFAULT_DEF (tem
)
2075 || ! prop_simulate_again_p (SSA_NAME_DEF_STMT (tem
))))
2077 extract_range_from_ssa_name (vr
, tem
);
2080 else if (is_gimple_min_invariant (tem
))
2086 /* Then dispatch to value-range extracting functions. */
2087 if (code
== GIMPLE_CALL
)
2088 extract_range_basic (vr
, stmt
);
2090 extract_range_from_assignment (vr
, as_a
<gassign
*> (stmt
));
2094 /* Helper that gets the value range of the SSA_NAME with version I
2095 or a symbolic range containing the SSA_NAME only if the value range
2096 is varying or undefined. Uses TEM as storage for the alternate range. */
2099 vr_values::get_vr_for_comparison (int i
, value_range
*tem
)
2101 /* Shallow-copy equiv bitmap. */
2102 const value_range
*vr
= get_value_range (ssa_name (i
));
2104 /* If name N_i does not have a valid range, use N_i as its own
2105 range. This allows us to compare against names that may
2106 have N_i in their ranges. */
2107 if (vr
->varying_p () || vr
->undefined_p ())
2109 tem
->set (ssa_name (i
));
2116 /* Compare all the value ranges for names equivalent to VAR with VAL
2117 using comparison code COMP. Return the same value returned by
2118 compare_range_with_value, including the setting of
2119 *STRICT_OVERFLOW_P. */
2122 vr_values::compare_name_with_value (enum tree_code comp
, tree var
, tree val
,
2123 bool *strict_overflow_p
, bool use_equiv_p
)
2129 int used_strict_overflow
;
2131 const value_range
*equiv_vr
;
2134 /* Get the set of equivalences for VAR. */
2135 e
= get_value_range (var
)->equiv ();
2137 /* Start at -1. Set it to 0 if we do a comparison without relying
2138 on overflow, or 1 if all comparisons rely on overflow. */
2139 used_strict_overflow
= -1;
2141 /* Compare vars' value range with val. */
2142 equiv_vr
= get_vr_for_comparison (SSA_NAME_VERSION (var
), &tem_vr
);
2144 retval
= compare_range_with_value (comp
, equiv_vr
, val
, &sop
);
2146 used_strict_overflow
= sop
? 1 : 0;
2148 /* If the equiv set is empty we have done all work we need to do. */
2152 && used_strict_overflow
> 0)
2153 *strict_overflow_p
= true;
2157 EXECUTE_IF_SET_IN_BITMAP (e
, 0, i
, bi
)
2159 tree name
= ssa_name (i
);
2164 && ! SSA_NAME_IS_DEFAULT_DEF (name
)
2165 && prop_simulate_again_p (SSA_NAME_DEF_STMT (name
)))
2168 equiv_vr
= get_vr_for_comparison (i
, &tem_vr
);
2170 t
= compare_range_with_value (comp
, equiv_vr
, val
, &sop
);
2173 /* If we get different answers from different members
2174 of the equivalence set this check must be in a dead
2175 code region. Folding it to a trap representation
2176 would be correct here. For now just return don't-know. */
2186 used_strict_overflow
= 0;
2187 else if (used_strict_overflow
< 0)
2188 used_strict_overflow
= 1;
2193 && used_strict_overflow
> 0)
2194 *strict_overflow_p
= true;
2200 /* Given a comparison code COMP and names N1 and N2, compare all the
2201 ranges equivalent to N1 against all the ranges equivalent to N2
2202 to determine the value of N1 COMP N2. Return the same value
2203 returned by compare_ranges. Set *STRICT_OVERFLOW_P to indicate
2204 whether we relied on undefined signed overflow in the comparison. */
2208 vr_values::compare_names (enum tree_code comp
, tree n1
, tree n2
,
2209 bool *strict_overflow_p
)
2213 bitmap_iterator bi1
, bi2
;
2215 int used_strict_overflow
;
2216 static bitmap_obstack
*s_obstack
= NULL
;
2217 static bitmap s_e1
= NULL
, s_e2
= NULL
;
2219 /* Compare the ranges of every name equivalent to N1 against the
2220 ranges of every name equivalent to N2. */
2221 e1
= get_value_range (n1
)->equiv ();
2222 e2
= get_value_range (n2
)->equiv ();
2224 /* Use the fake bitmaps if e1 or e2 are not available. */
2225 if (s_obstack
== NULL
)
2227 s_obstack
= XNEW (bitmap_obstack
);
2228 bitmap_obstack_initialize (s_obstack
);
2229 s_e1
= BITMAP_ALLOC (s_obstack
);
2230 s_e2
= BITMAP_ALLOC (s_obstack
);
2237 /* Add N1 and N2 to their own set of equivalences to avoid
2238 duplicating the body of the loop just to check N1 and N2
2240 bitmap_set_bit (e1
, SSA_NAME_VERSION (n1
));
2241 bitmap_set_bit (e2
, SSA_NAME_VERSION (n2
));
2243 /* If the equivalence sets have a common intersection, then the two
2244 names can be compared without checking their ranges. */
2245 if (bitmap_intersect_p (e1
, e2
))
2247 bitmap_clear_bit (e1
, SSA_NAME_VERSION (n1
));
2248 bitmap_clear_bit (e2
, SSA_NAME_VERSION (n2
));
2250 return (comp
== EQ_EXPR
|| comp
== GE_EXPR
|| comp
== LE_EXPR
)
2252 : boolean_false_node
;
2255 /* Start at -1. Set it to 0 if we do a comparison without relying
2256 on overflow, or 1 if all comparisons rely on overflow. */
2257 used_strict_overflow
= -1;
2259 /* Otherwise, compare all the equivalent ranges. First, add N1 and
2260 N2 to their own set of equivalences to avoid duplicating the body
2261 of the loop just to check N1 and N2 ranges. */
2262 EXECUTE_IF_SET_IN_BITMAP (e1
, 0, i1
, bi1
)
2264 if (! ssa_name (i1
))
2267 value_range tem_vr1
;
2268 const value_range
*vr1
= get_vr_for_comparison (i1
, &tem_vr1
);
2270 t
= retval
= NULL_TREE
;
2271 EXECUTE_IF_SET_IN_BITMAP (e2
, 0, i2
, bi2
)
2273 if (! ssa_name (i2
))
2278 value_range tem_vr2
;
2279 const value_range
*vr2
= get_vr_for_comparison (i2
, &tem_vr2
);
2281 t
= compare_ranges (comp
, vr1
, vr2
, &sop
);
2284 /* If we get different answers from different members
2285 of the equivalence set this check must be in a dead
2286 code region. Folding it to a trap representation
2287 would be correct here. For now just return don't-know. */
2291 bitmap_clear_bit (e1
, SSA_NAME_VERSION (n1
));
2292 bitmap_clear_bit (e2
, SSA_NAME_VERSION (n2
));
2298 used_strict_overflow
= 0;
2299 else if (used_strict_overflow
< 0)
2300 used_strict_overflow
= 1;
2306 bitmap_clear_bit (e1
, SSA_NAME_VERSION (n1
));
2307 bitmap_clear_bit (e2
, SSA_NAME_VERSION (n2
));
2308 if (used_strict_overflow
> 0)
2309 *strict_overflow_p
= true;
2314 /* None of the equivalent ranges are useful in computing this
2316 bitmap_clear_bit (e1
, SSA_NAME_VERSION (n1
));
2317 bitmap_clear_bit (e2
, SSA_NAME_VERSION (n2
));
2321 /* Helper function for vrp_evaluate_conditional_warnv & other
2325 vr_values::vrp_evaluate_conditional_warnv_with_ops_using_ranges
2326 (enum tree_code code
, tree op0
, tree op1
, bool * strict_overflow_p
)
2328 const value_range
*vr0
, *vr1
;
2330 vr0
= (TREE_CODE (op0
) == SSA_NAME
) ? get_value_range (op0
) : NULL
;
2331 vr1
= (TREE_CODE (op1
) == SSA_NAME
) ? get_value_range (op1
) : NULL
;
2333 tree res
= NULL_TREE
;
2335 res
= compare_ranges (code
, vr0
, vr1
, strict_overflow_p
);
2337 res
= compare_range_with_value (code
, vr0
, op1
, strict_overflow_p
);
2339 res
= (compare_range_with_value
2340 (swap_tree_comparison (code
), vr1
, op0
, strict_overflow_p
));
2344 /* Helper function for vrp_evaluate_conditional_warnv. */
2347 vr_values::vrp_evaluate_conditional_warnv_with_ops (enum tree_code code
,
2350 bool *strict_overflow_p
,
2355 *only_ranges
= true;
2357 /* We only deal with integral and pointer types. */
2358 if (!INTEGRAL_TYPE_P (TREE_TYPE (op0
))
2359 && !POINTER_TYPE_P (TREE_TYPE (op0
)))
2362 /* If OP0 CODE OP1 is an overflow comparison, if it can be expressed
2363 as a simple equality test, then prefer that over its current form
2366 An overflow test which collapses to an equality test can always be
2367 expressed as a comparison of one argument against zero. Overflow
2368 occurs when the chosen argument is zero and does not occur if the
2369 chosen argument is not zero. */
2371 if (overflow_comparison_p (code
, op0
, op1
, use_equiv_p
, &x
))
2373 wide_int max
= wi::max_value (TYPE_PRECISION (TREE_TYPE (op0
)), UNSIGNED
);
2374 /* B = A - 1; if (A < B) -> B = A - 1; if (A == 0)
2375 B = A - 1; if (A > B) -> B = A - 1; if (A != 0)
2376 B = A + 1; if (B < A) -> B = A + 1; if (B == 0)
2377 B = A + 1; if (B > A) -> B = A + 1; if (B != 0) */
2378 if (integer_zerop (x
))
2381 code
= (code
== LT_EXPR
|| code
== LE_EXPR
) ? EQ_EXPR
: NE_EXPR
;
2383 /* B = A + 1; if (A > B) -> B = A + 1; if (B == 0)
2384 B = A + 1; if (A < B) -> B = A + 1; if (B != 0)
2385 B = A - 1; if (B > A) -> B = A - 1; if (A == 0)
2386 B = A - 1; if (B < A) -> B = A - 1; if (A != 0) */
2387 else if (wi::to_wide (x
) == max
- 1)
2390 op1
= wide_int_to_tree (TREE_TYPE (op0
), 0);
2391 code
= (code
== GT_EXPR
|| code
== GE_EXPR
) ? EQ_EXPR
: NE_EXPR
;
2395 value_range_base vro
, vri
;
2396 if (code
== GT_EXPR
|| code
== GE_EXPR
)
2398 vro
.set (VR_ANTI_RANGE
, TYPE_MIN_VALUE (TREE_TYPE (op0
)), x
);
2399 vri
.set (VR_RANGE
, TYPE_MIN_VALUE (TREE_TYPE (op0
)), x
);
2401 else if (code
== LT_EXPR
|| code
== LE_EXPR
)
2403 vro
.set (VR_RANGE
, TYPE_MIN_VALUE (TREE_TYPE (op0
)), x
);
2404 vri
.set (VR_ANTI_RANGE
, TYPE_MIN_VALUE (TREE_TYPE (op0
)), x
);
2408 const value_range
*vr0
= get_value_range (op0
);
2409 /* If vro, the range for OP0 to pass the overflow test, has
2410 no intersection with *vr0, OP0's known range, then the
2411 overflow test can't pass, so return the node for false.
2412 If it is the inverted range, vri, that has no
2413 intersection, then the overflow test must pass, so return
2414 the node for true. In other cases, we could proceed with
2415 a simplified condition comparing OP0 and X, with LE_EXPR
2416 for previously LE_ or LT_EXPR and GT_EXPR otherwise, but
2417 the comments next to the enclosing if suggest it's not
2418 generally profitable to do so. */
2419 vro
.intersect (vr0
);
2420 if (vro
.undefined_p ())
2421 return boolean_false_node
;
2422 vri
.intersect (vr0
);
2423 if (vri
.undefined_p ())
2424 return boolean_true_node
;
2428 if ((ret
= vrp_evaluate_conditional_warnv_with_ops_using_ranges
2429 (code
, op0
, op1
, strict_overflow_p
)))
2432 *only_ranges
= false;
2433 /* Do not use compare_names during propagation, it's quadratic. */
2434 if (TREE_CODE (op0
) == SSA_NAME
&& TREE_CODE (op1
) == SSA_NAME
2436 return compare_names (code
, op0
, op1
, strict_overflow_p
);
2437 else if (TREE_CODE (op0
) == SSA_NAME
)
2438 return compare_name_with_value (code
, op0
, op1
,
2439 strict_overflow_p
, use_equiv_p
);
2440 else if (TREE_CODE (op1
) == SSA_NAME
)
2441 return compare_name_with_value (swap_tree_comparison (code
), op1
, op0
,
2442 strict_overflow_p
, use_equiv_p
);
2446 /* Given (CODE OP0 OP1) within STMT, try to simplify it based on value range
2447 information. Return NULL if the conditional cannot be evaluated.
2448 The ranges of all the names equivalent with the operands in COND
2449 will be used when trying to compute the value. If the result is
2450 based on undefined signed overflow, issue a warning if
2454 vr_values::vrp_evaluate_conditional (tree_code code
, tree op0
,
2455 tree op1
, gimple
*stmt
)
2461 /* Some passes and foldings leak constants with overflow flag set
2462 into the IL. Avoid doing wrong things with these and bail out. */
2463 if ((TREE_CODE (op0
) == INTEGER_CST
2464 && TREE_OVERFLOW (op0
))
2465 || (TREE_CODE (op1
) == INTEGER_CST
2466 && TREE_OVERFLOW (op1
)))
2470 ret
= vrp_evaluate_conditional_warnv_with_ops (code
, op0
, op1
, true, &sop
,
2475 enum warn_strict_overflow_code wc
;
2476 const char* warnmsg
;
2478 if (is_gimple_min_invariant (ret
))
2480 wc
= WARN_STRICT_OVERFLOW_CONDITIONAL
;
2481 warnmsg
= G_("assuming signed overflow does not occur when "
2482 "simplifying conditional to constant");
2486 wc
= WARN_STRICT_OVERFLOW_COMPARISON
;
2487 warnmsg
= G_("assuming signed overflow does not occur when "
2488 "simplifying conditional");
2491 if (issue_strict_overflow_warning (wc
))
2493 location_t location
;
2495 if (!gimple_has_location (stmt
))
2496 location
= input_location
;
2498 location
= gimple_location (stmt
);
2499 warning_at (location
, OPT_Wstrict_overflow
, "%s", warnmsg
);
2503 if (warn_type_limits
2504 && ret
&& only_ranges
2505 && TREE_CODE_CLASS (code
) == tcc_comparison
2506 && TREE_CODE (op0
) == SSA_NAME
)
2508 /* If the comparison is being folded and the operand on the LHS
2509 is being compared against a constant value that is outside of
2510 the natural range of OP0's type, then the predicate will
2511 always fold regardless of the value of OP0. If -Wtype-limits
2512 was specified, emit a warning. */
2513 tree type
= TREE_TYPE (op0
);
2514 const value_range
*vr0
= get_value_range (op0
);
2516 if (vr0
->kind () == VR_RANGE
2517 && INTEGRAL_TYPE_P (type
)
2518 && vrp_val_is_min (vr0
->min ())
2519 && vrp_val_is_max (vr0
->max ())
2520 && is_gimple_min_invariant (op1
))
2522 location_t location
;
2524 if (!gimple_has_location (stmt
))
2525 location
= input_location
;
2527 location
= gimple_location (stmt
);
2529 warning_at (location
, OPT_Wtype_limits
,
2531 ? G_("comparison always false "
2532 "due to limited range of data type")
2533 : G_("comparison always true "
2534 "due to limited range of data type"));
2542 /* Visit conditional statement STMT. If we can determine which edge
2543 will be taken out of STMT's basic block, record it in
2544 *TAKEN_EDGE_P. Otherwise, set *TAKEN_EDGE_P to NULL. */
2547 vr_values::vrp_visit_cond_stmt (gcond
*stmt
, edge
*taken_edge_p
)
2551 *taken_edge_p
= NULL
;
2553 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2558 fprintf (dump_file
, "\nVisiting conditional with predicate: ");
2559 print_gimple_stmt (dump_file
, stmt
, 0);
2560 fprintf (dump_file
, "\nWith known ranges\n");
2562 FOR_EACH_SSA_TREE_OPERAND (use
, stmt
, i
, SSA_OP_USE
)
2564 fprintf (dump_file
, "\t");
2565 print_generic_expr (dump_file
, use
);
2566 fprintf (dump_file
, ": ");
2567 dump_value_range (dump_file
, vr_value
[SSA_NAME_VERSION (use
)]);
2570 fprintf (dump_file
, "\n");
2573 /* Compute the value of the predicate COND by checking the known
2574 ranges of each of its operands.
2576 Note that we cannot evaluate all the equivalent ranges here
2577 because those ranges may not yet be final and with the current
2578 propagation strategy, we cannot determine when the value ranges
2579 of the names in the equivalence set have changed.
2581 For instance, given the following code fragment
2585 i_14 = ASSERT_EXPR <i_5, i_5 != 0>
2589 Assume that on the first visit to i_14, i_5 has the temporary
2590 range [8, 8] because the second argument to the PHI function is
2591 not yet executable. We derive the range ~[0, 0] for i_14 and the
2592 equivalence set { i_5 }. So, when we visit 'if (i_14 == 1)' for
2593 the first time, since i_14 is equivalent to the range [8, 8], we
2594 determine that the predicate is always false.
2596 On the next round of propagation, i_13 is determined to be
2597 VARYING, which causes i_5 to drop down to VARYING. So, another
2598 visit to i_14 is scheduled. In this second visit, we compute the
2599 exact same range and equivalence set for i_14, namely ~[0, 0] and
2600 { i_5 }. But we did not have the previous range for i_5
2601 registered, so vrp_visit_assignment thinks that the range for
2602 i_14 has not changed. Therefore, the predicate 'if (i_14 == 1)'
2603 is not visited again, which stops propagation from visiting
2604 statements in the THEN clause of that if().
2606 To properly fix this we would need to keep the previous range
2607 value for the names in the equivalence set. This way we would've
2608 discovered that from one visit to the other i_5 changed from
2609 range [8, 8] to VR_VARYING.
2611 However, fixing this apparent limitation may not be worth the
2612 additional checking. Testing on several code bases (GCC, DLV,
2613 MICO, TRAMP3D and SPEC2000) showed that doing this results in
2614 4 more predicates folded in SPEC. */
2617 val
= vrp_evaluate_conditional_warnv_with_ops (gimple_cond_code (stmt
),
2618 gimple_cond_lhs (stmt
),
2619 gimple_cond_rhs (stmt
),
2622 *taken_edge_p
= find_taken_edge (gimple_bb (stmt
), val
);
2624 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2626 fprintf (dump_file
, "\nPredicate evaluates to: ");
2627 if (val
== NULL_TREE
)
2628 fprintf (dump_file
, "DON'T KNOW\n");
2630 print_generic_stmt (dump_file
, val
);
2634 /* Searches the case label vector VEC for the ranges of CASE_LABELs that are
2635 used in range VR. The indices are placed in MIN_IDX1, MAX_IDX, MIN_IDX2 and
2636 MAX_IDX2. If the ranges of CASE_LABELs are empty then MAX_IDX1 < MIN_IDX1.
2637 Returns true if the default label is not needed. */
2640 find_case_label_ranges (gswitch
*stmt
, const value_range
*vr
, size_t *min_idx1
,
2641 size_t *max_idx1
, size_t *min_idx2
,
2645 unsigned int n
= gimple_switch_num_labels (stmt
);
2647 tree case_low
, case_high
;
2648 tree min
= vr
->min (), max
= vr
->max ();
2650 gcc_checking_assert (!vr
->varying_p () && !vr
->undefined_p ());
2652 take_default
= !find_case_label_range (stmt
, min
, max
, &i
, &j
);
2654 /* Set second range to empty. */
2658 if (vr
->kind () == VR_RANGE
)
2662 return !take_default
;
2665 /* Set first range to all case labels. */
2672 /* Make sure all the values of case labels [i , j] are contained in
2673 range [MIN, MAX]. */
2674 case_low
= CASE_LOW (gimple_switch_label (stmt
, i
));
2675 case_high
= CASE_HIGH (gimple_switch_label (stmt
, j
));
2676 if (tree_int_cst_compare (case_low
, min
) < 0)
2678 if (case_high
!= NULL_TREE
2679 && tree_int_cst_compare (max
, case_high
) < 0)
2685 /* If the range spans case labels [i, j], the corresponding anti-range spans
2686 the labels [1, i - 1] and [j + 1, n - 1]. */
2712 /* Visit switch statement STMT. If we can determine which edge
2713 will be taken out of STMT's basic block, record it in
2714 *TAKEN_EDGE_P. Otherwise, *TAKEN_EDGE_P set to NULL. */
2717 vr_values::vrp_visit_switch_stmt (gswitch
*stmt
, edge
*taken_edge_p
)
2720 const value_range
*vr
;
2721 size_t i
= 0, j
= 0, k
, l
;
2724 *taken_edge_p
= NULL
;
2725 op
= gimple_switch_index (stmt
);
2726 if (TREE_CODE (op
) != SSA_NAME
)
2729 vr
= get_value_range (op
);
2730 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2732 fprintf (dump_file
, "\nVisiting switch expression with operand ");
2733 print_generic_expr (dump_file
, op
);
2734 fprintf (dump_file
, " with known range ");
2735 dump_value_range (dump_file
, vr
);
2736 fprintf (dump_file
, "\n");
2739 if (vr
->undefined_p ()
2741 || vr
->symbolic_p ())
2744 /* Find the single edge that is taken from the switch expression. */
2745 take_default
= !find_case_label_ranges (stmt
, vr
, &i
, &j
, &k
, &l
);
2747 /* Check if the range spans no CASE_LABEL. If so, we only reach the default
2751 gcc_assert (take_default
);
2752 val
= gimple_switch_default_label (stmt
);
2756 /* Check if labels with index i to j and maybe the default label
2757 are all reaching the same label. */
2759 val
= gimple_switch_label (stmt
, i
);
2761 && CASE_LABEL (gimple_switch_default_label (stmt
))
2762 != CASE_LABEL (val
))
2764 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2765 fprintf (dump_file
, " not a single destination for this "
2769 for (++i
; i
<= j
; ++i
)
2771 if (CASE_LABEL (gimple_switch_label (stmt
, i
)) != CASE_LABEL (val
))
2773 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2774 fprintf (dump_file
, " not a single destination for this "
2781 if (CASE_LABEL (gimple_switch_label (stmt
, k
)) != CASE_LABEL (val
))
2783 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2784 fprintf (dump_file
, " not a single destination for this "
2791 *taken_edge_p
= find_edge (gimple_bb (stmt
),
2792 label_to_block (cfun
, CASE_LABEL (val
)));
2794 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2796 fprintf (dump_file
, " will take edge to ");
2797 print_generic_stmt (dump_file
, CASE_LABEL (val
));
2802 /* Evaluate statement STMT. If the statement produces a useful range,
2803 set VR and corepsponding OUTPUT_P.
2805 If STMT is a conditional branch and we can determine its truth
2806 value, the taken edge is recorded in *TAKEN_EDGE_P. */
2809 vr_values::extract_range_from_stmt (gimple
*stmt
, edge
*taken_edge_p
,
2810 tree
*output_p
, value_range
*vr
)
2813 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2815 fprintf (dump_file
, "\nVisiting statement:\n");
2816 print_gimple_stmt (dump_file
, stmt
, 0, dump_flags
);
2819 if (!stmt_interesting_for_vrp (stmt
))
2820 gcc_assert (stmt_ends_bb_p (stmt
));
2821 else if (is_gimple_assign (stmt
) || is_gimple_call (stmt
))
2822 vrp_visit_assignment_or_call (stmt
, output_p
, vr
);
2823 else if (gimple_code (stmt
) == GIMPLE_COND
)
2824 vrp_visit_cond_stmt (as_a
<gcond
*> (stmt
), taken_edge_p
);
2825 else if (gimple_code (stmt
) == GIMPLE_SWITCH
)
2826 vrp_visit_switch_stmt (as_a
<gswitch
*> (stmt
), taken_edge_p
);
2829 /* Visit all arguments for PHI node PHI that flow through executable
2830 edges. If a valid value range can be derived from all the incoming
2831 value ranges, set a new range in VR_RESULT. */
2834 vr_values::extract_range_from_phi_node (gphi
*phi
, value_range
*vr_result
)
2837 tree lhs
= PHI_RESULT (phi
);
2838 const value_range
*lhs_vr
= get_value_range (lhs
);
2840 int edges
, old_edges
;
2843 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2845 fprintf (dump_file
, "\nVisiting PHI node: ");
2846 print_gimple_stmt (dump_file
, phi
, 0, dump_flags
);
2849 bool may_simulate_backedge_again
= false;
2851 for (i
= 0; i
< gimple_phi_num_args (phi
); i
++)
2853 edge e
= gimple_phi_arg_edge (phi
, i
);
2855 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2858 " Argument #%d (%d -> %d %sexecutable)\n",
2859 (int) i
, e
->src
->index
, e
->dest
->index
,
2860 (e
->flags
& EDGE_EXECUTABLE
) ? "" : "not ");
2863 if (e
->flags
& EDGE_EXECUTABLE
)
2865 tree arg
= PHI_ARG_DEF (phi
, i
);
2866 value_range vr_arg_tem
;
2867 const value_range
*vr_arg
= &vr_arg_tem
;
2871 if (TREE_CODE (arg
) == SSA_NAME
)
2873 /* See if we are eventually going to change one of the args. */
2874 gimple
*def_stmt
= SSA_NAME_DEF_STMT (arg
);
2875 if (! gimple_nop_p (def_stmt
)
2876 && prop_simulate_again_p (def_stmt
)
2877 && e
->flags
& EDGE_DFS_BACK
)
2878 may_simulate_backedge_again
= true;
2880 const value_range
*vr_arg_
= get_value_range (arg
);
2881 /* Do not allow equivalences or symbolic ranges to leak in from
2882 backedges. That creates invalid equivalencies.
2883 See PR53465 and PR54767. */
2884 if (e
->flags
& EDGE_DFS_BACK
)
2886 if (!vr_arg_
->varying_p () && !vr_arg_
->undefined_p ())
2888 vr_arg_tem
.set (vr_arg_
->kind (), vr_arg_
->min (),
2889 vr_arg_
->max (), NULL
);
2890 if (vr_arg_tem
.symbolic_p ())
2891 vr_arg_tem
.set_varying (TREE_TYPE (arg
));
2896 /* If the non-backedge arguments range is VR_VARYING then
2897 we can still try recording a simple equivalence. */
2898 else if (vr_arg_
->varying_p ())
2899 vr_arg_tem
.set (arg
);
2905 if (TREE_OVERFLOW_P (arg
))
2906 arg
= drop_tree_overflow (arg
);
2908 vr_arg_tem
.set (arg
);
2911 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2913 fprintf (dump_file
, "\t");
2914 print_generic_expr (dump_file
, arg
, dump_flags
);
2915 fprintf (dump_file
, ": ");
2916 dump_value_range (dump_file
, vr_arg
);
2917 fprintf (dump_file
, "\n");
2921 vr_result
->deep_copy (vr_arg
);
2923 vr_result
->union_ (vr_arg
);
2926 if (vr_result
->varying_p ())
2931 if (vr_result
->varying_p ())
2933 else if (vr_result
->undefined_p ())
2936 old_edges
= vr_phi_edge_counts
[SSA_NAME_VERSION (lhs
)];
2937 vr_phi_edge_counts
[SSA_NAME_VERSION (lhs
)] = edges
;
2939 /* To prevent infinite iterations in the algorithm, derive ranges
2940 when the new value is slightly bigger or smaller than the
2941 previous one. We don't do this if we have seen a new executable
2942 edge; this helps us avoid an infinity for conditionals
2943 which are not in a loop. If the old value-range was VR_UNDEFINED
2944 use the updated range and iterate one more time. If we will not
2945 simulate this PHI again via the backedge allow us to iterate. */
2947 && gimple_phi_num_args (phi
) > 1
2948 && edges
== old_edges
2949 && !lhs_vr
->undefined_p ()
2950 && may_simulate_backedge_again
)
2952 /* Compare old and new ranges, fall back to varying if the
2953 values are not comparable. */
2954 int cmp_min
= compare_values (lhs_vr
->min (), vr_result
->min ());
2957 int cmp_max
= compare_values (lhs_vr
->max (), vr_result
->max ());
2961 /* For non VR_RANGE or for pointers fall back to varying if
2962 the range changed. */
2963 if ((lhs_vr
->kind () != VR_RANGE
|| vr_result
->kind () != VR_RANGE
2964 || POINTER_TYPE_P (TREE_TYPE (lhs
)))
2965 && (cmp_min
!= 0 || cmp_max
!= 0))
2968 /* If the new minimum is larger than the previous one
2969 retain the old value. If the new minimum value is smaller
2970 than the previous one and not -INF go all the way to -INF + 1.
2971 In the first case, to avoid infinite bouncing between different
2972 minimums, and in the other case to avoid iterating millions of
2973 times to reach -INF. Going to -INF + 1 also lets the following
2974 iteration compute whether there will be any overflow, at the
2975 expense of one additional iteration. */
2976 tree new_min
= vr_result
->min ();
2977 tree new_max
= vr_result
->max ();
2979 new_min
= lhs_vr
->min ();
2980 else if (cmp_min
> 0
2981 && (TREE_CODE (vr_result
->min ()) != INTEGER_CST
2982 || tree_int_cst_lt (vrp_val_min (vr_result
->type ()),
2983 vr_result
->min ())))
2984 new_min
= int_const_binop (PLUS_EXPR
,
2985 vrp_val_min (vr_result
->type ()),
2986 build_int_cst (vr_result
->type (), 1));
2988 /* Similarly for the maximum value. */
2990 new_max
= lhs_vr
->max ();
2991 else if (cmp_max
< 0
2992 && (TREE_CODE (vr_result
->max ()) != INTEGER_CST
2993 || tree_int_cst_lt (vr_result
->max (),
2994 vrp_val_max (vr_result
->type ()))))
2995 new_max
= int_const_binop (MINUS_EXPR
,
2996 vrp_val_max (vr_result
->type ()),
2997 build_int_cst (vr_result
->type (), 1));
2999 vr_result
->update (vr_result
->kind (), new_min
, new_max
);
3001 /* If we dropped either bound to +-INF then if this is a loop
3002 PHI node SCEV may known more about its value-range. */
3003 if (cmp_min
> 0 || cmp_min
< 0
3004 || cmp_max
< 0 || cmp_max
> 0)
3007 goto infinite_check
;
3013 vr_result
->set_varying (TREE_TYPE (lhs
));
3016 /* If this is a loop PHI node SCEV may known more about its value-range.
3017 scev_check can be reached from two paths, one is a fall through from above
3018 "varying" label, the other is direct goto from code block which tries to
3019 avoid infinite simulation. */
3020 if (scev_initialized_p ()
3021 && (l
= loop_containing_stmt (phi
))
3022 && l
->header
== gimple_bb (phi
))
3023 adjust_range_with_scev (vr_result
, l
, phi
, lhs
);
3026 /* If we will end up with a (-INF, +INF) range, set it to
3027 VARYING. Same if the previous max value was invalid for
3028 the type and we end up with vr_result.min > vr_result.max. */
3029 if ((!vr_result
->varying_p () && !vr_result
->undefined_p ())
3030 && !((vrp_val_is_max (vr_result
->max ()) && vrp_val_is_min (vr_result
->min ()))
3031 || compare_values (vr_result
->min (), vr_result
->max ()) > 0))
3034 vr_result
->set_varying (TREE_TYPE (lhs
));
3036 /* If the new range is different than the previous value, keep
3042 /* Simplify boolean operations if the source is known
3043 to be already a boolean. */
3045 vr_values::simplify_truth_ops_using_ranges (gimple_stmt_iterator
*gsi
,
3048 enum tree_code rhs_code
= gimple_assign_rhs_code (stmt
);
3050 bool need_conversion
;
3052 /* We handle only !=/== case here. */
3053 gcc_assert (rhs_code
== EQ_EXPR
|| rhs_code
== NE_EXPR
);
3055 op0
= gimple_assign_rhs1 (stmt
);
3056 if (!op_with_boolean_value_range_p (op0
))
3059 op1
= gimple_assign_rhs2 (stmt
);
3060 if (!op_with_boolean_value_range_p (op1
))
3063 /* Reduce number of cases to handle to NE_EXPR. As there is no
3064 BIT_XNOR_EXPR we cannot replace A == B with a single statement. */
3065 if (rhs_code
== EQ_EXPR
)
3067 if (TREE_CODE (op1
) == INTEGER_CST
)
3068 op1
= int_const_binop (BIT_XOR_EXPR
, op1
,
3069 build_int_cst (TREE_TYPE (op1
), 1));
3074 lhs
= gimple_assign_lhs (stmt
);
3076 = !useless_type_conversion_p (TREE_TYPE (lhs
), TREE_TYPE (op0
));
3078 /* Make sure to not sign-extend a 1-bit 1 when converting the result. */
3080 && !TYPE_UNSIGNED (TREE_TYPE (op0
))
3081 && TYPE_PRECISION (TREE_TYPE (op0
)) == 1
3082 && TYPE_PRECISION (TREE_TYPE (lhs
)) > 1)
3085 /* For A != 0 we can substitute A itself. */
3086 if (integer_zerop (op1
))
3087 gimple_assign_set_rhs_with_ops (gsi
,
3089 ? NOP_EXPR
: TREE_CODE (op0
), op0
);
3090 /* For A != B we substitute A ^ B. Either with conversion. */
3091 else if (need_conversion
)
3093 tree tem
= make_ssa_name (TREE_TYPE (op0
));
3095 = gimple_build_assign (tem
, BIT_XOR_EXPR
, op0
, op1
);
3096 gsi_insert_before (gsi
, newop
, GSI_SAME_STMT
);
3097 if (INTEGRAL_TYPE_P (TREE_TYPE (tem
))
3098 && TYPE_PRECISION (TREE_TYPE (tem
)) > 1)
3099 set_range_info (tem
, VR_RANGE
,
3100 wi::zero (TYPE_PRECISION (TREE_TYPE (tem
))),
3101 wi::one (TYPE_PRECISION (TREE_TYPE (tem
))));
3102 gimple_assign_set_rhs_with_ops (gsi
, NOP_EXPR
, tem
);
3106 gimple_assign_set_rhs_with_ops (gsi
, BIT_XOR_EXPR
, op0
, op1
);
3107 update_stmt (gsi_stmt (*gsi
));
3108 fold_stmt (gsi
, follow_single_use_edges
);
3113 /* Simplify a division or modulo operator to a right shift or bitwise and
3114 if the first operand is unsigned or is greater than zero and the second
3115 operand is an exact power of two. For TRUNC_MOD_EXPR op0 % op1 with
3116 constant op1 (op1min = op1) or with op1 in [op1min, op1max] range,
3117 optimize it into just op0 if op0's range is known to be a subset of
3118 [-op1min + 1, op1min - 1] for signed and [0, op1min - 1] for unsigned
3122 vr_values::simplify_div_or_mod_using_ranges (gimple_stmt_iterator
*gsi
,
3125 enum tree_code rhs_code
= gimple_assign_rhs_code (stmt
);
3127 tree op0
= gimple_assign_rhs1 (stmt
);
3128 tree op1
= gimple_assign_rhs2 (stmt
);
3129 tree op0min
= NULL_TREE
, op0max
= NULL_TREE
;
3131 const value_range
*vr
= NULL
;
3133 if (TREE_CODE (op0
) == INTEGER_CST
)
3140 vr
= get_value_range (op0
);
3141 if (range_int_cst_p (vr
))
3143 op0min
= vr
->min ();
3144 op0max
= vr
->max ();
3148 if (rhs_code
== TRUNC_MOD_EXPR
3149 && TREE_CODE (op1
) == SSA_NAME
)
3151 const value_range
*vr1
= get_value_range (op1
);
3152 if (range_int_cst_p (vr1
))
3153 op1min
= vr1
->min ();
3155 if (rhs_code
== TRUNC_MOD_EXPR
3156 && TREE_CODE (op1min
) == INTEGER_CST
3157 && tree_int_cst_sgn (op1min
) == 1
3159 && tree_int_cst_lt (op0max
, op1min
))
3161 if (TYPE_UNSIGNED (TREE_TYPE (op0
))
3162 || tree_int_cst_sgn (op0min
) >= 0
3163 || tree_int_cst_lt (fold_unary (NEGATE_EXPR
, TREE_TYPE (op1min
), op1min
),
3166 /* If op0 already has the range op0 % op1 has,
3167 then TRUNC_MOD_EXPR won't change anything. */
3168 gimple_assign_set_rhs_from_tree (gsi
, op0
);
3173 if (TREE_CODE (op0
) != SSA_NAME
)
3176 if (!integer_pow2p (op1
))
3178 /* X % -Y can be only optimized into X % Y either if
3179 X is not INT_MIN, or Y is not -1. Fold it now, as after
3180 remove_range_assertions the range info might be not available
3182 if (rhs_code
== TRUNC_MOD_EXPR
3183 && fold_stmt (gsi
, follow_single_use_edges
))
3188 if (TYPE_UNSIGNED (TREE_TYPE (op0
)))
3189 val
= integer_one_node
;
3194 val
= compare_range_with_value (GE_EXPR
, vr
, integer_zero_node
, &sop
);
3198 && integer_onep (val
)
3199 && issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC
))
3201 location_t location
;
3203 if (!gimple_has_location (stmt
))
3204 location
= input_location
;
3206 location
= gimple_location (stmt
);
3207 warning_at (location
, OPT_Wstrict_overflow
,
3208 "assuming signed overflow does not occur when "
3209 "simplifying %</%> or %<%%%> to %<>>%> or %<&%>");
3213 if (val
&& integer_onep (val
))
3217 if (rhs_code
== TRUNC_DIV_EXPR
)
3219 t
= build_int_cst (integer_type_node
, tree_log2 (op1
));
3220 gimple_assign_set_rhs_code (stmt
, RSHIFT_EXPR
);
3221 gimple_assign_set_rhs1 (stmt
, op0
);
3222 gimple_assign_set_rhs2 (stmt
, t
);
3226 t
= build_int_cst (TREE_TYPE (op1
), 1);
3227 t
= int_const_binop (MINUS_EXPR
, op1
, t
);
3228 t
= fold_convert (TREE_TYPE (op0
), t
);
3230 gimple_assign_set_rhs_code (stmt
, BIT_AND_EXPR
);
3231 gimple_assign_set_rhs1 (stmt
, op0
);
3232 gimple_assign_set_rhs2 (stmt
, t
);
3236 fold_stmt (gsi
, follow_single_use_edges
);
3243 /* Simplify a min or max if the ranges of the two operands are
3244 disjoint. Return true if we do simplify. */
3247 vr_values::simplify_min_or_max_using_ranges (gimple_stmt_iterator
*gsi
,
3250 tree op0
= gimple_assign_rhs1 (stmt
);
3251 tree op1
= gimple_assign_rhs2 (stmt
);
3255 val
= (vrp_evaluate_conditional_warnv_with_ops_using_ranges
3256 (LE_EXPR
, op0
, op1
, &sop
));
3260 val
= (vrp_evaluate_conditional_warnv_with_ops_using_ranges
3261 (LT_EXPR
, op0
, op1
, &sop
));
3266 if (sop
&& issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC
))
3268 location_t location
;
3270 if (!gimple_has_location (stmt
))
3271 location
= input_location
;
3273 location
= gimple_location (stmt
);
3274 warning_at (location
, OPT_Wstrict_overflow
,
3275 "assuming signed overflow does not occur when "
3276 "simplifying %<min/max (X,Y)%> to %<X%> or %<Y%>");
3279 /* VAL == TRUE -> OP0 < or <= op1
3280 VAL == FALSE -> OP0 > or >= op1. */
3281 tree res
= ((gimple_assign_rhs_code (stmt
) == MAX_EXPR
)
3282 == integer_zerop (val
)) ? op0
: op1
;
3283 gimple_assign_set_rhs_from_tree (gsi
, res
);
3290 /* If the operand to an ABS_EXPR is >= 0, then eliminate the
3291 ABS_EXPR. If the operand is <= 0, then simplify the
3292 ABS_EXPR into a NEGATE_EXPR. */
3295 vr_values::simplify_abs_using_ranges (gimple_stmt_iterator
*gsi
, gimple
*stmt
)
3297 tree op
= gimple_assign_rhs1 (stmt
);
3298 const value_range
*vr
= get_value_range (op
);
3305 val
= compare_range_with_value (LE_EXPR
, vr
, integer_zero_node
, &sop
);
3308 /* The range is neither <= 0 nor > 0. Now see if it is
3309 either < 0 or >= 0. */
3311 val
= compare_range_with_value (LT_EXPR
, vr
, integer_zero_node
,
3317 if (sop
&& issue_strict_overflow_warning (WARN_STRICT_OVERFLOW_MISC
))
3319 location_t location
;
3321 if (!gimple_has_location (stmt
))
3322 location
= input_location
;
3324 location
= gimple_location (stmt
);
3325 warning_at (location
, OPT_Wstrict_overflow
,
3326 "assuming signed overflow does not occur when "
3327 "simplifying %<abs (X)%> to %<X%> or %<-X%>");
3330 gimple_assign_set_rhs1 (stmt
, op
);
3331 if (integer_zerop (val
))
3332 gimple_assign_set_rhs_code (stmt
, SSA_NAME
);
3334 gimple_assign_set_rhs_code (stmt
, NEGATE_EXPR
);
3336 fold_stmt (gsi
, follow_single_use_edges
);
3344 /* Optimize away redundant BIT_AND_EXPR and BIT_IOR_EXPR.
3345 If all the bits that are being cleared by & are already
3346 known to be zero from VR, or all the bits that are being
3347 set by | are already known to be one from VR, the bit
3348 operation is redundant. */
3351 vr_values::simplify_bit_ops_using_ranges (gimple_stmt_iterator
*gsi
,
3354 tree op0
= gimple_assign_rhs1 (stmt
);
3355 tree op1
= gimple_assign_rhs2 (stmt
);
3356 tree op
= NULL_TREE
;
3357 value_range_base vr0
, vr1
;
3358 wide_int may_be_nonzero0
, may_be_nonzero1
;
3359 wide_int must_be_nonzero0
, must_be_nonzero1
;
3362 if (TREE_CODE (op0
) == SSA_NAME
)
3363 vr0
= *(get_value_range (op0
));
3364 else if (is_gimple_min_invariant (op0
))
3369 if (TREE_CODE (op1
) == SSA_NAME
)
3370 vr1
= *(get_value_range (op1
));
3371 else if (is_gimple_min_invariant (op1
))
3376 if (!vrp_set_zero_nonzero_bits (TREE_TYPE (op0
), &vr0
, &may_be_nonzero0
,
3379 if (!vrp_set_zero_nonzero_bits (TREE_TYPE (op1
), &vr1
, &may_be_nonzero1
,
3383 switch (gimple_assign_rhs_code (stmt
))
3386 mask
= wi::bit_and_not (may_be_nonzero0
, must_be_nonzero1
);
3392 mask
= wi::bit_and_not (may_be_nonzero1
, must_be_nonzero0
);
3400 mask
= wi::bit_and_not (may_be_nonzero0
, must_be_nonzero1
);
3406 mask
= wi::bit_and_not (may_be_nonzero1
, must_be_nonzero0
);
3417 if (op
== NULL_TREE
)
3420 gimple_assign_set_rhs_with_ops (gsi
, TREE_CODE (op
), op
);
3421 update_stmt (gsi_stmt (*gsi
));
3425 /* We are comparing trees OP0 and OP1 using COND_CODE. OP0 has
3426 a known value range VR.
3428 If there is one and only one value which will satisfy the
3429 conditional, then return that value. Else return NULL.
3431 If signed overflow must be undefined for the value to satisfy
3432 the conditional, then set *STRICT_OVERFLOW_P to true. */
3435 test_for_singularity (enum tree_code cond_code
, tree op0
,
3436 tree op1
, const value_range
*vr
)
3441 /* Extract minimum/maximum values which satisfy the conditional as it was
3443 if (cond_code
== LE_EXPR
|| cond_code
== LT_EXPR
)
3445 min
= TYPE_MIN_VALUE (TREE_TYPE (op0
));
3448 if (cond_code
== LT_EXPR
)
3450 tree one
= build_int_cst (TREE_TYPE (op0
), 1);
3451 max
= fold_build2 (MINUS_EXPR
, TREE_TYPE (op0
), max
, one
);
3452 /* Signal to compare_values_warnv this expr doesn't overflow. */
3454 TREE_NO_WARNING (max
) = 1;
3457 else if (cond_code
== GE_EXPR
|| cond_code
== GT_EXPR
)
3459 max
= TYPE_MAX_VALUE (TREE_TYPE (op0
));
3462 if (cond_code
== GT_EXPR
)
3464 tree one
= build_int_cst (TREE_TYPE (op0
), 1);
3465 min
= fold_build2 (PLUS_EXPR
, TREE_TYPE (op0
), min
, one
);
3466 /* Signal to compare_values_warnv this expr doesn't overflow. */
3468 TREE_NO_WARNING (min
) = 1;
3472 /* Now refine the minimum and maximum values using any
3473 value range information we have for op0. */
3476 if (compare_values (vr
->min (), min
) == 1)
3478 if (compare_values (vr
->max (), max
) == -1)
3481 /* If the new min/max values have converged to a single value,
3482 then there is only one value which can satisfy the condition,
3483 return that value. */
3484 if (operand_equal_p (min
, max
, 0) && is_gimple_min_invariant (min
))
3490 /* Return whether the value range *VR fits in an integer type specified
3491 by PRECISION and UNSIGNED_P. */
3494 range_fits_type_p (const value_range
*vr
,
3495 unsigned dest_precision
, signop dest_sgn
)
3498 unsigned src_precision
;
3502 /* We can only handle integral and pointer types. */
3503 src_type
= vr
->type ();
3504 if (!INTEGRAL_TYPE_P (src_type
)
3505 && !POINTER_TYPE_P (src_type
))
3508 /* An extension is fine unless VR is SIGNED and dest_sgn is UNSIGNED,
3509 and so is an identity transform. */
3510 src_precision
= TYPE_PRECISION (vr
->type ());
3511 src_sgn
= TYPE_SIGN (src_type
);
3512 if ((src_precision
< dest_precision
3513 && !(dest_sgn
== UNSIGNED
&& src_sgn
== SIGNED
))
3514 || (src_precision
== dest_precision
&& src_sgn
== dest_sgn
))
3517 /* Now we can only handle ranges with constant bounds. */
3518 if (!range_int_cst_p (vr
))
3521 /* For sign changes, the MSB of the wide_int has to be clear.
3522 An unsigned value with its MSB set cannot be represented by
3523 a signed wide_int, while a negative value cannot be represented
3524 by an unsigned wide_int. */
3525 if (src_sgn
!= dest_sgn
3526 && (wi::lts_p (wi::to_wide (vr
->min ()), 0)
3527 || wi::lts_p (wi::to_wide (vr
->max ()), 0)))
3530 /* Then we can perform the conversion on both ends and compare
3531 the result for equality. */
3532 tem
= wi::ext (wi::to_widest (vr
->min ()), dest_precision
, dest_sgn
);
3533 if (tem
!= wi::to_widest (vr
->min ()))
3535 tem
= wi::ext (wi::to_widest (vr
->max ()), dest_precision
, dest_sgn
);
3536 if (tem
!= wi::to_widest (vr
->max ()))
3542 /* Simplify a conditional using a relational operator to an equality
3543 test if the range information indicates only one value can satisfy
3544 the original conditional. */
3547 vr_values::simplify_cond_using_ranges_1 (gcond
*stmt
)
3549 tree op0
= gimple_cond_lhs (stmt
);
3550 tree op1
= gimple_cond_rhs (stmt
);
3551 enum tree_code cond_code
= gimple_cond_code (stmt
);
3553 if (cond_code
!= NE_EXPR
3554 && cond_code
!= EQ_EXPR
3555 && TREE_CODE (op0
) == SSA_NAME
3556 && INTEGRAL_TYPE_P (TREE_TYPE (op0
))
3557 && is_gimple_min_invariant (op1
))
3559 const value_range
*vr
= get_value_range (op0
);
3561 /* If we have range information for OP0, then we might be
3562 able to simplify this conditional. */
3563 if (vr
->kind () == VR_RANGE
)
3565 tree new_tree
= test_for_singularity (cond_code
, op0
, op1
, vr
);
3570 fprintf (dump_file
, "Simplified relational ");
3571 print_gimple_stmt (dump_file
, stmt
, 0);
3572 fprintf (dump_file
, " into ");
3575 gimple_cond_set_code (stmt
, EQ_EXPR
);
3576 gimple_cond_set_lhs (stmt
, op0
);
3577 gimple_cond_set_rhs (stmt
, new_tree
);
3583 print_gimple_stmt (dump_file
, stmt
, 0);
3584 fprintf (dump_file
, "\n");
3590 /* Try again after inverting the condition. We only deal
3591 with integral types here, so no need to worry about
3592 issues with inverting FP comparisons. */
3593 new_tree
= test_for_singularity
3594 (invert_tree_comparison (cond_code
, false),
3600 fprintf (dump_file
, "Simplified relational ");
3601 print_gimple_stmt (dump_file
, stmt
, 0);
3602 fprintf (dump_file
, " into ");
3605 gimple_cond_set_code (stmt
, NE_EXPR
);
3606 gimple_cond_set_lhs (stmt
, op0
);
3607 gimple_cond_set_rhs (stmt
, new_tree
);
3613 print_gimple_stmt (dump_file
, stmt
, 0);
3614 fprintf (dump_file
, "\n");
3624 /* STMT is a conditional at the end of a basic block.
3626 If the conditional is of the form SSA_NAME op constant and the SSA_NAME
3627 was set via a type conversion, try to replace the SSA_NAME with the RHS
3628 of the type conversion. Doing so makes the conversion dead which helps
3629 subsequent passes. */
3632 vr_values::simplify_cond_using_ranges_2 (gcond
*stmt
)
3634 tree op0
= gimple_cond_lhs (stmt
);
3635 tree op1
= gimple_cond_rhs (stmt
);
3637 /* If we have a comparison of an SSA_NAME (OP0) against a constant,
3638 see if OP0 was set by a type conversion where the source of
3639 the conversion is another SSA_NAME with a range that fits
3640 into the range of OP0's type.
3642 If so, the conversion is redundant as the earlier SSA_NAME can be
3643 used for the comparison directly if we just massage the constant in the
3645 if (TREE_CODE (op0
) == SSA_NAME
3646 && TREE_CODE (op1
) == INTEGER_CST
)
3648 gimple
*def_stmt
= SSA_NAME_DEF_STMT (op0
);
3651 if (!is_gimple_assign (def_stmt
)
3652 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt
)))
3655 innerop
= gimple_assign_rhs1 (def_stmt
);
3657 if (TREE_CODE (innerop
) == SSA_NAME
3658 && !POINTER_TYPE_P (TREE_TYPE (innerop
))
3659 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (innerop
)
3660 && desired_pro_or_demotion_p (TREE_TYPE (innerop
), TREE_TYPE (op0
)))
3662 const value_range
*vr
= get_value_range (innerop
);
3664 if (range_int_cst_p (vr
)
3665 && range_fits_type_p (vr
,
3666 TYPE_PRECISION (TREE_TYPE (op0
)),
3667 TYPE_SIGN (TREE_TYPE (op0
)))
3668 && int_fits_type_p (op1
, TREE_TYPE (innerop
)))
3670 tree newconst
= fold_convert (TREE_TYPE (innerop
), op1
);
3671 gimple_cond_set_lhs (stmt
, innerop
);
3672 gimple_cond_set_rhs (stmt
, newconst
);
3674 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3676 fprintf (dump_file
, "Folded into: ");
3677 print_gimple_stmt (dump_file
, stmt
, 0, TDF_SLIM
);
3678 fprintf (dump_file
, "\n");
3685 /* Simplify a switch statement using the value range of the switch
3689 vr_values::simplify_switch_using_ranges (gswitch
*stmt
)
3691 tree op
= gimple_switch_index (stmt
);
3692 const value_range
*vr
= NULL
;
3696 size_t i
= 0, j
= 0, n
, n2
;
3699 size_t k
= 1, l
= 0;
3701 if (TREE_CODE (op
) == SSA_NAME
)
3703 vr
= get_value_range (op
);
3705 /* We can only handle integer ranges. */
3706 if (vr
->varying_p ()
3707 || vr
->undefined_p ()
3708 || vr
->symbolic_p ())
3711 /* Find case label for min/max of the value range. */
3712 take_default
= !find_case_label_ranges (stmt
, vr
, &i
, &j
, &k
, &l
);
3714 else if (TREE_CODE (op
) == INTEGER_CST
)
3716 take_default
= !find_case_label_index (stmt
, 1, op
, &i
);
3730 n
= gimple_switch_num_labels (stmt
);
3732 /* We can truncate the case label ranges that partially overlap with OP's
3734 size_t min_idx
= 1, max_idx
= 0;
3736 find_case_label_range (stmt
, vr
->min (), vr
->max (), &min_idx
, &max_idx
);
3737 if (min_idx
<= max_idx
)
3739 tree min_label
= gimple_switch_label (stmt
, min_idx
);
3740 tree max_label
= gimple_switch_label (stmt
, max_idx
);
3742 /* Avoid changing the type of the case labels when truncating. */
3743 tree case_label_type
= TREE_TYPE (CASE_LOW (min_label
));
3744 tree vr_min
= fold_convert (case_label_type
, vr
->min ());
3745 tree vr_max
= fold_convert (case_label_type
, vr
->max ());
3747 if (vr
->kind () == VR_RANGE
)
3749 /* If OP's value range is [2,8] and the low label range is
3750 0 ... 3, truncate the label's range to 2 .. 3. */
3751 if (tree_int_cst_compare (CASE_LOW (min_label
), vr_min
) < 0
3752 && CASE_HIGH (min_label
) != NULL_TREE
3753 && tree_int_cst_compare (CASE_HIGH (min_label
), vr_min
) >= 0)
3754 CASE_LOW (min_label
) = vr_min
;
3756 /* If OP's value range is [2,8] and the high label range is
3757 7 ... 10, truncate the label's range to 7 .. 8. */
3758 if (tree_int_cst_compare (CASE_LOW (max_label
), vr_max
) <= 0
3759 && CASE_HIGH (max_label
) != NULL_TREE
3760 && tree_int_cst_compare (CASE_HIGH (max_label
), vr_max
) > 0)
3761 CASE_HIGH (max_label
) = vr_max
;
3763 else if (vr
->kind () == VR_ANTI_RANGE
)
3765 tree one_cst
= build_one_cst (case_label_type
);
3767 if (min_label
== max_label
)
3769 /* If OP's value range is ~[7,8] and the label's range is
3770 7 ... 10, truncate the label's range to 9 ... 10. */
3771 if (tree_int_cst_compare (CASE_LOW (min_label
), vr_min
) == 0
3772 && CASE_HIGH (min_label
) != NULL_TREE
3773 && tree_int_cst_compare (CASE_HIGH (min_label
), vr_max
) > 0)
3774 CASE_LOW (min_label
)
3775 = int_const_binop (PLUS_EXPR
, vr_max
, one_cst
);
3777 /* If OP's value range is ~[7,8] and the label's range is
3778 5 ... 8, truncate the label's range to 5 ... 6. */
3779 if (tree_int_cst_compare (CASE_LOW (min_label
), vr_min
) < 0
3780 && CASE_HIGH (min_label
) != NULL_TREE
3781 && tree_int_cst_compare (CASE_HIGH (min_label
), vr_max
) == 0)
3782 CASE_HIGH (min_label
)
3783 = int_const_binop (MINUS_EXPR
, vr_min
, one_cst
);
3787 /* If OP's value range is ~[2,8] and the low label range is
3788 0 ... 3, truncate the label's range to 0 ... 1. */
3789 if (tree_int_cst_compare (CASE_LOW (min_label
), vr_min
) < 0
3790 && CASE_HIGH (min_label
) != NULL_TREE
3791 && tree_int_cst_compare (CASE_HIGH (min_label
), vr_min
) >= 0)
3792 CASE_HIGH (min_label
)
3793 = int_const_binop (MINUS_EXPR
, vr_min
, one_cst
);
3795 /* If OP's value range is ~[2,8] and the high label range is
3796 7 ... 10, truncate the label's range to 9 ... 10. */
3797 if (tree_int_cst_compare (CASE_LOW (max_label
), vr_max
) <= 0
3798 && CASE_HIGH (max_label
) != NULL_TREE
3799 && tree_int_cst_compare (CASE_HIGH (max_label
), vr_max
) > 0)
3800 CASE_LOW (max_label
)
3801 = int_const_binop (PLUS_EXPR
, vr_max
, one_cst
);
3805 /* Canonicalize singleton case ranges. */
3806 if (tree_int_cst_equal (CASE_LOW (min_label
), CASE_HIGH (min_label
)))
3807 CASE_HIGH (min_label
) = NULL_TREE
;
3808 if (tree_int_cst_equal (CASE_LOW (max_label
), CASE_HIGH (max_label
)))
3809 CASE_HIGH (max_label
) = NULL_TREE
;
3812 /* We can also eliminate case labels that lie completely outside OP's value
3815 /* Bail out if this is just all edges taken. */
3821 /* Build a new vector of taken case labels. */
3822 vec2
= make_tree_vec (j
- i
+ 1 + l
- k
+ 1 + (int)take_default
);
3825 /* Add the default edge, if necessary. */
3827 TREE_VEC_ELT (vec2
, n2
++) = gimple_switch_default_label (stmt
);
3829 for (; i
<= j
; ++i
, ++n2
)
3830 TREE_VEC_ELT (vec2
, n2
) = gimple_switch_label (stmt
, i
);
3832 for (; k
<= l
; ++k
, ++n2
)
3833 TREE_VEC_ELT (vec2
, n2
) = gimple_switch_label (stmt
, k
);
3835 /* Mark needed edges. */
3836 for (i
= 0; i
< n2
; ++i
)
3838 e
= find_edge (gimple_bb (stmt
),
3839 label_to_block (cfun
,
3840 CASE_LABEL (TREE_VEC_ELT (vec2
, i
))));
3841 e
->aux
= (void *)-1;
3844 /* Queue not needed edges for later removal. */
3845 FOR_EACH_EDGE (e
, ei
, gimple_bb (stmt
)->succs
)
3847 if (e
->aux
== (void *)-1)
3853 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3855 fprintf (dump_file
, "removing unreachable case label\n");
3857 to_remove_edges
.safe_push (e
);
3858 e
->flags
&= ~EDGE_EXECUTABLE
;
3859 e
->flags
|= EDGE_IGNORE
;
3862 /* And queue an update for the stmt. */
3865 to_update_switch_stmts
.safe_push (su
);
3870 vr_values::cleanup_edges_and_switches (void)
3876 /* Remove dead edges from SWITCH_EXPR optimization. This leaves the
3877 CFG in a broken state and requires a cfg_cleanup run. */
3878 FOR_EACH_VEC_ELT (to_remove_edges
, i
, e
)
3881 /* Update SWITCH_EXPR case label vector. */
3882 FOR_EACH_VEC_ELT (to_update_switch_stmts
, i
, su
)
3885 size_t n
= TREE_VEC_LENGTH (su
->vec
);
3887 gimple_switch_set_num_labels (su
->stmt
, n
);
3888 for (j
= 0; j
< n
; j
++)
3889 gimple_switch_set_label (su
->stmt
, j
, TREE_VEC_ELT (su
->vec
, j
));
3890 /* As we may have replaced the default label with a regular one
3891 make sure to make it a real default label again. This ensures
3892 optimal expansion. */
3893 label
= gimple_switch_label (su
->stmt
, 0);
3894 CASE_LOW (label
) = NULL_TREE
;
3895 CASE_HIGH (label
) = NULL_TREE
;
3898 if (!to_remove_edges
.is_empty ())
3900 free_dominance_info (CDI_DOMINATORS
);
3901 loops_state_set (LOOPS_NEED_FIXUP
);
3904 to_remove_edges
.release ();
3905 to_update_switch_stmts
.release ();
3908 /* Simplify an integral conversion from an SSA name in STMT. */
3911 simplify_conversion_using_ranges (gimple_stmt_iterator
*gsi
, gimple
*stmt
)
3913 tree innerop
, middleop
, finaltype
;
3915 signop inner_sgn
, middle_sgn
, final_sgn
;
3916 unsigned inner_prec
, middle_prec
, final_prec
;
3917 widest_int innermin
, innermed
, innermax
, middlemin
, middlemed
, middlemax
;
3919 finaltype
= TREE_TYPE (gimple_assign_lhs (stmt
));
3920 if (!INTEGRAL_TYPE_P (finaltype
))
3922 middleop
= gimple_assign_rhs1 (stmt
);
3923 def_stmt
= SSA_NAME_DEF_STMT (middleop
);
3924 if (!is_gimple_assign (def_stmt
)
3925 || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt
)))
3927 innerop
= gimple_assign_rhs1 (def_stmt
);
3928 if (TREE_CODE (innerop
) != SSA_NAME
3929 || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (innerop
))
3932 /* Get the value-range of the inner operand. Use get_range_info in
3933 case innerop was created during substitute-and-fold. */
3934 wide_int imin
, imax
;
3935 if (!INTEGRAL_TYPE_P (TREE_TYPE (innerop
))
3936 || get_range_info (innerop
, &imin
, &imax
) != VR_RANGE
)
3938 innermin
= widest_int::from (imin
, TYPE_SIGN (TREE_TYPE (innerop
)));
3939 innermax
= widest_int::from (imax
, TYPE_SIGN (TREE_TYPE (innerop
)));
3941 /* Simulate the conversion chain to check if the result is equal if
3942 the middle conversion is removed. */
3943 inner_prec
= TYPE_PRECISION (TREE_TYPE (innerop
));
3944 middle_prec
= TYPE_PRECISION (TREE_TYPE (middleop
));
3945 final_prec
= TYPE_PRECISION (finaltype
);
3947 /* If the first conversion is not injective, the second must not
3949 if (wi::gtu_p (innermax
- innermin
,
3950 wi::mask
<widest_int
> (middle_prec
, false))
3951 && middle_prec
< final_prec
)
3953 /* We also want a medium value so that we can track the effect that
3954 narrowing conversions with sign change have. */
3955 inner_sgn
= TYPE_SIGN (TREE_TYPE (innerop
));
3956 if (inner_sgn
== UNSIGNED
)
3957 innermed
= wi::shifted_mask
<widest_int
> (1, inner_prec
- 1, false);
3960 if (wi::cmp (innermin
, innermed
, inner_sgn
) >= 0
3961 || wi::cmp (innermed
, innermax
, inner_sgn
) >= 0)
3962 innermed
= innermin
;
3964 middle_sgn
= TYPE_SIGN (TREE_TYPE (middleop
));
3965 middlemin
= wi::ext (innermin
, middle_prec
, middle_sgn
);
3966 middlemed
= wi::ext (innermed
, middle_prec
, middle_sgn
);
3967 middlemax
= wi::ext (innermax
, middle_prec
, middle_sgn
);
3969 /* Require that the final conversion applied to both the original
3970 and the intermediate range produces the same result. */
3971 final_sgn
= TYPE_SIGN (finaltype
);
3972 if (wi::ext (middlemin
, final_prec
, final_sgn
)
3973 != wi::ext (innermin
, final_prec
, final_sgn
)
3974 || wi::ext (middlemed
, final_prec
, final_sgn
)
3975 != wi::ext (innermed
, final_prec
, final_sgn
)
3976 || wi::ext (middlemax
, final_prec
, final_sgn
)
3977 != wi::ext (innermax
, final_prec
, final_sgn
))
3980 gimple_assign_set_rhs1 (stmt
, innerop
);
3981 fold_stmt (gsi
, follow_single_use_edges
);
3985 /* Simplify a conversion from integral SSA name to float in STMT. */
3988 vr_values::simplify_float_conversion_using_ranges (gimple_stmt_iterator
*gsi
,
3991 tree rhs1
= gimple_assign_rhs1 (stmt
);
3992 const value_range
*vr
= get_value_range (rhs1
);
3993 scalar_float_mode fltmode
3994 = SCALAR_FLOAT_TYPE_MODE (TREE_TYPE (gimple_assign_lhs (stmt
)));
3995 scalar_int_mode mode
;
3999 /* We can only handle constant ranges. */
4000 if (!range_int_cst_p (vr
))
4003 /* First check if we can use a signed type in place of an unsigned. */
4004 scalar_int_mode rhs_mode
= SCALAR_INT_TYPE_MODE (TREE_TYPE (rhs1
));
4005 if (TYPE_UNSIGNED (TREE_TYPE (rhs1
))
4006 && can_float_p (fltmode
, rhs_mode
, 0) != CODE_FOR_nothing
4007 && range_fits_type_p (vr
, TYPE_PRECISION (TREE_TYPE (rhs1
)), SIGNED
))
4009 /* If we can do the conversion in the current input mode do nothing. */
4010 else if (can_float_p (fltmode
, rhs_mode
,
4011 TYPE_UNSIGNED (TREE_TYPE (rhs1
))) != CODE_FOR_nothing
)
4013 /* Otherwise search for a mode we can use, starting from the narrowest
4014 integer mode available. */
4017 mode
= NARROWEST_INT_MODE
;
4020 /* If we cannot do a signed conversion to float from mode
4021 or if the value-range does not fit in the signed type
4022 try with a wider mode. */
4023 if (can_float_p (fltmode
, mode
, 0) != CODE_FOR_nothing
4024 && range_fits_type_p (vr
, GET_MODE_PRECISION (mode
), SIGNED
))
4027 /* But do not widen the input. Instead leave that to the
4028 optabs expansion code. */
4029 if (!GET_MODE_WIDER_MODE (mode
).exists (&mode
)
4030 || GET_MODE_PRECISION (mode
) > TYPE_PRECISION (TREE_TYPE (rhs1
)))
4035 /* It works, insert a truncation or sign-change before the
4036 float conversion. */
4037 tem
= make_ssa_name (build_nonstandard_integer_type
4038 (GET_MODE_PRECISION (mode
), 0));
4039 conv
= gimple_build_assign (tem
, NOP_EXPR
, rhs1
);
4040 gsi_insert_before (gsi
, conv
, GSI_SAME_STMT
);
4041 gimple_assign_set_rhs1 (stmt
, tem
);
4042 fold_stmt (gsi
, follow_single_use_edges
);
4047 /* Simplify an internal fn call using ranges if possible. */
4050 vr_values::simplify_internal_call_using_ranges (gimple_stmt_iterator
*gsi
,
4053 enum tree_code subcode
;
4054 bool is_ubsan
= false;
4056 switch (gimple_call_internal_fn (stmt
))
4058 case IFN_UBSAN_CHECK_ADD
:
4059 subcode
= PLUS_EXPR
;
4062 case IFN_UBSAN_CHECK_SUB
:
4063 subcode
= MINUS_EXPR
;
4066 case IFN_UBSAN_CHECK_MUL
:
4067 subcode
= MULT_EXPR
;
4070 case IFN_ADD_OVERFLOW
:
4071 subcode
= PLUS_EXPR
;
4073 case IFN_SUB_OVERFLOW
:
4074 subcode
= MINUS_EXPR
;
4076 case IFN_MUL_OVERFLOW
:
4077 subcode
= MULT_EXPR
;
4083 tree op0
= gimple_call_arg (stmt
, 0);
4084 tree op1
= gimple_call_arg (stmt
, 1);
4088 type
= TREE_TYPE (op0
);
4089 if (VECTOR_TYPE_P (type
))
4092 else if (gimple_call_lhs (stmt
) == NULL_TREE
)
4095 type
= TREE_TYPE (TREE_TYPE (gimple_call_lhs (stmt
)));
4096 if (!check_for_binary_op_overflow (subcode
, type
, op0
, op1
, &ovf
)
4097 || (is_ubsan
&& ovf
))
4101 location_t loc
= gimple_location (stmt
);
4103 g
= gimple_build_assign (gimple_call_lhs (stmt
), subcode
, op0
, op1
);
4106 int prec
= TYPE_PRECISION (type
);
4109 || !useless_type_conversion_p (type
, TREE_TYPE (op0
))
4110 || !useless_type_conversion_p (type
, TREE_TYPE (op1
)))
4111 utype
= build_nonstandard_integer_type (prec
, 1);
4112 if (TREE_CODE (op0
) == INTEGER_CST
)
4113 op0
= fold_convert (utype
, op0
);
4114 else if (!useless_type_conversion_p (utype
, TREE_TYPE (op0
)))
4116 g
= gimple_build_assign (make_ssa_name (utype
), NOP_EXPR
, op0
);
4117 gimple_set_location (g
, loc
);
4118 gsi_insert_before (gsi
, g
, GSI_SAME_STMT
);
4119 op0
= gimple_assign_lhs (g
);
4121 if (TREE_CODE (op1
) == INTEGER_CST
)
4122 op1
= fold_convert (utype
, op1
);
4123 else if (!useless_type_conversion_p (utype
, TREE_TYPE (op1
)))
4125 g
= gimple_build_assign (make_ssa_name (utype
), NOP_EXPR
, op1
);
4126 gimple_set_location (g
, loc
);
4127 gsi_insert_before (gsi
, g
, GSI_SAME_STMT
);
4128 op1
= gimple_assign_lhs (g
);
4130 g
= gimple_build_assign (make_ssa_name (utype
), subcode
, op0
, op1
);
4131 gimple_set_location (g
, loc
);
4132 gsi_insert_before (gsi
, g
, GSI_SAME_STMT
);
4135 g
= gimple_build_assign (make_ssa_name (type
), NOP_EXPR
,
4136 gimple_assign_lhs (g
));
4137 gimple_set_location (g
, loc
);
4138 gsi_insert_before (gsi
, g
, GSI_SAME_STMT
);
4140 g
= gimple_build_assign (gimple_call_lhs (stmt
), COMPLEX_EXPR
,
4141 gimple_assign_lhs (g
),
4142 build_int_cst (type
, ovf
));
4144 gimple_set_location (g
, loc
);
4145 gsi_replace (gsi
, g
, false);
4149 /* Return true if VAR is a two-valued variable. Set a and b with the
4150 two-values when it is true. Return false otherwise. */
4153 vr_values::two_valued_val_range_p (tree var
, tree
*a
, tree
*b
)
4155 const value_range
*vr
= get_value_range (var
);
4156 if (vr
->varying_p ()
4157 || vr
->undefined_p ()
4158 || TREE_CODE (vr
->min ()) != INTEGER_CST
4159 || TREE_CODE (vr
->max ()) != INTEGER_CST
)
4162 if (vr
->kind () == VR_RANGE
4163 && wi::to_wide (vr
->max ()) - wi::to_wide (vr
->min ()) == 1)
4170 /* ~[TYPE_MIN + 1, TYPE_MAX - 1] */
4171 if (vr
->kind () == VR_ANTI_RANGE
4172 && (wi::to_wide (vr
->min ())
4173 - wi::to_wide (vrp_val_min (TREE_TYPE (var
)))) == 1
4174 && (wi::to_wide (vrp_val_max (TREE_TYPE (var
)))
4175 - wi::to_wide (vr
->max ())) == 1)
4177 *a
= vrp_val_min (TREE_TYPE (var
));
4178 *b
= vrp_val_max (TREE_TYPE (var
));
4185 /* Simplify STMT using ranges if possible. */
4188 vr_values::simplify_stmt_using_ranges (gimple_stmt_iterator
*gsi
)
4190 gimple
*stmt
= gsi_stmt (*gsi
);
4191 if (is_gimple_assign (stmt
))
4193 enum tree_code rhs_code
= gimple_assign_rhs_code (stmt
);
4194 tree rhs1
= gimple_assign_rhs1 (stmt
);
4195 tree rhs2
= gimple_assign_rhs2 (stmt
);
4196 tree lhs
= gimple_assign_lhs (stmt
);
4197 tree val1
= NULL_TREE
, val2
= NULL_TREE
;
4198 use_operand_p use_p
;
4203 Where VAR is two-valued and LHS is used in GIMPLE_COND only
4205 LHS = VAR == VAL1 ? (CST BINOP VAL1) : (CST BINOP VAL2)
4209 Where VAR is two-valued and LHS is used in GIMPLE_COND only
4211 LHS = VAR == VAL1 ? (VAL1 BINOP CST) : (VAL2 BINOP CST) */
4213 if (TREE_CODE_CLASS (rhs_code
) == tcc_binary
4214 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1
))
4215 && ((TREE_CODE (rhs1
) == INTEGER_CST
4216 && TREE_CODE (rhs2
) == SSA_NAME
)
4217 || (TREE_CODE (rhs2
) == INTEGER_CST
4218 && TREE_CODE (rhs1
) == SSA_NAME
))
4219 && single_imm_use (lhs
, &use_p
, &use_stmt
)
4220 && gimple_code (use_stmt
) == GIMPLE_COND
)
4223 tree new_rhs1
= NULL_TREE
;
4224 tree new_rhs2
= NULL_TREE
;
4225 tree cmp_var
= NULL_TREE
;
4227 if (TREE_CODE (rhs2
) == SSA_NAME
4228 && two_valued_val_range_p (rhs2
, &val1
, &val2
))
4230 /* Optimize RHS1 OP [VAL1, VAL2]. */
4231 new_rhs1
= int_const_binop (rhs_code
, rhs1
, val1
);
4232 new_rhs2
= int_const_binop (rhs_code
, rhs1
, val2
);
4235 else if (TREE_CODE (rhs1
) == SSA_NAME
4236 && two_valued_val_range_p (rhs1
, &val1
, &val2
))
4238 /* Optimize [VAL1, VAL2] OP RHS2. */
4239 new_rhs1
= int_const_binop (rhs_code
, val1
, rhs2
);
4240 new_rhs2
= int_const_binop (rhs_code
, val2
, rhs2
);
4244 /* If we could not find two-vals or the optimzation is invalid as
4245 in divide by zero, new_rhs1 / new_rhs will be NULL_TREE. */
4246 if (new_rhs1
&& new_rhs2
)
4248 tree cond
= build2 (EQ_EXPR
, boolean_type_node
, cmp_var
, val1
);
4249 gimple_assign_set_rhs_with_ops (gsi
,
4253 update_stmt (gsi_stmt (*gsi
));
4254 fold_stmt (gsi
, follow_single_use_edges
);
4263 /* Transform EQ_EXPR, NE_EXPR into BIT_XOR_EXPR or identity
4264 if the RHS is zero or one, and the LHS are known to be boolean
4266 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
4267 return simplify_truth_ops_using_ranges (gsi
, stmt
);
4270 /* Transform TRUNC_DIV_EXPR and TRUNC_MOD_EXPR into RSHIFT_EXPR
4271 and BIT_AND_EXPR respectively if the first operand is greater
4272 than zero and the second operand is an exact power of two.
4273 Also optimize TRUNC_MOD_EXPR away if the second operand is
4274 constant and the first operand already has the right value
4276 case TRUNC_DIV_EXPR
:
4277 case TRUNC_MOD_EXPR
:
4278 if ((TREE_CODE (rhs1
) == SSA_NAME
4279 || TREE_CODE (rhs1
) == INTEGER_CST
)
4280 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
4281 return simplify_div_or_mod_using_ranges (gsi
, stmt
);
4284 /* Transform ABS (X) into X or -X as appropriate. */
4286 if (TREE_CODE (rhs1
) == SSA_NAME
4287 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
4288 return simplify_abs_using_ranges (gsi
, stmt
);
4293 /* Optimize away BIT_AND_EXPR and BIT_IOR_EXPR
4294 if all the bits being cleared are already cleared or
4295 all the bits being set are already set. */
4296 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
4297 return simplify_bit_ops_using_ranges (gsi
, stmt
);
4301 if (TREE_CODE (rhs1
) == SSA_NAME
4302 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
4303 return simplify_conversion_using_ranges (gsi
, stmt
);
4307 if (TREE_CODE (rhs1
) == SSA_NAME
4308 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1
)))
4309 return simplify_float_conversion_using_ranges (gsi
, stmt
);
4314 return simplify_min_or_max_using_ranges (gsi
, stmt
);
4320 else if (gimple_code (stmt
) == GIMPLE_COND
)
4321 return simplify_cond_using_ranges_1 (as_a
<gcond
*> (stmt
));
4322 else if (gimple_code (stmt
) == GIMPLE_SWITCH
)
4323 return simplify_switch_using_ranges (as_a
<gswitch
*> (stmt
));
4324 else if (is_gimple_call (stmt
)
4325 && gimple_call_internal_p (stmt
))
4326 return simplify_internal_call_using_ranges (gsi
, stmt
);
4331 /* Set the lattice entry for VAR to VR. */
4334 vr_values::set_vr_value (tree var
, value_range
*vr
)
4336 if (SSA_NAME_VERSION (var
) >= num_vr_values
)
4338 vr_value
[SSA_NAME_VERSION (var
)] = vr
;
4341 /* Swap the lattice entry for VAR with VR and return the old entry. */
4344 vr_values::swap_vr_value (tree var
, value_range
*vr
)
4346 if (SSA_NAME_VERSION (var
) >= num_vr_values
)
4348 std::swap (vr_value
[SSA_NAME_VERSION (var
)], vr
);